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