0001
0002
0003
0004
0005
0006
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
0051
0052
0053
0054
0055 s64 memstart_addr __ro_after_init = -1;
0056 EXPORT_SYMBOL(memstart_addr);
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
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
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
0121
0122
0123
0124
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
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
0147
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
0156 return;
0157 }
0158
0159 crash_size = PAGE_ALIGN(crash_size);
0160
0161
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
0184
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
0197
0198
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
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
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
0272
0273
0274
0275
0276
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
0285 memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX);
0286
0287
0288
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
0298
0299
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
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
0312
0313
0314
0315
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
0322
0323
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
0333
0334
0335
0336 u64 base = phys_initrd_start & PAGE_MASK;
0337 u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base;
0338
0339
0340
0341
0342
0343
0344
0345
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
0369
0370
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
0381
0382
0383 memblock_reserve(__pa_symbol(_stext), _end - _stext);
0384 if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
0385
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
0414
0415
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
0427
0428
0429 sparse_init();
0430 zone_sizes_init();
0431
0432
0433
0434
0435 dma_contiguous_reserve(arm64_dma_phys_limit);
0436
0437
0438
0439
0440
0441 if (defer_reserve_crashkernel())
0442 reserve_crashkernel();
0443
0444 memblock_dump_all();
0445 }
0446
0447
0448
0449
0450
0451
0452 void __init mem_init(void)
0453 {
0454 swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE);
0455
0456
0457 memblock_free_all();
0458
0459
0460
0461
0462
0463 #ifdef CONFIG_COMPAT
0464 BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
0465 #endif
0466
0467
0468
0469
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
0478
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
0491
0492
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 }