xref: /openbmc/linux/arch/powerpc/mm/book3s64/hash_4k.c (revision 6b34a099)
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 
__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)2147d99948SChristophe Leroy int __hash_page_4K(unsigned long ea, unsigned long access, unsigned long vsid,
2247d99948SChristophe Leroy 		   pte_t *ptep, unsigned long trap, unsigned long flags,
2347d99948SChristophe Leroy 		   int ssize, int subpg_prot)
2447d99948SChristophe Leroy {
2547d99948SChristophe Leroy 	real_pte_t rpte;
2647d99948SChristophe Leroy 	unsigned long hpte_group;
2747d99948SChristophe Leroy 	unsigned long rflags, pa;
2847d99948SChristophe Leroy 	unsigned long old_pte, new_pte;
2947d99948SChristophe Leroy 	unsigned long vpn, hash, slot;
3047d99948SChristophe Leroy 	unsigned long shift = mmu_psize_defs[MMU_PAGE_4K].shift;
3147d99948SChristophe Leroy 
3247d99948SChristophe Leroy 	/*
3347d99948SChristophe Leroy 	 * atomically mark the linux large page PTE busy and dirty
3447d99948SChristophe Leroy 	 */
3547d99948SChristophe Leroy 	do {
3647d99948SChristophe Leroy 		pte_t pte = READ_ONCE(*ptep);
3747d99948SChristophe Leroy 
3847d99948SChristophe Leroy 		old_pte = pte_val(pte);
3947d99948SChristophe Leroy 		/* If PTE busy, retry the access */
4047d99948SChristophe Leroy 		if (unlikely(old_pte & H_PAGE_BUSY))
4147d99948SChristophe Leroy 			return 0;
4247d99948SChristophe Leroy 		/* If PTE permissions don't match, take page fault */
4347d99948SChristophe Leroy 		if (unlikely(!check_pte_access(access, old_pte)))
4447d99948SChristophe Leroy 			return 1;
4547d99948SChristophe Leroy 		/*
4647d99948SChristophe Leroy 		 * Try to lock the PTE, add ACCESSED and DIRTY if it was
4747d99948SChristophe Leroy 		 * a write access. Since this is 4K insert of 64K page size
4847d99948SChristophe Leroy 		 * also add H_PAGE_COMBO
4947d99948SChristophe Leroy 		 */
5047d99948SChristophe Leroy 		new_pte = old_pte | H_PAGE_BUSY | _PAGE_ACCESSED;
5147d99948SChristophe Leroy 		if (access & _PAGE_WRITE)
5247d99948SChristophe Leroy 			new_pte |= _PAGE_DIRTY;
5347d99948SChristophe Leroy 	} while (!pte_xchg(ptep, __pte(old_pte), __pte(new_pte)));
5447d99948SChristophe Leroy 
5547d99948SChristophe Leroy 	/*
5647d99948SChristophe Leroy 	 * PP bits. _PAGE_USER is already PP bit 0x2, so we only
5747d99948SChristophe Leroy 	 * need to add in 0x1 if it's a read-only user page
5847d99948SChristophe Leroy 	 */
59d94b827eSAneesh Kumar K.V 	rflags = htab_convert_pte_flags(new_pte, flags);
6047d99948SChristophe Leroy 	rpte = __real_pte(__pte(old_pte), ptep, PTRS_PER_PTE);
6147d99948SChristophe Leroy 
6247d99948SChristophe Leroy 	if (cpu_has_feature(CPU_FTR_NOEXECUTE) &&
6347d99948SChristophe Leroy 	    !cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
6447d99948SChristophe Leroy 		rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
6547d99948SChristophe Leroy 
6647d99948SChristophe Leroy 	vpn  = hpt_vpn(ea, vsid, ssize);
6747d99948SChristophe Leroy 	if (unlikely(old_pte & H_PAGE_HASHPTE)) {
6847d99948SChristophe Leroy 		/*
6947d99948SChristophe Leroy 		 * There MIGHT be an HPTE for this pte
7047d99948SChristophe Leroy 		 */
7147d99948SChristophe Leroy 		unsigned long gslot = pte_get_hash_gslot(vpn, shift, ssize,
7247d99948SChristophe Leroy 							 rpte, 0);
7347d99948SChristophe Leroy 
7447d99948SChristophe Leroy 		if (mmu_hash_ops.hpte_updatepp(gslot, rflags, vpn, MMU_PAGE_4K,
7547d99948SChristophe Leroy 					       MMU_PAGE_4K, ssize, flags) == -1)
7647d99948SChristophe Leroy 			old_pte &= ~_PAGE_HPTEFLAGS;
7747d99948SChristophe Leroy 	}
7847d99948SChristophe Leroy 
7947d99948SChristophe Leroy 	if (likely(!(old_pte & H_PAGE_HASHPTE))) {
8047d99948SChristophe Leroy 
8147d99948SChristophe Leroy 		pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;
8247d99948SChristophe Leroy 		hash = hpt_hash(vpn, shift, ssize);
8347d99948SChristophe Leroy 
8447d99948SChristophe Leroy repeat:
8547d99948SChristophe Leroy 		hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
8647d99948SChristophe Leroy 
8747d99948SChristophe Leroy 		/* Insert into the hash table, primary slot */
8847d99948SChristophe Leroy 		slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
8947d99948SChristophe Leroy 						MMU_PAGE_4K, MMU_PAGE_4K, ssize);
9047d99948SChristophe Leroy 		/*
9147d99948SChristophe Leroy 		 * Primary is full, try the secondary
9247d99948SChristophe Leroy 		 */
9347d99948SChristophe Leroy 		if (unlikely(slot == -1)) {
9447d99948SChristophe Leroy 			hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
9547d99948SChristophe Leroy 			slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
9647d99948SChristophe Leroy 							rflags,
9747d99948SChristophe Leroy 							HPTE_V_SECONDARY,
9847d99948SChristophe Leroy 							MMU_PAGE_4K,
9947d99948SChristophe Leroy 							MMU_PAGE_4K, ssize);
10047d99948SChristophe Leroy 			if (slot == -1) {
10147d99948SChristophe Leroy 				if (mftb() & 0x1)
10247d99948SChristophe Leroy 					hpte_group = (hash & htab_hash_mask) *
10347d99948SChristophe Leroy 							HPTES_PER_GROUP;
10447d99948SChristophe Leroy 				mmu_hash_ops.hpte_remove(hpte_group);
10547d99948SChristophe Leroy 				/*
10647d99948SChristophe Leroy 				 * FIXME!! Should be try the group from which we removed ?
10747d99948SChristophe Leroy 				 */
10847d99948SChristophe Leroy 				goto repeat;
10947d99948SChristophe Leroy 			}
11047d99948SChristophe Leroy 		}
11147d99948SChristophe Leroy 		/*
11247d99948SChristophe Leroy 		 * Hypervisor failure. Restore old pte and return -1
11347d99948SChristophe Leroy 		 * similar to __hash_page_*
11447d99948SChristophe Leroy 		 */
11547d99948SChristophe Leroy 		if (unlikely(slot == -2)) {
11647d99948SChristophe Leroy 			*ptep = __pte(old_pte);
11747d99948SChristophe Leroy 			hash_failure_debug(ea, access, vsid, trap, ssize,
11847d99948SChristophe Leroy 					   MMU_PAGE_4K, MMU_PAGE_4K, old_pte);
11947d99948SChristophe Leroy 			return -1;
12047d99948SChristophe Leroy 		}
12147d99948SChristophe Leroy 		new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | H_PAGE_HASHPTE;
12247d99948SChristophe Leroy 		new_pte |= pte_set_hidx(ptep, rpte, 0, slot, PTRS_PER_PTE);
123*6b34a099SNicholas Piggin 
124*6b34a099SNicholas Piggin 		if (stress_hpt())
125*6b34a099SNicholas Piggin 			hpt_do_stress(ea, hpte_group);
12647d99948SChristophe Leroy 	}
12747d99948SChristophe Leroy 	*ptep = __pte(new_pte & ~H_PAGE_BUSY);
12847d99948SChristophe Leroy 	return 0;
12947d99948SChristophe Leroy }
130