xref: /onnv-gate/usr/src/uts/i86pc/os/x_call.c (revision 2006:11a559c797d5)
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
5*2006Sandrei  * Common Development and Distribution License (the "License").
6*2006Sandrei  * 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 /*
221251Skchow  * Copyright 2006 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 /*
290Sstevel@tonic-gate  * Facilities for cross-processor subroutine calls using "mailbox" interrupts.
300Sstevel@tonic-gate  *
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include <sys/param.h>
360Sstevel@tonic-gate #include <sys/t_lock.h>
370Sstevel@tonic-gate #include <sys/thread.h>
380Sstevel@tonic-gate #include <sys/cpuvar.h>
390Sstevel@tonic-gate #include <sys/x_call.h>
400Sstevel@tonic-gate #include <sys/cpu.h>
410Sstevel@tonic-gate #include <sys/psw.h>
420Sstevel@tonic-gate #include <sys/sunddi.h>
430Sstevel@tonic-gate #include <sys/mmu.h>
440Sstevel@tonic-gate #include <sys/debug.h>
450Sstevel@tonic-gate #include <sys/systm.h>
460Sstevel@tonic-gate #include <sys/machsystm.h>
470Sstevel@tonic-gate #include <sys/mutex_impl.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static struct	xc_mbox xc_mboxes[X_CALL_LEVELS];
500Sstevel@tonic-gate static kmutex_t xc_mbox_lock[X_CALL_LEVELS];
510Sstevel@tonic-gate static uint_t 	xc_xlat_xcptoipl[X_CALL_LEVELS] = {
520Sstevel@tonic-gate 	XC_LO_PIL,
530Sstevel@tonic-gate 	XC_MED_PIL,
540Sstevel@tonic-gate 	XC_HI_PIL
550Sstevel@tonic-gate };
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static void xc_common(xc_func_t, xc_arg_t, xc_arg_t, xc_arg_t,
580Sstevel@tonic-gate     int, cpuset_t, int);
590Sstevel@tonic-gate 
600Sstevel@tonic-gate static int	xc_initialized = 0;
61*2006Sandrei extern cpuset_t	cpu_ready_set;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate void
640Sstevel@tonic-gate xc_init()
650Sstevel@tonic-gate {
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * By making these mutexes type MUTEX_DRIVER, the ones below
680Sstevel@tonic-gate 	 * LOCK_LEVEL will be implemented as adaptive mutexes, and the
690Sstevel@tonic-gate 	 * ones above LOCK_LEVEL will be spin mutexes.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	mutex_init(&xc_mbox_lock[0], NULL, MUTEX_DRIVER,
720Sstevel@tonic-gate 	    (void *)ipltospl(XC_LO_PIL));
730Sstevel@tonic-gate 	mutex_init(&xc_mbox_lock[1], NULL, MUTEX_DRIVER,
740Sstevel@tonic-gate 	    (void *)ipltospl(XC_MED_PIL));
750Sstevel@tonic-gate 	mutex_init(&xc_mbox_lock[2], NULL, MUTEX_DRIVER,
760Sstevel@tonic-gate 	    (void *)ipltospl(XC_HI_PIL));
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	xc_initialized = 1;
790Sstevel@tonic-gate }
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Used by the debugger to determine whether or not cross calls have been
830Sstevel@tonic-gate  * initialized and are safe to use.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate int
860Sstevel@tonic-gate kdi_xc_initialized(void)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	return (xc_initialized);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
91*2006Sandrei #define	CAPTURE_CPU_ARG	~0UL
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * X-call interrupt service routine.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * arg == X_CALL_MEDPRI	-  capture cpus.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  * We're protected against changing CPUs by being a high-priority interrupt.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate /*ARGSUSED*/
1010Sstevel@tonic-gate uint_t
1020Sstevel@tonic-gate xc_serv(caddr_t arg1, caddr_t arg2)
1030Sstevel@tonic-gate {
1040Sstevel@tonic-gate 	int	op;
1050Sstevel@tonic-gate 	int	pri = (int)(uintptr_t)arg1;
1060Sstevel@tonic-gate 	struct cpu *cpup = CPU;
1070Sstevel@tonic-gate 	xc_arg_t *argp;
1080Sstevel@tonic-gate 	xc_arg_t arg2val;
1090Sstevel@tonic-gate 	uint_t	tlbflush;
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (pri == X_CALL_MEDPRI) {
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 		argp = &xc_mboxes[X_CALL_MEDPRI].arg2;
1140Sstevel@tonic-gate 		arg2val = *argp;
1150Sstevel@tonic-gate 		if (arg2val != CAPTURE_CPU_ARG &&
116*2006Sandrei 		    !CPU_IN_SET((cpuset_t)arg2val, cpup->cpu_id))
1170Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
1180Sstevel@tonic-gate 		ASSERT(arg2val == CAPTURE_CPU_ARG);
1190Sstevel@tonic-gate 		if (cpup->cpu_m.xc_pend[pri] == 0)
1200Sstevel@tonic-gate 			return (DDI_INTR_UNCLAIMED);
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 		cpup->cpu_m.xc_pend[X_CALL_MEDPRI] = 0;
1230Sstevel@tonic-gate 		cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 1;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		for (;;) {
1260Sstevel@tonic-gate 			if ((cpup->cpu_m.xc_state[X_CALL_MEDPRI] == XC_DONE) ||
1270Sstevel@tonic-gate 				(cpup->cpu_m.xc_pend[X_CALL_MEDPRI]))
1280Sstevel@tonic-gate 				break;
1290Sstevel@tonic-gate 			ht_pause();
1300Sstevel@tonic-gate 		}
1310Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 	if (cpup->cpu_m.xc_pend[pri] == 0)
1340Sstevel@tonic-gate 		return (DDI_INTR_UNCLAIMED);
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	cpup->cpu_m.xc_pend[pri] = 0;
1370Sstevel@tonic-gate 	op = cpup->cpu_m.xc_state[pri];
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	/*
1400Sstevel@tonic-gate 	 * When invalidating TLB entries, wait until the initiator changes the
1410Sstevel@tonic-gate 	 * memory PTE before doing any INVLPG. Otherwise, if the PTE in memory
1420Sstevel@tonic-gate 	 * hasn't been changed, the processor's TLB Flush filter may ignore
1430Sstevel@tonic-gate 	 * the INVLPG instruction.
1440Sstevel@tonic-gate 	 */
1450Sstevel@tonic-gate 	tlbflush = (cpup->cpu_m.xc_wait[pri] == 2);
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/*
1480Sstevel@tonic-gate 	 * Don't invoke a null function.
1490Sstevel@tonic-gate 	 */
1500Sstevel@tonic-gate 	if (xc_mboxes[pri].func != NULL) {
1510Sstevel@tonic-gate 		if (!tlbflush)
1520Sstevel@tonic-gate 			cpup->cpu_m.xc_retval[pri] = (*xc_mboxes[pri].func)
1530Sstevel@tonic-gate 			    (xc_mboxes[pri].arg1, xc_mboxes[pri].arg2,
1540Sstevel@tonic-gate 				xc_mboxes[pri].arg3);
1550Sstevel@tonic-gate 	} else
1560Sstevel@tonic-gate 		cpup->cpu_m.xc_retval[pri] = 0;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Acknowledge that we have completed the x-call operation.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	cpup->cpu_m.xc_ack[pri] = 1;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (op == XC_CALL_OP)
1640Sstevel@tonic-gate 		return (DDI_INTR_CLAIMED);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	/*
1670Sstevel@tonic-gate 	 * for (op == XC_SYNC_OP)
1680Sstevel@tonic-gate 	 * Wait for the initiator of the x-call to indicate
1690Sstevel@tonic-gate 	 * that all CPUs involved can proceed.
1700Sstevel@tonic-gate 	 */
171355Ssherrym 	while (cpup->cpu_m.xc_wait[pri])
1720Sstevel@tonic-gate 		ht_pause();
1730Sstevel@tonic-gate 
174355Ssherrym 	while (cpup->cpu_m.xc_state[pri] != XC_DONE)
1750Sstevel@tonic-gate 		ht_pause();
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	/*
1780Sstevel@tonic-gate 	 * Flush the TLB, if that's what is requested.
1790Sstevel@tonic-gate 	 */
1800Sstevel@tonic-gate 	if (xc_mboxes[pri].func != NULL && tlbflush) {
1810Sstevel@tonic-gate 		cpup->cpu_m.xc_retval[pri] = (*xc_mboxes[pri].func)
1820Sstevel@tonic-gate 		    (xc_mboxes[pri].arg1, xc_mboxes[pri].arg2,
1830Sstevel@tonic-gate 			xc_mboxes[pri].arg3);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * Acknowledge that we have received the directive to continue.
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	ASSERT(cpup->cpu_m.xc_ack[pri] == 0);
1900Sstevel@tonic-gate 	cpup->cpu_m.xc_ack[pri] = 1;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	return (DDI_INTR_CLAIMED);
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * xc_do_call:
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate static void
2000Sstevel@tonic-gate xc_do_call(
2010Sstevel@tonic-gate 	xc_arg_t arg1,
2020Sstevel@tonic-gate 	xc_arg_t arg2,
2030Sstevel@tonic-gate 	xc_arg_t arg3,
2040Sstevel@tonic-gate 	int pri,
2050Sstevel@tonic-gate 	cpuset_t set,
2060Sstevel@tonic-gate 	xc_func_t func,
2070Sstevel@tonic-gate 	int sync)
2080Sstevel@tonic-gate {
2090Sstevel@tonic-gate 	/*
2100Sstevel@tonic-gate 	 * If the pri indicates a low priority lock (below LOCK_LEVEL),
2110Sstevel@tonic-gate 	 * we must disable preemption to avoid migrating to another CPU
2120Sstevel@tonic-gate 	 * during the call.
2130Sstevel@tonic-gate 	 */
2140Sstevel@tonic-gate 	if (pri == X_CALL_LOPRI) {
2150Sstevel@tonic-gate 		kpreempt_disable();
2160Sstevel@tonic-gate 	} else {
2170Sstevel@tonic-gate 		pri = X_CALL_HIPRI;
2180Sstevel@tonic-gate 	}
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* always grab highest mutex to avoid deadlock */
2210Sstevel@tonic-gate 	mutex_enter(&xc_mbox_lock[X_CALL_HIPRI]);
2220Sstevel@tonic-gate 	xc_common(func, arg1, arg2, arg3, pri, set, sync);
2230Sstevel@tonic-gate 	mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
2240Sstevel@tonic-gate 	if (pri == X_CALL_LOPRI)
2250Sstevel@tonic-gate 		kpreempt_enable();
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /*
2300Sstevel@tonic-gate  * xc_call: call specified function on all processors
2310Sstevel@tonic-gate  * remotes may continue after service
2320Sstevel@tonic-gate  * we wait here until everybody has completed.
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate void
2350Sstevel@tonic-gate xc_call(
2360Sstevel@tonic-gate 	xc_arg_t arg1,
2370Sstevel@tonic-gate 	xc_arg_t arg2,
2380Sstevel@tonic-gate 	xc_arg_t arg3,
2390Sstevel@tonic-gate 	int pri,
2400Sstevel@tonic-gate 	cpuset_t set,
2410Sstevel@tonic-gate 	xc_func_t func)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate 	xc_do_call(arg1, arg2, arg3, pri, set, func, 0);
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /*
2470Sstevel@tonic-gate  * xc_sync: call specified function on all processors
2480Sstevel@tonic-gate  * after doing work, each remote waits until we let
2490Sstevel@tonic-gate  * it continue; send the contiunue after everyone has
2500Sstevel@tonic-gate  * informed us that they are done.
2510Sstevel@tonic-gate  */
2520Sstevel@tonic-gate void
2530Sstevel@tonic-gate xc_sync(
2540Sstevel@tonic-gate 	xc_arg_t arg1,
2550Sstevel@tonic-gate 	xc_arg_t arg2,
2560Sstevel@tonic-gate 	xc_arg_t arg3,
2570Sstevel@tonic-gate 	int pri,
2580Sstevel@tonic-gate 	cpuset_t set,
2590Sstevel@tonic-gate 	xc_func_t func)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	xc_do_call(arg1, arg2, arg3, pri, set, func, 1);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /*
2650Sstevel@tonic-gate  * xc_sync_wait: similar to xc_sync(), except that the starting
2660Sstevel@tonic-gate  * cpu waits for all other cpus to check in before running its
2670Sstevel@tonic-gate  * service locally.
2680Sstevel@tonic-gate  */
2690Sstevel@tonic-gate void
2700Sstevel@tonic-gate xc_wait_sync(
2710Sstevel@tonic-gate 	xc_arg_t arg1,
2720Sstevel@tonic-gate 	xc_arg_t arg2,
2730Sstevel@tonic-gate 	xc_arg_t arg3,
2740Sstevel@tonic-gate 	int pri,
2750Sstevel@tonic-gate 	cpuset_t set,
2760Sstevel@tonic-gate 	xc_func_t func)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate 	xc_do_call(arg1, arg2, arg3, pri, set, func, 2);
2790Sstevel@tonic-gate }
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate /*
2830Sstevel@tonic-gate  * The routines xc_capture_cpus and xc_release_cpus
2840Sstevel@tonic-gate  * can be used in place of xc_sync in order to implement a critical
2850Sstevel@tonic-gate  * code section where all CPUs in the system can be controlled.
2860Sstevel@tonic-gate  * xc_capture_cpus is used to start the critical code section, and
2870Sstevel@tonic-gate  * xc_release_cpus is used to end the critical code section.
2880Sstevel@tonic-gate  */
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate  * Capture the CPUs specified in order to start a x-call session,
2920Sstevel@tonic-gate  * and/or to begin a critical section.
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate void
2950Sstevel@tonic-gate xc_capture_cpus(cpuset_t set)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	int cix;
2980Sstevel@tonic-gate 	int lcx;
2990Sstevel@tonic-gate 	struct cpu *cpup;
3000Sstevel@tonic-gate 	int	i;
3010Sstevel@tonic-gate 	cpuset_t *cpus;
3020Sstevel@tonic-gate 	cpuset_t c;
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Prevent deadlocks where we take an interrupt and are waiting
3080Sstevel@tonic-gate 	 * for a mutex owned by one of the CPUs that is captured for
3090Sstevel@tonic-gate 	 * the x-call, while that CPU is waiting for some x-call signal
3100Sstevel@tonic-gate 	 * to be set by us.
3110Sstevel@tonic-gate 	 *
3120Sstevel@tonic-gate 	 * This mutex also prevents preemption, since it raises SPL above
3130Sstevel@tonic-gate 	 * LOCK_LEVEL (it is a spin-type driver mutex).
3140Sstevel@tonic-gate 	 */
3150Sstevel@tonic-gate 	/* always grab highest mutex to avoid deadlock */
3160Sstevel@tonic-gate 	mutex_enter(&xc_mbox_lock[X_CALL_HIPRI]);
3170Sstevel@tonic-gate 	lcx = CPU->cpu_id;	/* now we're safe */
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 	ASSERT(CPU->cpu_flags & CPU_READY);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	/*
3220Sstevel@tonic-gate 	 * Wait for all cpus
3230Sstevel@tonic-gate 	 */
3240Sstevel@tonic-gate 	cpus = (cpuset_t *)&xc_mboxes[X_CALL_MEDPRI].arg2;
3250Sstevel@tonic-gate 	if (CPU_IN_SET(*cpus, CPU->cpu_id))
3260Sstevel@tonic-gate 		CPUSET_ATOMIC_DEL(*cpus, CPU->cpu_id);
3270Sstevel@tonic-gate 	for (;;) {
3280Sstevel@tonic-gate 		c = *(volatile cpuset_t *)cpus;
3290Sstevel@tonic-gate 		CPUSET_AND(c, cpu_ready_set);
3300Sstevel@tonic-gate 		if (CPUSET_ISNULL(c))
3310Sstevel@tonic-gate 			break;
3320Sstevel@tonic-gate 		ht_pause();
3330Sstevel@tonic-gate 	}
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Store the set of CPUs involved in the x-call session, so that
3370Sstevel@tonic-gate 	 * xc_release_cpus will know what CPUs to act upon.
3380Sstevel@tonic-gate 	 */
3390Sstevel@tonic-gate 	xc_mboxes[X_CALL_MEDPRI].set = set;
3400Sstevel@tonic-gate 	xc_mboxes[X_CALL_MEDPRI].arg2 = CAPTURE_CPU_ARG;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	/*
3430Sstevel@tonic-gate 	 * Now capture each CPU in the set and cause it to go into a
3440Sstevel@tonic-gate 	 * holding pattern.
3450Sstevel@tonic-gate 	 */
3460Sstevel@tonic-gate 	i = 0;
3470Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
3480Sstevel@tonic-gate 		if ((cpup = cpu[cix]) == NULL ||
3490Sstevel@tonic-gate 		    (cpup->cpu_flags & CPU_READY) == 0) {
3500Sstevel@tonic-gate 			/*
3510Sstevel@tonic-gate 			 * In case CPU wasn't ready, but becomes ready later,
3520Sstevel@tonic-gate 			 * take the CPU out of the set now.
3530Sstevel@tonic-gate 			 */
3540Sstevel@tonic-gate 			CPUSET_DEL(set, cix);
3550Sstevel@tonic-gate 			continue;
3560Sstevel@tonic-gate 		}
3570Sstevel@tonic-gate 		if (cix != lcx && CPU_IN_SET(set, cix)) {
3580Sstevel@tonic-gate 			cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 0;
3590Sstevel@tonic-gate 			cpup->cpu_m.xc_state[X_CALL_MEDPRI] = XC_HOLD;
3600Sstevel@tonic-gate 			cpup->cpu_m.xc_pend[X_CALL_MEDPRI] = 1;
3610Sstevel@tonic-gate 			send_dirint(cix, XC_MED_PIL);
3620Sstevel@tonic-gate 		}
3630Sstevel@tonic-gate 		i++;
3640Sstevel@tonic-gate 		if (i >= ncpus)
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 	}
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * Wait here until all remote calls to complete.
3700Sstevel@tonic-gate 	 */
3710Sstevel@tonic-gate 	i = 0;
3720Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
3730Sstevel@tonic-gate 		if (lcx != cix && CPU_IN_SET(set, cix)) {
3740Sstevel@tonic-gate 			cpup = cpu[cix];
375355Ssherrym 			while (cpup->cpu_m.xc_ack[X_CALL_MEDPRI] == 0)
3760Sstevel@tonic-gate 				ht_pause();
3770Sstevel@tonic-gate 			cpup->cpu_m.xc_ack[X_CALL_MEDPRI] = 0;
3780Sstevel@tonic-gate 		}
3790Sstevel@tonic-gate 		i++;
3800Sstevel@tonic-gate 		if (i >= ncpus)
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate /*
3870Sstevel@tonic-gate  * Release the CPUs captured by xc_capture_cpus, thus terminating the
3880Sstevel@tonic-gate  * x-call session and exiting the critical section.
3890Sstevel@tonic-gate  */
3900Sstevel@tonic-gate void
3910Sstevel@tonic-gate xc_release_cpus(void)
3920Sstevel@tonic-gate {
3930Sstevel@tonic-gate 	int cix;
3940Sstevel@tonic-gate 	int lcx = (int)(CPU->cpu_id);
3950Sstevel@tonic-gate 	cpuset_t set = xc_mboxes[X_CALL_MEDPRI].set;
3960Sstevel@tonic-gate 	struct cpu *cpup;
3970Sstevel@tonic-gate 	int	i;
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&xc_mbox_lock[X_CALL_HIPRI]));
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	/*
4020Sstevel@tonic-gate 	 * Allow each CPU to exit its holding pattern.
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	i = 0;
4050Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
4060Sstevel@tonic-gate 		if ((cpup = cpu[cix]) == NULL)
4070Sstevel@tonic-gate 			continue;
4080Sstevel@tonic-gate 		if ((cpup->cpu_flags & CPU_READY) &&
4090Sstevel@tonic-gate 		    (cix != lcx) && CPU_IN_SET(set, cix)) {
4100Sstevel@tonic-gate 			/*
4110Sstevel@tonic-gate 			 * Clear xc_ack since we will be waiting for it
4120Sstevel@tonic-gate 			 * to be set again after we set XC_DONE.
4130Sstevel@tonic-gate 			 */
4140Sstevel@tonic-gate 			cpup->cpu_m.xc_state[X_CALL_MEDPRI] = XC_DONE;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 		i++;
4170Sstevel@tonic-gate 		if (i >= ncpus)
4180Sstevel@tonic-gate 			break;
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	xc_mboxes[X_CALL_MEDPRI].arg2 = 0;
4220Sstevel@tonic-gate 	mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate /*
4260Sstevel@tonic-gate  * Common code to call a specified function on a set of processors.
4270Sstevel@tonic-gate  * sync specifies what kind of waiting is done.
4280Sstevel@tonic-gate  *	-1 - no waiting, don't release remotes
4290Sstevel@tonic-gate  *	0 - no waiting, release remotes immediately
4300Sstevel@tonic-gate  *	1 - run service locally w/o waiting for remotes.
4310Sstevel@tonic-gate  *	2 - wait for remotes before running locally
4320Sstevel@tonic-gate  */
4330Sstevel@tonic-gate static void
4340Sstevel@tonic-gate xc_common(
4350Sstevel@tonic-gate 	xc_func_t func,
4360Sstevel@tonic-gate 	xc_arg_t arg1,
4370Sstevel@tonic-gate 	xc_arg_t arg2,
4380Sstevel@tonic-gate 	xc_arg_t arg3,
4390Sstevel@tonic-gate 	int pri,
4400Sstevel@tonic-gate 	cpuset_t set,
4410Sstevel@tonic-gate 	int sync)
4420Sstevel@tonic-gate {
4430Sstevel@tonic-gate 	int cix;
4440Sstevel@tonic-gate 	int lcx = (int)(CPU->cpu_id);
4450Sstevel@tonic-gate 	struct cpu *cpup;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	ASSERT(panicstr == NULL);
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&xc_mbox_lock[X_CALL_HIPRI]));
4500Sstevel@tonic-gate 	ASSERT(CPU->cpu_flags & CPU_READY);
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	/*
4530Sstevel@tonic-gate 	 * Set up the service definition mailbox.
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	xc_mboxes[pri].func = func;
4560Sstevel@tonic-gate 	xc_mboxes[pri].arg1 = arg1;
4570Sstevel@tonic-gate 	xc_mboxes[pri].arg2 = arg2;
4580Sstevel@tonic-gate 	xc_mboxes[pri].arg3 = arg3;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * Request service on all remote processors.
4620Sstevel@tonic-gate 	 */
4630Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
4640Sstevel@tonic-gate 		if ((cpup = cpu[cix]) == NULL ||
4650Sstevel@tonic-gate 		    (cpup->cpu_flags & CPU_READY) == 0) {
4660Sstevel@tonic-gate 			/*
4671251Skchow 			 * In case the non-local CPU is not ready but becomes
4681251Skchow 			 * ready later, take it out of the set now. The local
4691251Skchow 			 * CPU needs to remain in the set to complete the
4701251Skchow 			 * requested function.
4710Sstevel@tonic-gate 			 */
4721251Skchow 			if (cix != lcx)
4731251Skchow 				CPUSET_DEL(set, cix);
4740Sstevel@tonic-gate 		} else if (cix != lcx && CPU_IN_SET(set, cix)) {
4750Sstevel@tonic-gate 			CPU_STATS_ADDQ(CPU, sys, xcalls, 1);
4760Sstevel@tonic-gate 			cpup->cpu_m.xc_ack[pri] = 0;
4770Sstevel@tonic-gate 			cpup->cpu_m.xc_wait[pri] = sync;
4780Sstevel@tonic-gate 			if (sync > 0)
4790Sstevel@tonic-gate 				cpup->cpu_m.xc_state[pri] = XC_SYNC_OP;
4800Sstevel@tonic-gate 			else
4810Sstevel@tonic-gate 				cpup->cpu_m.xc_state[pri] = XC_CALL_OP;
4820Sstevel@tonic-gate 			cpup->cpu_m.xc_pend[pri] = 1;
4830Sstevel@tonic-gate 			send_dirint(cix, xc_xlat_xcptoipl[pri]);
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * Run service locally if not waiting for remotes.
4890Sstevel@tonic-gate 	 */
4900Sstevel@tonic-gate 	if (sync != 2 && CPU_IN_SET(set, lcx) && func != NULL)
4910Sstevel@tonic-gate 		CPU->cpu_m.xc_retval[pri] = (*func)(arg1, arg2, arg3);
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (sync == -1)
4940Sstevel@tonic-gate 		return;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	/*
4970Sstevel@tonic-gate 	 * Wait here until all remote calls complete.
4980Sstevel@tonic-gate 	 */
4990Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
5000Sstevel@tonic-gate 		if (lcx != cix && CPU_IN_SET(set, cix)) {
5010Sstevel@tonic-gate 			cpup = cpu[cix];
502355Ssherrym 			while (cpup->cpu_m.xc_ack[pri] == 0)
5030Sstevel@tonic-gate 				ht_pause();
5040Sstevel@tonic-gate 			cpup->cpu_m.xc_ack[pri] = 0;
5050Sstevel@tonic-gate 		}
5060Sstevel@tonic-gate 	}
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	/*
5090Sstevel@tonic-gate 	 * Run service locally if waiting for remotes.
5100Sstevel@tonic-gate 	 */
5110Sstevel@tonic-gate 	if (sync == 2 && CPU_IN_SET(set, lcx) && func != NULL)
5120Sstevel@tonic-gate 		CPU->cpu_m.xc_retval[pri] = (*func)(arg1, arg2, arg3);
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (sync == 0)
5150Sstevel@tonic-gate 		return;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Release any waiting CPUs
5190Sstevel@tonic-gate 	 */
5200Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
5210Sstevel@tonic-gate 		if (lcx != cix && CPU_IN_SET(set, cix)) {
5220Sstevel@tonic-gate 			cpup = cpu[cix];
5230Sstevel@tonic-gate 			if (cpup != NULL && (cpup->cpu_flags & CPU_READY)) {
5240Sstevel@tonic-gate 				cpup->cpu_m.xc_wait[pri] = 0;
5250Sstevel@tonic-gate 				cpup->cpu_m.xc_state[pri] = XC_DONE;
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 	/*
5310Sstevel@tonic-gate 	 * Wait for all CPUs to acknowledge completion before we continue.
5320Sstevel@tonic-gate 	 * Without this check it's possible (on a VM or hyper-threaded CPUs
5330Sstevel@tonic-gate 	 * or in the presence of Service Management Interrupts which can all
5340Sstevel@tonic-gate 	 * cause delays) for the remote processor to still be waiting by
5350Sstevel@tonic-gate 	 * the time xc_common() is next invoked with the sync flag set
5360Sstevel@tonic-gate 	 * resulting in a deadlock.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	for (cix = 0; cix < NCPU; cix++) {
5390Sstevel@tonic-gate 		if (lcx != cix && CPU_IN_SET(set, cix)) {
5400Sstevel@tonic-gate 			cpup = cpu[cix];
5410Sstevel@tonic-gate 			if (cpup != NULL && (cpup->cpu_flags & CPU_READY)) {
542355Ssherrym 				while (cpup->cpu_m.xc_ack[pri] == 0)
5430Sstevel@tonic-gate 					ht_pause();
5440Sstevel@tonic-gate 				cpup->cpu_m.xc_ack[pri] = 0;
5450Sstevel@tonic-gate 			}
5460Sstevel@tonic-gate 		}
5470Sstevel@tonic-gate 	}
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate  * xc_trycall: attempt to call specified function on all processors
5520Sstevel@tonic-gate  * remotes may wait for a long time
5530Sstevel@tonic-gate  * we continue immediately
5540Sstevel@tonic-gate  */
5550Sstevel@tonic-gate void
5560Sstevel@tonic-gate xc_trycall(
5570Sstevel@tonic-gate 	xc_arg_t arg1,
5580Sstevel@tonic-gate 	xc_arg_t arg2,
5590Sstevel@tonic-gate 	xc_arg_t arg3,
5600Sstevel@tonic-gate 	cpuset_t set,
5610Sstevel@tonic-gate 	xc_func_t func)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	int		save_kernel_preemption;
5640Sstevel@tonic-gate 	extern int	IGNORE_KERNEL_PREEMPTION;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	/*
5670Sstevel@tonic-gate 	 * If we can grab the mutex, we'll do the cross-call.  If not -- if
5680Sstevel@tonic-gate 	 * someone else is already doing a cross-call -- we won't.
5690Sstevel@tonic-gate 	 */
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	save_kernel_preemption = IGNORE_KERNEL_PREEMPTION;
5720Sstevel@tonic-gate 	IGNORE_KERNEL_PREEMPTION = 1;
5730Sstevel@tonic-gate 	if (mutex_tryenter(&xc_mbox_lock[X_CALL_HIPRI])) {
5740Sstevel@tonic-gate 		xc_common(func, arg1, arg2, arg3, X_CALL_HIPRI, set, -1);
5750Sstevel@tonic-gate 		mutex_exit(&xc_mbox_lock[X_CALL_HIPRI]);
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 	IGNORE_KERNEL_PREEMPTION = save_kernel_preemption;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate 
5800Sstevel@tonic-gate /*
5810Sstevel@tonic-gate  * Used by the debugger to cross-call the other CPUs, thus causing them to
5820Sstevel@tonic-gate  * enter the debugger.  We can't hold locks, so we spin on the cross-call
5830Sstevel@tonic-gate  * lock until we get it.  When we get it, we send the cross-call, and assume
5840Sstevel@tonic-gate  * that we successfully stopped the other CPUs.
5850Sstevel@tonic-gate  */
5860Sstevel@tonic-gate void
5870Sstevel@tonic-gate kdi_xc_others(int this_cpu, void (*func)(void))
5880Sstevel@tonic-gate {
5890Sstevel@tonic-gate 	extern int	IGNORE_KERNEL_PREEMPTION;
5900Sstevel@tonic-gate 	int save_kernel_preemption;
5910Sstevel@tonic-gate 	mutex_impl_t *lp;
5920Sstevel@tonic-gate 	cpuset_t set;
5930Sstevel@tonic-gate 	int x;
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	CPUSET_ALL_BUT(set, this_cpu);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	save_kernel_preemption = IGNORE_KERNEL_PREEMPTION;
5980Sstevel@tonic-gate 	IGNORE_KERNEL_PREEMPTION = 1;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	lp = (mutex_impl_t *)&xc_mbox_lock[X_CALL_HIPRI];
6010Sstevel@tonic-gate 	for (x = 0; x < 0x400000; x++) {
6020Sstevel@tonic-gate 		if (lock_spin_try(&lp->m_spin.m_spinlock)) {
6030Sstevel@tonic-gate 			xc_common((xc_func_t)func, 0, 0, 0, X_CALL_HIPRI,
6040Sstevel@tonic-gate 			    set, -1);
6050Sstevel@tonic-gate 			lp->m_spin.m_spinlock = 0; /* XXX */
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 		}
6080Sstevel@tonic-gate 		(void) xc_serv((caddr_t)X_CALL_MEDPRI, NULL);
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 	IGNORE_KERNEL_PREEMPTION = save_kernel_preemption;
6110Sstevel@tonic-gate }
612