hash64_64k.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * Return true, if the entry has a slot value which
  19. * the software considers as invalid.
  20. */
  21. static inline bool hpte_soft_invalid(unsigned long hidx)
  22. {
  23. return ((hidx & 0xfUL) == 0xfUL);
  24. }
  25. /*
  26. * index from 0 - 15
  27. */
  28. bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
  29. {
  30. return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index)));
  31. }
  32. int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
  33. pte_t *ptep, unsigned long trap, unsigned long flags,
  34. int ssize, int subpg_prot)
  35. {
  36. real_pte_t rpte;
  37. unsigned long hpte_group;
  38. unsigned int subpg_index;
  39. unsigned long rflags, pa;
  40. unsigned long old_pte, new_pte, subpg_pte;
  41. unsigned long vpn, hash, slot, gslot;
  42. unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
  43. /*
  44. * atomically mark the linux large page PTE busy and dirty
  45. */
  46. do {
  47. pte_t pte = READ_ONCE(*ptep);
  48. old_pte = pte_val(pte);
  49. /* If PTE busy, retry the access */
  50. if (unlikely(old_pte & H_PAGE_BUSY))
  51. return 0;
  52. /* If PTE permissions don't match, take page fault */
  53. if (unlikely(!check_pte_access(access, old_pte)))
  54. return 1;
  55. /*
  56. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  57. * a write access. Since this is 4K insert of 64K page size
  58. * also add H_PAGE_COMBO
  59. */
  60. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
  61. if (access & _PAGE_WRITE)
  62. new_pte |= _PAGE_DIRTY;
  63. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  64. /*
  65. * Handle the subpage protection bits
  66. */
  67. subpg_pte = new_pte & ~subpg_prot;
  68. rflags = htab_convert_pte_flags(subpg_pte);
  69. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  70. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  71. /*
  72. * No CPU has hugepages but lacks no execute, so we
  73. * don't need to worry about that case
  74. */
  75. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  76. }
  77. subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
  78. vpn = hpt_vpn(ea, vsid, ssize);
  79. rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
  80. /*
  81. *None of the sub 4k page is hashed
  82. */
  83. if (!(old_pte & H_PAGE_HASHPTE))
  84. goto htab_insert_hpte;
  85. /*
  86. * Check if the pte was already inserted into the hash table
  87. * as a 64k HW page, and invalidate the 64k HPTE if so.
  88. */
  89. if (!(old_pte & H_PAGE_COMBO)) {
  90. flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
  91. /*
  92. * clear the old slot details from the old and new pte.
  93. * On hash insert failure we use old pte value and we don't
  94. * want slot information there if we have a insert failure.
  95. */
  96. old_pte &= ~H_PAGE_HASHPTE;
  97. new_pte &= ~H_PAGE_HASHPTE;
  98. goto htab_insert_hpte;
  99. }
  100. /*
  101. * Check for sub page valid and update
  102. */
  103. if (__rpte_sub_valid(rpte, subpg_index)) {
  104. int ret;
  105. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte,
  106. subpg_index);
  107. ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn,
  108. MMU_PAGE_4K, MMU_PAGE_4K,
  109. ssize, flags);
  110. /*
  111. * If we failed because typically the HPTE wasn't really here
  112. * we try an insertion.
  113. */
  114. if (ret == -1)
  115. goto htab_insert_hpte;
  116. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  117. return 0;
  118. }
  119. htab_insert_hpte:
  120. /*
  121. * Initialize all hidx entries to invalid value, the first time
  122. * the PTE is about to allocate a 4K HPTE.
  123. */
  124. if (!(old_pte & H_PAGE_COMBO))
  125. rpte.hidx = INVALID_RPTE_HIDX;
  126. /*
  127. * handle H_PAGE_4K_PFN case
  128. */
  129. if (old_pte & H_PAGE_4K_PFN) {
  130. /*
  131. * All the sub 4k page have the same
  132. * physical address.
  133. */
  134. pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
  135. } else {
  136. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  137. pa += (subpg_index << shift);
  138. }
  139. hash = hpt_hash(vpn, shift, ssize);
  140. repeat:
  141. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  142. /* Insert into the hash table, primary slot */
  143. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  144. MMU_PAGE_4K, MMU_PAGE_4K, ssize);
  145. /*
  146. * Primary is full, try the secondary
  147. */
  148. if (unlikely(slot == -1)) {
  149. bool soft_invalid;
  150. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  151. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  152. rflags, HPTE_V_SECONDARY,
  153. MMU_PAGE_4K, MMU_PAGE_4K,
  154. ssize);
  155. soft_invalid = hpte_soft_invalid(slot);
  156. if (unlikely(soft_invalid)) {
  157. /*
  158. * We got a valid slot from a hardware point of view.
  159. * but we cannot use it, because we use this special
  160. * value; as defined by hpte_soft_invalid(), to track
  161. * invalid slots. We cannot use it. So invalidate it.
  162. */
  163. gslot = slot & _PTEIDX_GROUP_IX;
  164. mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn,
  165. MMU_PAGE_4K, MMU_PAGE_4K,
  166. ssize, 0);
  167. }
  168. if (unlikely(slot == -1 || soft_invalid)) {
  169. /*
  170. * For soft invalid slot, let's ensure that we release a
  171. * slot from the primary, with the hope that we will
  172. * acquire that slot next time we try. This will ensure
  173. * that we do not get the same soft-invalid slot.
  174. */
  175. if (soft_invalid || (mftb() & 0x1))
  176. hpte_group = ((hash & htab_hash_mask) *
  177. HPTES_PER_GROUP) & ~0x7UL;
  178. mmu_hash_ops.hpte_remove(hpte_group);
  179. /*
  180. * FIXME!! Should be try the group from which we removed ?
  181. */
  182. goto repeat;
  183. }
  184. }
  185. /*
  186. * Hypervisor failure. Restore old pte and return -1
  187. * similar to __hash_page_*
  188. */
  189. if (unlikely(slot == -2)) {
  190. *ptep = __pte(old_pte);
  191. hash_failure_debug(ea, access, vsid, trap, ssize,
  192. MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
  193. return -1;
  194. }
  195. new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
  196. new_pte |= H_PAGE_HASHPTE;
  197. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  198. return 0;
  199. }
  200. int __hash_page_64K(unsigned long ea, unsigned long access,
  201. unsigned long vsid, pte_t *ptep, unsigned long trap,
  202. unsigned long flags, int ssize)
  203. {
  204. real_pte_t rpte;
  205. unsigned long hpte_group;
  206. unsigned long rflags, pa;
  207. unsigned long old_pte, new_pte;
  208. unsigned long vpn, hash, slot;
  209. unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
  210. /*
  211. * atomically mark the linux large page PTE busy and dirty
  212. */
  213. do {
  214. pte_t pte = READ_ONCE(*ptep);
  215. old_pte = pte_val(pte);
  216. /* If PTE busy, retry the access */
  217. if (unlikely(old_pte & H_PAGE_BUSY))
  218. return 0;
  219. /* If PTE permissions don't match, take page fault */
  220. if (unlikely(!check_pte_access(access, old_pte)))
  221. return 1;
  222. /*
  223. * Check if PTE has the cache-inhibit bit set
  224. * If so, bail out and refault as a 4k page
  225. */
  226. if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
  227. unlikely(pte_ci(pte)))
  228. return 0;
  229. /*
  230. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  231. * a write access.
  232. */
  233. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  234. if (access & _PAGE_WRITE)
  235. new_pte |= _PAGE_DIRTY;
  236. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  237. rflags = htab_convert_pte_flags(new_pte);
  238. rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
  239. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  240. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  241. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  242. vpn = hpt_vpn(ea, vsid, ssize);
  243. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  244. unsigned long gslot;
  245. /*
  246. * There MIGHT be an HPTE for this pte
  247. */
  248. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
  249. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K,
  250. MMU_PAGE_64K, ssize,
  251. flags) == -1)
  252. old_pte &= ~_PAGE_HPTEFLAGS;
  253. }
  254. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  255. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  256. hash = hpt_hash(vpn, shift, ssize);
  257. repeat:
  258. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  259. /* Insert into the hash table, primary slot */
  260. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  261. MMU_PAGE_64K, MMU_PAGE_64K,
  262. ssize);
  263. /*
  264. * Primary is full, try the secondary
  265. */
  266. if (unlikely(slot == -1)) {
  267. hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  268. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  269. rflags,
  270. HPTE_V_SECONDARY,
  271. MMU_PAGE_64K,
  272. MMU_PAGE_64K, ssize);
  273. if (slot == -1) {
  274. if (mftb() & 0x1)
  275. hpte_group = ((hash & htab_hash_mask) *
  276. HPTES_PER_GROUP) & ~0x7UL;
  277. mmu_hash_ops.hpte_remove(hpte_group);
  278. /*
  279. * FIXME!! Should be try the group from which we removed ?
  280. */
  281. goto repeat;
  282. }
  283. }
  284. /*
  285. * Hypervisor failure. Restore old pte and return -1
  286. * similar to __hash_page_*
  287. */
  288. if (unlikely(slot == -2)) {
  289. *ptep = __pte(old_pte);
  290. hash_failure_debug(ea, access, vsid, trap, ssize,
  291. MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
  292. return -1;
  293. }
  294. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  295. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
  296. }
  297. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  298. return 0;
  299. }