xref: /linux/arch/powerpc/include/asm/book3s/64/mmu-hash.h (revision d182b8fd6084412963cdb1a16d04c2f07234e82b)
111a6f6abSAneesh Kumar K.V #ifndef _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
211a6f6abSAneesh Kumar K.V #define _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_
38d2169e8SDavid Gibson /*
48d2169e8SDavid Gibson  * PowerPC64 memory management structures
58d2169e8SDavid Gibson  *
68d2169e8SDavid Gibson  * Dave Engebretsen & Mike Corrigan <{engebret|mikejc}@us.ibm.com>
78d2169e8SDavid Gibson  *   PPC64 rework.
88d2169e8SDavid Gibson  *
98d2169e8SDavid Gibson  * This program is free software; you can redistribute it and/or
108d2169e8SDavid Gibson  * modify it under the terms of the GNU General Public License
118d2169e8SDavid Gibson  * as published by the Free Software Foundation; either version
128d2169e8SDavid Gibson  * 2 of the License, or (at your option) any later version.
138d2169e8SDavid Gibson  */
148d2169e8SDavid Gibson 
158d2169e8SDavid Gibson #include <asm/asm-compat.h>
168d2169e8SDavid Gibson #include <asm/page.h>
17891121e6SAneesh Kumar K.V #include <asm/bug.h>
188d2169e8SDavid Gibson 
198d2169e8SDavid Gibson /*
2078f1dbdeSAneesh Kumar K.V  * This is necessary to get the definition of PGTABLE_RANGE which we
2178f1dbdeSAneesh Kumar K.V  * need for various slices related matters. Note that this isn't the
2278f1dbdeSAneesh Kumar K.V  * complete pgtable.h but only a portion of it.
2378f1dbdeSAneesh Kumar K.V  */
243dfcb315SAneesh Kumar K.V #include <asm/book3s/64/pgtable.h>
25cf9427b8SAneesh Kumar K.V #include <asm/bug.h>
26dad6f37cSAneesh Kumar K.V #include <asm/processor.h>
27b92a226eSKevin Hao #include <asm/cpu_has_feature.h>
2878f1dbdeSAneesh Kumar K.V 
2978f1dbdeSAneesh Kumar K.V /*
308d2169e8SDavid Gibson  * SLB
318d2169e8SDavid Gibson  */
328d2169e8SDavid Gibson 
338d2169e8SDavid Gibson #define SLB_NUM_BOLTED		3
348d2169e8SDavid Gibson #define SLB_CACHE_ENTRIES	8
3546db2f86SBrian King #define SLB_MIN_SIZE		32
368d2169e8SDavid Gibson 
378d2169e8SDavid Gibson /* Bits in the SLB ESID word */
388d2169e8SDavid Gibson #define SLB_ESID_V		ASM_CONST(0x0000000008000000) /* valid */
398d2169e8SDavid Gibson 
408d2169e8SDavid Gibson /* Bits in the SLB VSID word */
418d2169e8SDavid Gibson #define SLB_VSID_SHIFT		12
42e6f81a92SAneesh Kumar K.V #define SLB_VSID_SHIFT_256M	SLB_VSID_SHIFT
431189be65SPaul Mackerras #define SLB_VSID_SHIFT_1T	24
441189be65SPaul Mackerras #define SLB_VSID_SSIZE_SHIFT	62
458d2169e8SDavid Gibson #define SLB_VSID_B		ASM_CONST(0xc000000000000000)
468d2169e8SDavid Gibson #define SLB_VSID_B_256M		ASM_CONST(0x0000000000000000)
478d2169e8SDavid Gibson #define SLB_VSID_B_1T		ASM_CONST(0x4000000000000000)
488d2169e8SDavid Gibson #define SLB_VSID_KS		ASM_CONST(0x0000000000000800)
498d2169e8SDavid Gibson #define SLB_VSID_KP		ASM_CONST(0x0000000000000400)
508d2169e8SDavid Gibson #define SLB_VSID_N		ASM_CONST(0x0000000000000200) /* no-execute */
518d2169e8SDavid Gibson #define SLB_VSID_L		ASM_CONST(0x0000000000000100)
528d2169e8SDavid Gibson #define SLB_VSID_C		ASM_CONST(0x0000000000000080) /* class */
538d2169e8SDavid Gibson #define SLB_VSID_LP		ASM_CONST(0x0000000000000030)
548d2169e8SDavid Gibson #define SLB_VSID_LP_00		ASM_CONST(0x0000000000000000)
558d2169e8SDavid Gibson #define SLB_VSID_LP_01		ASM_CONST(0x0000000000000010)
568d2169e8SDavid Gibson #define SLB_VSID_LP_10		ASM_CONST(0x0000000000000020)
578d2169e8SDavid Gibson #define SLB_VSID_LP_11		ASM_CONST(0x0000000000000030)
588d2169e8SDavid Gibson #define SLB_VSID_LLP		(SLB_VSID_L|SLB_VSID_LP)
598d2169e8SDavid Gibson 
608d2169e8SDavid Gibson #define SLB_VSID_KERNEL		(SLB_VSID_KP)
618d2169e8SDavid Gibson #define SLB_VSID_USER		(SLB_VSID_KP|SLB_VSID_KS|SLB_VSID_C)
628d2169e8SDavid Gibson 
638d2169e8SDavid Gibson #define SLBIE_C			(0x08000000)
641189be65SPaul Mackerras #define SLBIE_SSIZE_SHIFT	25
658d2169e8SDavid Gibson 
668d2169e8SDavid Gibson /*
678d2169e8SDavid Gibson  * Hash table
688d2169e8SDavid Gibson  */
698d2169e8SDavid Gibson 
708d2169e8SDavid Gibson #define HPTES_PER_GROUP 8
718d2169e8SDavid Gibson 
722454c7e9SPaul Mackerras #define HPTE_V_SSIZE_SHIFT	62
738d2169e8SDavid Gibson #define HPTE_V_AVPN_SHIFT	7
746b243fcfSPaul Mackerras #define HPTE_V_COMMON_BITS	ASM_CONST(0x000fffffffffffff)
752454c7e9SPaul Mackerras #define HPTE_V_AVPN		ASM_CONST(0x3fffffffffffff80)
766b243fcfSPaul Mackerras #define HPTE_V_AVPN_3_0		ASM_CONST(0x000fffffffffff80)
778d2169e8SDavid Gibson #define HPTE_V_AVPN_VAL(x)	(((x) & HPTE_V_AVPN) >> HPTE_V_AVPN_SHIFT)
7891bbbe22SGeert Uytterhoeven #define HPTE_V_COMPARE(x,y)	(!(((x) ^ (y)) & 0xffffffffffffff80UL))
798d2169e8SDavid Gibson #define HPTE_V_BOLTED		ASM_CONST(0x0000000000000010)
808d2169e8SDavid Gibson #define HPTE_V_LOCK		ASM_CONST(0x0000000000000008)
818d2169e8SDavid Gibson #define HPTE_V_LARGE		ASM_CONST(0x0000000000000004)
828d2169e8SDavid Gibson #define HPTE_V_SECONDARY	ASM_CONST(0x0000000000000002)
838d2169e8SDavid Gibson #define HPTE_V_VALID		ASM_CONST(0x0000000000000001)
848d2169e8SDavid Gibson 
8550de596dSAneesh Kumar K.V /*
866b243fcfSPaul Mackerras  * ISA 3.0 has a different HPTE format.
8750de596dSAneesh Kumar K.V  */
8850de596dSAneesh Kumar K.V #define HPTE_R_3_0_SSIZE_SHIFT	58
896b243fcfSPaul Mackerras #define HPTE_R_3_0_SSIZE_MASK	(3ull << HPTE_R_3_0_SSIZE_SHIFT)
908d2169e8SDavid Gibson #define HPTE_R_PP0		ASM_CONST(0x8000000000000000)
918d2169e8SDavid Gibson #define HPTE_R_TS		ASM_CONST(0x4000000000000000)
92de56a948SPaul Mackerras #define HPTE_R_KEY_HI		ASM_CONST(0x3000000000000000)
938d2169e8SDavid Gibson #define HPTE_R_RPN_SHIFT	12
94de56a948SPaul Mackerras #define HPTE_R_RPN		ASM_CONST(0x0ffffffffffff000)
956b243fcfSPaul Mackerras #define HPTE_R_RPN_3_0		ASM_CONST(0x01fffffffffff000)
968d2169e8SDavid Gibson #define HPTE_R_PP		ASM_CONST(0x0000000000000003)
978550e2faSAneesh Kumar K.V #define HPTE_R_PPP		ASM_CONST(0x8000000000000003)
988d2169e8SDavid Gibson #define HPTE_R_N		ASM_CONST(0x0000000000000004)
99de56a948SPaul Mackerras #define HPTE_R_G		ASM_CONST(0x0000000000000008)
100de56a948SPaul Mackerras #define HPTE_R_M		ASM_CONST(0x0000000000000010)
101de56a948SPaul Mackerras #define HPTE_R_I		ASM_CONST(0x0000000000000020)
102de56a948SPaul Mackerras #define HPTE_R_W		ASM_CONST(0x0000000000000040)
103de56a948SPaul Mackerras #define HPTE_R_WIMG		ASM_CONST(0x0000000000000078)
1048d2169e8SDavid Gibson #define HPTE_R_C		ASM_CONST(0x0000000000000080)
1058d2169e8SDavid Gibson #define HPTE_R_R		ASM_CONST(0x0000000000000100)
106de56a948SPaul Mackerras #define HPTE_R_KEY_LO		ASM_CONST(0x0000000000000e00)
107*d182b8fdSRam Pai #define HPTE_R_KEY		(HPTE_R_KEY_LO | HPTE_R_KEY_HI)
1088d2169e8SDavid Gibson 
109b7abc5c5SSachin P. Sant #define HPTE_V_1TB_SEG		ASM_CONST(0x4000000000000000)
110b7abc5c5SSachin P. Sant #define HPTE_V_VRMA_MASK	ASM_CONST(0x4001ffffff000000)
111b7abc5c5SSachin P. Sant 
1128d2169e8SDavid Gibson /* Values for PP (assumes Ks=0, Kp=1) */
1138d2169e8SDavid Gibson #define PP_RWXX	0	/* Supervisor read/write, User none */
1148d2169e8SDavid Gibson #define PP_RWRX 1	/* Supervisor read/write, User read */
1158d2169e8SDavid Gibson #define PP_RWRW 2	/* Supervisor read/write, User read/write */
1168d2169e8SDavid Gibson #define PP_RXRX 3	/* Supervisor read,       User read */
117697d3899SPaul Mackerras #define PP_RXXX	(HPTE_R_PP0 | 2)	/* Supervisor read, user none */
1188d2169e8SDavid Gibson 
119b4072df4SPaul Mackerras /* Fields for tlbiel instruction in architecture 2.06 */
120b4072df4SPaul Mackerras #define TLBIEL_INVAL_SEL_MASK	0xc00	/* invalidation selector */
121b4072df4SPaul Mackerras #define  TLBIEL_INVAL_PAGE	0x000	/* invalidate a single page */
122b4072df4SPaul Mackerras #define  TLBIEL_INVAL_SET_LPID	0x800	/* invalidate a set for current LPID */
123b4072df4SPaul Mackerras #define  TLBIEL_INVAL_SET	0xc00	/* invalidate a set for all LPIDs */
124b4072df4SPaul Mackerras #define TLBIEL_INVAL_SET_MASK	0xfff000	/* set number to inval. */
125b4072df4SPaul Mackerras #define TLBIEL_INVAL_SET_SHIFT	12
126b4072df4SPaul Mackerras 
127b4072df4SPaul Mackerras #define POWER7_TLB_SETS		128	/* # sets in POWER7 TLB */
12845706bb5SMahesh Salgaonkar #define POWER8_TLB_SETS		512	/* # sets in POWER8 TLB */
129c3ab300eSMichael Neuling #define POWER9_TLB_SETS_HASH	256	/* # sets in POWER9 TLB Hash mode */
1301a472c9dSAneesh Kumar K.V #define POWER9_TLB_SETS_RADIX	128	/* # sets in POWER9 TLB Radix mode */
131b4072df4SPaul Mackerras 
1328d2169e8SDavid Gibson #ifndef __ASSEMBLY__
1338d2169e8SDavid Gibson 
1347025776eSBenjamin Herrenschmidt struct mmu_hash_ops {
1357025776eSBenjamin Herrenschmidt 	void            (*hpte_invalidate)(unsigned long slot,
1367025776eSBenjamin Herrenschmidt 					   unsigned long vpn,
1377025776eSBenjamin Herrenschmidt 					   int bpsize, int apsize,
1387025776eSBenjamin Herrenschmidt 					   int ssize, int local);
1397025776eSBenjamin Herrenschmidt 	long		(*hpte_updatepp)(unsigned long slot,
1407025776eSBenjamin Herrenschmidt 					 unsigned long newpp,
1417025776eSBenjamin Herrenschmidt 					 unsigned long vpn,
1427025776eSBenjamin Herrenschmidt 					 int bpsize, int apsize,
1437025776eSBenjamin Herrenschmidt 					 int ssize, unsigned long flags);
1447025776eSBenjamin Herrenschmidt 	void            (*hpte_updateboltedpp)(unsigned long newpp,
1457025776eSBenjamin Herrenschmidt 					       unsigned long ea,
1467025776eSBenjamin Herrenschmidt 					       int psize, int ssize);
1477025776eSBenjamin Herrenschmidt 	long		(*hpte_insert)(unsigned long hpte_group,
1487025776eSBenjamin Herrenschmidt 				       unsigned long vpn,
1497025776eSBenjamin Herrenschmidt 				       unsigned long prpn,
1507025776eSBenjamin Herrenschmidt 				       unsigned long rflags,
1517025776eSBenjamin Herrenschmidt 				       unsigned long vflags,
1527025776eSBenjamin Herrenschmidt 				       int psize, int apsize,
1537025776eSBenjamin Herrenschmidt 				       int ssize);
1547025776eSBenjamin Herrenschmidt 	long		(*hpte_remove)(unsigned long hpte_group);
1557025776eSBenjamin Herrenschmidt 	int             (*hpte_removebolted)(unsigned long ea,
1567025776eSBenjamin Herrenschmidt 					     int psize, int ssize);
1577025776eSBenjamin Herrenschmidt 	void		(*flush_hash_range)(unsigned long number, int local);
1587025776eSBenjamin Herrenschmidt 	void		(*hugepage_invalidate)(unsigned long vsid,
1597025776eSBenjamin Herrenschmidt 					       unsigned long addr,
1607025776eSBenjamin Herrenschmidt 					       unsigned char *hpte_slot_array,
1617025776eSBenjamin Herrenschmidt 					       int psize, int ssize, int local);
162dbcf929cSDavid Gibson 	int		(*resize_hpt)(unsigned long shift);
1637025776eSBenjamin Herrenschmidt 	/*
1647025776eSBenjamin Herrenschmidt 	 * Special for kexec.
1657025776eSBenjamin Herrenschmidt 	 * To be called in real mode with interrupts disabled. No locks are
1667025776eSBenjamin Herrenschmidt 	 * taken as such, concurrent access on pre POWER5 hardware could result
1677025776eSBenjamin Herrenschmidt 	 * in a deadlock.
1687025776eSBenjamin Herrenschmidt 	 * The linear mapping is destroyed as well.
1697025776eSBenjamin Herrenschmidt 	 */
1707025776eSBenjamin Herrenschmidt 	void		(*hpte_clear_all)(void);
1717025776eSBenjamin Herrenschmidt };
1727025776eSBenjamin Herrenschmidt extern struct mmu_hash_ops mmu_hash_ops;
1737025776eSBenjamin Herrenschmidt 
1748e561e7eSDavid Gibson struct hash_pte {
17512f04f2bSAnton Blanchard 	__be64 v;
17612f04f2bSAnton Blanchard 	__be64 r;
1778e561e7eSDavid Gibson };
1788d2169e8SDavid Gibson 
1798e561e7eSDavid Gibson extern struct hash_pte *htab_address;
1808d2169e8SDavid Gibson extern unsigned long htab_size_bytes;
1818d2169e8SDavid Gibson extern unsigned long htab_hash_mask;
1828d2169e8SDavid Gibson 
183cf9427b8SAneesh Kumar K.V 
184cf9427b8SAneesh Kumar K.V static inline int shift_to_mmu_psize(unsigned int shift)
185cf9427b8SAneesh Kumar K.V {
186cf9427b8SAneesh Kumar K.V 	int psize;
187cf9427b8SAneesh Kumar K.V 
188cf9427b8SAneesh Kumar K.V 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
189cf9427b8SAneesh Kumar K.V 		if (mmu_psize_defs[psize].shift == shift)
190cf9427b8SAneesh Kumar K.V 			return psize;
191cf9427b8SAneesh Kumar K.V 	return -1;
192cf9427b8SAneesh Kumar K.V }
193cf9427b8SAneesh Kumar K.V 
194cf9427b8SAneesh Kumar K.V static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
195cf9427b8SAneesh Kumar K.V {
196cf9427b8SAneesh Kumar K.V 	if (mmu_psize_defs[mmu_psize].shift)
197cf9427b8SAneesh Kumar K.V 		return mmu_psize_defs[mmu_psize].shift;
198cf9427b8SAneesh Kumar K.V 	BUG();
199cf9427b8SAneesh Kumar K.V }
2008d2169e8SDavid Gibson 
201138ee7eeSAneesh Kumar K.V static inline unsigned long get_sllp_encoding(int psize)
202138ee7eeSAneesh Kumar K.V {
203138ee7eeSAneesh Kumar K.V 	unsigned long sllp;
204138ee7eeSAneesh Kumar K.V 
205138ee7eeSAneesh Kumar K.V 	sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
206138ee7eeSAneesh Kumar K.V 		((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
207138ee7eeSAneesh Kumar K.V 	return sllp;
208138ee7eeSAneesh Kumar K.V }
209138ee7eeSAneesh Kumar K.V 
2108d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
2118d2169e8SDavid Gibson 
2128d2169e8SDavid Gibson /*
2132454c7e9SPaul Mackerras  * Segment sizes.
2142454c7e9SPaul Mackerras  * These are the values used by hardware in the B field of
2152454c7e9SPaul Mackerras  * SLB entries and the first dword of MMU hashtable entries.
2162454c7e9SPaul Mackerras  * The B field is 2 bits; the values 2 and 3 are unused and reserved.
2172454c7e9SPaul Mackerras  */
2182454c7e9SPaul Mackerras #define MMU_SEGSIZE_256M	0
2192454c7e9SPaul Mackerras #define MMU_SEGSIZE_1T		1
2202454c7e9SPaul Mackerras 
2215524a27dSAneesh Kumar K.V /*
2225524a27dSAneesh Kumar K.V  * encode page number shift.
2235524a27dSAneesh Kumar K.V  * in order to fit the 78 bit va in a 64 bit variable we shift the va by
2245524a27dSAneesh Kumar K.V  * 12 bits. This enable us to address upto 76 bit va.
2255524a27dSAneesh Kumar K.V  * For hpt hash from a va we can ignore the page size bits of va and for
2265524a27dSAneesh Kumar K.V  * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
2275524a27dSAneesh Kumar K.V  * we work in all cases including 4k page size.
2285524a27dSAneesh Kumar K.V  */
2295524a27dSAneesh Kumar K.V #define VPN_SHIFT	12
2301189be65SPaul Mackerras 
231b1022fbdSAneesh Kumar K.V /*
232b1022fbdSAneesh Kumar K.V  * HPTE Large Page (LP) details
233b1022fbdSAneesh Kumar K.V  */
234b1022fbdSAneesh Kumar K.V #define LP_SHIFT	12
235b1022fbdSAneesh Kumar K.V #define LP_BITS		8
236b1022fbdSAneesh Kumar K.V #define LP_MASK(i)	((0xFF >> (i)) << LP_SHIFT)
237b1022fbdSAneesh Kumar K.V 
2388d2169e8SDavid Gibson #ifndef __ASSEMBLY__
2398d2169e8SDavid Gibson 
24073d16a6eSIan Munsie static inline int slb_vsid_shift(int ssize)
24173d16a6eSIan Munsie {
24273d16a6eSIan Munsie 	if (ssize == MMU_SEGSIZE_256M)
24373d16a6eSIan Munsie 		return SLB_VSID_SHIFT;
24473d16a6eSIan Munsie 	return SLB_VSID_SHIFT_1T;
24573d16a6eSIan Munsie }
24673d16a6eSIan Munsie 
2475524a27dSAneesh Kumar K.V static inline int segment_shift(int ssize)
2485524a27dSAneesh Kumar K.V {
2495524a27dSAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M)
2505524a27dSAneesh Kumar K.V 		return SID_SHIFT;
2515524a27dSAneesh Kumar K.V 	return SID_SHIFT_1T;
2525524a27dSAneesh Kumar K.V }
2535524a27dSAneesh Kumar K.V 
2548d2169e8SDavid Gibson /*
2550eeede0cSPaul Mackerras  * This array is indexed by the LP field of the HPTE second dword.
2560eeede0cSPaul Mackerras  * Since this field may contain some RPN bits, some entries are
2570eeede0cSPaul Mackerras  * replicated so that we get the same value irrespective of RPN.
2580eeede0cSPaul Mackerras  * The top 4 bits are the page size index (MMU_PAGE_*) for the
2590eeede0cSPaul Mackerras  * actual page size, the bottom 4 bits are the base page size.
2600eeede0cSPaul Mackerras  */
2610eeede0cSPaul Mackerras extern u8 hpte_page_sizes[1 << LP_BITS];
2620eeede0cSPaul Mackerras 
2630eeede0cSPaul Mackerras static inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
2640eeede0cSPaul Mackerras 					     bool is_base_size)
2650eeede0cSPaul Mackerras {
2660eeede0cSPaul Mackerras 	unsigned int i, lp;
2670eeede0cSPaul Mackerras 
2680eeede0cSPaul Mackerras 	if (!(h & HPTE_V_LARGE))
2690eeede0cSPaul Mackerras 		return 1ul << 12;
2700eeede0cSPaul Mackerras 
2710eeede0cSPaul Mackerras 	/* Look at the 8 bit LP value */
2720eeede0cSPaul Mackerras 	lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
2730eeede0cSPaul Mackerras 	i = hpte_page_sizes[lp];
2740eeede0cSPaul Mackerras 	if (!i)
2750eeede0cSPaul Mackerras 		return 0;
2760eeede0cSPaul Mackerras 	if (!is_base_size)
2770eeede0cSPaul Mackerras 		i >>= 4;
2780eeede0cSPaul Mackerras 	return 1ul << mmu_psize_defs[i & 0xf].shift;
2790eeede0cSPaul Mackerras }
2800eeede0cSPaul Mackerras 
2810eeede0cSPaul Mackerras static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
2820eeede0cSPaul Mackerras {
2830eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 0);
2840eeede0cSPaul Mackerras }
2850eeede0cSPaul Mackerras 
2860eeede0cSPaul Mackerras static inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
2870eeede0cSPaul Mackerras {
2880eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 1);
2890eeede0cSPaul Mackerras }
2900eeede0cSPaul Mackerras 
2910eeede0cSPaul Mackerras /*
2921189be65SPaul Mackerras  * The current system page and segment sizes
2938d2169e8SDavid Gibson  */
2941189be65SPaul Mackerras extern int mmu_kernel_ssize;
2951189be65SPaul Mackerras extern int mmu_highuser_ssize;
296584f8b71SMichael Neuling extern u16 mmu_slb_size;
297572fb578SMichael Ellerman extern unsigned long tce_alloc_start, tce_alloc_end;
2988d2169e8SDavid Gibson 
2998d2169e8SDavid Gibson /*
3008d2169e8SDavid Gibson  * If the processor supports 64k normal pages but not 64k cache
3018d2169e8SDavid Gibson  * inhibited pages, we have to be prepared to switch processes
3028d2169e8SDavid Gibson  * to use 4k pages when they create cache-inhibited mappings.
3038d2169e8SDavid Gibson  * If this is the case, mmu_ci_restrictions will be set to 1.
3048d2169e8SDavid Gibson  */
3058d2169e8SDavid Gibson extern int mmu_ci_restrictions;
3068d2169e8SDavid Gibson 
3078d2169e8SDavid Gibson /*
3085524a27dSAneesh Kumar K.V  * This computes the AVPN and B fields of the first dword of a HPTE,
3095524a27dSAneesh Kumar K.V  * for use when we want to match an existing PTE.  The bottom 7 bits
3105524a27dSAneesh Kumar K.V  * of the returned value are zero.
3118d2169e8SDavid Gibson  */
3125524a27dSAneesh Kumar K.V static inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
3131189be65SPaul Mackerras 					     int ssize)
3148d2169e8SDavid Gibson {
3151189be65SPaul Mackerras 	unsigned long v;
3165524a27dSAneesh Kumar K.V 	/*
3175524a27dSAneesh Kumar K.V 	 * The AVA field omits the low-order 23 bits of the 78 bits VA.
3185524a27dSAneesh Kumar K.V 	 * These bits are not needed in the PTE, because the
3195524a27dSAneesh Kumar K.V 	 * low-order b of these bits are part of the byte offset
3205524a27dSAneesh Kumar K.V 	 * into the virtual page and, if b < 23, the high-order
3215524a27dSAneesh Kumar K.V 	 * 23-b of these bits are always used in selecting the
3225524a27dSAneesh Kumar K.V 	 * PTEGs to be searched
3235524a27dSAneesh Kumar K.V 	 */
3245524a27dSAneesh Kumar K.V 	v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
3258d2169e8SDavid Gibson 	v <<= HPTE_V_AVPN_SHIFT;
3265524a27dSAneesh Kumar K.V 	v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
3275524a27dSAneesh Kumar K.V 	return v;
3285524a27dSAneesh Kumar K.V }
3295524a27dSAneesh Kumar K.V 
3305524a27dSAneesh Kumar K.V /*
3316b243fcfSPaul Mackerras  * ISA v3.0 defines a new HPTE format, which differs from the old
3326b243fcfSPaul Mackerras  * format in having smaller AVPN and ARPN fields, and the B field
3336b243fcfSPaul Mackerras  * in the second dword instead of the first.
3346b243fcfSPaul Mackerras  */
3356b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_v(unsigned long v)
3366b243fcfSPaul Mackerras {
3376b243fcfSPaul Mackerras 	/* trim AVPN, drop B */
3386b243fcfSPaul Mackerras 	return v & HPTE_V_COMMON_BITS;
3396b243fcfSPaul Mackerras }
3406b243fcfSPaul Mackerras 
3416b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
3426b243fcfSPaul Mackerras {
3436b243fcfSPaul Mackerras 	/* move B field from 1st to 2nd dword, trim ARPN */
3446b243fcfSPaul Mackerras 	return (r & ~HPTE_R_3_0_SSIZE_MASK) |
3456b243fcfSPaul Mackerras 		(((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
3466b243fcfSPaul Mackerras }
3476b243fcfSPaul Mackerras 
3486b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
3496b243fcfSPaul Mackerras {
3506b243fcfSPaul Mackerras 	/* insert B field */
3516b243fcfSPaul Mackerras 	return (v & HPTE_V_COMMON_BITS) |
3526b243fcfSPaul Mackerras 		((r & HPTE_R_3_0_SSIZE_MASK) <<
3536b243fcfSPaul Mackerras 		 (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
3546b243fcfSPaul Mackerras }
3556b243fcfSPaul Mackerras 
3566b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_r(unsigned long r)
3576b243fcfSPaul Mackerras {
3586b243fcfSPaul Mackerras 	/* clear out B field */
3596b243fcfSPaul Mackerras 	return r & ~HPTE_R_3_0_SSIZE_MASK;
3606b243fcfSPaul Mackerras }
3616b243fcfSPaul Mackerras 
3626b243fcfSPaul Mackerras /*
3635524a27dSAneesh Kumar K.V  * This function sets the AVPN and L fields of the HPTE  appropriately
364b1022fbdSAneesh Kumar K.V  * using the base page size and actual page size.
3655524a27dSAneesh Kumar K.V  */
366b1022fbdSAneesh Kumar K.V static inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
367b1022fbdSAneesh Kumar K.V 					  int actual_psize, int ssize)
3685524a27dSAneesh Kumar K.V {
3695524a27dSAneesh Kumar K.V 	unsigned long v;
370b1022fbdSAneesh Kumar K.V 	v = hpte_encode_avpn(vpn, base_psize, ssize);
371b1022fbdSAneesh Kumar K.V 	if (actual_psize != MMU_PAGE_4K)
3728d2169e8SDavid Gibson 		v |= HPTE_V_LARGE;
3738d2169e8SDavid Gibson 	return v;
3748d2169e8SDavid Gibson }
3758d2169e8SDavid Gibson 
3768d2169e8SDavid Gibson /*
3778d2169e8SDavid Gibson  * This function sets the ARPN, and LP fields of the HPTE appropriately
3788d2169e8SDavid Gibson  * for the page size. We assume the pa is already "clean" that is properly
3798d2169e8SDavid Gibson  * aligned for the requested page size
3808d2169e8SDavid Gibson  */
381b1022fbdSAneesh Kumar K.V static inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
3826b243fcfSPaul Mackerras 					  int actual_psize)
3838d2169e8SDavid Gibson {
3848d2169e8SDavid Gibson 	/* A 4K page needs no special encoding */
385b1022fbdSAneesh Kumar K.V 	if (actual_psize == MMU_PAGE_4K)
3868d2169e8SDavid Gibson 		return pa & HPTE_R_RPN;
3878d2169e8SDavid Gibson 	else {
388b1022fbdSAneesh Kumar K.V 		unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
389b1022fbdSAneesh Kumar K.V 		unsigned int shift = mmu_psize_defs[actual_psize].shift;
390b1022fbdSAneesh Kumar K.V 		return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
3918d2169e8SDavid Gibson 	}
3928d2169e8SDavid Gibson }
3938d2169e8SDavid Gibson 
3948d2169e8SDavid Gibson /*
3955524a27dSAneesh Kumar K.V  * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
3961189be65SPaul Mackerras  */
3975524a27dSAneesh Kumar K.V static inline unsigned long hpt_vpn(unsigned long ea,
3985524a27dSAneesh Kumar K.V 				    unsigned long vsid, int ssize)
3991189be65SPaul Mackerras {
4005524a27dSAneesh Kumar K.V 	unsigned long mask;
4015524a27dSAneesh Kumar K.V 	int s_shift = segment_shift(ssize);
4025524a27dSAneesh Kumar K.V 
4035524a27dSAneesh Kumar K.V 	mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
4045524a27dSAneesh Kumar K.V 	return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
4051189be65SPaul Mackerras }
4061189be65SPaul Mackerras 
4071189be65SPaul Mackerras /*
4081189be65SPaul Mackerras  * This hashes a virtual address
4098d2169e8SDavid Gibson  */
4105524a27dSAneesh Kumar K.V static inline unsigned long hpt_hash(unsigned long vpn,
4115524a27dSAneesh Kumar K.V 				     unsigned int shift, int ssize)
4128d2169e8SDavid Gibson {
41359248aecSAneesh Kumar K.V 	unsigned long mask;
4141189be65SPaul Mackerras 	unsigned long hash, vsid;
4151189be65SPaul Mackerras 
4165524a27dSAneesh Kumar K.V 	/* VPN_SHIFT can be atmost 12 */
4171189be65SPaul Mackerras 	if (ssize == MMU_SEGSIZE_256M) {
4185524a27dSAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
4195524a27dSAneesh Kumar K.V 		hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
4205524a27dSAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT));
4211189be65SPaul Mackerras 	} else {
4225524a27dSAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
4235524a27dSAneesh Kumar K.V 		vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
4245524a27dSAneesh Kumar K.V 		hash = vsid ^ (vsid << 25) ^
4255524a27dSAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT)) ;
4261189be65SPaul Mackerras 	}
4271189be65SPaul Mackerras 	return hash & 0x7fffffffffUL;
4288d2169e8SDavid Gibson }
4298d2169e8SDavid Gibson 
430aefa5688SAneesh Kumar K.V #define HPTE_LOCAL_UPDATE	0x1
431aefa5688SAneesh Kumar K.V #define HPTE_NOHPTE_UPDATE	0x2
432aefa5688SAneesh Kumar K.V 
4338d2169e8SDavid Gibson extern int __hash_page_4K(unsigned long ea, unsigned long access,
4348d2169e8SDavid Gibson 			  unsigned long vsid, pte_t *ptep, unsigned long trap,
435aefa5688SAneesh Kumar K.V 			  unsigned long flags, int ssize, int subpage_prot);
4368d2169e8SDavid Gibson extern int __hash_page_64K(unsigned long ea, unsigned long access,
4378d2169e8SDavid Gibson 			   unsigned long vsid, pte_t *ptep, unsigned long trap,
438aefa5688SAneesh Kumar K.V 			   unsigned long flags, int ssize);
4398d2169e8SDavid Gibson struct mm_struct;
4400895ecdaSDavid Gibson unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
441aefa5688SAneesh Kumar K.V extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
442aefa5688SAneesh Kumar K.V 			unsigned long access, unsigned long trap,
443aefa5688SAneesh Kumar K.V 			unsigned long flags);
444aefa5688SAneesh Kumar K.V extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
445aefa5688SAneesh Kumar K.V 		     unsigned long dsisr);
446a4fe3ce7SDavid Gibson int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
447aefa5688SAneesh Kumar K.V 		     pte_t *ptep, unsigned long trap, unsigned long flags,
448aefa5688SAneesh Kumar K.V 		     int ssize, unsigned int shift, unsigned int mmu_psize);
4496d492eccSAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4506d492eccSAneesh Kumar K.V extern int __hash_page_thp(unsigned long ea, unsigned long access,
4516d492eccSAneesh Kumar K.V 			   unsigned long vsid, pmd_t *pmdp, unsigned long trap,
452aefa5688SAneesh Kumar K.V 			   unsigned long flags, int ssize, unsigned int psize);
4536d492eccSAneesh Kumar K.V #else
4546d492eccSAneesh Kumar K.V static inline int __hash_page_thp(unsigned long ea, unsigned long access,
4556d492eccSAneesh Kumar K.V 				  unsigned long vsid, pmd_t *pmdp,
456aefa5688SAneesh Kumar K.V 				  unsigned long trap, unsigned long flags,
4576d492eccSAneesh Kumar K.V 				  int ssize, unsigned int psize)
4586d492eccSAneesh Kumar K.V {
4596d492eccSAneesh Kumar K.V 	BUG();
460ff1e7683SNathan Fontenot 	return -1;
4616d492eccSAneesh Kumar K.V }
4626d492eccSAneesh Kumar K.V #endif
4634b8692c0SBenjamin Herrenschmidt extern void hash_failure_debug(unsigned long ea, unsigned long access,
4644b8692c0SBenjamin Herrenschmidt 			       unsigned long vsid, unsigned long trap,
465d8139ebfSAneesh Kumar K.V 			       int ssize, int psize, int lpsize,
466d8139ebfSAneesh Kumar K.V 			       unsigned long pte);
4678d2169e8SDavid Gibson extern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
468bc033b63SBenjamin Herrenschmidt 			     unsigned long pstart, unsigned long prot,
4691189be65SPaul Mackerras 			     int psize, int ssize);
470f6026df1SAnton Blanchard int htab_remove_mapping(unsigned long vstart, unsigned long vend,
471f6026df1SAnton Blanchard 			int psize, int ssize);
47241151e77SBecky Bruce extern void add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
473fa28237cSPaul Mackerras extern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
4748d2169e8SDavid Gibson 
4756364e84eSMichael Ellerman #ifdef CONFIG_PPC_PSERIES
4766364e84eSMichael Ellerman void hpte_init_pseries(void);
4776364e84eSMichael Ellerman #else
4786364e84eSMichael Ellerman static inline void hpte_init_pseries(void) { }
4796364e84eSMichael Ellerman #endif
4806364e84eSMichael Ellerman 
4818d2169e8SDavid Gibson extern void hpte_init_native(void);
4828d2169e8SDavid Gibson 
4838d2169e8SDavid Gibson extern void slb_initialize(void);
4848d2169e8SDavid Gibson extern void slb_flush_and_rebolt(void);
4858d2169e8SDavid Gibson 
48667439b76SMichael Neuling extern void slb_vmalloc_update(void);
48746db2f86SBrian King extern void slb_set_size(u16 size);
4888d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
4898d2169e8SDavid Gibson 
4908d2169e8SDavid Gibson /*
491f033d659SAneesh Kumar K.V  * VSID allocation (256MB segment)
4928d2169e8SDavid Gibson  *
493c60ac569SAneesh Kumar K.V  * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
494c60ac569SAneesh Kumar K.V  * from mmu context id and effective segment id of the address.
4958d2169e8SDavid Gibson  *
496941711a3SAneesh Kumar K.V  * For user processes max context id is limited to MAX_USER_CONTEXT.
497941711a3SAneesh Kumar K.V 
498add2e1e5SMichael Ellerman  * For kernel space, we use context ids 1-4 to map addresses as below:
499c60ac569SAneesh Kumar K.V  * NOTE: each context only support 64TB now.
500941711a3SAneesh Kumar K.V  * 0x00001 -  [ 0xc000000000000000 - 0xc0003fffffffffff ]
501941711a3SAneesh Kumar K.V  * 0x00002 -  [ 0xd000000000000000 - 0xd0003fffffffffff ]
502941711a3SAneesh Kumar K.V  * 0x00003 -  [ 0xe000000000000000 - 0xe0003fffffffffff ]
503941711a3SAneesh Kumar K.V  * 0x00004 -  [ 0xf000000000000000 - 0xf0003fffffffffff ]
5048d2169e8SDavid Gibson  *
5058d2169e8SDavid Gibson  * The proto-VSIDs are then scrambled into real VSIDs with the
5068d2169e8SDavid Gibson  * multiplicative hash:
5078d2169e8SDavid Gibson  *
5088d2169e8SDavid Gibson  *	VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
5098d2169e8SDavid Gibson  *
510f033d659SAneesh Kumar K.V  * VSID_MULTIPLIER is prime, so in particular it is
5118d2169e8SDavid Gibson  * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
5128d2169e8SDavid Gibson  * Because the modulus is 2^n-1 we can compute it efficiently without
513c60ac569SAneesh Kumar K.V  * a divide or extra multiply (see below). The scramble function gives
514c60ac569SAneesh Kumar K.V  * robust scattering in the hash table (at least based on some initial
515c60ac569SAneesh Kumar K.V  * results).
5168d2169e8SDavid Gibson  *
517941711a3SAneesh Kumar K.V  * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
518941711a3SAneesh Kumar K.V  * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
519941711a3SAneesh Kumar K.V  * will produce a VSID of 0.
5208d2169e8SDavid Gibson  *
521c60ac569SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
522c60ac569SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
523941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
5248d2169e8SDavid Gibson  */
5258d2169e8SDavid Gibson 
526e6f81a92SAneesh Kumar K.V /*
527e6f81a92SAneesh Kumar K.V  * Max Va bits we support as of now is 68 bits. We want 19 bit
528e6f81a92SAneesh Kumar K.V  * context ID.
529e6f81a92SAneesh Kumar K.V  * Restrictions:
530e6f81a92SAneesh Kumar K.V  * GPU has restrictions of not able to access beyond 128TB
531e6f81a92SAneesh Kumar K.V  * (47 bit effective address). We also cannot do more than 20bit PID.
532e6f81a92SAneesh Kumar K.V  * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
533e6f81a92SAneesh Kumar K.V  * to 16 bits (ie, we can only have 2^16 pids at the same time).
534e6f81a92SAneesh Kumar K.V  */
535e6f81a92SAneesh Kumar K.V #define VA_BITS			68
536e39d1a47SAneesh Kumar K.V #define CONTEXT_BITS		19
537e6f81a92SAneesh Kumar K.V #define ESID_BITS		(VA_BITS - (SID_SHIFT + CONTEXT_BITS))
538e6f81a92SAneesh Kumar K.V #define ESID_BITS_1T		(VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
539e39d1a47SAneesh Kumar K.V 
54079270e0aSAneesh Kumar K.V #define ESID_BITS_MASK		((1 << ESID_BITS) - 1)
54179270e0aSAneesh Kumar K.V #define ESID_BITS_1T_MASK	((1 << ESID_BITS_1T) - 1)
54279270e0aSAneesh Kumar K.V 
543048ee099SAneesh Kumar K.V /*
544c60ac569SAneesh Kumar K.V  * 256MB segment
545af81d787SAneesh Kumar K.V  * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
546941711a3SAneesh Kumar K.V  * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
547941711a3SAneesh Kumar K.V  * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
548e6f81a92SAneesh Kumar K.V  * context maps 2^49 bytes (512TB).
549941711a3SAneesh Kumar K.V  *
550941711a3SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
551941711a3SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
552941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
553c60ac569SAneesh Kumar K.V  */
554941711a3SAneesh Kumar K.V #define MAX_USER_CONTEXT	((ASM_CONST(1) << CONTEXT_BITS) - 2)
555941711a3SAneesh Kumar K.V #define MIN_USER_CONTEXT	(5)
556941711a3SAneesh Kumar K.V 
557941711a3SAneesh Kumar K.V /* Would be nice to use KERNEL_REGION_ID here */
558941711a3SAneesh Kumar K.V #define KERNEL_REGION_CONTEXT_OFFSET	(0xc - 1)
559c60ac569SAneesh Kumar K.V 
560c60ac569SAneesh Kumar K.V /*
561e6f81a92SAneesh Kumar K.V  * For platforms that support on 65bit VA we limit the context bits
562048ee099SAneesh Kumar K.V  */
563e6f81a92SAneesh Kumar K.V #define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
5648d2169e8SDavid Gibson 
5658d2169e8SDavid Gibson /*
566e6f81a92SAneesh Kumar K.V  * This should be computed such that protovosid * vsid_mulitplier
567e6f81a92SAneesh Kumar K.V  * doesn't overflow 64 bits. The vsid_mutliplier should also be
568e6f81a92SAneesh Kumar K.V  * co-prime to vsid_modulus. We also need to make sure that number
569e6f81a92SAneesh Kumar K.V  * of bits in multiplied result (dividend) is less than twice the number of
570e6f81a92SAneesh Kumar K.V  * protovsid bits for our modulus optmization to work.
5718d2169e8SDavid Gibson  *
572e6f81a92SAneesh Kumar K.V  * The below table shows the current values used.
573e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
574e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
575e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
576e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   25 |         49 |                50 |
577e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
578e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   37 |         61 |                74 |
579e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
5808d2169e8SDavid Gibson  *
581e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
582e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
583e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
584e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   28 |         52 |                 56 |
585e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
586e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   40 |         64 |                 80 |
587e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
588e6f81a92SAneesh Kumar K.V  *
5898d2169e8SDavid Gibson  */
590e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_256M	ASM_CONST(12538073)	/* 24-bit prime */
591e6f81a92SAneesh Kumar K.V #define VSID_BITS_256M		(VA_BITS - SID_SHIFT)
592e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_256M	(65 - SID_SHIFT)
59382228e36SAneesh Kumar K.V /*
59482228e36SAneesh Kumar K.V  * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
59582228e36SAneesh Kumar K.V  */
59682228e36SAneesh Kumar K.V #define VSID_MULINV_256M	ASM_CONST(665548017062)
597e6f81a92SAneesh Kumar K.V 
598e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_1T	ASM_CONST(12538073)	/* 24-bit prime */
599e6f81a92SAneesh Kumar K.V #define VSID_BITS_1T		(VA_BITS - SID_SHIFT_1T)
600e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_1T		(65 - SID_SHIFT_1T)
60182228e36SAneesh Kumar K.V #define VSID_MULINV_1T		ASM_CONST(209034062)
602e6f81a92SAneesh Kumar K.V 
60382228e36SAneesh Kumar K.V /* 1TB VSID reserved for VRMA */
60482228e36SAneesh Kumar K.V #define VRMA_VSID	0x1ffffffUL
605e6f81a92SAneesh Kumar K.V #define USER_VSID_RANGE	(1UL << (ESID_BITS + SID_SHIFT))
6068d2169e8SDavid Gibson 
60778f1dbdeSAneesh Kumar K.V /* 4 bits per slice and we have one slice per 1TB */
608dd1842a2SAneesh Kumar K.V #define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
609957b778aSAneesh Kumar K.V #define TASK_SLICE_ARRAY_SZ(x)	((x)->context.addr_limit >> 41)
6108d2169e8SDavid Gibson 
6118d2169e8SDavid Gibson #ifndef __ASSEMBLY__
6128d2169e8SDavid Gibson 
613d28513bcSDavid Gibson #ifdef CONFIG_PPC_SUBPAGE_PROT
614d28513bcSDavid Gibson /*
615d28513bcSDavid Gibson  * For the sub-page protection option, we extend the PGD with one of
616d28513bcSDavid Gibson  * these.  Basically we have a 3-level tree, with the top level being
617d28513bcSDavid Gibson  * the protptrs array.  To optimize speed and memory consumption when
618d28513bcSDavid Gibson  * only addresses < 4GB are being protected, pointers to the first
619d28513bcSDavid Gibson  * four pages of sub-page protection words are stored in the low_prot
620d28513bcSDavid Gibson  * array.
621d28513bcSDavid Gibson  * Each page of sub-page protection words protects 1GB (4 bytes
622d28513bcSDavid Gibson  * protects 64k).  For the 3-level tree, each page of pointers then
623d28513bcSDavid Gibson  * protects 8TB.
624d28513bcSDavid Gibson  */
625d28513bcSDavid Gibson struct subpage_prot_table {
626d28513bcSDavid Gibson 	unsigned long maxaddr;	/* only addresses < this are protected */
627dad6f37cSAneesh Kumar K.V 	unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
628d28513bcSDavid Gibson 	unsigned int *low_prot[4];
629d28513bcSDavid Gibson };
630d28513bcSDavid Gibson 
631d28513bcSDavid Gibson #define SBP_L1_BITS		(PAGE_SHIFT - 2)
632d28513bcSDavid Gibson #define SBP_L2_BITS		(PAGE_SHIFT - 3)
633d28513bcSDavid Gibson #define SBP_L1_COUNT		(1 << SBP_L1_BITS)
634d28513bcSDavid Gibson #define SBP_L2_COUNT		(1 << SBP_L2_BITS)
635d28513bcSDavid Gibson #define SBP_L2_SHIFT		(PAGE_SHIFT + SBP_L1_BITS)
636d28513bcSDavid Gibson #define SBP_L3_SHIFT		(SBP_L2_SHIFT + SBP_L2_BITS)
637d28513bcSDavid Gibson 
638d28513bcSDavid Gibson extern void subpage_prot_free(struct mm_struct *mm);
639d28513bcSDavid Gibson extern void subpage_prot_init_new_context(struct mm_struct *mm);
640d28513bcSDavid Gibson #else
641d28513bcSDavid Gibson static inline void subpage_prot_free(struct mm_struct *mm) {}
642d28513bcSDavid Gibson static inline void subpage_prot_init_new_context(struct mm_struct *mm) { }
643d28513bcSDavid Gibson #endif /* CONFIG_PPC_SUBPAGE_PROT */
644d28513bcSDavid Gibson 
6458d2169e8SDavid Gibson #if 0
6461189be65SPaul Mackerras /*
6471189be65SPaul Mackerras  * The code below is equivalent to this function for arguments
6488d2169e8SDavid Gibson  * < 2^VSID_BITS, which is all this should ever be called
6498d2169e8SDavid Gibson  * with.  However gcc is not clever enough to compute the
6501189be65SPaul Mackerras  * modulus (2^n-1) without a second multiply.
6511189be65SPaul Mackerras  */
65234692708SAnton Blanchard #define vsid_scramble(protovsid, size) \
6531189be65SPaul Mackerras 	((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
6548d2169e8SDavid Gibson 
655e6f81a92SAneesh Kumar K.V /* simplified form avoiding mod operation */
6561189be65SPaul Mackerras #define vsid_scramble(protovsid, size) \
6571189be65SPaul Mackerras 	({								 \
6581189be65SPaul Mackerras 		unsigned long x;					 \
6591189be65SPaul Mackerras 		x = (protovsid) * VSID_MULTIPLIER_##size;		 \
6601189be65SPaul Mackerras 		x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
6611189be65SPaul Mackerras 		(x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
6621189be65SPaul Mackerras 	})
663e6f81a92SAneesh Kumar K.V 
664e6f81a92SAneesh Kumar K.V #else /* 1 */
665e6f81a92SAneesh Kumar K.V static inline unsigned long vsid_scramble(unsigned long protovsid,
666e6f81a92SAneesh Kumar K.V 				  unsigned long vsid_multiplier, int vsid_bits)
667e6f81a92SAneesh Kumar K.V {
668e6f81a92SAneesh Kumar K.V 	unsigned long vsid;
669e6f81a92SAneesh Kumar K.V 	unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
670e6f81a92SAneesh Kumar K.V 	/*
671e6f81a92SAneesh Kumar K.V 	 * We have same multipler for both 256 and 1T segements now
672e6f81a92SAneesh Kumar K.V 	 */
673e6f81a92SAneesh Kumar K.V 	vsid = protovsid * vsid_multiplier;
674e6f81a92SAneesh Kumar K.V 	vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
675e6f81a92SAneesh Kumar K.V 	return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
676e6f81a92SAneesh Kumar K.V }
677e6f81a92SAneesh Kumar K.V 
6788d2169e8SDavid Gibson #endif /* 1 */
6798d2169e8SDavid Gibson 
6801189be65SPaul Mackerras /* Returns the segment size indicator for a user address */
6811189be65SPaul Mackerras static inline int user_segment_size(unsigned long addr)
6828d2169e8SDavid Gibson {
6831189be65SPaul Mackerras 	/* Use 1T segments if possible for addresses >= 1T */
6841189be65SPaul Mackerras 	if (addr >= (1UL << SID_SHIFT_1T))
6851189be65SPaul Mackerras 		return mmu_highuser_ssize;
6861189be65SPaul Mackerras 	return MMU_SEGSIZE_256M;
6871189be65SPaul Mackerras }
6881189be65SPaul Mackerras 
6891189be65SPaul Mackerras static inline unsigned long get_vsid(unsigned long context, unsigned long ea,
6901189be65SPaul Mackerras 				     int ssize)
6911189be65SPaul Mackerras {
692e6f81a92SAneesh Kumar K.V 	unsigned long va_bits = VA_BITS;
693e6f81a92SAneesh Kumar K.V 	unsigned long vsid_bits;
694e6f81a92SAneesh Kumar K.V 	unsigned long protovsid;
695e6f81a92SAneesh Kumar K.V 
696c60ac569SAneesh Kumar K.V 	/*
697c60ac569SAneesh Kumar K.V 	 * Bad address. We return VSID 0 for that
698c60ac569SAneesh Kumar K.V 	 */
699dd1842a2SAneesh Kumar K.V 	if ((ea & ~REGION_MASK) >= H_PGTABLE_RANGE)
700c60ac569SAneesh Kumar K.V 		return 0;
701c60ac569SAneesh Kumar K.V 
702e6f81a92SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
703e6f81a92SAneesh Kumar K.V 		va_bits = 65;
704e6f81a92SAneesh Kumar K.V 
705e6f81a92SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M) {
706e6f81a92SAneesh Kumar K.V 		vsid_bits = va_bits - SID_SHIFT;
707e6f81a92SAneesh Kumar K.V 		protovsid = (context << ESID_BITS) |
708e6f81a92SAneesh Kumar K.V 			((ea >> SID_SHIFT) & ESID_BITS_MASK);
709e6f81a92SAneesh Kumar K.V 		return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
710e6f81a92SAneesh Kumar K.V 	}
711e6f81a92SAneesh Kumar K.V 	/* 1T segment */
712e6f81a92SAneesh Kumar K.V 	vsid_bits = va_bits - SID_SHIFT_1T;
713e6f81a92SAneesh Kumar K.V 	protovsid = (context << ESID_BITS_1T) |
714e6f81a92SAneesh Kumar K.V 		((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
715e6f81a92SAneesh Kumar K.V 	return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
7168d2169e8SDavid Gibson }
7178d2169e8SDavid Gibson 
718c60ac569SAneesh Kumar K.V /*
719c60ac569SAneesh Kumar K.V  * This is only valid for addresses >= PAGE_OFFSET
720c60ac569SAneesh Kumar K.V  */
721c60ac569SAneesh Kumar K.V static inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
722c60ac569SAneesh Kumar K.V {
723c60ac569SAneesh Kumar K.V 	unsigned long context;
724c60ac569SAneesh Kumar K.V 
72585beb1c4SMichael Ellerman 	if (!is_kernel_addr(ea))
72685beb1c4SMichael Ellerman 		return 0;
72785beb1c4SMichael Ellerman 
728c60ac569SAneesh Kumar K.V 	/*
729941711a3SAneesh Kumar K.V 	 * For kernel space, we use context ids 1-4 to map the address space as
730941711a3SAneesh Kumar K.V 	 * below:
731941711a3SAneesh Kumar K.V 	 *
732941711a3SAneesh Kumar K.V 	 * 0x00001 -  [ 0xc000000000000000 - 0xc0003fffffffffff ]
733941711a3SAneesh Kumar K.V 	 * 0x00002 -  [ 0xd000000000000000 - 0xd0003fffffffffff ]
734941711a3SAneesh Kumar K.V 	 * 0x00003 -  [ 0xe000000000000000 - 0xe0003fffffffffff ]
735941711a3SAneesh Kumar K.V 	 * 0x00004 -  [ 0xf000000000000000 - 0xf0003fffffffffff ]
736941711a3SAneesh Kumar K.V 	 *
737941711a3SAneesh Kumar K.V 	 * So we can compute the context from the region (top nibble) by
738941711a3SAneesh Kumar K.V 	 * subtracting 11, or 0xc - 1.
739c60ac569SAneesh Kumar K.V 	 */
740941711a3SAneesh Kumar K.V 	context = (ea >> 60) - KERNEL_REGION_CONTEXT_OFFSET;
741941711a3SAneesh Kumar K.V 
742c60ac569SAneesh Kumar K.V 	return get_vsid(context, ea, ssize);
743c60ac569SAneesh Kumar K.V }
7445c3c7edeSDavid Gibson 
7455c3c7edeSDavid Gibson unsigned htab_shift_for_mem_size(unsigned long mem_size);
7465c3c7edeSDavid Gibson 
7478d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
7488d2169e8SDavid Gibson 
74911a6f6abSAneesh Kumar K.V #endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */
750