xref: /linux/arch/arc/mm/init.c (revision 5ea5880764cbb164afb17a62e76ca75dc371409d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/mm.h>
8 #include <linux/memblock.h>
9 #ifdef CONFIG_BLK_DEV_INITRD
10 #include <linux/initrd.h>
11 #endif
12 #include <linux/of_fdt.h>
13 #include <linux/swap.h>
14 #include <linux/module.h>
15 #include <linux/highmem.h>
16 #include <asm/page.h>
17 #include <asm/sections.h>
18 #include <asm/setup.h>
19 #include <asm/arcregs.h>
20 
21 pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
22 
23 static const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
24 static unsigned long low_mem_sz;
25 
26 #ifdef CONFIG_HIGHMEM
27 static unsigned long min_high_pfn, max_high_pfn;
28 static phys_addr_t high_mem_start;
29 static phys_addr_t high_mem_sz;
30 unsigned long arch_pfn_offset;
31 EXPORT_SYMBOL(arch_pfn_offset);
32 #endif
33 
34 long __init arc_get_mem_sz(void)
35 {
36 	return low_mem_sz;
37 }
38 
39 /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
40 static int __init setup_mem_sz(char *str)
41 {
42 	low_mem_sz = memparse(str, NULL) & PAGE_MASK;
43 
44 	/* early console might not be setup yet - it will show up later */
45 	pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
46 
47 	return 0;
48 }
49 early_param("mem", setup_mem_sz);
50 
51 void __init early_init_dt_add_memory_arch(u64 base, u64 size)
52 {
53 	int in_use = 0;
54 
55 	if (!low_mem_sz) {
56 		if (base != low_mem_start)
57 			panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
58 
59 		low_mem_sz = size;
60 		in_use = 1;
61 		memblock_add_node(base, size, 0, MEMBLOCK_NONE);
62 	} else {
63 #ifdef CONFIG_HIGHMEM
64 		high_mem_start = base;
65 		high_mem_sz = size;
66 		in_use = 1;
67 		memblock_add_node(base, size, 1, MEMBLOCK_NONE);
68 		memblock_reserve(base, size);
69 #endif
70 	}
71 
72 	pr_info("Memory @ %llx [%lldM] %s\n",
73 		base, TO_MB(size), !in_use ? "Not used":"");
74 }
75 
76 void __init arch_zone_limits_init(unsigned long *max_zone_pfn)
77 {
78 	/*----------------- node/zones setup --------------------------*/
79 	max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
80 
81 #ifdef CONFIG_HIGHMEM
82 	/*
83 	 * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
84 	 * For HIGHMEM without PAE max_high_pfn should be less than
85 	 * min_low_pfn to guarantee that these two regions don't overlap.
86 	 * For PAE case highmem is greater than lowmem, so it is natural
87 	 * to use max_high_pfn.
88 	 *
89 	 * In both cases, holes should be handled by pfn_valid().
90 	 */
91 	max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
92 #endif
93 }
94 
95 /*
96  * First memory setup routine called from setup_arch()
97  * 1. setup swapper's mm @init_mm
98  * 2. Count the pages we have and setup bootmem allocator
99  * 3. zone setup
100  */
101 void __init setup_arch_memory(void)
102 {
103 	setup_initial_init_mm(_text, _etext, _edata, _end);
104 
105 	/* first page of system - kernel .vector starts here */
106 	min_low_pfn = virt_to_pfn((void *)CONFIG_LINUX_RAM_BASE);
107 
108 	/* Last usable page of low mem */
109 	max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
110 
111 	/*------------- bootmem allocator setup -----------------------*/
112 
113 	/*
114 	 * seed the bootmem allocator after any DT memory node parsing or
115 	 * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
116 	 *
117 	 * Only low mem is added, otherwise we have crashes when allocating
118 	 * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
119 	 * avail memory, ending in highmem with a > 32-bit address. However
120 	 * it then tries to memset it with a truncaed 32-bit handle, causing
121 	 * the crash
122 	 */
123 
124 	memblock_reserve(CONFIG_LINUX_LINK_BASE,
125 			 __pa(_end) - CONFIG_LINUX_LINK_BASE);
126 
127 #ifdef CONFIG_BLK_DEV_INITRD
128 	if (phys_initrd_size) {
129 		memblock_reserve(phys_initrd_start, phys_initrd_size);
130 		initrd_start = (unsigned long)__va(phys_initrd_start);
131 		initrd_end = initrd_start + phys_initrd_size;
132 	}
133 #endif
134 
135 	early_init_fdt_reserve_self();
136 	early_init_fdt_scan_reserved_mem();
137 
138 	memblock_dump_all();
139 
140 #ifdef CONFIG_HIGHMEM
141 	/*
142 	 * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
143 	 * than addresses in normal aka low memory (0x8000_0000 based).
144 	 * Even with PAE, the huge peripheral space hole would waste a lot of
145 	 * mem with single contiguous mem_map[].
146 	 * Thus when HIGHMEM on ARC is enabled the memory map corresponding
147 	 * to the hole is freed and ARC specific version of pfn_valid()
148 	 * handles the hole in the memory map.
149 	 */
150 
151 	min_high_pfn = PFN_DOWN(high_mem_start);
152 	max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
153 
154 	arch_pfn_offset = min(min_low_pfn, min_high_pfn);
155 	kmap_init();
156 #endif /* CONFIG_HIGHMEM */
157 }
158 
159 void __init arch_mm_preinit(void)
160 {
161 #ifdef CONFIG_HIGHMEM
162 	memblock_phys_free(high_mem_start, high_mem_sz);
163 #endif
164 
165 	BUILD_BUG_ON((PTRS_PER_PGD * sizeof(pgd_t)) > PAGE_SIZE);
166 	BUILD_BUG_ON((PTRS_PER_PUD * sizeof(pud_t)) > PAGE_SIZE);
167 	BUILD_BUG_ON((PTRS_PER_PMD * sizeof(pmd_t)) > PAGE_SIZE);
168 	BUILD_BUG_ON((PTRS_PER_PTE * sizeof(pte_t)) > PAGE_SIZE);
169 }
170 
171 #ifdef CONFIG_HIGHMEM
172 int pfn_valid(unsigned long pfn)
173 {
174 	return (pfn >= min_high_pfn && pfn <= max_high_pfn) ||
175 		(pfn >= min_low_pfn && pfn <= max_low_pfn);
176 }
177 EXPORT_SYMBOL(pfn_valid);
178 #endif
179