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