Revision: linuxbios@linuxbios.org--devel/freebios--devel--2.0--patch-30
[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                         sprintf(buffer, "PCI: %02x:%02x.%01x",
143                                 dev->bus->secondary, 
144                                 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
145                         break;
146                 case DEVICE_PATH_PNP:
147                         sprintf(buffer, "PNP: %04x.%01x",
148                                 dev->path.u.pnp.port, dev->path.u.pnp.device);
149                         break;
150                 case DEVICE_PATH_I2C:
151                         sprintf(buffer, "I2C: %02x:%02x",
152                                 dev->bus->secondary,
153                                 dev->path.u.i2c.device);
154                         break;
155                 case DEVICE_PATH_APIC:
156                         sprintf(buffer, "APIC: %02x",
157                                 dev->path.u.apic.apic_id);
158                         break;
159                 case DEVICE_PATH_PCI_DOMAIN:
160                         sprintf(buffer, "PCI_DOMAIN: %04x",
161                                 dev->path.u.pci_domain.domain);
162                         break;
163                 case DEVICE_PATH_APIC_CLUSTER:
164                         sprintf(buffer, "APIC_CLUSTER: %01x",
165                                 dev->path.u.apic_cluster.cluster);
166                         break;
167                 case DEVICE_PATH_CPU:
168                         sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
169                         break;
170                 case DEVICE_PATH_CPU_BUS:
171                         sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
172                         break;
173                 default:
174                         printk_err("Unknown device path type: %d\n", dev->path.type);
175                         break;
176                 }
177         }
178         return buffer;
179 }
180
181 int path_eq(struct device_path *path1, struct device_path *path2)
182 {
183         int equal = 0;
184         if (path1->type == path2->type) {
185                 switch(path1->type) {
186                 case DEVICE_PATH_NONE:
187                         break;
188                 case DEVICE_PATH_ROOT:
189                         equal = 1;
190                         break;
191                 case DEVICE_PATH_PCI:
192                         equal = (path1->u.pci.devfn == path2->u.pci.devfn);
193                         break;
194                 case DEVICE_PATH_PNP:
195                         equal = (path1->u.pnp.port == path2->u.pnp.port) &&
196                                 (path1->u.pnp.device == path2->u.pnp.device);
197                         break;
198                 case DEVICE_PATH_I2C:
199                         equal = (path1->u.i2c.device == path2->u.i2c.device);
200                         break;
201                 case DEVICE_PATH_APIC:
202                         equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
203                         break;
204                 case DEVICE_PATH_PCI_DOMAIN:
205                         equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
206                         break;
207                 case DEVICE_PATH_APIC_CLUSTER:
208                         equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
209                         break;
210                 case DEVICE_PATH_CPU:
211                         equal = (path1->u.cpu.id == path2->u.cpu.id);
212                         break;
213                 case DEVICE_PATH_CPU_BUS:
214                         equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
215                         break;
216                 default:
217                         printk_err("Uknown device type: %d\n", path1->type);
218                         break;
219                 }
220         }
221         return equal;
222 }
223
224 /**
225  * See if we have unused but allocated resource structures.
226  * If so remove the allocation.
227  * @param dev The device to find the resource on
228  */
229 void compact_resources(device_t dev)
230 {
231         struct resource *resource;
232         int i;
233         /* Move all of the free resources to the end */
234         for(i = 0; i < dev->resources;) {
235                 resource = &dev->resource[i];
236                 if (!resource->flags) {
237                         memmove(resource, resource + 1, dev->resources - i);
238                         dev->resources -= 1;
239                         memset(&dev->resource[dev->resources], 0, sizeof(*resource));
240                 } else {
241                         i++;
242                 }
243         }
244 }
245
246
247 /**
248  * See if a resource structure already exists for a given index
249  * @param dev The device to find the resource on
250  * @param index  The index of the resource on the device.
251  * @return the resource if it already exists
252  */
253 struct resource *probe_resource(device_t dev, unsigned index)
254 {
255         struct resource *resource;
256         int i;
257         /* See if there is a resource with the appropriate index */
258         resource = 0;
259         for(i = 0; i < dev->resources; i++) {
260                 if (dev->resource[i].index == index) {
261                         resource = &dev->resource[i];
262                         break;
263                 }
264         }
265         return resource;
266 }
267
268 /**
269  * See if a resource structure already exists for a given index and if
270  * not allocate one.  Then initialize the initialize the resource
271  * to default values.
272  * @param dev The device to find the resource on
273  * @param index  The index of the resource on the device.
274  */
275 struct resource *new_resource(device_t dev, unsigned index)
276 {
277         struct resource *resource;
278
279         /* First move all of the free resources to the end */
280         compact_resources(dev);
281
282         /* See if there is a resource with the appropriate index */
283         resource = probe_resource(dev, index);
284         if (!resource) {
285                 if (dev->resources == MAX_RESOURCES) {
286                         die("MAX_RESOURCES exceeded.");
287                 }
288                 resource = &dev->resource[dev->resources];
289                 memset(resource, 0, sizeof(*resource));
290                 dev->resources++;
291         }
292         /* Initialize the resource values */
293         if (!(resource->flags & IORESOURCE_FIXED)) {
294                 resource->flags = 0;
295                 resource->base = 0;
296         }
297         resource->size  = 0;
298         resource->limit = 0;
299         resource->index = index;
300         resource->align = 0;
301         resource->gran  = 0;
302
303         return resource;
304 }
305
306 /**
307  * Return an existing resource structure for a given index.
308  * @param dev The device to find the resource on
309  * @param index  The index of the resource on the device.
310  */
311 struct resource *find_resource(device_t dev, unsigned index)
312 {
313         struct resource *resource;
314
315         /* See if there is a resource with the appropriate index */
316         resource = probe_resource(dev, index);
317         if (!resource) {
318                 printk_emerg("%s missing resource: %02x\n",
319                         dev_path(dev), index);
320                 die("");
321         }
322         return resource;
323 }
324
325
326 /**
327  * @brief round a number up to the next multiple of gran
328  * @param val the starting value
329  * @param gran granularity we are aligning the number to.
330  * @returns aligned value
331  */
332 static resource_t align_up(resource_t val, unsigned long gran)
333 {
334         resource_t mask;
335         mask = (1ULL << gran) - 1ULL;
336         val += mask;
337         val &= ~mask;
338         return val;
339 }
340
341 /**
342  * @brief round a number up to the previous multiple of gran
343  * @param val the starting value
344  * @param gran granularity we are aligning the number to.
345  * @returns aligned value
346  */
347 static resource_t align_down(resource_t val, unsigned long gran)
348 {
349         resource_t mask;
350         mask = (1ULL << gran) - 1ULL;
351         val &= ~mask;
352         return val;
353 }
354
355 /**
356  * @brief Compute the maximum address that is part of a resource
357  * @param resource the resource whose limit is desired
358  * @returns the end
359  */
360 resource_t resource_end(struct resource *resource)
361 {
362         resource_t base, end;
363         /* get the base address */
364         base = resource->base;
365
366         /* For a non bridge resource granularity and alignment are the same.
367          * For a bridge resource align is the largest needed alignment below
368          * the bridge.  While the granularity is simply how many low bits of the
369          * address cannot be set.
370          */
371         
372         /* Get the end (rounded up) */
373         end = base + align_up(resource->size, resource->gran) - 1;
374
375         return end;
376 }
377
378 /**
379  * @brief Compute the maximum legal value for resource->base
380  * @param resource the resource whose maximum is desired
381  * @returns the maximum
382  */
383 resource_t resource_max(struct resource *resource)
384 {
385         resource_t max;
386
387         max = align_down(resource->limit - resource->size + 1, resource->align);
388
389         return max;
390 }
391
392 /**
393  * @brief print the resource that was just stored.
394  * @param dev the device the stored resorce lives on
395  * @param resource the resource that was just stored.
396  */
397 void report_resource_stored(device_t dev, struct resource *resource,
398                             const char *comment)
399 {
400         if (resource->flags & IORESOURCE_STORED) {
401                 unsigned char buf[10];
402                 unsigned long long base, end;
403                 base = resource->base;
404                 end = resource_end(resource);
405                 buf[0] = '\0';
406                 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
407                         sprintf(buf, "bus %d ", dev->link[0].secondary);
408                 }
409                 printk_debug(
410                         "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s%s\n",
411                         dev_path(dev),
412                         resource->index,
413                         base, end,
414                         buf,
415                         (resource->flags & IORESOURCE_PREFETCH) ? "pref" : "",
416                         (resource->flags & IORESOURCE_IO)? "io":
417                         (resource->flags & IORESOURCE_DRQ)? "drq":
418                         (resource->flags & IORESOURCE_IRQ)? "irq":
419                         (resource->flags & IORESOURCE_READONLY)? "rom":
420                         (resource->flags & IORESOURCE_MEM)? "mem":
421                         "????",
422                         comment);
423         }
424 }
425
426 void search_bus_resources(struct bus *bus,
427         unsigned long type_mask, unsigned long type,
428         resource_search_t search, void *gp)
429 {
430         struct device *curdev;
431         for(curdev = bus->children; curdev; curdev = curdev->sibling) {
432                 int i;
433                 /* Ignore disabled devices */
434                 if (!curdev->have_resources) continue;
435                 for(i = 0; i < curdev->resources; i++) {
436                         struct resource *resource = &curdev->resource[i];
437                         /* If it isn't the right kind of resource ignore it */
438                         if ((resource->flags & type_mask) != type) {
439                                 continue;
440                         }
441                         /* If it is a subtractive resource recurse */
442                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
443                                 struct bus * subbus;
444                                 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
445                                 search_bus_resources(subbus, type_mask, type, search, gp);
446                                 continue;
447                         }
448                         search(gp, curdev, resource);
449                 }
450         }
451 }
452
453 void search_global_resources(
454         unsigned long type_mask, unsigned long type,
455         resource_search_t search, void *gp)
456 {
457         struct device *curdev;
458         for(curdev = all_devices; curdev; curdev = curdev->next) {
459                 int i;
460                 /* Ignore disabled devices */
461                 if (!curdev->have_resources) continue;
462                 for(i = 0; i < curdev->resources; i++) {
463                         struct resource *resource = &curdev->resource[i];
464                         /* If it isn't the right kind of resource ignore it */
465                         if ((resource->flags & type_mask) != type) {
466                                 continue;
467                         }
468                         /* If it is a subtractive resource ignore it */
469                         if (resource->flags & IORESOURCE_SUBTRACTIVE) {
470                                 continue;
471                         }
472                         search(gp, curdev, resource);
473                 }
474         }
475 }