x86: cpa: move flush to cpa
[linux-2.6/x86.git] / arch / x86 / mm / ioremap.c
blob6a9a1418bc98b75e2da18f0849de1791f4adb925
1 /*
2 * Re-map IO memory to kernel address space so that we can access it.
3 * This is needed for high PCI addresses that aren't mapped in the
4 * 640k-1MB IO memory area on PC's
6 * (C) Copyright 1995 1996 Linus Torvalds
7 */
9 #include <linux/bootmem.h>
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
16 #include <asm/cacheflush.h>
17 #include <asm/e820.h>
18 #include <asm/fixmap.h>
19 #include <asm/pgtable.h>
20 #include <asm/tlbflush.h>
22 enum ioremap_mode {
23 IOR_MODE_UNCACHED,
24 IOR_MODE_CACHED,
27 #ifdef CONFIG_X86_64
29 unsigned long __phys_addr(unsigned long x)
31 if (x >= __START_KERNEL_map)
32 return x - __START_KERNEL_map + phys_base;
33 return x - PAGE_OFFSET;
35 EXPORT_SYMBOL(__phys_addr);
37 #endif
39 int page_is_ram(unsigned long pagenr)
41 unsigned long addr, end;
42 int i;
44 for (i = 0; i < e820.nr_map; i++) {
46 * Not usable memory:
48 if (e820.map[i].type != E820_RAM)
49 continue;
50 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
51 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
54 * Sanity check: Some BIOSen report areas as RAM that
55 * are not. Notably the 640->1Mb area, which is the
56 * PCI BIOS area.
58 if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
59 end < (BIOS_END >> PAGE_SHIFT))
60 continue;
62 if ((pagenr >= addr) && (pagenr < end))
63 return 1;
65 return 0;
69 * Fix up the linear direct mapping of the kernel to avoid cache attribute
70 * conflicts.
72 static int ioremap_change_attr(unsigned long paddr, unsigned long size,
73 enum ioremap_mode mode)
75 unsigned long vaddr = (unsigned long)__va(paddr);
76 unsigned long nrpages = size >> PAGE_SHIFT;
77 int err, level;
79 /* No change for pages after the last mapping */
80 if ((paddr + size - 1) >= (max_pfn_mapped << PAGE_SHIFT))
81 return 0;
84 * If there is no identity map for this address,
85 * change_page_attr_addr is unnecessary
87 if (!lookup_address(vaddr, &level))
88 return 0;
90 switch (mode) {
91 case IOR_MODE_UNCACHED:
92 default:
93 err = set_memory_uc(vaddr, nrpages);
94 break;
95 case IOR_MODE_CACHED:
96 err = set_memory_wb(vaddr, nrpages);
97 break;
100 return err;
104 * Remap an arbitrary physical address space into the kernel virtual
105 * address space. Needed when the kernel wants to access high addresses
106 * directly.
108 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
109 * have to convert them into an offset in a page-aligned mapping, but the
110 * caller shouldn't need to know that small detail.
112 static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
113 enum ioremap_mode mode)
115 void __iomem *addr;
116 struct vm_struct *area;
117 unsigned long offset, last_addr;
118 pgprot_t prot;
120 /* Don't allow wraparound or zero size */
121 last_addr = phys_addr + size - 1;
122 if (!size || last_addr < phys_addr)
123 return NULL;
126 * Don't remap the low PCI/ISA area, it's always mapped..
128 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
129 return (__force void __iomem *)phys_to_virt(phys_addr);
132 * Don't allow anybody to remap normal RAM that we're using..
134 for (offset = phys_addr >> PAGE_SHIFT; offset < max_pfn_mapped &&
135 (offset << PAGE_SHIFT) < last_addr; offset++) {
136 if (page_is_ram(offset))
137 return NULL;
140 switch (mode) {
141 case IOR_MODE_UNCACHED:
142 default:
143 prot = PAGE_KERNEL_NOCACHE;
144 break;
145 case IOR_MODE_CACHED:
146 prot = PAGE_KERNEL;
147 break;
151 * Mappings have to be page-aligned
153 offset = phys_addr & ~PAGE_MASK;
154 phys_addr &= PAGE_MASK;
155 size = PAGE_ALIGN(last_addr+1) - phys_addr;
158 * Ok, go for it..
160 area = get_vm_area(size, VM_IOREMAP);
161 if (!area)
162 return NULL;
163 area->phys_addr = phys_addr;
164 addr = (void __iomem *) area->addr;
165 if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
166 phys_addr, prot)) {
167 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
168 return NULL;
171 if (ioremap_change_attr(phys_addr, size, mode) < 0) {
172 vunmap(addr);
173 return NULL;
176 return (void __iomem *) (offset + (char __iomem *)addr);
180 * ioremap_nocache - map bus memory into CPU space
181 * @offset: bus address of the memory
182 * @size: size of the resource to map
184 * ioremap_nocache performs a platform specific sequence of operations to
185 * make bus memory CPU accessible via the readb/readw/readl/writeb/
186 * writew/writel functions and the other mmio helpers. The returned
187 * address is not guaranteed to be usable directly as a virtual
188 * address.
190 * This version of ioremap ensures that the memory is marked uncachable
191 * on the CPU as well as honouring existing caching rules from things like
192 * the PCI bus. Note that there are other caches and buffers on many
193 * busses. In particular driver authors should read up on PCI writes
195 * It's useful if some control registers are in such an area and
196 * write combining or read caching is not desirable:
198 * Must be freed with iounmap.
200 void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
202 return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
204 EXPORT_SYMBOL(ioremap_nocache);
206 void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
208 return __ioremap(phys_addr, size, IOR_MODE_CACHED);
210 EXPORT_SYMBOL(ioremap_cache);
213 * iounmap - Free a IO remapping
214 * @addr: virtual address from ioremap_*
216 * Caller must ensure there is only one unmapping for the same pointer.
218 void iounmap(volatile void __iomem *addr)
220 struct vm_struct *p, *o;
222 if ((void __force *)addr <= high_memory)
223 return;
226 * __ioremap special-cases the PCI/ISA range by not instantiating a
227 * vm_area and by simply returning an address into the kernel mapping
228 * of ISA space. So handle that here.
230 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
231 addr < phys_to_virt(ISA_END_ADDRESS))
232 return;
234 addr = (volatile void __iomem *)
235 (PAGE_MASK & (unsigned long __force)addr);
237 /* Use the vm area unlocked, assuming the caller
238 ensures there isn't another iounmap for the same address
239 in parallel. Reuse of the virtual address is prevented by
240 leaving it in the global lists until we're done with it.
241 cpa takes care of the direct mappings. */
242 read_lock(&vmlist_lock);
243 for (p = vmlist; p; p = p->next) {
244 if (p->addr == addr)
245 break;
247 read_unlock(&vmlist_lock);
249 if (!p) {
250 printk(KERN_ERR "iounmap: bad address %p\n", addr);
251 dump_stack();
252 return;
255 /* Reset the direct mapping. Can block */
256 ioremap_change_attr(p->phys_addr, p->size, IOR_MODE_CACHED);
258 /* Finally remove it */
259 o = remove_vm_area((void *)addr);
260 BUG_ON(p != o || o == NULL);
261 kfree(p);
263 EXPORT_SYMBOL(iounmap);
265 #ifdef CONFIG_X86_32
267 int __initdata early_ioremap_debug;
269 static int __init early_ioremap_debug_setup(char *str)
271 early_ioremap_debug = 1;
273 return 0;
275 early_param("early_ioremap_debug", early_ioremap_debug_setup);
277 static __initdata int after_paging_init;
278 static __initdata unsigned long bm_pte[1024]
279 __attribute__((aligned(PAGE_SIZE)));
281 static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
283 return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
286 static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
288 return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
291 void __init early_ioremap_init(void)
293 unsigned long *pgd;
295 if (early_ioremap_debug)
296 printk(KERN_DEBUG "early_ioremap_init()\n");
298 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
299 *pgd = __pa(bm_pte) | _PAGE_TABLE;
300 memset(bm_pte, 0, sizeof(bm_pte));
302 * The boot-ioremap range spans multiple pgds, for which
303 * we are not prepared:
305 if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
306 WARN_ON(1);
307 printk(KERN_WARNING "pgd %p != %p\n",
308 pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
309 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
310 fix_to_virt(FIX_BTMAP_BEGIN));
311 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END): %08lx\n",
312 fix_to_virt(FIX_BTMAP_END));
314 printk(KERN_WARNING "FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
315 printk(KERN_WARNING "FIX_BTMAP_BEGIN: %d\n",
316 FIX_BTMAP_BEGIN);
320 void __init early_ioremap_clear(void)
322 unsigned long *pgd;
324 if (early_ioremap_debug)
325 printk(KERN_DEBUG "early_ioremap_clear()\n");
327 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
328 *pgd = 0;
329 __flush_tlb_all();
332 void __init early_ioremap_reset(void)
334 enum fixed_addresses idx;
335 unsigned long *pte, phys, addr;
337 after_paging_init = 1;
338 for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
339 addr = fix_to_virt(idx);
340 pte = early_ioremap_pte(addr);
341 if (!*pte & _PAGE_PRESENT) {
342 phys = *pte & PAGE_MASK;
343 set_fixmap(idx, phys);
348 static void __init __early_set_fixmap(enum fixed_addresses idx,
349 unsigned long phys, pgprot_t flags)
351 unsigned long *pte, addr = __fix_to_virt(idx);
353 if (idx >= __end_of_fixed_addresses) {
354 BUG();
355 return;
357 pte = early_ioremap_pte(addr);
358 if (pgprot_val(flags))
359 *pte = (phys & PAGE_MASK) | pgprot_val(flags);
360 else
361 *pte = 0;
362 __flush_tlb_one(addr);
365 static inline void __init early_set_fixmap(enum fixed_addresses idx,
366 unsigned long phys)
368 if (after_paging_init)
369 set_fixmap(idx, phys);
370 else
371 __early_set_fixmap(idx, phys, PAGE_KERNEL);
374 static inline void __init early_clear_fixmap(enum fixed_addresses idx)
376 if (after_paging_init)
377 clear_fixmap(idx);
378 else
379 __early_set_fixmap(idx, 0, __pgprot(0));
383 int __initdata early_ioremap_nested;
385 static int __init check_early_ioremap_leak(void)
387 if (!early_ioremap_nested)
388 return 0;
390 printk(KERN_WARNING
391 "Debug warning: early ioremap leak of %d areas detected.\n",
392 early_ioremap_nested);
393 printk(KERN_WARNING
394 "please boot with early_ioremap_debug and report the dmesg.\n");
395 WARN_ON(1);
397 return 1;
399 late_initcall(check_early_ioremap_leak);
401 void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
403 unsigned long offset, last_addr;
404 unsigned int nrpages, nesting;
405 enum fixed_addresses idx0, idx;
407 WARN_ON(system_state != SYSTEM_BOOTING);
409 nesting = early_ioremap_nested;
410 if (early_ioremap_debug) {
411 printk(KERN_DEBUG "early_ioremap(%08lx, %08lx) [%d] => ",
412 phys_addr, size, nesting);
413 dump_stack();
416 /* Don't allow wraparound or zero size */
417 last_addr = phys_addr + size - 1;
418 if (!size || last_addr < phys_addr) {
419 WARN_ON(1);
420 return NULL;
423 if (nesting >= FIX_BTMAPS_NESTING) {
424 WARN_ON(1);
425 return NULL;
427 early_ioremap_nested++;
429 * Mappings have to be page-aligned
431 offset = phys_addr & ~PAGE_MASK;
432 phys_addr &= PAGE_MASK;
433 size = PAGE_ALIGN(last_addr) - phys_addr;
436 * Mappings have to fit in the FIX_BTMAP area.
438 nrpages = size >> PAGE_SHIFT;
439 if (nrpages > NR_FIX_BTMAPS) {
440 WARN_ON(1);
441 return NULL;
445 * Ok, go for it..
447 idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
448 idx = idx0;
449 while (nrpages > 0) {
450 early_set_fixmap(idx, phys_addr);
451 phys_addr += PAGE_SIZE;
452 --idx;
453 --nrpages;
455 if (early_ioremap_debug)
456 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
458 return (void *) (offset + fix_to_virt(idx0));
461 void __init early_iounmap(void *addr, unsigned long size)
463 unsigned long virt_addr;
464 unsigned long offset;
465 unsigned int nrpages;
466 enum fixed_addresses idx;
467 unsigned int nesting;
469 nesting = --early_ioremap_nested;
470 WARN_ON(nesting < 0);
472 if (early_ioremap_debug) {
473 printk(KERN_DEBUG "early_iounmap(%p, %08lx) [%d]\n", addr,
474 size, nesting);
475 dump_stack();
478 virt_addr = (unsigned long)addr;
479 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
480 WARN_ON(1);
481 return;
483 offset = virt_addr & ~PAGE_MASK;
484 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
486 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
487 while (nrpages > 0) {
488 early_clear_fixmap(idx);
489 --idx;
490 --nrpages;
494 void __this_fixmap_does_not_exist(void)
496 WARN_ON(1);
499 #endif /* CONFIG_X86_32 */