xref: /onnv-gate/usr/src/uts/sun4u/sys/zulu_hat.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #ifndef	__ZULU_HAT_INCL__
28*0Sstevel@tonic-gate #define	__ZULU_HAT_INCL__
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*0Sstevel@tonic-gate 
32*0Sstevel@tonic-gate #ifdef	__cplusplus
33*0Sstevel@tonic-gate extern "C" {
34*0Sstevel@tonic-gate #endif
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #define	ZULU_TTE8K		0
37*0Sstevel@tonic-gate #define	ZULU_TTE64K		1
38*0Sstevel@tonic-gate #define	ZULU_TTE512K		2
39*0Sstevel@tonic-gate #define	ZULU_TTE4M		3
40*0Sstevel@tonic-gate #define	ZULUM_MAX_PG_SIZES	4
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate #define	ZULU_CTX_MASK		0x1fff
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate #ifndef _ASM
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #include <sys/types.h>
47*0Sstevel@tonic-gate #include <sys/atomic.h>
48*0Sstevel@tonic-gate #include <vm/xhat.h>
49*0Sstevel@tonic-gate #include <sys/avl.h>
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate #define	ZULU_HAT_BP_SHIFT		13
53*0Sstevel@tonic-gate #define	ZULU_HAT_SZ_SHIFT(sz)		((sz) * 3)
54*0Sstevel@tonic-gate #define	ZULU_HAT_NUM_PGS(sz)		(1<<ZULU_HAT_SZ_SHIFT(sz))
55*0Sstevel@tonic-gate #define	ZULU_HAT_PGSHIFT(s)		(ZULU_HAT_BP_SHIFT + \
56*0Sstevel@tonic-gate 					ZULU_HAT_SZ_SHIFT(s))
57*0Sstevel@tonic-gate #define	ZULU_HAT_PGSZ(s)		((uint64_t)1<<ZULU_HAT_PGSHIFT(s))
58*0Sstevel@tonic-gate #define	ZULU_HAT_PGOFFSET(s)		(ZULU_HAT_PGSZ(s) - 1)
59*0Sstevel@tonic-gate #define	ZULU_HAT_PGMASK(s)		(~ZULU_HAT_PGOFFSET((uint64_t)s))
60*0Sstevel@tonic-gate #define	ZULU_HAT_PGADDR(a, s)		((uintptr_t)(a) & ZULU_HAT_PGMASK(s))
61*0Sstevel@tonic-gate #define	ZULU_HAT_PGADDROFF(a, s)	((uintptr_t)(a) & ZULU_HAT_PGOFFSET(s))
62*0Sstevel@tonic-gate #define	ZULU_HAT_PGDIFF(a, s)		(ZULU_HAT_PGSZ(s) - \
63*0Sstevel@tonic-gate 					ZULU_HAT_PGADDROFF(a, s))
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate #define	ZULU_HAT_PFN_MASK(sz)		((1 << ZULU_HAT_SZ_SHIFT(sz)) - 1)
66*0Sstevel@tonic-gate #define	ZULU_HAT_ADJ_PFN(ttep, vaddr) \
67*0Sstevel@tonic-gate 	((ttep->zulu_tte_pfn & ~ZULU_HAT_PFN_MASK(ttep->zulu_tte_size)) | \
68*0Sstevel@tonic-gate 	(((uintptr_t)vaddr >> ZULU_HAT_BP_SHIFT) & \
69*0Sstevel@tonic-gate 	ZULU_HAT_PFN_MASK(ttep->zulu_tte_size)))
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * zulu_ctx_tab is an array of pointers to zulu hat structures.
73*0Sstevel@tonic-gate  * since the addresses are 8 byte aligned we use bit 0 as a lock flag.
74*0Sstevel@tonic-gate  * This will synchronize TL1 access to the tsb and the mappings.
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate #define	ZULU_CTX_LOCK	0x1
78*0Sstevel@tonic-gate 
79*0Sstevel@tonic-gate #define	ZULU_CTX_LOCK_INIT(c)		zulu_ctx_tab[c] = NULL
80*0Sstevel@tonic-gate #define	ZULU_CTX_IS_FREE(c)		(zulu_ctx_tab[c] == NULL)
81*0Sstevel@tonic-gate #define	ZULU_CTX_SET_HAT(c, h) 		zulu_ctx_tab[c] = h
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate #define	ZULU_CTX_GET_HAT(c)	(struct zulu_hat *)((uint64_t) \
84*0Sstevel@tonic-gate 				    zulu_ctx_tab[c] & ~ZULU_CTX_LOCK)
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate struct zulu_tag {
87*0Sstevel@tonic-gate 	uint64_t	zulu_tag_page:51;	/* [63:13] vpage */
88*0Sstevel@tonic-gate };
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate struct zulu_tte {
91*0Sstevel@tonic-gate 	union {
92*0Sstevel@tonic-gate 		struct zulu_tag zulu_tte_tag;
93*0Sstevel@tonic-gate 		uint64_t 	zulu_tte_addr;
94*0Sstevel@tonic-gate 	} un;
95*0Sstevel@tonic-gate 	uint_t	zulu_tte_valid  :1;
96*0Sstevel@tonic-gate 	uint_t	zulu_tte_perm   :1;
97*0Sstevel@tonic-gate 	uint_t	zulu_tte_size   :3;
98*0Sstevel@tonic-gate 	uint_t	zulu_tte_locked :1;
99*0Sstevel@tonic-gate 	uint_t	zulu_tte_pfn;
100*0Sstevel@tonic-gate };
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate /*
103*0Sstevel@tonic-gate  * zulu hat stores its list of translation in a hash table.
104*0Sstevel@tonic-gate  * TODO: size this table. 256 buckets may be too small.
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate #define	ZULU_HASH_TBL_NUM  0x100
107*0Sstevel@tonic-gate #define	ZULU_HASH_TBL_MASK (ZULU_HASH_TBL_NUM - 1)
108*0Sstevel@tonic-gate #define	ZULU_HASH_TBL_SHIFT(_s) (ZULU_HAT_BP_SHIFT + (3 * _s))
109*0Sstevel@tonic-gate #define	ZULU_HASH_TBL_SZ  (ZULU_HASH_TBL_NUM * sizeof (struct zulu_hat_blk *))
110*0Sstevel@tonic-gate #define	ZULU_MAP_HASH_VAL(_v, _s) (((_v) >> ZULU_HASH_TBL_SHIFT(_s)) & \
111*0Sstevel@tonic-gate 							ZULU_HASH_TBL_MASK)
112*0Sstevel@tonic-gate #define	ZULU_MAP_HASH_HEAD(_zh, _v, _s) \
113*0Sstevel@tonic-gate 		(_zh->hash_tbl[ZULU_MAP_HASH_VAL(_v, _s)])
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate /*
116*0Sstevel@tonic-gate  *
117*0Sstevel@tonic-gate  * TODO: need finalize the number of entries in the TSB
118*0Sstevel@tonic-gate  * 32K tsb entries caused us to never get a tsb miss that didn't cause
119*0Sstevel@tonic-gate  * a page fault.
120*0Sstevel@tonic-gate  *
121*0Sstevel@tonic-gate  * Reducing TSB_NUM to 512 entries caused tsb_miss > tsb_hit
122*0Sstevel@tonic-gate  * in a yoyo run.
123*0Sstevel@tonic-gate  */
124*0Sstevel@tonic-gate #define	ZULU_TSB_NUM		4096
125*0Sstevel@tonic-gate #define	ZULU_TSB_SZ		(ZULU_TSB_NUM * sizeof (struct zulu_tte))
126*0Sstevel@tonic-gate #define	ZULU_TSB_HASH(a, ts, s)	(((uintptr_t)(a) >> ZULU_HAT_PGSHIFT(ts)) & \
127*0Sstevel@tonic-gate 					(s-1))
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate #define	ZULU_VADDR(tag)		(tag & ~ZULU_CTX_MASK)
130*0Sstevel@tonic-gate #define	ZULU_TTE_TO_PAGE(a)	a.un.zulu_tte_tag.zulu_tag_page
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate struct zulu_hat_blk {
134*0Sstevel@tonic-gate 	struct zulu_hat_blk	*zulu_hash_next;
135*0Sstevel@tonic-gate 	struct zulu_hat_blk	*zulu_hash_prev;
136*0Sstevel@tonic-gate 	struct zulu_shadow_blk  *zulu_shadow_blk;
137*0Sstevel@tonic-gate 	struct zulu_tte		zulu_hat_blk_tte;
138*0Sstevel@tonic-gate };
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate #define	zulu_hat_blk_vaddr	zulu_hat_blk_tte.un.zulu_tte_addr
141*0Sstevel@tonic-gate #define	zulu_hat_blk_pfn	zulu_hat_blk_tte.zulu_tte_pfn
142*0Sstevel@tonic-gate #define	zulu_hat_blk_page	ZULU_TTE_TO_PAGE(zulu_hat_blk_tte)
143*0Sstevel@tonic-gate #define	zulu_hat_blk_perm	zulu_hat_blk_tte.zulu_tte_perm
144*0Sstevel@tonic-gate #define	zulu_hat_blk_size	zulu_hat_blk_tte.zulu_tte_size
145*0Sstevel@tonic-gate #define	zulu_hat_blk_valid	zulu_hat_blk_tte.zulu_tte_valid
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * for fast lookups by address, len we use an avl list to shadow occupied
149*0Sstevel@tonic-gate  * 4Mb regions that have mappings.
150*0Sstevel@tonic-gate  */
151*0Sstevel@tonic-gate #define	ZULU_SHADOW_BLK_RANGE 0x400000
152*0Sstevel@tonic-gate #define	ZULU_SHADOW_BLK_MASK (~(ZULU_SHADOW_BLK_RANGE - 1))
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate struct zulu_shadow_blk {
155*0Sstevel@tonic-gate 	avl_node_t	link;		/* must be at beginning of struct */
156*0Sstevel@tonic-gate 	uint64_t	ivaddr;		/* base address of this node */
157*0Sstevel@tonic-gate 	uint64_t	ref_count;
158*0Sstevel@tonic-gate 	uint64_t	min_addr;
159*0Sstevel@tonic-gate 	uint64_t	max_addr;
160*0Sstevel@tonic-gate };
161*0Sstevel@tonic-gate #define	ZULU_SHADOW_BLK_LINK_OFFSET (0)
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate struct zulu_hat {
164*0Sstevel@tonic-gate 	struct xhat		zulu_xhat;
165*0Sstevel@tonic-gate 	kmutex_t		lock;
166*0Sstevel@tonic-gate 	avl_tree_t		shadow_tree;
167*0Sstevel@tonic-gate 	char			magic;	    /* =42 to mark our data for mdb */
168*0Sstevel@tonic-gate 	unsigned		in_fault  : 1;
169*0Sstevel@tonic-gate 	unsigned		freed	  : 1;
170*0Sstevel@tonic-gate 	unsigned		map8k	  : 1;
171*0Sstevel@tonic-gate 	unsigned		map64k	  : 1;
172*0Sstevel@tonic-gate 	unsigned		map512k	  : 1;
173*0Sstevel@tonic-gate 	unsigned		map4m	  : 1;
174*0Sstevel@tonic-gate 	short			zulu_ctx;
175*0Sstevel@tonic-gate 	unsigned short		zulu_tsb_size;	/* TODO why not a constant? */
176*0Sstevel@tonic-gate 	struct zulu_hat_blk	**hash_tbl;
177*0Sstevel@tonic-gate 	struct zulu_tte 	*zulu_tsb;
178*0Sstevel@tonic-gate 	struct zulu_shadow_blk	*sblk_last;	/* last sblk looked up */
179*0Sstevel@tonic-gate 	uint64_t		fault_ivaddr_last; /* last translation loaded */
180*0Sstevel@tonic-gate 	caddr_t			vaddr_max;
181*0Sstevel@tonic-gate 	hrtime_t		last_used;
182*0Sstevel@tonic-gate 	void			*zdev;
183*0Sstevel@tonic-gate };
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate #define	ZULU_HAT2AS(h)	((h)->zulu_xhat.xhat_as)
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate /*
188*0Sstevel@tonic-gate  * Assembly language function for TSB lookups
189*0Sstevel@tonic-gate  */
190*0Sstevel@tonic-gate uint64_t zulu_hat_tsb_lookup_tl0(struct zulu_hat *zhat, caddr_t vaddr);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate /*
193*0Sstevel@tonic-gate  * zuluvm's interface to zulu_hat
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate int zulu_hat_load(struct zulu_hat *zhat, caddr_t vaddr, enum seg_rw rw, int *);
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate int zulu_hat_init();
199*0Sstevel@tonic-gate int zulu_hat_destroy();
200*0Sstevel@tonic-gate int zulu_hat_attach(void *arg);
201*0Sstevel@tonic-gate int zulu_hat_detach(void *arg);
202*0Sstevel@tonic-gate struct zulu_hat *zulu_hat_proc_attach(struct as *as, void *zdev);
203*0Sstevel@tonic-gate void zulu_hat_proc_detach(struct zulu_hat *zhat);
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate void zulu_hat_validate_ctx(struct zulu_hat *zhat);
206*0Sstevel@tonic-gate void zulu_hat_terminate(struct zulu_hat *zhat);
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate #endif /* _ASM */
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate #ifdef	__cplusplus
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate #endif
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate #endif	/* __ZULU_HAT_INCL__ */
215