hugepage-hash64.c 6.7 KB

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