docs-rst: don't require adjustbox anymore
[linux/fpc-iii.git] / mm / early_ioremap.c
blobb1dd4a948fc0b3afc375964d23ebcb9f69eaafa3
1 /*
2 * Provide common bits of early_ioremap() support for architectures needing
3 * temporary mappings during boot before ioremap() is available.
5 * This is mostly a direct copy of the x86 early_ioremap implementation.
7 * (C) Copyright 1995 1996, 2014 Linus Torvalds
9 */
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/io.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/mm.h>
16 #include <linux/vmalloc.h>
17 #include <asm/fixmap.h>
18 #include <asm/early_ioremap.h>
20 #ifdef CONFIG_MMU
21 static int early_ioremap_debug __initdata;
23 static int __init early_ioremap_debug_setup(char *str)
25 early_ioremap_debug = 1;
27 return 0;
29 early_param("early_ioremap_debug", early_ioremap_debug_setup);
31 static int after_paging_init __initdata;
33 pgprot_t __init __weak early_memremap_pgprot_adjust(resource_size_t phys_addr,
34 unsigned long size,
35 pgprot_t prot)
37 return prot;
40 void __init __weak early_ioremap_shutdown(void)
44 void __init early_ioremap_reset(void)
46 early_ioremap_shutdown();
47 after_paging_init = 1;
51 * Generally, ioremap() is available after paging_init() has been called.
52 * Architectures wanting to allow early_ioremap after paging_init() can
53 * define __late_set_fixmap and __late_clear_fixmap to do the right thing.
55 #ifndef __late_set_fixmap
56 static inline void __init __late_set_fixmap(enum fixed_addresses idx,
57 phys_addr_t phys, pgprot_t prot)
59 BUG();
61 #endif
63 #ifndef __late_clear_fixmap
64 static inline void __init __late_clear_fixmap(enum fixed_addresses idx)
66 BUG();
68 #endif
70 static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata;
71 static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata;
72 static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata;
74 void __init early_ioremap_setup(void)
76 int i;
78 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
79 if (WARN_ON(prev_map[i]))
80 break;
82 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
83 slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i);
86 static int __init check_early_ioremap_leak(void)
88 int count = 0;
89 int i;
91 for (i = 0; i < FIX_BTMAPS_SLOTS; i++)
92 if (prev_map[i])
93 count++;
95 if (WARN(count, KERN_WARNING
96 "Debug warning: early ioremap leak of %d areas detected.\n"
97 "please boot with early_ioremap_debug and report the dmesg.\n",
98 count))
99 return 1;
100 return 0;
102 late_initcall(check_early_ioremap_leak);
104 static void __init __iomem *
105 __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
107 unsigned long offset;
108 resource_size_t last_addr;
109 unsigned int nrpages;
110 enum fixed_addresses idx;
111 int i, slot;
113 WARN_ON(system_state != SYSTEM_BOOTING);
115 slot = -1;
116 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
117 if (!prev_map[i]) {
118 slot = i;
119 break;
123 if (WARN(slot < 0, "%s(%08llx, %08lx) not found slot\n",
124 __func__, (u64)phys_addr, size))
125 return NULL;
127 /* Don't allow wraparound or zero size */
128 last_addr = phys_addr + size - 1;
129 if (WARN_ON(!size || last_addr < phys_addr))
130 return NULL;
132 prev_size[slot] = size;
134 * Mappings have to be page-aligned
136 offset = offset_in_page(phys_addr);
137 phys_addr &= PAGE_MASK;
138 size = PAGE_ALIGN(last_addr + 1) - phys_addr;
141 * Mappings have to fit in the FIX_BTMAP area.
143 nrpages = size >> PAGE_SHIFT;
144 if (WARN_ON(nrpages > NR_FIX_BTMAPS))
145 return NULL;
148 * Ok, go for it..
150 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
151 while (nrpages > 0) {
152 if (after_paging_init)
153 __late_set_fixmap(idx, phys_addr, prot);
154 else
155 __early_set_fixmap(idx, phys_addr, prot);
156 phys_addr += PAGE_SIZE;
157 --idx;
158 --nrpages;
160 WARN(early_ioremap_debug, "%s(%08llx, %08lx) [%d] => %08lx + %08lx\n",
161 __func__, (u64)phys_addr, size, slot, offset, slot_virt[slot]);
163 prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]);
164 return prev_map[slot];
167 void __init early_iounmap(void __iomem *addr, unsigned long size)
169 unsigned long virt_addr;
170 unsigned long offset;
171 unsigned int nrpages;
172 enum fixed_addresses idx;
173 int i, slot;
175 slot = -1;
176 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) {
177 if (prev_map[i] == addr) {
178 slot = i;
179 break;
183 if (WARN(slot < 0, "early_iounmap(%p, %08lx) not found slot\n",
184 addr, size))
185 return;
187 if (WARN(prev_size[slot] != size,
188 "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n",
189 addr, size, slot, prev_size[slot]))
190 return;
192 WARN(early_ioremap_debug, "early_iounmap(%p, %08lx) [%d]\n",
193 addr, size, slot);
195 virt_addr = (unsigned long)addr;
196 if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN)))
197 return;
199 offset = offset_in_page(virt_addr);
200 nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT;
202 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot;
203 while (nrpages > 0) {
204 if (after_paging_init)
205 __late_clear_fixmap(idx);
206 else
207 __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR);
208 --idx;
209 --nrpages;
211 prev_map[slot] = NULL;
214 /* Remap an IO device */
215 void __init __iomem *
216 early_ioremap(resource_size_t phys_addr, unsigned long size)
218 return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO);
221 /* Remap memory */
222 void __init *
223 early_memremap(resource_size_t phys_addr, unsigned long size)
225 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
226 FIXMAP_PAGE_NORMAL);
228 return (__force void *)__early_ioremap(phys_addr, size, prot);
230 #ifdef FIXMAP_PAGE_RO
231 void __init *
232 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
234 pgprot_t prot = early_memremap_pgprot_adjust(phys_addr, size,
235 FIXMAP_PAGE_RO);
237 return (__force void *)__early_ioremap(phys_addr, size, prot);
239 #endif
241 #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
242 void __init *
243 early_memremap_prot(resource_size_t phys_addr, unsigned long size,
244 unsigned long prot_val)
246 return (__force void *)__early_ioremap(phys_addr, size,
247 __pgprot(prot_val));
249 #endif
251 #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
253 void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size)
255 unsigned long slop, clen;
256 char *p;
258 while (size) {
259 slop = offset_in_page(src);
260 clen = size;
261 if (clen > MAX_MAP_CHUNK - slop)
262 clen = MAX_MAP_CHUNK - slop;
263 p = early_memremap(src & PAGE_MASK, clen + slop);
264 memcpy(dest, p + slop, clen);
265 early_memunmap(p, clen + slop);
266 dest += clen;
267 src += clen;
268 size -= clen;
272 #else /* CONFIG_MMU */
274 void __init __iomem *
275 early_ioremap(resource_size_t phys_addr, unsigned long size)
277 return (__force void __iomem *)phys_addr;
280 /* Remap memory */
281 void __init *
282 early_memremap(resource_size_t phys_addr, unsigned long size)
284 return (void *)phys_addr;
286 void __init *
287 early_memremap_ro(resource_size_t phys_addr, unsigned long size)
289 return (void *)phys_addr;
292 void __init early_iounmap(void __iomem *addr, unsigned long size)
296 #endif /* CONFIG_MMU */
299 void __init early_memunmap(void *addr, unsigned long size)
301 early_iounmap((__force void __iomem *)addr, size);