d7335c95a15899d7107ea96385f8e927ebe8217e
[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 /* 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 ...
27  *
28  * Lots of cleanups by Eric Biederman to handle bridges, and to
29  * handle resource allocation for non-pci devices.
30  */
31
32 #include <console/console.h>
33 #include <bitops.h>
34 #include <arch/io.h>
35 #include <device/device.h>
36 #include <device/pci.h>
37 #include <device/pci_ids.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <smp/spinlock.h>
41
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;
48
49 DECLARE_SPIN_LOCK(dev_lock)
50
51 /**
52  * Allocate a new device structure.
53  *
54  * Allocte a new device structure and attach it to the device tree as a
55  * child of the parent bus.
56  *
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.
60  *
61  * @see device_path
62  */
63 device_t alloc_dev(struct bus *parent, struct device_path *path)
64 {
65         device_t dev, child;
66
67         spin_lock(&dev_lock);
68
69         /* Find the last child of our parent. */
70         for (child = parent->children; child && child->sibling; /* */ ) {
71                 child = child->sibling;
72         }
73
74         dev = malloc(sizeof(*dev));
75         if (dev == 0)
76                 die("DEV: out of memory.\n");
77
78         memset(dev, 0, sizeof(*dev));
79         memcpy(&dev->path, path, sizeof(*path));
80
81         /* By default devices are enabled. */
82         dev->enabled = 1;
83
84         /* Add the new device to the list of children of the bus. */
85         dev->bus = parent;
86         if (child) {
87                 child->sibling = dev;
88         } else {
89                 parent->children = dev;
90         }
91
92         /* Append a new device to the global device list.
93          * The list is used to find devices once everything is set up.
94          */
95         last_dev->next = dev;
96         last_dev = dev;
97
98         spin_unlock(&dev_lock);
99         return dev;
100 }
101
102 /**
103  * Round a number up to an alignment.
104  *
105  * @param val The starting value.
106  * @param roundup Alignment as a power of two.
107  * @return Rounded up number.
108  */
109 static resource_t round(resource_t val, unsigned long pow)
110 {
111         resource_t mask;
112         mask = (1ULL << pow) - 1ULL;
113         val += mask;
114         val &= ~mask;
115         return val;
116 }
117
118 /**
119  * Read the resources on all devices of a given bus.
120  *
121  * @param bus Bus to read the resources on.
122  */
123 static void read_resources(struct bus *bus)
124 {
125         struct device *curdev;
126
127         printk(BIOS_SPEW, "%s %s bus %x link: %d\n", dev_path(bus->dev),
128                __func__, bus->secondary, bus->link_num);
129
130         /* Walk through all devices and find which resources they need. */
131         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
132                 struct bus *link;
133                 if (!curdev->enabled) {
134                         continue;
135                 }
136                 if (!curdev->ops || !curdev->ops->read_resources) {
137                         printk(BIOS_ERR, "%s missing read_resources\n",
138                                    dev_path(curdev));
139                         continue;
140                 }
141                 curdev->ops->read_resources(curdev);
142
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);
146         }
147         printk(BIOS_SPEW, "%s read_resources bus %d link: %d done\n",
148                     dev_path(bus->dev), bus->secondary, bus->link_num);
149 }
150
151 struct pick_largest_state {
152         struct resource *last;
153         struct device *result_dev;
154         struct resource *result;
155         int seen_last;
156 };
157
158 static void pick_largest_resource(void *gp, struct device *dev,
159                                   struct resource *resource)
160 {
161         struct pick_largest_state *state = gp;
162         struct resource *last;
163
164         last = state->last;
165
166         /* Be certain to pick the successor to last. */
167         if (resource == last) {
168                 state->seen_last = 1;
169                 return;
170         }
171         if (resource->flags & IORESOURCE_FIXED)
172                 return;         // Skip it.
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)))) {
178                 return;
179         }
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;
186         }
187 }
188
189 static struct device *largest_resource(struct bus *bus,
190                                        struct resource **result_res,
191                                        unsigned long type_mask,
192                                        unsigned long type)
193 {
194         struct pick_largest_state state;
195
196         state.last = *result_res;
197         state.result_dev = NULL;
198         state.result = NULL;
199         state.seen_last = 0;
200
201         search_bus_resources(bus, type_mask, type, pick_largest_resource,
202                              &state);
203
204         *result_res = state.result;
205         return state.result_dev;
206 }
207
208 /**
209  * Compute allocate resources is the guts of the resource allocator.
210  *
211  * The problem.
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.
216  *
217  * The strategy.
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.
222  *
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.
227  *
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.
234  *
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.
239  * @return TODO
240  */
241 static void compute_resources(struct bus *bus, struct resource *bridge,
242                        unsigned long type_mask, unsigned long type)
243 {
244         struct device *dev;
245         struct resource *resource;
246         resource_t base;
247         base = round(bridge->base, bridge->align);
248
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) ?
252                "prefmem" : "mem",
253                base, bridge->size, bridge->align, bridge->gran, bridge->limit);
254
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;
258
259                 if (!dev->link_list)
260                         continue;
261
262                 /* Find the resources with matching type flags. */
263                 for (child_bridge = dev->resource_list; child_bridge;
264                      child_bridge = child_bridge->next) {
265                         struct bus* link;
266
267                         if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
268                             (child_bridge->flags & type_mask) != type)
269                                 continue;
270
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.
276                          */
277                         link = dev->link_list;
278                         while (link && link->link_num !=
279                                         IOINDEX_LINK(child_bridge->index))
280                                 link = link->next;
281                         if (link == NULL)
282                                 printk(BIOS_ERR, "link %ld not found on %s\n",
283                                        IOINDEX_LINK(child_bridge->index),
284                                        dev_path(dev));
285                         compute_resources(link, child_bridge,
286                                           type_mask | IORESOURCE_PREFETCH,
287                                           type | (child_bridge->flags &
288                                                   IORESOURCE_PREFETCH));
289                 }
290         }
291
292         /* Remember we haven't found anything yet. */
293         resource = NULL;
294
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.
298          */
299         while ((dev = largest_resource(bus, &resource, type_mask, type))) {
300
301                 /* Size 0 resources can be skipped. */
302                 if (!resource->size) {
303                         continue;
304                 }
305
306                 /* Propagate the resource alignment to the bridge resource. */
307                 if (resource->align > bridge->align) {
308                         bridge->align = resource->align;
309                 }
310
311                 /* Propagate the resource limit to the bridge register. */
312                 if (bridge->limit > resource->limit) {
313                         bridge->limit = resource->limit;
314                 }
315
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);
322                 }
323
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
329                          * I/O space.
330                          */
331                         if ((base & 0x300) != 0) {
332                                 base = (base & ~0x3ff) + 0x400;
333                         }
334                         /* Don't allow allocations in the VGA I/O range.
335                          * PCI has special cases for that.
336                          */
337                         else if ((base >= 0x3b0) && (base <= 0x3df)) {
338                                 base = 0x3e0;
339                         }
340                 }
341                 /* Base must be aligned. */
342                 base = round(base, resource->align);
343                 resource->base = base;
344                 base += resource->size;
345
346                 printk(BIOS_SPEW, "%s %02lx *  [0x%llx - 0x%llx] %s\n",
347                             dev_path(dev), resource->index,
348                             resource->base,
349                             resource->base + resource->size - 1,
350                             (resource->flags & IORESOURCE_IO) ? "io" :
351                             (resource->flags & IORESOURCE_PREFETCH) ?
352                              "prefmem" : "mem");
353         }
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.
359          */
360         bridge->size = round(base, bridge->gran) -
361                        round(bridge->base, bridge->align);
362
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);
368 }
369
370 /**
371  * This function is the second part of the resource allocator.
372  *
373  * The problem.
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.
378  *
379  * The strategy.
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.
384  *
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.
389  *
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.
396  *
397  * - This function assigns the resources a value.
398  *
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.
403  */
404 static void allocate_resources(struct bus *bus, struct resource *bridge,
405                         unsigned long type_mask, unsigned long type)
406 {
407         struct device *dev;
408         struct resource *resource;
409         resource_t base;
410         base = bridge->base;
411
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) ?
415                "prefmem" : "mem",
416                base, bridge->size, bridge->align, bridge->gran, bridge->limit);
417
418         /* Remember we haven't found anything yet. */
419         resource = NULL;
420
421         /* Walk through all the resources on the current bus and allocate them
422          * address space.
423          */
424         while ((dev = largest_resource(bus, &resource, type_mask, type))) {
425
426                 /* Propagate the bridge limit to the resource register. */
427                 if (resource->limit > bridge->limit) {
428                         resource->limit = bridge->limit;
429                 }
430
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;
436                         continue;
437                 }
438
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
444                          * I/O space.
445                          */
446                         if ((base & 0x300) != 0) {
447                                 base = (base & ~0x3ff) + 0x400;
448                         }
449                         /* Don't allow allocations in the VGA I/O range.
450                          * PCI has special cases for that.
451                          */
452                         else if ((base >= 0x3b0) && (base <= 0x3df)) {
453                                 base = 0x3e0;
454                         }
455                 }
456
457                 if ((round(base, resource->align) + resource->size - 1) <=
458                     resource->limit) {
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;
465                 } else {
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,
469                                resource->limit);
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",
474                                (resource->
475                                 flags & IORESOURCE_ASSIGNED) ? "Assigned: " :
476                                "", dev_path(dev), resource->index,
477                                resource->base,
478                                resource->base + resource->size - 1,
479                                (resource->
480                                 flags & IORESOURCE_IO) ? "io" : (resource->
481                                                                  flags &
482                                                                  IORESOURCE_PREFETCH)
483                                ? "prefmem" : "mem");
484                 }
485
486                 printk(BIOS_SPEW, "%s%s %02lx *  [0x%llx - 0x%llx] %s\n",
487                        (resource->flags & IORESOURCE_ASSIGNED) ? "Assigned: "
488                        : "",
489                        dev_path(dev), resource->index, resource->base,
490                        resource->size ? resource->base + resource->size - 1 :
491                        resource->base,
492                        (resource->flags & IORESOURCE_IO) ? "io" :
493                        (resource->flags & IORESOURCE_PREFETCH) ? "prefmem" :
494                        "mem");
495         }
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.
500          */
501
502         bridge->flags |= IORESOURCE_ASSIGNED;
503
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) ?
507                "prefmem" : "mem",
508                base, bridge->size, bridge->align, bridge->gran);
509
510         /* For each child which is a bridge, allocate_resources. */
511         for (dev = bus->children; dev; dev = dev->sibling) {
512                 struct resource *child_bridge;
513
514                 if (!dev->link_list)
515                         continue;
516
517                 /* Find the resources with matching type flags. */
518                 for (child_bridge = dev->resource_list; child_bridge;
519                      child_bridge = child_bridge->next) {
520                         struct bus* link;
521
522                         if (!(child_bridge->flags & IORESOURCE_BRIDGE) ||
523                             (child_bridge->flags & type_mask) != type)
524                                 continue;
525
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.
531                          */
532                         link = dev->link_list;
533                         while (link && link->link_num !=
534                                        IOINDEX_LINK(child_bridge->index))
535                                 link = link->next;
536                         if (link == NULL)
537                                 printk(BIOS_ERR, "link %ld not found on %s\n",
538                                        IOINDEX_LINK(child_bridge->index),
539                                        dev_path(dev));
540                         allocate_resources(link, child_bridge,
541                                            type_mask | IORESOURCE_PREFETCH,
542                                            type | (child_bridge->flags &
543                                                    IORESOURCE_PREFETCH));
544                 }
545         }
546 }
547
548 #if CONFIG_PCI_64BIT_PREF_MEM == 1
549 #define MEM_MASK (IORESOURCE_PREFETCH | IORESOURCE_MEM)
550 #else
551 #define MEM_MASK (IORESOURCE_MEM)
552 #endif
553
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)
558
559 struct constraints {
560         struct resource pref, io, mem;
561 };
562
563 static void constrain_resources(struct device *dev, struct constraints* limits)
564 {
565         struct device *child;
566         struct resource *res;
567         struct resource *lim;
568         struct bus *link;
569
570         printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
571
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))
575                         continue;
576                 if (!res->size) {
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);
580                         continue;
581                 }
582
583                 /* PREFETCH, MEM, or I/O - skip any others. */
584                 if ((res->flags & MEM_MASK) == PREF_TYPE)
585                         lim = &limits->pref;
586                 else if ((res->flags & MEM_MASK) == MEM_TYPE)
587                         lim = &limits->mem;
588                 else if ((res->flags & IO_MASK) == IO_TYPE)
589                         lim = &limits->io;
590                 else
591                         continue;
592
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))
597                         continue;
598
599                 /* Choose to be above or below fixed resources.  This
600                  * check is signed so that "negative" amounts of space
601                  * are handled correctly.
602                  */
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;
606                 else
607                         lim->limit = res->base -1;
608         }
609
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)
614                         if (child->enabled)
615                                 constrain_resources(child, limits);
616 }
617
618 static void avoid_fixed_resources(struct device *dev)
619 {
620         struct constraints limits;
621         struct resource *res;
622
623         printk(BIOS_SPEW, "%s: %s\n", __func__, dev_path(dev));
624         /* Initialize constraints to maximum size. */
625
626         limits.pref.base = 0;
627         limits.pref.limit = 0xffffffffffffffffULL;
628         limits.io.base = 0;
629         limits.io.limit = 0xffffffffffffffffULL;
630         limits.mem.base = 0;
631         limits.mem.limit = 0xffffffffffffffffULL;
632
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))
636                         continue;
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;
648         }
649
650         /* Look through the tree for fixed resources and update the limits. */
651         constrain_resources(dev, &limits);
652
653         /* Update dev's resources with new limits. */
654         for (res = dev->resource_list; res; res = res->next) {
655                 struct resource *lim;
656
657                 if ((res->flags & IORESOURCE_FIXED))
658                         continue;
659
660                 /* PREFETCH, MEM, or I/O - skip any others. */
661                 if ((res->flags & MEM_MASK) == PREF_TYPE)
662                         lim = &limits.pref;
663                 else if ((res->flags & MEM_MASK) == MEM_TYPE)
664                         lim = &limits.mem;
665                 else if ((res->flags & IO_MASK) == IO_TYPE)
666                         lim = &limits.io;
667                 else
668                         continue;
669
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);
674
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;
680         }
681 }
682
683 #if CONFIG_VGA_BRIDGE_SETUP == 1
684 device_t vga_pri = 0;
685 static void set_vga_bridge_bits(void)
686 {
687         /*
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.
691          */
692
693         /* FIXME: Handle the VGA palette snooping. */
694         struct device *dev, *vga, *vga_onboard, *vga_first, *vga_last;
695         struct bus *bus;
696         bus = 0;
697         vga = 0;
698         vga_onboard = 0;
699         vga_first = 0;
700         vga_last = 0;
701         for (dev = all_devices; dev; dev = dev->next) {
702                 if (!dev->enabled)
703                         continue;
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                                 }
712                         } else {
713                                 if (dev->on_mainboard) {
714                                         vga_onboard = dev;
715                                 } else {
716                                         vga_last = dev;
717                                 }
718                         }
719
720                         /* It isn't safe to enable other VGA cards. */
721                         dev->command &= ~(PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
722                 }
723         }
724
725         vga = vga_last;
726
727         if (!vga) {
728                 vga = vga_first;
729         }
730 #if CONFIG_CONSOLE_VGA_ONBOARD_AT_FIRST == 1
731         if (vga_onboard)        // Will use on board VGA as pri.
732 #else
733         if (!vga)               // Will use last add on adapter as pri.
734 #endif
735         {
736                 vga = vga_onboard;
737         }
738
739         if (vga) {
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);
744                 vga_pri = vga;
745                 bus = vga->bus;
746         }
747         /* Now walk up the bridges setting the VGA enable. */
748         while (bus) {
749                 printk(BIOS_DEBUG, "Setting PCI_BRIDGE_CTL_VGA for bridge %s\n",
750                              dev_path(bus->dev));
751                 bus->bridge_ctrl |= PCI_BRIDGE_CTL_VGA;
752                 bus = (bus == bus->dev->bus) ? 0 : bus->dev->bus;
753         }
754 }
755
756 #endif
757
758 /**
759  * Assign the computed resources to the devices on the bus.
760  *
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.
764  *
765  * Mutual recursion:
766  *      assign_resources() -> device_operation::set_resources()
767  *      device_operation::set_resources() -> assign_resources()
768  *
769  * @param bus Pointer to the structure for this bus.
770  */
771 void assign_resources(struct bus *bus)
772 {
773         struct device *curdev;
774
775         printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
776                     dev_path(bus->dev), bus->secondary, bus->link_num);
777
778         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
779                 if (!curdev->enabled || !curdev->resource_list) {
780                         continue;
781                 }
782                 if (!curdev->ops || !curdev->ops->set_resources) {
783                         printk(BIOS_ERR, "%s missing set_resources\n",
784                                    dev_path(curdev));
785                         continue;
786                 }
787                 curdev->ops->set_resources(curdev);
788         }
789         printk(BIOS_SPEW, "%s assign_resources, bus %d link: %d\n",
790                     dev_path(bus->dev), bus->secondary, bus->link_num);
791 }
792
793 /**
794  * Enable the resources for devices on a link.
795  *
796  * Enable resources of the device by calling the device specific
797  * enable_resources() method.
798  *
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.
802  *
803  * @param link The link whose devices' resources are to be enabled.
804  */
805 static void enable_resources(struct bus *link)
806 {
807         struct device *dev;
808         struct bus *c_link;
809
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);
813                 }
814         }
815
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);
819                 }
820         }
821 }
822
823 /**
824  * Reset all of the devices on a bus and clear the bus's reset_needed flag.
825  *
826  * @param bus Pointer to the bus structure.
827  * @return 1 if the bus was successfully reset, 0 otherwise.
828  */
829 int reset_bus(struct bus *bus)
830 {
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;
834                 return 1;
835         }
836         return 0;
837 }
838
839 /**
840  * Scan for devices on a bus.
841  *
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.
845  *
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.
849  */
850 unsigned int scan_bus(struct device *busdev, unsigned int max)
851 {
852         unsigned int new_max;
853         int do_scan_bus;
854         if (!busdev || !busdev->enabled || !busdev->ops ||
855             !busdev->ops->scan_bus) {
856                 return max;
857         }
858
859         do_scan_bus = 1;
860         while (do_scan_bus) {
861                 struct bus *link;
862                 new_max = busdev->ops->scan_bus(busdev, max);
863                 do_scan_bus = 0;
864                 for (link = busdev->link_list; link; link = link->next) {
865                         if (link->reset_needed) {
866                                 if (reset_bus(link)) {
867                                         do_scan_bus = 1;
868                                 } else {
869                                         busdev->bus->reset_needed = 1;
870                                 }
871                         }
872                 }
873         }
874         return new_max;
875 }
876
877 /**
878  * Determine the existence of devices and extend the device tree.
879  *
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.
885  *
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
888  * device tree.
889  *
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
892  * the probe.
893  *
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.
898  */
899 void dev_enumerate(void)
900 {
901         struct device *root;
902         printk(BIOS_INFO, "Enumerating buses...\n");
903         root = &dev_root;
904
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);
908
909         if (root->chip_ops && root->chip_ops->enable_dev) {
910                 root->chip_ops->enable_dev(root);
911         }
912         if (!root->ops || !root->ops->scan_bus) {
913                 printk(BIOS_ERR, "dev_root missing scan_bus operation");
914                 return;
915         }
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         /* 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.
950          */
951
952         /* Read the resources for the entire tree. */
953
954         printk(BIOS_INFO, "Reading resources...\n");
955         read_resources(root->link_list);
956         printk(BIOS_INFO, "Done reading resources.\n");
957
958         print_resource_tree(root, BIOS_SPEW, "After reading.");
959
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))
963                         continue;
964                 for (res = child->resource_list; res; res = res->next) {
965                         if (res->flags & IORESOURCE_FIXED)
966                                 continue;
967                         if (res->flags & IORESOURCE_PREFETCH) {
968                                 compute_resources(child->link_list,
969                                                res, MEM_MASK, PREF_TYPE);
970                                 continue;
971                         }
972                         if (res->flags & IORESOURCE_MEM) {
973                                 compute_resources(child->link_list,
974                                                res, MEM_MASK, MEM_TYPE);
975                                 continue;
976                         }
977                         if (res->flags & IORESOURCE_IO) {
978                                 compute_resources(child->link_list,
979                                                res, IO_MASK, IO_TYPE);
980                                 continue;
981                         }
982                 }
983         }
984
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);
989
990         /* Now we need to adjust the resources. MEM resources need to start at
991          * the highest address managable.
992          */
993         for (child = root->link_list->children; child; child = child->sibling) {
994                 if (child->path.type != DEVICE_PATH_PCI_DOMAIN)
995                         continue;
996                 for (res = child->resource_list; res; res = res->next) {
997                         if (!(res->flags & IORESOURCE_MEM) ||
998                             res->flags & IORESOURCE_FIXED)
999                                 continue;
1000                         res->base = resource_max(res);
1001                 }
1002         }
1003
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))
1008                         continue;
1009                 for (res = child->resource_list; res; res = res->next) {
1010                         if (res->flags & IORESOURCE_FIXED)
1011                                 continue;
1012                         if (res->flags & IORESOURCE_PREFETCH) {
1013                                 allocate_resources(child->link_list,
1014                                                res, MEM_MASK, PREF_TYPE);
1015                                 continue;
1016                         }
1017                         if (res->flags & IORESOURCE_MEM) {
1018                                 allocate_resources(child->link_list,
1019                                                res, MEM_MASK, MEM_TYPE);
1020                                 continue;
1021                         }
1022                         if (res->flags & IORESOURCE_IO) {
1023                                 allocate_resources(child->link_list,
1024                                                res, IO_MASK, IO_TYPE);
1025                                 continue;
1026                         }
1027                 }
1028         }
1029         assign_resources(root->link_list);
1030         printk(BIOS_INFO, "Done setting resources.\n");
1031         print_resource_tree(root, BIOS_SPEW, "After assigning values.");
1032
1033         printk(BIOS_INFO, "Done allocating resources.\n");
1034 }
1035
1036 /**
1037  * Enable devices on the device tree.
1038  *
1039  * Starting at the root, walk the tree and enable all devices/bridges by
1040  * calling the device's enable_resources() method.
1041  */
1042 void dev_enable(void)
1043 {
1044         struct bus *link;
1045
1046         printk(BIOS_INFO, "Enabling resources...\n");
1047
1048         /* now enable everything. */
1049         for (link = dev_root.link_list; link; link = link->next)
1050                 enable_resources(link);
1051
1052         printk(BIOS_INFO, "done.\n");
1053 }
1054
1055 /**
1056  * Initialize a specific device.
1057  *
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.
1061  *
1062  * @param dev The device to be initialized.
1063  */
1064 static void init_dev(struct device *dev)
1065 {
1066         if (!dev->enabled) {
1067                 return;
1068         }
1069
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);
1074                 }
1075
1076                 printk(BIOS_DEBUG, "%s init\n", dev_path(dev));
1077                 dev->initialized = 1;
1078                 dev->ops->init(dev);
1079         }
1080 }
1081
1082 static void init_link(struct bus *link)
1083 {
1084         struct device *dev;
1085         struct bus *c_link;
1086
1087         for (dev = link->children; dev; dev = dev->sibling) {
1088                 init_dev(dev);
1089         }
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 /**
1099  * Initialize all devices in the global device tree.
1100  *
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.
1103  */
1104 void dev_initialize(void)
1105 {
1106         struct bus *link;
1107
1108         printk(BIOS_INFO, "Initializing devices...\n");
1109
1110         /* First call the mainboard init. */
1111         init_dev(&dev_root);
1112
1113         /* now initialize everything. */
1114         for (link = dev_root.link_list; link; link = link->next)
1115                 init_link(link);
1116
1117         printk(BIOS_INFO, "Devices initialized\n");
1118         show_all_devs(BIOS_SPEW, "After init.");
1119 }