2 * This file is part of the coreboot project.
4 * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
7 * Copyright (C) 2003 Eric Biederman <ebiederm@xmission.com>
8 * Copyright (C) 2003-2004 Linux Networx
9 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
10 * Copyright (C) 2003 Ronald G. Minnich <rminnich@gmail.com>
11 * Copyright (C) 2004-2005 Li-Ta Lo <ollie@lanl.gov>
12 * Copyright (C) 2005-2006 Tyan
13 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
14 * Copyright (C) 2005-2006 Stefan Reinauer <stepan@openbios.org>
15 * Copyright (C) 2009 Myles Watson <mylesgw@gmail.com>
19 * (c) 1999--2000 Martin Mares <mj@suse.cz>
23 * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with
24 * the final architecture guidance from Tom Merritt <tjm@codegen.com>.
26 * In particular, we changed from the one-pass original version to
27 * Tom's recommended multiple-pass version. I wasn't sure about doing
28 * it with multiple passes, until I actually started doing it and saw
29 * the wisdom of Tom's recommendations...
31 * Lots of cleanups by Eric Biederman to handle bridges, and to
32 * handle resource allocation for non-PCI devices.
35 #include <console/console.h>
38 #include <device/device.h>
39 #include <device/pci.h>
40 #include <device/pci_ids.h>
43 #include <smp/spinlock.h>
45 /** Linked list of ALL devices */
46 struct device *all_devices = &dev_root;
47 /** Pointer to the last device */
48 extern struct device *last_dev;
49 /** Linked list of free resources */
50 struct resource *free_resources = NULL;
52 DECLARE_SPIN_LOCK(dev_lock)
55 * Allocate a new device structure.
57 * Allocte a new device structure and attach it to the device tree as a
58 * child of the parent bus.
60 * @param parent Parent bus the newly created device should be attached to.
61 * @param path Path to the device to be created.
62 * @return Pointer to the newly created device structure.
66 device_t alloc_dev(struct bus *parent, struct device_path *path)
72 /* Find the last child of our parent. */
73 for (child = parent->children; child && child->sibling; /* */ )
74 child = child->sibling;
76 dev = malloc(sizeof(*dev));
78 die("alloc_dev(): out of memory.\n");
80 memset(dev, 0, sizeof(*dev));
81 memcpy(&dev->path, path, sizeof(*path));
83 /* By default devices are enabled. */
86 /* Add the new device to the list of children of the bus. */
91 parent->children = dev;
93 /* Append a new device to the global device list.
94 * The list is used to find devices once everything is set up.
99 spin_unlock(&dev_lock);
104 * Round a number up to an alignment.
106 * @param val The starting value.
107 * @param roundup Alignment as a power of two.
108 * @return Rounded up number.
110 static resource_t round(resource_t val, unsigned long pow)
113 mask = (1ULL << pow) - 1ULL;
120 * Read the resources on all devices of a given bus.
122 * @param bus Bus to read the resources on.
124 static void read_resources(struct bus *bus)
126 struct device *curdev;
128 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
129 __func__, bus->secondary, bus->link_num);
131 /* Walk through all devices and find which resources they need. */
132 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
135 if (!curdev->enabled)
138 if (!curdev->ops || !curdev->ops->read_resources) {
139 printk(BIOS_ERR, "%s missing read_resources\n",
143 curdev->ops->read_resources(curdev);
145 /* Read in the resources behind the current device's links. */
146 for (link = curdev->link_list; link; link = link->next)
147 read_resources(link);
149 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
150 dev_path(bus->dev), bus->secondary, bus->link_num);
153 struct pick_largest_state {
154 struct resource *last;
155 struct device *result_dev;
156 struct resource *result;
160 static void pick_largest_resource(void *gp, struct device *dev,
161 struct resource *resource)
163 struct pick_largest_state *state = gp;
164 struct resource *last;
168 /* Be certain to pick the successor to last. */
169 if (resource == last) {
170 state->seen_last = 1;
173 if (resource->flags & IORESOURCE_FIXED)
174 return; /* Skip it. */
175 if (last && ((last->align < resource->align) ||
176 ((last->align == resource->align) &&
177 (last->size < resource->size)) ||
178 ((last->align == resource->align) &&
179 (last->size == resource->size) && (!state->seen_last)))) {
182 if (!state->result ||
183 (state->result->align < resource->align) ||
184 ((state->result->align == resource->align) &&
185 (state->result->size < resource->size))) {
186 state->result_dev = dev;
187 state->result = resource;
191 static struct device *largest_resource(struct bus *bus,
192 struct resource **result_res,
193 unsigned long type_mask,
196 struct pick_largest_state state;
198 state.last = *result_res;
199 state.result_dev = NULL;
203 search_bus_resources(bus, type_mask, type, pick_largest_resource,
206 *result_res = state.result;
207 return state.result_dev;
211 * This function is the guts of the resource allocator.
214 * - Allocate resource locations for every device.
215 * - Don't overlap, and follow the rules of bridges.
216 * - Don't overlap with resources in fixed locations.
217 * - Be efficient so we don't have ugly strategies.
220 * - Devices that have fixed addresses are the minority so don't
221 * worry about them too much. Instead only use part of the address
222 * space for devices with programmable addresses. This easily handles
223 * everything except bridges.
225 * - PCI devices are required to have their sizes and their alignments
226 * equal. In this case an optimal solution to the packing problem
227 * exists. Allocate all devices from highest alignment to least
228 * alignment or vice versa. Use this.
230 * - So we can handle more than PCI run two allocation passes on bridges. The
231 * first to see how large the resources are behind the bridge, and what
232 * their alignment requirements are. The second to assign a safe address to
233 * the devices behind the bridge. This allows us to treat a bridge as just
234 * a device with a couple of resources, and not need to special case it in
235 * the allocator. Also this allows handling of other types of bridges.
237 * @param bus The bus we are traversing.
238 * @param bridge The bridge resource which must contain the bus' resources.
239 * @param type_mask This value gets ANDed with the resource type.
240 * @param type This value must match the result of the AND.
243 static void compute_resources(struct bus *bus, struct resource *bridge,
244 unsigned long type_mask, unsigned long type)
247 struct resource *resource;
249 base = round(bridge->base, bridge->align);
251 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
252 " limit: %llx\n", dev_path(bus->dev), __func__,
253 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
254 "prefmem" : "mem", base, bridge->size, bridge->align,
255 bridge->gran, bridge->limit);
257 /* For each child which is a bridge, compute the resource needs. */
258 for (dev = bus->children; dev; dev = dev->sibling) {
259 struct resource *child_bridge;
264 /* Find the resources with matching type flags. */
265 for (child_bridge = dev->resource_list; child_bridge;
266 child_bridge = child_bridge->next) {
269 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
270 || (child_bridge->flags & type_mask) != type)
274 * Split prefetchable memory if combined. Many domains
275 * use the same address space for prefetchable memory
276 * and non-prefetchable memory. Bridges below them need
277 * it separated. Add the PREFETCH flag to the type_mask
280 link = dev->link_list;
281 while (link && link->link_num !=
282 IOINDEX_LINK(child_bridge->index))
286 printk(BIOS_ERR, "link %ld not found on %s\n",
287 IOINDEX_LINK(child_bridge->index),
291 compute_resources(link, child_bridge,
292 type_mask | IORESOURCE_PREFETCH,
293 type | (child_bridge->flags &
294 IORESOURCE_PREFETCH));
298 /* Remember we haven't found anything yet. */
302 * Walk through all the resources on the current bus and compute the
303 * amount of address space taken by them. Take granularity and
304 * alignment into account.
306 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
308 /* Size 0 resources can be skipped. */
312 /* Propagate the resource alignment to the bridge resource. */
313 if (resource->align > bridge->align)
314 bridge->align = resource->align;
316 /* Propagate the resource limit to the bridge register. */
317 if (bridge->limit > resource->limit)
318 bridge->limit = resource->limit;
320 /* Warn if it looks like APICs aren't declared. */
321 if ((resource->limit == 0xffffffff) &&
322 (resource->flags & IORESOURCE_ASSIGNED)) {
324 "Resource limit looks wrong! (no APIC?)\n");
325 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
326 dev_path(dev), resource->index, resource->limit);
329 if (resource->flags & IORESOURCE_IO) {
331 * Don't allow potential aliases over the legacy PCI
332 * expansion card addresses. The legacy PCI decodes
333 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
334 * 0x00 - 0xff can be used out of each 0x400 block of
337 if ((base & 0x300) != 0) {
338 base = (base & ~0x3ff) + 0x400;
341 * Don't allow allocations in the VGA I/O range.
342 * PCI has special cases for that.
344 else if ((base >= 0x3b0) && (base <= 0x3df)) {
348 /* Base must be aligned. */
349 base = round(base, resource->align);
350 resource->base = base;
351 base += resource->size;
353 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
354 dev_path(dev), resource->index, resource->base,
355 resource->base + resource->size - 1,
356 (resource->flags & IORESOURCE_IO) ? "io" :
357 (resource->flags & IORESOURCE_PREFETCH) ?
362 * A PCI bridge resource does not need to be a power of two size, but
363 * it does have a minimum granularity. Round the size up to that
364 * minimum granularity so we know not to place something else at an
365 * address postitively decoded by the bridge.
367 bridge->size = round(base, bridge->gran) -
368 round(bridge->base, bridge->align);
370 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
371 " limit: %llx done\n", dev_path(bus->dev), __func__,
372 (bridge->flags & IORESOURCE_IO) ? "io" :
373 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
374 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
378 * This function is the second part of the resource allocator.
380 * See the compute_resources function for a more detailed explanation.
382 * This function assigns the resources a value.
384 * @param bus The bus we are traversing.
385 * @param bridge The bridge resource which must contain the bus' resources.
386 * @param type_mask This value gets ANDed with the resource type.
387 * @param type This value must match the result of the AND.
389 * @see compute_resources
391 static void allocate_resources(struct bus *bus, struct resource *bridge,
392 unsigned long type_mask, unsigned long type)
395 struct resource *resource;
399 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
400 "limit:%llx\n", dev_path(bus->dev), __func__,
401 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
403 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
405 /* Remember we haven't found anything yet. */
409 * Walk through all the resources on the current bus and allocate them
412 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
414 /* Propagate the bridge limit to the resource register. */
415 if (resource->limit > bridge->limit)
416 resource->limit = bridge->limit;
418 /* Size 0 resources can be skipped. */
419 if (!resource->size) {
420 /* Set the base to limit so it doesn't confuse tolm. */
421 resource->base = resource->limit;
422 resource->flags |= IORESOURCE_ASSIGNED;
426 if (resource->flags & IORESOURCE_IO) {
428 * Don't allow potential aliases over the legacy PCI
429 * expansion card addresses. The legacy PCI decodes
430 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
431 * 0x00 - 0xff can be used out of each 0x400 block of
434 if ((base & 0x300) != 0) {
435 base = (base & ~0x3ff) + 0x400;
438 * Don't allow allocations in the VGA I/O range.
439 * PCI has special cases for that.
441 else if ((base >= 0x3b0) && (base <= 0x3df)) {
446 if ((round(base, resource->align) + resource->size - 1) <=
448 /* Base must be aligned. */
449 base = round(base, resource->align);
450 resource->base = base;
451 resource->flags |= IORESOURCE_ASSIGNED;
452 resource->flags &= ~IORESOURCE_STORED;
453 base += resource->size;
455 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
456 printk(BIOS_ERR, " aligned base %llx size %llx "
457 "limit %llx\n", round(base, resource->align),
458 resource->size, resource->limit);
459 printk(BIOS_ERR, " %llx needs to be <= %llx "
460 "(limit)\n", (round(base, resource->align) +
461 resource->size) - 1, resource->limit);
462 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
463 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
464 ? "Assigned: " : "", dev_path(dev),
465 resource->index, resource->base,
466 resource->base + resource->size - 1,
467 (resource->flags & IORESOURCE_IO) ? "io"
468 : (resource->flags & IORESOURCE_PREFETCH)
469 ? "prefmem" : "mem");
472 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
473 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
474 : "", dev_path(dev), resource->index, resource->base,
475 resource->size ? resource->base + resource->size - 1 :
476 resource->base, (resource->flags & IORESOURCE_IO)
477 ? "io" : (resource->flags & IORESOURCE_PREFETCH)
478 ? "prefmem" : "mem");
482 * A PCI bridge resource does not need to be a power of two size, but
483 * it does have a minimum granularity. Round the size up to that
484 * minimum granularity so we know not to place something else at an
485 * address positively decoded by the bridge.
488 bridge->flags |= IORESOURCE_ASSIGNED;
490 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
491 "gran: %d done\n", dev_path(bus->dev), __func__,
492 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
493 "prefmem" : "mem", base, bridge->size, bridge->align,
496 /* For each child which is a bridge, allocate_resources. */
497 for (dev = bus->children; dev; dev = dev->sibling) {
498 struct resource *child_bridge;
503 /* Find the resources with matching type flags. */
504 for (child_bridge = dev->resource_list; child_bridge;
505 child_bridge = child_bridge->next) {
508 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
509 (child_bridge->flags & type_mask) != type)
513 * Split prefetchable memory if combined. Many domains
514 * use the same address space for prefetchable memory
515 * and non-prefetchable memory. Bridges below them need
516 * it separated. Add the PREFETCH flag to the type_mask
519 link = dev->link_list;
520 while (link && link->link_num !=
521 IOINDEX_LINK(child_bridge->index))
524 printk(BIOS_ERR, "link %ld not found on %s\n",
525 IOINDEX_LINK(child_bridge->index),
528 allocate_resources(link, child_bridge,
529 type_mask | IORESOURCE_PREFETCH,
530 type | (child_bridge->flags &
531 IORESOURCE_PREFETCH));
536 #if CONFIG_PCI_64BIT_PREF_MEM == 1
537 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
539 #define MEM_MASK (IORESOURCE_MEM)
542 #define IO_MASK (IORESOURCE_IO)
543 #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
544 #define MEM_TYPE (IORESOURCE_MEM)
545 #define IO_TYPE (IORESOURCE_IO)
548 struct resource pref, io, mem;
551 static void constrain_resources(struct device *dev, struct constraints* limits)
553 struct device *child;
554 struct resource *res;
555 struct resource *lim;
558 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
560 /* Constrain limits based on the fixed resources of this device. */
561 for (res = dev->resource_list; res; res = res->next) {
562 if (!(res->flags & IORESOURCE_FIXED))
565 /* It makes no sense to have 0-sized, fixed resources.*/
566 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
567 "size=0!\n", dev_path(dev), res->index);
571 /* PREFETCH, MEM, or I/O - skip any others. */
572 if ((res->flags & MEM_MASK) == PREF_TYPE)
574 else if ((res->flags & MEM_MASK) == MEM_TYPE)
576 else if ((res->flags & IO_MASK) == IO_TYPE)
582 * Is it a fixed resource outside the current known region?
583 * If so, we don't have to consider it - it will be handled
584 * correctly and doesn't affect current region's limits.
586 if (((res->base + res->size -1) < lim->base)
587 || (res->base > lim->limit))
591 * Choose to be above or below fixed resources. This check is
592 * signed so that "negative" amounts of space are handled
595 if ((signed long long)(lim->limit - (res->base + res->size -1))
596 > (signed long long)(res->base - lim->base))
597 lim->base = res->base + res->size;
599 lim->limit = res->base -1;
602 /* Descend into every enabled child and look for fixed resources. */
603 for (link = dev->link_list; link; link = link->next) {
604 for (child = link->children; child; child = child->sibling) {
606 constrain_resources(child, limits);
611 static void avoid_fixed_resources(struct device *dev)
613 struct constraints limits;
614 struct resource *res;
616 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
618 /* Initialize constraints to maximum size. */
619 limits.pref.base = 0;
620 limits.pref.limit = 0xffffffffffffffffULL;
622 limits.io.limit = 0xffffffffffffffffULL;
624 limits.mem.limit = 0xffffffffffffffffULL;
626 /* Constrain the limits to dev's initial resources. */
627 for (res = dev->resource_list; res; res = res->next) {
628 if ((res->flags & IORESOURCE_FIXED))
630 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
631 dev_path(dev), res->index, res->limit);
632 if ((res->flags & MEM_MASK) == PREF_TYPE &&
633 (res->limit < limits.pref.limit))
634 limits.pref.limit = res->limit;
635 if ((res->flags & MEM_MASK) == MEM_TYPE &&
636 (res->limit < limits.mem.limit))
637 limits.mem.limit = res->limit;
638 if ((res->flags & IO_MASK) == IO_TYPE &&
639 (res->limit < limits.io.limit))
640 limits.io.limit = res->limit;
643 /* Look through the tree for fixed resources and update the limits. */
644 constrain_resources(dev, &limits);
646 /* Update dev's resources with new limits. */
647 for (res = dev->resource_list; res; res = res->next) {
648 struct resource *lim;
650 if ((res->flags & IORESOURCE_FIXED))
653 /* PREFETCH, MEM, or I/O - skip any others. */
654 if ((res->flags & MEM_MASK) == PREF_TYPE)
656 else if ((res->flags & MEM_MASK) == MEM_TYPE)
658 else if ((res->flags & IO_MASK) == IO_TYPE)
663 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
664 dev_path(dev), res->index, res->limit);
665 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
666 lim->base, lim->limit);
668 /* Is the resource outside the limits? */
669 if (lim->base > res->base)
670 res->base = lim->base;
671 if (res->limit > lim->limit)
672 res->limit = lim->limit;
676 #if CONFIG_VGA_BRIDGE_SETUP == 1
677 device_t vga_pri = 0;
678 static void set_vga_bridge_bits(void)
681 * FIXME: Modify set_vga_bridge() so it is less PCI centric!
682 * This function knows too much about PCI stuff, it should be just
683 * an iterator/visitor.
686 /* FIXME: Handle the VGA palette snooping. */
687 struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
696 for (dev = all_devices; dev; dev = dev->next) {
701 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
702 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) {
704 if (dev->on_mainboard)
709 if (dev->on_mainboard)
715 /* It isn't safe to enable other VGA cards. */
716 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
725 #if CONFIG_ONBOARD_VGA_IS_PRIMARY == 1
726 if (vga_onboard) /* Will use onboard VGA as primary. */
728 if (!vga) /* Will use last add-on adapter as primary. */
735 /* VGA is first add-on card or the only onboard VGA. */
736 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
737 /* All legacy VGA cards have MEM & I/O space registers. */
738 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
743 /* Now walk up the bridges setting the VGA enable. */
745 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
747 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
748 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
755 * Assign the computed resources to the devices on the bus.
757 * Use the device specific set_resources() method to store the computed
758 * resources to hardware. For bridge devices, the set_resources() method
759 * has to recurse into every down stream buses.
762 * assign_resources() -> device_operation::set_resources()
763 * device_operation::set_resources() -> assign_resources()
765 * @param bus Pointer to the structure for this bus.
767 void assign_resources(struct bus *bus)
769 struct device *curdev;
771 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
772 dev_path(bus->dev), bus->secondary, bus->link_num);
774 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
775 if (!curdev->enabled || !curdev->resource_list)
778 if (!curdev->ops || !curdev->ops->set_resources) {
779 printk(BIOS_ERR, "%s missing set_resources\n",
783 curdev->ops->set_resources(curdev);
785 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
786 dev_path(bus->dev), bus->secondary, bus->link_num);
790 * Enable the resources for devices on a link.
792 * Enable resources of the device by calling the device specific
793 * enable_resources() method.
795 * The parent's resources should be enabled first to avoid having enabling
796 * order problem. This is done by calling the parent's enable_resources()
797 * method before its childrens' enable_resources() methods.
799 * @param link The link whose devices' resources are to be enabled.
801 static void enable_resources(struct bus *link)
806 for (dev = link->children; dev; dev = dev->sibling) {
807 if (dev->enabled && dev->ops && dev->ops->enable_resources)
808 dev->ops->enable_resources(dev);
811 for (dev = link->children; dev; dev = dev->sibling) {
812 for (c_link = dev->link_list; c_link; c_link = c_link->next)
813 enable_resources(c_link);
818 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
820 * @param bus Pointer to the bus structure.
821 * @return 1 if the bus was successfully reset, 0 otherwise.
823 int reset_bus(struct bus *bus)
825 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
826 bus->dev->ops->reset_bus(bus);
827 bus->reset_needed = 0;
834 * Scan for devices on a bus.
836 * If there are bridges on the bus, recursively scan the buses behind the
837 * bridges. If the setting up and tuning of the bus causes a reset to be
838 * required, reset the bus and scan it again.
840 * @param busdev Pointer to the bus device.
841 * @param max Current bus number.
842 * @return The maximum bus number found, after scanning all subordinate buses.
844 unsigned int scan_bus(struct device *busdev, unsigned int max)
846 unsigned int new_max;
849 if (!busdev || !busdev->enabled || !busdev->ops ||
850 !busdev->ops->scan_bus) {
855 while (do_scan_bus) {
857 new_max = busdev->ops->scan_bus(busdev, max);
859 for (link = busdev->link_list; link; link = link->next) {
860 if (link->reset_needed) {
864 busdev->bus->reset_needed = 1;
872 * Determine the existence of devices and extend the device tree.
874 * Most of the devices in the system are listed in the mainboard devicetree.cb
875 * file. The device structures for these devices are generated at compile
876 * time by the config tool and are organized into the device tree. This
877 * function determines if the devices created at compile time actually exist
878 * in the physical system.
880 * For devices in the physical system but not listed in devicetree.cb,
881 * the device structures have to be created at run time and attached to the
884 * This function starts from the root device 'dev_root', scans the buses in
885 * the system recursively, and modifies the device tree according to the
886 * result of the probe.
888 * This function has no idea how to scan and probe buses and devices at all.
889 * It depends on the bus/device specific scan_bus() method to do it. The
890 * scan_bus() method also has to create the device structure and attach
891 * it to the device tree.
893 void dev_enumerate(void)
897 printk(BIOS_INFO, "Enumerating buses...\n");
901 show_all_devs(BIOS_SPEW, "Before device enumeration.");
902 printk(BIOS_SPEW, "Compare with tree...\n");
903 show_devs_tree(root, BIOS_SPEW, 0, 0);
905 if (root->chip_ops && root->chip_ops->enable_dev)
906 root->chip_ops->enable_dev(root);
908 if (!root->ops || !root->ops->scan_bus) {
909 printk(BIOS_ERR, "dev_root missing scan_bus operation");
913 printk(BIOS_INFO, "done\n");
917 * Configure devices on the devices tree.
919 * Starting at the root of the device tree, travel it recursively in two
920 * passes. In the first pass, we compute and allocate resources (ranges)
921 * requried by each device. In the second pass, the resources ranges are
922 * relocated to their final position and stored to the hardware.
924 * I/O resources grow upward. MEM resources grow downward.
926 * Since the assignment is hierarchical we set the values into the dev_root
929 void dev_configure(void)
931 struct resource *res;
933 struct device *child;
935 #if CONFIG_VGA_BRIDGE_SETUP == 1
936 set_vga_bridge_bits();
939 printk(BIOS_INFO, "Allocating resources...\n");
944 * Each domain should create resources which contain the entire address
945 * space for IO, MEM, and PREFMEM resources in the domain. The
946 * allocation of device resources will be done from this address space.
949 /* Read the resources for the entire tree. */
951 printk(BIOS_INFO, "Reading resources...\n");
952 read_resources(root->link_list);
953 printk(BIOS_INFO, "Done reading resources.\n");
955 print_resource_tree(root, BIOS_SPEW, "After reading.");
957 /* Compute resources for all domains. */
958 for (child = root->link_list->children; child; child = child->sibling) {
959 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
961 for (res = child->resource_list; res; res = res->next) {
962 if (res->flags & IORESOURCE_FIXED)
964 if (res->flags & IORESOURCE_PREFETCH) {
965 compute_resources(child->link_list,
966 res, MEM_MASK, PREF_TYPE);
969 if (res->flags & IORESOURCE_MEM) {
970 compute_resources(child->link_list,
971 res, MEM_MASK, MEM_TYPE);
974 if (res->flags & IORESOURCE_IO) {
975 compute_resources(child->link_list,
976 res, IO_MASK, IO_TYPE);
982 /* For all domains. */
983 for (child = root->link_list->children; child; child=child->sibling)
984 if (child->path.type == DEVICE_PATH_PCI_DOMAIN)
985 avoid_fixed_resources(child);
988 * Now we need to adjust the resources. MEM resources need to start at
989 * the highest address managable.
991 for (child = root->link_list->children; child; child = child->sibling) {
992 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
994 for (res = child->resource_list; res; res = res->next) {
995 if (!(res->flags & IORESOURCE_MEM) ||
996 res->flags & IORESOURCE_FIXED)
998 res->base = resource_max(res);
1002 /* Store the computed resource allocations into device registers ... */
1003 printk(BIOS_INFO, "Setting resources...\n");
1004 for (child = root->link_list->children; child; child = child->sibling) {
1005 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
1007 for (res = child->resource_list; res; res = res->next) {
1008 if (res->flags & IORESOURCE_FIXED)
1010 if (res->flags & IORESOURCE_PREFETCH) {
1011 allocate_resources(child->link_list,
1012 res, MEM_MASK, PREF_TYPE);
1015 if (res->flags & IORESOURCE_MEM) {
1016 allocate_resources(child->link_list,
1017 res, MEM_MASK, MEM_TYPE);
1020 if (res->flags & IORESOURCE_IO) {
1021 allocate_resources(child->link_list,
1022 res, IO_MASK, IO_TYPE);
1027 assign_resources(root->link_list);
1028 printk(BIOS_INFO, "Done setting resources.\n");
1029 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
1031 printk(BIOS_INFO, "Done allocating resources.\n");
1035 * Enable devices on the device tree.
1037 * Starting at the root, walk the tree and enable all devices/bridges by
1038 * calling the device's enable_resources() method.
1040 void dev_enable(void)
1044 printk(BIOS_INFO, "Enabling resources...\n");
1046 /* Now enable everything. */
1047 for (link = dev_root.link_list; link; link = link->next)
1048 enable_resources(link);
1050 printk(BIOS_INFO, "done.\n");
1054 * Initialize a specific device.
1056 * The parent should be initialized first to avoid having an ordering problem.
1057 * This is done by calling the parent's init() method before its childrens'
1060 * @param dev The device to be initialized.
1062 static void init_dev(struct device *dev)
1067 if (!dev->initialized && dev->ops && dev->ops->init) {
1068 if (dev->path.type == DEVICE_PATH_I2C) {
1069 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1070 dev_path(dev->bus->dev), dev->bus->link_num);
1073 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1074 dev->initialized = 1;
1075 dev->ops->init(dev);
1079 static void init_link(struct bus *link)
1084 for (dev = link->children; dev; dev = dev->sibling)
1087 for (dev = link->children; dev; dev = dev->sibling) {
1088 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1094 * Initialize all devices in the global device tree.
1096 * Starting at the root device, call the device's init() method to do
1097 * device-specific setup, then call each child's init() method.
1099 void dev_initialize(void)
1103 printk(BIOS_INFO, "Initializing devices...\n");
1105 /* First call the mainboard init. */
1106 init_dev(&dev_root);
1108 /* Now initialize everything. */
1109 for (link = dev_root.link_list; link; link = link->next)
1112 printk(BIOS_INFO, "Devices initialized\n");
1113 show_all_devs(BIOS_SPEW, "After init.");