xref: /openbmc/linux/arch/xtensa/mm/init.c (revision d003d772)
1 /*
2  * arch/xtensa/mm/init.c
3  *
4  * Derived from MIPS, PPC.
5  *
6  * This file is subject to the terms and conditions of the GNU General Public
7  * License.  See the file "COPYING" in the main directory of this archive
8  * for more details.
9  *
10  * Copyright (C) 2001 - 2005 Tensilica Inc.
11  * Copyright (C) 2014 - 2016 Cadence Design Systems Inc.
12  *
13  * Chris Zankel	<chris@zankel.net>
14  * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
15  * Marc Gauthier
16  * Kevin Chea
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/memblock.h>
22 #include <linux/gfp.h>
23 #include <linux/highmem.h>
24 #include <linux/swap.h>
25 #include <linux/mman.h>
26 #include <linux/nodemask.h>
27 #include <linux/mm.h>
28 #include <linux/of_fdt.h>
29 #include <linux/dma-contiguous.h>
30 
31 #include <asm/bootparam.h>
32 #include <asm/page.h>
33 #include <asm/sections.h>
34 #include <asm/sysmem.h>
35 
36 /*
37  * Initialize the bootmem system and give it all low memory we have available.
38  */
39 
40 void __init bootmem_init(void)
41 {
42 	/* Reserve all memory below PHYS_OFFSET, as memory
43 	 * accounting doesn't work for pages below that address.
44 	 *
45 	 * If PHYS_OFFSET is zero reserve page at address 0:
46 	 * successfull allocations should never return NULL.
47 	 */
48 	if (PHYS_OFFSET)
49 		memblock_reserve(0, PHYS_OFFSET);
50 	else
51 		memblock_reserve(0, 1);
52 
53 	early_init_fdt_scan_reserved_mem();
54 
55 	if (!memblock_phys_mem_size())
56 		panic("No memory found!\n");
57 
58 	min_low_pfn = PFN_UP(memblock_start_of_DRAM());
59 	min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
60 	max_pfn = PFN_DOWN(memblock_end_of_DRAM());
61 	max_low_pfn = min(max_pfn, MAX_LOW_PFN);
62 
63 	early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
64 		      (phys_addr_t)max_low_pfn << PAGE_SHIFT);
65 
66 	memblock_set_current_limit(PFN_PHYS(max_low_pfn));
67 	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
68 
69 	memblock_dump_all();
70 }
71 
72 
73 void __init zones_init(void)
74 {
75 	/* All pages are DMA-able, so we put them all in the DMA zone. */
76 	unsigned long zones_size[MAX_NR_ZONES] = {
77 		[ZONE_NORMAL] = max_low_pfn - ARCH_PFN_OFFSET,
78 #ifdef CONFIG_HIGHMEM
79 		[ZONE_HIGHMEM] = max_pfn - max_low_pfn,
80 #endif
81 	};
82 	free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
83 }
84 
85 #ifdef CONFIG_HIGHMEM
86 static void __init free_area_high(unsigned long pfn, unsigned long end)
87 {
88 	for (; pfn < end; pfn++)
89 		free_highmem_page(pfn_to_page(pfn));
90 }
91 
92 static void __init free_highpages(void)
93 {
94 	unsigned long max_low = max_low_pfn;
95 	struct memblock_region *mem, *res;
96 
97 	reset_all_zones_managed_pages();
98 	/* set highmem page free */
99 	for_each_memblock(memory, mem) {
100 		unsigned long start = memblock_region_memory_base_pfn(mem);
101 		unsigned long end = memblock_region_memory_end_pfn(mem);
102 
103 		/* Ignore complete lowmem entries */
104 		if (end <= max_low)
105 			continue;
106 
107 		if (memblock_is_nomap(mem))
108 			continue;
109 
110 		/* Truncate partial highmem entries */
111 		if (start < max_low)
112 			start = max_low;
113 
114 		/* Find and exclude any reserved regions */
115 		for_each_memblock(reserved, res) {
116 			unsigned long res_start, res_end;
117 
118 			res_start = memblock_region_reserved_base_pfn(res);
119 			res_end = memblock_region_reserved_end_pfn(res);
120 
121 			if (res_end < start)
122 				continue;
123 			if (res_start < start)
124 				res_start = start;
125 			if (res_start > end)
126 				res_start = end;
127 			if (res_end > end)
128 				res_end = end;
129 			if (res_start != start)
130 				free_area_high(start, res_start);
131 			start = res_end;
132 			if (start == end)
133 				break;
134 		}
135 
136 		/* And now free anything which remains */
137 		if (start < end)
138 			free_area_high(start, end);
139 	}
140 }
141 #else
142 static void __init free_highpages(void)
143 {
144 }
145 #endif
146 
147 /*
148  * Initialize memory pages.
149  */
150 
151 void __init mem_init(void)
152 {
153 	free_highpages();
154 
155 	max_mapnr = max_pfn - ARCH_PFN_OFFSET;
156 	high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
157 
158 	memblock_free_all();
159 
160 	mem_init_print_info(NULL);
161 	pr_info("virtual kernel memory layout:\n"
162 #ifdef CONFIG_KASAN
163 		"    kasan   : 0x%08lx - 0x%08lx  (%5lu MB)\n"
164 #endif
165 #ifdef CONFIG_MMU
166 		"    vmalloc : 0x%08lx - 0x%08lx  (%5lu MB)\n"
167 #endif
168 #ifdef CONFIG_HIGHMEM
169 		"    pkmap   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
170 		"    fixmap  : 0x%08lx - 0x%08lx  (%5lu kB)\n"
171 #endif
172 		"    lowmem  : 0x%08lx - 0x%08lx  (%5lu MB)\n"
173 		"    .text   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
174 		"    .rodata : 0x%08lx - 0x%08lx  (%5lu kB)\n"
175 		"    .data   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
176 		"    .init   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
177 		"    .bss    : 0x%08lx - 0x%08lx  (%5lu kB)\n",
178 #ifdef CONFIG_KASAN
179 		KASAN_SHADOW_START, KASAN_SHADOW_START + KASAN_SHADOW_SIZE,
180 		KASAN_SHADOW_SIZE >> 20,
181 #endif
182 #ifdef CONFIG_MMU
183 		VMALLOC_START, VMALLOC_END,
184 		(VMALLOC_END - VMALLOC_START) >> 20,
185 #ifdef CONFIG_HIGHMEM
186 		PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
187 		(LAST_PKMAP*PAGE_SIZE) >> 10,
188 		FIXADDR_START, FIXADDR_TOP,
189 		(FIXADDR_TOP - FIXADDR_START) >> 10,
190 #endif
191 		PAGE_OFFSET, PAGE_OFFSET +
192 		(max_low_pfn - min_low_pfn) * PAGE_SIZE,
193 #else
194 		min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
195 #endif
196 		((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20,
197 		(unsigned long)_text, (unsigned long)_etext,
198 		(unsigned long)(_etext - _text) >> 10,
199 		(unsigned long)__start_rodata, (unsigned long)_sdata,
200 		(unsigned long)(_sdata - __start_rodata) >> 10,
201 		(unsigned long)_sdata, (unsigned long)_edata,
202 		(unsigned long)(_edata - _sdata) >> 10,
203 		(unsigned long)__init_begin, (unsigned long)__init_end,
204 		(unsigned long)(__init_end - __init_begin) >> 10,
205 		(unsigned long)__bss_start, (unsigned long)__bss_stop,
206 		(unsigned long)(__bss_stop - __bss_start) >> 10);
207 }
208 
209 #ifdef CONFIG_BLK_DEV_INITRD
210 extern int initrd_is_mapped;
211 
212 void free_initrd_mem(unsigned long start, unsigned long end)
213 {
214 	if (initrd_is_mapped)
215 		free_reserved_area((void *)start, (void *)end, -1, "initrd");
216 }
217 #endif
218 
219 void free_initmem(void)
220 {
221 	free_initmem_default(-1);
222 }
223 
224 static void __init parse_memmap_one(char *p)
225 {
226 	char *oldp;
227 	unsigned long start_at, mem_size;
228 
229 	if (!p)
230 		return;
231 
232 	oldp = p;
233 	mem_size = memparse(p, &p);
234 	if (p == oldp)
235 		return;
236 
237 	switch (*p) {
238 	case '@':
239 		start_at = memparse(p + 1, &p);
240 		memblock_add(start_at, mem_size);
241 		break;
242 
243 	case '$':
244 		start_at = memparse(p + 1, &p);
245 		memblock_reserve(start_at, mem_size);
246 		break;
247 
248 	case 0:
249 		memblock_reserve(mem_size, -mem_size);
250 		break;
251 
252 	default:
253 		pr_warn("Unrecognized memmap syntax: %s\n", p);
254 		break;
255 	}
256 }
257 
258 static int __init parse_memmap_opt(char *str)
259 {
260 	while (str) {
261 		char *k = strchr(str, ',');
262 
263 		if (k)
264 			*k++ = 0;
265 
266 		parse_memmap_one(str);
267 		str = k;
268 	}
269 
270 	return 0;
271 }
272 early_param("memmap", parse_memmap_opt);
273