2007-04-22 21:08:13 +02:00
|
|
|
/*
|
2008-01-18 11:35:56 +01:00
|
|
|
* This file is part of the coreboot project.
|
2007-04-22 21:08:13 +02:00
|
|
|
*
|
|
|
|
* Copyright (C) 2003-2004 Linux Networx
|
|
|
|
* (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
|
|
|
|
* Copyright (C) 2003 Greg Watson <jarrah@users.sourceforge.net>
|
|
|
|
* Copyright (C) 2004 Li-Ta Lo <ollie@lanl.gov>
|
|
|
|
* Copyright (C) 2005-2006 Tyan
|
|
|
|
* (Written by Yinghai Lu <yhlu@tyan.com> for Tyan)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
#include <console/console.h>
|
2003-04-24 08:25:08 +02:00
|
|
|
#include <device/device.h>
|
2003-09-02 05:36:25 +02:00
|
|
|
#include <device/path.h>
|
|
|
|
#include <device/pci.h>
|
2004-10-30 10:05:41 +02:00
|
|
|
#include <device/resource.h>
|
2003-09-02 05:36:25 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/**
|
2004-10-14 23:25:53 +02:00
|
|
|
* @brief See if a device structure exists for path
|
2004-03-23 22:28:05 +01:00
|
|
|
*
|
2003-09-02 05:36:25 +02:00
|
|
|
* @param bus The bus to find the device on
|
|
|
|
* @param path The relative path from the bus to the appropriate device
|
2004-03-23 22:28:05 +01:00
|
|
|
* @return pointer to a device structure for the device on bus at path
|
2004-10-14 23:25:53 +02:00
|
|
|
* or 0/NULL if no device is found
|
2003-09-02 05:36:25 +02:00
|
|
|
*/
|
2004-10-14 23:25:53 +02:00
|
|
|
device_t find_dev_path(struct bus *parent, struct device_path *path)
|
2003-09-02 05:36:25 +02:00
|
|
|
{
|
|
|
|
device_t child;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (child = parent->children; child; child = child->sibling) {
|
2003-09-02 05:36:25 +02:00
|
|
|
if (path_eq(path, &child->path)) {
|
2004-10-14 23:25:53 +02:00
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
2004-10-14 23:25:53 +02:00
|
|
|
return child;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief See if a device structure already exists and if not allocate it
|
|
|
|
*
|
|
|
|
* @param bus The bus to find the device on
|
|
|
|
* @param path The relative path from the bus to the appropriate device
|
|
|
|
* @return pointer to a device structure for the device on bus at path
|
|
|
|
*/
|
|
|
|
device_t alloc_find_dev(struct bus *parent, struct device_path *path)
|
|
|
|
{
|
|
|
|
device_t child;
|
|
|
|
child = find_dev_path(parent, path);
|
|
|
|
if (!child) {
|
|
|
|
child = alloc_dev(parent, path);
|
|
|
|
}
|
|
|
|
return child;
|
2003-09-02 05:36:25 +02:00
|
|
|
}
|
2003-04-22 21:02:15 +02:00
|
|
|
|
|
|
|
/**
|
2004-12-27 05:25:41 +01:00
|
|
|
* @brief Given a PCI bus and a devfn number, find the device structure
|
|
|
|
*
|
2003-04-22 21:02:15 +02:00
|
|
|
* @param bus The bus number
|
|
|
|
* @param devfn a device/function number
|
|
|
|
* @return pointer to the device structure
|
|
|
|
*/
|
|
|
|
struct device *dev_find_slot(unsigned int bus, unsigned int devfn)
|
|
|
|
{
|
2003-10-11 08:20:25 +02:00
|
|
|
struct device *dev, *result;
|
2003-04-22 21:02:15 +02:00
|
|
|
|
2003-10-11 08:20:25 +02:00
|
|
|
result = 0;
|
2003-09-02 05:36:25 +02:00
|
|
|
for (dev = all_devices; dev; dev = dev->next) {
|
2004-03-11 16:01:31 +01:00
|
|
|
if ((dev->path.type == DEVICE_PATH_PCI) &&
|
2010-04-27 08:56:47 +02:00
|
|
|
(dev->bus->secondary == bus) &&
|
2009-02-28 21:10:20 +01:00
|
|
|
(dev->path.pci.devfn == devfn)) {
|
2003-10-11 08:20:25 +02:00
|
|
|
result = dev;
|
2003-04-22 21:02:15 +02:00
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
}
|
|
|
|
}
|
2003-10-11 08:20:25 +02:00
|
|
|
return result;
|
2003-04-22 21:02:15 +02:00
|
|
|
}
|
|
|
|
|
2005-07-06 19:13:46 +02:00
|
|
|
/**
|
|
|
|
* @brief Given a smbus bus and a device number, find the device structure
|
|
|
|
*
|
|
|
|
* @param bus The bus number
|
2010-04-27 08:56:47 +02:00
|
|
|
* @param addr a device number
|
2005-07-06 19:13:46 +02:00
|
|
|
* @return pointer to the device structure
|
|
|
|
*/
|
|
|
|
struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
|
|
|
|
{
|
|
|
|
struct device *dev, *result;
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2005-07-06 19:13:46 +02:00
|
|
|
result = 0;
|
|
|
|
for (dev = all_devices; dev; dev = dev->next) {
|
|
|
|
if ((dev->path.type == DEVICE_PATH_I2C) &&
|
2010-04-27 08:56:47 +02:00
|
|
|
(dev->bus->secondary == bus) &&
|
2009-02-28 21:10:20 +01:00
|
|
|
(dev->path.i2c.device == addr)) {
|
2005-07-06 19:13:46 +02:00
|
|
|
result = dev;
|
2010-04-27 08:56:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-07-06 19:13:46 +02:00
|
|
|
return result;
|
2010-04-27 08:56:47 +02:00
|
|
|
}
|
2005-07-06 19:13:46 +02:00
|
|
|
|
2003-04-22 21:02:15 +02:00
|
|
|
/** Find a device of a given vendor and type
|
|
|
|
* @param vendor Vendor ID (e.g. 0x8086 for Intel)
|
|
|
|
* @param device Device ID
|
|
|
|
* @param from Pointer to the device structure, used as a starting point
|
2010-04-27 08:56:47 +02:00
|
|
|
* in the linked list of all_devices, which can be 0 to start at the
|
2003-04-22 21:02:15 +02:00
|
|
|
* head of the list (i.e. all_devices)
|
2010-04-27 08:56:47 +02:00
|
|
|
* @return Pointer to the device struct
|
2003-04-22 21:02:15 +02:00
|
|
|
*/
|
|
|
|
struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
|
|
|
|
{
|
|
|
|
if (!from)
|
|
|
|
from = all_devices;
|
|
|
|
else
|
|
|
|
from = from->next;
|
2004-03-11 16:01:31 +01:00
|
|
|
while (from && (from->vendor != vendor || from->device != device)) {
|
2003-04-22 21:02:15 +02:00
|
|
|
from = from->next;
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
2003-04-22 21:02:15 +02:00
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find a device of a given class
|
|
|
|
* @param class Class of the device
|
|
|
|
* @param from Pointer to the device structure, used as a starting point
|
2010-04-27 08:56:47 +02:00
|
|
|
* in the linked list of all_devices, which can be 0 to start at the
|
2003-04-22 21:02:15 +02:00
|
|
|
* head of the list (i.e. all_devices)
|
2010-04-27 08:56:47 +02:00
|
|
|
* @return Pointer to the device struct
|
2003-04-22 21:02:15 +02:00
|
|
|
*/
|
|
|
|
struct device *dev_find_class(unsigned int class, struct device *from)
|
|
|
|
{
|
|
|
|
if (!from)
|
|
|
|
from = all_devices;
|
|
|
|
else
|
|
|
|
from = from->next;
|
2003-12-17 18:39:53 +01:00
|
|
|
while (from && (from->class & 0xffffff00) != class)
|
2003-04-22 21:02:15 +02:00
|
|
|
from = from->next;
|
|
|
|
return from;
|
|
|
|
}
|
|
|
|
|
2009-10-29 22:27:43 +01:00
|
|
|
/* Warning: This function uses a static buffer. Don't call it more than once
|
|
|
|
* from the same print statement! */
|
2003-09-02 05:36:25 +02:00
|
|
|
|
|
|
|
const char *dev_path(device_t dev)
|
|
|
|
{
|
|
|
|
static char buffer[DEVICE_PATH_MAX];
|
|
|
|
buffer[0] = '\0';
|
|
|
|
if (!dev) {
|
|
|
|
memcpy(buffer, "<null>", 7);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch(dev->path.type) {
|
2003-10-11 08:20:25 +02:00
|
|
|
case DEVICE_PATH_ROOT:
|
|
|
|
memcpy(buffer, "Root Device", 12);
|
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
case DEVICE_PATH_PCI:
|
2009-06-30 17:17:49 +02:00
|
|
|
#if CONFIG_PCI_BUS_SEGN_BITS
|
2006-10-05 00:56:21 +02:00
|
|
|
sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
|
2010-04-27 08:56:47 +02:00
|
|
|
dev->bus->secondary>>8, dev->bus->secondary & 0xff,
|
2009-02-28 21:10:20 +01:00
|
|
|
PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
|
2006-10-05 00:56:21 +02:00
|
|
|
#else
|
2003-09-02 05:36:25 +02:00
|
|
|
sprintf(buffer, "PCI: %02x:%02x.%01x",
|
2010-04-27 08:56:47 +02:00
|
|
|
dev->bus->secondary,
|
2009-02-28 21:10:20 +01:00
|
|
|
PCI_SLOT(dev->path.pci.devfn), PCI_FUNC(dev->path.pci.devfn));
|
2006-10-05 00:56:21 +02:00
|
|
|
#endif
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_PNP:
|
|
|
|
sprintf(buffer, "PNP: %04x.%01x",
|
2009-02-28 21:10:20 +01:00
|
|
|
dev->path.pnp.port, dev->path.pnp.device);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_I2C:
|
2005-07-06 19:13:46 +02:00
|
|
|
sprintf(buffer, "I2C: %02x:%02x",
|
|
|
|
dev->bus->secondary,
|
2009-02-28 21:10:20 +01:00
|
|
|
dev->path.i2c.device);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
2004-10-14 23:25:53 +02:00
|
|
|
case DEVICE_PATH_APIC:
|
|
|
|
sprintf(buffer, "APIC: %02x",
|
2009-02-28 21:10:20 +01:00
|
|
|
dev->path.apic.apic_id);
|
2004-10-14 23:25:53 +02:00
|
|
|
break;
|
2004-10-16 08:20:29 +02:00
|
|
|
case DEVICE_PATH_PCI_DOMAIN:
|
|
|
|
sprintf(buffer, "PCI_DOMAIN: %04x",
|
2009-02-28 21:10:20 +01:00
|
|
|
dev->path.pci_domain.domain);
|
2004-10-16 08:20:29 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_APIC_CLUSTER:
|
|
|
|
sprintf(buffer, "APIC_CLUSTER: %01x",
|
2009-02-28 21:10:20 +01:00
|
|
|
dev->path.apic_cluster.cluster);
|
2004-10-16 08:20:29 +02:00
|
|
|
break;
|
2004-11-18 23:38:08 +01:00
|
|
|
case DEVICE_PATH_CPU:
|
2009-02-28 21:10:20 +01:00
|
|
|
sprintf(buffer, "CPU: %02x", dev->path.cpu.id);
|
2004-11-18 23:38:08 +01:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_CPU_BUS:
|
2009-02-28 21:10:20 +01:00
|
|
|
sprintf(buffer, "CPU_BUS: %02x", dev->path.cpu_bus.id);
|
2004-11-18 23:38:08 +01:00
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
default:
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "Unknown device path type: %d\n", dev->path.type);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2005-07-08 04:49:49 +02:00
|
|
|
const char *bus_path(struct bus *bus)
|
|
|
|
{
|
|
|
|
static char buffer[BUS_PATH_MAX];
|
2010-06-10 00:41:35 +02:00
|
|
|
sprintf(buffer, "%s,%d", dev_path(bus->dev), bus->link_num);
|
2005-07-08 04:49:49 +02:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2003-09-02 05:36:25 +02:00
|
|
|
int path_eq(struct device_path *path1, struct device_path *path2)
|
|
|
|
{
|
|
|
|
int equal = 0;
|
|
|
|
if (path1->type == path2->type) {
|
|
|
|
switch(path1->type) {
|
|
|
|
case DEVICE_PATH_NONE:
|
|
|
|
break;
|
2003-10-11 08:20:25 +02:00
|
|
|
case DEVICE_PATH_ROOT:
|
|
|
|
equal = 1;
|
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
case DEVICE_PATH_PCI:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->pci.devfn == path2->pci.devfn);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_PNP:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->pnp.port == path2->pnp.port) &&
|
|
|
|
(path1->pnp.device == path2->pnp.device);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_I2C:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->i2c.device == path2->i2c.device);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
2004-10-14 23:25:53 +02:00
|
|
|
case DEVICE_PATH_APIC:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->apic.apic_id == path2->apic.apic_id);
|
2004-10-14 23:25:53 +02:00
|
|
|
break;
|
2004-10-16 08:20:29 +02:00
|
|
|
case DEVICE_PATH_PCI_DOMAIN:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->pci_domain.domain == path2->pci_domain.domain);
|
2004-10-16 08:20:29 +02:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_APIC_CLUSTER:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->apic_cluster.cluster == path2->apic_cluster.cluster);
|
2004-10-16 08:20:29 +02:00
|
|
|
break;
|
2004-11-18 23:38:08 +01:00
|
|
|
case DEVICE_PATH_CPU:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->cpu.id == path2->cpu.id);
|
2004-11-18 23:38:08 +01:00
|
|
|
break;
|
|
|
|
case DEVICE_PATH_CPU_BUS:
|
2009-02-28 21:10:20 +01:00
|
|
|
equal = (path1->cpu_bus.id == path2->cpu_bus.id);
|
2004-11-18 23:38:08 +01:00
|
|
|
break;
|
2003-09-02 05:36:25 +02:00
|
|
|
default:
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
|
2003-09-02 05:36:25 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return equal;
|
|
|
|
}
|
2004-03-11 16:01:31 +01:00
|
|
|
|
2010-05-21 16:33:48 +02:00
|
|
|
/**
|
|
|
|
* Allocate 64 more resources to the free list.
|
|
|
|
*/
|
|
|
|
static int allocate_more_resources(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct resource *new_res_list;
|
|
|
|
new_res_list = malloc(64 * sizeof(*new_res_list));
|
|
|
|
|
|
|
|
if (new_res_list == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
memset(new_res_list, 0, 64 * sizeof(*new_res_list));
|
|
|
|
|
|
|
|
for (i = 0; i < 64-1; i++)
|
|
|
|
new_res_list[i].next = &new_res_list[i+1];
|
|
|
|
|
|
|
|
free_resources = new_res_list;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove resource res from the device's list and add it to the free list.
|
|
|
|
*/
|
|
|
|
static void free_resource(device_t dev, struct resource *res, struct resource *prev)
|
|
|
|
{
|
|
|
|
if (prev)
|
|
|
|
prev->next = res->next;
|
|
|
|
else
|
|
|
|
dev->resource_list = res->next;
|
|
|
|
res->next = free_resources;
|
|
|
|
free_resources = res;
|
|
|
|
}
|
|
|
|
|
2004-03-11 16:01:31 +01:00
|
|
|
/**
|
|
|
|
* See if we have unused but allocated resource structures.
|
|
|
|
* If so remove the allocation.
|
|
|
|
* @param dev The device to find the resource on
|
|
|
|
*/
|
|
|
|
void compact_resources(device_t dev)
|
|
|
|
{
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res, *next, *prev = NULL;
|
2004-03-11 16:01:31 +01:00
|
|
|
/* Move all of the free resources to the end */
|
2010-06-10 00:41:35 +02:00
|
|
|
for (res = dev->resource_list; res; res = next) {
|
2010-05-21 16:33:48 +02:00
|
|
|
next = res->next;
|
|
|
|
if (!res->flags)
|
|
|
|
free_resource(dev, res, prev);
|
|
|
|
else
|
|
|
|
prev = res;
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
|
2004-03-11 16:01:31 +01:00
|
|
|
/**
|
2004-10-14 23:25:53 +02:00
|
|
|
* See if a resource structure already exists for a given index
|
2004-03-11 16:01:31 +01:00
|
|
|
* @param dev The device to find the resource on
|
|
|
|
* @param index The index of the resource on the device.
|
2004-10-14 23:25:53 +02:00
|
|
|
* @return the resource if it already exists
|
2004-03-11 16:01:31 +01:00
|
|
|
*/
|
2004-10-14 23:25:53 +02:00
|
|
|
struct resource *probe_resource(device_t dev, unsigned index)
|
2004-03-11 16:01:31 +01:00
|
|
|
{
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res;
|
2004-03-11 16:01:31 +01:00
|
|
|
/* See if there is a resource with the appropriate index */
|
2010-06-10 00:41:35 +02:00
|
|
|
for (res = dev->resource_list; res; res = res->next) {
|
2010-05-21 16:33:48 +02:00
|
|
|
if (res->index == index)
|
2004-03-11 16:01:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-05-21 16:33:48 +02:00
|
|
|
return res;
|
2004-10-14 23:25:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* See if a resource structure already exists for a given index and if
|
|
|
|
* not allocate one. Then initialize the initialize the resource
|
|
|
|
* to default values.
|
|
|
|
* @param dev The device to find the resource on
|
|
|
|
* @param index The index of the resource on the device.
|
|
|
|
*/
|
|
|
|
struct resource *new_resource(device_t dev, unsigned index)
|
|
|
|
{
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *resource, *tail;
|
2004-10-14 23:25:53 +02:00
|
|
|
|
|
|
|
/* First move all of the free resources to the end */
|
|
|
|
compact_resources(dev);
|
|
|
|
|
|
|
|
/* See if there is a resource with the appropriate index */
|
|
|
|
resource = probe_resource(dev, index);
|
2004-03-11 16:01:31 +01:00
|
|
|
if (!resource) {
|
2010-05-21 16:33:48 +02:00
|
|
|
if (free_resources == NULL && !allocate_more_resources())
|
|
|
|
die("Couldn't allocate more resources.");
|
|
|
|
|
|
|
|
resource = free_resources;
|
|
|
|
free_resources = free_resources->next;
|
2004-03-11 16:01:31 +01:00
|
|
|
memset(resource, 0, sizeof(*resource));
|
2010-05-21 16:33:48 +02:00
|
|
|
resource->next = NULL;
|
|
|
|
tail = dev->resource_list;
|
|
|
|
if (tail) {
|
|
|
|
while (tail->next) tail = tail->next;
|
|
|
|
tail->next = resource;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dev->resource_list = resource;
|
2004-03-11 16:01:31 +01:00
|
|
|
}
|
|
|
|
/* Initialize the resource values */
|
|
|
|
if (!(resource->flags & IORESOURCE_FIXED)) {
|
|
|
|
resource->flags = 0;
|
|
|
|
resource->base = 0;
|
|
|
|
}
|
|
|
|
resource->size = 0;
|
|
|
|
resource->limit = 0;
|
|
|
|
resource->index = index;
|
|
|
|
resource->align = 0;
|
|
|
|
resource->gran = 0;
|
|
|
|
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
/**
|
|
|
|
* Return an existing resource structure for a given index.
|
|
|
|
* @param dev The device to find the resource on
|
|
|
|
* @param index The index of the resource on the device.
|
|
|
|
*/
|
|
|
|
struct resource *find_resource(device_t dev, unsigned index)
|
|
|
|
{
|
|
|
|
struct resource *resource;
|
|
|
|
|
|
|
|
/* See if there is a resource with the appropriate index */
|
|
|
|
resource = probe_resource(dev, index);
|
|
|
|
if (!resource) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_EMERG, "%s missing resource: %02x\n",
|
2004-10-14 23:25:53 +02:00
|
|
|
dev_path(dev), index);
|
|
|
|
die("");
|
|
|
|
}
|
|
|
|
return resource;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief round a number up to the next multiple of gran
|
|
|
|
* @param val the starting value
|
|
|
|
* @param gran granularity we are aligning the number to.
|
|
|
|
* @returns aligned value
|
|
|
|
*/
|
|
|
|
static resource_t align_up(resource_t val, unsigned long gran)
|
|
|
|
{
|
|
|
|
resource_t mask;
|
|
|
|
mask = (1ULL << gran) - 1ULL;
|
|
|
|
val += mask;
|
|
|
|
val &= ~mask;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief round a number up to the previous multiple of gran
|
|
|
|
* @param val the starting value
|
|
|
|
* @param gran granularity we are aligning the number to.
|
|
|
|
* @returns aligned value
|
|
|
|
*/
|
|
|
|
static resource_t align_down(resource_t val, unsigned long gran)
|
|
|
|
{
|
|
|
|
resource_t mask;
|
|
|
|
mask = (1ULL << gran) - 1ULL;
|
|
|
|
val &= ~mask;
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute the maximum address that is part of a resource
|
|
|
|
* @param resource the resource whose limit is desired
|
|
|
|
* @returns the end
|
|
|
|
*/
|
|
|
|
resource_t resource_end(struct resource *resource)
|
|
|
|
{
|
|
|
|
resource_t base, end;
|
|
|
|
/* get the base address */
|
|
|
|
base = resource->base;
|
|
|
|
|
|
|
|
/* For a non bridge resource granularity and alignment are the same.
|
|
|
|
* For a bridge resource align is the largest needed alignment below
|
|
|
|
* the bridge. While the granularity is simply how many low bits of the
|
|
|
|
* address cannot be set.
|
|
|
|
*/
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
/* Get the end (rounded up) */
|
|
|
|
end = base + align_up(resource->size, resource->gran) - 1;
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Compute the maximum legal value for resource->base
|
|
|
|
* @param resource the resource whose maximum is desired
|
|
|
|
* @returns the maximum
|
|
|
|
*/
|
|
|
|
resource_t resource_max(struct resource *resource)
|
|
|
|
{
|
|
|
|
resource_t max;
|
|
|
|
|
|
|
|
max = align_down(resource->limit - resource->size + 1, resource->align);
|
|
|
|
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
2005-07-08 04:49:49 +02:00
|
|
|
/**
|
|
|
|
* @brief return the resource type of a resource
|
|
|
|
* @param resource the resource type to decode.
|
|
|
|
*/
|
|
|
|
const char *resource_type(struct resource *resource)
|
|
|
|
{
|
|
|
|
static char buffer[RESOURCE_TYPE_MAX];
|
|
|
|
sprintf(buffer, "%s%s%s%s",
|
|
|
|
((resource->flags & IORESOURCE_READONLY)? "ro": ""),
|
|
|
|
((resource->flags & IORESOURCE_PREFETCH)? "pref":""),
|
|
|
|
((resource->flags == 0)? "unused":
|
|
|
|
(resource->flags & IORESOURCE_IO)? "io":
|
|
|
|
(resource->flags & IORESOURCE_DRQ)? "drq":
|
|
|
|
(resource->flags & IORESOURCE_IRQ)? "irq":
|
|
|
|
(resource->flags & IORESOURCE_MEM)? "mem":"??????"),
|
|
|
|
((resource->flags & IORESOURCE_PCI64)?"64":""));
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2004-10-14 23:25:53 +02:00
|
|
|
/**
|
|
|
|
* @brief print the resource that was just stored.
|
|
|
|
* @param dev the device the stored resorce lives on
|
|
|
|
* @param resource the resource that was just stored.
|
|
|
|
*/
|
2005-07-08 04:49:49 +02:00
|
|
|
void report_resource_stored(device_t dev, struct resource *resource, const char *comment)
|
2004-10-14 23:25:53 +02:00
|
|
|
{
|
|
|
|
if (resource->flags & IORESOURCE_STORED) {
|
2007-10-24 00:17:45 +02:00
|
|
|
char buf[10];
|
2004-10-14 23:25:53 +02:00
|
|
|
unsigned long long base, end;
|
|
|
|
base = resource->base;
|
|
|
|
end = resource_end(resource);
|
|
|
|
buf[0] = '\0';
|
|
|
|
if (resource->flags & IORESOURCE_PCI_BRIDGE) {
|
2009-06-30 17:17:49 +02:00
|
|
|
#if CONFIG_PCI_BUS_SEGN_BITS
|
2010-06-10 00:41:35 +02:00
|
|
|
sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link_list->secondary & 0xff);
|
2006-10-05 00:56:21 +02:00
|
|
|
#else
|
2010-06-10 00:41:35 +02:00
|
|
|
sprintf(buf, "bus %02x ", dev->link_list->secondary);
|
2006-10-05 00:56:21 +02:00
|
|
|
#endif
|
2004-10-14 23:25:53 +02:00
|
|
|
}
|
2010-04-27 08:56:47 +02:00
|
|
|
printk(BIOS_DEBUG,
|
2009-02-09 18:52:54 +01:00
|
|
|
"%s %02lx <- [0x%010Lx - 0x%010Lx] size 0x%08Lx gran 0x%02x %s%s%s\n",
|
2004-10-14 23:25:53 +02:00
|
|
|
dev_path(dev),
|
|
|
|
resource->index,
|
|
|
|
base, end,
|
2007-10-16 20:21:22 +02:00
|
|
|
resource->size, resource->gran,
|
2004-10-14 23:25:53 +02:00
|
|
|
buf,
|
2005-07-08 04:49:49 +02:00
|
|
|
resource_type(resource),
|
2004-10-14 23:25:53 +02:00
|
|
|
comment);
|
|
|
|
}
|
|
|
|
}
|
2004-10-30 10:05:41 +02:00
|
|
|
|
|
|
|
void search_bus_resources(struct bus *bus,
|
|
|
|
unsigned long type_mask, unsigned long type,
|
|
|
|
resource_search_t search, void *gp)
|
|
|
|
{
|
|
|
|
struct device *curdev;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (curdev = bus->children; curdev; curdev = curdev->sibling) {
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res;
|
2004-10-30 10:05:41 +02:00
|
|
|
/* Ignore disabled devices */
|
2009-07-02 20:56:24 +02:00
|
|
|
if (!curdev->enabled) continue;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (res = curdev->resource_list; res; res = res->next) {
|
2004-10-30 10:05:41 +02:00
|
|
|
/* If it isn't the right kind of resource ignore it */
|
2010-05-21 16:33:48 +02:00
|
|
|
if ((res->flags & type_mask) != type) {
|
2004-10-30 10:05:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* If it is a subtractive resource recurse */
|
2010-05-21 16:33:48 +02:00
|
|
|
if (res->flags & IORESOURCE_SUBTRACTIVE) {
|
2004-10-30 10:05:41 +02:00
|
|
|
struct bus * subbus;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (subbus = curdev->link_list; subbus; subbus = subbus->next)
|
|
|
|
if (subbus->link_num == IOINDEX_SUBTRACTIVE_LINK(res->index))
|
|
|
|
break;
|
2004-10-30 10:05:41 +02:00
|
|
|
search_bus_resources(subbus, type_mask, type, search, gp);
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-21 16:33:48 +02:00
|
|
|
search(gp, curdev, res);
|
2004-10-30 10:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void search_global_resources(
|
|
|
|
unsigned long type_mask, unsigned long type,
|
|
|
|
resource_search_t search, void *gp)
|
|
|
|
{
|
|
|
|
struct device *curdev;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (curdev = all_devices; curdev; curdev = curdev->next) {
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res;
|
2004-10-30 10:05:41 +02:00
|
|
|
/* Ignore disabled devices */
|
2009-07-02 20:56:24 +02:00
|
|
|
if (!curdev->enabled) continue;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (res = curdev->resource_list; res; res = res->next) {
|
2004-10-30 10:05:41 +02:00
|
|
|
/* If it isn't the right kind of resource ignore it */
|
2010-05-21 16:33:48 +02:00
|
|
|
if ((res->flags & type_mask) != type) {
|
2004-10-30 10:05:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* If it is a subtractive resource ignore it */
|
2010-05-21 16:33:48 +02:00
|
|
|
if (res->flags & IORESOURCE_SUBTRACTIVE) {
|
2004-10-30 10:05:41 +02:00
|
|
|
continue;
|
|
|
|
}
|
2010-05-21 16:33:48 +02:00
|
|
|
search(gp, curdev, res);
|
2004-10-30 10:05:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-08 04:49:49 +02:00
|
|
|
|
|
|
|
void dev_set_enabled(device_t dev, int enable)
|
|
|
|
{
|
|
|
|
if (dev->enabled == enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dev->enabled = enable;
|
|
|
|
if (dev->ops && dev->ops->enable) {
|
|
|
|
dev->ops->enable(dev);
|
|
|
|
}
|
|
|
|
else if (dev->chip_ops && dev->chip_ops->enable_dev) {
|
|
|
|
dev->chip_ops->enable_dev(dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable_children(struct bus *bus)
|
|
|
|
{
|
|
|
|
device_t child;
|
2010-06-10 00:41:35 +02:00
|
|
|
for (child = bus->children; child; child = child->sibling) {
|
|
|
|
struct bus *link;
|
|
|
|
for (link = child->link_list; link; link = link->next) {
|
|
|
|
disable_children(link);
|
2005-07-08 04:49:49 +02:00
|
|
|
}
|
|
|
|
dev_set_enabled(child, 0);
|
|
|
|
}
|
|
|
|
}
|
2009-05-12 00:45:35 +02:00
|
|
|
|
2009-10-27 15:29:29 +01:00
|
|
|
static void resource_tree(struct device *root, int debug_level, int depth)
|
2009-05-12 00:45:35 +02:00
|
|
|
{
|
2010-06-10 00:41:35 +02:00
|
|
|
int i = 0;
|
2009-05-12 00:45:35 +02:00
|
|
|
struct device *child;
|
2010-06-10 00:41:35 +02:00
|
|
|
struct bus *link;
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res;
|
2009-05-12 00:45:35 +02:00
|
|
|
char indent[30]; /* If your tree has more levels, it's wrong. */
|
|
|
|
|
|
|
|
for (i = 0; i < depth + 1 && i < 29; i++)
|
|
|
|
indent[i] = ' ';
|
|
|
|
indent[i] = '\0';
|
|
|
|
|
2010-06-10 00:41:35 +02:00
|
|
|
do_printk(BIOS_DEBUG, "%s%s", indent, dev_path(root));
|
|
|
|
if (root->link_list && root->link_list->children)
|
|
|
|
do_printk(BIOS_DEBUG, " child on link 0 %s",
|
|
|
|
dev_path(root->link_list->children));
|
|
|
|
do_printk(BIOS_DEBUG, "\n");
|
|
|
|
|
2010-05-21 16:33:48 +02:00
|
|
|
for (res = root->resource_list; res; res = res->next) {
|
2010-06-02 23:13:44 +02:00
|
|
|
do_printk(debug_level,
|
2009-05-12 00:45:35 +02:00
|
|
|
"%s%s resource base %llx size %llx align %d gran %d limit %llx flags %lx index %lx\n",
|
2010-05-21 16:33:48 +02:00
|
|
|
indent, dev_path(root), res->base,
|
|
|
|
res->size, res->align,
|
|
|
|
res->gran, res->limit,
|
|
|
|
res->flags, res->index);
|
2009-05-12 00:45:35 +02:00
|
|
|
}
|
|
|
|
|
2010-06-10 00:41:35 +02:00
|
|
|
for (link = root->link_list; link; link = link->next) {
|
|
|
|
for (child = link->children; child; child = child->sibling)
|
2009-05-12 00:45:35 +02:00
|
|
|
resource_tree(child, debug_level, depth + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_resource_tree(struct device * root, int debug_level,
|
|
|
|
const char *msg)
|
|
|
|
{
|
|
|
|
/* Bail if root is null. */
|
|
|
|
if (!root) {
|
|
|
|
do_printk(debug_level, "%s passed NULL for root!\n", __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bail if not printing to screen. */
|
|
|
|
if (!do_printk(debug_level, "Show resources in subtree (%s)...%s\n",
|
|
|
|
dev_path(root), msg))
|
|
|
|
return;
|
|
|
|
resource_tree(root, debug_level, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_devs_tree(struct device *dev, int debug_level, int depth, int linknum)
|
|
|
|
{
|
|
|
|
char depth_str[20] = "";
|
|
|
|
int i;
|
|
|
|
struct device *sibling;
|
2010-06-10 00:41:35 +02:00
|
|
|
struct bus *link;
|
|
|
|
|
2009-05-12 00:45:35 +02:00
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
depth_str[i] = ' ';
|
|
|
|
depth_str[i] = '\0';
|
2010-05-21 16:33:48 +02:00
|
|
|
do_printk(debug_level, "%s%s: enabled %d\n",
|
|
|
|
depth_str, dev_path(dev), dev->enabled);
|
2010-06-10 00:41:35 +02:00
|
|
|
for (link = dev->link_list; link; link = link->next) {
|
|
|
|
for (sibling = link->children; sibling;
|
2009-05-12 00:45:35 +02:00
|
|
|
sibling = sibling->sibling)
|
|
|
|
show_devs_tree(sibling, debug_level, depth + 1, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_all_devs_tree(int debug_level, const char *msg)
|
|
|
|
{
|
|
|
|
/* Bail if not printing to screen. */
|
|
|
|
if (!do_printk(debug_level, "Show all devs in tree form...%s\n", msg))
|
|
|
|
return;
|
|
|
|
show_devs_tree(all_devices, debug_level, 0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_devs_subtree(struct device *root, int debug_level, const char *msg)
|
|
|
|
{
|
|
|
|
/* Bail if not printing to screen. */
|
|
|
|
if (!do_printk(debug_level, "Show all devs in subtree %s...%s\n",
|
|
|
|
dev_path(root), msg))
|
|
|
|
return;
|
|
|
|
do_printk(debug_level, "%s\n", msg);
|
|
|
|
show_devs_tree(root, debug_level, 0, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_all_devs(int debug_level, const char *msg)
|
|
|
|
{
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
/* Bail if not printing to screen. */
|
|
|
|
if (!do_printk(debug_level, "Show all devs...%s\n", msg))
|
|
|
|
return;
|
|
|
|
for (dev = all_devices; dev; dev = dev->next) {
|
2010-05-21 16:33:48 +02:00
|
|
|
do_printk(debug_level, "%s: enabled %d\n",
|
|
|
|
dev_path(dev), dev->enabled);
|
2009-05-12 00:45:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_one_resource(int debug_level, struct device *dev,
|
|
|
|
struct resource *resource, const char *comment)
|
|
|
|
{
|
|
|
|
char buf[10];
|
|
|
|
unsigned long long base, end;
|
|
|
|
base = resource->base;
|
|
|
|
end = resource_end(resource);
|
|
|
|
buf[0] = '\0';
|
|
|
|
/*
|
|
|
|
if (resource->flags & IORESOURCE_BRIDGE) {
|
2009-06-30 17:17:49 +02:00
|
|
|
#if CONFIG_PCI_BUS_SEGN_BITS
|
2009-05-12 00:45:35 +02:00
|
|
|
sprintf(buf, "bus %04x:%02x ", dev->bus->secondary >> 8,
|
|
|
|
dev->link[0].secondary & 0xff);
|
|
|
|
#else
|
|
|
|
sprintf(buf, "bus %02x ", dev->link[0].secondary);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
do_printk(debug_level, "%s %02lx <- [0x%010llx - 0x%010llx] "
|
|
|
|
"size 0x%08Lx gran 0x%02x %s%s%s\n",
|
|
|
|
dev_path(dev), resource->index, base, end,
|
|
|
|
resource->size, resource->gran, buf,
|
|
|
|
resource_type(resource), comment);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void show_all_devs_resources(int debug_level, const char* msg)
|
|
|
|
{
|
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
if(!do_printk(debug_level, "Show all devs with resources...%s\n", msg))
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (dev = all_devices; dev; dev = dev->next) {
|
2010-05-21 16:33:48 +02:00
|
|
|
struct resource *res;
|
|
|
|
do_printk(debug_level, "%s: enabled %d\n",
|
|
|
|
dev_path(dev), dev->enabled);
|
|
|
|
for (res = dev->resource_list; res; res = res->next)
|
|
|
|
show_one_resource(debug_level, dev, res, "");
|
2009-05-12 00:45:35 +02:00
|
|
|
}
|
|
|
|
}
|