2005-04-14 10:10:54 +00:00
|
|
|
/*
|
2006-05-16 15:12:06 +00:00
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
2005-04-14 10:10:54 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* Based in part on pci.c from Etherboot 5.4, by Ken Yap and David
|
|
|
|
* Munro, in turn based on the Linux kernel's PCI implementation.
|
|
|
|
*
|
|
|
|
* 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 any later version.
|
2005-04-16 11:16:31 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2005-04-16 11:16:31 +00:00
|
|
|
*/
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
#include <stdint.h>
|
2006-12-19 23:42:46 +00:00
|
|
|
#include <stdlib.h>
|
2006-05-16 15:12:06 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <gpxe/tables.h>
|
|
|
|
#include <gpxe/device.h>
|
|
|
|
#include <gpxe/pci.h>
|
2005-04-16 11:16:31 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/** @file
|
2005-04-11 19:19:35 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* PCI bus
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2005-04-11 19:19:35 +00:00
|
|
|
*/
|
2005-04-16 11:16:31 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
static struct pci_driver pci_drivers[0] __table_start ( pci_drivers );
|
|
|
|
static struct pci_driver pci_drivers_end[0] __table_end ( pci_drivers );
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
static void pcibus_remove ( struct root_device *rootdev );
|
2005-04-16 11:42:56 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Read PCI BAR
|
|
|
|
*
|
|
|
|
* @v pci PCI device
|
|
|
|
* @v reg PCI register number
|
|
|
|
* @ret bar Base address register
|
|
|
|
*
|
|
|
|
* Reads the specified PCI base address register, including the flags
|
|
|
|
* portion. 64-bit BARs will be handled automatically. If the value
|
|
|
|
* of the 64-bit BAR exceeds the size of an unsigned long (i.e. if the
|
|
|
|
* high dword is non-zero on a 32-bit platform), then the value
|
|
|
|
* returned will be zero plus the flags for a 64-bit BAR. Unreachable
|
|
|
|
* 64-bit BARs are therefore returned as uninitialised 64-bit BARs.
|
|
|
|
*/
|
|
|
|
static unsigned long pci_bar ( struct pci_device *pci, unsigned int reg ) {
|
|
|
|
uint32_t low;
|
|
|
|
uint32_t high;
|
|
|
|
|
|
|
|
pci_read_config_dword ( pci, reg, &low );
|
|
|
|
if ( ( low & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK) )
|
|
|
|
== (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64) ){
|
|
|
|
pci_read_config_dword ( pci, reg + 4, &high );
|
|
|
|
if ( high ) {
|
|
|
|
if ( sizeof ( unsigned long ) > sizeof ( uint32_t ) ) {
|
|
|
|
return ( ( ( uint64_t ) high << 32 ) | low );
|
|
|
|
} else {
|
2006-12-20 00:34:53 +00:00
|
|
|
DBG ( "Unhandled 64-bit BAR %08lx%08lx\n",
|
2006-05-16 15:12:06 +00:00
|
|
|
high, low );
|
|
|
|
return PCI_BASE_ADDRESS_MEM_TYPE_64;
|
2005-04-11 19:19:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-05-16 15:12:06 +00:00
|
|
|
return low;
|
2005-04-11 19:19:35 +00:00
|
|
|
}
|
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Find the start of a PCI BAR
|
|
|
|
*
|
|
|
|
* @v pci PCI device
|
|
|
|
* @v reg PCI register number
|
|
|
|
* @ret start BAR start address
|
|
|
|
*
|
|
|
|
* Reads the specified PCI base address register, and returns the
|
|
|
|
* address portion of the BAR (i.e. without the flags).
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* If the address exceeds the size of an unsigned long (i.e. if a
|
|
|
|
* 64-bit BAR has a non-zero high dword on a 32-bit machine), the
|
|
|
|
* return value will be zero.
|
2005-04-21 18:18:29 +00:00
|
|
|
*/
|
2006-05-16 15:12:06 +00:00
|
|
|
unsigned long pci_bar_start ( struct pci_device *pci, unsigned int reg ) {
|
|
|
|
unsigned long bar;
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
bar = pci_bar ( pci, reg );
|
|
|
|
if ( (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY ){
|
|
|
|
return ( bar & PCI_BASE_ADDRESS_MEM_MASK );
|
|
|
|
} else {
|
|
|
|
return ( bar & PCI_BASE_ADDRESS_IO_MASK );
|
2005-04-21 18:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Read membase and ioaddr for a PCI device
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* @v pci PCI device
|
|
|
|
*
|
|
|
|
* This scans through all PCI BARs on the specified device. The first
|
|
|
|
* valid memory BAR is recorded as pci_device::membase, and the first
|
|
|
|
* valid IO BAR is recorded as pci_device::ioaddr.
|
|
|
|
*
|
|
|
|
* 64-bit BARs are handled automatically. On a 32-bit platform, if a
|
|
|
|
* 64-bit BAR has a non-zero high dword, it will be regarded as
|
|
|
|
* invalid.
|
2005-04-21 18:18:29 +00:00
|
|
|
*/
|
2006-05-16 15:12:06 +00:00
|
|
|
static void pci_read_bases ( struct pci_device *pci ) {
|
|
|
|
unsigned long bar;
|
|
|
|
int reg;
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
|
|
|
|
bar = pci_bar ( pci, reg );
|
|
|
|
if ( bar & PCI_BASE_ADDRESS_SPACE_IO ) {
|
|
|
|
if ( ! pci->ioaddr )
|
|
|
|
pci->ioaddr =
|
|
|
|
( bar & PCI_BASE_ADDRESS_IO_MASK );
|
|
|
|
} else {
|
|
|
|
if ( ! pci->membase )
|
|
|
|
pci->membase =
|
|
|
|
( bar & PCI_BASE_ADDRESS_MEM_MASK );
|
|
|
|
/* Skip next BAR if 64-bit */
|
|
|
|
if ( bar & PCI_BASE_ADDRESS_MEM_TYPE_64 )
|
|
|
|
reg += 4;
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 18:18:29 +00:00
|
|
|
}
|
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Enable PCI device
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* @v pci PCI device
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2005-04-12 16:36:55 +00:00
|
|
|
* Set device to be a busmaster in case BIOS neglected to do so. Also
|
|
|
|
* adjust PCI latency timer to a reasonable value, 32.
|
|
|
|
*/
|
2005-04-12 18:10:57 +00:00
|
|
|
void adjust_pci_device ( struct pci_device *pci ) {
|
2006-05-16 15:12:06 +00:00
|
|
|
unsigned short new_command, pci_command;
|
|
|
|
unsigned char pci_latency;
|
2005-04-12 16:36:55 +00:00
|
|
|
|
|
|
|
pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
|
|
|
|
new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
|
|
|
|
if ( pci_command != new_command ) {
|
2006-05-16 15:12:06 +00:00
|
|
|
DBG ( "PCI BIOS has not enabled device %02x:%02x.%x! "
|
|
|
|
"Updating PCI command %04x->%04x\n", pci->bus,
|
|
|
|
PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
|
|
|
|
pci_command, new_command );
|
2005-04-12 16:36:55 +00:00
|
|
|
pci_write_config_word ( pci, PCI_COMMAND, new_command );
|
|
|
|
}
|
2006-05-16 15:12:06 +00:00
|
|
|
|
2005-04-12 16:36:55 +00:00
|
|
|
pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
|
|
|
|
if ( pci_latency < 32 ) {
|
2006-05-16 15:12:06 +00:00
|
|
|
DBG ( "PCI device %02x:%02x.%x latency timer is unreasonably "
|
|
|
|
"low at %d. Setting to 32.\n", pci->bus,
|
|
|
|
PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
|
|
|
|
pci_latency );
|
2005-04-12 16:36:55 +00:00
|
|
|
pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Register PCI device
|
|
|
|
*
|
|
|
|
* @v pci PCI device
|
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
|
|
|
* Searches for a driver for the PCI device. If a driver is found,
|
|
|
|
* its probe() routine is called, and the device is added to the
|
|
|
|
* device hierarchy.
|
2005-04-11 13:43:53 +00:00
|
|
|
*/
|
2006-05-16 15:12:06 +00:00
|
|
|
static int register_pcidev ( struct pci_device *pci ) {
|
|
|
|
struct pci_driver *driver;
|
|
|
|
struct pci_device_id *id;
|
|
|
|
unsigned int i;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
DBG ( "Registering PCI device %02x:%02x.%x (%04x:%04x mem %lx "
|
|
|
|
"io %lx irq %d)\n", pci->bus, PCI_SLOT ( pci->devfn ),
|
|
|
|
PCI_FUNC ( pci->devfn ), pci->vendor, pci->device,
|
|
|
|
pci->membase, pci->ioaddr, pci->irq );
|
|
|
|
|
|
|
|
for ( driver = pci_drivers ; driver < pci_drivers_end ; driver++ ) {
|
|
|
|
for ( i = 0 ; i < driver->id_count ; i++ ) {
|
|
|
|
id = &driver->ids[i];
|
|
|
|
if ( ( id->vendor != pci->vendor ) ||
|
|
|
|
( id->device != pci->device ) )
|
|
|
|
continue;
|
|
|
|
pci->driver = driver;
|
|
|
|
pci->name = id->name;
|
|
|
|
DBG ( "...using driver %s\n", pci->name );
|
|
|
|
if ( ( rc = driver->probe ( pci, id ) ) != 0 ) {
|
|
|
|
DBG ( "......probe failed\n" );
|
|
|
|
continue;
|
2005-04-11 13:43:53 +00:00
|
|
|
}
|
2006-05-16 15:12:06 +00:00
|
|
|
list_add ( &pci->dev.siblings,
|
|
|
|
&pci->dev.parent->children );
|
|
|
|
return 0;
|
2005-04-11 13:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
DBG ( "...no driver found\n" );
|
|
|
|
return -ENOTTY;
|
2005-04-11 13:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-05-16 15:12:06 +00:00
|
|
|
* Unregister a PCI device
|
2005-04-11 13:43:53 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* @v pci PCI device
|
2005-04-11 13:43:53 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* Calls the device's driver's remove() routine, and removes the
|
|
|
|
* device from the device hierarchy.
|
|
|
|
*/
|
|
|
|
static void unregister_pcidev ( struct pci_device *pci ) {
|
|
|
|
pci->driver->remove ( pci );
|
|
|
|
list_del ( &pci->dev.siblings );
|
|
|
|
DBG ( "Unregistered PCI device %02x:%02x.%x\n", pci->bus,
|
|
|
|
PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Probe PCI root bus
|
2005-04-11 13:43:53 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* @v rootdev PCI bus root device
|
2005-04-11 13:43:53 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* Scans the PCI bus for devices and registers all devices it can
|
|
|
|
* find.
|
2005-04-11 13:43:53 +00:00
|
|
|
*/
|
2006-05-16 15:12:06 +00:00
|
|
|
static int pcibus_probe ( struct root_device *rootdev ) {
|
|
|
|
struct pci_device *pci = NULL;
|
2006-05-17 01:12:11 +00:00
|
|
|
unsigned int max_bus;
|
2006-05-16 15:12:06 +00:00
|
|
|
unsigned int bus;
|
|
|
|
unsigned int devfn;
|
2006-05-17 01:12:11 +00:00
|
|
|
uint8_t hdrtype = 0;
|
2006-05-16 15:12:06 +00:00
|
|
|
uint32_t tmp;
|
|
|
|
int rc;
|
|
|
|
|
2006-05-17 01:12:11 +00:00
|
|
|
max_bus = pci_max_bus();
|
|
|
|
for ( bus = 0 ; bus <= max_bus ; bus++ ) {
|
2006-05-16 15:12:06 +00:00
|
|
|
for ( devfn = 0 ; devfn <= 0xff ; devfn++ ) {
|
|
|
|
|
|
|
|
/* Allocate struct pci_device */
|
|
|
|
if ( ! pci )
|
|
|
|
pci = malloc ( sizeof ( *pci ) );
|
|
|
|
if ( ! pci ) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
memset ( pci, 0, sizeof ( *pci ) );
|
|
|
|
pci->bus = bus;
|
|
|
|
pci->devfn = devfn;
|
|
|
|
|
|
|
|
/* Skip all but the first function on
|
|
|
|
* non-multifunction cards
|
|
|
|
*/
|
|
|
|
if ( PCI_FUNC ( devfn ) == 0 ) {
|
|
|
|
pci_read_config_byte ( pci, PCI_HEADER_TYPE,
|
|
|
|
&hdrtype );
|
|
|
|
} else if ( ! ( hdrtype & 0x80 ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2005-04-11 13:43:53 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/* Check for physical device presence */
|
|
|
|
pci_read_config_dword ( pci, PCI_VENDOR_ID, &tmp );
|
|
|
|
if ( ( tmp == 0xffffffff ) || ( tmp == 0 ) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Populate struct pci_device */
|
|
|
|
pci->vendor = ( tmp & 0xffff );
|
|
|
|
pci->device = ( tmp >> 16 );
|
|
|
|
pci_read_config_dword ( pci, PCI_REVISION, &tmp );
|
|
|
|
pci->class = ( tmp >> 8 );
|
|
|
|
pci_read_config_byte ( pci, PCI_INTERRUPT_LINE,
|
|
|
|
&pci->irq );
|
|
|
|
pci_read_bases ( pci );
|
|
|
|
INIT_LIST_HEAD ( &pci->dev.children );
|
|
|
|
pci->dev.parent = &rootdev->dev;
|
|
|
|
|
|
|
|
/* Look for a driver */
|
|
|
|
if ( register_pcidev ( pci ) == 0 ) {
|
|
|
|
/* pcidev registered, we can drop our ref */
|
|
|
|
pci = NULL;
|
|
|
|
} else {
|
|
|
|
/* Not registered; re-use struct pci_device */
|
|
|
|
}
|
|
|
|
}
|
2005-04-11 13:43:53 +00:00
|
|
|
}
|
2006-05-16 15:12:06 +00:00
|
|
|
|
|
|
|
free ( pci );
|
2005-04-11 13:43:53 +00:00
|
|
|
return 0;
|
2006-05-16 15:12:06 +00:00
|
|
|
|
|
|
|
err:
|
|
|
|
free ( pci );
|
|
|
|
pcibus_remove ( rootdev );
|
|
|
|
return rc;
|
2005-04-11 13:43:53 +00:00
|
|
|
}
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/**
|
|
|
|
* Remove PCI root bus
|
2005-04-21 18:18:29 +00:00
|
|
|
*
|
2006-05-16 15:12:06 +00:00
|
|
|
* @v rootdev PCI bus root device
|
2005-04-21 18:18:29 +00:00
|
|
|
*/
|
2006-05-16 15:12:06 +00:00
|
|
|
static void pcibus_remove ( struct root_device *rootdev ) {
|
|
|
|
struct pci_device *pci;
|
|
|
|
struct pci_device *tmp;
|
|
|
|
|
|
|
|
list_for_each_entry_safe ( pci, tmp, &rootdev->dev.children,
|
|
|
|
dev.siblings ) {
|
|
|
|
unregister_pcidev ( pci );
|
|
|
|
free ( pci );
|
|
|
|
}
|
|
|
|
}
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/** PCI bus root device driver */
|
|
|
|
static struct root_driver pci_root_driver = {
|
|
|
|
.probe = pcibus_probe,
|
|
|
|
.remove = pcibus_remove,
|
|
|
|
};
|
2005-04-21 18:18:29 +00:00
|
|
|
|
2006-05-16 15:12:06 +00:00
|
|
|
/** PCI bus root device */
|
|
|
|
struct root_device pci_root_device __root_device = {
|
|
|
|
.name = "PCI",
|
|
|
|
.driver = &pci_root_driver,
|
|
|
|
.dev = {
|
|
|
|
.children = LIST_HEAD_INIT ( pci_root_device.dev.children ),
|
|
|
|
},
|
|
|
|
};
|