init.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/memblock.h>
  8. #ifdef CONFIG_BLK_DEV_INITRD
  9. #include <linux/initrd.h>
  10. #endif
  11. #include <linux/of_fdt.h>
  12. #include <linux/swap.h>
  13. #include <linux/module.h>
  14. #include <linux/highmem.h>
  15. #include <asm/page.h>
  16. #include <asm/sections.h>
  17. #include <asm/setup.h>
  18. #include <asm/arcregs.h>
  19. pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
  20. char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  21. EXPORT_SYMBOL(empty_zero_page);
  22. static const unsigned long low_mem_start = CONFIG_LINUX_RAM_BASE;
  23. static unsigned long low_mem_sz;
  24. #ifdef CONFIG_HIGHMEM
  25. static unsigned long min_high_pfn, max_high_pfn;
  26. static phys_addr_t high_mem_start;
  27. static phys_addr_t high_mem_sz;
  28. unsigned long arch_pfn_offset;
  29. EXPORT_SYMBOL(arch_pfn_offset);
  30. #endif
  31. long __init arc_get_mem_sz(void)
  32. {
  33. return low_mem_sz;
  34. }
  35. /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
  36. static int __init setup_mem_sz(char *str)
  37. {
  38. low_mem_sz = memparse(str, NULL) & PAGE_MASK;
  39. /* early console might not be setup yet - it will show up later */
  40. pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
  41. return 0;
  42. }
  43. early_param("mem", setup_mem_sz);
  44. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  45. {
  46. int in_use = 0;
  47. if (!low_mem_sz) {
  48. if (base != low_mem_start)
  49. panic("CONFIG_LINUX_RAM_BASE != DT memory { }");
  50. low_mem_sz = size;
  51. in_use = 1;
  52. memblock_add_node(base, size, 0, MEMBLOCK_NONE);
  53. } else {
  54. #ifdef CONFIG_HIGHMEM
  55. high_mem_start = base;
  56. high_mem_sz = size;
  57. in_use = 1;
  58. memblock_add_node(base, size, 1, MEMBLOCK_NONE);
  59. memblock_reserve(base, size);
  60. #endif
  61. }
  62. pr_info("Memory @ %llx [%lldM] %s\n",
  63. base, TO_MB(size), !in_use ? "Not used":"");
  64. }
  65. /*
  66. * First memory setup routine called from setup_arch()
  67. * 1. setup swapper's mm @init_mm
  68. * 2. Count the pages we have and setup bootmem allocator
  69. * 3. zone setup
  70. */
  71. void __init setup_arch_memory(void)
  72. {
  73. unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
  74. setup_initial_init_mm(_text, _etext, _edata, _end);
  75. /* first page of system - kernel .vector starts here */
  76. min_low_pfn = virt_to_pfn((void *)CONFIG_LINUX_RAM_BASE);
  77. /* Last usable page of low mem */
  78. max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
  79. /*------------- bootmem allocator setup -----------------------*/
  80. /*
  81. * seed the bootmem allocator after any DT memory node parsing or
  82. * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
  83. *
  84. * Only low mem is added, otherwise we have crashes when allocating
  85. * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
  86. * avail memory, ending in highmem with a > 32-bit address. However
  87. * it then tries to memset it with a truncaed 32-bit handle, causing
  88. * the crash
  89. */
  90. memblock_reserve(CONFIG_LINUX_LINK_BASE,
  91. __pa(_end) - CONFIG_LINUX_LINK_BASE);
  92. #ifdef CONFIG_BLK_DEV_INITRD
  93. if (phys_initrd_size) {
  94. memblock_reserve(phys_initrd_start, phys_initrd_size);
  95. initrd_start = (unsigned long)__va(phys_initrd_start);
  96. initrd_end = initrd_start + phys_initrd_size;
  97. }
  98. #endif
  99. early_init_fdt_reserve_self();
  100. early_init_fdt_scan_reserved_mem();
  101. memblock_dump_all();
  102. /*----------------- node/zones setup --------------------------*/
  103. max_zone_pfn[ZONE_NORMAL] = max_low_pfn;
  104. #ifdef CONFIG_HIGHMEM
  105. /*
  106. * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
  107. * than addresses in normal aka low memory (0x8000_0000 based).
  108. * Even with PAE, the huge peripheral space hole would waste a lot of
  109. * mem with single contiguous mem_map[].
  110. * Thus when HIGHMEM on ARC is enabled the memory map corresponding
  111. * to the hole is freed and ARC specific version of pfn_valid()
  112. * handles the hole in the memory map.
  113. */
  114. min_high_pfn = PFN_DOWN(high_mem_start);
  115. max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
  116. /*
  117. * max_high_pfn should be ok here for both HIGHMEM and HIGHMEM+PAE.
  118. * For HIGHMEM without PAE max_high_pfn should be less than
  119. * min_low_pfn to guarantee that these two regions don't overlap.
  120. * For PAE case highmem is greater than lowmem, so it is natural
  121. * to use max_high_pfn.
  122. *
  123. * In both cases, holes should be handled by pfn_valid().
  124. */
  125. max_zone_pfn[ZONE_HIGHMEM] = max_high_pfn;
  126. high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
  127. arch_pfn_offset = min(min_low_pfn, min_high_pfn);
  128. kmap_init();
  129. #else /* CONFIG_HIGHMEM */
  130. /* pfn_valid() uses this when FLATMEM=y and HIGHMEM=n */
  131. max_mapnr = max_low_pfn - min_low_pfn;
  132. #endif /* CONFIG_HIGHMEM */
  133. free_area_init(max_zone_pfn);
  134. }
  135. static void __init highmem_init(void)
  136. {
  137. #ifdef CONFIG_HIGHMEM
  138. unsigned long tmp;
  139. memblock_phys_free(high_mem_start, high_mem_sz);
  140. for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
  141. free_highmem_page(pfn_to_page(tmp));
  142. #endif
  143. }
  144. /*
  145. * mem_init - initializes memory
  146. *
  147. * Frees up bootmem
  148. * Calculates and displays memory available/used
  149. */
  150. void __init mem_init(void)
  151. {
  152. memblock_free_all();
  153. highmem_init();
  154. BUILD_BUG_ON((PTRS_PER_PGD * sizeof(pgd_t)) > PAGE_SIZE);
  155. BUILD_BUG_ON((PTRS_PER_PUD * sizeof(pud_t)) > PAGE_SIZE);
  156. BUILD_BUG_ON((PTRS_PER_PMD * sizeof(pmd_t)) > PAGE_SIZE);
  157. BUILD_BUG_ON((PTRS_PER_PTE * sizeof(pte_t)) > PAGE_SIZE);
  158. }
  159. #ifdef CONFIG_HIGHMEM
  160. int pfn_valid(unsigned long pfn)
  161. {
  162. return (pfn >= min_high_pfn && pfn <= max_high_pfn) ||
  163. (pfn >= min_low_pfn && pfn <= max_low_pfn);
  164. }
  165. EXPORT_SYMBOL(pfn_valid);
  166. #endif