init.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/mm.h>
  10. #include <linux/bootmem.h>
  11. #include <linux/memblock.h>
  12. #ifdef CONFIG_BLK_DEV_INITRD
  13. #include <linux/initrd.h>
  14. #endif
  15. #include <linux/of_fdt.h>
  16. #include <linux/swap.h>
  17. #include <linux/module.h>
  18. #include <linux/highmem.h>
  19. #include <asm/page.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/sections.h>
  22. #include <asm/arcregs.h>
  23. pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
  24. char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  25. EXPORT_SYMBOL(empty_zero_page);
  26. static const unsigned long low_mem_start = CONFIG_LINUX_LINK_BASE;
  27. static unsigned long low_mem_sz;
  28. #ifdef CONFIG_HIGHMEM
  29. static unsigned long min_high_pfn, max_high_pfn;
  30. static u64 high_mem_start;
  31. static u64 high_mem_sz;
  32. #endif
  33. #ifdef CONFIG_DISCONTIGMEM
  34. struct pglist_data node_data[MAX_NUMNODES] __read_mostly;
  35. EXPORT_SYMBOL(node_data);
  36. #endif
  37. /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
  38. static int __init setup_mem_sz(char *str)
  39. {
  40. low_mem_sz = memparse(str, NULL) & PAGE_MASK;
  41. /* early console might not be setup yet - it will show up later */
  42. pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(low_mem_sz));
  43. return 0;
  44. }
  45. early_param("mem", setup_mem_sz);
  46. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  47. {
  48. int in_use = 0;
  49. if (!low_mem_sz) {
  50. if (base != low_mem_start)
  51. panic("CONFIG_LINUX_LINK_BASE != DT memory { }");
  52. low_mem_sz = size;
  53. in_use = 1;
  54. } else {
  55. #ifdef CONFIG_HIGHMEM
  56. high_mem_start = base;
  57. high_mem_sz = size;
  58. in_use = 1;
  59. #endif
  60. }
  61. pr_info("Memory @ %llx [%lldM] %s\n",
  62. base, TO_MB(size), !in_use ? "Not used":"");
  63. }
  64. #ifdef CONFIG_BLK_DEV_INITRD
  65. static int __init early_initrd(char *p)
  66. {
  67. unsigned long start, size;
  68. char *endp;
  69. start = memparse(p, &endp);
  70. if (*endp == ',') {
  71. size = memparse(endp + 1, NULL);
  72. initrd_start = (unsigned long)__va(start);
  73. initrd_end = (unsigned long)__va(start + size);
  74. }
  75. return 0;
  76. }
  77. early_param("initrd", early_initrd);
  78. #endif
  79. /*
  80. * First memory setup routine called from setup_arch()
  81. * 1. setup swapper's mm @init_mm
  82. * 2. Count the pages we have and setup bootmem allocator
  83. * 3. zone setup
  84. */
  85. void __init setup_arch_memory(void)
  86. {
  87. unsigned long zones_size[MAX_NR_ZONES];
  88. unsigned long zones_holes[MAX_NR_ZONES];
  89. init_mm.start_code = (unsigned long)_text;
  90. init_mm.end_code = (unsigned long)_etext;
  91. init_mm.end_data = (unsigned long)_edata;
  92. init_mm.brk = (unsigned long)_end;
  93. /* first page of system - kernel .vector starts here */
  94. min_low_pfn = ARCH_PFN_OFFSET;
  95. /* Last usable page of low mem */
  96. max_low_pfn = max_pfn = PFN_DOWN(low_mem_start + low_mem_sz);
  97. #ifdef CONFIG_FLATMEM
  98. /* pfn_valid() uses this */
  99. max_mapnr = max_low_pfn - min_low_pfn;
  100. #endif
  101. /*------------- bootmem allocator setup -----------------------*/
  102. /*
  103. * seed the bootmem allocator after any DT memory node parsing or
  104. * "mem=xxx" cmdline overrides have potentially updated @arc_mem_sz
  105. *
  106. * Only low mem is added, otherwise we have crashes when allocating
  107. * mem_map[] itself. NO_BOOTMEM allocates mem_map[] at the end of
  108. * avail memory, ending in highmem with a > 32-bit address. However
  109. * it then tries to memset it with a truncaed 32-bit handle, causing
  110. * the crash
  111. */
  112. memblock_add_node(low_mem_start, low_mem_sz, 0);
  113. memblock_reserve(low_mem_start, __pa(_end) - low_mem_start);
  114. #ifdef CONFIG_BLK_DEV_INITRD
  115. if (initrd_start)
  116. memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
  117. #endif
  118. early_init_fdt_reserve_self();
  119. early_init_fdt_scan_reserved_mem();
  120. memblock_dump_all();
  121. /*----------------- node/zones setup --------------------------*/
  122. memset(zones_size, 0, sizeof(zones_size));
  123. memset(zones_holes, 0, sizeof(zones_holes));
  124. zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
  125. zones_holes[ZONE_NORMAL] = 0;
  126. /*
  127. * We can't use the helper free_area_init(zones[]) because it uses
  128. * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
  129. * when our kernel doesn't start at PAGE_OFFSET, i.e.
  130. * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
  131. */
  132. free_area_init_node(0, /* node-id */
  133. zones_size, /* num pages per zone */
  134. min_low_pfn, /* first pfn of node */
  135. zones_holes); /* holes */
  136. #ifdef CONFIG_HIGHMEM
  137. /*
  138. * Populate a new node with highmem
  139. *
  140. * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
  141. * than addresses in normal ala low memory (0x8000_0000 based).
  142. * Even with PAE, the huge peripheral space hole would waste a lot of
  143. * mem with single mem_map[]. This warrants a mem_map per region design.
  144. * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
  145. *
  146. * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
  147. * populated with normal memory zone while node 1 only has highmem
  148. */
  149. node_set_online(1);
  150. min_high_pfn = PFN_DOWN(high_mem_start);
  151. max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
  152. zones_size[ZONE_NORMAL] = 0;
  153. zones_holes[ZONE_NORMAL] = 0;
  154. zones_size[ZONE_HIGHMEM] = max_high_pfn - min_high_pfn;
  155. zones_holes[ZONE_HIGHMEM] = 0;
  156. free_area_init_node(1, /* node-id */
  157. zones_size, /* num pages per zone */
  158. min_high_pfn, /* first pfn of node */
  159. zones_holes); /* holes */
  160. high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
  161. kmap_init();
  162. #endif
  163. }
  164. /*
  165. * mem_init - initializes memory
  166. *
  167. * Frees up bootmem
  168. * Calculates and displays memory available/used
  169. */
  170. void __init mem_init(void)
  171. {
  172. #ifdef CONFIG_HIGHMEM
  173. unsigned long tmp;
  174. reset_all_zones_managed_pages();
  175. for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
  176. free_highmem_page(pfn_to_page(tmp));
  177. #endif
  178. free_all_bootmem();
  179. mem_init_print_info(NULL);
  180. }
  181. /*
  182. * free_initmem: Free all the __init memory.
  183. */
  184. void __ref free_initmem(void)
  185. {
  186. free_initmem_default(-1);
  187. }
  188. #ifdef CONFIG_BLK_DEV_INITRD
  189. void __init free_initrd_mem(unsigned long start, unsigned long end)
  190. {
  191. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  192. }
  193. #endif