blob: 0bce26a99e5db45c20942589c449c9ccd149f7f4 [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) {
155 memcpy(buffer, "<null>", 7);
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:
159 memcpy(buffer, "NONE", 5);
160 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000161 case DEVICE_PATH_ROOT:
162 memcpy(buffer, "Root Device", 12);
163 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;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600221 case DEVICE_PATH_ESPI:
222 snprintf(buffer, sizeof(buffer), "ESPI: %08lx",
223 dev->path.espi.addr);
224 break;
225 case DEVICE_PATH_LPC:
226 snprintf(buffer, sizeof(buffer), "LPC: %08lx",
227 dev->path.lpc.addr);
228 break;
Michael Niewöhnerdbb667a2020-12-11 21:26:02 +0100229 case DEVICE_PATH_GPIO:
230 snprintf(buffer, sizeof(buffer), "GPIO: %d", dev->path.gpio.id);
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) {
373 while (tail->next) tail = tail->next;
374 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000375 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000376 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000377 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000378 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000379
380 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000381 if (!(resource->flags & IORESOURCE_FIXED)) {
382 resource->flags = 0;
383 resource->base = 0;
384 }
385 resource->size = 0;
386 resource->limit = 0;
387 resource->index = index;
388 resource->align = 0;
389 resource->gran = 0;
390
391 return resource;
392}
393
Eric Biederman03acab62004-10-14 21:25:53 +0000394/**
395 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000396 *
397 * @param dev The device to find the resource on.
398 * @param index The index of the resource on the device.
399 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000400 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600401struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000402{
403 struct resource *resource;
404
Uwe Hermanne4870472010-11-04 23:23:47 +0000405 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000406 resource = probe_resource(dev, index);
407 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000408 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000409 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000410 die("");
411 }
412 return resource;
413}
414
Eric Biederman03acab62004-10-14 21:25:53 +0000415/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000416 * Round a number up to the next multiple of gran.
417 *
418 * @param val The starting value.
419 * @param gran Granularity we are aligning the number to.
420 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000421 */
422static resource_t align_up(resource_t val, unsigned long gran)
423{
424 resource_t mask;
425 mask = (1ULL << gran) - 1ULL;
426 val += mask;
427 val &= ~mask;
428 return val;
429}
430
431/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000432 * Round a number up to the previous multiple of gran.
433 *
434 * @param val The starting value.
435 * @param gran Granularity we are aligning the number to.
436 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000437 */
438static resource_t align_down(resource_t val, unsigned long gran)
439{
440 resource_t mask;
441 mask = (1ULL << gran) - 1ULL;
442 val &= ~mask;
443 return val;
444}
445
446/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000447 * Compute the maximum address that is part of a resource.
448 *
449 * @param resource The resource whose limit is desired.
450 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000451 */
452resource_t resource_end(struct resource *resource)
453{
454 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000455
456 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000457 base = resource->base;
458
Uwe Hermanne4870472010-11-04 23:23:47 +0000459 /*
460 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000461 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000462 * the bridge. While the granularity is simply how many low bits of
463 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000464 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000465
Uwe Hermanne4870472010-11-04 23:23:47 +0000466 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000467 end = base + align_up(resource->size, resource->gran) - 1;
468
469 return end;
470}
471
472/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000473 * Compute the maximum legal value for resource->base.
474 *
475 * @param resource The resource whose maximum is desired.
476 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000477 */
478resource_t resource_max(struct resource *resource)
479{
480 resource_t max;
481
482 max = align_down(resource->limit - resource->size + 1, resource->align);
483
484 return max;
485}
486
487/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000488 * Return the resource type of a resource.
489 *
490 * @param resource The resource type to decode.
491 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000492 */
493const char *resource_type(struct resource *resource)
494{
495 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200496 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100497 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
498 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
499 ((resource->flags == 0) ? "unused" :
500 (resource->flags & IORESOURCE_IO) ? "io" :
501 (resource->flags & IORESOURCE_DRQ) ? "drq" :
502 (resource->flags & IORESOURCE_IRQ) ? "irq" :
503 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
504 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000505 return buffer;
506}
507
508/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000509 * Print the resource that was just stored.
510 *
Martin Roth63373ed2013-07-08 16:24:19 -0600511 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000512 * @param resource The resource that was just stored.
513 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000514 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700515void report_resource_stored(struct device *dev, struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000516 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000517{
Uwe Hermanne4870472010-11-04 23:23:47 +0000518 char buf[10];
519 unsigned long long base, end;
520
521 if (!(resource->flags & IORESOURCE_STORED))
522 return;
523
524 base = resource->base;
525 end = resource_end(resource);
526 buf[0] = '\0';
527
John Zhao4792f8f2020-09-22 14:52:42 -0700528 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200529 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100530 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000531 }
Patrick Georgi51615092012-03-11 19:31:03 +0100532 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000533 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
534 base, end, resource->size, resource->gran, buf,
535 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000536}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000537
Uwe Hermanne4870472010-11-04 23:23:47 +0000538void search_bus_resources(struct bus *bus, unsigned long type_mask,
539 unsigned long type, resource_search_t search,
540 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000541{
542 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000543
Myles Watson894a3472010-06-09 22:41:35 +0000544 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000545 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000546
547 /* Ignore disabled devices. */
548 if (!curdev->enabled)
549 continue;
550
Myles Watson894a3472010-06-09 22:41:35 +0000551 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000552 /* If it isn't the right kind of resource ignore it. */
553 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000554 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000555
556 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000557 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000558 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000559 for (subbus = curdev->link_list; subbus;
560 subbus = subbus->next)
561 if (subbus->link_num
562 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000563 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700564 if (!subbus) /* Why can subbus be NULL? */
565 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000566 search_bus_resources(subbus, type_mask, type,
567 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000568 continue;
569 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000570 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000571 }
572 }
573}
574
Uwe Hermanne4870472010-11-04 23:23:47 +0000575void search_global_resources(unsigned long type_mask, unsigned long type,
576 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000577{
578 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000579
Myles Watson894a3472010-06-09 22:41:35 +0000580 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000581 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000582
583 /* Ignore disabled devices. */
584 if (!curdev->enabled)
585 continue;
586
Myles Watson894a3472010-06-09 22:41:35 +0000587 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000588 /* If it isn't the right kind of resource ignore it. */
589 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000590 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000591
592 /* If it is a subtractive resource ignore it. */
593 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000594 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000595
Myles Watsonc25cc112010-05-21 14:33:48 +0000596 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000597 }
598 }
599}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000600
Aaron Durbinf0349022017-11-10 10:47:54 -0700601void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000602{
Uwe Hermanne4870472010-11-04 23:23:47 +0000603 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000604 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000605
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000606 dev->enabled = enable;
607 if (dev->ops && dev->ops->enable) {
608 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000609 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000610 dev->chip_ops->enable_dev(dev);
611 }
612}
613
614void disable_children(struct bus *bus)
615{
Aaron Durbinf0349022017-11-10 10:47:54 -0700616 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000617
Myles Watson894a3472010-06-09 22:41:35 +0000618 for (child = bus->children; child; child = child->sibling) {
619 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000620 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000621 disable_children(link);
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{
632 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700633 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200634
635 if (!dev || !dev->enabled)
636 return 0;
637
638 if (!dev->link_list || !dev->link_list->children)
639 return 0;
640
641 for (link = dev->link_list; link; link = link->next) {
642 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200643 if (child->path.type == DEVICE_PATH_NONE)
644 continue;
645
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200646 if (child->enabled)
647 return 1;
648 }
649 }
650
651 return 0;
652}
653
Jacob Garber464f4d62019-06-05 17:03:30 -0600654/**
655 * Ensure the device has a minimum number of bus links.
656 *
657 * @param dev The device to add links to.
658 * @param total_links The minimum number of links to have.
659 */
660void add_more_links(struct device *dev, unsigned int total_links)
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600661{
662 struct bus *link, *last = NULL;
663 int link_num = -1;
664
665 for (link = dev->link_list; link; link = link->next) {
666 if (link_num < link->link_num)
667 link_num = link->link_num;
668 last = link;
669 }
670
671 if (last) {
672 int links = total_links - (link_num + 1);
673 if (links > 0) {
Jacob Garber464f4d62019-06-05 17:03:30 -0600674 link = malloc(links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600675 if (!link)
676 die("Couldn't allocate more links!\n");
Jacob Garber464f4d62019-06-05 17:03:30 -0600677 memset(link, 0, links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600678 last->next = link;
Jacob Garber464f4d62019-06-05 17:03:30 -0600679 } else {
680 /* No more links to add */
681 return;
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600682 }
683 } else {
Jacob Garber464f4d62019-06-05 17:03:30 -0600684 link = malloc(total_links * sizeof(*link));
685 if (!link)
686 die("Couldn't allocate more links!\n");
687 memset(link, 0, total_links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600688 dev->link_list = link;
689 }
690
691 for (link_num = link_num + 1; link_num < total_links; link_num++) {
692 link->link_num = link_num;
693 link->dev = dev;
694 link->next = link + 1;
695 last = link;
696 link = link->next;
697 }
698 last->next = NULL;
699}
700
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200701static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000702{
Myles Watson894a3472010-06-09 22:41:35 +0000703 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000704 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000705 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000706 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000707 char indent[30]; /* If your tree has more levels, it's wrong. */
708
709 for (i = 0; i < depth + 1 && i < 29; i++)
710 indent[i] = ' ';
711 indent[i] = '\0';
712
Martin Roth7f35d3a2017-07-23 16:22:25 -0600713 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
714 if (root->link_list && root->link_list->children)
715 do_printk(BIOS_DEBUG, " child on link 0 %s",
716 dev_path(root->link_list->children));
717 do_printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000718
Myles Watsonc25cc112010-05-21 14:33:48 +0000719 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000720 do_printk(debug_level, "%s%s resource base %llx size %llx "
721 "align %d gran %d limit %llx flags %lx index %lx\n",
722 indent, dev_path(root), res->base, res->size,
723 res->align, res->gran, res->limit, res->flags,
724 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000725 }
726
Myles Watson894a3472010-06-09 22:41:35 +0000727 for (link = root->link_list; link; link = link->next) {
728 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000729 resource_tree(child, debug_level, depth + 1);
730 }
731}
732
Elyes HAOUASe3480662018-05-06 20:32:23 +0200733void print_resource_tree(const struct device *root, int debug_level,
734 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000735{
736 /* Bail if root is null. */
737 if (!root) {
738 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
739 return;
740 }
741
742 /* Bail if not printing to screen. */
743 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000744 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000745 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000746
Myles Watsonbb3d8122009-05-11 22:45:35 +0000747 resource_tree(root, debug_level, 0);
748}
749
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200750void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000751{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800752 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000753 int i;
754 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000755 struct bus *link;
756
Myles Watsonbb3d8122009-05-11 22:45:35 +0000757 for (i = 0; i < depth; i++)
758 depth_str[i] = ' ';
759 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000760
Myles Watsonc25cc112010-05-21 14:33:48 +0000761 do_printk(debug_level, "%s%s: enabled %d\n",
762 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000763
Myles Watson894a3472010-06-09 22:41:35 +0000764 for (link = dev->link_list; link; link = link->next) {
765 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000766 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300767 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000768 }
769}
770
771void show_all_devs_tree(int debug_level, const char *msg)
772{
773 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100774 if (!do_printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000775 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300776 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000777}
778
779void show_devs_subtree(struct device *root, int debug_level, const char *msg)
780{
781 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100782 if (!do_printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000783 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000784 return;
785 do_printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300786 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000787}
788
789void show_all_devs(int debug_level, const char *msg)
790{
791 struct device *dev;
792
793 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100794 if (!do_printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000795 return;
796 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000797 do_printk(debug_level, "%s: enabled %d\n",
798 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000799 }
800}
801
802void show_one_resource(int debug_level, struct device *dev,
803 struct resource *resource, const char *comment)
804{
805 char buf[10];
806 unsigned long long base, end;
807 base = resource->base;
808 end = resource_end(resource);
809 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000810
Uwe Hermanne4870472010-11-04 23:23:47 +0000811 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100812 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000813 resource->index, base, end, resource->size, resource->gran,
814 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000815}
816
817void show_all_devs_resources(int debug_level, const char* msg)
818{
819 struct device *dev;
820
Paul Menzeleb605d72015-02-12 00:29:32 +0100821 if (!do_printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000822 return;
823
824 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000825 struct resource *res;
826 do_printk(debug_level, "%s: enabled %d\n",
827 dev_path(dev), dev->enabled);
828 for (res = dev->resource_list; res; res = res->next)
829 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000830 }
831}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000832
Aaron Durbinf0349022017-11-10 10:47:54 -0700833void fixed_mem_resource(struct device *dev, unsigned long index,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200834 unsigned long basek, unsigned long sizek,
835 unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000836{
837 struct resource *resource;
838
839 if (!sizek)
840 return;
841
842 resource = new_resource(dev, index);
843 resource->base = ((resource_t)basek) << 10;
844 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300845 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
846 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000847
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300848 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300849}
850
Subrata Banik217ca362019-03-15 17:18:44 +0530851void fixed_io_resource(struct device *dev, unsigned long index,
852 unsigned long base, unsigned long size)
853{
854 struct resource *resource;
855
856 resource = new_resource(dev, index);
857 resource->base = (resource_t)base;
858 resource->size = (resource_t)size;
859 resource->limit = resource->base + resource->size - 1;
860 resource->flags = IORESOURCE_IO | IORESOURCE_FIXED |
861 IORESOURCE_STORED | IORESOURCE_ASSIGNED |
862 IORESOURCE_RESERVE;
863}
864
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200865void mmconf_resource_init(struct resource *resource, resource_t base,
866 int buses)
867{
868 resource->base = base;
869 resource->size = buses * MiB;
870 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
871 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
872
873 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR "
874 "0x%08lx-0x%08lx.\n", (unsigned long)(resource->base),
875 (unsigned long)(resource->base + resource->size));
876}
877
878void mmconf_resource(struct device *dev, unsigned long index)
879{
880 struct resource *resource = new_resource(dev, index);
881 mmconf_resource_init(resource, CONFIG_MMCONF_BASE_ADDRESS,
882 CONFIG_MMCONF_BUS_NUMBER);
883}
884
Uwe Hermann4b42a622010-10-11 19:36:13 +0000885void tolm_test(void *gp, struct device *dev, struct resource *new)
886{
887 struct resource **best_p = gp;
888 struct resource *best;
889
890 best = *best_p;
891
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700892 /*
893 * If resource is not allocated any space i.e. size is zero,
894 * then do not consider this resource in tolm calculations.
895 */
896 if (new->size == 0)
897 return;
898
Uwe Hermann4b42a622010-10-11 19:36:13 +0000899 if (!best || (best->base > new->base))
900 best = new;
901
902 *best_p = best;
903}
904
905u32 find_pci_tolm(struct bus *bus)
906{
907 struct resource *min = NULL;
908 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700909 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000910
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700911 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000912
913 tolm = 0xffffffffUL;
914
915 if (min && tolm > min->base)
916 tolm = min->base;
917
918 return tolm;
919}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700920
921/* Count of enabled CPUs */
922int dev_count_cpu(void)
923{
Aaron Durbinf0349022017-11-10 10:47:54 -0700924 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700925 int count = 0;
926
927 for (cpu = all_devices; cpu; cpu = cpu->next) {
928 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800929 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700930 continue;
931 if (!cpu->enabled)
932 continue;
933 count++;
934 }
935
936 return count;
937}
Lee Leahya95baf92016-02-13 06:10:04 -0800938
939/* Get device path name */
940const char *dev_path_name(enum device_path_type type)
941{
942 static const char *const type_names[] = DEVICE_PATH_NAMES;
943 const char *type_name = "Unknown";
944
945 /* Translate the type value into a string */
946 if (type < ARRAY_SIZE(type_names))
947 type_name = type_names[type];
948 return type_name;
949}