xref: /titanic_52/usr/src/uts/sun4v/sys/pte.h (revision b9e93c10c0a2a4bb069d38bb311021a9478c4711)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SYS_PTE_H
27 #define	_SYS_PTE_H
28 
29 #ifndef _ASM
30 #include <sys/types.h>
31 #endif /* _ASM */
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #ifndef _ASM
38 /*
39  * The tte struct is a 64 bit data type.  Since we currently plan to
40  * use a V8 compiler all manipulations in C will be done using the bit fields
41  * or as 2 integers.  In assembly code we will deal with it as a double (using
42  * ldx and stx).  The structure is defined to force a double alignment.
43  */
44 typedef union {
45 	struct tte {
46 		unsigned int	v:1;		/* <63> valid */
47 		unsigned int	nfo:1;		/* <62> non-fault only */
48 		unsigned int	hmenum:3;	/* <61:59> sw hmenum */
49 		unsigned int	no_sync:1;	/* <58> sw - ghost unload */
50 		unsigned int	lock:1;		/* <57> sw - locked */
51 		unsigned int	susp:1;		/* <56> sw - suspend? */
52 		unsigned int	pahi:24;	/* <55:32> pa */
53 		/* ------------------- */
54 		unsigned int	palo:19;	/* <31:13> pa */
55 		unsigned int	ie:1;		/* <12> 1=invert endianness */
56 		unsigned int	e:1;		/* <11> side effect */
57 		unsigned int	cp:1;		/* <10> physically cache */
58 		unsigned int	cv:1;		/* <9> virtually cache */
59 		unsigned int	p:1;		/* <8> privilege required */
60 		unsigned int	x:1;		/* <7> execute perm */
61 		unsigned int	w:1;		/* <6> write perm */
62 		unsigned int	ref:1;		/* <5> sw - ref */
63 		unsigned int	wr_perm:1;	/* <4> sw - write perm */
64 		unsigned int	xsoft:1;	/* <3> sw - soft execute */
65 		unsigned int	sz:3;		/* <2:0> pagesize */
66 	} tte_bit;
67 	struct {
68 		int32_t		inthi;
69 		uint32_t	intlo;
70 	} tte_int;
71 	uint64_t		ll;
72 } tte_t;
73 
74 #define	tte_val 	tte_bit.v		/* use < 0 check in asm */
75 #define	tte_size	tte_bit.sz
76 #define	tte_nfo		tte_bit.nfo
77 #define	tte_ie		tte_bit.ie		/* XXX? */
78 #define	tte_hmenum	tte_bit.hmenum
79 #define	tte_pahi	tte_bit.pahi
80 #define	tte_palo	tte_bit.palo
81 #define	tte_ref		tte_bit.ref
82 #define	tte_wr_perm	tte_bit.wr_perm
83 #define	tte_no_sync	tte_bit.no_sync
84 #define	tte_suspend	tte_bit.susp
85 #define	tte_exec_perm	tte_bit.x
86 #define	tte_soft_exec	tte_bit.xsoft
87 #define	tte_lock	tte_bit.lock
88 #define	tte_cp		tte_bit.cp
89 #define	tte_cv		tte_bit.cv
90 #define	tte_se		tte_bit.e
91 #define	tte_priv	tte_bit.p
92 #define	tte_hwwr	tte_bit.w
93 
94 #define	tte_inthi	tte_int.inthi
95 #define	tte_intlo	tte_int.intlo
96 
97 #endif /* !_ASM */
98 
99 /* Defines for sz field in tte */
100 #define	TTE8K			0x0
101 #define	TTE64K			0x1
102 #define	TTE512K			0x2
103 #define	TTE4M			0x3
104 #define	TTE32M			0x4
105 #define	TTE256M			0x5
106 #define	TTE2G			0x6
107 #define	TTE16G			0x7
108 
109 #define	TTE_SZ_SHFT		0
110 #define	TTE_SZ_BITS		0x7
111 
112 #define	TTE_CSZ(ttep)	((ttep)->tte_size)
113 
114 #define	TTE_BSZS_SHIFT(sz)	((sz) * 3)
115 #define	TTEBYTES(sz)	(MMU_PAGESIZE << TTE_BSZS_SHIFT(sz))
116 #define	TTEPAGES(sz)	(1 << TTE_BSZS_SHIFT(sz))
117 #define	TTE_PAGE_SHIFT(sz)	(MMU_PAGESHIFT + TTE_BSZS_SHIFT(sz))
118 #define	TTE_PAGE_OFFSET(sz)	(TTEBYTES(sz) - 1)
119 #define	TTE_PAGEMASK(sz)	(~TTE_PAGE_OFFSET(sz))
120 #define	TTE_PFNMASK(sz)	(~(TTE_PAGE_OFFSET(sz) >> MMU_PAGESHIFT))
121 
122 #define	TTE_PA_LSHIFT	8	/* used to do sllx on tte to get pa */
123 
124 #ifndef _ASM
125 
126 #define	TTE_PASHIFT	19	/* used to manage pahi and palo */
127 #define	TTE_PALOMASK	((1 << TTE_PASHIFT) -1)
128 /* PFN is defined as bits [40-13] of the physical address */
129 #define	TTE_TO_TTEPFN(ttep)						\
130 	((((ttep)->tte_pahi << TTE_PASHIFT) | (ttep)->tte_palo) &	\
131 	TTE_PFNMASK(TTE_CSZ(ttep)))
132 /*
133  * This define adds the vaddr page offset to obtain a correct pfn
134  */
135 #define	TTE_TO_PFN(vaddr, ttep)						\
136 	(sfmmu_ttetopfn(ttep, vaddr))
137 
138 #define	PFN_TO_TTE(entry, pfn) {			\
139 	entry.tte_pahi = pfn >> TTE_PASHIFT;	\
140 	entry.tte_palo = pfn & TTE_PALOMASK;	\
141 	}
142 
143 #endif /* !_ASM */
144 
145 /*
146  * The tte defines are separated into integers because the compiler doesn't
147  * support 64bit defines.
148  */
149 /* Defines for tte using inthi */
150 #define	TTE_VALID_INT			0x80000000
151 #define	TTE_NFO_INT			0x40000000
152 #define	TTE_NOSYNC_INT			0x04000000
153 #define	TTE_SUSPEND			0x01000000
154 #define	TTE_SUSPEND_SHIFT		32
155 
156 /* Defines for tte using intlo */
157 #define	TTE_IE_INT			0x00001000
158 #define	TTE_SIDEFF_INT			0x00000800
159 #define	TTE_CP_INT			0x00000400
160 #define	TTE_CV_INT			0x00000200
161 #define	TTE_PRIV_INT			0x00000100
162 #define	TTE_EXECPRM_INT			0x00000080
163 #define	TTE_HWWR_INT			0x00000040
164 #define	TTE_REF_INT			0x00000020
165 #define	TTE_WRPRM_INT			0x00000010
166 #define	TTE_SOFTEXEC_INT		0x00000008
167 
168 #define	TTE_PROT_INT			(TTE_WRPRM_INT | TTE_PRIV_INT)
169 
170 #ifndef ASM
171 
172 /* Defines to help build ttes using inthi */
173 #define	TTE_SZ_INTLO(sz)		((sz) & TTE_SZ_BITS)
174 #define	TTE_HMENUM_INT(hmenum)		((hmenum) << 27)
175 
176 /* PFN is defined as bits [40-13] of the physical address */
177 #define	TTE_PFN_INTHI(pfn)		((pfn) >> TTE_PASHIFT)
178 #define	TTE_VALID_CHECK(attr)	\
179 	(((attr) & PROT_ALL) ? TTE_VALID_INT : 0)
180 #define	TTE_NFO_CHECK(attr)	\
181 	(((attr) & HAT_NOFAULT) ? TTE_NFO_INT : 0)
182 
183 /* Defines to help build ttes using intlo */
184 #define	TTE_PFN_INTLO(pfn)		(((pfn) & TTE_PALOMASK) << 13)
185 #define	TTE_IE_CHECK(attr)	\
186 	(((attr) & HAT_STRUCTURE_LE) ? TTE_IE_INT : 0)
187 #define	TTE_WRPRM_CHECK(attr)	 \
188 	(((attr) & PROT_WRITE) ? TTE_WRPRM_INT : 0)
189 #define	TTE_EXECPRM_CHECK(attr)	 \
190 	(((attr) & PROT_EXEC) ? TTE_EXECPRM_INT : 0)
191 #define	TTE_NOSYNC_CHECK(attr)	 \
192 	(((attr) & HAT_NOSYNC) ? TTE_NOSYNC_INT : 0)
193 #define	TTE_CP_CHECK(attr)	\
194 	(((attr) & SFMMU_UNCACHEPTTE) ? 0: TTE_CP_INT)
195 #define	TTE_CV_CHECK(attr)	\
196 	(((attr) & SFMMU_UNCACHEVTTE) ? 0: TTE_CV_INT)
197 #define	TTE_SE_CHECK(attr)	\
198 	(((attr) & SFMMU_SIDEFFECT) ? TTE_SIDEFF_INT : 0)
199 #define	TTE_PRIV_CHECK(attr)	\
200 	(((attr) & PROT_USER) ? 0 : TTE_PRIV_INT)
201 
202 #define	MAKE_TTEATTR_INTHI(attr)				\
203 	(TTE_VALID_CHECK(attr) | TTE_NFO_CHECK(attr))
204 
205 #define	MAKE_TTE_INTHI(pfn, attr, sz, hmenum)			\
206 	(MAKE_TTEATTR_INTHI(attr) | TTE_HMENUM_INT(hmenum) |	\
207 	TTE_NOSYNC_CHECK(attr) | TTE_PFN_INTHI(pfn))
208 
209 #define	MAKE_TTEATTR_INTLO(attr)					\
210 	(TTE_WRPRM_CHECK(attr) | TTE_CP_CHECK(attr) | TTE_CV_CHECK(attr) | \
211 	TTE_SE_CHECK(attr) | TTE_PRIV_CHECK(attr) | TTE_EXECPRM_CHECK(attr) | \
212 	TTE_IE_CHECK(attr))
213 
214 #define	MAKE_TTE_INTLO(pfn, attr, sz, hmenum)				\
215 	(TTE_PFN_INTLO(pfn) | TTE_REF_INT | MAKE_TTEATTR_INTLO(attr) | \
216 	TTE_SZ_INTLO(sz))
217 
218 #define	TTEINTHI_ATTR	(TTE_VALID_INT | TTE_NFO_INT | TTE_NOSYNC_INT)
219 
220 #define	TTEINTLO_ATTR							\
221 	(TTE_IE_INT | TTE_WRPRM_INT | TTE_CP_INT | TTE_CV_INT |		\
222 	TTE_SIDEFF_INT | TTE_PRIV_INT | TTE_EXECPRM_INT)
223 
224 #define	MAKE_TTE_MASK(ttep)				\
225 	{						\
226 		(ttep)->tte_bit.v = 1;			\
227 		(ttep)->tte_bit.nfo = 1;		\
228 		(ttep)->tte_bit.pahi = 0xffffff;	\
229 		(ttep)->tte_bit.palo = 0x7ffff;		\
230 		(ttep)->tte_bit.ie = 1;			\
231 		(ttep)->tte_bit.e = 1;			\
232 		(ttep)->tte_bit.cp = 1;			\
233 		(ttep)->tte_bit.cv = 1;			\
234 		(ttep)->tte_bit.p = 1;			\
235 		(ttep)->tte_bit.x = 1;			\
236 		(ttep)->tte_bit.w = 1;			\
237 		(ttep)->tte_bit.sz = 7;			\
238 	}
239 
240 /*
241  * Defines to check/set TTE bits.
242  */
243 #define	TTE_IS_VALID(ttep)	((ttep)->tte_inthi < 0)
244 #define	TTE_SET_INVALID(ttep)	((ttep)->tte_val = 0)
245 #define	TTE_IS_8K(ttep)		(TTE_CSZ(ttep) == TTE8K)
246 #define	TTE_IS_WRITABLE(ttep)	((ttep)->tte_wr_perm)
247 #define	TTE_IS_EXECUTABLE(ttep)	((ttep)->tte_exec_perm)
248 #define	TTE_IS_SOFTEXEC(ttep)	((ttep)->tte_soft_exec)
249 #define	TTE_IS_PRIVILEGED(ttep)	((ttep)->tte_priv)
250 #define	TTE_IS_NOSYNC(ttep)	((ttep)->tte_no_sync)
251 #define	TTE_IS_LOCKED(ttep)	((ttep)->tte_lock)
252 #define	TTE_IS_SIDEFFECT(ttep)	((ttep)->tte_se)
253 #define	TTE_IS_NFO(ttep)	((ttep)->tte_nfo)
254 
255 #define	TTE_IS_REF(ttep)	((ttep)->tte_ref)
256 #define	TTE_IS_MOD(ttep)	((ttep)->tte_hwwr)
257 #define	TTE_IS_IE(ttep)		((ttep)->tte_ie)
258 #define	TTE_SET_SUSPEND(ttep)	((ttep)->tte_suspend = 1)
259 #define	TTE_CLR_SUSPEND(ttep)	((ttep)->tte_suspend = 0)
260 #define	TTE_IS_SUSPEND(ttep)	((ttep)->tte_suspend)
261 #define	TTE_SET_REF(ttep)	((ttep)->tte_ref = 1)
262 #define	TTE_CLR_REF(ttep)	((ttep)->tte_ref = 0)
263 #define	TTE_SET_LOCKED(ttep)	((ttep)->tte_lock = 1)
264 #define	TTE_CLR_LOCKED(ttep)	((ttep)->tte_lock = 0)
265 #define	TTE_SET_MOD(ttep)	((ttep)->tte_hwwr = 1)
266 #define	TTE_CLR_MOD(ttep)	((ttep)->tte_hwwr = 0)
267 #define	TTE_SET_RM(ttep)						\
268 	(((ttep)->tte_intlo) =						\
269 	(ttep)->tte_intlo | TTE_HWWR_INT | TTE_REF_INT)
270 #define	TTE_CLR_RM(ttep)						\
271 	(((ttep)->tte_intlo) =						\
272 	(ttep)->tte_intlo & ~(TTE_HWWR_INT | TTE_REF_INT))
273 
274 #define	TTE_SET_WRT(ttep)	((ttep)->tte_wr_perm = 1)
275 #define	TTE_CLR_WRT(ttep)	((ttep)->tte_wr_perm = 0)
276 #define	TTE_SET_EXEC(ttep)	((ttep)->tte_exec_perm = 1)
277 #define	TTE_CLR_EXEC(ttep)	((ttep)->tte_exec_perm = 0)
278 #define	TTE_SET_SOFTEXEC(ttep)	((ttep)->tte_soft_exec = 1)
279 #define	TTE_CLR_SOFTEXEC(ttep)	((ttep)->tte_soft_exec = 0)
280 #define	TTE_SET_PRIV(ttep)	((ttep)->tte_priv = 1)
281 #define	TTE_CLR_PRIV(ttep)	((ttep)->tte_priv = 0)
282 
283 #define	TTE_IS_VCACHEABLE(ttep)		((ttep)->tte_cv)
284 #define	TTE_SET_VCACHEABLE(ttep)	((ttep)->tte_cv = 1)
285 #define	TTE_CLR_VCACHEABLE(ttep)	((ttep)->tte_cv = 0)
286 #define	TTE_IS_PCACHEABLE(ttep)		((ttep)->tte_cp)
287 #define	TTE_SET_PCACHEABLE(ttep)	((ttep)->tte_cp = 1)
288 #define	TTE_CLR_PCACHEABLE(ttep)	((ttep)->tte_cp = 0)
289 
290 
291 #define	KPM_TTE_VCACHED(tte64, pfn, tte_sz)				\
292 	tte64 = ((uint64_t)TTE_VALID_INT << 32) |			\
293 	    ((uint64_t)((tte_sz) << TTE_SZ_SHFT)) |			\
294 	    (((pfn) >> TTE_BSZS_SHIFT(tte_sz)) <<			\
295 	    (TTE_BSZS_SHIFT(tte_sz) + MMU_PAGESHIFT)) |			\
296 	    (TTE_CP_INT | TTE_CV_INT | TTE_PRIV_INT | TTE_HWWR_INT)
297 
298 #define	KPM_TTE_VUNCACHED(tte64, pfn, tte_sz)				\
299 	tte64 = ((uint64_t)TTE_VALID_INT << 32) |			\
300 	    ((uint64_t)((tte_sz) << TTE_SZ_SHFT)) |			\
301 	    (((pfn) >> TTE_BSZS_SHIFT(tte_sz)) <<			\
302 	    (TTE_BSZS_SHIFT(tte_sz) + MMU_PAGESHIFT)) |			\
303 	    (TTE_CP_INT | TTE_PRIV_INT | TTE_HWWR_INT)
304 
305 
306 /*
307  * This define provides a generic method to set and clear multiple tte flags.
308  * A bitmask of all flags to be affected is passed in "flags" and a bitmask
309  * of the new values is passed in "newflags".
310  */
311 #define	TTE_SET_LOFLAGS(ttep, flags, newflags)				\
312 	((ttep)->tte_intlo = ((ttep)->tte_intlo & ~(flags)) | (newflags))
313 
314 #define	TTE_GET_LOFLAGS(ttep, flags)	((ttep)->tte_intlo & flags)
315 
316 #endif /* !_ASM */
317 
318 #ifdef	__cplusplus
319 }
320 #endif
321 
322 #endif /* !_SYS_PTE_H */
323