hugepage-hash64.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 permissions don't match, take page fault */
  39. if (unlikely(access & ~old_pmd))
  40. return 1;
  41. /*
  42. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  43. * a write access
  44. */
  45. new_pmd = old_pmd | _PAGE_BUSY | _PAGE_ACCESSED;
  46. if (access & _PAGE_RW)
  47. new_pmd |= _PAGE_DIRTY;
  48. } while (old_pmd != __cmpxchg_u64((unsigned long *)pmdp,
  49. old_pmd, new_pmd));
  50. rflags = htab_convert_pte_flags(new_pmd);
  51. #if 0
  52. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  53. /*
  54. * No CPU has hugepages but lacks no execute, so we
  55. * don't need to worry about that case
  56. */
  57. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  58. }
  59. #endif
  60. /*
  61. * Find the slot index details for this ea, using base page size.
  62. */
  63. shift = mmu_psize_defs[psize].shift;
  64. index = (ea & ~HPAGE_PMD_MASK) >> shift;
  65. BUG_ON(index >= PTE_FRAG_SIZE);
  66. vpn = hpt_vpn(ea, vsid, ssize);
  67. hpte_slot_array = get_hpte_slot_array(pmdp);
  68. if (psize == MMU_PAGE_4K) {
  69. /*
  70. * invalidate the old hpte entry if we have that mapped via 64K
  71. * base page size. This is because demote_segment won't flush
  72. * hash page table entries.
  73. */
  74. if ((old_pmd & _PAGE_HASHPTE) && !(old_pmd & _PAGE_COMBO))
  75. flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
  76. ssize, flags);
  77. }
  78. valid = hpte_valid(hpte_slot_array, index);
  79. if (valid) {
  80. /* update the hpte bits */
  81. hash = hpt_hash(vpn, shift, ssize);
  82. hidx = hpte_hash_index(hpte_slot_array, index);
  83. if (hidx & _PTEIDX_SECONDARY)
  84. hash = ~hash;
  85. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  86. slot += hidx & _PTEIDX_GROUP_IX;
  87. ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
  88. psize, lpsize, ssize, flags);
  89. /*
  90. * We failed to update, try to insert a new entry.
  91. */
  92. if (ret == -1) {
  93. /*
  94. * large pte is marked busy, so we can be sure
  95. * nobody is looking at hpte_slot_array. hence we can
  96. * safely update this here.
  97. */
  98. valid = 0;
  99. hpte_slot_array[index] = 0;
  100. }
  101. }
  102. if (!valid) {
  103. unsigned long hpte_group;
  104. hash = hpt_hash(vpn, shift, ssize);
  105. /* insert new entry */
  106. pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
  107. new_pmd |= _PAGE_HASHPTE;
  108. repeat:
  109. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  110. /* Insert into the hash table, primary slot */
  111. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  112. psize, lpsize, ssize);
  113. /*
  114. * Primary is full, try the secondary
  115. */
  116. if (unlikely(slot == -1)) {
  117. hpte_group = ((~hash & htab_hash_mask) *
  118. HPTES_PER_GROUP) & ~0x7UL;
  119. slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
  120. rflags, HPTE_V_SECONDARY,
  121. psize, lpsize, ssize);
  122. if (slot == -1) {
  123. if (mftb() & 0x1)
  124. hpte_group = ((hash & htab_hash_mask) *
  125. HPTES_PER_GROUP) & ~0x7UL;
  126. ppc_md.hpte_remove(hpte_group);
  127. goto repeat;
  128. }
  129. }
  130. /*
  131. * Hypervisor failure. Restore old pmd and return -1
  132. * similar to __hash_page_*
  133. */
  134. if (unlikely(slot == -2)) {
  135. *pmdp = __pmd(old_pmd);
  136. hash_failure_debug(ea, access, vsid, trap, ssize,
  137. psize, lpsize, old_pmd);
  138. return -1;
  139. }
  140. /*
  141. * large pte is marked busy, so we can be sure
  142. * nobody is looking at hpte_slot_array. hence we can
  143. * safely update this here.
  144. */
  145. mark_hpte_slot_valid(hpte_slot_array, index, slot);
  146. }
  147. /*
  148. * Mark the pte with _PAGE_COMBO, if we are trying to hash it with
  149. * base page size 4k.
  150. */
  151. if (psize == MMU_PAGE_4K)
  152. new_pmd |= _PAGE_COMBO;
  153. /*
  154. * The hpte valid is stored in the pgtable whose address is in the
  155. * second half of the PMD. Order this against clearing of the busy bit in
  156. * huge pmd.
  157. */
  158. smp_wmb();
  159. *pmdp = __pmd(new_pmd & ~_PAGE_BUSY);
  160. return 0;
  161. }