Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 7e61e45 | 2008-01-18 10:35:56 +0000 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Uwe Hermann | b80dbf0 | 2007-04-22 19:08:13 +0000 | [diff] [blame] | 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 Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 25 | #include <console/console.h> |
Eric Biederman | 5899fd8 | 2003-04-24 06:25:08 +0000 | [diff] [blame] | 26 | #include <device/device.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 27 | #include <device/path.h> |
| 28 | #include <device/pci.h> |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 29 | #include <device/resource.h> |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 30 | #include <string.h> |
| 31 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 32 | /** |
| 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 | */ |
| 40 | device_t find_dev_path(struct bus *parent, struct device_path *path) |
| 41 | { |
| 42 | device_t child; |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 43 | for(child = parent->children; child; child = child->sibling) { |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 44 | if (path_eq(path, &child->path)) { |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | return child; |
| 49 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 50 | |
| 51 | /** |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 52 | * @brief See if a device structure already exists and if not allocate it |
| 53 | * |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 54 | * @param bus The bus to find the device on |
| 55 | * @param path The relative path from the bus to the appropriate device |
Li-Ta Lo | e526669 | 2004-03-23 21:28:05 +0000 | [diff] [blame] | 56 | * @return pointer to a device structure for the device on bus at path |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 57 | */ |
| 58 | device_t alloc_find_dev(struct bus *parent, struct device_path *path) |
| 59 | { |
| 60 | device_t child; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 61 | child = find_dev_path(parent, path); |
| 62 | if (!child) { |
| 63 | child = alloc_dev(parent, path); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 64 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 65 | return child; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 66 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 69 | * @brief Given a PCI bus and a devfn number, find the device structure |
| 70 | * |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | * @param bus The bus number |
| 72 | * @param devfn a device/function number |
| 73 | * @return pointer to the device structure |
| 74 | */ |
| 75 | struct device *dev_find_slot(unsigned int bus, unsigned int devfn) |
| 76 | { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 77 | struct device *dev, *result; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 78 | |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 79 | result = 0; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 80 | for (dev = all_devices; dev; dev = dev->next) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 81 | if ((dev->path.type == DEVICE_PATH_PCI) && |
| 82 | (dev->bus->secondary == bus) && |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 83 | (dev->path.pci.devfn == devfn)) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 84 | result = dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 85 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 88 | return result; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 89 | } |
| 90 | |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 91 | /** |
| 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 | */ |
| 98 | struct 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) && |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 106 | (dev->path.i2c.device == addr)) { |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 107 | result = dev; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | return result; |
| 112 | } |
| 113 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 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 |
| 121 | */ |
| 122 | struct 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 Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 128 | while (from && (from->vendor != vendor || from->device != device)) { |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 129 | from = from->next; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 130 | } |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 131 | 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 | */ |
| 141 | struct 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 Watson | 5965169 | 2003-12-17 17:39:53 +0000 | [diff] [blame] | 147 | while (from && (from->class & 0xffffff00) != class) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 148 | from = from->next; |
| 149 | return from; |
| 150 | } |
| 151 | |
Myles Watson | 8f6354b | 2009-10-29 21:27:43 +0000 | [diff] [blame] | 152 | /* Warning: This function uses a static buffer. Don't call it more than once |
| 153 | * from the same print statement! */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 154 | |
| 155 | const char *dev_path(device_t dev) |
| 156 | { |
| 157 | static char buffer[DEVICE_PATH_MAX]; |
| 158 | buffer[0] = '\0'; |
| 159 | if (!dev) { |
| 160 | memcpy(buffer, "<null>", 7); |
| 161 | } |
| 162 | else { |
| 163 | switch(dev->path.type) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 164 | case DEVICE_PATH_ROOT: |
| 165 | memcpy(buffer, "Root Device", 12); |
| 166 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 167 | case DEVICE_PATH_PCI: |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 168 | #if CONFIG_PCI_BUS_SEGN_BITS |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 169 | sprintf(buffer, "PCI: %04x:%02x:%02x.%01x", |
| 170 | dev->bus->secondary>>8, dev->bus->secondary & 0xff, |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 171 | PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn)); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 172 | #else |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 173 | sprintf(buffer, "PCI: %02x:%02x.%01x", |
| 174 | dev->bus->secondary, |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 175 | PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn)); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 176 | #endif |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 177 | break; |
| 178 | case DEVICE_PATH_PNP: |
| 179 | sprintf(buffer, "PNP: %04x.%01x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 180 | dev->path.pnp.port, dev->path.pnp.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 181 | break; |
| 182 | case DEVICE_PATH_I2C: |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 183 | sprintf(buffer, "I2C: %02x:%02x", |
| 184 | dev->bus->secondary, |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 185 | dev->path.i2c.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 186 | break; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 187 | case DEVICE_PATH_APIC: |
| 188 | sprintf(buffer, "APIC: %02x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 189 | dev->path.apic.apic_id); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 190 | break; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 191 | case DEVICE_PATH_PCI_DOMAIN: |
| 192 | sprintf(buffer, "PCI_DOMAIN: %04x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 193 | dev->path.pci_domain.domain); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 194 | break; |
| 195 | case DEVICE_PATH_APIC_CLUSTER: |
| 196 | sprintf(buffer, "APIC_CLUSTER: %01x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 197 | dev->path.apic_cluster.cluster); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 198 | break; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 199 | case DEVICE_PATH_CPU: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 200 | sprintf(buffer, "CPU: %02x", dev->path.cpu.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 201 | break; |
| 202 | case DEVICE_PATH_CPU_BUS: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 203 | sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 204 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 205 | default: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 206 | printk(BIOS_ERR, "Unknown device path type: %d\n", dev->path.type); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 207 | break; |
| 208 | } |
| 209 | } |
| 210 | return buffer; |
| 211 | } |
| 212 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 213 | const char *bus_path(struct bus *bus) |
| 214 | { |
| 215 | static char buffer[BUS_PATH_MAX]; |
Myles Watson | 8f6354b | 2009-10-29 21:27:43 +0000 | [diff] [blame] | 216 | sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 217 | return buffer; |
| 218 | } |
| 219 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 220 | int path_eq(struct device_path *path1, struct device_path *path2) |
| 221 | { |
| 222 | int equal = 0; |
| 223 | if (path1->type == path2->type) { |
| 224 | switch(path1->type) { |
| 225 | case DEVICE_PATH_NONE: |
| 226 | break; |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 227 | case DEVICE_PATH_ROOT: |
| 228 | equal = 1; |
| 229 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 230 | case DEVICE_PATH_PCI: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 231 | equal = (path1->pci.devfn == path2->pci.devfn); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 232 | break; |
| 233 | case DEVICE_PATH_PNP: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 234 | equal = (path1->pnp.port == path2->pnp.port) && |
| 235 | (path1->pnp.device == path2->pnp.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 236 | break; |
| 237 | case DEVICE_PATH_I2C: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 238 | equal = (path1->i2c.device == path2->i2c.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 239 | break; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 240 | case DEVICE_PATH_APIC: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 241 | equal = (path1->apic.apic_id == path2->apic.apic_id); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 242 | break; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 243 | case DEVICE_PATH_PCI_DOMAIN: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 244 | equal = (path1->pci_domain.domain == path2->pci_domain.domain); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 245 | break; |
| 246 | case DEVICE_PATH_APIC_CLUSTER: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 247 | equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 248 | break; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 249 | case DEVICE_PATH_CPU: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 250 | equal = (path1->cpu.id == path2->cpu.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 251 | break; |
| 252 | case DEVICE_PATH_CPU_BUS: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 253 | equal = (path1->cpu_bus.id == path2->cpu_bus.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 254 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 255 | default: |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 256 | printk(BIOS_ERR, "Uknown device type: %d\n", path1->type); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | return equal; |
| 261 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * See if we have unused but allocated resource structures. |
| 265 | * If so remove the allocation. |
| 266 | * @param dev The device to find the resource on |
| 267 | */ |
| 268 | void compact_resources(device_t dev) |
| 269 | { |
| 270 | struct resource *resource; |
| 271 | int i; |
| 272 | /* Move all of the free resources to the end */ |
| 273 | for(i = 0; i < dev->resources;) { |
| 274 | resource = &dev->resource[i]; |
| 275 | if (!resource->flags) { |
Myles Watson | f9f4eaf | 2009-05-28 21:57:11 +0000 | [diff] [blame] | 276 | memmove(resource, resource + 1, (dev->resources - i) * |
| 277 | sizeof(*resource)); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 278 | dev->resources -= 1; |
| 279 | memset(&dev->resource[dev->resources], 0, sizeof(*resource)); |
| 280 | } else { |
| 281 | i++; |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 286 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 287 | /** |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 288 | * See if a resource structure already exists for a given index |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 289 | * @param dev The device to find the resource on |
| 290 | * @param index The index of the resource on the device. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 291 | * @return the resource if it already exists |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 292 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 293 | struct resource *probe_resource(device_t dev, unsigned index) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 294 | { |
| 295 | struct resource *resource; |
| 296 | int i; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 297 | /* See if there is a resource with the appropriate index */ |
| 298 | resource = 0; |
Eric Biederman | b78c197 | 2004-10-14 20:54:17 +0000 | [diff] [blame] | 299 | for(i = 0; i < dev->resources; i++) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 300 | if (dev->resource[i].index == index) { |
| 301 | resource = &dev->resource[i]; |
| 302 | break; |
| 303 | } |
| 304 | } |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 305 | return resource; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * See if a resource structure already exists for a given index and if |
| 310 | * not allocate one. Then initialize the initialize the resource |
| 311 | * to default values. |
| 312 | * @param dev The device to find the resource on |
| 313 | * @param index The index of the resource on the device. |
| 314 | */ |
| 315 | struct resource *new_resource(device_t dev, unsigned index) |
| 316 | { |
| 317 | struct resource *resource; |
| 318 | |
| 319 | /* First move all of the free resources to the end */ |
| 320 | compact_resources(dev); |
| 321 | |
| 322 | /* See if there is a resource with the appropriate index */ |
| 323 | resource = probe_resource(dev, index); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 324 | if (!resource) { |
| 325 | if (dev->resources == MAX_RESOURCES) { |
| 326 | die("MAX_RESOURCES exceeded."); |
| 327 | } |
| 328 | resource = &dev->resource[dev->resources]; |
| 329 | memset(resource, 0, sizeof(*resource)); |
| 330 | dev->resources++; |
| 331 | } |
| 332 | /* Initialize the resource values */ |
| 333 | if (!(resource->flags & IORESOURCE_FIXED)) { |
| 334 | resource->flags = 0; |
| 335 | resource->base = 0; |
| 336 | } |
| 337 | resource->size = 0; |
| 338 | resource->limit = 0; |
| 339 | resource->index = index; |
| 340 | resource->align = 0; |
| 341 | resource->gran = 0; |
| 342 | |
| 343 | return resource; |
| 344 | } |
| 345 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 346 | /** |
| 347 | * Return an existing resource structure for a given index. |
| 348 | * @param dev The device to find the resource on |
| 349 | * @param index The index of the resource on the device. |
| 350 | */ |
| 351 | struct resource *find_resource(device_t dev, unsigned index) |
| 352 | { |
| 353 | struct resource *resource; |
| 354 | |
| 355 | /* See if there is a resource with the appropriate index */ |
| 356 | resource = probe_resource(dev, index); |
| 357 | if (!resource) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 358 | printk(BIOS_EMERG, "%s missing resource: %02x\n", |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 359 | dev_path(dev), index); |
| 360 | die(""); |
| 361 | } |
| 362 | return resource; |
| 363 | } |
| 364 | |
| 365 | |
| 366 | /** |
| 367 | * @brief round a number up to the next multiple of gran |
| 368 | * @param val the starting value |
| 369 | * @param gran granularity we are aligning the number to. |
| 370 | * @returns aligned value |
| 371 | */ |
| 372 | static resource_t align_up(resource_t val, unsigned long gran) |
| 373 | { |
| 374 | resource_t mask; |
| 375 | mask = (1ULL << gran) - 1ULL; |
| 376 | val += mask; |
| 377 | val &= ~mask; |
| 378 | return val; |
| 379 | } |
| 380 | |
| 381 | /** |
| 382 | * @brief round a number up to the previous multiple of gran |
| 383 | * @param val the starting value |
| 384 | * @param gran granularity we are aligning the number to. |
| 385 | * @returns aligned value |
| 386 | */ |
| 387 | static resource_t align_down(resource_t val, unsigned long gran) |
| 388 | { |
| 389 | resource_t mask; |
| 390 | mask = (1ULL << gran) - 1ULL; |
| 391 | val &= ~mask; |
| 392 | return val; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * @brief Compute the maximum address that is part of a resource |
| 397 | * @param resource the resource whose limit is desired |
| 398 | * @returns the end |
| 399 | */ |
| 400 | resource_t resource_end(struct resource *resource) |
| 401 | { |
| 402 | resource_t base, end; |
| 403 | /* get the base address */ |
| 404 | base = resource->base; |
| 405 | |
| 406 | /* For a non bridge resource granularity and alignment are the same. |
| 407 | * For a bridge resource align is the largest needed alignment below |
| 408 | * the bridge. While the granularity is simply how many low bits of the |
| 409 | * address cannot be set. |
| 410 | */ |
| 411 | |
| 412 | /* Get the end (rounded up) */ |
| 413 | end = base + align_up(resource->size, resource->gran) - 1; |
| 414 | |
| 415 | return end; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @brief Compute the maximum legal value for resource->base |
| 420 | * @param resource the resource whose maximum is desired |
| 421 | * @returns the maximum |
| 422 | */ |
| 423 | resource_t resource_max(struct resource *resource) |
| 424 | { |
| 425 | resource_t max; |
| 426 | |
| 427 | max = align_down(resource->limit - resource->size + 1, resource->align); |
| 428 | |
| 429 | return max; |
| 430 | } |
| 431 | |
| 432 | /** |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 433 | * @brief return the resource type of a resource |
| 434 | * @param resource the resource type to decode. |
| 435 | */ |
| 436 | const char *resource_type(struct resource *resource) |
| 437 | { |
| 438 | static char buffer[RESOURCE_TYPE_MAX]; |
| 439 | sprintf(buffer, "%s%s%s%s", |
| 440 | ((resource->flags & IORESOURCE_READONLY)? "ro": ""), |
| 441 | ((resource->flags & IORESOURCE_PREFETCH)? "pref":""), |
| 442 | ((resource->flags == 0)? "unused": |
| 443 | (resource->flags & IORESOURCE_IO)? "io": |
| 444 | (resource->flags & IORESOURCE_DRQ)? "drq": |
| 445 | (resource->flags & IORESOURCE_IRQ)? "irq": |
| 446 | (resource->flags & IORESOURCE_MEM)? "mem":"??????"), |
| 447 | ((resource->flags & IORESOURCE_PCI64)?"64":"")); |
| 448 | return buffer; |
| 449 | } |
| 450 | |
| 451 | /** |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 452 | * @brief print the resource that was just stored. |
| 453 | * @param dev the device the stored resorce lives on |
| 454 | * @param resource the resource that was just stored. |
| 455 | */ |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 456 | void report_resource_stored(device_t dev, struct resource *resource, const char *comment) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 457 | { |
| 458 | if (resource->flags & IORESOURCE_STORED) { |
Stefan Reinauer | 0dff6e3 | 2007-10-23 22:17:45 +0000 | [diff] [blame] | 459 | char buf[10]; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 460 | unsigned long long base, end; |
| 461 | base = resource->base; |
| 462 | end = resource_end(resource); |
| 463 | buf[0] = '\0'; |
| 464 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 465 | #if CONFIG_PCI_BUS_SEGN_BITS |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 466 | sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff); |
| 467 | #else |
Yinghai Lu | d4b278c | 2006-10-04 20:46:15 +0000 | [diff] [blame] | 468 | sprintf(buf, "bus %02x ", dev->link[0].secondary); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 469 | #endif |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 470 | } |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 471 | printk(BIOS_DEBUG, |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 472 | "%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n", |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 473 | dev_path(dev), |
| 474 | resource->index, |
| 475 | base, end, |
Carl-Daniel Hailfinger | 52bc993 | 2007-10-16 18:21:22 +0000 | [diff] [blame] | 476 | resource->size, resource->gran, |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 477 | buf, |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 478 | resource_type(resource), |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 479 | comment); |
| 480 | } |
| 481 | } |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 482 | |
| 483 | void search_bus_resources(struct bus *bus, |
| 484 | unsigned long type_mask, unsigned long type, |
| 485 | resource_search_t search, void *gp) |
| 486 | { |
| 487 | struct device *curdev; |
| 488 | for(curdev = bus->children; curdev; curdev = curdev->sibling) { |
| 489 | int i; |
| 490 | /* Ignore disabled devices */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 491 | if (!curdev->enabled) continue; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 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 recurse */ |
| 499 | if (resource->flags & IORESOURCE_SUBTRACTIVE) { |
| 500 | struct bus * subbus; |
| 501 | subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)]; |
| 502 | search_bus_resources(subbus, type_mask, type, search, gp); |
| 503 | continue; |
| 504 | } |
| 505 | search(gp, curdev, resource); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | void search_global_resources( |
| 511 | unsigned long type_mask, unsigned long type, |
| 512 | resource_search_t search, void *gp) |
| 513 | { |
| 514 | struct device *curdev; |
| 515 | for(curdev = all_devices; curdev; curdev = curdev->next) { |
| 516 | int i; |
| 517 | /* Ignore disabled devices */ |
Myles Watson | 29cc9ed | 2009-07-02 18:56:24 +0000 | [diff] [blame] | 518 | if (!curdev->enabled) continue; |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 519 | for(i = 0; i < curdev->resources; i++) { |
| 520 | struct resource *resource = &curdev->resource[i]; |
| 521 | /* If it isn't the right kind of resource ignore it */ |
| 522 | if ((resource->flags & type_mask) != type) { |
| 523 | continue; |
| 524 | } |
| 525 | /* If it is a subtractive resource ignore it */ |
| 526 | if (resource->flags & IORESOURCE_SUBTRACTIVE) { |
| 527 | continue; |
| 528 | } |
| 529 | search(gp, curdev, resource); |
| 530 | } |
| 531 | } |
| 532 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 533 | |
| 534 | void dev_set_enabled(device_t dev, int enable) |
| 535 | { |
| 536 | if (dev->enabled == enable) { |
| 537 | return; |
| 538 | } |
| 539 | dev->enabled = enable; |
| 540 | if (dev->ops && dev->ops->enable) { |
| 541 | dev->ops->enable(dev); |
| 542 | } |
| 543 | else if (dev->chip_ops && dev->chip_ops->enable_dev) { |
| 544 | dev->chip_ops->enable_dev(dev); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | void disable_children(struct bus *bus) |
| 549 | { |
| 550 | device_t child; |
| 551 | for(child = bus->children; child; child = child->sibling) { |
| 552 | int link; |
| 553 | for(link = 0; link < child->links; link++) { |
| 554 | disable_children(&child->link[link]); |
| 555 | } |
| 556 | dev_set_enabled(child, 0); |
| 557 | } |
| 558 | } |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 559 | |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 560 | static void resource_tree(struct device *root, int debug_level, int depth) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 561 | { |
| 562 | int i = 0, link = 0; |
| 563 | struct device *child; |
| 564 | char indent[30]; /* If your tree has more levels, it's wrong. */ |
| 565 | |
| 566 | for (i = 0; i < depth + 1 && i < 29; i++) |
| 567 | indent[i] = ' '; |
| 568 | indent[i] = '\0'; |
| 569 | |
Myles Watson | 8f6354b | 2009-10-29 21:27:43 +0000 | [diff] [blame] | 570 | do_printk(BIOS_DEBUG, "%s%s links %x child on link 0", indent, |
| 571 | dev_path(root), root->links); |
| 572 | do_printk(BIOS_DEBUG, " %s\n", root->link[0].children ? |
| 573 | dev_path(root->link[0].children) : "NULL"); |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 574 | for (i = 0; i < root->resources; i++) { |
| 575 | do_printk(BIOS_DEBUG, |
| 576 | "%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n", |
| 577 | indent, dev_path(root), root->resource[i].base, |
| 578 | root->resource[i].size, root->resource[i].align, |
| 579 | root->resource[i].gran, root->resource[i].limit, |
| 580 | root->resource[i].flags, root->resource[i].index); |
| 581 | } |
| 582 | |
| 583 | for (link = 0; link < root->links; link++) { |
| 584 | for (child = root->link[link].children; child; |
| 585 | child = child->sibling) |
| 586 | resource_tree(child, debug_level, depth + 1); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | void print_resource_tree(struct device * root, int debug_level, |
| 591 | const char *msg) |
| 592 | { |
| 593 | /* Bail if root is null. */ |
| 594 | if (!root) { |
| 595 | do_printk(debug_level, "%s passed NULL for root!\n", __func__); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | /* Bail if not printing to screen. */ |
| 600 | if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n", |
| 601 | dev_path(root), msg)) |
| 602 | return; |
| 603 | resource_tree(root, debug_level, 0); |
| 604 | } |
| 605 | |
| 606 | void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum) |
| 607 | { |
| 608 | char depth_str[20] = ""; |
| 609 | int i; |
| 610 | struct device *sibling; |
| 611 | for (i = 0; i < depth; i++) |
| 612 | depth_str[i] = ' '; |
| 613 | depth_str[i] = '\0'; |
| 614 | do_printk(debug_level, "%s%s: enabled %d, %d resources\n", |
| 615 | depth_str, dev_path(dev), dev->enabled, dev->resources); |
| 616 | for (i = 0; i < dev->links; i++) { |
| 617 | for (sibling = dev->link[i].children; sibling; |
| 618 | sibling = sibling->sibling) |
| 619 | show_devs_tree(sibling, debug_level, depth + 1, i); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | void show_all_devs_tree(int debug_level, const char *msg) |
| 624 | { |
| 625 | /* Bail if not printing to screen. */ |
| 626 | if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg)) |
| 627 | return; |
| 628 | show_devs_tree(all_devices, debug_level, 0, -1); |
| 629 | } |
| 630 | |
| 631 | void show_devs_subtree(struct device *root, int debug_level, const char *msg) |
| 632 | { |
| 633 | /* Bail if not printing to screen. */ |
| 634 | if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n", |
| 635 | dev_path(root), msg)) |
| 636 | return; |
| 637 | do_printk(debug_level, "%s\n", msg); |
| 638 | show_devs_tree(root, debug_level, 0, -1); |
| 639 | } |
| 640 | |
| 641 | void show_all_devs(int debug_level, const char *msg) |
| 642 | { |
| 643 | struct device *dev; |
| 644 | |
| 645 | /* Bail if not printing to screen. */ |
| 646 | if (!do_printk(debug_level, "Show all devs...%s\n", msg)) |
| 647 | return; |
| 648 | for (dev = all_devices; dev; dev = dev->next) { |
| 649 | do_printk(debug_level, |
| 650 | "%s: enabled %d, %d resources\n", |
| 651 | dev_path(dev), dev->enabled, |
| 652 | dev->resources); |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | void show_one_resource(int debug_level, struct device *dev, |
| 657 | struct resource *resource, const char *comment) |
| 658 | { |
| 659 | char buf[10]; |
| 660 | unsigned long long base, end; |
| 661 | base = resource->base; |
| 662 | end = resource_end(resource); |
| 663 | buf[0] = '\0'; |
| 664 | /* |
| 665 | if (resource->flags & IORESOURCE_BRIDGE) { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 666 | #if CONFIG_PCI_BUS_SEGN_BITS |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 667 | sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8, |
| 668 | dev->link[0].secondary & 0xff); |
| 669 | #else |
| 670 | sprintf(buf, "bus %02x ", dev->link[0].secondary); |
| 671 | #endif |
| 672 | } |
| 673 | */ |
| 674 | do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] " |
| 675 | "size 0x%08Lx gran 0x%02x %s%s%s\n", |
| 676 | dev_path(dev), resource->index, base, end, |
| 677 | resource->size, resource->gran, buf, |
| 678 | resource_type(resource), comment); |
| 679 | |
| 680 | } |
| 681 | |
| 682 | void show_all_devs_resources(int debug_level, const char* msg) |
| 683 | { |
| 684 | struct device *dev; |
| 685 | |
| 686 | if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg)) |
| 687 | return; |
| 688 | |
| 689 | for (dev = all_devices; dev; dev = dev->next) { |
| 690 | int i; |
| 691 | do_printk(debug_level, |
| 692 | "%s: enabled %d, %d resources\n", |
| 693 | dev_path(dev), dev->enabled, |
| 694 | dev->resources); |
| 695 | for (i = 0; i < dev->resources; i++) |
| 696 | show_one_resource(debug_level, dev, &dev->resource[i], ""); |
| 697 | } |
| 698 | } |