2 * (c) 1999--2000 Martin Mares <mj@suse.cz>
3 * (c) 2003 Eric Biederman <ebiederm@xmission.com>
4 * (c) 2003 Linux Networx
6 /* lots of mods by ron minnich (rminnich@lanl.gov), with
7 * the final architecture guidance from Tom Merritt (tjm@codegen.com)
8 * In particular, we changed from the one-pass original version to
9 * Tom's recommended multiple-pass version. I wasn't sure about doing
10 * it with multiple passes, until I actually started doing it and saw
11 * the wisdom of Tom's recommendations ...
13 * Lots of cleanups by Eric Biederman to handle bridges, and to
14 * handle resource allocation for non-pci devices.
17 #include <console/console.h>
20 #include <device/device.h>
21 #include <device/pci.h>
22 #include <device/pci_ids.h>
25 #include <smp/spinlock.h>
27 /** Linked list of ALL devices */
28 struct device *all_devices = &dev_root;
29 /** Pointer to the last device */
30 static struct device **last_dev_p = &dev_root.next;
32 /** The upper limit of MEM resource of the devices.
33 * Reserve 20M for the system */
34 #define DEVICE_MEM_HIGH 0xFEBFFFFFUL
35 /** The lower limit of IO resource of the devices.
36 * Reserve 4k for ISA/Legacy devices */
37 #define DEVICE_IO_START 0x1000
40 * @brief Allocate a new device structure.
42 * Allocte a new device structure and attached it to the device tree as a
43 * child of the parent bus.
45 * @param parent parent bus the newly created device attached to.
46 * @param path path to the device to be created.
48 * @return pointer to the newly created device structure.
52 device_t alloc_dev(struct bus *parent, struct device_path *path)
58 /* Find the last child of our parent */
59 for(child = parent->children; child && child->sibling; ) {
60 child = child->sibling;
62 dev = malloc(sizeof(*dev));
64 die("DEV: out of memory.\n");
66 memset(dev, 0, sizeof(*dev));
67 memcpy(&dev->path, path, sizeof(*path));
70 /* Initialize the back pointers in the link fields */
71 for(link = 0; link < MAX_LINKS; link++) {
72 dev->link[link].dev = dev;
73 dev->link[link].link = link;
76 /* By default devices are enabled */
79 /* Add the new device to the list of children of the bus. */
84 parent->children = dev;
87 /* Append a new device to the global device list.
88 * The list is used to find devices once everything is set up.
91 last_dev_p = &dev->next;
93 spin_unlock(&dev_lock);
98 * @brief round a number up to an alignment.
99 * @param val the starting value
100 * @param roundup Alignment as a power of two
101 * @returns rounded up number
103 static unsigned long round(unsigned long val, unsigned long roundup)
105 /* ROUNDUP MUST BE A POWER OF TWO. */
106 unsigned long inverse;
107 inverse = ~(roundup - 1);
108 val += (roundup - 1);
113 static unsigned long round_down(unsigned long val, unsigned long round_down)
115 /* ROUND_DOWN MUST BE A POWER OF TWO. */
116 unsigned long inverse;
117 inverse = ~(round_down - 1);
123 /** Read the resources on all devices of a given bus.
124 * @param bus bus to read the resources on.
126 static void read_resources(struct bus *bus)
128 struct device *curdev;
130 /* Walk through all of the devices and find which resources they need. */
131 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
134 if (curdev->resources > 0) {
137 if (!curdev->ops || !curdev->ops->read_resources) {
138 printk_err("%s missing read_resources\n",
142 if (!curdev->enabled) {
145 curdev->ops->read_resources(curdev);
146 /* Read in subtractive resources behind the current device */
148 for(i = 0; i < curdev->resources; i++) {
149 struct resource *resource;
150 resource = &curdev->resource[i];
151 if ((resource->flags & IORESOURCE_SUBTRACTIVE) &&
152 (!(links & (1 << resource->index))))
154 links |= (1 << resource->index);
155 read_resources(&curdev->link[resource->index]);
162 struct pick_largest_state {
163 struct resource *last;
164 struct device *result_dev;
165 struct resource *result;
169 static void pick_largest_resource(struct pick_largest_state *state,
170 struct device *dev, struct resource *resource)
172 struct resource *last;
174 /* Be certain to pick the successor to last */
175 if (resource == last) {
176 state->seen_last = 1;
180 (last->align < resource->align) ||
181 ((last->align == resource->align) &&
182 (last->size < resource->size)) ||
183 ((last->align == resource->align) &&
184 (last->size == resource->size) &&
185 (!state->seen_last)))) {
188 if (!state->result ||
189 (state->result->align < resource->align) ||
190 ((state->result->align == resource->align) &&
191 (state->result->size < resource->size))) {
192 state->result_dev = dev;
193 state->result = resource;
197 static void find_largest_resource(struct pick_largest_state *state,
198 struct bus *bus, unsigned long type_mask, unsigned long type)
200 struct device *curdev;
201 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
203 for(i = 0; i < curdev->resources; i++) {
204 struct resource *resource = &curdev->resource[i];
205 /* If it isn't the right kind of resource ignore it */
206 if ((resource->flags & type_mask) != type) {
209 /* If it is a subtractive resource recurse */
210 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
212 subbus = &curdev->link[resource->index];
213 find_largest_resource(state, subbus, type_mask, type);
216 /* See if this is the largest resource */
217 pick_largest_resource(state, curdev, resource);
222 static struct device *largest_resource(struct bus *bus,
223 struct resource **result_res,
224 unsigned long type_mask,
227 struct pick_largest_state state;
229 state.last = *result_res;
230 state.result_dev = 0;
234 find_largest_resource(&state, bus, type_mask, type);
236 *result_res = state.result;
237 return state.result_dev;
240 /* Compute allocate resources is the guts of the resource allocator.
243 * - Allocate resources locations for every device.
244 * - Don't overlap, and follow the rules of bridges.
245 * - Don't overlap with resources in fixed locations.
246 * - Be efficient so we don't have ugly strategies.
249 * - Devices that have fixed addresses are the minority so don't
250 * worry about them too much. Instead only use part of the address
251 * space for devices with programmable addresses. This easily handles
252 * everything except bridges.
254 * - PCI devices are required to have thier sizes and their alignments
255 * equal. In this case an optimal solution to the packing problem
256 * exists. Allocate all devices from highest alignment to least
257 * alignment or vice versa. Use this.
259 * - So we can handle more than PCI run two allocation passes on
260 * bridges. The first to see how large the resources are behind
261 * the bridge, and what their alignment requirements are. The
262 * second to assign a safe address to the devices behind the
263 * bridge. This allows me to treat a bridge as just a device with
264 * a couple of resources, and not need to special case it in the
265 * allocator. Also this allows handling of other types of bridges.
269 void compute_allocate_resource(
271 struct resource *bridge,
272 unsigned long type_mask,
276 struct resource *resource;
278 unsigned long align, min_align;
282 printk_spew("%s compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d\n",
284 (bridge->flags & IORESOURCE_IO)? "io":
285 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
286 base, bridge->size, bridge->align, bridge->gran);
289 /* We want different minimum alignments for different kinds of
290 * resources. These minimums are not device type specific
291 * but resource type specific.
293 if (bridge->flags & IORESOURCE_IO) {
294 min_align = log2(DEVICE_IO_ALIGN);
296 if (bridge->flags & IORESOURCE_MEM) {
297 min_align = log2(DEVICE_MEM_ALIGN);
300 /* Make certain I have read in all of the resources */
303 /* Remember I haven't found anything yet. */
306 /* Walk through all the devices on the current bus and
307 * compute the addresses.
309 while((dev = largest_resource(bus, &resource, type_mask, type))) {
311 /* Do NOT I repeat do not ignore resources which have zero size.
312 * If they need to be ignored dev->read_resources should not even
313 * return them. Some resources must be set even when they have
314 * no size. PCI bridge resources are a good example of this.
317 /* Propogate the resource alignment to the bridge register */
318 if (resource->align > bridge->align) {
319 bridge->align = resource->align;
322 /* Propogate the resource limit to the bridge register */
323 if (bridge->limit > resource->limit) {
324 bridge->limit = resource->limit;
326 /* Artificially deny limits between DEVICE_MEM_HIGH and 0xffffffff */
327 if ((bridge->limit > DEVICE_MEM_HIGH) && (bridge->limit <= 0xffffffff)) {
328 bridge->limit = DEVICE_MEM_HIGH;
331 /* Make certain we are dealing with a good minimum size */
332 size = resource->size;
333 align = resource->align;
334 if (align < min_align) {
337 if (resource->flags & IORESOURCE_FIXED) {
340 if (resource->flags & IORESOURCE_IO) {
341 /* Don't allow potential aliases over the
342 * legacy pci expansion card addresses.
343 * The legacy pci decodes only 10 bits,
344 * uses 100h - 3ffh. Therefor, only 0 - ff
345 * can be used out of each 400h block of io
348 if ((base & 0x300) != 0) {
349 base = (base & ~0x3ff) + 0x400;
351 /* Don't allow allocations in the VGA IO range.
352 * PCI has special cases for that.
354 else if ((base >= 0x3b0) && (base <= 0x3df)) {
358 if (((round(base, align) + size) -1) <= resource->limit) {
359 /* base must be aligned to size */
360 base = round(base, align);
361 resource->base = base;
362 resource->flags |= IORESOURCE_ASSIGNED;
363 resource->flags &= ~IORESOURCE_STORED;
367 "%s %02x * [0x%08Lx - 0x%08Lx] %s\n",
371 resource->base + resource->size - 1,
372 (resource->flags & IORESOURCE_IO)? "io":
373 (resource->flags & IORESOURCE_PREFETCH)? "prefmem": "mem");
376 /* A pci bridge resource does not need to be a power
377 * of two size, but it does have a minimum granularity.
378 * Round the size up to that minimum granularity so we
379 * know not to place something else at an address postitively
380 * decoded by the bridge.
382 bridge->size = round(base, bridge->gran) - bridge->base;
384 printk_spew("%s compute_allocate_%s: base: %08lx size: %08lx align: %d gran: %d done\n",
386 (bridge->flags & IORESOURCE_IO)? "io":
387 (bridge->flags & IORESOURCE_PREFETCH)? "prefmem" : "mem",
388 base, bridge->size, bridge->align, bridge->gran);
393 static void allocate_vga_resource(void)
395 #warning "FIXME modify allocate_vga_resource so it is less pci centric!"
396 #warning "This function knows to much about PCI stuff, it should be just a ietrator/visitor."
398 /* FIXME handle the VGA pallette snooping */
399 struct device *dev, *vga;
403 for(dev = all_devices; dev; dev = dev->next) {
404 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
405 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER))
408 printk_debug("Allocating VGA resource %s\n",
413 /* All legacy VGA cards have MEM & I/O space registers */
414 dev->command |= PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
416 /* It isn't safe to enable other VGA cards */
417 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
424 /* Now walk up the bridges setting the VGA enable */
426 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
427 bus = (bus == bus->dev->bus)? 0 : bus->dev->bus;
432 /** Assign the computed resources to the bridges and devices on the bus.
433 * Recurse to any bridges found on this bus first. Then do the devices
436 * @param bus Pointer to the structure for this bus
438 void assign_resources(struct bus *bus)
440 struct device *curdev;
442 printk_spew("%s assign_resources, bus %d link: %d\n",
443 dev_path(bus->dev), bus->secondary, bus->link);
445 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
446 if (!curdev->enabled || !curdev->resources) {
449 if (!curdev->ops || !curdev->ops->set_resources) {
450 printk_err("%s missing set_resources\n",
454 curdev->ops->set_resources(curdev);
456 printk_spew("%s assign_resources, bus %d link: %d\n",
457 dev_path(bus->dev), bus->secondary, bus->link);
461 * @brief Enable the resources for a specific device
463 * @param dev the device whose resources are to be enabled
465 * Enable resources of the device by calling the device specific
466 * enable_resources() method.
468 * The parent's resources should be enabled first to avoid having enabling
469 * order problem. This is done by calling the parent's enable_resources()
470 * method and let that method to call it's children's enable_resoruces() via
471 * enable_childrens_resources().
473 * Indirect mutual recursion:
475 void enable_resources(struct device *dev)
480 if (!dev->ops || !dev->ops->enable_resources) {
481 printk_err("%s missing enable_resources\n", dev_path(dev));
484 dev->ops->enable_resources(dev);
488 * @brief Determine the existence of dynamic devices and construct dynamic
491 * Start from the root device 'dev_root', scan the buses in the system
492 * recursively, build the dynamic device tree according to the result
495 * This function has no idea how to scan and probe buses and devices at all.
496 * It depends on the bus/device specific scan_bus() method to do it. The
497 * scan_bus() function also has to create the device structure and attach
498 * it to the device tree.
500 void dev_enumerate(void)
503 unsigned subordinate;
504 printk_info("Enumerating buses...\n");
506 if (!root->ops || !root->ops->scan_bus) {
507 printk_err("dev_root missing scan_bus operation");
510 subordinate = root->ops->scan_bus(root, 0);
511 printk_info("done\n");
516 * @brief Configure devices on the devices tree.
518 * Starting at the root of the dynamic device tree, travel recursively,
519 * and compute resources needed by each device and allocate them.
521 * I/O resources start at DEVICE_IO_START and grow upward. MEM resources start
522 * at DEVICE_MEM_START and grow downward.
524 * Since the assignment is hierarchical we set the values into the dev_root
527 void dev_configure(void)
529 struct resource *io, *mem;
532 printk_info("Allocating resources...\n");
535 if (!root->ops || !root->ops->read_resources) {
536 printk_err("dev_root missing read_resources\n");
539 if (!root->ops || !root->ops->set_resources) {
540 printk_err("dev_root missing set_resources\n");
543 root->ops->read_resources(root);
545 /* Get the resources */
546 io = &root->resource[0];
547 mem = &root->resource[1];
548 /* Make certain the io devices are allocated somewhere safe. */
549 io->base = DEVICE_IO_START;
550 io->flags |= IORESOURCE_ASSIGNED;
551 io->flags &= ~IORESOURCE_STORED;
552 /* Now reallocate the pci resources memory with the
553 * highest addresses I can manage.
555 mem->base = resource_max(&root->resource[1]);
556 mem->flags |= IORESOURCE_ASSIGNED;
557 mem->flags &= ~IORESOURCE_STORED;
559 /* Allocate the VGA I/O resource.. */
560 allocate_vga_resource();
562 /* Store the computed resource allocations into device registers ... */
563 root->ops->set_resources(root);
566 mem->flags |= IORESOURCE_STORED;
567 report_resource_stored(root, mem, "");
570 printk_info("done.\n");
574 * @brief Enable devices on the device tree.
576 * Starting at the root, walk the tree and enable all devices/bridges by
577 * calling the device's enable_resources() method.
579 void dev_enable(void)
581 printk_info("Enabling resourcess...\n");
583 /* now enable everything. */
584 enable_resources(&dev_root);
586 printk_info("done.\n");
590 * @brief Initialize all devices in the global device list.
592 * Starting at the first device on the global device link list,
593 * walk the list and call a driver to do device specific setup.
595 void dev_initialize(void)
599 printk_info("Initializing devices...\n");
600 for(dev = all_devices; dev; dev = dev->next) {
601 if (dev->enabled && !dev->initialized &&
602 dev->ops && dev->ops->init)
604 printk_debug("%s init\n", dev_path(dev));
605 dev->initialized = 1;
609 printk_info("Devices initialized\n");