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-mapping.h> 25 #include <linux/dma-contiguous.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 34 #include <asm/boot.h> 35 #include <asm/fixmap.h> 36 #include <asm/kasan.h> 37 #include <asm/kernel-pgtable.h> 38 #include <asm/memory.h> 39 #include <asm/numa.h> 40 #include <asm/sections.h> 41 #include <asm/setup.h> 42 #include <linux/sizes.h> 43 #include <asm/tlb.h> 44 #include <asm/alternative.h> 45 46 #define ARM64_ZONE_DMA_BITS 30 47 48 /* 49 * We need to be able to catch inadvertent references to memstart_addr 50 * that occur (potentially in generic code) before arm64_memblock_init() 51 * executes, which assigns it its actual value. So use a default value 52 * that cannot be mistaken for a real physical address. 53 */ 54 s64 memstart_addr __ro_after_init = -1; 55 EXPORT_SYMBOL(memstart_addr); 56 57 s64 physvirt_offset __ro_after_init; 58 EXPORT_SYMBOL(physvirt_offset); 59 60 struct page *vmemmap __ro_after_init; 61 EXPORT_SYMBOL(vmemmap); 62 63 /* 64 * We create both ZONE_DMA and ZONE_DMA32. ZONE_DMA covers the first 1G of 65 * memory as some devices, namely the Raspberry Pi 4, have peripherals with 66 * this limited view of the memory. ZONE_DMA32 will cover the rest of the 32 67 * bit addressable memory area. 68 */ 69 phys_addr_t arm64_dma_phys_limit __ro_after_init; 70 static phys_addr_t arm64_dma32_phys_limit __ro_after_init; 71 72 #ifdef CONFIG_KEXEC_CORE 73 /* 74 * reserve_crashkernel() - reserves memory for crash kernel 75 * 76 * This function reserves memory area given in "crashkernel=" kernel command 77 * line parameter. The memory reserved is used by dump capture kernel when 78 * primary kernel is crashing. 79 */ 80 static void __init reserve_crashkernel(void) 81 { 82 unsigned long long crash_base, crash_size; 83 int ret; 84 85 ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 86 &crash_size, &crash_base); 87 /* no crashkernel= or invalid value specified */ 88 if (ret || !crash_size) 89 return; 90 91 crash_size = PAGE_ALIGN(crash_size); 92 93 if (crash_base == 0) { 94 /* Current arm64 boot protocol requires 2MB alignment */ 95 crash_base = memblock_find_in_range(0, arm64_dma32_phys_limit, 96 crash_size, SZ_2M); 97 if (crash_base == 0) { 98 pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 99 crash_size); 100 return; 101 } 102 } else { 103 /* User specifies base address explicitly. */ 104 if (!memblock_is_region_memory(crash_base, crash_size)) { 105 pr_warn("cannot reserve crashkernel: region is not memory\n"); 106 return; 107 } 108 109 if (memblock_is_region_reserved(crash_base, crash_size)) { 110 pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n"); 111 return; 112 } 113 114 if (!IS_ALIGNED(crash_base, SZ_2M)) { 115 pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n"); 116 return; 117 } 118 } 119 memblock_reserve(crash_base, crash_size); 120 121 pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 122 crash_base, crash_base + crash_size, crash_size >> 20); 123 124 crashk_res.start = crash_base; 125 crashk_res.end = crash_base + crash_size - 1; 126 } 127 #else 128 static void __init reserve_crashkernel(void) 129 { 130 } 131 #endif /* CONFIG_KEXEC_CORE */ 132 133 #ifdef CONFIG_CRASH_DUMP 134 static int __init early_init_dt_scan_elfcorehdr(unsigned long node, 135 const char *uname, int depth, void *data) 136 { 137 const __be32 *reg; 138 int len; 139 140 if (depth != 1 || strcmp(uname, "chosen") != 0) 141 return 0; 142 143 reg = of_get_flat_dt_prop(node, "linux,elfcorehdr", &len); 144 if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells))) 145 return 1; 146 147 elfcorehdr_addr = dt_mem_next_cell(dt_root_addr_cells, ®); 148 elfcorehdr_size = dt_mem_next_cell(dt_root_size_cells, ®); 149 150 return 1; 151 } 152 153 /* 154 * reserve_elfcorehdr() - reserves memory for elf core header 155 * 156 * This function reserves the memory occupied by an elf core header 157 * described in the device tree. This region contains all the 158 * information about primary kernel's core image and is used by a dump 159 * capture kernel to access the system memory on primary kernel. 160 */ 161 static void __init reserve_elfcorehdr(void) 162 { 163 of_scan_flat_dt(early_init_dt_scan_elfcorehdr, NULL); 164 165 if (!elfcorehdr_size) 166 return; 167 168 if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) { 169 pr_warn("elfcorehdr is overlapped\n"); 170 return; 171 } 172 173 memblock_reserve(elfcorehdr_addr, elfcorehdr_size); 174 175 pr_info("Reserving %lldKB of memory at 0x%llx for elfcorehdr\n", 176 elfcorehdr_size >> 10, elfcorehdr_addr); 177 } 178 #else 179 static void __init reserve_elfcorehdr(void) 180 { 181 } 182 #endif /* CONFIG_CRASH_DUMP */ 183 184 /* 185 * Return the maximum physical address for a zone with a given address size 186 * limit. It currently assumes that for memory starting above 4G, 32-bit 187 * devices will use a DMA offset. 188 */ 189 static phys_addr_t __init max_zone_phys(unsigned int zone_bits) 190 { 191 phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, zone_bits); 192 return min(offset + (1ULL << zone_bits), memblock_end_of_DRAM()); 193 } 194 195 #ifdef CONFIG_NUMA 196 197 static void __init zone_sizes_init(unsigned long min, unsigned long max) 198 { 199 unsigned long max_zone_pfns[MAX_NR_ZONES] = {0}; 200 201 #ifdef CONFIG_ZONE_DMA 202 max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit); 203 #endif 204 #ifdef CONFIG_ZONE_DMA32 205 max_zone_pfns[ZONE_DMA32] = PFN_DOWN(arm64_dma32_phys_limit); 206 #endif 207 max_zone_pfns[ZONE_NORMAL] = max; 208 209 free_area_init_nodes(max_zone_pfns); 210 } 211 212 #else 213 214 static void __init zone_sizes_init(unsigned long min, unsigned long max) 215 { 216 struct memblock_region *reg; 217 unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES]; 218 unsigned long __maybe_unused max_dma, max_dma32; 219 220 memset(zone_size, 0, sizeof(zone_size)); 221 222 max_dma = max_dma32 = min; 223 #ifdef CONFIG_ZONE_DMA 224 max_dma = max_dma32 = PFN_DOWN(arm64_dma_phys_limit); 225 zone_size[ZONE_DMA] = max_dma - min; 226 #endif 227 #ifdef CONFIG_ZONE_DMA32 228 max_dma32 = PFN_DOWN(arm64_dma32_phys_limit); 229 zone_size[ZONE_DMA32] = max_dma32 - max_dma; 230 #endif 231 zone_size[ZONE_NORMAL] = max - max_dma32; 232 233 memcpy(zhole_size, zone_size, sizeof(zhole_size)); 234 235 for_each_memblock(memory, reg) { 236 unsigned long start = memblock_region_memory_base_pfn(reg); 237 unsigned long end = memblock_region_memory_end_pfn(reg); 238 239 #ifdef CONFIG_ZONE_DMA 240 if (start >= min && start < max_dma) { 241 unsigned long dma_end = min(end, max_dma); 242 zhole_size[ZONE_DMA] -= dma_end - start; 243 start = dma_end; 244 } 245 #endif 246 #ifdef CONFIG_ZONE_DMA32 247 if (start >= max_dma && start < max_dma32) { 248 unsigned long dma32_end = min(end, max_dma32); 249 zhole_size[ZONE_DMA32] -= dma32_end - start; 250 start = dma32_end; 251 } 252 #endif 253 if (start >= max_dma32 && start < max) { 254 unsigned long normal_end = min(end, max); 255 zhole_size[ZONE_NORMAL] -= normal_end - start; 256 } 257 } 258 259 free_area_init_node(0, zone_size, min, zhole_size); 260 } 261 262 #endif /* CONFIG_NUMA */ 263 264 int pfn_valid(unsigned long pfn) 265 { 266 phys_addr_t addr = pfn << PAGE_SHIFT; 267 268 if ((addr >> PAGE_SHIFT) != pfn) 269 return 0; 270 271 #ifdef CONFIG_SPARSEMEM 272 if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS) 273 return 0; 274 275 if (!valid_section(__nr_to_section(pfn_to_section_nr(pfn)))) 276 return 0; 277 #endif 278 return memblock_is_map_memory(addr); 279 } 280 EXPORT_SYMBOL(pfn_valid); 281 282 static phys_addr_t memory_limit = PHYS_ADDR_MAX; 283 284 /* 285 * Limit the memory size that was specified via FDT. 286 */ 287 static int __init early_mem(char *p) 288 { 289 if (!p) 290 return 1; 291 292 memory_limit = memparse(p, &p) & PAGE_MASK; 293 pr_notice("Memory limited to %lldMB\n", memory_limit >> 20); 294 295 return 0; 296 } 297 early_param("mem", early_mem); 298 299 static int __init early_init_dt_scan_usablemem(unsigned long node, 300 const char *uname, int depth, void *data) 301 { 302 struct memblock_region *usablemem = data; 303 const __be32 *reg; 304 int len; 305 306 if (depth != 1 || strcmp(uname, "chosen") != 0) 307 return 0; 308 309 reg = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len); 310 if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells))) 311 return 1; 312 313 usablemem->base = dt_mem_next_cell(dt_root_addr_cells, ®); 314 usablemem->size = dt_mem_next_cell(dt_root_size_cells, ®); 315 316 return 1; 317 } 318 319 static void __init fdt_enforce_memory_region(void) 320 { 321 struct memblock_region reg = { 322 .size = 0, 323 }; 324 325 of_scan_flat_dt(early_init_dt_scan_usablemem, ®); 326 327 if (reg.size) 328 memblock_cap_memory_range(reg.base, reg.size); 329 } 330 331 void __init arm64_memblock_init(void) 332 { 333 const s64 linear_region_size = BIT(vabits_actual - 1); 334 335 /* Handle linux,usable-memory-range property */ 336 fdt_enforce_memory_region(); 337 338 /* Remove memory above our supported physical address size */ 339 memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX); 340 341 /* 342 * Select a suitable value for the base of physical memory. 343 */ 344 memstart_addr = round_down(memblock_start_of_DRAM(), 345 ARM64_MEMSTART_ALIGN); 346 347 physvirt_offset = PHYS_OFFSET - PAGE_OFFSET; 348 349 vmemmap = ((struct page *)VMEMMAP_START - (memstart_addr >> PAGE_SHIFT)); 350 351 /* 352 * If we are running with a 52-bit kernel VA config on a system that 353 * does not support it, we have to offset our vmemmap and physvirt_offset 354 * s.t. we avoid the 52-bit portion of the direct linear map 355 */ 356 if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52)) { 357 vmemmap += (_PAGE_OFFSET(48) - _PAGE_OFFSET(52)) >> PAGE_SHIFT; 358 physvirt_offset = PHYS_OFFSET - _PAGE_OFFSET(48); 359 } 360 361 /* 362 * Remove the memory that we will not be able to cover with the 363 * linear mapping. Take care not to clip the kernel which may be 364 * high in memory. 365 */ 366 memblock_remove(max_t(u64, memstart_addr + linear_region_size, 367 __pa_symbol(_end)), ULLONG_MAX); 368 if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) { 369 /* ensure that memstart_addr remains sufficiently aligned */ 370 memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size, 371 ARM64_MEMSTART_ALIGN); 372 memblock_remove(0, memstart_addr); 373 } 374 375 /* 376 * Apply the memory limit if it was set. Since the kernel may be loaded 377 * high up in memory, add back the kernel region that must be accessible 378 * via the linear mapping. 379 */ 380 if (memory_limit != PHYS_ADDR_MAX) { 381 memblock_mem_limit_remove_map(memory_limit); 382 memblock_add(__pa_symbol(_text), (u64)(_end - _text)); 383 } 384 385 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { 386 /* 387 * Add back the memory we just removed if it results in the 388 * initrd to become inaccessible via the linear mapping. 389 * Otherwise, this is a no-op 390 */ 391 u64 base = phys_initrd_start & PAGE_MASK; 392 u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base; 393 394 /* 395 * We can only add back the initrd memory if we don't end up 396 * with more memory than we can address via the linear mapping. 397 * It is up to the bootloader to position the kernel and the 398 * initrd reasonably close to each other (i.e., within 32 GB of 399 * each other) so that all granule/#levels combinations can 400 * always access both. 401 */ 402 if (WARN(base < memblock_start_of_DRAM() || 403 base + size > memblock_start_of_DRAM() + 404 linear_region_size, 405 "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) { 406 phys_initrd_size = 0; 407 } else { 408 memblock_remove(base, size); /* clear MEMBLOCK_ flags */ 409 memblock_add(base, size); 410 memblock_reserve(base, size); 411 } 412 } 413 414 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 415 extern u16 memstart_offset_seed; 416 u64 range = linear_region_size - 417 (memblock_end_of_DRAM() - memblock_start_of_DRAM()); 418 419 /* 420 * If the size of the linear region exceeds, by a sufficient 421 * margin, the size of the region that the available physical 422 * memory spans, randomize the linear region as well. 423 */ 424 if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) { 425 range /= ARM64_MEMSTART_ALIGN; 426 memstart_addr -= ARM64_MEMSTART_ALIGN * 427 ((range * memstart_offset_seed) >> 16); 428 } 429 } 430 431 /* 432 * Register the kernel text, kernel data, initrd, and initial 433 * pagetables with memblock. 434 */ 435 memblock_reserve(__pa_symbol(_text), _end - _text); 436 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { 437 /* the generic initrd code expects virtual addresses */ 438 initrd_start = __phys_to_virt(phys_initrd_start); 439 initrd_end = initrd_start + phys_initrd_size; 440 } 441 442 early_init_fdt_scan_reserved_mem(); 443 444 if (IS_ENABLED(CONFIG_ZONE_DMA)) { 445 zone_dma_bits = ARM64_ZONE_DMA_BITS; 446 arm64_dma_phys_limit = max_zone_phys(ARM64_ZONE_DMA_BITS); 447 } 448 449 if (IS_ENABLED(CONFIG_ZONE_DMA32)) 450 arm64_dma32_phys_limit = max_zone_phys(32); 451 else 452 arm64_dma32_phys_limit = PHYS_MASK + 1; 453 454 reserve_crashkernel(); 455 456 reserve_elfcorehdr(); 457 458 high_memory = __va(memblock_end_of_DRAM() - 1) + 1; 459 460 dma_contiguous_reserve(arm64_dma32_phys_limit); 461 462 #ifdef CONFIG_ARM64_4K_PAGES 463 hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); 464 #endif 465 466 } 467 468 void __init bootmem_init(void) 469 { 470 unsigned long min, max; 471 472 min = PFN_UP(memblock_start_of_DRAM()); 473 max = PFN_DOWN(memblock_end_of_DRAM()); 474 475 early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT); 476 477 max_pfn = max_low_pfn = max; 478 min_low_pfn = min; 479 480 arm64_numa_init(); 481 /* 482 * Sparsemem tries to allocate bootmem in memory_present(), so must be 483 * done after the fixed reservations. 484 */ 485 memblocks_present(); 486 487 sparse_init(); 488 zone_sizes_init(min, max); 489 490 memblock_dump_all(); 491 } 492 493 #ifndef CONFIG_SPARSEMEM_VMEMMAP 494 static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn) 495 { 496 struct page *start_pg, *end_pg; 497 unsigned long pg, pgend; 498 499 /* 500 * Convert start_pfn/end_pfn to a struct page pointer. 501 */ 502 start_pg = pfn_to_page(start_pfn - 1) + 1; 503 end_pg = pfn_to_page(end_pfn - 1) + 1; 504 505 /* 506 * Convert to physical addresses, and round start upwards and end 507 * downwards. 508 */ 509 pg = (unsigned long)PAGE_ALIGN(__pa(start_pg)); 510 pgend = (unsigned long)__pa(end_pg) & PAGE_MASK; 511 512 /* 513 * If there are free pages between these, free the section of the 514 * memmap array. 515 */ 516 if (pg < pgend) 517 memblock_free(pg, pgend - pg); 518 } 519 520 /* 521 * The mem_map array can get very big. Free the unused area of the memory map. 522 */ 523 static void __init free_unused_memmap(void) 524 { 525 unsigned long start, prev_end = 0; 526 struct memblock_region *reg; 527 528 for_each_memblock(memory, reg) { 529 start = __phys_to_pfn(reg->base); 530 531 #ifdef CONFIG_SPARSEMEM 532 /* 533 * Take care not to free memmap entries that don't exist due 534 * to SPARSEMEM sections which aren't present. 535 */ 536 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); 537 #endif 538 /* 539 * If we had a previous bank, and there is a space between the 540 * current bank and the previous, free it. 541 */ 542 if (prev_end && prev_end < start) 543 free_memmap(prev_end, start); 544 545 /* 546 * Align up here since the VM subsystem insists that the 547 * memmap entries are valid from the bank end aligned to 548 * MAX_ORDER_NR_PAGES. 549 */ 550 prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size), 551 MAX_ORDER_NR_PAGES); 552 } 553 554 #ifdef CONFIG_SPARSEMEM 555 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) 556 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); 557 #endif 558 } 559 #endif /* !CONFIG_SPARSEMEM_VMEMMAP */ 560 561 /* 562 * mem_init() marks the free areas in the mem_map and tells us how much memory 563 * is free. This is done after various parts of the system have claimed their 564 * memory after the kernel image. 565 */ 566 void __init mem_init(void) 567 { 568 if (swiotlb_force == SWIOTLB_FORCE || 569 max_pfn > PFN_DOWN(arm64_dma_phys_limit ? : arm64_dma32_phys_limit)) 570 swiotlb_init(1); 571 else 572 swiotlb_force = SWIOTLB_NO_FORCE; 573 574 set_max_mapnr(max_pfn - PHYS_PFN_OFFSET); 575 576 #ifndef CONFIG_SPARSEMEM_VMEMMAP 577 free_unused_memmap(); 578 #endif 579 /* this will put all unused low memory onto the freelists */ 580 memblock_free_all(); 581 582 mem_init_print_info(NULL); 583 584 /* 585 * Check boundaries twice: Some fundamental inconsistencies can be 586 * detected at build time already. 587 */ 588 #ifdef CONFIG_COMPAT 589 BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64); 590 #endif 591 592 if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { 593 extern int sysctl_overcommit_memory; 594 /* 595 * On a machine this small we won't get anywhere without 596 * overcommit, so turn it on by default. 597 */ 598 sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 599 } 600 } 601 602 void free_initmem(void) 603 { 604 free_reserved_area(lm_alias(__init_begin), 605 lm_alias(__init_end), 606 POISON_FREE_INITMEM, "unused kernel"); 607 /* 608 * Unmap the __init region but leave the VM area in place. This 609 * prevents the region from being reused for kernel modules, which 610 * is not supported by kallsyms. 611 */ 612 unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin)); 613 } 614 615 /* 616 * Dump out memory limit information on panic. 617 */ 618 static int dump_mem_limit(struct notifier_block *self, unsigned long v, void *p) 619 { 620 if (memory_limit != PHYS_ADDR_MAX) { 621 pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20); 622 } else { 623 pr_emerg("Memory Limit: none\n"); 624 } 625 return 0; 626 } 627 628 static struct notifier_block mem_limit_notifier = { 629 .notifier_call = dump_mem_limit, 630 }; 631 632 static int __init register_mem_limit_dumper(void) 633 { 634 atomic_notifier_chain_register(&panic_notifier_list, 635 &mem_limit_notifier); 636 return 0; 637 } 638 __initcall(register_mem_limit_dumper); 639