xref: /titanic_53/usr/src/uts/sun4v/cpu/niagara2.c (revision ef29e9078142ba78702f73f4854d952174aca80b)
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  */
21459190a5Srsmaeda 
2244961713Sgirish /*
23fb2f18f8Sesaxe  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
2444961713Sgirish  * Use is subject to license terms.
2544961713Sgirish  */
2644961713Sgirish 
2744961713Sgirish #pragma ident	"%Z%%M%	%I%	%E% SMI"
2844961713Sgirish 
2944961713Sgirish #include <sys/types.h>
3044961713Sgirish #include <sys/systm.h>
3144961713Sgirish #include <sys/archsystm.h>
3244961713Sgirish #include <sys/machparam.h>
3344961713Sgirish #include <sys/machsystm.h>
3444961713Sgirish #include <sys/cpu.h>
3544961713Sgirish #include <sys/elf_SPARC.h>
3644961713Sgirish #include <vm/hat_sfmmu.h>
3744961713Sgirish #include <vm/page.h>
38102033aaSdp78419 #include <vm/vm_dep.h>
3944961713Sgirish #include <sys/cpuvar.h>
4044961713Sgirish #include <sys/async.h>
4144961713Sgirish #include <sys/cmn_err.h>
4244961713Sgirish #include <sys/debug.h>
4344961713Sgirish #include <sys/dditypes.h>
4444961713Sgirish #include <sys/sunddi.h>
4544961713Sgirish #include <sys/cpu_module.h>
4644961713Sgirish #include <sys/prom_debug.h>
4744961713Sgirish #include <sys/vmsystm.h>
4844961713Sgirish #include <sys/prom_plat.h>
4944961713Sgirish #include <sys/sysmacros.h>
5044961713Sgirish #include <sys/intreg.h>
5144961713Sgirish #include <sys/machtrap.h>
5244961713Sgirish #include <sys/ontrap.h>
5344961713Sgirish #include <sys/ivintr.h>
5444961713Sgirish #include <sys/atomic.h>
5544961713Sgirish #include <sys/panic.h>
5644961713Sgirish #include <sys/dtrace.h>
5744961713Sgirish #include <sys/simulate.h>
5844961713Sgirish #include <sys/fault.h>
5944961713Sgirish #include <sys/niagara2regs.h>
6044961713Sgirish #include <sys/hsvc.h>
6144961713Sgirish #include <sys/trapstat.h>
6244961713Sgirish 
6344961713Sgirish uint_t root_phys_addr_lo_mask = 0xffffffffU;
6444961713Sgirish char cpu_module_name[] = "SUNW,UltraSPARC-T2";
6544961713Sgirish 
6644961713Sgirish /*
6744961713Sgirish  * Hypervisor services information for the NIAGARA2 CPU module
6844961713Sgirish  */
6944961713Sgirish static boolean_t niagara2_hsvc_available = B_TRUE;
7044961713Sgirish static uint64_t niagara2_sup_minor;		/* Supported minor number */
7144961713Sgirish static hsvc_info_t niagara2_hsvc = {
7244961713Sgirish 	HSVC_REV_1, NULL, HSVC_GROUP_NIAGARA2_CPU, NIAGARA2_HSVC_MAJOR,
7344961713Sgirish 	NIAGARA2_HSVC_MINOR, cpu_module_name
7444961713Sgirish };
7544961713Sgirish 
7644961713Sgirish void
7744961713Sgirish cpu_setup(void)
7844961713Sgirish {
7944961713Sgirish 	extern int mmu_exported_pagesize_mask;
8044961713Sgirish 	extern int cpc_has_overflow_intr;
81aaa10e67Sha137994 	extern size_t contig_mem_prealloc_base;
8244961713Sgirish 	int status;
8344961713Sgirish 
8444961713Sgirish 	/*
8544961713Sgirish 	 * Negotiate the API version for Niagara2 specific hypervisor
8644961713Sgirish 	 * services.
8744961713Sgirish 	 */
8844961713Sgirish 	status = hsvc_register(&niagara2_hsvc, &niagara2_sup_minor);
8944961713Sgirish 	if (status != 0) {
9044961713Sgirish 		cmn_err(CE_WARN, "%s: cannot negotiate hypervisor services "
9144961713Sgirish 		    "group: 0x%lx major: 0x%lx minor: 0x%lx errno: %d",
9244961713Sgirish 		    niagara2_hsvc.hsvc_modname, niagara2_hsvc.hsvc_group,
9344961713Sgirish 		    niagara2_hsvc.hsvc_major, niagara2_hsvc.hsvc_minor, status);
9444961713Sgirish 		niagara2_hsvc_available = B_FALSE;
9544961713Sgirish 	}
9644961713Sgirish 
9744961713Sgirish 	/*
9844961713Sgirish 	 * The setup common to all CPU modules is done in cpu_setup_common
9944961713Sgirish 	 * routine.
10044961713Sgirish 	 */
10144961713Sgirish 	cpu_setup_common(NULL);
10244961713Sgirish 
10344961713Sgirish 	cache |= (CACHE_PTAG | CACHE_IOCOHERENT);
10444961713Sgirish 
10544961713Sgirish 	if ((mmu_exported_pagesize_mask &
10644961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK) !=
10744961713Sgirish 	    DEFAULT_SUN4V_MMU_PAGESIZE_MASK)
10844961713Sgirish 		cmn_err(CE_PANIC, "machine description"
10944961713Sgirish 		    " does not have required sun4v page sizes"
11044961713Sgirish 		    " 8K, 64K and 4M: MD mask is 0x%x",
11144961713Sgirish 		    mmu_exported_pagesize_mask);
11244961713Sgirish 
11344961713Sgirish 	cpu_hwcap_flags = AV_SPARC_VIS | AV_SPARC_VIS2 | AV_SPARC_ASI_BLK_INIT;
11444961713Sgirish 
11544961713Sgirish 	/*
11644961713Sgirish 	 * Niagara2 supports a 48-bit subset of the full 64-bit virtual
11744961713Sgirish 	 * address space. Virtual addresses between 0x0000800000000000
11844961713Sgirish 	 * and 0xffff.7fff.ffff.ffff inclusive lie within a "VA Hole"
11944961713Sgirish 	 * and must never be mapped. In addition, software must not use
12044961713Sgirish 	 * pages within 4GB of the VA hole as instruction pages to
12144961713Sgirish 	 * avoid problems with prefetching into the VA hole.
12244961713Sgirish 	 */
12344961713Sgirish 	hole_start = (caddr_t)((1ull << (va_bits - 1)) - (1ull << 32));
12444961713Sgirish 	hole_end = (caddr_t)((0ull - (1ull << (va_bits - 1))) + (1ull << 32));
12544961713Sgirish 
12644961713Sgirish 	/*
12744961713Sgirish 	 * Niagara2 has a performance counter overflow interrupt
12844961713Sgirish 	 */
12944961713Sgirish 	cpc_has_overflow_intr = 1;
130102033aaSdp78419 
131102033aaSdp78419 	/*
132102033aaSdp78419 	 * Enable 4M pages for OOB.
133102033aaSdp78419 	 */
134102033aaSdp78419 	max_uheap_lpsize = MMU_PAGESIZE4M;
135102033aaSdp78419 	max_ustack_lpsize = MMU_PAGESIZE4M;
136102033aaSdp78419 	max_privmap_lpsize = MMU_PAGESIZE4M;
137aaa10e67Sha137994 
138aaa10e67Sha137994 	contig_mem_prealloc_base = NIAGARA2_PREALLOC_BASE;
13944961713Sgirish }
14044961713Sgirish 
14144961713Sgirish /*
14244961713Sgirish  * Set the magic constants of the implementation.
14344961713Sgirish  */
14444961713Sgirish void
14544961713Sgirish cpu_fiximp(struct cpu_node *cpunode)
14644961713Sgirish {
14744961713Sgirish 	/*
14844961713Sgirish 	 * The Cache node is optional in MD. Therefore in case "Cache"
14944961713Sgirish 	 * node does not exists in MD, set the default L2 cache associativity,
15044961713Sgirish 	 * size, linesize.
15144961713Sgirish 	 */
15244961713Sgirish 	if (cpunode->ecache_size == 0)
15344961713Sgirish 		cpunode->ecache_size = L2CACHE_SIZE;
15444961713Sgirish 	if (cpunode->ecache_linesize == 0)
15544961713Sgirish 		cpunode->ecache_linesize = L2CACHE_LINESIZE;
15644961713Sgirish 	if (cpunode->ecache_associativity == 0)
15744961713Sgirish 		cpunode->ecache_associativity = L2CACHE_ASSOCIATIVITY;
15844961713Sgirish }
15944961713Sgirish 
16044961713Sgirish void
161459190a5Srsmaeda cpu_map_exec_units(struct cpu *cp)
16244961713Sgirish {
163459190a5Srsmaeda 	ASSERT(MUTEX_HELD(&cpu_lock));
16444961713Sgirish 
16544961713Sgirish 	/*
166fb2f18f8Sesaxe 	 * The cpu_ipipe and cpu_fpu fields are initialized based on
167459190a5Srsmaeda 	 * the execution unit sharing information from the MD. They
168459190a5Srsmaeda 	 * default to the CPU id in the absence of such information.
16944961713Sgirish 	 */
17044961713Sgirish 	cp->cpu_m.cpu_ipipe = cpunodes[cp->cpu_id].exec_unit_mapping;
17144961713Sgirish 	if (cp->cpu_m.cpu_ipipe == NO_EU_MAPPING_FOUND)
17244961713Sgirish 		cp->cpu_m.cpu_ipipe = (id_t)(cp->cpu_id);
17344961713Sgirish 
174fb2f18f8Sesaxe 	cp->cpu_m.cpu_fpu = cpunodes[cp->cpu_id].fpu_mapping;
175fb2f18f8Sesaxe 	if (cp->cpu_m.cpu_fpu == NO_EU_MAPPING_FOUND)
176fb2f18f8Sesaxe 		cp->cpu_m.cpu_fpu = (id_t)(cp->cpu_id);
177fb2f18f8Sesaxe 
178fb2f18f8Sesaxe 	/*
179fb2f18f8Sesaxe 	 * Niagara 2 defines the core to be at the FPU level
180fb2f18f8Sesaxe 	 */
181fb2f18f8Sesaxe 	cp->cpu_m.cpu_core = cp->cpu_m.cpu_fpu;
182459190a5Srsmaeda }
183459190a5Srsmaeda 
184459190a5Srsmaeda static int niagara2_cpucnt;
185459190a5Srsmaeda 
186459190a5Srsmaeda void
187459190a5Srsmaeda cpu_init_private(struct cpu *cp)
188459190a5Srsmaeda {
189459190a5Srsmaeda 	extern void niagara_kstat_init(void);
190fb2f18f8Sesaxe 
19144961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
192459190a5Srsmaeda 
193459190a5Srsmaeda 	cpu_map_exec_units(cp);
194459190a5Srsmaeda 
19544961713Sgirish 	if ((niagara2_cpucnt++ == 0) && (niagara2_hsvc_available == B_TRUE))
196459190a5Srsmaeda 		niagara_kstat_init();
19744961713Sgirish }
19844961713Sgirish 
19944961713Sgirish /*ARGSUSED*/
20044961713Sgirish void
20144961713Sgirish cpu_uninit_private(struct cpu *cp)
20244961713Sgirish {
203459190a5Srsmaeda 	extern void niagara_kstat_fini(void);
20444961713Sgirish 
20544961713Sgirish 	ASSERT(MUTEX_HELD(&cpu_lock));
206459190a5Srsmaeda 
20744961713Sgirish 	if ((--niagara2_cpucnt == 0) && (niagara2_hsvc_available == B_TRUE))
208459190a5Srsmaeda 		niagara_kstat_fini();
20944961713Sgirish }
21044961713Sgirish 
21144961713Sgirish /*
21244961713Sgirish  * On Niagara2, any flush will cause all preceding stores to be
21344961713Sgirish  * synchronized wrt the i$, regardless of address or ASI.  In fact,
21444961713Sgirish  * the address is ignored, so we always flush address 0.
21544961713Sgirish  */
21644961713Sgirish /*ARGSUSED*/
21744961713Sgirish void
21844961713Sgirish dtrace_flush_sec(uintptr_t addr)
21944961713Sgirish {
22044961713Sgirish 	doflush(0);
22144961713Sgirish }
22244961713Sgirish 
22344961713Sgirish /*
22444961713Sgirish  * Trapstat support for Niagara2 processor
22544961713Sgirish  * The Niagara2 provides HWTW support for TSB lookup and with HWTW
22644961713Sgirish  * enabled no TSB hit information will be available. Therefore setting
22744961713Sgirish  * the time spent in TLB miss handler for TSB hits to 0.
22844961713Sgirish  */
22944961713Sgirish int
23044961713Sgirish cpu_trapstat_conf(int cmd)
23144961713Sgirish {
23244961713Sgirish 	int status = 0;
23344961713Sgirish 
23444961713Sgirish 	switch (cmd) {
23544961713Sgirish 	case CPU_TSTATCONF_INIT:
23644961713Sgirish 	case CPU_TSTATCONF_FINI:
23744961713Sgirish 	case CPU_TSTATCONF_ENABLE:
23844961713Sgirish 	case CPU_TSTATCONF_DISABLE:
23944961713Sgirish 		break;
24044961713Sgirish 	default:
24144961713Sgirish 		status = EINVAL;
24244961713Sgirish 		break;
24344961713Sgirish 	}
24444961713Sgirish 	return (status);
24544961713Sgirish }
24644961713Sgirish 
24744961713Sgirish void
24844961713Sgirish cpu_trapstat_data(void *buf, uint_t tstat_pgszs)
24944961713Sgirish {
25044961713Sgirish 	tstat_pgszdata_t	*tstatp = (tstat_pgszdata_t *)buf;
25144961713Sgirish 	int	i;
25244961713Sgirish 
25344961713Sgirish 	for (i = 0; i < tstat_pgszs; i++, tstatp++) {
25444961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_count = 0;
25544961713Sgirish 		tstatp->tpgsz_kernel.tmode_itlb.ttlb_tlb.tmiss_time = 0;
25644961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_count = 0;
25744961713Sgirish 		tstatp->tpgsz_user.tmode_itlb.ttlb_tlb.tmiss_time = 0;
25844961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
25944961713Sgirish 		tstatp->tpgsz_kernel.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
26044961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_count = 0;
26144961713Sgirish 		tstatp->tpgsz_user.tmode_dtlb.ttlb_tlb.tmiss_time = 0;
26244961713Sgirish 	}
26344961713Sgirish }
264102033aaSdp78419 
265102033aaSdp78419 /* NI2 L2$ index is pa[32:28]^pa[17:13].pa[19:18]^pa[12:11].pa[10:6] */
266102033aaSdp78419 uint_t
267102033aaSdp78419 page_pfn_2_color_cpu(pfn_t pfn, uchar_t szc)
268102033aaSdp78419 {
269102033aaSdp78419 	uint_t color;
270102033aaSdp78419 
271102033aaSdp78419 	ASSERT(szc <= TTE256M);
272102033aaSdp78419 
273102033aaSdp78419 	pfn = PFN_BASE(pfn, szc);
274102033aaSdp78419 	color = ((pfn >> 15) ^ pfn) & 0x1f;
275102033aaSdp78419 	if (szc >= TTE4M)
276102033aaSdp78419 		return (color);
277102033aaSdp78419 
278102033aaSdp78419 	color = (color << 2) | ((pfn >> 5) & 0x3);
279102033aaSdp78419 
280102033aaSdp78419 	return (szc <= TTE64K ? color : (color >> 1));
281102033aaSdp78419 }
282102033aaSdp78419 
283102033aaSdp78419 #if TTE256M != 5
284102033aaSdp78419 #error TTE256M is not 5
285102033aaSdp78419 #endif
286102033aaSdp78419 
287102033aaSdp78419 uint_t
288102033aaSdp78419 page_get_nsz_color_mask_cpu(uchar_t szc, uint_t mask)
289102033aaSdp78419 {
290102033aaSdp78419 	static uint_t ni2_color_masks[5] = {0x63, 0x1e, 0x3e, 0x1f, 0x1f};
291102033aaSdp78419 	ASSERT(szc < TTE256M);
292102033aaSdp78419 
293102033aaSdp78419 	mask &= ni2_color_masks[szc];
294102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (mask >> 1) : mask);
295102033aaSdp78419 }
296102033aaSdp78419 
297102033aaSdp78419 uint_t
298102033aaSdp78419 page_get_nsz_color_cpu(uchar_t szc, uint_t color)
299102033aaSdp78419 {
300102033aaSdp78419 	ASSERT(szc < TTE256M);
301102033aaSdp78419 	return ((szc == TTE64K || szc == TTE512K) ? (color >> 1) : color);
302102033aaSdp78419 }
303102033aaSdp78419 
304102033aaSdp78419 uint_t
305102033aaSdp78419 page_get_color_shift_cpu(uchar_t szc, uchar_t nszc)
306102033aaSdp78419 {
307*ef29e907Ssusans 	ASSERT(nszc >= szc);
308102033aaSdp78419 	ASSERT(nszc <= TTE256M);
309102033aaSdp78419 
310*ef29e907Ssusans 	if (szc == nszc)
311*ef29e907Ssusans 		return (0);
312102033aaSdp78419 	if (szc <= TTE64K)
313102033aaSdp78419 		return ((nszc >= TTE4M) ? 2 : ((nszc >= TTE512K) ? 1 : 0));
314102033aaSdp78419 	if (szc == TTE512K)
315102033aaSdp78419 		return (1);
316102033aaSdp78419 
317102033aaSdp78419 	return (0);
318102033aaSdp78419 }
319102033aaSdp78419 
320102033aaSdp78419 /*ARGSUSED*/
321102033aaSdp78419 pfn_t
322102033aaSdp78419 page_next_pfn_for_color_cpu(pfn_t pfn, uchar_t szc, uint_t color,
323102033aaSdp78419     uint_t ceq_mask, uint_t color_mask)
324102033aaSdp78419 {
325102033aaSdp78419 	pfn_t pstep = PNUM_SIZE(szc);
326102033aaSdp78419 	pfn_t npfn, pfn_ceq_mask, pfn_color;
327102033aaSdp78419 	pfn_t tmpmask, mask = (pfn_t)-1;
328102033aaSdp78419 
329102033aaSdp78419 	ASSERT((color & ~ceq_mask) == 0);
330102033aaSdp78419 
331102033aaSdp78419 	if (((page_pfn_2_color_cpu(pfn, szc) ^ color) & ceq_mask) == 0) {
332102033aaSdp78419 
333102033aaSdp78419 		/* we start from the page with correct color */
334102033aaSdp78419 		if (szc >= TTE512K) {
335102033aaSdp78419 			if (szc >= TTE4M) {
336102033aaSdp78419 				/* page color is PA[32:28] */
337102033aaSdp78419 				pfn_ceq_mask = ceq_mask << 15;
338102033aaSdp78419 			} else {
339102033aaSdp78419 				/* page color is PA[32:28].PA[19:19] */
340102033aaSdp78419 				pfn_ceq_mask = ((ceq_mask & 1) << 6) |
341102033aaSdp78419 				    ((ceq_mask >> 1) << 15);
342102033aaSdp78419 			}
343102033aaSdp78419 			pfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
344102033aaSdp78419 			return (pfn);
345102033aaSdp78419 		} else {
346102033aaSdp78419 			/*
347102033aaSdp78419 			 * We deal 64K or 8K page. Check if we could the
348102033aaSdp78419 			 * satisfy the request without changing PA[32:28]
349102033aaSdp78419 			 */
350102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
351102033aaSdp78419 			npfn = ADD_MASKED(pfn, pstep, pfn_ceq_mask, mask);
352102033aaSdp78419 
353102033aaSdp78419 			if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
354102033aaSdp78419 				return (npfn);
355102033aaSdp78419 
356102033aaSdp78419 			/*
357102033aaSdp78419 			 * for next pfn we have to change bits PA[32:28]
358102033aaSdp78419 			 * set PA[63:28] and PA[19:18] of the next pfn
359102033aaSdp78419 			 */
360102033aaSdp78419 			npfn = (pfn >> 15) << 15;
361102033aaSdp78419 			npfn |= (ceq_mask & color & 3) << 5;
362102033aaSdp78419 			pfn_ceq_mask = (szc == TTE8K) ? 0 :
363102033aaSdp78419 			    (ceq_mask & 0x1c) << 13;
364102033aaSdp78419 			npfn = ADD_MASKED(npfn, (1 << 15), pfn_ceq_mask, mask);
365102033aaSdp78419 
366102033aaSdp78419 			/*
367102033aaSdp78419 			 * set bits PA[17:13] to match the color
368102033aaSdp78419 			 */
369102033aaSdp78419 			ceq_mask >>= 2;
370102033aaSdp78419 			color = (color >> 2) & ceq_mask;
371102033aaSdp78419 			npfn |= ((npfn >> 15) ^ color) & ceq_mask;
372102033aaSdp78419 			return (npfn);
373102033aaSdp78419 		}
374102033aaSdp78419 	}
375102033aaSdp78419 
376102033aaSdp78419 	/*
377102033aaSdp78419 	 * we start from the page with incorrect color - rare case
378102033aaSdp78419 	 */
379102033aaSdp78419 	if (szc >= TTE512K) {
380102033aaSdp78419 		if (szc >= TTE4M) {
381102033aaSdp78419 			/* page color is in bits PA[32:28] */
382102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | (color << 15);
383102033aaSdp78419 			pfn_ceq_mask = (ceq_mask << 15) | 0x7fff;
384102033aaSdp78419 		} else {
385102033aaSdp78419 			/* try get the right color by changing bit PA[19:19] */
386102033aaSdp78419 			npfn = pfn + pstep;
387102033aaSdp78419 			if (((page_pfn_2_color_cpu(npfn, szc) ^ color) &
388102033aaSdp78419 			    ceq_mask) == 0)
389102033aaSdp78419 				return (npfn);
390102033aaSdp78419 
391102033aaSdp78419 			/* page color is PA[32:28].PA[19:19] */
392102033aaSdp78419 			pfn_ceq_mask = ((ceq_mask & 1) << 6) |
393102033aaSdp78419 			    ((ceq_mask >> 1) << 15) | (0xff << 7);
394102033aaSdp78419 			pfn_color = ((color & 1) << 6) | ((color >> 1) << 15);
395102033aaSdp78419 			npfn = ((pfn >> 20) << 20) | pfn_color;
396102033aaSdp78419 		}
397102033aaSdp78419 
398102033aaSdp78419 		while (npfn <= pfn) {
399102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
400102033aaSdp78419 		}
401102033aaSdp78419 		return (npfn);
402102033aaSdp78419 	}
403102033aaSdp78419 
404102033aaSdp78419 	/*
405102033aaSdp78419 	 * We deal 64K or 8K page of incorrect color.
406102033aaSdp78419 	 * Try correcting color without changing PA[32:28]
407102033aaSdp78419 	 */
408102033aaSdp78419 
409102033aaSdp78419 	pfn_ceq_mask = ((ceq_mask & 3) << 5) | (ceq_mask >> 2);
410102033aaSdp78419 	pfn_color = ((color & 3) << 5) | (color >> 2);
411102033aaSdp78419 	npfn = (pfn & ~(pfn_t)0x7f);
412102033aaSdp78419 	npfn |= (((pfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
413102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
414102033aaSdp78419 
415102033aaSdp78419 	if (((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0) {
416102033aaSdp78419 
417102033aaSdp78419 		/* the color is fixed - find the next page */
418102033aaSdp78419 		while (npfn <= pfn) {
419102033aaSdp78419 			npfn = ADD_MASKED(npfn, pstep, pfn_ceq_mask, mask);
420102033aaSdp78419 		}
421102033aaSdp78419 		if ((((npfn ^ pfn) >> 15) & 0x1f) == 0)
422102033aaSdp78419 			return (npfn);
423102033aaSdp78419 	}
424102033aaSdp78419 
425102033aaSdp78419 	/* to fix the color need to touch PA[32:28] */
426102033aaSdp78419 	npfn = (szc == TTE8K) ? ((pfn >> 15) << 15) :
427102033aaSdp78419 	    (((pfn >> 18) << 18) | ((color & 0x1c) << 13));
428102033aaSdp78419 	tmpmask = (szc == TTE8K) ? 0 : (ceq_mask & 0x1c) << 13;
429102033aaSdp78419 
430102033aaSdp78419 	while (npfn <= pfn) {
431102033aaSdp78419 		npfn = ADD_MASKED(npfn, (1 << 15), tmpmask, mask);
432102033aaSdp78419 	}
433102033aaSdp78419 
434102033aaSdp78419 	/* set bits PA[19:13] to match the color */
435102033aaSdp78419 	npfn |= (((npfn >> 15) & 0x1f) ^ pfn_color) & pfn_ceq_mask;
436102033aaSdp78419 	npfn = (szc == TTE64K) ? (npfn & ~(pfn_t)0x7) : npfn;
437102033aaSdp78419 
438102033aaSdp78419 	ASSERT(((page_pfn_2_color_cpu(npfn, szc) ^ color) & ceq_mask) == 0);
439102033aaSdp78419 
440102033aaSdp78419 	return (npfn);
441102033aaSdp78419 }
442102033aaSdp78419 
443102033aaSdp78419 /*
444102033aaSdp78419  * init page coloring
445102033aaSdp78419  */
446102033aaSdp78419 void
447102033aaSdp78419 page_coloring_init_cpu()
448102033aaSdp78419 {
449102033aaSdp78419 	int i;
450102033aaSdp78419 
451102033aaSdp78419 	hw_page_array[0].hp_colors = 1 << 7;
452102033aaSdp78419 	hw_page_array[1].hp_colors = 1 << 7;
453102033aaSdp78419 	hw_page_array[2].hp_colors = 1 << 6;
454102033aaSdp78419 
455102033aaSdp78419 	for (i = 3; i < mmu_page_sizes; i++) {
456102033aaSdp78419 		hw_page_array[i].hp_colors = 1 << 5;
457102033aaSdp78419 	}
458102033aaSdp78419 }
459fe70c9cfSdp78419 
460fe70c9cfSdp78419 /*
461fe70c9cfSdp78419  * group colorequiv colors on N2 by low order bits of the color first
462fe70c9cfSdp78419  */
463fe70c9cfSdp78419 void
464fe70c9cfSdp78419 page_set_colorequiv_arr_cpu(void)
465fe70c9cfSdp78419 {
466fe70c9cfSdp78419 	static uint_t nequiv_shades_log2[MMU_PAGE_SIZES] = {2, 5, 0, 0, 0, 0};
467fe70c9cfSdp78419 
468fe70c9cfSdp78419 	if (colorequiv > 1) {
469fe70c9cfSdp78419 		int i;
470fe70c9cfSdp78419 		uint_t sv_a = lowbit(colorequiv) - 1;
471fe70c9cfSdp78419 
472fe70c9cfSdp78419 		if (sv_a > 15)
473fe70c9cfSdp78419 			sv_a = 15;
474fe70c9cfSdp78419 
475fe70c9cfSdp78419 		for (i = 0; i < MMU_PAGE_SIZES; i++) {
476fe70c9cfSdp78419 			uint_t colors;
477fe70c9cfSdp78419 			uint_t a = sv_a;
478fe70c9cfSdp78419 
479fe70c9cfSdp78419 			if ((colors = hw_page_array[i].hp_colors) <= 1)
480fe70c9cfSdp78419 				continue;
481fe70c9cfSdp78419 			while ((colors >> a) == 0)
482fe70c9cfSdp78419 				a--;
483fe70c9cfSdp78419 			if (a > (colorequivszc[i] & 0xf) +
484fe70c9cfSdp78419 			    (colorequivszc[i] >> 4)) {
485fe70c9cfSdp78419 				if (a <= nequiv_shades_log2[i]) {
486fe70c9cfSdp78419 					colorequivszc[i] = a;
487fe70c9cfSdp78419 				} else {
488fe70c9cfSdp78419 					colorequivszc[i] =
489fe70c9cfSdp78419 					    ((a - nequiv_shades_log2[i]) << 4) |
490fe70c9cfSdp78419 					    nequiv_shades_log2[i];
491fe70c9cfSdp78419 				}
492fe70c9cfSdp78419 			}
493fe70c9cfSdp78419 		}
494fe70c9cfSdp78419 	}
495fe70c9cfSdp78419 }
496