18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * PowerPC64 memory management structures
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
88c2ecf20Sopenharmony_ci *   PPC64 rework.
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <asm/page.h>
128c2ecf20Sopenharmony_ci#include <asm/bug.h>
138c2ecf20Sopenharmony_ci#include <asm/asm-const.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/*
168c2ecf20Sopenharmony_ci * This is necessary to get the definition of PGTABLE_RANGE which we
178c2ecf20Sopenharmony_ci * need for various slices related matters. Note that this isn't the
188c2ecf20Sopenharmony_ci * complete pgtable.h but only a portion of it.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ci#include <asm/book3s/64/pgtable.h>
218c2ecf20Sopenharmony_ci#include <asm/bug.h>
228c2ecf20Sopenharmony_ci#include <asm/task_size_64.h>
238c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * SLB
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define SLB_NUM_BOLTED		2
308c2ecf20Sopenharmony_ci#define SLB_CACHE_ENTRIES	8
318c2ecf20Sopenharmony_ci#define SLB_MIN_SIZE		32
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Bits in the SLB ESID word */
348c2ecf20Sopenharmony_ci#define SLB_ESID_V		ASM_CONST(0x0000000008000000) /* valid */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* Bits in the SLB VSID word */
378c2ecf20Sopenharmony_ci#define SLB_VSID_SHIFT		12
388c2ecf20Sopenharmony_ci#define SLB_VSID_SHIFT_256M	SLB_VSID_SHIFT
398c2ecf20Sopenharmony_ci#define SLB_VSID_SHIFT_1T	24
408c2ecf20Sopenharmony_ci#define SLB_VSID_SSIZE_SHIFT	62
418c2ecf20Sopenharmony_ci#define SLB_VSID_B		ASM_CONST(0xc000000000000000)
428c2ecf20Sopenharmony_ci#define SLB_VSID_B_256M		ASM_CONST(0x0000000000000000)
438c2ecf20Sopenharmony_ci#define SLB_VSID_B_1T		ASM_CONST(0x4000000000000000)
448c2ecf20Sopenharmony_ci#define SLB_VSID_KS		ASM_CONST(0x0000000000000800)
458c2ecf20Sopenharmony_ci#define SLB_VSID_KP		ASM_CONST(0x0000000000000400)
468c2ecf20Sopenharmony_ci#define SLB_VSID_N		ASM_CONST(0x0000000000000200) /* no-execute */
478c2ecf20Sopenharmony_ci#define SLB_VSID_L		ASM_CONST(0x0000000000000100)
488c2ecf20Sopenharmony_ci#define SLB_VSID_C		ASM_CONST(0x0000000000000080) /* class */
498c2ecf20Sopenharmony_ci#define SLB_VSID_LP		ASM_CONST(0x0000000000000030)
508c2ecf20Sopenharmony_ci#define SLB_VSID_LP_00		ASM_CONST(0x0000000000000000)
518c2ecf20Sopenharmony_ci#define SLB_VSID_LP_01		ASM_CONST(0x0000000000000010)
528c2ecf20Sopenharmony_ci#define SLB_VSID_LP_10		ASM_CONST(0x0000000000000020)
538c2ecf20Sopenharmony_ci#define SLB_VSID_LP_11		ASM_CONST(0x0000000000000030)
548c2ecf20Sopenharmony_ci#define SLB_VSID_LLP		(SLB_VSID_L|SLB_VSID_LP)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define SLB_VSID_KERNEL		(SLB_VSID_KP)
578c2ecf20Sopenharmony_ci#define SLB_VSID_USER		(SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci#define SLBIE_C			(0x08000000)
608c2ecf20Sopenharmony_ci#define SLBIE_SSIZE_SHIFT	25
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * Hash table
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci#define HPTES_PER_GROUP 8
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define HPTE_V_SSIZE_SHIFT	62
698c2ecf20Sopenharmony_ci#define HPTE_V_AVPN_SHIFT	7
708c2ecf20Sopenharmony_ci#define HPTE_V_COMMON_BITS	ASM_CONST(0x000fffffffffffff)
718c2ecf20Sopenharmony_ci#define HPTE_V_AVPN		ASM_CONST(0x3fffffffffffff80)
728c2ecf20Sopenharmony_ci#define HPTE_V_AVPN_3_0		ASM_CONST(0x000fffffffffff80)
738c2ecf20Sopenharmony_ci#define HPTE_V_AVPN_VAL(x)	(((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
748c2ecf20Sopenharmony_ci#define HPTE_V_COMPARE(x,y)	(!(((x) ^ (y)) & 0xffffffffffffff80UL))
758c2ecf20Sopenharmony_ci#define HPTE_V_BOLTED		ASM_CONST(0x0000000000000010)
768c2ecf20Sopenharmony_ci#define HPTE_V_LOCK		ASM_CONST(0x0000000000000008)
778c2ecf20Sopenharmony_ci#define HPTE_V_LARGE		ASM_CONST(0x0000000000000004)
788c2ecf20Sopenharmony_ci#define HPTE_V_SECONDARY	ASM_CONST(0x0000000000000002)
798c2ecf20Sopenharmony_ci#define HPTE_V_VALID		ASM_CONST(0x0000000000000001)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci/*
828c2ecf20Sopenharmony_ci * ISA 3.0 has a different HPTE format.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_ci#define HPTE_R_3_0_SSIZE_SHIFT	58
858c2ecf20Sopenharmony_ci#define HPTE_R_3_0_SSIZE_MASK	(3ull << HPTE_R_3_0_SSIZE_SHIFT)
868c2ecf20Sopenharmony_ci#define HPTE_R_PP0		ASM_CONST(0x8000000000000000)
878c2ecf20Sopenharmony_ci#define HPTE_R_TS		ASM_CONST(0x4000000000000000)
888c2ecf20Sopenharmony_ci#define HPTE_R_KEY_HI		ASM_CONST(0x3000000000000000)
898c2ecf20Sopenharmony_ci#define HPTE_R_KEY_BIT4		ASM_CONST(0x2000000000000000)
908c2ecf20Sopenharmony_ci#define HPTE_R_KEY_BIT3		ASM_CONST(0x1000000000000000)
918c2ecf20Sopenharmony_ci#define HPTE_R_RPN_SHIFT	12
928c2ecf20Sopenharmony_ci#define HPTE_R_RPN		ASM_CONST(0x0ffffffffffff000)
938c2ecf20Sopenharmony_ci#define HPTE_R_RPN_3_0		ASM_CONST(0x01fffffffffff000)
948c2ecf20Sopenharmony_ci#define HPTE_R_PP		ASM_CONST(0x0000000000000003)
958c2ecf20Sopenharmony_ci#define HPTE_R_PPP		ASM_CONST(0x8000000000000003)
968c2ecf20Sopenharmony_ci#define HPTE_R_N		ASM_CONST(0x0000000000000004)
978c2ecf20Sopenharmony_ci#define HPTE_R_G		ASM_CONST(0x0000000000000008)
988c2ecf20Sopenharmony_ci#define HPTE_R_M		ASM_CONST(0x0000000000000010)
998c2ecf20Sopenharmony_ci#define HPTE_R_I		ASM_CONST(0x0000000000000020)
1008c2ecf20Sopenharmony_ci#define HPTE_R_W		ASM_CONST(0x0000000000000040)
1018c2ecf20Sopenharmony_ci#define HPTE_R_WIMG		ASM_CONST(0x0000000000000078)
1028c2ecf20Sopenharmony_ci#define HPTE_R_C		ASM_CONST(0x0000000000000080)
1038c2ecf20Sopenharmony_ci#define HPTE_R_R		ASM_CONST(0x0000000000000100)
1048c2ecf20Sopenharmony_ci#define HPTE_R_KEY_LO		ASM_CONST(0x0000000000000e00)
1058c2ecf20Sopenharmony_ci#define HPTE_R_KEY_BIT2		ASM_CONST(0x0000000000000800)
1068c2ecf20Sopenharmony_ci#define HPTE_R_KEY_BIT1		ASM_CONST(0x0000000000000400)
1078c2ecf20Sopenharmony_ci#define HPTE_R_KEY_BIT0		ASM_CONST(0x0000000000000200)
1088c2ecf20Sopenharmony_ci#define HPTE_R_KEY		(HPTE_R_KEY_LO | HPTE_R_KEY_HI)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#define HPTE_V_1TB_SEG		ASM_CONST(0x4000000000000000)
1118c2ecf20Sopenharmony_ci#define HPTE_V_VRMA_MASK	ASM_CONST(0x4001ffffff000000)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/* Values for PP (assumes Ks=0, Kp=1) */
1148c2ecf20Sopenharmony_ci#define PP_RWXX	0	/* Supervisor read/write, User none */
1158c2ecf20Sopenharmony_ci#define PP_RWRX 1	/* Supervisor read/write, User read */
1168c2ecf20Sopenharmony_ci#define PP_RWRW 2	/* Supervisor read/write, User read/write */
1178c2ecf20Sopenharmony_ci#define PP_RXRX 3	/* Supervisor read,       User read */
1188c2ecf20Sopenharmony_ci#define PP_RXXX	(HPTE_R_PP0 | 2)	/* Supervisor read, user none */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* Fields for tlbiel instruction in architecture 2.06 */
1218c2ecf20Sopenharmony_ci#define TLBIEL_INVAL_SEL_MASK	0xc00	/* invalidation selector */
1228c2ecf20Sopenharmony_ci#define  TLBIEL_INVAL_PAGE	0x000	/* invalidate a single page */
1238c2ecf20Sopenharmony_ci#define  TLBIEL_INVAL_SET_LPID	0x800	/* invalidate a set for current LPID */
1248c2ecf20Sopenharmony_ci#define  TLBIEL_INVAL_SET	0xc00	/* invalidate a set for all LPIDs */
1258c2ecf20Sopenharmony_ci#define TLBIEL_INVAL_SET_MASK	0xfff000	/* set number to inval. */
1268c2ecf20Sopenharmony_ci#define TLBIEL_INVAL_SET_SHIFT	12
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci#define POWER7_TLB_SETS		128	/* # sets in POWER7 TLB */
1298c2ecf20Sopenharmony_ci#define POWER8_TLB_SETS		512	/* # sets in POWER8 TLB */
1308c2ecf20Sopenharmony_ci#define POWER9_TLB_SETS_HASH	256	/* # sets in POWER9 TLB Hash mode */
1318c2ecf20Sopenharmony_ci#define POWER9_TLB_SETS_RADIX	128	/* # sets in POWER9 TLB Radix mode */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct mmu_hash_ops {
1368c2ecf20Sopenharmony_ci	void            (*hpte_invalidate)(unsigned long slot,
1378c2ecf20Sopenharmony_ci					   unsigned long vpn,
1388c2ecf20Sopenharmony_ci					   int bpsize, int apsize,
1398c2ecf20Sopenharmony_ci					   int ssize, int local);
1408c2ecf20Sopenharmony_ci	long		(*hpte_updatepp)(unsigned long slot,
1418c2ecf20Sopenharmony_ci					 unsigned long newpp,
1428c2ecf20Sopenharmony_ci					 unsigned long vpn,
1438c2ecf20Sopenharmony_ci					 int bpsize, int apsize,
1448c2ecf20Sopenharmony_ci					 int ssize, unsigned long flags);
1458c2ecf20Sopenharmony_ci	void            (*hpte_updateboltedpp)(unsigned long newpp,
1468c2ecf20Sopenharmony_ci					       unsigned long ea,
1478c2ecf20Sopenharmony_ci					       int psize, int ssize);
1488c2ecf20Sopenharmony_ci	long		(*hpte_insert)(unsigned long hpte_group,
1498c2ecf20Sopenharmony_ci				       unsigned long vpn,
1508c2ecf20Sopenharmony_ci				       unsigned long prpn,
1518c2ecf20Sopenharmony_ci				       unsigned long rflags,
1528c2ecf20Sopenharmony_ci				       unsigned long vflags,
1538c2ecf20Sopenharmony_ci				       int psize, int apsize,
1548c2ecf20Sopenharmony_ci				       int ssize);
1558c2ecf20Sopenharmony_ci	long		(*hpte_remove)(unsigned long hpte_group);
1568c2ecf20Sopenharmony_ci	int             (*hpte_removebolted)(unsigned long ea,
1578c2ecf20Sopenharmony_ci					     int psize, int ssize);
1588c2ecf20Sopenharmony_ci	void		(*flush_hash_range)(unsigned long number, int local);
1598c2ecf20Sopenharmony_ci	void		(*hugepage_invalidate)(unsigned long vsid,
1608c2ecf20Sopenharmony_ci					       unsigned long addr,
1618c2ecf20Sopenharmony_ci					       unsigned char *hpte_slot_array,
1628c2ecf20Sopenharmony_ci					       int psize, int ssize, int local);
1638c2ecf20Sopenharmony_ci	int		(*resize_hpt)(unsigned long shift);
1648c2ecf20Sopenharmony_ci	/*
1658c2ecf20Sopenharmony_ci	 * Special for kexec.
1668c2ecf20Sopenharmony_ci	 * To be called in real mode with interrupts disabled. No locks are
1678c2ecf20Sopenharmony_ci	 * taken as such, concurrent access on pre POWER5 hardware could result
1688c2ecf20Sopenharmony_ci	 * in a deadlock.
1698c2ecf20Sopenharmony_ci	 * The linear mapping is destroyed as well.
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci	void		(*hpte_clear_all)(void);
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ciextern struct mmu_hash_ops mmu_hash_ops;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistruct hash_pte {
1768c2ecf20Sopenharmony_ci	__be64 v;
1778c2ecf20Sopenharmony_ci	__be64 r;
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciextern struct hash_pte *htab_address;
1818c2ecf20Sopenharmony_ciextern unsigned long htab_size_bytes;
1828c2ecf20Sopenharmony_ciextern unsigned long htab_hash_mask;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic inline int shift_to_mmu_psize(unsigned int shift)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	int psize;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
1908c2ecf20Sopenharmony_ci		if (mmu_psize_defs[psize].shift == shift)
1918c2ecf20Sopenharmony_ci			return psize;
1928c2ecf20Sopenharmony_ci	return -1;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	if (mmu_psize_defs[mmu_psize].shift)
1988c2ecf20Sopenharmony_ci		return mmu_psize_defs[mmu_psize].shift;
1998c2ecf20Sopenharmony_ci	BUG();
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistatic inline unsigned int ap_to_shift(unsigned long ap)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	int psize;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
2078c2ecf20Sopenharmony_ci		if (mmu_psize_defs[psize].ap == ap)
2088c2ecf20Sopenharmony_ci			return mmu_psize_defs[psize].shift;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return -1;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic inline unsigned long get_sllp_encoding(int psize)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	unsigned long sllp;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
2198c2ecf20Sopenharmony_ci		((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
2208c2ecf20Sopenharmony_ci	return sllp;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci/*
2268c2ecf20Sopenharmony_ci * Segment sizes.
2278c2ecf20Sopenharmony_ci * These are the values used by hardware in the B field of
2288c2ecf20Sopenharmony_ci * SLB entries and the first dword of MMU hashtable entries.
2298c2ecf20Sopenharmony_ci * The B field is 2 bits; the values 2 and 3 are unused and reserved.
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_ci#define MMU_SEGSIZE_256M	0
2328c2ecf20Sopenharmony_ci#define MMU_SEGSIZE_1T		1
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci/*
2358c2ecf20Sopenharmony_ci * encode page number shift.
2368c2ecf20Sopenharmony_ci * in order to fit the 78 bit va in a 64 bit variable we shift the va by
2378c2ecf20Sopenharmony_ci * 12 bits. This enable us to address upto 76 bit va.
2388c2ecf20Sopenharmony_ci * For hpt hash from a va we can ignore the page size bits of va and for
2398c2ecf20Sopenharmony_ci * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
2408c2ecf20Sopenharmony_ci * we work in all cases including 4k page size.
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_ci#define VPN_SHIFT	12
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/*
2458c2ecf20Sopenharmony_ci * HPTE Large Page (LP) details
2468c2ecf20Sopenharmony_ci */
2478c2ecf20Sopenharmony_ci#define LP_SHIFT	12
2488c2ecf20Sopenharmony_ci#define LP_BITS		8
2498c2ecf20Sopenharmony_ci#define LP_MASK(i)	((0xFF >> (i)) << LP_SHIFT)
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic inline int slb_vsid_shift(int ssize)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M)
2568c2ecf20Sopenharmony_ci		return SLB_VSID_SHIFT;
2578c2ecf20Sopenharmony_ci	return SLB_VSID_SHIFT_1T;
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_cistatic inline int segment_shift(int ssize)
2618c2ecf20Sopenharmony_ci{
2628c2ecf20Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M)
2638c2ecf20Sopenharmony_ci		return SID_SHIFT;
2648c2ecf20Sopenharmony_ci	return SID_SHIFT_1T;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * This array is indexed by the LP field of the HPTE second dword.
2698c2ecf20Sopenharmony_ci * Since this field may contain some RPN bits, some entries are
2708c2ecf20Sopenharmony_ci * replicated so that we get the same value irrespective of RPN.
2718c2ecf20Sopenharmony_ci * The top 4 bits are the page size index (MMU_PAGE_*) for the
2728c2ecf20Sopenharmony_ci * actual page size, the bottom 4 bits are the base page size.
2738c2ecf20Sopenharmony_ci */
2748c2ecf20Sopenharmony_ciextern u8 hpte_page_sizes[1 << LP_BITS];
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
2778c2ecf20Sopenharmony_ci					     bool is_base_size)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	unsigned int i, lp;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (!(h & HPTE_V_LARGE))
2828c2ecf20Sopenharmony_ci		return 1ul << 12;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Look at the 8 bit LP value */
2858c2ecf20Sopenharmony_ci	lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
2868c2ecf20Sopenharmony_ci	i = hpte_page_sizes[lp];
2878c2ecf20Sopenharmony_ci	if (!i)
2888c2ecf20Sopenharmony_ci		return 0;
2898c2ecf20Sopenharmony_ci	if (!is_base_size)
2908c2ecf20Sopenharmony_ci		i >>= 4;
2918c2ecf20Sopenharmony_ci	return 1ul << mmu_psize_defs[i & 0xf].shift;
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	return __hpte_page_size(h, l, 0);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	return __hpte_page_size(h, l, 1);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci/*
3058c2ecf20Sopenharmony_ci * The current system page and segment sizes
3068c2ecf20Sopenharmony_ci */
3078c2ecf20Sopenharmony_ciextern int mmu_kernel_ssize;
3088c2ecf20Sopenharmony_ciextern int mmu_highuser_ssize;
3098c2ecf20Sopenharmony_ciextern u16 mmu_slb_size;
3108c2ecf20Sopenharmony_ciextern unsigned long tce_alloc_start, tce_alloc_end;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/*
3138c2ecf20Sopenharmony_ci * If the processor supports 64k normal pages but not 64k cache
3148c2ecf20Sopenharmony_ci * inhibited pages, we have to be prepared to switch processes
3158c2ecf20Sopenharmony_ci * to use 4k pages when they create cache-inhibited mappings.
3168c2ecf20Sopenharmony_ci * If this is the case, mmu_ci_restrictions will be set to 1.
3178c2ecf20Sopenharmony_ci */
3188c2ecf20Sopenharmony_ciextern int mmu_ci_restrictions;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci/*
3218c2ecf20Sopenharmony_ci * This computes the AVPN and B fields of the first dword of a HPTE,
3228c2ecf20Sopenharmony_ci * for use when we want to match an existing PTE.  The bottom 7 bits
3238c2ecf20Sopenharmony_ci * of the returned value are zero.
3248c2ecf20Sopenharmony_ci */
3258c2ecf20Sopenharmony_cistatic inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
3268c2ecf20Sopenharmony_ci					     int ssize)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	unsigned long v;
3298c2ecf20Sopenharmony_ci	/*
3308c2ecf20Sopenharmony_ci	 * The AVA field omits the low-order 23 bits of the 78 bits VA.
3318c2ecf20Sopenharmony_ci	 * These bits are not needed in the PTE, because the
3328c2ecf20Sopenharmony_ci	 * low-order b of these bits are part of the byte offset
3338c2ecf20Sopenharmony_ci	 * into the virtual page and, if b < 23, the high-order
3348c2ecf20Sopenharmony_ci	 * 23-b of these bits are always used in selecting the
3358c2ecf20Sopenharmony_ci	 * PTEGs to be searched
3368c2ecf20Sopenharmony_ci	 */
3378c2ecf20Sopenharmony_ci	v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
3388c2ecf20Sopenharmony_ci	v <<= HPTE_V_AVPN_SHIFT;
3398c2ecf20Sopenharmony_ci	v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
3408c2ecf20Sopenharmony_ci	return v;
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/*
3448c2ecf20Sopenharmony_ci * ISA v3.0 defines a new HPTE format, which differs from the old
3458c2ecf20Sopenharmony_ci * format in having smaller AVPN and ARPN fields, and the B field
3468c2ecf20Sopenharmony_ci * in the second dword instead of the first.
3478c2ecf20Sopenharmony_ci */
3488c2ecf20Sopenharmony_cistatic inline unsigned long hpte_old_to_new_v(unsigned long v)
3498c2ecf20Sopenharmony_ci{
3508c2ecf20Sopenharmony_ci	/* trim AVPN, drop B */
3518c2ecf20Sopenharmony_ci	return v & HPTE_V_COMMON_BITS;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	/* move B field from 1st to 2nd dword, trim ARPN */
3578c2ecf20Sopenharmony_ci	return (r & ~HPTE_R_3_0_SSIZE_MASK) |
3588c2ecf20Sopenharmony_ci		(((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_cistatic inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
3628c2ecf20Sopenharmony_ci{
3638c2ecf20Sopenharmony_ci	/* insert B field */
3648c2ecf20Sopenharmony_ci	return (v & HPTE_V_COMMON_BITS) |
3658c2ecf20Sopenharmony_ci		((r & HPTE_R_3_0_SSIZE_MASK) <<
3668c2ecf20Sopenharmony_ci		 (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic inline unsigned long hpte_new_to_old_r(unsigned long r)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	/* clear out B field */
3728c2ecf20Sopenharmony_ci	return r & ~HPTE_R_3_0_SSIZE_MASK;
3738c2ecf20Sopenharmony_ci}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_cistatic inline unsigned long hpte_get_old_v(struct hash_pte *hptep)
3768c2ecf20Sopenharmony_ci{
3778c2ecf20Sopenharmony_ci	unsigned long hpte_v;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	hpte_v = be64_to_cpu(hptep->v);
3808c2ecf20Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_ARCH_300))
3818c2ecf20Sopenharmony_ci		hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
3828c2ecf20Sopenharmony_ci	return hpte_v;
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci/*
3868c2ecf20Sopenharmony_ci * This function sets the AVPN and L fields of the HPTE  appropriately
3878c2ecf20Sopenharmony_ci * using the base page size and actual page size.
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_cistatic inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
3908c2ecf20Sopenharmony_ci					  int actual_psize, int ssize)
3918c2ecf20Sopenharmony_ci{
3928c2ecf20Sopenharmony_ci	unsigned long v;
3938c2ecf20Sopenharmony_ci	v = hpte_encode_avpn(vpn, base_psize, ssize);
3948c2ecf20Sopenharmony_ci	if (actual_psize != MMU_PAGE_4K)
3958c2ecf20Sopenharmony_ci		v |= HPTE_V_LARGE;
3968c2ecf20Sopenharmony_ci	return v;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci/*
4008c2ecf20Sopenharmony_ci * This function sets the ARPN, and LP fields of the HPTE appropriately
4018c2ecf20Sopenharmony_ci * for the page size. We assume the pa is already "clean" that is properly
4028c2ecf20Sopenharmony_ci * aligned for the requested page size
4038c2ecf20Sopenharmony_ci */
4048c2ecf20Sopenharmony_cistatic inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
4058c2ecf20Sopenharmony_ci					  int actual_psize)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	/* A 4K page needs no special encoding */
4088c2ecf20Sopenharmony_ci	if (actual_psize == MMU_PAGE_4K)
4098c2ecf20Sopenharmony_ci		return pa & HPTE_R_RPN;
4108c2ecf20Sopenharmony_ci	else {
4118c2ecf20Sopenharmony_ci		unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
4128c2ecf20Sopenharmony_ci		unsigned int shift = mmu_psize_defs[actual_psize].shift;
4138c2ecf20Sopenharmony_ci		return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
4148c2ecf20Sopenharmony_ci	}
4158c2ecf20Sopenharmony_ci}
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci/*
4188c2ecf20Sopenharmony_ci * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
4198c2ecf20Sopenharmony_ci */
4208c2ecf20Sopenharmony_cistatic inline unsigned long hpt_vpn(unsigned long ea,
4218c2ecf20Sopenharmony_ci				    unsigned long vsid, int ssize)
4228c2ecf20Sopenharmony_ci{
4238c2ecf20Sopenharmony_ci	unsigned long mask;
4248c2ecf20Sopenharmony_ci	int s_shift = segment_shift(ssize);
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
4278c2ecf20Sopenharmony_ci	return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci/*
4318c2ecf20Sopenharmony_ci * This hashes a virtual address
4328c2ecf20Sopenharmony_ci */
4338c2ecf20Sopenharmony_cistatic inline unsigned long hpt_hash(unsigned long vpn,
4348c2ecf20Sopenharmony_ci				     unsigned int shift, int ssize)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	unsigned long mask;
4378c2ecf20Sopenharmony_ci	unsigned long hash, vsid;
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	/* VPN_SHIFT can be atmost 12 */
4408c2ecf20Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M) {
4418c2ecf20Sopenharmony_ci		mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
4428c2ecf20Sopenharmony_ci		hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
4438c2ecf20Sopenharmony_ci			((vpn & mask) >> (shift - VPN_SHIFT));
4448c2ecf20Sopenharmony_ci	} else {
4458c2ecf20Sopenharmony_ci		mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
4468c2ecf20Sopenharmony_ci		vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
4478c2ecf20Sopenharmony_ci		hash = vsid ^ (vsid << 25) ^
4488c2ecf20Sopenharmony_ci			((vpn & mask) >> (shift - VPN_SHIFT)) ;
4498c2ecf20Sopenharmony_ci	}
4508c2ecf20Sopenharmony_ci	return hash & 0x7fffffffffUL;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci#define HPTE_LOCAL_UPDATE	0x1
4548c2ecf20Sopenharmony_ci#define HPTE_NOHPTE_UPDATE	0x2
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ciextern int __hash_page_4K(unsigned long ea, unsigned long access,
4578c2ecf20Sopenharmony_ci			  unsigned long vsid, pte_t *ptep, unsigned long trap,
4588c2ecf20Sopenharmony_ci			  unsigned long flags, int ssize, int subpage_prot);
4598c2ecf20Sopenharmony_ciextern int __hash_page_64K(unsigned long ea, unsigned long access,
4608c2ecf20Sopenharmony_ci			   unsigned long vsid, pte_t *ptep, unsigned long trap,
4618c2ecf20Sopenharmony_ci			   unsigned long flags, int ssize);
4628c2ecf20Sopenharmony_cistruct mm_struct;
4638c2ecf20Sopenharmony_ciunsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
4648c2ecf20Sopenharmony_ciextern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
4658c2ecf20Sopenharmony_ci			unsigned long access, unsigned long trap,
4668c2ecf20Sopenharmony_ci			unsigned long flags);
4678c2ecf20Sopenharmony_ciextern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
4688c2ecf20Sopenharmony_ci		     unsigned long dsisr);
4698c2ecf20Sopenharmony_ciint __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
4708c2ecf20Sopenharmony_ci		     pte_t *ptep, unsigned long trap, unsigned long flags,
4718c2ecf20Sopenharmony_ci		     int ssize, unsigned int shift, unsigned int mmu_psize);
4728c2ecf20Sopenharmony_ci#ifdef CONFIG_TRANSPARENT_HUGEPAGE
4738c2ecf20Sopenharmony_ciextern int __hash_page_thp(unsigned long ea, unsigned long access,
4748c2ecf20Sopenharmony_ci			   unsigned long vsid, pmd_t *pmdp, unsigned long trap,
4758c2ecf20Sopenharmony_ci			   unsigned long flags, int ssize, unsigned int psize);
4768c2ecf20Sopenharmony_ci#else
4778c2ecf20Sopenharmony_cistatic inline int __hash_page_thp(unsigned long ea, unsigned long access,
4788c2ecf20Sopenharmony_ci				  unsigned long vsid, pmd_t *pmdp,
4798c2ecf20Sopenharmony_ci				  unsigned long trap, unsigned long flags,
4808c2ecf20Sopenharmony_ci				  int ssize, unsigned int psize)
4818c2ecf20Sopenharmony_ci{
4828c2ecf20Sopenharmony_ci	BUG();
4838c2ecf20Sopenharmony_ci	return -1;
4848c2ecf20Sopenharmony_ci}
4858c2ecf20Sopenharmony_ci#endif
4868c2ecf20Sopenharmony_ciextern void hash_failure_debug(unsigned long ea, unsigned long access,
4878c2ecf20Sopenharmony_ci			       unsigned long vsid, unsigned long trap,
4888c2ecf20Sopenharmony_ci			       int ssize, int psize, int lpsize,
4898c2ecf20Sopenharmony_ci			       unsigned long pte);
4908c2ecf20Sopenharmony_ciextern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
4918c2ecf20Sopenharmony_ci			     unsigned long pstart, unsigned long prot,
4928c2ecf20Sopenharmony_ci			     int psize, int ssize);
4938c2ecf20Sopenharmony_ciint htab_remove_mapping(unsigned long vstart, unsigned long vend,
4948c2ecf20Sopenharmony_ci			int psize, int ssize);
4958c2ecf20Sopenharmony_ciextern void pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
4968c2ecf20Sopenharmony_ciextern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ciextern void hash__setup_new_exec(void);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_PSERIES
5018c2ecf20Sopenharmony_civoid hpte_init_pseries(void);
5028c2ecf20Sopenharmony_ci#else
5038c2ecf20Sopenharmony_cistatic inline void hpte_init_pseries(void) { }
5048c2ecf20Sopenharmony_ci#endif
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ciextern void hpte_init_native(void);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistruct slb_entry {
5098c2ecf20Sopenharmony_ci	u64	esid;
5108c2ecf20Sopenharmony_ci	u64	vsid;
5118c2ecf20Sopenharmony_ci};
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ciextern void slb_initialize(void);
5148c2ecf20Sopenharmony_civoid slb_flush_and_restore_bolted(void);
5158c2ecf20Sopenharmony_civoid slb_flush_all_realmode(void);
5168c2ecf20Sopenharmony_civoid __slb_restore_bolted_realmode(void);
5178c2ecf20Sopenharmony_civoid slb_restore_bolted_realmode(void);
5188c2ecf20Sopenharmony_civoid slb_save_contents(struct slb_entry *slb_ptr);
5198c2ecf20Sopenharmony_civoid slb_dump_contents(struct slb_entry *slb_ptr);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ciextern void slb_vmalloc_update(void);
5228c2ecf20Sopenharmony_ciextern void slb_set_size(u16 size);
5238c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci/*
5268c2ecf20Sopenharmony_ci * VSID allocation (256MB segment)
5278c2ecf20Sopenharmony_ci *
5288c2ecf20Sopenharmony_ci * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
5298c2ecf20Sopenharmony_ci * from mmu context id and effective segment id of the address.
5308c2ecf20Sopenharmony_ci *
5318c2ecf20Sopenharmony_ci * For user processes max context id is limited to MAX_USER_CONTEXT.
5328c2ecf20Sopenharmony_ci * more details in get_user_context
5338c2ecf20Sopenharmony_ci *
5348c2ecf20Sopenharmony_ci * For kernel space get_kernel_context
5358c2ecf20Sopenharmony_ci *
5368c2ecf20Sopenharmony_ci * The proto-VSIDs are then scrambled into real VSIDs with the
5378c2ecf20Sopenharmony_ci * multiplicative hash:
5388c2ecf20Sopenharmony_ci *
5398c2ecf20Sopenharmony_ci *	VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
5408c2ecf20Sopenharmony_ci *
5418c2ecf20Sopenharmony_ci * VSID_MULTIPLIER is prime, so in particular it is
5428c2ecf20Sopenharmony_ci * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
5438c2ecf20Sopenharmony_ci * Because the modulus is 2^n-1 we can compute it efficiently without
5448c2ecf20Sopenharmony_ci * a divide or extra multiply (see below). The scramble function gives
5458c2ecf20Sopenharmony_ci * robust scattering in the hash table (at least based on some initial
5468c2ecf20Sopenharmony_ci * results).
5478c2ecf20Sopenharmony_ci *
5488c2ecf20Sopenharmony_ci * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
5498c2ecf20Sopenharmony_ci * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
5508c2ecf20Sopenharmony_ci * will produce a VSID of 0.
5518c2ecf20Sopenharmony_ci *
5528c2ecf20Sopenharmony_ci * We also need to avoid the last segment of the last context, because that
5538c2ecf20Sopenharmony_ci * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
5548c2ecf20Sopenharmony_ci * because of the modulo operation in vsid scramble.
5558c2ecf20Sopenharmony_ci */
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/*
5588c2ecf20Sopenharmony_ci * Max Va bits we support as of now is 68 bits. We want 19 bit
5598c2ecf20Sopenharmony_ci * context ID.
5608c2ecf20Sopenharmony_ci * Restrictions:
5618c2ecf20Sopenharmony_ci * GPU has restrictions of not able to access beyond 128TB
5628c2ecf20Sopenharmony_ci * (47 bit effective address). We also cannot do more than 20bit PID.
5638c2ecf20Sopenharmony_ci * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
5648c2ecf20Sopenharmony_ci * to 16 bits (ie, we can only have 2^16 pids at the same time).
5658c2ecf20Sopenharmony_ci */
5668c2ecf20Sopenharmony_ci#define VA_BITS			68
5678c2ecf20Sopenharmony_ci#define CONTEXT_BITS		19
5688c2ecf20Sopenharmony_ci#define ESID_BITS		(VA_BITS - (SID_SHIFT + CONTEXT_BITS))
5698c2ecf20Sopenharmony_ci#define ESID_BITS_1T		(VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci#define ESID_BITS_MASK		((1 << ESID_BITS) - 1)
5728c2ecf20Sopenharmony_ci#define ESID_BITS_1T_MASK	((1 << ESID_BITS_1T) - 1)
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci/*
5758c2ecf20Sopenharmony_ci * Now certain config support MAX_PHYSMEM more than 512TB. Hence we will need
5768c2ecf20Sopenharmony_ci * to use more than one context for linear mapping the kernel.
5778c2ecf20Sopenharmony_ci * For vmalloc and memmap, we use just one context with 512TB. With 64 byte
5788c2ecf20Sopenharmony_ci * struct page size, we need ony 32 TB in memmap for 2PB (51 bits (MAX_PHYSMEM_BITS)).
5798c2ecf20Sopenharmony_ci */
5808c2ecf20Sopenharmony_ci#if (H_MAX_PHYSMEM_BITS > MAX_EA_BITS_PER_CONTEXT)
5818c2ecf20Sopenharmony_ci#define MAX_KERNEL_CTX_CNT	(1UL << (H_MAX_PHYSMEM_BITS - MAX_EA_BITS_PER_CONTEXT))
5828c2ecf20Sopenharmony_ci#else
5838c2ecf20Sopenharmony_ci#define MAX_KERNEL_CTX_CNT	1
5848c2ecf20Sopenharmony_ci#endif
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci#define MAX_VMALLOC_CTX_CNT	1
5878c2ecf20Sopenharmony_ci#define MAX_IO_CTX_CNT		1
5888c2ecf20Sopenharmony_ci#define MAX_VMEMMAP_CTX_CNT	1
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_ci/*
5918c2ecf20Sopenharmony_ci * 256MB segment
5928c2ecf20Sopenharmony_ci * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
5938c2ecf20Sopenharmony_ci * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
5948c2ecf20Sopenharmony_ci * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
5958c2ecf20Sopenharmony_ci * context maps 2^49 bytes (512TB).
5968c2ecf20Sopenharmony_ci *
5978c2ecf20Sopenharmony_ci * We also need to avoid the last segment of the last context, because that
5988c2ecf20Sopenharmony_ci * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
5998c2ecf20Sopenharmony_ci * because of the modulo operation in vsid scramble.
6008c2ecf20Sopenharmony_ci *
6018c2ecf20Sopenharmony_ci */
6028c2ecf20Sopenharmony_ci#define MAX_USER_CONTEXT	((ASM_CONST(1) << CONTEXT_BITS) - 2)
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci// The + 2 accounts for INVALID_REGION and 1 more to avoid overlap with kernel
6058c2ecf20Sopenharmony_ci#define MIN_USER_CONTEXT	(MAX_KERNEL_CTX_CNT + MAX_VMALLOC_CTX_CNT + \
6068c2ecf20Sopenharmony_ci				 MAX_IO_CTX_CNT + MAX_VMEMMAP_CTX_CNT + 2)
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci/*
6098c2ecf20Sopenharmony_ci * For platforms that support on 65bit VA we limit the context bits
6108c2ecf20Sopenharmony_ci */
6118c2ecf20Sopenharmony_ci#define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci/*
6148c2ecf20Sopenharmony_ci * This should be computed such that protovosid * vsid_mulitplier
6158c2ecf20Sopenharmony_ci * doesn't overflow 64 bits. The vsid_mutliplier should also be
6168c2ecf20Sopenharmony_ci * co-prime to vsid_modulus. We also need to make sure that number
6178c2ecf20Sopenharmony_ci * of bits in multiplied result (dividend) is less than twice the number of
6188c2ecf20Sopenharmony_ci * protovsid bits for our modulus optmization to work.
6198c2ecf20Sopenharmony_ci *
6208c2ecf20Sopenharmony_ci * The below table shows the current values used.
6218c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
6228c2ecf20Sopenharmony_ci * |       | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
6238c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
6248c2ecf20Sopenharmony_ci * | 1T    |         24 |                   25 |         49 |                50 |
6258c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
6268c2ecf20Sopenharmony_ci * | 256MB |         24 |                   37 |         61 |                74 |
6278c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+-------------------|
6288c2ecf20Sopenharmony_ci *
6298c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
6308c2ecf20Sopenharmony_ci * |       | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
6318c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
6328c2ecf20Sopenharmony_ci * | 1T    |         24 |                   28 |         52 |                 56 |
6338c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
6348c2ecf20Sopenharmony_ci * | 256MB |         24 |                   40 |         64 |                 80 |
6358c2ecf20Sopenharmony_ci * |-------+------------+----------------------+------------+--------------------|
6368c2ecf20Sopenharmony_ci *
6378c2ecf20Sopenharmony_ci */
6388c2ecf20Sopenharmony_ci#define VSID_MULTIPLIER_256M	ASM_CONST(12538073)	/* 24-bit prime */
6398c2ecf20Sopenharmony_ci#define VSID_BITS_256M		(VA_BITS - SID_SHIFT)
6408c2ecf20Sopenharmony_ci#define VSID_BITS_65_256M	(65 - SID_SHIFT)
6418c2ecf20Sopenharmony_ci/*
6428c2ecf20Sopenharmony_ci * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
6438c2ecf20Sopenharmony_ci */
6448c2ecf20Sopenharmony_ci#define VSID_MULINV_256M	ASM_CONST(665548017062)
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci#define VSID_MULTIPLIER_1T	ASM_CONST(12538073)	/* 24-bit prime */
6478c2ecf20Sopenharmony_ci#define VSID_BITS_1T		(VA_BITS - SID_SHIFT_1T)
6488c2ecf20Sopenharmony_ci#define VSID_BITS_65_1T		(65 - SID_SHIFT_1T)
6498c2ecf20Sopenharmony_ci#define VSID_MULINV_1T		ASM_CONST(209034062)
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/* 1TB VSID reserved for VRMA */
6528c2ecf20Sopenharmony_ci#define VRMA_VSID	0x1ffffffUL
6538c2ecf20Sopenharmony_ci#define USER_VSID_RANGE	(1UL << (ESID_BITS + SID_SHIFT))
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci/* 4 bits per slice and we have one slice per 1TB */
6568c2ecf20Sopenharmony_ci#define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
6578c2ecf20Sopenharmony_ci#define LOW_SLICE_ARRAY_SZ	(BITS_PER_LONG / BITS_PER_BYTE)
6588c2ecf20Sopenharmony_ci#define TASK_SLICE_ARRAY_SZ(x)	((x)->hash_context->slb_addr_limit >> 41)
6598c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_SUBPAGE_PROT
6628c2ecf20Sopenharmony_ci/*
6638c2ecf20Sopenharmony_ci * For the sub-page protection option, we extend the PGD with one of
6648c2ecf20Sopenharmony_ci * these.  Basically we have a 3-level tree, with the top level being
6658c2ecf20Sopenharmony_ci * the protptrs array.  To optimize speed and memory consumption when
6668c2ecf20Sopenharmony_ci * only addresses < 4GB are being protected, pointers to the first
6678c2ecf20Sopenharmony_ci * four pages of sub-page protection words are stored in the low_prot
6688c2ecf20Sopenharmony_ci * array.
6698c2ecf20Sopenharmony_ci * Each page of sub-page protection words protects 1GB (4 bytes
6708c2ecf20Sopenharmony_ci * protects 64k).  For the 3-level tree, each page of pointers then
6718c2ecf20Sopenharmony_ci * protects 8TB.
6728c2ecf20Sopenharmony_ci */
6738c2ecf20Sopenharmony_cistruct subpage_prot_table {
6748c2ecf20Sopenharmony_ci	unsigned long maxaddr;	/* only addresses < this are protected */
6758c2ecf20Sopenharmony_ci	unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
6768c2ecf20Sopenharmony_ci	unsigned int *low_prot[4];
6778c2ecf20Sopenharmony_ci};
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci#define SBP_L1_BITS		(PAGE_SHIFT - 2)
6808c2ecf20Sopenharmony_ci#define SBP_L2_BITS		(PAGE_SHIFT - 3)
6818c2ecf20Sopenharmony_ci#define SBP_L1_COUNT		(1 << SBP_L1_BITS)
6828c2ecf20Sopenharmony_ci#define SBP_L2_COUNT		(1 << SBP_L2_BITS)
6838c2ecf20Sopenharmony_ci#define SBP_L2_SHIFT		(PAGE_SHIFT + SBP_L1_BITS)
6848c2ecf20Sopenharmony_ci#define SBP_L3_SHIFT		(SBP_L2_SHIFT + SBP_L2_BITS)
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ciextern void subpage_prot_free(struct mm_struct *mm);
6878c2ecf20Sopenharmony_ci#else
6888c2ecf20Sopenharmony_cistatic inline void subpage_prot_free(struct mm_struct *mm) {}
6898c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_SUBPAGE_PROT */
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci/*
6928c2ecf20Sopenharmony_ci * One bit per slice. We have lower slices which cover 256MB segments
6938c2ecf20Sopenharmony_ci * upto 4G range. That gets us 16 low slices. For the rest we track slices
6948c2ecf20Sopenharmony_ci * in 1TB size.
6958c2ecf20Sopenharmony_ci */
6968c2ecf20Sopenharmony_cistruct slice_mask {
6978c2ecf20Sopenharmony_ci	u64 low_slices;
6988c2ecf20Sopenharmony_ci	DECLARE_BITMAP(high_slices, SLICE_NUM_HIGH);
6998c2ecf20Sopenharmony_ci};
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_cistruct hash_mm_context {
7028c2ecf20Sopenharmony_ci	u16 user_psize; /* page size index */
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci	/* SLB page size encodings*/
7058c2ecf20Sopenharmony_ci	unsigned char low_slices_psize[LOW_SLICE_ARRAY_SZ];
7068c2ecf20Sopenharmony_ci	unsigned char high_slices_psize[SLICE_ARRAY_SIZE];
7078c2ecf20Sopenharmony_ci	unsigned long slb_addr_limit;
7088c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_64K_PAGES
7098c2ecf20Sopenharmony_ci	struct slice_mask mask_64k;
7108c2ecf20Sopenharmony_ci#endif
7118c2ecf20Sopenharmony_ci	struct slice_mask mask_4k;
7128c2ecf20Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
7138c2ecf20Sopenharmony_ci	struct slice_mask mask_16m;
7148c2ecf20Sopenharmony_ci	struct slice_mask mask_16g;
7158c2ecf20Sopenharmony_ci#endif
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_SUBPAGE_PROT
7188c2ecf20Sopenharmony_ci	struct subpage_prot_table *spt;
7198c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_SUBPAGE_PROT */
7208c2ecf20Sopenharmony_ci};
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci#if 0
7238c2ecf20Sopenharmony_ci/*
7248c2ecf20Sopenharmony_ci * The code below is equivalent to this function for arguments
7258c2ecf20Sopenharmony_ci * < 2^VSID_BITS, which is all this should ever be called
7268c2ecf20Sopenharmony_ci * with.  However gcc is not clever enough to compute the
7278c2ecf20Sopenharmony_ci * modulus (2^n-1) without a second multiply.
7288c2ecf20Sopenharmony_ci */
7298c2ecf20Sopenharmony_ci#define vsid_scramble(protovsid, size) \
7308c2ecf20Sopenharmony_ci	((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_ci/* simplified form avoiding mod operation */
7338c2ecf20Sopenharmony_ci#define vsid_scramble(protovsid, size) \
7348c2ecf20Sopenharmony_ci	({								 \
7358c2ecf20Sopenharmony_ci		unsigned long x;					 \
7368c2ecf20Sopenharmony_ci		x = (protovsid) * VSID_MULTIPLIER_##size;		 \
7378c2ecf20Sopenharmony_ci		x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
7388c2ecf20Sopenharmony_ci		(x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
7398c2ecf20Sopenharmony_ci	})
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci#else /* 1 */
7428c2ecf20Sopenharmony_cistatic inline unsigned long vsid_scramble(unsigned long protovsid,
7438c2ecf20Sopenharmony_ci				  unsigned long vsid_multiplier, int vsid_bits)
7448c2ecf20Sopenharmony_ci{
7458c2ecf20Sopenharmony_ci	unsigned long vsid;
7468c2ecf20Sopenharmony_ci	unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
7478c2ecf20Sopenharmony_ci	/*
7488c2ecf20Sopenharmony_ci	 * We have same multipler for both 256 and 1T segements now
7498c2ecf20Sopenharmony_ci	 */
7508c2ecf20Sopenharmony_ci	vsid = protovsid * vsid_multiplier;
7518c2ecf20Sopenharmony_ci	vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
7528c2ecf20Sopenharmony_ci	return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci#endif /* 1 */
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci/* Returns the segment size indicator for a user address */
7588c2ecf20Sopenharmony_cistatic inline int user_segment_size(unsigned long addr)
7598c2ecf20Sopenharmony_ci{
7608c2ecf20Sopenharmony_ci	/* Use 1T segments if possible for addresses >= 1T */
7618c2ecf20Sopenharmony_ci	if (addr >= (1UL << SID_SHIFT_1T))
7628c2ecf20Sopenharmony_ci		return mmu_highuser_ssize;
7638c2ecf20Sopenharmony_ci	return MMU_SEGSIZE_256M;
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic inline unsigned long get_vsid(unsigned long context, unsigned long ea,
7678c2ecf20Sopenharmony_ci				     int ssize)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	unsigned long va_bits = VA_BITS;
7708c2ecf20Sopenharmony_ci	unsigned long vsid_bits;
7718c2ecf20Sopenharmony_ci	unsigned long protovsid;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	/*
7748c2ecf20Sopenharmony_ci	 * Bad address. We return VSID 0 for that
7758c2ecf20Sopenharmony_ci	 */
7768c2ecf20Sopenharmony_ci	if ((ea & EA_MASK)  >= H_PGTABLE_RANGE)
7778c2ecf20Sopenharmony_ci		return 0;
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
7808c2ecf20Sopenharmony_ci		va_bits = 65;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	if (ssize == MMU_SEGSIZE_256M) {
7838c2ecf20Sopenharmony_ci		vsid_bits = va_bits - SID_SHIFT;
7848c2ecf20Sopenharmony_ci		protovsid = (context << ESID_BITS) |
7858c2ecf20Sopenharmony_ci			((ea >> SID_SHIFT) & ESID_BITS_MASK);
7868c2ecf20Sopenharmony_ci		return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci	/* 1T segment */
7898c2ecf20Sopenharmony_ci	vsid_bits = va_bits - SID_SHIFT_1T;
7908c2ecf20Sopenharmony_ci	protovsid = (context << ESID_BITS_1T) |
7918c2ecf20Sopenharmony_ci		((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
7928c2ecf20Sopenharmony_ci	return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
7938c2ecf20Sopenharmony_ci}
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci/*
7968c2ecf20Sopenharmony_ci * For kernel space, we use context ids as
7978c2ecf20Sopenharmony_ci * below. Range is 512TB per context.
7988c2ecf20Sopenharmony_ci *
7998c2ecf20Sopenharmony_ci * 0x00001 -  [ 0xc000000000000000 - 0xc001ffffffffffff]
8008c2ecf20Sopenharmony_ci * 0x00002 -  [ 0xc002000000000000 - 0xc003ffffffffffff]
8018c2ecf20Sopenharmony_ci * 0x00003 -  [ 0xc004000000000000 - 0xc005ffffffffffff]
8028c2ecf20Sopenharmony_ci * 0x00004 -  [ 0xc006000000000000 - 0xc007ffffffffffff]
8038c2ecf20Sopenharmony_ci *
8048c2ecf20Sopenharmony_ci * vmap, IO, vmemap
8058c2ecf20Sopenharmony_ci *
8068c2ecf20Sopenharmony_ci * 0x00005 -  [ 0xc008000000000000 - 0xc009ffffffffffff]
8078c2ecf20Sopenharmony_ci * 0x00006 -  [ 0xc00a000000000000 - 0xc00bffffffffffff]
8088c2ecf20Sopenharmony_ci * 0x00007 -  [ 0xc00c000000000000 - 0xc00dffffffffffff]
8098c2ecf20Sopenharmony_ci *
8108c2ecf20Sopenharmony_ci */
8118c2ecf20Sopenharmony_cistatic inline unsigned long get_kernel_context(unsigned long ea)
8128c2ecf20Sopenharmony_ci{
8138c2ecf20Sopenharmony_ci	unsigned long region_id = get_region_id(ea);
8148c2ecf20Sopenharmony_ci	unsigned long ctx;
8158c2ecf20Sopenharmony_ci	/*
8168c2ecf20Sopenharmony_ci	 * Depending on Kernel config, kernel region can have one context
8178c2ecf20Sopenharmony_ci	 * or more.
8188c2ecf20Sopenharmony_ci	 */
8198c2ecf20Sopenharmony_ci	if (region_id == LINEAR_MAP_REGION_ID) {
8208c2ecf20Sopenharmony_ci		/*
8218c2ecf20Sopenharmony_ci		 * We already verified ea to be not beyond the addr limit.
8228c2ecf20Sopenharmony_ci		 */
8238c2ecf20Sopenharmony_ci		ctx =  1 + ((ea & EA_MASK) >> MAX_EA_BITS_PER_CONTEXT);
8248c2ecf20Sopenharmony_ci	} else
8258c2ecf20Sopenharmony_ci		ctx = region_id + MAX_KERNEL_CTX_CNT - 1;
8268c2ecf20Sopenharmony_ci	return ctx;
8278c2ecf20Sopenharmony_ci}
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci/*
8308c2ecf20Sopenharmony_ci * This is only valid for addresses >= PAGE_OFFSET
8318c2ecf20Sopenharmony_ci */
8328c2ecf20Sopenharmony_cistatic inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
8338c2ecf20Sopenharmony_ci{
8348c2ecf20Sopenharmony_ci	unsigned long context;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	if (!is_kernel_addr(ea))
8378c2ecf20Sopenharmony_ci		return 0;
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	context = get_kernel_context(ea);
8408c2ecf20Sopenharmony_ci	return get_vsid(context, ea, ssize);
8418c2ecf20Sopenharmony_ci}
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ciunsigned htab_shift_for_mem_size(unsigned long mem_size);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */
848