Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 3 | #include <assert.h> |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 4 | #include <console/console.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 5 | #include <device/device.h> |
| 6 | #include <device/path.h> |
| 7 | #include <device/pci.h> |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 8 | #include <device/pci_def.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 9 | #include <device/resource.h> |
Furquan Shaikh | 7f6ae79 | 2021-05-20 22:47:02 -0700 | [diff] [blame] | 10 | #include <fw_config.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 11 | |
| 12 | /** Linked list of ALL devices */ |
Frans Hendriks | a9caa50 | 2021-02-01 11:44:37 +0100 | [diff] [blame] | 13 | DEVTREE_CONST struct device *DEVTREE_CONST all_devices = &dev_root; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Given a PCI bus and a devfn number, find the device structure. |
| 17 | * |
Aaron Durbin | 55ef0d2 | 2019-09-26 12:05:27 -0600 | [diff] [blame] | 18 | * Note that this function can return the incorrect device prior |
| 19 | * to PCI enumeration because the secondary field of the bus object |
| 20 | * is 0. The failing scenario is determined by the order of the |
| 21 | * devices in all_devices singly-linked list as well as the time |
| 22 | * when this function is called (secondary reflecting topology). |
| 23 | * |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 24 | * @param bus The bus number. |
| 25 | * @param devfn A device/function number. |
| 26 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 27 | */ |
Kyösti Mälkki | bd585fa | 2019-07-03 07:51:43 +0300 | [diff] [blame] | 28 | |
| 29 | static DEVTREE_CONST struct device *dev_find_slot(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 30 | unsigned int devfn) |
| 31 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 32 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 33 | |
| 34 | result = 0; |
| 35 | for (dev = all_devices; dev; dev = dev->next) { |
| 36 | if ((dev->path.type == DEVICE_PATH_PCI) && |
| 37 | (dev->bus->secondary == bus) && |
| 38 | (dev->path.pci.devfn == devfn)) { |
| 39 | result = dev; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | /** |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 47 | * Given a Device Path Type, find the device structure. |
| 48 | * |
| 49 | * @param prev_match The previously matched device instance. |
| 50 | * @param path_type The Device Path Type. |
| 51 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 52 | */ |
| 53 | DEVTREE_CONST struct device *dev_find_path( |
| 54 | DEVTREE_CONST struct device *prev_match, |
| 55 | enum device_path_type path_type) |
| 56 | { |
| 57 | DEVTREE_CONST struct device *dev, *result = NULL; |
| 58 | |
| 59 | if (prev_match == NULL) |
| 60 | prev_match = all_devices; |
| 61 | else |
| 62 | prev_match = prev_match->next; |
| 63 | |
| 64 | for (dev = prev_match; dev; dev = dev->next) { |
| 65 | if (dev->path.type == path_type) { |
| 66 | result = dev; |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | return result; |
| 71 | } |
| 72 | |
Furquan Shaikh | 1f3055a | 2020-04-20 17:59:50 -0700 | [diff] [blame] | 73 | DEVTREE_CONST struct device *dev_find_matching_device_on_bus(const struct bus *bus, |
| 74 | match_device_fn fn) |
| 75 | { |
| 76 | DEVTREE_CONST struct device *child = NULL; |
| 77 | |
| 78 | while ((child = dev_bus_each_child(bus, child)) != NULL) { |
| 79 | if (fn(child)) |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | return child; |
| 84 | } |
| 85 | |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 86 | /** |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 87 | * Given a device pointer, find the next PCI device. |
| 88 | * |
| 89 | * @param previous_dev A pointer to a PCI device structure. |
| 90 | * @return Pointer to the next device structure (if found), 0 otherwise. |
| 91 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 92 | DEVTREE_CONST struct device *dev_find_next_pci_device( |
| 93 | DEVTREE_CONST struct device *previous_dev) |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 94 | { |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 95 | return dev_find_path(previous_dev, DEVICE_PATH_PCI); |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 98 | static int path_eq(const struct device_path *path1, |
| 99 | const struct device_path *path2) |
| 100 | { |
| 101 | int equal = 0; |
| 102 | |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 103 | if (!path1 || !path2) { |
| 104 | assert(path1); |
| 105 | assert(path2); |
| 106 | /* Return 0 in case assert is considered non-fatal. */ |
| 107 | return 0; |
| 108 | } |
| 109 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 110 | if (path1->type != path2->type) |
| 111 | return 0; |
| 112 | |
| 113 | switch (path1->type) { |
| 114 | case DEVICE_PATH_NONE: |
| 115 | break; |
| 116 | case DEVICE_PATH_ROOT: |
| 117 | equal = 1; |
| 118 | break; |
| 119 | case DEVICE_PATH_PCI: |
| 120 | equal = (path1->pci.devfn == path2->pci.devfn); |
| 121 | break; |
| 122 | case DEVICE_PATH_PNP: |
| 123 | equal = (path1->pnp.port == path2->pnp.port) && |
| 124 | (path1->pnp.device == path2->pnp.device); |
| 125 | break; |
| 126 | case DEVICE_PATH_I2C: |
| 127 | equal = (path1->i2c.device == path2->i2c.device) && |
| 128 | (path1->i2c.mode_10bit == path2->i2c.mode_10bit); |
| 129 | break; |
| 130 | case DEVICE_PATH_APIC: |
| 131 | equal = (path1->apic.apic_id == path2->apic.apic_id); |
| 132 | break; |
| 133 | case DEVICE_PATH_DOMAIN: |
| 134 | equal = (path1->domain.domain == path2->domain.domain); |
| 135 | break; |
| 136 | case DEVICE_PATH_CPU_CLUSTER: |
| 137 | equal = (path1->cpu_cluster.cluster |
| 138 | == path2->cpu_cluster.cluster); |
| 139 | break; |
| 140 | case DEVICE_PATH_CPU: |
| 141 | equal = (path1->cpu.id == path2->cpu.id); |
| 142 | break; |
| 143 | case DEVICE_PATH_CPU_BUS: |
| 144 | equal = (path1->cpu_bus.id == path2->cpu_bus.id); |
| 145 | break; |
| 146 | case DEVICE_PATH_GENERIC: |
| 147 | equal = (path1->generic.id == path2->generic.id) && |
| 148 | (path1->generic.subid == path2->generic.subid); |
| 149 | break; |
| 150 | case DEVICE_PATH_SPI: |
| 151 | equal = (path1->spi.cs == path2->spi.cs); |
| 152 | break; |
| 153 | case DEVICE_PATH_USB: |
| 154 | equal = (path1->usb.port_type == path2->usb.port_type) && |
| 155 | (path1->usb.port_id == path2->usb.port_id); |
| 156 | break; |
| 157 | case DEVICE_PATH_MMIO: |
| 158 | equal = (path1->mmio.addr == path2->mmio.addr); |
| 159 | break; |
Raul E Rangel | 3f3f53c | 2020-05-06 11:47:04 -0600 | [diff] [blame] | 160 | case DEVICE_PATH_ESPI: |
| 161 | equal = (path1->espi.addr == path2->espi.addr); |
| 162 | break; |
| 163 | case DEVICE_PATH_LPC: |
| 164 | equal = (path1->lpc.addr == path2->lpc.addr); |
| 165 | break; |
Michael Niewöhner | dbb667a | 2020-12-11 21:26:02 +0100 | [diff] [blame] | 166 | case DEVICE_PATH_GPIO: |
| 167 | equal = (path1->gpio.id == path2->gpio.id); |
| 168 | break; |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 169 | default: |
| 170 | printk(BIOS_ERR, "Unknown device type: %d\n", path1->type); |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | return equal; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * See if a device structure exists for path. |
| 179 | * |
| 180 | * @param parent The bus to find the device on. |
| 181 | * @param path The relative path from the bus to the appropriate device. |
| 182 | * @return Pointer to a device structure for the device on bus at path |
| 183 | * or 0/NULL if no device is found. |
| 184 | */ |
| 185 | DEVTREE_CONST struct device *find_dev_path( |
| 186 | const struct bus *parent, const struct device_path *path) |
| 187 | { |
| 188 | DEVTREE_CONST struct device *child; |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 189 | |
| 190 | if (!parent) { |
Julius Werner | 3e034b6 | 2020-07-29 17:39:21 -0700 | [diff] [blame] | 191 | BUG(); |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 192 | /* Return NULL in case asserts are considered non-fatal. */ |
| 193 | return NULL; |
| 194 | } |
| 195 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 196 | for (child = parent->children; child; child = child->sibling) { |
| 197 | if (path_eq(path, &child->path)) |
| 198 | break; |
| 199 | } |
| 200 | return child; |
| 201 | } |
| 202 | |
Rob Barnes | a223e65 | 2020-07-23 08:25:42 -0600 | [diff] [blame] | 203 | /** |
| 204 | * Find the device structure given an array of nested device paths, |
| 205 | * |
| 206 | * @param parent The parent bus to start the search on. |
| 207 | * @param nested_path An array of relative paths from the parent bus to the target device. |
| 208 | * @param nested_path_length Number of path elements in nested_path array. |
| 209 | * @return Pointer to a device structure for the device at nested path |
| 210 | * or 0/NULL if no device is found. |
| 211 | */ |
| 212 | DEVTREE_CONST struct device *find_dev_nested_path( |
| 213 | const struct bus *parent, const struct device_path nested_path[], |
| 214 | size_t nested_path_length) |
| 215 | { |
| 216 | DEVTREE_CONST struct device *child; |
| 217 | |
| 218 | if (!parent || !nested_path || !nested_path_length) |
| 219 | return NULL; |
| 220 | |
| 221 | child = find_dev_path(parent, nested_path); |
| 222 | |
| 223 | /* Terminate recursion at end of nested path or child not found */ |
| 224 | if (nested_path_length == 1 || !child) |
| 225 | return child; |
| 226 | |
| 227 | return find_dev_nested_path(child->link_list, nested_path + 1, nested_path_length - 1); |
| 228 | } |
| 229 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 230 | DEVTREE_CONST struct device *pcidev_path_behind( |
| 231 | const struct bus *parent, pci_devfn_t devfn) |
| 232 | { |
| 233 | const struct device_path path = { |
| 234 | .type = DEVICE_PATH_PCI, |
| 235 | .pci.devfn = devfn, |
| 236 | }; |
| 237 | return find_dev_path(parent, &path); |
| 238 | } |
| 239 | |
Kyösti Mälkki | ab275f0 | 2019-07-04 07:16:59 +0300 | [diff] [blame] | 240 | DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn) |
| 241 | { |
| 242 | DEVTREE_CONST struct bus *parent = pci_root_bus(); |
| 243 | DEVTREE_CONST struct device *dev = parent->children; |
| 244 | |
| 245 | /* FIXME: Write the loop with topology links. */ |
| 246 | while (dev) { |
| 247 | if (dev->path.type != DEVICE_PATH_PCI) { |
| 248 | dev = dev->next; |
| 249 | continue; |
| 250 | } |
| 251 | if (dev->bus->secondary == bus) |
| 252 | return pcidev_path_behind(dev->bus, devfn); |
| 253 | dev = dev->next; |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 258 | DEVTREE_CONST struct bus *pci_root_bus(void) |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 259 | { |
| 260 | DEVTREE_CONST struct device *pci_domain; |
Kyösti Mälkki | fcbbb91 | 2020-04-20 10:21:39 +0300 | [diff] [blame] | 261 | static DEVTREE_CONST struct bus *pci_root; |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 262 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 263 | if (pci_root) |
| 264 | return pci_root; |
| 265 | |
| 266 | pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN); |
| 267 | if (!pci_domain) |
| 268 | return NULL; |
| 269 | |
| 270 | pci_root = pci_domain->link_list; |
| 271 | return pci_root; |
| 272 | } |
| 273 | |
| 274 | DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn) |
| 275 | { |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 276 | return pcidev_path_behind(pci_root_bus(), devfn); |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn) |
| 280 | { |
| 281 | return pcidev_path_on_root(PCI_DEVFN(dev, fn)); |
| 282 | } |
| 283 | |
Furquan Shaikh | 7778e5c | 2020-04-16 08:25:58 -0700 | [diff] [blame] | 284 | DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge( |
| 285 | const struct device *bridge, |
| 286 | pci_devfn_t devfn) |
| 287 | { |
| 288 | if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) { |
Julius Werner | 3e034b6 | 2020-07-29 17:39:21 -0700 | [diff] [blame] | 289 | BUG(); |
Furquan Shaikh | 7778e5c | 2020-04-16 08:25:58 -0700 | [diff] [blame] | 290 | /* Return NULL in case asserts are non-fatal. */ |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | return pcidev_path_behind(bridge->link_list, devfn); |
| 295 | } |
| 296 | |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 297 | DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func) |
| 298 | { |
| 299 | DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn); |
| 300 | if (dev) |
| 301 | return dev; |
| 302 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 303 | devtree_bug(func, devfn); |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 304 | |
| 305 | /* FIXME: This can return wrong device. */ |
| 306 | return dev_find_slot(0, devfn); |
| 307 | } |
| 308 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 309 | void devtree_bug(const char *func, pci_devfn_t devfn) |
| 310 | { |
| 311 | printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7); |
| 312 | } |
| 313 | |
| 314 | void __noreturn devtree_die(void) |
| 315 | { |
| 316 | die("DEVTREE: dev or chip_info is NULL\n"); |
| 317 | } |
| 318 | |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 319 | /** |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 320 | * Given an SMBus bus and a device number, find the device structure. |
| 321 | * |
| 322 | * @param bus The bus number. |
| 323 | * @param addr A device number. |
| 324 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 325 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 326 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 327 | unsigned int addr) |
| 328 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 329 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 330 | |
| 331 | result = 0; |
| 332 | for (dev = all_devices; dev; dev = dev->next) { |
| 333 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 334 | (dev->bus->secondary == bus) && |
| 335 | (dev->path.i2c.device == addr)) { |
| 336 | result = dev; |
| 337 | break; |
| 338 | } |
| 339 | } |
| 340 | return result; |
| 341 | } |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * Given a PnP port and a device number, find the device structure. |
| 345 | * |
| 346 | * @param port The I/O port. |
| 347 | * @param device Logical device number. |
| 348 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 349 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 350 | DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device) |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 351 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 352 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 353 | |
| 354 | for (dev = all_devices; dev; dev = dev->next) { |
| 355 | if ((dev->path.type == DEVICE_PATH_PNP) && |
| 356 | (dev->path.pnp.port == port) && |
| 357 | (dev->path.pnp.device == device)) { |
| 358 | return dev; |
| 359 | } |
| 360 | } |
| 361 | return 0; |
| 362 | } |
Aaron Durbin | a0dabd1 | 2018-07-25 08:49:19 -0600 | [diff] [blame] | 363 | |
| 364 | /** |
| 365 | * Given a device and previous match iterate through all the children. |
| 366 | * |
| 367 | * @param bus parent device's bus holding all the children |
| 368 | * @param prev_child previous child already traversed, if NULL start at |
| 369 | * children of parent bus. |
| 370 | * @return pointer to child or NULL when no more children |
| 371 | */ |
| 372 | DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent, |
| 373 | DEVTREE_CONST struct device *prev_child) |
| 374 | { |
| 375 | DEVTREE_CONST struct device *dev; |
| 376 | |
| 377 | if (parent == NULL) |
| 378 | return NULL; |
| 379 | |
| 380 | if (prev_child == NULL) |
| 381 | dev = parent->children; |
| 382 | else |
| 383 | dev = prev_child->sibling; |
| 384 | |
| 385 | return dev; |
| 386 | } |
Furquan Shaikh | 7f6ae79 | 2021-05-20 22:47:02 -0700 | [diff] [blame] | 387 | |
| 388 | bool is_dev_enabled(const struct device *dev) |
| 389 | { |
| 390 | if (!dev) |
| 391 | return false; |
| 392 | |
| 393 | /* For stages with immutable device tree, first check if device is disabled because of |
| 394 | fw_config probing. In these stages, dev->enabled does not reflect the true state of a |
| 395 | device that uses fw_config probing. */ |
| 396 | if (DEVTREE_EARLY && !fw_config_probe_dev(dev, NULL)) |
| 397 | return false; |
| 398 | return dev->enabled; |
| 399 | } |