979b0253247958f7aa67abd434ae330685a481ae
[coreboot.git] / src / devices / device_util.c
1 #include <console/console.h>
2 #include <device/device.h>
3 #include <device/path.h>
4 #include <device/pci.h>
5 #include <device/resource.h>
6 #include <string.h>
7
8 /**
9  * @brief See if a device structure exists for path
10  *
11  * @param bus The bus to find the device on
12  * @param path The relative path from the bus to the appropriate device
13  * @return pointer to a device structure for the device on bus at path
14  *         or 0/NULL if no device is found
15  */
16 device_t find_dev_path(struct bus *parent, struct device_path *path)
17 {
18         device_t child;
19         for(child = parent->children; child; child = child->sibling) {
20                 if (path_eq(path, &child->path)) {
21                         break;
22                 }
23         }
24         return child;
25 }
26
27 /**
28  * @brief See if a device structure already exists and if not allocate it
29  *
30  * @param bus The bus to find the device on
31  * @param path The relative path from the bus to the appropriate device
32  * @return pointer to a device structure for the device on bus at path
33  */
34 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
35 {
36         device_t child;
37         child = find_dev_path(parent, path);
38         if (!child) {
39                 child = alloc_dev(parent, path);
40         }
41         return child;
42 }
43
44 /**
45  * @brief Given a PCI bus and a devfn number, find the device structure
46  *
47  * @param bus The bus number
48  * @param devfn a device/function number
49  * @return pointer to the device structure
50  */
51 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
52 {
53         struct device *dev, *result;
54
55         result = 0;
56         for (dev = all_devices; dev; dev = dev->next) {
57                 if ((dev->path.type == DEVICE_PATH_PCI) &&
58                         (dev->bus->secondary == bus) && 
59                         (dev->path.u.pci.devfn == devfn)) {
60                         result = dev;
61                         break;
62                 }
63         }
64         return result;
65 }
66
67 /**
68  * @brief Given a smbus bus and a device number, find the device structure
69  *
70  * @param bus The bus number
71  * @param addr a device number 
72  * @return pointer to the device structure
73  */
74 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
75 {
76         struct device *dev, *result;
77         
78         result = 0;
79         for (dev = all_devices; dev; dev = dev->next) {
80                 if ((dev->path.type == DEVICE_PATH_I2C) &&
81                         (dev->bus->secondary == bus) && 
82                         (dev->path.u.i2c.device == addr)) {
83                         result = dev;
84                         break; 
85                 }       
86         }       
87         return result;
88 }    
89
90 /** Find a device of a given vendor and type
91  * @param vendor Vendor ID (e.g. 0x8086 for Intel)
92  * @param device Device ID
93  * @param from Pointer to the device structure, used as a starting point
94  *        in the linked list of all_devices, which can be 0 to start at the 
95  *        head of the list (i.e. all_devices)
96  * @return Pointer to the device struct 
97  */
98 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
99 {
100         if (!from)
101                 from = all_devices;
102         else
103                 from = from->next;
104         while (from && (from->vendor != vendor || from->device != device)) {
105                 from = from->next;
106         }
107         return from;
108 }
109
110 /** Find a device of a given class
111  * @param class Class of the device
112  * @param from Pointer to the device structure, used as a starting point
113  *        in the linked list of all_devices, which can be 0 to start at the 
114  *        head of the list (i.e. all_devices)
115  * @return Pointer to the device struct 
116  */
117 struct device *dev_find_class(unsigned int class, struct device *from)
118 {
119         if (!from)
120                 from = all_devices;
121         else
122                 from = from->next;
123         while (from && (from->class & 0xffffff00) != class)
124                 from = from->next;
125         return from;
126 }
127
128
129 const char *dev_path(device_t dev)
130 {
131         static char buffer[DEVICE_PATH_MAX];
132         buffer[0] = '\0';
133         if (!dev) {
134                 memcpy(buffer, "<null>", 7);
135         }
136         else {
137                 switch(dev->path.type) {
138                 case DEVICE_PATH_ROOT:
139                         memcpy(buffer, "Root Device", 12);
140                         break;
141                 case DEVICE_PATH_PCI:
142 #if PCI_BUS_SEGN_BITS
143                         sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
144                                 dev->bus->secondary>>8, dev->bus->secondary & 0xff, 
145                                 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
146 #else
147                         sprintf(buffer, "PCI: %02x:%02x.%01x",
148                                 dev->bus->secondary, 
149                                 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
150 #endif
151                         break;
152                 case DEVICE_PATH_PNP:
153                         sprintf(buffer, "PNP: %04x.%01x",
154                                 dev->path.u.pnp.port, dev->path.u.pnp.device);
155                         break;
156                 case DEVICE_PATH_I2C:
157                         sprintf(buffer, "I2C: %02x:%02x",
158                                 dev->bus->secondary,
159                                 dev->path.u.i2c.device);
160                         break;
161                 case DEVICE_PATH_APIC:
162                         sprintf(buffer, "APIC: %02x",
163                                 dev->path.u.apic.apic_id);
164                         break;
165                 case DEVICE_PATH_PCI_DOMAIN:
166                         sprintf(buffer, "PCI_DOMAIN: %04x",
167                                 dev->path.u.pci_domain.domain);
168                         break;
169                 case DEVICE_PATH_APIC_CLUSTER:
170                         sprintf(buffer, "APIC_CLUSTER: %01x",
171                                 dev->path.u.apic_cluster.cluster);
172                         break;
173                 case DEVICE_PATH_CPU:
174                         sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
175                         break;
176                 case DEVICE_PATH_CPU_BUS:
177                         sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
178                         break;
179                 default:
180                         printk_err("Unknown device path type: %d\n", dev->path.type);
181                         break;
182                 }
183         }
184         return buffer;
185 }
186
187 const char *bus_path(struct bus *bus)
188 {
189         static char buffer[BUS_PATH_MAX];
190         sprintf(buffer, "%s,%d",
191                 dev_path(bus->dev), bus->link);
192         return buffer;
193 }
194
195 int path_eq(struct device_path *path1, struct device_path *path2)
196 {
197         int equal = 0;
198         if (path1->type == path2->type) {
199                 switch(path1->type) {
200                 case DEVICE_PATH_NONE:
201                         break;
202                 case DEVICE_PATH_ROOT:
203                         equal = 1;
204                         break;
205                 case DEVICE_PATH_PCI:
206                         equal = (path1->u.pci.devfn == path2->u.pci.devfn);
207                         break;
208                 case DEVICE_PATH_PNP:
209                         equal = (path1->u.pnp.port == path2->u.pnp.port) &&
210                                 (path1->u.pnp.device == path2->u.pnp.device);
211                         break;
212                 case DEVICE_PATH_I2C:
213                         equal = (path1->u.i2c.device == path2->u.i2c.device);
214                         break;
215                 case DEVICE_PATH_APIC:
216                         equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
217                         break;
218                 case DEVICE_PATH_PCI_DOMAIN:
219                         equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
220                         break;
221                 case DEVICE_PATH_APIC_CLUSTER:
222                         equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
223                         break;
224                 case DEVICE_PATH_CPU:
225                         equal = (path1->u.cpu.id == path2->u.cpu.id);
226                         break;
227                 case DEVICE_PATH_CPU_BUS:
228                         equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
229                         break;
230                 default:
231                         printk_err("Uknown device type: %d\n", path1->type);
232                         break;
233                 }
234         }
235         return equal;
236 }
237
238 /**
239  * See if we have unused but allocated resource structures.
240  * If so remove the allocation.
241  * @param dev The device to find the resource on
242  */
243 void compact_resources(device_t dev)
244 {
245         struct resource *resource;
246         int i;
247         /* Move all of the free resources to the end */
248         for(i = 0; i < dev->resources;) {
249                 resource = &dev->resource[i];
250                 if (!resource->flags) {
251                         memmove(resource, resource + 1, dev->resources - i);
252                         dev->resources -= 1;
253                         memset(&dev->resource[dev->resources], 0, sizeof(*resource));
254                 } else {
255                         i++;
256                 }
257         }
258 }
259
260
261 /**
262  * See if a resource structure already exists for a given index
263  * @param dev The device to find the resource on
264  * @param index  The index of the resource on the device.
265  * @return the resource if it already exists
266  */
267 struct resource *probe_resource(device_t dev, unsigned index)
268 {
269         struct resource *resource;
270         int i;
271         /* See if there is a resource with the appropriate index */
272         resource = 0;
273         for(i = 0; i < dev->resources; i++) {
274                 if (dev->resource[i].index == index) {
275                         resource = &dev->resource[i];
276                         break;
277                 }
278         }
279         return resource;
280 }
281
282 /**
283  * See if a resource structure already exists for a given index and if
284  * not allocate one.  Then initialize the initialize the resource
285  * to default values.
286  * @param dev The device to find the resource on
287  * @param index  The index of the resource on the device.
288  */
289 struct resource *new_resource(device_t dev, unsigned index)
290 {
291         struct resource *resource;
292
293         /* First move all of the free resources to the end */
294         compact_resources(dev);
295
296         /* See if there is a resource with the appropriate index */
297         resource = probe_resource(dev, index);
298         if (!resource) {
299                 if (dev->resources == MAX_RESOURCES) {
300                         die("MAX_RESOURCES exceeded.");
301                 }
302                 resource = &dev->resource[dev->resources];
303                 memset(resource, 0, sizeof(*resource));
304                 dev->resources++;
305         }
306         /* Initialize the resource values */
307         if (!(resource->flags & IORESOURCE_FIXED)) {
308                 resource->flags = 0;
309                 resource->base = 0;
310         }
311         resource->size  = 0;
312         resource->limit = 0;
313         resource->index = index;
314         resource->align = 0;
315         resource->gran  = 0;
316
317         return resource;
318 }
319
320 /**
321  * Return an existing resource structure for a given index.
322  * @param dev The device to find the resource on
323  * @param index  The index of the resource on the device.
324  */
325 struct resource *find_resource(device_t dev, unsigned index)
326 {
327         struct resource *resource;
328
329         /* See if there is a resource with the appropriate index */
330         resource = probe_resource(dev, index);
331         if (!resource) {
332                 printk_emerg("%s missing resource: %02x\n",
333                         dev_path(dev), index);
334                 die("");
335         }
336         return resource;
337 }
338
339
340 /**
341  * @brief round a number up to the next multiple of gran
342  * @param val the starting value
343  * @param gran granularity we are aligning the number to.
344  * @returns aligned value
345  */
346 static resource_t align_up(resource_t val, unsigned long gran)
347 {
348         resource_t mask;
349         mask = (1ULL << gran) - 1ULL;
350         val += mask;
351         val &= ~mask;
352         return val;
353 }
354
355 /**
356  * @brief round a number up to the previous multiple of gran
357  * @param val the starting value
358  * @param gran granularity we are aligning the number to.
359  * @returns aligned value
360  */
361 static resource_t align_down(resource_t val, unsigned long gran)
362 {
363         resource_t mask;
364         mask = (1ULL << gran) - 1ULL;
365         val &= ~mask;
366         return val;
367 }
368
369 /**
370  * @brief Compute the maximum address that is part of a resource
371  * @param resource the resource whose limit is desired
372  * @returns the end
373  */
374 resource_t resource_end(struct resource *resource)
375 {
376         resource_t base, end;
377         /* get the base address */
378         base = resource->base;
379
380         /* For a non bridge resource granularity and alignment are the same.
381          * For a bridge resource align is the largest needed alignment below
382          * the bridge.  While the granularity is simply how many low bits of the
383          * address cannot be set.
384          */
385         
386         /* Get the end (rounded up) */
387         end = base + align_up(resource->size, resource->gran) - 1;
388
389         return end;
390 }
391
392 /**
393  * @brief Compute the maximum legal value for resource->base
394  * @param resource the resource whose maximum is desired
395  * @returns the maximum
396  */
397 resource_t resource_max(struct resource *resource)
398 {
399         resource_t max;
400
401         max = align_down(resource->limit - resource->size + 1, resource->align);
402
403         return max;
404 }
405
406 /**
407  * @brief return the resource type of a resource
408  * @param resource the resource type to decode.
409  */
410 const char *resource_type(struct resource *resource)
411 {
412         static char buffer[RESOURCE_TYPE_MAX];
413         sprintf(buffer, "%s%s%s%s",
414                 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
415                 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
416                 ((resource->flags == 0)? "unused":
417                 (resource->flags & IORESOURCE_IO)? "io":
418                 (resource->flags & IORESOURCE_DRQ)? "drq":
419                 (resource->flags & IORESOURCE_IRQ)? "irq":
420                 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
421                 ((resource->flags & IORESOURCE_PCI64)?"64":""));
422         return buffer;
423 }
424
425 /**
426  * @brief print the resource that was just stored.
427  * @param dev the device the stored resorce lives on
428  * @param resource the resource that was just stored.
429  */
430 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
431 {
432         if (resource->flags & IORESOURCE_STORED) {
433                 unsigned char buf[10];
434                 unsigned long long base, end;
435                 base = resource->base;
436                 end = resource_end(resource);
437                 buf[0] = '\0';
438                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
439 #if PCI_BUS_SEGN_BITS
440                         sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
441 #else
442                         sprintf(buf, "bus %02x ", dev->link[0].secondary);
443 #endif
444                 }
445                 printk_debug(
446                         "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s\n",
447                         dev_path(dev),
448                         resource->index,
449                         base, end,
450                         buf,
451                         resource_type(resource),
452                         comment);
453         }
454 }
455
456 void search_bus_resources(struct bus *bus,
457         unsigned long type_mask, unsigned long type,
458         resource_search_t search, void *gp)
459 {
460         struct device *curdev;
461         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
462                 int i;
463                 /* Ignore disabled devices */
464                 if (!curdev->have_resources) continue;
465                 for(i = 0; i < curdev->resources; i++) {
466                         struct resource *resource = &curdev->resource[i];
467                         /* If it isn't the right kind of resource ignore it */
468                         if ((resource->flags & type_mask) != type) {
469                                 continue;
470                         }
471                         /* If it is a subtractive resource recurse */
472                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
473                                 struct bus * subbus;
474                                 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
475                                 search_bus_resources(subbus, type_mask, type, search, gp);
476                                 continue;
477                         }
478                         search(gp, curdev, resource);
479                 }
480         }
481 }
482
483 void search_global_resources(
484         unsigned long type_mask, unsigned long type,
485         resource_search_t search, void *gp)
486 {
487         struct device *curdev;
488         for(curdev = all_devices; curdev; curdev = curdev->next) {
489                 int i;
490                 /* Ignore disabled devices */
491                 if (!curdev->have_resources) continue;
492                 for(i = 0; i < curdev->resources; i++) {
493                         struct resource *resource = &curdev->resource[i];
494                         /* If it isn't the right kind of resource ignore it */
495                         if ((resource->flags & type_mask) != type) {
496                                 continue;
497                         }
498                         /* If it is a subtractive resource ignore it */
499                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
500                                 continue;
501                         }
502                         search(gp, curdev, resource);
503                 }
504         }
505 }
506
507 void dev_set_enabled(device_t dev, int enable)
508 {
509         if (dev->enabled == enable) {
510                 return;
511         }
512         dev->enabled = enable;
513         if (dev->ops && dev->ops->enable) {
514                 dev->ops->enable(dev);
515         }
516         else if (dev->chip_ops && dev->chip_ops->enable_dev) {
517                 dev->chip_ops->enable_dev(dev);
518         }
519 }
520
521 void disable_children(struct bus *bus)
522 {
523         device_t child;
524         for(child = bus->children; child; child = child->sibling) {
525                 int link;
526                 for(link = 0; link < child->links; link++) {
527                         disable_children(&child->link[link]);
528                 }
529                 dev_set_enabled(child, 0);
530         }
531 }