blob: 79f025da97319539a3d2de209b3804fae874705d [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;
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300165 default:
166 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
167 break;
168 }
169
170 return equal;
171}
172
173/**
174 * See if a device structure exists for path.
175 *
176 * @param parent The bus to find the device on.
177 * @param path The relative path from the bus to the appropriate device.
178 * @return Pointer to a device structure for the device on bus at path
179 * or 0/NULL if no device is found.
180 */
181DEVTREE_CONST struct device *find_dev_path(
182 const struct bus *parent, const struct device_path *path)
183{
184 DEVTREE_CONST struct device *child;
Furquan Shaikh86803782020-04-16 23:13:28 -0700185
186 if (!parent) {
187 assert(0);
188 /* Return NULL in case asserts are considered non-fatal. */
189 return NULL;
190 }
191
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300192 for (child = parent->children; child; child = child->sibling) {
193 if (path_eq(path, &child->path))
194 break;
195 }
196 return child;
197}
198
Rob Barnesa223e652020-07-23 08:25:42 -0600199/**
200 * Find the device structure given an array of nested device paths,
201 *
202 * @param parent The parent bus to start the search on.
203 * @param nested_path An array of relative paths from the parent bus to the target device.
204 * @param nested_path_length Number of path elements in nested_path array.
205 * @return Pointer to a device structure for the device at nested path
206 * or 0/NULL if no device is found.
207 */
208DEVTREE_CONST struct device *find_dev_nested_path(
209 const struct bus *parent, const struct device_path nested_path[],
210 size_t nested_path_length)
211{
212 DEVTREE_CONST struct device *child;
213
214 if (!parent || !nested_path || !nested_path_length)
215 return NULL;
216
217 child = find_dev_path(parent, nested_path);
218
219 /* Terminate recursion at end of nested path or child not found */
220 if (nested_path_length == 1 || !child)
221 return child;
222
223 return find_dev_nested_path(child->link_list, nested_path + 1, nested_path_length - 1);
224}
225
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300226DEVTREE_CONST struct device *pcidev_path_behind(
227 const struct bus *parent, pci_devfn_t devfn)
228{
229 const struct device_path path = {
230 .type = DEVICE_PATH_PCI,
231 .pci.devfn = devfn,
232 };
233 return find_dev_path(parent, &path);
234}
235
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300236DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
237{
238 DEVTREE_CONST struct bus *parent = pci_root_bus();
239 DEVTREE_CONST struct device *dev = parent->children;
240
241 /* FIXME: Write the loop with topology links. */
242 while (dev) {
243 if (dev->path.type != DEVICE_PATH_PCI) {
244 dev = dev->next;
245 continue;
246 }
247 if (dev->bus->secondary == bus)
248 return pcidev_path_behind(dev->bus, devfn);
249 dev = dev->next;
250 }
251 return NULL;
252}
253
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300254DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300255{
256 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +0300257 static DEVTREE_CONST struct bus *pci_root;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300258
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300259 if (pci_root)
260 return pci_root;
261
262 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
263 if (!pci_domain)
264 return NULL;
265
266 pci_root = pci_domain->link_list;
267 return pci_root;
268}
269
270DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
271{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300272 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300273}
274
275DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
276{
277 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
278}
279
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700280DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
281 const struct device *bridge,
282 pci_devfn_t devfn)
283{
284 if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) {
285 assert(0);
286 /* Return NULL in case asserts are non-fatal. */
287 return NULL;
288 }
289
290 return pcidev_path_behind(bridge->link_list, devfn);
291}
292
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300293DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
294{
295 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
296 if (dev)
297 return dev;
298
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300299 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300300
301 /* FIXME: This can return wrong device. */
302 return dev_find_slot(0, devfn);
303}
304
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300305void devtree_bug(const char *func, pci_devfn_t devfn)
306{
307 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
308}
309
310void __noreturn devtree_die(void)
311{
312 die("DEVTREE: dev or chip_info is NULL\n");
313}
314
Martin Roth16d953a2014-05-12 17:38:59 -0600315/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700316 * Given an SMBus bus and a device number, find the device structure.
317 *
318 * @param bus The bus number.
319 * @param addr A device number.
320 * @return Pointer to the device structure (if found), 0 otherwise.
321 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500322DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700323 unsigned int addr)
324{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500325 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700326
327 result = 0;
328 for (dev = all_devices; dev; dev = dev->next) {
329 if ((dev->path.type == DEVICE_PATH_I2C) &&
330 (dev->bus->secondary == bus) &&
331 (dev->path.i2c.device == addr)) {
332 result = dev;
333 break;
334 }
335 }
336 return result;
337}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200338
339/**
340 * Given a PnP port and a device number, find the device structure.
341 *
342 * @param port The I/O port.
343 * @param device Logical device number.
344 * @return Pointer to the device structure (if found), 0 otherwise.
345 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500346DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200347{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500348 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200349
350 for (dev = all_devices; dev; dev = dev->next) {
351 if ((dev->path.type == DEVICE_PATH_PNP) &&
352 (dev->path.pnp.port == port) &&
353 (dev->path.pnp.device == device)) {
354 return dev;
355 }
356 }
357 return 0;
358}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600359
360/**
361 * Given a device and previous match iterate through all the children.
362 *
363 * @param bus parent device's bus holding all the children
364 * @param prev_child previous child already traversed, if NULL start at
365 * children of parent bus.
366 * @return pointer to child or NULL when no more children
367 */
368DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
369 DEVTREE_CONST struct device *prev_child)
370{
371 DEVTREE_CONST struct device *dev;
372
373 if (parent == NULL)
374 return NULL;
375
376 if (prev_child == NULL)
377 dev = parent->children;
378 else
379 dev = prev_child->sibling;
380
381 return dev;
382}