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
10 #include <linux/kernel.h>
11 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <asm/fixmap.h>
18 #include <asm/early_ioremap.h>
21 static int early_ioremap_debug __initdata
;
23 static int __init
early_ioremap_debug_setup(char *str
)
25 early_ioremap_debug
= 1;
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
,
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
)
63 #ifndef __late_clear_fixmap
64 static inline void __init
__late_clear_fixmap(enum fixed_addresses idx
)
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)
78 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++)
79 if (WARN_ON(prev_map
[i
]))
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)
91 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++)
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",
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
;
113 WARN_ON(system_state
!= SYSTEM_BOOTING
);
116 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++) {
123 if (WARN(slot
< 0, "%s(%08llx, %08lx) not found slot\n",
124 __func__
, (u64
)phys_addr
, size
))
127 /* Don't allow wraparound or zero size */
128 last_addr
= phys_addr
+ size
- 1;
129 if (WARN_ON(!size
|| last_addr
< phys_addr
))
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
))
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
);
155 __early_set_fixmap(idx
, phys_addr
, prot
);
156 phys_addr
+= PAGE_SIZE
;
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
;
176 for (i
= 0; i
< FIX_BTMAPS_SLOTS
; i
++) {
177 if (prev_map
[i
] == addr
) {
183 if (WARN(slot
< 0, "early_iounmap(%p, %08lx) not found slot\n",
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
]))
192 WARN(early_ioremap_debug
, "early_iounmap(%p, %08lx) [%d]\n",
195 virt_addr
= (unsigned long)addr
;
196 if (WARN_ON(virt_addr
< fix_to_virt(FIX_BTMAP_BEGIN
)))
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
);
207 __early_set_fixmap(idx
, 0, FIXMAP_PAGE_CLEAR
);
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
);
223 early_memremap(resource_size_t phys_addr
, unsigned long size
)
225 pgprot_t prot
= early_memremap_pgprot_adjust(phys_addr
, size
,
228 return (__force
void *)__early_ioremap(phys_addr
, size
, prot
);
230 #ifdef FIXMAP_PAGE_RO
232 early_memremap_ro(resource_size_t phys_addr
, unsigned long size
)
234 pgprot_t prot
= early_memremap_pgprot_adjust(phys_addr
, size
,
237 return (__force
void *)__early_ioremap(phys_addr
, size
, prot
);
241 #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT
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
,
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
;
259 slop
= offset_in_page(src
);
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
);
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
;
282 early_memremap(resource_size_t phys_addr
, unsigned long size
)
284 return (void *)phys_addr
;
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
);