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 #include <arch/ebda.h>
48 /** Linked list of ALL devices */
49 struct device *all_devices = &dev_root;
50 /** Pointer to the last device */
51 extern struct device *last_dev;
52 /** Linked list of free resources */
53 struct resource *free_resources = NULL;
55 DECLARE_SPIN_LOCK(dev_lock)
58 * Allocate a new device structure.
60 * Allocte a new device structure and attach it to the device tree as a
61 * child of the parent bus.
63 * @param parent Parent bus the newly created device should be attached to.
64 * @param path Path to the device to be created.
65 * @return Pointer to the newly created device structure.
69 device_t alloc_dev(struct bus *parent, struct device_path *path)
75 /* Find the last child of our parent. */
76 for (child = parent->children; child && child->sibling; /* */ )
77 child = child->sibling;
79 dev = malloc(sizeof(*dev));
81 die("alloc_dev(): out of memory.\n");
83 memset(dev, 0, sizeof(*dev));
84 memcpy(&dev->path, path, sizeof(*path));
86 /* By default devices are enabled. */
89 /* Add the new device to the list of children of the bus. */
94 parent->children = dev;
96 /* Append a new device to the global device list.
97 * The list is used to find devices once everything is set up.
102 spin_unlock(&dev_lock);
107 * Round a number up to an alignment.
109 * @param val The starting value.
110 * @param roundup Alignment as a power of two.
111 * @return Rounded up number.
113 static resource_t round(resource_t val, unsigned long pow)
116 mask = (1ULL << pow) - 1ULL;
123 * Read the resources on all devices of a given bus.
125 * @param bus Bus to read the resources on.
127 static void read_resources(struct bus *bus)
129 struct device *curdev;
131 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
132 __func__, bus->secondary, bus->link_num);
134 /* Walk through all devices and find which resources they need. */
135 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
138 if (!curdev->enabled)
141 if (!curdev->ops || !curdev->ops->read_resources) {
142 printk(BIOS_ERR, "%s missing read_resources\n",
146 curdev->ops->read_resources(curdev);
148 /* Read in the resources behind the current device's links. */
149 for (link = curdev->link_list; link; link = link->next)
150 read_resources(link);
152 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
153 dev_path(bus->dev), bus->secondary, bus->link_num);
156 struct pick_largest_state {
157 struct resource *last;
158 struct device *result_dev;
159 struct resource *result;
163 static void pick_largest_resource(void *gp, struct device *dev,
164 struct resource *resource)
166 struct pick_largest_state *state = gp;
167 struct resource *last;
171 /* Be certain to pick the successor to last. */
172 if (resource == last) {
173 state->seen_last = 1;
176 if (resource->flags & IORESOURCE_FIXED)
177 return; /* Skip it. */
178 if (last && ((last->align < resource->align) ||
179 ((last->align == resource->align) &&
180 (last->size < resource->size)) ||
181 ((last->align == resource->align) &&
182 (last->size == resource->size) && (!state->seen_last)))) {
185 if (!state->result ||
186 (state->result->align < resource->align) ||
187 ((state->result->align == resource->align) &&
188 (state->result->size < resource->size))) {
189 state->result_dev = dev;
190 state->result = resource;
194 static struct device *largest_resource(struct bus *bus,
195 struct resource **result_res,
196 unsigned long type_mask,
199 struct pick_largest_state state;
201 state.last = *result_res;
202 state.result_dev = NULL;
206 search_bus_resources(bus, type_mask, type, pick_largest_resource,
209 *result_res = state.result;
210 return state.result_dev;
214 * This function is the guts of the resource allocator.
217 * - Allocate resource locations for every device.
218 * - Don't overlap, and follow the rules of bridges.
219 * - Don't overlap with resources in fixed locations.
220 * - Be efficient so we don't have ugly strategies.
223 * - Devices that have fixed addresses are the minority so don't
224 * worry about them too much. Instead only use part of the address
225 * space for devices with programmable addresses. This easily handles
226 * everything except bridges.
228 * - PCI devices are required to have their sizes and their alignments
229 * equal. In this case an optimal solution to the packing problem
230 * exists. Allocate all devices from highest alignment to least
231 * alignment or vice versa. Use this.
233 * - So we can handle more than PCI run two allocation passes on bridges. The
234 * first to see how large the resources are behind the bridge, and what
235 * their alignment requirements are. The second to assign a safe address to
236 * the devices behind the bridge. This allows us to treat a bridge as just
237 * a device with a couple of resources, and not need to special case it in
238 * the allocator. Also this allows handling of other types of bridges.
240 * @param bus The bus we are traversing.
241 * @param bridge The bridge resource which must contain the bus' resources.
242 * @param type_mask This value gets ANDed with the resource type.
243 * @param type This value must match the result of the AND.
246 static void compute_resources(struct bus *bus, struct resource *bridge,
247 unsigned long type_mask, unsigned long type)
250 struct resource *resource;
252 base = round(bridge->base, bridge->align);
254 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
255 " limit: %llx\n", dev_path(bus->dev), __func__,
256 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
257 "prefmem" : "mem", base, bridge->size, bridge->align,
258 bridge->gran, bridge->limit);
260 /* For each child which is a bridge, compute the resource needs. */
261 for (dev = bus->children; dev; dev = dev->sibling) {
262 struct resource *child_bridge;
267 /* Find the resources with matching type flags. */
268 for (child_bridge = dev->resource_list; child_bridge;
269 child_bridge = child_bridge->next) {
272 if (!(child_bridge->flags & IORESOURCE_BRIDGE)
273 || (child_bridge->flags & type_mask) != type)
277 * Split prefetchable memory if combined. Many domains
278 * use the same address space for prefetchable memory
279 * and non-prefetchable memory. Bridges below them need
280 * it separated. Add the PREFETCH flag to the type_mask
283 link = dev->link_list;
284 while (link && link->link_num !=
285 IOINDEX_LINK(child_bridge->index))
289 printk(BIOS_ERR, "link %ld not found on %s\n",
290 IOINDEX_LINK(child_bridge->index),
294 compute_resources(link, child_bridge,
295 type_mask | IORESOURCE_PREFETCH,
296 type | (child_bridge->flags &
297 IORESOURCE_PREFETCH));
301 /* Remember we haven't found anything yet. */
305 * Walk through all the resources on the current bus and compute the
306 * amount of address space taken by them. Take granularity and
307 * alignment into account.
309 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
311 /* Size 0 resources can be skipped. */
315 /* Propagate the resource alignment to the bridge resource. */
316 if (resource->align > bridge->align)
317 bridge->align = resource->align;
319 /* Propagate the resource limit to the bridge register. */
320 if (bridge->limit > resource->limit)
321 bridge->limit = resource->limit;
323 /* Warn if it looks like APICs aren't declared. */
324 if ((resource->limit == 0xffffffff) &&
325 (resource->flags & IORESOURCE_ASSIGNED)) {
327 "Resource limit looks wrong! (no APIC?)\n");
328 printk(BIOS_ERR, "%s %02lx limit %08llx\n",
329 dev_path(dev), resource->index, resource->limit);
332 if (resource->flags & IORESOURCE_IO) {
334 * Don't allow potential aliases over the legacy PCI
335 * expansion card addresses. The legacy PCI decodes
336 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
337 * 0x00 - 0xff can be used out of each 0x400 block of
340 if ((base & 0x300) != 0) {
341 base = (base & ~0x3ff) + 0x400;
344 * Don't allow allocations in the VGA I/O range.
345 * PCI has special cases for that.
347 else if ((base >= 0x3b0) && (base <= 0x3df)) {
351 /* Base must be aligned. */
352 base = round(base, resource->align);
353 resource->base = base;
354 base += resource->size;
356 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
357 dev_path(dev), resource->index, resource->base,
358 resource->base + resource->size - 1,
359 (resource->flags & IORESOURCE_IO) ? "io" :
360 (resource->flags & IORESOURCE_PREFETCH) ?
365 * A PCI bridge resource does not need to be a power of two size, but
366 * it does have a minimum granularity. Round the size up to that
367 * minimum granularity so we know not to place something else at an
368 * address postitively decoded by the bridge.
370 bridge->size = round(base, bridge->gran) -
371 round(bridge->base, bridge->align);
373 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
374 " limit: %llx done\n", dev_path(bus->dev), __func__,
375 (bridge->flags & IORESOURCE_IO) ? "io" :
376 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
377 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
381 * This function is the second part of the resource allocator.
383 * See the compute_resources function for a more detailed explanation.
385 * This function assigns the resources a value.
387 * @param bus The bus we are traversing.
388 * @param bridge The bridge resource which must contain the bus' resources.
389 * @param type_mask This value gets ANDed with the resource type.
390 * @param type This value must match the result of the AND.
392 * @see compute_resources
394 static void allocate_resources(struct bus *bus, struct resource *bridge,
395 unsigned long type_mask, unsigned long type)
398 struct resource *resource;
402 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
403 "limit:%llx\n", dev_path(bus->dev), __func__,
404 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
406 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
408 /* Remember we haven't found anything yet. */
412 * Walk through all the resources on the current bus and allocate them
415 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
417 /* Propagate the bridge limit to the resource register. */
418 if (resource->limit > bridge->limit)
419 resource->limit = bridge->limit;
421 /* Size 0 resources can be skipped. */
422 if (!resource->size) {
423 /* Set the base to limit so it doesn't confuse tolm. */
424 resource->base = resource->limit;
425 resource->flags |= IORESOURCE_ASSIGNED;
429 if (resource->flags & IORESOURCE_IO) {
431 * Don't allow potential aliases over the legacy PCI
432 * expansion card addresses. The legacy PCI decodes
433 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
434 * 0x00 - 0xff can be used out of each 0x400 block of
437 if ((base & 0x300) != 0) {
438 base = (base & ~0x3ff) + 0x400;
441 * Don't allow allocations in the VGA I/O range.
442 * PCI has special cases for that.
444 else if ((base >= 0x3b0) && (base <= 0x3df)) {
449 if ((round(base, resource->align) + resource->size - 1) <=
451 /* Base must be aligned. */
452 base = round(base, resource->align);
453 resource->base = base;
454 resource->flags |= IORESOURCE_ASSIGNED;
455 resource->flags &= ~IORESOURCE_STORED;
456 base += resource->size;
458 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
459 printk(BIOS_ERR, " aligned base %llx size %llx "
460 "limit %llx\n", round(base, resource->align),
461 resource->size, resource->limit);
462 printk(BIOS_ERR, " %llx needs to be <= %llx "
463 "(limit)\n", (round(base, resource->align) +
464 resource->size) - 1, resource->limit);
465 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx]"
466 " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
467 ? "Assigned: " : "", dev_path(dev),
468 resource->index, resource->base,
469 resource->base + resource->size - 1,
470 (resource->flags & IORESOURCE_IO) ? "io"
471 : (resource->flags & IORESOURCE_PREFETCH)
472 ? "prefmem" : "mem");
475 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
476 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
477 : "", dev_path(dev), resource->index, resource->base,
478 resource->size ? resource->base + resource->size - 1 :
479 resource->base, (resource->flags & IORESOURCE_IO)
480 ? "io" : (resource->flags & IORESOURCE_PREFETCH)
481 ? "prefmem" : "mem");
485 * A PCI bridge resource does not need to be a power of two size, but
486 * it does have a minimum granularity. Round the size up to that
487 * minimum granularity so we know not to place something else at an
488 * address positively decoded by the bridge.
491 bridge->flags |= IORESOURCE_ASSIGNED;
493 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
494 "gran: %d done\n", dev_path(bus->dev), __func__,
495 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
496 "prefmem" : "mem", base, bridge->size, bridge->align,
499 /* For each child which is a bridge, allocate_resources. */
500 for (dev = bus->children; dev; dev = dev->sibling) {
501 struct resource *child_bridge;
506 /* Find the resources with matching type flags. */
507 for (child_bridge = dev->resource_list; child_bridge;
508 child_bridge = child_bridge->next) {
511 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
512 (child_bridge->flags & type_mask) != type)
516 * Split prefetchable memory if combined. Many domains
517 * use the same address space for prefetchable memory
518 * and non-prefetchable memory. Bridges below them need
519 * it separated. Add the PREFETCH flag to the type_mask
522 link = dev->link_list;
523 while (link && link->link_num !=
524 IOINDEX_LINK(child_bridge->index))
527 printk(BIOS_ERR, "link %ld not found on %s\n",
528 IOINDEX_LINK(child_bridge->index),
531 allocate_resources(link, child_bridge,
532 type_mask | IORESOURCE_PREFETCH,
533 type | (child_bridge->flags &
534 IORESOURCE_PREFETCH));
539 #if CONFIG_PCI_64BIT_PREF_MEM == 1
540 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
542 #define MEM_MASK (IORESOURCE_MEM)
545 #define IO_MASK (IORESOURCE_IO)
546 #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
547 #define MEM_TYPE (IORESOURCE_MEM)
548 #define IO_TYPE (IORESOURCE_IO)
551 struct resource pref, io, mem;
554 static void constrain_resources(struct device *dev, struct constraints* limits)
556 struct device *child;
557 struct resource *res;
558 struct resource *lim;
561 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
563 /* Constrain limits based on the fixed resources of this device. */
564 for (res = dev->resource_list; res; res = res->next) {
565 if (!(res->flags & IORESOURCE_FIXED))
568 /* It makes no sense to have 0-sized, fixed resources.*/
569 printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
570 "size=0!\n", dev_path(dev), res->index);
574 /* PREFETCH, MEM, or I/O - skip any others. */
575 if ((res->flags & MEM_MASK) == PREF_TYPE)
577 else if ((res->flags & MEM_MASK) == MEM_TYPE)
579 else if ((res->flags & IO_MASK) == IO_TYPE)
585 * Is it a fixed resource outside the current known region?
586 * If so, we don't have to consider it - it will be handled
587 * correctly and doesn't affect current region's limits.
589 if (((res->base + res->size -1) < lim->base)
590 || (res->base > lim->limit))
594 * Choose to be above or below fixed resources. This check is
595 * signed so that "negative" amounts of space are handled
598 if ((signed long long)(lim->limit - (res->base + res->size -1))
599 > (signed long long)(res->base - lim->base))
600 lim->base = res->base + res->size;
602 lim->limit = res->base -1;
605 /* Descend into every enabled child and look for fixed resources. */
606 for (link = dev->link_list; link; link = link->next) {
607 for (child = link->children; child; child = child->sibling) {
609 constrain_resources(child, limits);
614 static void avoid_fixed_resources(struct device *dev)
616 struct constraints limits;
617 struct resource *res;
619 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
621 /* Initialize constraints to maximum size. */
622 limits.pref.base = 0;
623 limits.pref.limit = 0xffffffffffffffffULL;
625 limits.io.limit = 0xffffffffffffffffULL;
627 limits.mem.limit = 0xffffffffffffffffULL;
629 /* Constrain the limits to dev's initial resources. */
630 for (res = dev->resource_list; res; res = res->next) {
631 if ((res->flags & IORESOURCE_FIXED))
633 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
634 dev_path(dev), res->index, res->limit);
635 if ((res->flags & MEM_MASK) == PREF_TYPE &&
636 (res->limit < limits.pref.limit))
637 limits.pref.limit = res->limit;
638 if ((res->flags & MEM_MASK) == MEM_TYPE &&
639 (res->limit < limits.mem.limit))
640 limits.mem.limit = res->limit;
641 if ((res->flags & IO_MASK) == IO_TYPE &&
642 (res->limit < limits.io.limit))
643 limits.io.limit = res->limit;
646 /* Look through the tree for fixed resources and update the limits. */
647 constrain_resources(dev, &limits);
649 /* Update dev's resources with new limits. */
650 for (res = dev->resource_list; res; res = res->next) {
651 struct resource *lim;
653 if ((res->flags & IORESOURCE_FIXED))
656 /* PREFETCH, MEM, or I/O - skip any others. */
657 if ((res->flags & MEM_MASK) == PREF_TYPE)
659 else if ((res->flags & MEM_MASK) == MEM_TYPE)
661 else if ((res->flags & IO_MASK) == IO_TYPE)
666 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
667 dev_path(dev), res->index, res->limit);
668 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
669 lim->base, lim->limit);
671 /* Is the resource outside the limits? */
672 if (lim->base > res->base)
673 res->base = lim->base;
674 if (res->limit > lim->limit)
675 res->limit = lim->limit;
679 #if CONFIG_VGA_BRIDGE_SETUP == 1
680 device_t vga_pri = 0;
681 static void set_vga_bridge_bits(void)
684 * FIXME: Modify set_vga_bridge() so it is less PCI centric!
685 * This function knows too much about PCI stuff, it should be just
686 * an iterator/visitor.
689 /* FIXME: Handle the VGA palette snooping. */
690 struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
699 for (dev = all_devices; dev; dev = dev->next) {
704 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
705 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) {
707 if (dev->on_mainboard)
712 if (dev->on_mainboard)
718 /* It isn't safe to enable other VGA cards. */
719 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
728 #if CONFIG_ONBOARD_VGA_IS_PRIMARY == 1
729 if (vga_onboard) /* Will use onboard VGA as primary. */
731 if (!vga) /* Will use last add-on adapter as primary. */
738 /* VGA is first add-on card or the only onboard VGA. */
739 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
740 /* All legacy VGA cards have MEM & I/O space registers. */
741 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
746 /* Now walk up the bridges setting the VGA enable. */
748 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
750 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
751 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
758 * Assign the computed resources to the devices on the bus.
760 * Use the device specific set_resources() method to store the computed
761 * resources to hardware. For bridge devices, the set_resources() method
762 * has to recurse into every down stream buses.
765 * assign_resources() -> device_operation::set_resources()
766 * device_operation::set_resources() -> assign_resources()
768 * @param bus Pointer to the structure for this bus.
770 void assign_resources(struct bus *bus)
772 struct device *curdev;
774 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
775 dev_path(bus->dev), bus->secondary, bus->link_num);
777 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
778 if (!curdev->enabled || !curdev->resource_list)
781 if (!curdev->ops || !curdev->ops->set_resources) {
782 printk(BIOS_ERR, "%s missing set_resources\n",
786 curdev->ops->set_resources(curdev);
788 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
789 dev_path(bus->dev), bus->secondary, bus->link_num);
793 * Enable the resources for devices on a link.
795 * Enable resources of the device by calling the device specific
796 * enable_resources() method.
798 * The parent's resources should be enabled first to avoid having enabling
799 * order problem. This is done by calling the parent's enable_resources()
800 * method before its childrens' enable_resources() methods.
802 * @param link The link whose devices' resources are to be enabled.
804 static void enable_resources(struct bus *link)
809 for (dev = link->children; dev; dev = dev->sibling) {
810 if (dev->enabled && dev->ops && dev->ops->enable_resources)
811 dev->ops->enable_resources(dev);
814 for (dev = link->children; dev; dev = dev->sibling) {
815 for (c_link = dev->link_list; c_link; c_link = c_link->next)
816 enable_resources(c_link);
821 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
823 * @param bus Pointer to the bus structure.
824 * @return 1 if the bus was successfully reset, 0 otherwise.
826 int reset_bus(struct bus *bus)
828 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
829 bus->dev->ops->reset_bus(bus);
830 bus->reset_needed = 0;
837 * Scan for devices on a bus.
839 * If there are bridges on the bus, recursively scan the buses behind the
840 * bridges. If the setting up and tuning of the bus causes a reset to be
841 * required, reset the bus and scan it again.
843 * @param busdev Pointer to the bus device.
844 * @param max Current bus number.
845 * @return The maximum bus number found, after scanning all subordinate buses.
847 unsigned int scan_bus(struct device *busdev, unsigned int max)
849 unsigned int new_max;
852 if (!busdev || !busdev->enabled || !busdev->ops ||
853 !busdev->ops->scan_bus) {
858 while (do_scan_bus) {
860 new_max = busdev->ops->scan_bus(busdev, max);
862 for (link = busdev->link_list; link; link = link->next) {
863 if (link->reset_needed) {
867 busdev->bus->reset_needed = 1;
875 * Determine the existence of devices and extend the device tree.
877 * Most of the devices in the system are listed in the mainboard devicetree.cb
878 * file. The device structures for these devices are generated at compile
879 * time by the config tool and are organized into the device tree. This
880 * function determines if the devices created at compile time actually exist
881 * in the physical system.
883 * For devices in the physical system but not listed in devicetree.cb,
884 * the device structures have to be created at run time and attached to the
887 * This function starts from the root device 'dev_root', scans the buses in
888 * the system recursively, and modifies the device tree according to the
889 * result of the probe.
891 * This function has no idea how to scan and probe buses and devices at all.
892 * It depends on the bus/device specific scan_bus() method to do it. The
893 * scan_bus() method also has to create the device structure and attach
894 * it to the device tree.
896 void dev_enumerate(void)
900 printk(BIOS_INFO, "Enumerating buses...\n");
904 show_all_devs(BIOS_SPEW, "Before device enumeration.");
905 printk(BIOS_SPEW, "Compare with tree...\n");
906 show_devs_tree(root, BIOS_SPEW, 0, 0);
908 if (root->chip_ops && root->chip_ops->enable_dev)
909 root->chip_ops->enable_dev(root);
911 if (!root->ops || !root->ops->scan_bus) {
912 printk(BIOS_ERR, "dev_root missing scan_bus operation");
916 printk(BIOS_INFO, "done\n");
920 * Configure devices on the devices tree.
922 * Starting at the root of the device tree, travel it recursively in two
923 * passes. In the first pass, we compute and allocate resources (ranges)
924 * requried by each device. In the second pass, the resources ranges are
925 * relocated to their final position and stored to the hardware.
927 * I/O resources grow upward. MEM resources grow downward.
929 * Since the assignment is hierarchical we set the values into the dev_root
932 void dev_configure(void)
934 struct resource *res;
936 struct device *child;
938 #if CONFIG_VGA_BRIDGE_SETUP == 1
939 set_vga_bridge_bits();
942 printk(BIOS_INFO, "Allocating resources...\n");
947 * Each domain should create resources which contain the entire address
948 * space for IO, MEM, and PREFMEM resources in the domain. The
949 * allocation of device resources will be done from this address space.
952 /* Read the resources for the entire tree. */
954 printk(BIOS_INFO, "Reading resources...\n");
955 read_resources(root->link_list);
956 printk(BIOS_INFO, "Done reading resources.\n");
958 print_resource_tree(root, BIOS_SPEW, "After reading.");
960 /* Compute resources for all domains. */
961 for (child = root->link_list->children; child; child = child->sibling) {
962 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
964 for (res = child->resource_list; res; res = res->next) {
965 if (res->flags & IORESOURCE_FIXED)
967 if (res->flags & IORESOURCE_PREFETCH) {
968 compute_resources(child->link_list,
969 res, MEM_MASK, PREF_TYPE);
972 if (res->flags & IORESOURCE_MEM) {
973 compute_resources(child->link_list,
974 res, MEM_MASK, MEM_TYPE);
977 if (res->flags & IORESOURCE_IO) {
978 compute_resources(child->link_list,
979 res, IO_MASK, IO_TYPE);
985 /* For all domains. */
986 for (child = root->link_list->children; child; child=child->sibling)
987 if (child->path.type == DEVICE_PATH_PCI_DOMAIN)
988 avoid_fixed_resources(child);
991 * Now we need to adjust the resources. MEM resources need to start at
992 * the highest address managable.
994 for (child = root->link_list->children; child; child = child->sibling) {
995 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
997 for (res = child->resource_list; res; res = res->next) {
998 if (!(res->flags & IORESOURCE_MEM) ||
999 res->flags & IORESOURCE_FIXED)
1001 res->base = resource_max(res);
1005 /* Store the computed resource allocations into device registers ... */
1006 printk(BIOS_INFO, "Setting resources...\n");
1007 for (child = root->link_list->children; child; child = child->sibling) {
1008 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
1010 for (res = child->resource_list; res; res = res->next) {
1011 if (res->flags & IORESOURCE_FIXED)
1013 if (res->flags & IORESOURCE_PREFETCH) {
1014 allocate_resources(child->link_list,
1015 res, MEM_MASK, PREF_TYPE);
1018 if (res->flags & IORESOURCE_MEM) {
1019 allocate_resources(child->link_list,
1020 res, MEM_MASK, MEM_TYPE);
1023 if (res->flags & IORESOURCE_IO) {
1024 allocate_resources(child->link_list,
1025 res, IO_MASK, IO_TYPE);
1030 assign_resources(root->link_list);
1031 printk(BIOS_INFO, "Done setting resources.\n");
1032 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
1034 printk(BIOS_INFO, "Done allocating resources.\n");
1038 * Enable devices on the device tree.
1040 * Starting at the root, walk the tree and enable all devices/bridges by
1041 * calling the device's enable_resources() method.
1043 void dev_enable(void)
1047 printk(BIOS_INFO, "Enabling resources...\n");
1049 /* Now enable everything. */
1050 for (link = dev_root.link_list; link; link = link->next)
1051 enable_resources(link);
1053 printk(BIOS_INFO, "done.\n");
1057 * Initialize a specific device.
1059 * The parent should be initialized first to avoid having an ordering problem.
1060 * This is done by calling the parent's init() method before its childrens'
1063 * @param dev The device to be initialized.
1065 static void init_dev(struct device *dev)
1070 if (!dev->initialized && dev->ops && dev->ops->init) {
1071 if (dev->path.type == DEVICE_PATH_I2C) {
1072 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1073 dev_path(dev->bus->dev), dev->bus->link_num);
1076 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1077 dev->initialized = 1;
1078 dev->ops->init(dev);
1082 static void init_link(struct bus *link)
1087 for (dev = link->children; dev; dev = dev->sibling)
1090 for (dev = link->children; dev; dev = dev->sibling) {
1091 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1097 * Initialize all devices in the global device tree.
1099 * Starting at the root device, call the device's init() method to do
1100 * device-specific setup, then call each child's init() method.
1102 void dev_initialize(void)
1106 printk(BIOS_INFO, "Initializing devices...\n");
1109 /* Ensure EBDA is prepared before Option ROMs. */
1110 setup_default_ebda();
1113 /* First call the mainboard init. */
1114 init_dev(&dev_root);
1116 /* Now initialize everything. */
1117 for (link = dev_root.link_list; link; link = link->next)
1120 printk(BIOS_INFO, "Devices initialized\n");
1121 show_all_devs(BIOS_SPEW, "After init.");