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