xref: /titanic_50/usr/src/uts/common/os/softint.c (revision f1fa5dcf799749330b7bd6cdee34de6a17ea3fd4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a1af7ba0Scwb  * Common Development and Distribution License (the "License").
6a1af7ba0Scwb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22a1af7ba0Scwb  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/param.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/spl.h>
337c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
347c478bd9Sstevel@tonic-gate #include <sys/debug.h>
357c478bd9Sstevel@tonic-gate #include <sys/kdi_impl.h>
363aedfe0bSmishra #include <sys/cpuvar.h>
373aedfe0bSmishra #include <sys/cpuvar.h>
383aedfe0bSmishra #include <sys/archsystm.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Handle software interrupts through 'softcall' mechanism
42f8047eabSsudheer  *
43f8047eabSsudheer  * At present softcall mechanism uses a global list headed by softhead.
44f8047eabSsudheer  * Entries are added to tail and removed from head so as to preserve FIFO
45f8047eabSsudheer  * nature of entries in the softcall list. softcall() takes care of adding
46f8047eabSsudheer  * entries to the softtail.
47f8047eabSsudheer  *
48f8047eabSsudheer  * softint must take care of executing the entries in the FIFO
49f8047eabSsudheer  * order. It could be called simultaneously from multiple cpus, however only
503aedfe0bSmishra  * one instance of softint should process the softcall list with the exception
513aedfe0bSmishra  * when CPU is stuck due to high interrupt load and can't execute callbacks.
523aedfe0bSmishra  * State diagram is as follows :-
533aedfe0bSmishra  *
543aedfe0bSmishra  *	- Upper half which is same as old state machine
55f8047eabSsudheer  *	  (IDLE->PEND->DRAIN->IDLE)
56f8047eabSsudheer  *
573aedfe0bSmishra  *	- Lower half which steals the entries from softcall queue and execute
583aedfe0bSmishra  *        in the context of softint interrupt handler. The interrupt handler
593aedfe0bSmishra  *        is fired on a different CPU by sending a cross-call.
603aedfe0bSmishra  *
613aedfe0bSmishra  * Starting state is IDLE.
623aedfe0bSmishra  *
633aedfe0bSmishra  * 				softint()
643aedfe0bSmishra  *
653aedfe0bSmishra  *
663aedfe0bSmishra  *				(c)
673aedfe0bSmishra  * 	____________________________________________________
683aedfe0bSmishra  * 	|                          ^                         ^
693aedfe0bSmishra  * 	v            (a)           |           (b)           |
703aedfe0bSmishra  * 	IDLE--------------------->PEND--------------------->DRAIN
713aedfe0bSmishra  *	^                         |                         |
723aedfe0bSmishra  * 	|                         |                         |
733aedfe0bSmishra  * 	|                         |                         |
743aedfe0bSmishra  * 	|                         |                         |
753aedfe0bSmishra  * 	|                         |                         |
763aedfe0bSmishra  * 	|                         d                         d
773aedfe0bSmishra  * 	|                         |                         |
783aedfe0bSmishra  * 	|                         v                         v
793aedfe0bSmishra  * 	|                         PEND                      DRAIN
803aedfe0bSmishra  * 	|            (e)           &                          &
813aedfe0bSmishra  * 	|<-----------------------STEAL                      STEAL
823aedfe0bSmishra  * 	^                                                    |
833aedfe0bSmishra  * 	|                                                    |
843aedfe0bSmishra  * 	|                         (e)                        v
853aedfe0bSmishra  * 	|_________________________<__________________________|
863aedfe0bSmishra  *
873aedfe0bSmishra  *
883aedfe0bSmishra  *
893aedfe0bSmishra  * Edge (a)->(b)->(c) are same as old state machine and these
903aedfe0bSmishra  * are mutually exclusive state.
913aedfe0bSmishra  *
923aedfe0bSmishra  * a - When an entry is being enqueued to softcall queue then the state
933aedfe0bSmishra  *     moves from IDLE to PEND.
943aedfe0bSmishra  *
953aedfe0bSmishra  * b - When interrupt handler has started processing softcall queue.
963aedfe0bSmishra  *
973aedfe0bSmishra  * c - When interrupt handler finished processing softcall queue, the
983aedfe0bSmishra  *     state of machines goes back to IDLE.
993aedfe0bSmishra  *
1003aedfe0bSmishra  * d - softcall() generates another softlevel1 iff interrupt handler
1013aedfe0bSmishra  *     hasn't run recently.
1023aedfe0bSmishra  *
1033aedfe0bSmishra  * e - Either PEND|STEAL or DRAIN|STEAL is set. We let softlevel1
1043aedfe0bSmishra  *     handler exit because we have processed all the entries.
1053aedfe0bSmishra  *
1063aedfe0bSmishra  * When CPU is being pinned by higher level interrupts for more than
1073aedfe0bSmishra  * softcall_delay clock ticks, SOFT_STEAL is OR'ed so that softlevel1
1083aedfe0bSmishra  * handler on the other CPU can drain the queue.
1093aedfe0bSmishra  *
110f8047eabSsudheer  * These states are needed for softcall mechanism since Solaris has only
1113aedfe0bSmishra  * one interface (ie. siron ) as of now for :
1123aedfe0bSmishra  *
113f8047eabSsudheer  * - raising a soft interrupt architecture independently (ie not through
114f8047eabSsudheer  *   setsoftint(..) )
115f8047eabSsudheer  * - to process the softcall queue.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate #define	NSOFTCALLS	200
1193aedfe0bSmishra 
120f8047eabSsudheer /*
121f8047eabSsudheer  * Defined states for softcall processing.
122f8047eabSsudheer  */
123f8047eabSsudheer #define	SOFT_IDLE		0x01	/* no processing is needed */
124f8047eabSsudheer #define	SOFT_PEND		0x02	/* softcall list needs processing */
1253aedfe0bSmishra #define	SOFT_DRAIN		0x04	/* list is being processed */
1263aedfe0bSmishra #define	SOFT_STEAL		0x08	/* list is being stolen for draining */
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate typedef struct softcall {
1297c478bd9Sstevel@tonic-gate 	void (*sc_func)(void *);	/* function to call */
1307c478bd9Sstevel@tonic-gate 	void *sc_arg;			/* arg to pass to func */
1317c478bd9Sstevel@tonic-gate 	struct softcall *sc_next;	/* next in list */
1327c478bd9Sstevel@tonic-gate } softcall_t;
1337c478bd9Sstevel@tonic-gate 
1343aedfe0bSmishra /*
1353aedfe0bSmishra  * softcall list and state variables.
1363aedfe0bSmishra  */
1373aedfe0bSmishra static softcall_t *softcalls;
1383aedfe0bSmishra static softcall_t *softhead, *softtail, *softfree;
139f8047eabSsudheer static uint_t	softcall_state;
1403aedfe0bSmishra static clock_t softcall_tick;
1413aedfe0bSmishra 
1423aedfe0bSmishra /*
1433aedfe0bSmishra  * This ensures that softcall entries don't get stuck for long. It's expressed
1443aedfe0bSmishra  * in 10 milliseconds as 1 unit. When hires_tick is set or other clock frequency
1453aedfe0bSmishra  * is used, softcall_init() ensures that it's still expressed as 1 =  10 milli
1463aedfe0bSmishra  * seconds.
1473aedfe0bSmishra  */
1483aedfe0bSmishra static int softcall_delay = 1;
1493aedfe0bSmishra 
1503aedfe0bSmishra /*
1513aedfe0bSmishra  * The last CPU which will drain softcall queue.
1523aedfe0bSmishra  */
1533aedfe0bSmishra static int softcall_latest_cpuid = -1;
1543aedfe0bSmishra 
1553aedfe0bSmishra /*
1563aedfe0bSmishra  * CPUSET to hold the CPU which is processing softcall queue
1573aedfe0bSmishra  * currently. There can be more than one CPU having bit set
1583aedfe0bSmishra  * but it will happen only when they are stuck.
1593aedfe0bSmishra  */
1603aedfe0bSmishra static cpuset_t *softcall_cpuset = NULL;
1617c478bd9Sstevel@tonic-gate 
162f8047eabSsudheer /*
163f8047eabSsudheer  * protects softcall lists and control variable softcall_state.
164f8047eabSsudheer  */
165f8047eabSsudheer static kmutex_t	softcall_lock;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static void (*kdi_softcall_func)(void);
1683aedfe0bSmishra extern void siron_poke_cpu(cpuset_t);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate extern void siron(void);
171*f1fa5dcfSmishra extern void kdi_siron(void);
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate void
1747c478bd9Sstevel@tonic-gate softcall_init(void)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 	softcall_t *sc;
1777c478bd9Sstevel@tonic-gate 
1783aedfe0bSmishra 	softcalls = kmem_zalloc(sizeof (softcall_t) * NSOFTCALLS, KM_SLEEP);
1793aedfe0bSmishra 	softcall_cpuset = kmem_zalloc(sizeof (cpuset_t), KM_SLEEP);
1807c478bd9Sstevel@tonic-gate 	for (sc = softcalls; sc < &softcalls[NSOFTCALLS]; sc++) {
1817c478bd9Sstevel@tonic-gate 		sc->sc_next = softfree;
1827c478bd9Sstevel@tonic-gate 		softfree = sc;
1837c478bd9Sstevel@tonic-gate 	}
1843aedfe0bSmishra 	mutex_init(&softcall_lock, NULL, MUTEX_SPIN,
1853aedfe0bSmishra 	    (void *)ipltospl(SPL8));
1863aedfe0bSmishra 	softcall_state = SOFT_IDLE;
1873aedfe0bSmishra 	softcall_tick = lbolt;
1883aedfe0bSmishra 
1893aedfe0bSmishra 	if (softcall_delay < 0)
1903aedfe0bSmishra 		softcall_delay = 1;
1913aedfe0bSmishra 
1923aedfe0bSmishra 	/*
1933aedfe0bSmishra 	 * Since softcall_delay is expressed as 1 = 10 milliseconds.
1943aedfe0bSmishra 	 */
1953aedfe0bSmishra 	softcall_delay = softcall_delay * (hz/100);
1963aedfe0bSmishra 	CPUSET_ZERO(*softcall_cpuset);
1973aedfe0bSmishra }
1983aedfe0bSmishra 
1993aedfe0bSmishra /*
2003aedfe0bSmishra  * Gets called when softcall queue is not moving forward. We choose
2013aedfe0bSmishra  * a CPU and poke except the ones which are already poked.
2023aedfe0bSmishra  */
2033aedfe0bSmishra static int
2043aedfe0bSmishra softcall_choose_cpu()
2053aedfe0bSmishra {
2063aedfe0bSmishra 	cpu_t *cplist = CPU;
2073aedfe0bSmishra 	cpu_t *cp;
2083aedfe0bSmishra 	int intr_load = INT_MAX;
2093aedfe0bSmishra 	int cpuid = -1;
2103aedfe0bSmishra 	cpuset_t poke;
2113aedfe0bSmishra 	int s;
2123aedfe0bSmishra 
2133aedfe0bSmishra 	ASSERT(getpil() >= DISP_LEVEL);
2143aedfe0bSmishra 	ASSERT(ncpus > 1);
2153aedfe0bSmishra 	ASSERT(MUTEX_HELD(&softcall_lock));
2163aedfe0bSmishra 
2173aedfe0bSmishra 	CPUSET_ZERO(poke);
2183aedfe0bSmishra 
2193aedfe0bSmishra 	/*
2203aedfe0bSmishra 	 * The hint is to start from current CPU.
2213aedfe0bSmishra 	 */
2223aedfe0bSmishra 	cp = cplist;
2233aedfe0bSmishra 	do {
224*f1fa5dcfSmishra 		/*
225*f1fa5dcfSmishra 		 * Don't select this CPU if :
226*f1fa5dcfSmishra 		 *   - in cpuset already
227*f1fa5dcfSmishra 		 *   - CPU is not accepting interrupts
228*f1fa5dcfSmishra 		 *   - CPU is being offlined
229*f1fa5dcfSmishra 		 */
2303aedfe0bSmishra 		if (CPU_IN_SET(*softcall_cpuset, cp->cpu_id) ||
231*f1fa5dcfSmishra 		    (cp->cpu_flags & CPU_ENABLE) == 0 ||
232*f1fa5dcfSmishra 		    (cp == cpu_inmotion))
2333aedfe0bSmishra 			continue;
2343aedfe0bSmishra 
2353aedfe0bSmishra 		/* if CPU is not busy */
2363aedfe0bSmishra 		if (cp->cpu_intrload == 0) {
2373aedfe0bSmishra 			cpuid = cp->cpu_id;
2383aedfe0bSmishra 			break;
2393aedfe0bSmishra 		}
2403aedfe0bSmishra 
2413aedfe0bSmishra 		if (cp->cpu_intrload < intr_load) {
2423aedfe0bSmishra 			cpuid = cp->cpu_id;
2433aedfe0bSmishra 			intr_load = cp->cpu_intrload;
2443aedfe0bSmishra 		} else if (cp->cpu_intrload == intr_load) {
2453aedfe0bSmishra 			/*
2463aedfe0bSmishra 			 * We want to poke CPUs having similar
2473aedfe0bSmishra 			 * load because we don't know which CPU is
2483aedfe0bSmishra 			 * can acknowledge level1 interrupt. The
2493aedfe0bSmishra 			 * list of such CPUs should not be large.
2503aedfe0bSmishra 			 */
2513aedfe0bSmishra 			if (cpuid != -1) {
2523aedfe0bSmishra 				/*
2533aedfe0bSmishra 				 * Put the last CPU chosen because
2543aedfe0bSmishra 				 * it also has same interrupt load.
2553aedfe0bSmishra 				 */
2563aedfe0bSmishra 				CPUSET_ADD(poke, cpuid);
2573aedfe0bSmishra 				cpuid = -1;
2583aedfe0bSmishra 			}
2593aedfe0bSmishra 
2603aedfe0bSmishra 			CPUSET_ADD(poke, cp->cpu_id);
2613aedfe0bSmishra 		}
2623aedfe0bSmishra 	} while ((cp = cp->cpu_next_onln) != cplist);
2633aedfe0bSmishra 
2643aedfe0bSmishra 	/* if we found a CPU which suits best to poke */
2653aedfe0bSmishra 	if (cpuid != -1) {
2663aedfe0bSmishra 		CPUSET_ZERO(poke);
2673aedfe0bSmishra 		CPUSET_ADD(poke, cpuid);
2683aedfe0bSmishra 	}
2693aedfe0bSmishra 
2703aedfe0bSmishra 	if (CPUSET_ISNULL(poke)) {
2713aedfe0bSmishra 		mutex_exit(&softcall_lock);
2723aedfe0bSmishra 		return (0);
2733aedfe0bSmishra 	}
2743aedfe0bSmishra 
2753aedfe0bSmishra 	/*
2763aedfe0bSmishra 	 * We first set the bit in cpuset and then poke.
2773aedfe0bSmishra 	 */
2783aedfe0bSmishra 	CPUSET_XOR(*softcall_cpuset, poke);
2793aedfe0bSmishra 	mutex_exit(&softcall_lock);
2803aedfe0bSmishra 
2813aedfe0bSmishra 	/*
2823aedfe0bSmishra 	 * If softcall() was called at low pil then we may
2833aedfe0bSmishra 	 * get preempted before we raise PIL. It should be okay
2843aedfe0bSmishra 	 * because we are just going to poke CPUs now or at most
2853aedfe0bSmishra 	 * another thread may start choosing CPUs in this routine.
2863aedfe0bSmishra 	 */
2873aedfe0bSmishra 	s = splhigh();
2883aedfe0bSmishra 	siron_poke_cpu(poke);
2893aedfe0bSmishra 	splx(s);
2903aedfe0bSmishra 	return (1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate  * Call function func with argument arg
2957c478bd9Sstevel@tonic-gate  * at some later time at software interrupt priority
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate void
2987c478bd9Sstevel@tonic-gate softcall(void (*func)(void *), void *arg)
2997c478bd9Sstevel@tonic-gate {
3007c478bd9Sstevel@tonic-gate 	softcall_t *sc;
3013aedfe0bSmishra 	clock_t w;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	/*
3047c478bd9Sstevel@tonic-gate 	 * protect against cross-calls
3057c478bd9Sstevel@tonic-gate 	 */
3067c478bd9Sstevel@tonic-gate 	mutex_enter(&softcall_lock);
3077c478bd9Sstevel@tonic-gate 	/* coalesce identical softcalls */
3087c478bd9Sstevel@tonic-gate 	for (sc = softhead; sc != 0; sc = sc->sc_next) {
3097c478bd9Sstevel@tonic-gate 		if (sc->sc_func == func && sc->sc_arg == arg) {
3103aedfe0bSmishra 			goto intr;
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if ((sc = softfree) == 0)
3157c478bd9Sstevel@tonic-gate 		panic("too many softcalls");
3163aedfe0bSmishra 
3177c478bd9Sstevel@tonic-gate 	softfree = sc->sc_next;
3187c478bd9Sstevel@tonic-gate 	sc->sc_func = func;
3197c478bd9Sstevel@tonic-gate 	sc->sc_arg = arg;
3207c478bd9Sstevel@tonic-gate 	sc->sc_next = 0;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	if (softhead) {
3237c478bd9Sstevel@tonic-gate 		softtail->sc_next = sc;
3247c478bd9Sstevel@tonic-gate 		softtail = sc;
3253aedfe0bSmishra 	} else
3267c478bd9Sstevel@tonic-gate 		softhead = softtail = sc;
3273aedfe0bSmishra 
3283aedfe0bSmishra intr:
3293aedfe0bSmishra 	if (softcall_state & SOFT_IDLE) {
330f8047eabSsudheer 		softcall_state = SOFT_PEND;
3313aedfe0bSmishra 		softcall_tick = lbolt;
3327c478bd9Sstevel@tonic-gate 		mutex_exit(&softcall_lock);
3337c478bd9Sstevel@tonic-gate 		siron();
3343aedfe0bSmishra 	} else if (softcall_state & (SOFT_DRAIN|SOFT_PEND)) {
3353aedfe0bSmishra 		w = lbolt - softcall_tick;
3363aedfe0bSmishra 		if (w <= softcall_delay || ncpus == 1) {
3373aedfe0bSmishra 			mutex_exit(&softcall_lock);
3383aedfe0bSmishra 			return;
3397c478bd9Sstevel@tonic-gate 		}
3403aedfe0bSmishra 
3413aedfe0bSmishra 		if (!(softcall_state & SOFT_STEAL)) {
3423aedfe0bSmishra 			softcall_state |= SOFT_STEAL;
3433aedfe0bSmishra 
3443aedfe0bSmishra 			/*
3453aedfe0bSmishra 			 * We want to give some more chance before
3463aedfe0bSmishra 			 * fishing around again.
3473aedfe0bSmishra 			 */
3483aedfe0bSmishra 			softcall_tick = lbolt;
3493aedfe0bSmishra 		}
3503aedfe0bSmishra 
3513aedfe0bSmishra 		/* softcall_lock will be released by this routine */
3523aedfe0bSmishra 		(void) softcall_choose_cpu();
3537c478bd9Sstevel@tonic-gate 	}
354f8047eabSsudheer }
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate void
3577c478bd9Sstevel@tonic-gate kdi_softcall(void (*func)(void))
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate 	kdi_softcall_func = func;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (softhead == NULL)
362*f1fa5dcfSmishra 		kdi_siron();
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*
366f8047eabSsudheer  * Called to process software interrupts take one off queue, call it,
367f8047eabSsudheer  * repeat.
368f8047eabSsudheer  *
3693aedfe0bSmishra  * Note queue may change during call; softcall_lock, state variables
3703aedfe0bSmishra  * softcall_state and softcall_latest_cpuid ensures that -
371f8047eabSsudheer  * - we don't have multiple cpus pulling from the list (thus causing
3723aedfe0bSmishra  *   a violation of FIFO order with an exception when we are stuck).
373f8047eabSsudheer  * - we don't miss a new entry having been added to the head.
374f8047eabSsudheer  * - we don't miss a wakeup.
3757c478bd9Sstevel@tonic-gate  */
376f8047eabSsudheer 
3777c478bd9Sstevel@tonic-gate void
3787c478bd9Sstevel@tonic-gate softint(void)
3797c478bd9Sstevel@tonic-gate {
3803aedfe0bSmishra 	softcall_t *sc = NULL;
3817c478bd9Sstevel@tonic-gate 	void (*func)();
3827c478bd9Sstevel@tonic-gate 	caddr_t arg;
3833aedfe0bSmishra 	int cpu_id = CPU->cpu_id;
3847c478bd9Sstevel@tonic-gate 
385*f1fa5dcfSmishra 	/*
386*f1fa5dcfSmishra 	 * Don't process softcall queue if current CPU is quiesced or
387*f1fa5dcfSmishra 	 * offlined. This can happen when a CPU is running pause
388*f1fa5dcfSmishra 	 * thread but softcall already sent a xcall.
389*f1fa5dcfSmishra 	 */
390*f1fa5dcfSmishra 	if (CPU->cpu_flags & (CPU_QUIESCED|CPU_OFFLINE)) {
391*f1fa5dcfSmishra 		if (softcall_cpuset != NULL &&
392*f1fa5dcfSmishra 		    CPU_IN_SET(*softcall_cpuset, cpu_id)) {
393*f1fa5dcfSmishra 			CPUSET_DEL(*softcall_cpuset, cpu_id);
394*f1fa5dcfSmishra 			goto out;
395*f1fa5dcfSmishra 		}
396*f1fa5dcfSmishra 	}
397*f1fa5dcfSmishra 
3987c478bd9Sstevel@tonic-gate 	mutex_enter(&softcall_lock);
3993aedfe0bSmishra 
4003aedfe0bSmishra 	if (softcall_state & (SOFT_STEAL|SOFT_PEND)) {
4013aedfe0bSmishra 		softcall_state = SOFT_DRAIN;
4023aedfe0bSmishra 	} else  {
4033aedfe0bSmishra 		/*
4043aedfe0bSmishra 		 * The check for softcall_cpuset being
4053aedfe0bSmishra 		 * NULL is required because it may get
4063aedfe0bSmishra 		 * called very early during boot.
4073aedfe0bSmishra 		 */
4083aedfe0bSmishra 		if (softcall_cpuset != NULL &&
4093aedfe0bSmishra 		    CPU_IN_SET(*softcall_cpuset, cpu_id))
4103aedfe0bSmishra 			CPUSET_DEL(*softcall_cpuset, cpu_id);
411f8047eabSsudheer 		mutex_exit(&softcall_lock);
412f8047eabSsudheer 		goto out;
413f8047eabSsudheer 	}
4143aedfe0bSmishra 
4153aedfe0bSmishra 	/*
4163aedfe0bSmishra 	 * Setting softcall_latest_cpuid to current CPU ensures
4173aedfe0bSmishra 	 * that there is only one active softlevel1 handler to
4183aedfe0bSmishra 	 * process softcall queues.
4193aedfe0bSmishra 	 *
4203aedfe0bSmishra 	 * Since softcall_lock lock is dropped before calling
4213aedfe0bSmishra 	 * func (callback), we need softcall_latest_cpuid
4223aedfe0bSmishra 	 * to prevent two softlevel1 hanlders working on the
4233aedfe0bSmishra 	 * queue when the first softlevel1 handler gets
4243aedfe0bSmishra 	 * stuck due to high interrupt load.
4253aedfe0bSmishra 	 */
4263aedfe0bSmishra 	softcall_latest_cpuid = cpu_id;
4273aedfe0bSmishra 
4283aedfe0bSmishra 	/* add ourself to the cpuset */
4293aedfe0bSmishra 	if (!CPU_IN_SET(*softcall_cpuset, cpu_id))
4303aedfe0bSmishra 		CPUSET_ADD(*softcall_cpuset, cpu_id);
431f8047eabSsudheer 
432f8047eabSsudheer 	for (;;) {
4333aedfe0bSmishra 		softcall_tick = lbolt;
4347c478bd9Sstevel@tonic-gate 		if ((sc = softhead) != NULL) {
4357c478bd9Sstevel@tonic-gate 			func = sc->sc_func;
4367c478bd9Sstevel@tonic-gate 			arg = sc->sc_arg;
4377c478bd9Sstevel@tonic-gate 			softhead = sc->sc_next;
4387c478bd9Sstevel@tonic-gate 			sc->sc_next = softfree;
4397c478bd9Sstevel@tonic-gate 			softfree = sc;
4407c478bd9Sstevel@tonic-gate 		}
4413aedfe0bSmishra 
442f8047eabSsudheer 		if (sc == NULL) {
4433aedfe0bSmishra 			if (CPU_IN_SET(*softcall_cpuset, cpu_id))
4443aedfe0bSmishra 				CPUSET_DEL(*softcall_cpuset, cpu_id);
4453aedfe0bSmishra 
446f8047eabSsudheer 			softcall_state = SOFT_IDLE;
4473aedfe0bSmishra 			ASSERT(softcall_latest_cpuid == cpu_id);
4483aedfe0bSmishra 			softcall_latest_cpuid = -1;
4493aedfe0bSmishra 
4507c478bd9Sstevel@tonic-gate 			mutex_exit(&softcall_lock);
4517c478bd9Sstevel@tonic-gate 			break;
4527c478bd9Sstevel@tonic-gate 		}
4533aedfe0bSmishra 
454f8047eabSsudheer 		mutex_exit(&softcall_lock);
455f8047eabSsudheer 		func(arg);
456f8047eabSsudheer 		mutex_enter(&softcall_lock);
4573aedfe0bSmishra 
4583aedfe0bSmishra 		/*
4593aedfe0bSmishra 		 * No longer need softcall processing from current
4603aedfe0bSmishra 		 * interrupt handler because either
4613aedfe0bSmishra 		 *  (a) softcall is in SOFT_IDLE state or
4623aedfe0bSmishra 		 *  (b) There is a CPU already draining softcall
4633aedfe0bSmishra 		 *	queue and the current softlevel1 is no
4643aedfe0bSmishra 		 *	longer required.
4653aedfe0bSmishra 		 */
4663aedfe0bSmishra 		if (softcall_latest_cpuid != cpu_id) {
4673aedfe0bSmishra 			if (CPU_IN_SET(*softcall_cpuset, cpu_id))
4683aedfe0bSmishra 				CPUSET_DEL(*softcall_cpuset, cpu_id);
4693aedfe0bSmishra 
4703aedfe0bSmishra 			mutex_exit(&softcall_lock);
4713aedfe0bSmishra 			break;
472f8047eabSsudheer 		}
4733aedfe0bSmishra 	}
4743aedfe0bSmishra 
475f8047eabSsudheer out:
4767c478bd9Sstevel@tonic-gate 	if ((func = kdi_softcall_func) != NULL) {
4777c478bd9Sstevel@tonic-gate 		kdi_softcall_func = NULL;
4787c478bd9Sstevel@tonic-gate 		func();
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate }
481