switch-coreboot/device/device_util.c
Ronald G. Minnich 446979be46 This set of changes:
- makes sprintf work, in a re-entrant way
- gets rid of a lot of ugly debug stuff (thanks for the Kconfig log
  level stuff guys!)
- boots like a bat out of hell on qemu :-)

The changes are trivial, I am self-acking. 

To run this under qemu, I do this:

cp LinuxBIOSv3/build/linuxbios.rom bios.bin ;  qemu -nographic -L .
-kernel linux-2.6.15-bochs/vmlinux -hdc hda -hda hda | tee  out 2>&1

here is the output from a run. Note that cpu's still don't have a path
of any kind, and that show_all_devs is not working right. But we're
getting there. I think I'll next try to really boot a linux with a hard
drive, and see how it goes.


LinuxBIOS-3.0.0- Tue Mar  6 18:41:59 MST 2007 starting...
Start search at 0xfffc0000, size 258048
filename is normal/initram
start 0xfffc0000 len 0x3f000
fullname is normal/initram
RAM init code started
Nothing to do.Done ram init code
filename is normal/stage2
start (qemu) 0xfffc0000 len 0x3f000
fullname is normal/initram
fullname is normal/stage2
Phase 1: done
show all devs..
cpus: Unknown device path type: 0
Phase 2: Early setup...
Phase 2: Done.
show all devs..
cpus: Unknown device path type: 0
Phase 3: Enumerating buses...
qemu-x86 enable_dev done
dev_phase3_scan: scanning root(Root Device)
scan_static_bus for root(Root Device)
cpus: Unknown device path type: 0
i440bxemulation_enable_dev: 
i440bxemulation_enable_dev: Done.
dev_phase5: device0_0(PCI: 00:00.0) missing ops
domain0(PCI_DOMAIN: 0000) scanning...
dev_phase3_scan: scanning domain0(PCI_DOMAIN: 0000)
set_pci_ops: dev 00007aa0(device0_0) set ops to 00007220
set_pci_ops: dev 00007d80(dynamic PCI: 00:01.0) set ops to 00007220
set_pci_ops: dev 00008040(dynamic PCI: 00:01.1) set ops to 00007220
set_pci_ops: dev 00008300(dynamic PCI: 00:01.3) set ops to 00007220
set_pci_ops: dev 000085c0(dynamic PCI: 00:02.0) set ops to 00007220
set_pci_ops: dev 00008880(dynamic PCI: 00:03.0) set ops to 00007220
dev_phase3_scan: device0_0: busdevice 00007aa0 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: dynamic PCI: 00:01.0: busdevice 00007d80 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: dynamic PCI: 00:01.1: busdevice 00008040 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: dynamic PCI: 00:01.3: busdevice 00008300 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: dynamic PCI: 00:02.0: busdevice 000085c0 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: dynamic PCI: 00:03.0: busdevice 00008880 enabled 1 ops 00007220
dev_phase3_scan: can not scan from here, returning 0
dev_phase3_scan: returning 0
scan_static_bus for root(Root Device) done
dev_phase3_scan: returning 0
Phase 3: Done.
show all devs..
cpus: Unknown device path type: 0
Phase 4: Allocating resources...
Phase 4: Reading resources...
cpus: Unknown device path type: 0
read_resources: cpus() missing phase4_read_resources
cpus: Unknown device path type: 0
read_resources: cpus() missing phase4_read_resources
Phase 4: Done reading resources.
Phase 4: Setting resources...
cpus: Unknown device path type: 0
read_resources: cpus() missing phase4_read_resources
cpus: Unknown device path type: 0
read_resources: cpus() missing phase4_read_resources
ram_resource: add ram resoource 33554432 bytes
Phase 4: Done setting resources.
Phase 4: Done allocating resources.
show all devs..
cpus: Unknown device path type: 0
Phase 5: Enabling resources...
cpus: Unknown device path type: 0
dev_phase5: cpus() missing ops
Phase 5: Done.
show all devs..
cpus: Unknown device path type: 0
Phase 6: Initializing devices...
Phase 6: Devices initialized.
show all devs..
cpus: Unknown device path type: 0
lb_memory_range: start 0x0 size 0x2000000
lb_cleanup_memory_ranges: # entries 1
  #0: base 0x0 size 0x2000000
lb_memory_range: start 0x0 size 0x500
lb_cleanup_memory_ranges: # entries 2
  #0: base 0x500 size 0x1fffb00
  #1: base 0x0 size 0x500
lb_memory_range: start 0xf0000 size 0x0
lb_cleanup_memory_ranges: # entries 4
  #0: base 0x0 size 0x500
  #1: base 0x500 size 0xefb00
  #2: base 0xf0000 size 0x1f10000
  #3: base 0xf0000 size 0x0
show all devs..
cpus: Unknown device path type: 0
Done stage2 code
filename is normal/payload
start 0xfffc0000 len 0x3f000
fullname is normal/initram
fullname is normal/stage2
fullname is normal/payload

Elfboot
set 00100000 to 0 for 137936 bytes
Copy to 00100000 from fffc6ef4 for 30472 bytes
set 00121ae0 to 0 for 72 bytes
Copy to 00121ae0 from fffce614 for 72 bytes
FILO version 0.5 (rminnich@q.ccstar.lanl.gov) Tue Mar  6 18:41:56 MST 2007
collect_sys_info: boot eax = 0xfe
collect_sys_info: boot ebx = 0xffffd444
collect_sys_info: boot arg = 0x1049c0
collect_sys_info: info->memrange 00000000
collect_linuxbios_info: Searching for LinuxBIOS tables...
find_lb_table: Found candidate at: 00000500
find_lb_table: header checksum o.k.
find_lb_table: table checksum o.k.
find_lb_table: record count o.k.
collect_linuxbios_info: collect_linuxbios_info: yes
collect_linuxbios_info: Found LinuxBIOS table at: 00000500
read_lbtable: read_lbtable begin
read_lbtable: lbrec tag 1 LB_TAG_MEMORY 1
convert_memmap: info->memrange 0x107740
convert_memmap: 0x00000000000000 0x000000000005a4 16
convert_memmap: 0x000000000005a4 0x000000000efa5c 1
convert_memmap: 0x000000000f0000 0x00000001f10000 1
convert_memmap: 0x000000000f0000 0x00000000000000 16
read_lbtable: lbrec tag 3 LB_TAG_MEMORY 1
read_lbtable: lbrec tag 4 LB_TAG_MEMORY 1
read_lbtable: read_lbtable end
collect_linuxbios_info: collect_linuxbios_info: done
collect_sys_info: after collect info->memrange 00107740
collect_sys_info: 00000000000005a4-00000000000f0000
collect_sys_info: 00000000000f0000-0000000002000000
collect_sys_info: RAM 32 MB
relocate: virt_offset is 0
relocate: Current location: 0x100000-0x121b27
relocate: Relocating to 0x1fde4d0-0x1fffff7... ok
setup_timers: CPU 599 MHz
Press <Enter> for default boot, or <Esc> for boot prompt... 2 1 timed out
boot: hda:/bzImage console=ttyS0,115200
malloc_diag: alloc: 0 bytes (0 blocks), free: 16376 bytes (1 blocks)
malloc_diag: alloc: 48 bytes (1 blocks), free: 16328 bytes (1 blocks)
malloc_diag: alloc: 64 bytes (2 blocks), free: 16312 bytes (1 blocks)
file_open: dev=hda, path=/bzImage
ide_software_reset: Waiting for ide0 to become ready for reset... ok
init_drive: Testing for hda
init_drive: Probing for hda
init_drive: LBA mode, sectors=32768
init_drive: LBA48 mode, sectors=32768
init_drive: Init device params... ok
hda: LBA48 16MB: QEMU HARDDISK                           
Mounted ext2fs
malloc_diag: alloc: 48 bytes (1 blocks), free: 16328 bytes (1 blocks)
elf_load: Not a bootable ELF image
malloc_diag: alloc: 64 bytes (2 blocks), free: 16312 bytes (1 blocks)
file_open: dev=hda, path=/bzImage
devopen: already open
malloc_diag: alloc: 48 bytes (1 blocks), free: 16328 bytes (1 blocks)
Found Linux version 2.6.15-geode (rminnich@q.ccstar.lanl.gov) #36 Sun Mar 4 22:20:47 MST 2007 (protocol 0x204) (loadflags 0x1) bzImage.
init_linux_params: Setting up paramters at 0x90000
set_memory_size: 00000000000005a4 - 00000000000f0000
set_memory_size: 00000000000f0000 - 0000000002000000
set_memory_size: ramtop=0x2000000
set_memory_size: ext_mem_k=31744, alt_mem_k=31744
parse_command_line: original command line: "console=ttyS0,115200"
parse_command_line: kernel command line at 0x91000
parse_command_line: kernel command line (20 bytes): "console=ttyS0,115200"
load_linux_kernel: offset=0x1600 addr=0x100000 size=0x1175e1
Loading kernel... ok
start_linux: eip=0x100000
Jumping to entry point...
Linux version 2.6.15-geode (rminnich@q.ccstar.lanl.gov) (gcc version 4.0.2 20051125 (Red Hat 4.0.2-8)) #36 Sun Mar 4 22:20:47 MST 2007
BIOS-provided physical RAM map:
 BIOS-e801: 0000000000000000 - 000000000009f000 (usable)
 BIOS-e801: 0000000000100000 - 0000000002000000 (usable)
32MB LOWMEM available.
DMI not present.
Allocating PCI resources starting at 10000000 (gap: 02000000:fe000000)
Built 1 zonelists
Kernel command line: console=ttyS0,115200
Initializing CPU#0
PID hash table entries: 256 (order: 8, 4096 bytes)
Detected 1694.987 MHz processor.
Using pit for high-res timesource
Console: colour dummy device 80x25
Dentry cache hash table entries: 8192 (order: 3, 32768 bytes)
Inode-cache hash table entries: 4096 (order: 2, 16384 bytes)
Memory: 29752k/32768k available (1500k kernel code, 2628k reserved, 502k data, 144k init, 0k highmem)
Checking if this processor honours the WP bit even in supervisor mode... Ok.
Mount-cache hash table entries: 512
CPU: L1 I cache: 8K
CPU: L2 cache: 128K
mtrr: v2.0 (20020519)
CPU: Intel Pentium II (Klamath) stepping 03
Enabling fast FPU save and restore... done.
Enabling unmasked SIMD FPU exception support... done.
Checking 'hlt' instruction... OK.
Checking for popad bug... OK.
NET: Registered protocol family 16
PCI: Using configuration type 1
PCI: Probing PCI hardware
PCI quirk: region b100-b10f claimed by PIIX4 SMB
PCI: Ignore bogus resource 6 [0:0] of 0000:00:02.0
Initializing Cryptographic API
io scheduler noop registered
io scheduler anticipatory registered
io scheduler deadline registered
io scheduler cfq registered
PCI: PIIX3: Enabling Passive Release on 0000:00:01.0
Limiting direct PCI/PCI transfers.
Activating ISA DMA hang workarounds.
serio: i8042 AUX port at 0x60,0x64 irq 12
serio: i8042 KBD port at 0x60,0x64 irq 1
Serial: 8250/16550 driver $Revision: 1.90 $ 4 ports, IRQ sharing disabled
serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16450
RAMDISK driver initialized: 16 RAM disks of 4096K size 1024 blocksize
loop: loaded (max 8 devices)
e100: Intel(R) PRO/100 Network Driver, 3.4.14-k4-NAPI
e100: Copyright(c) 1999-2005 Intel Corporation
tun: Universal TUN/TAP device driver, 1.6
tun: (C) 1999-2004 Max Krasnyansky <maxk@qualcomm.com>
Uniform Multi-Platform E-IDE driver Revision: 7.00alpha2
ide: Assuming 33MHz system bus speed for PIO modes; override with idebus=xx
PIIX3: IDE controller at PCI slot 0000:00:01.1
PIIX3: chipset revision 0
PIIX3: not 100% native mode: will probe irqs later
PIIX3: neither IDE port enabled (BIOS)
hda: QEMU HARDDISK, ATA DISK drive
hdc: QEMU HARDDISK, ATA DISK drive
ide0 at 0x1f0-0x1f7,0x3f6 on irq 14
ide1 at 0x170-0x177,0x376 on irq 15
hda: max request size: 1024KiB
hda: 32768 sectors (16 MB) w/256KiB Cache, CHS=32/255/63
hda: set_multmode: status=0x41 { DriveReady Error }
hda: set_multmode: error=0x04 { DriveStatusError }
ide: failed opcode was: 0xef
hda: cache flushes supported
 hda: hda1
hdc: max request size: 1024KiB
hdc: 32768 sectors (16 MB) w/256KiB Cache, CHS=32/255/63
hdc: set_multmode: status=0x41 { DriveReady Error }
hdc: set_multmode: error=0x04 { DriveStatusError }
ide: failed opcode was: 0xef
hdc: cache flushes supported
 hdc: hdc1
mice: PS/2 mouse device common for all mice
NET: Registered protocol family 2
input: AT Raw Set 2 keyboard as /class/input/input0
input: ImExPS/2 Generic Explorer Mouse as /class/input/input1
IP route cache hash table entries: 512 (order: -1, 2048 bytes)
TCP established hash table entries: 2048 (order: 1, 8192 bytes)
TCP bind hash table entries: 2048 (order: 1, 8192 bytes)
TCP: Hash tables configured (established 2048 bind 2048)
TCP reno registered
TCP bic registered
NET: Registered protocol family 1
NET: Registered protocol family 17
Using IPI Shortcut mode
VFS: Cannot open root device "<NULL>" or unknown-block(3,2)
Please append a correct "root=" boot option
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(3,2)
 

Signed-off-by: Ronald G. Minnich <rminnich@gmail.com>
Acked-by: Ronald G. Minnich <rminnich@gmail.com>



git-svn-id: svn://coreboot.org/repository/LinuxBIOSv3@207 f3766cd6-281f-0410-b1cd-43a5c92072e9
2007-03-07 00:53:40 +00:00

556 lines
15 KiB
C

/*
* Author(s) unknown
*
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; either version 2 of the License, or
(at your option) any later version.
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
*/
#include <console/console.h>
#include <arch/io.h>
#include <device/device.h>
#include <device/pci.h>
#include <device/pci_ids.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief See if a device structure exists for path
*
* @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
* or 0/NULL if no device is found
*/
struct device * find_dev_path(struct bus *parent, struct device_path *path)
{
struct device * child;
for(child = parent->children; child; child = child->sibling) {
if (path_eq(path, &child->path)) {
break;
}
}
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
*/
struct device * alloc_find_dev(struct bus *parent, struct device_path *path)
{
struct device * child;
child = find_dev_path(parent, path);
if (!child) {
child = alloc_dev(parent, path);
}
return child;
}
/**
* @brief Given a PCI bus and a devfn number, find the device structure
*
* @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)
{
struct device *dev, *result;
result = 0;
for (dev = all_devices; dev; dev = dev->next) {
if ((dev->path.type == DEVICE_PATH_PCI) &&
(dev->bus->secondary == bus) &&
(dev->path.u.pci.devfn == devfn)) {
result = dev;
break;
}
}
return result;
}
/**
* @brief Given a smbus bus and a device number, find the device structure
*
* @param bus The bus number
* @param addr a device number
* @return pointer to the device structure
*/
struct device *dev_find_slot_on_smbus(unsigned int bus, unsigned int addr)
{
struct device *dev, *result;
result = 0;
for (dev = all_devices; dev; dev = dev->next) {
if ((dev->path.type == DEVICE_PATH_I2C) &&
(dev->bus->secondary == bus) &&
(dev->path.u.i2c.device == addr)) {
result = dev;
break;
}
}
return result;
}
/** 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
* in the linked list of all_devices, which can be 0 to start at the
* head of the list (i.e. all_devices)
* @return Pointer to the device struct
*/
struct device *dev_find_device(unsigned int vendor, unsigned int device, struct device *from)
{
if (!from)
from = all_devices;
else
from = from->next;
while (from && (from->vendor != vendor || from->device != device)) {
from = from->next;
}
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
* in the linked list of all_devices, which can be 0 to start at the
* head of the list (i.e. all_devices)
* @return Pointer to the device struct
*/
struct device *dev_find_class(unsigned int class, struct device *from)
{
if (!from)
from = all_devices;
else
from = from->next;
while (from && (from->class & 0xffffff00) != class)
from = from->next;
return from;
}
const char *dev_path(struct device * dev)
{
static char buffer[DEVICE_PATH_MAX];
buffer[0] = '\0';
if (!dev) {
memcpy(buffer, "<null>", 7);
}
else {
switch(dev->path.type) {
case DEVICE_PATH_ROOT:
memcpy(buffer, "Root Device", 12);
break;
case DEVICE_PATH_PCI:
#if PCI_BUS_SEGN_BITS
sprintf(buffer, "PCI: %04x:%02x:%02x.%01x",
dev->bus->secondary>>8, dev->bus->secondary & 0xff,
PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
#else
sprintf(buffer, "PCI: %02x:%02x.%01x",
dev->bus->secondary,
PCI_SLOT(dev->path.u.pci.devfn), PCI_FUNC(dev->path.u.pci.devfn));
#endif
break;
case DEVICE_PATH_PNP:
sprintf(buffer, "PNP: %04x.%01x",
dev->path.u.pnp.port, dev->path.u.pnp.device);
break;
case DEVICE_PATH_I2C:
sprintf(buffer, "I2C: %02x:%02x",
dev->bus->secondary,
dev->path.u.i2c.device);
break;
case DEVICE_PATH_APIC:
sprintf(buffer, "APIC: %02x",
dev->path.u.apic.apic_id);
break;
case DEVICE_PATH_PCI_DOMAIN:
sprintf(buffer, "PCI_DOMAIN: %04x",
dev->path.u.pci_domain.domain);
break;
case DEVICE_PATH_APIC_CLUSTER:
sprintf(buffer, "APIC_CLUSTER: %01x",
dev->path.u.apic_cluster.cluster);
break;
case DEVICE_PATH_CPU:
sprintf(buffer, "CPU: %02x", dev->path.u.cpu.id);
break;
case DEVICE_PATH_CPU_BUS:
sprintf(buffer, "CPU_BUS: %02x", dev->path.u.cpu_bus.id);
break;
default:
printk(BIOS_ERR, "%s: Unknown device path type: %d\n", dev->dtsname, dev->path.type);
break;
}
}
return buffer;
}
const char *bus_path(struct bus *bus)
{
static char buffer[BUS_PATH_MAX];
sprintf(buffer, "%s,%d",
dev_path(bus->dev), bus->link);
return buffer;
}
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;
case DEVICE_PATH_ROOT:
equal = 1;
break;
case DEVICE_PATH_PCI:
equal = (path1->u.pci.devfn == path2->u.pci.devfn);
break;
case DEVICE_PATH_PNP:
equal = (path1->u.pnp.port == path2->u.pnp.port) &&
(path1->u.pnp.device == path2->u.pnp.device);
break;
case DEVICE_PATH_I2C:
equal = (path1->u.i2c.device == path2->u.i2c.device);
break;
case DEVICE_PATH_APIC:
equal = (path1->u.apic.apic_id == path2->u.apic.apic_id);
break;
case DEVICE_PATH_PCI_DOMAIN:
equal = (path1->u.pci_domain.domain == path2->u.pci_domain.domain);
break;
case DEVICE_PATH_APIC_CLUSTER:
equal = (path1->u.apic_cluster.cluster == path2->u.apic_cluster.cluster);
break;
case DEVICE_PATH_CPU:
equal = (path1->u.cpu.id == path2->u.cpu.id);
break;
case DEVICE_PATH_CPU_BUS:
equal = (path1->u.cpu_bus.id == path2->u.cpu_bus.id);
break;
default:
printk(BIOS_ERR, "Uknown device type: %d\n", path1->type);
break;
}
}
return equal;
}
/**
* 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(struct device * dev)
{
struct resource *resource;
int i;
/* Move all of the free resources to the end */
for(i = 0; i < dev->resources;) {
resource = &dev->resource[i];
if (!resource->flags) {
/* note: memmove was used here. But this can never overlap, right? */
memcpy(resource, resource + 1, dev->resources - i);
dev->resources -= 1;
memset(&dev->resource[dev->resources], 0, sizeof(*resource));
} else {
i++;
}
}
}
/**
* See if a resource structure already exists for a given index
* @param dev The device to find the resource on
* @param index The index of the resource on the device.
* @return the resource if it already exists
*/
struct resource *probe_resource(struct device * dev, unsigned index)
{
struct resource *resource;
int i;
/* See if there is a resource with the appropriate index */
resource = 0;
for(i = 0; i < dev->resources; i++) {
if (dev->resource[i].index == index) {
resource = &dev->resource[i];
break;
}
}
return resource;
}
/**
* 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(struct device * dev, unsigned index)
{
struct resource *resource;
/* 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);
if (!resource) {
if (dev->resources == MAX_RESOURCES) {
die("MAX_RESOURCES exceeded.");
}
resource = &dev->resource[dev->resources];
memset(resource, 0, sizeof(*resource));
dev->resources++;
}
/* 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;
}
/**
* 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(struct device * dev, unsigned index)
{
struct resource *resource;
/* See if there is a resource with the appropriate index */
resource = probe_resource(dev, index);
if (!resource) {
printk(BIOS_EMERG, "%s missing resource: %02x\n",
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.
*/
/* 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;
}
/**
* @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;
}
/**
* @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.
*/
void report_resource_stored(struct device * dev, struct resource *resource, const char *comment)
{
if (resource->flags & IORESOURCE_STORED) {
char buf[10];
unsigned long long base, end;
base = resource->base;
end = resource_end(resource);
buf[0] = '\0';
if (resource->flags & IORESOURCE_PCI_BRIDGE) {
#if PCI_BUS_SEGN_BITS
sprintf(buf, "bus %04x:%02x ", dev->bus->secondary>>8, dev->link[0].secondary & 0xff);
#else
sprintf(buf, "bus %02x ", dev->link[0].secondary);
#endif
}
printk(BIOS_DEBUG,
"%s %02x <- [0x%010Lx - 0x%010Lx] %s%s%s\n",
dev_path(dev),
resource->index,
base, end,
buf,
resource_type(resource),
comment);
}
}
void search_bus_resources(struct bus *bus,
unsigned long type_mask, unsigned long type,
resource_search_t search, void *gp)
{
struct device *curdev;
for(curdev = bus->children; curdev; curdev = curdev->sibling) {
int i;
/* Ignore disabled devices */
if (!curdev->have_resources) continue;
for(i = 0; i < curdev->resources; i++) {
struct resource *resource = &curdev->resource[i];
/* If it isn't the right kind of resource ignore it */
if ((resource->flags & type_mask) != type) {
continue;
}
/* If it is a subtractive resource recurse */
if (resource->flags & IORESOURCE_SUBTRACTIVE) {
struct bus * subbus;
subbus = &curdev->link[IOINDEX_SUBTRACTIVE_LINK(resource->index)];
search_bus_resources(subbus, type_mask, type, search, gp);
continue;
}
search(gp, curdev, resource);
}
}
}
void search_global_resources(
unsigned long type_mask, unsigned long type,
resource_search_t search, void *gp)
{
struct device *curdev;
printk(BIOS_SPEW, "%s: mask %x type %x \n", __func__, type_mask, type);
for(curdev = all_devices; curdev; curdev = curdev->next) {
int i;
printk(BIOS_SPEW, "%s: dev %s, have_resources %d #resources %d\n", __func__, curdev->dtsname,
curdev->have_resources, curdev->resources);
/* Ignore disabled devices */
if (!curdev->have_resources) continue;
for(i = 0; i < curdev->resources; i++) {
struct resource *resource = &curdev->resource[i];
printk(BIOS_SPEW, "%s: dev %s, resource %d, flags %x base 0x%lx size 0x%lx\n", __func__, curdev->dtsname,
i, resource->flags, (u32) resource->base, (u32)resource->size);
/* If it isn't the right kind of resource ignore it */
if ((resource->flags & type_mask) != type) {
continue;
}
/* If it is a subtractive resource ignore it */
if (resource->flags & IORESOURCE_SUBTRACTIVE) {
continue;
}
search(gp, curdev, resource);
}
}
}
void dev_set_enabled(struct device * dev, int enable)
{
if (dev->enabled == enable) {
return;
}
dev->enabled = enable;
if (dev->ops && dev->ops->phase5_enable_resources) {
dev->ops->phase5_enable_resources(dev);
}
else if (dev->chip_ops && dev->chip_ops->enable_dev) {
dev->chip_ops->enable_dev(dev);
}
}
void disable_children(struct bus *bus)
{
struct device * child;
for(child = bus->children; child; child = child->sibling) {
int link;
for(link = 0; link < child->links; link++) {
disable_children(&child->link[link]);
}
dev_set_enabled(child, 0);
}
}