init.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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_LINK_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(low_mem_start, __pa(_end) - low_mem_start);
  118. #ifdef CONFIG_BLK_DEV_INITRD
  119. if (initrd_start)
  120. memblock_reserve(__pa(initrd_start), initrd_end - initrd_start);
  121. #endif
  122. early_init_fdt_reserve_self();
  123. early_init_fdt_scan_reserved_mem();
  124. memblock_dump_all();
  125. /*----------------- node/zones setup --------------------------*/
  126. memset(zones_size, 0, sizeof(zones_size));
  127. memset(zones_holes, 0, sizeof(zones_holes));
  128. zones_size[ZONE_NORMAL] = max_low_pfn - min_low_pfn;
  129. zones_holes[ZONE_NORMAL] = 0;
  130. /*
  131. * We can't use the helper free_area_init(zones[]) because it uses
  132. * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
  133. * when our kernel doesn't start at PAGE_OFFSET, i.e.
  134. * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
  135. */
  136. free_area_init_node(0, /* node-id */
  137. zones_size, /* num pages per zone */
  138. min_low_pfn, /* first pfn of node */
  139. zones_holes); /* holes */
  140. #ifdef CONFIG_HIGHMEM
  141. /*
  142. * Populate a new node with highmem
  143. *
  144. * On ARC (w/o PAE) HIGHMEM addresses are actually smaller (0 based)
  145. * than addresses in normal ala low memory (0x8000_0000 based).
  146. * Even with PAE, the huge peripheral space hole would waste a lot of
  147. * mem with single mem_map[]. This warrants a mem_map per region design.
  148. * Thus HIGHMEM on ARC is imlemented with DISCONTIGMEM.
  149. *
  150. * DISCONTIGMEM in turns requires multiple nodes. node 0 above is
  151. * populated with normal memory zone while node 1 only has highmem
  152. */
  153. node_set_online(1);
  154. min_high_pfn = PFN_DOWN(high_mem_start);
  155. max_high_pfn = PFN_DOWN(high_mem_start + high_mem_sz);
  156. zones_size[ZONE_NORMAL] = 0;
  157. zones_holes[ZONE_NORMAL] = 0;
  158. zones_size[ZONE_HIGHMEM] = max_high_pfn - min_high_pfn;
  159. zones_holes[ZONE_HIGHMEM] = 0;
  160. free_area_init_node(1, /* node-id */
  161. zones_size, /* num pages per zone */
  162. min_high_pfn, /* first pfn of node */
  163. zones_holes); /* holes */
  164. high_memory = (void *)(min_high_pfn << PAGE_SHIFT);
  165. kmap_init();
  166. #endif
  167. }
  168. /*
  169. * mem_init - initializes memory
  170. *
  171. * Frees up bootmem
  172. * Calculates and displays memory available/used
  173. */
  174. void __init mem_init(void)
  175. {
  176. #ifdef CONFIG_HIGHMEM
  177. unsigned long tmp;
  178. reset_all_zones_managed_pages();
  179. for (tmp = min_high_pfn; tmp < max_high_pfn; tmp++)
  180. free_highmem_page(pfn_to_page(tmp));
  181. #endif
  182. free_all_bootmem();
  183. mem_init_print_info(NULL);
  184. }
  185. /*
  186. * free_initmem: Free all the __init memory.
  187. */
  188. void __ref free_initmem(void)
  189. {
  190. free_initmem_default(-1);
  191. }
  192. #ifdef CONFIG_BLK_DEV_INITRD
  193. void __init free_initrd_mem(unsigned long start, unsigned long end)
  194. {
  195. free_reserved_area((void *)start, (void *)end, -1, "initrd");
  196. }
  197. #endif