xref: /onnv-gate/usr/src/uts/sun4/os/x_call.c (revision 6625)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52973Sgovinda  * Common Development and Distribution License (the "License").
62973Sgovinda  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226138Ssvemuri  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/archsystm.h>
300Sstevel@tonic-gate #include <sys/machsystm.h>
310Sstevel@tonic-gate #include <sys/cpuvar.h>
320Sstevel@tonic-gate #include <sys/intreg.h>
330Sstevel@tonic-gate #include <sys/x_call.h>
340Sstevel@tonic-gate #include <sys/cmn_err.h>
350Sstevel@tonic-gate #include <sys/membar.h>
360Sstevel@tonic-gate #include <sys/disp.h>
370Sstevel@tonic-gate #include <sys/debug.h>
380Sstevel@tonic-gate #include <sys/privregs.h>
390Sstevel@tonic-gate #include <sys/xc_impl.h>
400Sstevel@tonic-gate #include <sys/ivintr.h>
410Sstevel@tonic-gate #include <sys/dmv.h>
42193Sjkennedy #include <sys/sysmacros.h>
430Sstevel@tonic-gate 
441395Srjnoe #ifdef TRAPTRACE
450Sstevel@tonic-gate uint_t x_dstat[NCPU][XC_LOOP_EXIT+1];
460Sstevel@tonic-gate uint_t x_rstat[NCPU][4];
471395Srjnoe #endif /* TRAPTRACE */
480Sstevel@tonic-gate 
492973Sgovinda static uint64_t xc_serv_inum;	/* software interrupt number for xc_serv() */
502973Sgovinda static uint64_t xc_loop_inum;	/* software interrupt number for xc_loop() */
510Sstevel@tonic-gate kmutex_t xc_sys_mutex;		/* protect xcall session and xc_mbox */
520Sstevel@tonic-gate int xc_spl_enter[NCPU];		/* protect sending x-call */
530Sstevel@tonic-gate static int xc_holder = -1; /* the cpu who initiates xc_attention, 0 is valid */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * Mail box for handshaking and xcall request; protected by xc_sys_mutex
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate static struct xc_mbox {
590Sstevel@tonic-gate 	xcfunc_t *xc_func;
600Sstevel@tonic-gate 	uint64_t xc_arg1;
610Sstevel@tonic-gate 	uint64_t xc_arg2;
620Sstevel@tonic-gate 	cpuset_t xc_cpuset;
630Sstevel@tonic-gate 	volatile uint_t	xc_state;
640Sstevel@tonic-gate } xc_mbox[NCPU];
650Sstevel@tonic-gate 
660Sstevel@tonic-gate uint64_t xc_tick_limit;		/* send_mondo() tick limit value */
670Sstevel@tonic-gate uint64_t xc_tick_limit_scale = 1;	/* scale used to increase the limit */
680Sstevel@tonic-gate uint64_t xc_tick_jump_limit;	/* send_mondo() irregular tick jump limit */
696138Ssvemuri uint64_t xc_sync_tick_limit;	/* timeout limit for xt_sync() calls */
700Sstevel@tonic-gate 
710Sstevel@tonic-gate /* timeout value for xcalls to be received by the target CPU */
720Sstevel@tonic-gate uint64_t xc_mondo_time_limit;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /* timeout value for xcall functions to be executed on the target CPU */
750Sstevel@tonic-gate uint64_t xc_func_time_limit;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate uint64_t xc_scale = 1;	/* scale used to calculate timeout limits */
78193Sjkennedy uint64_t xc_mondo_multiplier = 10;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate uint_t sendmondo_in_recover;
810Sstevel@tonic-gate 
820Sstevel@tonic-gate /*
830Sstevel@tonic-gate  * sending x-calls
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate void	init_mondo(xcfunc_t *func, uint64_t arg1, uint64_t arg2);
860Sstevel@tonic-gate void	send_one_mondo(int cpuid);
870Sstevel@tonic-gate void	send_mondo_set(cpuset_t set);
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
90193Sjkennedy  * Adjust xc_attention timeout if a faster cpu is dynamically added.
91193Sjkennedy  * Ignore the dynamic removal of a cpu that would lower these timeout
92193Sjkennedy  * values.
93193Sjkennedy  */
94193Sjkennedy static int
95193Sjkennedy xc_func_timeout_adj(cpu_setup_t what, int cpuid) {
96193Sjkennedy 	uint64_t freq = cpunodes[cpuid].clock_freq;
97193Sjkennedy 
98193Sjkennedy 	switch (what) {
99193Sjkennedy 	case CPU_ON:
100193Sjkennedy 	case CPU_INIT:
101193Sjkennedy 	case CPU_CONFIG:
102193Sjkennedy 	case CPU_CPUPART_IN:
103193Sjkennedy 		if (freq * xc_scale > xc_mondo_time_limit) {
104193Sjkennedy 			xc_mondo_time_limit = freq * xc_scale;
105193Sjkennedy 			xc_func_time_limit = xc_mondo_time_limit *
106193Sjkennedy 			    xc_mondo_multiplier;
107193Sjkennedy 		}
108193Sjkennedy 		break;
109193Sjkennedy 	case CPU_OFF:
110193Sjkennedy 	case CPU_UNCONFIG:
111193Sjkennedy 	case CPU_CPUPART_OUT:
112193Sjkennedy 	default:
113193Sjkennedy 		break;
114193Sjkennedy 	}
115193Sjkennedy 
116193Sjkennedy 	return (0);
117193Sjkennedy }
118193Sjkennedy 
119193Sjkennedy /*
1200Sstevel@tonic-gate  * xc_init - initialize x-call related locks
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate void
1230Sstevel@tonic-gate xc_init(void)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	int pix;
126193Sjkennedy 	uint64_t maxfreq = 0;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	mutex_init(&xc_sys_mutex, NULL, MUTEX_SPIN,
1290Sstevel@tonic-gate 	    (void *)ipltospl(XCALL_PIL));
1300Sstevel@tonic-gate 
1311395Srjnoe #ifdef TRAPTRACE
1320Sstevel@tonic-gate 	/* Initialize for all possible CPUs. */
1330Sstevel@tonic-gate 	for (pix = 0; pix < NCPU; pix++) {
1340Sstevel@tonic-gate 		XC_STAT_INIT(pix);
1350Sstevel@tonic-gate 	}
1361395Srjnoe #endif /* TRAPTRACE */
1370Sstevel@tonic-gate 
1382973Sgovinda 	xc_serv_inum = add_softintr(XCALL_PIL, (softintrfunc)xc_serv, 0,
1392973Sgovinda 	    SOFTINT_MT);
1402973Sgovinda 	xc_loop_inum = add_softintr(XCALL_PIL, (softintrfunc)xc_loop, 0,
1412973Sgovinda 	    SOFTINT_MT);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/*
1440Sstevel@tonic-gate 	 * Initialize the calibrated tick limit for send_mondo.
1450Sstevel@tonic-gate 	 * The value represents the maximum tick count to wait.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	xc_tick_limit =
1480Sstevel@tonic-gate 	    ((uint64_t)sys_tick_freq * XC_SEND_MONDO_MSEC) / 1000;
1490Sstevel@tonic-gate 	xc_tick_jump_limit = xc_tick_limit / 32;
1500Sstevel@tonic-gate 	xc_tick_limit *= xc_tick_limit_scale;
1516138Ssvemuri 	xc_sync_tick_limit = xc_tick_limit;
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	/*
1540Sstevel@tonic-gate 	 * Maximum number of loops to wait before timing out in xc_attention.
1550Sstevel@tonic-gate 	 */
156193Sjkennedy 	for (pix = 0; pix < NCPU; pix++) {
157193Sjkennedy 		maxfreq = MAX(cpunodes[pix].clock_freq, maxfreq);
158193Sjkennedy 	}
159193Sjkennedy 	xc_mondo_time_limit = maxfreq * xc_scale;
160193Sjkennedy 	register_cpu_setup_func((cpu_setup_func_t *)xc_func_timeout_adj, NULL);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	/*
1630Sstevel@tonic-gate 	 * Maximum number of loops to wait for a xcall function to be
164193Sjkennedy 	 * executed on the target CPU.
1650Sstevel@tonic-gate 	 */
166193Sjkennedy 	xc_func_time_limit = xc_mondo_time_limit * xc_mondo_multiplier;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * The following routines basically provide callers with two kinds of
1710Sstevel@tonic-gate  * inter-processor interrupt services:
1720Sstevel@tonic-gate  *	1. cross calls (x-calls) - requests are handled at target cpu's TL=0
1730Sstevel@tonic-gate  *	2. cross traps (c-traps) - requests are handled at target cpu's TL>0
1740Sstevel@tonic-gate  *
1750Sstevel@tonic-gate  * Although these routines protect the services from migrating to other cpus
1760Sstevel@tonic-gate  * "after" they are called, it is the caller's choice or responsibility to
1770Sstevel@tonic-gate  * prevent the cpu migration "before" calling them.
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  * X-call routines:
1800Sstevel@tonic-gate  *
1810Sstevel@tonic-gate  *	xc_one()  - send a request to one processor
1820Sstevel@tonic-gate  *	xc_some() - send a request to some processors
1830Sstevel@tonic-gate  *	xc_all()  - send a request to all processors
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  *	Their common parameters:
1860Sstevel@tonic-gate  *		func - a TL=0 handler address
1870Sstevel@tonic-gate  *		arg1 and arg2  - optional
1880Sstevel@tonic-gate  *
1890Sstevel@tonic-gate  *	The services provided by x-call routines allow callers
1900Sstevel@tonic-gate  *	to send a request to target cpus to execute a TL=0
1910Sstevel@tonic-gate  *	handler.
1920Sstevel@tonic-gate  *	The interface of the registers of the TL=0 handler:
1930Sstevel@tonic-gate  *		%o0: arg1
1940Sstevel@tonic-gate  *		%o1: arg2
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * X-trap routines:
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  *	xt_one()  - send a request to one processor
1990Sstevel@tonic-gate  *	xt_some() - send a request to some processors
2000Sstevel@tonic-gate  *	xt_all()  - send a request to all processors
2010Sstevel@tonic-gate  *
2020Sstevel@tonic-gate  *	Their common parameters:
2030Sstevel@tonic-gate  *		func - a TL>0 handler address or an interrupt number
2040Sstevel@tonic-gate  *		arg1, arg2
2050Sstevel@tonic-gate  *		       optional when "func" is an address;
2060Sstevel@tonic-gate  *		       0        when "func" is an interrupt number
2070Sstevel@tonic-gate  *
2080Sstevel@tonic-gate  *	If the request of "func" is a kernel address, then
2090Sstevel@tonic-gate  *	the target cpu will execute the request of "func" with
2100Sstevel@tonic-gate  *	args at "TL>0" level.
2110Sstevel@tonic-gate  *	The interface of the registers of the TL>0 handler:
2120Sstevel@tonic-gate  *		%g1: arg1
2130Sstevel@tonic-gate  *		%g2: arg2
2140Sstevel@tonic-gate  *
2150Sstevel@tonic-gate  *	If the request of "func" is not a kernel address, then it has
2160Sstevel@tonic-gate  *	to be an assigned interrupt number through add_softintr().
2170Sstevel@tonic-gate  *	An interrupt number is an index to the interrupt vector table,
2180Sstevel@tonic-gate  *	which entry contains an interrupt handler address with its
2190Sstevel@tonic-gate  *	corresponding interrupt level and argument.
2200Sstevel@tonic-gate  *	The target cpu will arrange the request to be serviced according
2210Sstevel@tonic-gate  *	to its pre-registered information.
2220Sstevel@tonic-gate  *	args are assumed to be zeros in this case.
2230Sstevel@tonic-gate  *
2240Sstevel@tonic-gate  * In addition, callers are allowed to capture and release cpus by
2250Sstevel@tonic-gate  * calling the routines: xc_attention() and xc_dismissed().
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate /*
2290Sstevel@tonic-gate  * xt_one - send a "x-trap" to a cpu
2300Sstevel@tonic-gate  */
2310Sstevel@tonic-gate void
2320Sstevel@tonic-gate xt_one(int cix, xcfunc_t *func, uint64_t arg1, uint64_t arg2)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	if (!CPU_IN_SET(cpu_ready_set, cix)) {
2350Sstevel@tonic-gate 		return;
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	xt_one_unchecked(cix, func, arg1, arg2);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate  * xt_one_unchecked - send a "x-trap" to a cpu without checking for its
2420Sstevel@tonic-gate  * existance in cpu_ready_set
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate void
2450Sstevel@tonic-gate xt_one_unchecked(int cix, xcfunc_t *func, uint64_t arg1, uint64_t arg2)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	int lcx;
2480Sstevel@tonic-gate 	int opl;
2490Sstevel@tonic-gate 	cpuset_t tset;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Make sure the function address will not be interpreted as a
2530Sstevel@tonic-gate 	 * dmv interrupt
2540Sstevel@tonic-gate 	 */
2550Sstevel@tonic-gate 	ASSERT(!DMV_IS_DMV(func));
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	/*
2580Sstevel@tonic-gate 	 * It's illegal to send software inums through the cross-trap
2590Sstevel@tonic-gate 	 * interface.
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	ASSERT((uintptr_t)func >= KERNELBASE);
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	CPUSET_ZERO(tset);
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);			/* lcx set by the macro */
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate 	CPUSET_ADD(tset, cix);
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	if (cix == lcx) {
2700Sstevel@tonic-gate 		/*
2710Sstevel@tonic-gate 		 * same cpu - use software fast trap
2720Sstevel@tonic-gate 		 */
2730Sstevel@tonic-gate 		send_self_xcall(CPU, arg1, arg2, func);
2740Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XT_ONE_SELF]);
2750Sstevel@tonic-gate 		XC_TRACE(XT_ONE_SELF, &tset, func, arg1, arg2);
2760Sstevel@tonic-gate 	} else {	/* other cpu - send a mondo to the target cpu */
2770Sstevel@tonic-gate 		/*
2780Sstevel@tonic-gate 		 * other cpu - send a mondo to the target cpu
2790Sstevel@tonic-gate 		 */
2800Sstevel@tonic-gate 		XC_TRACE(XT_ONE_OTHER, &tset, func, arg1, arg2);
2810Sstevel@tonic-gate 		init_mondo(func, arg1, arg2);
2820Sstevel@tonic-gate 		send_one_mondo(cix);
2830Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XT_ONE_OTHER]);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate /*
2890Sstevel@tonic-gate  * xt_some - send a "x-trap" to some cpus
2900Sstevel@tonic-gate  */
2910Sstevel@tonic-gate void
2920Sstevel@tonic-gate xt_some(cpuset_t cpuset, xcfunc_t *func, uint64_t arg1, uint64_t arg2)
2930Sstevel@tonic-gate {
2940Sstevel@tonic-gate 	int lcx;
2950Sstevel@tonic-gate 	int opl;
2960Sstevel@tonic-gate 	cpuset_t xc_cpuset, tset;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Make sure the function address will not be interpreted as a
3000Sstevel@tonic-gate 	 * dmv interrupt
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	ASSERT(!DMV_IS_DMV(func));
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * It's illegal to send software inums through the cross-trap
3060Sstevel@tonic-gate 	 * interface.
3070Sstevel@tonic-gate 	 */
3080Sstevel@tonic-gate 	ASSERT((uintptr_t)func >= KERNELBASE);
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	CPUSET_ZERO(tset);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);		/* lcx set by the macro */
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	/*
3170Sstevel@tonic-gate 	 * only send to the CPU_READY ones
3180Sstevel@tonic-gate 	 */
3190Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
3200Sstevel@tonic-gate 	CPUSET_AND(xc_cpuset, cpuset);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * send to nobody; just return
3240Sstevel@tonic-gate 	 */
3250Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset)) {
3260Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
3270Sstevel@tonic-gate 		return;
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	/*
3310Sstevel@tonic-gate 	 * don't send mondo to self
3320Sstevel@tonic-gate 	 */
3330Sstevel@tonic-gate 	if (CPU_IN_SET(xc_cpuset, lcx)) {
3340Sstevel@tonic-gate 		/*
3350Sstevel@tonic-gate 		 * same cpu - use software fast trap
3360Sstevel@tonic-gate 		 */
3370Sstevel@tonic-gate 		send_self_xcall(CPU, arg1, arg2, func);
3380Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XT_SOME_SELF]);
3390Sstevel@tonic-gate 		XC_TRACE(XT_SOME_SELF, &tset, func, arg1, arg2);
3400Sstevel@tonic-gate 		CPUSET_DEL(xc_cpuset, lcx);
3410Sstevel@tonic-gate 		if (CPUSET_ISNULL(xc_cpuset)) {
3420Sstevel@tonic-gate 			XC_SPL_EXIT(lcx, opl);
3430Sstevel@tonic-gate 			return;
3440Sstevel@tonic-gate 		}
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 	XC_TRACE(XT_SOME_OTHER, &xc_cpuset, func, arg1, arg2);
3470Sstevel@tonic-gate 	init_mondo(func, arg1, arg2);
3480Sstevel@tonic-gate 	send_mondo_set(xc_cpuset);
3490Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XT_SOME_OTHER]);
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
3520Sstevel@tonic-gate }
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*
3550Sstevel@tonic-gate  * xt_all - send a "x-trap" to all cpus
3560Sstevel@tonic-gate  */
3570Sstevel@tonic-gate void
3580Sstevel@tonic-gate xt_all(xcfunc_t *func, uint64_t arg1, uint64_t arg2)
3590Sstevel@tonic-gate {
3600Sstevel@tonic-gate 	int lcx;
3610Sstevel@tonic-gate 	int opl;
3620Sstevel@tonic-gate 	cpuset_t xc_cpuset, tset;
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	/*
3650Sstevel@tonic-gate 	 * Make sure the function address will not be interpreted as a
3660Sstevel@tonic-gate 	 * dmv interrupt
3670Sstevel@tonic-gate 	 */
3680Sstevel@tonic-gate 	ASSERT(!DMV_IS_DMV(func));
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	/*
3710Sstevel@tonic-gate 	 * It's illegal to send software inums through the cross-trap
3720Sstevel@tonic-gate 	 * interface.
3730Sstevel@tonic-gate 	 */
3740Sstevel@tonic-gate 	ASSERT((uintptr_t)func >= KERNELBASE);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	CPUSET_ZERO(tset);
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);		/* lcx set by the macro */
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate 	/*
3830Sstevel@tonic-gate 	 * same cpu - use software fast trap
3840Sstevel@tonic-gate 	 */
3850Sstevel@tonic-gate 	if (CPU_IN_SET(cpu_ready_set, lcx))
3860Sstevel@tonic-gate 		send_self_xcall(CPU, arg1, arg2, func);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	XC_TRACE(XT_ALL_OTHER, &cpu_ready_set, func, arg1, arg2);
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	/*
3910Sstevel@tonic-gate 	 * don't send mondo to self
3920Sstevel@tonic-gate 	 */
3930Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
3940Sstevel@tonic-gate 	CPUSET_DEL(xc_cpuset, lcx);
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset)) {
3970Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XT_ALL_SELF]);
3980Sstevel@tonic-gate 		XC_TRACE(XT_ALL_SELF, &tset, func, arg1, arg2);
3990Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
4000Sstevel@tonic-gate 		return;
4010Sstevel@tonic-gate 	}
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 	init_mondo(func, arg1, arg2);
4040Sstevel@tonic-gate 	send_mondo_set(xc_cpuset);
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XT_ALL_OTHER]);
4070Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
4080Sstevel@tonic-gate }
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate /*
4110Sstevel@tonic-gate  * xc_one - send a "x-call" to a cpu
4120Sstevel@tonic-gate  */
4130Sstevel@tonic-gate void
4140Sstevel@tonic-gate xc_one(int cix, xcfunc_t *func, uint64_t arg1, uint64_t arg2)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	int lcx;
4170Sstevel@tonic-gate 	int opl;
4180Sstevel@tonic-gate 	uint64_t loop_cnt = 0;
4190Sstevel@tonic-gate 	cpuset_t tset;
4200Sstevel@tonic-gate 	int first_time = 1;
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	/*
4230Sstevel@tonic-gate 	 * send to nobody; just return
4240Sstevel@tonic-gate 	 */
4250Sstevel@tonic-gate 	if (!CPU_IN_SET(cpu_ready_set, cix))
4260Sstevel@tonic-gate 		return;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 	ASSERT((uintptr_t)func > KERNELBASE);
4290Sstevel@tonic-gate 	ASSERT(((uintptr_t)func % PC_ALIGN) == 0);
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	CPUSET_ZERO(tset);
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	kpreempt_disable();
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);		/* lcx set by the macro */
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate 	CPUSET_ADD(tset, cix);
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate 	if (cix == lcx) {	/* same cpu just do it */
4400Sstevel@tonic-gate 		XC_TRACE(XC_ONE_SELF, &tset, func, arg1, arg2);
4410Sstevel@tonic-gate 		(*func)(arg1, arg2);
4420Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XC_ONE_SELF]);
4430Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
4440Sstevel@tonic-gate 		kpreempt_enable();
4450Sstevel@tonic-gate 		return;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (xc_holder == lcx) {		/* got the xc_sys_mutex already */
4490Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&xc_sys_mutex));
4500Sstevel@tonic-gate 		ASSERT(CPU_IN_SET(xc_mbox[lcx].xc_cpuset, lcx));
4510Sstevel@tonic-gate 		ASSERT(CPU_IN_SET(xc_mbox[cix].xc_cpuset, cix));
4520Sstevel@tonic-gate 		ASSERT(xc_mbox[cix].xc_state == XC_WAIT);
4530Sstevel@tonic-gate 		XC_TRACE(XC_ONE_OTHER_H, &tset, func, arg1, arg2);
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 		/*
4560Sstevel@tonic-gate 		 * target processor's xc_loop should be waiting
4570Sstevel@tonic-gate 		 * for the work to do; just set up the xc_mbox
4580Sstevel@tonic-gate 		 */
4590Sstevel@tonic-gate 		XC_SETUP(cix, func, arg1, arg2);
4600Sstevel@tonic-gate 		membar_stld();
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 		while (xc_mbox[cix].xc_state != XC_WAIT) {
4630Sstevel@tonic-gate 			if (loop_cnt++ > xc_func_time_limit) {
4640Sstevel@tonic-gate 				if (sendmondo_in_recover) {
4650Sstevel@tonic-gate 					drv_usecwait(1);
4660Sstevel@tonic-gate 					loop_cnt = 0;
4670Sstevel@tonic-gate 					continue;
4680Sstevel@tonic-gate 				}
4690Sstevel@tonic-gate 				cmn_err(CE_PANIC, "xc_one() timeout, "
4700Sstevel@tonic-gate 				    "xc_state[%d] != XC_WAIT", cix);
4710Sstevel@tonic-gate 			}
4720Sstevel@tonic-gate 		}
4730Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XC_ONE_OTHER_H]);
4740Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
4750Sstevel@tonic-gate 		kpreempt_enable();
4760Sstevel@tonic-gate 		return;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Avoid dead lock if someone has sent us a xc_loop request while
4810Sstevel@tonic-gate 	 * we are trying to grab xc_sys_mutex.
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * At this point, since we don't own xc_sys_mutex,
4870Sstevel@tonic-gate 	 * our pil shouldn't run at or above the XCALL_PIL.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	ASSERT(getpil() < XCALL_PIL);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * Since xc_holder is not owned by us, it could be that
4930Sstevel@tonic-gate 	 * no one owns it, or we are not informed to enter into
4940Sstevel@tonic-gate 	 * xc_loop(). In either case, we need to grab the
4950Sstevel@tonic-gate 	 * xc_sys_mutex before we write to the xc_mbox, and
4960Sstevel@tonic-gate 	 * we shouldn't release it until the request is finished.
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	mutex_enter(&xc_sys_mutex);
5000Sstevel@tonic-gate 	xc_spl_enter[lcx] = 1;
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * Since we own xc_sys_mutex now, we are safe to
504*6625Scb222892 	 * write to the xc_mbox.
5050Sstevel@tonic-gate 	 */
5060Sstevel@tonic-gate 	ASSERT(xc_mbox[cix].xc_state == XC_IDLE);
5070Sstevel@tonic-gate 	XC_TRACE(XC_ONE_OTHER, &tset, func, arg1, arg2);
5080Sstevel@tonic-gate 	XC_SETUP(cix, func, arg1, arg2);
5090Sstevel@tonic-gate 	init_mondo(setsoftint_tl1, xc_serv_inum, 0);
5100Sstevel@tonic-gate 	send_one_mondo(cix);
511*6625Scb222892 	xc_spl_enter[lcx] = 0;
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/* xc_serv does membar_stld */
5140Sstevel@tonic-gate 	while (xc_mbox[cix].xc_state != XC_IDLE) {
5150Sstevel@tonic-gate 		if (loop_cnt++ > xc_func_time_limit) {
5160Sstevel@tonic-gate 			if (sendmondo_in_recover) {
5170Sstevel@tonic-gate 				drv_usecwait(1);
5180Sstevel@tonic-gate 				loop_cnt = 0;
5190Sstevel@tonic-gate 				continue;
5200Sstevel@tonic-gate 			}
5210Sstevel@tonic-gate 			if (first_time) {
5220Sstevel@tonic-gate 				XT_SYNC_ONE(cix);
5230Sstevel@tonic-gate 				first_time = 0;
5240Sstevel@tonic-gate 				loop_cnt = 0;
5250Sstevel@tonic-gate 				continue;
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 			cmn_err(CE_PANIC, "xc_one() timeout, "
5280Sstevel@tonic-gate 			    "xc_state[%d] != XC_IDLE", cix);
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	}
5310Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XC_ONE_OTHER]);
5320Sstevel@tonic-gate 	mutex_exit(&xc_sys_mutex);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	kpreempt_enable();
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate /*
5380Sstevel@tonic-gate  * xc_some - send a "x-call" to some cpus; sending to self is excluded
5390Sstevel@tonic-gate  */
5400Sstevel@tonic-gate void
5410Sstevel@tonic-gate xc_some(cpuset_t cpuset, xcfunc_t *func, uint64_t arg1, uint64_t arg2)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int lcx;
5440Sstevel@tonic-gate 	int opl;
5450Sstevel@tonic-gate 	cpuset_t xc_cpuset, tset;
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	ASSERT((uintptr_t)func > KERNELBASE);
5480Sstevel@tonic-gate 	ASSERT(((uintptr_t)func % PC_ALIGN) == 0);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	CPUSET_ZERO(tset);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	kpreempt_disable();
5530Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);			/* lcx set by the macro */
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	/*
5580Sstevel@tonic-gate 	 * only send to the CPU_READY ones
5590Sstevel@tonic-gate 	 */
5600Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
5610Sstevel@tonic-gate 	CPUSET_AND(xc_cpuset, cpuset);
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * send to nobody; just return
5650Sstevel@tonic-gate 	 */
5660Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset)) {
5670Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
5680Sstevel@tonic-gate 		kpreempt_enable();
5690Sstevel@tonic-gate 		return;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 	if (CPU_IN_SET(xc_cpuset, lcx)) {
5730Sstevel@tonic-gate 		/*
5740Sstevel@tonic-gate 		 * same cpu just do it
5750Sstevel@tonic-gate 		 */
5760Sstevel@tonic-gate 		(*func)(arg1, arg2);
5770Sstevel@tonic-gate 		CPUSET_DEL(xc_cpuset, lcx);
5780Sstevel@tonic-gate 		if (CPUSET_ISNULL(xc_cpuset)) {
5790Sstevel@tonic-gate 			XC_STAT_INC(x_dstat[lcx][XC_SOME_SELF]);
5800Sstevel@tonic-gate 			XC_TRACE(XC_SOME_SELF, &tset, func, arg1, arg2);
5810Sstevel@tonic-gate 			XC_SPL_EXIT(lcx, opl);
5820Sstevel@tonic-gate 			kpreempt_enable();
5830Sstevel@tonic-gate 			return;
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate 	}
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate 	if (xc_holder == lcx) {		/* got the xc_sys_mutex already */
5880Sstevel@tonic-gate 		cpuset_t mset = xc_mbox[lcx].xc_cpuset;
5890Sstevel@tonic-gate 
5900Sstevel@tonic-gate 		CPUSET_AND(mset, cpuset);
5910Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&xc_sys_mutex));
5920Sstevel@tonic-gate 		ASSERT(CPUSET_ISEQUAL(mset, cpuset));
5930Sstevel@tonic-gate 		SEND_MBOX_ONLY(xc_cpuset, func, arg1, arg2, lcx, XC_WAIT);
5940Sstevel@tonic-gate 		WAIT_MBOX_DONE(xc_cpuset, lcx, XC_WAIT, 0);
5950Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XC_SOME_OTHER_H]);
5960Sstevel@tonic-gate 		XC_TRACE(XC_SOME_OTHER_H, &xc_cpuset, func, arg1, arg2);
5970Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
5980Sstevel@tonic-gate 		kpreempt_enable();
5990Sstevel@tonic-gate 		return;
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/*
6030Sstevel@tonic-gate 	 * Avoid dead lock if someone has sent us a xc_loop request while
6040Sstevel@tonic-gate 	 * we are trying to grab xc_sys_mutex.
6050Sstevel@tonic-gate 	 */
6060Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	/*
6090Sstevel@tonic-gate 	 * At this point, since we don't own xc_sys_mutex,
6100Sstevel@tonic-gate 	 * our pil shouldn't run at or above the XCALL_PIL.
6110Sstevel@tonic-gate 	 */
6120Sstevel@tonic-gate 	ASSERT(getpil() < XCALL_PIL);
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate 	/*
6150Sstevel@tonic-gate 	 * grab xc_sys_mutex before writing to the xc_mbox
6160Sstevel@tonic-gate 	 */
6170Sstevel@tonic-gate 	mutex_enter(&xc_sys_mutex);
6180Sstevel@tonic-gate 	xc_spl_enter[lcx] = 1;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	XC_TRACE(XC_SOME_OTHER, &xc_cpuset, func, arg1, arg2);
6210Sstevel@tonic-gate 	init_mondo(setsoftint_tl1, xc_serv_inum, 0);
6220Sstevel@tonic-gate 	SEND_MBOX_MONDO(xc_cpuset, func, arg1, arg2, XC_IDLE);
6230Sstevel@tonic-gate 	WAIT_MBOX_DONE(xc_cpuset, lcx, XC_IDLE, 1);
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 	xc_spl_enter[lcx] = 0;
6260Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XC_SOME_OTHER]);
6270Sstevel@tonic-gate 	mutex_exit(&xc_sys_mutex);
6280Sstevel@tonic-gate 	kpreempt_enable();
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * xc_all - send a "x-call" to all cpus
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate void
6350Sstevel@tonic-gate xc_all(xcfunc_t *func, uint64_t arg1, uint64_t arg2)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	int lcx;
6380Sstevel@tonic-gate 	int opl;
6390Sstevel@tonic-gate 	cpuset_t xc_cpuset, tset;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	ASSERT((uintptr_t)func > KERNELBASE);
6420Sstevel@tonic-gate 	ASSERT(((uintptr_t)func % PC_ALIGN) == 0);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	CPUSET_ZERO(tset);
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	kpreempt_disable();
6470Sstevel@tonic-gate 	XC_SPL_ENTER(lcx, opl);			/* lcx set by the macro */
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * same cpu just do it
6530Sstevel@tonic-gate 	 */
6540Sstevel@tonic-gate 	(*func)(arg1, arg2);
6550Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
6560Sstevel@tonic-gate 	CPUSET_DEL(xc_cpuset, lcx);
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset)) {
6590Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XC_ALL_SELF]);
6600Sstevel@tonic-gate 		XC_TRACE(XC_ALL_SELF, &tset, func, arg1, arg2);
6610Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
6620Sstevel@tonic-gate 		kpreempt_enable();
6630Sstevel@tonic-gate 		return;
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	if (xc_holder == lcx) {		/* got the xc_sys_mutex already */
6670Sstevel@tonic-gate 		cpuset_t mset = xc_mbox[lcx].xc_cpuset;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		CPUSET_AND(mset, xc_cpuset);
6700Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&xc_sys_mutex));
6710Sstevel@tonic-gate 		ASSERT(CPUSET_ISEQUAL(mset, xc_cpuset));
6720Sstevel@tonic-gate 		XC_TRACE(XC_ALL_OTHER_H, &xc_cpuset, func, arg1, arg2);
6730Sstevel@tonic-gate 		SEND_MBOX_ONLY(xc_cpuset, func, arg1, arg2, lcx, XC_WAIT);
6740Sstevel@tonic-gate 		WAIT_MBOX_DONE(xc_cpuset, lcx, XC_WAIT, 0);
6750Sstevel@tonic-gate 		XC_STAT_INC(x_dstat[lcx][XC_ALL_OTHER_H]);
6760Sstevel@tonic-gate 		XC_SPL_EXIT(lcx, opl);
6770Sstevel@tonic-gate 		kpreempt_enable();
6780Sstevel@tonic-gate 		return;
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	/*
6820Sstevel@tonic-gate 	 * Avoid dead lock if someone has sent us a xc_loop request while
6830Sstevel@tonic-gate 	 * we are trying to grab xc_sys_mutex.
6840Sstevel@tonic-gate 	 */
6850Sstevel@tonic-gate 	XC_SPL_EXIT(lcx, opl);
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 	/*
6880Sstevel@tonic-gate 	 * At this point, since we don't own xc_sys_mutex,
6890Sstevel@tonic-gate 	 * our pil shouldn't run at or above the XCALL_PIL.
6900Sstevel@tonic-gate 	 */
6910Sstevel@tonic-gate 	ASSERT(getpil() < XCALL_PIL);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	/*
6940Sstevel@tonic-gate 	 * grab xc_sys_mutex before writing to the xc_mbox
6950Sstevel@tonic-gate 	 */
6960Sstevel@tonic-gate 	mutex_enter(&xc_sys_mutex);
6970Sstevel@tonic-gate 	xc_spl_enter[lcx] = 1;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	XC_TRACE(XC_ALL_OTHER, &xc_cpuset, func, arg1, arg2);
7000Sstevel@tonic-gate 	init_mondo(setsoftint_tl1, xc_serv_inum, 0);
7010Sstevel@tonic-gate 	SEND_MBOX_MONDO(xc_cpuset, func, arg1, arg2, XC_IDLE);
7020Sstevel@tonic-gate 	WAIT_MBOX_DONE(xc_cpuset, lcx, XC_IDLE, 1);
7030Sstevel@tonic-gate 
7040Sstevel@tonic-gate 	xc_spl_enter[lcx] = 0;
7050Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XC_ALL_OTHER]);
7060Sstevel@tonic-gate 	mutex_exit(&xc_sys_mutex);
7070Sstevel@tonic-gate 	kpreempt_enable();
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate /*
7110Sstevel@tonic-gate  * xc_attention - paired with xc_dismissed()
7120Sstevel@tonic-gate  *
7130Sstevel@tonic-gate  * xt_attention() holds the xc_sys_mutex and xc_dismissed() releases it
7140Sstevel@tonic-gate  * called when an initiator wants to capture some/all cpus for a critical
7150Sstevel@tonic-gate  * session.
7160Sstevel@tonic-gate  */
7170Sstevel@tonic-gate void
7180Sstevel@tonic-gate xc_attention(cpuset_t cpuset)
7190Sstevel@tonic-gate {
7200Sstevel@tonic-gate 	int pix, lcx;
7210Sstevel@tonic-gate 	cpuset_t xc_cpuset, tmpset;
7220Sstevel@tonic-gate 	cpuset_t recv_cpuset;
7230Sstevel@tonic-gate 	uint64_t loop_cnt = 0;
7240Sstevel@tonic-gate 	int first_time = 1;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	CPUSET_ZERO(recv_cpuset);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * don't migrate the cpu until xc_dismissed() is finished
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	ASSERT(getpil() < XCALL_PIL);
7320Sstevel@tonic-gate 	mutex_enter(&xc_sys_mutex);
7330Sstevel@tonic-gate 	lcx = (int)(CPU->cpu_id);
7340Sstevel@tonic-gate 	ASSERT(x_dstat[lcx][XC_ATTENTION] ==
7350Sstevel@tonic-gate 	    x_dstat[lcx][XC_DISMISSED]);
7360Sstevel@tonic-gate 	ASSERT(xc_holder == -1);
7370Sstevel@tonic-gate 	xc_mbox[lcx].xc_cpuset = cpuset;
7380Sstevel@tonic-gate 	xc_holder = lcx; /* no membar; only current cpu needs the right lcx */
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	/*
7410Sstevel@tonic-gate 	 * only send to the CPU_READY ones
7420Sstevel@tonic-gate 	 */
7430Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
7440Sstevel@tonic-gate 	CPUSET_AND(xc_cpuset, cpuset);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/*
7470Sstevel@tonic-gate 	 * don't send mondo to self
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	CPUSET_DEL(xc_cpuset, lcx);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XC_ATTENTION]);
7520Sstevel@tonic-gate 	XC_TRACE(XC_ATTENTION, &xc_cpuset, NULL, NULL, NULL);
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset))
7550Sstevel@tonic-gate 		return;
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	xc_spl_enter[lcx] = 1;
7580Sstevel@tonic-gate 	/*
7590Sstevel@tonic-gate 	 * inform the target processors to enter into xc_loop()
7600Sstevel@tonic-gate 	 */
7610Sstevel@tonic-gate 	init_mondo(setsoftint_tl1, xc_loop_inum, 0);
7621310Sha137994 	SEND_MBOX_MONDO_XC_ENTER(xc_cpuset);
7630Sstevel@tonic-gate 	xc_spl_enter[lcx] = 0;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * make sure target processors have entered into xc_loop()
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	while (!CPUSET_ISEQUAL(recv_cpuset, xc_cpuset)) {
7690Sstevel@tonic-gate 		tmpset = xc_cpuset;
7700Sstevel@tonic-gate 		for (pix = 0; pix < NCPU; pix++) {
7710Sstevel@tonic-gate 			if (CPU_IN_SET(tmpset, pix)) {
7720Sstevel@tonic-gate 				/*
7730Sstevel@tonic-gate 				 * membar_stld() is done in xc_loop
7740Sstevel@tonic-gate 				 */
7750Sstevel@tonic-gate 				if (xc_mbox[pix].xc_state == XC_WAIT) {
7760Sstevel@tonic-gate 					CPUSET_ADD(recv_cpuset, pix);
7770Sstevel@tonic-gate 				}
7780Sstevel@tonic-gate 				CPUSET_DEL(tmpset, pix);
7790Sstevel@tonic-gate 				if (CPUSET_ISNULL(tmpset)) {
7800Sstevel@tonic-gate 					break;
7810Sstevel@tonic-gate 				}
7820Sstevel@tonic-gate 			}
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		if (loop_cnt++ > xc_mondo_time_limit) {
7850Sstevel@tonic-gate 			if (sendmondo_in_recover) {
7860Sstevel@tonic-gate 				drv_usecwait(1);
7870Sstevel@tonic-gate 				loop_cnt = 0;
7880Sstevel@tonic-gate 				continue;
7890Sstevel@tonic-gate 			}
7900Sstevel@tonic-gate 			if (first_time) {
7910Sstevel@tonic-gate 				XT_SYNC_SOME(xc_cpuset);
7920Sstevel@tonic-gate 				first_time = 0;
7930Sstevel@tonic-gate 				loop_cnt = 0;
7940Sstevel@tonic-gate 				continue;
7950Sstevel@tonic-gate 			}
7960Sstevel@tonic-gate 			cmn_err(CE_PANIC, "xc_attention() timeout");
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 	}
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	/*
8010Sstevel@tonic-gate 	 * xc_sys_mutex remains held until xc_dismissed() is finished
8020Sstevel@tonic-gate 	 */
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * xc_dismissed - paired with xc_attention()
8070Sstevel@tonic-gate  *
8080Sstevel@tonic-gate  * Called after the critical session is finished.
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate void
8110Sstevel@tonic-gate xc_dismissed(cpuset_t cpuset)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	int pix;
8140Sstevel@tonic-gate 	int lcx = (int)(CPU->cpu_id);
8150Sstevel@tonic-gate 	cpuset_t xc_cpuset, tmpset;
8160Sstevel@tonic-gate 	cpuset_t recv_cpuset;
8170Sstevel@tonic-gate 	uint64_t loop_cnt = 0;
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	ASSERT(lcx == xc_holder);
8200Sstevel@tonic-gate 	ASSERT(CPUSET_ISEQUAL(xc_mbox[lcx].xc_cpuset, cpuset));
8210Sstevel@tonic-gate 	ASSERT(getpil() >= XCALL_PIL);
8220Sstevel@tonic-gate 	CPUSET_ZERO(xc_mbox[lcx].xc_cpuset);
8230Sstevel@tonic-gate 	CPUSET_ZERO(recv_cpuset);
8240Sstevel@tonic-gate 	membar_stld();
8250Sstevel@tonic-gate 
8260Sstevel@tonic-gate 	XC_STAT_INC(x_dstat[lcx][XC_DISMISSED]);
8270Sstevel@tonic-gate 	ASSERT(x_dstat[lcx][XC_DISMISSED] == x_dstat[lcx][XC_ATTENTION]);
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 	/*
8300Sstevel@tonic-gate 	 * only send to the CPU_READY ones
8310Sstevel@tonic-gate 	 */
8320Sstevel@tonic-gate 	xc_cpuset = cpu_ready_set;
8330Sstevel@tonic-gate 	CPUSET_AND(xc_cpuset, cpuset);
8340Sstevel@tonic-gate 
8350Sstevel@tonic-gate 	/*
8360Sstevel@tonic-gate 	 * exclude itself
8370Sstevel@tonic-gate 	 */
8380Sstevel@tonic-gate 	CPUSET_DEL(xc_cpuset, lcx);
8390Sstevel@tonic-gate 	XC_TRACE(XC_DISMISSED, &xc_cpuset, NULL, NULL, NULL);
8400Sstevel@tonic-gate 	if (CPUSET_ISNULL(xc_cpuset)) {
8410Sstevel@tonic-gate 		xc_holder = -1;
8420Sstevel@tonic-gate 		mutex_exit(&xc_sys_mutex);
8430Sstevel@tonic-gate 		return;
8440Sstevel@tonic-gate 	}
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 	/*
8470Sstevel@tonic-gate 	 * inform other processors to get out of xc_loop()
8480Sstevel@tonic-gate 	 */
8490Sstevel@tonic-gate 	tmpset = xc_cpuset;
8500Sstevel@tonic-gate 	for (pix = 0; pix < NCPU; pix++) {
8510Sstevel@tonic-gate 		if (CPU_IN_SET(tmpset, pix)) {
8520Sstevel@tonic-gate 			xc_mbox[pix].xc_state = XC_EXIT;
8530Sstevel@tonic-gate 			membar_stld();
8540Sstevel@tonic-gate 			CPUSET_DEL(tmpset, pix);
8550Sstevel@tonic-gate 			if (CPUSET_ISNULL(tmpset)) {
8560Sstevel@tonic-gate 				break;
8570Sstevel@tonic-gate 			}
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 	}
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	/*
8620Sstevel@tonic-gate 	 * make sure target processors have exited from xc_loop()
8630Sstevel@tonic-gate 	 */
8640Sstevel@tonic-gate 	while (!CPUSET_ISEQUAL(recv_cpuset, xc_cpuset)) {
8650Sstevel@tonic-gate 		tmpset = xc_cpuset;
8660Sstevel@tonic-gate 		for (pix = 0; pix < NCPU; pix++) {
8670Sstevel@tonic-gate 			if (CPU_IN_SET(tmpset, pix)) {
8680Sstevel@tonic-gate 				/*
8690Sstevel@tonic-gate 				 * membar_stld() is done in xc_loop
8700Sstevel@tonic-gate 				 */
8710Sstevel@tonic-gate 				if (xc_mbox[pix].xc_state == XC_IDLE) {
8720Sstevel@tonic-gate 					CPUSET_ADD(recv_cpuset, pix);
8730Sstevel@tonic-gate 				}
8740Sstevel@tonic-gate 				CPUSET_DEL(tmpset, pix);
8750Sstevel@tonic-gate 				if (CPUSET_ISNULL(tmpset)) {
8760Sstevel@tonic-gate 					break;
8770Sstevel@tonic-gate 				}
8780Sstevel@tonic-gate 			}
8790Sstevel@tonic-gate 		}
8800Sstevel@tonic-gate 		if (loop_cnt++ > xc_func_time_limit) {
8810Sstevel@tonic-gate 				if (sendmondo_in_recover) {
8820Sstevel@tonic-gate 					drv_usecwait(1);
8830Sstevel@tonic-gate 					loop_cnt = 0;
8840Sstevel@tonic-gate 					continue;
8850Sstevel@tonic-gate 				}
8860Sstevel@tonic-gate 			cmn_err(CE_PANIC, "xc_dismissed() timeout");
8870Sstevel@tonic-gate 		}
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 	xc_holder = -1;
8900Sstevel@tonic-gate 	mutex_exit(&xc_sys_mutex);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate /*
8940Sstevel@tonic-gate  * xc_serv - "x-call" handler at TL=0; serves only one x-call request
8950Sstevel@tonic-gate  * runs at XCALL_PIL level.
8960Sstevel@tonic-gate  */
8970Sstevel@tonic-gate uint_t
8980Sstevel@tonic-gate xc_serv(void)
8990Sstevel@tonic-gate {
9000Sstevel@tonic-gate 	int lcx = (int)(CPU->cpu_id);
9010Sstevel@tonic-gate 	struct xc_mbox *xmp;
9020Sstevel@tonic-gate 	xcfunc_t *func;
9030Sstevel@tonic-gate 	uint64_t arg1, arg2;
9040Sstevel@tonic-gate 	cpuset_t tset;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	ASSERT(getpil() == XCALL_PIL);
9070Sstevel@tonic-gate 	CPUSET_ZERO(tset);
9080Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
9090Sstevel@tonic-gate 	flush_windows();
9100Sstevel@tonic-gate 	xmp = &xc_mbox[lcx];
9110Sstevel@tonic-gate 	ASSERT(lcx != xc_holder);
9120Sstevel@tonic-gate 	ASSERT(xmp->xc_state == XC_DOIT);
9130Sstevel@tonic-gate 	func = xmp->xc_func;
9140Sstevel@tonic-gate 	XC_TRACE(XC_SERV, &tset, func, xmp->xc_arg1, xmp->xc_arg2);
9150Sstevel@tonic-gate 	if (func != NULL) {
9160Sstevel@tonic-gate 		arg1 = xmp->xc_arg1;
9170Sstevel@tonic-gate 		arg2 = xmp->xc_arg2;
9180Sstevel@tonic-gate 		(*func)(arg1, arg2);
9190Sstevel@tonic-gate 	}
9200Sstevel@tonic-gate 	XC_STAT_INC(x_rstat[lcx][XC_SERV]);
9210Sstevel@tonic-gate 	XC_TRACE(XC_SERV, &tset, func, arg1, arg2);
9220Sstevel@tonic-gate 	xmp->xc_state = XC_IDLE;
9230Sstevel@tonic-gate 	membar_stld();
9240Sstevel@tonic-gate 	return (1);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate  * if == 1, an xc_loop timeout will cause a panic
9290Sstevel@tonic-gate  * otherwise print a warning
9300Sstevel@tonic-gate  */
9310Sstevel@tonic-gate uint_t xc_loop_panic = 0;
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate /*
9340Sstevel@tonic-gate  * xc_loop - "x-call" handler at TL=0; capture the cpu for a critial
9350Sstevel@tonic-gate  * session, or serve multiple x-call requests runs at XCALL_PIL level.
9360Sstevel@tonic-gate  */
9370Sstevel@tonic-gate uint_t
9380Sstevel@tonic-gate xc_loop(void)
9390Sstevel@tonic-gate {
9400Sstevel@tonic-gate 	int lcx = (int)(CPU->cpu_id);
9410Sstevel@tonic-gate 	struct xc_mbox *xmp;
9420Sstevel@tonic-gate 	xcfunc_t *func;
9430Sstevel@tonic-gate 	uint64_t arg1, arg2;
9440Sstevel@tonic-gate 	uint64_t loop_cnt = 0;
9450Sstevel@tonic-gate 	cpuset_t tset;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	ASSERT(getpil() == XCALL_PIL);
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	CPUSET_ZERO(tset);
9500Sstevel@tonic-gate 	flush_windows();
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	/*
9530Sstevel@tonic-gate 	 * Some one must have owned the xc_sys_mutex;
9540Sstevel@tonic-gate 	 * no further interrupt (at XCALL_PIL or below) can
9550Sstevel@tonic-gate 	 * be taken by this processor until xc_loop exits.
9560Sstevel@tonic-gate 	 *
9570Sstevel@tonic-gate 	 * The owner of xc_sys_mutex (or xc_holder) can expect
9580Sstevel@tonic-gate 	 * its xc/xt requests are handled as follows:
9590Sstevel@tonic-gate 	 * 	xc requests use xc_mbox's handshaking for their services
9600Sstevel@tonic-gate 	 * 	xt requests at TL>0 will be handled immediately
9610Sstevel@tonic-gate 	 * 	xt requests at TL=0:
9620Sstevel@tonic-gate 	 *		if their handlers'pils are <= XCALL_PIL, then
9630Sstevel@tonic-gate 	 *			they will be handled after xc_loop exits
9640Sstevel@tonic-gate 	 *			(so, they probably should not be used)
9650Sstevel@tonic-gate 	 *		else they will be handled immediately
9660Sstevel@tonic-gate 	 *
9670Sstevel@tonic-gate 	 * For those who are not informed to enter xc_loop, if they
9680Sstevel@tonic-gate 	 * send xc/xt requests to this processor at this moment,
9690Sstevel@tonic-gate 	 * the requests will be handled as follows:
9700Sstevel@tonic-gate 	 *	xc requests will be handled after they grab xc_sys_mutex
9710Sstevel@tonic-gate 	 *	xt requests at TL>0 will be handled immediately
9720Sstevel@tonic-gate 	 * 	xt requests at TL=0:
9730Sstevel@tonic-gate 	 *		if their handlers'pils are <= XCALL_PIL, then
9740Sstevel@tonic-gate 	 *			they will be handled after xc_loop exits
9750Sstevel@tonic-gate 	 *		else they will be handled immediately
9760Sstevel@tonic-gate 	 */
9770Sstevel@tonic-gate 	xmp = &xc_mbox[lcx];
9780Sstevel@tonic-gate 	ASSERT(lcx != xc_holder);
9790Sstevel@tonic-gate 	ASSERT(xmp->xc_state == XC_ENTER);
9800Sstevel@tonic-gate 	xmp->xc_state = XC_WAIT;
9810Sstevel@tonic-gate 	CPUSET_ADD(tset, lcx);
9820Sstevel@tonic-gate 	membar_stld();
9830Sstevel@tonic-gate 	XC_STAT_INC(x_rstat[lcx][XC_LOOP]);
9840Sstevel@tonic-gate 	XC_TRACE(XC_LOOP_ENTER, &tset, NULL, NULL, NULL);
9850Sstevel@tonic-gate 	while (xmp->xc_state != XC_EXIT) {
9860Sstevel@tonic-gate 		if (xmp->xc_state == XC_DOIT) {
9870Sstevel@tonic-gate 			func = xmp->xc_func;
9880Sstevel@tonic-gate 			arg1 = xmp->xc_arg1;
9890Sstevel@tonic-gate 			arg2 = xmp->xc_arg2;
9900Sstevel@tonic-gate 			XC_TRACE(XC_LOOP_DOIT, &tset, func, arg1, arg2);
9910Sstevel@tonic-gate 			if (func != NULL)
9920Sstevel@tonic-gate 				(*func)(arg1, arg2);
9930Sstevel@tonic-gate 			xmp->xc_state = XC_WAIT;
9940Sstevel@tonic-gate 			membar_stld();
9950Sstevel@tonic-gate 			/*
9960Sstevel@tonic-gate 			 * reset the timeout counter
9970Sstevel@tonic-gate 			 * since some work was done
9980Sstevel@tonic-gate 			 */
9990Sstevel@tonic-gate 			loop_cnt = 0;
10000Sstevel@tonic-gate 		} else {
10010Sstevel@tonic-gate 			/* patience is a virtue... */
10020Sstevel@tonic-gate 			loop_cnt++;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 		if (loop_cnt > xc_func_time_limit) {
10060Sstevel@tonic-gate 			if (sendmondo_in_recover) {
10070Sstevel@tonic-gate 				drv_usecwait(1);
10080Sstevel@tonic-gate 				loop_cnt = 0;
10090Sstevel@tonic-gate 				continue;
10100Sstevel@tonic-gate 			}
10110Sstevel@tonic-gate 			cmn_err(xc_loop_panic ? CE_PANIC : CE_WARN,
10120Sstevel@tonic-gate 			    "xc_loop() timeout");
10130Sstevel@tonic-gate 			/*
10140Sstevel@tonic-gate 			 * if the above displayed a warning,
10150Sstevel@tonic-gate 			 * reset the timeout counter and be patient
10160Sstevel@tonic-gate 			 */
10170Sstevel@tonic-gate 			loop_cnt = 0;
10180Sstevel@tonic-gate 		}
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	ASSERT(xmp->xc_state == XC_EXIT);
10210Sstevel@tonic-gate 	ASSERT(xc_holder != -1);
10220Sstevel@tonic-gate 	XC_TRACE(XC_LOOP_EXIT, &tset, NULL, NULL, NULL);
10230Sstevel@tonic-gate 	xmp->xc_state = XC_IDLE;
10240Sstevel@tonic-gate 	membar_stld();
10250Sstevel@tonic-gate 	return (1);
10260Sstevel@tonic-gate }
1027