init.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/errno.h>
  11. #include <linux/swap.h>
  12. #include <linux/init.h>
  13. #include <linux/cache.h>
  14. #include <linux/mman.h>
  15. #include <linux/nodemask.h>
  16. #include <linux/initrd.h>
  17. #include <linux/gfp.h>
  18. #include <linux/memblock.h>
  19. #include <linux/sort.h>
  20. #include <linux/of.h>
  21. #include <linux/of_fdt.h>
  22. #include <linux/dma-direct.h>
  23. #include <linux/dma-map-ops.h>
  24. #include <linux/efi.h>
  25. #include <linux/swiotlb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mm.h>
  28. #include <linux/kexec.h>
  29. #include <linux/crash_dump.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/acpi_iort.h>
  32. #include <linux/kmemleak.h>
  33. #include <asm/boot.h>
  34. #include <asm/fixmap.h>
  35. #include <asm/kasan.h>
  36. #include <asm/kernel-pgtable.h>
  37. #include <asm/kvm_host.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. #include <asm/xen/swiotlb-xen.h>
  46. /*
  47. * We need to be able to catch inadvertent references to memstart_addr
  48. * that occur (potentially in generic code) before arm64_memblock_init()
  49. * executes, which assigns it its actual value. So use a default value
  50. * that cannot be mistaken for a real physical address.
  51. */
  52. s64 memstart_addr __ro_after_init = -1;
  53. EXPORT_SYMBOL(memstart_addr);
  54. /*
  55. * If the corresponding config options are enabled, we create both ZONE_DMA
  56. * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
  57. * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4).
  58. * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory,
  59. * otherwise it is empty.
  60. *
  61. * Memory reservation for crash kernel either done early or deferred
  62. * depending on DMA memory zones configs (ZONE_DMA) --
  63. *
  64. * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized
  65. * here instead of max_zone_phys(). This lets early reservation of
  66. * crash kernel memory which has a dependency on arm64_dma_phys_limit.
  67. * Reserving memory early for crash kernel allows linear creation of block
  68. * mappings (greater than page-granularity) for all the memory bank rangs.
  69. * In this scheme a comparatively quicker boot is observed.
  70. *
  71. * If ZONE_DMA configs are defined, crash kernel memory reservation
  72. * is delayed until DMA zone memory range size initialization performed in
  73. * zone_sizes_init(). The defer is necessary to steer clear of DMA zone
  74. * memory range to avoid overlap allocation. So crash kernel memory boundaries
  75. * are not known when mapping all bank memory ranges, which otherwise means
  76. * not possible to exclude crash kernel range from creating block mappings
  77. * so page-granularity mappings are created for the entire memory range.
  78. * Hence a slightly slower boot is observed.
  79. *
  80. * Note: Page-granularity mappings are necessary for crash kernel memory
  81. * range for shrinking its size via /sys/kernel/kexec_crash_size interface.
  82. */
  83. #if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32)
  84. phys_addr_t __ro_after_init arm64_dma_phys_limit;
  85. #else
  86. phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
  87. #endif
  88. /*
  89. * Provide a run-time mean of disabling ZONE_DMA32 if it is enabled via
  90. * CONFIG_ZONE_DMA32.
  91. */
  92. static bool disable_dma32 __ro_after_init;
  93. /* Current arm64 boot protocol requires 2MB alignment */
  94. #define CRASH_ALIGN SZ_2M
  95. #define CRASH_ADDR_LOW_MAX arm64_dma_phys_limit
  96. #define CRASH_ADDR_HIGH_MAX (PHYS_MASK + 1)
  97. static int __init reserve_crashkernel_low(unsigned long long low_size)
  98. {
  99. unsigned long long low_base;
  100. low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
  101. if (!low_base) {
  102. pr_err("cannot allocate crashkernel low memory (size:0x%llx).\n", low_size);
  103. return -ENOMEM;
  104. }
  105. pr_info("crashkernel low memory reserved: 0x%08llx - 0x%08llx (%lld MB)\n",
  106. low_base, low_base + low_size, low_size >> 20);
  107. crashk_low_res.start = low_base;
  108. crashk_low_res.end = low_base + low_size - 1;
  109. insert_resource(&iomem_resource, &crashk_low_res);
  110. return 0;
  111. }
  112. /*
  113. * reserve_crashkernel() - reserves memory for crash kernel
  114. *
  115. * This function reserves memory area given in "crashkernel=" kernel command
  116. * line parameter. The memory reserved is used by dump capture kernel when
  117. * primary kernel is crashing.
  118. */
  119. static void __init reserve_crashkernel(void)
  120. {
  121. unsigned long long crash_base, crash_size;
  122. unsigned long long crash_low_size = 0;
  123. unsigned long long crash_max = CRASH_ADDR_LOW_MAX;
  124. char *cmdline = boot_command_line;
  125. int ret;
  126. if (!IS_ENABLED(CONFIG_KEXEC_CORE))
  127. return;
  128. /* crashkernel=X[@offset] */
  129. ret = parse_crashkernel(cmdline, memblock_phys_mem_size(),
  130. &crash_size, &crash_base);
  131. if (ret == -ENOENT) {
  132. ret = parse_crashkernel_high(cmdline, 0, &crash_size, &crash_base);
  133. if (ret || !crash_size)
  134. return;
  135. /*
  136. * crashkernel=Y,low can be specified or not, but invalid value
  137. * is not allowed.
  138. */
  139. ret = parse_crashkernel_low(cmdline, 0, &crash_low_size, &crash_base);
  140. if (ret && (ret != -ENOENT))
  141. return;
  142. crash_max = CRASH_ADDR_HIGH_MAX;
  143. } else if (ret || !crash_size) {
  144. /* The specified value is invalid */
  145. return;
  146. }
  147. crash_size = PAGE_ALIGN(crash_size);
  148. /* User specifies base address explicitly. */
  149. if (crash_base)
  150. crash_max = crash_base + crash_size;
  151. crash_base = memblock_phys_alloc_range(crash_size, CRASH_ALIGN,
  152. crash_base, crash_max);
  153. if (!crash_base) {
  154. pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
  155. crash_size);
  156. return;
  157. }
  158. if ((crash_base >= CRASH_ADDR_LOW_MAX) &&
  159. crash_low_size && reserve_crashkernel_low(crash_low_size)) {
  160. memblock_phys_free(crash_base, crash_size);
  161. return;
  162. }
  163. pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
  164. crash_base, crash_base + crash_size, crash_size >> 20);
  165. /*
  166. * The crashkernel memory will be removed from the kernel linear
  167. * map. Inform kmemleak so that it won't try to access it.
  168. */
  169. kmemleak_ignore_phys(crash_base);
  170. if (crashk_low_res.end)
  171. kmemleak_ignore_phys(crashk_low_res.start);
  172. crashk_res.start = crash_base;
  173. crashk_res.end = crash_base + crash_size - 1;
  174. insert_resource(&iomem_resource, &crashk_res);
  175. }
  176. /*
  177. * Return the maximum physical address for a zone accessible by the given bits
  178. * limit. If DRAM starts above 32-bit, expand the zone to the maximum
  179. * available memory, otherwise cap it at 32-bit.
  180. */
  181. static phys_addr_t __init max_zone_phys(unsigned int zone_bits)
  182. {
  183. phys_addr_t zone_mask = DMA_BIT_MASK(zone_bits);
  184. phys_addr_t phys_start = memblock_start_of_DRAM();
  185. if (phys_start > U32_MAX)
  186. zone_mask = PHYS_ADDR_MAX;
  187. else if (phys_start > zone_mask)
  188. zone_mask = U32_MAX;
  189. return min(zone_mask, memblock_end_of_DRAM() - 1) + 1;
  190. }
  191. static void __init zone_sizes_init(void)
  192. {
  193. unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
  194. unsigned int __maybe_unused acpi_zone_dma_bits;
  195. unsigned int __maybe_unused dt_zone_dma_bits;
  196. phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32);
  197. #ifdef CONFIG_ZONE_DMA
  198. acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address());
  199. dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL));
  200. zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits);
  201. arm64_dma_phys_limit = max_zone_phys(zone_dma_bits);
  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] = disable_dma32 ? 0 : PFN_DOWN(dma32_phys_limit);
  206. if (!arm64_dma_phys_limit)
  207. arm64_dma_phys_limit = dma32_phys_limit;
  208. #endif
  209. max_zone_pfns[ZONE_NORMAL] = max_pfn;
  210. free_area_init(max_zone_pfns);
  211. }
  212. static int __init early_disable_dma32(char *buf)
  213. {
  214. if (!buf)
  215. return -EINVAL;
  216. if (!strcmp(buf, "on"))
  217. disable_dma32 = true;
  218. return 0;
  219. }
  220. early_param("disable_dma32", early_disable_dma32);
  221. int pfn_is_map_memory(unsigned long pfn)
  222. {
  223. phys_addr_t addr = PFN_PHYS(pfn);
  224. /* avoid false positives for bogus PFNs, see comment in pfn_valid() */
  225. if (PHYS_PFN(addr) != pfn)
  226. return 0;
  227. return memblock_is_map_memory(addr);
  228. }
  229. EXPORT_SYMBOL(pfn_is_map_memory);
  230. static phys_addr_t memory_limit __ro_after_init = PHYS_ADDR_MAX;
  231. /*
  232. * Limit the memory size that was specified via FDT.
  233. */
  234. static int __init early_mem(char *p)
  235. {
  236. if (!p)
  237. return 1;
  238. memory_limit = memparse(p, &p) & PAGE_MASK;
  239. pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
  240. return 0;
  241. }
  242. early_param("mem", early_mem);
  243. void __init arm64_memblock_init(void)
  244. {
  245. s64 linear_region_size = PAGE_END - _PAGE_OFFSET(vabits_actual);
  246. /*
  247. * Corner case: 52-bit VA capable systems running KVM in nVHE mode may
  248. * be limited in their ability to support a linear map that exceeds 51
  249. * bits of VA space, depending on the placement of the ID map. Given
  250. * that the placement of the ID map may be randomized, let's simply
  251. * limit the kernel's linear map to 51 bits as well if we detect this
  252. * configuration.
  253. */
  254. if (IS_ENABLED(CONFIG_KVM) && vabits_actual == 52 &&
  255. is_hyp_mode_available() && !is_kernel_in_hyp_mode()) {
  256. pr_info("Capping linear region to 51 bits for KVM in nVHE mode on LVA capable hardware.\n");
  257. linear_region_size = min_t(u64, linear_region_size, BIT(51));
  258. }
  259. /* Remove memory above our supported physical address size */
  260. memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX);
  261. /*
  262. * Select a suitable value for the base of physical memory.
  263. */
  264. memstart_addr = round_down(memblock_start_of_DRAM(),
  265. ARM64_MEMSTART_ALIGN);
  266. if ((memblock_end_of_DRAM() - memstart_addr) > linear_region_size)
  267. pr_warn("Memory doesn't fit in the linear mapping, VA_BITS too small\n");
  268. /*
  269. * Remove the memory that we will not be able to cover with the
  270. * linear mapping. Take care not to clip the kernel which may be
  271. * high in memory.
  272. */
  273. memblock_remove(max_t(u64, memstart_addr + linear_region_size,
  274. __pa_symbol(_end)), ULLONG_MAX);
  275. if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
  276. /* ensure that memstart_addr remains sufficiently aligned */
  277. memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
  278. ARM64_MEMSTART_ALIGN);
  279. memblock_remove(0, memstart_addr);
  280. }
  281. /*
  282. * If we are running with a 52-bit kernel VA config on a system that
  283. * does not support it, we have to place the available physical
  284. * memory in the 48-bit addressable part of the linear region, i.e.,
  285. * we have to move it upward. Since memstart_addr represents the
  286. * physical address of PAGE_OFFSET, we have to *subtract* from it.
  287. */
  288. if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52))
  289. memstart_addr -= _PAGE_OFFSET(48) - _PAGE_OFFSET(52);
  290. /*
  291. * Apply the memory limit if it was set. Since the kernel may be loaded
  292. * high up in memory, add back the kernel region that must be accessible
  293. * via the linear mapping.
  294. */
  295. if (memory_limit != PHYS_ADDR_MAX) {
  296. memblock_mem_limit_remove_map(memory_limit);
  297. memblock_add(__pa_symbol(_text), (u64)(_end - _text));
  298. }
  299. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
  300. /*
  301. * Add back the memory we just removed if it results in the
  302. * initrd to become inaccessible via the linear mapping.
  303. * Otherwise, this is a no-op
  304. */
  305. u64 base = phys_initrd_start & PAGE_MASK;
  306. u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base;
  307. /*
  308. * We can only add back the initrd memory if we don't end up
  309. * with more memory than we can address via the linear mapping.
  310. * It is up to the bootloader to position the kernel and the
  311. * initrd reasonably close to each other (i.e., within 32 GB of
  312. * each other) so that all granule/#levels combinations can
  313. * always access both.
  314. */
  315. if (WARN(base < memblock_start_of_DRAM() ||
  316. base + size > memblock_start_of_DRAM() +
  317. linear_region_size,
  318. "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
  319. phys_initrd_size = 0;
  320. } else {
  321. memblock_add(base, size);
  322. memblock_clear_nomap(base, size);
  323. memblock_reserve(base, size);
  324. }
  325. }
  326. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  327. extern u16 memstart_offset_seed;
  328. u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  329. int parange = cpuid_feature_extract_unsigned_field(
  330. mmfr0, ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  331. s64 range = linear_region_size -
  332. BIT(id_aa64mmfr0_parange_to_phys_shift(parange));
  333. /*
  334. * If the size of the linear region exceeds, by a sufficient
  335. * margin, the size of the region that the physical memory can
  336. * span, randomize the linear region as well.
  337. */
  338. if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) {
  339. range /= ARM64_MEMSTART_ALIGN;
  340. memstart_addr -= ARM64_MEMSTART_ALIGN *
  341. ((range * memstart_offset_seed) >> 16);
  342. }
  343. }
  344. /*
  345. * Register the kernel text, kernel data, initrd, and initial
  346. * pagetables with memblock.
  347. */
  348. memblock_reserve(__pa_symbol(_stext), _end - _stext);
  349. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
  350. /* the generic initrd code expects virtual addresses */
  351. initrd_start = __phys_to_virt(phys_initrd_start);
  352. initrd_end = initrd_start + phys_initrd_size;
  353. }
  354. early_init_fdt_scan_reserved_mem();
  355. if (!defer_reserve_crashkernel())
  356. reserve_crashkernel();
  357. high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
  358. }
  359. void __init bootmem_init(void)
  360. {
  361. unsigned long min, max;
  362. min = PFN_UP(memblock_start_of_DRAM());
  363. max = PFN_DOWN(memblock_end_of_DRAM());
  364. early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
  365. max_pfn = max_low_pfn = max;
  366. min_low_pfn = min;
  367. arch_numa_init();
  368. /*
  369. * must be done after arch_numa_init() which calls numa_init() to
  370. * initialize node_online_map that gets used in hugetlb_cma_reserve()
  371. * while allocating required CMA size across online nodes.
  372. */
  373. #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
  374. arm64_hugetlb_cma_reserve();
  375. #endif
  376. dma_pernuma_cma_reserve();
  377. kvm_hyp_reserve();
  378. /*
  379. * sparse_init() tries to allocate memory from memblock, so must be
  380. * done after the fixed reservations
  381. */
  382. sparse_init();
  383. zone_sizes_init();
  384. /*
  385. * Reserve the CMA area after arm64_dma_phys_limit was initialised.
  386. */
  387. dma_contiguous_reserve(arm64_dma_phys_limit);
  388. /*
  389. * request_standard_resources() depends on crashkernel's memory being
  390. * reserved, so do it here.
  391. */
  392. if (defer_reserve_crashkernel())
  393. reserve_crashkernel();
  394. memblock_dump_all();
  395. }
  396. /*
  397. * mem_init() marks the free areas in the mem_map and tells us how much memory
  398. * is free. This is done after various parts of the system have claimed their
  399. * memory after the kernel image.
  400. */
  401. void __init mem_init(void)
  402. {
  403. swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE);
  404. /* this will put all unused low memory onto the freelists */
  405. memblock_free_all();
  406. /*
  407. * Check boundaries twice: Some fundamental inconsistencies can be
  408. * detected at build time already.
  409. */
  410. #ifdef CONFIG_COMPAT
  411. BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
  412. #endif
  413. /*
  414. * Selected page table levels should match when derived from
  415. * scratch using the virtual address range and page size.
  416. */
  417. BUILD_BUG_ON(ARM64_HW_PGTABLE_LEVELS(CONFIG_ARM64_VA_BITS) !=
  418. CONFIG_PGTABLE_LEVELS);
  419. if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
  420. extern int sysctl_overcommit_memory;
  421. /*
  422. * On a machine this small we won't get anywhere without
  423. * overcommit, so turn it on by default.
  424. */
  425. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  426. }
  427. }
  428. void free_initmem(void)
  429. {
  430. free_reserved_area(lm_alias(__init_begin),
  431. lm_alias(__init_end),
  432. POISON_FREE_INITMEM, "unused kernel");
  433. /*
  434. * Unmap the __init region but leave the VM area in place. This
  435. * prevents the region from being reused for kernel modules, which
  436. * is not supported by kallsyms.
  437. */
  438. vunmap_range((u64)__init_begin, (u64)__init_end);
  439. }
  440. void dump_mem_limit(void)
  441. {
  442. if (memory_limit != PHYS_ADDR_MAX) {
  443. pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
  444. } else {
  445. pr_emerg("Memory Limit: none\n");
  446. }
  447. }