Merge branch 'agp-next' of git://git.kernel.org/pub/scm/linux/kernel/git/airlied...
[pandora-kernel.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 #include <linux/mmiotrace.h>
16
17 #include <asm/cacheflush.h>
18 #include <asm/e820.h>
19 #include <asm/fixmap.h>
20 #include <asm/pgtable.h>
21 #include <asm/tlbflush.h>
22 #include <asm/pgalloc.h>
23 #include <asm/pat.h>
24
25 #include "physaddr.h"
26
27 int page_is_ram(unsigned long pagenr)
28 {
29         resource_size_t addr, end;
30         int i;
31
32         /*
33          * A special case is the first 4Kb of memory;
34          * This is a BIOS owned area, not kernel ram, but generally
35          * not listed as such in the E820 table.
36          */
37         if (pagenr == 0)
38                 return 0;
39
40         /*
41          * Second special case: Some BIOSen report the PC BIOS
42          * area (640->1Mb) as ram even though it is not.
43          */
44         if (pagenr >= (BIOS_BEGIN >> PAGE_SHIFT) &&
45                     pagenr < (BIOS_END >> PAGE_SHIFT))
46                 return 0;
47
48         for (i = 0; i < e820.nr_map; i++) {
49                 /*
50                  * Not usable memory:
51                  */
52                 if (e820.map[i].type != E820_RAM)
53                         continue;
54                 addr = (e820.map[i].addr + PAGE_SIZE-1) >> PAGE_SHIFT;
55                 end = (e820.map[i].addr + e820.map[i].size) >> PAGE_SHIFT;
56
57
58                 if ((pagenr >= addr) && (pagenr < end))
59                         return 1;
60         }
61         return 0;
62 }
63
64 /*
65  * Fix up the linear direct mapping of the kernel to avoid cache attribute
66  * conflicts.
67  */
68 int ioremap_change_attr(unsigned long vaddr, unsigned long size,
69                                unsigned long prot_val)
70 {
71         unsigned long nrpages = size >> PAGE_SHIFT;
72         int err;
73
74         switch (prot_val) {
75         case _PAGE_CACHE_UC:
76         default:
77                 err = _set_memory_uc(vaddr, nrpages);
78                 break;
79         case _PAGE_CACHE_WC:
80                 err = _set_memory_wc(vaddr, nrpages);
81                 break;
82         case _PAGE_CACHE_WB:
83                 err = _set_memory_wb(vaddr, nrpages);
84                 break;
85         }
86
87         return err;
88 }
89
90 /*
91  * Remap an arbitrary physical address space into the kernel virtual
92  * address space. Needed when the kernel wants to access high addresses
93  * directly.
94  *
95  * NOTE! We need to allow non-page-aligned mappings too: we will obviously
96  * have to convert them into an offset in a page-aligned mapping, but the
97  * caller shouldn't need to know that small detail.
98  */
99 static void __iomem *__ioremap_caller(resource_size_t phys_addr,
100                 unsigned long size, unsigned long prot_val, void *caller)
101 {
102         unsigned long pfn, offset, vaddr;
103         resource_size_t last_addr;
104         const resource_size_t unaligned_phys_addr = phys_addr;
105         const unsigned long unaligned_size = size;
106         struct vm_struct *area;
107         unsigned long new_prot_val;
108         pgprot_t prot;
109         int retval;
110         void __iomem *ret_addr;
111
112         /* Don't allow wraparound or zero size */
113         last_addr = phys_addr + size - 1;
114         if (!size || last_addr < phys_addr)
115                 return NULL;
116
117         if (!phys_addr_valid(phys_addr)) {
118                 printk(KERN_WARNING "ioremap: invalid physical address %llx\n",
119                        (unsigned long long)phys_addr);
120                 WARN_ON_ONCE(1);
121                 return NULL;
122         }
123
124         /*
125          * Don't remap the low PCI/ISA area, it's always mapped..
126          */
127         if (is_ISA_range(phys_addr, last_addr))
128                 return (__force void __iomem *)phys_to_virt(phys_addr);
129
130         /*
131          * Check if the request spans more than any BAR in the iomem resource
132          * tree.
133          */
134         WARN_ONCE(iomem_map_sanity_check(phys_addr, size),
135                   KERN_INFO "Info: mapping multiple BARs. Your kernel is fine.");
136
137         /*
138          * Don't allow anybody to remap normal RAM that we're using..
139          */
140         for (pfn = phys_addr >> PAGE_SHIFT;
141                                 (pfn << PAGE_SHIFT) < (last_addr & PAGE_MASK);
142                                 pfn++) {
143
144                 int is_ram = page_is_ram(pfn);
145
146                 if (is_ram && pfn_valid(pfn) && !PageReserved(pfn_to_page(pfn)))
147                         return NULL;
148                 WARN_ON_ONCE(is_ram);
149         }
150
151         /*
152          * Mappings have to be page-aligned
153          */
154         offset = phys_addr & ~PAGE_MASK;
155         phys_addr &= PAGE_MASK;
156         size = PAGE_ALIGN(last_addr+1) - phys_addr;
157
158         retval = reserve_memtype(phys_addr, (u64)phys_addr + size,
159                                                 prot_val, &new_prot_val);
160         if (retval) {
161                 pr_debug("Warning: reserve_memtype returned %d\n", retval);
162                 return NULL;
163         }
164
165         if (prot_val != new_prot_val) {
166                 /*
167                  * Do not fallback to certain memory types with certain
168                  * requested type:
169                  * - request is uc-, return cannot be write-back
170                  * - request is uc-, return cannot be write-combine
171                  * - request is write-combine, return cannot be write-back
172                  */
173                 if ((prot_val == _PAGE_CACHE_UC_MINUS &&
174                      (new_prot_val == _PAGE_CACHE_WB ||
175                       new_prot_val == _PAGE_CACHE_WC)) ||
176                     (prot_val == _PAGE_CACHE_WC &&
177                      new_prot_val == _PAGE_CACHE_WB)) {
178                         pr_debug(
179                 "ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
180                                 (unsigned long long)phys_addr,
181                                 (unsigned long long)(phys_addr + size),
182                                 prot_val, new_prot_val);
183                         free_memtype(phys_addr, phys_addr + size);
184                         return NULL;
185                 }
186                 prot_val = new_prot_val;
187         }
188
189         switch (prot_val) {
190         case _PAGE_CACHE_UC:
191         default:
192                 prot = PAGE_KERNEL_IO_NOCACHE;
193                 break;
194         case _PAGE_CACHE_UC_MINUS:
195                 prot = PAGE_KERNEL_IO_UC_MINUS;
196                 break;
197         case _PAGE_CACHE_WC:
198                 prot = PAGE_KERNEL_IO_WC;
199                 break;
200         case _PAGE_CACHE_WB:
201                 prot = PAGE_KERNEL_IO;
202                 break;
203         }
204
205         /*
206          * Ok, go for it..
207          */
208         area = get_vm_area_caller(size, VM_IOREMAP, caller);
209         if (!area)
210                 return NULL;
211         area->phys_addr = phys_addr;
212         vaddr = (unsigned long) area->addr;
213
214         if (kernel_map_sync_memtype(phys_addr, size, prot_val)) {
215                 free_memtype(phys_addr, phys_addr + size);
216                 free_vm_area(area);
217                 return NULL;
218         }
219
220         if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
221                 free_memtype(phys_addr, phys_addr + size);
222                 free_vm_area(area);
223                 return NULL;
224         }
225
226         ret_addr = (void __iomem *) (vaddr + offset);
227         mmiotrace_ioremap(unaligned_phys_addr, unaligned_size, ret_addr);
228
229         return ret_addr;
230 }
231
232 /**
233  * ioremap_nocache     -   map bus memory into CPU space
234  * @offset:    bus address of the memory
235  * @size:      size of the resource to map
236  *
237  * ioremap_nocache performs a platform specific sequence of operations to
238  * make bus memory CPU accessible via the readb/readw/readl/writeb/
239  * writew/writel functions and the other mmio helpers. The returned
240  * address is not guaranteed to be usable directly as a virtual
241  * address.
242  *
243  * This version of ioremap ensures that the memory is marked uncachable
244  * on the CPU as well as honouring existing caching rules from things like
245  * the PCI bus. Note that there are other caches and buffers on many
246  * busses. In particular driver authors should read up on PCI writes
247  *
248  * It's useful if some control registers are in such an area and
249  * write combining or read caching is not desirable:
250  *
251  * Must be freed with iounmap.
252  */
253 void __iomem *ioremap_nocache(resource_size_t phys_addr, unsigned long size)
254 {
255         /*
256          * Ideally, this should be:
257          *      pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
258          *
259          * Till we fix all X drivers to use ioremap_wc(), we will use
260          * UC MINUS.
261          */
262         unsigned long val = _PAGE_CACHE_UC_MINUS;
263
264         return __ioremap_caller(phys_addr, size, val,
265                                 __builtin_return_address(0));
266 }
267 EXPORT_SYMBOL(ioremap_nocache);
268
269 /**
270  * ioremap_wc   -       map memory into CPU space write combined
271  * @offset:     bus address of the memory
272  * @size:       size of the resource to map
273  *
274  * This version of ioremap ensures that the memory is marked write combining.
275  * Write combining allows faster writes to some hardware devices.
276  *
277  * Must be freed with iounmap.
278  */
279 void __iomem *ioremap_wc(resource_size_t phys_addr, unsigned long size)
280 {
281         if (pat_enabled)
282                 return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WC,
283                                         __builtin_return_address(0));
284         else
285                 return ioremap_nocache(phys_addr, size);
286 }
287 EXPORT_SYMBOL(ioremap_wc);
288
289 void __iomem *ioremap_cache(resource_size_t phys_addr, unsigned long size)
290 {
291         return __ioremap_caller(phys_addr, size, _PAGE_CACHE_WB,
292                                 __builtin_return_address(0));
293 }
294 EXPORT_SYMBOL(ioremap_cache);
295
296 static void __iomem *ioremap_default(resource_size_t phys_addr,
297                                         unsigned long size)
298 {
299         unsigned long flags;
300         void __iomem *ret;
301         int err;
302
303         /*
304          * - WB for WB-able memory and no other conflicting mappings
305          * - UC_MINUS for non-WB-able memory with no other conflicting mappings
306          * - Inherit from confliting mappings otherwise
307          */
308         err = reserve_memtype(phys_addr, phys_addr + size,
309                                 _PAGE_CACHE_WB, &flags);
310         if (err < 0)
311                 return NULL;
312
313         ret = __ioremap_caller(phys_addr, size, flags,
314                                __builtin_return_address(0));
315
316         free_memtype(phys_addr, phys_addr + size);
317         return ret;
318 }
319
320 void __iomem *ioremap_prot(resource_size_t phys_addr, unsigned long size,
321                                 unsigned long prot_val)
322 {
323         return __ioremap_caller(phys_addr, size, (prot_val & _PAGE_CACHE_MASK),
324                                 __builtin_return_address(0));
325 }
326 EXPORT_SYMBOL(ioremap_prot);
327
328 /**
329  * iounmap - Free a IO remapping
330  * @addr: virtual address from ioremap_*
331  *
332  * Caller must ensure there is only one unmapping for the same pointer.
333  */
334 void iounmap(volatile void __iomem *addr)
335 {
336         struct vm_struct *p, *o;
337
338         if ((void __force *)addr <= high_memory)
339                 return;
340
341         /*
342          * __ioremap special-cases the PCI/ISA range by not instantiating a
343          * vm_area and by simply returning an address into the kernel mapping
344          * of ISA space.   So handle that here.
345          */
346         if ((void __force *)addr >= phys_to_virt(ISA_START_ADDRESS) &&
347             (void __force *)addr < phys_to_virt(ISA_END_ADDRESS))
348                 return;
349
350         addr = (volatile void __iomem *)
351                 (PAGE_MASK & (unsigned long __force)addr);
352
353         mmiotrace_iounmap(addr);
354
355         /* Use the vm area unlocked, assuming the caller
356            ensures there isn't another iounmap for the same address
357            in parallel. Reuse of the virtual address is prevented by
358            leaving it in the global lists until we're done with it.
359            cpa takes care of the direct mappings. */
360         read_lock(&vmlist_lock);
361         for (p = vmlist; p; p = p->next) {
362                 if (p->addr == (void __force *)addr)
363                         break;
364         }
365         read_unlock(&vmlist_lock);
366
367         if (!p) {
368                 printk(KERN_ERR "iounmap: bad address %p\n", addr);
369                 dump_stack();
370                 return;
371         }
372
373         free_memtype(p->phys_addr, p->phys_addr + get_vm_area_size(p));
374
375         /* Finally remove it */
376         o = remove_vm_area((void __force *)addr);
377         BUG_ON(p != o || o == NULL);
378         kfree(p);
379 }
380 EXPORT_SYMBOL(iounmap);
381
382 /*
383  * Convert a physical pointer to a virtual kernel pointer for /dev/mem
384  * access
385  */
386 void *xlate_dev_mem_ptr(unsigned long phys)
387 {
388         void *addr;
389         unsigned long start = phys & PAGE_MASK;
390
391         /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
392         if (page_is_ram(start >> PAGE_SHIFT))
393                 return __va(phys);
394
395         addr = (void __force *)ioremap_default(start, PAGE_SIZE);
396         if (addr)
397                 addr = (void *)((unsigned long)addr | (phys & ~PAGE_MASK));
398
399         return addr;
400 }
401
402 void unxlate_dev_mem_ptr(unsigned long phys, void *addr)
403 {
404         if (page_is_ram(phys >> PAGE_SHIFT))
405                 return;
406
407         iounmap((void __iomem *)((unsigned long)addr & PAGE_MASK));
408         return;
409 }
410
411 static int __initdata early_ioremap_debug;
412
413 static int __init early_ioremap_debug_setup(char *str)
414 {
415         early_ioremap_debug = 1;
416
417         return 0;
418 }
419 early_param("early_ioremap_debug", early_ioremap_debug_setup);
420
421 static __initdata int after_paging_init;
422 static pte_t bm_pte[PAGE_SIZE/sizeof(pte_t)] __page_aligned_bss;
423
424 static inline pmd_t * __init early_ioremap_pmd(unsigned long addr)
425 {
426         /* Don't assume we're using swapper_pg_dir at this point */
427         pgd_t *base = __va(read_cr3());
428         pgd_t *pgd = &base[pgd_index(addr)];
429         pud_t *pud = pud_offset(pgd, addr);
430         pmd_t *pmd = pmd_offset(pud, addr);
431
432         return pmd;
433 }
434
435 static inline pte_t * __init early_ioremap_pte(unsigned long addr)
436 {
437         return &bm_pte[pte_index(addr)];
438 }
439
440 static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
441
442 void __init early_ioremap_init(void)
443 {
444         pmd_t *pmd;
445         int i;
446
447         if (early_ioremap_debug)
448                 printk(KERN_INFO "early_ioremap_init()\n");
449
450         for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
451                 slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
452
453         pmd = early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN));
454         memset(bm_pte, 0, sizeof(bm_pte));
455         pmd_populate_kernel(&init_mm, pmd, bm_pte);
456
457         /*
458          * The boot-ioremap range spans multiple pmds, for which
459          * we are not prepared:
460          */
461         if (pmd != early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END))) {
462                 WARN_ON(1);
463                 printk(KERN_WARNING "pmd %p != %p\n",
464                        pmd, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END)));
465                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
466                         fix_to_virt(FIX_BTMAP_BEGIN));
467                 printk(KERN_WARNING "fix_to_virt(FIX_BTMAP_END):   %08lx\n",
468                         fix_to_virt(FIX_BTMAP_END));
469
470                 printk(KERN_WARNING "FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
471                 printk(KERN_WARNING "FIX_BTMAP_BEGIN:     %d\n",
472                        FIX_BTMAP_BEGIN);
473         }
474 }
475
476 void __init early_ioremap_reset(void)
477 {
478         after_paging_init = 1;
479 }
480
481 static void __init __early_set_fixmap(enum fixed_addresses idx,
482                                       phys_addr_t phys, pgprot_t flags)
483 {
484         unsigned long addr = __fix_to_virt(idx);
485         pte_t *pte;
486
487         if (idx >= __end_of_fixed_addresses) {
488                 BUG();
489                 return;
490         }
491         pte = early_ioremap_pte(addr);
492
493         if (pgprot_val(flags))
494                 set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
495         else
496                 pte_clear(&init_mm, addr, pte);
497         __flush_tlb_one(addr);
498 }
499
500 static inline void __init early_set_fixmap(enum fixed_addresses idx,
501                                            phys_addr_t phys, pgprot_t prot)
502 {
503         if (after_paging_init)
504                 __set_fixmap(idx, phys, prot);
505         else
506                 __early_set_fixmap(idx, phys, prot);
507 }
508
509 static inline void __init early_clear_fixmap(enum fixed_addresses idx)
510 {
511         if (after_paging_init)
512                 clear_fixmap(idx);
513         else
514                 __early_set_fixmap(idx, 0, __pgprot(0));
515 }
516
517 static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
518 static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
519
520 static int __init check_early_ioremap_leak(void)
521 {
522         int count = 0;
523         int i;
524
525         for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
526                 if (prev_map[i])
527                         count++;
528
529         if (!count)
530                 return 0;
531         WARN(1, KERN_WARNING
532                "Debug warning: early ioremap leak of %d areas detected.\n",
533                 count);
534         printk(KERN_WARNING
535                 "please boot with early_ioremap_debug and report the dmesg.\n");
536
537         return 1;
538 }
539 late_initcall(check_early_ioremap_leak);
540
541 static void __init __iomem *
542 __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
543 {
544         unsigned long offset;
545         resource_size_t last_addr;
546         unsigned int nrpages;
547         enum fixed_addresses idx0, idx;
548         int i, slot;
549
550         WARN_ON(system_state != SYSTEM_BOOTING);
551
552         slot = -1;
553         for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
554                 if (!prev_map[i]) {
555                         slot = i;
556                         break;
557                 }
558         }
559
560         if (slot < 0) {
561                 printk(KERN_INFO "early_iomap(%08llx, %08lx) not found slot\n",
562                          (u64)phys_addr, size);
563                 WARN_ON(1);
564                 return NULL;
565         }
566
567         if (early_ioremap_debug) {
568                 printk(KERN_INFO "early_ioremap(%08llx, %08lx) [%d] => ",
569                        (u64)phys_addr, size, slot);
570                 dump_stack();
571         }
572
573         /* Don't allow wraparound or zero size */
574         last_addr = phys_addr + size - 1;
575         if (!size || last_addr < phys_addr) {
576                 WARN_ON(1);
577                 return NULL;
578         }
579
580         prev_size[slot] = size;
581         /*
582          * Mappings have to be page-aligned
583          */
584         offset = phys_addr & ~PAGE_MASK;
585         phys_addr &= PAGE_MASK;
586         size = PAGE_ALIGN(last_addr + 1) - phys_addr;
587
588         /*
589          * Mappings have to fit in the FIX_BTMAP area.
590          */
591         nrpages = size >> PAGE_SHIFT;
592         if (nrpages > NR_FIX_BTMAPS) {
593                 WARN_ON(1);
594                 return NULL;
595         }
596
597         /*
598          * Ok, go for it..
599          */
600         idx0 = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
601         idx = idx0;
602         while (nrpages > 0) {
603                 early_set_fixmap(idx, phys_addr, prot);
604                 phys_addr += PAGE_SIZE;
605                 --idx;
606                 --nrpages;
607         }
608         if (early_ioremap_debug)
609                 printk(KERN_CONT "%08lx + %08lx\n", offset, slot_virt[slot]);
610
611         prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
612         return prev_map[slot];
613 }
614
615 /* Remap an IO device */
616 void __init __iomem *
617 early_ioremap(resource_size_t phys_addr, unsigned long size)
618 {
619         return __early_ioremap(phys_addr, size, PAGE_KERNEL_IO);
620 }
621
622 /* Remap memory */
623 void __init __iomem *
624 early_memremap(resource_size_t phys_addr, unsigned long size)
625 {
626         return __early_ioremap(phys_addr, size, PAGE_KERNEL);
627 }
628
629 void __init early_iounmap(void __iomem *addr, unsigned long size)
630 {
631         unsigned long virt_addr;
632         unsigned long offset;
633         unsigned int nrpages;
634         enum fixed_addresses idx;
635         int i, slot;
636
637         slot = -1;
638         for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
639                 if (prev_map[i] == addr) {
640                         slot = i;
641                         break;
642                 }
643         }
644
645         if (slot < 0) {
646                 printk(KERN_INFO "early_iounmap(%p, %08lx) not found slot\n",
647                          addr, size);
648                 WARN_ON(1);
649                 return;
650         }
651
652         if (prev_size[slot] != size) {
653                 printk(KERN_INFO "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
654                          addr, size, slot, prev_size[slot]);
655                 WARN_ON(1);
656                 return;
657         }
658
659         if (early_ioremap_debug) {
660                 printk(KERN_INFO "early_iounmap(%p, %08lx) [%d]\n", addr,
661                        size, slot);
662                 dump_stack();
663         }
664
665         virt_addr = (unsigned long)addr;
666         if (virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)) {
667                 WARN_ON(1);
668                 return;
669         }
670         offset = virt_addr & ~PAGE_MASK;
671         nrpages = PAGE_ALIGN(offset + size - 1) >> PAGE_SHIFT;
672
673         idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
674         while (nrpages > 0) {
675                 early_clear_fixmap(idx);
676                 --idx;
677                 --nrpages;
678         }
679         prev_map[slot] = NULL;
680 }