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-map-ops.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 
bootmem_init(void)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 	memblock_reserve(0, PHYS_OFFSET ? PHYS_OFFSET : 1);
49 
50 	early_init_fdt_scan_reserved_mem();
51 
52 	if (!memblock_phys_mem_size())
53 		panic("No memory found!\n");
54 
55 	min_low_pfn = PFN_UP(memblock_start_of_DRAM());
56 	min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
57 	max_pfn = PFN_DOWN(memblock_end_of_DRAM());
58 	max_low_pfn = min(max_pfn, MAX_LOW_PFN);
59 
60 	early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
61 		      (phys_addr_t)max_low_pfn << PAGE_SHIFT);
62 
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 
print_vm_layout(void)69 static void __init print_vm_layout(void)
70 {
71 	pr_info("virtual kernel memory layout:\n"
72 #ifdef CONFIG_KASAN
73 		"    kasan   : 0x%08lx - 0x%08lx  (%5lu MB)\n"
74 #endif
75 #ifdef CONFIG_MMU
76 		"    vmalloc : 0x%08lx - 0x%08lx  (%5lu MB)\n"
77 #endif
78 #ifdef CONFIG_HIGHMEM
79 		"    pkmap   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
80 		"    fixmap  : 0x%08lx - 0x%08lx  (%5lu kB)\n"
81 #endif
82 		"    lowmem  : 0x%08lx - 0x%08lx  (%5lu MB)\n"
83 		"    .text   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
84 		"    .rodata : 0x%08lx - 0x%08lx  (%5lu kB)\n"
85 		"    .data   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
86 		"    .init   : 0x%08lx - 0x%08lx  (%5lu kB)\n"
87 		"    .bss    : 0x%08lx - 0x%08lx  (%5lu kB)\n",
88 #ifdef CONFIG_KASAN
89 		KASAN_SHADOW_START, KASAN_SHADOW_START + KASAN_SHADOW_SIZE,
90 		KASAN_SHADOW_SIZE >> 20,
91 #endif
92 #ifdef CONFIG_MMU
93 		VMALLOC_START, VMALLOC_END,
94 		(VMALLOC_END - VMALLOC_START) >> 20,
95 #ifdef CONFIG_HIGHMEM
96 		PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
97 		(LAST_PKMAP*PAGE_SIZE) >> 10,
98 		FIXADDR_START, FIXADDR_END,
99 		(FIXADDR_END - FIXADDR_START) >> 10,
100 #endif
101 		PAGE_OFFSET, PAGE_OFFSET +
102 		(max_low_pfn - min_low_pfn) * PAGE_SIZE,
103 #else
104 		min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
105 #endif
106 		((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20,
107 		(unsigned long)_text, (unsigned long)_etext,
108 		(unsigned long)(_etext - _text) >> 10,
109 		(unsigned long)__start_rodata, (unsigned long)__end_rodata,
110 		(unsigned long)(__end_rodata - __start_rodata) >> 10,
111 		(unsigned long)_sdata, (unsigned long)_edata,
112 		(unsigned long)(_edata - _sdata) >> 10,
113 		(unsigned long)__init_begin, (unsigned long)__init_end,
114 		(unsigned long)(__init_end - __init_begin) >> 10,
115 		(unsigned long)__bss_start, (unsigned long)__bss_stop,
116 		(unsigned long)(__bss_stop - __bss_start) >> 10);
117 }
118 
zones_init(void)119 void __init zones_init(void)
120 {
121 	/* All pages are DMA-able, so we put them all in the DMA zone. */
122 	unsigned long max_zone_pfn[MAX_NR_ZONES] = {
123 		[ZONE_NORMAL] = max_low_pfn,
124 #ifdef CONFIG_HIGHMEM
125 		[ZONE_HIGHMEM] = max_pfn,
126 #endif
127 	};
128 	free_area_init(max_zone_pfn);
129 	print_vm_layout();
130 }
131 
parse_memmap_one(char * p)132 static void __init parse_memmap_one(char *p)
133 {
134 	char *oldp;
135 	unsigned long start_at, mem_size;
136 
137 	if (!p)
138 		return;
139 
140 	oldp = p;
141 	mem_size = memparse(p, &p);
142 	if (p == oldp)
143 		return;
144 
145 	switch (*p) {
146 	case '@':
147 		start_at = memparse(p + 1, &p);
148 		memblock_add(start_at, mem_size);
149 		break;
150 
151 	case '$':
152 		start_at = memparse(p + 1, &p);
153 		memblock_reserve(start_at, mem_size);
154 		break;
155 
156 	case 0:
157 		memblock_reserve(mem_size, -mem_size);
158 		break;
159 
160 	default:
161 		pr_warn("Unrecognized memmap syntax: %s\n", p);
162 		break;
163 	}
164 }
165 
parse_memmap_opt(char * str)166 static int __init parse_memmap_opt(char *str)
167 {
168 	while (str) {
169 		char *k = strchr(str, ',');
170 
171 		if (k)
172 			*k++ = 0;
173 
174 		parse_memmap_one(str);
175 		str = k;
176 	}
177 
178 	return 0;
179 }
180 early_param("memmap", parse_memmap_opt);
181 
182 #ifdef CONFIG_MMU
183 static const pgprot_t protection_map[16] = {
184 	[VM_NONE]					= PAGE_NONE,
185 	[VM_READ]					= PAGE_READONLY,
186 	[VM_WRITE]					= PAGE_COPY,
187 	[VM_WRITE | VM_READ]				= PAGE_COPY,
188 	[VM_EXEC]					= PAGE_READONLY_EXEC,
189 	[VM_EXEC | VM_READ]				= PAGE_READONLY_EXEC,
190 	[VM_EXEC | VM_WRITE]				= PAGE_COPY_EXEC,
191 	[VM_EXEC | VM_WRITE | VM_READ]			= PAGE_COPY_EXEC,
192 	[VM_SHARED]					= PAGE_NONE,
193 	[VM_SHARED | VM_READ]				= PAGE_READONLY,
194 	[VM_SHARED | VM_WRITE]				= PAGE_SHARED,
195 	[VM_SHARED | VM_WRITE | VM_READ]		= PAGE_SHARED,
196 	[VM_SHARED | VM_EXEC]				= PAGE_READONLY_EXEC,
197 	[VM_SHARED | VM_EXEC | VM_READ]			= PAGE_READONLY_EXEC,
198 	[VM_SHARED | VM_EXEC | VM_WRITE]		= PAGE_SHARED_EXEC,
199 	[VM_SHARED | VM_EXEC | VM_WRITE | VM_READ]	= PAGE_SHARED_EXEC
200 };
201 DECLARE_VM_GET_PAGE_PROT
202 #endif
203