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>
21 /* lots of mods by ron minnich (rminnich@lanl.gov), with
22 * the final architecture guidance from Tom Merritt (tjm@codegen.com)
23 * In particular, we changed from the one-pass original version to
24 * Tom's recommended multiple-pass version. I wasn't sure about doing
25 * it with multiple passes, until I actually started doing it and saw
26 * the wisdom of Tom's recommendations ...
28 * Lots of cleanups by Eric Biederman to handle bridges, and to
29 * handle resource allocation for non-pci devices.
32 #include <console/console.h>
35 #include <device/device.h>
36 #include <device/pci.h>
37 #include <device/pci_ids.h>
40 #include <smp/spinlock.h>
42 /** Linked list of ALL devices */
43 struct device *all_devices = &dev_root;
44 /** Pointer to the last device */
45 extern struct device *last_dev;
46 /** Linked list of free resources */
47 struct resource *free_resources = NULL;
51 * @brief Allocate a new device structure.
53 * Allocte a new device structure and attached it to the device tree as a
54 * child of the parent bus.
56 * @param parent parent bus the newly created device attached to.
57 * @param path path to the device to be created.
59 * @return pointer to the newly created device structure.
64 DECLARE_SPIN_LOCK(dev_lock)
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;
77 dev = malloc(sizeof(*dev));
79 die("DEV: out of memory.\n");
81 memset(dev, 0, sizeof(*dev));
82 memcpy(&dev->path, path, sizeof(*path));
84 /* By default devices are enabled. */
87 /* Add the new device to the list of children of the bus. */
92 parent->children = dev;
95 /* Append a new device to the global device list.
96 * The list is used to find devices once everything is set up.
101 spin_unlock(&dev_lock);
106 * @brief round a number up to an alignment.
107 * @param val the starting value
108 * @param roundup Alignment as a power of two
109 * @returns rounded up number
111 static resource_t round(resource_t val, unsigned long pow)
114 mask = (1ULL << pow) - 1ULL;
120 /** Read the resources on all devices of a given bus.
121 * @param bus bus to read the resources on.
123 static void read_resources(struct bus *bus)
125 struct device *curdev;
127 printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev), __func__,
128 bus->secondary, bus->link_num);
130 /* Walk through all devices and find which resources they need. */
131 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
133 if (!curdev->enabled) {
136 if (!curdev->ops || !curdev->ops->read_resources) {
137 printk(BIOS_ERR, "%s missing read_resources\n",
141 curdev->ops->read_resources(curdev);
143 /* Read in the resources behind the current device's links. */
144 for (link = curdev->link_list; link; link = link->next)
145 read_resources(link);
147 printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
148 dev_path(bus->dev), bus->secondary, bus->link_num);
151 struct pick_largest_state {
152 struct resource *last;
153 struct device *result_dev;
154 struct resource *result;
158 static void pick_largest_resource(void *gp, struct device *dev,
159 struct resource *resource)
161 struct pick_largest_state *state = gp;
162 struct resource *last;
166 /* Be certain to pick the successor to last. */
167 if (resource == last) {
168 state->seen_last = 1;
171 if (resource->flags & IORESOURCE_FIXED)
173 if (last && ((last->align < resource->align) ||
174 ((last->align == resource->align) &&
175 (last->size < resource->size)) ||
176 ((last->align == resource->align) &&
177 (last->size == resource->size) && (!state->seen_last)))) {
180 if (!state->result ||
181 (state->result->align < resource->align) ||
182 ((state->result->align == resource->align) &&
183 (state->result->size < resource->size))) {
184 state->result_dev = dev;
185 state->result = resource;
189 static struct device *largest_resource(struct bus *bus,
190 struct resource **result_res,
191 unsigned long type_mask,
194 struct pick_largest_state state;
196 state.last = *result_res;
197 state.result_dev = NULL;
201 search_bus_resources(bus, type_mask, type, pick_largest_resource,
204 *result_res = state.result;
205 return state.result_dev;
208 /* Compute allocate resources is the guts of the resource allocator.
211 * - Allocate resource locations for every device.
212 * - Don't overlap, and follow the rules of bridges.
213 * - Don't overlap with resources in fixed locations.
214 * - Be efficient so we don't have ugly strategies.
217 * - Devices that have fixed addresses are the minority so don't
218 * worry about them too much. Instead only use part of the address
219 * space for devices with programmable addresses. This easily handles
220 * everything except bridges.
222 * - PCI devices are required to have their sizes and their alignments
223 * equal. In this case an optimal solution to the packing problem
224 * exists. Allocate all devices from highest alignment to least
225 * alignment or vice versa. Use this.
227 * - So we can handle more than PCI run two allocation passes on bridges. The
228 * first to see how large the resources are behind the bridge, and what
229 * their alignment requirements are. The second to assign a safe address to
230 * the devices behind the bridge. This allows us to treat a bridge as just
231 * a device with a couple of resources, and not need to special case it in
232 * the allocator. Also this allows handling of other types of bridges.
235 static void compute_resources(struct bus *bus, struct resource *bridge,
236 unsigned long type_mask, unsigned long type)
239 struct resource *resource;
241 base = round(bridge->base, bridge->align);
243 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d limit: %llx\n",
244 dev_path(bus->dev), __func__,
245 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
247 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
249 /* For each child which is a bridge, compute_resource_needs. */
250 for (dev = bus->children; dev; dev = dev->sibling) {
251 struct resource *child_bridge;
256 /* Find the resources with matching type flags. */
257 for (child_bridge = dev->resource_list; child_bridge;
258 child_bridge = child_bridge->next) {
261 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
262 (child_bridge->flags & type_mask) != type)
265 /* Split prefetchable memory if combined. Many domains
266 * use the same address space for prefetchable memory
267 * and non-prefetchable memory. Bridges below them
268 * need it separated. Add the PREFETCH flag to the
269 * type_mask and type.
271 link = dev->link_list;
272 while (link && link->link_num !=
273 IOINDEX_LINK(child_bridge->index))
276 printk(BIOS_ERR, "link %ld not found on %s\n",
277 IOINDEX_LINK(child_bridge->index),
279 compute_resources(link, child_bridge,
280 type_mask | IORESOURCE_PREFETCH,
281 type | (child_bridge->flags &
282 IORESOURCE_PREFETCH));
286 /* Remember we haven't found anything yet. */
289 /* Walk through all the resources on the current bus and compute the
290 * amount of address space taken by them. Take granularity and
291 * alignment into account.
293 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
295 /* Size 0 resources can be skipped. */
296 if (!resource->size) {
300 /* Propagate the resource alignment to the bridge resource. */
301 if (resource->align > bridge->align) {
302 bridge->align = resource->align;
305 /* Propagate the resource limit to the bridge register. */
306 if (bridge->limit > resource->limit) {
307 bridge->limit = resource->limit;
310 /* Warn if it looks like APICs aren't declared. */
311 if ((resource->limit == 0xffffffff) &&
312 (resource->flags & IORESOURCE_ASSIGNED)) {
313 printk(BIOS_ERR, "Resource limit looks wrong! (no APIC?)\n");
314 printk(BIOS_ERR, "%s %02lx limit %08Lx\n", dev_path(dev),
315 resource->index, resource->limit);
318 if (resource->flags & IORESOURCE_IO) {
319 /* Don't allow potential aliases over the legacy PCI
320 * expansion card addresses. The legacy PCI decodes
321 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
322 * 0x00 - 0xff can be used out of each 0x400 block of
325 if ((base & 0x300) != 0) {
326 base = (base & ~0x3ff) + 0x400;
328 /* Don't allow allocations in the VGA I/O range.
329 * PCI has special cases for that.
331 else if ((base >= 0x3b0) && (base <= 0x3df)) {
335 /* Base must be aligned. */
336 base = round(base, resource->align);
337 resource->base = base;
338 base += resource->size;
340 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
341 dev_path(dev), resource->index,
343 resource->base + resource->size - 1,
344 (resource->flags & IORESOURCE_IO) ? "io" :
345 (resource->flags & IORESOURCE_PREFETCH) ?
348 /* A pci bridge resource does not need to be a power
349 * of two size, but it does have a minimum granularity.
350 * Round the size up to that minimum granularity so we
351 * know not to place something else at an address postitively
352 * decoded by the bridge.
354 bridge->size = round(base, bridge->gran) -
355 round(bridge->base, bridge->align);
357 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d limit: %llx done\n",
358 dev_path(bus->dev), __func__,
359 (bridge->flags & IORESOURCE_IO) ? "io" :
360 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
361 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
365 * This function is the second part of the resource allocator.
368 * - Allocate resource locations for every device.
369 * - Don't overlap, and follow the rules of bridges.
370 * - Don't overlap with resources in fixed locations.
371 * - Be efficient so we don't have ugly strategies.
374 * - Devices that have fixed addresses are the minority so don't
375 * worry about them too much. Instead only use part of the address
376 * space for devices with programmable addresses. This easily handles
377 * everything except bridges.
379 * - PCI devices are required to have their sizes and their alignments
380 * equal. In this case an optimal solution to the packing problem
381 * exists. Allocate all devices from highest alignment to least
382 * alignment or vice versa. Use this.
384 * - So we can handle more than PCI run two allocation passes on bridges. The
385 * first to see how large the resources are behind the bridge, and what
386 * their alignment requirements are. The second to assign a safe address to
387 * the devices behind the bridge. This allows us to treat a bridge as just
388 * a device with a couple of resources, and not need to special case it in
389 * the allocator. Also this allows handling of other types of bridges.
391 * - This function assigns the resources a value.
393 * @param bus The bus we are traversing.
394 * @param bridge The bridge resource which must contain the bus' resources.
395 * @param type_mask This value gets anded with the resource type.
396 * @param type This value must match the result of the and.
398 static void allocate_resources(struct bus *bus, struct resource *bridge,
399 unsigned long type_mask, unsigned long type)
402 struct resource *resource;
406 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d limit:%llx\n",
407 dev_path(bus->dev), __func__,
408 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
410 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
412 /* Remember we haven't found anything yet. */
415 /* Walk through all the resources on the current bus and allocate them
418 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
420 /* Propagate the bridge limit to the resource register. */
421 if (resource->limit > bridge->limit) {
422 resource->limit = bridge->limit;
425 /* Size 0 resources can be skipped. */
426 if (!resource->size) {
427 /* Set the base to limit so it doesn't confuse tolm. */
428 resource->base = resource->limit;
429 resource->flags |= IORESOURCE_ASSIGNED;
433 if (resource->flags & IORESOURCE_IO) {
434 /* Don't allow potential aliases over the legacy PCI
435 * expansion card addresses. The legacy PCI decodes
436 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
437 * 0x00 - 0xff can be used out of each 0x400 block of
440 if ((base & 0x300) != 0) {
441 base = (base & ~0x3ff) + 0x400;
443 /* Don't allow allocations in the VGA I/O range.
444 * PCI has special cases for that.
446 else if ((base >= 0x3b0) && (base <= 0x3df)) {
451 if ((round(base, resource->align) + resource->size - 1) <=
453 /* Base must be aligned. */
454 base = round(base, resource->align);
455 resource->base = base;
456 resource->flags |= IORESOURCE_ASSIGNED;
457 resource->flags &= ~IORESOURCE_STORED;
458 base += resource->size;
460 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
461 printk(BIOS_ERR, " aligned base %llx size %llx limit %llx\n",
462 round(base, resource->align), resource->size,
464 printk(BIOS_ERR, " %llx needs to be <= %llx (limit)\n",
465 (round(base, resource->align) +
466 resource->size) - 1, resource->limit);
467 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx] %s\n",
469 flags & IORESOURCE_ASSIGNED) ? "Assigned: " :
470 "", dev_path(dev), resource->index,
472 resource->base + resource->size - 1,
474 flags & IORESOURCE_IO) ? "io" : (resource->
477 ? "prefmem" : "mem");
480 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
481 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
483 dev_path(dev), resource->index, resource->base,
484 resource->size ? resource->base + resource->size - 1 :
486 (resource->flags & IORESOURCE_IO) ? "io" :
487 (resource->flags & IORESOURCE_PREFETCH) ? "prefmem" :
490 /* A PCI bridge resource does not need to be a power of two size, but
491 * it does have a minimum granularity. Round the size up to that
492 * minimum granularity so we know not to place something else at an
493 * address positively decoded by the bridge.
496 bridge->flags |= IORESOURCE_ASSIGNED;
498 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d gran: %d done\n",
499 dev_path(bus->dev), __func__,
500 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
502 base, bridge->size, bridge->align, bridge->gran);
504 /* For each child which is a bridge, allocate_resources. */
505 for (dev = bus->children; dev; dev = dev->sibling) {
506 struct resource *child_bridge;
511 /* Find the resources with matching type flags. */
512 for (child_bridge = dev->resource_list; child_bridge;
513 child_bridge = child_bridge->next) {
516 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
517 (child_bridge->flags & type_mask) != type)
520 /* Split prefetchable memory if combined. Many domains
521 * use the same address space for prefetchable memory
522 * and non-prefetchable memory. Bridges below them
523 * need it separated. Add the PREFETCH flag to the
524 * type_mask and type.
526 link = dev->link_list;
527 while (link && link->link_num !=
528 IOINDEX_LINK(child_bridge->index))
531 printk(BIOS_ERR, "link %ld not found on %s\n",
532 IOINDEX_LINK(child_bridge->index),
534 allocate_resources(link, child_bridge,
535 type_mask | IORESOURCE_PREFETCH,
536 type | (child_bridge->flags &
537 IORESOURCE_PREFETCH));
542 #if CONFIG_PCI_64BIT_PREF_MEM == 1
543 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
545 #define MEM_MASK (IORESOURCE_MEM)
547 #define IO_MASK (IORESOURCE_IO)
548 #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
549 #define MEM_TYPE (IORESOURCE_MEM)
550 #define IO_TYPE (IORESOURCE_IO)
553 struct resource pref, io, mem;
556 static void constrain_resources(struct device *dev, struct constraints* limits)
558 struct device *child;
559 struct resource *res;
560 struct resource *lim;
563 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
565 /* Constrain limits based on the fixed resources of this device. */
566 for (res = dev->resource_list; res; res = res->next) {
567 if (!(res->flags & IORESOURCE_FIXED))
570 /* It makes no sense to have 0-sized, fixed resources.*/
571 printk(BIOS_ERR, "skipping %s@%lx fixed resource, size=0!\n",
572 dev_path(dev), res->index);
576 /* PREFETCH, MEM, or I/O - skip any others. */
577 if ((res->flags & MEM_MASK) == PREF_TYPE)
579 else if ((res->flags & MEM_MASK) == MEM_TYPE)
581 else if ((res->flags & IO_MASK) == IO_TYPE)
586 /* Is it a fixed resource outside the current known region?
587 If so, we don't have to consider it - it will be handled
588 correctly and doesn't affect current region's limits */
589 if (((res->base + res->size -1) < lim->base) || (res->base > lim->limit))
592 /* Choose to be above or below fixed resources. This
593 * check is signed so that "negative" amounts of space
594 * are handled correctly.
596 if ((signed long long)(lim->limit - (res->base + res->size -1)) >
597 (signed long long)(res->base - lim->base))
598 lim->base = res->base + res->size;
600 lim->limit = res->base -1;
603 /* Descend into every enabled child and look for fixed resources. */
604 for (link = dev->link_list; link; link = link->next)
605 for (child = link->children; child;
606 child = child->sibling)
608 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));
617 /* 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 %08Lx\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 %08Lx\n", __func__,
664 dev_path(dev), res->index, res->limit);
665 printk(BIOS_SPEW, "\tlim->base %08Lx lim->limit %08Lx\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;
694 for (dev = all_devices; dev; dev = dev->next) {
697 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
698 ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) {
700 if (dev->on_mainboard) {
706 if (dev->on_mainboard) {
713 /* It isn't safe to enable other VGA cards. */
714 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
723 #if CONFIG_CONSOLE_VGA_ONBOARD_AT_FIRST == 1
724 if (vga_onboard) // Will use on board VGA as pri.
726 if (!vga) // Will use last add on adapter as pri.
733 /* VGA is first add on card or the only onboard VGA. */
734 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
735 /* All legacy VGA cards have MEM & I/O space registers. */
736 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
740 /* Now walk up the bridges setting the VGA enable. */
742 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
744 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
745 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
752 * @brief Assign the computed resources to the devices on the bus.
754 * @param bus Pointer to the structure for this bus
756 * Use the device specific set_resources method to store the computed
757 * resources to hardware. For bridge devices, the set_resources() method
758 * has to recurse into every down stream buses.
761 * assign_resources() -> device_operation::set_resources()
762 * device_operation::set_resources() -> assign_resources()
764 void assign_resources(struct bus *bus)
766 struct device *curdev;
768 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
769 dev_path(bus->dev), bus->secondary, bus->link_num);
771 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
772 if (!curdev->enabled || !curdev->resource_list) {
775 if (!curdev->ops || !curdev->ops->set_resources) {
776 printk(BIOS_ERR, "%s missing set_resources\n",
780 curdev->ops->set_resources(curdev);
782 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
783 dev_path(bus->dev), bus->secondary, bus->link_num);
787 * @brief Enable the resources for devices on a link
789 * @param link the link whose devices' resources are to be enabled
791 * Enable resources of the device by calling the device specific
792 * enable_resources() method.
794 * The parent's resources should be enabled first to avoid having enabling
795 * order problem. This is done by calling the parent's enable_resources()
796 * method before its childrens' enable_resources() methods.
799 static void enable_resources(struct bus *link)
804 for (dev = link->children; dev; dev = dev->sibling) {
805 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
806 dev->ops->enable_resources(dev);
810 for (dev = link->children; dev; dev = dev->sibling) {
811 for (c_link = dev->link_list; c_link; c_link = c_link->next) {
812 enable_resources(c_link);
818 * @brief Reset all of the devices a bus
820 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
822 * @param bus pointer to the bus structure
824 * @return 1 if the bus was successfully reset, 0 otherwise.
827 int reset_bus(struct bus *bus)
829 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
830 bus->dev->ops->reset_bus(bus);
831 bus->reset_needed = 0;
838 * @brief Scan for devices on a bus.
840 * If there are bridges on the bus, recursively scan the buses behind the
841 * bridges. If the setting up and tuning of the bus causes a reset to be
842 * required, reset the bus and scan it again.
844 * @param busdev Pointer to the bus device.
845 * @param max Current bus number.
846 * @return The maximum bus number found, after scanning all subordinate buses.
848 unsigned int scan_bus(struct device *busdev, unsigned int max)
850 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) {
864 if (reset_bus(link)) {
867 busdev->bus->reset_needed = 1;
876 * @brief Determine the existence of devices and extend the device tree.
878 * Most of the devices in the system are listed in the mainboard Config.lb
879 * file. The device structures for these devices are generated at compile
880 * time by the config tool and are organized into the device tree. This
881 * function determines if the devices created at compile time actually exist
882 * in the physical system.
884 * For devices in the physical system but not listed in the Config.lb file,
885 * the device structures have to be created at run time and attached to the
888 * This function starts from the root device 'dev_root', scan the buses in
889 * the system recursively, modify the device tree according to the result of
892 * This function has no idea how to scan and probe buses and devices at all.
893 * It depends on the bus/device specific scan_bus() method to do it. The
894 * scan_bus() method also has to create the device structure and attach
895 * it to the device tree.
897 void dev_enumerate(void)
900 printk(BIOS_INFO, "Enumerating buses...\n");
903 show_all_devs(BIOS_SPEW, "Before Device Enumeration.");
904 printk(BIOS_SPEW, "Compare with tree...\n");
905 show_devs_tree(root, BIOS_SPEW, 0, 0);
907 if (root->chip_ops && root->chip_ops->enable_dev) {
908 root->chip_ops->enable_dev(root);
910 if (!root->ops || !root->ops->scan_bus) {
911 printk(BIOS_ERR, "dev_root missing scan_bus operation");
915 printk(BIOS_INFO, "done\n");
919 * @brief Configure devices on the devices tree.
921 * Starting at the root of the device tree, travel it recursively in two
922 * passes. In the first pass, we compute and allocate resources (ranges)
923 * requried by each device. In the second pass, the resources ranges are
924 * relocated to their final position and stored to the hardware.
926 * I/O resources grow upward. MEM resources grow downward.
928 * Since the assignment is hierarchical we set the values into the dev_root
931 void dev_configure(void)
933 struct resource *res;
935 struct device *child;
937 #if CONFIG_VGA_BRIDGE_SETUP == 1
938 set_vga_bridge_bits();
941 printk(BIOS_INFO, "Allocating resources...\n");
945 /* Each domain should create resources which contain the entire address
946 * space for IO, MEM, and PREFMEM resources in the domain. The
947 * allocation of device resources will be done from this address space.
950 /* Read the resources for the entire tree. */
952 printk(BIOS_INFO, "Reading resources...\n");
953 read_resources(root->link_list);
954 printk(BIOS_INFO, "Done reading resources.\n");
956 print_resource_tree(root, BIOS_SPEW, "After reading.");
958 /* Compute resources for all domains. */
959 for (child = root->link_list->children; child; child = child->sibling) {
960 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
962 for (res = child->resource_list; res; res = res->next) {
963 if (res->flags & IORESOURCE_FIXED)
965 if (res->flags & IORESOURCE_PREFETCH) {
966 compute_resources(child->link_list,
967 res, MEM_MASK, PREF_TYPE);
970 if (res->flags & IORESOURCE_MEM) {
971 compute_resources(child->link_list,
972 res, MEM_MASK, MEM_TYPE);
975 if (res->flags & IORESOURCE_IO) {
976 compute_resources(child->link_list,
977 res, IO_MASK, IO_TYPE);
983 /* For all domains. */
984 for (child = root->link_list->children; child; child=child->sibling)
985 if (child->path.type == DEVICE_PATH_PCI_DOMAIN)
986 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 * @brief 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 * @brief Initialize a specific device
1056 * @param dev the device to be initialized
1058 * The parent should be initialized first to avoid having an ordering
1059 * problem. This is done by calling the parent's init()
1060 * method before its childrens' init() methods.
1063 static void init_dev(struct device *dev)
1065 if (!dev->enabled) {
1069 if (!dev->initialized && dev->ops && dev->ops->init) {
1070 if (dev->path.type == DEVICE_PATH_I2C) {
1071 printk(BIOS_DEBUG, "smbus: %s[%d]->",
1072 dev_path(dev->bus->dev), dev->bus->link_num);
1075 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1076 dev->initialized = 1;
1077 dev->ops->init(dev);
1081 static void init_link(struct bus *link)
1086 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) {
1098 * @brief Initialize all devices in the global device tree.
1100 * Starting at the root device, call the device's init() method to do device-
1101 * specific setup, then call each child's init() method.
1103 void dev_initialize(void)
1107 printk(BIOS_INFO, "Initializing devices...\n");
1109 /* First call the mainboard init. */
1110 init_dev(&dev_root);
1112 /* now initialize everything. */
1113 for (link = dev_root.link_list; link; link = link->next)
1116 printk(BIOS_INFO, "Devices initialized\n");
1117 show_all_devs(BIOS_SPEW, "After init.");