blob: 0b32504e280f1a04592305750ccca2c388e5e120 [file] [log] [blame]
Catalin Marinasc1cc1552012-03-05 11:49:27 +00001/*
2 * Based on arch/arm/mm/init.c
3 *
4 * Copyright (C) 1995-2005 Russell King
5 * Copyright (C) 2012 ARM Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <linux/kernel.h>
21#include <linux/export.h>
22#include <linux/errno.h>
23#include <linux/swap.h>
24#include <linux/init.h>
25#include <linux/bootmem.h>
26#include <linux/mman.h>
27#include <linux/nodemask.h>
28#include <linux/initrd.h>
29#include <linux/gfp.h>
30#include <linux/memblock.h>
31#include <linux/sort.h>
32#include <linux/of_fdt.h>
Catalin Marinas19e76402014-02-27 12:09:22 +000033#include <linux/dma-mapping.h>
Laura Abbott6ac21042013-12-12 19:28:33 +000034#include <linux/dma-contiguous.h>
Catalin Marinasc1cc1552012-03-05 11:49:27 +000035
Catalin Marinas08375192014-07-16 17:42:43 +010036#include <asm/fixmap.h>
Catalin Marinasc1cc1552012-03-05 11:49:27 +000037#include <asm/sections.h>
38#include <asm/setup.h>
39#include <asm/sizes.h>
40#include <asm/tlb.h>
41
42#include "mm.h"
43
Catalin Marinasc1cc1552012-03-05 11:49:27 +000044phys_addr_t memstart_addr __read_mostly = 0;
45
Rob Herringec2eaa72013-08-25 16:47:48 -050046#ifdef CONFIG_BLK_DEV_INITRD
Catalin Marinasc1cc1552012-03-05 11:49:27 +000047static int __init early_initrd(char *p)
48{
49 unsigned long start, size;
50 char *endp;
51
52 start = memparse(p, &endp);
53 if (*endp == ',') {
54 size = memparse(endp + 1, NULL);
55
Rob Herringec2eaa72013-08-25 16:47:48 -050056 initrd_start = (unsigned long)__va(start);
57 initrd_end = (unsigned long)__va(start + size);
Catalin Marinasc1cc1552012-03-05 11:49:27 +000058 }
59 return 0;
60}
61early_param("initrd", early_initrd);
Rob Herringec2eaa72013-08-25 16:47:48 -050062#endif
Catalin Marinasc1cc1552012-03-05 11:49:27 +000063
Catalin Marinasc1cc1552012-03-05 11:49:27 +000064static void __init zone_sizes_init(unsigned long min, unsigned long max)
65{
66 struct memblock_region *reg;
67 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
Catalin Marinas19e76402014-02-27 12:09:22 +000068 unsigned long max_dma = min;
Catalin Marinasc1cc1552012-03-05 11:49:27 +000069
70 memset(zone_size, 0, sizeof(zone_size));
71
Catalin Marinasc1cc1552012-03-05 11:49:27 +000072 /* 4GB maximum for 32-bit only capable devices */
Catalin Marinas19e76402014-02-27 12:09:22 +000073 if (IS_ENABLED(CONFIG_ZONE_DMA)) {
74 unsigned long max_dma_phys =
Catalin Marinas2d5a5612014-06-13 13:41:20 +010075 (unsigned long)(dma_to_phys(NULL, DMA_BIT_MASK(32)) + 1);
Catalin Marinas19e76402014-02-27 12:09:22 +000076 max_dma = max(min, min(max, max_dma_phys >> PAGE_SHIFT));
77 zone_size[ZONE_DMA] = max_dma - min;
78 }
79 zone_size[ZONE_NORMAL] = max - max_dma;
Catalin Marinasc1cc1552012-03-05 11:49:27 +000080
81 memcpy(zhole_size, zone_size, sizeof(zhole_size));
82
83 for_each_memblock(memory, reg) {
84 unsigned long start = memblock_region_memory_base_pfn(reg);
85 unsigned long end = memblock_region_memory_end_pfn(reg);
86
87 if (start >= max)
88 continue;
Catalin Marinas19e76402014-02-27 12:09:22 +000089
90 if (IS_ENABLED(CONFIG_ZONE_DMA) && start < max_dma) {
91 unsigned long dma_end = min(end, max_dma);
92 zhole_size[ZONE_DMA] -= dma_end - start;
Catalin Marinasc1cc1552012-03-05 11:49:27 +000093 }
Catalin Marinas19e76402014-02-27 12:09:22 +000094
95 if (end > max_dma) {
Catalin Marinasc1cc1552012-03-05 11:49:27 +000096 unsigned long normal_end = min(end, max);
Catalin Marinas19e76402014-02-27 12:09:22 +000097 unsigned long normal_start = max(start, max_dma);
Catalin Marinasc1cc1552012-03-05 11:49:27 +000098 zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
99 }
100 }
101
102 free_area_init_node(0, zone_size, min, zhole_size);
103}
104
105#ifdef CONFIG_HAVE_ARCH_PFN_VALID
106int pfn_valid(unsigned long pfn)
107{
108 return memblock_is_memory(pfn << PAGE_SHIFT);
109}
110EXPORT_SYMBOL(pfn_valid);
111#endif
112
113#ifndef CONFIG_SPARSEMEM
114static void arm64_memory_present(void)
115{
116}
117#else
118static void arm64_memory_present(void)
119{
120 struct memblock_region *reg;
121
122 for_each_memblock(memory, reg)
123 memory_present(0, memblock_region_memory_base_pfn(reg),
124 memblock_region_memory_end_pfn(reg));
125}
126#endif
127
128void __init arm64_memblock_init(void)
129{
Catalin Marinas2d5a5612014-06-13 13:41:20 +0100130 phys_addr_t dma_phys_limit = 0;
131
Mark Rutlandbd00cd52014-06-24 16:51:35 +0100132 /*
133 * Register the kernel text, kernel data, initrd, and initial
134 * pagetables with memblock.
135 */
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000136 memblock_reserve(__pa(_text), _end - _text);
137#ifdef CONFIG_BLK_DEV_INITRD
Rob Herringec2eaa72013-08-25 16:47:48 -0500138 if (initrd_start)
139 memblock_reserve(__virt_to_phys(initrd_start), initrd_end - initrd_start);
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000140#endif
141
Marek Szyprowski9bf14b72014-02-28 14:42:55 +0100142 early_init_fdt_scan_reserved_mem();
Catalin Marinas2d5a5612014-06-13 13:41:20 +0100143
144 /* 4GB maximum for 32-bit only capable devices */
145 if (IS_ENABLED(CONFIG_ZONE_DMA))
146 dma_phys_limit = dma_to_phys(NULL, DMA_BIT_MASK(32)) + 1;
147 dma_contiguous_reserve(dma_phys_limit);
Laura Abbott6ac21042013-12-12 19:28:33 +0000148
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000149 memblock_allow_resize();
150 memblock_dump_all();
151}
152
153void __init bootmem_init(void)
154{
155 unsigned long min, max;
156
157 min = PFN_UP(memblock_start_of_DRAM());
158 max = PFN_DOWN(memblock_end_of_DRAM());
159
160 /*
161 * Sparsemem tries to allocate bootmem in memory_present(), so must be
162 * done after the fixed reservations.
163 */
164 arm64_memory_present();
165
166 sparse_init();
167 zone_sizes_init(min, max);
168
169 high_memory = __va((max << PAGE_SHIFT) - 1) + 1;
170 max_pfn = max_low_pfn = max;
171}
172
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000173#ifndef CONFIG_SPARSEMEM_VMEMMAP
174static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
175{
176 struct page *start_pg, *end_pg;
177 unsigned long pg, pgend;
178
179 /*
180 * Convert start_pfn/end_pfn to a struct page pointer.
181 */
182 start_pg = pfn_to_page(start_pfn - 1) + 1;
183 end_pg = pfn_to_page(end_pfn - 1) + 1;
184
185 /*
186 * Convert to physical addresses, and round start upwards and end
187 * downwards.
188 */
189 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
190 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
191
192 /*
193 * If there are free pages between these, free the section of the
194 * memmap array.
195 */
196 if (pg < pgend)
197 free_bootmem(pg, pgend - pg);
198}
199
200/*
201 * The mem_map array can get very big. Free the unused area of the memory map.
202 */
203static void __init free_unused_memmap(void)
204{
205 unsigned long start, prev_end = 0;
206 struct memblock_region *reg;
207
208 for_each_memblock(memory, reg) {
209 start = __phys_to_pfn(reg->base);
210
211#ifdef CONFIG_SPARSEMEM
212 /*
213 * Take care not to free memmap entries that don't exist due
214 * to SPARSEMEM sections which aren't present.
215 */
216 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
217#endif
218 /*
219 * If we had a previous bank, and there is a space between the
220 * current bank and the previous, free it.
221 */
222 if (prev_end && prev_end < start)
223 free_memmap(prev_end, start);
224
225 /*
226 * Align up here since the VM subsystem insists that the
227 * memmap entries are valid from the bank end aligned to
228 * MAX_ORDER_NR_PAGES.
229 */
230 prev_end = ALIGN(start + __phys_to_pfn(reg->size),
231 MAX_ORDER_NR_PAGES);
232 }
233
234#ifdef CONFIG_SPARSEMEM
235 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
236 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
237#endif
238}
239#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
240
241/*
242 * mem_init() marks the free areas in the mem_map and tells us how much memory
243 * is free. This is done after various parts of the system have claimed their
244 * memory after the kernel image.
245 */
246void __init mem_init(void)
247{
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000248 max_mapnr = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
249
250#ifndef CONFIG_SPARSEMEM_VMEMMAP
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000251 free_unused_memmap();
252#endif
Jiang Liubee4ebd2013-07-03 15:03:49 -0700253 /* this will put all unused low memory onto the freelists */
Jiang Liu0c988532013-07-03 15:03:24 -0700254 free_all_bootmem();
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000255
Jiang Liu6879ea82013-07-03 15:04:02 -0700256 mem_init_print_info(NULL);
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000257
258#define MLK(b, t) b, t, ((t) - (b)) >> 10
259#define MLM(b, t) b, t, ((t) - (b)) >> 20
Catalin Marinas08375192014-07-16 17:42:43 +0100260#define MLG(b, t) b, t, ((t) - (b)) >> 30
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000261#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
262
263 pr_notice("Virtual kernel memory layout:\n"
Catalin Marinas08375192014-07-16 17:42:43 +0100264 " vmalloc : 0x%16lx - 0x%16lx (%6ld GB)\n"
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000265#ifdef CONFIG_SPARSEMEM_VMEMMAP
Catalin Marinas08375192014-07-16 17:42:43 +0100266 " vmemmap : 0x%16lx - 0x%16lx (%6ld GB maximum)\n"
267 " 0x%16lx - 0x%16lx (%6ld MB actual)\n"
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000268#endif
Catalin Marinas08375192014-07-16 17:42:43 +0100269 " PCI I/O : 0x%16lx - 0x%16lx (%6ld MB)\n"
270 " fixed : 0x%16lx - 0x%16lx (%6ld KB)\n"
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000271 " modules : 0x%16lx - 0x%16lx (%6ld MB)\n"
272 " memory : 0x%16lx - 0x%16lx (%6ld MB)\n"
Catalin Marinas08375192014-07-16 17:42:43 +0100273 " .init : 0x%p" " - 0x%p" " (%6ld KB)\n"
274 " .text : 0x%p" " - 0x%p" " (%6ld KB)\n"
275 " .data : 0x%p" " - 0x%p" " (%6ld KB)\n",
276 MLG(VMALLOC_START, VMALLOC_END),
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000277#ifdef CONFIG_SPARSEMEM_VMEMMAP
Catalin Marinas08375192014-07-16 17:42:43 +0100278 MLG((unsigned long)vmemmap,
279 (unsigned long)vmemmap + VMEMMAP_SIZE),
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000280 MLM((unsigned long)virt_to_page(PAGE_OFFSET),
281 (unsigned long)virt_to_page(high_memory)),
282#endif
Catalin Marinas08375192014-07-16 17:42:43 +0100283 MLM((unsigned long)PCI_IOBASE, (unsigned long)PCI_IOBASE + SZ_16M),
284 MLK(FIXADDR_START, FIXADDR_TOP),
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000285 MLM(MODULES_VADDR, MODULES_END),
286 MLM(PAGE_OFFSET, (unsigned long)high_memory),
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000287 MLK_ROUNDUP(__init_begin, __init_end),
288 MLK_ROUNDUP(_text, _etext),
289 MLK_ROUNDUP(_sdata, _edata));
290
291#undef MLK
292#undef MLM
293#undef MLK_ROUNDUP
294
295 /*
296 * Check boundaries twice: Some fundamental inconsistencies can be
297 * detected at build time already.
298 */
299#ifdef CONFIG_COMPAT
300 BUILD_BUG_ON(TASK_SIZE_32 > TASK_SIZE_64);
301#endif
302 BUILD_BUG_ON(TASK_SIZE_64 > MODULES_VADDR);
303 BUG_ON(TASK_SIZE_64 > MODULES_VADDR);
304
Jiang Liubee4ebd2013-07-03 15:03:49 -0700305 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000306 extern int sysctl_overcommit_memory;
307 /*
308 * On a machine this small we won't get anywhere without
309 * overcommit, so turn it on by default.
310 */
311 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
312 }
313}
314
315void free_initmem(void)
316{
Jiang Liu9af5b802013-07-03 15:02:54 -0700317 free_initmem_default(0);
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000318}
319
320#ifdef CONFIG_BLK_DEV_INITRD
321
322static int keep_initrd;
323
324void free_initrd_mem(unsigned long start, unsigned long end)
325{
Jiang Liu9af5b802013-07-03 15:02:54 -0700326 if (!keep_initrd)
327 free_reserved_area((void *)start, (void *)end, 0, "initrd");
Catalin Marinasc1cc1552012-03-05 11:49:27 +0000328}
329
330static int __init keepinitrd_setup(char *__unused)
331{
332 keep_initrd = 1;
333 return 1;
334}
335
336__setup("keepinitrd", keepinitrd_setup);
337#endif