blob: 1a68cb19b0e33c0031a1568f9e977f1681b80cd8 [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
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100026 g_idx = (ptev & H_PAGE_COMBO_VALID) >> H_PAGE_F_GIX_SHIFT;
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053027 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
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100040 if (!(ptev & H_PAGE_COMBO))
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053041 return ptev;
42 index = index >> 2;
43 g_idx = 0x1 << index;
44
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100045 return ptev | (g_idx << H_PAGE_F_GIX_SHIFT);
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +053046}
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 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100069 if (unlikely(old_pte & H_PAGE_BUSY))
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053070 return 0;
71 /* If PTE permissions don't match, take page fault */
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +100072 if (unlikely(!check_pte_access(access, old_pte)))
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053073 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
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100077 * also add H_PAGE_COMBO
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053078 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +100079 new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +100080 if (access & _PAGE_WRITE)
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053081 new_pte |= _PAGE_DIRTY;
Michael Ellerman3910a7f2016-04-29 23:25:27 +100082 } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
83
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053084 /*
85 * Handle the subpage protection bits
86 */
87 subpg_pte = new_pte & ~subpg_prot;
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +053088 rflags = htab_convert_pte_flags(subpg_pte);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053089
Benjamin Herrenschmidtdd7b2f02016-11-29 13:13:46 +110090 if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +053091 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
92
93 /*
94 * No CPU has hugepages but lacks no execute, so we
95 * don't need to worry about that case
96 */
97 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
98 }
99
100 subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
101 vpn = hpt_vpn(ea, vsid, ssize);
102 rpte = __real_pte(__pte(old_pte), ptep);
103 /*
104 *None of the sub 4k page is hashed
105 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000106 if (!(old_pte & H_PAGE_HASHPTE))
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530107 goto htab_insert_hpte;
108 /*
109 * Check if the pte was already inserted into the hash table
110 * as a 64k HW page, and invalidate the 64k HPTE if so.
111 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000112 if (!(old_pte & H_PAGE_COMBO)) {
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530113 flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
Aneesh Kumar K.V9ab3ac22016-02-20 20:41:54 +0530114 /*
115 * clear the old slot details from the old and new pte.
116 * On hash insert failure we use old pte value and we don't
117 * want slot information there if we have a insert failure.
118 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000119 old_pte &= ~(H_PAGE_HASHPTE | H_PAGE_F_GIX | H_PAGE_F_SECOND);
120 new_pte &= ~(H_PAGE_HASHPTE | H_PAGE_F_GIX | H_PAGE_F_SECOND);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530121 goto htab_insert_hpte;
122 }
123 /*
124 * Check for sub page valid and update
125 */
126 if (__rpte_sub_valid(rpte, subpg_index)) {
127 int ret;
128
129 hash = hpt_hash(vpn, shift, ssize);
130 hidx = __rpte_to_hidx(rpte, subpg_index);
131 if (hidx & _PTEIDX_SECONDARY)
132 hash = ~hash;
133 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
134 slot += hidx & _PTEIDX_GROUP_IX;
135
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000136 ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
137 MMU_PAGE_4K, MMU_PAGE_4K,
138 ssize, flags);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530139 /*
140 *if we failed because typically the HPTE wasn't really here
141 * we try an insertion.
142 */
143 if (ret == -1)
144 goto htab_insert_hpte;
145
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000146 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530147 return 0;
148 }
149
150htab_insert_hpte:
151 /*
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000152 * handle H_PAGE_4K_PFN case
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530153 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000154 if (old_pte & H_PAGE_4K_PFN) {
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530155 /*
156 * All the sub 4k page have the same
157 * physical address.
158 */
159 pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
160 } else {
161 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
162 pa += (subpg_index << shift);
163 }
164 hash = hpt_hash(vpn, shift, ssize);
165repeat:
166 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
167
168 /* Insert into the hash table, primary slot */
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000169 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
170 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530171 /*
172 * Primary is full, try the secondary
173 */
174 if (unlikely(slot == -1)) {
175 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000176 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
177 rflags, HPTE_V_SECONDARY,
178 MMU_PAGE_4K, MMU_PAGE_4K,
179 ssize);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530180 if (slot == -1) {
181 if (mftb() & 0x1)
182 hpte_group = ((hash & htab_hash_mask) *
183 HPTES_PER_GROUP) & ~0x7UL;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000184 mmu_hash_ops.hpte_remove(hpte_group);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530185 /*
186 * FIXME!! Should be try the group from which we removed ?
187 */
188 goto repeat;
189 }
190 }
191 /*
Aneesh Kumar K.Ve9a68142016-03-01 12:59:17 +0530192 * Hypervisor failure. Restore old pte and return -1
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530193 * similar to __hash_page_*
194 */
195 if (unlikely(slot == -2)) {
196 *ptep = __pte(old_pte);
197 hash_failure_debug(ea, access, vsid, trap, ssize,
198 MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
199 return -1;
200 }
201 /*
202 * Insert slot number & secondary bit in PTE second half,
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000203 * clear H_PAGE_BUSY and set appropriate HPTE slot bit
204 * Since we have H_PAGE_BUSY set on ptep, we can be sure
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530205 * nobody is undating hidx.
206 */
207 hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530208 rpte.hidx &= ~(0xfUL << (subpg_index << 2));
209 *hidxp = rpte.hidx | (slot << (subpg_index << 2));
Aneesh Kumar K.Vbf680d52015-12-01 09:06:45 +0530210 new_pte = mark_subptegroup_valid(new_pte, subpg_index);
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000211 new_pte |= H_PAGE_HASHPTE;
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530212 /*
213 * check __real_pte for details on matching smp_rmb()
214 */
215 smp_wmb();
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000216 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
Aneesh Kumar K.V91f1da92015-12-01 09:06:43 +0530217 return 0;
218}
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530219
220int __hash_page_64K(unsigned long ea, unsigned long access,
221 unsigned long vsid, pte_t *ptep, unsigned long trap,
222 unsigned long flags, int ssize)
223{
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530224 unsigned long hpte_group;
225 unsigned long rflags, pa;
226 unsigned long old_pte, new_pte;
227 unsigned long vpn, hash, slot;
228 unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
229
230 /*
231 * atomically mark the linux large page PTE busy and dirty
232 */
233 do {
234 pte_t pte = READ_ONCE(*ptep);
235
236 old_pte = pte_val(pte);
237 /* If PTE busy, retry the access */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000238 if (unlikely(old_pte & H_PAGE_BUSY))
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530239 return 0;
240 /* If PTE permissions don't match, take page fault */
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +1000241 if (unlikely(!check_pte_access(access, old_pte)))
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530242 return 1;
243 /*
244 * Check if PTE has the cache-inhibit bit set
245 * If so, bail out and refault as a 4k page
246 */
247 if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
Aneesh Kumar K.V30bda412016-04-29 23:25:38 +1000248 unlikely(pte_ci(pte)))
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530249 return 0;
250 /*
251 * Try to lock the PTE, add ACCESSED and DIRTY if it was
Paul Mackerras1ec3f932016-02-22 13:41:12 +1100252 * a write access.
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530253 */
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000254 new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000255 if (access & _PAGE_WRITE)
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530256 new_pte |= _PAGE_DIRTY;
Michael Ellerman3910a7f2016-04-29 23:25:27 +1000257 } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
Aneesh Kumar K.Vc6a3c492015-12-01 09:06:50 +0530258
259 rflags = htab_convert_pte_flags(new_pte);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530260
Benjamin Herrenschmidtdd7b2f02016-11-29 13:13:46 +1100261 if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530262 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
263 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
264
265 vpn = hpt_vpn(ea, vsid, ssize);
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000266 if (unlikely(old_pte & H_PAGE_HASHPTE)) {
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530267 /*
268 * There MIGHT be an HPTE for this pte
269 */
270 hash = hpt_hash(vpn, shift, ssize);
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000271 if (old_pte & H_PAGE_F_SECOND)
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530272 hash = ~hash;
273 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000274 slot += (old_pte & H_PAGE_F_GIX) >> H_PAGE_F_GIX_SHIFT;
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530275
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000276 if (mmu_hash_ops.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K,
277 MMU_PAGE_64K, ssize,
278 flags) == -1)
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530279 old_pte &= ~_PAGE_HPTEFLAGS;
280 }
281
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000282 if (likely(!(old_pte & H_PAGE_HASHPTE))) {
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530283
284 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
285 hash = hpt_hash(vpn, shift, ssize);
286
287repeat:
288 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
289
290 /* Insert into the hash table, primary slot */
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000291 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
292 MMU_PAGE_64K, MMU_PAGE_64K,
293 ssize);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530294 /*
295 * Primary is full, try the secondary
296 */
297 if (unlikely(slot == -1)) {
298 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000299 slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
300 rflags,
301 HPTE_V_SECONDARY,
302 MMU_PAGE_64K,
303 MMU_PAGE_64K, ssize);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530304 if (slot == -1) {
305 if (mftb() & 0x1)
306 hpte_group = ((hash & htab_hash_mask) *
307 HPTES_PER_GROUP) & ~0x7UL;
Benjamin Herrenschmidt70257762016-07-05 15:03:58 +1000308 mmu_hash_ops.hpte_remove(hpte_group);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530309 /*
310 * FIXME!! Should be try the group from which we removed ?
311 */
312 goto repeat;
313 }
314 }
315 /*
Aneesh Kumar K.Ve9a68142016-03-01 12:59:17 +0530316 * Hypervisor failure. Restore old pte and return -1
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530317 * similar to __hash_page_*
318 */
319 if (unlikely(slot == -2)) {
320 *ptep = __pte(old_pte);
321 hash_failure_debug(ea, access, vsid, trap, ssize,
322 MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
323 return -1;
324 }
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000325 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
326 new_pte |= (slot << H_PAGE_F_GIX_SHIFT) &
327 (H_PAGE_F_SECOND | H_PAGE_F_GIX);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530328 }
Aneesh Kumar K.V945537d2016-04-29 23:25:45 +1000329 *ptep = __pte(new_pte & ~H_PAGE_BUSY);
Aneesh Kumar K.V89ff7252015-12-01 09:06:48 +0530330 return 0;
331}