xref: /titanic_44/usr/src/uts/sfmmu/vm/hat_sfmmu.h (revision 1a5e258f5471356ca102c7176637cdce45bac147)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
525cf1a30Sjl139090  * Common Development and Distribution License (the "License").
625cf1a30Sjl139090  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b52a336eSPavel Tatashin  * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * VM - Hardware Address Translation management.
277c478bd9Sstevel@tonic-gate  *
289f1a1f17Sdmick  * This file describes the contents of the sun-reference-mmu(sfmmu)-
299f1a1f17Sdmick  * specific hat data structures and the sfmmu-specific hat procedures.
309f1a1f17Sdmick  * The machine-independent interface is described in <vm/hat.h>.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifndef	_VM_HAT_SFMMU_H
347c478bd9Sstevel@tonic-gate #define	_VM_HAT_SFMMU_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
377c478bd9Sstevel@tonic-gate extern "C" {
387c478bd9Sstevel@tonic-gate #endif
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #ifndef _ASM
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #endif /* _ASM */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include <sys/pte.h>
497c478bd9Sstevel@tonic-gate #include <vm/mach_sfmmu.h>
507c478bd9Sstevel@tonic-gate #include <sys/mmu.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Don't alter these without considering changes to ism_map_t.
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate #define	DEFAULT_ISM_PAGESIZE		MMU_PAGESIZE4M
561426d65aSsm142603 #define	DEFAULT_ISM_PAGESZC		TTE4M
577c478bd9Sstevel@tonic-gate #define	ISM_PG_SIZE(ism_vbshift)	(1 << ism_vbshift)
587c478bd9Sstevel@tonic-gate #define	ISM_SZ_MASK(ism_vbshift)	(ISM_PG_SIZE(ism_vbshift) - 1)
597c478bd9Sstevel@tonic-gate #define	ISM_MAP_SLOTS	8	/* Change this carefully. */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #ifndef _ASM
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
647c478bd9Sstevel@tonic-gate #include <vm/hat.h>
657c478bd9Sstevel@tonic-gate #include <vm/seg.h>
667c478bd9Sstevel@tonic-gate #include <sys/machparam.h>
677c478bd9Sstevel@tonic-gate #include <sys/systm.h>
687c478bd9Sstevel@tonic-gate #include <sys/x_call.h>
697c478bd9Sstevel@tonic-gate #include <vm/page.h>
707c478bd9Sstevel@tonic-gate #include <sys/ksynch.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate typedef struct hat sfmmu_t;
7305d3dc4bSpaulsan typedef struct sf_scd sf_scd_t;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * SFMMU attributes for hat_memload/hat_devload
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate #define	SFMMU_UNCACHEPTTE	0x01000000	/* unencache in physical $ */
797c478bd9Sstevel@tonic-gate #define	SFMMU_UNCACHEVTTE	0x02000000	/* unencache in virtual $ */
807c478bd9Sstevel@tonic-gate #define	SFMMU_SIDEFFECT		0x04000000	/* set side effect bit */
817c478bd9Sstevel@tonic-gate #define	SFMMU_LOAD_ALLATTR	(HAT_PROT_MASK | HAT_ORDER_MASK |	\
827c478bd9Sstevel@tonic-gate 		HAT_ENDIAN_MASK | HAT_NOFAULT | HAT_NOSYNC |		\
837c478bd9Sstevel@tonic-gate 		SFMMU_UNCACHEPTTE | SFMMU_UNCACHEVTTE | SFMMU_SIDEFFECT)
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
877c478bd9Sstevel@tonic-gate  * sfmmu flags for hat_memload/hat_devload
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate #define	SFMMU_NO_TSBLOAD	0x08000000	/* do not preload tsb */
907c478bd9Sstevel@tonic-gate #define	SFMMU_LOAD_ALLFLAG	(HAT_LOAD | HAT_LOAD_LOCK |		\
917c478bd9Sstevel@tonic-gate 		HAT_LOAD_ADV | HAT_LOAD_CONTIG | HAT_LOAD_NOCONSIST |	\
927c478bd9Sstevel@tonic-gate 		HAT_LOAD_SHARE | HAT_LOAD_REMAP | SFMMU_NO_TSBLOAD |	\
937c478bd9Sstevel@tonic-gate 		HAT_RELOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_TEXT)
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * sfmmu internal flag to hat_pageunload that spares locked mappings
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate #define	SFMMU_KERNEL_RELOC	0x8000
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /*
1017c478bd9Sstevel@tonic-gate  * mode for sfmmu_chgattr
1027c478bd9Sstevel@tonic-gate  */
1037c478bd9Sstevel@tonic-gate #define	SFMMU_SETATTR	0x0
1047c478bd9Sstevel@tonic-gate #define	SFMMU_CLRATTR	0x1
1057c478bd9Sstevel@tonic-gate #define	SFMMU_CHGATTR	0x2
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * sfmmu specific flags for page_t
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate #define	P_PNC	0x8		/* non-caching is permanent bit */
1117c478bd9Sstevel@tonic-gate #define	P_TNC	0x10		/* non-caching is temporary bit */
1127c478bd9Sstevel@tonic-gate #define	P_KPMS	0x20		/* kpm mapped small (vac alias prevention) */
1137c478bd9Sstevel@tonic-gate #define	P_KPMC	0x40		/* kpm conflict page (vac alias prevention) */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define	PP_GENERIC_ATTR(pp)	((pp)->p_nrm & (P_MOD | P_REF | P_RO))
1167c478bd9Sstevel@tonic-gate #define	PP_ISMOD(pp)		((pp)->p_nrm & P_MOD)
1177c478bd9Sstevel@tonic-gate #define	PP_ISREF(pp)		((pp)->p_nrm & P_REF)
1187c478bd9Sstevel@tonic-gate #define	PP_ISRO(pp)		((pp)->p_nrm & P_RO)
1197c478bd9Sstevel@tonic-gate #define	PP_ISNC(pp)		((pp)->p_nrm & (P_PNC|P_TNC))
1207c478bd9Sstevel@tonic-gate #define	PP_ISPNC(pp)		((pp)->p_nrm & P_PNC)
121fedab560Sae112802 #ifdef VAC
1227c478bd9Sstevel@tonic-gate #define	PP_ISTNC(pp)		((pp)->p_nrm & P_TNC)
123fedab560Sae112802 #endif
1247c478bd9Sstevel@tonic-gate #define	PP_ISKPMS(pp)		((pp)->p_nrm & P_KPMS)
1257c478bd9Sstevel@tonic-gate #define	PP_ISKPMC(pp)		((pp)->p_nrm & P_KPMC)
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #define	PP_SETMOD(pp)		((pp)->p_nrm |= P_MOD)
1287c478bd9Sstevel@tonic-gate #define	PP_SETREF(pp)		((pp)->p_nrm |= P_REF)
1297c478bd9Sstevel@tonic-gate #define	PP_SETREFMOD(pp)	((pp)->p_nrm |= (P_REF|P_MOD))
1307c478bd9Sstevel@tonic-gate #define	PP_SETRO(pp)		((pp)->p_nrm |= P_RO)
1317c478bd9Sstevel@tonic-gate #define	PP_SETREFRO(pp)		((pp)->p_nrm |= (P_REF|P_RO))
1327c478bd9Sstevel@tonic-gate #define	PP_SETPNC(pp)		((pp)->p_nrm |= P_PNC)
133fedab560Sae112802 #ifdef VAC
1347c478bd9Sstevel@tonic-gate #define	PP_SETTNC(pp)		((pp)->p_nrm |= P_TNC)
135fedab560Sae112802 #endif
1367c478bd9Sstevel@tonic-gate #define	PP_SETKPMS(pp)		((pp)->p_nrm |= P_KPMS)
1377c478bd9Sstevel@tonic-gate #define	PP_SETKPMC(pp)		((pp)->p_nrm |= P_KPMC)
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #define	PP_CLRMOD(pp)		((pp)->p_nrm &= ~P_MOD)
1407c478bd9Sstevel@tonic-gate #define	PP_CLRREF(pp)		((pp)->p_nrm &= ~P_REF)
1417c478bd9Sstevel@tonic-gate #define	PP_CLRREFMOD(pp)	((pp)->p_nrm &= ~(P_REF|P_MOD))
1427c478bd9Sstevel@tonic-gate #define	PP_CLRRO(pp)		((pp)->p_nrm &= ~P_RO)
1437c478bd9Sstevel@tonic-gate #define	PP_CLRPNC(pp)		((pp)->p_nrm &= ~P_PNC)
144fedab560Sae112802 #ifdef VAC
1457c478bd9Sstevel@tonic-gate #define	PP_CLRTNC(pp)		((pp)->p_nrm &= ~P_TNC)
146fedab560Sae112802 #endif
1477c478bd9Sstevel@tonic-gate #define	PP_CLRKPMS(pp)		((pp)->p_nrm &= ~P_KPMS)
1487c478bd9Sstevel@tonic-gate #define	PP_CLRKPMC(pp)		((pp)->p_nrm &= ~P_KPMC)
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * All shared memory segments attached with the SHM_SHARE_MMU flag (ISM)
1529f1a1f17Sdmick  * will be constrained to a 4M, 32M or 256M alignment. Also since every newly-
1537c478bd9Sstevel@tonic-gate  * created ISM segment is created out of a new address space at base va
1547c478bd9Sstevel@tonic-gate  * of 0 we don't need to store it.
1557c478bd9Sstevel@tonic-gate  */
1567c478bd9Sstevel@tonic-gate #define	ISM_ALIGN(shift)	(1 << shift)	/* base va aligned to <n>M  */
1577c478bd9Sstevel@tonic-gate #define	ISM_ALIGNED(shift, va)	(((uintptr_t)va & (ISM_ALIGN(shift) - 1)) == 0)
1587c478bd9Sstevel@tonic-gate #define	ISM_SHIFT(shift, x)	((uintptr_t)x >> (shift))
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate /*
1617c478bd9Sstevel@tonic-gate  * Pad locks out to cache sub-block boundaries to prevent
1627c478bd9Sstevel@tonic-gate  * false sharing, so several processes don't contend for
1637c478bd9Sstevel@tonic-gate  * the same line if they aren't using the same lock.  Since
1647c478bd9Sstevel@tonic-gate  * this is a typedef we also have a bit of freedom in
1657c478bd9Sstevel@tonic-gate  * changing lock implementations later if we decide it
1667c478bd9Sstevel@tonic-gate  * is necessary.
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate typedef struct hat_lock {
1697c478bd9Sstevel@tonic-gate 	kmutex_t hl_mutex;
1707c478bd9Sstevel@tonic-gate 	uchar_t hl_pad[64 - sizeof (kmutex_t)];
1717c478bd9Sstevel@tonic-gate } hatlock_t;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate #define	HATLOCK_MUTEXP(hatlockp)	(&((hatlockp)->hl_mutex))
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * All segments mapped with ISM are guaranteed to be 4M, 32M or 256M aligned.
1777c478bd9Sstevel@tonic-gate  * Also size is guaranteed to be in 4M, 32M or 256M chunks.
1787c478bd9Sstevel@tonic-gate  * ism_seg consists of the following members:
1797c478bd9Sstevel@tonic-gate  * [XX..22] base address of ism segment. XX is 63 or 31 depending whether
1807c478bd9Sstevel@tonic-gate  *	caddr_t is 64 bits or 32 bits.
1817c478bd9Sstevel@tonic-gate  * [21..0] size of segment.
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * NOTE: Don't alter this structure without changing defines above and
1847c478bd9Sstevel@tonic-gate  * the tsb_miss and protection handlers.
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate typedef struct ism_map {
1877c478bd9Sstevel@tonic-gate 	uintptr_t	imap_seg;  	/* base va + sz of ISM segment */
18805d3dc4bSpaulsan 	uchar_t		imap_vb_shift;	/* mmu_pageshift for ism page size */
18905d3dc4bSpaulsan 	uchar_t		imap_rid;	/* region id for ism */
1907c478bd9Sstevel@tonic-gate 	ushort_t	imap_hatflags;	/* primary ism page size */
1917c478bd9Sstevel@tonic-gate 	uint_t		imap_sz_mask;	/* mmu_pagemask for ism page size */
1927c478bd9Sstevel@tonic-gate 	sfmmu_t		*imap_ismhat; 	/* hat id of dummy ISM as */
1937c478bd9Sstevel@tonic-gate 	struct ism_ment	*imap_ment;	/* pointer to mapping list entry */
1947c478bd9Sstevel@tonic-gate } ism_map_t;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #define	ism_start(map)	((caddr_t)((map).imap_seg & \
1977c478bd9Sstevel@tonic-gate 				~ISM_SZ_MASK((map).imap_vb_shift)))
1987c478bd9Sstevel@tonic-gate #define	ism_size(map)	((map).imap_seg & ISM_SZ_MASK((map).imap_vb_shift))
1997c478bd9Sstevel@tonic-gate #define	ism_end(map)	((caddr_t)(ism_start(map) + (ism_size(map) * \
2007c478bd9Sstevel@tonic-gate 				ISM_PG_SIZE((map).imap_vb_shift))))
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * ISM mapping entry. Used to link all hat's sharing a ism_hat.
2037c478bd9Sstevel@tonic-gate  * Same function as the p_mapping list for a page.
2047c478bd9Sstevel@tonic-gate  */
2057c478bd9Sstevel@tonic-gate typedef struct ism_ment {
2067c478bd9Sstevel@tonic-gate 	sfmmu_t		*iment_hat;	/* back pointer to hat_share() hat */
2077c478bd9Sstevel@tonic-gate 	caddr_t		iment_base_va;	/* hat's va base for this ism seg */
2087c478bd9Sstevel@tonic-gate 	struct ism_ment	*iment_next;	/* next ism map entry */
2097c478bd9Sstevel@tonic-gate 	struct ism_ment	*iment_prev;	/* prev ism map entry */
2107c478bd9Sstevel@tonic-gate } ism_ment_t;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * ISM segment block. One will be hung off the sfmmu structure if a
2147c478bd9Sstevel@tonic-gate  * a process uses ISM.  More will be linked using ismblk_next if more
2157c478bd9Sstevel@tonic-gate  * than ISM_MAP_SLOTS segments are attached to this proc.
2167c478bd9Sstevel@tonic-gate  *
2177c478bd9Sstevel@tonic-gate  * All modifications to fields in this structure will be protected
2187c478bd9Sstevel@tonic-gate  * by the hat mutex.  In order to avoid grabbing this lock in low level
2197c478bd9Sstevel@tonic-gate  * routines (tsb miss/protection handlers and vatopfn) while not
2207c478bd9Sstevel@tonic-gate  * introducing any race conditions with hat_unshare, we will set
2217c478bd9Sstevel@tonic-gate  * CTX_ISM_BUSY bit in the ctx struct. Any mmu traps that occur
2227c478bd9Sstevel@tonic-gate  * for this ctx while this bit is set will be handled in sfmmu_tsb_excption
2237c478bd9Sstevel@tonic-gate  * where it will synchronize behind the hat mutex.
2247c478bd9Sstevel@tonic-gate  */
2257c478bd9Sstevel@tonic-gate typedef struct ism_blk {
2267c478bd9Sstevel@tonic-gate 	ism_map_t		iblk_maps[ISM_MAP_SLOTS];
2277c478bd9Sstevel@tonic-gate 	struct ism_blk		*iblk_next;
2287c478bd9Sstevel@tonic-gate 	uint64_t		iblk_nextpa;
2297c478bd9Sstevel@tonic-gate } ism_blk_t;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * TSB access information.  All fields are protected by the process's
2337c478bd9Sstevel@tonic-gate  * hat lock.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate struct tsb_info {
2377c478bd9Sstevel@tonic-gate 	caddr_t		tsb_va;		/* tsb base virtual address */
2387c478bd9Sstevel@tonic-gate 	uint64_t	tsb_pa;		/* tsb base physical address */
2397c478bd9Sstevel@tonic-gate 	struct tsb_info	*tsb_next;	/* next tsb used by this process */
2407c478bd9Sstevel@tonic-gate 	uint16_t	tsb_szc;	/* tsb size code */
2417c478bd9Sstevel@tonic-gate 	uint16_t	tsb_flags;	/* flags for this tsb; see below */
2427c478bd9Sstevel@tonic-gate 	uint_t		tsb_ttesz_mask;	/* page size masks; see below */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	tte_t		tsb_tte;	/* tte to lock into DTLB */
2457c478bd9Sstevel@tonic-gate 	sfmmu_t		*tsb_sfmmu;	/* sfmmu */
2467c478bd9Sstevel@tonic-gate 	kmem_cache_t	*tsb_cache;	/* cache from which mem allocated */
2477c478bd9Sstevel@tonic-gate 	vmem_t		*tsb_vmp;	/* vmem arena from which mem alloc'd */
2487c478bd9Sstevel@tonic-gate };
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate /*
2517c478bd9Sstevel@tonic-gate  * Values for "tsb_ttesz_mask" bitmask.
2527c478bd9Sstevel@tonic-gate  */
2537c478bd9Sstevel@tonic-gate #define	TSB8K	(1 << TTE8K)
2547c478bd9Sstevel@tonic-gate #define	TSB64K  (1 << TTE64K)
2557c478bd9Sstevel@tonic-gate #define	TSB512K (1 << TTE512K)
2567c478bd9Sstevel@tonic-gate #define	TSB4M   (1 << TTE4M)
2577c478bd9Sstevel@tonic-gate #define	TSB32M  (1 << TTE32M)
2587c478bd9Sstevel@tonic-gate #define	TSB256M (1 << TTE256M)
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Values for "tsb_flags" field.
2627c478bd9Sstevel@tonic-gate  */
2637c478bd9Sstevel@tonic-gate #define	TSB_RELOC_FLAG		0x1
2647c478bd9Sstevel@tonic-gate #define	TSB_FLUSH_NEEDED	0x2
2657c478bd9Sstevel@tonic-gate #define	TSB_SWAPPED	0x4
26605d3dc4bSpaulsan #define	TSB_SHAREDCTX		0x8
26705d3dc4bSpaulsan 
26805d3dc4bSpaulsan #endif	/* !_ASM */
26905d3dc4bSpaulsan 
27005d3dc4bSpaulsan /*
27105d3dc4bSpaulsan  * Data structures for shared hmeblk support.
27205d3dc4bSpaulsan  */
27305d3dc4bSpaulsan 
27405d3dc4bSpaulsan /*
27505d3dc4bSpaulsan  * Do not increase the maximum number of ism/hme regions without checking first
27605d3dc4bSpaulsan  * the impact on ism_map_t, TSB miss area, hblk tag and region id type in
27705d3dc4bSpaulsan  * sf_region structure.
27805d3dc4bSpaulsan  * Initially, shared hmes will only be used for the main text segment
27905d3dc4bSpaulsan  * therefore this value will be set to 64, it will be increased when shared
28005d3dc4bSpaulsan  * libraries are included.
28105d3dc4bSpaulsan  */
28205d3dc4bSpaulsan 
28305d3dc4bSpaulsan #define	SFMMU_MAX_HME_REGIONS		(64)
28405d3dc4bSpaulsan #define	SFMMU_HMERGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_HME_REGIONS)
28505d3dc4bSpaulsan 
28605d3dc4bSpaulsan #define	SFMMU_PRIVATE	0
28705d3dc4bSpaulsan #define	SFMMU_SHARED	1
28805d3dc4bSpaulsan 
2890a90a7fdSAmritpal Sandhu #define	HMEBLK_ENDPA	1
2900a90a7fdSAmritpal Sandhu 
29105d3dc4bSpaulsan #ifndef _ASM
29205d3dc4bSpaulsan 
29305d3dc4bSpaulsan #define	SFMMU_MAX_ISM_REGIONS		(64)
29405d3dc4bSpaulsan #define	SFMMU_ISMRGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_ISM_REGIONS)
29505d3dc4bSpaulsan 
29605d3dc4bSpaulsan #define	SFMMU_RGNMAP_WORDS	(SFMMU_HMERGNMAP_WORDS + SFMMU_ISMRGNMAP_WORDS)
29705d3dc4bSpaulsan 
29805d3dc4bSpaulsan #define	SFMMU_MAX_REGION_BUCKETS	(128)
29905d3dc4bSpaulsan #define	SFMMU_MAX_SRD_BUCKETS		(2048)
30005d3dc4bSpaulsan 
30105d3dc4bSpaulsan typedef struct sf_hmeregion_map {
30205d3dc4bSpaulsan 	ulong_t	bitmap[SFMMU_HMERGNMAP_WORDS];
30305d3dc4bSpaulsan } sf_hmeregion_map_t;
30405d3dc4bSpaulsan 
30505d3dc4bSpaulsan typedef struct sf_ismregion_map {
30605d3dc4bSpaulsan 	ulong_t	bitmap[SFMMU_ISMRGNMAP_WORDS];
30705d3dc4bSpaulsan } sf_ismregion_map_t;
30805d3dc4bSpaulsan 
30905d3dc4bSpaulsan typedef union sf_region_map_u {
31005d3dc4bSpaulsan 	struct _h_rmap_s {
31105d3dc4bSpaulsan 		sf_hmeregion_map_t hmeregion_map;
31205d3dc4bSpaulsan 		sf_ismregion_map_t ismregion_map;
31305d3dc4bSpaulsan 	} h_rmap_s;
31405d3dc4bSpaulsan 	ulong_t	bitmap[SFMMU_RGNMAP_WORDS];
31505d3dc4bSpaulsan } sf_region_map_t;
31605d3dc4bSpaulsan 
31705d3dc4bSpaulsan #define	SF_RGNMAP_ZERO(map) {				\
31805d3dc4bSpaulsan 	int _i;						\
31905d3dc4bSpaulsan 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {	\
32005d3dc4bSpaulsan 		(map).bitmap[_i] = 0;			\
32105d3dc4bSpaulsan 	}						\
32205d3dc4bSpaulsan }
32305d3dc4bSpaulsan 
32405d3dc4bSpaulsan /*
3259d0d62adSJason Beloro  * Returns 1 if map1 and map2 are equal.
32605d3dc4bSpaulsan  */
3279d0d62adSJason Beloro #define	SF_RGNMAP_EQUAL(map1, map2, rval)	{		\
32805d3dc4bSpaulsan 	int _i;							\
3299d0d62adSJason Beloro 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
33005d3dc4bSpaulsan 		if ((map1)->bitmap[_i] != (map2)->bitmap[_i])	\
33105d3dc4bSpaulsan 			break;					\
33205d3dc4bSpaulsan 	}							\
3339d0d62adSJason Beloro 	if (_i < SFMMU_RGNMAP_WORDS)				\
33405d3dc4bSpaulsan 		rval = 0;					\
33505d3dc4bSpaulsan 	else							\
33605d3dc4bSpaulsan 		rval = 1;					\
33705d3dc4bSpaulsan }
33805d3dc4bSpaulsan 
33905d3dc4bSpaulsan #define	SF_RGNMAP_ADD(map, r)		BT_SET((map).bitmap, r)
34005d3dc4bSpaulsan #define	SF_RGNMAP_DEL(map, r)		BT_CLEAR((map).bitmap, r)
34105d3dc4bSpaulsan #define	SF_RGNMAP_TEST(map, r)		BT_TEST((map).bitmap, r)
34205d3dc4bSpaulsan 
34305d3dc4bSpaulsan /*
34405d3dc4bSpaulsan  * Tests whether map2 is a subset of map1, returns 1 if
34505d3dc4bSpaulsan  * this assertion is true.
34605d3dc4bSpaulsan  */
34705d3dc4bSpaulsan #define	SF_RGNMAP_IS_SUBSET(map1, map2, rval)	{		\
34805d3dc4bSpaulsan 	int _i;							\
34905d3dc4bSpaulsan 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
35005d3dc4bSpaulsan 		if (((map1)->bitmap[_i]	& (map2)->bitmap[_i])	\
35105d3dc4bSpaulsan 		    != (map2)->bitmap[_i])  {	 		\
35205d3dc4bSpaulsan 			break;					\
35305d3dc4bSpaulsan 		}						\
35405d3dc4bSpaulsan 	}							\
35505d3dc4bSpaulsan 	if (_i < SFMMU_RGNMAP_WORDS)		 		\
35605d3dc4bSpaulsan 		rval = 0;					\
35705d3dc4bSpaulsan 	else							\
35805d3dc4bSpaulsan 		rval = 1;					\
35905d3dc4bSpaulsan }
36005d3dc4bSpaulsan 
36105d3dc4bSpaulsan #define	SF_SCD_INCR_REF(scdp) {						\
362*1a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32((volatile uint32_t *)&(scdp)->scd_refcnt);	\
36305d3dc4bSpaulsan }
36405d3dc4bSpaulsan 
36505d3dc4bSpaulsan #define	SF_SCD_DECR_REF(srdp, scdp) {				\
36605d3dc4bSpaulsan 	sf_region_map_t _scd_rmap = (scdp)->scd_region_map;	\
367*1a5e258fSJosef 'Jeff' Sipek 	if (!atomic_dec_32_nv((volatile uint32_t *)&(scdp)->scd_refcnt)) {\
36805d3dc4bSpaulsan 		sfmmu_destroy_scd((srdp), (scdp), &_scd_rmap);	\
36905d3dc4bSpaulsan 	}							\
37005d3dc4bSpaulsan }
37105d3dc4bSpaulsan 
37205d3dc4bSpaulsan /*
37305d3dc4bSpaulsan  * A sfmmup link in the link list of sfmmups that share the same region.
37405d3dc4bSpaulsan  */
37505d3dc4bSpaulsan typedef struct sf_rgn_link {
37605d3dc4bSpaulsan 	sfmmu_t	*next;
37705d3dc4bSpaulsan 	sfmmu_t *prev;
37805d3dc4bSpaulsan } sf_rgn_link_t;
37905d3dc4bSpaulsan 
38005d3dc4bSpaulsan /*
38105d3dc4bSpaulsan  * rgn_flags values.
38205d3dc4bSpaulsan  */
38305d3dc4bSpaulsan #define	SFMMU_REGION_HME	0x1
38405d3dc4bSpaulsan #define	SFMMU_REGION_ISM	0x2
38505d3dc4bSpaulsan #define	SFMMU_REGION_FREE	0x8
38605d3dc4bSpaulsan 
38705d3dc4bSpaulsan #define	SFMMU_REGION_TYPE_MASK	(0x3)
38805d3dc4bSpaulsan 
38905d3dc4bSpaulsan /*
39005d3dc4bSpaulsan  * sf_region defines a text or (D)ISM segment which map
39105d3dc4bSpaulsan  * the same underlying physical object.
39205d3dc4bSpaulsan  */
39305d3dc4bSpaulsan typedef struct sf_region {
39405d3dc4bSpaulsan 	caddr_t			rgn_saddr;   /* base addr of attached seg */
39505d3dc4bSpaulsan 	size_t			rgn_size;    /* size of attached seg */
39605d3dc4bSpaulsan 	void			*rgn_obj;    /* the underlying object id */
39705d3dc4bSpaulsan 	u_offset_t		rgn_objoff;  /* offset in the object mapped */
39805d3dc4bSpaulsan 	uchar_t			rgn_perm;    /* PROT_READ/WRITE/EXEC */
39905d3dc4bSpaulsan 	uchar_t			rgn_pgszc;   /* page size of the region */
40005d3dc4bSpaulsan 	uchar_t			rgn_flags;   /* region type, free flag */
40105d3dc4bSpaulsan 	uchar_t			rgn_id;
40205d3dc4bSpaulsan 	int			rgn_refcnt;  /* # of hats sharing the region */
40305d3dc4bSpaulsan 	/* callback function for hat_unload_callback */
40405d3dc4bSpaulsan 	hat_rgn_cb_func_t	rgn_cb_function;
40505d3dc4bSpaulsan 	struct sf_region	*rgn_hash;   /* hash chain linking the rgns */
40605d3dc4bSpaulsan 	kmutex_t		rgn_mutex;   /* protect region sfmmu list */
40705d3dc4bSpaulsan 	/* A link list of processes attached to this region */
40805d3dc4bSpaulsan 	sfmmu_t			*rgn_sfmmu_head;
40905d3dc4bSpaulsan 	ulong_t			rgn_ttecnt[MMU_PAGE_SIZES];
41005d3dc4bSpaulsan 	uint16_t		rgn_hmeflags; /* rgn tte size flags */
41105d3dc4bSpaulsan } sf_region_t;
41205d3dc4bSpaulsan 
41305d3dc4bSpaulsan #define	rgn_next	rgn_hash
41405d3dc4bSpaulsan 
41505d3dc4bSpaulsan /* srd */
41605d3dc4bSpaulsan typedef struct sf_shared_region_domain {
41705d3dc4bSpaulsan 	vnode_t			*srd_evp;	/* executable vnode */
41805d3dc4bSpaulsan 	/* hme region table */
41905d3dc4bSpaulsan 	sf_region_t		*srd_hmergnp[SFMMU_MAX_HME_REGIONS];
42005d3dc4bSpaulsan 	/* ism region table */
42105d3dc4bSpaulsan 	sf_region_t		*srd_ismrgnp[SFMMU_MAX_ISM_REGIONS];
42205d3dc4bSpaulsan 	/* hash chain linking srds */
42305d3dc4bSpaulsan 	struct sf_shared_region_domain *srd_hash;
42405d3dc4bSpaulsan 	/* pointer to the next free hme region */
42505d3dc4bSpaulsan 	sf_region_t		*srd_hmergnfree;
42605d3dc4bSpaulsan 	/* pointer to the next free ism region */
42705d3dc4bSpaulsan 	sf_region_t		*srd_ismrgnfree;
4287dacfc44Spaulsan 	/* id of next ism region created */
42905d3dc4bSpaulsan 	uint16_t		srd_next_ismrid;
4307dacfc44Spaulsan 	/* id of next hme region created */
43105d3dc4bSpaulsan 	uint16_t		srd_next_hmerid;
43205d3dc4bSpaulsan 	uint16_t		srd_ismbusyrgns; /* # of ism rgns in use */
43305d3dc4bSpaulsan 	uint16_t		srd_hmebusyrgns; /* # of hme rgns in use */
43405d3dc4bSpaulsan 	int			srd_refcnt;	 /* # of procs in the srd */
43505d3dc4bSpaulsan 	kmutex_t		srd_mutex;	 /* sync add/remove rgns */
43605d3dc4bSpaulsan 	kmutex_t		srd_scd_mutex;
43705d3dc4bSpaulsan 	sf_scd_t		*srd_scdp;	 /* list of scds in srd */
43805d3dc4bSpaulsan 	/* hash of regions associated with the same executable */
43905d3dc4bSpaulsan 	sf_region_t		*srd_rgnhash[SFMMU_MAX_REGION_BUCKETS];
44005d3dc4bSpaulsan } sf_srd_t;
44105d3dc4bSpaulsan 
44205d3dc4bSpaulsan typedef struct sf_srd_bucket {
44305d3dc4bSpaulsan 	kmutex_t	srdb_lock;
44405d3dc4bSpaulsan 	sf_srd_t	*srdb_srdp;
44505d3dc4bSpaulsan } sf_srd_bucket_t;
44605d3dc4bSpaulsan 
44705d3dc4bSpaulsan /*
44805d3dc4bSpaulsan  * The value of SFMMU_L1_HMERLINKS and SFMMU_L2_HMERLINKS will be increased
44905d3dc4bSpaulsan  * to 16 when the use of shared hmes for shared libraries is enabled.
45005d3dc4bSpaulsan  */
45105d3dc4bSpaulsan 
45205d3dc4bSpaulsan #define	SFMMU_L1_HMERLINKS		(8)
45305d3dc4bSpaulsan #define	SFMMU_L2_HMERLINKS		(8)
45405d3dc4bSpaulsan #define	SFMMU_L1_HMERLINKS_SHIFT	(3)
45505d3dc4bSpaulsan #define	SFMMU_L1_HMERLINKS_MASK		(SFMMU_L1_HMERLINKS - 1)
45605d3dc4bSpaulsan #define	SFMMU_L2_HMERLINKS_MASK		(SFMMU_L2_HMERLINKS - 1)
45705d3dc4bSpaulsan #define	SFMMU_L1_HMERLINKS_SIZE		\
45805d3dc4bSpaulsan 	(SFMMU_L1_HMERLINKS * sizeof (sf_rgn_link_t *))
45905d3dc4bSpaulsan #define	SFMMU_L2_HMERLINKS_SIZE		\
46005d3dc4bSpaulsan 	(SFMMU_L2_HMERLINKS * sizeof (sf_rgn_link_t))
46105d3dc4bSpaulsan 
46205d3dc4bSpaulsan #if (SFMMU_L1_HMERLINKS * SFMMU_L2_HMERLINKS < SFMMU_MAX_HME_REGIONS)
46305d3dc4bSpaulsan #error Not Enough HMERLINKS
46405d3dc4bSpaulsan #endif
46505d3dc4bSpaulsan 
46605d3dc4bSpaulsan /*
46705d3dc4bSpaulsan  * This macro grabs hat lock and allocates level 2 hat chain
46805d3dc4bSpaulsan  * associated with a shme rgn. In the majority of cases, the macro
46905d3dc4bSpaulsan  * is called with alloc = 0, and lock = 0.
4707dacfc44Spaulsan  * A pointer to the level 2 sf_rgn_link_t structure is returned in the lnkp
4717dacfc44Spaulsan  * parameter.
47205d3dc4bSpaulsan  */
47305d3dc4bSpaulsan #define	SFMMU_HMERID2RLINKP(sfmmup, rid, lnkp, alloc, lock)		\
47405d3dc4bSpaulsan {									\
47505d3dc4bSpaulsan 	int _l1ix = ((rid) >> SFMMU_L1_HMERLINKS_SHIFT) &		\
47605d3dc4bSpaulsan 	    SFMMU_L1_HMERLINKS_MASK;					\
47705d3dc4bSpaulsan 	int _l2ix = ((rid) & SFMMU_L2_HMERLINKS_MASK);			\
47805d3dc4bSpaulsan 	hatlock_t *_hatlockp;						\
47905d3dc4bSpaulsan 	lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];			\
48005d3dc4bSpaulsan 	if (lnkp != NULL) {						\
48105d3dc4bSpaulsan 		lnkp = &lnkp[_l2ix];					\
48205d3dc4bSpaulsan 	} else if (alloc && lock) {					\
48305d3dc4bSpaulsan 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
48405d3dc4bSpaulsan 		_hatlockp = sfmmu_hat_enter(sfmmup);			\
48505d3dc4bSpaulsan 		if ((sfmmup)->sfmmu_hmeregion_links[_l1ix] != NULL) {	\
48605d3dc4bSpaulsan 			sfmmu_hat_exit(_hatlockp);			\
48705d3dc4bSpaulsan 			kmem_free(lnkp, SFMMU_L2_HMERLINKS_SIZE);	\
48805d3dc4bSpaulsan 			lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];	\
48905d3dc4bSpaulsan 			ASSERT(lnkp != NULL);				\
49005d3dc4bSpaulsan 		} else {						\
49105d3dc4bSpaulsan 			(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;	\
49205d3dc4bSpaulsan 			sfmmu_hat_exit(_hatlockp);			\
49305d3dc4bSpaulsan 		}							\
49405d3dc4bSpaulsan 		lnkp = &lnkp[_l2ix];					\
49505d3dc4bSpaulsan 	} else if (alloc) {						\
49605d3dc4bSpaulsan 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
49705d3dc4bSpaulsan 		ASSERT((sfmmup)->sfmmu_hmeregion_links[_l1ix] == NULL);	\
49805d3dc4bSpaulsan 		(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;		\
49905d3dc4bSpaulsan 		lnkp = &lnkp[_l2ix];					\
50005d3dc4bSpaulsan 	}								\
50105d3dc4bSpaulsan }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate /*
5040a90a7fdSAmritpal Sandhu  *  Per cpu pending freelist of hmeblks.
5050a90a7fdSAmritpal Sandhu  */
5060a90a7fdSAmritpal Sandhu typedef struct cpu_hme_pend {
5070a90a7fdSAmritpal Sandhu 	struct   hme_blk *chp_listp;
5080a90a7fdSAmritpal Sandhu 	kmutex_t chp_mutex;
5090a90a7fdSAmritpal Sandhu 	time_t	 chp_timestamp;
5100a90a7fdSAmritpal Sandhu 	uint_t   chp_count;
5110a90a7fdSAmritpal Sandhu 	uint8_t	 chp_pad[36];		/* pad to 64 bytes */
5120a90a7fdSAmritpal Sandhu } cpu_hme_pend_t;
5130a90a7fdSAmritpal Sandhu 
5140a90a7fdSAmritpal Sandhu /*
5150a90a7fdSAmritpal Sandhu  * The default value of the threshold for the per cpu pending queues of hmeblks.
5160a90a7fdSAmritpal Sandhu  * The queues are flushed if either the number of hmeblks on the queue is above
5170a90a7fdSAmritpal Sandhu  * the threshold, or one second has elapsed since the last flush.
5180a90a7fdSAmritpal Sandhu  */
5190a90a7fdSAmritpal Sandhu #define	CPU_HME_PEND_THRESH 1000
5200a90a7fdSAmritpal Sandhu 
5210a90a7fdSAmritpal Sandhu /*
5221e2e7a75Shuah  * Per-MMU context domain kstats.
5231e2e7a75Shuah  *
5241e2e7a75Shuah  * TSB Miss Exceptions
5251e2e7a75Shuah  *	Number of times a TSB miss exception is handled in an MMU. See
5261e2e7a75Shuah  *	sfmmu_tsbmiss_exception() for more details.
5271e2e7a75Shuah  * TSB Raise Exception
5281e2e7a75Shuah  *	Number of times the CPUs within an MMU are cross-called
5291e2e7a75Shuah  *	to invalidate either a specific process context (when the process
5301e2e7a75Shuah  *	switches MMU contexts) or the context of any process that is
5311e2e7a75Shuah  *	running on those CPUs (as part of the MMU context wrap-around).
5321e2e7a75Shuah  * Wrap Around
5331e2e7a75Shuah  *	The number of times a wrap-around of MMU context happens.
5341e2e7a75Shuah  */
5351e2e7a75Shuah typedef enum mmu_ctx_stat_types {
5361e2e7a75Shuah 	MMU_CTX_TSB_EXCEPTIONS,		/* TSB miss exceptions handled */
5371e2e7a75Shuah 	MMU_CTX_TSB_RAISE_EXCEPTION,	/* ctx invalidation cross calls */
5381e2e7a75Shuah 	MMU_CTX_WRAP_AROUND,		/* wraparounds */
5391e2e7a75Shuah 	MMU_CTX_NUM_STATS
5401e2e7a75Shuah } mmu_ctx_stat_t;
5411e2e7a75Shuah 
5421e2e7a75Shuah /*
5431e2e7a75Shuah  * Per-MMU context domain structure. This is instantiated the first time a CPU
5441e2e7a75Shuah  * belonging to the MMU context domain is configured into the system, at boot
5451e2e7a75Shuah  * time or at DR time.
5461e2e7a75Shuah  *
5471e2e7a75Shuah  * mmu_gnum
5481e2e7a75Shuah  *	The current generation number for the context IDs on this MMU context
5491e2e7a75Shuah  *	domain. It is protected by mmu_lock.
5501e2e7a75Shuah  * mmu_cnum
5511e2e7a75Shuah  *	The current cnum to be allocated on this MMU context domain. It
5521e2e7a75Shuah  *	is protected via CAS.
5531e2e7a75Shuah  * mmu_nctxs
5541e2e7a75Shuah  *	The max number of context IDs supported on every CPU in this
555d2365b01SPavel Tatashin  *	MMU context domain. This is needed here in case the system supports
556d2365b01SPavel Tatashin  *      mixed type of processors/MMUs. It also helps to make ctx switch code
557d2365b01SPavel Tatashin  *      access fewer cache lines i.e. no need to retrieve it from some global
558d2365b01SPavel Tatashin  *      nctxs.
5591e2e7a75Shuah  * mmu_lock
5601e2e7a75Shuah  *	The mutex spin lock used to serialize context ID wrap around
5611e2e7a75Shuah  * mmu_idx
5621e2e7a75Shuah  *	The index for this MMU context domain structure in the global array
5631e2e7a75Shuah  *	mmu_ctxdoms.
5641e2e7a75Shuah  * mmu_ncpus
5651e2e7a75Shuah  *	The actual number of CPUs that have been configured in this
5661e2e7a75Shuah  *	MMU context domain. This also acts as a reference count for the
5671e2e7a75Shuah  *	structure. When the last CPU in an MMU context domain is unconfigured,
5681e2e7a75Shuah  *	the structure is freed. It is protected by mmu_lock.
5691e2e7a75Shuah  * mmu_cpuset
5701e2e7a75Shuah  *	The CPU set of configured CPUs for this MMU context domain. Used
5711e2e7a75Shuah  *	to cross-call all the CPUs in the MMU context domain to invalidate
5721e2e7a75Shuah  *	context IDs during a wraparound operation. It is protected by mmu_lock.
5731e2e7a75Shuah  */
5741e2e7a75Shuah 
5751e2e7a75Shuah typedef struct mmu_ctx {
5761e2e7a75Shuah 	uint64_t	mmu_gnum;
5771e2e7a75Shuah 	uint_t		mmu_cnum;
5781e2e7a75Shuah 	uint_t		mmu_nctxs;
5791e2e7a75Shuah 	kmutex_t	mmu_lock;
5801e2e7a75Shuah 	uint_t		mmu_idx;
5811e2e7a75Shuah 	uint_t		mmu_ncpus;
5821e2e7a75Shuah 	cpuset_t	mmu_cpuset;
5831e2e7a75Shuah 	kstat_t		*mmu_kstat;
5841e2e7a75Shuah 	kstat_named_t	mmu_kstat_data[MMU_CTX_NUM_STATS];
5851e2e7a75Shuah } mmu_ctx_t;
5861e2e7a75Shuah 
5871e2e7a75Shuah #define	mmu_tsb_exceptions	\
5881e2e7a75Shuah 		mmu_kstat_data[MMU_CTX_TSB_EXCEPTIONS].value.ui64
5891e2e7a75Shuah #define	mmu_tsb_raise_exception	\
5901e2e7a75Shuah 		mmu_kstat_data[MMU_CTX_TSB_RAISE_EXCEPTION].value.ui64
5911e2e7a75Shuah #define	mmu_wrap_around		\
5921e2e7a75Shuah 		mmu_kstat_data[MMU_CTX_WRAP_AROUND].value.ui64
5931e2e7a75Shuah 
5941e2e7a75Shuah extern uint_t		max_mmu_ctxdoms;
5951e2e7a75Shuah extern mmu_ctx_t	**mmu_ctxs_tbl;
5961e2e7a75Shuah 
5971e2e7a75Shuah extern void	sfmmu_cpu_init(cpu_t *);
5981e2e7a75Shuah extern void	sfmmu_cpu_cleanup(cpu_t *);
5991e2e7a75Shuah 
600d2365b01SPavel Tatashin extern uint_t	sfmmu_ctxdom_nctxs(int);
601d2365b01SPavel Tatashin 
602d2365b01SPavel Tatashin #ifdef sun4v
603d2365b01SPavel Tatashin extern void	sfmmu_ctxdoms_remove(void);
604d2365b01SPavel Tatashin extern void	sfmmu_ctxdoms_lock(void);
605d2365b01SPavel Tatashin extern void	sfmmu_ctxdoms_unlock(void);
606d2365b01SPavel Tatashin extern void	sfmmu_ctxdoms_update(void);
607d2365b01SPavel Tatashin #endif
608d2365b01SPavel Tatashin 
6091e2e7a75Shuah /*
6101e2e7a75Shuah  * The following structure is used to get MMU context domain information for
6111e2e7a75Shuah  * a CPU from the platform.
6121e2e7a75Shuah  *
6131e2e7a75Shuah  * mmu_idx
6141e2e7a75Shuah  *	The MMU context domain index within the global array mmu_ctxs
6151e2e7a75Shuah  * mmu_nctxs
6161e2e7a75Shuah  *	The number of context IDs supported in the MMU context domain
6171e2e7a75Shuah  */
6181e2e7a75Shuah typedef struct mmu_ctx_info {
6191e2e7a75Shuah 	uint_t		mmu_idx;
6201e2e7a75Shuah 	uint_t		mmu_nctxs;
6211e2e7a75Shuah } mmu_ctx_info_t;
6221e2e7a75Shuah 
6231e2e7a75Shuah #pragma weak plat_cpuid_to_mmu_ctx_info
6241e2e7a75Shuah 
6251e2e7a75Shuah extern void	plat_cpuid_to_mmu_ctx_info(processorid_t, mmu_ctx_info_t *);
6261e2e7a75Shuah 
6271e2e7a75Shuah /*
6281e2e7a75Shuah  * Each address space has an array of sfmmu_ctx_t structures, one structure
6291e2e7a75Shuah  * per MMU context domain.
6301e2e7a75Shuah  *
6311e2e7a75Shuah  * cnum
6321e2e7a75Shuah  *	The context ID allocated for an address space on an MMU context domain
6331e2e7a75Shuah  * gnum
6341e2e7a75Shuah  *	The generation number for the context ID in the MMU context domain.
6351e2e7a75Shuah  *
6361e2e7a75Shuah  * This structure needs to be a power-of-two in size.
6371e2e7a75Shuah  */
6381e2e7a75Shuah typedef struct sfmmu_ctx {
6391e2e7a75Shuah 	uint64_t	gnum:48;
6401e2e7a75Shuah 	uint64_t	cnum:16;
6411e2e7a75Shuah } sfmmu_ctx_t;
6421e2e7a75Shuah 
6439d0d62adSJason Beloro 
6441e2e7a75Shuah /*
6457c478bd9Sstevel@tonic-gate  * The platform dependent hat structure.
6467c478bd9Sstevel@tonic-gate  * tte counts should be protected by cas.
6477c478bd9Sstevel@tonic-gate  * cpuset is protected by cas.
6487c478bd9Sstevel@tonic-gate  *
6497dacfc44Spaulsan  * ttecnt accounting for mappings which do not use shared hme is carried out
6507dacfc44Spaulsan  * during pagefault handling. In the shared hme case, only the first process
6517dacfc44Spaulsan  * to access a mapping generates a pagefault, subsequent processes simply
6527dacfc44Spaulsan  * find the shared hme entry during trap handling and therefore there is no
6537dacfc44Spaulsan  * corresponding event to initiate ttecnt accounting. Currently, as shared
6547dacfc44Spaulsan  * hmes are only used for text segments, when joining a region we assume the
6557dacfc44Spaulsan  * worst case and add the the number of ttes required to map the entire region
6567dacfc44Spaulsan  * to the ttecnt corresponding to the region pagesize. However, if the region
6577dacfc44Spaulsan  * has a 4M pagesize, and memory is low, the allocation of 4M pages may fail
6587dacfc44Spaulsan  * then 8K pages will be allocated instead and the first TSB which stores 8K
6597dacfc44Spaulsan  * mappings will potentially be undersized. To compensate for the potential
6607dacfc44Spaulsan  * underaccounting in this case we always add 1/4 of the region size to the 8K
6617dacfc44Spaulsan  * ttecnt.
6627dacfc44Spaulsan  *
6637c478bd9Sstevel@tonic-gate  * Note that sfmmu_xhat_provider MUST be the first element.
6647c478bd9Sstevel@tonic-gate  */
6657dacfc44Spaulsan 
6667c478bd9Sstevel@tonic-gate struct hat {
6677c478bd9Sstevel@tonic-gate 	void		*sfmmu_xhat_provider;	/* NULL for CPU hat */
6687c478bd9Sstevel@tonic-gate 	cpuset_t	sfmmu_cpusran;	/* cpu bit mask for efficient xcalls */
6697c478bd9Sstevel@tonic-gate 	struct	as	*sfmmu_as;	/* as this hat provides mapping for */
67005d3dc4bSpaulsan 	/* per pgsz private ttecnt + shme rgns ttecnt for rgns not in SCD */
67105d3dc4bSpaulsan 	ulong_t		sfmmu_ttecnt[MMU_PAGE_SIZES];
67205d3dc4bSpaulsan 	/* shme rgns ttecnt for rgns in SCD */
67305d3dc4bSpaulsan 	ulong_t		sfmmu_scdrttecnt[MMU_PAGE_SIZES];
67405d3dc4bSpaulsan 	/* est. ism ttes that are NOT in a SCD */
67505d3dc4bSpaulsan 	ulong_t		sfmmu_ismttecnt[MMU_PAGE_SIZES];
67605d3dc4bSpaulsan 	/* ttecnt for isms that are in a SCD */
67705d3dc4bSpaulsan 	ulong_t		sfmmu_scdismttecnt[MMU_PAGE_SIZES];
67805d3dc4bSpaulsan 	/* inflate tsb0 to allow for large page alloc failure in region */
67905d3dc4bSpaulsan 	ulong_t		sfmmu_tsb0_4minflcnt;
6807c478bd9Sstevel@tonic-gate 	union _h_un {
6817c478bd9Sstevel@tonic-gate 		ism_blk_t	*sfmmu_iblkp;  /* maps to ismhat(s) */
6827c478bd9Sstevel@tonic-gate 		ism_ment_t	*sfmmu_imentp; /* ism hat's mapping list */
6837c478bd9Sstevel@tonic-gate 	} h_un;
6847c478bd9Sstevel@tonic-gate 	uint_t		sfmmu_free:1;	/* hat to be freed - set on as_free */
6857c478bd9Sstevel@tonic-gate 	uint_t		sfmmu_ismhat:1;	/* hat is dummy ism hatid */
68605d3dc4bSpaulsan 	uint_t		sfmmu_scdhat:1;	/* hat is dummy scd hatid */
6877c478bd9Sstevel@tonic-gate 	uchar_t		sfmmu_rmstat;	/* refmod stats refcnt */
6885d07b933Sdp78419 	ushort_t	sfmmu_clrstart;	/* start color bin for page coloring */
6897c478bd9Sstevel@tonic-gate 	ushort_t	sfmmu_clrbin;	/* per as phys page coloring bin */
6907c478bd9Sstevel@tonic-gate 	ushort_t	sfmmu_flags;	/* flags */
69105d3dc4bSpaulsan 	uchar_t		sfmmu_tteflags;	/* pgsz flags */
69205d3dc4bSpaulsan 	uchar_t		sfmmu_rtteflags; /* pgsz flags for SRD hmes */
6937c478bd9Sstevel@tonic-gate 	struct tsb_info	*sfmmu_tsb;	/* list of per as tsbs */
6947c478bd9Sstevel@tonic-gate 	uint64_t	sfmmu_ismblkpa; /* pa of sfmmu_iblkp, or -1 */
6951e2e7a75Shuah 	lock_t		sfmmu_ctx_lock;	/* sync ctx alloc and invalidation */
6967c478bd9Sstevel@tonic-gate 	kcondvar_t	sfmmu_tsb_cv;	/* signals TSB swapin or relocation */
6977c478bd9Sstevel@tonic-gate 	uchar_t		sfmmu_cext;	/* context page size encoding */
6987c478bd9Sstevel@tonic-gate 	uint8_t		sfmmu_pgsz[MMU_PAGE_SIZES];  /* ranking for MMU */
69905d3dc4bSpaulsan 	sf_srd_t	*sfmmu_srdp;
70005d3dc4bSpaulsan 	sf_scd_t	*sfmmu_scdp;	/* scd this address space belongs to */
70105d3dc4bSpaulsan 	sf_region_map_t	sfmmu_region_map;
70205d3dc4bSpaulsan 	sf_rgn_link_t	*sfmmu_hmeregion_links[SFMMU_L1_HMERLINKS];
70305d3dc4bSpaulsan 	sf_rgn_link_t	sfmmu_scd_link;	/* link to scd or pending queue */
7047c478bd9Sstevel@tonic-gate #ifdef sun4v
7057c478bd9Sstevel@tonic-gate 	struct hv_tsb_block sfmmu_hvblock;
7067c478bd9Sstevel@tonic-gate #endif
7071e2e7a75Shuah 	/*
7081e2e7a75Shuah 	 * sfmmu_ctxs is a variable length array of max_mmu_ctxdoms # of
7091e2e7a75Shuah 	 * elements. max_mmu_ctxdoms is determined at run-time.
7101e2e7a75Shuah 	 * sfmmu_ctxs[1] is just the fist element of an array, it always
7111e2e7a75Shuah 	 * has to be the last field to ensure that the memory allocated
7121e2e7a75Shuah 	 * for sfmmu_ctxs is consecutive with the memory of the rest of
7131e2e7a75Shuah 	 * the hat data structure.
7141e2e7a75Shuah 	 */
7151e2e7a75Shuah 	sfmmu_ctx_t	sfmmu_ctxs[1];
7161e2e7a75Shuah 
7177c478bd9Sstevel@tonic-gate };
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate #define	sfmmu_iblk	h_un.sfmmu_iblkp
7207c478bd9Sstevel@tonic-gate #define	sfmmu_iment	h_un.sfmmu_imentp
7217c478bd9Sstevel@tonic-gate 
72205d3dc4bSpaulsan #define	sfmmu_hmeregion_map	sfmmu_region_map.h_rmap_s.hmeregion_map
72305d3dc4bSpaulsan #define	sfmmu_ismregion_map	sfmmu_region_map.h_rmap_s.ismregion_map
72405d3dc4bSpaulsan 
72505d3dc4bSpaulsan #define	SF_RGNMAP_ISNULL(sfmmup)	\
72605d3dc4bSpaulsan 	(sfrgnmap_isnull(&(sfmmup)->sfmmu_region_map))
72705d3dc4bSpaulsan #define	SF_HMERGNMAP_ISNULL(sfmmup)	\
72805d3dc4bSpaulsan 	(sfhmergnmap_isnull(&(sfmmup)->sfmmu_hmeregion_map))
72905d3dc4bSpaulsan 
73005d3dc4bSpaulsan struct sf_scd {
73105d3dc4bSpaulsan 	sfmmu_t		*scd_sfmmup;	/* shared context hat */
73205d3dc4bSpaulsan 	/* per pgsz ttecnt for shme rgns in SCD */
73305d3dc4bSpaulsan 	ulong_t		scd_rttecnt[MMU_PAGE_SIZES];
73405d3dc4bSpaulsan 	uint_t		scd_refcnt;	/* address spaces attached to scd */
73505d3dc4bSpaulsan 	sf_region_map_t scd_region_map; /* bit mask of attached segments */
73605d3dc4bSpaulsan 	sf_scd_t	*scd_next;	/* link pointers for srd_scd list */
73705d3dc4bSpaulsan 	sf_scd_t	*scd_prev;
73805d3dc4bSpaulsan 	sfmmu_t 	*scd_sf_list;	/* list of doubly linked hat structs */
73905d3dc4bSpaulsan 	kmutex_t 	scd_mutex;
74005d3dc4bSpaulsan 	/*
74105d3dc4bSpaulsan 	 * Link used to add an scd to the sfmmu_iment list.
74205d3dc4bSpaulsan 	 */
74305d3dc4bSpaulsan 	ism_ment_t	scd_ism_links[SFMMU_MAX_ISM_REGIONS];
74405d3dc4bSpaulsan };
74505d3dc4bSpaulsan 
74605d3dc4bSpaulsan #define	scd_hmeregion_map	scd_region_map.h_rmap_s.hmeregion_map
74705d3dc4bSpaulsan #define	scd_ismregion_map	scd_region_map.h_rmap_s.ismregion_map
74805d3dc4bSpaulsan 
74905d3dc4bSpaulsan extern int disable_shctx;
75005d3dc4bSpaulsan extern int shctx_on;
75105d3dc4bSpaulsan 
7527c478bd9Sstevel@tonic-gate /*
7537c478bd9Sstevel@tonic-gate  * bit mask for managing vac conflicts on large pages.
7547c478bd9Sstevel@tonic-gate  * bit 1 is for uncache flag.
7557c478bd9Sstevel@tonic-gate  * bits 2 through min(num of cache colors + 1,31) are
7567c478bd9Sstevel@tonic-gate  * for cache colors that have already been flushed.
7577c478bd9Sstevel@tonic-gate  */
758fedab560Sae112802 #ifdef VAC
7597c478bd9Sstevel@tonic-gate #define	CACHE_NUM_COLOR		(shm_alignment >> MMU_PAGESHIFT)
760fedab560Sae112802 #else
761fedab560Sae112802 #define	CACHE_NUM_COLOR		1
762fedab560Sae112802 #endif
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate #define	CACHE_VCOLOR_MASK(vcolor)	(2 << (vcolor & (CACHE_NUM_COLOR - 1)))
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate #define	CacheColor_IsFlushed(flag, vcolor) \
7677c478bd9Sstevel@tonic-gate 					((flag) & CACHE_VCOLOR_MASK(vcolor))
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate #define	CacheColor_SetFlushed(flag, vcolor) \
7707c478bd9Sstevel@tonic-gate 					((flag) |= CACHE_VCOLOR_MASK(vcolor))
7717c478bd9Sstevel@tonic-gate /*
7727c478bd9Sstevel@tonic-gate  * Flags passed to sfmmu_page_cache to flush page from vac or not.
7737c478bd9Sstevel@tonic-gate  */
7747c478bd9Sstevel@tonic-gate #define	CACHE_FLUSH	0
7757c478bd9Sstevel@tonic-gate #define	CACHE_NO_FLUSH	1
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*
7787c478bd9Sstevel@tonic-gate  * Flags passed to sfmmu_tlbcache_demap
7797c478bd9Sstevel@tonic-gate  */
7807c478bd9Sstevel@tonic-gate #define	FLUSH_NECESSARY_CPUS	0
7817c478bd9Sstevel@tonic-gate #define	FLUSH_ALL_CPUS		1
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  * For debugging purpose only. Maybe removed later.
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate struct ctx_trace {
7887c478bd9Sstevel@tonic-gate 	sfmmu_t		*sc_sfmmu_stolen;
7897c478bd9Sstevel@tonic-gate 	sfmmu_t		*sc_sfmmu_stealing;
7907c478bd9Sstevel@tonic-gate 	clock_t		sc_time;
7917c478bd9Sstevel@tonic-gate 	ushort_t	sc_type;
7927c478bd9Sstevel@tonic-gate 	ushort_t	sc_cnum;
7937c478bd9Sstevel@tonic-gate };
7947c478bd9Sstevel@tonic-gate #define	CTX_TRC_STEAL	0x1
7957c478bd9Sstevel@tonic-gate #define	CTX_TRC_FREE	0x0
7967c478bd9Sstevel@tonic-gate #define	TRSIZE	0x400
7977c478bd9Sstevel@tonic-gate #define	NEXT_CTXTR(ptr)	(((ptr) >= ctx_trace_last) ? \
7987c478bd9Sstevel@tonic-gate 		ctx_trace_first : ((ptr) + 1))
7997c478bd9Sstevel@tonic-gate #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type) \
8007c478bd9Sstevel@tonic-gate 	mutex_enter(mutex);						\
8017c478bd9Sstevel@tonic-gate 	(ptr)->sc_sfmmu_stolen = (stolen_sfmmu);			\
8027c478bd9Sstevel@tonic-gate 	(ptr)->sc_sfmmu_stealing = (stealing_sfmmu);			\
8037c478bd9Sstevel@tonic-gate 	(ptr)->sc_cnum = (cnum);					\
8047c478bd9Sstevel@tonic-gate 	(ptr)->sc_type = (type);					\
805d3d50737SRafael Vanoni 	(ptr)->sc_time = ddi_get_lbolt();				\
8067c478bd9Sstevel@tonic-gate 	(ptr) = NEXT_CTXTR(ptr);					\
8077c478bd9Sstevel@tonic-gate 	num_ctx_stolen += (type);					\
8087c478bd9Sstevel@tonic-gate 	mutex_exit(mutex);
8097c478bd9Sstevel@tonic-gate #else
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type)
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate #endif	/* !_ASM */
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /*
8187c478bd9Sstevel@tonic-gate  * Macros for sfmmup->sfmmu_flags access.  The macros that change the flags
8197c478bd9Sstevel@tonic-gate  * ASSERT() that we're holding the HAT lock before changing the flags;
8207c478bd9Sstevel@tonic-gate  * however callers that read the flags may do so without acquiring the lock
8217c478bd9Sstevel@tonic-gate  * in a fast path, and then recheck the flag after acquiring the lock in
8227c478bd9Sstevel@tonic-gate  * a slow path.
8237c478bd9Sstevel@tonic-gate  */
8247c478bd9Sstevel@tonic-gate #define	SFMMU_FLAGS_ISSET(sfmmup, flags) \
8257c478bd9Sstevel@tonic-gate 	(((sfmmup)->sfmmu_flags & (flags)) == (flags))
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate #define	SFMMU_FLAGS_CLEAR(sfmmup, flags) \
8287c478bd9Sstevel@tonic-gate 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
8297c478bd9Sstevel@tonic-gate 	(sfmmup)->sfmmu_flags &= ~(flags))
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate #define	SFMMU_FLAGS_SET(sfmmup, flags) \
8327c478bd9Sstevel@tonic-gate 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
8337c478bd9Sstevel@tonic-gate 	(sfmmup)->sfmmu_flags |= (flags))
8347c478bd9Sstevel@tonic-gate 
83505d3dc4bSpaulsan #define	SFMMU_TTEFLAGS_ISSET(sfmmup, flags) \
83605d3dc4bSpaulsan 	((((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) & (flags)) == \
83705d3dc4bSpaulsan 	    (flags))
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate /*
84105d3dc4bSpaulsan  * sfmmu tte HAT flags, must fit in 8 bits
8427c478bd9Sstevel@tonic-gate  */
84305d3dc4bSpaulsan #define	HAT_CHKCTX1_FLAG 0x1
84405d3dc4bSpaulsan #define	HAT_64K_FLAG	(0x1 << TTE64K)
84505d3dc4bSpaulsan #define	HAT_512K_FLAG	(0x1 << TTE512K)
84605d3dc4bSpaulsan #define	HAT_4M_FLAG	(0x1 << TTE4M)
84705d3dc4bSpaulsan #define	HAT_32M_FLAG	(0x1 << TTE32M)
84805d3dc4bSpaulsan #define	HAT_256M_FLAG	(0x1 << TTE256M)
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
85105d3dc4bSpaulsan  * sfmmu HAT flags, 16 bits at the moment.
8527c478bd9Sstevel@tonic-gate  */
85305d3dc4bSpaulsan #define	HAT_4MTEXT_FLAG		0x01
85405d3dc4bSpaulsan #define	HAT_32M_ISM		0x02
85505d3dc4bSpaulsan #define	HAT_256M_ISM		0x04
85605d3dc4bSpaulsan #define	HAT_SWAPPED		0x08 /* swapped out */
85705d3dc4bSpaulsan #define	HAT_SWAPIN		0x10 /* swapping in */
85805d3dc4bSpaulsan #define	HAT_BUSY		0x20 /* replacing TSB(s) */
85905d3dc4bSpaulsan #define	HAT_ISMBUSY		0x40 /* adding/removing/traversing ISM maps */
86005d3dc4bSpaulsan 
86105d3dc4bSpaulsan #define	HAT_CTX1_FLAG   	0x100 /* ISM imap hatflag for ctx1 */
86205d3dc4bSpaulsan #define	HAT_JOIN_SCD		0x200 /* region is joining scd */
86305d3dc4bSpaulsan #define	HAT_ALLCTX_INVALID	0x400 /* all per-MMU ctxs are invalidated */
86405d3dc4bSpaulsan 
86505d3dc4bSpaulsan #define	SFMMU_LGPGS_INUSE(sfmmup)					\
86605d3dc4bSpaulsan 	(((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) ||	\
86705d3dc4bSpaulsan 	    ((sfmmup)->sfmmu_iblk != NULL))
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * Starting with context 0, the first NUM_LOCKED_CTXS contexts
8717c478bd9Sstevel@tonic-gate  * are locked so that sfmmu_getctx can't steal any of these
8727c478bd9Sstevel@tonic-gate  * contexts.  At the time this software was being developed, the
8737c478bd9Sstevel@tonic-gate  * only context that needs to be locked is context 0 (the kernel
8747c478bd9Sstevel@tonic-gate  * context), and context 1 (reserved for stolen context). So this constant
8757c478bd9Sstevel@tonic-gate  * was originally defined to be 2.
87660972f37Sjb145095  *
87760972f37Sjb145095  * For sun4v only, USER_CONTEXT_TYPE represents any user context.  Many
87860972f37Sjb145095  * routines only care whether the context is kernel, invalid or user.
8797c478bd9Sstevel@tonic-gate  */
88060972f37Sjb145095 
8817c478bd9Sstevel@tonic-gate #define	NUM_LOCKED_CTXS 2
8827c478bd9Sstevel@tonic-gate #define	INVALID_CONTEXT	1
8837c478bd9Sstevel@tonic-gate 
88460972f37Sjb145095 #ifdef sun4v
88560972f37Sjb145095 #define	USER_CONTEXT_TYPE	NUM_LOCKED_CTXS
88660972f37Sjb145095 #endif
8871426d65aSsm142603 #if defined(sun4v) || defined(UTSB_PHYS)
8881426d65aSsm142603 /*
8891426d65aSsm142603  * Get the location in the 4MB base TSB of the tsbe for this fault.
8901426d65aSsm142603  * Assumes that the second TSB only contains 4M mappings.
8911426d65aSsm142603  *
8921426d65aSsm142603  * In:
8931426d65aSsm142603  *   tagacc = tag access register (not clobbered)
8941426d65aSsm142603  *   tsbe = 2nd TSB base register
8951426d65aSsm142603  *   tmp1, tmp2 = scratch registers
8961426d65aSsm142603  * Out:
8971426d65aSsm142603  *   tsbe = pointer to the tsbe in the 2nd TSB
8981426d65aSsm142603  */
8991426d65aSsm142603 
9001426d65aSsm142603 #define	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9011426d65aSsm142603 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;	/* tmp2=szc */		\
9021426d65aSsm142603 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;	/* tsbbase */		\
9031426d65aSsm142603 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
9041426d65aSsm142603 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
9051426d65aSsm142603 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
9061426d65aSsm142603 	srlx	tagacc, MMU_PAGESHIFT4M, tmp2; 				\
9071426d65aSsm142603 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
9081426d65aSsm142603 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;	/* entry num --> ptr */	\
9091426d65aSsm142603 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
9101426d65aSsm142603 
9111426d65aSsm142603 #define	GET_2ND_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9121426d65aSsm142603 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
9131426d65aSsm142603 
9141426d65aSsm142603 /*
9151426d65aSsm142603  * Get the location in the 3rd TSB of the tsbe for this fault.
9161426d65aSsm142603  * The 3rd TSB corresponds to the shared context, and is used
9171426d65aSsm142603  * for 8K - 512k pages.
9181426d65aSsm142603  *
9191426d65aSsm142603  * In:
9201426d65aSsm142603  *   tagacc = tag access register (not clobbered)
9211426d65aSsm142603  *   tsbe, tmp1, tmp2 = scratch registers
9221426d65aSsm142603  * Out:
9231426d65aSsm142603  *   tsbe = pointer to the tsbe in the 3rd TSB
9241426d65aSsm142603  */
9251426d65aSsm142603 
9261426d65aSsm142603 #define	GET_3RD_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9271426d65aSsm142603 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;    /* tmp2=szc */		\
9281426d65aSsm142603 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;    /* tsbbase */		\
9291426d65aSsm142603 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
9301426d65aSsm142603 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
9311426d65aSsm142603 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
9321426d65aSsm142603 	srlx	tagacc, MMU_PAGESHIFT, tmp2;				\
9331426d65aSsm142603 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
9341426d65aSsm142603 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;    /* entry num --> ptr */	\
9351426d65aSsm142603 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
9361426d65aSsm142603 
9371426d65aSsm142603 #define	GET_4TH_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)                      \
9381426d65aSsm142603 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
9391426d65aSsm142603 /*
9401426d65aSsm142603  * Copy the sfmmu_region_map or scd_region_map to the tsbmiss
9411426d65aSsm142603  * shmermap or scd_shmermap, from sfmmu_load_mmustate.
9421426d65aSsm142603  */
9431426d65aSsm142603 #define	SET_REGION_MAP(rgn_map, tsbmiss_map, cnt, tmp, label)		\
9441426d65aSsm142603 	/* BEGIN CSTYLED */						\
9451426d65aSsm142603 label:									;\
9461426d65aSsm142603         ldx     [rgn_map], tmp						;\
9471426d65aSsm142603         dec     cnt							;\
9481426d65aSsm142603         add     rgn_map, CLONGSIZE, rgn_map                             ;\
9491426d65aSsm142603         stx     tmp, [tsbmiss_map]                                      ;\
9501426d65aSsm142603         brnz,pt cnt, label                                              ;\
9511426d65aSsm142603 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map                    \
9521426d65aSsm142603 	/* END CSTYLED */
9531426d65aSsm142603 
9541426d65aSsm142603 /*
9551426d65aSsm142603  * If there is no scd, then zero the tsbmiss scd_shmermap,
9561426d65aSsm142603  * from sfmmu_load_mmustate.
9571426d65aSsm142603  */
9581426d65aSsm142603 #define	ZERO_REGION_MAP(tsbmiss_map, cnt, label)                        \
9591426d65aSsm142603 	/* BEGIN CSTYLED */                                             \
9601426d65aSsm142603 label:                                                                  ;\
9611426d65aSsm142603         dec     cnt                                                     ;\
9621426d65aSsm142603         stx     %g0, [tsbmiss_map]                                      ;\
9631426d65aSsm142603         brnz,pt cnt, label                                              ;\
9641426d65aSsm142603 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map
9651426d65aSsm142603 	/* END CSTYLED */
9661426d65aSsm142603 
9671426d65aSsm142603 /*
9681426d65aSsm142603  * Set hmemisc to 1 if the shared hme is also part of an scd.
9691426d65aSsm142603  * In:
9701426d65aSsm142603  *   tsbarea = tsbmiss area (not clobbered)
9711426d65aSsm142603  *   hmeblkpa  = hmeblkpa +  hmentoff + SFHME_TTE (not clobbered)
9721426d65aSsm142603  *   hmentoff = hmentoff + SFHME_TTE = tte offset(clobbered)
9731426d65aSsm142603  * Out:
9741426d65aSsm142603  *   use_shctx = 1 if shme is in scd and 0 otherwise
9751426d65aSsm142603  */
9761426d65aSsm142603 #define	GET_SCDSHMERMAP(tsbarea, hmeblkpa, hmentoff, use_shctx)               \
9771426d65aSsm142603 	/* BEGIN CSTYLED */   	                                              \
9781426d65aSsm142603         sub     hmeblkpa, hmentoff, hmentoff    /* hmentofff = hmeblkpa */   ;\
9791426d65aSsm142603         add     hmentoff, HMEBLK_TAG, hmentoff                               ;\
9801426d65aSsm142603         ldxa    [hmentoff]ASI_MEM, hmentoff     /* read 1st part of tag */   ;\
9811426d65aSsm142603         and     hmentoff, HTAG_RID_MASK, hmentoff       /* mask off rid */   ;\
9821426d65aSsm142603         and     hmentoff, BT_ULMASK, use_shctx  /* mask bit index */         ;\
9831426d65aSsm142603         srlx    hmentoff, BT_ULSHIFT, hmentoff  /* extract word */           ;\
9841426d65aSsm142603         sllx    hmentoff, CLONGSHIFT, hmentoff  /* index */                  ;\
9851426d65aSsm142603         add     tsbarea, hmentoff, hmentoff             /* add to tsbarea */ ;\
9861426d65aSsm142603         ldx     [hmentoff + TSBMISS_SCDSHMERMAP], hmentoff      /* scdrgn */ ;\
9871426d65aSsm142603         srlx    hmentoff, use_shctx, use_shctx                               ;\
9881426d65aSsm142603         and     use_shctx, 0x1, use_shctx                                     \
9891426d65aSsm142603 	/* END CSTYLED */
9901426d65aSsm142603 
9911426d65aSsm142603 /*
9921426d65aSsm142603  * Synthesize a TSB base register contents for a process.
9931426d65aSsm142603  *
9941426d65aSsm142603  * In:
9951426d65aSsm142603  *   tsbinfo = TSB info pointer (ro)
9961426d65aSsm142603  *   tsbreg, tmp1 = scratch registers
9971426d65aSsm142603  * Out:
9981426d65aSsm142603  *   tsbreg = value to program into TSB base register
9991426d65aSsm142603  */
10001426d65aSsm142603 
10011426d65aSsm142603 #define	MAKE_UTSBREG(tsbinfo, tsbreg, tmp1)			\
10021426d65aSsm142603 	ldx	[tsbinfo + TSBINFO_PADDR], tsbreg;		\
10031426d65aSsm142603 	lduh	[tsbinfo + TSBINFO_SZCODE], tmp1;		\
10041426d65aSsm142603 	and	tmp1, TSB_SOFTSZ_MASK, tmp1;			\
10051426d65aSsm142603 	or	tsbreg, tmp1, tsbreg;
10061426d65aSsm142603 
10071426d65aSsm142603 
10081426d65aSsm142603 /*
10091426d65aSsm142603  * Load TSB base register to TSBMISS area for privte contexts.
10101426d65aSsm142603  * This register contains utsb_pabase in bits 63:13, and TSB size
10111426d65aSsm142603  * code in bits 2:0.
10121426d65aSsm142603  *
10131426d65aSsm142603  * For private context
10141426d65aSsm142603  * In:
10151426d65aSsm142603  *   tsbreg = value to load (ro)
10161426d65aSsm142603  *   regnum = constant or register
10171426d65aSsm142603  *   tmp1 = scratch register
10181426d65aSsm142603  * Out:
10191426d65aSsm142603  *   Specified scratchpad register updated
10201426d65aSsm142603  *
10211426d65aSsm142603  */
10221426d65aSsm142603 #define	SET_UTSBREG(regnum, tsbreg, tmp1)				\
10231426d65aSsm142603 	mov	regnum, tmp1;						\
10241426d65aSsm142603 	stxa	tsbreg, [tmp1]ASI_SCRATCHPAD	/* save tsbreg */
10251426d65aSsm142603 /*
10261426d65aSsm142603  * Get TSB base register from the scratchpad for private contexts
10271426d65aSsm142603  *
10281426d65aSsm142603  * In:
10291426d65aSsm142603  *   regnum = constant or register
10301426d65aSsm142603  *   tsbreg = scratch
10311426d65aSsm142603  * Out:
10321426d65aSsm142603  *   tsbreg = tsbreg from the specified scratchpad register
10331426d65aSsm142603  */
10341426d65aSsm142603 #define	GET_UTSBREG(regnum, tsbreg)					\
10351426d65aSsm142603 	mov	regnum, tsbreg;						\
10361426d65aSsm142603 	ldxa	[tsbreg]ASI_SCRATCHPAD, tsbreg
10371426d65aSsm142603 
10381426d65aSsm142603 /*
10391426d65aSsm142603  * Load TSB base register to TSBMISS area for shared contexts.
10401426d65aSsm142603  * This register contains utsb_pabase in bits 63:13, and TSB size
10411426d65aSsm142603  * code in bits 2:0.
10421426d65aSsm142603  *
10431426d65aSsm142603  * In:
10441426d65aSsm142603  *   tsbmiss = pointer to tsbmiss area
10451426d65aSsm142603  *   tsbmissoffset = offset to right tsb pointer
10461426d65aSsm142603  *   tsbreg = value to load (ro)
10471426d65aSsm142603  * Out:
10481426d65aSsm142603  *   Specified tsbmiss area updated
10491426d65aSsm142603  *
10501426d65aSsm142603  */
10511426d65aSsm142603 #define	SET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
10521426d65aSsm142603 	stx	tsbreg, [tsbmiss + tsbmissoffset]	/* save tsbreg */
10531426d65aSsm142603 
10541426d65aSsm142603 /*
10551426d65aSsm142603  * Get TSB base register from the scratchpad for
10561426d65aSsm142603  * shared contexts
10571426d65aSsm142603  *
10581426d65aSsm142603  * In:
10591426d65aSsm142603  *   tsbmiss = pointer to tsbmiss area
10601426d65aSsm142603  *   tsbmissoffset = offset to right tsb pointer
10611426d65aSsm142603  *   tsbreg = scratch
10621426d65aSsm142603  * Out:
10631426d65aSsm142603  *   tsbreg = tsbreg from the specified scratchpad register
10641426d65aSsm142603  */
10651426d65aSsm142603 #define	GET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
10661426d65aSsm142603 	ldx	[tsbmiss + tsbmissoffset], tsbreg
10671426d65aSsm142603 
10681426d65aSsm142603 #endif /* defined(sun4v) || defined(UTSB_PHYS) */
106960972f37Sjb145095 
10707c478bd9Sstevel@tonic-gate #ifndef	_ASM
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate /*
10737c478bd9Sstevel@tonic-gate  * Kernel page relocation stuff.
10747c478bd9Sstevel@tonic-gate  */
10757c478bd9Sstevel@tonic-gate struct sfmmu_callback {
10761bd5c35fSelowe 	int key;
10777c478bd9Sstevel@tonic-gate 	int (*prehandler)(caddr_t, uint_t, uint_t, void *);
10787c478bd9Sstevel@tonic-gate 	int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t);
10797c478bd9Sstevel@tonic-gate 	int (*errhandler)(caddr_t, uint_t, uint_t, void *);
10807c478bd9Sstevel@tonic-gate 	int capture_cpus;
10817c478bd9Sstevel@tonic-gate };
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate extern int sfmmu_max_cb_id;
10847c478bd9Sstevel@tonic-gate extern struct sfmmu_callback *sfmmu_cb_table;
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate struct pa_hment;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * RFE: With multihat gone we gain back an int.  We could use this to
10907c478bd9Sstevel@tonic-gate  * keep ref bits on a per cpu basis to eliminate xcalls.
10917c478bd9Sstevel@tonic-gate  */
10927c478bd9Sstevel@tonic-gate struct sf_hment {
10937c478bd9Sstevel@tonic-gate 	tte_t hme_tte;			/* tte for this hment */
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	union {
10967c478bd9Sstevel@tonic-gate 		struct page *page;	/* what page this maps */
10977c478bd9Sstevel@tonic-gate 		struct pa_hment *data;	/* pa_hment */
10987c478bd9Sstevel@tonic-gate 	} sf_hment_un;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	struct	sf_hment *hme_next;	/* next hment */
11017c478bd9Sstevel@tonic-gate 	struct	sf_hment *hme_prev;	/* prev hment */
11027c478bd9Sstevel@tonic-gate };
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate struct pa_hment {
11057c478bd9Sstevel@tonic-gate 	caddr_t		addr;		/* va */
11067c478bd9Sstevel@tonic-gate 	uint_t		len;		/* bytes */
11077c478bd9Sstevel@tonic-gate 	ushort_t	flags;		/* internal flags */
11087c478bd9Sstevel@tonic-gate 	ushort_t	refcnt;		/* reference count */
11097c478bd9Sstevel@tonic-gate 	id_t		cb_id;		/* callback id, table index */
11107c478bd9Sstevel@tonic-gate 	void		*pvt;		/* handler's private data */
11117c478bd9Sstevel@tonic-gate 	struct sf_hment	sfment;		/* corresponding dummy sf_hment */
11127c478bd9Sstevel@tonic-gate };
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate #define	hme_page		sf_hment_un.page
11157c478bd9Sstevel@tonic-gate #define	hme_data		sf_hment_un.data
11167c478bd9Sstevel@tonic-gate #define	hme_size(sfhmep)	((int)(TTE_CSZ(&(sfhmep)->hme_tte)))
11177c478bd9Sstevel@tonic-gate #define	PAHME_SZ		(sizeof (struct pa_hment))
11187c478bd9Sstevel@tonic-gate #define	SFHME_SZ		(sizeof (struct sf_hment))
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate #define	IS_PAHME(hme)	((hme)->hme_tte.ll == 0)
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate /*
11237c478bd9Sstevel@tonic-gate  * hmeblk_tag structure
11247c478bd9Sstevel@tonic-gate  * structure used to obtain a match on a hme_blk.  Currently consists of
11257c478bd9Sstevel@tonic-gate  * the address of the sfmmu struct (or hatid), the base page address of the
11267c478bd9Sstevel@tonic-gate  * hme_blk, and the rehash count.  The rehash count is actually only 2 bits
11277c478bd9Sstevel@tonic-gate  * and has the following meaning:
11287c478bd9Sstevel@tonic-gate  * 1 = 8k or 64k hash sequence.
11297c478bd9Sstevel@tonic-gate  * 2 = 512k hash sequence.
11307c478bd9Sstevel@tonic-gate  * 3 = 4M hash sequence.
11317c478bd9Sstevel@tonic-gate  * We require this count because we don't want to get a false hit on a 512K or
11327c478bd9Sstevel@tonic-gate  * 4M rehash with a base address corresponding to a 8k or 64k hmeblk.
11337c478bd9Sstevel@tonic-gate  * Note:  The ordering and size of the hmeblk_tag members are implictly known
11347c478bd9Sstevel@tonic-gate  * by the tsb miss handlers written in assembly.  Do not change this structure
11357c478bd9Sstevel@tonic-gate  * without checking those routines.  See HTAG_SFMMUPSZ define.
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate 
113805d3dc4bSpaulsan /*
113905d3dc4bSpaulsan  * In private hmeblks hblk_rid field must be SFMMU_INVALID_RID.
114005d3dc4bSpaulsan  */
11417c478bd9Sstevel@tonic-gate typedef union {
11427c478bd9Sstevel@tonic-gate 	struct {
11437c478bd9Sstevel@tonic-gate 		uint64_t	hblk_basepg: 51,	/* hme_blk base pg # */
114405d3dc4bSpaulsan 				hblk_rehash: 3,		/* rehash number */
114505d3dc4bSpaulsan 				hblk_rid: 10;		/* hme_blk region id */
114605d3dc4bSpaulsan 		void		*hblk_id;
11477c478bd9Sstevel@tonic-gate 	} hblk_tag_un;
11487c478bd9Sstevel@tonic-gate 	uint64_t		htag_tag[2];
11497c478bd9Sstevel@tonic-gate } hmeblk_tag;
11507c478bd9Sstevel@tonic-gate 
115105d3dc4bSpaulsan #define	htag_id		hblk_tag_un.hblk_id
11527c478bd9Sstevel@tonic-gate #define	htag_bspage	hblk_tag_un.hblk_basepg
11537c478bd9Sstevel@tonic-gate #define	htag_rehash	hblk_tag_un.hblk_rehash
115405d3dc4bSpaulsan #define	htag_rid	hblk_tag_un.hblk_rid
115505d3dc4bSpaulsan 
115605d3dc4bSpaulsan #endif /* !_ASM */
115705d3dc4bSpaulsan 
115805d3dc4bSpaulsan #define	HTAG_REHASH_SHIFT	10
115905d3dc4bSpaulsan #define	HTAG_MAX_RID	(((0x1 << HTAG_REHASH_SHIFT) - 1))
116005d3dc4bSpaulsan #define	HTAG_RID_MASK	HTAG_MAX_RID
116105d3dc4bSpaulsan 
116205d3dc4bSpaulsan /* used for tagging all per sfmmu (i.e. non SRD) private hmeblks */
116305d3dc4bSpaulsan #define	SFMMU_INVALID_SHMERID	HTAG_MAX_RID
116405d3dc4bSpaulsan 
116505d3dc4bSpaulsan #if SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
116605d3dc4bSpaulsan #error SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
116705d3dc4bSpaulsan #endif
116805d3dc4bSpaulsan 
116905d3dc4bSpaulsan #define	SFMMU_IS_SHMERID_VALID(rid)	((rid) != SFMMU_INVALID_SHMERID)
117005d3dc4bSpaulsan 
117105d3dc4bSpaulsan /* ISM regions */
117205d3dc4bSpaulsan #define	SFMMU_INVALID_ISMRID	0xff
117305d3dc4bSpaulsan 
117405d3dc4bSpaulsan #if SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
117505d3dc4bSpaulsan #error SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
117605d3dc4bSpaulsan #endif
117705d3dc4bSpaulsan 
117805d3dc4bSpaulsan #define	SFMMU_IS_ISMRID_VALID(rid)	((rid) != SFMMU_INVALID_ISMRID)
117905d3dc4bSpaulsan 
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate #define	HTAGS_EQ(tag1, tag2)	(((tag1.htag_tag[0] ^ tag2.htag_tag[0]) | \
11827c478bd9Sstevel@tonic-gate 				(tag1.htag_tag[1] ^ tag2.htag_tag[1])) == 0)
118305d3dc4bSpaulsan 
118405d3dc4bSpaulsan /*
118505d3dc4bSpaulsan  * this macro must only be used for comparing tags in shared hmeblks.
118605d3dc4bSpaulsan  */
118705d3dc4bSpaulsan #define	HTAGS_EQ_SHME(hmetag, tag, hrmap)				\
118805d3dc4bSpaulsan 	(((hmetag).htag_rid != SFMMU_INVALID_SHMERID) &&	        \
118905d3dc4bSpaulsan 	(((((hmetag).htag_tag[0] ^ (tag).htag_tag[0]) &			\
119005d3dc4bSpaulsan 		~HTAG_RID_MASK) |	        			\
119105d3dc4bSpaulsan 	    ((hmetag).htag_tag[1] ^ (tag).htag_tag[1])) == 0) &&	\
119205d3dc4bSpaulsan 	SF_RGNMAP_TEST(hrmap, hmetag.htag_rid))
119305d3dc4bSpaulsan 
11947c478bd9Sstevel@tonic-gate #define	HME_REHASH(sfmmup)						\
11957c478bd9Sstevel@tonic-gate 	((sfmmup)->sfmmu_ttecnt[TTE512K] != 0 ||			\
11967c478bd9Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE4M] != 0 ||				\
11977c478bd9Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE32M] != 0 ||				\
11987c478bd9Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE256M] != 0)
11997c478bd9Sstevel@tonic-gate 
12007c478bd9Sstevel@tonic-gate #define	NHMENTS		8		/* # of hments in an 8k hme_blk */
12017c478bd9Sstevel@tonic-gate 					/* needs to be multiple of 2 */
120205d3dc4bSpaulsan 
12037c478bd9Sstevel@tonic-gate #ifndef	_ASM
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate #ifdef	HBLK_TRACE
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate #define	HBLK_LOCK		1
12087c478bd9Sstevel@tonic-gate #define	HBLK_UNLOCK		0
12097c478bd9Sstevel@tonic-gate #define	HBLK_STACK_DEPTH	6
12107c478bd9Sstevel@tonic-gate #define	HBLK_AUDIT_CACHE_SIZE	16
12117c478bd9Sstevel@tonic-gate #define	HBLK_LOCK_PATTERN	0xaaaaaaaa
12127c478bd9Sstevel@tonic-gate #define	HBLK_UNLOCK_PATTERN	0xbbbbbbbb
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate struct hblk_lockcnt_audit {
12157c478bd9Sstevel@tonic-gate 	int		flag;		/* lock or unlock */
12167c478bd9Sstevel@tonic-gate 	kthread_id_t	thread;
12177c478bd9Sstevel@tonic-gate 	int		depth;
12187c478bd9Sstevel@tonic-gate 	pc_t		stack[HBLK_STACK_DEPTH];
12197c478bd9Sstevel@tonic-gate };
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate #endif	/* HBLK_TRACE */
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate /*
12257c478bd9Sstevel@tonic-gate  * Hment block structure.
12267c478bd9Sstevel@tonic-gate  * The hme_blk is the node data structure which the hash structure
12277c478bd9Sstevel@tonic-gate  * mantains. An hme_blk can have 2 different sizes depending on the
12287c478bd9Sstevel@tonic-gate  * number of hments it implicitly contains.  When dealing with 64K, 512K,
12297c478bd9Sstevel@tonic-gate  * or 4M hments there is one hment per hme_blk.  When dealing with
12307c478bd9Sstevel@tonic-gate  * 8k hments we allocate an hme_blk plus an additional 7 hments to
12317c478bd9Sstevel@tonic-gate  * give us a total of 8 (NHMENTS) hments that can be referenced through a
12327c478bd9Sstevel@tonic-gate  * hme_blk.
12337c478bd9Sstevel@tonic-gate  *
12347c478bd9Sstevel@tonic-gate  * The hmeblk structure contains 2 tte reference counters used to determine if
12357c478bd9Sstevel@tonic-gate  * it is ok to free up the hmeblk.  Both counters have to be zero in order
12367c478bd9Sstevel@tonic-gate  * to be able to free up hmeblk.  They are protected by cas.
12377c478bd9Sstevel@tonic-gate  * hblk_hmecnt is the number of hments present on pp mapping lists.
12387c478bd9Sstevel@tonic-gate  * hblk_vcnt reflects number of valid ttes in hmeblk.
12397c478bd9Sstevel@tonic-gate  *
12407c478bd9Sstevel@tonic-gate  * The hmeblk now also has per tte lock cnts.  This is required because
12417c478bd9Sstevel@tonic-gate  * the counts can be high and there are not enough bits in the tte. When
12427c478bd9Sstevel@tonic-gate  * physio is fixed to not lock the translations we should be able to move
12437c478bd9Sstevel@tonic-gate  * the lock cnt back to the tte.  See bug id 1198554.
12447c478bd9Sstevel@tonic-gate  *
12457c478bd9Sstevel@tonic-gate  * Note that xhat_hme_blk's layout follows this structure: hme_blk_misc
12467c478bd9Sstevel@tonic-gate  * and sf_hment are at the same offsets in both structures. Whenever
12477c478bd9Sstevel@tonic-gate  * hme_blk is changed, xhat_hme_blk may need to be updated as well.
12487c478bd9Sstevel@tonic-gate  */
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate struct hme_blk_misc {
125105d3dc4bSpaulsan 	uint_t	notused:25;
125205d3dc4bSpaulsan 	uint_t	shared_bit:1;	/* set for SRD shared hmeblk */
12537c478bd9Sstevel@tonic-gate 	uint_t	xhat_bit:1;	/* set for an xhat hme_blk */
12547c478bd9Sstevel@tonic-gate 	uint_t	shadow_bit:1;	/* set for a shadow hme_blk */
12557c478bd9Sstevel@tonic-gate 	uint_t	nucleus_bit:1;	/* set for a nucleus hme_blk */
12567c478bd9Sstevel@tonic-gate 	uint_t	ttesize:3;	/* contains ttesz of hmeblk */
12577c478bd9Sstevel@tonic-gate };
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate struct hme_blk {
12600a90a7fdSAmritpal Sandhu 	volatile uint64_t hblk_nextpa;	/* physical address for hash list */
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 	hmeblk_tag	hblk_tag;	/* tag used to obtain an hmeblk match */
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	struct hme_blk	*hblk_next;	/* on free list or on hash list */
12657c478bd9Sstevel@tonic-gate 					/* protected by hash lock */
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 	struct hme_blk	*hblk_shadow;	/* pts to shadow hblk */
12687c478bd9Sstevel@tonic-gate 					/* protected by hash lock */
12697c478bd9Sstevel@tonic-gate 	uint_t		hblk_span;	/* span of memory hmeblk maps */
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 	struct hme_blk_misc	hblk_misc;
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	union {
12747c478bd9Sstevel@tonic-gate 		struct {
12757c478bd9Sstevel@tonic-gate 			ushort_t hblk_hmecount;	/* hment on mlists counter */
12767c478bd9Sstevel@tonic-gate 			ushort_t hblk_validcnt;	/* valid tte reference count */
12777c478bd9Sstevel@tonic-gate 		} hblk_counts;
12787c478bd9Sstevel@tonic-gate 		uint_t		hblk_shadow_mask;
12797c478bd9Sstevel@tonic-gate 	} hblk_un;
12807c478bd9Sstevel@tonic-gate 
128105d3dc4bSpaulsan 	uint_t		hblk_lckcnt;
128205d3dc4bSpaulsan 
12837c478bd9Sstevel@tonic-gate #ifdef	HBLK_TRACE
12847c478bd9Sstevel@tonic-gate 	kmutex_t	hblk_audit_lock;	/* lock to protect index */
12857c478bd9Sstevel@tonic-gate 	uint_t		hblk_audit_index;	/* index into audit_cache */
12867c478bd9Sstevel@tonic-gate 	struct	hblk_lockcnt_audit hblk_audit_cache[HBLK_AUDIT_CACHE_SIZE];
12877c478bd9Sstevel@tonic-gate #endif	/* HBLK_AUDIT */
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	struct sf_hment hblk_hme[1];	/* hment array */
12907c478bd9Sstevel@tonic-gate };
12917c478bd9Sstevel@tonic-gate 
129205d3dc4bSpaulsan #define	hblk_shared	hblk_misc.shared_bit
12937c478bd9Sstevel@tonic-gate #define	hblk_xhat_bit   hblk_misc.xhat_bit
12947c478bd9Sstevel@tonic-gate #define	hblk_shw_bit	hblk_misc.shadow_bit
12957c478bd9Sstevel@tonic-gate #define	hblk_nuc_bit	hblk_misc.nucleus_bit
12967c478bd9Sstevel@tonic-gate #define	hblk_ttesz	hblk_misc.ttesize
12977c478bd9Sstevel@tonic-gate #define	hblk_hmecnt	hblk_un.hblk_counts.hblk_hmecount
12987c478bd9Sstevel@tonic-gate #define	hblk_vcnt	hblk_un.hblk_counts.hblk_validcnt
12997c478bd9Sstevel@tonic-gate #define	hblk_shw_mask	hblk_un.hblk_shadow_mask
13007c478bd9Sstevel@tonic-gate 
130105d3dc4bSpaulsan #define	MAX_HBLK_LCKCNT	0xFFFFFFFF
13027c478bd9Sstevel@tonic-gate #define	HMEBLK_ALIGN	0x8		/* hmeblk has to be double aligned */
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate #ifdef	HBLK_TRACE
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate #define	HBLK_STACK_TRACE(hmeblkp, lock)					\
13077c478bd9Sstevel@tonic-gate {									\
13087c478bd9Sstevel@tonic-gate 	int flag = lock;	/* to pacify lint */			\
13097c478bd9Sstevel@tonic-gate 	int audit_index;						\
13107c478bd9Sstevel@tonic-gate 									\
13117c478bd9Sstevel@tonic-gate 	mutex_enter(&hmeblkp->hblk_audit_lock);				\
13127c478bd9Sstevel@tonic-gate 	audit_index = hmeblkp->hblk_audit_index;			\
13137c478bd9Sstevel@tonic-gate 	hmeblkp->hblk_audit_index = ((hmeblkp->hblk_audit_index + 1) &	\
13147c478bd9Sstevel@tonic-gate 	    (HBLK_AUDIT_CACHE_SIZE - 1));				\
13157c478bd9Sstevel@tonic-gate 	mutex_exit(&hmeblkp->hblk_audit_lock);				\
13167c478bd9Sstevel@tonic-gate 									\
13177c478bd9Sstevel@tonic-gate 	if (flag)							\
13187c478bd9Sstevel@tonic-gate 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
13197c478bd9Sstevel@tonic-gate 		    HBLK_LOCK_PATTERN;					\
13207c478bd9Sstevel@tonic-gate 	else								\
13217c478bd9Sstevel@tonic-gate 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
13227c478bd9Sstevel@tonic-gate 		    HBLK_UNLOCK_PATTERN;				\
13237c478bd9Sstevel@tonic-gate 									\
13247c478bd9Sstevel@tonic-gate 	hmeblkp->hblk_audit_cache[audit_index].thread = curthread;	\
13257c478bd9Sstevel@tonic-gate 	hmeblkp->hblk_audit_cache[audit_index].depth =			\
13267c478bd9Sstevel@tonic-gate 	    getpcstack(hmeblkp->hblk_audit_cache[audit_index].stack,	\
13277c478bd9Sstevel@tonic-gate 	    HBLK_STACK_DEPTH);						\
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate #else
13317c478bd9Sstevel@tonic-gate 
13327c478bd9Sstevel@tonic-gate #define	HBLK_STACK_TRACE(hmeblkp, lock)
13337c478bd9Sstevel@tonic-gate 
13347c478bd9Sstevel@tonic-gate #endif	/* HBLK_TRACE */
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate #define	HMEHASH_FACTOR	16	/* used to calc # of buckets in hme hash */
13377c478bd9Sstevel@tonic-gate 
13387c478bd9Sstevel@tonic-gate /*
13397c478bd9Sstevel@tonic-gate  * A maximum number of user hmeblks is defined in order to place an upper
13407c478bd9Sstevel@tonic-gate  * limit on how much nucleus memory is required and to avoid overflowing the
13417c478bd9Sstevel@tonic-gate  * tsbmiss uhashsz and khashsz data areas. The number below corresponds to
13427c478bd9Sstevel@tonic-gate  * the number of buckets required, for an average hash chain length of 4 on
13437c478bd9Sstevel@tonic-gate  * a 16TB machine.
13447c478bd9Sstevel@tonic-gate  */
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate #define	MAX_UHME_BUCKETS	(0x1 << 30)
13477c478bd9Sstevel@tonic-gate #define	MAX_KHME_BUCKETS	(0x1 << 30)
13487c478bd9Sstevel@tonic-gate 
13497c478bd9Sstevel@tonic-gate /*
13507c478bd9Sstevel@tonic-gate  * The minimum number of kernel hash buckets.
13517c478bd9Sstevel@tonic-gate  */
13527c478bd9Sstevel@tonic-gate #define	MIN_KHME_BUCKETS	0x800
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate /*
13557c478bd9Sstevel@tonic-gate  * The number of hash buckets must be a power of 2. If the initial calculated
13567c478bd9Sstevel@tonic-gate  * value is less than USER_BUCKETS_THRESHOLD we round up to the next greater
13577c478bd9Sstevel@tonic-gate  * power of 2, otherwise we round down to avoid huge over allocations.
13587c478bd9Sstevel@tonic-gate  */
13597c478bd9Sstevel@tonic-gate #define	USER_BUCKETS_THRESHOLD	(1<<22)
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate #define	MAX_NUCUHME_BUCKETS	0x4000
13627c478bd9Sstevel@tonic-gate #define	MAX_NUCKHME_BUCKETS	0x2000
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate /*
13657c478bd9Sstevel@tonic-gate  * There are 2 locks in the hmehash bucket.  The hmehash_mutex is
13667c478bd9Sstevel@tonic-gate  * a regular mutex used to make sure operations on a hash link are only
13677c478bd9Sstevel@tonic-gate  * done by one thread.  Any operation which comes into the hat with
13687c478bd9Sstevel@tonic-gate  * a <vaddr, as> will grab the hmehash_mutex.  Normally one would expect
13697c478bd9Sstevel@tonic-gate  * the tsb miss handlers to grab the hash lock to make sure the hash list
13707c478bd9Sstevel@tonic-gate  * is consistent while we traverse it.  Unfortunately this can lead to
13717c478bd9Sstevel@tonic-gate  * deadlocks or recursive mutex enters since it is possible for
13727c478bd9Sstevel@tonic-gate  * someone holding the lock to take a tlb/tsb miss.
13737c478bd9Sstevel@tonic-gate  * To solve this problem we have added the hmehash_listlock.  This lock
13747c478bd9Sstevel@tonic-gate  * is only grabbed by the tsb miss handlers, vatopfn, and while
13757c478bd9Sstevel@tonic-gate  * adding/removing a hmeblk from the hash list. The code is written to
13767c478bd9Sstevel@tonic-gate  * guarantee we won't take a tlb miss while holding this lock.
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate struct hmehash_bucket {
13797c478bd9Sstevel@tonic-gate 	kmutex_t	hmehash_mutex;
13800a90a7fdSAmritpal Sandhu 	volatile uint64_t hmeh_nextpa;	/* physical address for hash list */
13817c478bd9Sstevel@tonic-gate 	struct hme_blk *hmeblkp;
13827c478bd9Sstevel@tonic-gate 	uint_t		hmeh_listlock;
13837c478bd9Sstevel@tonic-gate };
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate #endif /* !_ASM */
13867c478bd9Sstevel@tonic-gate 
13871e2e7a75Shuah #define	SFMMU_PGCNT_MASK	0x3f
13881e2e7a75Shuah #define	SFMMU_PGCNT_SHIFT	6
13891e2e7a75Shuah #define	INVALID_MMU_ID		-1
13901e2e7a75Shuah #define	SFMMU_MMU_GNUM_RSHIFT	16
13911e2e7a75Shuah #define	SFMMU_MMU_CNUM_LSHIFT	(64 - SFMMU_MMU_GNUM_RSHIFT)
13921e2e7a75Shuah #define	MAX_SFMMU_CTX_VAL	((1 << 16) - 1) /* for sanity check */
13931e2e7a75Shuah #define	MAX_SFMMU_GNUM_VAL	((0x1UL << 48) - 1)
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate /*
13967c478bd9Sstevel@tonic-gate  * The tsb miss handlers written in assembly know that sfmmup
13977c478bd9Sstevel@tonic-gate  * is a 64 bit ptr.
13987c478bd9Sstevel@tonic-gate  *
13997c478bd9Sstevel@tonic-gate  * The bspage and re-hash part is 64 bits, with the sfmmup being another 64
14007c478bd9Sstevel@tonic-gate  * bits.
14017c478bd9Sstevel@tonic-gate  */
14027c478bd9Sstevel@tonic-gate #define	HTAG_SFMMUPSZ		0	/* Not really used for LP64 */
140305d3dc4bSpaulsan #define	HTAG_BSPAGE_SHIFT	13
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate /*
14067c478bd9Sstevel@tonic-gate  * Assembly routines need to be able to get to ttesz
14077c478bd9Sstevel@tonic-gate  */
14087c478bd9Sstevel@tonic-gate #define	HBLK_SZMASK		0x7
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate #ifndef _ASM
14117c478bd9Sstevel@tonic-gate 
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate  * Returns the number of bytes that an hmeblk spans given its tte size
14147c478bd9Sstevel@tonic-gate  */
14157c478bd9Sstevel@tonic-gate #define	get_hblk_span(hmeblkp) ((hmeblkp)->hblk_span)
14167c478bd9Sstevel@tonic-gate #define	get_hblk_ttesz(hmeblkp)	((hmeblkp)->hblk_ttesz)
14177c478bd9Sstevel@tonic-gate #define	get_hblk_cache(hmeblkp)	(((hmeblkp)->hblk_ttesz == TTE8K) ? \
14187c478bd9Sstevel@tonic-gate 	sfmmu8_cache : sfmmu1_cache)
14197c478bd9Sstevel@tonic-gate #define	HMEBLK_SPAN(ttesz)						\
14207c478bd9Sstevel@tonic-gate 	((ttesz == TTE8K)? (TTEBYTES(ttesz) * NHMENTS) : TTEBYTES(ttesz))
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate #define	set_hblk_sz(hmeblkp, ttesz)				\
14237c478bd9Sstevel@tonic-gate 	(hmeblkp)->hblk_ttesz = (ttesz);			\
14247c478bd9Sstevel@tonic-gate 	(hmeblkp)->hblk_span = HMEBLK_SPAN(ttesz)
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate #define	get_hblk_base(hmeblkp)					\
14277c478bd9Sstevel@tonic-gate 	((uintptr_t)(hmeblkp)->hblk_tag.htag_bspage << MMU_PAGESHIFT)
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate #define	get_hblk_endaddr(hmeblkp)				\
14307c478bd9Sstevel@tonic-gate 	((caddr_t)(get_hblk_base(hmeblkp) + get_hblk_span(hmeblkp)))
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate #define	in_hblk_range(hmeblkp, vaddr)					\
14337c478bd9Sstevel@tonic-gate 	(((uintptr_t)(vaddr) >= get_hblk_base(hmeblkp)) &&		\
14347c478bd9Sstevel@tonic-gate 	((uintptr_t)(vaddr) < (get_hblk_base(hmeblkp) +			\
14357c478bd9Sstevel@tonic-gate 	get_hblk_span(hmeblkp))))
14367c478bd9Sstevel@tonic-gate 
14377c478bd9Sstevel@tonic-gate #define	tte_to_vaddr(hmeblkp, tte)	((caddr_t)(get_hblk_base(hmeblkp) \
14387c478bd9Sstevel@tonic-gate 	+ (TTEBYTES(TTE_CSZ(&tte)) * (tte).tte_hmenum)))
14397c478bd9Sstevel@tonic-gate 
144005d3dc4bSpaulsan #define	tte_to_evaddr(hmeblkp, ttep)	((caddr_t)(get_hblk_base(hmeblkp) \
144105d3dc4bSpaulsan 	+ (TTEBYTES(TTE_CSZ(ttep)) * ((ttep)->tte_hmenum + 1))))
144205d3dc4bSpaulsan 
14437c478bd9Sstevel@tonic-gate #define	vaddr_to_vshift(hblktag, vaddr, shwsz)				\
14447c478bd9Sstevel@tonic-gate 	((((uintptr_t)(vaddr) >> MMU_PAGESHIFT) - (hblktag.htag_bspage)) >>\
14457c478bd9Sstevel@tonic-gate 	TTE_BSZS_SHIFT((shwsz) - 1))
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate #define	HME8BLK_SZ	(sizeof (struct hme_blk) + \
14487c478bd9Sstevel@tonic-gate 			(NHMENTS - 1) * sizeof (struct sf_hment))
14497c478bd9Sstevel@tonic-gate #define	HME1BLK_SZ	(sizeof (struct hme_blk))
14507c478bd9Sstevel@tonic-gate #define	H1MIN		(2 + MAX_BIGKTSB_TTES)	/* nucleus text+data, ktsb */
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate /*
14537c478bd9Sstevel@tonic-gate  * Hme_blk hash structure
14547c478bd9Sstevel@tonic-gate  * Active mappings are kept in a hash structure of hme_blks.  The hash
14557c478bd9Sstevel@tonic-gate  * function is based on (ctx, vaddr) The size of the hash table size is a
14567c478bd9Sstevel@tonic-gate  * power of 2 such that the average hash chain lenth is HMENT_HASHAVELEN.
14577c478bd9Sstevel@tonic-gate  * The hash actually consists of 2 separate hashes.  One hash is for the user
14587c478bd9Sstevel@tonic-gate  * address space and the other hash is for the kernel address space.
14597c478bd9Sstevel@tonic-gate  * The number of buckets are calculated at boot time and stored in the global
14607c478bd9Sstevel@tonic-gate  * variables "uhmehash_num" and "khmehash_num".  By making the hash table size
14617c478bd9Sstevel@tonic-gate  * a power of 2 we can use a simply & function to derive an index instead of
14627c478bd9Sstevel@tonic-gate  * a divide.
14637c478bd9Sstevel@tonic-gate  *
14647c478bd9Sstevel@tonic-gate  * HME_HASH_FUNCTION(hatid, vaddr, shift) returns a pointer to a hme_hash
14657c478bd9Sstevel@tonic-gate  * bucket.
14667c478bd9Sstevel@tonic-gate  * An hme hash bucket contains a pointer to an hme_blk and the mutex that
14677c478bd9Sstevel@tonic-gate  * protects the link list.
14687c478bd9Sstevel@tonic-gate  * Spitfire supports 4 page sizes.  8k and 64K pages only need one hash.
14697c478bd9Sstevel@tonic-gate  * 512K pages need 2 hashes and 4M pages need 3 hashes.
14707c478bd9Sstevel@tonic-gate  * The 'shift' parameter controls how many bits the vaddr will be shifted in
14717c478bd9Sstevel@tonic-gate  * the hash function. It is calculated in the HME_HASH_SHIFT(ttesz) function
14727c478bd9Sstevel@tonic-gate  * and it varies depending on the page size as follows:
14737c478bd9Sstevel@tonic-gate  *	8k pages:  	HBLK_RANGE_SHIFT
14747c478bd9Sstevel@tonic-gate  *	64k pages:	MMU_PAGESHIFT64K
14757c478bd9Sstevel@tonic-gate  *	512K pages:	MMU_PAGESHIFT512K
14767c478bd9Sstevel@tonic-gate  *	4M pages:	MMU_PAGESHIFT4M
14777c478bd9Sstevel@tonic-gate  * An assembly version of the hash function exists in sfmmu_ktsb_miss(). All
14787c478bd9Sstevel@tonic-gate  * changes should be reflected in both versions.  This function and the TSB
14797c478bd9Sstevel@tonic-gate  * miss handlers are the only places which know about the two hashes.
14807c478bd9Sstevel@tonic-gate  *
14817c478bd9Sstevel@tonic-gate  * HBLK_RANGE_SHIFT controls range of virtual addresses that will fall
14827c478bd9Sstevel@tonic-gate  * into the same bucket for a particular process.  It is currently set to
14837c478bd9Sstevel@tonic-gate  * be equivalent to 64K range or one hme_blk.
14847c478bd9Sstevel@tonic-gate  *
14857c478bd9Sstevel@tonic-gate  * The hme_blks in the hash are protected by a per hash bucket mutex
14867c478bd9Sstevel@tonic-gate  * known as SFMMU_HASH_LOCK.
14877c478bd9Sstevel@tonic-gate  * You need to acquire this lock before traversing the hash bucket link
14887c478bd9Sstevel@tonic-gate  * list, while adding/removing a hme_blk to the list, and while
14897c478bd9Sstevel@tonic-gate  * modifying an hme_blk.  A possible optimization is to replace these
14907c478bd9Sstevel@tonic-gate  * mutexes by readers/writer lock but right now it is not clear whether
14917c478bd9Sstevel@tonic-gate  * this is a win or not.
14927c478bd9Sstevel@tonic-gate  *
14937c478bd9Sstevel@tonic-gate  * The HME_HASH_TABLE_SEARCH will search the hash table for the
14947c478bd9Sstevel@tonic-gate  * hme_blk that contains the hment that corresponds to the passed
14957c478bd9Sstevel@tonic-gate  * ctx and vaddr.  It assumed the SFMMU_HASH_LOCK is held.
14967c478bd9Sstevel@tonic-gate  */
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate #endif /* ! _ASM */
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate #define	KHATID			ksfmmup
15017c478bd9Sstevel@tonic-gate #define	UHMEHASH_SZ		uhmehash_num
15027c478bd9Sstevel@tonic-gate #define	KHMEHASH_SZ		khmehash_num
15037c478bd9Sstevel@tonic-gate #define	HMENT_HASHAVELEN	4
15047c478bd9Sstevel@tonic-gate #define	HBLK_RANGE_SHIFT	MMU_PAGESHIFT64K /* shift for HBLK_BS_MASK */
150505d3dc4bSpaulsan #define	HBLK_MIN_TTESZ		1
150605d3dc4bSpaulsan #define	HBLK_MIN_BYTES		MMU_PAGESIZE64K
150705d3dc4bSpaulsan #define	HBLK_MIN_SHIFT		MMU_PAGESHIFT64K
15087c478bd9Sstevel@tonic-gate #define	MAX_HASHCNT		5
15097c478bd9Sstevel@tonic-gate #define	DEFAULT_MAX_HASHCNT	3
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate #ifndef _ASM
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate #define	HASHADDR_MASK(hashno)	TTE_PAGEMASK(hashno)
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate #define	HME_HASH_SHIFT(ttesz)						\
1516d39fefdfSkupfer 	((ttesz == TTE8K)? HBLK_RANGE_SHIFT : TTE_PAGE_SHIFT(ttesz))
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate #define	HME_HASH_ADDR(vaddr, hmeshift)					\
15197c478bd9Sstevel@tonic-gate 	((caddr_t)(((uintptr_t)(vaddr) >> (hmeshift)) << (hmeshift)))
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate #define	HME_HASH_BSPAGE(vaddr, hmeshift)				\
15227c478bd9Sstevel@tonic-gate 	(((uintptr_t)(vaddr) >> (hmeshift)) << ((hmeshift) - MMU_PAGESHIFT))
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate #define	HME_HASH_REHASH(ttesz)						\
15257c478bd9Sstevel@tonic-gate 	(((ttesz) < TTE512K)? 1 : (ttesz))
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate #define	HME_HASH_FUNCTION(hatid, vaddr, shift)				     \
152805d3dc4bSpaulsan 	((((void *)hatid) != ((void *)KHATID)) ?			     \
152905d3dc4bSpaulsan 	(&uhme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
153005d3dc4bSpaulsan 	    UHMEHASH_SZ) ]):						     \
153105d3dc4bSpaulsan 	(&khme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
153205d3dc4bSpaulsan 	    KHMEHASH_SZ) ]))
15337c478bd9Sstevel@tonic-gate 
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate  * This macro will traverse a hmeblk hash link list looking for an hme_blk
15367c478bd9Sstevel@tonic-gate  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
15377c478bd9Sstevel@tonic-gate  * will be set to NULL, otherwise it will point to the correct hme_blk.
15387c478bd9Sstevel@tonic-gate  * This macro also cleans empty hblks.
15397c478bd9Sstevel@tonic-gate  */
15400a90a7fdSAmritpal Sandhu #define	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp, pr_hblk, listp)	\
15417c478bd9Sstevel@tonic-gate {									\
15427c478bd9Sstevel@tonic-gate 	struct hme_blk *nx_hblk;					\
15437c478bd9Sstevel@tonic-gate 									\
15447c478bd9Sstevel@tonic-gate 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
15457c478bd9Sstevel@tonic-gate 	hblkp = hmebp->hmeblkp;						\
15467c478bd9Sstevel@tonic-gate 	pr_hblk = NULL;							\
15477c478bd9Sstevel@tonic-gate 	while (hblkp) {							\
15487c478bd9Sstevel@tonic-gate 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
15497c478bd9Sstevel@tonic-gate 			/* found hme_blk */				\
15507c478bd9Sstevel@tonic-gate 			break;						\
15517c478bd9Sstevel@tonic-gate 		}							\
15527c478bd9Sstevel@tonic-gate 		nx_hblk = hblkp->hblk_next;				\
15537c478bd9Sstevel@tonic-gate 		if (!hblkp->hblk_vcnt && !hblkp->hblk_hmecnt) {		\
15540a90a7fdSAmritpal Sandhu 			sfmmu_hblk_hash_rm(hmebp, hblkp, pr_hblk,	\
15550a90a7fdSAmritpal Sandhu 			    listp, 0);					\
15567c478bd9Sstevel@tonic-gate 		} else {						\
15577c478bd9Sstevel@tonic-gate 			pr_hblk = hblkp;				\
15587c478bd9Sstevel@tonic-gate 		}							\
15597c478bd9Sstevel@tonic-gate 		hblkp = nx_hblk;					\
15607c478bd9Sstevel@tonic-gate 	}								\
15617c478bd9Sstevel@tonic-gate }
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate #define	HME_HASH_SEARCH(hmebp, hblktag, hblkp, listp)			\
15647c478bd9Sstevel@tonic-gate {									\
15657c478bd9Sstevel@tonic-gate 	struct hme_blk *pr_hblk;					\
15667c478bd9Sstevel@tonic-gate 									\
15670a90a7fdSAmritpal Sandhu 	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp,  pr_hblk, listp);	\
15687c478bd9Sstevel@tonic-gate }
15697c478bd9Sstevel@tonic-gate 
15707c478bd9Sstevel@tonic-gate /*
15717c478bd9Sstevel@tonic-gate  * This macro will traverse a hmeblk hash link list looking for an hme_blk
15727c478bd9Sstevel@tonic-gate  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
15737c478bd9Sstevel@tonic-gate  * will be set to NULL, otherwise it will point to the correct hme_blk.
15747c478bd9Sstevel@tonic-gate  * It doesn't remove empty hblks.
15757c478bd9Sstevel@tonic-gate  */
15767c478bd9Sstevel@tonic-gate #define	HME_HASH_FAST_SEARCH(hmebp, hblktag, hblkp)			\
15777c478bd9Sstevel@tonic-gate 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
15787c478bd9Sstevel@tonic-gate 	for (hblkp = hmebp->hmeblkp; hblkp;				\
15797c478bd9Sstevel@tonic-gate 	    hblkp = hblkp->hblk_next) {					\
15807c478bd9Sstevel@tonic-gate 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
15817c478bd9Sstevel@tonic-gate 			/* found hme_blk */				\
15827c478bd9Sstevel@tonic-gate 			break;						\
15837c478bd9Sstevel@tonic-gate 		}							\
1584d39fefdfSkupfer 	}
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate #define	SFMMU_HASH_LOCK(hmebp)						\
15877c478bd9Sstevel@tonic-gate 		(mutex_enter(&hmebp->hmehash_mutex))
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate #define	SFMMU_HASH_UNLOCK(hmebp)					\
15907c478bd9Sstevel@tonic-gate 		(mutex_exit(&hmebp->hmehash_mutex))
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate #define	SFMMU_HASH_LOCK_TRYENTER(hmebp)					\
15937c478bd9Sstevel@tonic-gate 		(mutex_tryenter(&hmebp->hmehash_mutex))
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate #define	SFMMU_HASH_LOCK_ISHELD(hmebp)					\
15967c478bd9Sstevel@tonic-gate 		(mutex_owned(&hmebp->hmehash_mutex))
15977c478bd9Sstevel@tonic-gate 
15981e2e7a75Shuah #define	SFMMU_XCALL_STATS(sfmmup)					\
15997c478bd9Sstevel@tonic-gate {									\
16001e2e7a75Shuah 	if (sfmmup == ksfmmup) {					\
16017c478bd9Sstevel@tonic-gate 		SFMMU_STAT(sf_kernel_xcalls);				\
16027c478bd9Sstevel@tonic-gate 	} else {							\
16037c478bd9Sstevel@tonic-gate 		SFMMU_STAT(sf_user_xcalls);				\
16047c478bd9Sstevel@tonic-gate 	}								\
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate #define	astosfmmu(as)		((as)->a_hat)
16087c478bd9Sstevel@tonic-gate #define	hblktosfmmu(hmeblkp)	((sfmmu_t *)(hmeblkp)->hblk_tag.htag_id)
160905d3dc4bSpaulsan #define	hblktosrd(hmeblkp)	((sf_srd_t *)(hmeblkp)->hblk_tag.htag_id)
16107c478bd9Sstevel@tonic-gate #define	sfmmutoas(sfmmup)	((sfmmup)->sfmmu_as)
161105d3dc4bSpaulsan 
161205d3dc4bSpaulsan #define	sfmmutohtagid(sfmmup, rid)			   \
161305d3dc4bSpaulsan 	(((rid) == SFMMU_INVALID_SHMERID) ? (void *)(sfmmup) : \
161405d3dc4bSpaulsan 	(void *)((sfmmup)->sfmmu_srdp))
161505d3dc4bSpaulsan 
16167c478bd9Sstevel@tonic-gate /*
16177c478bd9Sstevel@tonic-gate  * We use the sfmmu data structure to keep the per as page coloring info.
16187c478bd9Sstevel@tonic-gate  */
16197c478bd9Sstevel@tonic-gate #define	as_color_bin(as)	(astosfmmu(as)->sfmmu_clrbin)
16207c478bd9Sstevel@tonic-gate #define	as_color_start(as)	(astosfmmu(as)->sfmmu_clrstart)
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate typedef struct {
16237c478bd9Sstevel@tonic-gate 	char	h8[HME8BLK_SZ];
16247c478bd9Sstevel@tonic-gate } hblk8_t;
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate typedef struct {
16277c478bd9Sstevel@tonic-gate 	char	h1[HME1BLK_SZ];
16287c478bd9Sstevel@tonic-gate } hblk1_t;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate typedef struct {
16317c478bd9Sstevel@tonic-gate 	ulong_t  	index;
16327c478bd9Sstevel@tonic-gate 	ulong_t  	len;
16337c478bd9Sstevel@tonic-gate 	hblk8_t		*list;
16347c478bd9Sstevel@tonic-gate } nucleus_hblk8_info_t;
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate typedef struct {
16377c478bd9Sstevel@tonic-gate 	ulong_t		index;
16387c478bd9Sstevel@tonic-gate 	ulong_t		len;
16397c478bd9Sstevel@tonic-gate 	hblk1_t		*list;
16407c478bd9Sstevel@tonic-gate } nucleus_hblk1_info_t;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate /*
16437c478bd9Sstevel@tonic-gate  * This struct is used for accumlating information about a range
16447c478bd9Sstevel@tonic-gate  * of pages that are unloading so that a single xcall can flush
16457c478bd9Sstevel@tonic-gate  * the entire range from remote tlbs. A function that must demap
16467c478bd9Sstevel@tonic-gate  * a range of virtual addresses declares one of these structures
16477c478bd9Sstevel@tonic-gate  * and initializes using DEMP_RANGE_INIT(). It then passes a pointer to this
16487c478bd9Sstevel@tonic-gate  * struct to the appropriate sfmmu_hblk_* level function which does
16497c478bd9Sstevel@tonic-gate  * all the bookkeeping using the other macros. When the function has
16507c478bd9Sstevel@tonic-gate  * finished the virtual address range, it needs to call DEMAP_RANGE_FLUSH()
16517c478bd9Sstevel@tonic-gate  * macro to take care of any remaining unflushed mappings.
16527c478bd9Sstevel@tonic-gate  *
16537c478bd9Sstevel@tonic-gate  * The maximum range this struct can represent is the number of bits
16547c478bd9Sstevel@tonic-gate  * in the dmr_bitvec field times the pagesize in dmr_pgsz. Currently, only
16557c478bd9Sstevel@tonic-gate  * MMU_PAGESIZE pages are supported.
16567c478bd9Sstevel@tonic-gate  *
16577c478bd9Sstevel@tonic-gate  * Since there are now cases where it's no longer necessary to do
16587c478bd9Sstevel@tonic-gate  * flushes (e.g. when the process isn't runnable because it's swapping
16597c478bd9Sstevel@tonic-gate  * out or exiting) we allow these macros to take a NULL dmr input and do
16607c478bd9Sstevel@tonic-gate  * nothing in that case.
16617c478bd9Sstevel@tonic-gate  */
16627c478bd9Sstevel@tonic-gate typedef struct {
166363360950Smp204432 	sfmmu_t		*dmr_sfmmup;	/* relevant hat */
16647c478bd9Sstevel@tonic-gate 	caddr_t		dmr_addr;	/* beginning address */
16657c478bd9Sstevel@tonic-gate 	caddr_t		dmr_endaddr;	/* ending  address */
16667c478bd9Sstevel@tonic-gate 	ulong_t		dmr_bitvec;	/* valid pages found */
16677c478bd9Sstevel@tonic-gate 	ulong_t		dmr_bit;	/* next page to examine */
16687c478bd9Sstevel@tonic-gate 	ulong_t		dmr_maxbit;	/* highest page in range */
16697c478bd9Sstevel@tonic-gate 	ulong_t		dmr_pgsz;	/* page size in range */
16707c478bd9Sstevel@tonic-gate } demap_range_t;
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate #define	DMR_MAXBIT ((ulong_t)1<<63) /* dmr_bit high bit */
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_INIT(sfmmup, dmrp) \
16757c478bd9Sstevel@tonic-gate 	(dmrp)->dmr_sfmmup = (sfmmup); \
16767c478bd9Sstevel@tonic-gate 	(dmrp)->dmr_bitvec = 0; \
16777c478bd9Sstevel@tonic-gate 	(dmrp)->dmr_maxbit = sfmmu_dmr_maxbit; \
1678683b2949SRichard Lowe 	(dmrp)->dmr_pgsz = MMU_PAGESIZE;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_PGSZ(dmrp) ((dmrp)? (dmrp)->dmr_pgsz : MMU_PAGESIZE)
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr) \
16837c478bd9Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
16847c478bd9Sstevel@tonic-gate 	if ((dmrp)->dmr_bitvec != 0 && (dmrp)->dmr_endaddr != (addr)) \
16857c478bd9Sstevel@tonic-gate 		sfmmu_tlb_range_demap(dmrp); \
16867c478bd9Sstevel@tonic-gate 	(dmrp)->dmr_endaddr = (endaddr); \
16877c478bd9Sstevel@tonic-gate 	}
16887c478bd9Sstevel@tonic-gate 
16897c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_FLUSH(dmrp) \
16907c478bd9Sstevel@tonic-gate 	if ((dmrp)->dmr_bitvec != 0)			\
1691683b2949SRichard Lowe 		sfmmu_tlb_range_demap(dmrp);
1692683b2949SRichard Lowe 
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_MARKPG(dmrp, addr) \
16957c478bd9Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
16967c478bd9Sstevel@tonic-gate 		if ((dmrp)->dmr_bitvec == 0) { \
16977c478bd9Sstevel@tonic-gate 			(dmrp)->dmr_addr = (addr); \
16987c478bd9Sstevel@tonic-gate 			(dmrp)->dmr_bit = 1; \
16997c478bd9Sstevel@tonic-gate 		} \
17007c478bd9Sstevel@tonic-gate 		(dmrp)->dmr_bitvec |= (dmrp)->dmr_bit; \
17017c478bd9Sstevel@tonic-gate 	}
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate #define	DEMAP_RANGE_NEXTPG(dmrp) \
17047c478bd9Sstevel@tonic-gate 	if ((dmrp) != NULL && (dmrp)->dmr_bitvec != 0) { \
17057c478bd9Sstevel@tonic-gate 		if ((dmrp)->dmr_bit & (dmrp)->dmr_maxbit) { \
17067c478bd9Sstevel@tonic-gate 			sfmmu_tlb_range_demap(dmrp); \
17077c478bd9Sstevel@tonic-gate 		} else { \
17087c478bd9Sstevel@tonic-gate 			(dmrp)->dmr_bit <<= 1; \
17097c478bd9Sstevel@tonic-gate 		} \
17107c478bd9Sstevel@tonic-gate 	}
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate /*
17137c478bd9Sstevel@tonic-gate  * TSB related structures
17147c478bd9Sstevel@tonic-gate  *
17157c478bd9Sstevel@tonic-gate  * The TSB is made up of tte entries.  Both the tag and data are present
17167c478bd9Sstevel@tonic-gate  * in the TSB.  The TSB locking is managed as follows:
17177c478bd9Sstevel@tonic-gate  * A software bit in the tsb tag is used to indicate that entry is locked.
17187c478bd9Sstevel@tonic-gate  * If a cpu servicing a tsb miss reads a locked entry the tag compare will
17197c478bd9Sstevel@tonic-gate  * fail forcing the cpu to go to the hat hash for the translation.
17207c478bd9Sstevel@tonic-gate  * The cpu who holds the lock can then modify the data side, and the tag side.
17217c478bd9Sstevel@tonic-gate  * The last write should be to the word containing the lock bit which will
17227c478bd9Sstevel@tonic-gate  * clear the lock and allow the tsb entry to be read.  It is assumed that all
17237c478bd9Sstevel@tonic-gate  * cpus reading the tsb will do so with atomic 128-bit loads.  An atomic 128
17247c478bd9Sstevel@tonic-gate  * bit load is required to prevent the following from happening:
17257c478bd9Sstevel@tonic-gate  *
17267c478bd9Sstevel@tonic-gate  * cpu 0			cpu 1			comments
17277c478bd9Sstevel@tonic-gate  *
17287c478bd9Sstevel@tonic-gate  * ldx tag						tag unlocked
17297c478bd9Sstevel@tonic-gate  *				ldstub lock		set lock
17307c478bd9Sstevel@tonic-gate  *				stx data
17317c478bd9Sstevel@tonic-gate  *				stx tag			unlock
17327c478bd9Sstevel@tonic-gate  * ldx tag						incorrect tte!!!
17337c478bd9Sstevel@tonic-gate  *
17347c478bd9Sstevel@tonic-gate  * The software also maintains a bit in the tag to indicate an invalid
17357c478bd9Sstevel@tonic-gate  * tsb entry.  The purpose of this bit is to allow the tsb invalidate code
17367c478bd9Sstevel@tonic-gate  * to invalidate a tsb entry with a single cas.  See code for details.
17377c478bd9Sstevel@tonic-gate  */
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate union tsb_tag {
17407c478bd9Sstevel@tonic-gate 	struct {
17417c478bd9Sstevel@tonic-gate 		uint32_t	tag_res0:16;	/* reserved - context area */
17427c478bd9Sstevel@tonic-gate 		uint32_t	tag_inv:1;	/* sw - invalid tsb entry */
17437c478bd9Sstevel@tonic-gate 		uint32_t	tag_lock:1;	/* sw - locked tsb entry */
17447c478bd9Sstevel@tonic-gate 		uint32_t	tag_res1:4;	/* reserved */
17457c478bd9Sstevel@tonic-gate 		uint32_t	tag_va_hi:10;	/* va[63:54] */
17467c478bd9Sstevel@tonic-gate 		uint32_t	tag_va_lo;	/* va[53:22] */
17477c478bd9Sstevel@tonic-gate 	} tagbits;
17487c478bd9Sstevel@tonic-gate 	struct tsb_tagints {
17497c478bd9Sstevel@tonic-gate 		uint32_t	inthi;
17507c478bd9Sstevel@tonic-gate 		uint32_t	intlo;
17517c478bd9Sstevel@tonic-gate 	} tagints;
17527c478bd9Sstevel@tonic-gate };
17537c478bd9Sstevel@tonic-gate #define	tag_invalid		tagbits.tag_inv
17547c478bd9Sstevel@tonic-gate #define	tag_locked		tagbits.tag_lock
17557c478bd9Sstevel@tonic-gate #define	tag_vahi		tagbits.tag_va_hi
17567c478bd9Sstevel@tonic-gate #define	tag_valo		tagbits.tag_va_lo
17577c478bd9Sstevel@tonic-gate #define	tag_inthi		tagints.inthi
17587c478bd9Sstevel@tonic-gate #define	tag_intlo		tagints.intlo
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate struct tsbe {
17617c478bd9Sstevel@tonic-gate 	union tsb_tag	tte_tag;
17627c478bd9Sstevel@tonic-gate 	tte_t		tte_data;
17637c478bd9Sstevel@tonic-gate };
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /*
17667c478bd9Sstevel@tonic-gate  * A per cpu struct is kept that duplicates some info
17677c478bd9Sstevel@tonic-gate  * used by the tl>0 tsb miss handlers plus it provides
17687c478bd9Sstevel@tonic-gate  * a scratch area.  Its purpose is to minimize cache misses
17697c478bd9Sstevel@tonic-gate  * in the tsb miss handler and is 128 bytes (2 e$ lines).
17707c478bd9Sstevel@tonic-gate  *
17717c478bd9Sstevel@tonic-gate  * There should be one allocated per cpu in nucleus memory
17727c478bd9Sstevel@tonic-gate  * and should be aligned on an ecache line boundary.
17737c478bd9Sstevel@tonic-gate  */
17747c478bd9Sstevel@tonic-gate struct tsbmiss {
17757c478bd9Sstevel@tonic-gate 	sfmmu_t			*ksfmmup;	/* kernel hat id */
17767c478bd9Sstevel@tonic-gate 	sfmmu_t			*usfmmup;	/* user hat id */
177705d3dc4bSpaulsan 	sf_srd_t		*usrdp;		/* user's SRD hat id */
17787c478bd9Sstevel@tonic-gate 	struct tsbe		*tsbptr;	/* hardware computed ptr */
17797c478bd9Sstevel@tonic-gate 	struct tsbe		*tsbptr4m;	/* hardware computed ptr */
178005d3dc4bSpaulsan 	struct tsbe		*tsbscdptr;	/* hardware computed ptr */
178105d3dc4bSpaulsan 	struct tsbe		*tsbscdptr4m;	/* hardware computed ptr */
17827c478bd9Sstevel@tonic-gate 	uint64_t		ismblkpa;
17837c478bd9Sstevel@tonic-gate 	struct hmehash_bucket	*khashstart;
17847c478bd9Sstevel@tonic-gate 	struct hmehash_bucket	*uhashstart;
17857c478bd9Sstevel@tonic-gate 	uint_t			khashsz;
17867c478bd9Sstevel@tonic-gate 	uint_t			uhashsz;
17877c478bd9Sstevel@tonic-gate 	uint16_t 		dcache_line_mask; /* used to flush dcache */
178805d3dc4bSpaulsan 	uchar_t			uhat_tteflags;	/* private page sizes */
178905d3dc4bSpaulsan 	uchar_t			uhat_rtteflags;	/* SHME pagesizes */
17907c478bd9Sstevel@tonic-gate 	uint32_t		utsb_misses;
17917c478bd9Sstevel@tonic-gate 	uint32_t		ktsb_misses;
17927c478bd9Sstevel@tonic-gate 	uint16_t		uprot_traps;
17937c478bd9Sstevel@tonic-gate 	uint16_t		kprot_traps;
17947c478bd9Sstevel@tonic-gate 	/*
17957c478bd9Sstevel@tonic-gate 	 * scratch[0] -> TSB_TAGACC
17967c478bd9Sstevel@tonic-gate 	 * scratch[1] -> TSBMISS_HMEBP
17977c478bd9Sstevel@tonic-gate 	 * scratch[2] -> TSBMISS_HATID
17987c478bd9Sstevel@tonic-gate 	 */
17997c478bd9Sstevel@tonic-gate 	uintptr_t		scratch[3];
180005d3dc4bSpaulsan 	ulong_t		shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
180105d3dc4bSpaulsan 	ulong_t		scd_shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
18029d0d62adSJason Beloro 	uint8_t		pad[48];			/* pad to 64 bytes */
18037c478bd9Sstevel@tonic-gate };
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate /*
18067c478bd9Sstevel@tonic-gate  * A per cpu struct is kept for the use within the tl>0 kpm tsb
18077c478bd9Sstevel@tonic-gate  * miss handler. Some members are duplicates of common data or
18087c478bd9Sstevel@tonic-gate  * the physical addresses of common data. A few members are also
18097c478bd9Sstevel@tonic-gate  * written by the tl>0 kpm tsb miss handler. Its purpose is to
18107c478bd9Sstevel@tonic-gate  * minimize cache misses in the kpm tsb miss handler and occupies
18117c478bd9Sstevel@tonic-gate  * one ecache line. There should be one allocated per cpu in
18127c478bd9Sstevel@tonic-gate  * nucleus memory and it should be aligned on an ecache line
18137c478bd9Sstevel@tonic-gate  * boundary. It is not merged w/ struct tsbmiss since there is
18147c478bd9Sstevel@tonic-gate  * not much to share and the tsbmiss pathes are different, so
18157c478bd9Sstevel@tonic-gate  * a kpm tlbmiss/tsbmiss only touches one cacheline, except for
18167c478bd9Sstevel@tonic-gate  * (DEBUG || SFMMU_STAT_GATHER) where the dtlb_misses counter
18177c478bd9Sstevel@tonic-gate  * of struct tsbmiss is used on every dtlb miss.
18187c478bd9Sstevel@tonic-gate  */
18197c478bd9Sstevel@tonic-gate struct kpmtsbm {
18207c478bd9Sstevel@tonic-gate 	caddr_t		vbase;		/* start of address kpm range */
18217c478bd9Sstevel@tonic-gate 	caddr_t		vend;		/* end of address kpm range */
18227c478bd9Sstevel@tonic-gate 	uchar_t		flags;		/* flags needed in TL tsbmiss handler */
18237c478bd9Sstevel@tonic-gate 	uchar_t		sz_shift;	/* for single kpm window */
18247c478bd9Sstevel@tonic-gate 	uchar_t		kpmp_shift;	/* hash lock shift */
18257c478bd9Sstevel@tonic-gate 	uchar_t		kpmp2pshft;	/* kpm page to page shift */
18267c478bd9Sstevel@tonic-gate 	uint_t		kpmp_table_sz;	/* size of kpmp_table or kpmp_stable */
18277c478bd9Sstevel@tonic-gate 	uint64_t	kpmp_tablepa;	/* paddr of kpmp_table or kpmp_stable */
18287c478bd9Sstevel@tonic-gate 	uint64_t	msegphashpa;	/* paddr of memseg_phash */
18297c478bd9Sstevel@tonic-gate 	struct tsbe	*tsbptr;	/* saved ktsb pointer */
18307c478bd9Sstevel@tonic-gate 	uint_t		kpm_dtlb_misses; /* kpm tlbmiss counter */
18317c478bd9Sstevel@tonic-gate 	uint_t		kpm_tsb_misses;	/* kpm tsbmiss counter */
18327c478bd9Sstevel@tonic-gate 	uintptr_t	pad[1];
18337c478bd9Sstevel@tonic-gate };
18347c478bd9Sstevel@tonic-gate 
183505d3dc4bSpaulsan extern size_t	tsb_slab_size;
18367c478bd9Sstevel@tonic-gate extern uint_t	tsb_slab_shift;
183705d3dc4bSpaulsan extern size_t	tsb_slab_mask;
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate #endif /* !_ASM */
18407c478bd9Sstevel@tonic-gate 
18417c478bd9Sstevel@tonic-gate /*
18427c478bd9Sstevel@tonic-gate  * Flags for TL kpm tsbmiss handler
18437c478bd9Sstevel@tonic-gate  */
18447c478bd9Sstevel@tonic-gate #define	KPMTSBM_ENABLE_FLAG	0x01	/* bit copy of kpm_enable */
18457c478bd9Sstevel@tonic-gate #define	KPMTSBM_TLTSBM_FLAG	0x02	/* use TL tsbmiss handler */
18467c478bd9Sstevel@tonic-gate #define	KPMTSBM_TSBPHYS_FLAG	0x04	/* use ASI_MEM for TSB update */
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate /*
18497c478bd9Sstevel@tonic-gate  * The TSB
18507c478bd9Sstevel@tonic-gate  * All TSB sizes supported by the hardware are now supported (8K - 1M).
18517c478bd9Sstevel@tonic-gate  * For kernel TSBs we may go beyond the hardware supported sizes and support
18527c478bd9Sstevel@tonic-gate  * larger TSBs via software.
18537c478bd9Sstevel@tonic-gate  * All TTE sizes are supported in the TSB; the manner in which this is
18547c478bd9Sstevel@tonic-gate  * done is cpu dependent.
18557c478bd9Sstevel@tonic-gate  */
18567c478bd9Sstevel@tonic-gate #define	TSB_MIN_SZCODE		TSB_8K_SZCODE	/* min. supported TSB size */
18577c478bd9Sstevel@tonic-gate #define	TSB_MIN_OFFSET_MASK	(TSB_OFFSET_MASK(TSB_MIN_SZCODE))
18587c478bd9Sstevel@tonic-gate 
185905d3dc4bSpaulsan #ifdef sun4v
186005d3dc4bSpaulsan #define	UTSB_MAX_SZCODE		TSB_256M_SZCODE /* max. supported TSB size */
186105d3dc4bSpaulsan #else /* sun4u */
18627c478bd9Sstevel@tonic-gate #define	UTSB_MAX_SZCODE		TSB_1M_SZCODE	/* max. supported TSB size */
186305d3dc4bSpaulsan #endif /* sun4v */
186405d3dc4bSpaulsan 
18657c478bd9Sstevel@tonic-gate #define	UTSB_MAX_OFFSET_MASK	(TSB_OFFSET_MASK(UTSB_MAX_SZCODE))
18667c478bd9Sstevel@tonic-gate 
18677c478bd9Sstevel@tonic-gate #define	TSB_FREEMEM_MIN		0x1000		/* 32 mb */
18687c478bd9Sstevel@tonic-gate #define	TSB_FREEMEM_LARGE	0x10000		/* 512 mb */
18697c478bd9Sstevel@tonic-gate #define	TSB_8K_SZCODE		0		/* 512 entries */
18707c478bd9Sstevel@tonic-gate #define	TSB_16K_SZCODE		1		/* 1k entries */
18717c478bd9Sstevel@tonic-gate #define	TSB_32K_SZCODE		2		/* 2k entries */
18727c478bd9Sstevel@tonic-gate #define	TSB_64K_SZCODE		3		/* 4k entries */
18737c478bd9Sstevel@tonic-gate #define	TSB_128K_SZCODE		4		/* 8k entries */
18747c478bd9Sstevel@tonic-gate #define	TSB_256K_SZCODE		5		/* 16k entries */
18757c478bd9Sstevel@tonic-gate #define	TSB_512K_SZCODE		6		/* 32k entries */
18767c478bd9Sstevel@tonic-gate #define	TSB_1M_SZCODE		7		/* 64k entries */
18777c478bd9Sstevel@tonic-gate #define	TSB_2M_SZCODE		8		/* 128k entries */
18787c478bd9Sstevel@tonic-gate #define	TSB_4M_SZCODE		9		/* 256k entries */
187905d3dc4bSpaulsan #define	TSB_8M_SZCODE		10		/* 512k entries */
188005d3dc4bSpaulsan #define	TSB_16M_SZCODE		11		/* 1M entries */
188105d3dc4bSpaulsan #define	TSB_32M_SZCODE		12		/* 2M entries */
188205d3dc4bSpaulsan #define	TSB_64M_SZCODE		13		/* 4M entries */
188305d3dc4bSpaulsan #define	TSB_128M_SZCODE		14		/* 8M entries */
188405d3dc4bSpaulsan #define	TSB_256M_SZCODE		15		/* 16M entries */
18857c478bd9Sstevel@tonic-gate #define	TSB_ENTRY_SHIFT		4	/* each entry = 128 bits = 16 bytes */
18867c478bd9Sstevel@tonic-gate #define	TSB_ENTRY_SIZE		(1 << 4)
18877c478bd9Sstevel@tonic-gate #define	TSB_START_SIZE		9
18887c478bd9Sstevel@tonic-gate #define	TSB_ENTRIES(tsbsz)	(1 << (TSB_START_SIZE + tsbsz))
18897c478bd9Sstevel@tonic-gate #define	TSB_BYTES(tsbsz)	(TSB_ENTRIES(tsbsz) << TSB_ENTRY_SHIFT)
18907c478bd9Sstevel@tonic-gate #define	TSB_OFFSET_MASK(tsbsz)	(TSB_ENTRIES(tsbsz) - 1)
18917c478bd9Sstevel@tonic-gate #define	TSB_BASEADDR_MASK	((1 << 12) - 1)
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate /*
18947c478bd9Sstevel@tonic-gate  * sun4u platforms
18957c478bd9Sstevel@tonic-gate  * ---------------
18967c478bd9Sstevel@tonic-gate  * We now support two user TSBs with one TSB base register.
18977c478bd9Sstevel@tonic-gate  * Hence the TSB base register is split up as follows:
18987c478bd9Sstevel@tonic-gate  *
18997c478bd9Sstevel@tonic-gate  * When only one TSB present:
19007c478bd9Sstevel@tonic-gate  *   [63  62..42  41..13  12..4  3..0]
19017c478bd9Sstevel@tonic-gate  *     ^   ^       ^       ^     ^
19027c478bd9Sstevel@tonic-gate  *     |   |       |       |     |
19037c478bd9Sstevel@tonic-gate  *     |   |       |       |     |_ TSB size code
19047c478bd9Sstevel@tonic-gate  *     |   |       |       |
19057c478bd9Sstevel@tonic-gate  *     |   |       |       |_ Reserved 0
19067c478bd9Sstevel@tonic-gate  *     |   |       |
19077c478bd9Sstevel@tonic-gate  *     |   |       |_ TSB VA[41..13]
19087c478bd9Sstevel@tonic-gate  *     |   |
19097c478bd9Sstevel@tonic-gate  *     |   |_ VA hole (Spitfire), zeros (Cheetah and beyond)
19107c478bd9Sstevel@tonic-gate  *     |
19117c478bd9Sstevel@tonic-gate  *     |_ 0
19127c478bd9Sstevel@tonic-gate  *
19137c478bd9Sstevel@tonic-gate  * When second TSB present:
19147c478bd9Sstevel@tonic-gate  *   [63  62..42  41..33  32..29  28..22  21..13  12..4  3..0]
19157c478bd9Sstevel@tonic-gate  *     ^   ^       ^       ^       ^       ^       ^     ^
19167c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |       |     |
19177c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |       |     |_ First TSB size code
19187c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |       |
19197c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |       |_ Reserved 0
19207c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |
19217c478bd9Sstevel@tonic-gate  *     |   |       |       |       |       |_ First TSB's VA[21..13]
19227c478bd9Sstevel@tonic-gate  *     |   |       |       |       |
19237c478bd9Sstevel@tonic-gate  *     |   |       |       |       |_ Reserved for future use
19247c478bd9Sstevel@tonic-gate  *     |   |       |       |
19257c478bd9Sstevel@tonic-gate  *     |   |       |       |_ Second TSB's size code
19267c478bd9Sstevel@tonic-gate  *     |   |       |
19277c478bd9Sstevel@tonic-gate  *     |   |       |_ Second TSB's VA[21..13]
19287c478bd9Sstevel@tonic-gate  *     |   |
19297c478bd9Sstevel@tonic-gate  *     |   |_ VA hole (Spitfire) / ones (Cheetah and beyond)
19307c478bd9Sstevel@tonic-gate  *     |
19317c478bd9Sstevel@tonic-gate  *     |_ 1
19327c478bd9Sstevel@tonic-gate  *
19337c478bd9Sstevel@tonic-gate  * Note that since we store 21..13 of each TSB's VA, TSBs and their slabs
19347c478bd9Sstevel@tonic-gate  * may be up to 4M in size.  For now, only hardware supported TSB sizes
19357c478bd9Sstevel@tonic-gate  * are supported, though the slabs are usually 4M in size.
19367c478bd9Sstevel@tonic-gate  *
193725cf1a30Sjl139090  * sun4u platforms that define UTSB_PHYS use physical addressing to access
193825cf1a30Sjl139090  * the user TSBs at TL>0.  The first user TSB base is in the MMU I/D TSB Base
193925cf1a30Sjl139090  * registers.  The second TSB base uses a dedicated scratchpad register which
19401426d65aSsm142603  * requires a definition of SCRATCHPAD_UTSBREG2 in mach_sfmmu.h.  The layout for
194125cf1a30Sjl139090  * both registers is equivalent to sun4v below, except the TSB PA range is
194225cf1a30Sjl139090  * [46..13] for sun4u.
194325cf1a30Sjl139090  *
19447c478bd9Sstevel@tonic-gate  * sun4v platforms
19457c478bd9Sstevel@tonic-gate  * ---------------
19467c478bd9Sstevel@tonic-gate  * On sun4v platforms, we use two dedicated scratchpad registers as pseudo
19477c478bd9Sstevel@tonic-gate  * hardware TSB base registers to hold up to two different user TSBs.
19487c478bd9Sstevel@tonic-gate  *
19497c478bd9Sstevel@tonic-gate  * Each register contains TSB's physical base and size code information
19507c478bd9Sstevel@tonic-gate  * as follows:
19517c478bd9Sstevel@tonic-gate  *
19527c478bd9Sstevel@tonic-gate  *   [63..56  55..13  12..4  3..0]
19537c478bd9Sstevel@tonic-gate  *      ^       ^       ^     ^
19547c478bd9Sstevel@tonic-gate  *      |       |       |     |
19557c478bd9Sstevel@tonic-gate  *      |       |       |     |_ TSB size code
19567c478bd9Sstevel@tonic-gate  *      |       |       |
19577c478bd9Sstevel@tonic-gate  *      |       |       |_ Reserved 0
19587c478bd9Sstevel@tonic-gate  *      |       |
19597c478bd9Sstevel@tonic-gate  *      |       |_ TSB PA[55..13]
19607c478bd9Sstevel@tonic-gate  *      |
19617c478bd9Sstevel@tonic-gate  *      |
19627c478bd9Sstevel@tonic-gate  *      |
19637c478bd9Sstevel@tonic-gate  *      |_ 0 for valid TSB
19647c478bd9Sstevel@tonic-gate  *
19657c478bd9Sstevel@tonic-gate  * Absence of a user TSB (primarily the second user TSB) is indicated by
19667c478bd9Sstevel@tonic-gate  * storing a negative value in the TSB base register. This allows us to
19677c478bd9Sstevel@tonic-gate  * check for presence of a user TSB by simply checking bit# 63.
19687c478bd9Sstevel@tonic-gate  */
19697c478bd9Sstevel@tonic-gate #define	TSBREG_MSB_SHIFT	32		/* set upper bits */
19707c478bd9Sstevel@tonic-gate #define	TSBREG_MSB_CONST	0xfffff800	/* set bits 63..43 */
19717c478bd9Sstevel@tonic-gate #define	TSBREG_FIRTSB_SHIFT	42		/* to clear bits 63:22 */
19727c478bd9Sstevel@tonic-gate #define	TSBREG_SECTSB_MKSHIFT	20		/* 21:13 --> 41:33 */
19737c478bd9Sstevel@tonic-gate #define	TSBREG_SECTSB_LSHIFT	22		/* to clear bits 63:42 */
19747c478bd9Sstevel@tonic-gate #define	TSBREG_SECTSB_RSHIFT	(TSBREG_SECTSB_MKSHIFT + TSBREG_SECTSB_LSHIFT)
19757c478bd9Sstevel@tonic-gate 						/* sectsb va -> bits 21:13 */
19767c478bd9Sstevel@tonic-gate 						/* after clearing upper bits */
19777c478bd9Sstevel@tonic-gate #define	TSBREG_SECSZ_SHIFT	29		/* to get sectsb szc to 3:0 */
19787c478bd9Sstevel@tonic-gate #define	TSBREG_VAMASK_SHIFT	13		/* set up VA mask */
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate #define	BIGKTSB_SZ_MASK		0xf
19817c478bd9Sstevel@tonic-gate #define	TSB_SOFTSZ_MASK		BIGKTSB_SZ_MASK
19827c478bd9Sstevel@tonic-gate #define	MIN_BIGKTSB_SZCODE	9	/* 256k entries */
19837c478bd9Sstevel@tonic-gate #define	MAX_BIGKTSB_SZCODE	11	/* 1024k entries */
19847c478bd9Sstevel@tonic-gate #define	MAX_BIGKTSB_TTES	(TSB_BYTES(MAX_BIGKTSB_SZCODE) / MMU_PAGESIZE4M)
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate #define	TAG_VALO_SHIFT		22		/* tag's va are bits 63-22 */
19877c478bd9Sstevel@tonic-gate /*
19887c478bd9Sstevel@tonic-gate  * sw bits used on tsb_tag - bit masks used only in assembly
19897c478bd9Sstevel@tonic-gate  * use only a sethi for these fields.
19907c478bd9Sstevel@tonic-gate  */
19917c478bd9Sstevel@tonic-gate #define	TSBTAG_INVALID	0x00008000		/* tsb_tag.tag_invalid */
19927c478bd9Sstevel@tonic-gate #define	TSBTAG_LOCKED	0x00004000		/* tsb_tag.tag_locked */
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate #ifdef	_ASM
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate /*
19977c478bd9Sstevel@tonic-gate  * Marker to indicate that this instruction will be hot patched at runtime
19987c478bd9Sstevel@tonic-gate  * to some other value.
19997c478bd9Sstevel@tonic-gate  * This value must be zero since it fills in the imm bits of the target
20007c478bd9Sstevel@tonic-gate  * instructions to be patched
20017c478bd9Sstevel@tonic-gate  */
20027c478bd9Sstevel@tonic-gate #define	RUNTIME_PATCH	(0)
20037c478bd9Sstevel@tonic-gate 
20047c478bd9Sstevel@tonic-gate /*
20057c478bd9Sstevel@tonic-gate  * V9 defines nop instruction as the following, which we use
20067c478bd9Sstevel@tonic-gate  * at runtime to nullify some instructions we don't want to
20077c478bd9Sstevel@tonic-gate  * execute in the trap handlers on certain platforms.
20087c478bd9Sstevel@tonic-gate  */
20097c478bd9Sstevel@tonic-gate #define	MAKE_NOP_INSTR(reg)	\
20107c478bd9Sstevel@tonic-gate 	sethi	%hi(0x1000000), reg
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate /*
201305d3dc4bSpaulsan  * This macro constructs a SPARC V9 "jmpl <source reg>, %g0"
201405d3dc4bSpaulsan  * instruction, with the source register specified by the jump_reg_number.
201505d3dc4bSpaulsan  * The jmp opcode [24:19] = 11 1000 and source register is bits [18:14].
201605d3dc4bSpaulsan  * The instruction is returned in reg. The macro is used to patch in a jmpl
201705d3dc4bSpaulsan  * instruction at runtime.
201805d3dc4bSpaulsan  */
201905d3dc4bSpaulsan #define	MAKE_JMP_INSTR(jump_reg_number, reg, tmp)	\
202005d3dc4bSpaulsan 	sethi	%hi(0x81c00000), reg;			\
202105d3dc4bSpaulsan 	mov	jump_reg_number, tmp;			\
202205d3dc4bSpaulsan 	sll	tmp, 14, tmp;				\
202305d3dc4bSpaulsan 	or	reg, tmp, reg
202405d3dc4bSpaulsan 
202505d3dc4bSpaulsan /*
20261e2e7a75Shuah  * Macro to get hat per-MMU cnum on this CPU.
20271e2e7a75Shuah  * sfmmu - In, pass in "sfmmup" from the caller.
20281e2e7a75Shuah  * cnum	- Out, return 'cnum' to the caller
20291e2e7a75Shuah  * scr	- scratch
20301e2e7a75Shuah  */
20311e2e7a75Shuah #define	SFMMU_CPU_CNUM(sfmmu, cnum, scr)				      \
20321e2e7a75Shuah 	CPU_ADDR(scr, cnum);	/* scr = load CPU struct addr */	      \
20331e2e7a75Shuah 	ld	[scr + CPU_MMU_IDX], cnum;	/* cnum = mmuid */	      \
20341e2e7a75Shuah 	add	sfmmu, SFMMU_CTXS, scr;	/* scr = sfmmup->sfmmu_ctxs[] */      \
20351e2e7a75Shuah 	sllx    cnum, SFMMU_MMU_CTX_SHIFT, cnum;			      \
20361e2e7a75Shuah 	add	scr, cnum, scr;		/* scr = sfmmup->sfmmu_ctxs[id] */    \
20371e2e7a75Shuah 	ldx	[scr + SFMMU_MMU_GC_NUM], scr;	/* sfmmu_ctxs[id].gcnum */    \
20381e2e7a75Shuah 	sllx    scr, SFMMU_MMU_CNUM_LSHIFT, scr;			      \
20391e2e7a75Shuah 	srlx    scr, SFMMU_MMU_CNUM_LSHIFT, cnum;	/* cnum = sfmmu cnum */
20401e2e7a75Shuah 
20411e2e7a75Shuah /*
20421e2e7a75Shuah  * Macro to get hat gnum & cnum assocaited with sfmmu_ctx[mmuid] entry
20431e2e7a75Shuah  * entry - In,  pass in (&sfmmu_ctxs[mmuid] - SFMMU_CTXS) from the caller.
20441e2e7a75Shuah  * gnum - Out, return sfmmu gnum
20451e2e7a75Shuah  * cnum - Out, return sfmmu cnum
20461e2e7a75Shuah  * reg	- scratch
20471e2e7a75Shuah  */
20481e2e7a75Shuah #define	SFMMU_MMUID_GNUM_CNUM(entry, gnum, cnum, reg)			     \
20491e2e7a75Shuah 	ldx	[entry + SFMMU_CTXS], reg;  /* reg = sfmmu (gnum | cnum) */  \
20501e2e7a75Shuah 	srlx	reg, SFMMU_MMU_GNUM_RSHIFT, gnum;    /* gnum = sfmmu gnum */ \
20511e2e7a75Shuah 	sllx	reg, SFMMU_MMU_CNUM_LSHIFT, cnum;			     \
20521e2e7a75Shuah 	srlx	cnum, SFMMU_MMU_CNUM_LSHIFT, cnum;   /* cnum = sfmmu cnum */
20531e2e7a75Shuah 
20541e2e7a75Shuah /*
20557c478bd9Sstevel@tonic-gate  * Macro to get this CPU's tsbmiss area.
20567c478bd9Sstevel@tonic-gate  */
20577c478bd9Sstevel@tonic-gate #define	CPU_TSBMISS_AREA(tsbmiss, tmp1)					\
20587c478bd9Sstevel@tonic-gate 	CPU_INDEX(tmp1, tsbmiss);		/* tmp1 = cpu idx */	\
20597c478bd9Sstevel@tonic-gate 	sethi	%hi(tsbmiss_area), tsbmiss;	/* tsbmiss base ptr */	\
206005d3dc4bSpaulsan 	mulx    tmp1, TSBMISS_SIZE, tmp1;	/* byte offset */	\
20617c478bd9Sstevel@tonic-gate 	or	tsbmiss, %lo(tsbmiss_area), tsbmiss;			\
20627c478bd9Sstevel@tonic-gate 	add	tsbmiss, tmp1, tsbmiss		/* tsbmiss area of CPU */
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate /*
20667c478bd9Sstevel@tonic-gate  * Macro to set kernel context + page size codes in DMMU primary context
20677c478bd9Sstevel@tonic-gate  * register. It is only necessary for sun4u because sun4v does not need
20687c478bd9Sstevel@tonic-gate  * page size codes
20697c478bd9Sstevel@tonic-gate  */
20707c478bd9Sstevel@tonic-gate #ifdef sun4v
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3)
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate #else
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3) \
20777c478bd9Sstevel@tonic-gate 	sethi	%hi(kcontextreg), reg0;					\
20787c478bd9Sstevel@tonic-gate 	ldx	[reg0 + %lo(kcontextreg)], reg0;			\
20797c478bd9Sstevel@tonic-gate 	mov	MMU_PCONTEXT, reg1;					\
20807c478bd9Sstevel@tonic-gate 	ldxa	[reg1]ASI_MMU_CTX, reg2;				\
20817c478bd9Sstevel@tonic-gate 	xor	reg0, reg2, reg2;					\
20827c478bd9Sstevel@tonic-gate 	brz	reg2, label3;						\
20837c478bd9Sstevel@tonic-gate 	srlx	reg2, CTXREG_NEXT_SHIFT, reg2;				\
20847c478bd9Sstevel@tonic-gate 	rdpr	%pstate, reg3;		/* disable interrupts */	\
20857c478bd9Sstevel@tonic-gate 	btst	PSTATE_IE, reg3;					\
20867c478bd9Sstevel@tonic-gate /*CSTYLED*/								\
20877c478bd9Sstevel@tonic-gate 	bnz,a,pt %icc, label1;						\
20887c478bd9Sstevel@tonic-gate 	wrpr	reg3, PSTATE_IE, %pstate;				\
20897c478bd9Sstevel@tonic-gate /*CSTYLED*/								\
20907c478bd9Sstevel@tonic-gate label1:;								\
20917c478bd9Sstevel@tonic-gate 	brz	reg2, label2;	   /* need demap if N_pgsz0/1 change */	\
20927c478bd9Sstevel@tonic-gate 	sethi	%hi(FLUSH_ADDR), reg4;					\
20937c478bd9Sstevel@tonic-gate 	mov	DEMAP_ALL_TYPE, reg2;					\
20947c478bd9Sstevel@tonic-gate 	stxa	%g0, [reg2]ASI_DTLB_DEMAP;				\
20957c478bd9Sstevel@tonic-gate 	stxa	%g0, [reg2]ASI_ITLB_DEMAP;				\
20967c478bd9Sstevel@tonic-gate /*CSTYLED*/								\
20977c478bd9Sstevel@tonic-gate label2:;								\
20987c478bd9Sstevel@tonic-gate 	stxa	reg0, [reg1]ASI_MMU_CTX;				\
20997c478bd9Sstevel@tonic-gate 	flush	reg4;							\
21007c478bd9Sstevel@tonic-gate 	btst	PSTATE_IE, reg3;					\
21017c478bd9Sstevel@tonic-gate /*CSTYLED*/								\
21027c478bd9Sstevel@tonic-gate 	bnz,a,pt %icc, label3;						\
21037c478bd9Sstevel@tonic-gate 	wrpr	%g0, reg3, %pstate;	/* restore interrupt state */	\
21047c478bd9Sstevel@tonic-gate label3:;
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate #endif
21077c478bd9Sstevel@tonic-gate 
21081e2e7a75Shuah /*
21091e2e7a75Shuah  * Macro to setup arguments with kernel sfmmup context + page size before
21101e2e7a75Shuah  * calling sfmmu_setctx_sec()
21111e2e7a75Shuah  */
21121e2e7a75Shuah #ifdef sun4v
21131e2e7a75Shuah #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
21141e2e7a75Shuah 	set	KCONTEXT, arg0;					\
21151e2e7a75Shuah 	set	0, arg1;
21161e2e7a75Shuah #else
21171e2e7a75Shuah #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
21181e2e7a75Shuah 	ldub	[sfmmup + SFMMU_CEXT], arg1;			\
21191e2e7a75Shuah 	set	KCONTEXT, arg0;					\
21201e2e7a75Shuah 	sll	arg1, CTXREG_EXT_SHIFT, arg1;
21211e2e7a75Shuah #endif
21221e2e7a75Shuah 
21231e2e7a75Shuah #define	PANIC_IF_INTR_DISABLED_PSTR(pstatereg, label, scr)	       	\
21241e2e7a75Shuah 	andcc	pstatereg, PSTATE_IE, %g0;	/* panic if intrs */	\
21251e2e7a75Shuah /*CSTYLED*/								\
21261e2e7a75Shuah 	bnz,pt	%icc, label;			/* already disabled */	\
21271e2e7a75Shuah 	nop;								\
21281e2e7a75Shuah 									\
21291e2e7a75Shuah 	sethi	%hi(panicstr), scr;					\
21301e2e7a75Shuah 	ldx	[scr + %lo(panicstr)], scr;				\
21311e2e7a75Shuah 	tst	scr;							\
21321e2e7a75Shuah /*CSTYLED*/								\
21331e2e7a75Shuah 	bnz,pt	%xcc, label;						\
21341e2e7a75Shuah 	nop;								\
21351e2e7a75Shuah 									\
21361e2e7a75Shuah 	save	%sp, -SA(MINFRAME), %sp;				\
21371e2e7a75Shuah 	sethi	%hi(sfmmu_panic1), %o0;					\
21381e2e7a75Shuah 	call	panic;							\
21391e2e7a75Shuah 	or	%o0, %lo(sfmmu_panic1), %o0;				\
21401e2e7a75Shuah /*CSTYLED*/								\
21411e2e7a75Shuah label:
21421e2e7a75Shuah 
21431e2e7a75Shuah #define	PANIC_IF_INTR_ENABLED_PSTR(label, scr)				\
21441e2e7a75Shuah 	/*								\
21451e2e7a75Shuah 	 * The caller must have disabled interrupts.			\
21461e2e7a75Shuah 	 * If interrupts are not disabled, panic			\
21471e2e7a75Shuah 	 */								\
21481e2e7a75Shuah 	rdpr	%pstate, scr;						\
21491e2e7a75Shuah 	andcc	scr, PSTATE_IE, %g0;					\
21501e2e7a75Shuah /*CSTYLED*/								\
21511e2e7a75Shuah 	bz,pt	%icc, label;						\
21521e2e7a75Shuah 	nop;								\
21531e2e7a75Shuah 									\
21541e2e7a75Shuah 	sethi	%hi(panicstr), scr;					\
21551e2e7a75Shuah 	ldx	[scr + %lo(panicstr)], scr;				\
21561e2e7a75Shuah 	tst	scr;							\
21571e2e7a75Shuah /*CSTYLED*/								\
21581e2e7a75Shuah 	bnz,pt	%xcc, label;						\
21591e2e7a75Shuah 	nop;								\
21601e2e7a75Shuah 									\
21611e2e7a75Shuah 	sethi	%hi(sfmmu_panic6), %o0;					\
21621e2e7a75Shuah 	call	panic;							\
21631e2e7a75Shuah 	or	%o0, %lo(sfmmu_panic6), %o0;				\
21641e2e7a75Shuah /*CSTYLED*/								\
21651e2e7a75Shuah label:
21661e2e7a75Shuah 
21677c478bd9Sstevel@tonic-gate #endif	/* _ASM */
21687c478bd9Sstevel@tonic-gate 
21697c478bd9Sstevel@tonic-gate #ifndef _ASM
21707c478bd9Sstevel@tonic-gate 
2171fedab560Sae112802 #ifdef VAC
21727c478bd9Sstevel@tonic-gate /*
21737c478bd9Sstevel@tonic-gate  * Page coloring
21747c478bd9Sstevel@tonic-gate  * The p_vcolor field of the page struct (1 byte) is used to store the
21757c478bd9Sstevel@tonic-gate  * virtual page color.  This provides for 255 colors.  The value zero is
21767c478bd9Sstevel@tonic-gate  * used to mean the page has no color - never been mapped or somehow
21777c478bd9Sstevel@tonic-gate  * purified.
21787c478bd9Sstevel@tonic-gate  */
21797c478bd9Sstevel@tonic-gate 
21807c478bd9Sstevel@tonic-gate #define	PP_GET_VCOLOR(pp)	(((pp)->p_vcolor) - 1)
21817c478bd9Sstevel@tonic-gate #define	PP_NEWPAGE(pp)		(!(pp)->p_vcolor)
21827c478bd9Sstevel@tonic-gate #define	PP_SET_VCOLOR(pp, color)                                          \
21837c478bd9Sstevel@tonic-gate 	((pp)->p_vcolor = ((color) + 1))
21847c478bd9Sstevel@tonic-gate 
21857c478bd9Sstevel@tonic-gate /*
21867c478bd9Sstevel@tonic-gate  * As mentioned p_vcolor == 0 means there is no color for this page.
21877c478bd9Sstevel@tonic-gate  * But PP_SET_VCOLOR(pp, color) expects 'color' to be real color minus
21887c478bd9Sstevel@tonic-gate  * one so we define this constant.
21897c478bd9Sstevel@tonic-gate  */
21907c478bd9Sstevel@tonic-gate #define	NO_VCOLOR	(-1)
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate #define	addr_to_vcolor(addr) \
21939f1a1f17Sdmick 	(((uint_t)(uintptr_t)(addr) >> MMU_PAGESHIFT) & vac_colors_mask)
2194fedab560Sae112802 #else	/* VAC */
2195fedab560Sae112802 #define	addr_to_vcolor(addr)	(0)
2196fedab560Sae112802 #endif	/* VAC */
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate /*
21997c478bd9Sstevel@tonic-gate  * The field p_index in the psm page structure is for large pages support.
22007c478bd9Sstevel@tonic-gate  * P_index is a bit-vector of the different mapping sizes that a given page
22017c478bd9Sstevel@tonic-gate  * is part of. An hme structure for a large mapping is only added in the
22027c478bd9Sstevel@tonic-gate  * group leader page (first page). All pages covered by a given large mapping
22037c478bd9Sstevel@tonic-gate  * have the corrosponding mapping bit set in their p_index field. This allows
22047c478bd9Sstevel@tonic-gate  * us to only store an explicit hme structure in the leading page which
22057c478bd9Sstevel@tonic-gate  * simplifies the mapping link list management. Furthermore, it provides us
22067c478bd9Sstevel@tonic-gate  * a fast mechanism for determining the largest mapping a page is part of. For
22077c478bd9Sstevel@tonic-gate  * exmaple, a page with a 64K and a 4M mappings has a p_index value of 0x0A.
22087c478bd9Sstevel@tonic-gate  *
22097c478bd9Sstevel@tonic-gate  * Implementation note: even though the first bit in p_index is reserved
22107c478bd9Sstevel@tonic-gate  * for 8K mappings, it is NOT USED by the code and SHOULD NOT be set.
22117c478bd9Sstevel@tonic-gate  * In addition, the upper four bits of the p_index field are used by the
22127c478bd9Sstevel@tonic-gate  * code as temporaries
22137c478bd9Sstevel@tonic-gate  */
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate /*
22167c478bd9Sstevel@tonic-gate  * Defines for psm page struct fields and large page support
22177c478bd9Sstevel@tonic-gate  */
22187c478bd9Sstevel@tonic-gate #define	SFMMU_INDEX_SHIFT		6
22197c478bd9Sstevel@tonic-gate #define	SFMMU_INDEX_MASK		((1 << SFMMU_INDEX_SHIFT) - 1)
22207c478bd9Sstevel@tonic-gate 
22217c478bd9Sstevel@tonic-gate /* Return the mapping index */
22227c478bd9Sstevel@tonic-gate #define	PP_MAPINDEX(pp)	((pp)->p_index & SFMMU_INDEX_MASK)
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate /*
22257c478bd9Sstevel@tonic-gate  * These macros rely on the following property:
22267c478bd9Sstevel@tonic-gate  * All pages constituting a large page are covered by a virtually
22277c478bd9Sstevel@tonic-gate  * contiguous set of page_t's.
22287c478bd9Sstevel@tonic-gate  */
22297c478bd9Sstevel@tonic-gate 
22307c478bd9Sstevel@tonic-gate /* Return the leader for this mapping size */
22317c478bd9Sstevel@tonic-gate #define	PP_GROUPLEADER(pp, sz) \
22327c478bd9Sstevel@tonic-gate 	(&(pp)[-(int)(pp->p_pagenum & (TTEPAGES(sz)-1))])
22337c478bd9Sstevel@tonic-gate 
22347c478bd9Sstevel@tonic-gate /* Return the root page for this page based on p_szc */
22357c478bd9Sstevel@tonic-gate #define	PP_PAGEROOT(pp)	((pp)->p_szc == 0 ? (pp) : \
22367c478bd9Sstevel@tonic-gate 	PP_GROUPLEADER((pp), (pp)->p_szc))
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate #define	PP_PAGENEXT_N(pp, n)	((pp) + (n))
22397c478bd9Sstevel@tonic-gate #define	PP_PAGENEXT(pp)		PP_PAGENEXT_N((pp), 1)
22407c478bd9Sstevel@tonic-gate 
22417c478bd9Sstevel@tonic-gate #define	PP_PAGEPREV_N(pp, n)	((pp) - (n))
22427c478bd9Sstevel@tonic-gate #define	PP_PAGEPREV(pp)		PP_PAGEPREV_N((pp), 1)
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate #define	PP_ISMAPPED_LARGE(pp)	(PP_MAPINDEX(pp) != 0)
22457c478bd9Sstevel@tonic-gate 
22467c478bd9Sstevel@tonic-gate /* Need function to test the page mappping which takes p_index into account */
22477c478bd9Sstevel@tonic-gate #define	PP_ISMAPPED(pp)	((pp)->p_mapping || PP_ISMAPPED_LARGE(pp))
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate /*
22507c478bd9Sstevel@tonic-gate  * Don't call this macro with sz equal to zero. 8K mappings SHOULD NOT
22517c478bd9Sstevel@tonic-gate  * set p_index field.
22527c478bd9Sstevel@tonic-gate  */
22537c478bd9Sstevel@tonic-gate #define	PAGESZ_TO_INDEX(sz)	(1 << (sz))
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate /*
22577c478bd9Sstevel@tonic-gate  * prototypes for hat assembly routines.  Some of these are
22587c478bd9Sstevel@tonic-gate  * known to machine dependent VM code.
22597c478bd9Sstevel@tonic-gate  */
22607c478bd9Sstevel@tonic-gate extern uint64_t sfmmu_make_tsbtag(caddr_t);
22617c478bd9Sstevel@tonic-gate extern struct tsbe *
22627c478bd9Sstevel@tonic-gate 		sfmmu_get_tsbe(uint64_t, caddr_t, int, int);
22637c478bd9Sstevel@tonic-gate extern void	sfmmu_load_tsbe(struct tsbe *, uint64_t, tte_t *, int);
22647c478bd9Sstevel@tonic-gate extern void	sfmmu_unload_tsbe(struct tsbe *, uint64_t, int);
22657c478bd9Sstevel@tonic-gate extern void	sfmmu_load_mmustate(sfmmu_t *);
22667c478bd9Sstevel@tonic-gate extern void	sfmmu_raise_tsb_exception(uint64_t, uint64_t);
22677c478bd9Sstevel@tonic-gate #ifndef sun4v
22681e2e7a75Shuah extern void	sfmmu_itlb_ld_kva(caddr_t, tte_t *);
22691e2e7a75Shuah extern void	sfmmu_dtlb_ld_kva(caddr_t, tte_t *);
22707c478bd9Sstevel@tonic-gate #endif /* sun4v */
22717c478bd9Sstevel@tonic-gate extern void	sfmmu_copytte(tte_t *, tte_t *);
22727c478bd9Sstevel@tonic-gate extern int	sfmmu_modifytte(tte_t *, tte_t *, tte_t *);
22737c478bd9Sstevel@tonic-gate extern int	sfmmu_modifytte_try(tte_t *, tte_t *, tte_t *);
22747c478bd9Sstevel@tonic-gate extern pfn_t	sfmmu_ttetopfn(tte_t *, caddr_t);
22751e2e7a75Shuah extern uint_t	sfmmu_disable_intrs(void);
22761e2e7a75Shuah extern void	sfmmu_enable_intrs(uint_t);
22777c478bd9Sstevel@tonic-gate /*
22787c478bd9Sstevel@tonic-gate  * functions exported to machine dependent VM code
22797c478bd9Sstevel@tonic-gate  */
22807c478bd9Sstevel@tonic-gate extern void	sfmmu_patch_ktsb(void);
228125cf1a30Sjl139090 #ifndef UTSB_PHYS
22827c478bd9Sstevel@tonic-gate extern void	sfmmu_patch_utsb(void);
228325cf1a30Sjl139090 #endif /* UTSB_PHYS */
22847c478bd9Sstevel@tonic-gate extern pfn_t	sfmmu_vatopfn(caddr_t, sfmmu_t *, tte_t *);
22857c478bd9Sstevel@tonic-gate extern void	sfmmu_vatopfn_suspended(caddr_t, sfmmu_t *, tte_t *);
2286081a94b0Saguzovsk extern pfn_t	sfmmu_kvaszc2pfn(caddr_t, int);
22877c478bd9Sstevel@tonic-gate #ifdef	DEBUG
22887c478bd9Sstevel@tonic-gate extern void	sfmmu_check_kpfn(pfn_t);
22897c478bd9Sstevel@tonic-gate #else
22907c478bd9Sstevel@tonic-gate #define		sfmmu_check_kpfn(pfn)	/* disabled */
22917c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
22927c478bd9Sstevel@tonic-gate extern void	sfmmu_memtte(tte_t *, pfn_t, uint_t, int);
22937c478bd9Sstevel@tonic-gate extern void	sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *,	uint_t);
22947c478bd9Sstevel@tonic-gate extern void	sfmmu_tsbmiss_exception(struct regs *, uintptr_t, uint_t);
22957c478bd9Sstevel@tonic-gate extern void	sfmmu_init_tsbs(void);
22967c478bd9Sstevel@tonic-gate extern caddr_t  sfmmu_ktsb_alloc(caddr_t);
22977c478bd9Sstevel@tonic-gate extern int	sfmmu_getctx_pri(void);
22987c478bd9Sstevel@tonic-gate extern int	sfmmu_getctx_sec(void);
229905d3dc4bSpaulsan extern void	sfmmu_setctx_sec(uint_t);
23007c478bd9Sstevel@tonic-gate extern void	sfmmu_inv_tsb(caddr_t, uint_t);
23017c478bd9Sstevel@tonic-gate extern void	sfmmu_init_ktsbinfo(void);
23027c478bd9Sstevel@tonic-gate extern int	sfmmu_setup_4lp(void);
23037c478bd9Sstevel@tonic-gate extern void	sfmmu_patch_mmu_asi(int);
23047c478bd9Sstevel@tonic-gate extern void	sfmmu_init_nucleus_hblks(caddr_t, size_t, int, int);
23057c478bd9Sstevel@tonic-gate extern void	sfmmu_cache_flushall(void);
23067c478bd9Sstevel@tonic-gate extern pgcnt_t  sfmmu_tte_cnt(sfmmu_t *, uint_t);
23077c478bd9Sstevel@tonic-gate extern void	*sfmmu_tsb_segkmem_alloc(vmem_t *, size_t, int);
23087c478bd9Sstevel@tonic-gate extern void	sfmmu_tsb_segkmem_free(vmem_t *, void *, size_t);
23091e2e7a75Shuah extern void	sfmmu_reprog_pgsz_arr(sfmmu_t *, uint8_t *);
23107c478bd9Sstevel@tonic-gate 
23117c478bd9Sstevel@tonic-gate extern void	hat_kern_setup(void);
23127c478bd9Sstevel@tonic-gate extern int	hat_page_relocate(page_t **, page_t **, spgcnt_t *);
23137c478bd9Sstevel@tonic-gate extern int	sfmmu_get_ppvcolor(struct page *);
23147c478bd9Sstevel@tonic-gate extern int	sfmmu_get_addrvcolor(caddr_t);
23157c478bd9Sstevel@tonic-gate extern int	sfmmu_hat_lock_held(sfmmu_t *);
231605d3dc4bSpaulsan extern int	sfmmu_alloc_ctx(sfmmu_t *, int, struct cpu *, int);
23177c478bd9Sstevel@tonic-gate 
23187c478bd9Sstevel@tonic-gate /*
23197c478bd9Sstevel@tonic-gate  * Functions exported to xhat_sfmmu.c
23207c478bd9Sstevel@tonic-gate  */
23217c478bd9Sstevel@tonic-gate extern kmutex_t *sfmmu_mlist_enter(page_t *);
23227c478bd9Sstevel@tonic-gate extern void	sfmmu_mlist_exit(kmutex_t *);
23237c478bd9Sstevel@tonic-gate extern int	sfmmu_mlist_held(struct page *);
23247c478bd9Sstevel@tonic-gate extern struct hme_blk *sfmmu_hmetohblk(struct sf_hment *);
23257c478bd9Sstevel@tonic-gate 
23267c478bd9Sstevel@tonic-gate /*
23277c478bd9Sstevel@tonic-gate  * MMU-specific functions optionally imported from the CPU module
23287c478bd9Sstevel@tonic-gate  */
232920064263SSean McEnroe #pragma weak mmu_init_scd
23307c478bd9Sstevel@tonic-gate #pragma weak mmu_large_pages_disabled
23317c478bd9Sstevel@tonic-gate #pragma weak mmu_set_ctx_page_sizes
23327c478bd9Sstevel@tonic-gate #pragma weak mmu_check_page_sizes
23337c478bd9Sstevel@tonic-gate 
233420064263SSean McEnroe extern void mmu_init_scd(sf_scd_t *);
2335ec25b48fSsusans extern uint_t mmu_large_pages_disabled(uint_t);
23367c478bd9Sstevel@tonic-gate extern void mmu_set_ctx_page_sizes(sfmmu_t *);
23377c478bd9Sstevel@tonic-gate extern void mmu_check_page_sizes(sfmmu_t *, uint64_t *);
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate extern sfmmu_t 		*ksfmmup;
23407c478bd9Sstevel@tonic-gate extern caddr_t		ktsb_base;
23417c478bd9Sstevel@tonic-gate extern uint64_t		ktsb_pbase;
23427c478bd9Sstevel@tonic-gate extern int		ktsb_sz;
23437c478bd9Sstevel@tonic-gate extern int		ktsb_szcode;
23447c478bd9Sstevel@tonic-gate extern caddr_t		ktsb4m_base;
23457c478bd9Sstevel@tonic-gate extern uint64_t		ktsb4m_pbase;
23467c478bd9Sstevel@tonic-gate extern int		ktsb4m_sz;
23477c478bd9Sstevel@tonic-gate extern int		ktsb4m_szcode;
23487c478bd9Sstevel@tonic-gate extern uint64_t		kpm_tsbbase;
23497c478bd9Sstevel@tonic-gate extern int		kpm_tsbsz;
23507c478bd9Sstevel@tonic-gate extern int		ktsb_phys;
23517c478bd9Sstevel@tonic-gate extern int		enable_bigktsb;
23527c478bd9Sstevel@tonic-gate #ifndef sun4v
23537c478bd9Sstevel@tonic-gate extern int		utsb_dtlb_ttenum;
23547c478bd9Sstevel@tonic-gate extern int		utsb4m_dtlb_ttenum;
23557c478bd9Sstevel@tonic-gate #endif /* sun4v */
23567c478bd9Sstevel@tonic-gate extern int		uhmehash_num;
23577c478bd9Sstevel@tonic-gate extern int		khmehash_num;
23587c478bd9Sstevel@tonic-gate extern struct hmehash_bucket *uhme_hash;
23597c478bd9Sstevel@tonic-gate extern struct hmehash_bucket *khme_hash;
23607c478bd9Sstevel@tonic-gate extern uint_t		hblk_alloc_dynamic;
23617c478bd9Sstevel@tonic-gate extern struct tsbmiss	tsbmiss_area[NCPU];
23627c478bd9Sstevel@tonic-gate extern struct kpmtsbm	kpmtsbm_area[NCPU];
236305d3dc4bSpaulsan 
23647c478bd9Sstevel@tonic-gate #ifndef sun4v
23657c478bd9Sstevel@tonic-gate extern int		dtlb_resv_ttenum;
23667c478bd9Sstevel@tonic-gate extern caddr_t		utsb_vabase;
23677c478bd9Sstevel@tonic-gate extern caddr_t		utsb4m_vabase;
23687c478bd9Sstevel@tonic-gate #endif /* sun4v */
23697c478bd9Sstevel@tonic-gate extern vmem_t		*kmem_tsb_default_arena[];
23707c478bd9Sstevel@tonic-gate extern int		tsb_lgrp_affinity;
23717c478bd9Sstevel@tonic-gate 
2372ec25b48fSsusans extern uint_t		disable_large_pages;
2373ec25b48fSsusans extern uint_t		disable_ism_large_pages;
2374ec25b48fSsusans extern uint_t		disable_auto_data_large_pages;
2375ec25b48fSsusans extern uint_t		disable_auto_text_large_pages;
2376ec25b48fSsusans 
23777c478bd9Sstevel@tonic-gate /* kpm externals */
23787c478bd9Sstevel@tonic-gate extern pfn_t		sfmmu_kpm_vatopfn(caddr_t);
23797c478bd9Sstevel@tonic-gate extern void		sfmmu_kpm_patch_tlbm(void);
23807c478bd9Sstevel@tonic-gate extern void		sfmmu_kpm_patch_tsbm(void);
23819d0d62adSJason Beloro extern void		sfmmu_patch_shctx(void);
23827c478bd9Sstevel@tonic-gate extern void		sfmmu_kpm_load_tsb(caddr_t, tte_t *, int);
23837c478bd9Sstevel@tonic-gate extern void		sfmmu_kpm_unload_tsb(caddr_t, int);
23847c478bd9Sstevel@tonic-gate extern void		sfmmu_kpm_tsbmtl(short *, uint_t *, int);
2385444ce08eSDonghai Qiao extern int		sfmmu_kpm_stsbmtl(uchar_t *, uint_t *, int);
23867c478bd9Sstevel@tonic-gate extern caddr_t		kpm_vbase;
23877c478bd9Sstevel@tonic-gate extern size_t		kpm_size;
23887c478bd9Sstevel@tonic-gate extern struct memseg	*memseg_hash[];
23897c478bd9Sstevel@tonic-gate extern uint64_t		memseg_phash[];
23907c478bd9Sstevel@tonic-gate extern kpm_hlk_t	*kpmp_table;
23917c478bd9Sstevel@tonic-gate extern kpm_shlk_t	*kpmp_stable;
23927c478bd9Sstevel@tonic-gate extern uint_t		kpmp_table_sz;
23937c478bd9Sstevel@tonic-gate extern uint_t		kpmp_stable_sz;
23947c478bd9Sstevel@tonic-gate extern uchar_t		kpmp_shift;
23957c478bd9Sstevel@tonic-gate 
23967c478bd9Sstevel@tonic-gate #define	PP_ISMAPPED_KPM(pp)	((pp)->p_kpmref > 0)
23977c478bd9Sstevel@tonic-gate 
23987c478bd9Sstevel@tonic-gate #define	IS_KPM_ALIAS_RANGE(vaddr)					\
23997c478bd9Sstevel@tonic-gate 	(((vaddr) - kpm_vbase) >> (uintptr_t)kpm_size_shift > 0)
24007c478bd9Sstevel@tonic-gate 
24017c478bd9Sstevel@tonic-gate #endif /* !_ASM */
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate /* sfmmu_kpm_tsbmtl flags */
24047c478bd9Sstevel@tonic-gate #define	KPMTSBM_STOP		0
24057c478bd9Sstevel@tonic-gate #define	KPMTSBM_START		1
24067c478bd9Sstevel@tonic-gate 
2407444ce08eSDonghai Qiao /*
2408444ce08eSDonghai Qiao  * For kpm_smallpages, the state about how a kpm page is mapped and whether
2409444ce08eSDonghai Qiao  * it is ready to go is indicated by the two 4-bit fields defined in the
2410444ce08eSDonghai Qiao  * kpm_spage structure as follows:
2411444ce08eSDonghai Qiao  * kp_mapped_flag bit[0:3] - the page is mapped cacheable or not
2412444ce08eSDonghai Qiao  * kp_mapped_flag bit[4:7] - the mapping is ready to go or not
2413444ce08eSDonghai Qiao  * If the bit KPM_MAPPED_GO is on, it indicates that the assembly tsb miss
2414444ce08eSDonghai Qiao  * handler can drop the mapping in regardless of the caching state of the
2415444ce08eSDonghai Qiao  * mapping. Otherwise, we will have C handler resolve the VAC conflict no
2416444ce08eSDonghai Qiao  * matter the page is currently mapped cacheable or non-cacheable.
2417444ce08eSDonghai Qiao  */
2418444ce08eSDonghai Qiao #define	KPM_MAPPEDS		0x1	/* small mapping valid, no conflict */
2419444ce08eSDonghai Qiao #define	KPM_MAPPEDSC		0x2	/* small mapping valid, conflict */
2420444ce08eSDonghai Qiao #define	KPM_MAPPED_GO		0x10	/* the mapping is ready to go */
2421444ce08eSDonghai Qiao #define	KPM_MAPPED_MASK		0xf
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate /* Physical memseg address NULL marker */
24247c478bd9Sstevel@tonic-gate #define	MSEG_NULLPTR_PA		-1
24257c478bd9Sstevel@tonic-gate 
24267c478bd9Sstevel@tonic-gate /*
24277c478bd9Sstevel@tonic-gate  * Memseg hash defines for kpm trap level tsbmiss handler.
24287c478bd9Sstevel@tonic-gate  * Must be in sync w/ page.h .
24297c478bd9Sstevel@tonic-gate  */
24307c478bd9Sstevel@tonic-gate #define	SFMMU_MEM_HASH_SHIFT		0x9
24317c478bd9Sstevel@tonic-gate #define	SFMMU_N_MEM_SLOTS		0x200
24327c478bd9Sstevel@tonic-gate #define	SFMMU_MEM_HASH_ENTRY_SHIFT	3
24337c478bd9Sstevel@tonic-gate 
24347c478bd9Sstevel@tonic-gate #ifndef	_ASM
24357c478bd9Sstevel@tonic-gate #if (SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT)
24367c478bd9Sstevel@tonic-gate #error SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT
24377c478bd9Sstevel@tonic-gate #endif
24387c478bd9Sstevel@tonic-gate #if (SFMMU_N_MEM_SLOTS != N_MEM_SLOTS)
24397c478bd9Sstevel@tonic-gate #error SFMMU_N_MEM_SLOTS != N_MEM_SLOTS
24407c478bd9Sstevel@tonic-gate #endif
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate /* Physical memseg address NULL marker */
24437c478bd9Sstevel@tonic-gate #define	SFMMU_MEMSEG_NULLPTR_PA		-1
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate /*
24467c478bd9Sstevel@tonic-gate  * Check KCONTEXT to be zero, asm parts depend on that assumption.
24477c478bd9Sstevel@tonic-gate  */
24487c478bd9Sstevel@tonic-gate #if (KCONTEXT != 0)
24497c478bd9Sstevel@tonic-gate #error KCONTEXT != 0
24507c478bd9Sstevel@tonic-gate #endif
24517c478bd9Sstevel@tonic-gate #endif	/* !_ASM */
24527c478bd9Sstevel@tonic-gate 
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
24557c478bd9Sstevel@tonic-gate 
24567c478bd9Sstevel@tonic-gate #ifndef _ASM
24577c478bd9Sstevel@tonic-gate /*
24587c478bd9Sstevel@tonic-gate  * ctx, hmeblk, mlistlock and other stats for sfmmu
24597c478bd9Sstevel@tonic-gate  */
24607c478bd9Sstevel@tonic-gate struct sfmmu_global_stat {
24617c478bd9Sstevel@tonic-gate 	int		sf_tsb_exceptions;	/* # of tsb exceptions */
24627c478bd9Sstevel@tonic-gate 	int		sf_tsb_raise_exception;	/* # tsb exc. w/o TLB flush */
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	int		sf_pagefaults;		/* # of pagefaults */
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	int		sf_uhash_searches;	/* # of user hash searches */
24677c478bd9Sstevel@tonic-gate 	int		sf_uhash_links;		/* # of user hash links */
24687c478bd9Sstevel@tonic-gate 	int		sf_khash_searches;	/* # of kernel hash searches */
24697c478bd9Sstevel@tonic-gate 	int		sf_khash_links;		/* # of kernel hash links */
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	int		sf_swapout;		/* # times hat swapped out */
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	int		sf_tsb_alloc;		/* # TSB allocations */
24747c478bd9Sstevel@tonic-gate 	int		sf_tsb_allocfail;	/* # times TSB alloc fail */
24757c478bd9Sstevel@tonic-gate 	int		sf_tsb_sectsb_create;	/* # times second TSB added */
24767c478bd9Sstevel@tonic-gate 
247705d3dc4bSpaulsan 	int		sf_scd_1sttsb_alloc;	/* # SCD 1st TSB allocations */
247805d3dc4bSpaulsan 	int		sf_scd_2ndtsb_alloc;	/* # SCD 2nd TSB allocations */
247905d3dc4bSpaulsan 	int		sf_scd_1sttsb_allocfail; /* # SCD 1st TSB alloc fail */
248005d3dc4bSpaulsan 	int		sf_scd_2ndtsb_allocfail; /* # SCD 2nd TSB alloc fail */
248105d3dc4bSpaulsan 
248205d3dc4bSpaulsan 
24837c478bd9Sstevel@tonic-gate 	int		sf_tteload8k;		/* calls to sfmmu_tteload */
24847c478bd9Sstevel@tonic-gate 	int		sf_tteload64k;		/* calls to sfmmu_tteload */
24857c478bd9Sstevel@tonic-gate 	int		sf_tteload512k;		/* calls to sfmmu_tteload */
24867c478bd9Sstevel@tonic-gate 	int		sf_tteload4m;		/* calls to sfmmu_tteload */
24877c478bd9Sstevel@tonic-gate 	int		sf_tteload32m;		/* calls to sfmmu_tteload */
24887c478bd9Sstevel@tonic-gate 	int		sf_tteload256m;		/* calls to sfmmu_tteload */
24897c478bd9Sstevel@tonic-gate 
24907c478bd9Sstevel@tonic-gate 	int		sf_tsb_load8k;		/* # times loaded 8K tsbent */
24917c478bd9Sstevel@tonic-gate 	int		sf_tsb_load4m;		/* # times loaded 4M tsbent */
24927c478bd9Sstevel@tonic-gate 
24937c478bd9Sstevel@tonic-gate 	int		sf_hblk_hit;		/* found hblk during tteload */
24947c478bd9Sstevel@tonic-gate 	int		sf_hblk8_ncreate;	/* static hblk8's created */
24957c478bd9Sstevel@tonic-gate 	int		sf_hblk8_nalloc;	/* static hblk8's allocated */
24967c478bd9Sstevel@tonic-gate 	int		sf_hblk1_ncreate;	/* static hblk1's created */
24977c478bd9Sstevel@tonic-gate 	int		sf_hblk1_nalloc;	/* static hblk1's allocated */
24987c478bd9Sstevel@tonic-gate 	int		sf_hblk_slab_cnt;	/* sfmmu8_cache slab creates */
24997c478bd9Sstevel@tonic-gate 	int		sf_hblk_reserve_cnt;	/* hblk_reserve usage */
25007c478bd9Sstevel@tonic-gate 	int		sf_hblk_recurse_cnt;	/* hblk_reserve	owner reqs */
25017c478bd9Sstevel@tonic-gate 	int		sf_hblk_reserve_hit;	/* hblk_reserve hash hits */
25027c478bd9Sstevel@tonic-gate 	int		sf_get_free_success;	/* reserve list allocs */
25037c478bd9Sstevel@tonic-gate 	int		sf_get_free_throttle;	/* fails due to throttling */
25047c478bd9Sstevel@tonic-gate 	int		sf_get_free_fail;	/* fails due to empty list */
25057c478bd9Sstevel@tonic-gate 	int		sf_put_free_success;	/* reserve list frees */
25067c478bd9Sstevel@tonic-gate 	int		sf_put_free_fail;	/* fails due to full list */
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 	int		sf_pgcolor_conflict;	/* VAC conflict resolution */
25097c478bd9Sstevel@tonic-gate 	int		sf_uncache_conflict;	/* VAC conflict resolution */
25107c478bd9Sstevel@tonic-gate 	int		sf_unload_conflict;	/* VAC unload resolution */
25117c478bd9Sstevel@tonic-gate 	int		sf_ism_uncache;		/* VAC conflict resolution */
25127c478bd9Sstevel@tonic-gate 	int		sf_ism_recache;		/* VAC conflict resolution */
25137c478bd9Sstevel@tonic-gate 	int		sf_recache;		/* VAC conflict resolution */
25147c478bd9Sstevel@tonic-gate 
25157c478bd9Sstevel@tonic-gate 	int		sf_steal_count;		/* # of hblks stolen */
25167c478bd9Sstevel@tonic-gate 
25177c478bd9Sstevel@tonic-gate 	int		sf_pagesync;		/* # of pagesyncs */
25187c478bd9Sstevel@tonic-gate 	int		sf_clrwrt;		/* # of clear write perms */
25197c478bd9Sstevel@tonic-gate 	int		sf_pagesync_invalid;	/* pagesync with inv tte */
25207c478bd9Sstevel@tonic-gate 
25217c478bd9Sstevel@tonic-gate 	int		sf_kernel_xcalls;	/* # of kernel cross calls */
25227c478bd9Sstevel@tonic-gate 	int		sf_user_xcalls;		/* # of user cross calls */
25237c478bd9Sstevel@tonic-gate 
25247c478bd9Sstevel@tonic-gate 	int		sf_tsb_grow;		/* # of user tsb grows */
25257c478bd9Sstevel@tonic-gate 	int		sf_tsb_shrink;		/* # of user tsb shrinks */
25267c478bd9Sstevel@tonic-gate 	int		sf_tsb_resize_failures;	/* # of user tsb resize */
25277c478bd9Sstevel@tonic-gate 	int		sf_tsb_reloc;		/* # of user tsb relocations */
25287c478bd9Sstevel@tonic-gate 
25297c478bd9Sstevel@tonic-gate 	int		sf_user_vtop;		/* # of user vatopfn calls */
25307c478bd9Sstevel@tonic-gate 
25311e2e7a75Shuah 	int		sf_ctx_inv;		/* #times invalidate MMU ctx */
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 	int		sf_tlb_reprog_pgsz;	/* # times switch TLB pgsz */
253405d3dc4bSpaulsan 
253505d3dc4bSpaulsan 	int		sf_region_remap_demap;	/* # times shme remap demap */
253605d3dc4bSpaulsan 
253705d3dc4bSpaulsan 	int		sf_create_scd;		/* # times SCD is created */
253805d3dc4bSpaulsan 	int		sf_join_scd;		/* # process joined scd */
253905d3dc4bSpaulsan 	int		sf_leave_scd;		/* # process left scd */
254005d3dc4bSpaulsan 	int		sf_destroy_scd;		/* # times SCD is destroyed */
25417c478bd9Sstevel@tonic-gate };
25427c478bd9Sstevel@tonic-gate 
25437c478bd9Sstevel@tonic-gate struct sfmmu_tsbsize_stat {
25447c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_8k;
25457c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_16k;
25467c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_32k;
25477c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_64k;
25487c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_128k;
25497c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_256k;
25507c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_512k;
25517c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_1m;
25527c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_2m;
25537c478bd9Sstevel@tonic-gate 	int		sf_tsbsz_4m;
255405d3dc4bSpaulsan 	int		sf_tsbsz_8m;
255505d3dc4bSpaulsan 	int		sf_tsbsz_16m;
255605d3dc4bSpaulsan 	int		sf_tsbsz_32m;
255705d3dc4bSpaulsan 	int		sf_tsbsz_64m;
255805d3dc4bSpaulsan 	int		sf_tsbsz_128m;
255905d3dc4bSpaulsan 	int		sf_tsbsz_256m;
25607c478bd9Sstevel@tonic-gate };
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate struct sfmmu_percpu_stat {
25637c478bd9Sstevel@tonic-gate 	int	sf_itlb_misses;		/* # of itlb misses */
25647c478bd9Sstevel@tonic-gate 	int	sf_dtlb_misses;		/* # of dtlb misses */
25657c478bd9Sstevel@tonic-gate 	int	sf_utsb_misses;		/* # of user tsb misses */
25667c478bd9Sstevel@tonic-gate 	int	sf_ktsb_misses;		/* # of kernel tsb misses */
25677c478bd9Sstevel@tonic-gate 	int	sf_tsb_hits;		/* # of tsb hits */
25687c478bd9Sstevel@tonic-gate 	int	sf_umod_faults;		/* # of mod (prot viol) flts */
25697c478bd9Sstevel@tonic-gate 	int	sf_kmod_faults;		/* # of mod (prot viol) flts */
25707c478bd9Sstevel@tonic-gate };
25717c478bd9Sstevel@tonic-gate 
25721e2e7a75Shuah #define	SFMMU_STAT(stat)		sfmmu_global_stat.stat++
25731e2e7a75Shuah #define	SFMMU_STAT_ADD(stat, amount)	sfmmu_global_stat.stat += (amount)
25741e2e7a75Shuah #define	SFMMU_STAT_SET(stat, count)	sfmmu_global_stat.stat = (count)
25751e2e7a75Shuah 
2576d2365b01SPavel Tatashin #define	SFMMU_MMU_STAT(stat)		{		\
2577d2365b01SPavel Tatashin 	mmu_ctx_t *ctx = CPU->cpu_m.cpu_mmu_ctxp;	\
2578d2365b01SPavel Tatashin 	if (ctx)					\
2579d2365b01SPavel Tatashin 		ctx->stat++;				\
2580d2365b01SPavel Tatashin }
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate #endif /* !_ASM */
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
25857c478bd9Sstevel@tonic-gate }
25867c478bd9Sstevel@tonic-gate #endif
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate #endif	/* _VM_HAT_SFMMU_H */
2589