blob: a63a629344422a8132b9af5696f24be609b8e983 [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>
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +03007#include <device/pci_def.h>
Elyes Haouasecb5e2d2022-10-02 13:13:17 +02008#include <device/pci_type.h>
Furquan Shaikh7f6ae792021-05-20 22:47:02 -07009#include <fw_config.h>
Elyes Haouasecb5e2d2022-10-02 13:13:17 +020010#include <types.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070011
12/** Linked list of ALL devices */
Frans Hendriksa9caa502021-02-01 11:44:37 +010013DEVTREE_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
73/**
Martin Roth16d953a2014-05-12 17:38:59 -060074 * Given a device pointer, find the next PCI device.
75 *
76 * @param previous_dev A pointer to a PCI device structure.
77 * @return Pointer to the next device structure (if found), 0 otherwise.
78 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050079DEVTREE_CONST struct device *dev_find_next_pci_device(
80 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060081{
Nico Huberf6a43442018-05-15 14:13:32 +020082 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060083}
84
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030085static int path_eq(const struct device_path *path1,
86 const struct device_path *path2)
87{
88 int equal = 0;
89
Furquan Shaikh86803782020-04-16 23:13:28 -070090 if (!path1 || !path2) {
91 assert(path1);
92 assert(path2);
93 /* Return 0 in case assert is considered non-fatal. */
94 return 0;
95 }
96
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030097 if (path1->type != path2->type)
98 return 0;
99
100 switch (path1->type) {
101 case DEVICE_PATH_NONE:
102 break;
103 case DEVICE_PATH_ROOT:
104 equal = 1;
105 break;
106 case DEVICE_PATH_PCI:
107 equal = (path1->pci.devfn == path2->pci.devfn);
108 break;
109 case DEVICE_PATH_PNP:
110 equal = (path1->pnp.port == path2->pnp.port) &&
111 (path1->pnp.device == path2->pnp.device);
112 break;
113 case DEVICE_PATH_I2C:
114 equal = (path1->i2c.device == path2->i2c.device) &&
115 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
116 break;
117 case DEVICE_PATH_APIC:
118 equal = (path1->apic.apic_id == path2->apic.apic_id);
119 break;
120 case DEVICE_PATH_DOMAIN:
121 equal = (path1->domain.domain == path2->domain.domain);
122 break;
123 case DEVICE_PATH_CPU_CLUSTER:
124 equal = (path1->cpu_cluster.cluster
125 == path2->cpu_cluster.cluster);
126 break;
127 case DEVICE_PATH_CPU:
128 equal = (path1->cpu.id == path2->cpu.id);
129 break;
130 case DEVICE_PATH_CPU_BUS:
131 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
132 break;
133 case DEVICE_PATH_GENERIC:
134 equal = (path1->generic.id == path2->generic.id) &&
135 (path1->generic.subid == path2->generic.subid);
136 break;
137 case DEVICE_PATH_SPI:
138 equal = (path1->spi.cs == path2->spi.cs);
139 break;
140 case DEVICE_PATH_USB:
141 equal = (path1->usb.port_type == path2->usb.port_type) &&
142 (path1->usb.port_id == path2->usb.port_id);
143 break;
144 case DEVICE_PATH_MMIO:
145 equal = (path1->mmio.addr == path2->mmio.addr);
146 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100147 case DEVICE_PATH_GPIO:
148 equal = (path1->gpio.id == path2->gpio.id);
149 break;
Mario Scheithauer67f63e72022-11-02 15:57:10 +0100150 case DEVICE_PATH_MDIO:
151 equal = (path1->mdio.addr == path2->mdio.addr);
152 break;
153
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300154 default:
155 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
156 break;
157 }
158
159 return equal;
160}
161
162/**
163 * See if a device structure exists for path.
164 *
165 * @param parent The bus to find the device on.
166 * @param path The relative path from the bus to the appropriate device.
167 * @return Pointer to a device structure for the device on bus at path
168 * or 0/NULL if no device is found.
169 */
170DEVTREE_CONST struct device *find_dev_path(
171 const struct bus *parent, const struct device_path *path)
172{
173 DEVTREE_CONST struct device *child;
Furquan Shaikh86803782020-04-16 23:13:28 -0700174
175 if (!parent) {
Julius Werner3e034b62020-07-29 17:39:21 -0700176 BUG();
Furquan Shaikh86803782020-04-16 23:13:28 -0700177 /* Return NULL in case asserts are considered non-fatal. */
178 return NULL;
179 }
180
Kyösti Mälkki2df1c122018-05-21 01:52:10 +0300181 for (child = parent->children; child; child = child->sibling) {
182 if (path_eq(path, &child->path))
183 break;
184 }
185 return child;
186}
187
Rob Barnesa223e652020-07-23 08:25:42 -0600188/**
189 * Find the device structure given an array of nested device paths,
190 *
191 * @param parent The parent bus to start the search on.
192 * @param nested_path An array of relative paths from the parent bus to the target device.
193 * @param nested_path_length Number of path elements in nested_path array.
194 * @return Pointer to a device structure for the device at nested path
195 * or 0/NULL if no device is found.
196 */
197DEVTREE_CONST struct device *find_dev_nested_path(
198 const struct bus *parent, const struct device_path nested_path[],
199 size_t nested_path_length)
200{
201 DEVTREE_CONST struct device *child;
202
203 if (!parent || !nested_path || !nested_path_length)
204 return NULL;
205
206 child = find_dev_path(parent, nested_path);
207
208 /* Terminate recursion at end of nested path or child not found */
209 if (nested_path_length == 1 || !child)
210 return child;
211
212 return find_dev_nested_path(child->link_list, nested_path + 1, nested_path_length - 1);
213}
214
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300215DEVTREE_CONST struct device *pcidev_path_behind(
216 const struct bus *parent, pci_devfn_t devfn)
217{
218 const struct device_path path = {
219 .type = DEVICE_PATH_PCI,
220 .pci.devfn = devfn,
221 };
222 return find_dev_path(parent, &path);
223}
224
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300225DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
226{
227 DEVTREE_CONST struct bus *parent = pci_root_bus();
228 DEVTREE_CONST struct device *dev = parent->children;
229
230 /* FIXME: Write the loop with topology links. */
231 while (dev) {
232 if (dev->path.type != DEVICE_PATH_PCI) {
233 dev = dev->next;
234 continue;
235 }
236 if (dev->bus->secondary == bus)
237 return pcidev_path_behind(dev->bus, devfn);
238 dev = dev->next;
239 }
240 return NULL;
241}
242
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300243DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300244{
245 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +0300246 static DEVTREE_CONST struct bus *pci_root;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300247
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300248 if (pci_root)
249 return pci_root;
250
251 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
252 if (!pci_domain)
253 return NULL;
254
255 pci_root = pci_domain->link_list;
256 return pci_root;
257}
258
259DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
260{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300261 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300262}
263
264DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
265{
266 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
267}
268
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700269DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
270 const struct device *bridge,
271 pci_devfn_t devfn)
272{
273 if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) {
Julius Werner3e034b62020-07-29 17:39:21 -0700274 BUG();
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700275 /* Return NULL in case asserts are non-fatal. */
276 return NULL;
277 }
278
279 return pcidev_path_behind(bridge->link_list, devfn);
280}
281
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300282DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
283{
284 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
285 if (dev)
286 return dev;
287
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300288 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300289
290 /* FIXME: This can return wrong device. */
291 return dev_find_slot(0, devfn);
292}
293
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300294void devtree_bug(const char *func, pci_devfn_t devfn)
295{
296 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
297}
298
299void __noreturn devtree_die(void)
300{
301 die("DEVTREE: dev or chip_info is NULL\n");
302}
303
Martin Roth16d953a2014-05-12 17:38:59 -0600304/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700305 * Given an SMBus bus and a device number, find the device structure.
306 *
307 * @param bus The bus number.
308 * @param addr A device number.
309 * @return Pointer to the device structure (if found), 0 otherwise.
310 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500311DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700312 unsigned int addr)
313{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500314 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700315
316 result = 0;
317 for (dev = all_devices; dev; dev = dev->next) {
318 if ((dev->path.type == DEVICE_PATH_I2C) &&
319 (dev->bus->secondary == bus) &&
320 (dev->path.i2c.device == addr)) {
321 result = dev;
322 break;
323 }
324 }
325 return result;
326}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200327
328/**
329 * Given a PnP port and a device number, find the device structure.
330 *
331 * @param port The I/O port.
332 * @param device Logical device number.
333 * @return Pointer to the device structure (if found), 0 otherwise.
334 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500335DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200336{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500337 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200338
339 for (dev = all_devices; dev; dev = dev->next) {
340 if ((dev->path.type == DEVICE_PATH_PNP) &&
341 (dev->path.pnp.port == port) &&
342 (dev->path.pnp.device == device)) {
343 return dev;
344 }
345 }
346 return 0;
347}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600348
349/**
350 * Given a device and previous match iterate through all the children.
351 *
352 * @param bus parent device's bus holding all the children
353 * @param prev_child previous child already traversed, if NULL start at
354 * children of parent bus.
355 * @return pointer to child or NULL when no more children
356 */
357DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
358 DEVTREE_CONST struct device *prev_child)
359{
360 DEVTREE_CONST struct device *dev;
361
362 if (parent == NULL)
363 return NULL;
364
365 if (prev_child == NULL)
366 dev = parent->children;
367 else
368 dev = prev_child->sibling;
369
370 return dev;
371}
Furquan Shaikh7f6ae792021-05-20 22:47:02 -0700372
373bool is_dev_enabled(const struct device *dev)
374{
375 if (!dev)
376 return false;
377
378 /* For stages with immutable device tree, first check if device is disabled because of
379 fw_config probing. In these stages, dev->enabled does not reflect the true state of a
380 device that uses fw_config probing. */
381 if (DEVTREE_EARLY && !fw_config_probe_dev(dev, NULL))
382 return false;
383 return dev->enabled;
384}
Subrata Banikeca3e622021-06-08 00:55:04 +0530385
386bool is_devfn_enabled(unsigned int devfn)
387{
388 const struct device *dev = pcidev_path_on_root(devfn);
389 return is_dev_enabled(dev);
390}