]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/mm/ioremap.c
x86: fix ioremap API
[linux-2.6-omap-h63xx.git] / arch / x86 / mm / ioremap.c
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
5  *
6  * (C) Copyright 1995 1996 Linus Torvalds
7  */
8
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>
15
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>
21
22 #ifdef CONFIG_X86_64
23
24 unsigned long __phys_addr(unsigned long x)
25 {
26         if (x >= __START_KERNEL_map)
27                 return x - __START_KERNEL_map + phys_base;
28         return x - PAGE_OFFSET;
29 }
30 EXPORT_SYMBOL(__phys_addr);
31
32 #endif
33
34 int page_is_ram(unsigned long pagenr)
35 {
36         unsigned long addr, end;
37         int i;
38
39         for (i = 0; i < e820.nr_map; i++) {
40                 /*
41                  * Not usable memory:
42                  */
43                 if (e820.map[i].type != E820_RAM)
44                         continue;
45                 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
46                 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
47
48                 /*
49                  * Sanity check: Some BIOSen report areas as RAM that
50                  * are not. Notably the 640->1Mb area, which is the
51                  * PCI BIOS area.
52                  */
53                 if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
54                     end < (BIOS_END >> PAGE_SHIFT))
55                         continue;
56
57                 if ((pagenr >= addr) && (pagenr < end))
58                         return 1;
59         }
60         return 0;
61 }
62
63 /*
64  * Fix up the linear direct mapping of the kernel to avoid cache attribute
65  * conflicts.
66  */
67 static int ioremap_change_attr(unsigned long phys_addr, unsigned long size,
68                                pgprot_t prot)
69 {
70         unsigned long npages, vaddr, last_addr = phys_addr + size - 1;
71         int err, level;
72
73         /* No change for pages after the last mapping */
74         if (last_addr >= (max_pfn_mapped << PAGE_SHIFT))
75                 return 0;
76
77         npages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
78         vaddr = (unsigned long) __va(phys_addr);
79
80         /*
81          * If there is no identity map for this address,
82          * change_page_attr_addr is unnecessary
83          */
84         if (!lookup_address(vaddr, &level))
85                 return 0;
86
87         /*
88          * Must use an address here and not struct page because the
89          * phys addr can be a in hole between nodes and not have a
90          * memmap entry.
91          */
92         err = change_page_attr_addr(vaddr, npages, prot);
93
94         if (!err)
95                 global_flush_tlb();
96
97         return err;
98 }
99
100 /*
101  * Remap an arbitrary physical address space into the kernel virtual
102  * address space. Needed when the kernel wants to access high addresses
103  * directly.
104  *
105  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
106  * have to convert them into an offset in a page-aligned mapping, but the
107  * caller shouldn't need to know that small detail.
108  */
109 static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
110                                unsigned long flags)
111 {
112         void __iomem *addr;
113         struct vm_struct *area;
114         unsigned long offset, last_addr;
115         pgprot_t pgprot;
116
117         /* Don't allow wraparound or zero size */
118         last_addr = phys_addr + size - 1;
119         if (!size || last_addr < phys_addr)
120                 return NULL;
121
122         /*
123          * Don't remap the low PCI/ISA area, it's always mapped..
124          */
125         if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
126                 return (__force void __iomem *)phys_to_virt(phys_addr);
127
128         /*
129          * Don't allow anybody to remap normal RAM that we're using..
130          */
131         for (offset = phys_addr >> PAGE_SHIFT; offset < max_pfn_mapped &&
132              (offset << PAGE_SHIFT) < last_addr; offset++) {
133                 if (page_is_ram(offset))
134                         return NULL;
135         }
136
137         pgprot = MAKE_GLOBAL(__PAGE_KERNEL | flags);
138
139         /*
140          * Mappings have to be page-aligned
141          */
142         offset = phys_addr & ~PAGE_MASK;
143         phys_addr &= PAGE_MASK;
144         size = PAGE_ALIGN(last_addr+1) - phys_addr;
145
146         /*
147          * Ok, go for it..
148          */
149         area = get_vm_area(size, VM_IOREMAP);
150         if (!area)
151                 return NULL;
152         area->phys_addr = phys_addr;
153         addr = (void __iomem *) area->addr;
154         if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
155                                phys_addr, pgprot)) {
156                 remove_vm_area((void *)(PAGE_MASK & (unsigned long) addr));
157                 return NULL;
158         }
159
160         if (ioremap_change_attr(phys_addr, size, pgprot) < 0) {
161                 vunmap(addr);
162                 return NULL;
163         }
164
165         return (void __iomem *) (offset + (char __iomem *)addr);
166 }
167
168 /**
169  * ioremap_nocache     -   map bus memory into CPU space
170  * @offset:    bus address of the memory
171  * @size:      size of the resource to map
172  *
173  * ioremap_nocache performs a platform specific sequence of operations to
174  * make bus memory CPU accessible via the readb/readw/readl/writeb/
175  * writew/writel functions and the other mmio helpers. The returned
176  * address is not guaranteed to be usable directly as a virtual
177  * address.
178  *
179  * This version of ioremap ensures that the memory is marked uncachable
180  * on the CPU as well as honouring existing caching rules from things like
181  * the PCI bus. Note that there are other caches and buffers on many
182  * busses. In particular driver authors should read up on PCI writes
183  *
184  * It's useful if some control registers are in such an area and
185  * write combining or read caching is not desirable:
186  *
187  * Must be freed with iounmap.
188  */
189 void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
190 {
191         return __ioremap(phys_addr, size, _PAGE_PCD | _PAGE_PWT);
192 }
193 EXPORT_SYMBOL(ioremap_nocache);
194
195 void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
196 {
197         return __ioremap(phys_addr, size, 0);
198 }
199 EXPORT_SYMBOL(ioremap_cache);
200
201 /**
202  * iounmap - Free a IO remapping
203  * @addr: virtual address from ioremap_*
204  *
205  * Caller must ensure there is only one unmapping for the same pointer.
206  */
207 void iounmap(volatile void __iomem *addr)
208 {
209         struct vm_struct *p, *o;
210
211         if ((void __force *)addr <= high_memory)
212                 return;
213
214         /*
215          * __ioremap special-cases the PCI/ISA range by not instantiating a
216          * vm_area and by simply returning an address into the kernel mapping
217          * of ISA space.   So handle that here.
218          */
219         if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
220             addr < phys_to_virt(ISA_END_ADDRESS))
221                 return;
222
223         addr = (volatile void __iomem *)
224                 (PAGE_MASK & (unsigned long __force)addr);
225
226         /* Use the vm area unlocked, assuming the caller
227            ensures there isn't another iounmap for the same address
228            in parallel. Reuse of the virtual address is prevented by
229            leaving it in the global lists until we're done with it.
230            cpa takes care of the direct mappings. */
231         read_lock(&vmlist_lock);
232         for (p = vmlist; p; p = p->next) {
233                 if (p->addr == addr)
234                         break;
235         }
236         read_unlock(&vmlist_lock);
237
238         if (!p) {
239                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
240                 dump_stack();
241                 return;
242         }
243
244         /* Reset the direct mapping. Can block */
245         ioremap_change_attr(p->phys_addr, p->size, PAGE_KERNEL);
246
247         /* Finally remove it */
248         o = remove_vm_area((void *)addr);
249         BUG_ON(p != o || o == NULL);
250         kfree(p);
251 }
252 EXPORT_SYMBOL(iounmap);
253
254 #ifdef CONFIG_X86_32
255
256 int __initdata early_ioremap_debug;
257
258 static int __init early_ioremap_debug_setup(char *str)
259 {
260         early_ioremap_debug = 1;
261
262         return 0;
263 }
264 early_param("early_ioremap_debug", early_ioremap_debug_setup);
265
266 static __initdata int after_paging_init;
267 static __initdata unsigned long bm_pte[1024]
268                                 __attribute__((aligned(PAGE_SIZE)));
269
270 static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
271 {
272         return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
273 }
274
275 static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
276 {
277         return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
278 }
279
280 void __init early_ioremap_init(void)
281 {
282         unsigned long *pgd;
283
284         if (early_ioremap_debug)
285                 printk(KERN_DEBUG "early_ioremap_init()\n");
286
287         pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
288         *pgd = __pa(bm_pte) | _PAGE_TABLE;
289         memset(bm_pte, 0, sizeof(bm_pte));
290         /*
291          * The boot-ioremap range spans multiple pgds, for which
292          * we are not prepared:
293          */
294         if (pgd != early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END))) {
295                 WARN_ON(1);
296                 printk(KERN_WARNING "pgd %p != %p\n",
297                        pgd, early_ioremap_pgd(fix_to_virt(FIX_BTMAP_END)));
298                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
299                        fix_to_virt(FIX_BTMAP_BEGIN));
300                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
301                        fix_to_virt(FIX_BTMAP_END));
302
303                 printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
304                 printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
305                        FIX_BTMAP_BEGIN);
306         }
307 }
308
309 void __init early_ioremap_clear(void)
310 {
311         unsigned long *pgd;
312
313         if (early_ioremap_debug)
314                 printk(KERN_DEBUG "early_ioremap_clear()\n");
315
316         pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
317         *pgd = 0;
318         __flush_tlb_all();
319 }
320
321 void __init early_ioremap_reset(void)
322 {
323         enum fixed_addresses idx;
324         unsigned long *pte, phys, addr;
325
326         after_paging_init = 1;
327         for (idx = FIX_BTMAP_BEGIN; idx >= FIX_BTMAP_END; idx--) {
328                 addr = fix_to_virt(idx);
329                 pte = early_ioremap_pte(addr);
330                 if (!*pte & _PAGE_PRESENT) {
331                         phys = *pte & PAGE_MASK;
332                         set_fixmap(idx, phys);
333                 }
334         }
335 }
336
337 static void __init __early_set_fixmap(enum fixed_addresses idx,
338                                    unsigned long phys, pgprot_t flags)
339 {
340         unsigned long *pte, addr = __fix_to_virt(idx);
341
342         if (idx >= __end_of_fixed_addresses) {
343                 BUG();
344                 return;
345         }
346         pte = early_ioremap_pte(addr);
347         if (pgprot_val(flags))
348                 *pte = (phys & PAGE_MASK) | pgprot_val(flags);
349         else
350                 *pte = 0;
351         __flush_tlb_one(addr);
352 }
353
354 static inline void __init early_set_fixmap(enum fixed_addresses idx,
355                                         unsigned long phys)
356 {
357         if (after_paging_init)
358                 set_fixmap(idx, phys);
359         else
360                 __early_set_fixmap(idx, phys, PAGE_KERNEL);
361 }
362
363 static inline void __init early_clear_fixmap(enum fixed_addresses idx)
364 {
365         if (after_paging_init)
366                 clear_fixmap(idx);
367         else
368                 __early_set_fixmap(idx, 0, __pgprot(0));
369 }
370
371
372 int __initdata early_ioremap_nested;
373
374 static int __init check_early_ioremap_leak(void)
375 {
376         if (!early_ioremap_nested)
377                 return 0;
378
379         printk(KERN_WARNING
380                "Debug warning: early ioremap leak of %d areas detected.\n",
381                early_ioremap_nested);
382         printk(KERN_WARNING
383                "please boot with early_ioremap_debug and report the dmesg.\n");
384         WARN_ON(1);
385
386         return 1;
387 }
388 late_initcall(check_early_ioremap_leak);
389
390 void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
391 {
392         unsigned long offset, last_addr;
393         unsigned int nrpages, nesting;
394         enum fixed_addresses idx0, idx;
395
396         WARN_ON(system_state != SYSTEM_BOOTING);
397
398         nesting = early_ioremap_nested;
399         if (early_ioremap_debug) {
400                 printk(KERN_DEBUG "early_ioremap(%08lx, %08lx) [%d] => ",
401                        phys_addr, size, nesting);
402                 dump_stack();
403         }
404
405         /* Don't allow wraparound or zero size */
406         last_addr = phys_addr + size - 1;
407         if (!size || last_addr < phys_addr) {
408                 WARN_ON(1);
409                 return NULL;
410         }
411
412         if (nesting >= FIX_BTMAPS_NESTING) {
413                 WARN_ON(1);
414                 return NULL;
415         }
416         early_ioremap_nested++;
417         /*
418          * Mappings have to be page-aligned
419          */
420         offset = phys_addr & ~PAGE_MASK;
421         phys_addr &= PAGE_MASK;
422         size = PAGE_ALIGN(last_addr) - phys_addr;
423
424         /*
425          * Mappings have to fit in the FIX_BTMAP area.
426          */
427         nrpages = size >> PAGE_SHIFT;
428         if (nrpages > NR_FIX_BTMAPS) {
429                 WARN_ON(1);
430                 return NULL;
431         }
432
433         /*
434          * Ok, go for it..
435          */
436         idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
437         idx = idx0;
438         while (nrpages > 0) {
439                 early_set_fixmap(idx, phys_addr);
440                 phys_addr += PAGE_SIZE;
441                 --idx;
442                 --nrpages;
443         }
444         if (early_ioremap_debug)
445                 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
446
447         return (void *) (offset + fix_to_virt(idx0));
448 }
449
450 void __init early_iounmap(void *addr, unsigned long size)
451 {
452         unsigned long virt_addr;
453         unsigned long offset;
454         unsigned int nrpages;
455         enum fixed_addresses idx;
456         unsigned int nesting;
457
458         nesting = --early_ioremap_nested;
459         WARN_ON(nesting < 0);
460
461         if (early_ioremap_debug) {
462                 printk(KERN_DEBUG "early_iounmap(%p, %08lx) [%d]\n", addr,
463                        size, nesting);
464                 dump_stack();
465         }
466
467         virt_addr = (unsigned long)addr;
468         if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
469                 WARN_ON(1);
470                 return;
471         }
472         offset = virt_addr & ~PAGE_MASK;
473         nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
474
475         idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
476         while (nrpages > 0) {
477                 early_clear_fixmap(idx);
478                 --idx;
479                 --nrpages;
480         }
481 }
482
483 void __this_fixmap_does_not_exist(void)
484 {
485         WARN_ON(1);
486 }
487
488 #endif /* CONFIG_X86_32 */