blob: 12d5386d2255179f9b6eed2584a4f3ae5a3c6e04 [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
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300199DEVTREE_CONST struct device *pcidev_path_behind(
200 const struct bus *parent, pci_devfn_t devfn)
201{
202 const struct device_path path = {
203 .type = DEVICE_PATH_PCI,
204 .pci.devfn = devfn,
205 };
206 return find_dev_path(parent, &path);
207}
208
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300209DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
210{
211 DEVTREE_CONST struct bus *parent = pci_root_bus();
212 DEVTREE_CONST struct device *dev = parent->children;
213
214 /* FIXME: Write the loop with topology links. */
215 while (dev) {
216 if (dev->path.type != DEVICE_PATH_PCI) {
217 dev = dev->next;
218 continue;
219 }
220 if (dev->bus->secondary == bus)
221 return pcidev_path_behind(dev->bus, devfn);
222 dev = dev->next;
223 }
224 return NULL;
225}
226
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300227DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300228{
229 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkkifcbbb912020-04-20 10:21:39 +0300230 static DEVTREE_CONST struct bus *pci_root;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300231
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300232 if (pci_root)
233 return pci_root;
234
235 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
236 if (!pci_domain)
237 return NULL;
238
239 pci_root = pci_domain->link_list;
240 return pci_root;
241}
242
243DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
244{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300245 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300246}
247
248DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
249{
250 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
251}
252
Furquan Shaikh7778e5c2020-04-16 08:25:58 -0700253DEVTREE_CONST struct device *pcidev_path_behind_pci2pci_bridge(
254 const struct device *bridge,
255 pci_devfn_t devfn)
256{
257 if (!bridge || (bridge->path.type != DEVICE_PATH_PCI)) {
258 assert(0);
259 /* Return NULL in case asserts are non-fatal. */
260 return NULL;
261 }
262
263 return pcidev_path_behind(bridge->link_list, devfn);
264}
265
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300266DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
267{
268 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
269 if (dev)
270 return dev;
271
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300272 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300273
274 /* FIXME: This can return wrong device. */
275 return dev_find_slot(0, devfn);
276}
277
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300278void devtree_bug(const char *func, pci_devfn_t devfn)
279{
280 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
281}
282
283void __noreturn devtree_die(void)
284{
285 die("DEVTREE: dev or chip_info is NULL\n");
286}
287
Martin Roth16d953a2014-05-12 17:38:59 -0600288/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700289 * Given an SMBus bus and a device number, find the device structure.
290 *
291 * @param bus The bus number.
292 * @param addr A device number.
293 * @return Pointer to the device structure (if found), 0 otherwise.
294 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500295DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700296 unsigned int addr)
297{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500298 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700299
300 result = 0;
301 for (dev = all_devices; dev; dev = dev->next) {
302 if ((dev->path.type == DEVICE_PATH_I2C) &&
303 (dev->bus->secondary == bus) &&
304 (dev->path.i2c.device == addr)) {
305 result = dev;
306 break;
307 }
308 }
309 return result;
310}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200311
312/**
313 * Given a PnP port and a device number, find the device structure.
314 *
315 * @param port The I/O port.
316 * @param device Logical device number.
317 * @return Pointer to the device structure (if found), 0 otherwise.
318 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500319DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200320{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500321 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200322
323 for (dev = all_devices; dev; dev = dev->next) {
324 if ((dev->path.type == DEVICE_PATH_PNP) &&
325 (dev->path.pnp.port == port) &&
326 (dev->path.pnp.device == device)) {
327 return dev;
328 }
329 }
330 return 0;
331}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600332
333/**
334 * Given a device and previous match iterate through all the children.
335 *
336 * @param bus parent device's bus holding all the children
337 * @param prev_child previous child already traversed, if NULL start at
338 * children of parent bus.
339 * @return pointer to child or NULL when no more children
340 */
341DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
342 DEVTREE_CONST struct device *prev_child)
343{
344 DEVTREE_CONST struct device *dev;
345
346 if (parent == NULL)
347 return NULL;
348
349 if (prev_child == NULL)
350 dev = parent->children;
351 else
352 dev = prev_child->sibling;
353
354 return dev;
355}