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;
49 DECLARE_SPIN_LOCK(dev_lock)
52 * Allocate a new device structure.
54 * Allocte a new device structure and attach it to the device tree as a
55 * child of the parent bus.
57 * @param parent Parent bus the newly created device should be attached to.
58 * @param path Path to the device to be created.
59 * @return Pointer to the newly created device structure.
63 device_t alloc_dev(struct bus *parent, struct device_path *path)
69 /* Find the last child of our parent. */
70 for (child = parent->children; child && child->sibling; /* */ ) {
71 child = child->sibling;
74 dev = malloc(sizeof(*dev));
76 die("DEV: out of memory.\n");
78 memset(dev, 0, sizeof(*dev));
79 memcpy(&dev->path, path, sizeof(*path));
81 /* By default devices are enabled. */
84 /* Add the new device to the list of children of the bus. */
89 parent->children = dev;
92 /* Append a new device to the global device list.
93 * The list is used to find devices once everything is set up.
98 spin_unlock(&dev_lock);
103 * Round a number up to an alignment.
105 * @param val The starting value.
106 * @param roundup Alignment as a power of two.
107 * @return Rounded up number.
109 static resource_t round(resource_t val, unsigned long pow)
112 mask = (1ULL << pow) - 1ULL;
119 * 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),
128 __func__, 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;
209 * Compute allocate resources is the guts of the resource allocator.
212 * - Allocate resource locations for every device.
213 * - Don't overlap, and follow the rules of bridges.
214 * - Don't overlap with resources in fixed locations.
215 * - Be efficient so we don't have ugly strategies.
218 * - Devices that have fixed addresses are the minority so don't
219 * worry about them too much. Instead only use part of the address
220 * space for devices with programmable addresses. This easily handles
221 * everything except bridges.
223 * - PCI devices are required to have their sizes and their alignments
224 * equal. In this case an optimal solution to the packing problem
225 * exists. Allocate all devices from highest alignment to least
226 * alignment or vice versa. Use this.
228 * - So we can handle more than PCI run two allocation passes on bridges. The
229 * first to see how large the resources are behind the bridge, and what
230 * their alignment requirements are. The second to assign a safe address to
231 * the devices behind the bridge. This allows us to treat a bridge as just
232 * a device with a couple of resources, and not need to special case it in
233 * the allocator. Also this allows handling of other types of bridges.
235 * @param bus The bus we are traversing.
236 * @param bridge The bridge resource which must contain the bus' resources.
237 * @param type_mask This value gets ANDed with the resource type.
238 * @param type This value must match the result of the AND.
241 static void compute_resources(struct bus *bus, struct resource *bridge,
242 unsigned long type_mask, unsigned long type)
245 struct resource *resource;
247 base = round(bridge->base, bridge->align);
249 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d limit: %llx\n",
250 dev_path(bus->dev), __func__,
251 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
253 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
255 /* For each child which is a bridge, compute_resource_needs. */
256 for (dev = bus->children; dev; dev = dev->sibling) {
257 struct resource *child_bridge;
262 /* Find the resources with matching type flags. */
263 for (child_bridge = dev->resource_list; child_bridge;
264 child_bridge = child_bridge->next) {
267 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
268 (child_bridge->flags & type_mask) != type)
271 /* Split prefetchable memory if combined. Many domains
272 * use the same address space for prefetchable memory
273 * and non-prefetchable memory. Bridges below them
274 * need it separated. Add the PREFETCH flag to the
275 * type_mask and type.
277 link = dev->link_list;
278 while (link && link->link_num !=
279 IOINDEX_LINK(child_bridge->index))
282 printk(BIOS_ERR, "link %ld not found on %s\n",
283 IOINDEX_LINK(child_bridge->index),
285 compute_resources(link, child_bridge,
286 type_mask | IORESOURCE_PREFETCH,
287 type | (child_bridge->flags &
288 IORESOURCE_PREFETCH));
292 /* Remember we haven't found anything yet. */
295 /* Walk through all the resources on the current bus and compute the
296 * amount of address space taken by them. Take granularity and
297 * alignment into account.
299 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
301 /* Size 0 resources can be skipped. */
302 if (!resource->size) {
306 /* Propagate the resource alignment to the bridge resource. */
307 if (resource->align > bridge->align) {
308 bridge->align = resource->align;
311 /* Propagate the resource limit to the bridge register. */
312 if (bridge->limit > resource->limit) {
313 bridge->limit = resource->limit;
316 /* Warn if it looks like APICs aren't declared. */
317 if ((resource->limit == 0xffffffff) &&
318 (resource->flags & IORESOURCE_ASSIGNED)) {
319 printk(BIOS_ERR, "Resource limit looks wrong! (no APIC?)\n");
320 printk(BIOS_ERR, "%s %02lx limit %08Lx\n", dev_path(dev),
321 resource->index, resource->limit);
324 if (resource->flags & IORESOURCE_IO) {
325 /* Don't allow potential aliases over the legacy PCI
326 * expansion card addresses. The legacy PCI decodes
327 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
328 * 0x00 - 0xff can be used out of each 0x400 block of
331 if ((base & 0x300) != 0) {
332 base = (base & ~0x3ff) + 0x400;
334 /* Don't allow allocations in the VGA I/O range.
335 * PCI has special cases for that.
337 else if ((base >= 0x3b0) && (base <= 0x3df)) {
341 /* Base must be aligned. */
342 base = round(base, resource->align);
343 resource->base = base;
344 base += resource->size;
346 printk(BIOS_SPEW, "%s %02lx * [0x%llx - 0x%llx] %s\n",
347 dev_path(dev), resource->index,
349 resource->base + resource->size - 1,
350 (resource->flags & IORESOURCE_IO) ? "io" :
351 (resource->flags & IORESOURCE_PREFETCH) ?
354 /* A pci bridge resource does not need to be a power
355 * of two size, but it does have a minimum granularity.
356 * Round the size up to that minimum granularity so we
357 * know not to place something else at an address postitively
358 * decoded by the bridge.
360 bridge->size = round(base, bridge->gran) -
361 round(bridge->base, bridge->align);
363 printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d limit: %llx done\n",
364 dev_path(bus->dev), __func__,
365 (bridge->flags & IORESOURCE_IO) ? "io" :
366 (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
367 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
371 * This function is the second part of the resource allocator.
374 * - Allocate resource locations for every device.
375 * - Don't overlap, and follow the rules of bridges.
376 * - Don't overlap with resources in fixed locations.
377 * - Be efficient so we don't have ugly strategies.
380 * - Devices that have fixed addresses are the minority so don't
381 * worry about them too much. Instead only use part of the address
382 * space for devices with programmable addresses. This easily handles
383 * everything except bridges.
385 * - PCI devices are required to have their sizes and their alignments
386 * equal. In this case an optimal solution to the packing problem
387 * exists. Allocate all devices from highest alignment to least
388 * alignment or vice versa. Use this.
390 * - So we can handle more than PCI run two allocation passes on bridges. The
391 * first to see how large the resources are behind the bridge, and what
392 * their alignment requirements are. The second to assign a safe address to
393 * the devices behind the bridge. This allows us to treat a bridge as just
394 * a device with a couple of resources, and not need to special case it in
395 * the allocator. Also this allows handling of other types of bridges.
397 * - This function assigns the resources a value.
399 * @param bus The bus we are traversing.
400 * @param bridge The bridge resource which must contain the bus' resources.
401 * @param type_mask This value gets ANDed with the resource type.
402 * @param type This value must match the result of the AND.
404 static void allocate_resources(struct bus *bus, struct resource *bridge,
405 unsigned long type_mask, unsigned long type)
408 struct resource *resource;
412 printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d limit:%llx\n",
413 dev_path(bus->dev), __func__,
414 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
416 base, bridge->size, bridge->align, bridge->gran, bridge->limit);
418 /* Remember we haven't found anything yet. */
421 /* Walk through all the resources on the current bus and allocate them
424 while ((dev = largest_resource(bus, &resource, type_mask, type))) {
426 /* Propagate the bridge limit to the resource register. */
427 if (resource->limit > bridge->limit) {
428 resource->limit = bridge->limit;
431 /* Size 0 resources can be skipped. */
432 if (!resource->size) {
433 /* Set the base to limit so it doesn't confuse tolm. */
434 resource->base = resource->limit;
435 resource->flags |= IORESOURCE_ASSIGNED;
439 if (resource->flags & IORESOURCE_IO) {
440 /* Don't allow potential aliases over the legacy PCI
441 * expansion card addresses. The legacy PCI decodes
442 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
443 * 0x00 - 0xff can be used out of each 0x400 block of
446 if ((base & 0x300) != 0) {
447 base = (base & ~0x3ff) + 0x400;
449 /* Don't allow allocations in the VGA I/O range.
450 * PCI has special cases for that.
452 else if ((base >= 0x3b0) && (base <= 0x3df)) {
457 if ((round(base, resource->align) + resource->size - 1) <=
459 /* Base must be aligned. */
460 base = round(base, resource->align);
461 resource->base = base;
462 resource->flags |= IORESOURCE_ASSIGNED;
463 resource->flags &= ~IORESOURCE_STORED;
464 base += resource->size;
466 printk(BIOS_ERR, "!! Resource didn't fit !!\n");
467 printk(BIOS_ERR, " aligned base %llx size %llx limit %llx\n",
468 round(base, resource->align), resource->size,
470 printk(BIOS_ERR, " %llx needs to be <= %llx (limit)\n",
471 (round(base, resource->align) +
472 resource->size) - 1, resource->limit);
473 printk(BIOS_ERR, " %s%s %02lx * [0x%llx - 0x%llx] %s\n",
475 flags & IORESOURCE_ASSIGNED) ? "Assigned: " :
476 "", dev_path(dev), resource->index,
478 resource->base + resource->size - 1,
480 flags & IORESOURCE_IO) ? "io" : (resource->
483 ? "prefmem" : "mem");
486 printk(BIOS_SPEW, "%s%s %02lx * [0x%llx - 0x%llx] %s\n",
487 (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
489 dev_path(dev), resource->index, resource->base,
490 resource->size ? resource->base + resource->size - 1 :
492 (resource->flags & IORESOURCE_IO) ? "io" :
493 (resource->flags & IORESOURCE_PREFETCH) ? "prefmem" :
496 /* A PCI bridge resource does not need to be a power of two size, but
497 * it does have a minimum granularity. Round the size up to that
498 * minimum granularity so we know not to place something else at an
499 * address positively decoded by the bridge.
502 bridge->flags |= IORESOURCE_ASSIGNED;
504 printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d gran: %d done\n",
505 dev_path(bus->dev), __func__,
506 (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
508 base, bridge->size, bridge->align, bridge->gran);
510 /* For each child which is a bridge, allocate_resources. */
511 for (dev = bus->children; dev; dev = dev->sibling) {
512 struct resource *child_bridge;
517 /* Find the resources with matching type flags. */
518 for (child_bridge = dev->resource_list; child_bridge;
519 child_bridge = child_bridge->next) {
522 if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
523 (child_bridge->flags & type_mask) != type)
526 /* Split prefetchable memory if combined. Many domains
527 * use the same address space for prefetchable memory
528 * and non-prefetchable memory. Bridges below them
529 * need it separated. Add the PREFETCH flag to the
530 * type_mask and type.
532 link = dev->link_list;
533 while (link && link->link_num !=
534 IOINDEX_LINK(child_bridge->index))
537 printk(BIOS_ERR, "link %ld not found on %s\n",
538 IOINDEX_LINK(child_bridge->index),
540 allocate_resources(link, child_bridge,
541 type_mask | IORESOURCE_PREFETCH,
542 type | (child_bridge->flags &
543 IORESOURCE_PREFETCH));
548 #if CONFIG_PCI_64BIT_PREF_MEM == 1
549 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
551 #define MEM_MASK (IORESOURCE_MEM)
554 #define IO_MASK (IORESOURCE_IO)
555 #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
556 #define MEM_TYPE (IORESOURCE_MEM)
557 #define IO_TYPE (IORESOURCE_IO)
560 struct resource pref, io, mem;
563 static void constrain_resources(struct device *dev, struct constraints* limits)
565 struct device *child;
566 struct resource *res;
567 struct resource *lim;
570 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
572 /* Constrain limits based on the fixed resources of this device. */
573 for (res = dev->resource_list; res; res = res->next) {
574 if (!(res->flags & IORESOURCE_FIXED))
577 /* It makes no sense to have 0-sized, fixed resources.*/
578 printk(BIOS_ERR, "skipping %s@%lx fixed resource, size=0!\n",
579 dev_path(dev), res->index);
583 /* PREFETCH, MEM, or I/O - skip any others. */
584 if ((res->flags & MEM_MASK) == PREF_TYPE)
586 else if ((res->flags & MEM_MASK) == MEM_TYPE)
588 else if ((res->flags & IO_MASK) == IO_TYPE)
593 /* Is it a fixed resource outside the current known region?
594 If so, we don't have to consider it - it will be handled
595 correctly and doesn't affect current region's limits */
596 if (((res->base + res->size -1) < lim->base) || (res->base > lim->limit))
599 /* Choose to be above or below fixed resources. This
600 * check is signed so that "negative" amounts of space
601 * are handled correctly.
603 if ((signed long long)(lim->limit - (res->base + res->size -1)) >
604 (signed long long)(res->base - lim->base))
605 lim->base = res->base + res->size;
607 lim->limit = res->base -1;
610 /* Descend into every enabled child and look for fixed resources. */
611 for (link = dev->link_list; link; link = link->next)
612 for (child = link->children; child;
613 child = child->sibling)
615 constrain_resources(child, limits);
618 static void avoid_fixed_resources(struct device *dev)
620 struct constraints limits;
621 struct resource *res;
623 printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
624 /* Initialize constraints to maximum size. */
626 limits.pref.base = 0;
627 limits.pref.limit = 0xffffffffffffffffULL;
629 limits.io.limit = 0xffffffffffffffffULL;
631 limits.mem.limit = 0xffffffffffffffffULL;
633 /* Constrain the limits to dev's initial resources. */
634 for (res = dev->resource_list; res; res = res->next) {
635 if ((res->flags & IORESOURCE_FIXED))
637 printk(BIOS_SPEW, "%s:@%s %02lx limit %08Lx\n", __func__,
638 dev_path(dev), res->index, res->limit);
639 if ((res->flags & MEM_MASK) == PREF_TYPE &&
640 (res->limit < limits.pref.limit))
641 limits.pref.limit = res->limit;
642 if ((res->flags & MEM_MASK) == MEM_TYPE &&
643 (res->limit < limits.mem.limit))
644 limits.mem.limit = res->limit;
645 if ((res->flags & IO_MASK) == IO_TYPE &&
646 (res->limit < limits.io.limit))
647 limits.io.limit = res->limit;
650 /* Look through the tree for fixed resources and update the limits. */
651 constrain_resources(dev, &limits);
653 /* Update dev's resources with new limits. */
654 for (res = dev->resource_list; res; res = res->next) {
655 struct resource *lim;
657 if ((res->flags & IORESOURCE_FIXED))
660 /* PREFETCH, MEM, or I/O - skip any others. */
661 if ((res->flags & MEM_MASK) == PREF_TYPE)
663 else if ((res->flags & MEM_MASK) == MEM_TYPE)
665 else if ((res->flags & IO_MASK) == IO_TYPE)
670 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08Lx\n", __func__,
671 dev_path(dev), res->index, res->limit);
672 printk(BIOS_SPEW, "\tlim->base %08Lx lim->limit %08Lx\n",
673 lim->base, lim->limit);
675 /* Is the resource outside the limits? */
676 if (lim->base > res->base)
677 res->base = lim->base;
678 if (res->limit > lim->limit)
679 res->limit = lim->limit;
683 #if CONFIG_VGA_BRIDGE_SETUP == 1
684 device_t vga_pri = 0;
685 static void set_vga_bridge_bits(void)
688 * FIXME: Modify set_vga_bridge so it is less PCI centric!
689 * This function knows too much about PCI stuff, it should be just
690 * an iterator/visitor.
693 /* FIXME: Handle the VGA palette snooping. */
694 struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
701 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) {
713 if (dev->on_mainboard) {
720 /* It isn't safe to enable other VGA cards. */
721 dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
730 #if CONFIG_CONSOLE_VGA_ONBOARD_AT_FIRST == 1
731 if (vga_onboard) // Will use on board VGA as pri.
733 if (!vga) // Will use last add on adapter as pri.
740 /* VGA is first add on card or the only onboard VGA. */
741 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
742 /* All legacy VGA cards have MEM & I/O space registers. */
743 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
747 /* Now walk up the bridges setting the VGA enable. */
749 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
751 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
752 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
759 * Assign the computed resources to the devices on the bus.
761 * Use the device specific set_resources method to store the computed
762 * resources to hardware. For bridge devices, the set_resources() method
763 * has to recurse into every down stream buses.
766 * assign_resources() -> device_operation::set_resources()
767 * device_operation::set_resources() -> assign_resources()
769 * @param bus Pointer to the structure for this bus.
771 void assign_resources(struct bus *bus)
773 struct device *curdev;
775 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
776 dev_path(bus->dev), bus->secondary, bus->link_num);
778 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
779 if (!curdev->enabled || !curdev->resource_list) {
782 if (!curdev->ops || !curdev->ops->set_resources) {
783 printk(BIOS_ERR, "%s missing set_resources\n",
787 curdev->ops->set_resources(curdev);
789 printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
790 dev_path(bus->dev), bus->secondary, bus->link_num);
794 * Enable the resources for devices on a link.
796 * Enable resources of the device by calling the device specific
797 * enable_resources() method.
799 * The parent's resources should be enabled first to avoid having enabling
800 * order problem. This is done by calling the parent's enable_resources()
801 * method before its childrens' enable_resources() methods.
803 * @param link The link whose devices' resources are to be enabled.
805 static void enable_resources(struct bus *link)
810 for (dev = link->children; dev; dev = dev->sibling) {
811 if (dev->enabled && dev->ops && dev->ops->enable_resources) {
812 dev->ops->enable_resources(dev);
816 for (dev = link->children; dev; dev = dev->sibling) {
817 for (c_link = dev->link_list; c_link; c_link = c_link->next) {
818 enable_resources(c_link);
824 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
826 * @param bus Pointer to the bus structure.
827 * @return 1 if the bus was successfully reset, 0 otherwise.
829 int reset_bus(struct bus *bus)
831 if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
832 bus->dev->ops->reset_bus(bus);
833 bus->reset_needed = 0;
840 * Scan for devices on a bus.
842 * If there are bridges on the bus, recursively scan the buses behind the
843 * bridges. If the setting up and tuning of the bus causes a reset to be
844 * required, reset the bus and scan it again.
846 * @param busdev Pointer to the bus device.
847 * @param max Current bus number.
848 * @return The maximum bus number found, after scanning all subordinate buses.
850 unsigned int scan_bus(struct device *busdev, unsigned int max)
852 unsigned int new_max;
854 if (!busdev || !busdev->enabled || !busdev->ops ||
855 !busdev->ops->scan_bus) {
860 while (do_scan_bus) {
862 new_max = busdev->ops->scan_bus(busdev, max);
864 for (link = busdev->link_list; link; link = link->next) {
865 if (link->reset_needed) {
866 if (reset_bus(link)) {
869 busdev->bus->reset_needed = 1;
878 * Determine the existence of devices and extend the device tree.
880 * Most of the devices in the system are listed in the mainboard Config.lb
881 * file. The device structures for these devices are generated at compile
882 * time by the config tool and are organized into the device tree. This
883 * function determines if the devices created at compile time actually exist
884 * in the physical system.
886 * For devices in the physical system but not listed in the Config.lb file,
887 * the device structures have to be created at run time and attached to the
890 * This function starts from the root device 'dev_root', scan the buses in
891 * the system recursively, modify the device tree according to the result of
894 * This function has no idea how to scan and probe buses and devices at all.
895 * It depends on the bus/device specific scan_bus() method to do it. The
896 * scan_bus() method also has to create the device structure and attach
897 * it to the device tree.
899 void dev_enumerate(void)
902 printk(BIOS_INFO, "Enumerating buses...\n");
905 show_all_devs(BIOS_SPEW, "Before Device Enumeration.");
906 printk(BIOS_SPEW, "Compare with tree...\n");
907 show_devs_tree(root, BIOS_SPEW, 0, 0);
909 if (root->chip_ops && root->chip_ops->enable_dev) {
910 root->chip_ops->enable_dev(root);
912 if (!root->ops || !root->ops->scan_bus) {
913 printk(BIOS_ERR, "dev_root missing scan_bus operation");
917 printk(BIOS_INFO, "done\n");
921 * Configure devices on the devices tree.
923 * Starting at the root of the device tree, travel it recursively in two
924 * passes. In the first pass, we compute and allocate resources (ranges)
925 * requried by each device. In the second pass, the resources ranges are
926 * relocated to their final position and stored to the hardware.
928 * I/O resources grow upward. MEM resources grow downward.
930 * Since the assignment is hierarchical we set the values into the dev_root
933 void dev_configure(void)
935 struct resource *res;
937 struct device *child;
939 #if CONFIG_VGA_BRIDGE_SETUP == 1
940 set_vga_bridge_bits();
943 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);
990 /* Now we need to adjust the resources. MEM resources need to start at
991 * the highest address managable.
993 for (child = root->link_list->children; child; child = child->sibling) {
994 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
996 for (res = child->resource_list; res; res = res->next) {
997 if (!(res->flags & IORESOURCE_MEM) ||
998 res->flags & IORESOURCE_FIXED)
1000 res->base = resource_max(res);
1004 /* Store the computed resource allocations into device registers ... */
1005 printk(BIOS_INFO, "Setting resources...\n");
1006 for (child = root->link_list->children; child; child = child->sibling) {
1007 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
1009 for (res = child->resource_list; res; res = res->next) {
1010 if (res->flags & IORESOURCE_FIXED)
1012 if (res->flags & IORESOURCE_PREFETCH) {
1013 allocate_resources(child->link_list,
1014 res, MEM_MASK, PREF_TYPE);
1017 if (res->flags & IORESOURCE_MEM) {
1018 allocate_resources(child->link_list,
1019 res, MEM_MASK, MEM_TYPE);
1022 if (res->flags & IORESOURCE_IO) {
1023 allocate_resources(child->link_list,
1024 res, IO_MASK, IO_TYPE);
1029 assign_resources(root->link_list);
1030 printk(BIOS_INFO, "Done setting resources.\n");
1031 print_resource_tree(root, BIOS_SPEW, "After assigning values.");
1033 printk(BIOS_INFO, "Done allocating resources.\n");
1037 * Enable devices on the device tree.
1039 * Starting at the root, walk the tree and enable all devices/bridges by
1040 * calling the device's enable_resources() method.
1042 void dev_enable(void)
1046 printk(BIOS_INFO, "Enabling resources...\n");
1048 /* now enable everything. */
1049 for (link = dev_root.link_list; link; link = link->next)
1050 enable_resources(link);
1052 printk(BIOS_INFO, "done.\n");
1056 * Initialize a specific device.
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.
1062 * @param dev The device to be initialized.
1064 static void init_dev(struct device *dev)
1066 if (!dev->enabled) {
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) {
1091 for (dev = link->children; dev; dev = dev->sibling) {
1092 for (c_link = dev->link_list; c_link; c_link = c_link->next) {
1099 * Initialize all devices in the global device tree.
1101 * Starting at the root device, call the device's init() method to do
1102 * device-specific setup, then call each child's init() method.
1104 void dev_initialize(void)
1108 printk(BIOS_INFO, "Initializing devices...\n");
1110 /* First call the mainboard init. */
1111 init_dev(&dev_root);
1113 /* now initialize everything. */
1114 for (link = dev_root.link_list; link; link = link->next)
1117 printk(BIOS_INFO, "Devices initialized\n");
1118 show_all_devs(BIOS_SPEW, "After init.");