hash64_64k.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright IBM Corporation, 2015
  3. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #include <linux/mm.h>
  15. #include <asm/machdep.h>
  16. #include <asm/mmu.h>
  17. /*
  18. * index from 0 - 15
  19. */
  20. bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
  21. {
  22. unsigned long g_idx;
  23. unsigned long ptev = pte_val(rpte.pte);
  24. g_idx = (ptev & H_PAGE_COMBO_VALID) >> H_PAGE_F_GIX_SHIFT;
  25. index = index >> 2;
  26. if (g_idx & (0x1 << index))
  27. return true;
  28. else
  29. return false;
  30. }
  31. /*
  32. * index from 0 - 15
  33. */
  34. static unsigned long mark_subptegroup_valid(unsigned long ptev, unsigned long index)
  35. {
  36. unsigned long g_idx;
  37. if (!(ptev & H_PAGE_COMBO))
  38. return ptev;
  39. index = index >> 2;
  40. g_idx = 0x1 << index;
  41. return ptev | (g_idx << H_PAGE_F_GIX_SHIFT);
  42. }
  43. int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
  44. pte_t *ptep, unsigned long trap, unsigned long flags,
  45. int ssize, int subpg_prot)
  46. {
  47. real_pte_t rpte;
  48. unsigned long *hidxp;
  49. unsigned long hpte_group;
  50. unsigned int subpg_index;
  51. unsigned long rflags, pa, hidx;
  52. unsigned long old_pte, new_pte, subpg_pte;
  53. unsigned long vpn, hash, slot;
  54. unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
  55. /*
  56. * atomically mark the linux large page PTE busy and dirty
  57. */
  58. do {
  59. pte_t pte = READ_ONCE(*ptep);
  60. old_pte = pte_val(pte);
  61. /* If PTE busy, retry the access */
  62. if (unlikely(old_pte & H_PAGE_BUSY))
  63. return 0;
  64. /* If PTE permissions don't match, take page fault */
  65. if (unlikely(!check_pte_access(access, old_pte)))
  66. return 1;
  67. /*
  68. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  69. * a write access. Since this is 4K insert of 64K page size
  70. * also add H_PAGE_COMBO
  71. */
  72. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
  73. if (access & _PAGE_WRITE)
  74. new_pte |= _PAGE_DIRTY;
  75. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  76. /*
  77. * Handle the subpage protection bits
  78. */
  79. subpg_pte = new_pte & ~subpg_prot;
  80. rflags = htab_convert_pte_flags(subpg_pte);
  81. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  82. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  83. /*
  84. * No CPU has hugepages but lacks no execute, so we
  85. * don't need to worry about that case
  86. */
  87. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  88. }
  89. subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
  90. vpn = hpt_vpn(ea, vsid, ssize);
  91. rpte = __real_pte(__pte(old_pte), ptep);
  92. /*
  93. *None of the sub 4k page is hashed
  94. */
  95. if (!(old_pte & H_PAGE_HASHPTE))
  96. goto htab_insert_hpte;
  97. /*
  98. * Check if the pte was already inserted into the hash table
  99. * as a 64k HW page, and invalidate the 64k HPTE if so.
  100. */
  101. if (!(old_pte & H_PAGE_COMBO)) {
  102. flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
  103. /*
  104. * clear the old slot details from the old and new pte.
  105. * On hash insert failure we use old pte value and we don't
  106. * want slot information there if we have a insert failure.
  107. */
  108. old_pte &= ~(H_PAGE_HASHPTE | H_PAGE_F_GIX | H_PAGE_F_SECOND);
  109. new_pte &= ~(H_PAGE_HASHPTE | H_PAGE_F_GIX | H_PAGE_F_SECOND);
  110. goto htab_insert_hpte;
  111. }
  112. /*
  113. * Check for sub page valid and update
  114. */
  115. if (__rpte_sub_valid(rpte, subpg_index)) {
  116. int ret;
  117. hash = hpt_hash(vpn, shift, ssize);
  118. hidx = __rpte_to_hidx(rpte, subpg_index);
  119. if (hidx & _PTEIDX_SECONDARY)
  120. hash = ~hash;
  121. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  122. slot += hidx & _PTEIDX_GROUP_IX;
  123. ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
  124. MMU_PAGE_4K, MMU_PAGE_4K,
  125. ssize, flags);
  126. /*
  127. *if we failed because typically the HPTE wasn't really here
  128. * we try an insertion.
  129. */
  130. if (ret == -1)
  131. goto htab_insert_hpte;
  132. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  133. return 0;
  134. }
  135. htab_insert_hpte:
  136. /*
  137. * handle H_PAGE_4K_PFN case
  138. */
  139. if (old_pte & H_PAGE_4K_PFN) {
  140. /*
  141. * All the sub 4k page have the same
  142. * physical address.
  143. */
  144. pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
  145. } else {
  146. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  147. pa += (subpg_index << shift);
  148. }
  149. hash = hpt_hash(vpn, shift, ssize);
  150. repeat:
  151. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  152. /* Insert into the hash table, primary slot */
  153. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  154. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  155. /*
  156. * Primary is full, try the secondary
  157. */
  158. if (unlikely(slot == -1)) {
  159. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  160. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  161. rflags, HPTE_V_SECONDARY,
  162. MMU_PAGE_4K, MMU_PAGE_4K,
  163. ssize);
  164. if (slot == -1) {
  165. if (mftb() & 0x1)
  166. hpte_group = ((hash & htab_hash_mask) *
  167. HPTES_PER_GROUP) & ~0x7UL;
  168. mmu_hash_ops.hpte_remove(hpte_group);
  169. /*
  170. * FIXME!! Should be try the group from which we removed ?
  171. */
  172. goto repeat;
  173. }
  174. }
  175. /*
  176. * Hypervisor failure. Restore old pte and return -1
  177. * similar to __hash_page_*
  178. */
  179. if (unlikely(slot == -2)) {
  180. *ptep = __pte(old_pte);
  181. hash_failure_debug(ea, access, vsid, trap, ssize,
  182. MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
  183. return -1;
  184. }
  185. /*
  186. * Insert slot number & secondary bit in PTE second half,
  187. * clear H_PAGE_BUSY and set appropriate HPTE slot bit
  188. * Since we have H_PAGE_BUSY set on ptep, we can be sure
  189. * nobody is undating hidx.
  190. */
  191. hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
  192. rpte.hidx &= ~(0xfUL << (subpg_index << 2));
  193. *hidxp = rpte.hidx | (slot << (subpg_index << 2));
  194. new_pte = mark_subptegroup_valid(new_pte, subpg_index);
  195. new_pte |= H_PAGE_HASHPTE;
  196. /*
  197. * check __real_pte for details on matching smp_rmb()
  198. */
  199. smp_wmb();
  200. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  201. return 0;
  202. }
  203. int __hash_page_64K(unsigned long ea, unsigned long access,
  204. unsigned long vsid, pte_t *ptep, unsigned long trap,
  205. unsigned long flags, int ssize)
  206. {
  207. unsigned long hpte_group;
  208. unsigned long rflags, pa;
  209. unsigned long old_pte, new_pte;
  210. unsigned long vpn, hash, slot;
  211. unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
  212. /*
  213. * atomically mark the linux large page PTE busy and dirty
  214. */
  215. do {
  216. pte_t pte = READ_ONCE(*ptep);
  217. old_pte = pte_val(pte);
  218. /* If PTE busy, retry the access */
  219. if (unlikely(old_pte & H_PAGE_BUSY))
  220. return 0;
  221. /* If PTE permissions don't match, take page fault */
  222. if (unlikely(!check_pte_access(access, old_pte)))
  223. return 1;
  224. /*
  225. * Check if PTE has the cache-inhibit bit set
  226. * If so, bail out and refault as a 4k page
  227. */
  228. if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
  229. unlikely(pte_ci(pte)))
  230. return 0;
  231. /*
  232. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  233. * a write access.
  234. */
  235. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  236. if (access & _PAGE_WRITE)
  237. new_pte |= _PAGE_DIRTY;
  238. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  239. rflags = htab_convert_pte_flags(new_pte);
  240. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  241. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  242. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  243. vpn = hpt_vpn(ea, vsid, ssize);
  244. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  245. /*
  246. * There MIGHT be an HPTE for this pte
  247. */
  248. hash = hpt_hash(vpn, shift, ssize);
  249. if (old_pte & H_PAGE_F_SECOND)
  250. hash = ~hash;
  251. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  252. slot += (old_pte & H_PAGE_F_GIX) >> H_PAGE_F_GIX_SHIFT;
  253. if (mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K,
  254. MMU_PAGE_64K, ssize,
  255. flags) == -1)
  256. old_pte &= ~_PAGE_HPTEFLAGS;
  257. }
  258. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  259. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  260. hash = hpt_hash(vpn, shift, ssize);
  261. repeat:
  262. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  263. /* Insert into the hash table, primary slot */
  264. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  265. MMU_PAGE_64K, MMU_PAGE_64K,
  266. ssize);
  267. /*
  268. * Primary is full, try the secondary
  269. */
  270. if (unlikely(slot == -1)) {
  271. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  272. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  273. rflags,
  274. HPTE_V_SECONDARY,
  275. MMU_PAGE_64K,
  276. MMU_PAGE_64K, ssize);
  277. if (slot == -1) {
  278. if (mftb() & 0x1)
  279. hpte_group = ((hash & htab_hash_mask) *
  280. HPTES_PER_GROUP) & ~0x7UL;
  281. mmu_hash_ops.hpte_remove(hpte_group);
  282. /*
  283. * FIXME!! Should be try the group from which we removed ?
  284. */
  285. goto repeat;
  286. }
  287. }
  288. /*
  289. * Hypervisor failure. Restore old pte and return -1
  290. * similar to __hash_page_*
  291. */
  292. if (unlikely(slot == -2)) {
  293. *ptep = __pte(old_pte);
  294. hash_failure_debug(ea, access, vsid, trap, ssize,
  295. MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
  296. return -1;
  297. }
  298. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  299. new_pte |= (slot << H_PAGE_F_GIX_SHIFT) &
  300. (H_PAGE_F_SECOND | H_PAGE_F_GIX);
  301. }
  302. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  303. return 0;
  304. }