init.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /* $Id: init.c,v 1.19 2004/02/21 04:42:16 kkojima Exp $
  2. *
  3. * linux/arch/sh/mm/init.c
  4. *
  5. * Copyright (C) 1999 Niibe Yutaka
  6. * Copyright (C) 2002, 2004 Paul Mundt
  7. *
  8. * Based on linux/arch/i386/mm/init.c:
  9. * Copyright (C) 1995 Linus Torvalds
  10. */
  11. #include <linux/signal.h>
  12. #include <linux/sched.h>
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/string.h>
  16. #include <linux/types.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/mman.h>
  19. #include <linux/mm.h>
  20. #include <linux/swap.h>
  21. #include <linux/smp.h>
  22. #include <linux/init.h>
  23. #include <linux/highmem.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/pagemap.h>
  26. #include <linux/proc_fs.h>
  27. #include <asm/processor.h>
  28. #include <asm/system.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/pgalloc.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/io.h>
  34. #include <asm/tlb.h>
  35. #include <asm/cacheflush.h>
  36. #include <asm/cache.h>
  37. DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
  38. pgd_t swapper_pg_dir[PTRS_PER_PGD];
  39. #ifdef CONFIG_MMU
  40. /* It'd be good if these lines were in the standard header file. */
  41. #define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
  42. #define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
  43. #endif
  44. void (*copy_page)(void *from, void *to);
  45. void (*clear_page)(void *to);
  46. void show_mem(void)
  47. {
  48. int i, total = 0, reserved = 0;
  49. int shared = 0, cached = 0;
  50. printk("Mem-info:\n");
  51. show_free_areas();
  52. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  53. i = max_mapnr;
  54. while (i-- > 0) {
  55. total++;
  56. if (PageReserved(mem_map+i))
  57. reserved++;
  58. else if (PageSwapCache(mem_map+i))
  59. cached++;
  60. else if (page_count(mem_map+i))
  61. shared += page_count(mem_map+i) - 1;
  62. }
  63. printk("%d pages of RAM\n",total);
  64. printk("%d reserved pages\n",reserved);
  65. printk("%d pages shared\n",shared);
  66. printk("%d pages swap cached\n",cached);
  67. }
  68. #ifdef CONFIG_MMU
  69. static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
  70. {
  71. pgd_t *pgd;
  72. pud_t *pud;
  73. pmd_t *pmd;
  74. pte_t *pte;
  75. pgd = pgd_offset_k(addr);
  76. if (pgd_none(*pgd)) {
  77. pgd_ERROR(*pgd);
  78. return;
  79. }
  80. pud = pud_alloc(NULL, pgd, addr);
  81. if (unlikely(!pud)) {
  82. pud_ERROR(*pud);
  83. return;
  84. }
  85. pmd = pmd_alloc(NULL, pud, addr);
  86. if (unlikely(!pmd)) {
  87. pmd_ERROR(*pmd);
  88. return;
  89. }
  90. pte = pte_offset_kernel(pmd, addr);
  91. if (!pte_none(*pte)) {
  92. pte_ERROR(*pte);
  93. return;
  94. }
  95. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
  96. flush_tlb_one(get_asid(), addr);
  97. }
  98. /*
  99. * As a performance optimization, other platforms preserve the fixmap mapping
  100. * across a context switch, we don't presently do this, but this could be done
  101. * in a similar fashion as to the wired TLB interface that sh64 uses (by way
  102. * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
  103. * give up a TLB entry for each mapping we want to preserve. While this may be
  104. * viable for a small number of fixmaps, it's not particularly useful for
  105. * everything and needs to be carefully evaluated. (ie, we may want this for
  106. * the vsyscall page).
  107. *
  108. * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
  109. * in at __set_fixmap() time to determine the appropriate behavior to follow.
  110. *
  111. * -- PFM.
  112. */
  113. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  114. {
  115. unsigned long address = __fix_to_virt(idx);
  116. if (idx >= __end_of_fixed_addresses) {
  117. BUG();
  118. return;
  119. }
  120. set_pte_phys(address, phys, prot);
  121. }
  122. #endif /* CONFIG_MMU */
  123. /* References to section boundaries */
  124. extern char _text, _etext, _edata, __bss_start, _end;
  125. extern char __init_begin, __init_end;
  126. /*
  127. * paging_init() sets up the page tables
  128. */
  129. void __init paging_init(void)
  130. {
  131. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  132. /*
  133. * Setup some defaults for the zone sizes.. these should be safe
  134. * regardless of distcontiguous memory or MMU settings.
  135. */
  136. zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
  137. #ifdef CONFIG_HIGHMEM
  138. zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
  139. #endif
  140. #ifdef CONFIG_MMU
  141. /*
  142. * If we have an MMU, and want to be using it .. we need to adjust
  143. * the zone sizes accordingly, in addition to turning it on.
  144. */
  145. {
  146. /* We don't need to map the kernel through the TLB, as
  147. * it is permanatly mapped using P1. So clear the
  148. * entire pgd. */
  149. memset(swapper_pg_dir, 0, sizeof(swapper_pg_dir));
  150. /* Turn on the MMU */
  151. enable_mmu();
  152. zones_size[ZONE_NORMAL] = MAX_LOW_PFN - START_PFN;
  153. }
  154. /* Set an initial value for the MMU.TTB so we don't have to
  155. * check for a null value. */
  156. set_TTB(swapper_pg_dir);
  157. #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
  158. /*
  159. * If we don't have CONFIG_MMU set and the processor in question
  160. * still has an MMU, care needs to be taken to make sure it doesn't
  161. * stay on.. Since the boot loader could have potentially already
  162. * turned it on, and we clearly don't want it, we simply turn it off.
  163. *
  164. * We don't need to do anything special for the zone sizes, since the
  165. * default values that were already configured up above should be
  166. * satisfactory.
  167. */
  168. disable_mmu();
  169. #endif
  170. NODE_DATA(0)->node_mem_map = NULL;
  171. free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
  172. }
  173. static struct kcore_list kcore_mem, kcore_vmalloc;
  174. void __init mem_init(void)
  175. {
  176. int codesize, reservedpages, datasize, initsize;
  177. int tmp;
  178. extern unsigned long memory_start;
  179. #ifdef CONFIG_MMU
  180. high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
  181. #else
  182. extern unsigned long memory_end;
  183. high_memory = (void *)(memory_end & PAGE_MASK);
  184. #endif
  185. max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
  186. /* clear the zero-page */
  187. memset(empty_zero_page, 0, PAGE_SIZE);
  188. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  189. /*
  190. * Setup wrappers for copy/clear_page(), these will get overridden
  191. * later in the boot process if a better method is available.
  192. */
  193. #ifdef CONFIG_MMU
  194. copy_page = copy_page_slow;
  195. clear_page = clear_page_slow;
  196. #else
  197. copy_page = copy_page_nommu;
  198. clear_page = clear_page_nommu;
  199. #endif
  200. /* this will put all low memory onto the freelists */
  201. totalram_pages += free_all_bootmem_node(NODE_DATA(0));
  202. reservedpages = 0;
  203. for (tmp = 0; tmp < num_physpages; tmp++)
  204. /*
  205. * Only count reserved RAM pages
  206. */
  207. if (PageReserved(mem_map+tmp))
  208. reservedpages++;
  209. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  210. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  211. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  212. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  213. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  214. VMALLOC_END - VMALLOC_START);
  215. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  216. "%dk reserved, %dk data, %dk init)\n",
  217. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  218. max_mapnr << (PAGE_SHIFT-10),
  219. codesize >> 10,
  220. reservedpages << (PAGE_SHIFT-10),
  221. datasize >> 10,
  222. initsize >> 10);
  223. p3_cache_init();
  224. /* Initialize the vDSO */
  225. vsyscall_init();
  226. }
  227. void free_initmem(void)
  228. {
  229. unsigned long addr;
  230. addr = (unsigned long)(&__init_begin);
  231. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  232. ClearPageReserved(virt_to_page(addr));
  233. init_page_count(virt_to_page(addr));
  234. free_page(addr);
  235. totalram_pages++;
  236. }
  237. printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
  238. }
  239. #ifdef CONFIG_BLK_DEV_INITRD
  240. void free_initrd_mem(unsigned long start, unsigned long end)
  241. {
  242. unsigned long p;
  243. for (p = start; p < end; p += PAGE_SIZE) {
  244. ClearPageReserved(virt_to_page(p));
  245. init_page_count(virt_to_page(p));
  246. free_page(p);
  247. totalram_pages++;
  248. }
  249. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  250. }
  251. #endif