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 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 33 | * See if a device structure exists for path. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 34 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 35 | * @param parent 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. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 39 | */ |
| 40 | device_t find_dev_path(struct bus *parent, struct device_path *path) |
| 41 | { |
| 42 | device_t child; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 43 | for (child = parent->children; child; child = child->sibling) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 44 | if (path_eq(path, &child->path)) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 45 | break; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 46 | } |
| 47 | return child; |
| 48 | } |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 49 | |
| 50 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 51 | * Given a PCI bus and a devfn number, find the device structure. |
Li-Ta Lo | e8b1c9d | 2004-12-27 04:25:41 +0000 | [diff] [blame] | 52 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 53 | * @param bus The bus number. |
| 54 | * @param devfn A device/function number. |
| 55 | * @return Pointer to the device structure (if found), 0 otherwise. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 56 | */ |
| 57 | struct device *dev_find_slot(unsigned int bus, unsigned int devfn) |
| 58 | { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 59 | struct device *dev, *result; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 60 | |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 61 | result = 0; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 62 | for (dev = all_devices; dev; dev = dev->next) { |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 63 | if ((dev->path.type == DEVICE_PATH_PCI) && |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 64 | (dev->bus->secondary == bus) && |
| 65 | (dev->path.pci.devfn == devfn)) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 66 | result = dev; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 67 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 70 | return result; |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 71 | } |
| 72 | |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 73 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 74 | * Given an SMBus bus and a device number, find the device structure. |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 75 | * |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 76 | * @param bus The bus number. |
| 77 | * @param addr A device number. |
| 78 | * @return Pointer to the device structure (if found), 0 otherwise. |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 79 | */ |
| 80 | struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr) |
| 81 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 82 | struct device *dev, *result; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 83 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 84 | result = 0; |
| 85 | for (dev = all_devices; dev; dev = dev->next) { |
| 86 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 87 | (dev->bus->secondary == bus) && |
| 88 | (dev->path.i2c.device == addr)) { |
| 89 | result = dev; |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | return result; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 94 | } |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 95 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 96 | /** |
Duncan Laurie | 6f88a6e | 2011-07-18 10:41:36 -0700 | [diff] [blame] | 97 | * Given a Local APIC ID, find the device structure. |
| 98 | * |
| 99 | * @param apic_id The Local APIC ID number. |
| 100 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 101 | */ |
| 102 | device_t dev_find_lapic(unsigned apic_id) |
| 103 | { |
| 104 | device_t dev, result = NULL; |
| 105 | |
| 106 | for (dev = all_devices; dev; dev = dev->next) { |
| 107 | if (dev->path.type == DEVICE_PATH_APIC && |
| 108 | dev->path.apic.apic_id == apic_id) { |
| 109 | result = dev; |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | return result; |
| 114 | } |
| 115 | |
| 116 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 117 | * Find a device of a given vendor and type. |
| 118 | * |
| 119 | * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel). |
| 120 | * @param device A PCI device ID. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 121 | * @param from Pointer to the device structure, used as a starting point in |
| 122 | * the linked list of all_devices, which can be 0 to start at the |
| 123 | * head of the list (i.e. all_devices). |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 124 | * @return Pointer to the device struct. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 125 | */ |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 126 | struct device *dev_find_device(u16 vendor, u16 device, struct device *from) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 127 | { |
| 128 | if (!from) |
| 129 | from = all_devices; |
| 130 | else |
| 131 | from = from->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 132 | |
| 133 | while (from && (from->vendor != vendor || from->device != device)) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 134 | from = from->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 135 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 136 | return from; |
| 137 | } |
| 138 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 139 | /** |
| 140 | * Find a device of a given class. |
| 141 | * |
| 142 | * @param class Class of the device. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 143 | * @param from Pointer to the device structure, used as a starting point in |
| 144 | * the linked list of all_devices, which can be 0 to start at the |
| 145 | * head of the list (i.e. all_devices). |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 146 | * @return Pointer to the device struct. |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 147 | */ |
| 148 | struct device *dev_find_class(unsigned int class, struct device *from) |
| 149 | { |
| 150 | if (!from) |
| 151 | from = all_devices; |
| 152 | else |
| 153 | from = from->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 154 | |
Greg Watson | 5965169 | 2003-12-17 17:39:53 +0000 | [diff] [blame] | 155 | while (from && (from->class & 0xffffff00) != class) |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 156 | from = from->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 157 | |
Eric Biederman | 8ca8d76 | 2003-04-22 19:02:15 +0000 | [diff] [blame] | 158 | return from; |
| 159 | } |
| 160 | |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 161 | /* |
| 162 | * Warning: This function uses a static buffer. Don't call it more than once |
| 163 | * from the same print statement! |
| 164 | */ |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 165 | const char *dev_path(device_t dev) |
| 166 | { |
| 167 | static char buffer[DEVICE_PATH_MAX]; |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 168 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 169 | buffer[0] = '\0'; |
| 170 | if (!dev) { |
| 171 | memcpy(buffer, "<null>", 7); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 172 | } else { |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 173 | switch(dev->path.type) { |
Eric Biederman | 83b991a | 2003-10-11 06:20:25 +0000 | [diff] [blame] | 174 | case DEVICE_PATH_ROOT: |
| 175 | memcpy(buffer, "Root Device", 12); |
| 176 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 177 | case DEVICE_PATH_PCI: |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 178 | #if CONFIG_PCI_BUS_SEGN_BITS |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 179 | sprintf(buffer, "PCI: %04x:%02x:%02x.%01x", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 180 | dev->bus->secondary >> 8, |
| 181 | dev->bus->secondary & 0xff, |
| 182 | PCI_SLOT(dev->path.pci.devfn), |
| 183 | PCI_FUNC(dev->path.pci.devfn)); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 184 | #else |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 185 | sprintf(buffer, "PCI: %02x:%02x.%01x", |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 186 | dev->bus->secondary, |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 187 | PCI_SLOT(dev->path.pci.devfn), |
| 188 | PCI_FUNC(dev->path.pci.devfn)); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 189 | #endif |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 190 | break; |
| 191 | case DEVICE_PATH_PNP: |
| 192 | sprintf(buffer, "PNP: %04x.%01x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 193 | dev->path.pnp.port, dev->path.pnp.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 194 | break; |
| 195 | case DEVICE_PATH_I2C: |
arch import user (historical) | 98d0d30 | 2005-07-06 17:13:46 +0000 | [diff] [blame] | 196 | sprintf(buffer, "I2C: %02x:%02x", |
| 197 | dev->bus->secondary, |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 198 | dev->path.i2c.device); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 199 | break; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 200 | case DEVICE_PATH_APIC: |
| 201 | sprintf(buffer, "APIC: %02x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 202 | dev->path.apic.apic_id); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 203 | break; |
Sven Schnelle | 0fa50a1 | 2012-06-21 22:19:48 +0200 | [diff] [blame] | 204 | case DEVICE_PATH_IOAPIC: |
| 205 | sprintf(buffer, "IOAPIC: %02x", |
| 206 | dev->path.ioapic.ioapic_id); |
| 207 | break; |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 208 | case DEVICE_PATH_PCI_DOMAIN: |
| 209 | sprintf(buffer, "PCI_DOMAIN: %04x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 210 | dev->path.pci_domain.domain); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 211 | break; |
| 212 | case DEVICE_PATH_APIC_CLUSTER: |
| 213 | sprintf(buffer, "APIC_CLUSTER: %01x", |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 214 | dev->path.apic_cluster.cluster); |
Eric Biederman | 7003ba4 | 2004-10-16 06:20:29 +0000 | [diff] [blame] | 215 | break; |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 216 | case DEVICE_PATH_CPU: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 217 | sprintf(buffer, "CPU: %02x", dev->path.cpu.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 218 | break; |
| 219 | case DEVICE_PATH_CPU_BUS: |
Stefan Reinauer | 2b34db8 | 2009-02-28 20:10:20 +0000 | [diff] [blame] | 220 | sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id); |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 221 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 222 | default: |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 223 | printk(BIOS_ERR, "Unknown device path type: %d\n", |
| 224 | dev->path.type); |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | return buffer; |
| 229 | } |
| 230 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 231 | const char *bus_path(struct bus *bus) |
| 232 | { |
| 233 | static char buffer[BUS_PATH_MAX]; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 234 | sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 235 | return buffer; |
| 236 | } |
| 237 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 238 | int path_eq(struct device_path *path1, struct device_path *path2) |
| 239 | { |
| 240 | int equal = 0; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 241 | |
| 242 | if (path1->type != path2->type) |
| 243 | return 0; |
| 244 | |
| 245 | switch (path1->type) { |
| 246 | case DEVICE_PATH_NONE: |
| 247 | break; |
| 248 | case DEVICE_PATH_ROOT: |
| 249 | equal = 1; |
| 250 | break; |
| 251 | case DEVICE_PATH_PCI: |
| 252 | equal = (path1->pci.devfn == path2->pci.devfn); |
| 253 | break; |
| 254 | case DEVICE_PATH_PNP: |
| 255 | equal = (path1->pnp.port == path2->pnp.port) && |
| 256 | (path1->pnp.device == path2->pnp.device); |
| 257 | break; |
| 258 | case DEVICE_PATH_I2C: |
| 259 | equal = (path1->i2c.device == path2->i2c.device); |
| 260 | break; |
| 261 | case DEVICE_PATH_APIC: |
| 262 | equal = (path1->apic.apic_id == path2->apic.apic_id); |
| 263 | break; |
| 264 | case DEVICE_PATH_PCI_DOMAIN: |
| 265 | equal = (path1->pci_domain.domain == path2->pci_domain.domain); |
| 266 | break; |
| 267 | case DEVICE_PATH_APIC_CLUSTER: |
| 268 | equal = (path1->apic_cluster.cluster |
| 269 | == path2->apic_cluster.cluster); |
| 270 | break; |
| 271 | case DEVICE_PATH_CPU: |
| 272 | equal = (path1->cpu.id == path2->cpu.id); |
| 273 | break; |
| 274 | case DEVICE_PATH_CPU_BUS: |
| 275 | equal = (path1->cpu_bus.id == path2->cpu_bus.id); |
| 276 | break; |
| 277 | default: |
| 278 | printk(BIOS_ERR, "Uknown device type: %d\n", path1->type); |
| 279 | break; |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 280 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 281 | |
Eric Biederman | e9a271e3 | 2003-09-02 03:36:25 +0000 | [diff] [blame] | 282 | return equal; |
| 283 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 284 | |
| 285 | /** |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 286 | * Allocate 64 more resources to the free list. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 287 | * |
| 288 | * @return TODO. |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 289 | */ |
| 290 | static int allocate_more_resources(void) |
| 291 | { |
| 292 | int i; |
| 293 | struct resource *new_res_list; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 294 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 295 | new_res_list = malloc(64 * sizeof(*new_res_list)); |
| 296 | |
| 297 | if (new_res_list == NULL) |
| 298 | return 0; |
| 299 | |
| 300 | memset(new_res_list, 0, 64 * sizeof(*new_res_list)); |
| 301 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 302 | for (i = 0; i < 64 - 1; i++) |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 303 | new_res_list[i].next = &new_res_list[i+1]; |
| 304 | |
| 305 | free_resources = new_res_list; |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Remove resource res from the device's list and add it to the free list. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 311 | * |
| 312 | * @param dev TODO |
| 313 | * @param res TODO |
| 314 | * @param prev TODO |
| 315 | * @return TODO. |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 316 | */ |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 317 | static void free_resource(device_t dev, struct resource *res, |
| 318 | struct resource *prev) |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 319 | { |
| 320 | if (prev) |
| 321 | prev->next = res->next; |
| 322 | else |
| 323 | dev->resource_list = res->next; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 324 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 325 | res->next = free_resources; |
| 326 | free_resources = res; |
| 327 | } |
| 328 | |
| 329 | /** |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 330 | * See if we have unused but allocated resource structures. |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 331 | * |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 332 | * If so remove the allocation. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 333 | * |
| 334 | * @param dev The device to find the resource on. |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 335 | */ |
| 336 | void compact_resources(device_t dev) |
| 337 | { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 338 | struct resource *res, *next, *prev = NULL; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 339 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 340 | /* Move all of the free resources to the end */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 341 | for (res = dev->resource_list; res; res = next) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 342 | next = res->next; |
| 343 | if (!res->flags) |
| 344 | free_resource(dev, res, prev); |
| 345 | else |
| 346 | prev = res; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 351 | * See if a resource structure already exists for a given index. |
| 352 | * |
| 353 | * @param dev The device to find the resource on. |
| 354 | * @param index The index of the resource on the device. |
| 355 | * @return The resource, if it already exists. |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 356 | */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 357 | struct resource *probe_resource(device_t dev, unsigned index) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 358 | { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 359 | struct resource *res; |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 360 | |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 361 | /* See if there is a resource with the appropriate index */ |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 362 | for (res = dev->resource_list; res; res = res->next) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 363 | if (res->index == index) |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 364 | break; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 365 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 366 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 367 | return res; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 371 | * See if a resource structure already exists for a given index and if not |
| 372 | * allocate one. |
| 373 | * |
| 374 | * Then initialize the initialize the resource to default values. |
| 375 | * |
| 376 | * @param dev The device to find the resource on. |
| 377 | * @param index The index of the resource on the device. |
| 378 | * @return TODO. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 379 | */ |
| 380 | struct resource *new_resource(device_t dev, unsigned index) |
| 381 | { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 382 | struct resource *resource, *tail; |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 383 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 384 | /* First move all of the free resources to the end. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 385 | compact_resources(dev); |
| 386 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 387 | /* See if there is a resource with the appropriate index. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 388 | resource = probe_resource(dev, index); |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 389 | if (!resource) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 390 | if (free_resources == NULL && !allocate_more_resources()) |
| 391 | die("Couldn't allocate more resources."); |
| 392 | |
| 393 | resource = free_resources; |
| 394 | free_resources = free_resources->next; |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 395 | memset(resource, 0, sizeof(*resource)); |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 396 | resource->next = NULL; |
| 397 | tail = dev->resource_list; |
| 398 | if (tail) { |
| 399 | while (tail->next) tail = tail->next; |
| 400 | tail->next = resource; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 401 | } else { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 402 | dev->resource_list = resource; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 403 | } |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 404 | } |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 405 | |
| 406 | /* Initialize the resource values. */ |
Eric Biederman | 5cd8173 | 2004-03-11 15:01:31 +0000 | [diff] [blame] | 407 | if (!(resource->flags & IORESOURCE_FIXED)) { |
| 408 | resource->flags = 0; |
| 409 | resource->base = 0; |
| 410 | } |
| 411 | resource->size = 0; |
| 412 | resource->limit = 0; |
| 413 | resource->index = index; |
| 414 | resource->align = 0; |
| 415 | resource->gran = 0; |
| 416 | |
| 417 | return resource; |
| 418 | } |
| 419 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 420 | /** |
| 421 | * Return an existing resource structure for a given index. |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 422 | * |
| 423 | * @param dev The device to find the resource on. |
| 424 | * @param index The index of the resource on the device. |
| 425 | * return TODO. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 426 | */ |
| 427 | struct resource *find_resource(device_t dev, unsigned index) |
| 428 | { |
| 429 | struct resource *resource; |
| 430 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 431 | /* See if there is a resource with the appropriate index. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 432 | resource = probe_resource(dev, index); |
| 433 | if (!resource) { |
Stefan Reinauer | c02b4fc | 2010-03-22 11:42:32 +0000 | [diff] [blame] | 434 | printk(BIOS_EMERG, "%s missing resource: %02x\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 435 | dev_path(dev), index); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 436 | die(""); |
| 437 | } |
| 438 | return resource; |
| 439 | } |
| 440 | |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 441 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 442 | * Round a number up to the next multiple of gran. |
| 443 | * |
| 444 | * @param val The starting value. |
| 445 | * @param gran Granularity we are aligning the number to. |
| 446 | * @return The aligned value. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 447 | */ |
| 448 | static resource_t align_up(resource_t val, unsigned long gran) |
| 449 | { |
| 450 | resource_t mask; |
| 451 | mask = (1ULL << gran) - 1ULL; |
| 452 | val += mask; |
| 453 | val &= ~mask; |
| 454 | return val; |
| 455 | } |
| 456 | |
| 457 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 458 | * Round a number up to the previous multiple of gran. |
| 459 | * |
| 460 | * @param val The starting value. |
| 461 | * @param gran Granularity we are aligning the number to. |
| 462 | * @return The aligned value. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 463 | */ |
| 464 | static resource_t align_down(resource_t val, unsigned long gran) |
| 465 | { |
| 466 | resource_t mask; |
| 467 | mask = (1ULL << gran) - 1ULL; |
| 468 | val &= ~mask; |
| 469 | return val; |
| 470 | } |
| 471 | |
| 472 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 473 | * Compute the maximum address that is part of a resource. |
| 474 | * |
| 475 | * @param resource The resource whose limit is desired. |
| 476 | * @return The end. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 477 | */ |
| 478 | resource_t resource_end(struct resource *resource) |
| 479 | { |
| 480 | resource_t base, end; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 481 | |
| 482 | /* Get the base address. */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 483 | base = resource->base; |
| 484 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 485 | /* |
| 486 | * For a non bridge resource granularity and alignment are the same. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 487 | * For a bridge resource align is the largest needed alignment below |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 488 | * the bridge. While the granularity is simply how many low bits of |
| 489 | * the address cannot be set. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 490 | */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 491 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 492 | /* Get the end (rounded up). */ |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 493 | end = base + align_up(resource->size, resource->gran) - 1; |
| 494 | |
| 495 | return end; |
| 496 | } |
| 497 | |
| 498 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 499 | * Compute the maximum legal value for resource->base. |
| 500 | * |
| 501 | * @param resource The resource whose maximum is desired. |
| 502 | * @return The maximum. |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 503 | */ |
| 504 | resource_t resource_max(struct resource *resource) |
| 505 | { |
| 506 | resource_t max; |
| 507 | |
| 508 | max = align_down(resource->limit - resource->size + 1, resource->align); |
| 509 | |
| 510 | return max; |
| 511 | } |
| 512 | |
| 513 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 514 | * Return the resource type of a resource. |
| 515 | * |
| 516 | * @param resource The resource type to decode. |
| 517 | * @return TODO. |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 518 | */ |
| 519 | const char *resource_type(struct resource *resource) |
| 520 | { |
| 521 | static char buffer[RESOURCE_TYPE_MAX]; |
| 522 | sprintf(buffer, "%s%s%s%s", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 523 | ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""), |
| 524 | ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""), |
| 525 | ((resource->flags == 0) ? "unused" : |
| 526 | (resource->flags & IORESOURCE_IO) ? "io" : |
| 527 | (resource->flags & IORESOURCE_DRQ) ? "drq" : |
| 528 | (resource->flags & IORESOURCE_IRQ) ? "irq" : |
| 529 | (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"), |
| 530 | ((resource->flags & IORESOURCE_PCI64) ? "64" : "")); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 531 | return buffer; |
| 532 | } |
| 533 | |
| 534 | /** |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 535 | * Print the resource that was just stored. |
| 536 | * |
| 537 | * @param dev The device the stored resorce lives on. |
| 538 | * @param resource The resource that was just stored. |
| 539 | * @param comment TODO |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 540 | */ |
Uwe Hermann | c1ee429 | 2010-10-17 19:01:48 +0000 | [diff] [blame] | 541 | void report_resource_stored(device_t dev, struct resource *resource, |
| 542 | const char *comment) |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 543 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 544 | char buf[10]; |
| 545 | unsigned long long base, end; |
| 546 | |
| 547 | if (!(resource->flags & IORESOURCE_STORED)) |
| 548 | return; |
| 549 | |
| 550 | base = resource->base; |
| 551 | end = resource_end(resource); |
| 552 | buf[0] = '\0'; |
| 553 | |
| 554 | if (resource->flags & IORESOURCE_PCI_BRIDGE) { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 555 | #if CONFIG_PCI_BUS_SEGN_BITS |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 556 | sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8, |
| 557 | dev->link_list->secondary & 0xff); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 558 | #else |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 559 | sprintf(buf, "bus %02x ", dev->link_list->secondary); |
Yinghai Lu | 5f9624d | 2006-10-04 22:56:21 +0000 | [diff] [blame] | 560 | #endif |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 561 | } |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 562 | printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx " |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 563 | "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index, |
| 564 | base, end, resource->size, resource->gran, buf, |
| 565 | resource_type(resource), comment); |
Eric Biederman | 03acab6 | 2004-10-14 21:25:53 +0000 | [diff] [blame] | 566 | } |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 567 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 568 | void search_bus_resources(struct bus *bus, unsigned long type_mask, |
| 569 | unsigned long type, resource_search_t search, |
| 570 | void *gp) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 571 | { |
| 572 | struct device *curdev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 573 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 574 | for (curdev = bus->children; curdev; curdev = curdev->sibling) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 575 | struct resource *res; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 576 | |
| 577 | /* Ignore disabled devices. */ |
| 578 | if (!curdev->enabled) |
| 579 | continue; |
| 580 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 581 | for (res = curdev->resource_list; res; res = res->next) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 582 | /* If it isn't the right kind of resource ignore it. */ |
| 583 | if ((res->flags & type_mask) != type) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 584 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 585 | |
| 586 | /* If it is a subtractive resource recurse. */ |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 587 | if (res->flags & IORESOURCE_SUBTRACTIVE) { |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 588 | struct bus * subbus; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 589 | for (subbus = curdev->link_list; subbus; |
| 590 | subbus = subbus->next) |
| 591 | if (subbus->link_num |
| 592 | == IOINDEX_SUBTRACTIVE_LINK(res->index)) |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 593 | break; |
Stefan Reinauer | 5807555 | 2011-05-11 15:57:07 -0700 | [diff] [blame] | 594 | if (!subbus) /* Why can subbus be NULL? */ |
| 595 | break; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 596 | search_bus_resources(subbus, type_mask, type, |
| 597 | search, gp); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 598 | continue; |
| 599 | } |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 600 | search(gp, curdev, res); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 605 | void search_global_resources(unsigned long type_mask, unsigned long type, |
| 606 | resource_search_t search, void *gp) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 607 | { |
| 608 | struct device *curdev; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 609 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 610 | for (curdev = all_devices; curdev; curdev = curdev->next) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 611 | struct resource *res; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 612 | |
| 613 | /* Ignore disabled devices. */ |
| 614 | if (!curdev->enabled) |
| 615 | continue; |
| 616 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 617 | for (res = curdev->resource_list; res; res = res->next) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 618 | /* If it isn't the right kind of resource ignore it. */ |
| 619 | if ((res->flags & type_mask) != type) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 620 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 621 | |
| 622 | /* If it is a subtractive resource ignore it. */ |
| 623 | if (res->flags & IORESOURCE_SUBTRACTIVE) |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 624 | continue; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 625 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 626 | search(gp, curdev, res); |
Eric Biederman | f8a2ddd | 2004-10-30 08:05:41 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | } |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 630 | |
| 631 | void dev_set_enabled(device_t dev, int enable) |
| 632 | { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 633 | if (dev->enabled == enable) |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 634 | return; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 635 | |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 636 | dev->enabled = enable; |
| 637 | if (dev->ops && dev->ops->enable) { |
| 638 | dev->ops->enable(dev); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 639 | } else if (dev->chip_ops && dev->chip_ops->enable_dev) { |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 640 | dev->chip_ops->enable_dev(dev); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | void disable_children(struct bus *bus) |
| 645 | { |
| 646 | device_t child; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 647 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 648 | for (child = bus->children; child; child = child->sibling) { |
| 649 | struct bus *link; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 650 | for (link = child->link_list; link; link = link->next) |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 651 | disable_children(link); |
Yinghai Lu | 13f1c2a | 2005-07-08 02:49:49 +0000 | [diff] [blame] | 652 | dev_set_enabled(child, 0); |
| 653 | } |
| 654 | } |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 655 | |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 656 | static void resource_tree(struct device *root, int debug_level, int depth) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 657 | { |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 658 | int i = 0; |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 659 | struct device *child; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 660 | struct bus *link; |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 661 | struct resource *res; |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 662 | char indent[30]; /* If your tree has more levels, it's wrong. */ |
| 663 | |
| 664 | for (i = 0; i < depth + 1 && i < 29; i++) |
| 665 | indent[i] = ' '; |
| 666 | indent[i] = '\0'; |
| 667 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 668 | do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root)); |
| 669 | if (root->link_list && root->link_list->children) |
| 670 | do_printk(BIOS_DEBUG, " child on link 0 %s", |
| 671 | dev_path(root->link_list->children)); |
| 672 | do_printk(BIOS_DEBUG, "\n"); |
| 673 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 674 | for (res = root->resource_list; res; res = res->next) { |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 675 | do_printk(debug_level, "%s%s resource base %llx size %llx " |
| 676 | "align %d gran %d limit %llx flags %lx index %lx\n", |
| 677 | indent, dev_path(root), res->base, res->size, |
| 678 | res->align, res->gran, res->limit, res->flags, |
| 679 | res->index); |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 682 | for (link = root->link_list; link; link = link->next) { |
| 683 | for (child = link->children; child; child = child->sibling) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 684 | resource_tree(child, debug_level, depth + 1); |
| 685 | } |
| 686 | } |
| 687 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 688 | void print_resource_tree(struct device *root, int debug_level, const char *msg) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 689 | { |
| 690 | /* Bail if root is null. */ |
| 691 | if (!root) { |
| 692 | do_printk(debug_level, "%s passed NULL for root!\n", __func__); |
| 693 | return; |
| 694 | } |
| 695 | |
| 696 | /* Bail if not printing to screen. */ |
| 697 | if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 698 | dev_path(root), msg)) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 699 | return; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 700 | |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 701 | resource_tree(root, debug_level, 0); |
| 702 | } |
| 703 | |
| 704 | void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum) |
| 705 | { |
| 706 | char depth_str[20] = ""; |
| 707 | int i; |
| 708 | struct device *sibling; |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 709 | struct bus *link; |
| 710 | |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 711 | for (i = 0; i < depth; i++) |
| 712 | depth_str[i] = ' '; |
| 713 | depth_str[i] = '\0'; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 714 | |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 715 | do_printk(debug_level, "%s%s: enabled %d\n", |
| 716 | depth_str, dev_path(dev), dev->enabled); |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 717 | |
Myles Watson | 894a347 | 2010-06-09 22:41:35 +0000 | [diff] [blame] | 718 | for (link = dev->link_list; link; link = link->next) { |
| 719 | for (sibling = link->children; sibling; |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 720 | sibling = sibling->sibling) |
| 721 | show_devs_tree(sibling, debug_level, depth + 1, i); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | void show_all_devs_tree(int debug_level, const char *msg) |
| 726 | { |
| 727 | /* Bail if not printing to screen. */ |
| 728 | if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg)) |
| 729 | return; |
| 730 | show_devs_tree(all_devices, debug_level, 0, -1); |
| 731 | } |
| 732 | |
| 733 | void show_devs_subtree(struct device *root, int debug_level, const char *msg) |
| 734 | { |
| 735 | /* Bail if not printing to screen. */ |
| 736 | if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n", |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 737 | dev_path(root), msg)) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 738 | return; |
| 739 | do_printk(debug_level, "%s\n", msg); |
| 740 | show_devs_tree(root, debug_level, 0, -1); |
| 741 | } |
| 742 | |
| 743 | void show_all_devs(int debug_level, const char *msg) |
| 744 | { |
| 745 | struct device *dev; |
| 746 | |
| 747 | /* Bail if not printing to screen. */ |
| 748 | if (!do_printk(debug_level, "Show all devs...%s\n", msg)) |
| 749 | return; |
| 750 | for (dev = all_devices; dev; dev = dev->next) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 751 | do_printk(debug_level, "%s: enabled %d\n", |
| 752 | dev_path(dev), dev->enabled); |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 753 | } |
| 754 | } |
| 755 | |
| 756 | void show_one_resource(int debug_level, struct device *dev, |
| 757 | struct resource *resource, const char *comment) |
| 758 | { |
| 759 | char buf[10]; |
| 760 | unsigned long long base, end; |
| 761 | base = resource->base; |
| 762 | end = resource_end(resource); |
| 763 | buf[0] = '\0'; |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 764 | |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 765 | /* |
| 766 | if (resource->flags & IORESOURCE_BRIDGE) { |
Stefan Reinauer | 0867062 | 2009-06-30 15:17:49 +0000 | [diff] [blame] | 767 | #if CONFIG_PCI_BUS_SEGN_BITS |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 768 | sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8, |
| 769 | dev->link[0].secondary & 0xff); |
| 770 | #else |
| 771 | sprintf(buf, "bus %02x ", dev->link[0].secondary); |
| 772 | #endif |
| 773 | } |
| 774 | */ |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 775 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 776 | do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] " |
Patrick Georgi | 5161509 | 2012-03-11 19:31:03 +0100 | [diff] [blame] | 777 | "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev), |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 778 | resource->index, base, end, resource->size, resource->gran, |
| 779 | buf, resource_type(resource), comment); |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void show_all_devs_resources(int debug_level, const char* msg) |
| 783 | { |
| 784 | struct device *dev; |
| 785 | |
Uwe Hermann | e487047 | 2010-11-04 23:23:47 +0000 | [diff] [blame] | 786 | if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg)) |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 787 | return; |
| 788 | |
| 789 | for (dev = all_devices; dev; dev = dev->next) { |
Myles Watson | c25cc11 | 2010-05-21 14:33:48 +0000 | [diff] [blame] | 790 | struct resource *res; |
| 791 | do_printk(debug_level, "%s: enabled %d\n", |
| 792 | dev_path(dev), dev->enabled); |
| 793 | for (res = dev->resource_list; res; res = res->next) |
| 794 | show_one_resource(debug_level, dev, res, ""); |
Myles Watson | bb3d812 | 2009-05-11 22:45:35 +0000 | [diff] [blame] | 795 | } |
| 796 | } |
Uwe Hermann | 4b42a62 | 2010-10-11 19:36:13 +0000 | [diff] [blame] | 797 | |
| 798 | void ram_resource(device_t dev, unsigned long index, |
| 799 | unsigned long basek, unsigned long sizek) |
| 800 | { |
| 801 | struct resource *resource; |
| 802 | |
| 803 | if (!sizek) |
| 804 | return; |
| 805 | |
| 806 | resource = new_resource(dev, index); |
| 807 | resource->base = ((resource_t)basek) << 10; |
| 808 | resource->size = ((resource_t)sizek) << 10; |
| 809 | resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \ |
| 810 | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 811 | } |
| 812 | |
Kyösti Mälkki | 63f8c08 | 2012-07-10 13:27:26 +0300 | [diff] [blame] | 813 | void uma_resource(device_t dev, unsigned long index, |
| 814 | unsigned long basek, unsigned long sizek) |
| 815 | { |
| 816 | struct resource *resource; |
| 817 | |
| 818 | if (!sizek) |
| 819 | return; |
| 820 | |
| 821 | resource = new_resource(dev, index); |
| 822 | resource->base = ((resource_t)basek) << 10; |
| 823 | resource->size = ((resource_t)sizek) << 10; |
Kyösti Mälkki | 0b08515 | 2012-07-11 23:14:49 +0300 | [diff] [blame] | 824 | resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE | IORESOURCE_UMA_FB | |
Kyösti Mälkki | 63f8c08 | 2012-07-10 13:27:26 +0300 | [diff] [blame] | 825 | IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED; |
| 826 | } |
| 827 | |
Uwe Hermann | 4b42a62 | 2010-10-11 19:36:13 +0000 | [diff] [blame] | 828 | void tolm_test(void *gp, struct device *dev, struct resource *new) |
| 829 | { |
| 830 | struct resource **best_p = gp; |
| 831 | struct resource *best; |
| 832 | |
| 833 | best = *best_p; |
| 834 | |
| 835 | if (!best || (best->base > new->base)) |
| 836 | best = new; |
| 837 | |
| 838 | *best_p = best; |
| 839 | } |
| 840 | |
| 841 | u32 find_pci_tolm(struct bus *bus) |
| 842 | { |
| 843 | struct resource *min = NULL; |
| 844 | u32 tolm; |
| 845 | |
| 846 | search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM, |
| 847 | tolm_test, &min); |
| 848 | |
| 849 | tolm = 0xffffffffUL; |
| 850 | |
| 851 | if (min && tolm > min->base) |
| 852 | tolm = min->base; |
| 853 | |
| 854 | return tolm; |
| 855 | } |
Stefan Reinauer | dc8448fd | 2012-03-30 13:52:58 -0700 | [diff] [blame] | 856 | |
| 857 | /* Count of enabled CPUs */ |
| 858 | int dev_count_cpu(void) |
| 859 | { |
| 860 | device_t cpu; |
| 861 | int count = 0; |
| 862 | |
| 863 | for (cpu = all_devices; cpu; cpu = cpu->next) { |
| 864 | if ((cpu->path.type != DEVICE_PATH_APIC) || |
| 865 | (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER)) |
| 866 | continue; |
| 867 | if (!cpu->enabled) |
| 868 | continue; |
| 869 | count++; |
| 870 | } |
| 871 | |
| 872 | return count; |
| 873 | } |