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