blob: add253b5110099b45d3917979060fc43c41135ac [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer57879c92012-07-31 16:47:25 -07003
Furquan Shaikh86803782020-04-16 23:13:28 -07004#include <assert.h>
Kyösti Mälkki2df1c122018-05-21 01:52:10 +03005#include <console/console.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -07006#include <device/device.h>
7#include <device/path.h>
8#include <device/pci.h>
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +03009#include <device/pci_def.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070010#include <device/resource.h>
11
12/** Linked list of ALL devices */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050013DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070014
15/**
16 * Given a PCI bus and a devfn number, find the device structure.
17 *
Aaron Durbin55ef0d22019-09-26 12:05:27 -060018 * Note that this function can return the incorrect device prior
19 * to PCI enumeration because the secondary field of the bus object
20 * is 0. The failing scenario is determined by the order of the
21 * devices in all_devices singly-linked list as well as the time
22 * when this function is called (secondary reflecting topology).
23 *
Stefan Reinauer57879c92012-07-31 16:47:25 -070024 * @param bus The bus number.
25 * @param devfn A device/function number.
26 * @return Pointer to the device structure (if found), 0 otherwise.
27 */
Kyösti Mälkkibd585fa2019-07-03 07:51:43 +030028
29static DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070030 unsigned int devfn)
31{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050032 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070033
34 result = 0;
35 for (dev = all_devices; dev; dev = dev->next) {
36 if ((dev->path.type == DEVICE_PATH_PCI) &&
37 (dev->bus->secondary == bus) &&
38 (dev->path.pci.devfn == devfn)) {
39 result = dev;
40 break;
41 }
42 }
43 return result;
44}
45
46/**
Nico Huberf6a43442018-05-15 14:13:32 +020047 * Given a Device Path Type, find the device structure.
48 *
49 * @param prev_match The previously matched device instance.
50 * @param path_type The Device Path Type.
51 * @return Pointer to the device structure (if found), 0 otherwise.
52 */
53DEVTREE_CONST struct device *dev_find_path(
54 DEVTREE_CONST struct device *prev_match,
55 enum device_path_type path_type)
56{
57 DEVTREE_CONST struct device *dev, *result = NULL;
58
59 if (prev_match == NULL)
60 prev_match = all_devices;
61 else
62 prev_match = prev_match->next;
63
64 for (dev = prev_match; dev; dev = dev->next) {
65 if (dev->path.type == path_type) {
66 result = dev;
67 break;
68 }
69 }
70 return result;
71}
72
Furquan Shaikh1f3055a2020-04-20 17:59:50 -070073DEVTREE_CONST struct device *dev_find_matching_device_on_bus(const struct bus *bus,
74 match_device_fn fn)
75{
76 DEVTREE_CONST struct device *child = NULL;
77
78 while ((child = dev_bus_each_child(bus, child)) != NULL) {
79 if (fn(child))
80 break;
81 }
82
83 return child;
84}
85
Nico Huberf6a43442018-05-15 14:13:32 +020086/**
Martin Roth16d953a2014-05-12 17:38:59 -060087 * Given a device pointer, find the next PCI device.
88 *
89 * @param previous_dev A pointer to a PCI device structure.
90 * @return Pointer to the next device structure (if found), 0 otherwise.
91 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050092DEVTREE_CONST struct device *dev_find_next_pci_device(
93 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060094{
Nico Huberf6a43442018-05-15 14:13:32 +020095 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060096}
97
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030098static int path_eq(const struct device_path *path1,
99 const struct device_path *path2)
100{
101 int equal = 0;
102
Furquan Shaikh86803782020-04-16 23:13:28 -0700103 if (!path1 || !path2) {
104 assert(path1);
105 assert(path2);
106 /* Return 0 in case assert is considered non-fatal. */
107 return 0;
108 }
109
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300110 if (path1->type != path2->type)
111 return 0;
112
113 switch (path1->type) {
114 case DEVICE_PATH_NONE:
115 break;
116 case DEVICE_PATH_ROOT:
117 equal = 1;
118 break;
119 case DEVICE_PATH_PCI:
120 equal = (path1->pci.devfn == path2->pci.devfn);
121 break;
122 case DEVICE_PATH_PNP:
123 equal = (path1->pnp.port == path2->pnp.port) &&
124 (path1->pnp.device == path2->pnp.device);
125 break;
126 case DEVICE_PATH_I2C:
127 equal = (path1->i2c.device == path2->i2c.device) &&
128 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
129 break;
130 case DEVICE_PATH_APIC:
131 equal = (path1->apic.apic_id == path2->apic.apic_id);
132 break;
133 case DEVICE_PATH_DOMAIN:
134 equal = (path1->domain.domain == path2->domain.domain);
135 break;
136 case DEVICE_PATH_CPU_CLUSTER:
137 equal = (path1->cpu_cluster.cluster
138 == path2->cpu_cluster.cluster);
139 break;
140 case DEVICE_PATH_CPU:
141 equal = (path1->cpu.id == path2->cpu.id);
142 break;
143 case DEVICE_PATH_CPU_BUS:
144 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
145 break;
146 case DEVICE_PATH_GENERIC:
147 equal = (path1->generic.id == path2->generic.id) &&
148 (path1->generic.subid == path2->generic.subid);
149 break;
150 case DEVICE_PATH_SPI:
151 equal = (path1->spi.cs == path2->spi.cs);
152 break;
153 case DEVICE_PATH_USB:
154 equal = (path1->usb.port_type == path2->usb.port_type) &&
155 (path1->usb.port_id == path2->usb.port_id);
156 break;
157 case DEVICE_PATH_MMIO:
158 equal = (path1->mmio.addr == path2->mmio.addr);
159 break;
160 default:
161 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
162 break;
163 }
164
165 return equal;
166}
167
168/**
169 * See if a device structure exists for path.
170 *
171 * @param parent The bus to find the device on.
172 * @param path The relative path from the bus to the appropriate device.
173 * @return Pointer to a device structure for the device on bus at path
174 * or 0/NULL if no device is found.
175 */
176DEVTREE_CONST struct device *find_dev_path(
177 const struct bus *parent, const struct device_path *path)
178{
179 DEVTREE_CONST struct device *child;
Furquan Shaikh86803782020-04-16 23:13:28 -0700180
181 if (!parent) {
182 assert(0);
183 /* Return NULL in case asserts are considered non-fatal. */
184 return NULL;
185 }
186
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300187 for (child = parent->children; child; child = child->sibling) {
188 if (path_eq(path, &child->path))
189 break;
190 }
191 return child;
192}
193
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300194DEVTREE_CONST struct device *pcidev_path_behind(
195 const struct bus *parent, pci_devfn_t devfn)
196{
197 const struct device_path path = {
198 .type = DEVICE_PATH_PCI,
199 .pci.devfn = devfn,
200 };
201 return find_dev_path(parent, &path);
202}
203
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300204DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
205{
206 DEVTREE_CONST struct bus *parent = pci_root_bus();
207 DEVTREE_CONST struct device *dev = parent->children;
208
209 /* FIXME: Write the loop with topology links. */
210 while (dev) {
211 if (dev->path.type != DEVICE_PATH_PCI) {
212 dev = dev->next;
213 continue;
214 }
215 if (dev->bus->secondary == bus)
216 return pcidev_path_behind(dev->bus, devfn);
217 dev = dev->next;
218 }
219 return NULL;
220}
221
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300222DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300223{
224 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkki117cf2b2019-08-20 06:01:57 +0300225 MAYBE_STATIC_BSS DEVTREE_CONST struct bus *pci_root = NULL;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300226
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300227 if (pci_root)
228 return pci_root;
229
230 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
231 if (!pci_domain)
232 return NULL;
233
234 pci_root = pci_domain->link_list;
235 return pci_root;
236}
237
238DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
239{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300240 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300241}
242
243DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
244{
245 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
246}
247
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700248DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
249 const struct device *bridge,
250 pci_devfn_t devfn)
251{
252 if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) {
253 assert(0);
254 /* Return NULL in case asserts are non-fatal. */
255 return NULL;
256 }
257
258 return pcidev_path_behind(bridge->link_list, devfn);
259}
260
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300261DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
262{
263 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
264 if (dev)
265 return dev;
266
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300267 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300268
269 /* FIXME: This can return wrong device. */
270 return dev_find_slot(0, devfn);
271}
272
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300273void devtree_bug(const char *func, pci_devfn_t devfn)
274{
275 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
276}
277
278void __noreturn devtree_die(void)
279{
280 die("DEVTREE: dev or chip_info is NULL\n");
281}
282
Martin Roth16d953a2014-05-12 17:38:59 -0600283/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700284 * Given an SMBus bus and a device number, find the device structure.
285 *
286 * @param bus The bus number.
287 * @param addr A device number.
288 * @return Pointer to the device structure (if found), 0 otherwise.
289 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500290DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700291 unsigned int addr)
292{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500293 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700294
295 result = 0;
296 for (dev = all_devices; dev; dev = dev->next) {
297 if ((dev->path.type == DEVICE_PATH_I2C) &&
298 (dev->bus->secondary == bus) &&
299 (dev->path.i2c.device == addr)) {
300 result = dev;
301 break;
302 }
303 }
304 return result;
305}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200306
307/**
308 * Given a PnP port and a device number, find the device structure.
309 *
310 * @param port The I/O port.
311 * @param device Logical device number.
312 * @return Pointer to the device structure (if found), 0 otherwise.
313 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500314DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200315{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500316 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200317
318 for (dev = all_devices; dev; dev = dev->next) {
319 if ((dev->path.type == DEVICE_PATH_PNP) &&
320 (dev->path.pnp.port == port) &&
321 (dev->path.pnp.device == device)) {
322 return dev;
323 }
324 }
325 return 0;
326}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600327
328/**
329 * Given a device and previous match iterate through all the children.
330 *
331 * @param bus parent device's bus holding all the children
332 * @param prev_child previous child already traversed, if NULL start at
333 * children of parent bus.
334 * @return pointer to child or NULL when no more children
335 */
336DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
337 DEVTREE_CONST struct device *prev_child)
338{
339 DEVTREE_CONST struct device *dev;
340
341 if (parent == NULL)
342 return NULL;
343
344 if (prev_child == NULL)
345 dev = parent->children;
346 else
347 dev = prev_child->sibling;
348
349 return dev;
350}