2 * Low-Level PCI Access for i386 machines
4 * Copyright 1993, 1994 Drew Eckhardt
6 * (Unix and Linux consulting and custom programming)
10 * Drew's work was sponsored by:
11 * iX Multiuser Multitasking Magazine
15 * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
17 * For more information, please consult the following manuals (look at
18 * http://www.pcisig.com/ for how to get them):
20 * PCI BIOS Specification
21 * PCI Local Bus Specification
22 * PCI to PCI Bridge Specification
23 * PCI System Design Guide
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/pci.h>
30 #include <linux/init.h>
31 #include <linux/ioport.h>
32 #include <linux/errno.h>
33 #include <linux/bootmem.h>
34 #include <linux/acpi.h>
38 #include <asm/io_apic.h>
43 skip_isa_ioresource_align(struct pci_dev *dev) {
45 if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
46 !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
52 * We need to avoid collisions with `mirrored' VGA ports
53 * and other strange ISA hardware, so we always want the
54 * addresses to be allocated in the 0x000-0x0ff region
57 * Why? Because some silly external IO cards only decode
58 * the low 10 bits of the IO address. The 0x00-0xff region
59 * is reserved for motherboard devices that decode all 16
60 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
61 * but we want to try to avoid allocating at 0x2900-0x2bff
62 * which might have be mirrored at 0x0100-0x03ff..
65 pcibios_align_resource(void *data, struct resource *res,
66 resource_size_t size, resource_size_t align)
68 struct pci_dev *dev = data;
70 if (res->flags & IORESOURCE_IO) {
71 resource_size_t start = res->start;
73 if (skip_isa_ioresource_align(dev))
76 start = (start + 0x3ff) & ~0x3ff;
81 EXPORT_SYMBOL(pcibios_align_resource);
83 static int check_res_with_valid(struct pci_dev *dev, struct resource *res)
90 size = (res->start == 0 && res->end == res->start) ? 0 :
91 (res->end - res->start + 1);
96 #ifdef CONFIG_HPET_TIMER
98 if (base == hpet_address && (res->flags & IORESOURCE_MEM)) {
99 dev_info(&dev->dev, "BAR has HPET at %08lx-%08lx\n",
100 base, base + size - 1);
105 #ifdef CONFIG_X86_IO_APIC
106 for (i = 0; i < nr_ioapics; i++) {
107 unsigned long ioapic_phys = mp_ioapics[i].mp_apicaddr;
109 if (base == ioapic_phys && (res->flags & IORESOURCE_MEM)) {
110 dev_info(&dev->dev, "BAR has ioapic at %08lx-%08lx\n",
111 base, base + size - 1);
117 #ifdef CONFIG_PCI_MMCONFIG
118 for (i = 0; i < pci_mmcfg_config_num; i++) {
121 addr = pci_mmcfg_config[i].address;
122 if (base == addr && (res->flags & IORESOURCE_MEM)) {
123 dev_info(&dev->dev, "BAR has MMCONFIG at %08lx-%08lx\n",
124 base, base + size - 1);
133 static int check_platform(struct pci_dev *dev, struct resource *res)
135 struct resource *root = NULL;
138 * forcibly insert it into the
141 if (res->flags & IORESOURCE_MEM)
142 root = &iomem_resource;
143 else if (res->flags & IORESOURCE_IO)
144 root = &ioport_resource;
146 if (root && check_res_with_valid(dev, res)) {
147 insert_resource(root, res);
155 * Handle resources of PCI devices. If the world were perfect, we could
156 * just allocate all the resource regions and do nothing more. It isn't.
157 * On the other hand, we cannot just re-allocate all devices, as it would
158 * require us to know lots of host bridge internals. So we attempt to
159 * keep as much of the original configuration as possible, but tweak it
160 * when it's found to be wrong.
162 * Known BIOS problems we have to work around:
163 * - I/O or memory regions not configured
164 * - regions configured, but not enabled in the command register
165 * - bogus I/O addresses above 64K used
166 * - expansion ROMs left enabled (this may sound harmless, but given
167 * the fact the PCI specs explicitly allow address decoders to be
168 * shared between expansion ROMs and other resource regions, it's
169 * at least dangerous)
172 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
173 * This gives us fixed barriers on where we can allocate.
174 * (2) Allocate resources for all enabled devices. If there is
175 * a collision, just mark the resource as unallocated. Also
176 * disable expansion ROMs during this step.
177 * (3) Try to allocate resources for disabled devices. If the
178 * resources were assigned correctly, everything goes well,
179 * if they weren't, they won't disturb allocation of other
181 * (4) Assign new addresses to resources which were either
182 * not configured at all or misconfigured. If explicitly
183 * requested by the user, configure expansion ROM address
187 static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
192 struct resource *r, *pr;
194 /* Depth-First Search on bus tree */
195 list_for_each_entry(bus, bus_list, node) {
196 if ((dev = bus->self)) {
197 for (idx = PCI_BRIDGE_RESOURCES;
198 idx < PCI_NUM_RESOURCES; idx++) {
199 r = &dev->resource[idx];
202 pr = pci_find_parent_resource(dev, r);
203 if (!r->start || !pr ||
204 request_resource(pr, r) < 0) {
205 if (check_platform(dev, r))
207 dev_err(&dev->dev, "BAR %d: can't "
208 "allocate resource\n", idx);
210 * Something is wrong with the region.
211 * Invalidate the resource to prevent
212 * child resource allocations in this
219 pcibios_allocate_bus_resources(&bus->children);
223 static void __init pcibios_allocate_resources(int pass)
225 struct pci_dev *dev = NULL;
228 struct resource *r, *pr;
230 for_each_pci_dev(dev) {
231 pci_read_config_word(dev, PCI_COMMAND, &command);
232 for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
233 r = &dev->resource[idx];
234 if (r->parent) /* Already allocated */
236 if (!r->start) /* Address not assigned at all */
238 if (r->flags & IORESOURCE_IO)
239 disabled = !(command & PCI_COMMAND_IO);
241 disabled = !(command & PCI_COMMAND_MEMORY);
242 if (pass == disabled) {
243 dev_dbg(&dev->dev, "resource %#08llx-%#08llx "
244 "(f=%lx, d=%d, p=%d)\n",
245 (unsigned long long) r->start,
246 (unsigned long long) r->end,
247 r->flags, disabled, pass);
248 pr = pci_find_parent_resource(dev, r);
249 if (!pr || request_resource(pr, r) < 0) {
250 if (check_platform(dev, r))
252 dev_err(&dev->dev, "BAR %d: can't "
253 "allocate resource\n", idx);
254 /* We'll assign a new address later */
261 r = &dev->resource[PCI_ROM_RESOURCE];
262 if (r->flags & IORESOURCE_ROM_ENABLE) {
263 /* Turn the ROM off, leave the resource region,
264 * but keep it unregistered. */
266 dev_dbg(&dev->dev, "disabling ROM\n");
267 r->flags &= ~IORESOURCE_ROM_ENABLE;
268 pci_read_config_dword(dev,
269 dev->rom_base_reg, ®);
270 pci_write_config_dword(dev, dev->rom_base_reg,
271 reg & ~PCI_ROM_ADDRESS_ENABLE);
277 static int __init pcibios_assign_resources(void)
279 struct pci_dev *dev = NULL;
280 struct resource *r, *pr;
282 if (!(pci_probe & PCI_ASSIGN_ROMS)) {
284 * Try to use BIOS settings for ROMs, otherwise let
285 * pci_assign_unassigned_resources() allocate the new
288 for_each_pci_dev(dev) {
289 r = &dev->resource[PCI_ROM_RESOURCE];
290 if (!r->flags || !r->start)
292 pr = pci_find_parent_resource(dev, r);
293 if (!pr || request_resource(pr, r) < 0) {
300 pci_assign_unassigned_resources();
305 void __init pcibios_resource_survey(void)
307 DBG("PCI: Allocating resources\n");
308 pcibios_allocate_bus_resources(&pci_root_buses);
309 pcibios_allocate_resources(0);
310 pcibios_allocate_resources(1);
314 * called in fs_initcall (one below subsys_initcall),
315 * give a chance for motherboard reserve resources
317 fs_initcall(pcibios_assign_resources);
320 * If we set up a device for bus mastering, we need to check the latency
321 * timer as certain crappy BIOSes forget to set it properly.
323 unsigned int pcibios_max_latency = 255;
325 void pcibios_set_master(struct pci_dev *dev)
328 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
330 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
331 else if (lat > pcibios_max_latency)
332 lat = pcibios_max_latency;
335 dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
336 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
339 static void pci_unmap_page_range(struct vm_area_struct *vma)
341 u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
342 free_memtype(addr, addr + vma->vm_end - vma->vm_start);
345 static void pci_track_mmap_page_range(struct vm_area_struct *vma)
347 u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
348 unsigned long flags = pgprot_val(vma->vm_page_prot)
351 reserve_memtype(addr, addr + vma->vm_end - vma->vm_start, flags, NULL);
354 static struct vm_operations_struct pci_mmap_ops = {
355 .open = pci_track_mmap_page_range,
356 .close = pci_unmap_page_range,
357 .access = generic_access_phys,
360 int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
361 enum pci_mmap_state mmap_state, int write_combine)
364 u64 addr = vma->vm_pgoff << PAGE_SHIFT;
365 unsigned long len = vma->vm_end - vma->vm_start;
367 unsigned long new_flags;
370 /* I/O space cannot be accessed via normal processor loads and
371 * stores on this platform.
373 if (mmap_state == pci_mmap_io)
376 prot = pgprot_val(vma->vm_page_prot);
377 if (pat_enabled && write_combine)
378 prot |= _PAGE_CACHE_WC;
379 else if (pat_enabled || boot_cpu_data.x86 > 3)
381 * ioremap() and ioremap_nocache() defaults to UC MINUS for now.
382 * To avoid attribute conflicts, request UC MINUS here
385 prot |= _PAGE_CACHE_UC_MINUS;
387 vma->vm_page_prot = __pgprot(prot);
389 flags = pgprot_val(vma->vm_page_prot) & _PAGE_CACHE_MASK;
390 retval = reserve_memtype(addr, addr + len, flags, &new_flags);
394 if (flags != new_flags) {
396 * Do not fallback to certain memory types with certain
398 * - request is uncached, return cannot be write-back
399 * - request is uncached, return cannot be write-combine
400 * - request is write-combine, return cannot be write-back
402 if ((flags == _PAGE_CACHE_UC_MINUS &&
403 (new_flags == _PAGE_CACHE_WB)) ||
404 (flags == _PAGE_CACHE_WC &&
405 new_flags == _PAGE_CACHE_WB)) {
406 free_memtype(addr, addr+len);
412 if (((vma->vm_pgoff < max_low_pfn_mapped) ||
413 (vma->vm_pgoff >= (1UL<<(32 - PAGE_SHIFT)) &&
414 vma->vm_pgoff < max_pfn_mapped)) &&
415 ioremap_change_attr((unsigned long)__va(addr), len, flags)) {
416 free_memtype(addr, addr + len);
420 if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
421 vma->vm_end - vma->vm_start,
425 vma->vm_ops = &pci_mmap_ops;