xref: /linux/arch/powerpc/mm/book3s64/hash_hugepage.c (revision beace86e61e465dba204a268ab3f3377153a4973)
16d492eccSAneesh Kumar K.V /*
26d492eccSAneesh Kumar K.V  * Copyright IBM Corporation, 2013
347d99948SChristophe Leroy  * Author Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
46d492eccSAneesh Kumar K.V  *
56d492eccSAneesh Kumar K.V  * This program is free software; you can redistribute it and/or modify it
66d492eccSAneesh Kumar K.V  * under the terms of version 2.1 of the GNU Lesser General Public License
76d492eccSAneesh Kumar K.V  * as published by the Free Software Foundation.
86d492eccSAneesh Kumar K.V  *
96d492eccSAneesh Kumar K.V  * This program is distributed in the hope that it would be useful, but
106d492eccSAneesh Kumar K.V  * WITHOUT ANY WARRANTY; without even the implied warranty of
116d492eccSAneesh Kumar K.V  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
126d492eccSAneesh Kumar K.V  *
136d492eccSAneesh Kumar K.V  */
146d492eccSAneesh Kumar K.V 
156d492eccSAneesh Kumar K.V /*
166d492eccSAneesh Kumar K.V  * PPC64 THP Support for hash based MMUs
176d492eccSAneesh Kumar K.V  */
186d492eccSAneesh Kumar K.V #include <linux/mm.h>
196d492eccSAneesh Kumar K.V #include <asm/machdep.h>
206d492eccSAneesh Kumar K.V 
__hash_page_thp(unsigned long ea,unsigned long access,unsigned long vsid,pmd_t * pmdp,unsigned long trap,unsigned long flags,int ssize,unsigned int psize)216d492eccSAneesh Kumar K.V int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,
22aefa5688SAneesh Kumar K.V 		    pmd_t *pmdp, unsigned long trap, unsigned long flags,
23aefa5688SAneesh Kumar K.V 		    int ssize, unsigned int psize)
246d492eccSAneesh Kumar K.V {
256d492eccSAneesh Kumar K.V 	unsigned int index, valid;
266d492eccSAneesh Kumar K.V 	unsigned char *hpte_slot_array;
276d492eccSAneesh Kumar K.V 	unsigned long rflags, pa, hidx;
286d492eccSAneesh Kumar K.V 	unsigned long old_pmd, new_pmd;
296d492eccSAneesh Kumar K.V 	int ret, lpsize = MMU_PAGE_16M;
306d492eccSAneesh Kumar K.V 	unsigned long vpn, hash, shift, slot;
316d492eccSAneesh Kumar K.V 
326d492eccSAneesh Kumar K.V 	/*
336d492eccSAneesh Kumar K.V 	 * atomically mark the linux large page PMD busy and dirty
346d492eccSAneesh Kumar K.V 	 */
356d492eccSAneesh Kumar K.V 	do {
364f9c53c8SMichael Ellerman 		pmd_t pmd = READ_ONCE(*pmdp);
377e467245SAneesh Kumar K.V 
387e467245SAneesh Kumar K.V 		old_pmd = pmd_val(pmd);
396d492eccSAneesh Kumar K.V 		/* If PMD busy, retry the access */
40945537dfSAneesh Kumar K.V 		if (unlikely(old_pmd & H_PAGE_BUSY))
416d492eccSAneesh Kumar K.V 			return 0;
426d492eccSAneesh Kumar K.V 		/* If PMD permissions don't match, take page fault */
43ac29c640SAneesh Kumar K.V 		if (unlikely(!check_pte_access(access, old_pmd)))
446d492eccSAneesh Kumar K.V 			return 1;
456d492eccSAneesh Kumar K.V 		/*
466d492eccSAneesh Kumar K.V 		 * Try to lock the PTE, add ACCESSED and DIRTY if it was
476d492eccSAneesh Kumar K.V 		 * a write access
486d492eccSAneesh Kumar K.V 		 */
49945537dfSAneesh Kumar K.V 		new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED;
50c7d54842SAneesh Kumar K.V 		if (access & _PAGE_WRITE)
516d492eccSAneesh Kumar K.V 			new_pmd |= _PAGE_DIRTY;
525dc1ef85SAneesh Kumar K.V 	} while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd)));
535dc1ef85SAneesh Kumar K.V 
5475646c48SAneesh Kumar K.V 	/*
5575646c48SAneesh Kumar K.V 	 * Make sure this is thp or devmap entry
5675646c48SAneesh Kumar K.V 	 */
57*bea0cc7cSAlistair Popple 	if (!(old_pmd & H_PAGE_THP_HUGE))
5875646c48SAneesh Kumar K.V 		return 0;
5975646c48SAneesh Kumar K.V 
60d94b827eSAneesh Kumar K.V 	rflags = htab_convert_pte_flags(new_pmd, flags);
616d492eccSAneesh Kumar K.V 
626d492eccSAneesh Kumar K.V 	/*
63d9cf600eSKunwu Chan 	 * THPs are only supported on platforms that can do mixed page size
64d9cf600eSKunwu Chan 	 * segments (MPSS) and all such platforms have coherent icache. Hence we
65d9cf600eSKunwu Chan 	 * don't need to do lazy icache flush (hash_page_do_lazy_icache()) on
66d9cf600eSKunwu Chan 	 * noexecute fault.
676d492eccSAneesh Kumar K.V 	 */
68d9cf600eSKunwu Chan 
696d492eccSAneesh Kumar K.V 	/*
706d492eccSAneesh Kumar K.V 	 * Find the slot index details for this ea, using base page size.
716d492eccSAneesh Kumar K.V 	 */
726d492eccSAneesh Kumar K.V 	shift = mmu_psize_defs[psize].shift;
736d492eccSAneesh Kumar K.V 	index = (ea & ~HPAGE_PMD_MASK) >> shift;
744dcbd88eSAneesh Kumar K.V 	BUG_ON(index >= PTE_FRAG_SIZE);
756d492eccSAneesh Kumar K.V 
766d492eccSAneesh Kumar K.V 	vpn = hpt_vpn(ea, vsid, ssize);
776d492eccSAneesh Kumar K.V 	hpte_slot_array = get_hpte_slot_array(pmdp);
78629149faSAneesh Kumar K.V 	if (psize == MMU_PAGE_4K) {
79629149faSAneesh Kumar K.V 		/*
80629149faSAneesh Kumar K.V 		 * invalidate the old hpte entry if we have that mapped via 64K
81629149faSAneesh Kumar K.V 		 * base page size. This is because demote_segment won't flush
82629149faSAneesh Kumar K.V 		 * hash page table entries.
83629149faSAneesh Kumar K.V 		 */
84945537dfSAneesh Kumar K.V 		if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) {
85f1581bf1SAneesh Kumar K.V 			flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
86aefa5688SAneesh Kumar K.V 					    ssize, flags);
879ab3ac23SAneesh Kumar K.V 			/*
889ab3ac23SAneesh Kumar K.V 			 * With THP, we also clear the slot information with
899ab3ac23SAneesh Kumar K.V 			 * respect to all the 64K hash pte mapping the 16MB
909ab3ac23SAneesh Kumar K.V 			 * page. They are all invalid now. This make sure we
919ab3ac23SAneesh Kumar K.V 			 * don't find the slot valid when we fault with 4k
929ab3ac23SAneesh Kumar K.V 			 * base page size.
939ab3ac23SAneesh Kumar K.V 			 *
949ab3ac23SAneesh Kumar K.V 			 */
959ab3ac23SAneesh Kumar K.V 			memset(hpte_slot_array, 0, PTE_FRAG_SIZE);
969ab3ac23SAneesh Kumar K.V 		}
97629149faSAneesh Kumar K.V 	}
986d492eccSAneesh Kumar K.V 
996d492eccSAneesh Kumar K.V 	valid = hpte_valid(hpte_slot_array, index);
1006d492eccSAneesh Kumar K.V 	if (valid) {
1016d492eccSAneesh Kumar K.V 		/* update the hpte bits */
10236b35d5dSAneesh Kumar K.V 		hash = hpt_hash(vpn, shift, ssize);
1036d492eccSAneesh Kumar K.V 		hidx =  hpte_hash_index(hpte_slot_array, index);
1046d492eccSAneesh Kumar K.V 		if (hidx & _PTEIDX_SECONDARY)
1056d492eccSAneesh Kumar K.V 			hash = ~hash;
1066d492eccSAneesh Kumar K.V 		slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1076d492eccSAneesh Kumar K.V 		slot += hidx & _PTEIDX_GROUP_IX;
1086d492eccSAneesh Kumar K.V 
1097025776eSBenjamin Herrenschmidt 		ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
110aefa5688SAneesh Kumar K.V 						 psize, lpsize, ssize, flags);
1116d492eccSAneesh Kumar K.V 		/*
1126d492eccSAneesh Kumar K.V 		 * We failed to update, try to insert a new entry.
1136d492eccSAneesh Kumar K.V 		 */
1146d492eccSAneesh Kumar K.V 		if (ret == -1) {
1156d492eccSAneesh Kumar K.V 			/*
1166d492eccSAneesh Kumar K.V 			 * large pte is marked busy, so we can be sure
1176d492eccSAneesh Kumar K.V 			 * nobody is looking at hpte_slot_array. hence we can
1186d492eccSAneesh Kumar K.V 			 * safely update this here.
1196d492eccSAneesh Kumar K.V 			 */
1206d492eccSAneesh Kumar K.V 			valid = 0;
1216d492eccSAneesh Kumar K.V 			hpte_slot_array[index] = 0;
122629149faSAneesh Kumar K.V 		}
1236d492eccSAneesh Kumar K.V 	}
1246d492eccSAneesh Kumar K.V 
1256d492eccSAneesh Kumar K.V 	if (!valid) {
1266d492eccSAneesh Kumar K.V 		unsigned long hpte_group;
1276d492eccSAneesh Kumar K.V 
12836b35d5dSAneesh Kumar K.V 		hash = hpt_hash(vpn, shift, ssize);
1296d492eccSAneesh Kumar K.V 		/* insert new entry */
1306d492eccSAneesh Kumar K.V 		pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
131945537dfSAneesh Kumar K.V 		new_pmd |= H_PAGE_HASHPTE;
1326d492eccSAneesh Kumar K.V 
133629149faSAneesh Kumar K.V repeat:
1341531cff4SAneesh Kumar K.V 		hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
1356d492eccSAneesh Kumar K.V 
1366d492eccSAneesh Kumar K.V 		/* Insert into the hash table, primary slot */
1377025776eSBenjamin Herrenschmidt 		slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
1386d492eccSAneesh Kumar K.V 						psize, lpsize, ssize);
1396d492eccSAneesh Kumar K.V 		/*
1406d492eccSAneesh Kumar K.V 		 * Primary is full, try the secondary
1416d492eccSAneesh Kumar K.V 		 */
1426d492eccSAneesh Kumar K.V 		if (unlikely(slot == -1)) {
1431531cff4SAneesh Kumar K.V 			hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
1447025776eSBenjamin Herrenschmidt 			slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
1457025776eSBenjamin Herrenschmidt 							rflags,
1467025776eSBenjamin Herrenschmidt 							HPTE_V_SECONDARY,
1476d492eccSAneesh Kumar K.V 							psize, lpsize, ssize);
1486d492eccSAneesh Kumar K.V 			if (slot == -1) {
1496d492eccSAneesh Kumar K.V 				if (mftb() & 0x1)
1501531cff4SAneesh Kumar K.V 					hpte_group = (hash & htab_hash_mask) *
1511531cff4SAneesh Kumar K.V 							HPTES_PER_GROUP;
1526d492eccSAneesh Kumar K.V 
1537025776eSBenjamin Herrenschmidt 				mmu_hash_ops.hpte_remove(hpte_group);
1546d492eccSAneesh Kumar K.V 				goto repeat;
1556d492eccSAneesh Kumar K.V 			}
1566d492eccSAneesh Kumar K.V 		}
1576d492eccSAneesh Kumar K.V 		/*
1586d492eccSAneesh Kumar K.V 		 * Hypervisor failure. Restore old pmd and return -1
1596d492eccSAneesh Kumar K.V 		 * similar to __hash_page_*
1606d492eccSAneesh Kumar K.V 		 */
1616d492eccSAneesh Kumar K.V 		if (unlikely(slot == -2)) {
1626d492eccSAneesh Kumar K.V 			*pmdp = __pmd(old_pmd);
1636d492eccSAneesh Kumar K.V 			hash_failure_debug(ea, access, vsid, trap, ssize,
1646d492eccSAneesh Kumar K.V 					   psize, lpsize, old_pmd);
1656d492eccSAneesh Kumar K.V 			return -1;
1666d492eccSAneesh Kumar K.V 		}
1676d492eccSAneesh Kumar K.V 		/*
1686d492eccSAneesh Kumar K.V 		 * large pte is marked busy, so we can be sure
1696d492eccSAneesh Kumar K.V 		 * nobody is looking at hpte_slot_array. hence we can
1706d492eccSAneesh Kumar K.V 		 * safely update this here.
1716d492eccSAneesh Kumar K.V 		 */
1726d492eccSAneesh Kumar K.V 		mark_hpte_slot_valid(hpte_slot_array, index, slot);
1736d492eccSAneesh Kumar K.V 	}
1746d492eccSAneesh Kumar K.V 	/*
175945537dfSAneesh Kumar K.V 	 * Mark the pte with H_PAGE_COMBO, if we are trying to hash it with
176629149faSAneesh Kumar K.V 	 * base page size 4k.
177629149faSAneesh Kumar K.V 	 */
178629149faSAneesh Kumar K.V 	if (psize == MMU_PAGE_4K)
179945537dfSAneesh Kumar K.V 		new_pmd |= H_PAGE_COMBO;
180629149faSAneesh Kumar K.V 	/*
181b0aa44a3SAneesh Kumar K.V 	 * The hpte valid is stored in the pgtable whose address is in the
182b0aa44a3SAneesh Kumar K.V 	 * second half of the PMD. Order this against clearing of the busy bit in
183b0aa44a3SAneesh Kumar K.V 	 * huge pmd.
1846d492eccSAneesh Kumar K.V 	 */
185b0aa44a3SAneesh Kumar K.V 	smp_wmb();
186945537dfSAneesh Kumar K.V 	*pmdp = __pmd(new_pmd & ~H_PAGE_BUSY);
1876d492eccSAneesh Kumar K.V 	return 0;
1886d492eccSAneesh Kumar K.V }
189