blob: 452a87bf14011ad5d0800ccd2904bd673c151ce5 [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;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700133 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800134 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700135 default:
136 break;
137 }
138
139 return ret;
140}
141
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000142/*
143 * Warning: This function uses a static buffer. Don't call it more than once
144 * from the same print statement!
145 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200146const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000147{
148 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000149
Eric Biedermane9a271e32003-09-02 03:36:25 +0000150 buffer[0] = '\0';
151 if (!dev) {
152 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000153 } else {
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100154 switch (dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100155 case DEVICE_PATH_NONE:
156 memcpy(buffer, "NONE", 5);
157 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000158 case DEVICE_PATH_ROOT:
159 memcpy(buffer, "Root Device", 12);
160 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000161 case DEVICE_PATH_PCI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200162 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100163 "PCI: %02x:%02x.%01x",
164 dev->bus->secondary,
165 PCI_SLOT(dev->path.pci.devfn),
166 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000167 break;
168 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200169 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100170 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000171 break;
172 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200173 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100174 dev->bus->secondary,
175 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000176 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000177 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200178 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100179 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000180 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200181 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200182 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100183 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200184 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800185 case DEVICE_PATH_DOMAIN:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200186 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800187 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000188 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800189 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200190 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800191 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000192 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000193 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200194 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100195 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000196 break;
197 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200198 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100199 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000200 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700201 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200202 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700203 "GENERIC: %d.%d", dev->path.generic.id,
204 dev->path.generic.subid);
205 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800206 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200207 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800208 dev->path.spi.cs);
209 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700210 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200211 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700212 dev->path.usb.port_type, dev->path.usb.port_id);
213 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800214 case DEVICE_PATH_MMIO:
Jacob Garberd552aca2019-07-16 12:55:00 -0600215 snprintf(buffer, sizeof(buffer), "MMIO: %08lx",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800216 dev->path.mmio.addr);
217 break;
Raul E Rangel3f3f53c2020-05-06 11:47:04 -0600218 case DEVICE_PATH_ESPI:
219 snprintf(buffer, sizeof(buffer), "ESPI: %08lx",
220 dev->path.espi.addr);
221 break;
222 case DEVICE_PATH_LPC:
223 snprintf(buffer, sizeof(buffer), "LPC: %08lx",
224 dev->path.lpc.addr);
225 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000226 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000227 printk(BIOS_ERR, "Unknown device path type: %d\n",
228 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000229 break;
230 }
231 }
232 return buffer;
233}
234
Furquan Shaikh5b5c2332020-04-24 21:31:35 -0700235const char *dev_name(const struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200236{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300237 if (dev->name)
238 return dev->name;
239 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200240 return dev->chip_ops->name;
241 else
242 return "unknown";
243}
244
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000245const char *bus_path(struct bus *bus)
246{
247 static char buffer[BUS_PATH_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200248 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100249 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000250 return buffer;
251}
252
Eric Biederman5cd81732004-03-11 15:01:31 +0000253/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000254 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000255 *
256 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000257 */
258static int allocate_more_resources(void)
259{
260 int i;
261 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000262
Myles Watsonc25cc112010-05-21 14:33:48 +0000263 new_res_list = malloc(64 * sizeof(*new_res_list));
264
265 if (new_res_list == NULL)
266 return 0;
267
268 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
269
Uwe Hermanne4870472010-11-04 23:23:47 +0000270 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000271 new_res_list[i].next = &new_res_list[i+1];
272
273 free_resources = new_res_list;
274 return 1;
275}
276
277/**
278 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000279 *
280 * @param dev TODO
281 * @param res TODO
282 * @param prev TODO
283 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000284 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700285static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000286 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000287{
288 if (prev)
289 prev->next = res->next;
290 else
291 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000292
Myles Watsonc25cc112010-05-21 14:33:48 +0000293 res->next = free_resources;
294 free_resources = res;
295}
296
297/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000298 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000299 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000300 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000301 *
302 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000303 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700304void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000305{
Myles Watsonc25cc112010-05-21 14:33:48 +0000306 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000307
Eric Biederman5cd81732004-03-11 15:01:31 +0000308 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000309 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000310 next = res->next;
311 if (!res->flags)
312 free_resource(dev, res, prev);
313 else
314 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000315 }
316}
317
318/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000319 * See if a resource structure already exists for a given index.
320 *
321 * @param dev The device to find the resource on.
322 * @param index The index of the resource on the device.
323 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000324 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600325struct resource *probe_resource(const struct device *dev, unsigned int index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000326{
Myles Watsonc25cc112010-05-21 14:33:48 +0000327 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000328
Eric Biederman5cd81732004-03-11 15:01:31 +0000329 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000330 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000331 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000332 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000333 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000334
Myles Watsonc25cc112010-05-21 14:33:48 +0000335 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000336}
337
338/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000339 * See if a resource structure already exists for a given index and if not
340 * allocate one.
341 *
Paul Menzel20923872014-06-07 13:31:29 +0200342 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000343 *
344 * @param dev The device to find the resource on.
345 * @param index The index of the resource on the device.
346 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000347 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600348struct resource *new_resource(struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000349{
Myles Watsonc25cc112010-05-21 14:33:48 +0000350 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000351
Uwe Hermanne4870472010-11-04 23:23:47 +0000352 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000353 compact_resources(dev);
354
Uwe Hermanne4870472010-11-04 23:23:47 +0000355 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000356 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000357 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000358 if (free_resources == NULL && !allocate_more_resources())
359 die("Couldn't allocate more resources.");
360
361 resource = free_resources;
362 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000363 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000364 resource->next = NULL;
365 tail = dev->resource_list;
366 if (tail) {
367 while (tail->next) tail = tail->next;
368 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000369 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000370 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000371 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000372 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000373
374 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000375 if (!(resource->flags & IORESOURCE_FIXED)) {
376 resource->flags = 0;
377 resource->base = 0;
378 }
379 resource->size = 0;
380 resource->limit = 0;
381 resource->index = index;
382 resource->align = 0;
383 resource->gran = 0;
384
385 return resource;
386}
387
Eric Biederman03acab62004-10-14 21:25:53 +0000388/**
389 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000390 *
391 * @param dev The device to find the resource on.
392 * @param index The index of the resource on the device.
393 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000394 */
Martin Roth38ddbfb2019-10-23 21:41:00 -0600395struct resource *find_resource(const struct device *dev, unsigned int index)
Eric Biederman03acab62004-10-14 21:25:53 +0000396{
397 struct resource *resource;
398
Uwe Hermanne4870472010-11-04 23:23:47 +0000399 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000400 resource = probe_resource(dev, index);
401 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000402 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000403 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000404 die("");
405 }
406 return resource;
407}
408
Eric Biederman03acab62004-10-14 21:25:53 +0000409/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000410 * Round a number up to the next multiple of gran.
411 *
412 * @param val The starting value.
413 * @param gran Granularity we are aligning the number to.
414 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000415 */
416static resource_t align_up(resource_t val, unsigned long gran)
417{
418 resource_t mask;
419 mask = (1ULL << gran) - 1ULL;
420 val += mask;
421 val &= ~mask;
422 return val;
423}
424
425/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000426 * Round a number up to the previous multiple of gran.
427 *
428 * @param val The starting value.
429 * @param gran Granularity we are aligning the number to.
430 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000431 */
432static resource_t align_down(resource_t val, unsigned long gran)
433{
434 resource_t mask;
435 mask = (1ULL << gran) - 1ULL;
436 val &= ~mask;
437 return val;
438}
439
440/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000441 * Compute the maximum address that is part of a resource.
442 *
443 * @param resource The resource whose limit is desired.
444 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000445 */
446resource_t resource_end(struct resource *resource)
447{
448 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000449
450 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000451 base = resource->base;
452
Uwe Hermanne4870472010-11-04 23:23:47 +0000453 /*
454 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000455 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000456 * the bridge. While the granularity is simply how many low bits of
457 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000458 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000459
Uwe Hermanne4870472010-11-04 23:23:47 +0000460 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000461 end = base + align_up(resource->size, resource->gran) - 1;
462
463 return end;
464}
465
466/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000467 * Compute the maximum legal value for resource->base.
468 *
469 * @param resource The resource whose maximum is desired.
470 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000471 */
472resource_t resource_max(struct resource *resource)
473{
474 resource_t max;
475
476 max = align_down(resource->limit - resource->size + 1, resource->align);
477
478 return max;
479}
480
481/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000482 * Return the resource type of a resource.
483 *
484 * @param resource The resource type to decode.
485 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000486 */
487const char *resource_type(struct resource *resource)
488{
489 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200490 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100491 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
492 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
493 ((resource->flags == 0) ? "unused" :
494 (resource->flags & IORESOURCE_IO) ? "io" :
495 (resource->flags & IORESOURCE_DRQ) ? "drq" :
496 (resource->flags & IORESOURCE_IRQ) ? "irq" :
497 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
498 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000499 return buffer;
500}
501
502/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000503 * Print the resource that was just stored.
504 *
Martin Roth63373ed2013-07-08 16:24:19 -0600505 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000506 * @param resource The resource that was just stored.
507 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000508 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700509void report_resource_stored(struct device *dev, struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000510 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000511{
Uwe Hermanne4870472010-11-04 23:23:47 +0000512 char buf[10];
513 unsigned long long base, end;
514
515 if (!(resource->flags & IORESOURCE_STORED))
516 return;
517
518 base = resource->base;
519 end = resource_end(resource);
520 buf[0] = '\0';
521
John Zhao4792f8f2020-09-22 14:52:42 -0700522 if (dev->link_list && (resource->flags & IORESOURCE_PCI_BRIDGE)) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200523 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100524 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000525 }
Patrick Georgi51615092012-03-11 19:31:03 +0100526 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000527 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
528 base, end, resource->size, resource->gran, buf,
529 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000530}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000531
Uwe Hermanne4870472010-11-04 23:23:47 +0000532void search_bus_resources(struct bus *bus, unsigned long type_mask,
533 unsigned long type, resource_search_t search,
534 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000535{
536 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000537
Myles Watson894a3472010-06-09 22:41:35 +0000538 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000539 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000540
541 /* Ignore disabled devices. */
542 if (!curdev->enabled)
543 continue;
544
Myles Watson894a3472010-06-09 22:41:35 +0000545 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000546 /* If it isn't the right kind of resource ignore it. */
547 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000548 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000549
550 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000551 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000552 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000553 for (subbus = curdev->link_list; subbus;
554 subbus = subbus->next)
555 if (subbus->link_num
556 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000557 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700558 if (!subbus) /* Why can subbus be NULL? */
559 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000560 search_bus_resources(subbus, type_mask, type,
561 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000562 continue;
563 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000564 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000565 }
566 }
567}
568
Uwe Hermanne4870472010-11-04 23:23:47 +0000569void search_global_resources(unsigned long type_mask, unsigned long type,
570 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000571{
572 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000573
Myles Watson894a3472010-06-09 22:41:35 +0000574 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000575 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000576
577 /* Ignore disabled devices. */
578 if (!curdev->enabled)
579 continue;
580
Myles Watson894a3472010-06-09 22:41:35 +0000581 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000582 /* If it isn't the right kind of resource ignore it. */
583 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000584 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000585
586 /* If it is a subtractive resource ignore it. */
587 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000588 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000589
Myles Watsonc25cc112010-05-21 14:33:48 +0000590 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000591 }
592 }
593}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000594
Aaron Durbinf0349022017-11-10 10:47:54 -0700595void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000596{
Uwe Hermanne4870472010-11-04 23:23:47 +0000597 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000598 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000599
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000600 dev->enabled = enable;
601 if (dev->ops && dev->ops->enable) {
602 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000603 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000604 dev->chip_ops->enable_dev(dev);
605 }
606}
607
608void disable_children(struct bus *bus)
609{
Aaron Durbinf0349022017-11-10 10:47:54 -0700610 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000611
Myles Watson894a3472010-06-09 22:41:35 +0000612 for (child = bus->children; child; child = child->sibling) {
613 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000614 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000615 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000616 dev_set_enabled(child, 0);
617 }
618}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000619
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200620/*
621 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200622 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200623 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700624bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200625{
626 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700627 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200628
629 if (!dev || !dev->enabled)
630 return 0;
631
632 if (!dev->link_list || !dev->link_list->children)
633 return 0;
634
635 for (link = dev->link_list; link; link = link->next) {
636 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200637 if (child->path.type == DEVICE_PATH_NONE)
638 continue;
639
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200640 if (child->enabled)
641 return 1;
642 }
643 }
644
645 return 0;
646}
647
Jacob Garber464f4d62019-06-05 17:03:30 -0600648/**
649 * Ensure the device has a minimum number of bus links.
650 *
651 * @param dev The device to add links to.
652 * @param total_links The minimum number of links to have.
653 */
654void add_more_links(struct device *dev, unsigned int total_links)
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600655{
656 struct bus *link, *last = NULL;
657 int link_num = -1;
658
659 for (link = dev->link_list; link; link = link->next) {
660 if (link_num < link->link_num)
661 link_num = link->link_num;
662 last = link;
663 }
664
665 if (last) {
666 int links = total_links - (link_num + 1);
667 if (links > 0) {
Jacob Garber464f4d62019-06-05 17:03:30 -0600668 link = malloc(links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600669 if (!link)
670 die("Couldn't allocate more links!\n");
Jacob Garber464f4d62019-06-05 17:03:30 -0600671 memset(link, 0, links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600672 last->next = link;
Jacob Garber464f4d62019-06-05 17:03:30 -0600673 } else {
674 /* No more links to add */
675 return;
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600676 }
677 } else {
Jacob Garber464f4d62019-06-05 17:03:30 -0600678 link = malloc(total_links * sizeof(*link));
679 if (!link)
680 die("Couldn't allocate more links!\n");
681 memset(link, 0, total_links * sizeof(*link));
Jacob Garberf77f7cd2019-06-05 16:32:28 -0600682 dev->link_list = link;
683 }
684
685 for (link_num = link_num + 1; link_num < total_links; link_num++) {
686 link->link_num = link_num;
687 link->dev = dev;
688 link->next = link + 1;
689 last = link;
690 link = link->next;
691 }
692 last->next = NULL;
693}
694
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200695static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000696{
Myles Watson894a3472010-06-09 22:41:35 +0000697 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000698 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000699 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000700 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000701 char indent[30]; /* If your tree has more levels, it's wrong. */
702
703 for (i = 0; i < depth + 1 && i < 29; i++)
704 indent[i] = ' ';
705 indent[i] = '\0';
706
Martin Roth7f35d3a2017-07-23 16:22:25 -0600707 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
708 if (root->link_list && root->link_list->children)
709 do_printk(BIOS_DEBUG, " child on link 0 %s",
710 dev_path(root->link_list->children));
711 do_printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000712
Myles Watsonc25cc112010-05-21 14:33:48 +0000713 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000714 do_printk(debug_level, "%s%s resource base %llx size %llx "
715 "align %d gran %d limit %llx flags %lx index %lx\n",
716 indent, dev_path(root), res->base, res->size,
717 res->align, res->gran, res->limit, res->flags,
718 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000719 }
720
Myles Watson894a3472010-06-09 22:41:35 +0000721 for (link = root->link_list; link; link = link->next) {
722 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000723 resource_tree(child, debug_level, depth + 1);
724 }
725}
726
Elyes HAOUASe3480662018-05-06 20:32:23 +0200727void print_resource_tree(const struct device *root, int debug_level,
728 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000729{
730 /* Bail if root is null. */
731 if (!root) {
732 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
733 return;
734 }
735
736 /* Bail if not printing to screen. */
737 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000738 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000739 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000740
Myles Watsonbb3d8122009-05-11 22:45:35 +0000741 resource_tree(root, debug_level, 0);
742}
743
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200744void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000745{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800746 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000747 int i;
748 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000749 struct bus *link;
750
Myles Watsonbb3d8122009-05-11 22:45:35 +0000751 for (i = 0; i < depth; i++)
752 depth_str[i] = ' ';
753 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000754
Myles Watsonc25cc112010-05-21 14:33:48 +0000755 do_printk(debug_level, "%s%s: enabled %d\n",
756 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000757
Myles Watson894a3472010-06-09 22:41:35 +0000758 for (link = dev->link_list; link; link = link->next) {
759 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000760 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300761 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000762 }
763}
764
765void show_all_devs_tree(int debug_level, const char *msg)
766{
767 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100768 if (!do_printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000769 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300770 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000771}
772
773void show_devs_subtree(struct device *root, int debug_level, const char *msg)
774{
775 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100776 if (!do_printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000777 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000778 return;
779 do_printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300780 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000781}
782
783void show_all_devs(int debug_level, const char *msg)
784{
785 struct device *dev;
786
787 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100788 if (!do_printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000789 return;
790 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000791 do_printk(debug_level, "%s: enabled %d\n",
792 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000793 }
794}
795
796void show_one_resource(int debug_level, struct device *dev,
797 struct resource *resource, const char *comment)
798{
799 char buf[10];
800 unsigned long long base, end;
801 base = resource->base;
802 end = resource_end(resource);
803 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000804
Uwe Hermanne4870472010-11-04 23:23:47 +0000805 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100806 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000807 resource->index, base, end, resource->size, resource->gran,
808 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000809}
810
811void show_all_devs_resources(int debug_level, const char* msg)
812{
813 struct device *dev;
814
Paul Menzeleb605d72015-02-12 00:29:32 +0100815 if (!do_printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000816 return;
817
818 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000819 struct resource *res;
820 do_printk(debug_level, "%s: enabled %d\n",
821 dev_path(dev), dev->enabled);
822 for (res = dev->resource_list; res; res = res->next)
823 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000824 }
825}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000826
Aaron Durbinf0349022017-11-10 10:47:54 -0700827void fixed_mem_resource(struct device *dev, unsigned long index,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200828 unsigned long basek, unsigned long sizek,
829 unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000830{
831 struct resource *resource;
832
833 if (!sizek)
834 return;
835
836 resource = new_resource(dev, index);
837 resource->base = ((resource_t)basek) << 10;
838 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300839 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
840 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000841
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300842 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300843}
844
Subrata Banik217ca362019-03-15 17:18:44 +0530845void fixed_io_resource(struct device *dev, unsigned long index,
846 unsigned long base, unsigned long size)
847{
848 struct resource *resource;
849
850 resource = new_resource(dev, index);
851 resource->base = (resource_t)base;
852 resource->size = (resource_t)size;
853 resource->limit = resource->base + resource->size - 1;
854 resource->flags = IORESOURCE_IO | IORESOURCE_FIXED |
855 IORESOURCE_STORED | IORESOURCE_ASSIGNED |
856 IORESOURCE_RESERVE;
857}
858
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200859void mmconf_resource_init(struct resource *resource, resource_t base,
860 int buses)
861{
862 resource->base = base;
863 resource->size = buses * MiB;
864 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
865 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
866
867 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR "
868 "0x%08lx-0x%08lx.\n", (unsigned long)(resource->base),
869 (unsigned long)(resource->base + resource->size));
870}
871
872void mmconf_resource(struct device *dev, unsigned long index)
873{
874 struct resource *resource = new_resource(dev, index);
875 mmconf_resource_init(resource, CONFIG_MMCONF_BASE_ADDRESS,
876 CONFIG_MMCONF_BUS_NUMBER);
877}
878
Uwe Hermann4b42a622010-10-11 19:36:13 +0000879void tolm_test(void *gp, struct device *dev, struct resource *new)
880{
881 struct resource **best_p = gp;
882 struct resource *best;
883
884 best = *best_p;
885
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700886 /*
887 * If resource is not allocated any space i.e. size is zero,
888 * then do not consider this resource in tolm calculations.
889 */
890 if (new->size == 0)
891 return;
892
Uwe Hermann4b42a622010-10-11 19:36:13 +0000893 if (!best || (best->base > new->base))
894 best = new;
895
896 *best_p = best;
897}
898
899u32 find_pci_tolm(struct bus *bus)
900{
901 struct resource *min = NULL;
902 u32 tolm;
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700903 unsigned long mask_match = IORESOURCE_MEM | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000904
Furquan Shaikhafaae8a2020-05-18 16:00:53 -0700905 search_bus_resources(bus, mask_match, mask_match, tolm_test, &min);
Uwe Hermann4b42a622010-10-11 19:36:13 +0000906
907 tolm = 0xffffffffUL;
908
909 if (min && tolm > min->base)
910 tolm = min->base;
911
912 return tolm;
913}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700914
915/* Count of enabled CPUs */
916int dev_count_cpu(void)
917{
Aaron Durbinf0349022017-11-10 10:47:54 -0700918 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700919 int count = 0;
920
921 for (cpu = all_devices; cpu; cpu = cpu->next) {
922 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800923 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700924 continue;
925 if (!cpu->enabled)
926 continue;
927 count++;
928 }
929
930 return count;
931}
Lee Leahya95baf92016-02-13 06:10:04 -0800932
933/* Get device path name */
934const char *dev_path_name(enum device_path_type type)
935{
936 static const char *const type_names[] = DEVICE_PATH_NAMES;
937 const char *type_name = "Unknown";
938
939 /* Translate the type value into a string */
940 if (type < ARRAY_SIZE(type_names))
941 type_name = type_names[type];
942 return type_name;
943}