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