blob: c377b859c658557264b24f5261921e503ed64b19 [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
Eric Biederman8ca8d762003-04-22 19:02:15 +00003#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +00004#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +00005#include <device/path.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +02006#include <device/pci_def.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +00007#include <device/resource.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +02008#include <stdlib.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +00009#include <string.h>
10
Eric Biederman03acab62004-10-14 21:25:53 +000011/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070012 * Given a Local APIC ID, find the device structure.
13 *
14 * @param apic_id The Local APIC ID number.
15 * @return Pointer to the device structure (if found), 0 otherwise.
16 */
Martin Roth38ddbfb2019-10-23 21:41:00 -060017struct device *dev_find_lapic(unsigned int apic_id)
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070018{
Aaron Durbinf0349022017-11-10 10:47:54 -070019 struct device *dev;
20 struct device *result = NULL;
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070021
22 for (dev = all_devices; dev; dev = dev->next) {
23 if (dev->path.type == DEVICE_PATH_APIC &&
24 dev->path.apic.apic_id == apic_id) {
25 result = dev;
26 break;
27 }
28 }
29 return result;
30}
31
32/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000033 * Find a device of a given vendor and type.
34 *
35 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
36 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +000037 * @param from Pointer to the device structure, used as a starting point in
38 * the linked list of all_devices, which can be 0 to start at the
39 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000040 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000041 */
Uwe Hermanne4870472010-11-04 23:23:47 +000042struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +000043{
44 if (!from)
45 from = all_devices;
46 else
47 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000048
49 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +000050 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000051
Eric Biederman8ca8d762003-04-22 19:02:15 +000052 return from;
53}
54
Uwe Hermannc1ee4292010-10-17 19:01:48 +000055/**
56 * Find a device of a given class.
57 *
58 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +000059 * @param from Pointer to the device structure, used as a starting point in
60 * the linked list of all_devices, which can be 0 to start at the
61 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000062 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000063 */
64struct device *dev_find_class(unsigned int class, struct device *from)
65{
66 if (!from)
67 from = all_devices;
68 else
69 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000070
Greg Watson59651692003-12-17 17:39:53 +000071 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +000072 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000073
Eric Biederman8ca8d762003-04-22 19:02:15 +000074 return from;
75}
76
Duncan Laurie5f5d9142013-06-10 09:59:17 -070077/**
78 * Encode the device path into 3 bytes for logging to CMOS.
79 *
80 * @param dev The device path to encode.
81 * @return Device path encoded into lower 3 bytes of dword.
82 */
Subrata Banik564547f2018-05-02 10:27:36 +053083u32 dev_path_encode(const struct device *dev)
Duncan Laurie5f5d9142013-06-10 09:59:17 -070084{
85 u32 ret;
86
87 if (!dev)
88 return 0;
89
90 /* Store the device type in 3rd byte. */
91 ret = dev->path.type << 16;
92
93 /* Encode the device specific path in the low word. */
94 switch (dev->path.type) {
95 case DEVICE_PATH_ROOT:
96 break;
97 case DEVICE_PATH_PCI:
98 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
99 break;
100 case DEVICE_PATH_PNP:
101 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
102 break;
103 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700104 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700105 break;
106 case DEVICE_PATH_APIC:
107 ret |= dev->path.apic.apic_id;
108 break;
109 case DEVICE_PATH_DOMAIN:
110 ret |= dev->path.domain.domain;
111 break;
112 case DEVICE_PATH_CPU_CLUSTER:
113 ret |= dev->path.cpu_cluster.cluster;
114 break;
115 case DEVICE_PATH_CPU:
116 ret |= dev->path.cpu.id;
117 break;
118 case DEVICE_PATH_CPU_BUS:
119 ret |= dev->path.cpu_bus.id;
120 break;
121 case DEVICE_PATH_IOAPIC:
122 ret |= dev->path.ioapic.ioapic_id;
123 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700124 case DEVICE_PATH_GENERIC:
125 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
126 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800127 case DEVICE_PATH_SPI:
128 ret |= dev->path.spi.cs;
129 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700130 case DEVICE_PATH_USB:
Karthikeyan Ramasubramanian19398242019-07-11 12:23:35 -0600131 ret |= dev->path.usb.port_type << 8 | dev->path.usb.port_id;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700132 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100133 case DEVICE_PATH_GPIO:
134 ret |= dev->path.gpio.id;
135 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700136 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800137 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700138 default:
139 break;
140 }
141
142 return ret;
143}
144
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000145/*
146 * Warning: This function uses a static buffer. Don't call it more than once
147 * from the same print statement!
148 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200149const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000150{
151 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000152
Eric Biedermane9a271e32003-09-02 03:36:25 +0000153 buffer[0] = '\0';
154 if (!dev) {
Angel Ponsd7df3832021-08-18 08:32:45 +0200155 strcpy(buffer, "<null>");
Uwe Hermanne4870472010-11-04 23:23:47 +0000156 } else {
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100157 switch (dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100158 case DEVICE_PATH_NONE:
Angel Ponsd7df3832021-08-18 08:32:45 +0200159 strcpy(buffer, "NONE");
Patrick Rudolph6a811842017-11-01 11:33:00 +0100160 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000161 case DEVICE_PATH_ROOT:
Angel Ponsd7df3832021-08-18 08:32:45 +0200162 strcpy(buffer, "Root Device");
Eric Biederman83b991a2003-10-11 06:20:25 +0000163 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000164 case DEVICE_PATH_PCI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200165 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100166 "PCI: %02x:%02x.%01x",
167 dev->bus->secondary,
168 PCI_SLOT(dev->path.pci.devfn),
169 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000170 break;
171 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200172 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100173 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000174 break;
175 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200176 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100177 dev->bus->secondary,
178 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000179 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000180 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200181 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100182 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000183 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200184 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200185 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100186 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200187 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800188 case DEVICE_PATH_DOMAIN:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200189 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800190 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000191 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800192 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200193 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800194 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000195 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000196 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200197 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100198 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000199 break;
200 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200201 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100202 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000203 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700204 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200205 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700206 "GENERIC: %d.%d", dev->path.generic.id,
207 dev->path.generic.subid);
208 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800209 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200210 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800211 dev->path.spi.cs);
212 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700213 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200214 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700215 dev->path.usb.port_type, dev->path.usb.port_id);
216 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800217 case DEVICE_PATH_MMIO:
Jacob Garberd552aca2019-07-16 12:55:00 -0600218 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800219 dev->path.mmio.addr);
220 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100221 case DEVICE_PATH_GPIO:
222 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
223 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000224 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000225 printk(BIOS_ERR, "Unknown device path type: %d\n",
226 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000227 break;
228 }
229 }
230 return buffer;
231}
232
Furquan Shaikh5b5c2332020-04-24 21:31:35 -0700233const char *dev_name(const struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200234{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300235 if (dev->name)
236 return dev->name;
237 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200238 return dev->chip_ops->name;
239 else
240 return "unknown";
241}
242
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000243const char *bus_path(struct bus *bus)
244{
245 static char buffer[BUS_PATH_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200246 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100247 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000248 return buffer;
249}
250
Eric Biederman5cd81732004-03-11 15:01:31 +0000251/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000252 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000253 *
254 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000255 */
256static int allocate_more_resources(void)
257{
258 int i;
259 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000260
Myles Watsonc25cc112010-05-21 14:33:48 +0000261 new_res_list = malloc(64 * sizeof(*new_res_list));
262
263 if (new_res_list == NULL)
264 return 0;
265
266 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
267
Uwe Hermanne4870472010-11-04 23:23:47 +0000268 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000269 new_res_list[i].next = &new_res_list[i+1];
270
271 free_resources = new_res_list;
272 return 1;
273}
274
275/**
276 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000277 *
278 * @param dev TODO
279 * @param res TODO
280 * @param prev TODO
281 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000282 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700283static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000284 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000285{
286 if (prev)
287 prev->next = res->next;
288 else
289 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000290
Myles Watsonc25cc112010-05-21 14:33:48 +0000291 res->next = free_resources;
292 free_resources = res;
293}
294
295/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000296 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000297 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000298 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000299 *
300 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000301 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700302void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000303{
Myles Watsonc25cc112010-05-21 14:33:48 +0000304 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000305
Eric Biederman5cd81732004-03-11 15:01:31 +0000306 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000307 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000308 next = res->next;
309 if (!res->flags)
310 free_resource(dev, res, prev);
311 else
312 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000313 }
314}
315
316/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000317 * See if a resource structure already exists for a given index.
318 *
319 * @param dev The device to find the resource on.
320 * @param index The index of the resource on the device.
321 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000322 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600323struct resource *probe_resource(const struct device *dev, unsigned int index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000324{
Myles Watsonc25cc112010-05-21 14:33:48 +0000325 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000326
Eric Biederman5cd81732004-03-11 15:01:31 +0000327 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000328 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000329 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000330 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000331 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000332
Myles Watsonc25cc112010-05-21 14:33:48 +0000333 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000334}
335
336/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000337 * See if a resource structure already exists for a given index and if not
338 * allocate one.
339 *
Paul Menzel20923872014-06-07 13:31:29 +0200340 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000341 *
342 * @param dev The device to find the resource on.
343 * @param index The index of the resource on the device.
344 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000345 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600346struct resource *new_resource(struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000347{
Myles Watsonc25cc112010-05-21 14:33:48 +0000348 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000349
Uwe Hermanne4870472010-11-04 23:23:47 +0000350 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000351 compact_resources(dev);
352
Uwe Hermanne4870472010-11-04 23:23:47 +0000353 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000354 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000355 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000356 if (free_resources == NULL && !allocate_more_resources())
357 die("Couldn't allocate more resources.");
358
359 resource = free_resources;
360 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000361 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000362 resource->next = NULL;
363 tail = dev->resource_list;
364 if (tail) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100365 while (tail->next)
366 tail = tail->next;
Myles Watsonc25cc112010-05-21 14:33:48 +0000367 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000368 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000369 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000370 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000371 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000372
373 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000374 if (!(resource->flags & IORESOURCE_FIXED)) {
375 resource->flags = 0;
376 resource->base = 0;
377 }
378 resource->size = 0;
379 resource->limit = 0;
380 resource->index = index;
381 resource->align = 0;
382 resource->gran = 0;
383
384 return resource;
385}
386
Eric Biederman03acab62004-10-14 21:25:53 +0000387/**
388 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000389 *
390 * @param dev The device to find the resource on.
391 * @param index The index of the resource on the device.
392 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000393 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600394struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000395{
396 struct resource *resource;
397
Uwe Hermanne4870472010-11-04 23:23:47 +0000398 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000399 resource = probe_resource(dev, index);
400 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000401 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000402 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000403 die("");
404 }
405 return resource;
406}
407
Eric Biederman03acab62004-10-14 21:25:53 +0000408/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000409 * Round a number up to the next multiple of gran.
410 *
411 * @param val The starting value.
412 * @param gran Granularity we are aligning the number to.
413 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000414 */
415static resource_t align_up(resource_t val, unsigned long gran)
416{
417 resource_t mask;
418 mask = (1ULL << gran) - 1ULL;
419 val += mask;
420 val &= ~mask;
421 return val;
422}
423
424/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000425 * Round a number up to the previous multiple of gran.
426 *
427 * @param val The starting value.
428 * @param gran Granularity we are aligning the number to.
429 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000430 */
431static resource_t align_down(resource_t val, unsigned long gran)
432{
433 resource_t mask;
434 mask = (1ULL << gran) - 1ULL;
435 val &= ~mask;
436 return val;
437}
438
439/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000440 * Compute the maximum address that is part of a resource.
441 *
442 * @param resource The resource whose limit is desired.
443 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000444 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300445resource_t resource_end(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000446{
447 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000448
449 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000450 base = resource->base;
451
Uwe Hermanne4870472010-11-04 23:23:47 +0000452 /*
453 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000454 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000455 * the bridge. While the granularity is simply how many low bits of
456 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000457 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000458
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000460 end = base + align_up(resource->size, resource->gran) - 1;
461
462 return end;
463}
464
465/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000466 * Compute the maximum legal value for resource->base.
467 *
468 * @param resource The resource whose maximum is desired.
469 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000470 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300471resource_t resource_max(const struct resource *resource)
Eric Biederman03acab62004-10-14 21:25:53 +0000472{
473 resource_t max;
474
475 max = align_down(resource->limit - resource->size + 1, resource->align);
476
477 return max;
478}
479
480/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000481 * Return the resource type of a resource.
482 *
483 * @param resource The resource type to decode.
484 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000485 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300486const char *resource_type(const struct resource *resource)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000487{
488 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200489 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100490 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
491 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
492 ((resource->flags == 0) ? "unused" :
493 (resource->flags & IORESOURCE_IO) ? "io" :
494 (resource->flags & IORESOURCE_DRQ) ? "drq" :
495 (resource->flags & IORESOURCE_IRQ) ? "irq" :
496 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
497 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000498 return buffer;
499}
500
501/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000502 * Print the resource that was just stored.
503 *
Martin Roth63373ed2013-07-08 16:24:19 -0600504 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000505 * @param resource The resource that was just stored.
506 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000507 */
Kyösti Mälkkib2287712021-06-14 11:29:51 +0300508void report_resource_stored(struct device *dev, const struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000509 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000510{
Uwe Hermanne4870472010-11-04 23:23:47 +0000511 char buf[10];
512 unsigned long long base, end;
513
514 if (!(resource->flags & IORESOURCE_STORED))
515 return;
516
517 base = resource->base;
518 end = resource_end(resource);
519 buf[0] = '\0';
520
John Zhao4792f8f2020-09-22 14:52:42 -0700521 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200522 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100523 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000524 }
Patrick Georgi51615092012-03-11 19:31:03 +0100525 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000526 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
527 base, end, resource->size, resource->gran, buf,
528 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000529}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000530
Uwe Hermanne4870472010-11-04 23:23:47 +0000531void search_bus_resources(struct bus *bus, unsigned long type_mask,
532 unsigned long type, resource_search_t search,
533 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000534{
535 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000536
Myles Watson894a3472010-06-09 22:41:35 +0000537 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000538 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000539
540 /* Ignore disabled devices. */
541 if (!curdev->enabled)
542 continue;
543
Myles Watson894a3472010-06-09 22:41:35 +0000544 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000545 /* If it isn't the right kind of resource ignore it. */
546 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000547 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000548
549 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000550 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Frans Hendriksa9caa502021-02-01 11:44:37 +0100551 struct bus *subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000552 for (subbus = curdev->link_list; subbus;
553 subbus = subbus->next)
554 if (subbus->link_num
555 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000556 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700557 if (!subbus) /* Why can subbus be NULL? */
558 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000559 search_bus_resources(subbus, type_mask, type,
560 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000561 continue;
562 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000563 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000564 }
565 }
566}
567
Uwe Hermanne4870472010-11-04 23:23:47 +0000568void search_global_resources(unsigned long type_mask, unsigned long type,
569 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000570{
571 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000572
Myles Watson894a3472010-06-09 22:41:35 +0000573 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000574 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000575
576 /* Ignore disabled devices. */
577 if (!curdev->enabled)
578 continue;
579
Myles Watson894a3472010-06-09 22:41:35 +0000580 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000581 /* If it isn't the right kind of resource ignore it. */
582 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000583 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000584
585 /* If it is a subtractive resource ignore it. */
586 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000587 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000588
Myles Watsonc25cc112010-05-21 14:33:48 +0000589 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000590 }
591 }
592}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000593
Aaron Durbinf0349022017-11-10 10:47:54 -0700594void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000595{
Uwe Hermanne4870472010-11-04 23:23:47 +0000596 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000597 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000598
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000599 dev->enabled = enable;
Frans Hendriksa9caa502021-02-01 11:44:37 +0100600 if (dev->ops && dev->ops->enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000601 dev->ops->enable(dev);
Frans Hendriksa9caa502021-02-01 11:44:37 +0100602 else if (dev->chip_ops && dev->chip_ops->enable_dev)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000603 dev->chip_ops->enable_dev(dev);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000604}
605
606void disable_children(struct bus *bus)
607{
Aaron Durbinf0349022017-11-10 10:47:54 -0700608 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000609
Myles Watson894a3472010-06-09 22:41:35 +0000610 for (child = bus->children; child; child = child->sibling) {
611 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000612 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000613 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000614 dev_set_enabled(child, 0);
615 }
616}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000617
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200618/*
619 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200620 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200621 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700622bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200623{
624 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700625 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200626
627 if (!dev || !dev->enabled)
628 return 0;
629
630 if (!dev->link_list || !dev->link_list->children)
631 return 0;
632
633 for (link = dev->link_list; link; link = link->next) {
634 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200635 if (child->path.type == DEVICE_PATH_NONE)
636 continue;
637
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200638 if (child->enabled)
639 return 1;
640 }
641 }
642
643 return 0;
644}
645
Jacob Garber464f4d62019-06-05 17:03:30 -0600646/**
647 * Ensure the device has a minimum number of bus links.
648 *
649 * @param dev The device to add links to.
650 * @param total_links The minimum number of links to have.
651 */
652void add_more_links(struct device *dev, unsigned int total_links)
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600653{
654 struct bus *link, *last = NULL;
655 int link_num = -1;
656
657 for (link = dev->link_list; link; link = link->next) {
658 if (link_num < link->link_num)
659 link_num = link->link_num;
660 last = link;
661 }
662
663 if (last) {
664 int links = total_links - (link_num + 1);
665 if (links > 0) {
Jacob Garber464f4d62019-06-05 17:03:30 -0600666 link = malloc(links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600667 if (!link)
668 die("Couldn't allocate more links!\n");
Jacob Garber464f4d62019-06-05 17:03:30 -0600669 memset(link, 0, links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600670 last->next = link;
Jacob Garber464f4d62019-06-05 17:03:30 -0600671 } else {
672 /* No more links to add */
673 return;
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600674 }
675 } else {
Jacob Garber464f4d62019-06-05 17:03:30 -0600676 link = malloc(total_links * sizeof(*link));
677 if (!link)
678 die("Couldn't allocate more links!\n");
679 memset(link, 0, total_links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600680 dev->link_list = link;
681 }
682
683 for (link_num = link_num + 1; link_num < total_links; link_num++) {
684 link->link_num = link_num;
685 link->dev = dev;
686 link->next = link + 1;
687 last = link;
688 link = link->next;
689 }
690 last->next = NULL;
691}
692
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200693static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000694{
Myles Watson894a3472010-06-09 22:41:35 +0000695 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000696 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000697 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000698 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000699 char indent[30]; /* If your tree has more levels, it's wrong. */
700
701 for (i = 0; i < depth + 1 && i < 29; i++)
702 indent[i] = ' ';
703 indent[i] = '\0';
704
Nico Huber7cc14ac2021-03-27 20:03:02 +0100705 printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
Martin Roth7f35d3a2017-07-23 16:22:25 -0600706 if (root->link_list && root->link_list->children)
Nico Huber7cc14ac2021-03-27 20:03:02 +0100707 printk(BIOS_DEBUG, " child on link 0 %s",
Martin Roth7f35d3a2017-07-23 16:22:25 -0600708 dev_path(root->link_list->children));
Nico Huber7cc14ac2021-03-27 20:03:02 +0100709 printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000710
Myles Watsonc25cc112010-05-21 14:33:48 +0000711 for (res = root->resource_list; res; res = res->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100712 printk(debug_level, "%s%s resource base %llx size %llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000713 "align %d gran %d limit %llx flags %lx index %lx\n",
714 indent, dev_path(root), res->base, res->size,
715 res->align, res->gran, res->limit, res->flags,
716 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000717 }
718
Myles Watson894a3472010-06-09 22:41:35 +0000719 for (link = root->link_list; link; link = link->next) {
720 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000721 resource_tree(child, debug_level, depth + 1);
722 }
723}
724
Elyes HAOUASe3480662018-05-06 20:32:23 +0200725void print_resource_tree(const struct device *root, int debug_level,
726 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000727{
728 /* Bail if root is null. */
729 if (!root) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100730 printk(debug_level, "%s passed NULL for root!\n", __func__);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000731 return;
732 }
733
734 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100735 if (!printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000736 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000737 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000738
Myles Watsonbb3d8122009-05-11 22:45:35 +0000739 resource_tree(root, debug_level, 0);
740}
741
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200742void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000743{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800744 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000745 int i;
746 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000747 struct bus *link;
748
Myles Watsonbb3d8122009-05-11 22:45:35 +0000749 for (i = 0; i < depth; i++)
750 depth_str[i] = ' ';
751 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000752
Nico Huber7cc14ac2021-03-27 20:03:02 +0100753 printk(debug_level, "%s%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000754 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000755
Myles Watson894a3472010-06-09 22:41:35 +0000756 for (link = dev->link_list; link; link = link->next) {
757 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000758 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300759 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000760 }
761}
762
763void show_all_devs_tree(int debug_level, const char *msg)
764{
765 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100766 if (!printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000767 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300768 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000769}
770
771void show_devs_subtree(struct device *root, int debug_level, const char *msg)
772{
773 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100774 if (!printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000775 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000776 return;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100777 printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300778 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000779}
780
781void show_all_devs(int debug_level, const char *msg)
782{
783 struct device *dev;
784
785 /* Bail if not printing to screen. */
Nico Huber7cc14ac2021-03-27 20:03:02 +0100786 if (!printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000787 return;
788 for (dev = all_devices; dev; dev = dev->next) {
Nico Huber7cc14ac2021-03-27 20:03:02 +0100789 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000790 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000791 }
792}
793
794void show_one_resource(int debug_level, struct device *dev,
795 struct resource *resource, const char *comment)
796{
797 char buf[10];
798 unsigned long long base, end;
799 base = resource->base;
800 end = resource_end(resource);
801 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000802
Nico Huber7cc14ac2021-03-27 20:03:02 +0100803 printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100804 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000805 resource->index, base, end, resource->size, resource->gran,
806 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000807}
808
Frans Hendriksa9caa502021-02-01 11:44:37 +0100809void show_all_devs_resources(int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000810{
811 struct device *dev;
812
Nico Huber7cc14ac2021-03-27 20:03:02 +0100813 if (!printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000814 return;
815
816 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000817 struct resource *res;
Nico Huber7cc14ac2021-03-27 20:03:02 +0100818 printk(debug_level, "%s: enabled %d\n",
Myles Watsonc25cc112010-05-21 14:33:48 +0000819 dev_path(dev), dev->enabled);
820 for (res = dev->resource_list; res; res = res->next)
821 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000822 }
823}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000824
Kyösti Mälkki27d62992022-05-24 20:25:58 +0300825void fixed_mem_resource_kb(struct device *dev, unsigned long index,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200826 unsigned long basek, unsigned long sizek,
827 unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000828{
829 struct resource *resource;
830
831 if (!sizek)
832 return;
833
834 resource = new_resource(dev, index);
835 resource->base = ((resource_t)basek) << 10;
836 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300837 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
838 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000839
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300840 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300841}
842
Subrata Banik217ca362019-03-15 17:18:44 +0530843void fixed_io_resource(struct device *dev, unsigned long index,
844 unsigned long base, unsigned long size)
845{
846 struct resource *resource;
847
848 resource = new_resource(dev, index);
849 resource->base = (resource_t)base;
850 resource->size = (resource_t)size;
851 resource->limit = resource->base + resource->size - 1;
852 resource->flags = IORESOURCE_IO | IORESOURCE_FIXED |
853 IORESOURCE_STORED | IORESOURCE_ASSIGNED |
854 IORESOURCE_RESERVE;
855}
856
Angel Pons90be7542021-01-20 13:03:58 +0100857void mmconf_resource(struct device *dev, unsigned long index)
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200858{
Angel Pons90be7542021-01-20 13:03:58 +0100859 struct resource *resource = new_resource(dev, index);
Shelley Chen4e9bb332021-10-20 15:43:45 -0700860 resource->base = CONFIG_ECAM_MMCONF_BASE_ADDRESS;
861 resource->size = CONFIG_ECAM_MMCONF_LENGTH;
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200862 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
863 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
864
Angel Ponsd19cc112021-07-04 11:41:31 +0200865 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR 0x%08lx-0x%08lx.\n",
866 (unsigned long)(resource->base),
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200867 (unsigned long)(resource->base + resource->size));
868}
869
Uwe Hermann4b42a622010-10-11 19:36:13 +0000870void tolm_test(void *gp, struct device *dev, struct resource *new)
871{
872 struct resource **best_p = gp;
873 struct resource *best;
874
875 best = *best_p;
876
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700877 /*
878 * If resource is not allocated any space i.e. size is zero,
879 * then do not consider this resource in tolm calculations.
880 */
881 if (new->size == 0)
882 return;
883
Uwe Hermann4b42a622010-10-11 19:36:13 +0000884 if (!best || (best->base > new->base))
885 best = new;
886
887 *best_p = best;
888}
889
890u32 find_pci_tolm(struct bus *bus)
891{
892 struct resource *min = NULL;
893 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700894 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000895
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700896 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000897
898 tolm = 0xffffffffUL;
899
900 if (min && tolm > min->base)
901 tolm = min->base;
902
903 return tolm;
904}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700905
906/* Count of enabled CPUs */
907int dev_count_cpu(void)
908{
Aaron Durbinf0349022017-11-10 10:47:54 -0700909 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700910 int count = 0;
911
912 for (cpu = all_devices; cpu; cpu = cpu->next) {
913 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800914 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700915 continue;
916 if (!cpu->enabled)
917 continue;
918 count++;
919 }
920
921 return count;
922}
Lee Leahya95baf92016-02-13 06:10:04 -0800923
924/* Get device path name */
925const char *dev_path_name(enum device_path_type type)
926{
927 static const char *const type_names[] = DEVICE_PATH_NAMES;
928 const char *type_name = "Unknown";
929
930 /* Translate the type value into a string */
931 if (type < ARRAY_SIZE(type_names))
932 type_name = type_names[type];
933 return type_name;
934}
Kyösti Mälkki68e6dc92021-06-14 00:40:22 +0300935
936void log_resource(const char *type, const struct device *dev, const struct resource *res,
937 const char *srcfile, const int line)
938{
939 printk(BIOS_SPEW, "%s:%d res: %s, dev: %s, index: 0x%lx, base: 0x%llx, "
940 "end: 0x%llx, size_kb: 0x%llx\n",
941 srcfile, line, type, dev_path(dev), res->index, res->base,
942 resource_end(res), res->size / KiB);
943}