fix a whole bunch of warnings. (trivial)
[coreboot.git] / src / devices / device_util.c
1 /*
2  * This file is part of the LinuxBIOS 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.u.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.u.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 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.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
170 #else
171                         sprintf(buffer, "PCI: %02x:%02x.%01x",
172                                 dev->bus->secondary, 
173                                 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
174 #endif
175                         break;
176                 case DEVICE_PATH_PNP:
177                         sprintf(buffer, "PNP: %04x.%01x",
178                                 dev->path.u.pnp.port, dev->path.u.pnp.device);
179                         break;
180                 case DEVICE_PATH_I2C:
181                         sprintf(buffer, "I2C: %02x:%02x",
182                                 dev->bus->secondary,
183                                 dev->path.u.i2c.device);
184                         break;
185                 case DEVICE_PATH_APIC:
186                         sprintf(buffer, "APIC: %02x",
187                                 dev->path.u.apic.apic_id);
188                         break;
189                 case DEVICE_PATH_PCI_DOMAIN:
190                         sprintf(buffer, "PCI_DOMAIN: %04x",
191                                 dev->path.u.pci_domain.domain);
192                         break;
193                 case DEVICE_PATH_APIC_CLUSTER:
194                         sprintf(buffer, "APIC_CLUSTER: %01x",
195                                 dev->path.u.apic_cluster.cluster);
196                         break;
197                 case DEVICE_PATH_CPU:
198                         sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
199                         break;
200                 case DEVICE_PATH_CPU_BUS:
201                         sprintf(buffer, "CPU_BUS: %02x", dev->path.u.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->u.pci.devfn == path2->u.pci.devfn);
231                         break;
232                 case DEVICE_PATH_PNP:
233                         equal = (path1->u.pnp.port == path2->u.pnp.port) &&
234                                 (path1->u.pnp.device == path2->u.pnp.device);
235                         break;
236                 case DEVICE_PATH_I2C:
237                         equal = (path1->u.i2c.device == path2->u.i2c.device);
238                         break;
239                 case DEVICE_PATH_APIC:
240                         equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
241                         break;
242                 case DEVICE_PATH_PCI_DOMAIN:
243                         equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
244                         break;
245                 case DEVICE_PATH_APIC_CLUSTER:
246                         equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
247                         break;
248                 case DEVICE_PATH_CPU:
249                         equal = (path1->u.cpu.id == path2->u.cpu.id);
250                         break;
251                 case DEVICE_PATH_CPU_BUS:
252                         equal = (path1->u.cpu_bus.id == path2->u.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                         dev->resources -= 1;
277                         memset(&dev->resource[dev->resources], 0, sizeof(*resource));
278                 } else {
279                         i++;
280                 }
281         }
282 }
283
284
285 /**
286  * See if a resource structure already exists for a given index
287  * @param dev The device to find the resource on
288  * @param index  The index of the resource on the device.
289  * @return the resource if it already exists
290  */
291 struct resource *probe_resource(device_t dev, unsigned index)
292 {
293         struct resource *resource;
294         int i;
295         /* See if there is a resource with the appropriate index */
296         resource = 0;
297         for(i = 0; i < dev->resources; i++) {
298                 if (dev->resource[i].index == index) {
299                         resource = &dev->resource[i];
300                         break;
301                 }
302         }
303         return resource;
304 }
305
306 /**
307  * See if a resource structure already exists for a given index and if
308  * not allocate one.  Then initialize the initialize the resource
309  * to default values.
310  * @param dev The device to find the resource on
311  * @param index  The index of the resource on the device.
312  */
313 struct resource *new_resource(device_t dev, unsigned index)
314 {
315         struct resource *resource;
316
317         /* First move all of the free resources to the end */
318         compact_resources(dev);
319
320         /* See if there is a resource with the appropriate index */
321         resource = probe_resource(dev, index);
322         if (!resource) {
323                 if (dev->resources == MAX_RESOURCES) {
324                         die("MAX_RESOURCES exceeded.");
325                 }
326                 resource = &dev->resource[dev->resources];
327                 memset(resource, 0, sizeof(*resource));
328                 dev->resources++;
329         }
330         /* Initialize the resource values */
331         if (!(resource->flags & IORESOURCE_FIXED)) {
332                 resource->flags = 0;
333                 resource->base = 0;
334         }
335         resource->size  = 0;
336         resource->limit = 0;
337         resource->index = index;
338         resource->align = 0;
339         resource->gran  = 0;
340
341         return resource;
342 }
343
344 /**
345  * Return an existing resource structure for a given index.
346  * @param dev The device to find the resource on
347  * @param index  The index of the resource on the device.
348  */
349 struct resource *find_resource(device_t dev, unsigned index)
350 {
351         struct resource *resource;
352
353         /* See if there is a resource with the appropriate index */
354         resource = probe_resource(dev, index);
355         if (!resource) {
356                 printk_emerg("%s missing resource: %02x\n",
357                         dev_path(dev), index);
358                 die("");
359         }
360         return resource;
361 }
362
363
364 /**
365  * @brief round a number up to the next multiple of gran
366  * @param val the starting value
367  * @param gran granularity we are aligning the number to.
368  * @returns aligned value
369  */
370 static resource_t align_up(resource_t val, unsigned long gran)
371 {
372         resource_t mask;
373         mask = (1ULL << gran) - 1ULL;
374         val += mask;
375         val &= ~mask;
376         return val;
377 }
378
379 /**
380  * @brief round a number up to the previous multiple of gran
381  * @param val the starting value
382  * @param gran granularity we are aligning the number to.
383  * @returns aligned value
384  */
385 static resource_t align_down(resource_t val, unsigned long gran)
386 {
387         resource_t mask;
388         mask = (1ULL << gran) - 1ULL;
389         val &= ~mask;
390         return val;
391 }
392
393 /**
394  * @brief Compute the maximum address that is part of a resource
395  * @param resource the resource whose limit is desired
396  * @returns the end
397  */
398 resource_t resource_end(struct resource *resource)
399 {
400         resource_t base, end;
401         /* get the base address */
402         base = resource->base;
403
404         /* For a non bridge resource granularity and alignment are the same.
405          * For a bridge resource align is the largest needed alignment below
406          * the bridge.  While the granularity is simply how many low bits of the
407          * address cannot be set.
408          */
409         
410         /* Get the end (rounded up) */
411         end = base + align_up(resource->size, resource->gran) - 1;
412
413         return end;
414 }
415
416 /**
417  * @brief Compute the maximum legal value for resource->base
418  * @param resource the resource whose maximum is desired
419  * @returns the maximum
420  */
421 resource_t resource_max(struct resource *resource)
422 {
423         resource_t max;
424
425         max = align_down(resource->limit - resource->size + 1, resource->align);
426
427         return max;
428 }
429
430 /**
431  * @brief return the resource type of a resource
432  * @param resource the resource type to decode.
433  */
434 const char *resource_type(struct resource *resource)
435 {
436         static char buffer[RESOURCE_TYPE_MAX];
437         sprintf(buffer, "%s%s%s%s",
438                 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
439                 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
440                 ((resource->flags == 0)? "unused":
441                 (resource->flags & IORESOURCE_IO)? "io":
442                 (resource->flags & IORESOURCE_DRQ)? "drq":
443                 (resource->flags & IORESOURCE_IRQ)? "irq":
444                 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
445                 ((resource->flags & IORESOURCE_PCI64)?"64":""));
446         return buffer;
447 }
448
449 /**
450  * @brief print the resource that was just stored.
451  * @param dev the device the stored resorce lives on
452  * @param resource the resource that was just stored.
453  */
454 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
455 {
456         if (resource->flags & IORESOURCE_STORED) {
457                 char buf[10];
458                 unsigned long long base, end;
459                 base = resource->base;
460                 end = resource_end(resource);
461                 buf[0] = '\0';
462                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
463 #if PCI_BUS_SEGN_BITS
464                         sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
465 #else
466                         sprintf(buf, "bus %02x ", dev->link[0].secondary);
467 #endif
468                 }
469                 printk_debug(
470                         "%s %02x <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
471                         dev_path(dev),
472                         resource->index,
473                         base, end,
474                         resource->size, resource->gran,
475                         buf,
476                         resource_type(resource),
477                         comment);
478         }
479 }
480
481 void search_bus_resources(struct bus *bus,
482         unsigned long type_mask, unsigned long type,
483         resource_search_t search, void *gp)
484 {
485         struct device *curdev;
486         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
487                 int i;
488                 /* Ignore disabled devices */
489                 if (!curdev->have_resources) continue;
490                 for(i = 0; i < curdev->resources; i++) {
491                         struct resource *resource = &curdev->resource[i];
492                         /* If it isn't the right kind of resource ignore it */
493                         if ((resource->flags & type_mask) != type) {
494                                 continue;
495                         }
496                         /* If it is a subtractive resource recurse */
497                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
498                                 struct bus * subbus;
499                                 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
500                                 search_bus_resources(subbus, type_mask, type, search, gp);
501                                 continue;
502                         }
503                         search(gp, curdev, resource);
504                 }
505         }
506 }
507
508 void search_global_resources(
509         unsigned long type_mask, unsigned long type,
510         resource_search_t search, void *gp)
511 {
512         struct device *curdev;
513         for(curdev = all_devices; curdev; curdev = curdev->next) {
514                 int i;
515                 /* Ignore disabled devices */
516                 if (!curdev->have_resources) continue;
517                 for(i = 0; i < curdev->resources; i++) {
518                         struct resource *resource = &curdev->resource[i];
519                         /* If it isn't the right kind of resource ignore it */
520                         if ((resource->flags & type_mask) != type) {
521                                 continue;
522                         }
523                         /* If it is a subtractive resource ignore it */
524                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
525                                 continue;
526                         }
527                         search(gp, curdev, resource);
528                 }
529         }
530 }
531
532 void dev_set_enabled(device_t dev, int enable)
533 {
534         if (dev->enabled == enable) {
535                 return;
536         }
537         dev->enabled = enable;
538         if (dev->ops && dev->ops->enable) {
539                 dev->ops->enable(dev);
540         }
541         else if (dev->chip_ops && dev->chip_ops->enable_dev) {
542                 dev->chip_ops->enable_dev(dev);
543         }
544 }
545
546 void disable_children(struct bus *bus)
547 {
548         device_t child;
549         for(child = bus->children; child; child = child->sibling) {
550                 int link;
551                 for(link = 0; link < child->links; link++) {
552                         disable_children(&child->link[link]);
553                 }
554                 dev_set_enabled(child, 0);
555         }
556 }