1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2012 Regents of the University of California 4 * Copyright (C) 2019 Western Digital Corporation or its affiliates. 5 */ 6 7 #include <linux/init.h> 8 #include <linux/mm.h> 9 #include <linux/memblock.h> 10 #include <linux/initrd.h> 11 #include <linux/swap.h> 12 #include <linux/sizes.h> 13 #include <linux/of_fdt.h> 14 #include <linux/libfdt.h> 15 16 #include <asm/fixmap.h> 17 #include <asm/tlbflush.h> 18 #include <asm/sections.h> 19 #include <asm/pgtable.h> 20 #include <asm/io.h> 21 22 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] 23 __page_aligned_bss; 24 EXPORT_SYMBOL(empty_zero_page); 25 26 extern char _start[]; 27 28 static void __init zone_sizes_init(void) 29 { 30 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; 31 32 #ifdef CONFIG_ZONE_DMA32 33 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G, 34 (unsigned long) PFN_PHYS(max_low_pfn))); 35 #endif 36 max_zone_pfns[ZONE_NORMAL] = max_low_pfn; 37 38 free_area_init_nodes(max_zone_pfns); 39 } 40 41 void setup_zero_page(void) 42 { 43 memset((void *)empty_zero_page, 0, PAGE_SIZE); 44 } 45 46 void __init mem_init(void) 47 { 48 #ifdef CONFIG_FLATMEM 49 BUG_ON(!mem_map); 50 #endif /* CONFIG_FLATMEM */ 51 52 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn))); 53 memblock_free_all(); 54 55 mem_init_print_info(NULL); 56 } 57 58 #ifdef CONFIG_BLK_DEV_INITRD 59 static void __init setup_initrd(void) 60 { 61 unsigned long size; 62 63 if (initrd_start >= initrd_end) { 64 pr_info("initrd not found or empty"); 65 goto disable; 66 } 67 if (__pa(initrd_end) > PFN_PHYS(max_low_pfn)) { 68 pr_err("initrd extends beyond end of memory"); 69 goto disable; 70 } 71 72 size = initrd_end - initrd_start; 73 memblock_reserve(__pa(initrd_start), size); 74 initrd_below_start_ok = 1; 75 76 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n", 77 (void *)(initrd_start), size); 78 return; 79 disable: 80 pr_cont(" - disabling initrd\n"); 81 initrd_start = 0; 82 initrd_end = 0; 83 } 84 #endif /* CONFIG_BLK_DEV_INITRD */ 85 86 static phys_addr_t dtb_early_pa __initdata; 87 88 void __init setup_bootmem(void) 89 { 90 struct memblock_region *reg; 91 phys_addr_t mem_size = 0; 92 phys_addr_t vmlinux_end = __pa(&_end); 93 phys_addr_t vmlinux_start = __pa(&_start); 94 95 /* Find the memory region containing the kernel */ 96 for_each_memblock(memory, reg) { 97 phys_addr_t end = reg->base + reg->size; 98 99 if (reg->base <= vmlinux_end && vmlinux_end <= end) { 100 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET); 101 102 /* 103 * Remove memblock from the end of usable area to the 104 * end of region 105 */ 106 if (reg->base + mem_size < end) 107 memblock_remove(reg->base + mem_size, 108 end - reg->base - mem_size); 109 } 110 } 111 BUG_ON(mem_size == 0); 112 113 /* Reserve from the start of the kernel to the end of the kernel */ 114 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); 115 116 set_max_mapnr(PFN_DOWN(mem_size)); 117 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM()); 118 119 #ifdef CONFIG_BLK_DEV_INITRD 120 setup_initrd(); 121 #endif /* CONFIG_BLK_DEV_INITRD */ 122 123 /* 124 * Avoid using early_init_fdt_reserve_self() since __pa() does 125 * not work for DTB pointers that are fixmap addresses 126 */ 127 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); 128 129 early_init_fdt_scan_reserved_mem(); 130 memblock_allow_resize(); 131 memblock_dump_all(); 132 133 for_each_memblock(memory, reg) { 134 unsigned long start_pfn = memblock_region_memory_base_pfn(reg); 135 unsigned long end_pfn = memblock_region_memory_end_pfn(reg); 136 137 memblock_set_node(PFN_PHYS(start_pfn), 138 PFN_PHYS(end_pfn - start_pfn), 139 &memblock.memory, 0); 140 } 141 } 142 143 unsigned long va_pa_offset; 144 EXPORT_SYMBOL(va_pa_offset); 145 unsigned long pfn_base; 146 EXPORT_SYMBOL(pfn_base); 147 148 void *dtb_early_va; 149 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 150 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 151 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; 152 static bool mmu_enabled; 153 154 #define MAX_EARLY_MAPPING_SIZE SZ_128M 155 156 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); 157 158 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) 159 { 160 unsigned long addr = __fix_to_virt(idx); 161 pte_t *ptep; 162 163 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 164 165 ptep = &fixmap_pte[pte_index(addr)]; 166 167 if (pgprot_val(prot)) { 168 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); 169 } else { 170 pte_clear(&init_mm, addr, ptep); 171 local_flush_tlb_page(addr); 172 } 173 } 174 175 static pte_t *__init get_pte_virt(phys_addr_t pa) 176 { 177 if (mmu_enabled) { 178 clear_fixmap(FIX_PTE); 179 return (pte_t *)set_fixmap_offset(FIX_PTE, pa); 180 } else { 181 return (pte_t *)((uintptr_t)pa); 182 } 183 } 184 185 static phys_addr_t __init alloc_pte(uintptr_t va) 186 { 187 /* 188 * We only create PMD or PGD early mappings so we 189 * should never reach here with MMU disabled. 190 */ 191 BUG_ON(!mmu_enabled); 192 193 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 194 } 195 196 static void __init create_pte_mapping(pte_t *ptep, 197 uintptr_t va, phys_addr_t pa, 198 phys_addr_t sz, pgprot_t prot) 199 { 200 uintptr_t pte_index = pte_index(va); 201 202 BUG_ON(sz != PAGE_SIZE); 203 204 if (pte_none(ptep[pte_index])) 205 ptep[pte_index] = pfn_pte(PFN_DOWN(pa), prot); 206 } 207 208 #ifndef __PAGETABLE_PMD_FOLDED 209 210 pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss; 211 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; 212 213 #if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE 214 #define NUM_EARLY_PMDS 1UL 215 #else 216 #define NUM_EARLY_PMDS (1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE) 217 #endif 218 pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE); 219 220 static pmd_t *__init get_pmd_virt(phys_addr_t pa) 221 { 222 if (mmu_enabled) { 223 clear_fixmap(FIX_PMD); 224 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); 225 } else { 226 return (pmd_t *)((uintptr_t)pa); 227 } 228 } 229 230 static phys_addr_t __init alloc_pmd(uintptr_t va) 231 { 232 uintptr_t pmd_num; 233 234 if (mmu_enabled) 235 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 236 237 pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT; 238 BUG_ON(pmd_num >= NUM_EARLY_PMDS); 239 return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD]; 240 } 241 242 static void __init create_pmd_mapping(pmd_t *pmdp, 243 uintptr_t va, phys_addr_t pa, 244 phys_addr_t sz, pgprot_t prot) 245 { 246 pte_t *ptep; 247 phys_addr_t pte_phys; 248 uintptr_t pmd_index = pmd_index(va); 249 250 if (sz == PMD_SIZE) { 251 if (pmd_none(pmdp[pmd_index])) 252 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pa), prot); 253 return; 254 } 255 256 if (pmd_none(pmdp[pmd_index])) { 257 pte_phys = alloc_pte(va); 258 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); 259 ptep = get_pte_virt(pte_phys); 260 memset(ptep, 0, PAGE_SIZE); 261 } else { 262 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_index])); 263 ptep = get_pte_virt(pte_phys); 264 } 265 266 create_pte_mapping(ptep, va, pa, sz, prot); 267 } 268 269 #define pgd_next_t pmd_t 270 #define alloc_pgd_next(__va) alloc_pmd(__va) 271 #define get_pgd_next_virt(__pa) get_pmd_virt(__pa) 272 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ 273 create_pmd_mapping(__nextp, __va, __pa, __sz, __prot) 274 #define PTE_PARENT_SIZE PMD_SIZE 275 #define fixmap_pgd_next fixmap_pmd 276 #else 277 #define pgd_next_t pte_t 278 #define alloc_pgd_next(__va) alloc_pte(__va) 279 #define get_pgd_next_virt(__pa) get_pte_virt(__pa) 280 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ 281 create_pte_mapping(__nextp, __va, __pa, __sz, __prot) 282 #define PTE_PARENT_SIZE PGDIR_SIZE 283 #define fixmap_pgd_next fixmap_pte 284 #endif 285 286 static void __init create_pgd_mapping(pgd_t *pgdp, 287 uintptr_t va, phys_addr_t pa, 288 phys_addr_t sz, pgprot_t prot) 289 { 290 pgd_next_t *nextp; 291 phys_addr_t next_phys; 292 uintptr_t pgd_index = pgd_index(va); 293 294 if (sz == PGDIR_SIZE) { 295 if (pgd_val(pgdp[pgd_index]) == 0) 296 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(pa), prot); 297 return; 298 } 299 300 if (pgd_val(pgdp[pgd_index]) == 0) { 301 next_phys = alloc_pgd_next(va); 302 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE); 303 nextp = get_pgd_next_virt(next_phys); 304 memset(nextp, 0, PAGE_SIZE); 305 } else { 306 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_index])); 307 nextp = get_pgd_next_virt(next_phys); 308 } 309 310 create_pgd_next_mapping(nextp, va, pa, sz, prot); 311 } 312 313 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) 314 { 315 uintptr_t map_size = PAGE_SIZE; 316 317 /* Upgrade to PMD/PGDIR mappings whenever possible */ 318 if (!(base & (PTE_PARENT_SIZE - 1)) && 319 !(size & (PTE_PARENT_SIZE - 1))) 320 map_size = PTE_PARENT_SIZE; 321 322 return map_size; 323 } 324 325 /* 326 * setup_vm() is called from head.S with MMU-off. 327 * 328 * Following requirements should be honoured for setup_vm() to work 329 * correctly: 330 * 1) It should use PC-relative addressing for accessing kernel symbols. 331 * To achieve this we always use GCC cmodel=medany. 332 * 2) The compiler instrumentation for FTRACE will not work for setup_vm() 333 * so disable compiler instrumentation when FTRACE is enabled. 334 * 335 * Currently, the above requirements are honoured by using custom CFLAGS 336 * for init.o in mm/Makefile. 337 */ 338 339 #ifndef __riscv_cmodel_medany 340 #error "setup_vm() is called from head.S before relocate so it should " 341 "not use absolute addressing." 342 #endif 343 344 asmlinkage void __init setup_vm(uintptr_t dtb_pa) 345 { 346 uintptr_t va, end_va; 347 uintptr_t load_pa = (uintptr_t)(&_start); 348 uintptr_t load_sz = (uintptr_t)(&_end) - load_pa; 349 uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE); 350 351 va_pa_offset = PAGE_OFFSET - load_pa; 352 pfn_base = PFN_DOWN(load_pa); 353 354 /* 355 * Enforce boot alignment requirements of RV32 and 356 * RV64 by only allowing PMD or PGD mappings. 357 */ 358 BUG_ON(map_size == PAGE_SIZE); 359 360 /* Sanity check alignment and size */ 361 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); 362 BUG_ON((load_pa % map_size) != 0); 363 BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE); 364 365 /* Setup early PGD for fixmap */ 366 create_pgd_mapping(early_pg_dir, FIXADDR_START, 367 (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); 368 369 #ifndef __PAGETABLE_PMD_FOLDED 370 /* Setup fixmap PMD */ 371 create_pmd_mapping(fixmap_pmd, FIXADDR_START, 372 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE); 373 /* Setup trampoline PGD and PMD */ 374 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET, 375 (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE); 376 create_pmd_mapping(trampoline_pmd, PAGE_OFFSET, 377 load_pa, PMD_SIZE, PAGE_KERNEL_EXEC); 378 #else 379 /* Setup trampoline PGD */ 380 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET, 381 load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC); 382 #endif 383 384 /* 385 * Setup early PGD covering entire kernel which will allows 386 * us to reach paging_init(). We map all memory banks later 387 * in setup_vm_final() below. 388 */ 389 end_va = PAGE_OFFSET + load_sz; 390 for (va = PAGE_OFFSET; va < end_va; va += map_size) 391 create_pgd_mapping(early_pg_dir, va, 392 load_pa + (va - PAGE_OFFSET), 393 map_size, PAGE_KERNEL_EXEC); 394 395 /* Create fixed mapping for early FDT parsing */ 396 end_va = __fix_to_virt(FIX_FDT) + FIX_FDT_SIZE; 397 for (va = __fix_to_virt(FIX_FDT); va < end_va; va += PAGE_SIZE) 398 create_pte_mapping(fixmap_pte, va, 399 dtb_pa + (va - __fix_to_virt(FIX_FDT)), 400 PAGE_SIZE, PAGE_KERNEL); 401 402 /* Save pointer to DTB for early FDT parsing */ 403 dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK); 404 /* Save physical address for memblock reservation */ 405 dtb_early_pa = dtb_pa; 406 } 407 408 static void __init setup_vm_final(void) 409 { 410 uintptr_t va, map_size; 411 phys_addr_t pa, start, end; 412 struct memblock_region *reg; 413 414 /* Set mmu_enabled flag */ 415 mmu_enabled = true; 416 417 /* Setup swapper PGD for fixmap */ 418 create_pgd_mapping(swapper_pg_dir, FIXADDR_START, 419 __pa(fixmap_pgd_next), 420 PGDIR_SIZE, PAGE_TABLE); 421 422 /* Map all memory banks */ 423 for_each_memblock(memory, reg) { 424 start = reg->base; 425 end = start + reg->size; 426 427 if (start >= end) 428 break; 429 if (memblock_is_nomap(reg)) 430 continue; 431 if (start <= __pa(PAGE_OFFSET) && 432 __pa(PAGE_OFFSET) < end) 433 start = __pa(PAGE_OFFSET); 434 435 map_size = best_map_size(start, end - start); 436 for (pa = start; pa < end; pa += map_size) { 437 va = (uintptr_t)__va(pa); 438 create_pgd_mapping(swapper_pg_dir, va, pa, 439 map_size, PAGE_KERNEL_EXEC); 440 } 441 } 442 443 /* Clear fixmap PTE and PMD mappings */ 444 clear_fixmap(FIX_PTE); 445 clear_fixmap(FIX_PMD); 446 447 /* Move to swapper page table */ 448 csr_write(CSR_SATP, PFN_DOWN(__pa(swapper_pg_dir)) | SATP_MODE); 449 local_flush_tlb_all(); 450 } 451 452 void __init paging_init(void) 453 { 454 setup_vm_final(); 455 memblocks_present(); 456 sparse_init(); 457 setup_zero_page(); 458 zone_sizes_init(); 459 } 460 461 #ifdef CONFIG_SPARSEMEM_VMEMMAP 462 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 463 struct vmem_altmap *altmap) 464 { 465 return vmemmap_populate_basepages(start, end, node); 466 } 467 #endif 468