blob: c6ef54128268a63d4c4e94ba067a3edd74c13d24 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
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
Eric Biederman8ca8d762003-04-22 19:02:15 +000025#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000026#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000027#include <device/path.h>
28#include <device/pci.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000029#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000030#include <string.h>
31
Eric Biederman03acab62004-10-14 21:25:53 +000032/**
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 */
40device_t find_dev_path(struct bus *parent, struct device_path *path)
41{
42 device_t child;
Yinghai Lu13f1c2a2005-07-08 02:49:49 +000043 for(child = parent->children; child; child = child->sibling) {
Eric Biederman03acab62004-10-14 21:25:53 +000044 if (path_eq(path, &child->path)) {
45 break;
46 }
47 }
48 return child;
49}
Eric Biedermane9a271e32003-09-02 03:36:25 +000050
51/**
Li-Ta Loe5266692004-03-23 21:28:05 +000052 * @brief See if a device structure already exists and if not allocate it
53 *
Eric Biedermane9a271e32003-09-02 03:36:25 +000054 * @param bus The bus to find the device on
55 * @param path The relative path from the bus to the appropriate device
Li-Ta Loe5266692004-03-23 21:28:05 +000056 * @return pointer to a device structure for the device on bus at path
Eric Biedermane9a271e32003-09-02 03:36:25 +000057 */
58device_t alloc_find_dev(struct bus *parent, struct device_path *path)
59{
60 device_t child;
Eric Biederman03acab62004-10-14 21:25:53 +000061 child = find_dev_path(parent, path);
62 if (!child) {
63 child = alloc_dev(parent, path);
Eric Biedermane9a271e32003-09-02 03:36:25 +000064 }
Eric Biederman03acab62004-10-14 21:25:53 +000065 return child;
Eric Biedermane9a271e32003-09-02 03:36:25 +000066}
Eric Biederman8ca8d762003-04-22 19:02:15 +000067
68/**
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000069 * @brief Given a PCI bus and a devfn number, find the device structure
70 *
Eric Biederman8ca8d762003-04-22 19:02:15 +000071 * @param bus The bus number
72 * @param devfn a device/function number
73 * @return pointer to the device structure
74 */
75struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
76{
Eric Biederman83b991a2003-10-11 06:20:25 +000077 struct device *dev, *result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000078
Eric Biederman83b991a2003-10-11 06:20:25 +000079 result = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +000080 for (dev = all_devices; dev; dev = dev->next) {
Eric Biederman5cd81732004-03-11 15:01:31 +000081 if ((dev->path.type == DEVICE_PATH_PCI) &&
82 (dev->bus->secondary == bus) &&
Eric Biedermane9a271e32003-09-02 03:36:25 +000083 (dev->path.u.pci.devfn == devfn)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000084 result = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +000085 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +000086 }
87 }
Eric Biederman83b991a2003-10-11 06:20:25 +000088 return result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000089}
90
arch import user (historical)98d0d302005-07-06 17:13:46 +000091/**
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 */
98struct 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
Eric Biederman8ca8d762003-04-22 19:02:15 +0000114/** 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 */
122struct 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;
Eric Biederman5cd81732004-03-11 15:01:31 +0000128 while (from && (from->vendor != vendor || from->device != device)) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000129 from = from->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000130 }
Eric Biederman8ca8d762003-04-22 19:02:15 +0000131 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 */
141struct device *dev_find_class(unsigned int class, struct device *from)
142{
143 if (!from)
144 from = all_devices;
145 else
146 from = from->next;
Greg Watson59651692003-12-17 17:39:53 +0000147 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148 from = from->next;
149 return from;
150}
151
Eric Biedermane9a271e32003-09-02 03:36:25 +0000152
153const 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) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000162 case DEVICE_PATH_ROOT:
163 memcpy(buffer, "Root Device", 12);
164 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000165 case DEVICE_PATH_PCI:
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000166#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
Eric Biedermane9a271e32003-09-02 03:36:25 +0000171 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));
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000174#endif
Eric Biedermane9a271e32003-09-02 03:36:25 +0000175 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:
arch import user (historical)98d0d302005-07-06 17:13:46 +0000181 sprintf(buffer, "I2C: %02x:%02x",
182 dev->bus->secondary,
Eric Biedermane9a271e32003-09-02 03:36:25 +0000183 dev->path.u.i2c.device);
184 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000185 case DEVICE_PATH_APIC:
186 sprintf(buffer, "APIC: %02x",
187 dev->path.u.apic.apic_id);
188 break;
Eric Biederman7003ba42004-10-16 06:20:29 +0000189 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;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000197 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;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000203 default:
204 printk_err("Unknown device path type: %d\n", dev->path.type);
205 break;
206 }
207 }
208 return buffer;
209}
210
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000211const 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
Eric Biedermane9a271e32003-09-02 03:36:25 +0000219int 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;
Eric Biederman83b991a2003-10-11 06:20:25 +0000226 case DEVICE_PATH_ROOT:
227 equal = 1;
228 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000229 case DEVICE_PATH_PCI:
Eric Biederman7003ba42004-10-16 06:20:29 +0000230 equal = (path1->u.pci.devfn == path2->u.pci.devfn);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000231 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;
Eric Biederman03acab62004-10-14 21:25:53 +0000239 case DEVICE_PATH_APIC:
240 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
241 break;
Eric Biederman7003ba42004-10-16 06:20:29 +0000242 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;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000248 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;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000254 default:
255 printk_err("Uknown device type: %d\n", path1->type);
256 break;
257 }
258 }
259 return equal;
260}
Eric Biederman5cd81732004-03-11 15:01:31 +0000261
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 */
267void 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
Eric Biederman03acab62004-10-14 21:25:53 +0000284
Eric Biederman5cd81732004-03-11 15:01:31 +0000285/**
Eric Biederman03acab62004-10-14 21:25:53 +0000286 * See if a resource structure already exists for a given index
Eric Biederman5cd81732004-03-11 15:01:31 +0000287 * @param dev The device to find the resource on
288 * @param index The index of the resource on the device.
Eric Biederman03acab62004-10-14 21:25:53 +0000289 * @return the resource if it already exists
Eric Biederman5cd81732004-03-11 15:01:31 +0000290 */
Eric Biederman03acab62004-10-14 21:25:53 +0000291struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000292{
293 struct resource *resource;
294 int i;
Eric Biederman5cd81732004-03-11 15:01:31 +0000295 /* See if there is a resource with the appropriate index */
296 resource = 0;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000297 for(i = 0; i < dev->resources; i++) {
Eric Biederman5cd81732004-03-11 15:01:31 +0000298 if (dev->resource[i].index == index) {
299 resource = &dev->resource[i];
300 break;
301 }
302 }
Eric Biederman03acab62004-10-14 21:25:53 +0000303 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 */
313struct 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);
Eric Biederman5cd81732004-03-11 15:01:31 +0000322 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
Eric Biederman03acab62004-10-14 21:25:53 +0000344/**
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 */
349struct 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 */
370static 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 */
385static 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 */
398resource_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 */
421resource_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/**
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000431 * @brief return the resource type of a resource
432 * @param resource the resource type to decode.
433 */
434const 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/**
Eric Biederman03acab62004-10-14 21:25:53 +0000450 * @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 */
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000454void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000455{
456 if (resource->flags & IORESOURCE_STORED) {
Stefan Reinauer0dff6e32007-10-23 22:17:45 +0000457 char buf[10];
Eric Biederman03acab62004-10-14 21:25:53 +0000458 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) {
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000463#if PCI_BUS_SEGN_BITS
464 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
465#else
Yinghai Lud4b278c2006-10-04 20:46:15 +0000466 sprintf(buf, "bus %02x ", dev->link[0].secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000467#endif
Eric Biederman03acab62004-10-14 21:25:53 +0000468 }
469 printk_debug(
Carl-Daniel Hailfinger52bc9932007-10-16 18:21:22 +0000470 "%s %02x <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
Eric Biederman03acab62004-10-14 21:25:53 +0000471 dev_path(dev),
472 resource->index,
473 base, end,
Carl-Daniel Hailfinger52bc9932007-10-16 18:21:22 +0000474 resource->size, resource->gran,
Eric Biederman03acab62004-10-14 21:25:53 +0000475 buf,
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000476 resource_type(resource),
Eric Biederman03acab62004-10-14 21:25:53 +0000477 comment);
478 }
479}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000480
481void 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
508void 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}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000531
532void 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
546void 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}