526490a8c199ab132dcce9bb9adc382e89042cbb
[coreboot.git] / src / devices / device_util.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2003-2004 Linux Networx
5  * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6  * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7  * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8  * Copyright (C) 2005-2006 Tyan
9  * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 2 of the License.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
23  */
24
25 #include <console/console.h>
26 #include <device/device.h>
27 #include <device/path.h>
28 #include <device/pci.h>
29 #include <device/resource.h>
30 #include <string.h>
31
32 /**
33  * See if a device structure exists for path.
34  *
35  * @param parent The bus to find the device on.
36  * @param path The relative path from the bus to the appropriate device.
37  * @return Pointer to a device structure for the device on bus at path
38  *         or 0/NULL if no device is found.
39  */
40 device_t find_dev_path(struct bus *parent, struct device_path *path)
41 {
42         device_t child;
43         for (child = parent->children; child; child = child->sibling) {
44                 if (path_eq(path, &child->path))
45                         break;
46         }
47         return child;
48 }
49
50 /**
51  * See if a device structure already exists and if not allocate it.
52  *
53  * @param parent The bus to find the device on.
54  * @param path The relative path from the bus to the appropriate device.
55  * @return Pointer to a device structure for the device on bus at path.
56  */
57 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
58 {
59         device_t child;
60         child = find_dev_path(parent, path);
61         if (!child)
62                 child = alloc_dev(parent, path);
63         return child;
64 }
65
66 /**
67  * Given a PCI bus and a devfn number, find the device structure.
68  *
69  * @param bus The bus number.
70  * @param devfn A device/function number.
71  * @return Pointer to the device structure (if found), 0 otherwise.
72  */
73 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
74 {
75         struct device *dev, *result;
76
77         result = 0;
78         for (dev = all_devices; dev; dev = dev->next) {
79                 if ((dev->path.type == DEVICE_PATH_PCI) &&
80                     (dev->bus->secondary == bus) &&
81                     (dev->path.pci.devfn == devfn)) {
82                         result = dev;
83                         break;
84                 }
85         }
86         return result;
87 }
88
89 /**
90  * Given an SMBus bus and a device number, find the device structure.
91  *
92  * @param bus The bus number.
93  * @param addr A device number.
94  * @return Pointer to the device structure (if found), 0 otherwise.
95  */
96 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
97 {
98         struct device *dev, *result;
99
100         result = 0;
101         for (dev = all_devices; dev; dev = dev->next) {
102                 if ((dev->path.type == DEVICE_PATH_I2C) &&
103                     (dev->bus->secondary == bus) &&
104                     (dev->path.i2c.device == addr)) {
105                         result = dev;
106                         break;
107                 }
108         }
109         return result;
110 }
111
112 /**
113  * Given a Local APIC ID, find the device structure.
114  *
115  * @param apic_id The Local APIC ID number.
116  * @return Pointer to the device structure (if found), 0 otherwise.
117  */
118 device_t dev_find_lapic(unsigned apic_id)
119 {
120         device_t dev, result = NULL;
121
122         for (dev = all_devices; dev; dev = dev->next) {
123                 if (dev->path.type == DEVICE_PATH_APIC &&
124                     dev->path.apic.apic_id == apic_id) {
125                         result = dev;
126                         break;
127                 }
128         }
129         return result;
130 }
131
132 /**
133  * Find a device of a given vendor and type.
134  *
135  * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
136  * @param device A PCI device ID.
137  * @param from Pointer to the device structure, used as a starting point in
138  *             the linked list of all_devices, which can be 0 to start at the
139  *             head of the list (i.e. all_devices).
140  * @return Pointer to the device struct.
141  */
142 struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
143 {
144         if (!from)
145                 from = all_devices;
146         else
147                 from = from->next;
148
149         while (from && (from->vendor != vendor || from->device != device))
150                 from = from->next;
151
152         return from;
153 }
154
155 /**
156  * Find a device of a given class.
157  *
158  * @param class Class of the device.
159  * @param from Pointer to the device structure, used as a starting point in
160  *             the linked list of all_devices, which can be 0 to start at the
161  *             head of the list (i.e. all_devices).
162  * @return Pointer to the device struct.
163  */
164 struct device *dev_find_class(unsigned int class, struct device *from)
165 {
166         if (!from)
167                 from = all_devices;
168         else
169                 from = from->next;
170
171         while (from && (from->class & 0xffffff00) != class)
172                 from = from->next;
173
174         return from;
175 }
176
177 /*
178  * Warning: This function uses a static buffer. Don't call it more than once
179  * from the same print statement!
180  */
181 const char *dev_path(device_t dev)
182 {
183         static char buffer[DEVICE_PATH_MAX];
184
185         buffer[0] = '\0';
186         if (!dev) {
187                 memcpy(buffer, "<null>", 7);
188         } else {
189                 switch(dev->path.type) {
190                 case DEVICE_PATH_ROOT:
191                         memcpy(buffer, "Root Device", 12);
192                         break;
193                 case DEVICE_PATH_PCI:
194 #if CONFIG_PCI_BUS_SEGN_BITS
195                         sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
196                                 dev->bus->secondary >> 8,
197                                 dev->bus->secondary & 0xff,
198                                 PCI_SLOT(dev->path.pci.devfn),
199                                 PCI_FUNC(dev->path.pci.devfn));
200 #else
201                         sprintf(buffer, "PCI: %02x:%02x.%01x",
202                                 dev->bus->secondary,
203                                 PCI_SLOT(dev->path.pci.devfn),
204                                 PCI_FUNC(dev->path.pci.devfn));
205 #endif
206                         break;
207                 case DEVICE_PATH_PNP:
208                         sprintf(buffer, "PNP: %04x.%01x",
209                                 dev->path.pnp.port, dev->path.pnp.device);
210                         break;
211                 case DEVICE_PATH_I2C:
212                         sprintf(buffer, "I2C: %02x:%02x",
213                                 dev->bus->secondary,
214                                 dev->path.i2c.device);
215                         break;
216                 case DEVICE_PATH_APIC:
217                         sprintf(buffer, "APIC: %02x",
218                                 dev->path.apic.apic_id);
219                         break;
220                 case DEVICE_PATH_PCI_DOMAIN:
221                         sprintf(buffer, "PCI_DOMAIN: %04x",
222                                 dev->path.pci_domain.domain);
223                         break;
224                 case DEVICE_PATH_APIC_CLUSTER:
225                         sprintf(buffer, "APIC_CLUSTER: %01x",
226                                 dev->path.apic_cluster.cluster);
227                         break;
228                 case DEVICE_PATH_CPU:
229                         sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
230                         break;
231                 case DEVICE_PATH_CPU_BUS:
232                         sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
233                         break;
234                 default:
235                         printk(BIOS_ERR, "Unknown device path type: %d\n",
236                                dev->path.type);
237                         break;
238                 }
239         }
240         return buffer;
241 }
242
243 const char *bus_path(struct bus *bus)
244 {
245         static char buffer[BUS_PATH_MAX];
246         sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
247         return buffer;
248 }
249
250 int path_eq(struct device_path *path1, struct device_path *path2)
251 {
252         int equal = 0;
253
254         if (path1->type != path2->type)
255                 return 0;
256
257         switch (path1->type) {
258         case DEVICE_PATH_NONE:
259                 break;
260         case DEVICE_PATH_ROOT:
261                 equal = 1;
262                 break;
263         case DEVICE_PATH_PCI:
264                 equal = (path1->pci.devfn == path2->pci.devfn);
265                 break;
266         case DEVICE_PATH_PNP:
267                 equal = (path1->pnp.port == path2->pnp.port) &&
268                         (path1->pnp.device == path2->pnp.device);
269                 break;
270         case DEVICE_PATH_I2C:
271                 equal = (path1->i2c.device == path2->i2c.device);
272                 break;
273         case DEVICE_PATH_APIC:
274                 equal = (path1->apic.apic_id == path2->apic.apic_id);
275                 break;
276         case DEVICE_PATH_PCI_DOMAIN:
277                 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
278                 break;
279         case DEVICE_PATH_APIC_CLUSTER:
280                 equal = (path1->apic_cluster.cluster
281                          == path2->apic_cluster.cluster);
282                 break;
283         case DEVICE_PATH_CPU:
284                 equal = (path1->cpu.id == path2->cpu.id);
285                 break;
286         case DEVICE_PATH_CPU_BUS:
287                 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
288                 break;
289         default:
290                 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
291                 break;
292         }
293
294         return equal;
295 }
296
297 /**
298  * Allocate 64 more resources to the free list.
299  *
300  * @return TODO.
301  */
302 static int allocate_more_resources(void)
303 {
304         int i;
305         struct resource *new_res_list;
306
307         new_res_list = malloc(64 * sizeof(*new_res_list));
308
309         if (new_res_list == NULL)
310                 return 0;
311
312         memset(new_res_list, 0, 64 * sizeof(*new_res_list));
313
314         for (i = 0; i < 64 - 1; i++)
315                 new_res_list[i].next = &new_res_list[i+1];
316
317         free_resources = new_res_list;
318         return 1;
319 }
320
321 /**
322  * Remove resource res from the device's list and add it to the free list.
323  *
324  * @param dev TODO
325  * @param res TODO
326  * @param prev TODO
327  * @return TODO.
328  */
329 static void free_resource(device_t dev, struct resource *res,
330                           struct resource *prev)
331 {
332         if (prev)
333                 prev->next = res->next;
334         else
335                 dev->resource_list = res->next;
336
337         res->next = free_resources;
338         free_resources = res;
339 }
340
341 /**
342  * See if we have unused but allocated resource structures.
343  *
344  * If so remove the allocation.
345  *
346  * @param dev The device to find the resource on.
347  */
348 void compact_resources(device_t dev)
349 {
350         struct resource *res, *next, *prev = NULL;
351
352         /* Move all of the free resources to the end */
353         for (res = dev->resource_list; res; res = next) {
354                 next = res->next;
355                 if (!res->flags)
356                         free_resource(dev, res, prev);
357                 else
358                         prev = res;
359         }
360 }
361
362 /**
363  * See if a resource structure already exists for a given index.
364  *
365  * @param dev The device to find the resource on.
366  * @param index The index of the resource on the device.
367  * @return The resource, if it already exists.
368  */
369 struct resource *probe_resource(device_t dev, unsigned index)
370 {
371         struct resource *res;
372
373         /* See if there is a resource with the appropriate index */
374         for (res = dev->resource_list; res; res = res->next) {
375                 if (res->index == index)
376                         break;
377         }
378
379         return res;
380 }
381
382 /**
383  * See if a resource structure already exists for a given index and if not
384  * allocate one.
385  *
386  * Then initialize the initialize the resource to default values.
387  *
388  * @param dev The device to find the resource on.
389  * @param index The index of the resource on the device.
390  * @return TODO.
391  */
392 struct resource *new_resource(device_t dev, unsigned index)
393 {
394         struct resource *resource, *tail;
395
396         /* First move all of the free resources to the end. */
397         compact_resources(dev);
398
399         /* See if there is a resource with the appropriate index. */
400         resource = probe_resource(dev, index);
401         if (!resource) {
402                 if (free_resources == NULL && !allocate_more_resources())
403                         die("Couldn't allocate more resources.");
404
405                 resource = free_resources;
406                 free_resources = free_resources->next;
407                 memset(resource, 0, sizeof(*resource));
408                 resource->next = NULL;
409                 tail = dev->resource_list;
410                 if (tail) {
411                         while (tail->next) tail = tail->next;
412                         tail->next = resource;
413                 } else {
414                         dev->resource_list = resource;
415                 }
416         }
417
418         /* Initialize the resource values. */
419         if (!(resource->flags & IORESOURCE_FIXED)) {
420                 resource->flags = 0;
421                 resource->base = 0;
422         }
423         resource->size  = 0;
424         resource->limit = 0;
425         resource->index = index;
426         resource->align = 0;
427         resource->gran  = 0;
428
429         return resource;
430 }
431
432 /**
433  * Return an existing resource structure for a given index.
434  *
435  * @param dev The device to find the resource on.
436  * @param index The index of the resource on the device.
437  * return TODO.
438  */
439 struct resource *find_resource(device_t dev, unsigned index)
440 {
441         struct resource *resource;
442
443         /* See if there is a resource with the appropriate index. */
444         resource = probe_resource(dev, index);
445         if (!resource) {
446                 printk(BIOS_EMERG, "%s missing resource: %02x\n",
447                        dev_path(dev), index);
448                 die("");
449         }
450         return resource;
451 }
452
453 /**
454  * Round a number up to the next multiple of gran.
455  *
456  * @param val The starting value.
457  * @param gran Granularity we are aligning the number to.
458  * @return The aligned value.
459  */
460 static resource_t align_up(resource_t val, unsigned long gran)
461 {
462         resource_t mask;
463         mask = (1ULL << gran) - 1ULL;
464         val += mask;
465         val &= ~mask;
466         return val;
467 }
468
469 /**
470  * Round a number up to the previous multiple of gran.
471  *
472  * @param val The starting value.
473  * @param gran Granularity we are aligning the number to.
474  * @return The aligned value.
475  */
476 static resource_t align_down(resource_t val, unsigned long gran)
477 {
478         resource_t mask;
479         mask = (1ULL << gran) - 1ULL;
480         val &= ~mask;
481         return val;
482 }
483
484 /**
485  * Compute the maximum address that is part of a resource.
486  *
487  * @param resource The resource whose limit is desired.
488  * @return The end.
489  */
490 resource_t resource_end(struct resource *resource)
491 {
492         resource_t base, end;
493
494         /* Get the base address. */
495         base = resource->base;
496
497         /*
498          * For a non bridge resource granularity and alignment are the same.
499          * For a bridge resource align is the largest needed alignment below
500          * the bridge. While the granularity is simply how many low bits of
501          * the address cannot be set.
502          */
503
504         /* Get the end (rounded up). */
505         end = base + align_up(resource->size, resource->gran) - 1;
506
507         return end;
508 }
509
510 /**
511  * Compute the maximum legal value for resource->base.
512  *
513  * @param resource The resource whose maximum is desired.
514  * @return The maximum.
515  */
516 resource_t resource_max(struct resource *resource)
517 {
518         resource_t max;
519
520         max = align_down(resource->limit - resource->size + 1, resource->align);
521
522         return max;
523 }
524
525 /**
526  * Return the resource type of a resource.
527  *
528  * @param resource The resource type to decode.
529  * @return TODO.
530  */
531 const char *resource_type(struct resource *resource)
532 {
533         static char buffer[RESOURCE_TYPE_MAX];
534         sprintf(buffer, "%s%s%s%s",
535                 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
536                 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
537                 ((resource->flags == 0) ? "unused" :
538                 (resource->flags & IORESOURCE_IO) ? "io" :
539                 (resource->flags & IORESOURCE_DRQ) ? "drq" :
540                 (resource->flags & IORESOURCE_IRQ) ? "irq" :
541                 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
542                 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
543         return buffer;
544 }
545
546 /**
547  * Print the resource that was just stored.
548  *
549  * @param dev The device the stored resorce lives on.
550  * @param resource The resource that was just stored.
551  * @param comment TODO
552  */
553 void report_resource_stored(device_t dev, struct resource *resource,
554                             const char *comment)
555 {
556         char buf[10];
557         unsigned long long base, end;
558
559         if (!(resource->flags & IORESOURCE_STORED))
560                 return;
561
562         base = resource->base;
563         end = resource_end(resource);
564         buf[0] = '\0';
565
566         if (resource->flags & IORESOURCE_PCI_BRIDGE) {
567 #if CONFIG_PCI_BUS_SEGN_BITS
568                 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
569                         dev->link_list->secondary & 0xff);
570 #else
571                 sprintf(buf, "bus %02x ", dev->link_list->secondary);
572 #endif
573         }
574         printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
575                "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
576                 base, end, resource->size, resource->gran, buf,
577                 resource_type(resource), comment);
578 }
579
580 void search_bus_resources(struct bus *bus, unsigned long type_mask,
581                           unsigned long type, resource_search_t search,
582                           void *gp)
583 {
584         struct device *curdev;
585
586         for (curdev = bus->children; curdev; curdev = curdev->sibling) {
587                 struct resource *res;
588
589                 /* Ignore disabled devices. */
590                 if (!curdev->enabled)
591                         continue;
592
593                 for (res = curdev->resource_list; res; res = res->next) {
594                         /* If it isn't the right kind of resource ignore it. */
595                         if ((res->flags & type_mask) != type)
596                                 continue;
597
598                         /* If it is a subtractive resource recurse. */
599                         if (res->flags & IORESOURCE_SUBTRACTIVE) {
600                                 struct bus * subbus;
601                                 for (subbus = curdev->link_list; subbus;
602                                      subbus = subbus->next)
603                                         if (subbus->link_num
604                                         == IOINDEX_SUBTRACTIVE_LINK(res->index))
605                                                 break;
606                                 if (!subbus) /* Why can subbus be NULL?  */
607                                         break;
608                                 search_bus_resources(subbus, type_mask, type,
609                                                      search, gp);
610                                 continue;
611                         }
612                         search(gp, curdev, res);
613                 }
614         }
615 }
616
617 void search_global_resources(unsigned long type_mask, unsigned long type,
618                              resource_search_t search, void *gp)
619 {
620         struct device *curdev;
621
622         for (curdev = all_devices; curdev; curdev = curdev->next) {
623                 struct resource *res;
624
625                 /* Ignore disabled devices. */
626                 if (!curdev->enabled)
627                         continue;
628
629                 for (res = curdev->resource_list; res; res = res->next) {
630                         /* If it isn't the right kind of resource ignore it. */
631                         if ((res->flags & type_mask) != type)
632                                 continue;
633
634                         /* If it is a subtractive resource ignore it. */
635                         if (res->flags & IORESOURCE_SUBTRACTIVE)
636                                 continue;
637
638                         search(gp, curdev, res);
639                 }
640         }
641 }
642
643 void dev_set_enabled(device_t dev, int enable)
644 {
645         if (dev->enabled == enable)
646                 return;
647
648         dev->enabled = enable;
649         if (dev->ops && dev->ops->enable) {
650                 dev->ops->enable(dev);
651         } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
652                 dev->chip_ops->enable_dev(dev);
653         }
654 }
655
656 void disable_children(struct bus *bus)
657 {
658         device_t child;
659
660         for (child = bus->children; child; child = child->sibling) {
661                 struct bus *link;
662                 for (link = child->link_list; link; link = link->next)
663                         disable_children(link);
664                 dev_set_enabled(child, 0);
665         }
666 }
667
668 static void resource_tree(struct device *root, int debug_level, int depth)
669 {
670         int i = 0;
671         struct device *child;
672         struct bus *link;
673         struct resource *res;
674         char indent[30];        /* If your tree has more levels, it's wrong. */
675
676         for (i = 0; i < depth + 1 && i < 29; i++)
677                 indent[i] = ' ';
678         indent[i] = '\0';
679
680         do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
681         if (root->link_list && root->link_list->children)
682                 do_printk(BIOS_DEBUG, " child on link 0 %s",
683                           dev_path(root->link_list->children));
684         do_printk(BIOS_DEBUG, "\n");
685
686         for (res = root->resource_list; res; res = res->next) {
687                 do_printk(debug_level, "%s%s resource base %llx size %llx "
688                           "align %d gran %d limit %llx flags %lx index %lx\n",
689                           indent, dev_path(root), res->base, res->size,
690                           res->align, res->gran, res->limit, res->flags,
691                           res->index);
692         }
693
694         for (link = root->link_list; link; link = link->next) {
695                 for (child = link->children; child; child = child->sibling)
696                         resource_tree(child, debug_level, depth + 1);
697         }
698 }
699
700 void print_resource_tree(struct device *root, int debug_level, const char *msg)
701 {
702         /* Bail if root is null. */
703         if (!root) {
704                 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
705                 return;
706         }
707
708         /* Bail if not printing to screen. */
709         if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
710                        dev_path(root), msg))
711                 return;
712
713         resource_tree(root, debug_level, 0);
714 }
715
716 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
717 {
718         char depth_str[20] = "";
719         int i;
720         struct device *sibling;
721         struct bus *link;
722
723         for (i = 0; i < depth; i++)
724                 depth_str[i] = ' ';
725         depth_str[i] = '\0';
726
727         do_printk(debug_level, "%s%s: enabled %d\n",
728                   depth_str, dev_path(dev), dev->enabled);
729
730         for (link = dev->link_list; link; link = link->next) {
731                 for (sibling = link->children; sibling;
732                      sibling = sibling->sibling)
733                         show_devs_tree(sibling, debug_level, depth + 1, i);
734         }
735 }
736
737 void show_all_devs_tree(int debug_level, const char *msg)
738 {
739         /* Bail if not printing to screen. */
740         if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
741                 return;
742         show_devs_tree(all_devices, debug_level, 0, -1);
743 }
744
745 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
746 {
747         /* Bail if not printing to screen. */
748         if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
749                        dev_path(root), msg))
750                 return;
751         do_printk(debug_level, "%s\n", msg);
752         show_devs_tree(root, debug_level, 0, -1);
753 }
754
755 void show_all_devs(int debug_level, const char *msg)
756 {
757         struct device *dev;
758
759         /* Bail if not printing to screen. */
760         if (!do_printk(debug_level, "Show all devs...%s\n", msg))
761                 return;
762         for (dev = all_devices; dev; dev = dev->next) {
763                 do_printk(debug_level, "%s: enabled %d\n",
764                           dev_path(dev), dev->enabled);
765         }
766 }
767
768 void show_one_resource(int debug_level, struct device *dev,
769                        struct resource *resource, const char *comment)
770 {
771         char buf[10];
772         unsigned long long base, end;
773         base = resource->base;
774         end = resource_end(resource);
775         buf[0] = '\0';
776
777 /*
778         if (resource->flags & IORESOURCE_BRIDGE) {
779 #if CONFIG_PCI_BUS_SEGN_BITS
780                 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
781                         dev->link[0].secondary & 0xff);
782 #else
783                 sprintf(buf, "bus %02x ", dev->link[0].secondary);
784 #endif
785         }
786 */
787
788         do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
789                   "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
790                   resource->index, base, end, resource->size, resource->gran,
791                   buf, resource_type(resource), comment);
792 }
793
794 void show_all_devs_resources(int debug_level, const char* msg)
795 {
796         struct device *dev;
797
798         if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
799                 return;
800
801         for (dev = all_devices; dev; dev = dev->next) {
802                 struct resource *res;
803                 do_printk(debug_level, "%s: enabled %d\n",
804                           dev_path(dev), dev->enabled);
805                 for (res = dev->resource_list; res; res = res->next)
806                         show_one_resource(debug_level, dev, res, "");
807         }
808 }
809
810 void ram_resource(device_t dev, unsigned long index,
811                   unsigned long basek, unsigned long sizek)
812 {
813         struct resource *resource;
814
815         if (!sizek)
816                 return;
817
818         resource = new_resource(dev, index);
819         resource->base = ((resource_t)basek) << 10;
820         resource->size = ((resource_t)sizek) << 10;
821         resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
822                 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
823 }
824
825 void tolm_test(void *gp, struct device *dev, struct resource *new)
826 {
827         struct resource **best_p = gp;
828         struct resource *best;
829
830         best = *best_p;
831
832         if (!best || (best->base > new->base))
833                 best = new;
834
835         *best_p = best;
836 }
837
838 u32 find_pci_tolm(struct bus *bus)
839 {
840         struct resource *min = NULL;
841         u32 tolm;
842
843         search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
844                              tolm_test, &min);
845
846         tolm = 0xffffffffUL;
847
848         if (min && tolm > min->base)
849                 tolm = min->base;
850
851         return tolm;
852 }