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