blob: 9beb2ce8e2e837e478bb28b1a88467ec5c706cd0 [file] [log] [blame]
Angel Ponsc74dae92020-04-02 23:48:16 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Uwe Hermannb80dbf02007-04-22 19:08:13 +00002
Elyes Haouas04c3b5a2022-10-07 10:08:05 +02003#include <commonlib/bsd/helpers.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00004#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +00005#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +00006#include <device/path.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +02007#include <device/pci_def.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00008#include <device/resource.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +02009#include <stdlib.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000010#include <string.h>
Elyes Haouas04c3b5a2022-10-07 10:08:05 +020011#include <types.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000012
Eric Biederman03acab62004-10-14 21:25:53 +000013/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070014 * Given a Local APIC ID, find the device structure.
15 *
16 * @param apic_id The Local APIC ID number.
17 * @return Pointer to the device structure (if found), 0 otherwise.
18 */
Martin Roth38ddbfb2019-10-23 21:41:00 -060019struct device *dev_find_lapic(unsigned int apic_id)
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070020{
Aaron Durbinf0349022017-11-10 10:47:54 -070021 struct device *dev;
22 struct device *result = NULL;
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070023
24 for (dev = all_devices; dev; dev = dev->next) {
25 if (dev->path.type == DEVICE_PATH_APIC &&
26 dev->path.apic.apic_id == apic_id) {
27 result = dev;
28 break;
29 }
30 }
31 return result;
32}
33
34/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000035 * Find a device of a given vendor and type.
36 *
37 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
38 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +000039 * @param from Pointer to the device structure, used as a starting point in
40 * the linked list of all_devices, which can be 0 to start at the
41 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000042 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000043 */
Uwe Hermanne4870472010-11-04 23:23:47 +000044struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +000045{
46 if (!from)
47 from = all_devices;
48 else
49 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000050
51 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +000052 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000053
Eric Biederman8ca8d762003-04-22 19:02:15 +000054 return from;
55}
56
Uwe Hermannc1ee4292010-10-17 19:01:48 +000057/**
58 * Find a device of a given class.
59 *
60 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +000061 * @param from Pointer to the device structure, used as a starting point in
62 * the linked list of all_devices, which can be 0 to start at the
63 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000064 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000065 */
66struct device *dev_find_class(unsigned int class, struct device *from)
67{
68 if (!from)
69 from = all_devices;
70 else
71 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000072
Greg Watson59651692003-12-17 17:39:53 +000073 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +000074 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000075
Eric Biederman8ca8d762003-04-22 19:02:15 +000076 return from;
77}
78
Duncan Laurie5f5d9142013-06-10 09:59:17 -070079/**
80 * Encode the device path into 3 bytes for logging to CMOS.
81 *
82 * @param dev The device path to encode.
83 * @return Device path encoded into lower 3 bytes of dword.
84 */
Subrata Banik564547f2018-05-02 10:27:36 +053085u32 dev_path_encode(const struct device *dev)
Duncan Laurie5f5d9142013-06-10 09:59:17 -070086{
87 u32 ret;
88
89 if (!dev)
90 return 0;
91
92 /* Store the device type in 3rd byte. */
93 ret = dev->path.type << 16;
94
95 /* Encode the device specific path in the low word. */
96 switch (dev->path.type) {
97 case DEVICE_PATH_ROOT:
98 break;
99 case DEVICE_PATH_PCI:
100 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
101 break;
102 case DEVICE_PATH_PNP:
103 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
104 break;
105 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700106 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700107 break;
108 case DEVICE_PATH_APIC:
109 ret |= dev->path.apic.apic_id;
110 break;
111 case DEVICE_PATH_DOMAIN:
112 ret |= dev->path.domain.domain;
113 break;
114 case DEVICE_PATH_CPU_CLUSTER:
115 ret |= dev->path.cpu_cluster.cluster;
116 break;
117 case DEVICE_PATH_CPU:
118 ret |= dev->path.cpu.id;
119 break;
120 case DEVICE_PATH_CPU_BUS:
121 ret |= dev->path.cpu_bus.id;
122 break;
123 case DEVICE_PATH_IOAPIC:
124 ret |= dev->path.ioapic.ioapic_id;
125 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700126 case DEVICE_PATH_GENERIC:
127 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
128 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800129 case DEVICE_PATH_SPI:
130 ret |= dev->path.spi.cs;
131 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700132 case DEVICE_PATH_USB:
Karthikeyan Ramasubramanian19398242019-07-11 12:23:35 -0600133 ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700134 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100135 case DEVICE_PATH_GPIO:
136 ret |= dev->path.gpio.id;
137 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700138 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800139 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700140 default:
141 break;
142 }
143
144 return ret;
145}
146
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000147/*
148 * Warning: This function uses a static buffer. Don't call it more than once
149 * from the same print statement!
150 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200151const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000152{
153 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000154
Eric Biedermane9a271e32003-09-02 03:36:25 +0000155 buffer[0] = '\0';
156 if (!dev) {
Angel Ponsd7df3832021-08-18 08:32:45 +0200157 strcpy(buffer, "<null>");
Uwe Hermanne4870472010-11-04 23:23:47 +0000158 } else {
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100159 switch (dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100160 case DEVICE_PATH_NONE:
Angel Ponsd7df3832021-08-18 08:32:45 +0200161 strcpy(buffer, "NONE");
Patrick Rudolph6a811842017-11-01 11:33:00 +0100162 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000163 case DEVICE_PATH_ROOT:
Angel Ponsd7df3832021-08-18 08:32:45 +0200164 strcpy(buffer, "Root Device");
Eric Biederman83b991a2003-10-11 06:20:25 +0000165 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000166 case DEVICE_PATH_PCI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200167 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100168 "PCI: %02x:%02x.%01x",
169 dev->bus->secondary,
170 PCI_SLOT(dev->path.pci.devfn),
171 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000172 break;
173 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200174 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100175 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000176 break;
177 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200178 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100179 dev->bus->secondary,
180 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000181 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000182 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200183 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100184 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000185 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200186 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200187 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100188 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200189 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800190 case DEVICE_PATH_DOMAIN:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200191 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800192 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000193 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800194 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200195 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800196 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000197 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000198 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200199 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100200 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000201 break;
202 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200203 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100204 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000205 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700206 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200207 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700208 "GENERIC: %d.%d", dev->path.generic.id,
209 dev->path.generic.subid);
210 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800211 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200212 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800213 dev->path.spi.cs);
214 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700215 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200216 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700217 dev->path.usb.port_type, dev->path.usb.port_id);
218 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800219 case DEVICE_PATH_MMIO:
Jacob Garberd552aca2019-07-16 12:55:00 -0600220 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800221 dev->path.mmio.addr);
222 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100223 case DEVICE_PATH_GPIO:
224 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
225 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000226 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000227 printk(BIOS_ERR, "Unknown device path type: %d\n",
228 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000229 break;
230 }
231 }
232 return buffer;
233}
234
Furquan Shaikh5b5c2332020-04-24 21:31:35 -0700235const char *dev_name(const struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200236{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300237 if (dev->name)
238 return dev->name;
239 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200240 return dev->chip_ops->name;
241 else
242 return "unknown";
243}
244
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000245const char *bus_path(struct bus *bus)
246{
247 static char buffer[BUS_PATH_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200248 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100249 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000250 return buffer;
251}
252
Eric Biederman5cd81732004-03-11 15:01:31 +0000253/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000254 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000255 *
256 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000257 */
258static int allocate_more_resources(void)
259{
260 int i;
261 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000262
Myles Watsonc25cc112010-05-21 14:33:48 +0000263 new_res_list = malloc(64 * sizeof(*new_res_list));
264
265 if (new_res_list == NULL)
266 return 0;
267
268 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
269
Uwe Hermanne4870472010-11-04 23:23:47 +0000270 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000271 new_res_list[i].next = &new_res_list[i+1];
272
273 free_resources = new_res_list;
274 return 1;
275}
276
277/**
278 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000279 *
280 * @param dev TODO
281 * @param res TODO
282 * @param prev TODO
283 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000284 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700285static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000286 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000287{
288 if (prev)
289 prev->next = res->next;
290 else
291 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000292
Myles Watsonc25cc112010-05-21 14:33:48 +0000293 res->next = free_resources;
294 free_resources = res;
295}
296
297/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000298 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000299 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000300 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000301 *
302 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000303 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700304void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000305{
Myles Watsonc25cc112010-05-21 14:33:48 +0000306 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000307
Eric Biederman5cd81732004-03-11 15:01:31 +0000308 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000309 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000310 next = res->next;
311 if (!res->flags)
312 free_resource(dev, res, prev);
313 else
314 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000315 }
316}
317
318/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000319 * See if a resource structure already exists for a given index.
320 *
321 * @param dev The device to find the resource on.
322 * @param index The index of the resource on the device.
323 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000324 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600325struct resource *probe_resource(const struct device *dev, unsigned int index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000326{
Myles Watsonc25cc112010-05-21 14:33:48 +0000327 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000328
Eric Biederman5cd81732004-03-11 15:01:31 +0000329 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000330 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000331 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000332 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000333 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000334
Myles Watsonc25cc112010-05-21 14:33:48 +0000335 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000336}
337
338/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000339 * See if a resource structure already exists for a given index and if not
340 * allocate one.
341 *
Paul Menzel20923872014-06-07 13:31:29 +0200342 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000343 *
344 * @param dev The device to find the resource on.
345 * @param index The index of the resource on the device.
346 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000347 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600348struct resource *new_resource(struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000349{
Myles Watsonc25cc112010-05-21 14:33:48 +0000350 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000351
Uwe Hermanne4870472010-11-04 23:23:47 +0000352 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000353 compact_resources(dev);
354
Uwe Hermanne4870472010-11-04 23:23:47 +0000355 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000356 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000357 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000358 if (free_resources == NULL && !allocate_more_resources())
359 die("Couldn't allocate more resources.");
360
361 resource = free_resources;
362 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000363 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000364 resource->next = NULL;
365 tail = dev->resource_list;
366 if (tail) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100367 while (tail->next)
368 tail = tail->next;
Myles Watsonc25cc112010-05-21 14:33:48 +0000369 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000370 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000371 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000372 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000373 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000374
375 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000376 if (!(resource->flags & IORESOURCE_FIXED)) {
377 resource->flags = 0;
378 resource->base = 0;
379 }
380 resource->size = 0;
381 resource->limit = 0;
382 resource->index = index;
383 resource->align = 0;
384 resource->gran = 0;
385
386 return resource;
387}
388
Eric Biederman03acab62004-10-14 21:25:53 +0000389/**
390 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000391 *
392 * @param dev The device to find the resource on.
393 * @param index The index of the resource on the device.
394 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000395 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600396struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000397{
398 struct resource *resource;
399
Uwe Hermanne4870472010-11-04 23:23:47 +0000400 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000401 resource = probe_resource(dev, index);
402 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000403 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000404 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000405 die("");
406 }
407 return resource;
408}
409
Eric Biederman03acab62004-10-14 21:25:53 +0000410/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000411 * Round a number up to the next multiple of gran.
412 *
413 * @param val The starting value.
414 * @param gran Granularity we are aligning the number to.
415 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000416 */
417static resource_t align_up(resource_t val, unsigned long gran)
418{
419 resource_t mask;
420 mask = (1ULL << gran) - 1ULL;
421 val += mask;
422 val &= ~mask;
423 return val;
424}
425
426/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000427 * Round a number up to the previous multiple of gran.
428 *
429 * @param val The starting value.
430 * @param gran Granularity we are aligning the number to.
431 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000432 */
433static resource_t align_down(resource_t val, unsigned long gran)
434{
435 resource_t mask;
436 mask = (1ULL << gran) - 1ULL;
437 val &= ~mask;
438 return val;
439}
440
441/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000442 * Compute the maximum address that is part of a resource.
443 *
444 * @param resource The resource whose limit is desired.
445 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000446 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300447resource_t resource_end(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000448{
449 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000450
451 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000452 base = resource->base;
453
Uwe Hermanne4870472010-11-04 23:23:47 +0000454 /*
455 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000456 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000457 * the bridge. While the granularity is simply how many low bits of
458 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000459 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000460
Uwe Hermanne4870472010-11-04 23:23:47 +0000461 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000462 end = base + align_up(resource->size, resource->gran) - 1;
463
464 return end;
465}
466
467/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000468 * Compute the maximum legal value for resource->base.
469 *
470 * @param resource The resource whose maximum is desired.
471 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000472 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300473resource_t resource_max(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000474{
475 resource_t max;
476
477 max = align_down(resource->limit - resource->size + 1, resource->align);
478
479 return max;
480}
481
482/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000483 * Return the resource type of a resource.
484 *
485 * @param resource The resource type to decode.
486 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000487 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300488const char *resource_type(const struct resource *resource)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000489{
490 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200491 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100492 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
493 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
494 ((resource->flags == 0) ? "unused" :
495 (resource->flags & IORESOURCE_IO) ? "io" :
496 (resource->flags & IORESOURCE_DRQ) ? "drq" :
497 (resource->flags & IORESOURCE_IRQ) ? "irq" :
498 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
499 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000500 return buffer;
501}
502
503/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000504 * Print the resource that was just stored.
505 *
Martin Roth63373ed2013-07-08 16:24:19 -0600506 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000507 * @param resource The resource that was just stored.
508 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000509 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300510void report_resource_stored(struct device *dev, const struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000511 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000512{
Uwe Hermanne4870472010-11-04 23:23:47 +0000513 char buf[10];
514 unsigned long long base, end;
515
516 if (!(resource->flags & IORESOURCE_STORED))
517 return;
518
519 base = resource->base;
520 end = resource_end(resource);
521 buf[0] = '\0';
522
John Zhao4792f8f2020-09-22 14:52:42 -0700523 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200524 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100525 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000526 }
Gang Chencfb90fd2022-07-02 01:24:09 +0800527 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000528 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
529 base, end, resource->size, resource->gran, buf,
530 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000531}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000532
Uwe Hermanne4870472010-11-04 23:23:47 +0000533void search_bus_resources(struct bus *bus, unsigned long type_mask,
534 unsigned long type, resource_search_t search,
535 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000536{
537 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000538
Myles Watson894a3472010-06-09 22:41:35 +0000539 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000540 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000541
542 /* Ignore disabled devices. */
543 if (!curdev->enabled)
544 continue;
545
Myles Watson894a3472010-06-09 22:41:35 +0000546 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000547 /* If it isn't the right kind of resource ignore it. */
548 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000549 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000550
551 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000552 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100553 struct bus *subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000554 for (subbus = curdev->link_list; subbus;
555 subbus = subbus->next)
556 if (subbus->link_num
557 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000558 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700559 if (!subbus) /* Why can subbus be NULL? */
560 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000561 search_bus_resources(subbus, type_mask, type,
562 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000563 continue;
564 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000565 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000566 }
567 }
568}
569
Uwe Hermanne4870472010-11-04 23:23:47 +0000570void search_global_resources(unsigned long type_mask, unsigned long type,
571 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000572{
573 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000574
Myles Watson894a3472010-06-09 22:41:35 +0000575 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000576 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000577
578 /* Ignore disabled devices. */
579 if (!curdev->enabled)
580 continue;
581
Myles Watson894a3472010-06-09 22:41:35 +0000582 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000583 /* If it isn't the right kind of resource ignore it. */
584 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000585 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000586
587 /* If it is a subtractive resource ignore it. */
588 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000589 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000590
Shuo Liu0640c282022-08-02 01:50:36 +0800591 /* If the resource is not assigned ignore it. */
592 if (!(res->flags & IORESOURCE_ASSIGNED))
593 continue;
594
Myles Watsonc25cc112010-05-21 14:33:48 +0000595 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000596 }
597 }
598}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000599
Aaron Durbinf0349022017-11-10 10:47:54 -0700600void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000601{
Uwe Hermanne4870472010-11-04 23:23:47 +0000602 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000603 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000604
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000605 dev->enabled = enable;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100606 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000607 dev->ops->enable(dev);
Frans Hendriksa9caa502021-02-01 11:44:37 +0100608 else if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000609 dev->chip_ops->enable_dev(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000610}
611
612void disable_children(struct bus *bus)
613{
Aaron Durbinf0349022017-11-10 10:47:54 -0700614 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000615
Myles Watson894a3472010-06-09 22:41:35 +0000616 for (child = bus->children; child; child = child->sibling) {
617 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000618 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000619 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000620 dev_set_enabled(child, 0);
621 }
622}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000623
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200624/*
625 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200626 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200627 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700628bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200629{
630 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700631 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200632
633 if (!dev || !dev->enabled)
634 return 0;
635
636 if (!dev->link_list || !dev->link_list->children)
637 return 0;
638
639 for (link = dev->link_list; link; link = link->next) {
640 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200641 if (child->path.type == DEVICE_PATH_NONE)
642 continue;
643
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200644 if (child->enabled)
645 return 1;
646 }
647 }
648
649 return 0;
650}
651
Jacob Garber464f4d62019-06-05 17:03:30 -0600652/**
653 * Ensure the device has a minimum number of bus links.
654 *
655 * @param dev The device to add links to.
656 * @param total_links The minimum number of links to have.
657 */
658void add_more_links(struct device *dev, unsigned int total_links)
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600659{
660 struct bus *link, *last = NULL;
661 int link_num = -1;
662
663 for (link = dev->link_list; link; link = link->next) {
664 if (link_num < link->link_num)
665 link_num = link->link_num;
666 last = link;
667 }
668
669 if (last) {
670 int links = total_links - (link_num + 1);
671 if (links > 0) {
Jacob Garber464f4d62019-06-05 17:03:30 -0600672 link = malloc(links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600673 if (!link)
674 die("Couldn't allocate more links!\n");
Jacob Garber464f4d62019-06-05 17:03:30 -0600675 memset(link, 0, links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600676 last->next = link;
Jacob Garber464f4d62019-06-05 17:03:30 -0600677 } else {
678 /* No more links to add */
679 return;
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600680 }
681 } else {
Jacob Garber464f4d62019-06-05 17:03:30 -0600682 link = malloc(total_links * sizeof(*link));
683 if (!link)
684 die("Couldn't allocate more links!\n");
685 memset(link, 0, total_links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600686 dev->link_list = link;
687 }
688
689 for (link_num = link_num + 1; link_num < total_links; link_num++) {
690 link->link_num = link_num;
691 link->dev = dev;
692 link->next = link + 1;
693 last = link;
694 link = link->next;
695 }
696 last->next = NULL;
697}
698
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200699static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000700{
Myles Watson894a3472010-06-09 22:41:35 +0000701 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000702 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000703 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000704 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000705 char indent[30]; /* If your tree has more levels, it's wrong. */
706
707 for (i = 0; i < depth + 1 && i < 29; i++)
708 indent[i] = ' ';
709 indent[i] = '\0';
710
Nico Huber7cc14ac2021-03-27 20:03:02 +0100711 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
Martin Roth7f35d3a2017-07-23 16:22:25 -0600712 if (root->link_list && root->link_list->children)
Nico Huber7cc14ac2021-03-27 20:03:02 +0100713 printk(BIOS_DEBUG, " child on link 0 %s",
Martin Roth7f35d3a2017-07-23 16:22:25 -0600714 dev_path(root->link_list->children));
Nico Huber7cc14ac2021-03-27 20:03:02 +0100715 printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000716
Myles Watsonc25cc112010-05-21 14:33:48 +0000717 for (res = root->resource_list; res; res = res->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100718 printk(debug_level, "%s%s resource base %llx size %llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000719 "align %d gran %d limit %llx flags %lx index %lx\n",
720 indent, dev_path(root), res->base, res->size,
721 res->align, res->gran, res->limit, res->flags,
722 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000723 }
724
Myles Watson894a3472010-06-09 22:41:35 +0000725 for (link = root->link_list; link; link = link->next) {
726 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000727 resource_tree(child, debug_level, depth + 1);
728 }
729}
730
Elyes HAOUASe3480662018-05-06 20:32:23 +0200731void print_resource_tree(const struct device *root, int debug_level,
732 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000733{
734 /* Bail if root is null. */
735 if (!root) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100736 printk(debug_level, "%s passed NULL for root!\n", __func__);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000737 return;
738 }
739
740 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100741 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000742 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000743 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000744
Myles Watsonbb3d8122009-05-11 22:45:35 +0000745 resource_tree(root, debug_level, 0);
746}
747
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200748void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000749{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800750 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000751 int i;
752 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000753 struct bus *link;
754
Myles Watsonbb3d8122009-05-11 22:45:35 +0000755 for (i = 0; i < depth; i++)
756 depth_str[i] = ' ';
757 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000758
Nico Huber7cc14ac2021-03-27 20:03:02 +0100759 printk(debug_level, "%s%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000760 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000761
Myles Watson894a3472010-06-09 22:41:35 +0000762 for (link = dev->link_list; link; link = link->next) {
763 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000764 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300765 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000766 }
767}
768
769void show_all_devs_tree(int debug_level, const char *msg)
770{
771 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100772 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000773 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300774 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000775}
776
777void show_devs_subtree(struct device *root, int debug_level, const char *msg)
778{
779 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100780 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000781 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000782 return;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100783 printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300784 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000785}
786
787void show_all_devs(int debug_level, const char *msg)
788{
789 struct device *dev;
790
791 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100792 if (!printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000793 return;
794 for (dev = all_devices; dev; dev = dev->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100795 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000796 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000797 }
798}
799
800void show_one_resource(int debug_level, struct device *dev,
801 struct resource *resource, const char *comment)
802{
803 char buf[10];
804 unsigned long long base, end;
805 base = resource->base;
806 end = resource_end(resource);
807 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000808
Gang Chencfb90fd2022-07-02 01:24:09 +0800809 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100810 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000811 resource->index, base, end, resource->size, resource->gran,
812 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000813}
814
Frans Hendriksa9caa502021-02-01 11:44:37 +0100815void show_all_devs_resources(int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000816{
817 struct device *dev;
818
Nico Huber7cc14ac2021-03-27 20:03:02 +0100819 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000820 return;
821
822 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000823 struct resource *res;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100824 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000825 dev_path(dev), dev->enabled);
826 for (res = dev->resource_list; res; res = res->next)
827 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000828 }
829}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000830
Kyösti Mälkkice345962021-06-11 19:31:22 +0300831const struct resource *fixed_resource_range_idx(struct device *dev, unsigned long index,
832 uint64_t base, uint64_t size, unsigned long flags)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000833{
834 struct resource *resource;
Kyösti Mälkkice345962021-06-11 19:31:22 +0300835 if (!size)
836 return NULL;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000837
838 resource = new_resource(dev, index);
Kyösti Mälkkice345962021-06-11 19:31:22 +0300839 resource->base = base;
840 resource->size = size;
841 resource->flags = IORESOURCE_FIXED | IORESOURCE_ASSIGNED;
842 resource->flags |= flags;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000843
Kyösti Mälkkice345962021-06-11 19:31:22 +0300844 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
845 dev_path(dev), resource->index, resource->base, resource->size);
846
847 return resource;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300848}
849
Kyösti Mälkkid0525d42021-06-13 10:09:51 +0300850const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
851{
852 return ram_from_to(dev, index, 0, end);
853}
854
855const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
856{
857 if (end <= 4ull * GiB)
858 return NULL;
859
860 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
861
862 return ram_from_to(dev, index, 4ull * GiB, end);
863}
864
Angel Pons90be7542021-01-20 13:03:58 +0100865void mmconf_resource(struct device *dev, unsigned long index)
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200866{
Angel Pons90be7542021-01-20 13:03:58 +0100867 struct resource *resource = new_resource(dev, index);
Shelley Chen4e9bb332021-10-20 15:43:45 -0700868 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
869 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200870 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
871 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
872
Angel Ponsd19cc112021-07-04 11:41:31 +0200873 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
874 (unsigned long)(resource->base),
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200875 (unsigned long)(resource->base + resource->size));
876}
877
Uwe Hermann4b42a622010-10-11 19:36:13 +0000878void tolm_test(void *gp, struct device *dev, struct resource *new)
879{
880 struct resource **best_p = gp;
881 struct resource *best;
882
883 best = *best_p;
884
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700885 /*
886 * If resource is not allocated any space i.e. size is zero,
887 * then do not consider this resource in tolm calculations.
888 */
889 if (new->size == 0)
890 return;
891
Uwe Hermann4b42a622010-10-11 19:36:13 +0000892 if (!best || (best->base > new->base))
893 best = new;
894
895 *best_p = best;
896}
897
898u32 find_pci_tolm(struct bus *bus)
899{
900 struct resource *min = NULL;
901 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700902 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000903
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700904 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000905
906 tolm = 0xffffffffUL;
907
908 if (min && tolm > min->base)
909 tolm = min->base;
910
911 return tolm;
912}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700913
914/* Count of enabled CPUs */
915int dev_count_cpu(void)
916{
Aaron Durbinf0349022017-11-10 10:47:54 -0700917 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700918 int count = 0;
919
920 for (cpu = all_devices; cpu; cpu = cpu->next) {
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200921 if (!is_enabled_cpu(cpu))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700922 continue;
923 count++;
924 }
925
926 return count;
927}
Lee Leahya95baf92016-02-13 06:10:04 -0800928
929/* Get device path name */
930const char *dev_path_name(enum device_path_type type)
931{
932 static const char *const type_names[] = DEVICE_PATH_NAMES;
933 const char *type_name = "Unknown";
934
935 /* Translate the type value into a string */
936 if (type < ARRAY_SIZE(type_names))
937 type_name = type_names[type];
938 return type_name;
939}
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300940
Nico Huber577c6b92022-08-15 00:08:58 +0200941bool dev_path_hotplug(const struct device *dev)
942{
943 for (dev = dev->bus->dev; dev != dev->bus->dev; dev = dev->bus->dev) {
944 if (dev->hotplug_port)
945 return true;
946 }
947 return false;
948}
949
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300950void log_resource(const char *type, const struct device *dev, const struct resource *res,
951 const char *srcfile, const int line)
952{
953 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
954 "end: 0x%llx, size_kb: 0x%llx\n",
955 srcfile, line, type, dev_path(dev), res->index, res->base,
956 resource_end(res), res->size / KiB);
957}
Fabio Aiutoc5573d62022-09-10 14:23:38 +0200958
959bool is_cpu(const struct device *cpu)
960{
961 return cpu->path.type == DEVICE_PATH_APIC &&
962 cpu->bus->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
963}
964
965bool is_enabled_cpu(const struct device *cpu)
966{
967 return is_cpu(cpu) && cpu->enabled;
968}
Fabio Aiuto4fce79f2022-09-30 11:22:09 +0200969
970bool is_pci(const struct device *pci)
971{
972 return pci->path.type == DEVICE_PATH_PCI;
973}
974
975bool is_enabled_pci(const struct device *pci)
976{
977 return is_pci(pci) && pci->enabled;
978}
979
980bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
981{
982 return is_pci(pci) && pci->bus->secondary == bus;
983}