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 * Copyright (C) 2020 FORTH-ICS/CARV 6 * Nick Kossifidis <mick@ics.forth.gr> 7 */ 8 9 #include <linux/init.h> 10 #include <linux/mm.h> 11 #include <linux/memblock.h> 12 #include <linux/initrd.h> 13 #include <linux/swap.h> 14 #include <linux/swiotlb.h> 15 #include <linux/sizes.h> 16 #include <linux/of_fdt.h> 17 #include <linux/of_reserved_mem.h> 18 #include <linux/libfdt.h> 19 #include <linux/set_memory.h> 20 #include <linux/dma-map-ops.h> 21 #include <linux/crash_dump.h> 22 #include <linux/hugetlb.h> 23 24 #include <asm/fixmap.h> 25 #include <asm/tlbflush.h> 26 #include <asm/sections.h> 27 #include <asm/soc.h> 28 #include <asm/io.h> 29 #include <asm/ptdump.h> 30 #include <asm/numa.h> 31 32 #include "../kernel/head.h" 33 34 struct kernel_mapping kernel_map __ro_after_init; 35 EXPORT_SYMBOL(kernel_map); 36 #ifdef CONFIG_XIP_KERNEL 37 #define kernel_map (*(struct kernel_mapping *)XIP_FIXUP(&kernel_map)) 38 #endif 39 40 #ifdef CONFIG_64BIT 41 u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39; 42 #else 43 u64 satp_mode __ro_after_init = SATP_MODE_32; 44 #endif 45 EXPORT_SYMBOL(satp_mode); 46 47 bool pgtable_l4_enabled = IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_XIP_KERNEL); 48 bool pgtable_l5_enabled = IS_ENABLED(CONFIG_64BIT) && !IS_ENABLED(CONFIG_XIP_KERNEL); 49 EXPORT_SYMBOL(pgtable_l4_enabled); 50 EXPORT_SYMBOL(pgtable_l5_enabled); 51 52 phys_addr_t phys_ram_base __ro_after_init; 53 EXPORT_SYMBOL(phys_ram_base); 54 55 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] 56 __page_aligned_bss; 57 EXPORT_SYMBOL(empty_zero_page); 58 59 extern char _start[]; 60 #define DTB_EARLY_BASE_VA PGDIR_SIZE 61 void *_dtb_early_va __initdata; 62 uintptr_t _dtb_early_pa __initdata; 63 64 static phys_addr_t dma32_phys_limit __initdata; 65 66 static void __init zone_sizes_init(void) 67 { 68 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, }; 69 70 #ifdef CONFIG_ZONE_DMA32 71 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit); 72 #endif 73 max_zone_pfns[ZONE_NORMAL] = max_low_pfn; 74 75 free_area_init(max_zone_pfns); 76 } 77 78 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM) 79 static inline void print_mlk(char *name, unsigned long b, unsigned long t) 80 { 81 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t, 82 (((t) - (b)) >> 10)); 83 } 84 85 static inline void print_mlm(char *name, unsigned long b, unsigned long t) 86 { 87 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t, 88 (((t) - (b)) >> 20)); 89 } 90 91 static void __init print_vm_layout(void) 92 { 93 pr_notice("Virtual kernel memory layout:\n"); 94 print_mlk("fixmap", (unsigned long)FIXADDR_START, 95 (unsigned long)FIXADDR_TOP); 96 print_mlm("pci io", (unsigned long)PCI_IO_START, 97 (unsigned long)PCI_IO_END); 98 print_mlm("vmemmap", (unsigned long)VMEMMAP_START, 99 (unsigned long)VMEMMAP_END); 100 print_mlm("vmalloc", (unsigned long)VMALLOC_START, 101 (unsigned long)VMALLOC_END); 102 print_mlm("lowmem", (unsigned long)PAGE_OFFSET, 103 (unsigned long)high_memory); 104 if (IS_ENABLED(CONFIG_64BIT)) { 105 #ifdef CONFIG_KASAN 106 print_mlm("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END); 107 #endif 108 109 print_mlm("kernel", (unsigned long)KERNEL_LINK_ADDR, 110 (unsigned long)ADDRESS_SPACE_END); 111 } 112 } 113 #else 114 static void print_vm_layout(void) { } 115 #endif /* CONFIG_DEBUG_VM */ 116 117 void __init mem_init(void) 118 { 119 #ifdef CONFIG_FLATMEM 120 BUG_ON(!mem_map); 121 #endif /* CONFIG_FLATMEM */ 122 123 #ifdef CONFIG_SWIOTLB 124 if (swiotlb_force == SWIOTLB_FORCE || 125 max_pfn > PFN_DOWN(dma32_phys_limit)) 126 swiotlb_init(1); 127 else 128 swiotlb_force = SWIOTLB_NO_FORCE; 129 #endif 130 memblock_free_all(); 131 132 print_vm_layout(); 133 } 134 135 /* Limit the memory size via mem. */ 136 static phys_addr_t memory_limit; 137 138 static int __init early_mem(char *p) 139 { 140 u64 size; 141 142 if (!p) 143 return 1; 144 145 size = memparse(p, &p) & PAGE_MASK; 146 memory_limit = min_t(u64, size, memory_limit); 147 148 pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20); 149 150 return 0; 151 } 152 early_param("mem", early_mem); 153 154 static void __init setup_bootmem(void) 155 { 156 phys_addr_t vmlinux_end = __pa_symbol(&_end); 157 phys_addr_t max_mapped_addr; 158 phys_addr_t phys_ram_end, vmlinux_start; 159 160 if (IS_ENABLED(CONFIG_XIP_KERNEL)) 161 vmlinux_start = __pa_symbol(&_sdata); 162 else 163 vmlinux_start = __pa_symbol(&_start); 164 165 memblock_enforce_memory_limit(memory_limit); 166 167 /* 168 * Make sure we align the reservation on PMD_SIZE since we will 169 * map the kernel in the linear mapping as read-only: we do not want 170 * any allocation to happen between _end and the next pmd aligned page. 171 */ 172 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) 173 vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK; 174 /* 175 * Reserve from the start of the kernel to the end of the kernel 176 */ 177 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); 178 179 phys_ram_end = memblock_end_of_DRAM(); 180 if (!IS_ENABLED(CONFIG_XIP_KERNEL)) 181 phys_ram_base = memblock_start_of_DRAM(); 182 /* 183 * memblock allocator is not aware of the fact that last 4K bytes of 184 * the addressable memory can not be mapped because of IS_ERR_VALUE 185 * macro. Make sure that last 4k bytes are not usable by memblock 186 * if end of dram is equal to maximum addressable memory. For 64-bit 187 * kernel, this problem can't happen here as the end of the virtual 188 * address space is occupied by the kernel mapping then this check must 189 * be done as soon as the kernel mapping base address is determined. 190 */ 191 if (!IS_ENABLED(CONFIG_64BIT)) { 192 max_mapped_addr = __pa(~(ulong)0); 193 if (max_mapped_addr == (phys_ram_end - 1)) 194 memblock_set_current_limit(max_mapped_addr - 4096); 195 } 196 197 min_low_pfn = PFN_UP(phys_ram_base); 198 max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end); 199 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn))); 200 201 dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn)); 202 set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET); 203 204 reserve_initrd_mem(); 205 /* 206 * If DTB is built in, no need to reserve its memblock. 207 * Otherwise, do reserve it but avoid using 208 * early_init_fdt_reserve_self() since __pa() does 209 * not work for DTB pointers that are fixmap addresses 210 */ 211 if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) 212 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va)); 213 214 early_init_fdt_scan_reserved_mem(); 215 dma_contiguous_reserve(dma32_phys_limit); 216 if (IS_ENABLED(CONFIG_64BIT)) 217 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); 218 memblock_allow_resize(); 219 } 220 221 #ifdef CONFIG_MMU 222 struct pt_alloc_ops pt_ops __initdata; 223 224 unsigned long riscv_pfn_base __ro_after_init; 225 EXPORT_SYMBOL(riscv_pfn_base); 226 227 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 228 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; 229 static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; 230 231 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE); 232 static p4d_t __maybe_unused early_dtb_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE); 233 static pud_t __maybe_unused early_dtb_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE); 234 static pmd_t __maybe_unused early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE); 235 236 #ifdef CONFIG_XIP_KERNEL 237 #define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops)) 238 #define riscv_pfn_base (*(unsigned long *)XIP_FIXUP(&riscv_pfn_base)) 239 #define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir)) 240 #define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte)) 241 #define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir)) 242 #endif /* CONFIG_XIP_KERNEL */ 243 244 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot) 245 { 246 unsigned long addr = __fix_to_virt(idx); 247 pte_t *ptep; 248 249 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses); 250 251 ptep = &fixmap_pte[pte_index(addr)]; 252 253 if (pgprot_val(prot)) 254 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot)); 255 else 256 pte_clear(&init_mm, addr, ptep); 257 local_flush_tlb_page(addr); 258 } 259 260 static inline pte_t *__init get_pte_virt_early(phys_addr_t pa) 261 { 262 return (pte_t *)((uintptr_t)pa); 263 } 264 265 static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa) 266 { 267 clear_fixmap(FIX_PTE); 268 return (pte_t *)set_fixmap_offset(FIX_PTE, pa); 269 } 270 271 static inline pte_t *__init get_pte_virt_late(phys_addr_t pa) 272 { 273 return (pte_t *) __va(pa); 274 } 275 276 static inline phys_addr_t __init alloc_pte_early(uintptr_t va) 277 { 278 /* 279 * We only create PMD or PGD early mappings so we 280 * should never reach here with MMU disabled. 281 */ 282 BUG(); 283 } 284 285 static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va) 286 { 287 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 288 } 289 290 static phys_addr_t __init alloc_pte_late(uintptr_t va) 291 { 292 unsigned long vaddr; 293 294 vaddr = __get_free_page(GFP_KERNEL); 295 BUG_ON(!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr))); 296 297 return __pa(vaddr); 298 } 299 300 static void __init create_pte_mapping(pte_t *ptep, 301 uintptr_t va, phys_addr_t pa, 302 phys_addr_t sz, pgprot_t prot) 303 { 304 uintptr_t pte_idx = pte_index(va); 305 306 BUG_ON(sz != PAGE_SIZE); 307 308 if (pte_none(ptep[pte_idx])) 309 ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot); 310 } 311 312 #ifndef __PAGETABLE_PMD_FOLDED 313 314 static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss; 315 static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss; 316 static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE); 317 318 #ifdef CONFIG_XIP_KERNEL 319 #define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd)) 320 #define fixmap_pmd ((pmd_t *)XIP_FIXUP(fixmap_pmd)) 321 #define early_pmd ((pmd_t *)XIP_FIXUP(early_pmd)) 322 #endif /* CONFIG_XIP_KERNEL */ 323 324 static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss; 325 static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss; 326 static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE); 327 328 #ifdef CONFIG_XIP_KERNEL 329 #define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d)) 330 #define fixmap_p4d ((p4d_t *)XIP_FIXUP(fixmap_p4d)) 331 #define early_p4d ((p4d_t *)XIP_FIXUP(early_p4d)) 332 #endif /* CONFIG_XIP_KERNEL */ 333 334 static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss; 335 static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss; 336 static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE); 337 338 #ifdef CONFIG_XIP_KERNEL 339 #define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud)) 340 #define fixmap_pud ((pud_t *)XIP_FIXUP(fixmap_pud)) 341 #define early_pud ((pud_t *)XIP_FIXUP(early_pud)) 342 #endif /* CONFIG_XIP_KERNEL */ 343 344 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa) 345 { 346 /* Before MMU is enabled */ 347 return (pmd_t *)((uintptr_t)pa); 348 } 349 350 static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa) 351 { 352 clear_fixmap(FIX_PMD); 353 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa); 354 } 355 356 static pmd_t *__init get_pmd_virt_late(phys_addr_t pa) 357 { 358 return (pmd_t *) __va(pa); 359 } 360 361 static phys_addr_t __init alloc_pmd_early(uintptr_t va) 362 { 363 BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT); 364 365 return (uintptr_t)early_pmd; 366 } 367 368 static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va) 369 { 370 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 371 } 372 373 static phys_addr_t __init alloc_pmd_late(uintptr_t va) 374 { 375 unsigned long vaddr; 376 377 vaddr = __get_free_page(GFP_KERNEL); 378 BUG_ON(!vaddr || !pgtable_pmd_page_ctor(virt_to_page(vaddr))); 379 380 return __pa(vaddr); 381 } 382 383 static void __init create_pmd_mapping(pmd_t *pmdp, 384 uintptr_t va, phys_addr_t pa, 385 phys_addr_t sz, pgprot_t prot) 386 { 387 pte_t *ptep; 388 phys_addr_t pte_phys; 389 uintptr_t pmd_idx = pmd_index(va); 390 391 if (sz == PMD_SIZE) { 392 if (pmd_none(pmdp[pmd_idx])) 393 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot); 394 return; 395 } 396 397 if (pmd_none(pmdp[pmd_idx])) { 398 pte_phys = pt_ops.alloc_pte(va); 399 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE); 400 ptep = pt_ops.get_pte_virt(pte_phys); 401 memset(ptep, 0, PAGE_SIZE); 402 } else { 403 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx])); 404 ptep = pt_ops.get_pte_virt(pte_phys); 405 } 406 407 create_pte_mapping(ptep, va, pa, sz, prot); 408 } 409 410 static pud_t *__init get_pud_virt_early(phys_addr_t pa) 411 { 412 return (pud_t *)((uintptr_t)pa); 413 } 414 415 static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa) 416 { 417 clear_fixmap(FIX_PUD); 418 return (pud_t *)set_fixmap_offset(FIX_PUD, pa); 419 } 420 421 static pud_t *__init get_pud_virt_late(phys_addr_t pa) 422 { 423 return (pud_t *)__va(pa); 424 } 425 426 static phys_addr_t __init alloc_pud_early(uintptr_t va) 427 { 428 /* Only one PUD is available for early mapping */ 429 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT); 430 431 return (uintptr_t)early_pud; 432 } 433 434 static phys_addr_t __init alloc_pud_fixmap(uintptr_t va) 435 { 436 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 437 } 438 439 static phys_addr_t alloc_pud_late(uintptr_t va) 440 { 441 unsigned long vaddr; 442 443 vaddr = __get_free_page(GFP_KERNEL); 444 BUG_ON(!vaddr); 445 return __pa(vaddr); 446 } 447 448 static p4d_t *__init get_p4d_virt_early(phys_addr_t pa) 449 { 450 return (p4d_t *)((uintptr_t)pa); 451 } 452 453 static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa) 454 { 455 clear_fixmap(FIX_P4D); 456 return (p4d_t *)set_fixmap_offset(FIX_P4D, pa); 457 } 458 459 static p4d_t *__init get_p4d_virt_late(phys_addr_t pa) 460 { 461 return (p4d_t *)__va(pa); 462 } 463 464 static phys_addr_t __init alloc_p4d_early(uintptr_t va) 465 { 466 /* Only one P4D is available for early mapping */ 467 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT); 468 469 return (uintptr_t)early_p4d; 470 } 471 472 static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va) 473 { 474 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE); 475 } 476 477 static phys_addr_t alloc_p4d_late(uintptr_t va) 478 { 479 unsigned long vaddr; 480 481 vaddr = __get_free_page(GFP_KERNEL); 482 BUG_ON(!vaddr); 483 return __pa(vaddr); 484 } 485 486 static void __init create_pud_mapping(pud_t *pudp, 487 uintptr_t va, phys_addr_t pa, 488 phys_addr_t sz, pgprot_t prot) 489 { 490 pmd_t *nextp; 491 phys_addr_t next_phys; 492 uintptr_t pud_index = pud_index(va); 493 494 if (sz == PUD_SIZE) { 495 if (pud_val(pudp[pud_index]) == 0) 496 pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot); 497 return; 498 } 499 500 if (pud_val(pudp[pud_index]) == 0) { 501 next_phys = pt_ops.alloc_pmd(va); 502 pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE); 503 nextp = pt_ops.get_pmd_virt(next_phys); 504 memset(nextp, 0, PAGE_SIZE); 505 } else { 506 next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index])); 507 nextp = pt_ops.get_pmd_virt(next_phys); 508 } 509 510 create_pmd_mapping(nextp, va, pa, sz, prot); 511 } 512 513 static void __init create_p4d_mapping(p4d_t *p4dp, 514 uintptr_t va, phys_addr_t pa, 515 phys_addr_t sz, pgprot_t prot) 516 { 517 pud_t *nextp; 518 phys_addr_t next_phys; 519 uintptr_t p4d_index = p4d_index(va); 520 521 if (sz == P4D_SIZE) { 522 if (p4d_val(p4dp[p4d_index]) == 0) 523 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot); 524 return; 525 } 526 527 if (p4d_val(p4dp[p4d_index]) == 0) { 528 next_phys = pt_ops.alloc_pud(va); 529 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE); 530 nextp = pt_ops.get_pud_virt(next_phys); 531 memset(nextp, 0, PAGE_SIZE); 532 } else { 533 next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index])); 534 nextp = pt_ops.get_pud_virt(next_phys); 535 } 536 537 create_pud_mapping(nextp, va, pa, sz, prot); 538 } 539 540 #define pgd_next_t p4d_t 541 #define alloc_pgd_next(__va) (pgtable_l5_enabled ? \ 542 pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \ 543 pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va))) 544 #define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \ 545 pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \ 546 pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa))) 547 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ 548 (pgtable_l5_enabled ? \ 549 create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \ 550 (pgtable_l4_enabled ? \ 551 create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \ 552 create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot))) 553 #define fixmap_pgd_next (pgtable_l5_enabled ? \ 554 (uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \ 555 (uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd)) 556 #define trampoline_pgd_next (pgtable_l5_enabled ? \ 557 (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \ 558 (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd)) 559 #define early_dtb_pgd_next (pgtable_l5_enabled ? \ 560 (uintptr_t)early_dtb_p4d : (pgtable_l4_enabled ? \ 561 (uintptr_t)early_dtb_pud : (uintptr_t)early_dtb_pmd)) 562 #else 563 #define pgd_next_t pte_t 564 #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va) 565 #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa) 566 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \ 567 create_pte_mapping(__nextp, __va, __pa, __sz, __prot) 568 #define fixmap_pgd_next ((uintptr_t)fixmap_pte) 569 #define early_dtb_pgd_next ((uintptr_t)early_dtb_pmd) 570 #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) 571 #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) 572 #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) 573 #endif /* __PAGETABLE_PMD_FOLDED */ 574 575 void __init create_pgd_mapping(pgd_t *pgdp, 576 uintptr_t va, phys_addr_t pa, 577 phys_addr_t sz, pgprot_t prot) 578 { 579 pgd_next_t *nextp; 580 phys_addr_t next_phys; 581 uintptr_t pgd_idx = pgd_index(va); 582 583 if (sz == PGDIR_SIZE) { 584 if (pgd_val(pgdp[pgd_idx]) == 0) 585 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot); 586 return; 587 } 588 589 if (pgd_val(pgdp[pgd_idx]) == 0) { 590 next_phys = alloc_pgd_next(va); 591 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE); 592 nextp = get_pgd_next_virt(next_phys); 593 memset(nextp, 0, PAGE_SIZE); 594 } else { 595 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx])); 596 nextp = get_pgd_next_virt(next_phys); 597 } 598 599 create_pgd_next_mapping(nextp, va, pa, sz, prot); 600 } 601 602 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) 603 { 604 /* Upgrade to PMD_SIZE mappings whenever possible */ 605 if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1))) 606 return PAGE_SIZE; 607 608 return PMD_SIZE; 609 } 610 611 #ifdef CONFIG_XIP_KERNEL 612 #define phys_ram_base (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base)) 613 extern char _xiprom[], _exiprom[], __data_loc; 614 615 /* called from head.S with MMU off */ 616 asmlinkage void __init __copy_data(void) 617 { 618 void *from = (void *)(&__data_loc); 619 void *to = (void *)CONFIG_PHYS_RAM_BASE; 620 size_t sz = (size_t)((uintptr_t)(&_end) - (uintptr_t)(&_sdata)); 621 622 memcpy(to, from, sz); 623 } 624 #endif 625 626 #ifdef CONFIG_STRICT_KERNEL_RWX 627 static __init pgprot_t pgprot_from_va(uintptr_t va) 628 { 629 if (is_va_kernel_text(va)) 630 return PAGE_KERNEL_READ_EXEC; 631 632 /* 633 * In 64-bit kernel, the kernel mapping is outside the linear mapping so 634 * we must protect its linear mapping alias from being executed and 635 * written. 636 * And rodata section is marked readonly in mark_rodata_ro. 637 */ 638 if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va)) 639 return PAGE_KERNEL_READ; 640 641 return PAGE_KERNEL; 642 } 643 644 void mark_rodata_ro(void) 645 { 646 set_kernel_memory(__start_rodata, _data, set_memory_ro); 647 if (IS_ENABLED(CONFIG_64BIT)) 648 set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data), 649 set_memory_ro); 650 651 debug_checkwx(); 652 } 653 #else 654 static __init pgprot_t pgprot_from_va(uintptr_t va) 655 { 656 if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va)) 657 return PAGE_KERNEL; 658 659 return PAGE_KERNEL_EXEC; 660 } 661 #endif /* CONFIG_STRICT_KERNEL_RWX */ 662 663 #ifdef CONFIG_64BIT 664 static void __init disable_pgtable_l5(void) 665 { 666 pgtable_l5_enabled = false; 667 kernel_map.page_offset = PAGE_OFFSET_L4; 668 satp_mode = SATP_MODE_48; 669 } 670 671 static void __init disable_pgtable_l4(void) 672 { 673 pgtable_l4_enabled = false; 674 kernel_map.page_offset = PAGE_OFFSET_L3; 675 satp_mode = SATP_MODE_39; 676 } 677 678 /* 679 * There is a simple way to determine if 4-level is supported by the 680 * underlying hardware: establish 1:1 mapping in 4-level page table mode 681 * then read SATP to see if the configuration was taken into account 682 * meaning sv48 is supported. 683 */ 684 static __init void set_satp_mode(void) 685 { 686 u64 identity_satp, hw_satp; 687 uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK; 688 bool check_l4 = false; 689 690 create_p4d_mapping(early_p4d, 691 set_satp_mode_pmd, (uintptr_t)early_pud, 692 P4D_SIZE, PAGE_TABLE); 693 create_pud_mapping(early_pud, 694 set_satp_mode_pmd, (uintptr_t)early_pmd, 695 PUD_SIZE, PAGE_TABLE); 696 /* Handle the case where set_satp_mode straddles 2 PMDs */ 697 create_pmd_mapping(early_pmd, 698 set_satp_mode_pmd, set_satp_mode_pmd, 699 PMD_SIZE, PAGE_KERNEL_EXEC); 700 create_pmd_mapping(early_pmd, 701 set_satp_mode_pmd + PMD_SIZE, 702 set_satp_mode_pmd + PMD_SIZE, 703 PMD_SIZE, PAGE_KERNEL_EXEC); 704 retry: 705 create_pgd_mapping(early_pg_dir, 706 set_satp_mode_pmd, 707 check_l4 ? (uintptr_t)early_pud : (uintptr_t)early_p4d, 708 PGDIR_SIZE, PAGE_TABLE); 709 710 identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode; 711 712 local_flush_tlb_all(); 713 csr_write(CSR_SATP, identity_satp); 714 hw_satp = csr_swap(CSR_SATP, 0ULL); 715 local_flush_tlb_all(); 716 717 if (hw_satp != identity_satp) { 718 if (!check_l4) { 719 disable_pgtable_l5(); 720 check_l4 = true; 721 memset(early_pg_dir, 0, PAGE_SIZE); 722 goto retry; 723 } 724 disable_pgtable_l4(); 725 } 726 727 memset(early_pg_dir, 0, PAGE_SIZE); 728 memset(early_p4d, 0, PAGE_SIZE); 729 memset(early_pud, 0, PAGE_SIZE); 730 memset(early_pmd, 0, PAGE_SIZE); 731 } 732 #endif 733 734 /* 735 * setup_vm() is called from head.S with MMU-off. 736 * 737 * Following requirements should be honoured for setup_vm() to work 738 * correctly: 739 * 1) It should use PC-relative addressing for accessing kernel symbols. 740 * To achieve this we always use GCC cmodel=medany. 741 * 2) The compiler instrumentation for FTRACE will not work for setup_vm() 742 * so disable compiler instrumentation when FTRACE is enabled. 743 * 744 * Currently, the above requirements are honoured by using custom CFLAGS 745 * for init.o in mm/Makefile. 746 */ 747 748 #ifndef __riscv_cmodel_medany 749 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing." 750 #endif 751 752 #ifdef CONFIG_XIP_KERNEL 753 static void __init create_kernel_page_table(pgd_t *pgdir, 754 __always_unused bool early) 755 { 756 uintptr_t va, end_va; 757 758 /* Map the flash resident part */ 759 end_va = kernel_map.virt_addr + kernel_map.xiprom_sz; 760 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE) 761 create_pgd_mapping(pgdir, va, 762 kernel_map.xiprom + (va - kernel_map.virt_addr), 763 PMD_SIZE, PAGE_KERNEL_EXEC); 764 765 /* Map the data in RAM */ 766 end_va = kernel_map.virt_addr + XIP_OFFSET + kernel_map.size; 767 for (va = kernel_map.virt_addr + XIP_OFFSET; va < end_va; va += PMD_SIZE) 768 create_pgd_mapping(pgdir, va, 769 kernel_map.phys_addr + (va - (kernel_map.virt_addr + XIP_OFFSET)), 770 PMD_SIZE, PAGE_KERNEL); 771 } 772 #else 773 static void __init create_kernel_page_table(pgd_t *pgdir, bool early) 774 { 775 uintptr_t va, end_va; 776 777 end_va = kernel_map.virt_addr + kernel_map.size; 778 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE) 779 create_pgd_mapping(pgdir, va, 780 kernel_map.phys_addr + (va - kernel_map.virt_addr), 781 PMD_SIZE, 782 early ? 783 PAGE_KERNEL_EXEC : pgprot_from_va(va)); 784 } 785 #endif 786 787 /* 788 * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel, 789 * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR 790 * entry. 791 */ 792 static void __init create_fdt_early_page_table(pgd_t *pgdir, uintptr_t dtb_pa) 793 { 794 #ifndef CONFIG_BUILTIN_DTB 795 uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1); 796 797 create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA, 798 IS_ENABLED(CONFIG_64BIT) ? early_dtb_pgd_next : pa, 799 PGDIR_SIZE, 800 IS_ENABLED(CONFIG_64BIT) ? PAGE_TABLE : PAGE_KERNEL); 801 802 if (pgtable_l5_enabled) 803 create_p4d_mapping(early_dtb_p4d, DTB_EARLY_BASE_VA, 804 (uintptr_t)early_dtb_pud, P4D_SIZE, PAGE_TABLE); 805 806 if (pgtable_l4_enabled) 807 create_pud_mapping(early_dtb_pud, DTB_EARLY_BASE_VA, 808 (uintptr_t)early_dtb_pmd, PUD_SIZE, PAGE_TABLE); 809 810 if (IS_ENABLED(CONFIG_64BIT)) { 811 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA, 812 pa, PMD_SIZE, PAGE_KERNEL); 813 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE, 814 pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL); 815 } 816 817 dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1)); 818 #else 819 /* 820 * For 64-bit kernel, __va can't be used since it would return a linear 821 * mapping address whereas dtb_early_va will be used before 822 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the 823 * kernel is mapped in the linear mapping, that makes no difference. 824 */ 825 dtb_early_va = kernel_mapping_pa_to_va(XIP_FIXUP(dtb_pa)); 826 #endif 827 828 dtb_early_pa = dtb_pa; 829 } 830 831 /* 832 * MMU is not enabled, the page tables are allocated directly using 833 * early_pmd/pud/p4d and the address returned is the physical one. 834 */ 835 void __init pt_ops_set_early(void) 836 { 837 pt_ops.alloc_pte = alloc_pte_early; 838 pt_ops.get_pte_virt = get_pte_virt_early; 839 #ifndef __PAGETABLE_PMD_FOLDED 840 pt_ops.alloc_pmd = alloc_pmd_early; 841 pt_ops.get_pmd_virt = get_pmd_virt_early; 842 pt_ops.alloc_pud = alloc_pud_early; 843 pt_ops.get_pud_virt = get_pud_virt_early; 844 pt_ops.alloc_p4d = alloc_p4d_early; 845 pt_ops.get_p4d_virt = get_p4d_virt_early; 846 #endif 847 } 848 849 /* 850 * MMU is enabled but page table setup is not complete yet. 851 * fixmap page table alloc functions must be used as a means to temporarily 852 * map the allocated physical pages since the linear mapping does not exist yet. 853 * 854 * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va, 855 * but it will be used as described above. 856 */ 857 void __init pt_ops_set_fixmap(void) 858 { 859 pt_ops.alloc_pte = kernel_mapping_pa_to_va((uintptr_t)alloc_pte_fixmap); 860 pt_ops.get_pte_virt = kernel_mapping_pa_to_va((uintptr_t)get_pte_virt_fixmap); 861 #ifndef __PAGETABLE_PMD_FOLDED 862 pt_ops.alloc_pmd = kernel_mapping_pa_to_va((uintptr_t)alloc_pmd_fixmap); 863 pt_ops.get_pmd_virt = kernel_mapping_pa_to_va((uintptr_t)get_pmd_virt_fixmap); 864 pt_ops.alloc_pud = kernel_mapping_pa_to_va((uintptr_t)alloc_pud_fixmap); 865 pt_ops.get_pud_virt = kernel_mapping_pa_to_va((uintptr_t)get_pud_virt_fixmap); 866 pt_ops.alloc_p4d = kernel_mapping_pa_to_va((uintptr_t)alloc_p4d_fixmap); 867 pt_ops.get_p4d_virt = kernel_mapping_pa_to_va((uintptr_t)get_p4d_virt_fixmap); 868 #endif 869 } 870 871 /* 872 * MMU is enabled and page table setup is complete, so from now, we can use 873 * generic page allocation functions to setup page table. 874 */ 875 void __init pt_ops_set_late(void) 876 { 877 pt_ops.alloc_pte = alloc_pte_late; 878 pt_ops.get_pte_virt = get_pte_virt_late; 879 #ifndef __PAGETABLE_PMD_FOLDED 880 pt_ops.alloc_pmd = alloc_pmd_late; 881 pt_ops.get_pmd_virt = get_pmd_virt_late; 882 pt_ops.alloc_pud = alloc_pud_late; 883 pt_ops.get_pud_virt = get_pud_virt_late; 884 pt_ops.alloc_p4d = alloc_p4d_late; 885 pt_ops.get_p4d_virt = get_p4d_virt_late; 886 #endif 887 } 888 889 asmlinkage void __init setup_vm(uintptr_t dtb_pa) 890 { 891 pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd; 892 893 kernel_map.virt_addr = KERNEL_LINK_ADDR; 894 kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL); 895 896 #ifdef CONFIG_XIP_KERNEL 897 kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR; 898 kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom); 899 900 phys_ram_base = CONFIG_PHYS_RAM_BASE; 901 kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE; 902 kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_sdata); 903 904 kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom; 905 #else 906 kernel_map.phys_addr = (uintptr_t)(&_start); 907 kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr; 908 #endif 909 910 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL) 911 set_satp_mode(); 912 #endif 913 914 kernel_map.va_pa_offset = PAGE_OFFSET - kernel_map.phys_addr; 915 kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; 916 917 riscv_pfn_base = PFN_DOWN(kernel_map.phys_addr); 918 919 /* 920 * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit 921 * kernel, whereas for 64-bit kernel, the end of the virtual address 922 * space is occupied by the modules/BPF/kernel mappings which reduces 923 * the available size of the linear mapping. 924 */ 925 memory_limit = KERN_VIRT_SIZE - (IS_ENABLED(CONFIG_64BIT) ? SZ_4G : 0); 926 927 /* Sanity check alignment and size */ 928 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0); 929 BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0); 930 931 #ifdef CONFIG_64BIT 932 /* 933 * The last 4K bytes of the addressable memory can not be mapped because 934 * of IS_ERR_VALUE macro. 935 */ 936 BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K); 937 #endif 938 939 pt_ops_set_early(); 940 941 /* Setup early PGD for fixmap */ 942 create_pgd_mapping(early_pg_dir, FIXADDR_START, 943 fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE); 944 945 #ifndef __PAGETABLE_PMD_FOLDED 946 /* Setup fixmap P4D and PUD */ 947 if (pgtable_l5_enabled) 948 create_p4d_mapping(fixmap_p4d, FIXADDR_START, 949 (uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE); 950 /* Setup fixmap PUD and PMD */ 951 if (pgtable_l4_enabled) 952 create_pud_mapping(fixmap_pud, FIXADDR_START, 953 (uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE); 954 create_pmd_mapping(fixmap_pmd, FIXADDR_START, 955 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE); 956 /* Setup trampoline PGD and PMD */ 957 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr, 958 trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE); 959 if (pgtable_l5_enabled) 960 create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr, 961 (uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE); 962 if (pgtable_l4_enabled) 963 create_pud_mapping(trampoline_pud, kernel_map.virt_addr, 964 (uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE); 965 #ifdef CONFIG_XIP_KERNEL 966 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr, 967 kernel_map.xiprom, PMD_SIZE, PAGE_KERNEL_EXEC); 968 #else 969 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr, 970 kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC); 971 #endif 972 #else 973 /* Setup trampoline PGD */ 974 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr, 975 kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC); 976 #endif 977 978 /* 979 * Setup early PGD covering entire kernel which will allow 980 * us to reach paging_init(). We map all memory banks later 981 * in setup_vm_final() below. 982 */ 983 create_kernel_page_table(early_pg_dir, true); 984 985 /* Setup early mapping for FDT early scan */ 986 create_fdt_early_page_table(early_pg_dir, dtb_pa); 987 988 /* 989 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap 990 * range can not span multiple pmds. 991 */ 992 BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT) 993 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT)); 994 995 #ifndef __PAGETABLE_PMD_FOLDED 996 /* 997 * Early ioremap fixmap is already created as it lies within first 2MB 998 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END 999 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn 1000 * the user if not. 1001 */ 1002 fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))]; 1003 fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))]; 1004 if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) { 1005 WARN_ON(1); 1006 pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n", 1007 pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd)); 1008 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n", 1009 fix_to_virt(FIX_BTMAP_BEGIN)); 1010 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n", 1011 fix_to_virt(FIX_BTMAP_END)); 1012 1013 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END); 1014 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN); 1015 } 1016 #endif 1017 1018 pt_ops_set_fixmap(); 1019 } 1020 1021 static void __init setup_vm_final(void) 1022 { 1023 uintptr_t va, map_size; 1024 phys_addr_t pa, start, end; 1025 u64 i; 1026 1027 /* Setup swapper PGD for fixmap */ 1028 create_pgd_mapping(swapper_pg_dir, FIXADDR_START, 1029 __pa_symbol(fixmap_pgd_next), 1030 PGDIR_SIZE, PAGE_TABLE); 1031 1032 /* Map all memory banks in the linear mapping */ 1033 for_each_mem_range(i, &start, &end) { 1034 if (start >= end) 1035 break; 1036 if (start <= __pa(PAGE_OFFSET) && 1037 __pa(PAGE_OFFSET) < end) 1038 start = __pa(PAGE_OFFSET); 1039 if (end >= __pa(PAGE_OFFSET) + memory_limit) 1040 end = __pa(PAGE_OFFSET) + memory_limit; 1041 1042 map_size = best_map_size(start, end - start); 1043 for (pa = start; pa < end; pa += map_size) { 1044 va = (uintptr_t)__va(pa); 1045 1046 create_pgd_mapping(swapper_pg_dir, va, pa, map_size, 1047 pgprot_from_va(va)); 1048 } 1049 } 1050 1051 /* Map the kernel */ 1052 if (IS_ENABLED(CONFIG_64BIT)) 1053 create_kernel_page_table(swapper_pg_dir, false); 1054 1055 #ifdef CONFIG_KASAN 1056 kasan_swapper_init(); 1057 #endif 1058 1059 /* Clear fixmap PTE and PMD mappings */ 1060 clear_fixmap(FIX_PTE); 1061 clear_fixmap(FIX_PMD); 1062 clear_fixmap(FIX_PUD); 1063 clear_fixmap(FIX_P4D); 1064 1065 /* Move to swapper page table */ 1066 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode); 1067 local_flush_tlb_all(); 1068 1069 pt_ops_set_late(); 1070 } 1071 #else 1072 asmlinkage void __init setup_vm(uintptr_t dtb_pa) 1073 { 1074 dtb_early_va = (void *)dtb_pa; 1075 dtb_early_pa = dtb_pa; 1076 } 1077 1078 static inline void setup_vm_final(void) 1079 { 1080 } 1081 #endif /* CONFIG_MMU */ 1082 1083 /* 1084 * reserve_crashkernel() - reserves memory for crash kernel 1085 * 1086 * This function reserves memory area given in "crashkernel=" kernel command 1087 * line parameter. The memory reserved is used by dump capture kernel when 1088 * primary kernel is crashing. 1089 */ 1090 static void __init reserve_crashkernel(void) 1091 { 1092 unsigned long long crash_base = 0; 1093 unsigned long long crash_size = 0; 1094 unsigned long search_start = memblock_start_of_DRAM(); 1095 unsigned long search_end = memblock_end_of_DRAM(); 1096 1097 int ret = 0; 1098 1099 if (!IS_ENABLED(CONFIG_KEXEC_CORE)) 1100 return; 1101 /* 1102 * Don't reserve a region for a crash kernel on a crash kernel 1103 * since it doesn't make much sense and we have limited memory 1104 * resources. 1105 */ 1106 if (is_kdump_kernel()) { 1107 pr_info("crashkernel: ignoring reservation request\n"); 1108 return; 1109 } 1110 1111 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 1112 &crash_size, &crash_base); 1113 if (ret || !crash_size) 1114 return; 1115 1116 crash_size = PAGE_ALIGN(crash_size); 1117 1118 if (crash_base) { 1119 search_start = crash_base; 1120 search_end = crash_base + crash_size; 1121 } 1122 1123 /* 1124 * Current riscv boot protocol requires 2MB alignment for 1125 * RV64 and 4MB alignment for RV32 (hugepage size) 1126 * 1127 * Try to alloc from 32bit addressible physical memory so that 1128 * swiotlb can work on the crash kernel. 1129 */ 1130 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE, 1131 search_start, 1132 min(search_end, (unsigned long) SZ_4G)); 1133 if (crash_base == 0) { 1134 /* Try again without restricting region to 32bit addressible memory */ 1135 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE, 1136 search_start, search_end); 1137 if (crash_base == 0) { 1138 pr_warn("crashkernel: couldn't allocate %lldKB\n", 1139 crash_size >> 10); 1140 return; 1141 } 1142 } 1143 1144 pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n", 1145 crash_base, crash_base + crash_size, crash_size >> 20); 1146 1147 crashk_res.start = crash_base; 1148 crashk_res.end = crash_base + crash_size - 1; 1149 } 1150 1151 void __init paging_init(void) 1152 { 1153 setup_bootmem(); 1154 setup_vm_final(); 1155 } 1156 1157 void __init misc_mem_init(void) 1158 { 1159 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT); 1160 arch_numa_init(); 1161 sparse_init(); 1162 zone_sizes_init(); 1163 reserve_crashkernel(); 1164 memblock_dump_all(); 1165 } 1166 1167 #ifdef CONFIG_SPARSEMEM_VMEMMAP 1168 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, 1169 struct vmem_altmap *altmap) 1170 { 1171 return vmemmap_populate_basepages(start, end, node, NULL); 1172 } 1173 #endif 1174