init.c 5.1 KB

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