xref: /illumos-gate/usr/src/uts/sun4v/cpu/niagara2.c (revision 102033aa92edf302ad31b3bdd7c6fcd2d6910903)
144961713Sgirish /*
244961713Sgirish  * CDDL HEADER START
344961713Sgirish  *
444961713Sgirish  * The contents of this file are subject to the terms of the
544961713Sgirish  * Common Development and Distribution License (the "License").
644961713Sgirish  * You may not use this file except in compliance with the License.
744961713Sgirish  *
844961713Sgirish  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
944961713Sgirish  * or http://www.opensolaris.org/os/licensing.
1044961713Sgirish  * See the License for the specific language governing permissions
1144961713Sgirish  * and limitations under the License.
1244961713Sgirish  *
1344961713Sgirish  * When distributing Covered Code, include this CDDL HEADER in each
1444961713Sgirish  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1544961713Sgirish  * If applicable, add the following below this CDDL HEADER, with the
1644961713Sgirish  * fields enclosed by brackets "[]" replaced with your own identifying
1744961713Sgirish  * information: Portions Copyright [yyyy] [name of copyright owner]
1844961713Sgirish  *
1944961713Sgirish  * CDDL HEADER END
2044961713Sgirish  */
2144961713Sgirish /*
2244961713Sgirish  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2344961713Sgirish  * Use is subject to license terms.
2444961713Sgirish  */
2544961713Sgirish 
2644961713Sgirish #pragma ident	"%Z%%M%	%I%	%E% SMI"
2744961713Sgirish 
2844961713Sgirish #include <sys/types.h>
2944961713Sgirish #include <sys/systm.h>
3044961713Sgirish #include <sys/archsystm.h>
3144961713Sgirish #include <sys/machparam.h>
3244961713Sgirish #include <sys/machsystm.h>
3344961713Sgirish #include <sys/cpu.h>
3444961713Sgirish #include <sys/elf_SPARC.h>
3544961713Sgirish #include <vm/hat_sfmmu.h>
3644961713Sgirish #include <vm/page.h>
37*102033aaSdp78419 #include <vm/vm_dep.h>
3844961713Sgirish #include <sys/cpuvar.h>
3944961713Sgirish #include <sys/async.h>
4044961713Sgirish #include <sys/cmn_err.h>
4144961713Sgirish #include <sys/debug.h>
4244961713Sgirish #include <sys/dditypes.h>
4344961713Sgirish #include <sys/sunddi.h>
4444961713Sgirish #include <sys/cpu_module.h>
4544961713Sgirish #include <sys/prom_debug.h>
4644961713Sgirish #include <sys/vmsystm.h>
4744961713Sgirish #include <sys/prom_plat.h>
4844961713Sgirish #include <sys/sysmacros.h>
4944961713Sgirish #include <sys/intreg.h>
5044961713Sgirish #include <sys/machtrap.h>
5144961713Sgirish #include <sys/ontrap.h>
5244961713Sgirish #include <sys/ivintr.h>
5344961713Sgirish #include <sys/atomic.h>
5444961713Sgirish #include <sys/panic.h>
5544961713Sgirish #include <sys/dtrace.h>
5644961713Sgirish #include <sys/simulate.h>
5744961713Sgirish #include <sys/fault.h>
5844961713Sgirish #include <sys/niagara2regs.h>
5944961713Sgirish #include <sys/hsvc.h>
6044961713Sgirish #include <sys/trapstat.h>
6144961713Sgirish 
6244961713Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
6344961713Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
6444961713Sgirish 
6544961713Sgirish /*
6644961713Sgirish  * Hypervisor services information for the NIAGARA2 CPU module
6744961713Sgirish  */
6844961713Sgirish static boolean_t niagara2_hsvc_available = B_TRUE;
6944961713Sgirish static uint64_t niagara2_sup_minor;		/* Supported minor number */
7044961713Sgirish static hsvc_info_t niagara2_hsvc = {
7144961713Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
7244961713Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
7344961713Sgirish };
7444961713Sgirish 
7544961713Sgirish void
7644961713Sgirish cpu_setup(void)
7744961713Sgirish {
7844961713Sgirish 	extern int mmu_exported_pagesize_mask;
7944961713Sgirish 	extern int cpc_has_overflow_intr;
8044961713Sgirish 	int status;
8144961713Sgirish 
8244961713Sgirish 	/*
8344961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
8444961713Sgirish 	 * services.
8544961713Sgirish 	 */
8644961713Sgirish 	status = hsvc_register(&niagara2_hsvc, &niagara2_sup_minor);
8744961713Sgirish 	if (status != 0) {
8844961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
8944961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
9044961713Sgirish 		    niagara2_hsvc.hsvc_modname, niagara2_hsvc.hsvc_group,
9144961713Sgirish 		    niagara2_hsvc.hsvc_major, niagara2_hsvc.hsvc_minor, status);
9244961713Sgirish 		niagara2_hsvc_available = B_FALSE;
9344961713Sgirish 	}
9444961713Sgirish 
9544961713Sgirish 	/*
9644961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
9744961713Sgirish 	 * routine.
9844961713Sgirish 	 */
9944961713Sgirish 	cpu_setup_common(NULL);
10044961713Sgirish 
10144961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
10244961713Sgirish 
10344961713Sgirish 	if ((mmu_exported_pagesize_mask &
10444961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
10544961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
10644961713Sgirish 		cmn_err(CE_PANIC, "machine description"
10744961713Sgirish 		    " does not have required sun4v page sizes"
10844961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
10944961713Sgirish 		    mmu_exported_pagesize_mask);
11044961713Sgirish 
11144961713Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
11244961713Sgirish 
11344961713Sgirish 	/*
11444961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
11544961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
11644961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
11744961713Sgirish 	 * and must never be mapped. In addition, software must not use
11844961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
11944961713Sgirish 	 * avoid problems with prefetching into the VA hole.
12044961713Sgirish 	 */
12144961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
12244961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
12344961713Sgirish 
12444961713Sgirish 	/*
12544961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
12644961713Sgirish 	 */
12744961713Sgirish 	cpc_has_overflow_intr = 1;
128*102033aaSdp78419 
129*102033aaSdp78419 	/*
130*102033aaSdp78419 	 * Enable 4M pages for OOB.
131*102033aaSdp78419 	 */
132*102033aaSdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
133*102033aaSdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
134*102033aaSdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
13544961713Sgirish }
13644961713Sgirish 
13744961713Sgirish /*
13844961713Sgirish  * Set the magic constants of the implementation.
13944961713Sgirish  */
14044961713Sgirish void
14144961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
14244961713Sgirish {
14344961713Sgirish 	/*
14444961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
14544961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
14644961713Sgirish 	 * size, linesize.
14744961713Sgirish 	 */
14844961713Sgirish 	if (cpunode->ecache_size == 0)
14944961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
15044961713Sgirish 	if (cpunode->ecache_linesize == 0)
15144961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
15244961713Sgirish 	if (cpunode->ecache_associativity == 0)
15344961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
15444961713Sgirish }
15544961713Sgirish 
15644961713Sgirish static int niagara2_cpucnt;
15744961713Sgirish 
15844961713Sgirish void
15944961713Sgirish cpu_init_private(struct cpu *cp)
16044961713Sgirish {
16144961713Sgirish 	extern int niagara_kstat_init(void);
16244961713Sgirish 
16344961713Sgirish 	/*
16444961713Sgirish 	 * The cpu_ipipe field is initialized based on the execution
16544961713Sgirish 	 * unit sharing information from the MD. It defaults to the
16644961713Sgirish 	 * virtual CPU id in the absence of such information.
16744961713Sgirish 	 */
16844961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
16944961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
17044961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
17144961713Sgirish 
17244961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
17344961713Sgirish 	if ((niagara2_cpucnt++ == 0) && (niagara2_hsvc_available == B_TRUE))
17444961713Sgirish 		(void) niagara_kstat_init();
17544961713Sgirish }
17644961713Sgirish 
17744961713Sgirish /*ARGSUSED*/
17844961713Sgirish void
17944961713Sgirish cpu_uninit_private(struct cpu *cp)
18044961713Sgirish {
18144961713Sgirish 	extern int niagara_kstat_fini(void);
18244961713Sgirish 
18344961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
18444961713Sgirish 	if ((--niagara2_cpucnt == 0) && (niagara2_hsvc_available == B_TRUE))
18544961713Sgirish 		(void) niagara_kstat_fini();
18644961713Sgirish }
18744961713Sgirish 
18844961713Sgirish /*
18944961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
19044961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
19144961713Sgirish  * the address is ignored, so we always flush address 0.
19244961713Sgirish  */
19344961713Sgirish /*ARGSUSED*/
19444961713Sgirish void
19544961713Sgirish dtrace_flush_sec(uintptr_t addr)
19644961713Sgirish {
19744961713Sgirish 	doflush(0);
19844961713Sgirish }
19944961713Sgirish 
20044961713Sgirish /*
20144961713Sgirish  * Trapstat support for Niagara2 processor
20244961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
20344961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
20444961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
20544961713Sgirish  */
20644961713Sgirish int
20744961713Sgirish cpu_trapstat_conf(int cmd)
20844961713Sgirish {
20944961713Sgirish 	int status = 0;
21044961713Sgirish 
21144961713Sgirish 	switch (cmd) {
21244961713Sgirish 	case CPU_TSTATCONF_INIT:
21344961713Sgirish 	case CPU_TSTATCONF_FINI:
21444961713Sgirish 	case CPU_TSTATCONF_ENABLE:
21544961713Sgirish 	case CPU_TSTATCONF_DISABLE:
21644961713Sgirish 		break;
21744961713Sgirish 	default:
21844961713Sgirish 		status = EINVAL;
21944961713Sgirish 		break;
22044961713Sgirish 	}
22144961713Sgirish 	return (status);
22244961713Sgirish }
22344961713Sgirish 
22444961713Sgirish void
22544961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
22644961713Sgirish {
22744961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
22844961713Sgirish 	int	i;
22944961713Sgirish 
23044961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
23144961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
23244961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
23344961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
23444961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
23544961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
23644961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
23744961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
23844961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
23944961713Sgirish 	}
24044961713Sgirish }
241*102033aaSdp78419 
242*102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
243*102033aaSdp78419 uint_t
244*102033aaSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc)
245*102033aaSdp78419 {
246*102033aaSdp78419 	uint_t color;
247*102033aaSdp78419 
248*102033aaSdp78419 	ASSERT(szc <= TTE256M);
249*102033aaSdp78419 
250*102033aaSdp78419 	pfn = PFN_BASE(pfn, szc);
251*102033aaSdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
252*102033aaSdp78419 	if (szc >= TTE4M)
253*102033aaSdp78419 		return (color);
254*102033aaSdp78419 
255*102033aaSdp78419 	color = (color << 2) | ((pfn >> 5) & 0x3);
256*102033aaSdp78419 
257*102033aaSdp78419 	return (szc <= TTE64K ? color : (color >> 1));
258*102033aaSdp78419 }
259*102033aaSdp78419 
260*102033aaSdp78419 #if TTE256M != 5
261*102033aaSdp78419 #error TTE256M is not 5
262*102033aaSdp78419 #endif
263*102033aaSdp78419 
264*102033aaSdp78419 uint_t
265*102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
266*102033aaSdp78419 {
267*102033aaSdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
268*102033aaSdp78419 	ASSERT(szc < TTE256M);
269*102033aaSdp78419 
270*102033aaSdp78419 	mask &= ni2_color_masks[szc];
271*102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (mask >> 1) : mask);
272*102033aaSdp78419 }
273*102033aaSdp78419 
274*102033aaSdp78419 uint_t
275*102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
276*102033aaSdp78419 {
277*102033aaSdp78419 	ASSERT(szc < TTE256M);
278*102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (color >> 1) : color);
279*102033aaSdp78419 }
280*102033aaSdp78419 
281*102033aaSdp78419 uint_t
282*102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
283*102033aaSdp78419 {
284*102033aaSdp78419 	ASSERT(nszc > szc);
285*102033aaSdp78419 	ASSERT(nszc <= TTE256M);
286*102033aaSdp78419 
287*102033aaSdp78419 	if (szc <= TTE64K)
288*102033aaSdp78419 		return ((nszc >= TTE4M) ? 2 : ((nszc >= TTE512K) ? 1 : 0));
289*102033aaSdp78419 	if (szc == TTE512K)
290*102033aaSdp78419 		return (1);
291*102033aaSdp78419 
292*102033aaSdp78419 	return (0);
293*102033aaSdp78419 }
294*102033aaSdp78419 
295*102033aaSdp78419 /*ARGSUSED*/
296*102033aaSdp78419 pfn_t
297*102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
298*102033aaSdp78419     uint_t ceq_mask, uint_t color_mask)
299*102033aaSdp78419 {
300*102033aaSdp78419 	pfn_t pstep = PNUM_SIZE(szc);
301*102033aaSdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
302*102033aaSdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
303*102033aaSdp78419 
304*102033aaSdp78419 	ASSERT((color & ~ceq_mask) == 0);
305*102033aaSdp78419 
306*102033aaSdp78419 	if (((page_pfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) {
307*102033aaSdp78419 
308*102033aaSdp78419 		/* we start from the page with correct color */
309*102033aaSdp78419 		if (szc >= TTE512K) {
310*102033aaSdp78419 			if (szc >= TTE4M) {
311*102033aaSdp78419 				/* page color is PA[32:28] */
312*102033aaSdp78419 				pfn_ceq_mask = ceq_mask << 15;
313*102033aaSdp78419 			} else {
314*102033aaSdp78419 				/* page color is PA[32:28].PA[19:19] */
315*102033aaSdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
316*102033aaSdp78419 				    ((ceq_mask >> 1) << 15);
317*102033aaSdp78419 			}
318*102033aaSdp78419 			pfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
319*102033aaSdp78419 			return (pfn);
320*102033aaSdp78419 		} else {
321*102033aaSdp78419 			/*
322*102033aaSdp78419 			 * We deal 64K or 8K page. Check if we could the
323*102033aaSdp78419 			 * satisfy the request without changing PA[32:28]
324*102033aaSdp78419 			 */
325*102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
326*102033aaSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
327*102033aaSdp78419 
328*102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
329*102033aaSdp78419 				return (npfn);
330*102033aaSdp78419 
331*102033aaSdp78419 			/*
332*102033aaSdp78419 			 * for next pfn we have to change bits PA[32:28]
333*102033aaSdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
334*102033aaSdp78419 			 */
335*102033aaSdp78419 			npfn = (pfn >> 15) << 15;
336*102033aaSdp78419 			npfn |= (ceq_mask & color & 3) << 5;
337*102033aaSdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
338*102033aaSdp78419 			    (ceq_mask & 0x1c) << 13;
339*102033aaSdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
340*102033aaSdp78419 
341*102033aaSdp78419 			/*
342*102033aaSdp78419 			 * set bits PA[17:13] to match the color
343*102033aaSdp78419 			 */
344*102033aaSdp78419 			ceq_mask >>= 2;
345*102033aaSdp78419 			color = (color >> 2) & ceq_mask;
346*102033aaSdp78419 			npfn |= ((npfn >> 15) ^ color) & ceq_mask;
347*102033aaSdp78419 			return (npfn);
348*102033aaSdp78419 		}
349*102033aaSdp78419 	}
350*102033aaSdp78419 
351*102033aaSdp78419 	/*
352*102033aaSdp78419 	 * we start from the page with incorrect color - rare case
353*102033aaSdp78419 	 */
354*102033aaSdp78419 	if (szc >= TTE512K) {
355*102033aaSdp78419 		if (szc >= TTE4M) {
356*102033aaSdp78419 			/* page color is in bits PA[32:28] */
357*102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
358*102033aaSdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
359*102033aaSdp78419 		} else {
360*102033aaSdp78419 			/* try get the right color by changing bit PA[19:19] */
361*102033aaSdp78419 			npfn = pfn + pstep;
362*102033aaSdp78419 			if (((page_pfn_2_color_cpu(npfn, szc) ^ color) &
363*102033aaSdp78419 			    ceq_mask) == 0)
364*102033aaSdp78419 				return (npfn);
365*102033aaSdp78419 
366*102033aaSdp78419 			/* page color is PA[32:28].PA[19:19] */
367*102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
368*102033aaSdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
369*102033aaSdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
370*102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
371*102033aaSdp78419 		}
372*102033aaSdp78419 
373*102033aaSdp78419 		while (npfn <= pfn) {
374*102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
375*102033aaSdp78419 		}
376*102033aaSdp78419 		return (npfn);
377*102033aaSdp78419 	}
378*102033aaSdp78419 
379*102033aaSdp78419 	/*
380*102033aaSdp78419 	 * We deal 64K or 8K page of incorrect color.
381*102033aaSdp78419 	 * Try correcting color without changing PA[32:28]
382*102033aaSdp78419 	 */
383*102033aaSdp78419 
384*102033aaSdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
385*102033aaSdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
386*102033aaSdp78419 	npfn = (pfn & ~(pfn_t)0x7f);
387*102033aaSdp78419 	npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
388*102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
389*102033aaSdp78419 
390*102033aaSdp78419 	if (((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0) {
391*102033aaSdp78419 
392*102033aaSdp78419 		/* the color is fixed - find the next page */
393*102033aaSdp78419 		while (npfn <= pfn) {
394*102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
395*102033aaSdp78419 		}
396*102033aaSdp78419 		if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
397*102033aaSdp78419 			return (npfn);
398*102033aaSdp78419 	}
399*102033aaSdp78419 
400*102033aaSdp78419 	/* to fix the color need to touch PA[32:28] */
401*102033aaSdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
402*102033aaSdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
403*102033aaSdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
404*102033aaSdp78419 
405*102033aaSdp78419 	while (npfn <= pfn) {
406*102033aaSdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
407*102033aaSdp78419 	}
408*102033aaSdp78419 
409*102033aaSdp78419 	/* set bits PA[19:13] to match the color */
410*102033aaSdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
411*102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
412*102033aaSdp78419 
413*102033aaSdp78419 	ASSERT(((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
414*102033aaSdp78419 
415*102033aaSdp78419 	return (npfn);
416*102033aaSdp78419 }
417*102033aaSdp78419 
418*102033aaSdp78419 /*
419*102033aaSdp78419  * init page coloring
420*102033aaSdp78419  */
421*102033aaSdp78419 void
422*102033aaSdp78419 page_coloring_init_cpu()
423*102033aaSdp78419 {
424*102033aaSdp78419 	int i;
425*102033aaSdp78419 	uint_t colors;
426*102033aaSdp78419 
427*102033aaSdp78419 	hw_page_array[0].hp_colors = 1 << 7;
428*102033aaSdp78419 	hw_page_array[1].hp_colors = 1 << 7;
429*102033aaSdp78419 	hw_page_array[2].hp_colors = 1 << 6;
430*102033aaSdp78419 
431*102033aaSdp78419 	for (i = 3; i < mmu_page_sizes; i++) {
432*102033aaSdp78419 		hw_page_array[i].hp_colors = 1 << 5;
433*102033aaSdp78419 	}
434*102033aaSdp78419 
435*102033aaSdp78419 	if (colorequiv > 1) {
436*102033aaSdp78419 		int a = lowbit(colorequiv) - 1;
437*102033aaSdp78419 
438*102033aaSdp78419 		if (a > 15)
439*102033aaSdp78419 			a = 15;
440*102033aaSdp78419 
441*102033aaSdp78419 		for (i = 0; i < mmu_page_sizes; i++) {
442*102033aaSdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1) {
443*102033aaSdp78419 				continue;
444*102033aaSdp78419 			}
445*102033aaSdp78419 			while ((colors >> a) == 0)
446*102033aaSdp78419 				a--;
447*102033aaSdp78419 			colorequivszc[i] = (a << 4);
448*102033aaSdp78419 		}
449*102033aaSdp78419 	}
450*102033aaSdp78419 }
451