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