init.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. /*
  40. * Cache of MMU context last used.
  41. */
  42. unsigned long mmu_context_cache = NO_CONTEXT;
  43. #ifdef CONFIG_MMU
  44. /* It'd be good if these lines were in the standard header file. */
  45. #define START_PFN (NODE_DATA(0)->bdata->node_boot_start >> PAGE_SHIFT)
  46. #define MAX_LOW_PFN (NODE_DATA(0)->bdata->node_low_pfn)
  47. #endif
  48. void (*copy_page)(void *from, void *to);
  49. void (*clear_page)(void *to);
  50. void show_mem(void)
  51. {
  52. int i, total = 0, reserved = 0;
  53. int shared = 0, cached = 0;
  54. printk("Mem-info:\n");
  55. show_free_areas();
  56. printk("Free swap: %6ldkB\n", nr_swap_pages<<(PAGE_SHIFT-10));
  57. i = max_mapnr;
  58. while (i-- > 0) {
  59. total++;
  60. if (PageReserved(mem_map+i))
  61. reserved++;
  62. else if (PageSwapCache(mem_map+i))
  63. cached++;
  64. else if (page_count(mem_map+i))
  65. shared += page_count(mem_map+i) - 1;
  66. }
  67. printk("%d pages of RAM\n",total);
  68. printk("%d reserved pages\n",reserved);
  69. printk("%d pages shared\n",shared);
  70. printk("%d pages swap cached\n",cached);
  71. }
  72. static void set_pte_phys(unsigned long addr, unsigned long phys, pgprot_t prot)
  73. {
  74. pgd_t *pgd;
  75. pud_t *pud;
  76. pmd_t *pmd;
  77. pte_t *pte;
  78. pgd = swapper_pg_dir + pgd_index(addr);
  79. if (pgd_none(*pgd)) {
  80. pgd_ERROR(*pgd);
  81. return;
  82. }
  83. pud = pud_offset(pgd, addr);
  84. if (pud_none(*pud)) {
  85. pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC);
  86. set_pud(pud, __pud(__pa(pmd) | _KERNPG_TABLE | _PAGE_USER));
  87. if (pmd != pmd_offset(pud, 0)) {
  88. pud_ERROR(*pud);
  89. return;
  90. }
  91. }
  92. pmd = pmd_offset(pud, addr);
  93. if (pmd_none(*pmd)) {
  94. pte = (pte_t *)get_zeroed_page(GFP_ATOMIC);
  95. set_pmd(pmd, __pmd(__pa(pte) | _KERNPG_TABLE | _PAGE_USER));
  96. if (pte != pte_offset_kernel(pmd, 0)) {
  97. pmd_ERROR(*pmd);
  98. return;
  99. }
  100. }
  101. pte = pte_offset_kernel(pmd, addr);
  102. if (!pte_none(*pte)) {
  103. pte_ERROR(*pte);
  104. return;
  105. }
  106. set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, prot));
  107. __flush_tlb_page(get_asid(), addr);
  108. }
  109. /*
  110. * As a performance optimization, other platforms preserve the fixmap mapping
  111. * across a context switch, we don't presently do this, but this could be done
  112. * in a similar fashion as to the wired TLB interface that sh64 uses (by way
  113. * of the memorry mapped UTLB configuration) -- this unfortunately forces us to
  114. * give up a TLB entry for each mapping we want to preserve. While this may be
  115. * viable for a small number of fixmaps, it's not particularly useful for
  116. * everything and needs to be carefully evaluated. (ie, we may want this for
  117. * the vsyscall page).
  118. *
  119. * XXX: Perhaps add a _PAGE_WIRED flag or something similar that we can pass
  120. * in at __set_fixmap() time to determine the appropriate behavior to follow.
  121. *
  122. * -- PFM.
  123. */
  124. void __set_fixmap(enum fixed_addresses idx, unsigned long phys, pgprot_t prot)
  125. {
  126. unsigned long address = __fix_to_virt(idx);
  127. if (idx >= __end_of_fixed_addresses) {
  128. BUG();
  129. return;
  130. }
  131. set_pte_phys(address, phys, prot);
  132. }
  133. /* References to section boundaries */
  134. extern char _text, _etext, _edata, __bss_start, _end;
  135. extern char __init_begin, __init_end;
  136. /*
  137. * paging_init() sets up the page tables
  138. *
  139. * This routines also unmaps the page at virtual kernel address 0, so
  140. * that we can trap those pesky NULL-reference errors in the kernel.
  141. */
  142. void __init paging_init(void)
  143. {
  144. unsigned long zones_size[MAX_NR_ZONES] = { 0, };
  145. /*
  146. * Setup some defaults for the zone sizes.. these should be safe
  147. * regardless of distcontiguous memory or MMU settings.
  148. */
  149. zones_size[ZONE_DMA] = 0 >> PAGE_SHIFT;
  150. zones_size[ZONE_NORMAL] = __MEMORY_SIZE >> PAGE_SHIFT;
  151. #ifdef CONFIG_HIGHMEM
  152. zones_size[ZONE_HIGHMEM] = 0 >> PAGE_SHIFT;
  153. #endif
  154. #ifdef CONFIG_MMU
  155. /*
  156. * If we have an MMU, and want to be using it .. we need to adjust
  157. * the zone sizes accordingly, in addition to turning it on.
  158. */
  159. {
  160. unsigned long max_dma, low, start_pfn;
  161. pgd_t *pg_dir;
  162. int i;
  163. /* We don't need kernel mapping as hardware support that. */
  164. pg_dir = swapper_pg_dir;
  165. for (i = 0; i < PTRS_PER_PGD; i++)
  166. pgd_val(pg_dir[i]) = 0;
  167. /* Turn on the MMU */
  168. enable_mmu();
  169. /* Fixup the zone sizes */
  170. start_pfn = START_PFN;
  171. max_dma = virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
  172. low = MAX_LOW_PFN;
  173. if (low < max_dma) {
  174. zones_size[ZONE_DMA] = low - start_pfn;
  175. zones_size[ZONE_NORMAL] = 0;
  176. } else {
  177. zones_size[ZONE_DMA] = max_dma - start_pfn;
  178. zones_size[ZONE_NORMAL] = low - max_dma;
  179. }
  180. }
  181. #elif defined(CONFIG_CPU_SH3) || defined(CONFIG_CPU_SH4)
  182. /*
  183. * If we don't have CONFIG_MMU set and the processor in question
  184. * still has an MMU, care needs to be taken to make sure it doesn't
  185. * stay on.. Since the boot loader could have potentially already
  186. * turned it on, and we clearly don't want it, we simply turn it off.
  187. *
  188. * We don't need to do anything special for the zone sizes, since the
  189. * default values that were already configured up above should be
  190. * satisfactory.
  191. */
  192. disable_mmu();
  193. #endif
  194. NODE_DATA(0)->node_mem_map = NULL;
  195. free_area_init_node(0, NODE_DATA(0), zones_size, __MEMORY_START >> PAGE_SHIFT, 0);
  196. }
  197. static struct kcore_list kcore_mem, kcore_vmalloc;
  198. void __init mem_init(void)
  199. {
  200. extern unsigned long empty_zero_page[1024];
  201. int codesize, reservedpages, datasize, initsize;
  202. int tmp;
  203. extern unsigned long memory_start;
  204. #ifdef CONFIG_MMU
  205. high_memory = (void *)__va(MAX_LOW_PFN * PAGE_SIZE);
  206. #else
  207. extern unsigned long memory_end;
  208. high_memory = (void *)(memory_end & PAGE_MASK);
  209. #endif
  210. max_mapnr = num_physpages = MAP_NR(high_memory) - MAP_NR(memory_start);
  211. /* clear the zero-page */
  212. memset(empty_zero_page, 0, PAGE_SIZE);
  213. __flush_wback_region(empty_zero_page, PAGE_SIZE);
  214. /*
  215. * Setup wrappers for copy/clear_page(), these will get overridden
  216. * later in the boot process if a better method is available.
  217. */
  218. #ifdef CONFIG_MMU
  219. copy_page = copy_page_slow;
  220. clear_page = clear_page_slow;
  221. #else
  222. copy_page = copy_page_nommu;
  223. clear_page = clear_page_nommu;
  224. #endif
  225. /* this will put all low memory onto the freelists */
  226. totalram_pages += free_all_bootmem_node(NODE_DATA(0));
  227. reservedpages = 0;
  228. for (tmp = 0; tmp < num_physpages; tmp++)
  229. /*
  230. * Only count reserved RAM pages
  231. */
  232. if (PageReserved(mem_map+tmp))
  233. reservedpages++;
  234. codesize = (unsigned long) &_etext - (unsigned long) &_text;
  235. datasize = (unsigned long) &_edata - (unsigned long) &_etext;
  236. initsize = (unsigned long) &__init_end - (unsigned long) &__init_begin;
  237. kclist_add(&kcore_mem, __va(0), max_low_pfn << PAGE_SHIFT);
  238. kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
  239. VMALLOC_END - VMALLOC_START);
  240. printk(KERN_INFO "Memory: %luk/%luk available (%dk kernel code, "
  241. "%dk reserved, %dk data, %dk init)\n",
  242. (unsigned long) nr_free_pages() << (PAGE_SHIFT-10),
  243. max_mapnr << (PAGE_SHIFT-10),
  244. codesize >> 10,
  245. reservedpages << (PAGE_SHIFT-10),
  246. datasize >> 10,
  247. initsize >> 10);
  248. p3_cache_init();
  249. /* Initialize the vDSO */
  250. vsyscall_init();
  251. }
  252. void free_initmem(void)
  253. {
  254. unsigned long addr;
  255. addr = (unsigned long)(&__init_begin);
  256. for (; addr < (unsigned long)(&__init_end); addr += PAGE_SIZE) {
  257. ClearPageReserved(virt_to_page(addr));
  258. init_page_count(virt_to_page(addr));
  259. free_page(addr);
  260. totalram_pages++;
  261. }
  262. printk ("Freeing unused kernel memory: %dk freed\n", (&__init_end - &__init_begin) >> 10);
  263. }
  264. #ifdef CONFIG_BLK_DEV_INITRD
  265. void free_initrd_mem(unsigned long start, unsigned long end)
  266. {
  267. unsigned long p;
  268. for (p = start; p < end; p += PAGE_SIZE) {
  269. ClearPageReserved(virt_to_page(p));
  270. init_page_count(virt_to_page(p));
  271. free_page(p);
  272. totalram_pages++;
  273. }
  274. printk ("Freeing initrd memory: %ldk freed\n", (end - start) >> 10);
  275. }
  276. #endif