blob: c59c5e9b69ad971f6489aa1a890341c2ade29e9b [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
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
72/**
Martin Roth16d953a2014-05-12 17:38:59 -060073 * Given a device pointer, find the next PCI device.
74 *
75 * @param previous_dev A pointer to a PCI device structure.
76 * @return Pointer to the next device structure (if found), 0 otherwise.
77 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050078DEVTREE_CONST struct device *dev_find_next_pci_device(
79 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060080{
Nico Huberf6a43442018-05-15 14:13:32 +020081 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060082}
83
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030084static int path_eq(const struct device_path *path1,
85 const struct device_path *path2)
86{
87 int equal = 0;
88
89 if (path1->type != path2->type)
90 return 0;
91
92 switch (path1->type) {
93 case DEVICE_PATH_NONE:
94 break;
95 case DEVICE_PATH_ROOT:
96 equal = 1;
97 break;
98 case DEVICE_PATH_PCI:
99 equal = (path1->pci.devfn == path2->pci.devfn);
100 break;
101 case DEVICE_PATH_PNP:
102 equal = (path1->pnp.port == path2->pnp.port) &&
103 (path1->pnp.device == path2->pnp.device);
104 break;
105 case DEVICE_PATH_I2C:
106 equal = (path1->i2c.device == path2->i2c.device) &&
107 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
108 break;
109 case DEVICE_PATH_APIC:
110 equal = (path1->apic.apic_id == path2->apic.apic_id);
111 break;
112 case DEVICE_PATH_DOMAIN:
113 equal = (path1->domain.domain == path2->domain.domain);
114 break;
115 case DEVICE_PATH_CPU_CLUSTER:
116 equal = (path1->cpu_cluster.cluster
117 == path2->cpu_cluster.cluster);
118 break;
119 case DEVICE_PATH_CPU:
120 equal = (path1->cpu.id == path2->cpu.id);
121 break;
122 case DEVICE_PATH_CPU_BUS:
123 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
124 break;
125 case DEVICE_PATH_GENERIC:
126 equal = (path1->generic.id == path2->generic.id) &&
127 (path1->generic.subid == path2->generic.subid);
128 break;
129 case DEVICE_PATH_SPI:
130 equal = (path1->spi.cs == path2->spi.cs);
131 break;
132 case DEVICE_PATH_USB:
133 equal = (path1->usb.port_type == path2->usb.port_type) &&
134 (path1->usb.port_id == path2->usb.port_id);
135 break;
136 case DEVICE_PATH_MMIO:
137 equal = (path1->mmio.addr == path2->mmio.addr);
138 break;
139 default:
140 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
141 break;
142 }
143
144 return equal;
145}
146
147/**
148 * See if a device structure exists for path.
149 *
150 * @param parent The bus to find the device on.
151 * @param path The relative path from the bus to the appropriate device.
152 * @return Pointer to a device structure for the device on bus at path
153 * or 0/NULL if no device is found.
154 */
155DEVTREE_CONST struct device *find_dev_path(
156 const struct bus *parent, const struct device_path *path)
157{
158 DEVTREE_CONST struct device *child;
159 for (child = parent->children; child; child = child->sibling) {
160 if (path_eq(path, &child->path))
161 break;
162 }
163 return child;
164}
165
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300166DEVTREE_CONST struct device *pcidev_path_behind(
167 const struct bus *parent, pci_devfn_t devfn)
168{
169 const struct device_path path = {
170 .type = DEVICE_PATH_PCI,
171 .pci.devfn = devfn,
172 };
173 return find_dev_path(parent, &path);
174}
175
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300176DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
177{
178 DEVTREE_CONST struct bus *parent = pci_root_bus();
179 DEVTREE_CONST struct device *dev = parent->children;
180
181 /* FIXME: Write the loop with topology links. */
182 while (dev) {
183 if (dev->path.type != DEVICE_PATH_PCI) {
184 dev = dev->next;
185 continue;
186 }
187 if (dev->bus->secondary == bus)
188 return pcidev_path_behind(dev->bus, devfn);
189 dev = dev->next;
190 }
191 return NULL;
192}
193
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300194DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300195{
196 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkki117cf2b2019-08-20 06:01:57 +0300197 MAYBE_STATIC_BSS DEVTREE_CONST struct bus *pci_root = NULL;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300198
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300199 if (pci_root)
200 return pci_root;
201
202 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
203 if (!pci_domain)
204 return NULL;
205
206 pci_root = pci_domain->link_list;
207 return pci_root;
208}
209
210DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
211{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300212 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300213}
214
215DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
216{
217 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
218}
219
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300220DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
221{
222 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
223 if (dev)
224 return dev;
225
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300226 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300227
228 /* FIXME: This can return wrong device. */
229 return dev_find_slot(0, devfn);
230}
231
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300232void devtree_bug(const char *func, pci_devfn_t devfn)
233{
234 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
235}
236
237void __noreturn devtree_die(void)
238{
239 die("DEVTREE: dev or chip_info is NULL\n");
240}
241
Martin Roth16d953a2014-05-12 17:38:59 -0600242/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700243 * Given an SMBus bus and a device number, find the device structure.
244 *
245 * @param bus The bus number.
246 * @param addr A device number.
247 * @return Pointer to the device structure (if found), 0 otherwise.
248 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500249DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700250 unsigned int addr)
251{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500252 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700253
254 result = 0;
255 for (dev = all_devices; dev; dev = dev->next) {
256 if ((dev->path.type == DEVICE_PATH_I2C) &&
257 (dev->bus->secondary == bus) &&
258 (dev->path.i2c.device == addr)) {
259 result = dev;
260 break;
261 }
262 }
263 return result;
264}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200265
266/**
267 * Given a PnP port and a device number, find the device structure.
268 *
269 * @param port The I/O port.
270 * @param device Logical device number.
271 * @return Pointer to the device structure (if found), 0 otherwise.
272 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500273DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200274{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500275 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200276
277 for (dev = all_devices; dev; dev = dev->next) {
278 if ((dev->path.type == DEVICE_PATH_PNP) &&
279 (dev->path.pnp.port == port) &&
280 (dev->path.pnp.device == device)) {
281 return dev;
282 }
283 }
284 return 0;
285}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600286
287/**
288 * Given a device and previous match iterate through all the children.
289 *
290 * @param bus parent device's bus holding all the children
291 * @param prev_child previous child already traversed, if NULL start at
292 * children of parent bus.
293 * @return pointer to child or NULL when no more children
294 */
295DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
296 DEVTREE_CONST struct device *prev_child)
297{
298 DEVTREE_CONST struct device *dev;
299
300 if (parent == NULL)
301 return NULL;
302
303 if (prev_child == NULL)
304 dev = parent->children;
305 else
306 dev = prev_child->sibling;
307
308 return dev;
309}