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