blob: 74984c23a87e0a55790d8c5efd329f7966df44b8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mark Salter9e5c33d2014-04-07 15:39:48 -07002/*
3 * Provide common bits of early_ioremap() support for architectures needing
4 * temporary mappings during boot before ioremap() is available.
5 *
6 * This is mostly a direct copy of the x86 early_ioremap implementation.
7 *
8 * (C) Copyright 1995 1996, 2014 Linus Torvalds
9 *
10 */
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/mm.h>
17#include <linux/vmalloc.h>
18#include <asm/fixmap.h>
Ard Biesheuvel4f1af602015-09-11 13:07:50 -070019#include <asm/early_ioremap.h>
Mark Salter9e5c33d2014-04-07 15:39:48 -070020
21#ifdef CONFIG_MMU
22static int early_ioremap_debug __initdata;
23
24static int __init early_ioremap_debug_setup(char *str)
25{
26 early_ioremap_debug = 1;
27
28 return 0;
29}
30early_param("early_ioremap_debug", early_ioremap_debug_setup);
31
32static int after_paging_init __initdata;
33
Tom Lendacky8f716c92017-07-17 16:10:16 -050034pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
35 unsigned long size,
36 pgprot_t prot)
37{
38 return prot;
39}
40
Mark Salter9e5c33d2014-04-07 15:39:48 -070041void __init early_ioremap_reset(void)
42{
Mark Salter9e5c33d2014-04-07 15:39:48 -070043 after_paging_init = 1;
44}
45
46/*
47 * Generally, ioremap() is available after paging_init() has been called.
48 * Architectures wanting to allow early_ioremap after paging_init() can
49 * define __late_set_fixmap and __late_clear_fixmap to do the right thing.
50 */
51#ifndef __late_set_fixmap
52static inline void __init __late_set_fixmap(enum fixed_addresses idx,
53 phys_addr_t phys, pgprot_t prot)
54{
55 BUG();
56}
57#endif
58
59#ifndef __late_clear_fixmap
60static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
61{
62 BUG();
63}
64#endif
65
66static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
67static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
68static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
69
70void __init early_ioremap_setup(void)
71{
72 int i;
73
74 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
75 if (WARN_ON(prev_map[i]))
76 break;
77
78 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
79 slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
80}
81
82static int __init check_early_ioremap_leak(void)
83{
84 int count = 0;
85 int i;
86
87 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
88 if (prev_map[i])
89 count++;
90
91 if (WARN(count, KERN_WARNING
92 "Debug warning: early ioremap leak of %d areas detected.\n"
93 "please boot with early_ioremap_debug and report the dmesg.\n",
94 count))
95 return 1;
96 return 0;
97}
98late_initcall(check_early_ioremap_leak);
99
100static void __init __iomem *
101__early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
102{
103 unsigned long offset;
104 resource_size_t last_addr;
105 unsigned int nrpages;
106 enum fixed_addresses idx;
107 int i, slot;
108
Dave Young7f6f60a2017-12-09 12:16:10 +0800109 WARN_ON(system_state >= SYSTEM_RUNNING);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700110
111 slot = -1;
112 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
113 if (!prev_map[i]) {
114 slot = i;
115 break;
116 }
117 }
118
Andy Shevchenko7b69d792020-01-30 22:13:54 -0800119 if (WARN(slot < 0, "%s(%pa, %08lx) not found slot\n",
120 __func__, &phys_addr, size))
Mark Salter9e5c33d2014-04-07 15:39:48 -0700121 return NULL;
122
123 /* Don't allow wraparound or zero size */
124 last_addr = phys_addr + size - 1;
125 if (WARN_ON(!size || last_addr < phys_addr))
126 return NULL;
127
128 prev_size[slot] = size;
129 /*
130 * Mappings have to be page-aligned
131 */
Alexander Kuleshov5d57b012015-11-05 18:46:40 -0800132 offset = offset_in_page(phys_addr);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700133 phys_addr &= PAGE_MASK;
134 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
135
136 /*
137 * Mappings have to fit in the FIX_BTMAP area.
138 */
139 nrpages = size >> PAGE_SHIFT;
140 if (WARN_ON(nrpages > NR_FIX_BTMAPS))
141 return NULL;
142
143 /*
144 * Ok, go for it..
145 */
146 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
147 while (nrpages > 0) {
148 if (after_paging_init)
149 __late_set_fixmap(idx, phys_addr, prot);
150 else
151 __early_set_fixmap(idx, phys_addr, prot);
152 phys_addr += PAGE_SIZE;
153 --idx;
154 --nrpages;
155 }
Andy Shevchenko7b69d792020-01-30 22:13:54 -0800156 WARN(early_ioremap_debug, "%s(%pa, %08lx) [%d] => %08lx + %08lx\n",
157 __func__, &phys_addr, size, slot, offset, slot_virt[slot]);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700158
159 prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
160 return prev_map[slot];
161}
162
163void __init early_iounmap(void __iomem *addr, unsigned long size)
164{
165 unsigned long virt_addr;
166 unsigned long offset;
167 unsigned int nrpages;
168 enum fixed_addresses idx;
169 int i, slot;
170
171 slot = -1;
172 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
173 if (prev_map[i] == addr) {
174 slot = i;
175 break;
176 }
177 }
178
Stephen Zhang87005392021-02-25 17:18:48 -0800179 if (WARN(slot < 0, "%s(%p, %08lx) not found slot\n",
180 __func__, addr, size))
Mark Salter9e5c33d2014-04-07 15:39:48 -0700181 return;
182
183 if (WARN(prev_size[slot] != size,
Stephen Zhang87005392021-02-25 17:18:48 -0800184 "%s(%p, %08lx) [%d] size not consistent %08lx\n",
185 __func__, addr, size, slot, prev_size[slot]))
Mark Salter9e5c33d2014-04-07 15:39:48 -0700186 return;
187
Stephen Zhang87005392021-02-25 17:18:48 -0800188 WARN(early_ioremap_debug, "%s(%p, %08lx) [%d]\n",
189 __func__, addr, size, slot);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700190
191 virt_addr = (unsigned long)addr;
192 if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
193 return;
194
Alexander Kuleshov5d57b012015-11-05 18:46:40 -0800195 offset = offset_in_page(virt_addr);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700196 nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
197
198 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
199 while (nrpages > 0) {
200 if (after_paging_init)
201 __late_clear_fixmap(idx);
202 else
203 __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
204 --idx;
205 --nrpages;
206 }
207 prev_map[slot] = NULL;
208}
209
210/* Remap an IO device */
211void __init __iomem *
212early_ioremap(resource_size_t phys_addr, unsigned long size)
213{
214 return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
215}
216
217/* Remap memory */
218void __init *
219early_memremap(resource_size_t phys_addr, unsigned long size)
220{
Tom Lendacky8f716c92017-07-17 16:10:16 -0500221 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
222 FIXMAP_PAGE_NORMAL);
223
224 return (__force void *)__early_ioremap(phys_addr, size, prot);
Mark Salter9e5c33d2014-04-07 15:39:48 -0700225}
Juergen Gross2592dbb2015-07-17 06:51:33 +0200226#ifdef FIXMAP_PAGE_RO
227void __init *
228early_memremap_ro(resource_size_t phys_addr, unsigned long size)
229{
Tom Lendacky8f716c92017-07-17 16:10:16 -0500230 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
231 FIXMAP_PAGE_RO);
232
233 return (__force void *)__early_ioremap(phys_addr, size, prot);
Juergen Gross2592dbb2015-07-17 06:51:33 +0200234}
235#endif
Mark Salter6b0f68e2015-09-08 15:03:01 -0700236
Tom Lendackyf88a68f2017-07-17 16:10:09 -0500237#ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
238void __init *
239early_memremap_prot(resource_size_t phys_addr, unsigned long size,
240 unsigned long prot_val)
241{
242 return (__force void *)__early_ioremap(phys_addr, size,
243 __pgprot(prot_val));
244}
245#endif
246
Mark Salter6b0f68e2015-09-08 15:03:01 -0700247#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
248
249void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
250{
251 unsigned long slop, clen;
252 char *p;
253
254 while (size) {
Alexander Kuleshov5d57b012015-11-05 18:46:40 -0800255 slop = offset_in_page(src);
Mark Salter6b0f68e2015-09-08 15:03:01 -0700256 clen = size;
257 if (clen > MAX_MAP_CHUNK - slop)
258 clen = MAX_MAP_CHUNK - slop;
259 p = early_memremap(src & PAGE_MASK, clen + slop);
260 memcpy(dest, p + slop, clen);
261 early_memunmap(p, clen + slop);
262 dest += clen;
263 src += clen;
264 size -= clen;
265 }
266}
267
Mark Salter9e5c33d2014-04-07 15:39:48 -0700268#else /* CONFIG_MMU */
269
270void __init __iomem *
271early_ioremap(resource_size_t phys_addr, unsigned long size)
272{
273 return (__force void __iomem *)phys_addr;
274}
275
276/* Remap memory */
277void __init *
278early_memremap(resource_size_t phys_addr, unsigned long size)
279{
280 return (void *)phys_addr;
281}
Juergen Gross2592dbb2015-07-17 06:51:33 +0200282void __init *
283early_memremap_ro(resource_size_t phys_addr, unsigned long size)
284{
285 return (void *)phys_addr;
286}
Mark Salter9e5c33d2014-04-07 15:39:48 -0700287
288void __init early_iounmap(void __iomem *addr, unsigned long size)
289{
290}
291
292#endif /* CONFIG_MMU */
293
294
295void __init early_memunmap(void *addr, unsigned long size)
296{
297 early_iounmap((__force void __iomem *)addr, size);
298}