1 // SPDX-License-Identifier: GPL-2.0
3 * Re-map IO memory to kernel address space so that we can access it.
4 * Needed for memory-mapped I/O devices mapped outside our normal DRAM
5 * window (that is, all memory-mapped I/O devices).
7 * Copyright (C) 1995,1996 Linus Torvalds
9 * Meta port based on CRIS-port by Axis Communications AB
12 #include <linux/vmalloc.h>
14 #include <linux/export.h>
15 #include <linux/slab.h>
18 #include <asm/pgtable.h>
21 * Remap an arbitrary physical address space into the kernel virtual
22 * address space. Needed when the kernel wants to access high addresses
25 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
26 * have to convert them into an offset in a page-aligned mapping, but the
27 * caller shouldn't need to know that small detail.
29 void __iomem *__ioremap(unsigned long phys_addr, size_t size,
33 struct vm_struct *area;
34 unsigned long offset, last_addr;
37 /* Don't allow wraparound or zero size */
38 last_addr = phys_addr + size - 1;
39 if (!size || last_addr < phys_addr)
42 /* Custom region addresses are accessible and uncached by default. */
43 if (phys_addr >= LINSYSCUSTOM_BASE &&
44 phys_addr < (LINSYSCUSTOM_BASE + LINSYSCUSTOM_LIMIT))
45 return (__force void __iomem *) phys_addr;
48 * Mappings have to be page-aligned
50 offset = phys_addr & ~PAGE_MASK;
51 phys_addr &= PAGE_MASK;
52 size = PAGE_ALIGN(last_addr+1) - phys_addr;
53 prot = __pgprot(_PAGE_PRESENT | _PAGE_WRITE | _PAGE_DIRTY |
54 _PAGE_ACCESSED | _PAGE_KERNEL | _PAGE_CACHE_WIN0 |
60 area = get_vm_area(size, VM_IOREMAP);
63 area->phys_addr = phys_addr;
64 addr = (unsigned long) area->addr;
65 if (ioremap_page_range(addr, addr + size, phys_addr, prot)) {
66 vunmap((void *) addr);
69 return (__force void __iomem *) (offset + (char *)addr);
71 EXPORT_SYMBOL(__ioremap);
73 void __iounmap(void __iomem *addr)
77 if ((__force unsigned long)addr >= LINSYSCUSTOM_BASE &&
78 (__force unsigned long)addr < (LINSYSCUSTOM_BASE +
82 p = remove_vm_area((void *)(PAGE_MASK & (unsigned long __force)addr));
84 pr_err("iounmap: bad address %p\n", addr);
90 EXPORT_SYMBOL(__iounmap);