blob: 5288a743b6ad17e6fe593e89d5c43b94d843c7e7 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer57879c92012-07-31 16:47:25 -07002
Furquan Shaikh86803782020-04-16 23:13:28 -07003#include <assert.h>
Kyösti Mälkki2df1c122018-05-21 01:52:10 +03004#include <console/console.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -07005#include <device/device.h>
6#include <device/path.h>
7#include <device/pci.h>
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +03008#include <device/pci_def.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -07009#include <device/resource.h>
10
11/** Linked list of ALL devices */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050012DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070013
14/**
15 * Given a PCI bus and a devfn number, find the device structure.
16 *
Aaron Durbin55ef0d22019-09-26 12:05:27 -060017 * 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 Reinauer57879c92012-07-31 16:47:25 -070023 * @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älkkibd585fa2019-07-03 07:51:43 +030027
28static DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070029 unsigned int devfn)
30{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050031 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070032
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 Huberf6a43442018-05-15 14:13:32 +020046 * 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 */
52DEVTREE_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 Shaikh1f3055a2020-04-20 17:59:50 -070072DEVTREE_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 Huberf6a43442018-05-15 14:13:32 +020085/**
Martin Roth16d953a2014-05-12 17:38:59 -060086 * 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 Durbine4d7abc2017-04-16 22:05:36 -050091DEVTREE_CONST struct device *dev_find_next_pci_device(
92 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060093{
Nico Huberf6a43442018-05-15 14:13:32 +020094 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060095}
96
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030097static int path_eq(const struct device_path *path1,
98 const struct device_path *path2)
99{
100 int equal = 0;
101
Furquan Shaikh86803782020-04-16 23:13:28 -0700102 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älkki2df1c122018-05-21 01:52:10 +0300109 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 Rangel3f3f53c2020-05-06 11:47:04 -0600159 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öhnerdbb667a2020-12-11 21:26:02 +0100165 case DEVICE_PATH_GPIO:
166 equal = (path1->gpio.id == path2->gpio.id);
167 break;
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300168 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 */
184DEVTREE_CONST struct device *find_dev_path(
185 const struct bus *parent, const struct device_path *path)
186{
187 DEVTREE_CONST struct device *child;
Furquan Shaikh86803782020-04-16 23:13:28 -0700188
189 if (!parent) {
Julius Werner3e034b62020-07-29 17:39:21 -0700190 BUG();
Furquan Shaikh86803782020-04-16 23:13:28 -0700191 /* Return NULL in case asserts are considered non-fatal. */
192 return NULL;
193 }
194
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300195 for (child = parent->children; child; child = child->sibling) {
196 if (path_eq(path, &child->path))
197 break;
198 }
199 return child;
200}
201
Rob Barnesa223e652020-07-23 08:25:42 -0600202/**
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 */
211DEVTREE_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älkkiad7674e2018-05-20 10:31:23 +0300229DEVTREE_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älkkiab275f02019-07-04 07:16:59 +0300239DEVTREE_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älkkifffc9f32019-07-03 09:55:01 +0300257DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300258{
259 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +0300260 static DEVTREE_CONST struct bus *pci_root;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300261
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300262 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
273DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
274{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300275 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300276}
277
278DEVTREE_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 Shaikh7778e5c2020-04-16 08:25:58 -0700283DEVTREE_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 Werner3e034b62020-07-29 17:39:21 -0700288 BUG();
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700289 /* 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älkkif2ac0132019-07-12 15:26:29 +0300296DEVTREE_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älkki4323d262019-07-12 16:20:14 +0300302 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300303
304 /* FIXME: This can return wrong device. */
305 return dev_find_slot(0, devfn);
306}
307
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300308void 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
313void __noreturn devtree_die(void)
314{
315 die("DEVTREE: dev or chip_info is NULL\n");
316}
317
Martin Roth16d953a2014-05-12 17:38:59 -0600318/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700319 * 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 Durbine4d7abc2017-04-16 22:05:36 -0500325DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700326 unsigned int addr)
327{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500328 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700329
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älkki96643452015-01-28 22:03:46 +0200341
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 Durbine4d7abc2017-04-16 22:05:36 -0500349DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200350{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500351 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200352
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 Durbina0dabd12018-07-25 08:49:19 -0600362
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 */
371DEVTREE_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}