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