1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) Copyright 1995 1996 Linus Torvalds
4 * (C) Copyright 2012 Regents of the University of California
7 #include <linux/export.h>
9 #include <linux/vmalloc.h>
12 #include <asm/pgtable.h>
15 * Remap an arbitrary physical address space into the kernel virtual
16 * address space. Needed when the kernel wants to access high addresses
19 * NOTE! We need to allow non-page-aligned mappings too: we will obviously
20 * have to convert them into an offset in a page-aligned mapping, but the
21 * caller shouldn't need to know that small detail.
23 static void __iomem *__ioremap_caller(phys_addr_t addr, size_t size,
24 pgprot_t prot, void *caller)
26 phys_addr_t last_addr;
27 unsigned long offset, vaddr;
28 struct vm_struct *area;
30 /* Disallow wrap-around or zero size */
31 last_addr = addr + size - 1;
32 if (!size || last_addr < addr)
35 /* Page-align mappings */
36 offset = addr & (~PAGE_MASK);
38 size = PAGE_ALIGN(size + offset);
40 area = get_vm_area_caller(size, VM_IOREMAP, caller);
43 vaddr = (unsigned long)area->addr;
45 if (ioremap_page_range(vaddr, vaddr + size, addr, prot)) {
50 return (void __iomem *)(vaddr + offset);
54 * ioremap - map bus memory into CPU space
55 * @offset: bus address of the memory
56 * @size: size of the resource to map
58 * ioremap performs a platform specific sequence of operations to
59 * make bus memory CPU accessible via the readb/readw/readl/writeb/
60 * writew/writel functions and the other mmio helpers. The returned
61 * address is not guaranteed to be usable directly as a virtual
64 * Must be freed with iounmap.
66 void __iomem *ioremap(phys_addr_t offset, unsigned long size)
68 return __ioremap_caller(offset, size, PAGE_KERNEL,
69 __builtin_return_address(0));
71 EXPORT_SYMBOL(ioremap);
75 * iounmap - Free a IO remapping
76 * @addr: virtual address from ioremap_*
78 * Caller must ensure there is only one unmapping for the same pointer.
80 void iounmap(volatile void __iomem *addr)
82 vunmap((void *)((unsigned long)addr & PAGE_MASK));
84 EXPORT_SYMBOL(iounmap);