init.c 6.1 KB

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