init.c 5.2 KB

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