2 * Copyright IBM Corporation, 2015
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
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.
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.
16 #include <asm/machdep.h>
21 bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
24 unsigned long ptev = pte_val(rpte.pte);
26 g_idx = (ptev & _PAGE_COMBO_VALID) >> _PAGE_F_GIX_SHIFT;
28 if (g_idx & (0x1 << index))
36 static unsigned long mark_subptegroup_valid(unsigned long ptev, unsigned long index)
40 if (!(ptev & _PAGE_COMBO))
45 return ptev | (g_idx << _PAGE_F_GIX_SHIFT);
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)
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;
62 * atomically mark the linux large page PTE busy and dirty
65 pte_t pte = READ_ONCE(*ptep);
67 old_pte = pte_val(pte);
68 /* If PTE busy, retry the access */
69 if (unlikely(old_pte & _PAGE_BUSY))
71 /* If PTE permissions don't match, take page fault */
72 if (unlikely(access & ~old_pte))
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
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,
85 * Handle the subpage protection bits
87 subpg_pte = new_pte & ~subpg_prot;
88 rflags = htab_convert_pte_flags(subpg_pte);
92 rflags |= (subpg_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
93 _PAGE_COHERENT | _PAGE_GUARDED));
95 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
96 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
99 * No CPU has hugepages but lacks no execute, so we
100 * don't need to worry about that case
102 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
105 subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
106 vpn = hpt_vpn(ea, vsid, ssize);
107 rpte = __real_pte(__pte(old_pte), ptep);
109 *None of the sub 4k page is hashed
111 if (!(old_pte & _PAGE_HASHPTE))
112 goto htab_insert_hpte;
114 * Check if the pte was already inserted into the hash table
115 * as a 64k HW page, and invalidate the 64k HPTE if so.
117 if (!(old_pte & _PAGE_COMBO)) {
118 flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
119 old_pte &= ~_PAGE_HASHPTE | _PAGE_F_GIX | _PAGE_F_SECOND;
120 goto htab_insert_hpte;
123 * Check for sub page valid and update
125 if (__rpte_sub_valid(rpte, subpg_index)) {
128 hash = hpt_hash(vpn, shift, ssize);
129 hidx = __rpte_to_hidx(rpte, subpg_index);
130 if (hidx & _PTEIDX_SECONDARY)
132 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
133 slot += hidx & _PTEIDX_GROUP_IX;
135 ret = ppc_md.hpte_updatepp(slot, rflags, vpn,
136 MMU_PAGE_4K, MMU_PAGE_4K,
139 *if we failed because typically the HPTE wasn't really here
140 * we try an insertion.
143 goto htab_insert_hpte;
145 *ptep = __pte(new_pte & ~_PAGE_BUSY);
151 * handle _PAGE_4K_PFN case
153 if (old_pte & _PAGE_4K_PFN) {
155 * All the sub 4k page have the same
158 pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
160 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
161 pa += (subpg_index << shift);
163 hash = hpt_hash(vpn, shift, ssize);
165 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
167 /* Insert into the hash table, primary slot */
168 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
169 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
171 * Primary is full, try the secondary
173 if (unlikely(slot == -1)) {
174 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
175 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
176 rflags, HPTE_V_SECONDARY,
177 MMU_PAGE_4K, MMU_PAGE_4K, ssize);
180 hpte_group = ((hash & htab_hash_mask) *
181 HPTES_PER_GROUP) & ~0x7UL;
182 ppc_md.hpte_remove(hpte_group);
184 * FIXME!! Should be try the group from which we removed ?
190 * Hypervisor failure. Restore old pmd and return -1
191 * similar to __hash_page_*
193 if (unlikely(slot == -2)) {
194 *ptep = __pte(old_pte);
195 hash_failure_debug(ea, access, vsid, trap, ssize,
196 MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
200 * Insert slot number & secondary bit in PTE second half,
201 * clear _PAGE_BUSY and set appropriate HPTE slot bit
202 * Since we have _PAGE_BUSY set on ptep, we can be sure
203 * nobody is undating hidx.
205 hidxp = (unsigned long *)(ptep + PTRS_PER_PTE);
206 rpte.hidx &= ~(0xfUL << (subpg_index << 2));
207 *hidxp = rpte.hidx | (slot << (subpg_index << 2));
208 new_pte = mark_subptegroup_valid(new_pte, subpg_index);
209 new_pte |= _PAGE_HASHPTE;
211 * check __real_pte for details on matching smp_rmb()
214 *ptep = __pte(new_pte & ~_PAGE_BUSY);
218 int __hash_page_64K(unsigned long ea, unsigned long access,
219 unsigned long vsid, pte_t *ptep, unsigned long trap,
220 unsigned long flags, int ssize)
223 unsigned long hpte_group;
224 unsigned long rflags, pa;
225 unsigned long old_pte, new_pte;
226 unsigned long vpn, hash, slot;
227 unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
230 * atomically mark the linux large page PTE busy and dirty
233 pte_t pte = READ_ONCE(*ptep);
235 old_pte = pte_val(pte);
236 /* If PTE busy, retry the access */
237 if (unlikely(old_pte & _PAGE_BUSY))
239 /* If PTE permissions don't match, take page fault */
240 if (unlikely(access & ~old_pte))
243 * Check if PTE has the cache-inhibit bit set
244 * If so, bail out and refault as a 4k page
246 if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
247 unlikely(old_pte & _PAGE_NO_CACHE))
250 * Try to lock the PTE, add ACCESSED and DIRTY if it was
251 * a write access. Since this is 4K insert of 64K page size
252 * also add _PAGE_COMBO
254 new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;
255 if (access & _PAGE_RW)
256 new_pte |= _PAGE_DIRTY;
257 } while (old_pte != __cmpxchg_u64((unsigned long *)ptep,
260 rflags = htab_convert_pte_flags(new_pte);
264 rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |
265 _PAGE_COHERENT | _PAGE_GUARDED));
267 if (!cpu_has_feature(CPU_FTR_NOEXECUTE) &&
268 !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
269 rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
271 vpn = hpt_vpn(ea, vsid, ssize);
272 if (unlikely(old_pte & _PAGE_HASHPTE)) {
274 * There MIGHT be an HPTE for this pte
276 hash = hpt_hash(vpn, shift, ssize);
277 if (old_pte & _PAGE_F_SECOND)
279 slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
280 slot += (old_pte & _PAGE_F_GIX) >> _PAGE_F_GIX_SHIFT;
282 if (ppc_md.hpte_updatepp(slot, rflags, vpn, MMU_PAGE_64K,
283 MMU_PAGE_64K, ssize, flags) == -1)
284 old_pte &= ~_PAGE_HPTEFLAGS;
287 if (likely(!(old_pte & _PAGE_HASHPTE))) {
289 pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
290 hash = hpt_hash(vpn, shift, ssize);
293 hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
295 /* Insert into the hash table, primary slot */
296 slot = ppc_md.hpte_insert(hpte_group, vpn, pa, rflags, 0,
297 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
299 * Primary is full, try the secondary
301 if (unlikely(slot == -1)) {
302 hpte_group = ((~hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
303 slot = ppc_md.hpte_insert(hpte_group, vpn, pa,
304 rflags, HPTE_V_SECONDARY,
305 MMU_PAGE_64K, MMU_PAGE_64K, ssize);
308 hpte_group = ((hash & htab_hash_mask) *
309 HPTES_PER_GROUP) & ~0x7UL;
310 ppc_md.hpte_remove(hpte_group);
312 * FIXME!! Should be try the group from which we removed ?
318 * Hypervisor failure. Restore old pmd and return -1
319 * similar to __hash_page_*
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);
327 new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;
328 new_pte |= (slot << _PAGE_F_GIX_SHIFT) & (_PAGE_F_SECOND | _PAGE_F_GIX);
330 *ptep = __pte(new_pte & ~_PAGE_BUSY);