xref: /freebsd/sys/kern/kern_intr.c (revision 982d11f836278f1e95ae1ae398aa4d1d07a19006)
19454b2d8SWarner Losh /*-
2425f9fdaSStefan Eßer  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3425f9fdaSStefan Eßer  * All rights reserved.
4425f9fdaSStefan Eßer  *
5425f9fdaSStefan Eßer  * Redistribution and use in source and binary forms, with or without
6425f9fdaSStefan Eßer  * modification, are permitted provided that the following conditions
7425f9fdaSStefan Eßer  * are met:
8425f9fdaSStefan Eßer  * 1. Redistributions of source code must retain the above copyright
9425f9fdaSStefan Eßer  *    notice unmodified, this list of conditions, and the following
10425f9fdaSStefan Eßer  *    disclaimer.
11425f9fdaSStefan Eßer  * 2. Redistributions in binary form must reproduce the above copyright
12425f9fdaSStefan Eßer  *    notice, this list of conditions and the following disclaimer in the
13425f9fdaSStefan Eßer  *    documentation and/or other materials provided with the distribution.
14425f9fdaSStefan Eßer  *
15425f9fdaSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16425f9fdaSStefan Eßer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17425f9fdaSStefan Eßer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18425f9fdaSStefan Eßer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19425f9fdaSStefan Eßer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20425f9fdaSStefan Eßer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21425f9fdaSStefan Eßer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22425f9fdaSStefan Eßer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23425f9fdaSStefan Eßer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24425f9fdaSStefan Eßer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25425f9fdaSStefan Eßer  */
26425f9fdaSStefan Eßer 
27677b542eSDavid E. O'Brien #include <sys/cdefs.h>
28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
293900ddb2SDoug Rabson 
308b201c42SJohn Baldwin #include "opt_ddb.h"
318b201c42SJohn Baldwin 
321c5bb3eaSPeter Wemm #include <sys/param.h>
339a94c9c5SJohn Baldwin #include <sys/bus.h>
34c11110eaSAlfred Perlstein #include <sys/conf.h>
359a94c9c5SJohn Baldwin #include <sys/rtprio.h>
36425f9fdaSStefan Eßer #include <sys/systm.h>
3768352337SDoug Rabson #include <sys/interrupt.h>
381931cf94SJohn Baldwin #include <sys/kernel.h>
391931cf94SJohn Baldwin #include <sys/kthread.h>
401931cf94SJohn Baldwin #include <sys/ktr.h>
4105b2c96fSBruce Evans #include <sys/limits.h>
42f34fa851SJohn Baldwin #include <sys/lock.h>
431931cf94SJohn Baldwin #include <sys/malloc.h>
4435e0e5b3SJohn Baldwin #include <sys/mutex.h>
451931cf94SJohn Baldwin #include <sys/proc.h>
463e5da754SJohn Baldwin #include <sys/random.h>
47b4151f71SJohn Baldwin #include <sys/resourcevar.h>
4863710c4dSJohn Baldwin #include <sys/sched.h>
49d279178dSThomas Moestl #include <sys/sysctl.h>
501931cf94SJohn Baldwin #include <sys/unistd.h>
511931cf94SJohn Baldwin #include <sys/vmmeter.h>
521931cf94SJohn Baldwin #include <machine/atomic.h>
531931cf94SJohn Baldwin #include <machine/cpu.h>
548088699fSJohn Baldwin #include <machine/md_var.h>
55b4151f71SJohn Baldwin #include <machine/stdarg.h>
568b201c42SJohn Baldwin #ifdef DDB
578b201c42SJohn Baldwin #include <ddb/ddb.h>
588b201c42SJohn Baldwin #include <ddb/db_sym.h>
598b201c42SJohn Baldwin #endif
60425f9fdaSStefan Eßer 
61e0f66ef8SJohn Baldwin /*
62e0f66ef8SJohn Baldwin  * Describe an interrupt thread.  There is one of these per interrupt event.
63e0f66ef8SJohn Baldwin  */
64e0f66ef8SJohn Baldwin struct intr_thread {
65e0f66ef8SJohn Baldwin 	struct intr_event *it_event;
66e0f66ef8SJohn Baldwin 	struct thread *it_thread;	/* Kernel thread. */
67e0f66ef8SJohn Baldwin 	int	it_flags;		/* (j) IT_* flags. */
68e0f66ef8SJohn Baldwin 	int	it_need;		/* Needs service. */
693e5da754SJohn Baldwin };
703e5da754SJohn Baldwin 
71e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */
72e0f66ef8SJohn Baldwin #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
73e0f66ef8SJohn Baldwin 
74e0f66ef8SJohn Baldwin struct	intr_entropy {
75e0f66ef8SJohn Baldwin 	struct	thread *td;
76e0f66ef8SJohn Baldwin 	uintptr_t event;
77e0f66ef8SJohn Baldwin };
78e0f66ef8SJohn Baldwin 
79e0f66ef8SJohn Baldwin struct	intr_event *clk_intr_event;
80e0f66ef8SJohn Baldwin struct	intr_event *tty_intr_event;
817b1fe905SBruce Evans void	*softclock_ih;
827b1fe905SBruce Evans void	*vm_ih;
831931cf94SJohn Baldwin 
84b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
85b4151f71SJohn Baldwin 
860ae62c18SNate Lawson static int intr_storm_threshold = 1000;
877870c3c6SJohn Baldwin TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
887870c3c6SJohn Baldwin SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
897870c3c6SJohn Baldwin     &intr_storm_threshold, 0,
907b1fe905SBruce Evans     "Number of consecutive interrupts before storm protection is enabled");
91e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list =
92e0f66ef8SJohn Baldwin     TAILQ_HEAD_INITIALIZER(event_list);
937b1fe905SBruce Evans 
94e0f66ef8SJohn Baldwin static void	intr_event_update(struct intr_event *ie);
95bafe5a31SPaolo Pisati #ifdef INTR_FILTER
96bafe5a31SPaolo Pisati static struct intr_thread *ithread_create(const char *name,
97bafe5a31SPaolo Pisati 			      struct intr_handler *ih);
98bafe5a31SPaolo Pisati #else
99e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name);
100bafe5a31SPaolo Pisati #endif
101e0f66ef8SJohn Baldwin static void	ithread_destroy(struct intr_thread *ithread);
102bafe5a31SPaolo Pisati static void	ithread_execute_handlers(struct proc *p,
103bafe5a31SPaolo Pisati 		    struct intr_event *ie);
104bafe5a31SPaolo Pisati #ifdef INTR_FILTER
105bafe5a31SPaolo Pisati static void	priv_ithread_execute_handler(struct proc *p,
106bafe5a31SPaolo Pisati 		    struct intr_handler *ih);
107bafe5a31SPaolo Pisati #endif
1087b1fe905SBruce Evans static void	ithread_loop(void *);
109e0f66ef8SJohn Baldwin static void	ithread_update(struct intr_thread *ithd);
1107b1fe905SBruce Evans static void	start_softintr(void *);
1117870c3c6SJohn Baldwin 
112bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */
113b4151f71SJohn Baldwin u_char
114e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags)
1159a94c9c5SJohn Baldwin {
116b4151f71SJohn Baldwin 	u_char pri;
1179a94c9c5SJohn Baldwin 
118b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
1195a280d9cSPeter Wemm 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
1209a94c9c5SJohn Baldwin 	switch (flags) {
121b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
1229a94c9c5SJohn Baldwin 		pri = PI_TTYLOW;
1239a94c9c5SJohn Baldwin 		break;
1249a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
1259a94c9c5SJohn Baldwin 		/*
1269a94c9c5SJohn Baldwin 		 * XXX We need to refine this.  BSD/OS distinguishes
1279a94c9c5SJohn Baldwin 		 * between tape and disk priorities.
1289a94c9c5SJohn Baldwin 		 */
1299a94c9c5SJohn Baldwin 		pri = PI_DISK;
1309a94c9c5SJohn Baldwin 		break;
1319a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
1329a94c9c5SJohn Baldwin 		pri = PI_NET;
1339a94c9c5SJohn Baldwin 		break;
1349a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
1359a94c9c5SJohn Baldwin 		pri = PI_DISK;          /* XXX or PI_CAM? */
1369a94c9c5SJohn Baldwin 		break;
1375a280d9cSPeter Wemm 	case INTR_TYPE_AV:		/* Audio/video */
1385a280d9cSPeter Wemm 		pri = PI_AV;
1395a280d9cSPeter Wemm 		break;
140b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
141b4151f71SJohn Baldwin 		pri = PI_REALTIME;
142b4151f71SJohn Baldwin 		break;
1439a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1449a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1459a94c9c5SJohn Baldwin 		break;
1469a94c9c5SJohn Baldwin 	default:
147b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
148e0f66ef8SJohn Baldwin 		panic("intr_priority: no interrupt type in flags");
1499a94c9c5SJohn Baldwin 	}
1509a94c9c5SJohn Baldwin 
1519a94c9c5SJohn Baldwin 	return pri;
1529a94c9c5SJohn Baldwin }
1539a94c9c5SJohn Baldwin 
154b4151f71SJohn Baldwin /*
155e0f66ef8SJohn Baldwin  * Update an ithread based on the associated intr_event.
156b4151f71SJohn Baldwin  */
157b4151f71SJohn Baldwin static void
158e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd)
159b4151f71SJohn Baldwin {
160e0f66ef8SJohn Baldwin 	struct intr_event *ie;
161b40ce416SJulian Elischer 	struct thread *td;
162e0f66ef8SJohn Baldwin 	u_char pri;
1638088699fSJohn Baldwin 
164e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
165e0f66ef8SJohn Baldwin 	td = ithd->it_thread;
166b4151f71SJohn Baldwin 
167e0f66ef8SJohn Baldwin 	/* Determine the overall priority of this event. */
168e0f66ef8SJohn Baldwin 	if (TAILQ_EMPTY(&ie->ie_handlers))
169e0f66ef8SJohn Baldwin 		pri = PRI_MAX_ITHD;
170e0f66ef8SJohn Baldwin 	else
171e0f66ef8SJohn Baldwin 		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
172e80fb434SRobert Drehmel 
173e0f66ef8SJohn Baldwin 	/* Update name and priority. */
174e0f66ef8SJohn Baldwin 	strlcpy(td->td_proc->p_comm, ie->ie_fullname,
175e0f66ef8SJohn Baldwin 	    sizeof(td->td_proc->p_comm));
176982d11f8SJeff Roberson 	thread_lock(td);
177e0f66ef8SJohn Baldwin 	sched_prio(td, pri);
178982d11f8SJeff Roberson 	thread_unlock(td);
179b4151f71SJohn Baldwin }
180e0f66ef8SJohn Baldwin 
181e0f66ef8SJohn Baldwin /*
182e0f66ef8SJohn Baldwin  * Regenerate the full name of an interrupt event and update its priority.
183e0f66ef8SJohn Baldwin  */
184e0f66ef8SJohn Baldwin static void
185e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie)
186e0f66ef8SJohn Baldwin {
187e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
188e0f66ef8SJohn Baldwin 	char *last;
189e0f66ef8SJohn Baldwin 	int missed, space;
190e0f66ef8SJohn Baldwin 
191e0f66ef8SJohn Baldwin 	/* Start off with no entropy and just the name of the event. */
192e0f66ef8SJohn Baldwin 	mtx_assert(&ie->ie_lock, MA_OWNED);
193e0f66ef8SJohn Baldwin 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
194e0f66ef8SJohn Baldwin 	ie->ie_flags &= ~IE_ENTROPY;
1950811d60aSJohn Baldwin 	missed = 0;
196e0f66ef8SJohn Baldwin 	space = 1;
197e0f66ef8SJohn Baldwin 
198e0f66ef8SJohn Baldwin 	/* Run through all the handlers updating values. */
199e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
200e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
201e0f66ef8SJohn Baldwin 		    sizeof(ie->ie_fullname)) {
202e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " ");
203e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, ih->ih_name);
204e0f66ef8SJohn Baldwin 			space = 0;
2050811d60aSJohn Baldwin 		} else
2060811d60aSJohn Baldwin 			missed++;
2070811d60aSJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY)
208e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ENTROPY;
2090811d60aSJohn Baldwin 	}
210e0f66ef8SJohn Baldwin 
211e0f66ef8SJohn Baldwin 	/*
212e0f66ef8SJohn Baldwin 	 * If the handler names were too long, add +'s to indicate missing
213e0f66ef8SJohn Baldwin 	 * names. If we run out of room and still have +'s to add, change
214e0f66ef8SJohn Baldwin 	 * the last character from a + to a *.
215e0f66ef8SJohn Baldwin 	 */
216e0f66ef8SJohn Baldwin 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
2170811d60aSJohn Baldwin 	while (missed-- > 0) {
218e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
219e0f66ef8SJohn Baldwin 			if (*last == '+') {
220e0f66ef8SJohn Baldwin 				*last = '*';
221e0f66ef8SJohn Baldwin 				break;
222b4151f71SJohn Baldwin 			} else
223e0f66ef8SJohn Baldwin 				*last = '+';
224e0f66ef8SJohn Baldwin 		} else if (space) {
225e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " +");
226e0f66ef8SJohn Baldwin 			space = 0;
227e0f66ef8SJohn Baldwin 		} else
228e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, "+");
229b4151f71SJohn Baldwin 	}
230e0f66ef8SJohn Baldwin 
231e0f66ef8SJohn Baldwin 	/*
232e0f66ef8SJohn Baldwin 	 * If this event has an ithread, update it's priority and
233e0f66ef8SJohn Baldwin 	 * name.
234e0f66ef8SJohn Baldwin 	 */
235e0f66ef8SJohn Baldwin 	if (ie->ie_thread != NULL)
236e0f66ef8SJohn Baldwin 		ithread_update(ie->ie_thread);
237e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
238b4151f71SJohn Baldwin }
239b4151f71SJohn Baldwin 
240bafe5a31SPaolo Pisati #ifndef INTR_FILTER
241b4151f71SJohn Baldwin int
242e0f66ef8SJohn Baldwin intr_event_create(struct intr_event **event, void *source, int flags,
243e0f66ef8SJohn Baldwin     void (*enable)(void *), const char *fmt, ...)
244b4151f71SJohn Baldwin {
245e0f66ef8SJohn Baldwin 	struct intr_event *ie;
246b4151f71SJohn Baldwin 	va_list ap;
247b4151f71SJohn Baldwin 
248e0f66ef8SJohn Baldwin 	/* The only valid flag during creation is IE_SOFT. */
249e0f66ef8SJohn Baldwin 	if ((flags & ~IE_SOFT) != 0)
2503e5da754SJohn Baldwin 		return (EINVAL);
251e0f66ef8SJohn Baldwin 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
252e0f66ef8SJohn Baldwin 	ie->ie_source = source;
253e0f66ef8SJohn Baldwin 	ie->ie_enable = enable;
254e0f66ef8SJohn Baldwin 	ie->ie_flags = flags;
255e0f66ef8SJohn Baldwin 	TAILQ_INIT(&ie->ie_handlers);
256e0f66ef8SJohn Baldwin 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
257b4151f71SJohn Baldwin 
258b4151f71SJohn Baldwin 	va_start(ap, fmt);
259e0f66ef8SJohn Baldwin 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
260b4151f71SJohn Baldwin 	va_end(ap);
261e0f66ef8SJohn Baldwin 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
262e0f66ef8SJohn Baldwin 	mtx_pool_lock(mtxpool_sleep, &event_list);
263e0f66ef8SJohn Baldwin 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
264e0f66ef8SJohn Baldwin 	mtx_pool_unlock(mtxpool_sleep, &event_list);
265e0f66ef8SJohn Baldwin 	if (event != NULL)
266e0f66ef8SJohn Baldwin 		*event = ie;
267e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
268b4151f71SJohn Baldwin 	return (0);
269b4151f71SJohn Baldwin }
270bafe5a31SPaolo Pisati #else
271bafe5a31SPaolo Pisati int
272bafe5a31SPaolo Pisati intr_event_create(struct intr_event **event, void *source, int flags,
273bafe5a31SPaolo Pisati     void (*enable)(void *), void (*eoi)(void *), void (*disab)(void *),
274bafe5a31SPaolo Pisati     const char *fmt, ...)
275bafe5a31SPaolo Pisati {
276bafe5a31SPaolo Pisati 	struct intr_event *ie;
277bafe5a31SPaolo Pisati 	va_list ap;
278bafe5a31SPaolo Pisati 
279bafe5a31SPaolo Pisati 	/* The only valid flag during creation is IE_SOFT. */
280bafe5a31SPaolo Pisati 	if ((flags & ~IE_SOFT) != 0)
281bafe5a31SPaolo Pisati 		return (EINVAL);
282bafe5a31SPaolo Pisati 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
283bafe5a31SPaolo Pisati 	ie->ie_source = source;
284bafe5a31SPaolo Pisati 	ie->ie_enable = enable;
285bafe5a31SPaolo Pisati 	ie->ie_eoi = eoi;
286bafe5a31SPaolo Pisati 	ie->ie_disab = disab;
287bafe5a31SPaolo Pisati 	ie->ie_flags = flags;
288bafe5a31SPaolo Pisati 	TAILQ_INIT(&ie->ie_handlers);
289bafe5a31SPaolo Pisati 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
290bafe5a31SPaolo Pisati 
291bafe5a31SPaolo Pisati 	va_start(ap, fmt);
292bafe5a31SPaolo Pisati 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
293bafe5a31SPaolo Pisati 	va_end(ap);
294bafe5a31SPaolo Pisati 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
295bafe5a31SPaolo Pisati 	mtx_pool_lock(mtxpool_sleep, &event_list);
296bafe5a31SPaolo Pisati 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
297bafe5a31SPaolo Pisati 	mtx_pool_unlock(mtxpool_sleep, &event_list);
298bafe5a31SPaolo Pisati 	if (event != NULL)
299bafe5a31SPaolo Pisati 		*event = ie;
300bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
301bafe5a31SPaolo Pisati 	return (0);
302bafe5a31SPaolo Pisati }
303bafe5a31SPaolo Pisati #endif
304b4151f71SJohn Baldwin 
305b4151f71SJohn Baldwin int
306e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie)
307b4151f71SJohn Baldwin {
308b4151f71SJohn Baldwin 
309e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
310e0f66ef8SJohn Baldwin 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
311e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
312e0f66ef8SJohn Baldwin 		return (EBUSY);
3134d29cb2dSJohn Baldwin 	}
314e0f66ef8SJohn Baldwin 	mtx_pool_lock(mtxpool_sleep, &event_list);
315e0f66ef8SJohn Baldwin 	TAILQ_REMOVE(&event_list, ie, ie_list);
316e0f66ef8SJohn Baldwin 	mtx_pool_unlock(mtxpool_sleep, &event_list);
3179477358dSJohn Baldwin #ifndef notyet
3189477358dSJohn Baldwin 	if (ie->ie_thread != NULL) {
3199477358dSJohn Baldwin 		ithread_destroy(ie->ie_thread);
3209477358dSJohn Baldwin 		ie->ie_thread = NULL;
3219477358dSJohn Baldwin 	}
3229477358dSJohn Baldwin #endif
323e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
324e0f66ef8SJohn Baldwin 	mtx_destroy(&ie->ie_lock);
325e0f66ef8SJohn Baldwin 	free(ie, M_ITHREAD);
326e0f66ef8SJohn Baldwin 	return (0);
327e0f66ef8SJohn Baldwin }
328e0f66ef8SJohn Baldwin 
329bafe5a31SPaolo Pisati #ifndef INTR_FILTER
330e0f66ef8SJohn Baldwin static struct intr_thread *
331e0f66ef8SJohn Baldwin ithread_create(const char *name)
332e0f66ef8SJohn Baldwin {
333e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
334e0f66ef8SJohn Baldwin 	struct thread *td;
335e0f66ef8SJohn Baldwin 	struct proc *p;
336e0f66ef8SJohn Baldwin 	int error;
337e0f66ef8SJohn Baldwin 
338e0f66ef8SJohn Baldwin 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
339e0f66ef8SJohn Baldwin 
340e0f66ef8SJohn Baldwin 	error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
341e0f66ef8SJohn Baldwin 	    0, "%s", name);
342e0f66ef8SJohn Baldwin 	if (error)
343e0f66ef8SJohn Baldwin 		panic("kthread_create() failed with %d", error);
344e0f66ef8SJohn Baldwin 	td = FIRST_THREAD_IN_PROC(p);	/* XXXKSE */
345982d11f8SJeff Roberson 	thread_lock(td);
346ad1e7d28SJulian Elischer 	sched_class(td, PRI_ITHD);
347e0f66ef8SJohn Baldwin 	TD_SET_IWAIT(td);
348982d11f8SJeff Roberson 	thread_unlock(td);
349e0f66ef8SJohn Baldwin 	td->td_pflags |= TDP_ITHREAD;
350e0f66ef8SJohn Baldwin 	ithd->it_thread = td;
351e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
352e0f66ef8SJohn Baldwin 	return (ithd);
353e0f66ef8SJohn Baldwin }
354bafe5a31SPaolo Pisati #else
355bafe5a31SPaolo Pisati static struct intr_thread *
356bafe5a31SPaolo Pisati ithread_create(const char *name, struct intr_handler *ih)
357bafe5a31SPaolo Pisati {
358bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
359bafe5a31SPaolo Pisati 	struct thread *td;
360bafe5a31SPaolo Pisati 	struct proc *p;
361bafe5a31SPaolo Pisati 	int error;
362bafe5a31SPaolo Pisati 
363bafe5a31SPaolo Pisati 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
364bafe5a31SPaolo Pisati 
365bafe5a31SPaolo Pisati 	error = kthread_create(ithread_loop, ih, &p, RFSTOPPED | RFHIGHPID,
366bafe5a31SPaolo Pisati 	    0, "%s", name);
367bafe5a31SPaolo Pisati 	if (error)
368bafe5a31SPaolo Pisati 		panic("kthread_create() failed with %d", error);
369bafe5a31SPaolo Pisati 	td = FIRST_THREAD_IN_PROC(p);	/* XXXKSE */
370982d11f8SJeff Roberson 	thread_lock(td);
371bafe5a31SPaolo Pisati 	sched_class(td, PRI_ITHD);
372bafe5a31SPaolo Pisati 	TD_SET_IWAIT(td);
373982d11f8SJeff Roberson 	thread_unlock(td);
374bafe5a31SPaolo Pisati 	td->td_pflags |= TDP_ITHREAD;
375bafe5a31SPaolo Pisati 	ithd->it_thread = td;
376bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
377bafe5a31SPaolo Pisati 	return (ithd);
378bafe5a31SPaolo Pisati }
379bafe5a31SPaolo Pisati #endif
380e0f66ef8SJohn Baldwin 
381e0f66ef8SJohn Baldwin static void
382e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread)
383e0f66ef8SJohn Baldwin {
384e0f66ef8SJohn Baldwin 	struct thread *td;
385e0f66ef8SJohn Baldwin 
386bb141be1SScott Long 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
387e0f66ef8SJohn Baldwin 	td = ithread->it_thread;
388982d11f8SJeff Roberson 	thread_lock(td);
389e0f66ef8SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
39071fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
39171fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
392f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
393b4151f71SJohn Baldwin 	}
394982d11f8SJeff Roberson 	thread_unlock(td);
395b4151f71SJohn Baldwin }
396b4151f71SJohn Baldwin 
397bafe5a31SPaolo Pisati #ifndef INTR_FILTER
398b4151f71SJohn Baldwin int
399e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name,
400ef544f63SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
401ef544f63SPaolo Pisati     enum intr_type flags, void **cookiep)
402b4151f71SJohn Baldwin {
403e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *temp_ih;
404e0f66ef8SJohn Baldwin 	struct intr_thread *it;
405b4151f71SJohn Baldwin 
406ef544f63SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
407b4151f71SJohn Baldwin 		return (EINVAL);
408b4151f71SJohn Baldwin 
409e0f66ef8SJohn Baldwin 	/* Allocate and populate an interrupt handler structure. */
410e0f66ef8SJohn Baldwin 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
411ef544f63SPaolo Pisati 	ih->ih_filter = filter;
412b4151f71SJohn Baldwin 	ih->ih_handler = handler;
413b4151f71SJohn Baldwin 	ih->ih_argument = arg;
414b4151f71SJohn Baldwin 	ih->ih_name = name;
415e0f66ef8SJohn Baldwin 	ih->ih_event = ie;
416b4151f71SJohn Baldwin 	ih->ih_pri = pri;
417ef544f63SPaolo Pisati 	if (flags & INTR_EXCL)
418b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
419b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
420b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
421b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
422b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
423b4151f71SJohn Baldwin 
424e0f66ef8SJohn Baldwin 	/* We can only have one exclusive handler in a event. */
425e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
426e0f66ef8SJohn Baldwin 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
427e0f66ef8SJohn Baldwin 		if ((flags & INTR_EXCL) ||
428e0f66ef8SJohn Baldwin 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
429e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
430b4151f71SJohn Baldwin 			free(ih, M_ITHREAD);
431b4151f71SJohn Baldwin 			return (EINVAL);
432b4151f71SJohn Baldwin 		}
433e0f66ef8SJohn Baldwin 	}
434e0f66ef8SJohn Baldwin 
435e0f66ef8SJohn Baldwin 	/* Add the new handler to the event in priority order. */
436e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
437e0f66ef8SJohn Baldwin 		if (temp_ih->ih_pri > ih->ih_pri)
438e0f66ef8SJohn Baldwin 			break;
439e0f66ef8SJohn Baldwin 	}
440e0f66ef8SJohn Baldwin 	if (temp_ih == NULL)
441e0f66ef8SJohn Baldwin 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
442e0f66ef8SJohn Baldwin 	else
443e0f66ef8SJohn Baldwin 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
444e0f66ef8SJohn Baldwin 	intr_event_update(ie);
445e0f66ef8SJohn Baldwin 
446e0f66ef8SJohn Baldwin 	/* Create a thread if we need one. */
447ef544f63SPaolo Pisati 	while (ie->ie_thread == NULL && handler != NULL) {
448e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD)
4490f180a7cSJohn Baldwin 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
450e0f66ef8SJohn Baldwin 		else {
451e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ADDING_THREAD;
452e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
453e0f66ef8SJohn Baldwin 			it = ithread_create("intr: newborn");
454e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
455e0f66ef8SJohn Baldwin 			ie->ie_flags &= ~IE_ADDING_THREAD;
456e0f66ef8SJohn Baldwin 			ie->ie_thread = it;
457e0f66ef8SJohn Baldwin 			it->it_event = ie;
458e0f66ef8SJohn Baldwin 			ithread_update(it);
459e0f66ef8SJohn Baldwin 			wakeup(ie);
460e0f66ef8SJohn Baldwin 		}
461e0f66ef8SJohn Baldwin 	}
462e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
463e0f66ef8SJohn Baldwin 	    ie->ie_name);
464e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
465e0f66ef8SJohn Baldwin 
466e0f66ef8SJohn Baldwin 	if (cookiep != NULL)
467e0f66ef8SJohn Baldwin 		*cookiep = ih;
468e0f66ef8SJohn Baldwin 	return (0);
469e0f66ef8SJohn Baldwin }
470bafe5a31SPaolo Pisati #else
471bafe5a31SPaolo Pisati int
472bafe5a31SPaolo Pisati intr_event_add_handler(struct intr_event *ie, const char *name,
473bafe5a31SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
474bafe5a31SPaolo Pisati     enum intr_type flags, void **cookiep)
475bafe5a31SPaolo Pisati {
476bafe5a31SPaolo Pisati 	struct intr_handler *ih, *temp_ih;
477bafe5a31SPaolo Pisati 	struct intr_thread *it;
478bafe5a31SPaolo Pisati 
479bafe5a31SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
480bafe5a31SPaolo Pisati 		return (EINVAL);
481bafe5a31SPaolo Pisati 
482bafe5a31SPaolo Pisati 	/* Allocate and populate an interrupt handler structure. */
483bafe5a31SPaolo Pisati 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
484bafe5a31SPaolo Pisati 	ih->ih_filter = filter;
485bafe5a31SPaolo Pisati 	ih->ih_handler = handler;
486bafe5a31SPaolo Pisati 	ih->ih_argument = arg;
487bafe5a31SPaolo Pisati 	ih->ih_name = name;
488bafe5a31SPaolo Pisati 	ih->ih_event = ie;
489bafe5a31SPaolo Pisati 	ih->ih_pri = pri;
490bafe5a31SPaolo Pisati 	if (flags & INTR_EXCL)
491bafe5a31SPaolo Pisati 		ih->ih_flags = IH_EXCLUSIVE;
492bafe5a31SPaolo Pisati 	if (flags & INTR_MPSAFE)
493bafe5a31SPaolo Pisati 		ih->ih_flags |= IH_MPSAFE;
494bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
495bafe5a31SPaolo Pisati 		ih->ih_flags |= IH_ENTROPY;
496bafe5a31SPaolo Pisati 
497bafe5a31SPaolo Pisati 	/* We can only have one exclusive handler in a event. */
498bafe5a31SPaolo Pisati 	mtx_lock(&ie->ie_lock);
499bafe5a31SPaolo Pisati 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
500bafe5a31SPaolo Pisati 		if ((flags & INTR_EXCL) ||
501bafe5a31SPaolo Pisati 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
502bafe5a31SPaolo Pisati 			mtx_unlock(&ie->ie_lock);
503bafe5a31SPaolo Pisati 			free(ih, M_ITHREAD);
504bafe5a31SPaolo Pisati 			return (EINVAL);
505bafe5a31SPaolo Pisati 		}
506bafe5a31SPaolo Pisati 	}
507bafe5a31SPaolo Pisati 
508bafe5a31SPaolo Pisati 	/* Add the new handler to the event in priority order. */
509bafe5a31SPaolo Pisati 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
510bafe5a31SPaolo Pisati 		if (temp_ih->ih_pri > ih->ih_pri)
511bafe5a31SPaolo Pisati 			break;
512bafe5a31SPaolo Pisati 	}
513bafe5a31SPaolo Pisati 	if (temp_ih == NULL)
514bafe5a31SPaolo Pisati 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
515bafe5a31SPaolo Pisati 	else
516bafe5a31SPaolo Pisati 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
517bafe5a31SPaolo Pisati 	intr_event_update(ie);
518bafe5a31SPaolo Pisati 
519bafe5a31SPaolo Pisati 	/* For filtered handlers, create a private ithread to run on. */
520bafe5a31SPaolo Pisati 	if (filter != NULL && handler != NULL) {
521bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
522bafe5a31SPaolo Pisati 		it = ithread_create("intr: newborn", ih);
523bafe5a31SPaolo Pisati 		mtx_lock(&ie->ie_lock);
524bafe5a31SPaolo Pisati 		it->it_event = ie;
525bafe5a31SPaolo Pisati 		ih->ih_thread = it;
526bafe5a31SPaolo Pisati 		ithread_update(it); // XXX - do we really need this?!?!?
527bafe5a31SPaolo Pisati 	} else { /* Create the global per-event thread if we need one. */
528bafe5a31SPaolo Pisati 		while (ie->ie_thread == NULL && handler != NULL) {
529bafe5a31SPaolo Pisati 			if (ie->ie_flags & IE_ADDING_THREAD)
530bafe5a31SPaolo Pisati 				msleep(ie, &ie->ie_lock, 0, "ithread", 0);
531bafe5a31SPaolo Pisati 			else {
532bafe5a31SPaolo Pisati 				ie->ie_flags |= IE_ADDING_THREAD;
533bafe5a31SPaolo Pisati 				mtx_unlock(&ie->ie_lock);
534bafe5a31SPaolo Pisati 				it = ithread_create("intr: newborn", ih);
535bafe5a31SPaolo Pisati 				mtx_lock(&ie->ie_lock);
536bafe5a31SPaolo Pisati 				ie->ie_flags &= ~IE_ADDING_THREAD;
537bafe5a31SPaolo Pisati 				ie->ie_thread = it;
538bafe5a31SPaolo Pisati 				it->it_event = ie;
539bafe5a31SPaolo Pisati 				ithread_update(it);
540bafe5a31SPaolo Pisati 				wakeup(ie);
541bafe5a31SPaolo Pisati 			}
542bafe5a31SPaolo Pisati 		}
543bafe5a31SPaolo Pisati 	}
544bafe5a31SPaolo Pisati 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
545bafe5a31SPaolo Pisati 	    ie->ie_name);
546bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
547bafe5a31SPaolo Pisati 
548bafe5a31SPaolo Pisati 	if (cookiep != NULL)
549bafe5a31SPaolo Pisati 		*cookiep = ih;
550bafe5a31SPaolo Pisati 	return (0);
551bafe5a31SPaolo Pisati }
552bafe5a31SPaolo Pisati #endif
553b4151f71SJohn Baldwin 
554c3045318SJohn Baldwin /*
555c3045318SJohn Baldwin  * Return the ie_source field from the intr_event an intr_handler is
556c3045318SJohn Baldwin  * associated with.
557c3045318SJohn Baldwin  */
558c3045318SJohn Baldwin void *
559c3045318SJohn Baldwin intr_handler_source(void *cookie)
560c3045318SJohn Baldwin {
561c3045318SJohn Baldwin 	struct intr_handler *ih;
562c3045318SJohn Baldwin 	struct intr_event *ie;
563c3045318SJohn Baldwin 
564c3045318SJohn Baldwin 	ih = (struct intr_handler *)cookie;
565c3045318SJohn Baldwin 	if (ih == NULL)
566c3045318SJohn Baldwin 		return (NULL);
567c3045318SJohn Baldwin 	ie = ih->ih_event;
568c3045318SJohn Baldwin 	KASSERT(ie != NULL,
569c3045318SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
570c3045318SJohn Baldwin 	    ih->ih_name));
571c3045318SJohn Baldwin 	return (ie->ie_source);
572c3045318SJohn Baldwin }
573c3045318SJohn Baldwin 
574bafe5a31SPaolo Pisati #ifndef INTR_FILTER
575b4151f71SJohn Baldwin int
576e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie)
577b4151f71SJohn Baldwin {
578e0f66ef8SJohn Baldwin 	struct intr_handler *handler = (struct intr_handler *)cookie;
579e0f66ef8SJohn Baldwin 	struct intr_event *ie;
580b4151f71SJohn Baldwin #ifdef INVARIANTS
581e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
582e0f66ef8SJohn Baldwin #endif
583e0f66ef8SJohn Baldwin #ifdef notyet
584e0f66ef8SJohn Baldwin 	int dead;
585b4151f71SJohn Baldwin #endif
586b4151f71SJohn Baldwin 
5873e5da754SJohn Baldwin 	if (handler == NULL)
588b4151f71SJohn Baldwin 		return (EINVAL);
589e0f66ef8SJohn Baldwin 	ie = handler->ih_event;
590e0f66ef8SJohn Baldwin 	KASSERT(ie != NULL,
591e0f66ef8SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
5923e5da754SJohn Baldwin 	    handler->ih_name));
593e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
59491f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
595e0f66ef8SJohn Baldwin 	    ie->ie_name);
596b4151f71SJohn Baldwin #ifdef INVARIANTS
597e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
5983e5da754SJohn Baldwin 		if (ih == handler)
5993e5da754SJohn Baldwin 			goto ok;
600e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
601e0f66ef8SJohn Baldwin 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
602e0f66ef8SJohn Baldwin 	    ih->ih_name, ie->ie_name);
6033e5da754SJohn Baldwin ok:
604b4151f71SJohn Baldwin #endif
605de271f01SJohn Baldwin 	/*
606e0f66ef8SJohn Baldwin 	 * If there is no ithread, then just remove the handler and return.
607e0f66ef8SJohn Baldwin 	 * XXX: Note that an INTR_FAST handler might be running on another
608e0f66ef8SJohn Baldwin 	 * CPU!
609e0f66ef8SJohn Baldwin 	 */
610e0f66ef8SJohn Baldwin 	if (ie->ie_thread == NULL) {
611e0f66ef8SJohn Baldwin 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
612e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
613e0f66ef8SJohn Baldwin 		free(handler, M_ITHREAD);
614e0f66ef8SJohn Baldwin 		return (0);
615e0f66ef8SJohn Baldwin 	}
616e0f66ef8SJohn Baldwin 
617e0f66ef8SJohn Baldwin 	/*
618de271f01SJohn Baldwin 	 * If the interrupt thread is already running, then just mark this
619de271f01SJohn Baldwin 	 * handler as being dead and let the ithread do the actual removal.
620288e351bSDon Lewis 	 *
621288e351bSDon Lewis 	 * During a cold boot while cold is set, msleep() does not sleep,
622288e351bSDon Lewis 	 * so we have to remove the handler here rather than letting the
623288e351bSDon Lewis 	 * thread do it.
624de271f01SJohn Baldwin 	 */
625982d11f8SJeff Roberson 	thread_lock(ie->ie_thread->it_thread);
626e0f66ef8SJohn Baldwin 	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
627de271f01SJohn Baldwin 		handler->ih_flags |= IH_DEAD;
628de271f01SJohn Baldwin 
629de271f01SJohn Baldwin 		/*
630de271f01SJohn Baldwin 		 * Ensure that the thread will process the handler list
631de271f01SJohn Baldwin 		 * again and remove this handler if it has already passed
632de271f01SJohn Baldwin 		 * it on the list.
633de271f01SJohn Baldwin 		 */
634e0f66ef8SJohn Baldwin 		ie->ie_thread->it_need = 1;
6354d29cb2dSJohn Baldwin 	} else
636e0f66ef8SJohn Baldwin 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
637982d11f8SJeff Roberson 	thread_unlock(ie->ie_thread->it_thread);
638e0f66ef8SJohn Baldwin 	while (handler->ih_flags & IH_DEAD)
6390f180a7cSJohn Baldwin 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
640e0f66ef8SJohn Baldwin 	intr_event_update(ie);
641e0f66ef8SJohn Baldwin #ifdef notyet
642e0f66ef8SJohn Baldwin 	/*
643e0f66ef8SJohn Baldwin 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
644e0f66ef8SJohn Baldwin 	 * this could lead to races of stale data when servicing an
645e0f66ef8SJohn Baldwin 	 * interrupt.
646e0f66ef8SJohn Baldwin 	 */
647e0f66ef8SJohn Baldwin 	dead = 1;
648e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
649e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_FAST)) {
650e0f66ef8SJohn Baldwin 			dead = 0;
651e0f66ef8SJohn Baldwin 			break;
652e0f66ef8SJohn Baldwin 		}
653e0f66ef8SJohn Baldwin 	}
654e0f66ef8SJohn Baldwin 	if (dead) {
655e0f66ef8SJohn Baldwin 		ithread_destroy(ie->ie_thread);
656e0f66ef8SJohn Baldwin 		ie->ie_thread = NULL;
657e0f66ef8SJohn Baldwin 	}
658e0f66ef8SJohn Baldwin #endif
659e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
660b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
661b4151f71SJohn Baldwin 	return (0);
662b4151f71SJohn Baldwin }
663b4151f71SJohn Baldwin 
664b4151f71SJohn Baldwin int
665e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie)
6663e5da754SJohn Baldwin {
667e0f66ef8SJohn Baldwin 	struct intr_entropy entropy;
668e0f66ef8SJohn Baldwin 	struct intr_thread *it;
669b40ce416SJulian Elischer 	struct thread *td;
67004774f23SJulian Elischer 	struct thread *ctd;
6713e5da754SJohn Baldwin 	struct proc *p;
6723e5da754SJohn Baldwin 
6733e5da754SJohn Baldwin 	/*
6743e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
6753e5da754SJohn Baldwin 	 */
676e0f66ef8SJohn Baldwin 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
677e0f66ef8SJohn Baldwin 	    ie->ie_thread == NULL)
6783e5da754SJohn Baldwin 		return (EINVAL);
6793e5da754SJohn Baldwin 
68004774f23SJulian Elischer 	ctd = curthread;
681e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
682e0f66ef8SJohn Baldwin 	td = it->it_thread;
6836f40c417SRobert Watson 	p = td->td_proc;
684e0f66ef8SJohn Baldwin 
6853e5da754SJohn Baldwin 	/*
6863e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
6873e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
6883e5da754SJohn Baldwin 	 */
689e0f66ef8SJohn Baldwin 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
6906f40c417SRobert Watson 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
6916f40c417SRobert Watson 		    p->p_pid, p->p_comm);
692e0f66ef8SJohn Baldwin 		entropy.event = (uintptr_t)ie;
693e0f66ef8SJohn Baldwin 		entropy.td = ctd;
6943e5da754SJohn Baldwin 		random_harvest(&entropy, sizeof(entropy), 2, 0,
6953e5da754SJohn Baldwin 		    RANDOM_INTERRUPT);
6963e5da754SJohn Baldwin 	}
6973e5da754SJohn Baldwin 
698e0f66ef8SJohn Baldwin 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
6993e5da754SJohn Baldwin 
7003e5da754SJohn Baldwin 	/*
7013e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
702982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
703982d11f8SJeff Roberson 	 * put it on the runqueue.
7043e5da754SJohn Baldwin 	 */
705e0f66ef8SJohn Baldwin 	it->it_need = 1;
706982d11f8SJeff Roberson 	thread_lock(td);
70771fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
708e0f66ef8SJohn Baldwin 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
709e0f66ef8SJohn Baldwin 		    p->p_comm);
71071fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
711f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
7123e5da754SJohn Baldwin 	} else {
713e0f66ef8SJohn Baldwin 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
714e0f66ef8SJohn Baldwin 		    __func__, p->p_pid, p->p_comm, it->it_need, td->td_state);
7153e5da754SJohn Baldwin 	}
716982d11f8SJeff Roberson 	thread_unlock(td);
7173e5da754SJohn Baldwin 
7183e5da754SJohn Baldwin 	return (0);
7193e5da754SJohn Baldwin }
720bafe5a31SPaolo Pisati #else
721bafe5a31SPaolo Pisati int
722bafe5a31SPaolo Pisati intr_event_remove_handler(void *cookie)
723bafe5a31SPaolo Pisati {
724bafe5a31SPaolo Pisati 	struct intr_handler *handler = (struct intr_handler *)cookie;
725bafe5a31SPaolo Pisati 	struct intr_event *ie;
726bafe5a31SPaolo Pisati 	struct intr_thread *it;
727bafe5a31SPaolo Pisati #ifdef INVARIANTS
728bafe5a31SPaolo Pisati 	struct intr_handler *ih;
729bafe5a31SPaolo Pisati #endif
730bafe5a31SPaolo Pisati #ifdef notyet
731bafe5a31SPaolo Pisati 	int dead;
732bafe5a31SPaolo Pisati #endif
733bafe5a31SPaolo Pisati 
734bafe5a31SPaolo Pisati 	if (handler == NULL)
735bafe5a31SPaolo Pisati 		return (EINVAL);
736bafe5a31SPaolo Pisati 	ie = handler->ih_event;
737bafe5a31SPaolo Pisati 	KASSERT(ie != NULL,
738bafe5a31SPaolo Pisati 	    ("interrupt handler \"%s\" has a NULL interrupt event",
739bafe5a31SPaolo Pisati 	    handler->ih_name));
740bafe5a31SPaolo Pisati 	mtx_lock(&ie->ie_lock);
741bafe5a31SPaolo Pisati 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
742bafe5a31SPaolo Pisati 	    ie->ie_name);
743bafe5a31SPaolo Pisati #ifdef INVARIANTS
744bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
745bafe5a31SPaolo Pisati 		if (ih == handler)
746bafe5a31SPaolo Pisati 			goto ok;
747bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
748bafe5a31SPaolo Pisati 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
749bafe5a31SPaolo Pisati 	    ih->ih_name, ie->ie_name);
750bafe5a31SPaolo Pisati ok:
751bafe5a31SPaolo Pisati #endif
752bafe5a31SPaolo Pisati 	/*
753bafe5a31SPaolo Pisati 	 * If there are no ithreads (per event and per handler), then
754bafe5a31SPaolo Pisati 	 * just remove the handler and return.
755bafe5a31SPaolo Pisati 	 * XXX: Note that an INTR_FAST handler might be running on another CPU!
756bafe5a31SPaolo Pisati 	 */
757bafe5a31SPaolo Pisati 	if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
758bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
759bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
760bafe5a31SPaolo Pisati 		free(handler, M_ITHREAD);
761bafe5a31SPaolo Pisati 		return (0);
762bafe5a31SPaolo Pisati 	}
763bafe5a31SPaolo Pisati 
764bafe5a31SPaolo Pisati 	/* Private or global ithread? */
765bafe5a31SPaolo Pisati 	it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
766bafe5a31SPaolo Pisati 	/*
767bafe5a31SPaolo Pisati 	 * If the interrupt thread is already running, then just mark this
768bafe5a31SPaolo Pisati 	 * handler as being dead and let the ithread do the actual removal.
769bafe5a31SPaolo Pisati 	 *
770bafe5a31SPaolo Pisati 	 * During a cold boot while cold is set, msleep() does not sleep,
771bafe5a31SPaolo Pisati 	 * so we have to remove the handler here rather than letting the
772bafe5a31SPaolo Pisati 	 * thread do it.
773bafe5a31SPaolo Pisati 	 */
774982d11f8SJeff Roberson 	thread_lock(it->it_thread);
775bafe5a31SPaolo Pisati 	if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
776bafe5a31SPaolo Pisati 		handler->ih_flags |= IH_DEAD;
777bafe5a31SPaolo Pisati 
778bafe5a31SPaolo Pisati 		/*
779bafe5a31SPaolo Pisati 		 * Ensure that the thread will process the handler list
780bafe5a31SPaolo Pisati 		 * again and remove this handler if it has already passed
781bafe5a31SPaolo Pisati 		 * it on the list.
782bafe5a31SPaolo Pisati 		 */
783bafe5a31SPaolo Pisati 		it->it_need = 1;
784bafe5a31SPaolo Pisati 	} else
785bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
786982d11f8SJeff Roberson 	thread_unlock(it->it_thread);
787bafe5a31SPaolo Pisati 	while (handler->ih_flags & IH_DEAD)
788bafe5a31SPaolo Pisati 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
789bafe5a31SPaolo Pisati 	/*
790bafe5a31SPaolo Pisati 	 * At this point, the handler has been disconnected from the event,
791bafe5a31SPaolo Pisati 	 * so we can kill the private ithread if any.
792bafe5a31SPaolo Pisati 	 */
793bafe5a31SPaolo Pisati 	if (handler->ih_thread) {
794bafe5a31SPaolo Pisati 		ithread_destroy(handler->ih_thread);
795bafe5a31SPaolo Pisati 		handler->ih_thread = NULL;
796bafe5a31SPaolo Pisati 	}
797bafe5a31SPaolo Pisati 	intr_event_update(ie);
798bafe5a31SPaolo Pisati #ifdef notyet
799bafe5a31SPaolo Pisati 	/*
800bafe5a31SPaolo Pisati 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
801bafe5a31SPaolo Pisati 	 * this could lead to races of stale data when servicing an
802bafe5a31SPaolo Pisati 	 * interrupt.
803bafe5a31SPaolo Pisati 	 */
804bafe5a31SPaolo Pisati 	dead = 1;
805bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
806bafe5a31SPaolo Pisati 		if (handler != NULL) {
807bafe5a31SPaolo Pisati 			dead = 0;
808bafe5a31SPaolo Pisati 			break;
809bafe5a31SPaolo Pisati 		}
810bafe5a31SPaolo Pisati 	}
811bafe5a31SPaolo Pisati 	if (dead) {
812bafe5a31SPaolo Pisati 		ithread_destroy(ie->ie_thread);
813bafe5a31SPaolo Pisati 		ie->ie_thread = NULL;
814bafe5a31SPaolo Pisati 	}
815bafe5a31SPaolo Pisati #endif
816bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
817bafe5a31SPaolo Pisati 	free(handler, M_ITHREAD);
818bafe5a31SPaolo Pisati 	return (0);
819bafe5a31SPaolo Pisati }
820bafe5a31SPaolo Pisati 
821bafe5a31SPaolo Pisati int
822bafe5a31SPaolo Pisati intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
823bafe5a31SPaolo Pisati {
824bafe5a31SPaolo Pisati 	struct intr_entropy entropy;
825bafe5a31SPaolo Pisati 	struct thread *td;
826bafe5a31SPaolo Pisati 	struct thread *ctd;
827bafe5a31SPaolo Pisati 	struct proc *p;
828bafe5a31SPaolo Pisati 
829bafe5a31SPaolo Pisati 	/*
830bafe5a31SPaolo Pisati 	 * If no ithread or no handlers, then we have a stray interrupt.
831bafe5a31SPaolo Pisati 	 */
832bafe5a31SPaolo Pisati 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
833bafe5a31SPaolo Pisati 		return (EINVAL);
834bafe5a31SPaolo Pisati 
835bafe5a31SPaolo Pisati 	ctd = curthread;
836bafe5a31SPaolo Pisati 	td = it->it_thread;
837bafe5a31SPaolo Pisati 	p = td->td_proc;
838bafe5a31SPaolo Pisati 
839bafe5a31SPaolo Pisati 	/*
840bafe5a31SPaolo Pisati 	 * If any of the handlers for this ithread claim to be good
841bafe5a31SPaolo Pisati 	 * sources of entropy, then gather some.
842bafe5a31SPaolo Pisati 	 */
843bafe5a31SPaolo Pisati 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
844bafe5a31SPaolo Pisati 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
845bafe5a31SPaolo Pisati 		    p->p_pid, p->p_comm);
846bafe5a31SPaolo Pisati 		entropy.event = (uintptr_t)ie;
847bafe5a31SPaolo Pisati 		entropy.td = ctd;
848bafe5a31SPaolo Pisati 		random_harvest(&entropy, sizeof(entropy), 2, 0,
849bafe5a31SPaolo Pisati 		    RANDOM_INTERRUPT);
850bafe5a31SPaolo Pisati 	}
851bafe5a31SPaolo Pisati 
852bafe5a31SPaolo Pisati 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
853bafe5a31SPaolo Pisati 
854bafe5a31SPaolo Pisati 	/*
855bafe5a31SPaolo Pisati 	 * Set it_need to tell the thread to keep running if it is already
856982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
857982d11f8SJeff Roberson 	 * put it on the runqueue.
858bafe5a31SPaolo Pisati 	 */
859bafe5a31SPaolo Pisati 	it->it_need = 1;
860982d11f8SJeff Roberson 	thread_lock(td);
861bafe5a31SPaolo Pisati 	if (TD_AWAITING_INTR(td)) {
862bafe5a31SPaolo Pisati 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
863bafe5a31SPaolo Pisati 		    p->p_comm);
864bafe5a31SPaolo Pisati 		TD_CLR_IWAIT(td);
865bafe5a31SPaolo Pisati 		sched_add(td, SRQ_INTR);
866bafe5a31SPaolo Pisati 	} else {
867bafe5a31SPaolo Pisati 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
868bafe5a31SPaolo Pisati 		    __func__, p->p_pid, p->p_comm, it->it_need, td->td_state);
869bafe5a31SPaolo Pisati 	}
870982d11f8SJeff Roberson 	thread_unlock(td);
871bafe5a31SPaolo Pisati 
872bafe5a31SPaolo Pisati 	return (0);
873bafe5a31SPaolo Pisati }
874bafe5a31SPaolo Pisati #endif
8753e5da754SJohn Baldwin 
876fe486a37SJohn Baldwin /*
877fe486a37SJohn Baldwin  * Add a software interrupt handler to a specified event.  If a given event
878fe486a37SJohn Baldwin  * is not specified, then a new event is created.
879fe486a37SJohn Baldwin  */
8803e5da754SJohn Baldwin int
881e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
882b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
8838088699fSJohn Baldwin {
884e0f66ef8SJohn Baldwin 	struct intr_event *ie;
885b4151f71SJohn Baldwin 	int error;
8868088699fSJohn Baldwin 
887bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
8883e5da754SJohn Baldwin 		return (EINVAL);
8893e5da754SJohn Baldwin 
890e0f66ef8SJohn Baldwin 	ie = (eventp != NULL) ? *eventp : NULL;
8918088699fSJohn Baldwin 
892e0f66ef8SJohn Baldwin 	if (ie != NULL) {
893e0f66ef8SJohn Baldwin 		if (!(ie->ie_flags & IE_SOFT))
8943e5da754SJohn Baldwin 			return (EINVAL);
8953e5da754SJohn Baldwin 	} else {
896bafe5a31SPaolo Pisati #ifdef INTR_FILTER
897bafe5a31SPaolo Pisati 		error = intr_event_create(&ie, NULL, IE_SOFT,
898bafe5a31SPaolo Pisati 		    NULL, NULL, NULL, "swi%d:", pri);
899bafe5a31SPaolo Pisati #else
900bafe5a31SPaolo Pisati 		error = intr_event_create(&ie, NULL, IE_SOFT,
901bafe5a31SPaolo Pisati 		    NULL, "swi%d:", pri);
902bafe5a31SPaolo Pisati #endif
9038088699fSJohn Baldwin 		if (error)
904b4151f71SJohn Baldwin 			return (error);
905e0f66ef8SJohn Baldwin 		if (eventp != NULL)
906e0f66ef8SJohn Baldwin 			*eventp = ie;
9078088699fSJohn Baldwin 	}
908ef544f63SPaolo Pisati 	return (intr_event_add_handler(ie, name, NULL, handler, arg,
909d5a08a60SJake Burkholder 		    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
910ed062c8dSJulian Elischer 		    /* XXKSE.. think of a better way to get separate queues */
9118088699fSJohn Baldwin }
9128088699fSJohn Baldwin 
9131931cf94SJohn Baldwin /*
914e0f66ef8SJohn Baldwin  * Schedule a software interrupt thread.
9151931cf94SJohn Baldwin  */
9161931cf94SJohn Baldwin void
917b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
9181931cf94SJohn Baldwin {
919e0f66ef8SJohn Baldwin 	struct intr_handler *ih = (struct intr_handler *)cookie;
920e0f66ef8SJohn Baldwin 	struct intr_event *ie = ih->ih_event;
9213e5da754SJohn Baldwin 	int error;
9228088699fSJohn Baldwin 
923e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
924e0f66ef8SJohn Baldwin 	    ih->ih_need);
9251931cf94SJohn Baldwin 
9261931cf94SJohn Baldwin 	/*
9273e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
9283e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
9293e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
9301931cf94SJohn Baldwin 	 */
931b4151f71SJohn Baldwin 	atomic_store_rel_int(&ih->ih_need, 1);
9321ca2c018SBruce Evans 
933b4151f71SJohn Baldwin 	if (!(flags & SWI_DELAY)) {
93467596082SAttilio Rao 		PCPU_INC(cnt.v_soft);
935bafe5a31SPaolo Pisati #ifdef INTR_FILTER
936bafe5a31SPaolo Pisati 		error = intr_event_schedule_thread(ie, ie->ie_thread);
937bafe5a31SPaolo Pisati #else
938e0f66ef8SJohn Baldwin 		error = intr_event_schedule_thread(ie);
939bafe5a31SPaolo Pisati #endif
9403e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
9418088699fSJohn Baldwin 	}
9428088699fSJohn Baldwin }
9438088699fSJohn Baldwin 
944fe486a37SJohn Baldwin /*
945fe486a37SJohn Baldwin  * Remove a software interrupt handler.  Currently this code does not
946fe486a37SJohn Baldwin  * remove the associated interrupt event if it becomes empty.  Calling code
947fe486a37SJohn Baldwin  * may do so manually via intr_event_destroy(), but that's not really
948fe486a37SJohn Baldwin  * an optimal interface.
949fe486a37SJohn Baldwin  */
950fe486a37SJohn Baldwin int
951fe486a37SJohn Baldwin swi_remove(void *cookie)
952fe486a37SJohn Baldwin {
953fe486a37SJohn Baldwin 
954fe486a37SJohn Baldwin 	return (intr_event_remove_handler(cookie));
955fe486a37SJohn Baldwin }
956fe486a37SJohn Baldwin 
957bafe5a31SPaolo Pisati #ifdef INTR_FILTER
958bafe5a31SPaolo Pisati static void
959bafe5a31SPaolo Pisati priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
960bafe5a31SPaolo Pisati {
961bafe5a31SPaolo Pisati 	struct intr_event *ie;
962bafe5a31SPaolo Pisati 
963bafe5a31SPaolo Pisati 	ie = ih->ih_event;
964bafe5a31SPaolo Pisati 	/*
965bafe5a31SPaolo Pisati 	 * If this handler is marked for death, remove it from
966bafe5a31SPaolo Pisati 	 * the list of handlers and wake up the sleeper.
967bafe5a31SPaolo Pisati 	 */
968bafe5a31SPaolo Pisati 	if (ih->ih_flags & IH_DEAD) {
969bafe5a31SPaolo Pisati 		mtx_lock(&ie->ie_lock);
970bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
971bafe5a31SPaolo Pisati 		ih->ih_flags &= ~IH_DEAD;
972bafe5a31SPaolo Pisati 		wakeup(ih);
973bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
974bafe5a31SPaolo Pisati 		return;
975bafe5a31SPaolo Pisati 	}
976bafe5a31SPaolo Pisati 
977bafe5a31SPaolo Pisati 	/* Execute this handler. */
978bafe5a31SPaolo Pisati 	CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
979bafe5a31SPaolo Pisati 	     __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
980bafe5a31SPaolo Pisati 	     ih->ih_name, ih->ih_flags);
981bafe5a31SPaolo Pisati 
982bafe5a31SPaolo Pisati 	if (!(ih->ih_flags & IH_MPSAFE))
983bafe5a31SPaolo Pisati 		mtx_lock(&Giant);
984bafe5a31SPaolo Pisati 	ih->ih_handler(ih->ih_argument);
985bafe5a31SPaolo Pisati 	if (!(ih->ih_flags & IH_MPSAFE))
986bafe5a31SPaolo Pisati 		mtx_unlock(&Giant);
987bafe5a31SPaolo Pisati }
988bafe5a31SPaolo Pisati #endif
989bafe5a31SPaolo Pisati 
990e0f66ef8SJohn Baldwin static void
991e0f66ef8SJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie)
992e0f66ef8SJohn Baldwin {
993e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *ihn;
994e0f66ef8SJohn Baldwin 
995e0f66ef8SJohn Baldwin 	/* Interrupt handlers should not sleep. */
996e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
997e0f66ef8SJohn Baldwin 		THREAD_NO_SLEEPING();
998e0f66ef8SJohn Baldwin 	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
999e0f66ef8SJohn Baldwin 
1000e0f66ef8SJohn Baldwin 		/*
1001e0f66ef8SJohn Baldwin 		 * If this handler is marked for death, remove it from
1002e0f66ef8SJohn Baldwin 		 * the list of handlers and wake up the sleeper.
1003e0f66ef8SJohn Baldwin 		 */
1004e0f66ef8SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
1005e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
1006e0f66ef8SJohn Baldwin 			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1007e0f66ef8SJohn Baldwin 			ih->ih_flags &= ~IH_DEAD;
1008e0f66ef8SJohn Baldwin 			wakeup(ih);
1009e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
1010e0f66ef8SJohn Baldwin 			continue;
1011e0f66ef8SJohn Baldwin 		}
1012e0f66ef8SJohn Baldwin 
1013f2d619c8SPaolo Pisati 		/* Skip filter only handlers */
1014f2d619c8SPaolo Pisati 		if (ih->ih_handler == NULL)
1015f2d619c8SPaolo Pisati 			continue;
1016f2d619c8SPaolo Pisati 
1017e0f66ef8SJohn Baldwin 		/*
1018e0f66ef8SJohn Baldwin 		 * For software interrupt threads, we only execute
1019e0f66ef8SJohn Baldwin 		 * handlers that have their need flag set.  Hardware
1020e0f66ef8SJohn Baldwin 		 * interrupt threads always invoke all of their handlers.
1021e0f66ef8SJohn Baldwin 		 */
1022e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
1023e0f66ef8SJohn Baldwin 			if (!ih->ih_need)
1024e0f66ef8SJohn Baldwin 				continue;
1025e0f66ef8SJohn Baldwin 			else
1026e0f66ef8SJohn Baldwin 				atomic_store_rel_int(&ih->ih_need, 0);
1027e0f66ef8SJohn Baldwin 		}
1028e0f66ef8SJohn Baldwin 
1029e0f66ef8SJohn Baldwin 		/* Execute this handler. */
1030e0f66ef8SJohn Baldwin 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1031bafe5a31SPaolo Pisati 		    __func__, p->p_pid, (void *)ih->ih_handler,
1032bafe5a31SPaolo Pisati 		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1033e0f66ef8SJohn Baldwin 
1034e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1035e0f66ef8SJohn Baldwin 			mtx_lock(&Giant);
1036e0f66ef8SJohn Baldwin 		ih->ih_handler(ih->ih_argument);
1037e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1038e0f66ef8SJohn Baldwin 			mtx_unlock(&Giant);
1039e0f66ef8SJohn Baldwin 	}
1040e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
1041e0f66ef8SJohn Baldwin 		THREAD_SLEEPING_OK();
1042e0f66ef8SJohn Baldwin 
1043e0f66ef8SJohn Baldwin 	/*
1044e0f66ef8SJohn Baldwin 	 * Interrupt storm handling:
1045e0f66ef8SJohn Baldwin 	 *
1046e0f66ef8SJohn Baldwin 	 * If this interrupt source is currently storming, then throttle
1047e0f66ef8SJohn Baldwin 	 * it to only fire the handler once  per clock tick.
1048e0f66ef8SJohn Baldwin 	 *
1049e0f66ef8SJohn Baldwin 	 * If this interrupt source is not currently storming, but the
1050e0f66ef8SJohn Baldwin 	 * number of back to back interrupts exceeds the storm threshold,
1051e0f66ef8SJohn Baldwin 	 * then enter storming mode.
1052e0f66ef8SJohn Baldwin 	 */
1053e41bcf3cSJohn Baldwin 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1054e41bcf3cSJohn Baldwin 	    !(ie->ie_flags & IE_SOFT)) {
10550ae62c18SNate Lawson 		/* Report the message only once every second. */
10560ae62c18SNate Lawson 		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1057e0f66ef8SJohn Baldwin 			printf(
10580ae62c18SNate Lawson 	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1059e0f66ef8SJohn Baldwin 			    ie->ie_name);
1060e0f66ef8SJohn Baldwin 		}
1061e41bcf3cSJohn Baldwin 		pause("istorm", 1);
1062e0f66ef8SJohn Baldwin 	} else
1063e0f66ef8SJohn Baldwin 		ie->ie_count++;
1064e0f66ef8SJohn Baldwin 
1065e0f66ef8SJohn Baldwin 	/*
1066e0f66ef8SJohn Baldwin 	 * Now that all the handlers have had a chance to run, reenable
1067e0f66ef8SJohn Baldwin 	 * the interrupt source.
1068e0f66ef8SJohn Baldwin 	 */
1069e0f66ef8SJohn Baldwin 	if (ie->ie_enable != NULL)
1070e0f66ef8SJohn Baldwin 		ie->ie_enable(ie->ie_source);
1071e0f66ef8SJohn Baldwin }
1072e0f66ef8SJohn Baldwin 
1073bafe5a31SPaolo Pisati #ifndef INTR_FILTER
10748088699fSJohn Baldwin /*
1075b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
10768088699fSJohn Baldwin  */
107737c84183SPoul-Henning Kamp static void
1078b4151f71SJohn Baldwin ithread_loop(void *arg)
10798088699fSJohn Baldwin {
1080e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
1081e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1082b40ce416SJulian Elischer 	struct thread *td;
1083b4151f71SJohn Baldwin 	struct proc *p;
10848088699fSJohn Baldwin 
1085b40ce416SJulian Elischer 	td = curthread;
1086b40ce416SJulian Elischer 	p = td->td_proc;
1087e0f66ef8SJohn Baldwin 	ithd = (struct intr_thread *)arg;
1088e0f66ef8SJohn Baldwin 	KASSERT(ithd->it_thread == td,
108991f91617SDavid E. O'Brien 	    ("%s: ithread and proc linkage out of sync", __func__));
1090e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
1091e0f66ef8SJohn Baldwin 	ie->ie_count = 0;
10928088699fSJohn Baldwin 
10938088699fSJohn Baldwin 	/*
10948088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
10958088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
10968088699fSJohn Baldwin 	 */
10978088699fSJohn Baldwin 	for (;;) {
1098b4151f71SJohn Baldwin 		/*
1099b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
1100b4151f71SJohn Baldwin 		 */
1101b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
1102e0f66ef8SJohn Baldwin 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1103b4151f71SJohn Baldwin 			    p->p_pid, p->p_comm);
1104b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
1105b4151f71SJohn Baldwin 			kthread_exit(0);
1106b4151f71SJohn Baldwin 		}
1107b4151f71SJohn Baldwin 
1108e0f66ef8SJohn Baldwin 		/*
1109e0f66ef8SJohn Baldwin 		 * Service interrupts.  If another interrupt arrives while
1110e0f66ef8SJohn Baldwin 		 * we are running, it will set it_need to note that we
1111e0f66ef8SJohn Baldwin 		 * should make another pass.
1112e0f66ef8SJohn Baldwin 		 */
1113b4151f71SJohn Baldwin 		while (ithd->it_need) {
11148088699fSJohn Baldwin 			/*
1115e0f66ef8SJohn Baldwin 			 * This might need a full read and write barrier
1116e0f66ef8SJohn Baldwin 			 * to make sure that this write posts before any
1117e0f66ef8SJohn Baldwin 			 * of the memory or device accesses in the
1118e0f66ef8SJohn Baldwin 			 * handlers.
11198088699fSJohn Baldwin 			 */
1120b4151f71SJohn Baldwin 			atomic_store_rel_int(&ithd->it_need, 0);
1121e0f66ef8SJohn Baldwin 			ithread_execute_handlers(p, ie);
11228088699fSJohn Baldwin 		}
11237870c3c6SJohn Baldwin 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
11247870c3c6SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
11258088699fSJohn Baldwin 
11268088699fSJohn Baldwin 		/*
11278088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
11288088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
11298088699fSJohn Baldwin 		 * set again, so we have to check it again.
11308088699fSJohn Baldwin 		 */
1131982d11f8SJeff Roberson 		thread_lock(td);
1132e0f66ef8SJohn Baldwin 		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
11337870c3c6SJohn Baldwin 			TD_SET_IWAIT(td);
1134e0f66ef8SJohn Baldwin 			ie->ie_count = 0;
1135bf0acc27SJohn Baldwin 			mi_switch(SW_VOL, NULL);
11368088699fSJohn Baldwin 		}
1137982d11f8SJeff Roberson 		thread_unlock(td);
11388088699fSJohn Baldwin 	}
11391931cf94SJohn Baldwin }
1140bafe5a31SPaolo Pisati #else
1141bafe5a31SPaolo Pisati /*
1142bafe5a31SPaolo Pisati  * This is the main code for interrupt threads.
1143bafe5a31SPaolo Pisati  */
1144bafe5a31SPaolo Pisati static void
1145bafe5a31SPaolo Pisati ithread_loop(void *arg)
1146bafe5a31SPaolo Pisati {
1147bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
1148bafe5a31SPaolo Pisati 	struct intr_handler *ih;
1149bafe5a31SPaolo Pisati 	struct intr_event *ie;
1150bafe5a31SPaolo Pisati 	struct thread *td;
1151bafe5a31SPaolo Pisati 	struct proc *p;
1152bafe5a31SPaolo Pisati 	int priv;
1153bafe5a31SPaolo Pisati 
1154bafe5a31SPaolo Pisati 	td = curthread;
1155bafe5a31SPaolo Pisati 	p = td->td_proc;
1156bafe5a31SPaolo Pisati 	ih = (struct intr_handler *)arg;
1157bafe5a31SPaolo Pisati 	priv = (ih->ih_thread != NULL) ? 1 : 0;
1158bafe5a31SPaolo Pisati 	ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1159bafe5a31SPaolo Pisati 	KASSERT(ithd->it_thread == td,
1160bafe5a31SPaolo Pisati 	    ("%s: ithread and proc linkage out of sync", __func__));
1161bafe5a31SPaolo Pisati 	ie = ithd->it_event;
1162bafe5a31SPaolo Pisati 	ie->ie_count = 0;
1163bafe5a31SPaolo Pisati 
1164bafe5a31SPaolo Pisati 	/*
1165bafe5a31SPaolo Pisati 	 * As long as we have interrupts outstanding, go through the
1166bafe5a31SPaolo Pisati 	 * list of handlers, giving each one a go at it.
1167bafe5a31SPaolo Pisati 	 */
1168bafe5a31SPaolo Pisati 	for (;;) {
1169bafe5a31SPaolo Pisati 		/*
1170bafe5a31SPaolo Pisati 		 * If we are an orphaned thread, then just die.
1171bafe5a31SPaolo Pisati 		 */
1172bafe5a31SPaolo Pisati 		if (ithd->it_flags & IT_DEAD) {
1173bafe5a31SPaolo Pisati 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1174bafe5a31SPaolo Pisati 			    p->p_pid, p->p_comm);
1175bafe5a31SPaolo Pisati 			free(ithd, M_ITHREAD);
1176bafe5a31SPaolo Pisati 			kthread_exit(0);
1177bafe5a31SPaolo Pisati 		}
1178bafe5a31SPaolo Pisati 
1179bafe5a31SPaolo Pisati 		/*
1180bafe5a31SPaolo Pisati 		 * Service interrupts.  If another interrupt arrives while
1181bafe5a31SPaolo Pisati 		 * we are running, it will set it_need to note that we
1182bafe5a31SPaolo Pisati 		 * should make another pass.
1183bafe5a31SPaolo Pisati 		 */
1184bafe5a31SPaolo Pisati 		while (ithd->it_need) {
1185bafe5a31SPaolo Pisati 			/*
1186bafe5a31SPaolo Pisati 			 * This might need a full read and write barrier
1187bafe5a31SPaolo Pisati 			 * to make sure that this write posts before any
1188bafe5a31SPaolo Pisati 			 * of the memory or device accesses in the
1189bafe5a31SPaolo Pisati 			 * handlers.
1190bafe5a31SPaolo Pisati 			 */
1191bafe5a31SPaolo Pisati 			atomic_store_rel_int(&ithd->it_need, 0);
1192bafe5a31SPaolo Pisati 			if (priv)
1193bafe5a31SPaolo Pisati 				priv_ithread_execute_handler(p, ih);
1194bafe5a31SPaolo Pisati 			else
1195bafe5a31SPaolo Pisati 				ithread_execute_handlers(p, ie);
1196bafe5a31SPaolo Pisati 		}
1197bafe5a31SPaolo Pisati 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1198bafe5a31SPaolo Pisati 		mtx_assert(&Giant, MA_NOTOWNED);
1199bafe5a31SPaolo Pisati 
1200bafe5a31SPaolo Pisati 		/*
1201bafe5a31SPaolo Pisati 		 * Processed all our interrupts.  Now get the sched
1202bafe5a31SPaolo Pisati 		 * lock.  This may take a while and it_need may get
1203bafe5a31SPaolo Pisati 		 * set again, so we have to check it again.
1204bafe5a31SPaolo Pisati 		 */
1205982d11f8SJeff Roberson 		thread_lock(td);
1206bafe5a31SPaolo Pisati 		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1207bafe5a31SPaolo Pisati 			TD_SET_IWAIT(td);
1208bafe5a31SPaolo Pisati 			ie->ie_count = 0;
1209bafe5a31SPaolo Pisati 			mi_switch(SW_VOL, NULL);
1210bafe5a31SPaolo Pisati 		}
1211982d11f8SJeff Roberson 		thread_unlock(td);
1212bafe5a31SPaolo Pisati 	}
1213bafe5a31SPaolo Pisati }
1214bafe5a31SPaolo Pisati 
1215bafe5a31SPaolo Pisati /*
1216bafe5a31SPaolo Pisati  * Main loop for interrupt filter.
1217bafe5a31SPaolo Pisati  *
1218bafe5a31SPaolo Pisati  * Some architectures (i386, amd64 and arm) require the optional frame
1219bafe5a31SPaolo Pisati  * parameter, and use it as the main argument for fast handler execution
1220bafe5a31SPaolo Pisati  * when ih_argument == NULL.
1221bafe5a31SPaolo Pisati  *
1222bafe5a31SPaolo Pisati  * Return value:
1223bafe5a31SPaolo Pisati  * o FILTER_STRAY:              No filter recognized the event, and no
1224bafe5a31SPaolo Pisati  *                              filter-less handler is registered on this
1225bafe5a31SPaolo Pisati  *                              line.
1226bafe5a31SPaolo Pisati  * o FILTER_HANDLED:            A filter claimed the event and served it.
1227bafe5a31SPaolo Pisati  * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1228bafe5a31SPaolo Pisati  *                              least one filter-less handler on this line.
1229bafe5a31SPaolo Pisati  * o FILTER_HANDLED |
1230bafe5a31SPaolo Pisati  *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1231bafe5a31SPaolo Pisati  *                              scheduling the per-handler ithread.
1232bafe5a31SPaolo Pisati  *
1233bafe5a31SPaolo Pisati  * In case an ithread has to be scheduled, in *ithd there will be a
1234bafe5a31SPaolo Pisati  * pointer to a struct intr_thread containing the thread to be
1235bafe5a31SPaolo Pisati  * scheduled.
1236bafe5a31SPaolo Pisati  */
1237bafe5a31SPaolo Pisati 
1238bafe5a31SPaolo Pisati int
1239bafe5a31SPaolo Pisati intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1240bafe5a31SPaolo Pisati 		 struct intr_thread **ithd)
1241bafe5a31SPaolo Pisati {
1242bafe5a31SPaolo Pisati 	struct intr_handler *ih;
1243bafe5a31SPaolo Pisati 	void *arg;
1244bafe5a31SPaolo Pisati 	int ret, thread_only;
1245bafe5a31SPaolo Pisati 
1246bafe5a31SPaolo Pisati 	ret = 0;
1247bafe5a31SPaolo Pisati 	thread_only = 0;
1248bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1249bafe5a31SPaolo Pisati 		/*
1250bafe5a31SPaolo Pisati 		 * Execute fast interrupt handlers directly.
1251bafe5a31SPaolo Pisati 		 * To support clock handlers, if a handler registers
1252bafe5a31SPaolo Pisati 		 * with a NULL argument, then we pass it a pointer to
1253bafe5a31SPaolo Pisati 		 * a trapframe as its argument.
1254bafe5a31SPaolo Pisati 		 */
1255bafe5a31SPaolo Pisati 		arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1256bafe5a31SPaolo Pisati 
1257bafe5a31SPaolo Pisati 		CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1258bafe5a31SPaolo Pisati 		     ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1259bafe5a31SPaolo Pisati 
1260bafe5a31SPaolo Pisati 		if (ih->ih_filter != NULL)
1261bafe5a31SPaolo Pisati 			ret = ih->ih_filter(arg);
1262bafe5a31SPaolo Pisati 		else {
1263bafe5a31SPaolo Pisati 			thread_only = 1;
1264bafe5a31SPaolo Pisati 			continue;
1265bafe5a31SPaolo Pisati 		}
1266bafe5a31SPaolo Pisati 
1267bafe5a31SPaolo Pisati 		if (ret & FILTER_STRAY)
1268bafe5a31SPaolo Pisati 			continue;
1269bafe5a31SPaolo Pisati 		else {
1270bafe5a31SPaolo Pisati 			*ithd = ih->ih_thread;
1271bafe5a31SPaolo Pisati 			return (ret);
1272bafe5a31SPaolo Pisati 		}
1273bafe5a31SPaolo Pisati 	}
1274bafe5a31SPaolo Pisati 
1275bafe5a31SPaolo Pisati 	/*
1276bafe5a31SPaolo Pisati 	 * No filters handled the interrupt and we have at least
1277bafe5a31SPaolo Pisati 	 * one handler without a filter.  In this case, we schedule
1278bafe5a31SPaolo Pisati 	 * all of the filter-less handlers to run in the ithread.
1279bafe5a31SPaolo Pisati 	 */
1280bafe5a31SPaolo Pisati 	if (thread_only) {
1281bafe5a31SPaolo Pisati 		*ithd = ie->ie_thread;
1282bafe5a31SPaolo Pisati 		return (FILTER_SCHEDULE_THREAD);
1283bafe5a31SPaolo Pisati 	}
1284bafe5a31SPaolo Pisati 	return (FILTER_STRAY);
1285bafe5a31SPaolo Pisati }
1286bafe5a31SPaolo Pisati 
1287bafe5a31SPaolo Pisati /*
1288bafe5a31SPaolo Pisati  * Main interrupt handling body.
1289bafe5a31SPaolo Pisati  *
1290bafe5a31SPaolo Pisati  * Input:
1291bafe5a31SPaolo Pisati  * o ie:                        the event connected to this interrupt.
1292bafe5a31SPaolo Pisati  * o frame:                     some archs (i.e. i386) pass a frame to some.
1293bafe5a31SPaolo Pisati  *                              handlers as their main argument.
1294bafe5a31SPaolo Pisati  * Return value:
1295bafe5a31SPaolo Pisati  * o 0:                         everything ok.
1296bafe5a31SPaolo Pisati  * o EINVAL:                    stray interrupt.
1297bafe5a31SPaolo Pisati  */
1298bafe5a31SPaolo Pisati int
1299bafe5a31SPaolo Pisati intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1300bafe5a31SPaolo Pisati {
1301bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
1302bafe5a31SPaolo Pisati 	struct thread *td;
1303bafe5a31SPaolo Pisati 	int thread;
1304bafe5a31SPaolo Pisati 
1305bafe5a31SPaolo Pisati 	ithd = NULL;
1306bafe5a31SPaolo Pisati 	td = curthread;
1307bafe5a31SPaolo Pisati 
1308bafe5a31SPaolo Pisati 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1309bafe5a31SPaolo Pisati 		return (EINVAL);
1310bafe5a31SPaolo Pisati 
1311bafe5a31SPaolo Pisati 	td->td_intr_nesting_level++;
1312bafe5a31SPaolo Pisati 	thread = 0;
1313bafe5a31SPaolo Pisati 	critical_enter();
1314bafe5a31SPaolo Pisati 	thread = intr_filter_loop(ie, frame, &ithd);
1315bafe5a31SPaolo Pisati 
1316bafe5a31SPaolo Pisati 	/*
1317bafe5a31SPaolo Pisati 	 * If the interrupt was fully served, send it an EOI but leave
1318bafe5a31SPaolo Pisati 	 * it unmasked. Otherwise, mask the source as well as sending
1319bafe5a31SPaolo Pisati 	 * it an EOI.
1320bafe5a31SPaolo Pisati 	 */
1321bafe5a31SPaolo Pisati 	if (thread & FILTER_HANDLED) {
1322bafe5a31SPaolo Pisati 		if (ie->ie_eoi != NULL)
1323bafe5a31SPaolo Pisati 			ie->ie_eoi(ie->ie_source);
1324bafe5a31SPaolo Pisati 	} else {
1325bafe5a31SPaolo Pisati 		if (ie->ie_disab != NULL)
1326bafe5a31SPaolo Pisati 			ie->ie_disab(ie->ie_source);
1327bafe5a31SPaolo Pisati 	}
1328bafe5a31SPaolo Pisati 	critical_exit();
1329bafe5a31SPaolo Pisati 
1330bafe5a31SPaolo Pisati 	/* Interrupt storm logic */
1331bafe5a31SPaolo Pisati 	if (thread & FILTER_STRAY) {
1332bafe5a31SPaolo Pisati 		ie->ie_count++;
1333bafe5a31SPaolo Pisati 		if (ie->ie_count < intr_storm_threshold)
1334bafe5a31SPaolo Pisati 			printf("Interrupt stray detection not present\n");
1335bafe5a31SPaolo Pisati 	}
1336bafe5a31SPaolo Pisati 
1337bafe5a31SPaolo Pisati 	/* Schedule an ithread if needed. */
1338bafe5a31SPaolo Pisati 	if (thread & FILTER_SCHEDULE_THREAD) {
1339bafe5a31SPaolo Pisati 		if (intr_event_schedule_thread(ie, ithd) != 0)
1340bafe5a31SPaolo Pisati 			panic("%s: impossible stray interrupt", __func__);
1341bafe5a31SPaolo Pisati 	}
1342bafe5a31SPaolo Pisati 	td->td_intr_nesting_level--;
1343bafe5a31SPaolo Pisati 	return (0);
1344bafe5a31SPaolo Pisati }
1345bafe5a31SPaolo Pisati #endif
13461931cf94SJohn Baldwin 
13478b201c42SJohn Baldwin #ifdef DDB
13488b201c42SJohn Baldwin /*
13498b201c42SJohn Baldwin  * Dump details about an interrupt handler
13508b201c42SJohn Baldwin  */
13518b201c42SJohn Baldwin static void
1352e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih)
13538b201c42SJohn Baldwin {
13548b201c42SJohn Baldwin 	int comma;
13558b201c42SJohn Baldwin 
13568b201c42SJohn Baldwin 	db_printf("\t%-10s ", ih->ih_name);
13578b201c42SJohn Baldwin 	switch (ih->ih_pri) {
13588b201c42SJohn Baldwin 	case PI_REALTIME:
13598b201c42SJohn Baldwin 		db_printf("CLK ");
13608b201c42SJohn Baldwin 		break;
13618b201c42SJohn Baldwin 	case PI_AV:
13628b201c42SJohn Baldwin 		db_printf("AV  ");
13638b201c42SJohn Baldwin 		break;
13648b201c42SJohn Baldwin 	case PI_TTYHIGH:
13658b201c42SJohn Baldwin 	case PI_TTYLOW:
13668b201c42SJohn Baldwin 		db_printf("TTY ");
13678b201c42SJohn Baldwin 		break;
13688b201c42SJohn Baldwin 	case PI_TAPE:
13698b201c42SJohn Baldwin 		db_printf("TAPE");
13708b201c42SJohn Baldwin 		break;
13718b201c42SJohn Baldwin 	case PI_NET:
13728b201c42SJohn Baldwin 		db_printf("NET ");
13738b201c42SJohn Baldwin 		break;
13748b201c42SJohn Baldwin 	case PI_DISK:
13758b201c42SJohn Baldwin 	case PI_DISKLOW:
13768b201c42SJohn Baldwin 		db_printf("DISK");
13778b201c42SJohn Baldwin 		break;
13788b201c42SJohn Baldwin 	case PI_DULL:
13798b201c42SJohn Baldwin 		db_printf("DULL");
13808b201c42SJohn Baldwin 		break;
13818b201c42SJohn Baldwin 	default:
13828b201c42SJohn Baldwin 		if (ih->ih_pri >= PI_SOFT)
13838b201c42SJohn Baldwin 			db_printf("SWI ");
13848b201c42SJohn Baldwin 		else
13858b201c42SJohn Baldwin 			db_printf("%4u", ih->ih_pri);
13868b201c42SJohn Baldwin 		break;
13878b201c42SJohn Baldwin 	}
13888b201c42SJohn Baldwin 	db_printf(" ");
13898b201c42SJohn Baldwin 	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
13908b201c42SJohn Baldwin 	db_printf("(%p)", ih->ih_argument);
13918b201c42SJohn Baldwin 	if (ih->ih_need ||
1392ef544f63SPaolo Pisati 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
13938b201c42SJohn Baldwin 	    IH_MPSAFE)) != 0) {
13948b201c42SJohn Baldwin 		db_printf(" {");
13958b201c42SJohn Baldwin 		comma = 0;
13968b201c42SJohn Baldwin 		if (ih->ih_flags & IH_EXCLUSIVE) {
13978b201c42SJohn Baldwin 			if (comma)
13988b201c42SJohn Baldwin 				db_printf(", ");
13998b201c42SJohn Baldwin 			db_printf("EXCL");
14008b201c42SJohn Baldwin 			comma = 1;
14018b201c42SJohn Baldwin 		}
14028b201c42SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY) {
14038b201c42SJohn Baldwin 			if (comma)
14048b201c42SJohn Baldwin 				db_printf(", ");
14058b201c42SJohn Baldwin 			db_printf("ENTROPY");
14068b201c42SJohn Baldwin 			comma = 1;
14078b201c42SJohn Baldwin 		}
14088b201c42SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
14098b201c42SJohn Baldwin 			if (comma)
14108b201c42SJohn Baldwin 				db_printf(", ");
14118b201c42SJohn Baldwin 			db_printf("DEAD");
14128b201c42SJohn Baldwin 			comma = 1;
14138b201c42SJohn Baldwin 		}
14148b201c42SJohn Baldwin 		if (ih->ih_flags & IH_MPSAFE) {
14158b201c42SJohn Baldwin 			if (comma)
14168b201c42SJohn Baldwin 				db_printf(", ");
14178b201c42SJohn Baldwin 			db_printf("MPSAFE");
14188b201c42SJohn Baldwin 			comma = 1;
14198b201c42SJohn Baldwin 		}
14208b201c42SJohn Baldwin 		if (ih->ih_need) {
14218b201c42SJohn Baldwin 			if (comma)
14228b201c42SJohn Baldwin 				db_printf(", ");
14238b201c42SJohn Baldwin 			db_printf("NEED");
14248b201c42SJohn Baldwin 		}
14258b201c42SJohn Baldwin 		db_printf("}");
14268b201c42SJohn Baldwin 	}
14278b201c42SJohn Baldwin 	db_printf("\n");
14288b201c42SJohn Baldwin }
14298b201c42SJohn Baldwin 
14308b201c42SJohn Baldwin /*
1431e0f66ef8SJohn Baldwin  * Dump details about a event.
14328b201c42SJohn Baldwin  */
14338b201c42SJohn Baldwin void
1434e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers)
14358b201c42SJohn Baldwin {
1436e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
1437e0f66ef8SJohn Baldwin 	struct intr_thread *it;
14388b201c42SJohn Baldwin 	int comma;
14398b201c42SJohn Baldwin 
1440e0f66ef8SJohn Baldwin 	db_printf("%s ", ie->ie_fullname);
1441e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
1442e0f66ef8SJohn Baldwin 	if (it != NULL)
1443e0f66ef8SJohn Baldwin 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1444e0f66ef8SJohn Baldwin 	else
1445e0f66ef8SJohn Baldwin 		db_printf("(no thread)");
1446e0f66ef8SJohn Baldwin 	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1447e0f66ef8SJohn Baldwin 	    (it != NULL && it->it_need)) {
14488b201c42SJohn Baldwin 		db_printf(" {");
14498b201c42SJohn Baldwin 		comma = 0;
1450e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
14518b201c42SJohn Baldwin 			db_printf("SOFT");
14528b201c42SJohn Baldwin 			comma = 1;
14538b201c42SJohn Baldwin 		}
1454e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ENTROPY) {
14558b201c42SJohn Baldwin 			if (comma)
14568b201c42SJohn Baldwin 				db_printf(", ");
14578b201c42SJohn Baldwin 			db_printf("ENTROPY");
14588b201c42SJohn Baldwin 			comma = 1;
14598b201c42SJohn Baldwin 		}
1460e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD) {
14618b201c42SJohn Baldwin 			if (comma)
14628b201c42SJohn Baldwin 				db_printf(", ");
1463e0f66ef8SJohn Baldwin 			db_printf("ADDING_THREAD");
14648b201c42SJohn Baldwin 			comma = 1;
14658b201c42SJohn Baldwin 		}
1466e0f66ef8SJohn Baldwin 		if (it != NULL && it->it_need) {
14678b201c42SJohn Baldwin 			if (comma)
14688b201c42SJohn Baldwin 				db_printf(", ");
14698b201c42SJohn Baldwin 			db_printf("NEED");
14708b201c42SJohn Baldwin 		}
14718b201c42SJohn Baldwin 		db_printf("}");
14728b201c42SJohn Baldwin 	}
14738b201c42SJohn Baldwin 	db_printf("\n");
14748b201c42SJohn Baldwin 
14758b201c42SJohn Baldwin 	if (handlers)
1476e0f66ef8SJohn Baldwin 		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
14778b201c42SJohn Baldwin 		    db_dump_intrhand(ih);
14788b201c42SJohn Baldwin }
1479e0f66ef8SJohn Baldwin 
1480e0f66ef8SJohn Baldwin /*
1481e0f66ef8SJohn Baldwin  * Dump data about interrupt handlers
1482e0f66ef8SJohn Baldwin  */
1483e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr)
1484e0f66ef8SJohn Baldwin {
1485e0f66ef8SJohn Baldwin 	struct intr_event *ie;
148619e9205aSJohn Baldwin 	int all, verbose;
1487e0f66ef8SJohn Baldwin 
1488e0f66ef8SJohn Baldwin 	verbose = index(modif, 'v') != NULL;
1489e0f66ef8SJohn Baldwin 	all = index(modif, 'a') != NULL;
1490e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ie, &event_list, ie_list) {
1491e0f66ef8SJohn Baldwin 		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1492e0f66ef8SJohn Baldwin 			continue;
1493e0f66ef8SJohn Baldwin 		db_dump_intr_event(ie, verbose);
149419e9205aSJohn Baldwin 		if (db_pager_quit)
149519e9205aSJohn Baldwin 			break;
1496e0f66ef8SJohn Baldwin 	}
1497e0f66ef8SJohn Baldwin }
14988b201c42SJohn Baldwin #endif /* DDB */
14998b201c42SJohn Baldwin 
1500b4151f71SJohn Baldwin /*
15018088699fSJohn Baldwin  * Start standard software interrupt threads
15021931cf94SJohn Baldwin  */
15031931cf94SJohn Baldwin static void
1504b4151f71SJohn Baldwin start_softintr(void *dummy)
15051931cf94SJohn Baldwin {
15068804bf6bSJohn Baldwin 	struct proc *p;
1507b4151f71SJohn Baldwin 
1508e0f66ef8SJohn Baldwin 	if (swi_add(&clk_intr_event, "clock", softclock, NULL, SWI_CLOCK,
1509b4151f71SJohn Baldwin 		INTR_MPSAFE, &softclock_ih) ||
151079501b66SScott Long 	    swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1511b4151f71SJohn Baldwin 		panic("died while creating standard software ithreads");
15123e5da754SJohn Baldwin 
1513e0f66ef8SJohn Baldwin 	p = clk_intr_event->ie_thread->it_thread->td_proc;
15148804bf6bSJohn Baldwin 	PROC_LOCK(p);
15158804bf6bSJohn Baldwin 	p->p_flag |= P_NOLOAD;
15168804bf6bSJohn Baldwin 	PROC_UNLOCK(p);
15171931cf94SJohn Baldwin }
1518b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
15191931cf94SJohn Baldwin 
1520d279178dSThomas Moestl /*
1521d279178dSThomas Moestl  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1522d279178dSThomas Moestl  * The data for this machine dependent, and the declarations are in machine
1523d279178dSThomas Moestl  * dependent code.  The layout of intrnames and intrcnt however is machine
1524d279178dSThomas Moestl  * independent.
1525d279178dSThomas Moestl  *
1526d279178dSThomas Moestl  * We do not know the length of intrcnt and intrnames at compile time, so
1527d279178dSThomas Moestl  * calculate things at run time.
1528d279178dSThomas Moestl  */
1529d279178dSThomas Moestl static int
1530d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1531d279178dSThomas Moestl {
1532d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1533d279178dSThomas Moestl 	   req));
1534d279178dSThomas Moestl }
1535d279178dSThomas Moestl 
1536d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1537d279178dSThomas Moestl     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1538d279178dSThomas Moestl 
1539d279178dSThomas Moestl static int
1540d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1541d279178dSThomas Moestl {
1542d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrcnt,
1543d279178dSThomas Moestl 	    (char *)eintrcnt - (char *)intrcnt, req));
1544d279178dSThomas Moestl }
1545d279178dSThomas Moestl 
1546d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1547d279178dSThomas Moestl     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
15488b201c42SJohn Baldwin 
15498b201c42SJohn Baldwin #ifdef DDB
15508b201c42SJohn Baldwin /*
15518b201c42SJohn Baldwin  * DDB command to dump the interrupt statistics.
15528b201c42SJohn Baldwin  */
15538b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
15548b201c42SJohn Baldwin {
15558b201c42SJohn Baldwin 	u_long *i;
15568b201c42SJohn Baldwin 	char *cp;
15578b201c42SJohn Baldwin 
15588b201c42SJohn Baldwin 	cp = intrnames;
155919e9205aSJohn Baldwin 	for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
15608b201c42SJohn Baldwin 		if (*cp == '\0')
15618b201c42SJohn Baldwin 			break;
15628b201c42SJohn Baldwin 		if (*i != 0)
15638b201c42SJohn Baldwin 			db_printf("%s\t%lu\n", cp, *i);
15648b201c42SJohn Baldwin 		cp += strlen(cp) + 1;
15658b201c42SJohn Baldwin 	}
15668b201c42SJohn Baldwin }
15678b201c42SJohn Baldwin #endif
1568