blob: c5e03f20795ffae208ad32585ce496a03d7097c9 [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;
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),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100171 "PCI: %02x:%02x.%01x",
172 dev->bus->secondary,
173 PCI_SLOT(dev->path.pci.devfn),
174 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000175 break;
176 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200177 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100178 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000179 break;
180 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200181 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100182 dev->bus->secondary,
183 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000184 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000185 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200186 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100187 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000188 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200189 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200190 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100191 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200192 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800193 case DEVICE_PATH_DOMAIN:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200194 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800195 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000196 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800197 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200198 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800199 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000200 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000201 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200202 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100203 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000204 break;
205 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200206 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100207 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000208 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700209 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200210 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700211 "GENERIC: %d.%d", dev->path.generic.id,
212 dev->path.generic.subid);
213 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800214 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200215 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800216 dev->path.spi.cs);
217 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700218 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200219 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700220 dev->path.usb.port_type, dev->path.usb.port_id);
221 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800222 case DEVICE_PATH_MMIO:
Jacob Garberd552aca2019-07-16 12:55:00 -0600223 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800224 dev->path.mmio.addr);
225 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100226 case DEVICE_PATH_GPIO:
227 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
228 break;
Mario Scheithauer67f63e72022-11-02 15:57:10 +0100229 case DEVICE_PATH_MDIO:
230 snprintf(buffer, sizeof(buffer), "MDIO: %02x", dev->path.mdio.addr);
231 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000232 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000233 printk(BIOS_ERR, "Unknown device path type: %d\n",
234 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000235 break;
236 }
237 }
238 return buffer;
239}
240
Furquan Shaikh5b5c2332020-04-24 21:31:35 -0700241const char *dev_name(const struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200242{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300243 if (dev->name)
244 return dev->name;
245 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200246 return dev->chip_ops->name;
247 else
248 return "unknown";
249}
250
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000251const char *bus_path(struct bus *bus)
252{
253 static char buffer[BUS_PATH_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200254 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100255 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000256 return buffer;
257}
258
Eric Biederman5cd81732004-03-11 15:01:31 +0000259/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000260 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000261 *
262 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000263 */
264static int allocate_more_resources(void)
265{
266 int i;
267 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000268
Myles Watsonc25cc112010-05-21 14:33:48 +0000269 new_res_list = malloc(64 * sizeof(*new_res_list));
270
271 if (new_res_list == NULL)
272 return 0;
273
274 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
275
Uwe Hermanne4870472010-11-04 23:23:47 +0000276 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000277 new_res_list[i].next = &new_res_list[i+1];
278
279 free_resources = new_res_list;
280 return 1;
281}
282
283/**
284 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000285 *
286 * @param dev TODO
287 * @param res TODO
288 * @param prev TODO
289 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000290 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700291static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000292 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000293{
294 if (prev)
295 prev->next = res->next;
296 else
297 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000298
Myles Watsonc25cc112010-05-21 14:33:48 +0000299 res->next = free_resources;
300 free_resources = res;
301}
302
303/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000304 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000305 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000306 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000307 *
308 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000309 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700310void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000311{
Myles Watsonc25cc112010-05-21 14:33:48 +0000312 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000313
Eric Biederman5cd81732004-03-11 15:01:31 +0000314 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000315 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000316 next = res->next;
317 if (!res->flags)
318 free_resource(dev, res, prev);
319 else
320 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000321 }
322}
323
324/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000325 * See if a resource structure already exists for a given index.
326 *
327 * @param dev The device to find the resource on.
328 * @param index The index of the resource on the device.
329 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000330 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600331struct resource *probe_resource(const struct device *dev, unsigned int index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000332{
Myles Watsonc25cc112010-05-21 14:33:48 +0000333 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000334
Eric Biederman5cd81732004-03-11 15:01:31 +0000335 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000336 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000337 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000338 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000339 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000340
Myles Watsonc25cc112010-05-21 14:33:48 +0000341 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000342}
343
344/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000345 * See if a resource structure already exists for a given index and if not
346 * allocate one.
347 *
Paul Menzel20923872014-06-07 13:31:29 +0200348 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000349 *
350 * @param dev The device to find the resource on.
351 * @param index The index of the resource on the device.
352 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000353 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600354struct resource *new_resource(struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000355{
Myles Watsonc25cc112010-05-21 14:33:48 +0000356 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000357
Uwe Hermanne4870472010-11-04 23:23:47 +0000358 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000359 compact_resources(dev);
360
Uwe Hermanne4870472010-11-04 23:23:47 +0000361 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000362 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000363 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000364 if (free_resources == NULL && !allocate_more_resources())
365 die("Couldn't allocate more resources.");
366
367 resource = free_resources;
368 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000369 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000370 resource->next = NULL;
371 tail = dev->resource_list;
372 if (tail) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100373 while (tail->next)
374 tail = tail->next;
Myles Watsonc25cc112010-05-21 14:33:48 +0000375 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000376 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000377 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000378 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000379 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000380
381 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000382 if (!(resource->flags & IORESOURCE_FIXED)) {
383 resource->flags = 0;
384 resource->base = 0;
385 }
386 resource->size = 0;
387 resource->limit = 0;
388 resource->index = index;
389 resource->align = 0;
390 resource->gran = 0;
391
392 return resource;
393}
394
Eric Biederman03acab62004-10-14 21:25:53 +0000395/**
396 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000397 *
398 * @param dev The device to find the resource on.
399 * @param index The index of the resource on the device.
400 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000401 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600402struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000403{
404 struct resource *resource;
405
Uwe Hermanne4870472010-11-04 23:23:47 +0000406 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000407 resource = probe_resource(dev, index);
408 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000409 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000410 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000411 die("");
412 }
413 return resource;
414}
415
Eric Biederman03acab62004-10-14 21:25:53 +0000416/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000417 * Round a number up to the next multiple of gran.
418 *
419 * @param val The starting value.
420 * @param gran Granularity we are aligning the number to.
421 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000422 */
423static resource_t align_up(resource_t val, unsigned long gran)
424{
425 resource_t mask;
426 mask = (1ULL << gran) - 1ULL;
427 val += mask;
428 val &= ~mask;
429 return val;
430}
431
432/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000433 * Round a number up to the previous multiple of gran.
434 *
435 * @param val The starting value.
436 * @param gran Granularity we are aligning the number to.
437 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000438 */
439static resource_t align_down(resource_t val, unsigned long gran)
440{
441 resource_t mask;
442 mask = (1ULL << gran) - 1ULL;
443 val &= ~mask;
444 return val;
445}
446
447/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000448 * Compute the maximum address that is part of a resource.
449 *
450 * @param resource The resource whose limit is desired.
451 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000452 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300453resource_t resource_end(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000454{
455 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000456
457 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000458 base = resource->base;
459
Uwe Hermanne4870472010-11-04 23:23:47 +0000460 /*
461 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000462 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000463 * the bridge. While the granularity is simply how many low bits of
464 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000465 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000466
Uwe Hermanne4870472010-11-04 23:23:47 +0000467 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000468 end = base + align_up(resource->size, resource->gran) - 1;
469
470 return end;
471}
472
473/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000474 * Compute the maximum legal value for resource->base.
475 *
476 * @param resource The resource whose maximum is desired.
477 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000478 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300479resource_t resource_max(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000480{
481 resource_t max;
482
483 max = align_down(resource->limit - resource->size + 1, resource->align);
484
485 return max;
486}
487
488/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000489 * Return the resource type of a resource.
490 *
491 * @param resource The resource type to decode.
492 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000493 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300494const char *resource_type(const struct resource *resource)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000495{
496 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200497 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100498 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
499 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
500 ((resource->flags == 0) ? "unused" :
501 (resource->flags & IORESOURCE_IO) ? "io" :
502 (resource->flags & IORESOURCE_DRQ) ? "drq" :
503 (resource->flags & IORESOURCE_IRQ) ? "irq" :
504 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
505 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000506 return buffer;
507}
508
509/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000510 * Print the resource that was just stored.
511 *
Martin Roth63373ed2013-07-08 16:24:19 -0600512 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000513 * @param resource The resource that was just stored.
514 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000515 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300516void report_resource_stored(struct device *dev, const struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000517 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000518{
Uwe Hermanne4870472010-11-04 23:23:47 +0000519 char buf[10];
520 unsigned long long base, end;
521
522 if (!(resource->flags & IORESOURCE_STORED))
523 return;
524
525 base = resource->base;
526 end = resource_end(resource);
527 buf[0] = '\0';
528
John Zhao4792f8f2020-09-22 14:52:42 -0700529 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200530 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100531 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000532 }
Gang Chencfb90fd2022-07-02 01:24:09 +0800533 printk(BIOS_DEBUG, "%s %02lx <- [0x%016llx - 0x%016llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000534 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
535 base, end, resource->size, resource->gran, buf,
536 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000537}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000538
Uwe Hermanne4870472010-11-04 23:23:47 +0000539void search_bus_resources(struct bus *bus, unsigned long type_mask,
540 unsigned long type, resource_search_t search,
541 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000542{
543 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000544
Myles Watson894a3472010-06-09 22:41:35 +0000545 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000546 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000547
548 /* Ignore disabled devices. */
549 if (!curdev->enabled)
550 continue;
551
Myles Watson894a3472010-06-09 22:41:35 +0000552 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000553 /* If it isn't the right kind of resource ignore it. */
554 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000555 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000556
557 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000558 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100559 struct bus *subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000560 for (subbus = curdev->link_list; subbus;
561 subbus = subbus->next)
562 if (subbus->link_num
563 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000564 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700565 if (!subbus) /* Why can subbus be NULL? */
566 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000567 search_bus_resources(subbus, type_mask, type,
568 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000569 continue;
570 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000571 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000572 }
573 }
574}
575
Uwe Hermanne4870472010-11-04 23:23:47 +0000576void search_global_resources(unsigned long type_mask, unsigned long type,
577 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000578{
579 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000580
Myles Watson894a3472010-06-09 22:41:35 +0000581 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000582 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000583
584 /* Ignore disabled devices. */
585 if (!curdev->enabled)
586 continue;
587
Myles Watson894a3472010-06-09 22:41:35 +0000588 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000589 /* If it isn't the right kind of resource ignore it. */
590 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000591 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000592
593 /* If it is a subtractive resource ignore it. */
594 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000595 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000596
Shuo Liu0640c282022-08-02 01:50:36 +0800597 /* If the resource is not assigned ignore it. */
598 if (!(res->flags & IORESOURCE_ASSIGNED))
599 continue;
600
Myles Watsonc25cc112010-05-21 14:33:48 +0000601 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000602 }
603 }
604}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000605
Aaron Durbinf0349022017-11-10 10:47:54 -0700606void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000607{
Uwe Hermanne4870472010-11-04 23:23:47 +0000608 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000609 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000610
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000611 dev->enabled = enable;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100612 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000613 dev->ops->enable(dev);
Frans Hendriksa9caa502021-02-01 11:44:37 +0100614 else if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000615 dev->chip_ops->enable_dev(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000616}
617
618void disable_children(struct bus *bus)
619{
Aaron Durbinf0349022017-11-10 10:47:54 -0700620 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000621
Myles Watson894a3472010-06-09 22:41:35 +0000622 for (child = bus->children; child; child = child->sibling) {
623 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000624 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000625 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000626 dev_set_enabled(child, 0);
627 }
628}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000629
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200630/*
631 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200632 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200633 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700634bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200635{
636 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700637 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200638
639 if (!dev || !dev->enabled)
640 return 0;
641
642 if (!dev->link_list || !dev->link_list->children)
643 return 0;
644
645 for (link = dev->link_list; link; link = link->next) {
646 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200647 if (child->path.type == DEVICE_PATH_NONE)
648 continue;
649
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200650 if (child->enabled)
651 return 1;
652 }
653 }
654
655 return 0;
656}
657
Jacob Garber464f4d62019-06-05 17:03:30 -0600658/**
659 * Ensure the device has a minimum number of bus links.
660 *
661 * @param dev The device to add links to.
662 * @param total_links The minimum number of links to have.
663 */
664void add_more_links(struct device *dev, unsigned int total_links)
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600665{
666 struct bus *link, *last = NULL;
667 int link_num = -1;
668
669 for (link = dev->link_list; link; link = link->next) {
670 if (link_num < link->link_num)
671 link_num = link->link_num;
672 last = link;
673 }
674
675 if (last) {
676 int links = total_links - (link_num + 1);
677 if (links > 0) {
Jacob Garber464f4d62019-06-05 17:03:30 -0600678 link = malloc(links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600679 if (!link)
680 die("Couldn't allocate more links!\n");
Jacob Garber464f4d62019-06-05 17:03:30 -0600681 memset(link, 0, links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600682 last->next = link;
Jacob Garber464f4d62019-06-05 17:03:30 -0600683 } else {
684 /* No more links to add */
685 return;
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600686 }
687 } else {
Jacob Garber464f4d62019-06-05 17:03:30 -0600688 link = malloc(total_links * sizeof(*link));
689 if (!link)
690 die("Couldn't allocate more links!\n");
691 memset(link, 0, total_links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600692 dev->link_list = link;
693 }
694
695 for (link_num = link_num + 1; link_num < total_links; link_num++) {
696 link->link_num = link_num;
697 link->dev = dev;
698 link->next = link + 1;
699 last = link;
700 link = link->next;
701 }
702 last->next = NULL;
703}
704
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200705static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000706{
Myles Watson894a3472010-06-09 22:41:35 +0000707 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000708 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000709 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000710 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000711 char indent[30]; /* If your tree has more levels, it's wrong. */
712
713 for (i = 0; i < depth + 1 && i < 29; i++)
714 indent[i] = ' ';
715 indent[i] = '\0';
716
Nico Huber7cc14ac2021-03-27 20:03:02 +0100717 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
Martin Roth7f35d3a2017-07-23 16:22:25 -0600718 if (root->link_list && root->link_list->children)
Nico Huber7cc14ac2021-03-27 20:03:02 +0100719 printk(BIOS_DEBUG, " child on link 0 %s",
Martin Roth7f35d3a2017-07-23 16:22:25 -0600720 dev_path(root->link_list->children));
Nico Huber7cc14ac2021-03-27 20:03:02 +0100721 printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000722
Myles Watsonc25cc112010-05-21 14:33:48 +0000723 for (res = root->resource_list; res; res = res->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100724 printk(debug_level, "%s%s resource base %llx size %llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000725 "align %d gran %d limit %llx flags %lx index %lx\n",
726 indent, dev_path(root), res->base, res->size,
727 res->align, res->gran, res->limit, res->flags,
728 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000729 }
730
Myles Watson894a3472010-06-09 22:41:35 +0000731 for (link = root->link_list; link; link = link->next) {
732 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000733 resource_tree(child, debug_level, depth + 1);
734 }
735}
736
Elyes HAOUASe3480662018-05-06 20:32:23 +0200737void print_resource_tree(const struct device *root, int debug_level,
738 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000739{
740 /* Bail if root is null. */
741 if (!root) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100742 printk(debug_level, "%s passed NULL for root!\n", __func__);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000743 return;
744 }
745
746 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100747 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000748 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000749 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000750
Myles Watsonbb3d8122009-05-11 22:45:35 +0000751 resource_tree(root, debug_level, 0);
752}
753
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200754void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000755{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800756 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000757 int i;
758 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000759 struct bus *link;
760
Myles Watsonbb3d8122009-05-11 22:45:35 +0000761 for (i = 0; i < depth; i++)
762 depth_str[i] = ' ';
763 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000764
Nico Huber7cc14ac2021-03-27 20:03:02 +0100765 printk(debug_level, "%s%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000766 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000767
Myles Watson894a3472010-06-09 22:41:35 +0000768 for (link = dev->link_list; link; link = link->next) {
769 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000770 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300771 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000772 }
773}
774
775void show_all_devs_tree(int debug_level, const char *msg)
776{
777 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100778 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000779 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300780 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000781}
782
783void show_devs_subtree(struct device *root, int debug_level, const char *msg)
784{
785 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100786 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000787 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000788 return;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100789 printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300790 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000791}
792
793void show_all_devs(int debug_level, const char *msg)
794{
795 struct device *dev;
796
797 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100798 if (!printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000799 return;
800 for (dev = all_devices; dev; dev = dev->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100801 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000802 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000803 }
804}
805
806void show_one_resource(int debug_level, struct device *dev,
807 struct resource *resource, const char *comment)
808{
809 char buf[10];
810 unsigned long long base, end;
811 base = resource->base;
812 end = resource_end(resource);
813 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000814
Gang Chencfb90fd2022-07-02 01:24:09 +0800815 printk(debug_level, "%s %02lx <- [0x%016llx - 0x%016llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100816 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000817 resource->index, base, end, resource->size, resource->gran,
818 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000819}
820
Frans Hendriksa9caa502021-02-01 11:44:37 +0100821void show_all_devs_resources(int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000822{
823 struct device *dev;
824
Nico Huber7cc14ac2021-03-27 20:03:02 +0100825 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000826 return;
827
828 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000829 struct resource *res;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100830 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000831 dev_path(dev), dev->enabled);
832 for (res = dev->resource_list; res; res = res->next)
833 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000834 }
835}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000836
Kyösti Mälkkice345962021-06-11 19:31:22 +0300837const struct resource *fixed_resource_range_idx(struct device *dev, unsigned long index,
838 uint64_t base, uint64_t size, unsigned long flags)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000839{
840 struct resource *resource;
Kyösti Mälkkice345962021-06-11 19:31:22 +0300841 if (!size)
842 return NULL;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000843
844 resource = new_resource(dev, index);
Kyösti Mälkkice345962021-06-11 19:31:22 +0300845 resource->base = base;
846 resource->size = size;
847 resource->flags = IORESOURCE_FIXED | IORESOURCE_ASSIGNED;
848 resource->flags |= flags;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000849
Kyösti Mälkkice345962021-06-11 19:31:22 +0300850 printk(BIOS_SPEW, "dev: %s, index: 0x%lx, base: 0x%llx, size: 0x%llx\n",
851 dev_path(dev), resource->index, resource->base, resource->size);
852
853 return resource;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300854}
855
Kyösti Mälkkid0525d42021-06-13 10:09:51 +0300856const struct resource *lower_ram_end(struct device *dev, unsigned long index, uint64_t end)
857{
858 return ram_from_to(dev, index, 0, end);
859}
860
861const struct resource *upper_ram_end(struct device *dev, unsigned long index, uint64_t end)
862{
863 if (end <= 4ull * GiB)
864 return NULL;
865
866 printk(BIOS_INFO, "Available memory above 4GB: %lluM\n", (end - 4ull * GiB) / MiB);
867
868 return ram_from_to(dev, index, 4ull * GiB, end);
869}
870
Angel Pons90be7542021-01-20 13:03:58 +0100871void mmconf_resource(struct device *dev, unsigned long index)
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200872{
Angel Pons90be7542021-01-20 13:03:58 +0100873 struct resource *resource = new_resource(dev, index);
Shelley Chen4e9bb332021-10-20 15:43:45 -0700874 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
875 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200876 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
877 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
878
Angel Ponsd19cc112021-07-04 11:41:31 +0200879 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
880 (unsigned long)(resource->base),
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200881 (unsigned long)(resource->base + resource->size));
882}
883
Uwe Hermann4b42a622010-10-11 19:36:13 +0000884void tolm_test(void *gp, struct device *dev, struct resource *new)
885{
886 struct resource **best_p = gp;
887 struct resource *best;
888
889 best = *best_p;
890
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700891 /*
892 * If resource is not allocated any space i.e. size is zero,
893 * then do not consider this resource in tolm calculations.
894 */
895 if (new->size == 0)
896 return;
897
Uwe Hermann4b42a622010-10-11 19:36:13 +0000898 if (!best || (best->base > new->base))
899 best = new;
900
901 *best_p = best;
902}
903
904u32 find_pci_tolm(struct bus *bus)
905{
906 struct resource *min = NULL;
907 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700908 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000909
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700910 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000911
912 tolm = 0xffffffffUL;
913
914 if (min && tolm > min->base)
915 tolm = min->base;
916
917 return tolm;
918}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700919
920/* Count of enabled CPUs */
921int dev_count_cpu(void)
922{
Aaron Durbinf0349022017-11-10 10:47:54 -0700923 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700924 int count = 0;
925
926 for (cpu = all_devices; cpu; cpu = cpu->next) {
Fabio Aiuto45aae7f2022-09-23 16:51:34 +0200927 if (!is_enabled_cpu(cpu))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700928 continue;
929 count++;
930 }
931
932 return count;
933}
Lee Leahya95baf92016-02-13 06:10:04 -0800934
935/* Get device path name */
936const char *dev_path_name(enum device_path_type type)
937{
938 static const char *const type_names[] = DEVICE_PATH_NAMES;
939 const char *type_name = "Unknown";
940
941 /* Translate the type value into a string */
942 if (type < ARRAY_SIZE(type_names))
943 type_name = type_names[type];
944 return type_name;
945}
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300946
Nico Huber577c6b92022-08-15 00:08:58 +0200947bool dev_path_hotplug(const struct device *dev)
948{
949 for (dev = dev->bus->dev; dev != dev->bus->dev; dev = dev->bus->dev) {
950 if (dev->hotplug_port)
951 return true;
952 }
953 return false;
954}
955
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300956void log_resource(const char *type, const struct device *dev, const struct resource *res,
957 const char *srcfile, const int line)
958{
959 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
960 "end: 0x%llx, size_kb: 0x%llx\n",
961 srcfile, line, type, dev_path(dev), res->index, res->base,
962 resource_end(res), res->size / KiB);
963}
Fabio Aiutoc5573d62022-09-10 14:23:38 +0200964
965bool is_cpu(const struct device *cpu)
966{
967 return cpu->path.type == DEVICE_PATH_APIC &&
968 cpu->bus->dev->path.type == DEVICE_PATH_CPU_CLUSTER;
969}
970
971bool is_enabled_cpu(const struct device *cpu)
972{
973 return is_cpu(cpu) && cpu->enabled;
974}
Fabio Aiuto4fce79f2022-09-30 11:22:09 +0200975
976bool is_pci(const struct device *pci)
977{
978 return pci->path.type == DEVICE_PATH_PCI;
979}
980
981bool is_enabled_pci(const struct device *pci)
982{
983 return is_pci(pci) && pci->enabled;
984}
985
986bool is_pci_dev_on_bus(const struct device *pci, unsigned int bus)
987{
988 return is_pci(pci) && pci->bus->secondary == bus;
989}