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
9 #include <linux/bootmem.h>
10 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/vmalloc.h>
15 #include <linux/mmiotrace.h>
17 #include <asm/cacheflush.h>
19 #include <asm/fixmap.h>
20 #include <asm/pgtable.h>
21 #include <asm/tlbflush.h>
22 #include <asm/pgalloc.h>
28 * Fix up the linear direct mapping of the kernel to avoid cache attribute
31 int ioremap_change_attr(unsigned long vaddr
, unsigned long size
,
32 unsigned long prot_val
)
34 unsigned long nrpages
= size
>> PAGE_SHIFT
;
40 err
= _set_memory_uc(vaddr
, nrpages
);
43 err
= _set_memory_wc(vaddr
, nrpages
);
46 err
= _set_memory_wb(vaddr
, nrpages
);
54 static void ident_pte_range(unsigned long paddr
, unsigned long vaddr
,
55 pmd_t
*ppmd
, pmd_t
*vpmd
, unsigned long end
)
57 pte_t
*ppte
= pte_offset_kernel(ppmd
, paddr
);
58 pte_t
*vpte
= pte_offset_kernel(vpmd
, vaddr
);
62 } while (ppte
++, vpte
++, vaddr
+= PAGE_SIZE
, vaddr
!= end
);
65 static int ident_pmd_range(unsigned long paddr
, unsigned long vaddr
,
66 pud_t
*ppud
, pud_t
*vpud
, unsigned long end
)
68 pmd_t
*ppmd
= pmd_offset(ppud
, paddr
);
69 pmd_t
*vpmd
= pmd_offset(vpud
, vaddr
);
73 next
= pmd_addr_end(vaddr
, end
);
75 if (!pmd_present(*ppmd
)) {
76 pte_t
*ppte
= (pte_t
*)get_zeroed_page(GFP_KERNEL
);
80 set_pmd(ppmd
, __pmd(_KERNPG_TABLE
| __pa(ppte
)));
83 ident_pte_range(paddr
, vaddr
, ppmd
, vpmd
, next
);
84 } while (ppmd
++, vpmd
++, vaddr
= next
, vaddr
!= end
);
89 static int ident_pud_range(unsigned long paddr
, unsigned long vaddr
,
90 pgd_t
*ppgd
, pgd_t
*vpgd
, unsigned long end
)
92 pud_t
*ppud
= pud_offset(ppgd
, paddr
);
93 pud_t
*vpud
= pud_offset(vpgd
, vaddr
);
97 next
= pud_addr_end(vaddr
, end
);
99 if (!pud_present(*ppud
)) {
100 pmd_t
*ppmd
= (pmd_t
*)get_zeroed_page(GFP_KERNEL
);
104 set_pud(ppud
, __pud(_KERNPG_TABLE
| __pa(ppmd
)));
107 if (ident_pmd_range(paddr
, vaddr
, ppud
, vpud
, next
))
109 } while (ppud
++, vpud
++, vaddr
= next
, vaddr
!= end
);
114 static int insert_identity_mapping(resource_size_t paddr
, unsigned long vaddr
,
117 unsigned long end
= vaddr
+ size
;
121 /* Don't map over the guard hole. */
122 if (paddr
>= 0x800000000000 || paddr
+ size
> 0x800000000000)
125 ppgd
= __va(real_mode_header
->trampoline_pgd
) + pgd_index(paddr
);
127 vpgd
= pgd_offset_k(vaddr
);
129 next
= pgd_addr_end(vaddr
, end
);
131 if (!pgd_present(*ppgd
)) {
132 pud_t
*ppud
= (pud_t
*)get_zeroed_page(GFP_KERNEL
);
136 set_pgd(ppgd
, __pgd(_KERNPG_TABLE
| __pa(ppud
)));
139 if (ident_pud_range(paddr
, vaddr
, ppgd
, vpgd
, next
))
141 } while (ppgd
++, vpgd
++, vaddr
= next
, vaddr
!= end
);
146 static inline int insert_identity_mapping(resource_size_t paddr
,
152 #endif /* CONFIG_X86_64 */
155 * Remap an arbitrary physical address space into the kernel virtual
156 * address space. Needed when the kernel wants to access high addresses
159 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
160 * have to convert them into an offset in a page-aligned mapping, but the
161 * caller shouldn't need to know that small detail.
163 static void __iomem
*__ioremap_caller(resource_size_t phys_addr
,
164 unsigned long size
, unsigned long prot_val
, void *caller
)
166 unsigned long offset
, vaddr
;
167 resource_size_t pfn
, last_pfn
, last_addr
;
168 const resource_size_t unaligned_phys_addr
= phys_addr
;
169 const unsigned long unaligned_size
= size
;
170 struct vm_struct
*area
;
171 unsigned long new_prot_val
;
174 void __iomem
*ret_addr
;
176 /* Don't allow wraparound or zero size */
177 last_addr
= phys_addr
+ size
- 1;
178 if (!size
|| last_addr
< phys_addr
)
181 if (!phys_addr_valid(phys_addr
)) {
182 printk(KERN_WARNING
"ioremap: invalid physical address %llx\n",
183 (unsigned long long)phys_addr
);
189 * Don't remap the low PCI/ISA area, it's always mapped..
191 if (is_ISA_range(phys_addr
, last_addr
))
192 return (__force
void __iomem
*)phys_to_virt(phys_addr
);
195 * Don't allow anybody to remap normal RAM that we're using..
197 last_pfn
= last_addr
>> PAGE_SHIFT
;
198 for (pfn
= phys_addr
>> PAGE_SHIFT
; pfn
<= last_pfn
; pfn
++) {
199 int is_ram
= page_is_ram(pfn
);
201 if (is_ram
&& pfn_valid(pfn
) && !PageReserved(pfn_to_page(pfn
)))
203 WARN_ON_ONCE(is_ram
);
207 * Mappings have to be page-aligned
209 offset
= phys_addr
& ~PAGE_MASK
;
210 phys_addr
&= PHYSICAL_PAGE_MASK
;
211 size
= PAGE_ALIGN(last_addr
+1) - phys_addr
;
213 retval
= reserve_memtype(phys_addr
, (u64
)phys_addr
+ size
,
214 prot_val
, &new_prot_val
);
216 printk(KERN_ERR
"ioremap reserve_memtype failed %d\n", retval
);
220 if (prot_val
!= new_prot_val
) {
221 if (!is_new_memtype_allowed(phys_addr
, size
,
222 prot_val
, new_prot_val
)) {
224 "ioremap error for 0x%llx-0x%llx, requested 0x%lx, got 0x%lx\n",
225 (unsigned long long)phys_addr
,
226 (unsigned long long)(phys_addr
+ size
),
227 prot_val
, new_prot_val
);
228 goto err_free_memtype
;
230 prot_val
= new_prot_val
;
236 prot
= PAGE_KERNEL_IO_NOCACHE
;
238 case _PAGE_CACHE_UC_MINUS
:
239 prot
= PAGE_KERNEL_IO_UC_MINUS
;
242 prot
= PAGE_KERNEL_IO_WC
;
245 prot
= PAGE_KERNEL_IO
;
252 area
= get_vm_area_caller(size
, VM_IOREMAP
, caller
);
254 goto err_free_memtype
;
255 area
->phys_addr
= phys_addr
;
256 vaddr
= (unsigned long) area
->addr
;
258 if (kernel_map_sync_memtype(phys_addr
, size
, prot_val
))
261 if (ioremap_page_range(vaddr
, vaddr
+ size
, phys_addr
, prot
))
264 ret_addr
= (void __iomem
*) (vaddr
+ offset
);
265 mmiotrace_ioremap(unaligned_phys_addr
, unaligned_size
, ret_addr
);
267 if (insert_identity_mapping(phys_addr
, vaddr
, size
))
268 printk(KERN_WARNING
"ioremap: unable to map 0x%llx in identity pagetable\n",
269 (unsigned long long)phys_addr
);
272 * Check if the request spans more than any BAR in the iomem resource
275 WARN_ONCE(iomem_map_sanity_check(unaligned_phys_addr
, unaligned_size
),
276 KERN_INFO
"Info: mapping multiple BARs. Your kernel is fine.");
282 free_memtype(phys_addr
, phys_addr
+ size
);
287 * ioremap_nocache - map bus memory into CPU space
288 * @phys_addr: bus address of the memory
289 * @size: size of the resource to map
291 * ioremap_nocache performs a platform specific sequence of operations to
292 * make bus memory CPU accessible via the readb/readw/readl/writeb/
293 * writew/writel functions and the other mmio helpers. The returned
294 * address is not guaranteed to be usable directly as a virtual
297 * This version of ioremap ensures that the memory is marked uncachable
298 * on the CPU as well as honouring existing caching rules from things like
299 * the PCI bus. Note that there are other caches and buffers on many
300 * busses. In particular driver authors should read up on PCI writes
302 * It's useful if some control registers are in such an area and
303 * write combining or read caching is not desirable:
305 * Must be freed with iounmap.
307 void __iomem
*ioremap_nocache(resource_size_t phys_addr
, unsigned long size
)
310 * Ideally, this should be:
311 * pat_enabled ? _PAGE_CACHE_UC : _PAGE_CACHE_UC_MINUS;
313 * Till we fix all X drivers to use ioremap_wc(), we will use
316 unsigned long val
= _PAGE_CACHE_UC_MINUS
;
318 return __ioremap_caller(phys_addr
, size
, val
,
319 __builtin_return_address(0));
321 EXPORT_SYMBOL(ioremap_nocache
);
324 * ioremap_wc - map memory into CPU space write combined
325 * @phys_addr: bus address of the memory
326 * @size: size of the resource to map
328 * This version of ioremap ensures that the memory is marked write combining.
329 * Write combining allows faster writes to some hardware devices.
331 * Must be freed with iounmap.
333 void __iomem
*ioremap_wc(resource_size_t phys_addr
, unsigned long size
)
336 return __ioremap_caller(phys_addr
, size
, _PAGE_CACHE_WC
,
337 __builtin_return_address(0));
339 return ioremap_nocache(phys_addr
, size
);
341 EXPORT_SYMBOL(ioremap_wc
);
343 void __iomem
*ioremap_cache(resource_size_t phys_addr
, unsigned long size
)
345 return __ioremap_caller(phys_addr
, size
, _PAGE_CACHE_WB
,
346 __builtin_return_address(0));
348 EXPORT_SYMBOL(ioremap_cache
);
350 void __iomem
*ioremap_prot(resource_size_t phys_addr
, unsigned long size
,
351 unsigned long prot_val
)
353 return __ioremap_caller(phys_addr
, size
, (prot_val
& _PAGE_CACHE_MASK
),
354 __builtin_return_address(0));
356 EXPORT_SYMBOL(ioremap_prot
);
359 * iounmap - Free a IO remapping
360 * @addr: virtual address from ioremap_*
362 * Caller must ensure there is only one unmapping for the same pointer.
364 void iounmap(volatile void __iomem
*addr
)
366 struct vm_struct
*p
, *o
;
368 if ((void __force
*)addr
<= high_memory
)
372 * __ioremap special-cases the PCI/ISA range by not instantiating a
373 * vm_area and by simply returning an address into the kernel mapping
374 * of ISA space. So handle that here.
376 if ((void __force
*)addr
>= phys_to_virt(ISA_START_ADDRESS
) &&
377 (void __force
*)addr
< phys_to_virt(ISA_END_ADDRESS
))
380 addr
= (volatile void __iomem
*)
381 (PAGE_MASK
& (unsigned long __force
)addr
);
383 mmiotrace_iounmap(addr
);
385 /* Use the vm area unlocked, assuming the caller
386 ensures there isn't another iounmap for the same address
387 in parallel. Reuse of the virtual address is prevented by
388 leaving it in the global lists until we're done with it.
389 cpa takes care of the direct mappings. */
390 read_lock(&vmlist_lock
);
391 for (p
= vmlist
; p
; p
= p
->next
) {
392 if (p
->addr
== (void __force
*)addr
)
395 read_unlock(&vmlist_lock
);
398 printk(KERN_ERR
"iounmap: bad address %p\n", addr
);
403 free_memtype(p
->phys_addr
, p
->phys_addr
+ get_vm_area_size(p
));
405 /* Finally remove it */
406 o
= remove_vm_area((void __force
*)addr
);
407 BUG_ON(p
!= o
|| o
== NULL
);
410 EXPORT_SYMBOL(iounmap
);
413 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
416 void *xlate_dev_mem_ptr(unsigned long phys
)
419 unsigned long start
= phys
& PAGE_MASK
;
421 /* If page is RAM, we can use __va. Otherwise ioremap and unmap. */
422 if (page_is_ram(start
>> PAGE_SHIFT
))
425 addr
= (void __force
*)ioremap_cache(start
, PAGE_SIZE
);
427 addr
= (void *)((unsigned long)addr
| (phys
& ~PAGE_MASK
));
432 void unxlate_dev_mem_ptr(unsigned long phys
, void *addr
)
434 if (page_is_ram(phys
>> PAGE_SHIFT
))
437 iounmap((void __iomem
*)((unsigned long)addr
& PAGE_MASK
));
441 static int __initdata early_ioremap_debug
;
443 static int __init
early_ioremap_debug_setup(char *str
)
445 early_ioremap_debug
= 1;
449 early_param("early_ioremap_debug", early_ioremap_debug_setup
);
451 static __initdata
int after_paging_init
;
452 static pte_t bm_pte
[PAGE_SIZE
/sizeof(pte_t
)] __page_aligned_bss
;
454 static inline pmd_t
* __init
early_ioremap_pmd(unsigned long addr
)
456 /* Don't assume we're using swapper_pg_dir at this point */
457 pgd_t
*base
= __va(read_cr3());
458 pgd_t
*pgd
= &base
[pgd_index(addr
)];
459 pud_t
*pud
= pud_offset(pgd
, addr
);
460 pmd_t
*pmd
= pmd_offset(pud
, addr
);
465 static inline pte_t
* __init
early_ioremap_pte(unsigned long addr
)
467 return &bm_pte
[pte_index(addr
)];
470 bool __init
is_early_ioremap_ptep(pte_t
*ptep
)
472 return ptep
>= &bm_pte
[0] && ptep
< &bm_pte
[PAGE_SIZE
/sizeof(pte_t
)];
475 static unsigned long slot_virt
[FIX_BTMAPS_SLOTS
] __initdata
;
477 void __init
early_ioremap_init(void)
482 if (early_ioremap_debug
)
483 printk(KERN_INFO
"early_ioremap_init()\n");
485 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++)
486 slot_virt
[i
] = __fix_to_virt(FIX_BTMAP_BEGIN
- NR_FIX_BTMAPS
*i
);
488 pmd
= early_ioremap_pmd(fix_to_virt(FIX_BTMAP_BEGIN
));
489 memset(bm_pte
, 0, sizeof(bm_pte
));
490 pmd_populate_kernel(&init_mm
, pmd
, bm_pte
);
493 * The boot-ioremap range spans multiple pmds, for which
494 * we are not prepared:
496 #define __FIXADDR_TOP (-PAGE_SIZE)
497 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN
) >> PMD_SHIFT
)
498 != (__fix_to_virt(FIX_BTMAP_END
) >> PMD_SHIFT
));
500 if (pmd
!= early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END
))) {
502 printk(KERN_WARNING
"pmd %p != %p\n",
503 pmd
, early_ioremap_pmd(fix_to_virt(FIX_BTMAP_END
)));
504 printk(KERN_WARNING
"fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
505 fix_to_virt(FIX_BTMAP_BEGIN
));
506 printk(KERN_WARNING
"fix_to_virt(FIX_BTMAP_END): %08lx\n",
507 fix_to_virt(FIX_BTMAP_END
));
509 printk(KERN_WARNING
"FIX_BTMAP_END: %d\n", FIX_BTMAP_END
);
510 printk(KERN_WARNING
"FIX_BTMAP_BEGIN: %d\n",
515 void __init
early_ioremap_reset(void)
517 after_paging_init
= 1;
520 static void __init
__early_set_fixmap(enum fixed_addresses idx
,
521 phys_addr_t phys
, pgprot_t flags
)
523 unsigned long addr
= __fix_to_virt(idx
);
526 if (idx
>= __end_of_fixed_addresses
) {
530 pte
= early_ioremap_pte(addr
);
532 if (pgprot_val(flags
))
533 set_pte(pte
, pfn_pte(phys
>> PAGE_SHIFT
, flags
));
535 pte_clear(&init_mm
, addr
, pte
);
536 __flush_tlb_one(addr
);
539 static inline void __init
early_set_fixmap(enum fixed_addresses idx
,
540 phys_addr_t phys
, pgprot_t prot
)
542 if (after_paging_init
)
543 __set_fixmap(idx
, phys
, prot
);
545 __early_set_fixmap(idx
, phys
, prot
);
548 static inline void __init
early_clear_fixmap(enum fixed_addresses idx
)
550 if (after_paging_init
)
553 __early_set_fixmap(idx
, 0, __pgprot(0));
556 static void __iomem
*prev_map
[FIX_BTMAPS_SLOTS
] __initdata
;
557 static unsigned long prev_size
[FIX_BTMAPS_SLOTS
] __initdata
;
559 void __init
fixup_early_ioremap(void)
563 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++) {
570 early_ioremap_init();
573 static int __init
check_early_ioremap_leak(void)
578 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++)
585 "Debug warning: early ioremap leak of %d areas detected.\n",
588 "please boot with early_ioremap_debug and report the dmesg.\n");
592 late_initcall(check_early_ioremap_leak
);
594 static void __init __iomem
*
595 __early_ioremap(resource_size_t phys_addr
, unsigned long size
, pgprot_t prot
)
597 unsigned long offset
;
598 resource_size_t last_addr
;
599 unsigned int nrpages
;
600 enum fixed_addresses idx0
, idx
;
603 WARN_ON(system_state
!= SYSTEM_BOOTING
);
606 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++) {
614 printk(KERN_INFO
"early_iomap(%08llx, %08lx) not found slot\n",
615 (u64
)phys_addr
, size
);
620 if (early_ioremap_debug
) {
621 printk(KERN_INFO
"early_ioremap(%08llx, %08lx) [%d] => ",
622 (u64
)phys_addr
, size
, slot
);
626 /* Don't allow wraparound or zero size */
627 last_addr
= phys_addr
+ size
- 1;
628 if (!size
|| last_addr
< phys_addr
) {
633 prev_size
[slot
] = size
;
635 * Mappings have to be page-aligned
637 offset
= phys_addr
& ~PAGE_MASK
;
638 phys_addr
&= PAGE_MASK
;
639 size
= PAGE_ALIGN(last_addr
+ 1) - phys_addr
;
642 * Mappings have to fit in the FIX_BTMAP area.
644 nrpages
= size
>> PAGE_SHIFT
;
645 if (nrpages
> NR_FIX_BTMAPS
) {
653 idx0
= FIX_BTMAP_BEGIN
- NR_FIX_BTMAPS
*slot
;
655 while (nrpages
> 0) {
656 early_set_fixmap(idx
, phys_addr
, prot
);
657 phys_addr
+= PAGE_SIZE
;
661 if (early_ioremap_debug
)
662 printk(KERN_CONT
"%08lx + %08lx\n", offset
, slot_virt
[slot
]);
664 prev_map
[slot
] = (void __iomem
*)(offset
+ slot_virt
[slot
]);
665 return prev_map
[slot
];
668 /* Remap an IO device */
669 void __init __iomem
*
670 early_ioremap(resource_size_t phys_addr
, unsigned long size
)
672 return __early_ioremap(phys_addr
, size
, PAGE_KERNEL_IO
);
676 void __init __iomem
*
677 early_memremap(resource_size_t phys_addr
, unsigned long size
)
679 return __early_ioremap(phys_addr
, size
, PAGE_KERNEL
);
682 void __init
early_iounmap(void __iomem
*addr
, unsigned long size
)
684 unsigned long virt_addr
;
685 unsigned long offset
;
686 unsigned int nrpages
;
687 enum fixed_addresses idx
;
691 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++) {
692 if (prev_map
[i
] == addr
) {
699 printk(KERN_INFO
"early_iounmap(%p, %08lx) not found slot\n",
705 if (prev_size
[slot
] != size
) {
706 printk(KERN_INFO
"early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
707 addr
, size
, slot
, prev_size
[slot
]);
712 if (early_ioremap_debug
) {
713 printk(KERN_INFO
"early_iounmap(%p, %08lx) [%d]\n", addr
,
718 virt_addr
= (unsigned long)addr
;
719 if (virt_addr
< fix_to_virt(FIX_BTMAP_BEGIN
)) {
723 offset
= virt_addr
& ~PAGE_MASK
;
724 nrpages
= PAGE_ALIGN(offset
+ size
) >> PAGE_SHIFT
;
726 idx
= FIX_BTMAP_BEGIN
- NR_FIX_BTMAPS
*slot
;
727 while (nrpages
> 0) {
728 early_clear_fixmap(idx
);
732 prev_map
[slot
] = NULL
;