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>
37 #include <device/device.h>
38 #include <device/pci_def.h>
39 #include <device/pci_ids.h>
42 #include <smp/spinlock.h>
44 #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
;
56 * Initialize all chips of statically known devices.
58 * Will be called before bus enumeration to initialize chips stated in the
61 void dev_initialize_chips(void)
65 for (dev
= all_devices
; dev
; dev
= dev
->next
) {
66 /* Initialize chip if we haven't yet. */
67 if (dev
->chip_ops
&& dev
->chip_ops
->init
&&
68 !dev
->chip_ops
->initialized
) {
70 dev
->chip_ops
->init(dev
->chip_info
);
71 dev
->chip_ops
->initialized
= 1;
78 * Finalize all chips of statically known devices.
80 * This is the last call before calling the payload. This is a good place
81 * to lock registers or other final cleanup.
83 void dev_finalize_chips(void)
87 for (dev
= all_devices
; dev
; dev
= dev
->next
) {
88 /* Initialize chip if we haven't yet. */
89 if (dev
->chip_ops
&& dev
->chip_ops
->final
&&
90 !dev
->chip_ops
->finalized
) {
91 dev
->chip_ops
->final(dev
->chip_info
);
92 dev
->chip_ops
->finalized
= 1;
97 DECLARE_SPIN_LOCK(dev_lock
)
101 uint64_t uma_memory_base
= 0;
102 uint64_t uma_memory_size
= 0;
106 * Allocate a new device structure.
108 * Allocate a new device structure and attach it to the device tree as a
109 * child of the parent bus.
111 * @param parent Parent bus the newly created device should be attached to.
112 * @param path Path to the device to be created.
113 * @return Pointer to the newly created device structure.
117 static device_t
__alloc_dev(struct bus
*parent
, struct device_path
*path
)
121 /* Find the last child of our parent. */
122 for (child
= parent
->children
; child
&& child
->sibling
; /* */ )
123 child
= child
->sibling
;
125 dev
= malloc(sizeof(*dev
));
127 die("alloc_dev(): out of memory.\n");
129 memset(dev
, 0, sizeof(*dev
));
130 memcpy(&dev
->path
, path
, sizeof(*path
));
132 /* By default devices are enabled. */
135 /* Add the new device to the list of children of the bus. */
138 child
->sibling
= dev
;
140 parent
->children
= dev
;
142 /* Append a new device to the global device list.
143 * The list is used to find devices once everything is set up.
145 last_dev
->next
= dev
;
151 device_t
alloc_dev(struct bus
*parent
, struct device_path
*path
)
154 spin_lock(&dev_lock
);
155 dev
= __alloc_dev(parent
, path
);
156 spin_unlock(&dev_lock
);
161 * See if a device structure already exists and if not allocate it.
163 * @param parent The bus to find the device on.
164 * @param path The relative path from the bus to the appropriate device.
165 * @return Pointer to a device structure for the device on bus at path.
167 device_t
alloc_find_dev(struct bus
*parent
, struct device_path
*path
)
170 spin_lock(&dev_lock
);
171 child
= find_dev_path(parent
, path
);
173 child
= __alloc_dev(parent
, path
);
174 spin_unlock(&dev_lock
);
179 * Round a number up to an alignment.
181 * @param val The starting value.
182 * @param pow Alignment as a power of two.
183 * @return Rounded up number.
185 static resource_t
round(resource_t val
, unsigned long pow
)
188 mask
= (1ULL << pow
) - 1ULL;
194 static const char * resource2str(struct resource
*res
)
196 if (res
->flags
& IORESOURCE_IO
)
198 if (res
->flags
& IORESOURCE_PREFETCH
)
200 if (res
->flags
& IORESOURCE_MEM
)
206 * Read the resources on all devices of a given bus.
208 * @param bus Bus to read the resources on.
210 static void read_resources(struct bus
*bus
)
212 struct device
*curdev
;
214 printk(BIOS_SPEW
, "%s %s bus %x link: %d\n", dev_path(bus
->dev
),
215 __func__
, bus
->secondary
, bus
->link_num
);
217 /* Walk through all devices and find which resources they need. */
218 for (curdev
= bus
->children
; curdev
; curdev
= curdev
->sibling
) {
221 if (!curdev
->enabled
)
224 if (!curdev
->ops
|| !curdev
->ops
->read_resources
) {
225 printk(BIOS_ERR
, "%s missing read_resources\n",
229 post_log_path(curdev
);
230 curdev
->ops
->read_resources(curdev
);
232 /* Read in the resources behind the current device's links. */
233 for (link
= curdev
->link_list
; link
; link
= link
->next
)
234 read_resources(link
);
237 printk(BIOS_SPEW
, "%s read_resources bus %d link: %d done\n",
238 dev_path(bus
->dev
), bus
->secondary
, bus
->link_num
);
241 struct pick_largest_state
{
242 struct resource
*last
;
243 struct device
*result_dev
;
244 struct resource
*result
;
248 static void pick_largest_resource(void *gp
, struct device
*dev
,
249 struct resource
*resource
)
251 struct pick_largest_state
*state
= gp
;
252 struct resource
*last
;
256 /* Be certain to pick the successor to last. */
257 if (resource
== last
) {
258 state
->seen_last
= 1;
261 if (resource
->flags
& IORESOURCE_FIXED
)
262 return; /* Skip it. */
263 if (last
&& ((last
->align
< resource
->align
) ||
264 ((last
->align
== resource
->align
) &&
265 (last
->size
< resource
->size
)) ||
266 ((last
->align
== resource
->align
) &&
267 (last
->size
== resource
->size
) && (!state
->seen_last
)))) {
270 if (!state
->result
||
271 (state
->result
->align
< resource
->align
) ||
272 ((state
->result
->align
== resource
->align
) &&
273 (state
->result
->size
< resource
->size
))) {
274 state
->result_dev
= dev
;
275 state
->result
= resource
;
279 static struct device
*largest_resource(struct bus
*bus
,
280 struct resource
**result_res
,
281 unsigned long type_mask
,
284 struct pick_largest_state state
;
286 state
.last
= *result_res
;
287 state
.result_dev
= NULL
;
291 search_bus_resources(bus
, type_mask
, type
, pick_largest_resource
,
294 *result_res
= state
.result
;
295 return state
.result_dev
;
299 * This function is the guts of the resource allocator.
302 * - Allocate resource locations for every device.
303 * - Don't overlap, and follow the rules of bridges.
304 * - Don't overlap with resources in fixed locations.
305 * - Be efficient so we don't have ugly strategies.
308 * - Devices that have fixed addresses are the minority so don't
309 * worry about them too much. Instead only use part of the address
310 * space for devices with programmable addresses. This easily handles
311 * everything except bridges.
313 * - PCI devices are required to have their sizes and their alignments
314 * equal. In this case an optimal solution to the packing problem
315 * exists. Allocate all devices from highest alignment to least
316 * alignment or vice versa. Use this.
318 * - So we can handle more than PCI run two allocation passes on bridges. The
319 * first to see how large the resources are behind the bridge, and what
320 * their alignment requirements are. The second to assign a safe address to
321 * the devices behind the bridge. This allows us to treat a bridge as just
322 * a device with a couple of resources, and not need to special case it in
323 * the allocator. Also this allows handling of other types of bridges.
325 * @param bus The bus we are traversing.
326 * @param bridge The bridge resource which must contain the bus' resources.
327 * @param type_mask This value gets ANDed with the resource type.
328 * @param type This value must match the result of the AND.
331 static void compute_resources(struct bus
*bus
, struct resource
*bridge
,
332 unsigned long type_mask
, unsigned long type
)
335 struct resource
*resource
;
337 base
= round(bridge
->base
, bridge
->align
);
339 printk(BIOS_SPEW
, "%s %s: base: %llx size: %llx align: %d gran: %d"
340 " limit: %llx\n", dev_path(bus
->dev
), resource2str(bridge
),
341 base
, bridge
->size
, bridge
->align
,
342 bridge
->gran
, bridge
->limit
);
344 /* For each child which is a bridge, compute the resource needs. */
345 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
346 struct resource
*child_bridge
;
351 /* Find the resources with matching type flags. */
352 for (child_bridge
= dev
->resource_list
; child_bridge
;
353 child_bridge
= child_bridge
->next
) {
356 if (!(child_bridge
->flags
& IORESOURCE_BRIDGE
)
357 || (child_bridge
->flags
& type_mask
) != type
)
361 * Split prefetchable memory if combined. Many domains
362 * use the same address space for prefetchable memory
363 * and non-prefetchable memory. Bridges below them need
364 * it separated. Add the PREFETCH flag to the type_mask
367 link
= dev
->link_list
;
368 while (link
&& link
->link_num
!=
369 IOINDEX_LINK(child_bridge
->index
))
373 printk(BIOS_ERR
, "link %ld not found on %s\n",
374 IOINDEX_LINK(child_bridge
->index
),
378 compute_resources(link
, child_bridge
,
379 type_mask
| IORESOURCE_PREFETCH
,
380 type
| (child_bridge
->flags
&
381 IORESOURCE_PREFETCH
));
385 /* Remember we haven't found anything yet. */
389 * Walk through all the resources on the current bus and compute the
390 * amount of address space taken by them. Take granularity and
391 * alignment into account.
393 while ((dev
= largest_resource(bus
, &resource
, type_mask
, type
))) {
395 /* Size 0 resources can be skipped. */
399 /* Propagate the resource alignment to the bridge resource. */
400 if (resource
->align
> bridge
->align
)
401 bridge
->align
= resource
->align
;
403 /* Propagate the resource limit to the bridge register. */
404 if (bridge
->limit
> resource
->limit
)
405 bridge
->limit
= resource
->limit
;
407 /* Warn if it looks like APICs aren't declared. */
408 if ((resource
->limit
== 0xffffffff) &&
409 (resource
->flags
& IORESOURCE_ASSIGNED
)) {
411 "Resource limit looks wrong! (no APIC?)\n");
412 printk(BIOS_ERR
, "%s %02lx limit %08llx\n",
413 dev_path(dev
), resource
->index
, resource
->limit
);
416 if (resource
->flags
& IORESOURCE_IO
) {
418 * Don't allow potential aliases over the legacy PCI
419 * expansion card addresses. The legacy PCI decodes
420 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
421 * 0x00 - 0xff can be used out of each 0x400 block of
424 if ((base
& 0x300) != 0) {
425 base
= (base
& ~0x3ff) + 0x400;
428 * Don't allow allocations in the VGA I/O range.
429 * PCI has special cases for that.
431 else if ((base
>= 0x3b0) && (base
<= 0x3df)) {
435 /* Base must be aligned. */
436 base
= round(base
, resource
->align
);
437 resource
->base
= base
;
438 base
+= resource
->size
;
440 printk(BIOS_SPEW
, "%s %02lx * [0x%llx - 0x%llx] %s\n",
441 dev_path(dev
), resource
->index
, resource
->base
,
442 resource
->base
+ resource
->size
- 1,
443 resource2str(resource
));
447 * A PCI bridge resource does not need to be a power of two size, but
448 * it does have a minimum granularity. Round the size up to that
449 * minimum granularity so we know not to place something else at an
450 * address positively decoded by the bridge.
452 bridge
->size
= round(base
, bridge
->gran
) -
453 round(bridge
->base
, bridge
->align
);
455 printk(BIOS_SPEW
, "%s %s: base: %llx size: %llx align: %d gran: %d"
456 " limit: %llx done\n", dev_path(bus
->dev
),
457 resource2str(bridge
),
458 base
, bridge
->size
, bridge
->align
, bridge
->gran
, bridge
->limit
);
462 * This function is the second part of the resource allocator.
464 * See the compute_resources function for a more detailed explanation.
466 * This function assigns the resources a value.
468 * @param bus The bus we are traversing.
469 * @param bridge The bridge resource which must contain the bus' resources.
470 * @param type_mask This value gets ANDed with the resource type.
471 * @param type This value must match the result of the AND.
473 * @see compute_resources
475 static void allocate_resources(struct bus
*bus
, struct resource
*bridge
,
476 unsigned long type_mask
, unsigned long type
)
479 struct resource
*resource
;
483 printk(BIOS_SPEW
, "%s %s: base:%llx size:%llx align:%d gran:%d "
484 "limit:%llx\n", dev_path(bus
->dev
),
485 resource2str(bridge
),
486 base
, bridge
->size
, bridge
->align
, bridge
->gran
, bridge
->limit
);
488 /* Remember we haven't found anything yet. */
492 * Walk through all the resources on the current bus and allocate them
495 while ((dev
= largest_resource(bus
, &resource
, type_mask
, type
))) {
497 /* Propagate the bridge limit to the resource register. */
498 if (resource
->limit
> bridge
->limit
)
499 resource
->limit
= bridge
->limit
;
501 /* Size 0 resources can be skipped. */
502 if (!resource
->size
) {
503 /* Set the base to limit so it doesn't confuse tolm. */
504 resource
->base
= resource
->limit
;
505 resource
->flags
|= IORESOURCE_ASSIGNED
;
509 if (resource
->flags
& IORESOURCE_IO
) {
511 * Don't allow potential aliases over the legacy PCI
512 * expansion card addresses. The legacy PCI decodes
513 * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
514 * 0x00 - 0xff can be used out of each 0x400 block of
517 if ((base
& 0x300) != 0) {
518 base
= (base
& ~0x3ff) + 0x400;
521 * Don't allow allocations in the VGA I/O range.
522 * PCI has special cases for that.
524 else if ((base
>= 0x3b0) && (base
<= 0x3df)) {
529 if ((round(base
, resource
->align
) + resource
->size
- 1) <=
531 /* Base must be aligned. */
532 base
= round(base
, resource
->align
);
533 resource
->base
= base
;
534 resource
->limit
= resource
->base
+ resource
->size
- 1;
535 resource
->flags
|= IORESOURCE_ASSIGNED
;
536 resource
->flags
&= ~IORESOURCE_STORED
;
537 base
+= resource
->size
;
539 printk(BIOS_ERR
, "!! Resource didn't fit !!\n");
540 printk(BIOS_ERR
, " aligned base %llx size %llx "
541 "limit %llx\n", round(base
, resource
->align
),
542 resource
->size
, resource
->limit
);
543 printk(BIOS_ERR
, " %llx needs to be <= %llx "
544 "(limit)\n", (round(base
, resource
->align
) +
545 resource
->size
) - 1, resource
->limit
);
546 printk(BIOS_ERR
, " %s%s %02lx * [0x%llx - 0x%llx]"
547 " %s\n", (resource
->flags
& IORESOURCE_ASSIGNED
)
548 ? "Assigned: " : "", dev_path(dev
),
549 resource
->index
, resource
->base
,
550 resource
->base
+ resource
->size
- 1,
551 resource2str(resource
));
554 printk(BIOS_SPEW
, "%s %02lx * [0x%llx - 0x%llx] %s\n",
555 dev_path(dev
), resource
->index
, resource
->base
,
556 resource
->size
? resource
->base
+ resource
->size
- 1 :
557 resource
->base
, resource2str(resource
));
561 * A PCI bridge resource does not need to be a power of two size, but
562 * it does have a minimum granularity. Round the size up to that
563 * minimum granularity so we know not to place something else at an
564 * address positively decoded by the bridge.
567 bridge
->flags
|= IORESOURCE_ASSIGNED
;
569 printk(BIOS_SPEW
, "%s %s: next_base: %llx size: %llx align: %d "
570 "gran: %d done\n", dev_path(bus
->dev
),
571 resource2str(bridge
), base
, bridge
->size
, bridge
->align
,
574 /* For each child which is a bridge, allocate_resources. */
575 for (dev
= bus
->children
; dev
; dev
= dev
->sibling
) {
576 struct resource
*child_bridge
;
581 /* Find the resources with matching type flags. */
582 for (child_bridge
= dev
->resource_list
; child_bridge
;
583 child_bridge
= child_bridge
->next
) {
586 if (!(child_bridge
->flags
& IORESOURCE_BRIDGE
) ||
587 (child_bridge
->flags
& type_mask
) != type
)
591 * Split prefetchable memory if combined. Many domains
592 * use the same address space for prefetchable memory
593 * and non-prefetchable memory. Bridges below them need
594 * it separated. Add the PREFETCH flag to the type_mask
597 link
= dev
->link_list
;
598 while (link
&& link
->link_num
!=
599 IOINDEX_LINK(child_bridge
->index
))
602 printk(BIOS_ERR
, "link %ld not found on %s\n",
603 IOINDEX_LINK(child_bridge
->index
),
606 allocate_resources(link
, child_bridge
,
607 type_mask
| IORESOURCE_PREFETCH
,
608 type
| (child_bridge
->flags
&
609 IORESOURCE_PREFETCH
));
614 static int resource_is(struct resource
*res
, u32 type
)
616 return (res
->flags
& IORESOURCE_TYPE_MASK
) == type
;
620 struct resource io
, mem
;
623 static struct resource
* resource_limit(struct constraints
*limits
, struct resource
*res
)
625 struct resource
*lim
= NULL
;
627 /* MEM, or I/O - skip any others. */
628 if (resource_is(res
, IORESOURCE_MEM
))
630 else if (resource_is(res
, IORESOURCE_IO
))
636 static void constrain_resources(struct device
*dev
, struct constraints
* limits
)
638 struct device
*child
;
639 struct resource
*res
;
640 struct resource
*lim
;
643 /* Constrain limits based on the fixed resources of this device. */
644 for (res
= dev
->resource_list
; res
; res
= res
->next
) {
645 if (!(res
->flags
& IORESOURCE_FIXED
))
648 /* It makes no sense to have 0-sized, fixed resources.*/
649 printk(BIOS_ERR
, "skipping %s@%lx fixed resource, "
650 "size=0!\n", dev_path(dev
), res
->index
);
654 lim
= resource_limit(limits
, res
);
659 * Is it a fixed resource outside the current known region?
660 * If so, we don't have to consider it - it will be handled
661 * correctly and doesn't affect current region's limits.
663 if (((res
->base
+ res
->size
-1) < lim
->base
)
664 || (res
->base
> lim
->limit
))
667 printk(BIOS_SPEW
, "%s: %s %02lx base %08llx limit %08llx %s (fixed)\n",
668 __func__
, dev_path(dev
), res
->index
, res
->base
,
669 res
->base
+ res
->size
- 1, resource2str(res
));
672 * Choose to be above or below fixed resources. This check is
673 * signed so that "negative" amounts of space are handled
676 if ((signed long long)(lim
->limit
- (res
->base
+ res
->size
-1))
677 > (signed long long)(res
->base
- lim
->base
))
678 lim
->base
= res
->base
+ res
->size
;
680 lim
->limit
= res
->base
-1;
683 /* Descend into every enabled child and look for fixed resources. */
684 for (link
= dev
->link_list
; link
; link
= link
->next
) {
685 for (child
= link
->children
; child
; child
= child
->sibling
) {
687 constrain_resources(child
, limits
);
692 static void avoid_fixed_resources(struct device
*dev
)
694 struct constraints limits
;
695 struct resource
*res
;
696 struct resource
*lim
;
698 printk(BIOS_SPEW
, "%s: %s\n", __func__
, dev_path(dev
));
700 /* Initialize constraints to maximum size. */
702 limits
.io
.limit
= 0xffffffffffffffffULL
;
704 limits
.mem
.limit
= 0xffffffffffffffffULL
;
706 /* Constrain the limits to dev's initial resources. */
707 for (res
= dev
->resource_list
; res
; res
= res
->next
) {
708 if ((res
->flags
& IORESOURCE_FIXED
))
710 printk(BIOS_SPEW
, "%s:@%s %02lx limit %08llx\n", __func__
,
711 dev_path(dev
), res
->index
, res
->limit
);
713 lim
= resource_limit(&limits
, res
);
717 if (res
->base
> lim
->base
)
718 lim
->base
= res
->base
;
719 if (res
->limit
< lim
->limit
)
720 lim
->limit
= res
->limit
;
723 /* Look through the tree for fixed resources and update the limits. */
724 constrain_resources(dev
, &limits
);
726 /* Update dev's resources with new limits. */
727 for (res
= dev
->resource_list
; res
; res
= res
->next
) {
728 if ((res
->flags
& IORESOURCE_FIXED
))
731 lim
= resource_limit(&limits
, res
);
735 /* Is the resource outside the limits? */
736 if (lim
->base
> res
->base
)
737 res
->base
= lim
->base
;
738 if (res
->limit
> lim
->limit
)
739 res
->limit
= lim
->limit
;
741 /* MEM resources need to start at the highest address manageable. */
742 if (res
->flags
& IORESOURCE_MEM
)
743 res
->base
= resource_max(res
);
745 printk(BIOS_SPEW
, "%s:@%s %02lx base %08llx limit %08llx\n",
746 __func__
, dev_path(dev
), res
->index
, res
->base
, res
->limit
);
750 device_t vga_pri
= 0;
751 static void set_vga_bridge_bits(void)
754 * FIXME: Modify set_vga_bridge() so it is less PCI-centric!
755 * This function knows too much about PCI stuff, it should be just
756 * an iterator/visitor.
759 /* FIXME: Handle the VGA palette snooping. */
760 struct device
*dev
, *vga
, *vga_onboard
;
768 while ((dev
= dev_find_class(PCI_CLASS_DISPLAY_VGA
<< 8, dev
))) {
772 printk(BIOS_DEBUG
, "found VGA at %s\n", dev_path(dev
));
774 if (dev
->on_mainboard
) {
780 /* It isn't safe to enable all VGA cards. */
781 dev
->command
&= ~(PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
787 if (CONFIG_ONBOARD_VGA_IS_PRIMARY
&& vga_onboard
)
790 /* If we prefer plugin VGA over chipset VGA, the chipset might
792 if (!CONFIG_ONBOARD_VGA_IS_PRIMARY
&& (vga
!= vga_onboard
) &&
793 vga_onboard
&& vga_onboard
->ops
&& vga_onboard
->ops
->disable
) {
794 printk(BIOS_DEBUG
, "Use plugin graphics over integrated.\n");
795 vga_onboard
->ops
->disable(vga_onboard
);
799 /* VGA is first add-on card or the only onboard VGA. */
800 printk(BIOS_DEBUG
, "Setting up VGA for %s\n", dev_path(vga
));
801 /* All legacy VGA cards have MEM & I/O space registers. */
802 vga
->command
|= (PCI_COMMAND_MEMORY
| PCI_COMMAND_IO
);
807 /* Now walk up the bridges setting the VGA enable. */
809 printk(BIOS_DEBUG
, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
811 bus
->bridge_ctrl
|= PCI_BRIDGE_CTL_VGA
;
812 bus
= (bus
== bus
->dev
->bus
) ? 0 : bus
->dev
->bus
;
817 * Assign the computed resources to the devices on the bus.
819 * Use the device specific set_resources() method to store the computed
820 * resources to hardware. For bridge devices, the set_resources() method
821 * has to recurse into every down stream buses.
824 * assign_resources() -> device_operation::set_resources()
825 * device_operation::set_resources() -> assign_resources()
827 * @param bus Pointer to the structure for this bus.
829 void assign_resources(struct bus
*bus
)
831 struct device
*curdev
;
833 printk(BIOS_SPEW
, "%s assign_resources, bus %d link: %d\n",
834 dev_path(bus
->dev
), bus
->secondary
, bus
->link_num
);
836 for (curdev
= bus
->children
; curdev
; curdev
= curdev
->sibling
) {
837 if (!curdev
->enabled
|| !curdev
->resource_list
)
840 if (!curdev
->ops
|| !curdev
->ops
->set_resources
) {
841 printk(BIOS_ERR
, "%s missing set_resources\n",
845 post_log_path(curdev
);
846 curdev
->ops
->set_resources(curdev
);
849 printk(BIOS_SPEW
, "%s assign_resources, bus %d link: %d\n",
850 dev_path(bus
->dev
), bus
->secondary
, bus
->link_num
);
854 * Enable the resources for devices on a link.
856 * Enable resources of the device by calling the device specific
857 * enable_resources() method.
859 * The parent's resources should be enabled first to avoid having enabling
860 * order problem. This is done by calling the parent's enable_resources()
861 * method before its children's enable_resources() methods.
863 * @param link The link whose devices' resources are to be enabled.
865 static void enable_resources(struct bus
*link
)
870 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
871 if (dev
->enabled
&& dev
->ops
&& dev
->ops
->enable_resources
) {
873 dev
->ops
->enable_resources(dev
);
877 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
878 for (c_link
= dev
->link_list
; c_link
; c_link
= c_link
->next
)
879 enable_resources(c_link
);
885 * Reset all of the devices on a bus and clear the bus's reset_needed flag.
887 * @param bus Pointer to the bus structure.
888 * @return 1 if the bus was successfully reset, 0 otherwise.
890 int reset_bus(struct bus
*bus
)
892 if (bus
&& bus
->dev
&& bus
->dev
->ops
&& bus
->dev
->ops
->reset_bus
) {
893 bus
->dev
->ops
->reset_bus(bus
);
894 bus
->reset_needed
= 0;
901 * Scan for devices on a bus.
903 * If there are bridges on the bus, recursively scan the buses behind the
904 * bridges. If the setting up and tuning of the bus causes a reset to be
905 * required, reset the bus and scan it again.
907 * @param busdev Pointer to the bus device.
909 static void scan_bus(struct device
*busdev
)
913 if (!busdev
->enabled
)
916 printk(BIOS_SPEW
, "%s scanning...\n", dev_path(busdev
));
918 post_log_path(busdev
);
921 while (do_scan_bus
) {
923 busdev
->ops
->scan_bus(busdev
);
925 for (link
= busdev
->link_list
; link
; link
= link
->next
) {
926 if (link
->reset_needed
) {
930 busdev
->bus
->reset_needed
= 1;
936 void scan_bridges(struct bus
*bus
)
938 struct device
*child
;
940 for (child
= bus
->children
; child
; child
= child
->sibling
) {
941 if (!child
->ops
|| !child
->ops
->scan_bus
)
948 * Determine the existence of devices and extend the device tree.
950 * Most of the devices in the system are listed in the mainboard devicetree.cb
951 * file. The device structures for these devices are generated at compile
952 * time by the config tool and are organized into the device tree. This
953 * function determines if the devices created at compile time actually exist
954 * in the physical system.
956 * For devices in the physical system but not listed in devicetree.cb,
957 * the device structures have to be created at run time and attached to the
960 * This function starts from the root device 'dev_root', scans the buses in
961 * the system recursively, and modifies the device tree according to the
962 * result of the probe.
964 * This function has no idea how to scan and probe buses and devices at all.
965 * It depends on the bus/device specific scan_bus() method to do it. The
966 * scan_bus() method also has to create the device structure and attach
967 * it to the device tree.
969 void dev_enumerate(void)
973 printk(BIOS_INFO
, "Enumerating buses...\n");
977 show_all_devs(BIOS_SPEW
, "Before device enumeration.");
978 printk(BIOS_SPEW
, "Compare with tree...\n");
979 show_devs_tree(root
, BIOS_SPEW
, 0, 0);
981 if (root
->chip_ops
&& root
->chip_ops
->enable_dev
)
982 root
->chip_ops
->enable_dev(root
);
984 if (!root
->ops
|| !root
->ops
->scan_bus
) {
985 printk(BIOS_ERR
, "dev_root missing scan_bus operation");
990 printk(BIOS_INFO
, "done\n");
994 * Configure devices on the devices tree.
996 * Starting at the root of the device tree, travel it recursively in two
997 * passes. In the first pass, we compute and allocate resources (ranges)
998 * required by each device. In the second pass, the resources ranges are
999 * relocated to their final position and stored to the hardware.
1001 * I/O resources grow upward. MEM resources grow downward.
1003 * Since the assignment is hierarchical we set the values into the dev_root
1006 void dev_configure(void)
1008 struct resource
*res
;
1009 struct device
*root
;
1010 struct device
*child
;
1012 set_vga_bridge_bits();
1014 printk(BIOS_INFO
, "Allocating resources...\n");
1019 * Each domain should create resources which contain the entire address
1020 * space for IO, MEM, and PREFMEM resources in the domain. The
1021 * allocation of device resources will be done from this address space.
1024 /* Read the resources for the entire tree. */
1026 printk(BIOS_INFO
, "Reading resources...\n");
1027 read_resources(root
->link_list
);
1028 printk(BIOS_INFO
, "Done reading resources.\n");
1030 print_resource_tree(root
, BIOS_SPEW
, "After reading.");
1032 /* Compute resources for all domains. */
1033 for (child
= root
->link_list
->children
; child
; child
= child
->sibling
) {
1034 if (!(child
->path
.type
== DEVICE_PATH_DOMAIN
))
1036 post_log_path(child
);
1037 for (res
= child
->resource_list
; res
; res
= res
->next
) {
1038 if (res
->flags
& IORESOURCE_FIXED
)
1040 if (res
->flags
& IORESOURCE_MEM
) {
1041 compute_resources(child
->link_list
,
1042 res
, IORESOURCE_TYPE_MASK
, IORESOURCE_MEM
);
1045 if (res
->flags
& IORESOURCE_IO
) {
1046 compute_resources(child
->link_list
,
1047 res
, IORESOURCE_TYPE_MASK
, IORESOURCE_IO
);
1053 /* For all domains. */
1054 for (child
= root
->link_list
->children
; child
; child
=child
->sibling
)
1055 if (child
->path
.type
== DEVICE_PATH_DOMAIN
)
1056 avoid_fixed_resources(child
);
1058 /* Store the computed resource allocations into device registers ... */
1059 printk(BIOS_INFO
, "Setting resources...\n");
1060 for (child
= root
->link_list
->children
; child
; child
= child
->sibling
) {
1061 if (!(child
->path
.type
== DEVICE_PATH_DOMAIN
))
1063 post_log_path(child
);
1064 for (res
= child
->resource_list
; res
; res
= res
->next
) {
1065 if (res
->flags
& IORESOURCE_FIXED
)
1067 if (res
->flags
& IORESOURCE_MEM
) {
1068 allocate_resources(child
->link_list
,
1069 res
, IORESOURCE_TYPE_MASK
, IORESOURCE_MEM
);
1072 if (res
->flags
& IORESOURCE_IO
) {
1073 allocate_resources(child
->link_list
,
1074 res
, IORESOURCE_TYPE_MASK
, IORESOURCE_IO
);
1079 assign_resources(root
->link_list
);
1080 printk(BIOS_INFO
, "Done setting resources.\n");
1081 print_resource_tree(root
, BIOS_SPEW
, "After assigning values.");
1083 printk(BIOS_INFO
, "Done allocating resources.\n");
1087 * Enable devices on the device tree.
1089 * Starting at the root, walk the tree and enable all devices/bridges by
1090 * calling the device's enable_resources() method.
1092 void dev_enable(void)
1096 printk(BIOS_INFO
, "Enabling resources...\n");
1098 /* Now enable everything. */
1099 for (link
= dev_root
.link_list
; link
; link
= link
->next
)
1100 enable_resources(link
);
1102 printk(BIOS_INFO
, "done.\n");
1106 * Initialize a specific device.
1108 * The parent should be initialized first to avoid having an ordering problem.
1109 * This is done by calling the parent's init() method before its children's
1112 * @param dev The device to be initialized.
1114 static void init_dev(struct device
*dev
)
1119 if (!dev
->initialized
&& dev
->ops
&& dev
->ops
->init
) {
1120 #if CONFIG_HAVE_MONOTONIC_TIMER
1121 struct stopwatch sw
;
1122 stopwatch_init(&sw
);
1124 if (dev
->path
.type
== DEVICE_PATH_I2C
) {
1125 printk(BIOS_DEBUG
, "smbus: %s[%d]->",
1126 dev_path(dev
->bus
->dev
), dev
->bus
->link_num
);
1129 printk(BIOS_DEBUG
, "%s init ...\n", dev_path(dev
));
1130 dev
->initialized
= 1;
1131 dev
->ops
->init(dev
);
1132 #if CONFIG_HAVE_MONOTONIC_TIMER
1133 printk(BIOS_DEBUG
, "%s init finished in %ld usecs\n", dev_path(dev
),
1134 stopwatch_duration_usecs(&sw
));
1139 static void init_link(struct bus
*link
)
1144 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
1145 post_code(POST_BS_DEV_INIT
);
1150 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
1151 for (c_link
= dev
->link_list
; c_link
; c_link
= c_link
->next
)
1157 * Initialize all devices in the global device tree.
1159 * Starting at the root device, call the device's init() method to do
1160 * device-specific setup, then call each child's init() method.
1162 void dev_initialize(void)
1166 printk(BIOS_INFO
, "Initializing devices...\n");
1169 /* Ensure EBDA is prepared before Option ROMs. */
1170 setup_default_ebda();
1173 /* First call the mainboard init. */
1174 init_dev(&dev_root
);
1176 /* Now initialize everything. */
1177 for (link
= dev_root
.link_list
; link
; link
= link
->next
)
1181 printk(BIOS_INFO
, "Devices initialized\n");
1182 show_all_devs(BIOS_SPEW
, "After init.");
1186 * Finalize a specific device.
1188 * The parent should be finalized first to avoid having an ordering problem.
1189 * This is done by calling the parent's final() method before its childrens'
1192 * @param dev The device to be initialized.
1194 static void final_dev(struct device
*dev
)
1199 if (dev
->ops
&& dev
->ops
->final
) {
1200 printk(BIOS_DEBUG
, "%s final\n", dev_path(dev
));
1201 dev
->ops
->final(dev
);
1205 static void final_link(struct bus
*link
)
1210 for (dev
= link
->children
; dev
; dev
= dev
->sibling
)
1213 for (dev
= link
->children
; dev
; dev
= dev
->sibling
) {
1214 for (c_link
= dev
->link_list
; c_link
; c_link
= c_link
->next
)
1219 * Finalize all devices in the global device tree.
1221 * Starting at the root device, call the device's final() method to do
1222 * device-specific cleanup, then call each child's final() method.
1224 void dev_finalize(void)
1228 printk(BIOS_INFO
, "Finalize devices...\n");
1230 /* First call the mainboard finalize. */
1231 final_dev(&dev_root
);
1233 /* Now finalize everything. */
1234 for (link
= dev_root
.link_list
; link
; link
= link
->next
)
1237 printk(BIOS_INFO
, "Devices finalized\n");