xref: /titanic_53/usr/src/uts/sun4v/cpu/niagara2.c (revision fe70c9cf90dfc23d18485fb7b4b20a1175d53a8b)
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 /*
22fb2f18f8Sesaxe  * Copyright 2007 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>
37102033aaSdp78419 #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 
758d4e547dSae112802 #ifdef N2_1x_CPC_WORKAROUNDS
768d4e547dSae112802 static uint64_t cpu_ver;		/* Niagara2 CPU version reg */
778d4e547dSae112802 uint64_t	ni2_1x_perf_workarounds = 0;
788d4e547dSae112802 
798d4e547dSae112802 /* Niagara2 CPU version register */
808d4e547dSae112802 #define	VER_MASK_MAJOR_SHIFT	28
818d4e547dSae112802 #define	VER_MASK_MAJOR_MASK	0xf
828d4e547dSae112802 
838d4e547dSae112802 extern uint64_t va_to_pa(void *);
848d4e547dSae112802 extern uint64_t ni2_getver();		/* HV code to get %hver */
858d4e547dSae112802 extern uint64_t niagara2_getver(uint64_t ni2_getver_ra, uint64_t *cpu_version);
868d4e547dSae112802 #endif
878d4e547dSae112802 
8844961713Sgirish void
8944961713Sgirish cpu_setup(void)
9044961713Sgirish {
9144961713Sgirish 	extern int mmu_exported_pagesize_mask;
9244961713Sgirish 	extern int cpc_has_overflow_intr;
9344961713Sgirish 	int status;
9444961713Sgirish 
958d4e547dSae112802 #ifdef N2_1x_CPC_WORKAROUNDS
968d4e547dSae112802 	/*
978d4e547dSae112802 	 * Get CPU version for Niagara2 part.
988d4e547dSae112802 	 */
998d4e547dSae112802 	if (niagara2_getver(va_to_pa((void *)ni2_getver), &cpu_ver) == H_EOK &&
1008d4e547dSae112802 	    ((cpu_ver >> VER_MASK_MAJOR_SHIFT) & VER_MASK_MAJOR_MASK) <= 1)
1018d4e547dSae112802 		ni2_1x_perf_workarounds = 1;
1028d4e547dSae112802 #endif
1038d4e547dSae112802 
10444961713Sgirish 	/*
10544961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
10644961713Sgirish 	 * services.
10744961713Sgirish 	 */
10844961713Sgirish 	status = hsvc_register(&niagara2_hsvc, &niagara2_sup_minor);
10944961713Sgirish 	if (status != 0) {
11044961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
11144961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
11244961713Sgirish 		    niagara2_hsvc.hsvc_modname, niagara2_hsvc.hsvc_group,
11344961713Sgirish 		    niagara2_hsvc.hsvc_major, niagara2_hsvc.hsvc_minor, status);
11444961713Sgirish 		niagara2_hsvc_available = B_FALSE;
11544961713Sgirish 	}
11644961713Sgirish 
11744961713Sgirish 	/*
11844961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
11944961713Sgirish 	 * routine.
12044961713Sgirish 	 */
12144961713Sgirish 	cpu_setup_common(NULL);
12244961713Sgirish 
12344961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
12444961713Sgirish 
12544961713Sgirish 	if ((mmu_exported_pagesize_mask &
12644961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
12744961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
12844961713Sgirish 		cmn_err(CE_PANIC, "machine description"
12944961713Sgirish 		    " does not have required sun4v page sizes"
13044961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
13144961713Sgirish 		    mmu_exported_pagesize_mask);
13244961713Sgirish 
13344961713Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
13444961713Sgirish 
13544961713Sgirish 	/*
13644961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
13744961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
13844961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
13944961713Sgirish 	 * and must never be mapped. In addition, software must not use
14044961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
14144961713Sgirish 	 * avoid problems with prefetching into the VA hole.
14244961713Sgirish 	 */
14344961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
14444961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
14544961713Sgirish 
14644961713Sgirish 	/*
14744961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
14844961713Sgirish 	 */
14944961713Sgirish 	cpc_has_overflow_intr = 1;
150102033aaSdp78419 
151102033aaSdp78419 	/*
152102033aaSdp78419 	 * Enable 4M pages for OOB.
153102033aaSdp78419 	 */
154102033aaSdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
155102033aaSdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
156102033aaSdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
15744961713Sgirish }
15844961713Sgirish 
15944961713Sgirish /*
16044961713Sgirish  * Set the magic constants of the implementation.
16144961713Sgirish  */
16244961713Sgirish void
16344961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
16444961713Sgirish {
16544961713Sgirish 	/*
16644961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
16744961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
16844961713Sgirish 	 * size, linesize.
16944961713Sgirish 	 */
17044961713Sgirish 	if (cpunode->ecache_size == 0)
17144961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
17244961713Sgirish 	if (cpunode->ecache_linesize == 0)
17344961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
17444961713Sgirish 	if (cpunode->ecache_associativity == 0)
17544961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
17644961713Sgirish }
17744961713Sgirish 
17844961713Sgirish static int niagara2_cpucnt;
17944961713Sgirish 
18044961713Sgirish void
18144961713Sgirish cpu_init_private(struct cpu *cp)
18244961713Sgirish {
18344961713Sgirish 	extern int niagara_kstat_init(void);
18444961713Sgirish 
18544961713Sgirish 	/*
186fb2f18f8Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
187fb2f18f8Sesaxe 	 * the execution unit sharing information from the MD. They default
188fb2f18f8Sesaxe 	 * to the virtual CPU id in the absence of such information.
18944961713Sgirish 	 */
19044961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
19144961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
19244961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
19344961713Sgirish 
194fb2f18f8Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
195fb2f18f8Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
196fb2f18f8Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
197fb2f18f8Sesaxe 
198fb2f18f8Sesaxe 	/*
199fb2f18f8Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
200fb2f18f8Sesaxe 	 */
201fb2f18f8Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
202fb2f18f8Sesaxe 
20344961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
20444961713Sgirish 	if ((niagara2_cpucnt++ == 0) && (niagara2_hsvc_available == B_TRUE))
20544961713Sgirish 		(void) niagara_kstat_init();
20644961713Sgirish }
20744961713Sgirish 
20844961713Sgirish /*ARGSUSED*/
20944961713Sgirish void
21044961713Sgirish cpu_uninit_private(struct cpu *cp)
21144961713Sgirish {
21244961713Sgirish 	extern int niagara_kstat_fini(void);
21344961713Sgirish 
21444961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
21544961713Sgirish 	if ((--niagara2_cpucnt == 0) && (niagara2_hsvc_available == B_TRUE))
21644961713Sgirish 		(void) niagara_kstat_fini();
21744961713Sgirish }
21844961713Sgirish 
21944961713Sgirish /*
22044961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
22144961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
22244961713Sgirish  * the address is ignored, so we always flush address 0.
22344961713Sgirish  */
22444961713Sgirish /*ARGSUSED*/
22544961713Sgirish void
22644961713Sgirish dtrace_flush_sec(uintptr_t addr)
22744961713Sgirish {
22844961713Sgirish 	doflush(0);
22944961713Sgirish }
23044961713Sgirish 
23144961713Sgirish /*
23244961713Sgirish  * Trapstat support for Niagara2 processor
23344961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
23444961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
23544961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
23644961713Sgirish  */
23744961713Sgirish int
23844961713Sgirish cpu_trapstat_conf(int cmd)
23944961713Sgirish {
24044961713Sgirish 	int status = 0;
24144961713Sgirish 
24244961713Sgirish 	switch (cmd) {
24344961713Sgirish 	case CPU_TSTATCONF_INIT:
24444961713Sgirish 	case CPU_TSTATCONF_FINI:
24544961713Sgirish 	case CPU_TSTATCONF_ENABLE:
24644961713Sgirish 	case CPU_TSTATCONF_DISABLE:
24744961713Sgirish 		break;
24844961713Sgirish 	default:
24944961713Sgirish 		status = EINVAL;
25044961713Sgirish 		break;
25144961713Sgirish 	}
25244961713Sgirish 	return (status);
25344961713Sgirish }
25444961713Sgirish 
25544961713Sgirish void
25644961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
25744961713Sgirish {
25844961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
25944961713Sgirish 	int	i;
26044961713Sgirish 
26144961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
26244961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
26344961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
26444961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
26544961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
26644961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
26744961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
26844961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
26944961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
27044961713Sgirish 	}
27144961713Sgirish }
272102033aaSdp78419 
273102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
274102033aaSdp78419 uint_t
275102033aaSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc)
276102033aaSdp78419 {
277102033aaSdp78419 	uint_t color;
278102033aaSdp78419 
279102033aaSdp78419 	ASSERT(szc <= TTE256M);
280102033aaSdp78419 
281102033aaSdp78419 	pfn = PFN_BASE(pfn, szc);
282102033aaSdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
283102033aaSdp78419 	if (szc >= TTE4M)
284102033aaSdp78419 		return (color);
285102033aaSdp78419 
286102033aaSdp78419 	color = (color << 2) | ((pfn >> 5) & 0x3);
287102033aaSdp78419 
288102033aaSdp78419 	return (szc <= TTE64K ? color : (color >> 1));
289102033aaSdp78419 }
290102033aaSdp78419 
291102033aaSdp78419 #if TTE256M != 5
292102033aaSdp78419 #error TTE256M is not 5
293102033aaSdp78419 #endif
294102033aaSdp78419 
295102033aaSdp78419 uint_t
296102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
297102033aaSdp78419 {
298102033aaSdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
299102033aaSdp78419 	ASSERT(szc < TTE256M);
300102033aaSdp78419 
301102033aaSdp78419 	mask &= ni2_color_masks[szc];
302102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (mask >> 1) : mask);
303102033aaSdp78419 }
304102033aaSdp78419 
305102033aaSdp78419 uint_t
306102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
307102033aaSdp78419 {
308102033aaSdp78419 	ASSERT(szc < TTE256M);
309102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (color >> 1) : color);
310102033aaSdp78419 }
311102033aaSdp78419 
312102033aaSdp78419 uint_t
313102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
314102033aaSdp78419 {
315102033aaSdp78419 	ASSERT(nszc > szc);
316102033aaSdp78419 	ASSERT(nszc <= TTE256M);
317102033aaSdp78419 
318102033aaSdp78419 	if (szc <= TTE64K)
319102033aaSdp78419 		return ((nszc >= TTE4M) ? 2 : ((nszc >= TTE512K) ? 1 : 0));
320102033aaSdp78419 	if (szc == TTE512K)
321102033aaSdp78419 		return (1);
322102033aaSdp78419 
323102033aaSdp78419 	return (0);
324102033aaSdp78419 }
325102033aaSdp78419 
326102033aaSdp78419 /*ARGSUSED*/
327102033aaSdp78419 pfn_t
328102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
329102033aaSdp78419     uint_t ceq_mask, uint_t color_mask)
330102033aaSdp78419 {
331102033aaSdp78419 	pfn_t pstep = PNUM_SIZE(szc);
332102033aaSdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
333102033aaSdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
334102033aaSdp78419 
335102033aaSdp78419 	ASSERT((color & ~ceq_mask) == 0);
336102033aaSdp78419 
337102033aaSdp78419 	if (((page_pfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) {
338102033aaSdp78419 
339102033aaSdp78419 		/* we start from the page with correct color */
340102033aaSdp78419 		if (szc >= TTE512K) {
341102033aaSdp78419 			if (szc >= TTE4M) {
342102033aaSdp78419 				/* page color is PA[32:28] */
343102033aaSdp78419 				pfn_ceq_mask = ceq_mask << 15;
344102033aaSdp78419 			} else {
345102033aaSdp78419 				/* page color is PA[32:28].PA[19:19] */
346102033aaSdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
347102033aaSdp78419 				    ((ceq_mask >> 1) << 15);
348102033aaSdp78419 			}
349102033aaSdp78419 			pfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
350102033aaSdp78419 			return (pfn);
351102033aaSdp78419 		} else {
352102033aaSdp78419 			/*
353102033aaSdp78419 			 * We deal 64K or 8K page. Check if we could the
354102033aaSdp78419 			 * satisfy the request without changing PA[32:28]
355102033aaSdp78419 			 */
356102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
357102033aaSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
358102033aaSdp78419 
359102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
360102033aaSdp78419 				return (npfn);
361102033aaSdp78419 
362102033aaSdp78419 			/*
363102033aaSdp78419 			 * for next pfn we have to change bits PA[32:28]
364102033aaSdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
365102033aaSdp78419 			 */
366102033aaSdp78419 			npfn = (pfn >> 15) << 15;
367102033aaSdp78419 			npfn |= (ceq_mask & color & 3) << 5;
368102033aaSdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
369102033aaSdp78419 			    (ceq_mask & 0x1c) << 13;
370102033aaSdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
371102033aaSdp78419 
372102033aaSdp78419 			/*
373102033aaSdp78419 			 * set bits PA[17:13] to match the color
374102033aaSdp78419 			 */
375102033aaSdp78419 			ceq_mask >>= 2;
376102033aaSdp78419 			color = (color >> 2) & ceq_mask;
377102033aaSdp78419 			npfn |= ((npfn >> 15) ^ color) & ceq_mask;
378102033aaSdp78419 			return (npfn);
379102033aaSdp78419 		}
380102033aaSdp78419 	}
381102033aaSdp78419 
382102033aaSdp78419 	/*
383102033aaSdp78419 	 * we start from the page with incorrect color - rare case
384102033aaSdp78419 	 */
385102033aaSdp78419 	if (szc >= TTE512K) {
386102033aaSdp78419 		if (szc >= TTE4M) {
387102033aaSdp78419 			/* page color is in bits PA[32:28] */
388102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
389102033aaSdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
390102033aaSdp78419 		} else {
391102033aaSdp78419 			/* try get the right color by changing bit PA[19:19] */
392102033aaSdp78419 			npfn = pfn + pstep;
393102033aaSdp78419 			if (((page_pfn_2_color_cpu(npfn, szc) ^ color) &
394102033aaSdp78419 			    ceq_mask) == 0)
395102033aaSdp78419 				return (npfn);
396102033aaSdp78419 
397102033aaSdp78419 			/* page color is PA[32:28].PA[19:19] */
398102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
399102033aaSdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
400102033aaSdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
401102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
402102033aaSdp78419 		}
403102033aaSdp78419 
404102033aaSdp78419 		while (npfn <= pfn) {
405102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
406102033aaSdp78419 		}
407102033aaSdp78419 		return (npfn);
408102033aaSdp78419 	}
409102033aaSdp78419 
410102033aaSdp78419 	/*
411102033aaSdp78419 	 * We deal 64K or 8K page of incorrect color.
412102033aaSdp78419 	 * Try correcting color without changing PA[32:28]
413102033aaSdp78419 	 */
414102033aaSdp78419 
415102033aaSdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
416102033aaSdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
417102033aaSdp78419 	npfn = (pfn & ~(pfn_t)0x7f);
418102033aaSdp78419 	npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
419102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
420102033aaSdp78419 
421102033aaSdp78419 	if (((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0) {
422102033aaSdp78419 
423102033aaSdp78419 		/* the color is fixed - find the next page */
424102033aaSdp78419 		while (npfn <= pfn) {
425102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
426102033aaSdp78419 		}
427102033aaSdp78419 		if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
428102033aaSdp78419 			return (npfn);
429102033aaSdp78419 	}
430102033aaSdp78419 
431102033aaSdp78419 	/* to fix the color need to touch PA[32:28] */
432102033aaSdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
433102033aaSdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
434102033aaSdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
435102033aaSdp78419 
436102033aaSdp78419 	while (npfn <= pfn) {
437102033aaSdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
438102033aaSdp78419 	}
439102033aaSdp78419 
440102033aaSdp78419 	/* set bits PA[19:13] to match the color */
441102033aaSdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
442102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
443102033aaSdp78419 
444102033aaSdp78419 	ASSERT(((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
445102033aaSdp78419 
446102033aaSdp78419 	return (npfn);
447102033aaSdp78419 }
448102033aaSdp78419 
449102033aaSdp78419 /*
450102033aaSdp78419  * init page coloring
451102033aaSdp78419  */
452102033aaSdp78419 void
453102033aaSdp78419 page_coloring_init_cpu()
454102033aaSdp78419 {
455102033aaSdp78419 	int i;
456102033aaSdp78419 
457102033aaSdp78419 	hw_page_array[0].hp_colors = 1 << 7;
458102033aaSdp78419 	hw_page_array[1].hp_colors = 1 << 7;
459102033aaSdp78419 	hw_page_array[2].hp_colors = 1 << 6;
460102033aaSdp78419 
461102033aaSdp78419 	for (i = 3; i < mmu_page_sizes; i++) {
462102033aaSdp78419 		hw_page_array[i].hp_colors = 1 << 5;
463102033aaSdp78419 	}
464102033aaSdp78419 }
465*fe70c9cfSdp78419 
466*fe70c9cfSdp78419 /*
467*fe70c9cfSdp78419  * group colorequiv colors on N2 by low order bits of the color first
468*fe70c9cfSdp78419  */
469*fe70c9cfSdp78419 void
470*fe70c9cfSdp78419 page_set_colorequiv_arr_cpu(void)
471*fe70c9cfSdp78419 {
472*fe70c9cfSdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
473*fe70c9cfSdp78419 
474*fe70c9cfSdp78419 	if (colorequiv > 1) {
475*fe70c9cfSdp78419 		int i;
476*fe70c9cfSdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
477*fe70c9cfSdp78419 
478*fe70c9cfSdp78419 		if (sv_a > 15)
479*fe70c9cfSdp78419 			sv_a = 15;
480*fe70c9cfSdp78419 
481*fe70c9cfSdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
482*fe70c9cfSdp78419 			uint_t colors;
483*fe70c9cfSdp78419 			uint_t a = sv_a;
484*fe70c9cfSdp78419 
485*fe70c9cfSdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
486*fe70c9cfSdp78419 				continue;
487*fe70c9cfSdp78419 			while ((colors >> a) == 0)
488*fe70c9cfSdp78419 				a--;
489*fe70c9cfSdp78419 			if (a > (colorequivszc[i] & 0xf) +
490*fe70c9cfSdp78419 			    (colorequivszc[i] >> 4)) {
491*fe70c9cfSdp78419 				if (a <= nequiv_shades_log2[i]) {
492*fe70c9cfSdp78419 					colorequivszc[i] = a;
493*fe70c9cfSdp78419 				} else {
494*fe70c9cfSdp78419 					colorequivszc[i] =
495*fe70c9cfSdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
496*fe70c9cfSdp78419 					    nequiv_shades_log2[i];
497*fe70c9cfSdp78419 				}
498*fe70c9cfSdp78419 			}
499*fe70c9cfSdp78419 		}
500*fe70c9cfSdp78419 	}
501*fe70c9cfSdp78419 }
502