hash_64k.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. * Copyright IBM Corporation, 2015
  3. * Author Aneesh Kumar K.V <[email protected]>
  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, flags);
  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;
  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;
  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) * HPTES_PER_GROUP;
  177. mmu_hash_ops.hpte_remove(hpte_group);
  178. /*
  179. * FIXME!! Should be try the group from which we removed ?
  180. */
  181. goto repeat;
  182. }
  183. }
  184. /*
  185. * Hypervisor failure. Restore old pte and return -1
  186. * similar to __hash_page_*
  187. */
  188. if (unlikely(slot == -2)) {
  189. *ptep = __pte(old_pte);
  190. hash_failure_debug(ea, access, vsid, trap, ssize,
  191. MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
  192. return -1;
  193. }
  194. new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
  195. new_pte |= H_PAGE_HASHPTE;
  196. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  197. return 0;
  198. }
  199. int __hash_page_64K(unsigned long ea, unsigned long access,
  200. unsigned long vsid, pte_t *ptep, unsigned long trap,
  201. unsigned long flags, int ssize)
  202. {
  203. real_pte_t rpte;
  204. unsigned long hpte_group;
  205. unsigned long rflags, pa;
  206. unsigned long old_pte, new_pte;
  207. unsigned long vpn, hash, slot;
  208. unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
  209. /*
  210. * atomically mark the linux large page PTE busy and dirty
  211. */
  212. do {
  213. pte_t pte = READ_ONCE(*ptep);
  214. old_pte = pte_val(pte);
  215. /* If PTE busy, retry the access */
  216. if (unlikely(old_pte & H_PAGE_BUSY))
  217. return 0;
  218. /* If PTE permissions don't match, take page fault */
  219. if (unlikely(!check_pte_access(access, old_pte)))
  220. return 1;
  221. /*
  222. * Check if PTE has the cache-inhibit bit set
  223. * If so, bail out and refault as a 4k page
  224. */
  225. if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
  226. unlikely(pte_ci(pte)))
  227. return 0;
  228. /*
  229. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  230. * a write access.
  231. */
  232. new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
  233. if (access & _PAGE_WRITE)
  234. new_pte |= _PAGE_DIRTY;
  235. } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
  236. rflags = htab_convert_pte_flags(new_pte, flags);
  237. rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
  238. if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
  239. !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
  240. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  241. vpn = hpt_vpn(ea, vsid, ssize);
  242. if (unlikely(old_pte & H_PAGE_HASHPTE)) {
  243. unsigned long gslot;
  244. /*
  245. * There MIGHT be an HPTE for this pte
  246. */
  247. gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
  248. if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K,
  249. MMU_PAGE_64K, ssize,
  250. flags) == -1)
  251. old_pte &= ~_PAGE_HPTEFLAGS;
  252. }
  253. if (likely(!(old_pte & H_PAGE_HASHPTE))) {
  254. pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
  255. hash = hpt_hash(vpn, shift, ssize);
  256. repeat:
  257. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  258. /* Insert into the hash table, primary slot */
  259. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  260. MMU_PAGE_64K, MMU_PAGE_64K,
  261. ssize);
  262. /*
  263. * Primary is full, try the secondary
  264. */
  265. if (unlikely(slot == -1)) {
  266. hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
  267. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  268. rflags,
  269. HPTE_V_SECONDARY,
  270. MMU_PAGE_64K,
  271. MMU_PAGE_64K, ssize);
  272. if (slot == -1) {
  273. if (mftb() & 0x1)
  274. hpte_group = (hash & htab_hash_mask) *
  275. HPTES_PER_GROUP;
  276. mmu_hash_ops.hpte_remove(hpte_group);
  277. /*
  278. * FIXME!! Should be try the group from which we removed ?
  279. */
  280. goto repeat;
  281. }
  282. }
  283. /*
  284. * Hypervisor failure. Restore old pte and return -1
  285. * similar to __hash_page_*
  286. */
  287. if (unlikely(slot == -2)) {
  288. *ptep = __pte(old_pte);
  289. hash_failure_debug(ea, access, vsid, trap, ssize,
  290. MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
  291. return -1;
  292. }
  293. new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
  294. new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
  295. }
  296. *ptep = __pte(new_pte & ~H_PAGE_BUSY);
  297. return 0;
  298. }