init.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_BLOCK_DEV_RAM
  13. #include <linux/blk.h>
  14. #endif
  15. #include <linux/swap.h>
  16. #include <linux/module.h>
  17. #include <asm/page.h>
  18. #include <asm/pgalloc.h>
  19. #include <asm/sections.h>
  20. #include <asm/arcregs.h>
  21. pgd_t swapper_pg_dir[PTRS_PER_PGD] __aligned(PAGE_SIZE);
  22. char empty_zero_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  23. EXPORT_SYMBOL(empty_zero_page);
  24. /* Default tot mem from .config */
  25. static unsigned long arc_mem_sz = 0x20000000; /* some default */
  26. /* User can over-ride above with "mem=nnn[KkMm]" in cmdline */
  27. static int __init setup_mem_sz(char *str)
  28. {
  29. arc_mem_sz = memparse(str, NULL) & PAGE_MASK;
  30. /* early console might not be setup yet - it will show up later */
  31. pr_info("\"mem=%s\": mem sz set to %ldM\n", str, TO_MB(arc_mem_sz));
  32. return 0;
  33. }
  34. early_param("mem", setup_mem_sz);
  35. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  36. {
  37. arc_mem_sz = size & PAGE_MASK;
  38. pr_info("Memory size set via devicetree %ldM\n", TO_MB(arc_mem_sz));
  39. }
  40. /*
  41. * First memory setup routine called from setup_arch()
  42. * 1. setup swapper's mm @init_mm
  43. * 2. Count the pages we have and setup bootmem allocator
  44. * 3. zone setup
  45. */
  46. void __init setup_arch_memory(void)
  47. {
  48. unsigned long zones_size[MAX_NR_ZONES] = { 0, 0 };
  49. unsigned long end_mem = CONFIG_LINUX_LINK_BASE + arc_mem_sz;
  50. init_mm.start_code = (unsigned long)_text;
  51. init_mm.end_code = (unsigned long)_etext;
  52. init_mm.end_data = (unsigned long)_edata;
  53. init_mm.brk = (unsigned long)_end;
  54. /*
  55. * We do it here, so that memory is correctly instantiated
  56. * even if "mem=xxx" cmline over-ride is given and/or
  57. * DT has memory node. Each causes an update to @arc_mem_sz
  58. * and we finally add memory one here
  59. */
  60. memblock_add(CONFIG_LINUX_LINK_BASE, arc_mem_sz);
  61. /*------------- externs in mm need setting up ---------------*/
  62. /* first page of system - kernel .vector starts here */
  63. min_low_pfn = PFN_DOWN(CONFIG_LINUX_LINK_BASE);
  64. /* Last usable page of low mem (no HIGHMEM yet for ARC port) */
  65. max_low_pfn = max_pfn = PFN_DOWN(end_mem);
  66. max_mapnr = num_physpages = max_low_pfn - min_low_pfn;
  67. /*------------- reserve kernel image -----------------------*/
  68. memblock_reserve(CONFIG_LINUX_LINK_BASE,
  69. __pa(_end) - CONFIG_LINUX_LINK_BASE);
  70. memblock_dump_all();
  71. /*-------------- node setup --------------------------------*/
  72. memset(zones_size, 0, sizeof(zones_size));
  73. zones_size[ZONE_NORMAL] = num_physpages;
  74. /*
  75. * We can't use the helper free_area_init(zones[]) because it uses
  76. * PAGE_OFFSET to compute the @min_low_pfn which would be wrong
  77. * when our kernel doesn't start at PAGE_OFFSET, i.e.
  78. * PAGE_OFFSET != CONFIG_LINUX_LINK_BASE
  79. */
  80. free_area_init_node(0, /* node-id */
  81. zones_size, /* num pages per zone */
  82. min_low_pfn, /* first pfn of node */
  83. NULL); /* NO holes */
  84. }
  85. /*
  86. * mem_init - initializes memory
  87. *
  88. * Frees up bootmem
  89. * Calculates and displays memory available/used
  90. */
  91. void __init mem_init(void)
  92. {
  93. int codesize, datasize, initsize, reserved_pages, free_pages;
  94. int tmp;
  95. high_memory = (void *)(CONFIG_LINUX_LINK_BASE + arc_mem_sz);
  96. totalram_pages = free_all_bootmem();
  97. /* count all reserved pages [kernel code/data/mem_map..] */
  98. reserved_pages = 0;
  99. for (tmp = 0; tmp < max_mapnr; tmp++)
  100. if (PageReserved(mem_map + tmp))
  101. reserved_pages++;
  102. /* XXX: nr_free_pages() is equivalent */
  103. free_pages = max_mapnr - reserved_pages;
  104. /*
  105. * For the purpose of display below, split the "reserve mem"
  106. * kernel code/data is already shown explicitly,
  107. * Show any other reservations (mem_map[ ] et al)
  108. */
  109. reserved_pages -= (((unsigned int)_end - CONFIG_LINUX_LINK_BASE) >>
  110. PAGE_SHIFT);
  111. codesize = _etext - _text;
  112. datasize = _end - _etext;
  113. initsize = __init_end - __init_begin;
  114. pr_info("Memory Available: %dM / %ldM (%dK code, %dK data, %dK init, %dK reserv)\n",
  115. PAGES_TO_MB(free_pages),
  116. TO_MB(arc_mem_sz),
  117. TO_KB(codesize), TO_KB(datasize), TO_KB(initsize),
  118. PAGES_TO_KB(reserved_pages));
  119. }
  120. static void __init free_init_pages(const char *what, unsigned long begin,
  121. unsigned long end)
  122. {
  123. unsigned long addr;
  124. pr_info("Freeing %s: %ldk [%lx] to [%lx]\n",
  125. what, TO_KB(end - begin), begin, end);
  126. /* need to check that the page we free is not a partial page */
  127. for (addr = begin; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
  128. ClearPageReserved(virt_to_page(addr));
  129. init_page_count(virt_to_page(addr));
  130. free_page(addr);
  131. totalram_pages++;
  132. }
  133. }
  134. /*
  135. * free_initmem: Free all the __init memory.
  136. */
  137. void __init_refok free_initmem(void)
  138. {
  139. free_init_pages("unused kernel memory",
  140. (unsigned long)__init_begin,
  141. (unsigned long)__init_end);
  142. }
  143. #ifdef CONFIG_BLK_DEV_INITRD
  144. void __init free_initrd_mem(unsigned long start, unsigned long end)
  145. {
  146. free_init_pages("initrd memory", start, end);
  147. }
  148. #endif
  149. #ifdef CONFIG_OF_FLATTREE
  150. void __init early_init_dt_setup_initrd_arch(unsigned long start,
  151. unsigned long end)
  152. {
  153. pr_err("%s(%lx, %lx)\n", __func__, start, end);
  154. }
  155. #endif /* CONFIG_OF_FLATTREE */