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