xref: /kernel/linux/linux-5.10/arch/riscv/mm/init.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2012 Regents of the University of California
48c2ecf20Sopenharmony_ci * Copyright (C) 2019 Western Digital Corporation or its affiliates.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/init.h>
88c2ecf20Sopenharmony_ci#include <linux/mm.h>
98c2ecf20Sopenharmony_ci#include <linux/memblock.h>
108c2ecf20Sopenharmony_ci#include <linux/initrd.h>
118c2ecf20Sopenharmony_ci#include <linux/swap.h>
128c2ecf20Sopenharmony_ci#include <linux/sizes.h>
138c2ecf20Sopenharmony_ci#include <linux/of_fdt.h>
148c2ecf20Sopenharmony_ci#include <linux/libfdt.h>
158c2ecf20Sopenharmony_ci#include <linux/set_memory.h>
168c2ecf20Sopenharmony_ci#include <linux/dma-map-ops.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/fixmap.h>
198c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
208c2ecf20Sopenharmony_ci#include <asm/sections.h>
218c2ecf20Sopenharmony_ci#include <asm/soc.h>
228c2ecf20Sopenharmony_ci#include <asm/io.h>
238c2ecf20Sopenharmony_ci#include <asm/ptdump.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "../kernel/head.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ciunsigned long kernel_virt_addr = KERNEL_LINK_ADDR;
288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kernel_virt_addr);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciunsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
318c2ecf20Sopenharmony_ci							__page_aligned_bss;
328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(empty_zero_page);
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciextern char _start[];
358c2ecf20Sopenharmony_ci#define DTB_EARLY_BASE_VA      PGDIR_SIZE
368c2ecf20Sopenharmony_civoid *dtb_early_va __initdata;
378c2ecf20Sopenharmony_ciuintptr_t dtb_early_pa __initdata;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct pt_alloc_ops {
408c2ecf20Sopenharmony_ci	pte_t *(*get_pte_virt)(phys_addr_t pa);
418c2ecf20Sopenharmony_ci	phys_addr_t (*alloc_pte)(uintptr_t va);
428c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
438c2ecf20Sopenharmony_ci	pmd_t *(*get_pmd_virt)(phys_addr_t pa);
448c2ecf20Sopenharmony_ci	phys_addr_t (*alloc_pmd)(uintptr_t va);
458c2ecf20Sopenharmony_ci#endif
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic phys_addr_t dma32_phys_limit __ro_after_init;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic void __init zone_sizes_init(void)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#ifdef CONFIG_ZONE_DMA32
558c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
568c2ecf20Sopenharmony_ci#endif
578c2ecf20Sopenharmony_ci	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	free_area_init(max_zone_pfns);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void setup_zero_page(void)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	memset((void *)empty_zero_page, 0, PAGE_SIZE);
658c2ecf20Sopenharmony_ci}
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
688c2ecf20Sopenharmony_cistatic inline void print_mlk(char *name, unsigned long b, unsigned long t)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld kB)\n", name, b, t,
718c2ecf20Sopenharmony_ci		  (((t) - (b)) >> 10));
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic inline void print_mlm(char *name, unsigned long b, unsigned long t)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld MB)\n", name, b, t,
778c2ecf20Sopenharmony_ci		  (((t) - (b)) >> 20));
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic void print_vm_layout(void)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	pr_notice("Virtual kernel memory layout:\n");
838c2ecf20Sopenharmony_ci	print_mlk("fixmap", (unsigned long)FIXADDR_START,
848c2ecf20Sopenharmony_ci		  (unsigned long)FIXADDR_TOP);
858c2ecf20Sopenharmony_ci	print_mlm("pci io", (unsigned long)PCI_IO_START,
868c2ecf20Sopenharmony_ci		  (unsigned long)PCI_IO_END);
878c2ecf20Sopenharmony_ci	print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
888c2ecf20Sopenharmony_ci		  (unsigned long)VMEMMAP_END);
898c2ecf20Sopenharmony_ci	print_mlm("vmalloc", (unsigned long)VMALLOC_START,
908c2ecf20Sopenharmony_ci		  (unsigned long)VMALLOC_END);
918c2ecf20Sopenharmony_ci	print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
928c2ecf20Sopenharmony_ci		  (unsigned long)high_memory);
938c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
948c2ecf20Sopenharmony_ci	print_mlm("kernel", (unsigned long)KERNEL_LINK_ADDR,
958c2ecf20Sopenharmony_ci		  (unsigned long)ADDRESS_SPACE_END);
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci}
988c2ecf20Sopenharmony_ci#else
998c2ecf20Sopenharmony_cistatic void print_vm_layout(void) { }
1008c2ecf20Sopenharmony_ci#endif /* CONFIG_DEBUG_VM */
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_civoid __init mem_init(void)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci#ifdef CONFIG_FLATMEM
1058c2ecf20Sopenharmony_ci	BUG_ON(!mem_map);
1068c2ecf20Sopenharmony_ci#endif /* CONFIG_FLATMEM */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
1098c2ecf20Sopenharmony_ci	memblock_free_all();
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	mem_init_print_info(NULL);
1128c2ecf20Sopenharmony_ci	print_vm_layout();
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
1168c2ecf20Sopenharmony_cistatic void __init setup_initrd(void)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	phys_addr_t start;
1198c2ecf20Sopenharmony_ci	unsigned long size;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	/* Ignore the virtul address computed during device tree parsing */
1228c2ecf20Sopenharmony_ci	initrd_start = initrd_end = 0;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!phys_initrd_size)
1258c2ecf20Sopenharmony_ci		return;
1268c2ecf20Sopenharmony_ci	/*
1278c2ecf20Sopenharmony_ci	 * Round the memory region to page boundaries as per free_initrd_mem()
1288c2ecf20Sopenharmony_ci	 * This allows us to detect whether the pages overlapping the initrd
1298c2ecf20Sopenharmony_ci	 * are in use, but more importantly, reserves the entire set of pages
1308c2ecf20Sopenharmony_ci	 * as we don't want these pages allocated for other purposes.
1318c2ecf20Sopenharmony_ci	 */
1328c2ecf20Sopenharmony_ci	start = round_down(phys_initrd_start, PAGE_SIZE);
1338c2ecf20Sopenharmony_ci	size = phys_initrd_size + (phys_initrd_start - start);
1348c2ecf20Sopenharmony_ci	size = round_up(size, PAGE_SIZE);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	if (!memblock_is_region_memory(start, size)) {
1378c2ecf20Sopenharmony_ci		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
1388c2ecf20Sopenharmony_ci		       (u64)start, size);
1398c2ecf20Sopenharmony_ci		goto disable;
1408c2ecf20Sopenharmony_ci	}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (memblock_is_region_reserved(start, size)) {
1438c2ecf20Sopenharmony_ci		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
1448c2ecf20Sopenharmony_ci		       (u64)start, size);
1458c2ecf20Sopenharmony_ci		goto disable;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	memblock_reserve(start, size);
1498c2ecf20Sopenharmony_ci	/* Now convert initrd to virtual addresses */
1508c2ecf20Sopenharmony_ci	initrd_start = (unsigned long)__va(phys_initrd_start);
1518c2ecf20Sopenharmony_ci	initrd_end = initrd_start + phys_initrd_size;
1528c2ecf20Sopenharmony_ci	initrd_below_start_ok = 1;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
1558c2ecf20Sopenharmony_ci		(void *)(initrd_start), size);
1568c2ecf20Sopenharmony_ci	return;
1578c2ecf20Sopenharmony_cidisable:
1588c2ecf20Sopenharmony_ci	pr_cont(" - disabling initrd\n");
1598c2ecf20Sopenharmony_ci	initrd_start = 0;
1608c2ecf20Sopenharmony_ci	initrd_end = 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci#endif /* CONFIG_BLK_DEV_INITRD */
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_civoid __init setup_bootmem(void)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	phys_addr_t mem_start = 0;
1678c2ecf20Sopenharmony_ci	phys_addr_t start, dram_end, end = 0;
1688c2ecf20Sopenharmony_ci	phys_addr_t vmlinux_end = __pa_symbol(&_end);
1698c2ecf20Sopenharmony_ci	phys_addr_t vmlinux_start = __pa_symbol(&_start);
1708c2ecf20Sopenharmony_ci	phys_addr_t max_mapped_addr = __pa(~(ulong)0);
1718c2ecf20Sopenharmony_ci	u64 i;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/*
1748c2ecf20Sopenharmony_ci	 * Reserve from the start of the kernel to the end of the kernel
1758c2ecf20Sopenharmony_ci	 */
1768c2ecf20Sopenharmony_ci#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX)
1778c2ecf20Sopenharmony_ci	/*
1788c2ecf20Sopenharmony_ci	 * Make sure we align the reservation on PMD_SIZE since we will
1798c2ecf20Sopenharmony_ci	 * map the kernel in the linear mapping as read-only: we do not want
1808c2ecf20Sopenharmony_ci	 * any allocation to happen between _end and the next pmd aligned page.
1818c2ecf20Sopenharmony_ci	 */
1828c2ecf20Sopenharmony_ci	vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
1838c2ecf20Sopenharmony_ci#endif
1848c2ecf20Sopenharmony_ci	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	/*
1878c2ecf20Sopenharmony_ci	 * The maximal physical memory size is -PAGE_OFFSET.
1888c2ecf20Sopenharmony_ci	 * Make sure that any memory beyond mem_start + (-PAGE_OFFSET) is removed
1898c2ecf20Sopenharmony_ci	 * as it is unusable by kernel.
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	memblock_enforce_memory_limit(-PAGE_OFFSET);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Reserve from the start of the kernel to the end of the kernel */
1948c2ecf20Sopenharmony_ci	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	dram_end = memblock_end_of_DRAM();
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/*
1998c2ecf20Sopenharmony_ci	 * memblock allocator is not aware of the fact that last 4K bytes of
2008c2ecf20Sopenharmony_ci	 * the addressable memory can not be mapped because of IS_ERR_VALUE
2018c2ecf20Sopenharmony_ci	 * macro. Make sure that last 4k bytes are not usable by memblock
2028c2ecf20Sopenharmony_ci	 * if end of dram is equal to maximum addressable memory.
2038c2ecf20Sopenharmony_ci	 */
2048c2ecf20Sopenharmony_ci	if (max_mapped_addr == (dram_end - 1))
2058c2ecf20Sopenharmony_ci		memblock_set_current_limit(max_mapped_addr - 4096);
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	max_pfn = PFN_DOWN(dram_end);
2088c2ecf20Sopenharmony_ci	max_low_pfn = max_pfn;
2098c2ecf20Sopenharmony_ci	dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
2108c2ecf20Sopenharmony_ci	set_max_mapnr(max_low_pfn);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci#ifdef CONFIG_BLK_DEV_INITRD
2138c2ecf20Sopenharmony_ci	setup_initrd();
2148c2ecf20Sopenharmony_ci#endif /* CONFIG_BLK_DEV_INITRD */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/*
2178c2ecf20Sopenharmony_ci	 * Avoid using early_init_fdt_reserve_self() since __pa() does
2188c2ecf20Sopenharmony_ci	 * not work for DTB pointers that are fixmap addresses
2198c2ecf20Sopenharmony_ci	 */
2208c2ecf20Sopenharmony_ci	memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	dma_contiguous_reserve(dma32_phys_limit);
2238c2ecf20Sopenharmony_ci	memblock_allow_resize();
2248c2ecf20Sopenharmony_ci	memblock_dump_all();
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci#ifdef CONFIG_MMU
2288c2ecf20Sopenharmony_cistatic struct pt_alloc_ops pt_ops;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/* Offset between linear mapping virtual address and kernel load address */
2318c2ecf20Sopenharmony_ciunsigned long va_pa_offset;
2328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(va_pa_offset);
2338c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
2348c2ecf20Sopenharmony_ci/* Offset between kernel mapping virtual address and kernel load address */
2358c2ecf20Sopenharmony_ciunsigned long va_kernel_pa_offset;
2368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(va_kernel_pa_offset);
2378c2ecf20Sopenharmony_ci#endif
2388c2ecf20Sopenharmony_ciunsigned long pfn_base;
2398c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pfn_base);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cipgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
2428c2ecf20Sopenharmony_cipgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
2438c2ecf20Sopenharmony_cipte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cipgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_civoid __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
2488c2ecf20Sopenharmony_ci{
2498c2ecf20Sopenharmony_ci	unsigned long addr = __fix_to_virt(idx);
2508c2ecf20Sopenharmony_ci	pte_t *ptep;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	ptep = &fixmap_pte[pte_index(addr)];
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (pgprot_val(prot))
2578c2ecf20Sopenharmony_ci		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
2588c2ecf20Sopenharmony_ci	else
2598c2ecf20Sopenharmony_ci		pte_clear(&init_mm, addr, ptep);
2608c2ecf20Sopenharmony_ci	local_flush_tlb_page(addr);
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	return (pte_t *)((uintptr_t)pa);
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	clear_fixmap(FIX_PTE);
2718c2ecf20Sopenharmony_ci	return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic inline pte_t *get_pte_virt_late(phys_addr_t pa)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	return (pte_t *) __va(pa);
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic inline phys_addr_t __init alloc_pte_early(uintptr_t va)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	/*
2828c2ecf20Sopenharmony_ci	 * We only create PMD or PGD early mappings so we
2838c2ecf20Sopenharmony_ci	 * should never reach here with MMU disabled.
2848c2ecf20Sopenharmony_ci	 */
2858c2ecf20Sopenharmony_ci	BUG();
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_cistatic inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_cistatic phys_addr_t alloc_pte_late(uintptr_t va)
2948c2ecf20Sopenharmony_ci{
2958c2ecf20Sopenharmony_ci	unsigned long vaddr;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	vaddr = __get_free_page(GFP_KERNEL);
2988c2ecf20Sopenharmony_ci	if (!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr)))
2998c2ecf20Sopenharmony_ci		BUG();
3008c2ecf20Sopenharmony_ci	return __pa(vaddr);
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic void __init create_pte_mapping(pte_t *ptep,
3048c2ecf20Sopenharmony_ci				      uintptr_t va, phys_addr_t pa,
3058c2ecf20Sopenharmony_ci				      phys_addr_t sz, pgprot_t prot)
3068c2ecf20Sopenharmony_ci{
3078c2ecf20Sopenharmony_ci	uintptr_t pte_idx = pte_index(va);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	BUG_ON(sz != PAGE_SIZE);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (pte_none(ptep[pte_idx]))
3128c2ecf20Sopenharmony_ci		ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cipmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
3188c2ecf20Sopenharmony_cipmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
3198c2ecf20Sopenharmony_cipmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
3208c2ecf20Sopenharmony_cipmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	/* Before MMU is enabled */
3258c2ecf20Sopenharmony_ci	return (pmd_t *)((uintptr_t)pa);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_cistatic pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	clear_fixmap(FIX_PMD);
3318c2ecf20Sopenharmony_ci	return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
3328c2ecf20Sopenharmony_ci}
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cistatic pmd_t *get_pmd_virt_late(phys_addr_t pa)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	return (pmd_t *) __va(pa);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic phys_addr_t __init alloc_pmd_early(uintptr_t va)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	BUG_ON((va - kernel_virt_addr) >> PGDIR_SHIFT);
3428c2ecf20Sopenharmony_ci	return (uintptr_t)early_pmd;
3438c2ecf20Sopenharmony_ci}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_cistatic phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
3468c2ecf20Sopenharmony_ci{
3478c2ecf20Sopenharmony_ci	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
3488c2ecf20Sopenharmony_ci}
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic phys_addr_t alloc_pmd_late(uintptr_t va)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	unsigned long vaddr;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	vaddr = __get_free_page(GFP_KERNEL);
3558c2ecf20Sopenharmony_ci	BUG_ON(!vaddr);
3568c2ecf20Sopenharmony_ci	return __pa(vaddr);
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic void __init create_pmd_mapping(pmd_t *pmdp,
3608c2ecf20Sopenharmony_ci				      uintptr_t va, phys_addr_t pa,
3618c2ecf20Sopenharmony_ci				      phys_addr_t sz, pgprot_t prot)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	pte_t *ptep;
3648c2ecf20Sopenharmony_ci	phys_addr_t pte_phys;
3658c2ecf20Sopenharmony_ci	uintptr_t pmd_idx = pmd_index(va);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	if (sz == PMD_SIZE) {
3688c2ecf20Sopenharmony_ci		if (pmd_none(pmdp[pmd_idx]))
3698c2ecf20Sopenharmony_ci			pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
3708c2ecf20Sopenharmony_ci		return;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (pmd_none(pmdp[pmd_idx])) {
3748c2ecf20Sopenharmony_ci		pte_phys = pt_ops.alloc_pte(va);
3758c2ecf20Sopenharmony_ci		pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
3768c2ecf20Sopenharmony_ci		ptep = pt_ops.get_pte_virt(pte_phys);
3778c2ecf20Sopenharmony_ci		memset(ptep, 0, PAGE_SIZE);
3788c2ecf20Sopenharmony_ci	} else {
3798c2ecf20Sopenharmony_ci		pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
3808c2ecf20Sopenharmony_ci		ptep = pt_ops.get_pte_virt(pte_phys);
3818c2ecf20Sopenharmony_ci	}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	create_pte_mapping(ptep, va, pa, sz, prot);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci#define pgd_next_t		pmd_t
3878c2ecf20Sopenharmony_ci#define alloc_pgd_next(__va)	pt_ops.alloc_pmd(__va)
3888c2ecf20Sopenharmony_ci#define get_pgd_next_virt(__pa)	pt_ops.get_pmd_virt(__pa)
3898c2ecf20Sopenharmony_ci#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
3908c2ecf20Sopenharmony_ci	create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
3918c2ecf20Sopenharmony_ci#define fixmap_pgd_next		fixmap_pmd
3928c2ecf20Sopenharmony_ci#else
3938c2ecf20Sopenharmony_ci#define pgd_next_t		pte_t
3948c2ecf20Sopenharmony_ci#define alloc_pgd_next(__va)	pt_ops.alloc_pte(__va)
3958c2ecf20Sopenharmony_ci#define get_pgd_next_virt(__pa)	pt_ops.get_pte_virt(__pa)
3968c2ecf20Sopenharmony_ci#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
3978c2ecf20Sopenharmony_ci	create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
3988c2ecf20Sopenharmony_ci#define fixmap_pgd_next		fixmap_pte
3998c2ecf20Sopenharmony_ci#endif
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_civoid __init create_pgd_mapping(pgd_t *pgdp,
4028c2ecf20Sopenharmony_ci				      uintptr_t va, phys_addr_t pa,
4038c2ecf20Sopenharmony_ci				      phys_addr_t sz, pgprot_t prot)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	pgd_next_t *nextp;
4068c2ecf20Sopenharmony_ci	phys_addr_t next_phys;
4078c2ecf20Sopenharmony_ci	uintptr_t pgd_idx = pgd_index(va);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	if (sz == PGDIR_SIZE) {
4108c2ecf20Sopenharmony_ci		if (pgd_val(pgdp[pgd_idx]) == 0)
4118c2ecf20Sopenharmony_ci			pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
4128c2ecf20Sopenharmony_ci		return;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	if (pgd_val(pgdp[pgd_idx]) == 0) {
4168c2ecf20Sopenharmony_ci		next_phys = alloc_pgd_next(va);
4178c2ecf20Sopenharmony_ci		pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
4188c2ecf20Sopenharmony_ci		nextp = get_pgd_next_virt(next_phys);
4198c2ecf20Sopenharmony_ci		memset(nextp, 0, PAGE_SIZE);
4208c2ecf20Sopenharmony_ci	} else {
4218c2ecf20Sopenharmony_ci		next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
4228c2ecf20Sopenharmony_ci		nextp = get_pgd_next_virt(next_phys);
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	create_pgd_next_mapping(nextp, va, pa, sz, prot);
4268c2ecf20Sopenharmony_ci}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_cistatic uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	/* Upgrade to PMD_SIZE mappings whenever possible */
4318c2ecf20Sopenharmony_ci	if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
4328c2ecf20Sopenharmony_ci		return PAGE_SIZE;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	return PMD_SIZE;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci/*
4388c2ecf20Sopenharmony_ci * setup_vm() is called from head.S with MMU-off.
4398c2ecf20Sopenharmony_ci *
4408c2ecf20Sopenharmony_ci * Following requirements should be honoured for setup_vm() to work
4418c2ecf20Sopenharmony_ci * correctly:
4428c2ecf20Sopenharmony_ci * 1) It should use PC-relative addressing for accessing kernel symbols.
4438c2ecf20Sopenharmony_ci *    To achieve this we always use GCC cmodel=medany.
4448c2ecf20Sopenharmony_ci * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
4458c2ecf20Sopenharmony_ci *    so disable compiler instrumentation when FTRACE is enabled.
4468c2ecf20Sopenharmony_ci *
4478c2ecf20Sopenharmony_ci * Currently, the above requirements are honoured by using custom CFLAGS
4488c2ecf20Sopenharmony_ci * for init.o in mm/Makefile.
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci#ifndef __riscv_cmodel_medany
4528c2ecf20Sopenharmony_ci#error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
4538c2ecf20Sopenharmony_ci#endif
4548c2ecf20Sopenharmony_ci
4558c2ecf20Sopenharmony_ciuintptr_t load_pa, load_sz;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	uintptr_t va, end_va;
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	end_va = kernel_virt_addr + load_sz;
4628c2ecf20Sopenharmony_ci	for (va = kernel_virt_addr; va < end_va; va += map_size)
4638c2ecf20Sopenharmony_ci		create_pgd_mapping(pgdir, va,
4648c2ecf20Sopenharmony_ci				   load_pa + (va - kernel_virt_addr),
4658c2ecf20Sopenharmony_ci				   map_size, PAGE_KERNEL_EXEC);
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ciasmlinkage void __init setup_vm(uintptr_t dtb_pa)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	uintptr_t pa;
4718c2ecf20Sopenharmony_ci	uintptr_t map_size;
4728c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
4738c2ecf20Sopenharmony_ci	pmd_t fix_bmap_spmd, fix_bmap_epmd;
4748c2ecf20Sopenharmony_ci#endif
4758c2ecf20Sopenharmony_ci	load_pa = (uintptr_t)(&_start);
4768c2ecf20Sopenharmony_ci	load_sz = (uintptr_t)(&_end) - load_pa;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	va_pa_offset = PAGE_OFFSET - load_pa;
4798c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
4808c2ecf20Sopenharmony_ci	va_kernel_pa_offset = kernel_virt_addr - load_pa;
4818c2ecf20Sopenharmony_ci#endif
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	pfn_base = PFN_DOWN(load_pa);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_ci	/*
4868c2ecf20Sopenharmony_ci	 * Enforce boot alignment requirements of RV32 and
4878c2ecf20Sopenharmony_ci	 * RV64 by only allowing PMD or PGD mappings.
4888c2ecf20Sopenharmony_ci	 */
4898c2ecf20Sopenharmony_ci	map_size = PMD_SIZE;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	/* Sanity check alignment and size */
4928c2ecf20Sopenharmony_ci	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
4938c2ecf20Sopenharmony_ci	BUG_ON((load_pa % map_size) != 0);
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	pt_ops.alloc_pte = alloc_pte_early;
4968c2ecf20Sopenharmony_ci	pt_ops.get_pte_virt = get_pte_virt_early;
4978c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
4988c2ecf20Sopenharmony_ci	pt_ops.alloc_pmd = alloc_pmd_early;
4998c2ecf20Sopenharmony_ci	pt_ops.get_pmd_virt = get_pmd_virt_early;
5008c2ecf20Sopenharmony_ci#endif
5018c2ecf20Sopenharmony_ci	/* Setup early PGD for fixmap */
5028c2ecf20Sopenharmony_ci	create_pgd_mapping(early_pg_dir, FIXADDR_START,
5038c2ecf20Sopenharmony_ci			   (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
5068c2ecf20Sopenharmony_ci	/* Setup fixmap PMD */
5078c2ecf20Sopenharmony_ci	create_pmd_mapping(fixmap_pmd, FIXADDR_START,
5088c2ecf20Sopenharmony_ci			   (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
5098c2ecf20Sopenharmony_ci	/* Setup trampoline PGD and PMD */
5108c2ecf20Sopenharmony_ci	create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
5118c2ecf20Sopenharmony_ci			   (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
5128c2ecf20Sopenharmony_ci	create_pmd_mapping(trampoline_pmd, kernel_virt_addr,
5138c2ecf20Sopenharmony_ci			   load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
5148c2ecf20Sopenharmony_ci#else
5158c2ecf20Sopenharmony_ci	/* Setup trampoline PGD */
5168c2ecf20Sopenharmony_ci	create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
5178c2ecf20Sopenharmony_ci			   load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
5188c2ecf20Sopenharmony_ci#endif
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/*
5218c2ecf20Sopenharmony_ci	 * Setup early PGD covering entire kernel which will allow
5228c2ecf20Sopenharmony_ci	 * us to reach paging_init(). We map all memory banks later
5238c2ecf20Sopenharmony_ci	 * in setup_vm_final() below.
5248c2ecf20Sopenharmony_ci	 */
5258c2ecf20Sopenharmony_ci	create_kernel_page_table(early_pg_dir, map_size);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
5288c2ecf20Sopenharmony_ci	/* Setup early PMD for DTB */
5298c2ecf20Sopenharmony_ci	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
5308c2ecf20Sopenharmony_ci			   (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE);
5318c2ecf20Sopenharmony_ci#ifndef CONFIG_BUILTIN_DTB
5328c2ecf20Sopenharmony_ci	/* Create two consecutive PMD mappings for FDT early scan */
5338c2ecf20Sopenharmony_ci	pa = dtb_pa & ~(PMD_SIZE - 1);
5348c2ecf20Sopenharmony_ci	create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,
5358c2ecf20Sopenharmony_ci			   pa, PMD_SIZE, PAGE_KERNEL);
5368c2ecf20Sopenharmony_ci	create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,
5378c2ecf20Sopenharmony_ci			   pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
5388c2ecf20Sopenharmony_ci	dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1));
5398c2ecf20Sopenharmony_ci#else /* CONFIG_BUILTIN_DTB */
5408c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
5418c2ecf20Sopenharmony_ci	/*
5428c2ecf20Sopenharmony_ci	 * __va can't be used since it would return a linear mapping address
5438c2ecf20Sopenharmony_ci	 * whereas dtb_early_va will be used before setup_vm_final installs
5448c2ecf20Sopenharmony_ci	 * the linear mapping.
5458c2ecf20Sopenharmony_ci	 */
5468c2ecf20Sopenharmony_ci	dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
5478c2ecf20Sopenharmony_ci#else
5488c2ecf20Sopenharmony_ci	dtb_early_va = __va(dtb_pa);
5498c2ecf20Sopenharmony_ci#endif /* CONFIG_64BIT */
5508c2ecf20Sopenharmony_ci#endif /* CONFIG_BUILTIN_DTB */
5518c2ecf20Sopenharmony_ci#else
5528c2ecf20Sopenharmony_ci#ifndef CONFIG_BUILTIN_DTB
5538c2ecf20Sopenharmony_ci	/* Create two consecutive PGD mappings for FDT early scan */
5548c2ecf20Sopenharmony_ci	pa = dtb_pa & ~(PGDIR_SIZE - 1);
5558c2ecf20Sopenharmony_ci	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
5568c2ecf20Sopenharmony_ci			   pa, PGDIR_SIZE, PAGE_KERNEL);
5578c2ecf20Sopenharmony_ci	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE,
5588c2ecf20Sopenharmony_ci			   pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL);
5598c2ecf20Sopenharmony_ci	dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
5608c2ecf20Sopenharmony_ci#else /* CONFIG_BUILTIN_DTB */
5618c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
5628c2ecf20Sopenharmony_ci	dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
5638c2ecf20Sopenharmony_ci#else
5648c2ecf20Sopenharmony_ci	dtb_early_va = __va(dtb_pa);
5658c2ecf20Sopenharmony_ci#endif /* CONFIG_64BIT */
5668c2ecf20Sopenharmony_ci#endif /* CONFIG_BUILTIN_DTB */
5678c2ecf20Sopenharmony_ci#endif
5688c2ecf20Sopenharmony_ci	dtb_early_pa = dtb_pa;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	/*
5718c2ecf20Sopenharmony_ci	 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
5728c2ecf20Sopenharmony_ci	 * range can not span multiple pmds.
5738c2ecf20Sopenharmony_ci	 */
5748c2ecf20Sopenharmony_ci	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
5758c2ecf20Sopenharmony_ci		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
5788c2ecf20Sopenharmony_ci	/*
5798c2ecf20Sopenharmony_ci	 * Early ioremap fixmap is already created as it lies within first 2MB
5808c2ecf20Sopenharmony_ci	 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
5818c2ecf20Sopenharmony_ci	 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
5828c2ecf20Sopenharmony_ci	 * the user if not.
5838c2ecf20Sopenharmony_ci	 */
5848c2ecf20Sopenharmony_ci	fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
5858c2ecf20Sopenharmony_ci	fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
5868c2ecf20Sopenharmony_ci	if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
5878c2ecf20Sopenharmony_ci		WARN_ON(1);
5888c2ecf20Sopenharmony_ci		pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
5898c2ecf20Sopenharmony_ci			pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
5908c2ecf20Sopenharmony_ci		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
5918c2ecf20Sopenharmony_ci			fix_to_virt(FIX_BTMAP_BEGIN));
5928c2ecf20Sopenharmony_ci		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
5938c2ecf20Sopenharmony_ci			fix_to_virt(FIX_BTMAP_END));
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
5968c2ecf20Sopenharmony_ci		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
5978c2ecf20Sopenharmony_ci	}
5988c2ecf20Sopenharmony_ci#endif
5998c2ecf20Sopenharmony_ci}
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
6028c2ecf20Sopenharmony_civoid protect_kernel_linear_mapping_text_rodata(void)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	unsigned long text_start = (unsigned long)lm_alias(_start);
6058c2ecf20Sopenharmony_ci	unsigned long init_text_start = (unsigned long)lm_alias(__init_text_begin);
6068c2ecf20Sopenharmony_ci	unsigned long rodata_start = (unsigned long)lm_alias(__start_rodata);
6078c2ecf20Sopenharmony_ci	unsigned long data_start = (unsigned long)lm_alias(_data);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
6108c2ecf20Sopenharmony_ci	set_memory_nx(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
6138c2ecf20Sopenharmony_ci	set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
6148c2ecf20Sopenharmony_ci}
6158c2ecf20Sopenharmony_ci#endif
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_cistatic void __init setup_vm_final(void)
6188c2ecf20Sopenharmony_ci{
6198c2ecf20Sopenharmony_ci	uintptr_t va, map_size;
6208c2ecf20Sopenharmony_ci	phys_addr_t pa, start, end;
6218c2ecf20Sopenharmony_ci	u64 i;
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ci	/**
6248c2ecf20Sopenharmony_ci	 * MMU is enabled at this point. But page table setup is not complete yet.
6258c2ecf20Sopenharmony_ci	 * fixmap page table alloc functions should be used at this point
6268c2ecf20Sopenharmony_ci	 */
6278c2ecf20Sopenharmony_ci	pt_ops.alloc_pte = alloc_pte_fixmap;
6288c2ecf20Sopenharmony_ci	pt_ops.get_pte_virt = get_pte_virt_fixmap;
6298c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
6308c2ecf20Sopenharmony_ci	pt_ops.alloc_pmd = alloc_pmd_fixmap;
6318c2ecf20Sopenharmony_ci	pt_ops.get_pmd_virt = get_pmd_virt_fixmap;
6328c2ecf20Sopenharmony_ci#endif
6338c2ecf20Sopenharmony_ci	/* Setup swapper PGD for fixmap */
6348c2ecf20Sopenharmony_ci	create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
6358c2ecf20Sopenharmony_ci			   __pa_symbol(fixmap_pgd_next),
6368c2ecf20Sopenharmony_ci			   PGDIR_SIZE, PAGE_TABLE);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	/* Map all memory banks in the linear mapping */
6398c2ecf20Sopenharmony_ci	for_each_mem_range(i, &start, &end) {
6408c2ecf20Sopenharmony_ci		if (start >= end)
6418c2ecf20Sopenharmony_ci			break;
6428c2ecf20Sopenharmony_ci		if (start <= __pa(PAGE_OFFSET) &&
6438c2ecf20Sopenharmony_ci		    __pa(PAGE_OFFSET) < end)
6448c2ecf20Sopenharmony_ci			start = __pa(PAGE_OFFSET);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		map_size = best_map_size(start, end - start);
6478c2ecf20Sopenharmony_ci		for (pa = start; pa < end; pa += map_size) {
6488c2ecf20Sopenharmony_ci			va = (uintptr_t)__va(pa);
6498c2ecf20Sopenharmony_ci			create_pgd_mapping(swapper_pg_dir, va, pa,
6508c2ecf20Sopenharmony_ci					   map_size,
6518c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
6528c2ecf20Sopenharmony_ci					   PAGE_KERNEL
6538c2ecf20Sopenharmony_ci#else
6548c2ecf20Sopenharmony_ci					   PAGE_KERNEL_EXEC
6558c2ecf20Sopenharmony_ci#endif
6568c2ecf20Sopenharmony_ci					);
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ci		}
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci#ifdef CONFIG_64BIT
6628c2ecf20Sopenharmony_ci	/* Map the kernel */
6638c2ecf20Sopenharmony_ci	create_kernel_page_table(swapper_pg_dir, PMD_SIZE);
6648c2ecf20Sopenharmony_ci#endif
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* Clear fixmap PTE and PMD mappings */
6678c2ecf20Sopenharmony_ci	clear_fixmap(FIX_PTE);
6688c2ecf20Sopenharmony_ci	clear_fixmap(FIX_PMD);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	/* Move to swapper page table */
6718c2ecf20Sopenharmony_ci	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
6728c2ecf20Sopenharmony_ci	local_flush_tlb_all();
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* generic page allocation functions must be used to setup page table */
6758c2ecf20Sopenharmony_ci	pt_ops.alloc_pte = alloc_pte_late;
6768c2ecf20Sopenharmony_ci	pt_ops.get_pte_virt = get_pte_virt_late;
6778c2ecf20Sopenharmony_ci#ifndef __PAGETABLE_PMD_FOLDED
6788c2ecf20Sopenharmony_ci	pt_ops.alloc_pmd = alloc_pmd_late;
6798c2ecf20Sopenharmony_ci	pt_ops.get_pmd_virt = get_pmd_virt_late;
6808c2ecf20Sopenharmony_ci#endif
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci#else
6838c2ecf20Sopenharmony_ciasmlinkage void __init setup_vm(uintptr_t dtb_pa)
6848c2ecf20Sopenharmony_ci{
6858c2ecf20Sopenharmony_ci#ifdef CONFIG_BUILTIN_DTB
6868c2ecf20Sopenharmony_ci	dtb_early_va = soc_lookup_builtin_dtb();
6878c2ecf20Sopenharmony_ci	if (!dtb_early_va) {
6888c2ecf20Sopenharmony_ci		/* Fallback to first available DTS */
6898c2ecf20Sopenharmony_ci		dtb_early_va = (void *) __dtb_start;
6908c2ecf20Sopenharmony_ci	}
6918c2ecf20Sopenharmony_ci#else
6928c2ecf20Sopenharmony_ci	dtb_early_va = (void *)dtb_pa;
6938c2ecf20Sopenharmony_ci#endif
6948c2ecf20Sopenharmony_ci	dtb_early_pa = dtb_pa;
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_cistatic inline void setup_vm_final(void)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci}
7008c2ecf20Sopenharmony_ci#endif /* CONFIG_MMU */
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci#ifdef CONFIG_STRICT_KERNEL_RWX
7038c2ecf20Sopenharmony_civoid protect_kernel_text_data(void)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	unsigned long text_start = (unsigned long)_start;
7068c2ecf20Sopenharmony_ci	unsigned long init_text_start = (unsigned long)__init_text_begin;
7078c2ecf20Sopenharmony_ci	unsigned long init_data_start = (unsigned long)__init_data_begin;
7088c2ecf20Sopenharmony_ci	unsigned long rodata_start = (unsigned long)__start_rodata;
7098c2ecf20Sopenharmony_ci	unsigned long data_start = (unsigned long)_data;
7108c2ecf20Sopenharmony_ci#if defined(CONFIG_64BIT) && defined(CONFIG_MMU)
7118c2ecf20Sopenharmony_ci	unsigned long end_va = kernel_virt_addr + load_sz;
7128c2ecf20Sopenharmony_ci#else
7138c2ecf20Sopenharmony_ci	unsigned long end_va = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
7148c2ecf20Sopenharmony_ci#endif
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
7178c2ecf20Sopenharmony_ci	set_memory_ro(init_text_start, (init_data_start - init_text_start) >> PAGE_SHIFT);
7188c2ecf20Sopenharmony_ci	set_memory_nx(init_data_start, (rodata_start - init_data_start) >> PAGE_SHIFT);
7198c2ecf20Sopenharmony_ci	/* rodata section is marked readonly in mark_rodata_ro */
7208c2ecf20Sopenharmony_ci	set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
7218c2ecf20Sopenharmony_ci	set_memory_nx(data_start, (end_va - data_start) >> PAGE_SHIFT);
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_civoid mark_rodata_ro(void)
7258c2ecf20Sopenharmony_ci{
7268c2ecf20Sopenharmony_ci	unsigned long rodata_start = (unsigned long)__start_rodata;
7278c2ecf20Sopenharmony_ci	unsigned long data_start = (unsigned long)_data;
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	debug_checkwx();
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci#endif
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic void __init resource_init(void)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	struct memblock_region *region;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	for_each_mem_region(region) {
7408c2ecf20Sopenharmony_ci		struct resource *res;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci		res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
7438c2ecf20Sopenharmony_ci		if (!res)
7448c2ecf20Sopenharmony_ci			panic("%s: Failed to allocate %zu bytes\n", __func__,
7458c2ecf20Sopenharmony_ci			      sizeof(struct resource));
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci		if (memblock_is_nomap(region)) {
7488c2ecf20Sopenharmony_ci			res->name = "reserved";
7498c2ecf20Sopenharmony_ci			res->flags = IORESOURCE_MEM;
7508c2ecf20Sopenharmony_ci		} else {
7518c2ecf20Sopenharmony_ci			res->name = "System RAM";
7528c2ecf20Sopenharmony_ci			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
7538c2ecf20Sopenharmony_ci		}
7548c2ecf20Sopenharmony_ci		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
7558c2ecf20Sopenharmony_ci		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci		request_resource(&iomem_resource, res);
7588c2ecf20Sopenharmony_ci	}
7598c2ecf20Sopenharmony_ci}
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_civoid __init paging_init(void)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	setup_vm_final();
7648c2ecf20Sopenharmony_ci	setup_zero_page();
7658c2ecf20Sopenharmony_ci}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_civoid __init misc_mem_init(void)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	sparse_init();
7708c2ecf20Sopenharmony_ci	zone_sizes_init();
7718c2ecf20Sopenharmony_ci	resource_init();
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci#ifdef CONFIG_SPARSEMEM_VMEMMAP
7758c2ecf20Sopenharmony_ciint __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7768c2ecf20Sopenharmony_ci			       struct vmem_altmap *altmap)
7778c2ecf20Sopenharmony_ci{
7788c2ecf20Sopenharmony_ci	return vmemmap_populate_basepages(start, end, node, NULL);
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci#endif
781