blob: a64b63ae235396644c004b707e3ef7c92b46d834 [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
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.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000019 */
20
Eric Biederman8ca8d762003-04-22 19:02:15 +000021#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000022#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000023#include <device/path.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020024#include <device/pci_def.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000025#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000026#include <string.h>
27
Eric Biederman03acab62004-10-14 21:25:53 +000028/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000029 * See if a device structure exists for path.
Eric Biederman03acab62004-10-14 21:25:53 +000030 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000031 * @param parent The bus to find the device on.
32 * @param path The relative path from the bus to the appropriate device.
33 * @return Pointer to a device structure for the device on bus at path
34 * or 0/NULL if no device is found.
Eric Biederman03acab62004-10-14 21:25:53 +000035 */
Aaron Durbinf0349022017-11-10 10:47:54 -070036struct device *find_dev_path(struct bus *parent, struct device_path *path)
Eric Biederman03acab62004-10-14 21:25:53 +000037{
Aaron Durbinf0349022017-11-10 10:47:54 -070038 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +000039 for (child = parent->children; child; child = child->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +000040 if (path_eq(path, &child->path))
Eric Biederman03acab62004-10-14 21:25:53 +000041 break;
Eric Biederman03acab62004-10-14 21:25:53 +000042 }
43 return child;
44}
Eric Biedermane9a271e32003-09-02 03:36:25 +000045
46/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000047 * Given a PCI bus and a devfn number, find the device structure.
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000048 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000049 * @param bus The bus number.
50 * @param devfn A device/function number.
51 * @return Pointer to the device structure (if found), 0 otherwise.
Eric Biederman8ca8d762003-04-22 19:02:15 +000052 */
53struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
54{
Eric Biederman83b991a2003-10-11 06:20:25 +000055 struct device *dev, *result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000056
Eric Biederman83b991a2003-10-11 06:20:25 +000057 result = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +000058 for (dev = all_devices; dev; dev = dev->next) {
Eric Biederman5cd81732004-03-11 15:01:31 +000059 if ((dev->path.type == DEVICE_PATH_PCI) &&
Uwe Hermanne4870472010-11-04 23:23:47 +000060 (dev->bus->secondary == bus) &&
61 (dev->path.pci.devfn == devfn)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000062 result = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +000063 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +000064 }
65 }
Eric Biederman83b991a2003-10-11 06:20:25 +000066 return result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000067}
68
arch import user (historical)98d0d302005-07-06 17:13:46 +000069/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000070 * Given an SMBus bus and a device number, find the device structure.
arch import user (historical)98d0d302005-07-06 17:13:46 +000071 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000072 * @param bus The bus number.
73 * @param addr A device number.
74 * @return Pointer to the device structure (if found), 0 otherwise.
arch import user (historical)98d0d302005-07-06 17:13:46 +000075 */
76struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
77{
Uwe Hermanne4870472010-11-04 23:23:47 +000078 struct device *dev, *result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000079
Uwe Hermanne4870472010-11-04 23:23:47 +000080 result = 0;
81 for (dev = all_devices; dev; dev = dev->next) {
82 if ((dev->path.type == DEVICE_PATH_I2C) &&
83 (dev->bus->secondary == bus) &&
84 (dev->path.i2c.device == addr)) {
85 result = dev;
86 break;
87 }
88 }
89 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000090}
arch import user (historical)98d0d302005-07-06 17:13:46 +000091
Uwe Hermannc1ee4292010-10-17 19:01:48 +000092/**
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +010093 * Given a PnP port and a device number, find the device structure.
94 *
95 * @param port The I/O port.
96 * @param device Logical device number.
97 * @return Pointer to the device structure (if found), 0 otherwise.
98 */
Vladimir Serbinenkob33384a2014-02-08 18:58:39 +010099struct device *dev_find_slot_pnp(u16 port, u16 device)
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +0100100{
101 struct device *dev;
102
103 for (dev = all_devices; dev; dev = dev->next) {
104 if ((dev->path.type == DEVICE_PATH_PNP) &&
105 (dev->path.pnp.port == port) &&
106 (dev->path.pnp.device == device)) {
107 return dev;
108 }
109 }
110 return 0;
111}
112
113/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -0700114 * Given a Local APIC ID, find the device structure.
115 *
116 * @param apic_id The Local APIC ID number.
117 * @return Pointer to the device structure (if found), 0 otherwise.
118 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700119struct device *dev_find_lapic(unsigned apic_id)
Duncan Laurie6f88a6e2011-07-18 10:41:36 -0700120{
Aaron Durbinf0349022017-11-10 10:47:54 -0700121 struct device *dev;
122 struct device *result = NULL;
Duncan Laurie6f88a6e2011-07-18 10:41:36 -0700123
124 for (dev = all_devices; dev; dev = dev->next) {
125 if (dev->path.type == DEVICE_PATH_APIC &&
126 dev->path.apic.apic_id == apic_id) {
127 result = dev;
128 break;
129 }
130 }
131 return result;
132}
133
134/**
Subrata Banikfe204fe2016-12-06 18:06:30 +0530135 * Given a Device Path Type, find the device structure.
136 *
137 * @param prev_match The previously matched device instance.
138 * @param path_type The Device Path Type.
139 * @return Pointer to the device structure (if found), 0 otherwise.
140 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700141struct device *dev_find_path(device_t prev_match,
142 enum device_path_type path_type)
Subrata Banikfe204fe2016-12-06 18:06:30 +0530143{
Aaron Durbinf0349022017-11-10 10:47:54 -0700144 struct device *dev;
145 struct device *result = NULL;
Subrata Banikfe204fe2016-12-06 18:06:30 +0530146
147 if (prev_match == NULL)
148 prev_match = all_devices;
149 else
150 prev_match = prev_match->next;
151
152 for (dev = prev_match; dev; dev = dev->next) {
153 if (dev->path.type == path_type) {
154 result = dev;
155 break;
156 }
157 }
158 return result;
159}
160
161/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000162 * Find a device of a given vendor and type.
163 *
164 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
165 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +0000166 * @param from Pointer to the device structure, used as a starting point in
167 * the linked list of all_devices, which can be 0 to start at the
168 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000169 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000170 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000171struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000172{
173 if (!from)
174 from = all_devices;
175 else
176 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000177
178 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000180
Eric Biederman8ca8d762003-04-22 19:02:15 +0000181 return from;
182}
183
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000184/**
185 * Find a device of a given class.
186 *
187 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +0000188 * @param from Pointer to the device structure, used as a starting point in
189 * the linked list of all_devices, which can be 0 to start at the
190 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000191 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000192 */
193struct device *dev_find_class(unsigned int class, struct device *from)
194{
195 if (!from)
196 from = all_devices;
197 else
198 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000199
Greg Watson59651692003-12-17 17:39:53 +0000200 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000201 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000202
Eric Biederman8ca8d762003-04-22 19:02:15 +0000203 return from;
204}
205
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700206/**
207 * Encode the device path into 3 bytes for logging to CMOS.
208 *
209 * @param dev The device path to encode.
210 * @return Device path encoded into lower 3 bytes of dword.
211 */
Subrata Banik564547f2018-05-02 10:27:36 +0530212u32 dev_path_encode(const struct device *dev)
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700213{
214 u32 ret;
215
216 if (!dev)
217 return 0;
218
219 /* Store the device type in 3rd byte. */
220 ret = dev->path.type << 16;
221
222 /* Encode the device specific path in the low word. */
223 switch (dev->path.type) {
224 case DEVICE_PATH_ROOT:
225 break;
226 case DEVICE_PATH_PCI:
227 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
228 break;
229 case DEVICE_PATH_PNP:
230 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
231 break;
232 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700233 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700234 break;
235 case DEVICE_PATH_APIC:
236 ret |= dev->path.apic.apic_id;
237 break;
238 case DEVICE_PATH_DOMAIN:
239 ret |= dev->path.domain.domain;
240 break;
241 case DEVICE_PATH_CPU_CLUSTER:
242 ret |= dev->path.cpu_cluster.cluster;
243 break;
244 case DEVICE_PATH_CPU:
245 ret |= dev->path.cpu.id;
246 break;
247 case DEVICE_PATH_CPU_BUS:
248 ret |= dev->path.cpu_bus.id;
249 break;
250 case DEVICE_PATH_IOAPIC:
251 ret |= dev->path.ioapic.ioapic_id;
252 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700253 case DEVICE_PATH_GENERIC:
254 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
255 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800256 case DEVICE_PATH_SPI:
257 ret |= dev->path.spi.cs;
258 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700259 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800260 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700261 default:
262 break;
263 }
264
265 return ret;
266}
267
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000268/*
269 * Warning: This function uses a static buffer. Don't call it more than once
270 * from the same print statement!
271 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200272const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000273{
274 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000275
Eric Biedermane9a271e32003-09-02 03:36:25 +0000276 buffer[0] = '\0';
277 if (!dev) {
278 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000279 } else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000280 switch(dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100281 case DEVICE_PATH_NONE:
282 memcpy(buffer, "NONE", 5);
283 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000284 case DEVICE_PATH_ROOT:
285 memcpy(buffer, "Root Device", 12);
286 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000287 case DEVICE_PATH_PCI:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100288 snprintf(buffer, sizeof (buffer),
289 "PCI: %02x:%02x.%01x",
290 dev->bus->secondary,
291 PCI_SLOT(dev->path.pci.devfn),
292 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000293 break;
294 case DEVICE_PATH_PNP:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100295 snprintf(buffer, sizeof (buffer), "PNP: %04x.%01x",
296 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000297 break;
298 case DEVICE_PATH_I2C:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100299 snprintf(buffer, sizeof (buffer), "I2C: %02x:%02x",
300 dev->bus->secondary,
301 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000302 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000303 case DEVICE_PATH_APIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100304 snprintf(buffer, sizeof (buffer), "APIC: %02x",
305 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000306 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200307 case DEVICE_PATH_IOAPIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100308 snprintf(buffer, sizeof (buffer), "IOAPIC: %02x",
309 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200310 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800311 case DEVICE_PATH_DOMAIN:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100312 snprintf(buffer, sizeof (buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800313 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000314 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800315 case DEVICE_PATH_CPU_CLUSTER:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100316 snprintf(buffer, sizeof (buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800317 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000318 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000319 case DEVICE_PATH_CPU:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100320 snprintf(buffer, sizeof (buffer),
321 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000322 break;
323 case DEVICE_PATH_CPU_BUS:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100324 snprintf(buffer, sizeof (buffer),
325 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000326 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700327 case DEVICE_PATH_GENERIC:
328 snprintf(buffer, sizeof (buffer),
329 "GENERIC: %d.%d", dev->path.generic.id,
330 dev->path.generic.subid);
331 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800332 case DEVICE_PATH_SPI:
333 snprintf(buffer, sizeof (buffer), "SPI: %02x",
334 dev->path.spi.cs);
335 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800336 case DEVICE_PATH_MMIO:
337 snprintf(buffer, sizeof (buffer), "MMIO: %08x",
338 dev->path.mmio.addr);
339 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000340 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000341 printk(BIOS_ERR, "Unknown device path type: %d\n",
342 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000343 break;
344 }
345 }
346 return buffer;
347}
348
Aaron Durbinf0349022017-11-10 10:47:54 -0700349const char *dev_name(struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200350{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300351 if (dev->name)
352 return dev->name;
353 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200354 return dev->chip_ops->name;
355 else
356 return "unknown";
357}
358
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000359const char *bus_path(struct bus *bus)
360{
361 static char buffer[BUS_PATH_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100362 snprintf(buffer, sizeof (buffer),
363 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000364 return buffer;
365}
366
Eric Biedermane9a271e32003-09-02 03:36:25 +0000367int path_eq(struct device_path *path1, struct device_path *path2)
368{
369 int equal = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000370
371 if (path1->type != path2->type)
372 return 0;
373
374 switch (path1->type) {
375 case DEVICE_PATH_NONE:
376 break;
377 case DEVICE_PATH_ROOT:
378 equal = 1;
379 break;
380 case DEVICE_PATH_PCI:
381 equal = (path1->pci.devfn == path2->pci.devfn);
382 break;
383 case DEVICE_PATH_PNP:
384 equal = (path1->pnp.port == path2->pnp.port) &&
385 (path1->pnp.device == path2->pnp.device);
386 break;
387 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700388 equal = (path1->i2c.device == path2->i2c.device) &&
389 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000390 break;
391 case DEVICE_PATH_APIC:
392 equal = (path1->apic.apic_id == path2->apic.apic_id);
393 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800394 case DEVICE_PATH_DOMAIN:
395 equal = (path1->domain.domain == path2->domain.domain);
Uwe Hermanne4870472010-11-04 23:23:47 +0000396 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800397 case DEVICE_PATH_CPU_CLUSTER:
398 equal = (path1->cpu_cluster.cluster
399 == path2->cpu_cluster.cluster);
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 break;
401 case DEVICE_PATH_CPU:
402 equal = (path1->cpu.id == path2->cpu.id);
403 break;
404 case DEVICE_PATH_CPU_BUS:
405 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
406 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700407 case DEVICE_PATH_GENERIC:
408 equal = (path1->generic.id == path2->generic.id) &&
409 (path1->generic.subid == path2->generic.subid);
410 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800411 case DEVICE_PATH_SPI:
412 equal = (path1->spi.cs == path2->spi.cs);
413 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800414 case DEVICE_PATH_MMIO:
415 equal = (path1->mmio.addr == path2->mmio.addr);
416 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000417 default:
Martin Roth63373ed2013-07-08 16:24:19 -0600418 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
Uwe Hermanne4870472010-11-04 23:23:47 +0000419 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000420 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000421
Eric Biedermane9a271e32003-09-02 03:36:25 +0000422 return equal;
423}
Eric Biederman5cd81732004-03-11 15:01:31 +0000424
425/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000426 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000427 *
428 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000429 */
430static int allocate_more_resources(void)
431{
432 int i;
433 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000434
Myles Watsonc25cc112010-05-21 14:33:48 +0000435 new_res_list = malloc(64 * sizeof(*new_res_list));
436
437 if (new_res_list == NULL)
438 return 0;
439
440 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
441
Uwe Hermanne4870472010-11-04 23:23:47 +0000442 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000443 new_res_list[i].next = &new_res_list[i+1];
444
445 free_resources = new_res_list;
446 return 1;
447}
448
449/**
450 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000451 *
452 * @param dev TODO
453 * @param res TODO
454 * @param prev TODO
455 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000456 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700457static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000458 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000459{
460 if (prev)
461 prev->next = res->next;
462 else
463 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000464
Myles Watsonc25cc112010-05-21 14:33:48 +0000465 res->next = free_resources;
466 free_resources = res;
467}
468
469/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000470 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000471 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000472 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000473 *
474 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000475 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700476void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000477{
Myles Watsonc25cc112010-05-21 14:33:48 +0000478 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000479
Eric Biederman5cd81732004-03-11 15:01:31 +0000480 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000481 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000482 next = res->next;
483 if (!res->flags)
484 free_resource(dev, res, prev);
485 else
486 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000487 }
488}
489
490/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000491 * See if a resource structure already exists for a given index.
492 *
493 * @param dev The device to find the resource on.
494 * @param index The index of the resource on the device.
495 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000496 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700497struct resource *probe_resource(struct device *dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000498{
Myles Watsonc25cc112010-05-21 14:33:48 +0000499 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000500
Eric Biederman5cd81732004-03-11 15:01:31 +0000501 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000502 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000503 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000504 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000505 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000506
Myles Watsonc25cc112010-05-21 14:33:48 +0000507 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000508}
509
510/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000511 * See if a resource structure already exists for a given index and if not
512 * allocate one.
513 *
Paul Menzel20923872014-06-07 13:31:29 +0200514 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000515 *
516 * @param dev The device to find the resource on.
517 * @param index The index of the resource on the device.
518 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000519 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700520struct resource *new_resource(struct device *dev, unsigned index)
Eric Biederman03acab62004-10-14 21:25:53 +0000521{
Myles Watsonc25cc112010-05-21 14:33:48 +0000522 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000523
Uwe Hermanne4870472010-11-04 23:23:47 +0000524 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000525 compact_resources(dev);
526
Uwe Hermanne4870472010-11-04 23:23:47 +0000527 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000528 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000529 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000530 if (free_resources == NULL && !allocate_more_resources())
531 die("Couldn't allocate more resources.");
532
533 resource = free_resources;
534 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000535 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000536 resource->next = NULL;
537 tail = dev->resource_list;
538 if (tail) {
539 while (tail->next) tail = tail->next;
540 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000541 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000542 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000543 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000544 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000545
546 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000547 if (!(resource->flags & IORESOURCE_FIXED)) {
548 resource->flags = 0;
549 resource->base = 0;
550 }
551 resource->size = 0;
552 resource->limit = 0;
553 resource->index = index;
554 resource->align = 0;
555 resource->gran = 0;
556
557 return resource;
558}
559
Eric Biederman03acab62004-10-14 21:25:53 +0000560/**
561 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000562 *
563 * @param dev The device to find the resource on.
564 * @param index The index of the resource on the device.
565 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000566 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700567struct resource *find_resource(struct device *dev, unsigned index)
Eric Biederman03acab62004-10-14 21:25:53 +0000568{
569 struct resource *resource;
570
Uwe Hermanne4870472010-11-04 23:23:47 +0000571 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000572 resource = probe_resource(dev, index);
573 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000574 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000575 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000576 die("");
577 }
578 return resource;
579}
580
Eric Biederman03acab62004-10-14 21:25:53 +0000581/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000582 * Round a number up to the next multiple of gran.
583 *
584 * @param val The starting value.
585 * @param gran Granularity we are aligning the number to.
586 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000587 */
588static resource_t align_up(resource_t val, unsigned long gran)
589{
590 resource_t mask;
591 mask = (1ULL << gran) - 1ULL;
592 val += mask;
593 val &= ~mask;
594 return val;
595}
596
597/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000598 * Round a number up to the previous multiple of gran.
599 *
600 * @param val The starting value.
601 * @param gran Granularity we are aligning the number to.
602 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000603 */
604static resource_t align_down(resource_t val, unsigned long gran)
605{
606 resource_t mask;
607 mask = (1ULL << gran) - 1ULL;
608 val &= ~mask;
609 return val;
610}
611
612/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000613 * Compute the maximum address that is part of a resource.
614 *
615 * @param resource The resource whose limit is desired.
616 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000617 */
618resource_t resource_end(struct resource *resource)
619{
620 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000621
622 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000623 base = resource->base;
624
Uwe Hermanne4870472010-11-04 23:23:47 +0000625 /*
626 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000627 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000628 * the bridge. While the granularity is simply how many low bits of
629 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000630 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000631
Uwe Hermanne4870472010-11-04 23:23:47 +0000632 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000633 end = base + align_up(resource->size, resource->gran) - 1;
634
635 return end;
636}
637
638/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000639 * Compute the maximum legal value for resource->base.
640 *
641 * @param resource The resource whose maximum is desired.
642 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000643 */
644resource_t resource_max(struct resource *resource)
645{
646 resource_t max;
647
648 max = align_down(resource->limit - resource->size + 1, resource->align);
649
650 return max;
651}
652
653/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000654 * Return the resource type of a resource.
655 *
656 * @param resource The resource type to decode.
657 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000658 */
659const char *resource_type(struct resource *resource)
660{
661 static char buffer[RESOURCE_TYPE_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100662 snprintf(buffer, sizeof (buffer), "%s%s%s%s",
663 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
664 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
665 ((resource->flags == 0) ? "unused" :
666 (resource->flags & IORESOURCE_IO) ? "io" :
667 (resource->flags & IORESOURCE_DRQ) ? "drq" :
668 (resource->flags & IORESOURCE_IRQ) ? "irq" :
669 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
670 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000671 return buffer;
672}
673
674/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000675 * Print the resource that was just stored.
676 *
Martin Roth63373ed2013-07-08 16:24:19 -0600677 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000678 * @param resource The resource that was just stored.
679 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000680 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700681void report_resource_stored(struct device *dev, struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000682 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000683{
Uwe Hermanne4870472010-11-04 23:23:47 +0000684 char buf[10];
685 unsigned long long base, end;
686
687 if (!(resource->flags & IORESOURCE_STORED))
688 return;
689
690 base = resource->base;
691 end = resource_end(resource);
692 buf[0] = '\0';
693
694 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100695 snprintf(buf, sizeof (buf),
696 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000697 }
Patrick Georgi51615092012-03-11 19:31:03 +0100698 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000699 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
700 base, end, resource->size, resource->gran, buf,
701 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000702}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000703
Uwe Hermanne4870472010-11-04 23:23:47 +0000704void search_bus_resources(struct bus *bus, unsigned long type_mask,
705 unsigned long type, resource_search_t search,
706 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000707{
708 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000709
Myles Watson894a3472010-06-09 22:41:35 +0000710 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000711 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000712
713 /* Ignore disabled devices. */
714 if (!curdev->enabled)
715 continue;
716
Myles Watson894a3472010-06-09 22:41:35 +0000717 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000718 /* If it isn't the right kind of resource ignore it. */
719 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000720 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000721
722 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000723 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000724 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000725 for (subbus = curdev->link_list; subbus;
726 subbus = subbus->next)
727 if (subbus->link_num
728 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000729 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700730 if (!subbus) /* Why can subbus be NULL? */
731 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000732 search_bus_resources(subbus, type_mask, type,
733 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000734 continue;
735 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000736 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000737 }
738 }
739}
740
Uwe Hermanne4870472010-11-04 23:23:47 +0000741void search_global_resources(unsigned long type_mask, unsigned long type,
742 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000743{
744 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000745
Myles Watson894a3472010-06-09 22:41:35 +0000746 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000747 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000748
749 /* Ignore disabled devices. */
750 if (!curdev->enabled)
751 continue;
752
Myles Watson894a3472010-06-09 22:41:35 +0000753 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000754 /* If it isn't the right kind of resource ignore it. */
755 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000756 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000757
758 /* If it is a subtractive resource ignore it. */
759 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000760 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000761
Myles Watsonc25cc112010-05-21 14:33:48 +0000762 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000763 }
764 }
765}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000766
Aaron Durbinf0349022017-11-10 10:47:54 -0700767void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000768{
Uwe Hermanne4870472010-11-04 23:23:47 +0000769 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000770 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000771
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000772 dev->enabled = enable;
773 if (dev->ops && dev->ops->enable) {
774 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000775 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000776 dev->chip_ops->enable_dev(dev);
777 }
778}
779
780void disable_children(struct bus *bus)
781{
Aaron Durbinf0349022017-11-10 10:47:54 -0700782 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000783
Myles Watson894a3472010-06-09 22:41:35 +0000784 for (child = bus->children; child; child = child->sibling) {
785 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000786 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000787 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000788 dev_set_enabled(child, 0);
789 }
790}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000791
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200792/*
793 * Returns true if the device is an enabled bridge that has at least
794 * one enabled device on its secondary bus.
795 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700796bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200797{
798 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700799 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200800
801 if (!dev || !dev->enabled)
802 return 0;
803
804 if (!dev->link_list || !dev->link_list->children)
805 return 0;
806
807 for (link = dev->link_list; link; link = link->next) {
808 for (child = link->children; child; child = child->sibling) {
809 if (child->enabled)
810 return 1;
811 }
812 }
813
814 return 0;
815}
816
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200817static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000818{
Myles Watson894a3472010-06-09 22:41:35 +0000819 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000820 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000821 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000822 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000823 char indent[30]; /* If your tree has more levels, it's wrong. */
824
825 for (i = 0; i < depth + 1 && i < 29; i++)
826 indent[i] = ' ';
827 indent[i] = '\0';
828
Martin Roth7f35d3a2017-07-23 16:22:25 -0600829 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
830 if (root->link_list && root->link_list->children)
831 do_printk(BIOS_DEBUG, " child on link 0 %s",
832 dev_path(root->link_list->children));
833 do_printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000834
Myles Watsonc25cc112010-05-21 14:33:48 +0000835 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000836 do_printk(debug_level, "%s%s resource base %llx size %llx "
837 "align %d gran %d limit %llx flags %lx index %lx\n",
838 indent, dev_path(root), res->base, res->size,
839 res->align, res->gran, res->limit, res->flags,
840 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000841 }
842
Myles Watson894a3472010-06-09 22:41:35 +0000843 for (link = root->link_list; link; link = link->next) {
844 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000845 resource_tree(child, debug_level, depth + 1);
846 }
847}
848
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200849void print_resource_tree(const struct device *root, int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000850{
851 /* Bail if root is null. */
852 if (!root) {
853 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
854 return;
855 }
856
857 /* Bail if not printing to screen. */
858 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000859 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000860 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000861
Myles Watsonbb3d8122009-05-11 22:45:35 +0000862 resource_tree(root, debug_level, 0);
863}
864
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200865void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000866{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800867 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000868 int i;
869 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000870 struct bus *link;
871
Myles Watsonbb3d8122009-05-11 22:45:35 +0000872 for (i = 0; i < depth; i++)
873 depth_str[i] = ' ';
874 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000875
Myles Watsonc25cc112010-05-21 14:33:48 +0000876 do_printk(debug_level, "%s%s: enabled %d\n",
877 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000878
Myles Watson894a3472010-06-09 22:41:35 +0000879 for (link = dev->link_list; link; link = link->next) {
880 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000881 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300882 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000883 }
884}
885
886void show_all_devs_tree(int debug_level, const char *msg)
887{
888 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100889 if (!do_printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000890 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300891 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000892}
893
894void show_devs_subtree(struct device *root, int debug_level, const char *msg)
895{
896 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100897 if (!do_printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000898 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000899 return;
900 do_printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300901 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000902}
903
904void show_all_devs(int debug_level, const char *msg)
905{
906 struct device *dev;
907
908 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100909 if (!do_printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000910 return;
911 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000912 do_printk(debug_level, "%s: enabled %d\n",
913 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000914 }
915}
916
917void show_one_resource(int debug_level, struct device *dev,
918 struct resource *resource, const char *comment)
919{
920 char buf[10];
921 unsigned long long base, end;
922 base = resource->base;
923 end = resource_end(resource);
924 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000925
Uwe Hermanne4870472010-11-04 23:23:47 +0000926 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100927 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000928 resource->index, base, end, resource->size, resource->gran,
929 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000930}
931
932void show_all_devs_resources(int debug_level, const char* msg)
933{
934 struct device *dev;
935
Paul Menzeleb605d72015-02-12 00:29:32 +0100936 if (!do_printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000937 return;
938
939 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000940 struct resource *res;
941 do_printk(debug_level, "%s: enabled %d\n",
942 dev_path(dev), dev->enabled);
943 for (res = dev->resource_list; res; res = res->next)
944 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000945 }
946}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000947
Aaron Durbinf0349022017-11-10 10:47:54 -0700948void fixed_mem_resource(struct device *dev, unsigned long index,
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300949 unsigned long basek, unsigned long sizek, unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000950{
951 struct resource *resource;
952
953 if (!sizek)
954 return;
955
956 resource = new_resource(dev, index);
957 resource->base = ((resource_t)basek) << 10;
958 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300959 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
960 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000961
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300962 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300963}
964
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200965void mmconf_resource_init(struct resource *resource, resource_t base,
966 int buses)
967{
968 resource->base = base;
969 resource->size = buses * MiB;
970 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
971 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
972
973 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR "
974 "0x%08lx-0x%08lx.\n", (unsigned long)(resource->base),
975 (unsigned long)(resource->base + resource->size));
976}
977
978void mmconf_resource(struct device *dev, unsigned long index)
979{
980 struct resource *resource = new_resource(dev, index);
981 mmconf_resource_init(resource, CONFIG_MMCONF_BASE_ADDRESS,
982 CONFIG_MMCONF_BUS_NUMBER);
983}
984
Uwe Hermann4b42a622010-10-11 19:36:13 +0000985void tolm_test(void *gp, struct device *dev, struct resource *new)
986{
987 struct resource **best_p = gp;
988 struct resource *best;
989
990 best = *best_p;
991
992 if (!best || (best->base > new->base))
993 best = new;
994
995 *best_p = best;
996}
997
998u32 find_pci_tolm(struct bus *bus)
999{
1000 struct resource *min = NULL;
1001 u32 tolm;
1002
1003 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
1004 tolm_test, &min);
1005
1006 tolm = 0xffffffffUL;
1007
1008 if (min && tolm > min->base)
1009 tolm = min->base;
1010
1011 return tolm;
1012}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -07001013
1014/* Count of enabled CPUs */
1015int dev_count_cpu(void)
1016{
Aaron Durbinf0349022017-11-10 10:47:54 -07001017 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -07001018 int count = 0;
1019
1020 for (cpu = all_devices; cpu; cpu = cpu->next) {
1021 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -08001022 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -07001023 continue;
1024 if (!cpu->enabled)
1025 continue;
1026 count++;
1027 }
1028
1029 return count;
1030}
Lee Leahya95baf92016-02-13 06:10:04 -08001031
1032/* Get device path name */
1033const char *dev_path_name(enum device_path_type type)
1034{
1035 static const char *const type_names[] = DEVICE_PATH_NAMES;
1036 const char *type_name = "Unknown";
1037
1038 /* Translate the type value into a string */
1039 if (type < ARRAY_SIZE(type_names))
1040 type_name = type_names[type];
1041 return type_name;
1042}