1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2c1cc1552SCatalin Marinas /* 3c1cc1552SCatalin Marinas * Based on arch/arm/mm/init.c 4c1cc1552SCatalin Marinas * 5c1cc1552SCatalin Marinas * Copyright (C) 1995-2005 Russell King 6c1cc1552SCatalin Marinas * Copyright (C) 2012 ARM Ltd. 7c1cc1552SCatalin Marinas */ 8c1cc1552SCatalin Marinas 9c1cc1552SCatalin Marinas #include <linux/kernel.h> 10c1cc1552SCatalin Marinas #include <linux/export.h> 11c1cc1552SCatalin Marinas #include <linux/errno.h> 12c1cc1552SCatalin Marinas #include <linux/swap.h> 13c1cc1552SCatalin Marinas #include <linux/init.h> 145a9e3e15SJisheng Zhang #include <linux/cache.h> 15c1cc1552SCatalin Marinas #include <linux/mman.h> 16c1cc1552SCatalin Marinas #include <linux/nodemask.h> 17c1cc1552SCatalin Marinas #include <linux/initrd.h> 18c1cc1552SCatalin Marinas #include <linux/gfp.h> 19c1cc1552SCatalin Marinas #include <linux/memblock.h> 20c1cc1552SCatalin Marinas #include <linux/sort.h> 21764b51eaSAKASHI Takahiro #include <linux/of.h> 22c1cc1552SCatalin Marinas #include <linux/of_fdt.h> 238b5369eaSNicolas Saenz Julienne #include <linux/dma-direct.h> 240b1abd1fSChristoph Hellwig #include <linux/dma-map-ops.h> 2586c8b27aSLeif Lindholm #include <linux/efi.h> 26a1e50a82SCatalin Marinas #include <linux/swiotlb.h> 27dae8c235SKefeng Wang #include <linux/vmalloc.h> 282077be67SLaura Abbott #include <linux/mm.h> 29764b51eaSAKASHI Takahiro #include <linux/kexec.h> 30e62aaeacSAKASHI Takahiro #include <linux/crash_dump.h> 31cf11e85fSRoman Gushchin #include <linux/hugetlb.h> 322b865293SArd Biesheuvel #include <linux/acpi_iort.h> 3385f58eb1SChen Wandun #include <linux/kmemleak.h> 34c1cc1552SCatalin Marinas 35a7f8de16SArd Biesheuvel #include <asm/boot.h> 3608375198SCatalin Marinas #include <asm/fixmap.h> 37f9040773SArd Biesheuvel #include <asm/kasan.h> 38a7f8de16SArd Biesheuvel #include <asm/kernel-pgtable.h> 39f320bc74SQuentin Perret #include <asm/kvm_host.h> 40aa03c428SMark Rutland #include <asm/memory.h> 411a2db300SGanapatrao Kulkarni #include <asm/numa.h> 42c1cc1552SCatalin Marinas #include <asm/sections.h> 43c1cc1552SCatalin Marinas #include <asm/setup.h> 4487dfb311SMasahiro Yamada #include <linux/sizes.h> 45c1cc1552SCatalin Marinas #include <asm/tlb.h> 46e039ee4eSAndre Przywara #include <asm/alternative.h> 47687842ecSChristoph Hellwig #include <asm/xen/swiotlb-xen.h> 48c1cc1552SCatalin Marinas 49a7f8de16SArd Biesheuvel /* 50a7f8de16SArd Biesheuvel * We need to be able to catch inadvertent references to memstart_addr 51a7f8de16SArd Biesheuvel * that occur (potentially in generic code) before arm64_memblock_init() 52a7f8de16SArd Biesheuvel * executes, which assigns it its actual value. So use a default value 53a7f8de16SArd Biesheuvel * that cannot be mistaken for a real physical address. 54a7f8de16SArd Biesheuvel */ 555a9e3e15SJisheng Zhang s64 memstart_addr __ro_after_init = -1; 5603ef055fSMark Rutland EXPORT_SYMBOL(memstart_addr); 5703ef055fSMark Rutland 581a8e1cefSNicolas Saenz Julienne /* 59d78050eeSCatalin Marinas * If the corresponding config options are enabled, we create both ZONE_DMA 60d78050eeSCatalin Marinas * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory 61d78050eeSCatalin Marinas * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4). 62d78050eeSCatalin Marinas * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory, 63d78050eeSCatalin Marinas * otherwise it is empty. 6403149563SVijay Balakrishna * 6503149563SVijay Balakrishna * Memory reservation for crash kernel either done early or deferred 6603149563SVijay Balakrishna * depending on DMA memory zones configs (ZONE_DMA) -- 6703149563SVijay Balakrishna * 6803149563SVijay Balakrishna * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized 6903149563SVijay Balakrishna * here instead of max_zone_phys(). This lets early reservation of 7003149563SVijay Balakrishna * crash kernel memory which has a dependency on arm64_dma_phys_limit. 7103149563SVijay Balakrishna * Reserving memory early for crash kernel allows linear creation of block 7203149563SVijay Balakrishna * mappings (greater than page-granularity) for all the memory bank rangs. 7303149563SVijay Balakrishna * In this scheme a comparatively quicker boot is observed. 7403149563SVijay Balakrishna * 7503149563SVijay Balakrishna * If ZONE_DMA configs are defined, crash kernel memory reservation 7603149563SVijay Balakrishna * is delayed until DMA zone memory range size initilazation performed in 7703149563SVijay Balakrishna * zone_sizes_init(). The defer is necessary to steer clear of DMA zone 7803149563SVijay Balakrishna * memory range to avoid overlap allocation. So crash kernel memory boundaries 7903149563SVijay Balakrishna * are not known when mapping all bank memory ranges, which otherwise means 8003149563SVijay Balakrishna * not possible to exclude crash kernel range from creating block mappings 8103149563SVijay Balakrishna * so page-granularity mappings are created for the entire memory range. 8203149563SVijay Balakrishna * Hence a slightly slower boot is observed. 8303149563SVijay Balakrishna * 8403149563SVijay Balakrishna * Note: Page-granularity mapppings are necessary for crash kernel memory 8503149563SVijay Balakrishna * range for shrinking its size via /sys/kernel/kexec_crash_size interface. 861a8e1cefSNicolas Saenz Julienne */ 8703149563SVijay Balakrishna #if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32) 8803149563SVijay Balakrishna phys_addr_t __ro_after_init arm64_dma_phys_limit; 8903149563SVijay Balakrishna #else 90*77009345SWill Deacon phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1; 9103149563SVijay Balakrishna #endif 92c1cc1552SCatalin Marinas 93764b51eaSAKASHI Takahiro #ifdef CONFIG_KEXEC_CORE 94764b51eaSAKASHI Takahiro /* 95764b51eaSAKASHI Takahiro * reserve_crashkernel() - reserves memory for crash kernel 96764b51eaSAKASHI Takahiro * 97764b51eaSAKASHI Takahiro * This function reserves memory area given in "crashkernel=" kernel command 98764b51eaSAKASHI Takahiro * line parameter. The memory reserved is used by dump capture kernel when 99764b51eaSAKASHI Takahiro * primary kernel is crashing. 100764b51eaSAKASHI Takahiro */ 101764b51eaSAKASHI Takahiro static void __init reserve_crashkernel(void) 102764b51eaSAKASHI Takahiro { 103764b51eaSAKASHI Takahiro unsigned long long crash_base, crash_size; 104a7259df7SMike Rapoport unsigned long long crash_max = arm64_dma_phys_limit; 105764b51eaSAKASHI Takahiro int ret; 106764b51eaSAKASHI Takahiro 107764b51eaSAKASHI Takahiro ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(), 108764b51eaSAKASHI Takahiro &crash_size, &crash_base); 109764b51eaSAKASHI Takahiro /* no crashkernel= or invalid value specified */ 110764b51eaSAKASHI Takahiro if (ret || !crash_size) 111764b51eaSAKASHI Takahiro return; 112764b51eaSAKASHI Takahiro 113764b51eaSAKASHI Takahiro crash_size = PAGE_ALIGN(crash_size); 114764b51eaSAKASHI Takahiro 115a7259df7SMike Rapoport /* User specifies base address explicitly. */ 116a7259df7SMike Rapoport if (crash_base) 117a7259df7SMike Rapoport crash_max = crash_base + crash_size; 118a7259df7SMike Rapoport 119764b51eaSAKASHI Takahiro /* Current arm64 boot protocol requires 2MB alignment */ 120a7259df7SMike Rapoport crash_base = memblock_phys_alloc_range(crash_size, SZ_2M, 121a7259df7SMike Rapoport crash_base, crash_max); 122a7259df7SMike Rapoport if (!crash_base) { 123764b51eaSAKASHI Takahiro pr_warn("cannot allocate crashkernel (size:0x%llx)\n", 124764b51eaSAKASHI Takahiro crash_size); 125764b51eaSAKASHI Takahiro return; 126764b51eaSAKASHI Takahiro } 127764b51eaSAKASHI Takahiro 128764b51eaSAKASHI Takahiro pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n", 129764b51eaSAKASHI Takahiro crash_base, crash_base + crash_size, crash_size >> 20); 130764b51eaSAKASHI Takahiro 13185f58eb1SChen Wandun /* 13285f58eb1SChen Wandun * The crashkernel memory will be removed from the kernel linear 13385f58eb1SChen Wandun * map. Inform kmemleak so that it won't try to access it. 13485f58eb1SChen Wandun */ 13585f58eb1SChen Wandun kmemleak_ignore_phys(crash_base); 136764b51eaSAKASHI Takahiro crashk_res.start = crash_base; 137764b51eaSAKASHI Takahiro crashk_res.end = crash_base + crash_size - 1; 138764b51eaSAKASHI Takahiro } 139764b51eaSAKASHI Takahiro #else 140764b51eaSAKASHI Takahiro static void __init reserve_crashkernel(void) 141764b51eaSAKASHI Takahiro { 142764b51eaSAKASHI Takahiro } 143764b51eaSAKASHI Takahiro #endif /* CONFIG_KEXEC_CORE */ 144764b51eaSAKASHI Takahiro 145d50314a6SCatalin Marinas /* 146791ab8b2SCatalin Marinas * Return the maximum physical address for a zone accessible by the given bits 147791ab8b2SCatalin Marinas * limit. If DRAM starts above 32-bit, expand the zone to the maximum 148791ab8b2SCatalin Marinas * available memory, otherwise cap it at 32-bit. 149d50314a6SCatalin Marinas */ 1501a8e1cefSNicolas Saenz Julienne static phys_addr_t __init max_zone_phys(unsigned int zone_bits) 151d50314a6SCatalin Marinas { 152791ab8b2SCatalin Marinas phys_addr_t zone_mask = DMA_BIT_MASK(zone_bits); 153791ab8b2SCatalin Marinas phys_addr_t phys_start = memblock_start_of_DRAM(); 154791ab8b2SCatalin Marinas 155791ab8b2SCatalin Marinas if (phys_start > U32_MAX) 156791ab8b2SCatalin Marinas zone_mask = PHYS_ADDR_MAX; 157791ab8b2SCatalin Marinas else if (phys_start > zone_mask) 158791ab8b2SCatalin Marinas zone_mask = U32_MAX; 159791ab8b2SCatalin Marinas 160791ab8b2SCatalin Marinas return min(zone_mask, memblock_end_of_DRAM() - 1) + 1; 161d50314a6SCatalin Marinas } 162d50314a6SCatalin Marinas 1631a2db300SGanapatrao Kulkarni static void __init zone_sizes_init(unsigned long min, unsigned long max) 1641a2db300SGanapatrao Kulkarni { 1651a2db300SGanapatrao Kulkarni unsigned long max_zone_pfns[MAX_NR_ZONES] = {0}; 1662b865293SArd Biesheuvel unsigned int __maybe_unused acpi_zone_dma_bits; 1678424ecddSNicolas Saenz Julienne unsigned int __maybe_unused dt_zone_dma_bits; 168d78050eeSCatalin Marinas phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32); 1691a2db300SGanapatrao Kulkarni 1701a8e1cefSNicolas Saenz Julienne #ifdef CONFIG_ZONE_DMA 1712b865293SArd Biesheuvel acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address()); 1728424ecddSNicolas Saenz Julienne dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL)); 1732b865293SArd Biesheuvel zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits); 1749804f8c6SNicolas Saenz Julienne arm64_dma_phys_limit = max_zone_phys(zone_dma_bits); 1751a8e1cefSNicolas Saenz Julienne max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit); 1761a8e1cefSNicolas Saenz Julienne #endif 1770c1f14edSMiles Chen #ifdef CONFIG_ZONE_DMA32 178d78050eeSCatalin Marinas max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit); 179d78050eeSCatalin Marinas if (!arm64_dma_phys_limit) 180d78050eeSCatalin Marinas arm64_dma_phys_limit = dma32_phys_limit; 1810c1f14edSMiles Chen #endif 1821a2db300SGanapatrao Kulkarni max_zone_pfns[ZONE_NORMAL] = max; 1831a2db300SGanapatrao Kulkarni 1849691a071SMike Rapoport free_area_init(max_zone_pfns); 1851a2db300SGanapatrao Kulkarni } 1861a2db300SGanapatrao Kulkarni 187873ba463SMike Rapoport int pfn_is_map_memory(unsigned long pfn) 188c1cc1552SCatalin Marinas { 189093bbe21SAnshuman Khandual phys_addr_t addr = PFN_PHYS(pfn); 1905ad356eaSGreg Hackmann 191873ba463SMike Rapoport /* avoid false positives for bogus PFNs, see comment in pfn_valid() */ 192093bbe21SAnshuman Khandual if (PHYS_PFN(addr) != pfn) 1935ad356eaSGreg Hackmann return 0; 1944ab21506SRobin Murphy 1955ad356eaSGreg Hackmann return memblock_is_map_memory(addr); 196c1cc1552SCatalin Marinas } 197873ba463SMike Rapoport EXPORT_SYMBOL(pfn_is_map_memory); 198c1cc1552SCatalin Marinas 199bb425a75SPeng Fan static phys_addr_t memory_limit __ro_after_init = PHYS_ADDR_MAX; 2006083fe74SMark Rutland 2016083fe74SMark Rutland /* 2026083fe74SMark Rutland * Limit the memory size that was specified via FDT. 2036083fe74SMark Rutland */ 2046083fe74SMark Rutland static int __init early_mem(char *p) 2056083fe74SMark Rutland { 2066083fe74SMark Rutland if (!p) 2076083fe74SMark Rutland return 1; 2086083fe74SMark Rutland 2096083fe74SMark Rutland memory_limit = memparse(p, &p) & PAGE_MASK; 2106083fe74SMark Rutland pr_notice("Memory limited to %lldMB\n", memory_limit >> 20); 2116083fe74SMark Rutland 2126083fe74SMark Rutland return 0; 2136083fe74SMark Rutland } 2146083fe74SMark Rutland early_param("mem", early_mem); 2156083fe74SMark Rutland 216c1cc1552SCatalin Marinas void __init arm64_memblock_init(void) 217c1cc1552SCatalin Marinas { 21888053ec8SArd Biesheuvel s64 linear_region_size = PAGE_END - _PAGE_OFFSET(vabits_actual); 21988053ec8SArd Biesheuvel 22088053ec8SArd Biesheuvel /* 22188053ec8SArd Biesheuvel * Corner case: 52-bit VA capable systems running KVM in nVHE mode may 22288053ec8SArd Biesheuvel * be limited in their ability to support a linear map that exceeds 51 22388053ec8SArd Biesheuvel * bits of VA space, depending on the placement of the ID map. Given 22488053ec8SArd Biesheuvel * that the placement of the ID map may be randomized, let's simply 22588053ec8SArd Biesheuvel * limit the kernel's linear map to 51 bits as well if we detect this 22688053ec8SArd Biesheuvel * configuration. 22788053ec8SArd Biesheuvel */ 22888053ec8SArd Biesheuvel if (IS_ENABLED(CONFIG_KVM) && vabits_actual == 52 && 22988053ec8SArd Biesheuvel is_hyp_mode_available() && !is_kernel_in_hyp_mode()) { 23088053ec8SArd Biesheuvel pr_info("Capping linear region to 51 bits for KVM in nVHE mode on LVA capable hardware.\n"); 23188053ec8SArd Biesheuvel linear_region_size = min_t(u64, linear_region_size, BIT(51)); 23288053ec8SArd Biesheuvel } 233a7f8de16SArd Biesheuvel 234e9eaa805SKristina Martsenko /* Remove memory above our supported physical address size */ 235e9eaa805SKristina Martsenko memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX); 236e9eaa805SKristina Martsenko 237a7f8de16SArd Biesheuvel /* 238a7f8de16SArd Biesheuvel * Select a suitable value for the base of physical memory. 239a7f8de16SArd Biesheuvel */ 240a7f8de16SArd Biesheuvel memstart_addr = round_down(memblock_start_of_DRAM(), 241a7f8de16SArd Biesheuvel ARM64_MEMSTART_ALIGN); 242a7f8de16SArd Biesheuvel 24331f80a4eSMarc Zyngier if ((memblock_end_of_DRAM() - memstart_addr) > linear_region_size) 24431f80a4eSMarc Zyngier pr_warn("Memory doesn't fit in the linear mapping, VA_BITS too small\n"); 24531f80a4eSMarc Zyngier 246a7f8de16SArd Biesheuvel /* 247a7f8de16SArd Biesheuvel * Remove the memory that we will not be able to cover with the 248a7f8de16SArd Biesheuvel * linear mapping. Take care not to clip the kernel which may be 249a7f8de16SArd Biesheuvel * high in memory. 250a7f8de16SArd Biesheuvel */ 2512077be67SLaura Abbott memblock_remove(max_t(u64, memstart_addr + linear_region_size, 2522077be67SLaura Abbott __pa_symbol(_end)), ULLONG_MAX); 2532958987fSArd Biesheuvel if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) { 2542958987fSArd Biesheuvel /* ensure that memstart_addr remains sufficiently aligned */ 2552958987fSArd Biesheuvel memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size, 2562958987fSArd Biesheuvel ARM64_MEMSTART_ALIGN); 2572958987fSArd Biesheuvel memblock_remove(0, memstart_addr); 2582958987fSArd Biesheuvel } 259a7f8de16SArd Biesheuvel 260a7f8de16SArd Biesheuvel /* 2617bc1a0f9SArd Biesheuvel * If we are running with a 52-bit kernel VA config on a system that 2627bc1a0f9SArd Biesheuvel * does not support it, we have to place the available physical 2637bc1a0f9SArd Biesheuvel * memory in the 48-bit addressable part of the linear region, i.e., 2647bc1a0f9SArd Biesheuvel * we have to move it upward. Since memstart_addr represents the 2657bc1a0f9SArd Biesheuvel * physical address of PAGE_OFFSET, we have to *subtract* from it. 2667bc1a0f9SArd Biesheuvel */ 2677bc1a0f9SArd Biesheuvel if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52)) 2687bc1a0f9SArd Biesheuvel memstart_addr -= _PAGE_OFFSET(48) - _PAGE_OFFSET(52); 2697bc1a0f9SArd Biesheuvel 2707bc1a0f9SArd Biesheuvel /* 271a7f8de16SArd Biesheuvel * Apply the memory limit if it was set. Since the kernel may be loaded 272a7f8de16SArd Biesheuvel * high up in memory, add back the kernel region that must be accessible 273a7f8de16SArd Biesheuvel * via the linear mapping. 274a7f8de16SArd Biesheuvel */ 275d7dc899aSStefan Agner if (memory_limit != PHYS_ADDR_MAX) { 276cb0a6502SDennis Chen memblock_mem_limit_remove_map(memory_limit); 2772077be67SLaura Abbott memblock_add(__pa_symbol(_text), (u64)(_end - _text)); 278a7f8de16SArd Biesheuvel } 2796083fe74SMark Rutland 280c756c592SFlorian Fainelli if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { 281177e15f0SArd Biesheuvel /* 282177e15f0SArd Biesheuvel * Add back the memory we just removed if it results in the 283177e15f0SArd Biesheuvel * initrd to become inaccessible via the linear mapping. 284177e15f0SArd Biesheuvel * Otherwise, this is a no-op 285177e15f0SArd Biesheuvel */ 286c756c592SFlorian Fainelli u64 base = phys_initrd_start & PAGE_MASK; 287d4d18e3eSBjorn Andersson u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base; 288177e15f0SArd Biesheuvel 289177e15f0SArd Biesheuvel /* 290177e15f0SArd Biesheuvel * We can only add back the initrd memory if we don't end up 291177e15f0SArd Biesheuvel * with more memory than we can address via the linear mapping. 292177e15f0SArd Biesheuvel * It is up to the bootloader to position the kernel and the 293177e15f0SArd Biesheuvel * initrd reasonably close to each other (i.e., within 32 GB of 294177e15f0SArd Biesheuvel * each other) so that all granule/#levels combinations can 295177e15f0SArd Biesheuvel * always access both. 296177e15f0SArd Biesheuvel */ 297177e15f0SArd Biesheuvel if (WARN(base < memblock_start_of_DRAM() || 298177e15f0SArd Biesheuvel base + size > memblock_start_of_DRAM() + 299177e15f0SArd Biesheuvel linear_region_size, 300177e15f0SArd Biesheuvel "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) { 30170b3d237SWill Deacon phys_initrd_size = 0; 302177e15f0SArd Biesheuvel } else { 303177e15f0SArd Biesheuvel memblock_remove(base, size); /* clear MEMBLOCK_ flags */ 304177e15f0SArd Biesheuvel memblock_add(base, size); 305177e15f0SArd Biesheuvel memblock_reserve(base, size); 306177e15f0SArd Biesheuvel } 307177e15f0SArd Biesheuvel } 308177e15f0SArd Biesheuvel 309c031a421SArd Biesheuvel if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { 310c031a421SArd Biesheuvel extern u16 memstart_offset_seed; 31197d6786eSArd Biesheuvel u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1); 31297d6786eSArd Biesheuvel int parange = cpuid_feature_extract_unsigned_field( 31397d6786eSArd Biesheuvel mmfr0, ID_AA64MMFR0_PARANGE_SHIFT); 31497d6786eSArd Biesheuvel s64 range = linear_region_size - 31597d6786eSArd Biesheuvel BIT(id_aa64mmfr0_parange_to_phys_shift(parange)); 316c031a421SArd Biesheuvel 317c031a421SArd Biesheuvel /* 318c031a421SArd Biesheuvel * If the size of the linear region exceeds, by a sufficient 31997d6786eSArd Biesheuvel * margin, the size of the region that the physical memory can 32097d6786eSArd Biesheuvel * span, randomize the linear region as well. 321c031a421SArd Biesheuvel */ 32297d6786eSArd Biesheuvel if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) { 323c8a43c18SYueyi Li range /= ARM64_MEMSTART_ALIGN; 324c031a421SArd Biesheuvel memstart_addr -= ARM64_MEMSTART_ALIGN * 325c031a421SArd Biesheuvel ((range * memstart_offset_seed) >> 16); 326c031a421SArd Biesheuvel } 327c031a421SArd Biesheuvel } 3282d5a5612SCatalin Marinas 329bd00cd5fSMark Rutland /* 330bd00cd5fSMark Rutland * Register the kernel text, kernel data, initrd, and initial 331bd00cd5fSMark Rutland * pagetables with memblock. 332bd00cd5fSMark Rutland */ 333e2a073ddSArd Biesheuvel memblock_reserve(__pa_symbol(_stext), _end - _stext); 334c756c592SFlorian Fainelli if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) { 335a89dea58SArd Biesheuvel /* the generic initrd code expects virtual addresses */ 336c756c592SFlorian Fainelli initrd_start = __phys_to_virt(phys_initrd_start); 337c756c592SFlorian Fainelli initrd_end = initrd_start + phys_initrd_size; 338a89dea58SArd Biesheuvel } 339c1cc1552SCatalin Marinas 3409bf14b7cSMarek Szyprowski early_init_fdt_scan_reserved_mem(); 3412d5a5612SCatalin Marinas 34203149563SVijay Balakrishna if (!IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32)) 34303149563SVijay Balakrishna reserve_crashkernel(); 34403149563SVijay Balakrishna 345f24e5834SSteve Capper high_memory = __va(memblock_end_of_DRAM() - 1) + 1; 346c1cc1552SCatalin Marinas } 347c1cc1552SCatalin Marinas 348c1cc1552SCatalin Marinas void __init bootmem_init(void) 349c1cc1552SCatalin Marinas { 350c1cc1552SCatalin Marinas unsigned long min, max; 351c1cc1552SCatalin Marinas 352c1cc1552SCatalin Marinas min = PFN_UP(memblock_start_of_DRAM()); 353c1cc1552SCatalin Marinas max = PFN_DOWN(memblock_end_of_DRAM()); 354c1cc1552SCatalin Marinas 35536dd9086SVladimir Murzin early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT); 35636dd9086SVladimir Murzin 3571a2db300SGanapatrao Kulkarni max_pfn = max_low_pfn = max; 35819d6242eSMiles Chen min_low_pfn = min; 3591a2db300SGanapatrao Kulkarni 360eb75541fSAtish Patra arch_numa_init(); 361618e0786SBarry Song 362618e0786SBarry Song /* 363eb75541fSAtish Patra * must be done after arch_numa_init() which calls numa_init() to 364618e0786SBarry Song * initialize node_online_map that gets used in hugetlb_cma_reserve() 365618e0786SBarry Song * while allocating required CMA size across online nodes. 366618e0786SBarry Song */ 367abb7962aSAnshuman Khandual #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA) 368abb7962aSAnshuman Khandual arm64_hugetlb_cma_reserve(); 369618e0786SBarry Song #endif 370618e0786SBarry Song 371c6303ab9SBarry Song dma_pernuma_cma_reserve(); 372c6303ab9SBarry Song 373f320bc74SQuentin Perret kvm_hyp_reserve(); 374f320bc74SQuentin Perret 375c1cc1552SCatalin Marinas /* 376c89ab04fSMike Rapoport * sparse_init() tries to allocate memory from memblock, so must be 377c89ab04fSMike Rapoport * done after the fixed reservations 378c1cc1552SCatalin Marinas */ 379c1cc1552SCatalin Marinas sparse_init(); 380c1cc1552SCatalin Marinas zone_sizes_init(min, max); 381c1cc1552SCatalin Marinas 3820a30c535SNicolas Saenz Julienne /* 383d78050eeSCatalin Marinas * Reserve the CMA area after arm64_dma_phys_limit was initialised. 384d78050eeSCatalin Marinas */ 385d78050eeSCatalin Marinas dma_contiguous_reserve(arm64_dma_phys_limit); 386d78050eeSCatalin Marinas 387d78050eeSCatalin Marinas /* 3880a30c535SNicolas Saenz Julienne * request_standard_resources() depends on crashkernel's memory being 3890a30c535SNicolas Saenz Julienne * reserved, so do it here. 3900a30c535SNicolas Saenz Julienne */ 39103149563SVijay Balakrishna if (IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32)) 3920a30c535SNicolas Saenz Julienne reserve_crashkernel(); 3930a30c535SNicolas Saenz Julienne 3941a2db300SGanapatrao Kulkarni memblock_dump_all(); 395c1cc1552SCatalin Marinas } 396c1cc1552SCatalin Marinas 397c1cc1552SCatalin Marinas /* 398c1cc1552SCatalin Marinas * mem_init() marks the free areas in the mem_map and tells us how much memory 399c1cc1552SCatalin Marinas * is free. This is done after various parts of the system have claimed their 400c1cc1552SCatalin Marinas * memory after the kernel image. 401c1cc1552SCatalin Marinas */ 402c1cc1552SCatalin Marinas void __init mem_init(void) 403c1cc1552SCatalin Marinas { 404ae7871beSGeert Uytterhoeven if (swiotlb_force == SWIOTLB_FORCE || 405d78050eeSCatalin Marinas max_pfn > PFN_DOWN(arm64_dma_phys_limit)) 406a1e50a82SCatalin Marinas swiotlb_init(1); 407687842ecSChristoph Hellwig else if (!xen_swiotlb_detect()) 408524dabe1SAlexander Graf swiotlb_force = SWIOTLB_NO_FORCE; 409a1e50a82SCatalin Marinas 410bee4ebd1SJiang Liu /* this will put all unused low memory onto the freelists */ 411c6ffc5caSMike Rapoport memblock_free_all(); 412c1cc1552SCatalin Marinas 413c1cc1552SCatalin Marinas /* 414c1cc1552SCatalin Marinas * Check boundaries twice: Some fundamental inconsistencies can be 415c1cc1552SCatalin Marinas * detected at build time already. 416c1cc1552SCatalin Marinas */ 417c1cc1552SCatalin Marinas #ifdef CONFIG_COMPAT 418363524d2SSteve Capper BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64); 419c1cc1552SCatalin Marinas #endif 420c1cc1552SCatalin Marinas 4217e04cc91SAnshuman Khandual /* 4227e04cc91SAnshuman Khandual * Selected page table levels should match when derived from 4237e04cc91SAnshuman Khandual * scratch using the virtual address range and page size. 4247e04cc91SAnshuman Khandual */ 4257e04cc91SAnshuman Khandual BUILD_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) != 4267e04cc91SAnshuman Khandual CONFIG_PGTABLE_LEVELS); 4277e04cc91SAnshuman Khandual 428bee4ebd1SJiang Liu if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) { 429c1cc1552SCatalin Marinas extern int sysctl_overcommit_memory; 430c1cc1552SCatalin Marinas /* 431c1cc1552SCatalin Marinas * On a machine this small we won't get anywhere without 432c1cc1552SCatalin Marinas * overcommit, so turn it on by default. 433c1cc1552SCatalin Marinas */ 434c1cc1552SCatalin Marinas sysctl_overcommit_memory = OVERCOMMIT_ALWAYS; 435c1cc1552SCatalin Marinas } 436c1cc1552SCatalin Marinas } 437c1cc1552SCatalin Marinas 438c1cc1552SCatalin Marinas void free_initmem(void) 439c1cc1552SCatalin Marinas { 4402077be67SLaura Abbott free_reserved_area(lm_alias(__init_begin), 4412077be67SLaura Abbott lm_alias(__init_end), 4426ec939f8SAnshuman Khandual POISON_FREE_INITMEM, "unused kernel"); 443dae8c235SKefeng Wang /* 444dae8c235SKefeng Wang * Unmap the __init region but leave the VM area in place. This 445dae8c235SKefeng Wang * prevents the region from being reused for kernel modules, which 446dae8c235SKefeng Wang * is not supported by kallsyms. 447dae8c235SKefeng Wang */ 4484ad0ae8cSNicholas Piggin vunmap_range((u64)__init_begin, (u64)__init_end); 449c1cc1552SCatalin Marinas } 450c1cc1552SCatalin Marinas 451638d5031SAnshuman Khandual void dump_mem_limit(void) 452a7f8de16SArd Biesheuvel { 453d7dc899aSStefan Agner if (memory_limit != PHYS_ADDR_MAX) { 454a7f8de16SArd Biesheuvel pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20); 455a7f8de16SArd Biesheuvel } else { 456a7f8de16SArd Biesheuvel pr_emerg("Memory Limit: none\n"); 457a7f8de16SArd Biesheuvel } 458a7f8de16SArd Biesheuvel } 459