blob: f2f0177f57fc6bd70eb16318a9d6dc00ef3a335f [file] [log] [blame]
Stefan Reinauer57879c92012-07-31 16:47:25 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Stefan Reinauer57879c92012-07-31 16:47:25 -070019 */
20
Kyösti Mälkki2df1c122018-05-21 01:52:10 +030021#include <console/console.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070022#include <device/device.h>
23#include <device/path.h>
24#include <device/pci.h>
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +030025#include <device/pci_def.h>
Stefan Reinauer57879c92012-07-31 16:47:25 -070026#include <device/resource.h>
27
28/** Linked list of ALL devices */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050029DEVTREE_CONST struct device * DEVTREE_CONST all_devices = &dev_root;
Stefan Reinauer57879c92012-07-31 16:47:25 -070030
31/**
32 * Given a PCI bus and a devfn number, find the device structure.
33 *
34 * @param bus The bus number.
35 * @param devfn A device/function number.
36 * @return Pointer to the device structure (if found), 0 otherwise.
37 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -050038DEVTREE_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älkkifffc9f32019-07-03 09:55:01 +0300207 MAYBE_STATIC 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älkkidb866ba2019-01-06 17:40:36 +0200222 /* Work around pcidev_path_behind() below failing
223 * due tue complicated devicetree with topology
224 * being manipulated on-the-fly.
225 */
Julius Wernercd49cce2019-03-05 16:53:33 -0800226 if (CONFIG(NORTHBRIDGE_AMD_AMDFAM10))
Kyösti Mälkkidb866ba2019-01-06 17:40:36 +0200227 return dev_find_slot(0, devfn);
228
Kyösti Mälkkifffc9f32019-07-03 09:55:01 +0300229 return pcidev_path_behind(pci_root_bus(), devfn);
Kyösti Mälkkiad7674e2018-05-20 10:31:23 +0300230}
231
232DEVTREE_CONST struct device *pcidev_on_root(uint8_t dev, uint8_t fn)
233{
234 return pcidev_path_on_root(PCI_DEVFN(dev, fn));
235}
236
Kyösti Mälkkif2ac0132019-07-12 15:26:29 +0300237DEVTREE_CONST struct device *pcidev_path_on_root_debug(pci_devfn_t devfn, const char *func)
238{
239 DEVTREE_CONST struct device *dev = pcidev_path_on_root(devfn);
240 if (dev)
241 return dev;
242
243 printk(BIOS_ERR, "BUG: %s requests hidden 00:%02x.%u\n", func, devfn >> 3, devfn & 7);
244
245 /* FIXME: This can return wrong device. */
246 return dev_find_slot(0, devfn);
247}
248
Martin Roth16d953a2014-05-12 17:38:59 -0600249/**
Stefan Reinauer57879c92012-07-31 16:47:25 -0700250 * Given an SMBus bus and a device number, find the device structure.
251 *
252 * @param bus The bus number.
253 * @param addr A device number.
254 * @return Pointer to the device structure (if found), 0 otherwise.
255 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500256DEVTREE_CONST struct device *dev_find_slot_on_smbus(unsigned int bus,
Stefan Reinauer57879c92012-07-31 16:47:25 -0700257 unsigned int addr)
258{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500259 DEVTREE_CONST struct device *dev, *result;
Stefan Reinauer57879c92012-07-31 16:47:25 -0700260
261 result = 0;
262 for (dev = all_devices; dev; dev = dev->next) {
263 if ((dev->path.type == DEVICE_PATH_I2C) &&
264 (dev->bus->secondary == bus) &&
265 (dev->path.i2c.device == addr)) {
266 result = dev;
267 break;
268 }
269 }
270 return result;
271}
Kyösti Mälkki96643452015-01-28 22:03:46 +0200272
273/**
274 * Given a PnP port and a device number, find the device structure.
275 *
276 * @param port The I/O port.
277 * @param device Logical device number.
278 * @return Pointer to the device structure (if found), 0 otherwise.
279 */
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500280DEVTREE_CONST struct device *dev_find_slot_pnp(u16 port, u16 device)
Kyösti Mälkki96643452015-01-28 22:03:46 +0200281{
Aaron Durbine4d7abc2017-04-16 22:05:36 -0500282 DEVTREE_CONST struct device *dev;
Kyösti Mälkki96643452015-01-28 22:03:46 +0200283
284 for (dev = all_devices; dev; dev = dev->next) {
285 if ((dev->path.type == DEVICE_PATH_PNP) &&
286 (dev->path.pnp.port == port) &&
287 (dev->path.pnp.device == device)) {
288 return dev;
289 }
290 }
291 return 0;
292}
Aaron Durbina0dabd12018-07-25 08:49:19 -0600293
294/**
295 * Given a device and previous match iterate through all the children.
296 *
297 * @param bus parent device's bus holding all the children
298 * @param prev_child previous child already traversed, if NULL start at
299 * children of parent bus.
300 * @return pointer to child or NULL when no more children
301 */
302DEVTREE_CONST struct device *dev_bus_each_child(const struct bus *parent,
303 DEVTREE_CONST struct device *prev_child)
304{
305 DEVTREE_CONST struct device *dev;
306
307 if (parent == NULL)
308 return NULL;
309
310 if (prev_child == NULL)
311 dev = parent->children;
312 else
313 dev = prev_child->sibling;
314
315 return dev;
316}