/*1* Low-Level PCI Access for i386 machines2*3* Copyright 1993, 1994 Drew Eckhardt4* Visionary Computing5* (Unix and Linux consulting and custom programming)6* [email protected]7* +1 (303) 786-79758*9* Drew's work was sponsored by:10* iX Multiuser Multitasking Magazine11* Hannover, Germany12* [email protected]13*14* Copyright 1997--2000 Martin Mares <[email protected]>15*16* For more information, please consult the following manuals (look at17* http://www.pcisig.com/ for how to get them):18*19* PCI BIOS Specification20* PCI Local Bus Specification21* PCI to PCI Bridge Specification22* PCI System Design Guide23*24*/2526#include <linux/types.h>27#include <linux/kernel.h>28#include <linux/pci.h>29#include <linux/init.h>30#include <linux/ioport.h>31#include <linux/errno.h>32#include <linux/bootmem.h>3334#include <asm/pat.h>35#include <asm/e820.h>36#include <asm/pci_x86.h>37#include <asm/io_apic.h>383940static int41skip_isa_ioresource_align(struct pci_dev *dev) {4243if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&44!(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))45return 1;46return 0;47}4849/*50* We need to avoid collisions with `mirrored' VGA ports51* and other strange ISA hardware, so we always want the52* addresses to be allocated in the 0x000-0x0ff region53* modulo 0x400.54*55* Why? Because some silly external IO cards only decode56* the low 10 bits of the IO address. The 0x00-0xff region57* is reserved for motherboard devices that decode all 1658* bits, so it's ok to allocate at, say, 0x2800-0x28ff,59* but we want to try to avoid allocating at 0x2900-0x2bff60* which might have be mirrored at 0x0100-0x03ff..61*/62resource_size_t63pcibios_align_resource(void *data, const struct resource *res,64resource_size_t size, resource_size_t align)65{66struct pci_dev *dev = data;67resource_size_t start = res->start;6869if (res->flags & IORESOURCE_IO) {70if (skip_isa_ioresource_align(dev))71return start;72if (start & 0x300)73start = (start + 0x3ff) & ~0x3ff;74}75return start;76}77EXPORT_SYMBOL(pcibios_align_resource);7879/*80* Handle resources of PCI devices. If the world were perfect, we could81* just allocate all the resource regions and do nothing more. It isn't.82* On the other hand, we cannot just re-allocate all devices, as it would83* require us to know lots of host bridge internals. So we attempt to84* keep as much of the original configuration as possible, but tweak it85* when it's found to be wrong.86*87* Known BIOS problems we have to work around:88* - I/O or memory regions not configured89* - regions configured, but not enabled in the command register90* - bogus I/O addresses above 64K used91* - expansion ROMs left enabled (this may sound harmless, but given92* the fact the PCI specs explicitly allow address decoders to be93* shared between expansion ROMs and other resource regions, it's94* at least dangerous)95* - bad resource sizes or overlaps with other regions96*97* Our solution:98* (1) Allocate resources for all buses behind PCI-to-PCI bridges.99* This gives us fixed barriers on where we can allocate.100* (2) Allocate resources for all enabled devices. If there is101* a collision, just mark the resource as unallocated. Also102* disable expansion ROMs during this step.103* (3) Try to allocate resources for disabled devices. If the104* resources were assigned correctly, everything goes well,105* if they weren't, they won't disturb allocation of other106* resources.107* (4) Assign new addresses to resources which were either108* not configured at all or misconfigured. If explicitly109* requested by the user, configure expansion ROM address110* as well.111*/112113static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)114{115struct pci_bus *bus;116struct pci_dev *dev;117int idx;118struct resource *r;119120/* Depth-First Search on bus tree */121list_for_each_entry(bus, bus_list, node) {122if ((dev = bus->self)) {123for (idx = PCI_BRIDGE_RESOURCES;124idx < PCI_NUM_RESOURCES; idx++) {125r = &dev->resource[idx];126if (!r->flags)127continue;128if (!r->start ||129pci_claim_resource(dev, idx) < 0) {130/*131* Something is wrong with the region.132* Invalidate the resource to prevent133* child resource allocations in this134* range.135*/136r->start = r->end = 0;137r->flags = 0;138}139}140}141pcibios_allocate_bus_resources(&bus->children);142}143}144145struct pci_check_idx_range {146int start;147int end;148};149150static void __init pcibios_allocate_resources(int pass)151{152struct pci_dev *dev = NULL;153int idx, disabled, i;154u16 command;155struct resource *r;156157struct pci_check_idx_range idx_range[] = {158{ PCI_STD_RESOURCES, PCI_STD_RESOURCE_END },159#ifdef CONFIG_PCI_IOV160{ PCI_IOV_RESOURCES, PCI_IOV_RESOURCE_END },161#endif162};163164for_each_pci_dev(dev) {165pci_read_config_word(dev, PCI_COMMAND, &command);166for (i = 0; i < ARRAY_SIZE(idx_range); i++)167for (idx = idx_range[i].start; idx <= idx_range[i].end; idx++) {168r = &dev->resource[idx];169if (r->parent) /* Already allocated */170continue;171if (!r->start) /* Address not assigned at all */172continue;173if (r->flags & IORESOURCE_IO)174disabled = !(command & PCI_COMMAND_IO);175else176disabled = !(command & PCI_COMMAND_MEMORY);177if (pass == disabled) {178dev_dbg(&dev->dev,179"BAR %d: reserving %pr (d=%d, p=%d)\n",180idx, r, disabled, pass);181if (pci_claim_resource(dev, idx) < 0) {182/* We'll assign a new address later */183dev->fw_addr[idx] = r->start;184r->end -= r->start;185r->start = 0;186}187}188}189if (!pass) {190r = &dev->resource[PCI_ROM_RESOURCE];191if (r->flags & IORESOURCE_ROM_ENABLE) {192/* Turn the ROM off, leave the resource region,193* but keep it unregistered. */194u32 reg;195dev_dbg(&dev->dev, "disabling ROM %pR\n", r);196r->flags &= ~IORESOURCE_ROM_ENABLE;197pci_read_config_dword(dev,198dev->rom_base_reg, ®);199pci_write_config_dword(dev, dev->rom_base_reg,200reg & ~PCI_ROM_ADDRESS_ENABLE);201}202}203}204}205206static int __init pcibios_assign_resources(void)207{208struct pci_dev *dev = NULL;209struct resource *r;210211if (!(pci_probe & PCI_ASSIGN_ROMS)) {212/*213* Try to use BIOS settings for ROMs, otherwise let214* pci_assign_unassigned_resources() allocate the new215* addresses.216*/217for_each_pci_dev(dev) {218r = &dev->resource[PCI_ROM_RESOURCE];219if (!r->flags || !r->start)220continue;221if (pci_claim_resource(dev, PCI_ROM_RESOURCE) < 0) {222r->end -= r->start;223r->start = 0;224}225}226}227228pci_assign_unassigned_resources();229230return 0;231}232233void __init pcibios_resource_survey(void)234{235DBG("PCI: Allocating resources\n");236pcibios_allocate_bus_resources(&pci_root_buses);237pcibios_allocate_resources(0);238pcibios_allocate_resources(1);239240e820_reserve_resources_late();241/*242* Insert the IO APIC resources after PCI initialization has243* occurred to handle IO APICS that are mapped in on a BAR in244* PCI space, but before trying to assign unassigned pci res.245*/246ioapic_insert_resources();247}248249/**250* called in fs_initcall (one below subsys_initcall),251* give a chance for motherboard reserve resources252*/253fs_initcall(pcibios_assign_resources);254255/*256* If we set up a device for bus mastering, we need to check the latency257* timer as certain crappy BIOSes forget to set it properly.258*/259unsigned int pcibios_max_latency = 255;260261void pcibios_set_master(struct pci_dev *dev)262{263u8 lat;264pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);265if (lat < 16)266lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;267else if (lat > pcibios_max_latency)268lat = pcibios_max_latency;269else270return;271dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);272pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);273}274275static const struct vm_operations_struct pci_mmap_ops = {276.access = generic_access_phys,277};278279int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,280enum pci_mmap_state mmap_state, int write_combine)281{282unsigned long prot;283284/* I/O space cannot be accessed via normal processor loads and285* stores on this platform.286*/287if (mmap_state == pci_mmap_io)288return -EINVAL;289290prot = pgprot_val(vma->vm_page_prot);291292/*293* Return error if pat is not enabled and write_combine is requested.294* Caller can followup with UC MINUS request and add a WC mtrr if there295* is a free mtrr slot.296*/297if (!pat_enabled && write_combine)298return -EINVAL;299300if (pat_enabled && write_combine)301prot |= _PAGE_CACHE_WC;302else if (pat_enabled || boot_cpu_data.x86 > 3)303/*304* ioremap() and ioremap_nocache() defaults to UC MINUS for now.305* To avoid attribute conflicts, request UC MINUS here306* as well.307*/308prot |= _PAGE_CACHE_UC_MINUS;309310prot |= _PAGE_IOMAP; /* creating a mapping for IO */311312vma->vm_page_prot = __pgprot(prot);313314if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,315vma->vm_end - vma->vm_start,316vma->vm_page_prot))317return -EAGAIN;318319vma->vm_ops = &pci_mmap_ops;320321return 0;322}323324325