xref: /onnv-gate/usr/src/uts/common/io/avintr.c (revision 5107:bb9efa2ee1e8)
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
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * 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 /*
223446Smrj  * Copyright 2007 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  * Autovectored Interrupt Configuration and Deconfiguration
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/cmn_err.h>
340Sstevel@tonic-gate #include <sys/trap.h>
350Sstevel@tonic-gate #include <sys/t_lock.h>
360Sstevel@tonic-gate #include <sys/avintr.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/machlock.h>
390Sstevel@tonic-gate #include <sys/systm.h>
400Sstevel@tonic-gate #include <sys/machsystm.h>
410Sstevel@tonic-gate #include <sys/sunddi.h>
420Sstevel@tonic-gate #include <sys/x_call.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/atomic.h>
450Sstevel@tonic-gate #include <sys/smp_impldefs.h>
460Sstevel@tonic-gate #include <sys/sdt.h>
470Sstevel@tonic-gate #include <sys/stack.h>
480Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
495084Sjohnlev #ifdef __xpv
505084Sjohnlev #include <sys/evtchn_impl.h>
515084Sjohnlev #endif
520Sstevel@tonic-gate 
53999Slq150181 typedef struct av_softinfo {
54999Slq150181 	cpuset_t	av_pending;	/* pending bitmasks */
55999Slq150181 } av_softinfo_t;
56999Slq150181 
57999Slq150181 static void insert_av(void *intr_id, struct av_head *vectp, avfunc f,
58916Sschwartz 	caddr_t arg1, caddr_t arg2, uint64_t *ticksp, int pri_level,
59916Sschwartz 	dev_info_t *dip);
600Sstevel@tonic-gate static void remove_av(void *intr_id, struct av_head *vectp, avfunc f,
610Sstevel@tonic-gate 	int pri_level, int vect);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate  * Arrange for a driver to be called when a particular
650Sstevel@tonic-gate  * auto-vectored interrupt occurs.
660Sstevel@tonic-gate  * NOTE: if a device can generate interrupts on more than
670Sstevel@tonic-gate  * one level, or if a driver services devices that interrupt
680Sstevel@tonic-gate  * on more than one level, then the driver should install
690Sstevel@tonic-gate  * itself on each of those levels.
700Sstevel@tonic-gate  */
710Sstevel@tonic-gate static char badsoft[] =
720Sstevel@tonic-gate 	"add_avintr: bad soft interrupt level %d for driver '%s'\n";
730Sstevel@tonic-gate static char multilevel[] =
740Sstevel@tonic-gate 	"!IRQ%d is being shared by drivers with different interrupt levels.\n"
750Sstevel@tonic-gate 	"This may result in reduced system performance.";
760Sstevel@tonic-gate static char multilevel2[] =
770Sstevel@tonic-gate 	"Cannot register interrupt for '%s' device at IPL %d because it\n"
780Sstevel@tonic-gate 	"conflicts with another device using the same vector %d with an IPL\n"
790Sstevel@tonic-gate 	"of %d. Reconfigure the conflicting devices to use different vectors.";
800Sstevel@tonic-gate 
815084Sjohnlev #ifdef __xpv
825084Sjohnlev #define	MAX_VECT	NR_IRQS
835084Sjohnlev #else
840Sstevel@tonic-gate #define	MAX_VECT	256
855084Sjohnlev #endif
865084Sjohnlev 
870Sstevel@tonic-gate struct autovec *nmivect = NULL;
880Sstevel@tonic-gate struct av_head autovect[MAX_VECT];
890Sstevel@tonic-gate struct av_head softvect[LOCK_LEVEL + 1];
900Sstevel@tonic-gate kmutex_t av_lock;
91*5107Seota /*
92*5107Seota  * These are software interrupt handlers dedicated to ddi timer.
93*5107Seota  * The interrupt levels up to 10 are supported, but high interrupts
94*5107Seota  * must not be used there.
95*5107Seota  */
96*5107Seota ddi_softint_hdl_impl_t softlevel_hdl[DDI_IPL_10] = {
97*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 1 */
98*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 2 */
99*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 3 */
100*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 4 */
101*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 5 */
102*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 6 */
103*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 7 */
104*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 8 */
105*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 9 */
106*5107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 10 */
107*5107Seota };
1080Sstevel@tonic-gate ddi_softint_hdl_impl_t softlevel1_hdl =
109999Slq150181 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
110999Slq150181 
111999Slq150181 /*
112999Slq150181  * clear/check softint pending flag corresponding for
113999Slq150181  * the current CPU
114999Slq150181  */
1150Sstevel@tonic-gate void
116999Slq150181 av_clear_softint_pending(av_softinfo_t *infop)
117999Slq150181 {
118999Slq150181 	CPUSET_ATOMIC_DEL(infop->av_pending, CPU->cpu_seqid);
119999Slq150181 }
120999Slq150181 
121999Slq150181 boolean_t
122999Slq150181 av_check_softint_pending(av_softinfo_t *infop, boolean_t check_all)
1230Sstevel@tonic-gate {
124999Slq150181 	if (check_all)
125999Slq150181 		return (!CPUSET_ISNULL(infop->av_pending));
126999Slq150181 	else
127999Slq150181 		return (CPU_IN_SET(infop->av_pending, CPU->cpu_seqid) != 0);
128999Slq150181 }
129999Slq150181 
130999Slq150181 /*
1314652Scwb  * This is the wrapper function which is generally used to set a softint
1324652Scwb  * pending
1334652Scwb  */
1344652Scwb void
1354652Scwb av_set_softint_pending(int pri, av_softinfo_t *infop)
1364652Scwb {
1374652Scwb 	kdi_av_set_softint_pending(pri, infop);
1384652Scwb }
1394652Scwb 
1404652Scwb /*
1414652Scwb  * This is kmdb's private entry point to setsoftint called from kdi_siron
142999Slq150181  * It first sets our av softint pending bit for the current CPU,
143999Slq150181  * then it sets the CPU softint pending bit for pri.
144999Slq150181  */
145999Slq150181 void
1464652Scwb kdi_av_set_softint_pending(int pri, av_softinfo_t *infop)
147999Slq150181 {
148999Slq150181 	CPUSET_ATOMIC_ADD(infop->av_pending, CPU->cpu_seqid);
149999Slq150181 
1500Sstevel@tonic-gate 	atomic_or_32((uint32_t *)&CPU->cpu_softinfo.st_pending, 1 << pri);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * register nmi interrupt routine. The first arg is used only to order
1550Sstevel@tonic-gate  * various nmi interrupt service routines in the chain. Higher lvls will
1560Sstevel@tonic-gate  * be called first
1570Sstevel@tonic-gate  */
1580Sstevel@tonic-gate int
1590Sstevel@tonic-gate add_nmintr(int lvl, avfunc nmintr, char *name, caddr_t arg)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	struct autovec  *mem;
1620Sstevel@tonic-gate 	struct autovec *p, *prev = NULL;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	if (nmintr == NULL) {
1650Sstevel@tonic-gate 		printf("Attempt to add null vect for %s on nmi\n", name);
1660Sstevel@tonic-gate 		return (0);
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP);
1710Sstevel@tonic-gate 	mem->av_vector = nmintr;
1720Sstevel@tonic-gate 	mem->av_intarg1 = arg;
1730Sstevel@tonic-gate 	mem->av_intarg2 = NULL;
1740Sstevel@tonic-gate 	mem->av_intr_id = NULL;
1750Sstevel@tonic-gate 	mem->av_prilevel = lvl;
176191Sahl 	mem->av_dip = NULL;
1770Sstevel@tonic-gate 	mem->av_link = NULL;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	mutex_enter(&av_lock);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (!nmivect) {
1820Sstevel@tonic-gate 		nmivect = mem;
1830Sstevel@tonic-gate 		mutex_exit(&av_lock);
1840Sstevel@tonic-gate 		return (1);
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	/* find where it goes in list */
1870Sstevel@tonic-gate 	for (p = nmivect; p != NULL; p = p->av_link) {
1880Sstevel@tonic-gate 		if (p->av_vector == nmintr && p->av_intarg1 == arg) {
1890Sstevel@tonic-gate 			/*
1900Sstevel@tonic-gate 			 * already in list
1910Sstevel@tonic-gate 			 * So? Somebody added the same interrupt twice.
1920Sstevel@tonic-gate 			 */
1930Sstevel@tonic-gate 			cmn_err(CE_WARN, "Driver already registered '%s'",
1940Sstevel@tonic-gate 			    name);
1950Sstevel@tonic-gate 			kmem_free(mem, sizeof (struct autovec));
1960Sstevel@tonic-gate 			mutex_exit(&av_lock);
1970Sstevel@tonic-gate 			return (0);
1980Sstevel@tonic-gate 		}
1990Sstevel@tonic-gate 		if (p->av_prilevel < lvl) {
2000Sstevel@tonic-gate 			if (p == nmivect) {   /* it's at head of list */
2010Sstevel@tonic-gate 				mem->av_link = p;
2020Sstevel@tonic-gate 				nmivect = mem;
2030Sstevel@tonic-gate 			} else {
2040Sstevel@tonic-gate 				mem->av_link = p;
2050Sstevel@tonic-gate 				prev->av_link = mem;
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 			mutex_exit(&av_lock);
2080Sstevel@tonic-gate 			return (1);
2090Sstevel@tonic-gate 		}
2100Sstevel@tonic-gate 		prev = p;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	/* didn't find it, add it to the end */
2140Sstevel@tonic-gate 	prev->av_link = mem;
2150Sstevel@tonic-gate 	mutex_exit(&av_lock);
2160Sstevel@tonic-gate 	return (1);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * register a hardware interrupt handler.
2220Sstevel@tonic-gate  */
2230Sstevel@tonic-gate int
2240Sstevel@tonic-gate add_avintr(void *intr_id, int lvl, avfunc xxintr, char *name, int vect,
225916Sschwartz     caddr_t arg1, caddr_t arg2, uint64_t *ticksp, dev_info_t *dip)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
2280Sstevel@tonic-gate 	avfunc f;
2290Sstevel@tonic-gate 	int s, vectindex;			/* save old spl value */
2300Sstevel@tonic-gate 	ushort_t hi_pri;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if ((f = xxintr) == NULL) {
2330Sstevel@tonic-gate 		printf("Attempt to add null vect for %s on vector %d\n",
2344652Scwb 		    name, vect);
2350Sstevel@tonic-gate 		return (0);
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	vectindex = vect % MAX_VECT;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	vecp = &autovect[vectindex];
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * "hi_pri == 0" implies all entries on list are "unused",
2440Sstevel@tonic-gate 	 * which means that it's OK to just insert this one.
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	hi_pri = vecp->avh_hi_pri;
2470Sstevel@tonic-gate 	if (vecp->avh_link && (hi_pri != 0)) {
2480Sstevel@tonic-gate 		if (((hi_pri > LOCK_LEVEL) && (lvl < LOCK_LEVEL)) ||
2490Sstevel@tonic-gate 		    ((hi_pri < LOCK_LEVEL) && (lvl > LOCK_LEVEL))) {
2500Sstevel@tonic-gate 			cmn_err(CE_WARN, multilevel2, name, lvl, vect,
2514652Scwb 			    hi_pri);
2520Sstevel@tonic-gate 			return (0);
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 		if ((vecp->avh_lo_pri != lvl) || (hi_pri != lvl))
2550Sstevel@tonic-gate 			cmn_err(CE_NOTE, multilevel, vect);
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
258999Slq150181 	insert_av(intr_id, vecp, f, arg1, arg2, ticksp, lvl, dip);
2590Sstevel@tonic-gate 	s = splhi();
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * do what ever machine specific things are necessary
2620Sstevel@tonic-gate 	 * to set priority level (e.g. set picmasks)
2630Sstevel@tonic-gate 	 */
2640Sstevel@tonic-gate 	mutex_enter(&av_lock);
2650Sstevel@tonic-gate 	(*addspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri);
2660Sstevel@tonic-gate 	mutex_exit(&av_lock);
2670Sstevel@tonic-gate 	splx(s);
2680Sstevel@tonic-gate 	return (1);
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate void
2730Sstevel@tonic-gate update_avsoftintr_args(void *intr_id, int lvl, caddr_t arg2)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	struct autovec *p;
2760Sstevel@tonic-gate 	struct autovec *target = NULL;
2770Sstevel@tonic-gate 	struct av_head *vectp = (struct av_head *)&softvect[lvl];
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	for (p = vectp->avh_link; p && p->av_vector; p = p->av_link) {
2800Sstevel@tonic-gate 		if (p->av_intr_id == intr_id) {
2810Sstevel@tonic-gate 			target = p;
2820Sstevel@tonic-gate 			break;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (target == NULL)
2870Sstevel@tonic-gate 		return;
2880Sstevel@tonic-gate 	target->av_intarg2 = arg2;
2890Sstevel@tonic-gate }
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate  * Register a software interrupt handler
2930Sstevel@tonic-gate  */
2940Sstevel@tonic-gate int
2950Sstevel@tonic-gate add_avsoftintr(void *intr_id, int lvl, avfunc xxintr, char *name,
2960Sstevel@tonic-gate     caddr_t arg1, caddr_t arg2)
2970Sstevel@tonic-gate {
2980Sstevel@tonic-gate 	int slvl;
299999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if ((slvl = slvltovect(lvl)) != -1)
3020Sstevel@tonic-gate 		return (add_avintr(intr_id, lvl, xxintr,
303916Sschwartz 		    name, slvl, arg1, arg2, NULL, NULL));
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	if (intr_id == NULL) {
3060Sstevel@tonic-gate 		printf("Attempt to add null intr_id for %s on level %d\n",
3070Sstevel@tonic-gate 		    name, lvl);
3080Sstevel@tonic-gate 		return (0);
3090Sstevel@tonic-gate 	}
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	if (xxintr == NULL) {
3120Sstevel@tonic-gate 		printf("Attempt to add null handler for %s on level %d\n",
3130Sstevel@tonic-gate 		    name, lvl);
3140Sstevel@tonic-gate 		return (0);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 	if (lvl <= 0 || lvl > LOCK_LEVEL) {
3180Sstevel@tonic-gate 		printf(badsoft, lvl, name);
3190Sstevel@tonic-gate 		return (0);
3200Sstevel@tonic-gate 	}
321999Slq150181 
322999Slq150181 	if (hdlp->ih_pending == NULL) {
323999Slq150181 		hdlp->ih_pending =
3244652Scwb 		    kmem_zalloc(sizeof (av_softinfo_t), KM_SLEEP);
3250Sstevel@tonic-gate 	}
326999Slq150181 
327999Slq150181 	insert_av(intr_id, &softvect[lvl], xxintr, arg1, arg2, NULL, lvl, NULL);
328999Slq150181 
3290Sstevel@tonic-gate 	return (1);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /* insert an interrupt vector into chain */
333999Slq150181 static void
3340Sstevel@tonic-gate insert_av(void *intr_id, struct av_head *vectp, avfunc f, caddr_t arg1,
335916Sschwartz     caddr_t arg2, uint64_t *ticksp, int pri_level, dev_info_t *dip)
3360Sstevel@tonic-gate {
3370Sstevel@tonic-gate 	/*
3380Sstevel@tonic-gate 	 * Protect rewrites of the list
3390Sstevel@tonic-gate 	 */
3400Sstevel@tonic-gate 	struct autovec *p, *mem;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 	mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP);
3430Sstevel@tonic-gate 	mem->av_vector = f;
3440Sstevel@tonic-gate 	mem->av_intarg1 = arg1;
3450Sstevel@tonic-gate 	mem->av_intarg2 = arg2;
346916Sschwartz 	mem->av_ticksp = ticksp;
3470Sstevel@tonic-gate 	mem->av_intr_id = intr_id;
3480Sstevel@tonic-gate 	mem->av_prilevel = pri_level;
3490Sstevel@tonic-gate 	mem->av_dip = dip;
3500Sstevel@tonic-gate 	mem->av_link = NULL;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	mutex_enter(&av_lock);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	if (vectp->avh_link == NULL) {	/* Nothing on list - put it at head */
3550Sstevel@tonic-gate 		vectp->avh_link = mem;
3560Sstevel@tonic-gate 		vectp->avh_hi_pri = vectp->avh_lo_pri = (ushort_t)pri_level;
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 		mutex_exit(&av_lock);
359999Slq150181 		return;
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	/* find where it goes in list */
3630Sstevel@tonic-gate 	for (p = vectp->avh_link; p != NULL; p = p->av_link) {
3640Sstevel@tonic-gate 		if (p->av_vector == NULL) {	/* freed struct available */
3650Sstevel@tonic-gate 			p->av_intarg1 = arg1;
3660Sstevel@tonic-gate 			p->av_intarg2 = arg2;
367916Sschwartz 			p->av_ticksp = ticksp;
3680Sstevel@tonic-gate 			p->av_intr_id = intr_id;
3690Sstevel@tonic-gate 			p->av_prilevel = pri_level;
370191Sahl 			p->av_dip = dip;
3710Sstevel@tonic-gate 			if (pri_level > (int)vectp->avh_hi_pri) {
3720Sstevel@tonic-gate 				vectp->avh_hi_pri = (ushort_t)pri_level;
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 			if (pri_level < (int)vectp->avh_lo_pri) {
3750Sstevel@tonic-gate 				vectp->avh_lo_pri = (ushort_t)pri_level;
3760Sstevel@tonic-gate 			}
3775084Sjohnlev 			/*
3785084Sjohnlev 			 * To prevent calling service routine before args
3795084Sjohnlev 			 * and ticksp are ready fill in vector last.
3805084Sjohnlev 			 */
3810Sstevel@tonic-gate 			p->av_vector = f;
3820Sstevel@tonic-gate 			mutex_exit(&av_lock);
3835084Sjohnlev 			kmem_free(mem, sizeof (struct autovec));
384999Slq150181 			return;
3850Sstevel@tonic-gate 		}
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	/* insert new intpt at beginning of chain */
3880Sstevel@tonic-gate 	mem->av_link = vectp->avh_link;
3890Sstevel@tonic-gate 	vectp->avh_link = mem;
3900Sstevel@tonic-gate 	if (pri_level > (int)vectp->avh_hi_pri) {
3910Sstevel@tonic-gate 		vectp->avh_hi_pri = (ushort_t)pri_level;
3920Sstevel@tonic-gate 	}
3930Sstevel@tonic-gate 	if (pri_level < (int)vectp->avh_lo_pri) {
3940Sstevel@tonic-gate 		vectp->avh_lo_pri = (ushort_t)pri_level;
3950Sstevel@tonic-gate 	}
3960Sstevel@tonic-gate 	mutex_exit(&av_lock);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate 
399999Slq150181 static int
400999Slq150181 av_rem_softintr(void *intr_id, int lvl, avfunc xxintr, boolean_t rem_softinfo)
4010Sstevel@tonic-gate {
4020Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
4030Sstevel@tonic-gate 	int slvl;
404999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
405999Slq150181 	av_softinfo_t *infop = (av_softinfo_t *)hdlp->ih_pending;
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	if (xxintr == NULL)
4080Sstevel@tonic-gate 		return (0);
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	if ((slvl = slvltovect(lvl)) != -1) {
4110Sstevel@tonic-gate 		rem_avintr(intr_id, lvl, xxintr, slvl);
4120Sstevel@tonic-gate 		return (1);
4130Sstevel@tonic-gate 	}
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (lvl <= 0 && lvl >= LOCK_LEVEL) {
4160Sstevel@tonic-gate 		return (0);
4170Sstevel@tonic-gate 	}
4180Sstevel@tonic-gate 	vecp = &softvect[lvl];
4190Sstevel@tonic-gate 	remove_av(intr_id, vecp, xxintr, lvl, 0);
4200Sstevel@tonic-gate 
421999Slq150181 	if (rem_softinfo) {
422999Slq150181 		kmem_free(infop, sizeof (av_softinfo_t));
423999Slq150181 		hdlp->ih_pending = NULL;
424999Slq150181 	}
425999Slq150181 
4260Sstevel@tonic-gate 	return (1);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
429999Slq150181 int
430999Slq150181 av_softint_movepri(void *intr_id, int old_lvl)
431999Slq150181 {
432999Slq150181 	int ret;
433999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
434999Slq150181 
435999Slq150181 	ret = add_avsoftintr(intr_id, hdlp->ih_pri, hdlp->ih_cb_func,
436999Slq150181 	    DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
437999Slq150181 
438999Slq150181 	if (ret) {
439999Slq150181 		(void) av_rem_softintr(intr_id, old_lvl, hdlp->ih_cb_func,
440999Slq150181 		    B_FALSE);
441999Slq150181 	}
442999Slq150181 
443999Slq150181 	return (ret);
444999Slq150181 }
445999Slq150181 
446999Slq150181 /*
447999Slq150181  * Remove a driver from the autovector list.
448999Slq150181  */
449999Slq150181 int
450999Slq150181 rem_avsoftintr(void *intr_id, int lvl, avfunc xxintr)
451999Slq150181 {
452999Slq150181 	return (av_rem_softintr(intr_id, lvl, xxintr, B_TRUE));
453999Slq150181 }
454999Slq150181 
4550Sstevel@tonic-gate void
4560Sstevel@tonic-gate rem_avintr(void *intr_id, int lvl, avfunc xxintr, int vect)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
4590Sstevel@tonic-gate 	avfunc f;
4600Sstevel@tonic-gate 	int s, vectindex;			/* save old spl value */
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	if ((f = xxintr) == NULL)
4630Sstevel@tonic-gate 		return;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	vectindex = vect % MAX_VECT;
4660Sstevel@tonic-gate 	vecp = &autovect[vectindex];
4670Sstevel@tonic-gate 	remove_av(intr_id, vecp, f, lvl, vect);
4680Sstevel@tonic-gate 	s = splhi();
4690Sstevel@tonic-gate 	mutex_enter(&av_lock);
4700Sstevel@tonic-gate 	(*delspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri);
4710Sstevel@tonic-gate 	mutex_exit(&av_lock);
4720Sstevel@tonic-gate 	splx(s);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * After having made a change to an autovector list, wait until we have
4780Sstevel@tonic-gate  * seen each cpu not executing an interrupt at that level--so we know our
4790Sstevel@tonic-gate  * change has taken effect completely (no old state in registers, etc).
4800Sstevel@tonic-gate  */
4810Sstevel@tonic-gate void
4820Sstevel@tonic-gate wait_till_seen(int ipl)
4830Sstevel@tonic-gate {
4840Sstevel@tonic-gate 	int cpu_in_chain, cix;
4850Sstevel@tonic-gate 	struct cpu *cpup;
4860Sstevel@tonic-gate 	cpuset_t cpus_to_check;
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	CPUSET_ALL(cpus_to_check);
4890Sstevel@tonic-gate 	do {
4900Sstevel@tonic-gate 		cpu_in_chain = 0;
4910Sstevel@tonic-gate 		for (cix = 0; cix < NCPU; cix++) {
4920Sstevel@tonic-gate 			cpup = cpu[cix];
4930Sstevel@tonic-gate 			if (cpup != NULL && CPU_IN_SET(cpus_to_check, cix)) {
4940Sstevel@tonic-gate 				if (intr_active(cpup, ipl)) {
4950Sstevel@tonic-gate 					cpu_in_chain = 1;
4960Sstevel@tonic-gate 				} else {
4970Sstevel@tonic-gate 					CPUSET_DEL(cpus_to_check, cix);
4980Sstevel@tonic-gate 				}
4990Sstevel@tonic-gate 			}
5000Sstevel@tonic-gate 		}
5010Sstevel@tonic-gate 	} while (cpu_in_chain);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5045084Sjohnlev static uint64_t dummy_tick;
5055084Sjohnlev 
5060Sstevel@tonic-gate /* remove an interrupt vector from the chain */
5070Sstevel@tonic-gate static void
5080Sstevel@tonic-gate remove_av(void *intr_id, struct av_head *vectp, avfunc f, int pri_level,
5090Sstevel@tonic-gate 	int vect)
5100Sstevel@tonic-gate {
5115084Sjohnlev 	struct autovec *p, *target;
5120Sstevel@tonic-gate 	int	lo_pri, hi_pri;
5130Sstevel@tonic-gate 	int	ipl;
5140Sstevel@tonic-gate 	/*
5150Sstevel@tonic-gate 	 * Protect rewrites of the list
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	target = NULL;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	mutex_enter(&av_lock);
5200Sstevel@tonic-gate 	ipl = pri_level;
5210Sstevel@tonic-gate 	lo_pri = MAXIPL;
5220Sstevel@tonic-gate 	hi_pri = 0;
5235084Sjohnlev 	for (p = vectp->avh_link; p; p = p->av_link) {
5240Sstevel@tonic-gate 		if ((p->av_vector == f) && (p->av_intr_id == intr_id)) {
5250Sstevel@tonic-gate 			/* found the handler */
5260Sstevel@tonic-gate 			target = p;
5270Sstevel@tonic-gate 			continue;
5280Sstevel@tonic-gate 		}
5295084Sjohnlev 		if (p->av_vector != NULL) {
5305084Sjohnlev 			if (p->av_prilevel > hi_pri)
5315084Sjohnlev 				hi_pri = p->av_prilevel;
5325084Sjohnlev 			if (p->av_prilevel < lo_pri)
5335084Sjohnlev 				lo_pri = p->av_prilevel;
5345084Sjohnlev 		}
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate 	if (ipl < hi_pri)
5370Sstevel@tonic-gate 		ipl = hi_pri;
5380Sstevel@tonic-gate 	if (target == NULL) {	/* not found */
5390Sstevel@tonic-gate 		printf("Couldn't remove function %p at %d, %d\n",
5404652Scwb 		    (void *)f, vect, pri_level);
5410Sstevel@tonic-gate 		mutex_exit(&av_lock);
5420Sstevel@tonic-gate 		return;
5430Sstevel@tonic-gate 	}
5440Sstevel@tonic-gate 
5455084Sjohnlev 	/*
5465084Sjohnlev 	 * This drops the handler from the chain, it can no longer be called.
5475084Sjohnlev 	 * However, there is no guarantee that the handler is not currently
5485084Sjohnlev 	 * still executing.
5495084Sjohnlev 	 */
5500Sstevel@tonic-gate 	target->av_vector = NULL;
5515084Sjohnlev 	/*
5525084Sjohnlev 	 * There is a race where we could be just about to pick up the ticksp
5535084Sjohnlev 	 * pointer to increment it after returning from the service routine
5545084Sjohnlev 	 * in av_dispatch_autovect.  Rather than NULL it out let's just point
5555084Sjohnlev 	 * it off to something safe so that any final tick update attempt
5565084Sjohnlev 	 * won't fault.
5575084Sjohnlev 	 */
5585084Sjohnlev 	target->av_ticksp = &dummy_tick;
5590Sstevel@tonic-gate 	wait_till_seen(ipl);
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate 	if (lo_pri > hi_pri) {	/* the chain is now empty */
5620Sstevel@tonic-gate 		/* Leave the unused entries here for probable future use */
5630Sstevel@tonic-gate 		vectp->avh_lo_pri = MAXIPL;
5640Sstevel@tonic-gate 		vectp->avh_hi_pri = 0;
5650Sstevel@tonic-gate 	} else {
5660Sstevel@tonic-gate 		if ((int)vectp->avh_lo_pri < lo_pri)
5670Sstevel@tonic-gate 			vectp->avh_lo_pri = (ushort_t)lo_pri;
5680Sstevel@tonic-gate 		if ((int)vectp->avh_hi_pri > hi_pri)
5690Sstevel@tonic-gate 			vectp->avh_hi_pri = (ushort_t)hi_pri;
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 	mutex_exit(&av_lock);
5720Sstevel@tonic-gate 	wait_till_seen(ipl);
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate /*
5764652Scwb  * kmdb uses siron (and thus setsoftint) while the world is stopped in order to
5774652Scwb  * inform its driver component that there's work to be done.  We need to keep
5784652Scwb  * DTrace from instrumenting kmdb's siron and setsoftint.  We duplicate siron,
5794652Scwb  * giving kmdb's version a kdi prefix to keep DTrace at bay.   We also
5804652Scwb  * provide a version of the various setsoftint functions available for kmdb to
5814652Scwb  * use using a kdi_ prefix while the main *setsoftint() functionality is
5824652Scwb  * implemented as a wrapper.  This allows tracing, while still providing a
5834652Scwb  * way for kmdb to sneak in unmolested.
5844652Scwb  */
5854652Scwb void
5864652Scwb kdi_siron(void)
5874652Scwb {
5884652Scwb 	(*kdisetsoftint)(1, softlevel1_hdl.ih_pending);
5894652Scwb }
5904652Scwb 
5914652Scwb /*
5920Sstevel@tonic-gate  * Trigger a soft interrupt.
5930Sstevel@tonic-gate  */
5940Sstevel@tonic-gate void
5950Sstevel@tonic-gate siron(void)
5960Sstevel@tonic-gate {
597*5107Seota 	/* Level 1 software interrupt */
598999Slq150181 	(*setsoftint)(1, softlevel1_hdl.ih_pending);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
602*5107Seota  * Trigger software interrupts dedicated to ddi timer.
603*5107Seota  */
604*5107Seota void
605*5107Seota sir_on(int level)
606*5107Seota {
607*5107Seota 	ASSERT(level >= DDI_IPL_1 && level <= DDI_IPL_10);
608*5107Seota 	(*setsoftint)(level, softlevel_hdl[level-1].ih_pending);
609*5107Seota }
610*5107Seota 
611*5107Seota /*
6125076Smishra  * The handler which is executed on the target CPU.
6135076Smishra  */
6145076Smishra /*ARGSUSED*/
6155076Smishra static int
6165076Smishra siron_poke_intr(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
6175076Smishra {
6185076Smishra 	siron();
6195076Smishra 	return (0);
6205076Smishra }
6215076Smishra 
6225076Smishra /*
6235076Smishra  * May get called from softcall to poke CPUs.
6245076Smishra  */
6255076Smishra void
6265076Smishra siron_poke_cpu(cpuset_t poke)
6275076Smishra {
6285076Smishra 	int cpuid = CPU->cpu_id;
6295076Smishra 
6305076Smishra 	/*
6315076Smishra 	 * If we are poking to ourself then we can simply
6325076Smishra 	 * generate level1 using siron()
6335076Smishra 	 */
6345076Smishra 	if (CPU_IN_SET(poke, cpuid)) {
6355076Smishra 		siron();
6365076Smishra 		CPUSET_DEL(poke, cpuid);
6375076Smishra 		if (CPUSET_ISNULL(poke))
6385076Smishra 			return;
6395076Smishra 	}
6405076Smishra 
6415076Smishra 	xc_call(0, 0, 0, X_CALL_MEDPRI, poke, (xc_func_t)siron_poke_intr);
6425076Smishra }
6435076Smishra 
6445076Smishra /*
6450Sstevel@tonic-gate  * Walk the autovector table for this vector, invoking each
6460Sstevel@tonic-gate  * interrupt handler as we go.
6470Sstevel@tonic-gate  */
648916Sschwartz 
649916Sschwartz extern uint64_t intr_get_time(void);
650916Sschwartz 
6510Sstevel@tonic-gate void
6520Sstevel@tonic-gate av_dispatch_autovect(uint_t vec)
6530Sstevel@tonic-gate {
6540Sstevel@tonic-gate 	struct autovec *av;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
6570Sstevel@tonic-gate 
6580Sstevel@tonic-gate 	while ((av = autovect[vec].avh_link) != NULL) {
6590Sstevel@tonic-gate 		uint_t numcalled = 0;
6600Sstevel@tonic-gate 		uint_t claimed = 0;
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 		for (; av; av = av->av_link) {
6630Sstevel@tonic-gate 			uint_t r;
6640Sstevel@tonic-gate 			uint_t (*intr)() = av->av_vector;
6650Sstevel@tonic-gate 			caddr_t arg1 = av->av_intarg1;
6660Sstevel@tonic-gate 			caddr_t arg2 = av->av_intarg2;
6670Sstevel@tonic-gate 			dev_info_t *dip = av->av_dip;
6680Sstevel@tonic-gate 
6695084Sjohnlev 			/*
6705084Sjohnlev 			 * We must walk the entire chain.  Removed handlers
6715084Sjohnlev 			 * may be anywhere in the chain.
6725084Sjohnlev 			 */
6730Sstevel@tonic-gate 			if (intr == NULL)
6745084Sjohnlev 				continue;
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t *, dip,
6770Sstevel@tonic-gate 			    void *, intr, caddr_t, arg1, caddr_t, arg2);
6780Sstevel@tonic-gate 			r = (*intr)(arg1, arg2);
6790Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t *, dip,
6800Sstevel@tonic-gate 			    void *, intr, caddr_t, arg1, uint_t, r);
6813446Smrj 			numcalled++;
6820Sstevel@tonic-gate 			claimed |= r;
683916Sschwartz 			if (av->av_ticksp && av->av_prilevel <= LOCK_LEVEL)
684916Sschwartz 				atomic_add_64(av->av_ticksp, intr_get_time());
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * If there's only one interrupt handler in the chain,
6890Sstevel@tonic-gate 		 * or if no-one claimed the interrupt at all give up now.
6900Sstevel@tonic-gate 		 */
6910Sstevel@tonic-gate 		if (numcalled == 1 || claimed == 0)
6920Sstevel@tonic-gate 			break;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate }
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate /*
6970Sstevel@tonic-gate  * Call every soft interrupt handler we can find at this level once.
6980Sstevel@tonic-gate  */
6990Sstevel@tonic-gate void
7000Sstevel@tonic-gate av_dispatch_softvect(uint_t pil)
7010Sstevel@tonic-gate {
7020Sstevel@tonic-gate 	struct autovec *av;
7030Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp;
7040Sstevel@tonic-gate 	uint_t (*intr)();
7050Sstevel@tonic-gate 	caddr_t arg1;
7060Sstevel@tonic-gate 	caddr_t arg2;
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
7090Sstevel@tonic-gate 	ASSERT(pil >= 0 && pil <= PIL_MAX);
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	for (av = softvect[pil].avh_link; av; av = av->av_link) {
7125084Sjohnlev 		/*
7135084Sjohnlev 		 * We must walk the entire chain.  Removed handlers
7145084Sjohnlev 		 * may be anywhere in the chain.
7155084Sjohnlev 		 */
7160Sstevel@tonic-gate 		if ((intr = av->av_vector) == NULL)
7175084Sjohnlev 			continue;
7180Sstevel@tonic-gate 		arg1 = av->av_intarg1;
7190Sstevel@tonic-gate 		arg2 = av->av_intarg2;
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 		hdlp = (ddi_softint_hdl_impl_t *)av->av_intr_id;
7220Sstevel@tonic-gate 		ASSERT(hdlp);
7230Sstevel@tonic-gate 
724999Slq150181 		/*
725999Slq150181 		 * Each cpu has its own pending bit in hdlp->ih_pending,
726999Slq150181 		 * here av_check/clear_softint_pending is just checking
727999Slq150181 		 * and clearing the pending bit for the current cpu, who
728999Slq150181 		 * has just triggered a softint.
729999Slq150181 		 */
730999Slq150181 		if (av_check_softint_pending(hdlp->ih_pending, B_FALSE)) {
731999Slq150181 			av_clear_softint_pending(hdlp->ih_pending);
7320Sstevel@tonic-gate 			(void) (*intr)(arg1, arg2);
7330Sstevel@tonic-gate 		}
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate }
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate struct regs;
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7400Sstevel@tonic-gate  * Call every NMI handler we know of once.
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate void
7430Sstevel@tonic-gate av_dispatch_nmivect(struct regs *rp)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	struct autovec *av;
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	for (av = nmivect; av; av = av->av_link)
7500Sstevel@tonic-gate 		(void) (av->av_vector)(av->av_intarg1, rp);
7510Sstevel@tonic-gate }
752