147d99948SChristophe Leroy /*
247d99948SChristophe Leroy * Copyright IBM Corporation, 2015
347d99948SChristophe Leroy * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
447d99948SChristophe Leroy *
547d99948SChristophe Leroy * This program is free software; you can redistribute it and/or modify it
647d99948SChristophe Leroy * under the terms of version 2 of the GNU Lesser General Public License
747d99948SChristophe Leroy * as published by the Free Software Foundation.
847d99948SChristophe Leroy *
947d99948SChristophe Leroy * This program is distributed in the hope that it would be useful, but
1047d99948SChristophe Leroy * WITHOUT ANY WARRANTY; without even the implied warranty of
1147d99948SChristophe Leroy * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1247d99948SChristophe Leroy *
1347d99948SChristophe Leroy */
1447d99948SChristophe Leroy
1547d99948SChristophe Leroy #include <linux/mm.h>
1647d99948SChristophe Leroy #include <asm/machdep.h>
1747d99948SChristophe Leroy #include <asm/mmu.h>
1847d99948SChristophe Leroy
19*6b34a099SNicholas Piggin #include "internal.h"
20*6b34a099SNicholas Piggin
2147d99948SChristophe Leroy /*
2247d99948SChristophe Leroy * Return true, if the entry has a slot value which
2347d99948SChristophe Leroy * the software considers as invalid.
2447d99948SChristophe Leroy */
hpte_soft_invalid(unsigned long hidx)2547d99948SChristophe Leroy static inline bool hpte_soft_invalid(unsigned long hidx)
2647d99948SChristophe Leroy {
2747d99948SChristophe Leroy return ((hidx & 0xfUL) == 0xfUL);
2847d99948SChristophe Leroy }
2947d99948SChristophe Leroy
3047d99948SChristophe Leroy /*
3147d99948SChristophe Leroy * index from 0 - 15
3247d99948SChristophe Leroy */
__rpte_sub_valid(real_pte_t rpte,unsigned long index)3347d99948SChristophe Leroy bool __rpte_sub_valid(real_pte_t rpte, unsigned long index)
3447d99948SChristophe Leroy {
3547d99948SChristophe Leroy return !(hpte_soft_invalid(__rpte_to_hidx(rpte, index)));
3647d99948SChristophe Leroy }
3747d99948SChristophe Leroy
__hash_page_4K(unsigned long ea,unsigned long access,unsigned long vsid,pte_t * ptep,unsigned long trap,unsigned long flags,int ssize,int subpg_prot)3847d99948SChristophe Leroy int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
3947d99948SChristophe Leroy pte_t *ptep, unsigned long trap, unsigned long flags,
4047d99948SChristophe Leroy int ssize, int subpg_prot)
4147d99948SChristophe Leroy {
4247d99948SChristophe Leroy real_pte_t rpte;
4347d99948SChristophe Leroy unsigned long hpte_group;
4447d99948SChristophe Leroy unsigned int subpg_index;
4547d99948SChristophe Leroy unsigned long rflags, pa;
4647d99948SChristophe Leroy unsigned long old_pte, new_pte, subpg_pte;
4747d99948SChristophe Leroy unsigned long vpn, hash, slot, gslot;
4847d99948SChristophe Leroy unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
4947d99948SChristophe Leroy
5047d99948SChristophe Leroy /*
5147d99948SChristophe Leroy * atomically mark the linux large page PTE busy and dirty
5247d99948SChristophe Leroy */
5347d99948SChristophe Leroy do {
5447d99948SChristophe Leroy pte_t pte = READ_ONCE(*ptep);
5547d99948SChristophe Leroy
5647d99948SChristophe Leroy old_pte = pte_val(pte);
5747d99948SChristophe Leroy /* If PTE busy, retry the access */
5847d99948SChristophe Leroy if (unlikely(old_pte & H_PAGE_BUSY))
5947d99948SChristophe Leroy return 0;
6047d99948SChristophe Leroy /* If PTE permissions don't match, take page fault */
6147d99948SChristophe Leroy if (unlikely(!check_pte_access(access, old_pte)))
6247d99948SChristophe Leroy return 1;
6347d99948SChristophe Leroy /*
6447d99948SChristophe Leroy * Try to lock the PTE, add ACCESSED and DIRTY if it was
6547d99948SChristophe Leroy * a write access. Since this is 4K insert of 64K page size
6647d99948SChristophe Leroy * also add H_PAGE_COMBO
6747d99948SChristophe Leroy */
6847d99948SChristophe Leroy new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED | H_PAGE_COMBO;
6947d99948SChristophe Leroy if (access & _PAGE_WRITE)
7047d99948SChristophe Leroy new_pte |= _PAGE_DIRTY;
7147d99948SChristophe Leroy } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
7247d99948SChristophe Leroy
7347d99948SChristophe Leroy /*
7447d99948SChristophe Leroy * Handle the subpage protection bits
7547d99948SChristophe Leroy */
7647d99948SChristophe Leroy subpg_pte = new_pte & ~subpg_prot;
77d94b827eSAneesh Kumar K.V rflags = htab_convert_pte_flags(subpg_pte, flags);
7847d99948SChristophe Leroy
7947d99948SChristophe Leroy if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
8047d99948SChristophe Leroy !cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
8147d99948SChristophe Leroy
8247d99948SChristophe Leroy /*
8347d99948SChristophe Leroy * No CPU has hugepages but lacks no execute, so we
8447d99948SChristophe Leroy * don't need to worry about that case
8547d99948SChristophe Leroy */
8647d99948SChristophe Leroy rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
8747d99948SChristophe Leroy }
8847d99948SChristophe Leroy
8947d99948SChristophe Leroy subpg_index = (ea & (PAGE_SIZE - 1)) >> shift;
9047d99948SChristophe Leroy vpn = hpt_vpn(ea, vsid, ssize);
9147d99948SChristophe Leroy rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
9247d99948SChristophe Leroy /*
9347d99948SChristophe Leroy *None of the sub 4k page is hashed
9447d99948SChristophe Leroy */
9547d99948SChristophe Leroy if (!(old_pte & H_PAGE_HASHPTE))
9647d99948SChristophe Leroy goto htab_insert_hpte;
9747d99948SChristophe Leroy /*
9847d99948SChristophe Leroy * Check if the pte was already inserted into the hash table
9947d99948SChristophe Leroy * as a 64k HW page, and invalidate the 64k HPTE if so.
10047d99948SChristophe Leroy */
10147d99948SChristophe Leroy if (!(old_pte & H_PAGE_COMBO)) {
10247d99948SChristophe Leroy flush_hash_page(vpn, rpte, MMU_PAGE_64K, ssize, flags);
10347d99948SChristophe Leroy /*
10447d99948SChristophe Leroy * clear the old slot details from the old and new pte.
10547d99948SChristophe Leroy * On hash insert failure we use old pte value and we don't
10647d99948SChristophe Leroy * want slot information there if we have a insert failure.
10747d99948SChristophe Leroy */
10847d99948SChristophe Leroy old_pte &= ~H_PAGE_HASHPTE;
10947d99948SChristophe Leroy new_pte &= ~H_PAGE_HASHPTE;
11047d99948SChristophe Leroy goto htab_insert_hpte;
11147d99948SChristophe Leroy }
11247d99948SChristophe Leroy /*
11347d99948SChristophe Leroy * Check for sub page valid and update
11447d99948SChristophe Leroy */
11547d99948SChristophe Leroy if (__rpte_sub_valid(rpte, subpg_index)) {
11647d99948SChristophe Leroy int ret;
11747d99948SChristophe Leroy
11847d99948SChristophe Leroy gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte,
11947d99948SChristophe Leroy subpg_index);
12047d99948SChristophe Leroy ret = mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn,
12147d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K,
12247d99948SChristophe Leroy ssize, flags);
12347d99948SChristophe Leroy
12447d99948SChristophe Leroy /*
12547d99948SChristophe Leroy * If we failed because typically the HPTE wasn't really here
12647d99948SChristophe Leroy * we try an insertion.
12747d99948SChristophe Leroy */
12847d99948SChristophe Leroy if (ret == -1)
12947d99948SChristophe Leroy goto htab_insert_hpte;
13047d99948SChristophe Leroy
13147d99948SChristophe Leroy *ptep = __pte(new_pte & ~H_PAGE_BUSY);
13247d99948SChristophe Leroy return 0;
13347d99948SChristophe Leroy }
13447d99948SChristophe Leroy
13547d99948SChristophe Leroy htab_insert_hpte:
13647d99948SChristophe Leroy
13747d99948SChristophe Leroy /*
13847d99948SChristophe Leroy * Initialize all hidx entries to invalid value, the first time
13947d99948SChristophe Leroy * the PTE is about to allocate a 4K HPTE.
14047d99948SChristophe Leroy */
14147d99948SChristophe Leroy if (!(old_pte & H_PAGE_COMBO))
14247d99948SChristophe Leroy rpte.hidx = INVALID_RPTE_HIDX;
14347d99948SChristophe Leroy
14447d99948SChristophe Leroy /*
14547d99948SChristophe Leroy * handle H_PAGE_4K_PFN case
14647d99948SChristophe Leroy */
14747d99948SChristophe Leroy if (old_pte & H_PAGE_4K_PFN) {
14847d99948SChristophe Leroy /*
14947d99948SChristophe Leroy * All the sub 4k page have the same
15047d99948SChristophe Leroy * physical address.
15147d99948SChristophe Leroy */
15247d99948SChristophe Leroy pa = pte_pfn(__pte(old_pte)) << HW_PAGE_SHIFT;
15347d99948SChristophe Leroy } else {
15447d99948SChristophe Leroy pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
15547d99948SChristophe Leroy pa += (subpg_index << shift);
15647d99948SChristophe Leroy }
15747d99948SChristophe Leroy hash = hpt_hash(vpn, shift, ssize);
15847d99948SChristophe Leroy repeat:
15947d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
16047d99948SChristophe Leroy
16147d99948SChristophe Leroy /* Insert into the hash table, primary slot */
16247d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
16347d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K, ssize);
16447d99948SChristophe Leroy /*
16547d99948SChristophe Leroy * Primary is full, try the secondary
16647d99948SChristophe Leroy */
16747d99948SChristophe Leroy if (unlikely(slot == -1)) {
16847d99948SChristophe Leroy bool soft_invalid;
16947d99948SChristophe Leroy
17047d99948SChristophe Leroy hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
17147d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
17247d99948SChristophe Leroy rflags, HPTE_V_SECONDARY,
17347d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K,
17447d99948SChristophe Leroy ssize);
17547d99948SChristophe Leroy
17647d99948SChristophe Leroy soft_invalid = hpte_soft_invalid(slot);
17747d99948SChristophe Leroy if (unlikely(soft_invalid)) {
17847d99948SChristophe Leroy /*
17947d99948SChristophe Leroy * We got a valid slot from a hardware point of view.
18047d99948SChristophe Leroy * but we cannot use it, because we use this special
18147d99948SChristophe Leroy * value; as defined by hpte_soft_invalid(), to track
18247d99948SChristophe Leroy * invalid slots. We cannot use it. So invalidate it.
18347d99948SChristophe Leroy */
18447d99948SChristophe Leroy gslot = slot & _PTEIDX_GROUP_IX;
18547d99948SChristophe Leroy mmu_hash_ops.hpte_invalidate(hpte_group + gslot, vpn,
18647d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K,
18747d99948SChristophe Leroy ssize, 0);
18847d99948SChristophe Leroy }
18947d99948SChristophe Leroy
19047d99948SChristophe Leroy if (unlikely(slot == -1 || soft_invalid)) {
19147d99948SChristophe Leroy /*
19247d99948SChristophe Leroy * For soft invalid slot, let's ensure that we release a
19347d99948SChristophe Leroy * slot from the primary, with the hope that we will
19447d99948SChristophe Leroy * acquire that slot next time we try. This will ensure
19547d99948SChristophe Leroy * that we do not get the same soft-invalid slot.
19647d99948SChristophe Leroy */
19747d99948SChristophe Leroy if (soft_invalid || (mftb() & 0x1))
19847d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
19947d99948SChristophe Leroy
20047d99948SChristophe Leroy mmu_hash_ops.hpte_remove(hpte_group);
20147d99948SChristophe Leroy /*
20247d99948SChristophe Leroy * FIXME!! Should be try the group from which we removed ?
20347d99948SChristophe Leroy */
20447d99948SChristophe Leroy goto repeat;
20547d99948SChristophe Leroy }
20647d99948SChristophe Leroy }
20747d99948SChristophe Leroy /*
20847d99948SChristophe Leroy * Hypervisor failure. Restore old pte and return -1
20947d99948SChristophe Leroy * similar to __hash_page_*
21047d99948SChristophe Leroy */
21147d99948SChristophe Leroy if (unlikely(slot == -2)) {
21247d99948SChristophe Leroy *ptep = __pte(old_pte);
21347d99948SChristophe Leroy hash_failure_debug(ea, access, vsid, trap, ssize,
21447d99948SChristophe Leroy MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
21547d99948SChristophe Leroy return -1;
21647d99948SChristophe Leroy }
21747d99948SChristophe Leroy
21847d99948SChristophe Leroy new_pte |= pte_set_hidx(ptep, rpte, subpg_index, slot, PTRS_PER_PTE);
21947d99948SChristophe Leroy new_pte |= H_PAGE_HASHPTE;
22047d99948SChristophe Leroy
221*6b34a099SNicholas Piggin if (stress_hpt())
222*6b34a099SNicholas Piggin hpt_do_stress(ea, hpte_group);
223*6b34a099SNicholas Piggin
22447d99948SChristophe Leroy *ptep = __pte(new_pte & ~H_PAGE_BUSY);
22547d99948SChristophe Leroy return 0;
22647d99948SChristophe Leroy }
22747d99948SChristophe Leroy
__hash_page_64K(unsigned long ea,unsigned long access,unsigned long vsid,pte_t * ptep,unsigned long trap,unsigned long flags,int ssize)22847d99948SChristophe Leroy int __hash_page_64K(unsigned long ea, unsigned long access,
22947d99948SChristophe Leroy unsigned long vsid, pte_t *ptep, unsigned long trap,
23047d99948SChristophe Leroy unsigned long flags, int ssize)
23147d99948SChristophe Leroy {
23247d99948SChristophe Leroy real_pte_t rpte;
23347d99948SChristophe Leroy unsigned long hpte_group;
23447d99948SChristophe Leroy unsigned long rflags, pa;
23547d99948SChristophe Leroy unsigned long old_pte, new_pte;
23647d99948SChristophe Leroy unsigned long vpn, hash, slot;
23747d99948SChristophe Leroy unsigned long shift = mmu_psize_defs[MMU_PAGE_64K].shift;
23847d99948SChristophe Leroy
23947d99948SChristophe Leroy /*
24047d99948SChristophe Leroy * atomically mark the linux large page PTE busy and dirty
24147d99948SChristophe Leroy */
24247d99948SChristophe Leroy do {
24347d99948SChristophe Leroy pte_t pte = READ_ONCE(*ptep);
24447d99948SChristophe Leroy
24547d99948SChristophe Leroy old_pte = pte_val(pte);
24647d99948SChristophe Leroy /* If PTE busy, retry the access */
24747d99948SChristophe Leroy if (unlikely(old_pte & H_PAGE_BUSY))
24847d99948SChristophe Leroy return 0;
24947d99948SChristophe Leroy /* If PTE permissions don't match, take page fault */
25047d99948SChristophe Leroy if (unlikely(!check_pte_access(access, old_pte)))
25147d99948SChristophe Leroy return 1;
25247d99948SChristophe Leroy /*
25347d99948SChristophe Leroy * Check if PTE has the cache-inhibit bit set
25447d99948SChristophe Leroy * If so, bail out and refault as a 4k page
25547d99948SChristophe Leroy */
25647d99948SChristophe Leroy if (!mmu_has_feature(MMU_FTR_CI_LARGE_PAGE) &&
25747d99948SChristophe Leroy unlikely(pte_ci(pte)))
25847d99948SChristophe Leroy return 0;
25947d99948SChristophe Leroy /*
26047d99948SChristophe Leroy * Try to lock the PTE, add ACCESSED and DIRTY if it was
26147d99948SChristophe Leroy * a write access.
26247d99948SChristophe Leroy */
26347d99948SChristophe Leroy new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
26447d99948SChristophe Leroy if (access & _PAGE_WRITE)
26547d99948SChristophe Leroy new_pte |= _PAGE_DIRTY;
26647d99948SChristophe Leroy } while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
26747d99948SChristophe Leroy
268d94b827eSAneesh Kumar K.V rflags = htab_convert_pte_flags(new_pte, flags);
26947d99948SChristophe Leroy rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
27047d99948SChristophe Leroy
27147d99948SChristophe Leroy if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
27247d99948SChristophe Leroy !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
27347d99948SChristophe Leroy rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
27447d99948SChristophe Leroy
27547d99948SChristophe Leroy vpn = hpt_vpn(ea, vsid, ssize);
27647d99948SChristophe Leroy if (unlikely(old_pte & H_PAGE_HASHPTE)) {
27747d99948SChristophe Leroy unsigned long gslot;
27847d99948SChristophe Leroy
27947d99948SChristophe Leroy /*
28047d99948SChristophe Leroy * There MIGHT be an HPTE for this pte
28147d99948SChristophe Leroy */
28247d99948SChristophe Leroy gslot = pte_get_hash_gslot(vpn, shift, ssize, rpte, 0);
28347d99948SChristophe Leroy if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_64K,
28447d99948SChristophe Leroy MMU_PAGE_64K, ssize,
28547d99948SChristophe Leroy flags) == -1)
28647d99948SChristophe Leroy old_pte &= ~_PAGE_HPTEFLAGS;
28747d99948SChristophe Leroy }
28847d99948SChristophe Leroy
28947d99948SChristophe Leroy if (likely(!(old_pte & H_PAGE_HASHPTE))) {
29047d99948SChristophe Leroy
29147d99948SChristophe Leroy pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
29247d99948SChristophe Leroy hash = hpt_hash(vpn, shift, ssize);
29347d99948SChristophe Leroy
29447d99948SChristophe Leroy repeat:
29547d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
29647d99948SChristophe Leroy
29747d99948SChristophe Leroy /* Insert into the hash table, primary slot */
29847d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
29947d99948SChristophe Leroy MMU_PAGE_64K, MMU_PAGE_64K,
30047d99948SChristophe Leroy ssize);
30147d99948SChristophe Leroy /*
30247d99948SChristophe Leroy * Primary is full, try the secondary
30347d99948SChristophe Leroy */
30447d99948SChristophe Leroy if (unlikely(slot == -1)) {
30547d99948SChristophe Leroy hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
30647d99948SChristophe Leroy slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
30747d99948SChristophe Leroy rflags,
30847d99948SChristophe Leroy HPTE_V_SECONDARY,
30947d99948SChristophe Leroy MMU_PAGE_64K,
31047d99948SChristophe Leroy MMU_PAGE_64K, ssize);
31147d99948SChristophe Leroy if (slot == -1) {
31247d99948SChristophe Leroy if (mftb() & 0x1)
31347d99948SChristophe Leroy hpte_group = (hash & htab_hash_mask) *
31447d99948SChristophe Leroy HPTES_PER_GROUP;
31547d99948SChristophe Leroy mmu_hash_ops.hpte_remove(hpte_group);
31647d99948SChristophe Leroy /*
31747d99948SChristophe Leroy * FIXME!! Should be try the group from which we removed ?
31847d99948SChristophe Leroy */
31947d99948SChristophe Leroy goto repeat;
32047d99948SChristophe Leroy }
32147d99948SChristophe Leroy }
32247d99948SChristophe Leroy /*
32347d99948SChristophe Leroy * Hypervisor failure. Restore old pte and return -1
32447d99948SChristophe Leroy * similar to __hash_page_*
32547d99948SChristophe Leroy */
32647d99948SChristophe Leroy if (unlikely(slot == -2)) {
32747d99948SChristophe Leroy *ptep = __pte(old_pte);
32847d99948SChristophe Leroy hash_failure_debug(ea, access, vsid, trap, ssize,
32947d99948SChristophe Leroy MMU_PAGE_64K, MMU_PAGE_64K, old_pte);
33047d99948SChristophe Leroy return -1;
33147d99948SChristophe Leroy }
33247d99948SChristophe Leroy
33347d99948SChristophe Leroy new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
33447d99948SChristophe Leroy new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
335*6b34a099SNicholas Piggin
336*6b34a099SNicholas Piggin if (stress_hpt())
337*6b34a099SNicholas Piggin hpt_do_stress(ea, hpte_group);
33847d99948SChristophe Leroy }
339*6b34a099SNicholas Piggin
34047d99948SChristophe Leroy *ptep = __pte(new_pte & ~H_PAGE_BUSY);
341*6b34a099SNicholas Piggin
34247d99948SChristophe Leroy return 0;
34347d99948SChristophe Leroy }
344