hugepage-hash64.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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, int local, int ssize,
  21. 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. old_pmd = pmd_val(*pmdp);
  34. /* If PMD busy, retry the access */
  35. if (unlikely(old_pmd & _PAGE_BUSY))
  36. return 0;
  37. /* If PMD permissions don't match, take page fault */
  38. if (unlikely(access & ~old_pmd))
  39. return 1;
  40. /*
  41. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  42. * a write access
  43. */
  44. new_pmd = old_pmd | _PAGE_BUSY | _PAGE_ACCESSED;
  45. if (access & _PAGE_RW)
  46. new_pmd |= _PAGE_DIRTY;
  47. } while (old_pmd != __cmpxchg_u64((unsigned long *)pmdp,
  48. old_pmd, new_pmd));
  49. /*
  50. * PP bits. _PAGE_USER is already PP bit 0x2, so we only
  51. * need to add in 0x1 if it's a read-only user page
  52. */
  53. rflags = new_pmd & _PAGE_USER;
  54. if ((new_pmd & _PAGE_USER) && !((new_pmd & _PAGE_RW) &&
  55. (new_pmd & _PAGE_DIRTY)))
  56. rflags |= 0x1;
  57. /*
  58. * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
  59. */
  60. rflags |= ((new_pmd & _PAGE_EXEC) ? 0 : HPTE_R_N);
  61. #if 0
  62. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  63. /*
  64. * No CPU has hugepages but lacks no execute, so we
  65. * don't need to worry about that case
  66. */
  67. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  68. }
  69. #endif
  70. /*
  71. * Find the slot index details for this ea, using base page size.
  72. */
  73. shift = mmu_psize_defs[psize].shift;
  74. index = (ea & ~HPAGE_PMD_MASK) >> shift;
  75. BUG_ON(index >= 4096);
  76. vpn = hpt_vpn(ea, vsid, ssize);
  77. hash = hpt_hash(vpn, shift, ssize);
  78. hpte_slot_array = get_hpte_slot_array(pmdp);
  79. valid = hpte_valid(hpte_slot_array, index);
  80. if (valid) {
  81. /* update the hpte bits */
  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, local);
  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. new_pmd &= ~_PAGE_HPTEFLAGS;
  100. hpte_slot_array[index] = 0;
  101. } else
  102. /* clear the busy bits and set the hash pte bits */
  103. new_pmd = (new_pmd & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  104. }
  105. if (!valid) {
  106. unsigned long hpte_group;
  107. /* insert new entry */
  108. pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
  109. repeat:
  110. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  111. /* clear the busy bits and set the hash pte bits */
  112. new_pmd = (new_pmd & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
  113. /* Add in WIMG bits */
  114. rflags |= (new_pmd & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
  115. _PAGE_COHERENT | _PAGE_GUARDED));
  116. /* Insert into the hash table, primary slot */
  117. slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  118. psize, lpsize, ssize);
  119. /*
  120. * Primary is full, try the secondary
  121. */
  122. if (unlikely(slot == -1)) {
  123. hpte_group = ((~hash & htab_hash_mask) *
  124. HPTES_PER_GROUP) & ~0x7UL;
  125. slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
  126. rflags, HPTE_V_SECONDARY,
  127. psize, lpsize, ssize);
  128. if (slot == -1) {
  129. if (mftb() & 0x1)
  130. hpte_group = ((hash & htab_hash_mask) *
  131. HPTES_PER_GROUP) & ~0x7UL;
  132. ppc_md.hpte_remove(hpte_group);
  133. goto repeat;
  134. }
  135. }
  136. /*
  137. * Hypervisor failure. Restore old pmd and return -1
  138. * similar to __hash_page_*
  139. */
  140. if (unlikely(slot == -2)) {
  141. *pmdp = __pmd(old_pmd);
  142. hash_failure_debug(ea, access, vsid, trap, ssize,
  143. psize, lpsize, old_pmd);
  144. return -1;
  145. }
  146. /*
  147. * large pte is marked busy, so we can be sure
  148. * nobody is looking at hpte_slot_array. hence we can
  149. * safely update this here.
  150. */
  151. mark_hpte_slot_valid(hpte_slot_array, index, slot);
  152. }
  153. /*
  154. * No need to use ldarx/stdcx here
  155. */
  156. *pmdp = __pmd(new_pmd & ~_PAGE_BUSY);
  157. return 0;
  158. }