blob: fc0898eb309d8601759eda376a23d6786e1a417b [file] [log] [blame]
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +05301/*
2 * Copyright IBM Corporation, 2015
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 */
14
15#include <linux/mm.h>
16#include <asm/machdep.h>
17#include <asm/mmu.h>
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053018/*
19 * index from 0 - 15
20 */
21bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
22{
23 unsigned long g_idx;
24 unsigned long ptev = pte_val(rpte.pte);
25
26 g_idx = (ptev & _PAGE_COMBO_VALID) >> _PAGE_F_GIX_SHIFT;
27 index = index >> 2;
28 if (g_idx & (0x1 << index))
29 return true;
30 else
31 return false;
32}
33/*
34 * index from 0 - 15
35 */
36static unsigned long mark_subptegroup_valid(unsigned long ptev, unsigned long index)
37{
38 unsigned long g_idx;
39
40 if (!(ptev & _PAGE_COMBO))
41 return ptev;
42 index = index >> 2;
43 g_idx = 0x1 << index;
44
45 return ptev | (g_idx << _PAGE_F_GIX_SHIFT);
46}
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053047
48int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
49 pte_t *ptep, unsigned long trap, unsigned long flags,
50 int ssize, int subpg_prot)
51{
52 real_pte_t rpte;
53 unsigned long *hidxp;
54 unsigned long hpte_group;
55 unsigned int subpg_index;
56 unsigned long rflags, pa, hidx;
57 unsigned long old_pte, new_pte, subpg_pte;
58 unsigned long vpn, hash, slot;
59 unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
60
61 /*
62 * atomically mark the linux large page PTE busy and dirty
63 */
64 do {
65 pte_t pte = READ_ONCE(*ptep);
66
67 old_pte = pte_val(pte);
68 /* If PTE busy, retry the access */
69 if (unlikely(old_pte & _PAGE_BUSY))
70 return 0;
71 /* If PTE permissions don't match, take page fault */
72 if (unlikely(access & ~old_pte))
73 return 1;
74 /*
75 * Try to lock the PTE, add ACCESSED and DIRTY if it was
76 * a write access. Since this is 4K insert of 64K page size
77 * also add _PAGE_COMBO
78 */
79 new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED | _PAGE_COMBO;
80 if (access & _PAGE_RW)
81 new_pte |= _PAGE_DIRTY;
82 } while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
83 old_pte, new_pte));
84 /*
85 * Handle the subpage protection bits
86 */
87 subpg_pte = new_pte & ~subpg_prot;
88 /*
89 * PP bits. _PAGE_USER is already PP bit 0x2, so we only
90 * need to add in 0x1 if it's a read-only user page
91 */
92 rflags = subpg_pte & _PAGE_USER;
93 if ((subpg_pte & _PAGE_USER) && !((subpg_pte & _PAGE_RW) &&
94 (subpg_pte & _PAGE_DIRTY)))
95 rflags |= 0x1;
96 /*
97 * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
98 */
99 rflags |= ((subpg_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
100 /*
101 * Always add C and Memory coherence bit
102 */
103 rflags |= HPTE_R_C | HPTE_R_M;
104 /*
105 * Add in WIMG bits
106 */
107 rflags |= (subpg_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
108 _PAGE_COHERENT | _PAGE_GUARDED));
109
110 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
111 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
112
113 /*
114 * No CPU has hugepages but lacks no execute, so we
115 * don't need to worry about that case
116 */
117 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
118 }
119
120 subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
121 vpn = hpt_vpn(ea, vsid, ssize);
122 rpte = __real_pte(__pte(old_pte), ptep);
123 /*
124 *None of the sub 4k page is hashed
125 */
126 if (!(old_pte & _PAGE_HASHPTE))
127 goto htab_insert_hpte;
128 /*
129 * Check if the pte was already inserted into the hash table
130 * as a 64k HW page, and invalidate the 64k HPTE if so.
131 */
132 if (!(old_pte & _PAGE_COMBO)) {
133 flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +0530134 old_pte &= ~_PAGE_HASHPTE | _PAGE_F_GIX | _PAGE_F_SECOND;
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530135 goto htab_insert_hpte;
136 }
137 /*
138 * Check for sub page valid and update
139 */
140 if (__rpte_sub_valid(rpte, subpg_index)) {
141 int ret;
142
143 hash = hpt_hash(vpn, shift, ssize);
144 hidx = __rpte_to_hidx(rpte, subpg_index);
145 if (hidx & _PTEIDX_SECONDARY)
146 hash = ~hash;
147 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
148 slot += hidx & _PTEIDX_GROUP_IX;
149
150 ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
151 MMU_PAGE_4K, MMU_PAGE_4K,
152 ssize, flags);
153 /*
154 *if we failed because typically the HPTE wasn't really here
155 * we try an insertion.
156 */
157 if (ret == -1)
158 goto htab_insert_hpte;
159
160 *ptep = __pte(new_pte & ~_PAGE_BUSY);
161 return 0;
162 }
163
164htab_insert_hpte:
165 /*
166 * handle _PAGE_4K_PFN case
167 */
168 if (old_pte & _PAGE_4K_PFN) {
169 /*
170 * All the sub 4k page have the same
171 * physical address.
172 */
173 pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
174 } else {
175 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
176 pa += (subpg_index << shift);
177 }
178 hash = hpt_hash(vpn, shift, ssize);
179repeat:
180 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
181
182 /* Insert into the hash table, primary slot */
183 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
184 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
185 /*
186 * Primary is full, try the secondary
187 */
188 if (unlikely(slot == -1)) {
189 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
190 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
191 rflags, HPTE_V_SECONDARY,
192 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
193 if (slot == -1) {
194 if (mftb() & 0x1)
195 hpte_group = ((hash & htab_hash_mask) *
196 HPTES_PER_GROUP) & ~0x7UL;
197 ppc_md.hpte_remove(hpte_group);
198 /*
199 * FIXME!! Should be try the group from which we removed ?
200 */
201 goto repeat;
202 }
203 }
204 /*
205 * Hypervisor failure. Restore old pmd and return -1
206 * similar to __hash_page_*
207 */
208 if (unlikely(slot == -2)) {
209 *ptep = __pte(old_pte);
210 hash_failure_debug(ea, access, vsid, trap, ssize,
211 MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
212 return -1;
213 }
214 /*
215 * Insert slot number & secondary bit in PTE second half,
216 * clear _PAGE_BUSY and set appropriate HPTE slot bit
217 * Since we have _PAGE_BUSY set on ptep, we can be sure
218 * nobody is undating hidx.
219 */
220 hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530221 rpte.hidx &= ~(0xfUL << (subpg_index << 2));
222 *hidxp = rpte.hidx | (slot << (subpg_index << 2));
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +0530223 new_pte = mark_subptegroup_valid(new_pte, subpg_index);
224 new_pte |= _PAGE_HASHPTE;
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530225 /*
226 * check __real_pte for details on matching smp_rmb()
227 */
228 smp_wmb();
229 *ptep = __pte(new_pte & ~_PAGE_BUSY);
230 return 0;
231}
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530232
233int __hash_page_64K(unsigned long ea, unsigned long access,
234 unsigned long vsid, pte_t *ptep, unsigned long trap,
235 unsigned long flags, int ssize)
236{
237
238 unsigned long hpte_group;
239 unsigned long rflags, pa;
240 unsigned long old_pte, new_pte;
241 unsigned long vpn, hash, slot;
242 unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
243
244 /*
245 * atomically mark the linux large page PTE busy and dirty
246 */
247 do {
248 pte_t pte = READ_ONCE(*ptep);
249
250 old_pte = pte_val(pte);
251 /* If PTE busy, retry the access */
252 if (unlikely(old_pte & _PAGE_BUSY))
253 return 0;
254 /* If PTE permissions don't match, take page fault */
255 if (unlikely(access & ~old_pte))
256 return 1;
257 /*
258 * Check if PTE has the cache-inhibit bit set
259 * If so, bail out and refault as a 4k page
260 */
261 if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
262 unlikely(old_pte & _PAGE_NO_CACHE))
263 return 0;
264 /*
265 * Try to lock the PTE, add ACCESSED and DIRTY if it was
266 * a write access. Since this is 4K insert of 64K page size
267 * also add _PAGE_COMBO
268 */
269 new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
270 if (access & _PAGE_RW)
271 new_pte |= _PAGE_DIRTY;
272 } while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
273 old_pte, new_pte));
274 /*
275 * PP bits. _PAGE_USER is already PP bit 0x2, so we only
276 * need to add in 0x1 if it's a read-only user page
277 */
278 rflags = new_pte & _PAGE_USER;
279 if ((new_pte & _PAGE_USER) && !((new_pte & _PAGE_RW) &&
280 (new_pte & _PAGE_DIRTY)))
281 rflags |= 0x1;
282 /*
283 * _PAGE_EXEC -> HW_NO_EXEC since it's inverted
284 */
285 rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);
286 /*
287 * Always add C and Memory coherence bit
288 */
289 rflags |= HPTE_R_C | HPTE_R_M;
290 /*
291 * Add in WIMG bits
292 */
293 rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
294 _PAGE_COHERENT | _PAGE_GUARDED));
295
296 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
297 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
298 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
299
300 vpn = hpt_vpn(ea, vsid, ssize);
301 if (unlikely(old_pte & _PAGE_HASHPTE)) {
302 /*
303 * There MIGHT be an HPTE for this pte
304 */
305 hash = hpt_hash(vpn, shift, ssize);
306 if (old_pte & _PAGE_F_SECOND)
307 hash = ~hash;
308 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
309 slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT;
310
311 if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K,
312 MMU_PAGE_64K, ssize, flags) == -1)
313 old_pte &= ~_PAGE_HPTEFLAGS;
314 }
315
316 if (likely(!(old_pte & _PAGE_HASHPTE))) {
317
318 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
319 hash = hpt_hash(vpn, shift, ssize);
320
321repeat:
322 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
323
324 /* Insert into the hash table, primary slot */
325 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
326 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
327 /*
328 * Primary is full, try the secondary
329 */
330 if (unlikely(slot == -1)) {
331 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
332 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
333 rflags, HPTE_V_SECONDARY,
334 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
335 if (slot == -1) {
336 if (mftb() & 0x1)
337 hpte_group = ((hash & htab_hash_mask) *
338 HPTES_PER_GROUP) & ~0x7UL;
339 ppc_md.hpte_remove(hpte_group);
340 /*
341 * FIXME!! Should be try the group from which we removed ?
342 */
343 goto repeat;
344 }
345 }
346 /*
347 * Hypervisor failure. Restore old pmd and return -1
348 * similar to __hash_page_*
349 */
350 if (unlikely(slot == -2)) {
351 *ptep = __pte(old_pte);
352 hash_failure_debug(ea, access, vsid, trap, ssize,
353 MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
354 return -1;
355 }
356 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
357 new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX);
358 }
359 *ptep = __pte(new_pte & ~_PAGE_BUSY);
360 return 0;
361}