Angel Pons | c74dae9 | 2020-04-02 23:48:16 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 3 | |
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 | |
| 72 | /** |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 73 | * Given a device pointer, find the next PCI device. |
| 74 | * |
| 75 | * @param previous_dev A pointer to a PCI device structure. |
| 76 | * @return Pointer to the next device structure (if found), 0 otherwise. |
| 77 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 78 | DEVTREE_CONST struct device *dev_find_next_pci_device( |
| 79 | DEVTREE_CONST struct device *previous_dev) |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 80 | { |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 81 | return dev_find_path(previous_dev, DEVICE_PATH_PCI); |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 82 | } |
| 83 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 84 | static int path_eq(const struct device_path *path1, |
| 85 | const struct device_path *path2) |
| 86 | { |
| 87 | int equal = 0; |
| 88 | |
| 89 | if (path1->type != path2->type) |
| 90 | return 0; |
| 91 | |
| 92 | switch (path1->type) { |
| 93 | case DEVICE_PATH_NONE: |
| 94 | break; |
| 95 | case DEVICE_PATH_ROOT: |
| 96 | equal = 1; |
| 97 | break; |
| 98 | case DEVICE_PATH_PCI: |
| 99 | equal = (path1->pci.devfn == path2->pci.devfn); |
| 100 | break; |
| 101 | case DEVICE_PATH_PNP: |
| 102 | equal = (path1->pnp.port == path2->pnp.port) && |
| 103 | (path1->pnp.device == path2->pnp.device); |
| 104 | break; |
| 105 | case DEVICE_PATH_I2C: |
| 106 | equal = (path1->i2c.device == path2->i2c.device) && |
| 107 | (path1->i2c.mode_10bit == path2->i2c.mode_10bit); |
| 108 | break; |
| 109 | case DEVICE_PATH_APIC: |
| 110 | equal = (path1->apic.apic_id == path2->apic.apic_id); |
| 111 | break; |
| 112 | case DEVICE_PATH_DOMAIN: |
| 113 | equal = (path1->domain.domain == path2->domain.domain); |
| 114 | break; |
| 115 | case DEVICE_PATH_CPU_CLUSTER: |
| 116 | equal = (path1->cpu_cluster.cluster |
| 117 | == path2->cpu_cluster.cluster); |
| 118 | break; |
| 119 | case DEVICE_PATH_CPU: |
| 120 | equal = (path1->cpu.id == path2->cpu.id); |
| 121 | break; |
| 122 | case DEVICE_PATH_CPU_BUS: |
| 123 | equal = (path1->cpu_bus.id == path2->cpu_bus.id); |
| 124 | break; |
| 125 | case DEVICE_PATH_GENERIC: |
| 126 | equal = (path1->generic.id == path2->generic.id) && |
| 127 | (path1->generic.subid == path2->generic.subid); |
| 128 | break; |
| 129 | case DEVICE_PATH_SPI: |
| 130 | equal = (path1->spi.cs == path2->spi.cs); |
| 131 | break; |
| 132 | case DEVICE_PATH_USB: |
| 133 | equal = (path1->usb.port_type == path2->usb.port_type) && |
| 134 | (path1->usb.port_id == path2->usb.port_id); |
| 135 | break; |
| 136 | case DEVICE_PATH_MMIO: |
| 137 | equal = (path1->mmio.addr == path2->mmio.addr); |
| 138 | break; |
| 139 | default: |
| 140 | printk(BIOS_ERR, "Unknown device type: %d\n", path1->type); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | return equal; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * See if a device structure exists for path. |
| 149 | * |
| 150 | * @param parent The bus to find the device on. |
| 151 | * @param path The relative path from the bus to the appropriate device. |
| 152 | * @return Pointer to a device structure for the device on bus at path |
| 153 | * or 0/NULL if no device is found. |
| 154 | */ |
| 155 | DEVTREE_CONST struct device *find_dev_path( |
| 156 | const struct bus *parent, const struct device_path *path) |
| 157 | { |
| 158 | DEVTREE_CONST struct device *child; |
| 159 | for (child = parent->children; child; child = child->sibling) { |
| 160 | if (path_eq(path, &child->path)) |
| 161 | break; |
| 162 | } |
| 163 | return child; |
| 164 | } |
| 165 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 166 | DEVTREE_CONST struct device *pcidev_path_behind( |
| 167 | const struct bus *parent, pci_devfn_t devfn) |
| 168 | { |
| 169 | const struct device_path path = { |
| 170 | .type = DEVICE_PATH_PCI, |
| 171 | .pci.devfn = devfn, |
| 172 | }; |
| 173 | return find_dev_path(parent, &path); |
| 174 | } |
| 175 | |
Kyösti Mälkki | ab275f0 | 2019-07-04 07:16:59 +0300 | [diff] [blame] | 176 | DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn) |
| 177 | { |
| 178 | DEVTREE_CONST struct bus *parent = pci_root_bus(); |
| 179 | DEVTREE_CONST struct device *dev = parent->children; |
| 180 | |
| 181 | /* FIXME: Write the loop with topology links. */ |
| 182 | while (dev) { |
| 183 | if (dev->path.type != DEVICE_PATH_PCI) { |
| 184 | dev = dev->next; |
| 185 | continue; |
| 186 | } |
| 187 | if (dev->bus->secondary == bus) |
| 188 | return pcidev_path_behind(dev->bus, devfn); |
| 189 | dev = dev->next; |
| 190 | } |
| 191 | return NULL; |
| 192 | } |
| 193 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 194 | DEVTREE_CONST struct bus *pci_root_bus(void) |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 195 | { |
| 196 | DEVTREE_CONST struct device *pci_domain; |
Kyösti Mälkki | 117cf2b | 2019-08-20 06:01:57 +0300 | [diff] [blame] | 197 | MAYBE_STATIC_BSS DEVTREE_CONST struct bus *pci_root = NULL; |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 198 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 199 | if (pci_root) |
| 200 | return pci_root; |
| 201 | |
| 202 | pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN); |
| 203 | if (!pci_domain) |
| 204 | return NULL; |
| 205 | |
| 206 | pci_root = pci_domain->link_list; |
| 207 | return pci_root; |
| 208 | } |
| 209 | |
| 210 | DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn) |
| 211 | { |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 212 | return pcidev_path_behind(pci_root_bus(), devfn); |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn) |
| 216 | { |
| 217 | return pcidev_path_on_root(PCI_DEVFN(dev, fn)); |
| 218 | } |
| 219 | |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 220 | DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func) |
| 221 | { |
| 222 | DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn); |
| 223 | if (dev) |
| 224 | return dev; |
| 225 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 226 | devtree_bug(func, devfn); |
Kyösti Mälkki | f2ac013 | 2019-07-12 15:26:29 +0300 | [diff] [blame] | 227 | |
| 228 | /* FIXME: This can return wrong device. */ |
| 229 | return dev_find_slot(0, devfn); |
| 230 | } |
| 231 | |
Kyösti Mälkki | 4323d26 | 2019-07-12 16:20:14 +0300 | [diff] [blame] | 232 | void devtree_bug(const char *func, pci_devfn_t devfn) |
| 233 | { |
| 234 | printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7); |
| 235 | } |
| 236 | |
| 237 | void __noreturn devtree_die(void) |
| 238 | { |
| 239 | die("DEVTREE: dev or chip_info is NULL\n"); |
| 240 | } |
| 241 | |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 242 | /** |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 243 | * Given an SMBus bus and a device number, find the device structure. |
| 244 | * |
| 245 | * @param bus The bus number. |
| 246 | * @param addr A device number. |
| 247 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 248 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 249 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 250 | unsigned int addr) |
| 251 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 252 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 253 | |
| 254 | result = 0; |
| 255 | for (dev = all_devices; dev; dev = dev->next) { |
| 256 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 257 | (dev->bus->secondary == bus) && |
| 258 | (dev->path.i2c.device == addr)) { |
| 259 | result = dev; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | return result; |
| 264 | } |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 265 | |
| 266 | /** |
| 267 | * Given a PnP port and a device number, find the device structure. |
| 268 | * |
| 269 | * @param port The I/O port. |
| 270 | * @param device Logical device number. |
| 271 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 272 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 273 | 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] | 274 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 275 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 276 | |
| 277 | for (dev = all_devices; dev; dev = dev->next) { |
| 278 | if ((dev->path.type == DEVICE_PATH_PNP) && |
| 279 | (dev->path.pnp.port == port) && |
| 280 | (dev->path.pnp.device == device)) { |
| 281 | return dev; |
| 282 | } |
| 283 | } |
| 284 | return 0; |
| 285 | } |
Aaron Durbin | a0dabd1 | 2018-07-25 08:49:19 -0600 | [diff] [blame] | 286 | |
| 287 | /** |
| 288 | * Given a device and previous match iterate through all the children. |
| 289 | * |
| 290 | * @param bus parent device's bus holding all the children |
| 291 | * @param prev_child previous child already traversed, if NULL start at |
| 292 | * children of parent bus. |
| 293 | * @return pointer to child or NULL when no more children |
| 294 | */ |
| 295 | DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent, |
| 296 | DEVTREE_CONST struct device *prev_child) |
| 297 | { |
| 298 | DEVTREE_CONST struct device *dev; |
| 299 | |
| 300 | if (parent == NULL) |
| 301 | return NULL; |
| 302 | |
| 303 | if (prev_child == NULL) |
| 304 | dev = parent->children; |
| 305 | else |
| 306 | dev = prev_child->sibling; |
| 307 | |
| 308 | return dev; |
| 309 | } |