blob: 180169da85c8bd769103e732e6b96acdaa213fdb [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.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
Paul Menzela46a7122013-02-23 18:37:27 +010022 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Uwe Hermannb80dbf02007-04-22 19:08:13 +000023 */
24
Eric Biederman8ca8d762003-04-22 19:02:15 +000025#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000026#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000027#include <device/path.h>
28#include <device/pci.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000029#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000030#include <string.h>
31
Eric Biederman03acab62004-10-14 21:25:53 +000032/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000033 * See if a device structure exists for path.
Eric Biederman03acab62004-10-14 21:25:53 +000034 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000035 * @param parent The bus to find the device on.
36 * @param path The relative path from the bus to the appropriate device.
37 * @return Pointer to a device structure for the device on bus at path
38 * or 0/NULL if no device is found.
Eric Biederman03acab62004-10-14 21:25:53 +000039 */
40device_t find_dev_path(struct bus *parent, struct device_path *path)
41{
42 device_t child;
Myles Watson894a3472010-06-09 22:41:35 +000043 for (child = parent->children; child; child = child->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +000044 if (path_eq(path, &child->path))
Eric Biederman03acab62004-10-14 21:25:53 +000045 break;
Eric Biederman03acab62004-10-14 21:25:53 +000046 }
47 return child;
48}
Eric Biedermane9a271e32003-09-02 03:36:25 +000049
50/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000051 * Given a PCI bus and a devfn number, find the device structure.
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000052 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000053 * @param bus The bus number.
54 * @param devfn A device/function number.
55 * @return Pointer to the device structure (if found), 0 otherwise.
Eric Biederman8ca8d762003-04-22 19:02:15 +000056 */
57struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
58{
Eric Biederman83b991a2003-10-11 06:20:25 +000059 struct device *dev, *result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000060
Eric Biederman83b991a2003-10-11 06:20:25 +000061 result = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +000062 for (dev = all_devices; dev; dev = dev->next) {
Eric Biederman5cd81732004-03-11 15:01:31 +000063 if ((dev->path.type == DEVICE_PATH_PCI) &&
Uwe Hermanne4870472010-11-04 23:23:47 +000064 (dev->bus->secondary == bus) &&
65 (dev->path.pci.devfn == devfn)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000066 result = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +000067 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +000068 }
69 }
Eric Biederman83b991a2003-10-11 06:20:25 +000070 return result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000071}
72
arch import user (historical)98d0d302005-07-06 17:13:46 +000073/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000074 * Given an SMBus bus and a device number, find the device structure.
arch import user (historical)98d0d302005-07-06 17:13:46 +000075 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000076 * @param bus The bus number.
77 * @param addr A device number.
78 * @return Pointer to the device structure (if found), 0 otherwise.
arch import user (historical)98d0d302005-07-06 17:13:46 +000079 */
80struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
81{
Uwe Hermanne4870472010-11-04 23:23:47 +000082 struct device *dev, *result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000083
Uwe Hermanne4870472010-11-04 23:23:47 +000084 result = 0;
85 for (dev = all_devices; dev; dev = dev->next) {
86 if ((dev->path.type == DEVICE_PATH_I2C) &&
87 (dev->bus->secondary == bus) &&
88 (dev->path.i2c.device == addr)) {
89 result = dev;
90 break;
91 }
92 }
93 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000094}
arch import user (historical)98d0d302005-07-06 17:13:46 +000095
Uwe Hermannc1ee4292010-10-17 19:01:48 +000096/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -070097 * Given a Local APIC ID, find the device structure.
98 *
99 * @param apic_id The Local APIC ID number.
100 * @return Pointer to the device structure (if found), 0 otherwise.
101 */
102device_t dev_find_lapic(unsigned apic_id)
103{
104 device_t dev, result = NULL;
105
106 for (dev = all_devices; dev; dev = dev->next) {
107 if (dev->path.type == DEVICE_PATH_APIC &&
108 dev->path.apic.apic_id == apic_id) {
109 result = dev;
110 break;
111 }
112 }
113 return result;
114}
115
116/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000117 * Find a device of a given vendor and type.
118 *
119 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
120 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +0000121 * @param from Pointer to the device structure, used as a starting point in
122 * the linked list of all_devices, which can be 0 to start at the
123 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000124 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000125 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000126struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000127{
128 if (!from)
129 from = all_devices;
130 else
131 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000132
133 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000134 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000135
Eric Biederman8ca8d762003-04-22 19:02:15 +0000136 return from;
137}
138
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000139/**
140 * Find a device of a given class.
141 *
142 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +0000143 * @param from Pointer to the device structure, used as a starting point in
144 * the linked list of all_devices, which can be 0 to start at the
145 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000146 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000147 */
148struct device *dev_find_class(unsigned int class, struct device *from)
149{
150 if (!from)
151 from = all_devices;
152 else
153 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000154
Greg Watson59651692003-12-17 17:39:53 +0000155 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000156 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000157
Eric Biederman8ca8d762003-04-22 19:02:15 +0000158 return from;
159}
160
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000161/*
162 * Warning: This function uses a static buffer. Don't call it more than once
163 * from the same print statement!
164 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000165const char *dev_path(device_t dev)
166{
167 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000168
Eric Biedermane9a271e32003-09-02 03:36:25 +0000169 buffer[0] = '\0';
170 if (!dev) {
171 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000172 } else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000173 switch(dev->path.type) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000174 case DEVICE_PATH_ROOT:
175 memcpy(buffer, "Root Device", 12);
176 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000177 case DEVICE_PATH_PCI:
Stefan Reinauer08670622009-06-30 15:17:49 +0000178#if CONFIG_PCI_BUS_SEGN_BITS
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000179 sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
Uwe Hermanne4870472010-11-04 23:23:47 +0000180 dev->bus->secondary >> 8,
181 dev->bus->secondary & 0xff,
182 PCI_SLOT(dev->path.pci.devfn),
183 PCI_FUNC(dev->path.pci.devfn));
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000184#else
Eric Biedermane9a271e32003-09-02 03:36:25 +0000185 sprintf(buffer, "PCI: %02x:%02x.%01x",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000186 dev->bus->secondary,
Uwe Hermanne4870472010-11-04 23:23:47 +0000187 PCI_SLOT(dev->path.pci.devfn),
188 PCI_FUNC(dev->path.pci.devfn));
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000189#endif
Eric Biedermane9a271e32003-09-02 03:36:25 +0000190 break;
191 case DEVICE_PATH_PNP:
192 sprintf(buffer, "PNP: %04x.%01x",
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000193 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000194 break;
195 case DEVICE_PATH_I2C:
arch import user (historical)98d0d302005-07-06 17:13:46 +0000196 sprintf(buffer, "I2C: %02x:%02x",
197 dev->bus->secondary,
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000198 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000199 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000200 case DEVICE_PATH_APIC:
201 sprintf(buffer, "APIC: %02x",
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000202 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000203 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200204 case DEVICE_PATH_IOAPIC:
205 sprintf(buffer, "IOAPIC: %02x",
206 dev->path.ioapic.ioapic_id);
207 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800208 case DEVICE_PATH_DOMAIN:
209 sprintf(buffer, "DOMAIN: %04x",
210 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000211 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800212 case DEVICE_PATH_CPU_CLUSTER:
213 sprintf(buffer, "CPU_CLUSTER: %01x",
214 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000215 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000216 case DEVICE_PATH_CPU:
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000217 sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000218 break;
219 case DEVICE_PATH_CPU_BUS:
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000220 sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000221 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000222 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000223 printk(BIOS_ERR, "Unknown device path type: %d\n",
224 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000225 break;
226 }
227 }
228 return buffer;
229}
230
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200231const char *dev_name(device_t dev)
232{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300233 if (dev->name)
234 return dev->name;
235 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200236 return dev->chip_ops->name;
237 else
238 return "unknown";
239}
240
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000241const char *bus_path(struct bus *bus)
242{
243 static char buffer[BUS_PATH_MAX];
Myles Watson894a3472010-06-09 22:41:35 +0000244 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000245 return buffer;
246}
247
Eric Biedermane9a271e32003-09-02 03:36:25 +0000248int path_eq(struct device_path *path1, struct device_path *path2)
249{
250 int equal = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000251
252 if (path1->type != path2->type)
253 return 0;
254
255 switch (path1->type) {
256 case DEVICE_PATH_NONE:
257 break;
258 case DEVICE_PATH_ROOT:
259 equal = 1;
260 break;
261 case DEVICE_PATH_PCI:
262 equal = (path1->pci.devfn == path2->pci.devfn);
263 break;
264 case DEVICE_PATH_PNP:
265 equal = (path1->pnp.port == path2->pnp.port) &&
266 (path1->pnp.device == path2->pnp.device);
267 break;
268 case DEVICE_PATH_I2C:
269 equal = (path1->i2c.device == path2->i2c.device);
270 break;
271 case DEVICE_PATH_APIC:
272 equal = (path1->apic.apic_id == path2->apic.apic_id);
273 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800274 case DEVICE_PATH_DOMAIN:
275 equal = (path1->domain.domain == path2->domain.domain);
Uwe Hermanne4870472010-11-04 23:23:47 +0000276 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800277 case DEVICE_PATH_CPU_CLUSTER:
278 equal = (path1->cpu_cluster.cluster
279 == path2->cpu_cluster.cluster);
Uwe Hermanne4870472010-11-04 23:23:47 +0000280 break;
281 case DEVICE_PATH_CPU:
282 equal = (path1->cpu.id == path2->cpu.id);
283 break;
284 case DEVICE_PATH_CPU_BUS:
285 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
286 break;
287 default:
288 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
289 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000290 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000291
Eric Biedermane9a271e32003-09-02 03:36:25 +0000292 return equal;
293}
Eric Biederman5cd81732004-03-11 15:01:31 +0000294
295/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000296 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000297 *
298 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000299 */
300static int allocate_more_resources(void)
301{
302 int i;
303 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000304
Myles Watsonc25cc112010-05-21 14:33:48 +0000305 new_res_list = malloc(64 * sizeof(*new_res_list));
306
307 if (new_res_list == NULL)
308 return 0;
309
310 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
311
Uwe Hermanne4870472010-11-04 23:23:47 +0000312 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000313 new_res_list[i].next = &new_res_list[i+1];
314
315 free_resources = new_res_list;
316 return 1;
317}
318
319/**
320 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000321 *
322 * @param dev TODO
323 * @param res TODO
324 * @param prev TODO
325 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000326 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000327static void free_resource(device_t dev, struct resource *res,
328 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000329{
330 if (prev)
331 prev->next = res->next;
332 else
333 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000334
Myles Watsonc25cc112010-05-21 14:33:48 +0000335 res->next = free_resources;
336 free_resources = res;
337}
338
339/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000340 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000341 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000342 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000343 *
344 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000345 */
346void compact_resources(device_t dev)
347{
Myles Watsonc25cc112010-05-21 14:33:48 +0000348 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000349
Eric Biederman5cd81732004-03-11 15:01:31 +0000350 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000351 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000352 next = res->next;
353 if (!res->flags)
354 free_resource(dev, res, prev);
355 else
356 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000357 }
358}
359
360/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000361 * See if a resource structure already exists for a given index.
362 *
363 * @param dev The device to find the resource on.
364 * @param index The index of the resource on the device.
365 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000366 */
Eric Biederman03acab62004-10-14 21:25:53 +0000367struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000368{
Myles Watsonc25cc112010-05-21 14:33:48 +0000369 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000370
Eric Biederman5cd81732004-03-11 15:01:31 +0000371 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000372 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000373 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000374 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000375 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000376
Myles Watsonc25cc112010-05-21 14:33:48 +0000377 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000378}
379
380/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000381 * See if a resource structure already exists for a given index and if not
382 * allocate one.
383 *
384 * Then initialize the initialize the resource to default values.
385 *
386 * @param dev The device to find the resource on.
387 * @param index The index of the resource on the device.
388 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000389 */
390struct resource *new_resource(device_t dev, unsigned index)
391{
Myles Watsonc25cc112010-05-21 14:33:48 +0000392 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000393
Uwe Hermanne4870472010-11-04 23:23:47 +0000394 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000395 compact_resources(dev);
396
Uwe Hermanne4870472010-11-04 23:23:47 +0000397 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000398 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000399 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000400 if (free_resources == NULL && !allocate_more_resources())
401 die("Couldn't allocate more resources.");
402
403 resource = free_resources;
404 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000405 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000406 resource->next = NULL;
407 tail = dev->resource_list;
408 if (tail) {
409 while (tail->next) tail = tail->next;
410 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000411 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000412 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000413 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000414 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000415
416 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000417 if (!(resource->flags & IORESOURCE_FIXED)) {
418 resource->flags = 0;
419 resource->base = 0;
420 }
421 resource->size = 0;
422 resource->limit = 0;
423 resource->index = index;
424 resource->align = 0;
425 resource->gran = 0;
426
427 return resource;
428}
429
Eric Biederman03acab62004-10-14 21:25:53 +0000430/**
431 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000432 *
433 * @param dev The device to find the resource on.
434 * @param index The index of the resource on the device.
435 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000436 */
437struct resource *find_resource(device_t dev, unsigned index)
438{
439 struct resource *resource;
440
Uwe Hermanne4870472010-11-04 23:23:47 +0000441 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000442 resource = probe_resource(dev, index);
443 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000444 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000445 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000446 die("");
447 }
448 return resource;
449}
450
Eric Biederman03acab62004-10-14 21:25:53 +0000451/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000452 * Round a number up to the next multiple of gran.
453 *
454 * @param val The starting value.
455 * @param gran Granularity we are aligning the number to.
456 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000457 */
458static resource_t align_up(resource_t val, unsigned long gran)
459{
460 resource_t mask;
461 mask = (1ULL << gran) - 1ULL;
462 val += mask;
463 val &= ~mask;
464 return val;
465}
466
467/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000468 * Round a number up to the previous multiple of gran.
469 *
470 * @param val The starting value.
471 * @param gran Granularity we are aligning the number to.
472 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000473 */
474static resource_t align_down(resource_t val, unsigned long gran)
475{
476 resource_t mask;
477 mask = (1ULL << gran) - 1ULL;
478 val &= ~mask;
479 return val;
480}
481
482/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000483 * Compute the maximum address that is part of a resource.
484 *
485 * @param resource The resource whose limit is desired.
486 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000487 */
488resource_t resource_end(struct resource *resource)
489{
490 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000491
492 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000493 base = resource->base;
494
Uwe Hermanne4870472010-11-04 23:23:47 +0000495 /*
496 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000497 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000498 * the bridge. While the granularity is simply how many low bits of
499 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000500 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000501
Uwe Hermanne4870472010-11-04 23:23:47 +0000502 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000503 end = base + align_up(resource->size, resource->gran) - 1;
504
505 return end;
506}
507
508/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000509 * Compute the maximum legal value for resource->base.
510 *
511 * @param resource The resource whose maximum is desired.
512 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000513 */
514resource_t resource_max(struct resource *resource)
515{
516 resource_t max;
517
518 max = align_down(resource->limit - resource->size + 1, resource->align);
519
520 return max;
521}
522
523/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000524 * Return the resource type of a resource.
525 *
526 * @param resource The resource type to decode.
527 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000528 */
529const char *resource_type(struct resource *resource)
530{
531 static char buffer[RESOURCE_TYPE_MAX];
532 sprintf(buffer, "%s%s%s%s",
Uwe Hermanne4870472010-11-04 23:23:47 +0000533 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
534 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
535 ((resource->flags == 0) ? "unused" :
536 (resource->flags & IORESOURCE_IO) ? "io" :
537 (resource->flags & IORESOURCE_DRQ) ? "drq" :
538 (resource->flags & IORESOURCE_IRQ) ? "irq" :
539 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
540 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000541 return buffer;
542}
543
544/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000545 * Print the resource that was just stored.
546 *
547 * @param dev The device the stored resorce lives on.
548 * @param resource The resource that was just stored.
549 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000550 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000551void report_resource_stored(device_t dev, struct resource *resource,
552 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000553{
Uwe Hermanne4870472010-11-04 23:23:47 +0000554 char buf[10];
555 unsigned long long base, end;
556
557 if (!(resource->flags & IORESOURCE_STORED))
558 return;
559
560 base = resource->base;
561 end = resource_end(resource);
562 buf[0] = '\0';
563
564 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000565#if CONFIG_PCI_BUS_SEGN_BITS
Uwe Hermanne4870472010-11-04 23:23:47 +0000566 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
567 dev->link_list->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000568#else
Uwe Hermanne4870472010-11-04 23:23:47 +0000569 sprintf(buf, "bus %02x ", dev->link_list->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000570#endif
Eric Biederman03acab62004-10-14 21:25:53 +0000571 }
Patrick Georgi51615092012-03-11 19:31:03 +0100572 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000573 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
574 base, end, resource->size, resource->gran, buf,
575 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000576}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000577
Uwe Hermanne4870472010-11-04 23:23:47 +0000578void search_bus_resources(struct bus *bus, unsigned long type_mask,
579 unsigned long type, resource_search_t search,
580 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000581{
582 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000583
Myles Watson894a3472010-06-09 22:41:35 +0000584 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000585 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000586
587 /* Ignore disabled devices. */
588 if (!curdev->enabled)
589 continue;
590
Myles Watson894a3472010-06-09 22:41:35 +0000591 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000592 /* If it isn't the right kind of resource ignore it. */
593 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000594 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000595
596 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000597 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000598 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000599 for (subbus = curdev->link_list; subbus;
600 subbus = subbus->next)
601 if (subbus->link_num
602 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000603 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700604 if (!subbus) /* Why can subbus be NULL? */
605 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000606 search_bus_resources(subbus, type_mask, type,
607 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000608 continue;
609 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000610 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000611 }
612 }
613}
614
Uwe Hermanne4870472010-11-04 23:23:47 +0000615void search_global_resources(unsigned long type_mask, unsigned long type,
616 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000617{
618 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000619
Myles Watson894a3472010-06-09 22:41:35 +0000620 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000621 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000622
623 /* Ignore disabled devices. */
624 if (!curdev->enabled)
625 continue;
626
Myles Watson894a3472010-06-09 22:41:35 +0000627 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000628 /* If it isn't the right kind of resource ignore it. */
629 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000630 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000631
632 /* If it is a subtractive resource ignore it. */
633 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000634 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000635
Myles Watsonc25cc112010-05-21 14:33:48 +0000636 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000637 }
638 }
639}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000640
641void dev_set_enabled(device_t dev, int enable)
642{
Uwe Hermanne4870472010-11-04 23:23:47 +0000643 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000644 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000645
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000646 dev->enabled = enable;
647 if (dev->ops && dev->ops->enable) {
648 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000649 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000650 dev->chip_ops->enable_dev(dev);
651 }
652}
653
654void disable_children(struct bus *bus)
655{
656 device_t child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000657
Myles Watson894a3472010-06-09 22:41:35 +0000658 for (child = bus->children; child; child = child->sibling) {
659 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000660 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000661 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000662 dev_set_enabled(child, 0);
663 }
664}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000665
Maciej Pijankaea921852009-10-27 14:29:29 +0000666static void resource_tree(struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000667{
Myles Watson894a3472010-06-09 22:41:35 +0000668 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000669 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000670 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000671 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000672 char indent[30]; /* If your tree has more levels, it's wrong. */
673
674 for (i = 0; i < depth + 1 && i < 29; i++)
675 indent[i] = ' ';
676 indent[i] = '\0';
677
Myles Watson894a3472010-06-09 22:41:35 +0000678 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
679 if (root->link_list && root->link_list->children)
680 do_printk(BIOS_DEBUG, " child on link 0 %s",
681 dev_path(root->link_list->children));
682 do_printk(BIOS_DEBUG, "\n");
683
Myles Watsonc25cc112010-05-21 14:33:48 +0000684 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000685 do_printk(debug_level, "%s%s resource base %llx size %llx "
686 "align %d gran %d limit %llx flags %lx index %lx\n",
687 indent, dev_path(root), res->base, res->size,
688 res->align, res->gran, res->limit, res->flags,
689 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000690 }
691
Myles Watson894a3472010-06-09 22:41:35 +0000692 for (link = root->link_list; link; link = link->next) {
693 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000694 resource_tree(child, debug_level, depth + 1);
695 }
696}
697
Uwe Hermanne4870472010-11-04 23:23:47 +0000698void print_resource_tree(struct device *root, int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000699{
700 /* Bail if root is null. */
701 if (!root) {
702 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
703 return;
704 }
705
706 /* Bail if not printing to screen. */
707 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000708 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000709 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000710
Myles Watsonbb3d8122009-05-11 22:45:35 +0000711 resource_tree(root, debug_level, 0);
712}
713
714void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
715{
716 char depth_str[20] = "";
717 int i;
718 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000719 struct bus *link;
720
Myles Watsonbb3d8122009-05-11 22:45:35 +0000721 for (i = 0; i < depth; i++)
722 depth_str[i] = ' ';
723 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000724
Myles Watsonc25cc112010-05-21 14:33:48 +0000725 do_printk(debug_level, "%s%s: enabled %d\n",
726 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000727
Myles Watson894a3472010-06-09 22:41:35 +0000728 for (link = dev->link_list; link; link = link->next) {
729 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000730 sibling = sibling->sibling)
731 show_devs_tree(sibling, debug_level, depth + 1, i);
732 }
733}
734
735void show_all_devs_tree(int debug_level, const char *msg)
736{
737 /* Bail if not printing to screen. */
738 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
739 return;
740 show_devs_tree(all_devices, debug_level, 0, -1);
741}
742
743void show_devs_subtree(struct device *root, int debug_level, const char *msg)
744{
745 /* Bail if not printing to screen. */
746 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000747 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000748 return;
749 do_printk(debug_level, "%s\n", msg);
750 show_devs_tree(root, debug_level, 0, -1);
751}
752
753void show_all_devs(int debug_level, const char *msg)
754{
755 struct device *dev;
756
757 /* Bail if not printing to screen. */
758 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
759 return;
760 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000761 do_printk(debug_level, "%s: enabled %d\n",
762 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000763 }
764}
765
766void show_one_resource(int debug_level, struct device *dev,
767 struct resource *resource, const char *comment)
768{
769 char buf[10];
770 unsigned long long base, end;
771 base = resource->base;
772 end = resource_end(resource);
773 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000774
Myles Watsonbb3d8122009-05-11 22:45:35 +0000775/*
776 if (resource->flags & IORESOURCE_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000777#if CONFIG_PCI_BUS_SEGN_BITS
Myles Watsonbb3d8122009-05-11 22:45:35 +0000778 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
779 dev->link[0].secondary & 0xff);
780#else
781 sprintf(buf, "bus %02x ", dev->link[0].secondary);
782#endif
783 }
784*/
Myles Watsonbb3d8122009-05-11 22:45:35 +0000785
Uwe Hermanne4870472010-11-04 23:23:47 +0000786 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100787 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000788 resource->index, base, end, resource->size, resource->gran,
789 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000790}
791
792void show_all_devs_resources(int debug_level, const char* msg)
793{
794 struct device *dev;
795
Uwe Hermanne4870472010-11-04 23:23:47 +0000796 if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000797 return;
798
799 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000800 struct resource *res;
801 do_printk(debug_level, "%s: enabled %d\n",
802 dev_path(dev), dev->enabled);
803 for (res = dev->resource_list; res; res = res->next)
804 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000805 }
806}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000807
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300808void fixed_mem_resource(device_t dev, unsigned long index,
809 unsigned long basek, unsigned long sizek, unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000810{
811 struct resource *resource;
812
813 if (!sizek)
814 return;
815
816 resource = new_resource(dev, index);
817 resource->base = ((resource_t)basek) << 10;
818 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300819 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
820 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000821
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300822 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300823}
824
Uwe Hermann4b42a622010-10-11 19:36:13 +0000825void tolm_test(void *gp, struct device *dev, struct resource *new)
826{
827 struct resource **best_p = gp;
828 struct resource *best;
829
830 best = *best_p;
831
832 if (!best || (best->base > new->base))
833 best = new;
834
835 *best_p = best;
836}
837
838u32 find_pci_tolm(struct bus *bus)
839{
840 struct resource *min = NULL;
841 u32 tolm;
842
843 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
844 tolm_test, &min);
845
846 tolm = 0xffffffffUL;
847
848 if (min && tolm > min->base)
849 tolm = min->base;
850
851 return tolm;
852}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700853
854/* Count of enabled CPUs */
855int dev_count_cpu(void)
856{
857 device_t cpu;
858 int count = 0;
859
860 for (cpu = all_devices; cpu; cpu = cpu->next) {
861 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800862 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700863 continue;
864 if (!cpu->enabled)
865 continue;
866 count++;
867 }
868
869 return count;
870}