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