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