blob: 40811efaa25a12fa9f5ee3577dac50909499ee9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Low-Level PCI Access for i386 machines
3 *
4 * Copyright 1993, 1994 Drew Eckhardt
5 * Visionary Computing
6 * (Unix and Linux consulting and custom programming)
7 * Drew@Colorado.EDU
8 * +1 (303) 786-7975
9 *
10 * Drew's work was sponsored by:
11 * iX Multiuser Multitasking Magazine
12 * Hannover, Germany
13 * hm@ix.de
14 *
15 * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
16 *
17 * For more information, please consult the following manuals (look at
18 * http://www.pcisig.com/ for how to get them):
19 *
20 * PCI BIOS Specification
21 * PCI Local Bus Specification
22 * PCI to PCI Bridge Specification
23 * PCI System Design Guide
24 *
25 */
26
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>
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -070033#include <linux/bootmem.h>
Yinghai Lu58f7c982008-08-28 13:52:25 -070034#include <linux/acpi.h>
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -070035
36#include <asm/pat.h>
Yinghai Lu58f7c982008-08-28 13:52:25 -070037#include <asm/hpet.h>
38#include <asm/io_apic.h>
39#include <asm/e820.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "pci.h"
42
Gary Hade036fff42007-10-03 15:56:14 -070043static int
44skip_isa_ioresource_align(struct pci_dev *dev) {
45
46 if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
Gary Hade11949252007-10-08 16:24:16 -070047 !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
Gary Hade036fff42007-10-03 15:56:14 -070048 return 1;
49 return 0;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*
53 * We need to avoid collisions with `mirrored' VGA ports
54 * and other strange ISA hardware, so we always want the
55 * addresses to be allocated in the 0x000-0x0ff region
56 * modulo 0x400.
57 *
58 * Why? Because some silly external IO cards only decode
59 * the low 10 bits of the IO address. The 0x00-0xff region
60 * is reserved for motherboard devices that decode all 16
61 * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
62 * but we want to try to avoid allocating at 0x2900-0x2bff
63 * which might have be mirrored at 0x0100-0x03ff..
64 */
65void
66pcibios_align_resource(void *data, struct resource *res,
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070067 resource_size_t size, resource_size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Gary Hade036fff42007-10-03 15:56:14 -070069 struct pci_dev *dev = data;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (res->flags & IORESOURCE_IO) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070072 resource_size_t start = res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Gary Hade036fff42007-10-03 15:56:14 -070074 if (skip_isa_ioresource_align(dev))
75 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 if (start & 0x300) {
77 start = (start + 0x3ff) & ~0x3ff;
78 res->start = start;
79 }
80 }
81}
Dave Airlie6c00a612007-10-29 18:06:10 +100082EXPORT_SYMBOL(pcibios_align_resource);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Yinghai Lu58f7c982008-08-28 13:52:25 -070084static int check_res_with_valid(struct pci_dev *dev, struct resource *res)
85{
86 unsigned long base;
87 unsigned long size;
88 int i;
89
90 base = res->start;
91 size = (res->start == 0 && res->end == res->start) ? 0 :
92 (res->end - res->start + 1);
93
94 if (!base || !size)
95 return 0;
96
97#ifdef CONFIG_HPET_TIMER
98 /* for hpet */
99 if (base == hpet_address && (res->flags & IORESOURCE_MEM)) {
100 dev_info(&dev->dev, "BAR has HPET at %08lx-%08lx\n",
101 base, base + size - 1);
102 return 1;
103 }
104#endif
105
106#ifdef CONFIG_X86_IO_APIC
107 for (i = 0; i < nr_ioapics; i++) {
108 unsigned long ioapic_phys = mp_ioapics[i].mp_apicaddr;
109
110 if (base == ioapic_phys && (res->flags & IORESOURCE_MEM)) {
111 dev_info(&dev->dev, "BAR has ioapic at %08lx-%08lx\n",
112 base, base + size - 1);
113 return 1;
114 }
115 }
116#endif
117
118#ifdef CONFIG_PCI_MMCONFIG
119 for (i = 0; i < pci_mmcfg_config_num; i++) {
120 unsigned long addr;
121
122 addr = pci_mmcfg_config[i].address;
123 if (base == addr && (res->flags & IORESOURCE_MEM)) {
124 dev_info(&dev->dev, "BAR has MMCONFIG at %08lx-%08lx\n",
125 base, base + size - 1);
126 return 1;
127 }
128 }
129#endif
130
131 return 0;
132}
133
134static int check_platform(struct pci_dev *dev, struct resource *res)
135{
136 struct resource *root = NULL;
137
138 /*
139 * forcibly insert it into the
140 * resource tree
141 */
142 if (res->flags & IORESOURCE_MEM)
143 root = &iomem_resource;
144 else if (res->flags & IORESOURCE_IO)
145 root = &ioport_resource;
146
147 if (root && check_res_with_valid(dev, res)) {
148 insert_resource(root, res);
149
150 return 1;
151 }
152
153 return 0;
154}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
156 * Handle resources of PCI devices. If the world were perfect, we could
157 * just allocate all the resource regions and do nothing more. It isn't.
158 * On the other hand, we cannot just re-allocate all devices, as it would
159 * require us to know lots of host bridge internals. So we attempt to
160 * keep as much of the original configuration as possible, but tweak it
161 * when it's found to be wrong.
162 *
163 * Known BIOS problems we have to work around:
164 * - I/O or memory regions not configured
165 * - regions configured, but not enabled in the command register
166 * - bogus I/O addresses above 64K used
167 * - expansion ROMs left enabled (this may sound harmless, but given
168 * the fact the PCI specs explicitly allow address decoders to be
169 * shared between expansion ROMs and other resource regions, it's
170 * at least dangerous)
171 *
172 * Our solution:
173 * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
174 * This gives us fixed barriers on where we can allocate.
175 * (2) Allocate resources for all enabled devices. If there is
176 * a collision, just mark the resource as unallocated. Also
177 * disable expansion ROMs during this step.
178 * (3) Try to allocate resources for disabled devices. If the
179 * resources were assigned correctly, everything goes well,
180 * if they weren't, they won't disturb allocation of other
181 * resources.
182 * (4) Assign new addresses to resources which were either
183 * not configured at all or misconfigured. If explicitly
184 * requested by the user, configure expansion ROM address
185 * as well.
186 */
187
188static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
189{
190 struct pci_bus *bus;
191 struct pci_dev *dev;
192 int idx;
193 struct resource *r, *pr;
194
195 /* Depth-First Search on bus tree */
196 list_for_each_entry(bus, bus_list, node) {
197 if ((dev = bus->self)) {
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700198 for (idx = PCI_BRIDGE_RESOURCES;
199 idx < PCI_NUM_RESOURCES; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 r = &dev->resource[idx];
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400201 if (!r->flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 continue;
203 pr = pci_find_parent_resource(dev, r);
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700204 if (!r->start || !pr ||
205 request_resource(pr, r) < 0) {
Yinghai Lu58f7c982008-08-28 13:52:25 -0700206 if (check_platform(dev, r))
207 continue;
Linus Torvalds011fec72008-09-02 10:38:28 -0700208 dev_err(&dev->dev, "BAR %d: can't allocate resource\n", idx);
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700209 /*
210 * Something is wrong with the region.
211 * Invalidate the resource to prevent
212 * child resource allocations in this
213 * range.
214 */
Ivan Kokshaysky299de032005-06-15 18:59:27 +0400215 r->flags = 0;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 }
219 pcibios_allocate_bus_resources(&bus->children);
220 }
221}
222
223static void __init pcibios_allocate_resources(int pass)
224{
225 struct pci_dev *dev = NULL;
226 int idx, disabled;
227 u16 command;
228 struct resource *r, *pr;
229
230 for_each_pci_dev(dev) {
231 pci_read_config_word(dev, PCI_COMMAND, &command);
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700232 for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 r = &dev->resource[idx];
234 if (r->parent) /* Already allocated */
235 continue;
236 if (!r->start) /* Address not assigned at all */
237 continue;
238 if (r->flags & IORESOURCE_IO)
239 disabled = !(command & PCI_COMMAND_IO);
240 else
241 disabled = !(command & PCI_COMMAND_MEMORY);
242 if (pass == disabled) {
Linus Torvalds011fec72008-09-02 10:38:28 -0700243 dev_dbg(&dev->dev, "resource %#08llx-%#08llx (f=%lx, d=%d, p=%d)\n",
Bjorn Helgaas12c0b202008-07-23 17:00:13 -0600244 (unsigned long long) r->start,
245 (unsigned long long) r->end,
246 r->flags, disabled, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pr = pci_find_parent_resource(dev, r);
248 if (!pr || request_resource(pr, r) < 0) {
Yinghai Lu58f7c982008-08-28 13:52:25 -0700249 if (check_platform(dev, r))
250 continue;
Linus Torvalds011fec72008-09-02 10:38:28 -0700251 dev_err(&dev->dev, "BAR %d: can't allocate resource\n", idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* We'll assign a new address later */
253 r->end -= r->start;
254 r->start = 0;
255 }
256 }
257 }
258 if (!pass) {
259 r = &dev->resource[PCI_ROM_RESOURCE];
260 if (r->flags & IORESOURCE_ROM_ENABLE) {
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700261 /* Turn the ROM off, leave the resource region,
262 * but keep it unregistered. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 u32 reg;
Bjorn Helgaas12c0b202008-07-23 17:00:13 -0600264 dev_dbg(&dev->dev, "disabling ROM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 r->flags &= ~IORESOURCE_ROM_ENABLE;
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700266 pci_read_config_dword(dev,
267 dev->rom_base_reg, &reg);
268 pci_write_config_dword(dev, dev->rom_base_reg,
269 reg & ~PCI_ROM_ADDRESS_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 }
272 }
273}
274
275static int __init pcibios_assign_resources(void)
276{
277 struct pci_dev *dev = NULL;
Ivan Kokshaysky81d4af12005-08-30 18:48:52 +0400278 struct resource *r, *pr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Ivan Kokshaysky81d4af12005-08-30 18:48:52 +0400280 if (!(pci_probe & PCI_ASSIGN_ROMS)) {
Randy Dunlap7edab2f2006-10-17 10:17:58 -0700281 /*
282 * Try to use BIOS settings for ROMs, otherwise let
283 * pci_assign_unassigned_resources() allocate the new
284 * addresses.
285 */
Ivan Kokshaysky81d4af12005-08-30 18:48:52 +0400286 for_each_pci_dev(dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 r = &dev->resource[PCI_ROM_RESOURCE];
Ivan Kokshaysky81d4af12005-08-30 18:48:52 +0400288 if (!r->flags || !r->start)
289 continue;
290 pr = pci_find_parent_resource(dev, r);
291 if (!pr || request_resource(pr, r) < 0) {
292 r->end -= r->start;
293 r->start = 0;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 }
Ivan Kokshaysky81d4af12005-08-30 18:48:52 +0400297
298 pci_assign_unassigned_resources();
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
302
303void __init pcibios_resource_survey(void)
304{
305 DBG("PCI: Allocating resources\n");
306 pcibios_allocate_bus_resources(&pci_root_buses);
307 pcibios_allocate_resources(0);
308 pcibios_allocate_resources(1);
309}
310
311/**
312 * called in fs_initcall (one below subsys_initcall),
313 * give a chance for motherboard reserve resources
314 */
315fs_initcall(pcibios_assign_resources);
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * If we set up a device for bus mastering, we need to check the latency
319 * timer as certain crappy BIOSes forget to set it properly.
320 */
321unsigned int pcibios_max_latency = 255;
322
323void pcibios_set_master(struct pci_dev *dev)
324{
325 u8 lat;
326 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
327 if (lat < 16)
328 lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
329 else if (lat > pcibios_max_latency)
330 lat = pcibios_max_latency;
331 else
332 return;
Bjorn Helgaas12c0b202008-07-23 17:00:13 -0600333 dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
335}
336
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700337static void pci_unmap_page_range(struct vm_area_struct *vma)
338{
339 u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
340 free_memtype(addr, addr + vma->vm_end - vma->vm_start);
341}
342
343static void pci_track_mmap_page_range(struct vm_area_struct *vma)
344{
345 u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
346 unsigned long flags = pgprot_val(vma->vm_page_prot)
347 & _PAGE_CACHE_MASK;
348
349 reserve_memtype(addr, addr + vma->vm_end - vma->vm_start, flags, NULL);
350}
351
352static struct vm_operations_struct pci_mmap_ops = {
353 .open = pci_track_mmap_page_range,
354 .close = pci_unmap_page_range,
Rik van Riel7ae8ed52008-07-23 21:27:07 -0700355 .access = generic_access_phys,
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700356};
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
359 enum pci_mmap_state mmap_state, int write_combine)
360{
361 unsigned long prot;
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700362 u64 addr = vma->vm_pgoff << PAGE_SHIFT;
363 unsigned long len = vma->vm_end - vma->vm_start;
364 unsigned long flags;
365 unsigned long new_flags;
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700366 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* I/O space cannot be accessed via normal processor loads and
369 * stores on this platform.
370 */
371 if (mmap_state == pci_mmap_io)
372 return -EINVAL;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 prot = pgprot_val(vma->vm_page_prot);
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200375 if (pat_enabled && write_combine)
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700376 prot |= _PAGE_CACHE_WC;
Andreas Herrmann499f8f82008-06-10 16:06:21 +0200377 else if (pat_enabled || boot_cpu_data.x86 > 3)
Suresh Siddhade33c442008-04-25 17:07:22 -0700378 /*
379 * ioremap() and ioremap_nocache() defaults to UC MINUS for now.
380 * To avoid attribute conflicts, request UC MINUS here
381 * aswell.
382 */
383 prot |= _PAGE_CACHE_UC_MINUS;
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 vma->vm_page_prot = __pgprot(prot);
386
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700387 flags = pgprot_val(vma->vm_page_prot) & _PAGE_CACHE_MASK;
Venki Pallipadidee7cbb2008-03-24 14:39:55 -0700388 retval = reserve_memtype(addr, addr + len, flags, &new_flags);
389 if (retval)
390 return retval;
391
392 if (flags != new_flags) {
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700393 /*
394 * Do not fallback to certain memory types with certain
395 * requested type:
396 * - request is uncached, return cannot be write-back
397 * - request is uncached, return cannot be write-combine
398 * - request is write-combine, return cannot be write-back
399 */
Suresh Siddhade33c442008-04-25 17:07:22 -0700400 if ((flags == _PAGE_CACHE_UC_MINUS &&
401 (new_flags == _PAGE_CACHE_WB)) ||
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700402 (flags == _PAGE_CACHE_WC &&
403 new_flags == _PAGE_CACHE_WB)) {
404 free_memtype(addr, addr+len);
405 return -EINVAL;
406 }
407 flags = new_flags;
408 }
409
Yinghai Lu965194c2008-07-12 14:31:28 -0700410 if (((vma->vm_pgoff < max_low_pfn_mapped) ||
Yinghai Luf361a452008-07-10 20:38:26 -0700411 (vma->vm_pgoff >= (1UL<<(32 - PAGE_SHIFT)) &&
Yinghai Lu965194c2008-07-12 14:31:28 -0700412 vma->vm_pgoff < max_pfn_mapped)) &&
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700413 ioremap_change_attr((unsigned long)__va(addr), len, flags)) {
414 free_memtype(addr, addr + len);
415 return -EINVAL;
416 }
417
Michael S. Tsirkin346d3882005-07-31 11:51:45 +0300418 if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
419 vma->vm_end - vma->vm_start,
420 vma->vm_page_prot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return -EAGAIN;
422
venkatesh.pallipadi@intel.com03d72aa2008-03-18 17:00:19 -0700423 vma->vm_ops = &pci_mmap_ops;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return 0;
426}