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