1 // SPDX-License-Identifier: GPL-2.0-only
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>
9 #include <linux/init.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>
24 #include <asm/fixmap.h>
25 #include <asm/tlbflush.h>
26 #include <asm/sections.h>
29 #include <asm/ptdump.h>
32 #include "../kernel/head.h"
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))
41 u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39;
43 u64 satp_mode __ro_after_init = SATP_MODE_32;
45 EXPORT_SYMBOL(satp_mode);
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);
52 phys_addr_t phys_ram_base __ro_after_init;
53 EXPORT_SYMBOL(phys_ram_base);
55 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
57 EXPORT_SYMBOL(empty_zero_page);
60 #define DTB_EARLY_BASE_VA (ADDRESS_SPACE_END - (PTRS_PER_PGD / 2 * PGDIR_SIZE) + 1)
61 void *_dtb_early_va __initdata;
62 uintptr_t _dtb_early_pa __initdata;
64 static phys_addr_t dma32_phys_limit __initdata;
66 static void __init zone_sizes_init(void)
68 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
70 #ifdef CONFIG_ZONE_DMA32
71 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
73 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
75 free_area_init(max_zone_pfns);
78 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
80 #define LOG2_SZ_1K ilog2(SZ_1K)
81 #define LOG2_SZ_1M ilog2(SZ_1M)
82 #define LOG2_SZ_1G ilog2(SZ_1G)
83 #define LOG2_SZ_1T ilog2(SZ_1T)
85 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
87 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
88 (((t) - (b)) >> LOG2_SZ_1K));
91 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
93 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
94 (((t) - (b)) >> LOG2_SZ_1M));
97 static inline void print_mlg(char *name, unsigned long b, unsigned long t)
99 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld GB)\n", name, b, t,
100 (((t) - (b)) >> LOG2_SZ_1G));
104 static inline void print_mlt(char *name, unsigned long b, unsigned long t)
106 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld TB)\n", name, b, t,
107 (((t) - (b)) >> LOG2_SZ_1T));
110 #define print_mlt(n, b, t) do {} while (0)
113 static inline void print_ml(char *name, unsigned long b, unsigned long t)
115 unsigned long diff = t - b;
117 if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
118 print_mlt(name, b, t);
119 else if ((diff >> LOG2_SZ_1G) >= 10)
120 print_mlg(name, b, t);
121 else if ((diff >> LOG2_SZ_1M) >= 10)
122 print_mlm(name, b, t);
124 print_mlk(name, b, t);
127 static void __init print_vm_layout(void)
129 pr_notice("Virtual kernel memory layout:\n");
130 print_ml("fixmap", (unsigned long)FIXADDR_START,
131 (unsigned long)FIXADDR_TOP);
132 print_ml("pci io", (unsigned long)PCI_IO_START,
133 (unsigned long)PCI_IO_END);
134 print_ml("vmemmap", (unsigned long)VMEMMAP_START,
135 (unsigned long)VMEMMAP_END);
136 print_ml("vmalloc", (unsigned long)VMALLOC_START,
137 (unsigned long)VMALLOC_END);
139 print_ml("modules", (unsigned long)MODULES_VADDR,
140 (unsigned long)MODULES_END);
142 print_ml("lowmem", (unsigned long)PAGE_OFFSET,
143 (unsigned long)high_memory);
144 if (IS_ENABLED(CONFIG_64BIT)) {
146 print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
149 print_ml("kernel", (unsigned long)KERNEL_LINK_ADDR,
150 (unsigned long)ADDRESS_SPACE_END);
154 static void print_vm_layout(void) { }
155 #endif /* CONFIG_DEBUG_VM */
157 void __init mem_init(void)
159 #ifdef CONFIG_FLATMEM
161 #endif /* CONFIG_FLATMEM */
163 swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE);
169 /* Limit the memory size via mem. */
170 static phys_addr_t memory_limit;
172 static int __init early_mem(char *p)
179 size = memparse(p, &p) & PAGE_MASK;
180 memory_limit = min_t(u64, size, memory_limit);
182 pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20);
186 early_param("mem", early_mem);
188 static void __init setup_bootmem(void)
190 phys_addr_t vmlinux_end = __pa_symbol(&_end);
191 phys_addr_t max_mapped_addr;
192 phys_addr_t phys_ram_end, vmlinux_start;
194 if (IS_ENABLED(CONFIG_XIP_KERNEL))
195 vmlinux_start = __pa_symbol(&_sdata);
197 vmlinux_start = __pa_symbol(&_start);
199 memblock_enforce_memory_limit(memory_limit);
202 * Make sure we align the reservation on PMD_SIZE since we will
203 * map the kernel in the linear mapping as read-only: we do not want
204 * any allocation to happen between _end and the next pmd aligned page.
206 if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
207 vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
209 * Reserve from the start of the kernel to the end of the kernel
211 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
213 phys_ram_end = memblock_end_of_DRAM();
214 if (!IS_ENABLED(CONFIG_XIP_KERNEL))
215 phys_ram_base = memblock_start_of_DRAM();
218 * In 64-bit, any use of __va/__pa before this point is wrong as we
219 * did not know the start of DRAM before.
221 if (IS_ENABLED(CONFIG_64BIT))
222 kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
225 * memblock allocator is not aware of the fact that last 4K bytes of
226 * the addressable memory can not be mapped because of IS_ERR_VALUE
227 * macro. Make sure that last 4k bytes are not usable by memblock
228 * if end of dram is equal to maximum addressable memory. For 64-bit
229 * kernel, this problem can't happen here as the end of the virtual
230 * address space is occupied by the kernel mapping then this check must
231 * be done as soon as the kernel mapping base address is determined.
233 if (!IS_ENABLED(CONFIG_64BIT)) {
234 max_mapped_addr = __pa(~(ulong)0);
235 if (max_mapped_addr == (phys_ram_end - 1))
236 memblock_set_current_limit(max_mapped_addr - 4096);
239 min_low_pfn = PFN_UP(phys_ram_base);
240 max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
241 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
243 dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
244 set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
246 reserve_initrd_mem();
248 * If DTB is built in, no need to reserve its memblock.
249 * Otherwise, do reserve it but avoid using
250 * early_init_fdt_reserve_self() since __pa() does
251 * not work for DTB pointers that are fixmap addresses
253 if (!IS_ENABLED(CONFIG_BUILTIN_DTB)) {
255 * In case the DTB is not located in a memory region we won't
256 * be able to locate it later on via the linear mapping and
257 * get a segfault when accessing it via __va(dtb_early_pa).
258 * To avoid this situation copy DTB to a memory region.
259 * Note that memblock_phys_alloc will also reserve DTB region.
261 if (!memblock_is_memory(dtb_early_pa)) {
262 size_t fdt_size = fdt_totalsize(dtb_early_va);
263 phys_addr_t new_dtb_early_pa = memblock_phys_alloc(fdt_size, PAGE_SIZE);
264 void *new_dtb_early_va = early_memremap(new_dtb_early_pa, fdt_size);
266 memcpy(new_dtb_early_va, dtb_early_va, fdt_size);
267 early_memunmap(new_dtb_early_va, fdt_size);
268 _dtb_early_pa = new_dtb_early_pa;
270 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
273 dma_contiguous_reserve(dma32_phys_limit);
274 if (IS_ENABLED(CONFIG_64BIT))
275 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
276 memblock_allow_resize();
280 struct pt_alloc_ops pt_ops __initdata;
282 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
283 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
284 static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
286 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
287 static p4d_t __maybe_unused early_dtb_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
288 static pud_t __maybe_unused early_dtb_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
289 static pmd_t __maybe_unused early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
291 #ifdef CONFIG_XIP_KERNEL
292 #define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
293 #define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
294 #define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte))
295 #define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir))
296 #endif /* CONFIG_XIP_KERNEL */
298 static const pgprot_t protection_map[16] = {
299 [VM_NONE] = PAGE_NONE,
300 [VM_READ] = PAGE_READ,
301 [VM_WRITE] = PAGE_COPY,
302 [VM_WRITE | VM_READ] = PAGE_COPY,
303 [VM_EXEC] = PAGE_EXEC,
304 [VM_EXEC | VM_READ] = PAGE_READ_EXEC,
305 [VM_EXEC | VM_WRITE] = PAGE_COPY_EXEC,
306 [VM_EXEC | VM_WRITE | VM_READ] = PAGE_COPY_READ_EXEC,
307 [VM_SHARED] = PAGE_NONE,
308 [VM_SHARED | VM_READ] = PAGE_READ,
309 [VM_SHARED | VM_WRITE] = PAGE_SHARED,
310 [VM_SHARED | VM_WRITE | VM_READ] = PAGE_SHARED,
311 [VM_SHARED | VM_EXEC] = PAGE_EXEC,
312 [VM_SHARED | VM_EXEC | VM_READ] = PAGE_READ_EXEC,
313 [VM_SHARED | VM_EXEC | VM_WRITE] = PAGE_SHARED_EXEC,
314 [VM_SHARED | VM_EXEC | VM_WRITE | VM_READ] = PAGE_SHARED_EXEC
316 DECLARE_VM_GET_PAGE_PROT
318 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
320 unsigned long addr = __fix_to_virt(idx);
323 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
325 ptep = &fixmap_pte[pte_index(addr)];
327 if (pgprot_val(prot))
328 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
330 pte_clear(&init_mm, addr, ptep);
331 local_flush_tlb_page(addr);
334 static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
336 return (pte_t *)((uintptr_t)pa);
339 static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
341 clear_fixmap(FIX_PTE);
342 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
345 static inline pte_t *__init get_pte_virt_late(phys_addr_t pa)
347 return (pte_t *) __va(pa);
350 static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
353 * We only create PMD or PGD early mappings so we
354 * should never reach here with MMU disabled.
359 static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
361 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
364 static phys_addr_t __init alloc_pte_late(uintptr_t va)
368 vaddr = __get_free_page(GFP_KERNEL);
369 BUG_ON(!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr)));
374 static void __init create_pte_mapping(pte_t *ptep,
375 uintptr_t va, phys_addr_t pa,
376 phys_addr_t sz, pgprot_t prot)
378 uintptr_t pte_idx = pte_index(va);
380 BUG_ON(sz != PAGE_SIZE);
382 if (pte_none(ptep[pte_idx]))
383 ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
386 #ifndef __PAGETABLE_PMD_FOLDED
388 static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
389 static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
390 static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
392 #ifdef CONFIG_XIP_KERNEL
393 #define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
394 #define fixmap_pmd ((pmd_t *)XIP_FIXUP(fixmap_pmd))
395 #define early_pmd ((pmd_t *)XIP_FIXUP(early_pmd))
396 #endif /* CONFIG_XIP_KERNEL */
398 static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss;
399 static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss;
400 static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
402 #ifdef CONFIG_XIP_KERNEL
403 #define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d))
404 #define fixmap_p4d ((p4d_t *)XIP_FIXUP(fixmap_p4d))
405 #define early_p4d ((p4d_t *)XIP_FIXUP(early_p4d))
406 #endif /* CONFIG_XIP_KERNEL */
408 static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss;
409 static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss;
410 static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
412 #ifdef CONFIG_XIP_KERNEL
413 #define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud))
414 #define fixmap_pud ((pud_t *)XIP_FIXUP(fixmap_pud))
415 #define early_pud ((pud_t *)XIP_FIXUP(early_pud))
416 #endif /* CONFIG_XIP_KERNEL */
418 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
420 /* Before MMU is enabled */
421 return (pmd_t *)((uintptr_t)pa);
424 static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
426 clear_fixmap(FIX_PMD);
427 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
430 static pmd_t *__init get_pmd_virt_late(phys_addr_t pa)
432 return (pmd_t *) __va(pa);
435 static phys_addr_t __init alloc_pmd_early(uintptr_t va)
437 BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT);
439 return (uintptr_t)early_pmd;
442 static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
444 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
447 static phys_addr_t __init alloc_pmd_late(uintptr_t va)
451 vaddr = __get_free_page(GFP_KERNEL);
452 BUG_ON(!vaddr || !pgtable_pmd_page_ctor(virt_to_page(vaddr)));
457 static void __init create_pmd_mapping(pmd_t *pmdp,
458 uintptr_t va, phys_addr_t pa,
459 phys_addr_t sz, pgprot_t prot)
462 phys_addr_t pte_phys;
463 uintptr_t pmd_idx = pmd_index(va);
465 if (sz == PMD_SIZE) {
466 if (pmd_none(pmdp[pmd_idx]))
467 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
471 if (pmd_none(pmdp[pmd_idx])) {
472 pte_phys = pt_ops.alloc_pte(va);
473 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
474 ptep = pt_ops.get_pte_virt(pte_phys);
475 memset(ptep, 0, PAGE_SIZE);
477 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
478 ptep = pt_ops.get_pte_virt(pte_phys);
481 create_pte_mapping(ptep, va, pa, sz, prot);
484 static pud_t *__init get_pud_virt_early(phys_addr_t pa)
486 return (pud_t *)((uintptr_t)pa);
489 static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa)
491 clear_fixmap(FIX_PUD);
492 return (pud_t *)set_fixmap_offset(FIX_PUD, pa);
495 static pud_t *__init get_pud_virt_late(phys_addr_t pa)
497 return (pud_t *)__va(pa);
500 static phys_addr_t __init alloc_pud_early(uintptr_t va)
502 /* Only one PUD is available for early mapping */
503 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
505 return (uintptr_t)early_pud;
508 static phys_addr_t __init alloc_pud_fixmap(uintptr_t va)
510 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
513 static phys_addr_t alloc_pud_late(uintptr_t va)
517 vaddr = __get_free_page(GFP_KERNEL);
522 static p4d_t *__init get_p4d_virt_early(phys_addr_t pa)
524 return (p4d_t *)((uintptr_t)pa);
527 static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa)
529 clear_fixmap(FIX_P4D);
530 return (p4d_t *)set_fixmap_offset(FIX_P4D, pa);
533 static p4d_t *__init get_p4d_virt_late(phys_addr_t pa)
535 return (p4d_t *)__va(pa);
538 static phys_addr_t __init alloc_p4d_early(uintptr_t va)
540 /* Only one P4D is available for early mapping */
541 BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
543 return (uintptr_t)early_p4d;
546 static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va)
548 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
551 static phys_addr_t alloc_p4d_late(uintptr_t va)
555 vaddr = __get_free_page(GFP_KERNEL);
560 static void __init create_pud_mapping(pud_t *pudp,
561 uintptr_t va, phys_addr_t pa,
562 phys_addr_t sz, pgprot_t prot)
565 phys_addr_t next_phys;
566 uintptr_t pud_index = pud_index(va);
568 if (sz == PUD_SIZE) {
569 if (pud_val(pudp[pud_index]) == 0)
570 pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot);
574 if (pud_val(pudp[pud_index]) == 0) {
575 next_phys = pt_ops.alloc_pmd(va);
576 pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE);
577 nextp = pt_ops.get_pmd_virt(next_phys);
578 memset(nextp, 0, PAGE_SIZE);
580 next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index]));
581 nextp = pt_ops.get_pmd_virt(next_phys);
584 create_pmd_mapping(nextp, va, pa, sz, prot);
587 static void __init create_p4d_mapping(p4d_t *p4dp,
588 uintptr_t va, phys_addr_t pa,
589 phys_addr_t sz, pgprot_t prot)
592 phys_addr_t next_phys;
593 uintptr_t p4d_index = p4d_index(va);
595 if (sz == P4D_SIZE) {
596 if (p4d_val(p4dp[p4d_index]) == 0)
597 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot);
601 if (p4d_val(p4dp[p4d_index]) == 0) {
602 next_phys = pt_ops.alloc_pud(va);
603 p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE);
604 nextp = pt_ops.get_pud_virt(next_phys);
605 memset(nextp, 0, PAGE_SIZE);
607 next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index]));
608 nextp = pt_ops.get_pud_virt(next_phys);
611 create_pud_mapping(nextp, va, pa, sz, prot);
614 #define pgd_next_t p4d_t
615 #define alloc_pgd_next(__va) (pgtable_l5_enabled ? \
616 pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ? \
617 pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va)))
618 #define get_pgd_next_virt(__pa) (pgtable_l5_enabled ? \
619 pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ? \
620 pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa)))
621 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
622 (pgtable_l5_enabled ? \
623 create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \
624 (pgtable_l4_enabled ? \
625 create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) : \
626 create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot)))
627 #define fixmap_pgd_next (pgtable_l5_enabled ? \
628 (uintptr_t)fixmap_p4d : (pgtable_l4_enabled ? \
629 (uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd))
630 #define trampoline_pgd_next (pgtable_l5_enabled ? \
631 (uintptr_t)trampoline_p4d : (pgtable_l4_enabled ? \
632 (uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
633 #define early_dtb_pgd_next (pgtable_l5_enabled ? \
634 (uintptr_t)early_dtb_p4d : (pgtable_l4_enabled ? \
635 (uintptr_t)early_dtb_pud : (uintptr_t)early_dtb_pmd))
637 #define pgd_next_t pte_t
638 #define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
639 #define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
640 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
641 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
642 #define fixmap_pgd_next ((uintptr_t)fixmap_pte)
643 #define early_dtb_pgd_next ((uintptr_t)early_dtb_pmd)
644 #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
645 #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
646 #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
647 #endif /* __PAGETABLE_PMD_FOLDED */
649 void __init create_pgd_mapping(pgd_t *pgdp,
650 uintptr_t va, phys_addr_t pa,
651 phys_addr_t sz, pgprot_t prot)
654 phys_addr_t next_phys;
655 uintptr_t pgd_idx = pgd_index(va);
657 if (sz == PGDIR_SIZE) {
658 if (pgd_val(pgdp[pgd_idx]) == 0)
659 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
663 if (pgd_val(pgdp[pgd_idx]) == 0) {
664 next_phys = alloc_pgd_next(va);
665 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
666 nextp = get_pgd_next_virt(next_phys);
667 memset(nextp, 0, PAGE_SIZE);
669 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
670 nextp = get_pgd_next_virt(next_phys);
673 create_pgd_next_mapping(nextp, va, pa, sz, prot);
676 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
678 if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE)
681 if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE)
684 if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE)
687 if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE)
693 #ifdef CONFIG_XIP_KERNEL
694 #define phys_ram_base (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base))
695 extern char _xiprom[], _exiprom[], __data_loc;
697 /* called from head.S with MMU off */
698 asmlinkage void __init __copy_data(void)
700 void *from = (void *)(&__data_loc);
701 void *to = (void *)CONFIG_PHYS_RAM_BASE;
702 size_t sz = (size_t)((uintptr_t)(&_end) - (uintptr_t)(&_sdata));
704 memcpy(to, from, sz);
708 #ifdef CONFIG_STRICT_KERNEL_RWX
709 static __init pgprot_t pgprot_from_va(uintptr_t va)
711 if (is_va_kernel_text(va))
712 return PAGE_KERNEL_READ_EXEC;
715 * In 64-bit kernel, the kernel mapping is outside the linear mapping so
716 * we must protect its linear mapping alias from being executed and
718 * And rodata section is marked readonly in mark_rodata_ro.
720 if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va))
721 return PAGE_KERNEL_READ;
726 void mark_rodata_ro(void)
728 set_kernel_memory(__start_rodata, _data, set_memory_ro);
729 if (IS_ENABLED(CONFIG_64BIT))
730 set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data),
736 static __init pgprot_t pgprot_from_va(uintptr_t va)
738 if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va))
741 return PAGE_KERNEL_EXEC;
743 #endif /* CONFIG_STRICT_KERNEL_RWX */
745 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
746 static void __init disable_pgtable_l5(void)
748 pgtable_l5_enabled = false;
749 kernel_map.page_offset = PAGE_OFFSET_L4;
750 satp_mode = SATP_MODE_48;
753 static void __init disable_pgtable_l4(void)
755 pgtable_l4_enabled = false;
756 kernel_map.page_offset = PAGE_OFFSET_L3;
757 satp_mode = SATP_MODE_39;
761 * There is a simple way to determine if 4-level is supported by the
762 * underlying hardware: establish 1:1 mapping in 4-level page table mode
763 * then read SATP to see if the configuration was taken into account
764 * meaning sv48 is supported.
766 static __init void set_satp_mode(void)
768 u64 identity_satp, hw_satp;
769 uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
770 bool check_l4 = false;
772 create_p4d_mapping(early_p4d,
773 set_satp_mode_pmd, (uintptr_t)early_pud,
774 P4D_SIZE, PAGE_TABLE);
775 create_pud_mapping(early_pud,
776 set_satp_mode_pmd, (uintptr_t)early_pmd,
777 PUD_SIZE, PAGE_TABLE);
778 /* Handle the case where set_satp_mode straddles 2 PMDs */
779 create_pmd_mapping(early_pmd,
780 set_satp_mode_pmd, set_satp_mode_pmd,
781 PMD_SIZE, PAGE_KERNEL_EXEC);
782 create_pmd_mapping(early_pmd,
783 set_satp_mode_pmd + PMD_SIZE,
784 set_satp_mode_pmd + PMD_SIZE,
785 PMD_SIZE, PAGE_KERNEL_EXEC);
787 create_pgd_mapping(early_pg_dir,
789 check_l4 ? (uintptr_t)early_pud : (uintptr_t)early_p4d,
790 PGDIR_SIZE, PAGE_TABLE);
792 identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
794 local_flush_tlb_all();
795 csr_write(CSR_SATP, identity_satp);
796 hw_satp = csr_swap(CSR_SATP, 0ULL);
797 local_flush_tlb_all();
799 if (hw_satp != identity_satp) {
801 disable_pgtable_l5();
803 memset(early_pg_dir, 0, PAGE_SIZE);
806 disable_pgtable_l4();
809 memset(early_pg_dir, 0, PAGE_SIZE);
810 memset(early_p4d, 0, PAGE_SIZE);
811 memset(early_pud, 0, PAGE_SIZE);
812 memset(early_pmd, 0, PAGE_SIZE);
817 * setup_vm() is called from head.S with MMU-off.
819 * Following requirements should be honoured for setup_vm() to work
821 * 1) It should use PC-relative addressing for accessing kernel symbols.
822 * To achieve this we always use GCC cmodel=medany.
823 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
824 * so disable compiler instrumentation when FTRACE is enabled.
826 * Currently, the above requirements are honoured by using custom CFLAGS
827 * for init.o in mm/Makefile.
830 #ifndef __riscv_cmodel_medany
831 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
834 #ifdef CONFIG_XIP_KERNEL
835 static void __init create_kernel_page_table(pgd_t *pgdir,
836 __always_unused bool early)
838 uintptr_t va, end_va;
840 /* Map the flash resident part */
841 end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;
842 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
843 create_pgd_mapping(pgdir, va,
844 kernel_map.xiprom + (va - kernel_map.virt_addr),
845 PMD_SIZE, PAGE_KERNEL_EXEC);
847 /* Map the data in RAM */
848 end_va = kernel_map.virt_addr + XIP_OFFSET + kernel_map.size;
849 for (va = kernel_map.virt_addr + XIP_OFFSET; va < end_va; va += PMD_SIZE)
850 create_pgd_mapping(pgdir, va,
851 kernel_map.phys_addr + (va - (kernel_map.virt_addr + XIP_OFFSET)),
852 PMD_SIZE, PAGE_KERNEL);
855 static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
857 uintptr_t va, end_va;
859 end_va = kernel_map.virt_addr + kernel_map.size;
860 for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
861 create_pgd_mapping(pgdir, va,
862 kernel_map.phys_addr + (va - kernel_map.virt_addr),
865 PAGE_KERNEL_EXEC : pgprot_from_va(va));
870 * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel,
871 * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
874 static void __init create_fdt_early_page_table(pgd_t *pgdir, uintptr_t dtb_pa)
876 #ifndef CONFIG_BUILTIN_DTB
877 uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
879 create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
880 IS_ENABLED(CONFIG_64BIT) ? early_dtb_pgd_next : pa,
882 IS_ENABLED(CONFIG_64BIT) ? PAGE_TABLE : PAGE_KERNEL);
884 if (pgtable_l5_enabled)
885 create_p4d_mapping(early_dtb_p4d, DTB_EARLY_BASE_VA,
886 (uintptr_t)early_dtb_pud, P4D_SIZE, PAGE_TABLE);
888 if (pgtable_l4_enabled)
889 create_pud_mapping(early_dtb_pud, DTB_EARLY_BASE_VA,
890 (uintptr_t)early_dtb_pmd, PUD_SIZE, PAGE_TABLE);
892 if (IS_ENABLED(CONFIG_64BIT)) {
893 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,
894 pa, PMD_SIZE, PAGE_KERNEL);
895 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,
896 pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
899 dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1));
902 * For 64-bit kernel, __va can't be used since it would return a linear
903 * mapping address whereas dtb_early_va will be used before
904 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the
905 * kernel is mapped in the linear mapping, that makes no difference.
907 dtb_early_va = kernel_mapping_pa_to_va(XIP_FIXUP(dtb_pa));
910 dtb_early_pa = dtb_pa;
914 * MMU is not enabled, the page tables are allocated directly using
915 * early_pmd/pud/p4d and the address returned is the physical one.
917 static void __init pt_ops_set_early(void)
919 pt_ops.alloc_pte = alloc_pte_early;
920 pt_ops.get_pte_virt = get_pte_virt_early;
921 #ifndef __PAGETABLE_PMD_FOLDED
922 pt_ops.alloc_pmd = alloc_pmd_early;
923 pt_ops.get_pmd_virt = get_pmd_virt_early;
924 pt_ops.alloc_pud = alloc_pud_early;
925 pt_ops.get_pud_virt = get_pud_virt_early;
926 pt_ops.alloc_p4d = alloc_p4d_early;
927 pt_ops.get_p4d_virt = get_p4d_virt_early;
932 * MMU is enabled but page table setup is not complete yet.
933 * fixmap page table alloc functions must be used as a means to temporarily
934 * map the allocated physical pages since the linear mapping does not exist yet.
936 * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
937 * but it will be used as described above.
939 static void __init pt_ops_set_fixmap(void)
941 pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap);
942 pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap);
943 #ifndef __PAGETABLE_PMD_FOLDED
944 pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap);
945 pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap);
946 pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap);
947 pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap);
948 pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap);
949 pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap);
954 * MMU is enabled and page table setup is complete, so from now, we can use
955 * generic page allocation functions to setup page table.
957 static void __init pt_ops_set_late(void)
959 pt_ops.alloc_pte = alloc_pte_late;
960 pt_ops.get_pte_virt = get_pte_virt_late;
961 #ifndef __PAGETABLE_PMD_FOLDED
962 pt_ops.alloc_pmd = alloc_pmd_late;
963 pt_ops.get_pmd_virt = get_pmd_virt_late;
964 pt_ops.alloc_pud = alloc_pud_late;
965 pt_ops.get_pud_virt = get_pud_virt_late;
966 pt_ops.alloc_p4d = alloc_p4d_late;
967 pt_ops.get_p4d_virt = get_p4d_virt_late;
971 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
973 pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
975 kernel_map.virt_addr = KERNEL_LINK_ADDR;
976 kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
978 #ifdef CONFIG_XIP_KERNEL
979 kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
980 kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
982 phys_ram_base = CONFIG_PHYS_RAM_BASE;
983 kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
984 kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_sdata);
986 kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom;
988 kernel_map.phys_addr = (uintptr_t)(&_start);
989 kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr;
992 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
997 * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
998 * where we have the system memory layout: this allows us to align
999 * the physical and virtual mappings and then make use of PUD/P4D/PGD
1000 * for the linear mapping. This is only possible because the kernel
1001 * mapping lies outside the linear mapping.
1002 * In 32-bit however, as the kernel resides in the linear mapping,
1003 * setup_vm_final can not change the mapping established here,
1004 * otherwise the same kernel addresses would get mapped to different
1005 * physical addresses (if the start of dram is different from the
1006 * kernel physical address start).
1008 kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
1009 0UL : PAGE_OFFSET - kernel_map.phys_addr;
1010 kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;
1013 * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
1014 * kernel, whereas for 64-bit kernel, the end of the virtual address
1015 * space is occupied by the modules/BPF/kernel mappings which reduces
1016 * the available size of the linear mapping.
1018 memory_limit = KERN_VIRT_SIZE - (IS_ENABLED(CONFIG_64BIT) ? SZ_4G : 0);
1020 /* Sanity check alignment and size */
1021 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
1022 BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);
1026 * The last 4K bytes of the addressable memory can not be mapped because
1027 * of IS_ERR_VALUE macro.
1029 BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
1032 apply_early_boot_alternatives();
1035 /* Setup early PGD for fixmap */
1036 create_pgd_mapping(early_pg_dir, FIXADDR_START,
1037 fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1039 #ifndef __PAGETABLE_PMD_FOLDED
1040 /* Setup fixmap P4D and PUD */
1041 if (pgtable_l5_enabled)
1042 create_p4d_mapping(fixmap_p4d, FIXADDR_START,
1043 (uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE);
1044 /* Setup fixmap PUD and PMD */
1045 if (pgtable_l4_enabled)
1046 create_pud_mapping(fixmap_pud, FIXADDR_START,
1047 (uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE);
1048 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
1049 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
1050 /* Setup trampoline PGD and PMD */
1051 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1052 trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1053 if (pgtable_l5_enabled)
1054 create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr,
1055 (uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE);
1056 if (pgtable_l4_enabled)
1057 create_pud_mapping(trampoline_pud, kernel_map.virt_addr,
1058 (uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE);
1059 #ifdef CONFIG_XIP_KERNEL
1060 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1061 kernel_map.xiprom, PMD_SIZE, PAGE_KERNEL_EXEC);
1063 create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1064 kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC);
1067 /* Setup trampoline PGD */
1068 create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1069 kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC);
1073 * Setup early PGD covering entire kernel which will allow
1074 * us to reach paging_init(). We map all memory banks later
1075 * in setup_vm_final() below.
1077 create_kernel_page_table(early_pg_dir, true);
1079 /* Setup early mapping for FDT early scan */
1080 create_fdt_early_page_table(early_pg_dir, dtb_pa);
1083 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
1084 * range can not span multiple pmds.
1086 BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1087 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1089 #ifndef __PAGETABLE_PMD_FOLDED
1091 * Early ioremap fixmap is already created as it lies within first 2MB
1092 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
1093 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
1096 fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
1097 fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
1098 if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
1100 pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
1101 pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
1102 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1103 fix_to_virt(FIX_BTMAP_BEGIN));
1104 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
1105 fix_to_virt(FIX_BTMAP_END));
1107 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
1108 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
1112 pt_ops_set_fixmap();
1115 static void __init create_linear_mapping_range(phys_addr_t start,
1119 uintptr_t va, map_size;
1121 for (pa = start; pa < end; pa += map_size) {
1122 va = (uintptr_t)__va(pa);
1123 map_size = best_map_size(pa, end - pa);
1125 create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
1126 pgprot_from_va(va));
1130 static void __init create_linear_mapping_page_table(void)
1132 phys_addr_t start, end;
1135 #ifdef CONFIG_STRICT_KERNEL_RWX
1136 phys_addr_t ktext_start = __pa_symbol(_start);
1137 phys_addr_t ktext_size = __init_data_begin - _start;
1138 phys_addr_t krodata_start = __pa_symbol(__start_rodata);
1139 phys_addr_t krodata_size = _data - __start_rodata;
1141 /* Isolate kernel text and rodata so they don't get mapped with a PUD */
1142 memblock_mark_nomap(ktext_start, ktext_size);
1143 memblock_mark_nomap(krodata_start, krodata_size);
1146 /* Map all memory banks in the linear mapping */
1147 for_each_mem_range(i, &start, &end) {
1150 if (start <= __pa(PAGE_OFFSET) &&
1151 __pa(PAGE_OFFSET) < end)
1152 start = __pa(PAGE_OFFSET);
1153 if (end >= __pa(PAGE_OFFSET) + memory_limit)
1154 end = __pa(PAGE_OFFSET) + memory_limit;
1156 create_linear_mapping_range(start, end);
1159 #ifdef CONFIG_STRICT_KERNEL_RWX
1160 create_linear_mapping_range(ktext_start, ktext_start + ktext_size);
1161 create_linear_mapping_range(krodata_start,
1162 krodata_start + krodata_size);
1164 memblock_clear_nomap(ktext_start, ktext_size);
1165 memblock_clear_nomap(krodata_start, krodata_size);
1169 static void __init setup_vm_final(void)
1171 /* Setup swapper PGD for fixmap */
1172 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
1173 __pa_symbol(fixmap_pgd_next),
1174 PGDIR_SIZE, PAGE_TABLE);
1176 /* Map the linear mapping */
1177 create_linear_mapping_page_table();
1179 /* Map the kernel */
1180 if (IS_ENABLED(CONFIG_64BIT))
1181 create_kernel_page_table(swapper_pg_dir, false);
1184 kasan_swapper_init();
1187 /* Clear fixmap PTE and PMD mappings */
1188 clear_fixmap(FIX_PTE);
1189 clear_fixmap(FIX_PMD);
1190 clear_fixmap(FIX_PUD);
1191 clear_fixmap(FIX_P4D);
1193 /* Move to swapper page table */
1194 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
1195 local_flush_tlb_all();
1200 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1202 dtb_early_va = (void *)dtb_pa;
1203 dtb_early_pa = dtb_pa;
1206 static inline void setup_vm_final(void)
1209 #endif /* CONFIG_MMU */
1212 * reserve_crashkernel() - reserves memory for crash kernel
1214 * This function reserves memory area given in "crashkernel=" kernel command
1215 * line parameter. The memory reserved is used by dump capture kernel when
1216 * primary kernel is crashing.
1218 static void __init reserve_crashkernel(void)
1220 unsigned long long crash_base = 0;
1221 unsigned long long crash_size = 0;
1222 unsigned long search_start = memblock_start_of_DRAM();
1223 unsigned long search_end = memblock_end_of_DRAM();
1227 if (!IS_ENABLED(CONFIG_KEXEC_CORE))
1230 * Don't reserve a region for a crash kernel on a crash kernel
1231 * since it doesn't make much sense and we have limited memory
1234 if (is_kdump_kernel()) {
1235 pr_info("crashkernel: ignoring reservation request\n");
1239 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
1240 &crash_size, &crash_base);
1241 if (ret || !crash_size)
1244 crash_size = PAGE_ALIGN(crash_size);
1247 search_start = crash_base;
1248 search_end = crash_base + crash_size;
1252 * Current riscv boot protocol requires 2MB alignment for
1253 * RV64 and 4MB alignment for RV32 (hugepage size)
1255 * Try to alloc from 32bit addressible physical memory so that
1256 * swiotlb can work on the crash kernel.
1258 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1260 min(search_end, (unsigned long) SZ_4G));
1261 if (crash_base == 0) {
1262 /* Try again without restricting region to 32bit addressible memory */
1263 crash_base = memblock_phys_alloc_range(crash_size, PMD_SIZE,
1264 search_start, search_end);
1265 if (crash_base == 0) {
1266 pr_warn("crashkernel: couldn't allocate %lldKB\n",
1272 pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
1273 crash_base, crash_base + crash_size, crash_size >> 20);
1275 crashk_res.start = crash_base;
1276 crashk_res.end = crash_base + crash_size - 1;
1279 void __init paging_init(void)
1285 void __init misc_mem_init(void)
1287 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
1291 reserve_crashkernel();
1292 memblock_dump_all();
1295 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1296 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1297 struct vmem_altmap *altmap)
1299 return vmemmap_populate_basepages(start, end, node, NULL);