hash-64k.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H
  2. #define _ASM_POWERPC_BOOK3S_64_HASH_64K_H
  3. #define H_PTE_INDEX_SIZE 8
  4. #define H_PMD_INDEX_SIZE 5
  5. #define H_PUD_INDEX_SIZE 5
  6. #define H_PGD_INDEX_SIZE 12
  7. #define H_PAGE_COMBO 0x00001000 /* this is a combo 4k page */
  8. #define H_PAGE_4K_PFN 0x00002000 /* PFN is for a single 4k page */
  9. /*
  10. * We need to differentiate between explicit huge page and THP huge
  11. * page, since THP huge page also need to track real subpage details
  12. */
  13. #define H_PAGE_THP_HUGE H_PAGE_4K_PFN
  14. /*
  15. * Used to track subpage group valid if H_PAGE_COMBO is set
  16. * This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND
  17. */
  18. #define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
  19. /* PTE flags to conserve for HPTE identification */
  20. #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \
  21. H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO)
  22. /*
  23. * we support 16 fragments per PTE page of 64K size.
  24. */
  25. #define H_PTE_FRAG_NR 16
  26. /*
  27. * We use a 2K PTE page fragment and another 2K for storing
  28. * real_pte_t hash index
  29. */
  30. #define H_PTE_FRAG_SIZE_SHIFT 12
  31. #define PTE_FRAG_SIZE (1UL << PTE_FRAG_SIZE_SHIFT)
  32. #ifndef __ASSEMBLY__
  33. #include <asm/errno.h>
  34. /*
  35. * With 64K pages on hash table, we have a special PTE format that
  36. * uses a second "half" of the page table to encode sub-page information
  37. * in order to deal with 64K made of 4K HW pages. Thus we override the
  38. * generic accessors and iterators here
  39. */
  40. #define __real_pte __real_pte
  41. static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)
  42. {
  43. real_pte_t rpte;
  44. unsigned long *hidxp;
  45. rpte.pte = pte;
  46. rpte.hidx = 0;
  47. if (pte_val(pte) & H_PAGE_COMBO) {
  48. /*
  49. * Make sure we order the hidx load against the H_PAGE_COMBO
  50. * check. The store side ordering is done in __hash_page_4K
  51. */
  52. smp_rmb();
  53. hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
  54. rpte.hidx = *hidxp;
  55. }
  56. return rpte;
  57. }
  58. static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
  59. {
  60. if ((pte_val(rpte.pte) & H_PAGE_COMBO))
  61. return (rpte.hidx >> (index<<2)) & 0xf;
  62. return (pte_val(rpte.pte) >> H_PAGE_F_GIX_SHIFT) & 0xf;
  63. }
  64. #define __rpte_to_pte(r) ((r).pte)
  65. extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index);
  66. /*
  67. * Trick: we set __end to va + 64k, which happens works for
  68. * a 16M page as well as we want only one iteration
  69. */
  70. #define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift) \
  71. do { \
  72. unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT)); \
  73. unsigned __split = (psize == MMU_PAGE_4K || \
  74. psize == MMU_PAGE_64K_AP); \
  75. shift = mmu_psize_defs[psize].shift; \
  76. for (index = 0; vpn < __end; index++, \
  77. vpn += (1L << (shift - VPN_SHIFT))) { \
  78. if (!__split || __rpte_sub_valid(rpte, index)) \
  79. do {
  80. #define pte_iterate_hashed_end() } while(0); } } while(0)
  81. #define pte_pagesize_index(mm, addr, pte) \
  82. (((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K)
  83. extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
  84. unsigned long pfn, unsigned long size, pgprot_t);
  85. static inline int hash__remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr,
  86. unsigned long pfn, pgprot_t prot)
  87. {
  88. if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) {
  89. WARN(1, "remap_4k_pfn called with wrong pfn value\n");
  90. return -EINVAL;
  91. }
  92. return remap_pfn_range(vma, addr, pfn, PAGE_SIZE,
  93. __pgprot(pgprot_val(prot) | H_PAGE_4K_PFN));
  94. }
  95. #define H_PTE_TABLE_SIZE PTE_FRAG_SIZE
  96. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  97. #define H_PMD_TABLE_SIZE ((sizeof(pmd_t) << PMD_INDEX_SIZE) + \
  98. (sizeof(unsigned long) << PMD_INDEX_SIZE))
  99. #else
  100. #define H_PMD_TABLE_SIZE (sizeof(pmd_t) << PMD_INDEX_SIZE)
  101. #endif
  102. #define H_PUD_TABLE_SIZE (sizeof(pud_t) << PUD_INDEX_SIZE)
  103. #define H_PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE)
  104. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  105. static inline char *get_hpte_slot_array(pmd_t *pmdp)
  106. {
  107. /*
  108. * The hpte hindex is stored in the pgtable whose address is in the
  109. * second half of the PMD
  110. *
  111. * Order this load with the test for pmd_trans_huge in the caller
  112. */
  113. smp_rmb();
  114. return *(char **)(pmdp + PTRS_PER_PMD);
  115. }
  116. /*
  117. * The linux hugepage PMD now include the pmd entries followed by the address
  118. * to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits.
  119. * [ 000 | 1 bit secondary | 3 bit hidx | 1 bit valid]. We use one byte per
  120. * each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and
  121. * with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t.
  122. *
  123. * The top three bits are intentionally left as zero. This memory location
  124. * are also used as normal page PTE pointers. So if we have any pointers
  125. * left around while we collapse a hugepage, we need to make sure
  126. * _PAGE_PRESENT bit of that is zero when we look at them
  127. */
  128. static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)
  129. {
  130. return hpte_slot_array[index] & 0x1;
  131. }
  132. static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,
  133. int index)
  134. {
  135. return hpte_slot_array[index] >> 1;
  136. }
  137. static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,
  138. unsigned int index, unsigned int hidx)
  139. {
  140. hpte_slot_array[index] = (hidx << 1) | 0x1;
  141. }
  142. /*
  143. *
  144. * For core kernel code by design pmd_trans_huge is never run on any hugetlbfs
  145. * page. The hugetlbfs page table walking and mangling paths are totally
  146. * separated form the core VM paths and they're differentiated by
  147. * VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run.
  148. *
  149. * pmd_trans_huge() is defined as false at build time if
  150. * CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build
  151. * time in such case.
  152. *
  153. * For ppc64 we need to differntiate from explicit hugepages from THP, because
  154. * for THP we also track the subpage details at the pmd level. We don't do
  155. * that for explicit huge pages.
  156. *
  157. */
  158. static inline int hash__pmd_trans_huge(pmd_t pmd)
  159. {
  160. return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE)) ==
  161. (_PAGE_PTE | H_PAGE_THP_HUGE));
  162. }
  163. static inline int hash__pmd_same(pmd_t pmd_a, pmd_t pmd_b)
  164. {
  165. return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
  166. }
  167. static inline pmd_t hash__pmd_mkhuge(pmd_t pmd)
  168. {
  169. return __pmd(pmd_val(pmd) | (_PAGE_PTE | H_PAGE_THP_HUGE));
  170. }
  171. extern unsigned long hash__pmd_hugepage_update(struct mm_struct *mm,
  172. unsigned long addr, pmd_t *pmdp,
  173. unsigned long clr, unsigned long set);
  174. extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma,
  175. unsigned long address, pmd_t *pmdp);
  176. extern void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
  177. pgtable_t pgtable);
  178. extern pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
  179. extern void hash__pmdp_huge_split_prepare(struct vm_area_struct *vma,
  180. unsigned long address, pmd_t *pmdp);
  181. extern pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,
  182. unsigned long addr, pmd_t *pmdp);
  183. extern int hash__has_transparent_hugepage(void);
  184. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  185. #endif /* __ASSEMBLY__ */
  186. #endif /* _ASM_POWERPC_BOOK3S_64_HASH_64K_H */