1 // SPDX-License-Identifier: GPL-2.0
3 * Re-map IO memory to kernel address space so that we can access it.
4 * This is needed for high PCI addresses that aren't mapped in the
5 * 640k-1MB IO memory area on PC's
7 * (C) Copyright 1995 1996 Linus Torvalds
9 #include <linux/vmalloc.h>
12 #include <linux/export.h>
15 * Ioremap often, but not always uses the generic vmalloc area. E.g on
16 * Power ARCH, it could have different ioremap space.
19 #define IOREMAP_START VMALLOC_START
20 #define IOREMAP_END VMALLOC_END
23 void __iomem *generic_ioremap_prot(phys_addr_t phys_addr, size_t size,
26 unsigned long offset, vaddr;
27 phys_addr_t last_addr;
28 struct vm_struct *area;
30 /* An early platform driver might end up here */
31 if (WARN_ON_ONCE(!slab_is_available()))
34 /* Disallow wrap-around or zero size */
35 last_addr = phys_addr + size - 1;
36 if (!size || last_addr < phys_addr)
39 /* Page-align mappings */
40 offset = phys_addr & (~PAGE_MASK);
42 size = PAGE_ALIGN(size + offset);
44 if (!ioremap_allowed(phys_addr, size, pgprot_val(prot)))
47 area = __get_vm_area_caller(size, VM_IOREMAP, IOREMAP_START,
48 IOREMAP_END, __builtin_return_address(0));
51 vaddr = (unsigned long)area->addr;
52 area->phys_addr = phys_addr;
54 if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
59 return (void __iomem *)(vaddr + offset);
63 void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
66 return generic_ioremap_prot(phys_addr, size, __pgprot(prot));
68 EXPORT_SYMBOL(ioremap_prot);
71 void generic_iounmap(volatile void __iomem *addr)
73 void *vaddr = (void *)((unsigned long)addr & PAGE_MASK);
75 if (!iounmap_allowed(vaddr))
78 if (is_ioremap_addr(vaddr))
83 void iounmap(volatile void __iomem *addr)
85 generic_iounmap(addr);
87 EXPORT_SYMBOL(iounmap);