xref: /onnv-gate/usr/src/uts/common/io/avintr.c (revision 9717:faa3a34d70d8)
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 /*
229489SJoe.Bonasera@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Autovectored Interrupt Configuration and Deconfiguration
280Sstevel@tonic-gate  */
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/cmn_err.h>
320Sstevel@tonic-gate #include <sys/trap.h>
330Sstevel@tonic-gate #include <sys/t_lock.h>
340Sstevel@tonic-gate #include <sys/avintr.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/machlock.h>
370Sstevel@tonic-gate #include <sys/systm.h>
380Sstevel@tonic-gate #include <sys/machsystm.h>
390Sstevel@tonic-gate #include <sys/sunddi.h>
400Sstevel@tonic-gate #include <sys/x_call.h>
410Sstevel@tonic-gate #include <sys/cpuvar.h>
420Sstevel@tonic-gate #include <sys/atomic.h>
430Sstevel@tonic-gate #include <sys/smp_impldefs.h>
440Sstevel@tonic-gate #include <sys/sdt.h>
450Sstevel@tonic-gate #include <sys/stack.h>
460Sstevel@tonic-gate #include <sys/ddi_impldefs.h>
475084Sjohnlev #ifdef __xpv
485084Sjohnlev #include <sys/evtchn_impl.h>
495084Sjohnlev #endif
500Sstevel@tonic-gate 
51999Slq150181 typedef struct av_softinfo {
52999Slq150181 	cpuset_t	av_pending;	/* pending bitmasks */
53999Slq150181 } av_softinfo_t;
54999Slq150181 
55999Slq150181 static void insert_av(void *intr_id, struct av_head *vectp, avfunc f,
56916Sschwartz 	caddr_t arg1, caddr_t arg2, uint64_t *ticksp, int pri_level,
57916Sschwartz 	dev_info_t *dip);
580Sstevel@tonic-gate static void remove_av(void *intr_id, struct av_head *vectp, avfunc f,
590Sstevel@tonic-gate 	int pri_level, int vect);
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * Arrange for a driver to be called when a particular
630Sstevel@tonic-gate  * auto-vectored interrupt occurs.
640Sstevel@tonic-gate  * NOTE: if a device can generate interrupts on more than
650Sstevel@tonic-gate  * one level, or if a driver services devices that interrupt
660Sstevel@tonic-gate  * on more than one level, then the driver should install
670Sstevel@tonic-gate  * itself on each of those levels.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static char badsoft[] =
700Sstevel@tonic-gate 	"add_avintr: bad soft interrupt level %d for driver '%s'\n";
710Sstevel@tonic-gate static char multilevel[] =
720Sstevel@tonic-gate 	"!IRQ%d is being shared by drivers with different interrupt levels.\n"
730Sstevel@tonic-gate 	"This may result in reduced system performance.";
740Sstevel@tonic-gate static char multilevel2[] =
750Sstevel@tonic-gate 	"Cannot register interrupt for '%s' device at IPL %d because it\n"
760Sstevel@tonic-gate 	"conflicts with another device using the same vector %d with an IPL\n"
770Sstevel@tonic-gate 	"of %d. Reconfigure the conflicting devices to use different vectors.";
780Sstevel@tonic-gate 
795084Sjohnlev #ifdef __xpv
805084Sjohnlev #define	MAX_VECT	NR_IRQS
815084Sjohnlev #else
820Sstevel@tonic-gate #define	MAX_VECT	256
835084Sjohnlev #endif
845084Sjohnlev 
850Sstevel@tonic-gate struct autovec *nmivect = NULL;
860Sstevel@tonic-gate struct av_head autovect[MAX_VECT];
870Sstevel@tonic-gate struct av_head softvect[LOCK_LEVEL + 1];
880Sstevel@tonic-gate kmutex_t av_lock;
895107Seota /*
905107Seota  * These are software interrupt handlers dedicated to ddi timer.
915107Seota  * The interrupt levels up to 10 are supported, but high interrupts
925107Seota  * must not be used there.
935107Seota  */
945107Seota ddi_softint_hdl_impl_t softlevel_hdl[DDI_IPL_10] = {
955107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 1 */
965107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 2 */
975107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 3 */
985107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 4 */
995107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 5 */
1005107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 6 */
1015107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 7 */
1025107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 8 */
1035107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 9 */
1045107Seota 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL}, /* level 10 */
1055107Seota };
1060Sstevel@tonic-gate ddi_softint_hdl_impl_t softlevel1_hdl =
107999Slq150181 	{0, NULL, NULL, NULL, 0, NULL, NULL, NULL};
108999Slq150181 
109999Slq150181 /*
110999Slq150181  * clear/check softint pending flag corresponding for
111999Slq150181  * the current CPU
112999Slq150181  */
1130Sstevel@tonic-gate void
114999Slq150181 av_clear_softint_pending(av_softinfo_t *infop)
115999Slq150181 {
116999Slq150181 	CPUSET_ATOMIC_DEL(infop->av_pending, CPU->cpu_seqid);
117999Slq150181 }
118999Slq150181 
119999Slq150181 boolean_t
120999Slq150181 av_check_softint_pending(av_softinfo_t *infop, boolean_t check_all)
1210Sstevel@tonic-gate {
122999Slq150181 	if (check_all)
123999Slq150181 		return (!CPUSET_ISNULL(infop->av_pending));
124999Slq150181 	else
125999Slq150181 		return (CPU_IN_SET(infop->av_pending, CPU->cpu_seqid) != 0);
126999Slq150181 }
127999Slq150181 
128999Slq150181 /*
1294652Scwb  * This is the wrapper function which is generally used to set a softint
1304652Scwb  * pending
1314652Scwb  */
1324652Scwb void
1334652Scwb av_set_softint_pending(int pri, av_softinfo_t *infop)
1344652Scwb {
1354652Scwb 	kdi_av_set_softint_pending(pri, infop);
1364652Scwb }
1374652Scwb 
1384652Scwb /*
1394652Scwb  * This is kmdb's private entry point to setsoftint called from kdi_siron
140999Slq150181  * It first sets our av softint pending bit for the current CPU,
141999Slq150181  * then it sets the CPU softint pending bit for pri.
142999Slq150181  */
143999Slq150181 void
1444652Scwb kdi_av_set_softint_pending(int pri, av_softinfo_t *infop)
145999Slq150181 {
146999Slq150181 	CPUSET_ATOMIC_ADD(infop->av_pending, CPU->cpu_seqid);
147999Slq150181 
1480Sstevel@tonic-gate 	atomic_or_32((uint32_t *)&CPU->cpu_softinfo.st_pending, 1 << pri);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * register nmi interrupt routine. The first arg is used only to order
1530Sstevel@tonic-gate  * various nmi interrupt service routines in the chain. Higher lvls will
1540Sstevel@tonic-gate  * be called first
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate int
1570Sstevel@tonic-gate add_nmintr(int lvl, avfunc nmintr, char *name, caddr_t arg)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	struct autovec  *mem;
1600Sstevel@tonic-gate 	struct autovec *p, *prev = NULL;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (nmintr == NULL) {
1630Sstevel@tonic-gate 		printf("Attempt to add null vect for %s on nmi\n", name);
1640Sstevel@tonic-gate 		return (0);
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP);
1690Sstevel@tonic-gate 	mem->av_vector = nmintr;
1700Sstevel@tonic-gate 	mem->av_intarg1 = arg;
1710Sstevel@tonic-gate 	mem->av_intarg2 = NULL;
1720Sstevel@tonic-gate 	mem->av_intr_id = NULL;
1730Sstevel@tonic-gate 	mem->av_prilevel = lvl;
174191Sahl 	mem->av_dip = NULL;
1750Sstevel@tonic-gate 	mem->av_link = NULL;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	mutex_enter(&av_lock);
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (!nmivect) {
1800Sstevel@tonic-gate 		nmivect = mem;
1810Sstevel@tonic-gate 		mutex_exit(&av_lock);
1820Sstevel@tonic-gate 		return (1);
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	/* find where it goes in list */
1850Sstevel@tonic-gate 	for (p = nmivect; p != NULL; p = p->av_link) {
1860Sstevel@tonic-gate 		if (p->av_vector == nmintr && p->av_intarg1 == arg) {
1870Sstevel@tonic-gate 			/*
1880Sstevel@tonic-gate 			 * already in list
1890Sstevel@tonic-gate 			 * So? Somebody added the same interrupt twice.
1900Sstevel@tonic-gate 			 */
1910Sstevel@tonic-gate 			cmn_err(CE_WARN, "Driver already registered '%s'",
1920Sstevel@tonic-gate 			    name);
1930Sstevel@tonic-gate 			kmem_free(mem, sizeof (struct autovec));
1940Sstevel@tonic-gate 			mutex_exit(&av_lock);
1950Sstevel@tonic-gate 			return (0);
1960Sstevel@tonic-gate 		}
1970Sstevel@tonic-gate 		if (p->av_prilevel < lvl) {
1980Sstevel@tonic-gate 			if (p == nmivect) {   /* it's at head of list */
1990Sstevel@tonic-gate 				mem->av_link = p;
2000Sstevel@tonic-gate 				nmivect = mem;
2010Sstevel@tonic-gate 			} else {
2020Sstevel@tonic-gate 				mem->av_link = p;
2030Sstevel@tonic-gate 				prev->av_link = mem;
2040Sstevel@tonic-gate 			}
2050Sstevel@tonic-gate 			mutex_exit(&av_lock);
2060Sstevel@tonic-gate 			return (1);
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		prev = p;
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 	/* didn't find it, add it to the end */
2120Sstevel@tonic-gate 	prev->av_link = mem;
2130Sstevel@tonic-gate 	mutex_exit(&av_lock);
2140Sstevel@tonic-gate 	return (1);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate /*
2190Sstevel@tonic-gate  * register a hardware interrupt handler.
2200Sstevel@tonic-gate  */
2210Sstevel@tonic-gate int
2220Sstevel@tonic-gate add_avintr(void *intr_id, int lvl, avfunc xxintr, char *name, int vect,
223916Sschwartz     caddr_t arg1, caddr_t arg2, uint64_t *ticksp, dev_info_t *dip)
2240Sstevel@tonic-gate {
2250Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
2260Sstevel@tonic-gate 	avfunc f;
2270Sstevel@tonic-gate 	int s, vectindex;			/* save old spl value */
2280Sstevel@tonic-gate 	ushort_t hi_pri;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	if ((f = xxintr) == NULL) {
2310Sstevel@tonic-gate 		printf("Attempt to add null vect for %s on vector %d\n",
2324652Scwb 		    name, vect);
2330Sstevel@tonic-gate 		return (0);
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	}
2360Sstevel@tonic-gate 	vectindex = vect % MAX_VECT;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	vecp = &autovect[vectindex];
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	/*
2410Sstevel@tonic-gate 	 * "hi_pri == 0" implies all entries on list are "unused",
2420Sstevel@tonic-gate 	 * which means that it's OK to just insert this one.
2430Sstevel@tonic-gate 	 */
2440Sstevel@tonic-gate 	hi_pri = vecp->avh_hi_pri;
2450Sstevel@tonic-gate 	if (vecp->avh_link && (hi_pri != 0)) {
2460Sstevel@tonic-gate 		if (((hi_pri > LOCK_LEVEL) && (lvl < LOCK_LEVEL)) ||
2470Sstevel@tonic-gate 		    ((hi_pri < LOCK_LEVEL) && (lvl > LOCK_LEVEL))) {
2480Sstevel@tonic-gate 			cmn_err(CE_WARN, multilevel2, name, lvl, vect,
2494652Scwb 			    hi_pri);
2500Sstevel@tonic-gate 			return (0);
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		if ((vecp->avh_lo_pri != lvl) || (hi_pri != lvl))
2530Sstevel@tonic-gate 			cmn_err(CE_NOTE, multilevel, vect);
2540Sstevel@tonic-gate 	}
2550Sstevel@tonic-gate 
256999Slq150181 	insert_av(intr_id, vecp, f, arg1, arg2, ticksp, lvl, dip);
2570Sstevel@tonic-gate 	s = splhi();
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * do what ever machine specific things are necessary
2600Sstevel@tonic-gate 	 * to set priority level (e.g. set picmasks)
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	mutex_enter(&av_lock);
2630Sstevel@tonic-gate 	(*addspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri);
2640Sstevel@tonic-gate 	mutex_exit(&av_lock);
2650Sstevel@tonic-gate 	splx(s);
2660Sstevel@tonic-gate 	return (1);
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate }
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate void
2710Sstevel@tonic-gate update_avsoftintr_args(void *intr_id, int lvl, caddr_t arg2)
2720Sstevel@tonic-gate {
2730Sstevel@tonic-gate 	struct autovec *p;
2740Sstevel@tonic-gate 	struct autovec *target = NULL;
2750Sstevel@tonic-gate 	struct av_head *vectp = (struct av_head *)&softvect[lvl];
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	for (p = vectp->avh_link; p && p->av_vector; p = p->av_link) {
2780Sstevel@tonic-gate 		if (p->av_intr_id == intr_id) {
2790Sstevel@tonic-gate 			target = p;
2800Sstevel@tonic-gate 			break;
2810Sstevel@tonic-gate 		}
2820Sstevel@tonic-gate 	}
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	if (target == NULL)
2850Sstevel@tonic-gate 		return;
2860Sstevel@tonic-gate 	target->av_intarg2 = arg2;
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate /*
2900Sstevel@tonic-gate  * Register a software interrupt handler
2910Sstevel@tonic-gate  */
2920Sstevel@tonic-gate int
2930Sstevel@tonic-gate add_avsoftintr(void *intr_id, int lvl, avfunc xxintr, char *name,
2940Sstevel@tonic-gate     caddr_t arg1, caddr_t arg2)
2950Sstevel@tonic-gate {
2960Sstevel@tonic-gate 	int slvl;
297999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 	if ((slvl = slvltovect(lvl)) != -1)
3000Sstevel@tonic-gate 		return (add_avintr(intr_id, lvl, xxintr,
301916Sschwartz 		    name, slvl, arg1, arg2, NULL, NULL));
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (intr_id == NULL) {
3040Sstevel@tonic-gate 		printf("Attempt to add null intr_id for %s on level %d\n",
3050Sstevel@tonic-gate 		    name, lvl);
3060Sstevel@tonic-gate 		return (0);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (xxintr == NULL) {
3100Sstevel@tonic-gate 		printf("Attempt to add null handler for %s on level %d\n",
3110Sstevel@tonic-gate 		    name, lvl);
3120Sstevel@tonic-gate 		return (0);
3130Sstevel@tonic-gate 	}
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 	if (lvl <= 0 || lvl > LOCK_LEVEL) {
3160Sstevel@tonic-gate 		printf(badsoft, lvl, name);
3170Sstevel@tonic-gate 		return (0);
3180Sstevel@tonic-gate 	}
319999Slq150181 
320999Slq150181 	if (hdlp->ih_pending == NULL) {
321999Slq150181 		hdlp->ih_pending =
3224652Scwb 		    kmem_zalloc(sizeof (av_softinfo_t), KM_SLEEP);
3230Sstevel@tonic-gate 	}
324999Slq150181 
325999Slq150181 	insert_av(intr_id, &softvect[lvl], xxintr, arg1, arg2, NULL, lvl, NULL);
326999Slq150181 
3270Sstevel@tonic-gate 	return (1);
3280Sstevel@tonic-gate }
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate /* insert an interrupt vector into chain */
331999Slq150181 static void
3320Sstevel@tonic-gate insert_av(void *intr_id, struct av_head *vectp, avfunc f, caddr_t arg1,
333916Sschwartz     caddr_t arg2, uint64_t *ticksp, int pri_level, dev_info_t *dip)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	/*
3360Sstevel@tonic-gate 	 * Protect rewrites of the list
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	struct autovec *p, *mem;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	mem = kmem_zalloc(sizeof (struct autovec), KM_SLEEP);
3410Sstevel@tonic-gate 	mem->av_vector = f;
3420Sstevel@tonic-gate 	mem->av_intarg1 = arg1;
3430Sstevel@tonic-gate 	mem->av_intarg2 = arg2;
344916Sschwartz 	mem->av_ticksp = ticksp;
3450Sstevel@tonic-gate 	mem->av_intr_id = intr_id;
3460Sstevel@tonic-gate 	mem->av_prilevel = pri_level;
3470Sstevel@tonic-gate 	mem->av_dip = dip;
3480Sstevel@tonic-gate 	mem->av_link = NULL;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	mutex_enter(&av_lock);
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	if (vectp->avh_link == NULL) {	/* Nothing on list - put it at head */
3530Sstevel@tonic-gate 		vectp->avh_link = mem;
3540Sstevel@tonic-gate 		vectp->avh_hi_pri = vectp->avh_lo_pri = (ushort_t)pri_level;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 		mutex_exit(&av_lock);
357999Slq150181 		return;
3580Sstevel@tonic-gate 	}
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	/* find where it goes in list */
3610Sstevel@tonic-gate 	for (p = vectp->avh_link; p != NULL; p = p->av_link) {
3620Sstevel@tonic-gate 		if (p->av_vector == NULL) {	/* freed struct available */
3630Sstevel@tonic-gate 			p->av_intarg1 = arg1;
3640Sstevel@tonic-gate 			p->av_intarg2 = arg2;
365916Sschwartz 			p->av_ticksp = ticksp;
3660Sstevel@tonic-gate 			p->av_intr_id = intr_id;
3670Sstevel@tonic-gate 			p->av_prilevel = pri_level;
368191Sahl 			p->av_dip = dip;
3690Sstevel@tonic-gate 			if (pri_level > (int)vectp->avh_hi_pri) {
3700Sstevel@tonic-gate 				vectp->avh_hi_pri = (ushort_t)pri_level;
3710Sstevel@tonic-gate 			}
3720Sstevel@tonic-gate 			if (pri_level < (int)vectp->avh_lo_pri) {
3730Sstevel@tonic-gate 				vectp->avh_lo_pri = (ushort_t)pri_level;
3740Sstevel@tonic-gate 			}
3755084Sjohnlev 			/*
3765084Sjohnlev 			 * To prevent calling service routine before args
3775084Sjohnlev 			 * and ticksp are ready fill in vector last.
3785084Sjohnlev 			 */
3790Sstevel@tonic-gate 			p->av_vector = f;
3800Sstevel@tonic-gate 			mutex_exit(&av_lock);
3815084Sjohnlev 			kmem_free(mem, sizeof (struct autovec));
382999Slq150181 			return;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 	/* insert new intpt at beginning of chain */
3860Sstevel@tonic-gate 	mem->av_link = vectp->avh_link;
3870Sstevel@tonic-gate 	vectp->avh_link = mem;
3880Sstevel@tonic-gate 	if (pri_level > (int)vectp->avh_hi_pri) {
3890Sstevel@tonic-gate 		vectp->avh_hi_pri = (ushort_t)pri_level;
3900Sstevel@tonic-gate 	}
3910Sstevel@tonic-gate 	if (pri_level < (int)vectp->avh_lo_pri) {
3920Sstevel@tonic-gate 		vectp->avh_lo_pri = (ushort_t)pri_level;
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 	mutex_exit(&av_lock);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate 
397999Slq150181 static int
398999Slq150181 av_rem_softintr(void *intr_id, int lvl, avfunc xxintr, boolean_t rem_softinfo)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
4010Sstevel@tonic-gate 	int slvl;
402999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
403999Slq150181 	av_softinfo_t *infop = (av_softinfo_t *)hdlp->ih_pending;
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	if (xxintr == NULL)
4060Sstevel@tonic-gate 		return (0);
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	if ((slvl = slvltovect(lvl)) != -1) {
4090Sstevel@tonic-gate 		rem_avintr(intr_id, lvl, xxintr, slvl);
4100Sstevel@tonic-gate 		return (1);
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	if (lvl <= 0 && lvl >= LOCK_LEVEL) {
4140Sstevel@tonic-gate 		return (0);
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 	vecp = &softvect[lvl];
4170Sstevel@tonic-gate 	remove_av(intr_id, vecp, xxintr, lvl, 0);
4180Sstevel@tonic-gate 
419999Slq150181 	if (rem_softinfo) {
420999Slq150181 		kmem_free(infop, sizeof (av_softinfo_t));
421999Slq150181 		hdlp->ih_pending = NULL;
422999Slq150181 	}
423999Slq150181 
4240Sstevel@tonic-gate 	return (1);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
427999Slq150181 int
428999Slq150181 av_softint_movepri(void *intr_id, int old_lvl)
429999Slq150181 {
430999Slq150181 	int ret;
431999Slq150181 	ddi_softint_hdl_impl_t	*hdlp = (ddi_softint_hdl_impl_t *)intr_id;
432999Slq150181 
433999Slq150181 	ret = add_avsoftintr(intr_id, hdlp->ih_pri, hdlp->ih_cb_func,
434999Slq150181 	    DEVI(hdlp->ih_dip)->devi_name, hdlp->ih_cb_arg1, hdlp->ih_cb_arg2);
435999Slq150181 
436999Slq150181 	if (ret) {
437999Slq150181 		(void) av_rem_softintr(intr_id, old_lvl, hdlp->ih_cb_func,
438999Slq150181 		    B_FALSE);
439999Slq150181 	}
440999Slq150181 
441999Slq150181 	return (ret);
442999Slq150181 }
443999Slq150181 
444999Slq150181 /*
445999Slq150181  * Remove a driver from the autovector list.
446999Slq150181  */
447999Slq150181 int
448999Slq150181 rem_avsoftintr(void *intr_id, int lvl, avfunc xxintr)
449999Slq150181 {
450999Slq150181 	return (av_rem_softintr(intr_id, lvl, xxintr, B_TRUE));
451999Slq150181 }
452999Slq150181 
4530Sstevel@tonic-gate void
4540Sstevel@tonic-gate rem_avintr(void *intr_id, int lvl, avfunc xxintr, int vect)
4550Sstevel@tonic-gate {
4560Sstevel@tonic-gate 	struct av_head *vecp = (struct av_head *)0;
4570Sstevel@tonic-gate 	avfunc f;
4580Sstevel@tonic-gate 	int s, vectindex;			/* save old spl value */
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if ((f = xxintr) == NULL)
4610Sstevel@tonic-gate 		return;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	vectindex = vect % MAX_VECT;
4640Sstevel@tonic-gate 	vecp = &autovect[vectindex];
4650Sstevel@tonic-gate 	remove_av(intr_id, vecp, f, lvl, vect);
4660Sstevel@tonic-gate 	s = splhi();
4670Sstevel@tonic-gate 	mutex_enter(&av_lock);
4680Sstevel@tonic-gate 	(*delspl)(vect, lvl, vecp->avh_lo_pri, vecp->avh_hi_pri);
4690Sstevel@tonic-gate 	mutex_exit(&av_lock);
4700Sstevel@tonic-gate 	splx(s);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate 
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate /*
4750Sstevel@tonic-gate  * After having made a change to an autovector list, wait until we have
4760Sstevel@tonic-gate  * seen each cpu not executing an interrupt at that level--so we know our
4770Sstevel@tonic-gate  * change has taken effect completely (no old state in registers, etc).
4780Sstevel@tonic-gate  */
479*9717SColin.Yi@Sun.COM static void
4800Sstevel@tonic-gate wait_till_seen(int ipl)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	int cpu_in_chain, cix;
4830Sstevel@tonic-gate 	struct cpu *cpup;
4840Sstevel@tonic-gate 	cpuset_t cpus_to_check;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	CPUSET_ALL(cpus_to_check);
4870Sstevel@tonic-gate 	do {
4880Sstevel@tonic-gate 		cpu_in_chain = 0;
4890Sstevel@tonic-gate 		for (cix = 0; cix < NCPU; cix++) {
4900Sstevel@tonic-gate 			cpup = cpu[cix];
4910Sstevel@tonic-gate 			if (cpup != NULL && CPU_IN_SET(cpus_to_check, cix)) {
492*9717SColin.Yi@Sun.COM 				if (INTR_ACTIVE(cpup, ipl)) {
4930Sstevel@tonic-gate 					cpu_in_chain = 1;
4940Sstevel@tonic-gate 				} else {
4950Sstevel@tonic-gate 					CPUSET_DEL(cpus_to_check, cix);
4960Sstevel@tonic-gate 				}
4970Sstevel@tonic-gate 			}
4980Sstevel@tonic-gate 		}
4990Sstevel@tonic-gate 	} while (cpu_in_chain);
5000Sstevel@tonic-gate }
5010Sstevel@tonic-gate 
5025084Sjohnlev static uint64_t dummy_tick;
5035084Sjohnlev 
5040Sstevel@tonic-gate /* remove an interrupt vector from the chain */
5050Sstevel@tonic-gate static void
5060Sstevel@tonic-gate remove_av(void *intr_id, struct av_head *vectp, avfunc f, int pri_level,
5070Sstevel@tonic-gate 	int vect)
5080Sstevel@tonic-gate {
5095084Sjohnlev 	struct autovec *p, *target;
5100Sstevel@tonic-gate 	int	lo_pri, hi_pri;
5110Sstevel@tonic-gate 	int	ipl;
5120Sstevel@tonic-gate 	/*
5130Sstevel@tonic-gate 	 * Protect rewrites of the list
5140Sstevel@tonic-gate 	 */
5150Sstevel@tonic-gate 	target = NULL;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	mutex_enter(&av_lock);
5180Sstevel@tonic-gate 	ipl = pri_level;
5190Sstevel@tonic-gate 	lo_pri = MAXIPL;
5200Sstevel@tonic-gate 	hi_pri = 0;
5215084Sjohnlev 	for (p = vectp->avh_link; p; p = p->av_link) {
5220Sstevel@tonic-gate 		if ((p->av_vector == f) && (p->av_intr_id == intr_id)) {
5230Sstevel@tonic-gate 			/* found the handler */
5240Sstevel@tonic-gate 			target = p;
5250Sstevel@tonic-gate 			continue;
5260Sstevel@tonic-gate 		}
5275084Sjohnlev 		if (p->av_vector != NULL) {
5285084Sjohnlev 			if (p->av_prilevel > hi_pri)
5295084Sjohnlev 				hi_pri = p->av_prilevel;
5305084Sjohnlev 			if (p->av_prilevel < lo_pri)
5315084Sjohnlev 				lo_pri = p->av_prilevel;
5325084Sjohnlev 		}
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 	if (ipl < hi_pri)
5350Sstevel@tonic-gate 		ipl = hi_pri;
5360Sstevel@tonic-gate 	if (target == NULL) {	/* not found */
5370Sstevel@tonic-gate 		printf("Couldn't remove function %p at %d, %d\n",
5384652Scwb 		    (void *)f, vect, pri_level);
5390Sstevel@tonic-gate 		mutex_exit(&av_lock);
5400Sstevel@tonic-gate 		return;
5410Sstevel@tonic-gate 	}
5420Sstevel@tonic-gate 
5435084Sjohnlev 	/*
5445084Sjohnlev 	 * This drops the handler from the chain, it can no longer be called.
5455084Sjohnlev 	 * However, there is no guarantee that the handler is not currently
5465084Sjohnlev 	 * still executing.
5475084Sjohnlev 	 */
5480Sstevel@tonic-gate 	target->av_vector = NULL;
5495084Sjohnlev 	/*
5505084Sjohnlev 	 * There is a race where we could be just about to pick up the ticksp
5515084Sjohnlev 	 * pointer to increment it after returning from the service routine
5525084Sjohnlev 	 * in av_dispatch_autovect.  Rather than NULL it out let's just point
5535084Sjohnlev 	 * it off to something safe so that any final tick update attempt
5545084Sjohnlev 	 * won't fault.
5555084Sjohnlev 	 */
5565084Sjohnlev 	target->av_ticksp = &dummy_tick;
5570Sstevel@tonic-gate 	wait_till_seen(ipl);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 	if (lo_pri > hi_pri) {	/* the chain is now empty */
5600Sstevel@tonic-gate 		/* Leave the unused entries here for probable future use */
5610Sstevel@tonic-gate 		vectp->avh_lo_pri = MAXIPL;
5620Sstevel@tonic-gate 		vectp->avh_hi_pri = 0;
5630Sstevel@tonic-gate 	} else {
5640Sstevel@tonic-gate 		if ((int)vectp->avh_lo_pri < lo_pri)
5650Sstevel@tonic-gate 			vectp->avh_lo_pri = (ushort_t)lo_pri;
5660Sstevel@tonic-gate 		if ((int)vectp->avh_hi_pri > hi_pri)
5670Sstevel@tonic-gate 			vectp->avh_hi_pri = (ushort_t)hi_pri;
5680Sstevel@tonic-gate 	}
5690Sstevel@tonic-gate 	mutex_exit(&av_lock);
5700Sstevel@tonic-gate 	wait_till_seen(ipl);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate 
5730Sstevel@tonic-gate /*
5744652Scwb  * kmdb uses siron (and thus setsoftint) while the world is stopped in order to
5754652Scwb  * inform its driver component that there's work to be done.  We need to keep
5764652Scwb  * DTrace from instrumenting kmdb's siron and setsoftint.  We duplicate siron,
5774652Scwb  * giving kmdb's version a kdi prefix to keep DTrace at bay.   We also
5784652Scwb  * provide a version of the various setsoftint functions available for kmdb to
5794652Scwb  * use using a kdi_ prefix while the main *setsoftint() functionality is
5804652Scwb  * implemented as a wrapper.  This allows tracing, while still providing a
5814652Scwb  * way for kmdb to sneak in unmolested.
5824652Scwb  */
5834652Scwb void
5844652Scwb kdi_siron(void)
5854652Scwb {
5864652Scwb 	(*kdisetsoftint)(1, softlevel1_hdl.ih_pending);
5874652Scwb }
5884652Scwb 
5894652Scwb /*
5900Sstevel@tonic-gate  * Trigger a soft interrupt.
5910Sstevel@tonic-gate  */
5920Sstevel@tonic-gate void
5930Sstevel@tonic-gate siron(void)
5940Sstevel@tonic-gate {
5955107Seota 	/* Level 1 software interrupt */
596999Slq150181 	(*setsoftint)(1, softlevel1_hdl.ih_pending);
5970Sstevel@tonic-gate }
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6005107Seota  * Trigger software interrupts dedicated to ddi timer.
6015107Seota  */
6025107Seota void
6035107Seota sir_on(int level)
6045107Seota {
6055107Seota 	ASSERT(level >= DDI_IPL_1 && level <= DDI_IPL_10);
6065107Seota 	(*setsoftint)(level, softlevel_hdl[level-1].ih_pending);
6075107Seota }
6085107Seota 
6095107Seota /*
6105076Smishra  * The handler which is executed on the target CPU.
6115076Smishra  */
6125076Smishra /*ARGSUSED*/
6135076Smishra static int
6145076Smishra siron_poke_intr(xc_arg_t a1, xc_arg_t a2, xc_arg_t a3)
6155076Smishra {
6165076Smishra 	siron();
6175076Smishra 	return (0);
6185076Smishra }
6195076Smishra 
6205076Smishra /*
6215076Smishra  * May get called from softcall to poke CPUs.
6225076Smishra  */
6235076Smishra void
6245076Smishra siron_poke_cpu(cpuset_t poke)
6255076Smishra {
6265076Smishra 	int cpuid = CPU->cpu_id;
6275076Smishra 
6285076Smishra 	/*
6295076Smishra 	 * If we are poking to ourself then we can simply
6305076Smishra 	 * generate level1 using siron()
6315076Smishra 	 */
6325076Smishra 	if (CPU_IN_SET(poke, cpuid)) {
6335076Smishra 		siron();
6345076Smishra 		CPUSET_DEL(poke, cpuid);
6355076Smishra 		if (CPUSET_ISNULL(poke))
6365076Smishra 			return;
6375076Smishra 	}
6385076Smishra 
6399489SJoe.Bonasera@sun.com 	xc_call(0, 0, 0, CPUSET2BV(poke), (xc_func_t)siron_poke_intr);
6405076Smishra }
6415076Smishra 
6425076Smishra /*
6430Sstevel@tonic-gate  * Walk the autovector table for this vector, invoking each
6440Sstevel@tonic-gate  * interrupt handler as we go.
6450Sstevel@tonic-gate  */
646916Sschwartz 
647916Sschwartz extern uint64_t intr_get_time(void);
648916Sschwartz 
6490Sstevel@tonic-gate void
6500Sstevel@tonic-gate av_dispatch_autovect(uint_t vec)
6510Sstevel@tonic-gate {
6520Sstevel@tonic-gate 	struct autovec *av;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	while ((av = autovect[vec].avh_link) != NULL) {
6570Sstevel@tonic-gate 		uint_t numcalled = 0;
6580Sstevel@tonic-gate 		uint_t claimed = 0;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		for (; av; av = av->av_link) {
6610Sstevel@tonic-gate 			uint_t r;
6620Sstevel@tonic-gate 			uint_t (*intr)() = av->av_vector;
6630Sstevel@tonic-gate 			caddr_t arg1 = av->av_intarg1;
6640Sstevel@tonic-gate 			caddr_t arg2 = av->av_intarg2;
6650Sstevel@tonic-gate 			dev_info_t *dip = av->av_dip;
6660Sstevel@tonic-gate 
6675084Sjohnlev 			/*
6685084Sjohnlev 			 * We must walk the entire chain.  Removed handlers
6695084Sjohnlev 			 * may be anywhere in the chain.
6705084Sjohnlev 			 */
6710Sstevel@tonic-gate 			if (intr == NULL)
6725084Sjohnlev 				continue;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__start, dev_info_t *, dip,
6750Sstevel@tonic-gate 			    void *, intr, caddr_t, arg1, caddr_t, arg2);
6760Sstevel@tonic-gate 			r = (*intr)(arg1, arg2);
6770Sstevel@tonic-gate 			DTRACE_PROBE4(interrupt__complete, dev_info_t *, dip,
6780Sstevel@tonic-gate 			    void *, intr, caddr_t, arg1, uint_t, r);
6793446Smrj 			numcalled++;
6800Sstevel@tonic-gate 			claimed |= r;
681916Sschwartz 			if (av->av_ticksp && av->av_prilevel <= LOCK_LEVEL)
682916Sschwartz 				atomic_add_64(av->av_ticksp, intr_get_time());
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * If there's only one interrupt handler in the chain,
6870Sstevel@tonic-gate 		 * or if no-one claimed the interrupt at all give up now.
6880Sstevel@tonic-gate 		 */
6890Sstevel@tonic-gate 		if (numcalled == 1 || claimed == 0)
6900Sstevel@tonic-gate 			break;
6910Sstevel@tonic-gate 	}
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate /*
6950Sstevel@tonic-gate  * Call every soft interrupt handler we can find at this level once.
6960Sstevel@tonic-gate  */
6970Sstevel@tonic-gate void
6980Sstevel@tonic-gate av_dispatch_softvect(uint_t pil)
6990Sstevel@tonic-gate {
7000Sstevel@tonic-gate 	struct autovec *av;
7010Sstevel@tonic-gate 	ddi_softint_hdl_impl_t	*hdlp;
7020Sstevel@tonic-gate 	uint_t (*intr)();
7030Sstevel@tonic-gate 	caddr_t arg1;
7040Sstevel@tonic-gate 	caddr_t arg2;
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
7070Sstevel@tonic-gate 	ASSERT(pil >= 0 && pil <= PIL_MAX);
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 	for (av = softvect[pil].avh_link; av; av = av->av_link) {
7105084Sjohnlev 		/*
7115084Sjohnlev 		 * We must walk the entire chain.  Removed handlers
7125084Sjohnlev 		 * may be anywhere in the chain.
7135084Sjohnlev 		 */
7140Sstevel@tonic-gate 		if ((intr = av->av_vector) == NULL)
7155084Sjohnlev 			continue;
7160Sstevel@tonic-gate 		arg1 = av->av_intarg1;
7170Sstevel@tonic-gate 		arg2 = av->av_intarg2;
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		hdlp = (ddi_softint_hdl_impl_t *)av->av_intr_id;
7200Sstevel@tonic-gate 		ASSERT(hdlp);
7210Sstevel@tonic-gate 
722999Slq150181 		/*
723999Slq150181 		 * Each cpu has its own pending bit in hdlp->ih_pending,
724999Slq150181 		 * here av_check/clear_softint_pending is just checking
725999Slq150181 		 * and clearing the pending bit for the current cpu, who
726999Slq150181 		 * has just triggered a softint.
727999Slq150181 		 */
728999Slq150181 		if (av_check_softint_pending(hdlp->ih_pending, B_FALSE)) {
729999Slq150181 			av_clear_softint_pending(hdlp->ih_pending);
7300Sstevel@tonic-gate 			(void) (*intr)(arg1, arg2);
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate }
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate struct regs;
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate /*
7380Sstevel@tonic-gate  * Call every NMI handler we know of once.
7390Sstevel@tonic-gate  */
7400Sstevel@tonic-gate void
7410Sstevel@tonic-gate av_dispatch_nmivect(struct regs *rp)
7420Sstevel@tonic-gate {
7430Sstevel@tonic-gate 	struct autovec *av;
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 	ASSERT_STACK_ALIGNED();
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	for (av = nmivect; av; av = av->av_link)
7480Sstevel@tonic-gate 		(void) (av->av_vector)(av->av_intarg1, rp);
7490Sstevel@tonic-gate }
750