mmu.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 0x6
  26. #define STAB0_PHYS_ADDR (STAB0_PAGE<<PAGE_SHIFT)
  27. #ifndef __ASSEMBLY__
  28. extern char initial_stab[];
  29. #endif /* ! __ASSEMBLY */
  30. /*
  31. * SLB
  32. */
  33. #define SLB_NUM_BOLTED 3
  34. #define SLB_CACHE_ENTRIES 8
  35. /* Bits in the SLB ESID word */
  36. #define SLB_ESID_V ASM_CONST(0x0000000008000000) /* valid */
  37. /* Bits in the SLB VSID word */
  38. #define SLB_VSID_SHIFT 12
  39. #define SLB_VSID_KS ASM_CONST(0x0000000000000800)
  40. #define SLB_VSID_KP ASM_CONST(0x0000000000000400)
  41. #define SLB_VSID_N ASM_CONST(0x0000000000000200) /* no-execute */
  42. #define SLB_VSID_L ASM_CONST(0x0000000000000100) /* largepage */
  43. #define SLB_VSID_C ASM_CONST(0x0000000000000080) /* class */
  44. #define SLB_VSID_LS ASM_CONST(0x0000000000000070) /* size of largepage */
  45. #define SLB_VSID_KERNEL (SLB_VSID_KP)
  46. #define SLB_VSID_USER (SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
  47. #define SLBIE_C (0x08000000)
  48. /*
  49. * Hash table
  50. */
  51. #define HPTES_PER_GROUP 8
  52. #define HPTE_V_AVPN_SHIFT 7
  53. #define HPTE_V_AVPN ASM_CONST(0xffffffffffffff80)
  54. #define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
  55. #define HPTE_V_BOLTED ASM_CONST(0x0000000000000010)
  56. #define HPTE_V_LOCK ASM_CONST(0x0000000000000008)
  57. #define HPTE_V_LARGE ASM_CONST(0x0000000000000004)
  58. #define HPTE_V_SECONDARY ASM_CONST(0x0000000000000002)
  59. #define HPTE_V_VALID ASM_CONST(0x0000000000000001)
  60. #define HPTE_R_PP0 ASM_CONST(0x8000000000000000)
  61. #define HPTE_R_TS ASM_CONST(0x4000000000000000)
  62. #define HPTE_R_RPN_SHIFT 12
  63. #define HPTE_R_RPN ASM_CONST(0x3ffffffffffff000)
  64. #define HPTE_R_FLAGS ASM_CONST(0x00000000000003ff)
  65. #define HPTE_R_PP ASM_CONST(0x0000000000000003)
  66. /* Values for PP (assumes Ks=0, Kp=1) */
  67. /* pp0 will always be 0 for linux */
  68. #define PP_RWXX 0 /* Supervisor read/write, User none */
  69. #define PP_RWRX 1 /* Supervisor read/write, User read */
  70. #define PP_RWRW 2 /* Supervisor read/write, User read/write */
  71. #define PP_RXRX 3 /* Supervisor read, User read */
  72. #ifndef __ASSEMBLY__
  73. typedef struct {
  74. unsigned long v;
  75. unsigned long r;
  76. } hpte_t;
  77. extern hpte_t *htab_address;
  78. extern unsigned long htab_hash_mask;
  79. static inline unsigned long hpt_hash(unsigned long vpn, int large)
  80. {
  81. unsigned long vsid;
  82. unsigned long page;
  83. if (large) {
  84. vsid = vpn >> 4;
  85. page = vpn & 0xf;
  86. } else {
  87. vsid = vpn >> 16;
  88. page = vpn & 0xffff;
  89. }
  90. return (vsid & 0x7fffffffffUL) ^ page;
  91. }
  92. static inline void __tlbie(unsigned long va, int large)
  93. {
  94. /* clear top 16 bits, non SLS segment */
  95. va &= ~(0xffffULL << 48);
  96. if (large) {
  97. va &= HPAGE_MASK;
  98. asm volatile("tlbie %0,1" : : "r"(va) : "memory");
  99. } else {
  100. va &= PAGE_MASK;
  101. asm volatile("tlbie %0,0" : : "r"(va) : "memory");
  102. }
  103. }
  104. static inline void tlbie(unsigned long va, int large)
  105. {
  106. asm volatile("ptesync": : :"memory");
  107. __tlbie(va, large);
  108. asm volatile("eieio; tlbsync; ptesync": : :"memory");
  109. }
  110. static inline void __tlbiel(unsigned long va)
  111. {
  112. /* clear top 16 bits, non SLS segment */
  113. va &= ~(0xffffULL << 48);
  114. va &= PAGE_MASK;
  115. /*
  116. * Thanks to Alan Modra we are now able to use machine specific
  117. * assembly instructions (like tlbiel) by using the gas -many flag.
  118. * However we have to support older toolchains so for the moment
  119. * we hardwire it.
  120. */
  121. #if 0
  122. asm volatile("tlbiel %0" : : "r"(va) : "memory");
  123. #else
  124. asm volatile(".long 0x7c000224 | (%0 << 11)" : : "r"(va) : "memory");
  125. #endif
  126. }
  127. static inline void tlbiel(unsigned long va)
  128. {
  129. asm volatile("ptesync": : :"memory");
  130. __tlbiel(va);
  131. asm volatile("ptesync": : :"memory");
  132. }
  133. static inline unsigned long slot2va(unsigned long hpte_v, unsigned long slot)
  134. {
  135. unsigned long avpn = HPTE_V_AVPN_VAL(hpte_v);
  136. unsigned long va;
  137. va = avpn << 23;
  138. if (! (hpte_v & HPTE_V_LARGE)) {
  139. unsigned long vpi, pteg;
  140. pteg = slot / HPTES_PER_GROUP;
  141. if (hpte_v & HPTE_V_SECONDARY)
  142. pteg = ~pteg;
  143. vpi = ((va >> 28) ^ pteg) & htab_hash_mask;
  144. va |= vpi << PAGE_SHIFT;
  145. }
  146. return va;
  147. }
  148. /*
  149. * Handle a fault by adding an HPTE. If the address can't be determined
  150. * to be valid via Linux page tables, return 1. If handled return 0
  151. */
  152. extern int __hash_page(unsigned long ea, unsigned long access,
  153. unsigned long vsid, pte_t *ptep, unsigned long trap,
  154. int local);
  155. extern void htab_finish_init(void);
  156. extern void hpte_init_native(void);
  157. extern void hpte_init_lpar(void);
  158. extern void hpte_init_iSeries(void);
  159. extern long pSeries_lpar_hpte_insert(unsigned long hpte_group,
  160. unsigned long va, unsigned long prpn,
  161. unsigned long vflags,
  162. unsigned long rflags);
  163. extern long native_hpte_insert(unsigned long hpte_group, unsigned long va,
  164. unsigned long prpn,
  165. unsigned long vflags, unsigned long rflags);
  166. extern void stabs_alloc(void);
  167. #endif /* __ASSEMBLY__ */
  168. /*
  169. * VSID allocation
  170. *
  171. * We first generate a 36-bit "proto-VSID". For kernel addresses this
  172. * is equal to the ESID, for user addresses it is:
  173. * (context << 15) | (esid & 0x7fff)
  174. *
  175. * The two forms are distinguishable because the top bit is 0 for user
  176. * addresses, whereas the top two bits are 1 for kernel addresses.
  177. * Proto-VSIDs with the top two bits equal to 0b10 are reserved for
  178. * now.
  179. *
  180. * The proto-VSIDs are then scrambled into real VSIDs with the
  181. * multiplicative hash:
  182. *
  183. * VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
  184. * where VSID_MULTIPLIER = 268435399 = 0xFFFFFC7
  185. * VSID_MODULUS = 2^36-1 = 0xFFFFFFFFF
  186. *
  187. * This scramble is only well defined for proto-VSIDs below
  188. * 0xFFFFFFFFF, so both proto-VSID and actual VSID 0xFFFFFFFFF are
  189. * reserved. VSID_MULTIPLIER is prime, so in particular it is
  190. * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
  191. * Because the modulus is 2^n-1 we can compute it efficiently without
  192. * a divide or extra multiply (see below).
  193. *
  194. * This scheme has several advantages over older methods:
  195. *
  196. * - We have VSIDs allocated for every kernel address
  197. * (i.e. everything above 0xC000000000000000), except the very top
  198. * segment, which simplifies several things.
  199. *
  200. * - We allow for 15 significant bits of ESID and 20 bits of
  201. * context for user addresses. i.e. 8T (43 bits) of address space for
  202. * up to 1M contexts (although the page table structure and context
  203. * allocation will need changes to take advantage of this).
  204. *
  205. * - The scramble function gives robust scattering in the hash
  206. * table (at least based on some initial results). The previous
  207. * method was more susceptible to pathological cases giving excessive
  208. * hash collisions.
  209. */
  210. /*
  211. * WARNING - If you change these you must make sure the asm
  212. * implementations in slb_allocate (slb_low.S), do_stab_bolted
  213. * (head.S) and ASM_VSID_SCRAMBLE (below) are changed accordingly.
  214. *
  215. * You'll also need to change the precomputed VSID values in head.S
  216. * which are used by the iSeries firmware.
  217. */
  218. #define VSID_MULTIPLIER ASM_CONST(200730139) /* 28-bit prime */
  219. #define VSID_BITS 36
  220. #define VSID_MODULUS ((1UL<<VSID_BITS)-1)
  221. #define CONTEXT_BITS 19
  222. #define USER_ESID_BITS 16
  223. #define USER_VSID_RANGE (1UL << (USER_ESID_BITS + SID_SHIFT))
  224. /*
  225. * This macro generates asm code to compute the VSID scramble
  226. * function. Used in slb_allocate() and do_stab_bolted. The function
  227. * computed is: (protovsid*VSID_MULTIPLIER) % VSID_MODULUS
  228. *
  229. * rt = register continaing the proto-VSID and into which the
  230. * VSID will be stored
  231. * rx = scratch register (clobbered)
  232. *
  233. * - rt and rx must be different registers
  234. * - The answer will end up in the low 36 bits of rt. The higher
  235. * bits may contain other garbage, so you may need to mask the
  236. * result.
  237. */
  238. #define ASM_VSID_SCRAMBLE(rt, rx) \
  239. lis rx,VSID_MULTIPLIER@h; \
  240. ori rx,rx,VSID_MULTIPLIER@l; \
  241. mulld rt,rt,rx; /* rt = rt * MULTIPLIER */ \
  242. \
  243. srdi rx,rt,VSID_BITS; \
  244. clrldi rt,rt,(64-VSID_BITS); \
  245. add rt,rt,rx; /* add high and low bits */ \
  246. /* Now, r3 == VSID (mod 2^36-1), and lies between 0 and \
  247. * 2^36-1+2^28-1. That in particular means that if r3 >= \
  248. * 2^36-1, then r3+1 has the 2^36 bit set. So, if r3+1 has \
  249. * the bit clear, r3 already has the answer we want, if it \
  250. * doesn't, the answer is the low 36 bits of r3+1. So in all \
  251. * cases the answer is the low 36 bits of (r3 + ((r3+1) >> 36))*/\
  252. addi rx,rt,1; \
  253. srdi rx,rx,VSID_BITS; /* extract 2^36 bit */ \
  254. add rt,rt,rx
  255. #ifndef __ASSEMBLY__
  256. typedef unsigned long mm_context_id_t;
  257. typedef struct {
  258. mm_context_id_t id;
  259. #ifdef CONFIG_HUGETLB_PAGE
  260. u16 low_htlb_areas, high_htlb_areas;
  261. #endif
  262. } mm_context_t;
  263. static inline unsigned long vsid_scramble(unsigned long protovsid)
  264. {
  265. #if 0
  266. /* The code below is equivalent to this function for arguments
  267. * < 2^VSID_BITS, which is all this should ever be called
  268. * with. However gcc is not clever enough to compute the
  269. * modulus (2^n-1) without a second multiply. */
  270. return ((protovsid * VSID_MULTIPLIER) % VSID_MODULUS);
  271. #else /* 1 */
  272. unsigned long x;
  273. x = protovsid * VSID_MULTIPLIER;
  274. x = (x >> VSID_BITS) + (x & VSID_MODULUS);
  275. return (x + ((x+1) >> VSID_BITS)) & VSID_MODULUS;
  276. #endif /* 1 */
  277. }
  278. /* This is only valid for addresses >= KERNELBASE */
  279. static inline unsigned long get_kernel_vsid(unsigned long ea)
  280. {
  281. return vsid_scramble(ea >> SID_SHIFT);
  282. }
  283. /* This is only valid for user addresses (which are below 2^41) */
  284. static inline unsigned long get_vsid(unsigned long context, unsigned long ea)
  285. {
  286. return vsid_scramble((context << USER_ESID_BITS)
  287. | (ea >> SID_SHIFT));
  288. }
  289. #define VSID_SCRAMBLE(pvsid) (((pvsid) * VSID_MULTIPLIER) % VSID_MODULUS)
  290. #define KERNEL_VSID(ea) VSID_SCRAMBLE(GET_ESID(ea))
  291. #endif /* __ASSEMBLY */
  292. #endif /* _PPC64_MMU_H_ */