GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / arm64 / mm / ioremap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/mm/ioremap.c
4  *
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
8  * by Russell King
9  * Copyright (C) 2012 ARM Ltd.
10  */
11
12 #include <linux/export.h>
13 #include <linux/mm.h>
14 #include <linux/vmalloc.h>
15 #include <linux/io.h>
16 #include <linux/memblock.h>
17
18 #include <asm/fixmap.h>
19 #include <asm/tlbflush.h>
20 #include <asm/pgalloc.h>
21
22 static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
23                                       pgprot_t prot, void *caller)
24 {
25         unsigned long last_addr;
26         unsigned long offset = phys_addr & ~PAGE_MASK;
27         int err;
28         unsigned long addr;
29         struct vm_struct *area;
30
31         /*
32          * Page align the mapping address and size, taking account of any
33          * offset.
34          */
35         phys_addr &= PAGE_MASK;
36         size = PAGE_ALIGN(size + offset);
37
38         /*
39          * Don't allow wraparound, zero size or outside PHYS_MASK.
40          */
41         last_addr = phys_addr + size - 1;
42         if (!size || last_addr < phys_addr || (last_addr & ~PHYS_MASK))
43                 return NULL;
44
45         /*
46          * Don't allow RAM to be mapped.
47          */
48         if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr))))
49                 return NULL;
50
51         area = get_vm_area_caller(size, VM_IOREMAP, caller);
52         if (!area)
53                 return NULL;
54         addr = (unsigned long)area->addr;
55         area->phys_addr = phys_addr;
56
57         err = ioremap_page_range(addr, addr + size, phys_addr, prot);
58         if (err) {
59                 vunmap((void *)addr);
60                 return NULL;
61         }
62
63         return (void __iomem *)(offset + addr);
64 }
65
66 void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot)
67 {
68         return __ioremap_caller(phys_addr, size, prot,
69                                 __builtin_return_address(0));
70 }
71 EXPORT_SYMBOL(__ioremap);
72
73 void iounmap(volatile void __iomem *io_addr)
74 {
75         unsigned long addr = (unsigned long)io_addr & PAGE_MASK;
76
77         /*
78          * We could get an address outside vmalloc range in case
79          * of ioremap_cache() reusing a RAM mapping.
80          */
81         if (is_vmalloc_addr((void *)addr))
82                 vunmap((void *)addr);
83 }
84 EXPORT_SYMBOL(iounmap);
85
86 void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
87 {
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);
91
92         return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
93                                 __builtin_return_address(0));
94 }
95 EXPORT_SYMBOL(ioremap_cache);
96
97 /*
98  * Must be called after early_fixmap_init
99  */
100 void __init early_ioremap_init(void)
101 {
102         early_ioremap_setup();
103 }
104
105 bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
106                                  unsigned long flags)
107 {
108         unsigned long pfn = PHYS_PFN(offset);
109
110         return memblock_is_map_memory(pfn);
111 }