blob: cd33aac263ee61723d2ef2c648819326856312fd [file] [log] [blame]
Uwe Hermannb80dbf02007-04-22 19:08:13 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermannb80dbf02007-04-22 19:08:13 +00003 *
4 * Copyright (C) 2003-2004 Linux Networx
5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
6 * Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
7 * Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
8 * Copyright (C) 2005-2006 Tyan
9 * (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
Uwe Hermannb80dbf02007-04-22 19:08:13 +000019 */
20
Eric Biederman8ca8d762003-04-22 19:02:15 +000021#include <console/console.h>
Eric Biederman5899fd82003-04-24 06:25:08 +000022#include <device/device.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000023#include <device/path.h>
Kyösti Mälkki318066f2014-02-12 14:18:50 +020024#include <device/pci_def.h>
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +000025#include <device/resource.h>
Eric Biedermane9a271e32003-09-02 03:36:25 +000026#include <string.h>
27
Eric Biederman03acab62004-10-14 21:25:53 +000028/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000029 * See if a device structure exists for path.
Eric Biederman03acab62004-10-14 21:25:53 +000030 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000031 * @param parent The bus to find the device on.
32 * @param path The relative path from the bus to the appropriate device.
33 * @return Pointer to a device structure for the device on bus at path
34 * or 0/NULL if no device is found.
Eric Biederman03acab62004-10-14 21:25:53 +000035 */
36device_t find_dev_path(struct bus *parent, struct device_path *path)
37{
38 device_t child;
Myles Watson894a3472010-06-09 22:41:35 +000039 for (child = parent->children; child; child = child->sibling) {
Uwe Hermanne4870472010-11-04 23:23:47 +000040 if (path_eq(path, &child->path))
Eric Biederman03acab62004-10-14 21:25:53 +000041 break;
Eric Biederman03acab62004-10-14 21:25:53 +000042 }
43 return child;
44}
Eric Biedermane9a271e32003-09-02 03:36:25 +000045
46/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000047 * Given a PCI bus and a devfn number, find the device structure.
Li-Ta Loe8b1c9d2004-12-27 04:25:41 +000048 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000049 * @param bus The bus number.
50 * @param devfn A device/function number.
51 * @return Pointer to the device structure (if found), 0 otherwise.
Eric Biederman8ca8d762003-04-22 19:02:15 +000052 */
53struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
54{
Eric Biederman83b991a2003-10-11 06:20:25 +000055 struct device *dev, *result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000056
Eric Biederman83b991a2003-10-11 06:20:25 +000057 result = 0;
Eric Biedermane9a271e32003-09-02 03:36:25 +000058 for (dev = all_devices; dev; dev = dev->next) {
Eric Biederman5cd81732004-03-11 15:01:31 +000059 if ((dev->path.type == DEVICE_PATH_PCI) &&
Uwe Hermanne4870472010-11-04 23:23:47 +000060 (dev->bus->secondary == bus) &&
61 (dev->path.pci.devfn == devfn)) {
Eric Biederman83b991a2003-10-11 06:20:25 +000062 result = dev;
Eric Biederman8ca8d762003-04-22 19:02:15 +000063 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +000064 }
65 }
Eric Biederman83b991a2003-10-11 06:20:25 +000066 return result;
Eric Biederman8ca8d762003-04-22 19:02:15 +000067}
68
arch import user (historical)98d0d302005-07-06 17:13:46 +000069/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +000070 * Given an SMBus bus and a device number, find the device structure.
arch import user (historical)98d0d302005-07-06 17:13:46 +000071 *
Uwe Hermannc1ee4292010-10-17 19:01:48 +000072 * @param bus The bus number.
73 * @param addr A device number.
74 * @return Pointer to the device structure (if found), 0 otherwise.
arch import user (historical)98d0d302005-07-06 17:13:46 +000075 */
76struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
77{
Uwe Hermanne4870472010-11-04 23:23:47 +000078 struct device *dev, *result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000079
Uwe Hermanne4870472010-11-04 23:23:47 +000080 result = 0;
81 for (dev = all_devices; dev; dev = dev->next) {
82 if ((dev->path.type == DEVICE_PATH_I2C) &&
83 (dev->bus->secondary == bus) &&
84 (dev->path.i2c.device == addr)) {
85 result = dev;
86 break;
87 }
88 }
89 return result;
Stefan Reinauer14e22772010-04-27 06:56:47 +000090}
arch import user (historical)98d0d302005-07-06 17:13:46 +000091
Uwe Hermannc1ee4292010-10-17 19:01:48 +000092/**
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +010093 * Given a PnP port and a device number, find the device structure.
94 *
95 * @param port The I/O port.
96 * @param device Logical device number.
97 * @return Pointer to the device structure (if found), 0 otherwise.
98 */
Vladimir Serbinenkob33384a2014-02-08 18:58:39 +010099struct device *dev_find_slot_pnp(u16 port, u16 device)
Vladimir Serbinenko400c05c2014-02-04 14:34:11 +0100100{
101 struct device *dev;
102
103 for (dev = all_devices; dev; dev = dev->next) {
104 if ((dev->path.type == DEVICE_PATH_PNP) &&
105 (dev->path.pnp.port == port) &&
106 (dev->path.pnp.device == device)) {
107 return dev;
108 }
109 }
110 return 0;
111}
112
113/**
Duncan Laurie6f88a6e2011-07-18 10:41:36 -0700114 * Given a Local APIC ID, find the device structure.
115 *
116 * @param apic_id The Local APIC ID number.
117 * @return Pointer to the device structure (if found), 0 otherwise.
118 */
119device_t dev_find_lapic(unsigned apic_id)
120{
121 device_t dev, result = NULL;
122
123 for (dev = all_devices; dev; dev = dev->next) {
124 if (dev->path.type == DEVICE_PATH_APIC &&
125 dev->path.apic.apic_id == apic_id) {
126 result = dev;
127 break;
128 }
129 }
130 return result;
131}
132
133/**
Subrata Banikfe204fe2016-12-06 18:06:30 +0530134 * Given a Device Path Type, find the device structure.
135 *
136 * @param prev_match The previously matched device instance.
137 * @param path_type The Device Path Type.
138 * @return Pointer to the device structure (if found), 0 otherwise.
139 */
140device_t dev_find_path(device_t prev_match, enum device_path_type path_type)
141{
142 device_t dev, result = NULL;
143
144 if (prev_match == NULL)
145 prev_match = all_devices;
146 else
147 prev_match = prev_match->next;
148
149 for (dev = prev_match; dev; dev = dev->next) {
150 if (dev->path.type == path_type) {
151 result = dev;
152 break;
153 }
154 }
155 return result;
156}
157
158/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000159 * Find a device of a given vendor and type.
160 *
161 * @param vendor A PCI vendor ID (e.g. 0x8086 for Intel).
162 * @param device A PCI device ID.
Uwe Hermanne4870472010-11-04 23:23:47 +0000163 * @param from Pointer to the device structure, used as a starting point in
164 * the linked list of all_devices, which can be 0 to start at the
165 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000166 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000167 */
Uwe Hermanne4870472010-11-04 23:23:47 +0000168struct device *dev_find_device(u16 vendor, u16 device, struct device *from)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000169{
170 if (!from)
171 from = all_devices;
172 else
173 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000174
175 while (from && (from->vendor != vendor || from->device != device))
Eric Biederman8ca8d762003-04-22 19:02:15 +0000176 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000177
Eric Biederman8ca8d762003-04-22 19:02:15 +0000178 return from;
179}
180
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000181/**
182 * Find a device of a given class.
183 *
184 * @param class Class of the device.
Uwe Hermanne4870472010-11-04 23:23:47 +0000185 * @param from Pointer to the device structure, used as a starting point in
186 * the linked list of all_devices, which can be 0 to start at the
187 * head of the list (i.e. all_devices).
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000188 * @return Pointer to the device struct.
Eric Biederman8ca8d762003-04-22 19:02:15 +0000189 */
190struct device *dev_find_class(unsigned int class, struct device *from)
191{
192 if (!from)
193 from = all_devices;
194 else
195 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000196
Greg Watson59651692003-12-17 17:39:53 +0000197 while (from && (from->class & 0xffffff00) != class)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000198 from = from->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000199
Eric Biederman8ca8d762003-04-22 19:02:15 +0000200 return from;
201}
202
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700203/**
204 * Encode the device path into 3 bytes for logging to CMOS.
205 *
206 * @param dev The device path to encode.
207 * @return Device path encoded into lower 3 bytes of dword.
208 */
209u32 dev_path_encode(device_t dev)
210{
211 u32 ret;
212
213 if (!dev)
214 return 0;
215
216 /* Store the device type in 3rd byte. */
217 ret = dev->path.type << 16;
218
219 /* Encode the device specific path in the low word. */
220 switch (dev->path.type) {
221 case DEVICE_PATH_ROOT:
222 break;
223 case DEVICE_PATH_PCI:
224 ret |= dev->bus->secondary << 8 | dev->path.pci.devfn;
225 break;
226 case DEVICE_PATH_PNP:
227 ret |= dev->path.pnp.port << 8 | dev->path.pnp.device;
228 break;
229 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700230 ret |= dev->path.i2c.mode_10bit << 8 | dev->path.i2c.device;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700231 break;
232 case DEVICE_PATH_APIC:
233 ret |= dev->path.apic.apic_id;
234 break;
235 case DEVICE_PATH_DOMAIN:
236 ret |= dev->path.domain.domain;
237 break;
238 case DEVICE_PATH_CPU_CLUSTER:
239 ret |= dev->path.cpu_cluster.cluster;
240 break;
241 case DEVICE_PATH_CPU:
242 ret |= dev->path.cpu.id;
243 break;
244 case DEVICE_PATH_CPU_BUS:
245 ret |= dev->path.cpu_bus.id;
246 break;
247 case DEVICE_PATH_IOAPIC:
248 ret |= dev->path.ioapic.ioapic_id;
249 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700250 case DEVICE_PATH_GENERIC:
251 ret |= dev->path.generic.subid << 8 | dev->path.generic.id;
252 break;
Duncan Laurie5f5d9142013-06-10 09:59:17 -0700253 case DEVICE_PATH_NONE:
254 default:
255 break;
256 }
257
258 return ret;
259}
260
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000261/*
262 * Warning: This function uses a static buffer. Don't call it more than once
263 * from the same print statement!
264 */
Eric Biedermane9a271e32003-09-02 03:36:25 +0000265const char *dev_path(device_t dev)
266{
267 static char buffer[DEVICE_PATH_MAX];
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000268
Eric Biedermane9a271e32003-09-02 03:36:25 +0000269 buffer[0] = '\0';
270 if (!dev) {
271 memcpy(buffer, "<null>", 7);
Uwe Hermanne4870472010-11-04 23:23:47 +0000272 } else {
Eric Biedermane9a271e32003-09-02 03:36:25 +0000273 switch(dev->path.type) {
Eric Biederman83b991a2003-10-11 06:20:25 +0000274 case DEVICE_PATH_ROOT:
275 memcpy(buffer, "Root Device", 12);
276 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000277 case DEVICE_PATH_PCI:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100278 snprintf(buffer, sizeof (buffer),
279 "PCI: %02x:%02x.%01x",
280 dev->bus->secondary,
281 PCI_SLOT(dev->path.pci.devfn),
282 PCI_FUNC(dev->path.pci.devfn));
Eric Biedermane9a271e32003-09-02 03:36:25 +0000283 break;
284 case DEVICE_PATH_PNP:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100285 snprintf(buffer, sizeof (buffer), "PNP: %04x.%01x",
286 dev->path.pnp.port, dev->path.pnp.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000287 break;
288 case DEVICE_PATH_I2C:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100289 snprintf(buffer, sizeof (buffer), "I2C: %02x:%02x",
290 dev->bus->secondary,
291 dev->path.i2c.device);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000292 break;
Eric Biederman03acab62004-10-14 21:25:53 +0000293 case DEVICE_PATH_APIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100294 snprintf(buffer, sizeof (buffer), "APIC: %02x",
295 dev->path.apic.apic_id);
Eric Biederman03acab62004-10-14 21:25:53 +0000296 break;
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200297 case DEVICE_PATH_IOAPIC:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100298 snprintf(buffer, sizeof (buffer), "IOAPIC: %02x",
299 dev->path.ioapic.ioapic_id);
Sven Schnelle0fa50a12012-06-21 22:19:48 +0200300 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800301 case DEVICE_PATH_DOMAIN:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100302 snprintf(buffer, sizeof (buffer), "DOMAIN: %04x",
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800303 dev->path.domain.domain);
Eric Biederman7003ba42004-10-16 06:20:29 +0000304 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800305 case DEVICE_PATH_CPU_CLUSTER:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100306 snprintf(buffer, sizeof (buffer), "CPU_CLUSTER: %01x",
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800307 dev->path.cpu_cluster.cluster);
Eric Biederman7003ba42004-10-16 06:20:29 +0000308 break;
Eric Biedermana9e632c2004-11-18 22:38:08 +0000309 case DEVICE_PATH_CPU:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100310 snprintf(buffer, sizeof (buffer),
311 "CPU: %02x", dev->path.cpu.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000312 break;
313 case DEVICE_PATH_CPU_BUS:
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100314 snprintf(buffer, sizeof (buffer),
315 "CPU_BUS: %02x", dev->path.cpu_bus.id);
Eric Biedermana9e632c2004-11-18 22:38:08 +0000316 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700317 case DEVICE_PATH_GENERIC:
318 snprintf(buffer, sizeof (buffer),
319 "GENERIC: %d.%d", dev->path.generic.id,
320 dev->path.generic.subid);
321 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000322 default:
Uwe Hermanne4870472010-11-04 23:23:47 +0000323 printk(BIOS_ERR, "Unknown device path type: %d\n",
324 dev->path.type);
Eric Biedermane9a271e32003-09-02 03:36:25 +0000325 break;
326 }
327 }
328 return buffer;
329}
330
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200331const char *dev_name(device_t dev)
332{
Kyösti Mälkki7d54eb82012-10-10 23:14:28 +0300333 if (dev->name)
334 return dev->name;
335 else if (dev->chip_ops && dev->chip_ops->name)
Kyösti Mälkki7baadac2012-10-07 14:57:15 +0200336 return dev->chip_ops->name;
337 else
338 return "unknown";
339}
340
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000341const char *bus_path(struct bus *bus)
342{
343 static char buffer[BUS_PATH_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100344 snprintf(buffer, sizeof (buffer),
345 "%s,%d", dev_path(bus->dev), bus->link_num);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000346 return buffer;
347}
348
Eric Biedermane9a271e32003-09-02 03:36:25 +0000349int path_eq(struct device_path *path1, struct device_path *path2)
350{
351 int equal = 0;
Uwe Hermanne4870472010-11-04 23:23:47 +0000352
353 if (path1->type != path2->type)
354 return 0;
355
356 switch (path1->type) {
357 case DEVICE_PATH_NONE:
358 break;
359 case DEVICE_PATH_ROOT:
360 equal = 1;
361 break;
362 case DEVICE_PATH_PCI:
363 equal = (path1->pci.devfn == path2->pci.devfn);
364 break;
365 case DEVICE_PATH_PNP:
366 equal = (path1->pnp.port == path2->pnp.port) &&
367 (path1->pnp.device == path2->pnp.device);
368 break;
369 case DEVICE_PATH_I2C:
Duncan Laurieb7ce5fe2016-05-07 19:49:37 -0700370 equal = (path1->i2c.device == path2->i2c.device) &&
371 (path1->i2c.mode_10bit == path2->i2c.mode_10bit);
Uwe Hermanne4870472010-11-04 23:23:47 +0000372 break;
373 case DEVICE_PATH_APIC:
374 equal = (path1->apic.apic_id == path2->apic.apic_id);
375 break;
Stefan Reinauer4aff4452013-02-12 14:17:15 -0800376 case DEVICE_PATH_DOMAIN:
377 equal = (path1->domain.domain == path2->domain.domain);
Uwe Hermanne4870472010-11-04 23:23:47 +0000378 break;
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800379 case DEVICE_PATH_CPU_CLUSTER:
380 equal = (path1->cpu_cluster.cluster
381 == path2->cpu_cluster.cluster);
Uwe Hermanne4870472010-11-04 23:23:47 +0000382 break;
383 case DEVICE_PATH_CPU:
384 equal = (path1->cpu.id == path2->cpu.id);
385 break;
386 case DEVICE_PATH_CPU_BUS:
387 equal = (path1->cpu_bus.id == path2->cpu_bus.id);
388 break;
Duncan Laurie4650f5b2016-05-07 20:01:34 -0700389 case DEVICE_PATH_GENERIC:
390 equal = (path1->generic.id == path2->generic.id) &&
391 (path1->generic.subid == path2->generic.subid);
392 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000393 default:
Martin Roth63373ed2013-07-08 16:24:19 -0600394 printk(BIOS_ERR, "Unknown device type: %d\n", path1->type);
Uwe Hermanne4870472010-11-04 23:23:47 +0000395 break;
Eric Biedermane9a271e32003-09-02 03:36:25 +0000396 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000397
Eric Biedermane9a271e32003-09-02 03:36:25 +0000398 return equal;
399}
Eric Biederman5cd81732004-03-11 15:01:31 +0000400
401/**
Myles Watsonc25cc112010-05-21 14:33:48 +0000402 * Allocate 64 more resources to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000403 *
404 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000405 */
406static int allocate_more_resources(void)
407{
408 int i;
409 struct resource *new_res_list;
Uwe Hermanne4870472010-11-04 23:23:47 +0000410
Myles Watsonc25cc112010-05-21 14:33:48 +0000411 new_res_list = malloc(64 * sizeof(*new_res_list));
412
413 if (new_res_list == NULL)
414 return 0;
415
416 memset(new_res_list, 0, 64 * sizeof(*new_res_list));
417
Uwe Hermanne4870472010-11-04 23:23:47 +0000418 for (i = 0; i < 64 - 1; i++)
Myles Watsonc25cc112010-05-21 14:33:48 +0000419 new_res_list[i].next = &new_res_list[i+1];
420
421 free_resources = new_res_list;
422 return 1;
423}
424
425/**
426 * Remove resource res from the device's list and add it to the free list.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000427 *
428 * @param dev TODO
429 * @param res TODO
430 * @param prev TODO
431 * @return TODO.
Myles Watsonc25cc112010-05-21 14:33:48 +0000432 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000433static void free_resource(device_t dev, struct resource *res,
434 struct resource *prev)
Myles Watsonc25cc112010-05-21 14:33:48 +0000435{
436 if (prev)
437 prev->next = res->next;
438 else
439 dev->resource_list = res->next;
Uwe Hermanne4870472010-11-04 23:23:47 +0000440
Myles Watsonc25cc112010-05-21 14:33:48 +0000441 res->next = free_resources;
442 free_resources = res;
443}
444
445/**
Eric Biederman5cd81732004-03-11 15:01:31 +0000446 * See if we have unused but allocated resource structures.
Uwe Hermanne4870472010-11-04 23:23:47 +0000447 *
Eric Biederman5cd81732004-03-11 15:01:31 +0000448 * If so remove the allocation.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000449 *
450 * @param dev The device to find the resource on.
Eric Biederman5cd81732004-03-11 15:01:31 +0000451 */
452void compact_resources(device_t dev)
453{
Myles Watsonc25cc112010-05-21 14:33:48 +0000454 struct resource *res, *next, *prev = NULL;
Uwe Hermanne4870472010-11-04 23:23:47 +0000455
Eric Biederman5cd81732004-03-11 15:01:31 +0000456 /* Move all of the free resources to the end */
Myles Watson894a3472010-06-09 22:41:35 +0000457 for (res = dev->resource_list; res; res = next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000458 next = res->next;
459 if (!res->flags)
460 free_resource(dev, res, prev);
461 else
462 prev = res;
Eric Biederman5cd81732004-03-11 15:01:31 +0000463 }
464}
465
466/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000467 * See if a resource structure already exists for a given index.
468 *
469 * @param dev The device to find the resource on.
470 * @param index The index of the resource on the device.
471 * @return The resource, if it already exists.
Eric Biederman5cd81732004-03-11 15:01:31 +0000472 */
Eric Biederman03acab62004-10-14 21:25:53 +0000473struct resource *probe_resource(device_t dev, unsigned index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000474{
Myles Watsonc25cc112010-05-21 14:33:48 +0000475 struct resource *res;
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000476
Eric Biederman5cd81732004-03-11 15:01:31 +0000477 /* See if there is a resource with the appropriate index */
Myles Watson894a3472010-06-09 22:41:35 +0000478 for (res = dev->resource_list; res; res = res->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000479 if (res->index == index)
Eric Biederman5cd81732004-03-11 15:01:31 +0000480 break;
Eric Biederman5cd81732004-03-11 15:01:31 +0000481 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000482
Myles Watsonc25cc112010-05-21 14:33:48 +0000483 return res;
Eric Biederman03acab62004-10-14 21:25:53 +0000484}
485
486/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000487 * See if a resource structure already exists for a given index and if not
488 * allocate one.
489 *
Paul Menzel20923872014-06-07 13:31:29 +0200490 * Then initialize the resource to default values.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000491 *
492 * @param dev The device to find the resource on.
493 * @param index The index of the resource on the device.
494 * @return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000495 */
496struct resource *new_resource(device_t dev, unsigned index)
497{
Myles Watsonc25cc112010-05-21 14:33:48 +0000498 struct resource *resource, *tail;
Eric Biederman03acab62004-10-14 21:25:53 +0000499
Uwe Hermanne4870472010-11-04 23:23:47 +0000500 /* First move all of the free resources to the end. */
Eric Biederman03acab62004-10-14 21:25:53 +0000501 compact_resources(dev);
502
Uwe Hermanne4870472010-11-04 23:23:47 +0000503 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000504 resource = probe_resource(dev, index);
Eric Biederman5cd81732004-03-11 15:01:31 +0000505 if (!resource) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000506 if (free_resources == NULL && !allocate_more_resources())
507 die("Couldn't allocate more resources.");
508
509 resource = free_resources;
510 free_resources = free_resources->next;
Eric Biederman5cd81732004-03-11 15:01:31 +0000511 memset(resource, 0, sizeof(*resource));
Myles Watsonc25cc112010-05-21 14:33:48 +0000512 resource->next = NULL;
513 tail = dev->resource_list;
514 if (tail) {
515 while (tail->next) tail = tail->next;
516 tail->next = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000517 } else {
Myles Watsonc25cc112010-05-21 14:33:48 +0000518 dev->resource_list = resource;
Uwe Hermanne4870472010-11-04 23:23:47 +0000519 }
Eric Biederman5cd81732004-03-11 15:01:31 +0000520 }
Uwe Hermanne4870472010-11-04 23:23:47 +0000521
522 /* Initialize the resource values. */
Eric Biederman5cd81732004-03-11 15:01:31 +0000523 if (!(resource->flags & IORESOURCE_FIXED)) {
524 resource->flags = 0;
525 resource->base = 0;
526 }
527 resource->size = 0;
528 resource->limit = 0;
529 resource->index = index;
530 resource->align = 0;
531 resource->gran = 0;
532
533 return resource;
534}
535
Eric Biederman03acab62004-10-14 21:25:53 +0000536/**
537 * Return an existing resource structure for a given index.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000538 *
539 * @param dev The device to find the resource on.
540 * @param index The index of the resource on the device.
541 * return TODO.
Eric Biederman03acab62004-10-14 21:25:53 +0000542 */
543struct resource *find_resource(device_t dev, unsigned index)
544{
545 struct resource *resource;
546
Uwe Hermanne4870472010-11-04 23:23:47 +0000547 /* See if there is a resource with the appropriate index. */
Eric Biederman03acab62004-10-14 21:25:53 +0000548 resource = probe_resource(dev, index);
549 if (!resource) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000550 printk(BIOS_EMERG, "%s missing resource: %02x\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000551 dev_path(dev), index);
Eric Biederman03acab62004-10-14 21:25:53 +0000552 die("");
553 }
554 return resource;
555}
556
Eric Biederman03acab62004-10-14 21:25:53 +0000557/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000558 * Round a number up to the next multiple of gran.
559 *
560 * @param val The starting value.
561 * @param gran Granularity we are aligning the number to.
562 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000563 */
564static resource_t align_up(resource_t val, unsigned long gran)
565{
566 resource_t mask;
567 mask = (1ULL << gran) - 1ULL;
568 val += mask;
569 val &= ~mask;
570 return val;
571}
572
573/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000574 * Round a number up to the previous multiple of gran.
575 *
576 * @param val The starting value.
577 * @param gran Granularity we are aligning the number to.
578 * @return The aligned value.
Eric Biederman03acab62004-10-14 21:25:53 +0000579 */
580static resource_t align_down(resource_t val, unsigned long gran)
581{
582 resource_t mask;
583 mask = (1ULL << gran) - 1ULL;
584 val &= ~mask;
585 return val;
586}
587
588/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000589 * Compute the maximum address that is part of a resource.
590 *
591 * @param resource The resource whose limit is desired.
592 * @return The end.
Eric Biederman03acab62004-10-14 21:25:53 +0000593 */
594resource_t resource_end(struct resource *resource)
595{
596 resource_t base, end;
Uwe Hermanne4870472010-11-04 23:23:47 +0000597
598 /* Get the base address. */
Eric Biederman03acab62004-10-14 21:25:53 +0000599 base = resource->base;
600
Uwe Hermanne4870472010-11-04 23:23:47 +0000601 /*
602 * For a non bridge resource granularity and alignment are the same.
Eric Biederman03acab62004-10-14 21:25:53 +0000603 * For a bridge resource align is the largest needed alignment below
Uwe Hermanne4870472010-11-04 23:23:47 +0000604 * the bridge. While the granularity is simply how many low bits of
605 * the address cannot be set.
Eric Biederman03acab62004-10-14 21:25:53 +0000606 */
Stefan Reinauer14e22772010-04-27 06:56:47 +0000607
Uwe Hermanne4870472010-11-04 23:23:47 +0000608 /* Get the end (rounded up). */
Eric Biederman03acab62004-10-14 21:25:53 +0000609 end = base + align_up(resource->size, resource->gran) - 1;
610
611 return end;
612}
613
614/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000615 * Compute the maximum legal value for resource->base.
616 *
617 * @param resource The resource whose maximum is desired.
618 * @return The maximum.
Eric Biederman03acab62004-10-14 21:25:53 +0000619 */
620resource_t resource_max(struct resource *resource)
621{
622 resource_t max;
623
624 max = align_down(resource->limit - resource->size + 1, resource->align);
625
626 return max;
627}
628
629/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000630 * Return the resource type of a resource.
631 *
632 * @param resource The resource type to decode.
633 * @return TODO.
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000634 */
635const char *resource_type(struct resource *resource)
636{
637 static char buffer[RESOURCE_TYPE_MAX];
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100638 snprintf(buffer, sizeof (buffer), "%s%s%s%s",
639 ((resource->flags & IORESOURCE_READONLY) ? "ro" : ""),
640 ((resource->flags & IORESOURCE_PREFETCH) ? "pref" : ""),
641 ((resource->flags == 0) ? "unused" :
642 (resource->flags & IORESOURCE_IO) ? "io" :
643 (resource->flags & IORESOURCE_DRQ) ? "drq" :
644 (resource->flags & IORESOURCE_IRQ) ? "irq" :
645 (resource->flags & IORESOURCE_MEM) ? "mem" : "??????"),
646 ((resource->flags & IORESOURCE_PCI64) ? "64" : ""));
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000647 return buffer;
648}
649
650/**
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000651 * Print the resource that was just stored.
652 *
Martin Roth63373ed2013-07-08 16:24:19 -0600653 * @param dev The device the stored resource lives on.
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000654 * @param resource The resource that was just stored.
655 * @param comment TODO
Eric Biederman03acab62004-10-14 21:25:53 +0000656 */
Uwe Hermannc1ee4292010-10-17 19:01:48 +0000657void report_resource_stored(device_t dev, struct resource *resource,
658 const char *comment)
Eric Biederman03acab62004-10-14 21:25:53 +0000659{
Uwe Hermanne4870472010-11-04 23:23:47 +0000660 char buf[10];
661 unsigned long long base, end;
662
663 if (!(resource->flags & IORESOURCE_STORED))
664 return;
665
666 base = resource->base;
667 end = resource_end(resource);
668 buf[0] = '\0';
669
670 if (resource->flags & IORESOURCE_PCI_BRIDGE) {
Vladimir Serbinenkoa37383d2013-11-26 02:41:26 +0100671 snprintf(buf, sizeof (buf),
672 "bus %02x ", dev->link_list->secondary);
Eric Biederman03acab62004-10-14 21:25:53 +0000673 }
Patrick Georgi51615092012-03-11 19:31:03 +0100674 printk(BIOS_DEBUG, "%s %02lx <- [0x%010llx - 0x%010llx] size 0x%08llx "
Uwe Hermanne4870472010-11-04 23:23:47 +0000675 "gran 0x%02x %s%s%s\n", dev_path(dev), resource->index,
676 base, end, resource->size, resource->gran, buf,
677 resource_type(resource), comment);
Eric Biederman03acab62004-10-14 21:25:53 +0000678}
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000679
Uwe Hermanne4870472010-11-04 23:23:47 +0000680void search_bus_resources(struct bus *bus, unsigned long type_mask,
681 unsigned long type, resource_search_t search,
682 void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000683{
684 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000685
Myles Watson894a3472010-06-09 22:41:35 +0000686 for (curdev = bus->children; curdev; curdev = curdev->sibling) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000687 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000688
689 /* Ignore disabled devices. */
690 if (!curdev->enabled)
691 continue;
692
Myles Watson894a3472010-06-09 22:41:35 +0000693 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000694 /* If it isn't the right kind of resource ignore it. */
695 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000696 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000697
698 /* If it is a subtractive resource recurse. */
Myles Watsonc25cc112010-05-21 14:33:48 +0000699 if (res->flags & IORESOURCE_SUBTRACTIVE) {
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000700 struct bus * subbus;
Uwe Hermanne4870472010-11-04 23:23:47 +0000701 for (subbus = curdev->link_list; subbus;
702 subbus = subbus->next)
703 if (subbus->link_num
704 == IOINDEX_SUBTRACTIVE_LINK(res->index))
Myles Watson894a3472010-06-09 22:41:35 +0000705 break;
Stefan Reinauer58075552011-05-11 15:57:07 -0700706 if (!subbus) /* Why can subbus be NULL? */
707 break;
Uwe Hermanne4870472010-11-04 23:23:47 +0000708 search_bus_resources(subbus, type_mask, type,
709 search, gp);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000710 continue;
711 }
Myles Watsonc25cc112010-05-21 14:33:48 +0000712 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000713 }
714 }
715}
716
Uwe Hermanne4870472010-11-04 23:23:47 +0000717void search_global_resources(unsigned long type_mask, unsigned long type,
718 resource_search_t search, void *gp)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000719{
720 struct device *curdev;
Uwe Hermanne4870472010-11-04 23:23:47 +0000721
Myles Watson894a3472010-06-09 22:41:35 +0000722 for (curdev = all_devices; curdev; curdev = curdev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000723 struct resource *res;
Uwe Hermanne4870472010-11-04 23:23:47 +0000724
725 /* Ignore disabled devices. */
726 if (!curdev->enabled)
727 continue;
728
Myles Watson894a3472010-06-09 22:41:35 +0000729 for (res = curdev->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000730 /* If it isn't the right kind of resource ignore it. */
731 if ((res->flags & type_mask) != type)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000732 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000733
734 /* If it is a subtractive resource ignore it. */
735 if (res->flags & IORESOURCE_SUBTRACTIVE)
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000736 continue;
Uwe Hermanne4870472010-11-04 23:23:47 +0000737
Myles Watsonc25cc112010-05-21 14:33:48 +0000738 search(gp, curdev, res);
Eric Biedermanf8a2ddd2004-10-30 08:05:41 +0000739 }
740 }
741}
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000742
743void dev_set_enabled(device_t dev, int enable)
744{
Uwe Hermanne4870472010-11-04 23:23:47 +0000745 if (dev->enabled == enable)
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000746 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000747
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000748 dev->enabled = enable;
749 if (dev->ops && dev->ops->enable) {
750 dev->ops->enable(dev);
Uwe Hermanne4870472010-11-04 23:23:47 +0000751 } else if (dev->chip_ops && dev->chip_ops->enable_dev) {
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000752 dev->chip_ops->enable_dev(dev);
753 }
754}
755
756void disable_children(struct bus *bus)
757{
758 device_t child;
Uwe Hermanne4870472010-11-04 23:23:47 +0000759
Myles Watson894a3472010-06-09 22:41:35 +0000760 for (child = bus->children; child; child = child->sibling) {
761 struct bus *link;
Uwe Hermanne4870472010-11-04 23:23:47 +0000762 for (link = child->link_list; link; link = link->next)
Myles Watson894a3472010-06-09 22:41:35 +0000763 disable_children(link);
Yinghai Lu13f1c2a2005-07-08 02:49:49 +0000764 dev_set_enabled(child, 0);
765 }
766}
Myles Watsonbb3d8122009-05-11 22:45:35 +0000767
Maciej Pijankaea921852009-10-27 14:29:29 +0000768static void resource_tree(struct device *root, int debug_level, int depth)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000769{
Myles Watson894a3472010-06-09 22:41:35 +0000770 int i = 0;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000771 struct device *child;
Myles Watson894a3472010-06-09 22:41:35 +0000772 struct bus *link;
Myles Watsonc25cc112010-05-21 14:33:48 +0000773 struct resource *res;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000774 char indent[30]; /* If your tree has more levels, it's wrong. */
775
776 for (i = 0; i < depth + 1 && i < 29; i++)
777 indent[i] = ' ';
778 indent[i] = '\0';
779
Myles Watson894a3472010-06-09 22:41:35 +0000780 do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
781 if (root->link_list && root->link_list->children)
782 do_printk(BIOS_DEBUG, " child on link 0 %s",
783 dev_path(root->link_list->children));
784 do_printk(BIOS_DEBUG, "\n");
785
Myles Watsonc25cc112010-05-21 14:33:48 +0000786 for (res = root->resource_list; res; res = res->next) {
Uwe Hermanne4870472010-11-04 23:23:47 +0000787 do_printk(debug_level, "%s%s resource base %llx size %llx "
788 "align %d gran %d limit %llx flags %lx index %lx\n",
789 indent, dev_path(root), res->base, res->size,
790 res->align, res->gran, res->limit, res->flags,
791 res->index);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000792 }
793
Myles Watson894a3472010-06-09 22:41:35 +0000794 for (link = root->link_list; link; link = link->next) {
795 for (child = link->children; child; child = child->sibling)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000796 resource_tree(child, debug_level, depth + 1);
797 }
798}
799
Uwe Hermanne4870472010-11-04 23:23:47 +0000800void print_resource_tree(struct device *root, int debug_level, const char *msg)
Myles Watsonbb3d8122009-05-11 22:45:35 +0000801{
802 /* Bail if root is null. */
803 if (!root) {
804 do_printk(debug_level, "%s passed NULL for root!\n", __func__);
805 return;
806 }
807
808 /* Bail if not printing to screen. */
809 if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000810 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000811 return;
Uwe Hermanne4870472010-11-04 23:23:47 +0000812
Myles Watsonbb3d8122009-05-11 22:45:35 +0000813 resource_tree(root, debug_level, 0);
814}
815
816void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
817{
Marcelo Povoa8404dca2014-02-14 15:42:49 -0800818 char depth_str[20];
Myles Watsonbb3d8122009-05-11 22:45:35 +0000819 int i;
820 struct device *sibling;
Myles Watson894a3472010-06-09 22:41:35 +0000821 struct bus *link;
822
Myles Watsonbb3d8122009-05-11 22:45:35 +0000823 for (i = 0; i < depth; i++)
824 depth_str[i] = ' ';
825 depth_str[i] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000826
Myles Watsonc25cc112010-05-21 14:33:48 +0000827 do_printk(debug_level, "%s%s: enabled %d\n",
828 depth_str, dev_path(dev), dev->enabled);
Uwe Hermanne4870472010-11-04 23:23:47 +0000829
Myles Watson894a3472010-06-09 22:41:35 +0000830 for (link = dev->link_list; link; link = link->next) {
831 for (sibling = link->children; sibling;
Myles Watsonbb3d8122009-05-11 22:45:35 +0000832 sibling = sibling->sibling)
833 show_devs_tree(sibling, debug_level, depth + 1, i);
834 }
835}
836
837void show_all_devs_tree(int debug_level, const char *msg)
838{
839 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100840 if (!do_printk(debug_level, "Show all devs in tree form... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000841 return;
842 show_devs_tree(all_devices, debug_level, 0, -1);
843}
844
845void show_devs_subtree(struct device *root, int debug_level, const char *msg)
846{
847 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100848 if (!do_printk(debug_level, "Show all devs in subtree %s... %s\n",
Uwe Hermanne4870472010-11-04 23:23:47 +0000849 dev_path(root), msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000850 return;
851 do_printk(debug_level, "%s\n", msg);
852 show_devs_tree(root, debug_level, 0, -1);
853}
854
855void show_all_devs(int debug_level, const char *msg)
856{
857 struct device *dev;
858
859 /* Bail if not printing to screen. */
Paul Menzeleb605d72015-02-12 00:29:32 +0100860 if (!do_printk(debug_level, "Show all devs... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000861 return;
862 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000863 do_printk(debug_level, "%s: enabled %d\n",
864 dev_path(dev), dev->enabled);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000865 }
866}
867
868void show_one_resource(int debug_level, struct device *dev,
869 struct resource *resource, const char *comment)
870{
871 char buf[10];
872 unsigned long long base, end;
873 base = resource->base;
874 end = resource_end(resource);
875 buf[0] = '\0';
Uwe Hermanne4870472010-11-04 23:23:47 +0000876
Uwe Hermanne4870472010-11-04 23:23:47 +0000877 do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
Patrick Georgi51615092012-03-11 19:31:03 +0100878 "size 0x%08llx gran 0x%02x %s%s%s\n", dev_path(dev),
Uwe Hermanne4870472010-11-04 23:23:47 +0000879 resource->index, base, end, resource->size, resource->gran,
880 buf, resource_type(resource), comment);
Myles Watsonbb3d8122009-05-11 22:45:35 +0000881}
882
883void show_all_devs_resources(int debug_level, const char* msg)
884{
885 struct device *dev;
886
Paul Menzeleb605d72015-02-12 00:29:32 +0100887 if (!do_printk(debug_level, "Show all devs with resources... %s\n", msg))
Myles Watsonbb3d8122009-05-11 22:45:35 +0000888 return;
889
890 for (dev = all_devices; dev; dev = dev->next) {
Myles Watsonc25cc112010-05-21 14:33:48 +0000891 struct resource *res;
892 do_printk(debug_level, "%s: enabled %d\n",
893 dev_path(dev), dev->enabled);
894 for (res = dev->resource_list; res; res = res->next)
895 show_one_resource(debug_level, dev, res, "");
Myles Watsonbb3d8122009-05-11 22:45:35 +0000896 }
897}
Uwe Hermann4b42a622010-10-11 19:36:13 +0000898
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300899void fixed_mem_resource(device_t dev, unsigned long index,
900 unsigned long basek, unsigned long sizek, unsigned long type)
Uwe Hermann4b42a622010-10-11 19:36:13 +0000901{
902 struct resource *resource;
903
904 if (!sizek)
905 return;
906
907 resource = new_resource(dev, index);
908 resource->base = ((resource_t)basek) << 10;
909 resource->size = ((resource_t)sizek) << 10;
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300910 resource->flags = IORESOURCE_MEM | IORESOURCE_FIXED |
911 IORESOURCE_STORED | IORESOURCE_ASSIGNED;
Uwe Hermann4b42a622010-10-11 19:36:13 +0000912
Kyösti Mälkkiecf1ed42012-07-27 08:37:12 +0300913 resource->flags |= type;
Kyösti Mälkki63f8c082012-07-10 13:27:26 +0300914}
915
Kyösti Mälkkie25b5ef2016-12-02 08:56:05 +0200916void mmconf_resource_init(struct resource *resource, resource_t base,
917 int buses)
918{
919 resource->base = base;
920 resource->size = buses * MiB;
921 resource->flags = IORESOURCE_MEM | IORESOURCE_RESERVE |
922 IORESOURCE_FIXED | IORESOURCE_STORED | IORESOURCE_ASSIGNED;
923
924 printk(BIOS_DEBUG, "Adding PCIe enhanced config space BAR "
925 "0x%08lx-0x%08lx.\n", (unsigned long)(resource->base),
926 (unsigned long)(resource->base + resource->size));
927}
928
929void mmconf_resource(struct device *dev, unsigned long index)
930{
931 struct resource *resource = new_resource(dev, index);
932 mmconf_resource_init(resource, CONFIG_MMCONF_BASE_ADDRESS,
933 CONFIG_MMCONF_BUS_NUMBER);
934}
935
Uwe Hermann4b42a622010-10-11 19:36:13 +0000936void tolm_test(void *gp, struct device *dev, struct resource *new)
937{
938 struct resource **best_p = gp;
939 struct resource *best;
940
941 best = *best_p;
942
943 if (!best || (best->base > new->base))
944 best = new;
945
946 *best_p = best;
947}
948
949u32 find_pci_tolm(struct bus *bus)
950{
951 struct resource *min = NULL;
952 u32 tolm;
953
954 search_bus_resources(bus, IORESOURCE_MEM, IORESOURCE_MEM,
955 tolm_test, &min);
956
957 tolm = 0xffffffffUL;
958
959 if (min && tolm > min->base)
960 tolm = min->base;
961
962 return tolm;
963}
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700964
965/* Count of enabled CPUs */
966int dev_count_cpu(void)
967{
968 device_t cpu;
969 int count = 0;
970
971 for (cpu = all_devices; cpu; cpu = cpu->next) {
972 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Stefan Reinauer0aa37c42013-02-12 15:20:54 -0800973 (cpu->bus->dev->path.type != DEVICE_PATH_CPU_CLUSTER))
Stefan Reinauerdc8448fd2012-03-30 13:52:58 -0700974 continue;
975 if (!cpu->enabled)
976 continue;
977 count++;
978 }
979
980 return count;
981}
Lee Leahya95baf92016-02-13 06:10:04 -0800982
983/* Get device path name */
984const char *dev_path_name(enum device_path_type type)
985{
986 static const char *const type_names[] = DEVICE_PATH_NAMES;
987 const char *type_name = "Unknown";
988
989 /* Translate the type value into a string */
990 if (type < ARRAY_SIZE(type_names))
991 type_name = type_names[type];
992 return type_name;
993}