init.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * arch/xtensa/mm/init.c
  3. *
  4. * Derived from MIPS, PPC.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. * Copyright (C) 2014 - 2016 Cadence Design Systems Inc.
  12. *
  13. * Chris Zankel <chris@zankel.net>
  14. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  15. * Marc Gauthier
  16. * Kevin Chea
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/bootmem.h>
  21. #include <linux/gfp.h>
  22. #include <linux/highmem.h>
  23. #include <linux/swap.h>
  24. #include <linux/mman.h>
  25. #include <linux/nodemask.h>
  26. #include <linux/mm.h>
  27. #include <linux/of_fdt.h>
  28. #include <asm/bootparam.h>
  29. #include <asm/page.h>
  30. #include <asm/sections.h>
  31. #include <asm/sysmem.h>
  32. /*
  33. * Initialize the bootmem system and give it all low memory we have available.
  34. */
  35. void __init bootmem_init(void)
  36. {
  37. /* Reserve all memory below PHYS_OFFSET, as memory
  38. * accounting doesn't work for pages below that address.
  39. *
  40. * If PHYS_OFFSET is zero reserve page at address 0:
  41. * successfull allocations should never return NULL.
  42. */
  43. if (PHYS_OFFSET)
  44. memblock_reserve(0, PHYS_OFFSET);
  45. else
  46. memblock_reserve(0, 1);
  47. early_init_fdt_scan_reserved_mem();
  48. if (!memblock_phys_mem_size())
  49. panic("No memory found!\n");
  50. min_low_pfn = PFN_UP(memblock_start_of_DRAM());
  51. min_low_pfn = max(min_low_pfn, PFN_UP(PHYS_OFFSET));
  52. max_pfn = PFN_DOWN(memblock_end_of_DRAM());
  53. max_low_pfn = min(max_pfn, MAX_LOW_PFN);
  54. memblock_set_current_limit(PFN_PHYS(max_low_pfn));
  55. memblock_dump_all();
  56. }
  57. void __init zones_init(void)
  58. {
  59. /* All pages are DMA-able, so we put them all in the DMA zone. */
  60. unsigned long zones_size[MAX_NR_ZONES] = {
  61. [ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET,
  62. #ifdef CONFIG_HIGHMEM
  63. [ZONE_HIGHMEM] = max_pfn - max_low_pfn,
  64. #endif
  65. };
  66. free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
  67. }
  68. #ifdef CONFIG_HIGHMEM
  69. static void __init free_area_high(unsigned long pfn, unsigned long end)
  70. {
  71. for (; pfn < end; pfn++)
  72. free_highmem_page(pfn_to_page(pfn));
  73. }
  74. static void __init free_highpages(void)
  75. {
  76. unsigned long max_low = max_low_pfn;
  77. struct memblock_region *mem, *res;
  78. reset_all_zones_managed_pages();
  79. /* set highmem page free */
  80. for_each_memblock(memory, mem) {
  81. unsigned long start = memblock_region_memory_base_pfn(mem);
  82. unsigned long end = memblock_region_memory_end_pfn(mem);
  83. /* Ignore complete lowmem entries */
  84. if (end <= max_low)
  85. continue;
  86. if (memblock_is_nomap(mem))
  87. continue;
  88. /* Truncate partial highmem entries */
  89. if (start < max_low)
  90. start = max_low;
  91. /* Find and exclude any reserved regions */
  92. for_each_memblock(reserved, res) {
  93. unsigned long res_start, res_end;
  94. res_start = memblock_region_reserved_base_pfn(res);
  95. res_end = memblock_region_reserved_end_pfn(res);
  96. if (res_end < start)
  97. continue;
  98. if (res_start < start)
  99. res_start = start;
  100. if (res_start > end)
  101. res_start = end;
  102. if (res_end > end)
  103. res_end = end;
  104. if (res_start != start)
  105. free_area_high(start, res_start);
  106. start = res_end;
  107. if (start == end)
  108. break;
  109. }
  110. /* And now free anything which remains */
  111. if (start < end)
  112. free_area_high(start, end);
  113. }
  114. }
  115. #else
  116. static void __init free_highpages(void)
  117. {
  118. }
  119. #endif
  120. /*
  121. * Initialize memory pages.
  122. */
  123. void __init mem_init(void)
  124. {
  125. free_highpages();
  126. max_mapnr = max_pfn - ARCH_PFN_OFFSET;
  127. high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);
  128. free_all_bootmem();
  129. mem_init_print_info(NULL);
  130. pr_info("virtual kernel memory layout:\n"
  131. #ifdef CONFIG_HIGHMEM
  132. " pkmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
  133. " fixmap : 0x%08lx - 0x%08lx (%5lu kB)\n"
  134. #endif
  135. #ifdef CONFIG_MMU
  136. " vmalloc : 0x%08lx - 0x%08lx (%5lu MB)\n"
  137. #endif
  138. " lowmem : 0x%08lx - 0x%08lx (%5lu MB)\n",
  139. #ifdef CONFIG_HIGHMEM
  140. PKMAP_BASE, PKMAP_BASE + LAST_PKMAP * PAGE_SIZE,
  141. (LAST_PKMAP*PAGE_SIZE) >> 10,
  142. FIXADDR_START, FIXADDR_TOP,
  143. (FIXADDR_TOP - FIXADDR_START) >> 10,
  144. #endif
  145. #ifdef CONFIG_MMU
  146. VMALLOC_START, VMALLOC_END,
  147. (VMALLOC_END - VMALLOC_START) >> 20,
  148. PAGE_OFFSET, PAGE_OFFSET +
  149. (max_low_pfn - min_low_pfn) * PAGE_SIZE,
  150. #else
  151. min_low_pfn * PAGE_SIZE, max_low_pfn * PAGE_SIZE,
  152. #endif
  153. ((max_low_pfn - min_low_pfn) * PAGE_SIZE) >> 20);
  154. }
  155. #ifdef CONFIG_BLK_DEV_INITRD
  156. extern int initrd_is_mapped;
  157. void free_initrd_mem(unsigned long start, unsigned long end)
  158. {
  159. if (initrd_is_mapped)
  160. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  161. }
  162. #endif
  163. void free_initmem(void)
  164. {
  165. free_initmem_default(-1);
  166. }
  167. static void __init parse_memmap_one(char *p)
  168. {
  169. char *oldp;
  170. unsigned long start_at, mem_size;
  171. if (!p)
  172. return;
  173. oldp = p;
  174. mem_size = memparse(p, &p);
  175. if (p == oldp)
  176. return;
  177. switch (*p) {
  178. case '@':
  179. start_at = memparse(p + 1, &p);
  180. memblock_add(start_at, mem_size);
  181. break;
  182. case '$':
  183. start_at = memparse(p + 1, &p);
  184. memblock_reserve(start_at, mem_size);
  185. break;
  186. case 0:
  187. memblock_reserve(mem_size, -mem_size);
  188. break;
  189. default:
  190. pr_warn("Unrecognized memmap syntax: %s\n", p);
  191. break;
  192. }
  193. }
  194. static int __init parse_memmap_opt(char *str)
  195. {
  196. while (str) {
  197. char *k = strchr(str, ',');
  198. if (k)
  199. *k++ = 0;
  200. parse_memmap_one(str);
  201. str = k;
  202. }
  203. return 0;
  204. }
  205. early_param("memmap", parse_memmap_opt);