]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/powerpc/mm/hash64_64k.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / mm / hash64_64k.c
1 /*
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>
18 /*
19 * index from 0 - 15
20 */
21 bool __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 */
36 static 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 }
47
48 int __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 rflags = htab_convert_pte_flags(subpg_pte);
89
90 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
91 !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 */
106 if (!(old_pte & _PAGE_HASHPTE))
107 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 */
112 if (!(old_pte & _PAGE_COMBO)) {
113 flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
114 /*
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 */
119 old_pte &= ~(_PAGE_HASHPTE | _PAGE_F_GIX | _PAGE_F_SECOND);
120 new_pte &= ~(_PAGE_HASHPTE | _PAGE_F_GIX | _PAGE_F_SECOND);
121 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
136 ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
137 MMU_PAGE_4K, MMU_PAGE_4K,
138 ssize, flags);
139 /*
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
146 *ptep = __pte(new_pte & ~_PAGE_BUSY);
147 return 0;
148 }
149
150 htab_insert_hpte:
151 /*
152 * handle _PAGE_4K_PFN case
153 */
154 if (old_pte & _PAGE_4K_PFN) {
155 /*
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);
165 repeat:
166 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
167
168 /* Insert into the hash table, primary slot */
169 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
170 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
171 /*
172 * Primary is full, try the secondary
173 */
174 if (unlikely(slot == -1)) {
175 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
176 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
177 rflags, HPTE_V_SECONDARY,
178 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
179 if (slot == -1) {
180 if (mftb() & 0x1)
181 hpte_group = ((hash & htab_hash_mask) *
182 HPTES_PER_GROUP) & ~0x7UL;
183 ppc_md.hpte_remove(hpte_group);
184 /*
185 * FIXME!! Should be try the group from which we removed ?
186 */
187 goto repeat;
188 }
189 }
190 /*
191 * Hypervisor failure. Restore old pmd and return -1
192 * similar to __hash_page_*
193 */
194 if (unlikely(slot == -2)) {
195 *ptep = __pte(old_pte);
196 hash_failure_debug(ea, access, vsid, trap, ssize,
197 MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
198 return -1;
199 }
200 /*
201 * Insert slot number & secondary bit in PTE second half,
202 * clear _PAGE_BUSY and set appropriate HPTE slot bit
203 * Since we have _PAGE_BUSY set on ptep, we can be sure
204 * nobody is undating hidx.
205 */
206 hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
207 rpte.hidx &= ~(0xfUL << (subpg_index << 2));
208 *hidxp = rpte.hidx | (slot << (subpg_index << 2));
209 new_pte = mark_subptegroup_valid(new_pte, subpg_index);
210 new_pte |= _PAGE_HASHPTE;
211 /*
212 * check __real_pte for details on matching smp_rmb()
213 */
214 smp_wmb();
215 *ptep = __pte(new_pte & ~_PAGE_BUSY);
216 return 0;
217 }
218
219 int __hash_page_64K(unsigned long ea, unsigned long access,
220 unsigned long vsid, pte_t *ptep, unsigned long trap,
221 unsigned long flags, int ssize)
222 {
223
224 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 */
238 if (unlikely(old_pte & _PAGE_BUSY))
239 return 0;
240 /* If PTE permissions don't match, take page fault */
241 if (unlikely(access & ~old_pte))
242 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) &&
248 unlikely(old_pte & _PAGE_NO_CACHE))
249 return 0;
250 /*
251 * Try to lock the PTE, add ACCESSED and DIRTY if it was
252 * a write access. Since this is 4K insert of 64K page size
253 * also add _PAGE_COMBO
254 */
255 new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
256 if (access & _PAGE_RW)
257 new_pte |= _PAGE_DIRTY;
258 } while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
259 old_pte, new_pte));
260
261 rflags = htab_convert_pte_flags(new_pte);
262
263 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
264 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
265 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
266
267 vpn = hpt_vpn(ea, vsid, ssize);
268 if (unlikely(old_pte & _PAGE_HASHPTE)) {
269 /*
270 * There MIGHT be an HPTE for this pte
271 */
272 hash = hpt_hash(vpn, shift, ssize);
273 if (old_pte & _PAGE_F_SECOND)
274 hash = ~hash;
275 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
276 slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT;
277
278 if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K,
279 MMU_PAGE_64K, ssize, flags) == -1)
280 old_pte &= ~_PAGE_HPTEFLAGS;
281 }
282
283 if (likely(!(old_pte & _PAGE_HASHPTE))) {
284
285 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
286 hash = hpt_hash(vpn, shift, ssize);
287
288 repeat:
289 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
290
291 /* Insert into the hash table, primary slot */
292 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
293 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
294 /*
295 * Primary is full, try the secondary
296 */
297 if (unlikely(slot == -1)) {
298 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
299 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
300 rflags, HPTE_V_SECONDARY,
301 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
302 if (slot == -1) {
303 if (mftb() & 0x1)
304 hpte_group = ((hash & htab_hash_mask) *
305 HPTES_PER_GROUP) & ~0x7UL;
306 ppc_md.hpte_remove(hpte_group);
307 /*
308 * FIXME!! Should be try the group from which we removed ?
309 */
310 goto repeat;
311 }
312 }
313 /*
314 * Hypervisor failure. Restore old pmd and return -1
315 * similar to __hash_page_*
316 */
317 if (unlikely(slot == -2)) {
318 *ptep = __pte(old_pte);
319 hash_failure_debug(ea, access, vsid, trap, ssize,
320 MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
321 return -1;
322 }
323 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
324 new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX);
325 }
326 *ptep = __pte(new_pte & ~_PAGE_BUSY);
327 return 0;
328 }