Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2012 Regents of the University of California
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/initrd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/of_fdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/libfdt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/set_memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <asm/fixmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/sections.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/ptdump.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "../kernel/head.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 							__page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) EXPORT_SYMBOL(empty_zero_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) extern char _start[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define DTB_EARLY_BASE_VA      PGDIR_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) void *dtb_early_va __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) uintptr_t dtb_early_pa __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) struct pt_alloc_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	pte_t *(*get_pte_virt)(phys_addr_t pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	phys_addr_t (*alloc_pte)(uintptr_t va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	pmd_t *(*get_pmd_virt)(phys_addr_t pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	phys_addr_t (*alloc_pmd)(uintptr_t va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static void __init zone_sizes_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #ifdef CONFIG_ZONE_DMA32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 			(unsigned long) PFN_PHYS(max_low_pfn)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	free_area_init(max_zone_pfns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static void setup_zero_page(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	memset((void *)empty_zero_page, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) static inline void print_mlk(char *name, unsigned long b, unsigned long t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld kB)\n", name, b, t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		  (((t) - (b)) >> 10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static inline void print_mlm(char *name, unsigned long b, unsigned long t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld MB)\n", name, b, t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		  (((t) - (b)) >> 20));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) static void print_vm_layout(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	pr_notice("Virtual kernel memory layout:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	print_mlk("fixmap", (unsigned long)FIXADDR_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		  (unsigned long)FIXADDR_TOP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	print_mlm("pci io", (unsigned long)PCI_IO_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		  (unsigned long)PCI_IO_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		  (unsigned long)VMEMMAP_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	print_mlm("vmalloc", (unsigned long)VMALLOC_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		  (unsigned long)VMALLOC_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		  (unsigned long)high_memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static void print_vm_layout(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #endif /* CONFIG_DEBUG_VM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) void __init mem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) #ifdef CONFIG_FLATMEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	BUG_ON(!mem_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) #endif /* CONFIG_FLATMEM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	memblock_free_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	mem_init_print_info(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	print_vm_layout();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #ifdef CONFIG_BLK_DEV_INITRD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static void __init setup_initrd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	phys_addr_t start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned long size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	/* Ignore the virtul address computed during device tree parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	initrd_start = initrd_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!phys_initrd_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	 * Round the memory region to page boundaries as per free_initrd_mem()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	 * This allows us to detect whether the pages overlapping the initrd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	 * are in use, but more importantly, reserves the entire set of pages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	 * as we don't want these pages allocated for other purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	start = round_down(phys_initrd_start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	size = phys_initrd_size + (phys_initrd_start - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	size = round_up(size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!memblock_is_region_memory(start, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		       (u64)start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		goto disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (memblock_is_region_reserved(start, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		       (u64)start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		goto disable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	memblock_reserve(start, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* Now convert initrd to virtual addresses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	initrd_start = (unsigned long)__va(phys_initrd_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	initrd_end = initrd_start + phys_initrd_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	initrd_below_start_ok = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		(void *)(initrd_start), size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) disable:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	pr_cont(" - disabling initrd\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	initrd_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	initrd_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #endif /* CONFIG_BLK_DEV_INITRD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) void __init setup_bootmem(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	phys_addr_t mem_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	phys_addr_t start, dram_end, end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	phys_addr_t vmlinux_end = __pa_symbol(&_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	phys_addr_t vmlinux_start = __pa_symbol(&_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	phys_addr_t max_mapped_addr = __pa(~(ulong)0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	/* Find the memory region containing the kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	for_each_mem_range(i, &start, &end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		phys_addr_t size = end - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (!mem_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			mem_start = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		if (start <= vmlinux_start && vmlinux_end <= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			BUG_ON(size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	 * The maximal physical memory size is -PAGE_OFFSET.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * Make sure that any memory beyond mem_start + (-PAGE_OFFSET) is removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 * as it is unusable by kernel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	memblock_enforce_memory_limit(-PAGE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	/* Reserve from the start of the kernel to the end of the kernel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	dram_end = memblock_end_of_DRAM();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	 * memblock allocator is not aware of the fact that last 4K bytes of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	 * the addressable memory can not be mapped because of IS_ERR_VALUE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	 * macro. Make sure that last 4k bytes are not usable by memblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	 * if end of dram is equal to maximum addressable memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (max_mapped_addr == (dram_end - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		memblock_set_current_limit(max_mapped_addr - 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	max_pfn = PFN_DOWN(dram_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	max_low_pfn = max_pfn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	set_max_mapnr(max_low_pfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #ifdef CONFIG_BLK_DEV_INITRD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	setup_initrd();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) #endif /* CONFIG_BLK_DEV_INITRD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * Avoid using early_init_fdt_reserve_self() since __pa() does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * not work for DTB pointers that are fixmap addresses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	early_init_fdt_scan_reserved_mem();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	memblock_allow_resize();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	memblock_dump_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #ifdef CONFIG_MMU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static struct pt_alloc_ops pt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) unsigned long va_pa_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) EXPORT_SYMBOL(va_pa_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unsigned long pfn_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) EXPORT_SYMBOL(pfn_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	unsigned long addr = __fix_to_virt(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	ptep = &fixmap_pte[pte_index(addr)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (pgprot_val(prot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		pte_clear(&init_mm, addr, ptep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	local_flush_tlb_page(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return (pte_t *)((uintptr_t)pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	clear_fixmap(FIX_PTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static inline pte_t *get_pte_virt_late(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return (pte_t *) __va(pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	 * We only create PMD or PGD early mappings so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 * should never reach here with MMU disabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static phys_addr_t alloc_pte_late(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	unsigned long vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	vaddr = __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return __pa(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static void __init create_pte_mapping(pte_t *ptep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 				      uintptr_t va, phys_addr_t pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 				      phys_addr_t sz, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	uintptr_t pte_idx = pte_index(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	BUG_ON(sz != PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (pte_none(ptep[pte_idx]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) pmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	/* Before MMU is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return (pmd_t *)((uintptr_t)pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	clear_fixmap(FIX_PMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static pmd_t *get_pmd_virt_late(phys_addr_t pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	return (pmd_t *) __va(pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static phys_addr_t __init alloc_pmd_early(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	BUG_ON((va - PAGE_OFFSET) >> PGDIR_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	return (uintptr_t)early_pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static phys_addr_t alloc_pmd_late(uintptr_t va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	unsigned long vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	vaddr = __get_free_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	BUG_ON(!vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return __pa(vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) static void __init create_pmd_mapping(pmd_t *pmdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 				      uintptr_t va, phys_addr_t pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 				      phys_addr_t sz, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	pte_t *ptep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	phys_addr_t pte_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	uintptr_t pmd_idx = pmd_index(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (sz == PMD_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (pmd_none(pmdp[pmd_idx]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (pmd_none(pmdp[pmd_idx])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		pte_phys = pt_ops.alloc_pte(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		ptep = pt_ops.get_pte_virt(pte_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		memset(ptep, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		ptep = pt_ops.get_pte_virt(pte_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	create_pte_mapping(ptep, va, pa, sz, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) #define pgd_next_t		pmd_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) #define alloc_pgd_next(__va)	pt_ops.alloc_pmd(__va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) #define get_pgd_next_virt(__pa)	pt_ops.get_pmd_virt(__pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) #define fixmap_pgd_next		fixmap_pmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) #define pgd_next_t		pte_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) #define alloc_pgd_next(__va)	pt_ops.alloc_pte(__va)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) #define get_pgd_next_virt(__pa)	pt_ops.get_pte_virt(__pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) #define fixmap_pgd_next		fixmap_pte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) void __init create_pgd_mapping(pgd_t *pgdp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 				      uintptr_t va, phys_addr_t pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 				      phys_addr_t sz, pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	pgd_next_t *nextp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	phys_addr_t next_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	uintptr_t pgd_idx = pgd_index(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (sz == PGDIR_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		if (pgd_val(pgdp[pgd_idx]) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (pgd_val(pgdp[pgd_idx]) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		next_phys = alloc_pgd_next(va);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		nextp = get_pgd_next_virt(next_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		memset(nextp, 0, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		nextp = get_pgd_next_virt(next_phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	create_pgd_next_mapping(nextp, va, pa, sz, prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/* Upgrade to PMD_SIZE mappings whenever possible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		return PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	return PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * setup_vm() is called from head.S with MMU-off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * Following requirements should be honoured for setup_vm() to work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  * correctly:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  * 1) It should use PC-relative addressing for accessing kernel symbols.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  *    To achieve this we always use GCC cmodel=medany.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  *    so disable compiler instrumentation when FTRACE is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  * Currently, the above requirements are honoured by using custom CFLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  * for init.o in mm/Makefile.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) #ifndef __riscv_cmodel_medany
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) asmlinkage void __init setup_vm(uintptr_t dtb_pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	uintptr_t va, pa, end_va;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	uintptr_t load_pa = (uintptr_t)(&_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	uintptr_t map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	pmd_t fix_bmap_spmd, fix_bmap_epmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	va_pa_offset = PAGE_OFFSET - load_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	pfn_base = PFN_DOWN(load_pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 * Enforce boot alignment requirements of RV32 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	 * RV64 by only allowing PMD or PGD mappings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	map_size = PMD_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	/* Sanity check alignment and size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	BUG_ON((load_pa % map_size) != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	pt_ops.alloc_pte = alloc_pte_early;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	pt_ops.get_pte_virt = get_pte_virt_early;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	pt_ops.alloc_pmd = alloc_pmd_early;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	pt_ops.get_pmd_virt = get_pmd_virt_early;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	/* Setup early PGD for fixmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	create_pgd_mapping(early_pg_dir, FIXADDR_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			   (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	/* Setup fixmap PMD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	create_pmd_mapping(fixmap_pmd, FIXADDR_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			   (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	/* Setup trampoline PGD and PMD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 			   (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 			   load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	/* Setup trampoline PGD */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 			   load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	 * Setup early PGD covering entire kernel which will allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	 * us to reach paging_init(). We map all memory banks later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	 * in setup_vm_final() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	end_va = PAGE_OFFSET + load_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	for (va = PAGE_OFFSET; va < end_va; va += map_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		create_pgd_mapping(early_pg_dir, va,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 				   load_pa + (va - PAGE_OFFSET),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 				   map_size, PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	/* Setup early PMD for DTB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 			   (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* Create two consecutive PMD mappings for FDT early scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	pa = dtb_pa & ~(PMD_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 			   pa, PMD_SIZE, PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 			   pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	/* Create two consecutive PGD mappings for FDT early scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	pa = dtb_pa & ~(PGDIR_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			   pa, PGDIR_SIZE, PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			   pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	dtb_early_pa = dtb_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 * range can not span multiple pmds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	 * Early ioremap fixmap is already created as it lies within first 2MB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 * the user if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		WARN_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 			pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 			fix_to_virt(FIX_BTMAP_BEGIN));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			fix_to_virt(FIX_BTMAP_END));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) static void __init setup_vm_final(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	uintptr_t va, map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	phys_addr_t pa, start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	u64 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	/**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	 * MMU is enabled at this point. But page table setup is not complete yet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	 * fixmap page table alloc functions should be used at this point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	pt_ops.alloc_pte = alloc_pte_fixmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	pt_ops.get_pte_virt = get_pte_virt_fixmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	pt_ops.alloc_pmd = alloc_pmd_fixmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	pt_ops.get_pmd_virt = get_pmd_virt_fixmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	/* Setup swapper PGD for fixmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			   __pa_symbol(fixmap_pgd_next),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			   PGDIR_SIZE, PAGE_TABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	/* Map all memory banks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	for_each_mem_range(i, &start, &end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		if (start >= end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		if (start <= __pa(PAGE_OFFSET) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		    __pa(PAGE_OFFSET) < end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			start = __pa(PAGE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		map_size = best_map_size(start, end - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		for (pa = start; pa < end; pa += map_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			va = (uintptr_t)__va(pa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			create_pgd_mapping(swapper_pg_dir, va, pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 					   map_size, PAGE_KERNEL_EXEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	/* Clear fixmap PTE and PMD mappings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	clear_fixmap(FIX_PTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	clear_fixmap(FIX_PMD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	/* Move to swapper page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	local_flush_tlb_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	/* generic page allocation functions must be used to setup page table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	pt_ops.alloc_pte = alloc_pte_late;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	pt_ops.get_pte_virt = get_pte_virt_late;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) #ifndef __PAGETABLE_PMD_FOLDED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	pt_ops.alloc_pmd = alloc_pmd_late;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	pt_ops.get_pmd_virt = get_pmd_virt_late;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) asmlinkage void __init setup_vm(uintptr_t dtb_pa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) #ifdef CONFIG_BUILTIN_DTB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	dtb_early_va = soc_lookup_builtin_dtb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	if (!dtb_early_va) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 		/* Fallback to first available DTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		dtb_early_va = (void *) __dtb_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	dtb_early_va = (void *)dtb_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	dtb_early_pa = dtb_pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) static inline void setup_vm_final(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) #endif /* CONFIG_MMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) #ifdef CONFIG_STRICT_KERNEL_RWX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) void mark_rodata_ro(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	unsigned long text_start = (unsigned long)_text;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	unsigned long text_end = (unsigned long)_etext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	unsigned long rodata_start = (unsigned long)__start_rodata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	unsigned long data_start = (unsigned long)_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	debug_checkwx();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) static void __init resource_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	struct memblock_region *region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	for_each_mem_region(region) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 			panic("%s: Failed to allocate %zu bytes\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 			      sizeof(struct resource));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 		if (memblock_is_nomap(region)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 			res->name = "reserved";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 			res->flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 			res->name = "System RAM";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		request_resource(&iomem_resource, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) void __init paging_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	setup_vm_final();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	sparse_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	setup_zero_page();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	zone_sizes_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	resource_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #ifdef CONFIG_SPARSEMEM_VMEMMAP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 			       struct vmem_altmap *altmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	return vmemmap_populate_basepages(start, end, node, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) #endif