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; |
Michael Niewöhner | dbb667a | 2020-12-11 21:26:02 +0100 | [diff] [blame] | 165 | case DEVICE_PATH_GPIO: |
| 166 | equal = (path1->gpio.id == path2->gpio.id); |
| 167 | break; |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 168 | default: |
| 169 | printk(BIOS_ERR, "Unknown device type: %d\n", path1->type); |
| 170 | break; |
| 171 | } |
| 172 | |
| 173 | return equal; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * See if a device structure exists for path. |
| 178 | * |
| 179 | * @param parent The bus to find the device on. |
| 180 | * @param path The relative path from the bus to the appropriate device. |
| 181 | * @return Pointer to a device structure for the device on bus at path |
| 182 | * or 0/NULL if no device is found. |
| 183 | */ |
| 184 | DEVTREE_CONST struct device *find_dev_path( |
| 185 | const struct bus *parent, const struct device_path *path) |
| 186 | { |
| 187 | DEVTREE_CONST struct device *child; |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 188 | |
| 189 | if (!parent) { |
Julius Werner | 3e034b6 | 2020-07-29 17:39:21 -0700 | [diff] [blame] | 190 | BUG(); |
Furquan Shaikh | 8680378 | 2020-04-16 23:13:28 -0700 | [diff] [blame] | 191 | /* Return NULL in case asserts are considered non-fatal. */ |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 195 | for (child = parent->children; child; child = child->sibling) { |
| 196 | if (path_eq(path, &child->path)) |
| 197 | break; |
| 198 | } |
| 199 | return child; |
| 200 | } |
| 201 | |
Rob Barnes | a223e65 | 2020-07-23 08:25:42 -0600 | [diff] [blame] | 202 | /** |
| 203 | * Find the device structure given an array of nested device paths, |
| 204 | * |
| 205 | * @param parent The parent bus to start the search on. |
| 206 | * @param nested_path An array of relative paths from the parent bus to the target device. |
| 207 | * @param nested_path_length Number of path elements in nested_path array. |
| 208 | * @return Pointer to a device structure for the device at nested path |
| 209 | * or 0/NULL if no device is found. |
| 210 | */ |
| 211 | DEVTREE_CONST struct device *find_dev_nested_path( |
| 212 | const struct bus *parent, const struct device_path nested_path[], |
| 213 | size_t nested_path_length) |
| 214 | { |
| 215 | DEVTREE_CONST struct device *child; |
| 216 | |
| 217 | if (!parent || !nested_path || !nested_path_length) |
| 218 | return NULL; |
| 219 | |
| 220 | child = find_dev_path(parent, nested_path); |
| 221 | |
| 222 | /* Terminate recursion at end of nested path or child not found */ |
| 223 | if (nested_path_length == 1 || !child) |
| 224 | return child; |
| 225 | |
| 226 | return find_dev_nested_path(child->link_list, nested_path + 1, nested_path_length - 1); |
| 227 | } |
| 228 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 229 | DEVTREE_CONST struct device *pcidev_path_behind( |
| 230 | const struct bus *parent, pci_devfn_t devfn) |
| 231 | { |
| 232 | const struct device_path path = { |
| 233 | .type = DEVICE_PATH_PCI, |
| 234 | .pci.devfn = devfn, |
| 235 | }; |
| 236 | return find_dev_path(parent, &path); |
| 237 | } |
| 238 | |
Kyösti Mälkki | ab275f0 | 2019-07-04 07:16:59 +0300 | [diff] [blame] | 239 | DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn) |
| 240 | { |
| 241 | DEVTREE_CONST struct bus *parent = pci_root_bus(); |
| 242 | DEVTREE_CONST struct device *dev = parent->children; |
| 243 | |
| 244 | /* FIXME: Write the loop with topology links. */ |
| 245 | while (dev) { |
| 246 | if (dev->path.type != DEVICE_PATH_PCI) { |
| 247 | dev = dev->next; |
| 248 | continue; |
| 249 | } |
| 250 | if (dev->bus->secondary == bus) |
| 251 | return pcidev_path_behind(dev->bus, devfn); |
| 252 | dev = dev->next; |
| 253 | } |
| 254 | return NULL; |
| 255 | } |
| 256 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 257 | DEVTREE_CONST struct bus *pci_root_bus(void) |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 258 | { |
| 259 | DEVTREE_CONST struct device *pci_domain; |
Kyösti Mälkki | fcbbb91 | 2020-04-20 10:21:39 +0300 | [diff] [blame] | 260 | static DEVTREE_CONST struct bus *pci_root; |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 261 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 262 | if (pci_root) |
| 263 | return pci_root; |
| 264 | |
| 265 | pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN); |
| 266 | if (!pci_domain) |
| 267 | return NULL; |
| 268 | |
| 269 | pci_root = pci_domain->link_list; |
| 270 | return pci_root; |
| 271 | } |
| 272 | |
| 273 | DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn) |
| 274 | { |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 275 | return pcidev_path_behind(pci_root_bus(), devfn); |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn) |
| 279 | { |
| 280 | return pcidev_path_on_root(PCI_DEVFN(dev, fn)); |
| 281 | } |
| 282 | |
Furquan Shaikh | 7778e5c | 2020-04-16 08:25:58 -0700 | [diff] [blame] | 283 | DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge( |
| 284 | const struct device *bridge, |
| 285 | pci_devfn_t devfn) |
| 286 | { |
| 287 | if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) { |
Julius Werner | 3e034b6 | 2020-07-29 17:39:21 -0700 | [diff] [blame] | 288 | BUG(); |
Furquan Shaikh | 7778e5c | 2020-04-16 08:25:58 -0700 | [diff] [blame] | 289 | /* Return NULL in case asserts are non-fatal. */ |
| 290 | return NULL; |
| 291 | } |
| 292 | |
| 293 | return pcidev_path_behind(bridge->link_list, devfn); |
| 294 | } |
| 295 | |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 296 | DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func) |
| 297 | { |
| 298 | DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn); |
| 299 | if (dev) |
| 300 | return dev; |
| 301 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 302 | devtree_bug(func, devfn); |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 303 | |
| 304 | /* FIXME: This can return wrong device. */ |
| 305 | return dev_find_slot(0, devfn); |
| 306 | } |
| 307 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 308 | void devtree_bug(const char *func, pci_devfn_t devfn) |
| 309 | { |
| 310 | printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7); |
| 311 | } |
| 312 | |
| 313 | void __noreturn devtree_die(void) |
| 314 | { |
| 315 | die("DEVTREE: dev or chip_info is NULL\n"); |
| 316 | } |
| 317 | |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 318 | /** |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 319 | * Given an SMBus bus and a device number, find the device structure. |
| 320 | * |
| 321 | * @param bus The bus number. |
| 322 | * @param addr A device number. |
| 323 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 324 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 325 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 326 | unsigned int addr) |
| 327 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 328 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 329 | |
| 330 | result = 0; |
| 331 | for (dev = all_devices; dev; dev = dev->next) { |
| 332 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 333 | (dev->bus->secondary == bus) && |
| 334 | (dev->path.i2c.device == addr)) { |
| 335 | result = dev; |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | return result; |
| 340 | } |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * Given a PnP port and a device number, find the device structure. |
| 344 | * |
| 345 | * @param port The I/O port. |
| 346 | * @param device Logical device number. |
| 347 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 348 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 349 | 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] | 350 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 351 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 352 | |
| 353 | for (dev = all_devices; dev; dev = dev->next) { |
| 354 | if ((dev->path.type == DEVICE_PATH_PNP) && |
| 355 | (dev->path.pnp.port == port) && |
| 356 | (dev->path.pnp.device == device)) { |
| 357 | return dev; |
| 358 | } |
| 359 | } |
| 360 | return 0; |
| 361 | } |
Aaron Durbin | a0dabd1 | 2018-07-25 08:49:19 -0600 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * Given a device and previous match iterate through all the children. |
| 365 | * |
| 366 | * @param bus parent device's bus holding all the children |
| 367 | * @param prev_child previous child already traversed, if NULL start at |
| 368 | * children of parent bus. |
| 369 | * @return pointer to child or NULL when no more children |
| 370 | */ |
| 371 | DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent, |
| 372 | DEVTREE_CONST struct device *prev_child) |
| 373 | { |
| 374 | DEVTREE_CONST struct device *dev; |
| 375 | |
| 376 | if (parent == NULL) |
| 377 | return NULL; |
| 378 | |
| 379 | if (prev_child == NULL) |
| 380 | dev = parent->children; |
| 381 | else |
| 382 | dev = prev_child->sibling; |
| 383 | |
| 384 | return dev; |
| 385 | } |