Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
ab537dca AK |
2 | #ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H |
3 | #define _ASM_POWERPC_BOOK3S_64_HASH_64K_H | |
4 | ||
eea86aa4 ME |
5 | #define H_PTE_INDEX_SIZE 8 // size: 8B << 8 = 2KB, maps 2^8 x 64KB = 16MB |
6 | #define H_PMD_INDEX_SIZE 10 // size: 8B << 10 = 8KB, maps 2^10 x 16MB = 16GB | |
7 | #define H_PUD_INDEX_SIZE 10 // size: 8B << 10 = 8KB, maps 2^10 x 16GB = 16TB | |
8 | #define H_PGD_INDEX_SIZE 8 // size: 8B << 8 = 2KB, maps 2^8 x 16TB = 4PB | |
9 | ||
ab537dca | 10 | |
f384796c AK |
11 | /* |
12 | * Each context is 512TB size. SLB miss for first context/default context | |
13 | * is handled in the hotpath. | |
14 | */ | |
15 | #define MAX_EA_BITS_PER_CONTEXT 49 | |
16 | ||
0034d395 AK |
17 | /* |
18 | * We use one context for each MAP area. | |
19 | */ | |
20 | #define H_KERN_MAP_SIZE (1UL << MAX_EA_BITS_PER_CONTEXT) | |
21 | ||
22 | /* | |
23 | * Define the address range of the kernel non-linear virtual area | |
24 | * 2PB | |
25 | */ | |
26 | #define H_KERN_VIRT_START ASM_CONST(0xc008000000000000) | |
27 | ||
f5bd0fdc AK |
28 | /* |
29 | * 64k aligned address free up few of the lower bits of RPN for us | |
30 | * We steal that here. For more deatils look at pte_pfn/pfn_pte() | |
31 | */ | |
32789d38 AK |
32 | #define H_PAGE_COMBO _RPAGE_RPN0 /* this is a combo 4k page */ |
33 | #define H_PAGE_4K_PFN _RPAGE_RPN1 /* PFN is for a single 4k page */ | |
bf9a95f9 | 34 | #define H_PAGE_BUSY _RPAGE_RPN44 /* software: PTE & hash are busy */ |
273b4936 | 35 | #define H_PAGE_HASHPTE _RPAGE_RPN43 /* PTE has associated HPTE */ |
9d2edb18 | 36 | |
1a2f7789 AK |
37 | /* memory key bits. */ |
38 | #define H_PTE_PKEY_BIT0 _RPAGE_RSV1 | |
39 | #define H_PTE_PKEY_BIT1 _RPAGE_RSV2 | |
40 | #define H_PTE_PKEY_BIT2 _RPAGE_RSV3 | |
41 | #define H_PTE_PKEY_BIT3 _RPAGE_RSV4 | |
42 | #define H_PTE_PKEY_BIT4 _RPAGE_RSV5 | |
43 | ||
bf680d51 | 44 | /* |
945537df AK |
45 | * We need to differentiate between explicit huge page and THP huge |
46 | * page, since THP huge page also need to track real subpage details | |
16c2d476 | 47 | */ |
945537df AK |
48 | #define H_PAGE_THP_HUGE H_PAGE_4K_PFN |
49 | ||
3c726f8d | 50 | /* PTE flags to conserve for HPTE identification */ |
bf9a95f9 | 51 | #define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO) |
62607bc6 AK |
52 | /* |
53 | * We use a 2K PTE page fragment and another 2K for storing | |
54 | * real_pte_t hash index | |
fb4e5dbd AK |
55 | * 8 bytes per each pte entry and another 8 bytes for storing |
56 | * slot details. | |
62607bc6 | 57 | */ |
fb4e5dbd AK |
58 | #define H_PTE_FRAG_SIZE_SHIFT (H_PTE_INDEX_SIZE + 3 + 1) |
59 | #define H_PTE_FRAG_NR (PAGE_SIZE >> H_PTE_FRAG_SIZE_SHIFT) | |
62607bc6 | 60 | |
8a6c697b AK |
61 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE) |
62 | #define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3 + 1) | |
63 | #else | |
64 | #define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3) | |
65 | #endif | |
66 | #define H_PMD_FRAG_NR (PAGE_SIZE >> H_PMD_FRAG_SIZE_SHIFT) | |
67 | ||
c605782b | 68 | #ifndef __ASSEMBLY__ |
96270b1f | 69 | #include <asm/errno.h> |
3c726f8d | 70 | |
c605782b BH |
71 | /* |
72 | * With 64K pages on hash table, we have a special PTE format that | |
73 | * uses a second "half" of the page table to encode sub-page information | |
74 | * in order to deal with 64K made of 4K HW pages. Thus we override the | |
75 | * generic accessors and iterators here | |
76 | */ | |
85c1fafd | 77 | #define __real_pte __real_pte |
ff31e105 | 78 | static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep, int offset) |
85c1fafd AK |
79 | { |
80 | real_pte_t rpte; | |
506b863c | 81 | unsigned long *hidxp; |
85c1fafd AK |
82 | |
83 | rpte.pte = pte; | |
bf9a95f9 RP |
84 | |
85 | /* | |
86 | * Ensure that we do not read the hidx before we read the PTE. Because | |
87 | * the writer side is expected to finish writing the hidx first followed | |
88 | * by the PTE, by using smp_wmb(). pte_set_hash_slot() ensures that. | |
89 | */ | |
90 | smp_rmb(); | |
91 | ||
ff31e105 | 92 | hidxp = (unsigned long *)(ptep + offset); |
bf9a95f9 | 93 | rpte.hidx = *hidxp; |
85c1fafd AK |
94 | return rpte; |
95 | } | |
96 | ||
7b84947c RP |
97 | /* |
98 | * shift the hidx representation by one-modulo-0xf; i.e hidx 0 is respresented | |
99 | * as 1, 1 as 2,... , and 0xf as 0. This convention lets us represent a | |
100 | * invalid hidx 0xf with a 0x0 bit value. PTEs are anyway zero'd when | |
101 | * allocated. We dont have to zero them gain; thus save on the initialization. | |
102 | */ | |
103 | #define HIDX_UNSHIFT_BY_ONE(x) ((x + 0xfUL) & 0xfUL) /* shift backward by one */ | |
104 | #define HIDX_SHIFT_BY_ONE(x) ((x + 0x1UL) & 0xfUL) /* shift forward by one */ | |
59aa31fd | 105 | #define HIDX_BITS(x, index) (x << (index << 2)) |
bf9a95f9 | 106 | #define BITS_TO_HIDX(x, index) ((x >> (index << 2)) & 0xfUL) |
7b84947c | 107 | #define INVALID_RPTE_HIDX 0x0UL |
59aa31fd | 108 | |
85c1fafd AK |
109 | static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index) |
110 | { | |
7b84947c | 111 | return HIDX_UNSHIFT_BY_ONE(BITS_TO_HIDX(rpte.hidx, index)); |
85c1fafd AK |
112 | } |
113 | ||
59aa31fd RP |
114 | /* |
115 | * Commit the hidx and return PTE bits that needs to be modified. The caller is | |
116 | * expected to modify the PTE bits accordingly and commit the PTE to memory. | |
117 | */ | |
118 | static inline unsigned long pte_set_hidx(pte_t *ptep, real_pte_t rpte, | |
ff31e105 AK |
119 | unsigned int subpg_index, |
120 | unsigned long hidx, int offset) | |
59aa31fd | 121 | { |
ff31e105 | 122 | unsigned long *hidxp = (unsigned long *)(ptep + offset); |
59aa31fd RP |
123 | |
124 | rpte.hidx &= ~HIDX_BITS(0xfUL, subpg_index); | |
7b84947c | 125 | *hidxp = rpte.hidx | HIDX_BITS(HIDX_SHIFT_BY_ONE(hidx), subpg_index); |
59aa31fd RP |
126 | |
127 | /* | |
128 | * Anyone reading PTE must ensure hidx bits are read after reading the | |
129 | * PTE by using the read-side barrier smp_rmb(). __real_pte() can be | |
130 | * used for that. | |
131 | */ | |
132 | smp_wmb(); | |
133 | ||
134 | /* No PTE bits to be modified, return 0x0UL */ | |
135 | return 0x0UL; | |
85c1fafd AK |
136 | } |
137 | ||
3c726f8d | 138 | #define __rpte_to_pte(r) ((r).pte) |
bf680d51 | 139 | extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index); |
ab537dca AK |
140 | /* |
141 | * Trick: we set __end to va + 64k, which happens works for | |
3c726f8d BH |
142 | * a 16M page as well as we want only one iteration |
143 | */ | |
5524a27d AK |
144 | #define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift) \ |
145 | do { \ | |
146 | unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT)); \ | |
147 | unsigned __split = (psize == MMU_PAGE_4K || \ | |
148 | psize == MMU_PAGE_64K_AP); \ | |
149 | shift = mmu_psize_defs[psize].shift; \ | |
150 | for (index = 0; vpn < __end; index++, \ | |
151 | vpn += (1L << (shift - VPN_SHIFT))) { \ | |
f405b510 | 152 | if (!__split || __rpte_sub_valid(rpte, index)) |
3c726f8d | 153 | |
f405b510 | 154 | #define pte_iterate_hashed_end() } } while(0) |
3c726f8d | 155 | |
16c2d476 | 156 | #define pte_pagesize_index(mm, addr, pte) \ |
945537df | 157 | (((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K) |
3c726f8d | 158 | |
96270b1f AK |
159 | extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr, |
160 | unsigned long pfn, unsigned long size, pgprot_t); | |
6cc1a0ee AK |
161 | static inline int hash__remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr, |
162 | unsigned long pfn, pgprot_t prot) | |
96270b1f AK |
163 | { |
164 | if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) { | |
165 | WARN(1, "remap_4k_pfn called with wrong pfn value\n"); | |
166 | return -EINVAL; | |
167 | } | |
168 | return remap_pfn_range(vma, addr, pfn, PAGE_SIZE, | |
945537df | 169 | __pgprot(pgprot_val(prot) | H_PAGE_4K_PFN)); |
96270b1f | 170 | } |
721151d0 | 171 | |
dd1842a2 | 172 | #define H_PTE_TABLE_SIZE PTE_FRAG_SIZE |
4a7aa4fe | 173 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined (CONFIG_HUGETLB_PAGE) |
dd1842a2 AK |
174 | #define H_PMD_TABLE_SIZE ((sizeof(pmd_t) << PMD_INDEX_SIZE) + \ |
175 | (sizeof(unsigned long) << PMD_INDEX_SIZE)) | |
62607bc6 | 176 | #else |
dd1842a2 | 177 | #define H_PMD_TABLE_SIZE (sizeof(pmd_t) << PMD_INDEX_SIZE) |
62607bc6 | 178 | #endif |
fae22116 AK |
179 | #ifdef CONFIG_HUGETLB_PAGE |
180 | #define H_PUD_TABLE_SIZE ((sizeof(pud_t) << PUD_INDEX_SIZE) + \ | |
181 | (sizeof(unsigned long) << PUD_INDEX_SIZE)) | |
182 | #else | |
dd1842a2 | 183 | #define H_PUD_TABLE_SIZE (sizeof(pud_t) << PUD_INDEX_SIZE) |
fae22116 | 184 | #endif |
dd1842a2 | 185 | #define H_PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE) |
ab537dca | 186 | |
e34aa03c | 187 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
e34aa03c AK |
188 | static inline char *get_hpte_slot_array(pmd_t *pmdp) |
189 | { | |
190 | /* | |
191 | * The hpte hindex is stored in the pgtable whose address is in the | |
192 | * second half of the PMD | |
193 | * | |
194 | * Order this load with the test for pmd_trans_huge in the caller | |
195 | */ | |
196 | smp_rmb(); | |
197 | return *(char **)(pmdp + PTRS_PER_PMD); | |
198 | ||
199 | ||
200 | } | |
201 | /* | |
202 | * The linux hugepage PMD now include the pmd entries followed by the address | |
203 | * to the stashed pgtable_t. The stashed pgtable_t contains the hpte bits. | |
849f86a6 | 204 | * [ 000 | 1 bit secondary | 3 bit hidx | 1 bit valid]. We use one byte per |
e34aa03c AK |
205 | * each HPTE entry. With 16MB hugepage and 64K HPTE we need 256 entries and |
206 | * with 4K HPTE we need 4096 entries. Both will fit in a 4K pgtable_t. | |
207 | * | |
849f86a6 | 208 | * The top three bits are intentionally left as zero. This memory location |
e34aa03c AK |
209 | * are also used as normal page PTE pointers. So if we have any pointers |
210 | * left around while we collapse a hugepage, we need to make sure | |
211 | * _PAGE_PRESENT bit of that is zero when we look at them | |
212 | */ | |
213 | static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index) | |
214 | { | |
849f86a6 | 215 | return hpte_slot_array[index] & 0x1; |
e34aa03c AK |
216 | } |
217 | ||
218 | static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array, | |
219 | int index) | |
220 | { | |
849f86a6 | 221 | return hpte_slot_array[index] >> 1; |
e34aa03c AK |
222 | } |
223 | ||
224 | static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array, | |
225 | unsigned int index, unsigned int hidx) | |
226 | { | |
849f86a6 | 227 | hpte_slot_array[index] = (hidx << 1) | 0x1; |
e34aa03c AK |
228 | } |
229 | ||
230 | /* | |
231 | * | |
232 | * For core kernel code by design pmd_trans_huge is never run on any hugetlbfs | |
233 | * page. The hugetlbfs page table walking and mangling paths are totally | |
234 | * separated form the core VM paths and they're differentiated by | |
235 | * VM_HUGETLB being set on vm_flags well before any pmd_trans_huge could run. | |
236 | * | |
237 | * pmd_trans_huge() is defined as false at build time if | |
238 | * CONFIG_TRANSPARENT_HUGEPAGE=n to optimize away code blocks at build | |
239 | * time in such case. | |
240 | * | |
241 | * For ppc64 we need to differntiate from explicit hugepages from THP, because | |
242 | * for THP we also track the subpage details at the pmd level. We don't do | |
243 | * that for explicit huge pages. | |
244 | * | |
245 | */ | |
6cc1a0ee | 246 | static inline int hash__pmd_trans_huge(pmd_t pmd) |
e34aa03c | 247 | { |
945537df AK |
248 | return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE)) == |
249 | (_PAGE_PTE | H_PAGE_THP_HUGE)); | |
e34aa03c AK |
250 | } |
251 | ||
6cc1a0ee | 252 | static inline int hash__pmd_same(pmd_t pmd_a, pmd_t pmd_b) |
e34aa03c | 253 | { |
ee3caed3 | 254 | return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0); |
e34aa03c AK |
255 | } |
256 | ||
3df33f12 AK |
257 | static inline pmd_t hash__pmd_mkhuge(pmd_t pmd) |
258 | { | |
259 | return __pmd(pmd_val(pmd) | (_PAGE_PTE | H_PAGE_THP_HUGE)); | |
260 | } | |
261 | ||
262 | extern unsigned long hash__pmd_hugepage_update(struct mm_struct *mm, | |
263 | unsigned long addr, pmd_t *pmdp, | |
264 | unsigned long clr, unsigned long set); | |
265 | extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma, | |
266 | unsigned long address, pmd_t *pmdp); | |
267 | extern void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp, | |
268 | pgtable_t pgtable); | |
269 | extern pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp); | |
3df33f12 AK |
270 | extern pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm, |
271 | unsigned long addr, pmd_t *pmdp); | |
272 | extern int hash__has_transparent_hugepage(void); | |
e34aa03c | 273 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
c605782b | 274 | #endif /* __ASSEMBLY__ */ |
ab537dca AK |
275 | |
276 | #endif /* _ASM_POWERPC_BOOK3S_64_HASH_64K_H */ |