init.c 6.1 KB

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