blob: 47df3052e24ae5982ce6a96ca4b324829794908f [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000019 */
20
Eric Biederman8ca8d762003-04-22 19:02:15 +000021#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000022#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000023#include <device/path.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020024#include <device/pci_def.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000025#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000026#include <string.h>
27
Eric Biederman03acab62004-10-14 21:25:53 +000028/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070029 * Given a Local APIC ID, find the device structure.
30 *
31 * @param apic_id The Local APIC ID number.
32 * @return Pointer to the device structure (if found), 0 otherwise.
33 */
Aaron Durbinf0349022017-11-10 10:47:54 -070034struct device *dev_find_lapic(unsigned apic_id)
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070035{
Aaron Durbinf0349022017-11-10 10:47:54 -070036 struct device *dev;
37 struct device *result = NULL;
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070038
39 for (dev = all_devices; dev; dev = dev->next) {
40 if (dev->path.type == DEVICE_PATH_APIC &&
41 dev->path.apic.apic_id == apic_id) {
42 result = dev;
43 break;
44 }
45 }
46 return result;
47}
48
49/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000050 * Find a device of a given vendor and type.
51 *
52 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
53 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +000054 * @param from Pointer to the device structure, used as a starting point in
55 * the linked list of all_devices, which can be 0 to start at the
56 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000057 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000058 */
Uwe Hermanne4870472010-11-04 23:23:47 +000059struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +000060{
61 if (!from)
62 from = all_devices;
63 else
64 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000065
66 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +000067 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000068
Eric Biederman8ca8d762003-04-22 19:02:15 +000069 return from;
70}
71
Uwe Hermannc1ee4292010-10-17 19:01:48 +000072/**
73 * Find a device of a given class.
74 *
75 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +000076 * @param from Pointer to the device structure, used as a starting point in
77 * the linked list of all_devices, which can be 0 to start at the
78 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +000079 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +000080 */
81struct device *dev_find_class(unsigned int class, struct device *from)
82{
83 if (!from)
84 from = all_devices;
85 else
86 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000087
Greg Watson59651692003-12-17 17:39:53 +000088 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +000089 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +000090
Eric Biederman8ca8d762003-04-22 19:02:15 +000091 return from;
92}
93
Duncan Laurie5f5d9142013-06-10 09:59:17 -070094/**
95 * Encode the device path into 3 bytes for logging to CMOS.
96 *
97 * @param dev The device path to encode.
98 * @return Device path encoded into lower 3 bytes of dword.
99 */
Subrata Banik564547f2018-05-02 10:27:36 +0530100u32 dev_path_encode(const struct device *dev)
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700101{
102 u32 ret;
103
104 if (!dev)
105 return 0;
106
107 /* Store the device type in 3rd byte. */
108 ret = dev->path.type << 16;
109
110 /* Encode the device specific path in the low word. */
111 switch (dev->path.type) {
112 case DEVICE_PATH_ROOT:
113 break;
114 case DEVICE_PATH_PCI:
115 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
116 break;
117 case DEVICE_PATH_PNP:
118 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
119 break;
120 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700121 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700122 break;
123 case DEVICE_PATH_APIC:
124 ret |= dev->path.apic.apic_id;
125 break;
126 case DEVICE_PATH_DOMAIN:
127 ret |= dev->path.domain.domain;
128 break;
129 case DEVICE_PATH_CPU_CLUSTER:
130 ret |= dev->path.cpu_cluster.cluster;
131 break;
132 case DEVICE_PATH_CPU:
133 ret |= dev->path.cpu.id;
134 break;
135 case DEVICE_PATH_CPU_BUS:
136 ret |= dev->path.cpu_bus.id;
137 break;
138 case DEVICE_PATH_IOAPIC:
139 ret |= dev->path.ioapic.ioapic_id;
140 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700141 case DEVICE_PATH_GENERIC:
142 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
143 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800144 case DEVICE_PATH_SPI:
145 ret |= dev->path.spi.cs;
146 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700147 case DEVICE_PATH_USB:
148 ret |= dev->path.usb.port_type << 8 || dev->path.usb.port_id;
149 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700150 case DEVICE_PATH_NONE:
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800151 case DEVICE_PATH_MMIO: /* don't care */
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700152 default:
153 break;
154 }
155
156 return ret;
157}
158
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000159/*
160 * Warning: This function uses a static buffer. Don't call it more than once
161 * from the same print statement!
162 */
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200163const char *dev_path(const struct device *dev)
Eric Biedermane9a271e32003-09-02 03:36:25 +0000164{
165 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000166
Eric Biedermane9a271e32003-09-02 03:36:25 +0000167 buffer[0] = '\0';
168 if (!dev) {
169 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000170 } else {
Elyes HAOUAS0ce41f12018-11-13 10:03:31 +0100171 switch (dev->path.type) {
Patrick Rudolph6a811842017-11-01 11:33:00 +0100172 case DEVICE_PATH_NONE:
173 memcpy(buffer, "NONE", 5);
174 break;
Eric Biederman83b991a2003-10-11 06:20:25 +0000175 case DEVICE_PATH_ROOT:
176 memcpy(buffer, "Root Device", 12);
177 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000178 case DEVICE_PATH_PCI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200179 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100180 "PCI: %02x:%02x.%01x",
181 dev->bus->secondary,
182 PCI_SLOT(dev->path.pci.devfn),
183 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000184 break;
185 case DEVICE_PATH_PNP:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200186 snprintf(buffer, sizeof(buffer), "PNP: %04x.%01x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100187 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000188 break;
189 case DEVICE_PATH_I2C:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200190 snprintf(buffer, sizeof(buffer), "I2C: %02x:%02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100191 dev->bus->secondary,
192 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000193 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000194 case DEVICE_PATH_APIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200195 snprintf(buffer, sizeof(buffer), "APIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100196 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000197 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200198 case DEVICE_PATH_IOAPIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200199 snprintf(buffer, sizeof(buffer), "IOAPIC: %02x",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100200 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200201 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800202 case DEVICE_PATH_DOMAIN:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200203 snprintf(buffer, sizeof(buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800204 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000205 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800206 case DEVICE_PATH_CPU_CLUSTER:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200207 snprintf(buffer, sizeof(buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800208 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000209 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000210 case DEVICE_PATH_CPU:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200211 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100212 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000213 break;
214 case DEVICE_PATH_CPU_BUS:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200215 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100216 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000217 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700218 case DEVICE_PATH_GENERIC:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200219 snprintf(buffer, sizeof(buffer),
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700220 "GENERIC: %d.%d", dev->path.generic.id,
221 dev->path.generic.subid);
222 break;
Furquan Shaikh7606c372017-02-11 10:57:23 -0800223 case DEVICE_PATH_SPI:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200224 snprintf(buffer, sizeof(buffer), "SPI: %02x",
Furquan Shaikh7606c372017-02-11 10:57:23 -0800225 dev->path.spi.cs);
226 break;
Duncan Lauriebae9f852018-05-07 14:18:13 -0700227 case DEVICE_PATH_USB:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200228 snprintf(buffer, sizeof(buffer), "USB%u port %u",
Duncan Lauriebae9f852018-05-07 14:18:13 -0700229 dev->path.usb.port_type, dev->path.usb.port_id);
230 break;
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800231 case DEVICE_PATH_MMIO:
Elyes HAOUASb7482212018-05-16 12:58:19 +0200232 snprintf(buffer, sizeof(buffer), "MMIO: %08x",
Justin TerAvestca2ed9f2018-01-17 16:36:30 -0800233 dev->path.mmio.addr);
234 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000235 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000236 printk(BIOS_ERR, "Unknown device path type: %d\n",
237 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000238 break;
239 }
240 }
241 return buffer;
242}
243
Aaron Durbinf0349022017-11-10 10:47:54 -0700244const char *dev_name(struct device *dev)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200245{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300246 if (dev->name)
247 return dev->name;
248 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200249 return dev->chip_ops->name;
250 else
251 return "unknown";
252}
253
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000254const char *bus_path(struct bus *bus)
255{
256 static char buffer[BUS_PATH_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200257 snprintf(buffer, sizeof(buffer),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100258 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000259 return buffer;
260}
261
Eric Biederman5cd81732004-03-11 15:01:31 +0000262/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000263 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000264 *
265 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000266 */
267static int allocate_more_resources(void)
268{
269 int i;
270 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000271
Myles Watsonc25cc112010-05-21 14:33:48 +0000272 new_res_list = malloc(64 * sizeof(*new_res_list));
273
274 if (new_res_list == NULL)
275 return 0;
276
277 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
278
Uwe Hermanne4870472010-11-04 23:23:47 +0000279 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000280 new_res_list[i].next = &new_res_list[i+1];
281
282 free_resources = new_res_list;
283 return 1;
284}
285
286/**
287 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000288 *
289 * @param dev TODO
290 * @param res TODO
291 * @param prev TODO
292 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000293 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700294static void free_resource(struct device *dev, struct resource *res,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000295 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000296{
297 if (prev)
298 prev->next = res->next;
299 else
300 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000301
Myles Watsonc25cc112010-05-21 14:33:48 +0000302 res->next = free_resources;
303 free_resources = res;
304}
305
306/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000307 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000308 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000309 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000310 *
311 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000312 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700313void compact_resources(struct device *dev)
Eric Biederman5cd81732004-03-11 15:01:31 +0000314{
Myles Watsonc25cc112010-05-21 14:33:48 +0000315 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000316
Eric Biederman5cd81732004-03-11 15:01:31 +0000317 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000318 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000319 next = res->next;
320 if (!res->flags)
321 free_resource(dev, res, prev);
322 else
323 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000324 }
325}
326
327/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000328 * See if a resource structure already exists for a given index.
329 *
330 * @param dev The device to find the resource on.
331 * @param index The index of the resource on the device.
332 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000333 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700334struct resource *probe_resource(struct device *dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000335{
Myles Watsonc25cc112010-05-21 14:33:48 +0000336 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000337
Eric Biederman5cd81732004-03-11 15:01:31 +0000338 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000339 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000340 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000341 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000342 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000343
Myles Watsonc25cc112010-05-21 14:33:48 +0000344 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000345}
346
347/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000348 * See if a resource structure already exists for a given index and if not
349 * allocate one.
350 *
Paul Menzel20923872014-06-07 13:31:29 +0200351 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000352 *
353 * @param dev The device to find the resource on.
354 * @param index The index of the resource on the device.
355 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000356 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700357struct resource *new_resource(struct device *dev, unsigned index)
Eric Biederman03acab62004-10-14 21:25:53 +0000358{
Myles Watsonc25cc112010-05-21 14:33:48 +0000359 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000360
Uwe Hermanne4870472010-11-04 23:23:47 +0000361 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000362 compact_resources(dev);
363
Uwe Hermanne4870472010-11-04 23:23:47 +0000364 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000365 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000366 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000367 if (free_resources == NULL && !allocate_more_resources())
368 die("Couldn't allocate more resources.");
369
370 resource = free_resources;
371 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000372 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000373 resource->next = NULL;
374 tail = dev->resource_list;
375 if (tail) {
376 while (tail->next) tail = tail->next;
377 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000378 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000379 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000380 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000381 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000382
383 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000384 if (!(resource->flags & IORESOURCE_FIXED)) {
385 resource->flags = 0;
386 resource->base = 0;
387 }
388 resource->size = 0;
389 resource->limit = 0;
390 resource->index = index;
391 resource->align = 0;
392 resource->gran = 0;
393
394 return resource;
395}
396
Eric Biederman03acab62004-10-14 21:25:53 +0000397/**
398 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000399 *
400 * @param dev The device to find the resource on.
401 * @param index The index of the resource on the device.
402 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000403 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700404struct resource *find_resource(struct device *dev, unsigned index)
Eric Biederman03acab62004-10-14 21:25:53 +0000405{
406 struct resource *resource;
407
Uwe Hermanne4870472010-11-04 23:23:47 +0000408 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000409 resource = probe_resource(dev, index);
410 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000411 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000412 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000413 die("");
414 }
415 return resource;
416}
417
Eric Biederman03acab62004-10-14 21:25:53 +0000418/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000419 * Round a number up to the next multiple of gran.
420 *
421 * @param val The starting value.
422 * @param gran Granularity we are aligning the number to.
423 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000424 */
425static resource_t align_up(resource_t val, unsigned long gran)
426{
427 resource_t mask;
428 mask = (1ULL << gran) - 1ULL;
429 val += mask;
430 val &= ~mask;
431 return val;
432}
433
434/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000435 * Round a number up to the previous multiple of gran.
436 *
437 * @param val The starting value.
438 * @param gran Granularity we are aligning the number to.
439 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000440 */
441static resource_t align_down(resource_t val, unsigned long gran)
442{
443 resource_t mask;
444 mask = (1ULL << gran) - 1ULL;
445 val &= ~mask;
446 return val;
447}
448
449/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000450 * Compute the maximum address that is part of a resource.
451 *
452 * @param resource The resource whose limit is desired.
453 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000454 */
455resource_t resource_end(struct resource *resource)
456{
457 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000458
459 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000460 base = resource->base;
461
Uwe Hermanne4870472010-11-04 23:23:47 +0000462 /*
463 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000464 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000465 * the bridge. While the granularity is simply how many low bits of
466 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000467 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000468
Uwe Hermanne4870472010-11-04 23:23:47 +0000469 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000470 end = base + align_up(resource->size, resource->gran) - 1;
471
472 return end;
473}
474
475/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000476 * Compute the maximum legal value for resource->base.
477 *
478 * @param resource The resource whose maximum is desired.
479 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000480 */
481resource_t resource_max(struct resource *resource)
482{
483 resource_t max;
484
485 max = align_down(resource->limit - resource->size + 1, resource->align);
486
487 return max;
488}
489
490/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000491 * Return the resource type of a resource.
492 *
493 * @param resource The resource type to decode.
494 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000495 */
496const char *resource_type(struct resource *resource)
497{
498 static char buffer[RESOURCE_TYPE_MAX];
Elyes HAOUASb7482212018-05-16 12:58:19 +0200499 snprintf(buffer, sizeof(buffer), "%s%s%s%s",
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100500 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
501 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
502 ((resource->flags == 0) ? "unused" :
503 (resource->flags & IORESOURCE_IO) ? "io" :
504 (resource->flags & IORESOURCE_DRQ) ? "drq" :
505 (resource->flags & IORESOURCE_IRQ) ? "irq" :
506 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
507 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000508 return buffer;
509}
510
511/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000512 * Print the resource that was just stored.
513 *
Martin Roth63373ed2013-07-08 16:24:19 -0600514 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000515 * @param resource The resource that was just stored.
516 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000517 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700518void report_resource_stored(struct device *dev, struct resource *resource,
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000519 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000520{
Uwe Hermanne4870472010-11-04 23:23:47 +0000521 char buf[10];
522 unsigned long long base, end;
523
524 if (!(resource->flags & IORESOURCE_STORED))
525 return;
526
527 base = resource->base;
528 end = resource_end(resource);
529 buf[0] = '\0';
530
531 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Elyes HAOUASb7482212018-05-16 12:58:19 +0200532 snprintf(buf, sizeof(buf),
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100533 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000534 }
Patrick Georgi51615092012-03-11 19:31:03 +0100535 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000536 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
537 base, end, resource->size, resource->gran, buf,
538 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000539}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000540
Uwe Hermanne4870472010-11-04 23:23:47 +0000541void search_bus_resources(struct bus *bus, unsigned long type_mask,
542 unsigned long type, resource_search_t search,
543 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000544{
545 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000546
Myles Watson894a3472010-06-09 22:41:35 +0000547 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000548 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000549
550 /* Ignore disabled devices. */
551 if (!curdev->enabled)
552 continue;
553
Myles Watson894a3472010-06-09 22:41:35 +0000554 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000555 /* If it isn't the right kind of resource ignore it. */
556 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000557 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000558
559 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000560 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000561 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000562 for (subbus = curdev->link_list; subbus;
563 subbus = subbus->next)
564 if (subbus->link_num
565 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000566 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700567 if (!subbus) /* Why can subbus be NULL? */
568 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000569 search_bus_resources(subbus, type_mask, type,
570 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000571 continue;
572 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000573 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000574 }
575 }
576}
577
Uwe Hermanne4870472010-11-04 23:23:47 +0000578void search_global_resources(unsigned long type_mask, unsigned long type,
579 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000580{
581 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000582
Myles Watson894a3472010-06-09 22:41:35 +0000583 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000584 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000585
586 /* Ignore disabled devices. */
587 if (!curdev->enabled)
588 continue;
589
Myles Watson894a3472010-06-09 22:41:35 +0000590 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000591 /* If it isn't the right kind of resource ignore it. */
592 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000593 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000594
595 /* If it is a subtractive resource ignore it. */
596 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000597 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000598
Myles Watsonc25cc112010-05-21 14:33:48 +0000599 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000600 }
601 }
602}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000603
Aaron Durbinf0349022017-11-10 10:47:54 -0700604void dev_set_enabled(struct device *dev, int enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000605{
Uwe Hermanne4870472010-11-04 23:23:47 +0000606 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000607 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000608
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000609 dev->enabled = enable;
610 if (dev->ops && dev->ops->enable) {
611 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000612 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000613 dev->chip_ops->enable_dev(dev);
614 }
615}
616
617void disable_children(struct bus *bus)
618{
Aaron Durbinf0349022017-11-10 10:47:54 -0700619 struct device *child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000620
Myles Watson894a3472010-06-09 22:41:35 +0000621 for (child = bus->children; child; child = child->sibling) {
622 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000623 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000624 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000625 dev_set_enabled(child, 0);
626 }
627}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000628
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200629/*
630 * Returns true if the device is an enabled bridge that has at least
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200631 * one enabled device on its secondary bus that is not of type NONE.
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200632 */
Aaron Durbinf0349022017-11-10 10:47:54 -0700633bool dev_is_active_bridge(struct device *dev)
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200634{
635 struct bus *link;
Aaron Durbinf0349022017-11-10 10:47:54 -0700636 struct device *child;
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200637
638 if (!dev || !dev->enabled)
639 return 0;
640
641 if (!dev->link_list || !dev->link_list->children)
642 return 0;
643
644 for (link = dev->link_list; link; link = link->next) {
645 for (child = link->children; child; child = child->sibling) {
Patrick Rudolphbd7739f2019-04-24 09:35:51 +0200646 if (child->path.type == DEVICE_PATH_NONE)
647 continue;
648
Patrick Rudolpha6909f82017-05-22 18:30:27 +0200649 if (child->enabled)
650 return 1;
651 }
652 }
653
654 return 0;
655}
656
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200657static void resource_tree(const struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000658{
Myles Watson894a3472010-06-09 22:41:35 +0000659 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000660 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000661 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000662 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000663 char indent[30]; /* If your tree has more levels, it's wrong. */
664
665 for (i = 0; i < depth + 1 && i < 29; i++)
666 indent[i] = ' ';
667 indent[i] = '\0';
668
Martin Roth7f35d3a2017-07-23 16:22:25 -0600669 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
670 if (root->link_list && root->link_list->children)
671 do_printk(BIOS_DEBUG, " child on link 0 %s",
672 dev_path(root->link_list->children));
673 do_printk(BIOS_DEBUG, "\n");
Myles Watson894a3472010-06-09 22:41:35 +0000674
Myles Watsonc25cc112010-05-21 14:33:48 +0000675 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000676 do_printk(debug_level, "%s%s resource base %llx size %llx "
677 "align %d gran %d limit %llx flags %lx index %lx\n",
678 indent, dev_path(root), res->base, res->size,
679 res->align, res->gran, res->limit, res->flags,
680 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000681 }
682
Myles Watson894a3472010-06-09 22:41:35 +0000683 for (link = root->link_list; link; link = link->next) {
684 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000685 resource_tree(child, debug_level, depth + 1);
686 }
687}
688
Elyes HAOUASe3480662018-05-06 20:32:23 +0200689void print_resource_tree(const struct device *root, int debug_level,
690 const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000691{
692 /* Bail if root is null. */
693 if (!root) {
694 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
695 return;
696 }
697
698 /* Bail if not printing to screen. */
699 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000700 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000701 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000702
Myles Watsonbb3d8122009-05-11 22:45:35 +0000703 resource_tree(root, debug_level, 0);
704}
705
Lubomir Rintel9ba8f7c2018-04-26 00:00:22 +0200706void show_devs_tree(const struct device *dev, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000707{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800708 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000709 int i;
710 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000711 struct bus *link;
712
Myles Watsonbb3d8122009-05-11 22:45:35 +0000713 for (i = 0; i < depth; i++)
714 depth_str[i] = ' ';
715 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000716
Myles Watsonc25cc112010-05-21 14:33:48 +0000717 do_printk(debug_level, "%s%s: enabled %d\n",
718 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000719
Myles Watson894a3472010-06-09 22:41:35 +0000720 for (link = dev->link_list; link; link = link->next) {
721 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000722 sibling = sibling->sibling)
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300723 show_devs_tree(sibling, debug_level, depth + 1);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000724 }
725}
726
727void show_all_devs_tree(int debug_level, const char *msg)
728{
729 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100730 if (!do_printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000731 return;
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300732 show_devs_tree(all_devices, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000733}
734
735void show_devs_subtree(struct device *root, int debug_level, const char *msg)
736{
737 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100738 if (!do_printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000739 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000740 return;
741 do_printk(debug_level, "%s\n", msg);
Kyösti Mälkki3d3c8c32016-08-15 10:04:21 +0300742 show_devs_tree(root, debug_level, 0);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000743}
744
745void show_all_devs(int debug_level, const char *msg)
746{
747 struct device *dev;
748
749 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100750 if (!do_printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000751 return;
752 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000753 do_printk(debug_level, "%s: enabled %d\n",
754 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000755 }
756}
757
758void show_one_resource(int debug_level, struct device *dev,
759 struct resource *resource, const char *comment)
760{
761 char buf[10];
762 unsigned long long base, end;
763 base = resource->base;
764 end = resource_end(resource);
765 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000766
Uwe Hermanne4870472010-11-04 23:23:47 +0000767 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100768 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000769 resource->index, base, end, resource->size, resource->gran,
770 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000771}
772
773void show_all_devs_resources(int debug_level, const char* msg)
774{
775 struct device *dev;
776
Paul Menzeleb605d72015-02-12 00:29:32 +0100777 if (!do_printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000778 return;
779
780 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000781 struct resource *res;
782 do_printk(debug_level, "%s: enabled %d\n",
783 dev_path(dev), dev->enabled);
784 for (res = dev->resource_list; res; res = res->next)
785 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000786 }
787}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000788
Aaron Durbinf0349022017-11-10 10:47:54 -0700789void fixed_mem_resource(struct device *dev, unsigned long index,
Elyes HAOUASe3480662018-05-06 20:32:23 +0200790 unsigned long basek, unsigned long sizek,
791 unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000792{
793 struct resource *resource;
794
795 if (!sizek)
796 return;
797
798 resource = new_resource(dev, index);
799 resource->base = ((resource_t)basek) << 10;
800 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300801 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
802 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000803
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300804 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300805}
806
Subrata Banik217ca362019-03-15 17:18:44 +0530807void fixed_io_resource(struct device *dev, unsigned long index,
808 unsigned long base, unsigned long size)
809{
810 struct resource *resource;
811
812 resource = new_resource(dev, index);
813 resource->base = (resource_t)base;
814 resource->size = (resource_t)size;
815 resource->limit = resource->base + resource->size - 1;
816 resource->flags = IORESOURCE_IO | IORESOURCE_FIXED |
817 IORESOURCE_STORED | IORESOURCE_ASSIGNED |
818 IORESOURCE_RESERVE;
819}
820
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200821void mmconf_resource_init(struct resource *resource, resource_t base,
822 int buses)
823{
824 resource->base = base;
825 resource->size = buses * MiB;
826 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
827 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
828
829 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR "
830 "0x%08lx-0x%08lx.\n", (unsigned long)(resource->base),
831 (unsigned long)(resource->base + resource->size));
832}
833
834void mmconf_resource(struct device *dev, unsigned long index)
835{
836 struct resource *resource = new_resource(dev, index);
837 mmconf_resource_init(resource, CONFIG_MMCONF_BASE_ADDRESS,
838 CONFIG_MMCONF_BUS_NUMBER);
839}
840
Uwe Hermann4b42a622010-10-11 19:36:13 +0000841void tolm_test(void *gp, struct device *dev, struct resource *new)
842{
843 struct resource **best_p = gp;
844 struct resource *best;
845
846 best = *best_p;
847
848 if (!best || (best->base > new->base))
849 best = new;
850
851 *best_p = best;
852}
853
854u32 find_pci_tolm(struct bus *bus)
855{
856 struct resource *min = NULL;
857 u32 tolm;
858
859 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
860 tolm_test, &min);
861
862 tolm = 0xffffffffUL;
863
864 if (min && tolm > min->base)
865 tolm = min->base;
866
867 return tolm;
868}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700869
870/* Count of enabled CPUs */
871int dev_count_cpu(void)
872{
Aaron Durbinf0349022017-11-10 10:47:54 -0700873 struct device *cpu;
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700874 int count = 0;
875
876 for (cpu = all_devices; cpu; cpu = cpu->next) {
877 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800878 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700879 continue;
880 if (!cpu->enabled)
881 continue;
882 count++;
883 }
884
885 return count;
886}
Lee Leahya95baf92016-02-13 06:10:04 -0800887
888/* Get device path name */
889const char *dev_path_name(enum device_path_type type)
890{
891 static const char *const type_names[] = DEVICE_PATH_NAMES;
892 const char *type_name = "Unknown";
893
894 /* Translate the type value into a string */
895 if (type < ARRAY_SIZE(type_names))
896 type_name = type_names[type];
897 return type_name;
898}