]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/arm64/mm/init.c
arm64: limit memory regions based on DT property, usable-memory-range
[mirror_ubuntu-zesty-kernel.git] / arch / arm64 / mm / init.c
CommitLineData
c1cc1552
CM
1/*
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>
5a9e3e15 26#include <linux/cache.h>
c1cc1552
CM
27#include <linux/mman.h>
28#include <linux/nodemask.h>
29#include <linux/initrd.h>
30#include <linux/gfp.h>
31#include <linux/memblock.h>
32#include <linux/sort.h>
33#include <linux/of_fdt.h>
19e7640d 34#include <linux/dma-mapping.h>
6ac2104d 35#include <linux/dma-contiguous.h>
86c8b27a 36#include <linux/efi.h>
a1e50a82 37#include <linux/swiotlb.h>
dae8c235 38#include <linux/vmalloc.h>
c1cc1552 39
a7f8de16 40#include <asm/boot.h>
08375198 41#include <asm/fixmap.h>
f9040773 42#include <asm/kasan.h>
a7f8de16 43#include <asm/kernel-pgtable.h>
aa03c428 44#include <asm/memory.h>
1a2db300 45#include <asm/numa.h>
c1cc1552
CM
46#include <asm/sections.h>
47#include <asm/setup.h>
48#include <asm/sizes.h>
49#include <asm/tlb.h>
e039ee4e 50#include <asm/alternative.h>
c1cc1552 51
a7f8de16
AB
52/*
53 * We need to be able to catch inadvertent references to memstart_addr
54 * that occur (potentially in generic code) before arm64_memblock_init()
55 * executes, which assigns it its actual value. So use a default value
56 * that cannot be mistaken for a real physical address.
57 */
5a9e3e15
JZ
58s64 memstart_addr __ro_after_init = -1;
59phys_addr_t arm64_dma_phys_limit __ro_after_init;
c1cc1552 60
ec2eaa73 61#ifdef CONFIG_BLK_DEV_INITRD
c1cc1552
CM
62static int __init early_initrd(char *p)
63{
64 unsigned long start, size;
65 char *endp;
66
67 start = memparse(p, &endp);
68 if (*endp == ',') {
69 size = memparse(endp + 1, NULL);
70
a89dea58
AB
71 initrd_start = start;
72 initrd_end = start + size;
c1cc1552
CM
73 }
74 return 0;
75}
76early_param("initrd", early_initrd);
ec2eaa73 77#endif
c1cc1552 78
d50314a6
CM
79/*
80 * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It
81 * currently assumes that for memory starting above 4G, 32-bit devices will
82 * use a DMA offset.
83 */
a7c61a34 84static phys_addr_t __init max_zone_dma_phys(void)
d50314a6
CM
85{
86 phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
87 return min(offset + (1ULL << 32), memblock_end_of_DRAM());
88}
89
1a2db300
GK
90#ifdef CONFIG_NUMA
91
92static void __init zone_sizes_init(unsigned long min, unsigned long max)
93{
94 unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
95
96 if (IS_ENABLED(CONFIG_ZONE_DMA))
97 max_zone_pfns[ZONE_DMA] = PFN_DOWN(max_zone_dma_phys());
98 max_zone_pfns[ZONE_NORMAL] = max;
99
100 free_area_init_nodes(max_zone_pfns);
101}
102
103#else
104
c1cc1552
CM
105static void __init zone_sizes_init(unsigned long min, unsigned long max)
106{
107 struct memblock_region *reg;
108 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
19e7640d 109 unsigned long max_dma = min;
c1cc1552
CM
110
111 memset(zone_size, 0, sizeof(zone_size));
112
c1cc1552 113 /* 4GB maximum for 32-bit only capable devices */
86a5906e
RM
114#ifdef CONFIG_ZONE_DMA
115 max_dma = PFN_DOWN(arm64_dma_phys_limit);
116 zone_size[ZONE_DMA] = max_dma - min;
117#endif
19e7640d 118 zone_size[ZONE_NORMAL] = max - max_dma;
c1cc1552
CM
119
120 memcpy(zhole_size, zone_size, sizeof(zhole_size));
121
122 for_each_memblock(memory, reg) {
123 unsigned long start = memblock_region_memory_base_pfn(reg);
124 unsigned long end = memblock_region_memory_end_pfn(reg);
125
126 if (start >= max)
127 continue;
19e7640d 128
86a5906e
RM
129#ifdef CONFIG_ZONE_DMA
130 if (start < max_dma) {
19e7640d
CM
131 unsigned long dma_end = min(end, max_dma);
132 zhole_size[ZONE_DMA] -= dma_end - start;
c1cc1552 133 }
86a5906e 134#endif
19e7640d 135 if (end > max_dma) {
c1cc1552 136 unsigned long normal_end = min(end, max);
19e7640d 137 unsigned long normal_start = max(start, max_dma);
c1cc1552
CM
138 zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
139 }
140 }
141
142 free_area_init_node(0, zone_size, min, zhole_size);
143}
144
1a2db300
GK
145#endif /* CONFIG_NUMA */
146
c1cc1552
CM
147#ifdef CONFIG_HAVE_ARCH_PFN_VALID
148int pfn_valid(unsigned long pfn)
149{
68709f45 150 return memblock_is_map_memory(pfn << PAGE_SHIFT);
c1cc1552
CM
151}
152EXPORT_SYMBOL(pfn_valid);
153#endif
154
155#ifndef CONFIG_SPARSEMEM
a7c61a34 156static void __init arm64_memory_present(void)
c1cc1552
CM
157{
158}
159#else
a7c61a34 160static void __init arm64_memory_present(void)
c1cc1552
CM
161{
162 struct memblock_region *reg;
163
1a2db300 164 for_each_memblock(memory, reg) {
ea2cbee3
MR
165 int nid = memblock_get_region_node(reg);
166
1a2db300
GK
167 memory_present(nid, memblock_region_memory_base_pfn(reg),
168 memblock_region_memory_end_pfn(reg));
169 }
c1cc1552
CM
170}
171#endif
172
6083fe74
MR
173static phys_addr_t memory_limit = (phys_addr_t)ULLONG_MAX;
174
175/*
176 * Limit the memory size that was specified via FDT.
177 */
178static int __init early_mem(char *p)
179{
180 if (!p)
181 return 1;
182
183 memory_limit = memparse(p, &p) & PAGE_MASK;
184 pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
185
186 return 0;
187}
188early_param("mem", early_mem);
189
6384eca9
AT
190static int __init early_init_dt_scan_usablemem(unsigned long node,
191 const char *uname, int depth, void *data)
192{
193 struct memblock_region *usablemem = data;
194 const __be32 *reg;
195 int len;
196
197 if (depth != 1 || strcmp(uname, "chosen") != 0)
198 return 0;
199
200 reg = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
201 if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells)))
202 return 1;
203
204 usablemem->base = dt_mem_next_cell(dt_root_addr_cells, &reg);
205 usablemem->size = dt_mem_next_cell(dt_root_size_cells, &reg);
206
207 return 1;
208}
209
210static void __init fdt_enforce_memory_region(void)
211{
212 struct memblock_region reg = {
213 .size = 0,
214 };
215
216 of_scan_flat_dt(early_init_dt_scan_usablemem, &reg);
217
218 if (reg.size)
219 memblock_cap_memory_range(reg.base, reg.size);
220}
221
c1cc1552
CM
222void __init arm64_memblock_init(void)
223{
a7f8de16
AB
224 const s64 linear_region_size = -(s64)PAGE_OFFSET;
225
6384eca9
AT
226 /* Handle linux,usable-memory-range property */
227 fdt_enforce_memory_region();
228
6d2aa549
AB
229 /*
230 * Ensure that the linear region takes up exactly half of the kernel
231 * virtual address space. This way, we can distinguish a linear address
232 * from a kernel/module/vmalloc address by testing a single bit.
233 */
234 BUILD_BUG_ON(linear_region_size != BIT(VA_BITS - 1));
235
a7f8de16
AB
236 /*
237 * Select a suitable value for the base of physical memory.
238 */
239 memstart_addr = round_down(memblock_start_of_DRAM(),
240 ARM64_MEMSTART_ALIGN);
241
242 /*
243 * Remove the memory that we will not be able to cover with the
244 * linear mapping. Take care not to clip the kernel which may be
245 * high in memory.
246 */
020d044f 247 memblock_remove(max_t(u64, memstart_addr + linear_region_size, __pa(_end)),
a7f8de16 248 ULLONG_MAX);
2958987f
AB
249 if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
250 /* ensure that memstart_addr remains sufficiently aligned */
251 memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
252 ARM64_MEMSTART_ALIGN);
253 memblock_remove(0, memstart_addr);
254 }
a7f8de16
AB
255
256 /*
257 * Apply the memory limit if it was set. Since the kernel may be loaded
258 * high up in memory, add back the kernel region that must be accessible
259 * via the linear mapping.
260 */
261 if (memory_limit != (phys_addr_t)ULLONG_MAX) {
cb0a6502 262 memblock_mem_limit_remove_map(memory_limit);
a7f8de16
AB
263 memblock_add(__pa(_text), (u64)(_end - _text));
264 }
6083fe74 265
177e15f0
AB
266 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && initrd_start) {
267 /*
268 * Add back the memory we just removed if it results in the
269 * initrd to become inaccessible via the linear mapping.
270 * Otherwise, this is a no-op
271 */
272 u64 base = initrd_start & PAGE_MASK;
273 u64 size = PAGE_ALIGN(initrd_end) - base;
274
275 /*
276 * We can only add back the initrd memory if we don't end up
277 * with more memory than we can address via the linear mapping.
278 * It is up to the bootloader to position the kernel and the
279 * initrd reasonably close to each other (i.e., within 32 GB of
280 * each other) so that all granule/#levels combinations can
281 * always access both.
282 */
283 if (WARN(base < memblock_start_of_DRAM() ||
284 base + size > memblock_start_of_DRAM() +
285 linear_region_size,
286 "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
287 initrd_start = 0;
288 } else {
289 memblock_remove(base, size); /* clear MEMBLOCK_ flags */
290 memblock_add(base, size);
291 memblock_reserve(base, size);
292 }
293 }
294
c031a421
AB
295 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
296 extern u16 memstart_offset_seed;
297 u64 range = linear_region_size -
298 (memblock_end_of_DRAM() - memblock_start_of_DRAM());
299
300 /*
301 * If the size of the linear region exceeds, by a sufficient
302 * margin, the size of the region that the available physical
303 * memory spans, randomize the linear region as well.
304 */
305 if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) {
306 range = range / ARM64_MEMSTART_ALIGN + 1;
307 memstart_addr -= ARM64_MEMSTART_ALIGN *
308 ((range * memstart_offset_seed) >> 16);
309 }
310 }
6083fe74 311
bd00cd5f
MR
312 /*
313 * Register the kernel text, kernel data, initrd, and initial
314 * pagetables with memblock.
315 */
c1cc1552
CM
316 memblock_reserve(__pa(_text), _end - _text);
317#ifdef CONFIG_BLK_DEV_INITRD
a89dea58
AB
318 if (initrd_start) {
319 memblock_reserve(initrd_start, initrd_end - initrd_start);
320
321 /* the generic initrd code expects virtual addresses */
322 initrd_start = __phys_to_virt(initrd_start);
323 initrd_end = __phys_to_virt(initrd_end);
324 }
c1cc1552
CM
325#endif
326
0ceac9e0 327 early_init_fdt_scan_reserved_mem();
2d5a5612
CM
328
329 /* 4GB maximum for 32-bit only capable devices */
330 if (IS_ENABLED(CONFIG_ZONE_DMA))
a1e50a82
CM
331 arm64_dma_phys_limit = max_zone_dma_phys();
332 else
333 arm64_dma_phys_limit = PHYS_MASK + 1;
334 dma_contiguous_reserve(arm64_dma_phys_limit);
6ac2104d 335
c1cc1552 336 memblock_allow_resize();
c1cc1552
CM
337}
338
339void __init bootmem_init(void)
340{
341 unsigned long min, max;
342
343 min = PFN_UP(memblock_start_of_DRAM());
344 max = PFN_DOWN(memblock_end_of_DRAM());
345
36dd9086
VM
346 early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
347
1a2db300
GK
348 max_pfn = max_low_pfn = max;
349
350 arm64_numa_init();
c1cc1552
CM
351 /*
352 * Sparsemem tries to allocate bootmem in memory_present(), so must be
353 * done after the fixed reservations.
354 */
355 arm64_memory_present();
356
357 sparse_init();
358 zone_sizes_init(min, max);
359
360 high_memory = __va((max << PAGE_SHIFT) - 1) + 1;
1a2db300 361 memblock_dump_all();
c1cc1552
CM
362}
363
c1cc1552
CM
364#ifndef CONFIG_SPARSEMEM_VMEMMAP
365static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
366{
367 struct page *start_pg, *end_pg;
368 unsigned long pg, pgend;
369
370 /*
371 * Convert start_pfn/end_pfn to a struct page pointer.
372 */
373 start_pg = pfn_to_page(start_pfn - 1) + 1;
374 end_pg = pfn_to_page(end_pfn - 1) + 1;
375
376 /*
377 * Convert to physical addresses, and round start upwards and end
378 * downwards.
379 */
380 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
381 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
382
383 /*
384 * If there are free pages between these, free the section of the
385 * memmap array.
386 */
387 if (pg < pgend)
388 free_bootmem(pg, pgend - pg);
389}
390
391/*
392 * The mem_map array can get very big. Free the unused area of the memory map.
393 */
394static void __init free_unused_memmap(void)
395{
396 unsigned long start, prev_end = 0;
397 struct memblock_region *reg;
398
399 for_each_memblock(memory, reg) {
400 start = __phys_to_pfn(reg->base);
401
402#ifdef CONFIG_SPARSEMEM
403 /*
404 * Take care not to free memmap entries that don't exist due
405 * to SPARSEMEM sections which aren't present.
406 */
407 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
408#endif
409 /*
410 * If we had a previous bank, and there is a space between the
411 * current bank and the previous, free it.
412 */
413 if (prev_end && prev_end < start)
414 free_memmap(prev_end, start);
415
416 /*
417 * Align up here since the VM subsystem insists that the
418 * memmap entries are valid from the bank end aligned to
419 * MAX_ORDER_NR_PAGES.
420 */
b9bcc919 421 prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size),
c1cc1552
CM
422 MAX_ORDER_NR_PAGES);
423 }
424
425#ifdef CONFIG_SPARSEMEM
426 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
427 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
428#endif
429}
430#endif /* !CONFIG_SPARSEMEM_VMEMMAP */
431
432/*
433 * mem_init() marks the free areas in the mem_map and tells us how much memory
434 * is free. This is done after various parts of the system have claimed their
435 * memory after the kernel image.
436 */
437void __init mem_init(void)
438{
ae7871be
GU
439 if (swiotlb_force == SWIOTLB_FORCE ||
440 max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT))
b67a8b29 441 swiotlb_init(1);
524dabe1
AG
442 else
443 swiotlb_force = SWIOTLB_NO_FORCE;
a1e50a82 444
a6583c7c 445 set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
c1cc1552
CM
446
447#ifndef CONFIG_SPARSEMEM_VMEMMAP
c1cc1552
CM
448 free_unused_memmap();
449#endif
bee4ebd1 450 /* this will put all unused low memory onto the freelists */
0c988534 451 free_all_bootmem();
c1cc1552 452
6879ea83 453 mem_init_print_info(NULL);
c1cc1552
CM
454
455#define MLK(b, t) b, t, ((t) - (b)) >> 10
456#define MLM(b, t) b, t, ((t) - (b)) >> 20
08375198 457#define MLG(b, t) b, t, ((t) - (b)) >> 30
c1cc1552
CM
458#define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
459
f09f1bac 460 pr_notice("Virtual kernel memory layout:\n");
ee7f881b 461#ifdef CONFIG_KASAN
f7881bd6 462 pr_notice(" kasan : 0x%16lx - 0x%16lx (%6ld GB)\n",
f09f1bac 463 MLG(KASAN_SHADOW_START, KASAN_SHADOW_END));
ee7f881b 464#endif
f7881bd6 465 pr_notice(" modules : 0x%16lx - 0x%16lx (%6ld MB)\n",
f09f1bac 466 MLM(MODULES_VADDR, MODULES_END));
f7881bd6 467 pr_notice(" vmalloc : 0x%16lx - 0x%16lx (%6ld GB)\n",
f09f1bac 468 MLG(VMALLOC_START, VMALLOC_END));
f7881bd6 469 pr_notice(" .text : 0x%p" " - 0x%p" " (%6ld KB)\n",
9fdc14c5 470 MLK_ROUNDUP(_text, _etext));
f7881bd6 471 pr_notice(" .rodata : 0x%p" " - 0x%p" " (%6ld KB)\n",
9fdc14c5 472 MLK_ROUNDUP(__start_rodata, __init_begin));
f7881bd6 473 pr_notice(" .init : 0x%p" " - 0x%p" " (%6ld KB)\n",
d32351c8 474 MLK_ROUNDUP(__init_begin, __init_end));
f7881bd6 475 pr_notice(" .data : 0x%p" " - 0x%p" " (%6ld KB)\n",
f09f1bac 476 MLK_ROUNDUP(_sdata, _edata));
f7881bd6 477 pr_notice(" .bss : 0x%p" " - 0x%p" " (%6ld KB)\n",
9974723e 478 MLK_ROUNDUP(__bss_start, __bss_stop));
f7881bd6 479 pr_notice(" fixed : 0x%16lx - 0x%16lx (%6ld KB)\n",
3e1907d5 480 MLK(FIXADDR_START, FIXADDR_TOP));
f7881bd6 481 pr_notice(" PCI I/O : 0x%16lx - 0x%16lx (%6ld MB)\n",
3e1907d5 482 MLM(PCI_IO_START, PCI_IO_END));
c1cc1552 483#ifdef CONFIG_SPARSEMEM_VMEMMAP
f7881bd6 484 pr_notice(" vmemmap : 0x%16lx - 0x%16lx (%6ld GB maximum)\n",
d32351c8 485 MLG(VMEMMAP_START, VMEMMAP_START + VMEMMAP_SIZE));
f7881bd6 486 pr_notice(" 0x%16lx - 0x%16lx (%6ld MB actual)\n",
f09f1bac
CM
487 MLM((unsigned long)phys_to_page(memblock_start_of_DRAM()),
488 (unsigned long)virt_to_page(high_memory)));
c1cc1552 489#endif
f7881bd6 490 pr_notice(" memory : 0x%16lx - 0x%16lx (%6ld MB)\n",
f09f1bac
CM
491 MLM(__phys_to_virt(memblock_start_of_DRAM()),
492 (unsigned long)high_memory));
c1cc1552
CM
493
494#undef MLK
495#undef MLM
496#undef MLK_ROUNDUP
497
498 /*
499 * Check boundaries twice: Some fundamental inconsistencies can be
500 * detected at build time already.
501 */
502#ifdef CONFIG_COMPAT
503 BUILD_BUG_ON(TASK_SIZE_32 > TASK_SIZE_64);
504#endif
c1cc1552 505
3e1907d5
AB
506 /*
507 * Make sure we chose the upper bound of sizeof(struct page)
508 * correctly.
509 */
510 BUILD_BUG_ON(sizeof(struct page) > (1 << STRUCT_PAGE_MAX_SHIFT));
511
bee4ebd1 512 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
c1cc1552
CM
513 extern int sysctl_overcommit_memory;
514 /*
515 * On a machine this small we won't get anywhere without
516 * overcommit, so turn it on by default.
517 */
518 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
519 }
520}
521
522void free_initmem(void)
523{
d386825c
AB
524 free_reserved_area(__va(__pa(__init_begin)), __va(__pa(__init_end)),
525 0, "unused kernel");
dae8c235
KW
526 /*
527 * Unmap the __init region but leave the VM area in place. This
528 * prevents the region from being reused for kernel modules, which
529 * is not supported by kallsyms.
530 */
531 unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin));
c1cc1552
CM
532}
533
534#ifdef CONFIG_BLK_DEV_INITRD
535
662ba3db 536static int keep_initrd __initdata;
c1cc1552 537
662ba3db 538void __init free_initrd_mem(unsigned long start, unsigned long end)
c1cc1552 539{
0145058c 540 if (!keep_initrd)
9af5b807 541 free_reserved_area((void *)start, (void *)end, 0, "initrd");
c1cc1552
CM
542}
543
544static int __init keepinitrd_setup(char *__unused)
545{
546 keep_initrd = 1;
547 return 1;
548}
549
550__setup("keepinitrd", keepinitrd_setup);
551#endif
a7f8de16
AB
552
553/*
554 * Dump out memory limit information on panic.
555 */
556static int dump_mem_limit(struct notifier_block *self, unsigned long v, void *p)
557{
558 if (memory_limit != (phys_addr_t)ULLONG_MAX) {
559 pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
560 } else {
561 pr_emerg("Memory Limit: none\n");
562 }
563 return 0;
564}
565
566static struct notifier_block mem_limit_notifier = {
567 .notifier_call = dump_mem_limit,
568};
569
570static int __init register_mem_limit_dumper(void)
571{
572 atomic_notifier_chain_register(&panic_notifier_list,
573 &mem_limit_notifier);
574 return 0;
575}
576__initcall(register_mem_limit_dumper);