1
2#ifndef _ASM_POWERPC_BOOK3S_64_HASH_64K_H
3#define _ASM_POWERPC_BOOK3S_64_HASH_64K_H
4
5#define H_PTE_INDEX_SIZE 8
6#define H_PMD_INDEX_SIZE 10
7#define H_PUD_INDEX_SIZE 10
8#define H_PGD_INDEX_SIZE 8
9
10
11
12
13
14
15
16
17
18#if defined(CONFIG_SPARSEMEM_VMEMMAP) && defined(CONFIG_SPARSEMEM_EXTREME)
19#define H_MAX_PHYSMEM_BITS 51
20#else
21#define H_MAX_PHYSMEM_BITS 46
22#endif
23
24
25
26
27
28#define MAX_EA_BITS_PER_CONTEXT 49
29#define REGION_SHIFT MAX_EA_BITS_PER_CONTEXT
30
31
32
33
34#define H_KERN_MAP_SIZE (1UL << MAX_EA_BITS_PER_CONTEXT)
35
36
37
38
39
40#define H_KERN_VIRT_START ASM_CONST(0xc008000000000000)
41
42
43
44
45
46#define H_PAGE_COMBO _RPAGE_RPN0
47#define H_PAGE_4K_PFN _RPAGE_RPN1
48#define H_PAGE_BUSY _RPAGE_RSV1
49#define H_PAGE_HASHPTE _RPAGE_RPN43
50
51
52#define H_PTE_PKEY_BIT4 _RPAGE_PKEY_BIT4
53#define H_PTE_PKEY_BIT3 _RPAGE_PKEY_BIT3
54#define H_PTE_PKEY_BIT2 _RPAGE_PKEY_BIT2
55#define H_PTE_PKEY_BIT1 _RPAGE_PKEY_BIT1
56#define H_PTE_PKEY_BIT0 _RPAGE_PKEY_BIT0
57
58
59
60
61
62#define H_PAGE_THP_HUGE H_PAGE_4K_PFN
63
64
65#define _PAGE_HPTEFLAGS (H_PAGE_BUSY | H_PAGE_HASHPTE | H_PAGE_COMBO)
66
67
68
69
70
71
72#define H_PTE_FRAG_SIZE_SHIFT (H_PTE_INDEX_SIZE + 3 + 1)
73#define H_PTE_FRAG_NR (PAGE_SIZE >> H_PTE_FRAG_SIZE_SHIFT)
74
75#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined(CONFIG_HUGETLB_PAGE)
76#define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3 + 1)
77#else
78#define H_PMD_FRAG_SIZE_SHIFT (H_PMD_INDEX_SIZE + 3)
79#endif
80#define H_PMD_FRAG_NR (PAGE_SIZE >> H_PMD_FRAG_SIZE_SHIFT)
81
82#ifndef __ASSEMBLY__
83#include <asm/errno.h>
84
85
86
87
88
89
90
91#define __real_pte __real_pte
92static inline real_pte_t __real_pte(pte_t pte, pte_t *ptep, int offset)
93{
94 real_pte_t rpte;
95 unsigned long *hidxp;
96
97 rpte.pte = pte;
98
99
100
101
102
103
104 smp_rmb();
105
106 hidxp = (unsigned long *)(ptep + offset);
107 rpte.hidx = *hidxp;
108 return rpte;
109}
110
111
112
113
114
115
116
117#define HIDX_UNSHIFT_BY_ONE(x) ((x + 0xfUL) & 0xfUL)
118#define HIDX_SHIFT_BY_ONE(x) ((x + 0x1UL) & 0xfUL)
119#define HIDX_BITS(x, index) (x << (index << 2))
120#define BITS_TO_HIDX(x, index) ((x >> (index << 2)) & 0xfUL)
121#define INVALID_RPTE_HIDX 0x0UL
122
123static inline unsigned long __rpte_to_hidx(real_pte_t rpte, unsigned long index)
124{
125 return HIDX_UNSHIFT_BY_ONE(BITS_TO_HIDX(rpte.hidx, index));
126}
127
128
129
130
131
132static inline unsigned long pte_set_hidx(pte_t *ptep, real_pte_t rpte,
133 unsigned int subpg_index,
134 unsigned long hidx, int offset)
135{
136 unsigned long *hidxp = (unsigned long *)(ptep + offset);
137
138 rpte.hidx &= ~HIDX_BITS(0xfUL, subpg_index);
139 *hidxp = rpte.hidx | HIDX_BITS(HIDX_SHIFT_BY_ONE(hidx), subpg_index);
140
141
142
143
144
145
146 smp_wmb();
147
148
149 return 0x0UL;
150}
151
152#define __rpte_to_pte(r) ((r).pte)
153extern bool __rpte_sub_valid(real_pte_t rpte, unsigned long index);
154
155
156
157
158#define pte_iterate_hashed_subpages(rpte, psize, vpn, index, shift) \
159 do { \
160 unsigned long __end = vpn + (1UL << (PAGE_SHIFT - VPN_SHIFT)); \
161 unsigned __split = (psize == MMU_PAGE_4K || \
162 psize == MMU_PAGE_64K_AP); \
163 shift = mmu_psize_defs[psize].shift; \
164 for (index = 0; vpn < __end; index++, \
165 vpn += (1L << (shift - VPN_SHIFT))) { \
166 if (!__split || __rpte_sub_valid(rpte, index))
167
168#define pte_iterate_hashed_end() } } while(0)
169
170#define pte_pagesize_index(mm, addr, pte) \
171 (((pte) & H_PAGE_COMBO)? MMU_PAGE_4K: MMU_PAGE_64K)
172
173extern int remap_pfn_range(struct vm_area_struct *, unsigned long addr,
174 unsigned long pfn, unsigned long size, pgprot_t);
175static inline int hash__remap_4k_pfn(struct vm_area_struct *vma, unsigned long addr,
176 unsigned long pfn, pgprot_t prot)
177{
178 if (pfn > (PTE_RPN_MASK >> PAGE_SHIFT)) {
179 WARN(1, "remap_4k_pfn called with wrong pfn value\n");
180 return -EINVAL;
181 }
182 return remap_pfn_range(vma, addr, pfn, PAGE_SIZE,
183 __pgprot(pgprot_val(prot) | H_PAGE_4K_PFN));
184}
185
186#define H_PTE_TABLE_SIZE PTE_FRAG_SIZE
187#if defined(CONFIG_TRANSPARENT_HUGEPAGE) || defined (CONFIG_HUGETLB_PAGE)
188#define H_PMD_TABLE_SIZE ((sizeof(pmd_t) << PMD_INDEX_SIZE) + \
189 (sizeof(unsigned long) << PMD_INDEX_SIZE))
190#else
191#define H_PMD_TABLE_SIZE (sizeof(pmd_t) << PMD_INDEX_SIZE)
192#endif
193#ifdef CONFIG_HUGETLB_PAGE
194#define H_PUD_TABLE_SIZE ((sizeof(pud_t) << PUD_INDEX_SIZE) + \
195 (sizeof(unsigned long) << PUD_INDEX_SIZE))
196#else
197#define H_PUD_TABLE_SIZE (sizeof(pud_t) << PUD_INDEX_SIZE)
198#endif
199#define H_PGD_TABLE_SIZE (sizeof(pgd_t) << PGD_INDEX_SIZE)
200
201#ifdef CONFIG_TRANSPARENT_HUGEPAGE
202static inline char *get_hpte_slot_array(pmd_t *pmdp)
203{
204
205
206
207
208
209
210 smp_rmb();
211 return *(char **)(pmdp + PTRS_PER_PMD);
212
213
214}
215
216
217
218
219
220
221
222
223
224
225
226
227static inline unsigned int hpte_valid(unsigned char *hpte_slot_array, int index)
228{
229 return hpte_slot_array[index] & 0x1;
230}
231
232static inline unsigned int hpte_hash_index(unsigned char *hpte_slot_array,
233 int index)
234{
235 return hpte_slot_array[index] >> 1;
236}
237
238static inline void mark_hpte_slot_valid(unsigned char *hpte_slot_array,
239 unsigned int index, unsigned int hidx)
240{
241 hpte_slot_array[index] = (hidx << 1) | 0x1;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260static inline int hash__pmd_trans_huge(pmd_t pmd)
261{
262 return !!((pmd_val(pmd) & (_PAGE_PTE | H_PAGE_THP_HUGE | _PAGE_DEVMAP)) ==
263 (_PAGE_PTE | H_PAGE_THP_HUGE));
264}
265
266static inline int hash__pmd_same(pmd_t pmd_a, pmd_t pmd_b)
267{
268 return (((pmd_raw(pmd_a) ^ pmd_raw(pmd_b)) & ~cpu_to_be64(_PAGE_HPTEFLAGS)) == 0);
269}
270
271static inline pmd_t hash__pmd_mkhuge(pmd_t pmd)
272{
273 return __pmd(pmd_val(pmd) | (_PAGE_PTE | H_PAGE_THP_HUGE));
274}
275
276extern unsigned long hash__pmd_hugepage_update(struct mm_struct *mm,
277 unsigned long addr, pmd_t *pmdp,
278 unsigned long clr, unsigned long set);
279extern pmd_t hash__pmdp_collapse_flush(struct vm_area_struct *vma,
280 unsigned long address, pmd_t *pmdp);
281extern void hash__pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
282 pgtable_t pgtable);
283extern pgtable_t hash__pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp);
284extern pmd_t hash__pmdp_huge_get_and_clear(struct mm_struct *mm,
285 unsigned long addr, pmd_t *pmdp);
286extern int hash__has_transparent_hugepage(void);
287#endif
288
289static inline pmd_t hash__pmd_mkdevmap(pmd_t pmd)
290{
291 return __pmd(pmd_val(pmd) | (_PAGE_PTE | H_PAGE_THP_HUGE | _PAGE_DEVMAP));
292}
293
294#endif
295
296#endif
297