mmu.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * PowerPC memory management structures
  3. *
  4. * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
  5. * PPC64 rework.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef _PPC64_MMU_H_
  13. #define _PPC64_MMU_H_
  14. #include <linux/config.h>
  15. #include <asm/page.h>
  16. /*
  17. * Segment table
  18. */
  19. #define STE_ESID_V 0x80
  20. #define STE_ESID_KS 0x20
  21. #define STE_ESID_KP 0x10
  22. #define STE_ESID_N 0x08
  23. #define STE_VSID_SHIFT 12
  24. /* Location of cpu0's segment table */
  25. #define STAB0_PAGE 0x9
  26. #define STAB0_PHYS_ADDR (STAB0_PAGE<<PAGE_SHIFT)
  27. #define STAB0_VIRT_ADDR (KERNELBASE+STAB0_PHYS_ADDR)
  28. /*
  29. * SLB
  30. */
  31. #define SLB_NUM_BOLTED 3
  32. #define SLB_CACHE_ENTRIES 8
  33. /* Bits in the SLB ESID word */
  34. #define SLB_ESID_V ASM_CONST(0x0000000008000000) /* valid */
  35. /* Bits in the SLB VSID word */
  36. #define SLB_VSID_SHIFT 12
  37. #define SLB_VSID_KS ASM_CONST(0x0000000000000800)
  38. #define SLB_VSID_KP ASM_CONST(0x0000000000000400)
  39. #define SLB_VSID_N ASM_CONST(0x0000000000000200) /* no-execute */
  40. #define SLB_VSID_L ASM_CONST(0x0000000000000100) /* largepage */
  41. #define SLB_VSID_C ASM_CONST(0x0000000000000080) /* class */
  42. #define SLB_VSID_LS ASM_CONST(0x0000000000000070) /* size of largepage */
  43. #define SLB_VSID_KERNEL (SLB_VSID_KP|SLB_VSID_C)
  44. #define SLB_VSID_USER (SLB_VSID_KP|SLB_VSID_KS)
  45. /*
  46. * Hash table
  47. */
  48. #define HPTES_PER_GROUP 8
  49. /* Values for PP (assumes Ks=0, Kp=1) */
  50. /* pp0 will always be 0 for linux */
  51. #define PP_RWXX 0 /* Supervisor read/write, User none */
  52. #define PP_RWRX 1 /* Supervisor read/write, User read */
  53. #define PP_RWRW 2 /* Supervisor read/write, User read/write */
  54. #define PP_RXRX 3 /* Supervisor read, User read */
  55. #ifndef __ASSEMBLY__
  56. /* Hardware Page Table Entry */
  57. typedef struct {
  58. unsigned long avpn:57; /* vsid | api == avpn */
  59. unsigned long : 2; /* Software use */
  60. unsigned long bolted: 1; /* HPTE is "bolted" */
  61. unsigned long lock: 1; /* lock on pSeries SMP */
  62. unsigned long l: 1; /* Virtual page is large (L=1) or 4 KB (L=0) */
  63. unsigned long h: 1; /* Hash function identifier */
  64. unsigned long v: 1; /* Valid (v=1) or invalid (v=0) */
  65. } Hpte_dword0;
  66. typedef struct {
  67. unsigned long pp0: 1; /* Page protection bit 0 */
  68. unsigned long ts: 1; /* Tag set bit */
  69. unsigned long rpn: 50; /* Real page number */
  70. unsigned long : 2; /* Reserved */
  71. unsigned long ac: 1; /* Address compare */
  72. unsigned long r: 1; /* Referenced */
  73. unsigned long c: 1; /* Changed */
  74. unsigned long w: 1; /* Write-thru cache mode */
  75. unsigned long i: 1; /* Cache inhibited */
  76. unsigned long m: 1; /* Memory coherence required */
  77. unsigned long g: 1; /* Guarded */
  78. unsigned long n: 1; /* No-execute */
  79. unsigned long pp: 2; /* Page protection bits 1:2 */
  80. } Hpte_dword1;
  81. typedef struct {
  82. char padding[6]; /* padding */
  83. unsigned long : 6; /* padding */
  84. unsigned long flags: 10; /* HPTE flags */
  85. } Hpte_dword1_flags;
  86. typedef struct {
  87. union {
  88. unsigned long dword0;
  89. Hpte_dword0 dw0;
  90. } dw0;
  91. union {
  92. unsigned long dword1;
  93. Hpte_dword1 dw1;
  94. Hpte_dword1_flags flags;
  95. } dw1;
  96. } HPTE;
  97. extern HPTE * htab_address;
  98. extern unsigned long htab_hash_mask;
  99. static inline unsigned long hpt_hash(unsigned long vpn, int large)
  100. {
  101. unsigned long vsid;
  102. unsigned long page;
  103. if (large) {
  104. vsid = vpn >> 4;
  105. page = vpn & 0xf;
  106. } else {
  107. vsid = vpn >> 16;
  108. page = vpn & 0xffff;
  109. }
  110. return (vsid & 0x7fffffffffUL) ^ page;
  111. }
  112. static inline void __tlbie(unsigned long va, int large)
  113. {
  114. /* clear top 16 bits, non SLS segment */
  115. va &= ~(0xffffULL << 48);
  116. if (large) {
  117. va &= HPAGE_MASK;
  118. asm volatile("tlbie %0,1" : : "r"(va) : "memory");
  119. } else {
  120. va &= PAGE_MASK;
  121. asm volatile("tlbie %0,0" : : "r"(va) : "memory");
  122. }
  123. }
  124. static inline void tlbie(unsigned long va, int large)
  125. {
  126. asm volatile("ptesync": : :"memory");
  127. __tlbie(va, large);
  128. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  129. }
  130. static inline void __tlbiel(unsigned long va)
  131. {
  132. /* clear top 16 bits, non SLS segment */
  133. va &= ~(0xffffULL << 48);
  134. va &= PAGE_MASK;
  135. /*
  136. * Thanks to Alan Modra we are now able to use machine specific
  137. * assembly instructions (like tlbiel) by using the gas -many flag.
  138. * However we have to support older toolchains so for the moment
  139. * we hardwire it.
  140. */
  141. #if 0
  142. asm volatile("tlbiel %0" : : "r"(va) : "memory");
  143. #else
  144. asm volatile(".long 0x7c000224 | (%0 << 11)" : : "r"(va) : "memory");
  145. #endif
  146. }
  147. static inline void tlbiel(unsigned long va)
  148. {
  149. asm volatile("ptesync": : :"memory");
  150. __tlbiel(va);
  151. asm volatile("ptesync": : :"memory");
  152. }
  153. /*
  154. * Handle a fault by adding an HPTE. If the address can't be determined
  155. * to be valid via Linux page tables, return 1. If handled return 0
  156. */
  157. extern int __hash_page(unsigned long ea, unsigned long access,
  158. unsigned long vsid, pte_t *ptep, unsigned long trap,
  159. int local);
  160. extern void htab_finish_init(void);
  161. extern void hpte_init_native(void);
  162. extern void hpte_init_lpar(void);
  163. extern void hpte_init_iSeries(void);
  164. extern long pSeries_lpar_hpte_insert(unsigned long hpte_group,
  165. unsigned long va, unsigned long prpn,
  166. int secondary, unsigned long hpteflags,
  167. int bolted, int large);
  168. extern long native_hpte_insert(unsigned long hpte_group, unsigned long va,
  169. unsigned long prpn, int secondary,
  170. unsigned long hpteflags, int bolted, int large);
  171. #endif /* __ASSEMBLY__ */
  172. /*
  173. * VSID allocation
  174. *
  175. * We first generate a 36-bit "proto-VSID". For kernel addresses this
  176. * is equal to the ESID, for user addresses it is:
  177. * (context << 15) | (esid & 0x7fff)
  178. *
  179. * The two forms are distinguishable because the top bit is 0 for user
  180. * addresses, whereas the top two bits are 1 for kernel addresses.
  181. * Proto-VSIDs with the top two bits equal to 0b10 are reserved for
  182. * now.
  183. *
  184. * The proto-VSIDs are then scrambled into real VSIDs with the
  185. * multiplicative hash:
  186. *
  187. * VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
  188. * where VSID_MULTIPLIER = 268435399 = 0xFFFFFC7
  189. * VSID_MODULUS = 2^36-1 = 0xFFFFFFFFF
  190. *
  191. * This scramble is only well defined for proto-VSIDs below
  192. * 0xFFFFFFFFF, so both proto-VSID and actual VSID 0xFFFFFFFFF are
  193. * reserved. VSID_MULTIPLIER is prime, so in particular it is
  194. * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
  195. * Because the modulus is 2^n-1 we can compute it efficiently without
  196. * a divide or extra multiply (see below).
  197. *
  198. * This scheme has several advantages over older methods:
  199. *
  200. * - We have VSIDs allocated for every kernel address
  201. * (i.e. everything above 0xC000000000000000), except the very top
  202. * segment, which simplifies several things.
  203. *
  204. * - We allow for 15 significant bits of ESID and 20 bits of
  205. * context for user addresses. i.e. 8T (43 bits) of address space for
  206. * up to 1M contexts (although the page table structure and context
  207. * allocation will need changes to take advantage of this).
  208. *
  209. * - The scramble function gives robust scattering in the hash
  210. * table (at least based on some initial results). The previous
  211. * method was more susceptible to pathological cases giving excessive
  212. * hash collisions.
  213. */
  214. /*
  215. * WARNING - If you change these you must make sure the asm
  216. * implementations in slb_allocate (slb_low.S), do_stab_bolted
  217. * (head.S) and ASM_VSID_SCRAMBLE (below) are changed accordingly.
  218. *
  219. * You'll also need to change the precomputed VSID values in head.S
  220. * which are used by the iSeries firmware.
  221. */
  222. #define VSID_MULTIPLIER ASM_CONST(200730139) /* 28-bit prime */
  223. #define VSID_BITS 36
  224. #define VSID_MODULUS ((1UL<<VSID_BITS)-1)
  225. #define CONTEXT_BITS 20
  226. #define USER_ESID_BITS 15
  227. /*
  228. * This macro generates asm code to compute the VSID scramble
  229. * function. Used in slb_allocate() and do_stab_bolted. The function
  230. * computed is: (protovsid*VSID_MULTIPLIER) % VSID_MODULUS
  231. *
  232. * rt = register continaing the proto-VSID and into which the
  233. * VSID will be stored
  234. * rx = scratch register (clobbered)
  235. *
  236. * - rt and rx must be different registers
  237. * - The answer will end up in the low 36 bits of rt. The higher
  238. * bits may contain other garbage, so you may need to mask the
  239. * result.
  240. */
  241. #define ASM_VSID_SCRAMBLE(rt, rx) \
  242. lis rx,VSID_MULTIPLIER@h; \
  243. ori rx,rx,VSID_MULTIPLIER@l; \
  244. mulld rt,rt,rx; /* rt = rt * MULTIPLIER */ \
  245. \
  246. srdi rx,rt,VSID_BITS; \
  247. clrldi rt,rt,(64-VSID_BITS); \
  248. add rt,rt,rx; /* add high and low bits */ \
  249. /* Now, r3 == VSID (mod 2^36-1), and lies between 0 and \
  250. * 2^36-1+2^28-1. That in particular means that if r3 >= \
  251. * 2^36-1, then r3+1 has the 2^36 bit set. So, if r3+1 has \
  252. * the bit clear, r3 already has the answer we want, if it \
  253. * doesn't, the answer is the low 36 bits of r3+1. So in all \
  254. * cases the answer is the low 36 bits of (r3 + ((r3+1) >> 36))*/\
  255. addi rx,rt,1; \
  256. srdi rx,rx,VSID_BITS; /* extract 2^36 bit */ \
  257. add rt,rt,rx
  258. #ifndef __ASSEMBLY__
  259. typedef unsigned long mm_context_id_t;
  260. typedef struct {
  261. mm_context_id_t id;
  262. #ifdef CONFIG_HUGETLB_PAGE
  263. pgd_t *huge_pgdir;
  264. u16 htlb_segs; /* bitmask */
  265. #endif
  266. } mm_context_t;
  267. static inline unsigned long vsid_scramble(unsigned long protovsid)
  268. {
  269. #if 0
  270. /* The code below is equivalent to this function for arguments
  271. * < 2^VSID_BITS, which is all this should ever be called
  272. * with. However gcc is not clever enough to compute the
  273. * modulus (2^n-1) without a second multiply. */
  274. return ((protovsid * VSID_MULTIPLIER) % VSID_MODULUS);
  275. #else /* 1 */
  276. unsigned long x;
  277. x = protovsid * VSID_MULTIPLIER;
  278. x = (x >> VSID_BITS) + (x & VSID_MODULUS);
  279. return (x + ((x+1) >> VSID_BITS)) & VSID_MODULUS;
  280. #endif /* 1 */
  281. }
  282. /* This is only valid for addresses >= KERNELBASE */
  283. static inline unsigned long get_kernel_vsid(unsigned long ea)
  284. {
  285. return vsid_scramble(ea >> SID_SHIFT);
  286. }
  287. /* This is only valid for user addresses (which are below 2^41) */
  288. static inline unsigned long get_vsid(unsigned long context, unsigned long ea)
  289. {
  290. return vsid_scramble((context << USER_ESID_BITS)
  291. | (ea >> SID_SHIFT));
  292. }
  293. #endif /* __ASSEMBLY */
  294. #endif /* _PPC64_MMU_H_ */