xref: /openbmc/linux/arch/arm64/mm/init.c (revision 745b7908)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Based on arch/arm/mm/init.c
4  *
5  * Copyright (C) 1995-2005 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 
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>
21 #include <linux/of.h>
22 #include <linux/of_fdt.h>
23 #include <linux/dma-direct.h>
24 #include <linux/dma-map-ops.h>
25 #include <linux/efi.h>
26 #include <linux/swiotlb.h>
27 #include <linux/vmalloc.h>
28 #include <linux/mm.h>
29 #include <linux/kexec.h>
30 #include <linux/crash_dump.h>
31 #include <linux/hugetlb.h>
32 #include <linux/acpi_iort.h>
33 #include <linux/kmemleak.h>
34 
35 #include <asm/boot.h>
36 #include <asm/fixmap.h>
37 #include <asm/kasan.h>
38 #include <asm/kernel-pgtable.h>
39 #include <asm/kvm_host.h>
40 #include <asm/memory.h>
41 #include <asm/numa.h>
42 #include <asm/sections.h>
43 #include <asm/setup.h>
44 #include <linux/sizes.h>
45 #include <asm/tlb.h>
46 #include <asm/alternative.h>
47 #include <asm/xen/swiotlb-xen.h>
48 
49 /*
50  * We need to be able to catch inadvertent references to memstart_addr
51  * that occur (potentially in generic code) before arm64_memblock_init()
52  * executes, which assigns it its actual value. So use a default value
53  * that cannot be mistaken for a real physical address.
54  */
55 s64 memstart_addr __ro_after_init = -1;
56 EXPORT_SYMBOL(memstart_addr);
57 
58 /*
59  * If the corresponding config options are enabled, we create both ZONE_DMA
60  * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
61  * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4).
62  * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory,
63  * otherwise it is empty.
64  */
65 phys_addr_t __ro_after_init arm64_dma_phys_limit;
66 
67 /* Current arm64 boot protocol requires 2MB alignment */
68 #define CRASH_ALIGN			SZ_2M
69 
70 #define CRASH_ADDR_LOW_MAX		arm64_dma_phys_limit
71 #define CRASH_ADDR_HIGH_MAX		(PHYS_MASK + 1)
72 
73 #define DEFAULT_CRASH_KERNEL_LOW_SIZE	(128UL << 20)
74 
75 static int __init reserve_crashkernel_low(unsigned long long low_size)
76 {
77 	unsigned long long low_base;
78 
79 	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
80 	if (!low_base) {
81 		pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
82 		return -ENOMEM;
83 	}
84 
85 	pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
86 		low_base, low_base + low_size, low_size >> 20);
87 
88 	crashk_low_res.start = low_base;
89 	crashk_low_res.end   = low_base + low_size - 1;
90 	insert_resource(&iomem_resource, &crashk_low_res);
91 
92 	return 0;
93 }
94 
95 /*
96  * reserve_crashkernel() - reserves memory for crash kernel
97  *
98  * This function reserves memory area given in "crashkernel=" kernel command
99  * line parameter. The memory reserved is used by dump capture kernel when
100  * primary kernel is crashing.
101  */
102 static void __init reserve_crashkernel(void)
103 {
104 	unsigned long long crash_base, crash_size;
105 	unsigned long long crash_low_size = 0;
106 	unsigned long long crash_max = CRASH_ADDR_LOW_MAX;
107 	char *cmdline = boot_command_line;
108 	int ret;
109 	bool fixed_base = false;
110 
111 	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
112 		return;
113 
114 	/* crashkernel=X[@offset] */
115 	ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
116 				&crash_size, &crash_base);
117 	if (ret == -ENOENT) {
118 		ret = parse_crashkernel_high(cmdline, 0, &crash_size, &crash_base);
119 		if (ret || !crash_size)
120 			return;
121 
122 		/*
123 		 * crashkernel=Y,low can be specified or not, but invalid value
124 		 * is not allowed.
125 		 */
126 		ret = parse_crashkernel_low(cmdline, 0, &crash_low_size, &crash_base);
127 		if (ret == -ENOENT)
128 			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
129 		else if (ret)
130 			return;
131 
132 		crash_max = CRASH_ADDR_HIGH_MAX;
133 	} else if (ret || !crash_size) {
134 		/* The specified value is invalid */
135 		return;
136 	}
137 
138 	crash_size = PAGE_ALIGN(crash_size);
139 
140 	/* User specifies base address explicitly. */
141 	if (crash_base) {
142 		fixed_base = true;
143 		crash_max = crash_base + crash_size;
144 	}
145 
146 retry:
147 	crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
148 					       crash_base, crash_max);
149 	if (!crash_base) {
150 		/*
151 		 * If the first attempt was for low memory, fall back to
152 		 * high memory, the minimum required low memory will be
153 		 * reserved later.
154 		 */
155 		if (!fixed_base && (crash_max == CRASH_ADDR_LOW_MAX)) {
156 			crash_max = CRASH_ADDR_HIGH_MAX;
157 			crash_low_size = DEFAULT_CRASH_KERNEL_LOW_SIZE;
158 			goto retry;
159 		}
160 
161 		pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
162 			crash_size);
163 		return;
164 	}
165 
166 	if ((crash_base > CRASH_ADDR_LOW_MAX - crash_low_size) &&
167 	     crash_low_size && reserve_crashkernel_low(crash_low_size)) {
168 		memblock_phys_free(crash_base, crash_size);
169 		return;
170 	}
171 
172 	pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
173 		crash_base, crash_base + crash_size, crash_size >> 20);
174 
175 	/*
176 	 * The crashkernel memory will be removed from the kernel linear
177 	 * map. Inform kmemleak so that it won't try to access it.
178 	 */
179 	kmemleak_ignore_phys(crash_base);
180 	if (crashk_low_res.end)
181 		kmemleak_ignore_phys(crashk_low_res.start);
182 
183 	crashk_res.start = crash_base;
184 	crashk_res.end = crash_base + crash_size - 1;
185 	insert_resource(&iomem_resource, &crashk_res);
186 }
187 
188 /*
189  * Return the maximum physical address for a zone accessible by the given bits
190  * limit. If DRAM starts above 32-bit, expand the zone to the maximum
191  * available memory, otherwise cap it at 32-bit.
192  */
193 static phys_addr_t __init max_zone_phys(unsigned int zone_bits)
194 {
195 	phys_addr_t zone_mask = DMA_BIT_MASK(zone_bits);
196 	phys_addr_t phys_start = memblock_start_of_DRAM();
197 
198 	if (phys_start > U32_MAX)
199 		zone_mask = PHYS_ADDR_MAX;
200 	else if (phys_start > zone_mask)
201 		zone_mask = U32_MAX;
202 
203 	return min(zone_mask, memblock_end_of_DRAM() - 1) + 1;
204 }
205 
206 static void __init zone_sizes_init(void)
207 {
208 	unsigned long max_zone_pfns[MAX_NR_ZONES]  = {0};
209 	unsigned int __maybe_unused acpi_zone_dma_bits;
210 	unsigned int __maybe_unused dt_zone_dma_bits;
211 	phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32);
212 
213 #ifdef CONFIG_ZONE_DMA
214 	acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address());
215 	dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL));
216 	zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits);
217 	arm64_dma_phys_limit = max_zone_phys(zone_dma_bits);
218 	max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
219 #endif
220 #ifdef CONFIG_ZONE_DMA32
221 	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
222 	if (!arm64_dma_phys_limit)
223 		arm64_dma_phys_limit = dma32_phys_limit;
224 #endif
225 	if (!arm64_dma_phys_limit)
226 		arm64_dma_phys_limit = PHYS_MASK + 1;
227 	max_zone_pfns[ZONE_NORMAL] = max_pfn;
228 
229 	free_area_init(max_zone_pfns);
230 }
231 
232 int pfn_is_map_memory(unsigned long pfn)
233 {
234 	phys_addr_t addr = PFN_PHYS(pfn);
235 
236 	/* avoid false positives for bogus PFNs, see comment in pfn_valid() */
237 	if (PHYS_PFN(addr) != pfn)
238 		return 0;
239 
240 	return memblock_is_map_memory(addr);
241 }
242 EXPORT_SYMBOL(pfn_is_map_memory);
243 
244 static phys_addr_t memory_limit __ro_after_init = PHYS_ADDR_MAX;
245 
246 /*
247  * Limit the memory size that was specified via FDT.
248  */
249 static int __init early_mem(char *p)
250 {
251 	if (!p)
252 		return 1;
253 
254 	memory_limit = memparse(p, &p) & PAGE_MASK;
255 	pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
256 
257 	return 0;
258 }
259 early_param("mem", early_mem);
260 
261 void __init arm64_memblock_init(void)
262 {
263 	s64 linear_region_size = PAGE_END - _PAGE_OFFSET(vabits_actual);
264 
265 	/*
266 	 * Corner case: 52-bit VA capable systems running KVM in nVHE mode may
267 	 * be limited in their ability to support a linear map that exceeds 51
268 	 * bits of VA space, depending on the placement of the ID map. Given
269 	 * that the placement of the ID map may be randomized, let's simply
270 	 * limit the kernel's linear map to 51 bits as well if we detect this
271 	 * configuration.
272 	 */
273 	if (IS_ENABLED(CONFIG_KVM) && vabits_actual == 52 &&
274 	    is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
275 		pr_info("Capping linear region to 51 bits for KVM in nVHE mode on LVA capable hardware.\n");
276 		linear_region_size = min_t(u64, linear_region_size, BIT(51));
277 	}
278 
279 	/* Remove memory above our supported physical address size */
280 	memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX);
281 
282 	/*
283 	 * Select a suitable value for the base of physical memory.
284 	 */
285 	memstart_addr = round_down(memblock_start_of_DRAM(),
286 				   ARM64_MEMSTART_ALIGN);
287 
288 	if ((memblock_end_of_DRAM() - memstart_addr) > linear_region_size)
289 		pr_warn("Memory doesn't fit in the linear mapping, VA_BITS too small\n");
290 
291 	/*
292 	 * Remove the memory that we will not be able to cover with the
293 	 * linear mapping. Take care not to clip the kernel which may be
294 	 * high in memory.
295 	 */
296 	memblock_remove(max_t(u64, memstart_addr + linear_region_size,
297 			__pa_symbol(_end)), ULLONG_MAX);
298 	if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
299 		/* ensure that memstart_addr remains sufficiently aligned */
300 		memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
301 					 ARM64_MEMSTART_ALIGN);
302 		memblock_remove(0, memstart_addr);
303 	}
304 
305 	/*
306 	 * If we are running with a 52-bit kernel VA config on a system that
307 	 * does not support it, we have to place the available physical
308 	 * memory in the 48-bit addressable part of the linear region, i.e.,
309 	 * we have to move it upward. Since memstart_addr represents the
310 	 * physical address of PAGE_OFFSET, we have to *subtract* from it.
311 	 */
312 	if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52))
313 		memstart_addr -= _PAGE_OFFSET(48) - _PAGE_OFFSET(52);
314 
315 	/*
316 	 * Apply the memory limit if it was set. Since the kernel may be loaded
317 	 * high up in memory, add back the kernel region that must be accessible
318 	 * via the linear mapping.
319 	 */
320 	if (memory_limit != PHYS_ADDR_MAX) {
321 		memblock_mem_limit_remove_map(memory_limit);
322 		memblock_add(__pa_symbol(_text), (u64)(_end - _text));
323 	}
324 
325 	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
326 		/*
327 		 * Add back the memory we just removed if it results in the
328 		 * initrd to become inaccessible via the linear mapping.
329 		 * Otherwise, this is a no-op
330 		 */
331 		u64 base = phys_initrd_start & PAGE_MASK;
332 		u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base;
333 
334 		/*
335 		 * We can only add back the initrd memory if we don't end up
336 		 * with more memory than we can address via the linear mapping.
337 		 * It is up to the bootloader to position the kernel and the
338 		 * initrd reasonably close to each other (i.e., within 32 GB of
339 		 * each other) so that all granule/#levels combinations can
340 		 * always access both.
341 		 */
342 		if (WARN(base < memblock_start_of_DRAM() ||
343 			 base + size > memblock_start_of_DRAM() +
344 				       linear_region_size,
345 			"initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
346 			phys_initrd_size = 0;
347 		} else {
348 			memblock_add(base, size);
349 			memblock_clear_nomap(base, size);
350 			memblock_reserve(base, size);
351 		}
352 	}
353 
354 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
355 		extern u16 memstart_offset_seed;
356 		u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
357 		int parange = cpuid_feature_extract_unsigned_field(
358 					mmfr0, ID_AA64MMFR0_EL1_PARANGE_SHIFT);
359 		s64 range = linear_region_size -
360 			    BIT(id_aa64mmfr0_parange_to_phys_shift(parange));
361 
362 		/*
363 		 * If the size of the linear region exceeds, by a sufficient
364 		 * margin, the size of the region that the physical memory can
365 		 * span, randomize the linear region as well.
366 		 */
367 		if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) {
368 			range /= ARM64_MEMSTART_ALIGN;
369 			memstart_addr -= ARM64_MEMSTART_ALIGN *
370 					 ((range * memstart_offset_seed) >> 16);
371 		}
372 	}
373 
374 	/*
375 	 * Register the kernel text, kernel data, initrd, and initial
376 	 * pagetables with memblock.
377 	 */
378 	memblock_reserve(__pa_symbol(_stext), _end - _stext);
379 	if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
380 		/* the generic initrd code expects virtual addresses */
381 		initrd_start = __phys_to_virt(phys_initrd_start);
382 		initrd_end = initrd_start + phys_initrd_size;
383 	}
384 
385 	early_init_fdt_scan_reserved_mem();
386 
387 	high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
388 }
389 
390 void __init bootmem_init(void)
391 {
392 	unsigned long min, max;
393 
394 	min = PFN_UP(memblock_start_of_DRAM());
395 	max = PFN_DOWN(memblock_end_of_DRAM());
396 
397 	early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
398 
399 	max_pfn = max_low_pfn = max;
400 	min_low_pfn = min;
401 
402 	arch_numa_init();
403 
404 	/*
405 	 * must be done after arch_numa_init() which calls numa_init() to
406 	 * initialize node_online_map that gets used in hugetlb_cma_reserve()
407 	 * while allocating required CMA size across online nodes.
408 	 */
409 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
410 	arm64_hugetlb_cma_reserve();
411 #endif
412 
413 	dma_pernuma_cma_reserve();
414 
415 	kvm_hyp_reserve();
416 
417 	/*
418 	 * sparse_init() tries to allocate memory from memblock, so must be
419 	 * done after the fixed reservations
420 	 */
421 	sparse_init();
422 	zone_sizes_init();
423 
424 	/*
425 	 * Reserve the CMA area after arm64_dma_phys_limit was initialised.
426 	 */
427 	dma_contiguous_reserve(arm64_dma_phys_limit);
428 
429 	/*
430 	 * request_standard_resources() depends on crashkernel's memory being
431 	 * reserved, so do it here.
432 	 */
433 	reserve_crashkernel();
434 
435 	memblock_dump_all();
436 }
437 
438 /*
439  * mem_init() marks the free areas in the mem_map and tells us how much memory
440  * is free.  This is done after various parts of the system have claimed their
441  * memory after the kernel image.
442  */
443 void __init mem_init(void)
444 {
445 	swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE);
446 
447 	/* this will put all unused low memory onto the freelists */
448 	memblock_free_all();
449 
450 	/*
451 	 * Check boundaries twice: Some fundamental inconsistencies can be
452 	 * detected at build time already.
453 	 */
454 #ifdef CONFIG_COMPAT
455 	BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
456 #endif
457 
458 	/*
459 	 * Selected page table levels should match when derived from
460 	 * scratch using the virtual address range and page size.
461 	 */
462 	BUILD_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
463 		     CONFIG_PGTABLE_LEVELS);
464 
465 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
466 		extern int sysctl_overcommit_memory;
467 		/*
468 		 * On a machine this small we won't get anywhere without
469 		 * overcommit, so turn it on by default.
470 		 */
471 		sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
472 	}
473 }
474 
475 void free_initmem(void)
476 {
477 	free_reserved_area(lm_alias(__init_begin),
478 			   lm_alias(__init_end),
479 			   POISON_FREE_INITMEM, "unused kernel");
480 	/*
481 	 * Unmap the __init region but leave the VM area in place. This
482 	 * prevents the region from being reused for kernel modules, which
483 	 * is not supported by kallsyms.
484 	 */
485 	vunmap_range((u64)__init_begin, (u64)__init_end);
486 }
487 
488 void dump_mem_limit(void)
489 {
490 	if (memory_limit != PHYS_ADDR_MAX) {
491 		pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
492 	} else {
493 		pr_emerg("Memory Limit: none\n");
494 	}
495 }
496