This patch unifies the use of config options in v2 to all start with CONFIG_
[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  * @brief See if a device structure exists for path
34  *
35  * @param bus 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         }
48         return child;
49 }
50
51 /**
52  * @brief See if a device structure already exists and if not allocate it
53  *
54  * @param bus The bus to find the device on
55  * @param path The relative path from the bus to the appropriate device
56  * @return pointer to a device structure for the device on bus at path
57  */
58 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
59 {
60         device_t child;
61         child = find_dev_path(parent, path);
62         if (!child) {
63                 child = alloc_dev(parent, path);
64         }
65         return child;
66 }
67
68 /**
69  * @brief Given a PCI bus and a devfn number, find the device structure
70  *
71  * @param bus The bus number
72  * @param devfn a device/function number
73  * @return pointer to the device structure
74  */
75 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
76 {
77         struct device *dev, *result;
78
79         result = 0;
80         for (dev = all_devices; dev; dev = dev->next) {
81                 if ((dev->path.type == DEVICE_PATH_PCI) &&
82                         (dev->bus->secondary == bus) && 
83                         (dev->path.pci.devfn == devfn)) {
84                         result = dev;
85                         break;
86                 }
87         }
88         return result;
89 }
90
91 /**
92  * @brief Given a smbus bus and a device number, find the device structure
93  *
94  * @param bus The bus number
95  * @param addr a device number 
96  * @return pointer to the device structure
97  */
98 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
99 {
100         struct device *dev, *result;
101         
102         result = 0;
103         for (dev = all_devices; dev; dev = dev->next) {
104                 if ((dev->path.type == DEVICE_PATH_I2C) &&
105                         (dev->bus->secondary == bus) && 
106                         (dev->path.i2c.device == addr)) {
107                         result = dev;
108                         break; 
109                 }       
110         }       
111         return result;
112 }    
113
114 /** Find a device of a given vendor and type
115  * @param vendor Vendor ID (e.g. 0x8086 for Intel)
116  * @param device Device ID
117  * @param from Pointer to the device structure, used as a starting point
118  *        in the linked list of all_devices, which can be 0 to start at the 
119  *        head of the list (i.e. all_devices)
120  * @return Pointer to the device struct 
121  */
122 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
123 {
124         if (!from)
125                 from = all_devices;
126         else
127                 from = from->next;
128         while (from && (from->vendor != vendor || from->device != device)) {
129                 from = from->next;
130         }
131         return from;
132 }
133
134 /** Find a device of a given class
135  * @param class Class of the device
136  * @param from Pointer to the device structure, used as a starting point
137  *        in the linked list of all_devices, which can be 0 to start at the 
138  *        head of the list (i.e. all_devices)
139  * @return Pointer to the device struct 
140  */
141 struct device *dev_find_class(unsigned int class, struct device *from)
142 {
143         if (!from)
144                 from = all_devices;
145         else
146                 from = from->next;
147         while (from && (from->class & 0xffffff00) != class)
148                 from = from->next;
149         return from;
150 }
151
152
153 const char *dev_path(device_t dev)
154 {
155         static char buffer[DEVICE_PATH_MAX];
156         buffer[0] = '\0';
157         if (!dev) {
158                 memcpy(buffer, "<null>", 7);
159         }
160         else {
161                 switch(dev->path.type) {
162                 case DEVICE_PATH_ROOT:
163                         memcpy(buffer, "Root Device", 12);
164                         break;
165                 case DEVICE_PATH_PCI:
166 #if CONFIG_PCI_BUS_SEGN_BITS
167                         sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
168                                 dev->bus->secondary>>8, dev->bus->secondary & 0xff, 
169                                 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
170 #else
171                         sprintf(buffer, "PCI: %02x:%02x.%01x",
172                                 dev->bus->secondary, 
173                                 PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
174 #endif
175                         break;
176                 case DEVICE_PATH_PNP:
177                         sprintf(buffer, "PNP: %04x.%01x",
178                                 dev->path.pnp.port, dev->path.pnp.device);
179                         break;
180                 case DEVICE_PATH_I2C:
181                         sprintf(buffer, "I2C: %02x:%02x",
182                                 dev->bus->secondary,
183                                 dev->path.i2c.device);
184                         break;
185                 case DEVICE_PATH_APIC:
186                         sprintf(buffer, "APIC: %02x",
187                                 dev->path.apic.apic_id);
188                         break;
189                 case DEVICE_PATH_PCI_DOMAIN:
190                         sprintf(buffer, "PCI_DOMAIN: %04x",
191                                 dev->path.pci_domain.domain);
192                         break;
193                 case DEVICE_PATH_APIC_CLUSTER:
194                         sprintf(buffer, "APIC_CLUSTER: %01x",
195                                 dev->path.apic_cluster.cluster);
196                         break;
197                 case DEVICE_PATH_CPU:
198                         sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
199                         break;
200                 case DEVICE_PATH_CPU_BUS:
201                         sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
202                         break;
203                 default:
204                         printk_err("Unknown device path type: %d\n", dev->path.type);
205                         break;
206                 }
207         }
208         return buffer;
209 }
210
211 const char *bus_path(struct bus *bus)
212 {
213         static char buffer[BUS_PATH_MAX];
214         sprintf(buffer, "%s,%d",
215                 dev_path(bus->dev), bus->link);
216         return buffer;
217 }
218
219 int path_eq(struct device_path *path1, struct device_path *path2)
220 {
221         int equal = 0;
222         if (path1->type == path2->type) {
223                 switch(path1->type) {
224                 case DEVICE_PATH_NONE:
225                         break;
226                 case DEVICE_PATH_ROOT:
227                         equal = 1;
228                         break;
229                 case DEVICE_PATH_PCI:
230                         equal = (path1->pci.devfn == path2->pci.devfn);
231                         break;
232                 case DEVICE_PATH_PNP:
233                         equal = (path1->pnp.port == path2->pnp.port) &&
234                                 (path1->pnp.device == path2->pnp.device);
235                         break;
236                 case DEVICE_PATH_I2C:
237                         equal = (path1->i2c.device == path2->i2c.device);
238                         break;
239                 case DEVICE_PATH_APIC:
240                         equal = (path1->apic.apic_id == path2->apic.apic_id);
241                         break;
242                 case DEVICE_PATH_PCI_DOMAIN:
243                         equal = (path1->pci_domain.domain == path2->pci_domain.domain);
244                         break;
245                 case DEVICE_PATH_APIC_CLUSTER:
246                         equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster);
247                         break;
248                 case DEVICE_PATH_CPU:
249                         equal = (path1->cpu.id == path2->cpu.id);
250                         break;
251                 case DEVICE_PATH_CPU_BUS:
252                         equal = (path1->cpu_bus.id == path2->cpu_bus.id);
253                         break;
254                 default:
255                         printk_err("Uknown device type: %d\n", path1->type);
256                         break;
257                 }
258         }
259         return equal;
260 }
261
262 /**
263  * See if we have unused but allocated resource structures.
264  * If so remove the allocation.
265  * @param dev The device to find the resource on
266  */
267 void compact_resources(device_t dev)
268 {
269         struct resource *resource;
270         int i;
271         /* Move all of the free resources to the end */
272         for(i = 0; i < dev->resources;) {
273                 resource = &dev->resource[i];
274                 if (!resource->flags) {
275                         memmove(resource, resource + 1, (dev->resources - i) *
276                                 sizeof(*resource));
277                         dev->resources -= 1;
278                         memset(&dev->resource[dev->resources], 0, sizeof(*resource));
279                 } else {
280                         i++;
281                 }
282         }
283 }
284
285
286 /**
287  * See if a resource structure already exists for a given index
288  * @param dev The device to find the resource on
289  * @param index  The index of the resource on the device.
290  * @return the resource if it already exists
291  */
292 struct resource *probe_resource(device_t dev, unsigned index)
293 {
294         struct resource *resource;
295         int i;
296         /* See if there is a resource with the appropriate index */
297         resource = 0;
298         for(i = 0; i < dev->resources; i++) {
299                 if (dev->resource[i].index == index) {
300                         resource = &dev->resource[i];
301                         break;
302                 }
303         }
304         return resource;
305 }
306
307 /**
308  * See if a resource structure already exists for a given index and if
309  * not allocate one.  Then initialize the initialize the resource
310  * to default values.
311  * @param dev The device to find the resource on
312  * @param index  The index of the resource on the device.
313  */
314 struct resource *new_resource(device_t dev, unsigned index)
315 {
316         struct resource *resource;
317
318         /* First move all of the free resources to the end */
319         compact_resources(dev);
320
321         /* See if there is a resource with the appropriate index */
322         resource = probe_resource(dev, index);
323         if (!resource) {
324                 if (dev->resources == MAX_RESOURCES) {
325                         die("MAX_RESOURCES exceeded.");
326                 }
327                 resource = &dev->resource[dev->resources];
328                 memset(resource, 0, sizeof(*resource));
329                 dev->resources++;
330         }
331         /* Initialize the resource values */
332         if (!(resource->flags & IORESOURCE_FIXED)) {
333                 resource->flags = 0;
334                 resource->base = 0;
335         }
336         resource->size  = 0;
337         resource->limit = 0;
338         resource->index = index;
339         resource->align = 0;
340         resource->gran  = 0;
341
342         return resource;
343 }
344
345 /**
346  * Return an existing resource structure for a given index.
347  * @param dev The device to find the resource on
348  * @param index  The index of the resource on the device.
349  */
350 struct resource *find_resource(device_t dev, unsigned index)
351 {
352         struct resource *resource;
353
354         /* See if there is a resource with the appropriate index */
355         resource = probe_resource(dev, index);
356         if (!resource) {
357                 printk_emerg("%s missing resource: %02x\n",
358                         dev_path(dev), index);
359                 die("");
360         }
361         return resource;
362 }
363
364
365 /**
366  * @brief round a number up to the next multiple of gran
367  * @param val the starting value
368  * @param gran granularity we are aligning the number to.
369  * @returns aligned value
370  */
371 static resource_t align_up(resource_t val, unsigned long gran)
372 {
373         resource_t mask;
374         mask = (1ULL << gran) - 1ULL;
375         val += mask;
376         val &= ~mask;
377         return val;
378 }
379
380 /**
381  * @brief round a number up to the previous multiple of gran
382  * @param val the starting value
383  * @param gran granularity we are aligning the number to.
384  * @returns aligned value
385  */
386 static resource_t align_down(resource_t val, unsigned long gran)
387 {
388         resource_t mask;
389         mask = (1ULL << gran) - 1ULL;
390         val &= ~mask;
391         return val;
392 }
393
394 /**
395  * @brief Compute the maximum address that is part of a resource
396  * @param resource the resource whose limit is desired
397  * @returns the end
398  */
399 resource_t resource_end(struct resource *resource)
400 {
401         resource_t base, end;
402         /* get the base address */
403         base = resource->base;
404
405         /* For a non bridge resource granularity and alignment are the same.
406          * For a bridge resource align is the largest needed alignment below
407          * the bridge.  While the granularity is simply how many low bits of the
408          * address cannot be set.
409          */
410         
411         /* Get the end (rounded up) */
412         end = base + align_up(resource->size, resource->gran) - 1;
413
414         return end;
415 }
416
417 /**
418  * @brief Compute the maximum legal value for resource->base
419  * @param resource the resource whose maximum is desired
420  * @returns the maximum
421  */
422 resource_t resource_max(struct resource *resource)
423 {
424         resource_t max;
425
426         max = align_down(resource->limit - resource->size + 1, resource->align);
427
428         return max;
429 }
430
431 /**
432  * @brief return the resource type of a resource
433  * @param resource the resource type to decode.
434  */
435 const char *resource_type(struct resource *resource)
436 {
437         static char buffer[RESOURCE_TYPE_MAX];
438         sprintf(buffer, "%s%s%s%s",
439                 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
440                 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
441                 ((resource->flags == 0)? "unused":
442                 (resource->flags & IORESOURCE_IO)? "io":
443                 (resource->flags & IORESOURCE_DRQ)? "drq":
444                 (resource->flags & IORESOURCE_IRQ)? "irq":
445                 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
446                 ((resource->flags & IORESOURCE_PCI64)?"64":""));
447         return buffer;
448 }
449
450 /**
451  * @brief print the resource that was just stored.
452  * @param dev the device the stored resorce lives on
453  * @param resource the resource that was just stored.
454  */
455 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
456 {
457         if (resource->flags & IORESOURCE_STORED) {
458                 char buf[10];
459                 unsigned long long base, end;
460                 base = resource->base;
461                 end = resource_end(resource);
462                 buf[0] = '\0';
463                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
464 #if CONFIG_PCI_BUS_SEGN_BITS
465                         sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
466 #else
467                         sprintf(buf, "bus %02x ", dev->link[0].secondary);
468 #endif
469                 }
470                 printk_debug(
471                         "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
472                         dev_path(dev),
473                         resource->index,
474                         base, end,
475                         resource->size, resource->gran,
476                         buf,
477                         resource_type(resource),
478                         comment);
479         }
480 }
481
482 void search_bus_resources(struct bus *bus,
483         unsigned long type_mask, unsigned long type,
484         resource_search_t search, void *gp)
485 {
486         struct device *curdev;
487         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
488                 int i;
489                 /* Ignore disabled devices */
490                 if (!curdev->have_resources) continue;
491                 for(i = 0; i < curdev->resources; i++) {
492                         struct resource *resource = &curdev->resource[i];
493                         /* If it isn't the right kind of resource ignore it */
494                         if ((resource->flags & type_mask) != type) {
495                                 continue;
496                         }
497                         /* If it is a subtractive resource recurse */
498                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
499                                 struct bus * subbus;
500                                 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
501                                 search_bus_resources(subbus, type_mask, type, search, gp);
502                                 continue;
503                         }
504                         search(gp, curdev, resource);
505                 }
506         }
507 }
508
509 void search_global_resources(
510         unsigned long type_mask, unsigned long type,
511         resource_search_t search, void *gp)
512 {
513         struct device *curdev;
514         for(curdev = all_devices; curdev; curdev = curdev->next) {
515                 int i;
516                 /* Ignore disabled devices */
517                 if (!curdev->have_resources) continue;
518                 for(i = 0; i < curdev->resources; i++) {
519                         struct resource *resource = &curdev->resource[i];
520                         /* If it isn't the right kind of resource ignore it */
521                         if ((resource->flags & type_mask) != type) {
522                                 continue;
523                         }
524                         /* If it is a subtractive resource ignore it */
525                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
526                                 continue;
527                         }
528                         search(gp, curdev, resource);
529                 }
530         }
531 }
532
533 void dev_set_enabled(device_t dev, int enable)
534 {
535         if (dev->enabled == enable) {
536                 return;
537         }
538         dev->enabled = enable;
539         if (dev->ops && dev->ops->enable) {
540                 dev->ops->enable(dev);
541         }
542         else if (dev->chip_ops && dev->chip_ops->enable_dev) {
543                 dev->chip_ops->enable_dev(dev);
544         }
545 }
546
547 void disable_children(struct bus *bus)
548 {
549         device_t child;
550         for(child = bus->children; child; child = child->sibling) {
551                 int link;
552                 for(link = 0; link < child->links; link++) {
553                         disable_children(&child->link[link]);
554                 }
555                 dev_set_enabled(child, 0);
556         }
557 }
558
559 void resource_tree(struct device *root, int debug_level, int depth)
560 {
561         int i = 0, link = 0;
562         struct device *child;
563         char indent[30];        /* If your tree has more levels, it's wrong. */
564
565         for (i = 0; i < depth + 1 && i < 29; i++)
566                 indent[i] = ' ';
567         indent[i] = '\0';
568
569         do_printk(BIOS_DEBUG, "%s%s links %x child on link 0 %s\n",
570                   indent, dev_path(root), root->links,
571                   root->link[0].children ? dev_path(root->link[0].children) :
572                                           "NULL");
573         for (i = 0; i < root->resources; i++) {
574                 do_printk(BIOS_DEBUG,
575                           "%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n",
576                           indent, dev_path(root), root->resource[i].base,
577                           root->resource[i].size, root->resource[i].align,
578                           root->resource[i].gran, root->resource[i].limit,
579                           root->resource[i].flags, root->resource[i].index);
580         }
581
582         for (link = 0; link < root->links; link++) {
583                 for (child = root->link[link].children; child;
584                      child = child->sibling)
585                         resource_tree(child, debug_level, depth + 1);
586         }
587 }
588
589 void print_resource_tree(struct device * root, int debug_level,
590                          const char *msg)
591 {
592         /* Bail if root is null. */
593         if (!root) {
594                 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
595                 return;
596         }
597
598         /* Bail if not printing to screen. */
599         if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
600                     dev_path(root), msg))
601                 return;
602         resource_tree(root, debug_level, 0);
603 }
604
605 void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
606 {
607         char depth_str[20] = "";
608         int i;
609         struct device *sibling;
610         for (i = 0; i < depth; i++)
611                 depth_str[i] = ' ';
612         depth_str[i] = '\0';
613         do_printk(debug_level, "%s%s: enabled %d, %d resources\n",
614                   depth_str, dev_path(dev), dev->enabled, dev->resources);
615         for (i = 0; i < dev->links; i++) {
616                 for (sibling = dev->link[i].children; sibling;
617                      sibling = sibling->sibling)
618                         show_devs_tree(sibling, debug_level, depth + 1, i);
619         }
620 }
621
622 void show_all_devs_tree(int debug_level, const char *msg)
623 {
624         /* Bail if not printing to screen. */
625         if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
626                 return;
627         show_devs_tree(all_devices, debug_level, 0, -1);
628 }
629
630 void show_devs_subtree(struct device *root, int debug_level, const char *msg)
631 {
632         /* Bail if not printing to screen. */
633         if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
634                     dev_path(root), msg))
635                 return;
636         do_printk(debug_level, "%s\n", msg);
637         show_devs_tree(root, debug_level, 0, -1);
638 }
639
640 void show_all_devs(int debug_level, const char *msg)
641 {
642         struct device *dev;
643
644         /* Bail if not printing to screen. */
645         if (!do_printk(debug_level, "Show all devs...%s\n", msg))
646                 return;
647         for (dev = all_devices; dev; dev = dev->next) {
648                 do_printk(debug_level,
649                           "%s: enabled %d, %d resources\n",
650                           dev_path(dev), dev->enabled,
651                           dev->resources);
652         }
653 }
654
655 void show_one_resource(int debug_level, struct device *dev,
656                        struct resource *resource, const char *comment)
657 {
658         char buf[10];
659         unsigned long long base, end;
660         base = resource->base;
661         end = resource_end(resource);
662         buf[0] = '\0';
663 /*
664         if (resource->flags & IORESOURCE_BRIDGE) {
665 #if CONFIG_PCI_BUS_SEGN_BITS
666                 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
667                         dev->link[0].secondary & 0xff);
668 #else
669                 sprintf(buf, "bus %02x ", dev->link[0].secondary);
670 #endif
671         }
672 */
673         do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
674                   "size 0x%08Lx gran 0x%02x %s%s%s\n",
675                   dev_path(dev), resource->index, base, end,
676                   resource->size, resource->gran, buf,
677                   resource_type(resource), comment);
678
679 }
680
681 void show_all_devs_resources(int debug_level, const char* msg)
682 {
683         struct device *dev;
684
685         if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
686                 return;
687
688         for (dev = all_devices; dev; dev = dev->next) {
689                 int i;
690                 do_printk(debug_level,
691                           "%s: enabled %d, %d resources\n",
692                           dev_path(dev), dev->enabled,
693                           dev->resources);
694                 for (i = 0; i < dev->resources; i++)
695                         show_one_resource(debug_level, dev, &dev->resource[i], "");
696         }
697 }