init.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* init.c: memory initialisation for FRV
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * Derived from:
  12. * - linux/arch/m68knommu/mm/init.c
  13. * - Copyright (C) 1998 D. Jeff Dionne <jeff@lineo.ca>, Kenneth Albanowski <kjahds@kjahds.com>,
  14. * - Copyright (C) 2000 Lineo, Inc. (www.lineo.com)
  15. * - linux/arch/m68k/mm/init.c
  16. * - Copyright (C) 1995 Hamish Macdonald
  17. */
  18. #include <linux/config.h>
  19. #include <linux/signal.h>
  20. #include <linux/sched.h>
  21. #include <linux/pagemap.h>
  22. #include <linux/swap.h>
  23. #include <linux/mm.h>
  24. #include <linux/kernel.h>
  25. #include <linux/string.h>
  26. #include <linux/types.h>
  27. #include <linux/bootmem.h>
  28. #include <linux/highmem.h>
  29. #include <asm/setup.h>
  30. #include <asm/segment.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/system.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/virtconvert.h>
  36. #include <asm/sections.h>
  37. #include <asm/tlb.h>
  38. #undef DEBUG
  39. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  40. /*
  41. * BAD_PAGE is the page that is used for page faults when linux
  42. * is out-of-memory. Older versions of linux just did a
  43. * do_exit(), but using this instead means there is less risk
  44. * for a process dying in kernel mode, possibly leaving a inode
  45. * unused etc..
  46. *
  47. * BAD_PAGETABLE is the accompanying page-table: it is initialized
  48. * to point to BAD_PAGE entries.
  49. *
  50. * ZERO_PAGE is a special page that is used for zero-initialized
  51. * data and COW.
  52. */
  53. static unsigned long empty_bad_page_table;
  54. static unsigned long empty_bad_page;
  55. unsigned long empty_zero_page;
  56. /*****************************************************************************/
  57. /*
  58. *
  59. */
  60. void show_mem(void)
  61. {
  62. unsigned long i;
  63. int free = 0, total = 0, reserved = 0, shared = 0;
  64. printk("\nMem-info:\n");
  65. show_free_areas();
  66. i = max_mapnr;
  67. while (i-- > 0) {
  68. struct page *page = &mem_map[i];
  69. total++;
  70. if (PageReserved(page))
  71. reserved++;
  72. else if (!page_count(page))
  73. free++;
  74. else
  75. shared += page_count(page) - 1;
  76. }
  77. printk("%d pages of RAM\n",total);
  78. printk("%d free pages\n",free);
  79. printk("%d reserved pages\n",reserved);
  80. printk("%d pages shared\n",shared);
  81. } /* end show_mem() */
  82. /*****************************************************************************/
  83. /*
  84. * paging_init() continues the virtual memory environment setup which
  85. * was begun by the code in arch/head.S.
  86. * The parameters are pointers to where to stick the starting and ending
  87. * addresses of available kernel virtual memory.
  88. */
  89. void __init paging_init(void)
  90. {
  91. unsigned long zones_size[MAX_NR_ZONES] = {0, 0, 0};
  92. /* allocate some pages for kernel housekeeping tasks */
  93. empty_bad_page_table = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  94. empty_bad_page = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  95. empty_zero_page = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
  96. memset((void *) empty_zero_page, 0, PAGE_SIZE);
  97. #if CONFIG_HIGHMEM
  98. if (num_physpages - num_mappedpages) {
  99. pgd_t *pge;
  100. pud_t *pue;
  101. pmd_t *pme;
  102. pkmap_page_table = alloc_bootmem_pages(PAGE_SIZE);
  103. memset(pkmap_page_table, 0, PAGE_SIZE);
  104. pge = swapper_pg_dir + pgd_index_k(PKMAP_BASE);
  105. pue = pud_offset(pge, PKMAP_BASE);
  106. pme = pmd_offset(pue, PKMAP_BASE);
  107. __set_pmd(pme, virt_to_phys(pkmap_page_table) | _PAGE_TABLE);
  108. }
  109. #endif
  110. /* distribute the allocatable pages across the various zones and pass them to the allocator
  111. */
  112. zones_size[ZONE_DMA] = max_low_pfn - min_low_pfn;
  113. zones_size[ZONE_NORMAL] = 0;
  114. #ifdef CONFIG_HIGHMEM
  115. zones_size[ZONE_HIGHMEM] = num_physpages - num_mappedpages;
  116. #endif
  117. free_area_init(zones_size);
  118. #ifdef CONFIG_MMU
  119. /* initialise init's MMU context */
  120. init_new_context(&init_task, &init_mm);
  121. #endif
  122. } /* end paging_init() */
  123. /*****************************************************************************/
  124. /*
  125. *
  126. */
  127. void __init mem_init(void)
  128. {
  129. unsigned long npages = (memory_end - memory_start) >> PAGE_SHIFT;
  130. unsigned long tmp;
  131. #ifdef CONFIG_MMU
  132. unsigned long loop, pfn;
  133. int datapages = 0;
  134. #endif
  135. int codek = 0, datak = 0;
  136. /* this will put all memory onto the freelists */
  137. totalram_pages = free_all_bootmem();
  138. #ifdef CONFIG_MMU
  139. for (loop = 0 ; loop < npages ; loop++)
  140. if (PageReserved(&mem_map[loop]))
  141. datapages++;
  142. #ifdef CONFIG_HIGHMEM
  143. for (pfn = num_physpages - 1; pfn >= num_mappedpages; pfn--) {
  144. struct page *page = &mem_map[pfn];
  145. ClearPageReserved(page);
  146. set_page_count(page, 1);
  147. __free_page(page);
  148. totalram_pages++;
  149. }
  150. #endif
  151. codek = ((unsigned long) &_etext - (unsigned long) &_stext) >> 10;
  152. datak = datapages << (PAGE_SHIFT - 10);
  153. #else
  154. codek = (_etext - _stext) >> 10;
  155. datak = 0; //(_ebss - _sdata) >> 10;
  156. #endif
  157. tmp = nr_free_pages() << PAGE_SHIFT;
  158. printk("Memory available: %luKiB/%luKiB RAM, %luKiB/%luKiB ROM (%dKiB kernel code, %dKiB data)\n",
  159. tmp >> 10,
  160. npages << (PAGE_SHIFT - 10),
  161. (rom_length > 0) ? ((rom_length >> 10) - codek) : 0,
  162. rom_length >> 10,
  163. codek,
  164. datak
  165. );
  166. } /* end mem_init() */
  167. /*****************************************************************************/
  168. /*
  169. * free the memory that was only required for initialisation
  170. */
  171. void __init free_initmem(void)
  172. {
  173. #if defined(CONFIG_RAMKERNEL) && !defined(CONFIG_PROTECT_KERNEL)
  174. unsigned long start, end, addr;
  175. start = PAGE_ALIGN((unsigned long) &__init_begin); /* round up */
  176. end = ((unsigned long) &__init_end) & PAGE_MASK; /* round down */
  177. /* next to check that the page we free is not a partial page */
  178. for (addr = start; addr < end; addr += PAGE_SIZE) {
  179. ClearPageReserved(virt_to_page(addr));
  180. set_page_count(virt_to_page(addr), 1);
  181. free_page(addr);
  182. totalram_pages++;
  183. }
  184. printk("Freeing unused kernel memory: %ldKiB freed (0x%lx - 0x%lx)\n",
  185. (end - start) >> 10, start, end);
  186. #endif
  187. } /* end free_initmem() */
  188. /*****************************************************************************/
  189. /*
  190. * free the initial ramdisk memory
  191. */
  192. #ifdef CONFIG_BLK_DEV_INITRD
  193. void __init free_initrd_mem(unsigned long start, unsigned long end)
  194. {
  195. int pages = 0;
  196. for (; start < end; start += PAGE_SIZE) {
  197. ClearPageReserved(virt_to_page(start));
  198. set_page_count(virt_to_page(start), 1);
  199. free_page(start);
  200. totalram_pages++;
  201. pages++;
  202. }
  203. printk("Freeing initrd memory: %dKiB freed\n", (pages * PAGE_SIZE) >> 10);
  204. } /* end free_initrd_mem() */
  205. #endif