1 // SPDX-License-Identifier: GPL-2.0-only
3 * Based on arch/arm/mm/init.c
5 * Copyright (C) 1995-2005 Russell King
6 * Copyright (C) 2012 ARM Ltd.
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/errno.h>
12 #include <linux/swap.h>
13 #include <linux/init.h>
14 #include <linux/cache.h>
15 #include <linux/mman.h>
16 #include <linux/nodemask.h>
17 #include <linux/initrd.h>
18 #include <linux/gfp.h>
19 #include <linux/memblock.h>
20 #include <linux/sort.h>
22 #include <linux/of_fdt.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/dma-contiguous.h>
25 #include <linux/efi.h>
26 #include <linux/swiotlb.h>
27 #include <linux/vmalloc.h>
29 #include <linux/kexec.h>
30 #include <linux/crash_dump.h>
33 #include <asm/fixmap.h>
34 #include <asm/kasan.h>
35 #include <asm/kernel-pgtable.h>
36 #include <asm/memory.h>
38 #include <asm/sections.h>
39 #include <asm/setup.h>
40 #include <linux/sizes.h>
42 #include <asm/alternative.h>
45 * We need to be able to catch inadvertent references to memstart_addr
46 * that occur (potentially in generic code) before arm64_memblock_init()
47 * executes, which assigns it its actual value. So use a default value
48 * that cannot be mistaken for a real physical address.
50 s64 memstart_addr __ro_after_init
= -1;
51 EXPORT_SYMBOL(memstart_addr
);
53 phys_addr_t arm64_dma_phys_limit __ro_after_init
;
55 #ifdef CONFIG_KEXEC_CORE
57 * reserve_crashkernel() - reserves memory for crash kernel
59 * This function reserves memory area given in "crashkernel=" kernel command
60 * line parameter. The memory reserved is used by dump capture kernel when
61 * primary kernel is crashing.
63 static void __init
reserve_crashkernel(void)
65 unsigned long long crash_base
, crash_size
;
68 ret
= parse_crashkernel(boot_command_line
, memblock_phys_mem_size(),
69 &crash_size
, &crash_base
);
70 /* no crashkernel= or invalid value specified */
71 if (ret
|| !crash_size
)
74 crash_size
= PAGE_ALIGN(crash_size
);
76 if (crash_base
== 0) {
77 /* Current arm64 boot protocol requires 2MB alignment */
78 crash_base
= memblock_find_in_range(0, ARCH_LOW_ADDRESS_LIMIT
,
80 if (crash_base
== 0) {
81 pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
86 /* User specifies base address explicitly. */
87 if (!memblock_is_region_memory(crash_base
, crash_size
)) {
88 pr_warn("cannot reserve crashkernel: region is not memory\n");
92 if (memblock_is_region_reserved(crash_base
, crash_size
)) {
93 pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n");
97 if (!IS_ALIGNED(crash_base
, SZ_2M
)) {
98 pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n");
102 memblock_reserve(crash_base
, crash_size
);
104 pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
105 crash_base
, crash_base
+ crash_size
, crash_size
>> 20);
107 crashk_res
.start
= crash_base
;
108 crashk_res
.end
= crash_base
+ crash_size
- 1;
111 static void __init
reserve_crashkernel(void)
114 #endif /* CONFIG_KEXEC_CORE */
116 #ifdef CONFIG_CRASH_DUMP
117 static int __init
early_init_dt_scan_elfcorehdr(unsigned long node
,
118 const char *uname
, int depth
, void *data
)
123 if (depth
!= 1 || strcmp(uname
, "chosen") != 0)
126 reg
= of_get_flat_dt_prop(node
, "linux,elfcorehdr", &len
);
127 if (!reg
|| (len
< (dt_root_addr_cells
+ dt_root_size_cells
)))
130 elfcorehdr_addr
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
131 elfcorehdr_size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
137 * reserve_elfcorehdr() - reserves memory for elf core header
139 * This function reserves the memory occupied by an elf core header
140 * described in the device tree. This region contains all the
141 * information about primary kernel's core image and is used by a dump
142 * capture kernel to access the system memory on primary kernel.
144 static void __init
reserve_elfcorehdr(void)
146 of_scan_flat_dt(early_init_dt_scan_elfcorehdr
, NULL
);
148 if (!elfcorehdr_size
)
151 if (memblock_is_region_reserved(elfcorehdr_addr
, elfcorehdr_size
)) {
152 pr_warn("elfcorehdr is overlapped\n");
156 memblock_reserve(elfcorehdr_addr
, elfcorehdr_size
);
158 pr_info("Reserving %lldKB of memory at 0x%llx for elfcorehdr\n",
159 elfcorehdr_size
>> 10, elfcorehdr_addr
);
162 static void __init
reserve_elfcorehdr(void)
165 #endif /* CONFIG_CRASH_DUMP */
167 * Return the maximum physical address for ZONE_DMA32 (DMA_BIT_MASK(32)). It
168 * currently assumes that for memory starting above 4G, 32-bit devices will
171 static phys_addr_t __init
max_zone_dma_phys(void)
173 phys_addr_t offset
= memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
174 return min(offset
+ (1ULL << 32), memblock_end_of_DRAM());
179 static void __init
zone_sizes_init(unsigned long min
, unsigned long max
)
181 unsigned long max_zone_pfns
[MAX_NR_ZONES
] = {0};
183 #ifdef CONFIG_ZONE_DMA32
184 max_zone_pfns
[ZONE_DMA32
] = PFN_DOWN(max_zone_dma_phys());
186 max_zone_pfns
[ZONE_NORMAL
] = max
;
188 free_area_init_nodes(max_zone_pfns
);
193 static void __init
zone_sizes_init(unsigned long min
, unsigned long max
)
195 struct memblock_region
*reg
;
196 unsigned long zone_size
[MAX_NR_ZONES
], zhole_size
[MAX_NR_ZONES
];
197 unsigned long max_dma
= min
;
199 memset(zone_size
, 0, sizeof(zone_size
));
201 /* 4GB maximum for 32-bit only capable devices */
202 #ifdef CONFIG_ZONE_DMA32
203 max_dma
= PFN_DOWN(arm64_dma_phys_limit
);
204 zone_size
[ZONE_DMA32
] = max_dma
- min
;
206 zone_size
[ZONE_NORMAL
] = max
- max_dma
;
208 memcpy(zhole_size
, zone_size
, sizeof(zhole_size
));
210 for_each_memblock(memory
, reg
) {
211 unsigned long start
= memblock_region_memory_base_pfn(reg
);
212 unsigned long end
= memblock_region_memory_end_pfn(reg
);
217 #ifdef CONFIG_ZONE_DMA32
218 if (start
< max_dma
) {
219 unsigned long dma_end
= min(end
, max_dma
);
220 zhole_size
[ZONE_DMA32
] -= dma_end
- start
;
224 unsigned long normal_end
= min(end
, max
);
225 unsigned long normal_start
= max(start
, max_dma
);
226 zhole_size
[ZONE_NORMAL
] -= normal_end
- normal_start
;
230 free_area_init_node(0, zone_size
, min
, zhole_size
);
233 #endif /* CONFIG_NUMA */
235 int pfn_valid(unsigned long pfn
)
237 phys_addr_t addr
= pfn
<< PAGE_SHIFT
;
239 if ((addr
>> PAGE_SHIFT
) != pfn
)
242 #ifdef CONFIG_SPARSEMEM
243 if (pfn_to_section_nr(pfn
) >= NR_MEM_SECTIONS
)
246 if (!valid_section(__nr_to_section(pfn_to_section_nr(pfn
))))
249 return memblock_is_map_memory(addr
);
251 EXPORT_SYMBOL(pfn_valid
);
253 static phys_addr_t memory_limit
= PHYS_ADDR_MAX
;
256 * Limit the memory size that was specified via FDT.
258 static int __init
early_mem(char *p
)
263 memory_limit
= memparse(p
, &p
) & PAGE_MASK
;
264 pr_notice("Memory limited to %lldMB\n", memory_limit
>> 20);
268 early_param("mem", early_mem
);
270 static int __init
early_init_dt_scan_usablemem(unsigned long node
,
271 const char *uname
, int depth
, void *data
)
273 struct memblock_region
*usablemem
= data
;
277 if (depth
!= 1 || strcmp(uname
, "chosen") != 0)
280 reg
= of_get_flat_dt_prop(node
, "linux,usable-memory-range", &len
);
281 if (!reg
|| (len
< (dt_root_addr_cells
+ dt_root_size_cells
)))
284 usablemem
->base
= dt_mem_next_cell(dt_root_addr_cells
, ®
);
285 usablemem
->size
= dt_mem_next_cell(dt_root_size_cells
, ®
);
290 static void __init
fdt_enforce_memory_region(void)
292 struct memblock_region reg
= {
296 of_scan_flat_dt(early_init_dt_scan_usablemem
, ®
);
299 memblock_cap_memory_range(reg
.base
, reg
.size
);
302 void __init
arm64_memblock_init(void)
304 const s64 linear_region_size
= -(s64
)PAGE_OFFSET
;
306 /* Handle linux,usable-memory-range property */
307 fdt_enforce_memory_region();
309 /* Remove memory above our supported physical address size */
310 memblock_remove(1ULL << PHYS_MASK_SHIFT
, ULLONG_MAX
);
313 * Ensure that the linear region takes up exactly half of the kernel
314 * virtual address space. This way, we can distinguish a linear address
315 * from a kernel/module/vmalloc address by testing a single bit.
317 BUILD_BUG_ON(linear_region_size
!= BIT(VA_BITS
- 1));
320 * Select a suitable value for the base of physical memory.
322 memstart_addr
= round_down(memblock_start_of_DRAM(),
323 ARM64_MEMSTART_ALIGN
);
326 * Remove the memory that we will not be able to cover with the
327 * linear mapping. Take care not to clip the kernel which may be
330 memblock_remove(max_t(u64
, memstart_addr
+ linear_region_size
,
331 __pa_symbol(_end
)), ULLONG_MAX
);
332 if (memstart_addr
+ linear_region_size
< memblock_end_of_DRAM()) {
333 /* ensure that memstart_addr remains sufficiently aligned */
334 memstart_addr
= round_up(memblock_end_of_DRAM() - linear_region_size
,
335 ARM64_MEMSTART_ALIGN
);
336 memblock_remove(0, memstart_addr
);
340 * Apply the memory limit if it was set. Since the kernel may be loaded
341 * high up in memory, add back the kernel region that must be accessible
342 * via the linear mapping.
344 if (memory_limit
!= PHYS_ADDR_MAX
) {
345 memblock_mem_limit_remove_map(memory_limit
);
346 memblock_add(__pa_symbol(_text
), (u64
)(_end
- _text
));
349 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD
) && phys_initrd_size
) {
351 * Add back the memory we just removed if it results in the
352 * initrd to become inaccessible via the linear mapping.
353 * Otherwise, this is a no-op
355 u64 base
= phys_initrd_start
& PAGE_MASK
;
356 u64 size
= PAGE_ALIGN(phys_initrd_start
+ phys_initrd_size
) - base
;
359 * We can only add back the initrd memory if we don't end up
360 * with more memory than we can address via the linear mapping.
361 * It is up to the bootloader to position the kernel and the
362 * initrd reasonably close to each other (i.e., within 32 GB of
363 * each other) so that all granule/#levels combinations can
364 * always access both.
366 if (WARN(base
< memblock_start_of_DRAM() ||
367 base
+ size
> memblock_start_of_DRAM() +
369 "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
370 phys_initrd_size
= 0;
372 memblock_remove(base
, size
); /* clear MEMBLOCK_ flags */
373 memblock_add(base
, size
);
374 memblock_reserve(base
, size
);
378 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE
)) {
379 extern u16 memstart_offset_seed
;
380 u64 range
= linear_region_size
-
381 (memblock_end_of_DRAM() - memblock_start_of_DRAM());
384 * If the size of the linear region exceeds, by a sufficient
385 * margin, the size of the region that the available physical
386 * memory spans, randomize the linear region as well.
388 if (memstart_offset_seed
> 0 && range
>= ARM64_MEMSTART_ALIGN
) {
389 range
/= ARM64_MEMSTART_ALIGN
;
390 memstart_addr
-= ARM64_MEMSTART_ALIGN
*
391 ((range
* memstart_offset_seed
) >> 16);
396 * Register the kernel text, kernel data, initrd, and initial
397 * pagetables with memblock.
399 memblock_reserve(__pa_symbol(_text
), _end
- _text
);
400 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD
) && phys_initrd_size
) {
401 /* the generic initrd code expects virtual addresses */
402 initrd_start
= __phys_to_virt(phys_initrd_start
);
403 initrd_end
= initrd_start
+ phys_initrd_size
;
406 early_init_fdt_scan_reserved_mem();
408 /* 4GB maximum for 32-bit only capable devices */
409 if (IS_ENABLED(CONFIG_ZONE_DMA32
))
410 arm64_dma_phys_limit
= max_zone_dma_phys();
412 arm64_dma_phys_limit
= PHYS_MASK
+ 1;
414 reserve_crashkernel();
416 reserve_elfcorehdr();
418 high_memory
= __va(memblock_end_of_DRAM() - 1) + 1;
420 dma_contiguous_reserve(arm64_dma_phys_limit
);
423 void __init
bootmem_init(void)
425 unsigned long min
, max
;
427 min
= PFN_UP(memblock_start_of_DRAM());
428 max
= PFN_DOWN(memblock_end_of_DRAM());
430 early_memtest(min
<< PAGE_SHIFT
, max
<< PAGE_SHIFT
);
432 max_pfn
= max_low_pfn
= max
;
437 * Sparsemem tries to allocate bootmem in memory_present(), so must be
438 * done after the fixed reservations.
443 zone_sizes_init(min
, max
);
448 #ifndef CONFIG_SPARSEMEM_VMEMMAP
449 static inline void free_memmap(unsigned long start_pfn
, unsigned long end_pfn
)
451 struct page
*start_pg
, *end_pg
;
452 unsigned long pg
, pgend
;
455 * Convert start_pfn/end_pfn to a struct page pointer.
457 start_pg
= pfn_to_page(start_pfn
- 1) + 1;
458 end_pg
= pfn_to_page(end_pfn
- 1) + 1;
461 * Convert to physical addresses, and round start upwards and end
464 pg
= (unsigned long)PAGE_ALIGN(__pa(start_pg
));
465 pgend
= (unsigned long)__pa(end_pg
) & PAGE_MASK
;
468 * If there are free pages between these, free the section of the
472 memblock_free(pg
, pgend
- pg
);
476 * The mem_map array can get very big. Free the unused area of the memory map.
478 static void __init
free_unused_memmap(void)
480 unsigned long start
, prev_end
= 0;
481 struct memblock_region
*reg
;
483 for_each_memblock(memory
, reg
) {
484 start
= __phys_to_pfn(reg
->base
);
486 #ifdef CONFIG_SPARSEMEM
488 * Take care not to free memmap entries that don't exist due
489 * to SPARSEMEM sections which aren't present.
491 start
= min(start
, ALIGN(prev_end
, PAGES_PER_SECTION
));
494 * If we had a previous bank, and there is a space between the
495 * current bank and the previous, free it.
497 if (prev_end
&& prev_end
< start
)
498 free_memmap(prev_end
, start
);
501 * Align up here since the VM subsystem insists that the
502 * memmap entries are valid from the bank end aligned to
503 * MAX_ORDER_NR_PAGES.
505 prev_end
= ALIGN(__phys_to_pfn(reg
->base
+ reg
->size
),
509 #ifdef CONFIG_SPARSEMEM
510 if (!IS_ALIGNED(prev_end
, PAGES_PER_SECTION
))
511 free_memmap(prev_end
, ALIGN(prev_end
, PAGES_PER_SECTION
));
514 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
517 * mem_init() marks the free areas in the mem_map and tells us how much memory
518 * is free. This is done after various parts of the system have claimed their
519 * memory after the kernel image.
521 void __init
mem_init(void)
523 if (swiotlb_force
== SWIOTLB_FORCE
||
524 max_pfn
> (arm64_dma_phys_limit
>> PAGE_SHIFT
))
527 swiotlb_force
= SWIOTLB_NO_FORCE
;
529 set_max_mapnr(max_pfn
- PHYS_PFN_OFFSET
);
531 #ifndef CONFIG_SPARSEMEM_VMEMMAP
532 free_unused_memmap();
534 /* this will put all unused low memory onto the freelists */
537 mem_init_print_info(NULL
);
540 * Check boundaries twice: Some fundamental inconsistencies can be
541 * detected at build time already.
544 BUILD_BUG_ON(TASK_SIZE_32
> DEFAULT_MAP_WINDOW_64
);
547 if (PAGE_SIZE
>= 16384 && get_num_physpages() <= 128) {
548 extern int sysctl_overcommit_memory
;
550 * On a machine this small we won't get anywhere without
551 * overcommit, so turn it on by default.
553 sysctl_overcommit_memory
= OVERCOMMIT_ALWAYS
;
557 void free_initmem(void)
559 free_reserved_area(lm_alias(__init_begin
),
560 lm_alias(__init_end
),
563 * Unmap the __init region but leave the VM area in place. This
564 * prevents the region from being reused for kernel modules, which
565 * is not supported by kallsyms.
567 unmap_kernel_range((u64
)__init_begin
, (u64
)(__init_end
- __init_begin
));
570 #ifdef CONFIG_BLK_DEV_INITRD
571 void __init
free_initrd_mem(unsigned long start
, unsigned long end
)
573 free_reserved_area((void *)start
, (void *)end
, 0, "initrd");
574 memblock_free(__virt_to_phys(start
), end
- start
);
579 * Dump out memory limit information on panic.
581 static int dump_mem_limit(struct notifier_block
*self
, unsigned long v
, void *p
)
583 if (memory_limit
!= PHYS_ADDR_MAX
) {
584 pr_emerg("Memory Limit: %llu MB\n", memory_limit
>> 20);
586 pr_emerg("Memory Limit: none\n");
591 static struct notifier_block mem_limit_notifier
= {
592 .notifier_call
= dump_mem_limit
,
595 static int __init
register_mem_limit_dumper(void)
597 atomic_notifier_chain_register(&panic_notifier_list
,
598 &mem_limit_notifier
);
601 __initcall(register_mem_limit_dumper
);