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>
14 void __iomem *generic_ioremap_prot(phys_addr_t phys_addr, size_t size,
17 unsigned long offset, vaddr;
18 phys_addr_t last_addr;
19 struct vm_struct *area;
21 /* An early platform driver might end up here */
22 if (WARN_ON_ONCE(!slab_is_available()))
25 /* Disallow wrap-around or zero size */
26 last_addr = phys_addr + size - 1;
27 if (!size || last_addr < phys_addr)
30 /* Page-align mappings */
31 offset = phys_addr & (~PAGE_MASK);
33 size = PAGE_ALIGN(size + offset);
35 if (!ioremap_allowed(phys_addr, size, pgprot_val(prot)))
38 area = get_vm_area_caller(size, VM_IOREMAP,
39 __builtin_return_address(0));
42 vaddr = (unsigned long)area->addr;
43 area->phys_addr = phys_addr;
45 if (ioremap_page_range(vaddr, vaddr + size, phys_addr, prot)) {
50 return (void __iomem *)(vaddr + offset);
54 void __iomem *ioremap_prot(phys_addr_t phys_addr, size_t size,
57 return generic_ioremap_prot(phys_addr, size, __pgprot(prot));
59 EXPORT_SYMBOL(ioremap_prot);
62 void generic_iounmap(volatile void __iomem *addr)
64 void *vaddr = (void *)((unsigned long)addr & PAGE_MASK);
66 if (!iounmap_allowed(vaddr))
69 if (is_vmalloc_addr(vaddr))
74 void iounmap(volatile void __iomem *addr)
76 generic_iounmap(addr);
78 EXPORT_SYMBOL(iounmap);