blob: d5466cb011f83e57ed458b31bf8d7fc78b73746a [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>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020028#include <device/pci_def.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/**
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +010097 * Given a PnP port and a device number, find the device structure.
98 *
99 * @param port The I/O port.
100 * @param device Logical device number.
101 * @return Pointer to the device structure (if found), 0 otherwise.
102 */
Vladimir Serbinenkob33384a2014-02-08 18:58:39 +0100103struct device *dev_find_slot_pnp(u16 port, u16 device)
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +0100104{
105 struct device *dev;
106
107 for (dev = all_devices; dev; dev = dev->next) {
108 if ((dev->path.type == DEVICE_PATH_PNP) &&
109 (dev->path.pnp.port == port) &&
110 (dev->path.pnp.device == device)) {
111 return dev;
112 }
113 }
114 return 0;
115}
116
117/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -0700118 * Given a Local APIC ID, find the device structure.
119 *
120 * @param apic_id The Local APIC ID number.
121 * @return Pointer to the device structure (if found), 0 otherwise.
122 */
123device_t dev_find_lapic(unsigned apic_id)
124{
125 device_t dev, result = NULL;
126
127 for (dev = all_devices; dev; dev = dev->next) {
128 if (dev->path.type == DEVICE_PATH_APIC &&
129 dev->path.apic.apic_id == apic_id) {
130 result = dev;
131 break;
132 }
133 }
134 return result;
135}
136
137/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000138 * Find a device of a given vendor and type.
139 *
140 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
141 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +0000142 * @param from Pointer to the device structure, used as a starting point in
143 * the linked list of all_devices, which can be 0 to start at the
144 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000145 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000146 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000147struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000148{
149 if (!from)
150 from = all_devices;
151 else
152 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000153
154 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000155 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000156
Eric Biederman8ca8d762003-04-22 19:02:15 +0000157 return from;
158}
159
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000160/**
161 * Find a device of a given class.
162 *
163 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +0000164 * @param from Pointer to the device structure, used as a starting point in
165 * the linked list of all_devices, which can be 0 to start at the
166 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000167 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000168 */
169struct device *dev_find_class(unsigned int class, struct device *from)
170{
171 if (!from)
172 from = all_devices;
173 else
174 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000175
Greg Watson59651692003-12-17 17:39:53 +0000176 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000177 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000178
Eric Biederman8ca8d762003-04-22 19:02:15 +0000179 return from;
180}
181
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700182/**
183 * Encode the device path into 3 bytes for logging to CMOS.
184 *
185 * @param dev The device path to encode.
186 * @return Device path encoded into lower 3 bytes of dword.
187 */
188u32 dev_path_encode(device_t dev)
189{
190 u32 ret;
191
192 if (!dev)
193 return 0;
194
195 /* Store the device type in 3rd byte. */
196 ret = dev->path.type << 16;
197
198 /* Encode the device specific path in the low word. */
199 switch (dev->path.type) {
200 case DEVICE_PATH_ROOT:
201 break;
202 case DEVICE_PATH_PCI:
203 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
204 break;
205 case DEVICE_PATH_PNP:
206 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
207 break;
208 case DEVICE_PATH_I2C:
209 ret |= dev->bus->secondary << 8 | dev->path.pnp.device;
210 break;
211 case DEVICE_PATH_APIC:
212 ret |= dev->path.apic.apic_id;
213 break;
214 case DEVICE_PATH_DOMAIN:
215 ret |= dev->path.domain.domain;
216 break;
217 case DEVICE_PATH_CPU_CLUSTER:
218 ret |= dev->path.cpu_cluster.cluster;
219 break;
220 case DEVICE_PATH_CPU:
221 ret |= dev->path.cpu.id;
222 break;
223 case DEVICE_PATH_CPU_BUS:
224 ret |= dev->path.cpu_bus.id;
225 break;
226 case DEVICE_PATH_IOAPIC:
227 ret |= dev->path.ioapic.ioapic_id;
228 break;
229 case DEVICE_PATH_NONE:
230 default:
231 break;
232 }
233
234 return ret;
235}
236
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000237/*
238 * Warning: This function uses a static buffer. Don't call it more than once
239 * from the same print statement!
240 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000241const char *dev_path(device_t dev)
242{
243 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000244
Eric Biedermane9a271e32003-09-02 03:36:25 +0000245 buffer[0] = '\0';
246 if (!dev) {
247 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000248 } else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000249 switch(dev->path.type) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000250 case DEVICE_PATH_ROOT:
251 memcpy(buffer, "Root Device", 12);
252 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000253 case DEVICE_PATH_PCI:
Stefan Reinauer08670622009-06-30 15:17:49 +0000254#if CONFIG_PCI_BUS_SEGN_BITS
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100255 snprintf(buffer, sizeof (buffer),
256 "PCI: %04x:%02x:%02x.%01x",
257 dev->bus->secondary >> 8,
258 dev->bus->secondary & 0xff,
259 PCI_SLOT(dev->path.pci.devfn),
260 PCI_FUNC(dev->path.pci.devfn));
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000261#else
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100262 snprintf(buffer, sizeof (buffer),
263 "PCI: %02x:%02x.%01x",
264 dev->bus->secondary,
265 PCI_SLOT(dev->path.pci.devfn),
266 PCI_FUNC(dev->path.pci.devfn));
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000267#endif
Eric Biedermane9a271e32003-09-02 03:36:25 +0000268 break;
269 case DEVICE_PATH_PNP:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100270 snprintf(buffer, sizeof (buffer), "PNP: %04x.%01x",
271 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000272 break;
273 case DEVICE_PATH_I2C:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100274 snprintf(buffer, sizeof (buffer), "I2C: %02x:%02x",
275 dev->bus->secondary,
276 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000277 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000278 case DEVICE_PATH_APIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100279 snprintf(buffer, sizeof (buffer), "APIC: %02x",
280 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000281 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200282 case DEVICE_PATH_IOAPIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100283 snprintf(buffer, sizeof (buffer), "IOAPIC: %02x",
284 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200285 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800286 case DEVICE_PATH_DOMAIN:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100287 snprintf(buffer, sizeof (buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800288 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000289 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800290 case DEVICE_PATH_CPU_CLUSTER:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100291 snprintf(buffer, sizeof (buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800292 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000293 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000294 case DEVICE_PATH_CPU:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100295 snprintf(buffer, sizeof (buffer),
296 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000297 break;
298 case DEVICE_PATH_CPU_BUS:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100299 snprintf(buffer, sizeof (buffer),
300 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000301 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000302 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000303 printk(BIOS_ERR, "Unknown device path type: %d\n",
304 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000305 break;
306 }
307 }
308 return buffer;
309}
310
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200311const char *dev_name(device_t dev)
312{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300313 if (dev->name)
314 return dev->name;
315 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200316 return dev->chip_ops->name;
317 else
318 return "unknown";
319}
320
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000321const char *bus_path(struct bus *bus)
322{
323 static char buffer[BUS_PATH_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100324 snprintf(buffer, sizeof (buffer),
325 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000326 return buffer;
327}
328
Eric Biedermane9a271e32003-09-02 03:36:25 +0000329int path_eq(struct device_path *path1, struct device_path *path2)
330{
331 int equal = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000332
333 if (path1->type != path2->type)
334 return 0;
335
336 switch (path1->type) {
337 case DEVICE_PATH_NONE:
338 break;
339 case DEVICE_PATH_ROOT:
340 equal = 1;
341 break;
342 case DEVICE_PATH_PCI:
343 equal = (path1->pci.devfn == path2->pci.devfn);
344 break;
345 case DEVICE_PATH_PNP:
346 equal = (path1->pnp.port == path2->pnp.port) &&
347 (path1->pnp.device == path2->pnp.device);
348 break;
349 case DEVICE_PATH_I2C:
350 equal = (path1->i2c.device == path2->i2c.device);
351 break;
352 case DEVICE_PATH_APIC:
353 equal = (path1->apic.apic_id == path2->apic.apic_id);
354 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800355 case DEVICE_PATH_DOMAIN:
356 equal = (path1->domain.domain == path2->domain.domain);
Uwe Hermanne4870472010-11-04 23:23:47 +0000357 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800358 case DEVICE_PATH_CPU_CLUSTER:
359 equal = (path1->cpu_cluster.cluster
360 == path2->cpu_cluster.cluster);
Uwe Hermanne4870472010-11-04 23:23:47 +0000361 break;
362 case DEVICE_PATH_CPU:
363 equal = (path1->cpu.id == path2->cpu.id);
364 break;
365 case DEVICE_PATH_CPU_BUS:
366 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
367 break;
368 default:
Martin Roth63373ed2013-07-08 16:24:19 -0600369 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
Uwe Hermanne4870472010-11-04 23:23:47 +0000370 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000371 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000372
Eric Biedermane9a271e32003-09-02 03:36:25 +0000373 return equal;
374}
Eric Biederman5cd81732004-03-11 15:01:31 +0000375
376/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000377 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000378 *
379 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000380 */
381static int allocate_more_resources(void)
382{
383 int i;
384 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000385
Myles Watsonc25cc112010-05-21 14:33:48 +0000386 new_res_list = malloc(64 * sizeof(*new_res_list));
387
388 if (new_res_list == NULL)
389 return 0;
390
391 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
392
Uwe Hermanne4870472010-11-04 23:23:47 +0000393 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000394 new_res_list[i].next = &new_res_list[i+1];
395
396 free_resources = new_res_list;
397 return 1;
398}
399
400/**
401 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000402 *
403 * @param dev TODO
404 * @param res TODO
405 * @param prev TODO
406 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000407 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000408static void free_resource(device_t dev, struct resource *res,
409 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000410{
411 if (prev)
412 prev->next = res->next;
413 else
414 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000415
Myles Watsonc25cc112010-05-21 14:33:48 +0000416 res->next = free_resources;
417 free_resources = res;
418}
419
420/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000421 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000422 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000423 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000424 *
425 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000426 */
427void compact_resources(device_t dev)
428{
Myles Watsonc25cc112010-05-21 14:33:48 +0000429 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000430
Eric Biederman5cd81732004-03-11 15:01:31 +0000431 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000432 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000433 next = res->next;
434 if (!res->flags)
435 free_resource(dev, res, prev);
436 else
437 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000438 }
439}
440
441/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000442 * See if a resource structure already exists for a given index.
443 *
444 * @param dev The device to find the resource on.
445 * @param index The index of the resource on the device.
446 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000447 */
Eric Biederman03acab62004-10-14 21:25:53 +0000448struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000449{
Myles Watsonc25cc112010-05-21 14:33:48 +0000450 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000451
Eric Biederman5cd81732004-03-11 15:01:31 +0000452 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000453 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000454 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000455 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000456 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000457
Myles Watsonc25cc112010-05-21 14:33:48 +0000458 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000459}
460
461/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000462 * See if a resource structure already exists for a given index and if not
463 * allocate one.
464 *
Paul Menzel20923872014-06-07 13:31:29 +0200465 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000466 *
467 * @param dev The device to find the resource on.
468 * @param index The index of the resource on the device.
469 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000470 */
471struct resource *new_resource(device_t dev, unsigned index)
472{
Myles Watsonc25cc112010-05-21 14:33:48 +0000473 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000474
Uwe Hermanne4870472010-11-04 23:23:47 +0000475 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000476 compact_resources(dev);
477
Uwe Hermanne4870472010-11-04 23:23:47 +0000478 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000479 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000480 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000481 if (free_resources == NULL && !allocate_more_resources())
482 die("Couldn't allocate more resources.");
483
484 resource = free_resources;
485 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000486 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000487 resource->next = NULL;
488 tail = dev->resource_list;
489 if (tail) {
490 while (tail->next) tail = tail->next;
491 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000492 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000493 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000494 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000495 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000496
497 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000498 if (!(resource->flags & IORESOURCE_FIXED)) {
499 resource->flags = 0;
500 resource->base = 0;
501 }
502 resource->size = 0;
503 resource->limit = 0;
504 resource->index = index;
505 resource->align = 0;
506 resource->gran = 0;
507
508 return resource;
509}
510
Eric Biederman03acab62004-10-14 21:25:53 +0000511/**
512 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000513 *
514 * @param dev The device to find the resource on.
515 * @param index The index of the resource on the device.
516 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000517 */
518struct resource *find_resource(device_t dev, unsigned index)
519{
520 struct resource *resource;
521
Uwe Hermanne4870472010-11-04 23:23:47 +0000522 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000523 resource = probe_resource(dev, index);
524 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000525 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000526 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000527 die("");
528 }
529 return resource;
530}
531
Eric Biederman03acab62004-10-14 21:25:53 +0000532/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000533 * Round a number up to the next multiple of gran.
534 *
535 * @param val The starting value.
536 * @param gran Granularity we are aligning the number to.
537 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000538 */
539static resource_t align_up(resource_t val, unsigned long gran)
540{
541 resource_t mask;
542 mask = (1ULL << gran) - 1ULL;
543 val += mask;
544 val &= ~mask;
545 return val;
546}
547
548/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000549 * Round a number up to the previous multiple of gran.
550 *
551 * @param val The starting value.
552 * @param gran Granularity we are aligning the number to.
553 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000554 */
555static resource_t align_down(resource_t val, unsigned long gran)
556{
557 resource_t mask;
558 mask = (1ULL << gran) - 1ULL;
559 val &= ~mask;
560 return val;
561}
562
563/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000564 * Compute the maximum address that is part of a resource.
565 *
566 * @param resource The resource whose limit is desired.
567 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000568 */
569resource_t resource_end(struct resource *resource)
570{
571 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000572
573 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000574 base = resource->base;
575
Uwe Hermanne4870472010-11-04 23:23:47 +0000576 /*
577 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000578 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000579 * the bridge. While the granularity is simply how many low bits of
580 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000581 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000582
Uwe Hermanne4870472010-11-04 23:23:47 +0000583 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000584 end = base + align_up(resource->size, resource->gran) - 1;
585
586 return end;
587}
588
589/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000590 * Compute the maximum legal value for resource->base.
591 *
592 * @param resource The resource whose maximum is desired.
593 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000594 */
595resource_t resource_max(struct resource *resource)
596{
597 resource_t max;
598
599 max = align_down(resource->limit - resource->size + 1, resource->align);
600
601 return max;
602}
603
604/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000605 * Return the resource type of a resource.
606 *
607 * @param resource The resource type to decode.
608 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000609 */
610const char *resource_type(struct resource *resource)
611{
612 static char buffer[RESOURCE_TYPE_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100613 snprintf(buffer, sizeof (buffer), "%s%s%s%s",
614 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
615 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
616 ((resource->flags == 0) ? "unused" :
617 (resource->flags & IORESOURCE_IO) ? "io" :
618 (resource->flags & IORESOURCE_DRQ) ? "drq" :
619 (resource->flags & IORESOURCE_IRQ) ? "irq" :
620 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
621 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000622 return buffer;
623}
624
625/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000626 * Print the resource that was just stored.
627 *
Martin Roth63373ed2013-07-08 16:24:19 -0600628 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000629 * @param resource The resource that was just stored.
630 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000631 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000632void report_resource_stored(device_t dev, struct resource *resource,
633 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000634{
Uwe Hermanne4870472010-11-04 23:23:47 +0000635 char buf[10];
636 unsigned long long base, end;
637
638 if (!(resource->flags & IORESOURCE_STORED))
639 return;
640
641 base = resource->base;
642 end = resource_end(resource);
643 buf[0] = '\0';
644
645 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000646#if CONFIG_PCI_BUS_SEGN_BITS
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100647 snprintf(buf, sizeof (buf),
648 "bus %04x:%02x ", dev->bus->secondary >> 8,
Uwe Hermanne4870472010-11-04 23:23:47 +0000649 dev->link_list->secondary & 0xff);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000650#else
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100651 snprintf(buf, sizeof (buf),
652 "bus %02x ", dev->link_list->secondary);
Yinghai Lu5f9624d2006-10-04 22:56:21 +0000653#endif
Eric Biederman03acab62004-10-14 21:25:53 +0000654 }
Patrick Georgi51615092012-03-11 19:31:03 +0100655 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000656 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
657 base, end, resource->size, resource->gran, buf,
658 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000659}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000660
Uwe Hermanne4870472010-11-04 23:23:47 +0000661void search_bus_resources(struct bus *bus, unsigned long type_mask,
662 unsigned long type, resource_search_t search,
663 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000664{
665 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000666
Myles Watson894a3472010-06-09 22:41:35 +0000667 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000668 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000669
670 /* Ignore disabled devices. */
671 if (!curdev->enabled)
672 continue;
673
Myles Watson894a3472010-06-09 22:41:35 +0000674 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000675 /* If it isn't the right kind of resource ignore it. */
676 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000677 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000678
679 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000680 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000681 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000682 for (subbus = curdev->link_list; subbus;
683 subbus = subbus->next)
684 if (subbus->link_num
685 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000686 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700687 if (!subbus) /* Why can subbus be NULL? */
688 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000689 search_bus_resources(subbus, type_mask, type,
690 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000691 continue;
692 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000693 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000694 }
695 }
696}
697
Uwe Hermanne4870472010-11-04 23:23:47 +0000698void search_global_resources(unsigned long type_mask, unsigned long type,
699 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000700{
701 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000702
Myles Watson894a3472010-06-09 22:41:35 +0000703 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000704 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000705
706 /* Ignore disabled devices. */
707 if (!curdev->enabled)
708 continue;
709
Myles Watson894a3472010-06-09 22:41:35 +0000710 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000711 /* If it isn't the right kind of resource ignore it. */
712 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000713 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000714
715 /* If it is a subtractive resource ignore it. */
716 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000717 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000718
Myles Watsonc25cc112010-05-21 14:33:48 +0000719 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000720 }
721 }
722}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000723
724void dev_set_enabled(device_t dev, int enable)
725{
Uwe Hermanne4870472010-11-04 23:23:47 +0000726 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000727 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000728
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000729 dev->enabled = enable;
730 if (dev->ops && dev->ops->enable) {
731 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000732 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000733 dev->chip_ops->enable_dev(dev);
734 }
735}
736
737void disable_children(struct bus *bus)
738{
739 device_t child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000740
Myles Watson894a3472010-06-09 22:41:35 +0000741 for (child = bus->children; child; child = child->sibling) {
742 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000743 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000744 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000745 dev_set_enabled(child, 0);
746 }
747}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000748
Maciej Pijankaea921852009-10-27 14:29:29 +0000749static void resource_tree(struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000750{
Myles Watson894a3472010-06-09 22:41:35 +0000751 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000752 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000753 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000754 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000755 char indent[30]; /* If your tree has more levels, it's wrong. */
756
757 for (i = 0; i < depth + 1 && i < 29; i++)
758 indent[i] = ' ';
759 indent[i] = '\0';
760
Myles Watson894a3472010-06-09 22:41:35 +0000761 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
762 if (root->link_list && root->link_list->children)
763 do_printk(BIOS_DEBUG, " child on link 0 %s",
764 dev_path(root->link_list->children));
765 do_printk(BIOS_DEBUG, "\n");
766
Myles Watsonc25cc112010-05-21 14:33:48 +0000767 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000768 do_printk(debug_level, "%s%s resource base %llx size %llx "
769 "align %d gran %d limit %llx flags %lx index %lx\n",
770 indent, dev_path(root), res->base, res->size,
771 res->align, res->gran, res->limit, res->flags,
772 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000773 }
774
Myles Watson894a3472010-06-09 22:41:35 +0000775 for (link = root->link_list; link; link = link->next) {
776 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000777 resource_tree(child, debug_level, depth + 1);
778 }
779}
780
Uwe Hermanne4870472010-11-04 23:23:47 +0000781void print_resource_tree(struct device *root, int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000782{
783 /* Bail if root is null. */
784 if (!root) {
785 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
786 return;
787 }
788
789 /* Bail if not printing to screen. */
790 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000791 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000792 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000793
Myles Watsonbb3d8122009-05-11 22:45:35 +0000794 resource_tree(root, debug_level, 0);
795}
796
797void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
798{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800799 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000800 int i;
801 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000802 struct bus *link;
803
Myles Watsonbb3d8122009-05-11 22:45:35 +0000804 for (i = 0; i < depth; i++)
805 depth_str[i] = ' ';
806 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000807
Myles Watsonc25cc112010-05-21 14:33:48 +0000808 do_printk(debug_level, "%s%s: enabled %d\n",
809 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000810
Myles Watson894a3472010-06-09 22:41:35 +0000811 for (link = dev->link_list; link; link = link->next) {
812 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000813 sibling = sibling->sibling)
814 show_devs_tree(sibling, debug_level, depth + 1, i);
815 }
816}
817
818void show_all_devs_tree(int debug_level, const char *msg)
819{
820 /* Bail if not printing to screen. */
821 if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
822 return;
823 show_devs_tree(all_devices, debug_level, 0, -1);
824}
825
826void show_devs_subtree(struct device *root, int debug_level, const char *msg)
827{
828 /* Bail if not printing to screen. */
829 if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000830 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000831 return;
832 do_printk(debug_level, "%s\n", msg);
833 show_devs_tree(root, debug_level, 0, -1);
834}
835
836void show_all_devs(int debug_level, const char *msg)
837{
838 struct device *dev;
839
840 /* Bail if not printing to screen. */
841 if (!do_printk(debug_level, "Show all devs...%s\n", msg))
842 return;
843 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000844 do_printk(debug_level, "%s: enabled %d\n",
845 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000846 }
847}
848
849void show_one_resource(int debug_level, struct device *dev,
850 struct resource *resource, const char *comment)
851{
852 char buf[10];
853 unsigned long long base, end;
854 base = resource->base;
855 end = resource_end(resource);
856 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000857
Myles Watsonbb3d8122009-05-11 22:45:35 +0000858/*
859 if (resource->flags & IORESOURCE_BRIDGE) {
Stefan Reinauer08670622009-06-30 15:17:49 +0000860#if CONFIG_PCI_BUS_SEGN_BITS
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100861 snprintf(buf, sizeof (buf), "bus %04x:%02x ",
862 dev->bus->secondary >> 8,
863 dev->link[0].secondary & 0xff);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000864#else
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100865 snprintf(buf, sizeof (buf),
866 "bus %02x ", dev->link[0].secondary);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000867#endif
868 }
869*/
Myles Watsonbb3d8122009-05-11 22:45:35 +0000870
Uwe Hermanne4870472010-11-04 23:23:47 +0000871 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100872 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000873 resource->index, base, end, resource->size, resource->gran,
874 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000875}
876
877void show_all_devs_resources(int debug_level, const char* msg)
878{
879 struct device *dev;
880
Uwe Hermanne4870472010-11-04 23:23:47 +0000881 if (!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000882 return;
883
884 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000885 struct resource *res;
886 do_printk(debug_level, "%s: enabled %d\n",
887 dev_path(dev), dev->enabled);
888 for (res = dev->resource_list; res; res = res->next)
889 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000890 }
891}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000892
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300893void fixed_mem_resource(device_t dev, unsigned long index,
894 unsigned long basek, unsigned long sizek, unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000895{
896 struct resource *resource;
897
898 if (!sizek)
899 return;
900
901 resource = new_resource(dev, index);
902 resource->base = ((resource_t)basek) << 10;
903 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300904 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
905 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000906
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300907 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300908}
909
Uwe Hermann4b42a622010-10-11 19:36:13 +0000910void tolm_test(void *gp, struct device *dev, struct resource *new)
911{
912 struct resource **best_p = gp;
913 struct resource *best;
914
915 best = *best_p;
916
917 if (!best || (best->base > new->base))
918 best = new;
919
920 *best_p = best;
921}
922
923u32 find_pci_tolm(struct bus *bus)
924{
925 struct resource *min = NULL;
926 u32 tolm;
927
928 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
929 tolm_test, &min);
930
931 tolm = 0xffffffffUL;
932
933 if (min && tolm > min->base)
934 tolm = min->base;
935
936 return tolm;
937}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700938
939/* Count of enabled CPUs */
940int dev_count_cpu(void)
941{
942 device_t cpu;
943 int count = 0;
944
945 for (cpu = all_devices; cpu; cpu = cpu->next) {
946 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800947 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700948 continue;
949 if (!cpu->enabled)
950 continue;
951 count++;
952 }
953
954 return count;
955}