162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
362306a36Sopenharmony_ci#define _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * PowerPC64 memory management structures
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
862306a36Sopenharmony_ci *   PPC64 rework.
962306a36Sopenharmony_ci */
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <asm/page.h>
1262306a36Sopenharmony_ci#include <asm/bug.h>
1362306a36Sopenharmony_ci#include <asm/asm-const.h>
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/*
1662306a36Sopenharmony_ci * This is necessary to get the definition of PGTABLE_RANGE which we
1762306a36Sopenharmony_ci * need for various slices related matters. Note that this isn't the
1862306a36Sopenharmony_ci * complete pgtable.h but only a portion of it.
1962306a36Sopenharmony_ci */
2062306a36Sopenharmony_ci#include <asm/book3s/64/pgtable.h>
2162306a36Sopenharmony_ci#include <asm/book3s/64/slice.h>
2262306a36Sopenharmony_ci#include <asm/task_size_64.h>
2362306a36Sopenharmony_ci#include <asm/cpu_has_feature.h>
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci/*
2662306a36Sopenharmony_ci * SLB
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define SLB_NUM_BOLTED		2
3062306a36Sopenharmony_ci#define SLB_CACHE_ENTRIES	8
3162306a36Sopenharmony_ci#define SLB_MIN_SIZE		32
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* Bits in the SLB ESID word */
3462306a36Sopenharmony_ci#define SLB_ESID_V		ASM_CONST(0x0000000008000000) /* valid */
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* Bits in the SLB VSID word */
3762306a36Sopenharmony_ci#define SLB_VSID_SHIFT		12
3862306a36Sopenharmony_ci#define SLB_VSID_SHIFT_256M	SLB_VSID_SHIFT
3962306a36Sopenharmony_ci#define SLB_VSID_SHIFT_1T	24
4062306a36Sopenharmony_ci#define SLB_VSID_SSIZE_SHIFT	62
4162306a36Sopenharmony_ci#define SLB_VSID_B		ASM_CONST(0xc000000000000000)
4262306a36Sopenharmony_ci#define SLB_VSID_B_256M		ASM_CONST(0x0000000000000000)
4362306a36Sopenharmony_ci#define SLB_VSID_B_1T		ASM_CONST(0x4000000000000000)
4462306a36Sopenharmony_ci#define SLB_VSID_KS		ASM_CONST(0x0000000000000800)
4562306a36Sopenharmony_ci#define SLB_VSID_KP		ASM_CONST(0x0000000000000400)
4662306a36Sopenharmony_ci#define SLB_VSID_N		ASM_CONST(0x0000000000000200) /* no-execute */
4762306a36Sopenharmony_ci#define SLB_VSID_L		ASM_CONST(0x0000000000000100)
4862306a36Sopenharmony_ci#define SLB_VSID_C		ASM_CONST(0x0000000000000080) /* class */
4962306a36Sopenharmony_ci#define SLB_VSID_LP		ASM_CONST(0x0000000000000030)
5062306a36Sopenharmony_ci#define SLB_VSID_LP_00		ASM_CONST(0x0000000000000000)
5162306a36Sopenharmony_ci#define SLB_VSID_LP_01		ASM_CONST(0x0000000000000010)
5262306a36Sopenharmony_ci#define SLB_VSID_LP_10		ASM_CONST(0x0000000000000020)
5362306a36Sopenharmony_ci#define SLB_VSID_LP_11		ASM_CONST(0x0000000000000030)
5462306a36Sopenharmony_ci#define SLB_VSID_LLP		(SLB_VSID_L|SLB_VSID_LP)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define SLB_VSID_KERNEL		(SLB_VSID_KP)
5762306a36Sopenharmony_ci#define SLB_VSID_USER		(SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci#define SLBIE_C			(0x08000000)
6062306a36Sopenharmony_ci#define SLBIE_SSIZE_SHIFT	25
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/*
6362306a36Sopenharmony_ci * Hash table
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#define HPTES_PER_GROUP 8
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define HPTE_V_SSIZE_SHIFT	62
6962306a36Sopenharmony_ci#define HPTE_V_AVPN_SHIFT	7
7062306a36Sopenharmony_ci#define HPTE_V_COMMON_BITS	ASM_CONST(0x000fffffffffffff)
7162306a36Sopenharmony_ci#define HPTE_V_AVPN		ASM_CONST(0x3fffffffffffff80)
7262306a36Sopenharmony_ci#define HPTE_V_AVPN_3_0		ASM_CONST(0x000fffffffffff80)
7362306a36Sopenharmony_ci#define HPTE_V_AVPN_VAL(x)	(((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
7462306a36Sopenharmony_ci#define HPTE_V_COMPARE(x,y)	(!(((x) ^ (y)) & 0xffffffffffffff80UL))
7562306a36Sopenharmony_ci#define HPTE_V_BOLTED		ASM_CONST(0x0000000000000010)
7662306a36Sopenharmony_ci#define HPTE_V_LOCK		ASM_CONST(0x0000000000000008)
7762306a36Sopenharmony_ci#define HPTE_V_LARGE		ASM_CONST(0x0000000000000004)
7862306a36Sopenharmony_ci#define HPTE_V_SECONDARY	ASM_CONST(0x0000000000000002)
7962306a36Sopenharmony_ci#define HPTE_V_VALID		ASM_CONST(0x0000000000000001)
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci/*
8262306a36Sopenharmony_ci * ISA 3.0 has a different HPTE format.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci#define HPTE_R_3_0_SSIZE_SHIFT	58
8562306a36Sopenharmony_ci#define HPTE_R_3_0_SSIZE_MASK	(3ull << HPTE_R_3_0_SSIZE_SHIFT)
8662306a36Sopenharmony_ci#define HPTE_R_PP0		ASM_CONST(0x8000000000000000)
8762306a36Sopenharmony_ci#define HPTE_R_TS		ASM_CONST(0x4000000000000000)
8862306a36Sopenharmony_ci#define HPTE_R_KEY_HI		ASM_CONST(0x3000000000000000)
8962306a36Sopenharmony_ci#define HPTE_R_KEY_BIT4		ASM_CONST(0x2000000000000000)
9062306a36Sopenharmony_ci#define HPTE_R_KEY_BIT3		ASM_CONST(0x1000000000000000)
9162306a36Sopenharmony_ci#define HPTE_R_RPN_SHIFT	12
9262306a36Sopenharmony_ci#define HPTE_R_RPN		ASM_CONST(0x0ffffffffffff000)
9362306a36Sopenharmony_ci#define HPTE_R_RPN_3_0		ASM_CONST(0x01fffffffffff000)
9462306a36Sopenharmony_ci#define HPTE_R_PP		ASM_CONST(0x0000000000000003)
9562306a36Sopenharmony_ci#define HPTE_R_PPP		ASM_CONST(0x8000000000000003)
9662306a36Sopenharmony_ci#define HPTE_R_N		ASM_CONST(0x0000000000000004)
9762306a36Sopenharmony_ci#define HPTE_R_G		ASM_CONST(0x0000000000000008)
9862306a36Sopenharmony_ci#define HPTE_R_M		ASM_CONST(0x0000000000000010)
9962306a36Sopenharmony_ci#define HPTE_R_I		ASM_CONST(0x0000000000000020)
10062306a36Sopenharmony_ci#define HPTE_R_W		ASM_CONST(0x0000000000000040)
10162306a36Sopenharmony_ci#define HPTE_R_WIMG		ASM_CONST(0x0000000000000078)
10262306a36Sopenharmony_ci#define HPTE_R_C		ASM_CONST(0x0000000000000080)
10362306a36Sopenharmony_ci#define HPTE_R_R		ASM_CONST(0x0000000000000100)
10462306a36Sopenharmony_ci#define HPTE_R_KEY_LO		ASM_CONST(0x0000000000000e00)
10562306a36Sopenharmony_ci#define HPTE_R_KEY_BIT2		ASM_CONST(0x0000000000000800)
10662306a36Sopenharmony_ci#define HPTE_R_KEY_BIT1		ASM_CONST(0x0000000000000400)
10762306a36Sopenharmony_ci#define HPTE_R_KEY_BIT0		ASM_CONST(0x0000000000000200)
10862306a36Sopenharmony_ci#define HPTE_R_KEY		(HPTE_R_KEY_LO | HPTE_R_KEY_HI)
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci#define HPTE_V_1TB_SEG		ASM_CONST(0x4000000000000000)
11162306a36Sopenharmony_ci#define HPTE_V_VRMA_MASK	ASM_CONST(0x4001ffffff000000)
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci/* Values for PP (assumes Ks=0, Kp=1) */
11462306a36Sopenharmony_ci#define PP_RWXX	0	/* Supervisor read/write, User none */
11562306a36Sopenharmony_ci#define PP_RWRX 1	/* Supervisor read/write, User read */
11662306a36Sopenharmony_ci#define PP_RWRW 2	/* Supervisor read/write, User read/write */
11762306a36Sopenharmony_ci#define PP_RXRX 3	/* Supervisor read,       User read */
11862306a36Sopenharmony_ci#define PP_RXXX	(HPTE_R_PP0 | 2)	/* Supervisor read, user none */
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/* Fields for tlbiel instruction in architecture 2.06 */
12162306a36Sopenharmony_ci#define TLBIEL_INVAL_SEL_MASK	0xc00	/* invalidation selector */
12262306a36Sopenharmony_ci#define  TLBIEL_INVAL_PAGE	0x000	/* invalidate a single page */
12362306a36Sopenharmony_ci#define  TLBIEL_INVAL_SET_LPID	0x800	/* invalidate a set for current LPID */
12462306a36Sopenharmony_ci#define  TLBIEL_INVAL_SET	0xc00	/* invalidate a set for all LPIDs */
12562306a36Sopenharmony_ci#define TLBIEL_INVAL_SET_MASK	0xfff000	/* set number to inval. */
12662306a36Sopenharmony_ci#define TLBIEL_INVAL_SET_SHIFT	12
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#define POWER7_TLB_SETS		128	/* # sets in POWER7 TLB */
12962306a36Sopenharmony_ci#define POWER8_TLB_SETS		512	/* # sets in POWER8 TLB */
13062306a36Sopenharmony_ci#define POWER9_TLB_SETS_HASH	256	/* # sets in POWER9 TLB Hash mode */
13162306a36Sopenharmony_ci#define POWER9_TLB_SETS_RADIX	128	/* # sets in POWER9 TLB Radix mode */
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci#ifndef __ASSEMBLY__
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_cistruct mmu_hash_ops {
13662306a36Sopenharmony_ci	void            (*hpte_invalidate)(unsigned long slot,
13762306a36Sopenharmony_ci					   unsigned long vpn,
13862306a36Sopenharmony_ci					   int bpsize, int apsize,
13962306a36Sopenharmony_ci					   int ssize, int local);
14062306a36Sopenharmony_ci	long		(*hpte_updatepp)(unsigned long slot,
14162306a36Sopenharmony_ci					 unsigned long newpp,
14262306a36Sopenharmony_ci					 unsigned long vpn,
14362306a36Sopenharmony_ci					 int bpsize, int apsize,
14462306a36Sopenharmony_ci					 int ssize, unsigned long flags);
14562306a36Sopenharmony_ci	void            (*hpte_updateboltedpp)(unsigned long newpp,
14662306a36Sopenharmony_ci					       unsigned long ea,
14762306a36Sopenharmony_ci					       int psize, int ssize);
14862306a36Sopenharmony_ci	long		(*hpte_insert)(unsigned long hpte_group,
14962306a36Sopenharmony_ci				       unsigned long vpn,
15062306a36Sopenharmony_ci				       unsigned long prpn,
15162306a36Sopenharmony_ci				       unsigned long rflags,
15262306a36Sopenharmony_ci				       unsigned long vflags,
15362306a36Sopenharmony_ci				       int psize, int apsize,
15462306a36Sopenharmony_ci				       int ssize);
15562306a36Sopenharmony_ci	long		(*hpte_remove)(unsigned long hpte_group);
15662306a36Sopenharmony_ci	int             (*hpte_removebolted)(unsigned long ea,
15762306a36Sopenharmony_ci					     int psize, int ssize);
15862306a36Sopenharmony_ci	void		(*flush_hash_range)(unsigned long number, int local);
15962306a36Sopenharmony_ci	void		(*hugepage_invalidate)(unsigned long vsid,
16062306a36Sopenharmony_ci					       unsigned long addr,
16162306a36Sopenharmony_ci					       unsigned char *hpte_slot_array,
16262306a36Sopenharmony_ci					       int psize, int ssize, int local);
16362306a36Sopenharmony_ci	int		(*resize_hpt)(unsigned long shift);
16462306a36Sopenharmony_ci	/*
16562306a36Sopenharmony_ci	 * Special for kexec.
16662306a36Sopenharmony_ci	 * To be called in real mode with interrupts disabled. No locks are
16762306a36Sopenharmony_ci	 * taken as such, concurrent access on pre POWER5 hardware could result
16862306a36Sopenharmony_ci	 * in a deadlock.
16962306a36Sopenharmony_ci	 * The linear mapping is destroyed as well.
17062306a36Sopenharmony_ci	 */
17162306a36Sopenharmony_ci	void		(*hpte_clear_all)(void);
17262306a36Sopenharmony_ci};
17362306a36Sopenharmony_ciextern struct mmu_hash_ops mmu_hash_ops;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistruct hash_pte {
17662306a36Sopenharmony_ci	__be64 v;
17762306a36Sopenharmony_ci	__be64 r;
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciextern struct hash_pte *htab_address;
18162306a36Sopenharmony_ciextern unsigned long htab_size_bytes;
18262306a36Sopenharmony_ciextern unsigned long htab_hash_mask;
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic inline int shift_to_mmu_psize(unsigned int shift)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	int psize;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
19062306a36Sopenharmony_ci		if (mmu_psize_defs[psize].shift == shift)
19162306a36Sopenharmony_ci			return psize;
19262306a36Sopenharmony_ci	return -1;
19362306a36Sopenharmony_ci}
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_cistatic inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
19662306a36Sopenharmony_ci{
19762306a36Sopenharmony_ci	if (mmu_psize_defs[mmu_psize].shift)
19862306a36Sopenharmony_ci		return mmu_psize_defs[mmu_psize].shift;
19962306a36Sopenharmony_ci	BUG();
20062306a36Sopenharmony_ci}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_cistatic inline unsigned int ap_to_shift(unsigned long ap)
20362306a36Sopenharmony_ci{
20462306a36Sopenharmony_ci	int psize;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
20762306a36Sopenharmony_ci		if (mmu_psize_defs[psize].ap == ap)
20862306a36Sopenharmony_ci			return mmu_psize_defs[psize].shift;
20962306a36Sopenharmony_ci	}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	return -1;
21262306a36Sopenharmony_ci}
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_cistatic inline unsigned long get_sllp_encoding(int psize)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	unsigned long sllp;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
21962306a36Sopenharmony_ci		((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
22062306a36Sopenharmony_ci	return sllp;
22162306a36Sopenharmony_ci}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci/*
22662306a36Sopenharmony_ci * Segment sizes.
22762306a36Sopenharmony_ci * These are the values used by hardware in the B field of
22862306a36Sopenharmony_ci * SLB entries and the first dword of MMU hashtable entries.
22962306a36Sopenharmony_ci * The B field is 2 bits; the values 2 and 3 are unused and reserved.
23062306a36Sopenharmony_ci */
23162306a36Sopenharmony_ci#define MMU_SEGSIZE_256M	0
23262306a36Sopenharmony_ci#define MMU_SEGSIZE_1T		1
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci/*
23562306a36Sopenharmony_ci * encode page number shift.
23662306a36Sopenharmony_ci * in order to fit the 78 bit va in a 64 bit variable we shift the va by
23762306a36Sopenharmony_ci * 12 bits. This enable us to address upto 76 bit va.
23862306a36Sopenharmony_ci * For hpt hash from a va we can ignore the page size bits of va and for
23962306a36Sopenharmony_ci * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
24062306a36Sopenharmony_ci * we work in all cases including 4k page size.
24162306a36Sopenharmony_ci */
24262306a36Sopenharmony_ci#define VPN_SHIFT	12
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci/*
24562306a36Sopenharmony_ci * HPTE Large Page (LP) details
24662306a36Sopenharmony_ci */
24762306a36Sopenharmony_ci#define LP_SHIFT	12
24862306a36Sopenharmony_ci#define LP_BITS		8
24962306a36Sopenharmony_ci#define LP_MASK(i)	((0xFF >> (i)) << LP_SHIFT)
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci#ifndef __ASSEMBLY__
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_cistatic inline int slb_vsid_shift(int ssize)
25462306a36Sopenharmony_ci{
25562306a36Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M)
25662306a36Sopenharmony_ci		return SLB_VSID_SHIFT;
25762306a36Sopenharmony_ci	return SLB_VSID_SHIFT_1T;
25862306a36Sopenharmony_ci}
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_cistatic inline int segment_shift(int ssize)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M)
26362306a36Sopenharmony_ci		return SID_SHIFT;
26462306a36Sopenharmony_ci	return SID_SHIFT_1T;
26562306a36Sopenharmony_ci}
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/*
26862306a36Sopenharmony_ci * This array is indexed by the LP field of the HPTE second dword.
26962306a36Sopenharmony_ci * Since this field may contain some RPN bits, some entries are
27062306a36Sopenharmony_ci * replicated so that we get the same value irrespective of RPN.
27162306a36Sopenharmony_ci * The top 4 bits are the page size index (MMU_PAGE_*) for the
27262306a36Sopenharmony_ci * actual page size, the bottom 4 bits are the base page size.
27362306a36Sopenharmony_ci */
27462306a36Sopenharmony_ciextern u8 hpte_page_sizes[1 << LP_BITS];
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_cistatic inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
27762306a36Sopenharmony_ci					     bool is_base_size)
27862306a36Sopenharmony_ci{
27962306a36Sopenharmony_ci	unsigned int i, lp;
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci	if (!(h & HPTE_V_LARGE))
28262306a36Sopenharmony_ci		return 1ul << 12;
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	/* Look at the 8 bit LP value */
28562306a36Sopenharmony_ci	lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
28662306a36Sopenharmony_ci	i = hpte_page_sizes[lp];
28762306a36Sopenharmony_ci	if (!i)
28862306a36Sopenharmony_ci		return 0;
28962306a36Sopenharmony_ci	if (!is_base_size)
29062306a36Sopenharmony_ci		i >>= 4;
29162306a36Sopenharmony_ci	return 1ul << mmu_psize_defs[i & 0xf].shift;
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_cistatic inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
29562306a36Sopenharmony_ci{
29662306a36Sopenharmony_ci	return __hpte_page_size(h, l, 0);
29762306a36Sopenharmony_ci}
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_cistatic inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
30062306a36Sopenharmony_ci{
30162306a36Sopenharmony_ci	return __hpte_page_size(h, l, 1);
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/*
30562306a36Sopenharmony_ci * The current system page and segment sizes
30662306a36Sopenharmony_ci */
30762306a36Sopenharmony_ciextern int mmu_kernel_ssize;
30862306a36Sopenharmony_ciextern int mmu_highuser_ssize;
30962306a36Sopenharmony_ciextern u16 mmu_slb_size;
31062306a36Sopenharmony_ciextern unsigned long tce_alloc_start, tce_alloc_end;
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_ci/*
31362306a36Sopenharmony_ci * If the processor supports 64k normal pages but not 64k cache
31462306a36Sopenharmony_ci * inhibited pages, we have to be prepared to switch processes
31562306a36Sopenharmony_ci * to use 4k pages when they create cache-inhibited mappings.
31662306a36Sopenharmony_ci * If this is the case, mmu_ci_restrictions will be set to 1.
31762306a36Sopenharmony_ci */
31862306a36Sopenharmony_ciextern int mmu_ci_restrictions;
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci/*
32162306a36Sopenharmony_ci * This computes the AVPN and B fields of the first dword of a HPTE,
32262306a36Sopenharmony_ci * for use when we want to match an existing PTE.  The bottom 7 bits
32362306a36Sopenharmony_ci * of the returned value are zero.
32462306a36Sopenharmony_ci */
32562306a36Sopenharmony_cistatic inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
32662306a36Sopenharmony_ci					     int ssize)
32762306a36Sopenharmony_ci{
32862306a36Sopenharmony_ci	unsigned long v;
32962306a36Sopenharmony_ci	/*
33062306a36Sopenharmony_ci	 * The AVA field omits the low-order 23 bits of the 78 bits VA.
33162306a36Sopenharmony_ci	 * These bits are not needed in the PTE, because the
33262306a36Sopenharmony_ci	 * low-order b of these bits are part of the byte offset
33362306a36Sopenharmony_ci	 * into the virtual page and, if b < 23, the high-order
33462306a36Sopenharmony_ci	 * 23-b of these bits are always used in selecting the
33562306a36Sopenharmony_ci	 * PTEGs to be searched
33662306a36Sopenharmony_ci	 */
33762306a36Sopenharmony_ci	v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
33862306a36Sopenharmony_ci	v <<= HPTE_V_AVPN_SHIFT;
33962306a36Sopenharmony_ci	v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
34062306a36Sopenharmony_ci	return v;
34162306a36Sopenharmony_ci}
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci/*
34462306a36Sopenharmony_ci * ISA v3.0 defines a new HPTE format, which differs from the old
34562306a36Sopenharmony_ci * format in having smaller AVPN and ARPN fields, and the B field
34662306a36Sopenharmony_ci * in the second dword instead of the first.
34762306a36Sopenharmony_ci */
34862306a36Sopenharmony_cistatic inline unsigned long hpte_old_to_new_v(unsigned long v)
34962306a36Sopenharmony_ci{
35062306a36Sopenharmony_ci	/* trim AVPN, drop B */
35162306a36Sopenharmony_ci	return v & HPTE_V_COMMON_BITS;
35262306a36Sopenharmony_ci}
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_cistatic inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
35562306a36Sopenharmony_ci{
35662306a36Sopenharmony_ci	/* move B field from 1st to 2nd dword, trim ARPN */
35762306a36Sopenharmony_ci	return (r & ~HPTE_R_3_0_SSIZE_MASK) |
35862306a36Sopenharmony_ci		(((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
35962306a36Sopenharmony_ci}
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_cistatic inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
36262306a36Sopenharmony_ci{
36362306a36Sopenharmony_ci	/* insert B field */
36462306a36Sopenharmony_ci	return (v & HPTE_V_COMMON_BITS) |
36562306a36Sopenharmony_ci		((r & HPTE_R_3_0_SSIZE_MASK) <<
36662306a36Sopenharmony_ci		 (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
36762306a36Sopenharmony_ci}
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_cistatic inline unsigned long hpte_new_to_old_r(unsigned long r)
37062306a36Sopenharmony_ci{
37162306a36Sopenharmony_ci	/* clear out B field */
37262306a36Sopenharmony_ci	return r & ~HPTE_R_3_0_SSIZE_MASK;
37362306a36Sopenharmony_ci}
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_cistatic inline unsigned long hpte_get_old_v(struct hash_pte *hptep)
37662306a36Sopenharmony_ci{
37762306a36Sopenharmony_ci	unsigned long hpte_v;
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	hpte_v = be64_to_cpu(hptep->v);
38062306a36Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_300))
38162306a36Sopenharmony_ci		hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
38262306a36Sopenharmony_ci	return hpte_v;
38362306a36Sopenharmony_ci}
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci/*
38662306a36Sopenharmony_ci * This function sets the AVPN and L fields of the HPTE  appropriately
38762306a36Sopenharmony_ci * using the base page size and actual page size.
38862306a36Sopenharmony_ci */
38962306a36Sopenharmony_cistatic inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
39062306a36Sopenharmony_ci					  int actual_psize, int ssize)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	unsigned long v;
39362306a36Sopenharmony_ci	v = hpte_encode_avpn(vpn, base_psize, ssize);
39462306a36Sopenharmony_ci	if (actual_psize != MMU_PAGE_4K)
39562306a36Sopenharmony_ci		v |= HPTE_V_LARGE;
39662306a36Sopenharmony_ci	return v;
39762306a36Sopenharmony_ci}
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci/*
40062306a36Sopenharmony_ci * This function sets the ARPN, and LP fields of the HPTE appropriately
40162306a36Sopenharmony_ci * for the page size. We assume the pa is already "clean" that is properly
40262306a36Sopenharmony_ci * aligned for the requested page size
40362306a36Sopenharmony_ci */
40462306a36Sopenharmony_cistatic inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
40562306a36Sopenharmony_ci					  int actual_psize)
40662306a36Sopenharmony_ci{
40762306a36Sopenharmony_ci	/* A 4K page needs no special encoding */
40862306a36Sopenharmony_ci	if (actual_psize == MMU_PAGE_4K)
40962306a36Sopenharmony_ci		return pa & HPTE_R_RPN;
41062306a36Sopenharmony_ci	else {
41162306a36Sopenharmony_ci		unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
41262306a36Sopenharmony_ci		unsigned int shift = mmu_psize_defs[actual_psize].shift;
41362306a36Sopenharmony_ci		return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
41462306a36Sopenharmony_ci	}
41562306a36Sopenharmony_ci}
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci/*
41862306a36Sopenharmony_ci * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
41962306a36Sopenharmony_ci */
42062306a36Sopenharmony_cistatic inline unsigned long hpt_vpn(unsigned long ea,
42162306a36Sopenharmony_ci				    unsigned long vsid, int ssize)
42262306a36Sopenharmony_ci{
42362306a36Sopenharmony_ci	unsigned long mask;
42462306a36Sopenharmony_ci	int s_shift = segment_shift(ssize);
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci	mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
42762306a36Sopenharmony_ci	return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
42862306a36Sopenharmony_ci}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci/*
43162306a36Sopenharmony_ci * This hashes a virtual address
43262306a36Sopenharmony_ci */
43362306a36Sopenharmony_cistatic inline unsigned long hpt_hash(unsigned long vpn,
43462306a36Sopenharmony_ci				     unsigned int shift, int ssize)
43562306a36Sopenharmony_ci{
43662306a36Sopenharmony_ci	unsigned long mask;
43762306a36Sopenharmony_ci	unsigned long hash, vsid;
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci	/* VPN_SHIFT can be atmost 12 */
44062306a36Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M) {
44162306a36Sopenharmony_ci		mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
44262306a36Sopenharmony_ci		hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
44362306a36Sopenharmony_ci			((vpn & mask) >> (shift - VPN_SHIFT));
44462306a36Sopenharmony_ci	} else {
44562306a36Sopenharmony_ci		mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
44662306a36Sopenharmony_ci		vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
44762306a36Sopenharmony_ci		hash = vsid ^ (vsid << 25) ^
44862306a36Sopenharmony_ci			((vpn & mask) >> (shift - VPN_SHIFT)) ;
44962306a36Sopenharmony_ci	}
45062306a36Sopenharmony_ci	return hash & 0x7fffffffffUL;
45162306a36Sopenharmony_ci}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci#define HPTE_LOCAL_UPDATE	0x1
45462306a36Sopenharmony_ci#define HPTE_NOHPTE_UPDATE	0x2
45562306a36Sopenharmony_ci#define HPTE_USE_KERNEL_KEY	0x4
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_cilong hpte_insert_repeating(unsigned long hash, unsigned long vpn, unsigned long pa,
45862306a36Sopenharmony_ci			   unsigned long rlags, unsigned long vflags, int psize, int ssize);
45962306a36Sopenharmony_ciextern int __hash_page_4K(unsigned long ea, unsigned long access,
46062306a36Sopenharmony_ci			  unsigned long vsid, pte_t *ptep, unsigned long trap,
46162306a36Sopenharmony_ci			  unsigned long flags, int ssize, int subpage_prot);
46262306a36Sopenharmony_ciextern int __hash_page_64K(unsigned long ea, unsigned long access,
46362306a36Sopenharmony_ci			   unsigned long vsid, pte_t *ptep, unsigned long trap,
46462306a36Sopenharmony_ci			   unsigned long flags, int ssize);
46562306a36Sopenharmony_cistruct mm_struct;
46662306a36Sopenharmony_ciunsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
46762306a36Sopenharmony_ciextern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
46862306a36Sopenharmony_ci			unsigned long access, unsigned long trap,
46962306a36Sopenharmony_ci			unsigned long flags);
47062306a36Sopenharmony_ciextern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
47162306a36Sopenharmony_ci		     unsigned long dsisr);
47262306a36Sopenharmony_civoid low_hash_fault(struct pt_regs *regs, unsigned long address, int rc);
47362306a36Sopenharmony_ciint __hash_page(unsigned long trap, unsigned long ea, unsigned long dsisr, unsigned long msr);
47462306a36Sopenharmony_ciint __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
47562306a36Sopenharmony_ci		     pte_t *ptep, unsigned long trap, unsigned long flags,
47662306a36Sopenharmony_ci		     int ssize, unsigned int shift, unsigned int mmu_psize);
47762306a36Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
47862306a36Sopenharmony_ciextern int __hash_page_thp(unsigned long ea, unsigned long access,
47962306a36Sopenharmony_ci			   unsigned long vsid, pmd_t *pmdp, unsigned long trap,
48062306a36Sopenharmony_ci			   unsigned long flags, int ssize, unsigned int psize);
48162306a36Sopenharmony_ci#else
48262306a36Sopenharmony_cistatic inline int __hash_page_thp(unsigned long ea, unsigned long access,
48362306a36Sopenharmony_ci				  unsigned long vsid, pmd_t *pmdp,
48462306a36Sopenharmony_ci				  unsigned long trap, unsigned long flags,
48562306a36Sopenharmony_ci				  int ssize, unsigned int psize)
48662306a36Sopenharmony_ci{
48762306a36Sopenharmony_ci	BUG();
48862306a36Sopenharmony_ci	return -1;
48962306a36Sopenharmony_ci}
49062306a36Sopenharmony_ci#endif
49162306a36Sopenharmony_ciextern void hash_failure_debug(unsigned long ea, unsigned long access,
49262306a36Sopenharmony_ci			       unsigned long vsid, unsigned long trap,
49362306a36Sopenharmony_ci			       int ssize, int psize, int lpsize,
49462306a36Sopenharmony_ci			       unsigned long pte);
49562306a36Sopenharmony_ciextern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
49662306a36Sopenharmony_ci			     unsigned long pstart, unsigned long prot,
49762306a36Sopenharmony_ci			     int psize, int ssize);
49862306a36Sopenharmony_ciint htab_remove_mapping(unsigned long vstart, unsigned long vend,
49962306a36Sopenharmony_ci			int psize, int ssize);
50062306a36Sopenharmony_ciextern void pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
50162306a36Sopenharmony_ciextern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ciextern void hash__setup_new_exec(void);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci#ifdef CONFIG_PPC_PSERIES
50662306a36Sopenharmony_civoid hpte_init_pseries(void);
50762306a36Sopenharmony_ci#else
50862306a36Sopenharmony_cistatic inline void hpte_init_pseries(void) { }
50962306a36Sopenharmony_ci#endif
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ciextern void hpte_init_native(void);
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_cistruct slb_entry {
51462306a36Sopenharmony_ci	u64	esid;
51562306a36Sopenharmony_ci	u64	vsid;
51662306a36Sopenharmony_ci};
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ciextern void slb_initialize(void);
51962306a36Sopenharmony_civoid slb_flush_and_restore_bolted(void);
52062306a36Sopenharmony_civoid slb_flush_all_realmode(void);
52162306a36Sopenharmony_civoid __slb_restore_bolted_realmode(void);
52262306a36Sopenharmony_civoid slb_restore_bolted_realmode(void);
52362306a36Sopenharmony_civoid slb_save_contents(struct slb_entry *slb_ptr);
52462306a36Sopenharmony_civoid slb_dump_contents(struct slb_entry *slb_ptr);
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciextern void slb_vmalloc_update(void);
52762306a36Sopenharmony_civoid preload_new_slb_context(unsigned long start, unsigned long sp);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_ci#ifdef CONFIG_PPC_64S_HASH_MMU
53062306a36Sopenharmony_civoid slb_set_size(u16 size);
53162306a36Sopenharmony_ci#else
53262306a36Sopenharmony_cistatic inline void slb_set_size(u16 size) { }
53362306a36Sopenharmony_ci#endif
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci/*
53862306a36Sopenharmony_ci * VSID allocation (256MB segment)
53962306a36Sopenharmony_ci *
54062306a36Sopenharmony_ci * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
54162306a36Sopenharmony_ci * from mmu context id and effective segment id of the address.
54262306a36Sopenharmony_ci *
54362306a36Sopenharmony_ci * For user processes max context id is limited to MAX_USER_CONTEXT.
54462306a36Sopenharmony_ci * more details in get_user_context
54562306a36Sopenharmony_ci *
54662306a36Sopenharmony_ci * For kernel space get_kernel_context
54762306a36Sopenharmony_ci *
54862306a36Sopenharmony_ci * The proto-VSIDs are then scrambled into real VSIDs with the
54962306a36Sopenharmony_ci * multiplicative hash:
55062306a36Sopenharmony_ci *
55162306a36Sopenharmony_ci *	VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
55262306a36Sopenharmony_ci *
55362306a36Sopenharmony_ci * VSID_MULTIPLIER is prime, so in particular it is
55462306a36Sopenharmony_ci * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
55562306a36Sopenharmony_ci * Because the modulus is 2^n-1 we can compute it efficiently without
55662306a36Sopenharmony_ci * a divide or extra multiply (see below). The scramble function gives
55762306a36Sopenharmony_ci * robust scattering in the hash table (at least based on some initial
55862306a36Sopenharmony_ci * results).
55962306a36Sopenharmony_ci *
56062306a36Sopenharmony_ci * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
56162306a36Sopenharmony_ci * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
56262306a36Sopenharmony_ci * will produce a VSID of 0.
56362306a36Sopenharmony_ci *
56462306a36Sopenharmony_ci * We also need to avoid the last segment of the last context, because that
56562306a36Sopenharmony_ci * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
56662306a36Sopenharmony_ci * because of the modulo operation in vsid scramble.
56762306a36Sopenharmony_ci */
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci/*
57062306a36Sopenharmony_ci * Max Va bits we support as of now is 68 bits. We want 19 bit
57162306a36Sopenharmony_ci * context ID.
57262306a36Sopenharmony_ci * Restrictions:
57362306a36Sopenharmony_ci * GPU has restrictions of not able to access beyond 128TB
57462306a36Sopenharmony_ci * (47 bit effective address). We also cannot do more than 20bit PID.
57562306a36Sopenharmony_ci * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
57662306a36Sopenharmony_ci * to 16 bits (ie, we can only have 2^16 pids at the same time).
57762306a36Sopenharmony_ci */
57862306a36Sopenharmony_ci#define VA_BITS			68
57962306a36Sopenharmony_ci#define CONTEXT_BITS		19
58062306a36Sopenharmony_ci#define ESID_BITS		(VA_BITS - (SID_SHIFT + CONTEXT_BITS))
58162306a36Sopenharmony_ci#define ESID_BITS_1T		(VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_ci#define ESID_BITS_MASK		((1 << ESID_BITS) - 1)
58462306a36Sopenharmony_ci#define ESID_BITS_1T_MASK	((1 << ESID_BITS_1T) - 1)
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci/*
58762306a36Sopenharmony_ci * Now certain config support MAX_PHYSMEM more than 512TB. Hence we will need
58862306a36Sopenharmony_ci * to use more than one context for linear mapping the kernel.
58962306a36Sopenharmony_ci * For vmalloc and memmap, we use just one context with 512TB. With 64 byte
59062306a36Sopenharmony_ci * struct page size, we need ony 32 TB in memmap for 2PB (51 bits (MAX_PHYSMEM_BITS)).
59162306a36Sopenharmony_ci */
59262306a36Sopenharmony_ci#if (H_MAX_PHYSMEM_BITS > MAX_EA_BITS_PER_CONTEXT)
59362306a36Sopenharmony_ci#define MAX_KERNEL_CTX_CNT	(1UL << (H_MAX_PHYSMEM_BITS - MAX_EA_BITS_PER_CONTEXT))
59462306a36Sopenharmony_ci#else
59562306a36Sopenharmony_ci#define MAX_KERNEL_CTX_CNT	1
59662306a36Sopenharmony_ci#endif
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci#define MAX_VMALLOC_CTX_CNT	1
59962306a36Sopenharmony_ci#define MAX_IO_CTX_CNT		1
60062306a36Sopenharmony_ci#define MAX_VMEMMAP_CTX_CNT	1
60162306a36Sopenharmony_ci
60262306a36Sopenharmony_ci/*
60362306a36Sopenharmony_ci * 256MB segment
60462306a36Sopenharmony_ci * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
60562306a36Sopenharmony_ci * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
60662306a36Sopenharmony_ci * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
60762306a36Sopenharmony_ci * context maps 2^49 bytes (512TB).
60862306a36Sopenharmony_ci *
60962306a36Sopenharmony_ci * We also need to avoid the last segment of the last context, because that
61062306a36Sopenharmony_ci * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
61162306a36Sopenharmony_ci * because of the modulo operation in vsid scramble.
61262306a36Sopenharmony_ci *
61362306a36Sopenharmony_ci */
61462306a36Sopenharmony_ci#define MAX_USER_CONTEXT	((ASM_CONST(1) << CONTEXT_BITS) - 2)
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ci// The + 2 accounts for INVALID_REGION and 1 more to avoid overlap with kernel
61762306a36Sopenharmony_ci#define MIN_USER_CONTEXT	(MAX_KERNEL_CTX_CNT + MAX_VMALLOC_CTX_CNT + \
61862306a36Sopenharmony_ci				 MAX_IO_CTX_CNT + MAX_VMEMMAP_CTX_CNT + 2)
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci/*
62162306a36Sopenharmony_ci * For platforms that support on 65bit VA we limit the context bits
62262306a36Sopenharmony_ci */
62362306a36Sopenharmony_ci#define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_ci/*
62662306a36Sopenharmony_ci * This should be computed such that protovosid * vsid_mulitplier
62762306a36Sopenharmony_ci * doesn't overflow 64 bits. The vsid_mutliplier should also be
62862306a36Sopenharmony_ci * co-prime to vsid_modulus. We also need to make sure that number
62962306a36Sopenharmony_ci * of bits in multiplied result (dividend) is less than twice the number of
63062306a36Sopenharmony_ci * protovsid bits for our modulus optmization to work.
63162306a36Sopenharmony_ci *
63262306a36Sopenharmony_ci * The below table shows the current values used.
63362306a36Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
63462306a36Sopenharmony_ci * |       | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
63562306a36Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
63662306a36Sopenharmony_ci * | 1T    |         24 |                   25 |         49 |                50 |
63762306a36Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
63862306a36Sopenharmony_ci * | 256MB |         24 |                   37 |         61 |                74 |
63962306a36Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
64062306a36Sopenharmony_ci *
64162306a36Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
64262306a36Sopenharmony_ci * |       | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
64362306a36Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
64462306a36Sopenharmony_ci * | 1T    |         24 |                   28 |         52 |                 56 |
64562306a36Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
64662306a36Sopenharmony_ci * | 256MB |         24 |                   40 |         64 |                 80 |
64762306a36Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
64862306a36Sopenharmony_ci *
64962306a36Sopenharmony_ci */
65062306a36Sopenharmony_ci#define VSID_MULTIPLIER_256M	ASM_CONST(12538073)	/* 24-bit prime */
65162306a36Sopenharmony_ci#define VSID_BITS_256M		(VA_BITS - SID_SHIFT)
65262306a36Sopenharmony_ci#define VSID_BITS_65_256M	(65 - SID_SHIFT)
65362306a36Sopenharmony_ci/*
65462306a36Sopenharmony_ci * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
65562306a36Sopenharmony_ci */
65662306a36Sopenharmony_ci#define VSID_MULINV_256M	ASM_CONST(665548017062)
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci#define VSID_MULTIPLIER_1T	ASM_CONST(12538073)	/* 24-bit prime */
65962306a36Sopenharmony_ci#define VSID_BITS_1T		(VA_BITS - SID_SHIFT_1T)
66062306a36Sopenharmony_ci#define VSID_BITS_65_1T		(65 - SID_SHIFT_1T)
66162306a36Sopenharmony_ci#define VSID_MULINV_1T		ASM_CONST(209034062)
66262306a36Sopenharmony_ci
66362306a36Sopenharmony_ci/* 1TB VSID reserved for VRMA */
66462306a36Sopenharmony_ci#define VRMA_VSID	0x1ffffffUL
66562306a36Sopenharmony_ci#define USER_VSID_RANGE	(1UL << (ESID_BITS + SID_SHIFT))
66662306a36Sopenharmony_ci
66762306a36Sopenharmony_ci/* 4 bits per slice and we have one slice per 1TB */
66862306a36Sopenharmony_ci#define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
66962306a36Sopenharmony_ci#define LOW_SLICE_ARRAY_SZ	(BITS_PER_LONG / BITS_PER_BYTE)
67062306a36Sopenharmony_ci#define TASK_SLICE_ARRAY_SZ(x)	((x)->hash_context->slb_addr_limit >> 41)
67162306a36Sopenharmony_ci#ifndef __ASSEMBLY__
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci#ifdef CONFIG_PPC_SUBPAGE_PROT
67462306a36Sopenharmony_ci/*
67562306a36Sopenharmony_ci * For the sub-page protection option, we extend the PGD with one of
67662306a36Sopenharmony_ci * these.  Basically we have a 3-level tree, with the top level being
67762306a36Sopenharmony_ci * the protptrs array.  To optimize speed and memory consumption when
67862306a36Sopenharmony_ci * only addresses < 4GB are being protected, pointers to the first
67962306a36Sopenharmony_ci * four pages of sub-page protection words are stored in the low_prot
68062306a36Sopenharmony_ci * array.
68162306a36Sopenharmony_ci * Each page of sub-page protection words protects 1GB (4 bytes
68262306a36Sopenharmony_ci * protects 64k).  For the 3-level tree, each page of pointers then
68362306a36Sopenharmony_ci * protects 8TB.
68462306a36Sopenharmony_ci */
68562306a36Sopenharmony_cistruct subpage_prot_table {
68662306a36Sopenharmony_ci	unsigned long maxaddr;	/* only addresses < this are protected */
68762306a36Sopenharmony_ci	unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
68862306a36Sopenharmony_ci	unsigned int *low_prot[4];
68962306a36Sopenharmony_ci};
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci#define SBP_L1_BITS		(PAGE_SHIFT - 2)
69262306a36Sopenharmony_ci#define SBP_L2_BITS		(PAGE_SHIFT - 3)
69362306a36Sopenharmony_ci#define SBP_L1_COUNT		(1 << SBP_L1_BITS)
69462306a36Sopenharmony_ci#define SBP_L2_COUNT		(1 << SBP_L2_BITS)
69562306a36Sopenharmony_ci#define SBP_L2_SHIFT		(PAGE_SHIFT + SBP_L1_BITS)
69662306a36Sopenharmony_ci#define SBP_L3_SHIFT		(SBP_L2_SHIFT + SBP_L2_BITS)
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_ciextern void subpage_prot_free(struct mm_struct *mm);
69962306a36Sopenharmony_ci#else
70062306a36Sopenharmony_cistatic inline void subpage_prot_free(struct mm_struct *mm) {}
70162306a36Sopenharmony_ci#endif /* CONFIG_PPC_SUBPAGE_PROT */
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci/*
70462306a36Sopenharmony_ci * One bit per slice. We have lower slices which cover 256MB segments
70562306a36Sopenharmony_ci * upto 4G range. That gets us 16 low slices. For the rest we track slices
70662306a36Sopenharmony_ci * in 1TB size.
70762306a36Sopenharmony_ci */
70862306a36Sopenharmony_cistruct slice_mask {
70962306a36Sopenharmony_ci	u64 low_slices;
71062306a36Sopenharmony_ci	DECLARE_BITMAP(high_slices, SLICE_NUM_HIGH);
71162306a36Sopenharmony_ci};
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_cistruct hash_mm_context {
71462306a36Sopenharmony_ci	u16 user_psize; /* page size index */
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci	/* SLB page size encodings*/
71762306a36Sopenharmony_ci	unsigned char low_slices_psize[LOW_SLICE_ARRAY_SZ];
71862306a36Sopenharmony_ci	unsigned char high_slices_psize[SLICE_ARRAY_SIZE];
71962306a36Sopenharmony_ci	unsigned long slb_addr_limit;
72062306a36Sopenharmony_ci#ifdef CONFIG_PPC_64K_PAGES
72162306a36Sopenharmony_ci	struct slice_mask mask_64k;
72262306a36Sopenharmony_ci#endif
72362306a36Sopenharmony_ci	struct slice_mask mask_4k;
72462306a36Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
72562306a36Sopenharmony_ci	struct slice_mask mask_16m;
72662306a36Sopenharmony_ci	struct slice_mask mask_16g;
72762306a36Sopenharmony_ci#endif
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ci#ifdef CONFIG_PPC_SUBPAGE_PROT
73062306a36Sopenharmony_ci	struct subpage_prot_table *spt;
73162306a36Sopenharmony_ci#endif /* CONFIG_PPC_SUBPAGE_PROT */
73262306a36Sopenharmony_ci};
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci#if 0
73562306a36Sopenharmony_ci/*
73662306a36Sopenharmony_ci * The code below is equivalent to this function for arguments
73762306a36Sopenharmony_ci * < 2^VSID_BITS, which is all this should ever be called
73862306a36Sopenharmony_ci * with.  However gcc is not clever enough to compute the
73962306a36Sopenharmony_ci * modulus (2^n-1) without a second multiply.
74062306a36Sopenharmony_ci */
74162306a36Sopenharmony_ci#define vsid_scramble(protovsid, size) \
74262306a36Sopenharmony_ci	((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_ci/* simplified form avoiding mod operation */
74562306a36Sopenharmony_ci#define vsid_scramble(protovsid, size) \
74662306a36Sopenharmony_ci	({								 \
74762306a36Sopenharmony_ci		unsigned long x;					 \
74862306a36Sopenharmony_ci		x = (protovsid) * VSID_MULTIPLIER_##size;		 \
74962306a36Sopenharmony_ci		x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
75062306a36Sopenharmony_ci		(x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
75162306a36Sopenharmony_ci	})
75262306a36Sopenharmony_ci
75362306a36Sopenharmony_ci#else /* 1 */
75462306a36Sopenharmony_cistatic inline unsigned long vsid_scramble(unsigned long protovsid,
75562306a36Sopenharmony_ci				  unsigned long vsid_multiplier, int vsid_bits)
75662306a36Sopenharmony_ci{
75762306a36Sopenharmony_ci	unsigned long vsid;
75862306a36Sopenharmony_ci	unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
75962306a36Sopenharmony_ci	/*
76062306a36Sopenharmony_ci	 * We have same multipler for both 256 and 1T segements now
76162306a36Sopenharmony_ci	 */
76262306a36Sopenharmony_ci	vsid = protovsid * vsid_multiplier;
76362306a36Sopenharmony_ci	vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
76462306a36Sopenharmony_ci	return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
76562306a36Sopenharmony_ci}
76662306a36Sopenharmony_ci
76762306a36Sopenharmony_ci#endif /* 1 */
76862306a36Sopenharmony_ci
76962306a36Sopenharmony_ci/* Returns the segment size indicator for a user address */
77062306a36Sopenharmony_cistatic inline int user_segment_size(unsigned long addr)
77162306a36Sopenharmony_ci{
77262306a36Sopenharmony_ci	/* Use 1T segments if possible for addresses >= 1T */
77362306a36Sopenharmony_ci	if (addr >= (1UL << SID_SHIFT_1T))
77462306a36Sopenharmony_ci		return mmu_highuser_ssize;
77562306a36Sopenharmony_ci	return MMU_SEGSIZE_256M;
77662306a36Sopenharmony_ci}
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_cistatic inline unsigned long get_vsid(unsigned long context, unsigned long ea,
77962306a36Sopenharmony_ci				     int ssize)
78062306a36Sopenharmony_ci{
78162306a36Sopenharmony_ci	unsigned long va_bits = VA_BITS;
78262306a36Sopenharmony_ci	unsigned long vsid_bits;
78362306a36Sopenharmony_ci	unsigned long protovsid;
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci	/*
78662306a36Sopenharmony_ci	 * Bad address. We return VSID 0 for that
78762306a36Sopenharmony_ci	 */
78862306a36Sopenharmony_ci	if ((ea & EA_MASK)  >= H_PGTABLE_RANGE)
78962306a36Sopenharmony_ci		return 0;
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
79262306a36Sopenharmony_ci		va_bits = 65;
79362306a36Sopenharmony_ci
79462306a36Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M) {
79562306a36Sopenharmony_ci		vsid_bits = va_bits - SID_SHIFT;
79662306a36Sopenharmony_ci		protovsid = (context << ESID_BITS) |
79762306a36Sopenharmony_ci			((ea >> SID_SHIFT) & ESID_BITS_MASK);
79862306a36Sopenharmony_ci		return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
79962306a36Sopenharmony_ci	}
80062306a36Sopenharmony_ci	/* 1T segment */
80162306a36Sopenharmony_ci	vsid_bits = va_bits - SID_SHIFT_1T;
80262306a36Sopenharmony_ci	protovsid = (context << ESID_BITS_1T) |
80362306a36Sopenharmony_ci		((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
80462306a36Sopenharmony_ci	return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
80562306a36Sopenharmony_ci}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_ci/*
80862306a36Sopenharmony_ci * For kernel space, we use context ids as
80962306a36Sopenharmony_ci * below. Range is 512TB per context.
81062306a36Sopenharmony_ci *
81162306a36Sopenharmony_ci * 0x00001 -  [ 0xc000000000000000 - 0xc001ffffffffffff]
81262306a36Sopenharmony_ci * 0x00002 -  [ 0xc002000000000000 - 0xc003ffffffffffff]
81362306a36Sopenharmony_ci * 0x00003 -  [ 0xc004000000000000 - 0xc005ffffffffffff]
81462306a36Sopenharmony_ci * 0x00004 -  [ 0xc006000000000000 - 0xc007ffffffffffff]
81562306a36Sopenharmony_ci *
81662306a36Sopenharmony_ci * vmap, IO, vmemap
81762306a36Sopenharmony_ci *
81862306a36Sopenharmony_ci * 0x00005 -  [ 0xc008000000000000 - 0xc009ffffffffffff]
81962306a36Sopenharmony_ci * 0x00006 -  [ 0xc00a000000000000 - 0xc00bffffffffffff]
82062306a36Sopenharmony_ci * 0x00007 -  [ 0xc00c000000000000 - 0xc00dffffffffffff]
82162306a36Sopenharmony_ci *
82262306a36Sopenharmony_ci */
82362306a36Sopenharmony_cistatic inline unsigned long get_kernel_context(unsigned long ea)
82462306a36Sopenharmony_ci{
82562306a36Sopenharmony_ci	unsigned long region_id = get_region_id(ea);
82662306a36Sopenharmony_ci	unsigned long ctx;
82762306a36Sopenharmony_ci	/*
82862306a36Sopenharmony_ci	 * Depending on Kernel config, kernel region can have one context
82962306a36Sopenharmony_ci	 * or more.
83062306a36Sopenharmony_ci	 */
83162306a36Sopenharmony_ci	if (region_id == LINEAR_MAP_REGION_ID) {
83262306a36Sopenharmony_ci		/*
83362306a36Sopenharmony_ci		 * We already verified ea to be not beyond the addr limit.
83462306a36Sopenharmony_ci		 */
83562306a36Sopenharmony_ci		ctx =  1 + ((ea & EA_MASK) >> MAX_EA_BITS_PER_CONTEXT);
83662306a36Sopenharmony_ci	} else
83762306a36Sopenharmony_ci		ctx = region_id + MAX_KERNEL_CTX_CNT - 1;
83862306a36Sopenharmony_ci	return ctx;
83962306a36Sopenharmony_ci}
84062306a36Sopenharmony_ci
84162306a36Sopenharmony_ci/*
84262306a36Sopenharmony_ci * This is only valid for addresses >= PAGE_OFFSET
84362306a36Sopenharmony_ci */
84462306a36Sopenharmony_cistatic inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
84562306a36Sopenharmony_ci{
84662306a36Sopenharmony_ci	unsigned long context;
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci	if (!is_kernel_addr(ea))
84962306a36Sopenharmony_ci		return 0;
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci	context = get_kernel_context(ea);
85262306a36Sopenharmony_ci	return get_vsid(context, ea, ssize);
85362306a36Sopenharmony_ci}
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ciunsigned htab_shift_for_mem_size(unsigned long mem_size);
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_cienum slb_index {
85862306a36Sopenharmony_ci	LINEAR_INDEX	= 0, /* Kernel linear map  (0xc000000000000000) */
85962306a36Sopenharmony_ci	KSTACK_INDEX	= 1, /* Kernel stack map */
86062306a36Sopenharmony_ci};
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci#define slb_esid_mask(ssize)	\
86362306a36Sopenharmony_ci	(((ssize) == MMU_SEGSIZE_256M) ? ESID_MASK : ESID_MASK_1T)
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_cistatic inline unsigned long mk_esid_data(unsigned long ea, int ssize,
86662306a36Sopenharmony_ci					 enum slb_index index)
86762306a36Sopenharmony_ci{
86862306a36Sopenharmony_ci	return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
86962306a36Sopenharmony_ci}
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_cistatic inline unsigned long __mk_vsid_data(unsigned long vsid, int ssize,
87262306a36Sopenharmony_ci					   unsigned long flags)
87362306a36Sopenharmony_ci{
87462306a36Sopenharmony_ci	return (vsid << slb_vsid_shift(ssize)) | flags |
87562306a36Sopenharmony_ci		((unsigned long)ssize << SLB_VSID_SSIZE_SHIFT);
87662306a36Sopenharmony_ci}
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_cistatic inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
87962306a36Sopenharmony_ci					 unsigned long flags)
88062306a36Sopenharmony_ci{
88162306a36Sopenharmony_ci	return __mk_vsid_data(get_kernel_vsid(ea, ssize), ssize, flags);
88262306a36Sopenharmony_ci}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
88562306a36Sopenharmony_ci#endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */
886