xref: /linux/arch/riscv/mm/init.c (revision 170aafe35cb98e0f3fbacb446ea86389fbce22ea)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
5  * Copyright (C) 2020 FORTH-ICS/CARV
6  *  Nick Kossifidis <mick@ics.forth.gr>
7  */
8 
9 #include <linux/init.h>
10 #include <linux/mm.h>
11 #include <linux/memblock.h>
12 #include <linux/initrd.h>
13 #include <linux/swap.h>
14 #include <linux/swiotlb.h>
15 #include <linux/sizes.h>
16 #include <linux/of_fdt.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/libfdt.h>
19 #include <linux/set_memory.h>
20 #include <linux/dma-map-ops.h>
21 #include <linux/crash_dump.h>
22 #include <linux/hugetlb.h>
23 #ifdef CONFIG_RELOCATABLE
24 #include <linux/elf.h>
25 #endif
26 #include <linux/kfence.h>
27 #include <linux/execmem.h>
28 
29 #include <asm/fixmap.h>
30 #include <asm/io.h>
31 #include <asm/kasan.h>
32 #include <asm/numa.h>
33 #include <asm/pgtable.h>
34 #include <asm/sections.h>
35 #include <asm/soc.h>
36 #include <asm/tlbflush.h>
37 
38 #include "../kernel/head.h"
39 
40 struct kernel_mapping kernel_map __ro_after_init;
41 EXPORT_SYMBOL(kernel_map);
42 #ifdef CONFIG_XIP_KERNEL
43 #define kernel_map	(*(struct kernel_mapping *)XIP_FIXUP(&kernel_map))
44 #endif
45 
46 #ifdef CONFIG_64BIT
47 u64 satp_mode __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL) ? SATP_MODE_57 : SATP_MODE_39;
48 #else
49 u64 satp_mode __ro_after_init = SATP_MODE_32;
50 #endif
51 EXPORT_SYMBOL(satp_mode);
52 
53 #ifdef CONFIG_64BIT
54 bool pgtable_l4_enabled __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL);
55 bool pgtable_l5_enabled __ro_after_init = !IS_ENABLED(CONFIG_XIP_KERNEL);
56 EXPORT_SYMBOL(pgtable_l4_enabled);
57 EXPORT_SYMBOL(pgtable_l5_enabled);
58 #endif
59 
60 phys_addr_t phys_ram_base __ro_after_init;
61 EXPORT_SYMBOL(phys_ram_base);
62 
63 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
64 							__page_aligned_bss;
65 EXPORT_SYMBOL(empty_zero_page);
66 
67 extern char _start[];
68 void *_dtb_early_va __initdata;
69 uintptr_t _dtb_early_pa __initdata;
70 
71 phys_addr_t dma32_phys_limit __initdata;
72 
73 static void __init zone_sizes_init(void)
74 {
75 	unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
76 
77 #ifdef CONFIG_ZONE_DMA32
78 	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
79 #endif
80 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
81 
82 	free_area_init(max_zone_pfns);
83 }
84 
85 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
86 
87 #define LOG2_SZ_1K  ilog2(SZ_1K)
88 #define LOG2_SZ_1M  ilog2(SZ_1M)
89 #define LOG2_SZ_1G  ilog2(SZ_1G)
90 #define LOG2_SZ_1T  ilog2(SZ_1T)
91 
92 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
93 {
94 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld kB)\n", name, b, t,
95 		  (((t) - (b)) >> LOG2_SZ_1K));
96 }
97 
98 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
99 {
100 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld MB)\n", name, b, t,
101 		  (((t) - (b)) >> LOG2_SZ_1M));
102 }
103 
104 static inline void print_mlg(char *name, unsigned long b, unsigned long t)
105 {
106 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld GB)\n", name, b, t,
107 		   (((t) - (b)) >> LOG2_SZ_1G));
108 }
109 
110 #ifdef CONFIG_64BIT
111 static inline void print_mlt(char *name, unsigned long b, unsigned long t)
112 {
113 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld TB)\n", name, b, t,
114 		   (((t) - (b)) >> LOG2_SZ_1T));
115 }
116 #else
117 #define print_mlt(n, b, t) do {} while (0)
118 #endif
119 
120 static inline void print_ml(char *name, unsigned long b, unsigned long t)
121 {
122 	unsigned long diff = t - b;
123 
124 	if (IS_ENABLED(CONFIG_64BIT) && (diff >> LOG2_SZ_1T) >= 10)
125 		print_mlt(name, b, t);
126 	else if ((diff >> LOG2_SZ_1G) >= 10)
127 		print_mlg(name, b, t);
128 	else if ((diff >> LOG2_SZ_1M) >= 10)
129 		print_mlm(name, b, t);
130 	else
131 		print_mlk(name, b, t);
132 }
133 
134 static void __init print_vm_layout(void)
135 {
136 	pr_notice("Virtual kernel memory layout:\n");
137 	print_ml("fixmap", (unsigned long)FIXADDR_START,
138 		(unsigned long)FIXADDR_TOP);
139 	print_ml("pci io", (unsigned long)PCI_IO_START,
140 		(unsigned long)PCI_IO_END);
141 	print_ml("vmemmap", (unsigned long)VMEMMAP_START,
142 		(unsigned long)VMEMMAP_END);
143 	print_ml("vmalloc", (unsigned long)VMALLOC_START,
144 		(unsigned long)VMALLOC_END);
145 #ifdef CONFIG_64BIT
146 	print_ml("modules", (unsigned long)MODULES_VADDR,
147 		(unsigned long)MODULES_END);
148 #endif
149 	print_ml("lowmem", (unsigned long)PAGE_OFFSET,
150 		(unsigned long)high_memory);
151 	if (IS_ENABLED(CONFIG_64BIT)) {
152 #ifdef CONFIG_KASAN
153 		print_ml("kasan", KASAN_SHADOW_START, KASAN_SHADOW_END);
154 #endif
155 
156 		print_ml("kernel", (unsigned long)kernel_map.virt_addr,
157 			 (unsigned long)ADDRESS_SPACE_END);
158 	}
159 }
160 #else
161 static void print_vm_layout(void) { }
162 #endif /* CONFIG_DEBUG_VM */
163 
164 void __init mem_init(void)
165 {
166 	bool swiotlb = max_pfn > PFN_DOWN(dma32_phys_limit);
167 #ifdef CONFIG_FLATMEM
168 	BUG_ON(!mem_map);
169 #endif /* CONFIG_FLATMEM */
170 
171 	if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) && !swiotlb &&
172 	    dma_cache_alignment != 1) {
173 		/*
174 		 * If no bouncing needed for ZONE_DMA, allocate 1MB swiotlb
175 		 * buffer per 1GB of RAM for kmalloc() bouncing on
176 		 * non-coherent platforms.
177 		 */
178 		unsigned long size =
179 			DIV_ROUND_UP(memblock_phys_mem_size(), 1024);
180 		swiotlb_adjust_size(min(swiotlb_size_or_default(), size));
181 		swiotlb = true;
182 	}
183 
184 	swiotlb_init(swiotlb, SWIOTLB_VERBOSE);
185 	memblock_free_all();
186 
187 	print_vm_layout();
188 }
189 
190 /* Limit the memory size via mem. */
191 static phys_addr_t memory_limit;
192 #ifdef CONFIG_XIP_KERNEL
193 #define memory_limit	(*(phys_addr_t *)XIP_FIXUP(&memory_limit))
194 #endif /* CONFIG_XIP_KERNEL */
195 
196 static int __init early_mem(char *p)
197 {
198 	u64 size;
199 
200 	if (!p)
201 		return 1;
202 
203 	size = memparse(p, &p) & PAGE_MASK;
204 	memory_limit = min_t(u64, size, memory_limit);
205 
206 	pr_notice("Memory limited to %lldMB\n", (u64)memory_limit >> 20);
207 
208 	return 0;
209 }
210 early_param("mem", early_mem);
211 
212 static void __init setup_bootmem(void)
213 {
214 	phys_addr_t vmlinux_end = __pa_symbol(&_end);
215 	phys_addr_t max_mapped_addr;
216 	phys_addr_t phys_ram_end, vmlinux_start;
217 
218 	if (IS_ENABLED(CONFIG_XIP_KERNEL))
219 		vmlinux_start = __pa_symbol(&_sdata);
220 	else
221 		vmlinux_start = __pa_symbol(&_start);
222 
223 	memblock_enforce_memory_limit(memory_limit);
224 
225 	/*
226 	 * Make sure we align the reservation on PMD_SIZE since we will
227 	 * map the kernel in the linear mapping as read-only: we do not want
228 	 * any allocation to happen between _end and the next pmd aligned page.
229 	 */
230 	if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_STRICT_KERNEL_RWX))
231 		vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK;
232 	/*
233 	 * Reserve from the start of the kernel to the end of the kernel
234 	 */
235 	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
236 
237 	/*
238 	 * Make sure we align the start of the memory on a PMD boundary so that
239 	 * at worst, we map the linear mapping with PMD mappings.
240 	 */
241 	if (!IS_ENABLED(CONFIG_XIP_KERNEL))
242 		phys_ram_base = memblock_start_of_DRAM() & PMD_MASK;
243 
244 	/*
245 	 * In 64-bit, any use of __va/__pa before this point is wrong as we
246 	 * did not know the start of DRAM before.
247 	 */
248 	if (IS_ENABLED(CONFIG_64BIT) && IS_ENABLED(CONFIG_MMU))
249 		kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base;
250 
251 	/*
252 	 * The size of the linear page mapping may restrict the amount of
253 	 * usable RAM.
254 	 */
255 	if (IS_ENABLED(CONFIG_64BIT)) {
256 		max_mapped_addr = __pa(PAGE_OFFSET) + KERN_VIRT_SIZE;
257 		memblock_cap_memory_range(phys_ram_base,
258 					  max_mapped_addr - phys_ram_base);
259 	}
260 
261 	/*
262 	 * Reserve physical address space that would be mapped to virtual
263 	 * addresses greater than (void *)(-PAGE_SIZE) because:
264 	 *  - This memory would overlap with ERR_PTR
265 	 *  - This memory belongs to high memory, which is not supported
266 	 *
267 	 * This is not applicable to 64-bit kernel, because virtual addresses
268 	 * after (void *)(-PAGE_SIZE) are not linearly mapped: they are
269 	 * occupied by kernel mapping. Also it is unrealistic for high memory
270 	 * to exist on 64-bit platforms.
271 	 */
272 	if (!IS_ENABLED(CONFIG_64BIT)) {
273 		max_mapped_addr = __va_to_pa_nodebug(-PAGE_SIZE);
274 		memblock_reserve(max_mapped_addr, (phys_addr_t)-max_mapped_addr);
275 	}
276 
277 	phys_ram_end = memblock_end_of_DRAM();
278 	min_low_pfn = PFN_UP(phys_ram_base);
279 	max_low_pfn = max_pfn = PFN_DOWN(phys_ram_end);
280 	high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
281 
282 	dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
283 	set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
284 
285 	reserve_initrd_mem();
286 
287 	/*
288 	 * No allocation should be done before reserving the memory as defined
289 	 * in the device tree, otherwise the allocation could end up in a
290 	 * reserved region.
291 	 */
292 	early_init_fdt_scan_reserved_mem();
293 
294 	/*
295 	 * If DTB is built in, no need to reserve its memblock.
296 	 * Otherwise, do reserve it but avoid using
297 	 * early_init_fdt_reserve_self() since __pa() does
298 	 * not work for DTB pointers that are fixmap addresses
299 	 */
300 	if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
301 		memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
302 
303 	dma_contiguous_reserve(dma32_phys_limit);
304 	if (IS_ENABLED(CONFIG_64BIT))
305 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
306 }
307 
308 #ifdef CONFIG_MMU
309 struct pt_alloc_ops pt_ops __meminitdata;
310 
311 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
312 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
313 static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
314 
315 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
316 
317 #ifdef CONFIG_XIP_KERNEL
318 #define pt_ops			(*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops))
319 #define trampoline_pg_dir      ((pgd_t *)XIP_FIXUP(trampoline_pg_dir))
320 #define fixmap_pte             ((pte_t *)XIP_FIXUP(fixmap_pte))
321 #define early_pg_dir           ((pgd_t *)XIP_FIXUP(early_pg_dir))
322 #endif /* CONFIG_XIP_KERNEL */
323 
324 static const pgprot_t protection_map[16] = {
325 	[VM_NONE]					= PAGE_NONE,
326 	[VM_READ]					= PAGE_READ,
327 	[VM_WRITE]					= PAGE_COPY,
328 	[VM_WRITE | VM_READ]				= PAGE_COPY,
329 	[VM_EXEC]					= PAGE_EXEC,
330 	[VM_EXEC | VM_READ]				= PAGE_READ_EXEC,
331 	[VM_EXEC | VM_WRITE]				= PAGE_COPY_EXEC,
332 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY_EXEC,
333 	[VM_SHARED]					= PAGE_NONE,
334 	[VM_SHARED | VM_READ]				= PAGE_READ,
335 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
336 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
337 	[VM_SHARED | VM_EXEC]				= PAGE_EXEC,
338 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READ_EXEC,
339 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED_EXEC,
340 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED_EXEC
341 };
342 DECLARE_VM_GET_PAGE_PROT
343 
344 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
345 {
346 	unsigned long addr = __fix_to_virt(idx);
347 	pte_t *ptep;
348 
349 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
350 
351 	ptep = &fixmap_pte[pte_index(addr)];
352 
353 	if (pgprot_val(prot))
354 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
355 	else
356 		pte_clear(&init_mm, addr, ptep);
357 	local_flush_tlb_page(addr);
358 }
359 
360 static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
361 {
362 	return (pte_t *)((uintptr_t)pa);
363 }
364 
365 static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
366 {
367 	clear_fixmap(FIX_PTE);
368 	return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
369 }
370 
371 static inline pte_t *__meminit get_pte_virt_late(phys_addr_t pa)
372 {
373 	return (pte_t *) __va(pa);
374 }
375 
376 static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
377 {
378 	/*
379 	 * We only create PMD or PGD early mappings so we
380 	 * should never reach here with MMU disabled.
381 	 */
382 	BUG();
383 }
384 
385 static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
386 {
387 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
388 }
389 
390 static phys_addr_t __meminit alloc_pte_late(uintptr_t va)
391 {
392 	struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
393 
394 	BUG_ON(!ptdesc || !pagetable_pte_ctor(ptdesc));
395 	return __pa((pte_t *)ptdesc_address(ptdesc));
396 }
397 
398 static void __meminit create_pte_mapping(pte_t *ptep, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
399 					 pgprot_t prot)
400 {
401 	uintptr_t pte_idx = pte_index(va);
402 
403 	BUG_ON(sz != PAGE_SIZE);
404 
405 	if (pte_none(ptep[pte_idx]))
406 		ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
407 }
408 
409 #ifndef __PAGETABLE_PMD_FOLDED
410 
411 static pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
412 static pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
413 static pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
414 
415 #ifdef CONFIG_XIP_KERNEL
416 #define trampoline_pmd ((pmd_t *)XIP_FIXUP(trampoline_pmd))
417 #define fixmap_pmd     ((pmd_t *)XIP_FIXUP(fixmap_pmd))
418 #define early_pmd      ((pmd_t *)XIP_FIXUP(early_pmd))
419 #endif /* CONFIG_XIP_KERNEL */
420 
421 static p4d_t trampoline_p4d[PTRS_PER_P4D] __page_aligned_bss;
422 static p4d_t fixmap_p4d[PTRS_PER_P4D] __page_aligned_bss;
423 static p4d_t early_p4d[PTRS_PER_P4D] __initdata __aligned(PAGE_SIZE);
424 
425 #ifdef CONFIG_XIP_KERNEL
426 #define trampoline_p4d ((p4d_t *)XIP_FIXUP(trampoline_p4d))
427 #define fixmap_p4d     ((p4d_t *)XIP_FIXUP(fixmap_p4d))
428 #define early_p4d      ((p4d_t *)XIP_FIXUP(early_p4d))
429 #endif /* CONFIG_XIP_KERNEL */
430 
431 static pud_t trampoline_pud[PTRS_PER_PUD] __page_aligned_bss;
432 static pud_t fixmap_pud[PTRS_PER_PUD] __page_aligned_bss;
433 static pud_t early_pud[PTRS_PER_PUD] __initdata __aligned(PAGE_SIZE);
434 
435 #ifdef CONFIG_XIP_KERNEL
436 #define trampoline_pud ((pud_t *)XIP_FIXUP(trampoline_pud))
437 #define fixmap_pud     ((pud_t *)XIP_FIXUP(fixmap_pud))
438 #define early_pud      ((pud_t *)XIP_FIXUP(early_pud))
439 #endif /* CONFIG_XIP_KERNEL */
440 
441 static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
442 {
443 	/* Before MMU is enabled */
444 	return (pmd_t *)((uintptr_t)pa);
445 }
446 
447 static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
448 {
449 	clear_fixmap(FIX_PMD);
450 	return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
451 }
452 
453 static pmd_t *__meminit get_pmd_virt_late(phys_addr_t pa)
454 {
455 	return (pmd_t *) __va(pa);
456 }
457 
458 static phys_addr_t __init alloc_pmd_early(uintptr_t va)
459 {
460 	BUG_ON((va - kernel_map.virt_addr) >> PUD_SHIFT);
461 
462 	return (uintptr_t)early_pmd;
463 }
464 
465 static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
466 {
467 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
468 }
469 
470 static phys_addr_t __meminit alloc_pmd_late(uintptr_t va)
471 {
472 	struct ptdesc *ptdesc = pagetable_alloc(GFP_KERNEL & ~__GFP_HIGHMEM, 0);
473 
474 	BUG_ON(!ptdesc || !pagetable_pmd_ctor(ptdesc));
475 	return __pa((pmd_t *)ptdesc_address(ptdesc));
476 }
477 
478 static void __meminit create_pmd_mapping(pmd_t *pmdp,
479 					 uintptr_t va, phys_addr_t pa,
480 					 phys_addr_t sz, pgprot_t prot)
481 {
482 	pte_t *ptep;
483 	phys_addr_t pte_phys;
484 	uintptr_t pmd_idx = pmd_index(va);
485 
486 	if (sz == PMD_SIZE) {
487 		if (pmd_none(pmdp[pmd_idx]))
488 			pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
489 		return;
490 	}
491 
492 	if (pmd_none(pmdp[pmd_idx])) {
493 		pte_phys = pt_ops.alloc_pte(va);
494 		pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
495 		ptep = pt_ops.get_pte_virt(pte_phys);
496 		memset(ptep, 0, PAGE_SIZE);
497 	} else {
498 		pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
499 		ptep = pt_ops.get_pte_virt(pte_phys);
500 	}
501 
502 	create_pte_mapping(ptep, va, pa, sz, prot);
503 }
504 
505 static pud_t *__init get_pud_virt_early(phys_addr_t pa)
506 {
507 	return (pud_t *)((uintptr_t)pa);
508 }
509 
510 static pud_t *__init get_pud_virt_fixmap(phys_addr_t pa)
511 {
512 	clear_fixmap(FIX_PUD);
513 	return (pud_t *)set_fixmap_offset(FIX_PUD, pa);
514 }
515 
516 static pud_t *__meminit get_pud_virt_late(phys_addr_t pa)
517 {
518 	return (pud_t *)__va(pa);
519 }
520 
521 static phys_addr_t __init alloc_pud_early(uintptr_t va)
522 {
523 	/* Only one PUD is available for early mapping */
524 	BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
525 
526 	return (uintptr_t)early_pud;
527 }
528 
529 static phys_addr_t __init alloc_pud_fixmap(uintptr_t va)
530 {
531 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
532 }
533 
534 static phys_addr_t __meminit alloc_pud_late(uintptr_t va)
535 {
536 	unsigned long vaddr;
537 
538 	vaddr = __get_free_page(GFP_KERNEL);
539 	BUG_ON(!vaddr);
540 	return __pa(vaddr);
541 }
542 
543 static p4d_t *__init get_p4d_virt_early(phys_addr_t pa)
544 {
545 	return (p4d_t *)((uintptr_t)pa);
546 }
547 
548 static p4d_t *__init get_p4d_virt_fixmap(phys_addr_t pa)
549 {
550 	clear_fixmap(FIX_P4D);
551 	return (p4d_t *)set_fixmap_offset(FIX_P4D, pa);
552 }
553 
554 static p4d_t *__meminit get_p4d_virt_late(phys_addr_t pa)
555 {
556 	return (p4d_t *)__va(pa);
557 }
558 
559 static phys_addr_t __init alloc_p4d_early(uintptr_t va)
560 {
561 	/* Only one P4D is available for early mapping */
562 	BUG_ON((va - kernel_map.virt_addr) >> PGDIR_SHIFT);
563 
564 	return (uintptr_t)early_p4d;
565 }
566 
567 static phys_addr_t __init alloc_p4d_fixmap(uintptr_t va)
568 {
569 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
570 }
571 
572 static phys_addr_t __meminit alloc_p4d_late(uintptr_t va)
573 {
574 	unsigned long vaddr;
575 
576 	vaddr = __get_free_page(GFP_KERNEL);
577 	BUG_ON(!vaddr);
578 	return __pa(vaddr);
579 }
580 
581 static void __meminit create_pud_mapping(pud_t *pudp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
582 					 pgprot_t prot)
583 {
584 	pmd_t *nextp;
585 	phys_addr_t next_phys;
586 	uintptr_t pud_index = pud_index(va);
587 
588 	if (sz == PUD_SIZE) {
589 		if (pud_val(pudp[pud_index]) == 0)
590 			pudp[pud_index] = pfn_pud(PFN_DOWN(pa), prot);
591 		return;
592 	}
593 
594 	if (pud_val(pudp[pud_index]) == 0) {
595 		next_phys = pt_ops.alloc_pmd(va);
596 		pudp[pud_index] = pfn_pud(PFN_DOWN(next_phys), PAGE_TABLE);
597 		nextp = pt_ops.get_pmd_virt(next_phys);
598 		memset(nextp, 0, PAGE_SIZE);
599 	} else {
600 		next_phys = PFN_PHYS(_pud_pfn(pudp[pud_index]));
601 		nextp = pt_ops.get_pmd_virt(next_phys);
602 	}
603 
604 	create_pmd_mapping(nextp, va, pa, sz, prot);
605 }
606 
607 static void __meminit create_p4d_mapping(p4d_t *p4dp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
608 					 pgprot_t prot)
609 {
610 	pud_t *nextp;
611 	phys_addr_t next_phys;
612 	uintptr_t p4d_index = p4d_index(va);
613 
614 	if (sz == P4D_SIZE) {
615 		if (p4d_val(p4dp[p4d_index]) == 0)
616 			p4dp[p4d_index] = pfn_p4d(PFN_DOWN(pa), prot);
617 		return;
618 	}
619 
620 	if (p4d_val(p4dp[p4d_index]) == 0) {
621 		next_phys = pt_ops.alloc_pud(va);
622 		p4dp[p4d_index] = pfn_p4d(PFN_DOWN(next_phys), PAGE_TABLE);
623 		nextp = pt_ops.get_pud_virt(next_phys);
624 		memset(nextp, 0, PAGE_SIZE);
625 	} else {
626 		next_phys = PFN_PHYS(_p4d_pfn(p4dp[p4d_index]));
627 		nextp = pt_ops.get_pud_virt(next_phys);
628 	}
629 
630 	create_pud_mapping(nextp, va, pa, sz, prot);
631 }
632 
633 #define pgd_next_t		p4d_t
634 #define alloc_pgd_next(__va)	(pgtable_l5_enabled ?			\
635 		pt_ops.alloc_p4d(__va) : (pgtable_l4_enabled ?		\
636 		pt_ops.alloc_pud(__va) : pt_ops.alloc_pmd(__va)))
637 #define get_pgd_next_virt(__pa)	(pgtable_l5_enabled ?			\
638 		pt_ops.get_p4d_virt(__pa) : (pgd_next_t *)(pgtable_l4_enabled ?	\
639 		pt_ops.get_pud_virt(__pa) : (pud_t *)pt_ops.get_pmd_virt(__pa)))
640 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
641 				(pgtable_l5_enabled ?			\
642 		create_p4d_mapping(__nextp, __va, __pa, __sz, __prot) : \
643 				(pgtable_l4_enabled ?			\
644 		create_pud_mapping((pud_t *)__nextp, __va, __pa, __sz, __prot) :	\
645 		create_pmd_mapping((pmd_t *)__nextp, __va, __pa, __sz, __prot)))
646 #define fixmap_pgd_next		(pgtable_l5_enabled ?			\
647 		(uintptr_t)fixmap_p4d : (pgtable_l4_enabled ?		\
648 		(uintptr_t)fixmap_pud : (uintptr_t)fixmap_pmd))
649 #define trampoline_pgd_next	(pgtable_l5_enabled ?			\
650 		(uintptr_t)trampoline_p4d : (pgtable_l4_enabled ?	\
651 		(uintptr_t)trampoline_pud : (uintptr_t)trampoline_pmd))
652 #else
653 #define pgd_next_t		pte_t
654 #define alloc_pgd_next(__va)	pt_ops.alloc_pte(__va)
655 #define get_pgd_next_virt(__pa)	pt_ops.get_pte_virt(__pa)
656 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
657 	create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
658 #define fixmap_pgd_next		((uintptr_t)fixmap_pte)
659 #define create_p4d_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
660 #define create_pud_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
661 #define create_pmd_mapping(__pmdp, __va, __pa, __sz, __prot) do {} while(0)
662 #endif /* __PAGETABLE_PMD_FOLDED */
663 
664 void __meminit create_pgd_mapping(pgd_t *pgdp, uintptr_t va, phys_addr_t pa, phys_addr_t sz,
665 				  pgprot_t prot)
666 {
667 	pgd_next_t *nextp;
668 	phys_addr_t next_phys;
669 	uintptr_t pgd_idx = pgd_index(va);
670 
671 	if (sz == PGDIR_SIZE) {
672 		if (pgd_val(pgdp[pgd_idx]) == 0)
673 			pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
674 		return;
675 	}
676 
677 	if (pgd_val(pgdp[pgd_idx]) == 0) {
678 		next_phys = alloc_pgd_next(va);
679 		pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
680 		nextp = get_pgd_next_virt(next_phys);
681 		memset(nextp, 0, PAGE_SIZE);
682 	} else {
683 		next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
684 		nextp = get_pgd_next_virt(next_phys);
685 	}
686 
687 	create_pgd_next_mapping(nextp, va, pa, sz, prot);
688 }
689 
690 static uintptr_t __meminit best_map_size(phys_addr_t pa, uintptr_t va, phys_addr_t size)
691 {
692 	if (debug_pagealloc_enabled())
693 		return PAGE_SIZE;
694 
695 	if (pgtable_l5_enabled &&
696 	    !(pa & (P4D_SIZE - 1)) && !(va & (P4D_SIZE - 1)) && size >= P4D_SIZE)
697 		return P4D_SIZE;
698 
699 	if (pgtable_l4_enabled &&
700 	    !(pa & (PUD_SIZE - 1)) && !(va & (PUD_SIZE - 1)) && size >= PUD_SIZE)
701 		return PUD_SIZE;
702 
703 	if (IS_ENABLED(CONFIG_64BIT) &&
704 	    !(pa & (PMD_SIZE - 1)) && !(va & (PMD_SIZE - 1)) && size >= PMD_SIZE)
705 		return PMD_SIZE;
706 
707 	return PAGE_SIZE;
708 }
709 
710 #ifdef CONFIG_XIP_KERNEL
711 #define phys_ram_base  (*(phys_addr_t *)XIP_FIXUP(&phys_ram_base))
712 extern char _xiprom[], _exiprom[], __data_loc;
713 
714 /* called from head.S with MMU off */
715 asmlinkage void __init __copy_data(void)
716 {
717 	void *from = (void *)(&__data_loc);
718 	void *to = (void *)CONFIG_PHYS_RAM_BASE;
719 	size_t sz = (size_t)((uintptr_t)(&_end) - (uintptr_t)(&_sdata));
720 
721 	memcpy(to, from, sz);
722 }
723 #endif
724 
725 #ifdef CONFIG_STRICT_KERNEL_RWX
726 static __meminit pgprot_t pgprot_from_va(uintptr_t va)
727 {
728 	if (is_va_kernel_text(va))
729 		return PAGE_KERNEL_READ_EXEC;
730 
731 	/*
732 	 * In 64-bit kernel, the kernel mapping is outside the linear mapping so
733 	 * we must protect its linear mapping alias from being executed and
734 	 * written.
735 	 * And rodata section is marked readonly in mark_rodata_ro.
736 	 */
737 	if (IS_ENABLED(CONFIG_64BIT) && is_va_kernel_lm_alias_text(va))
738 		return PAGE_KERNEL_READ;
739 
740 	return PAGE_KERNEL;
741 }
742 
743 void mark_rodata_ro(void)
744 {
745 	set_kernel_memory(__start_rodata, _data, set_memory_ro);
746 	if (IS_ENABLED(CONFIG_64BIT))
747 		set_kernel_memory(lm_alias(__start_rodata), lm_alias(_data),
748 				  set_memory_ro);
749 }
750 #else
751 static __meminit pgprot_t pgprot_from_va(uintptr_t va)
752 {
753 	if (IS_ENABLED(CONFIG_64BIT) && !is_kernel_mapping(va))
754 		return PAGE_KERNEL;
755 
756 	return PAGE_KERNEL_EXEC;
757 }
758 #endif /* CONFIG_STRICT_KERNEL_RWX */
759 
760 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
761 u64 __pi_set_satp_mode_from_cmdline(uintptr_t dtb_pa);
762 
763 static void __init disable_pgtable_l5(void)
764 {
765 	pgtable_l5_enabled = false;
766 	kernel_map.page_offset = PAGE_OFFSET_L4;
767 	satp_mode = SATP_MODE_48;
768 }
769 
770 static void __init disable_pgtable_l4(void)
771 {
772 	pgtable_l4_enabled = false;
773 	kernel_map.page_offset = PAGE_OFFSET_L3;
774 	satp_mode = SATP_MODE_39;
775 }
776 
777 static int __init print_no4lvl(char *p)
778 {
779 	pr_info("Disabled 4-level and 5-level paging");
780 	return 0;
781 }
782 early_param("no4lvl", print_no4lvl);
783 
784 static int __init print_no5lvl(char *p)
785 {
786 	pr_info("Disabled 5-level paging");
787 	return 0;
788 }
789 early_param("no5lvl", print_no5lvl);
790 
791 static void __init set_mmap_rnd_bits_max(void)
792 {
793 	mmap_rnd_bits_max = MMAP_VA_BITS - PAGE_SHIFT - 3;
794 }
795 
796 /*
797  * There is a simple way to determine if 4-level is supported by the
798  * underlying hardware: establish 1:1 mapping in 4-level page table mode
799  * then read SATP to see if the configuration was taken into account
800  * meaning sv48 is supported.
801  */
802 static __init void set_satp_mode(uintptr_t dtb_pa)
803 {
804 	u64 identity_satp, hw_satp;
805 	uintptr_t set_satp_mode_pmd = ((unsigned long)set_satp_mode) & PMD_MASK;
806 	u64 satp_mode_cmdline = __pi_set_satp_mode_from_cmdline(dtb_pa);
807 
808 	if (satp_mode_cmdline == SATP_MODE_57) {
809 		disable_pgtable_l5();
810 	} else if (satp_mode_cmdline == SATP_MODE_48) {
811 		disable_pgtable_l5();
812 		disable_pgtable_l4();
813 		return;
814 	}
815 
816 	create_p4d_mapping(early_p4d,
817 			set_satp_mode_pmd, (uintptr_t)early_pud,
818 			P4D_SIZE, PAGE_TABLE);
819 	create_pud_mapping(early_pud,
820 			   set_satp_mode_pmd, (uintptr_t)early_pmd,
821 			   PUD_SIZE, PAGE_TABLE);
822 	/* Handle the case where set_satp_mode straddles 2 PMDs */
823 	create_pmd_mapping(early_pmd,
824 			   set_satp_mode_pmd, set_satp_mode_pmd,
825 			   PMD_SIZE, PAGE_KERNEL_EXEC);
826 	create_pmd_mapping(early_pmd,
827 			   set_satp_mode_pmd + PMD_SIZE,
828 			   set_satp_mode_pmd + PMD_SIZE,
829 			   PMD_SIZE, PAGE_KERNEL_EXEC);
830 retry:
831 	create_pgd_mapping(early_pg_dir,
832 			   set_satp_mode_pmd,
833 			   pgtable_l5_enabled ?
834 				(uintptr_t)early_p4d : (uintptr_t)early_pud,
835 			   PGDIR_SIZE, PAGE_TABLE);
836 
837 	identity_satp = PFN_DOWN((uintptr_t)&early_pg_dir) | satp_mode;
838 
839 	local_flush_tlb_all();
840 	csr_write(CSR_SATP, identity_satp);
841 	hw_satp = csr_swap(CSR_SATP, 0ULL);
842 	local_flush_tlb_all();
843 
844 	if (hw_satp != identity_satp) {
845 		if (pgtable_l5_enabled) {
846 			disable_pgtable_l5();
847 			memset(early_pg_dir, 0, PAGE_SIZE);
848 			goto retry;
849 		}
850 		disable_pgtable_l4();
851 	}
852 
853 	memset(early_pg_dir, 0, PAGE_SIZE);
854 	memset(early_p4d, 0, PAGE_SIZE);
855 	memset(early_pud, 0, PAGE_SIZE);
856 	memset(early_pmd, 0, PAGE_SIZE);
857 }
858 #endif
859 
860 /*
861  * setup_vm() is called from head.S with MMU-off.
862  *
863  * Following requirements should be honoured for setup_vm() to work
864  * correctly:
865  * 1) It should use PC-relative addressing for accessing kernel symbols.
866  *    To achieve this we always use GCC cmodel=medany.
867  * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
868  *    so disable compiler instrumentation when FTRACE is enabled.
869  *
870  * Currently, the above requirements are honoured by using custom CFLAGS
871  * for init.o in mm/Makefile.
872  */
873 
874 #ifndef __riscv_cmodel_medany
875 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
876 #endif
877 
878 #ifdef CONFIG_RELOCATABLE
879 extern unsigned long __rela_dyn_start, __rela_dyn_end;
880 
881 static void __init relocate_kernel(void)
882 {
883 	Elf64_Rela *rela = (Elf64_Rela *)&__rela_dyn_start;
884 	/*
885 	 * This holds the offset between the linked virtual address and the
886 	 * relocated virtual address.
887 	 */
888 	uintptr_t reloc_offset = kernel_map.virt_addr - KERNEL_LINK_ADDR;
889 	/*
890 	 * This holds the offset between kernel linked virtual address and
891 	 * physical address.
892 	 */
893 	uintptr_t va_kernel_link_pa_offset = KERNEL_LINK_ADDR - kernel_map.phys_addr;
894 
895 	for ( ; rela < (Elf64_Rela *)&__rela_dyn_end; rela++) {
896 		Elf64_Addr addr = (rela->r_offset - va_kernel_link_pa_offset);
897 		Elf64_Addr relocated_addr = rela->r_addend;
898 
899 		if (rela->r_info != R_RISCV_RELATIVE)
900 			continue;
901 
902 		/*
903 		 * Make sure to not relocate vdso symbols like rt_sigreturn
904 		 * which are linked from the address 0 in vmlinux since
905 		 * vdso symbol addresses are actually used as an offset from
906 		 * mm->context.vdso in VDSO_OFFSET macro.
907 		 */
908 		if (relocated_addr >= KERNEL_LINK_ADDR)
909 			relocated_addr += reloc_offset;
910 
911 		*(Elf64_Addr *)addr = relocated_addr;
912 	}
913 }
914 #endif /* CONFIG_RELOCATABLE */
915 
916 #ifdef CONFIG_XIP_KERNEL
917 static void __init create_kernel_page_table(pgd_t *pgdir,
918 					    __always_unused bool early)
919 {
920 	uintptr_t va, end_va;
921 
922 	/* Map the flash resident part */
923 	end_va = kernel_map.virt_addr + kernel_map.xiprom_sz;
924 	for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
925 		create_pgd_mapping(pgdir, va,
926 				   kernel_map.xiprom + (va - kernel_map.virt_addr),
927 				   PMD_SIZE, PAGE_KERNEL_EXEC);
928 
929 	/* Map the data in RAM */
930 	end_va = kernel_map.virt_addr + kernel_map.size;
931 	for (va = kernel_map.virt_addr + XIP_OFFSET; va < end_va; va += PMD_SIZE)
932 		create_pgd_mapping(pgdir, va,
933 				   kernel_map.phys_addr + (va - (kernel_map.virt_addr + XIP_OFFSET)),
934 				   PMD_SIZE, PAGE_KERNEL);
935 }
936 #else
937 static void __init create_kernel_page_table(pgd_t *pgdir, bool early)
938 {
939 	uintptr_t va, end_va;
940 
941 	end_va = kernel_map.virt_addr + kernel_map.size;
942 	for (va = kernel_map.virt_addr; va < end_va; va += PMD_SIZE)
943 		create_pgd_mapping(pgdir, va,
944 				   kernel_map.phys_addr + (va - kernel_map.virt_addr),
945 				   PMD_SIZE,
946 				   early ?
947 					PAGE_KERNEL_EXEC : pgprot_from_va(va));
948 }
949 #endif
950 
951 /*
952  * Setup a 4MB mapping that encompasses the device tree: for 64-bit kernel,
953  * this means 2 PMD entries whereas for 32-bit kernel, this is only 1 PGDIR
954  * entry.
955  */
956 static void __init create_fdt_early_page_table(uintptr_t fix_fdt_va,
957 					       uintptr_t dtb_pa)
958 {
959 #ifndef CONFIG_BUILTIN_DTB
960 	uintptr_t pa = dtb_pa & ~(PMD_SIZE - 1);
961 
962 	/* Make sure the fdt fixmap address is always aligned on PMD size */
963 	BUILD_BUG_ON(FIX_FDT % (PMD_SIZE / PAGE_SIZE));
964 
965 	/* In 32-bit only, the fdt lies in its own PGD */
966 	if (!IS_ENABLED(CONFIG_64BIT)) {
967 		create_pgd_mapping(early_pg_dir, fix_fdt_va,
968 				   pa, MAX_FDT_SIZE, PAGE_KERNEL);
969 	} else {
970 		create_pmd_mapping(fixmap_pmd, fix_fdt_va,
971 				   pa, PMD_SIZE, PAGE_KERNEL);
972 		create_pmd_mapping(fixmap_pmd, fix_fdt_va + PMD_SIZE,
973 				   pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
974 	}
975 
976 	dtb_early_va = (void *)fix_fdt_va + (dtb_pa & (PMD_SIZE - 1));
977 #else
978 	/*
979 	 * For 64-bit kernel, __va can't be used since it would return a linear
980 	 * mapping address whereas dtb_early_va will be used before
981 	 * setup_vm_final installs the linear mapping. For 32-bit kernel, as the
982 	 * kernel is mapped in the linear mapping, that makes no difference.
983 	 */
984 	dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
985 #endif
986 
987 	dtb_early_pa = dtb_pa;
988 }
989 
990 /*
991  * MMU is not enabled, the page tables are allocated directly using
992  * early_pmd/pud/p4d and the address returned is the physical one.
993  */
994 static void __init pt_ops_set_early(void)
995 {
996 	pt_ops.alloc_pte = alloc_pte_early;
997 	pt_ops.get_pte_virt = get_pte_virt_early;
998 #ifndef __PAGETABLE_PMD_FOLDED
999 	pt_ops.alloc_pmd = alloc_pmd_early;
1000 	pt_ops.get_pmd_virt = get_pmd_virt_early;
1001 	pt_ops.alloc_pud = alloc_pud_early;
1002 	pt_ops.get_pud_virt = get_pud_virt_early;
1003 	pt_ops.alloc_p4d = alloc_p4d_early;
1004 	pt_ops.get_p4d_virt = get_p4d_virt_early;
1005 #endif
1006 }
1007 
1008 /*
1009  * MMU is enabled but page table setup is not complete yet.
1010  * fixmap page table alloc functions must be used as a means to temporarily
1011  * map the allocated physical pages since the linear mapping does not exist yet.
1012  *
1013  * Note that this is called with MMU disabled, hence kernel_mapping_pa_to_va,
1014  * but it will be used as described above.
1015  */
1016 static void __init pt_ops_set_fixmap(void)
1017 {
1018 	pt_ops.alloc_pte = kernel_mapping_pa_to_va(alloc_pte_fixmap);
1019 	pt_ops.get_pte_virt = kernel_mapping_pa_to_va(get_pte_virt_fixmap);
1020 #ifndef __PAGETABLE_PMD_FOLDED
1021 	pt_ops.alloc_pmd = kernel_mapping_pa_to_va(alloc_pmd_fixmap);
1022 	pt_ops.get_pmd_virt = kernel_mapping_pa_to_va(get_pmd_virt_fixmap);
1023 	pt_ops.alloc_pud = kernel_mapping_pa_to_va(alloc_pud_fixmap);
1024 	pt_ops.get_pud_virt = kernel_mapping_pa_to_va(get_pud_virt_fixmap);
1025 	pt_ops.alloc_p4d = kernel_mapping_pa_to_va(alloc_p4d_fixmap);
1026 	pt_ops.get_p4d_virt = kernel_mapping_pa_to_va(get_p4d_virt_fixmap);
1027 #endif
1028 }
1029 
1030 /*
1031  * MMU is enabled and page table setup is complete, so from now, we can use
1032  * generic page allocation functions to setup page table.
1033  */
1034 static void __init pt_ops_set_late(void)
1035 {
1036 	pt_ops.alloc_pte = alloc_pte_late;
1037 	pt_ops.get_pte_virt = get_pte_virt_late;
1038 #ifndef __PAGETABLE_PMD_FOLDED
1039 	pt_ops.alloc_pmd = alloc_pmd_late;
1040 	pt_ops.get_pmd_virt = get_pmd_virt_late;
1041 	pt_ops.alloc_pud = alloc_pud_late;
1042 	pt_ops.get_pud_virt = get_pud_virt_late;
1043 	pt_ops.alloc_p4d = alloc_p4d_late;
1044 	pt_ops.get_p4d_virt = get_p4d_virt_late;
1045 #endif
1046 }
1047 
1048 #ifdef CONFIG_RANDOMIZE_BASE
1049 extern bool __init __pi_set_nokaslr_from_cmdline(uintptr_t dtb_pa);
1050 extern u64 __init __pi_get_kaslr_seed(uintptr_t dtb_pa);
1051 
1052 static int __init print_nokaslr(char *p)
1053 {
1054 	pr_info("Disabled KASLR");
1055 	return 0;
1056 }
1057 early_param("nokaslr", print_nokaslr);
1058 
1059 unsigned long kaslr_offset(void)
1060 {
1061 	return kernel_map.virt_offset;
1062 }
1063 #endif
1064 
1065 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1066 {
1067 	pmd_t __maybe_unused fix_bmap_spmd, fix_bmap_epmd;
1068 
1069 #ifdef CONFIG_RANDOMIZE_BASE
1070 	if (!__pi_set_nokaslr_from_cmdline(dtb_pa)) {
1071 		u64 kaslr_seed = __pi_get_kaslr_seed(dtb_pa);
1072 		u32 kernel_size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
1073 		u32 nr_pos;
1074 
1075 		/*
1076 		 * Compute the number of positions available: we are limited
1077 		 * by the early page table that only has one PUD and we must
1078 		 * be aligned on PMD_SIZE.
1079 		 */
1080 		nr_pos = (PUD_SIZE - kernel_size) / PMD_SIZE;
1081 
1082 		kernel_map.virt_offset = (kaslr_seed % nr_pos) * PMD_SIZE;
1083 	}
1084 #endif
1085 
1086 	kernel_map.virt_addr = KERNEL_LINK_ADDR + kernel_map.virt_offset;
1087 
1088 #ifdef CONFIG_XIP_KERNEL
1089 #ifdef CONFIG_64BIT
1090 	kernel_map.page_offset = PAGE_OFFSET_L3;
1091 #else
1092 	kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
1093 #endif
1094 	kernel_map.xiprom = (uintptr_t)CONFIG_XIP_PHYS_ADDR;
1095 	kernel_map.xiprom_sz = (uintptr_t)(&_exiprom) - (uintptr_t)(&_xiprom);
1096 
1097 	phys_ram_base = CONFIG_PHYS_RAM_BASE;
1098 	kernel_map.phys_addr = (uintptr_t)CONFIG_PHYS_RAM_BASE;
1099 	kernel_map.size = (uintptr_t)(&_end) - (uintptr_t)(&_start);
1100 
1101 	kernel_map.va_kernel_xip_pa_offset = kernel_map.virt_addr - kernel_map.xiprom;
1102 #else
1103 	kernel_map.page_offset = _AC(CONFIG_PAGE_OFFSET, UL);
1104 	kernel_map.phys_addr = (uintptr_t)(&_start);
1105 	kernel_map.size = (uintptr_t)(&_end) - kernel_map.phys_addr;
1106 #endif
1107 
1108 #if defined(CONFIG_64BIT) && !defined(CONFIG_XIP_KERNEL)
1109 	set_satp_mode(dtb_pa);
1110 	set_mmap_rnd_bits_max();
1111 #endif
1112 
1113 	/*
1114 	 * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem,
1115 	 * where we have the system memory layout: this allows us to align
1116 	 * the physical and virtual mappings and then make use of PUD/P4D/PGD
1117 	 * for the linear mapping. This is only possible because the kernel
1118 	 * mapping lies outside the linear mapping.
1119 	 * In 32-bit however, as the kernel resides in the linear mapping,
1120 	 * setup_vm_final can not change the mapping established here,
1121 	 * otherwise the same kernel addresses would get mapped to different
1122 	 * physical addresses (if the start of dram is different from the
1123 	 * kernel physical address start).
1124 	 */
1125 	kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ?
1126 				0UL : PAGE_OFFSET - kernel_map.phys_addr;
1127 	kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr;
1128 
1129 	/*
1130 	 * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit
1131 	 * kernel, whereas for 64-bit kernel, the end of the virtual address
1132 	 * space is occupied by the modules/BPF/kernel mappings which reduces
1133 	 * the available size of the linear mapping.
1134 	 */
1135 	memory_limit = KERN_VIRT_SIZE - (IS_ENABLED(CONFIG_64BIT) ? SZ_4G : 0);
1136 
1137 	/* Sanity check alignment and size */
1138 	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
1139 	BUG_ON((kernel_map.phys_addr % PMD_SIZE) != 0);
1140 
1141 #ifdef CONFIG_64BIT
1142 	/*
1143 	 * The last 4K bytes of the addressable memory can not be mapped because
1144 	 * of IS_ERR_VALUE macro.
1145 	 */
1146 	BUG_ON((kernel_map.virt_addr + kernel_map.size) > ADDRESS_SPACE_END - SZ_4K);
1147 #endif
1148 
1149 #ifdef CONFIG_RELOCATABLE
1150 	/*
1151 	 * Early page table uses only one PUD, which makes it possible
1152 	 * to map PUD_SIZE aligned on PUD_SIZE: if the relocation offset
1153 	 * makes the kernel cross over a PUD_SIZE boundary, raise a bug
1154 	 * since a part of the kernel would not get mapped.
1155 	 */
1156 	BUG_ON(PUD_SIZE - (kernel_map.virt_addr & (PUD_SIZE - 1)) < kernel_map.size);
1157 	relocate_kernel();
1158 #endif
1159 
1160 	apply_early_boot_alternatives();
1161 	pt_ops_set_early();
1162 
1163 	/* Setup early PGD for fixmap */
1164 	create_pgd_mapping(early_pg_dir, FIXADDR_START,
1165 			   fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1166 
1167 #ifndef __PAGETABLE_PMD_FOLDED
1168 	/* Setup fixmap P4D and PUD */
1169 	if (pgtable_l5_enabled)
1170 		create_p4d_mapping(fixmap_p4d, FIXADDR_START,
1171 				   (uintptr_t)fixmap_pud, P4D_SIZE, PAGE_TABLE);
1172 	/* Setup fixmap PUD and PMD */
1173 	if (pgtable_l4_enabled)
1174 		create_pud_mapping(fixmap_pud, FIXADDR_START,
1175 				   (uintptr_t)fixmap_pmd, PUD_SIZE, PAGE_TABLE);
1176 	create_pmd_mapping(fixmap_pmd, FIXADDR_START,
1177 			   (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
1178 	/* Setup trampoline PGD and PMD */
1179 	create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1180 			   trampoline_pgd_next, PGDIR_SIZE, PAGE_TABLE);
1181 	if (pgtable_l5_enabled)
1182 		create_p4d_mapping(trampoline_p4d, kernel_map.virt_addr,
1183 				   (uintptr_t)trampoline_pud, P4D_SIZE, PAGE_TABLE);
1184 	if (pgtable_l4_enabled)
1185 		create_pud_mapping(trampoline_pud, kernel_map.virt_addr,
1186 				   (uintptr_t)trampoline_pmd, PUD_SIZE, PAGE_TABLE);
1187 #ifdef CONFIG_XIP_KERNEL
1188 	create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1189 			   kernel_map.xiprom, PMD_SIZE, PAGE_KERNEL_EXEC);
1190 #else
1191 	create_pmd_mapping(trampoline_pmd, kernel_map.virt_addr,
1192 			   kernel_map.phys_addr, PMD_SIZE, PAGE_KERNEL_EXEC);
1193 #endif
1194 #else
1195 	/* Setup trampoline PGD */
1196 	create_pgd_mapping(trampoline_pg_dir, kernel_map.virt_addr,
1197 			   kernel_map.phys_addr, PGDIR_SIZE, PAGE_KERNEL_EXEC);
1198 #endif
1199 
1200 	/*
1201 	 * Setup early PGD covering entire kernel which will allow
1202 	 * us to reach paging_init(). We map all memory banks later
1203 	 * in setup_vm_final() below.
1204 	 */
1205 	create_kernel_page_table(early_pg_dir, true);
1206 
1207 	/* Setup early mapping for FDT early scan */
1208 	create_fdt_early_page_table(__fix_to_virt(FIX_FDT), dtb_pa);
1209 
1210 	/*
1211 	 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
1212 	 * range can not span multiple pmds.
1213 	 */
1214 	BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1215 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1216 
1217 #ifndef __PAGETABLE_PMD_FOLDED
1218 	/*
1219 	 * Early ioremap fixmap is already created as it lies within first 2MB
1220 	 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
1221 	 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
1222 	 * the user if not.
1223 	 */
1224 	fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
1225 	fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
1226 	if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
1227 		WARN_ON(1);
1228 		pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
1229 			pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
1230 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1231 			fix_to_virt(FIX_BTMAP_BEGIN));
1232 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
1233 			fix_to_virt(FIX_BTMAP_END));
1234 
1235 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
1236 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
1237 	}
1238 #endif
1239 
1240 	pt_ops_set_fixmap();
1241 }
1242 
1243 static void __meminit create_linear_mapping_range(phys_addr_t start, phys_addr_t end,
1244 						  uintptr_t fixed_map_size, const pgprot_t *pgprot)
1245 {
1246 	phys_addr_t pa;
1247 	uintptr_t va, map_size;
1248 
1249 	for (pa = start; pa < end; pa += map_size) {
1250 		va = (uintptr_t)__va(pa);
1251 		map_size = fixed_map_size ? fixed_map_size :
1252 					    best_map_size(pa, va, end - pa);
1253 
1254 		create_pgd_mapping(swapper_pg_dir, va, pa, map_size,
1255 				   pgprot ? *pgprot : pgprot_from_va(va));
1256 	}
1257 }
1258 
1259 static void __init create_linear_mapping_page_table(void)
1260 {
1261 	phys_addr_t start, end;
1262 	phys_addr_t kfence_pool __maybe_unused;
1263 	u64 i;
1264 
1265 #ifdef CONFIG_STRICT_KERNEL_RWX
1266 	phys_addr_t ktext_start = __pa_symbol(_start);
1267 	phys_addr_t ktext_size = __init_data_begin - _start;
1268 	phys_addr_t krodata_start = __pa_symbol(__start_rodata);
1269 	phys_addr_t krodata_size = _data - __start_rodata;
1270 
1271 	/* Isolate kernel text and rodata so they don't get mapped with a PUD */
1272 	memblock_mark_nomap(ktext_start,  ktext_size);
1273 	memblock_mark_nomap(krodata_start, krodata_size);
1274 #endif
1275 
1276 #ifdef CONFIG_KFENCE
1277 	/*
1278 	 *  kfence pool must be backed by PAGE_SIZE mappings, so allocate it
1279 	 *  before we setup the linear mapping so that we avoid using hugepages
1280 	 *  for this region.
1281 	 */
1282 	kfence_pool = memblock_phys_alloc(KFENCE_POOL_SIZE, PAGE_SIZE);
1283 	BUG_ON(!kfence_pool);
1284 
1285 	memblock_mark_nomap(kfence_pool, KFENCE_POOL_SIZE);
1286 	__kfence_pool = __va(kfence_pool);
1287 #endif
1288 
1289 	/* Map all memory banks in the linear mapping */
1290 	for_each_mem_range(i, &start, &end) {
1291 		if (start >= end)
1292 			break;
1293 		if (start <= __pa(PAGE_OFFSET) &&
1294 		    __pa(PAGE_OFFSET) < end)
1295 			start = __pa(PAGE_OFFSET);
1296 
1297 		create_linear_mapping_range(start, end, 0, NULL);
1298 	}
1299 
1300 #ifdef CONFIG_STRICT_KERNEL_RWX
1301 	create_linear_mapping_range(ktext_start, ktext_start + ktext_size, 0, NULL);
1302 	create_linear_mapping_range(krodata_start, krodata_start + krodata_size, 0, NULL);
1303 
1304 	memblock_clear_nomap(ktext_start,  ktext_size);
1305 	memblock_clear_nomap(krodata_start, krodata_size);
1306 #endif
1307 
1308 #ifdef CONFIG_KFENCE
1309 	create_linear_mapping_range(kfence_pool, kfence_pool + KFENCE_POOL_SIZE, PAGE_SIZE, NULL);
1310 
1311 	memblock_clear_nomap(kfence_pool, KFENCE_POOL_SIZE);
1312 #endif
1313 }
1314 
1315 static void __init setup_vm_final(void)
1316 {
1317 	/* Setup swapper PGD for fixmap */
1318 #if !defined(CONFIG_64BIT)
1319 	/*
1320 	 * In 32-bit, the device tree lies in a pgd entry, so it must be copied
1321 	 * directly in swapper_pg_dir in addition to the pgd entry that points
1322 	 * to fixmap_pte.
1323 	 */
1324 	unsigned long idx = pgd_index(__fix_to_virt(FIX_FDT));
1325 
1326 	set_pgd(&swapper_pg_dir[idx], early_pg_dir[idx]);
1327 #endif
1328 	create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
1329 			   __pa_symbol(fixmap_pgd_next),
1330 			   PGDIR_SIZE, PAGE_TABLE);
1331 
1332 	/* Map the linear mapping */
1333 	create_linear_mapping_page_table();
1334 
1335 	/* Map the kernel */
1336 	if (IS_ENABLED(CONFIG_64BIT))
1337 		create_kernel_page_table(swapper_pg_dir, false);
1338 
1339 #ifdef CONFIG_KASAN
1340 	kasan_swapper_init();
1341 #endif
1342 
1343 	/* Clear fixmap PTE and PMD mappings */
1344 	clear_fixmap(FIX_PTE);
1345 	clear_fixmap(FIX_PMD);
1346 	clear_fixmap(FIX_PUD);
1347 	clear_fixmap(FIX_P4D);
1348 
1349 	/* Move to swapper page table */
1350 	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | satp_mode);
1351 	local_flush_tlb_all();
1352 
1353 	pt_ops_set_late();
1354 }
1355 #else
1356 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
1357 {
1358 	dtb_early_va = (void *)dtb_pa;
1359 	dtb_early_pa = dtb_pa;
1360 }
1361 
1362 static inline void setup_vm_final(void)
1363 {
1364 }
1365 #endif /* CONFIG_MMU */
1366 
1367 /*
1368  * reserve_crashkernel() - reserves memory for crash kernel
1369  *
1370  * This function reserves memory area given in "crashkernel=" kernel command
1371  * line parameter. The memory reserved is used by dump capture kernel when
1372  * primary kernel is crashing.
1373  */
1374 static void __init arch_reserve_crashkernel(void)
1375 {
1376 	unsigned long long low_size = 0;
1377 	unsigned long long crash_base, crash_size;
1378 	char *cmdline = boot_command_line;
1379 	bool high = false;
1380 	int ret;
1381 
1382 	if (!IS_ENABLED(CONFIG_CRASH_RESERVE))
1383 		return;
1384 
1385 	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
1386 				&crash_size, &crash_base,
1387 				&low_size, &high);
1388 	if (ret)
1389 		return;
1390 
1391 	reserve_crashkernel_generic(cmdline, crash_size, crash_base,
1392 				    low_size, high);
1393 }
1394 
1395 void __init paging_init(void)
1396 {
1397 	setup_bootmem();
1398 	setup_vm_final();
1399 
1400 	/* Depend on that Linear Mapping is ready */
1401 	memblock_allow_resize();
1402 }
1403 
1404 void __init misc_mem_init(void)
1405 {
1406 	early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
1407 	arch_numa_init();
1408 	sparse_init();
1409 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1410 	/* The entire VMEMMAP region has been populated. Flush TLB for this region */
1411 	local_flush_tlb_kernel_range(VMEMMAP_START, VMEMMAP_END);
1412 #endif
1413 	zone_sizes_init();
1414 	arch_reserve_crashkernel();
1415 	memblock_dump_all();
1416 }
1417 
1418 #ifdef CONFIG_SPARSEMEM_VMEMMAP
1419 void __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
1420 			       unsigned long addr, unsigned long next)
1421 {
1422 	pmd_set_huge(pmd, virt_to_phys(p), PAGE_KERNEL);
1423 }
1424 
1425 int __meminit vmemmap_check_pmd(pmd_t *pmdp, int node,
1426 				unsigned long addr, unsigned long next)
1427 {
1428 	vmemmap_verify((pte_t *)pmdp, node, addr, next);
1429 	return 1;
1430 }
1431 
1432 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
1433 			       struct vmem_altmap *altmap)
1434 {
1435 	/*
1436 	 * Note that SPARSEMEM_VMEMMAP is only selected for rv64 and that we
1437 	 * can't use hugepage mappings for 2-level page table because in case of
1438 	 * memory hotplug, we are not able to update all the page tables with
1439 	 * the new PMDs.
1440 	 */
1441 	return vmemmap_populate_hugepages(start, end, node, altmap);
1442 }
1443 #endif
1444 
1445 #if defined(CONFIG_MMU) && defined(CONFIG_64BIT)
1446 /*
1447  * Pre-allocates page-table pages for a specific area in the kernel
1448  * page-table. Only the level which needs to be synchronized between
1449  * all page-tables is allocated because the synchronization can be
1450  * expensive.
1451  */
1452 static void __init preallocate_pgd_pages_range(unsigned long start, unsigned long end,
1453 					       const char *area)
1454 {
1455 	unsigned long addr;
1456 	const char *lvl;
1457 
1458 	for (addr = start; addr < end && addr >= start; addr = ALIGN(addr + 1, PGDIR_SIZE)) {
1459 		pgd_t *pgd = pgd_offset_k(addr);
1460 		p4d_t *p4d;
1461 		pud_t *pud;
1462 		pmd_t *pmd;
1463 
1464 		lvl = "p4d";
1465 		p4d = p4d_alloc(&init_mm, pgd, addr);
1466 		if (!p4d)
1467 			goto failed;
1468 
1469 		if (pgtable_l5_enabled)
1470 			continue;
1471 
1472 		lvl = "pud";
1473 		pud = pud_alloc(&init_mm, p4d, addr);
1474 		if (!pud)
1475 			goto failed;
1476 
1477 		if (pgtable_l4_enabled)
1478 			continue;
1479 
1480 		lvl = "pmd";
1481 		pmd = pmd_alloc(&init_mm, pud, addr);
1482 		if (!pmd)
1483 			goto failed;
1484 	}
1485 	return;
1486 
1487 failed:
1488 	/*
1489 	 * The pages have to be there now or they will be missing in
1490 	 * process page-tables later.
1491 	 */
1492 	panic("Failed to pre-allocate %s pages for %s area\n", lvl, area);
1493 }
1494 
1495 #define PAGE_END KASAN_SHADOW_START
1496 
1497 void __init pgtable_cache_init(void)
1498 {
1499 	preallocate_pgd_pages_range(VMALLOC_START, VMALLOC_END, "vmalloc");
1500 	if (IS_ENABLED(CONFIG_MODULES))
1501 		preallocate_pgd_pages_range(MODULES_VADDR, MODULES_END, "bpf/modules");
1502 	if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) {
1503 		preallocate_pgd_pages_range(VMEMMAP_START, VMEMMAP_END, "vmemmap");
1504 		preallocate_pgd_pages_range(PAGE_OFFSET, PAGE_END, "direct map");
1505 		if (IS_ENABLED(CONFIG_KASAN))
1506 			preallocate_pgd_pages_range(KASAN_SHADOW_START, KASAN_SHADOW_END, "kasan");
1507 	}
1508 }
1509 #endif
1510 
1511 #ifdef CONFIG_EXECMEM
1512 #ifdef CONFIG_MMU
1513 static struct execmem_info execmem_info __ro_after_init;
1514 
1515 struct execmem_info __init *execmem_arch_setup(void)
1516 {
1517 	execmem_info = (struct execmem_info){
1518 		.ranges = {
1519 			[EXECMEM_DEFAULT] = {
1520 				.start	= MODULES_VADDR,
1521 				.end	= MODULES_END,
1522 				.pgprot	= PAGE_KERNEL,
1523 				.alignment = 1,
1524 			},
1525 			[EXECMEM_KPROBES] = {
1526 				.start	= VMALLOC_START,
1527 				.end	= VMALLOC_END,
1528 				.pgprot	= PAGE_KERNEL_READ_EXEC,
1529 				.alignment = 1,
1530 			},
1531 			[EXECMEM_BPF] = {
1532 				.start	= BPF_JIT_REGION_START,
1533 				.end	= BPF_JIT_REGION_END,
1534 				.pgprot	= PAGE_KERNEL,
1535 				.alignment = PAGE_SIZE,
1536 			},
1537 		},
1538 	};
1539 
1540 	return &execmem_info;
1541 }
1542 #endif /* CONFIG_MMU */
1543 #endif /* CONFIG_EXECMEM */
1544 
1545 #ifdef CONFIG_MEMORY_HOTPLUG
1546 static void __meminit free_pte_table(pte_t *pte_start, pmd_t *pmd)
1547 {
1548 	struct page *page = pmd_page(*pmd);
1549 	struct ptdesc *ptdesc = page_ptdesc(page);
1550 	pte_t *pte;
1551 	int i;
1552 
1553 	for (i = 0; i < PTRS_PER_PTE; i++) {
1554 		pte = pte_start + i;
1555 		if (!pte_none(*pte))
1556 			return;
1557 	}
1558 
1559 	pagetable_pte_dtor(ptdesc);
1560 	if (PageReserved(page))
1561 		free_reserved_page(page);
1562 	else
1563 		pagetable_free(ptdesc);
1564 	pmd_clear(pmd);
1565 }
1566 
1567 static void __meminit free_pmd_table(pmd_t *pmd_start, pud_t *pud)
1568 {
1569 	struct page *page = pud_page(*pud);
1570 	struct ptdesc *ptdesc = page_ptdesc(page);
1571 	pmd_t *pmd;
1572 	int i;
1573 
1574 	for (i = 0; i < PTRS_PER_PMD; i++) {
1575 		pmd = pmd_start + i;
1576 		if (!pmd_none(*pmd))
1577 			return;
1578 	}
1579 
1580 	pagetable_pmd_dtor(ptdesc);
1581 	if (PageReserved(page))
1582 		free_reserved_page(page);
1583 	else
1584 		pagetable_free(ptdesc);
1585 	pud_clear(pud);
1586 }
1587 
1588 static void __meminit free_pud_table(pud_t *pud_start, p4d_t *p4d)
1589 {
1590 	struct page *page = p4d_page(*p4d);
1591 	pud_t *pud;
1592 	int i;
1593 
1594 	for (i = 0; i < PTRS_PER_PUD; i++) {
1595 		pud = pud_start + i;
1596 		if (!pud_none(*pud))
1597 			return;
1598 	}
1599 
1600 	if (PageReserved(page))
1601 		free_reserved_page(page);
1602 	else
1603 		free_pages((unsigned long)page_address(page), 0);
1604 	p4d_clear(p4d);
1605 }
1606 
1607 static void __meminit free_vmemmap_storage(struct page *page, size_t size,
1608 					   struct vmem_altmap *altmap)
1609 {
1610 	int order = get_order(size);
1611 
1612 	if (altmap) {
1613 		vmem_altmap_free(altmap, size >> PAGE_SHIFT);
1614 		return;
1615 	}
1616 
1617 	if (PageReserved(page)) {
1618 		unsigned int nr_pages = 1 << order;
1619 
1620 		while (nr_pages--)
1621 			free_reserved_page(page++);
1622 		return;
1623 	}
1624 
1625 	free_pages((unsigned long)page_address(page), order);
1626 }
1627 
1628 static void __meminit remove_pte_mapping(pte_t *pte_base, unsigned long addr, unsigned long end,
1629 					 bool is_vmemmap, struct vmem_altmap *altmap)
1630 {
1631 	unsigned long next;
1632 	pte_t *ptep, pte;
1633 
1634 	for (; addr < end; addr = next) {
1635 		next = (addr + PAGE_SIZE) & PAGE_MASK;
1636 		if (next > end)
1637 			next = end;
1638 
1639 		ptep = pte_base + pte_index(addr);
1640 		pte = ptep_get(ptep);
1641 		if (!pte_present(*ptep))
1642 			continue;
1643 
1644 		pte_clear(&init_mm, addr, ptep);
1645 		if (is_vmemmap)
1646 			free_vmemmap_storage(pte_page(pte), PAGE_SIZE, altmap);
1647 	}
1648 }
1649 
1650 static void __meminit remove_pmd_mapping(pmd_t *pmd_base, unsigned long addr, unsigned long end,
1651 					 bool is_vmemmap, struct vmem_altmap *altmap)
1652 {
1653 	unsigned long next;
1654 	pte_t *pte_base;
1655 	pmd_t *pmdp, pmd;
1656 
1657 	for (; addr < end; addr = next) {
1658 		next = pmd_addr_end(addr, end);
1659 		pmdp = pmd_base + pmd_index(addr);
1660 		pmd = pmdp_get(pmdp);
1661 		if (!pmd_present(pmd))
1662 			continue;
1663 
1664 		if (pmd_leaf(pmd)) {
1665 			pmd_clear(pmdp);
1666 			if (is_vmemmap)
1667 				free_vmemmap_storage(pmd_page(pmd), PMD_SIZE, altmap);
1668 			continue;
1669 		}
1670 
1671 		pte_base = (pte_t *)pmd_page_vaddr(*pmdp);
1672 		remove_pte_mapping(pte_base, addr, next, is_vmemmap, altmap);
1673 		free_pte_table(pte_base, pmdp);
1674 	}
1675 }
1676 
1677 static void __meminit remove_pud_mapping(pud_t *pud_base, unsigned long addr, unsigned long end,
1678 					 bool is_vmemmap, struct vmem_altmap *altmap)
1679 {
1680 	unsigned long next;
1681 	pud_t *pudp, pud;
1682 	pmd_t *pmd_base;
1683 
1684 	for (; addr < end; addr = next) {
1685 		next = pud_addr_end(addr, end);
1686 		pudp = pud_base + pud_index(addr);
1687 		pud = pudp_get(pudp);
1688 		if (!pud_present(pud))
1689 			continue;
1690 
1691 		if (pud_leaf(pud)) {
1692 			if (pgtable_l4_enabled) {
1693 				pud_clear(pudp);
1694 				if (is_vmemmap)
1695 					free_vmemmap_storage(pud_page(pud), PUD_SIZE, altmap);
1696 			}
1697 			continue;
1698 		}
1699 
1700 		pmd_base = pmd_offset(pudp, 0);
1701 		remove_pmd_mapping(pmd_base, addr, next, is_vmemmap, altmap);
1702 
1703 		if (pgtable_l4_enabled)
1704 			free_pmd_table(pmd_base, pudp);
1705 	}
1706 }
1707 
1708 static void __meminit remove_p4d_mapping(p4d_t *p4d_base, unsigned long addr, unsigned long end,
1709 					 bool is_vmemmap, struct vmem_altmap *altmap)
1710 {
1711 	unsigned long next;
1712 	p4d_t *p4dp, p4d;
1713 	pud_t *pud_base;
1714 
1715 	for (; addr < end; addr = next) {
1716 		next = p4d_addr_end(addr, end);
1717 		p4dp = p4d_base + p4d_index(addr);
1718 		p4d = p4dp_get(p4dp);
1719 		if (!p4d_present(p4d))
1720 			continue;
1721 
1722 		if (p4d_leaf(p4d)) {
1723 			if (pgtable_l5_enabled) {
1724 				p4d_clear(p4dp);
1725 				if (is_vmemmap)
1726 					free_vmemmap_storage(p4d_page(p4d), P4D_SIZE, altmap);
1727 			}
1728 			continue;
1729 		}
1730 
1731 		pud_base = pud_offset(p4dp, 0);
1732 		remove_pud_mapping(pud_base, addr, next, is_vmemmap, altmap);
1733 
1734 		if (pgtable_l5_enabled)
1735 			free_pud_table(pud_base, p4dp);
1736 	}
1737 }
1738 
1739 static void __meminit remove_pgd_mapping(unsigned long va, unsigned long end, bool is_vmemmap,
1740 					 struct vmem_altmap *altmap)
1741 {
1742 	unsigned long addr, next;
1743 	p4d_t *p4d_base;
1744 	pgd_t *pgd;
1745 
1746 	for (addr = va; addr < end; addr = next) {
1747 		next = pgd_addr_end(addr, end);
1748 		pgd = pgd_offset_k(addr);
1749 
1750 		if (!pgd_present(*pgd))
1751 			continue;
1752 
1753 		if (pgd_leaf(*pgd))
1754 			continue;
1755 
1756 		p4d_base = p4d_offset(pgd, 0);
1757 		remove_p4d_mapping(p4d_base, addr, next, is_vmemmap, altmap);
1758 	}
1759 
1760 	flush_tlb_all();
1761 }
1762 
1763 static void __meminit remove_linear_mapping(phys_addr_t start, u64 size)
1764 {
1765 	unsigned long va = (unsigned long)__va(start);
1766 	unsigned long end = (unsigned long)__va(start + size);
1767 
1768 	remove_pgd_mapping(va, end, false, NULL);
1769 }
1770 
1771 struct range arch_get_mappable_range(void)
1772 {
1773 	struct range mhp_range;
1774 
1775 	mhp_range.start = __pa(PAGE_OFFSET);
1776 	mhp_range.end = __pa(PAGE_END - 1);
1777 	return mhp_range;
1778 }
1779 
1780 int __ref arch_add_memory(int nid, u64 start, u64 size, struct mhp_params *params)
1781 {
1782 	int ret = 0;
1783 
1784 	create_linear_mapping_range(start, start + size, 0, &params->pgprot);
1785 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT, params);
1786 	if (ret) {
1787 		remove_linear_mapping(start, size);
1788 		goto out;
1789 	}
1790 
1791 	max_pfn = PFN_UP(start + size);
1792 	max_low_pfn = max_pfn;
1793 
1794  out:
1795 	flush_tlb_all();
1796 	return ret;
1797 }
1798 
1799 void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
1800 {
1801 	__remove_pages(start >> PAGE_SHIFT, size >> PAGE_SHIFT, altmap);
1802 	remove_linear_mapping(start, size);
1803 	flush_tlb_all();
1804 }
1805 
1806 void __ref vmemmap_free(unsigned long start, unsigned long end, struct vmem_altmap *altmap)
1807 {
1808 	remove_pgd_mapping(start, end, true, altmap);
1809 }
1810 #endif /* CONFIG_MEMORY_HOTPLUG */
1811