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