ppc_mmu.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:8k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * BK Id: %F% %I% %G% %U% %#%
  3.  */
  4. /*
  5.  * This file contains the routines for handling the MMU on those
  6.  * PowerPC implementations where the MMU substantially follows the
  7.  * architecture specification.  This includes the 6xx, 7xx, 7xxx,
  8.  * 8260, and POWER3 implementations but excludes the 8xx and 4xx.
  9.  * Although the iSeries hardware does comply with the architecture
  10.  * specification, the need to work through the hypervisor makes
  11.  * things sufficiently different that it is handled elsewhere.
  12.  *  -- paulus
  13.  * 
  14.  *  Derived from arch/ppc/mm/init.c:
  15.  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  16.  *
  17.  *  Modifications by Paul Mackerras (PowerMac) (paulus@cs.anu.edu.au)
  18.  *  and Cort Dougan (PReP) (cort@cs.nmt.edu)
  19.  *    Copyright (C) 1996 Paul Mackerras
  20.  *  Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  21.  *
  22.  *  Derived from "arch/i386/mm/init.c"
  23.  *    Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
  24.  *
  25.  *  This program is free software; you can redistribute it and/or
  26.  *  modify it under the terms of the GNU General Public License
  27.  *  as published by the Free Software Foundation; either version
  28.  *  2 of the License, or (at your option) any later version.
  29.  *
  30.  */
  31. #include <linux/config.h>
  32. #include <linux/kernel.h>
  33. #include <linux/mm.h>
  34. #include <linux/init.h>
  35. #include <asm/prom.h>
  36. #include <asm/mmu.h>
  37. #include <asm/machdep.h>
  38. #include "mmu_decl.h"
  39. #include "mem_pieces.h"
  40. PTE *Hash, *Hash_end;
  41. unsigned long Hash_size, Hash_mask;
  42. unsigned long _SDR1;
  43. union ubat { /* BAT register values to be loaded */
  44. BAT bat;
  45. #ifdef CONFIG_PPC64BRIDGE
  46. u64 word[2];
  47. #else
  48. u32 word[2];
  49. #endif
  50. } BATS[4][2]; /* 4 pairs of IBAT, DBAT */
  51. struct batrange { /* stores address ranges mapped by BATs */
  52. unsigned long start;
  53. unsigned long limit;
  54. unsigned long phys;
  55. } bat_addrs[4];
  56. /*
  57.  * Return PA for this VA if it is mapped by a BAT, or 0
  58.  */
  59. unsigned long v_mapped_by_bats(unsigned long va)
  60. {
  61. int b;
  62. for (b = 0; b < 4; ++b)
  63. if (va >= bat_addrs[b].start && va < bat_addrs[b].limit)
  64. return bat_addrs[b].phys + (va - bat_addrs[b].start);
  65. return 0;
  66. }
  67. /*
  68.  * Return VA for a given PA or 0 if not mapped
  69.  */
  70. unsigned long p_mapped_by_bats(unsigned long pa)
  71. {
  72. int b;
  73. for (b = 0; b < 4; ++b)
  74. if (pa >= bat_addrs[b].phys
  75.          && pa < (bat_addrs[b].limit-bat_addrs[b].start)
  76.               +bat_addrs[b].phys)
  77. return bat_addrs[b].start+(pa-bat_addrs[b].phys);
  78. return 0;
  79. }
  80. void __init bat_mapin_ram(unsigned long bat2, unsigned long bat3)
  81. {
  82. unsigned long tot, done;
  83. tot = total_lowmem;
  84. setbat(2, KERNELBASE, ram_phys_base, bat2, _PAGE_KERNEL);
  85. done = (unsigned long)bat_addrs[2].limit - KERNELBASE + 1;
  86. if ((done < tot) && !bat_addrs[3].limit && bat3) {
  87. tot -= done;
  88. setbat(3, KERNELBASE+done, ram_phys_base+done, bat3, 
  89.        _PAGE_KERNEL);
  90. }
  91. }
  92. /*
  93.  * Set up one of the I/D BAT (block address translation) register pairs.
  94.  * The parameters are not checked; in particular size must be a power
  95.  * of 2 between 128k and 256M.
  96.  */
  97. void __init setbat(int index, unsigned long virt, unsigned long phys,
  98.    unsigned int size, int flags)
  99. {
  100. unsigned int bl;
  101. int wimgxpp;
  102. union ubat *bat = BATS[index];
  103. #ifdef CONFIG_SMP
  104. if ((flags & _PAGE_NO_CACHE) == 0)
  105. flags |= _PAGE_COHERENT;
  106. #endif
  107. bl = (size >> 17) - 1;
  108. if (PVR_VER(mfspr(PVR)) != 1) {
  109. /* 603, 604, etc. */
  110. /* Do DBAT first */
  111. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  112.    | _PAGE_COHERENT | _PAGE_GUARDED);
  113. wimgxpp |= (flags & _PAGE_RW)? BPP_RW: BPP_RX;
  114. bat[1].word[0] = virt | (bl << 2) | 2; /* Vs=1, Vp=0 */
  115. bat[1].word[1] = phys | wimgxpp;
  116. #ifndef CONFIG_KGDB /* want user access for breakpoints */
  117. if (flags & _PAGE_USER)
  118. #endif
  119. bat[1].bat.batu.vp = 1;
  120. if (flags & _PAGE_GUARDED) {
  121. /* G bit must be zero in IBATs */
  122. bat[0].word[0] = bat[0].word[1] = 0;
  123. } else {
  124. /* make IBAT same as DBAT */
  125. bat[0] = bat[1];
  126. }
  127. } else {
  128. /* 601 cpu */
  129. if (bl > BL_8M)
  130. bl = BL_8M;
  131. wimgxpp = flags & (_PAGE_WRITETHRU | _PAGE_NO_CACHE
  132.    | _PAGE_COHERENT);
  133. wimgxpp |= (flags & _PAGE_RW)?
  134. ((flags & _PAGE_USER)? PP_RWRW: PP_RWXX): PP_RXRX;
  135. bat->word[0] = virt | wimgxpp | 4; /* Ks=0, Ku=1 */
  136. bat->word[1] = phys | bl | 0x40; /* V=1 */
  137. }
  138. bat_addrs[index].start = virt;
  139. bat_addrs[index].limit = virt + ((bl + 1) << 17) - 1;
  140. bat_addrs[index].phys = phys;
  141. }
  142. /*
  143.  * Initialize the hash table and patch the instructions in hashtable.S.
  144.  */
  145. void __init MMU_init_hw(void)
  146. {
  147. unsigned int hmask, mb, mb2;
  148. unsigned int n_hpteg, lg_n_hpteg;
  149. extern unsigned int hash_page_patch_A[];
  150. extern unsigned int hash_page_patch_B[], hash_page_patch_C[];
  151. extern unsigned int flush_hash_patch_A[], flush_hash_patch_B[];
  152. if ((cur_cpu_spec[0]->cpu_features & CPU_FTR_HPTE_TABLE) == 0) {
  153. Hash_size = 0;
  154. Hash_end = 0;
  155. Hash = 0;
  156. return;
  157. }
  158. if ( ppc_md.progress ) ppc_md.progress("hash:enter", 0x105);
  159. #ifdef CONFIG_PPC64BRIDGE
  160. #define LG_HPTEG_SIZE 7 /* 128 bytes per HPTEG */
  161. #define SDR1_LOW_BITS (lg_n_hpteg - 11)
  162. #define MIN_N_HPTEG 2048 /* min 256kB hash table */
  163. #else
  164. #define LG_HPTEG_SIZE 6 /* 64 bytes per HPTEG */
  165. #define SDR1_LOW_BITS ((n_hpteg - 1) >> 10)
  166. #define MIN_N_HPTEG 1024 /* min 64kB hash table */
  167. #endif
  168. /*
  169.  * Allow 1 HPTE (1/8 HPTEG) for each page of memory.
  170.  * This is less than the recommended amount, but then
  171.  * Linux ain't AIX.
  172.  */
  173. n_hpteg = total_memory / (PAGE_SIZE * 8);
  174. if (n_hpteg < MIN_N_HPTEG)
  175. n_hpteg = MIN_N_HPTEG;
  176. lg_n_hpteg = __ilog2(n_hpteg);
  177. if (n_hpteg & (n_hpteg - 1)) {
  178. ++lg_n_hpteg; /* round up if not power of 2 */
  179. n_hpteg = 1 << lg_n_hpteg;
  180. }
  181. Hash_size = n_hpteg << LG_HPTEG_SIZE;
  182. /*
  183.  * Find some memory for the hash table.
  184.  */
  185. if ( ppc_md.progress ) ppc_md.progress("hash:find piece", 0x322);
  186. Hash = mem_pieces_find(Hash_size, Hash_size);
  187. cacheable_memzero(Hash, Hash_size);
  188. _SDR1 = __pa(Hash) | SDR1_LOW_BITS;
  189. Hash_end = (PTE *) ((unsigned long)Hash + Hash_size);
  190. printk("Total memory = %ldMB; using %ldkB for hash table (at %p)n",
  191.        total_memory >> 20, Hash_size >> 10, Hash);
  192. /*
  193.  * Patch up the instructions in hashtable.S:create_hpte
  194.  */
  195. if ( ppc_md.progress ) ppc_md.progress("hash:patch", 0x345);
  196. Hash_mask = n_hpteg - 1;
  197. hmask = Hash_mask >> (16 - LG_HPTEG_SIZE);
  198. mb2 = mb = 32 - LG_HPTEG_SIZE - lg_n_hpteg;
  199. if (lg_n_hpteg > 16)
  200. mb2 = 16 - LG_HPTEG_SIZE;
  201. hash_page_patch_A[0] = (hash_page_patch_A[0] & ~0xffff)
  202. | ((unsigned int)(Hash) >> 16);
  203. hash_page_patch_A[1] = (hash_page_patch_A[1] & ~0x7c0) | (mb << 6);
  204. hash_page_patch_A[2] = (hash_page_patch_A[2] & ~0x7c0) | (mb2 << 6);
  205. hash_page_patch_B[0] = (hash_page_patch_B[0] & ~0xffff) | hmask;
  206. hash_page_patch_C[0] = (hash_page_patch_C[0] & ~0xffff) | hmask;
  207. /*
  208.  * Ensure that the locations we've patched have been written
  209.  * out from the data cache and invalidated in the instruction
  210.  * cache, on those machines with split caches.
  211.  */
  212. flush_icache_range((unsigned long) &hash_page_patch_A[0],
  213.    (unsigned long) &hash_page_patch_C[1]);
  214. /*
  215.  * Patch up the instructions in hashtable.S:flush_hash_page
  216.  */
  217. flush_hash_patch_A[0] = (flush_hash_patch_A[0] & ~0xffff)
  218. | ((unsigned int)(Hash) >> 16);
  219. flush_hash_patch_A[1] = (flush_hash_patch_A[1] & ~0x7c0) | (mb << 6);
  220. flush_hash_patch_A[2] = (flush_hash_patch_A[2] & ~0x7c0) | (mb2 << 6);
  221. flush_hash_patch_B[0] = (flush_hash_patch_B[0] & ~0xffff) | hmask;
  222. flush_icache_range((unsigned long) &flush_hash_patch_A[0],
  223.    (unsigned long) &flush_hash_patch_B[1]);
  224. if ( ppc_md.progress ) ppc_md.progress("hash:done", 0x205);
  225. }
  226. /*
  227.  * This is called at the end of handling a user page fault, when the
  228.  * fault has been handled by updating a PTE in the linux page tables.
  229.  * We use it to preload an HPTE into the hash table corresponding to
  230.  * the updated linux PTE.
  231.  */
  232. void update_mmu_cache(struct vm_area_struct *vma, unsigned long address,
  233.       pte_t pte)
  234. {
  235. struct mm_struct *mm;
  236. pmd_t *pmd;
  237. pte_t *ptep;
  238. static int nopreload;
  239. if (Hash == 0 || nopreload)
  240. return;
  241. /* We only want HPTEs for linux PTEs that have _PAGE_ACCESSED set */
  242. if (!pte_young(pte))
  243. return;
  244. mm = (address < TASK_SIZE)? vma->vm_mm: &init_mm;
  245. pmd = pmd_offset(pgd_offset(mm, address), address);
  246. if (!pmd_none(*pmd)) {
  247. ptep = pte_offset(pmd, address);
  248. add_hash_page(mm->context, address, ptep);
  249. }
  250. }