1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/mm.h>
16#include <asm/machdep.h>
17#include <asm/mmu.h>
18
19
20
21
22
23static inline bool hpte_soft_invalid(unsigned long hidx)
24{
25 return ((hidx & 0xfUL) == 0xfUL);
26}
27
28
29
30
31bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
32{
33 return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index)));
34}
35
36int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
37 pte_t *ptep, unsigned long trap, unsigned long flags,
38 int ssize, int subpg_prot)
39{
40 real_pte_t rpte;
41 unsigned long hpte_group;
42 unsigned int subpg_index;
43 unsigned long rflags, pa;
44 unsigned long old_pte, new_pte, subpg_pte;
45 unsigned long vpn, hash, slot, gslot;
46 unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
47
48
49
50
51 do {
52 pte_t pte = READ_ONCE(*ptep);
53
54 old_pte = pte_val(pte);
55
56 if (unlikely(old_pte & H_PAGE_BUSY))
57 return 0;
58
59 if (unlikely(!check_pte_access(access, old_pte)))
60 return 1;
61
62
63
64
65
66 new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
67 if (access & _PAGE_WRITE)
68 new_pte |= _PAGE_DIRTY;
69 } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
70
71
72
73
74 subpg_pte = new_pte & ~subpg_prot;
75 rflags = htab_convert_pte_flags(subpg_pte, flags);
76
77 if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
78 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
79
80
81
82
83
84 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
85 }
86
87 subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
88 vpn = hpt_vpn(ea, vsid, ssize);
89 rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
90
91
92
93 if (!(old_pte & H_PAGE_HASHPTE))
94 goto htab_insert_hpte;
95
96
97
98
99 if (!(old_pte & H_PAGE_COMBO)) {
100 flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
101
102
103
104
105
106 old_pte &= ~H_PAGE_HASHPTE;
107 new_pte &= ~H_PAGE_HASHPTE;
108 goto htab_insert_hpte;
109 }
110
111
112
113 if (__rpte_sub_valid(rpte, subpg_index)) {
114 int ret;
115
116 gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte,
117 subpg_index);
118 ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn,
119 MMU_PAGE_4K, MMU_PAGE_4K,
120 ssize, flags);
121
122
123
124
125
126 if (ret == -1)
127 goto htab_insert_hpte;
128
129 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
130 return 0;
131 }
132
133htab_insert_hpte:
134
135
136
137
138
139 if (!(old_pte & H_PAGE_COMBO))
140 rpte.hidx = INVALID_RPTE_HIDX;
141
142
143
144
145 if (old_pte & H_PAGE_4K_PFN) {
146
147
148
149
150 pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
151 } else {
152 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
153 pa += (subpg_index << shift);
154 }
155 hash = hpt_hash(vpn, shift, ssize);
156repeat:
157 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
158
159
160 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
161 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
162
163
164
165 if (unlikely(slot == -1)) {
166 bool soft_invalid;
167
168 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
169 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
170 rflags, HPTE_V_SECONDARY,
171 MMU_PAGE_4K, MMU_PAGE_4K,
172 ssize);
173
174 soft_invalid = hpte_soft_invalid(slot);
175 if (unlikely(soft_invalid)) {
176
177
178
179
180
181
182 gslot = slot & _PTEIDX_GROUP_IX;
183 mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn,
184 MMU_PAGE_4K, MMU_PAGE_4K,
185 ssize, 0);
186 }
187
188 if (unlikely(slot == -1 || soft_invalid)) {
189
190
191
192
193
194
195 if (soft_invalid || (mftb() & 0x1))
196 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
197
198 mmu_hash_ops.hpte_remove(hpte_group);
199
200
201
202 goto repeat;
203 }
204 }
205
206
207
208
209 if (unlikely(slot == -2)) {
210 *ptep = __pte(old_pte);
211 hash_failure_debug(ea, access, vsid, trap, ssize,
212 MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
213 return -1;
214 }
215
216 new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
217 new_pte |= H_PAGE_HASHPTE;
218
219 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
220 return 0;
221}
222
223int __hash_page_64K(unsigned long ea, unsigned long access,
224 unsigned long vsid, pte_t *ptep, unsigned long trap,
225 unsigned long flags, int ssize)
226{
227 real_pte_t rpte;
228 unsigned long hpte_group;
229 unsigned long rflags, pa;
230 unsigned long old_pte, new_pte;
231 unsigned long vpn, hash, slot;
232 unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
233
234
235
236
237 do {
238 pte_t pte = READ_ONCE(*ptep);
239
240 old_pte = pte_val(pte);
241
242 if (unlikely(old_pte & H_PAGE_BUSY))
243 return 0;
244
245 if (unlikely(!check_pte_access(access, old_pte)))
246 return 1;
247
248
249
250
251 if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
252 unlikely(pte_ci(pte)))
253 return 0;
254
255
256
257
258 new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
259 if (access & _PAGE_WRITE)
260 new_pte |= _PAGE_DIRTY;
261 } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
262
263 rflags = htab_convert_pte_flags(new_pte, flags);
264 rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
265
266 if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
267 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
268 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
269
270 vpn = hpt_vpn(ea, vsid, ssize);
271 if (unlikely(old_pte & H_PAGE_HASHPTE)) {
272 unsigned long gslot;
273
274
275
276
277 gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
278 if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K,
279 MMU_PAGE_64K, ssize,
280 flags) == -1)
281 old_pte &= ~_PAGE_HPTEFLAGS;
282 }
283
284 if (likely(!(old_pte & H_PAGE_HASHPTE))) {
285
286 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
287 hash = hpt_hash(vpn, shift, ssize);
288
289repeat:
290 hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
291
292
293 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
294 MMU_PAGE_64K, MMU_PAGE_64K,
295 ssize);
296
297
298
299 if (unlikely(slot == -1)) {
300 hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
301 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
302 rflags,
303 HPTE_V_SECONDARY,
304 MMU_PAGE_64K,
305 MMU_PAGE_64K, ssize);
306 if (slot == -1) {
307 if (mftb() & 0x1)
308 hpte_group = (hash & htab_hash_mask) *
309 HPTES_PER_GROUP;
310 mmu_hash_ops.hpte_remove(hpte_group);
311
312
313
314 goto repeat;
315 }
316 }
317
318
319
320
321 if (unlikely(slot == -2)) {
322 *ptep = __pte(old_pte);
323 hash_failure_debug(ea, access, vsid, trap, ssize,
324 MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
325 return -1;
326 }
327
328 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
329 new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
330 }
331 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
332 return 0;
333}
334