Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Based on arch/arm/mm/init.c
0004  *
0005  * Copyright (C) 1995-2005 Russell King
0006  * Copyright (C) 2012 ARM Ltd.
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/export.h>
0011 #include <linux/errno.h>
0012 #include <linux/swap.h>
0013 #include <linux/init.h>
0014 #include <linux/cache.h>
0015 #include <linux/mman.h>
0016 #include <linux/nodemask.h>
0017 #include <linux/initrd.h>
0018 #include <linux/gfp.h>
0019 #include <linux/memblock.h>
0020 #include <linux/sort.h>
0021 #include <linux/of.h>
0022 #include <linux/of_fdt.h>
0023 #include <linux/dma-direct.h>
0024 #include <linux/dma-map-ops.h>
0025 #include <linux/efi.h>
0026 #include <linux/swiotlb.h>
0027 #include <linux/vmalloc.h>
0028 #include <linux/mm.h>
0029 #include <linux/kexec.h>
0030 #include <linux/crash_dump.h>
0031 #include <linux/hugetlb.h>
0032 #include <linux/acpi_iort.h>
0033 #include <linux/kmemleak.h>
0034 
0035 #include <asm/boot.h>
0036 #include <asm/fixmap.h>
0037 #include <asm/kasan.h>
0038 #include <asm/kernel-pgtable.h>
0039 #include <asm/kvm_host.h>
0040 #include <asm/memory.h>
0041 #include <asm/numa.h>
0042 #include <asm/sections.h>
0043 #include <asm/setup.h>
0044 #include <linux/sizes.h>
0045 #include <asm/tlb.h>
0046 #include <asm/alternative.h>
0047 #include <asm/xen/swiotlb-xen.h>
0048 
0049 /*
0050  * We need to be able to catch inadvertent references to memstart_addr
0051  * that occur (potentially in generic code) before arm64_memblock_init()
0052  * executes, which assigns it its actual value. So use a default value
0053  * that cannot be mistaken for a real physical address.
0054  */
0055 s64 memstart_addr __ro_after_init = -1;
0056 EXPORT_SYMBOL(memstart_addr);
0057 
0058 /*
0059  * If the corresponding config options are enabled, we create both ZONE_DMA
0060  * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
0061  * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4).
0062  * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory,
0063  * otherwise it is empty.
0064  *
0065  * Memory reservation for crash kernel either done early or deferred
0066  * depending on DMA memory zones configs (ZONE_DMA) --
0067  *
0068  * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized
0069  * here instead of max_zone_phys().  This lets early reservation of
0070  * crash kernel memory which has a dependency on arm64_dma_phys_limit.
0071  * Reserving memory early for crash kernel allows linear creation of block
0072  * mappings (greater than page-granularity) for all the memory bank rangs.
0073  * In this scheme a comparatively quicker boot is observed.
0074  *
0075  * If ZONE_DMA configs are defined, crash kernel memory reservation
0076  * is delayed until DMA zone memory range size initialization performed in
0077  * zone_sizes_init().  The defer is necessary to steer clear of DMA zone
0078  * memory range to avoid overlap allocation.  So crash kernel memory boundaries
0079  * are not known when mapping all bank memory ranges, which otherwise means
0080  * not possible to exclude crash kernel range from creating block mappings
0081  * so page-granularity mappings are created for the entire memory range.
0082  * Hence a slightly slower boot is observed.
0083  *
0084  * Note: Page-granularity mappings are necessary for crash kernel memory
0085  * range for shrinking its size via /sys/kernel/kexec_crash_size interface.
0086  */
0087 #if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32)
0088 phys_addr_t __ro_after_init arm64_dma_phys_limit;
0089 #else
0090 phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
0091 #endif
0092 
0093 /* Current arm64 boot protocol requires 2MB alignment */
0094 #define CRASH_ALIGN         SZ_2M
0095 
0096 #define CRASH_ADDR_LOW_MAX      arm64_dma_phys_limit
0097 #define CRASH_ADDR_HIGH_MAX     (PHYS_MASK + 1)
0098 
0099 static int __init reserve_crashkernel_low(unsigned long long low_size)
0100 {
0101     unsigned long long low_base;
0102 
0103     low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
0104     if (!low_base) {
0105         pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
0106         return -ENOMEM;
0107     }
0108 
0109     pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
0110         low_base, low_base + low_size, low_size >> 20);
0111 
0112     crashk_low_res.start = low_base;
0113     crashk_low_res.end   = low_base + low_size - 1;
0114     insert_resource(&iomem_resource, &crashk_low_res);
0115 
0116     return 0;
0117 }
0118 
0119 /*
0120  * reserve_crashkernel() - reserves memory for crash kernel
0121  *
0122  * This function reserves memory area given in "crashkernel=" kernel command
0123  * line parameter. The memory reserved is used by dump capture kernel when
0124  * primary kernel is crashing.
0125  */
0126 static void __init reserve_crashkernel(void)
0127 {
0128     unsigned long long crash_base, crash_size;
0129     unsigned long long crash_low_size = 0;
0130     unsigned long long crash_max = CRASH_ADDR_LOW_MAX;
0131     char *cmdline = boot_command_line;
0132     int ret;
0133 
0134     if (!IS_ENABLED(CONFIG_KEXEC_CORE))
0135         return;
0136 
0137     /* crashkernel=X[@offset] */
0138     ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
0139                 &crash_size, &crash_base);
0140     if (ret == -ENOENT) {
0141         ret = parse_crashkernel_high(cmdline, 0, &crash_size, &crash_base);
0142         if (ret || !crash_size)
0143             return;
0144 
0145         /*
0146          * crashkernel=Y,low can be specified or not, but invalid value
0147          * is not allowed.
0148          */
0149         ret = parse_crashkernel_low(cmdline, 0, &crash_low_size, &crash_base);
0150         if (ret && (ret != -ENOENT))
0151             return;
0152 
0153         crash_max = CRASH_ADDR_HIGH_MAX;
0154     } else if (ret || !crash_size) {
0155         /* The specified value is invalid */
0156         return;
0157     }
0158 
0159     crash_size = PAGE_ALIGN(crash_size);
0160 
0161     /* User specifies base address explicitly. */
0162     if (crash_base)
0163         crash_max = crash_base + crash_size;
0164 
0165     crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
0166                            crash_base, crash_max);
0167     if (!crash_base) {
0168         pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
0169             crash_size);
0170         return;
0171     }
0172 
0173     if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
0174          crash_low_size && reserve_crashkernel_low(crash_low_size)) {
0175         memblock_phys_free(crash_base, crash_size);
0176         return;
0177     }
0178 
0179     pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
0180         crash_base, crash_base + crash_size, crash_size >> 20);
0181 
0182     /*
0183      * The crashkernel memory will be removed from the kernel linear
0184      * map. Inform kmemleak so that it won't try to access it.
0185      */
0186     kmemleak_ignore_phys(crash_base);
0187     if (crashk_low_res.end)
0188         kmemleak_ignore_phys(crashk_low_res.start);
0189 
0190     crashk_res.start = crash_base;
0191     crashk_res.end = crash_base + crash_size - 1;
0192     insert_resource(&iomem_resource, &crashk_res);
0193 }
0194 
0195 /*
0196  * Return the maximum physical address for a zone accessible by the given bits
0197  * limit. If DRAM starts above 32-bit, expand the zone to the maximum
0198  * available memory, otherwise cap it at 32-bit.
0199  */
0200 static phys_addr_t __init max_zone_phys(unsigned int zone_bits)
0201 {
0202     phys_addr_t zone_mask = DMA_BIT_MASK(zone_bits);
0203     phys_addr_t phys_start = memblock_start_of_DRAM();
0204 
0205     if (phys_start > U32_MAX)
0206         zone_mask = PHYS_ADDR_MAX;
0207     else if (phys_start > zone_mask)
0208         zone_mask = U32_MAX;
0209 
0210     return min(zone_mask, memblock_end_of_DRAM() - 1) + 1;
0211 }
0212 
0213 static void __init zone_sizes_init(void)
0214 {
0215     unsigned long max_zone_pfns[MAX_NR_ZONES]  = {0};
0216     unsigned int __maybe_unused acpi_zone_dma_bits;
0217     unsigned int __maybe_unused dt_zone_dma_bits;
0218     phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32);
0219 
0220 #ifdef CONFIG_ZONE_DMA
0221     acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address());
0222     dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL));
0223     zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits);
0224     arm64_dma_phys_limit = max_zone_phys(zone_dma_bits);
0225     max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
0226 #endif
0227 #ifdef CONFIG_ZONE_DMA32
0228     max_zone_pfns[ZONE_DMA32] = PFN_DOWN(dma32_phys_limit);
0229     if (!arm64_dma_phys_limit)
0230         arm64_dma_phys_limit = dma32_phys_limit;
0231 #endif
0232     max_zone_pfns[ZONE_NORMAL] = max_pfn;
0233 
0234     free_area_init(max_zone_pfns);
0235 }
0236 
0237 int pfn_is_map_memory(unsigned long pfn)
0238 {
0239     phys_addr_t addr = PFN_PHYS(pfn);
0240 
0241     /* avoid false positives for bogus PFNs, see comment in pfn_valid() */
0242     if (PHYS_PFN(addr) != pfn)
0243         return 0;
0244 
0245     return memblock_is_map_memory(addr);
0246 }
0247 EXPORT_SYMBOL(pfn_is_map_memory);
0248 
0249 static phys_addr_t memory_limit __ro_after_init = PHYS_ADDR_MAX;
0250 
0251 /*
0252  * Limit the memory size that was specified via FDT.
0253  */
0254 static int __init early_mem(char *p)
0255 {
0256     if (!p)
0257         return 1;
0258 
0259     memory_limit = memparse(p, &p) & PAGE_MASK;
0260     pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
0261 
0262     return 0;
0263 }
0264 early_param("mem", early_mem);
0265 
0266 void __init arm64_memblock_init(void)
0267 {
0268     s64 linear_region_size = PAGE_END - _PAGE_OFFSET(vabits_actual);
0269 
0270     /*
0271      * Corner case: 52-bit VA capable systems running KVM in nVHE mode may
0272      * be limited in their ability to support a linear map that exceeds 51
0273      * bits of VA space, depending on the placement of the ID map. Given
0274      * that the placement of the ID map may be randomized, let's simply
0275      * limit the kernel's linear map to 51 bits as well if we detect this
0276      * configuration.
0277      */
0278     if (IS_ENABLED(CONFIG_KVM) && vabits_actual == 52 &&
0279         is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
0280         pr_info("Capping linear region to 51 bits for KVM in nVHE mode on LVA capable hardware.\n");
0281         linear_region_size = min_t(u64, linear_region_size, BIT(51));
0282     }
0283 
0284     /* Remove memory above our supported physical address size */
0285     memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX);
0286 
0287     /*
0288      * Select a suitable value for the base of physical memory.
0289      */
0290     memstart_addr = round_down(memblock_start_of_DRAM(),
0291                    ARM64_MEMSTART_ALIGN);
0292 
0293     if ((memblock_end_of_DRAM() - memstart_addr) > linear_region_size)
0294         pr_warn("Memory doesn't fit in the linear mapping, VA_BITS too small\n");
0295 
0296     /*
0297      * Remove the memory that we will not be able to cover with the
0298      * linear mapping. Take care not to clip the kernel which may be
0299      * high in memory.
0300      */
0301     memblock_remove(max_t(u64, memstart_addr + linear_region_size,
0302             __pa_symbol(_end)), ULLONG_MAX);
0303     if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
0304         /* ensure that memstart_addr remains sufficiently aligned */
0305         memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
0306                      ARM64_MEMSTART_ALIGN);
0307         memblock_remove(0, memstart_addr);
0308     }
0309 
0310     /*
0311      * If we are running with a 52-bit kernel VA config on a system that
0312      * does not support it, we have to place the available physical
0313      * memory in the 48-bit addressable part of the linear region, i.e.,
0314      * we have to move it upward. Since memstart_addr represents the
0315      * physical address of PAGE_OFFSET, we have to *subtract* from it.
0316      */
0317     if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52))
0318         memstart_addr -= _PAGE_OFFSET(48) - _PAGE_OFFSET(52);
0319 
0320     /*
0321      * Apply the memory limit if it was set. Since the kernel may be loaded
0322      * high up in memory, add back the kernel region that must be accessible
0323      * via the linear mapping.
0324      */
0325     if (memory_limit != PHYS_ADDR_MAX) {
0326         memblock_mem_limit_remove_map(memory_limit);
0327         memblock_add(__pa_symbol(_text), (u64)(_end - _text));
0328     }
0329 
0330     if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
0331         /*
0332          * Add back the memory we just removed if it results in the
0333          * initrd to become inaccessible via the linear mapping.
0334          * Otherwise, this is a no-op
0335          */
0336         u64 base = phys_initrd_start & PAGE_MASK;
0337         u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base;
0338 
0339         /*
0340          * We can only add back the initrd memory if we don't end up
0341          * with more memory than we can address via the linear mapping.
0342          * It is up to the bootloader to position the kernel and the
0343          * initrd reasonably close to each other (i.e., within 32 GB of
0344          * each other) so that all granule/#levels combinations can
0345          * always access both.
0346          */
0347         if (WARN(base < memblock_start_of_DRAM() ||
0348              base + size > memblock_start_of_DRAM() +
0349                        linear_region_size,
0350             "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
0351             phys_initrd_size = 0;
0352         } else {
0353             memblock_add(base, size);
0354             memblock_clear_nomap(base, size);
0355             memblock_reserve(base, size);
0356         }
0357     }
0358 
0359     if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
0360         extern u16 memstart_offset_seed;
0361         u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
0362         int parange = cpuid_feature_extract_unsigned_field(
0363                     mmfr0, ID_AA64MMFR0_PARANGE_SHIFT);
0364         s64 range = linear_region_size -
0365                 BIT(id_aa64mmfr0_parange_to_phys_shift(parange));
0366 
0367         /*
0368          * If the size of the linear region exceeds, by a sufficient
0369          * margin, the size of the region that the physical memory can
0370          * span, randomize the linear region as well.
0371          */
0372         if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) {
0373             range /= ARM64_MEMSTART_ALIGN;
0374             memstart_addr -= ARM64_MEMSTART_ALIGN *
0375                      ((range * memstart_offset_seed) >> 16);
0376         }
0377     }
0378 
0379     /*
0380      * Register the kernel text, kernel data, initrd, and initial
0381      * pagetables with memblock.
0382      */
0383     memblock_reserve(__pa_symbol(_stext), _end - _stext);
0384     if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
0385         /* the generic initrd code expects virtual addresses */
0386         initrd_start = __phys_to_virt(phys_initrd_start);
0387         initrd_end = initrd_start + phys_initrd_size;
0388     }
0389 
0390     early_init_fdt_scan_reserved_mem();
0391 
0392     if (!defer_reserve_crashkernel())
0393         reserve_crashkernel();
0394 
0395     high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
0396 }
0397 
0398 void __init bootmem_init(void)
0399 {
0400     unsigned long min, max;
0401 
0402     min = PFN_UP(memblock_start_of_DRAM());
0403     max = PFN_DOWN(memblock_end_of_DRAM());
0404 
0405     early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
0406 
0407     max_pfn = max_low_pfn = max;
0408     min_low_pfn = min;
0409 
0410     arch_numa_init();
0411 
0412     /*
0413      * must be done after arch_numa_init() which calls numa_init() to
0414      * initialize node_online_map that gets used in hugetlb_cma_reserve()
0415      * while allocating required CMA size across online nodes.
0416      */
0417 #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
0418     arm64_hugetlb_cma_reserve();
0419 #endif
0420 
0421     dma_pernuma_cma_reserve();
0422 
0423     kvm_hyp_reserve();
0424 
0425     /*
0426      * sparse_init() tries to allocate memory from memblock, so must be
0427      * done after the fixed reservations
0428      */
0429     sparse_init();
0430     zone_sizes_init();
0431 
0432     /*
0433      * Reserve the CMA area after arm64_dma_phys_limit was initialised.
0434      */
0435     dma_contiguous_reserve(arm64_dma_phys_limit);
0436 
0437     /*
0438      * request_standard_resources() depends on crashkernel's memory being
0439      * reserved, so do it here.
0440      */
0441     if (defer_reserve_crashkernel())
0442         reserve_crashkernel();
0443 
0444     memblock_dump_all();
0445 }
0446 
0447 /*
0448  * mem_init() marks the free areas in the mem_map and tells us how much memory
0449  * is free.  This is done after various parts of the system have claimed their
0450  * memory after the kernel image.
0451  */
0452 void __init mem_init(void)
0453 {
0454     swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE);
0455 
0456     /* this will put all unused low memory onto the freelists */
0457     memblock_free_all();
0458 
0459     /*
0460      * Check boundaries twice: Some fundamental inconsistencies can be
0461      * detected at build time already.
0462      */
0463 #ifdef CONFIG_COMPAT
0464     BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
0465 #endif
0466 
0467     /*
0468      * Selected page table levels should match when derived from
0469      * scratch using the virtual address range and page size.
0470      */
0471     BUILD_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
0472              CONFIG_PGTABLE_LEVELS);
0473 
0474     if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
0475         extern int sysctl_overcommit_memory;
0476         /*
0477          * On a machine this small we won't get anywhere without
0478          * overcommit, so turn it on by default.
0479          */
0480         sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
0481     }
0482 }
0483 
0484 void free_initmem(void)
0485 {
0486     free_reserved_area(lm_alias(__init_begin),
0487                lm_alias(__init_end),
0488                POISON_FREE_INITMEM, "unused kernel");
0489     /*
0490      * Unmap the __init region but leave the VM area in place. This
0491      * prevents the region from being reused for kernel modules, which
0492      * is not supported by kallsyms.
0493      */
0494     vunmap_range((u64)__init_begin, (u64)__init_end);
0495 }
0496 
0497 void dump_mem_limit(void)
0498 {
0499     if (memory_limit != PHYS_ADDR_MAX) {
0500         pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
0501     } else {
0502         pr_emerg("Memory Limit: none\n");
0503     }
0504 }