blob: ea4ef27f9f0ba097ec39b96d0706d9fbfc166283 [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:
Felix Held3b5b66d2024-01-11 22:26:18 +0100100 ret |= dev->bus->segment_group << 16 | dev->bus->secondary << 8 | dev->path.pci.devfn;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700101 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;
Mario Scheithauer67f63e72022-11-02 15:57:10 +0100138 case DEVICE_PATH_MDIO:
139 ret |= dev->path.mdio.addr;
140 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700141 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800142 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700143 default:
144 break;
145 }
146
147 return ret;
148}
149
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000150/*
151 * Warning: This function uses a static buffer. Don't call it more than once
152 * from the same print statement!
153 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200154const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000155{
156 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000157
Eric Biedermane9a271e32003-09-02 03:36:25 +0000158 buffer[0] = '\0';
159 if (!dev) {
Angel Ponsd7df3832021-08-18 08:32:45 +0200160 strcpy(buffer, "<null>");
Uwe Hermanne4870472010-11-04 23:23:47 +0000161 } else {
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100162 switch (dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100163 case DEVICE_PATH_NONE:
Angel Ponsd7df3832021-08-18 08:32:45 +0200164 strcpy(buffer, "NONE");
Patrick Rudolph6a811842017-11-01 11:33:00 +0100165 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000166 case DEVICE_PATH_ROOT:
Angel Ponsd7df3832021-08-18 08:32:45 +0200167 strcpy(buffer, "Root Device");
Eric Biederman83b991a2003-10-11 06:20:25 +0000168 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000169 case DEVICE_PATH_PCI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200170 snprintf(buffer, sizeof(buffer),
Felix Held3b5b66d2024-01-11 22:26:18 +0100171 "PCI: %02x:%02x:%02x.%01x",
172 dev->bus->segment_group,
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100173 dev->bus->secondary,
174 PCI_SLOT(dev->path.pci.devfn),
175 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000176 break;
177 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200178 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100179 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000180 break;
181 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200182 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100183 dev->bus->secondary,
184 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000185 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000186 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200187 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100188 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000189 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200190 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200191 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100192 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200193 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800194 case DEVICE_PATH_DOMAIN:
Patrick Rudolphdb779802024-01-19 20:04:31 +0100195 snprintf(buffer, sizeof(buffer), "DOMAIN: %08x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800196 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000197 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800198 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200199 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800200 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000201 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000202 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200203 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100204 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000205 break;
206 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200207 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100208 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000209 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700210 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200211 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700212 "GENERIC: %d.%d", dev->path.generic.id,
213 dev->path.generic.subid);
214 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800215 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200216 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800217 dev->path.spi.cs);
218 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700219 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200220 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700221 dev->path.usb.port_type, dev->path.usb.port_id);
222 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800223 case DEVICE_PATH_MMIO:
Jacob Garberd552aca2019-07-16 12:55:00 -0600224 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800225 dev->path.mmio.addr);
226 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100227 case DEVICE_PATH_GPIO:
228 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
229 break;
Mario Scheithauer67f63e72022-11-02 15:57:10 +0100230 case DEVICE_PATH_MDIO:
231 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr);
232 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000233 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000234 printk(BIOS_ERR, "Unknown device path type: %d\n",
235 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000236 break;
237 }
238 }
239 return buffer;
240}
241
Furquan Shaikh5b5c2332020-04-24 21:31:35 -0700242const char *dev_name(const struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200243{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300244 if (dev->name)
245 return dev->name;
246 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200247 return dev->chip_ops->name;
248 else
249 return "unknown";
250}
251
Patrick Rudolphab6bcd22024-01-19 08:24:36 +0100252/* Returns the PCI domain for the given PCI device */
253struct device *dev_get_pci_domain(struct device *dev)
254{
255 /* Walk up the tree up to the PCI domain */
256 while (dev && dev->bus && !is_root_device(dev)) {
257 dev = dev->bus->dev;
258 if (dev->path.type == DEVICE_PATH_DOMAIN)
259 return dev;
260 }
261
262 return NULL;
263}
264
Eric Biederman5cd81732004-03-11 15:01:31 +0000265/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000266 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000267 *
268 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000269 */
270static int allocate_more_resources(void)
271{
272 int i;
273 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000274
Myles Watsonc25cc112010-05-21 14:33:48 +0000275 new_res_list = malloc(64 * sizeof(*new_res_list));
276
277 if (new_res_list == NULL)
278 return 0;
279
280 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
281
Uwe Hermanne4870472010-11-04 23:23:47 +0000282 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000283 new_res_list[i].next = &new_res_list[i+1];
284
285 free_resources = new_res_list;
286 return 1;
287}
288
289/**
290 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000291 *
292 * @param dev TODO
293 * @param res TODO
294 * @param prev TODO
295 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000296 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700297static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000298 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000299{
300 if (prev)
301 prev->next = res->next;
302 else
303 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000304
Myles Watsonc25cc112010-05-21 14:33:48 +0000305 res->next = free_resources;
306 free_resources = res;
307}
308
309/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000310 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000311 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000312 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000313 *
314 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000315 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700316void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000317{
Myles Watsonc25cc112010-05-21 14:33:48 +0000318 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000319
Eric Biederman5cd81732004-03-11 15:01:31 +0000320 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000321 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000322 next = res->next;
323 if (!res->flags)
324 free_resource(dev, res, prev);
325 else
326 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000327 }
328}
329
330/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000331 * See if a resource structure already exists for a given index.
332 *
333 * @param dev The device to find the resource on.
334 * @param index The index of the resource on the device.
335 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000336 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600337struct resource *probe_resource(const struct device *dev, unsigned int index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000338{
Myles Watsonc25cc112010-05-21 14:33:48 +0000339 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000340
Eric Biederman5cd81732004-03-11 15:01:31 +0000341 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000342 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000343 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000344 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000345 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000346
Myles Watsonc25cc112010-05-21 14:33:48 +0000347 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000348}
349
350/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000351 * See if a resource structure already exists for a given index and if not
352 * allocate one.
353 *
Paul Menzel20923872014-06-07 13:31:29 +0200354 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000355 *
356 * @param dev The device to find the resource on.
357 * @param index The index of the resource on the device.
358 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000359 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600360struct resource *new_resource(struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000361{
Myles Watsonc25cc112010-05-21 14:33:48 +0000362 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000363
Uwe Hermanne4870472010-11-04 23:23:47 +0000364 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000365 compact_resources(dev);
366
Uwe Hermanne4870472010-11-04 23:23:47 +0000367 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000368 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000369 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000370 if (free_resources == NULL && !allocate_more_resources())
371 die("Couldn't allocate more resources.");
372
373 resource = free_resources;
374 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000375 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000376 resource->next = NULL;
377 tail = dev->resource_list;
378 if (tail) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100379 while (tail->next)
380 tail = tail->next;
Myles Watsonc25cc112010-05-21 14:33:48 +0000381 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000382 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000383 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000384 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000385 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000386
387 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000388 if (!(resource->flags & IORESOURCE_FIXED)) {
389 resource->flags = 0;
390 resource->base = 0;
391 }
392 resource->size = 0;
393 resource->limit = 0;
394 resource->index = index;
395 resource->align = 0;
396 resource->gran = 0;
397
398 return resource;
399}
400
Eric Biederman03acab62004-10-14 21:25:53 +0000401/**
402 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000403 *
404 * @param dev The device to find the resource on.
405 * @param index The index of the resource on the device.
406 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000407 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600408struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000409{
410 struct resource *resource;
411
Uwe Hermanne4870472010-11-04 23:23:47 +0000412 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000413 resource = probe_resource(dev, index);
Nico Huber6a07db22023-05-12 15:46:24 +0200414 if (!resource)
415 die("%s missing resource: %02x\n", dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000416 return resource;
417}
418
Eric Biederman03acab62004-10-14 21:25:53 +0000419/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000420 * Round a number up to the next multiple of gran.
421 *
422 * @param val The starting value.
423 * @param gran Granularity we are aligning the number to.
424 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000425 */
426static resource_t align_up(resource_t val, unsigned long gran)
427{
428 resource_t mask;
429 mask = (1ULL << gran) - 1ULL;
430 val += mask;
431 val &= ~mask;
432 return val;
433}
434
435/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000436 * Round a number up to the previous multiple of gran.
437 *
438 * @param val The starting value.
439 * @param gran Granularity we are aligning the number to.
440 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000441 */
442static resource_t align_down(resource_t val, unsigned long gran)
443{
444 resource_t mask;
445 mask = (1ULL << gran) - 1ULL;
446 val &= ~mask;
447 return val;
448}
449
450/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000451 * Compute the maximum address that is part of a resource.
452 *
453 * @param resource The resource whose limit is desired.
454 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000455 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300456resource_t resource_end(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000457{
458 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000459
460 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000461 base = resource->base;
462
Uwe Hermanne4870472010-11-04 23:23:47 +0000463 /*
464 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000465 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000466 * the bridge. While the granularity is simply how many low bits of
467 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000468 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000469
Uwe Hermanne4870472010-11-04 23:23:47 +0000470 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000471 end = base + align_up(resource->size, resource->gran) - 1;
472
473 return end;
474}
475
476/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000477 * Compute the maximum legal value for resource->base.
478 *
479 * @param resource The resource whose maximum is desired.
480 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000481 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300482resource_t resource_max(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000483{
484 resource_t max;
485
486 max = align_down(resource->limit - resource->size + 1, resource->align);
487
488 return max;
489}
490
491/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000492 * Return the resource type of a resource.
493 *
494 * @param resource The resource type to decode.
495 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000496 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300497const char *resource_type(const struct resource *resource)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000498{
499 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200500 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100501 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
502 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
503 ((resource->flags == 0) ? "unused" :
504 (resource->flags & IORESOURCE_IO) ? "io" :
505 (resource->flags & IORESOURCE_DRQ) ? "drq" :
506 (resource->flags & IORESOURCE_IRQ) ? "irq" :
507 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
508 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000509 return buffer;
510}
511
512/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000513 * Print the resource that was just stored.
514 *
Martin Roth63373ed2013-07-08 16:24:19 -0600515 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000516 * @param resource The resource that was just stored.
517 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000518 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300519void report_resource_stored(struct device *dev, const struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000520 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000521{
Uwe Hermanne4870472010-11-04 23:23:47 +0000522 char buf[10];
523 unsigned long long base, end;
524
525 if (!(resource->flags & IORESOURCE_STORED))
526 return;
527
528 base = resource->base;
529 end = resource_end(resource);
530 buf[0] = '\0';
531
John Zhao4792f8f2020-09-22 14:52:42 -0700532 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200533 snprintf(buf, sizeof(buf),
Felix Held3b5b66d2024-01-11 22:26:18 +0100534 "seg %02x bus %02x ", dev->link_list->segment_group,
535 dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000536 }
Gang Chencfb90fd2022-07-02 01:24:09 +0800537 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000538 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
539 base, end, resource->size, resource->gran, buf,
540 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000541}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000542
Uwe Hermanne4870472010-11-04 23:23:47 +0000543void search_bus_resources(struct bus *bus, unsigned long type_mask,
544 unsigned long type, resource_search_t search,
545 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000546{
547 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000548
Myles Watson894a3472010-06-09 22:41:35 +0000549 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000550 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000551
552 /* Ignore disabled devices. */
553 if (!curdev->enabled)
554 continue;
555
Myles Watson894a3472010-06-09 22:41:35 +0000556 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000557 /* If it isn't the right kind of resource ignore it. */
558 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000559 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000560
561 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000562 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Arthur Heymans80c79a52023-08-24 15:12:19 +0200563 if (curdev->link_list)
564 search_bus_resources(curdev->link_list, type_mask, type,
565 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000566 continue;
567 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000568 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000569 }
570 }
571}
572
Uwe Hermanne4870472010-11-04 23:23:47 +0000573void search_global_resources(unsigned long type_mask, unsigned long type,
574 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000575{
576 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000577
Myles Watson894a3472010-06-09 22:41:35 +0000578 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000579 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000580
581 /* Ignore disabled devices. */
582 if (!curdev->enabled)
583 continue;
584
Myles Watson894a3472010-06-09 22:41:35 +0000585 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000586 /* If it isn't the right kind of resource ignore it. */
587 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000588 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000589
590 /* If it is a subtractive resource ignore it. */
591 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000592 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000593
Shuo Liu0640c282022-08-02 01:50:36 +0800594 /* If the resource is not assigned ignore it. */
595 if (!(res->flags & IORESOURCE_ASSIGNED))
596 continue;
597
Myles Watsonc25cc112010-05-21 14:33:48 +0000598 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000599 }
600 }
601}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000602
Aaron Durbinf0349022017-11-10 10:47:54 -0700603void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000604{
Uwe Hermanne4870472010-11-04 23:23:47 +0000605 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000606 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000607
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000608 dev->enabled = enable;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100609 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000610 dev->ops->enable(dev);
Frans Hendriksa9caa502021-02-01 11:44:37 +0100611 else if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000612 dev->chip_ops->enable_dev(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000613}
614
615void disable_children(struct bus *bus)
616{
Aaron Durbinf0349022017-11-10 10:47:54 -0700617 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000618
Myles Watson894a3472010-06-09 22:41:35 +0000619 for (child = bus->children; child; child = child->sibling) {
Arthur Heymans80c79a52023-08-24 15:12:19 +0200620 if (child->link_list)
621 disable_children(child->link_list);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000622 dev_set_enabled(child, 0);
623 }
624}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000625
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200626/*
627 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200628 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200629 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700630bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200631{
Aaron Durbinf0349022017-11-10 10:47:54 -0700632 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200633
634 if (!dev || !dev->enabled)
635 return 0;
636
637 if (!dev->link_list || !dev->link_list->children)
638 return 0;
639
Arthur Heymans80c79a52023-08-24 15:12:19 +0200640 for (child = dev->link_list->children; child; child = child->sibling) {
641 if (child->path.type == DEVICE_PATH_NONE)
642 continue;
643 if (child->enabled)
644 return 1;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200645 }
646
647 return 0;
648}
649
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200650static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000651{
Myles Watson894a3472010-06-09 22:41:35 +0000652 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000653 struct device *child;
Myles Watsonc25cc112010-05-21 14:33:48 +0000654 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000655 char indent[30]; /* If your tree has more levels, it's wrong. */
656
657 for (i = 0; i < depth + 1 && i < 29; i++)
658 indent[i] = ' ';
659 indent[i] = '\0';
660
Nico Huber7cc14ac2021-03-27 20:03:02 +0100661 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
Martin Roth7f35d3a2017-07-23 16:22:25 -0600662 if (root->link_list && root->link_list->children)
Nico Huber7cc14ac2021-03-27 20:03:02 +0100663 printk(BIOS_DEBUG, " child on link 0 %s",
Martin Roth7f35d3a2017-07-23 16:22:25 -0600664 dev_path(root->link_list->children));
Nico Huber7cc14ac2021-03-27 20:03:02 +0100665 printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000666
Myles Watsonc25cc112010-05-21 14:33:48 +0000667 for (res = root->resource_list; res; res = res->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100668 printk(debug_level, "%s%s resource base %llx size %llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000669 "align %d gran %d limit %llx flags %lx index %lx\n",
670 indent, dev_path(root), res->base, res->size,
671 res->align, res->gran, res->limit, res->flags,
672 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000673 }
674
Arthur Heymans80c79a52023-08-24 15:12:19 +0200675 if (!root->link_list)
676 return;
677
678 for (child = root->link_list->children; child; child = child->sibling)
679 resource_tree(child, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000680}
681
Elyes HAOUASe3480662018-05-06 20:32:23 +0200682void print_resource_tree(const struct device *root, int debug_level,
683 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000684{
685 /* Bail if root is null. */
686 if (!root) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100687 printk(debug_level, "%s passed NULL for root!\n", __func__);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000688 return;
689 }
690
691 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100692 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000693 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000694 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000695
Myles Watsonbb3d8122009-05-11 22:45:35 +0000696 resource_tree(root, debug_level, 0);
697}
698
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200699void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000700{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800701 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000702 int i;
703 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000704
Myles Watsonbb3d8122009-05-11 22:45:35 +0000705 for (i = 0; i < depth; i++)
706 depth_str[i] = ' ';
707 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000708
Nico Huber7cc14ac2021-03-27 20:03:02 +0100709 printk(debug_level, "%s%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000710 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000711
Arthur Heymans80c79a52023-08-24 15:12:19 +0200712 if (!dev->link_list)
713 return;
714
715 for (sibling = dev->link_list->children; sibling; sibling = sibling->sibling)
716 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000717}
718
719void show_all_devs_tree(int debug_level, const char *msg)
720{
721 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100722 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000723 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300724 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000725}
726
727void show_devs_subtree(struct device *root, int debug_level, const char *msg)
728{
729 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100730 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000731 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000732 return;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100733 printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300734 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000735}
736
737void show_all_devs(int debug_level, const char *msg)
738{
739 struct device *dev;
740
741 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100742 if (!printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000743 return;
744 for (dev = all_devices; dev; dev = dev->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100745 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000746 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000747 }
748}
749
750void show_one_resource(int debug_level, struct device *dev,
751 struct resource *resource, const char *comment)
752{
753 char buf[10];
754 unsigned long long base, end;
755 base = resource->base;
756 end = resource_end(resource);
757 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000758
Gang Chencfb90fd2022-07-02 01:24:09 +0800759 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100760 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000761 resource->index, base, end, resource->size, resource->gran,
762 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000763}
764
Frans Hendriksa9caa502021-02-01 11:44:37 +0100765void show_all_devs_resources(int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000766{
767 struct device *dev;
768
Nico Huber7cc14ac2021-03-27 20:03:02 +0100769 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000770 return;
771
772 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000773 struct resource *res;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100774 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000775 dev_path(dev), dev->enabled);
776 for (res = dev->resource_list; res; res = res->next)
777 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000778 }
779}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000780
Kyösti Mälkkice345962021-06-11 19:31:22 +0300781const struct resource *fixed_resource_range_idx(struct device *dev, unsigned long index,
782 uint64_t base, uint64_t size, unsigned long flags)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000783{
784 struct resource *resource;
Kyösti Mälkkice345962021-06-11 19:31:22 +0300785 if (!size)
786 return NULL;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000787
788 resource = new_resource(dev, index);
Kyösti Mälkkice345962021-06-11 19:31:22 +0300789 resource->base = base;
790 resource->size = size;
791 resource->flags = IORESOURCE_FIXED | IORESOURCE_ASSIGNED;
792 resource->flags |= flags;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000793
Kyösti Mälkkice345962021-06-11 19:31:22 +0300794 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
795 dev_path(dev), resource->index, resource->base, resource->size);
796
797 return resource;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300798}
799
Kyösti Mälkkid0525d42021-06-13 10:09:51 +0300800const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
801{
802 return ram_from_to(dev, index, 0, end);
803}
804
805const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
806{
807 if (end <= 4ull * GiB)
808 return NULL;
809
810 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
811
812 return ram_from_to(dev, index, 4ull * GiB, end);
813}
814
Angel Pons90be7542021-01-20 13:03:58 +0100815void mmconf_resource(struct device *dev, unsigned long index)
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200816{
Angel Pons90be7542021-01-20 13:03:58 +0100817 struct resource *resource = new_resource(dev, index);
Shelley Chen4e9bb332021-10-20 15:43:45 -0700818 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
819 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200820 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
821 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
822
Angel Ponsd19cc112021-07-04 11:41:31 +0200823 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
824 (unsigned long)(resource->base),
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200825 (unsigned long)(resource->base + resource->size));
826}
827
Uwe Hermann4b42a622010-10-11 19:36:13 +0000828void tolm_test(void *gp, struct device *dev, struct resource *new)
829{
830 struct resource **best_p = gp;
831 struct resource *best;
832
833 best = *best_p;
834
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700835 /*
836 * If resource is not allocated any space i.e. size is zero,
837 * then do not consider this resource in tolm calculations.
838 */
839 if (new->size == 0)
840 return;
841
Uwe Hermann4b42a622010-10-11 19:36:13 +0000842 if (!best || (best->base > new->base))
843 best = new;
844
845 *best_p = best;
846}
847
848u32 find_pci_tolm(struct bus *bus)
849{
850 struct resource *min = NULL;
851 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700852 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000853
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700854 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000855
856 tolm = 0xffffffffUL;
857
858 if (min && tolm > min->base)
859 tolm = min->base;
860
861 return tolm;
862}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700863
864/* Count of enabled CPUs */
865int dev_count_cpu(void)
866{
Aaron Durbinf0349022017-11-10 10:47:54 -0700867 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700868 int count = 0;
869
870 for (cpu = all_devices; cpu; cpu = cpu->next) {
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200871 if (!is_enabled_cpu(cpu))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700872 continue;
873 count++;
874 }
875
876 return count;
877}
Lee Leahya95baf92016-02-13 06:10:04 -0800878
879/* Get device path name */
880const char *dev_path_name(enum device_path_type type)
881{
882 static const char *const type_names[] = DEVICE_PATH_NAMES;
883 const char *type_name = "Unknown";
884
885 /* Translate the type value into a string */
886 if (type < ARRAY_SIZE(type_names))
887 type_name = type_names[type];
888 return type_name;
889}
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300890
Nico Huber577c6b92022-08-15 00:08:58 +0200891bool dev_path_hotplug(const struct device *dev)
892{
893 for (dev = dev->bus->dev; dev != dev->bus->dev; dev = dev->bus->dev) {
894 if (dev->hotplug_port)
895 return true;
896 }
897 return false;
898}
899
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300900void log_resource(const char *type, const struct device *dev, const struct resource *res,
901 const char *srcfile, const int line)
902{
903 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
904 "end: 0x%llx, size_kb: 0x%llx\n",
905 srcfile, line, type, dev_path(dev), res->index, res->base,
906 resource_end(res), res->size / KiB);
907}
Fabio Aiutoc5573d62022-09-10 14:23:38 +0200908
909bool is_cpu(const struct device *cpu)
910{
911 return cpu->path.type == DEVICE_PATH_APIC &&
912 cpu->bus->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
913}
914
915bool is_enabled_cpu(const struct device *cpu)
916{
917 return is_cpu(cpu) && cpu->enabled;
918}
Fabio Aiuto4fce79f2022-09-30 11:22:09 +0200919
920bool is_pci(const struct device *pci)
921{
922 return pci->path.type == DEVICE_PATH_PCI;
923}
924
925bool is_enabled_pci(const struct device *pci)
926{
927 return is_pci(pci) && pci->enabled;
928}
929
930bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
931{
Felix Held3b5b66d2024-01-11 22:26:18 +0100932 return is_pci(pci) && pci->bus->segment_group == 0 && pci->bus->secondary == bus;
Fabio Aiuto4fce79f2022-09-30 11:22:09 +0200933}