init.c 5.5 KB

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