xref: /openbmc/linux/arch/powerpc/include/asm/book3s/64/mmu-hash.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
211a6f6abSAneesh Kumar K.V #ifndef _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
311a6f6abSAneesh Kumar K.V #define _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
4f64e8084SAneesh Kumar K.V /*
5f64e8084SAneesh Kumar K.V  * PowerPC64 memory management structures
6f64e8084SAneesh Kumar K.V  *
7f64e8084SAneesh Kumar K.V  * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
8f64e8084SAneesh Kumar K.V  *   PPC64 rework.
9f64e8084SAneesh Kumar K.V  */
10f64e8084SAneesh Kumar K.V 
11f64e8084SAneesh Kumar K.V #include <asm/page.h>
12f64e8084SAneesh Kumar K.V #include <asm/bug.h>
13ec0c464cSChristophe Leroy #include <asm/asm-const.h>
14f64e8084SAneesh Kumar K.V 
15f64e8084SAneesh Kumar K.V /*
16f64e8084SAneesh Kumar K.V  * This is necessary to get the definition of PGTABLE_RANGE which we
17f64e8084SAneesh Kumar K.V  * need for various slices related matters. Note that this isn't the
18f64e8084SAneesh Kumar K.V  * complete pgtable.h but only a portion of it.
19f64e8084SAneesh Kumar K.V  */
20f64e8084SAneesh Kumar K.V #include <asm/book3s/64/pgtable.h>
21*1408fca0SChristophe Leroy #include <asm/book3s/64/slice.h>
2292ab45c5SChristophe Leroy #include <asm/task_size_64.h>
23b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
24f64e8084SAneesh Kumar K.V 
25f64e8084SAneesh Kumar K.V /*
26f64e8084SAneesh Kumar K.V  * SLB
27f64e8084SAneesh Kumar K.V  */
28f64e8084SAneesh Kumar K.V 
2985376e2aSNicholas Piggin #define SLB_NUM_BOLTED		2
30f64e8084SAneesh Kumar K.V #define SLB_CACHE_ENTRIES	8
31f64e8084SAneesh Kumar K.V #define SLB_MIN_SIZE		32
32f64e8084SAneesh Kumar K.V 
33f64e8084SAneesh Kumar K.V /* Bits in the SLB ESID word */
34f64e8084SAneesh Kumar K.V #define SLB_ESID_V		ASM_CONST(0x0000000008000000) /* valid */
35f64e8084SAneesh Kumar K.V 
36f64e8084SAneesh Kumar K.V /* Bits in the SLB VSID word */
37f64e8084SAneesh Kumar K.V #define SLB_VSID_SHIFT		12
38e6f81a92SAneesh Kumar K.V #define SLB_VSID_SHIFT_256M	SLB_VSID_SHIFT
39f64e8084SAneesh Kumar K.V #define SLB_VSID_SHIFT_1T	24
40f64e8084SAneesh Kumar K.V #define SLB_VSID_SSIZE_SHIFT	62
41f64e8084SAneesh Kumar K.V #define SLB_VSID_B		ASM_CONST(0xc000000000000000)
42f64e8084SAneesh Kumar K.V #define SLB_VSID_B_256M		ASM_CONST(0x0000000000000000)
43f64e8084SAneesh Kumar K.V #define SLB_VSID_B_1T		ASM_CONST(0x4000000000000000)
44f64e8084SAneesh Kumar K.V #define SLB_VSID_KS		ASM_CONST(0x0000000000000800)
45f64e8084SAneesh Kumar K.V #define SLB_VSID_KP		ASM_CONST(0x0000000000000400)
46f64e8084SAneesh Kumar K.V #define SLB_VSID_N		ASM_CONST(0x0000000000000200) /* no-execute */
47f64e8084SAneesh Kumar K.V #define SLB_VSID_L		ASM_CONST(0x0000000000000100)
48f64e8084SAneesh Kumar K.V #define SLB_VSID_C		ASM_CONST(0x0000000000000080) /* class */
49f64e8084SAneesh Kumar K.V #define SLB_VSID_LP		ASM_CONST(0x0000000000000030)
50f64e8084SAneesh Kumar K.V #define SLB_VSID_LP_00		ASM_CONST(0x0000000000000000)
51f64e8084SAneesh Kumar K.V #define SLB_VSID_LP_01		ASM_CONST(0x0000000000000010)
52f64e8084SAneesh Kumar K.V #define SLB_VSID_LP_10		ASM_CONST(0x0000000000000020)
53f64e8084SAneesh Kumar K.V #define SLB_VSID_LP_11		ASM_CONST(0x0000000000000030)
54f64e8084SAneesh Kumar K.V #define SLB_VSID_LLP		(SLB_VSID_L|SLB_VSID_LP)
55f64e8084SAneesh Kumar K.V 
56f64e8084SAneesh Kumar K.V #define SLB_VSID_KERNEL		(SLB_VSID_KP)
57f64e8084SAneesh Kumar K.V #define SLB_VSID_USER		(SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
58f64e8084SAneesh Kumar K.V 
59f64e8084SAneesh Kumar K.V #define SLBIE_C			(0x08000000)
60f64e8084SAneesh Kumar K.V #define SLBIE_SSIZE_SHIFT	25
61f64e8084SAneesh Kumar K.V 
62f64e8084SAneesh Kumar K.V /*
63f64e8084SAneesh Kumar K.V  * Hash table
64f64e8084SAneesh Kumar K.V  */
65f64e8084SAneesh Kumar K.V 
66f64e8084SAneesh Kumar K.V #define HPTES_PER_GROUP 8
67f64e8084SAneesh Kumar K.V 
68f64e8084SAneesh Kumar K.V #define HPTE_V_SSIZE_SHIFT	62
69f64e8084SAneesh Kumar K.V #define HPTE_V_AVPN_SHIFT	7
706b243fcfSPaul Mackerras #define HPTE_V_COMMON_BITS	ASM_CONST(0x000fffffffffffff)
71f64e8084SAneesh Kumar K.V #define HPTE_V_AVPN		ASM_CONST(0x3fffffffffffff80)
726b243fcfSPaul Mackerras #define HPTE_V_AVPN_3_0		ASM_CONST(0x000fffffffffff80)
73f64e8084SAneesh Kumar K.V #define HPTE_V_AVPN_VAL(x)	(((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
74f64e8084SAneesh Kumar K.V #define HPTE_V_COMPARE(x,y)	(!(((x) ^ (y)) & 0xffffffffffffff80UL))
75f64e8084SAneesh Kumar K.V #define HPTE_V_BOLTED		ASM_CONST(0x0000000000000010)
76f64e8084SAneesh Kumar K.V #define HPTE_V_LOCK		ASM_CONST(0x0000000000000008)
77f64e8084SAneesh Kumar K.V #define HPTE_V_LARGE		ASM_CONST(0x0000000000000004)
78f64e8084SAneesh Kumar K.V #define HPTE_V_SECONDARY	ASM_CONST(0x0000000000000002)
79f64e8084SAneesh Kumar K.V #define HPTE_V_VALID		ASM_CONST(0x0000000000000001)
80f64e8084SAneesh Kumar K.V 
8150de596dSAneesh Kumar K.V /*
826b243fcfSPaul Mackerras  * ISA 3.0 has a different HPTE format.
8350de596dSAneesh Kumar K.V  */
8450de596dSAneesh Kumar K.V #define HPTE_R_3_0_SSIZE_SHIFT	58
856b243fcfSPaul Mackerras #define HPTE_R_3_0_SSIZE_MASK	(3ull << HPTE_R_3_0_SSIZE_SHIFT)
86f64e8084SAneesh Kumar K.V #define HPTE_R_PP0		ASM_CONST(0x8000000000000000)
87f64e8084SAneesh Kumar K.V #define HPTE_R_TS		ASM_CONST(0x4000000000000000)
88f64e8084SAneesh Kumar K.V #define HPTE_R_KEY_HI		ASM_CONST(0x3000000000000000)
8933699023SAneesh Kumar K.V #define HPTE_R_KEY_BIT4		ASM_CONST(0x2000000000000000)
9033699023SAneesh Kumar K.V #define HPTE_R_KEY_BIT3		ASM_CONST(0x1000000000000000)
91f64e8084SAneesh Kumar K.V #define HPTE_R_RPN_SHIFT	12
92f64e8084SAneesh Kumar K.V #define HPTE_R_RPN		ASM_CONST(0x0ffffffffffff000)
936b243fcfSPaul Mackerras #define HPTE_R_RPN_3_0		ASM_CONST(0x01fffffffffff000)
94f64e8084SAneesh Kumar K.V #define HPTE_R_PP		ASM_CONST(0x0000000000000003)
958550e2faSAneesh Kumar K.V #define HPTE_R_PPP		ASM_CONST(0x8000000000000003)
96f64e8084SAneesh Kumar K.V #define HPTE_R_N		ASM_CONST(0x0000000000000004)
97f64e8084SAneesh Kumar K.V #define HPTE_R_G		ASM_CONST(0x0000000000000008)
98f64e8084SAneesh Kumar K.V #define HPTE_R_M		ASM_CONST(0x0000000000000010)
99f64e8084SAneesh Kumar K.V #define HPTE_R_I		ASM_CONST(0x0000000000000020)
100f64e8084SAneesh Kumar K.V #define HPTE_R_W		ASM_CONST(0x0000000000000040)
101f64e8084SAneesh Kumar K.V #define HPTE_R_WIMG		ASM_CONST(0x0000000000000078)
102f64e8084SAneesh Kumar K.V #define HPTE_R_C		ASM_CONST(0x0000000000000080)
103f64e8084SAneesh Kumar K.V #define HPTE_R_R		ASM_CONST(0x0000000000000100)
104f64e8084SAneesh Kumar K.V #define HPTE_R_KEY_LO		ASM_CONST(0x0000000000000e00)
105a6590ca5SRam Pai #define HPTE_R_KEY_BIT2		ASM_CONST(0x0000000000000800)
10633699023SAneesh Kumar K.V #define HPTE_R_KEY_BIT1		ASM_CONST(0x0000000000000400)
10733699023SAneesh Kumar K.V #define HPTE_R_KEY_BIT0		ASM_CONST(0x0000000000000200)
108d182b8fdSRam Pai #define HPTE_R_KEY		(HPTE_R_KEY_LO | HPTE_R_KEY_HI)
109f64e8084SAneesh Kumar K.V 
110f64e8084SAneesh Kumar K.V #define HPTE_V_1TB_SEG		ASM_CONST(0x4000000000000000)
111f64e8084SAneesh Kumar K.V #define HPTE_V_VRMA_MASK	ASM_CONST(0x4001ffffff000000)
112f64e8084SAneesh Kumar K.V 
113f64e8084SAneesh Kumar K.V /* Values for PP (assumes Ks=0, Kp=1) */
114f64e8084SAneesh Kumar K.V #define PP_RWXX	0	/* Supervisor read/write, User none */
115f64e8084SAneesh Kumar K.V #define PP_RWRX 1	/* Supervisor read/write, User read */
116f64e8084SAneesh Kumar K.V #define PP_RWRW 2	/* Supervisor read/write, User read/write */
117f64e8084SAneesh Kumar K.V #define PP_RXRX 3	/* Supervisor read,       User read */
118f64e8084SAneesh Kumar K.V #define PP_RXXX	(HPTE_R_PP0 | 2)	/* Supervisor read, user none */
119f64e8084SAneesh Kumar K.V 
120f64e8084SAneesh Kumar K.V /* Fields for tlbiel instruction in architecture 2.06 */
121f64e8084SAneesh Kumar K.V #define TLBIEL_INVAL_SEL_MASK	0xc00	/* invalidation selector */
122f64e8084SAneesh Kumar K.V #define  TLBIEL_INVAL_PAGE	0x000	/* invalidate a single page */
123f64e8084SAneesh Kumar K.V #define  TLBIEL_INVAL_SET_LPID	0x800	/* invalidate a set for current LPID */
124f64e8084SAneesh Kumar K.V #define  TLBIEL_INVAL_SET	0xc00	/* invalidate a set for all LPIDs */
125f64e8084SAneesh Kumar K.V #define TLBIEL_INVAL_SET_MASK	0xfff000	/* set number to inval. */
126f64e8084SAneesh Kumar K.V #define TLBIEL_INVAL_SET_SHIFT	12
127f64e8084SAneesh Kumar K.V 
128f64e8084SAneesh Kumar K.V #define POWER7_TLB_SETS		128	/* # sets in POWER7 TLB */
129f64e8084SAneesh Kumar K.V #define POWER8_TLB_SETS		512	/* # sets in POWER8 TLB */
130f64e8084SAneesh Kumar K.V #define POWER9_TLB_SETS_HASH	256	/* # sets in POWER9 TLB Hash mode */
1311a472c9dSAneesh Kumar K.V #define POWER9_TLB_SETS_RADIX	128	/* # sets in POWER9 TLB Radix mode */
132f64e8084SAneesh Kumar K.V 
133f64e8084SAneesh Kumar K.V #ifndef __ASSEMBLY__
134f64e8084SAneesh Kumar K.V 
1357025776eSBenjamin Herrenschmidt struct mmu_hash_ops {
1367025776eSBenjamin Herrenschmidt 	void            (*hpte_invalidate)(unsigned long slot,
1377025776eSBenjamin Herrenschmidt 					   unsigned long vpn,
1387025776eSBenjamin Herrenschmidt 					   int bpsize, int apsize,
1397025776eSBenjamin Herrenschmidt 					   int ssize, int local);
1407025776eSBenjamin Herrenschmidt 	long		(*hpte_updatepp)(unsigned long slot,
1417025776eSBenjamin Herrenschmidt 					 unsigned long newpp,
1427025776eSBenjamin Herrenschmidt 					 unsigned long vpn,
1437025776eSBenjamin Herrenschmidt 					 int bpsize, int apsize,
1447025776eSBenjamin Herrenschmidt 					 int ssize, unsigned long flags);
1457025776eSBenjamin Herrenschmidt 	void            (*hpte_updateboltedpp)(unsigned long newpp,
1467025776eSBenjamin Herrenschmidt 					       unsigned long ea,
1477025776eSBenjamin Herrenschmidt 					       int psize, int ssize);
1487025776eSBenjamin Herrenschmidt 	long		(*hpte_insert)(unsigned long hpte_group,
1497025776eSBenjamin Herrenschmidt 				       unsigned long vpn,
1507025776eSBenjamin Herrenschmidt 				       unsigned long prpn,
1517025776eSBenjamin Herrenschmidt 				       unsigned long rflags,
1527025776eSBenjamin Herrenschmidt 				       unsigned long vflags,
1537025776eSBenjamin Herrenschmidt 				       int psize, int apsize,
1547025776eSBenjamin Herrenschmidt 				       int ssize);
1557025776eSBenjamin Herrenschmidt 	long		(*hpte_remove)(unsigned long hpte_group);
1567025776eSBenjamin Herrenschmidt 	int             (*hpte_removebolted)(unsigned long ea,
1577025776eSBenjamin Herrenschmidt 					     int psize, int ssize);
1587025776eSBenjamin Herrenschmidt 	void		(*flush_hash_range)(unsigned long number, int local);
1597025776eSBenjamin Herrenschmidt 	void		(*hugepage_invalidate)(unsigned long vsid,
1607025776eSBenjamin Herrenschmidt 					       unsigned long addr,
1617025776eSBenjamin Herrenschmidt 					       unsigned char *hpte_slot_array,
1627025776eSBenjamin Herrenschmidt 					       int psize, int ssize, int local);
163dbcf929cSDavid Gibson 	int		(*resize_hpt)(unsigned long shift);
1647025776eSBenjamin Herrenschmidt 	/*
1657025776eSBenjamin Herrenschmidt 	 * Special for kexec.
1667025776eSBenjamin Herrenschmidt 	 * To be called in real mode with interrupts disabled. No locks are
1677025776eSBenjamin Herrenschmidt 	 * taken as such, concurrent access on pre POWER5 hardware could result
1687025776eSBenjamin Herrenschmidt 	 * in a deadlock.
1697025776eSBenjamin Herrenschmidt 	 * The linear mapping is destroyed as well.
1707025776eSBenjamin Herrenschmidt 	 */
1717025776eSBenjamin Herrenschmidt 	void		(*hpte_clear_all)(void);
1727025776eSBenjamin Herrenschmidt };
1737025776eSBenjamin Herrenschmidt extern struct mmu_hash_ops mmu_hash_ops;
1747025776eSBenjamin Herrenschmidt 
175f64e8084SAneesh Kumar K.V struct hash_pte {
176f64e8084SAneesh Kumar K.V 	__be64 v;
177f64e8084SAneesh Kumar K.V 	__be64 r;
178f64e8084SAneesh Kumar K.V };
179f64e8084SAneesh Kumar K.V 
180f64e8084SAneesh Kumar K.V extern struct hash_pte *htab_address;
181f64e8084SAneesh Kumar K.V extern unsigned long htab_size_bytes;
182f64e8084SAneesh Kumar K.V extern unsigned long htab_hash_mask;
183f64e8084SAneesh Kumar K.V 
184f64e8084SAneesh Kumar K.V 
shift_to_mmu_psize(unsigned int shift)185f64e8084SAneesh Kumar K.V static inline int shift_to_mmu_psize(unsigned int shift)
186f64e8084SAneesh Kumar K.V {
187f64e8084SAneesh Kumar K.V 	int psize;
188f64e8084SAneesh Kumar K.V 
189f64e8084SAneesh Kumar K.V 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
190f64e8084SAneesh Kumar K.V 		if (mmu_psize_defs[psize].shift == shift)
191f64e8084SAneesh Kumar K.V 			return psize;
192f64e8084SAneesh Kumar K.V 	return -1;
193f64e8084SAneesh Kumar K.V }
194f64e8084SAneesh Kumar K.V 
mmu_psize_to_shift(unsigned int mmu_psize)195f64e8084SAneesh Kumar K.V static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
196f64e8084SAneesh Kumar K.V {
197f64e8084SAneesh Kumar K.V 	if (mmu_psize_defs[mmu_psize].shift)
198f64e8084SAneesh Kumar K.V 		return mmu_psize_defs[mmu_psize].shift;
199f64e8084SAneesh Kumar K.V 	BUG();
200f64e8084SAneesh Kumar K.V }
201f64e8084SAneesh Kumar K.V 
ap_to_shift(unsigned long ap)202e3b6b466SSuraj Jitindar Singh static inline unsigned int ap_to_shift(unsigned long ap)
203e3b6b466SSuraj Jitindar Singh {
204e3b6b466SSuraj Jitindar Singh 	int psize;
205e3b6b466SSuraj Jitindar Singh 
206e3b6b466SSuraj Jitindar Singh 	for (psize = 0; psize < MMU_PAGE_COUNT; psize++) {
207e3b6b466SSuraj Jitindar Singh 		if (mmu_psize_defs[psize].ap == ap)
208e3b6b466SSuraj Jitindar Singh 			return mmu_psize_defs[psize].shift;
209e3b6b466SSuraj Jitindar Singh 	}
210e3b6b466SSuraj Jitindar Singh 
211e3b6b466SSuraj Jitindar Singh 	return -1;
212e3b6b466SSuraj Jitindar Singh }
213e3b6b466SSuraj Jitindar Singh 
get_sllp_encoding(int psize)214138ee7eeSAneesh Kumar K.V static inline unsigned long get_sllp_encoding(int psize)
215138ee7eeSAneesh Kumar K.V {
216138ee7eeSAneesh Kumar K.V 	unsigned long sllp;
217138ee7eeSAneesh Kumar K.V 
218138ee7eeSAneesh Kumar K.V 	sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
219138ee7eeSAneesh Kumar K.V 		((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
220138ee7eeSAneesh Kumar K.V 	return sllp;
221138ee7eeSAneesh Kumar K.V }
222138ee7eeSAneesh Kumar K.V 
223f64e8084SAneesh Kumar K.V #endif /* __ASSEMBLY__ */
224f64e8084SAneesh Kumar K.V 
225f64e8084SAneesh Kumar K.V /*
226f64e8084SAneesh Kumar K.V  * Segment sizes.
227f64e8084SAneesh Kumar K.V  * These are the values used by hardware in the B field of
228f64e8084SAneesh Kumar K.V  * SLB entries and the first dword of MMU hashtable entries.
229f64e8084SAneesh Kumar K.V  * The B field is 2 bits; the values 2 and 3 are unused and reserved.
230f64e8084SAneesh Kumar K.V  */
231f64e8084SAneesh Kumar K.V #define MMU_SEGSIZE_256M	0
232f64e8084SAneesh Kumar K.V #define MMU_SEGSIZE_1T		1
233f64e8084SAneesh Kumar K.V 
234f64e8084SAneesh Kumar K.V /*
235f64e8084SAneesh Kumar K.V  * encode page number shift.
236f64e8084SAneesh Kumar K.V  * in order to fit the 78 bit va in a 64 bit variable we shift the va by
237f64e8084SAneesh Kumar K.V  * 12 bits. This enable us to address upto 76 bit va.
238f64e8084SAneesh Kumar K.V  * For hpt hash from a va we can ignore the page size bits of va and for
239f64e8084SAneesh Kumar K.V  * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
240f64e8084SAneesh Kumar K.V  * we work in all cases including 4k page size.
241f64e8084SAneesh Kumar K.V  */
242f64e8084SAneesh Kumar K.V #define VPN_SHIFT	12
243f64e8084SAneesh Kumar K.V 
244f64e8084SAneesh Kumar K.V /*
245f64e8084SAneesh Kumar K.V  * HPTE Large Page (LP) details
246f64e8084SAneesh Kumar K.V  */
247f64e8084SAneesh Kumar K.V #define LP_SHIFT	12
248f64e8084SAneesh Kumar K.V #define LP_BITS		8
249f64e8084SAneesh Kumar K.V #define LP_MASK(i)	((0xFF >> (i)) << LP_SHIFT)
250f64e8084SAneesh Kumar K.V 
251f64e8084SAneesh Kumar K.V #ifndef __ASSEMBLY__
252f64e8084SAneesh Kumar K.V 
slb_vsid_shift(int ssize)253f64e8084SAneesh Kumar K.V static inline int slb_vsid_shift(int ssize)
254f64e8084SAneesh Kumar K.V {
255f64e8084SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M)
256f64e8084SAneesh Kumar K.V 		return SLB_VSID_SHIFT;
257f64e8084SAneesh Kumar K.V 	return SLB_VSID_SHIFT_1T;
258f64e8084SAneesh Kumar K.V }
259f64e8084SAneesh Kumar K.V 
segment_shift(int ssize)260f64e8084SAneesh Kumar K.V static inline int segment_shift(int ssize)
261f64e8084SAneesh Kumar K.V {
262f64e8084SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M)
263f64e8084SAneesh Kumar K.V 		return SID_SHIFT;
264f64e8084SAneesh Kumar K.V 	return SID_SHIFT_1T;
265f64e8084SAneesh Kumar K.V }
266f64e8084SAneesh Kumar K.V 
267f64e8084SAneesh Kumar K.V /*
2680eeede0cSPaul Mackerras  * This array is indexed by the LP field of the HPTE second dword.
2690eeede0cSPaul Mackerras  * Since this field may contain some RPN bits, some entries are
2700eeede0cSPaul Mackerras  * replicated so that we get the same value irrespective of RPN.
2710eeede0cSPaul Mackerras  * The top 4 bits are the page size index (MMU_PAGE_*) for the
2720eeede0cSPaul Mackerras  * actual page size, the bottom 4 bits are the base page size.
2730eeede0cSPaul Mackerras  */
2740eeede0cSPaul Mackerras extern u8 hpte_page_sizes[1 << LP_BITS];
2750eeede0cSPaul Mackerras 
__hpte_page_size(unsigned long h,unsigned long l,bool is_base_size)2760eeede0cSPaul Mackerras static inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
2770eeede0cSPaul Mackerras 					     bool is_base_size)
2780eeede0cSPaul Mackerras {
2790eeede0cSPaul Mackerras 	unsigned int i, lp;
2800eeede0cSPaul Mackerras 
2810eeede0cSPaul Mackerras 	if (!(h & HPTE_V_LARGE))
2820eeede0cSPaul Mackerras 		return 1ul << 12;
2830eeede0cSPaul Mackerras 
2840eeede0cSPaul Mackerras 	/* Look at the 8 bit LP value */
2850eeede0cSPaul Mackerras 	lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
2860eeede0cSPaul Mackerras 	i = hpte_page_sizes[lp];
2870eeede0cSPaul Mackerras 	if (!i)
2880eeede0cSPaul Mackerras 		return 0;
2890eeede0cSPaul Mackerras 	if (!is_base_size)
2900eeede0cSPaul Mackerras 		i >>= 4;
2910eeede0cSPaul Mackerras 	return 1ul << mmu_psize_defs[i & 0xf].shift;
2920eeede0cSPaul Mackerras }
2930eeede0cSPaul Mackerras 
hpte_page_size(unsigned long h,unsigned long l)2940eeede0cSPaul Mackerras static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
2950eeede0cSPaul Mackerras {
2960eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 0);
2970eeede0cSPaul Mackerras }
2980eeede0cSPaul Mackerras 
hpte_base_page_size(unsigned long h,unsigned long l)2990eeede0cSPaul Mackerras static inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
3000eeede0cSPaul Mackerras {
3010eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 1);
3020eeede0cSPaul Mackerras }
3030eeede0cSPaul Mackerras 
3040eeede0cSPaul Mackerras /*
305f64e8084SAneesh Kumar K.V  * The current system page and segment sizes
306f64e8084SAneesh Kumar K.V  */
307f64e8084SAneesh Kumar K.V extern int mmu_kernel_ssize;
308f64e8084SAneesh Kumar K.V extern int mmu_highuser_ssize;
309f64e8084SAneesh Kumar K.V extern u16 mmu_slb_size;
310f64e8084SAneesh Kumar K.V extern unsigned long tce_alloc_start, tce_alloc_end;
311f64e8084SAneesh Kumar K.V 
312f64e8084SAneesh Kumar K.V /*
313f64e8084SAneesh Kumar K.V  * If the processor supports 64k normal pages but not 64k cache
314f64e8084SAneesh Kumar K.V  * inhibited pages, we have to be prepared to switch processes
315f64e8084SAneesh Kumar K.V  * to use 4k pages when they create cache-inhibited mappings.
316f64e8084SAneesh Kumar K.V  * If this is the case, mmu_ci_restrictions will be set to 1.
317f64e8084SAneesh Kumar K.V  */
318f64e8084SAneesh Kumar K.V extern int mmu_ci_restrictions;
319f64e8084SAneesh Kumar K.V 
320f64e8084SAneesh Kumar K.V /*
321f64e8084SAneesh Kumar K.V  * This computes the AVPN and B fields of the first dword of a HPTE,
322f64e8084SAneesh Kumar K.V  * for use when we want to match an existing PTE.  The bottom 7 bits
323f64e8084SAneesh Kumar K.V  * of the returned value are zero.
324f64e8084SAneesh Kumar K.V  */
hpte_encode_avpn(unsigned long vpn,int psize,int ssize)325f64e8084SAneesh Kumar K.V static inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
326f64e8084SAneesh Kumar K.V 					     int ssize)
327f64e8084SAneesh Kumar K.V {
328f64e8084SAneesh Kumar K.V 	unsigned long v;
329f64e8084SAneesh Kumar K.V 	/*
330f64e8084SAneesh Kumar K.V 	 * The AVA field omits the low-order 23 bits of the 78 bits VA.
331f64e8084SAneesh Kumar K.V 	 * These bits are not needed in the PTE, because the
332f64e8084SAneesh Kumar K.V 	 * low-order b of these bits are part of the byte offset
333f64e8084SAneesh Kumar K.V 	 * into the virtual page and, if b < 23, the high-order
334f64e8084SAneesh Kumar K.V 	 * 23-b of these bits are always used in selecting the
335f64e8084SAneesh Kumar K.V 	 * PTEGs to be searched
336f64e8084SAneesh Kumar K.V 	 */
337f64e8084SAneesh Kumar K.V 	v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
338f64e8084SAneesh Kumar K.V 	v <<= HPTE_V_AVPN_SHIFT;
339f64e8084SAneesh Kumar K.V 	v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
340f64e8084SAneesh Kumar K.V 	return v;
341f64e8084SAneesh Kumar K.V }
342f64e8084SAneesh Kumar K.V 
343f64e8084SAneesh Kumar K.V /*
3446b243fcfSPaul Mackerras  * ISA v3.0 defines a new HPTE format, which differs from the old
3456b243fcfSPaul Mackerras  * format in having smaller AVPN and ARPN fields, and the B field
3466b243fcfSPaul Mackerras  * in the second dword instead of the first.
3476b243fcfSPaul Mackerras  */
hpte_old_to_new_v(unsigned long v)3486b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_v(unsigned long v)
3496b243fcfSPaul Mackerras {
3506b243fcfSPaul Mackerras 	/* trim AVPN, drop B */
3516b243fcfSPaul Mackerras 	return v & HPTE_V_COMMON_BITS;
3526b243fcfSPaul Mackerras }
3536b243fcfSPaul Mackerras 
hpte_old_to_new_r(unsigned long v,unsigned long r)3546b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
3556b243fcfSPaul Mackerras {
3566b243fcfSPaul Mackerras 	/* move B field from 1st to 2nd dword, trim ARPN */
3576b243fcfSPaul Mackerras 	return (r & ~HPTE_R_3_0_SSIZE_MASK) |
3586b243fcfSPaul Mackerras 		(((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
3596b243fcfSPaul Mackerras }
3606b243fcfSPaul Mackerras 
hpte_new_to_old_v(unsigned long v,unsigned long r)3616b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
3626b243fcfSPaul Mackerras {
3636b243fcfSPaul Mackerras 	/* insert B field */
3646b243fcfSPaul Mackerras 	return (v & HPTE_V_COMMON_BITS) |
3656b243fcfSPaul Mackerras 		((r & HPTE_R_3_0_SSIZE_MASK) <<
3666b243fcfSPaul Mackerras 		 (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
3676b243fcfSPaul Mackerras }
3686b243fcfSPaul Mackerras 
hpte_new_to_old_r(unsigned long r)3696b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_r(unsigned long r)
3706b243fcfSPaul Mackerras {
3716b243fcfSPaul Mackerras 	/* clear out B field */
3726b243fcfSPaul Mackerras 	return r & ~HPTE_R_3_0_SSIZE_MASK;
3736b243fcfSPaul Mackerras }
3746b243fcfSPaul Mackerras 
hpte_get_old_v(struct hash_pte * hptep)375a833280bSAneesh Kumar K.V static inline unsigned long hpte_get_old_v(struct hash_pte *hptep)
376a833280bSAneesh Kumar K.V {
377a833280bSAneesh Kumar K.V 	unsigned long hpte_v;
378a833280bSAneesh Kumar K.V 
379a833280bSAneesh Kumar K.V 	hpte_v = be64_to_cpu(hptep->v);
380a833280bSAneesh Kumar K.V 	if (cpu_has_feature(CPU_FTR_ARCH_300))
381a833280bSAneesh Kumar K.V 		hpte_v = hpte_new_to_old_v(hpte_v, be64_to_cpu(hptep->r));
382a833280bSAneesh Kumar K.V 	return hpte_v;
383a833280bSAneesh Kumar K.V }
384a833280bSAneesh Kumar K.V 
3856b243fcfSPaul Mackerras /*
386f64e8084SAneesh Kumar K.V  * This function sets the AVPN and L fields of the HPTE  appropriately
387f64e8084SAneesh Kumar K.V  * using the base page size and actual page size.
388f64e8084SAneesh Kumar K.V  */
hpte_encode_v(unsigned long vpn,int base_psize,int actual_psize,int ssize)389f64e8084SAneesh Kumar K.V static inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
390f64e8084SAneesh Kumar K.V 					  int actual_psize, int ssize)
391f64e8084SAneesh Kumar K.V {
392f64e8084SAneesh Kumar K.V 	unsigned long v;
393f64e8084SAneesh Kumar K.V 	v = hpte_encode_avpn(vpn, base_psize, ssize);
394f64e8084SAneesh Kumar K.V 	if (actual_psize != MMU_PAGE_4K)
395f64e8084SAneesh Kumar K.V 		v |= HPTE_V_LARGE;
396f64e8084SAneesh Kumar K.V 	return v;
397f64e8084SAneesh Kumar K.V }
398f64e8084SAneesh Kumar K.V 
399f64e8084SAneesh Kumar K.V /*
400f64e8084SAneesh Kumar K.V  * This function sets the ARPN, and LP fields of the HPTE appropriately
401f64e8084SAneesh Kumar K.V  * for the page size. We assume the pa is already "clean" that is properly
402f64e8084SAneesh Kumar K.V  * aligned for the requested page size
403f64e8084SAneesh Kumar K.V  */
hpte_encode_r(unsigned long pa,int base_psize,int actual_psize)404f64e8084SAneesh Kumar K.V static inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
4056b243fcfSPaul Mackerras 					  int actual_psize)
406f64e8084SAneesh Kumar K.V {
407f64e8084SAneesh Kumar K.V 	/* A 4K page needs no special encoding */
408f64e8084SAneesh Kumar K.V 	if (actual_psize == MMU_PAGE_4K)
409f64e8084SAneesh Kumar K.V 		return pa & HPTE_R_RPN;
410f64e8084SAneesh Kumar K.V 	else {
411f64e8084SAneesh Kumar K.V 		unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
412f64e8084SAneesh Kumar K.V 		unsigned int shift = mmu_psize_defs[actual_psize].shift;
413f64e8084SAneesh Kumar K.V 		return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
414f64e8084SAneesh Kumar K.V 	}
415f64e8084SAneesh Kumar K.V }
416f64e8084SAneesh Kumar K.V 
417f64e8084SAneesh Kumar K.V /*
418f64e8084SAneesh Kumar K.V  * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
419f64e8084SAneesh Kumar K.V  */
hpt_vpn(unsigned long ea,unsigned long vsid,int ssize)420f64e8084SAneesh Kumar K.V static inline unsigned long hpt_vpn(unsigned long ea,
421f64e8084SAneesh Kumar K.V 				    unsigned long vsid, int ssize)
422f64e8084SAneesh Kumar K.V {
423f64e8084SAneesh Kumar K.V 	unsigned long mask;
424f64e8084SAneesh Kumar K.V 	int s_shift = segment_shift(ssize);
425f64e8084SAneesh Kumar K.V 
426f64e8084SAneesh Kumar K.V 	mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
427f64e8084SAneesh Kumar K.V 	return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
428f64e8084SAneesh Kumar K.V }
429f64e8084SAneesh Kumar K.V 
430f64e8084SAneesh Kumar K.V /*
431f64e8084SAneesh Kumar K.V  * This hashes a virtual address
432f64e8084SAneesh Kumar K.V  */
hpt_hash(unsigned long vpn,unsigned int shift,int ssize)433f64e8084SAneesh Kumar K.V static inline unsigned long hpt_hash(unsigned long vpn,
434f64e8084SAneesh Kumar K.V 				     unsigned int shift, int ssize)
435f64e8084SAneesh Kumar K.V {
43659248aecSAneesh Kumar K.V 	unsigned long mask;
437f64e8084SAneesh Kumar K.V 	unsigned long hash, vsid;
438f64e8084SAneesh Kumar K.V 
439f64e8084SAneesh Kumar K.V 	/* VPN_SHIFT can be atmost 12 */
440f64e8084SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M) {
441f64e8084SAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
442f64e8084SAneesh Kumar K.V 		hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
443f64e8084SAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT));
444f64e8084SAneesh Kumar K.V 	} else {
445f64e8084SAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
446f64e8084SAneesh Kumar K.V 		vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
447f64e8084SAneesh Kumar K.V 		hash = vsid ^ (vsid << 25) ^
448f64e8084SAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT)) ;
449f64e8084SAneesh Kumar K.V 	}
450f64e8084SAneesh Kumar K.V 	return hash & 0x7fffffffffUL;
451f64e8084SAneesh Kumar K.V }
452f64e8084SAneesh Kumar K.V 
453f64e8084SAneesh Kumar K.V #define HPTE_LOCAL_UPDATE	0x1
454f64e8084SAneesh Kumar K.V #define HPTE_NOHPTE_UPDATE	0x2
455d94b827eSAneesh Kumar K.V #define HPTE_USE_KERNEL_KEY	0x4
456f64e8084SAneesh Kumar K.V 
45711f9c1d2SCédric Le Goater long hpte_insert_repeating(unsigned long hash, unsigned long vpn, unsigned long pa,
45811f9c1d2SCédric Le Goater 			   unsigned long rlags, unsigned long vflags, int psize, int ssize);
459f64e8084SAneesh Kumar K.V extern int __hash_page_4K(unsigned long ea, unsigned long access,
460f64e8084SAneesh Kumar K.V 			  unsigned long vsid, pte_t *ptep, unsigned long trap,
461f64e8084SAneesh Kumar K.V 			  unsigned long flags, int ssize, int subpage_prot);
462f64e8084SAneesh Kumar K.V extern int __hash_page_64K(unsigned long ea, unsigned long access,
463f64e8084SAneesh Kumar K.V 			   unsigned long vsid, pte_t *ptep, unsigned long trap,
464f64e8084SAneesh Kumar K.V 			   unsigned long flags, int ssize);
465f64e8084SAneesh Kumar K.V struct mm_struct;
466f64e8084SAneesh Kumar K.V unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
467f64e8084SAneesh Kumar K.V extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
468f64e8084SAneesh Kumar K.V 			unsigned long access, unsigned long trap,
469f64e8084SAneesh Kumar K.V 			unsigned long flags);
470f64e8084SAneesh Kumar K.V extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
471f64e8084SAneesh Kumar K.V 		     unsigned long dsisr);
472cccaf1a1SCédric Le Goater void low_hash_fault(struct pt_regs *regs, unsigned long address, int rc);
473cccaf1a1SCédric Le Goater int __hash_page(unsigned long trap, unsigned long ea, unsigned long dsisr, unsigned long msr);
474f64e8084SAneesh Kumar K.V int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
475f64e8084SAneesh Kumar K.V 		     pte_t *ptep, unsigned long trap, unsigned long flags,
476f64e8084SAneesh Kumar K.V 		     int ssize, unsigned int shift, unsigned int mmu_psize);
477f64e8084SAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
478f64e8084SAneesh Kumar K.V extern int __hash_page_thp(unsigned long ea, unsigned long access,
479f64e8084SAneesh Kumar K.V 			   unsigned long vsid, pmd_t *pmdp, unsigned long trap,
480f64e8084SAneesh Kumar K.V 			   unsigned long flags, int ssize, unsigned int psize);
481f64e8084SAneesh Kumar K.V #else
__hash_page_thp(unsigned long ea,unsigned long access,unsigned long vsid,pmd_t * pmdp,unsigned long trap,unsigned long flags,int ssize,unsigned int psize)482f64e8084SAneesh Kumar K.V static inline int __hash_page_thp(unsigned long ea, unsigned long access,
483f64e8084SAneesh Kumar K.V 				  unsigned long vsid, pmd_t *pmdp,
484f64e8084SAneesh Kumar K.V 				  unsigned long trap, unsigned long flags,
485f64e8084SAneesh Kumar K.V 				  int ssize, unsigned int psize)
486f64e8084SAneesh Kumar K.V {
487f64e8084SAneesh Kumar K.V 	BUG();
488f64e8084SAneesh Kumar K.V 	return -1;
489f64e8084SAneesh Kumar K.V }
490f64e8084SAneesh Kumar K.V #endif
491f64e8084SAneesh Kumar K.V extern void hash_failure_debug(unsigned long ea, unsigned long access,
492f64e8084SAneesh Kumar K.V 			       unsigned long vsid, unsigned long trap,
493f64e8084SAneesh Kumar K.V 			       int ssize, int psize, int lpsize,
494f64e8084SAneesh Kumar K.V 			       unsigned long pte);
495f64e8084SAneesh Kumar K.V extern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
496f64e8084SAneesh Kumar K.V 			     unsigned long pstart, unsigned long prot,
497f64e8084SAneesh Kumar K.V 			     int psize, int ssize);
498f64e8084SAneesh Kumar K.V int htab_remove_mapping(unsigned long vstart, unsigned long vend,
499f64e8084SAneesh Kumar K.V 			int psize, int ssize);
50079cc38deSAneesh Kumar K.V extern void pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
501f64e8084SAneesh Kumar K.V extern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
502f64e8084SAneesh Kumar K.V 
503425d3314SNicholas Piggin extern void hash__setup_new_exec(void);
504425d3314SNicholas Piggin 
5056364e84eSMichael Ellerman #ifdef CONFIG_PPC_PSERIES
5066364e84eSMichael Ellerman void hpte_init_pseries(void);
5076364e84eSMichael Ellerman #else
hpte_init_pseries(void)5086364e84eSMichael Ellerman static inline void hpte_init_pseries(void) { }
5096364e84eSMichael Ellerman #endif
5106364e84eSMichael Ellerman 
511f64e8084SAneesh Kumar K.V extern void hpte_init_native(void);
512f64e8084SAneesh Kumar K.V 
513c6d15258SMahesh Salgaonkar struct slb_entry {
514c6d15258SMahesh Salgaonkar 	u64	esid;
515c6d15258SMahesh Salgaonkar 	u64	vsid;
516c6d15258SMahesh Salgaonkar };
517c6d15258SMahesh Salgaonkar 
518f64e8084SAneesh Kumar K.V extern void slb_initialize(void);
51994ee4272SNicholas Piggin void slb_flush_and_restore_bolted(void);
520e7e81847SNicholas Piggin void slb_flush_all_realmode(void);
521e7e81847SNicholas Piggin void __slb_restore_bolted_realmode(void);
522e7e81847SNicholas Piggin void slb_restore_bolted_realmode(void);
523c6d15258SMahesh Salgaonkar void slb_save_contents(struct slb_entry *slb_ptr);
524c6d15258SMahesh Salgaonkar void slb_dump_contents(struct slb_entry *slb_ptr);
525f64e8084SAneesh Kumar K.V 
526f64e8084SAneesh Kumar K.V extern void slb_vmalloc_update(void);
5271f55aefeSCédric Le Goater void preload_new_slb_context(unsigned long start, unsigned long sp);
528387e220aSNicholas Piggin 
529387e220aSNicholas Piggin #ifdef CONFIG_PPC_64S_HASH_MMU
530387e220aSNicholas Piggin void slb_set_size(u16 size);
531387e220aSNicholas Piggin #else
slb_set_size(u16 size)532387e220aSNicholas Piggin static inline void slb_set_size(u16 size) { }
533387e220aSNicholas Piggin #endif
534387e220aSNicholas Piggin 
535f64e8084SAneesh Kumar K.V #endif /* __ASSEMBLY__ */
536f64e8084SAneesh Kumar K.V 
537f64e8084SAneesh Kumar K.V /*
538f64e8084SAneesh Kumar K.V  * VSID allocation (256MB segment)
539f64e8084SAneesh Kumar K.V  *
540f64e8084SAneesh Kumar K.V  * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
541f64e8084SAneesh Kumar K.V  * from mmu context id and effective segment id of the address.
542f64e8084SAneesh Kumar K.V  *
543941711a3SAneesh Kumar K.V  * For user processes max context id is limited to MAX_USER_CONTEXT.
5444ffe713bSAneesh Kumar K.V  * more details in get_user_context
5454ffe713bSAneesh Kumar K.V  *
5464ffe713bSAneesh Kumar K.V  * For kernel space get_kernel_context
547f64e8084SAneesh Kumar K.V  *
548f64e8084SAneesh Kumar K.V  * The proto-VSIDs are then scrambled into real VSIDs with the
549f64e8084SAneesh Kumar K.V  * multiplicative hash:
550f64e8084SAneesh Kumar K.V  *
551f64e8084SAneesh Kumar K.V  *	VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
552f64e8084SAneesh Kumar K.V  *
553f64e8084SAneesh Kumar K.V  * VSID_MULTIPLIER is prime, so in particular it is
554f64e8084SAneesh Kumar K.V  * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
555f64e8084SAneesh Kumar K.V  * Because the modulus is 2^n-1 we can compute it efficiently without
556f64e8084SAneesh Kumar K.V  * a divide or extra multiply (see below). The scramble function gives
557f64e8084SAneesh Kumar K.V  * robust scattering in the hash table (at least based on some initial
558f64e8084SAneesh Kumar K.V  * results).
559f64e8084SAneesh Kumar K.V  *
560941711a3SAneesh Kumar K.V  * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
561941711a3SAneesh Kumar K.V  * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
562941711a3SAneesh Kumar K.V  * will produce a VSID of 0.
563f64e8084SAneesh Kumar K.V  *
564f64e8084SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
565f64e8084SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
566941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
567f64e8084SAneesh Kumar K.V  */
568f64e8084SAneesh Kumar K.V 
569e6f81a92SAneesh Kumar K.V /*
570e6f81a92SAneesh Kumar K.V  * Max Va bits we support as of now is 68 bits. We want 19 bit
571e6f81a92SAneesh Kumar K.V  * context ID.
572e6f81a92SAneesh Kumar K.V  * Restrictions:
573e6f81a92SAneesh Kumar K.V  * GPU has restrictions of not able to access beyond 128TB
574e6f81a92SAneesh Kumar K.V  * (47 bit effective address). We also cannot do more than 20bit PID.
575e6f81a92SAneesh Kumar K.V  * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
576e6f81a92SAneesh Kumar K.V  * to 16 bits (ie, we can only have 2^16 pids at the same time).
577e6f81a92SAneesh Kumar K.V  */
578e6f81a92SAneesh Kumar K.V #define VA_BITS			68
579f64e8084SAneesh Kumar K.V #define CONTEXT_BITS		19
580e6f81a92SAneesh Kumar K.V #define ESID_BITS		(VA_BITS - (SID_SHIFT + CONTEXT_BITS))
581e6f81a92SAneesh Kumar K.V #define ESID_BITS_1T		(VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
582f64e8084SAneesh Kumar K.V 
58379270e0aSAneesh Kumar K.V #define ESID_BITS_MASK		((1 << ESID_BITS) - 1)
58479270e0aSAneesh Kumar K.V #define ESID_BITS_1T_MASK	((1 << ESID_BITS_1T) - 1)
58579270e0aSAneesh Kumar K.V 
586f64e8084SAneesh Kumar K.V /*
5874ffe713bSAneesh Kumar K.V  * Now certain config support MAX_PHYSMEM more than 512TB. Hence we will need
5884ffe713bSAneesh Kumar K.V  * to use more than one context for linear mapping the kernel.
5894ffe713bSAneesh Kumar K.V  * For vmalloc and memmap, we use just one context with 512TB. With 64 byte
5904ffe713bSAneesh Kumar K.V  * struct page size, we need ony 32 TB in memmap for 2PB (51 bits (MAX_PHYSMEM_BITS)).
5914ffe713bSAneesh Kumar K.V  */
592b32d5d7eSAneesh Kumar K.V #if (H_MAX_PHYSMEM_BITS > MAX_EA_BITS_PER_CONTEXT)
593b32d5d7eSAneesh Kumar K.V #define MAX_KERNEL_CTX_CNT	(1UL << (H_MAX_PHYSMEM_BITS - MAX_EA_BITS_PER_CONTEXT))
5944ffe713bSAneesh Kumar K.V #else
5954ffe713bSAneesh Kumar K.V #define MAX_KERNEL_CTX_CNT	1
5964ffe713bSAneesh Kumar K.V #endif
5974ffe713bSAneesh Kumar K.V 
5984ffe713bSAneesh Kumar K.V #define MAX_VMALLOC_CTX_CNT	1
5990034d395SAneesh Kumar K.V #define MAX_IO_CTX_CNT		1
6000034d395SAneesh Kumar K.V #define MAX_VMEMMAP_CTX_CNT	1
6014ffe713bSAneesh Kumar K.V 
6024ffe713bSAneesh Kumar K.V /*
603f64e8084SAneesh Kumar K.V  * 256MB segment
604f64e8084SAneesh Kumar K.V  * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
605941711a3SAneesh Kumar K.V  * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
606941711a3SAneesh Kumar K.V  * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
607e6f81a92SAneesh Kumar K.V  * context maps 2^49 bytes (512TB).
608941711a3SAneesh Kumar K.V  *
609941711a3SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
610941711a3SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
611941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
6124ffe713bSAneesh Kumar K.V  *
613f64e8084SAneesh Kumar K.V  */
614941711a3SAneesh Kumar K.V #define MAX_USER_CONTEXT	((ASM_CONST(1) << CONTEXT_BITS) - 2)
6155d2e5dd5SAneesh Kumar K.V 
6165d2e5dd5SAneesh Kumar K.V // The + 2 accounts for INVALID_REGION and 1 more to avoid overlap with kernel
6174ffe713bSAneesh Kumar K.V #define MIN_USER_CONTEXT	(MAX_KERNEL_CTX_CNT + MAX_VMALLOC_CTX_CNT + \
6185d2e5dd5SAneesh Kumar K.V 				 MAX_IO_CTX_CNT + MAX_VMEMMAP_CTX_CNT + 2)
6195d2e5dd5SAneesh Kumar K.V 
620f64e8084SAneesh Kumar K.V /*
621e6f81a92SAneesh Kumar K.V  * For platforms that support on 65bit VA we limit the context bits
622f64e8084SAneesh Kumar K.V  */
623e6f81a92SAneesh Kumar K.V #define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
624f64e8084SAneesh Kumar K.V 
625f64e8084SAneesh Kumar K.V /*
626e6f81a92SAneesh Kumar K.V  * This should be computed such that protovosid * vsid_mulitplier
627e6f81a92SAneesh Kumar K.V  * doesn't overflow 64 bits. The vsid_mutliplier should also be
628e6f81a92SAneesh Kumar K.V  * co-prime to vsid_modulus. We also need to make sure that number
629e6f81a92SAneesh Kumar K.V  * of bits in multiplied result (dividend) is less than twice the number of
630e6f81a92SAneesh Kumar K.V  * protovsid bits for our modulus optmization to work.
631f64e8084SAneesh Kumar K.V  *
632e6f81a92SAneesh Kumar K.V  * The below table shows the current values used.
633e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
634e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
635e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
636e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   25 |         49 |                50 |
637e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
638e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   37 |         61 |                74 |
639e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
640f64e8084SAneesh Kumar K.V  *
641e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
642e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
643e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
644e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   28 |         52 |                 56 |
645e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
646e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   40 |         64 |                 80 |
647e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
648e6f81a92SAneesh Kumar K.V  *
649f64e8084SAneesh Kumar K.V  */
650e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_256M	ASM_CONST(12538073)	/* 24-bit prime */
651e6f81a92SAneesh Kumar K.V #define VSID_BITS_256M		(VA_BITS - SID_SHIFT)
652e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_256M	(65 - SID_SHIFT)
65382228e36SAneesh Kumar K.V /*
65482228e36SAneesh Kumar K.V  * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
65582228e36SAneesh Kumar K.V  */
65682228e36SAneesh Kumar K.V #define VSID_MULINV_256M	ASM_CONST(665548017062)
657e6f81a92SAneesh Kumar K.V 
658e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_1T	ASM_CONST(12538073)	/* 24-bit prime */
659e6f81a92SAneesh Kumar K.V #define VSID_BITS_1T		(VA_BITS - SID_SHIFT_1T)
660e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_1T		(65 - SID_SHIFT_1T)
66182228e36SAneesh Kumar K.V #define VSID_MULINV_1T		ASM_CONST(209034062)
662e6f81a92SAneesh Kumar K.V 
66382228e36SAneesh Kumar K.V /* 1TB VSID reserved for VRMA */
66482228e36SAneesh Kumar K.V #define VRMA_VSID	0x1ffffffUL
665e6f81a92SAneesh Kumar K.V #define USER_VSID_RANGE	(1UL << (ESID_BITS + SID_SHIFT))
666f64e8084SAneesh Kumar K.V 
667f64e8084SAneesh Kumar K.V /* 4 bits per slice and we have one slice per 1TB */
668dd1842a2SAneesh Kumar K.V #define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
66960458fbaSAneesh Kumar K.V #define LOW_SLICE_ARRAY_SZ	(BITS_PER_LONG / BITS_PER_BYTE)
67070110186SAneesh Kumar K.V #define TASK_SLICE_ARRAY_SZ(x)	((x)->hash_context->slb_addr_limit >> 41)
671f64e8084SAneesh Kumar K.V #ifndef __ASSEMBLY__
672f64e8084SAneesh Kumar K.V 
673f64e8084SAneesh Kumar K.V #ifdef CONFIG_PPC_SUBPAGE_PROT
674f64e8084SAneesh Kumar K.V /*
675f64e8084SAneesh Kumar K.V  * For the sub-page protection option, we extend the PGD with one of
676f64e8084SAneesh Kumar K.V  * these.  Basically we have a 3-level tree, with the top level being
677f64e8084SAneesh Kumar K.V  * the protptrs array.  To optimize speed and memory consumption when
678f64e8084SAneesh Kumar K.V  * only addresses < 4GB are being protected, pointers to the first
679f64e8084SAneesh Kumar K.V  * four pages of sub-page protection words are stored in the low_prot
680f64e8084SAneesh Kumar K.V  * array.
681f64e8084SAneesh Kumar K.V  * Each page of sub-page protection words protects 1GB (4 bytes
682f64e8084SAneesh Kumar K.V  * protects 64k).  For the 3-level tree, each page of pointers then
683f64e8084SAneesh Kumar K.V  * protects 8TB.
684f64e8084SAneesh Kumar K.V  */
685f64e8084SAneesh Kumar K.V struct subpage_prot_table {
686f64e8084SAneesh Kumar K.V 	unsigned long maxaddr;	/* only addresses < this are protected */
687f64e8084SAneesh Kumar K.V 	unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
688f64e8084SAneesh Kumar K.V 	unsigned int *low_prot[4];
689f64e8084SAneesh Kumar K.V };
690f64e8084SAneesh Kumar K.V 
691f64e8084SAneesh Kumar K.V #define SBP_L1_BITS		(PAGE_SHIFT - 2)
692f64e8084SAneesh Kumar K.V #define SBP_L2_BITS		(PAGE_SHIFT - 3)
693f64e8084SAneesh Kumar K.V #define SBP_L1_COUNT		(1 << SBP_L1_BITS)
694f64e8084SAneesh Kumar K.V #define SBP_L2_COUNT		(1 << SBP_L2_BITS)
695f64e8084SAneesh Kumar K.V #define SBP_L2_SHIFT		(PAGE_SHIFT + SBP_L1_BITS)
696f64e8084SAneesh Kumar K.V #define SBP_L3_SHIFT		(SBP_L2_SHIFT + SBP_L2_BITS)
697f64e8084SAneesh Kumar K.V 
698f64e8084SAneesh Kumar K.V extern void subpage_prot_free(struct mm_struct *mm);
699f64e8084SAneesh Kumar K.V #else
subpage_prot_free(struct mm_struct * mm)700f64e8084SAneesh Kumar K.V static inline void subpage_prot_free(struct mm_struct *mm) {}
701f64e8084SAneesh Kumar K.V #endif /* CONFIG_PPC_SUBPAGE_PROT */
702f64e8084SAneesh Kumar K.V 
70370110186SAneesh Kumar K.V /*
70470110186SAneesh Kumar K.V  * One bit per slice. We have lower slices which cover 256MB segments
70570110186SAneesh Kumar K.V  * upto 4G range. That gets us 16 low slices. For the rest we track slices
70670110186SAneesh Kumar K.V  * in 1TB size.
70770110186SAneesh Kumar K.V  */
70870110186SAneesh Kumar K.V struct slice_mask {
70970110186SAneesh Kumar K.V 	u64 low_slices;
71070110186SAneesh Kumar K.V 	DECLARE_BITMAP(high_slices, SLICE_NUM_HIGH);
71170110186SAneesh Kumar K.V };
71270110186SAneesh Kumar K.V 
71370110186SAneesh Kumar K.V struct hash_mm_context {
71470110186SAneesh Kumar K.V 	u16 user_psize; /* page size index */
71570110186SAneesh Kumar K.V 
71670110186SAneesh Kumar K.V 	/* SLB page size encodings*/
71770110186SAneesh Kumar K.V 	unsigned char low_slices_psize[LOW_SLICE_ARRAY_SZ];
71870110186SAneesh Kumar K.V 	unsigned char high_slices_psize[SLICE_ARRAY_SIZE];
71970110186SAneesh Kumar K.V 	unsigned long slb_addr_limit;
72070110186SAneesh Kumar K.V #ifdef CONFIG_PPC_64K_PAGES
72170110186SAneesh Kumar K.V 	struct slice_mask mask_64k;
72270110186SAneesh Kumar K.V #endif
72370110186SAneesh Kumar K.V 	struct slice_mask mask_4k;
72470110186SAneesh Kumar K.V #ifdef CONFIG_HUGETLB_PAGE
72570110186SAneesh Kumar K.V 	struct slice_mask mask_16m;
72670110186SAneesh Kumar K.V 	struct slice_mask mask_16g;
72770110186SAneesh Kumar K.V #endif
72870110186SAneesh Kumar K.V 
72970110186SAneesh Kumar K.V #ifdef CONFIG_PPC_SUBPAGE_PROT
730ef629cc5SAneesh Kumar K.V 	struct subpage_prot_table *spt;
73170110186SAneesh Kumar K.V #endif /* CONFIG_PPC_SUBPAGE_PROT */
73270110186SAneesh Kumar K.V };
73370110186SAneesh Kumar K.V 
734f64e8084SAneesh Kumar K.V #if 0
735f64e8084SAneesh Kumar K.V /*
736f64e8084SAneesh Kumar K.V  * The code below is equivalent to this function for arguments
737f64e8084SAneesh Kumar K.V  * < 2^VSID_BITS, which is all this should ever be called
738f64e8084SAneesh Kumar K.V  * with.  However gcc is not clever enough to compute the
739f64e8084SAneesh Kumar K.V  * modulus (2^n-1) without a second multiply.
740f64e8084SAneesh Kumar K.V  */
741f64e8084SAneesh Kumar K.V #define vsid_scramble(protovsid, size) \
742f64e8084SAneesh Kumar K.V 	((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
743f64e8084SAneesh Kumar K.V 
744e6f81a92SAneesh Kumar K.V /* simplified form avoiding mod operation */
745f64e8084SAneesh Kumar K.V #define vsid_scramble(protovsid, size) \
746f64e8084SAneesh Kumar K.V 	({								 \
747f64e8084SAneesh Kumar K.V 		unsigned long x;					 \
748f64e8084SAneesh Kumar K.V 		x = (protovsid) * VSID_MULTIPLIER_##size;		 \
749f64e8084SAneesh Kumar K.V 		x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
750f64e8084SAneesh Kumar K.V 		(x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
751f64e8084SAneesh Kumar K.V 	})
752e6f81a92SAneesh Kumar K.V 
753e6f81a92SAneesh Kumar K.V #else /* 1 */
vsid_scramble(unsigned long protovsid,unsigned long vsid_multiplier,int vsid_bits)754e6f81a92SAneesh Kumar K.V static inline unsigned long vsid_scramble(unsigned long protovsid,
755e6f81a92SAneesh Kumar K.V 				  unsigned long vsid_multiplier, int vsid_bits)
756e6f81a92SAneesh Kumar K.V {
757e6f81a92SAneesh Kumar K.V 	unsigned long vsid;
758e6f81a92SAneesh Kumar K.V 	unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
759e6f81a92SAneesh Kumar K.V 	/*
760e6f81a92SAneesh Kumar K.V 	 * We have same multipler for both 256 and 1T segements now
761e6f81a92SAneesh Kumar K.V 	 */
762e6f81a92SAneesh Kumar K.V 	vsid = protovsid * vsid_multiplier;
763e6f81a92SAneesh Kumar K.V 	vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
764e6f81a92SAneesh Kumar K.V 	return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
765e6f81a92SAneesh Kumar K.V }
766e6f81a92SAneesh Kumar K.V 
767f64e8084SAneesh Kumar K.V #endif /* 1 */
768f64e8084SAneesh Kumar K.V 
769f64e8084SAneesh Kumar K.V /* Returns the segment size indicator for a user address */
user_segment_size(unsigned long addr)770f64e8084SAneesh Kumar K.V static inline int user_segment_size(unsigned long addr)
771f64e8084SAneesh Kumar K.V {
772f64e8084SAneesh Kumar K.V 	/* Use 1T segments if possible for addresses >= 1T */
773f64e8084SAneesh Kumar K.V 	if (addr >= (1UL << SID_SHIFT_1T))
774f64e8084SAneesh Kumar K.V 		return mmu_highuser_ssize;
775f64e8084SAneesh Kumar K.V 	return MMU_SEGSIZE_256M;
776f64e8084SAneesh Kumar K.V }
777f64e8084SAneesh Kumar K.V 
get_vsid(unsigned long context,unsigned long ea,int ssize)778f64e8084SAneesh Kumar K.V static inline unsigned long get_vsid(unsigned long context, unsigned long ea,
779f64e8084SAneesh Kumar K.V 				     int ssize)
780f64e8084SAneesh Kumar K.V {
781e6f81a92SAneesh Kumar K.V 	unsigned long va_bits = VA_BITS;
782e6f81a92SAneesh Kumar K.V 	unsigned long vsid_bits;
783e6f81a92SAneesh Kumar K.V 	unsigned long protovsid;
784e6f81a92SAneesh Kumar K.V 
785f64e8084SAneesh Kumar K.V 	/*
786f64e8084SAneesh Kumar K.V 	 * Bad address. We return VSID 0 for that
787f64e8084SAneesh Kumar K.V 	 */
7880034d395SAneesh Kumar K.V 	if ((ea & EA_MASK)  >= H_PGTABLE_RANGE)
789f64e8084SAneesh Kumar K.V 		return 0;
790f64e8084SAneesh Kumar K.V 
791e6f81a92SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
792e6f81a92SAneesh Kumar K.V 		va_bits = 65;
793e6f81a92SAneesh Kumar K.V 
794e6f81a92SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M) {
795e6f81a92SAneesh Kumar K.V 		vsid_bits = va_bits - SID_SHIFT;
796e6f81a92SAneesh Kumar K.V 		protovsid = (context << ESID_BITS) |
797e6f81a92SAneesh Kumar K.V 			((ea >> SID_SHIFT) & ESID_BITS_MASK);
798e6f81a92SAneesh Kumar K.V 		return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
799e6f81a92SAneesh Kumar K.V 	}
800e6f81a92SAneesh Kumar K.V 	/* 1T segment */
801e6f81a92SAneesh Kumar K.V 	vsid_bits = va_bits - SID_SHIFT_1T;
802e6f81a92SAneesh Kumar K.V 	protovsid = (context << ESID_BITS_1T) |
803e6f81a92SAneesh Kumar K.V 		((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
804e6f81a92SAneesh Kumar K.V 	return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
805f64e8084SAneesh Kumar K.V }
806f64e8084SAneesh Kumar K.V 
807f64e8084SAneesh Kumar K.V /*
80810a4a016SRandy Dunlap  * For kernel space, we use context ids as
8094ffe713bSAneesh Kumar K.V  * below. Range is 512TB per context.
8104ffe713bSAneesh Kumar K.V  *
8114ffe713bSAneesh Kumar K.V  * 0x00001 -  [ 0xc000000000000000 - 0xc001ffffffffffff]
8124ffe713bSAneesh Kumar K.V  * 0x00002 -  [ 0xc002000000000000 - 0xc003ffffffffffff]
8134ffe713bSAneesh Kumar K.V  * 0x00003 -  [ 0xc004000000000000 - 0xc005ffffffffffff]
8144ffe713bSAneesh Kumar K.V  * 0x00004 -  [ 0xc006000000000000 - 0xc007ffffffffffff]
8154ffe713bSAneesh Kumar K.V  *
8160034d395SAneesh Kumar K.V  * vmap, IO, vmemap
8170034d395SAneesh Kumar K.V  *
8180034d395SAneesh Kumar K.V  * 0x00005 -  [ 0xc008000000000000 - 0xc009ffffffffffff]
8190034d395SAneesh Kumar K.V  * 0x00006 -  [ 0xc00a000000000000 - 0xc00bffffffffffff]
8200034d395SAneesh Kumar K.V  * 0x00007 -  [ 0xc00c000000000000 - 0xc00dffffffffffff]
8210034d395SAneesh Kumar K.V  *
8224ffe713bSAneesh Kumar K.V  */
get_kernel_context(unsigned long ea)8234ffe713bSAneesh Kumar K.V static inline unsigned long get_kernel_context(unsigned long ea)
8244ffe713bSAneesh Kumar K.V {
8250034d395SAneesh Kumar K.V 	unsigned long region_id = get_region_id(ea);
8264ffe713bSAneesh Kumar K.V 	unsigned long ctx;
8274ffe713bSAneesh Kumar K.V 	/*
8280034d395SAneesh Kumar K.V 	 * Depending on Kernel config, kernel region can have one context
8290034d395SAneesh Kumar K.V 	 * or more.
8304ffe713bSAneesh Kumar K.V 	 */
8315f53d286SAneesh Kumar K.V 	if (region_id == LINEAR_MAP_REGION_ID) {
8324ffe713bSAneesh Kumar K.V 		/*
8334ffe713bSAneesh Kumar K.V 		 * We already verified ea to be not beyond the addr limit.
8344ffe713bSAneesh Kumar K.V 		 */
8350034d395SAneesh Kumar K.V 		ctx =  1 + ((ea & EA_MASK) >> MAX_EA_BITS_PER_CONTEXT);
8364ffe713bSAneesh Kumar K.V 	} else
8371c946c1bSAneesh Kumar K.V 		ctx = region_id + MAX_KERNEL_CTX_CNT - 1;
8384ffe713bSAneesh Kumar K.V 	return ctx;
8394ffe713bSAneesh Kumar K.V }
8404ffe713bSAneesh Kumar K.V 
8414ffe713bSAneesh Kumar K.V /*
842f64e8084SAneesh Kumar K.V  * This is only valid for addresses >= PAGE_OFFSET
843f64e8084SAneesh Kumar K.V  */
get_kernel_vsid(unsigned long ea,int ssize)844f64e8084SAneesh Kumar K.V static inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
845f64e8084SAneesh Kumar K.V {
846f64e8084SAneesh Kumar K.V 	unsigned long context;
847f64e8084SAneesh Kumar K.V 
84885beb1c4SMichael Ellerman 	if (!is_kernel_addr(ea))
84985beb1c4SMichael Ellerman 		return 0;
85085beb1c4SMichael Ellerman 
8514ffe713bSAneesh Kumar K.V 	context = get_kernel_context(ea);
852f64e8084SAneesh Kumar K.V 	return get_vsid(context, ea, ssize);
853f64e8084SAneesh Kumar K.V }
854f64e8084SAneesh Kumar K.V 
855f64e8084SAneesh Kumar K.V unsigned htab_shift_for_mem_size(unsigned long mem_size);
856f64e8084SAneesh Kumar K.V 
8573ba150fbSGanesh Goudar enum slb_index {
8583ba150fbSGanesh Goudar 	LINEAR_INDEX	= 0, /* Kernel linear map  (0xc000000000000000) */
8593ba150fbSGanesh Goudar 	KSTACK_INDEX	= 1, /* Kernel stack map */
8603ba150fbSGanesh Goudar };
861f64e8084SAneesh Kumar K.V 
8623ba150fbSGanesh Goudar #define slb_esid_mask(ssize)	\
8633ba150fbSGanesh Goudar 	(((ssize) == MMU_SEGSIZE_256M) ? ESID_MASK : ESID_MASK_1T)
8643ba150fbSGanesh Goudar 
mk_esid_data(unsigned long ea,int ssize,enum slb_index index)8653ba150fbSGanesh Goudar static inline unsigned long mk_esid_data(unsigned long ea, int ssize,
8663ba150fbSGanesh Goudar 					 enum slb_index index)
8673ba150fbSGanesh Goudar {
8683ba150fbSGanesh Goudar 	return (ea & slb_esid_mask(ssize)) | SLB_ESID_V | index;
8693ba150fbSGanesh Goudar }
8703ba150fbSGanesh Goudar 
__mk_vsid_data(unsigned long vsid,int ssize,unsigned long flags)8713ba150fbSGanesh Goudar static inline unsigned long __mk_vsid_data(unsigned long vsid, int ssize,
8723ba150fbSGanesh Goudar 					   unsigned long flags)
8733ba150fbSGanesh Goudar {
8743ba150fbSGanesh Goudar 	return (vsid << slb_vsid_shift(ssize)) | flags |
8753ba150fbSGanesh Goudar 		((unsigned long)ssize << SLB_VSID_SSIZE_SHIFT);
8763ba150fbSGanesh Goudar }
8773ba150fbSGanesh Goudar 
mk_vsid_data(unsigned long ea,int ssize,unsigned long flags)8783ba150fbSGanesh Goudar static inline unsigned long mk_vsid_data(unsigned long ea, int ssize,
8793ba150fbSGanesh Goudar 					 unsigned long flags)
8803ba150fbSGanesh Goudar {
8813ba150fbSGanesh Goudar 	return __mk_vsid_data(get_kernel_vsid(ea, ssize), ssize, flags);
8823ba150fbSGanesh Goudar }
8833ba150fbSGanesh Goudar 
8843ba150fbSGanesh Goudar #endif /* __ASSEMBLY__ */
88511a6f6abSAneesh Kumar K.V #endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */
886