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