hugepage-hash64.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright IBM Corporation, 2013
  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.1 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. /*
  15. * PPC64 THP Support for hash based MMUs
  16. */
  17. #include <linux/mm.h>
  18. #include <asm/machdep.h>
  19. int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,
  20. pmd_t *pmdp, unsigned long trap, unsigned long flags,
  21. int ssize, unsigned int psize)
  22. {
  23. unsigned int index, valid;
  24. unsigned char *hpte_slot_array;
  25. unsigned long rflags, pa, hidx;
  26. unsigned long old_pmd, new_pmd;
  27. int ret, lpsize = MMU_PAGE_16M;
  28. unsigned long vpn, hash, shift, slot;
  29. /*
  30. * atomically mark the linux large page PMD busy and dirty
  31. */
  32. do {
  33. pmd_t pmd = READ_ONCE(*pmdp);
  34. old_pmd = pmd_val(pmd);
  35. /* If PMD busy, retry the access */
  36. if (unlikely(old_pmd & _PAGE_BUSY))
  37. return 0;
  38. /* If PMD is trans splitting retry the access */
  39. if (unlikely(old_pmd & _PAGE_SPLITTING))
  40. return 0;
  41. /* If PMD permissions don't match, take page fault */
  42. if (unlikely(access & ~old_pmd))
  43. return 1;
  44. /*
  45. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  46. * a write access
  47. */
  48. new_pmd = old_pmd | _PAGE_BUSY | _PAGE_ACCESSED;
  49. if (access & _PAGE_RW)
  50. new_pmd |= _PAGE_DIRTY;
  51. } while (old_pmd != __cmpxchg_u64((unsigned long *)pmdp,
  52. old_pmd, new_pmd));
  53. /*
  54. * PP bits. _PAGE_USER is already PP bit 0x2, so we only
  55. * need to add in 0x1 if it's a read-only user page
  56. */
  57. rflags = new_pmd & _PAGE_USER;
  58. if ((new_pmd & _PAGE_USER) && !((new_pmd & _PAGE_RW) &&
  59. (new_pmd & _PAGE_DIRTY)))
  60. rflags |= 0x1;
  61. /*
  62. * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
  63. */
  64. rflags |= ((new_pmd & _PAGE_EXEC) ? 0 : HPTE_R_N);
  65. #if 0
  66. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  67. /*
  68. * No CPU has hugepages but lacks no execute, so we
  69. * don't need to worry about that case
  70. */
  71. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  72. }
  73. #endif
  74. /*
  75. * Find the slot index details for this ea, using base page size.
  76. */
  77. shift = mmu_psize_defs[psize].shift;
  78. index = (ea & ~HPAGE_PMD_MASK) >> shift;
  79. BUG_ON(index >= 4096);
  80. vpn = hpt_vpn(ea, vsid, ssize);
  81. hpte_slot_array = get_hpte_slot_array(pmdp);
  82. if (psize == MMU_PAGE_4K) {
  83. /*
  84. * invalidate the old hpte entry if we have that mapped via 64K
  85. * base page size. This is because demote_segment won't flush
  86. * hash page table entries.
  87. */
  88. if ((old_pmd & _PAGE_HASHPTE) && !(old_pmd & _PAGE_COMBO))
  89. flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
  90. ssize, flags);
  91. }
  92. valid = hpte_valid(hpte_slot_array, index);
  93. if (valid) {
  94. /* update the hpte bits */
  95. hash = hpt_hash(vpn, shift, ssize);
  96. hidx = hpte_hash_index(hpte_slot_array, index);
  97. if (hidx & _PTEIDX_SECONDARY)
  98. hash = ~hash;
  99. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  100. slot += hidx & _PTEIDX_GROUP_IX;
  101. ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
  102. psize, lpsize, ssize, flags);
  103. /*
  104. * We failed to update, try to insert a new entry.
  105. */
  106. if (ret == -1) {
  107. /*
  108. * large pte is marked busy, so we can be sure
  109. * nobody is looking at hpte_slot_array. hence we can
  110. * safely update this here.
  111. */
  112. valid = 0;
  113. hpte_slot_array[index] = 0;
  114. }
  115. }
  116. if (!valid) {
  117. unsigned long hpte_group;
  118. hash = hpt_hash(vpn, shift, ssize);
  119. /* insert new entry */
  120. pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
  121. new_pmd |= _PAGE_HASHPTE;
  122. /* Add in WIMG bits */
  123. rflags |= (new_pmd & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
  124. _PAGE_GUARDED));
  125. /*
  126. * enable the memory coherence always
  127. */
  128. rflags |= HPTE_R_M;
  129. repeat:
  130. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  131. /* Insert into the hash table, primary slot */
  132. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  133. psize, lpsize, ssize);
  134. /*
  135. * Primary is full, try the secondary
  136. */
  137. if (unlikely(slot == -1)) {
  138. hpte_group = ((~hash & htab_hash_mask) *
  139. HPTES_PER_GROUP) & ~0x7UL;
  140. slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
  141. rflags, HPTE_V_SECONDARY,
  142. psize, lpsize, ssize);
  143. if (slot == -1) {
  144. if (mftb() & 0x1)
  145. hpte_group = ((hash & htab_hash_mask) *
  146. HPTES_PER_GROUP) & ~0x7UL;
  147. ppc_md.hpte_remove(hpte_group);
  148. goto repeat;
  149. }
  150. }
  151. /*
  152. * Hypervisor failure. Restore old pmd and return -1
  153. * similar to __hash_page_*
  154. */
  155. if (unlikely(slot == -2)) {
  156. *pmdp = __pmd(old_pmd);
  157. hash_failure_debug(ea, access, vsid, trap, ssize,
  158. psize, lpsize, old_pmd);
  159. return -1;
  160. }
  161. /*
  162. * large pte is marked busy, so we can be sure
  163. * nobody is looking at hpte_slot_array. hence we can
  164. * safely update this here.
  165. */
  166. mark_hpte_slot_valid(hpte_slot_array, index, slot);
  167. }
  168. /*
  169. * Mark the pte with _PAGE_COMBO, if we are trying to hash it with
  170. * base page size 4k.
  171. */
  172. if (psize == MMU_PAGE_4K)
  173. new_pmd |= _PAGE_COMBO;
  174. /*
  175. * The hpte valid is stored in the pgtable whose address is in the
  176. * second half of the PMD. Order this against clearing of the busy bit in
  177. * huge pmd.
  178. */
  179. smp_wmb();
  180. *pmdp = __pmd(new_pmd & ~_PAGE_BUSY);
  181. return 0;
  182. }