blob: c46f2836081ca892fdee6366eeb62c926f36aa20 [file] [log] [blame]
Stefan Reinauer57879c92012-07-31 16:47:25 -07001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer57879c92012-07-31 16:47:25 -07004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
Stefan Reinauer57879c92012-07-31 16:47:25 -070012 */
13
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030014#include <console/console.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070015#include <device/device.h>
16#include <device/path.h>
17#include <device/pci.h>
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +030018#include <device/pci_def.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070019#include <device/resource.h>
20
21/** Linked list of ALL devices */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050022DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070023
24/**
25 * Given a PCI bus and a devfn number, find the device structure.
26 *
Aaron Durbin55ef0d22019-09-26 12:05:27 -060027 * Note that this function can return the incorrect device prior
28 * to PCI enumeration because the secondary field of the bus object
29 * is 0. The failing scenario is determined by the order of the
30 * devices in all_devices singly-linked list as well as the time
31 * when this function is called (secondary reflecting topology).
32 *
Stefan Reinauer57879c92012-07-31 16:47:25 -070033 * @param bus The bus number.
34 * @param devfn A device/function number.
35 * @return Pointer to the device structure (if found), 0 otherwise.
36 */
Kyösti Mälkkibd585fa2019-07-03 07:51:43 +030037
38static DEVTREE_CONST struct device *dev_find_slot(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -070039 unsigned int devfn)
40{
Aaron Durbine4d7abc2017-04-16 22:05:36 -050041 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -070042
43 result = 0;
44 for (dev = all_devices; dev; dev = dev->next) {
45 if ((dev->path.type == DEVICE_PATH_PCI) &&
46 (dev->bus->secondary == bus) &&
47 (dev->path.pci.devfn == devfn)) {
48 result = dev;
49 break;
50 }
51 }
52 return result;
53}
54
55/**
Nico Huberf6a43442018-05-15 14:13:32 +020056 * Given a Device Path Type, find the device structure.
57 *
58 * @param prev_match The previously matched device instance.
59 * @param path_type The Device Path Type.
60 * @return Pointer to the device structure (if found), 0 otherwise.
61 */
62DEVTREE_CONST struct device *dev_find_path(
63 DEVTREE_CONST struct device *prev_match,
64 enum device_path_type path_type)
65{
66 DEVTREE_CONST struct device *dev, *result = NULL;
67
68 if (prev_match == NULL)
69 prev_match = all_devices;
70 else
71 prev_match = prev_match->next;
72
73 for (dev = prev_match; dev; dev = dev->next) {
74 if (dev->path.type == path_type) {
75 result = dev;
76 break;
77 }
78 }
79 return result;
80}
81
82/**
Martin Roth16d953a2014-05-12 17:38:59 -060083 * Given a device pointer, find the next PCI device.
84 *
85 * @param previous_dev A pointer to a PCI device structure.
86 * @return Pointer to the next device structure (if found), 0 otherwise.
87 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050088DEVTREE_CONST struct device *dev_find_next_pci_device(
89 DEVTREE_CONST struct device *previous_dev)
Martin Roth16d953a2014-05-12 17:38:59 -060090{
Nico Huberf6a43442018-05-15 14:13:32 +020091 return dev_find_path(previous_dev, DEVICE_PATH_PCI);
Martin Roth16d953a2014-05-12 17:38:59 -060092}
93
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030094static int path_eq(const struct device_path *path1,
95 const struct device_path *path2)
96{
97 int equal = 0;
98
99 if (path1->type != path2->type)
100 return 0;
101
102 switch (path1->type) {
103 case DEVICE_PATH_NONE:
104 break;
105 case DEVICE_PATH_ROOT:
106 equal = 1;
107 break;
108 case DEVICE_PATH_PCI:
109 equal = (path1->pci.devfn == path2->pci.devfn);
110 break;
111 case DEVICE_PATH_PNP:
112 equal = (path1->pnp.port == path2->pnp.port) &&
113 (path1->pnp.device == path2->pnp.device);
114 break;
115 case DEVICE_PATH_I2C:
116 equal = (path1->i2c.device == path2->i2c.device) &&
117 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
118 break;
119 case DEVICE_PATH_APIC:
120 equal = (path1->apic.apic_id == path2->apic.apic_id);
121 break;
122 case DEVICE_PATH_DOMAIN:
123 equal = (path1->domain.domain == path2->domain.domain);
124 break;
125 case DEVICE_PATH_CPU_CLUSTER:
126 equal = (path1->cpu_cluster.cluster
127 == path2->cpu_cluster.cluster);
128 break;
129 case DEVICE_PATH_CPU:
130 equal = (path1->cpu.id == path2->cpu.id);
131 break;
132 case DEVICE_PATH_CPU_BUS:
133 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
134 break;
135 case DEVICE_PATH_GENERIC:
136 equal = (path1->generic.id == path2->generic.id) &&
137 (path1->generic.subid == path2->generic.subid);
138 break;
139 case DEVICE_PATH_SPI:
140 equal = (path1->spi.cs == path2->spi.cs);
141 break;
142 case DEVICE_PATH_USB:
143 equal = (path1->usb.port_type == path2->usb.port_type) &&
144 (path1->usb.port_id == path2->usb.port_id);
145 break;
146 case DEVICE_PATH_MMIO:
147 equal = (path1->mmio.addr == path2->mmio.addr);
148 break;
149 default:
150 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
151 break;
152 }
153
154 return equal;
155}
156
157/**
158 * See if a device structure exists for path.
159 *
160 * @param parent The bus to find the device on.
161 * @param path The relative path from the bus to the appropriate device.
162 * @return Pointer to a device structure for the device on bus at path
163 * or 0/NULL if no device is found.
164 */
165DEVTREE_CONST struct device *find_dev_path(
166 const struct bus *parent, const struct device_path *path)
167{
168 DEVTREE_CONST struct device *child;
169 for (child = parent->children; child; child = child->sibling) {
170 if (path_eq(path, &child->path))
171 break;
172 }
173 return child;
174}
175
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300176DEVTREE_CONST struct device *pcidev_path_behind(
177 const struct bus *parent, pci_devfn_t devfn)
178{
179 const struct device_path path = {
180 .type = DEVICE_PATH_PCI,
181 .pci.devfn = devfn,
182 };
183 return find_dev_path(parent, &path);
184}
185
Kyösti Mälkkiab275f02019-07-04 07:16:59 +0300186DEVTREE_CONST struct device *pcidev_path_on_bus(unsigned int bus, pci_devfn_t devfn)
187{
188 DEVTREE_CONST struct bus *parent = pci_root_bus();
189 DEVTREE_CONST struct device *dev = parent->children;
190
191 /* FIXME: Write the loop with topology links. */
192 while (dev) {
193 if (dev->path.type != DEVICE_PATH_PCI) {
194 dev = dev->next;
195 continue;
196 }
197 if (dev->bus->secondary == bus)
198 return pcidev_path_behind(dev->bus, devfn);
199 dev = dev->next;
200 }
201 return NULL;
202}
203
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300204DEVTREE_CONST struct bus *pci_root_bus(void)
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300205{
206 DEVTREE_CONST struct device *pci_domain;
Kyösti Mälkki117cf2b2019-08-20 06:01:57 +0300207 MAYBE_STATIC_BSS DEVTREE_CONST struct bus *pci_root = NULL;
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300208
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300209 if (pci_root)
210 return pci_root;
211
212 pci_domain = dev_find_path(NULL, DEVICE_PATH_DOMAIN);
213 if (!pci_domain)
214 return NULL;
215
216 pci_root = pci_domain->link_list;
217 return pci_root;
218}
219
220DEVTREE_CONST struct device *pcidev_path_on_root(pci_devfn_t devfn)
221{
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300222 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300223}
224
225DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
226{
227 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
228}
229
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300230DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
231{
232 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
233 if (dev)
234 return dev;
235
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300236 devtree_bug(func, devfn);
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300237
238 /* FIXME: This can return wrong device. */
239 return dev_find_slot(0, devfn);
240}
241
Kyösti Mälkki4323d262019-07-12 16:20:14 +0300242void devtree_bug(const char *func, pci_devfn_t devfn)
243{
244 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
245}
246
247void __noreturn devtree_die(void)
248{
249 die("DEVTREE: dev or chip_info is NULL\n");
250}
251
Martin Roth16d953a2014-05-12 17:38:59 -0600252/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700253 * Given an SMBus bus and a device number, find the device structure.
254 *
255 * @param bus The bus number.
256 * @param addr A device number.
257 * @return Pointer to the device structure (if found), 0 otherwise.
258 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500259DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700260 unsigned int addr)
261{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500262 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700263
264 result = 0;
265 for (dev = all_devices; dev; dev = dev->next) {
266 if ((dev->path.type == DEVICE_PATH_I2C) &&
267 (dev->bus->secondary == bus) &&
268 (dev->path.i2c.device == addr)) {
269 result = dev;
270 break;
271 }
272 }
273 return result;
274}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200275
276/**
277 * Given a PnP port and a device number, find the device structure.
278 *
279 * @param port The I/O port.
280 * @param device Logical device number.
281 * @return Pointer to the device structure (if found), 0 otherwise.
282 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500283DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200284{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500285 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200286
287 for (dev = all_devices; dev; dev = dev->next) {
288 if ((dev->path.type == DEVICE_PATH_PNP) &&
289 (dev->path.pnp.port == port) &&
290 (dev->path.pnp.device == device)) {
291 return dev;
292 }
293 }
294 return 0;
295}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600296
297/**
298 * Given a device and previous match iterate through all the children.
299 *
300 * @param bus parent device's bus holding all the children
301 * @param prev_child previous child already traversed, if NULL start at
302 * children of parent bus.
303 * @return pointer to child or NULL when no more children
304 */
305DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
306 DEVTREE_CONST struct device *prev_child)
307{
308 DEVTREE_CONST struct device *dev;
309
310 if (parent == NULL)
311 return NULL;
312
313 if (prev_child == NULL)
314 dev = parent->children;
315 else
316 dev = prev_child->sibling;
317
318 return dev;
319}