blob: fa2adaef4f947aadb00bfbfa0a2fa339df6ff69b [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +00002#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +00003#include <device/path.h>
4#include <device/pci.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00005#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +00006#include <string.h>
7
Eric Biederman03acab62004-10-14 21:25:53 +00008/**
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 */
16device_t find_dev_path(struct bus *parent, struct device_path *path)
17{
18 device_t child;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000019 for(child = parent->children; child; child = child->sibling) {
Eric Biederman03acab62004-10-14 21:25:53 +000020 if (path_eq(path, &child->path)) {
21 break;
22 }
23 }
24 return child;
25}
Eric Biedermane9a271e32003-09-02 03:36:25 +000026
27/**
Li-Ta Loe5266692004-03-23 21:28:05 +000028 * @brief See if a device structure already exists and if not allocate it
29 *
Eric Biedermane9a271e32003-09-02 03:36:25 +000030 * @param bus The bus to find the device on
31 * @param path The relative path from the bus to the appropriate device
Li-Ta Loe5266692004-03-23 21:28:05 +000032 * @return pointer to a device structure for the device on bus at path
Eric Biedermane9a271e32003-09-02 03:36:25 +000033 */
34device_t alloc_find_dev(struct bus *parent, struct device_path *path)
35{
36 device_t child;
Eric Biederman03acab62004-10-14 21:25:53 +000037 child = find_dev_path(parent, path);
38 if (!child) {
39 child = alloc_dev(parent, path);
Eric Biedermane9a271e32003-09-02 03:36:25 +000040 }
Eric Biederman03acab62004-10-14 21:25:53 +000041 return child;
Eric Biedermane9a271e32003-09-02 03:36:25 +000042}
Eric Biederman8ca8d762003-04-22 19:02:15 +000043
44/**
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000045 * @brief Given a PCI bus and a devfn number, find the device structure
46 *
Eric Biederman8ca8d762003-04-22 19:02:15 +000047 * @param bus The bus number
48 * @param devfn a device/function number
49 * @return pointer to the device structure
50 */
51struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
52{
Eric Biederman83b991a2003-10-11 06:20:25 +000053 struct device *dev, *result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000054
Eric Biederman83b991a2003-10-11 06:20:25 +000055 result = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +000056 for (dev = all_devices; dev; dev = dev->next) {
Eric Biederman5cd81732004-03-11 15:01:31 +000057 if ((dev->path.type == DEVICE_PATH_PCI) &&
58 (dev->bus->secondary == bus) &&
Eric Biedermane9a271e32003-09-02 03:36:25 +000059 (dev->path.u.pci.devfn == devfn)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000060 result = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +000061 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +000062 }
63 }
Eric Biederman83b991a2003-10-11 06:20:25 +000064 return result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000065}
66
arch import user (historical)98d0d302005-07-06 17:13:46 +000067/**
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 */
74struct 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
Eric Biederman8ca8d762003-04-22 19:02:15 +000090/** 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 */
98struct 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;
Eric Biederman5cd81732004-03-11 15:01:31 +0000104 while (from && (from->vendor != vendor || from->device != device)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000105 from = from->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000106 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000107 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 */
117struct device *dev_find_class(unsigned int class, struct device *from)
118{
119 if (!from)
120 from = all_devices;
121 else
122 from = from->next;
Greg Watson59651692003-12-17 17:39:53 +0000123 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000124 from = from->next;
125 return from;
126}
127
Eric Biedermane9a271e32003-09-02 03:36:25 +0000128
129const 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) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000138 case DEVICE_PATH_ROOT:
139 memcpy(buffer, "Root Device", 12);
140 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000141 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:
arch import user (historical)98d0d302005-07-06 17:13:46 +0000151 sprintf(buffer, "I2C: %02x:%02x",
152 dev->bus->secondary,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000153 dev->path.u.i2c.device);
154 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000155 case DEVICE_PATH_APIC:
156 sprintf(buffer, "APIC: %02x",
157 dev->path.u.apic.apic_id);
158 break;
Eric Biederman7003ba42004-10-16 06:20:29 +0000159 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;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000167 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;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000173 default:
174 printk_err("Unknown device path type: %d\n", dev->path.type);
175 break;
176 }
177 }
178 return buffer;
179}
180
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000181const char *bus_path(struct bus *bus)
182{
183 static char buffer[BUS_PATH_MAX];
184 sprintf(buffer, "%s,%d",
185 dev_path(bus->dev), bus->link);
186 return buffer;
187}
188
Eric Biedermane9a271e32003-09-02 03:36:25 +0000189int path_eq(struct device_path *path1, struct device_path *path2)
190{
191 int equal = 0;
192 if (path1->type == path2->type) {
193 switch(path1->type) {
194 case DEVICE_PATH_NONE:
195 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000196 case DEVICE_PATH_ROOT:
197 equal = 1;
198 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000199 case DEVICE_PATH_PCI:
Eric Biederman7003ba42004-10-16 06:20:29 +0000200 equal = (path1->u.pci.devfn == path2->u.pci.devfn);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000201 break;
202 case DEVICE_PATH_PNP:
203 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
204 (path1->u.pnp.device == path2->u.pnp.device);
205 break;
206 case DEVICE_PATH_I2C:
207 equal = (path1->u.i2c.device == path2->u.i2c.device);
208 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000209 case DEVICE_PATH_APIC:
210 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
211 break;
Eric Biederman7003ba42004-10-16 06:20:29 +0000212 case DEVICE_PATH_PCI_DOMAIN:
213 equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
214 break;
215 case DEVICE_PATH_APIC_CLUSTER:
216 equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
217 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000218 case DEVICE_PATH_CPU:
219 equal = (path1->u.cpu.id == path2->u.cpu.id);
220 break;
221 case DEVICE_PATH_CPU_BUS:
222 equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
223 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000224 default:
225 printk_err("Uknown device type: %d\n", path1->type);
226 break;
227 }
228 }
229 return equal;
230}
Eric Biederman5cd81732004-03-11 15:01:31 +0000231
232/**
233 * See if we have unused but allocated resource structures.
234 * If so remove the allocation.
235 * @param dev The device to find the resource on
236 */
237void compact_resources(device_t dev)
238{
239 struct resource *resource;
240 int i;
241 /* Move all of the free resources to the end */
242 for(i = 0; i < dev->resources;) {
243 resource = &dev->resource[i];
244 if (!resource->flags) {
245 memmove(resource, resource + 1, dev->resources - i);
246 dev->resources -= 1;
247 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
248 } else {
249 i++;
250 }
251 }
252}
253
Eric Biederman03acab62004-10-14 21:25:53 +0000254
Eric Biederman5cd81732004-03-11 15:01:31 +0000255/**
Eric Biederman03acab62004-10-14 21:25:53 +0000256 * See if a resource structure already exists for a given index
Eric Biederman5cd81732004-03-11 15:01:31 +0000257 * @param dev The device to find the resource on
258 * @param index The index of the resource on the device.
Eric Biederman03acab62004-10-14 21:25:53 +0000259 * @return the resource if it already exists
Eric Biederman5cd81732004-03-11 15:01:31 +0000260 */
Eric Biederman03acab62004-10-14 21:25:53 +0000261struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000262{
263 struct resource *resource;
264 int i;
Eric Biederman5cd81732004-03-11 15:01:31 +0000265 /* See if there is a resource with the appropriate index */
266 resource = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000267 for(i = 0; i < dev->resources; i++) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000268 if (dev->resource[i].index == index) {
269 resource = &dev->resource[i];
270 break;
271 }
272 }
Eric Biederman03acab62004-10-14 21:25:53 +0000273 return resource;
274}
275
276/**
277 * See if a resource structure already exists for a given index and if
278 * not allocate one. Then initialize the initialize the resource
279 * to default values.
280 * @param dev The device to find the resource on
281 * @param index The index of the resource on the device.
282 */
283struct resource *new_resource(device_t dev, unsigned index)
284{
285 struct resource *resource;
286
287 /* First move all of the free resources to the end */
288 compact_resources(dev);
289
290 /* See if there is a resource with the appropriate index */
291 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000292 if (!resource) {
293 if (dev->resources == MAX_RESOURCES) {
294 die("MAX_RESOURCES exceeded.");
295 }
296 resource = &dev->resource[dev->resources];
297 memset(resource, 0, sizeof(*resource));
298 dev->resources++;
299 }
300 /* Initialize the resource values */
301 if (!(resource->flags & IORESOURCE_FIXED)) {
302 resource->flags = 0;
303 resource->base = 0;
304 }
305 resource->size = 0;
306 resource->limit = 0;
307 resource->index = index;
308 resource->align = 0;
309 resource->gran = 0;
310
311 return resource;
312}
313
Eric Biederman03acab62004-10-14 21:25:53 +0000314/**
315 * Return an existing resource structure for a given index.
316 * @param dev The device to find the resource on
317 * @param index The index of the resource on the device.
318 */
319struct resource *find_resource(device_t dev, unsigned index)
320{
321 struct resource *resource;
322
323 /* See if there is a resource with the appropriate index */
324 resource = probe_resource(dev, index);
325 if (!resource) {
326 printk_emerg("%s missing resource: %02x\n",
327 dev_path(dev), index);
328 die("");
329 }
330 return resource;
331}
332
333
334/**
335 * @brief round a number up to the next multiple of gran
336 * @param val the starting value
337 * @param gran granularity we are aligning the number to.
338 * @returns aligned value
339 */
340static resource_t align_up(resource_t val, unsigned long gran)
341{
342 resource_t mask;
343 mask = (1ULL << gran) - 1ULL;
344 val += mask;
345 val &= ~mask;
346 return val;
347}
348
349/**
350 * @brief round a number up to the previous multiple of gran
351 * @param val the starting value
352 * @param gran granularity we are aligning the number to.
353 * @returns aligned value
354 */
355static resource_t align_down(resource_t val, unsigned long gran)
356{
357 resource_t mask;
358 mask = (1ULL << gran) - 1ULL;
359 val &= ~mask;
360 return val;
361}
362
363/**
364 * @brief Compute the maximum address that is part of a resource
365 * @param resource the resource whose limit is desired
366 * @returns the end
367 */
368resource_t resource_end(struct resource *resource)
369{
370 resource_t base, end;
371 /* get the base address */
372 base = resource->base;
373
374 /* For a non bridge resource granularity and alignment are the same.
375 * For a bridge resource align is the largest needed alignment below
376 * the bridge. While the granularity is simply how many low bits of the
377 * address cannot be set.
378 */
379
380 /* Get the end (rounded up) */
381 end = base + align_up(resource->size, resource->gran) - 1;
382
383 return end;
384}
385
386/**
387 * @brief Compute the maximum legal value for resource->base
388 * @param resource the resource whose maximum is desired
389 * @returns the maximum
390 */
391resource_t resource_max(struct resource *resource)
392{
393 resource_t max;
394
395 max = align_down(resource->limit - resource->size + 1, resource->align);
396
397 return max;
398}
399
400/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000401 * @brief return the resource type of a resource
402 * @param resource the resource type to decode.
403 */
404const char *resource_type(struct resource *resource)
405{
406 static char buffer[RESOURCE_TYPE_MAX];
407 sprintf(buffer, "%s%s%s%s",
408 ((resource->flags & IORESOURCE_READONLY)? "ro": ""),
409 ((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
410 ((resource->flags == 0)? "unused":
411 (resource->flags & IORESOURCE_IO)? "io":
412 (resource->flags & IORESOURCE_DRQ)? "drq":
413 (resource->flags & IORESOURCE_IRQ)? "irq":
414 (resource->flags & IORESOURCE_MEM)? "mem":"??????"),
415 ((resource->flags & IORESOURCE_PCI64)?"64":""));
416 return buffer;
417}
418
419/**
Eric Biederman03acab62004-10-14 21:25:53 +0000420 * @brief print the resource that was just stored.
421 * @param dev the device the stored resorce lives on
422 * @param resource the resource that was just stored.
423 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000424void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000425{
426 if (resource->flags & IORESOURCE_STORED) {
427 unsigned char buf[10];
428 unsigned long long base, end;
429 base = resource->base;
430 end = resource_end(resource);
431 buf[0] = '\0';
432 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
433 sprintf(buf, "bus %d ", dev->link[0].secondary);
434 }
435 printk_debug(
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000436 "%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000437 dev_path(dev),
438 resource->index,
439 base, end,
440 buf,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000441 resource_type(resource),
Eric Biederman03acab62004-10-14 21:25:53 +0000442 comment);
443 }
444}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000445
446void search_bus_resources(struct bus *bus,
447 unsigned long type_mask, unsigned long type,
448 resource_search_t search, void *gp)
449{
450 struct device *curdev;
451 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
452 int i;
453 /* Ignore disabled devices */
454 if (!curdev->have_resources) continue;
455 for(i = 0; i < curdev->resources; i++) {
456 struct resource *resource = &curdev->resource[i];
457 /* If it isn't the right kind of resource ignore it */
458 if ((resource->flags & type_mask) != type) {
459 continue;
460 }
461 /* If it is a subtractive resource recurse */
462 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
463 struct bus * subbus;
464 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
465 search_bus_resources(subbus, type_mask, type, search, gp);
466 continue;
467 }
468 search(gp, curdev, resource);
469 }
470 }
471}
472
473void search_global_resources(
474 unsigned long type_mask, unsigned long type,
475 resource_search_t search, void *gp)
476{
477 struct device *curdev;
478 for(curdev = all_devices; curdev; curdev = curdev->next) {
479 int i;
480 /* Ignore disabled devices */
481 if (!curdev->have_resources) continue;
482 for(i = 0; i < curdev->resources; i++) {
483 struct resource *resource = &curdev->resource[i];
484 /* If it isn't the right kind of resource ignore it */
485 if ((resource->flags & type_mask) != type) {
486 continue;
487 }
488 /* If it is a subtractive resource ignore it */
489 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
490 continue;
491 }
492 search(gp, curdev, resource);
493 }
494 }
495}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000496
497void dev_set_enabled(device_t dev, int enable)
498{
499 if (dev->enabled == enable) {
500 return;
501 }
502 dev->enabled = enable;
503 if (dev->ops && dev->ops->enable) {
504 dev->ops->enable(dev);
505 }
506 else if (dev->chip_ops && dev->chip_ops->enable_dev) {
507 dev->chip_ops->enable_dev(dev);
508 }
509}
510
511void disable_children(struct bus *bus)
512{
513 device_t child;
514 for(child = bus->children; child; child = child->sibling) {
515 int link;
516 for(link = 0; link < child->links; link++) {
517 disable_children(&child->link[link]);
518 }
519 dev_set_enabled(child, 0);
520 }
521}