blob: 3cb4c1b6cee1d1082b7a179f5e2ebcd2b580cc11 [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.
Nick Kossifidise53d2812021-04-19 03:55:38 +03005 * Copyright (C) 2020 FORTH-ICS/CARV
6 * Nick Kossifidis <mick@ics.forth.gr>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07007 */
8
9#include <linux/init.h>
10#include <linux/mm.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070011#include <linux/memblock.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -070012#include <linux/initrd.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070013#include <linux/swap.h>
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010014#include <linux/sizes.h>
Anup Patel0651c262019-02-21 11:25:49 +053015#include <linux/of_fdt.h>
Nick Kossifidis56409752021-04-19 03:55:39 +030016#include <linux/of_reserved_mem.h>
Albert Ou922b0372019-09-27 16:14:18 -070017#include <linux/libfdt.h>
Zong Lid27c3c92020-03-10 00:55:41 +080018#include <linux/set_memory.h>
Kefeng Wangda815582020-10-31 14:01:12 +080019#include <linux/dma-map-ops.h>
Nick Kossifidise53d2812021-04-19 03:55:38 +030020#include <linux/crash_dump.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070021
Anup Patelf2c17aa2019-01-07 20:57:01 +053022#include <asm/fixmap.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070023#include <asm/tlbflush.h>
24#include <asm/sections.h>
Palmer Dabbelt2d268252020-04-14 13:43:24 +090025#include <asm/soc.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070026#include <asm/io.h>
Zong Lib422d282020-06-03 16:03:55 -070027#include <asm/ptdump.h>
Atish Patra4f0e8ee2020-11-18 16:38:29 -080028#include <asm/numa.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070029
Paul Walmsleyffaee272019-10-17 15:00:17 -070030#include "../kernel/head.h"
31
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -040032unsigned long kernel_virt_addr = KERNEL_LINK_ADDR;
33EXPORT_SYMBOL(kernel_virt_addr);
34
Anup Patel387181d2019-03-26 08:03:47 +000035unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
36 __page_aligned_bss;
37EXPORT_SYMBOL(empty_zero_page);
38
Anup Pateld90d45d2019-06-07 06:01:29 +000039extern char _start[];
Anup Patel8f3a2b42020-09-17 15:37:10 -070040#define DTB_EARLY_BASE_VA PGDIR_SIZE
41void *dtb_early_va __initdata;
42uintptr_t dtb_early_pa __initdata;
Anup Pateld90d45d2019-06-07 06:01:29 +000043
Atish Patrae8dcb612020-09-17 15:37:12 -070044struct pt_alloc_ops {
45 pte_t *(*get_pte_virt)(phys_addr_t pa);
46 phys_addr_t (*alloc_pte)(uintptr_t va);
47#ifndef __PAGETABLE_PMD_FOLDED
48 pmd_t *(*get_pmd_virt)(phys_addr_t pa);
49 phys_addr_t (*alloc_pmd)(uintptr_t va);
50#endif
51};
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070052
Kefeng Wangda815582020-10-31 14:01:12 +080053static phys_addr_t dma32_phys_limit __ro_after_init;
54
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070055static void __init zone_sizes_init(void)
56{
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010057 unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070058
Zong Lid5fad482018-06-25 16:49:37 +080059#ifdef CONFIG_ZONE_DMA32
Kefeng Wangda815582020-10-31 14:01:12 +080060 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
Zong Lid5fad482018-06-25 16:49:37 +080061#endif
Christoph Hellwig5ec9c4f2018-01-16 09:37:50 +010062 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
63
Mike Rapoport9691a072020-06-03 15:57:10 -070064 free_area_init(max_zone_pfns);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070065}
66
Jisheng Zhang19875012021-03-30 02:22:21 +080067static void __init setup_zero_page(void)
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070068{
69 memset((void *)empty_zero_page, 0, PAGE_SIZE);
70}
71
Kefeng Wang8fa3cdf2020-05-14 19:53:35 +080072#if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
Yash Shah2cc6c4a2019-11-18 05:58:34 +000073static inline void print_mlk(char *name, unsigned long b, unsigned long t)
74{
75 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld kB)\n", name, b, t,
76 (((t) - (b)) >> 10));
77}
78
79static inline void print_mlm(char *name, unsigned long b, unsigned long t)
80{
81 pr_notice("%12s : 0x%08lx - 0x%08lx (%4ld MB)\n", name, b, t,
82 (((t) - (b)) >> 20));
83}
84
Jisheng Zhang19875012021-03-30 02:22:21 +080085static void __init print_vm_layout(void)
Yash Shah2cc6c4a2019-11-18 05:58:34 +000086{
87 pr_notice("Virtual kernel memory layout:\n");
88 print_mlk("fixmap", (unsigned long)FIXADDR_START,
89 (unsigned long)FIXADDR_TOP);
90 print_mlm("pci io", (unsigned long)PCI_IO_START,
91 (unsigned long)PCI_IO_END);
92 print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
93 (unsigned long)VMEMMAP_END);
94 print_mlm("vmalloc", (unsigned long)VMALLOC_START,
95 (unsigned long)VMALLOC_END);
96 print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
97 (unsigned long)high_memory);
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -040098#ifdef CONFIG_64BIT
99 print_mlm("kernel", (unsigned long)KERNEL_LINK_ADDR,
100 (unsigned long)ADDRESS_SPACE_END);
101#endif
Yash Shah2cc6c4a2019-11-18 05:58:34 +0000102}
103#else
104static void print_vm_layout(void) { }
105#endif /* CONFIG_DEBUG_VM */
106
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700107void __init mem_init(void)
108{
109#ifdef CONFIG_FLATMEM
110 BUG_ON(!mem_map);
111#endif /* CONFIG_FLATMEM */
112
113 high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -0700114 memblock_free_all();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700115
116 mem_init_print_info(NULL);
Yash Shah2cc6c4a2019-11-18 05:58:34 +0000117 print_vm_layout();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700118}
119
Anup Patel0651c262019-02-21 11:25:49 +0530120void __init setup_bootmem(void)
121{
Zong Liac51e002020-01-02 11:12:40 +0800122 phys_addr_t vmlinux_end = __pa_symbol(&_end);
123 phys_addr_t vmlinux_start = __pa_symbol(&_start);
Kefeng Wangdd2d0822021-02-09 09:01:51 +0800124 phys_addr_t dram_end = memblock_end_of_DRAM();
Atish Patraabb8e862021-01-11 15:45:02 -0800125 phys_addr_t max_mapped_addr = __pa(~(ulong)0);
Anup Patel0651c262019-02-21 11:25:49 +0530126
Kefeng Wangdd2d0822021-02-09 09:01:51 +0800127 /* The maximal physical memory size is -PAGE_OFFSET. */
Atish Patrade043da2020-12-18 16:13:56 -0800128 memblock_enforce_memory_limit(-PAGE_OFFSET);
Anup Patel0651c262019-02-21 11:25:49 +0530129
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400130 /*
131 * Reserve from the start of the kernel to the end of the kernel
132 * and make sure we align the reservation on PMD_SIZE since we will
133 * map the kernel in the linear mapping as read-only: we do not want
134 * any allocation to happen between _end and the next pmd aligned page.
135 */
136 memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK);
Anup Pateld90d45d2019-06-07 06:01:29 +0000137
Atish Patraabb8e862021-01-11 15:45:02 -0800138 /*
139 * memblock allocator is not aware of the fact that last 4K bytes of
140 * the addressable memory can not be mapped because of IS_ERR_VALUE
141 * macro. Make sure that last 4k bytes are not usable by memblock
142 * if end of dram is equal to maximum addressable memory.
143 */
144 if (max_mapped_addr == (dram_end - 1))
145 memblock_set_current_limit(max_mapped_addr - 4096);
146
Kefeng Wangf6e5aed2021-02-25 14:54:17 +0800147 min_low_pfn = PFN_UP(memblock_start_of_DRAM());
148 max_low_pfn = max_pfn = PFN_DOWN(dram_end);
149
Kefeng Wangda815582020-10-31 14:01:12 +0800150 dma32_phys_limit = min(4UL * SZ_1G, (unsigned long)PFN_PHYS(max_low_pfn));
Guo Ren336e8eb2021-01-21 14:31:17 +0800151 set_max_mapnr(max_low_pfn - ARCH_PFN_OFFSET);
Anup Patel0651c262019-02-21 11:25:49 +0530152
Kefeng Wangaec33b52021-01-15 13:46:06 +0800153 reserve_initrd_mem();
Albert Ou922b0372019-09-27 16:14:18 -0700154 /*
Vitaly Woolf105aa92021-01-16 01:49:48 +0200155 * If DTB is built in, no need to reserve its memblock.
156 * Otherwise, do reserve it but avoid using
157 * early_init_fdt_reserve_self() since __pa() does
Albert Ou922b0372019-09-27 16:14:18 -0700158 * not work for DTB pointers that are fixmap addresses
159 */
Vitaly Woolf105aa92021-01-16 01:49:48 +0200160 if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
161 memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
Albert Ou922b0372019-09-27 16:14:18 -0700162
Anup Patel0651c262019-02-21 11:25:49 +0530163 early_init_fdt_scan_reserved_mem();
Kefeng Wangda815582020-10-31 14:01:12 +0800164 dma_contiguous_reserve(dma32_phys_limit);
Anup Patel0651c262019-02-21 11:25:49 +0530165 memblock_allow_resize();
Anup Patel0651c262019-02-21 11:25:49 +0530166}
Anup Patel6f1e9e92019-02-13 16:38:36 +0530167
Christoph Hellwig6bd33e12019-10-28 13:10:41 +0100168#ifdef CONFIG_MMU
Jisheng Zhangde31ea42021-03-30 02:22:51 +0800169static struct pt_alloc_ops pt_ops __ro_after_init;
Atish Patrae8dcb612020-09-17 15:37:12 -0700170
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400171/* Offset between linear mapping virtual address and kernel load address */
Jisheng Zhangde31ea42021-03-30 02:22:51 +0800172unsigned long va_pa_offset __ro_after_init;
Anup Patel387181d2019-03-26 08:03:47 +0000173EXPORT_SYMBOL(va_pa_offset);
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400174#ifdef CONFIG_64BIT
175/* Offset between kernel mapping virtual address and kernel load address */
176unsigned long va_kernel_pa_offset;
177EXPORT_SYMBOL(va_kernel_pa_offset);
178#endif
Jisheng Zhangde31ea42021-03-30 02:22:51 +0800179unsigned long pfn_base __ro_after_init;
Anup Patel387181d2019-03-26 08:03:47 +0000180EXPORT_SYMBOL(pfn_base);
181
Anup Patel6f1e9e92019-02-13 16:38:36 +0530182pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700183pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
Anup Patelf2c17aa2019-01-07 20:57:01 +0530184pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
Anup Patel671f9a32019-06-28 13:36:21 -0700185
Anup Patel671f9a32019-06-28 13:36:21 -0700186pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530187
188void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
189{
190 unsigned long addr = __fix_to_virt(idx);
191 pte_t *ptep;
192
193 BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
194
195 ptep = &fixmap_pte[pte_index(addr)];
196
Greentime Hu21190b72020-08-04 11:02:05 +0800197 if (pgprot_val(prot))
Anup Patelf2c17aa2019-01-07 20:57:01 +0530198 set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
Greentime Hu21190b72020-08-04 11:02:05 +0800199 else
Anup Patelf2c17aa2019-01-07 20:57:01 +0530200 pte_clear(&init_mm, addr, ptep);
Greentime Hu21190b72020-08-04 11:02:05 +0800201 local_flush_tlb_page(addr);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530202}
203
Atish Patrae8dcb612020-09-17 15:37:12 -0700204static inline pte_t *__init get_pte_virt_early(phys_addr_t pa)
Anup Patel671f9a32019-06-28 13:36:21 -0700205{
Atish Patrae8dcb612020-09-17 15:37:12 -0700206 return (pte_t *)((uintptr_t)pa);
Anup Patel671f9a32019-06-28 13:36:21 -0700207}
208
Atish Patrae8dcb612020-09-17 15:37:12 -0700209static inline pte_t *__init get_pte_virt_fixmap(phys_addr_t pa)
210{
211 clear_fixmap(FIX_PTE);
212 return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
213}
214
215static inline pte_t *get_pte_virt_late(phys_addr_t pa)
216{
217 return (pte_t *) __va(pa);
218}
219
220static inline phys_addr_t __init alloc_pte_early(uintptr_t va)
Anup Patel671f9a32019-06-28 13:36:21 -0700221{
222 /*
223 * We only create PMD or PGD early mappings so we
224 * should never reach here with MMU disabled.
225 */
Atish Patrae8dcb612020-09-17 15:37:12 -0700226 BUG();
227}
Anup Patel671f9a32019-06-28 13:36:21 -0700228
Atish Patrae8dcb612020-09-17 15:37:12 -0700229static inline phys_addr_t __init alloc_pte_fixmap(uintptr_t va)
230{
Anup Patel671f9a32019-06-28 13:36:21 -0700231 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
232}
233
Atish Patrae8dcb612020-09-17 15:37:12 -0700234static phys_addr_t alloc_pte_late(uintptr_t va)
235{
236 unsigned long vaddr;
237
238 vaddr = __get_free_page(GFP_KERNEL);
zhouchuangaoe75e6bf2021-03-30 06:56:26 -0700239 BUG_ON(!vaddr || !pgtable_pte_page_ctor(virt_to_page(vaddr)));
240
Atish Patrae8dcb612020-09-17 15:37:12 -0700241 return __pa(vaddr);
242}
243
Anup Patel671f9a32019-06-28 13:36:21 -0700244static void __init create_pte_mapping(pte_t *ptep,
245 uintptr_t va, phys_addr_t pa,
246 phys_addr_t sz, pgprot_t prot)
247{
Mike Rapoport974b9b22020-06-08 21:33:10 -0700248 uintptr_t pte_idx = pte_index(va);
Anup Patel671f9a32019-06-28 13:36:21 -0700249
250 BUG_ON(sz != PAGE_SIZE);
251
Mike Rapoport974b9b22020-06-08 21:33:10 -0700252 if (pte_none(ptep[pte_idx]))
253 ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
Anup Patel671f9a32019-06-28 13:36:21 -0700254}
255
256#ifndef __PAGETABLE_PMD_FOLDED
257
258pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
259pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
Alexandre Ghiti0f02de42021-02-21 09:22:33 -0500260pmd_t early_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
Anup Patel1074dd42020-11-04 12:07:13 +0530261pmd_t early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAGE_SIZE);
Anup Patel671f9a32019-06-28 13:36:21 -0700262
Atish Patrae8dcb612020-09-17 15:37:12 -0700263static pmd_t *__init get_pmd_virt_early(phys_addr_t pa)
Anup Patel671f9a32019-06-28 13:36:21 -0700264{
Atish Patrae8dcb612020-09-17 15:37:12 -0700265 /* Before MMU is enabled */
266 return (pmd_t *)((uintptr_t)pa);
Anup Patel671f9a32019-06-28 13:36:21 -0700267}
268
Atish Patrae8dcb612020-09-17 15:37:12 -0700269static pmd_t *__init get_pmd_virt_fixmap(phys_addr_t pa)
270{
271 clear_fixmap(FIX_PMD);
272 return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
273}
274
275static pmd_t *get_pmd_virt_late(phys_addr_t pa)
276{
277 return (pmd_t *) __va(pa);
278}
279
280static phys_addr_t __init alloc_pmd_early(uintptr_t va)
Anup Patel671f9a32019-06-28 13:36:21 -0700281{
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400282 BUG_ON((va - kernel_virt_addr) >> PGDIR_SHIFT);
Anup Patel671f9a32019-06-28 13:36:21 -0700283
Alexandre Ghiti0f02de42021-02-21 09:22:33 -0500284 return (uintptr_t)early_pmd;
Anup Patel671f9a32019-06-28 13:36:21 -0700285}
286
Atish Patrae8dcb612020-09-17 15:37:12 -0700287static phys_addr_t __init alloc_pmd_fixmap(uintptr_t va)
288{
289 return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
290}
291
292static phys_addr_t alloc_pmd_late(uintptr_t va)
293{
294 unsigned long vaddr;
295
296 vaddr = __get_free_page(GFP_KERNEL);
297 BUG_ON(!vaddr);
298 return __pa(vaddr);
299}
300
Anup Patel671f9a32019-06-28 13:36:21 -0700301static void __init create_pmd_mapping(pmd_t *pmdp,
302 uintptr_t va, phys_addr_t pa,
303 phys_addr_t sz, pgprot_t prot)
304{
305 pte_t *ptep;
306 phys_addr_t pte_phys;
Mike Rapoport974b9b22020-06-08 21:33:10 -0700307 uintptr_t pmd_idx = pmd_index(va);
Anup Patel671f9a32019-06-28 13:36:21 -0700308
309 if (sz == PMD_SIZE) {
Mike Rapoport974b9b22020-06-08 21:33:10 -0700310 if (pmd_none(pmdp[pmd_idx]))
311 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
Anup Patel671f9a32019-06-28 13:36:21 -0700312 return;
313 }
314
Mike Rapoport974b9b22020-06-08 21:33:10 -0700315 if (pmd_none(pmdp[pmd_idx])) {
Atish Patrae8dcb612020-09-17 15:37:12 -0700316 pte_phys = pt_ops.alloc_pte(va);
Mike Rapoport974b9b22020-06-08 21:33:10 -0700317 pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
Atish Patrae8dcb612020-09-17 15:37:12 -0700318 ptep = pt_ops.get_pte_virt(pte_phys);
Anup Patel671f9a32019-06-28 13:36:21 -0700319 memset(ptep, 0, PAGE_SIZE);
320 } else {
Mike Rapoport974b9b22020-06-08 21:33:10 -0700321 pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
Atish Patrae8dcb612020-09-17 15:37:12 -0700322 ptep = pt_ops.get_pte_virt(pte_phys);
Anup Patel671f9a32019-06-28 13:36:21 -0700323 }
324
325 create_pte_mapping(ptep, va, pa, sz, prot);
326}
327
328#define pgd_next_t pmd_t
Atish Patrae8dcb612020-09-17 15:37:12 -0700329#define alloc_pgd_next(__va) pt_ops.alloc_pmd(__va)
330#define get_pgd_next_virt(__pa) pt_ops.get_pmd_virt(__pa)
Anup Patel671f9a32019-06-28 13:36:21 -0700331#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
332 create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
Anup Patel671f9a32019-06-28 13:36:21 -0700333#define fixmap_pgd_next fixmap_pmd
334#else
335#define pgd_next_t pte_t
Atish Patrae8dcb612020-09-17 15:37:12 -0700336#define alloc_pgd_next(__va) pt_ops.alloc_pte(__va)
337#define get_pgd_next_virt(__pa) pt_ops.get_pte_virt(__pa)
Anup Patel671f9a32019-06-28 13:36:21 -0700338#define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot) \
339 create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
Anup Patel671f9a32019-06-28 13:36:21 -0700340#define fixmap_pgd_next fixmap_pte
341#endif
342
Atish Patrab91540d2020-09-17 15:37:15 -0700343void __init create_pgd_mapping(pgd_t *pgdp,
Anup Patel671f9a32019-06-28 13:36:21 -0700344 uintptr_t va, phys_addr_t pa,
345 phys_addr_t sz, pgprot_t prot)
346{
347 pgd_next_t *nextp;
348 phys_addr_t next_phys;
Mike Rapoport974b9b22020-06-08 21:33:10 -0700349 uintptr_t pgd_idx = pgd_index(va);
Anup Patel671f9a32019-06-28 13:36:21 -0700350
351 if (sz == PGDIR_SIZE) {
Mike Rapoport974b9b22020-06-08 21:33:10 -0700352 if (pgd_val(pgdp[pgd_idx]) == 0)
353 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
Anup Patel671f9a32019-06-28 13:36:21 -0700354 return;
355 }
356
Mike Rapoport974b9b22020-06-08 21:33:10 -0700357 if (pgd_val(pgdp[pgd_idx]) == 0) {
Anup Patel671f9a32019-06-28 13:36:21 -0700358 next_phys = alloc_pgd_next(va);
Mike Rapoport974b9b22020-06-08 21:33:10 -0700359 pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
Anup Patel671f9a32019-06-28 13:36:21 -0700360 nextp = get_pgd_next_virt(next_phys);
361 memset(nextp, 0, PAGE_SIZE);
362 } else {
Mike Rapoport974b9b22020-06-08 21:33:10 -0700363 next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
Anup Patel671f9a32019-06-28 13:36:21 -0700364 nextp = get_pgd_next_virt(next_phys);
365 }
366
367 create_pgd_next_mapping(nextp, va, pa, sz, prot);
368}
369
370static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
371{
Zong Li0fdc6362019-11-08 01:00:40 -0800372 /* Upgrade to PMD_SIZE mappings whenever possible */
373 if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
374 return PAGE_SIZE;
Anup Patel671f9a32019-06-28 13:36:21 -0700375
Zong Li0fdc6362019-11-08 01:00:40 -0800376 return PMD_SIZE;
Anup Patel671f9a32019-06-28 13:36:21 -0700377}
378
Anup Patel387181d2019-03-26 08:03:47 +0000379/*
380 * setup_vm() is called from head.S with MMU-off.
381 *
382 * Following requirements should be honoured for setup_vm() to work
383 * correctly:
384 * 1) It should use PC-relative addressing for accessing kernel symbols.
385 * To achieve this we always use GCC cmodel=medany.
386 * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
387 * so disable compiler instrumentation when FTRACE is enabled.
388 *
389 * Currently, the above requirements are honoured by using custom CFLAGS
390 * for init.o in mm/Makefile.
391 */
392
393#ifndef __riscv_cmodel_medany
Paul Walmsley6a527b62019-10-17 14:45:58 -0700394#error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
Anup Patel387181d2019-03-26 08:03:47 +0000395#endif
396
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400397uintptr_t load_pa, load_sz;
398
399static void __init create_kernel_page_table(pgd_t *pgdir, uintptr_t map_size)
400{
401 uintptr_t va, end_va;
402
403 end_va = kernel_virt_addr + load_sz;
404 for (va = kernel_virt_addr; va < end_va; va += map_size)
405 create_pgd_mapping(pgdir, va,
406 load_pa + (va - kernel_virt_addr),
407 map_size, PAGE_KERNEL_EXEC);
408}
409
Anup Patel671f9a32019-06-28 13:36:21 -0700410asmlinkage void __init setup_vm(uintptr_t dtb_pa)
Anup Patel6f1e9e92019-02-13 16:38:36 +0530411{
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400412 uintptr_t pa;
Alexandre Ghiti0f02de42021-02-21 09:22:33 -0500413 uintptr_t map_size;
Atish Patra6262f662020-09-17 15:37:11 -0700414#ifndef __PAGETABLE_PMD_FOLDED
415 pmd_t fix_bmap_spmd, fix_bmap_epmd;
416#endif
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400417 load_pa = (uintptr_t)(&_start);
418 load_sz = (uintptr_t)(&_end) - load_pa;
Anup Patel6f1e9e92019-02-13 16:38:36 +0530419
Anup Patel671f9a32019-06-28 13:36:21 -0700420 va_pa_offset = PAGE_OFFSET - load_pa;
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400421#ifdef CONFIG_64BIT
422 va_kernel_pa_offset = kernel_virt_addr - load_pa;
423#endif
424
Anup Patel671f9a32019-06-28 13:36:21 -0700425 pfn_base = PFN_DOWN(load_pa);
426
427 /*
428 * Enforce boot alignment requirements of RV32 and
429 * RV64 by only allowing PMD or PGD mappings.
430 */
Alexandre Ghiti0f02de42021-02-21 09:22:33 -0500431 map_size = PMD_SIZE;
Anup Patel6f1e9e92019-02-13 16:38:36 +0530432
433 /* Sanity check alignment and size */
434 BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
Anup Patel671f9a32019-06-28 13:36:21 -0700435 BUG_ON((load_pa % map_size) != 0);
Anup Patel671f9a32019-06-28 13:36:21 -0700436
Atish Patrae8dcb612020-09-17 15:37:12 -0700437 pt_ops.alloc_pte = alloc_pte_early;
438 pt_ops.get_pte_virt = get_pte_virt_early;
439#ifndef __PAGETABLE_PMD_FOLDED
440 pt_ops.alloc_pmd = alloc_pmd_early;
441 pt_ops.get_pmd_virt = get_pmd_virt_early;
442#endif
Anup Patel671f9a32019-06-28 13:36:21 -0700443 /* Setup early PGD for fixmap */
444 create_pgd_mapping(early_pg_dir, FIXADDR_START,
445 (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530446
447#ifndef __PAGETABLE_PMD_FOLDED
Anup Patel671f9a32019-06-28 13:36:21 -0700448 /* Setup fixmap PMD */
449 create_pmd_mapping(fixmap_pmd, FIXADDR_START,
450 (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
451 /* Setup trampoline PGD and PMD */
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400452 create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
Anup Patel671f9a32019-06-28 13:36:21 -0700453 (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400454 create_pmd_mapping(trampoline_pmd, kernel_virt_addr,
Anup Patel671f9a32019-06-28 13:36:21 -0700455 load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
Anup Patel6f1e9e92019-02-13 16:38:36 +0530456#else
Anup Patel671f9a32019-06-28 13:36:21 -0700457 /* Setup trampoline PGD */
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400458 create_pgd_mapping(trampoline_pg_dir, kernel_virt_addr,
Anup Patel671f9a32019-06-28 13:36:21 -0700459 load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
460#endif
Anup Patel6f1e9e92019-02-13 16:38:36 +0530461
Anup Patel671f9a32019-06-28 13:36:21 -0700462 /*
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400463 * Setup early PGD covering entire kernel which will allow
Anup Patel671f9a32019-06-28 13:36:21 -0700464 * us to reach paging_init(). We map all memory banks later
465 * in setup_vm_final() below.
466 */
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400467 create_kernel_page_table(early_pg_dir, map_size);
Anup Patelf2c17aa2019-01-07 20:57:01 +0530468
Anup Patel1074dd42020-11-04 12:07:13 +0530469#ifndef __PAGETABLE_PMD_FOLDED
470 /* Setup early PMD for DTB */
471 create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
472 (uintptr_t)early_dtb_pmd, PGDIR_SIZE, PAGE_TABLE);
Vitaly Woolf105aa92021-01-16 01:49:48 +0200473#ifndef CONFIG_BUILTIN_DTB
Anup Patel1074dd42020-11-04 12:07:13 +0530474 /* Create two consecutive PMD mappings for FDT early scan */
475 pa = dtb_pa & ~(PMD_SIZE - 1);
476 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA,
477 pa, PMD_SIZE, PAGE_KERNEL);
478 create_pmd_mapping(early_dtb_pmd, DTB_EARLY_BASE_VA + PMD_SIZE,
479 pa + PMD_SIZE, PMD_SIZE, PAGE_KERNEL);
480 dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PMD_SIZE - 1));
Vitaly Woolf105aa92021-01-16 01:49:48 +0200481#else /* CONFIG_BUILTIN_DTB */
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400482#ifdef CONFIG_64BIT
483 /*
484 * __va can't be used since it would return a linear mapping address
485 * whereas dtb_early_va will be used before setup_vm_final installs
486 * the linear mapping.
487 */
488 dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
489#else
Vitaly Woolf105aa92021-01-16 01:49:48 +0200490 dtb_early_va = __va(dtb_pa);
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400491#endif /* CONFIG_64BIT */
Vitaly Woolf105aa92021-01-16 01:49:48 +0200492#endif /* CONFIG_BUILTIN_DTB */
Anup Patel1074dd42020-11-04 12:07:13 +0530493#else
Vitaly Woolf105aa92021-01-16 01:49:48 +0200494#ifndef CONFIG_BUILTIN_DTB
Anup Patel8f3a2b42020-09-17 15:37:10 -0700495 /* Create two consecutive PGD mappings for FDT early scan */
496 pa = dtb_pa & ~(PGDIR_SIZE - 1);
497 create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
498 pa, PGDIR_SIZE, PAGE_KERNEL);
499 create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE,
500 pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL);
501 dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
Vitaly Woolf105aa92021-01-16 01:49:48 +0200502#else /* CONFIG_BUILTIN_DTB */
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400503#ifdef CONFIG_64BIT
504 dtb_early_va = kernel_mapping_pa_to_va(dtb_pa);
505#else
Vitaly Woolf105aa92021-01-16 01:49:48 +0200506 dtb_early_va = __va(dtb_pa);
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400507#endif /* CONFIG_64BIT */
Vitaly Woolf105aa92021-01-16 01:49:48 +0200508#endif /* CONFIG_BUILTIN_DTB */
Anup Patel1074dd42020-11-04 12:07:13 +0530509#endif
Albert Ou922b0372019-09-27 16:14:18 -0700510 dtb_early_pa = dtb_pa;
Atish Patra6262f662020-09-17 15:37:11 -0700511
512 /*
513 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
514 * range can not span multiple pmds.
515 */
516 BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
517 != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
518
519#ifndef __PAGETABLE_PMD_FOLDED
520 /*
521 * Early ioremap fixmap is already created as it lies within first 2MB
522 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
523 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
524 * the user if not.
525 */
526 fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
527 fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
528 if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
529 WARN_ON(1);
530 pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
531 pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
532 pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
533 fix_to_virt(FIX_BTMAP_BEGIN));
534 pr_warn("fix_to_virt(FIX_BTMAP_END): %08lx\n",
535 fix_to_virt(FIX_BTMAP_END));
536
537 pr_warn("FIX_BTMAP_END: %d\n", FIX_BTMAP_END);
538 pr_warn("FIX_BTMAP_BEGIN: %d\n", FIX_BTMAP_BEGIN);
539 }
540#endif
Anup Patel671f9a32019-06-28 13:36:21 -0700541}
542
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400543#ifdef CONFIG_64BIT
544void protect_kernel_linear_mapping_text_rodata(void)
545{
546 unsigned long text_start = (unsigned long)lm_alias(_start);
547 unsigned long init_text_start = (unsigned long)lm_alias(__init_text_begin);
548 unsigned long rodata_start = (unsigned long)lm_alias(__start_rodata);
549 unsigned long data_start = (unsigned long)lm_alias(_data);
550
551 set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
552 set_memory_nx(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
553
554 set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
555 set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
556}
557#endif
558
Anup Patel671f9a32019-06-28 13:36:21 -0700559static void __init setup_vm_final(void)
560{
561 uintptr_t va, map_size;
562 phys_addr_t pa, start, end;
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700563 u64 i;
Anup Patel671f9a32019-06-28 13:36:21 -0700564
Atish Patrae8dcb612020-09-17 15:37:12 -0700565 /**
566 * MMU is enabled at this point. But page table setup is not complete yet.
567 * fixmap page table alloc functions should be used at this point
568 */
569 pt_ops.alloc_pte = alloc_pte_fixmap;
570 pt_ops.get_pte_virt = get_pte_virt_fixmap;
571#ifndef __PAGETABLE_PMD_FOLDED
572 pt_ops.alloc_pmd = alloc_pmd_fixmap;
573 pt_ops.get_pmd_virt = get_pmd_virt_fixmap;
574#endif
Anup Patel671f9a32019-06-28 13:36:21 -0700575 /* Setup swapper PGD for fixmap */
576 create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
Zong Liac51e002020-01-02 11:12:40 +0800577 __pa_symbol(fixmap_pgd_next),
Anup Patel671f9a32019-06-28 13:36:21 -0700578 PGDIR_SIZE, PAGE_TABLE);
579
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400580 /* Map all memory banks in the linear mapping */
Mike Rapoportb10d6bc2020-10-13 16:58:08 -0700581 for_each_mem_range(i, &start, &end) {
Anup Patel671f9a32019-06-28 13:36:21 -0700582 if (start >= end)
583 break;
Anup Patel671f9a32019-06-28 13:36:21 -0700584 if (start <= __pa(PAGE_OFFSET) &&
585 __pa(PAGE_OFFSET) < end)
586 start = __pa(PAGE_OFFSET);
587
588 map_size = best_map_size(start, end - start);
589 for (pa = start; pa < end; pa += map_size) {
590 va = (uintptr_t)__va(pa);
591 create_pgd_mapping(swapper_pg_dir, va, pa,
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400592 map_size,
593#ifdef CONFIG_64BIT
594 PAGE_KERNEL
595#else
596 PAGE_KERNEL_EXEC
597#endif
598 );
599
Anup Patel671f9a32019-06-28 13:36:21 -0700600 }
Anup Patel6f1e9e92019-02-13 16:38:36 +0530601 }
Anup Patelf2c17aa2019-01-07 20:57:01 +0530602
Alexandre Ghiti2bfc6cd2021-04-11 12:41:44 -0400603#ifdef CONFIG_64BIT
604 /* Map the kernel */
605 create_kernel_page_table(swapper_pg_dir, PMD_SIZE);
606#endif
607
Anup Patel671f9a32019-06-28 13:36:21 -0700608 /* Clear fixmap PTE and PMD mappings */
609 clear_fixmap(FIX_PTE);
610 clear_fixmap(FIX_PMD);
611
612 /* Move to swapper page table */
Zong Liac51e002020-01-02 11:12:40 +0800613 csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
Anup Patel671f9a32019-06-28 13:36:21 -0700614 local_flush_tlb_all();
Atish Patrae8dcb612020-09-17 15:37:12 -0700615
616 /* generic page allocation functions must be used to setup page table */
617 pt_ops.alloc_pte = alloc_pte_late;
618 pt_ops.get_pte_virt = get_pte_virt_late;
619#ifndef __PAGETABLE_PMD_FOLDED
620 pt_ops.alloc_pmd = alloc_pmd_late;
621 pt_ops.get_pmd_virt = get_pmd_virt_late;
622#endif
Anup Patel671f9a32019-06-28 13:36:21 -0700623}
Christoph Hellwig6bd33e12019-10-28 13:10:41 +0100624#else
625asmlinkage void __init setup_vm(uintptr_t dtb_pa)
626{
627 dtb_early_va = (void *)dtb_pa;
Atish Patraa78c6f52020-10-01 12:04:56 -0700628 dtb_early_pa = dtb_pa;
Christoph Hellwig6bd33e12019-10-28 13:10:41 +0100629}
630
631static inline void setup_vm_final(void)
632{
633}
634#endif /* CONFIG_MMU */
Anup Patel671f9a32019-06-28 13:36:21 -0700635
Zong Lid27c3c92020-03-10 00:55:41 +0800636#ifdef CONFIG_STRICT_KERNEL_RWX
Jisheng Zhang19875012021-03-30 02:22:21 +0800637void __init protect_kernel_text_data(void)
Zong Lid27c3c92020-03-10 00:55:41 +0800638{
Atish Patra19a00862020-11-04 16:04:38 -0800639 unsigned long text_start = (unsigned long)_start;
640 unsigned long init_text_start = (unsigned long)__init_text_begin;
641 unsigned long init_data_start = (unsigned long)__init_data_begin;
Zong Lid27c3c92020-03-10 00:55:41 +0800642 unsigned long rodata_start = (unsigned long)__start_rodata;
643 unsigned long data_start = (unsigned long)_data;
644 unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
645
Atish Patra19a00862020-11-04 16:04:38 -0800646 set_memory_ro(text_start, (init_text_start - text_start) >> PAGE_SHIFT);
647 set_memory_ro(init_text_start, (init_data_start - init_text_start) >> PAGE_SHIFT);
648 set_memory_nx(init_data_start, (rodata_start - init_data_start) >> PAGE_SHIFT);
649 /* rodata section is marked readonly in mark_rodata_ro */
Zong Lid27c3c92020-03-10 00:55:41 +0800650 set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
651 set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
Atish Patra19a00862020-11-04 16:04:38 -0800652}
653
654void mark_rodata_ro(void)
655{
656 unsigned long rodata_start = (unsigned long)__start_rodata;
657 unsigned long data_start = (unsigned long)_data;
658
659 set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
Zong Lib422d282020-06-03 16:03:55 -0700660
661 debug_checkwx();
Zong Lid27c3c92020-03-10 00:55:41 +0800662}
663#endif
664
Nick Kossifidise53d2812021-04-19 03:55:38 +0300665#ifdef CONFIG_KEXEC_CORE
666/*
667 * reserve_crashkernel() - reserves memory for crash kernel
668 *
669 * This function reserves memory area given in "crashkernel=" kernel command
670 * line parameter. The memory reserved is used by dump capture kernel when
671 * primary kernel is crashing.
672 */
673static void __init reserve_crashkernel(void)
674{
675 unsigned long long crash_base = 0;
676 unsigned long long crash_size = 0;
677 unsigned long search_start = memblock_start_of_DRAM();
678 unsigned long search_end = memblock_end_of_DRAM();
679
680 int ret = 0;
681
Nick Kossifidis56409752021-04-19 03:55:39 +0300682 /*
683 * Don't reserve a region for a crash kernel on a crash kernel
684 * since it doesn't make much sense and we have limited memory
685 * resources.
686 */
687#ifdef CONFIG_CRASH_DUMP
688 if (is_kdump_kernel()) {
689 pr_info("crashkernel: ignoring reservation request\n");
690 return;
691 }
692#endif
693
Nick Kossifidise53d2812021-04-19 03:55:38 +0300694 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
695 &crash_size, &crash_base);
696 if (ret || !crash_size)
697 return;
698
699 crash_size = PAGE_ALIGN(crash_size);
700
701 if (crash_base == 0) {
702 /*
703 * Current riscv boot protocol requires 2MB alignment for
704 * RV64 and 4MB alignment for RV32 (hugepage size)
705 */
706 crash_base = memblock_find_in_range(search_start, search_end,
707 crash_size, PMD_SIZE);
708
709 if (crash_base == 0) {
710 pr_warn("crashkernel: couldn't allocate %lldKB\n",
711 crash_size >> 10);
712 return;
713 }
714 } else {
715 /* User specifies base address explicitly. */
716 if (!memblock_is_region_memory(crash_base, crash_size)) {
717 pr_warn("crashkernel: requested region is not memory\n");
718 return;
719 }
720
721 if (memblock_is_region_reserved(crash_base, crash_size)) {
722 pr_warn("crashkernel: requested region is reserved\n");
723 return;
724 }
725
726
727 if (!IS_ALIGNED(crash_base, PMD_SIZE)) {
728 pr_warn("crashkernel: requested region is misaligned\n");
729 return;
730 }
731 }
732 memblock_reserve(crash_base, crash_size);
733
734 pr_info("crashkernel: reserved 0x%016llx - 0x%016llx (%lld MB)\n",
735 crash_base, crash_base + crash_size, crash_size >> 20);
736
737 crashk_res.start = crash_base;
738 crashk_res.end = crash_base + crash_size - 1;
739}
740#endif /* CONFIG_KEXEC_CORE */
741
Nick Kossifidis56409752021-04-19 03:55:39 +0300742#ifdef CONFIG_CRASH_DUMP
743/*
744 * We keep track of the ELF core header of the crashed
745 * kernel with a reserved-memory region with compatible
746 * string "linux,elfcorehdr". Here we register a callback
747 * to populate elfcorehdr_addr/size when this region is
748 * present. Note that this region will be marked as
749 * reserved once we call early_init_fdt_scan_reserved_mem()
750 * later on.
751 */
752static int elfcore_hdr_setup(struct reserved_mem *rmem)
753{
754 elfcorehdr_addr = rmem->base;
755 elfcorehdr_size = rmem->size;
756 return 0;
757}
758
759RESERVEDMEM_OF_DECLARE(elfcorehdr, "linux,elfcorehdr", elfcore_hdr_setup);
760#endif
761
Anup Patel671f9a32019-06-28 13:36:21 -0700762void __init paging_init(void)
763{
764 setup_vm_final();
765 setup_zero_page();
Atish Patracbd34f42020-11-18 16:38:27 -0800766}
767
768void __init misc_mem_init(void)
769{
Kefeng Wangf6e5aed2021-02-25 14:54:17 +0800770 early_memtest(min_low_pfn << PAGE_SHIFT, max_low_pfn << PAGE_SHIFT);
Atish Patra4f0e8ee2020-11-18 16:38:29 -0800771 arch_numa_init();
Atish Patracbd34f42020-11-18 16:38:27 -0800772 sparse_init();
Anup Patel671f9a32019-06-28 13:36:21 -0700773 zone_sizes_init();
Nick Kossifidise53d2812021-04-19 03:55:38 +0300774#ifdef CONFIG_KEXEC_CORE
775 reserve_crashkernel();
776#endif
Atish Patra4f0e8ee2020-11-18 16:38:29 -0800777 memblock_dump_all();
Anup Patel6f1e9e92019-02-13 16:38:36 +0530778}
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600779
Kefeng Wang9fe57d82019-10-23 11:23:02 +0800780#ifdef CONFIG_SPARSEMEM_VMEMMAP
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600781int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
782 struct vmem_altmap *altmap)
783{
Anshuman Khandual1d9cfee2020-08-06 23:23:19 -0700784 return vmemmap_populate_basepages(start, end, node, NULL);
Logan Gunthorped95f1a52019-08-28 15:40:54 -0600785}
786#endif