xref: /onnv-gate/usr/src/uts/sfmmu/vm/hat_sfmmu.h (revision 12908:80a39220b451)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51772Sjl139090  * Common Development and Distribution License (the "License").
61772Sjl139090  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12908SPavel.Tatashin@Sun.COM  * Copyright (c) 1987, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate  * VM - Hardware Address Translation management.
270Sstevel@tonic-gate  *
28567Sdmick  * This file describes the contents of the sun-reference-mmu(sfmmu)-
29567Sdmick  * specific hat data structures and the sfmmu-specific hat procedures.
30567Sdmick  * The machine-independent interface is described in <vm/hat.h>.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #ifndef	_VM_HAT_SFMMU_H
340Sstevel@tonic-gate #define	_VM_HAT_SFMMU_H
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #ifdef	__cplusplus
370Sstevel@tonic-gate extern "C" {
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #ifndef _ASM
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include <sys/types.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #endif /* _ASM */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate #ifdef	_KERNEL
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <sys/pte.h>
490Sstevel@tonic-gate #include <vm/mach_sfmmu.h>
500Sstevel@tonic-gate #include <sys/mmu.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate /*
530Sstevel@tonic-gate  * Don't alter these without considering changes to ism_map_t.
540Sstevel@tonic-gate  */
550Sstevel@tonic-gate #define	DEFAULT_ISM_PAGESIZE		MMU_PAGESIZE4M
566127Ssm142603 #define	DEFAULT_ISM_PAGESZC		TTE4M
570Sstevel@tonic-gate #define	ISM_PG_SIZE(ism_vbshift)	(1 << ism_vbshift)
580Sstevel@tonic-gate #define	ISM_SZ_MASK(ism_vbshift)	(ISM_PG_SIZE(ism_vbshift) - 1)
590Sstevel@tonic-gate #define	ISM_MAP_SLOTS	8	/* Change this carefully. */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #ifndef _ASM
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/t_lock.h>
640Sstevel@tonic-gate #include <vm/hat.h>
650Sstevel@tonic-gate #include <vm/seg.h>
660Sstevel@tonic-gate #include <sys/machparam.h>
670Sstevel@tonic-gate #include <sys/systm.h>
680Sstevel@tonic-gate #include <sys/x_call.h>
690Sstevel@tonic-gate #include <vm/page.h>
700Sstevel@tonic-gate #include <sys/ksynch.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate typedef struct hat sfmmu_t;
734528Spaulsan typedef struct sf_scd sf_scd_t;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * SFMMU attributes for hat_memload/hat_devload
770Sstevel@tonic-gate  */
780Sstevel@tonic-gate #define	SFMMU_UNCACHEPTTE	0x01000000	/* unencache in physical $ */
790Sstevel@tonic-gate #define	SFMMU_UNCACHEVTTE	0x02000000	/* unencache in virtual $ */
800Sstevel@tonic-gate #define	SFMMU_SIDEFFECT		0x04000000	/* set side effect bit */
810Sstevel@tonic-gate #define	SFMMU_LOAD_ALLATTR	(HAT_PROT_MASK | HAT_ORDER_MASK |	\
820Sstevel@tonic-gate 		HAT_ENDIAN_MASK | HAT_NOFAULT | HAT_NOSYNC |		\
830Sstevel@tonic-gate 		SFMMU_UNCACHEPTTE | SFMMU_UNCACHEVTTE | SFMMU_SIDEFFECT)
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * sfmmu flags for hat_memload/hat_devload
880Sstevel@tonic-gate  */
890Sstevel@tonic-gate #define	SFMMU_NO_TSBLOAD	0x08000000	/* do not preload tsb */
900Sstevel@tonic-gate #define	SFMMU_LOAD_ALLFLAG	(HAT_LOAD | HAT_LOAD_LOCK |		\
910Sstevel@tonic-gate 		HAT_LOAD_ADV | HAT_LOAD_CONTIG | HAT_LOAD_NOCONSIST |	\
920Sstevel@tonic-gate 		HAT_LOAD_SHARE | HAT_LOAD_REMAP | SFMMU_NO_TSBLOAD |	\
930Sstevel@tonic-gate 		HAT_RELOAD_SHARE | HAT_NO_KALLOC | HAT_LOAD_TEXT)
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate  * sfmmu internal flag to hat_pageunload that spares locked mappings
970Sstevel@tonic-gate  */
980Sstevel@tonic-gate #define	SFMMU_KERNEL_RELOC	0x8000
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /*
1010Sstevel@tonic-gate  * mode for sfmmu_chgattr
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate #define	SFMMU_SETATTR	0x0
1040Sstevel@tonic-gate #define	SFMMU_CLRATTR	0x1
1050Sstevel@tonic-gate #define	SFMMU_CHGATTR	0x2
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate  * sfmmu specific flags for page_t
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate #define	P_PNC	0x8		/* non-caching is permanent bit */
1110Sstevel@tonic-gate #define	P_TNC	0x10		/* non-caching is temporary bit */
1120Sstevel@tonic-gate #define	P_KPMS	0x20		/* kpm mapped small (vac alias prevention) */
1130Sstevel@tonic-gate #define	P_KPMC	0x40		/* kpm conflict page (vac alias prevention) */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #define	PP_GENERIC_ATTR(pp)	((pp)->p_nrm & (P_MOD | P_REF | P_RO))
1160Sstevel@tonic-gate #define	PP_ISMOD(pp)		((pp)->p_nrm & P_MOD)
1170Sstevel@tonic-gate #define	PP_ISREF(pp)		((pp)->p_nrm & P_REF)
1180Sstevel@tonic-gate #define	PP_ISRO(pp)		((pp)->p_nrm & P_RO)
1190Sstevel@tonic-gate #define	PP_ISNC(pp)		((pp)->p_nrm & (P_PNC|P_TNC))
1200Sstevel@tonic-gate #define	PP_ISPNC(pp)		((pp)->p_nrm & P_PNC)
1212296Sae112802 #ifdef VAC
1220Sstevel@tonic-gate #define	PP_ISTNC(pp)		((pp)->p_nrm & P_TNC)
1232296Sae112802 #endif
1240Sstevel@tonic-gate #define	PP_ISKPMS(pp)		((pp)->p_nrm & P_KPMS)
1250Sstevel@tonic-gate #define	PP_ISKPMC(pp)		((pp)->p_nrm & P_KPMC)
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate #define	PP_SETMOD(pp)		((pp)->p_nrm |= P_MOD)
1280Sstevel@tonic-gate #define	PP_SETREF(pp)		((pp)->p_nrm |= P_REF)
1290Sstevel@tonic-gate #define	PP_SETREFMOD(pp)	((pp)->p_nrm |= (P_REF|P_MOD))
1300Sstevel@tonic-gate #define	PP_SETRO(pp)		((pp)->p_nrm |= P_RO)
1310Sstevel@tonic-gate #define	PP_SETREFRO(pp)		((pp)->p_nrm |= (P_REF|P_RO))
1320Sstevel@tonic-gate #define	PP_SETPNC(pp)		((pp)->p_nrm |= P_PNC)
1332296Sae112802 #ifdef VAC
1340Sstevel@tonic-gate #define	PP_SETTNC(pp)		((pp)->p_nrm |= P_TNC)
1352296Sae112802 #endif
1360Sstevel@tonic-gate #define	PP_SETKPMS(pp)		((pp)->p_nrm |= P_KPMS)
1370Sstevel@tonic-gate #define	PP_SETKPMC(pp)		((pp)->p_nrm |= P_KPMC)
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate #define	PP_CLRMOD(pp)		((pp)->p_nrm &= ~P_MOD)
1400Sstevel@tonic-gate #define	PP_CLRREF(pp)		((pp)->p_nrm &= ~P_REF)
1410Sstevel@tonic-gate #define	PP_CLRREFMOD(pp)	((pp)->p_nrm &= ~(P_REF|P_MOD))
1420Sstevel@tonic-gate #define	PP_CLRRO(pp)		((pp)->p_nrm &= ~P_RO)
1430Sstevel@tonic-gate #define	PP_CLRPNC(pp)		((pp)->p_nrm &= ~P_PNC)
1442296Sae112802 #ifdef VAC
1450Sstevel@tonic-gate #define	PP_CLRTNC(pp)		((pp)->p_nrm &= ~P_TNC)
1462296Sae112802 #endif
1470Sstevel@tonic-gate #define	PP_CLRKPMS(pp)		((pp)->p_nrm &= ~P_KPMS)
1480Sstevel@tonic-gate #define	PP_CLRKPMC(pp)		((pp)->p_nrm &= ~P_KPMC)
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
1510Sstevel@tonic-gate  * All shared memory segments attached with the SHM_SHARE_MMU flag (ISM)
152567Sdmick  * will be constrained to a 4M, 32M or 256M alignment. Also since every newly-
1530Sstevel@tonic-gate  * created ISM segment is created out of a new address space at base va
1540Sstevel@tonic-gate  * of 0 we don't need to store it.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate #define	ISM_ALIGN(shift)	(1 << shift)	/* base va aligned to <n>M  */
1570Sstevel@tonic-gate #define	ISM_ALIGNED(shift, va)	(((uintptr_t)va & (ISM_ALIGN(shift) - 1)) == 0)
1580Sstevel@tonic-gate #define	ISM_SHIFT(shift, x)	((uintptr_t)x >> (shift))
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Pad locks out to cache sub-block boundaries to prevent
1620Sstevel@tonic-gate  * false sharing, so several processes don't contend for
1630Sstevel@tonic-gate  * the same line if they aren't using the same lock.  Since
1640Sstevel@tonic-gate  * this is a typedef we also have a bit of freedom in
1650Sstevel@tonic-gate  * changing lock implementations later if we decide it
1660Sstevel@tonic-gate  * is necessary.
1670Sstevel@tonic-gate  */
1680Sstevel@tonic-gate typedef struct hat_lock {
1690Sstevel@tonic-gate 	kmutex_t hl_mutex;
1700Sstevel@tonic-gate 	uchar_t hl_pad[64 - sizeof (kmutex_t)];
1710Sstevel@tonic-gate } hatlock_t;
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate #define	HATLOCK_MUTEXP(hatlockp)	(&((hatlockp)->hl_mutex))
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate /*
1760Sstevel@tonic-gate  * All segments mapped with ISM are guaranteed to be 4M, 32M or 256M aligned.
1770Sstevel@tonic-gate  * Also size is guaranteed to be in 4M, 32M or 256M chunks.
1780Sstevel@tonic-gate  * ism_seg consists of the following members:
1790Sstevel@tonic-gate  * [XX..22] base address of ism segment. XX is 63 or 31 depending whether
1800Sstevel@tonic-gate  *	caddr_t is 64 bits or 32 bits.
1810Sstevel@tonic-gate  * [21..0] size of segment.
1820Sstevel@tonic-gate  *
1830Sstevel@tonic-gate  * NOTE: Don't alter this structure without changing defines above and
1840Sstevel@tonic-gate  * the tsb_miss and protection handlers.
1850Sstevel@tonic-gate  */
1860Sstevel@tonic-gate typedef struct ism_map {
1870Sstevel@tonic-gate 	uintptr_t	imap_seg;  	/* base va + sz of ISM segment */
1884528Spaulsan 	uchar_t		imap_vb_shift;	/* mmu_pageshift for ism page size */
1894528Spaulsan 	uchar_t		imap_rid;	/* region id for ism */
1900Sstevel@tonic-gate 	ushort_t	imap_hatflags;	/* primary ism page size */
1910Sstevel@tonic-gate 	uint_t		imap_sz_mask;	/* mmu_pagemask for ism page size */
1920Sstevel@tonic-gate 	sfmmu_t		*imap_ismhat; 	/* hat id of dummy ISM as */
1930Sstevel@tonic-gate 	struct ism_ment	*imap_ment;	/* pointer to mapping list entry */
1940Sstevel@tonic-gate } ism_map_t;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #define	ism_start(map)	((caddr_t)((map).imap_seg & \
1970Sstevel@tonic-gate 				~ISM_SZ_MASK((map).imap_vb_shift)))
1980Sstevel@tonic-gate #define	ism_size(map)	((map).imap_seg & ISM_SZ_MASK((map).imap_vb_shift))
1990Sstevel@tonic-gate #define	ism_end(map)	((caddr_t)(ism_start(map) + (ism_size(map) * \
2000Sstevel@tonic-gate 				ISM_PG_SIZE((map).imap_vb_shift))))
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate  * ISM mapping entry. Used to link all hat's sharing a ism_hat.
2030Sstevel@tonic-gate  * Same function as the p_mapping list for a page.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate typedef struct ism_ment {
2060Sstevel@tonic-gate 	sfmmu_t		*iment_hat;	/* back pointer to hat_share() hat */
2070Sstevel@tonic-gate 	caddr_t		iment_base_va;	/* hat's va base for this ism seg */
2080Sstevel@tonic-gate 	struct ism_ment	*iment_next;	/* next ism map entry */
2090Sstevel@tonic-gate 	struct ism_ment	*iment_prev;	/* prev ism map entry */
2100Sstevel@tonic-gate } ism_ment_t;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * ISM segment block. One will be hung off the sfmmu structure if a
2140Sstevel@tonic-gate  * a process uses ISM.  More will be linked using ismblk_next if more
2150Sstevel@tonic-gate  * than ISM_MAP_SLOTS segments are attached to this proc.
2160Sstevel@tonic-gate  *
2170Sstevel@tonic-gate  * All modifications to fields in this structure will be protected
2180Sstevel@tonic-gate  * by the hat mutex.  In order to avoid grabbing this lock in low level
2190Sstevel@tonic-gate  * routines (tsb miss/protection handlers and vatopfn) while not
2200Sstevel@tonic-gate  * introducing any race conditions with hat_unshare, we will set
2210Sstevel@tonic-gate  * CTX_ISM_BUSY bit in the ctx struct. Any mmu traps that occur
2220Sstevel@tonic-gate  * for this ctx while this bit is set will be handled in sfmmu_tsb_excption
2230Sstevel@tonic-gate  * where it will synchronize behind the hat mutex.
2240Sstevel@tonic-gate  */
2250Sstevel@tonic-gate typedef struct ism_blk {
2260Sstevel@tonic-gate 	ism_map_t		iblk_maps[ISM_MAP_SLOTS];
2270Sstevel@tonic-gate 	struct ism_blk		*iblk_next;
2280Sstevel@tonic-gate 	uint64_t		iblk_nextpa;
2290Sstevel@tonic-gate } ism_blk_t;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * TSB access information.  All fields are protected by the process's
2330Sstevel@tonic-gate  * hat lock.
2340Sstevel@tonic-gate  */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate struct tsb_info {
2370Sstevel@tonic-gate 	caddr_t		tsb_va;		/* tsb base virtual address */
2380Sstevel@tonic-gate 	uint64_t	tsb_pa;		/* tsb base physical address */
2390Sstevel@tonic-gate 	struct tsb_info	*tsb_next;	/* next tsb used by this process */
2400Sstevel@tonic-gate 	uint16_t	tsb_szc;	/* tsb size code */
2410Sstevel@tonic-gate 	uint16_t	tsb_flags;	/* flags for this tsb; see below */
2420Sstevel@tonic-gate 	uint_t		tsb_ttesz_mask;	/* page size masks; see below */
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 	tte_t		tsb_tte;	/* tte to lock into DTLB */
2450Sstevel@tonic-gate 	sfmmu_t		*tsb_sfmmu;	/* sfmmu */
2460Sstevel@tonic-gate 	kmem_cache_t	*tsb_cache;	/* cache from which mem allocated */
2470Sstevel@tonic-gate 	vmem_t		*tsb_vmp;	/* vmem arena from which mem alloc'd */
2480Sstevel@tonic-gate };
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  * Values for "tsb_ttesz_mask" bitmask.
2520Sstevel@tonic-gate  */
2530Sstevel@tonic-gate #define	TSB8K	(1 << TTE8K)
2540Sstevel@tonic-gate #define	TSB64K  (1 << TTE64K)
2550Sstevel@tonic-gate #define	TSB512K (1 << TTE512K)
2560Sstevel@tonic-gate #define	TSB4M   (1 << TTE4M)
2570Sstevel@tonic-gate #define	TSB32M  (1 << TTE32M)
2580Sstevel@tonic-gate #define	TSB256M (1 << TTE256M)
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate /*
2610Sstevel@tonic-gate  * Values for "tsb_flags" field.
2620Sstevel@tonic-gate  */
2630Sstevel@tonic-gate #define	TSB_RELOC_FLAG		0x1
2640Sstevel@tonic-gate #define	TSB_FLUSH_NEEDED	0x2
2650Sstevel@tonic-gate #define	TSB_SWAPPED	0x4
2664528Spaulsan #define	TSB_SHAREDCTX		0x8
2674528Spaulsan 
2684528Spaulsan #endif	/* !_ASM */
2694528Spaulsan 
2704528Spaulsan /*
2714528Spaulsan  * Data structures for shared hmeblk support.
2724528Spaulsan  */
2734528Spaulsan 
2744528Spaulsan /*
2754528Spaulsan  * Do not increase the maximum number of ism/hme regions without checking first
2764528Spaulsan  * the impact on ism_map_t, TSB miss area, hblk tag and region id type in
2774528Spaulsan  * sf_region structure.
2784528Spaulsan  * Initially, shared hmes will only be used for the main text segment
2794528Spaulsan  * therefore this value will be set to 64, it will be increased when shared
2804528Spaulsan  * libraries are included.
2814528Spaulsan  */
2824528Spaulsan 
2834528Spaulsan #define	SFMMU_MAX_HME_REGIONS		(64)
2844528Spaulsan #define	SFMMU_HMERGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_HME_REGIONS)
2854528Spaulsan 
2864528Spaulsan #define	SFMMU_PRIVATE	0
2874528Spaulsan #define	SFMMU_SHARED	1
2884528Spaulsan 
2898187SPaul.Sandhu@Sun.COM #define	HMEBLK_ENDPA	1
2908187SPaul.Sandhu@Sun.COM 
2914528Spaulsan #ifndef _ASM
2924528Spaulsan 
2934528Spaulsan #define	SFMMU_MAX_ISM_REGIONS		(64)
2944528Spaulsan #define	SFMMU_ISMRGNMAP_WORDS		BT_BITOUL(SFMMU_MAX_ISM_REGIONS)
2954528Spaulsan 
2964528Spaulsan #define	SFMMU_RGNMAP_WORDS	(SFMMU_HMERGNMAP_WORDS + SFMMU_ISMRGNMAP_WORDS)
2974528Spaulsan 
2984528Spaulsan #define	SFMMU_MAX_REGION_BUCKETS	(128)
2994528Spaulsan #define	SFMMU_MAX_SRD_BUCKETS		(2048)
3004528Spaulsan 
3014528Spaulsan typedef struct sf_hmeregion_map {
3024528Spaulsan 	ulong_t	bitmap[SFMMU_HMERGNMAP_WORDS];
3034528Spaulsan } sf_hmeregion_map_t;
3044528Spaulsan 
3054528Spaulsan typedef struct sf_ismregion_map {
3064528Spaulsan 	ulong_t	bitmap[SFMMU_ISMRGNMAP_WORDS];
3074528Spaulsan } sf_ismregion_map_t;
3084528Spaulsan 
3094528Spaulsan typedef union sf_region_map_u {
3104528Spaulsan 	struct _h_rmap_s {
3114528Spaulsan 		sf_hmeregion_map_t hmeregion_map;
3124528Spaulsan 		sf_ismregion_map_t ismregion_map;
3134528Spaulsan 	} h_rmap_s;
3144528Spaulsan 	ulong_t	bitmap[SFMMU_RGNMAP_WORDS];
3154528Spaulsan } sf_region_map_t;
3164528Spaulsan 
3174528Spaulsan #define	SF_RGNMAP_ZERO(map) {				\
3184528Spaulsan 	int _i;						\
3194528Spaulsan 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {	\
3204528Spaulsan 		(map).bitmap[_i] = 0;			\
3214528Spaulsan 	}						\
3224528Spaulsan }
3234528Spaulsan 
3244528Spaulsan /*
32510271SJason.Beloro@Sun.COM  * Returns 1 if map1 and map2 are equal.
3264528Spaulsan  */
32710271SJason.Beloro@Sun.COM #define	SF_RGNMAP_EQUAL(map1, map2, rval)	{		\
3284528Spaulsan 	int _i;							\
32910271SJason.Beloro@Sun.COM 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
3304528Spaulsan 		if ((map1)->bitmap[_i] != (map2)->bitmap[_i])	\
3314528Spaulsan 			break;					\
3324528Spaulsan 	}							\
33310271SJason.Beloro@Sun.COM 	if (_i < SFMMU_RGNMAP_WORDS)				\
3344528Spaulsan 		rval = 0;					\
3354528Spaulsan 	else							\
3364528Spaulsan 		rval = 1;					\
3374528Spaulsan }
3384528Spaulsan 
3394528Spaulsan #define	SF_RGNMAP_ADD(map, r)		BT_SET((map).bitmap, r)
3404528Spaulsan #define	SF_RGNMAP_DEL(map, r)		BT_CLEAR((map).bitmap, r)
3414528Spaulsan #define	SF_RGNMAP_TEST(map, r)		BT_TEST((map).bitmap, r)
3424528Spaulsan 
3434528Spaulsan /*
3444528Spaulsan  * Tests whether map2 is a subset of map1, returns 1 if
3454528Spaulsan  * this assertion is true.
3464528Spaulsan  */
3474528Spaulsan #define	SF_RGNMAP_IS_SUBSET(map1, map2, rval)	{		\
3484528Spaulsan 	int _i;							\
3494528Spaulsan 	for (_i = 0; _i < SFMMU_RGNMAP_WORDS; _i++) {		\
3504528Spaulsan 		if (((map1)->bitmap[_i]	& (map2)->bitmap[_i])	\
3514528Spaulsan 		    != (map2)->bitmap[_i])  {	 		\
3524528Spaulsan 			break;					\
3534528Spaulsan 		}						\
3544528Spaulsan 	}							\
3554528Spaulsan 	if (_i < SFMMU_RGNMAP_WORDS)		 		\
3564528Spaulsan 		rval = 0;					\
3574528Spaulsan 	else							\
3584528Spaulsan 		rval = 1;					\
3594528Spaulsan }
3604528Spaulsan 
3614528Spaulsan #define	SF_SCD_INCR_REF(scdp) {						\
3624528Spaulsan 	atomic_add_32((volatile uint32_t *)&(scdp)->scd_refcnt, 1);	\
3634528Spaulsan }
3644528Spaulsan 
3654528Spaulsan #define	SF_SCD_DECR_REF(srdp, scdp) {				\
3664528Spaulsan 	sf_region_map_t _scd_rmap = (scdp)->scd_region_map;	\
3674528Spaulsan 	if (!atomic_add_32_nv(					\
3684528Spaulsan 	    (volatile uint32_t *)&(scdp)->scd_refcnt, -1)) {	\
3694528Spaulsan 		sfmmu_destroy_scd((srdp), (scdp), &_scd_rmap);	\
3704528Spaulsan 	}							\
3714528Spaulsan }
3724528Spaulsan 
3734528Spaulsan /*
3744528Spaulsan  * A sfmmup link in the link list of sfmmups that share the same region.
3754528Spaulsan  */
3764528Spaulsan typedef struct sf_rgn_link {
3774528Spaulsan 	sfmmu_t	*next;
3784528Spaulsan 	sfmmu_t *prev;
3794528Spaulsan } sf_rgn_link_t;
3804528Spaulsan 
3814528Spaulsan /*
3824528Spaulsan  * rgn_flags values.
3834528Spaulsan  */
3844528Spaulsan #define	SFMMU_REGION_HME	0x1
3854528Spaulsan #define	SFMMU_REGION_ISM	0x2
3864528Spaulsan #define	SFMMU_REGION_FREE	0x8
3874528Spaulsan 
3884528Spaulsan #define	SFMMU_REGION_TYPE_MASK	(0x3)
3894528Spaulsan 
3904528Spaulsan /*
3914528Spaulsan  * sf_region defines a text or (D)ISM segment which map
3924528Spaulsan  * the same underlying physical object.
3934528Spaulsan  */
3944528Spaulsan typedef struct sf_region {
3954528Spaulsan 	caddr_t			rgn_saddr;   /* base addr of attached seg */
3964528Spaulsan 	size_t			rgn_size;    /* size of attached seg */
3974528Spaulsan 	void			*rgn_obj;    /* the underlying object id */
3984528Spaulsan 	u_offset_t		rgn_objoff;  /* offset in the object mapped */
3994528Spaulsan 	uchar_t			rgn_perm;    /* PROT_READ/WRITE/EXEC */
4004528Spaulsan 	uchar_t			rgn_pgszc;   /* page size of the region */
4014528Spaulsan 	uchar_t			rgn_flags;   /* region type, free flag */
4024528Spaulsan 	uchar_t			rgn_id;
4034528Spaulsan 	int			rgn_refcnt;  /* # of hats sharing the region */
4044528Spaulsan 	/* callback function for hat_unload_callback */
4054528Spaulsan 	hat_rgn_cb_func_t	rgn_cb_function;
4064528Spaulsan 	struct sf_region	*rgn_hash;   /* hash chain linking the rgns */
4074528Spaulsan 	kmutex_t		rgn_mutex;   /* protect region sfmmu list */
4084528Spaulsan 	/* A link list of processes attached to this region */
4094528Spaulsan 	sfmmu_t			*rgn_sfmmu_head;
4104528Spaulsan 	ulong_t			rgn_ttecnt[MMU_PAGE_SIZES];
4114528Spaulsan 	uint16_t		rgn_hmeflags; /* rgn tte size flags */
4124528Spaulsan } sf_region_t;
4134528Spaulsan 
4144528Spaulsan #define	rgn_next	rgn_hash
4154528Spaulsan 
4164528Spaulsan /* srd */
4174528Spaulsan typedef struct sf_shared_region_domain {
4184528Spaulsan 	vnode_t			*srd_evp;	/* executable vnode */
4194528Spaulsan 	/* hme region table */
4204528Spaulsan 	sf_region_t		*srd_hmergnp[SFMMU_MAX_HME_REGIONS];
4214528Spaulsan 	/* ism region table */
4224528Spaulsan 	sf_region_t		*srd_ismrgnp[SFMMU_MAX_ISM_REGIONS];
4234528Spaulsan 	/* hash chain linking srds */
4244528Spaulsan 	struct sf_shared_region_domain *srd_hash;
4254528Spaulsan 	/* pointer to the next free hme region */
4264528Spaulsan 	sf_region_t		*srd_hmergnfree;
4274528Spaulsan 	/* pointer to the next free ism region */
4284528Spaulsan 	sf_region_t		*srd_ismrgnfree;
4295075Spaulsan 	/* id of next ism region created */
4304528Spaulsan 	uint16_t		srd_next_ismrid;
4315075Spaulsan 	/* id of next hme region created */
4324528Spaulsan 	uint16_t		srd_next_hmerid;
4334528Spaulsan 	uint16_t		srd_ismbusyrgns; /* # of ism rgns in use */
4344528Spaulsan 	uint16_t		srd_hmebusyrgns; /* # of hme rgns in use */
4354528Spaulsan 	int			srd_refcnt;	 /* # of procs in the srd */
4364528Spaulsan 	kmutex_t		srd_mutex;	 /* sync add/remove rgns */
4374528Spaulsan 	kmutex_t		srd_scd_mutex;
4384528Spaulsan 	sf_scd_t		*srd_scdp;	 /* list of scds in srd */
4394528Spaulsan 	/* hash of regions associated with the same executable */
4404528Spaulsan 	sf_region_t		*srd_rgnhash[SFMMU_MAX_REGION_BUCKETS];
4414528Spaulsan } sf_srd_t;
4424528Spaulsan 
4434528Spaulsan typedef struct sf_srd_bucket {
4444528Spaulsan 	kmutex_t	srdb_lock;
4454528Spaulsan 	sf_srd_t	*srdb_srdp;
4464528Spaulsan } sf_srd_bucket_t;
4474528Spaulsan 
4484528Spaulsan /*
4494528Spaulsan  * The value of SFMMU_L1_HMERLINKS and SFMMU_L2_HMERLINKS will be increased
4504528Spaulsan  * to 16 when the use of shared hmes for shared libraries is enabled.
4514528Spaulsan  */
4524528Spaulsan 
4534528Spaulsan #define	SFMMU_L1_HMERLINKS		(8)
4544528Spaulsan #define	SFMMU_L2_HMERLINKS		(8)
4554528Spaulsan #define	SFMMU_L1_HMERLINKS_SHIFT	(3)
4564528Spaulsan #define	SFMMU_L1_HMERLINKS_MASK		(SFMMU_L1_HMERLINKS - 1)
4574528Spaulsan #define	SFMMU_L2_HMERLINKS_MASK		(SFMMU_L2_HMERLINKS - 1)
4584528Spaulsan #define	SFMMU_L1_HMERLINKS_SIZE		\
4594528Spaulsan 	(SFMMU_L1_HMERLINKS * sizeof (sf_rgn_link_t *))
4604528Spaulsan #define	SFMMU_L2_HMERLINKS_SIZE		\
4614528Spaulsan 	(SFMMU_L2_HMERLINKS * sizeof (sf_rgn_link_t))
4624528Spaulsan 
4634528Spaulsan #if (SFMMU_L1_HMERLINKS * SFMMU_L2_HMERLINKS < SFMMU_MAX_HME_REGIONS)
4644528Spaulsan #error Not Enough HMERLINKS
4654528Spaulsan #endif
4664528Spaulsan 
4674528Spaulsan /*
4684528Spaulsan  * This macro grabs hat lock and allocates level 2 hat chain
4694528Spaulsan  * associated with a shme rgn. In the majority of cases, the macro
4704528Spaulsan  * is called with alloc = 0, and lock = 0.
4715075Spaulsan  * A pointer to the level 2 sf_rgn_link_t structure is returned in the lnkp
4725075Spaulsan  * parameter.
4734528Spaulsan  */
4744528Spaulsan #define	SFMMU_HMERID2RLINKP(sfmmup, rid, lnkp, alloc, lock)		\
4754528Spaulsan {									\
4764528Spaulsan 	int _l1ix = ((rid) >> SFMMU_L1_HMERLINKS_SHIFT) &		\
4774528Spaulsan 	    SFMMU_L1_HMERLINKS_MASK;					\
4784528Spaulsan 	int _l2ix = ((rid) & SFMMU_L2_HMERLINKS_MASK);			\
4794528Spaulsan 	hatlock_t *_hatlockp;						\
4804528Spaulsan 	lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];			\
4814528Spaulsan 	if (lnkp != NULL) {						\
4824528Spaulsan 		lnkp = &lnkp[_l2ix];					\
4834528Spaulsan 	} else if (alloc && lock) {					\
4844528Spaulsan 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
4854528Spaulsan 		_hatlockp = sfmmu_hat_enter(sfmmup);			\
4864528Spaulsan 		if ((sfmmup)->sfmmu_hmeregion_links[_l1ix] != NULL) {	\
4874528Spaulsan 			sfmmu_hat_exit(_hatlockp);			\
4884528Spaulsan 			kmem_free(lnkp, SFMMU_L2_HMERLINKS_SIZE);	\
4894528Spaulsan 			lnkp = (sfmmup)->sfmmu_hmeregion_links[_l1ix];	\
4904528Spaulsan 			ASSERT(lnkp != NULL);				\
4914528Spaulsan 		} else {						\
4924528Spaulsan 			(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;	\
4934528Spaulsan 			sfmmu_hat_exit(_hatlockp);			\
4944528Spaulsan 		}							\
4954528Spaulsan 		lnkp = &lnkp[_l2ix];					\
4964528Spaulsan 	} else if (alloc) {						\
4974528Spaulsan 		lnkp = kmem_zalloc(SFMMU_L2_HMERLINKS_SIZE, KM_SLEEP);	\
4984528Spaulsan 		ASSERT((sfmmup)->sfmmu_hmeregion_links[_l1ix] == NULL);	\
4994528Spaulsan 		(sfmmup)->sfmmu_hmeregion_links[_l1ix] = lnkp;		\
5004528Spaulsan 		lnkp = &lnkp[_l2ix];					\
5014528Spaulsan 	}								\
5024528Spaulsan }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5058187SPaul.Sandhu@Sun.COM  *  Per cpu pending freelist of hmeblks.
5068187SPaul.Sandhu@Sun.COM  */
5078187SPaul.Sandhu@Sun.COM typedef struct cpu_hme_pend {
5088187SPaul.Sandhu@Sun.COM 	struct   hme_blk *chp_listp;
5098187SPaul.Sandhu@Sun.COM 	kmutex_t chp_mutex;
5108187SPaul.Sandhu@Sun.COM 	time_t	 chp_timestamp;
5118187SPaul.Sandhu@Sun.COM 	uint_t   chp_count;
5128187SPaul.Sandhu@Sun.COM 	uint8_t	 chp_pad[36];		/* pad to 64 bytes */
5138187SPaul.Sandhu@Sun.COM } cpu_hme_pend_t;
5148187SPaul.Sandhu@Sun.COM 
5158187SPaul.Sandhu@Sun.COM /*
5168187SPaul.Sandhu@Sun.COM  * The default value of the threshold for the per cpu pending queues of hmeblks.
5178187SPaul.Sandhu@Sun.COM  * The queues are flushed if either the number of hmeblks on the queue is above
5188187SPaul.Sandhu@Sun.COM  * the threshold, or one second has elapsed since the last flush.
5198187SPaul.Sandhu@Sun.COM  */
5208187SPaul.Sandhu@Sun.COM #define	CPU_HME_PEND_THRESH 1000
5218187SPaul.Sandhu@Sun.COM 
5228187SPaul.Sandhu@Sun.COM /*
5232241Shuah  * Per-MMU context domain kstats.
5242241Shuah  *
5252241Shuah  * TSB Miss Exceptions
5262241Shuah  *	Number of times a TSB miss exception is handled in an MMU. See
5272241Shuah  *	sfmmu_tsbmiss_exception() for more details.
5282241Shuah  * TSB Raise Exception
5292241Shuah  *	Number of times the CPUs within an MMU are cross-called
5302241Shuah  *	to invalidate either a specific process context (when the process
5312241Shuah  *	switches MMU contexts) or the context of any process that is
5322241Shuah  *	running on those CPUs (as part of the MMU context wrap-around).
5332241Shuah  * Wrap Around
5342241Shuah  *	The number of times a wrap-around of MMU context happens.
5352241Shuah  */
5362241Shuah typedef enum mmu_ctx_stat_types {
5372241Shuah 	MMU_CTX_TSB_EXCEPTIONS,		/* TSB miss exceptions handled */
5382241Shuah 	MMU_CTX_TSB_RAISE_EXCEPTION,	/* ctx invalidation cross calls */
5392241Shuah 	MMU_CTX_WRAP_AROUND,		/* wraparounds */
5402241Shuah 	MMU_CTX_NUM_STATS
5412241Shuah } mmu_ctx_stat_t;
5422241Shuah 
5432241Shuah /*
5442241Shuah  * Per-MMU context domain structure. This is instantiated the first time a CPU
5452241Shuah  * belonging to the MMU context domain is configured into the system, at boot
5462241Shuah  * time or at DR time.
5472241Shuah  *
5482241Shuah  * mmu_gnum
5492241Shuah  *	The current generation number for the context IDs on this MMU context
5502241Shuah  *	domain. It is protected by mmu_lock.
5512241Shuah  * mmu_cnum
5522241Shuah  *	The current cnum to be allocated on this MMU context domain. It
5532241Shuah  *	is protected via CAS.
5542241Shuah  * mmu_nctxs
5552241Shuah  *	The max number of context IDs supported on every CPU in this
55611713SPavel.Tatashin@Sun.COM  *	MMU context domain. This is needed here in case the system supports
55711713SPavel.Tatashin@Sun.COM  *      mixed type of processors/MMUs. It also helps to make ctx switch code
55811713SPavel.Tatashin@Sun.COM  *      access fewer cache lines i.e. no need to retrieve it from some global
55911713SPavel.Tatashin@Sun.COM  *      nctxs.
5602241Shuah  * mmu_lock
5612241Shuah  *	The mutex spin lock used to serialize context ID wrap around
5622241Shuah  * mmu_idx
5632241Shuah  *	The index for this MMU context domain structure in the global array
5642241Shuah  *	mmu_ctxdoms.
5652241Shuah  * mmu_ncpus
5662241Shuah  *	The actual number of CPUs that have been configured in this
5672241Shuah  *	MMU context domain. This also acts as a reference count for the
5682241Shuah  *	structure. When the last CPU in an MMU context domain is unconfigured,
5692241Shuah  *	the structure is freed. It is protected by mmu_lock.
5702241Shuah  * mmu_cpuset
5712241Shuah  *	The CPU set of configured CPUs for this MMU context domain. Used
5722241Shuah  *	to cross-call all the CPUs in the MMU context domain to invalidate
5732241Shuah  *	context IDs during a wraparound operation. It is protected by mmu_lock.
5742241Shuah  */
5752241Shuah 
5762241Shuah typedef struct mmu_ctx {
5772241Shuah 	uint64_t	mmu_gnum;
5782241Shuah 	uint_t		mmu_cnum;
5792241Shuah 	uint_t		mmu_nctxs;
5802241Shuah 	kmutex_t	mmu_lock;
5812241Shuah 	uint_t		mmu_idx;
5822241Shuah 	uint_t		mmu_ncpus;
5832241Shuah 	cpuset_t	mmu_cpuset;
5842241Shuah 	kstat_t		*mmu_kstat;
5852241Shuah 	kstat_named_t	mmu_kstat_data[MMU_CTX_NUM_STATS];
5862241Shuah } mmu_ctx_t;
5872241Shuah 
5882241Shuah #define	mmu_tsb_exceptions	\
5892241Shuah 		mmu_kstat_data[MMU_CTX_TSB_EXCEPTIONS].value.ui64
5902241Shuah #define	mmu_tsb_raise_exception	\
5912241Shuah 		mmu_kstat_data[MMU_CTX_TSB_RAISE_EXCEPTION].value.ui64
5922241Shuah #define	mmu_wrap_around		\
5932241Shuah 		mmu_kstat_data[MMU_CTX_WRAP_AROUND].value.ui64
5942241Shuah 
5952241Shuah extern uint_t		max_mmu_ctxdoms;
5962241Shuah extern mmu_ctx_t	**mmu_ctxs_tbl;
5972241Shuah 
5982241Shuah extern void	sfmmu_cpu_init(cpu_t *);
5992241Shuah extern void	sfmmu_cpu_cleanup(cpu_t *);
6002241Shuah 
60111713SPavel.Tatashin@Sun.COM extern uint_t	sfmmu_ctxdom_nctxs(int);
60211713SPavel.Tatashin@Sun.COM 
60311713SPavel.Tatashin@Sun.COM #ifdef sun4v
60411713SPavel.Tatashin@Sun.COM extern void	sfmmu_ctxdoms_remove(void);
60511713SPavel.Tatashin@Sun.COM extern void	sfmmu_ctxdoms_lock(void);
60611713SPavel.Tatashin@Sun.COM extern void	sfmmu_ctxdoms_unlock(void);
60711713SPavel.Tatashin@Sun.COM extern void	sfmmu_ctxdoms_update(void);
60811713SPavel.Tatashin@Sun.COM #endif
60911713SPavel.Tatashin@Sun.COM 
6102241Shuah /*
6112241Shuah  * The following structure is used to get MMU context domain information for
6122241Shuah  * a CPU from the platform.
6132241Shuah  *
6142241Shuah  * mmu_idx
6152241Shuah  *	The MMU context domain index within the global array mmu_ctxs
6162241Shuah  * mmu_nctxs
6172241Shuah  *	The number of context IDs supported in the MMU context domain
6182241Shuah  */
6192241Shuah typedef struct mmu_ctx_info {
6202241Shuah 	uint_t		mmu_idx;
6212241Shuah 	uint_t		mmu_nctxs;
6222241Shuah } mmu_ctx_info_t;
6232241Shuah 
6242241Shuah #pragma weak plat_cpuid_to_mmu_ctx_info
6252241Shuah 
6262241Shuah extern void	plat_cpuid_to_mmu_ctx_info(processorid_t, mmu_ctx_info_t *);
6272241Shuah 
6282241Shuah /*
6292241Shuah  * Each address space has an array of sfmmu_ctx_t structures, one structure
6302241Shuah  * per MMU context domain.
6312241Shuah  *
6322241Shuah  * cnum
6332241Shuah  *	The context ID allocated for an address space on an MMU context domain
6342241Shuah  * gnum
6352241Shuah  *	The generation number for the context ID in the MMU context domain.
6362241Shuah  *
6372241Shuah  * This structure needs to be a power-of-two in size.
6382241Shuah  */
6392241Shuah typedef struct sfmmu_ctx {
6402241Shuah 	uint64_t	gnum:48;
6412241Shuah 	uint64_t	cnum:16;
6422241Shuah } sfmmu_ctx_t;
6432241Shuah 
64410271SJason.Beloro@Sun.COM 
6452241Shuah /*
6460Sstevel@tonic-gate  * The platform dependent hat structure.
6470Sstevel@tonic-gate  * tte counts should be protected by cas.
6480Sstevel@tonic-gate  * cpuset is protected by cas.
6490Sstevel@tonic-gate  *
6505075Spaulsan  * ttecnt accounting for mappings which do not use shared hme is carried out
6515075Spaulsan  * during pagefault handling. In the shared hme case, only the first process
6525075Spaulsan  * to access a mapping generates a pagefault, subsequent processes simply
6535075Spaulsan  * find the shared hme entry during trap handling and therefore there is no
6545075Spaulsan  * corresponding event to initiate ttecnt accounting. Currently, as shared
6555075Spaulsan  * hmes are only used for text segments, when joining a region we assume the
6565075Spaulsan  * worst case and add the the number of ttes required to map the entire region
6575075Spaulsan  * to the ttecnt corresponding to the region pagesize. However, if the region
6585075Spaulsan  * has a 4M pagesize, and memory is low, the allocation of 4M pages may fail
6595075Spaulsan  * then 8K pages will be allocated instead and the first TSB which stores 8K
6605075Spaulsan  * mappings will potentially be undersized. To compensate for the potential
6615075Spaulsan  * underaccounting in this case we always add 1/4 of the region size to the 8K
6625075Spaulsan  * ttecnt.
6635075Spaulsan  *
6640Sstevel@tonic-gate  * Note that sfmmu_xhat_provider MUST be the first element.
6650Sstevel@tonic-gate  */
6665075Spaulsan 
6670Sstevel@tonic-gate struct hat {
6680Sstevel@tonic-gate 	void		*sfmmu_xhat_provider;	/* NULL for CPU hat */
6690Sstevel@tonic-gate 	cpuset_t	sfmmu_cpusran;	/* cpu bit mask for efficient xcalls */
6700Sstevel@tonic-gate 	struct	as	*sfmmu_as;	/* as this hat provides mapping for */
6714528Spaulsan 	/* per pgsz private ttecnt + shme rgns ttecnt for rgns not in SCD */
6724528Spaulsan 	ulong_t		sfmmu_ttecnt[MMU_PAGE_SIZES];
6734528Spaulsan 	/* shme rgns ttecnt for rgns in SCD */
6744528Spaulsan 	ulong_t		sfmmu_scdrttecnt[MMU_PAGE_SIZES];
6754528Spaulsan 	/* est. ism ttes that are NOT in a SCD */
6764528Spaulsan 	ulong_t		sfmmu_ismttecnt[MMU_PAGE_SIZES];
6774528Spaulsan 	/* ttecnt for isms that are in a SCD */
6784528Spaulsan 	ulong_t		sfmmu_scdismttecnt[MMU_PAGE_SIZES];
6794528Spaulsan 	/* inflate tsb0 to allow for large page alloc failure in region */
6804528Spaulsan 	ulong_t		sfmmu_tsb0_4minflcnt;
6810Sstevel@tonic-gate 	union _h_un {
6820Sstevel@tonic-gate 		ism_blk_t	*sfmmu_iblkp;  /* maps to ismhat(s) */
6830Sstevel@tonic-gate 		ism_ment_t	*sfmmu_imentp; /* ism hat's mapping list */
6840Sstevel@tonic-gate 	} h_un;
6850Sstevel@tonic-gate 	uint_t		sfmmu_free:1;	/* hat to be freed - set on as_free */
6860Sstevel@tonic-gate 	uint_t		sfmmu_ismhat:1;	/* hat is dummy ism hatid */
6874528Spaulsan 	uint_t		sfmmu_scdhat:1;	/* hat is dummy scd hatid */
6880Sstevel@tonic-gate 	uchar_t		sfmmu_rmstat;	/* refmod stats refcnt */
6892961Sdp78419 	ushort_t	sfmmu_clrstart;	/* start color bin for page coloring */
6900Sstevel@tonic-gate 	ushort_t	sfmmu_clrbin;	/* per as phys page coloring bin */
6910Sstevel@tonic-gate 	ushort_t	sfmmu_flags;	/* flags */
6924528Spaulsan 	uchar_t		sfmmu_tteflags;	/* pgsz flags */
6934528Spaulsan 	uchar_t		sfmmu_rtteflags; /* pgsz flags for SRD hmes */
6940Sstevel@tonic-gate 	struct tsb_info	*sfmmu_tsb;	/* list of per as tsbs */
6950Sstevel@tonic-gate 	uint64_t	sfmmu_ismblkpa; /* pa of sfmmu_iblkp, or -1 */
6962241Shuah 	lock_t		sfmmu_ctx_lock;	/* sync ctx alloc and invalidation */
6970Sstevel@tonic-gate 	kcondvar_t	sfmmu_tsb_cv;	/* signals TSB swapin or relocation */
6980Sstevel@tonic-gate 	uchar_t		sfmmu_cext;	/* context page size encoding */
6990Sstevel@tonic-gate 	uint8_t		sfmmu_pgsz[MMU_PAGE_SIZES];  /* ranking for MMU */
7004528Spaulsan 	sf_srd_t	*sfmmu_srdp;
7014528Spaulsan 	sf_scd_t	*sfmmu_scdp;	/* scd this address space belongs to */
7024528Spaulsan 	sf_region_map_t	sfmmu_region_map;
7034528Spaulsan 	sf_rgn_link_t	*sfmmu_hmeregion_links[SFMMU_L1_HMERLINKS];
7044528Spaulsan 	sf_rgn_link_t	sfmmu_scd_link;	/* link to scd or pending queue */
7050Sstevel@tonic-gate #ifdef sun4v
7060Sstevel@tonic-gate 	struct hv_tsb_block sfmmu_hvblock;
7070Sstevel@tonic-gate #endif
7082241Shuah 	/*
7092241Shuah 	 * sfmmu_ctxs is a variable length array of max_mmu_ctxdoms # of
7102241Shuah 	 * elements. max_mmu_ctxdoms is determined at run-time.
7112241Shuah 	 * sfmmu_ctxs[1] is just the fist element of an array, it always
7122241Shuah 	 * has to be the last field to ensure that the memory allocated
7132241Shuah 	 * for sfmmu_ctxs is consecutive with the memory of the rest of
7142241Shuah 	 * the hat data structure.
7152241Shuah 	 */
7162241Shuah 	sfmmu_ctx_t	sfmmu_ctxs[1];
7172241Shuah 
7180Sstevel@tonic-gate };
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate #define	sfmmu_iblk	h_un.sfmmu_iblkp
7210Sstevel@tonic-gate #define	sfmmu_iment	h_un.sfmmu_imentp
7220Sstevel@tonic-gate 
7234528Spaulsan #define	sfmmu_hmeregion_map	sfmmu_region_map.h_rmap_s.hmeregion_map
7244528Spaulsan #define	sfmmu_ismregion_map	sfmmu_region_map.h_rmap_s.ismregion_map
7254528Spaulsan 
7264528Spaulsan #define	SF_RGNMAP_ISNULL(sfmmup)	\
7274528Spaulsan 	(sfrgnmap_isnull(&(sfmmup)->sfmmu_region_map))
7284528Spaulsan #define	SF_HMERGNMAP_ISNULL(sfmmup)	\
7294528Spaulsan 	(sfhmergnmap_isnull(&(sfmmup)->sfmmu_hmeregion_map))
7304528Spaulsan 
7314528Spaulsan struct sf_scd {
7324528Spaulsan 	sfmmu_t		*scd_sfmmup;	/* shared context hat */
7334528Spaulsan 	/* per pgsz ttecnt for shme rgns in SCD */
7344528Spaulsan 	ulong_t		scd_rttecnt[MMU_PAGE_SIZES];
7354528Spaulsan 	uint_t		scd_refcnt;	/* address spaces attached to scd */
7364528Spaulsan 	sf_region_map_t scd_region_map; /* bit mask of attached segments */
7374528Spaulsan 	sf_scd_t	*scd_next;	/* link pointers for srd_scd list */
7384528Spaulsan 	sf_scd_t	*scd_prev;
7394528Spaulsan 	sfmmu_t 	*scd_sf_list;	/* list of doubly linked hat structs */
7404528Spaulsan 	kmutex_t 	scd_mutex;
7414528Spaulsan 	/*
7424528Spaulsan 	 * Link used to add an scd to the sfmmu_iment list.
7434528Spaulsan 	 */
7444528Spaulsan 	ism_ment_t	scd_ism_links[SFMMU_MAX_ISM_REGIONS];
7454528Spaulsan };
7464528Spaulsan 
7474528Spaulsan #define	scd_hmeregion_map	scd_region_map.h_rmap_s.hmeregion_map
7484528Spaulsan #define	scd_ismregion_map	scd_region_map.h_rmap_s.ismregion_map
7494528Spaulsan 
7504528Spaulsan extern int disable_shctx;
7514528Spaulsan extern int shctx_on;
7524528Spaulsan 
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate  * bit mask for managing vac conflicts on large pages.
7550Sstevel@tonic-gate  * bit 1 is for uncache flag.
7560Sstevel@tonic-gate  * bits 2 through min(num of cache colors + 1,31) are
7570Sstevel@tonic-gate  * for cache colors that have already been flushed.
7580Sstevel@tonic-gate  */
7592296Sae112802 #ifdef VAC
7600Sstevel@tonic-gate #define	CACHE_NUM_COLOR		(shm_alignment >> MMU_PAGESHIFT)
7612296Sae112802 #else
7622296Sae112802 #define	CACHE_NUM_COLOR		1
7632296Sae112802 #endif
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate #define	CACHE_VCOLOR_MASK(vcolor)	(2 << (vcolor & (CACHE_NUM_COLOR - 1)))
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate #define	CacheColor_IsFlushed(flag, vcolor) \
7680Sstevel@tonic-gate 					((flag) & CACHE_VCOLOR_MASK(vcolor))
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate #define	CacheColor_SetFlushed(flag, vcolor) \
7710Sstevel@tonic-gate 					((flag) |= CACHE_VCOLOR_MASK(vcolor))
7720Sstevel@tonic-gate /*
7730Sstevel@tonic-gate  * Flags passed to sfmmu_page_cache to flush page from vac or not.
7740Sstevel@tonic-gate  */
7750Sstevel@tonic-gate #define	CACHE_FLUSH	0
7760Sstevel@tonic-gate #define	CACHE_NO_FLUSH	1
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate /*
7790Sstevel@tonic-gate  * Flags passed to sfmmu_tlbcache_demap
7800Sstevel@tonic-gate  */
7810Sstevel@tonic-gate #define	FLUSH_NECESSARY_CPUS	0
7820Sstevel@tonic-gate #define	FLUSH_ALL_CPUS		1
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate #ifdef	DEBUG
7850Sstevel@tonic-gate /*
7860Sstevel@tonic-gate  * For debugging purpose only. Maybe removed later.
7870Sstevel@tonic-gate  */
7880Sstevel@tonic-gate struct ctx_trace {
7890Sstevel@tonic-gate 	sfmmu_t		*sc_sfmmu_stolen;
7900Sstevel@tonic-gate 	sfmmu_t		*sc_sfmmu_stealing;
7910Sstevel@tonic-gate 	clock_t		sc_time;
7920Sstevel@tonic-gate 	ushort_t	sc_type;
7930Sstevel@tonic-gate 	ushort_t	sc_cnum;
7940Sstevel@tonic-gate };
7950Sstevel@tonic-gate #define	CTX_TRC_STEAL	0x1
7960Sstevel@tonic-gate #define	CTX_TRC_FREE	0x0
7970Sstevel@tonic-gate #define	TRSIZE	0x400
7980Sstevel@tonic-gate #define	NEXT_CTXTR(ptr)	(((ptr) >= ctx_trace_last) ? \
7990Sstevel@tonic-gate 		ctx_trace_first : ((ptr) + 1))
8000Sstevel@tonic-gate #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type) \
8010Sstevel@tonic-gate 	mutex_enter(mutex);						\
8020Sstevel@tonic-gate 	(ptr)->sc_sfmmu_stolen = (stolen_sfmmu);			\
8030Sstevel@tonic-gate 	(ptr)->sc_sfmmu_stealing = (stealing_sfmmu);			\
8040Sstevel@tonic-gate 	(ptr)->sc_cnum = (cnum);					\
8050Sstevel@tonic-gate 	(ptr)->sc_type = (type);					\
80611066Srafael.vanoni@sun.com 	(ptr)->sc_time = ddi_get_lbolt();				\
8070Sstevel@tonic-gate 	(ptr) = NEXT_CTXTR(ptr);					\
8080Sstevel@tonic-gate 	num_ctx_stolen += (type);					\
8090Sstevel@tonic-gate 	mutex_exit(mutex);
8100Sstevel@tonic-gate #else
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate #define	TRACE_CTXS(mutex, ptr, cnum, stolen_sfmmu, stealing_sfmmu, type)
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate #endif	/* DEBUG */
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate #endif	/* !_ASM */
8170Sstevel@tonic-gate 
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate  * Macros for sfmmup->sfmmu_flags access.  The macros that change the flags
8200Sstevel@tonic-gate  * ASSERT() that we're holding the HAT lock before changing the flags;
8210Sstevel@tonic-gate  * however callers that read the flags may do so without acquiring the lock
8220Sstevel@tonic-gate  * in a fast path, and then recheck the flag after acquiring the lock in
8230Sstevel@tonic-gate  * a slow path.
8240Sstevel@tonic-gate  */
8250Sstevel@tonic-gate #define	SFMMU_FLAGS_ISSET(sfmmup, flags) \
8260Sstevel@tonic-gate 	(((sfmmup)->sfmmu_flags & (flags)) == (flags))
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate #define	SFMMU_FLAGS_CLEAR(sfmmup, flags) \
8290Sstevel@tonic-gate 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
8300Sstevel@tonic-gate 	(sfmmup)->sfmmu_flags &= ~(flags))
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate #define	SFMMU_FLAGS_SET(sfmmup, flags) \
8330Sstevel@tonic-gate 	(ASSERT(sfmmu_hat_lock_held((sfmmup))), \
8340Sstevel@tonic-gate 	(sfmmup)->sfmmu_flags |= (flags))
8350Sstevel@tonic-gate 
8364528Spaulsan #define	SFMMU_TTEFLAGS_ISSET(sfmmup, flags) \
8374528Spaulsan 	((((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) & (flags)) == \
8384528Spaulsan 	    (flags))
8394528Spaulsan 
8404528Spaulsan 
8410Sstevel@tonic-gate /*
8424528Spaulsan  * sfmmu tte HAT flags, must fit in 8 bits
8430Sstevel@tonic-gate  */
8444528Spaulsan #define	HAT_CHKCTX1_FLAG 0x1
8454528Spaulsan #define	HAT_64K_FLAG	(0x1 << TTE64K)
8464528Spaulsan #define	HAT_512K_FLAG	(0x1 << TTE512K)
8474528Spaulsan #define	HAT_4M_FLAG	(0x1 << TTE4M)
8484528Spaulsan #define	HAT_32M_FLAG	(0x1 << TTE32M)
8494528Spaulsan #define	HAT_256M_FLAG	(0x1 << TTE256M)
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate /*
8524528Spaulsan  * sfmmu HAT flags, 16 bits at the moment.
8530Sstevel@tonic-gate  */
8544528Spaulsan #define	HAT_4MTEXT_FLAG		0x01
8554528Spaulsan #define	HAT_32M_ISM		0x02
8564528Spaulsan #define	HAT_256M_ISM		0x04
8574528Spaulsan #define	HAT_SWAPPED		0x08 /* swapped out */
8584528Spaulsan #define	HAT_SWAPIN		0x10 /* swapping in */
8594528Spaulsan #define	HAT_BUSY		0x20 /* replacing TSB(s) */
8604528Spaulsan #define	HAT_ISMBUSY		0x40 /* adding/removing/traversing ISM maps */
8610Sstevel@tonic-gate 
8624528Spaulsan #define	HAT_CTX1_FLAG   	0x100 /* ISM imap hatflag for ctx1 */
8634528Spaulsan #define	HAT_JOIN_SCD		0x200 /* region is joining scd */
8644528Spaulsan #define	HAT_ALLCTX_INVALID	0x400 /* all per-MMU ctxs are invalidated */
8650Sstevel@tonic-gate 
8664528Spaulsan #define	SFMMU_LGPGS_INUSE(sfmmup)					\
8674528Spaulsan 	(((sfmmup)->sfmmu_tteflags | (sfmmup)->sfmmu_rtteflags) ||	\
8684528Spaulsan 	    ((sfmmup)->sfmmu_iblk != NULL))
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * Starting with context 0, the first NUM_LOCKED_CTXS contexts
8720Sstevel@tonic-gate  * are locked so that sfmmu_getctx can't steal any of these
8730Sstevel@tonic-gate  * contexts.  At the time this software was being developed, the
8740Sstevel@tonic-gate  * only context that needs to be locked is context 0 (the kernel
8750Sstevel@tonic-gate  * context), and context 1 (reserved for stolen context). So this constant
8760Sstevel@tonic-gate  * was originally defined to be 2.
8773687Sjb145095  *
8783687Sjb145095  * For sun4v only, USER_CONTEXT_TYPE represents any user context.  Many
8793687Sjb145095  * routines only care whether the context is kernel, invalid or user.
8800Sstevel@tonic-gate  */
8813687Sjb145095 
8820Sstevel@tonic-gate #define	NUM_LOCKED_CTXS 2
8830Sstevel@tonic-gate #define	INVALID_CONTEXT	1
8840Sstevel@tonic-gate 
8853687Sjb145095 #ifdef sun4v
8863687Sjb145095 #define	USER_CONTEXT_TYPE	NUM_LOCKED_CTXS
8873687Sjb145095 #endif
8886127Ssm142603 #if defined(sun4v) || defined(UTSB_PHYS)
8896127Ssm142603 /*
8906127Ssm142603  * Get the location in the 4MB base TSB of the tsbe for this fault.
8916127Ssm142603  * Assumes that the second TSB only contains 4M mappings.
8926127Ssm142603  *
8936127Ssm142603  * In:
8946127Ssm142603  *   tagacc = tag access register (not clobbered)
8956127Ssm142603  *   tsbe = 2nd TSB base register
8966127Ssm142603  *   tmp1, tmp2 = scratch registers
8976127Ssm142603  * Out:
8986127Ssm142603  *   tsbe = pointer to the tsbe in the 2nd TSB
8996127Ssm142603  */
9006127Ssm142603 
9016127Ssm142603 #define	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9026127Ssm142603 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;	/* tmp2=szc */		\
9036127Ssm142603 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;	/* tsbbase */		\
9046127Ssm142603 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
9056127Ssm142603 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
9066127Ssm142603 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
9076127Ssm142603 	srlx	tagacc, MMU_PAGESHIFT4M, tmp2; 				\
9086127Ssm142603 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
9096127Ssm142603 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;	/* entry num --> ptr */	\
9106127Ssm142603 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
9116127Ssm142603 
9126127Ssm142603 #define	GET_2ND_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9136127Ssm142603 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
9146127Ssm142603 
9156127Ssm142603 /*
9166127Ssm142603  * Get the location in the 3rd TSB of the tsbe for this fault.
9176127Ssm142603  * The 3rd TSB corresponds to the shared context, and is used
9186127Ssm142603  * for 8K - 512k pages.
9196127Ssm142603  *
9206127Ssm142603  * In:
9216127Ssm142603  *   tagacc = tag access register (not clobbered)
9226127Ssm142603  *   tsbe, tmp1, tmp2 = scratch registers
9236127Ssm142603  * Out:
9246127Ssm142603  *   tsbe = pointer to the tsbe in the 3rd TSB
9256127Ssm142603  */
9266127Ssm142603 
9276127Ssm142603 #define	GET_3RD_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)			\
9286127Ssm142603 	and	tsbe, TSB_SOFTSZ_MASK, tmp2;    /* tmp2=szc */		\
9296127Ssm142603 	andn	tsbe, TSB_SOFTSZ_MASK, tsbe;    /* tsbbase */		\
9306127Ssm142603 	mov	TSB_ENTRIES(0), tmp1;	/* nentries in TSB size 0 */	\
9316127Ssm142603 	sllx	tmp1, tmp2, tmp1;	/* tmp1 = nentries in TSB */	\
9326127Ssm142603 	sub	tmp1, 1, tmp1;		/* mask = nentries - 1 */	\
9336127Ssm142603 	srlx	tagacc, MMU_PAGESHIFT, tmp2;				\
9346127Ssm142603 	and	tmp2, tmp1, tmp1;	/* tsbent = virtpage & mask */	\
9356127Ssm142603 	sllx	tmp1, TSB_ENTRY_SHIFT, tmp1;    /* entry num --> ptr */	\
9366127Ssm142603 	add	tsbe, tmp1, tsbe	/* add entry offset to TSB base */
9376127Ssm142603 
9386127Ssm142603 #define	GET_4TH_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)                      \
9396127Ssm142603 	GET_4MBASE_TSBE_PTR(tagacc, tsbe, tmp1, tmp2)
9406127Ssm142603 /*
9416127Ssm142603  * Copy the sfmmu_region_map or scd_region_map to the tsbmiss
9426127Ssm142603  * shmermap or scd_shmermap, from sfmmu_load_mmustate.
9436127Ssm142603  */
9446127Ssm142603 #define	SET_REGION_MAP(rgn_map, tsbmiss_map, cnt, tmp, label)		\
9456127Ssm142603 	/* BEGIN CSTYLED */						\
9466127Ssm142603 label:									;\
9476127Ssm142603         ldx     [rgn_map], tmp						;\
9486127Ssm142603         dec     cnt							;\
9496127Ssm142603         add     rgn_map, CLONGSIZE, rgn_map                             ;\
9506127Ssm142603         stx     tmp, [tsbmiss_map]                                      ;\
9516127Ssm142603         brnz,pt cnt, label                                              ;\
9526127Ssm142603 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map                    \
9536127Ssm142603 	/* END CSTYLED */
9546127Ssm142603 
9556127Ssm142603 /*
9566127Ssm142603  * If there is no scd, then zero the tsbmiss scd_shmermap,
9576127Ssm142603  * from sfmmu_load_mmustate.
9586127Ssm142603  */
9596127Ssm142603 #define	ZERO_REGION_MAP(tsbmiss_map, cnt, label)                        \
9606127Ssm142603 	/* BEGIN CSTYLED */                                             \
9616127Ssm142603 label:                                                                  ;\
9626127Ssm142603         dec     cnt                                                     ;\
9636127Ssm142603         stx     %g0, [tsbmiss_map]                                      ;\
9646127Ssm142603         brnz,pt cnt, label                                              ;\
9656127Ssm142603 	    add   tsbmiss_map, CLONGSIZE, tsbmiss_map
9666127Ssm142603 	/* END CSTYLED */
9676127Ssm142603 
9686127Ssm142603 /*
9696127Ssm142603  * Set hmemisc to 1 if the shared hme is also part of an scd.
9706127Ssm142603  * In:
9716127Ssm142603  *   tsbarea = tsbmiss area (not clobbered)
9726127Ssm142603  *   hmeblkpa  = hmeblkpa +  hmentoff + SFHME_TTE (not clobbered)
9736127Ssm142603  *   hmentoff = hmentoff + SFHME_TTE = tte offset(clobbered)
9746127Ssm142603  * Out:
9756127Ssm142603  *   use_shctx = 1 if shme is in scd and 0 otherwise
9766127Ssm142603  */
9776127Ssm142603 #define	GET_SCDSHMERMAP(tsbarea, hmeblkpa, hmentoff, use_shctx)               \
9786127Ssm142603 	/* BEGIN CSTYLED */   	                                              \
9796127Ssm142603         sub     hmeblkpa, hmentoff, hmentoff    /* hmentofff = hmeblkpa */   ;\
9806127Ssm142603         add     hmentoff, HMEBLK_TAG, hmentoff                               ;\
9816127Ssm142603         ldxa    [hmentoff]ASI_MEM, hmentoff     /* read 1st part of tag */   ;\
9826127Ssm142603         and     hmentoff, HTAG_RID_MASK, hmentoff       /* mask off rid */   ;\
9836127Ssm142603         and     hmentoff, BT_ULMASK, use_shctx  /* mask bit index */         ;\
9846127Ssm142603         srlx    hmentoff, BT_ULSHIFT, hmentoff  /* extract word */           ;\
9856127Ssm142603         sllx    hmentoff, CLONGSHIFT, hmentoff  /* index */                  ;\
9866127Ssm142603         add     tsbarea, hmentoff, hmentoff             /* add to tsbarea */ ;\
9876127Ssm142603         ldx     [hmentoff + TSBMISS_SCDSHMERMAP], hmentoff      /* scdrgn */ ;\
9886127Ssm142603         srlx    hmentoff, use_shctx, use_shctx                               ;\
9896127Ssm142603         and     use_shctx, 0x1, use_shctx                                     \
9906127Ssm142603 	/* END CSTYLED */
9916127Ssm142603 
9926127Ssm142603 /*
9936127Ssm142603  * Synthesize a TSB base register contents for a process.
9946127Ssm142603  *
9956127Ssm142603  * In:
9966127Ssm142603  *   tsbinfo = TSB info pointer (ro)
9976127Ssm142603  *   tsbreg, tmp1 = scratch registers
9986127Ssm142603  * Out:
9996127Ssm142603  *   tsbreg = value to program into TSB base register
10006127Ssm142603  */
10016127Ssm142603 
10026127Ssm142603 #define	MAKE_UTSBREG(tsbinfo, tsbreg, tmp1)			\
10036127Ssm142603 	ldx	[tsbinfo + TSBINFO_PADDR], tsbreg;		\
10046127Ssm142603 	lduh	[tsbinfo + TSBINFO_SZCODE], tmp1;		\
10056127Ssm142603 	and	tmp1, TSB_SOFTSZ_MASK, tmp1;			\
10066127Ssm142603 	or	tsbreg, tmp1, tsbreg;
10076127Ssm142603 
10086127Ssm142603 
10096127Ssm142603 /*
10106127Ssm142603  * Load TSB base register to TSBMISS area for privte contexts.
10116127Ssm142603  * This register contains utsb_pabase in bits 63:13, and TSB size
10126127Ssm142603  * code in bits 2:0.
10136127Ssm142603  *
10146127Ssm142603  * For private context
10156127Ssm142603  * In:
10166127Ssm142603  *   tsbreg = value to load (ro)
10176127Ssm142603  *   regnum = constant or register
10186127Ssm142603  *   tmp1 = scratch register
10196127Ssm142603  * Out:
10206127Ssm142603  *   Specified scratchpad register updated
10216127Ssm142603  *
10226127Ssm142603  */
10236127Ssm142603 #define	SET_UTSBREG(regnum, tsbreg, tmp1)				\
10246127Ssm142603 	mov	regnum, tmp1;						\
10256127Ssm142603 	stxa	tsbreg, [tmp1]ASI_SCRATCHPAD	/* save tsbreg */
10266127Ssm142603 /*
10276127Ssm142603  * Get TSB base register from the scratchpad for private contexts
10286127Ssm142603  *
10296127Ssm142603  * In:
10306127Ssm142603  *   regnum = constant or register
10316127Ssm142603  *   tsbreg = scratch
10326127Ssm142603  * Out:
10336127Ssm142603  *   tsbreg = tsbreg from the specified scratchpad register
10346127Ssm142603  */
10356127Ssm142603 #define	GET_UTSBREG(regnum, tsbreg)					\
10366127Ssm142603 	mov	regnum, tsbreg;						\
10376127Ssm142603 	ldxa	[tsbreg]ASI_SCRATCHPAD, tsbreg
10386127Ssm142603 
10396127Ssm142603 /*
10406127Ssm142603  * Load TSB base register to TSBMISS area for shared contexts.
10416127Ssm142603  * This register contains utsb_pabase in bits 63:13, and TSB size
10426127Ssm142603  * code in bits 2:0.
10436127Ssm142603  *
10446127Ssm142603  * In:
10456127Ssm142603  *   tsbmiss = pointer to tsbmiss area
10466127Ssm142603  *   tsbmissoffset = offset to right tsb pointer
10476127Ssm142603  *   tsbreg = value to load (ro)
10486127Ssm142603  * Out:
10496127Ssm142603  *   Specified tsbmiss area updated
10506127Ssm142603  *
10516127Ssm142603  */
10526127Ssm142603 #define	SET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
10536127Ssm142603 	stx	tsbreg, [tsbmiss + tsbmissoffset]	/* save tsbreg */
10546127Ssm142603 
10556127Ssm142603 /*
10566127Ssm142603  * Get TSB base register from the scratchpad for
10576127Ssm142603  * shared contexts
10586127Ssm142603  *
10596127Ssm142603  * In:
10606127Ssm142603  *   tsbmiss = pointer to tsbmiss area
10616127Ssm142603  *   tsbmissoffset = offset to right tsb pointer
10626127Ssm142603  *   tsbreg = scratch
10636127Ssm142603  * Out:
10646127Ssm142603  *   tsbreg = tsbreg from the specified scratchpad register
10656127Ssm142603  */
10666127Ssm142603 #define	GET_UTSBREG_SHCTX(tsbmiss, tsbmissoffset, tsbreg)		\
10676127Ssm142603 	ldx	[tsbmiss + tsbmissoffset], tsbreg
10686127Ssm142603 
10696127Ssm142603 #endif /* defined(sun4v) || defined(UTSB_PHYS) */
10703687Sjb145095 
10710Sstevel@tonic-gate #ifndef	_ASM
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10740Sstevel@tonic-gate  * Kernel page relocation stuff.
10750Sstevel@tonic-gate  */
10760Sstevel@tonic-gate struct sfmmu_callback {
1077670Selowe 	int key;
10780Sstevel@tonic-gate 	int (*prehandler)(caddr_t, uint_t, uint_t, void *);
10790Sstevel@tonic-gate 	int (*posthandler)(caddr_t, uint_t, uint_t, void *, pfn_t);
10800Sstevel@tonic-gate 	int (*errhandler)(caddr_t, uint_t, uint_t, void *);
10810Sstevel@tonic-gate 	int capture_cpus;
10820Sstevel@tonic-gate };
10830Sstevel@tonic-gate 
10840Sstevel@tonic-gate extern int sfmmu_max_cb_id;
10850Sstevel@tonic-gate extern struct sfmmu_callback *sfmmu_cb_table;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate extern int hat_kpr_enabled;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate struct pa_hment;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate /*
10920Sstevel@tonic-gate  * RFE: With multihat gone we gain back an int.  We could use this to
10930Sstevel@tonic-gate  * keep ref bits on a per cpu basis to eliminate xcalls.
10940Sstevel@tonic-gate  */
10950Sstevel@tonic-gate struct sf_hment {
10960Sstevel@tonic-gate 	tte_t hme_tte;			/* tte for this hment */
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate 	union {
10990Sstevel@tonic-gate 		struct page *page;	/* what page this maps */
11000Sstevel@tonic-gate 		struct pa_hment *data;	/* pa_hment */
11010Sstevel@tonic-gate 	} sf_hment_un;
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate 	struct	sf_hment *hme_next;	/* next hment */
11040Sstevel@tonic-gate 	struct	sf_hment *hme_prev;	/* prev hment */
11050Sstevel@tonic-gate };
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate struct pa_hment {
11080Sstevel@tonic-gate 	caddr_t		addr;		/* va */
11090Sstevel@tonic-gate 	uint_t		len;		/* bytes */
11100Sstevel@tonic-gate 	ushort_t	flags;		/* internal flags */
11110Sstevel@tonic-gate 	ushort_t	refcnt;		/* reference count */
11120Sstevel@tonic-gate 	id_t		cb_id;		/* callback id, table index */
11130Sstevel@tonic-gate 	void		*pvt;		/* handler's private data */
11140Sstevel@tonic-gate 	struct sf_hment	sfment;		/* corresponding dummy sf_hment */
11150Sstevel@tonic-gate };
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate #define	hme_page		sf_hment_un.page
11180Sstevel@tonic-gate #define	hme_data		sf_hment_un.data
11190Sstevel@tonic-gate #define	hme_size(sfhmep)	((int)(TTE_CSZ(&(sfhmep)->hme_tte)))
11200Sstevel@tonic-gate #define	PAHME_SZ		(sizeof (struct pa_hment))
11210Sstevel@tonic-gate #define	SFHME_SZ		(sizeof (struct sf_hment))
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate #define	IS_PAHME(hme)	((hme)->hme_tte.ll == 0)
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate /*
11260Sstevel@tonic-gate  * hmeblk_tag structure
11270Sstevel@tonic-gate  * structure used to obtain a match on a hme_blk.  Currently consists of
11280Sstevel@tonic-gate  * the address of the sfmmu struct (or hatid), the base page address of the
11290Sstevel@tonic-gate  * hme_blk, and the rehash count.  The rehash count is actually only 2 bits
11300Sstevel@tonic-gate  * and has the following meaning:
11310Sstevel@tonic-gate  * 1 = 8k or 64k hash sequence.
11320Sstevel@tonic-gate  * 2 = 512k hash sequence.
11330Sstevel@tonic-gate  * 3 = 4M hash sequence.
11340Sstevel@tonic-gate  * We require this count because we don't want to get a false hit on a 512K or
11350Sstevel@tonic-gate  * 4M rehash with a base address corresponding to a 8k or 64k hmeblk.
11360Sstevel@tonic-gate  * Note:  The ordering and size of the hmeblk_tag members are implictly known
11370Sstevel@tonic-gate  * by the tsb miss handlers written in assembly.  Do not change this structure
11380Sstevel@tonic-gate  * without checking those routines.  See HTAG_SFMMUPSZ define.
11390Sstevel@tonic-gate  */
11400Sstevel@tonic-gate 
11414528Spaulsan /*
11424528Spaulsan  * In private hmeblks hblk_rid field must be SFMMU_INVALID_RID.
11434528Spaulsan  */
11440Sstevel@tonic-gate typedef union {
11450Sstevel@tonic-gate 	struct {
11464528Spaulsan 		uint64_t	hblk_basepg: 51,	/* hme_blk base pg # */
11474528Spaulsan 				hblk_rehash: 3,		/* rehash number */
11484528Spaulsan 				hblk_rid: 10;		/* hme_blk region id */
11494528Spaulsan 		void		*hblk_id;
11500Sstevel@tonic-gate 	} hblk_tag_un;
11510Sstevel@tonic-gate 	uint64_t		htag_tag[2];
11520Sstevel@tonic-gate } hmeblk_tag;
11530Sstevel@tonic-gate 
11544528Spaulsan #define	htag_id		hblk_tag_un.hblk_id
11550Sstevel@tonic-gate #define	htag_bspage	hblk_tag_un.hblk_basepg
11560Sstevel@tonic-gate #define	htag_rehash	hblk_tag_un.hblk_rehash
11574528Spaulsan #define	htag_rid	hblk_tag_un.hblk_rid
11584528Spaulsan 
11594528Spaulsan #endif /* !_ASM */
11604528Spaulsan 
11614528Spaulsan #define	HTAG_REHASH_SHIFT	10
11624528Spaulsan #define	HTAG_MAX_RID	(((0x1 << HTAG_REHASH_SHIFT) - 1))
11634528Spaulsan #define	HTAG_RID_MASK	HTAG_MAX_RID
11644528Spaulsan 
11654528Spaulsan /* used for tagging all per sfmmu (i.e. non SRD) private hmeblks */
11664528Spaulsan #define	SFMMU_INVALID_SHMERID	HTAG_MAX_RID
11674528Spaulsan 
11684528Spaulsan #if SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
11694528Spaulsan #error SFMMU_INVALID_SHMERID < SFMMU_MAX_HME_REGIONS
11704528Spaulsan #endif
11714528Spaulsan 
11724528Spaulsan #define	SFMMU_IS_SHMERID_VALID(rid)	((rid) != SFMMU_INVALID_SHMERID)
11734528Spaulsan 
11744528Spaulsan /* ISM regions */
11754528Spaulsan #define	SFMMU_INVALID_ISMRID	0xff
11764528Spaulsan 
11774528Spaulsan #if SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
11784528Spaulsan #error SFMMU_INVALID_ISMRID < SFMMU_MAX_ISM_REGIONS
11794528Spaulsan #endif
11804528Spaulsan 
11814528Spaulsan #define	SFMMU_IS_ISMRID_VALID(rid)	((rid) != SFMMU_INVALID_ISMRID)
11824528Spaulsan 
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate #define	HTAGS_EQ(tag1, tag2)	(((tag1.htag_tag[0] ^ tag2.htag_tag[0]) | \
11850Sstevel@tonic-gate 				(tag1.htag_tag[1] ^ tag2.htag_tag[1])) == 0)
11864528Spaulsan 
11874528Spaulsan /*
11884528Spaulsan  * this macro must only be used for comparing tags in shared hmeblks.
11894528Spaulsan  */
11904528Spaulsan #define	HTAGS_EQ_SHME(hmetag, tag, hrmap)				\
11914528Spaulsan 	(((hmetag).htag_rid != SFMMU_INVALID_SHMERID) &&	        \
11924528Spaulsan 	(((((hmetag).htag_tag[0] ^ (tag).htag_tag[0]) &			\
11934528Spaulsan 		~HTAG_RID_MASK) |	        			\
11944528Spaulsan 	    ((hmetag).htag_tag[1] ^ (tag).htag_tag[1])) == 0) &&	\
11954528Spaulsan 	SF_RGNMAP_TEST(hrmap, hmetag.htag_rid))
11964528Spaulsan 
11970Sstevel@tonic-gate #define	HME_REHASH(sfmmup)						\
11980Sstevel@tonic-gate 	((sfmmup)->sfmmu_ttecnt[TTE512K] != 0 ||			\
11990Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE4M] != 0 ||				\
12000Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE32M] != 0 ||				\
12010Sstevel@tonic-gate 	(sfmmup)->sfmmu_ttecnt[TTE256M] != 0)
12020Sstevel@tonic-gate 
12030Sstevel@tonic-gate #define	NHMENTS		8		/* # of hments in an 8k hme_blk */
12040Sstevel@tonic-gate 					/* needs to be multiple of 2 */
12054528Spaulsan 
12060Sstevel@tonic-gate #ifndef	_ASM
12070Sstevel@tonic-gate 
12080Sstevel@tonic-gate #ifdef	HBLK_TRACE
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate #define	HBLK_LOCK		1
12110Sstevel@tonic-gate #define	HBLK_UNLOCK		0
12120Sstevel@tonic-gate #define	HBLK_STACK_DEPTH	6
12130Sstevel@tonic-gate #define	HBLK_AUDIT_CACHE_SIZE	16
12140Sstevel@tonic-gate #define	HBLK_LOCK_PATTERN	0xaaaaaaaa
12150Sstevel@tonic-gate #define	HBLK_UNLOCK_PATTERN	0xbbbbbbbb
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate struct hblk_lockcnt_audit {
12180Sstevel@tonic-gate 	int		flag;		/* lock or unlock */
12190Sstevel@tonic-gate 	kthread_id_t	thread;
12200Sstevel@tonic-gate 	int		depth;
12210Sstevel@tonic-gate 	pc_t		stack[HBLK_STACK_DEPTH];
12220Sstevel@tonic-gate };
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate #endif	/* HBLK_TRACE */
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * Hment block structure.
12290Sstevel@tonic-gate  * The hme_blk is the node data structure which the hash structure
12300Sstevel@tonic-gate  * mantains. An hme_blk can have 2 different sizes depending on the
12310Sstevel@tonic-gate  * number of hments it implicitly contains.  When dealing with 64K, 512K,
12320Sstevel@tonic-gate  * or 4M hments there is one hment per hme_blk.  When dealing with
12330Sstevel@tonic-gate  * 8k hments we allocate an hme_blk plus an additional 7 hments to
12340Sstevel@tonic-gate  * give us a total of 8 (NHMENTS) hments that can be referenced through a
12350Sstevel@tonic-gate  * hme_blk.
12360Sstevel@tonic-gate  *
12370Sstevel@tonic-gate  * The hmeblk structure contains 2 tte reference counters used to determine if
12380Sstevel@tonic-gate  * it is ok to free up the hmeblk.  Both counters have to be zero in order
12390Sstevel@tonic-gate  * to be able to free up hmeblk.  They are protected by cas.
12400Sstevel@tonic-gate  * hblk_hmecnt is the number of hments present on pp mapping lists.
12410Sstevel@tonic-gate  * hblk_vcnt reflects number of valid ttes in hmeblk.
12420Sstevel@tonic-gate  *
12430Sstevel@tonic-gate  * The hmeblk now also has per tte lock cnts.  This is required because
12440Sstevel@tonic-gate  * the counts can be high and there are not enough bits in the tte. When
12450Sstevel@tonic-gate  * physio is fixed to not lock the translations we should be able to move
12460Sstevel@tonic-gate  * the lock cnt back to the tte.  See bug id 1198554.
12470Sstevel@tonic-gate  *
12480Sstevel@tonic-gate  * Note that xhat_hme_blk's layout follows this structure: hme_blk_misc
12490Sstevel@tonic-gate  * and sf_hment are at the same offsets in both structures. Whenever
12500Sstevel@tonic-gate  * hme_blk is changed, xhat_hme_blk may need to be updated as well.
12510Sstevel@tonic-gate  */
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate struct hme_blk_misc {
12544528Spaulsan 	uint_t	notused:25;
12554528Spaulsan 	uint_t	shared_bit:1;	/* set for SRD shared hmeblk */
12560Sstevel@tonic-gate 	uint_t	xhat_bit:1;	/* set for an xhat hme_blk */
12570Sstevel@tonic-gate 	uint_t	shadow_bit:1;	/* set for a shadow hme_blk */
12580Sstevel@tonic-gate 	uint_t	nucleus_bit:1;	/* set for a nucleus hme_blk */
12590Sstevel@tonic-gate 	uint_t	ttesize:3;	/* contains ttesz of hmeblk */
12600Sstevel@tonic-gate };
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate struct hme_blk {
12638187SPaul.Sandhu@Sun.COM 	volatile uint64_t hblk_nextpa;	/* physical address for hash list */
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	hmeblk_tag	hblk_tag;	/* tag used to obtain an hmeblk match */
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	struct hme_blk	*hblk_next;	/* on free list or on hash list */
12680Sstevel@tonic-gate 					/* protected by hash lock */
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	struct hme_blk	*hblk_shadow;	/* pts to shadow hblk */
12710Sstevel@tonic-gate 					/* protected by hash lock */
12720Sstevel@tonic-gate 	uint_t		hblk_span;	/* span of memory hmeblk maps */
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 	struct hme_blk_misc	hblk_misc;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	union {
12770Sstevel@tonic-gate 		struct {
12780Sstevel@tonic-gate 			ushort_t hblk_hmecount;	/* hment on mlists counter */
12790Sstevel@tonic-gate 			ushort_t hblk_validcnt;	/* valid tte reference count */
12800Sstevel@tonic-gate 		} hblk_counts;
12810Sstevel@tonic-gate 		uint_t		hblk_shadow_mask;
12820Sstevel@tonic-gate 	} hblk_un;
12830Sstevel@tonic-gate 
12844528Spaulsan 	uint_t		hblk_lckcnt;
12854528Spaulsan 
12860Sstevel@tonic-gate #ifdef	HBLK_TRACE
12870Sstevel@tonic-gate 	kmutex_t	hblk_audit_lock;	/* lock to protect index */
12880Sstevel@tonic-gate 	uint_t		hblk_audit_index;	/* index into audit_cache */
12890Sstevel@tonic-gate 	struct	hblk_lockcnt_audit hblk_audit_cache[HBLK_AUDIT_CACHE_SIZE];
12900Sstevel@tonic-gate #endif	/* HBLK_AUDIT */
12910Sstevel@tonic-gate 
12920Sstevel@tonic-gate 	struct sf_hment hblk_hme[1];	/* hment array */
12930Sstevel@tonic-gate };
12940Sstevel@tonic-gate 
12954528Spaulsan #define	hblk_shared	hblk_misc.shared_bit
12960Sstevel@tonic-gate #define	hblk_xhat_bit   hblk_misc.xhat_bit
12970Sstevel@tonic-gate #define	hblk_shw_bit	hblk_misc.shadow_bit
12980Sstevel@tonic-gate #define	hblk_nuc_bit	hblk_misc.nucleus_bit
12990Sstevel@tonic-gate #define	hblk_ttesz	hblk_misc.ttesize
13000Sstevel@tonic-gate #define	hblk_hmecnt	hblk_un.hblk_counts.hblk_hmecount
13010Sstevel@tonic-gate #define	hblk_vcnt	hblk_un.hblk_counts.hblk_validcnt
13020Sstevel@tonic-gate #define	hblk_shw_mask	hblk_un.hblk_shadow_mask
13030Sstevel@tonic-gate 
13044528Spaulsan #define	MAX_HBLK_LCKCNT	0xFFFFFFFF
13050Sstevel@tonic-gate #define	HMEBLK_ALIGN	0x8		/* hmeblk has to be double aligned */
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate #ifdef	HBLK_TRACE
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate #define	HBLK_STACK_TRACE(hmeblkp, lock)					\
13100Sstevel@tonic-gate {									\
13110Sstevel@tonic-gate 	int flag = lock;	/* to pacify lint */			\
13120Sstevel@tonic-gate 	int audit_index;						\
13130Sstevel@tonic-gate 									\
13140Sstevel@tonic-gate 	mutex_enter(&hmeblkp->hblk_audit_lock);				\
13150Sstevel@tonic-gate 	audit_index = hmeblkp->hblk_audit_index;			\
13160Sstevel@tonic-gate 	hmeblkp->hblk_audit_index = ((hmeblkp->hblk_audit_index + 1) &	\
13170Sstevel@tonic-gate 	    (HBLK_AUDIT_CACHE_SIZE - 1));				\
13180Sstevel@tonic-gate 	mutex_exit(&hmeblkp->hblk_audit_lock);				\
13190Sstevel@tonic-gate 									\
13200Sstevel@tonic-gate 	if (flag)							\
13210Sstevel@tonic-gate 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
13220Sstevel@tonic-gate 		    HBLK_LOCK_PATTERN;					\
13230Sstevel@tonic-gate 	else								\
13240Sstevel@tonic-gate 		hmeblkp->hblk_audit_cache[audit_index].flag =		\
13250Sstevel@tonic-gate 		    HBLK_UNLOCK_PATTERN;				\
13260Sstevel@tonic-gate 									\
13270Sstevel@tonic-gate 	hmeblkp->hblk_audit_cache[audit_index].thread = curthread;	\
13280Sstevel@tonic-gate 	hmeblkp->hblk_audit_cache[audit_index].depth =			\
13290Sstevel@tonic-gate 	    getpcstack(hmeblkp->hblk_audit_cache[audit_index].stack,	\
13300Sstevel@tonic-gate 	    HBLK_STACK_DEPTH);						\
13310Sstevel@tonic-gate }
13320Sstevel@tonic-gate 
13330Sstevel@tonic-gate #else
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate #define	HBLK_STACK_TRACE(hmeblkp, lock)
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate #endif	/* HBLK_TRACE */
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate #define	HMEHASH_FACTOR	16	/* used to calc # of buckets in hme hash */
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate /*
13420Sstevel@tonic-gate  * A maximum number of user hmeblks is defined in order to place an upper
13430Sstevel@tonic-gate  * limit on how much nucleus memory is required and to avoid overflowing the
13440Sstevel@tonic-gate  * tsbmiss uhashsz and khashsz data areas. The number below corresponds to
13450Sstevel@tonic-gate  * the number of buckets required, for an average hash chain length of 4 on
13460Sstevel@tonic-gate  * a 16TB machine.
13470Sstevel@tonic-gate  */
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate #define	MAX_UHME_BUCKETS	(0x1 << 30)
13500Sstevel@tonic-gate #define	MAX_KHME_BUCKETS	(0x1 << 30)
13510Sstevel@tonic-gate 
13520Sstevel@tonic-gate /*
13530Sstevel@tonic-gate  * The minimum number of kernel hash buckets.
13540Sstevel@tonic-gate  */
13550Sstevel@tonic-gate #define	MIN_KHME_BUCKETS	0x800
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate /*
13580Sstevel@tonic-gate  * The number of hash buckets must be a power of 2. If the initial calculated
13590Sstevel@tonic-gate  * value is less than USER_BUCKETS_THRESHOLD we round up to the next greater
13600Sstevel@tonic-gate  * power of 2, otherwise we round down to avoid huge over allocations.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate #define	USER_BUCKETS_THRESHOLD	(1<<22)
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate #define	MAX_NUCUHME_BUCKETS	0x4000
13650Sstevel@tonic-gate #define	MAX_NUCKHME_BUCKETS	0x2000
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate /*
13680Sstevel@tonic-gate  * There are 2 locks in the hmehash bucket.  The hmehash_mutex is
13690Sstevel@tonic-gate  * a regular mutex used to make sure operations on a hash link are only
13700Sstevel@tonic-gate  * done by one thread.  Any operation which comes into the hat with
13710Sstevel@tonic-gate  * a <vaddr, as> will grab the hmehash_mutex.  Normally one would expect
13720Sstevel@tonic-gate  * the tsb miss handlers to grab the hash lock to make sure the hash list
13730Sstevel@tonic-gate  * is consistent while we traverse it.  Unfortunately this can lead to
13740Sstevel@tonic-gate  * deadlocks or recursive mutex enters since it is possible for
13750Sstevel@tonic-gate  * someone holding the lock to take a tlb/tsb miss.
13760Sstevel@tonic-gate  * To solve this problem we have added the hmehash_listlock.  This lock
13770Sstevel@tonic-gate  * is only grabbed by the tsb miss handlers, vatopfn, and while
13780Sstevel@tonic-gate  * adding/removing a hmeblk from the hash list. The code is written to
13790Sstevel@tonic-gate  * guarantee we won't take a tlb miss while holding this lock.
13800Sstevel@tonic-gate  */
13810Sstevel@tonic-gate struct hmehash_bucket {
13820Sstevel@tonic-gate 	kmutex_t	hmehash_mutex;
13838187SPaul.Sandhu@Sun.COM 	volatile uint64_t hmeh_nextpa;	/* physical address for hash list */
13840Sstevel@tonic-gate 	struct hme_blk *hmeblkp;
13850Sstevel@tonic-gate 	uint_t		hmeh_listlock;
13860Sstevel@tonic-gate };
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate #endif /* !_ASM */
13890Sstevel@tonic-gate 
13902241Shuah #define	SFMMU_PGCNT_MASK	0x3f
13912241Shuah #define	SFMMU_PGCNT_SHIFT	6
13922241Shuah #define	INVALID_MMU_ID		-1
13932241Shuah #define	SFMMU_MMU_GNUM_RSHIFT	16
13942241Shuah #define	SFMMU_MMU_CNUM_LSHIFT	(64 - SFMMU_MMU_GNUM_RSHIFT)
13952241Shuah #define	MAX_SFMMU_CTX_VAL	((1 << 16) - 1) /* for sanity check */
13962241Shuah #define	MAX_SFMMU_GNUM_VAL	((0x1UL << 48) - 1)
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate /*
13990Sstevel@tonic-gate  * The tsb miss handlers written in assembly know that sfmmup
14000Sstevel@tonic-gate  * is a 64 bit ptr.
14010Sstevel@tonic-gate  *
14020Sstevel@tonic-gate  * The bspage and re-hash part is 64 bits, with the sfmmup being another 64
14030Sstevel@tonic-gate  * bits.
14040Sstevel@tonic-gate  */
14050Sstevel@tonic-gate #define	HTAG_SFMMUPSZ		0	/* Not really used for LP64 */
14064528Spaulsan #define	HTAG_BSPAGE_SHIFT	13
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * Assembly routines need to be able to get to ttesz
14100Sstevel@tonic-gate  */
14110Sstevel@tonic-gate #define	HBLK_SZMASK		0x7
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate #ifndef _ASM
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate  * Returns the number of bytes that an hmeblk spans given its tte size
14170Sstevel@tonic-gate  */
14180Sstevel@tonic-gate #define	get_hblk_span(hmeblkp) ((hmeblkp)->hblk_span)
14190Sstevel@tonic-gate #define	get_hblk_ttesz(hmeblkp)	((hmeblkp)->hblk_ttesz)
14200Sstevel@tonic-gate #define	get_hblk_cache(hmeblkp)	(((hmeblkp)->hblk_ttesz == TTE8K) ? \
14210Sstevel@tonic-gate 	sfmmu8_cache : sfmmu1_cache)
14220Sstevel@tonic-gate #define	HMEBLK_SPAN(ttesz)						\
14230Sstevel@tonic-gate 	((ttesz == TTE8K)? (TTEBYTES(ttesz) * NHMENTS) : TTEBYTES(ttesz))
14240Sstevel@tonic-gate 
14250Sstevel@tonic-gate #define	set_hblk_sz(hmeblkp, ttesz)				\
14260Sstevel@tonic-gate 	(hmeblkp)->hblk_ttesz = (ttesz);			\
14270Sstevel@tonic-gate 	(hmeblkp)->hblk_span = HMEBLK_SPAN(ttesz)
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate #define	get_hblk_base(hmeblkp)					\
14300Sstevel@tonic-gate 	((uintptr_t)(hmeblkp)->hblk_tag.htag_bspage << MMU_PAGESHIFT)
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate #define	get_hblk_endaddr(hmeblkp)				\
14330Sstevel@tonic-gate 	((caddr_t)(get_hblk_base(hmeblkp) + get_hblk_span(hmeblkp)))
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate #define	in_hblk_range(hmeblkp, vaddr)					\
14360Sstevel@tonic-gate 	(((uintptr_t)(vaddr) >= get_hblk_base(hmeblkp)) &&		\
14370Sstevel@tonic-gate 	((uintptr_t)(vaddr) < (get_hblk_base(hmeblkp) +			\
14380Sstevel@tonic-gate 	get_hblk_span(hmeblkp))))
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate #define	tte_to_vaddr(hmeblkp, tte)	((caddr_t)(get_hblk_base(hmeblkp) \
14410Sstevel@tonic-gate 	+ (TTEBYTES(TTE_CSZ(&tte)) * (tte).tte_hmenum)))
14420Sstevel@tonic-gate 
14434528Spaulsan #define	tte_to_evaddr(hmeblkp, ttep)	((caddr_t)(get_hblk_base(hmeblkp) \
14444528Spaulsan 	+ (TTEBYTES(TTE_CSZ(ttep)) * ((ttep)->tte_hmenum + 1))))
14454528Spaulsan 
14460Sstevel@tonic-gate #define	vaddr_to_vshift(hblktag, vaddr, shwsz)				\
14470Sstevel@tonic-gate 	((((uintptr_t)(vaddr) >> MMU_PAGESHIFT) - (hblktag.htag_bspage)) >>\
14480Sstevel@tonic-gate 	TTE_BSZS_SHIFT((shwsz) - 1))
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate #define	HME8BLK_SZ	(sizeof (struct hme_blk) + \
14510Sstevel@tonic-gate 			(NHMENTS - 1) * sizeof (struct sf_hment))
14520Sstevel@tonic-gate #define	HME1BLK_SZ	(sizeof (struct hme_blk))
14530Sstevel@tonic-gate #define	H1MIN		(2 + MAX_BIGKTSB_TTES)	/* nucleus text+data, ktsb */
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate /*
14560Sstevel@tonic-gate  * Hme_blk hash structure
14570Sstevel@tonic-gate  * Active mappings are kept in a hash structure of hme_blks.  The hash
14580Sstevel@tonic-gate  * function is based on (ctx, vaddr) The size of the hash table size is a
14590Sstevel@tonic-gate  * power of 2 such that the average hash chain lenth is HMENT_HASHAVELEN.
14600Sstevel@tonic-gate  * The hash actually consists of 2 separate hashes.  One hash is for the user
14610Sstevel@tonic-gate  * address space and the other hash is for the kernel address space.
14620Sstevel@tonic-gate  * The number of buckets are calculated at boot time and stored in the global
14630Sstevel@tonic-gate  * variables "uhmehash_num" and "khmehash_num".  By making the hash table size
14640Sstevel@tonic-gate  * a power of 2 we can use a simply & function to derive an index instead of
14650Sstevel@tonic-gate  * a divide.
14660Sstevel@tonic-gate  *
14670Sstevel@tonic-gate  * HME_HASH_FUNCTION(hatid, vaddr, shift) returns a pointer to a hme_hash
14680Sstevel@tonic-gate  * bucket.
14690Sstevel@tonic-gate  * An hme hash bucket contains a pointer to an hme_blk and the mutex that
14700Sstevel@tonic-gate  * protects the link list.
14710Sstevel@tonic-gate  * Spitfire supports 4 page sizes.  8k and 64K pages only need one hash.
14720Sstevel@tonic-gate  * 512K pages need 2 hashes and 4M pages need 3 hashes.
14730Sstevel@tonic-gate  * The 'shift' parameter controls how many bits the vaddr will be shifted in
14740Sstevel@tonic-gate  * the hash function. It is calculated in the HME_HASH_SHIFT(ttesz) function
14750Sstevel@tonic-gate  * and it varies depending on the page size as follows:
14760Sstevel@tonic-gate  *	8k pages:  	HBLK_RANGE_SHIFT
14770Sstevel@tonic-gate  *	64k pages:	MMU_PAGESHIFT64K
14780Sstevel@tonic-gate  *	512K pages:	MMU_PAGESHIFT512K
14790Sstevel@tonic-gate  *	4M pages:	MMU_PAGESHIFT4M
14800Sstevel@tonic-gate  * An assembly version of the hash function exists in sfmmu_ktsb_miss(). All
14810Sstevel@tonic-gate  * changes should be reflected in both versions.  This function and the TSB
14820Sstevel@tonic-gate  * miss handlers are the only places which know about the two hashes.
14830Sstevel@tonic-gate  *
14840Sstevel@tonic-gate  * HBLK_RANGE_SHIFT controls range of virtual addresses that will fall
14850Sstevel@tonic-gate  * into the same bucket for a particular process.  It is currently set to
14860Sstevel@tonic-gate  * be equivalent to 64K range or one hme_blk.
14870Sstevel@tonic-gate  *
14880Sstevel@tonic-gate  * The hme_blks in the hash are protected by a per hash bucket mutex
14890Sstevel@tonic-gate  * known as SFMMU_HASH_LOCK.
14900Sstevel@tonic-gate  * You need to acquire this lock before traversing the hash bucket link
14910Sstevel@tonic-gate  * list, while adding/removing a hme_blk to the list, and while
14920Sstevel@tonic-gate  * modifying an hme_blk.  A possible optimization is to replace these
14930Sstevel@tonic-gate  * mutexes by readers/writer lock but right now it is not clear whether
14940Sstevel@tonic-gate  * this is a win or not.
14950Sstevel@tonic-gate  *
14960Sstevel@tonic-gate  * The HME_HASH_TABLE_SEARCH will search the hash table for the
14970Sstevel@tonic-gate  * hme_blk that contains the hment that corresponds to the passed
14980Sstevel@tonic-gate  * ctx and vaddr.  It assumed the SFMMU_HASH_LOCK is held.
14990Sstevel@tonic-gate  */
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate #endif /* ! _ASM */
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate #define	KHATID			ksfmmup
15040Sstevel@tonic-gate #define	UHMEHASH_SZ		uhmehash_num
15050Sstevel@tonic-gate #define	KHMEHASH_SZ		khmehash_num
15060Sstevel@tonic-gate #define	HMENT_HASHAVELEN	4
15070Sstevel@tonic-gate #define	HBLK_RANGE_SHIFT	MMU_PAGESHIFT64K /* shift for HBLK_BS_MASK */
15084528Spaulsan #define	HBLK_MIN_TTESZ		1
15094528Spaulsan #define	HBLK_MIN_BYTES		MMU_PAGESIZE64K
15104528Spaulsan #define	HBLK_MIN_SHIFT		MMU_PAGESHIFT64K
15110Sstevel@tonic-gate #define	MAX_HASHCNT		5
15120Sstevel@tonic-gate #define	DEFAULT_MAX_HASHCNT	3
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate #ifndef _ASM
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate #define	HASHADDR_MASK(hashno)	TTE_PAGEMASK(hashno)
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate #define	HME_HASH_SHIFT(ttesz)						\
15194319Skupfer 	((ttesz == TTE8K)? HBLK_RANGE_SHIFT : TTE_PAGE_SHIFT(ttesz))
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate #define	HME_HASH_ADDR(vaddr, hmeshift)					\
15220Sstevel@tonic-gate 	((caddr_t)(((uintptr_t)(vaddr) >> (hmeshift)) << (hmeshift)))
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate #define	HME_HASH_BSPAGE(vaddr, hmeshift)				\
15250Sstevel@tonic-gate 	(((uintptr_t)(vaddr) >> (hmeshift)) << ((hmeshift) - MMU_PAGESHIFT))
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate #define	HME_HASH_REHASH(ttesz)						\
15280Sstevel@tonic-gate 	(((ttesz) < TTE512K)? 1 : (ttesz))
15290Sstevel@tonic-gate 
15304528Spaulsan #define	HME_HASH_FUNCTION(hatid, vaddr, shift)				     \
15314528Spaulsan 	((((void *)hatid) != ((void *)KHATID)) ?			     \
15324528Spaulsan 	(&uhme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
15334528Spaulsan 	    UHMEHASH_SZ) ]):						     \
15344528Spaulsan 	(&khme_hash[ (((uintptr_t)(hatid) ^ ((uintptr_t)vaddr >> (shift))) & \
15354528Spaulsan 	    KHMEHASH_SZ) ]))
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate /*
15380Sstevel@tonic-gate  * This macro will traverse a hmeblk hash link list looking for an hme_blk
15390Sstevel@tonic-gate  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
15400Sstevel@tonic-gate  * will be set to NULL, otherwise it will point to the correct hme_blk.
15410Sstevel@tonic-gate  * This macro also cleans empty hblks.
15420Sstevel@tonic-gate  */
15438187SPaul.Sandhu@Sun.COM #define	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp, pr_hblk, listp)	\
15440Sstevel@tonic-gate {									\
15450Sstevel@tonic-gate 	struct hme_blk *nx_hblk;					\
15460Sstevel@tonic-gate 									\
15470Sstevel@tonic-gate 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
15480Sstevel@tonic-gate 	hblkp = hmebp->hmeblkp;						\
15490Sstevel@tonic-gate 	pr_hblk = NULL;							\
15500Sstevel@tonic-gate 	while (hblkp) {							\
15510Sstevel@tonic-gate 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
15520Sstevel@tonic-gate 			/* found hme_blk */				\
15530Sstevel@tonic-gate 			break;						\
15540Sstevel@tonic-gate 		}							\
15550Sstevel@tonic-gate 		nx_hblk = hblkp->hblk_next;				\
15560Sstevel@tonic-gate 		if (!hblkp->hblk_vcnt && !hblkp->hblk_hmecnt) {		\
15578187SPaul.Sandhu@Sun.COM 			sfmmu_hblk_hash_rm(hmebp, hblkp, pr_hblk,	\
15588187SPaul.Sandhu@Sun.COM 			    listp, 0);					\
15590Sstevel@tonic-gate 		} else {						\
15600Sstevel@tonic-gate 			pr_hblk = hblkp;				\
15610Sstevel@tonic-gate 		}							\
15620Sstevel@tonic-gate 		hblkp = nx_hblk;					\
15630Sstevel@tonic-gate 	}								\
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate #define	HME_HASH_SEARCH(hmebp, hblktag, hblkp, listp)			\
15670Sstevel@tonic-gate {									\
15680Sstevel@tonic-gate 	struct hme_blk *pr_hblk;					\
15690Sstevel@tonic-gate 									\
15708187SPaul.Sandhu@Sun.COM 	HME_HASH_SEARCH_PREV(hmebp, hblktag, hblkp,  pr_hblk, listp);	\
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate /*
15740Sstevel@tonic-gate  * This macro will traverse a hmeblk hash link list looking for an hme_blk
15750Sstevel@tonic-gate  * that owns the specified vaddr and hatid.  If if doesn't find one , hmeblkp
15760Sstevel@tonic-gate  * will be set to NULL, otherwise it will point to the correct hme_blk.
15770Sstevel@tonic-gate  * It doesn't remove empty hblks.
15780Sstevel@tonic-gate  */
15790Sstevel@tonic-gate #define	HME_HASH_FAST_SEARCH(hmebp, hblktag, hblkp)			\
15800Sstevel@tonic-gate 	ASSERT(SFMMU_HASH_LOCK_ISHELD(hmebp));				\
15810Sstevel@tonic-gate 	for (hblkp = hmebp->hmeblkp; hblkp;				\
15820Sstevel@tonic-gate 	    hblkp = hblkp->hblk_next) {					\
15830Sstevel@tonic-gate 		if (HTAGS_EQ(hblkp->hblk_tag, hblktag)) {		\
15840Sstevel@tonic-gate 			/* found hme_blk */				\
15850Sstevel@tonic-gate 			break;						\
15860Sstevel@tonic-gate 		}							\
15874319Skupfer 	}
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate #define	SFMMU_HASH_LOCK(hmebp)						\
15900Sstevel@tonic-gate 		(mutex_enter(&hmebp->hmehash_mutex))
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate #define	SFMMU_HASH_UNLOCK(hmebp)					\
15930Sstevel@tonic-gate 		(mutex_exit(&hmebp->hmehash_mutex))
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate #define	SFMMU_HASH_LOCK_TRYENTER(hmebp)					\
15960Sstevel@tonic-gate 		(mutex_tryenter(&hmebp->hmehash_mutex))
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate #define	SFMMU_HASH_LOCK_ISHELD(hmebp)					\
15990Sstevel@tonic-gate 		(mutex_owned(&hmebp->hmehash_mutex))
16000Sstevel@tonic-gate 
16012241Shuah #define	SFMMU_XCALL_STATS(sfmmup)					\
16020Sstevel@tonic-gate {									\
16032241Shuah 	if (sfmmup == ksfmmup) {					\
16040Sstevel@tonic-gate 		SFMMU_STAT(sf_kernel_xcalls);				\
16050Sstevel@tonic-gate 	} else {							\
16060Sstevel@tonic-gate 		SFMMU_STAT(sf_user_xcalls);				\
16070Sstevel@tonic-gate 	}								\
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate #define	astosfmmu(as)		((as)->a_hat)
16110Sstevel@tonic-gate #define	hblktosfmmu(hmeblkp)	((sfmmu_t *)(hmeblkp)->hblk_tag.htag_id)
16124528Spaulsan #define	hblktosrd(hmeblkp)	((sf_srd_t *)(hmeblkp)->hblk_tag.htag_id)
16130Sstevel@tonic-gate #define	sfmmutoas(sfmmup)	((sfmmup)->sfmmu_as)
16144528Spaulsan 
16154528Spaulsan #define	sfmmutohtagid(sfmmup, rid)			   \
16164528Spaulsan 	(((rid) == SFMMU_INVALID_SHMERID) ? (void *)(sfmmup) : \
16174528Spaulsan 	(void *)((sfmmup)->sfmmu_srdp))
16184528Spaulsan 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * We use the sfmmu data structure to keep the per as page coloring info.
16210Sstevel@tonic-gate  */
16220Sstevel@tonic-gate #define	as_color_bin(as)	(astosfmmu(as)->sfmmu_clrbin)
16230Sstevel@tonic-gate #define	as_color_start(as)	(astosfmmu(as)->sfmmu_clrstart)
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate typedef struct {
16260Sstevel@tonic-gate 	char	h8[HME8BLK_SZ];
16270Sstevel@tonic-gate } hblk8_t;
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate typedef struct {
16300Sstevel@tonic-gate 	char	h1[HME1BLK_SZ];
16310Sstevel@tonic-gate } hblk1_t;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate typedef struct {
16340Sstevel@tonic-gate 	ulong_t  	index;
16350Sstevel@tonic-gate 	ulong_t  	len;
16360Sstevel@tonic-gate 	hblk8_t		*list;
16370Sstevel@tonic-gate } nucleus_hblk8_info_t;
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate typedef struct {
16400Sstevel@tonic-gate 	ulong_t		index;
16410Sstevel@tonic-gate 	ulong_t		len;
16420Sstevel@tonic-gate 	hblk1_t		*list;
16430Sstevel@tonic-gate } nucleus_hblk1_info_t;
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate /*
16460Sstevel@tonic-gate  * This struct is used for accumlating information about a range
16470Sstevel@tonic-gate  * of pages that are unloading so that a single xcall can flush
16480Sstevel@tonic-gate  * the entire range from remote tlbs. A function that must demap
16490Sstevel@tonic-gate  * a range of virtual addresses declares one of these structures
16500Sstevel@tonic-gate  * and initializes using DEMP_RANGE_INIT(). It then passes a pointer to this
16510Sstevel@tonic-gate  * struct to the appropriate sfmmu_hblk_* level function which does
16520Sstevel@tonic-gate  * all the bookkeeping using the other macros. When the function has
16530Sstevel@tonic-gate  * finished the virtual address range, it needs to call DEMAP_RANGE_FLUSH()
16540Sstevel@tonic-gate  * macro to take care of any remaining unflushed mappings.
16550Sstevel@tonic-gate  *
16560Sstevel@tonic-gate  * The maximum range this struct can represent is the number of bits
16570Sstevel@tonic-gate  * in the dmr_bitvec field times the pagesize in dmr_pgsz. Currently, only
16580Sstevel@tonic-gate  * MMU_PAGESIZE pages are supported.
16590Sstevel@tonic-gate  *
16600Sstevel@tonic-gate  * Since there are now cases where it's no longer necessary to do
16610Sstevel@tonic-gate  * flushes (e.g. when the process isn't runnable because it's swapping
16620Sstevel@tonic-gate  * out or exiting) we allow these macros to take a NULL dmr input and do
16630Sstevel@tonic-gate  * nothing in that case.
16640Sstevel@tonic-gate  */
16650Sstevel@tonic-gate typedef struct {
16663517Smp204432 	sfmmu_t		*dmr_sfmmup;	/* relevant hat */
16670Sstevel@tonic-gate 	caddr_t		dmr_addr;	/* beginning address */
16680Sstevel@tonic-gate 	caddr_t		dmr_endaddr;	/* ending  address */
16690Sstevel@tonic-gate 	ulong_t		dmr_bitvec;	/* valid pages found */
16700Sstevel@tonic-gate 	ulong_t		dmr_bit;	/* next page to examine */
16710Sstevel@tonic-gate 	ulong_t		dmr_maxbit;	/* highest page in range */
16720Sstevel@tonic-gate 	ulong_t		dmr_pgsz;	/* page size in range */
16730Sstevel@tonic-gate } demap_range_t;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate #define	DMR_MAXBIT ((ulong_t)1<<63) /* dmr_bit high bit */
16760Sstevel@tonic-gate 
16770Sstevel@tonic-gate #define	DEMAP_RANGE_INIT(sfmmup, dmrp) \
16780Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
16790Sstevel@tonic-gate 	(dmrp)->dmr_sfmmup = (sfmmup); \
16800Sstevel@tonic-gate 	(dmrp)->dmr_bitvec = 0; \
16810Sstevel@tonic-gate 	(dmrp)->dmr_maxbit = sfmmu_dmr_maxbit; \
16820Sstevel@tonic-gate 	(dmrp)->dmr_pgsz = MMU_PAGESIZE; \
16830Sstevel@tonic-gate 	}
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate #define	DEMAP_RANGE_PGSZ(dmrp) ((dmrp)? (dmrp)->dmr_pgsz : MMU_PAGESIZE)
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate #define	DEMAP_RANGE_CONTINUE(dmrp, addr, endaddr) \
16880Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
16890Sstevel@tonic-gate 	if ((dmrp)->dmr_bitvec != 0 && (dmrp)->dmr_endaddr != (addr)) \
16900Sstevel@tonic-gate 		sfmmu_tlb_range_demap(dmrp); \
16910Sstevel@tonic-gate 	(dmrp)->dmr_endaddr = (endaddr); \
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate #define	DEMAP_RANGE_FLUSH(dmrp) \
16950Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
16960Sstevel@tonic-gate 		if ((dmrp)->dmr_bitvec != 0) \
16970Sstevel@tonic-gate 			sfmmu_tlb_range_demap(dmrp); \
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 
17000Sstevel@tonic-gate #define	DEMAP_RANGE_MARKPG(dmrp, addr) \
17010Sstevel@tonic-gate 	if ((dmrp) != NULL) { \
17020Sstevel@tonic-gate 		if ((dmrp)->dmr_bitvec == 0) { \
17030Sstevel@tonic-gate 			(dmrp)->dmr_addr = (addr); \
17040Sstevel@tonic-gate 			(dmrp)->dmr_bit = 1; \
17050Sstevel@tonic-gate 		} \
17060Sstevel@tonic-gate 		(dmrp)->dmr_bitvec |= (dmrp)->dmr_bit; \
17070Sstevel@tonic-gate 	}
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate #define	DEMAP_RANGE_NEXTPG(dmrp) \
17100Sstevel@tonic-gate 	if ((dmrp) != NULL && (dmrp)->dmr_bitvec != 0) { \
17110Sstevel@tonic-gate 		if ((dmrp)->dmr_bit & (dmrp)->dmr_maxbit) { \
17120Sstevel@tonic-gate 			sfmmu_tlb_range_demap(dmrp); \
17130Sstevel@tonic-gate 		} else { \
17140Sstevel@tonic-gate 			(dmrp)->dmr_bit <<= 1; \
17150Sstevel@tonic-gate 		} \
17160Sstevel@tonic-gate 	}
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate /*
17190Sstevel@tonic-gate  * TSB related structures
17200Sstevel@tonic-gate  *
17210Sstevel@tonic-gate  * The TSB is made up of tte entries.  Both the tag and data are present
17220Sstevel@tonic-gate  * in the TSB.  The TSB locking is managed as follows:
17230Sstevel@tonic-gate  * A software bit in the tsb tag is used to indicate that entry is locked.
17240Sstevel@tonic-gate  * If a cpu servicing a tsb miss reads a locked entry the tag compare will
17250Sstevel@tonic-gate  * fail forcing the cpu to go to the hat hash for the translation.
17260Sstevel@tonic-gate  * The cpu who holds the lock can then modify the data side, and the tag side.
17270Sstevel@tonic-gate  * The last write should be to the word containing the lock bit which will
17280Sstevel@tonic-gate  * clear the lock and allow the tsb entry to be read.  It is assumed that all
17290Sstevel@tonic-gate  * cpus reading the tsb will do so with atomic 128-bit loads.  An atomic 128
17300Sstevel@tonic-gate  * bit load is required to prevent the following from happening:
17310Sstevel@tonic-gate  *
17320Sstevel@tonic-gate  * cpu 0			cpu 1			comments
17330Sstevel@tonic-gate  *
17340Sstevel@tonic-gate  * ldx tag						tag unlocked
17350Sstevel@tonic-gate  *				ldstub lock		set lock
17360Sstevel@tonic-gate  *				stx data
17370Sstevel@tonic-gate  *				stx tag			unlock
17380Sstevel@tonic-gate  * ldx tag						incorrect tte!!!
17390Sstevel@tonic-gate  *
17400Sstevel@tonic-gate  * The software also maintains a bit in the tag to indicate an invalid
17410Sstevel@tonic-gate  * tsb entry.  The purpose of this bit is to allow the tsb invalidate code
17420Sstevel@tonic-gate  * to invalidate a tsb entry with a single cas.  See code for details.
17430Sstevel@tonic-gate  */
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate union tsb_tag {
17460Sstevel@tonic-gate 	struct {
17470Sstevel@tonic-gate 		uint32_t	tag_res0:16;	/* reserved - context area */
17480Sstevel@tonic-gate 		uint32_t	tag_inv:1;	/* sw - invalid tsb entry */
17490Sstevel@tonic-gate 		uint32_t	tag_lock:1;	/* sw - locked tsb entry */
17500Sstevel@tonic-gate 		uint32_t	tag_res1:4;	/* reserved */
17510Sstevel@tonic-gate 		uint32_t	tag_va_hi:10;	/* va[63:54] */
17520Sstevel@tonic-gate 		uint32_t	tag_va_lo;	/* va[53:22] */
17530Sstevel@tonic-gate 	} tagbits;
17540Sstevel@tonic-gate 	struct tsb_tagints {
17550Sstevel@tonic-gate 		uint32_t	inthi;
17560Sstevel@tonic-gate 		uint32_t	intlo;
17570Sstevel@tonic-gate 	} tagints;
17580Sstevel@tonic-gate };
17590Sstevel@tonic-gate #define	tag_invalid		tagbits.tag_inv
17600Sstevel@tonic-gate #define	tag_locked		tagbits.tag_lock
17610Sstevel@tonic-gate #define	tag_vahi		tagbits.tag_va_hi
17620Sstevel@tonic-gate #define	tag_valo		tagbits.tag_va_lo
17630Sstevel@tonic-gate #define	tag_inthi		tagints.inthi
17640Sstevel@tonic-gate #define	tag_intlo		tagints.intlo
17650Sstevel@tonic-gate 
17660Sstevel@tonic-gate struct tsbe {
17670Sstevel@tonic-gate 	union tsb_tag	tte_tag;
17680Sstevel@tonic-gate 	tte_t		tte_data;
17690Sstevel@tonic-gate };
17700Sstevel@tonic-gate 
17710Sstevel@tonic-gate /*
17720Sstevel@tonic-gate  * A per cpu struct is kept that duplicates some info
17730Sstevel@tonic-gate  * used by the tl>0 tsb miss handlers plus it provides
17740Sstevel@tonic-gate  * a scratch area.  Its purpose is to minimize cache misses
17750Sstevel@tonic-gate  * in the tsb miss handler and is 128 bytes (2 e$ lines).
17760Sstevel@tonic-gate  *
17770Sstevel@tonic-gate  * There should be one allocated per cpu in nucleus memory
17780Sstevel@tonic-gate  * and should be aligned on an ecache line boundary.
17790Sstevel@tonic-gate  */
17800Sstevel@tonic-gate struct tsbmiss {
17810Sstevel@tonic-gate 	sfmmu_t			*ksfmmup;	/* kernel hat id */
17820Sstevel@tonic-gate 	sfmmu_t			*usfmmup;	/* user hat id */
17834528Spaulsan 	sf_srd_t		*usrdp;		/* user's SRD hat id */
17840Sstevel@tonic-gate 	struct tsbe		*tsbptr;	/* hardware computed ptr */
17850Sstevel@tonic-gate 	struct tsbe		*tsbptr4m;	/* hardware computed ptr */
17864528Spaulsan 	struct tsbe		*tsbscdptr;	/* hardware computed ptr */
17874528Spaulsan 	struct tsbe		*tsbscdptr4m;	/* hardware computed ptr */
17880Sstevel@tonic-gate 	uint64_t		ismblkpa;
17890Sstevel@tonic-gate 	struct hmehash_bucket	*khashstart;
17900Sstevel@tonic-gate 	struct hmehash_bucket	*uhashstart;
17910Sstevel@tonic-gate 	uint_t			khashsz;
17920Sstevel@tonic-gate 	uint_t			uhashsz;
17930Sstevel@tonic-gate 	uint16_t 		dcache_line_mask; /* used to flush dcache */
17944528Spaulsan 	uchar_t			uhat_tteflags;	/* private page sizes */
17954528Spaulsan 	uchar_t			uhat_rtteflags;	/* SHME pagesizes */
17960Sstevel@tonic-gate 	uint32_t		utsb_misses;
17970Sstevel@tonic-gate 	uint32_t		ktsb_misses;
17980Sstevel@tonic-gate 	uint16_t		uprot_traps;
17990Sstevel@tonic-gate 	uint16_t		kprot_traps;
18000Sstevel@tonic-gate 	/*
18010Sstevel@tonic-gate 	 * scratch[0] -> TSB_TAGACC
18020Sstevel@tonic-gate 	 * scratch[1] -> TSBMISS_HMEBP
18030Sstevel@tonic-gate 	 * scratch[2] -> TSBMISS_HATID
18040Sstevel@tonic-gate 	 */
18050Sstevel@tonic-gate 	uintptr_t		scratch[3];
18064528Spaulsan 	ulong_t		shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
18074528Spaulsan 	ulong_t		scd_shmermap[SFMMU_HMERGNMAP_WORDS];	/* 8 bytes */
180810271SJason.Beloro@Sun.COM 	uint8_t		pad[48];			/* pad to 64 bytes */
18090Sstevel@tonic-gate };
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate /*
18120Sstevel@tonic-gate  * A per cpu struct is kept for the use within the tl>0 kpm tsb
18130Sstevel@tonic-gate  * miss handler. Some members are duplicates of common data or
18140Sstevel@tonic-gate  * the physical addresses of common data. A few members are also
18150Sstevel@tonic-gate  * written by the tl>0 kpm tsb miss handler. Its purpose is to
18160Sstevel@tonic-gate  * minimize cache misses in the kpm tsb miss handler and occupies
18170Sstevel@tonic-gate  * one ecache line. There should be one allocated per cpu in
18180Sstevel@tonic-gate  * nucleus memory and it should be aligned on an ecache line
18190Sstevel@tonic-gate  * boundary. It is not merged w/ struct tsbmiss since there is
18200Sstevel@tonic-gate  * not much to share and the tsbmiss pathes are different, so
18210Sstevel@tonic-gate  * a kpm tlbmiss/tsbmiss only touches one cacheline, except for
18220Sstevel@tonic-gate  * (DEBUG || SFMMU_STAT_GATHER) where the dtlb_misses counter
18230Sstevel@tonic-gate  * of struct tsbmiss is used on every dtlb miss.
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate struct kpmtsbm {
18260Sstevel@tonic-gate 	caddr_t		vbase;		/* start of address kpm range */
18270Sstevel@tonic-gate 	caddr_t		vend;		/* end of address kpm range */
18280Sstevel@tonic-gate 	uchar_t		flags;		/* flags needed in TL tsbmiss handler */
18290Sstevel@tonic-gate 	uchar_t		sz_shift;	/* for single kpm window */
18300Sstevel@tonic-gate 	uchar_t		kpmp_shift;	/* hash lock shift */
18310Sstevel@tonic-gate 	uchar_t		kpmp2pshft;	/* kpm page to page shift */
18320Sstevel@tonic-gate 	uint_t		kpmp_table_sz;	/* size of kpmp_table or kpmp_stable */
18330Sstevel@tonic-gate 	uint64_t	kpmp_tablepa;	/* paddr of kpmp_table or kpmp_stable */
18340Sstevel@tonic-gate 	uint64_t	msegphashpa;	/* paddr of memseg_phash */
18350Sstevel@tonic-gate 	struct tsbe	*tsbptr;	/* saved ktsb pointer */
18360Sstevel@tonic-gate 	uint_t		kpm_dtlb_misses; /* kpm tlbmiss counter */
18370Sstevel@tonic-gate 	uint_t		kpm_tsb_misses;	/* kpm tsbmiss counter */
18380Sstevel@tonic-gate 	uintptr_t	pad[1];
18390Sstevel@tonic-gate };
18400Sstevel@tonic-gate 
18414528Spaulsan extern size_t	tsb_slab_size;
18424528Spaulsan extern uint_t	tsb_slab_shift;
18434528Spaulsan extern size_t	tsb_slab_mask;
18440Sstevel@tonic-gate 
18450Sstevel@tonic-gate #endif /* !_ASM */
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate /*
18480Sstevel@tonic-gate  * Flags for TL kpm tsbmiss handler
18490Sstevel@tonic-gate  */
18500Sstevel@tonic-gate #define	KPMTSBM_ENABLE_FLAG	0x01	/* bit copy of kpm_enable */
18510Sstevel@tonic-gate #define	KPMTSBM_TLTSBM_FLAG	0x02	/* use TL tsbmiss handler */
18520Sstevel@tonic-gate #define	KPMTSBM_TSBPHYS_FLAG	0x04	/* use ASI_MEM for TSB update */
18530Sstevel@tonic-gate 
18540Sstevel@tonic-gate /*
18550Sstevel@tonic-gate  * The TSB
18560Sstevel@tonic-gate  * All TSB sizes supported by the hardware are now supported (8K - 1M).
18570Sstevel@tonic-gate  * For kernel TSBs we may go beyond the hardware supported sizes and support
18580Sstevel@tonic-gate  * larger TSBs via software.
18590Sstevel@tonic-gate  * All TTE sizes are supported in the TSB; the manner in which this is
18600Sstevel@tonic-gate  * done is cpu dependent.
18610Sstevel@tonic-gate  */
18620Sstevel@tonic-gate #define	TSB_MIN_SZCODE		TSB_8K_SZCODE	/* min. supported TSB size */
18630Sstevel@tonic-gate #define	TSB_MIN_OFFSET_MASK	(TSB_OFFSET_MASK(TSB_MIN_SZCODE))
18640Sstevel@tonic-gate 
18654528Spaulsan #ifdef sun4v
18664528Spaulsan #define	UTSB_MAX_SZCODE		TSB_256M_SZCODE /* max. supported TSB size */
18674528Spaulsan #else /* sun4u */
18684528Spaulsan #define	UTSB_MAX_SZCODE		TSB_1M_SZCODE	/* max. supported TSB size */
18694528Spaulsan #endif /* sun4v */
18704528Spaulsan 
18710Sstevel@tonic-gate #define	UTSB_MAX_OFFSET_MASK	(TSB_OFFSET_MASK(UTSB_MAX_SZCODE))
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate #define	TSB_FREEMEM_MIN		0x1000		/* 32 mb */
18740Sstevel@tonic-gate #define	TSB_FREEMEM_LARGE	0x10000		/* 512 mb */
18750Sstevel@tonic-gate #define	TSB_8K_SZCODE		0		/* 512 entries */
18760Sstevel@tonic-gate #define	TSB_16K_SZCODE		1		/* 1k entries */
18770Sstevel@tonic-gate #define	TSB_32K_SZCODE		2		/* 2k entries */
18780Sstevel@tonic-gate #define	TSB_64K_SZCODE		3		/* 4k entries */
18790Sstevel@tonic-gate #define	TSB_128K_SZCODE		4		/* 8k entries */
18800Sstevel@tonic-gate #define	TSB_256K_SZCODE		5		/* 16k entries */
18810Sstevel@tonic-gate #define	TSB_512K_SZCODE		6		/* 32k entries */
18820Sstevel@tonic-gate #define	TSB_1M_SZCODE		7		/* 64k entries */
18830Sstevel@tonic-gate #define	TSB_2M_SZCODE		8		/* 128k entries */
18840Sstevel@tonic-gate #define	TSB_4M_SZCODE		9		/* 256k entries */
18854528Spaulsan #define	TSB_8M_SZCODE		10		/* 512k entries */
18864528Spaulsan #define	TSB_16M_SZCODE		11		/* 1M entries */
18874528Spaulsan #define	TSB_32M_SZCODE		12		/* 2M entries */
18884528Spaulsan #define	TSB_64M_SZCODE		13		/* 4M entries */
18894528Spaulsan #define	TSB_128M_SZCODE		14		/* 8M entries */
18904528Spaulsan #define	TSB_256M_SZCODE		15		/* 16M entries */
18910Sstevel@tonic-gate #define	TSB_ENTRY_SHIFT		4	/* each entry = 128 bits = 16 bytes */
18920Sstevel@tonic-gate #define	TSB_ENTRY_SIZE		(1 << 4)
18930Sstevel@tonic-gate #define	TSB_START_SIZE		9
18940Sstevel@tonic-gate #define	TSB_ENTRIES(tsbsz)	(1 << (TSB_START_SIZE + tsbsz))
18950Sstevel@tonic-gate #define	TSB_BYTES(tsbsz)	(TSB_ENTRIES(tsbsz) << TSB_ENTRY_SHIFT)
18960Sstevel@tonic-gate #define	TSB_OFFSET_MASK(tsbsz)	(TSB_ENTRIES(tsbsz) - 1)
18970Sstevel@tonic-gate #define	TSB_BASEADDR_MASK	((1 << 12) - 1)
18980Sstevel@tonic-gate 
18990Sstevel@tonic-gate /*
19000Sstevel@tonic-gate  * sun4u platforms
19010Sstevel@tonic-gate  * ---------------
19020Sstevel@tonic-gate  * We now support two user TSBs with one TSB base register.
19030Sstevel@tonic-gate  * Hence the TSB base register is split up as follows:
19040Sstevel@tonic-gate  *
19050Sstevel@tonic-gate  * When only one TSB present:
19060Sstevel@tonic-gate  *   [63  62..42  41..13  12..4  3..0]
19070Sstevel@tonic-gate  *     ^   ^       ^       ^     ^
19080Sstevel@tonic-gate  *     |   |       |       |     |
19090Sstevel@tonic-gate  *     |   |       |       |     |_ TSB size code
19100Sstevel@tonic-gate  *     |   |       |       |
19110Sstevel@tonic-gate  *     |   |       |       |_ Reserved 0
19120Sstevel@tonic-gate  *     |   |       |
19130Sstevel@tonic-gate  *     |   |       |_ TSB VA[41..13]
19140Sstevel@tonic-gate  *     |   |
19150Sstevel@tonic-gate  *     |   |_ VA hole (Spitfire), zeros (Cheetah and beyond)
19160Sstevel@tonic-gate  *     |
19170Sstevel@tonic-gate  *     |_ 0
19180Sstevel@tonic-gate  *
19190Sstevel@tonic-gate  * When second TSB present:
19200Sstevel@tonic-gate  *   [63  62..42  41..33  32..29  28..22  21..13  12..4  3..0]
19210Sstevel@tonic-gate  *     ^   ^       ^       ^       ^       ^       ^     ^
19220Sstevel@tonic-gate  *     |   |       |       |       |       |       |     |
19230Sstevel@tonic-gate  *     |   |       |       |       |       |       |     |_ First TSB size code
19240Sstevel@tonic-gate  *     |   |       |       |       |       |       |
19250Sstevel@tonic-gate  *     |   |       |       |       |       |       |_ Reserved 0
19260Sstevel@tonic-gate  *     |   |       |       |       |       |
19270Sstevel@tonic-gate  *     |   |       |       |       |       |_ First TSB's VA[21..13]
19280Sstevel@tonic-gate  *     |   |       |       |       |
19290Sstevel@tonic-gate  *     |   |       |       |       |_ Reserved for future use
19300Sstevel@tonic-gate  *     |   |       |       |
19310Sstevel@tonic-gate  *     |   |       |       |_ Second TSB's size code
19320Sstevel@tonic-gate  *     |   |       |
19330Sstevel@tonic-gate  *     |   |       |_ Second TSB's VA[21..13]
19340Sstevel@tonic-gate  *     |   |
19350Sstevel@tonic-gate  *     |   |_ VA hole (Spitfire) / ones (Cheetah and beyond)
19360Sstevel@tonic-gate  *     |
19370Sstevel@tonic-gate  *     |_ 1
19380Sstevel@tonic-gate  *
19390Sstevel@tonic-gate  * Note that since we store 21..13 of each TSB's VA, TSBs and their slabs
19400Sstevel@tonic-gate  * may be up to 4M in size.  For now, only hardware supported TSB sizes
19410Sstevel@tonic-gate  * are supported, though the slabs are usually 4M in size.
19420Sstevel@tonic-gate  *
19431772Sjl139090  * sun4u platforms that define UTSB_PHYS use physical addressing to access
19441772Sjl139090  * the user TSBs at TL>0.  The first user TSB base is in the MMU I/D TSB Base
19451772Sjl139090  * registers.  The second TSB base uses a dedicated scratchpad register which
19466127Ssm142603  * requires a definition of SCRATCHPAD_UTSBREG2 in mach_sfmmu.h.  The layout for
19471772Sjl139090  * both registers is equivalent to sun4v below, except the TSB PA range is
19481772Sjl139090  * [46..13] for sun4u.
19491772Sjl139090  *
19500Sstevel@tonic-gate  * sun4v platforms
19510Sstevel@tonic-gate  * ---------------
19520Sstevel@tonic-gate  * On sun4v platforms, we use two dedicated scratchpad registers as pseudo
19530Sstevel@tonic-gate  * hardware TSB base registers to hold up to two different user TSBs.
19540Sstevel@tonic-gate  *
19550Sstevel@tonic-gate  * Each register contains TSB's physical base and size code information
19560Sstevel@tonic-gate  * as follows:
19570Sstevel@tonic-gate  *
19580Sstevel@tonic-gate  *   [63..56  55..13  12..4  3..0]
19590Sstevel@tonic-gate  *      ^       ^       ^     ^
19600Sstevel@tonic-gate  *      |       |       |     |
19610Sstevel@tonic-gate  *      |       |       |     |_ TSB size code
19620Sstevel@tonic-gate  *      |       |       |
19630Sstevel@tonic-gate  *      |       |       |_ Reserved 0
19640Sstevel@tonic-gate  *      |       |
19650Sstevel@tonic-gate  *      |       |_ TSB PA[55..13]
19660Sstevel@tonic-gate  *      |
19670Sstevel@tonic-gate  *      |
19680Sstevel@tonic-gate  *      |
19690Sstevel@tonic-gate  *      |_ 0 for valid TSB
19700Sstevel@tonic-gate  *
19710Sstevel@tonic-gate  * Absence of a user TSB (primarily the second user TSB) is indicated by
19720Sstevel@tonic-gate  * storing a negative value in the TSB base register. This allows us to
19730Sstevel@tonic-gate  * check for presence of a user TSB by simply checking bit# 63.
19740Sstevel@tonic-gate  */
19750Sstevel@tonic-gate #define	TSBREG_MSB_SHIFT	32		/* set upper bits */
19760Sstevel@tonic-gate #define	TSBREG_MSB_CONST	0xfffff800	/* set bits 63..43 */
19770Sstevel@tonic-gate #define	TSBREG_FIRTSB_SHIFT	42		/* to clear bits 63:22 */
19780Sstevel@tonic-gate #define	TSBREG_SECTSB_MKSHIFT	20		/* 21:13 --> 41:33 */
19790Sstevel@tonic-gate #define	TSBREG_SECTSB_LSHIFT	22		/* to clear bits 63:42 */
19800Sstevel@tonic-gate #define	TSBREG_SECTSB_RSHIFT	(TSBREG_SECTSB_MKSHIFT + TSBREG_SECTSB_LSHIFT)
19810Sstevel@tonic-gate 						/* sectsb va -> bits 21:13 */
19820Sstevel@tonic-gate 						/* after clearing upper bits */
19830Sstevel@tonic-gate #define	TSBREG_SECSZ_SHIFT	29		/* to get sectsb szc to 3:0 */
19840Sstevel@tonic-gate #define	TSBREG_VAMASK_SHIFT	13		/* set up VA mask */
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate #define	BIGKTSB_SZ_MASK		0xf
19870Sstevel@tonic-gate #define	TSB_SOFTSZ_MASK		BIGKTSB_SZ_MASK
19880Sstevel@tonic-gate #define	MIN_BIGKTSB_SZCODE	9	/* 256k entries */
19890Sstevel@tonic-gate #define	MAX_BIGKTSB_SZCODE	11	/* 1024k entries */
19900Sstevel@tonic-gate #define	MAX_BIGKTSB_TTES	(TSB_BYTES(MAX_BIGKTSB_SZCODE) / MMU_PAGESIZE4M)
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate #define	TAG_VALO_SHIFT		22		/* tag's va are bits 63-22 */
19930Sstevel@tonic-gate /*
19940Sstevel@tonic-gate  * sw bits used on tsb_tag - bit masks used only in assembly
19950Sstevel@tonic-gate  * use only a sethi for these fields.
19960Sstevel@tonic-gate  */
19970Sstevel@tonic-gate #define	TSBTAG_INVALID	0x00008000		/* tsb_tag.tag_invalid */
19980Sstevel@tonic-gate #define	TSBTAG_LOCKED	0x00004000		/* tsb_tag.tag_locked */
19990Sstevel@tonic-gate 
20000Sstevel@tonic-gate #ifdef	_ASM
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate /*
20030Sstevel@tonic-gate  * Marker to indicate that this instruction will be hot patched at runtime
20040Sstevel@tonic-gate  * to some other value.
20050Sstevel@tonic-gate  * This value must be zero since it fills in the imm bits of the target
20060Sstevel@tonic-gate  * instructions to be patched
20070Sstevel@tonic-gate  */
20080Sstevel@tonic-gate #define	RUNTIME_PATCH	(0)
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate /*
20110Sstevel@tonic-gate  * V9 defines nop instruction as the following, which we use
20120Sstevel@tonic-gate  * at runtime to nullify some instructions we don't want to
20130Sstevel@tonic-gate  * execute in the trap handlers on certain platforms.
20140Sstevel@tonic-gate  */
20150Sstevel@tonic-gate #define	MAKE_NOP_INSTR(reg)	\
20160Sstevel@tonic-gate 	sethi	%hi(0x1000000), reg
20170Sstevel@tonic-gate 
20180Sstevel@tonic-gate /*
20194528Spaulsan  * This macro constructs a SPARC V9 "jmpl <source reg>, %g0"
20204528Spaulsan  * instruction, with the source register specified by the jump_reg_number.
20214528Spaulsan  * The jmp opcode [24:19] = 11 1000 and source register is bits [18:14].
20224528Spaulsan  * The instruction is returned in reg. The macro is used to patch in a jmpl
20234528Spaulsan  * instruction at runtime.
20244528Spaulsan  */
20254528Spaulsan #define	MAKE_JMP_INSTR(jump_reg_number, reg, tmp)	\
20264528Spaulsan 	sethi	%hi(0x81c00000), reg;			\
20274528Spaulsan 	mov	jump_reg_number, tmp;			\
20284528Spaulsan 	sll	tmp, 14, tmp;				\
20294528Spaulsan 	or	reg, tmp, reg
20304528Spaulsan 
20314528Spaulsan /*
20322241Shuah  * Macro to get hat per-MMU cnum on this CPU.
20332241Shuah  * sfmmu - In, pass in "sfmmup" from the caller.
20342241Shuah  * cnum	- Out, return 'cnum' to the caller
20352241Shuah  * scr	- scratch
20362241Shuah  */
20372241Shuah #define	SFMMU_CPU_CNUM(sfmmu, cnum, scr)				      \
20382241Shuah 	CPU_ADDR(scr, cnum);	/* scr = load CPU struct addr */	      \
20392241Shuah 	ld	[scr + CPU_MMU_IDX], cnum;	/* cnum = mmuid */	      \
20402241Shuah 	add	sfmmu, SFMMU_CTXS, scr;	/* scr = sfmmup->sfmmu_ctxs[] */      \
20412241Shuah 	sllx    cnum, SFMMU_MMU_CTX_SHIFT, cnum;			      \
20422241Shuah 	add	scr, cnum, scr;		/* scr = sfmmup->sfmmu_ctxs[id] */    \
20432241Shuah 	ldx	[scr + SFMMU_MMU_GC_NUM], scr;	/* sfmmu_ctxs[id].gcnum */    \
20442241Shuah 	sllx    scr, SFMMU_MMU_CNUM_LSHIFT, scr;			      \
20452241Shuah 	srlx    scr, SFMMU_MMU_CNUM_LSHIFT, cnum;	/* cnum = sfmmu cnum */
20462241Shuah 
20472241Shuah /*
20482241Shuah  * Macro to get hat gnum & cnum assocaited with sfmmu_ctx[mmuid] entry
20492241Shuah  * entry - In,  pass in (&sfmmu_ctxs[mmuid] - SFMMU_CTXS) from the caller.
20502241Shuah  * gnum - Out, return sfmmu gnum
20512241Shuah  * cnum - Out, return sfmmu cnum
20522241Shuah  * reg	- scratch
20532241Shuah  */
20542241Shuah #define	SFMMU_MMUID_GNUM_CNUM(entry, gnum, cnum, reg)			     \
20552241Shuah 	ldx	[entry + SFMMU_CTXS], reg;  /* reg = sfmmu (gnum | cnum) */  \
20562241Shuah 	srlx	reg, SFMMU_MMU_GNUM_RSHIFT, gnum;    /* gnum = sfmmu gnum */ \
20572241Shuah 	sllx	reg, SFMMU_MMU_CNUM_LSHIFT, cnum;			     \
20582241Shuah 	srlx	cnum, SFMMU_MMU_CNUM_LSHIFT, cnum;   /* cnum = sfmmu cnum */
20592241Shuah 
20602241Shuah /*
20610Sstevel@tonic-gate  * Macro to get this CPU's tsbmiss area.
20620Sstevel@tonic-gate  */
20630Sstevel@tonic-gate #define	CPU_TSBMISS_AREA(tsbmiss, tmp1)					\
20640Sstevel@tonic-gate 	CPU_INDEX(tmp1, tsbmiss);		/* tmp1 = cpu idx */	\
20650Sstevel@tonic-gate 	sethi	%hi(tsbmiss_area), tsbmiss;	/* tsbmiss base ptr */	\
20664528Spaulsan 	mulx    tmp1, TSBMISS_SIZE, tmp1;	/* byte offset */	\
20670Sstevel@tonic-gate 	or	tsbmiss, %lo(tsbmiss_area), tsbmiss;			\
20680Sstevel@tonic-gate 	add	tsbmiss, tmp1, tsbmiss		/* tsbmiss area of CPU */
20690Sstevel@tonic-gate 
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate /*
20720Sstevel@tonic-gate  * Macro to set kernel context + page size codes in DMMU primary context
20730Sstevel@tonic-gate  * register. It is only necessary for sun4u because sun4v does not need
20740Sstevel@tonic-gate  * page size codes
20750Sstevel@tonic-gate  */
20760Sstevel@tonic-gate #ifdef sun4v
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3)
20790Sstevel@tonic-gate 
20800Sstevel@tonic-gate #else
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate #define	SET_KCONTEXTREG(reg0, reg1, reg2, reg3, reg4, label1, label2, label3) \
20830Sstevel@tonic-gate 	sethi	%hi(kcontextreg), reg0;					\
20840Sstevel@tonic-gate 	ldx	[reg0 + %lo(kcontextreg)], reg0;			\
20850Sstevel@tonic-gate 	mov	MMU_PCONTEXT, reg1;					\
20860Sstevel@tonic-gate 	ldxa	[reg1]ASI_MMU_CTX, reg2;				\
20870Sstevel@tonic-gate 	xor	reg0, reg2, reg2;					\
20880Sstevel@tonic-gate 	brz	reg2, label3;						\
20890Sstevel@tonic-gate 	srlx	reg2, CTXREG_NEXT_SHIFT, reg2;				\
20900Sstevel@tonic-gate 	rdpr	%pstate, reg3;		/* disable interrupts */	\
20910Sstevel@tonic-gate 	btst	PSTATE_IE, reg3;					\
20920Sstevel@tonic-gate /*CSTYLED*/								\
20930Sstevel@tonic-gate 	bnz,a,pt %icc, label1;						\
20940Sstevel@tonic-gate 	wrpr	reg3, PSTATE_IE, %pstate;				\
20950Sstevel@tonic-gate /*CSTYLED*/								\
20960Sstevel@tonic-gate label1:;								\
20970Sstevel@tonic-gate 	brz	reg2, label2;	   /* need demap if N_pgsz0/1 change */	\
20980Sstevel@tonic-gate 	sethi	%hi(FLUSH_ADDR), reg4;					\
20990Sstevel@tonic-gate 	mov	DEMAP_ALL_TYPE, reg2;					\
21000Sstevel@tonic-gate 	stxa	%g0, [reg2]ASI_DTLB_DEMAP;				\
21010Sstevel@tonic-gate 	stxa	%g0, [reg2]ASI_ITLB_DEMAP;				\
21020Sstevel@tonic-gate /*CSTYLED*/								\
21030Sstevel@tonic-gate label2:;								\
21040Sstevel@tonic-gate 	stxa	reg0, [reg1]ASI_MMU_CTX;				\
21050Sstevel@tonic-gate 	flush	reg4;							\
21060Sstevel@tonic-gate 	btst	PSTATE_IE, reg3;					\
21070Sstevel@tonic-gate /*CSTYLED*/								\
21080Sstevel@tonic-gate 	bnz,a,pt %icc, label3;						\
21090Sstevel@tonic-gate 	wrpr	%g0, reg3, %pstate;	/* restore interrupt state */	\
21100Sstevel@tonic-gate label3:;
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate #endif
21130Sstevel@tonic-gate 
21142241Shuah /*
21152241Shuah  * Macro to setup arguments with kernel sfmmup context + page size before
21162241Shuah  * calling sfmmu_setctx_sec()
21172241Shuah  */
21182241Shuah #ifdef sun4v
21192241Shuah #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
21202241Shuah 	set	KCONTEXT, arg0;					\
21212241Shuah 	set	0, arg1;
21222241Shuah #else
21232241Shuah #define	SET_KAS_CTXSEC_ARGS(sfmmup, arg0, arg1)			\
21242241Shuah 	ldub	[sfmmup + SFMMU_CEXT], arg1;			\
21252241Shuah 	set	KCONTEXT, arg0;					\
21262241Shuah 	sll	arg1, CTXREG_EXT_SHIFT, arg1;
21272241Shuah #endif
21282241Shuah 
21292241Shuah #define	PANIC_IF_INTR_DISABLED_PSTR(pstatereg, label, scr)	       	\
21302241Shuah 	andcc	pstatereg, PSTATE_IE, %g0;	/* panic if intrs */	\
21312241Shuah /*CSTYLED*/								\
21322241Shuah 	bnz,pt	%icc, label;			/* already disabled */	\
21332241Shuah 	nop;								\
21342241Shuah 									\
21352241Shuah 	sethi	%hi(panicstr), scr;					\
21362241Shuah 	ldx	[scr + %lo(panicstr)], scr;				\
21372241Shuah 	tst	scr;							\
21382241Shuah /*CSTYLED*/								\
21392241Shuah 	bnz,pt	%xcc, label;						\
21402241Shuah 	nop;								\
21412241Shuah 									\
21422241Shuah 	save	%sp, -SA(MINFRAME), %sp;				\
21432241Shuah 	sethi	%hi(sfmmu_panic1), %o0;					\
21442241Shuah 	call	panic;							\
21452241Shuah 	or	%o0, %lo(sfmmu_panic1), %o0;				\
21462241Shuah /*CSTYLED*/								\
21472241Shuah label:
21482241Shuah 
21492241Shuah #define	PANIC_IF_INTR_ENABLED_PSTR(label, scr)				\
21502241Shuah 	/*								\
21512241Shuah 	 * The caller must have disabled interrupts.			\
21522241Shuah 	 * If interrupts are not disabled, panic			\
21532241Shuah 	 */								\
21542241Shuah 	rdpr	%pstate, scr;						\
21552241Shuah 	andcc	scr, PSTATE_IE, %g0;					\
21562241Shuah /*CSTYLED*/								\
21572241Shuah 	bz,pt	%icc, label;						\
21582241Shuah 	nop;								\
21592241Shuah 									\
21602241Shuah 	sethi	%hi(panicstr), scr;					\
21612241Shuah 	ldx	[scr + %lo(panicstr)], scr;				\
21622241Shuah 	tst	scr;							\
21632241Shuah /*CSTYLED*/								\
21642241Shuah 	bnz,pt	%xcc, label;						\
21652241Shuah 	nop;								\
21662241Shuah 									\
21672241Shuah 	sethi	%hi(sfmmu_panic6), %o0;					\
21682241Shuah 	call	panic;							\
21692241Shuah 	or	%o0, %lo(sfmmu_panic6), %o0;				\
21702241Shuah /*CSTYLED*/								\
21712241Shuah label:
21722241Shuah 
21730Sstevel@tonic-gate #endif	/* _ASM */
21740Sstevel@tonic-gate 
21750Sstevel@tonic-gate #ifndef _ASM
21760Sstevel@tonic-gate 
21772296Sae112802 #ifdef VAC
21780Sstevel@tonic-gate /*
21790Sstevel@tonic-gate  * Page coloring
21800Sstevel@tonic-gate  * The p_vcolor field of the page struct (1 byte) is used to store the
21810Sstevel@tonic-gate  * virtual page color.  This provides for 255 colors.  The value zero is
21820Sstevel@tonic-gate  * used to mean the page has no color - never been mapped or somehow
21830Sstevel@tonic-gate  * purified.
21840Sstevel@tonic-gate  */
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate #define	PP_GET_VCOLOR(pp)	(((pp)->p_vcolor) - 1)
21870Sstevel@tonic-gate #define	PP_NEWPAGE(pp)		(!(pp)->p_vcolor)
21880Sstevel@tonic-gate #define	PP_SET_VCOLOR(pp, color)                                          \
21890Sstevel@tonic-gate 	((pp)->p_vcolor = ((color) + 1))
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate /*
21920Sstevel@tonic-gate  * As mentioned p_vcolor == 0 means there is no color for this page.
21930Sstevel@tonic-gate  * But PP_SET_VCOLOR(pp, color) expects 'color' to be real color minus
21940Sstevel@tonic-gate  * one so we define this constant.
21950Sstevel@tonic-gate  */
21960Sstevel@tonic-gate #define	NO_VCOLOR	(-1)
21970Sstevel@tonic-gate 
21980Sstevel@tonic-gate #define	addr_to_vcolor(addr) \
2199567Sdmick 	(((uint_t)(uintptr_t)(addr) >> MMU_PAGESHIFT) & vac_colors_mask)
22002296Sae112802 #else	/* VAC */
22012296Sae112802 #define	addr_to_vcolor(addr)	(0)
22022296Sae112802 #endif	/* VAC */
22030Sstevel@tonic-gate 
22040Sstevel@tonic-gate /*
22050Sstevel@tonic-gate  * The field p_index in the psm page structure is for large pages support.
22060Sstevel@tonic-gate  * P_index is a bit-vector of the different mapping sizes that a given page
22070Sstevel@tonic-gate  * is part of. An hme structure for a large mapping is only added in the
22080Sstevel@tonic-gate  * group leader page (first page). All pages covered by a given large mapping
22090Sstevel@tonic-gate  * have the corrosponding mapping bit set in their p_index field. This allows
22100Sstevel@tonic-gate  * us to only store an explicit hme structure in the leading page which
22110Sstevel@tonic-gate  * simplifies the mapping link list management. Furthermore, it provides us
22120Sstevel@tonic-gate  * a fast mechanism for determining the largest mapping a page is part of. For
22130Sstevel@tonic-gate  * exmaple, a page with a 64K and a 4M mappings has a p_index value of 0x0A.
22140Sstevel@tonic-gate  *
22150Sstevel@tonic-gate  * Implementation note: even though the first bit in p_index is reserved
22160Sstevel@tonic-gate  * for 8K mappings, it is NOT USED by the code and SHOULD NOT be set.
22170Sstevel@tonic-gate  * In addition, the upper four bits of the p_index field are used by the
22180Sstevel@tonic-gate  * code as temporaries
22190Sstevel@tonic-gate  */
22200Sstevel@tonic-gate 
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate  * Defines for psm page struct fields and large page support
22230Sstevel@tonic-gate  */
22240Sstevel@tonic-gate #define	SFMMU_INDEX_SHIFT		6
22250Sstevel@tonic-gate #define	SFMMU_INDEX_MASK		((1 << SFMMU_INDEX_SHIFT) - 1)
22260Sstevel@tonic-gate 
22270Sstevel@tonic-gate /* Return the mapping index */
22280Sstevel@tonic-gate #define	PP_MAPINDEX(pp)	((pp)->p_index & SFMMU_INDEX_MASK)
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate /*
22310Sstevel@tonic-gate  * These macros rely on the following property:
22320Sstevel@tonic-gate  * All pages constituting a large page are covered by a virtually
22330Sstevel@tonic-gate  * contiguous set of page_t's.
22340Sstevel@tonic-gate  */
22350Sstevel@tonic-gate 
22360Sstevel@tonic-gate /* Return the leader for this mapping size */
22370Sstevel@tonic-gate #define	PP_GROUPLEADER(pp, sz) \
22380Sstevel@tonic-gate 	(&(pp)[-(int)(pp->p_pagenum & (TTEPAGES(sz)-1))])
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate /* Return the root page for this page based on p_szc */
22410Sstevel@tonic-gate #define	PP_PAGEROOT(pp)	((pp)->p_szc == 0 ? (pp) : \
22420Sstevel@tonic-gate 	PP_GROUPLEADER((pp), (pp)->p_szc))
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate #define	PP_PAGENEXT_N(pp, n)	((pp) + (n))
22450Sstevel@tonic-gate #define	PP_PAGENEXT(pp)		PP_PAGENEXT_N((pp), 1)
22460Sstevel@tonic-gate 
22470Sstevel@tonic-gate #define	PP_PAGEPREV_N(pp, n)	((pp) - (n))
22480Sstevel@tonic-gate #define	PP_PAGEPREV(pp)		PP_PAGEPREV_N((pp), 1)
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate #define	PP_ISMAPPED_LARGE(pp)	(PP_MAPINDEX(pp) != 0)
22510Sstevel@tonic-gate 
22520Sstevel@tonic-gate /* Need function to test the page mappping which takes p_index into account */
22530Sstevel@tonic-gate #define	PP_ISMAPPED(pp)	((pp)->p_mapping || PP_ISMAPPED_LARGE(pp))
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate /*
22560Sstevel@tonic-gate  * Don't call this macro with sz equal to zero. 8K mappings SHOULD NOT
22570Sstevel@tonic-gate  * set p_index field.
22580Sstevel@tonic-gate  */
22590Sstevel@tonic-gate #define	PAGESZ_TO_INDEX(sz)	(1 << (sz))
22600Sstevel@tonic-gate 
22610Sstevel@tonic-gate 
22620Sstevel@tonic-gate /*
22630Sstevel@tonic-gate  * prototypes for hat assembly routines.  Some of these are
22640Sstevel@tonic-gate  * known to machine dependent VM code.
22650Sstevel@tonic-gate  */
22660Sstevel@tonic-gate extern uint64_t sfmmu_make_tsbtag(caddr_t);
22670Sstevel@tonic-gate extern struct tsbe *
22680Sstevel@tonic-gate 		sfmmu_get_tsbe(uint64_t, caddr_t, int, int);
22690Sstevel@tonic-gate extern void	sfmmu_load_tsbe(struct tsbe *, uint64_t, tte_t *, int);
22700Sstevel@tonic-gate extern void	sfmmu_unload_tsbe(struct tsbe *, uint64_t, int);
22710Sstevel@tonic-gate extern void	sfmmu_load_mmustate(sfmmu_t *);
22720Sstevel@tonic-gate extern void	sfmmu_raise_tsb_exception(uint64_t, uint64_t);
22730Sstevel@tonic-gate #ifndef sun4v
22742241Shuah extern void	sfmmu_itlb_ld_kva(caddr_t, tte_t *);
22752241Shuah extern void	sfmmu_dtlb_ld_kva(caddr_t, tte_t *);
22760Sstevel@tonic-gate #endif /* sun4v */
22770Sstevel@tonic-gate extern void	sfmmu_copytte(tte_t *, tte_t *);
22780Sstevel@tonic-gate extern int	sfmmu_modifytte(tte_t *, tte_t *, tte_t *);
22790Sstevel@tonic-gate extern int	sfmmu_modifytte_try(tte_t *, tte_t *, tte_t *);
22800Sstevel@tonic-gate extern pfn_t	sfmmu_ttetopfn(tte_t *, caddr_t);
22812241Shuah extern uint_t	sfmmu_disable_intrs(void);
22822241Shuah extern void	sfmmu_enable_intrs(uint_t);
22830Sstevel@tonic-gate /*
22840Sstevel@tonic-gate  * functions exported to machine dependent VM code
22850Sstevel@tonic-gate  */
22860Sstevel@tonic-gate extern void	sfmmu_patch_ktsb(void);
22871772Sjl139090 #ifndef UTSB_PHYS
22880Sstevel@tonic-gate extern void	sfmmu_patch_utsb(void);
22891772Sjl139090 #endif /* UTSB_PHYS */
22900Sstevel@tonic-gate extern pfn_t	sfmmu_vatopfn(caddr_t, sfmmu_t *, tte_t *);
22910Sstevel@tonic-gate extern void	sfmmu_vatopfn_suspended(caddr_t, sfmmu_t *, tte_t *);
22923351Saguzovsk extern pfn_t	sfmmu_kvaszc2pfn(caddr_t, int);
22930Sstevel@tonic-gate #ifdef	DEBUG
22940Sstevel@tonic-gate extern void	sfmmu_check_kpfn(pfn_t);
22950Sstevel@tonic-gate #else
22960Sstevel@tonic-gate #define		sfmmu_check_kpfn(pfn)	/* disabled */
22970Sstevel@tonic-gate #endif	/* DEBUG */
22980Sstevel@tonic-gate extern void	sfmmu_memtte(tte_t *, pfn_t, uint_t, int);
22990Sstevel@tonic-gate extern void	sfmmu_tteload(struct hat *, tte_t *, caddr_t, page_t *,	uint_t);
23000Sstevel@tonic-gate extern void	sfmmu_tsbmiss_exception(struct regs *, uintptr_t, uint_t);
23010Sstevel@tonic-gate extern void	sfmmu_init_tsbs(void);
23020Sstevel@tonic-gate extern caddr_t  sfmmu_ktsb_alloc(caddr_t);
23030Sstevel@tonic-gate extern int	sfmmu_getctx_pri(void);
23040Sstevel@tonic-gate extern int	sfmmu_getctx_sec(void);
23054528Spaulsan extern void	sfmmu_setctx_sec(uint_t);
23060Sstevel@tonic-gate extern void	sfmmu_inv_tsb(caddr_t, uint_t);
23070Sstevel@tonic-gate extern void	sfmmu_init_ktsbinfo(void);
23080Sstevel@tonic-gate extern int	sfmmu_setup_4lp(void);
23090Sstevel@tonic-gate extern void	sfmmu_patch_mmu_asi(int);
23100Sstevel@tonic-gate extern void	sfmmu_init_nucleus_hblks(caddr_t, size_t, int, int);
23110Sstevel@tonic-gate extern void	sfmmu_cache_flushall(void);
23120Sstevel@tonic-gate extern pgcnt_t  sfmmu_tte_cnt(sfmmu_t *, uint_t);
23130Sstevel@tonic-gate extern void	*sfmmu_tsb_segkmem_alloc(vmem_t *, size_t, int);
23140Sstevel@tonic-gate extern void	sfmmu_tsb_segkmem_free(vmem_t *, void *, size_t);
23152241Shuah extern void	sfmmu_reprog_pgsz_arr(sfmmu_t *, uint8_t *);
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate extern void	hat_kern_setup(void);
23180Sstevel@tonic-gate extern int	hat_page_relocate(page_t **, page_t **, spgcnt_t *);
23190Sstevel@tonic-gate extern int	sfmmu_get_ppvcolor(struct page *);
23200Sstevel@tonic-gate extern int	sfmmu_get_addrvcolor(caddr_t);
23210Sstevel@tonic-gate extern int	sfmmu_hat_lock_held(sfmmu_t *);
23224528Spaulsan extern int	sfmmu_alloc_ctx(sfmmu_t *, int, struct cpu *, int);
23230Sstevel@tonic-gate 
23240Sstevel@tonic-gate /*
23250Sstevel@tonic-gate  * Functions exported to xhat_sfmmu.c
23260Sstevel@tonic-gate  */
23270Sstevel@tonic-gate extern kmutex_t *sfmmu_mlist_enter(page_t *);
23280Sstevel@tonic-gate extern void	sfmmu_mlist_exit(kmutex_t *);
23290Sstevel@tonic-gate extern int	sfmmu_mlist_held(struct page *);
23300Sstevel@tonic-gate extern struct hme_blk *sfmmu_hmetohblk(struct sf_hment *);
23310Sstevel@tonic-gate 
23320Sstevel@tonic-gate /*
23330Sstevel@tonic-gate  * MMU-specific functions optionally imported from the CPU module
23340Sstevel@tonic-gate  */
23357869SSean.McEnroe@Sun.COM #pragma weak mmu_init_scd
23360Sstevel@tonic-gate #pragma weak mmu_large_pages_disabled
23370Sstevel@tonic-gate #pragma weak mmu_set_ctx_page_sizes
23380Sstevel@tonic-gate #pragma weak mmu_check_page_sizes
23390Sstevel@tonic-gate 
23407869SSean.McEnroe@Sun.COM extern void mmu_init_scd(sf_scd_t *);
23412991Ssusans extern uint_t mmu_large_pages_disabled(uint_t);
23420Sstevel@tonic-gate extern void mmu_set_ctx_page_sizes(sfmmu_t *);
23430Sstevel@tonic-gate extern void mmu_check_page_sizes(sfmmu_t *, uint64_t *);
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate extern sfmmu_t 		*ksfmmup;
23460Sstevel@tonic-gate extern caddr_t		ktsb_base;
23470Sstevel@tonic-gate extern uint64_t		ktsb_pbase;
23480Sstevel@tonic-gate extern int		ktsb_sz;
23490Sstevel@tonic-gate extern int		ktsb_szcode;
23500Sstevel@tonic-gate extern caddr_t		ktsb4m_base;
23510Sstevel@tonic-gate extern uint64_t		ktsb4m_pbase;
23520Sstevel@tonic-gate extern int		ktsb4m_sz;
23530Sstevel@tonic-gate extern int		ktsb4m_szcode;
23540Sstevel@tonic-gate extern uint64_t		kpm_tsbbase;
23550Sstevel@tonic-gate extern int		kpm_tsbsz;
23560Sstevel@tonic-gate extern int		ktsb_phys;
23570Sstevel@tonic-gate extern int		enable_bigktsb;
23580Sstevel@tonic-gate #ifndef sun4v
23590Sstevel@tonic-gate extern int		utsb_dtlb_ttenum;
23600Sstevel@tonic-gate extern int		utsb4m_dtlb_ttenum;
23610Sstevel@tonic-gate #endif /* sun4v */
23620Sstevel@tonic-gate extern int		uhmehash_num;
23630Sstevel@tonic-gate extern int		khmehash_num;
23640Sstevel@tonic-gate extern struct hmehash_bucket *uhme_hash;
23650Sstevel@tonic-gate extern struct hmehash_bucket *khme_hash;
23660Sstevel@tonic-gate extern uint_t		hblk_alloc_dynamic;
23670Sstevel@tonic-gate extern struct tsbmiss	tsbmiss_area[NCPU];
23680Sstevel@tonic-gate extern struct kpmtsbm	kpmtsbm_area[NCPU];
23694528Spaulsan 
23700Sstevel@tonic-gate #ifndef sun4v
23710Sstevel@tonic-gate extern int		dtlb_resv_ttenum;
23720Sstevel@tonic-gate extern caddr_t		utsb_vabase;
23730Sstevel@tonic-gate extern caddr_t		utsb4m_vabase;
23740Sstevel@tonic-gate #endif /* sun4v */
23750Sstevel@tonic-gate extern vmem_t		*kmem_tsb_default_arena[];
23760Sstevel@tonic-gate extern int		tsb_lgrp_affinity;
23770Sstevel@tonic-gate 
23782991Ssusans extern uint_t		disable_large_pages;
23792991Ssusans extern uint_t		disable_ism_large_pages;
23802991Ssusans extern uint_t		disable_auto_data_large_pages;
23812991Ssusans extern uint_t		disable_auto_text_large_pages;
23822991Ssusans 
23830Sstevel@tonic-gate /* kpm externals */
23840Sstevel@tonic-gate extern pfn_t		sfmmu_kpm_vatopfn(caddr_t);
23850Sstevel@tonic-gate extern void		sfmmu_kpm_patch_tlbm(void);
23860Sstevel@tonic-gate extern void		sfmmu_kpm_patch_tsbm(void);
238710271SJason.Beloro@Sun.COM extern void		sfmmu_patch_shctx(void);
23880Sstevel@tonic-gate extern void		sfmmu_kpm_load_tsb(caddr_t, tte_t *, int);
23890Sstevel@tonic-gate extern void		sfmmu_kpm_unload_tsb(caddr_t, int);
23900Sstevel@tonic-gate extern void		sfmmu_kpm_tsbmtl(short *, uint_t *, int);
23917393SDonghai.Qiao@Sun.COM extern int		sfmmu_kpm_stsbmtl(uchar_t *, uint_t *, int);
23920Sstevel@tonic-gate extern caddr_t		kpm_vbase;
23930Sstevel@tonic-gate extern size_t		kpm_size;
23940Sstevel@tonic-gate extern struct memseg	*memseg_hash[];
23950Sstevel@tonic-gate extern uint64_t		memseg_phash[];
23960Sstevel@tonic-gate extern kpm_hlk_t	*kpmp_table;
23970Sstevel@tonic-gate extern kpm_shlk_t	*kpmp_stable;
23980Sstevel@tonic-gate extern uint_t		kpmp_table_sz;
23990Sstevel@tonic-gate extern uint_t		kpmp_stable_sz;
24000Sstevel@tonic-gate extern uchar_t		kpmp_shift;
24010Sstevel@tonic-gate 
24020Sstevel@tonic-gate #define	PP_ISMAPPED_KPM(pp)	((pp)->p_kpmref > 0)
24030Sstevel@tonic-gate 
24040Sstevel@tonic-gate #define	IS_KPM_ALIAS_RANGE(vaddr)					\
24050Sstevel@tonic-gate 	(((vaddr) - kpm_vbase) >> (uintptr_t)kpm_size_shift > 0)
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate #endif /* !_ASM */
24080Sstevel@tonic-gate 
24090Sstevel@tonic-gate /* sfmmu_kpm_tsbmtl flags */
24100Sstevel@tonic-gate #define	KPMTSBM_STOP		0
24110Sstevel@tonic-gate #define	KPMTSBM_START		1
24120Sstevel@tonic-gate 
24137393SDonghai.Qiao@Sun.COM /*
24147393SDonghai.Qiao@Sun.COM  * For kpm_smallpages, the state about how a kpm page is mapped and whether
24157393SDonghai.Qiao@Sun.COM  * it is ready to go is indicated by the two 4-bit fields defined in the
24167393SDonghai.Qiao@Sun.COM  * kpm_spage structure as follows:
24177393SDonghai.Qiao@Sun.COM  * kp_mapped_flag bit[0:3] - the page is mapped cacheable or not
24187393SDonghai.Qiao@Sun.COM  * kp_mapped_flag bit[4:7] - the mapping is ready to go or not
24197393SDonghai.Qiao@Sun.COM  * If the bit KPM_MAPPED_GO is on, it indicates that the assembly tsb miss
24207393SDonghai.Qiao@Sun.COM  * handler can drop the mapping in regardless of the caching state of the
24217393SDonghai.Qiao@Sun.COM  * mapping. Otherwise, we will have C handler resolve the VAC conflict no
24227393SDonghai.Qiao@Sun.COM  * matter the page is currently mapped cacheable or non-cacheable.
24237393SDonghai.Qiao@Sun.COM  */
24247393SDonghai.Qiao@Sun.COM #define	KPM_MAPPEDS		0x1	/* small mapping valid, no conflict */
24257393SDonghai.Qiao@Sun.COM #define	KPM_MAPPEDSC		0x2	/* small mapping valid, conflict */
24267393SDonghai.Qiao@Sun.COM #define	KPM_MAPPED_GO		0x10	/* the mapping is ready to go */
24277393SDonghai.Qiao@Sun.COM #define	KPM_MAPPED_MASK		0xf
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate /* Physical memseg address NULL marker */
24300Sstevel@tonic-gate #define	MSEG_NULLPTR_PA		-1
24310Sstevel@tonic-gate 
24320Sstevel@tonic-gate /*
24330Sstevel@tonic-gate  * Memseg hash defines for kpm trap level tsbmiss handler.
24340Sstevel@tonic-gate  * Must be in sync w/ page.h .
24350Sstevel@tonic-gate  */
24360Sstevel@tonic-gate #define	SFMMU_MEM_HASH_SHIFT		0x9
24370Sstevel@tonic-gate #define	SFMMU_N_MEM_SLOTS		0x200
24380Sstevel@tonic-gate #define	SFMMU_MEM_HASH_ENTRY_SHIFT	3
24390Sstevel@tonic-gate 
24400Sstevel@tonic-gate #ifndef	_ASM
24410Sstevel@tonic-gate #if (SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT)
24420Sstevel@tonic-gate #error SFMMU_MEM_HASH_SHIFT != MEM_HASH_SHIFT
24430Sstevel@tonic-gate #endif
24440Sstevel@tonic-gate #if (SFMMU_N_MEM_SLOTS != N_MEM_SLOTS)
24450Sstevel@tonic-gate #error SFMMU_N_MEM_SLOTS != N_MEM_SLOTS
24460Sstevel@tonic-gate #endif
24470Sstevel@tonic-gate 
24480Sstevel@tonic-gate /* Physical memseg address NULL marker */
24490Sstevel@tonic-gate #define	SFMMU_MEMSEG_NULLPTR_PA		-1
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate /*
24520Sstevel@tonic-gate  * Check KCONTEXT to be zero, asm parts depend on that assumption.
24530Sstevel@tonic-gate  */
24540Sstevel@tonic-gate #if (KCONTEXT != 0)
24550Sstevel@tonic-gate #error KCONTEXT != 0
24560Sstevel@tonic-gate #endif
24570Sstevel@tonic-gate #endif	/* !_ASM */
24580Sstevel@tonic-gate 
24590Sstevel@tonic-gate 
24600Sstevel@tonic-gate #endif /* _KERNEL */
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate #ifndef _ASM
24630Sstevel@tonic-gate /*
24640Sstevel@tonic-gate  * ctx, hmeblk, mlistlock and other stats for sfmmu
24650Sstevel@tonic-gate  */
24660Sstevel@tonic-gate struct sfmmu_global_stat {
24670Sstevel@tonic-gate 	int		sf_tsb_exceptions;	/* # of tsb exceptions */
24680Sstevel@tonic-gate 	int		sf_tsb_raise_exception;	/* # tsb exc. w/o TLB flush */
24690Sstevel@tonic-gate 
24700Sstevel@tonic-gate 	int		sf_pagefaults;		/* # of pagefaults */
24710Sstevel@tonic-gate 
24720Sstevel@tonic-gate 	int		sf_uhash_searches;	/* # of user hash searches */
24730Sstevel@tonic-gate 	int		sf_uhash_links;		/* # of user hash links */
24740Sstevel@tonic-gate 	int		sf_khash_searches;	/* # of kernel hash searches */
24750Sstevel@tonic-gate 	int		sf_khash_links;		/* # of kernel hash links */
24760Sstevel@tonic-gate 
24770Sstevel@tonic-gate 	int		sf_swapout;		/* # times hat swapped out */
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 	int		sf_tsb_alloc;		/* # TSB allocations */
24800Sstevel@tonic-gate 	int		sf_tsb_allocfail;	/* # times TSB alloc fail */
24810Sstevel@tonic-gate 	int		sf_tsb_sectsb_create;	/* # times second TSB added */
24820Sstevel@tonic-gate 
24834528Spaulsan 	int		sf_scd_1sttsb_alloc;	/* # SCD 1st TSB allocations */
24844528Spaulsan 	int		sf_scd_2ndtsb_alloc;	/* # SCD 2nd TSB allocations */
24854528Spaulsan 	int		sf_scd_1sttsb_allocfail; /* # SCD 1st TSB alloc fail */
24864528Spaulsan 	int		sf_scd_2ndtsb_allocfail; /* # SCD 2nd TSB alloc fail */
24874528Spaulsan 
24884528Spaulsan 
24890Sstevel@tonic-gate 	int		sf_tteload8k;		/* calls to sfmmu_tteload */
24900Sstevel@tonic-gate 	int		sf_tteload64k;		/* calls to sfmmu_tteload */
24910Sstevel@tonic-gate 	int		sf_tteload512k;		/* calls to sfmmu_tteload */
24920Sstevel@tonic-gate 	int		sf_tteload4m;		/* calls to sfmmu_tteload */
24930Sstevel@tonic-gate 	int		sf_tteload32m;		/* calls to sfmmu_tteload */
24940Sstevel@tonic-gate 	int		sf_tteload256m;		/* calls to sfmmu_tteload */
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 	int		sf_tsb_load8k;		/* # times loaded 8K tsbent */
24970Sstevel@tonic-gate 	int		sf_tsb_load4m;		/* # times loaded 4M tsbent */
24980Sstevel@tonic-gate 
24990Sstevel@tonic-gate 	int		sf_hblk_hit;		/* found hblk during tteload */
25000Sstevel@tonic-gate 	int		sf_hblk8_ncreate;	/* static hblk8's created */
25010Sstevel@tonic-gate 	int		sf_hblk8_nalloc;	/* static hblk8's allocated */
25020Sstevel@tonic-gate 	int		sf_hblk1_ncreate;	/* static hblk1's created */
25030Sstevel@tonic-gate 	int		sf_hblk1_nalloc;	/* static hblk1's allocated */
25040Sstevel@tonic-gate 	int		sf_hblk_slab_cnt;	/* sfmmu8_cache slab creates */
25050Sstevel@tonic-gate 	int		sf_hblk_reserve_cnt;	/* hblk_reserve usage */
25060Sstevel@tonic-gate 	int		sf_hblk_recurse_cnt;	/* hblk_reserve	owner reqs */
25070Sstevel@tonic-gate 	int		sf_hblk_reserve_hit;	/* hblk_reserve hash hits */
25080Sstevel@tonic-gate 	int		sf_get_free_success;	/* reserve list allocs */
25090Sstevel@tonic-gate 	int		sf_get_free_throttle;	/* fails due to throttling */
25100Sstevel@tonic-gate 	int		sf_get_free_fail;	/* fails due to empty list */
25110Sstevel@tonic-gate 	int		sf_put_free_success;	/* reserve list frees */
25120Sstevel@tonic-gate 	int		sf_put_free_fail;	/* fails due to full list */
25130Sstevel@tonic-gate 
25140Sstevel@tonic-gate 	int		sf_pgcolor_conflict;	/* VAC conflict resolution */
25150Sstevel@tonic-gate 	int		sf_uncache_conflict;	/* VAC conflict resolution */
25160Sstevel@tonic-gate 	int		sf_unload_conflict;	/* VAC unload resolution */
25170Sstevel@tonic-gate 	int		sf_ism_uncache;		/* VAC conflict resolution */
25180Sstevel@tonic-gate 	int		sf_ism_recache;		/* VAC conflict resolution */
25190Sstevel@tonic-gate 	int		sf_recache;		/* VAC conflict resolution */
25200Sstevel@tonic-gate 
25210Sstevel@tonic-gate 	int		sf_steal_count;		/* # of hblks stolen */
25220Sstevel@tonic-gate 
25230Sstevel@tonic-gate 	int		sf_pagesync;		/* # of pagesyncs */
25240Sstevel@tonic-gate 	int		sf_clrwrt;		/* # of clear write perms */
25250Sstevel@tonic-gate 	int		sf_pagesync_invalid;	/* pagesync with inv tte */
25260Sstevel@tonic-gate 
25270Sstevel@tonic-gate 	int		sf_kernel_xcalls;	/* # of kernel cross calls */
25280Sstevel@tonic-gate 	int		sf_user_xcalls;		/* # of user cross calls */
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 	int		sf_tsb_grow;		/* # of user tsb grows */
25310Sstevel@tonic-gate 	int		sf_tsb_shrink;		/* # of user tsb shrinks */
25320Sstevel@tonic-gate 	int		sf_tsb_resize_failures;	/* # of user tsb resize */
25330Sstevel@tonic-gate 	int		sf_tsb_reloc;		/* # of user tsb relocations */
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	int		sf_user_vtop;		/* # of user vatopfn calls */
25360Sstevel@tonic-gate 
25372241Shuah 	int		sf_ctx_inv;		/* #times invalidate MMU ctx */
25380Sstevel@tonic-gate 
25390Sstevel@tonic-gate 	int		sf_tlb_reprog_pgsz;	/* # times switch TLB pgsz */
25404528Spaulsan 
25414528Spaulsan 	int		sf_region_remap_demap;	/* # times shme remap demap */
25424528Spaulsan 
25434528Spaulsan 	int		sf_create_scd;		/* # times SCD is created */
25444528Spaulsan 	int		sf_join_scd;		/* # process joined scd */
25454528Spaulsan 	int		sf_leave_scd;		/* # process left scd */
25464528Spaulsan 	int		sf_destroy_scd;		/* # times SCD is destroyed */
25470Sstevel@tonic-gate };
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate struct sfmmu_tsbsize_stat {
25500Sstevel@tonic-gate 	int		sf_tsbsz_8k;
25510Sstevel@tonic-gate 	int		sf_tsbsz_16k;
25520Sstevel@tonic-gate 	int		sf_tsbsz_32k;
25530Sstevel@tonic-gate 	int		sf_tsbsz_64k;
25540Sstevel@tonic-gate 	int		sf_tsbsz_128k;
25550Sstevel@tonic-gate 	int		sf_tsbsz_256k;
25560Sstevel@tonic-gate 	int		sf_tsbsz_512k;
25570Sstevel@tonic-gate 	int		sf_tsbsz_1m;
25580Sstevel@tonic-gate 	int		sf_tsbsz_2m;
25590Sstevel@tonic-gate 	int		sf_tsbsz_4m;
25604528Spaulsan 	int		sf_tsbsz_8m;
25614528Spaulsan 	int		sf_tsbsz_16m;
25624528Spaulsan 	int		sf_tsbsz_32m;
25634528Spaulsan 	int		sf_tsbsz_64m;
25644528Spaulsan 	int		sf_tsbsz_128m;
25654528Spaulsan 	int		sf_tsbsz_256m;
25660Sstevel@tonic-gate };
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate struct sfmmu_percpu_stat {
25690Sstevel@tonic-gate 	int	sf_itlb_misses;		/* # of itlb misses */
25700Sstevel@tonic-gate 	int	sf_dtlb_misses;		/* # of dtlb misses */
25710Sstevel@tonic-gate 	int	sf_utsb_misses;		/* # of user tsb misses */
25720Sstevel@tonic-gate 	int	sf_ktsb_misses;		/* # of kernel tsb misses */
25730Sstevel@tonic-gate 	int	sf_tsb_hits;		/* # of tsb hits */
25740Sstevel@tonic-gate 	int	sf_umod_faults;		/* # of mod (prot viol) flts */
25750Sstevel@tonic-gate 	int	sf_kmod_faults;		/* # of mod (prot viol) flts */
25760Sstevel@tonic-gate };
25770Sstevel@tonic-gate 
25782241Shuah #define	SFMMU_STAT(stat)		sfmmu_global_stat.stat++
25792241Shuah #define	SFMMU_STAT_ADD(stat, amount)	sfmmu_global_stat.stat += (amount)
25802241Shuah #define	SFMMU_STAT_SET(stat, count)	sfmmu_global_stat.stat = (count)
25812241Shuah 
258211713SPavel.Tatashin@Sun.COM #define	SFMMU_MMU_STAT(stat)		{		\
258311713SPavel.Tatashin@Sun.COM 	mmu_ctx_t *ctx = CPU->cpu_m.cpu_mmu_ctxp;	\
258411713SPavel.Tatashin@Sun.COM 	if (ctx)					\
258511713SPavel.Tatashin@Sun.COM 		ctx->stat++;				\
258611713SPavel.Tatashin@Sun.COM }
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate #endif /* !_ASM */
25890Sstevel@tonic-gate 
25900Sstevel@tonic-gate #ifdef	__cplusplus
25910Sstevel@tonic-gate }
25920Sstevel@tonic-gate #endif
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate #endif	/* _VM_HAT_SFMMU_H */
2595