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