blob: b55be44ff9bdf3ebad11a952b56b366ab2310ed7 [file] [log] [blame]
Thomas Gleixner50acfb22019-05-29 07:18:00 -07001// SPDX-License-Identifier: GPL-2.0-only
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07002/*
3 * Copyright (C) 2012 Regents of the University of California
Anup Patel671f9a32019-06-28 13:36:21 -07004 * Copyright (C) 2019 Western Digital Corporation or its affiliates.
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07005 */
6
7#include <linux/init.h>
8#include <linux/mm.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07009#include <linux/memblock.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070010#include <linux/initrd.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070011#include <linux/swap.h>
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010012#include <linux/sizes.h>
Anup Patel0651c262019-02-21 11:25:49 +053013#include <linux/of_fdt.h>
Albert Ou922b0372019-09-27 16:14:18 -070014#include <linux/libfdt.h>
Zong Lid27c3c92020-03-10 00:55:41 +080015#include <linux/set_memory.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070016
Anup Patelf2c17aa2019-01-07 20:57:01 +053017#include <asm/fixmap.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070018#include <asm/tlbflush.h>
19#include <asm/sections.h>
20#include <asm/pgtable.h>
21#include <asm/io.h>
22
Paul Walmsleyffaee272019-10-17 15:00:17 -070023#include "../kernel/head.h"
24
Anup Patel387181d2019-03-26 08:03:47 +000025unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
26 __page_aligned_bss;
27EXPORT_SYMBOL(empty_zero_page);
28
Anup Pateld90d45d2019-06-07 06:01:29 +000029extern char _start[];
Christoph Hellwig6bd33e12019-10-28 13:10:41 +010030void *dtb_early_va;
Anup Pateld90d45d2019-06-07 06:01:29 +000031
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070032static void __init zone_sizes_init(void)
33{
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010034 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070035
Zong Lid5fad482018-06-25 16:49:37 +080036#ifdef CONFIG_ZONE_DMA32
Guo Ren28198c42019-01-12 16:16:27 +080037 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
38 (unsigned long) PFN_PHYS(max_low_pfn)));
Zong Lid5fad482018-06-25 16:49:37 +080039#endif
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010040 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
41
42 free_area_init_nodes(max_zone_pfns);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070043}
44
Christoph Hellwig6bd33e12019-10-28 13:10:41 +010045static void setup_zero_page(void)
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070046{
47 memset((void *)empty_zero_page, 0, PAGE_SIZE);
48}
49
Yash Shah2cc6c4a2019-11-18 05:58:34 +000050#ifdef CONFIG_DEBUG_VM
51static inline void print_mlk(char *name, unsigned long b, unsigned long t)
52{
53 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
54 (((t) - (b)) >> 10));
55}
56
57static inline void print_mlm(char *name, unsigned long b, unsigned long t)
58{
59 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
60 (((t) - (b)) >> 20));
61}
62
63static void print_vm_layout(void)
64{
65 pr_notice("Virtual kernel memory layout:\n");
66 print_mlk("fixmap", (unsigned long)FIXADDR_START,
67 (unsigned long)FIXADDR_TOP);
68 print_mlm("pci io", (unsigned long)PCI_IO_START,
69 (unsigned long)PCI_IO_END);
70 print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
71 (unsigned long)VMEMMAP_END);
72 print_mlm("vmalloc", (unsigned long)VMALLOC_START,
73 (unsigned long)VMALLOC_END);
74 print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
75 (unsigned long)high_memory);
76}
77#else
78static void print_vm_layout(void) { }
79#endif /* CONFIG_DEBUG_VM */
80
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070081void __init mem_init(void)
82{
83#ifdef CONFIG_FLATMEM
84 BUG_ON(!mem_map);
85#endif /* CONFIG_FLATMEM */
86
87 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -070088 memblock_free_all();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070089
90 mem_init_print_info(NULL);
Yash Shah2cc6c4a2019-11-18 05:58:34 +000091 print_vm_layout();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070092}
93
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070094#ifdef CONFIG_BLK_DEV_INITRD
Anup Patel0651c262019-02-21 11:25:49 +053095static void __init setup_initrd(void)
96{
97 unsigned long size;
98
99 if (initrd_start >= initrd_end) {
100 pr_info("initrd not found or empty");
101 goto disable;
102 }
Zong Liac51e002020-01-02 11:12:40 +0800103 if (__pa_symbol(initrd_end) > PFN_PHYS(max_low_pfn)) {
Anup Patel0651c262019-02-21 11:25:49 +0530104 pr_err("initrd extends beyond end of memory");
105 goto disable;
106 }
107
108 size = initrd_end - initrd_start;
Zong Liac51e002020-01-02 11:12:40 +0800109 memblock_reserve(__pa_symbol(initrd_start), size);
Anup Patel0651c262019-02-21 11:25:49 +0530110 initrd_below_start_ok = 1;
111
112 pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
113 (void *)(initrd_start), size);
114 return;
115disable:
116 pr_cont(" - disabling initrd\n");
117 initrd_start = 0;
118 initrd_end = 0;
119}
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700120#endif /* CONFIG_BLK_DEV_INITRD */
Anup Patel0651c262019-02-21 11:25:49 +0530121
Albert Ou922b0372019-09-27 16:14:18 -0700122static phys_addr_t dtb_early_pa __initdata;
123
Anup Patel0651c262019-02-21 11:25:49 +0530124void __init setup_bootmem(void)
125{
126 struct memblock_region *reg;
127 phys_addr_t mem_size = 0;
Zong Liac51e002020-01-02 11:12:40 +0800128 phys_addr_t vmlinux_end = __pa_symbol(&_end);
129 phys_addr_t vmlinux_start = __pa_symbol(&_start);
Anup Patel0651c262019-02-21 11:25:49 +0530130
131 /* Find the memory region containing the kernel */
132 for_each_memblock(memory, reg) {
Anup Patel0651c262019-02-21 11:25:49 +0530133 phys_addr_t end = reg->base + reg->size;
134
Alexandre Ghitia160eed2020-02-17 00:28:47 -0500135 if (reg->base <= vmlinux_start && vmlinux_end <= end) {
Anup Patel0651c262019-02-21 11:25:49 +0530136 mem_size = min(reg->size, (phys_addr_t)-PAGE_OFFSET);
Anup Patelf05badd2019-04-05 05:49:34 +0000137
138 /*
139 * Remove memblock from the end of usable area to the
140 * end of region
141 */
142 if (reg->base + mem_size < end)
143 memblock_remove(reg->base + mem_size,
144 end - reg->base - mem_size);
Anup Patel0651c262019-02-21 11:25:49 +0530145 }
146 }
147 BUG_ON(mem_size == 0);
148
Anup Pateld90d45d2019-06-07 06:01:29 +0000149 /* Reserve from the start of the kernel to the end of the kernel */
150 memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
151
Anup Patel0651c262019-02-21 11:25:49 +0530152 set_max_mapnr(PFN_DOWN(mem_size));
153 max_low_pfn = PFN_DOWN(memblock_end_of_DRAM());
154
155#ifdef CONFIG_BLK_DEV_INITRD
156 setup_initrd();
157#endif /* CONFIG_BLK_DEV_INITRD */
158
Albert Ou922b0372019-09-27 16:14:18 -0700159 /*
160 * Avoid using early_init_fdt_reserve_self() since __pa() does
161 * not work for DTB pointers that are fixmap addresses
162 */
163 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
164
Anup Patel0651c262019-02-21 11:25:49 +0530165 early_init_fdt_scan_reserved_mem();
166 memblock_allow_resize();
167 memblock_dump_all();
168
169 for_each_memblock(memory, reg) {
170 unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
171 unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
172
173 memblock_set_node(PFN_PHYS(start_pfn),
174 PFN_PHYS(end_pfn - start_pfn),
175 &memblock.memory, 0);
176 }
177}
Anup Patel6f1e9e92019-02-13 16:38:36 +0530178
Christoph Hellwig6bd33e12019-10-28 13:10:41 +0100179#ifdef CONFIG_MMU
Anup Patel387181d2019-03-26 08:03:47 +0000180unsigned long va_pa_offset;
181EXPORT_SYMBOL(va_pa_offset);
182unsigned long pfn_base;
183EXPORT_SYMBOL(pfn_base);
184
Anup Patel6f1e9e92019-02-13 16:38:36 +0530185pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700186pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patelf2c17aa2019-01-07 20:57:01 +0530187pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700188static bool mmu_enabled;
189
190#define MAX_EARLY_MAPPING_SIZE SZ_128M
191
192pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530193
194void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
195{
196 unsigned long addr = __fix_to_virt(idx);
197 pte_t *ptep;
198
199 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
200
201 ptep = &fixmap_pte[pte_index(addr)];
202
203 if (pgprot_val(prot)) {
204 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
205 } else {
206 pte_clear(&init_mm, addr, ptep);
207 local_flush_tlb_page(addr);
208 }
209}
210
Anup Patel671f9a32019-06-28 13:36:21 -0700211static pte_t *__init get_pte_virt(phys_addr_t pa)
212{
213 if (mmu_enabled) {
214 clear_fixmap(FIX_PTE);
215 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
216 } else {
217 return (pte_t *)((uintptr_t)pa);
218 }
219}
220
221static phys_addr_t __init alloc_pte(uintptr_t va)
222{
223 /*
224 * We only create PMD or PGD early mappings so we
225 * should never reach here with MMU disabled.
226 */
227 BUG_ON(!mmu_enabled);
228
229 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
230}
231
232static void __init create_pte_mapping(pte_t *ptep,
233 uintptr_t va, phys_addr_t pa,
234 phys_addr_t sz, pgprot_t prot)
235{
236 uintptr_t pte_index = pte_index(va);
237
238 BUG_ON(sz != PAGE_SIZE);
239
240 if (pte_none(ptep[pte_index]))
241 ptep[pte_index] = pfn_pte(PFN_DOWN(pa), prot);
242}
243
244#ifndef __PAGETABLE_PMD_FOLDED
245
246pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
247pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
248
249#if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE
250#define NUM_EARLY_PMDS 1UL
251#else
252#define NUM_EARLY_PMDS (1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE)
253#endif
254pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);
255
256static pmd_t *__init get_pmd_virt(phys_addr_t pa)
257{
258 if (mmu_enabled) {
259 clear_fixmap(FIX_PMD);
260 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
261 } else {
262 return (pmd_t *)((uintptr_t)pa);
263 }
264}
265
266static phys_addr_t __init alloc_pmd(uintptr_t va)
267{
268 uintptr_t pmd_num;
269
270 if (mmu_enabled)
271 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
272
273 pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT;
274 BUG_ON(pmd_num >= NUM_EARLY_PMDS);
275 return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD];
276}
277
278static void __init create_pmd_mapping(pmd_t *pmdp,
279 uintptr_t va, phys_addr_t pa,
280 phys_addr_t sz, pgprot_t prot)
281{
282 pte_t *ptep;
283 phys_addr_t pte_phys;
284 uintptr_t pmd_index = pmd_index(va);
285
286 if (sz == PMD_SIZE) {
287 if (pmd_none(pmdp[pmd_index]))
288 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pa), prot);
289 return;
290 }
291
292 if (pmd_none(pmdp[pmd_index])) {
293 pte_phys = alloc_pte(va);
294 pmdp[pmd_index] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
295 ptep = get_pte_virt(pte_phys);
296 memset(ptep, 0, PAGE_SIZE);
297 } else {
298 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_index]));
299 ptep = get_pte_virt(pte_phys);
300 }
301
302 create_pte_mapping(ptep, va, pa, sz, prot);
303}
304
305#define pgd_next_t pmd_t
306#define alloc_pgd_next(__va) alloc_pmd(__va)
307#define get_pgd_next_virt(__pa) get_pmd_virt(__pa)
308#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
309 create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
Anup Patel671f9a32019-06-28 13:36:21 -0700310#define fixmap_pgd_next fixmap_pmd
311#else
312#define pgd_next_t pte_t
313#define alloc_pgd_next(__va) alloc_pte(__va)
314#define get_pgd_next_virt(__pa) get_pte_virt(__pa)
315#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
316 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
Anup Patel671f9a32019-06-28 13:36:21 -0700317#define fixmap_pgd_next fixmap_pte
318#endif
319
320static void __init create_pgd_mapping(pgd_t *pgdp,
321 uintptr_t va, phys_addr_t pa,
322 phys_addr_t sz, pgprot_t prot)
323{
324 pgd_next_t *nextp;
325 phys_addr_t next_phys;
326 uintptr_t pgd_index = pgd_index(va);
327
328 if (sz == PGDIR_SIZE) {
329 if (pgd_val(pgdp[pgd_index]) == 0)
330 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(pa), prot);
331 return;
332 }
333
334 if (pgd_val(pgdp[pgd_index]) == 0) {
335 next_phys = alloc_pgd_next(va);
336 pgdp[pgd_index] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
337 nextp = get_pgd_next_virt(next_phys);
338 memset(nextp, 0, PAGE_SIZE);
339 } else {
340 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_index]));
341 nextp = get_pgd_next_virt(next_phys);
342 }
343
344 create_pgd_next_mapping(nextp, va, pa, sz, prot);
345}
346
347static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
348{
Zong Li0fdc6362019-11-08 01:00:40 -0800349 /* Upgrade to PMD_SIZE mappings whenever possible */
350 if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
351 return PAGE_SIZE;
Anup Patel671f9a32019-06-28 13:36:21 -0700352
Zong Li0fdc6362019-11-08 01:00:40 -0800353 return PMD_SIZE;
Anup Patel671f9a32019-06-28 13:36:21 -0700354}
355
Anup Patel387181d2019-03-26 08:03:47 +0000356/*
357 * setup_vm() is called from head.S with MMU-off.
358 *
359 * Following requirements should be honoured for setup_vm() to work
360 * correctly:
361 * 1) It should use PC-relative addressing for accessing kernel symbols.
362 * To achieve this we always use GCC cmodel=medany.
363 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
364 * so disable compiler instrumentation when FTRACE is enabled.
365 *
366 * Currently, the above requirements are honoured by using custom CFLAGS
367 * for init.o in mm/Makefile.
368 */
369
370#ifndef __riscv_cmodel_medany
Paul Walmsley6a527b62019-10-17 14:45:58 -0700371#error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
Anup Patel387181d2019-03-26 08:03:47 +0000372#endif
373
Anup Patel671f9a32019-06-28 13:36:21 -0700374asmlinkage void __init setup_vm(uintptr_t dtb_pa)
Anup Patel6f1e9e92019-02-13 16:38:36 +0530375{
Anup Patel671f9a32019-06-28 13:36:21 -0700376 uintptr_t va, end_va;
377 uintptr_t load_pa = (uintptr_t)(&_start);
378 uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
379 uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530380
Anup Patel671f9a32019-06-28 13:36:21 -0700381 va_pa_offset = PAGE_OFFSET - load_pa;
382 pfn_base = PFN_DOWN(load_pa);
383
384 /*
385 * Enforce boot alignment requirements of RV32 and
386 * RV64 by only allowing PMD or PGD mappings.
387 */
388 BUG_ON(map_size == PAGE_SIZE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530389
390 /* Sanity check alignment and size */
391 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
Anup Patel671f9a32019-06-28 13:36:21 -0700392 BUG_ON((load_pa % map_size) != 0);
393 BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE);
394
395 /* Setup early PGD for fixmap */
396 create_pgd_mapping(early_pg_dir, FIXADDR_START,
397 (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530398
399#ifndef __PAGETABLE_PMD_FOLDED
Anup Patel671f9a32019-06-28 13:36:21 -0700400 /* Setup fixmap PMD */
401 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
402 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
403 /* Setup trampoline PGD and PMD */
404 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
405 (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
406 create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
407 load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530408#else
Anup Patel671f9a32019-06-28 13:36:21 -0700409 /* Setup trampoline PGD */
410 create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
411 load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
412#endif
Anup Patel6f1e9e92019-02-13 16:38:36 +0530413
Anup Patel671f9a32019-06-28 13:36:21 -0700414 /*
415 * Setup early PGD covering entire kernel which will allows
416 * us to reach paging_init(). We map all memory banks later
417 * in setup_vm_final() below.
418 */
419 end_va = PAGE_OFFSET + load_sz;
420 for (va = PAGE_OFFSET; va < end_va; va += map_size)
421 create_pgd_mapping(early_pg_dir, va,
422 load_pa + (va - PAGE_OFFSET),
423 map_size, PAGE_KERNEL_EXEC);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530424
Anup Patel671f9a32019-06-28 13:36:21 -0700425 /* Create fixed mapping for early FDT parsing */
426 end_va = __fix_to_virt(FIX_FDT) + FIX_FDT_SIZE;
427 for (va = __fix_to_virt(FIX_FDT); va < end_va; va += PAGE_SIZE)
428 create_pte_mapping(fixmap_pte, va,
429 dtb_pa + (va - __fix_to_virt(FIX_FDT)),
430 PAGE_SIZE, PAGE_KERNEL);
431
432 /* Save pointer to DTB for early FDT parsing */
433 dtb_early_va = (void *)fix_to_virt(FIX_FDT) + (dtb_pa & ~PAGE_MASK);
Albert Ou922b0372019-09-27 16:14:18 -0700434 /* Save physical address for memblock reservation */
435 dtb_early_pa = dtb_pa;
Anup Patel671f9a32019-06-28 13:36:21 -0700436}
437
438static void __init setup_vm_final(void)
439{
440 uintptr_t va, map_size;
441 phys_addr_t pa, start, end;
442 struct memblock_region *reg;
443
444 /* Set mmu_enabled flag */
445 mmu_enabled = true;
446
447 /* Setup swapper PGD for fixmap */
448 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
Zong Liac51e002020-01-02 11:12:40 +0800449 __pa_symbol(fixmap_pgd_next),
Anup Patel671f9a32019-06-28 13:36:21 -0700450 PGDIR_SIZE, PAGE_TABLE);
451
452 /* Map all memory banks */
453 for_each_memblock(memory, reg) {
454 start = reg->base;
455 end = start + reg->size;
456
457 if (start >= end)
458 break;
459 if (memblock_is_nomap(reg))
460 continue;
461 if (start <= __pa(PAGE_OFFSET) &&
462 __pa(PAGE_OFFSET) < end)
463 start = __pa(PAGE_OFFSET);
464
465 map_size = best_map_size(start, end - start);
466 for (pa = start; pa < end; pa += map_size) {
467 va = (uintptr_t)__va(pa);
468 create_pgd_mapping(swapper_pg_dir, va, pa,
469 map_size, PAGE_KERNEL_EXEC);
470 }
Anup Patel6f1e9e92019-02-13 16:38:36 +0530471 }
Anup Patelf2c17aa2019-01-07 20:57:01 +0530472
Anup Patel671f9a32019-06-28 13:36:21 -0700473 /* Clear fixmap PTE and PMD mappings */
474 clear_fixmap(FIX_PTE);
475 clear_fixmap(FIX_PMD);
476
477 /* Move to swapper page table */
Zong Liac51e002020-01-02 11:12:40 +0800478 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
Anup Patel671f9a32019-06-28 13:36:21 -0700479 local_flush_tlb_all();
480}
Zong Lid27c3c92020-03-10 00:55:41 +0800481
482void free_initmem(void)
483{
484 unsigned long init_begin = (unsigned long)__init_begin;
485 unsigned long init_end = (unsigned long)__init_end;
486
487 /* Make the region as non-execuatble. */
488 set_memory_nx(init_begin, (init_end - init_begin) >> PAGE_SHIFT);
489 free_initmem_default(POISON_FREE_INITMEM);
490}
491
Christoph Hellwig6bd33e12019-10-28 13:10:41 +0100492#else
493asmlinkage void __init setup_vm(uintptr_t dtb_pa)
494{
495 dtb_early_va = (void *)dtb_pa;
496}
497
498static inline void setup_vm_final(void)
499{
500}
501#endif /* CONFIG_MMU */
Anup Patel671f9a32019-06-28 13:36:21 -0700502
Zong Lid27c3c92020-03-10 00:55:41 +0800503#ifdef CONFIG_STRICT_KERNEL_RWX
504void set_kernel_text_rw(void)
505{
506 unsigned long text_start = (unsigned long)_text;
507 unsigned long text_end = (unsigned long)_etext;
508
509 set_memory_rw(text_start, (text_end - text_start) >> PAGE_SHIFT);
510}
511
512void set_kernel_text_ro(void)
513{
514 unsigned long text_start = (unsigned long)_text;
515 unsigned long text_end = (unsigned long)_etext;
516
517 set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
518}
519
520void mark_rodata_ro(void)
521{
522 unsigned long text_start = (unsigned long)_text;
523 unsigned long text_end = (unsigned long)_etext;
524 unsigned long rodata_start = (unsigned long)__start_rodata;
525 unsigned long data_start = (unsigned long)_data;
526 unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
527
528 set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
529 set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
530 set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
531 set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
532}
533#endif
534
Anup Patel671f9a32019-06-28 13:36:21 -0700535void __init paging_init(void)
536{
537 setup_vm_final();
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600538 memblocks_present();
539 sparse_init();
Anup Patel671f9a32019-06-28 13:36:21 -0700540 setup_zero_page();
541 zone_sizes_init();
Anup Patel6f1e9e92019-02-13 16:38:36 +0530542}
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600543
Kefeng Wang9fe57d82019-10-23 11:23:02 +0800544#ifdef CONFIG_SPARSEMEM_VMEMMAP
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600545int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
546 struct vmem_altmap *altmap)
547{
548 return vmemmap_populate_basepages(start, end, node);
549}
550#endif