cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

init.c (36091B)


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