init.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. #include <linux/kernel.h>
  20. #include <linux/export.h>
  21. #include <linux/errno.h>
  22. #include <linux/swap.h>
  23. #include <linux/init.h>
  24. #include <linux/bootmem.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_fdt.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/dma-contiguous.h>
  35. #include <linux/efi.h>
  36. #include <linux/swiotlb.h>
  37. #include <linux/vmalloc.h>
  38. #include <asm/boot.h>
  39. #include <asm/fixmap.h>
  40. #include <asm/kasan.h>
  41. #include <asm/kernel-pgtable.h>
  42. #include <asm/memory.h>
  43. #include <asm/numa.h>
  44. #include <asm/sections.h>
  45. #include <asm/setup.h>
  46. #include <asm/sizes.h>
  47. #include <asm/tlb.h>
  48. #include <asm/alternative.h>
  49. /*
  50. * We need to be able to catch inadvertent references to memstart_addr
  51. * that occur (potentially in generic code) before arm64_memblock_init()
  52. * executes, which assigns it its actual value. So use a default value
  53. * that cannot be mistaken for a real physical address.
  54. */
  55. s64 memstart_addr __ro_after_init = -1;
  56. phys_addr_t arm64_dma_phys_limit __ro_after_init;
  57. #ifdef CONFIG_BLK_DEV_INITRD
  58. static int __init early_initrd(char *p)
  59. {
  60. unsigned long start, size;
  61. char *endp;
  62. start = memparse(p, &endp);
  63. if (*endp == ',') {
  64. size = memparse(endp + 1, NULL);
  65. initrd_start = start;
  66. initrd_end = start + size;
  67. }
  68. return 0;
  69. }
  70. early_param("initrd", early_initrd);
  71. #endif
  72. /*
  73. * Return the maximum physical address for ZONE_DMA (DMA_BIT_MASK(32)). It
  74. * currently assumes that for memory starting above 4G, 32-bit devices will
  75. * use a DMA offset.
  76. */
  77. static phys_addr_t __init max_zone_dma_phys(void)
  78. {
  79. phys_addr_t offset = memblock_start_of_DRAM() & GENMASK_ULL(63, 32);
  80. return min(offset + (1ULL << 32), memblock_end_of_DRAM());
  81. }
  82. #ifdef CONFIG_NUMA
  83. static void __init zone_sizes_init(unsigned long min, unsigned long max)
  84. {
  85. unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
  86. if (IS_ENABLED(CONFIG_ZONE_DMA))
  87. max_zone_pfns[ZONE_DMA] = PFN_DOWN(max_zone_dma_phys());
  88. max_zone_pfns[ZONE_NORMAL] = max;
  89. free_area_init_nodes(max_zone_pfns);
  90. }
  91. #else
  92. static void __init zone_sizes_init(unsigned long min, unsigned long max)
  93. {
  94. struct memblock_region *reg;
  95. unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
  96. unsigned long max_dma = min;
  97. memset(zone_size, 0, sizeof(zone_size));
  98. /* 4GB maximum for 32-bit only capable devices */
  99. #ifdef CONFIG_ZONE_DMA
  100. max_dma = PFN_DOWN(arm64_dma_phys_limit);
  101. zone_size[ZONE_DMA] = max_dma - min;
  102. #endif
  103. zone_size[ZONE_NORMAL] = max - max_dma;
  104. memcpy(zhole_size, zone_size, sizeof(zhole_size));
  105. for_each_memblock(memory, reg) {
  106. unsigned long start = memblock_region_memory_base_pfn(reg);
  107. unsigned long end = memblock_region_memory_end_pfn(reg);
  108. if (start >= max)
  109. continue;
  110. #ifdef CONFIG_ZONE_DMA
  111. if (start < max_dma) {
  112. unsigned long dma_end = min(end, max_dma);
  113. zhole_size[ZONE_DMA] -= dma_end - start;
  114. }
  115. #endif
  116. if (end > max_dma) {
  117. unsigned long normal_end = min(end, max);
  118. unsigned long normal_start = max(start, max_dma);
  119. zhole_size[ZONE_NORMAL] -= normal_end - normal_start;
  120. }
  121. }
  122. free_area_init_node(0, zone_size, min, zhole_size);
  123. }
  124. #endif /* CONFIG_NUMA */
  125. #ifdef CONFIG_HAVE_ARCH_PFN_VALID
  126. int pfn_valid(unsigned long pfn)
  127. {
  128. return memblock_is_map_memory(pfn << PAGE_SHIFT);
  129. }
  130. EXPORT_SYMBOL(pfn_valid);
  131. #endif
  132. #ifndef CONFIG_SPARSEMEM
  133. static void __init arm64_memory_present(void)
  134. {
  135. }
  136. #else
  137. static void __init arm64_memory_present(void)
  138. {
  139. struct memblock_region *reg;
  140. for_each_memblock(memory, reg) {
  141. int nid = memblock_get_region_node(reg);
  142. memory_present(nid, memblock_region_memory_base_pfn(reg),
  143. memblock_region_memory_end_pfn(reg));
  144. }
  145. }
  146. #endif
  147. static phys_addr_t memory_limit = (phys_addr_t)ULLONG_MAX;
  148. /*
  149. * Limit the memory size that was specified via FDT.
  150. */
  151. static int __init early_mem(char *p)
  152. {
  153. if (!p)
  154. return 1;
  155. memory_limit = memparse(p, &p) & PAGE_MASK;
  156. pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
  157. return 0;
  158. }
  159. early_param("mem", early_mem);
  160. void __init arm64_memblock_init(void)
  161. {
  162. const s64 linear_region_size = -(s64)PAGE_OFFSET;
  163. /*
  164. * Ensure that the linear region takes up exactly half of the kernel
  165. * virtual address space. This way, we can distinguish a linear address
  166. * from a kernel/module/vmalloc address by testing a single bit.
  167. */
  168. BUILD_BUG_ON(linear_region_size != BIT(VA_BITS - 1));
  169. /*
  170. * Select a suitable value for the base of physical memory.
  171. */
  172. memstart_addr = round_down(memblock_start_of_DRAM(),
  173. ARM64_MEMSTART_ALIGN);
  174. /*
  175. * Remove the memory that we will not be able to cover with the
  176. * linear mapping. Take care not to clip the kernel which may be
  177. * high in memory.
  178. */
  179. memblock_remove(max_t(u64, memstart_addr + linear_region_size, __pa(_end)),
  180. ULLONG_MAX);
  181. if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
  182. /* ensure that memstart_addr remains sufficiently aligned */
  183. memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
  184. ARM64_MEMSTART_ALIGN);
  185. memblock_remove(0, memstart_addr);
  186. }
  187. /*
  188. * Apply the memory limit if it was set. Since the kernel may be loaded
  189. * high up in memory, add back the kernel region that must be accessible
  190. * via the linear mapping.
  191. */
  192. if (memory_limit != (phys_addr_t)ULLONG_MAX) {
  193. memblock_mem_limit_remove_map(memory_limit);
  194. memblock_add(__pa(_text), (u64)(_end - _text));
  195. }
  196. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && initrd_start) {
  197. /*
  198. * Add back the memory we just removed if it results in the
  199. * initrd to become inaccessible via the linear mapping.
  200. * Otherwise, this is a no-op
  201. */
  202. u64 base = initrd_start & PAGE_MASK;
  203. u64 size = PAGE_ALIGN(initrd_end) - base;
  204. /*
  205. * We can only add back the initrd memory if we don't end up
  206. * with more memory than we can address via the linear mapping.
  207. * It is up to the bootloader to position the kernel and the
  208. * initrd reasonably close to each other (i.e., within 32 GB of
  209. * each other) so that all granule/#levels combinations can
  210. * always access both.
  211. */
  212. if (WARN(base < memblock_start_of_DRAM() ||
  213. base + size > memblock_start_of_DRAM() +
  214. linear_region_size,
  215. "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
  216. initrd_start = 0;
  217. } else {
  218. memblock_remove(base, size); /* clear MEMBLOCK_ flags */
  219. memblock_add(base, size);
  220. memblock_reserve(base, size);
  221. }
  222. }
  223. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  224. extern u16 memstart_offset_seed;
  225. u64 range = linear_region_size -
  226. (memblock_end_of_DRAM() - memblock_start_of_DRAM());
  227. /*
  228. * If the size of the linear region exceeds, by a sufficient
  229. * margin, the size of the region that the available physical
  230. * memory spans, randomize the linear region as well.
  231. */
  232. if (memstart_offset_seed > 0 && range >= ARM64_MEMSTART_ALIGN) {
  233. range = range / ARM64_MEMSTART_ALIGN + 1;
  234. memstart_addr -= ARM64_MEMSTART_ALIGN *
  235. ((range * memstart_offset_seed) >> 16);
  236. }
  237. }
  238. /*
  239. * Register the kernel text, kernel data, initrd, and initial
  240. * pagetables with memblock.
  241. */
  242. memblock_reserve(__pa(_text), _end - _text);
  243. #ifdef CONFIG_BLK_DEV_INITRD
  244. if (initrd_start) {
  245. memblock_reserve(initrd_start, initrd_end - initrd_start);
  246. /* the generic initrd code expects virtual addresses */
  247. initrd_start = __phys_to_virt(initrd_start);
  248. initrd_end = __phys_to_virt(initrd_end);
  249. }
  250. #endif
  251. early_init_fdt_scan_reserved_mem();
  252. /* 4GB maximum for 32-bit only capable devices */
  253. if (IS_ENABLED(CONFIG_ZONE_DMA))
  254. arm64_dma_phys_limit = max_zone_dma_phys();
  255. else
  256. arm64_dma_phys_limit = PHYS_MASK + 1;
  257. high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
  258. dma_contiguous_reserve(arm64_dma_phys_limit);
  259. memblock_allow_resize();
  260. }
  261. void __init bootmem_init(void)
  262. {
  263. unsigned long min, max;
  264. min = PFN_UP(memblock_start_of_DRAM());
  265. max = PFN_DOWN(memblock_end_of_DRAM());
  266. early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
  267. max_pfn = max_low_pfn = max;
  268. arm64_numa_init();
  269. /*
  270. * Sparsemem tries to allocate bootmem in memory_present(), so must be
  271. * done after the fixed reservations.
  272. */
  273. arm64_memory_present();
  274. sparse_init();
  275. zone_sizes_init(min, max);
  276. memblock_dump_all();
  277. }
  278. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  279. static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  280. {
  281. struct page *start_pg, *end_pg;
  282. unsigned long pg, pgend;
  283. /*
  284. * Convert start_pfn/end_pfn to a struct page pointer.
  285. */
  286. start_pg = pfn_to_page(start_pfn - 1) + 1;
  287. end_pg = pfn_to_page(end_pfn - 1) + 1;
  288. /*
  289. * Convert to physical addresses, and round start upwards and end
  290. * downwards.
  291. */
  292. pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
  293. pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
  294. /*
  295. * If there are free pages between these, free the section of the
  296. * memmap array.
  297. */
  298. if (pg < pgend)
  299. free_bootmem(pg, pgend - pg);
  300. }
  301. /*
  302. * The mem_map array can get very big. Free the unused area of the memory map.
  303. */
  304. static void __init free_unused_memmap(void)
  305. {
  306. unsigned long start, prev_end = 0;
  307. struct memblock_region *reg;
  308. for_each_memblock(memory, reg) {
  309. start = __phys_to_pfn(reg->base);
  310. #ifdef CONFIG_SPARSEMEM
  311. /*
  312. * Take care not to free memmap entries that don't exist due
  313. * to SPARSEMEM sections which aren't present.
  314. */
  315. start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
  316. #endif
  317. /*
  318. * If we had a previous bank, and there is a space between the
  319. * current bank and the previous, free it.
  320. */
  321. if (prev_end && prev_end < start)
  322. free_memmap(prev_end, start);
  323. /*
  324. * Align up here since the VM subsystem insists that the
  325. * memmap entries are valid from the bank end aligned to
  326. * MAX_ORDER_NR_PAGES.
  327. */
  328. prev_end = ALIGN(__phys_to_pfn(reg->base + reg->size),
  329. MAX_ORDER_NR_PAGES);
  330. }
  331. #ifdef CONFIG_SPARSEMEM
  332. if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
  333. free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
  334. #endif
  335. }
  336. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  337. /*
  338. * mem_init() marks the free areas in the mem_map and tells us how much memory
  339. * is free. This is done after various parts of the system have claimed their
  340. * memory after the kernel image.
  341. */
  342. void __init mem_init(void)
  343. {
  344. if (swiotlb_force == SWIOTLB_FORCE ||
  345. max_pfn > (arm64_dma_phys_limit >> PAGE_SHIFT))
  346. swiotlb_init(1);
  347. else
  348. swiotlb_force = SWIOTLB_NO_FORCE;
  349. set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
  350. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  351. free_unused_memmap();
  352. #endif
  353. /* this will put all unused low memory onto the freelists */
  354. free_all_bootmem();
  355. mem_init_print_info(NULL);
  356. #define MLK(b, t) b, t, ((t) - (b)) >> 10
  357. #define MLM(b, t) b, t, ((t) - (b)) >> 20
  358. #define MLG(b, t) b, t, ((t) - (b)) >> 30
  359. #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
  360. pr_notice("Virtual kernel memory layout:\n");
  361. #ifdef CONFIG_KASAN
  362. pr_notice(" kasan : 0x%16lx - 0x%16lx (%6ld GB)\n",
  363. MLG(KASAN_SHADOW_START, KASAN_SHADOW_END));
  364. #endif
  365. pr_notice(" modules : 0x%16lx - 0x%16lx (%6ld MB)\n",
  366. MLM(MODULES_VADDR, MODULES_END));
  367. pr_notice(" vmalloc : 0x%16lx - 0x%16lx (%6ld GB)\n",
  368. MLG(VMALLOC_START, VMALLOC_END));
  369. pr_notice(" .text : 0x%p" " - 0x%p" " (%6ld KB)\n",
  370. MLK_ROUNDUP(_text, _etext));
  371. pr_notice(" .rodata : 0x%p" " - 0x%p" " (%6ld KB)\n",
  372. MLK_ROUNDUP(__start_rodata, __init_begin));
  373. pr_notice(" .init : 0x%p" " - 0x%p" " (%6ld KB)\n",
  374. MLK_ROUNDUP(__init_begin, __init_end));
  375. pr_notice(" .data : 0x%p" " - 0x%p" " (%6ld KB)\n",
  376. MLK_ROUNDUP(_sdata, _edata));
  377. pr_notice(" .bss : 0x%p" " - 0x%p" " (%6ld KB)\n",
  378. MLK_ROUNDUP(__bss_start, __bss_stop));
  379. pr_notice(" fixed : 0x%16lx - 0x%16lx (%6ld KB)\n",
  380. MLK(FIXADDR_START, FIXADDR_TOP));
  381. pr_notice(" PCI I/O : 0x%16lx - 0x%16lx (%6ld MB)\n",
  382. MLM(PCI_IO_START, PCI_IO_END));
  383. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  384. pr_notice(" vmemmap : 0x%16lx - 0x%16lx (%6ld GB maximum)\n",
  385. MLG(VMEMMAP_START, VMEMMAP_START + VMEMMAP_SIZE));
  386. pr_notice(" 0x%16lx - 0x%16lx (%6ld MB actual)\n",
  387. MLM((unsigned long)phys_to_page(memblock_start_of_DRAM()),
  388. (unsigned long)virt_to_page(high_memory)));
  389. #endif
  390. pr_notice(" memory : 0x%16lx - 0x%16lx (%6ld MB)\n",
  391. MLM(__phys_to_virt(memblock_start_of_DRAM()),
  392. (unsigned long)high_memory));
  393. #undef MLK
  394. #undef MLM
  395. #undef MLK_ROUNDUP
  396. /*
  397. * Check boundaries twice: Some fundamental inconsistencies can be
  398. * detected at build time already.
  399. */
  400. #ifdef CONFIG_COMPAT
  401. BUILD_BUG_ON(TASK_SIZE_32 > TASK_SIZE_64);
  402. #endif
  403. #ifdef CONFIG_SPARSEMEM_VMEMMAP
  404. /*
  405. * Make sure we chose the upper bound of sizeof(struct page)
  406. * correctly when sizing the VMEMMAP array.
  407. */
  408. BUILD_BUG_ON(sizeof(struct page) > (1 << STRUCT_PAGE_MAX_SHIFT));
  409. #endif
  410. if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
  411. extern int sysctl_overcommit_memory;
  412. /*
  413. * On a machine this small we won't get anywhere without
  414. * overcommit, so turn it on by default.
  415. */
  416. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  417. }
  418. }
  419. void free_initmem(void)
  420. {
  421. free_reserved_area(__va(__pa(__init_begin)), __va(__pa(__init_end)),
  422. 0, "unused kernel");
  423. /*
  424. * Unmap the __init region but leave the VM area in place. This
  425. * prevents the region from being reused for kernel modules, which
  426. * is not supported by kallsyms.
  427. */
  428. unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin));
  429. }
  430. #ifdef CONFIG_BLK_DEV_INITRD
  431. static int keep_initrd __initdata;
  432. void __init free_initrd_mem(unsigned long start, unsigned long end)
  433. {
  434. if (!keep_initrd)
  435. free_reserved_area((void *)start, (void *)end, 0, "initrd");
  436. }
  437. static int __init keepinitrd_setup(char *__unused)
  438. {
  439. keep_initrd = 1;
  440. return 1;
  441. }
  442. __setup("keepinitrd", keepinitrd_setup);
  443. #endif
  444. /*
  445. * Dump out memory limit information on panic.
  446. */
  447. static int dump_mem_limit(struct notifier_block *self, unsigned long v, void *p)
  448. {
  449. if (memory_limit != (phys_addr_t)ULLONG_MAX) {
  450. pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
  451. } else {
  452. pr_emerg("Memory Limit: none\n");
  453. }
  454. return 0;
  455. }
  456. static struct notifier_block mem_limit_notifier = {
  457. .notifier_call = dump_mem_limit,
  458. };
  459. static int __init register_mem_limit_dumper(void)
  460. {
  461. atomic_notifier_chain_register(&panic_notifier_list,
  462. &mem_limit_notifier);
  463. return 0;
  464. }
  465. __initcall(register_mem_limit_dumper);