Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2003-2004 Linux Networx |
| 5 | * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx) |
| 6 | * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net> |
| 7 | * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov> |
| 8 | * Copyright (C) 2005-2006 Tyan |
| 9 | * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan) |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; version 2 of the License. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 21 | #include <console/console.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 22 | #include <device/device.h> |
| 23 | #include <device/path.h> |
| 24 | #include <device/pci.h> |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 25 | #include <device/pci_def.h> |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 26 | #include <device/resource.h> |
| 27 | |
| 28 | /** Linked list of ALL devices */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 29 | DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Given a PCI bus and a devfn number, find the device structure. |
| 33 | * |
| 34 | * @param bus The bus number. |
| 35 | * @param devfn A device/function number. |
| 36 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 37 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 38 | DEVTREE_CONST struct device *dev_find_slot(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 39 | unsigned int devfn) |
| 40 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 41 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 42 | |
| 43 | result = 0; |
| 44 | for (dev = all_devices; dev; dev = dev->next) { |
| 45 | if ((dev->path.type == DEVICE_PATH_PCI) && |
| 46 | (dev->bus->secondary == bus) && |
| 47 | (dev->path.pci.devfn == devfn)) { |
| 48 | result = dev; |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | return result; |
| 53 | } |
| 54 | |
| 55 | /** |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 56 | * Given a Device Path Type, find the device structure. |
| 57 | * |
| 58 | * @param prev_match The previously matched device instance. |
| 59 | * @param path_type The Device Path Type. |
| 60 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 61 | */ |
| 62 | DEVTREE_CONST struct device *dev_find_path( |
| 63 | DEVTREE_CONST struct device *prev_match, |
| 64 | enum device_path_type path_type) |
| 65 | { |
| 66 | DEVTREE_CONST struct device *dev, *result = NULL; |
| 67 | |
| 68 | if (prev_match == NULL) |
| 69 | prev_match = all_devices; |
| 70 | else |
| 71 | prev_match = prev_match->next; |
| 72 | |
| 73 | for (dev = prev_match; dev; dev = dev->next) { |
| 74 | if (dev->path.type == path_type) { |
| 75 | result = dev; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | /** |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 83 | * Given a device pointer, find the next PCI device. |
| 84 | * |
| 85 | * @param previous_dev A pointer to a PCI device structure. |
| 86 | * @return Pointer to the next device structure (if found), 0 otherwise. |
| 87 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 88 | DEVTREE_CONST struct device *dev_find_next_pci_device( |
| 89 | DEVTREE_CONST struct device *previous_dev) |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 90 | { |
Nico Huber | f6a4344 | 2018-05-15 14:13:32 +0200 | [diff] [blame] | 91 | return dev_find_path(previous_dev, DEVICE_PATH_PCI); |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 92 | } |
| 93 | |
Kyösti Mälkki | 2df1c12 | 2018-05-21 01:52:10 +0300 | [diff] [blame] | 94 | static int path_eq(const struct device_path *path1, |
| 95 | const struct device_path *path2) |
| 96 | { |
| 97 | int equal = 0; |
| 98 | |
| 99 | if (path1->type != path2->type) |
| 100 | return 0; |
| 101 | |
| 102 | switch (path1->type) { |
| 103 | case DEVICE_PATH_NONE: |
| 104 | break; |
| 105 | case DEVICE_PATH_ROOT: |
| 106 | equal = 1; |
| 107 | break; |
| 108 | case DEVICE_PATH_PCI: |
| 109 | equal = (path1->pci.devfn == path2->pci.devfn); |
| 110 | break; |
| 111 | case DEVICE_PATH_PNP: |
| 112 | equal = (path1->pnp.port == path2->pnp.port) && |
| 113 | (path1->pnp.device == path2->pnp.device); |
| 114 | break; |
| 115 | case DEVICE_PATH_I2C: |
| 116 | equal = (path1->i2c.device == path2->i2c.device) && |
| 117 | (path1->i2c.mode_10bit == path2->i2c.mode_10bit); |
| 118 | break; |
| 119 | case DEVICE_PATH_APIC: |
| 120 | equal = (path1->apic.apic_id == path2->apic.apic_id); |
| 121 | break; |
| 122 | case DEVICE_PATH_DOMAIN: |
| 123 | equal = (path1->domain.domain == path2->domain.domain); |
| 124 | break; |
| 125 | case DEVICE_PATH_CPU_CLUSTER: |
| 126 | equal = (path1->cpu_cluster.cluster |
| 127 | == path2->cpu_cluster.cluster); |
| 128 | break; |
| 129 | case DEVICE_PATH_CPU: |
| 130 | equal = (path1->cpu.id == path2->cpu.id); |
| 131 | break; |
| 132 | case DEVICE_PATH_CPU_BUS: |
| 133 | equal = (path1->cpu_bus.id == path2->cpu_bus.id); |
| 134 | break; |
| 135 | case DEVICE_PATH_GENERIC: |
| 136 | equal = (path1->generic.id == path2->generic.id) && |
| 137 | (path1->generic.subid == path2->generic.subid); |
| 138 | break; |
| 139 | case DEVICE_PATH_SPI: |
| 140 | equal = (path1->spi.cs == path2->spi.cs); |
| 141 | break; |
| 142 | case DEVICE_PATH_USB: |
| 143 | equal = (path1->usb.port_type == path2->usb.port_type) && |
| 144 | (path1->usb.port_id == path2->usb.port_id); |
| 145 | break; |
| 146 | case DEVICE_PATH_MMIO: |
| 147 | equal = (path1->mmio.addr == path2->mmio.addr); |
| 148 | break; |
| 149 | default: |
| 150 | printk(BIOS_ERR, "Unknown device type: %d\n", path1->type); |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | return equal; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * See if a device structure exists for path. |
| 159 | * |
| 160 | * @param parent The bus to find the device on. |
| 161 | * @param path The relative path from the bus to the appropriate device. |
| 162 | * @return Pointer to a device structure for the device on bus at path |
| 163 | * or 0/NULL if no device is found. |
| 164 | */ |
| 165 | DEVTREE_CONST struct device *find_dev_path( |
| 166 | const struct bus *parent, const struct device_path *path) |
| 167 | { |
| 168 | DEVTREE_CONST struct device *child; |
| 169 | for (child = parent->children; child; child = child->sibling) { |
| 170 | if (path_eq(path, &child->path)) |
| 171 | break; |
| 172 | } |
| 173 | return child; |
| 174 | } |
| 175 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 176 | DEVTREE_CONST struct device *pcidev_path_behind( |
| 177 | const struct bus *parent, pci_devfn_t devfn) |
| 178 | { |
| 179 | const struct device_path path = { |
| 180 | .type = DEVICE_PATH_PCI, |
| 181 | .pci.devfn = devfn, |
| 182 | }; |
| 183 | return find_dev_path(parent, &path); |
| 184 | } |
| 185 | |
Kyösti Mälkki | ab275f0 | 2019-07-04 07:16:59 +0300 | [diff] [blame] | 186 | DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn) |
| 187 | { |
| 188 | DEVTREE_CONST struct bus *parent = pci_root_bus(); |
| 189 | DEVTREE_CONST struct device *dev = parent->children; |
| 190 | |
| 191 | /* FIXME: Write the loop with topology links. */ |
| 192 | while (dev) { |
| 193 | if (dev->path.type != DEVICE_PATH_PCI) { |
| 194 | dev = dev->next; |
| 195 | continue; |
| 196 | } |
| 197 | if (dev->bus->secondary == bus) |
| 198 | return pcidev_path_behind(dev->bus, devfn); |
| 199 | dev = dev->next; |
| 200 | } |
| 201 | return NULL; |
| 202 | } |
| 203 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 204 | DEVTREE_CONST struct bus *pci_root_bus(void) |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 205 | { |
| 206 | DEVTREE_CONST struct device *pci_domain; |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 207 | MAYBE_STATIC DEVTREE_CONST struct bus *pci_root = NULL; |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 208 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 209 | if (pci_root) |
| 210 | return pci_root; |
| 211 | |
| 212 | pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN); |
| 213 | if (!pci_domain) |
| 214 | return NULL; |
| 215 | |
| 216 | pci_root = pci_domain->link_list; |
| 217 | return pci_root; |
| 218 | } |
| 219 | |
| 220 | DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn) |
| 221 | { |
Kyösti Mälkki | db866ba | 2019-01-06 17:40:36 +0200 | [diff] [blame] | 222 | /* Work around pcidev_path_behind() below failing |
| 223 | * due tue complicated devicetree with topology |
| 224 | * being manipulated on-the-fly. |
| 225 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 226 | if (CONFIG(NORTHBRIDGE_AMD_AMDFAM10)) |
Kyösti Mälkki | db866ba | 2019-01-06 17:40:36 +0200 | [diff] [blame] | 227 | return dev_find_slot(0, devfn); |
| 228 | |
Kyösti Mälkki | fffc9f3 | 2019-07-03 09:55:01 +0300 | [diff] [blame] | 229 | return pcidev_path_behind(pci_root_bus(), devfn); |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn) |
| 233 | { |
| 234 | return pcidev_path_on_root(PCI_DEVFN(dev, fn)); |
| 235 | } |
| 236 | |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 237 | /** |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 238 | * Given an SMBus bus and a device number, find the device structure. |
| 239 | * |
| 240 | * @param bus The bus number. |
| 241 | * @param addr A device number. |
| 242 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 243 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 244 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 245 | unsigned int addr) |
| 246 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 247 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 248 | |
| 249 | result = 0; |
| 250 | for (dev = all_devices; dev; dev = dev->next) { |
| 251 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 252 | (dev->bus->secondary == bus) && |
| 253 | (dev->path.i2c.device == addr)) { |
| 254 | result = dev; |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | return result; |
| 259 | } |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Given a PnP port and a device number, find the device structure. |
| 263 | * |
| 264 | * @param port The I/O port. |
| 265 | * @param device Logical device number. |
| 266 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 267 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 268 | 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] | 269 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 270 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 271 | |
| 272 | for (dev = all_devices; dev; dev = dev->next) { |
| 273 | if ((dev->path.type == DEVICE_PATH_PNP) && |
| 274 | (dev->path.pnp.port == port) && |
| 275 | (dev->path.pnp.device == device)) { |
| 276 | return dev; |
| 277 | } |
| 278 | } |
| 279 | return 0; |
| 280 | } |
Aaron Durbin | a0dabd1 | 2018-07-25 08:49:19 -0600 | [diff] [blame] | 281 | |
| 282 | /** |
| 283 | * Given a device and previous match iterate through all the children. |
| 284 | * |
| 285 | * @param bus parent device's bus holding all the children |
| 286 | * @param prev_child previous child already traversed, if NULL start at |
| 287 | * children of parent bus. |
| 288 | * @return pointer to child or NULL when no more children |
| 289 | */ |
| 290 | DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent, |
| 291 | DEVTREE_CONST struct device *prev_child) |
| 292 | { |
| 293 | DEVTREE_CONST struct device *dev; |
| 294 | |
| 295 | if (parent == NULL) |
| 296 | return NULL; |
| 297 | |
| 298 | if (prev_child == NULL) |
| 299 | dev = parent->children; |
| 300 | else |
| 301 | dev = prev_child->sibling; |
| 302 | |
| 303 | return dev; |
| 304 | } |