xref: /linux/arch/powerpc/include/asm/book3s/64/mmu-hash.h (revision 79cc38ded1e1ac86e69c90f604efadd50b0b3762)
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)
1078d2169e8SDavid Gibson 
108b7abc5c5SSachin P. Sant #define HPTE_V_1TB_SEG		ASM_CONST(0x4000000000000000)
109b7abc5c5SSachin P. Sant #define HPTE_V_VRMA_MASK	ASM_CONST(0x4001ffffff000000)
110b7abc5c5SSachin P. Sant 
1118d2169e8SDavid Gibson /* Values for PP (assumes Ks=0, Kp=1) */
1128d2169e8SDavid Gibson #define PP_RWXX	0	/* Supervisor read/write, User none */
1138d2169e8SDavid Gibson #define PP_RWRX 1	/* Supervisor read/write, User read */
1148d2169e8SDavid Gibson #define PP_RWRW 2	/* Supervisor read/write, User read/write */
1158d2169e8SDavid Gibson #define PP_RXRX 3	/* Supervisor read,       User read */
116697d3899SPaul Mackerras #define PP_RXXX	(HPTE_R_PP0 | 2)	/* Supervisor read, user none */
1178d2169e8SDavid Gibson 
118b4072df4SPaul Mackerras /* Fields for tlbiel instruction in architecture 2.06 */
119b4072df4SPaul Mackerras #define TLBIEL_INVAL_SEL_MASK	0xc00	/* invalidation selector */
120b4072df4SPaul Mackerras #define  TLBIEL_INVAL_PAGE	0x000	/* invalidate a single page */
121b4072df4SPaul Mackerras #define  TLBIEL_INVAL_SET_LPID	0x800	/* invalidate a set for current LPID */
122b4072df4SPaul Mackerras #define  TLBIEL_INVAL_SET	0xc00	/* invalidate a set for all LPIDs */
123b4072df4SPaul Mackerras #define TLBIEL_INVAL_SET_MASK	0xfff000	/* set number to inval. */
124b4072df4SPaul Mackerras #define TLBIEL_INVAL_SET_SHIFT	12
125b4072df4SPaul Mackerras 
126b4072df4SPaul Mackerras #define POWER7_TLB_SETS		128	/* # sets in POWER7 TLB */
12745706bb5SMahesh Salgaonkar #define POWER8_TLB_SETS		512	/* # sets in POWER8 TLB */
128c3ab300eSMichael Neuling #define POWER9_TLB_SETS_HASH	256	/* # sets in POWER9 TLB Hash mode */
1291a472c9dSAneesh Kumar K.V #define POWER9_TLB_SETS_RADIX	128	/* # sets in POWER9 TLB Radix mode */
130b4072df4SPaul Mackerras 
1318d2169e8SDavid Gibson #ifndef __ASSEMBLY__
1328d2169e8SDavid Gibson 
1337025776eSBenjamin Herrenschmidt struct mmu_hash_ops {
1347025776eSBenjamin Herrenschmidt 	void            (*hpte_invalidate)(unsigned long slot,
1357025776eSBenjamin Herrenschmidt 					   unsigned long vpn,
1367025776eSBenjamin Herrenschmidt 					   int bpsize, int apsize,
1377025776eSBenjamin Herrenschmidt 					   int ssize, int local);
1387025776eSBenjamin Herrenschmidt 	long		(*hpte_updatepp)(unsigned long slot,
1397025776eSBenjamin Herrenschmidt 					 unsigned long newpp,
1407025776eSBenjamin Herrenschmidt 					 unsigned long vpn,
1417025776eSBenjamin Herrenschmidt 					 int bpsize, int apsize,
1427025776eSBenjamin Herrenschmidt 					 int ssize, unsigned long flags);
1437025776eSBenjamin Herrenschmidt 	void            (*hpte_updateboltedpp)(unsigned long newpp,
1447025776eSBenjamin Herrenschmidt 					       unsigned long ea,
1457025776eSBenjamin Herrenschmidt 					       int psize, int ssize);
1467025776eSBenjamin Herrenschmidt 	long		(*hpte_insert)(unsigned long hpte_group,
1477025776eSBenjamin Herrenschmidt 				       unsigned long vpn,
1487025776eSBenjamin Herrenschmidt 				       unsigned long prpn,
1497025776eSBenjamin Herrenschmidt 				       unsigned long rflags,
1507025776eSBenjamin Herrenschmidt 				       unsigned long vflags,
1517025776eSBenjamin Herrenschmidt 				       int psize, int apsize,
1527025776eSBenjamin Herrenschmidt 				       int ssize);
1537025776eSBenjamin Herrenschmidt 	long		(*hpte_remove)(unsigned long hpte_group);
1547025776eSBenjamin Herrenschmidt 	int             (*hpte_removebolted)(unsigned long ea,
1557025776eSBenjamin Herrenschmidt 					     int psize, int ssize);
1567025776eSBenjamin Herrenschmidt 	void		(*flush_hash_range)(unsigned long number, int local);
1577025776eSBenjamin Herrenschmidt 	void		(*hugepage_invalidate)(unsigned long vsid,
1587025776eSBenjamin Herrenschmidt 					       unsigned long addr,
1597025776eSBenjamin Herrenschmidt 					       unsigned char *hpte_slot_array,
1607025776eSBenjamin Herrenschmidt 					       int psize, int ssize, int local);
161dbcf929cSDavid Gibson 	int		(*resize_hpt)(unsigned long shift);
1627025776eSBenjamin Herrenschmidt 	/*
1637025776eSBenjamin Herrenschmidt 	 * Special for kexec.
1647025776eSBenjamin Herrenschmidt 	 * To be called in real mode with interrupts disabled. No locks are
1657025776eSBenjamin Herrenschmidt 	 * taken as such, concurrent access on pre POWER5 hardware could result
1667025776eSBenjamin Herrenschmidt 	 * in a deadlock.
1677025776eSBenjamin Herrenschmidt 	 * The linear mapping is destroyed as well.
1687025776eSBenjamin Herrenschmidt 	 */
1697025776eSBenjamin Herrenschmidt 	void		(*hpte_clear_all)(void);
1707025776eSBenjamin Herrenschmidt };
1717025776eSBenjamin Herrenschmidt extern struct mmu_hash_ops mmu_hash_ops;
1727025776eSBenjamin Herrenschmidt 
1738e561e7eSDavid Gibson struct hash_pte {
17412f04f2bSAnton Blanchard 	__be64 v;
17512f04f2bSAnton Blanchard 	__be64 r;
1768e561e7eSDavid Gibson };
1778d2169e8SDavid Gibson 
1788e561e7eSDavid Gibson extern struct hash_pte *htab_address;
1798d2169e8SDavid Gibson extern unsigned long htab_size_bytes;
1808d2169e8SDavid Gibson extern unsigned long htab_hash_mask;
1818d2169e8SDavid Gibson 
182cf9427b8SAneesh Kumar K.V 
183cf9427b8SAneesh Kumar K.V static inline int shift_to_mmu_psize(unsigned int shift)
184cf9427b8SAneesh Kumar K.V {
185cf9427b8SAneesh Kumar K.V 	int psize;
186cf9427b8SAneesh Kumar K.V 
187cf9427b8SAneesh Kumar K.V 	for (psize = 0; psize < MMU_PAGE_COUNT; ++psize)
188cf9427b8SAneesh Kumar K.V 		if (mmu_psize_defs[psize].shift == shift)
189cf9427b8SAneesh Kumar K.V 			return psize;
190cf9427b8SAneesh Kumar K.V 	return -1;
191cf9427b8SAneesh Kumar K.V }
192cf9427b8SAneesh Kumar K.V 
193cf9427b8SAneesh Kumar K.V static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
194cf9427b8SAneesh Kumar K.V {
195cf9427b8SAneesh Kumar K.V 	if (mmu_psize_defs[mmu_psize].shift)
196cf9427b8SAneesh Kumar K.V 		return mmu_psize_defs[mmu_psize].shift;
197cf9427b8SAneesh Kumar K.V 	BUG();
198cf9427b8SAneesh Kumar K.V }
1998d2169e8SDavid Gibson 
200138ee7eeSAneesh Kumar K.V static inline unsigned long get_sllp_encoding(int psize)
201138ee7eeSAneesh Kumar K.V {
202138ee7eeSAneesh Kumar K.V 	unsigned long sllp;
203138ee7eeSAneesh Kumar K.V 
204138ee7eeSAneesh Kumar K.V 	sllp = ((mmu_psize_defs[psize].sllp & SLB_VSID_L) >> 6) |
205138ee7eeSAneesh Kumar K.V 		((mmu_psize_defs[psize].sllp & SLB_VSID_LP) >> 4);
206138ee7eeSAneesh Kumar K.V 	return sllp;
207138ee7eeSAneesh Kumar K.V }
208138ee7eeSAneesh Kumar K.V 
2098d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
2108d2169e8SDavid Gibson 
2118d2169e8SDavid Gibson /*
2122454c7e9SPaul Mackerras  * Segment sizes.
2132454c7e9SPaul Mackerras  * These are the values used by hardware in the B field of
2142454c7e9SPaul Mackerras  * SLB entries and the first dword of MMU hashtable entries.
2152454c7e9SPaul Mackerras  * The B field is 2 bits; the values 2 and 3 are unused and reserved.
2162454c7e9SPaul Mackerras  */
2172454c7e9SPaul Mackerras #define MMU_SEGSIZE_256M	0
2182454c7e9SPaul Mackerras #define MMU_SEGSIZE_1T		1
2192454c7e9SPaul Mackerras 
2205524a27dSAneesh Kumar K.V /*
2215524a27dSAneesh Kumar K.V  * encode page number shift.
2225524a27dSAneesh Kumar K.V  * in order to fit the 78 bit va in a 64 bit variable we shift the va by
2235524a27dSAneesh Kumar K.V  * 12 bits. This enable us to address upto 76 bit va.
2245524a27dSAneesh Kumar K.V  * For hpt hash from a va we can ignore the page size bits of va and for
2255524a27dSAneesh Kumar K.V  * hpte encoding we ignore up to 23 bits of va. So ignoring lower 12 bits ensure
2265524a27dSAneesh Kumar K.V  * we work in all cases including 4k page size.
2275524a27dSAneesh Kumar K.V  */
2285524a27dSAneesh Kumar K.V #define VPN_SHIFT	12
2291189be65SPaul Mackerras 
230b1022fbdSAneesh Kumar K.V /*
231b1022fbdSAneesh Kumar K.V  * HPTE Large Page (LP) details
232b1022fbdSAneesh Kumar K.V  */
233b1022fbdSAneesh Kumar K.V #define LP_SHIFT	12
234b1022fbdSAneesh Kumar K.V #define LP_BITS		8
235b1022fbdSAneesh Kumar K.V #define LP_MASK(i)	((0xFF >> (i)) << LP_SHIFT)
236b1022fbdSAneesh Kumar K.V 
2378d2169e8SDavid Gibson #ifndef __ASSEMBLY__
2388d2169e8SDavid Gibson 
23973d16a6eSIan Munsie static inline int slb_vsid_shift(int ssize)
24073d16a6eSIan Munsie {
24173d16a6eSIan Munsie 	if (ssize == MMU_SEGSIZE_256M)
24273d16a6eSIan Munsie 		return SLB_VSID_SHIFT;
24373d16a6eSIan Munsie 	return SLB_VSID_SHIFT_1T;
24473d16a6eSIan Munsie }
24573d16a6eSIan Munsie 
2465524a27dSAneesh Kumar K.V static inline int segment_shift(int ssize)
2475524a27dSAneesh Kumar K.V {
2485524a27dSAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M)
2495524a27dSAneesh Kumar K.V 		return SID_SHIFT;
2505524a27dSAneesh Kumar K.V 	return SID_SHIFT_1T;
2515524a27dSAneesh Kumar K.V }
2525524a27dSAneesh Kumar K.V 
2538d2169e8SDavid Gibson /*
2540eeede0cSPaul Mackerras  * This array is indexed by the LP field of the HPTE second dword.
2550eeede0cSPaul Mackerras  * Since this field may contain some RPN bits, some entries are
2560eeede0cSPaul Mackerras  * replicated so that we get the same value irrespective of RPN.
2570eeede0cSPaul Mackerras  * The top 4 bits are the page size index (MMU_PAGE_*) for the
2580eeede0cSPaul Mackerras  * actual page size, the bottom 4 bits are the base page size.
2590eeede0cSPaul Mackerras  */
2600eeede0cSPaul Mackerras extern u8 hpte_page_sizes[1 << LP_BITS];
2610eeede0cSPaul Mackerras 
2620eeede0cSPaul Mackerras static inline unsigned long __hpte_page_size(unsigned long h, unsigned long l,
2630eeede0cSPaul Mackerras 					     bool is_base_size)
2640eeede0cSPaul Mackerras {
2650eeede0cSPaul Mackerras 	unsigned int i, lp;
2660eeede0cSPaul Mackerras 
2670eeede0cSPaul Mackerras 	if (!(h & HPTE_V_LARGE))
2680eeede0cSPaul Mackerras 		return 1ul << 12;
2690eeede0cSPaul Mackerras 
2700eeede0cSPaul Mackerras 	/* Look at the 8 bit LP value */
2710eeede0cSPaul Mackerras 	lp = (l >> LP_SHIFT) & ((1 << LP_BITS) - 1);
2720eeede0cSPaul Mackerras 	i = hpte_page_sizes[lp];
2730eeede0cSPaul Mackerras 	if (!i)
2740eeede0cSPaul Mackerras 		return 0;
2750eeede0cSPaul Mackerras 	if (!is_base_size)
2760eeede0cSPaul Mackerras 		i >>= 4;
2770eeede0cSPaul Mackerras 	return 1ul << mmu_psize_defs[i & 0xf].shift;
2780eeede0cSPaul Mackerras }
2790eeede0cSPaul Mackerras 
2800eeede0cSPaul Mackerras static inline unsigned long hpte_page_size(unsigned long h, unsigned long l)
2810eeede0cSPaul Mackerras {
2820eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 0);
2830eeede0cSPaul Mackerras }
2840eeede0cSPaul Mackerras 
2850eeede0cSPaul Mackerras static inline unsigned long hpte_base_page_size(unsigned long h, unsigned long l)
2860eeede0cSPaul Mackerras {
2870eeede0cSPaul Mackerras 	return __hpte_page_size(h, l, 1);
2880eeede0cSPaul Mackerras }
2890eeede0cSPaul Mackerras 
2900eeede0cSPaul Mackerras /*
2911189be65SPaul Mackerras  * The current system page and segment sizes
2928d2169e8SDavid Gibson  */
2931189be65SPaul Mackerras extern int mmu_kernel_ssize;
2941189be65SPaul Mackerras extern int mmu_highuser_ssize;
295584f8b71SMichael Neuling extern u16 mmu_slb_size;
296572fb578SMichael Ellerman extern unsigned long tce_alloc_start, tce_alloc_end;
2978d2169e8SDavid Gibson 
2988d2169e8SDavid Gibson /*
2998d2169e8SDavid Gibson  * If the processor supports 64k normal pages but not 64k cache
3008d2169e8SDavid Gibson  * inhibited pages, we have to be prepared to switch processes
3018d2169e8SDavid Gibson  * to use 4k pages when they create cache-inhibited mappings.
3028d2169e8SDavid Gibson  * If this is the case, mmu_ci_restrictions will be set to 1.
3038d2169e8SDavid Gibson  */
3048d2169e8SDavid Gibson extern int mmu_ci_restrictions;
3058d2169e8SDavid Gibson 
3068d2169e8SDavid Gibson /*
3075524a27dSAneesh Kumar K.V  * This computes the AVPN and B fields of the first dword of a HPTE,
3085524a27dSAneesh Kumar K.V  * for use when we want to match an existing PTE.  The bottom 7 bits
3095524a27dSAneesh Kumar K.V  * of the returned value are zero.
3108d2169e8SDavid Gibson  */
3115524a27dSAneesh Kumar K.V static inline unsigned long hpte_encode_avpn(unsigned long vpn, int psize,
3121189be65SPaul Mackerras 					     int ssize)
3138d2169e8SDavid Gibson {
3141189be65SPaul Mackerras 	unsigned long v;
3155524a27dSAneesh Kumar K.V 	/*
3165524a27dSAneesh Kumar K.V 	 * The AVA field omits the low-order 23 bits of the 78 bits VA.
3175524a27dSAneesh Kumar K.V 	 * These bits are not needed in the PTE, because the
3185524a27dSAneesh Kumar K.V 	 * low-order b of these bits are part of the byte offset
3195524a27dSAneesh Kumar K.V 	 * into the virtual page and, if b < 23, the high-order
3205524a27dSAneesh Kumar K.V 	 * 23-b of these bits are always used in selecting the
3215524a27dSAneesh Kumar K.V 	 * PTEGs to be searched
3225524a27dSAneesh Kumar K.V 	 */
3235524a27dSAneesh Kumar K.V 	v = (vpn >> (23 - VPN_SHIFT)) & ~(mmu_psize_defs[psize].avpnm);
3248d2169e8SDavid Gibson 	v <<= HPTE_V_AVPN_SHIFT;
3255524a27dSAneesh Kumar K.V 	v |= ((unsigned long) ssize) << HPTE_V_SSIZE_SHIFT;
3265524a27dSAneesh Kumar K.V 	return v;
3275524a27dSAneesh Kumar K.V }
3285524a27dSAneesh Kumar K.V 
3295524a27dSAneesh Kumar K.V /*
3306b243fcfSPaul Mackerras  * ISA v3.0 defines a new HPTE format, which differs from the old
3316b243fcfSPaul Mackerras  * format in having smaller AVPN and ARPN fields, and the B field
3326b243fcfSPaul Mackerras  * in the second dword instead of the first.
3336b243fcfSPaul Mackerras  */
3346b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_v(unsigned long v)
3356b243fcfSPaul Mackerras {
3366b243fcfSPaul Mackerras 	/* trim AVPN, drop B */
3376b243fcfSPaul Mackerras 	return v & HPTE_V_COMMON_BITS;
3386b243fcfSPaul Mackerras }
3396b243fcfSPaul Mackerras 
3406b243fcfSPaul Mackerras static inline unsigned long hpte_old_to_new_r(unsigned long v, unsigned long r)
3416b243fcfSPaul Mackerras {
3426b243fcfSPaul Mackerras 	/* move B field from 1st to 2nd dword, trim ARPN */
3436b243fcfSPaul Mackerras 	return (r & ~HPTE_R_3_0_SSIZE_MASK) |
3446b243fcfSPaul Mackerras 		(((v) >> HPTE_V_SSIZE_SHIFT) << HPTE_R_3_0_SSIZE_SHIFT);
3456b243fcfSPaul Mackerras }
3466b243fcfSPaul Mackerras 
3476b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_v(unsigned long v, unsigned long r)
3486b243fcfSPaul Mackerras {
3496b243fcfSPaul Mackerras 	/* insert B field */
3506b243fcfSPaul Mackerras 	return (v & HPTE_V_COMMON_BITS) |
3516b243fcfSPaul Mackerras 		((r & HPTE_R_3_0_SSIZE_MASK) <<
3526b243fcfSPaul Mackerras 		 (HPTE_V_SSIZE_SHIFT - HPTE_R_3_0_SSIZE_SHIFT));
3536b243fcfSPaul Mackerras }
3546b243fcfSPaul Mackerras 
3556b243fcfSPaul Mackerras static inline unsigned long hpte_new_to_old_r(unsigned long r)
3566b243fcfSPaul Mackerras {
3576b243fcfSPaul Mackerras 	/* clear out B field */
3586b243fcfSPaul Mackerras 	return r & ~HPTE_R_3_0_SSIZE_MASK;
3596b243fcfSPaul Mackerras }
3606b243fcfSPaul Mackerras 
3616b243fcfSPaul Mackerras /*
3625524a27dSAneesh Kumar K.V  * This function sets the AVPN and L fields of the HPTE  appropriately
363b1022fbdSAneesh Kumar K.V  * using the base page size and actual page size.
3645524a27dSAneesh Kumar K.V  */
365b1022fbdSAneesh Kumar K.V static inline unsigned long hpte_encode_v(unsigned long vpn, int base_psize,
366b1022fbdSAneesh Kumar K.V 					  int actual_psize, int ssize)
3675524a27dSAneesh Kumar K.V {
3685524a27dSAneesh Kumar K.V 	unsigned long v;
369b1022fbdSAneesh Kumar K.V 	v = hpte_encode_avpn(vpn, base_psize, ssize);
370b1022fbdSAneesh Kumar K.V 	if (actual_psize != MMU_PAGE_4K)
3718d2169e8SDavid Gibson 		v |= HPTE_V_LARGE;
3728d2169e8SDavid Gibson 	return v;
3738d2169e8SDavid Gibson }
3748d2169e8SDavid Gibson 
3758d2169e8SDavid Gibson /*
3768d2169e8SDavid Gibson  * This function sets the ARPN, and LP fields of the HPTE appropriately
3778d2169e8SDavid Gibson  * for the page size. We assume the pa is already "clean" that is properly
3788d2169e8SDavid Gibson  * aligned for the requested page size
3798d2169e8SDavid Gibson  */
380b1022fbdSAneesh Kumar K.V static inline unsigned long hpte_encode_r(unsigned long pa, int base_psize,
3816b243fcfSPaul Mackerras 					  int actual_psize)
3828d2169e8SDavid Gibson {
3838d2169e8SDavid Gibson 	/* A 4K page needs no special encoding */
384b1022fbdSAneesh Kumar K.V 	if (actual_psize == MMU_PAGE_4K)
3858d2169e8SDavid Gibson 		return pa & HPTE_R_RPN;
3868d2169e8SDavid Gibson 	else {
387b1022fbdSAneesh Kumar K.V 		unsigned int penc = mmu_psize_defs[base_psize].penc[actual_psize];
388b1022fbdSAneesh Kumar K.V 		unsigned int shift = mmu_psize_defs[actual_psize].shift;
389b1022fbdSAneesh Kumar K.V 		return (pa & ~((1ul << shift) - 1)) | (penc << LP_SHIFT);
3908d2169e8SDavid Gibson 	}
3918d2169e8SDavid Gibson }
3928d2169e8SDavid Gibson 
3938d2169e8SDavid Gibson /*
3945524a27dSAneesh Kumar K.V  * Build a VPN_SHIFT bit shifted va given VSID, EA and segment size.
3951189be65SPaul Mackerras  */
3965524a27dSAneesh Kumar K.V static inline unsigned long hpt_vpn(unsigned long ea,
3975524a27dSAneesh Kumar K.V 				    unsigned long vsid, int ssize)
3981189be65SPaul Mackerras {
3995524a27dSAneesh Kumar K.V 	unsigned long mask;
4005524a27dSAneesh Kumar K.V 	int s_shift = segment_shift(ssize);
4015524a27dSAneesh Kumar K.V 
4025524a27dSAneesh Kumar K.V 	mask = (1ul << (s_shift - VPN_SHIFT)) - 1;
4035524a27dSAneesh Kumar K.V 	return (vsid << (s_shift - VPN_SHIFT)) | ((ea >> VPN_SHIFT) & mask);
4041189be65SPaul Mackerras }
4051189be65SPaul Mackerras 
4061189be65SPaul Mackerras /*
4071189be65SPaul Mackerras  * This hashes a virtual address
4088d2169e8SDavid Gibson  */
4095524a27dSAneesh Kumar K.V static inline unsigned long hpt_hash(unsigned long vpn,
4105524a27dSAneesh Kumar K.V 				     unsigned int shift, int ssize)
4118d2169e8SDavid Gibson {
41259248aecSAneesh Kumar K.V 	unsigned long mask;
4131189be65SPaul Mackerras 	unsigned long hash, vsid;
4141189be65SPaul Mackerras 
4155524a27dSAneesh Kumar K.V 	/* VPN_SHIFT can be atmost 12 */
4161189be65SPaul Mackerras 	if (ssize == MMU_SEGSIZE_256M) {
4175524a27dSAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT - VPN_SHIFT)) - 1;
4185524a27dSAneesh Kumar K.V 		hash = (vpn >> (SID_SHIFT - VPN_SHIFT)) ^
4195524a27dSAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT));
4201189be65SPaul Mackerras 	} else {
4215524a27dSAneesh Kumar K.V 		mask = (1ul << (SID_SHIFT_1T - VPN_SHIFT)) - 1;
4225524a27dSAneesh Kumar K.V 		vsid = vpn >> (SID_SHIFT_1T - VPN_SHIFT);
4235524a27dSAneesh Kumar K.V 		hash = vsid ^ (vsid << 25) ^
4245524a27dSAneesh Kumar K.V 			((vpn & mask) >> (shift - VPN_SHIFT)) ;
4251189be65SPaul Mackerras 	}
4261189be65SPaul Mackerras 	return hash & 0x7fffffffffUL;
4278d2169e8SDavid Gibson }
4288d2169e8SDavid Gibson 
429aefa5688SAneesh Kumar K.V #define HPTE_LOCAL_UPDATE	0x1
430aefa5688SAneesh Kumar K.V #define HPTE_NOHPTE_UPDATE	0x2
431aefa5688SAneesh Kumar K.V 
4328d2169e8SDavid Gibson extern int __hash_page_4K(unsigned long ea, unsigned long access,
4338d2169e8SDavid Gibson 			  unsigned long vsid, pte_t *ptep, unsigned long trap,
434aefa5688SAneesh Kumar K.V 			  unsigned long flags, int ssize, int subpage_prot);
4358d2169e8SDavid Gibson extern int __hash_page_64K(unsigned long ea, unsigned long access,
4368d2169e8SDavid Gibson 			   unsigned long vsid, pte_t *ptep, unsigned long trap,
437aefa5688SAneesh Kumar K.V 			   unsigned long flags, int ssize);
4388d2169e8SDavid Gibson struct mm_struct;
4390895ecdaSDavid Gibson unsigned int hash_page_do_lazy_icache(unsigned int pp, pte_t pte, int trap);
440aefa5688SAneesh Kumar K.V extern int hash_page_mm(struct mm_struct *mm, unsigned long ea,
441aefa5688SAneesh Kumar K.V 			unsigned long access, unsigned long trap,
442aefa5688SAneesh Kumar K.V 			unsigned long flags);
443aefa5688SAneesh Kumar K.V extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap,
444aefa5688SAneesh Kumar K.V 		     unsigned long dsisr);
445a4fe3ce7SDavid Gibson int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,
446aefa5688SAneesh Kumar K.V 		     pte_t *ptep, unsigned long trap, unsigned long flags,
447aefa5688SAneesh Kumar K.V 		     int ssize, unsigned int shift, unsigned int mmu_psize);
4486d492eccSAneesh Kumar K.V #ifdef CONFIG_TRANSPARENT_HUGEPAGE
4496d492eccSAneesh Kumar K.V extern int __hash_page_thp(unsigned long ea, unsigned long access,
4506d492eccSAneesh Kumar K.V 			   unsigned long vsid, pmd_t *pmdp, unsigned long trap,
451aefa5688SAneesh Kumar K.V 			   unsigned long flags, int ssize, unsigned int psize);
4526d492eccSAneesh Kumar K.V #else
4536d492eccSAneesh Kumar K.V static inline int __hash_page_thp(unsigned long ea, unsigned long access,
4546d492eccSAneesh Kumar K.V 				  unsigned long vsid, pmd_t *pmdp,
455aefa5688SAneesh Kumar K.V 				  unsigned long trap, unsigned long flags,
4566d492eccSAneesh Kumar K.V 				  int ssize, unsigned int psize)
4576d492eccSAneesh Kumar K.V {
4586d492eccSAneesh Kumar K.V 	BUG();
459ff1e7683SNathan Fontenot 	return -1;
4606d492eccSAneesh Kumar K.V }
4616d492eccSAneesh Kumar K.V #endif
4624b8692c0SBenjamin Herrenschmidt extern void hash_failure_debug(unsigned long ea, unsigned long access,
4634b8692c0SBenjamin Herrenschmidt 			       unsigned long vsid, unsigned long trap,
464d8139ebfSAneesh Kumar K.V 			       int ssize, int psize, int lpsize,
465d8139ebfSAneesh Kumar K.V 			       unsigned long pte);
4668d2169e8SDavid Gibson extern int htab_bolt_mapping(unsigned long vstart, unsigned long vend,
467bc033b63SBenjamin Herrenschmidt 			     unsigned long pstart, unsigned long prot,
4681189be65SPaul Mackerras 			     int psize, int ssize);
469f6026df1SAnton Blanchard int htab_remove_mapping(unsigned long vstart, unsigned long vend,
470f6026df1SAnton Blanchard 			int psize, int ssize);
471*79cc38deSAneesh Kumar K.V extern void pseries_add_gpage(u64 addr, u64 page_size, unsigned long number_of_pages);
472fa28237cSPaul Mackerras extern void demote_segment_4k(struct mm_struct *mm, unsigned long addr);
4738d2169e8SDavid Gibson 
4746364e84eSMichael Ellerman #ifdef CONFIG_PPC_PSERIES
4756364e84eSMichael Ellerman void hpte_init_pseries(void);
4766364e84eSMichael Ellerman #else
4776364e84eSMichael Ellerman static inline void hpte_init_pseries(void) { }
4786364e84eSMichael Ellerman #endif
4796364e84eSMichael Ellerman 
4808d2169e8SDavid Gibson extern void hpte_init_native(void);
4818d2169e8SDavid Gibson 
4828d2169e8SDavid Gibson extern void slb_initialize(void);
4838d2169e8SDavid Gibson extern void slb_flush_and_rebolt(void);
4848d2169e8SDavid Gibson 
48567439b76SMichael Neuling extern void slb_vmalloc_update(void);
48646db2f86SBrian King extern void slb_set_size(u16 size);
4878d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
4888d2169e8SDavid Gibson 
4898d2169e8SDavid Gibson /*
490f033d659SAneesh Kumar K.V  * VSID allocation (256MB segment)
4918d2169e8SDavid Gibson  *
492c60ac569SAneesh Kumar K.V  * We first generate a 37-bit "proto-VSID". Proto-VSIDs are generated
493c60ac569SAneesh Kumar K.V  * from mmu context id and effective segment id of the address.
4948d2169e8SDavid Gibson  *
495941711a3SAneesh Kumar K.V  * For user processes max context id is limited to MAX_USER_CONTEXT.
496941711a3SAneesh Kumar K.V 
497add2e1e5SMichael Ellerman  * For kernel space, we use context ids 1-4 to map addresses as below:
498c60ac569SAneesh Kumar K.V  * NOTE: each context only support 64TB now.
499941711a3SAneesh Kumar K.V  * 0x00001 -  [ 0xc000000000000000 - 0xc0003fffffffffff ]
500941711a3SAneesh Kumar K.V  * 0x00002 -  [ 0xd000000000000000 - 0xd0003fffffffffff ]
501941711a3SAneesh Kumar K.V  * 0x00003 -  [ 0xe000000000000000 - 0xe0003fffffffffff ]
502941711a3SAneesh Kumar K.V  * 0x00004 -  [ 0xf000000000000000 - 0xf0003fffffffffff ]
5038d2169e8SDavid Gibson  *
5048d2169e8SDavid Gibson  * The proto-VSIDs are then scrambled into real VSIDs with the
5058d2169e8SDavid Gibson  * multiplicative hash:
5068d2169e8SDavid Gibson  *
5078d2169e8SDavid Gibson  *	VSID = (proto-VSID * VSID_MULTIPLIER) % VSID_MODULUS
5088d2169e8SDavid Gibson  *
509f033d659SAneesh Kumar K.V  * VSID_MULTIPLIER is prime, so in particular it is
5108d2169e8SDavid Gibson  * co-prime to VSID_MODULUS, making this a 1:1 scrambling function.
5118d2169e8SDavid Gibson  * Because the modulus is 2^n-1 we can compute it efficiently without
512c60ac569SAneesh Kumar K.V  * a divide or extra multiply (see below). The scramble function gives
513c60ac569SAneesh Kumar K.V  * robust scattering in the hash table (at least based on some initial
514c60ac569SAneesh Kumar K.V  * results).
5158d2169e8SDavid Gibson  *
516941711a3SAneesh Kumar K.V  * We use VSID 0 to indicate an invalid VSID. The means we can't use context id
517941711a3SAneesh Kumar K.V  * 0, because a context id of 0 and an EA of 0 gives a proto-VSID of 0, which
518941711a3SAneesh Kumar K.V  * will produce a VSID of 0.
5198d2169e8SDavid Gibson  *
520c60ac569SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
521c60ac569SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
522941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
5238d2169e8SDavid Gibson  */
5248d2169e8SDavid Gibson 
525e6f81a92SAneesh Kumar K.V /*
526e6f81a92SAneesh Kumar K.V  * Max Va bits we support as of now is 68 bits. We want 19 bit
527e6f81a92SAneesh Kumar K.V  * context ID.
528e6f81a92SAneesh Kumar K.V  * Restrictions:
529e6f81a92SAneesh Kumar K.V  * GPU has restrictions of not able to access beyond 128TB
530e6f81a92SAneesh Kumar K.V  * (47 bit effective address). We also cannot do more than 20bit PID.
531e6f81a92SAneesh Kumar K.V  * For p4 and p5 which can only do 65 bit VA, we restrict our CONTEXT_BITS
532e6f81a92SAneesh Kumar K.V  * to 16 bits (ie, we can only have 2^16 pids at the same time).
533e6f81a92SAneesh Kumar K.V  */
534e6f81a92SAneesh Kumar K.V #define VA_BITS			68
535e39d1a47SAneesh Kumar K.V #define CONTEXT_BITS		19
536e6f81a92SAneesh Kumar K.V #define ESID_BITS		(VA_BITS - (SID_SHIFT + CONTEXT_BITS))
537e6f81a92SAneesh Kumar K.V #define ESID_BITS_1T		(VA_BITS - (SID_SHIFT_1T + CONTEXT_BITS))
538e39d1a47SAneesh Kumar K.V 
53979270e0aSAneesh Kumar K.V #define ESID_BITS_MASK		((1 << ESID_BITS) - 1)
54079270e0aSAneesh Kumar K.V #define ESID_BITS_1T_MASK	((1 << ESID_BITS_1T) - 1)
54179270e0aSAneesh Kumar K.V 
542048ee099SAneesh Kumar K.V /*
543c60ac569SAneesh Kumar K.V  * 256MB segment
544af81d787SAneesh Kumar K.V  * The proto-VSID space has 2^(CONTEX_BITS + ESID_BITS) - 1 segments
545941711a3SAneesh Kumar K.V  * available for user + kernel mapping. VSID 0 is reserved as invalid, contexts
546941711a3SAneesh Kumar K.V  * 1-4 are used for kernel mapping. Each segment contains 2^28 bytes. Each
547e6f81a92SAneesh Kumar K.V  * context maps 2^49 bytes (512TB).
548941711a3SAneesh Kumar K.V  *
549941711a3SAneesh Kumar K.V  * We also need to avoid the last segment of the last context, because that
550941711a3SAneesh Kumar K.V  * would give a protovsid of 0x1fffffffff. That will result in a VSID 0
551941711a3SAneesh Kumar K.V  * because of the modulo operation in vsid scramble.
552c60ac569SAneesh Kumar K.V  */
553941711a3SAneesh Kumar K.V #define MAX_USER_CONTEXT	((ASM_CONST(1) << CONTEXT_BITS) - 2)
554941711a3SAneesh Kumar K.V #define MIN_USER_CONTEXT	(5)
555941711a3SAneesh Kumar K.V 
556941711a3SAneesh Kumar K.V /* Would be nice to use KERNEL_REGION_ID here */
557941711a3SAneesh Kumar K.V #define KERNEL_REGION_CONTEXT_OFFSET	(0xc - 1)
558c60ac569SAneesh Kumar K.V 
559c60ac569SAneesh Kumar K.V /*
560e6f81a92SAneesh Kumar K.V  * For platforms that support on 65bit VA we limit the context bits
561048ee099SAneesh Kumar K.V  */
562e6f81a92SAneesh Kumar K.V #define MAX_USER_CONTEXT_65BIT_VA ((ASM_CONST(1) << (65 - (SID_SHIFT + ESID_BITS))) - 2)
5638d2169e8SDavid Gibson 
5648d2169e8SDavid Gibson /*
565e6f81a92SAneesh Kumar K.V  * This should be computed such that protovosid * vsid_mulitplier
566e6f81a92SAneesh Kumar K.V  * doesn't overflow 64 bits. The vsid_mutliplier should also be
567e6f81a92SAneesh Kumar K.V  * co-prime to vsid_modulus. We also need to make sure that number
568e6f81a92SAneesh Kumar K.V  * of bits in multiplied result (dividend) is less than twice the number of
569e6f81a92SAneesh Kumar K.V  * protovsid bits for our modulus optmization to work.
5708d2169e8SDavid Gibson  *
571e6f81a92SAneesh Kumar K.V  * The below table shows the current values used.
572e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
573e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_65VA | Total Bits | 2* prot VSID_BITS |
574e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
575e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   25 |         49 |                50 |
576e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
577e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   37 |         61 |                74 |
578e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+-------------------|
5798d2169e8SDavid Gibson  *
580e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
581e6f81a92SAneesh Kumar K.V  * |       | Prime Bits | proto VSID_BITS_68VA | Total Bits | 2* proto VSID_BITS |
582e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
583e6f81a92SAneesh Kumar K.V  * | 1T    |         24 |                   28 |         52 |                 56 |
584e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
585e6f81a92SAneesh Kumar K.V  * | 256MB |         24 |                   40 |         64 |                 80 |
586e6f81a92SAneesh Kumar K.V  * |-------+------------+----------------------+------------+--------------------|
587e6f81a92SAneesh Kumar K.V  *
5888d2169e8SDavid Gibson  */
589e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_256M	ASM_CONST(12538073)	/* 24-bit prime */
590e6f81a92SAneesh Kumar K.V #define VSID_BITS_256M		(VA_BITS - SID_SHIFT)
591e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_256M	(65 - SID_SHIFT)
59282228e36SAneesh Kumar K.V /*
59382228e36SAneesh Kumar K.V  * Modular multiplicative inverse of VSID_MULTIPLIER under modulo VSID_MODULUS
59482228e36SAneesh Kumar K.V  */
59582228e36SAneesh Kumar K.V #define VSID_MULINV_256M	ASM_CONST(665548017062)
596e6f81a92SAneesh Kumar K.V 
597e6f81a92SAneesh Kumar K.V #define VSID_MULTIPLIER_1T	ASM_CONST(12538073)	/* 24-bit prime */
598e6f81a92SAneesh Kumar K.V #define VSID_BITS_1T		(VA_BITS - SID_SHIFT_1T)
599e6f81a92SAneesh Kumar K.V #define VSID_BITS_65_1T		(65 - SID_SHIFT_1T)
60082228e36SAneesh Kumar K.V #define VSID_MULINV_1T		ASM_CONST(209034062)
601e6f81a92SAneesh Kumar K.V 
60282228e36SAneesh Kumar K.V /* 1TB VSID reserved for VRMA */
60382228e36SAneesh Kumar K.V #define VRMA_VSID	0x1ffffffUL
604e6f81a92SAneesh Kumar K.V #define USER_VSID_RANGE	(1UL << (ESID_BITS + SID_SHIFT))
6058d2169e8SDavid Gibson 
60678f1dbdeSAneesh Kumar K.V /* 4 bits per slice and we have one slice per 1TB */
607dd1842a2SAneesh Kumar K.V #define SLICE_ARRAY_SIZE	(H_PGTABLE_RANGE >> 41)
608957b778aSAneesh Kumar K.V #define TASK_SLICE_ARRAY_SZ(x)	((x)->context.addr_limit >> 41)
6098d2169e8SDavid Gibson 
6108d2169e8SDavid Gibson #ifndef __ASSEMBLY__
6118d2169e8SDavid Gibson 
612d28513bcSDavid Gibson #ifdef CONFIG_PPC_SUBPAGE_PROT
613d28513bcSDavid Gibson /*
614d28513bcSDavid Gibson  * For the sub-page protection option, we extend the PGD with one of
615d28513bcSDavid Gibson  * these.  Basically we have a 3-level tree, with the top level being
616d28513bcSDavid Gibson  * the protptrs array.  To optimize speed and memory consumption when
617d28513bcSDavid Gibson  * only addresses < 4GB are being protected, pointers to the first
618d28513bcSDavid Gibson  * four pages of sub-page protection words are stored in the low_prot
619d28513bcSDavid Gibson  * array.
620d28513bcSDavid Gibson  * Each page of sub-page protection words protects 1GB (4 bytes
621d28513bcSDavid Gibson  * protects 64k).  For the 3-level tree, each page of pointers then
622d28513bcSDavid Gibson  * protects 8TB.
623d28513bcSDavid Gibson  */
624d28513bcSDavid Gibson struct subpage_prot_table {
625d28513bcSDavid Gibson 	unsigned long maxaddr;	/* only addresses < this are protected */
626dad6f37cSAneesh Kumar K.V 	unsigned int **protptrs[(TASK_SIZE_USER64 >> 43)];
627d28513bcSDavid Gibson 	unsigned int *low_prot[4];
628d28513bcSDavid Gibson };
629d28513bcSDavid Gibson 
630d28513bcSDavid Gibson #define SBP_L1_BITS		(PAGE_SHIFT - 2)
631d28513bcSDavid Gibson #define SBP_L2_BITS		(PAGE_SHIFT - 3)
632d28513bcSDavid Gibson #define SBP_L1_COUNT		(1 << SBP_L1_BITS)
633d28513bcSDavid Gibson #define SBP_L2_COUNT		(1 << SBP_L2_BITS)
634d28513bcSDavid Gibson #define SBP_L2_SHIFT		(PAGE_SHIFT + SBP_L1_BITS)
635d28513bcSDavid Gibson #define SBP_L3_SHIFT		(SBP_L2_SHIFT + SBP_L2_BITS)
636d28513bcSDavid Gibson 
637d28513bcSDavid Gibson extern void subpage_prot_free(struct mm_struct *mm);
638d28513bcSDavid Gibson extern void subpage_prot_init_new_context(struct mm_struct *mm);
639d28513bcSDavid Gibson #else
640d28513bcSDavid Gibson static inline void subpage_prot_free(struct mm_struct *mm) {}
641d28513bcSDavid Gibson static inline void subpage_prot_init_new_context(struct mm_struct *mm) { }
642d28513bcSDavid Gibson #endif /* CONFIG_PPC_SUBPAGE_PROT */
643d28513bcSDavid Gibson 
6448d2169e8SDavid Gibson #if 0
6451189be65SPaul Mackerras /*
6461189be65SPaul Mackerras  * The code below is equivalent to this function for arguments
6478d2169e8SDavid Gibson  * < 2^VSID_BITS, which is all this should ever be called
6488d2169e8SDavid Gibson  * with.  However gcc is not clever enough to compute the
6491189be65SPaul Mackerras  * modulus (2^n-1) without a second multiply.
6501189be65SPaul Mackerras  */
65134692708SAnton Blanchard #define vsid_scramble(protovsid, size) \
6521189be65SPaul Mackerras 	((((protovsid) * VSID_MULTIPLIER_##size) % VSID_MODULUS_##size))
6538d2169e8SDavid Gibson 
654e6f81a92SAneesh Kumar K.V /* simplified form avoiding mod operation */
6551189be65SPaul Mackerras #define vsid_scramble(protovsid, size) \
6561189be65SPaul Mackerras 	({								 \
6571189be65SPaul Mackerras 		unsigned long x;					 \
6581189be65SPaul Mackerras 		x = (protovsid) * VSID_MULTIPLIER_##size;		 \
6591189be65SPaul Mackerras 		x = (x >> VSID_BITS_##size) + (x & VSID_MODULUS_##size); \
6601189be65SPaul Mackerras 		(x + ((x+1) >> VSID_BITS_##size)) & VSID_MODULUS_##size; \
6611189be65SPaul Mackerras 	})
662e6f81a92SAneesh Kumar K.V 
663e6f81a92SAneesh Kumar K.V #else /* 1 */
664e6f81a92SAneesh Kumar K.V static inline unsigned long vsid_scramble(unsigned long protovsid,
665e6f81a92SAneesh Kumar K.V 				  unsigned long vsid_multiplier, int vsid_bits)
666e6f81a92SAneesh Kumar K.V {
667e6f81a92SAneesh Kumar K.V 	unsigned long vsid;
668e6f81a92SAneesh Kumar K.V 	unsigned long vsid_modulus = ((1UL << vsid_bits) - 1);
669e6f81a92SAneesh Kumar K.V 	/*
670e6f81a92SAneesh Kumar K.V 	 * We have same multipler for both 256 and 1T segements now
671e6f81a92SAneesh Kumar K.V 	 */
672e6f81a92SAneesh Kumar K.V 	vsid = protovsid * vsid_multiplier;
673e6f81a92SAneesh Kumar K.V 	vsid = (vsid >> vsid_bits) + (vsid & vsid_modulus);
674e6f81a92SAneesh Kumar K.V 	return (vsid + ((vsid + 1) >> vsid_bits)) & vsid_modulus;
675e6f81a92SAneesh Kumar K.V }
676e6f81a92SAneesh Kumar K.V 
6778d2169e8SDavid Gibson #endif /* 1 */
6788d2169e8SDavid Gibson 
6791189be65SPaul Mackerras /* Returns the segment size indicator for a user address */
6801189be65SPaul Mackerras static inline int user_segment_size(unsigned long addr)
6818d2169e8SDavid Gibson {
6821189be65SPaul Mackerras 	/* Use 1T segments if possible for addresses >= 1T */
6831189be65SPaul Mackerras 	if (addr >= (1UL << SID_SHIFT_1T))
6841189be65SPaul Mackerras 		return mmu_highuser_ssize;
6851189be65SPaul Mackerras 	return MMU_SEGSIZE_256M;
6861189be65SPaul Mackerras }
6871189be65SPaul Mackerras 
6881189be65SPaul Mackerras static inline unsigned long get_vsid(unsigned long context, unsigned long ea,
6891189be65SPaul Mackerras 				     int ssize)
6901189be65SPaul Mackerras {
691e6f81a92SAneesh Kumar K.V 	unsigned long va_bits = VA_BITS;
692e6f81a92SAneesh Kumar K.V 	unsigned long vsid_bits;
693e6f81a92SAneesh Kumar K.V 	unsigned long protovsid;
694e6f81a92SAneesh Kumar K.V 
695c60ac569SAneesh Kumar K.V 	/*
696c60ac569SAneesh Kumar K.V 	 * Bad address. We return VSID 0 for that
697c60ac569SAneesh Kumar K.V 	 */
698dd1842a2SAneesh Kumar K.V 	if ((ea & ~REGION_MASK) >= H_PGTABLE_RANGE)
699c60ac569SAneesh Kumar K.V 		return 0;
700c60ac569SAneesh Kumar K.V 
701e6f81a92SAneesh Kumar K.V 	if (!mmu_has_feature(MMU_FTR_68_BIT_VA))
702e6f81a92SAneesh Kumar K.V 		va_bits = 65;
703e6f81a92SAneesh Kumar K.V 
704e6f81a92SAneesh Kumar K.V 	if (ssize == MMU_SEGSIZE_256M) {
705e6f81a92SAneesh Kumar K.V 		vsid_bits = va_bits - SID_SHIFT;
706e6f81a92SAneesh Kumar K.V 		protovsid = (context << ESID_BITS) |
707e6f81a92SAneesh Kumar K.V 			((ea >> SID_SHIFT) & ESID_BITS_MASK);
708e6f81a92SAneesh Kumar K.V 		return vsid_scramble(protovsid, VSID_MULTIPLIER_256M, vsid_bits);
709e6f81a92SAneesh Kumar K.V 	}
710e6f81a92SAneesh Kumar K.V 	/* 1T segment */
711e6f81a92SAneesh Kumar K.V 	vsid_bits = va_bits - SID_SHIFT_1T;
712e6f81a92SAneesh Kumar K.V 	protovsid = (context << ESID_BITS_1T) |
713e6f81a92SAneesh Kumar K.V 		((ea >> SID_SHIFT_1T) & ESID_BITS_1T_MASK);
714e6f81a92SAneesh Kumar K.V 	return vsid_scramble(protovsid, VSID_MULTIPLIER_1T, vsid_bits);
7158d2169e8SDavid Gibson }
7168d2169e8SDavid Gibson 
717c60ac569SAneesh Kumar K.V /*
718c60ac569SAneesh Kumar K.V  * This is only valid for addresses >= PAGE_OFFSET
719c60ac569SAneesh Kumar K.V  */
720c60ac569SAneesh Kumar K.V static inline unsigned long get_kernel_vsid(unsigned long ea, int ssize)
721c60ac569SAneesh Kumar K.V {
722c60ac569SAneesh Kumar K.V 	unsigned long context;
723c60ac569SAneesh Kumar K.V 
72485beb1c4SMichael Ellerman 	if (!is_kernel_addr(ea))
72585beb1c4SMichael Ellerman 		return 0;
72685beb1c4SMichael Ellerman 
727c60ac569SAneesh Kumar K.V 	/*
728941711a3SAneesh Kumar K.V 	 * For kernel space, we use context ids 1-4 to map the address space as
729941711a3SAneesh Kumar K.V 	 * below:
730941711a3SAneesh Kumar K.V 	 *
731941711a3SAneesh Kumar K.V 	 * 0x00001 -  [ 0xc000000000000000 - 0xc0003fffffffffff ]
732941711a3SAneesh Kumar K.V 	 * 0x00002 -  [ 0xd000000000000000 - 0xd0003fffffffffff ]
733941711a3SAneesh Kumar K.V 	 * 0x00003 -  [ 0xe000000000000000 - 0xe0003fffffffffff ]
734941711a3SAneesh Kumar K.V 	 * 0x00004 -  [ 0xf000000000000000 - 0xf0003fffffffffff ]
735941711a3SAneesh Kumar K.V 	 *
736941711a3SAneesh Kumar K.V 	 * So we can compute the context from the region (top nibble) by
737941711a3SAneesh Kumar K.V 	 * subtracting 11, or 0xc - 1.
738c60ac569SAneesh Kumar K.V 	 */
739941711a3SAneesh Kumar K.V 	context = (ea >> 60) - KERNEL_REGION_CONTEXT_OFFSET;
740941711a3SAneesh Kumar K.V 
741c60ac569SAneesh Kumar K.V 	return get_vsid(context, ea, ssize);
742c60ac569SAneesh Kumar K.V }
7435c3c7edeSDavid Gibson 
7445c3c7edeSDavid Gibson unsigned htab_shift_for_mem_size(unsigned long mem_size);
7455c3c7edeSDavid Gibson 
7468d2169e8SDavid Gibson #endif /* __ASSEMBLY__ */
7478d2169e8SDavid Gibson 
74811a6f6abSAneesh Kumar K.V #endif /* _ASM_POWERPC_BOOK3S_64_MMU_HASH_H_ */
749