init.c 8.2 KB

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