1 // SPDX-License-Identifier: GPL-2.0
3 * Provide common bits of early_ioremap() support for architectures needing
4 * temporary mappings during boot before ioremap() is available.
6 * This is mostly a direct copy of the x86 early_ioremap implementation.
8 * (C) Copyright 1995 1996, 2014 Linus Torvalds
11 #include <linux/kernel.h>
12 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18 #include <asm/fixmap.h>
19 #include <asm/early_ioremap.h>
23 static int early_ioremap_debug __initdata;
25 static int __init early_ioremap_debug_setup(char *str)
27 early_ioremap_debug = 1;
31 early_param("early_ioremap_debug", early_ioremap_debug_setup);
33 static int after_paging_init __initdata;
35 pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
42 void __init early_ioremap_reset(void)
44 after_paging_init = 1;
48 * Generally, ioremap() is available after paging_init() has been called.
49 * Architectures wanting to allow early_ioremap after paging_init() can
50 * define __late_set_fixmap and __late_clear_fixmap to do the right thing.
52 #ifndef __late_set_fixmap
53 static inline void __init __late_set_fixmap(enum fixed_addresses idx,
54 phys_addr_t phys, pgprot_t prot)
60 #ifndef __late_clear_fixmap
61 static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
67 static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
68 static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
69 static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
71 void __init early_ioremap_setup(void)
75 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
76 if (WARN_ON(prev_map[i]))
79 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
80 slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
83 static int __init check_early_ioremap_leak(void)
88 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
92 if (WARN(count, KERN_WARNING
93 "Debug warning: early ioremap leak of %d areas detected.\n"
94 "please boot with early_ioremap_debug and report the dmesg.\n",
99 late_initcall(check_early_ioremap_leak);
101 static void __init __iomem *
102 __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
104 unsigned long offset;
105 resource_size_t last_addr;
106 unsigned int nrpages;
107 enum fixed_addresses idx;
110 WARN_ON(system_state >= SYSTEM_RUNNING);
113 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
120 if (WARN(slot < 0, "%s(%pa, %08lx) not found slot\n",
121 __func__, &phys_addr, size))
124 /* Don't allow wraparound or zero size */
125 last_addr = phys_addr + size - 1;
126 if (WARN_ON(!size || last_addr < phys_addr))
129 prev_size[slot] = size;
131 * Mappings have to be page-aligned
133 offset = offset_in_page(phys_addr);
134 phys_addr &= PAGE_MASK;
135 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
138 * Mappings have to fit in the FIX_BTMAP area.
140 nrpages = size >> PAGE_SHIFT;
141 if (WARN_ON(nrpages > NR_FIX_BTMAPS))
147 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
148 while (nrpages > 0) {
149 if (after_paging_init)
150 __late_set_fixmap(idx, phys_addr, prot);
152 __early_set_fixmap(idx, phys_addr, prot);
153 phys_addr += PAGE_SIZE;
157 WARN(early_ioremap_debug, "%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
158 __func__, &phys_addr, size, slot, offset, slot_virt[slot]);
160 prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
161 return prev_map[slot];
164 void __init early_iounmap(void __iomem *addr, unsigned long size)
166 unsigned long virt_addr;
167 unsigned long offset;
168 unsigned int nrpages;
169 enum fixed_addresses idx;
173 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
174 if (prev_map[i] == addr) {
180 if (WARN(slot < 0, "%s(%p, %08lx) not found slot\n",
181 __func__, addr, size))
184 if (WARN(prev_size[slot] != size,
185 "%s(%p, %08lx) [%d] size not consistent %08lx\n",
186 __func__, addr, size, slot, prev_size[slot]))
189 WARN(early_ioremap_debug, "%s(%p, %08lx) [%d]\n",
190 __func__, addr, size, slot);
192 virt_addr = (unsigned long)addr;
193 if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
196 offset = offset_in_page(virt_addr);
197 nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
199 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
200 while (nrpages > 0) {
201 if (after_paging_init)
202 __late_clear_fixmap(idx);
204 __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
208 prev_map[slot] = NULL;
211 /* Remap an IO device */
212 void __init __iomem *
213 early_ioremap(resource_size_t phys_addr, unsigned long size)
215 return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
220 early_memremap(resource_size_t phys_addr, unsigned long size)
222 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
225 return (__force void *)__early_ioremap(phys_addr, size, prot);
227 #ifdef FIXMAP_PAGE_RO
229 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
231 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
234 return (__force void *)__early_ioremap(phys_addr, size, prot);
238 #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
240 early_memremap_prot(resource_size_t phys_addr, unsigned long size,
241 unsigned long prot_val)
243 return (__force void *)__early_ioremap(phys_addr, size,
248 #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
250 void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
252 unsigned long slop, clen;
256 slop = offset_in_page(src);
258 if (clen > MAX_MAP_CHUNK - slop)
259 clen = MAX_MAP_CHUNK - slop;
260 p = early_memremap(src & PAGE_MASK, clen + slop);
261 memcpy(dest, p + slop, clen);
262 early_memunmap(p, clen + slop);
269 #else /* CONFIG_MMU */
271 void __init __iomem *
272 early_ioremap(resource_size_t phys_addr, unsigned long size)
274 return (__force void __iomem *)phys_addr;
279 early_memremap(resource_size_t phys_addr, unsigned long size)
281 return (void *)phys_addr;
284 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
286 return (void *)phys_addr;
289 void __init early_iounmap(void __iomem *addr, unsigned long size)
293 #endif /* CONFIG_MMU */
296 void __init early_memunmap(void *addr, unsigned long size)
298 early_iounmap((__force void __iomem *)addr, size);