blob: 18757f058bdaec8844570f0864ea3b3131c6585d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/i386/mm/ioremap.c
3 *
4 * Re-map IO memory to kernel address space so that we can access it.
5 * This is needed for high PCI addresses that aren't mapped in the
6 * 640k-1MB IO memory area on PC's
7 *
8 * (C) Copyright 1995 1996 Linus Torvalds
9 */
10
11#include <linux/vmalloc.h>
12#include <linux/init.h>
13#include <linux/slab.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070014#include <linux/module.h>
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -070015#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/fixmap.h>
17#include <asm/cacheflush.h>
18#include <asm/tlbflush.h>
19#include <asm/pgtable.h>
20
21#define ISA_START_ADDRESS 0xa0000
22#define ISA_END_ADDRESS 0x100000
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024/*
25 * Generic mapping function (not visible outside):
26 */
27
28/*
29 * Remap an arbitrary physical address space into the kernel virtual
30 * address space. Needed when the kernel wants to access high addresses
31 * directly.
32 *
33 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
34 * have to convert them into an offset in a page-aligned mapping, but the
35 * caller shouldn't need to know that small detail.
36 */
37void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
38{
39 void __iomem * addr;
40 struct vm_struct * area;
41 unsigned long offset, last_addr;
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -070042 pgprot_t prot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 /* Don't allow wraparound or zero size */
45 last_addr = phys_addr + size - 1;
46 if (!size || last_addr < phys_addr)
47 return NULL;
48
49 /*
50 * Don't remap the low PCI/ISA area, it's always mapped..
51 */
52 if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
53 return (void __iomem *) phys_to_virt(phys_addr);
54
55 /*
56 * Don't allow anybody to remap normal RAM that we're using..
57 */
58 if (phys_addr <= virt_to_phys(high_memory - 1)) {
59 char *t_addr, *t_end;
60 struct page *page;
61
62 t_addr = __va(phys_addr);
63 t_end = t_addr + (size - 1);
64
65 for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
66 if(!PageReserved(page))
67 return NULL;
68 }
69
Thomas Gleixnera4034342008-01-30 13:34:04 +010070 prot = MAKE_GLOBAL(__PAGE_KERNEL | flags);
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /*
73 * Mappings have to be page-aligned
74 */
75 offset = phys_addr & ~PAGE_MASK;
76 phys_addr &= PAGE_MASK;
77 size = PAGE_ALIGN(last_addr+1) - phys_addr;
78
79 /*
80 * Ok, go for it..
81 */
82 area = get_vm_area(size, VM_IOREMAP | (flags << 20));
83 if (!area)
84 return NULL;
85 area->phys_addr = phys_addr;
86 addr = (void __iomem *) area->addr;
87 if (ioremap_page_range((unsigned long) addr,
Haavard Skinnemoena148ecf2006-09-30 23:29:17 -070088 (unsigned long) addr + size, phys_addr, prot)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 vunmap((void __force *) addr);
90 return NULL;
91 }
92 return (void __iomem *) (offset + (char __iomem *)addr);
93}
Alexey Dobriyan129f6942005-06-23 00:08:33 -070094EXPORT_SYMBOL(__ioremap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/**
97 * ioremap_nocache - map bus memory into CPU space
98 * @offset: bus address of the memory
99 * @size: size of the resource to map
100 *
101 * ioremap_nocache performs a platform specific sequence of operations to
102 * make bus memory CPU accessible via the readb/readw/readl/writeb/
103 * writew/writel functions and the other mmio helpers. The returned
104 * address is not guaranteed to be usable directly as a virtual
105 * address.
106 *
107 * This version of ioremap ensures that the memory is marked uncachable
108 * on the CPU as well as honouring existing caching rules from things like
109 * the PCI bus. Note that there are other caches and buffers on many
110 * busses. In particular driver authors should read up on PCI writes
111 *
112 * It's useful if some control registers are in such an area and
113 * write combining or read caching is not desirable:
114 *
115 * Must be freed with iounmap.
116 */
117
118void __iomem *ioremap_nocache (unsigned long phys_addr, unsigned long size)
119{
120 unsigned long last_addr;
Siddha, Suresh B4138cc32008-01-30 13:33:43 +0100121 void __iomem *p = __ioremap(phys_addr, size, _PAGE_PCD | _PAGE_PWT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!p)
123 return p;
124
125 /* Guaranteed to be > phys_addr, as per __ioremap() */
126 last_addr = phys_addr + size - 1;
127
128 if (last_addr < virt_to_phys(high_memory) - 1) {
129 struct page *ppage = virt_to_page(__va(phys_addr));
130 unsigned long npages;
131
132 phys_addr &= PAGE_MASK;
133
134 /* This might overflow and become zero.. */
135 last_addr = PAGE_ALIGN(last_addr);
136
137 /* .. but that's ok, because modulo-2**n arithmetic will make
138 * the page-aligned "last - first" come out right.
139 */
140 npages = (last_addr - phys_addr) >> PAGE_SHIFT;
141
142 if (change_page_attr(ppage, npages, PAGE_KERNEL_NOCACHE) < 0) {
143 iounmap(p);
144 p = NULL;
145 }
146 global_flush_tlb();
147 }
148
149 return p;
150}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700151EXPORT_SYMBOL(ioremap_nocache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Andi Kleenbf5421c2005-12-12 22:17:09 -0800153/**
154 * iounmap - Free a IO remapping
155 * @addr: virtual address from ioremap_*
156 *
157 * Caller must ensure there is only one unmapping for the same pointer.
158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159void iounmap(volatile void __iomem *addr)
160{
Andi Kleenbf5421c2005-12-12 22:17:09 -0800161 struct vm_struct *p, *o;
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700162
163 if ((void __force *)addr <= high_memory)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return;
165
166 /*
167 * __ioremap special-cases the PCI/ISA range by not instantiating a
168 * vm_area and by simply returning an address into the kernel mapping
169 * of ISA space. So handle that here.
170 */
171 if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
172 addr < phys_to_virt(ISA_END_ADDRESS))
173 return;
174
Al Virob16b88e2005-12-15 09:17:50 +0000175 addr = (volatile void __iomem *)(PAGE_MASK & (unsigned long __force)addr);
Andi Kleenbf5421c2005-12-12 22:17:09 -0800176
177 /* Use the vm area unlocked, assuming the caller
178 ensures there isn't another iounmap for the same address
179 in parallel. Reuse of the virtual address is prevented by
180 leaving it in the global lists until we're done with it.
181 cpa takes care of the direct mappings. */
182 read_lock(&vmlist_lock);
183 for (p = vmlist; p; p = p->next) {
184 if (p->addr == addr)
185 break;
186 }
187 read_unlock(&vmlist_lock);
188
189 if (!p) {
190 printk("iounmap: bad address %p\n", addr);
Andrew Mortonc23a4e92005-07-07 17:56:02 -0700191 dump_stack();
Andi Kleenbf5421c2005-12-12 22:17:09 -0800192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
Andi Kleenbf5421c2005-12-12 22:17:09 -0800195 /* Reset the direct mapping. Can block */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if ((p->flags >> 20) && p->phys_addr < virt_to_phys(high_memory) - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 change_page_attr(virt_to_page(__va(p->phys_addr)),
Jeremy Fitzhardinge95851162007-07-21 17:11:35 +0200198 get_vm_area_size(p) >> PAGE_SHIFT,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 PAGE_KERNEL);
200 global_flush_tlb();
201 }
Andi Kleenbf5421c2005-12-12 22:17:09 -0800202
203 /* Finally remove it */
204 o = remove_vm_area((void *)addr);
205 BUG_ON(p != o || o == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 kfree(p);
207}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700208EXPORT_SYMBOL(iounmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Ingo Molnard18d6d62008-01-30 13:33:45 +0100210
211int __initdata early_ioremap_debug;
212
213static int __init early_ioremap_debug_setup(char *str)
214{
215 early_ioremap_debug = 1;
216
Huang, Ying793b24a2008-01-30 13:33:45 +0100217 return 0;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100218}
Huang, Ying793b24a2008-01-30 13:33:45 +0100219early_param("early_ioremap_debug", early_ioremap_debug_setup);
Ingo Molnard18d6d62008-01-30 13:33:45 +0100220
Huang, Ying0947b2f2008-01-30 13:33:44 +0100221static __initdata int after_paging_init;
222static __initdata unsigned long bm_pte[1024]
223 __attribute__((aligned(PAGE_SIZE)));
224
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100225static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100226{
227 return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
228}
229
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100230static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100231{
232 return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
233}
234
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100235void __init early_ioremap_init(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100236{
237 unsigned long *pgd;
238
Ingo Molnard18d6d62008-01-30 13:33:45 +0100239 if (early_ioremap_debug)
240 printk("early_ioremap_init()\n");
241
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100242 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100243 *pgd = __pa(bm_pte) | _PAGE_TABLE;
244 memset(bm_pte, 0, sizeof(bm_pte));
Ingo Molnar0e3a9542008-01-30 13:33:49 +0100245 /*
246 * The boot-ioremap range spans multiple pgds, for which
247 * we are not prepared:
248 */
249 if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
250 WARN_ON(1);
251 printk("pgd %p != %p\n",
252 pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
253 printk("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
254 fix_to_virt(FIX_BTMAP_BEGIN));
255 printk("fix_to_virt(FIX_BTMAP_END): %08lx\n",
256 fix_to_virt(FIX_BTMAP_END));
257
258 printk("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
259 printk("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
260 }
Huang, Ying0947b2f2008-01-30 13:33:44 +0100261}
262
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100263void __init early_ioremap_clear(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100264{
265 unsigned long *pgd;
266
Ingo Molnard18d6d62008-01-30 13:33:45 +0100267 if (early_ioremap_debug)
268 printk("early_ioremap_clear()\n");
269
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100270 pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100271 *pgd = 0;
272 __flush_tlb_all();
273}
274
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100275void __init early_ioremap_reset(void)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100276{
277 enum fixed_addresses idx;
278 unsigned long *pte, phys, addr;
279
280 after_paging_init = 1;
Huang, Ying64a8f852008-01-30 13:33:44 +0100281 for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
Huang, Ying0947b2f2008-01-30 13:33:44 +0100282 addr = fix_to_virt(idx);
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100283 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100284 if (!*pte & _PAGE_PRESENT) {
285 phys = *pte & PAGE_MASK;
286 set_fixmap(idx, phys);
287 }
288 }
289}
290
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100291static void __init __early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100292 unsigned long phys, pgprot_t flags)
293{
294 unsigned long *pte, addr = __fix_to_virt(idx);
295
296 if (idx >= __end_of_fixed_addresses) {
297 BUG();
298 return;
299 }
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100300 pte = early_ioremap_pte(addr);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100301 if (pgprot_val(flags))
302 *pte = (phys & PAGE_MASK) | pgprot_val(flags);
303 else
304 *pte = 0;
305 __flush_tlb_one(addr);
306}
307
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100308static inline void __init early_set_fixmap(enum fixed_addresses idx,
Huang, Ying0947b2f2008-01-30 13:33:44 +0100309 unsigned long phys)
310{
311 if (after_paging_init)
312 set_fixmap(idx, phys);
313 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100314 __early_set_fixmap(idx, phys, PAGE_KERNEL);
Huang, Ying0947b2f2008-01-30 13:33:44 +0100315}
316
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100317static inline void __init early_clear_fixmap(enum fixed_addresses idx)
Huang, Ying0947b2f2008-01-30 13:33:44 +0100318{
319 if (after_paging_init)
320 clear_fixmap(idx);
321 else
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100322 __early_set_fixmap(idx, 0, __pgprot(0));
Huang, Ying0947b2f2008-01-30 13:33:44 +0100323}
324
Ingo Molnar1b42f512008-01-30 13:33:45 +0100325
326int __initdata early_ioremap_nested;
327
Ingo Molnard690b2a2008-01-30 13:33:47 +0100328static int __init check_early_ioremap_leak(void)
329{
330 if (!early_ioremap_nested)
331 return 0;
332
333 printk(KERN_WARNING
334 "Debug warning: early ioremap leak of %d areas detected.\n",
335 early_ioremap_nested);
336 printk(KERN_WARNING
337 "please boot with early_ioremap_debug and report the dmesg.\n");
338 WARN_ON(1);
339
340 return 1;
341}
342late_initcall(check_early_ioremap_leak);
343
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100344void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 unsigned long offset, last_addr;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100347 unsigned int nrpages, nesting;
348 enum fixed_addresses idx0, idx;
349
350 WARN_ON(system_state != SYSTEM_BOOTING);
351
352 nesting = early_ioremap_nested;
Ingo Molnard18d6d62008-01-30 13:33:45 +0100353 if (early_ioremap_debug) {
354 printk("early_ioremap(%08lx, %08lx) [%d] => ",
355 phys_addr, size, nesting);
356 dump_stack();
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Don't allow wraparound or zero size */
360 last_addr = phys_addr + size - 1;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100361 if (!size || last_addr < phys_addr) {
362 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100366 if (nesting >= FIX_BTMAPS_NESTING) {
367 WARN_ON(1);
Ingo Molnar1b42f512008-01-30 13:33:45 +0100368 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100369 }
Ingo Molnar1b42f512008-01-30 13:33:45 +0100370 early_ioremap_nested++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
372 * Mappings have to be page-aligned
373 */
374 offset = phys_addr & ~PAGE_MASK;
375 phys_addr &= PAGE_MASK;
376 size = PAGE_ALIGN(last_addr) - phys_addr;
377
378 /*
379 * Mappings have to fit in the FIX_BTMAP area.
380 */
381 nrpages = size >> PAGE_SHIFT;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100382 if (nrpages > NR_FIX_BTMAPS) {
383 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return NULL;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Ok, go for it..
389 */
Ingo Molnar1b42f512008-01-30 13:33:45 +0100390 idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
391 idx = idx0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100393 early_set_fixmap(idx, phys_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 phys_addr += PAGE_SIZE;
395 --idx;
396 --nrpages;
397 }
Ingo Molnard18d6d62008-01-30 13:33:45 +0100398 if (early_ioremap_debug)
399 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
Ingo Molnar1b42f512008-01-30 13:33:45 +0100400
401 return (void*) (offset + fix_to_virt(idx0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100404void __init early_iounmap(void *addr, unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 unsigned long virt_addr;
407 unsigned long offset;
408 unsigned int nrpages;
409 enum fixed_addresses idx;
Ingo Molnar1b42f512008-01-30 13:33:45 +0100410 unsigned int nesting;
411
412 nesting = --early_ioremap_nested;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100413 WARN_ON(nesting < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Ingo Molnard18d6d62008-01-30 13:33:45 +0100415 if (early_ioremap_debug) {
416 printk("early_iounmap(%p, %08lx) [%d]\n", addr, size, nesting);
417 dump_stack();
418 }
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 virt_addr = (unsigned long)addr;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100421 if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
422 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return;
Ingo Molnarbd796ed2008-01-30 13:33:45 +0100424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 offset = virt_addr & ~PAGE_MASK;
426 nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
427
Ingo Molnar1b42f512008-01-30 13:33:45 +0100428 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 while (nrpages > 0) {
Huang, Yingbeacfaa2008-01-30 13:33:44 +0100430 early_clear_fixmap(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 --idx;
432 --nrpages;
433 }
434}
Ingo Molnar1b42f512008-01-30 13:33:45 +0100435
436void __this_fixmap_does_not_exist(void)
437{
438 WARN_ON(1);
439}