init.c 6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/*
 * arch/xtensa/mm/init.c
 *
 * Derived from MIPS, PPC.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2001 - 2005 Tensilica Inc.
11
 * Copyright (C) 2014 - 2016 Cadence Design Systems Inc.
12
13
14
15
16
17
18
19
20
 *
 * Chris Zankel	<chris@zankel.net>
 * Joe Taylor	<joe@tensilica.com, joetylr@yahoo.com>
 * Marc Gauthier
 * Kevin Chea
 */

#include <linux/kernel.h>
#include <linux/errno.h>
21
#include <linux/memblock.h>
22
#include <linux/gfp.h>
Max Filippov's avatar
Max Filippov committed
23
#include <linux/highmem.h>
24
#include <linux/swap.h>
25
26
27
#include <linux/mman.h>
#include <linux/nodemask.h>
#include <linux/mm.h>
28
#include <linux/of_fdt.h>
29
#include <linux/dma-contiguous.h>
30
31
32

#include <asm/bootparam.h>
#include <asm/page.h>
33
#include <asm/sections.h>
34
35
#include <asm/sysmem.h>

36
/*
37
 * Initialize the bootmem system and give it all low memory we have available.
38
39
40
41
 */

void __init bootmem_init(void)
{
42
	/* Reserve all memory below PHYS_OFFSET, as memory
43
44
	 * accounting doesn't work for pages below that address.
	 *
45
	 * If PHYS_OFFSET is zero reserve page at address 0:
46
47
	 * successfull allocations should never return NULL.
	 */
48
	memblock_reserve(0, PHYS_OFFSET ? PHYS_OFFSET : 1);
49

50
	early_init_fdt_scan_reserved_mem();
51

52
	if (!memblock_phys_mem_size())
53
54
		panic("No memory found!\n");

55
56
57
	min_low_pfn = PFN_UP(memblock_start_of_DRAM());
	min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
	max_pfn = PFN_DOWN(memblock_end_of_DRAM());
58
	max_low_pfn = min(max_pfn, MAX_LOW_PFN);
59

Max Filippov's avatar
Max Filippov committed
60
61
62
	early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
		      (phys_addr_t)max_low_pfn << PAGE_SHIFT);

63
	memblock_set_current_limit(PFN_PHYS(max_low_pfn));
64
	dma_contiguous_reserve(PFN_PHYS(max_low_pfn));
65

66
	memblock_dump_all();
67
68
69
}


Johannes Weiner's avatar
Johannes Weiner committed
70
void __init zones_init(void)
71
72
{
	/* All pages are DMA-able, so we put them all in the DMA zone. */
Max Filippov's avatar
Max Filippov committed
73
	unsigned long zones_size[MAX_NR_ZONES] = {
Christoph Hellwig's avatar
Christoph Hellwig committed
74
		[ZONE_NORMAL] = max_low_pfn - ARCH_PFN_OFFSET,
75
#ifdef CONFIG_HIGHMEM
Max Filippov's avatar
Max Filippov committed
76
		[ZONE_HIGHMEM] = max_pfn - max_low_pfn,
77
#endif
Max Filippov's avatar
Max Filippov committed
78
	};
79
	free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
80
81
}

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#ifdef CONFIG_HIGHMEM
static void __init free_area_high(unsigned long pfn, unsigned long end)
{
	for (; pfn < end; pfn++)
		free_highmem_page(pfn_to_page(pfn));
}

static void __init free_highpages(void)
{
	unsigned long max_low = max_low_pfn;
	struct memblock_region *mem, *res;

	reset_all_zones_managed_pages();
	/* set highmem page free */
	for_each_memblock(memory, mem) {
		unsigned long start = memblock_region_memory_base_pfn(mem);
		unsigned long end = memblock_region_memory_end_pfn(mem);

		/* Ignore complete lowmem entries */
		if (end <= max_low)
			continue;

		if (memblock_is_nomap(mem))
			continue;

		/* Truncate partial highmem entries */
		if (start < max_low)
			start = max_low;

		/* Find and exclude any reserved regions */
		for_each_memblock(reserved, res) {
			unsigned long res_start, res_end;

			res_start = memblock_region_reserved_base_pfn(res);
			res_end = memblock_region_reserved_end_pfn(res);

			if (res_end < start)
				continue;
			if (res_start < start)
				res_start = start;
			if (res_start > end)
				res_start = end;
			if (res_end > end)
				res_end = end;
			if (res_start != start)
				free_area_high(start, res_start);
			start = res_end;
			if (start == end)
				break;
		}

		/* And now free anything which remains */
		if (start < end)
			free_area_high(start, end);
	}
}
#else
static void __init free_highpages(void)
{
}
#endif

144
145
146
147
148
149
/*
 * Initialize memory pages.
 */

void __init mem_init(void)
{
150
	free_highpages();
151

Max Filippov's avatar
Max Filippov committed
152
153
154
	max_mapnr = max_pfn - ARCH_PFN_OFFSET;
	high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);

155
	memblock_free_all();
156

157
	mem_init_print_info(NULL);
Max Filippov's avatar
Max Filippov committed
158
	pr_info("virtual kernel memory layout:\n"
Max Filippov's avatar
Max Filippov committed
159
160
161
#ifdef CONFIG_KASAN
		"    kasan   : 0x%08lx - 0x%08lx  (%5lu MB)\n"
#endif
162
163
164
#ifdef CONFIG_MMU
		"    vmalloc : 0x%08lx - 0x%08lx  (%5lu MB)\n"
#endif
Max Filippov's avatar
Max Filippov committed
165
166
167
#ifdef CONFIG_HIGHMEM
		"    pkmap   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    fixmap  : 0x%08lx - 0x%08lx  (%5lu kB)\n"
168
#endif
169
170
171
172
173
174
		"    lowmem  : 0x%08lx - 0x%08lx  (%5lu MB)\n"
		"    .text   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    .rodata : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    .data   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    .init   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
		"    .bss    : 0x%08lx - 0x%08lx  (%5lu kB)\n",
Max Filippov's avatar
Max Filippov committed
175
176
177
178
#ifdef CONFIG_KASAN
		KASAN_SHADOW_START, KASAN_SHADOW_START + KASAN_SHADOW_SIZE,
		KASAN_SHADOW_SIZE >> 20,
#endif
179
180
181
#ifdef CONFIG_MMU
		VMALLOC_START, VMALLOC_END,
		(VMALLOC_END - VMALLOC_START) >> 20,
Max Filippov's avatar
Max Filippov committed
182
183
184
185
186
187
188
189
#ifdef CONFIG_HIGHMEM
		PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
		(LAST_PKMAP*PAGE_SIZE) >> 10,
		FIXADDR_START, FIXADDR_TOP,
		(FIXADDR_TOP - FIXADDR_START) >> 10,
#endif
		PAGE_OFFSET, PAGE_OFFSET +
		(max_low_pfn - min_low_pfn) * PAGE_SIZE,
190
191
192
#else
		min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
#endif
193
194
195
196
197
198
199
200
201
202
203
		((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20,
		(unsigned long)_text, (unsigned long)_etext,
		(unsigned long)(_etext - _text) >> 10,
		(unsigned long)__start_rodata, (unsigned long)_sdata,
		(unsigned long)(_sdata - __start_rodata) >> 10,
		(unsigned long)_sdata, (unsigned long)_edata,
		(unsigned long)(_edata - _sdata) >> 10,
		(unsigned long)__init_begin, (unsigned long)__init_end,
		(unsigned long)(__init_end - __init_begin) >> 10,
		(unsigned long)__bss_start, (unsigned long)__bss_stop,
		(unsigned long)(__bss_stop - __bss_start) >> 10);
204
205
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
static void __init parse_memmap_one(char *p)
{
	char *oldp;
	unsigned long start_at, mem_size;

	if (!p)
		return;

	oldp = p;
	mem_size = memparse(p, &p);
	if (p == oldp)
		return;

	switch (*p) {
	case '@':
		start_at = memparse(p + 1, &p);
222
		memblock_add(start_at, mem_size);
223
224
225
226
		break;

	case '$':
		start_at = memparse(p + 1, &p);
227
		memblock_reserve(start_at, mem_size);
228
229
230
		break;

	case 0:
231
		memblock_reserve(mem_size, -mem_size);
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
		break;

	default:
		pr_warn("Unrecognized memmap syntax: %s\n", p);
		break;
	}
}

static int __init parse_memmap_opt(char *str)
{
	while (str) {
		char *k = strchr(str, ',');

		if (k)
			*k++ = 0;

		parse_memmap_one(str);
		str = k;
	}

	return 0;
}
early_param("memmap", parse_memmap_opt);