2005-04-16 22:20:36 +00: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 00:00:19 +00:00
|
|
|
#include <linux/bootmem.h>
|
|
|
|
|
|
|
|
#include <asm/pat.h>
|
2008-08-28 20:52:25 +00:00
|
|
|
#include <asm/e820.h>
|
2008-12-27 13:02:28 +00:00
|
|
|
#include <asm/pci_x86.h>
|
2009-07-10 16:36:20 +00:00
|
|
|
#include <asm/io_apic.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
|
2007-10-03 22:56:14 +00:00
|
|
|
static int
|
|
|
|
skip_isa_ioresource_align(struct pci_dev *dev) {
|
|
|
|
|
|
|
|
if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
|
2007-10-08 23:24:16 +00:00
|
|
|
!(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
|
2007-10-03 22:56:14 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00: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..
|
|
|
|
*/
|
2010-01-01 16:40:49 +00:00
|
|
|
resource_size_t
|
2010-01-01 16:40:50 +00:00
|
|
|
pcibios_align_resource(void *data, const struct resource *res,
|
2006-06-13 00:06:02 +00:00
|
|
|
resource_size_t size, resource_size_t align)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-10-03 22:56:14 +00:00
|
|
|
struct pci_dev *dev = data;
|
2010-12-16 17:38:31 +00:00
|
|
|
resource_size_t start = res->start;
|
2007-10-03 22:56:14 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
if (res->flags & IORESOURCE_IO) {
|
2010-12-16 17:38:31 +00:00
|
|
|
if (skip_isa_ioresource_align(dev))
|
|
|
|
return start;
|
|
|
|
if (start & 0x300)
|
|
|
|
start = (start + 0x3ff) & ~0x3ff;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2010-01-01 16:40:49 +00:00
|
|
|
return start;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2007-10-29 08:06:10 +00:00
|
|
|
EXPORT_SYMBOL(pcibios_align_resource);
|
2005-04-16 22:20:36 +00: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 64K 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)
|
2010-06-03 20:43:03 +00:00
|
|
|
* - bad resource sizes or overlaps with other regions
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* 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;
|
2009-06-17 20:33:35 +00:00
|
|
|
struct resource *r;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* Depth-First Search on bus tree */
|
|
|
|
list_for_each_entry(bus, bus_list, node) {
|
|
|
|
if ((dev = bus->self)) {
|
2006-10-17 17:17:58 +00:00
|
|
|
for (idx = PCI_BRIDGE_RESOURCES;
|
|
|
|
idx < PCI_NUM_RESOURCES; idx++) {
|
2005-04-16 22:20:36 +00:00
|
|
|
r = &dev->resource[idx];
|
2005-06-15 14:59:27 +00:00
|
|
|
if (!r->flags)
|
2005-04-16 22:20:36 +00:00
|
|
|
continue;
|
2009-06-17 20:33:35 +00:00
|
|
|
if (!r->start ||
|
|
|
|
pci_claim_resource(dev, idx) < 0) {
|
2006-10-17 17:17:58 +00:00
|
|
|
/*
|
|
|
|
* Something is wrong with the region.
|
|
|
|
* Invalidate the resource to prevent
|
|
|
|
* child resource allocations in this
|
|
|
|
* range.
|
|
|
|
*/
|
2010-06-03 20:43:03 +00:00
|
|
|
r->start = r->end = 0;
|
2005-06-15 14:59:27 +00:00
|
|
|
r->flags = 0;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pcibios_allocate_bus_resources(&bus->children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-25 02:05:12 +00:00
|
|
|
struct pci_check_idx_range {
|
|
|
|
int start;
|
|
|
|
int end;
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void __init pcibios_allocate_resources(int pass)
|
|
|
|
{
|
|
|
|
struct pci_dev *dev = NULL;
|
2009-11-25 02:05:12 +00:00
|
|
|
int idx, disabled, i;
|
2005-04-16 22:20:36 +00:00
|
|
|
u16 command;
|
2009-06-17 20:33:35 +00:00
|
|
|
struct resource *r;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2009-11-25 02:05:12 +00:00
|
|
|
struct pci_check_idx_range idx_range[] = {
|
|
|
|
{ PCI_STD_RESOURCES, PCI_STD_RESOURCE_END },
|
|
|
|
#ifdef CONFIG_PCI_IOV
|
|
|
|
{ PCI_IOV_RESOURCES, PCI_IOV_RESOURCE_END },
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
for_each_pci_dev(dev) {
|
|
|
|
pci_read_config_word(dev, PCI_COMMAND, &command);
|
2009-11-25 02:05:12 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE(idx_range); i++)
|
|
|
|
for (idx = idx_range[i].start; idx <= idx_range[i].end; idx++) {
|
2005-04-16 22:20:36 +00: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) {
|
2009-10-27 19:26:47 +00:00
|
|
|
dev_dbg(&dev->dev,
|
2009-11-04 17:32:57 +00:00
|
|
|
"BAR %d: reserving %pr (d=%d, p=%d)\n",
|
2009-10-27 19:26:47 +00:00
|
|
|
idx, r, disabled, pass);
|
2009-06-17 20:33:35 +00:00
|
|
|
if (pci_claim_resource(dev, idx) < 0) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* We'll assign a new address later */
|
PCI: fall back to original BIOS BAR addresses
If we fail to assign resources to a PCI BAR, this patch makes us try the
original address from BIOS rather than leaving it disabled.
Linux tries to make sure all PCI device BARs are inside the upstream
PCI host bridge or P2P bridge apertures, reassigning BARs if necessary.
Windows does similar reassignment.
Before this patch, if we could not move a BAR into an aperture, we left
the resource unassigned, i.e., at address zero. Windows leaves such BARs
at the original BIOS addresses, and this patch makes Linux do the same.
This is a bit ugly because we disable the resource long before we try to
reassign it, so we have to keep track of the BIOS BAR address somewhere.
For lack of a better place, I put it in the struct pci_dev.
I think it would be cleaner to attempt the assignment immediately when the
claim fails, so we could easily remember the original address. But we
currently claim motherboard resources in the middle, after attempting to
claim PCI resources and before assigning new PCI resources, and changing
that is a fairly big job.
Addresses https://bugzilla.kernel.org/show_bug.cgi?id=16263
Reported-by: Andrew <nitr0@seti.kr.ua>
Tested-by: Andrew <nitr0@seti.kr.ua>
Signed-off-by: Bjorn Helgaas <bjorn.helgaas@hp.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2010-07-15 15:41:42 +00:00
|
|
|
dev->fw_addr[idx] = r->start;
|
2005-04-16 22:20:36 +00:00
|
|
|
r->end -= r->start;
|
|
|
|
r->start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!pass) {
|
|
|
|
r = &dev->resource[PCI_ROM_RESOURCE];
|
|
|
|
if (r->flags & IORESOURCE_ROM_ENABLE) {
|
2006-10-17 17:17:58 +00:00
|
|
|
/* Turn the ROM off, leave the resource region,
|
|
|
|
* but keep it unregistered. */
|
2005-04-16 22:20:36 +00:00
|
|
|
u32 reg;
|
2009-10-27 19:26:47 +00:00
|
|
|
dev_dbg(&dev->dev, "disabling ROM %pR\n", r);
|
2005-04-16 22:20:36 +00:00
|
|
|
r->flags &= ~IORESOURCE_ROM_ENABLE;
|
2006-10-17 17:17:58 +00:00
|
|
|
pci_read_config_dword(dev,
|
|
|
|
dev->rom_base_reg, ®);
|
|
|
|
pci_write_config_dword(dev, dev->rom_base_reg,
|
|
|
|
reg & ~PCI_ROM_ADDRESS_ENABLE);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init pcibios_assign_resources(void)
|
|
|
|
{
|
|
|
|
struct pci_dev *dev = NULL;
|
2009-06-17 20:33:35 +00:00
|
|
|
struct resource *r;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-08-30 14:48:52 +00:00
|
|
|
if (!(pci_probe & PCI_ASSIGN_ROMS)) {
|
2006-10-17 17:17:58 +00:00
|
|
|
/*
|
|
|
|
* Try to use BIOS settings for ROMs, otherwise let
|
|
|
|
* pci_assign_unassigned_resources() allocate the new
|
|
|
|
* addresses.
|
|
|
|
*/
|
2005-08-30 14:48:52 +00:00
|
|
|
for_each_pci_dev(dev) {
|
2005-04-16 22:20:36 +00:00
|
|
|
r = &dev->resource[PCI_ROM_RESOURCE];
|
2005-08-30 14:48:52 +00:00
|
|
|
if (!r->flags || !r->start)
|
|
|
|
continue;
|
2009-06-17 20:33:35 +00:00
|
|
|
if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {
|
2005-08-30 14:48:52 +00:00
|
|
|
r->end -= r->start;
|
|
|
|
r->start = 0;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
2005-08-30 14:48:52 +00:00
|
|
|
|
|
|
|
pci_assign_unassigned_resources();
|
|
|
|
|
2005-04-16 22:20:36 +00: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);
|
2008-08-29 06:09:23 +00:00
|
|
|
|
|
|
|
e820_reserve_resources_late();
|
2009-07-10 16:36:20 +00:00
|
|
|
/*
|
|
|
|
* Insert the IO APIC resources after PCI initialization has
|
|
|
|
* occured to handle IO APICS that are mapped in on a BAR in
|
|
|
|
* PCI space, but before trying to assign unassigned pci res.
|
|
|
|
*/
|
|
|
|
ioapic_insert_resources();
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* called in fs_initcall (one below subsys_initcall),
|
|
|
|
* give a chance for motherboard reserve resources
|
|
|
|
*/
|
|
|
|
fs_initcall(pcibios_assign_resources);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2008-07-23 23:00:13 +00:00
|
|
|
dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
|
2005-04-16 22:20:36 +00:00
|
|
|
pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
|
|
|
|
}
|
|
|
|
|
2009-09-27 18:29:37 +00:00
|
|
|
static const struct vm_operations_struct pci_mmap_ops = {
|
2008-07-24 04:27:07 +00:00
|
|
|
.access = generic_access_phys,
|
2008-03-19 00:00:19 +00:00
|
|
|
};
|
|
|
|
|
2005-04-16 22:20:36 +00: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;
|
|
|
|
|
|
|
|
/* 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);
|
2009-10-26 21:21:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Return error if pat is not enabled and write_combine is requested.
|
|
|
|
* Caller can followup with UC MINUS request and add a WC mtrr if there
|
|
|
|
* is a free mtrr slot.
|
|
|
|
*/
|
|
|
|
if (!pat_enabled && write_combine)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2008-06-10 14:06:21 +00:00
|
|
|
if (pat_enabled && write_combine)
|
2008-03-19 00:00:19 +00:00
|
|
|
prot |= _PAGE_CACHE_WC;
|
2008-06-10 14:06:21 +00:00
|
|
|
else if (pat_enabled || boot_cpu_data.x86 > 3)
|
2008-04-26 00:07:22 +00:00
|
|
|
/*
|
|
|
|
* ioremap() and ioremap_nocache() defaults to UC MINUS for now.
|
|
|
|
* To avoid attribute conflicts, request UC MINUS here
|
|
|
|
* aswell.
|
|
|
|
*/
|
|
|
|
prot |= _PAGE_CACHE_UC_MINUS;
|
2008-03-19 00:00:19 +00:00
|
|
|
|
2010-03-18 18:31:30 +00:00
|
|
|
prot |= _PAGE_IOMAP; /* creating a mapping for IO */
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
vma->vm_page_prot = __pgprot(prot);
|
|
|
|
|
2005-07-31 08:51:45 +00: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-16 22:20:36 +00:00
|
|
|
return -EAGAIN;
|
|
|
|
|
2008-03-19 00:00:19 +00:00
|
|
|
vma->vm_ops = &pci_mmap_ops;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|