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