1 // SPDX-License-Identifier: GPL-2.0 << 2 /* 1 /* 3 * Provide common bits of early_ioremap() supp 2 * Provide common bits of early_ioremap() support for architectures needing 4 * temporary mappings during boot before iorem 3 * temporary mappings during boot before ioremap() is available. 5 * 4 * 6 * This is mostly a direct copy of the x86 ear 5 * This is mostly a direct copy of the x86 early_ioremap implementation. 7 * 6 * 8 * (C) Copyright 1995 1996, 2014 Linus Torvald 7 * (C) Copyright 1995 1996, 2014 Linus Torvalds 9 * 8 * 10 */ 9 */ 11 #include <linux/kernel.h> 10 #include <linux/kernel.h> 12 #include <linux/init.h> 11 #include <linux/init.h> 13 #include <linux/io.h> 12 #include <linux/io.h> 14 #include <linux/module.h> 13 #include <linux/module.h> 15 #include <linux/slab.h> 14 #include <linux/slab.h> 16 #include <linux/mm.h> 15 #include <linux/mm.h> 17 #include <linux/vmalloc.h> 16 #include <linux/vmalloc.h> 18 #include <asm/fixmap.h> 17 #include <asm/fixmap.h> 19 #include <asm/early_ioremap.h> 18 #include <asm/early_ioremap.h> 20 #include "internal.h" << 21 19 22 #ifdef CONFIG_MMU 20 #ifdef CONFIG_MMU 23 static int early_ioremap_debug __initdata; 21 static int early_ioremap_debug __initdata; 24 22 25 static int __init early_ioremap_debug_setup(ch 23 static int __init early_ioremap_debug_setup(char *str) 26 { 24 { 27 early_ioremap_debug = 1; 25 early_ioremap_debug = 1; 28 26 29 return 0; 27 return 0; 30 } 28 } 31 early_param("early_ioremap_debug", early_iorem 29 early_param("early_ioremap_debug", early_ioremap_debug_setup); 32 30 33 static int after_paging_init __initdata; 31 static int after_paging_init __initdata; 34 32 35 pgprot_t __init __weak early_memremap_pgprot_a !! 33 void __init __weak early_ioremap_shutdown(void) 36 << 37 << 38 { 34 { 39 return prot; << 40 } 35 } 41 36 42 void __init early_ioremap_reset(void) 37 void __init early_ioremap_reset(void) 43 { 38 { >> 39 early_ioremap_shutdown(); 44 after_paging_init = 1; 40 after_paging_init = 1; 45 } 41 } 46 42 47 /* 43 /* 48 * Generally, ioremap() is available after pag 44 * Generally, ioremap() is available after paging_init() has been called. 49 * Architectures wanting to allow early_iorema 45 * Architectures wanting to allow early_ioremap after paging_init() can 50 * define __late_set_fixmap and __late_clear_f 46 * define __late_set_fixmap and __late_clear_fixmap to do the right thing. 51 */ 47 */ 52 #ifndef __late_set_fixmap 48 #ifndef __late_set_fixmap 53 static inline void __init __late_set_fixmap(en 49 static inline void __init __late_set_fixmap(enum fixed_addresses idx, 54 ph 50 phys_addr_t phys, pgprot_t prot) 55 { 51 { 56 BUG(); 52 BUG(); 57 } 53 } 58 #endif 54 #endif 59 55 60 #ifndef __late_clear_fixmap 56 #ifndef __late_clear_fixmap 61 static inline void __init __late_clear_fixmap( 57 static inline void __init __late_clear_fixmap(enum fixed_addresses idx) 62 { 58 { 63 BUG(); 59 BUG(); 64 } 60 } 65 #endif 61 #endif 66 62 67 static void __iomem *prev_map[FIX_BTMAPS_SLOTS 63 static void __iomem *prev_map[FIX_BTMAPS_SLOTS] __initdata; 68 static unsigned long prev_size[FIX_BTMAPS_SLOT 64 static unsigned long prev_size[FIX_BTMAPS_SLOTS] __initdata; 69 static unsigned long slot_virt[FIX_BTMAPS_SLOT 65 static unsigned long slot_virt[FIX_BTMAPS_SLOTS] __initdata; 70 66 71 void __init early_ioremap_setup(void) 67 void __init early_ioremap_setup(void) 72 { 68 { 73 int i; 69 int i; 74 70 75 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) !! 71 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 76 WARN_ON_ONCE(prev_map[i]); !! 72 if (WARN_ON(prev_map[i])) >> 73 break; >> 74 >> 75 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 77 slot_virt[i] = __fix_to_virt(F 76 slot_virt[i] = __fix_to_virt(FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*i); 78 } << 79 } 77 } 80 78 81 static int __init check_early_ioremap_leak(voi 79 static int __init check_early_ioremap_leak(void) 82 { 80 { 83 int count = 0; 81 int count = 0; 84 int i; 82 int i; 85 83 86 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 84 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 87 if (prev_map[i]) 85 if (prev_map[i]) 88 count++; 86 count++; 89 87 90 if (WARN(count, KERN_WARNING 88 if (WARN(count, KERN_WARNING 91 "Debug warning: early ioremap 89 "Debug warning: early ioremap leak of %d areas detected.\n" 92 "please boot with early_iorem 90 "please boot with early_ioremap_debug and report the dmesg.\n", 93 count)) 91 count)) 94 return 1; 92 return 1; 95 return 0; 93 return 0; 96 } 94 } 97 late_initcall(check_early_ioremap_leak); 95 late_initcall(check_early_ioremap_leak); 98 96 99 static void __init __iomem * 97 static void __init __iomem * 100 __early_ioremap(resource_size_t phys_addr, uns 98 __early_ioremap(resource_size_t phys_addr, unsigned long size, pgprot_t prot) 101 { 99 { 102 unsigned long offset; 100 unsigned long offset; 103 resource_size_t last_addr; 101 resource_size_t last_addr; 104 unsigned int nrpages; 102 unsigned int nrpages; 105 enum fixed_addresses idx; 103 enum fixed_addresses idx; 106 int i, slot; 104 int i, slot; 107 105 108 WARN_ON(system_state >= SYSTEM_RUNNING !! 106 WARN_ON(system_state != SYSTEM_BOOTING); 109 107 110 slot = -1; 108 slot = -1; 111 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 109 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) { 112 if (!prev_map[i]) { 110 if (!prev_map[i]) { 113 slot = i; 111 slot = i; 114 break; 112 break; 115 } 113 } 116 } 114 } 117 115 118 if (WARN(slot < 0, "%s(%pa, %08lx) not !! 116 if (WARN(slot < 0, "%s(%08llx, %08lx) not found slot\n", 119 __func__, &phys_addr, size)) !! 117 __func__, (u64)phys_addr, size)) 120 return NULL; 118 return NULL; 121 119 122 /* Don't allow wraparound or zero size 120 /* Don't allow wraparound or zero size */ 123 last_addr = phys_addr + size - 1; 121 last_addr = phys_addr + size - 1; 124 if (WARN_ON(!size || last_addr < phys_ 122 if (WARN_ON(!size || last_addr < phys_addr)) 125 return NULL; 123 return NULL; 126 124 127 prev_size[slot] = size; 125 prev_size[slot] = size; 128 /* 126 /* 129 * Mappings have to be page-aligned 127 * Mappings have to be page-aligned 130 */ 128 */ 131 offset = offset_in_page(phys_addr); 129 offset = offset_in_page(phys_addr); 132 phys_addr &= PAGE_MASK; 130 phys_addr &= PAGE_MASK; 133 size = PAGE_ALIGN(last_addr + 1) - phy 131 size = PAGE_ALIGN(last_addr + 1) - phys_addr; 134 132 135 /* 133 /* 136 * Mappings have to fit in the FIX_BTM 134 * Mappings have to fit in the FIX_BTMAP area. 137 */ 135 */ 138 nrpages = size >> PAGE_SHIFT; 136 nrpages = size >> PAGE_SHIFT; 139 if (WARN_ON(nrpages > NR_FIX_BTMAPS)) 137 if (WARN_ON(nrpages > NR_FIX_BTMAPS)) 140 return NULL; 138 return NULL; 141 139 142 /* 140 /* 143 * Ok, go for it.. 141 * Ok, go for it.. 144 */ 142 */ 145 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS* 143 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot; 146 while (nrpages > 0) { 144 while (nrpages > 0) { 147 if (after_paging_init) 145 if (after_paging_init) 148 __late_set_fixmap(idx, 146 __late_set_fixmap(idx, phys_addr, prot); 149 else 147 else 150 __early_set_fixmap(idx 148 __early_set_fixmap(idx, phys_addr, prot); 151 phys_addr += PAGE_SIZE; 149 phys_addr += PAGE_SIZE; 152 --idx; 150 --idx; 153 --nrpages; 151 --nrpages; 154 } 152 } 155 WARN(early_ioremap_debug, "%s(%pa, %08 !! 153 WARN(early_ioremap_debug, "%s(%08llx, %08lx) [%d] => %08lx + %08lx\n", 156 __func__, &phys_addr, size, slot, !! 154 __func__, (u64)phys_addr, size, slot, offset, slot_virt[slot]); 157 155 158 prev_map[slot] = (void __iomem *)(offs 156 prev_map[slot] = (void __iomem *)(offset + slot_virt[slot]); 159 return prev_map[slot]; 157 return prev_map[slot]; 160 } 158 } 161 159 162 void __init early_iounmap(void __iomem *addr, 160 void __init early_iounmap(void __iomem *addr, unsigned long size) 163 { 161 { 164 unsigned long virt_addr; 162 unsigned long virt_addr; 165 unsigned long offset; 163 unsigned long offset; 166 unsigned int nrpages; 164 unsigned int nrpages; 167 enum fixed_addresses idx; 165 enum fixed_addresses idx; 168 int i, slot; 166 int i, slot; 169 167 170 slot = -1; 168 slot = -1; 171 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) 169 for (i = 0; i < FIX_BTMAPS_SLOTS; i++) { 172 if (prev_map[i] == addr) { 170 if (prev_map[i] == addr) { 173 slot = i; 171 slot = i; 174 break; 172 break; 175 } 173 } 176 } 174 } 177 175 178 if (WARN(slot < 0, "%s(%p, %08lx) not !! 176 if (WARN(slot < 0, "early_iounmap(%p, %08lx) not found slot\n", 179 __func__, addr, size)) !! 177 addr, size)) 180 return; 178 return; 181 179 182 if (WARN(prev_size[slot] != size, 180 if (WARN(prev_size[slot] != size, 183 "%s(%p, %08lx) [%d] size not !! 181 "early_iounmap(%p, %08lx) [%d] size not consistent %08lx\n", 184 __func__, addr, size, slot, !! 182 addr, size, slot, prev_size[slot])) 185 return; 183 return; 186 184 187 WARN(early_ioremap_debug, "%s(%p, %08l !! 185 WARN(early_ioremap_debug, "early_iounmap(%p, %08lx) [%d]\n", 188 __func__, addr, size, slot); !! 186 addr, size, slot); 189 187 190 virt_addr = (unsigned long)addr; 188 virt_addr = (unsigned long)addr; 191 if (WARN_ON(virt_addr < fix_to_virt(FI 189 if (WARN_ON(virt_addr < fix_to_virt(FIX_BTMAP_BEGIN))) 192 return; 190 return; 193 191 194 offset = offset_in_page(virt_addr); 192 offset = offset_in_page(virt_addr); 195 nrpages = PAGE_ALIGN(offset + size) >> 193 nrpages = PAGE_ALIGN(offset + size) >> PAGE_SHIFT; 196 194 197 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS* 195 idx = FIX_BTMAP_BEGIN - NR_FIX_BTMAPS*slot; 198 while (nrpages > 0) { 196 while (nrpages > 0) { 199 if (after_paging_init) 197 if (after_paging_init) 200 __late_clear_fixmap(id 198 __late_clear_fixmap(idx); 201 else 199 else 202 __early_set_fixmap(idx 200 __early_set_fixmap(idx, 0, FIXMAP_PAGE_CLEAR); 203 --idx; 201 --idx; 204 --nrpages; 202 --nrpages; 205 } 203 } 206 prev_map[slot] = NULL; 204 prev_map[slot] = NULL; 207 } 205 } 208 206 209 /* Remap an IO device */ 207 /* Remap an IO device */ 210 void __init __iomem * 208 void __init __iomem * 211 early_ioremap(resource_size_t phys_addr, unsig 209 early_ioremap(resource_size_t phys_addr, unsigned long size) 212 { 210 { 213 return __early_ioremap(phys_addr, size 211 return __early_ioremap(phys_addr, size, FIXMAP_PAGE_IO); 214 } 212 } 215 213 216 /* Remap memory */ 214 /* Remap memory */ 217 void __init * 215 void __init * 218 early_memremap(resource_size_t phys_addr, unsi 216 early_memremap(resource_size_t phys_addr, unsigned long size) 219 { 217 { 220 pgprot_t prot = early_memremap_pgprot_ !! 218 return (__force void *)__early_ioremap(phys_addr, size, 221 !! 219 FIXMAP_PAGE_NORMAL); 222 << 223 return (__force void *)__early_ioremap << 224 } 220 } 225 #ifdef FIXMAP_PAGE_RO 221 #ifdef FIXMAP_PAGE_RO 226 void __init * 222 void __init * 227 early_memremap_ro(resource_size_t phys_addr, u 223 early_memremap_ro(resource_size_t phys_addr, unsigned long size) 228 { 224 { 229 pgprot_t prot = early_memremap_pgprot_ !! 225 return (__force void *)__early_ioremap(phys_addr, size, FIXMAP_PAGE_RO); 230 << 231 << 232 return (__force void *)__early_ioremap << 233 } << 234 #endif << 235 << 236 #ifdef CONFIG_ARCH_USE_MEMREMAP_PROT << 237 void __init * << 238 early_memremap_prot(resource_size_t phys_addr, << 239 unsigned long prot_val) << 240 { << 241 return (__force void *)__early_ioremap << 242 << 243 } 226 } 244 #endif 227 #endif 245 228 246 #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE 229 #define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT) 247 230 248 void __init copy_from_early_mem(void *dest, ph 231 void __init copy_from_early_mem(void *dest, phys_addr_t src, unsigned long size) 249 { 232 { 250 unsigned long slop, clen; 233 unsigned long slop, clen; 251 char *p; 234 char *p; 252 235 253 while (size) { 236 while (size) { 254 slop = offset_in_page(src); 237 slop = offset_in_page(src); 255 clen = size; 238 clen = size; 256 if (clen > MAX_MAP_CHUNK - slo 239 if (clen > MAX_MAP_CHUNK - slop) 257 clen = MAX_MAP_CHUNK - 240 clen = MAX_MAP_CHUNK - slop; 258 p = early_memremap(src & PAGE_ 241 p = early_memremap(src & PAGE_MASK, clen + slop); 259 memcpy(dest, p + slop, clen); 242 memcpy(dest, p + slop, clen); 260 early_memunmap(p, clen + slop) 243 early_memunmap(p, clen + slop); 261 dest += clen; 244 dest += clen; 262 src += clen; 245 src += clen; 263 size -= clen; 246 size -= clen; 264 } 247 } 265 } 248 } 266 249 267 #else /* CONFIG_MMU */ 250 #else /* CONFIG_MMU */ 268 251 269 void __init __iomem * 252 void __init __iomem * 270 early_ioremap(resource_size_t phys_addr, unsig 253 early_ioremap(resource_size_t phys_addr, unsigned long size) 271 { 254 { 272 return (__force void __iomem *)phys_ad 255 return (__force void __iomem *)phys_addr; 273 } 256 } 274 257 275 /* Remap memory */ 258 /* Remap memory */ 276 void __init * 259 void __init * 277 early_memremap(resource_size_t phys_addr, unsi 260 early_memremap(resource_size_t phys_addr, unsigned long size) 278 { 261 { 279 return (void *)phys_addr; 262 return (void *)phys_addr; 280 } 263 } 281 void __init * 264 void __init * 282 early_memremap_ro(resource_size_t phys_addr, u 265 early_memremap_ro(resource_size_t phys_addr, unsigned long size) 283 { 266 { 284 return (void *)phys_addr; 267 return (void *)phys_addr; 285 } 268 } 286 269 287 void __init early_iounmap(void __iomem *addr, 270 void __init early_iounmap(void __iomem *addr, unsigned long size) 288 { 271 { 289 } 272 } 290 273 291 #endif /* CONFIG_MMU */ 274 #endif /* CONFIG_MMU */ 292 275 293 276 294 void __init early_memunmap(void *addr, unsigne 277 void __init early_memunmap(void *addr, unsigned long size) 295 { 278 { 296 early_iounmap((__force void __iomem *) 279 early_iounmap((__force void __iomem *)addr, size); 297 } 280 } 298 281
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.