2005-04-17 02:20:36 +04:00
/*
* Low - Level PCI Access for i386 machines
*
* Copyright 1993 , 1994 Drew Eckhardt
* Visionary Computing
* ( Unix and Linux consulting and custom programming )
* Drew @ Colorado . EDU
* + 1 ( 303 ) 786 - 7975
*
* Drew ' s work was sponsored by :
* iX Multiuser Multitasking Magazine
* Hannover , Germany
* hm @ ix . de
*
* Copyright 1997 - - 2000 Martin Mares < mj @ ucw . cz >
*
* For more information , please consult the following manuals ( look at
* http : //www.pcisig.com/ for how to get them):
*
* PCI BIOS Specification
* PCI Local Bus Specification
* PCI to PCI Bridge Specification
* PCI System Design Guide
*
*/
# include <linux/types.h>
# include <linux/kernel.h>
# include <linux/pci.h>
# include <linux/init.h>
# include <linux/ioport.h>
# include <linux/errno.h>
2008-03-19 03:00:19 +03:00
# include <linux/bootmem.h>
# include <asm/pat.h>
2005-04-17 02:20:36 +04:00
# include "pci.h"
2007-10-04 02:56:14 +04:00
static int
skip_isa_ioresource_align ( struct pci_dev * dev ) {
if ( ( pci_probe & PCI_CAN_SKIP_ISA_ALIGN ) & &
2007-10-09 03:24:16 +04:00
! ( dev - > bus - > bridge_ctl & PCI_BRIDGE_CTL_ISA ) )
2007-10-04 02:56:14 +04:00
return 1 ;
return 0 ;
}
2005-04-17 02:20:36 +04:00
/*
* We need to avoid collisions with ` mirrored ' VGA ports
* and other strange ISA hardware , so we always want the
* addresses to be allocated in the 0x000 - 0x0ff region
* modulo 0x400 .
*
* Why ? Because some silly external IO cards only decode
* the low 10 bits of the IO address . The 0x00 - 0xff region
* is reserved for motherboard devices that decode all 16
* bits , so it ' s ok to allocate at , say , 0x2800 - 0x28ff ,
* but we want to try to avoid allocating at 0x2900 - 0x2bff
* which might have be mirrored at 0x0100 - 0x03ff . .
*/
void
pcibios_align_resource ( void * data , struct resource * res ,
2006-06-13 04:06:02 +04:00
resource_size_t size , resource_size_t align )
2005-04-17 02:20:36 +04:00
{
2007-10-04 02:56:14 +04:00
struct pci_dev * dev = data ;
2005-04-17 02:20:36 +04:00
if ( res - > flags & IORESOURCE_IO ) {
2006-06-13 04:06:02 +04:00
resource_size_t start = res - > start ;
2005-04-17 02:20:36 +04:00
2007-10-04 02:56:14 +04:00
if ( skip_isa_ioresource_align ( dev ) )
return ;
2005-04-17 02:20:36 +04:00
if ( start & 0x300 ) {
start = ( start + 0x3ff ) & ~ 0x3ff ;
res - > start = start ;
}
}
}
2007-10-29 11:06:10 +03:00
EXPORT_SYMBOL ( pcibios_align_resource ) ;
2005-04-17 02:20:36 +04:00
/*
* Handle resources of PCI devices . If the world were perfect , we could
* just allocate all the resource regions and do nothing more . It isn ' t .
* On the other hand , we cannot just re - allocate all devices , as it would
* require us to know lots of host bridge internals . So we attempt to
* keep as much of the original configuration as possible , but tweak it
* when it ' s found to be wrong .
*
* Known BIOS problems we have to work around :
* - I / O or memory regions not configured
* - regions configured , but not enabled in the command register
* - bogus I / O addresses above 64 K used
* - expansion ROMs left enabled ( this may sound harmless , but given
* the fact the PCI specs explicitly allow address decoders to be
* shared between expansion ROMs and other resource regions , it ' s
* at least dangerous )
*
* Our solution :
* ( 1 ) Allocate resources for all buses behind PCI - to - PCI bridges .
* This gives us fixed barriers on where we can allocate .
* ( 2 ) Allocate resources for all enabled devices . If there is
* a collision , just mark the resource as unallocated . Also
* disable expansion ROMs during this step .
* ( 3 ) Try to allocate resources for disabled devices . If the
* resources were assigned correctly , everything goes well ,
* if they weren ' t , they won ' t disturb allocation of other
* resources .
* ( 4 ) Assign new addresses to resources which were either
* not configured at all or misconfigured . If explicitly
* requested by the user , configure expansion ROM address
* as well .
*/
static void __init pcibios_allocate_bus_resources ( struct list_head * bus_list )
{
struct pci_bus * bus ;
struct pci_dev * dev ;
int idx ;
struct resource * r , * pr ;
/* Depth-First Search on bus tree */
list_for_each_entry ( bus , bus_list , node ) {
if ( ( dev = bus - > self ) ) {
2006-10-17 21:17:58 +04:00
for ( idx = PCI_BRIDGE_RESOURCES ;
idx < PCI_NUM_RESOURCES ; idx + + ) {
2005-04-17 02:20:36 +04:00
r = & dev - > resource [ idx ] ;
2005-06-15 18:59:27 +04:00
if ( ! r - > flags )
2005-04-17 02:20:36 +04:00
continue ;
pr = pci_find_parent_resource ( dev , r ) ;
2006-10-17 21:17:58 +04:00
if ( ! r - > start | | ! pr | |
request_resource ( pr , r ) < 0 ) {
printk ( KERN_ERR " PCI: Cannot allocate "
" resource region %d "
" of bridge %s \n " ,
idx , pci_name ( dev ) ) ;
/*
* Something is wrong with the region .
* Invalidate the resource to prevent
* child resource allocations in this
* range .
*/
2005-06-15 18:59:27 +04:00
r - > flags = 0 ;
}
2005-04-17 02:20:36 +04:00
}
}
pcibios_allocate_bus_resources ( & bus - > children ) ;
}
}
static void __init pcibios_allocate_resources ( int pass )
{
struct pci_dev * dev = NULL ;
int idx , disabled ;
u16 command ;
struct resource * r , * pr ;
for_each_pci_dev ( dev ) {
pci_read_config_word ( dev , PCI_COMMAND , & command ) ;
2006-10-17 21:17:58 +04:00
for ( idx = 0 ; idx < PCI_ROM_RESOURCE ; idx + + ) {
2005-04-17 02:20:36 +04:00
r = & dev - > resource [ idx ] ;
if ( r - > parent ) /* Already allocated */
continue ;
if ( ! r - > start ) /* Address not assigned at all */
continue ;
if ( r - > flags & IORESOURCE_IO )
disabled = ! ( command & PCI_COMMAND_IO ) ;
else
disabled = ! ( command & PCI_COMMAND_MEMORY ) ;
if ( pass = = disabled ) {
2006-10-17 21:17:58 +04:00
DBG ( " PCI: Resource %08lx-%08lx "
" (f=%lx, d=%d, p=%d) \n " ,
2005-04-17 02:20:36 +04:00
r - > start , r - > end , r - > flags , disabled , pass ) ;
pr = pci_find_parent_resource ( dev , r ) ;
if ( ! pr | | request_resource ( pr , r ) < 0 ) {
2006-10-17 21:17:58 +04:00
printk ( KERN_ERR " PCI: Cannot allocate "
" resource region %d "
" of device %s \n " ,
idx , pci_name ( dev ) ) ;
2005-04-17 02:20:36 +04:00
/* We'll assign a new address later */
r - > end - = r - > start ;
r - > start = 0 ;
}
}
}
if ( ! pass ) {
r = & dev - > resource [ PCI_ROM_RESOURCE ] ;
if ( r - > flags & IORESOURCE_ROM_ENABLE ) {
2006-10-17 21:17:58 +04:00
/* Turn the ROM off, leave the resource region,
* but keep it unregistered . */
2005-04-17 02:20:36 +04:00
u32 reg ;
2006-10-17 21:17:58 +04:00
DBG ( " PCI: Switching off ROM of %s \n " ,
pci_name ( dev ) ) ;
2005-04-17 02:20:36 +04:00
r - > flags & = ~ IORESOURCE_ROM_ENABLE ;
2006-10-17 21:17:58 +04:00
pci_read_config_dword ( dev ,
dev - > rom_base_reg , & reg ) ;
pci_write_config_dword ( dev , dev - > rom_base_reg ,
reg & ~ PCI_ROM_ADDRESS_ENABLE ) ;
2005-04-17 02:20:36 +04:00
}
}
}
}
static int __init pcibios_assign_resources ( void )
{
struct pci_dev * dev = NULL ;
2005-08-30 18:48:52 +04:00
struct resource * r , * pr ;
2005-04-17 02:20:36 +04:00
2005-08-30 18:48:52 +04:00
if ( ! ( pci_probe & PCI_ASSIGN_ROMS ) ) {
2006-10-17 21:17:58 +04:00
/*
* Try to use BIOS settings for ROMs , otherwise let
* pci_assign_unassigned_resources ( ) allocate the new
* addresses .
*/
2005-08-30 18:48:52 +04:00
for_each_pci_dev ( dev ) {
2005-04-17 02:20:36 +04:00
r = & dev - > resource [ PCI_ROM_RESOURCE ] ;
2005-08-30 18:48:52 +04:00
if ( ! r - > flags | | ! r - > start )
continue ;
pr = pci_find_parent_resource ( dev , r ) ;
if ( ! pr | | request_resource ( pr , r ) < 0 ) {
r - > end - = r - > start ;
r - > start = 0 ;
}
2005-04-17 02:20:36 +04:00
}
}
2005-08-30 18:48:52 +04:00
pci_assign_unassigned_resources ( ) ;
2005-04-17 02:20:36 +04:00
return 0 ;
}
void __init pcibios_resource_survey ( void )
{
DBG ( " PCI: Allocating resources \n " ) ;
pcibios_allocate_bus_resources ( & pci_root_buses ) ;
pcibios_allocate_resources ( 0 ) ;
pcibios_allocate_resources ( 1 ) ;
}
/**
* called in fs_initcall ( one below subsys_initcall ) ,
* give a chance for motherboard reserve resources
*/
fs_initcall ( pcibios_assign_resources ) ;
int pcibios_enable_resources ( struct pci_dev * dev , int mask )
{
u16 cmd , old_cmd ;
int idx ;
struct resource * r ;
pci_read_config_word ( dev , PCI_COMMAND , & cmd ) ;
old_cmd = cmd ;
2006-10-17 21:17:58 +04:00
for ( idx = 0 ; idx < PCI_NUM_RESOURCES ; idx + + ) {
2005-04-17 02:20:36 +04:00
/* Only set up the requested stuff */
2006-10-17 21:17:58 +04:00
if ( ! ( mask & ( 1 < < idx ) ) )
2005-04-17 02:20:36 +04:00
continue ;
r = & dev - > resource [ idx ] ;
2005-11-24 02:44:59 +03:00
if ( ! ( r - > flags & ( IORESOURCE_IO | IORESOURCE_MEM ) ) )
continue ;
if ( ( idx = = PCI_ROM_RESOURCE ) & &
( ! ( r - > flags & IORESOURCE_ROM_ENABLE ) ) )
continue ;
2005-04-17 02:20:36 +04:00
if ( ! r - > start & & r - > end ) {
2006-10-17 21:17:58 +04:00
printk ( KERN_ERR " PCI: Device %s not available "
2007-04-10 18:25:44 +04:00
" because of resource %d collisions \n " ,
pci_name ( dev ) , idx ) ;
2005-04-17 02:20:36 +04:00
return - EINVAL ;
}
if ( r - > flags & IORESOURCE_IO )
cmd | = PCI_COMMAND_IO ;
if ( r - > flags & IORESOURCE_MEM )
cmd | = PCI_COMMAND_MEMORY ;
}
if ( cmd ! = old_cmd ) {
2006-10-17 21:17:58 +04:00
printk ( " PCI: Enabling device %s (%04x -> %04x) \n " ,
pci_name ( dev ) , old_cmd , cmd ) ;
2005-04-17 02:20:36 +04:00
pci_write_config_word ( dev , PCI_COMMAND , cmd ) ;
}
return 0 ;
}
/*
* If we set up a device for bus mastering , we need to check the latency
* timer as certain crappy BIOSes forget to set it properly .
*/
unsigned int pcibios_max_latency = 255 ;
void pcibios_set_master ( struct pci_dev * dev )
{
u8 lat ;
pci_read_config_byte ( dev , PCI_LATENCY_TIMER , & lat ) ;
if ( lat < 16 )
lat = ( 64 < = pcibios_max_latency ) ? 64 : pcibios_max_latency ;
else if ( lat > pcibios_max_latency )
lat = pcibios_max_latency ;
else
return ;
2006-10-17 21:17:58 +04:00
printk ( KERN_DEBUG " PCI: Setting latency timer of device %s to %d \n " ,
pci_name ( dev ) , lat ) ;
2005-04-17 02:20:36 +04:00
pci_write_config_byte ( dev , PCI_LATENCY_TIMER , lat ) ;
}
2008-03-19 03:00:19 +03:00
static void pci_unmap_page_range ( struct vm_area_struct * vma )
{
u64 addr = ( u64 ) vma - > vm_pgoff < < PAGE_SHIFT ;
free_memtype ( addr , addr + vma - > vm_end - vma - > vm_start ) ;
}
static void pci_track_mmap_page_range ( struct vm_area_struct * vma )
{
u64 addr = ( u64 ) vma - > vm_pgoff < < PAGE_SHIFT ;
unsigned long flags = pgprot_val ( vma - > vm_page_prot )
& _PAGE_CACHE_MASK ;
reserve_memtype ( addr , addr + vma - > vm_end - vma - > vm_start , flags , NULL ) ;
}
static struct vm_operations_struct pci_mmap_ops = {
. open = pci_track_mmap_page_range ,
. close = pci_unmap_page_range ,
} ;
2005-04-17 02:20:36 +04:00
int pci_mmap_page_range ( struct pci_dev * dev , struct vm_area_struct * vma ,
enum pci_mmap_state mmap_state , int write_combine )
{
unsigned long prot ;
2008-03-19 03:00:19 +03:00
u64 addr = vma - > vm_pgoff < < PAGE_SHIFT ;
unsigned long len = vma - > vm_end - vma - > vm_start ;
unsigned long flags ;
unsigned long new_flags ;
2005-04-17 02:20:36 +04:00
/* I/O space cannot be accessed via normal processor loads and
* stores on this platform .
*/
if ( mmap_state = = pci_mmap_io )
return - EINVAL ;
prot = pgprot_val ( vma - > vm_page_prot ) ;
2008-03-19 03:00:19 +03:00
if ( pat_wc_enabled & & write_combine )
prot | = _PAGE_CACHE_WC ;
else if ( boot_cpu_data . x86 > 3 )
prot | = _PAGE_CACHE_UC ;
2005-04-17 02:20:36 +04:00
vma - > vm_page_prot = __pgprot ( prot ) ;
2008-03-19 03:00:19 +03:00
flags = pgprot_val ( vma - > vm_page_prot ) & _PAGE_CACHE_MASK ;
if ( reserve_memtype ( addr , addr + len , flags , & new_flags ) ) {
/*
* Do not fallback to certain memory types with certain
* requested type :
* - request is uncached , return cannot be write - back
* - request is uncached , return cannot be write - combine
* - request is write - combine , return cannot be write - back
*/
if ( ( flags = = _PAGE_CACHE_UC & &
( new_flags = = _PAGE_CACHE_WB | |
new_flags = = _PAGE_CACHE_WC ) ) | |
( flags = = _PAGE_CACHE_WC & &
new_flags = = _PAGE_CACHE_WB ) ) {
free_memtype ( addr , addr + len ) ;
return - EINVAL ;
}
flags = new_flags ;
}
if ( vma - > vm_pgoff < = max_pfn_mapped & &
ioremap_change_attr ( ( unsigned long ) __va ( addr ) , len , flags ) ) {
free_memtype ( addr , addr + len ) ;
return - EINVAL ;
}
2005-07-31 12:51:45 +04:00
if ( io_remap_pfn_range ( vma , vma - > vm_start , vma - > vm_pgoff ,
vma - > vm_end - vma - > vm_start ,
vma - > vm_page_prot ) )
2005-04-17 02:20:36 +04:00
return - EAGAIN ;
2008-03-19 03:00:19 +03:00
vma - > vm_ops = & pci_mmap_ops ;
2005-04-17 02:20:36 +04:00
return 0 ;
}