x86: make sure initmem is writable
[safe/jmp/linux-2.6] / 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 enum ioremap_mode {
23         IOR_MODE_UNCACHED,
24         IOR_MODE_CACHED,
25 };
26
27 #ifdef CONFIG_X86_64
28
29 unsigned long __phys_addr(unsigned long x)
30 {
31         if (x >= __START_KERNEL_map)
32                 return x - __START_KERNEL_map + phys_base;
33         return x - PAGE_OFFSET;
34 }
35 EXPORT_SYMBOL(__phys_addr);
36
37 #endif
38
39 int page_is_ram(unsigned long pagenr)
40 {
41         unsigned long addr, end;
42         int i;
43
44         for (i = 0; i < e820.nr_map; i++) {
45                 /*
46                  * Not usable memory:
47                  */
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;
52
53                 /*
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.
57                  */
58                 if (addr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
59                     end < (BIOS_END >> PAGE_SHIFT))
60                         continue;
61
62                 if ((pagenr >= addr) && (pagenr < end))
63                         return 1;
64         }
65         return 0;
66 }
67
68 /*
69  * Fix up the linear direct mapping of the kernel to avoid cache attribute
70  * conflicts.
71  */
72 static int ioremap_change_attr(unsigned long paddr, unsigned long size,
73                                enum ioremap_mode mode)
74 {
75         unsigned long vaddr = (unsigned long)__va(paddr);
76         unsigned long nrpages = size >> PAGE_SHIFT;
77         int err, level;
78
79         /* No change for pages after the last mapping */
80         if ((paddr + size - 1) >= (max_pfn_mapped << PAGE_SHIFT))
81                 return 0;
82
83         /*
84          * If there is no identity map for this address,
85          * change_page_attr_addr is unnecessary
86          */
87         if (!lookup_address(vaddr, &level))
88                 return 0;
89
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;
98         }
99
100         return err;
101 }
102
103 /*
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.
107  *
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.
111  */
112 static void __iomem *__ioremap(unsigned long phys_addr, unsigned long size,
113                                enum ioremap_mode mode)
114 {
115         void __iomem *addr;
116         struct vm_struct *area;
117         unsigned long offset, last_addr;
118         pgprot_t prot;
119
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;
124
125         /*
126          * Don't remap the low PCI/ISA area, it's always mapped..
127          */
128         if (phys_addr >= ISA_START_ADDRESS && last_addr < ISA_END_ADDRESS)
129                 return (__force void __iomem *)phys_to_virt(phys_addr);
130
131         /*
132          * Don't allow anybody to remap normal RAM that we're using..
133          */
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;
138         }
139
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;
148         }
149
150         /*
151          * Mappings have to be page-aligned
152          */
153         offset = phys_addr & ~PAGE_MASK;
154         phys_addr &= PAGE_MASK;
155         size = PAGE_ALIGN(last_addr+1) - phys_addr;
156
157         /*
158          * Ok, go for it..
159          */
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;
169         }
170
171         if (ioremap_change_attr(phys_addr, size, mode) < 0) {
172                 vunmap(addr);
173                 return NULL;
174         }
175
176         return (void __iomem *) (offset + (char __iomem *)addr);
177 }
178
179 /**
180  * ioremap_nocache     -   map bus memory into CPU space
181  * @offset:    bus address of the memory
182  * @size:      size of the resource to map
183  *
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.
189  *
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
194  *
195  * It's useful if some control registers are in such an area and
196  * write combining or read caching is not desirable:
197  *
198  * Must be freed with iounmap.
199  */
200 void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
201 {
202         return __ioremap(phys_addr, size, IOR_MODE_UNCACHED);
203 }
204 EXPORT_SYMBOL(ioremap_nocache);
205
206 void __iomem *ioremap_cache(unsigned long phys_addr, unsigned long size)
207 {
208         return __ioremap(phys_addr, size, IOR_MODE_CACHED);
209 }
210 EXPORT_SYMBOL(ioremap_cache);
211
212 /**
213  * iounmap - Free a IO remapping
214  * @addr: virtual address from ioremap_*
215  *
216  * Caller must ensure there is only one unmapping for the same pointer.
217  */
218 void iounmap(volatile void __iomem *addr)
219 {
220         struct vm_struct *p, *o;
221
222         if ((void __force *)addr <= high_memory)
223                 return;
224
225         /*
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.
229          */
230         if (addr >= phys_to_virt(ISA_START_ADDRESS) &&
231             addr < phys_to_virt(ISA_END_ADDRESS))
232                 return;
233
234         addr = (volatile void __iomem *)
235                 (PAGE_MASK & (unsigned long __force)addr);
236
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;
246         }
247         read_unlock(&vmlist_lock);
248
249         if (!p) {
250                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
251                 dump_stack();
252                 return;
253         }
254
255         /* Reset the direct mapping. Can block */
256         ioremap_change_attr(p->phys_addr, p->size, IOR_MODE_CACHED);
257
258         /* Finally remove it */
259         o = remove_vm_area((void *)addr);
260         BUG_ON(p != o || o == NULL);
261         kfree(p);
262 }
263 EXPORT_SYMBOL(iounmap);
264
265 #ifdef CONFIG_X86_32
266
267 int __initdata early_ioremap_debug;
268
269 static int __init early_ioremap_debug_setup(char *str)
270 {
271         early_ioremap_debug = 1;
272
273         return 0;
274 }
275 early_param("early_ioremap_debug", early_ioremap_debug_setup);
276
277 static __initdata int after_paging_init;
278 static __initdata unsigned long bm_pte[1024]
279                                 __attribute__((aligned(PAGE_SIZE)));
280
281 static inline unsigned long * __init early_ioremap_pgd(unsigned long addr)
282 {
283         return (unsigned long *)swapper_pg_dir + ((addr >> 22) & 1023);
284 }
285
286 static inline unsigned long * __init early_ioremap_pte(unsigned long addr)
287 {
288         return bm_pte + ((addr >> PAGE_SHIFT) & 1023);
289 }
290
291 void __init early_ioremap_init(void)
292 {
293         unsigned long *pgd;
294
295         if (early_ioremap_debug)
296                 printk(KERN_DEBUG "early_ioremap_init()\n");
297
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));
301         /*
302          * The boot-ioremap range spans multiple pgds, for which
303          * we are not prepared:
304          */
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));
313
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);
317         }
318 }
319
320 void __init early_ioremap_clear(void)
321 {
322         unsigned long *pgd;
323
324         if (early_ioremap_debug)
325                 printk(KERN_DEBUG "early_ioremap_clear()\n");
326
327         pgd = early_ioremap_pgd(fix_to_virt(FIX_BTMAP_BEGIN));
328         *pgd = 0;
329         __flush_tlb_all();
330 }
331
332 void __init early_ioremap_reset(void)
333 {
334         enum fixed_addresses idx;
335         unsigned long *pte, phys, addr;
336
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);
344                 }
345         }
346 }
347
348 static void __init __early_set_fixmap(enum fixed_addresses idx,
349                                    unsigned long phys, pgprot_t flags)
350 {
351         unsigned long *pte, addr = __fix_to_virt(idx);
352
353         if (idx >= __end_of_fixed_addresses) {
354                 BUG();
355                 return;
356         }
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);
363 }
364
365 static inline void __init early_set_fixmap(enum fixed_addresses idx,
366                                         unsigned long phys)
367 {
368         if (after_paging_init)
369                 set_fixmap(idx, phys);
370         else
371                 __early_set_fixmap(idx, phys, PAGE_KERNEL);
372 }
373
374 static inline void __init early_clear_fixmap(enum fixed_addresses idx)
375 {
376         if (after_paging_init)
377                 clear_fixmap(idx);
378         else
379                 __early_set_fixmap(idx, 0, __pgprot(0));
380 }
381
382
383 int __initdata early_ioremap_nested;
384
385 static int __init check_early_ioremap_leak(void)
386 {
387         if (!early_ioremap_nested)
388                 return 0;
389
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);
396
397         return 1;
398 }
399 late_initcall(check_early_ioremap_leak);
400
401 void __init *early_ioremap(unsigned long phys_addr, unsigned long size)
402 {
403         unsigned long offset, last_addr;
404         unsigned int nrpages, nesting;
405         enum fixed_addresses idx0, idx;
406
407         WARN_ON(system_state != SYSTEM_BOOTING);
408
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();
414         }
415
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;
421         }
422
423         if (nesting >= FIX_BTMAPS_NESTING) {
424                 WARN_ON(1);
425                 return NULL;
426         }
427         early_ioremap_nested++;
428         /*
429          * Mappings have to be page-aligned
430          */
431         offset = phys_addr & ~PAGE_MASK;
432         phys_addr &= PAGE_MASK;
433         size = PAGE_ALIGN(last_addr) - phys_addr;
434
435         /*
436          * Mappings have to fit in the FIX_BTMAP area.
437          */
438         nrpages = size >> PAGE_SHIFT;
439         if (nrpages > NR_FIX_BTMAPS) {
440                 WARN_ON(1);
441                 return NULL;
442         }
443
444         /*
445          * Ok, go for it..
446          */
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;
454         }
455         if (early_ioremap_debug)
456                 printk(KERN_CONT "%08lx + %08lx\n", offset, fix_to_virt(idx0));
457
458         return (void *) (offset + fix_to_virt(idx0));
459 }
460
461 void __init early_iounmap(void *addr, unsigned long size)
462 {
463         unsigned long virt_addr;
464         unsigned long offset;
465         unsigned int nrpages;
466         enum fixed_addresses idx;
467         unsigned int nesting;
468
469         nesting = --early_ioremap_nested;
470         WARN_ON(nesting < 0);
471
472         if (early_ioremap_debug) {
473                 printk(KERN_DEBUG "early_iounmap(%p, %08lx) [%d]\n", addr,
474                        size, nesting);
475                 dump_stack();
476         }
477
478         virt_addr = (unsigned long)addr;
479         if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
480                 WARN_ON(1);
481                 return;
482         }
483         offset = virt_addr & ~PAGE_MASK;
484         nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
485
486         idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*nesting;
487         while (nrpages > 0) {
488                 early_clear_fixmap(idx);
489                 --idx;
490                 --nrpages;
491         }
492 }
493
494 void __this_fixmap_does_not_exist(void)
495 {
496         WARN_ON(1);
497 }
498
499 #endif /* CONFIG_X86_32 */