blob: 7d2a9b6a55caa970fa17d964764b187be985deed [file] [log] [blame]
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +05301#ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H
2#define _ASM_POWERPC_BOOK3S_64_HASH_64K_H
3
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +10004#define H_PTE_INDEX_SIZE 8
5#define H_PMD_INDEX_SIZE 5
6#define H_PUD_INDEX_SIZE 5
7#define H_PGD_INDEX_SIZE 12
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +05308
9/* With 4k base page size, hugepage PTEs go at the PMD level */
10#define MIN_HUGEPTE_SHIFT PAGE_SHIFT
11
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100012#define H_PAGE_COMBO 0x00001000 /* this is a combo 4k page */
13#define H_PAGE_4K_PFN 0x00002000 /* PFN is for a single 4k page */
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053014/*
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100015 * We need to differentiate between explicit huge page and THP huge
16 * page, since THP huge page also need to track real subpage details
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000017 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100018#define H_PAGE_THP_HUGE H_PAGE_4K_PFN
19
20/*
21 * Used to track subpage group valid if H_PAGE_COMBO is set
22 * This overloads H_PAGE_F_GIX and H_PAGE_F_SECOND
23 */
24#define H_PAGE_COMBO_VALID (H_PAGE_F_GIX | H_PAGE_F_SECOND)
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000025
26/* PTE flags to conserve for HPTE identification */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100027#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_F_SECOND | \
28 H_PAGE_F_GIX | H_PAGE_HASHPTE | H_PAGE_COMBO)
Aneesh Kumar K.V62607bc2015-12-01 09:06:55 +053029/*
30 * we support 16 fragments per PTE page of 64K size.
31 */
32#define PTE_FRAG_NR 16
33/*
34 * We use a 2K PTE page fragment and another 2K for storing
35 * real_pte_t hash index
36 */
37#define PTE_FRAG_SIZE_SHIFT 12
38#define PTE_FRAG_SIZE (1UL << PTE_FRAG_SIZE_SHIFT)
39
Stephen Rothwellee7a76d2007-09-18 17:22:59 +100040#ifndef __ASSEMBLY__
Aneesh Kumar K.V96270b12016-04-29 23:25:35 +100041#include <asm/errno.h>
Benjamin Herrenschmidt3c726f82005-11-07 11:06:55 +110042
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000043/*
44 * With 64K pages on hash table, we have a special PTE format that
45 * uses a second "half" of the page table to encode sub-page information
46 * in order to deal with 64K made of 4K HW pages. Thus we override the
47 * generic accessors and iterators here
48 */
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053049#define __real_pte __real_pte
50static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep)
51{
52 real_pte_t rpte;
Aneesh Kumar K.V506b8632015-12-01 09:06:46 +053053 unsigned long *hidxp;
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053054
55 rpte.pte = pte;
56 rpte.hidx = 0;
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100057 if (pte_val(pte) & H_PAGE_COMBO) {
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053058 /*
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100059 * Make sure we order the hidx load against the H_PAGE_COMBO
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053060 * check. The store side ordering is done in __hash_page_4K
61 */
62 smp_rmb();
Aneesh Kumar K.V506b8632015-12-01 09:06:46 +053063 hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
64 rpte.hidx = *hidxp;
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053065 }
66 return rpte;
67}
68
69static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
70{
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100071 if ((pte_val(rpte.pte) & H_PAGE_COMBO))
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053072 return (rpte.hidx >> (index<<2)) & 0xf;
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100073 return (pte_val(rpte.pte) >> H_PAGE_F_GIX_SHIFT) & 0xf;
Aneesh Kumar K.V85c1faf2014-08-13 12:32:03 +053074}
75
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000076#define __rpte_to_pte(r) ((r).pte)
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053077extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index);
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +053078/*
79 * Trick: we set __end to va + 64k, which happens works for
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000080 * a 16M page as well as we want only one iteration
81 */
Aneesh Kumar K.V5524a272012-09-10 02:52:50 +000082#define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift) \
83 do { \
84 unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT)); \
85 unsigned __split = (psize == MMU_PAGE_4K || \
86 psize == MMU_PAGE_64K_AP); \
87 shift = mmu_psize_defs[psize].shift; \
88 for (index = 0; vpn < __end; index++, \
89 vpn += (1L << (shift - VPN_SHIFT))) { \
90 if (!__split || __rpte_sub_valid(rpte, index)) \
91 do {
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000092
93#define pte_iterate_hashed_end() } while(0); } } while(0)
94
95#define pte_pagesize_index(mm, addr, pte) \
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100096 (((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K)
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +000097
Aneesh Kumar K.V96270b12016-04-29 23:25:35 +100098extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
99 unsigned long pfn, unsigned long size, pgprot_t);
100static inline int remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr,
101 unsigned long pfn, pgprot_t prot)
102{
103 if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) {
104 WARN(1, "remap_4k_pfn called with wrong pfn value\n");
105 return -EINVAL;
106 }
107 return remap_pfn_range(vma, addr, pfn, PAGE_SIZE,
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000108 __pgprot(pgprot_val(prot) | H_PAGE_4K_PFN));
Aneesh Kumar K.V96270b12016-04-29 23:25:35 +1000109}
Benjamin Herrenschmidtc6057822009-03-10 17:53:29 +0000110
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000111#define H_PTE_TABLE_SIZE PTE_FRAG_SIZE
Aneesh Kumar K.V62607bc2015-12-01 09:06:55 +0530112#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000113#define H_PMD_TABLE_SIZE ((sizeof(pmd_t) << PMD_INDEX_SIZE) + \
114 (sizeof(unsigned long) << PMD_INDEX_SIZE))
Aneesh Kumar K.V62607bc2015-12-01 09:06:55 +0530115#else
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000116#define H_PMD_TABLE_SIZE (sizeof(pmd_t) << PMD_INDEX_SIZE)
Aneesh Kumar K.V62607bc2015-12-01 09:06:55 +0530117#endif
Aneesh Kumar K.Vdd1842a2016-04-29 23:25:49 +1000118#define H_PUD_TABLE_SIZE (sizeof(pud_t) << PUD_INDEX_SIZE)
119#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE)
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +0530120
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530121#ifdef CONFIG_HUGETLB_PAGE
122/*
123 * We have PGD_INDEX_SIZ = 12 and PTE_INDEX_SIZE = 8, so that we can have
124 * 16GB hugepage pte in PGD and 16MB hugepage pte at PMD;
125 *
126 * Defined in such a way that we can optimize away code block at build time
127 * if CONFIG_HUGETLB_PAGE=n.
128 */
129static inline int pmd_huge(pmd_t pmd)
130{
131 /*
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530132 * leaf pte for huge page
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530133 */
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530134 return !!(pmd_val(pmd) & _PAGE_PTE);
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530135}
136
137static inline int pud_huge(pud_t pud)
138{
139 /*
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530140 * leaf pte for huge page
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530141 */
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530142 return !!(pud_val(pud) & _PAGE_PTE);
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530143}
144
145static inline int pgd_huge(pgd_t pgd)
146{
147 /*
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530148 * leaf pte for huge page
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530149 */
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530150 return !!(pgd_val(pgd) & _PAGE_PTE);
Aneesh Kumar K.V26a344a2015-12-01 09:06:52 +0530151}
152#define pgd_huge pgd_huge
153
154#ifdef CONFIG_DEBUG_VM
155extern int hugepd_ok(hugepd_t hpd);
156#define is_hugepd(hpd) (hugepd_ok(hpd))
157#else
158/*
159 * With 64k page size, we have hugepage ptes in the pgd and pmd entries. We don't
160 * need to setup hugepage directory for them. Our pte and page directory format
161 * enable us to have this enabled.
162 */
163static inline int hugepd_ok(hugepd_t hpd)
164{
165 return 0;
166}
167#define is_hugepd(pdep) 0
168#endif /* CONFIG_DEBUG_VM */
169
170#endif /* CONFIG_HUGETLB_PAGE */
171
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530172#ifdef CONFIG_TRANSPARENT_HUGEPAGE
173extern unsigned long pmd_hugepage_update(struct mm_struct *mm,
174 unsigned long addr,
175 pmd_t *pmdp,
176 unsigned long clr,
177 unsigned long set);
178static inline char *get_hpte_slot_array(pmd_t *pmdp)
179{
180 /*
181 * The hpte hindex is stored in the pgtable whose address is in the
182 * second half of the PMD
183 *
184 * Order this load with the test for pmd_trans_huge in the caller
185 */
186 smp_rmb();
187 return *(char **)(pmdp + PTRS_PER_PMD);
188
189
190}
191/*
192 * The linux hugepage PMD now include the pmd entries followed by the address
193 * to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits.
Paul Mackerras849f86a2016-02-22 13:41:15 +1100194 * [ 000 | 1 bit secondary | 3 bit hidx | 1 bit valid]. We use one byte per
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530195 * each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and
196 * with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t.
197 *
Paul Mackerras849f86a2016-02-22 13:41:15 +1100198 * The top three bits are intentionally left as zero. This memory location
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530199 * are also used as normal page PTE pointers. So if we have any pointers
200 * left around while we collapse a hugepage, we need to make sure
201 * _PAGE_PRESENT bit of that is zero when we look at them
202 */
203static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)
204{
Paul Mackerras849f86a2016-02-22 13:41:15 +1100205 return hpte_slot_array[index] & 0x1;
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530206}
207
208static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,
209 int index)
210{
Paul Mackerras849f86a2016-02-22 13:41:15 +1100211 return hpte_slot_array[index] >> 1;
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530212}
213
214static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,
215 unsigned int index, unsigned int hidx)
216{
Paul Mackerras849f86a2016-02-22 13:41:15 +1100217 hpte_slot_array[index] = (hidx << 1) | 0x1;
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530218}
219
220/*
221 *
222 * For core kernel code by design pmd_trans_huge is never run on any hugetlbfs
223 * page. The hugetlbfs page table walking and mangling paths are totally
224 * separated form the core VM paths and they're differentiated by
225 * VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run.
226 *
227 * pmd_trans_huge() is defined as false at build time if
228 * CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build
229 * time in such case.
230 *
231 * For ppc64 we need to differntiate from explicit hugepages from THP, because
232 * for THP we also track the subpage details at the pmd level. We don't do
233 * that for explicit huge pages.
234 *
235 */
236static inline int pmd_trans_huge(pmd_t pmd)
237{
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000238 return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE)) ==
239 (_PAGE_PTE | H_PAGE_THP_HUGE));
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530240}
241
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530242static inline int pmd_large(pmd_t pmd)
243{
Aneesh Kumar K.V6a119ea2015-12-01 09:06:54 +0530244 return !!(pmd_val(pmd) & _PAGE_PTE);
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530245}
246
247static inline pmd_t pmd_mknotpresent(pmd_t pmd)
248{
249 return __pmd(pmd_val(pmd) & ~_PAGE_PRESENT);
250}
251
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530252#define __HAVE_ARCH_PMD_SAME
253static inline int pmd_same(pmd_t pmd_a, pmd_t pmd_b)
254{
Michael Ellermanee3caed2016-04-29 23:25:29 +1000255 return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530256}
257
258static inline int __pmdp_test_and_clear_young(struct mm_struct *mm,
259 unsigned long addr, pmd_t *pmdp)
260{
261 unsigned long old;
262
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000263 if ((pmd_val(*pmdp) & (_PAGE_ACCESSED | H_PAGE_HASHPTE)) == 0)
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530264 return 0;
265 old = pmd_hugepage_update(mm, addr, pmdp, _PAGE_ACCESSED, 0);
266 return ((old & _PAGE_ACCESSED) != 0);
267}
268
269#define __HAVE_ARCH_PMDP_SET_WRPROTECT
270static inline void pmdp_set_wrprotect(struct mm_struct *mm, unsigned long addr,
271 pmd_t *pmdp)
272{
273
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000274 if ((pmd_val(*pmdp) & _PAGE_WRITE) == 0)
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530275 return;
276
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000277 pmd_hugepage_update(mm, addr, pmdp, _PAGE_WRITE, 0);
Aneesh Kumar K.Ve34aa032015-12-01 09:06:53 +0530278}
279
280#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Paul Mackerrasfa282372008-01-24 08:35:13 +1100281#endif /* __ASSEMBLY__ */
Aneesh Kumar K.Vab537dc2015-12-01 09:06:30 +0530282
283#endif /* _ASM_POWERPC_BOOK3S_64_HASH_64K_H */