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