blob: 81d679a20656306e8d80922790d2c4345e013549 [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
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
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;
Eric Biederman7003ba42004-10-16 06:20:29 +0000208 case DEVICE_PATH_PCI_DOMAIN:
209 sprintf(buffer, "PCI_DOMAIN: %04x",
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000210 dev->path.pci_domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000211 break;
212 case DEVICE_PATH_APIC_CLUSTER:
213 sprintf(buffer, "APIC_CLUSTER: %01x",
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000214 dev->path.apic_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
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000231const char *bus_path(struct bus *bus)
232{
233 static char buffer[BUS_PATH_MAX];
Myles Watson894a3472010-06-09 22:41:35 +0000234 sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000235 return buffer;
236}
237
Eric Biedermane9a271e32003-09-02 03:36:25 +0000238int path_eq(struct device_path *path1, struct device_path *path2)
239{
240 int equal = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000241
242 if (path1->type != path2->type)
243 return 0;
244
245 switch (path1->type) {
246 case DEVICE_PATH_NONE:
247 break;
248 case DEVICE_PATH_ROOT:
249 equal = 1;
250 break;
251 case DEVICE_PATH_PCI:
252 equal = (path1->pci.devfn == path2->pci.devfn);
253 break;
254 case DEVICE_PATH_PNP:
255 equal = (path1->pnp.port == path2->pnp.port) &&
256 (path1->pnp.device == path2->pnp.device);
257 break;
258 case DEVICE_PATH_I2C:
259 equal = (path1->i2c.device == path2->i2c.device);
260 break;
261 case DEVICE_PATH_APIC:
262 equal = (path1->apic.apic_id == path2->apic.apic_id);
263 break;
264 case DEVICE_PATH_PCI_DOMAIN:
265 equal = (path1->pci_domain.domain == path2->pci_domain.domain);
266 break;
267 case DEVICE_PATH_APIC_CLUSTER:
268 equal = (path1->apic_cluster.cluster
269 == path2->apic_cluster.cluster);
270 break;
271 case DEVICE_PATH_CPU:
272 equal = (path1->cpu.id == path2->cpu.id);
273 break;
274 case DEVICE_PATH_CPU_BUS:
275 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
276 break;
277 default:
278 printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
279 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000280 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000281
Eric Biedermane9a271e32003-09-02 03:36:25 +0000282 return equal;
283}
Eric Biederman5cd81732004-03-11 15:01:31 +0000284
285/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000286 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000287 *
288 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000289 */
290static int allocate_more_resources(void)
291{
292 int i;
293 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000294
Myles Watsonc25cc112010-05-21 14:33:48 +0000295 new_res_list = malloc(64 * sizeof(*new_res_list));
296
297 if (new_res_list == NULL)
298 return 0;
299
300 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
301
Uwe Hermanne4870472010-11-04 23:23:47 +0000302 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000303 new_res_list[i].next = &new_res_list[i+1];
304
305 free_resources = new_res_list;
306 return 1;
307}
308
309/**
310 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000311 *
312 * @param dev TODO
313 * @param res TODO
314 * @param prev TODO
315 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000316 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000317static void free_resource(device_t dev, struct resource *res,
318 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000319{
320 if (prev)
321 prev->next = res->next;
322 else
323 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000324
Myles Watsonc25cc112010-05-21 14:33:48 +0000325 res->next = free_resources;
326 free_resources = res;
327}
328
329/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000330 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000331 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000332 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000333 *
334 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000335 */
336void compact_resources(device_t dev)
337{
Myles Watsonc25cc112010-05-21 14:33:48 +0000338 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000339
Eric Biederman5cd81732004-03-11 15:01:31 +0000340 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000341 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000342 next = res->next;
343 if (!res->flags)
344 free_resource(dev, res, prev);
345 else
346 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000347 }
348}
349
350/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000351 * See if a resource structure already exists for a given index.
352 *
353 * @param dev The device to find the resource on.
354 * @param index The index of the resource on the device.
355 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000356 */
Eric Biederman03acab62004-10-14 21:25:53 +0000357struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000358{
Myles Watsonc25cc112010-05-21 14:33:48 +0000359 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000360
Eric Biederman5cd81732004-03-11 15:01:31 +0000361 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000362 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000363 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000364 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000365 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000366
Myles Watsonc25cc112010-05-21 14:33:48 +0000367 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000368}
369
370/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000371 * See if a resource structure already exists for a given index and if not
372 * allocate one.
373 *
374 * Then initialize the initialize the resource to default values.
375 *
376 * @param dev The device to find the resource on.
377 * @param index The index of the resource on the device.
378 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000379 */
380struct resource *new_resource(device_t dev, unsigned index)
381{
Myles Watsonc25cc112010-05-21 14:33:48 +0000382 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000383
Uwe Hermanne4870472010-11-04 23:23:47 +0000384 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000385 compact_resources(dev);
386
Uwe Hermanne4870472010-11-04 23:23:47 +0000387 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000388 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000389 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000390 if (free_resources == NULL && !allocate_more_resources())
391 die("Couldn't allocate more resources.");
392
393 resource = free_resources;
394 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000395 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000396 resource->next = NULL;
397 tail = dev->resource_list;
398 if (tail) {
399 while (tail->next) tail = tail->next;
400 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000401 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000402 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000403 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000404 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000405
406 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000407 if (!(resource->flags & IORESOURCE_FIXED)) {
408 resource->flags = 0;
409 resource->base = 0;
410 }
411 resource->size = 0;
412 resource->limit = 0;
413 resource->index = index;
414 resource->align = 0;
415 resource->gran = 0;
416
417 return resource;
418}
419
Eric Biederman03acab62004-10-14 21:25:53 +0000420/**
421 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000422 *
423 * @param dev The device to find the resource on.
424 * @param index The index of the resource on the device.
425 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000426 */
427struct resource *find_resource(device_t dev, unsigned index)
428{
429 struct resource *resource;
430
Uwe Hermanne4870472010-11-04 23:23:47 +0000431 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000432 resource = probe_resource(dev, index);
433 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000434 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000435 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000436 die("");
437 }
438 return resource;
439}
440
Eric Biederman03acab62004-10-14 21:25:53 +0000441/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000442 * Round a number up to the next multiple of gran.
443 *
444 * @param val The starting value.
445 * @param gran Granularity we are aligning the number to.
446 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000447 */
448static resource_t align_up(resource_t val, unsigned long gran)
449{
450 resource_t mask;
451 mask = (1ULL << gran) - 1ULL;
452 val += mask;
453 val &= ~mask;
454 return val;
455}
456
457/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000458 * Round a number up to the previous multiple of gran.
459 *
460 * @param val The starting value.
461 * @param gran Granularity we are aligning the number to.
462 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000463 */
464static resource_t align_down(resource_t val, unsigned long gran)
465{
466 resource_t mask;
467 mask = (1ULL << gran) - 1ULL;
468 val &= ~mask;
469 return val;
470}
471
472/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000473 * Compute the maximum address that is part of a resource.
474 *
475 * @param resource The resource whose limit is desired.
476 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000477 */
478resource_t resource_end(struct resource *resource)
479{
480 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000481
482 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000483 base = resource->base;
484
Uwe Hermanne4870472010-11-04 23:23:47 +0000485 /*
486 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000487 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000488 * the bridge. While the granularity is simply how many low bits of
489 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000490 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000491
Uwe Hermanne4870472010-11-04 23:23:47 +0000492 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000493 end = base + align_up(resource->size, resource->gran) - 1;
494
495 return end;
496}
497
498/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000499 * Compute the maximum legal value for resource->base.
500 *
501 * @param resource The resource whose maximum is desired.
502 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000503 */
504resource_t resource_max(struct resource *resource)
505{
506 resource_t max;
507
508 max = align_down(resource->limit - resource->size + 1, resource->align);
509
510 return max;
511}
512
513/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000514 * Return the resource type of a resource.
515 *
516 * @param resource The resource type to decode.
517 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000518 */
519const char *resource_type(struct resource *resource)
520{
521 static char buffer[RESOURCE_TYPE_MAX];
522 sprintf(buffer, "%s%s%s%s",
Uwe Hermanne4870472010-11-04 23:23:47 +0000523 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
524 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
525 ((resource->flags == 0) ? "unused" :
526 (resource->flags & IORESOURCE_IO) ? "io" :
527 (resource->flags & IORESOURCE_DRQ) ? "drq" :
528 (resource->flags & IORESOURCE_IRQ) ? "irq" :
529 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
530 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000531 return buffer;
532}
533
534/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000535 * Print the resource that was just stored.
536 *
537 * @param dev The device the stored resorce lives on.
538 * @param resource The resource that was just stored.
539 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000540 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000541void report_resource_stored(device_t dev, struct resource *resource,
542 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000543{
Uwe Hermanne4870472010-11-04 23:23:47 +0000544 char buf[10];
545 unsigned long long base, end;
546
547 if (!(resource->flags & IORESOURCE_STORED))
548 return;
549
550 base = resource->base;
551 end = resource_end(resource);
552 buf[0] = '\0';
553
554 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000555#if CONFIG_PCI_BUS_SEGN_BITS
Uwe Hermanne4870472010-11-04 23:23:47 +0000556 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
557 dev->link_list->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000558#else
Uwe Hermanne4870472010-11-04 23:23:47 +0000559 sprintf(buf, "bus %02x ", dev->link_list->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000560#endif
Eric Biederman03acab62004-10-14 21:25:53 +0000561 }
Patrick Georgi51615092012-03-11 19:31:03 +0100562 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000563 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
564 base, end, resource->size, resource->gran, buf,
565 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000566}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000567
Uwe Hermanne4870472010-11-04 23:23:47 +0000568void search_bus_resources(struct bus *bus, unsigned long type_mask,
569 unsigned long type, resource_search_t search,
570 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 = bus->children; curdev; curdev = curdev->sibling) {
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 recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000587 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000588 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000589 for (subbus = curdev->link_list; subbus;
590 subbus = subbus->next)
591 if (subbus->link_num
592 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000593 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700594 if (!subbus) /* Why can subbus be NULL? */
595 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000596 search_bus_resources(subbus, type_mask, type,
597 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000598 continue;
599 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000600 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000601 }
602 }
603}
604
Uwe Hermanne4870472010-11-04 23:23:47 +0000605void search_global_resources(unsigned long type_mask, unsigned long type,
606 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000607{
608 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000609
Myles Watson894a3472010-06-09 22:41:35 +0000610 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000611 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000612
613 /* Ignore disabled devices. */
614 if (!curdev->enabled)
615 continue;
616
Myles Watson894a3472010-06-09 22:41:35 +0000617 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000618 /* If it isn't the right kind of resource ignore it. */
619 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000620 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000621
622 /* If it is a subtractive resource ignore it. */
623 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000624 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000625
Myles Watsonc25cc112010-05-21 14:33:48 +0000626 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000627 }
628 }
629}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000630
631void dev_set_enabled(device_t dev, int enable)
632{
Uwe Hermanne4870472010-11-04 23:23:47 +0000633 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000634 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000635
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000636 dev->enabled = enable;
637 if (dev->ops && dev->ops->enable) {
638 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000639 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000640 dev->chip_ops->enable_dev(dev);
641 }
642}
643
644void disable_children(struct bus *bus)
645{
646 device_t child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000647
Myles Watson894a3472010-06-09 22:41:35 +0000648 for (child = bus->children; child; child = child->sibling) {
649 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000650 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000651 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000652 dev_set_enabled(child, 0);
653 }
654}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000655
Maciej Pijankaea921852009-10-27 14:29:29 +0000656static void resource_tree(struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000657{
Myles Watson894a3472010-06-09 22:41:35 +0000658 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000659 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000660 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000661 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000662 char indent[30]; /* If your tree has more levels, it's wrong. */
663
664 for (i = 0; i < depth + 1 && i < 29; i++)
665 indent[i] = ' ';
666 indent[i] = '\0';
667
Myles Watson894a3472010-06-09 22:41:35 +0000668 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
669 if (root->link_list && root->link_list->children)
670 do_printk(BIOS_DEBUG, " child on link 0 %s",
671 dev_path(root->link_list->children));
672 do_printk(BIOS_DEBUG, "\n");
673
Myles Watsonc25cc112010-05-21 14:33:48 +0000674 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000675 do_printk(debug_level, "%s%s resource base %llx size %llx "
676 "align %d gran %d limit %llx flags %lx index %lx\n",
677 indent, dev_path(root), res->base, res->size,
678 res->align, res->gran, res->limit, res->flags,
679 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000680 }
681
Myles Watson894a3472010-06-09 22:41:35 +0000682 for (link = root->link_list; link; link = link->next) {
683 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000684 resource_tree(child, debug_level, depth + 1);
685 }
686}
687
Uwe Hermanne4870472010-11-04 23:23:47 +0000688void print_resource_tree(struct device *root, int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000689{
690 /* Bail if root is null. */
691 if (!root) {
692 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
693 return;
694 }
695
696 /* Bail if not printing to screen. */
697 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000698 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000699 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000700
Myles Watsonbb3d8122009-05-11 22:45:35 +0000701 resource_tree(root, debug_level, 0);
702}
703
704void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
705{
706 char depth_str[20] = "";
707 int i;
708 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000709 struct bus *link;
710
Myles Watsonbb3d8122009-05-11 22:45:35 +0000711 for (i = 0; i < depth; i++)
712 depth_str[i] = ' ';
713 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000714
Myles Watsonc25cc112010-05-21 14:33:48 +0000715 do_printk(debug_level, "%s%s: enabled %d\n",
716 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000717
Myles Watson894a3472010-06-09 22:41:35 +0000718 for (link = dev->link_list; link; link = link->next) {
719 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000720 sibling = sibling->sibling)
721 show_devs_tree(sibling, debug_level, depth + 1, i);
722 }
723}
724
725void show_all_devs_tree(int debug_level, const char *msg)
726{
727 /* Bail if not printing to screen. */
728 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
729 return;
730 show_devs_tree(all_devices, debug_level, 0, -1);
731}
732
733void show_devs_subtree(struct device *root, int debug_level, const char *msg)
734{
735 /* Bail if not printing to screen. */
736 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000737 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000738 return;
739 do_printk(debug_level, "%s\n", msg);
740 show_devs_tree(root, debug_level, 0, -1);
741}
742
743void show_all_devs(int debug_level, const char *msg)
744{
745 struct device *dev;
746
747 /* Bail if not printing to screen. */
748 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
749 return;
750 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000751 do_printk(debug_level, "%s: enabled %d\n",
752 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000753 }
754}
755
756void show_one_resource(int debug_level, struct device *dev,
757 struct resource *resource, const char *comment)
758{
759 char buf[10];
760 unsigned long long base, end;
761 base = resource->base;
762 end = resource_end(resource);
763 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000764
Myles Watsonbb3d8122009-05-11 22:45:35 +0000765/*
766 if (resource->flags & IORESOURCE_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000767#if CONFIG_PCI_BUS_SEGN_BITS
Myles Watsonbb3d8122009-05-11 22:45:35 +0000768 sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
769 dev->link[0].secondary & 0xff);
770#else
771 sprintf(buf, "bus %02x ", dev->link[0].secondary);
772#endif
773 }
774*/
Myles Watsonbb3d8122009-05-11 22:45:35 +0000775
Uwe Hermanne4870472010-11-04 23:23:47 +0000776 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100777 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000778 resource->index, base, end, resource->size, resource->gran,
779 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000780}
781
782void show_all_devs_resources(int debug_level, const char* msg)
783{
784 struct device *dev;
785
Uwe Hermanne4870472010-11-04 23:23:47 +0000786 if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000787 return;
788
789 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000790 struct resource *res;
791 do_printk(debug_level, "%s: enabled %d\n",
792 dev_path(dev), dev->enabled);
793 for (res = dev->resource_list; res; res = res->next)
794 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000795 }
796}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000797
798void ram_resource(device_t dev, unsigned long index,
799 unsigned long basek, unsigned long sizek)
800{
801 struct resource *resource;
802
803 if (!sizek)
804 return;
805
806 resource = new_resource(dev, index);
807 resource->base = ((resource_t)basek) << 10;
808 resource->size = ((resource_t)sizek) << 10;
809 resource->flags = IORESOURCE_MEM | IORESOURCE_CACHEABLE | \
810 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
811}
812
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300813void uma_resource(device_t dev, unsigned long index,
814 unsigned long basek, unsigned long sizek)
815{
816 struct resource *resource;
817
818 if (!sizek)
819 return;
820
821 resource = new_resource(dev, index);
822 resource->base = ((resource_t)basek) << 10;
823 resource->size = ((resource_t)sizek) << 10;
824 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
825 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
826}
827
Uwe Hermann4b42a622010-10-11 19:36:13 +0000828void tolm_test(void *gp, struct device *dev, struct resource *new)
829{
830 struct resource **best_p = gp;
831 struct resource *best;
832
833 best = *best_p;
834
835 if (!best || (best->base > new->base))
836 best = new;
837
838 *best_p = best;
839}
840
841u32 find_pci_tolm(struct bus *bus)
842{
843 struct resource *min = NULL;
844 u32 tolm;
845
846 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
847 tolm_test, &min);
848
849 tolm = 0xffffffffUL;
850
851 if (min && tolm > min->base)
852 tolm = min->base;
853
854 return tolm;
855}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700856
857/* Count of enabled CPUs */
858int dev_count_cpu(void)
859{
860 device_t cpu;
861 int count = 0;
862
863 for (cpu = all_devices; cpu; cpu = cpu->next) {
864 if ((cpu->path.type != DEVICE_PATH_APIC) ||
865 (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
866 continue;
867 if (!cpu->enabled)
868 continue;
869 count++;
870 }
871
872 return count;
873}