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