1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Regents of the University of California
4 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
7 #include <linux/init.h>
9 #include <linux/memblock.h>
10 #include <linux/initrd.h>
11 #include <linux/swap.h>
12 #include <linux/sizes.h>
13 #include <linux/of_fdt.h>
14 #include <linux/libfdt.h>
15 #include <linux/set_memory.h>
17 #include <asm/fixmap.h>
18 #include <asm/tlbflush.h>
19 #include <asm/sections.h>
20 #include <asm/pgtable.h>
22 #include <asm/ptdump.h>
24 #include "../kernel/head.h"
26 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
28 EXPORT_SYMBOL(empty_zero_page);
33 static void __init zone_sizes_init(void)
35 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
37 #ifdef CONFIG_ZONE_DMA32
38 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
39 (unsigned long) PFN_PHYS(max_low_pfn)));
41 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
43 free_area_init(max_zone_pfns);
46 static void setup_zero_page(void)
48 memset((void *)empty_zero_page, 0, PAGE_SIZE);
51 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
52 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
54 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
58 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
60 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
64 static void print_vm_layout(void)
66 pr_notice("Virtual kernel memory layout:\n");
67 print_mlk("fixmap", (unsigned long)FIXADDR_START,
68 (unsigned long)FIXADDR_TOP);
69 print_mlm("pci io", (unsigned long)PCI_IO_START,
70 (unsigned long)PCI_IO_END);
71 print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
72 (unsigned long)VMEMMAP_END);
73 print_mlm("vmalloc", (unsigned long)VMALLOC_START,
74 (unsigned long)VMALLOC_END);
75 print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
76 (unsigned long)high_memory);
79 static void print_vm_layout(void) { }
80 #endif /* CONFIG_DEBUG_VM */
82 void __init mem_init(void)
86 #endif /* CONFIG_FLATMEM */
88 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
91 mem_init_print_info(NULL);
95 #ifdef CONFIG_BLK_DEV_INITRD
96 static void __init setup_initrd(void)
100 if (initrd_start >= initrd_end) {
101 pr_info("initrd not found or empty");
104 if (__pa_symbol(initrd_end) > PFN_PHYS(max_low_pfn)) {
105 pr_err("initrd extends beyond end of memory");
109 size = initrd_end - initrd_start;
110 memblock_reserve(__pa_symbol(initrd_start), size);
111 initrd_below_start_ok = 1;
113 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
114 (void *)(initrd_start), size);
117 pr_cont(" - disabling initrd\n");
121 #endif /* CONFIG_BLK_DEV_INITRD */
123 static phys_addr_t dtb_early_pa __initdata;
125 void __init setup_bootmem(void)
127 struct memblock_region *reg;
128 phys_addr_t mem_size = 0;
129 phys_addr_t vmlinux_end = __pa_symbol(&_end);
130 phys_addr_t vmlinux_start = __pa_symbol(&_start);
132 /* Find the memory region containing the kernel */
133 for_each_memblock(memory, reg) {
134 phys_addr_t end = reg->base + reg->size;
136 if (reg->base <= vmlinux_start && vmlinux_end <= end) {
137 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
140 * Remove memblock from the end of usable area to the
143 if (reg->base + mem_size < end)
144 memblock_remove(reg->base + mem_size,
145 end - reg->base - mem_size);
148 BUG_ON(mem_size == 0);
150 /* Reserve from the start of the kernel to the end of the kernel */
151 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
153 set_max_mapnr(PFN_DOWN(mem_size));
154 max_pfn = PFN_DOWN(memblock_end_of_DRAM());
155 max_low_pfn = max_pfn;
157 #ifdef CONFIG_BLK_DEV_INITRD
159 #endif /* CONFIG_BLK_DEV_INITRD */
162 * Avoid using early_init_fdt_reserve_self() since __pa() does
163 * not work for DTB pointers that are fixmap addresses
165 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
167 early_init_fdt_scan_reserved_mem();
168 memblock_allow_resize();
171 for_each_memblock(memory, reg) {
172 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
173 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
175 memblock_set_node(PFN_PHYS(start_pfn),
176 PFN_PHYS(end_pfn - start_pfn),
177 &memblock.memory, 0);
182 unsigned long va_pa_offset;
183 EXPORT_SYMBOL(va_pa_offset);
184 unsigned long pfn_base;
185 EXPORT_SYMBOL(pfn_base);
187 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
188 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
189 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
190 static bool mmu_enabled;
192 #define MAX_EARLY_MAPPING_SIZE SZ_128M
194 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
196 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
198 unsigned long addr = __fix_to_virt(idx);
201 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
203 ptep = &fixmap_pte[pte_index(addr)];
205 if (pgprot_val(prot)) {
206 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
208 pte_clear(&init_mm, addr, ptep);
209 local_flush_tlb_page(addr);
213 static pte_t *__init get_pte_virt(phys_addr_t pa)
216 clear_fixmap(FIX_PTE);
217 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
219 return (pte_t *)((uintptr_t)pa);
223 static phys_addr_t __init alloc_pte(uintptr_t va)
226 * We only create PMD or PGD early mappings so we
227 * should never reach here with MMU disabled.
229 BUG_ON(!mmu_enabled);
231 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
234 static void __init create_pte_mapping(pte_t *ptep,
235 uintptr_t va, phys_addr_t pa,
236 phys_addr_t sz, pgprot_t prot)
238 uintptr_t pte_index = pte_index(va);
240 BUG_ON(sz != PAGE_SIZE);
242 if (pte_none(ptep[pte_index]))
243 ptep[pte_index] = pfn_pte(PFN_DOWN(pa), prot);
246 #ifndef __PAGETABLE_PMD_FOLDED
248 pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
249 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
251 #if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE
252 #define NUM_EARLY_PMDS 1UL
254 #define NUM_EARLY_PMDS (1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE)
256 pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);
258 static pmd_t *__init get_pmd_virt(phys_addr_t pa)
261 clear_fixmap(FIX_PMD);
262 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
264 return (pmd_t *)((uintptr_t)pa);
268 static phys_addr_t __init alloc_pmd(uintptr_t va)
273 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
275 pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT;
276 BUG_ON(pmd_num >= NUM_EARLY_PMDS);
277 return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD];
280 static void __init create_pmd_mapping(pmd_t *pmdp,
281 uintptr_t va, phys_addr_t pa,
282 phys_addr_t sz, pgprot_t prot)
285 phys_addr_t pte_phys;
286 uintptr_t pmd_index = pmd_index(va);
288 if (sz == PMD_SIZE) {
289 if (pmd_none(pmdp[pmd_index]))
290 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pa), prot);
294 if (pmd_none(pmdp[pmd_index])) {
295 pte_phys = alloc_pte(va);
296 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
297 ptep = get_pte_virt(pte_phys);
298 memset(ptep, 0, PAGE_SIZE);
300 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_index]));
301 ptep = get_pte_virt(pte_phys);
304 create_pte_mapping(ptep, va, pa, sz, prot);
307 #define pgd_next_t pmd_t
308 #define alloc_pgd_next(__va) alloc_pmd(__va)
309 #define get_pgd_next_virt(__pa) get_pmd_virt(__pa)
310 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
311 create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
312 #define fixmap_pgd_next fixmap_pmd
314 #define pgd_next_t pte_t
315 #define alloc_pgd_next(__va) alloc_pte(__va)
316 #define get_pgd_next_virt(__pa) get_pte_virt(__pa)
317 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
318 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
319 #define fixmap_pgd_next fixmap_pte
322 static void __init create_pgd_mapping(pgd_t *pgdp,
323 uintptr_t va, phys_addr_t pa,
324 phys_addr_t sz, pgprot_t prot)
327 phys_addr_t next_phys;
328 uintptr_t pgd_index = pgd_index(va);
330 if (sz == PGDIR_SIZE) {
331 if (pgd_val(pgdp[pgd_index]) == 0)
332 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(pa), prot);
336 if (pgd_val(pgdp[pgd_index]) == 0) {
337 next_phys = alloc_pgd_next(va);
338 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
339 nextp = get_pgd_next_virt(next_phys);
340 memset(nextp, 0, PAGE_SIZE);
342 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_index]));
343 nextp = get_pgd_next_virt(next_phys);
346 create_pgd_next_mapping(nextp, va, pa, sz, prot);
349 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
351 /* Upgrade to PMD_SIZE mappings whenever possible */
352 if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
359 * setup_vm() is called from head.S with MMU-off.
361 * Following requirements should be honoured for setup_vm() to work
363 * 1) It should use PC-relative addressing for accessing kernel symbols.
364 * To achieve this we always use GCC cmodel=medany.
365 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
366 * so disable compiler instrumentation when FTRACE is enabled.
368 * Currently, the above requirements are honoured by using custom CFLAGS
369 * for init.o in mm/Makefile.
372 #ifndef __riscv_cmodel_medany
373 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
376 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
378 uintptr_t va, end_va;
379 uintptr_t load_pa = (uintptr_t)(&_start);
380 uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
381 uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE);
383 va_pa_offset = PAGE_OFFSET - load_pa;
384 pfn_base = PFN_DOWN(load_pa);
387 * Enforce boot alignment requirements of RV32 and
388 * RV64 by only allowing PMD or PGD mappings.
390 BUG_ON(map_size == PAGE_SIZE);
392 /* Sanity check alignment and size */
393 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
394 BUG_ON((load_pa % map_size) != 0);
395 BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE);
397 /* Setup early PGD for fixmap */
398 create_pgd_mapping(early_pg_dir, FIXADDR_START,
399 (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
401 #ifndef __PAGETABLE_PMD_FOLDED
402 /* Setup fixmap PMD */
403 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
404 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
405 /* Setup trampoline PGD and PMD */
406 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
407 (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
408 create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
409 load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
411 /* Setup trampoline PGD */
412 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
413 load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
417 * Setup early PGD covering entire kernel which will allows
418 * us to reach paging_init(). We map all memory banks later
419 * in setup_vm_final() below.
421 end_va = PAGE_OFFSET + load_sz;
422 for (va = PAGE_OFFSET; va < end_va; va += map_size)
423 create_pgd_mapping(early_pg_dir, va,
424 load_pa + (va - PAGE_OFFSET),
425 map_size, PAGE_KERNEL_EXEC);
427 /* Create fixed mapping for early FDT parsing */
428 end_va = __fix_to_virt(FIX_FDT) + FIX_FDT_SIZE;
429 for (va = __fix_to_virt(FIX_FDT); va < end_va; va += PAGE_SIZE)
430 create_pte_mapping(fixmap_pte, va,
431 dtb_pa + (va - __fix_to_virt(FIX_FDT)),
432 PAGE_SIZE, PAGE_KERNEL);
434 /* Save pointer to DTB for early FDT parsing */
435 dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK);
436 /* Save physical address for memblock reservation */
437 dtb_early_pa = dtb_pa;
440 static void __init setup_vm_final(void)
442 uintptr_t va, map_size;
443 phys_addr_t pa, start, end;
444 struct memblock_region *reg;
446 /* Set mmu_enabled flag */
449 /* Setup swapper PGD for fixmap */
450 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
451 __pa_symbol(fixmap_pgd_next),
452 PGDIR_SIZE, PAGE_TABLE);
454 /* Map all memory banks */
455 for_each_memblock(memory, reg) {
457 end = start + reg->size;
461 if (memblock_is_nomap(reg))
463 if (start <= __pa(PAGE_OFFSET) &&
464 __pa(PAGE_OFFSET) < end)
465 start = __pa(PAGE_OFFSET);
467 map_size = best_map_size(start, end - start);
468 for (pa = start; pa < end; pa += map_size) {
469 va = (uintptr_t)__va(pa);
470 create_pgd_mapping(swapper_pg_dir, va, pa,
471 map_size, PAGE_KERNEL_EXEC);
475 /* Clear fixmap PTE and PMD mappings */
476 clear_fixmap(FIX_PTE);
477 clear_fixmap(FIX_PMD);
479 /* Move to swapper page table */
480 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
481 local_flush_tlb_all();
484 void free_initmem(void)
486 unsigned long init_begin = (unsigned long)__init_begin;
487 unsigned long init_end = (unsigned long)__init_end;
489 /* Make the region as non-execuatble. */
490 set_memory_nx(init_begin, (init_end - init_begin) >> PAGE_SHIFT);
491 free_initmem_default(POISON_FREE_INITMEM);
495 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
497 dtb_early_va = (void *)dtb_pa;
500 static inline void setup_vm_final(void)
503 #endif /* CONFIG_MMU */
505 #ifdef CONFIG_STRICT_KERNEL_RWX
506 void mark_rodata_ro(void)
508 unsigned long text_start = (unsigned long)_text;
509 unsigned long text_end = (unsigned long)_etext;
510 unsigned long rodata_start = (unsigned long)__start_rodata;
511 unsigned long data_start = (unsigned long)_data;
512 unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
514 set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
515 set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
516 set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
517 set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
523 void __init paging_init(void)
532 #ifdef CONFIG_SPARSEMEM_VMEMMAP
533 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
534 struct vmem_altmap *altmap)
536 return vmemmap_populate_basepages(start, end, node);