1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/mm/ioremap.c
5 * (C) Copyright 1995 1996 Linus Torvalds
6 * Hacked for ARM by Phil Blundell <philb@gnu.org>
7 * Hacked to allow all architectures to build, and various cleanups
9 * Copyright (C) 2012 ARM Ltd.
12 #include <linux/export.h>
14 #include <linux/vmalloc.h>
16 #include <linux/memblock.h>
18 #include <asm/fixmap.h>
19 #include <asm/tlbflush.h>
20 #include <asm/pgalloc.h>
22 static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
23 pgprot_t prot, void *caller)
25 unsigned long last_addr;
26 unsigned long offset = phys_addr & ~PAGE_MASK;
29 struct vm_struct *area;
32 * Page align the mapping address and size, taking account of any
35 phys_addr &= PAGE_MASK;
36 size = PAGE_ALIGN(size + offset);
39 * Don't allow wraparound, zero size or outside PHYS_MASK.
41 last_addr = phys_addr + size - 1;
42 if (!size || last_addr < phys_addr || (last_addr & ~PHYS_MASK))
46 * Don't allow RAM to be mapped.
48 if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr))))
51 area = get_vm_area_caller(size, VM_IOREMAP, caller);
54 addr = (unsigned long)area->addr;
55 area->phys_addr = phys_addr;
57 err = ioremap_page_range(addr, addr + size, phys_addr, prot);
63 return (void __iomem *)(offset + addr);
66 void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot)
68 return __ioremap_caller(phys_addr, size, prot,
69 __builtin_return_address(0));
71 EXPORT_SYMBOL(__ioremap);
73 void iounmap(volatile void __iomem *io_addr)
75 unsigned long addr = (unsigned long)io_addr & PAGE_MASK;
78 * We could get an address outside vmalloc range in case
79 * of ioremap_cache() reusing a RAM mapping.
81 if (is_vmalloc_addr((void *)addr))
84 EXPORT_SYMBOL(iounmap);
86 void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
88 /* For normal memory we already have a cacheable mapping. */
89 if (pfn_valid(__phys_to_pfn(phys_addr)))
90 return (void __iomem *)__phys_to_virt(phys_addr);
92 return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
93 __builtin_return_address(0));
95 EXPORT_SYMBOL(ioremap_cache);
98 * Must be called after early_fixmap_init
100 void __init early_ioremap_init(void)
102 early_ioremap_setup();
105 bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
108 unsigned long pfn = PHYS_PFN(offset);
110 return memblock_is_map_memory(pfn);