xref: /titanic_50/usr/src/uts/sun4/vm/vm_dep.h (revision bb121940c2fe627557326e0143391ace6e6b7372)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * UNIX machine dependent virtual memory support.
28  */
29 
30 #ifndef	_VM_DEP_H
31 #define	_VM_DEP_H
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 #include <vm/hat_sfmmu.h>
40 #include <sys/archsystm.h>
41 #include <sys/memnode.h>
42 
43 #define	GETTICK()	gettick()
44 
45 /*
46  * Per page size free lists. Allocated dynamically.
47  */
48 #define	MAX_MEM_TYPES	2	/* 0 = reloc, 1 = noreloc */
49 #define	MTYPE_RELOC	0
50 #define	MTYPE_NORELOC	1
51 
52 #define	PP_2_MTYPE(pp)	(PP_ISNORELOC(pp) ? MTYPE_NORELOC : MTYPE_RELOC)
53 
54 #define	MTYPE_INIT(mtype, vp, vaddr, flags, pgsz)			\
55 	mtype = (flags & PG_NORELOC) ? MTYPE_NORELOC : MTYPE_RELOC;
56 
57 /* mtype init for page_get_replacement_page */
58 #define	MTYPE_PGR_INIT(mtype, flags, pp, mnode, pgcnt)			\
59 	mtype = (flags & PG_NORELOC) ? MTYPE_NORELOC : MTYPE_RELOC;
60 
61 #define	MNODETYPE_2_PFN(mnode, mtype, pfnlo, pfnhi)			\
62 	ASSERT(mtype != MTYPE_NORELOC);					\
63 	pfnlo = mem_node_config[mnode].physbase;			\
64 	pfnhi = mem_node_config[mnode].physmax;
65 
66 /*
67  * candidate counters in vm_pagelist.c are indexed by color and range
68  */
69 #define	MAX_MNODE_MRANGES		MAX_MEM_TYPES
70 #define	MNODE_RANGE_CNT(mnode)		MAX_MNODE_MRANGES
71 #define	MNODE_MAX_MRANGE(mnode)		(MAX_MEM_TYPES - 1)
72 #define	MTYPE_2_MRANGE(mnode, mtype)	(mtype)
73 
74 /*
75  * Internal PG_ flags.
76  */
77 #define	PGI_RELOCONLY	0x10000	/* acts in the opposite sense to PG_NORELOC */
78 #define	PGI_NOCAGE	0x20000	/* indicates Cage is disabled */
79 #define	PGI_PGCPHIPRI	0x40000	/* page_get_contig_page priority allocation */
80 #define	PGI_PGCPSZC0	0x80000	/* relocate base pagesize page */
81 
82 /*
83  * PGI mtype flags - should not overlap PGI flags
84  */
85 #define	PGI_MT_RANGE	0x1000000	/* mtype range */
86 #define	PGI_MT_NEXT	0x2000000	/* get next mtype */
87 
88 extern page_t ***page_freelists[MMU_PAGE_SIZES][MAX_MEM_TYPES];
89 extern page_t ***page_cachelists[MAX_MEM_TYPES];
90 
91 #define	PAGE_FREELISTS(mnode, szc, color, mtype) \
92 	(*(page_freelists[szc][mtype][mnode] + (color)))
93 
94 #define	PAGE_CACHELISTS(mnode, color, mtype) \
95 	(*(page_cachelists[mtype][mnode] + (color)))
96 
97 /*
98  * There are 'page_colors' colors/bins.  Spread them out under a
99  * couple of locks.  There are mutexes for both the page freelist
100  * and the page cachelist.  We want enough locks to make contention
101  * reasonable, but not too many -- otherwise page_freelist_lock() gets
102  * so expensive that it becomes the bottleneck!
103  */
104 #define	NPC_MUTEX	16
105 
106 extern kmutex_t	*fpc_mutex[NPC_MUTEX];
107 extern kmutex_t	*cpc_mutex[NPC_MUTEX];
108 
109 /*
110  * cpu specific color conversion functions
111  */
112 extern uint_t page_get_nsz_color_mask_cpu(uchar_t, uint_t);
113 #pragma weak page_get_nsz_color_mask_cpu
114 
115 extern uint_t page_get_nsz_color_cpu(uchar_t, uint_t);
116 #pragma weak page_get_nsz_color_cpu
117 
118 extern uint_t page_get_color_shift_cpu(uchar_t, uchar_t);
119 #pragma weak page_get_color_shift_cpu
120 
121 extern pfn_t page_next_pfn_for_color_cpu(pfn_t,
122     uchar_t, uint_t, uint_t, uint_t);
123 #pragma weak page_next_pfn_for_color_cpu
124 
125 extern uint_t  page_pfn_2_color_cpu(pfn_t, uchar_t);
126 #pragma weak page_pfn_2_color_cpu
127 
128 #define	PAGE_GET_COLOR_SHIFT(szc, nszc)				\
129 	((&page_get_color_shift_cpu != NULL) ?			\
130 	    page_get_color_shift_cpu(szc, nszc) :		\
131 	    (hw_page_array[(nszc)].hp_shift -			\
132 		hw_page_array[(szc)].hp_shift))
133 
134 #define	PFN_2_COLOR(pfn, szc)					\
135 	((&page_pfn_2_color_cpu != NULL) ?			\
136 	    page_pfn_2_color_cpu(pfn, szc) :			\
137 	    ((pfn & (hw_page_array[0].hp_colors - 1)) >>	\
138 		(hw_page_array[szc].hp_shift -			\
139 		    hw_page_array[0].hp_shift)))
140 
141 #define	PNUM_SIZE(szc)							\
142 	(hw_page_array[(szc)].hp_pgcnt)
143 #define	PNUM_SHIFT(szc)							\
144 	(hw_page_array[(szc)].hp_shift - hw_page_array[0].hp_shift)
145 #define	PAGE_GET_SHIFT(szc)						\
146 	(hw_page_array[(szc)].hp_shift)
147 #define	PAGE_GET_PAGECOLORS(szc)					\
148 	(hw_page_array[(szc)].hp_colors)
149 
150 /*
151  * This macro calculates the next sequential pfn with the specified
152  * color using color equivalency mask
153  */
154 #define	PAGE_NEXT_PFN_FOR_COLOR(pfn, szc, color, ceq_mask, color_mask)        \
155 	ASSERT(((color) & ~(ceq_mask)) == 0);                                 \
156 	if (&page_next_pfn_for_color_cpu == NULL) {                           \
157 		uint_t	pfn_shift = PAGE_BSZS_SHIFT(szc);                     \
158 		pfn_t	spfn = pfn >> pfn_shift;                              \
159 		pfn_t	stride = (ceq_mask) + 1;                              \
160 		ASSERT((((ceq_mask) + 1) & (ceq_mask)) == 0);                 \
161 		if (((spfn ^ (color)) & (ceq_mask)) == 0) {                   \
162 			pfn += stride << pfn_shift;                           \
163 		} else {                                                      \
164 			pfn = (spfn & ~(pfn_t)(ceq_mask)) | (color);          \
165 			pfn = (pfn > spfn ? pfn : pfn + stride) << pfn_shift; \
166 		}                                                             \
167 	} else {                                                              \
168 		pfn = page_next_pfn_for_color_cpu(pfn, szc, color,	      \
169 		    ceq_mask, color_mask);                                    \
170 	}
171 
172 /* get the color equivalency mask for the next szc */
173 #define	PAGE_GET_NSZ_MASK(szc, mask)                                         \
174 	((&page_get_nsz_color_mask_cpu == NULL) ?                            \
175 	    ((mask) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc))) :  \
176 	    page_get_nsz_color_mask_cpu(szc, mask))
177 
178 /* get the color of the next szc */
179 #define	PAGE_GET_NSZ_COLOR(szc, color)                                       \
180 	((&page_get_nsz_color_cpu == NULL) ?                                 \
181 	    ((color) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc))) : \
182 	    page_get_nsz_color_cpu(szc, color))
183 
184 /* Find the bin for the given page if it was of size szc */
185 #define	PP_2_BIN_SZC(pp, szc)	(PFN_2_COLOR(pp->p_pagenum, szc))
186 
187 #define	PP_2_BIN(pp)		(PP_2_BIN_SZC(pp, pp->p_szc))
188 
189 #define	PP_2_MEM_NODE(pp)	(PFN_2_MEM_NODE(pp->p_pagenum))
190 
191 #define	PC_BIN_MUTEX(mnode, bin, flags) ((flags & PG_FREE_LIST) ?	\
192 	&fpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode] :			\
193 	&cpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode])
194 
195 #define	FPC_MUTEX(mnode, i)	(&fpc_mutex[i][mnode])
196 #define	CPC_MUTEX(mnode, i)	(&cpc_mutex[i][mnode])
197 
198 #define	PFN_BASE(pfnum, szc)	(pfnum & ~((1 << PAGE_BSZS_SHIFT(szc)) - 1))
199 
200 /*
201  * this structure is used for walking free page lists
202  * controls when to split large pages into smaller pages,
203  * and when to coalesce smaller pages into larger pages
204  */
205 typedef struct page_list_walker {
206 	uint_t	plw_colors;		/* num of colors for szc */
207 	uint_t  plw_color_mask;		/* colors-1 */
208 	uint_t	plw_bin_step;		/* next bin: 1 or 2 */
209 	uint_t  plw_count;		/* loop count */
210 	uint_t	plw_bin0;		/* starting bin */
211 	uint_t  plw_bin_marker;		/* bin after initial jump */
212 	uint_t  plw_bin_split_prev;	/* last bin we tried to split */
213 	uint_t  plw_do_split;		/* set if OK to split */
214 	uint_t  plw_split_next;		/* next bin to split */
215 	uint_t	plw_ceq_dif;		/* number of different color groups */
216 					/* to check */
217 	uint_t	plw_ceq_mask[MMU_PAGE_SIZES + 1]; /* color equiv mask */
218 	uint_t	plw_bins[MMU_PAGE_SIZES + 1];	/* num of bins */
219 } page_list_walker_t;
220 
221 void	page_list_walk_init(uchar_t szc, uint_t flags, uint_t bin,
222     int can_split, int use_ceq, page_list_walker_t *plw);
223 
224 typedef	char	hpmctr_t;
225 
226 #ifdef DEBUG
227 #define	CHK_LPG(pp, szc)	chk_lpg(pp, szc)
228 extern void	chk_lpg(page_t *, uchar_t);
229 #else
230 #define	CHK_LPG(pp, szc)
231 #endif
232 
233 /*
234  * page list count per mnode and type.
235  */
236 typedef	struct {
237 	pgcnt_t	plc_mt_pgmax;		/* max page cnt */
238 	pgcnt_t plc_mt_clpgcnt;		/* cache list cnt */
239 	pgcnt_t plc_mt_flpgcnt;		/* free list cnt - small pages */
240 	pgcnt_t plc_mt_lgpgcnt;		/* free list cnt - large pages */
241 #ifdef DEBUG
242 	struct {
243 		pgcnt_t plc_mts_pgcnt;	/* per page size count */
244 		int	plc_mts_colors;
245 		pgcnt_t	*plc_mtsc_pgcnt; /* per color bin count */
246 	} plc_mts[MMU_PAGE_SIZES];
247 #endif
248 } plcnt_t[MAX_MEM_NODES][MAX_MEM_TYPES];
249 
250 #ifdef DEBUG
251 
252 #define	PLCNT_SZ(ctrs_sz) {						\
253 	int	szc;							\
254 	for (szc = 0; szc < mmu_page_sizes; szc++) {			\
255 		int	colors = page_get_pagecolors(szc);		\
256 		ctrs_sz += (max_mem_nodes * MAX_MEM_TYPES *		\
257 		    colors * sizeof (pgcnt_t));				\
258 	}								\
259 }
260 
261 #define	PLCNT_INIT(base) {						\
262 	int	mn, mt, szc, colors;					\
263 	for (szc = 0; szc < mmu_page_sizes; szc++) {			\
264 		colors = page_get_pagecolors(szc);			\
265 		for (mn = 0; mn < max_mem_nodes; mn++) {		\
266 			for (mt = 0; mt < MAX_MEM_TYPES; mt++) {	\
267 				plcnt[mn][mt].plc_mts[szc].		\
268 				    plc_mts_colors = colors;		\
269 				plcnt[mn][mt].plc_mts[szc].		\
270 				    plc_mtsc_pgcnt = (pgcnt_t *)base;	\
271 				base += (colors * sizeof (pgcnt_t));	\
272 			}						\
273 		}							\
274 	}								\
275 }
276 
277 #define	PLCNT_DO(pp, mn, mtype, szc, cnt, flags) {			\
278 	int	bin = PP_2_BIN(pp);					\
279 	if (flags & PG_CACHE_LIST)					\
280 		atomic_add_long(&plcnt[mn][mtype].plc_mt_clpgcnt, cnt);	\
281 	else if (szc)							\
282 		atomic_add_long(&plcnt[mn][mtype].plc_mt_lgpgcnt, cnt);	\
283 	else								\
284 		atomic_add_long(&plcnt[mn][mtype].plc_mt_flpgcnt, cnt);	\
285 	atomic_add_long(&plcnt[mn][mtype].plc_mts[szc].plc_mts_pgcnt,	\
286 	    cnt);							\
287 	atomic_add_long(&plcnt[mn][mtype].plc_mts[szc].			\
288 	    plc_mtsc_pgcnt[bin], cnt);					\
289 }
290 
291 #else
292 
293 #define	PLCNT_SZ(ctrs_sz)
294 
295 #define	PLCNT_INIT(base)
296 
297 /* PG_FREE_LIST may not be explicitly set in flags for large pages */
298 
299 #define	PLCNT_DO(pp, mn, mtype, szc, cnt, flags) {			\
300 	if (flags & PG_CACHE_LIST)					\
301 		atomic_add_long(&plcnt[mn][mtype].plc_mt_clpgcnt, cnt);	\
302 	else if (szc)							\
303 		atomic_add_long(&plcnt[mn][mtype].plc_mt_lgpgcnt, cnt);	\
304 	else								\
305 		atomic_add_long(&plcnt[mn][mtype].plc_mt_flpgcnt, cnt);	\
306 }
307 
308 #endif
309 
310 #define	PLCNT_INCR(pp, mn, mtype, szc, flags) {				\
311 	long	cnt = (1 << PAGE_BSZS_SHIFT(szc));			\
312 	PLCNT_DO(pp, mn, mtype, szc, cnt, flags);			\
313 }
314 
315 #define	PLCNT_DECR(pp, mn, mtype, szc, flags) {				\
316 	long	cnt = ((-1) << PAGE_BSZS_SHIFT(szc));			\
317 	PLCNT_DO(pp, mn, mtype, szc, cnt, flags);			\
318 }
319 
320 /*
321  * macros to update page list max counts - done when pages transferred
322  * from RELOC to NORELOC mtype (kcage_init or kcage_assimilate_page).
323  */
324 
325 #define	PLCNT_XFER_NORELOC(pp) {					\
326 	long	cnt = (1 << PAGE_BSZS_SHIFT((pp)->p_szc));		\
327 	int	mn = PP_2_MEM_NODE(pp);					\
328 	atomic_add_long(&plcnt[mn][MTYPE_NORELOC].plc_mt_pgmax, cnt);	\
329 	atomic_add_long(&plcnt[mn][MTYPE_RELOC].plc_mt_pgmax, -cnt);	\
330 }
331 
332 /*
333  * macro to modify the page list max counts when memory is added to
334  * the page lists during startup (add_physmem) or during a DR operation
335  * when memory is added (kphysm_add_memory_dynamic) or deleted
336  * (kphysm_del_cleanup).
337  */
338 #define	PLCNT_MODIFY_MAX(pfn, cnt) {					\
339 	int	mn = PFN_2_MEM_NODE(pfn);				\
340 	atomic_add_long(&plcnt[mn][MTYPE_RELOC].plc_mt_pgmax, (cnt));	\
341 }
342 
343 extern plcnt_t	plcnt;
344 
345 #define	MNODE_PGCNT(mn)							\
346 	(plcnt[mn][MTYPE_RELOC].plc_mt_clpgcnt +			\
347 	    plcnt[mn][MTYPE_NORELOC].plc_mt_clpgcnt +			\
348 	    plcnt[mn][MTYPE_RELOC].plc_mt_flpgcnt +			\
349 	    plcnt[mn][MTYPE_NORELOC].plc_mt_flpgcnt +			\
350 	    plcnt[mn][MTYPE_RELOC].plc_mt_lgpgcnt +			\
351 	    plcnt[mn][MTYPE_NORELOC].plc_mt_lgpgcnt)
352 
353 #define	MNODETYPE_PGCNT(mn, mtype)					\
354 	(plcnt[mn][mtype].plc_mt_clpgcnt +				\
355 	    plcnt[mn][mtype].plc_mt_flpgcnt +				\
356 	    plcnt[mn][mtype].plc_mt_lgpgcnt)
357 
358 /*
359  * macros to loop through the mtype range - MTYPE_START returns -1 in
360  * mtype if no pages in mnode/mtype and possibly NEXT mtype.
361  */
362 #define	MTYPE_START(mnode, mtype, flags) {				\
363 	if (plcnt[mnode][mtype].plc_mt_pgmax == 0) {			\
364 		ASSERT(mtype == MTYPE_RELOC ||				\
365 		    MNODETYPE_PGCNT(mnode, mtype) == 0 ||		\
366 		    plcnt[mnode][mtype].plc_mt_pgmax != 0);		\
367 		MTYPE_NEXT(mnode, mtype, flags);			\
368 	}								\
369 }
370 
371 /*
372  * if allocation from the RELOC pool failed and there is sufficient cage
373  * memory, attempt to allocate from the NORELOC pool.
374  */
375 #define	MTYPE_NEXT(mnode, mtype, flags) { 				\
376 	if (!(flags & (PG_NORELOC | PGI_NOCAGE | PGI_RELOCONLY)) &&	\
377 	    (kcage_freemem >= kcage_lotsfree)) {			\
378 		if (plcnt[mnode][MTYPE_NORELOC].plc_mt_pgmax == 0) {	\
379 			ASSERT(MNODETYPE_PGCNT(mnode, MTYPE_NORELOC) == 0 || \
380 			    plcnt[mnode][MTYPE_NORELOC].plc_mt_pgmax != 0);  \
381 			mtype = -1;					\
382 		} else {						\
383 			mtype = MTYPE_NORELOC;				\
384 			flags |= PG_NORELOC;				\
385 		}							\
386 	} else {							\
387 		mtype = -1;						\
388 	}								\
389 }
390 
391 /*
392  * get the ecache setsize for the current cpu.
393  */
394 #define	CPUSETSIZE()	(cpunodes[CPU->cpu_id].ecache_setsize)
395 
396 extern struct cpu	cpu0;
397 #define	CPU0		&cpu0
398 
399 #define	PAGE_BSZS_SHIFT(szc)	TTE_BSZS_SHIFT(szc)
400 /*
401  * For sfmmu each larger page is 8 times the size of the previous
402  * size page.
403  */
404 #define	FULL_REGION_CNT(rg_szc)	(8)
405 
406 /*
407  * The counter base must be per page_counter element to prevent
408  * races when re-indexing, and the base page size element should
409  * be aligned on a boundary of the given region size.
410  *
411  * We also round up the number of pages spanned by the counters
412  * for a given region to PC_BASE_ALIGN in certain situations to simplify
413  * the coding for some non-performance critical routines.
414  */
415 #define	PC_BASE_ALIGN		((pfn_t)1 << PAGE_BSZS_SHIFT(mmu_page_sizes-1))
416 #define	PC_BASE_ALIGN_MASK	(PC_BASE_ALIGN - 1)
417 
418 extern int ecache_alignsize;
419 #define	L2CACHE_ALIGN		ecache_alignsize
420 #define	L2CACHE_ALIGN_MAX	512
421 
422 extern int consistent_coloring;
423 extern uint_t vac_colors_mask;
424 extern int vac_size;
425 extern int vac_shift;
426 
427 /*
428  * Kernel mem segment in 64-bit space
429  */
430 extern caddr_t kmem64_base, kmem64_end, kmem64_aligned_end;
431 extern int kmem64_alignsize, kmem64_szc;
432 extern uint64_t kmem64_pabase;
433 extern int max_bootlp_tteszc;
434 
435 /*
436  * Maximum and default values for user heap, stack, private and shared
437  * anonymous memory, and user text and initialized data.
438  *
439  * Initial values are defined in architecture specific mach_vm_dep.c file.
440  * Used by map_pgsz*() routines.
441  */
442 extern size_t max_uheap_lpsize;
443 extern size_t default_uheap_lpsize;
444 extern size_t max_ustack_lpsize;
445 extern size_t default_ustack_lpsize;
446 extern size_t max_privmap_lpsize;
447 extern size_t max_uidata_lpsize;
448 extern size_t max_utext_lpsize;
449 extern size_t max_shm_lpsize;
450 
451 /*
452  * For adjusting the default lpsize, for DTLB-limited page sizes.
453  */
454 extern void adjust_data_maxlpsize(size_t ismpagesize);
455 
456 /*
457  * Sanity control. Don't use large pages regardless of user
458  * settings if there's less than priv or shm_lpg_min_physmem memory installed.
459  * The units for this variable are 8K pages.
460  */
461 extern pgcnt_t privm_lpg_min_physmem;
462 extern pgcnt_t shm_lpg_min_physmem;
463 
464 /*
465  * AS_2_BIN macro controls the page coloring policy.
466  * 0 (default) uses various vaddr bits
467  * 1 virtual=paddr
468  * 2 bin hopping
469  */
470 #define	AS_2_BIN(as, seg, vp, addr, bin, szc)				\
471 switch (consistent_coloring) {						\
472 	default:                                                        \
473 		cmn_err(CE_WARN,					\
474 			"AS_2_BIN: bad consistent coloring value");	\
475 		/* assume default algorithm -> continue */		\
476 	case 0: {                                                       \
477 		uint32_t ndx, new;					\
478 		int slew = 0;						\
479 		pfn_t pfn;                                              \
480                                                                         \
481 		if (vp != NULL && IS_SWAPVP(vp) &&			\
482 		    seg->s_ops == &segvn_ops)				\
483 			slew = as_color_bin(as);			\
484                                                                         \
485 		pfn = ((uintptr_t)addr >> MMU_PAGESHIFT) +		\
486 			(((uintptr_t)addr >> page_coloring_shift) <<	\
487 			(vac_shift - MMU_PAGESHIFT));			\
488 		if ((szc) == 0 || &page_pfn_2_color_cpu == NULL) {	\
489 			pfn += slew;					\
490 			bin = PFN_2_COLOR(pfn, szc);			\
491 		} else {						\
492 			bin = PFN_2_COLOR(pfn, szc);			\
493 			bin += slew >> (vac_shift - MMU_PAGESHIFT);	\
494 			bin &= hw_page_array[(szc)].hp_colors - 1;	\
495 		}							\
496 		break;                                                  \
497 	}                                                               \
498 	case 1:                                                         \
499 		bin = PFN_2_COLOR(((uintptr_t)addr >> MMU_PAGESHIFT),   \
500 					szc);	                        \
501 		break;                                                  \
502 	case 2: {                                                       \
503 		int cnt = as_color_bin(as);				\
504 		uint_t color_mask = page_get_pagecolors(0) - 1;		\
505                                                                         \
506 		/* make sure physical color aligns with vac color */	\
507 		while ((cnt & vac_colors_mask) !=			\
508 		    addr_to_vcolor(addr)) {				\
509 			cnt++;						\
510 		}                                                       \
511 		bin = cnt = cnt & color_mask;			        \
512 		bin >>= PAGE_GET_COLOR_SHIFT(0, szc);                   \
513 		/* update per as page coloring fields */		\
514 		cnt = (cnt + 1) & color_mask;			        \
515 		if (cnt == (as_color_start(as) & color_mask)) {	        \
516 			cnt = as_color_start(as) = as_color_start(as) + \
517 				PGCLR_LOOPFACTOR;			\
518 		}                                                       \
519 		as_color_bin(as) = cnt & color_mask;		        \
520 		break;                                                  \
521 	}								\
522 }									\
523 	ASSERT(bin < page_get_pagecolors(szc));
524 
525 /*
526  * cpu private vm data - accessed thru CPU->cpu_vm_data
527  *	vc_pnum_memseg: tracks last memseg visited in page_numtopp_nolock()
528  *	vc_pnext_memseg: tracks last memseg visited in page_nextn()
529  *	vc_kmptr: unaligned kmem pointer for this vm_cpu_data_t
530  *	vc_kmsize: orignal kmem size for this vm_cpu_data_t
531  */
532 
533 typedef struct {
534 	struct memseg	*vc_pnum_memseg;
535 	struct memseg	*vc_pnext_memseg;
536 	void		*vc_kmptr;
537 	size_t		vc_kmsize;
538 } vm_cpu_data_t;
539 
540 /* allocation size to ensure vm_cpu_data_t resides in its own cache line */
541 #define	VM_CPU_DATA_PADSIZE						\
542 	(P2ROUNDUP(sizeof (vm_cpu_data_t), L2CACHE_ALIGN_MAX))
543 
544 /* for boot cpu before kmem is initialized */
545 extern char	vm_cpu_data0[];
546 
547 /*
548  * Function to get an ecache color bin: F(as, cnt, vcolor).
549  * the goal of this function is to:
550  * - to spread a processes' physical pages across the entire ecache to
551  *	maximize its use.
552  * - to minimize vac flushes caused when we reuse a physical page on a
553  *	different vac color than it was previously used.
554  * - to prevent all processes to use the same exact colors and trash each
555  *	other.
556  *
557  * cnt is a bin ptr kept on a per as basis.  As we page_create we increment
558  * the ptr so we spread out the physical pages to cover the entire ecache.
559  * The virtual color is made a subset of the physical color in order to
560  * in minimize virtual cache flushing.
561  * We add in the as to spread out different as.	 This happens when we
562  * initialize the start count value.
563  * sizeof(struct as) is 60 so we shift by 3 to get into the bit range
564  * that will tend to change.  For example, on spitfire based machines
565  * (vcshft == 1) contigous as are spread bu ~6 bins.
566  * vcshft provides for proper virtual color alignment.
567  * In theory cnt should be updated using cas only but if we are off by one
568  * or 2 it is no big deal.
569  * We also keep a start value which is used to randomize on what bin we
570  * start counting when it is time to start another loop. This avoids
571  * contigous allocations of ecache size to point to the same bin.
572  * Why 3? Seems work ok. Better than 7 or anything larger.
573  */
574 #define	PGCLR_LOOPFACTOR 3
575 
576 /*
577  * When a bin is empty, and we can't satisfy a color request correctly,
578  * we scan.  If we assume that the programs have reasonable spatial
579  * behavior, then it will not be a good idea to use the adjacent color.
580  * Using the adjacent color would result in virtually adjacent addresses
581  * mapping into the same spot in the cache.  So, if we stumble across
582  * an empty bin, skip a bunch before looking.  After the first skip,
583  * then just look one bin at a time so we don't miss our cache on
584  * every look. Be sure to check every bin.  Page_create() will panic
585  * if we miss a page.
586  *
587  * This also explains the `<=' in the for loops in both page_get_freelist()
588  * and page_get_cachelist().  Since we checked the target bin, skipped
589  * a bunch, then continued one a time, we wind up checking the target bin
590  * twice to make sure we get all of them bins.
591  */
592 #define	BIN_STEP	20
593 
594 #ifdef VM_STATS
595 struct vmm_vmstats_str {
596 	ulong_t pgf_alloc[MMU_PAGE_SIZES];	/* page_get_freelist */
597 	ulong_t pgf_allocok[MMU_PAGE_SIZES];
598 	ulong_t pgf_allocokrem[MMU_PAGE_SIZES];
599 	ulong_t pgf_allocfailed[MMU_PAGE_SIZES];
600 	ulong_t pgf_allocdeferred;
601 	ulong_t	pgf_allocretry[MMU_PAGE_SIZES];
602 	ulong_t pgc_alloc;			/* page_get_cachelist */
603 	ulong_t pgc_allocok;
604 	ulong_t pgc_allocokrem;
605 	ulong_t	pgc_allocokdeferred;
606 	ulong_t pgc_allocfailed;
607 	ulong_t	pgcp_alloc[MMU_PAGE_SIZES];	/* page_get_contig_pages */
608 	ulong_t	pgcp_allocfailed[MMU_PAGE_SIZES];
609 	ulong_t	pgcp_allocempty[MMU_PAGE_SIZES];
610 	ulong_t	pgcp_allocok[MMU_PAGE_SIZES];
611 	ulong_t	ptcp[MMU_PAGE_SIZES];		/* page_trylock_contig_pages */
612 	ulong_t	ptcpfreethresh[MMU_PAGE_SIZES];
613 	ulong_t	ptcpfailexcl[MMU_PAGE_SIZES];
614 	ulong_t	ptcpfailszc[MMU_PAGE_SIZES];
615 	ulong_t	ptcpfailcage[MMU_PAGE_SIZES];
616 	ulong_t	ptcpok[MMU_PAGE_SIZES];
617 	ulong_t	pgmf_alloc[MMU_PAGE_SIZES];	/* page_get_mnode_freelist */
618 	ulong_t	pgmf_allocfailed[MMU_PAGE_SIZES];
619 	ulong_t	pgmf_allocempty[MMU_PAGE_SIZES];
620 	ulong_t	pgmf_allocok[MMU_PAGE_SIZES];
621 	ulong_t	pgmc_alloc;			/* page_get_mnode_cachelist */
622 	ulong_t	pgmc_allocfailed;
623 	ulong_t	pgmc_allocempty;
624 	ulong_t	pgmc_allocok;
625 	ulong_t	pladd_free[MMU_PAGE_SIZES];	/* page_list_add/sub */
626 	ulong_t	plsub_free[MMU_PAGE_SIZES];
627 	ulong_t	pladd_cache;
628 	ulong_t	plsub_cache;
629 	ulong_t	plsubpages_szcbig;
630 	ulong_t	plsubpages_szc0;
631 	ulong_t	pfs_req[MMU_PAGE_SIZES];	/* page_freelist_split */
632 	ulong_t	pfs_demote[MMU_PAGE_SIZES];
633 	ulong_t	pfc_coalok[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
634 	ulong_t ppr_reloc[MMU_PAGE_SIZES];	/* page_relocate */
635 	ulong_t ppr_relocok[MMU_PAGE_SIZES];
636 	ulong_t ppr_relocnoroot[MMU_PAGE_SIZES];
637 	ulong_t ppr_reloc_replnoroot[MMU_PAGE_SIZES];
638 	ulong_t ppr_relocnolock[MMU_PAGE_SIZES];
639 	ulong_t ppr_relocnomem[MMU_PAGE_SIZES];
640 	ulong_t ppr_krelocfail[MMU_PAGE_SIZES];
641 	ulong_t ppr_copyfail;
642 	/* page coalesce counter */
643 	ulong_t	page_ctrs_coalesce[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
644 	/* candidates useful */
645 	ulong_t	page_ctrs_cands_skip[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
646 	/* ctrs changed after locking */
647 	ulong_t	page_ctrs_changed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
648 	/* page_freelist_coalesce failed */
649 	ulong_t	page_ctrs_failed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
650 	ulong_t	page_ctrs_coalesce_all;	/* page coalesce all counter */
651 	ulong_t	page_ctrs_cands_skip_all; /* candidates useful for all func */
652 };
653 extern struct vmm_vmstats_str vmm_vmstats;
654 #endif	/* VM_STATS */
655 
656 /*
657  * Used to hold off page relocations into the cage until OBP has completed
658  * its boot-time handoff of its resources to the kernel.
659  */
660 extern int page_relocate_ready;
661 
662 /*
663  * cpu/mmu-dependent vm variables may be reset at bootup.
664  */
665 extern uint_t mmu_page_sizes;
666 extern uint_t max_mmu_page_sizes;
667 extern uint_t mmu_hashcnt;
668 extern uint_t max_mmu_hashcnt;
669 extern size_t mmu_ism_pagesize;
670 extern int mmu_exported_pagesize_mask;
671 extern uint_t mmu_exported_page_sizes;
672 extern uint_t szc_2_userszc[];
673 extern uint_t userszc_2_szc[];
674 
675 #define	USERSZC_2_SZC(userszc)	(userszc_2_szc[userszc])
676 #define	SZC_2_USERSZC(szc)	(szc_2_userszc[szc])
677 
678 /*
679  * Platform specific page routines
680  */
681 extern void mach_page_add(page_t **, page_t *);
682 extern void mach_page_sub(page_t **, page_t *);
683 extern uint_t page_get_pagecolors(uint_t);
684 extern void ppcopy_kernel__relocatable(page_t *, page_t *);
685 #define	ppcopy_kernel(p1, p2)	ppcopy_kernel__relocatable(p1, p2)
686 
687 /*
688  * platform specific large pages for kernel heap support
689  */
690 extern size_t get_segkmem_lpsize(size_t lpsize);
691 extern size_t mmu_get_kernel_lpsize(size_t lpsize);
692 extern void mmu_init_kernel_pgsz(struct hat *hat);
693 extern void mmu_init_kcontext();
694 extern uint64_t kcontextreg;
695 
696 #ifdef	__cplusplus
697 }
698 #endif
699 
700 #endif	/* _VM_DEP_H */
701