xref: /titanic_51/usr/src/uts/i86pc/vm/vm_dep.h (revision 71b4af9b981b0fc179b6c0bab0186b84a2bc2d9c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a985e578Skchow  * Common Development and Distribution License (the "License").
6a985e578Skchow  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*b52a336eSPavel Tatashin  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
24a3114836SGerry Liu /*
25a3114836SGerry Liu  * Copyright (c) 2010, Intel Corporation.
26a3114836SGerry Liu  * All rights reserved.
27a3114836SGerry Liu  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * UNIX machine dependent virtual memory support.
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #ifndef	_VM_DEP_H
347c478bd9Sstevel@tonic-gate #define	_VM_DEP_H
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
387c478bd9Sstevel@tonic-gate extern "C" {
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <sys/clock.h>
427c478bd9Sstevel@tonic-gate #include <vm/hat_pte.h>
43ae115bc7Smrj #include <sys/param.h>
44ce8eb11aSdp78419 #include <sys/memnode.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
47843e1988Sjohnlev  * WARNING: vm_dep.h is included by files in common.
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #define	GETTICK()	tsc_read()
5106fb6a36Sdv142724 /*
5206fb6a36Sdv142724  * Do not use this function for obtaining clock tick.  This
5306fb6a36Sdv142724  * is called by callers who do not need to have a guarenteed
5406fb6a36Sdv142724  * correct tick value.  The proper routine to use is tsc_read().
5506fb6a36Sdv142724  */
567c478bd9Sstevel@tonic-gate 
57*b52a336eSPavel Tatashin extern u_longlong_t	randtick();
58843e1988Sjohnlev extern uint_t page_create_update_flags_x86(uint_t);
597c478bd9Sstevel@tonic-gate 
60843e1988Sjohnlev extern size_t plcnt_sz(size_t);
61843e1988Sjohnlev #define	PLCNT_SZ(ctrs_sz) (ctrs_sz = plcnt_sz(ctrs_sz))
627c478bd9Sstevel@tonic-gate 
63843e1988Sjohnlev extern caddr_t plcnt_init(caddr_t);
64843e1988Sjohnlev #define	PLCNT_INIT(addr) (addr = plcnt_init(addr))
65843e1988Sjohnlev 
66843e1988Sjohnlev extern void plcnt_inc_dec(page_t *, int, int, long, int);
67843e1988Sjohnlev #define	PLCNT_INCR(pp, mnode, mtype, szc, flags)			\
68843e1988Sjohnlev 	plcnt_inc_dec(pp, mtype, szc, 1l << PAGE_BSZS_SHIFT(szc), flags)
69843e1988Sjohnlev #define	PLCNT_DECR(pp, mnode, mtype, szc, flags)			\
70843e1988Sjohnlev 	plcnt_inc_dec(pp, mtype, szc, -1l << PAGE_BSZS_SHIFT(szc), flags)
7107ad560dSkchow 
727c478bd9Sstevel@tonic-gate /*
73843e1988Sjohnlev  * macro to update page list max counts.  no-op on x86.
74affbd3ccSkchow  */
75e21bae1bSkchow #define	PLCNT_XFER_NORELOC(pp)
76e21bae1bSkchow 
77a3114836SGerry Liu /*
78a3114836SGerry Liu  * macro to modify the page list max counts when memory is added to
79a3114836SGerry Liu  * the page lists during startup (add_physmem) or during a DR operation
80a3114836SGerry Liu  * when memory is added (kphysm_add_memory_dynamic) or deleted
81a3114836SGerry Liu  * (kphysm_del_cleanup).
82a3114836SGerry Liu  */
83a3114836SGerry Liu #define	PLCNT_MODIFY_MAX(pfn, cnt)	mtype_modify_max(pfn, cnt)
84a3114836SGerry Liu 
857c478bd9Sstevel@tonic-gate extern int memrange_num(pfn_t);
867c478bd9Sstevel@tonic-gate extern int pfn_2_mtype(pfn_t);
877c478bd9Sstevel@tonic-gate extern int mtype_func(int, int, uint_t);
88e21bae1bSkchow extern void mtype_modify_max(pfn_t, long);
89affbd3ccSkchow extern int mnode_pgcnt(int);
905d07b933Sdp78419 extern int mnode_range_cnt(int);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /*
935d07b933Sdp78419  * candidate counters in vm_pagelist.c are indexed by color and range
945d07b933Sdp78419  */
95843e1988Sjohnlev #define	NUM_MEM_RANGES		4		/* memory range types */
965d07b933Sdp78419 #define	MAX_MNODE_MRANGES	NUM_MEM_RANGES
975d07b933Sdp78419 #define	MNODE_RANGE_CNT(mnode)	mnode_range_cnt(mnode)
98843e1988Sjohnlev #define	MNODE_MAX_MRANGE(mnode)	memrange_num(mem_node_config[mnode].physbase)
99843e1988Sjohnlev 
100843e1988Sjohnlev /*
101843e1988Sjohnlev  * This was really badly defined, it implicitly uses mnode_maxmrange[]
102843e1988Sjohnlev  * which is a static in vm_pagelist.c
103843e1988Sjohnlev  */
104843e1988Sjohnlev extern int mtype_2_mrange(int);
1055d07b933Sdp78419 #define	MTYPE_2_MRANGE(mnode, mtype)	\
106843e1988Sjohnlev 	(mnode_maxmrange[mnode] - mtype_2_mrange(mtype))
1075d07b933Sdp78419 
1085d07b933Sdp78419 /*
109d94ffb28Sjmcp  * Per page size free lists. Allocated dynamically.
1107c478bd9Sstevel@tonic-gate  * dimensions [mtype][mmu_page_sizes][colors]
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * mtype specifies a physical memory range with a unique mnode.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate 
115d94ffb28Sjmcp extern page_t ****page_freelists;
1167c478bd9Sstevel@tonic-gate 
117d94ffb28Sjmcp #define	PAGE_FREELISTS(mnode, szc, color, mtype)		\
118d94ffb28Sjmcp 	(*(page_freelists[mtype][szc] + (color)))
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /*
1217c478bd9Sstevel@tonic-gate  * For now there is only a single size cache list. Allocated dynamically.
1227c478bd9Sstevel@tonic-gate  * dimensions [mtype][colors]
1237c478bd9Sstevel@tonic-gate  *
1247c478bd9Sstevel@tonic-gate  * mtype specifies a physical memory range with a unique mnode.
1257c478bd9Sstevel@tonic-gate  */
1267c478bd9Sstevel@tonic-gate extern page_t ***page_cachelists;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	PAGE_CACHELISTS(mnode, color, mtype) 		\
1297c478bd9Sstevel@tonic-gate 	(*(page_cachelists[mtype] + (color)))
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /*
132d94ffb28Sjmcp  * There are mutexes for both the page freelist
1337c478bd9Sstevel@tonic-gate  * and the page cachelist.  We want enough locks to make contention
1347c478bd9Sstevel@tonic-gate  * reasonable, but not too many -- otherwise page_freelist_lock() gets
1357c478bd9Sstevel@tonic-gate  * so expensive that it becomes the bottleneck!
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define	NPC_MUTEX	16
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate extern kmutex_t	*fpc_mutex[NPC_MUTEX];
1417c478bd9Sstevel@tonic-gate extern kmutex_t	*cpc_mutex[NPC_MUTEX];
1427c478bd9Sstevel@tonic-gate 
143d94ffb28Sjmcp extern page_t *page_get_mnode_freelist(int, uint_t, int, uchar_t, uint_t);
1447c478bd9Sstevel@tonic-gate extern page_t *page_get_mnode_cachelist(uint_t, uint_t, int, int);
1457c478bd9Sstevel@tonic-gate 
146ce8eb11aSdp78419 /* mem node iterator is not used on x86 */
147ce8eb11aSdp78419 #define	MEM_NODE_ITERATOR_DECL(it)
148b779d3e0Sdp78419 #define	MEM_NODE_ITERATOR_INIT(pfn, mnode, szc, it)
149ce8eb11aSdp78419 
150ce8eb11aSdp78419 /*
151ce8eb11aSdp78419  * interleaved_mnodes mode is never set on x86, therefore,
152ce8eb11aSdp78419  * simply return the limits of the given mnode, which then
153ce8eb11aSdp78419  * determines the length of hpm_counters array for the mnode.
154ce8eb11aSdp78419  */
155ce8eb11aSdp78419 #define	HPM_COUNTERS_LIMITS(mnode, physbase, physmax, first) 	\
156ce8eb11aSdp78419 	{							\
157ce8eb11aSdp78419 		(physbase) = mem_node_config[(mnode)].physbase;	\
158ce8eb11aSdp78419 		(physmax) = mem_node_config[(mnode)].physmax;	\
159ce8eb11aSdp78419 		(first) = (mnode);				\
160ce8eb11aSdp78419 	}
161ce8eb11aSdp78419 
162ce8eb11aSdp78419 #define	PAGE_CTRS_WRITE_LOCK(mnode)				\
163ce8eb11aSdp78419 	{							\
164ce8eb11aSdp78419 		rw_enter(&page_ctrs_rwlock[(mnode)], RW_WRITER);\
165ce8eb11aSdp78419 		page_freelist_lock(mnode);			\
166ce8eb11aSdp78419 	}
167ce8eb11aSdp78419 
168ce8eb11aSdp78419 #define	PAGE_CTRS_WRITE_UNLOCK(mnode)				\
169ce8eb11aSdp78419 	{							\
170ce8eb11aSdp78419 		page_freelist_unlock(mnode);			\
171ce8eb11aSdp78419 		rw_exit(&page_ctrs_rwlock[(mnode)]);		\
172ce8eb11aSdp78419 	}
173ce8eb11aSdp78419 
174a3114836SGerry Liu /*
175a3114836SGerry Liu  * macro to call page_ctrs_adjust() when memory is added
176a3114836SGerry Liu  * during a DR operation.
177a3114836SGerry Liu  */
178a3114836SGerry Liu #define	PAGE_CTRS_ADJUST(pfn, cnt, rv) {				       \
179a3114836SGerry Liu 	spgcnt_t _cnt = (spgcnt_t)(cnt);				       \
180a3114836SGerry Liu 	int _mn;							       \
181a3114836SGerry Liu 	pgcnt_t _np;							       \
182a3114836SGerry Liu 	pfn_t _pfn = (pfn);						       \
183a3114836SGerry Liu 	pfn_t _endpfn = _pfn + _cnt;					       \
184a3114836SGerry Liu 	while (_pfn < _endpfn) {					       \
185a3114836SGerry Liu 		_mn = PFN_2_MEM_NODE(_pfn);				       \
186a3114836SGerry Liu 		_np = MIN(_endpfn, mem_node_config[_mn].physmax + 1) - _pfn;   \
187a3114836SGerry Liu 		_pfn += _np;						       \
188a3114836SGerry Liu 		if ((rv = page_ctrs_adjust(_mn)) != 0)			       \
189a3114836SGerry Liu 			break;						       \
190a3114836SGerry Liu 	}								       \
191a3114836SGerry Liu }
192a3114836SGerry Liu 
1935d07b933Sdp78419 #define	PAGE_GET_COLOR_SHIFT(szc, nszc)				\
1945d07b933Sdp78419 	    (hw_page_array[(nszc)].hp_shift - hw_page_array[(szc)].hp_shift)
1955d07b933Sdp78419 
196ce8eb11aSdp78419 #define	PAGE_CONVERT_COLOR(ncolor, szc, nszc)			\
197ce8eb11aSdp78419 	    ((ncolor) << PAGE_GET_COLOR_SHIFT((szc), (nszc)))
198ce8eb11aSdp78419 
199ce8eb11aSdp78419 #define	PFN_2_COLOR(pfn, szc, it)					\
2005d07b933Sdp78419 	(((pfn) & page_colors_mask) >>			                \
2017c478bd9Sstevel@tonic-gate 	(hw_page_array[szc].hp_shift - hw_page_array[0].hp_shift))
2027c478bd9Sstevel@tonic-gate 
2035d07b933Sdp78419 #define	PNUM_SIZE(szc)							\
2045d07b933Sdp78419 	(hw_page_array[(szc)].hp_pgcnt)
2055d07b933Sdp78419 #define	PNUM_SHIFT(szc)							\
2065d07b933Sdp78419 	(hw_page_array[(szc)].hp_shift - hw_page_array[0].hp_shift)
2075d07b933Sdp78419 #define	PAGE_GET_SHIFT(szc)						\
2085d07b933Sdp78419 	(hw_page_array[(szc)].hp_shift)
2095d07b933Sdp78419 #define	PAGE_GET_PAGECOLORS(szc)					\
2105d07b933Sdp78419 	(hw_page_array[(szc)].hp_colors)
2115d07b933Sdp78419 
2125d07b933Sdp78419 /*
2135d07b933Sdp78419  * This macro calculates the next sequential pfn with the specified
2145d07b933Sdp78419  * color using color equivalency mask
2155d07b933Sdp78419  */
216ce8eb11aSdp78419 #define	PAGE_NEXT_PFN_FOR_COLOR(pfn, szc, color, ceq_mask, color_mask, it)    \
2175d07b933Sdp78419 	{								      \
2185d07b933Sdp78419 		uint_t	pfn_shift = PAGE_BSZS_SHIFT(szc);                     \
2195d07b933Sdp78419 		pfn_t	spfn = pfn >> pfn_shift;                              \
2205d07b933Sdp78419 		pfn_t	stride = (ceq_mask) + 1;                              \
221a7c3ca36Sdp78419 		ASSERT(((color) & ~(ceq_mask)) == 0);                         \
2225d07b933Sdp78419 		ASSERT((((ceq_mask) + 1) & (ceq_mask)) == 0);                 \
2235d07b933Sdp78419 		if (((spfn ^ (color)) & (ceq_mask)) == 0) {                   \
2245d07b933Sdp78419 			pfn += stride << pfn_shift;                           \
2255d07b933Sdp78419 		} else {                                                      \
2265d07b933Sdp78419 			pfn = (spfn & ~(pfn_t)(ceq_mask)) | (color);          \
2275d07b933Sdp78419 			pfn = (pfn > spfn ? pfn : pfn + stride) << pfn_shift; \
2285d07b933Sdp78419 		}                                                             \
2295d07b933Sdp78419 	}
2305d07b933Sdp78419 
2315d07b933Sdp78419 /* get the color equivalency mask for the next szc */
2325d07b933Sdp78419 #define	PAGE_GET_NSZ_MASK(szc, mask)                                         \
2335d07b933Sdp78419 	((mask) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc)))
2345d07b933Sdp78419 
2355d07b933Sdp78419 /* get the color of the next szc */
2365d07b933Sdp78419 #define	PAGE_GET_NSZ_COLOR(szc, color)                                       \
2375d07b933Sdp78419 	((color) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc)))
2385d07b933Sdp78419 
2395d07b933Sdp78419 /* Find the bin for the given page if it was of size szc */
240ce8eb11aSdp78419 #define	PP_2_BIN_SZC(pp, szc)	(PFN_2_COLOR(pp->p_pagenum, szc, NULL))
2415d07b933Sdp78419 
242d94ffb28Sjmcp #define	PP_2_BIN(pp)		(PP_2_BIN_SZC(pp, pp->p_szc))
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #define	PP_2_MEM_NODE(pp)	(PFN_2_MEM_NODE(pp->p_pagenum))
245843e1988Sjohnlev #define	PP_2_MTYPE(pp)		(pfn_2_mtype(pp->p_pagenum))
2467c478bd9Sstevel@tonic-gate #define	PP_2_SZC(pp)		(pp->p_szc)
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate #define	SZCPAGES(szc)		(1 << PAGE_BSZS_SHIFT(szc))
2497c478bd9Sstevel@tonic-gate #define	PFN_BASE(pfnum, szc)	(pfnum & ~(SZCPAGES(szc) - 1))
2507c478bd9Sstevel@tonic-gate 
251d94ffb28Sjmcp /*
252d94ffb28Sjmcp  * this structure is used for walking free page lists
253d94ffb28Sjmcp  * controls when to split large pages into smaller pages,
254d94ffb28Sjmcp  * and when to coalesce smaller pages into larger pages
255d94ffb28Sjmcp  */
256d94ffb28Sjmcp typedef struct page_list_walker {
257d94ffb28Sjmcp 	uint_t	plw_colors;		/* num of colors for szc */
258d94ffb28Sjmcp 	uint_t  plw_color_mask;		/* colors-1 */
259d94ffb28Sjmcp 	uint_t	plw_bin_step;		/* next bin: 1 or 2 */
260d94ffb28Sjmcp 	uint_t  plw_count;		/* loop count */
261d94ffb28Sjmcp 	uint_t	plw_bin0;		/* starting bin */
262d94ffb28Sjmcp 	uint_t  plw_bin_marker;		/* bin after initial jump */
263d94ffb28Sjmcp 	uint_t  plw_bin_split_prev;	/* last bin we tried to split */
264d94ffb28Sjmcp 	uint_t  plw_do_split;		/* set if OK to split */
265d94ffb28Sjmcp 	uint_t  plw_split_next;		/* next bin to split */
266d94ffb28Sjmcp 	uint_t	plw_ceq_dif;		/* number of different color groups */
267d94ffb28Sjmcp 					/* to check */
268d94ffb28Sjmcp 	uint_t	plw_ceq_mask[MMU_PAGE_SIZES + 1]; /* color equiv mask */
269d94ffb28Sjmcp 	uint_t	plw_bins[MMU_PAGE_SIZES + 1];	/* num of bins */
270d94ffb28Sjmcp } page_list_walker_t;
271d94ffb28Sjmcp 
2725d07b933Sdp78419 void	page_list_walk_init(uchar_t szc, uint_t flags, uint_t bin,
2735d07b933Sdp78419     int can_split, int use_ceq, page_list_walker_t *plw);
2745d07b933Sdp78419 
2755d07b933Sdp78419 uint_t	page_list_walk_next_bin(uchar_t szc, uint_t bin,
2765d07b933Sdp78419     page_list_walker_t *plw);
2775d07b933Sdp78419 
278affbd3ccSkchow extern struct cpu	cpus[];
279affbd3ccSkchow #define	CPU0		cpus
280affbd3ccSkchow 
281843e1988Sjohnlev extern int mtype_init(vnode_t *, caddr_t, uint_t *, size_t);
282843e1988Sjohnlev #define	MTYPE_INIT(mtype, vp, vaddr, flags, pgsz)		\
283843e1988Sjohnlev 	(mtype = mtype_init(vp, vaddr, &(flags), pgsz))
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate /*
2867c478bd9Sstevel@tonic-gate  * macros to loop through the mtype range (page_get_mnode_{free,cache,any}list,
2877c478bd9Sstevel@tonic-gate  * and page_get_contig_pages)
2887c478bd9Sstevel@tonic-gate  *
2897c478bd9Sstevel@tonic-gate  * MTYPE_START sets the initial mtype. -1 if the mtype range specified does
2907c478bd9Sstevel@tonic-gate  * not contain mnode.
2917c478bd9Sstevel@tonic-gate  *
2927c478bd9Sstevel@tonic-gate  * MTYPE_NEXT sets the next mtype. -1 if there are no more valid
2937c478bd9Sstevel@tonic-gate  * mtype in the range.
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate #define	MTYPE_START(mnode, mtype, flags)				\
2977c478bd9Sstevel@tonic-gate 	(mtype = mtype_func(mnode, mtype, flags))
2987c478bd9Sstevel@tonic-gate 
299affbd3ccSkchow #define	MTYPE_NEXT(mnode, mtype, flags) {				\
300affbd3ccSkchow 	if (flags & PGI_MT_RANGE) {					\
301affbd3ccSkchow 		mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);	\
302affbd3ccSkchow 	} else {							\
303affbd3ccSkchow 		mtype = -1;						\
304affbd3ccSkchow 	}								\
305affbd3ccSkchow }
3067c478bd9Sstevel@tonic-gate 
307843e1988Sjohnlev extern int mtype_pgr_init(int *, page_t *, int, pgcnt_t);
308843e1988Sjohnlev #define	MTYPE_PGR_INIT(mtype, flags, pp, mnode, pgcnt)			\
309843e1988Sjohnlev 	(mtype = mtype_pgr_init(&flags, pp, mnode, pgcnt))
3107c478bd9Sstevel@tonic-gate 
311affbd3ccSkchow #define	MNODE_PGCNT(mnode)		mnode_pgcnt(mnode)
312affbd3ccSkchow 
313843e1988Sjohnlev extern void mnodetype_2_pfn(int, int, pfn_t *, pfn_t *);
3147c478bd9Sstevel@tonic-gate #define	MNODETYPE_2_PFN(mnode, mtype, pfnlo, pfnhi)			\
315843e1988Sjohnlev 	mnodetype_2_pfn(mnode, mtype, &pfnlo, &pfnhi)
3167c478bd9Sstevel@tonic-gate 
317d94ffb28Sjmcp #define	PC_BIN_MUTEX(mnode, bin, flags) ((flags & PG_FREE_LIST) ?	\
318d94ffb28Sjmcp 	&fpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode] :			\
3197c478bd9Sstevel@tonic-gate 	&cpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode])
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate #define	FPC_MUTEX(mnode, i)	(&fpc_mutex[i][mnode])
3227c478bd9Sstevel@tonic-gate #define	CPC_MUTEX(mnode, i)	(&cpc_mutex[i][mnode])
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate #ifdef DEBUG
3257c478bd9Sstevel@tonic-gate #define	CHK_LPG(pp, szc)	chk_lpg(pp, szc)
3267c478bd9Sstevel@tonic-gate extern void	chk_lpg(page_t *, uchar_t);
3277c478bd9Sstevel@tonic-gate #else
3287c478bd9Sstevel@tonic-gate #define	CHK_LPG(pp, szc)
3297c478bd9Sstevel@tonic-gate #endif
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate #define	FULL_REGION_CNT(rg_szc)	\
3327c478bd9Sstevel@tonic-gate 	(LEVEL_SIZE(rg_szc) >> LEVEL_SHIFT(rg_szc - 1))
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate /* Return the leader for this mapping size */
3357c478bd9Sstevel@tonic-gate #define	PP_GROUPLEADER(pp, szc) \
3367c478bd9Sstevel@tonic-gate 	(&(pp)[-(int)((pp)->p_pagenum & (SZCPAGES(szc)-1))])
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate /* Return the root page for this page based on p_szc */
3397c478bd9Sstevel@tonic-gate #define	PP_PAGEROOT(pp) ((pp)->p_szc == 0 ? (pp) : \
3407c478bd9Sstevel@tonic-gate 	PP_GROUPLEADER((pp), (pp)->p_szc))
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate  * The counter base must be per page_counter element to prevent
3447c478bd9Sstevel@tonic-gate  * races when re-indexing, and the base page size element should
3457c478bd9Sstevel@tonic-gate  * be aligned on a boundary of the given region size.
3467c478bd9Sstevel@tonic-gate  *
3477c478bd9Sstevel@tonic-gate  * We also round up the number of pages spanned by the counters
3487c478bd9Sstevel@tonic-gate  * for a given region to PC_BASE_ALIGN in certain situations to simplify
3497c478bd9Sstevel@tonic-gate  * the coding for some non-performance critical routines.
3507c478bd9Sstevel@tonic-gate  */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate #define	PC_BASE_ALIGN		((pfn_t)1 << PAGE_BSZS_SHIFT(MMU_PAGE_SIZES-1))
3537c478bd9Sstevel@tonic-gate #define	PC_BASE_ALIGN_MASK	(PC_BASE_ALIGN - 1)
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * cpu/mmu-dependent vm variables
3577c478bd9Sstevel@tonic-gate  */
3587c478bd9Sstevel@tonic-gate extern uint_t mmu_page_sizes;
3597c478bd9Sstevel@tonic-gate extern uint_t mmu_exported_page_sizes;
36002bc52beSkchow /*
36102bc52beSkchow  * page sizes that legacy applications can see via getpagesizes(3c).
36202bc52beSkchow  * Used to prevent legacy applications from inadvertantly using the
36302bc52beSkchow  * 'new' large pagesizes (1g and above).
36402bc52beSkchow  */
36502bc52beSkchow extern uint_t mmu_legacy_page_sizes;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /* For x86, userszc is the same as the kernel's szc */
3687c478bd9Sstevel@tonic-gate #define	USERSZC_2_SZC(userszc)	(userszc)
3697c478bd9Sstevel@tonic-gate #define	SZC_2_USERSZC(szc)	(szc)
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * for hw_page_map_t, sized to hold the ratio of large page to base
3737c478bd9Sstevel@tonic-gate  * pagesize (1024 max)
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate typedef	short	hpmctr_t;
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * get the setsize of the current cpu - assume homogenous for x86
3797c478bd9Sstevel@tonic-gate  */
3807c478bd9Sstevel@tonic-gate extern int	l2cache_sz, l2cache_linesz, l2cache_assoc;
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #define	L2CACHE_ALIGN		l2cache_linesz
383affbd3ccSkchow #define	L2CACHE_ALIGN_MAX	64
3847c478bd9Sstevel@tonic-gate #define	CPUSETSIZE()		\
3857c478bd9Sstevel@tonic-gate 	(l2cache_assoc ? (l2cache_sz / l2cache_assoc) : MMU_PAGESIZE)
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * Return the log2(pagesize(szc) / MMU_PAGESIZE) --- or the shift count
3897c478bd9Sstevel@tonic-gate  * for the number of base pages in this pagesize
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate #define	PAGE_BSZS_SHIFT(szc) (LEVEL_SHIFT(szc) - MMU_PAGESHIFT)
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*
3947c478bd9Sstevel@tonic-gate  * Internal PG_ flags.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate #define	PGI_RELOCONLY	0x010000	/* opposite of PG_NORELOC */
3977c478bd9Sstevel@tonic-gate #define	PGI_NOCAGE	0x020000	/* cage is disabled */
3987c478bd9Sstevel@tonic-gate #define	PGI_PGCPHIPRI	0x040000	/* page_get_contig_page pri alloc */
3997c478bd9Sstevel@tonic-gate #define	PGI_PGCPSZC0	0x080000	/* relocate base pagesize page */
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*
4027c478bd9Sstevel@tonic-gate  * PGI range flags - should not overlap PGI flags
4037c478bd9Sstevel@tonic-gate  */
4047c478bd9Sstevel@tonic-gate #define	PGI_MT_RANGE0	0x1000000	/* mtype range to 0 */
40507ad560dSkchow #define	PGI_MT_RANGE16M 0x2000000	/* mtype range to 16m */
40607ad560dSkchow #define	PGI_MT_RANGE4G	0x4000000	/* mtype range to 4g */
40707ad560dSkchow #define	PGI_MT_NEXT	0x8000000	/* get next mtype */
40807ad560dSkchow #define	PGI_MT_RANGE	(PGI_MT_RANGE0 | PGI_MT_RANGE16M | PGI_MT_RANGE4G)
4097c478bd9Sstevel@tonic-gate 
410843e1988Sjohnlev 
4117c478bd9Sstevel@tonic-gate /*
412ec25b48fSsusans  * Maximum and default values for user heap, stack, private and shared
413ec25b48fSsusans  * anonymous memory, and user text and initialized data.
414ec25b48fSsusans  * Used by map_pgsz*() routines.
415ec25b48fSsusans  */
416ec25b48fSsusans extern size_t max_uheap_lpsize;
417ec25b48fSsusans extern size_t default_uheap_lpsize;
418ec25b48fSsusans extern size_t max_ustack_lpsize;
419ec25b48fSsusans extern size_t default_ustack_lpsize;
420ec25b48fSsusans extern size_t max_privmap_lpsize;
421ec25b48fSsusans extern size_t max_uidata_lpsize;
422ec25b48fSsusans extern size_t max_utext_lpsize;
423ec25b48fSsusans extern size_t max_shm_lpsize;
424ec25b48fSsusans extern size_t mcntl0_lpsize;
425ec25b48fSsusans 
426ec25b48fSsusans /*
427ec25b48fSsusans  * Sanity control. Don't use large pages regardless of user
428ec25b48fSsusans  * settings if there's less than priv or shm_lpg_min_physmem memory installed.
429ec25b48fSsusans  * The units for this variable are 8K pages.
430ec25b48fSsusans  */
431ec25b48fSsusans extern pgcnt_t privm_lpg_min_physmem;
432ec25b48fSsusans extern pgcnt_t shm_lpg_min_physmem;
433ec25b48fSsusans 
434ec25b48fSsusans /*
4357c478bd9Sstevel@tonic-gate  * hash as and addr to get a bin.
4367c478bd9Sstevel@tonic-gate  */
4377c478bd9Sstevel@tonic-gate 
438d94ffb28Sjmcp #define	AS_2_BIN(as, seg, vp, addr, bin, szc)				    \
4395d07b933Sdp78419 	bin = (((((uintptr_t)(addr) >> PAGESHIFT) + ((uintptr_t)(as) >> 4)) \
4405d07b933Sdp78419 	    & page_colors_mask) >>					    \
4415d07b933Sdp78419 	    (hw_page_array[szc].hp_shift - hw_page_array[0].hp_shift))
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate /*
444affbd3ccSkchow  * cpu private vm data - accessed thru CPU->cpu_vm_data
445affbd3ccSkchow  *	vc_pnum_memseg: tracks last memseg visited in page_numtopp_nolock()
446affbd3ccSkchow  *	vc_pnext_memseg: tracks last memseg visited in page_nextn()
447affbd3ccSkchow  *	vc_kmptr: orignal unaligned kmem pointer for this vm_cpu_data_t
4486061ce8aSkchow  *	vc_kmsize: orignal kmem size for this vm_cpu_data_t
449affbd3ccSkchow  */
450affbd3ccSkchow 
451affbd3ccSkchow typedef struct {
452affbd3ccSkchow 	struct memseg	*vc_pnum_memseg;
453affbd3ccSkchow 	struct memseg	*vc_pnext_memseg;
454affbd3ccSkchow 	void		*vc_kmptr;
4556061ce8aSkchow 	size_t		vc_kmsize;
456affbd3ccSkchow } vm_cpu_data_t;
457affbd3ccSkchow 
458affbd3ccSkchow /* allocation size to ensure vm_cpu_data_t resides in its own cache line */
459affbd3ccSkchow #define	VM_CPU_DATA_PADSIZE						\
460affbd3ccSkchow 	(P2ROUNDUP(sizeof (vm_cpu_data_t), L2CACHE_ALIGN_MAX))
461affbd3ccSkchow 
462affbd3ccSkchow /*
4637c478bd9Sstevel@tonic-gate  * When a bin is empty, and we can't satisfy a color request correctly,
4647c478bd9Sstevel@tonic-gate  * we scan.  If we assume that the programs have reasonable spatial
4657c478bd9Sstevel@tonic-gate  * behavior, then it will not be a good idea to use the adjacent color.
4667c478bd9Sstevel@tonic-gate  * Using the adjacent color would result in virtually adjacent addresses
4677c478bd9Sstevel@tonic-gate  * mapping into the same spot in the cache.  So, if we stumble across
4687c478bd9Sstevel@tonic-gate  * an empty bin, skip a bunch before looking.  After the first skip,
4697c478bd9Sstevel@tonic-gate  * then just look one bin at a time so we don't miss our cache on
4707c478bd9Sstevel@tonic-gate  * every look. Be sure to check every bin.  Page_create() will panic
4717c478bd9Sstevel@tonic-gate  * if we miss a page.
4727c478bd9Sstevel@tonic-gate  *
4737c478bd9Sstevel@tonic-gate  * This also explains the `<=' in the for loops in both page_get_freelist()
4747c478bd9Sstevel@tonic-gate  * and page_get_cachelist().  Since we checked the target bin, skipped
4757c478bd9Sstevel@tonic-gate  * a bunch, then continued one a time, we wind up checking the target bin
4767c478bd9Sstevel@tonic-gate  * twice to make sure we get all of them bins.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate #define	BIN_STEP	19
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate #ifdef VM_STATS
4817c478bd9Sstevel@tonic-gate struct vmm_vmstats_str {
482d94ffb28Sjmcp 	ulong_t pgf_alloc[MMU_PAGE_SIZES];	/* page_get_freelist */
483d94ffb28Sjmcp 	ulong_t pgf_allocok[MMU_PAGE_SIZES];
484d94ffb28Sjmcp 	ulong_t pgf_allocokrem[MMU_PAGE_SIZES];
485d94ffb28Sjmcp 	ulong_t pgf_allocfailed[MMU_PAGE_SIZES];
4867c478bd9Sstevel@tonic-gate 	ulong_t	pgf_allocdeferred;
487d94ffb28Sjmcp 	ulong_t	pgf_allocretry[MMU_PAGE_SIZES];
488affbd3ccSkchow 	ulong_t pgc_alloc;			/* page_get_cachelist */
4897c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocok;
4907c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocokrem;
4917c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocokdeferred;
4927c478bd9Sstevel@tonic-gate 	ulong_t pgc_allocfailed;
493affbd3ccSkchow 	ulong_t	pgcp_alloc[MMU_PAGE_SIZES];	/* page_get_contig_pages */
4947c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocfailed[MMU_PAGE_SIZES];
4957c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocempty[MMU_PAGE_SIZES];
4967c478bd9Sstevel@tonic-gate 	ulong_t	pgcp_allocok[MMU_PAGE_SIZES];
497affbd3ccSkchow 	ulong_t	ptcp[MMU_PAGE_SIZES];		/* page_trylock_contig_pages */
4987c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfreethresh[MMU_PAGE_SIZES];
4997c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailexcl[MMU_PAGE_SIZES];
5007c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailszc[MMU_PAGE_SIZES];
5017c478bd9Sstevel@tonic-gate 	ulong_t	ptcpfailcage[MMU_PAGE_SIZES];
5027c478bd9Sstevel@tonic-gate 	ulong_t	ptcpok[MMU_PAGE_SIZES];
503affbd3ccSkchow 	ulong_t	pgmf_alloc[MMU_PAGE_SIZES];	/* page_get_mnode_freelist */
5047c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocfailed[MMU_PAGE_SIZES];
5057c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocempty[MMU_PAGE_SIZES];
5067c478bd9Sstevel@tonic-gate 	ulong_t	pgmf_allocok[MMU_PAGE_SIZES];
507affbd3ccSkchow 	ulong_t	pgmc_alloc;			/* page_get_mnode_cachelist */
5087c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocfailed;
5097c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocempty;
5107c478bd9Sstevel@tonic-gate 	ulong_t	pgmc_allocok;
511affbd3ccSkchow 	ulong_t	pladd_free[MMU_PAGE_SIZES];	/* page_list_add/sub */
512affbd3ccSkchow 	ulong_t	plsub_free[MMU_PAGE_SIZES];
513affbd3ccSkchow 	ulong_t	pladd_cache;
514affbd3ccSkchow 	ulong_t	plsub_cache;
515affbd3ccSkchow 	ulong_t	plsubpages_szcbig;
516affbd3ccSkchow 	ulong_t	plsubpages_szc0;
5175d07b933Sdp78419 	ulong_t	pfs_req[MMU_PAGE_SIZES];	/* page_freelist_split */
5185d07b933Sdp78419 	ulong_t	pfs_demote[MMU_PAGE_SIZES];
5195d07b933Sdp78419 	ulong_t	pfc_coalok[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
520affbd3ccSkchow 	ulong_t	ppr_reloc[MMU_PAGE_SIZES];	/* page_relocate */
5217c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnoroot[MMU_PAGE_SIZES];
5227c478bd9Sstevel@tonic-gate 	ulong_t ppr_reloc_replnoroot[MMU_PAGE_SIZES];
5237c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnolock[MMU_PAGE_SIZES];
5247c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocnomem[MMU_PAGE_SIZES];
5257c478bd9Sstevel@tonic-gate 	ulong_t ppr_relocok[MMU_PAGE_SIZES];
5268b464eb8Smec 	ulong_t ppr_copyfail;
5275d07b933Sdp78419 	/* page coalesce counter */
5285d07b933Sdp78419 	ulong_t page_ctrs_coalesce[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5295d07b933Sdp78419 	/* candidates useful */
5305d07b933Sdp78419 	ulong_t page_ctrs_cands_skip[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5315d07b933Sdp78419 	/* ctrs changed after locking */
5325d07b933Sdp78419 	ulong_t page_ctrs_changed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5335d07b933Sdp78419 	/* page_freelist_coalesce failed */
5345d07b933Sdp78419 	ulong_t page_ctrs_failed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
5357c478bd9Sstevel@tonic-gate 	ulong_t page_ctrs_coalesce_all;	/* page coalesce all counter */
5367c478bd9Sstevel@tonic-gate 	ulong_t page_ctrs_cands_skip_all; /* candidates useful for all func */
5377c478bd9Sstevel@tonic-gate 	ulong_t	restrict4gcnt;
53807ad560dSkchow 	ulong_t	unrestrict16mcnt;	/* non-DMA 16m allocs allowed */
539a985e578Skchow 	ulong_t	pgpanicalloc;		/* PG_PANIC allocation */
54078b03d3aSkchow 	ulong_t	pcf_deny[MMU_PAGE_SIZES];	/* page_chk_freelist */
54178b03d3aSkchow 	ulong_t	pcf_allow[MMU_PAGE_SIZES];
5427c478bd9Sstevel@tonic-gate };
5437c478bd9Sstevel@tonic-gate extern struct vmm_vmstats_str vmm_vmstats;
5447c478bd9Sstevel@tonic-gate #endif	/* VM_STATS */
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate extern size_t page_ctrs_sz(void);
5477c478bd9Sstevel@tonic-gate extern caddr_t page_ctrs_alloc(caddr_t);
548affbd3ccSkchow extern void page_ctr_sub(int, int, page_t *, int);
5495d07b933Sdp78419 extern page_t *page_freelist_split(uchar_t,
55019397407SSherry Moore     uint_t, int, int, pfn_t, pfn_t, page_list_walker_t *);
5515d07b933Sdp78419 extern page_t *page_freelist_coalesce(int, uchar_t, uint_t, uint_t, int,
5525d07b933Sdp78419     pfn_t);
553a3114836SGerry Liu extern void page_freelist_coalesce_all(int);
5547c478bd9Sstevel@tonic-gate extern uint_t page_get_pagecolors(uint_t);
555d2b85481Srscott extern void pfnzero(pfn_t, uint_t, uint_t);
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
5587c478bd9Sstevel@tonic-gate }
5597c478bd9Sstevel@tonic-gate #endif
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate #endif	/* _VM_DEP_H */
562