162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  PowerPC version
462306a36Sopenharmony_ci *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
562306a36Sopenharmony_ci *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
662306a36Sopenharmony_ci *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
762306a36Sopenharmony_ci *  Adapted for Power Macintosh by Paul Mackerras.
862306a36Sopenharmony_ci *  Low-level exception handlers and MMU support
962306a36Sopenharmony_ci *  rewritten by Paul Mackerras.
1062306a36Sopenharmony_ci *    Copyright (C) 1996 Paul Mackerras.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci *  This file contains low-level assembler routines for managing
1362306a36Sopenharmony_ci *  the PowerPC MMU hash table.  (PPC 8xx processors don't use a
1462306a36Sopenharmony_ci *  hash table, so this file is not used on them.)
1562306a36Sopenharmony_ci */
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/export.h>
1862306a36Sopenharmony_ci#include <linux/pgtable.h>
1962306a36Sopenharmony_ci#include <linux/init.h>
2062306a36Sopenharmony_ci#include <asm/reg.h>
2162306a36Sopenharmony_ci#include <asm/page.h>
2262306a36Sopenharmony_ci#include <asm/cputable.h>
2362306a36Sopenharmony_ci#include <asm/ppc_asm.h>
2462306a36Sopenharmony_ci#include <asm/thread_info.h>
2562306a36Sopenharmony_ci#include <asm/asm-offsets.h>
2662306a36Sopenharmony_ci#include <asm/feature-fixups.h>
2762306a36Sopenharmony_ci#include <asm/code-patching-asm.h>
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT
3062306a36Sopenharmony_ci#define PTE_T_SIZE		8
3162306a36Sopenharmony_ci#define PTE_FLAGS_OFFSET	4	/* offset of PTE flags, in bytes */
3262306a36Sopenharmony_ci#else
3362306a36Sopenharmony_ci#define PTE_T_SIZE		4
3462306a36Sopenharmony_ci#define PTE_FLAGS_OFFSET	0
3562306a36Sopenharmony_ci#endif
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/*
3862306a36Sopenharmony_ci * Load a PTE into the hash table, if possible.
3962306a36Sopenharmony_ci * The address is in r4, and r3 contains an access flag:
4062306a36Sopenharmony_ci * _PAGE_RW (0x400) if a write.
4162306a36Sopenharmony_ci * r9 contains the SRR1 value, from which we use the MSR_PR bit.
4262306a36Sopenharmony_ci * SPRG_THREAD contains the physical address of the current task's thread.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci * Returns to the caller if the access is illegal or there is no
4562306a36Sopenharmony_ci * mapping for the address.  Otherwise it places an appropriate PTE
4662306a36Sopenharmony_ci * in the hash table and returns from the exception.
4762306a36Sopenharmony_ci * Uses r0, r3 - r6, r8, r10, ctr, lr.
4862306a36Sopenharmony_ci */
4962306a36Sopenharmony_ci	.text
5062306a36Sopenharmony_ci_GLOBAL(hash_page)
5162306a36Sopenharmony_ci#ifdef CONFIG_SMP
5262306a36Sopenharmony_ci	lis	r8, (mmu_hash_lock - PAGE_OFFSET)@h
5362306a36Sopenharmony_ci	ori	r8, r8, (mmu_hash_lock - PAGE_OFFSET)@l
5462306a36Sopenharmony_ci	lis	r0,0x0fff
5562306a36Sopenharmony_ci	b	10f
5662306a36Sopenharmony_ci11:	lwz	r6,0(r8)
5762306a36Sopenharmony_ci	cmpwi	0,r6,0
5862306a36Sopenharmony_ci	bne	11b
5962306a36Sopenharmony_ci10:	lwarx	r6,0,r8
6062306a36Sopenharmony_ci	cmpwi	0,r6,0
6162306a36Sopenharmony_ci	bne-	11b
6262306a36Sopenharmony_ci	stwcx.	r0,0,r8
6362306a36Sopenharmony_ci	bne-	10b
6462306a36Sopenharmony_ci	isync
6562306a36Sopenharmony_ci#endif
6662306a36Sopenharmony_ci	/* Get PTE (linux-style) and check access */
6762306a36Sopenharmony_ci	lis	r0, TASK_SIZE@h		/* check if kernel address */
6862306a36Sopenharmony_ci	cmplw	0,r4,r0
6962306a36Sopenharmony_ci	mfspr	r8,SPRN_SPRG_THREAD	/* current task's THREAD (phys) */
7062306a36Sopenharmony_ci	ori	r3,r3,_PAGE_USER|_PAGE_PRESENT /* test low addresses as user */
7162306a36Sopenharmony_ci	lwz	r5,PGDIR(r8)		/* virt page-table root */
7262306a36Sopenharmony_ci	blt+	112f			/* assume user more likely */
7362306a36Sopenharmony_ci	lis	r5,swapper_pg_dir@ha	/* if kernel address, use */
7462306a36Sopenharmony_ci	addi	r5,r5,swapper_pg_dir@l	/* kernel page table */
7562306a36Sopenharmony_ci	rlwimi	r3,r9,32-12,29,29	/* MSR_PR -> _PAGE_USER */
7662306a36Sopenharmony_ci112:	tophys(r5, r5)
7762306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT
7862306a36Sopenharmony_ci	rlwimi	r5,r4,12,20,29		/* insert top 10 bits of address */
7962306a36Sopenharmony_ci	lwz	r8,0(r5)		/* get pmd entry */
8062306a36Sopenharmony_ci	rlwinm.	r8,r8,0,0,19		/* extract address of pte page */
8162306a36Sopenharmony_ci#else
8262306a36Sopenharmony_ci	rlwinm	r8,r4,13,19,29		/* Compute pgdir/pmd offset */
8362306a36Sopenharmony_ci	lwzx	r8,r8,r5		/* Get L1 entry */
8462306a36Sopenharmony_ci	rlwinm.	r8,r8,0,0,20		/* extract pt base address */
8562306a36Sopenharmony_ci#endif
8662306a36Sopenharmony_ci#ifdef CONFIG_SMP
8762306a36Sopenharmony_ci	beq-	.Lhash_page_out		/* return if no mapping */
8862306a36Sopenharmony_ci#else
8962306a36Sopenharmony_ci	/* XXX it seems like the 601 will give a machine fault on the
9062306a36Sopenharmony_ci	   rfi if its alignment is wrong (bottom 4 bits of address are
9162306a36Sopenharmony_ci	   8 or 0xc) and we have had a not-taken conditional branch
9262306a36Sopenharmony_ci	   to the address following the rfi. */
9362306a36Sopenharmony_ci	beqlr-
9462306a36Sopenharmony_ci#endif
9562306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT
9662306a36Sopenharmony_ci	rlwimi	r8,r4,22,20,29		/* insert next 10 bits of address */
9762306a36Sopenharmony_ci#else
9862306a36Sopenharmony_ci	rlwimi	r8,r4,23,20,28		/* compute pte address */
9962306a36Sopenharmony_ci	/*
10062306a36Sopenharmony_ci	 * If PTE_64BIT is set, the low word is the flags word; use that
10162306a36Sopenharmony_ci	 * word for locking since it contains all the interesting bits.
10262306a36Sopenharmony_ci	 */
10362306a36Sopenharmony_ci	addi	r8,r8,PTE_FLAGS_OFFSET
10462306a36Sopenharmony_ci#endif
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/*
10762306a36Sopenharmony_ci	 * Update the linux PTE atomically.  We do the lwarx up-front
10862306a36Sopenharmony_ci	 * because almost always, there won't be a permission violation
10962306a36Sopenharmony_ci	 * and there won't already be an HPTE, and thus we will have
11062306a36Sopenharmony_ci	 * to update the PTE to set _PAGE_HASHPTE.  -- paulus.
11162306a36Sopenharmony_ci	 */
11262306a36Sopenharmony_ci.Lretry:
11362306a36Sopenharmony_ci	lwarx	r6,0,r8			/* get linux-style pte, flag word */
11462306a36Sopenharmony_ci#ifdef CONFIG_PPC_KUAP
11562306a36Sopenharmony_ci	mfsrin	r5,r4
11662306a36Sopenharmony_ci	rlwinm	r0,r9,28,_PAGE_RW	/* MSR[PR] => _PAGE_RW */
11762306a36Sopenharmony_ci	rlwinm	r5,r5,12,_PAGE_RW	/* Ks => _PAGE_RW */
11862306a36Sopenharmony_ci	andc	r5,r5,r0		/* Ks & ~MSR[PR] */
11962306a36Sopenharmony_ci	andc	r5,r6,r5		/* Clear _PAGE_RW when Ks = 1 && MSR[PR] = 0 */
12062306a36Sopenharmony_ci	andc.	r5,r3,r5		/* check access & ~permission */
12162306a36Sopenharmony_ci#else
12262306a36Sopenharmony_ci	andc.	r5,r3,r6		/* check access & ~permission */
12362306a36Sopenharmony_ci#endif
12462306a36Sopenharmony_ci	rlwinm	r0,r3,32-3,24,24	/* _PAGE_RW access -> _PAGE_DIRTY */
12562306a36Sopenharmony_ci	ori	r0,r0,_PAGE_ACCESSED|_PAGE_HASHPTE
12662306a36Sopenharmony_ci#ifdef CONFIG_SMP
12762306a36Sopenharmony_ci	bne-	.Lhash_page_out		/* return if access not permitted */
12862306a36Sopenharmony_ci#else
12962306a36Sopenharmony_ci	bnelr-
13062306a36Sopenharmony_ci#endif
13162306a36Sopenharmony_ci	or	r5,r0,r6		/* set accessed/dirty bits */
13262306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT
13362306a36Sopenharmony_ci#ifdef CONFIG_SMP
13462306a36Sopenharmony_ci	subf	r10,r6,r8		/* create false data dependency */
13562306a36Sopenharmony_ci	subi	r10,r10,PTE_FLAGS_OFFSET
13662306a36Sopenharmony_ci	lwzx	r10,r6,r10		/* Get upper PTE word */
13762306a36Sopenharmony_ci#else
13862306a36Sopenharmony_ci	lwz	r10,-PTE_FLAGS_OFFSET(r8)
13962306a36Sopenharmony_ci#endif /* CONFIG_SMP */
14062306a36Sopenharmony_ci#endif /* CONFIG_PTE_64BIT */
14162306a36Sopenharmony_ci	stwcx.	r5,0,r8			/* attempt to update PTE */
14262306a36Sopenharmony_ci	bne-	.Lretry			/* retry if someone got there first */
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	mfsrin	r3,r4			/* get segment reg for segment */
14562306a36Sopenharmony_ci	bl	create_hpte		/* add the hash table entry */
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci#ifdef CONFIG_SMP
14862306a36Sopenharmony_ci	eieio
14962306a36Sopenharmony_ci	lis	r8, (mmu_hash_lock - PAGE_OFFSET)@ha
15062306a36Sopenharmony_ci	li	r0,0
15162306a36Sopenharmony_ci	stw	r0, (mmu_hash_lock - PAGE_OFFSET)@l(r8)
15262306a36Sopenharmony_ci#endif
15362306a36Sopenharmony_ci	b	fast_hash_page_return
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci#ifdef CONFIG_SMP
15662306a36Sopenharmony_ci.Lhash_page_out:
15762306a36Sopenharmony_ci	eieio
15862306a36Sopenharmony_ci	lis	r8, (mmu_hash_lock - PAGE_OFFSET)@ha
15962306a36Sopenharmony_ci	li	r0,0
16062306a36Sopenharmony_ci	stw	r0, (mmu_hash_lock - PAGE_OFFSET)@l(r8)
16162306a36Sopenharmony_ci	blr
16262306a36Sopenharmony_ci#endif /* CONFIG_SMP */
16362306a36Sopenharmony_ci_ASM_NOKPROBE_SYMBOL(hash_page)
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci/*
16662306a36Sopenharmony_ci * Add an entry for a particular page to the hash table.
16762306a36Sopenharmony_ci *
16862306a36Sopenharmony_ci * add_hash_page(unsigned context, unsigned long va, unsigned long pmdval)
16962306a36Sopenharmony_ci *
17062306a36Sopenharmony_ci * We assume any necessary modifications to the pte (e.g. setting
17162306a36Sopenharmony_ci * the accessed bit) have already been done and that there is actually
17262306a36Sopenharmony_ci * a hash table in use (i.e. we're not on a 603).
17362306a36Sopenharmony_ci */
17462306a36Sopenharmony_ci_GLOBAL(add_hash_page)
17562306a36Sopenharmony_ci	mflr	r0
17662306a36Sopenharmony_ci	stw	r0,4(r1)
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci#ifdef CONFIG_SMP
17962306a36Sopenharmony_ci	lwz	r8,TASK_CPU(r2)		/* to go in mmu_hash_lock */
18062306a36Sopenharmony_ci	oris	r8,r8,12
18162306a36Sopenharmony_ci#endif /* CONFIG_SMP */
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	/*
18462306a36Sopenharmony_ci	 * We disable interrupts here, even on UP, because we don't
18562306a36Sopenharmony_ci	 * want to race with hash_page, and because we want the
18662306a36Sopenharmony_ci	 * _PAGE_HASHPTE bit to be a reliable indication of whether
18762306a36Sopenharmony_ci	 * the HPTE exists (or at least whether one did once).
18862306a36Sopenharmony_ci	 * We also turn off the MMU for data accesses so that we
18962306a36Sopenharmony_ci	 * we can't take a hash table miss (assuming the code is
19062306a36Sopenharmony_ci	 * covered by a BAT).  -- paulus
19162306a36Sopenharmony_ci	 */
19262306a36Sopenharmony_ci	mfmsr	r9
19362306a36Sopenharmony_ci	rlwinm	r0,r9,0,17,15		/* clear bit 16 (MSR_EE) */
19462306a36Sopenharmony_ci	rlwinm	r0,r0,0,28,26		/* clear MSR_DR */
19562306a36Sopenharmony_ci	mtmsr	r0
19662306a36Sopenharmony_ci	isync
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#ifdef CONFIG_SMP
19962306a36Sopenharmony_ci	lis	r6, (mmu_hash_lock - PAGE_OFFSET)@ha
20062306a36Sopenharmony_ci	addi	r6, r6, (mmu_hash_lock - PAGE_OFFSET)@l
20162306a36Sopenharmony_ci10:	lwarx	r0,0,r6			/* take the mmu_hash_lock */
20262306a36Sopenharmony_ci	cmpwi	0,r0,0
20362306a36Sopenharmony_ci	bne-	11f
20462306a36Sopenharmony_ci	stwcx.	r8,0,r6
20562306a36Sopenharmony_ci	beq+	12f
20662306a36Sopenharmony_ci11:	lwz	r0,0(r6)
20762306a36Sopenharmony_ci	cmpwi	0,r0,0
20862306a36Sopenharmony_ci	beq	10b
20962306a36Sopenharmony_ci	b	11b
21062306a36Sopenharmony_ci12:	isync
21162306a36Sopenharmony_ci#endif
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	/*
21462306a36Sopenharmony_ci	 * Fetch the linux pte and test and set _PAGE_HASHPTE atomically.
21562306a36Sopenharmony_ci	 * If _PAGE_HASHPTE was already set, we don't replace the existing
21662306a36Sopenharmony_ci	 * HPTE, so we just unlock and return.
21762306a36Sopenharmony_ci	 */
21862306a36Sopenharmony_ci	mr	r8,r5
21962306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT
22062306a36Sopenharmony_ci	rlwimi	r8,r4,22,20,29
22162306a36Sopenharmony_ci#else
22262306a36Sopenharmony_ci	rlwimi	r8,r4,23,20,28
22362306a36Sopenharmony_ci	addi	r8,r8,PTE_FLAGS_OFFSET
22462306a36Sopenharmony_ci#endif
22562306a36Sopenharmony_ci1:	lwarx	r6,0,r8
22662306a36Sopenharmony_ci	andi.	r0,r6,_PAGE_HASHPTE
22762306a36Sopenharmony_ci	bne	9f			/* if HASHPTE already set, done */
22862306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT
22962306a36Sopenharmony_ci#ifdef CONFIG_SMP
23062306a36Sopenharmony_ci	subf	r10,r6,r8		/* create false data dependency */
23162306a36Sopenharmony_ci	subi	r10,r10,PTE_FLAGS_OFFSET
23262306a36Sopenharmony_ci	lwzx	r10,r6,r10		/* Get upper PTE word */
23362306a36Sopenharmony_ci#else
23462306a36Sopenharmony_ci	lwz	r10,-PTE_FLAGS_OFFSET(r8)
23562306a36Sopenharmony_ci#endif /* CONFIG_SMP */
23662306a36Sopenharmony_ci#endif /* CONFIG_PTE_64BIT */
23762306a36Sopenharmony_ci	ori	r5,r6,_PAGE_HASHPTE
23862306a36Sopenharmony_ci	stwcx.	r5,0,r8
23962306a36Sopenharmony_ci	bne-	1b
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	/* Convert context and va to VSID */
24262306a36Sopenharmony_ci	mulli	r3,r3,897*16		/* multiply context by context skew */
24362306a36Sopenharmony_ci	rlwinm	r0,r4,4,28,31		/* get ESID (top 4 bits of va) */
24462306a36Sopenharmony_ci	mulli	r0,r0,0x111		/* multiply by ESID skew */
24562306a36Sopenharmony_ci	add	r3,r3,r0		/* note create_hpte trims to 24 bits */
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	bl	create_hpte
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci9:
25062306a36Sopenharmony_ci#ifdef CONFIG_SMP
25162306a36Sopenharmony_ci	lis	r6, (mmu_hash_lock - PAGE_OFFSET)@ha
25262306a36Sopenharmony_ci	addi	r6, r6, (mmu_hash_lock - PAGE_OFFSET)@l
25362306a36Sopenharmony_ci	eieio
25462306a36Sopenharmony_ci	li	r0,0
25562306a36Sopenharmony_ci	stw	r0,0(r6)		/* clear mmu_hash_lock */
25662306a36Sopenharmony_ci#endif
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ci	/* reenable interrupts and DR */
25962306a36Sopenharmony_ci	mtmsr	r9
26062306a36Sopenharmony_ci	isync
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_ci	lwz	r0,4(r1)
26362306a36Sopenharmony_ci	mtlr	r0
26462306a36Sopenharmony_ci	blr
26562306a36Sopenharmony_ci_ASM_NOKPROBE_SYMBOL(add_hash_page)
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/*
26862306a36Sopenharmony_ci * This routine adds a hardware PTE to the hash table.
26962306a36Sopenharmony_ci * It is designed to be called with the MMU either on or off.
27062306a36Sopenharmony_ci * r3 contains the VSID, r4 contains the virtual address,
27162306a36Sopenharmony_ci * r5 contains the linux PTE, r6 contains the old value of the
27262306a36Sopenharmony_ci * linux PTE (before setting _PAGE_HASHPTE). r10 contains the
27362306a36Sopenharmony_ci * upper half of the PTE if CONFIG_PTE_64BIT.
27462306a36Sopenharmony_ci * On SMP, the caller should have the mmu_hash_lock held.
27562306a36Sopenharmony_ci * We assume that the caller has (or will) set the _PAGE_HASHPTE
27662306a36Sopenharmony_ci * bit in the linux PTE in memory.  The value passed in r6 should
27762306a36Sopenharmony_ci * be the old linux PTE value; if it doesn't have _PAGE_HASHPTE set
27862306a36Sopenharmony_ci * this routine will skip the search for an existing HPTE.
27962306a36Sopenharmony_ci * This procedure modifies r0, r3 - r6, r8, cr0.
28062306a36Sopenharmony_ci *  -- paulus.
28162306a36Sopenharmony_ci *
28262306a36Sopenharmony_ci * For speed, 4 of the instructions get patched once the size and
28362306a36Sopenharmony_ci * physical address of the hash table are known.  These definitions
28462306a36Sopenharmony_ci * of Hash_base and Hash_bits below are for the early hash table.
28562306a36Sopenharmony_ci */
28662306a36Sopenharmony_ciHash_base = early_hash
28762306a36Sopenharmony_ciHash_bits = 12				/* e.g. 256kB hash table */
28862306a36Sopenharmony_ciHash_msk = (((1 << Hash_bits) - 1) * 64)
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci/* defines for the PTE format for 32-bit PPCs */
29162306a36Sopenharmony_ci#define HPTE_SIZE	8
29262306a36Sopenharmony_ci#define PTEG_SIZE	64
29362306a36Sopenharmony_ci#define LG_PTEG_SIZE	6
29462306a36Sopenharmony_ci#define LDPTEu		lwzu
29562306a36Sopenharmony_ci#define LDPTE		lwz
29662306a36Sopenharmony_ci#define STPTE		stw
29762306a36Sopenharmony_ci#define CMPPTE		cmpw
29862306a36Sopenharmony_ci#define PTE_H		0x40
29962306a36Sopenharmony_ci#define PTE_V		0x80000000
30062306a36Sopenharmony_ci#define TST_V(r)	rlwinm. r,r,0,0,0
30162306a36Sopenharmony_ci#define SET_V(r)	oris r,r,PTE_V@h
30262306a36Sopenharmony_ci#define CLR_V(r,t)	rlwinm r,r,0,1,31
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci#define HASH_LEFT	31-(LG_PTEG_SIZE+Hash_bits-1)
30562306a36Sopenharmony_ci#define HASH_RIGHT	31-LG_PTEG_SIZE
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_ci__REF
30862306a36Sopenharmony_ci_GLOBAL(create_hpte)
30962306a36Sopenharmony_ci	/* Convert linux-style PTE (r5) to low word of PPC-style PTE (r8) */
31062306a36Sopenharmony_ci	rlwinm	r8,r5,32-9,30,30	/* _PAGE_RW -> PP msb */
31162306a36Sopenharmony_ci	rlwinm	r0,r5,32-6,30,30	/* _PAGE_DIRTY -> PP msb */
31262306a36Sopenharmony_ci	and	r8,r8,r0		/* writable if _RW & _DIRTY */
31362306a36Sopenharmony_ci	rlwimi	r5,r5,32-1,30,30	/* _PAGE_USER -> PP msb */
31462306a36Sopenharmony_ci	rlwimi	r5,r5,32-2,31,31	/* _PAGE_USER -> PP lsb */
31562306a36Sopenharmony_ci	ori	r8,r8,0xe04		/* clear out reserved bits */
31662306a36Sopenharmony_ci	andc	r8,r5,r8		/* PP = user? (rw&dirty? 1: 3): 0 */
31762306a36Sopenharmony_ciBEGIN_FTR_SECTION
31862306a36Sopenharmony_ci	rlwinm	r8,r8,0,~_PAGE_COHERENT	/* clear M (coherence not required) */
31962306a36Sopenharmony_ciEND_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
32062306a36Sopenharmony_ci#ifdef CONFIG_PTE_64BIT
32162306a36Sopenharmony_ci	/* Put the XPN bits into the PTE */
32262306a36Sopenharmony_ci	rlwimi	r8,r10,8,20,22
32362306a36Sopenharmony_ci	rlwimi	r8,r10,2,29,29
32462306a36Sopenharmony_ci#endif
32562306a36Sopenharmony_ci
32662306a36Sopenharmony_ci	/* Construct the high word of the PPC-style PTE (r5) */
32762306a36Sopenharmony_ci	rlwinm	r5,r3,7,1,24		/* put VSID in 0x7fffff80 bits */
32862306a36Sopenharmony_ci	rlwimi	r5,r4,10,26,31		/* put in API (abbrev page index) */
32962306a36Sopenharmony_ci	SET_V(r5)			/* set V (valid) bit */
33062306a36Sopenharmony_ci
33162306a36Sopenharmony_ci	patch_site	0f, patch__hash_page_A0
33262306a36Sopenharmony_ci	patch_site	1f, patch__hash_page_A1
33362306a36Sopenharmony_ci	patch_site	2f, patch__hash_page_A2
33462306a36Sopenharmony_ci	/* Get the address of the primary PTE group in the hash table (r3) */
33562306a36Sopenharmony_ci0:	lis	r0, (Hash_base - PAGE_OFFSET)@h	/* base address of hash table */
33662306a36Sopenharmony_ci1:	rlwimi	r0,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT    /* VSID -> hash */
33762306a36Sopenharmony_ci2:	rlwinm	r3,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
33862306a36Sopenharmony_ci	xor	r3,r3,r0		/* make primary hash */
33962306a36Sopenharmony_ci	li	r0,8			/* PTEs/group */
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	/*
34262306a36Sopenharmony_ci	 * Test the _PAGE_HASHPTE bit in the old linux PTE, and skip the search
34362306a36Sopenharmony_ci	 * if it is clear, meaning that the HPTE isn't there already...
34462306a36Sopenharmony_ci	 */
34562306a36Sopenharmony_ci	andi.	r6,r6,_PAGE_HASHPTE
34662306a36Sopenharmony_ci	beq+	10f			/* no PTE: go look for an empty slot */
34762306a36Sopenharmony_ci	tlbie	r4
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	/* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
35062306a36Sopenharmony_ci	mtctr	r0
35162306a36Sopenharmony_ci	addi	r4,r3,-HPTE_SIZE
35262306a36Sopenharmony_ci1:	LDPTEu	r6,HPTE_SIZE(r4)	/* get next PTE */
35362306a36Sopenharmony_ci	CMPPTE	0,r6,r5
35462306a36Sopenharmony_ci	bdnzf	2,1b			/* loop while ctr != 0 && !cr0.eq */
35562306a36Sopenharmony_ci	beq+	.Lfound_slot
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	patch_site	0f, patch__hash_page_B
35862306a36Sopenharmony_ci	/* Search the secondary PTEG for a matching PTE */
35962306a36Sopenharmony_ci	ori	r5,r5,PTE_H		/* set H (secondary hash) bit */
36062306a36Sopenharmony_ci0:	xoris	r4,r3,Hash_msk>>16	/* compute secondary hash */
36162306a36Sopenharmony_ci	xori	r4,r4,(-PTEG_SIZE & 0xffff)
36262306a36Sopenharmony_ci	addi	r4,r4,-HPTE_SIZE
36362306a36Sopenharmony_ci	mtctr	r0
36462306a36Sopenharmony_ci2:	LDPTEu	r6,HPTE_SIZE(r4)
36562306a36Sopenharmony_ci	CMPPTE	0,r6,r5
36662306a36Sopenharmony_ci	bdnzf	2,2b
36762306a36Sopenharmony_ci	beq+	.Lfound_slot
36862306a36Sopenharmony_ci	xori	r5,r5,PTE_H		/* clear H bit again */
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	/* Search the primary PTEG for an empty slot */
37162306a36Sopenharmony_ci10:	mtctr	r0
37262306a36Sopenharmony_ci	addi	r4,r3,-HPTE_SIZE	/* search primary PTEG */
37362306a36Sopenharmony_ci1:	LDPTEu	r6,HPTE_SIZE(r4)	/* get next PTE */
37462306a36Sopenharmony_ci	TST_V(r6)			/* test valid bit */
37562306a36Sopenharmony_ci	bdnzf	2,1b			/* loop while ctr != 0 && !cr0.eq */
37662306a36Sopenharmony_ci	beq+	.Lfound_empty
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ci	patch_site	0f, patch__hash_page_C
37962306a36Sopenharmony_ci	/* Search the secondary PTEG for an empty slot */
38062306a36Sopenharmony_ci	ori	r5,r5,PTE_H		/* set H (secondary hash) bit */
38162306a36Sopenharmony_ci0:	xoris	r4,r3,Hash_msk>>16	/* compute secondary hash */
38262306a36Sopenharmony_ci	xori	r4,r4,(-PTEG_SIZE & 0xffff)
38362306a36Sopenharmony_ci	addi	r4,r4,-HPTE_SIZE
38462306a36Sopenharmony_ci	mtctr	r0
38562306a36Sopenharmony_ci2:	LDPTEu	r6,HPTE_SIZE(r4)
38662306a36Sopenharmony_ci	TST_V(r6)
38762306a36Sopenharmony_ci	bdnzf	2,2b
38862306a36Sopenharmony_ci	beq+	.Lfound_empty
38962306a36Sopenharmony_ci	xori	r5,r5,PTE_H		/* clear H bit again */
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	/*
39262306a36Sopenharmony_ci	 * Choose an arbitrary slot in the primary PTEG to overwrite.
39362306a36Sopenharmony_ci	 * Since both the primary and secondary PTEGs are full, and we
39462306a36Sopenharmony_ci	 * have no information that the PTEs in the primary PTEG are
39562306a36Sopenharmony_ci	 * more important or useful than those in the secondary PTEG,
39662306a36Sopenharmony_ci	 * and we know there is a definite (although small) speed
39762306a36Sopenharmony_ci	 * advantage to putting the PTE in the primary PTEG, we always
39862306a36Sopenharmony_ci	 * put the PTE in the primary PTEG.
39962306a36Sopenharmony_ci	 */
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ci	lis	r4, (next_slot - PAGE_OFFSET)@ha	/* get next evict slot */
40262306a36Sopenharmony_ci	lwz	r6, (next_slot - PAGE_OFFSET)@l(r4)
40362306a36Sopenharmony_ci	addi	r6,r6,HPTE_SIZE			/* search for candidate */
40462306a36Sopenharmony_ci	andi.	r6,r6,7*HPTE_SIZE
40562306a36Sopenharmony_ci	stw	r6,next_slot@l(r4)
40662306a36Sopenharmony_ci	add	r4,r3,r6
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci#ifndef CONFIG_SMP
40962306a36Sopenharmony_ci	/* Store PTE in PTEG */
41062306a36Sopenharmony_ci.Lfound_empty:
41162306a36Sopenharmony_ci	STPTE	r5,0(r4)
41262306a36Sopenharmony_ci.Lfound_slot:
41362306a36Sopenharmony_ci	STPTE	r8,HPTE_SIZE/2(r4)
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci#else /* CONFIG_SMP */
41662306a36Sopenharmony_ci/*
41762306a36Sopenharmony_ci * Between the tlbie above and updating the hash table entry below,
41862306a36Sopenharmony_ci * another CPU could read the hash table entry and put it in its TLB.
41962306a36Sopenharmony_ci * There are 3 cases:
42062306a36Sopenharmony_ci * 1. using an empty slot
42162306a36Sopenharmony_ci * 2. updating an earlier entry to change permissions (i.e. enable write)
42262306a36Sopenharmony_ci * 3. taking over the PTE for an unrelated address
42362306a36Sopenharmony_ci *
42462306a36Sopenharmony_ci * In each case it doesn't really matter if the other CPUs have the old
42562306a36Sopenharmony_ci * PTE in their TLB.  So we don't need to bother with another tlbie here,
42662306a36Sopenharmony_ci * which is convenient as we've overwritten the register that had the
42762306a36Sopenharmony_ci * address. :-)  The tlbie above is mainly to make sure that this CPU comes
42862306a36Sopenharmony_ci * and gets the new PTE from the hash table.
42962306a36Sopenharmony_ci *
43062306a36Sopenharmony_ci * We do however have to make sure that the PTE is never in an invalid
43162306a36Sopenharmony_ci * state with the V bit set.
43262306a36Sopenharmony_ci */
43362306a36Sopenharmony_ci.Lfound_empty:
43462306a36Sopenharmony_ci.Lfound_slot:
43562306a36Sopenharmony_ci	CLR_V(r5,r0)		/* clear V (valid) bit in PTE */
43662306a36Sopenharmony_ci	STPTE	r5,0(r4)
43762306a36Sopenharmony_ci	sync
43862306a36Sopenharmony_ci	TLBSYNC
43962306a36Sopenharmony_ci	STPTE	r8,HPTE_SIZE/2(r4) /* put in correct RPN, WIMG, PP bits */
44062306a36Sopenharmony_ci	sync
44162306a36Sopenharmony_ci	SET_V(r5)
44262306a36Sopenharmony_ci	STPTE	r5,0(r4)	/* finally set V bit in PTE */
44362306a36Sopenharmony_ci#endif /* CONFIG_SMP */
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	sync		/* make sure pte updates get to memory */
44662306a36Sopenharmony_ci	blr
44762306a36Sopenharmony_ci	.previous
44862306a36Sopenharmony_ci_ASM_NOKPROBE_SYMBOL(create_hpte)
44962306a36Sopenharmony_ci
45062306a36Sopenharmony_ci	.section .bss
45162306a36Sopenharmony_ci	.align	2
45262306a36Sopenharmony_cinext_slot:
45362306a36Sopenharmony_ci	.space	4
45462306a36Sopenharmony_ci	.previous
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci/*
45762306a36Sopenharmony_ci * Flush the entry for a particular page from the hash table.
45862306a36Sopenharmony_ci *
45962306a36Sopenharmony_ci * flush_hash_pages(unsigned context, unsigned long va, unsigned long pmdval,
46062306a36Sopenharmony_ci *		    int count)
46162306a36Sopenharmony_ci *
46262306a36Sopenharmony_ci * We assume that there is a hash table in use (Hash != 0).
46362306a36Sopenharmony_ci */
46462306a36Sopenharmony_ci__REF
46562306a36Sopenharmony_ci_GLOBAL(flush_hash_pages)
46662306a36Sopenharmony_ci	/*
46762306a36Sopenharmony_ci	 * We disable interrupts here, even on UP, because we want
46862306a36Sopenharmony_ci	 * the _PAGE_HASHPTE bit to be a reliable indication of
46962306a36Sopenharmony_ci	 * whether the HPTE exists (or at least whether one did once).
47062306a36Sopenharmony_ci	 * We also turn off the MMU for data accesses so that we
47162306a36Sopenharmony_ci	 * we can't take a hash table miss (assuming the code is
47262306a36Sopenharmony_ci	 * covered by a BAT).  -- paulus
47362306a36Sopenharmony_ci	 */
47462306a36Sopenharmony_ci	mfmsr	r10
47562306a36Sopenharmony_ci	rlwinm	r0,r10,0,17,15		/* clear bit 16 (MSR_EE) */
47662306a36Sopenharmony_ci	rlwinm	r0,r0,0,28,26		/* clear MSR_DR */
47762306a36Sopenharmony_ci	mtmsr	r0
47862306a36Sopenharmony_ci	isync
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	/* First find a PTE in the range that has _PAGE_HASHPTE set */
48162306a36Sopenharmony_ci#ifndef CONFIG_PTE_64BIT
48262306a36Sopenharmony_ci	rlwimi	r5,r4,22,20,29
48362306a36Sopenharmony_ci#else
48462306a36Sopenharmony_ci	rlwimi	r5,r4,23,20,28
48562306a36Sopenharmony_ci	addi	r5,r5,PTE_FLAGS_OFFSET
48662306a36Sopenharmony_ci#endif
48762306a36Sopenharmony_ci1:	lwz	r0,0(r5)
48862306a36Sopenharmony_ci	cmpwi	cr1,r6,1
48962306a36Sopenharmony_ci	andi.	r0,r0,_PAGE_HASHPTE
49062306a36Sopenharmony_ci	bne	2f
49162306a36Sopenharmony_ci	ble	cr1,19f
49262306a36Sopenharmony_ci	addi	r4,r4,0x1000
49362306a36Sopenharmony_ci	addi	r5,r5,PTE_T_SIZE
49462306a36Sopenharmony_ci	addi	r6,r6,-1
49562306a36Sopenharmony_ci	b	1b
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	/* Convert context and va to VSID */
49862306a36Sopenharmony_ci2:	mulli	r3,r3,897*16		/* multiply context by context skew */
49962306a36Sopenharmony_ci	rlwinm	r0,r4,4,28,31		/* get ESID (top 4 bits of va) */
50062306a36Sopenharmony_ci	mulli	r0,r0,0x111		/* multiply by ESID skew */
50162306a36Sopenharmony_ci	add	r3,r3,r0		/* note code below trims to 24 bits */
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	/* Construct the high word of the PPC-style PTE (r11) */
50462306a36Sopenharmony_ci	rlwinm	r11,r3,7,1,24		/* put VSID in 0x7fffff80 bits */
50562306a36Sopenharmony_ci	rlwimi	r11,r4,10,26,31		/* put in API (abbrev page index) */
50662306a36Sopenharmony_ci	SET_V(r11)			/* set V (valid) bit */
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci#ifdef CONFIG_SMP
50962306a36Sopenharmony_ci	lis	r9, (mmu_hash_lock - PAGE_OFFSET)@ha
51062306a36Sopenharmony_ci	addi	r9, r9, (mmu_hash_lock - PAGE_OFFSET)@l
51162306a36Sopenharmony_ci	tophys	(r8, r2)
51262306a36Sopenharmony_ci	lwz	r8, TASK_CPU(r8)
51362306a36Sopenharmony_ci	oris	r8,r8,9
51462306a36Sopenharmony_ci10:	lwarx	r0,0,r9
51562306a36Sopenharmony_ci	cmpwi	0,r0,0
51662306a36Sopenharmony_ci	bne-	11f
51762306a36Sopenharmony_ci	stwcx.	r8,0,r9
51862306a36Sopenharmony_ci	beq+	12f
51962306a36Sopenharmony_ci11:	lwz	r0,0(r9)
52062306a36Sopenharmony_ci	cmpwi	0,r0,0
52162306a36Sopenharmony_ci	beq	10b
52262306a36Sopenharmony_ci	b	11b
52362306a36Sopenharmony_ci12:	isync
52462306a36Sopenharmony_ci#endif
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ci	/*
52762306a36Sopenharmony_ci	 * Check the _PAGE_HASHPTE bit in the linux PTE.  If it is
52862306a36Sopenharmony_ci	 * already clear, we're done (for this pte).  If not,
52962306a36Sopenharmony_ci	 * clear it (atomically) and proceed.  -- paulus.
53062306a36Sopenharmony_ci	 */
53162306a36Sopenharmony_ci33:	lwarx	r8,0,r5			/* fetch the pte flags word */
53262306a36Sopenharmony_ci	andi.	r0,r8,_PAGE_HASHPTE
53362306a36Sopenharmony_ci	beq	8f			/* done if HASHPTE is already clear */
53462306a36Sopenharmony_ci	rlwinm	r8,r8,0,31,29		/* clear HASHPTE bit */
53562306a36Sopenharmony_ci	stwcx.	r8,0,r5			/* update the pte */
53662306a36Sopenharmony_ci	bne-	33b
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ci	patch_site	0f, patch__flush_hash_A0
53962306a36Sopenharmony_ci	patch_site	1f, patch__flush_hash_A1
54062306a36Sopenharmony_ci	patch_site	2f, patch__flush_hash_A2
54162306a36Sopenharmony_ci	/* Get the address of the primary PTE group in the hash table (r3) */
54262306a36Sopenharmony_ci0:	lis	r8, (Hash_base - PAGE_OFFSET)@h	/* base address of hash table */
54362306a36Sopenharmony_ci1:	rlwimi	r8,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT    /* VSID -> hash */
54462306a36Sopenharmony_ci2:	rlwinm	r0,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */
54562306a36Sopenharmony_ci	xor	r8,r0,r8		/* make primary hash */
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	/* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */
54862306a36Sopenharmony_ci	li	r0,8			/* PTEs/group */
54962306a36Sopenharmony_ci	mtctr	r0
55062306a36Sopenharmony_ci	addi	r12,r8,-HPTE_SIZE
55162306a36Sopenharmony_ci1:	LDPTEu	r0,HPTE_SIZE(r12)	/* get next PTE */
55262306a36Sopenharmony_ci	CMPPTE	0,r0,r11
55362306a36Sopenharmony_ci	bdnzf	2,1b			/* loop while ctr != 0 && !cr0.eq */
55462306a36Sopenharmony_ci	beq+	3f
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	patch_site	0f, patch__flush_hash_B
55762306a36Sopenharmony_ci	/* Search the secondary PTEG for a matching PTE */
55862306a36Sopenharmony_ci	ori	r11,r11,PTE_H		/* set H (secondary hash) bit */
55962306a36Sopenharmony_ci	li	r0,8			/* PTEs/group */
56062306a36Sopenharmony_ci0:	xoris	r12,r8,Hash_msk>>16	/* compute secondary hash */
56162306a36Sopenharmony_ci	xori	r12,r12,(-PTEG_SIZE & 0xffff)
56262306a36Sopenharmony_ci	addi	r12,r12,-HPTE_SIZE
56362306a36Sopenharmony_ci	mtctr	r0
56462306a36Sopenharmony_ci2:	LDPTEu	r0,HPTE_SIZE(r12)
56562306a36Sopenharmony_ci	CMPPTE	0,r0,r11
56662306a36Sopenharmony_ci	bdnzf	2,2b
56762306a36Sopenharmony_ci	xori	r11,r11,PTE_H		/* clear H again */
56862306a36Sopenharmony_ci	bne-	4f			/* should rarely fail to find it */
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci3:	li	r0,0
57162306a36Sopenharmony_ci	STPTE	r0,0(r12)		/* invalidate entry */
57262306a36Sopenharmony_ci4:	sync
57362306a36Sopenharmony_ci	tlbie	r4			/* in hw tlb too */
57462306a36Sopenharmony_ci	sync
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci8:	ble	cr1,9f			/* if all ptes checked */
57762306a36Sopenharmony_ci81:	addi	r6,r6,-1
57862306a36Sopenharmony_ci	addi	r5,r5,PTE_T_SIZE
57962306a36Sopenharmony_ci	addi	r4,r4,0x1000
58062306a36Sopenharmony_ci	lwz	r0,0(r5)		/* check next pte */
58162306a36Sopenharmony_ci	cmpwi	cr1,r6,1
58262306a36Sopenharmony_ci	andi.	r0,r0,_PAGE_HASHPTE
58362306a36Sopenharmony_ci	bne	33b
58462306a36Sopenharmony_ci	bgt	cr1,81b
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci9:
58762306a36Sopenharmony_ci#ifdef CONFIG_SMP
58862306a36Sopenharmony_ci	TLBSYNC
58962306a36Sopenharmony_ci	li	r0,0
59062306a36Sopenharmony_ci	stw	r0,0(r9)		/* clear mmu_hash_lock */
59162306a36Sopenharmony_ci#endif
59262306a36Sopenharmony_ci
59362306a36Sopenharmony_ci19:	mtmsr	r10
59462306a36Sopenharmony_ci	isync
59562306a36Sopenharmony_ci	blr
59662306a36Sopenharmony_ci	.previous
59762306a36Sopenharmony_ciEXPORT_SYMBOL(flush_hash_pages)
59862306a36Sopenharmony_ci_ASM_NOKPROBE_SYMBOL(flush_hash_pages)
599