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 | |
| 186 | DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn) |
| 187 | { |
| 188 | DEVTREE_CONST struct device *pci_domain; |
| 189 | |
Kyösti Mälkki | db866ba | 2019-01-06 17:40:36 +0200 | [diff] [blame] | 190 | /* Work around pcidev_path_behind() below failing |
| 191 | * due tue complicated devicetree with topology |
| 192 | * being manipulated on-the-fly. |
| 193 | */ |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame^] | 194 | if (CONFIG(NORTHBRIDGE_AMD_AMDFAM10)) |
Kyösti Mälkki | db866ba | 2019-01-06 17:40:36 +0200 | [diff] [blame] | 195 | return dev_find_slot(0, devfn); |
| 196 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 197 | pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN); |
| 198 | if (!pci_domain) |
| 199 | return NULL; |
Kyösti Mälkki | db866ba | 2019-01-06 17:40:36 +0200 | [diff] [blame] | 200 | |
Kyösti Mälkki | ad7674e | 2018-05-20 10:31:23 +0300 | [diff] [blame] | 201 | return pcidev_path_behind(pci_domain->link_list, devfn); |
| 202 | } |
| 203 | |
| 204 | DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn) |
| 205 | { |
| 206 | return pcidev_path_on_root(PCI_DEVFN(dev, fn)); |
| 207 | } |
| 208 | |
Martin Roth | 16d953a | 2014-05-12 17:38:59 -0600 | [diff] [blame] | 209 | /** |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 210 | * Given an SMBus bus and a device number, find the device structure. |
| 211 | * |
| 212 | * @param bus The bus number. |
| 213 | * @param addr A device number. |
| 214 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 215 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 216 | DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus, |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 217 | unsigned int addr) |
| 218 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 219 | DEVTREE_CONST struct device *dev, *result; |
Stefan Reinauer | 57879c9 | 2012-07-31 16:47:25 -0700 | [diff] [blame] | 220 | |
| 221 | result = 0; |
| 222 | for (dev = all_devices; dev; dev = dev->next) { |
| 223 | if ((dev->path.type == DEVICE_PATH_I2C) && |
| 224 | (dev->bus->secondary == bus) && |
| 225 | (dev->path.i2c.device == addr)) { |
| 226 | result = dev; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | return result; |
| 231 | } |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 232 | |
| 233 | /** |
| 234 | * Given a PnP port and a device number, find the device structure. |
| 235 | * |
| 236 | * @param port The I/O port. |
| 237 | * @param device Logical device number. |
| 238 | * @return Pointer to the device structure (if found), 0 otherwise. |
| 239 | */ |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 240 | 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] | 241 | { |
Aaron Durbin | e4d7abc | 2017-04-16 22:05:36 -0500 | [diff] [blame] | 242 | DEVTREE_CONST struct device *dev; |
Kyösti Mälkki | 9664345 | 2015-01-28 22:03:46 +0200 | [diff] [blame] | 243 | |
| 244 | for (dev = all_devices; dev; dev = dev->next) { |
| 245 | if ((dev->path.type == DEVICE_PATH_PNP) && |
| 246 | (dev->path.pnp.port == port) && |
| 247 | (dev->path.pnp.device == device)) { |
| 248 | return dev; |
| 249 | } |
| 250 | } |
| 251 | return 0; |
| 252 | } |
Aaron Durbin | a0dabd1 | 2018-07-25 08:49:19 -0600 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * Given a device and previous match iterate through all the children. |
| 256 | * |
| 257 | * @param bus parent device's bus holding all the children |
| 258 | * @param prev_child previous child already traversed, if NULL start at |
| 259 | * children of parent bus. |
| 260 | * @return pointer to child or NULL when no more children |
| 261 | */ |
| 262 | DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent, |
| 263 | DEVTREE_CONST struct device *prev_child) |
| 264 | { |
| 265 | DEVTREE_CONST struct device *dev; |
| 266 | |
| 267 | if (parent == NULL) |
| 268 | return NULL; |
| 269 | |
| 270 | if (prev_child == NULL) |
| 271 | dev = parent->children; |
| 272 | else |
| 273 | dev = prev_child->sibling; |
| 274 | |
| 275 | return dev; |
| 276 | } |