init.c 14 KB

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