m5a99x-evo: ugly quirks, but WOOT: ohai seabios :-)
[coreboot.git] / src / devices / device.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * It was originally based on the Linux kernel (arch/i386/kernel/pci-pc.c).
5  *
6  * Modifications are:
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>
16  */
17
18 /*
19  *      (c) 1999--2000 Martin Mares <mj@suse.cz>
20  */
21
22 /*
23  * Lots of mods by Ron Minnich <rminnich@lanl.gov>, with
24  * the final architecture guidance from Tom Merritt <tjm@codegen.com>.
25  *
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...
30  *
31  * Lots of cleanups by Eric Biederman to handle bridges, and to
32  * handle resource allocation for non-PCI devices.
33  */
34
35 #include <console/console.h>
36 #include <bitops.h>
37 #include <arch/io.h>
38 #include <device/device.h>
39 #include <device/pci.h>
40 #include <device/pci_ids.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <smp/spinlock.h>
44 #if CONFIG_ARCH_X86
45 #include <arch/ebda.h>
46 #endif
47
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;
54
55 DECLARE_SPIN_LOCK(dev_lock)
56
57 /**
58  * Allocate a new device structure.
59  *
60  * Allocte a new device structure and attach it to the device tree as a
61  * child of the parent bus.
62  *
63  * @param parent Parent bus the newly created device should be attached to.
64  * @param path Path to the device to be created.
65  * @return Pointer to the newly created device structure.
66  *
67  * @see device_path
68  */
69 device_t alloc_dev(struct bus *parent, struct device_path *path)
70 {
71         device_t dev, child;
72
73         spin_lock(&dev_lock);
74
75         /* Find the last child of our parent. */
76         for (child = parent->children; child && child->sibling; /* */ )
77                 child = child->sibling;
78
79         dev = malloc(sizeof(*dev));
80         if (dev == 0)
81                 die("alloc_dev(): out of memory.\n");
82
83         memset(dev, 0, sizeof(*dev));
84         memcpy(&dev->path, path, sizeof(*path));
85
86         /* By default devices are enabled. */
87         dev->enabled = 1;
88
89         /* Add the new device to the list of children of the bus. */
90         dev->bus = parent;
91         if (child)
92                 child->sibling = dev;
93         else
94                 parent->children = dev;
95
96         /* Append a new device to the global device list.
97          * The list is used to find devices once everything is set up.
98          */
99         last_dev->next = dev;
100         last_dev = dev;
101
102         spin_unlock(&dev_lock);
103         return dev;
104 }
105
106 /**
107  * Round a number up to an alignment.
108  *
109  * @param val The starting value.
110  * @param roundup Alignment as a power of two.
111  * @return Rounded up number.
112  */
113 static resource_t round(resource_t val, unsigned long pow)
114 {
115         resource_t mask;
116         mask = (1ULL << pow) - 1ULL;
117         val += mask;
118         val &= ~mask;
119         return val;
120 }
121
122 /**
123  * Read the resources on all devices of a given bus.
124  *
125  * @param bus Bus to read the resources on.
126  */
127 static void read_resources(struct bus *bus)
128 {
129         struct device *curdev;
130
131         printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
132                __func__, bus->secondary, bus->link_num);
133
134         /* Walk through all devices and find which resources they need. */
135         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
136                 struct bus *link;
137
138                 if (!curdev->enabled)
139                         continue;
140
141                 if (!curdev->ops || !curdev->ops->read_resources) {
142                         printk(BIOS_ERR, "%s missing read_resources\n",
143                                dev_path(curdev));
144                         continue;
145                 }
146                 curdev->ops->read_resources(curdev);
147
148                 /* Read in the resources behind the current device's links. */
149                 for (link = curdev->link_list; link; link = link->next)
150                         read_resources(link);
151         }
152         printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
153                dev_path(bus->dev), bus->secondary, bus->link_num);
154 }
155
156 struct pick_largest_state {
157         struct resource *last;
158         struct device *result_dev;
159         struct resource *result;
160         int seen_last;
161 };
162
163 static void pick_largest_resource(void *gp, struct device *dev,
164                                   struct resource *resource)
165 {
166         struct pick_largest_state *state = gp;
167         struct resource *last;
168
169         last = state->last;
170
171         /* Be certain to pick the successor to last. */
172         if (resource == last) {
173                 state->seen_last = 1;
174                 return;
175         }
176         if (resource->flags & IORESOURCE_FIXED)
177                 return; /* Skip it. */
178         if (last && ((last->align < resource->align) ||
179                      ((last->align == resource->align) &&
180                       (last->size < resource->size)) ||
181                      ((last->align == resource->align) &&
182                       (last->size == resource->size) && (!state->seen_last)))) {
183                 return;
184         }
185         if (!state->result ||
186             (state->result->align < resource->align) ||
187             ((state->result->align == resource->align) &&
188              (state->result->size < resource->size))) {
189                 state->result_dev = dev;
190                 state->result = resource;
191         }
192 }
193
194 static struct device *largest_resource(struct bus *bus,
195                                        struct resource **result_res,
196                                        unsigned long type_mask,
197                                        unsigned long type)
198 {
199         struct pick_largest_state state;
200
201         state.last = *result_res;
202         state.result_dev = NULL;
203         state.result = NULL;
204         state.seen_last = 0;
205
206         search_bus_resources(bus, type_mask, type, pick_largest_resource,
207                              &state);
208
209         *result_res = state.result;
210         return state.result_dev;
211 }
212
213 /**
214  * This function is the guts of the resource allocator.
215  *
216  * The problem.
217  *  - Allocate resource locations for every device.
218  *  - Don't overlap, and follow the rules of bridges.
219  *  - Don't overlap with resources in fixed locations.
220  *  - Be efficient so we don't have ugly strategies.
221  *
222  * The strategy.
223  * - Devices that have fixed addresses are the minority so don't
224  *   worry about them too much. Instead only use part of the address
225  *   space for devices with programmable addresses. This easily handles
226  *   everything except bridges.
227  *
228  * - PCI devices are required to have their sizes and their alignments
229  *   equal. In this case an optimal solution to the packing problem
230  *   exists. Allocate all devices from highest alignment to least
231  *   alignment or vice versa. Use this.
232  *
233  * - So we can handle more than PCI run two allocation passes on bridges. The
234  *   first to see how large the resources are behind the bridge, and what
235  *   their alignment requirements are. The second to assign a safe address to
236  *   the devices behind the bridge. This allows us to treat a bridge as just
237  *   a device with a couple of resources, and not need to special case it in
238  *   the allocator. Also this allows handling of other types of bridges.
239  *
240  * @param bus The bus we are traversing.
241  * @param bridge The bridge resource which must contain the bus' resources.
242  * @param type_mask This value gets ANDed with the resource type.
243  * @param type This value must match the result of the AND.
244  * @return TODO
245  */
246 static void compute_resources(struct bus *bus, struct resource *bridge,
247                               unsigned long type_mask, unsigned long type)
248 {
249         struct device *dev;
250         struct resource *resource;
251         resource_t base;
252         base = round(bridge->base, bridge->align);
253
254         printk(BIOS_SPEW,  "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
255                " limit: %llx\n", dev_path(bus->dev), __func__,
256                (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
257                "prefmem" : "mem", base, bridge->size, bridge->align,
258                bridge->gran, bridge->limit);
259
260         /* For each child which is a bridge, compute the resource needs. */
261         for (dev = bus->children; dev; dev = dev->sibling) {
262                 struct resource *child_bridge;
263
264                 if (!dev->link_list)
265                         continue;
266
267                 /* Find the resources with matching type flags. */
268                 for (child_bridge = dev->resource_list; child_bridge;
269                      child_bridge = child_bridge->next) {
270                         struct bus* link;
271
272                         if (!(child_bridge->flags & IORESOURCE_BRIDGE)
273                             || (child_bridge->flags & type_mask) != type)
274                                 continue;
275
276                         /*
277                          * Split prefetchable memory if combined. Many domains
278                          * use the same address space for prefetchable memory
279                          * and non-prefetchable memory. Bridges below them need
280                          * it separated. Add the PREFETCH flag to the type_mask
281                          * and type.
282                          */
283                         link = dev->link_list;
284                         while (link && link->link_num !=
285                                         IOINDEX_LINK(child_bridge->index))
286                                 link = link->next;
287
288                         if (link == NULL) {
289                                 printk(BIOS_ERR, "link %ld not found on %s\n",
290                                        IOINDEX_LINK(child_bridge->index),
291                                        dev_path(dev));
292                         }
293
294                         compute_resources(link, child_bridge,
295                                           type_mask | IORESOURCE_PREFETCH,
296                                           type | (child_bridge->flags &
297                                                   IORESOURCE_PREFETCH));
298                 }
299         }
300
301         /* Remember we haven't found anything yet. */
302         resource = NULL;
303
304         /*
305          * Walk through all the resources on the current bus and compute the
306          * amount of address space taken by them. Take granularity and
307          * alignment into account.
308          */
309         while ((dev = largest_resource(bus, &resource, type_mask, type))) {
310
311                 /* Size 0 resources can be skipped. */
312                 if (!resource->size)
313                         continue;
314
315                 /* Propagate the resource alignment to the bridge resource. */
316                 if (resource->align > bridge->align)
317                         bridge->align = resource->align;
318
319                 /* Propagate the resource limit to the bridge register. */
320                 if (bridge->limit > resource->limit)
321                         bridge->limit = resource->limit;
322
323                 /* Warn if it looks like APICs aren't declared. */
324                 if ((resource->limit == 0xffffffff) &&
325                     (resource->flags & IORESOURCE_ASSIGNED)) {
326                         printk(BIOS_ERR,
327                                "Resource limit looks wrong! (no APIC?)\n");
328                         printk(BIOS_ERR, "%s %02lx limit %08llx\n",
329                                dev_path(dev), resource->index, resource->limit);
330                 }
331
332                 if (resource->flags & IORESOURCE_IO) {
333                         /*
334                          * Don't allow potential aliases over the legacy PCI
335                          * expansion card addresses. The legacy PCI decodes
336                          * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
337                          * 0x00 - 0xff can be used out of each 0x400 block of
338                          * I/O space.
339                          */
340                         if ((base & 0x300) != 0) {
341                                 base = (base & ~0x3ff) + 0x400;
342                         }
343                         /*
344                          * Don't allow allocations in the VGA I/O range.
345                          * PCI has special cases for that.
346                          */
347                         else if ((base >= 0x3b0) && (base <= 0x3df)) {
348                                 base = 0x3e0;
349                         }
350                 }
351                 /* Base must be aligned. */
352                 base = round(base, resource->align);
353                 resource->base = base;
354                 base += resource->size;
355
356                 printk(BIOS_SPEW, "%s %02lx *  [0x%llx - 0x%llx] %s\n",
357                        dev_path(dev), resource->index, resource->base,
358                        resource->base + resource->size - 1,
359                        (resource->flags & IORESOURCE_IO) ? "io" :
360                        (resource->flags & IORESOURCE_PREFETCH) ?
361                         "prefmem" : "mem");
362         }
363
364         /*
365          * A PCI bridge resource does not need to be a power of two size, but
366          * it does have a minimum granularity. Round the size up to that
367          * minimum granularity so we know not to place something else at an
368          * address postitively decoded by the bridge.
369          */
370         bridge->size = round(base, bridge->gran) -
371                        round(bridge->base, bridge->align);
372
373         printk(BIOS_SPEW, "%s %s_%s: base: %llx size: %llx align: %d gran: %d"
374                " limit: %llx done\n", dev_path(bus->dev), __func__,
375                (bridge->flags & IORESOURCE_IO) ? "io" :
376                (bridge->flags & IORESOURCE_PREFETCH) ? "prefmem" : "mem",
377                base, bridge->size, bridge->align, bridge->gran, bridge->limit);
378 }
379
380 /**
381  * This function is the second part of the resource allocator.
382  *
383  * See the compute_resources function for a more detailed explanation.
384  *
385  * This function assigns the resources a value.
386  *
387  * @param bus The bus we are traversing.
388  * @param bridge The bridge resource which must contain the bus' resources.
389  * @param type_mask This value gets ANDed with the resource type.
390  * @param type This value must match the result of the AND.
391  *
392  * @see compute_resources
393  */
394 static void allocate_resources(struct bus *bus, struct resource *bridge,
395                                unsigned long type_mask, unsigned long type)
396 {
397         struct device *dev;
398         struct resource *resource;
399         resource_t base;
400         base = bridge->base;
401
402         printk(BIOS_SPEW, "%s %s_%s: base:%llx size:%llx align:%d gran:%d "
403                "limit:%llx\n", dev_path(bus->dev), __func__,
404                (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
405                "prefmem" : "mem",
406                base, bridge->size, bridge->align, bridge->gran, bridge->limit);
407
408         /* Remember we haven't found anything yet. */
409         resource = NULL;
410
411         /*
412          * Walk through all the resources on the current bus and allocate them
413          * address space.
414          */
415         while ((dev = largest_resource(bus, &resource, type_mask, type))) {
416
417                 /* Propagate the bridge limit to the resource register. */
418                 if (resource->limit > bridge->limit)
419                         resource->limit = bridge->limit;
420
421                 /* Size 0 resources can be skipped. */
422                 if (!resource->size) {
423                         /* Set the base to limit so it doesn't confuse tolm. */
424                         resource->base = resource->limit;
425                         resource->flags |= IORESOURCE_ASSIGNED;
426                         continue;
427                 }
428
429                 if (resource->flags & IORESOURCE_IO) {
430                         /*
431                          * Don't allow potential aliases over the legacy PCI
432                          * expansion card addresses. The legacy PCI decodes
433                          * only 10 bits, uses 0x100 - 0x3ff. Therefore, only
434                          * 0x00 - 0xff can be used out of each 0x400 block of
435                          * I/O space.
436                          */
437                         if ((base & 0x300) != 0) {
438                                 base = (base & ~0x3ff) + 0x400;
439                         }
440                         /*
441                          * Don't allow allocations in the VGA I/O range.
442                          * PCI has special cases for that.
443                          */
444                         else if ((base >= 0x3b0) && (base <= 0x3df)) {
445                                 base = 0x3e0;
446                         }
447                 }
448
449                 if ((round(base, resource->align) + resource->size - 1) <=
450                     resource->limit) {
451                         /* Base must be aligned. */
452                         base = round(base, resource->align);
453                         resource->base = base;
454                         resource->flags |= IORESOURCE_ASSIGNED;
455                         resource->flags &= ~IORESOURCE_STORED;
456                         base += resource->size;
457                 } else {
458                         printk(BIOS_ERR, "!! Resource didn't fit !!\n");
459                         printk(BIOS_ERR, "   aligned base %llx size %llx "
460                                "limit %llx\n", round(base, resource->align),
461                                resource->size, resource->limit);
462                         printk(BIOS_ERR, "   %llx needs to be <= %llx "
463                                "(limit)\n", (round(base, resource->align) +
464                                 resource->size) - 1, resource->limit);
465                         printk(BIOS_ERR, "   %s%s %02lx *  [0x%llx - 0x%llx]"
466                                " %s\n", (resource->flags & IORESOURCE_ASSIGNED)
467                                ? "Assigned: " : "", dev_path(dev),
468                                resource->index, resource->base,
469                                resource->base + resource->size - 1,
470                                (resource->flags & IORESOURCE_IO) ? "io"
471                                : (resource->flags & IORESOURCE_PREFETCH)
472                                ? "prefmem" : "mem");
473                 }
474
475                 printk(BIOS_SPEW, "%s%s %02lx *  [0x%llx - 0x%llx] %s\n",
476                        (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
477                        : "", dev_path(dev), resource->index, resource->base,
478                        resource->size ? resource->base + resource->size - 1 :
479                        resource->base, (resource->flags & IORESOURCE_IO)
480                        ? "io" : (resource->flags & IORESOURCE_PREFETCH)
481                        ? "prefmem" : "mem");
482         }
483
484         /*
485          * A PCI bridge resource does not need to be a power of two size, but
486          * it does have a minimum granularity. Round the size up to that
487          * minimum granularity so we know not to place something else at an
488          * address positively decoded by the bridge.
489          */
490
491         bridge->flags |= IORESOURCE_ASSIGNED;
492
493         printk(BIOS_SPEW, "%s %s_%s: next_base: %llx size: %llx align: %d "
494                "gran: %d done\n", dev_path(bus->dev), __func__,
495                (type & IORESOURCE_IO) ? "io" : (type & IORESOURCE_PREFETCH) ?
496                "prefmem" : "mem", base, bridge->size, bridge->align,
497                bridge->gran);
498
499         /* For each child which is a bridge, allocate_resources. */
500         for (dev = bus->children; dev; dev = dev->sibling) {
501                 struct resource *child_bridge;
502
503                 if (!dev->link_list)
504                         continue;
505
506                 /* Find the resources with matching type flags. */
507                 for (child_bridge = dev->resource_list; child_bridge;
508                      child_bridge = child_bridge->next) {
509                         struct bus* link;
510
511                         if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
512                             (child_bridge->flags & type_mask) != type)
513                                 continue;
514
515                         /*
516                          * Split prefetchable memory if combined. Many domains
517                          * use the same address space for prefetchable memory
518                          * and non-prefetchable memory. Bridges below them need
519                          * it separated. Add the PREFETCH flag to the type_mask
520                          * and type.
521                          */
522                         link = dev->link_list;
523                         while (link && link->link_num !=
524                                        IOINDEX_LINK(child_bridge->index))
525                                 link = link->next;
526                         if (link == NULL)
527                                 printk(BIOS_ERR, "link %ld not found on %s\n",
528                                        IOINDEX_LINK(child_bridge->index),
529                                        dev_path(dev));
530
531                         allocate_resources(link, child_bridge,
532                                            type_mask | IORESOURCE_PREFETCH,
533                                            type | (child_bridge->flags &
534                                                    IORESOURCE_PREFETCH));
535                 }
536         }
537 }
538
539 #if CONFIG_PCI_64BIT_PREF_MEM == 1
540 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
541 #else
542 #define MEM_MASK (IORESOURCE_MEM)
543 #endif
544
545 #define IO_MASK   (IORESOURCE_IO)
546 #define PREF_TYPE (IORESOURCE_PREFETCH | IORESOURCE_MEM)
547 #define MEM_TYPE  (IORESOURCE_MEM)
548 #define IO_TYPE   (IORESOURCE_IO)
549
550 struct constraints {
551         struct resource pref, io, mem;
552 };
553
554 static void constrain_resources(struct device *dev, struct constraints* limits)
555 {
556         struct device *child;
557         struct resource *res;
558         struct resource *lim;
559         struct bus *link;
560
561         printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
562
563         /* Constrain limits based on the fixed resources of this device. */
564         for (res = dev->resource_list; res; res = res->next) {
565                 if (!(res->flags & IORESOURCE_FIXED))
566                         continue;
567                 if (!res->size) {
568                         /* It makes no sense to have 0-sized, fixed resources.*/
569                         printk(BIOS_ERR, "skipping %s@%lx fixed resource, "
570                                "size=0!\n", dev_path(dev), res->index);
571                         continue;
572                 }
573
574                 /* PREFETCH, MEM, or I/O - skip any others. */
575                 if ((res->flags & MEM_MASK) == PREF_TYPE)
576                         lim = &limits->pref;
577                 else if ((res->flags & MEM_MASK) == MEM_TYPE)
578                         lim = &limits->mem;
579                 else if ((res->flags & IO_MASK) == IO_TYPE)
580                         lim = &limits->io;
581                 else
582                         continue;
583
584                 /*
585                  * Is it a fixed resource outside the current known region?
586                  * If so, we don't have to consider it - it will be handled
587                  * correctly and doesn't affect current region's limits.
588                  */
589                 if (((res->base + res->size -1) < lim->base)
590                     || (res->base > lim->limit))
591                         continue;
592
593                 /*
594                  * Choose to be above or below fixed resources. This check is
595                  * signed so that "negative" amounts of space are handled
596                  * correctly.
597                  */
598                 if ((signed long long)(lim->limit - (res->base + res->size -1))
599                     > (signed long long)(res->base - lim->base))
600                         lim->base = res->base + res->size;
601                 else
602                         lim->limit = res->base -1;
603         }
604
605         /* Descend into every enabled child and look for fixed resources. */
606         for (link = dev->link_list; link; link = link->next) {
607                 for (child = link->children; child; child = child->sibling) {
608                         if (child->enabled)
609                                 constrain_resources(child, limits);
610                 }
611         }
612 }
613
614 static void avoid_fixed_resources(struct device *dev)
615 {
616         struct constraints limits;
617         struct resource *res;
618
619         printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
620
621         /* Initialize constraints to maximum size. */
622         limits.pref.base = 0;
623         limits.pref.limit = 0xffffffffffffffffULL;
624         limits.io.base = 0;
625         limits.io.limit = 0xffffffffffffffffULL;
626         limits.mem.base = 0;
627         limits.mem.limit = 0xffffffffffffffffULL;
628
629         /* Constrain the limits to dev's initial resources. */
630         for (res = dev->resource_list; res; res = res->next) {
631                 if ((res->flags & IORESOURCE_FIXED))
632                         continue;
633                 printk(BIOS_SPEW, "%s:@%s %02lx limit %08llx\n", __func__,
634                        dev_path(dev), res->index, res->limit);
635                 if ((res->flags & MEM_MASK) == PREF_TYPE &&
636                     (res->limit < limits.pref.limit))
637                         limits.pref.limit = res->limit;
638                 if ((res->flags & MEM_MASK) == MEM_TYPE &&
639                     (res->limit < limits.mem.limit))
640                         limits.mem.limit = res->limit;
641                 if ((res->flags & IO_MASK) == IO_TYPE &&
642                     (res->limit < limits.io.limit))
643                         limits.io.limit = res->limit;
644         }
645
646         /* Look through the tree for fixed resources and update the limits. */
647         constrain_resources(dev, &limits);
648
649         /* Update dev's resources with new limits. */
650         for (res = dev->resource_list; res; res = res->next) {
651                 struct resource *lim;
652
653                 if ((res->flags & IORESOURCE_FIXED))
654                         continue;
655
656                 /* PREFETCH, MEM, or I/O - skip any others. */
657                 if ((res->flags & MEM_MASK) == PREF_TYPE)
658                         lim = &limits.pref;
659                 else if ((res->flags & MEM_MASK) == MEM_TYPE)
660                         lim = &limits.mem;
661                 else if ((res->flags & IO_MASK) == IO_TYPE)
662                         lim = &limits.io;
663                 else
664                         continue;
665
666                 printk(BIOS_SPEW, "%s2: %s@%02lx limit %08llx\n", __func__,
667                              dev_path(dev), res->index, res->limit);
668                 printk(BIOS_SPEW, "\tlim->base %08llx lim->limit %08llx\n",
669                              lim->base, lim->limit);
670
671                 /* Is the resource outside the limits? */
672                 if (lim->base > res->base)
673                         res->base = lim->base;
674                 if (res->limit > lim->limit)
675                         res->limit = lim->limit;
676         }
677 }
678
679 #if CONFIG_VGA_BRIDGE_SETUP == 1
680 device_t vga_pri = 0;
681 static void set_vga_bridge_bits(void)
682 {
683         /*
684          * FIXME: Modify set_vga_bridge() so it is less PCI centric!
685          * This function knows too much about PCI stuff, it should be just
686          * an iterator/visitor.
687          */
688
689         /* FIXME: Handle the VGA palette snooping. */
690         struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
691         struct bus *bus;
692
693         bus = 0;
694         vga = 0;
695         vga_onboard = 0;
696         vga_first = 0;
697         vga_last = 0;
698
699         for (dev = all_devices; dev; dev = dev->next) {
700
701                 if (!dev->enabled)
702                         continue;
703
704                 if (((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) &&
705                     ((dev->class >> 8) != PCI_CLASS_DISPLAY_OTHER)) {
706                         if (!vga_first) {
707                                 if (dev->on_mainboard)
708                                         vga_onboard = dev;
709                                 else
710                                         vga_first = dev;
711                         } else {
712                                 if (dev->on_mainboard)
713                                         vga_onboard = dev;
714                                 else
715                                         vga_last = dev;
716                         }
717
718                         /* It isn't safe to enable other VGA cards. */
719                         dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
720                 }
721         }
722
723         vga = vga_last;
724
725         if (!vga)
726                 vga = vga_first;
727
728 #if CONFIG_ONBOARD_VGA_IS_PRIMARY == 1
729         if (vga_onboard)        /* Will use onboard VGA as primary. */
730 #else
731         if (!vga)               /* Will use last add-on adapter as primary. */
732 #endif
733         {
734                 vga = vga_onboard;
735         }
736
737         if (vga) {
738                 /* VGA is first add-on card or the only onboard VGA. */
739                 printk(BIOS_DEBUG, "Setting up VGA for %s\n", dev_path(vga));
740                 /* All legacy VGA cards have MEM & I/O space registers. */
741                 vga->command |= (PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
742                 vga_pri = vga;
743                 bus = vga->bus;
744         }
745
746         /* Now walk up the bridges setting the VGA enable. */
747         while (bus) {
748                 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
749                        dev_path(bus->dev));
750                 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
751                 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
752         }
753 }
754
755 #endif
756
757 /**
758  * Assign the computed resources to the devices on the bus.
759  *
760  * Use the device specific set_resources() method to store the computed
761  * resources to hardware. For bridge devices, the set_resources() method
762  * has to recurse into every down stream buses.
763  *
764  * Mutual recursion:
765  *      assign_resources() -> device_operation::set_resources()
766  *      device_operation::set_resources() -> assign_resources()
767  *
768  * @param bus Pointer to the structure for this bus.
769  */
770 void assign_resources(struct bus *bus)
771 {
772         struct device *curdev;
773
774         printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
775                dev_path(bus->dev), bus->secondary, bus->link_num);
776
777         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
778                 if (!curdev->enabled || !curdev->resource_list)
779                         continue;
780
781                 if (!curdev->ops || !curdev->ops->set_resources) {
782                         printk(BIOS_ERR, "%s missing set_resources\n",
783                                dev_path(curdev));
784                         continue;
785                 }
786                 curdev->ops->set_resources(curdev);
787         }
788         printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
789                dev_path(bus->dev), bus->secondary, bus->link_num);
790 }
791
792 /**
793  * Enable the resources for devices on a link.
794  *
795  * Enable resources of the device by calling the device specific
796  * enable_resources() method.
797  *
798  * The parent's resources should be enabled first to avoid having enabling
799  * order problem. This is done by calling the parent's enable_resources()
800  * method before its childrens' enable_resources() methods.
801  *
802  * @param link The link whose devices' resources are to be enabled.
803  */
804 static void enable_resources(struct bus *link)
805 {
806         struct device *dev;
807         struct bus *c_link;
808
809         for (dev = link->children; dev; dev = dev->sibling) {
810                 if (dev->enabled && dev->ops && dev->ops->enable_resources)
811                         dev->ops->enable_resources(dev);
812         }
813
814         for (dev = link->children; dev; dev = dev->sibling) {
815                 for (c_link = dev->link_list; c_link; c_link = c_link->next)
816                         enable_resources(c_link);
817         }
818 }
819
820 /**
821  * Reset all of the devices on a bus and clear the bus's reset_needed flag.
822  *
823  * @param bus Pointer to the bus structure.
824  * @return 1 if the bus was successfully reset, 0 otherwise.
825  */
826 int reset_bus(struct bus *bus)
827 {
828         if (bus && bus->dev && bus->dev->ops && bus->dev->ops->reset_bus) {
829                 bus->dev->ops->reset_bus(bus);
830                 bus->reset_needed = 0;
831                 return 1;
832         }
833         return 0;
834 }
835
836 /**
837  * Scan for devices on a bus.
838  *
839  * If there are bridges on the bus, recursively scan the buses behind the
840  * bridges. If the setting up and tuning of the bus causes a reset to be
841  * required, reset the bus and scan it again.
842  *
843  * @param busdev Pointer to the bus device.
844  * @param max Current bus number.
845  * @return The maximum bus number found, after scanning all subordinate buses.
846  */
847 unsigned int scan_bus(struct device *busdev, unsigned int max)
848 {
849         unsigned int new_max;
850         int do_scan_bus;
851
852         if (!busdev || !busdev->enabled || !busdev->ops ||
853             !busdev->ops->scan_bus) {
854                 return max;
855         }
856
857         do_scan_bus = 1;
858         while (do_scan_bus) {
859                 struct bus *link;
860                 new_max = busdev->ops->scan_bus(busdev, max);
861                 do_scan_bus = 0;
862                 for (link = busdev->link_list; link; link = link->next) {
863                         if (link->reset_needed) {
864                                 if (reset_bus(link))
865                                         do_scan_bus = 1;
866                                 else
867                                         busdev->bus->reset_needed = 1;
868                         }
869                 }
870         }
871         return new_max;
872 }
873
874 /**
875  * Determine the existence of devices and extend the device tree.
876  *
877  * Most of the devices in the system are listed in the mainboard devicetree.cb
878  * file. The device structures for these devices are generated at compile
879  * time by the config tool and are organized into the device tree. This
880  * function determines if the devices created at compile time actually exist
881  * in the physical system.
882  *
883  * For devices in the physical system but not listed in devicetree.cb,
884  * the device structures have to be created at run time and attached to the
885  * device tree.
886  *
887  * This function starts from the root device 'dev_root', scans the buses in
888  * the system recursively, and modifies the device tree according to the
889  * result of the probe.
890  *
891  * This function has no idea how to scan and probe buses and devices at all.
892  * It depends on the bus/device specific scan_bus() method to do it. The
893  * scan_bus() method also has to create the device structure and attach
894  * it to the device tree.
895  */
896 void dev_enumerate(void)
897 {
898         struct device *root;
899
900         printk(BIOS_INFO, "Enumerating buses...\n");
901
902         root = &dev_root;
903
904         show_all_devs(BIOS_SPEW, "Before device enumeration.");
905         printk(BIOS_SPEW, "Compare with tree...\n");
906         show_devs_tree(root, BIOS_SPEW, 0, 0);
907
908         if (root->chip_ops && root->chip_ops->enable_dev)
909                 root->chip_ops->enable_dev(root);
910
911         if (!root->ops || !root->ops->scan_bus) {
912                 printk(BIOS_ERR, "dev_root missing scan_bus operation");
913                 return;
914         }
915         printk(BIOS_INFO, "Enumerating buses... starting with root now\n");
916         scan_bus(root, 0);
917         printk(BIOS_INFO, "done\n");
918 }
919
920 /**
921  * Configure devices on the devices tree.
922  *
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.
927  *
928  * I/O resources grow upward. MEM resources grow downward.
929  *
930  * Since the assignment is hierarchical we set the values into the dev_root
931  * struct.
932  */
933 void dev_configure(void)
934 {
935         struct resource *res;
936         struct device *root;
937         struct device *child;
938
939 #if CONFIG_VGA_BRIDGE_SETUP == 1
940         set_vga_bridge_bits();
941 #endif
942
943         printk(BIOS_INFO, "Allocating resources...\n");
944
945         root = &dev_root;
946
947         /*
948          * Each domain should create resources which contain the entire address
949          * space for IO, MEM, and PREFMEM resources in the domain. The
950          * allocation of device resources will be done from this address space.
951          */
952
953         /* Read the resources for the entire tree. */
954
955         printk(BIOS_INFO, "Reading resources...\n");
956         read_resources(root->link_list);
957         printk(BIOS_INFO, "Done reading resources.\n");
958
959         print_resource_tree(root, BIOS_SPEW, "After reading.");
960
961         /* Compute resources for all domains. */
962         for (child = root->link_list->children; child; child = child->sibling) {
963                 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
964                         continue;
965                 for (res = child->resource_list; res; res = res->next) {
966                         if (res->flags & IORESOURCE_FIXED)
967                                 continue;
968                         if (res->flags & IORESOURCE_PREFETCH) {
969                                 compute_resources(child->link_list,
970                                                   res, MEM_MASK, PREF_TYPE);
971                                 continue;
972                         }
973                         if (res->flags & IORESOURCE_MEM) {
974                                 compute_resources(child->link_list,
975                                                   res, MEM_MASK, MEM_TYPE);
976                                 continue;
977                         }
978                         if (res->flags & IORESOURCE_IO) {
979                                 compute_resources(child->link_list,
980                                                   res, IO_MASK, IO_TYPE);
981                                 continue;
982                         }
983                 }
984         }
985
986         /* For all domains. */
987         for (child = root->link_list->children; child; child=child->sibling)
988                 if (child->path.type == DEVICE_PATH_PCI_DOMAIN)
989                         avoid_fixed_resources(child);
990
991         /*
992          * Now we need to adjust the resources. MEM resources need to start at
993          * the highest address managable.
994          */
995         for (child = root->link_list->children; child; child = child->sibling) {
996                 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
997                         continue;
998                 for (res = child->resource_list; res; res = res->next) {
999                         if (!(res->flags & IORESOURCE_MEM) ||
1000                             res->flags & IORESOURCE_FIXED)
1001                                 continue;
1002                         res->base = resource_max(res);
1003                 }
1004         }
1005
1006         /* Store the computed resource allocations into device registers ... */
1007         printk(BIOS_INFO, "Setting resources...\n");
1008         for (child = root->link_list->children; child; child = child->sibling) {
1009                 if (!(child->path.type == DEVICE_PATH_PCI_DOMAIN))
1010                         continue;
1011                 for (res = child->resource_list; res; res = res->next) {
1012                         if (res->flags & IORESOURCE_FIXED)
1013                                 continue;
1014                         if (res->flags & IORESOURCE_PREFETCH) {
1015                                 allocate_resources(child->link_list,
1016                                                    res, MEM_MASK, PREF_TYPE);
1017                                 continue;
1018                         }
1019                         if (res->flags & IORESOURCE_MEM) {
1020                                 allocate_resources(child->link_list,
1021                                                    res, MEM_MASK, MEM_TYPE);
1022                                 continue;
1023                         }
1024                         if (res->flags & IORESOURCE_IO) {
1025                                 allocate_resources(child->link_list,
1026                                                    res, IO_MASK, IO_TYPE);
1027                                 continue;
1028                         }
1029                 }
1030         }
1031         assign_resources(root->link_list);
1032         printk(BIOS_INFO, "Done setting resources.\n");
1033         print_resource_tree(root, BIOS_SPEW, "After assigning values.");
1034
1035         printk(BIOS_INFO, "Done allocating resources.\n");
1036 }
1037
1038 /**
1039  * Enable devices on the device tree.
1040  *
1041  * Starting at the root, walk the tree and enable all devices/bridges by
1042  * calling the device's enable_resources() method.
1043  */
1044 void dev_enable(void)
1045 {
1046         struct bus *link;
1047
1048         printk(BIOS_INFO, "Enabling resources...\n");
1049
1050         /* Now enable everything. */
1051         for (link = dev_root.link_list; link; link = link->next)
1052                 enable_resources(link);
1053
1054         printk(BIOS_INFO, "done.\n");
1055 }
1056
1057 /**
1058  * Initialize a specific device.
1059  *
1060  * The parent should be initialized first to avoid having an ordering problem.
1061  * This is done by calling the parent's init() method before its childrens'
1062  * init() methods.
1063  *
1064  * @param dev The device to be initialized.
1065  */
1066 static void init_dev(struct device *dev)
1067 {
1068         if (!dev->enabled)
1069                 return;
1070
1071         if (!dev->initialized && dev->ops && dev->ops->init) {
1072                 if (dev->path.type == DEVICE_PATH_I2C) {
1073                         printk(BIOS_DEBUG, "smbus: %s[%d]->",
1074                                dev_path(dev->bus->dev), dev->bus->link_num);
1075                 }
1076
1077                 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1078                 dev->initialized = 1;
1079                 dev->ops->init(dev);
1080         }
1081 }
1082
1083 static void init_link(struct bus *link)
1084 {
1085         struct device *dev;
1086         struct bus *c_link;
1087
1088         for (dev = link->children; dev; dev = dev->sibling)
1089                 init_dev(dev);
1090
1091         for (dev = link->children; dev; dev = dev->sibling) {
1092                 for (c_link = dev->link_list; c_link; c_link = c_link->next)
1093                         init_link(c_link);
1094         }
1095 }
1096
1097 /**
1098  * Initialize all devices in the global device tree.
1099  *
1100  * Starting at the root device, call the device's init() method to do
1101  * device-specific setup, then call each child's init() method.
1102  */
1103 void dev_initialize(void)
1104 {
1105         struct bus *link;
1106
1107         printk(BIOS_INFO, "Initializing devices...\n");
1108
1109 #if CONFIG_ARCH_X86
1110         /* Ensure EBDA is prepared before Option ROMs. */
1111         setup_default_ebda();
1112 #endif
1113
1114         /* First call the mainboard init. */
1115         init_dev(&dev_root);
1116
1117         /* Now initialize everything. */
1118         for (link = dev_root.link_list; link; link = link->next)
1119                 init_link(link);
1120
1121         printk(BIOS_INFO, "Devices initialized\n");
1122         show_all_devs(BIOS_SPEW, "After init.");
1123 }