mmu-hash.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. #ifndef _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
  2. #define _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
  3. /*
  4. * PowerPC64 memory management structures
  5. *
  6. * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
  7. * PPC64 rework.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <asm/asm-compat.h>
  15. #include <asm/page.h>
  16. #include <asm/bug.h>
  17. /*
  18. * This is necessary to get the definition of PGTABLE_RANGE which we
  19. * need for various slices related matters. Note that this isn't the
  20. * complete pgtable.h but only a portion of it.
  21. */
  22. #include <asm/book3s/64/pgtable.h>
  23. #include <asm/bug.h>
  24. #include <asm/processor.h>
  25. #include <asm/cpu_has_feature.h>
  26. /*
  27. * SLB
  28. */
  29. #define SLB_NUM_BOLTED 3
  30. #define SLB_CACHE_ENTRIES 8
  31. #define SLB_MIN_SIZE 32
  32. /* Bits in the SLB ESID word */
  33. #define SLB_ESID_V ASM_CONST(0x0000000008000000) /* valid */
  34. /* Bits in the SLB VSID word */
  35. #define SLB_VSID_SHIFT 12
  36. #define SLB_VSID_SHIFT_256M SLB_VSID_SHIFT
  37. #define SLB_VSID_SHIFT_1T 24
  38. #define SLB_VSID_SSIZE_SHIFT 62
  39. #define SLB_VSID_B ASM_CONST(0xc000000000000000)
  40. #define SLB_VSID_B_256M ASM_CONST(0x0000000000000000)
  41. #define SLB_VSID_B_1T ASM_CONST(0x4000000000000000)
  42. #define SLB_VSID_KS ASM_CONST(0x0000000000000800)
  43. #define SLB_VSID_KP ASM_CONST(0x0000000000000400)
  44. #define SLB_VSID_N ASM_CONST(0x0000000000000200) /* no-execute */
  45. #define SLB_VSID_L ASM_CONST(0x0000000000000100)
  46. #define SLB_VSID_C ASM_CONST(0x0000000000000080) /* class */
  47. #define SLB_VSID_LP ASM_CONST(0x0000000000000030)
  48. #define SLB_VSID_LP_00 ASM_CONST(0x0000000000000000)
  49. #define SLB_VSID_LP_01 ASM_CONST(0x0000000000000010)
  50. #define SLB_VSID_LP_10 ASM_CONST(0x0000000000000020)
  51. #define SLB_VSID_LP_11 ASM_CONST(0x0000000000000030)
  52. #define SLB_VSID_LLP (SLB_VSID_L|SLB_VSID_LP)
  53. #define SLB_VSID_KERNEL (SLB_VSID_KP)
  54. #define SLB_VSID_USER (SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
  55. #define SLBIE_C (0x08000000)
  56. #define SLBIE_SSIZE_SHIFT 25
  57. /*
  58. * Hash table
  59. */
  60. #define HPTES_PER_GROUP 8
  61. #define HPTE_V_SSIZE_SHIFT 62
  62. #define HPTE_V_AVPN_SHIFT 7
  63. #define HPTE_V_COMMON_BITS ASM_CONST(0x000fffffffffffff)
  64. #define HPTE_V_AVPN ASM_CONST(0x3fffffffffffff80)
  65. #define HPTE_V_AVPN_3_0 ASM_CONST(0x000fffffffffff80)
  66. #define HPTE_V_AVPN_VAL(x) (((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
  67. #define HPTE_V_COMPARE(x,y) (!(((x) ^ (y)) & 0xffffffffffffff80UL))
  68. #define HPTE_V_BOLTED ASM_CONST(0x0000000000000010)
  69. #define HPTE_V_LOCK ASM_CONST(0x0000000000000008)
  70. #define HPTE_V_LARGE ASM_CONST(0x0000000000000004)
  71. #define HPTE_V_SECONDARY ASM_CONST(0x0000000000000002)
  72. #define HPTE_V_VALID ASM_CONST(0x0000000000000001)
  73. /*
  74. * ISA 3.0 has a different HPTE format.
  75. */
  76. #define HPTE_R_3_0_SSIZE_SHIFT 58
  77. #define HPTE_R_3_0_SSIZE_MASK (3ull << HPTE_R_3_0_SSIZE_SHIFT)
  78. #define HPTE_R_PP0 ASM_CONST(0x8000000000000000)
  79. #define HPTE_R_TS ASM_CONST(0x4000000000000000)
  80. #define HPTE_R_KEY_HI ASM_CONST(0x3000000000000000)
  81. #define HPTE_R_RPN_SHIFT 12
  82. #define HPTE_R_RPN ASM_CONST(0x0ffffffffffff000)
  83. #define HPTE_R_RPN_3_0 ASM_CONST(0x01fffffffffff000)
  84. #define HPTE_R_PP ASM_CONST(0x0000000000000003)
  85. #define HPTE_R_PPP ASM_CONST(0x8000000000000003)
  86. #define HPTE_R_N ASM_CONST(0x0000000000000004)
  87. #define HPTE_R_G ASM_CONST(0x0000000000000008)
  88. #define HPTE_R_M ASM_CONST(0x0000000000000010)
  89. #define HPTE_R_I ASM_CONST(0x0000000000000020)
  90. #define HPTE_R_W ASM_CONST(0x0000000000000040)
  91. #define HPTE_R_WIMG ASM_CONST(0x0000000000000078)
  92. #define HPTE_R_C ASM_CONST(0x0000000000000080)
  93. #define HPTE_R_R ASM_CONST(0x0000000000000100)
  94. #define HPTE_R_KEY_LO ASM_CONST(0x0000000000000e00)
  95. #define HPTE_R_KEY (HPTE_R_KEY_LO | HPTE_R_KEY_HI)
  96. #define HPTE_V_1TB_SEG ASM_CONST(0x4000000000000000)
  97. #define HPTE_V_VRMA_MASK ASM_CONST(0x4001ffffff000000)
  98. /* Values for PP (assumes Ks=0, Kp=1) */
  99. #define PP_RWXX 0 /* Supervisor read/write, User none */
  100. #define PP_RWRX 1 /* Supervisor read/write, User read */
  101. #define PP_RWRW 2 /* Supervisor read/write, User read/write */
  102. #define PP_RXRX 3 /* Supervisor read, User read */
  103. #define PP_RXXX (HPTE_R_PP0 | 2) /* Supervisor read, user none */
  104. /* Fields for tlbiel instruction in architecture 2.06 */
  105. #define TLBIEL_INVAL_SEL_MASK 0xc00 /* invalidation selector */
  106. #define TLBIEL_INVAL_PAGE 0x000 /* invalidate a single page */
  107. #define TLBIEL_INVAL_SET_LPID 0x800 /* invalidate a set for current LPID */
  108. #define TLBIEL_INVAL_SET 0xc00 /* invalidate a set for all LPIDs */
  109. #define TLBIEL_INVAL_SET_MASK 0xfff000 /* set number to inval. */
  110. #define TLBIEL_INVAL_SET_SHIFT 12
  111. #define POWER7_TLB_SETS 128 /* # sets in POWER7 TLB */
  112. #define POWER8_TLB_SETS 512 /* # sets in POWER8 TLB */
  113. #define POWER9_TLB_SETS_HASH 256 /* # sets in POWER9 TLB Hash mode */
  114. #define POWER9_TLB_SETS_RADIX 128 /* # sets in POWER9 TLB Radix mode */
  115. #ifndef __ASSEMBLY__
  116. struct mmu_hash_ops {
  117. void (*hpte_invalidate)(unsigned long slot,
  118. unsigned long vpn,
  119. int bpsize, int apsize,
  120. int ssize, int local);
  121. long (*hpte_updatepp)(unsigned long slot,
  122. unsigned long newpp,
  123. unsigned long vpn,
  124. int bpsize, int apsize,
  125. int ssize, unsigned long flags);
  126. void (*hpte_updateboltedpp)(unsigned long newpp,
  127. unsigned long ea,
  128. int psize, int ssize);
  129. long (*hpte_insert)(unsigned long hpte_group,
  130. unsigned long vpn,
  131. unsigned long prpn,
  132. unsigned long rflags,
  133. unsigned long vflags,
  134. int psize, int apsize,
  135. int ssize);
  136. long (*hpte_remove)(unsigned long hpte_group);
  137. int (*hpte_removebolted)(unsigned long ea,
  138. int psize, int ssize);
  139. void (*flush_hash_range)(unsigned long number, int local);
  140. void (*hugepage_invalidate)(unsigned long vsid,
  141. unsigned long addr,
  142. unsigned char *hpte_slot_array,
  143. int psize, int ssize, int local);
  144. int (*resize_hpt)(unsigned long shift);
  145. /*
  146. * Special for kexec.
  147. * To be called in real mode with interrupts disabled. No locks are
  148. * taken as such, concurrent access on pre POWER5 hardware could result
  149. * in a deadlock.
  150. * The linear mapping is destroyed as well.
  151. */
  152. void (*hpte_clear_all)(void);
  153. };
  154. extern struct mmu_hash_ops mmu_hash_ops;
  155. struct hash_pte {
  156. __be64 v;
  157. __be64 r;
  158. };
  159. extern struct hash_pte *htab_address;
  160. extern unsigned long htab_size_bytes;
  161. extern unsigned long htab_hash_mask;
  162. static inline int shift_to_mmu_psize(unsigned int shift)
  163. {
  164. int psize;
  165. for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
  166. if (mmu_psize_defs[psize].shift == shift)
  167. return psize;
  168. return -1;
  169. }
  170. static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
  171. {
  172. if (mmu_psize_defs[mmu_psize].shift)
  173. return mmu_psize_defs[mmu_psize].shift;
  174. BUG();
  175. }
  176. static inline unsigned long get_sllp_encoding(int psize)
  177. {
  178. unsigned long sllp;
  179. sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
  180. ((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
  181. return sllp;
  182. }
  183. #endif /* __ASSEMBLY__ */
  184. /*
  185. * Segment sizes.
  186. * These are the values used by hardware in the B field of
  187. * SLB entries and the first dword of MMU hashtable entries.
  188. * The B field is 2 bits; the values 2 and 3 are unused and reserved.
  189. */
  190. #define MMU_SEGSIZE_256M 0
  191. #define MMU_SEGSIZE_1T 1
  192. /*
  193. * encode page number shift.
  194. * in order to fit the 78 bit va in a 64 bit variable we shift the va by
  195. * 12 bits. This enable us to address upto 76 bit va.
  196. * For hpt hash from a va we can ignore the page size bits of va and for
  197. * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
  198. * we work in all cases including 4k page size.
  199. */
  200. #define VPN_SHIFT 12
  201. /*
  202. * HPTE Large Page (LP) details
  203. */
  204. #define LP_SHIFT 12
  205. #define LP_BITS 8
  206. #define LP_MASK(i) ((0xFF >> (i)) << LP_SHIFT)
  207. #ifndef __ASSEMBLY__
  208. static inline int slb_vsid_shift(int ssize)
  209. {
  210. if (ssize == MMU_SEGSIZE_256M)
  211. return SLB_VSID_SHIFT;
  212. return SLB_VSID_SHIFT_1T;
  213. }
  214. static inline int segment_shift(int ssize)
  215. {
  216. if (ssize == MMU_SEGSIZE_256M)
  217. return SID_SHIFT;
  218. return SID_SHIFT_1T;
  219. }
  220. /*
  221. * This array is indexed by the LP field of the HPTE second dword.
  222. * Since this field may contain some RPN bits, some entries are
  223. * replicated so that we get the same value irrespective of RPN.
  224. * The top 4 bits are the page size index (MMU_PAGE_*) for the
  225. * actual page size, the bottom 4 bits are the base page size.
  226. */
  227. extern u8 hpte_page_sizes[1 << LP_BITS];
  228. static inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
  229. bool is_base_size)
  230. {
  231. unsigned int i, lp;
  232. if (!(h & HPTE_V_LARGE))
  233. return 1ul << 12;
  234. /* Look at the 8 bit LP value */
  235. lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
  236. i = hpte_page_sizes[lp];
  237. if (!i)
  238. return 0;
  239. if (!is_base_size)
  240. i >>= 4;
  241. return 1ul << mmu_psize_defs[i & 0xf].shift;
  242. }
  243. static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
  244. {
  245. return __hpte_page_size(h, l, 0);
  246. }
  247. static inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
  248. {
  249. return __hpte_page_size(h, l, 1);
  250. }
  251. /*
  252. * The current system page and segment sizes
  253. */
  254. extern int mmu_kernel_ssize;
  255. extern int mmu_highuser_ssize;
  256. extern u16 mmu_slb_size;
  257. extern unsigned long tce_alloc_start, tce_alloc_end;
  258. /*
  259. * If the processor supports 64k normal pages but not 64k cache
  260. * inhibited pages, we have to be prepared to switch processes
  261. * to use 4k pages when they create cache-inhibited mappings.
  262. * If this is the case, mmu_ci_restrictions will be set to 1.
  263. */
  264. extern int mmu_ci_restrictions;
  265. /*
  266. * This computes the AVPN and B fields of the first dword of a HPTE,
  267. * for use when we want to match an existing PTE. The bottom 7 bits
  268. * of the returned value are zero.
  269. */
  270. static inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
  271. int ssize)
  272. {
  273. unsigned long v;
  274. /*
  275. * The AVA field omits the low-order 23 bits of the 78 bits VA.
  276. * These bits are not needed in the PTE, because the
  277. * low-order b of these bits are part of the byte offset
  278. * into the virtual page and, if b < 23, the high-order
  279. * 23-b of these bits are always used in selecting the
  280. * PTEGs to be searched
  281. */
  282. v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
  283. v <<= HPTE_V_AVPN_SHIFT;
  284. v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
  285. return v;
  286. }
  287. /*
  288. * ISA v3.0 defines a new HPTE format, which differs from the old
  289. * format in having smaller AVPN and ARPN fields, and the B field
  290. * in the second dword instead of the first.
  291. */
  292. static inline unsigned long hpte_old_to_new_v(unsigned long v)
  293. {
  294. /* trim AVPN, drop B */
  295. return v & HPTE_V_COMMON_BITS;
  296. }
  297. static inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
  298. {
  299. /* move B field from 1st to 2nd dword, trim ARPN */
  300. return (r & ~HPTE_R_3_0_SSIZE_MASK) |
  301. (((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
  302. }
  303. static inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
  304. {
  305. /* insert B field */
  306. return (v & HPTE_V_COMMON_BITS) |
  307. ((r & HPTE_R_3_0_SSIZE_MASK) <<
  308. (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
  309. }
  310. static inline unsigned long hpte_new_to_old_r(unsigned long r)
  311. {
  312. /* clear out B field */
  313. return r & ~HPTE_R_3_0_SSIZE_MASK;
  314. }
  315. /*
  316. * This function sets the AVPN and L fields of the HPTE appropriately
  317. * using the base page size and actual page size.
  318. */
  319. static inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
  320. int actual_psize, int ssize)
  321. {
  322. unsigned long v;
  323. v = hpte_encode_avpn(vpn, base_psize, ssize);
  324. if (actual_psize != MMU_PAGE_4K)
  325. v |= HPTE_V_LARGE;
  326. return v;
  327. }
  328. /*
  329. * This function sets the ARPN, and LP fields of the HPTE appropriately
  330. * for the page size. We assume the pa is already "clean" that is properly
  331. * aligned for the requested page size
  332. */
  333. static inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
  334. int actual_psize)
  335. {
  336. /* A 4K page needs no special encoding */
  337. if (actual_psize == MMU_PAGE_4K)
  338. return pa & HPTE_R_RPN;
  339. else {
  340. unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
  341. unsigned int shift = mmu_psize_defs[actual_psize].shift;
  342. return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
  343. }
  344. }
  345. /*
  346. * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
  347. */
  348. static inline unsigned long hpt_vpn(unsigned long ea,
  349. unsigned long vsid, int ssize)
  350. {
  351. unsigned long mask;
  352. int s_shift = segment_shift(ssize);
  353. mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
  354. return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
  355. }
  356. /*
  357. * This hashes a virtual address
  358. */
  359. static inline unsigned long hpt_hash(unsigned long vpn,
  360. unsigned int shift, int ssize)
  361. {
  362. unsigned long mask;
  363. unsigned long hash, vsid;
  364. /* VPN_SHIFT can be atmost 12 */
  365. if (ssize == MMU_SEGSIZE_256M) {
  366. mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
  367. hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
  368. ((vpn & mask) >> (shift - VPN_SHIFT));
  369. } else {
  370. mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
  371. vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
  372. hash = vsid ^ (vsid << 25) ^
  373. ((vpn & mask) >> (shift - VPN_SHIFT)) ;
  374. }
  375. return hash & 0x7fffffffffUL;
  376. }
  377. #define HPTE_LOCAL_UPDATE 0x1
  378. #define HPTE_NOHPTE_UPDATE 0x2
  379. extern int __hash_page_4K(unsigned long ea, unsigned long access,
  380. unsigned long vsid, pte_t *ptep, unsigned long trap,
  381. unsigned long flags, int ssize, int subpage_prot);
  382. extern int __hash_page_64K(unsigned long ea, unsigned long access,
  383. unsigned long vsid, pte_t *ptep, unsigned long trap,
  384. unsigned long flags, int ssize);
  385. struct mm_struct;
  386. unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
  387. extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
  388. unsigned long access, unsigned long trap,
  389. unsigned long flags);
  390. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
  391. unsigned long dsisr);
  392. int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
  393. pte_t *ptep, unsigned long trap, unsigned long flags,
  394. int ssize, unsigned int shift, unsigned int mmu_psize);
  395. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  396. extern int __hash_page_thp(unsigned long ea, unsigned long access,
  397. unsigned long vsid, pmd_t *pmdp, unsigned long trap,
  398. unsigned long flags, int ssize, unsigned int psize);
  399. #else
  400. static inline int __hash_page_thp(unsigned long ea, unsigned long access,
  401. unsigned long vsid, pmd_t *pmdp,
  402. unsigned long trap, unsigned long flags,
  403. int ssize, unsigned int psize)
  404. {
  405. BUG();
  406. return -1;
  407. }
  408. #endif
  409. extern void hash_failure_debug(unsigned long ea, unsigned long access,
  410. unsigned long vsid, unsigned long trap,
  411. int ssize, int psize, int lpsize,
  412. unsigned long pte);
  413. extern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
  414. unsigned long pstart, unsigned long prot,
  415. int psize, int ssize);
  416. int htab_remove_mapping(unsigned long vstart, unsigned long vend,
  417. int psize, int ssize);
  418. extern void pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
  419. extern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
  420. #ifdef CONFIG_PPC_PSERIES
  421. void hpte_init_pseries(void);
  422. #else
  423. static inline void hpte_init_pseries(void) { }
  424. #endif
  425. extern void hpte_init_native(void);
  426. extern void slb_initialize(void);
  427. extern void slb_flush_and_rebolt(void);
  428. extern void slb_vmalloc_update(void);
  429. extern void slb_set_size(u16 size);
  430. #endif /* __ASSEMBLY__ */
  431. /*
  432. * VSID allocation (256MB segment)
  433. *
  434. * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
  435. * from mmu context id and effective segment id of the address.
  436. *
  437. * For user processes max context id is limited to MAX_USER_CONTEXT.
  438. * For kernel space, we use context ids 1-4 to map addresses as below:
  439. * NOTE: each context only support 64TB now.
  440. * 0x00001 - [ 0xc000000000000000 - 0xc0003fffffffffff ]
  441. * 0x00002 - [ 0xd000000000000000 - 0xd0003fffffffffff ]
  442. * 0x00003 - [ 0xe000000000000000 - 0xe0003fffffffffff ]
  443. * 0x00004 - [ 0xf000000000000000 - 0xf0003fffffffffff ]
  444. *
  445. * The proto-VSIDs are then scrambled into real VSIDs with the
  446. * multiplicative hash:
  447. *
  448. * VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
  449. *
  450. * VSID_MULTIPLIER is prime, so in particular it is
  451. * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
  452. * Because the modulus is 2^n-1 we can compute it efficiently without
  453. * a divide or extra multiply (see below). The scramble function gives
  454. * robust scattering in the hash table (at least based on some initial
  455. * results).
  456. *
  457. * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
  458. * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
  459. * will produce a VSID of 0.
  460. *
  461. * We also need to avoid the last segment of the last context, because that
  462. * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
  463. * because of the modulo operation in vsid scramble.
  464. */
  465. /*
  466. * Max Va bits we support as of now is 68 bits. We want 19 bit
  467. * context ID.
  468. * Restrictions:
  469. * GPU has restrictions of not able to access beyond 128TB
  470. * (47 bit effective address). We also cannot do more than 20bit PID.
  471. * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
  472. * to 16 bits (ie, we can only have 2^16 pids at the same time).
  473. */
  474. #define VA_BITS 68
  475. #define CONTEXT_BITS 19
  476. #define ESID_BITS (VA_BITS - (SID_SHIFT + CONTEXT_BITS))
  477. #define ESID_BITS_1T (VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
  478. #define ESID_BITS_MASK ((1 << ESID_BITS) - 1)
  479. #define ESID_BITS_1T_MASK ((1 << ESID_BITS_1T) - 1)
  480. /*
  481. * 256MB segment
  482. * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
  483. * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
  484. * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
  485. * context maps 2^49 bytes (512TB).
  486. *
  487. * We also need to avoid the last segment of the last context, because that
  488. * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
  489. * because of the modulo operation in vsid scramble.
  490. */
  491. #define MAX_USER_CONTEXT ((ASM_CONST(1) << CONTEXT_BITS) - 2)
  492. #define MIN_USER_CONTEXT (5)
  493. /* Would be nice to use KERNEL_REGION_ID here */
  494. #define KERNEL_REGION_CONTEXT_OFFSET (0xc - 1)
  495. /*
  496. * For platforms that support on 65bit VA we limit the context bits
  497. */
  498. #define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
  499. /*
  500. * This should be computed such that protovosid * vsid_mulitplier
  501. * doesn't overflow 64 bits. The vsid_mutliplier should also be
  502. * co-prime to vsid_modulus. We also need to make sure that number
  503. * of bits in multiplied result (dividend) is less than twice the number of
  504. * protovsid bits for our modulus optmization to work.
  505. *
  506. * The below table shows the current values used.
  507. * |-------+------------+----------------------+------------+-------------------|
  508. * | | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
  509. * |-------+------------+----------------------+------------+-------------------|
  510. * | 1T | 24 | 25 | 49 | 50 |
  511. * |-------+------------+----------------------+------------+-------------------|
  512. * | 256MB | 24 | 37 | 61 | 74 |
  513. * |-------+------------+----------------------+------------+-------------------|
  514. *
  515. * |-------+------------+----------------------+------------+--------------------|
  516. * | | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
  517. * |-------+------------+----------------------+------------+--------------------|
  518. * | 1T | 24 | 28 | 52 | 56 |
  519. * |-------+------------+----------------------+------------+--------------------|
  520. * | 256MB | 24 | 40 | 64 | 80 |
  521. * |-------+------------+----------------------+------------+--------------------|
  522. *
  523. */
  524. #define VSID_MULTIPLIER_256M ASM_CONST(12538073) /* 24-bit prime */
  525. #define VSID_BITS_256M (VA_BITS - SID_SHIFT)
  526. #define VSID_BITS_65_256M (65 - SID_SHIFT)
  527. /*
  528. * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
  529. */
  530. #define VSID_MULINV_256M ASM_CONST(665548017062)
  531. #define VSID_MULTIPLIER_1T ASM_CONST(12538073) /* 24-bit prime */
  532. #define VSID_BITS_1T (VA_BITS - SID_SHIFT_1T)
  533. #define VSID_BITS_65_1T (65 - SID_SHIFT_1T)
  534. #define VSID_MULINV_1T ASM_CONST(209034062)
  535. /* 1TB VSID reserved for VRMA */
  536. #define VRMA_VSID 0x1ffffffUL
  537. #define USER_VSID_RANGE (1UL << (ESID_BITS + SID_SHIFT))
  538. /* 4 bits per slice and we have one slice per 1TB */
  539. #define SLICE_ARRAY_SIZE (H_PGTABLE_RANGE >> 41)
  540. #define TASK_SLICE_ARRAY_SZ(x) ((x)->context.slb_addr_limit >> 41)
  541. #ifndef __ASSEMBLY__
  542. #ifdef CONFIG_PPC_SUBPAGE_PROT
  543. /*
  544. * For the sub-page protection option, we extend the PGD with one of
  545. * these. Basically we have a 3-level tree, with the top level being
  546. * the protptrs array. To optimize speed and memory consumption when
  547. * only addresses < 4GB are being protected, pointers to the first
  548. * four pages of sub-page protection words are stored in the low_prot
  549. * array.
  550. * Each page of sub-page protection words protects 1GB (4 bytes
  551. * protects 64k). For the 3-level tree, each page of pointers then
  552. * protects 8TB.
  553. */
  554. struct subpage_prot_table {
  555. unsigned long maxaddr; /* only addresses < this are protected */
  556. unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
  557. unsigned int *low_prot[4];
  558. };
  559. #define SBP_L1_BITS (PAGE_SHIFT - 2)
  560. #define SBP_L2_BITS (PAGE_SHIFT - 3)
  561. #define SBP_L1_COUNT (1 << SBP_L1_BITS)
  562. #define SBP_L2_COUNT (1 << SBP_L2_BITS)
  563. #define SBP_L2_SHIFT (PAGE_SHIFT + SBP_L1_BITS)
  564. #define SBP_L3_SHIFT (SBP_L2_SHIFT + SBP_L2_BITS)
  565. extern void subpage_prot_free(struct mm_struct *mm);
  566. extern void subpage_prot_init_new_context(struct mm_struct *mm);
  567. #else
  568. static inline void subpage_prot_free(struct mm_struct *mm) {}
  569. static inline void subpage_prot_init_new_context(struct mm_struct *mm) { }
  570. #endif /* CONFIG_PPC_SUBPAGE_PROT */
  571. #if 0
  572. /*
  573. * The code below is equivalent to this function for arguments
  574. * < 2^VSID_BITS, which is all this should ever be called
  575. * with. However gcc is not clever enough to compute the
  576. * modulus (2^n-1) without a second multiply.
  577. */
  578. #define vsid_scramble(protovsid, size) \
  579. ((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
  580. /* simplified form avoiding mod operation */
  581. #define vsid_scramble(protovsid, size) \
  582. ({ \
  583. unsigned long x; \
  584. x = (protovsid) * VSID_MULTIPLIER_##size; \
  585. x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
  586. (x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
  587. })
  588. #else /* 1 */
  589. static inline unsigned long vsid_scramble(unsigned long protovsid,
  590. unsigned long vsid_multiplier, int vsid_bits)
  591. {
  592. unsigned long vsid;
  593. unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
  594. /*
  595. * We have same multipler for both 256 and 1T segements now
  596. */
  597. vsid = protovsid * vsid_multiplier;
  598. vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
  599. return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
  600. }
  601. #endif /* 1 */
  602. /* Returns the segment size indicator for a user address */
  603. static inline int user_segment_size(unsigned long addr)
  604. {
  605. /* Use 1T segments if possible for addresses >= 1T */
  606. if (addr >= (1UL << SID_SHIFT_1T))
  607. return mmu_highuser_ssize;
  608. return MMU_SEGSIZE_256M;
  609. }
  610. static inline unsigned long get_vsid(unsigned long context, unsigned long ea,
  611. int ssize)
  612. {
  613. unsigned long va_bits = VA_BITS;
  614. unsigned long vsid_bits;
  615. unsigned long protovsid;
  616. /*
  617. * Bad address. We return VSID 0 for that
  618. */
  619. if ((ea & ~REGION_MASK) >= H_PGTABLE_RANGE)
  620. return 0;
  621. if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
  622. va_bits = 65;
  623. if (ssize == MMU_SEGSIZE_256M) {
  624. vsid_bits = va_bits - SID_SHIFT;
  625. protovsid = (context << ESID_BITS) |
  626. ((ea >> SID_SHIFT) & ESID_BITS_MASK);
  627. return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
  628. }
  629. /* 1T segment */
  630. vsid_bits = va_bits - SID_SHIFT_1T;
  631. protovsid = (context << ESID_BITS_1T) |
  632. ((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
  633. return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
  634. }
  635. /*
  636. * This is only valid for addresses >= PAGE_OFFSET
  637. */
  638. static inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
  639. {
  640. unsigned long context;
  641. if (!is_kernel_addr(ea))
  642. return 0;
  643. /*
  644. * For kernel space, we use context ids 1-4 to map the address space as
  645. * below:
  646. *
  647. * 0x00001 - [ 0xc000000000000000 - 0xc0003fffffffffff ]
  648. * 0x00002 - [ 0xd000000000000000 - 0xd0003fffffffffff ]
  649. * 0x00003 - [ 0xe000000000000000 - 0xe0003fffffffffff ]
  650. * 0x00004 - [ 0xf000000000000000 - 0xf0003fffffffffff ]
  651. *
  652. * So we can compute the context from the region (top nibble) by
  653. * subtracting 11, or 0xc - 1.
  654. */
  655. context = (ea >> 60) - KERNEL_REGION_CONTEXT_OFFSET;
  656. return get_vsid(context, ea, ssize);
  657. }
  658. unsigned htab_shift_for_mem_size(unsigned long mem_size);
  659. #endif /* __ASSEMBLY__ */
  660. #endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */