xref: /illumos-gate/usr/src/uts/sun4/vm/vm_dep.h (revision 437220cd296f6d8b6654d6d52508b40b1e2d1ac7)
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  * Iterator provides the info needed to convert RA to PA.
111  * MEM_NODE_ITERATOR_INIT() should be called before
112  * PAGE_NEXT_PFN_FOR_COLOR() if pfn was not obtained via a previous
113  * PAGE_NEXT_PFN_FOR_COLOR() call. Iterator caches color 2 hash
114  * translations requiring initializer call if color or ceq_mask changes,
115  * even if pfn doesn't. MEM_NODE_ITERATOR_INIT() must also be called before
116  * PFN_2_COLOR() that uses a valid iterator argument.
117  */
118 #ifdef	sun4v
119 
120 typedef struct mem_node_iterator {
121 	uint_t mi_mnode;		/* mnode in which to iterate */
122 	int mi_init;			/* set to 1 when first init */
123 	int mi_last_mblock;		/* last mblock visited */
124 	uint_t mi_hash_ceq_mask;	/* cached copy of ceq_mask */
125 	uint_t mi_hash_color;		/* cached copy of color */
126 	uint_t mi_mnode_mask;		/* number of mask bits */
127 	uint_t mi_mnode_pfn_shift;	/* mnode position in pfn */
128 	pfn_t mi_mblock_base;		/* first valid pfn in current mblock */
129 	pfn_t mi_mblock_end;		/* last valid pfn in current mblock */
130 	pfn_t mi_ra_to_pa;		/* ra adjustment for current mblock */
131 	pfn_t mi_mnode_pfn_mask;	/* mask to obtain mnode id bits */
132 } mem_node_iterator_t;
133 
134 #define	MEM_NODE_ITERATOR_DECL(it) \
135 	mem_node_iterator_t it
136 #define	MEM_NODE_ITERATOR_INIT(pfn, mnode, it) \
137 	(pfn) = plat_mem_node_iterator_init((pfn), (mnode), (it), 1)
138 
139 extern pfn_t plat_mem_node_iterator_init(pfn_t, int,
140     mem_node_iterator_t *, int);
141 extern pfn_t plat_rapfn_to_papfn(pfn_t);
142 extern int interleaved_mnodes;
143 
144 #else	/* sun4v */
145 
146 #define	MEM_NODE_ITERATOR_DECL(it) \
147 	void *it = NULL
148 #define	MEM_NODE_ITERATOR_INIT(pfn, mnode, it)
149 
150 #endif	/* sun4v */
151 
152 /*
153  * Return the mnode limits so that hpc_counters length and base
154  * index can be determined. When interleaved_mnodes is set, we
155  * create an array only for the first mnode that exists. All other
156  * mnodes will share the array in this case.
157  * If interleaved_mnodes is not set, simply return the limits for
158  * the given mnode.
159  */
160 #define	HPM_COUNTERS_LIMITS(mnode, physbase, physmax, first)		\
161 	if (!interleaved_mnodes) {					\
162 		(physbase) = mem_node_config[(mnode)].physbase;		\
163 		(physmax) = mem_node_config[(mnode)].physmax;		\
164 		(first) = (mnode);					\
165 	} else if ((first) < 0) {					\
166 		mem_node_max_range(&(physbase), &(physmax));		\
167 		(first) = (mnode);					\
168 	}
169 
170 #define	PAGE_CTRS_WRITE_LOCK(mnode)					\
171 	if (!interleaved_mnodes) {					\
172 		rw_enter(&page_ctrs_rwlock[(mnode)], RW_WRITER);	\
173 		page_freelist_lock(mnode);				\
174 	} else {							\
175 		/* changing shared hpm_counters */			\
176 		int _i;							\
177 		for (_i = 0; _i < max_mem_nodes; _i++) {		\
178 			rw_enter(&page_ctrs_rwlock[_i], RW_WRITER);	\
179 			page_freelist_lock(_i);				\
180 		}							\
181 	}
182 
183 #define	PAGE_CTRS_WRITE_UNLOCK(mnode)					\
184 	if (!interleaved_mnodes) {					\
185 		page_freelist_unlock(mnode);				\
186 		rw_exit(&page_ctrs_rwlock[(mnode)]);			\
187 	} else {							\
188 		int _i;							\
189 		for (_i = 0; _i < max_mem_nodes; _i++) {		\
190 			page_freelist_unlock(_i);			\
191 			rw_exit(&page_ctrs_rwlock[_i]);			\
192 		}							\
193 	}
194 
195 /*
196  * cpu specific color conversion functions
197  */
198 extern uint_t page_get_nsz_color_mask_cpu(uchar_t, uint_t);
199 #pragma weak page_get_nsz_color_mask_cpu
200 
201 extern uint_t page_get_nsz_color_cpu(uchar_t, uint_t);
202 #pragma weak page_get_nsz_color_cpu
203 
204 extern uint_t page_get_color_shift_cpu(uchar_t, uchar_t);
205 #pragma weak page_get_color_shift_cpu
206 
207 extern uint_t page_convert_color_cpu(uint_t, uchar_t, uchar_t);
208 #pragma weak page_convert_color_cpu
209 
210 extern pfn_t page_next_pfn_for_color_cpu(pfn_t,
211     uchar_t, uint_t, uint_t, uint_t, void *);
212 #pragma weak page_next_pfn_for_color_cpu
213 
214 extern uint_t  page_pfn_2_color_cpu(pfn_t, uchar_t, void *);
215 #pragma weak page_pfn_2_color_cpu
216 
217 #define	PAGE_GET_COLOR_SHIFT(szc, nszc)				\
218 	((&page_get_color_shift_cpu != NULL) ?			\
219 	    page_get_color_shift_cpu(szc, nszc) :		\
220 	    (hw_page_array[(nszc)].hp_shift -			\
221 		hw_page_array[(szc)].hp_shift))
222 
223 #define	PAGE_CONVERT_COLOR(ncolor, szc, nszc)			\
224 	((&page_convert_color_cpu != NULL) ?			\
225 	    page_convert_color_cpu(ncolor, szc, nszc) :		\
226 	    ((ncolor) << PAGE_GET_COLOR_SHIFT((szc), (nszc))))
227 
228 #define	PFN_2_COLOR(pfn, szc, it)				\
229 	((&page_pfn_2_color_cpu != NULL) ?			\
230 	    page_pfn_2_color_cpu(pfn, szc, it) :		\
231 	    ((pfn & (hw_page_array[0].hp_colors - 1)) >>	\
232 		(hw_page_array[szc].hp_shift -			\
233 		    hw_page_array[0].hp_shift)))
234 
235 #define	PNUM_SIZE(szc)							\
236 	(hw_page_array[(szc)].hp_pgcnt)
237 #define	PNUM_SHIFT(szc)							\
238 	(hw_page_array[(szc)].hp_shift - hw_page_array[0].hp_shift)
239 #define	PAGE_GET_SHIFT(szc)						\
240 	(hw_page_array[(szc)].hp_shift)
241 #define	PAGE_GET_PAGECOLORS(szc)					\
242 	(hw_page_array[(szc)].hp_colors)
243 
244 /*
245  * This macro calculates the next sequential pfn with the specified
246  * color using color equivalency mask
247  */
248 #define	PAGE_NEXT_PFN_FOR_COLOR(pfn, szc, color, ceq_mask, color_mask, it)    \
249 	ASSERT(((color) & ~(ceq_mask)) == 0);                                 \
250 	if (&page_next_pfn_for_color_cpu == NULL) {                           \
251 		uint_t	pfn_shift = PAGE_BSZS_SHIFT(szc);                     \
252 		pfn_t	spfn = pfn >> pfn_shift;                              \
253 		pfn_t	stride = (ceq_mask) + 1;                              \
254 		ASSERT((((ceq_mask) + 1) & (ceq_mask)) == 0);                 \
255 		if (((spfn ^ (color)) & (ceq_mask)) == 0) {                   \
256 			pfn += stride << pfn_shift;                           \
257 		} else {                                                      \
258 			pfn = (spfn & ~(pfn_t)(ceq_mask)) | (color);          \
259 			pfn = (pfn > spfn ? pfn : pfn + stride) << pfn_shift; \
260 		}                                                             \
261 	} else {                                                              \
262 	    pfn = page_next_pfn_for_color_cpu(pfn, szc, color,		      \
263 		ceq_mask, color_mask, it);				      \
264 	}
265 
266 /* get the color equivalency mask for the next szc */
267 #define	PAGE_GET_NSZ_MASK(szc, mask)                                         \
268 	((&page_get_nsz_color_mask_cpu == NULL) ?                            \
269 	    ((mask) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc))) :  \
270 	    page_get_nsz_color_mask_cpu(szc, mask))
271 
272 /* get the color of the next szc */
273 #define	PAGE_GET_NSZ_COLOR(szc, color)                                       \
274 	((&page_get_nsz_color_cpu == NULL) ?                                 \
275 	    ((color) >> (PAGE_GET_SHIFT((szc) + 1) - PAGE_GET_SHIFT(szc))) : \
276 	    page_get_nsz_color_cpu(szc, color))
277 
278 /* Find the bin for the given page if it was of size szc */
279 #define	PP_2_BIN_SZC(pp, szc)	(PFN_2_COLOR(pp->p_pagenum, szc, (void *)(-1)))
280 
281 #define	PP_2_BIN(pp)		(PP_2_BIN_SZC(pp, pp->p_szc))
282 
283 #define	PP_2_MEM_NODE(pp)	(PFN_2_MEM_NODE(pp->p_pagenum))
284 
285 #define	PC_BIN_MUTEX(mnode, bin, flags) ((flags & PG_FREE_LIST) ?	\
286 	&fpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode] :			\
287 	&cpc_mutex[(bin) & (NPC_MUTEX - 1)][mnode])
288 
289 #define	FPC_MUTEX(mnode, i)	(&fpc_mutex[i][mnode])
290 #define	CPC_MUTEX(mnode, i)	(&cpc_mutex[i][mnode])
291 
292 #define	PFN_BASE(pfnum, szc)	(pfnum & ~((1 << PAGE_BSZS_SHIFT(szc)) - 1))
293 
294 /*
295  * this structure is used for walking free page lists
296  * controls when to split large pages into smaller pages,
297  * and when to coalesce smaller pages into larger pages
298  */
299 typedef struct page_list_walker {
300 	uint_t	plw_colors;		/* num of colors for szc */
301 	uint_t  plw_color_mask;		/* colors-1 */
302 	uint_t	plw_bin_step;		/* next bin: 1 or 2 */
303 	uint_t  plw_count;		/* loop count */
304 	uint_t	plw_bin0;		/* starting bin */
305 	uint_t  plw_bin_marker;		/* bin after initial jump */
306 	uint_t  plw_bin_split_prev;	/* last bin we tried to split */
307 	uint_t  plw_do_split;		/* set if OK to split */
308 	uint_t  plw_split_next;		/* next bin to split */
309 	uint_t	plw_ceq_dif;		/* number of different color groups */
310 					/* to check */
311 	uint_t	plw_ceq_mask[MMU_PAGE_SIZES + 1]; /* color equiv mask */
312 	uint_t	plw_bins[MMU_PAGE_SIZES + 1];	/* num of bins */
313 } page_list_walker_t;
314 
315 void	page_list_walk_init(uchar_t szc, uint_t flags, uint_t bin,
316     int can_split, int use_ceq, page_list_walker_t *plw);
317 
318 typedef	char	hpmctr_t;
319 
320 #ifdef DEBUG
321 #define	CHK_LPG(pp, szc)	chk_lpg(pp, szc)
322 extern void	chk_lpg(page_t *, uchar_t);
323 #else
324 #define	CHK_LPG(pp, szc)
325 #endif
326 
327 /*
328  * page list count per mnode and type.
329  */
330 typedef	struct {
331 	pgcnt_t	plc_mt_pgmax;		/* max page cnt */
332 	pgcnt_t plc_mt_clpgcnt;		/* cache list cnt */
333 	pgcnt_t plc_mt_flpgcnt;		/* free list cnt - small pages */
334 	pgcnt_t plc_mt_lgpgcnt;		/* free list cnt - large pages */
335 #ifdef DEBUG
336 	struct {
337 		pgcnt_t plc_mts_pgcnt;	/* per page size count */
338 		int	plc_mts_colors;
339 		pgcnt_t	*plc_mtsc_pgcnt; /* per color bin count */
340 	} plc_mts[MMU_PAGE_SIZES];
341 #endif
342 } plcnt_t[MAX_MEM_NODES][MAX_MEM_TYPES];
343 
344 #ifdef DEBUG
345 
346 #define	PLCNT_SZ(ctrs_sz) {						\
347 	int	szc;							\
348 	for (szc = 0; szc < mmu_page_sizes; szc++) {			\
349 		int	colors = page_get_pagecolors(szc);		\
350 		ctrs_sz += (max_mem_nodes * MAX_MEM_TYPES *		\
351 		    colors * sizeof (pgcnt_t));				\
352 	}								\
353 }
354 
355 #define	PLCNT_INIT(base) {						\
356 	int	mn, mt, szc, colors;					\
357 	for (szc = 0; szc < mmu_page_sizes; szc++) {			\
358 		colors = page_get_pagecolors(szc);			\
359 		for (mn = 0; mn < max_mem_nodes; mn++) {		\
360 			for (mt = 0; mt < MAX_MEM_TYPES; mt++) {	\
361 				plcnt[mn][mt].plc_mts[szc].		\
362 				    plc_mts_colors = colors;		\
363 				plcnt[mn][mt].plc_mts[szc].		\
364 				    plc_mtsc_pgcnt = (pgcnt_t *)base;	\
365 				base += (colors * sizeof (pgcnt_t));	\
366 			}						\
367 		}							\
368 	}								\
369 }
370 
371 #define	PLCNT_DO(pp, mn, mtype, szc, cnt, flags) {			\
372 	int	bin = PP_2_BIN(pp);					\
373 	if (flags & PG_CACHE_LIST)					\
374 		atomic_add_long(&plcnt[mn][mtype].plc_mt_clpgcnt, cnt);	\
375 	else if (szc)							\
376 		atomic_add_long(&plcnt[mn][mtype].plc_mt_lgpgcnt, cnt);	\
377 	else								\
378 		atomic_add_long(&plcnt[mn][mtype].plc_mt_flpgcnt, cnt);	\
379 	atomic_add_long(&plcnt[mn][mtype].plc_mts[szc].plc_mts_pgcnt,	\
380 	    cnt);							\
381 	atomic_add_long(&plcnt[mn][mtype].plc_mts[szc].			\
382 	    plc_mtsc_pgcnt[bin], cnt);					\
383 }
384 
385 #else
386 
387 #define	PLCNT_SZ(ctrs_sz)
388 
389 #define	PLCNT_INIT(base)
390 
391 /* PG_FREE_LIST may not be explicitly set in flags for large pages */
392 
393 #define	PLCNT_DO(pp, mn, mtype, szc, cnt, flags) {			\
394 	if (flags & PG_CACHE_LIST)					\
395 		atomic_add_long(&plcnt[mn][mtype].plc_mt_clpgcnt, cnt);	\
396 	else if (szc)							\
397 		atomic_add_long(&plcnt[mn][mtype].plc_mt_lgpgcnt, cnt);	\
398 	else								\
399 		atomic_add_long(&plcnt[mn][mtype].plc_mt_flpgcnt, cnt);	\
400 }
401 
402 #endif
403 
404 #define	PLCNT_INCR(pp, mn, mtype, szc, flags) {				\
405 	long	cnt = (1 << PAGE_BSZS_SHIFT(szc));			\
406 	PLCNT_DO(pp, mn, mtype, szc, cnt, flags);			\
407 }
408 
409 #define	PLCNT_DECR(pp, mn, mtype, szc, flags) {				\
410 	long	cnt = ((-1) << PAGE_BSZS_SHIFT(szc));			\
411 	PLCNT_DO(pp, mn, mtype, szc, cnt, flags);			\
412 }
413 
414 /*
415  * macros to update page list max counts - done when pages transferred
416  * from RELOC to NORELOC mtype (kcage_init or kcage_assimilate_page).
417  */
418 
419 #define	PLCNT_XFER_NORELOC(pp) {					\
420 	long	cnt = (1 << PAGE_BSZS_SHIFT((pp)->p_szc));		\
421 	int	mn = PP_2_MEM_NODE(pp);					\
422 	atomic_add_long(&plcnt[mn][MTYPE_NORELOC].plc_mt_pgmax, cnt);	\
423 	atomic_add_long(&plcnt[mn][MTYPE_RELOC].plc_mt_pgmax, -cnt);	\
424 }
425 
426 /*
427  * macro to modify the page list max counts when memory is added to
428  * the page lists during startup (add_physmem) or during a DR operation
429  * when memory is added (kphysm_add_memory_dynamic) or deleted
430  * (kphysm_del_cleanup).
431  */
432 #define	PLCNT_MODIFY_MAX(pfn, cnt) {					       \
433 	spgcnt_t _cnt = (spgcnt_t)(cnt);				       \
434 	pgcnt_t _acnt = ABS(_cnt);					       \
435 	int _mn;							       \
436 	pgcnt_t _np;							       \
437 	if (&plat_mem_node_intersect_range != NULL) {			       \
438 		for (_mn = 0; _mn < max_mem_nodes; _mn++) {		       \
439 			plat_mem_node_intersect_range((pfn), _acnt, _mn, &_np);\
440 			if (_np == 0)					       \
441 				continue;				       \
442 			atomic_add_long(&plcnt[_mn][MTYPE_RELOC].plc_mt_pgmax, \
443 			    (_cnt < 0) ? -_np : _np);			       \
444 		}							       \
445 	} else {							       \
446 		pfn_t _pfn = (pfn);					       \
447 		pfn_t _endpfn = _pfn + _acnt;				       \
448 		while (_pfn < _endpfn) {				       \
449 			_mn = PFN_2_MEM_NODE(_pfn);			       \
450 			_np = MIN(_endpfn, mem_node_config[_mn].physmax + 1) - \
451 			    _pfn;					       \
452 			_pfn += _np;					       \
453 			atomic_add_long(&plcnt[_mn][MTYPE_RELOC].plc_mt_pgmax, \
454 			    (_cnt < 0) ? -_np : _np);			       \
455 		}							       \
456 	}								       \
457 }
458 
459 extern plcnt_t	plcnt;
460 
461 #define	MNODE_PGCNT(mn)							\
462 	(plcnt[mn][MTYPE_RELOC].plc_mt_clpgcnt +			\
463 	    plcnt[mn][MTYPE_NORELOC].plc_mt_clpgcnt +			\
464 	    plcnt[mn][MTYPE_RELOC].plc_mt_flpgcnt +			\
465 	    plcnt[mn][MTYPE_NORELOC].plc_mt_flpgcnt +			\
466 	    plcnt[mn][MTYPE_RELOC].plc_mt_lgpgcnt +			\
467 	    plcnt[mn][MTYPE_NORELOC].plc_mt_lgpgcnt)
468 
469 #define	MNODETYPE_PGCNT(mn, mtype)					\
470 	(plcnt[mn][mtype].plc_mt_clpgcnt +				\
471 	    plcnt[mn][mtype].plc_mt_flpgcnt +				\
472 	    plcnt[mn][mtype].plc_mt_lgpgcnt)
473 
474 /*
475  * macros to loop through the mtype range - MTYPE_START returns -1 in
476  * mtype if no pages in mnode/mtype and possibly NEXT mtype.
477  */
478 #define	MTYPE_START(mnode, mtype, flags) {				\
479 	if (plcnt[mnode][mtype].plc_mt_pgmax == 0) {			\
480 		ASSERT(mtype == MTYPE_RELOC ||				\
481 		    MNODETYPE_PGCNT(mnode, mtype) == 0 ||		\
482 		    plcnt[mnode][mtype].plc_mt_pgmax != 0);		\
483 		MTYPE_NEXT(mnode, mtype, flags);			\
484 	}								\
485 }
486 
487 /*
488  * if allocation from the RELOC pool failed and there is sufficient cage
489  * memory, attempt to allocate from the NORELOC pool.
490  */
491 #define	MTYPE_NEXT(mnode, mtype, flags) { 				\
492 	if (!(flags & (PG_NORELOC | PGI_NOCAGE | PGI_RELOCONLY)) &&	\
493 	    (kcage_freemem >= kcage_lotsfree)) {			\
494 		if (plcnt[mnode][MTYPE_NORELOC].plc_mt_pgmax == 0) {	\
495 			ASSERT(MNODETYPE_PGCNT(mnode, MTYPE_NORELOC) == 0 || \
496 			    plcnt[mnode][MTYPE_NORELOC].plc_mt_pgmax != 0);  \
497 			mtype = -1;					\
498 		} else {						\
499 			mtype = MTYPE_NORELOC;				\
500 			flags |= PG_NORELOC;				\
501 		}							\
502 	} else {							\
503 		mtype = -1;						\
504 	}								\
505 }
506 
507 /*
508  * get the ecache setsize for the current cpu.
509  */
510 #define	CPUSETSIZE()	(cpunodes[CPU->cpu_id].ecache_setsize)
511 
512 extern struct cpu	cpu0;
513 #define	CPU0		&cpu0
514 
515 #define	PAGE_BSZS_SHIFT(szc)	TTE_BSZS_SHIFT(szc)
516 /*
517  * For sfmmu each larger page is 8 times the size of the previous
518  * size page.
519  */
520 #define	FULL_REGION_CNT(rg_szc)	(8)
521 
522 /*
523  * The counter base must be per page_counter element to prevent
524  * races when re-indexing, and the base page size element should
525  * be aligned on a boundary of the given region size.
526  *
527  * We also round up the number of pages spanned by the counters
528  * for a given region to PC_BASE_ALIGN in certain situations to simplify
529  * the coding for some non-performance critical routines.
530  */
531 #define	PC_BASE_ALIGN		((pfn_t)1 << PAGE_BSZS_SHIFT(mmu_page_sizes-1))
532 #define	PC_BASE_ALIGN_MASK	(PC_BASE_ALIGN - 1)
533 
534 extern int ecache_alignsize;
535 #define	L2CACHE_ALIGN		ecache_alignsize
536 #define	L2CACHE_ALIGN_MAX	512
537 
538 extern int update_proc_pgcolorbase_after_fork;
539 extern int consistent_coloring;
540 extern uint_t vac_colors_mask;
541 extern int vac_size;
542 extern int vac_shift;
543 
544 /*
545  * Kernel mem segment in 64-bit space
546  */
547 extern caddr_t kmem64_base, kmem64_end, kmem64_aligned_end;
548 extern int kmem64_alignsize, kmem64_szc;
549 extern uint64_t kmem64_pabase;
550 extern int max_bootlp_tteszc;
551 
552 /*
553  * Maximum and default values for user heap, stack, private and shared
554  * anonymous memory, and user text and initialized data.
555  *
556  * Initial values are defined in architecture specific mach_vm_dep.c file.
557  * Used by map_pgsz*() routines.
558  */
559 extern size_t max_uheap_lpsize;
560 extern size_t default_uheap_lpsize;
561 extern size_t max_ustack_lpsize;
562 extern size_t default_ustack_lpsize;
563 extern size_t max_privmap_lpsize;
564 extern size_t max_uidata_lpsize;
565 extern size_t max_utext_lpsize;
566 extern size_t max_shm_lpsize;
567 
568 /*
569  * For adjusting the default lpsize, for DTLB-limited page sizes.
570  */
571 extern void adjust_data_maxlpsize(size_t ismpagesize);
572 
573 /*
574  * Sanity control. Don't use large pages regardless of user
575  * settings if there's less than priv or shm_lpg_min_physmem memory installed.
576  * The units for this variable are 8K pages.
577  */
578 extern pgcnt_t privm_lpg_min_physmem;
579 extern pgcnt_t shm_lpg_min_physmem;
580 
581 /*
582  * AS_2_BIN macro controls the page coloring policy.
583  * 0 (default) uses various vaddr bits
584  * 1 virtual=paddr
585  * 2 bin hopping
586  */
587 #define	AS_2_BIN(as, seg, vp, addr, bin, szc)				\
588 switch (consistent_coloring) {						\
589 	default:                                                        \
590 		cmn_err(CE_WARN,					\
591 			"AS_2_BIN: bad consistent coloring value");	\
592 		/* assume default algorithm -> continue */		\
593 	case 0: {                                                       \
594 		uint32_t ndx, new;					\
595 		int slew = 0;						\
596 		pfn_t pfn;                                              \
597                                                                         \
598 		if (vp != NULL && IS_SWAPVP(vp) &&			\
599 		    seg->s_ops == &segvn_ops)				\
600 			slew = as_color_bin(as);			\
601                                                                         \
602 		pfn = ((uintptr_t)addr >> MMU_PAGESHIFT) +		\
603 			(((uintptr_t)addr >> page_coloring_shift) <<	\
604 			(vac_shift - MMU_PAGESHIFT));			\
605 		if ((szc) == 0 || &page_pfn_2_color_cpu == NULL) {	\
606 			pfn += slew;					\
607 			bin = PFN_2_COLOR(pfn, szc, NULL);		\
608 		} else {						\
609 			bin = PFN_2_COLOR(pfn, szc, NULL);		\
610 			bin += slew >> (vac_shift - MMU_PAGESHIFT);	\
611 			bin &= hw_page_array[(szc)].hp_colors - 1;	\
612 		}							\
613 		break;                                                  \
614 	}                                                               \
615 	case 1:                                                         \
616 		bin = PFN_2_COLOR(((uintptr_t)addr >> MMU_PAGESHIFT),	\
617 		    szc, NULL);						\
618 		break;                                                  \
619 	case 2: {                                                       \
620 		int cnt = as_color_bin(as);				\
621 		uint_t color_mask = page_get_pagecolors(0) - 1;		\
622                                                                         \
623 		/* make sure physical color aligns with vac color */	\
624 		while ((cnt & vac_colors_mask) !=			\
625 		    addr_to_vcolor(addr)) {				\
626 			cnt++;						\
627 		}                                                       \
628 		bin = cnt = cnt & color_mask;			        \
629 		bin >>= PAGE_GET_COLOR_SHIFT(0, szc);                   \
630 		/* update per as page coloring fields */		\
631 		cnt = (cnt + 1) & color_mask;			        \
632 		if (cnt == (as_color_start(as) & color_mask)) {	        \
633 			cnt = as_color_start(as) = as_color_start(as) + \
634 				PGCLR_LOOPFACTOR;			\
635 		}                                                       \
636 		as_color_bin(as) = cnt & color_mask;		        \
637 		break;                                                  \
638 	}								\
639 }									\
640 	ASSERT(bin < page_get_pagecolors(szc));
641 
642 /*
643  * cpu private vm data - accessed thru CPU->cpu_vm_data
644  *	vc_pnum_memseg: tracks last memseg visited in page_numtopp_nolock()
645  *	vc_pnext_memseg: tracks last memseg visited in page_nextn()
646  *	vc_kmptr: unaligned kmem pointer for this vm_cpu_data_t
647  *	vc_kmsize: orignal kmem size for this vm_cpu_data_t
648  */
649 
650 typedef struct {
651 	struct memseg	*vc_pnum_memseg;
652 	struct memseg	*vc_pnext_memseg;
653 	void		*vc_kmptr;
654 	size_t		vc_kmsize;
655 } vm_cpu_data_t;
656 
657 /* allocation size to ensure vm_cpu_data_t resides in its own cache line */
658 #define	VM_CPU_DATA_PADSIZE						\
659 	(P2ROUNDUP(sizeof (vm_cpu_data_t), L2CACHE_ALIGN_MAX))
660 
661 /* for boot cpu before kmem is initialized */
662 extern char	vm_cpu_data0[];
663 
664 /*
665  * Function to get an ecache color bin: F(as, cnt, vcolor).
666  * the goal of this function is to:
667  * - to spread a processes' physical pages across the entire ecache to
668  *	maximize its use.
669  * - to minimize vac flushes caused when we reuse a physical page on a
670  *	different vac color than it was previously used.
671  * - to prevent all processes to use the same exact colors and trash each
672  *	other.
673  *
674  * cnt is a bin ptr kept on a per as basis.  As we page_create we increment
675  * the ptr so we spread out the physical pages to cover the entire ecache.
676  * The virtual color is made a subset of the physical color in order to
677  * in minimize virtual cache flushing.
678  * We add in the as to spread out different as.	 This happens when we
679  * initialize the start count value.
680  * sizeof(struct as) is 60 so we shift by 3 to get into the bit range
681  * that will tend to change.  For example, on spitfire based machines
682  * (vcshft == 1) contigous as are spread bu ~6 bins.
683  * vcshft provides for proper virtual color alignment.
684  * In theory cnt should be updated using cas only but if we are off by one
685  * or 2 it is no big deal.
686  * We also keep a start value which is used to randomize on what bin we
687  * start counting when it is time to start another loop. This avoids
688  * contigous allocations of ecache size to point to the same bin.
689  * Why 3? Seems work ok. Better than 7 or anything larger.
690  */
691 #define	PGCLR_LOOPFACTOR 3
692 
693 /*
694  * When a bin is empty, and we can't satisfy a color request correctly,
695  * we scan.  If we assume that the programs have reasonable spatial
696  * behavior, then it will not be a good idea to use the adjacent color.
697  * Using the adjacent color would result in virtually adjacent addresses
698  * mapping into the same spot in the cache.  So, if we stumble across
699  * an empty bin, skip a bunch before looking.  After the first skip,
700  * then just look one bin at a time so we don't miss our cache on
701  * every look. Be sure to check every bin.  Page_create() will panic
702  * if we miss a page.
703  *
704  * This also explains the `<=' in the for loops in both page_get_freelist()
705  * and page_get_cachelist().  Since we checked the target bin, skipped
706  * a bunch, then continued one a time, we wind up checking the target bin
707  * twice to make sure we get all of them bins.
708  */
709 #define	BIN_STEP	20
710 
711 #ifdef VM_STATS
712 struct vmm_vmstats_str {
713 	ulong_t pgf_alloc[MMU_PAGE_SIZES];	/* page_get_freelist */
714 	ulong_t pgf_allocok[MMU_PAGE_SIZES];
715 	ulong_t pgf_allocokrem[MMU_PAGE_SIZES];
716 	ulong_t pgf_allocfailed[MMU_PAGE_SIZES];
717 	ulong_t pgf_allocdeferred;
718 	ulong_t	pgf_allocretry[MMU_PAGE_SIZES];
719 	ulong_t pgc_alloc;			/* page_get_cachelist */
720 	ulong_t pgc_allocok;
721 	ulong_t pgc_allocokrem;
722 	ulong_t	pgc_allocokdeferred;
723 	ulong_t pgc_allocfailed;
724 	ulong_t	pgcp_alloc[MMU_PAGE_SIZES];	/* page_get_contig_pages */
725 	ulong_t	pgcp_allocfailed[MMU_PAGE_SIZES];
726 	ulong_t	pgcp_allocempty[MMU_PAGE_SIZES];
727 	ulong_t	pgcp_allocok[MMU_PAGE_SIZES];
728 	ulong_t	ptcp[MMU_PAGE_SIZES];		/* page_trylock_contig_pages */
729 	ulong_t	ptcpfreethresh[MMU_PAGE_SIZES];
730 	ulong_t	ptcpfailexcl[MMU_PAGE_SIZES];
731 	ulong_t	ptcpfailszc[MMU_PAGE_SIZES];
732 	ulong_t	ptcpfailcage[MMU_PAGE_SIZES];
733 	ulong_t	ptcpok[MMU_PAGE_SIZES];
734 	ulong_t	pgmf_alloc[MMU_PAGE_SIZES];	/* page_get_mnode_freelist */
735 	ulong_t	pgmf_allocfailed[MMU_PAGE_SIZES];
736 	ulong_t	pgmf_allocempty[MMU_PAGE_SIZES];
737 	ulong_t	pgmf_allocok[MMU_PAGE_SIZES];
738 	ulong_t	pgmc_alloc;			/* page_get_mnode_cachelist */
739 	ulong_t	pgmc_allocfailed;
740 	ulong_t	pgmc_allocempty;
741 	ulong_t	pgmc_allocok;
742 	ulong_t	pladd_free[MMU_PAGE_SIZES];	/* page_list_add/sub */
743 	ulong_t	plsub_free[MMU_PAGE_SIZES];
744 	ulong_t	pladd_cache;
745 	ulong_t	plsub_cache;
746 	ulong_t	plsubpages_szcbig;
747 	ulong_t	plsubpages_szc0;
748 	ulong_t	pfs_req[MMU_PAGE_SIZES];	/* page_freelist_split */
749 	ulong_t	pfs_demote[MMU_PAGE_SIZES];
750 	ulong_t	pfc_coalok[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
751 	ulong_t ppr_reloc[MMU_PAGE_SIZES];	/* page_relocate */
752 	ulong_t ppr_relocok[MMU_PAGE_SIZES];
753 	ulong_t ppr_relocnoroot[MMU_PAGE_SIZES];
754 	ulong_t ppr_reloc_replnoroot[MMU_PAGE_SIZES];
755 	ulong_t ppr_relocnolock[MMU_PAGE_SIZES];
756 	ulong_t ppr_relocnomem[MMU_PAGE_SIZES];
757 	ulong_t ppr_krelocfail[MMU_PAGE_SIZES];
758 	ulong_t ppr_copyfail;
759 	/* page coalesce counter */
760 	ulong_t	page_ctrs_coalesce[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
761 	/* candidates useful */
762 	ulong_t	page_ctrs_cands_skip[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
763 	/* ctrs changed after locking */
764 	ulong_t	page_ctrs_changed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
765 	/* page_freelist_coalesce failed */
766 	ulong_t	page_ctrs_failed[MMU_PAGE_SIZES][MAX_MNODE_MRANGES];
767 	ulong_t	page_ctrs_coalesce_all;	/* page coalesce all counter */
768 	ulong_t	page_ctrs_cands_skip_all; /* candidates useful for all func */
769 };
770 extern struct vmm_vmstats_str vmm_vmstats;
771 #endif	/* VM_STATS */
772 
773 /*
774  * Used to hold off page relocations into the cage until OBP has completed
775  * its boot-time handoff of its resources to the kernel.
776  */
777 extern int page_relocate_ready;
778 
779 /*
780  * cpu/mmu-dependent vm variables may be reset at bootup.
781  */
782 extern uint_t mmu_page_sizes;
783 extern uint_t max_mmu_page_sizes;
784 extern uint_t mmu_hashcnt;
785 extern uint_t max_mmu_hashcnt;
786 extern size_t mmu_ism_pagesize;
787 extern int mmu_exported_pagesize_mask;
788 extern uint_t mmu_exported_page_sizes;
789 extern uint_t szc_2_userszc[];
790 extern uint_t userszc_2_szc[];
791 
792 #define	USERSZC_2_SZC(userszc)	(userszc_2_szc[userszc])
793 #define	SZC_2_USERSZC(szc)	(szc_2_userszc[szc])
794 
795 /*
796  * Platform specific page routines
797  */
798 extern void mach_page_add(page_t **, page_t *);
799 extern void mach_page_sub(page_t **, page_t *);
800 extern uint_t page_get_pagecolors(uint_t);
801 extern void ppcopy_kernel__relocatable(page_t *, page_t *);
802 #define	ppcopy_kernel(p1, p2)	ppcopy_kernel__relocatable(p1, p2)
803 
804 /*
805  * platform specific large pages for kernel heap support
806  */
807 extern size_t get_segkmem_lpsize(size_t lpsize);
808 extern size_t mmu_get_kernel_lpsize(size_t lpsize);
809 extern void mmu_init_kernel_pgsz(struct hat *hat);
810 extern void mmu_init_kcontext();
811 extern uint64_t kcontextreg;
812 
813 /*
814  * Nucleus data page allocator routines
815  */
816 extern void ndata_alloc_init(struct memlist *, uintptr_t, uintptr_t);
817 extern void *ndata_alloc(struct memlist *, size_t, size_t);
818 extern void *ndata_extra_base(struct memlist *, size_t, caddr_t);
819 extern size_t ndata_maxsize(struct memlist *);
820 extern size_t ndata_spare(struct memlist *, size_t, size_t);
821 
822 #ifdef	__cplusplus
823 }
824 #endif
825 
826 #endif	/* _VM_DEP_H */
827