2 * This file is part of the coreboot project.
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)
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.
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.
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
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>
33 * @brief See if a device structure exists for path
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
40 device_t find_dev_path(struct bus *parent, struct device_path *path)
43 for(child = parent->children; child; child = child->sibling) {
44 if (path_eq(path, &child->path)) {
52 * @brief See if a device structure already exists and if not allocate it
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
58 device_t alloc_find_dev(struct bus *parent, struct device_path *path)
61 child = find_dev_path(parent, path);
63 child = alloc_dev(parent, path);
69 * @brief Given a PCI bus and a devfn number, find the device structure
71 * @param bus The bus number
72 * @param devfn a device/function number
73 * @return pointer to the device structure
75 struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
77 struct device *dev, *result;
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)) {
92 * @brief Given a smbus bus and a device number, find the device structure
94 * @param bus The bus number
95 * @param addr a device number
96 * @return pointer to the device structure
98 struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
100 struct device *dev, *result;
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)) {
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
122 struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
128 while (from && (from->vendor != vendor || from->device != device)) {
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
141 struct device *dev_find_class(unsigned int class, struct device *from)
147 while (from && (from->class & 0xffffff00) != class)
153 const char *dev_path(device_t dev)
155 static char buffer[DEVICE_PATH_MAX];
158 memcpy(buffer, "<null>", 7);
161 switch(dev->path.type) {
162 case DEVICE_PATH_ROOT:
163 memcpy(buffer, "Root Device", 12);
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));
171 sprintf(buffer, "PCI: %02x:%02x.%01x",
173 PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
176 case DEVICE_PATH_PNP:
177 sprintf(buffer, "PNP: %04x.%01x",
178 dev->path.u.pnp.port, dev->path.u.pnp.device);
180 case DEVICE_PATH_I2C:
181 sprintf(buffer, "I2C: %02x:%02x",
183 dev->path.u.i2c.device);
185 case DEVICE_PATH_APIC:
186 sprintf(buffer, "APIC: %02x",
187 dev->path.u.apic.apic_id);
189 case DEVICE_PATH_PCI_DOMAIN:
190 sprintf(buffer, "PCI_DOMAIN: %04x",
191 dev->path.u.pci_domain.domain);
193 case DEVICE_PATH_APIC_CLUSTER:
194 sprintf(buffer, "APIC_CLUSTER: %01x",
195 dev->path.u.apic_cluster.cluster);
197 case DEVICE_PATH_CPU:
198 sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
200 case DEVICE_PATH_CPU_BUS:
201 sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
204 printk_err("Unknown device path type: %d\n", dev->path.type);
211 const char *bus_path(struct bus *bus)
213 static char buffer[BUS_PATH_MAX];
214 sprintf(buffer, "%s,%d",
215 dev_path(bus->dev), bus->link);
219 int path_eq(struct device_path *path1, struct device_path *path2)
222 if (path1->type == path2->type) {
223 switch(path1->type) {
224 case DEVICE_PATH_NONE:
226 case DEVICE_PATH_ROOT:
229 case DEVICE_PATH_PCI:
230 equal = (path1->u.pci.devfn == path2->u.pci.devfn);
232 case DEVICE_PATH_PNP:
233 equal = (path1->u.pnp.port == path2->u.pnp.port) &&
234 (path1->u.pnp.device == path2->u.pnp.device);
236 case DEVICE_PATH_I2C:
237 equal = (path1->u.i2c.device == path2->u.i2c.device);
239 case DEVICE_PATH_APIC:
240 equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
242 case DEVICE_PATH_PCI_DOMAIN:
243 equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
245 case DEVICE_PATH_APIC_CLUSTER:
246 equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
248 case DEVICE_PATH_CPU:
249 equal = (path1->u.cpu.id == path2->u.cpu.id);
251 case DEVICE_PATH_CPU_BUS:
252 equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
255 printk_err("Uknown device type: %d\n", path1->type);
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
267 void compact_resources(device_t dev)
269 struct resource *resource;
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);
277 memset(&dev->resource[dev->resources], 0, sizeof(*resource));
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
291 struct resource *probe_resource(device_t dev, unsigned index)
293 struct resource *resource;
295 /* See if there is a resource with the appropriate index */
297 for(i = 0; i < dev->resources; i++) {
298 if (dev->resource[i].index == index) {
299 resource = &dev->resource[i];
307 * See if a resource structure already exists for a given index and if
308 * not allocate one. Then initialize the initialize the resource
310 * @param dev The device to find the resource on
311 * @param index The index of the resource on the device.
313 struct resource *new_resource(device_t dev, unsigned index)
315 struct resource *resource;
317 /* First move all of the free resources to the end */
318 compact_resources(dev);
320 /* See if there is a resource with the appropriate index */
321 resource = probe_resource(dev, index);
323 if (dev->resources == MAX_RESOURCES) {
324 die("MAX_RESOURCES exceeded.");
326 resource = &dev->resource[dev->resources];
327 memset(resource, 0, sizeof(*resource));
330 /* Initialize the resource values */
331 if (!(resource->flags & IORESOURCE_FIXED)) {
337 resource->index = index;
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.
349 struct resource *find_resource(device_t dev, unsigned index)
351 struct resource *resource;
353 /* See if there is a resource with the appropriate index */
354 resource = probe_resource(dev, index);
356 printk_emerg("%s missing resource: %02x\n",
357 dev_path(dev), index);
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
370 static resource_t align_up(resource_t val, unsigned long gran)
373 mask = (1ULL << gran) - 1ULL;
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
385 static resource_t align_down(resource_t val, unsigned long gran)
388 mask = (1ULL << gran) - 1ULL;
394 * @brief Compute the maximum address that is part of a resource
395 * @param resource the resource whose limit is desired
398 resource_t resource_end(struct resource *resource)
400 resource_t base, end;
401 /* get the base address */
402 base = resource->base;
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.
410 /* Get the end (rounded up) */
411 end = base + align_up(resource->size, resource->gran) - 1;
417 * @brief Compute the maximum legal value for resource->base
418 * @param resource the resource whose maximum is desired
419 * @returns the maximum
421 resource_t resource_max(struct resource *resource)
425 max = align_down(resource->limit - resource->size + 1, resource->align);
431 * @brief return the resource type of a resource
432 * @param resource the resource type to decode.
434 const char *resource_type(struct resource *resource)
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":""));
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.
454 void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
456 if (resource->flags & IORESOURCE_STORED) {
458 unsigned long long base, end;
459 base = resource->base;
460 end = resource_end(resource);
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);
466 sprintf(buf, "bus %02x ", dev->link[0].secondary);
470 "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
474 resource->size, resource->gran,
476 resource_type(resource),
481 void search_bus_resources(struct bus *bus,
482 unsigned long type_mask, unsigned long type,
483 resource_search_t search, void *gp)
485 struct device *curdev;
486 for(curdev = bus->children; curdev; curdev = curdev->sibling) {
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) {
496 /* If it is a subtractive resource recurse */
497 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
499 subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
500 search_bus_resources(subbus, type_mask, type, search, gp);
503 search(gp, curdev, resource);
508 void search_global_resources(
509 unsigned long type_mask, unsigned long type,
510 resource_search_t search, void *gp)
512 struct device *curdev;
513 for(curdev = all_devices; curdev; curdev = curdev->next) {
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) {
523 /* If it is a subtractive resource ignore it */
524 if (resource->flags & IORESOURCE_SUBTRACTIVE) {
527 search(gp, curdev, resource);
532 void dev_set_enabled(device_t dev, int enable)
534 if (dev->enabled == enable) {
537 dev->enabled = enable;
538 if (dev->ops && dev->ops->enable) {
539 dev->ops->enable(dev);
541 else if (dev->chip_ops && dev->chip_ops->enable_dev) {
542 dev->chip_ops->enable_dev(dev);
546 void disable_children(struct bus *bus)
549 for(child = bus->children; child; child = child->sibling) {
551 for(link = 0; link < child->links; link++) {
552 disable_children(&child->link[link]);
554 dev_set_enabled(child, 0);