init.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * arch/xtensa/mm/init.c
  3. *
  4. * Derived from MIPS, PPC.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2001 - 2005 Tensilica Inc.
  11. *
  12. * Chris Zankel <chris@zankel.net>
  13. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  14. * Marc Gauthier
  15. * Kevin Chea
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/bootmem.h>
  20. #include <linux/gfp.h>
  21. #include <linux/swap.h>
  22. #include <linux/mman.h>
  23. #include <linux/nodemask.h>
  24. #include <linux/mm.h>
  25. #include <asm/bootparam.h>
  26. #include <asm/page.h>
  27. /* References to section boundaries */
  28. extern char _stext, _etext, _sdata, _edata, _rodata_end;
  29. extern char __init_begin, __init_end;
  30. /*
  31. * mem_reserve(start, end, must_exist)
  32. *
  33. * Reserve some memory from the memory pool.
  34. *
  35. * Parameters:
  36. * start Start of region,
  37. * end End of region,
  38. * must_exist Must exist in memory pool.
  39. *
  40. * Returns:
  41. * 0 (memory area couldn't be mapped)
  42. * -1 (success)
  43. */
  44. int __init mem_reserve(unsigned long start, unsigned long end, int must_exist)
  45. {
  46. int i;
  47. if (start == end)
  48. return 0;
  49. start = start & PAGE_MASK;
  50. end = PAGE_ALIGN(end);
  51. for (i = 0; i < sysmem.nr_banks; i++)
  52. if (start < sysmem.bank[i].end
  53. && end >= sysmem.bank[i].start)
  54. break;
  55. if (i == sysmem.nr_banks) {
  56. if (must_exist)
  57. printk (KERN_WARNING "mem_reserve: [0x%0lx, 0x%0lx) "
  58. "not in any region!\n", start, end);
  59. return 0;
  60. }
  61. if (start > sysmem.bank[i].start) {
  62. if (end < sysmem.bank[i].end) {
  63. /* split entry */
  64. if (sysmem.nr_banks >= SYSMEM_BANKS_MAX)
  65. panic("meminfo overflow\n");
  66. sysmem.bank[sysmem.nr_banks].start = end;
  67. sysmem.bank[sysmem.nr_banks].end = sysmem.bank[i].end;
  68. sysmem.nr_banks++;
  69. }
  70. sysmem.bank[i].end = start;
  71. } else {
  72. if (end < sysmem.bank[i].end)
  73. sysmem.bank[i].start = end;
  74. else {
  75. /* remove entry */
  76. sysmem.nr_banks--;
  77. sysmem.bank[i].start = sysmem.bank[sysmem.nr_banks].start;
  78. sysmem.bank[i].end = sysmem.bank[sysmem.nr_banks].end;
  79. }
  80. }
  81. return -1;
  82. }
  83. /*
  84. * Initialize the bootmem system and give it all the memory we have available.
  85. */
  86. void __init bootmem_init(void)
  87. {
  88. unsigned long pfn;
  89. unsigned long bootmap_start, bootmap_size;
  90. int i;
  91. max_low_pfn = max_pfn = 0;
  92. min_low_pfn = ~0;
  93. for (i=0; i < sysmem.nr_banks; i++) {
  94. pfn = PAGE_ALIGN(sysmem.bank[i].start) >> PAGE_SHIFT;
  95. if (pfn < min_low_pfn)
  96. min_low_pfn = pfn;
  97. pfn = PAGE_ALIGN(sysmem.bank[i].end - 1) >> PAGE_SHIFT;
  98. if (pfn > max_pfn)
  99. max_pfn = pfn;
  100. }
  101. if (min_low_pfn > max_pfn)
  102. panic("No memory found!\n");
  103. max_low_pfn = max_pfn < MAX_MEM_PFN >> PAGE_SHIFT ?
  104. max_pfn : MAX_MEM_PFN >> PAGE_SHIFT;
  105. /* Find an area to use for the bootmem bitmap. */
  106. bootmap_size = bootmem_bootmap_pages(max_low_pfn - min_low_pfn);
  107. bootmap_size <<= PAGE_SHIFT;
  108. bootmap_start = ~0;
  109. for (i=0; i<sysmem.nr_banks; i++)
  110. if (sysmem.bank[i].end - sysmem.bank[i].start >= bootmap_size) {
  111. bootmap_start = sysmem.bank[i].start;
  112. break;
  113. }
  114. if (bootmap_start == ~0UL)
  115. panic("Cannot find %ld bytes for bootmap\n", bootmap_size);
  116. /* Reserve the bootmem bitmap area */
  117. mem_reserve(bootmap_start, bootmap_start + bootmap_size, 1);
  118. bootmap_size = init_bootmem_node(NODE_DATA(0),
  119. bootmap_start >> PAGE_SHIFT,
  120. min_low_pfn,
  121. max_low_pfn);
  122. /* Add all remaining memory pieces into the bootmem map */
  123. for (i=0; i<sysmem.nr_banks; i++)
  124. free_bootmem(sysmem.bank[i].start,
  125. sysmem.bank[i].end - sysmem.bank[i].start);
  126. }
  127. void __init zones_init(void)
  128. {
  129. unsigned long zones_size[MAX_NR_ZONES];
  130. int i;
  131. /* All pages are DMA-able, so we put them all in the DMA zone. */
  132. zones_size[ZONE_DMA] = max_low_pfn - ARCH_PFN_OFFSET;
  133. for (i = 1; i < MAX_NR_ZONES; i++)
  134. zones_size[i] = 0;
  135. #ifdef CONFIG_HIGHMEM
  136. zones_size[ZONE_HIGHMEM] = max_pfn - max_low_pfn;
  137. #endif
  138. free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL);
  139. }
  140. /*
  141. * Initialize memory pages.
  142. */
  143. void __init mem_init(void)
  144. {
  145. unsigned long codesize, reservedpages, datasize, initsize;
  146. unsigned long highmemsize, tmp, ram;
  147. max_mapnr = num_physpages = max_low_pfn - ARCH_PFN_OFFSET;
  148. high_memory = (void *) __va(max_low_pfn << PAGE_SHIFT);
  149. highmemsize = 0;
  150. #ifdef CONFIG_HIGHMEM
  151. #error HIGHGMEM not implemented in init.c
  152. #endif
  153. totalram_pages += free_all_bootmem();
  154. reservedpages = ram = 0;
  155. for (tmp = 0; tmp < max_mapnr; tmp++) {
  156. ram++;
  157. if (PageReserved(mem_map+tmp))
  158. reservedpages++;
  159. }
  160. codesize = (unsigned long) &_etext - (unsigned long) &_stext;
  161. datasize = (unsigned long) &_edata - (unsigned long) &_sdata;
  162. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  163. printk("Memory: %luk/%luk available (%ldk kernel code, %ldk reserved, "
  164. "%ldk data, %ldk init %ldk highmem)\n",
  165. nr_free_pages() << (PAGE_SHIFT-10),
  166. ram << (PAGE_SHIFT-10),
  167. codesize >> 10,
  168. reservedpages << (PAGE_SHIFT-10),
  169. datasize >> 10,
  170. initsize >> 10,
  171. highmemsize >> 10);
  172. }
  173. void
  174. free_reserved_mem(void *start, void *end)
  175. {
  176. for (; start < end; start += PAGE_SIZE) {
  177. ClearPageReserved(virt_to_page(start));
  178. init_page_count(virt_to_page(start));
  179. free_page((unsigned long)start);
  180. totalram_pages++;
  181. }
  182. }
  183. #ifdef CONFIG_BLK_DEV_INITRD
  184. extern int initrd_is_mapped;
  185. void free_initrd_mem(unsigned long start, unsigned long end)
  186. {
  187. if (initrd_is_mapped) {
  188. free_reserved_mem((void*)start, (void*)end);
  189. printk ("Freeing initrd memory: %ldk freed\n",(end-start)>>10);
  190. }
  191. }
  192. #endif
  193. void free_initmem(void)
  194. {
  195. free_reserved_mem(&__init_begin, &__init_end);
  196. printk("Freeing unused kernel memory: %dk freed\n",
  197. (&__init_end - &__init_begin) >> 10);
  198. }