xref: /freebsd/sys/kern/kern_intr.c (revision 1ee1b6879200ab6df85efd8a509d9f092597908b)
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>
49eaf86d16SJohn Baldwin #include <sys/smp.h>
50d279178dSThomas Moestl #include <sys/sysctl.h>
511931cf94SJohn Baldwin #include <sys/unistd.h>
521931cf94SJohn Baldwin #include <sys/vmmeter.h>
531931cf94SJohn Baldwin #include <machine/atomic.h>
541931cf94SJohn Baldwin #include <machine/cpu.h>
558088699fSJohn Baldwin #include <machine/md_var.h>
56b4151f71SJohn Baldwin #include <machine/stdarg.h>
578b201c42SJohn Baldwin #ifdef DDB
588b201c42SJohn Baldwin #include <ddb/ddb.h>
598b201c42SJohn Baldwin #include <ddb/db_sym.h>
608b201c42SJohn Baldwin #endif
61425f9fdaSStefan Eßer 
62e0f66ef8SJohn Baldwin /*
63e0f66ef8SJohn Baldwin  * Describe an interrupt thread.  There is one of these per interrupt event.
64e0f66ef8SJohn Baldwin  */
65e0f66ef8SJohn Baldwin struct intr_thread {
66e0f66ef8SJohn Baldwin 	struct intr_event *it_event;
67e0f66ef8SJohn Baldwin 	struct thread *it_thread;	/* Kernel thread. */
68e0f66ef8SJohn Baldwin 	int	it_flags;		/* (j) IT_* flags. */
69e0f66ef8SJohn Baldwin 	int	it_need;		/* Needs service. */
703e5da754SJohn Baldwin };
713e5da754SJohn Baldwin 
72e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */
73e0f66ef8SJohn Baldwin #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
74e0f66ef8SJohn Baldwin 
75e0f66ef8SJohn Baldwin struct	intr_entropy {
76e0f66ef8SJohn Baldwin 	struct	thread *td;
77e0f66ef8SJohn Baldwin 	uintptr_t event;
78e0f66ef8SJohn Baldwin };
79e0f66ef8SJohn Baldwin 
80e0f66ef8SJohn Baldwin struct	intr_event *clk_intr_event;
81e0f66ef8SJohn Baldwin struct	intr_event *tty_intr_event;
827b1fe905SBruce Evans void	*vm_ih;
837ab24ea3SJulian Elischer struct proc *intrproc;
841931cf94SJohn Baldwin 
85b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
86b4151f71SJohn Baldwin 
870ae62c18SNate Lawson static int intr_storm_threshold = 1000;
887870c3c6SJohn Baldwin TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
897870c3c6SJohn Baldwin SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
907870c3c6SJohn Baldwin     &intr_storm_threshold, 0,
917b1fe905SBruce Evans     "Number of consecutive interrupts before storm protection is enabled");
92e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list =
93e0f66ef8SJohn Baldwin     TAILQ_HEAD_INITIALIZER(event_list);
947b1fe905SBruce Evans 
95e0f66ef8SJohn Baldwin static void	intr_event_update(struct intr_event *ie);
96bafe5a31SPaolo Pisati #ifdef INTR_FILTER
971ee1b687SJohn Baldwin static int	intr_event_schedule_thread(struct intr_event *ie,
981ee1b687SJohn Baldwin 		    struct intr_thread *ithd);
991ee1b687SJohn Baldwin static int	intr_filter_loop(struct intr_event *ie,
1001ee1b687SJohn Baldwin 		    struct trapframe *frame, struct intr_thread **ithd);
101bafe5a31SPaolo Pisati static struct intr_thread *ithread_create(const char *name,
102bafe5a31SPaolo Pisati 			      struct intr_handler *ih);
103bafe5a31SPaolo Pisati #else
1041ee1b687SJohn Baldwin static int	intr_event_schedule_thread(struct intr_event *ie);
105e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name);
106bafe5a31SPaolo Pisati #endif
107e0f66ef8SJohn Baldwin static void	ithread_destroy(struct intr_thread *ithread);
108bafe5a31SPaolo Pisati static void	ithread_execute_handlers(struct proc *p,
109bafe5a31SPaolo Pisati 		    struct intr_event *ie);
110bafe5a31SPaolo Pisati #ifdef INTR_FILTER
111bafe5a31SPaolo Pisati static void	priv_ithread_execute_handler(struct proc *p,
112bafe5a31SPaolo Pisati 		    struct intr_handler *ih);
113bafe5a31SPaolo Pisati #endif
1147b1fe905SBruce Evans static void	ithread_loop(void *);
115e0f66ef8SJohn Baldwin static void	ithread_update(struct intr_thread *ithd);
1167b1fe905SBruce Evans static void	start_softintr(void *);
1177870c3c6SJohn Baldwin 
118bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */
119b4151f71SJohn Baldwin u_char
120e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags)
1219a94c9c5SJohn Baldwin {
122b4151f71SJohn Baldwin 	u_char pri;
1239a94c9c5SJohn Baldwin 
124b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
1255a280d9cSPeter Wemm 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
1269a94c9c5SJohn Baldwin 	switch (flags) {
127b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
1289a94c9c5SJohn Baldwin 		pri = PI_TTYLOW;
1299a94c9c5SJohn Baldwin 		break;
1309a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
1319a94c9c5SJohn Baldwin 		/*
1329a94c9c5SJohn Baldwin 		 * XXX We need to refine this.  BSD/OS distinguishes
1339a94c9c5SJohn Baldwin 		 * between tape and disk priorities.
1349a94c9c5SJohn Baldwin 		 */
1359a94c9c5SJohn Baldwin 		pri = PI_DISK;
1369a94c9c5SJohn Baldwin 		break;
1379a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
1389a94c9c5SJohn Baldwin 		pri = PI_NET;
1399a94c9c5SJohn Baldwin 		break;
1409a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
1419a94c9c5SJohn Baldwin 		pri = PI_DISK;          /* XXX or PI_CAM? */
1429a94c9c5SJohn Baldwin 		break;
1435a280d9cSPeter Wemm 	case INTR_TYPE_AV:		/* Audio/video */
1445a280d9cSPeter Wemm 		pri = PI_AV;
1455a280d9cSPeter Wemm 		break;
146b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
147b4151f71SJohn Baldwin 		pri = PI_REALTIME;
148b4151f71SJohn Baldwin 		break;
1499a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1509a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1519a94c9c5SJohn Baldwin 		break;
1529a94c9c5SJohn Baldwin 	default:
153b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
154e0f66ef8SJohn Baldwin 		panic("intr_priority: no interrupt type in flags");
1559a94c9c5SJohn Baldwin 	}
1569a94c9c5SJohn Baldwin 
1579a94c9c5SJohn Baldwin 	return pri;
1589a94c9c5SJohn Baldwin }
1599a94c9c5SJohn Baldwin 
160b4151f71SJohn Baldwin /*
161e0f66ef8SJohn Baldwin  * Update an ithread based on the associated intr_event.
162b4151f71SJohn Baldwin  */
163b4151f71SJohn Baldwin static void
164e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd)
165b4151f71SJohn Baldwin {
166e0f66ef8SJohn Baldwin 	struct intr_event *ie;
167b40ce416SJulian Elischer 	struct thread *td;
168e0f66ef8SJohn Baldwin 	u_char pri;
1698088699fSJohn Baldwin 
170e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
171e0f66ef8SJohn Baldwin 	td = ithd->it_thread;
172b4151f71SJohn Baldwin 
173e0f66ef8SJohn Baldwin 	/* Determine the overall priority of this event. */
174e0f66ef8SJohn Baldwin 	if (TAILQ_EMPTY(&ie->ie_handlers))
175e0f66ef8SJohn Baldwin 		pri = PRI_MAX_ITHD;
176e0f66ef8SJohn Baldwin 	else
177e0f66ef8SJohn Baldwin 		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
178e80fb434SRobert Drehmel 
179e0f66ef8SJohn Baldwin 	/* Update name and priority. */
1807ab24ea3SJulian Elischer 	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
181982d11f8SJeff Roberson 	thread_lock(td);
182e0f66ef8SJohn Baldwin 	sched_prio(td, pri);
183982d11f8SJeff Roberson 	thread_unlock(td);
184b4151f71SJohn Baldwin }
185e0f66ef8SJohn Baldwin 
186e0f66ef8SJohn Baldwin /*
187e0f66ef8SJohn Baldwin  * Regenerate the full name of an interrupt event and update its priority.
188e0f66ef8SJohn Baldwin  */
189e0f66ef8SJohn Baldwin static void
190e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie)
191e0f66ef8SJohn Baldwin {
192e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
193e0f66ef8SJohn Baldwin 	char *last;
194e0f66ef8SJohn Baldwin 	int missed, space;
195e0f66ef8SJohn Baldwin 
196e0f66ef8SJohn Baldwin 	/* Start off with no entropy and just the name of the event. */
197e0f66ef8SJohn Baldwin 	mtx_assert(&ie->ie_lock, MA_OWNED);
198e0f66ef8SJohn Baldwin 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
199e0f66ef8SJohn Baldwin 	ie->ie_flags &= ~IE_ENTROPY;
2000811d60aSJohn Baldwin 	missed = 0;
201e0f66ef8SJohn Baldwin 	space = 1;
202e0f66ef8SJohn Baldwin 
203e0f66ef8SJohn Baldwin 	/* Run through all the handlers updating values. */
204e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
205e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
206e0f66ef8SJohn Baldwin 		    sizeof(ie->ie_fullname)) {
207e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " ");
208e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, ih->ih_name);
209e0f66ef8SJohn Baldwin 			space = 0;
2100811d60aSJohn Baldwin 		} else
2110811d60aSJohn Baldwin 			missed++;
2120811d60aSJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY)
213e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ENTROPY;
2140811d60aSJohn Baldwin 	}
215e0f66ef8SJohn Baldwin 
216e0f66ef8SJohn Baldwin 	/*
217e0f66ef8SJohn Baldwin 	 * If the handler names were too long, add +'s to indicate missing
218e0f66ef8SJohn Baldwin 	 * names. If we run out of room and still have +'s to add, change
219e0f66ef8SJohn Baldwin 	 * the last character from a + to a *.
220e0f66ef8SJohn Baldwin 	 */
221e0f66ef8SJohn Baldwin 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
2220811d60aSJohn Baldwin 	while (missed-- > 0) {
223e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
224e0f66ef8SJohn Baldwin 			if (*last == '+') {
225e0f66ef8SJohn Baldwin 				*last = '*';
226e0f66ef8SJohn Baldwin 				break;
227b4151f71SJohn Baldwin 			} else
228e0f66ef8SJohn Baldwin 				*last = '+';
229e0f66ef8SJohn Baldwin 		} else if (space) {
230e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " +");
231e0f66ef8SJohn Baldwin 			space = 0;
232e0f66ef8SJohn Baldwin 		} else
233e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, "+");
234b4151f71SJohn Baldwin 	}
235e0f66ef8SJohn Baldwin 
236e0f66ef8SJohn Baldwin 	/*
237e0f66ef8SJohn Baldwin 	 * If this event has an ithread, update it's priority and
238e0f66ef8SJohn Baldwin 	 * name.
239e0f66ef8SJohn Baldwin 	 */
240e0f66ef8SJohn Baldwin 	if (ie->ie_thread != NULL)
241e0f66ef8SJohn Baldwin 		ithread_update(ie->ie_thread);
242e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
243b4151f71SJohn Baldwin }
244b4151f71SJohn Baldwin 
245b4151f71SJohn Baldwin int
246e0f66ef8SJohn Baldwin intr_event_create(struct intr_event **event, void *source,int flags,
2471ee1b687SJohn Baldwin     void (*pre_ithread)(void *), void (*post_ithread)(void *),
2481ee1b687SJohn Baldwin     void (*post_filter)(void *), int (*assign_cpu)(void *, u_char),
2491ee1b687SJohn Baldwin     const char *fmt, ...)
250bafe5a31SPaolo Pisati {
251bafe5a31SPaolo Pisati 	struct intr_event *ie;
252bafe5a31SPaolo Pisati 	va_list ap;
253bafe5a31SPaolo Pisati 
254bafe5a31SPaolo Pisati 	/* The only valid flag during creation is IE_SOFT. */
255bafe5a31SPaolo Pisati 	if ((flags & ~IE_SOFT) != 0)
256bafe5a31SPaolo Pisati 		return (EINVAL);
257bafe5a31SPaolo Pisati 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
258bafe5a31SPaolo Pisati 	ie->ie_source = source;
2591ee1b687SJohn Baldwin 	ie->ie_pre_ithread = pre_ithread;
2601ee1b687SJohn Baldwin 	ie->ie_post_ithread = post_ithread;
2611ee1b687SJohn Baldwin 	ie->ie_post_filter = post_filter;
2626d2d1c04SJohn Baldwin 	ie->ie_assign_cpu = assign_cpu;
263bafe5a31SPaolo Pisati 	ie->ie_flags = flags;
264eaf86d16SJohn Baldwin 	ie->ie_cpu = NOCPU;
265bafe5a31SPaolo Pisati 	TAILQ_INIT(&ie->ie_handlers);
266bafe5a31SPaolo Pisati 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
267bafe5a31SPaolo Pisati 
268bafe5a31SPaolo Pisati 	va_start(ap, fmt);
269bafe5a31SPaolo Pisati 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
270bafe5a31SPaolo Pisati 	va_end(ap);
271bafe5a31SPaolo Pisati 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
272bafe5a31SPaolo Pisati 	mtx_pool_lock(mtxpool_sleep, &event_list);
273bafe5a31SPaolo Pisati 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
274bafe5a31SPaolo Pisati 	mtx_pool_unlock(mtxpool_sleep, &event_list);
275bafe5a31SPaolo Pisati 	if (event != NULL)
276bafe5a31SPaolo Pisati 		*event = ie;
277bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
278bafe5a31SPaolo Pisati 	return (0);
279bafe5a31SPaolo Pisati }
280b4151f71SJohn Baldwin 
281eaf86d16SJohn Baldwin /*
282eaf86d16SJohn Baldwin  * Bind an interrupt event to the specified CPU.  Note that not all
283eaf86d16SJohn Baldwin  * platforms support binding an interrupt to a CPU.  For those
284eaf86d16SJohn Baldwin  * platforms this request will fail.  For supported platforms, any
285eaf86d16SJohn Baldwin  * associated ithreads as well as the primary interrupt context will
286eaf86d16SJohn Baldwin  * be bound to the specificed CPU.  Using a cpu id of NOCPU unbinds
287eaf86d16SJohn Baldwin  * the interrupt event.
288eaf86d16SJohn Baldwin  */
289eaf86d16SJohn Baldwin int
290eaf86d16SJohn Baldwin intr_event_bind(struct intr_event *ie, u_char cpu)
291eaf86d16SJohn Baldwin {
292eaf86d16SJohn Baldwin 	struct thread *td;
293eaf86d16SJohn Baldwin 	int error;
294eaf86d16SJohn Baldwin 
295eaf86d16SJohn Baldwin 	/* Need a CPU to bind to. */
296eaf86d16SJohn Baldwin 	if (cpu != NOCPU && CPU_ABSENT(cpu))
297eaf86d16SJohn Baldwin 		return (EINVAL);
298eaf86d16SJohn Baldwin 
299eaf86d16SJohn Baldwin 	if (ie->ie_assign_cpu == NULL)
300eaf86d16SJohn Baldwin 		return (EOPNOTSUPP);
301eaf86d16SJohn Baldwin 
302eaf86d16SJohn Baldwin 	/* Don't allow a bind request if the interrupt is already bound. */
303eaf86d16SJohn Baldwin 	mtx_lock(&ie->ie_lock);
304eaf86d16SJohn Baldwin 	if (ie->ie_cpu != NOCPU && cpu != NOCPU) {
305eaf86d16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
306eaf86d16SJohn Baldwin 		return (EBUSY);
307eaf86d16SJohn Baldwin 	}
308eaf86d16SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
309eaf86d16SJohn Baldwin 
310eaf86d16SJohn Baldwin 	error = ie->ie_assign_cpu(ie->ie_source, cpu);
311eaf86d16SJohn Baldwin 	if (error)
312eaf86d16SJohn Baldwin 		return (error);
313eaf86d16SJohn Baldwin 	mtx_lock(&ie->ie_lock);
314eaf86d16SJohn Baldwin 	if (ie->ie_thread != NULL)
315eaf86d16SJohn Baldwin 		td = ie->ie_thread->it_thread;
316eaf86d16SJohn Baldwin 	else
317eaf86d16SJohn Baldwin 		td = NULL;
318eaf86d16SJohn Baldwin 	if (td != NULL)
319eaf86d16SJohn Baldwin 		thread_lock(td);
320eaf86d16SJohn Baldwin 	ie->ie_cpu = cpu;
321eaf86d16SJohn Baldwin 	if (td != NULL)
322eaf86d16SJohn Baldwin 		thread_unlock(td);
323eaf86d16SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
324eaf86d16SJohn Baldwin 	return (0);
325eaf86d16SJohn Baldwin }
326eaf86d16SJohn Baldwin 
327b4151f71SJohn Baldwin int
328e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie)
329b4151f71SJohn Baldwin {
330b4151f71SJohn Baldwin 
331e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
332e0f66ef8SJohn Baldwin 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
333e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
334e0f66ef8SJohn Baldwin 		return (EBUSY);
3354d29cb2dSJohn Baldwin 	}
336e0f66ef8SJohn Baldwin 	mtx_pool_lock(mtxpool_sleep, &event_list);
337e0f66ef8SJohn Baldwin 	TAILQ_REMOVE(&event_list, ie, ie_list);
338e0f66ef8SJohn Baldwin 	mtx_pool_unlock(mtxpool_sleep, &event_list);
3399477358dSJohn Baldwin #ifndef notyet
3409477358dSJohn Baldwin 	if (ie->ie_thread != NULL) {
3419477358dSJohn Baldwin 		ithread_destroy(ie->ie_thread);
3429477358dSJohn Baldwin 		ie->ie_thread = NULL;
3439477358dSJohn Baldwin 	}
3449477358dSJohn Baldwin #endif
345e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
346e0f66ef8SJohn Baldwin 	mtx_destroy(&ie->ie_lock);
347e0f66ef8SJohn Baldwin 	free(ie, M_ITHREAD);
348e0f66ef8SJohn Baldwin 	return (0);
349e0f66ef8SJohn Baldwin }
350e0f66ef8SJohn Baldwin 
351bafe5a31SPaolo Pisati #ifndef INTR_FILTER
352e0f66ef8SJohn Baldwin static struct intr_thread *
353e0f66ef8SJohn Baldwin ithread_create(const char *name)
354e0f66ef8SJohn Baldwin {
355e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
356e0f66ef8SJohn Baldwin 	struct thread *td;
357e0f66ef8SJohn Baldwin 	int error;
358e0f66ef8SJohn Baldwin 
359e0f66ef8SJohn Baldwin 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
360e0f66ef8SJohn Baldwin 
3617ab24ea3SJulian Elischer 	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
3627ab24ea3SJulian Elischer 		    &td, RFSTOPPED | RFHIGHPID,
3639ef95d01SJulian Elischer 	    	    0, "intr", "%s", name);
364e0f66ef8SJohn Baldwin 	if (error)
3653745c395SJulian Elischer 		panic("kproc_create() failed with %d", error);
366982d11f8SJeff Roberson 	thread_lock(td);
367ad1e7d28SJulian Elischer 	sched_class(td, PRI_ITHD);
368e0f66ef8SJohn Baldwin 	TD_SET_IWAIT(td);
369982d11f8SJeff Roberson 	thread_unlock(td);
370e0f66ef8SJohn Baldwin 	td->td_pflags |= TDP_ITHREAD;
371e0f66ef8SJohn Baldwin 	ithd->it_thread = td;
372e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
373e0f66ef8SJohn Baldwin 	return (ithd);
374e0f66ef8SJohn Baldwin }
375bafe5a31SPaolo Pisati #else
376bafe5a31SPaolo Pisati static struct intr_thread *
377bafe5a31SPaolo Pisati ithread_create(const char *name, struct intr_handler *ih)
378bafe5a31SPaolo Pisati {
379bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
380bafe5a31SPaolo Pisati 	struct thread *td;
381bafe5a31SPaolo Pisati 	int error;
382bafe5a31SPaolo Pisati 
383bafe5a31SPaolo Pisati 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
384bafe5a31SPaolo Pisati 
385539976ffSJulian Elischer 	error = kproc_kthread_add(ithread_loop, ih, &intrproc,
3867ab24ea3SJulian Elischer 		    &td, RFSTOPPED | RFHIGHPID,
3879ef95d01SJulian Elischer 	    	    0, "intr", "%s", name);
388bafe5a31SPaolo Pisati 	if (error)
3893745c395SJulian Elischer 		panic("kproc_create() failed with %d", error);
390982d11f8SJeff Roberson 	thread_lock(td);
391bafe5a31SPaolo Pisati 	sched_class(td, PRI_ITHD);
392bafe5a31SPaolo Pisati 	TD_SET_IWAIT(td);
393982d11f8SJeff Roberson 	thread_unlock(td);
394bafe5a31SPaolo Pisati 	td->td_pflags |= TDP_ITHREAD;
395bafe5a31SPaolo Pisati 	ithd->it_thread = td;
396bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
397bafe5a31SPaolo Pisati 	return (ithd);
398bafe5a31SPaolo Pisati }
399bafe5a31SPaolo Pisati #endif
400e0f66ef8SJohn Baldwin 
401e0f66ef8SJohn Baldwin static void
402e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread)
403e0f66ef8SJohn Baldwin {
404e0f66ef8SJohn Baldwin 	struct thread *td;
405e0f66ef8SJohn Baldwin 
406bb141be1SScott Long 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
407e0f66ef8SJohn Baldwin 	td = ithread->it_thread;
408982d11f8SJeff Roberson 	thread_lock(td);
409e0f66ef8SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
41071fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
41171fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
412f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
413b4151f71SJohn Baldwin 	}
414982d11f8SJeff Roberson 	thread_unlock(td);
415b4151f71SJohn Baldwin }
416b4151f71SJohn Baldwin 
417bafe5a31SPaolo Pisati #ifndef INTR_FILTER
418b4151f71SJohn Baldwin int
419e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name,
420ef544f63SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
421ef544f63SPaolo Pisati     enum intr_type flags, void **cookiep)
422b4151f71SJohn Baldwin {
423e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *temp_ih;
424e0f66ef8SJohn Baldwin 	struct intr_thread *it;
425b4151f71SJohn Baldwin 
426ef544f63SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
427b4151f71SJohn Baldwin 		return (EINVAL);
428b4151f71SJohn Baldwin 
429e0f66ef8SJohn Baldwin 	/* Allocate and populate an interrupt handler structure. */
430e0f66ef8SJohn Baldwin 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
431ef544f63SPaolo Pisati 	ih->ih_filter = filter;
432b4151f71SJohn Baldwin 	ih->ih_handler = handler;
433b4151f71SJohn Baldwin 	ih->ih_argument = arg;
434b4151f71SJohn Baldwin 	ih->ih_name = name;
435e0f66ef8SJohn Baldwin 	ih->ih_event = ie;
436b4151f71SJohn Baldwin 	ih->ih_pri = pri;
437ef544f63SPaolo Pisati 	if (flags & INTR_EXCL)
438b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
439b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
440b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
441b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
442b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
443b4151f71SJohn Baldwin 
444e0f66ef8SJohn Baldwin 	/* We can only have one exclusive handler in a event. */
445e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
446e0f66ef8SJohn Baldwin 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
447e0f66ef8SJohn Baldwin 		if ((flags & INTR_EXCL) ||
448e0f66ef8SJohn Baldwin 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
449e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
450b4151f71SJohn Baldwin 			free(ih, M_ITHREAD);
451b4151f71SJohn Baldwin 			return (EINVAL);
452b4151f71SJohn Baldwin 		}
453e0f66ef8SJohn Baldwin 	}
454e0f66ef8SJohn Baldwin 
455e0f66ef8SJohn Baldwin 	/* Add the new handler to the event in priority order. */
456e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
457e0f66ef8SJohn Baldwin 		if (temp_ih->ih_pri > ih->ih_pri)
458e0f66ef8SJohn Baldwin 			break;
459e0f66ef8SJohn Baldwin 	}
460e0f66ef8SJohn Baldwin 	if (temp_ih == NULL)
461e0f66ef8SJohn Baldwin 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
462e0f66ef8SJohn Baldwin 	else
463e0f66ef8SJohn Baldwin 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
464e0f66ef8SJohn Baldwin 	intr_event_update(ie);
465e0f66ef8SJohn Baldwin 
466e0f66ef8SJohn Baldwin 	/* Create a thread if we need one. */
467ef544f63SPaolo Pisati 	while (ie->ie_thread == NULL && handler != NULL) {
468e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD)
4690f180a7cSJohn Baldwin 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
470e0f66ef8SJohn Baldwin 		else {
471e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ADDING_THREAD;
472e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
473e0f66ef8SJohn Baldwin 			it = ithread_create("intr: newborn");
474e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
475e0f66ef8SJohn Baldwin 			ie->ie_flags &= ~IE_ADDING_THREAD;
476e0f66ef8SJohn Baldwin 			ie->ie_thread = it;
477e0f66ef8SJohn Baldwin 			it->it_event = ie;
478e0f66ef8SJohn Baldwin 			ithread_update(it);
479e0f66ef8SJohn Baldwin 			wakeup(ie);
480e0f66ef8SJohn Baldwin 		}
481e0f66ef8SJohn Baldwin 	}
482e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
483e0f66ef8SJohn Baldwin 	    ie->ie_name);
484e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
485e0f66ef8SJohn Baldwin 
486e0f66ef8SJohn Baldwin 	if (cookiep != NULL)
487e0f66ef8SJohn Baldwin 		*cookiep = ih;
488e0f66ef8SJohn Baldwin 	return (0);
489e0f66ef8SJohn Baldwin }
490bafe5a31SPaolo Pisati #else
491bafe5a31SPaolo Pisati int
492bafe5a31SPaolo Pisati intr_event_add_handler(struct intr_event *ie, const char *name,
493bafe5a31SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
494bafe5a31SPaolo Pisati     enum intr_type flags, void **cookiep)
495bafe5a31SPaolo Pisati {
496bafe5a31SPaolo Pisati 	struct intr_handler *ih, *temp_ih;
497bafe5a31SPaolo Pisati 	struct intr_thread *it;
498bafe5a31SPaolo Pisati 
499bafe5a31SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
500bafe5a31SPaolo Pisati 		return (EINVAL);
501bafe5a31SPaolo Pisati 
502bafe5a31SPaolo Pisati 	/* Allocate and populate an interrupt handler structure. */
503bafe5a31SPaolo Pisati 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
504bafe5a31SPaolo Pisati 	ih->ih_filter = filter;
505bafe5a31SPaolo Pisati 	ih->ih_handler = handler;
506bafe5a31SPaolo Pisati 	ih->ih_argument = arg;
507bafe5a31SPaolo Pisati 	ih->ih_name = name;
508bafe5a31SPaolo Pisati 	ih->ih_event = ie;
509bafe5a31SPaolo Pisati 	ih->ih_pri = pri;
510bafe5a31SPaolo Pisati 	if (flags & INTR_EXCL)
511bafe5a31SPaolo Pisati 		ih->ih_flags = IH_EXCLUSIVE;
512bafe5a31SPaolo Pisati 	if (flags & INTR_MPSAFE)
513bafe5a31SPaolo Pisati 		ih->ih_flags |= IH_MPSAFE;
514bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
515bafe5a31SPaolo Pisati 		ih->ih_flags |= IH_ENTROPY;
516bafe5a31SPaolo Pisati 
517bafe5a31SPaolo Pisati 	/* We can only have one exclusive handler in a event. */
518bafe5a31SPaolo Pisati 	mtx_lock(&ie->ie_lock);
519bafe5a31SPaolo Pisati 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
520bafe5a31SPaolo Pisati 		if ((flags & INTR_EXCL) ||
521bafe5a31SPaolo Pisati 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
522bafe5a31SPaolo Pisati 			mtx_unlock(&ie->ie_lock);
523bafe5a31SPaolo Pisati 			free(ih, M_ITHREAD);
524bafe5a31SPaolo Pisati 			return (EINVAL);
525bafe5a31SPaolo Pisati 		}
526bafe5a31SPaolo Pisati 	}
527bafe5a31SPaolo Pisati 
528bafe5a31SPaolo Pisati 	/* Add the new handler to the event in priority order. */
529bafe5a31SPaolo Pisati 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
530bafe5a31SPaolo Pisati 		if (temp_ih->ih_pri > ih->ih_pri)
531bafe5a31SPaolo Pisati 			break;
532bafe5a31SPaolo Pisati 	}
533bafe5a31SPaolo Pisati 	if (temp_ih == NULL)
534bafe5a31SPaolo Pisati 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
535bafe5a31SPaolo Pisati 	else
536bafe5a31SPaolo Pisati 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
537bafe5a31SPaolo Pisati 	intr_event_update(ie);
538bafe5a31SPaolo Pisati 
539bafe5a31SPaolo Pisati 	/* For filtered handlers, create a private ithread to run on. */
540bafe5a31SPaolo Pisati 	if (filter != NULL && handler != NULL) {
541bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
542bafe5a31SPaolo Pisati 		it = ithread_create("intr: newborn", ih);
543bafe5a31SPaolo Pisati 		mtx_lock(&ie->ie_lock);
544bafe5a31SPaolo Pisati 		it->it_event = ie;
545bafe5a31SPaolo Pisati 		ih->ih_thread = it;
546bafe5a31SPaolo Pisati 		ithread_update(it); // XXX - do we really need this?!?!?
547bafe5a31SPaolo Pisati 	} else { /* Create the global per-event thread if we need one. */
548bafe5a31SPaolo Pisati 		while (ie->ie_thread == NULL && handler != NULL) {
549bafe5a31SPaolo Pisati 			if (ie->ie_flags & IE_ADDING_THREAD)
550bafe5a31SPaolo Pisati 				msleep(ie, &ie->ie_lock, 0, "ithread", 0);
551bafe5a31SPaolo Pisati 			else {
552bafe5a31SPaolo Pisati 				ie->ie_flags |= IE_ADDING_THREAD;
553bafe5a31SPaolo Pisati 				mtx_unlock(&ie->ie_lock);
554bafe5a31SPaolo Pisati 				it = ithread_create("intr: newborn", ih);
555bafe5a31SPaolo Pisati 				mtx_lock(&ie->ie_lock);
556bafe5a31SPaolo Pisati 				ie->ie_flags &= ~IE_ADDING_THREAD;
557bafe5a31SPaolo Pisati 				ie->ie_thread = it;
558bafe5a31SPaolo Pisati 				it->it_event = ie;
559bafe5a31SPaolo Pisati 				ithread_update(it);
560bafe5a31SPaolo Pisati 				wakeup(ie);
561bafe5a31SPaolo Pisati 			}
562bafe5a31SPaolo Pisati 		}
563bafe5a31SPaolo Pisati 	}
564bafe5a31SPaolo Pisati 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
565bafe5a31SPaolo Pisati 	    ie->ie_name);
566bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
567bafe5a31SPaolo Pisati 
568bafe5a31SPaolo Pisati 	if (cookiep != NULL)
569bafe5a31SPaolo Pisati 		*cookiep = ih;
570bafe5a31SPaolo Pisati 	return (0);
571bafe5a31SPaolo Pisati }
572bafe5a31SPaolo Pisati #endif
573b4151f71SJohn Baldwin 
574c3045318SJohn Baldwin /*
575c3045318SJohn Baldwin  * Return the ie_source field from the intr_event an intr_handler is
576c3045318SJohn Baldwin  * associated with.
577c3045318SJohn Baldwin  */
578c3045318SJohn Baldwin void *
579c3045318SJohn Baldwin intr_handler_source(void *cookie)
580c3045318SJohn Baldwin {
581c3045318SJohn Baldwin 	struct intr_handler *ih;
582c3045318SJohn Baldwin 	struct intr_event *ie;
583c3045318SJohn Baldwin 
584c3045318SJohn Baldwin 	ih = (struct intr_handler *)cookie;
585c3045318SJohn Baldwin 	if (ih == NULL)
586c3045318SJohn Baldwin 		return (NULL);
587c3045318SJohn Baldwin 	ie = ih->ih_event;
588c3045318SJohn Baldwin 	KASSERT(ie != NULL,
589c3045318SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
590c3045318SJohn Baldwin 	    ih->ih_name));
591c3045318SJohn Baldwin 	return (ie->ie_source);
592c3045318SJohn Baldwin }
593c3045318SJohn Baldwin 
594bafe5a31SPaolo Pisati #ifndef INTR_FILTER
595b4151f71SJohn Baldwin int
596e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie)
597b4151f71SJohn Baldwin {
598e0f66ef8SJohn Baldwin 	struct intr_handler *handler = (struct intr_handler *)cookie;
599e0f66ef8SJohn Baldwin 	struct intr_event *ie;
600b4151f71SJohn Baldwin #ifdef INVARIANTS
601e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
602e0f66ef8SJohn Baldwin #endif
603e0f66ef8SJohn Baldwin #ifdef notyet
604e0f66ef8SJohn Baldwin 	int dead;
605b4151f71SJohn Baldwin #endif
606b4151f71SJohn Baldwin 
6073e5da754SJohn Baldwin 	if (handler == NULL)
608b4151f71SJohn Baldwin 		return (EINVAL);
609e0f66ef8SJohn Baldwin 	ie = handler->ih_event;
610e0f66ef8SJohn Baldwin 	KASSERT(ie != NULL,
611e0f66ef8SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
6123e5da754SJohn Baldwin 	    handler->ih_name));
613e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
61491f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
615e0f66ef8SJohn Baldwin 	    ie->ie_name);
616b4151f71SJohn Baldwin #ifdef INVARIANTS
617e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
6183e5da754SJohn Baldwin 		if (ih == handler)
6193e5da754SJohn Baldwin 			goto ok;
620e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
621e0f66ef8SJohn Baldwin 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
622e0f66ef8SJohn Baldwin 	    ih->ih_name, ie->ie_name);
6233e5da754SJohn Baldwin ok:
624b4151f71SJohn Baldwin #endif
625de271f01SJohn Baldwin 	/*
626e0f66ef8SJohn Baldwin 	 * If there is no ithread, then just remove the handler and return.
627e0f66ef8SJohn Baldwin 	 * XXX: Note that an INTR_FAST handler might be running on another
628e0f66ef8SJohn Baldwin 	 * CPU!
629e0f66ef8SJohn Baldwin 	 */
630e0f66ef8SJohn Baldwin 	if (ie->ie_thread == NULL) {
631e0f66ef8SJohn Baldwin 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
632e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
633e0f66ef8SJohn Baldwin 		free(handler, M_ITHREAD);
634e0f66ef8SJohn Baldwin 		return (0);
635e0f66ef8SJohn Baldwin 	}
636e0f66ef8SJohn Baldwin 
637e0f66ef8SJohn Baldwin 	/*
638de271f01SJohn Baldwin 	 * If the interrupt thread is already running, then just mark this
639de271f01SJohn Baldwin 	 * handler as being dead and let the ithread do the actual removal.
640288e351bSDon Lewis 	 *
641288e351bSDon Lewis 	 * During a cold boot while cold is set, msleep() does not sleep,
642288e351bSDon Lewis 	 * so we have to remove the handler here rather than letting the
643288e351bSDon Lewis 	 * thread do it.
644de271f01SJohn Baldwin 	 */
645982d11f8SJeff Roberson 	thread_lock(ie->ie_thread->it_thread);
646e0f66ef8SJohn Baldwin 	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
647de271f01SJohn Baldwin 		handler->ih_flags |= IH_DEAD;
648de271f01SJohn Baldwin 
649de271f01SJohn Baldwin 		/*
650de271f01SJohn Baldwin 		 * Ensure that the thread will process the handler list
651de271f01SJohn Baldwin 		 * again and remove this handler if it has already passed
652de271f01SJohn Baldwin 		 * it on the list.
653de271f01SJohn Baldwin 		 */
654e0f66ef8SJohn Baldwin 		ie->ie_thread->it_need = 1;
6554d29cb2dSJohn Baldwin 	} else
656e0f66ef8SJohn Baldwin 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
657982d11f8SJeff Roberson 	thread_unlock(ie->ie_thread->it_thread);
658e0f66ef8SJohn Baldwin 	while (handler->ih_flags & IH_DEAD)
6590f180a7cSJohn Baldwin 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
660e0f66ef8SJohn Baldwin 	intr_event_update(ie);
661e0f66ef8SJohn Baldwin #ifdef notyet
662e0f66ef8SJohn Baldwin 	/*
663e0f66ef8SJohn Baldwin 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
664e0f66ef8SJohn Baldwin 	 * this could lead to races of stale data when servicing an
665e0f66ef8SJohn Baldwin 	 * interrupt.
666e0f66ef8SJohn Baldwin 	 */
667e0f66ef8SJohn Baldwin 	dead = 1;
668e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
669e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_FAST)) {
670e0f66ef8SJohn Baldwin 			dead = 0;
671e0f66ef8SJohn Baldwin 			break;
672e0f66ef8SJohn Baldwin 		}
673e0f66ef8SJohn Baldwin 	}
674e0f66ef8SJohn Baldwin 	if (dead) {
675e0f66ef8SJohn Baldwin 		ithread_destroy(ie->ie_thread);
676e0f66ef8SJohn Baldwin 		ie->ie_thread = NULL;
677e0f66ef8SJohn Baldwin 	}
678e0f66ef8SJohn Baldwin #endif
679e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
680b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
681b4151f71SJohn Baldwin 	return (0);
682b4151f71SJohn Baldwin }
683b4151f71SJohn Baldwin 
6841ee1b687SJohn Baldwin static int
685e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie)
6863e5da754SJohn Baldwin {
687e0f66ef8SJohn Baldwin 	struct intr_entropy entropy;
688e0f66ef8SJohn Baldwin 	struct intr_thread *it;
689b40ce416SJulian Elischer 	struct thread *td;
69004774f23SJulian Elischer 	struct thread *ctd;
6913e5da754SJohn Baldwin 	struct proc *p;
6923e5da754SJohn Baldwin 
6933e5da754SJohn Baldwin 	/*
6943e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
6953e5da754SJohn Baldwin 	 */
696e0f66ef8SJohn Baldwin 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
697e0f66ef8SJohn Baldwin 	    ie->ie_thread == NULL)
6983e5da754SJohn Baldwin 		return (EINVAL);
6993e5da754SJohn Baldwin 
70004774f23SJulian Elischer 	ctd = curthread;
701e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
702e0f66ef8SJohn Baldwin 	td = it->it_thread;
7036f40c417SRobert Watson 	p = td->td_proc;
704e0f66ef8SJohn Baldwin 
7053e5da754SJohn Baldwin 	/*
7063e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
7073e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
7083e5da754SJohn Baldwin 	 */
709e0f66ef8SJohn Baldwin 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
7106f40c417SRobert Watson 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
7117ab24ea3SJulian Elischer 		    p->p_pid, td->td_name);
712e0f66ef8SJohn Baldwin 		entropy.event = (uintptr_t)ie;
713e0f66ef8SJohn Baldwin 		entropy.td = ctd;
7143e5da754SJohn Baldwin 		random_harvest(&entropy, sizeof(entropy), 2, 0,
7153e5da754SJohn Baldwin 		    RANDOM_INTERRUPT);
7163e5da754SJohn Baldwin 	}
7173e5da754SJohn Baldwin 
718e0f66ef8SJohn Baldwin 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
7193e5da754SJohn Baldwin 
7203e5da754SJohn Baldwin 	/*
7213e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
722982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
723982d11f8SJeff Roberson 	 * put it on the runqueue.
7243e5da754SJohn Baldwin 	 */
725e0f66ef8SJohn Baldwin 	it->it_need = 1;
726982d11f8SJeff Roberson 	thread_lock(td);
72771fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
728e0f66ef8SJohn Baldwin 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
7297ab24ea3SJulian Elischer 		    td->td_name);
73071fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
731f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
7323e5da754SJohn Baldwin 	} else {
733e0f66ef8SJohn Baldwin 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
7347ab24ea3SJulian Elischer 		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
7353e5da754SJohn Baldwin 	}
736982d11f8SJeff Roberson 	thread_unlock(td);
7373e5da754SJohn Baldwin 
7383e5da754SJohn Baldwin 	return (0);
7393e5da754SJohn Baldwin }
740bafe5a31SPaolo Pisati #else
741bafe5a31SPaolo Pisati int
742bafe5a31SPaolo Pisati intr_event_remove_handler(void *cookie)
743bafe5a31SPaolo Pisati {
744bafe5a31SPaolo Pisati 	struct intr_handler *handler = (struct intr_handler *)cookie;
745bafe5a31SPaolo Pisati 	struct intr_event *ie;
746bafe5a31SPaolo Pisati 	struct intr_thread *it;
747bafe5a31SPaolo Pisati #ifdef INVARIANTS
748bafe5a31SPaolo Pisati 	struct intr_handler *ih;
749bafe5a31SPaolo Pisati #endif
750bafe5a31SPaolo Pisati #ifdef notyet
751bafe5a31SPaolo Pisati 	int dead;
752bafe5a31SPaolo Pisati #endif
753bafe5a31SPaolo Pisati 
754bafe5a31SPaolo Pisati 	if (handler == NULL)
755bafe5a31SPaolo Pisati 		return (EINVAL);
756bafe5a31SPaolo Pisati 	ie = handler->ih_event;
757bafe5a31SPaolo Pisati 	KASSERT(ie != NULL,
758bafe5a31SPaolo Pisati 	    ("interrupt handler \"%s\" has a NULL interrupt event",
759bafe5a31SPaolo Pisati 	    handler->ih_name));
760bafe5a31SPaolo Pisati 	mtx_lock(&ie->ie_lock);
761bafe5a31SPaolo Pisati 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
762bafe5a31SPaolo Pisati 	    ie->ie_name);
763bafe5a31SPaolo Pisati #ifdef INVARIANTS
764bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
765bafe5a31SPaolo Pisati 		if (ih == handler)
766bafe5a31SPaolo Pisati 			goto ok;
767bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
768bafe5a31SPaolo Pisati 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
769bafe5a31SPaolo Pisati 	    ih->ih_name, ie->ie_name);
770bafe5a31SPaolo Pisati ok:
771bafe5a31SPaolo Pisati #endif
772bafe5a31SPaolo Pisati 	/*
773bafe5a31SPaolo Pisati 	 * If there are no ithreads (per event and per handler), then
774bafe5a31SPaolo Pisati 	 * just remove the handler and return.
775bafe5a31SPaolo Pisati 	 * XXX: Note that an INTR_FAST handler might be running on another CPU!
776bafe5a31SPaolo Pisati 	 */
777bafe5a31SPaolo Pisati 	if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
778bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
779bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
780bafe5a31SPaolo Pisati 		free(handler, M_ITHREAD);
781bafe5a31SPaolo Pisati 		return (0);
782bafe5a31SPaolo Pisati 	}
783bafe5a31SPaolo Pisati 
784bafe5a31SPaolo Pisati 	/* Private or global ithread? */
785bafe5a31SPaolo Pisati 	it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
786bafe5a31SPaolo Pisati 	/*
787bafe5a31SPaolo Pisati 	 * If the interrupt thread is already running, then just mark this
788bafe5a31SPaolo Pisati 	 * handler as being dead and let the ithread do the actual removal.
789bafe5a31SPaolo Pisati 	 *
790bafe5a31SPaolo Pisati 	 * During a cold boot while cold is set, msleep() does not sleep,
791bafe5a31SPaolo Pisati 	 * so we have to remove the handler here rather than letting the
792bafe5a31SPaolo Pisati 	 * thread do it.
793bafe5a31SPaolo Pisati 	 */
794982d11f8SJeff Roberson 	thread_lock(it->it_thread);
795bafe5a31SPaolo Pisati 	if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
796bafe5a31SPaolo Pisati 		handler->ih_flags |= IH_DEAD;
797bafe5a31SPaolo Pisati 
798bafe5a31SPaolo Pisati 		/*
799bafe5a31SPaolo Pisati 		 * Ensure that the thread will process the handler list
800bafe5a31SPaolo Pisati 		 * again and remove this handler if it has already passed
801bafe5a31SPaolo Pisati 		 * it on the list.
802bafe5a31SPaolo Pisati 		 */
803bafe5a31SPaolo Pisati 		it->it_need = 1;
804bafe5a31SPaolo Pisati 	} else
805bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
806982d11f8SJeff Roberson 	thread_unlock(it->it_thread);
807bafe5a31SPaolo Pisati 	while (handler->ih_flags & IH_DEAD)
808bafe5a31SPaolo Pisati 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
809bafe5a31SPaolo Pisati 	/*
810bafe5a31SPaolo Pisati 	 * At this point, the handler has been disconnected from the event,
811bafe5a31SPaolo Pisati 	 * so we can kill the private ithread if any.
812bafe5a31SPaolo Pisati 	 */
813bafe5a31SPaolo Pisati 	if (handler->ih_thread) {
814bafe5a31SPaolo Pisati 		ithread_destroy(handler->ih_thread);
815bafe5a31SPaolo Pisati 		handler->ih_thread = NULL;
816bafe5a31SPaolo Pisati 	}
817bafe5a31SPaolo Pisati 	intr_event_update(ie);
818bafe5a31SPaolo Pisati #ifdef notyet
819bafe5a31SPaolo Pisati 	/*
820bafe5a31SPaolo Pisati 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
821bafe5a31SPaolo Pisati 	 * this could lead to races of stale data when servicing an
822bafe5a31SPaolo Pisati 	 * interrupt.
823bafe5a31SPaolo Pisati 	 */
824bafe5a31SPaolo Pisati 	dead = 1;
825bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
826bafe5a31SPaolo Pisati 		if (handler != NULL) {
827bafe5a31SPaolo Pisati 			dead = 0;
828bafe5a31SPaolo Pisati 			break;
829bafe5a31SPaolo Pisati 		}
830bafe5a31SPaolo Pisati 	}
831bafe5a31SPaolo Pisati 	if (dead) {
832bafe5a31SPaolo Pisati 		ithread_destroy(ie->ie_thread);
833bafe5a31SPaolo Pisati 		ie->ie_thread = NULL;
834bafe5a31SPaolo Pisati 	}
835bafe5a31SPaolo Pisati #endif
836bafe5a31SPaolo Pisati 	mtx_unlock(&ie->ie_lock);
837bafe5a31SPaolo Pisati 	free(handler, M_ITHREAD);
838bafe5a31SPaolo Pisati 	return (0);
839bafe5a31SPaolo Pisati }
840bafe5a31SPaolo Pisati 
8411ee1b687SJohn Baldwin static int
842bafe5a31SPaolo Pisati intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
843bafe5a31SPaolo Pisati {
844bafe5a31SPaolo Pisati 	struct intr_entropy entropy;
845bafe5a31SPaolo Pisati 	struct thread *td;
846bafe5a31SPaolo Pisati 	struct thread *ctd;
847bafe5a31SPaolo Pisati 	struct proc *p;
848bafe5a31SPaolo Pisati 
849bafe5a31SPaolo Pisati 	/*
850bafe5a31SPaolo Pisati 	 * If no ithread or no handlers, then we have a stray interrupt.
851bafe5a31SPaolo Pisati 	 */
852bafe5a31SPaolo Pisati 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
853bafe5a31SPaolo Pisati 		return (EINVAL);
854bafe5a31SPaolo Pisati 
855bafe5a31SPaolo Pisati 	ctd = curthread;
856bafe5a31SPaolo Pisati 	td = it->it_thread;
857bafe5a31SPaolo Pisati 	p = td->td_proc;
858bafe5a31SPaolo Pisati 
859bafe5a31SPaolo Pisati 	/*
860bafe5a31SPaolo Pisati 	 * If any of the handlers for this ithread claim to be good
861bafe5a31SPaolo Pisati 	 * sources of entropy, then gather some.
862bafe5a31SPaolo Pisati 	 */
863bafe5a31SPaolo Pisati 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
864bafe5a31SPaolo Pisati 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
8657ab24ea3SJulian Elischer 		    p->p_pid, td->td_name);
866bafe5a31SPaolo Pisati 		entropy.event = (uintptr_t)ie;
867bafe5a31SPaolo Pisati 		entropy.td = ctd;
868bafe5a31SPaolo Pisati 		random_harvest(&entropy, sizeof(entropy), 2, 0,
869bafe5a31SPaolo Pisati 		    RANDOM_INTERRUPT);
870bafe5a31SPaolo Pisati 	}
871bafe5a31SPaolo Pisati 
872bafe5a31SPaolo Pisati 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
873bafe5a31SPaolo Pisati 
874bafe5a31SPaolo Pisati 	/*
875bafe5a31SPaolo Pisati 	 * Set it_need to tell the thread to keep running if it is already
876982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
877982d11f8SJeff Roberson 	 * put it on the runqueue.
878bafe5a31SPaolo Pisati 	 */
879bafe5a31SPaolo Pisati 	it->it_need = 1;
880982d11f8SJeff Roberson 	thread_lock(td);
881bafe5a31SPaolo Pisati 	if (TD_AWAITING_INTR(td)) {
882bafe5a31SPaolo Pisati 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
8833c1ffc32SJulian Elischer 		    td->td_name);
884bafe5a31SPaolo Pisati 		TD_CLR_IWAIT(td);
885bafe5a31SPaolo Pisati 		sched_add(td, SRQ_INTR);
886bafe5a31SPaolo Pisati 	} else {
887bafe5a31SPaolo Pisati 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
8887ab24ea3SJulian Elischer 		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
889bafe5a31SPaolo Pisati 	}
890982d11f8SJeff Roberson 	thread_unlock(td);
891bafe5a31SPaolo Pisati 
892bafe5a31SPaolo Pisati 	return (0);
893bafe5a31SPaolo Pisati }
894bafe5a31SPaolo Pisati #endif
8953e5da754SJohn Baldwin 
896fe486a37SJohn Baldwin /*
897fe486a37SJohn Baldwin  * Add a software interrupt handler to a specified event.  If a given event
898fe486a37SJohn Baldwin  * is not specified, then a new event is created.
899fe486a37SJohn Baldwin  */
9003e5da754SJohn Baldwin int
901e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
902b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
9038088699fSJohn Baldwin {
904e0f66ef8SJohn Baldwin 	struct intr_event *ie;
905b4151f71SJohn Baldwin 	int error;
9068088699fSJohn Baldwin 
907bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
9083e5da754SJohn Baldwin 		return (EINVAL);
9093e5da754SJohn Baldwin 
910e0f66ef8SJohn Baldwin 	ie = (eventp != NULL) ? *eventp : NULL;
9118088699fSJohn Baldwin 
912e0f66ef8SJohn Baldwin 	if (ie != NULL) {
913e0f66ef8SJohn Baldwin 		if (!(ie->ie_flags & IE_SOFT))
9143e5da754SJohn Baldwin 			return (EINVAL);
9153e5da754SJohn Baldwin 	} else {
916bafe5a31SPaolo Pisati 		error = intr_event_create(&ie, NULL, IE_SOFT,
917eaf86d16SJohn Baldwin 		    NULL, NULL, NULL, NULL, "swi%d:", pri);
9188088699fSJohn Baldwin 		if (error)
919b4151f71SJohn Baldwin 			return (error);
920e0f66ef8SJohn Baldwin 		if (eventp != NULL)
921e0f66ef8SJohn Baldwin 			*eventp = ie;
9228088699fSJohn Baldwin 	}
9238d809d50SJeff Roberson 	error = intr_event_add_handler(ie, name, NULL, handler, arg,
9248d809d50SJeff Roberson 	    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep);
9258d809d50SJeff Roberson 	if (error)
9268d809d50SJeff Roberson 		return (error);
9278d809d50SJeff Roberson 	if (pri == SWI_CLOCK) {
9288d809d50SJeff Roberson 		struct proc *p;
9298d809d50SJeff Roberson 		p = ie->ie_thread->it_thread->td_proc;
9308d809d50SJeff Roberson 		PROC_LOCK(p);
9318d809d50SJeff Roberson 		p->p_flag |= P_NOLOAD;
9328d809d50SJeff Roberson 		PROC_UNLOCK(p);
9338d809d50SJeff Roberson 	}
9348d809d50SJeff Roberson 	return (0);
9358088699fSJohn Baldwin }
9368088699fSJohn Baldwin 
9371931cf94SJohn Baldwin /*
938e0f66ef8SJohn Baldwin  * Schedule a software interrupt thread.
9391931cf94SJohn Baldwin  */
9401931cf94SJohn Baldwin void
941b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
9421931cf94SJohn Baldwin {
943e0f66ef8SJohn Baldwin 	struct intr_handler *ih = (struct intr_handler *)cookie;
944e0f66ef8SJohn Baldwin 	struct intr_event *ie = ih->ih_event;
9453e5da754SJohn Baldwin 	int error;
9468088699fSJohn Baldwin 
947e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
948e0f66ef8SJohn Baldwin 	    ih->ih_need);
9491931cf94SJohn Baldwin 
9501931cf94SJohn Baldwin 	/*
9513e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
9523e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
9533e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
9541931cf94SJohn Baldwin 	 */
955b4151f71SJohn Baldwin 	atomic_store_rel_int(&ih->ih_need, 1);
9561ca2c018SBruce Evans 
957b4151f71SJohn Baldwin 	if (!(flags & SWI_DELAY)) {
95867596082SAttilio Rao 		PCPU_INC(cnt.v_soft);
959bafe5a31SPaolo Pisati #ifdef INTR_FILTER
960bafe5a31SPaolo Pisati 		error = intr_event_schedule_thread(ie, ie->ie_thread);
961bafe5a31SPaolo Pisati #else
962e0f66ef8SJohn Baldwin 		error = intr_event_schedule_thread(ie);
963bafe5a31SPaolo Pisati #endif
9643e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
9658088699fSJohn Baldwin 	}
9668088699fSJohn Baldwin }
9678088699fSJohn Baldwin 
968fe486a37SJohn Baldwin /*
969fe486a37SJohn Baldwin  * Remove a software interrupt handler.  Currently this code does not
970fe486a37SJohn Baldwin  * remove the associated interrupt event if it becomes empty.  Calling code
971fe486a37SJohn Baldwin  * may do so manually via intr_event_destroy(), but that's not really
972fe486a37SJohn Baldwin  * an optimal interface.
973fe486a37SJohn Baldwin  */
974fe486a37SJohn Baldwin int
975fe486a37SJohn Baldwin swi_remove(void *cookie)
976fe486a37SJohn Baldwin {
977fe486a37SJohn Baldwin 
978fe486a37SJohn Baldwin 	return (intr_event_remove_handler(cookie));
979fe486a37SJohn Baldwin }
980fe486a37SJohn Baldwin 
981bafe5a31SPaolo Pisati #ifdef INTR_FILTER
982bafe5a31SPaolo Pisati static void
983bafe5a31SPaolo Pisati priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
984bafe5a31SPaolo Pisati {
985bafe5a31SPaolo Pisati 	struct intr_event *ie;
986bafe5a31SPaolo Pisati 
987bafe5a31SPaolo Pisati 	ie = ih->ih_event;
988bafe5a31SPaolo Pisati 	/*
989bafe5a31SPaolo Pisati 	 * If this handler is marked for death, remove it from
990bafe5a31SPaolo Pisati 	 * the list of handlers and wake up the sleeper.
991bafe5a31SPaolo Pisati 	 */
992bafe5a31SPaolo Pisati 	if (ih->ih_flags & IH_DEAD) {
993bafe5a31SPaolo Pisati 		mtx_lock(&ie->ie_lock);
994bafe5a31SPaolo Pisati 		TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
995bafe5a31SPaolo Pisati 		ih->ih_flags &= ~IH_DEAD;
996bafe5a31SPaolo Pisati 		wakeup(ih);
997bafe5a31SPaolo Pisati 		mtx_unlock(&ie->ie_lock);
998bafe5a31SPaolo Pisati 		return;
999bafe5a31SPaolo Pisati 	}
1000bafe5a31SPaolo Pisati 
1001bafe5a31SPaolo Pisati 	/* Execute this handler. */
1002bafe5a31SPaolo Pisati 	CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1003bafe5a31SPaolo Pisati 	     __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1004bafe5a31SPaolo Pisati 	     ih->ih_name, ih->ih_flags);
1005bafe5a31SPaolo Pisati 
1006bafe5a31SPaolo Pisati 	if (!(ih->ih_flags & IH_MPSAFE))
1007bafe5a31SPaolo Pisati 		mtx_lock(&Giant);
1008bafe5a31SPaolo Pisati 	ih->ih_handler(ih->ih_argument);
1009bafe5a31SPaolo Pisati 	if (!(ih->ih_flags & IH_MPSAFE))
1010bafe5a31SPaolo Pisati 		mtx_unlock(&Giant);
1011bafe5a31SPaolo Pisati }
1012bafe5a31SPaolo Pisati #endif
1013bafe5a31SPaolo Pisati 
1014e0f66ef8SJohn Baldwin static void
1015e0f66ef8SJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1016e0f66ef8SJohn Baldwin {
1017e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *ihn;
1018e0f66ef8SJohn Baldwin 
1019e0f66ef8SJohn Baldwin 	/* Interrupt handlers should not sleep. */
1020e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
1021e0f66ef8SJohn Baldwin 		THREAD_NO_SLEEPING();
1022e0f66ef8SJohn Baldwin 	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1023e0f66ef8SJohn Baldwin 
1024e0f66ef8SJohn Baldwin 		/*
1025e0f66ef8SJohn Baldwin 		 * If this handler is marked for death, remove it from
1026e0f66ef8SJohn Baldwin 		 * the list of handlers and wake up the sleeper.
1027e0f66ef8SJohn Baldwin 		 */
1028e0f66ef8SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
1029e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
1030e0f66ef8SJohn Baldwin 			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1031e0f66ef8SJohn Baldwin 			ih->ih_flags &= ~IH_DEAD;
1032e0f66ef8SJohn Baldwin 			wakeup(ih);
1033e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
1034e0f66ef8SJohn Baldwin 			continue;
1035e0f66ef8SJohn Baldwin 		}
1036e0f66ef8SJohn Baldwin 
1037f2d619c8SPaolo Pisati 		/* Skip filter only handlers */
1038f2d619c8SPaolo Pisati 		if (ih->ih_handler == NULL)
1039f2d619c8SPaolo Pisati 			continue;
1040f2d619c8SPaolo Pisati 
1041e0f66ef8SJohn Baldwin 		/*
1042e0f66ef8SJohn Baldwin 		 * For software interrupt threads, we only execute
1043e0f66ef8SJohn Baldwin 		 * handlers that have their need flag set.  Hardware
1044e0f66ef8SJohn Baldwin 		 * interrupt threads always invoke all of their handlers.
1045e0f66ef8SJohn Baldwin 		 */
1046e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
1047e0f66ef8SJohn Baldwin 			if (!ih->ih_need)
1048e0f66ef8SJohn Baldwin 				continue;
1049e0f66ef8SJohn Baldwin 			else
1050e0f66ef8SJohn Baldwin 				atomic_store_rel_int(&ih->ih_need, 0);
1051e0f66ef8SJohn Baldwin 		}
1052e0f66ef8SJohn Baldwin 
1053e0f66ef8SJohn Baldwin 		/* Execute this handler. */
1054e0f66ef8SJohn Baldwin 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1055bafe5a31SPaolo Pisati 		    __func__, p->p_pid, (void *)ih->ih_handler,
1056bafe5a31SPaolo Pisati 		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1057e0f66ef8SJohn Baldwin 
1058e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1059e0f66ef8SJohn Baldwin 			mtx_lock(&Giant);
1060e0f66ef8SJohn Baldwin 		ih->ih_handler(ih->ih_argument);
1061e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1062e0f66ef8SJohn Baldwin 			mtx_unlock(&Giant);
1063e0f66ef8SJohn Baldwin 	}
1064e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
1065e0f66ef8SJohn Baldwin 		THREAD_SLEEPING_OK();
1066e0f66ef8SJohn Baldwin 
1067e0f66ef8SJohn Baldwin 	/*
1068e0f66ef8SJohn Baldwin 	 * Interrupt storm handling:
1069e0f66ef8SJohn Baldwin 	 *
1070e0f66ef8SJohn Baldwin 	 * If this interrupt source is currently storming, then throttle
1071e0f66ef8SJohn Baldwin 	 * it to only fire the handler once  per clock tick.
1072e0f66ef8SJohn Baldwin 	 *
1073e0f66ef8SJohn Baldwin 	 * If this interrupt source is not currently storming, but the
1074e0f66ef8SJohn Baldwin 	 * number of back to back interrupts exceeds the storm threshold,
1075e0f66ef8SJohn Baldwin 	 * then enter storming mode.
1076e0f66ef8SJohn Baldwin 	 */
1077e41bcf3cSJohn Baldwin 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1078e41bcf3cSJohn Baldwin 	    !(ie->ie_flags & IE_SOFT)) {
10790ae62c18SNate Lawson 		/* Report the message only once every second. */
10800ae62c18SNate Lawson 		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1081e0f66ef8SJohn Baldwin 			printf(
10820ae62c18SNate Lawson 	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1083e0f66ef8SJohn Baldwin 			    ie->ie_name);
1084e0f66ef8SJohn Baldwin 		}
1085e41bcf3cSJohn Baldwin 		pause("istorm", 1);
1086e0f66ef8SJohn Baldwin 	} else
1087e0f66ef8SJohn Baldwin 		ie->ie_count++;
1088e0f66ef8SJohn Baldwin 
1089e0f66ef8SJohn Baldwin 	/*
1090e0f66ef8SJohn Baldwin 	 * Now that all the handlers have had a chance to run, reenable
1091e0f66ef8SJohn Baldwin 	 * the interrupt source.
1092e0f66ef8SJohn Baldwin 	 */
10931ee1b687SJohn Baldwin 	if (ie->ie_post_ithread != NULL)
10941ee1b687SJohn Baldwin 		ie->ie_post_ithread(ie->ie_source);
1095e0f66ef8SJohn Baldwin }
1096e0f66ef8SJohn Baldwin 
1097bafe5a31SPaolo Pisati #ifndef INTR_FILTER
10988088699fSJohn Baldwin /*
1099b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
11008088699fSJohn Baldwin  */
110137c84183SPoul-Henning Kamp static void
1102b4151f71SJohn Baldwin ithread_loop(void *arg)
11038088699fSJohn Baldwin {
1104e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
1105e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1106b40ce416SJulian Elischer 	struct thread *td;
1107b4151f71SJohn Baldwin 	struct proc *p;
1108eaf86d16SJohn Baldwin 	u_char cpu;
11098088699fSJohn Baldwin 
1110b40ce416SJulian Elischer 	td = curthread;
1111b40ce416SJulian Elischer 	p = td->td_proc;
1112e0f66ef8SJohn Baldwin 	ithd = (struct intr_thread *)arg;
1113e0f66ef8SJohn Baldwin 	KASSERT(ithd->it_thread == td,
111491f91617SDavid E. O'Brien 	    ("%s: ithread and proc linkage out of sync", __func__));
1115e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
1116e0f66ef8SJohn Baldwin 	ie->ie_count = 0;
1117eaf86d16SJohn Baldwin 	cpu = NOCPU;
11188088699fSJohn Baldwin 
11198088699fSJohn Baldwin 	/*
11208088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
11218088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
11228088699fSJohn Baldwin 	 */
11238088699fSJohn Baldwin 	for (;;) {
1124b4151f71SJohn Baldwin 		/*
1125b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
1126b4151f71SJohn Baldwin 		 */
1127b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
1128e0f66ef8SJohn Baldwin 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
11297ab24ea3SJulian Elischer 			    p->p_pid, td->td_name);
1130b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
1131ca9a0ddfSJulian Elischer 			kthread_exit();
1132b4151f71SJohn Baldwin 		}
1133b4151f71SJohn Baldwin 
1134e0f66ef8SJohn Baldwin 		/*
1135e0f66ef8SJohn Baldwin 		 * Service interrupts.  If another interrupt arrives while
1136e0f66ef8SJohn Baldwin 		 * we are running, it will set it_need to note that we
1137e0f66ef8SJohn Baldwin 		 * should make another pass.
1138e0f66ef8SJohn Baldwin 		 */
1139b4151f71SJohn Baldwin 		while (ithd->it_need) {
11408088699fSJohn Baldwin 			/*
1141e0f66ef8SJohn Baldwin 			 * This might need a full read and write barrier
1142e0f66ef8SJohn Baldwin 			 * to make sure that this write posts before any
1143e0f66ef8SJohn Baldwin 			 * of the memory or device accesses in the
1144e0f66ef8SJohn Baldwin 			 * handlers.
11458088699fSJohn Baldwin 			 */
1146b4151f71SJohn Baldwin 			atomic_store_rel_int(&ithd->it_need, 0);
1147e0f66ef8SJohn Baldwin 			ithread_execute_handlers(p, ie);
11488088699fSJohn Baldwin 		}
11497870c3c6SJohn Baldwin 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
11507870c3c6SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
11518088699fSJohn Baldwin 
11528088699fSJohn Baldwin 		/*
11538088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
11548088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
11558088699fSJohn Baldwin 		 * set again, so we have to check it again.
11568088699fSJohn Baldwin 		 */
1157982d11f8SJeff Roberson 		thread_lock(td);
1158e0f66ef8SJohn Baldwin 		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
11597870c3c6SJohn Baldwin 			TD_SET_IWAIT(td);
1160e0f66ef8SJohn Baldwin 			ie->ie_count = 0;
1161bf0acc27SJohn Baldwin 			mi_switch(SW_VOL, NULL);
11628088699fSJohn Baldwin 		}
1163eaf86d16SJohn Baldwin 
1164eaf86d16SJohn Baldwin #ifdef SMP
1165eaf86d16SJohn Baldwin 		/*
1166eaf86d16SJohn Baldwin 		 * Ensure we are bound to the correct CPU.  We can't
1167eaf86d16SJohn Baldwin 		 * move ithreads until SMP is running however, so just
1168eaf86d16SJohn Baldwin 		 * leave interrupts on the boor CPU during boot.
1169eaf86d16SJohn Baldwin 		 */
1170eaf86d16SJohn Baldwin 		if (ie->ie_cpu != cpu && smp_started) {
1171eaf86d16SJohn Baldwin 			cpu = ie->ie_cpu;
1172eaf86d16SJohn Baldwin 			if (cpu == NOCPU)
1173eaf86d16SJohn Baldwin 				sched_unbind(td);
1174eaf86d16SJohn Baldwin 			else
1175eaf86d16SJohn Baldwin 				sched_bind(td, cpu);
1176eaf86d16SJohn Baldwin 		}
1177eaf86d16SJohn Baldwin #endif
1178982d11f8SJeff Roberson 		thread_unlock(td);
11798088699fSJohn Baldwin 	}
11801931cf94SJohn Baldwin }
11811ee1b687SJohn Baldwin 
11821ee1b687SJohn Baldwin /*
11831ee1b687SJohn Baldwin  * Main interrupt handling body.
11841ee1b687SJohn Baldwin  *
11851ee1b687SJohn Baldwin  * Input:
11861ee1b687SJohn Baldwin  * o ie:                        the event connected to this interrupt.
11871ee1b687SJohn Baldwin  * o frame:                     some archs (i.e. i386) pass a frame to some.
11881ee1b687SJohn Baldwin  *                              handlers as their main argument.
11891ee1b687SJohn Baldwin  * Return value:
11901ee1b687SJohn Baldwin  * o 0:                         everything ok.
11911ee1b687SJohn Baldwin  * o EINVAL:                    stray interrupt.
11921ee1b687SJohn Baldwin  */
11931ee1b687SJohn Baldwin int
11941ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame)
11951ee1b687SJohn Baldwin {
11961ee1b687SJohn Baldwin 	struct intr_handler *ih;
11971ee1b687SJohn Baldwin 	struct thread *td;
11981ee1b687SJohn Baldwin 	int error, ret, thread;
11991ee1b687SJohn Baldwin 
12001ee1b687SJohn Baldwin 	td = curthread;
12011ee1b687SJohn Baldwin 
12021ee1b687SJohn Baldwin 	/* An interrupt with no event or handlers is a stray interrupt. */
12031ee1b687SJohn Baldwin 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
12041ee1b687SJohn Baldwin 		return (EINVAL);
12051ee1b687SJohn Baldwin 
12061ee1b687SJohn Baldwin 	/*
12071ee1b687SJohn Baldwin 	 * Execute fast interrupt handlers directly.
12081ee1b687SJohn Baldwin 	 * To support clock handlers, if a handler registers
12091ee1b687SJohn Baldwin 	 * with a NULL argument, then we pass it a pointer to
12101ee1b687SJohn Baldwin 	 * a trapframe as its argument.
12111ee1b687SJohn Baldwin 	 */
12121ee1b687SJohn Baldwin 	td->td_intr_nesting_level++;
12131ee1b687SJohn Baldwin 	thread = 0;
12141ee1b687SJohn Baldwin 	ret = 0;
12151ee1b687SJohn Baldwin 	critical_enter();
12161ee1b687SJohn Baldwin 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
12171ee1b687SJohn Baldwin 		if (ih->ih_filter == NULL) {
12181ee1b687SJohn Baldwin 			thread = 1;
12191ee1b687SJohn Baldwin 			continue;
12201ee1b687SJohn Baldwin 		}
12211ee1b687SJohn Baldwin 		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
12221ee1b687SJohn Baldwin 		    ih->ih_filter, ih->ih_argument == NULL ? frame :
12231ee1b687SJohn Baldwin 		    ih->ih_argument, ih->ih_name);
12241ee1b687SJohn Baldwin 		if (ih->ih_argument == NULL)
12251ee1b687SJohn Baldwin 			ret = ih->ih_filter(frame);
12261ee1b687SJohn Baldwin 		else
12271ee1b687SJohn Baldwin 			ret = ih->ih_filter(ih->ih_argument);
12281ee1b687SJohn Baldwin 		/*
12291ee1b687SJohn Baldwin 		 * Wrapper handler special handling:
12301ee1b687SJohn Baldwin 		 *
12311ee1b687SJohn Baldwin 		 * in some particular cases (like pccard and pccbb),
12321ee1b687SJohn Baldwin 		 * the _real_ device handler is wrapped in a couple of
12331ee1b687SJohn Baldwin 		 * functions - a filter wrapper and an ithread wrapper.
12341ee1b687SJohn Baldwin 		 * In this case (and just in this case), the filter wrapper
12351ee1b687SJohn Baldwin 		 * could ask the system to schedule the ithread and mask
12361ee1b687SJohn Baldwin 		 * the interrupt source if the wrapped handler is composed
12371ee1b687SJohn Baldwin 		 * of just an ithread handler.
12381ee1b687SJohn Baldwin 		 *
12391ee1b687SJohn Baldwin 		 * TODO: write a generic wrapper to avoid people rolling
12401ee1b687SJohn Baldwin 		 * their own
12411ee1b687SJohn Baldwin 		 */
12421ee1b687SJohn Baldwin 		if (!thread) {
12431ee1b687SJohn Baldwin 			if (ret == FILTER_SCHEDULE_THREAD)
12441ee1b687SJohn Baldwin 				thread = 1;
12451ee1b687SJohn Baldwin 		}
12461ee1b687SJohn Baldwin 	}
12471ee1b687SJohn Baldwin 
12481ee1b687SJohn Baldwin 	if (thread) {
12491ee1b687SJohn Baldwin 		if (ie->ie_pre_ithread != NULL)
12501ee1b687SJohn Baldwin 			ie->ie_pre_ithread(ie->ie_source);
12511ee1b687SJohn Baldwin 	} else {
12521ee1b687SJohn Baldwin 		if (ie->ie_post_filter != NULL)
12531ee1b687SJohn Baldwin 			ie->ie_post_filter(ie->ie_source);
12541ee1b687SJohn Baldwin 	}
12551ee1b687SJohn Baldwin 
12561ee1b687SJohn Baldwin 	/* Schedule the ithread if needed. */
12571ee1b687SJohn Baldwin 	if (thread) {
12581ee1b687SJohn Baldwin 		error = intr_event_schedule_thread(ie);
12591ee1b687SJohn Baldwin 		KASSERT(error == 0, ("bad stray interrupt"));
12601ee1b687SJohn Baldwin 	}
12611ee1b687SJohn Baldwin 	critical_exit();
12621ee1b687SJohn Baldwin 	td->td_intr_nesting_level--;
12631ee1b687SJohn Baldwin 	return (0);
12641ee1b687SJohn Baldwin }
1265bafe5a31SPaolo Pisati #else
1266bafe5a31SPaolo Pisati /*
1267bafe5a31SPaolo Pisati  * This is the main code for interrupt threads.
1268bafe5a31SPaolo Pisati  */
1269bafe5a31SPaolo Pisati static void
1270bafe5a31SPaolo Pisati ithread_loop(void *arg)
1271bafe5a31SPaolo Pisati {
1272bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
1273bafe5a31SPaolo Pisati 	struct intr_handler *ih;
1274bafe5a31SPaolo Pisati 	struct intr_event *ie;
1275bafe5a31SPaolo Pisati 	struct thread *td;
1276bafe5a31SPaolo Pisati 	struct proc *p;
1277bafe5a31SPaolo Pisati 	int priv;
1278eaf86d16SJohn Baldwin 	u_char cpu;
1279bafe5a31SPaolo Pisati 
1280bafe5a31SPaolo Pisati 	td = curthread;
1281bafe5a31SPaolo Pisati 	p = td->td_proc;
1282bafe5a31SPaolo Pisati 	ih = (struct intr_handler *)arg;
1283bafe5a31SPaolo Pisati 	priv = (ih->ih_thread != NULL) ? 1 : 0;
1284bafe5a31SPaolo Pisati 	ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1285bafe5a31SPaolo Pisati 	KASSERT(ithd->it_thread == td,
1286bafe5a31SPaolo Pisati 	    ("%s: ithread and proc linkage out of sync", __func__));
1287bafe5a31SPaolo Pisati 	ie = ithd->it_event;
1288bafe5a31SPaolo Pisati 	ie->ie_count = 0;
1289eaf86d16SJohn Baldwin 	cpu = NOCPU;
1290bafe5a31SPaolo Pisati 
1291bafe5a31SPaolo Pisati 	/*
1292bafe5a31SPaolo Pisati 	 * As long as we have interrupts outstanding, go through the
1293bafe5a31SPaolo Pisati 	 * list of handlers, giving each one a go at it.
1294bafe5a31SPaolo Pisati 	 */
1295bafe5a31SPaolo Pisati 	for (;;) {
1296bafe5a31SPaolo Pisati 		/*
1297bafe5a31SPaolo Pisati 		 * If we are an orphaned thread, then just die.
1298bafe5a31SPaolo Pisati 		 */
1299bafe5a31SPaolo Pisati 		if (ithd->it_flags & IT_DEAD) {
1300bafe5a31SPaolo Pisati 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
13017ab24ea3SJulian Elischer 			    p->p_pid, td->td_name);
1302bafe5a31SPaolo Pisati 			free(ithd, M_ITHREAD);
1303ca9a0ddfSJulian Elischer 			kthread_exit();
1304bafe5a31SPaolo Pisati 		}
1305bafe5a31SPaolo Pisati 
1306bafe5a31SPaolo Pisati 		/*
1307bafe5a31SPaolo Pisati 		 * Service interrupts.  If another interrupt arrives while
1308bafe5a31SPaolo Pisati 		 * we are running, it will set it_need to note that we
1309bafe5a31SPaolo Pisati 		 * should make another pass.
1310bafe5a31SPaolo Pisati 		 */
1311bafe5a31SPaolo Pisati 		while (ithd->it_need) {
1312bafe5a31SPaolo Pisati 			/*
1313bafe5a31SPaolo Pisati 			 * This might need a full read and write barrier
1314bafe5a31SPaolo Pisati 			 * to make sure that this write posts before any
1315bafe5a31SPaolo Pisati 			 * of the memory or device accesses in the
1316bafe5a31SPaolo Pisati 			 * handlers.
1317bafe5a31SPaolo Pisati 			 */
1318bafe5a31SPaolo Pisati 			atomic_store_rel_int(&ithd->it_need, 0);
1319bafe5a31SPaolo Pisati 			if (priv)
1320bafe5a31SPaolo Pisati 				priv_ithread_execute_handler(p, ih);
1321bafe5a31SPaolo Pisati 			else
1322bafe5a31SPaolo Pisati 				ithread_execute_handlers(p, ie);
1323bafe5a31SPaolo Pisati 		}
1324bafe5a31SPaolo Pisati 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1325bafe5a31SPaolo Pisati 		mtx_assert(&Giant, MA_NOTOWNED);
1326bafe5a31SPaolo Pisati 
1327bafe5a31SPaolo Pisati 		/*
1328bafe5a31SPaolo Pisati 		 * Processed all our interrupts.  Now get the sched
1329bafe5a31SPaolo Pisati 		 * lock.  This may take a while and it_need may get
1330bafe5a31SPaolo Pisati 		 * set again, so we have to check it again.
1331bafe5a31SPaolo Pisati 		 */
1332982d11f8SJeff Roberson 		thread_lock(td);
1333bafe5a31SPaolo Pisati 		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1334bafe5a31SPaolo Pisati 			TD_SET_IWAIT(td);
1335bafe5a31SPaolo Pisati 			ie->ie_count = 0;
1336bafe5a31SPaolo Pisati 			mi_switch(SW_VOL, NULL);
1337bafe5a31SPaolo Pisati 		}
1338eaf86d16SJohn Baldwin 
1339eaf86d16SJohn Baldwin #ifdef SMP
1340eaf86d16SJohn Baldwin 		/*
1341eaf86d16SJohn Baldwin 		 * Ensure we are bound to the correct CPU.  We can't
1342eaf86d16SJohn Baldwin 		 * move ithreads until SMP is running however, so just
1343eaf86d16SJohn Baldwin 		 * leave interrupts on the boor CPU during boot.
1344eaf86d16SJohn Baldwin 		 */
1345eaf86d16SJohn Baldwin 		if (!priv && ie->ie_cpu != cpu && smp_started) {
1346eaf86d16SJohn Baldwin 			cpu = ie->ie_cpu;
1347eaf86d16SJohn Baldwin 			if (cpu == NOCPU)
1348eaf86d16SJohn Baldwin 				sched_unbind(td);
1349eaf86d16SJohn Baldwin 			else
1350eaf86d16SJohn Baldwin 				sched_bind(td, cpu);
1351eaf86d16SJohn Baldwin 		}
1352eaf86d16SJohn Baldwin #endif
1353982d11f8SJeff Roberson 		thread_unlock(td);
1354bafe5a31SPaolo Pisati 	}
1355bafe5a31SPaolo Pisati }
1356bafe5a31SPaolo Pisati 
1357bafe5a31SPaolo Pisati /*
1358bafe5a31SPaolo Pisati  * Main loop for interrupt filter.
1359bafe5a31SPaolo Pisati  *
1360bafe5a31SPaolo Pisati  * Some architectures (i386, amd64 and arm) require the optional frame
1361bafe5a31SPaolo Pisati  * parameter, and use it as the main argument for fast handler execution
1362bafe5a31SPaolo Pisati  * when ih_argument == NULL.
1363bafe5a31SPaolo Pisati  *
1364bafe5a31SPaolo Pisati  * Return value:
1365bafe5a31SPaolo Pisati  * o FILTER_STRAY:              No filter recognized the event, and no
1366bafe5a31SPaolo Pisati  *                              filter-less handler is registered on this
1367bafe5a31SPaolo Pisati  *                              line.
1368bafe5a31SPaolo Pisati  * o FILTER_HANDLED:            A filter claimed the event and served it.
1369bafe5a31SPaolo Pisati  * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1370bafe5a31SPaolo Pisati  *                              least one filter-less handler on this line.
1371bafe5a31SPaolo Pisati  * o FILTER_HANDLED |
1372bafe5a31SPaolo Pisati  *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1373bafe5a31SPaolo Pisati  *                              scheduling the per-handler ithread.
1374bafe5a31SPaolo Pisati  *
1375bafe5a31SPaolo Pisati  * In case an ithread has to be scheduled, in *ithd there will be a
1376bafe5a31SPaolo Pisati  * pointer to a struct intr_thread containing the thread to be
1377bafe5a31SPaolo Pisati  * scheduled.
1378bafe5a31SPaolo Pisati  */
1379bafe5a31SPaolo Pisati 
13801ee1b687SJohn Baldwin static int
1381bafe5a31SPaolo Pisati intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1382bafe5a31SPaolo Pisati 		 struct intr_thread **ithd)
1383bafe5a31SPaolo Pisati {
1384bafe5a31SPaolo Pisati 	struct intr_handler *ih;
1385bafe5a31SPaolo Pisati 	void *arg;
1386bafe5a31SPaolo Pisati 	int ret, thread_only;
1387bafe5a31SPaolo Pisati 
1388bafe5a31SPaolo Pisati 	ret = 0;
1389bafe5a31SPaolo Pisati 	thread_only = 0;
1390bafe5a31SPaolo Pisati 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1391bafe5a31SPaolo Pisati 		/*
1392bafe5a31SPaolo Pisati 		 * Execute fast interrupt handlers directly.
1393bafe5a31SPaolo Pisati 		 * To support clock handlers, if a handler registers
1394bafe5a31SPaolo Pisati 		 * with a NULL argument, then we pass it a pointer to
1395bafe5a31SPaolo Pisati 		 * a trapframe as its argument.
1396bafe5a31SPaolo Pisati 		 */
1397bafe5a31SPaolo Pisati 		arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1398bafe5a31SPaolo Pisati 
1399bafe5a31SPaolo Pisati 		CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1400bafe5a31SPaolo Pisati 		     ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1401bafe5a31SPaolo Pisati 
1402bafe5a31SPaolo Pisati 		if (ih->ih_filter != NULL)
1403bafe5a31SPaolo Pisati 			ret = ih->ih_filter(arg);
1404bafe5a31SPaolo Pisati 		else {
1405bafe5a31SPaolo Pisati 			thread_only = 1;
1406bafe5a31SPaolo Pisati 			continue;
1407bafe5a31SPaolo Pisati 		}
1408bafe5a31SPaolo Pisati 
1409bafe5a31SPaolo Pisati 		if (ret & FILTER_STRAY)
1410bafe5a31SPaolo Pisati 			continue;
1411bafe5a31SPaolo Pisati 		else {
1412bafe5a31SPaolo Pisati 			*ithd = ih->ih_thread;
1413bafe5a31SPaolo Pisati 			return (ret);
1414bafe5a31SPaolo Pisati 		}
1415bafe5a31SPaolo Pisati 	}
1416bafe5a31SPaolo Pisati 
1417bafe5a31SPaolo Pisati 	/*
1418bafe5a31SPaolo Pisati 	 * No filters handled the interrupt and we have at least
1419bafe5a31SPaolo Pisati 	 * one handler without a filter.  In this case, we schedule
1420bafe5a31SPaolo Pisati 	 * all of the filter-less handlers to run in the ithread.
1421bafe5a31SPaolo Pisati 	 */
1422bafe5a31SPaolo Pisati 	if (thread_only) {
1423bafe5a31SPaolo Pisati 		*ithd = ie->ie_thread;
1424bafe5a31SPaolo Pisati 		return (FILTER_SCHEDULE_THREAD);
1425bafe5a31SPaolo Pisati 	}
1426bafe5a31SPaolo Pisati 	return (FILTER_STRAY);
1427bafe5a31SPaolo Pisati }
1428bafe5a31SPaolo Pisati 
1429bafe5a31SPaolo Pisati /*
1430bafe5a31SPaolo Pisati  * Main interrupt handling body.
1431bafe5a31SPaolo Pisati  *
1432bafe5a31SPaolo Pisati  * Input:
1433bafe5a31SPaolo Pisati  * o ie:                        the event connected to this interrupt.
1434bafe5a31SPaolo Pisati  * o frame:                     some archs (i.e. i386) pass a frame to some.
1435bafe5a31SPaolo Pisati  *                              handlers as their main argument.
1436bafe5a31SPaolo Pisati  * Return value:
1437bafe5a31SPaolo Pisati  * o 0:                         everything ok.
1438bafe5a31SPaolo Pisati  * o EINVAL:                    stray interrupt.
1439bafe5a31SPaolo Pisati  */
1440bafe5a31SPaolo Pisati int
1441bafe5a31SPaolo Pisati intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1442bafe5a31SPaolo Pisati {
1443bafe5a31SPaolo Pisati 	struct intr_thread *ithd;
1444bafe5a31SPaolo Pisati 	struct thread *td;
1445bafe5a31SPaolo Pisati 	int thread;
1446bafe5a31SPaolo Pisati 
1447bafe5a31SPaolo Pisati 	ithd = NULL;
1448bafe5a31SPaolo Pisati 	td = curthread;
1449bafe5a31SPaolo Pisati 
1450bafe5a31SPaolo Pisati 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1451bafe5a31SPaolo Pisati 		return (EINVAL);
1452bafe5a31SPaolo Pisati 
1453bafe5a31SPaolo Pisati 	td->td_intr_nesting_level++;
1454bafe5a31SPaolo Pisati 	thread = 0;
1455bafe5a31SPaolo Pisati 	critical_enter();
1456bafe5a31SPaolo Pisati 	thread = intr_filter_loop(ie, frame, &ithd);
1457bafe5a31SPaolo Pisati 	if (thread & FILTER_HANDLED) {
14581ee1b687SJohn Baldwin 		if (ie->ie_post_filter != NULL)
14591ee1b687SJohn Baldwin 			ie->ie_post_filter(ie->ie_source);
1460bafe5a31SPaolo Pisati 	} else {
14611ee1b687SJohn Baldwin 		if (ie->ie_pre_ithread != NULL)
14621ee1b687SJohn Baldwin 			ie->ie_pre_ithread(ie->ie_source);
1463bafe5a31SPaolo Pisati 	}
1464bafe5a31SPaolo Pisati 	critical_exit();
1465bafe5a31SPaolo Pisati 
1466bafe5a31SPaolo Pisati 	/* Interrupt storm logic */
1467bafe5a31SPaolo Pisati 	if (thread & FILTER_STRAY) {
1468bafe5a31SPaolo Pisati 		ie->ie_count++;
1469bafe5a31SPaolo Pisati 		if (ie->ie_count < intr_storm_threshold)
1470bafe5a31SPaolo Pisati 			printf("Interrupt stray detection not present\n");
1471bafe5a31SPaolo Pisati 	}
1472bafe5a31SPaolo Pisati 
1473bafe5a31SPaolo Pisati 	/* Schedule an ithread if needed. */
1474bafe5a31SPaolo Pisati 	if (thread & FILTER_SCHEDULE_THREAD) {
1475bafe5a31SPaolo Pisati 		if (intr_event_schedule_thread(ie, ithd) != 0)
1476bafe5a31SPaolo Pisati 			panic("%s: impossible stray interrupt", __func__);
1477bafe5a31SPaolo Pisati 	}
1478bafe5a31SPaolo Pisati 	td->td_intr_nesting_level--;
1479bafe5a31SPaolo Pisati 	return (0);
1480bafe5a31SPaolo Pisati }
1481bafe5a31SPaolo Pisati #endif
14821931cf94SJohn Baldwin 
14838b201c42SJohn Baldwin #ifdef DDB
14848b201c42SJohn Baldwin /*
14858b201c42SJohn Baldwin  * Dump details about an interrupt handler
14868b201c42SJohn Baldwin  */
14878b201c42SJohn Baldwin static void
1488e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih)
14898b201c42SJohn Baldwin {
14908b201c42SJohn Baldwin 	int comma;
14918b201c42SJohn Baldwin 
14928b201c42SJohn Baldwin 	db_printf("\t%-10s ", ih->ih_name);
14938b201c42SJohn Baldwin 	switch (ih->ih_pri) {
14948b201c42SJohn Baldwin 	case PI_REALTIME:
14958b201c42SJohn Baldwin 		db_printf("CLK ");
14968b201c42SJohn Baldwin 		break;
14978b201c42SJohn Baldwin 	case PI_AV:
14988b201c42SJohn Baldwin 		db_printf("AV  ");
14998b201c42SJohn Baldwin 		break;
15008b201c42SJohn Baldwin 	case PI_TTYHIGH:
15018b201c42SJohn Baldwin 	case PI_TTYLOW:
15028b201c42SJohn Baldwin 		db_printf("TTY ");
15038b201c42SJohn Baldwin 		break;
15048b201c42SJohn Baldwin 	case PI_TAPE:
15058b201c42SJohn Baldwin 		db_printf("TAPE");
15068b201c42SJohn Baldwin 		break;
15078b201c42SJohn Baldwin 	case PI_NET:
15088b201c42SJohn Baldwin 		db_printf("NET ");
15098b201c42SJohn Baldwin 		break;
15108b201c42SJohn Baldwin 	case PI_DISK:
15118b201c42SJohn Baldwin 	case PI_DISKLOW:
15128b201c42SJohn Baldwin 		db_printf("DISK");
15138b201c42SJohn Baldwin 		break;
15148b201c42SJohn Baldwin 	case PI_DULL:
15158b201c42SJohn Baldwin 		db_printf("DULL");
15168b201c42SJohn Baldwin 		break;
15178b201c42SJohn Baldwin 	default:
15188b201c42SJohn Baldwin 		if (ih->ih_pri >= PI_SOFT)
15198b201c42SJohn Baldwin 			db_printf("SWI ");
15208b201c42SJohn Baldwin 		else
15218b201c42SJohn Baldwin 			db_printf("%4u", ih->ih_pri);
15228b201c42SJohn Baldwin 		break;
15238b201c42SJohn Baldwin 	}
15248b201c42SJohn Baldwin 	db_printf(" ");
15258b201c42SJohn Baldwin 	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
15268b201c42SJohn Baldwin 	db_printf("(%p)", ih->ih_argument);
15278b201c42SJohn Baldwin 	if (ih->ih_need ||
1528ef544f63SPaolo Pisati 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
15298b201c42SJohn Baldwin 	    IH_MPSAFE)) != 0) {
15308b201c42SJohn Baldwin 		db_printf(" {");
15318b201c42SJohn Baldwin 		comma = 0;
15328b201c42SJohn Baldwin 		if (ih->ih_flags & IH_EXCLUSIVE) {
15338b201c42SJohn Baldwin 			if (comma)
15348b201c42SJohn Baldwin 				db_printf(", ");
15358b201c42SJohn Baldwin 			db_printf("EXCL");
15368b201c42SJohn Baldwin 			comma = 1;
15378b201c42SJohn Baldwin 		}
15388b201c42SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY) {
15398b201c42SJohn Baldwin 			if (comma)
15408b201c42SJohn Baldwin 				db_printf(", ");
15418b201c42SJohn Baldwin 			db_printf("ENTROPY");
15428b201c42SJohn Baldwin 			comma = 1;
15438b201c42SJohn Baldwin 		}
15448b201c42SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
15458b201c42SJohn Baldwin 			if (comma)
15468b201c42SJohn Baldwin 				db_printf(", ");
15478b201c42SJohn Baldwin 			db_printf("DEAD");
15488b201c42SJohn Baldwin 			comma = 1;
15498b201c42SJohn Baldwin 		}
15508b201c42SJohn Baldwin 		if (ih->ih_flags & IH_MPSAFE) {
15518b201c42SJohn Baldwin 			if (comma)
15528b201c42SJohn Baldwin 				db_printf(", ");
15538b201c42SJohn Baldwin 			db_printf("MPSAFE");
15548b201c42SJohn Baldwin 			comma = 1;
15558b201c42SJohn Baldwin 		}
15568b201c42SJohn Baldwin 		if (ih->ih_need) {
15578b201c42SJohn Baldwin 			if (comma)
15588b201c42SJohn Baldwin 				db_printf(", ");
15598b201c42SJohn Baldwin 			db_printf("NEED");
15608b201c42SJohn Baldwin 		}
15618b201c42SJohn Baldwin 		db_printf("}");
15628b201c42SJohn Baldwin 	}
15638b201c42SJohn Baldwin 	db_printf("\n");
15648b201c42SJohn Baldwin }
15658b201c42SJohn Baldwin 
15668b201c42SJohn Baldwin /*
1567e0f66ef8SJohn Baldwin  * Dump details about a event.
15688b201c42SJohn Baldwin  */
15698b201c42SJohn Baldwin void
1570e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers)
15718b201c42SJohn Baldwin {
1572e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
1573e0f66ef8SJohn Baldwin 	struct intr_thread *it;
15748b201c42SJohn Baldwin 	int comma;
15758b201c42SJohn Baldwin 
1576e0f66ef8SJohn Baldwin 	db_printf("%s ", ie->ie_fullname);
1577e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
1578e0f66ef8SJohn Baldwin 	if (it != NULL)
1579e0f66ef8SJohn Baldwin 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1580e0f66ef8SJohn Baldwin 	else
1581e0f66ef8SJohn Baldwin 		db_printf("(no thread)");
1582eaf86d16SJohn Baldwin 	if (ie->ie_cpu != NOCPU)
1583eaf86d16SJohn Baldwin 		db_printf(" (CPU %d)", ie->ie_cpu);
1584e0f66ef8SJohn Baldwin 	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1585e0f66ef8SJohn Baldwin 	    (it != NULL && it->it_need)) {
15868b201c42SJohn Baldwin 		db_printf(" {");
15878b201c42SJohn Baldwin 		comma = 0;
1588e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
15898b201c42SJohn Baldwin 			db_printf("SOFT");
15908b201c42SJohn Baldwin 			comma = 1;
15918b201c42SJohn Baldwin 		}
1592e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ENTROPY) {
15938b201c42SJohn Baldwin 			if (comma)
15948b201c42SJohn Baldwin 				db_printf(", ");
15958b201c42SJohn Baldwin 			db_printf("ENTROPY");
15968b201c42SJohn Baldwin 			comma = 1;
15978b201c42SJohn Baldwin 		}
1598e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD) {
15998b201c42SJohn Baldwin 			if (comma)
16008b201c42SJohn Baldwin 				db_printf(", ");
1601e0f66ef8SJohn Baldwin 			db_printf("ADDING_THREAD");
16028b201c42SJohn Baldwin 			comma = 1;
16038b201c42SJohn Baldwin 		}
1604e0f66ef8SJohn Baldwin 		if (it != NULL && it->it_need) {
16058b201c42SJohn Baldwin 			if (comma)
16068b201c42SJohn Baldwin 				db_printf(", ");
16078b201c42SJohn Baldwin 			db_printf("NEED");
16088b201c42SJohn Baldwin 		}
16098b201c42SJohn Baldwin 		db_printf("}");
16108b201c42SJohn Baldwin 	}
16118b201c42SJohn Baldwin 	db_printf("\n");
16128b201c42SJohn Baldwin 
16138b201c42SJohn Baldwin 	if (handlers)
1614e0f66ef8SJohn Baldwin 		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
16158b201c42SJohn Baldwin 		    db_dump_intrhand(ih);
16168b201c42SJohn Baldwin }
1617e0f66ef8SJohn Baldwin 
1618e0f66ef8SJohn Baldwin /*
1619e0f66ef8SJohn Baldwin  * Dump data about interrupt handlers
1620e0f66ef8SJohn Baldwin  */
1621e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr)
1622e0f66ef8SJohn Baldwin {
1623e0f66ef8SJohn Baldwin 	struct intr_event *ie;
162419e9205aSJohn Baldwin 	int all, verbose;
1625e0f66ef8SJohn Baldwin 
1626e0f66ef8SJohn Baldwin 	verbose = index(modif, 'v') != NULL;
1627e0f66ef8SJohn Baldwin 	all = index(modif, 'a') != NULL;
1628e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ie, &event_list, ie_list) {
1629e0f66ef8SJohn Baldwin 		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1630e0f66ef8SJohn Baldwin 			continue;
1631e0f66ef8SJohn Baldwin 		db_dump_intr_event(ie, verbose);
163219e9205aSJohn Baldwin 		if (db_pager_quit)
163319e9205aSJohn Baldwin 			break;
1634e0f66ef8SJohn Baldwin 	}
1635e0f66ef8SJohn Baldwin }
16368b201c42SJohn Baldwin #endif /* DDB */
16378b201c42SJohn Baldwin 
1638b4151f71SJohn Baldwin /*
16398088699fSJohn Baldwin  * Start standard software interrupt threads
16401931cf94SJohn Baldwin  */
16411931cf94SJohn Baldwin static void
1642b4151f71SJohn Baldwin start_softintr(void *dummy)
16431931cf94SJohn Baldwin {
1644b4151f71SJohn Baldwin 
16458d809d50SJeff Roberson 	if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
16468d809d50SJeff Roberson 		panic("died while creating vm swi ithread");
16471931cf94SJohn Baldwin }
1648237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1649237fdd78SRobert Watson     NULL);
16501931cf94SJohn Baldwin 
1651d279178dSThomas Moestl /*
1652d279178dSThomas Moestl  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1653d279178dSThomas Moestl  * The data for this machine dependent, and the declarations are in machine
1654d279178dSThomas Moestl  * dependent code.  The layout of intrnames and intrcnt however is machine
1655d279178dSThomas Moestl  * independent.
1656d279178dSThomas Moestl  *
1657d279178dSThomas Moestl  * We do not know the length of intrcnt and intrnames at compile time, so
1658d279178dSThomas Moestl  * calculate things at run time.
1659d279178dSThomas Moestl  */
1660d279178dSThomas Moestl static int
1661d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1662d279178dSThomas Moestl {
1663d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1664d279178dSThomas Moestl 	   req));
1665d279178dSThomas Moestl }
1666d279178dSThomas Moestl 
1667d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1668d279178dSThomas Moestl     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1669d279178dSThomas Moestl 
1670d279178dSThomas Moestl static int
1671d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1672d279178dSThomas Moestl {
1673d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrcnt,
1674d279178dSThomas Moestl 	    (char *)eintrcnt - (char *)intrcnt, req));
1675d279178dSThomas Moestl }
1676d279178dSThomas Moestl 
1677d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1678d279178dSThomas Moestl     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
16798b201c42SJohn Baldwin 
16808b201c42SJohn Baldwin #ifdef DDB
16818b201c42SJohn Baldwin /*
16828b201c42SJohn Baldwin  * DDB command to dump the interrupt statistics.
16838b201c42SJohn Baldwin  */
16848b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
16858b201c42SJohn Baldwin {
16868b201c42SJohn Baldwin 	u_long *i;
16878b201c42SJohn Baldwin 	char *cp;
16888b201c42SJohn Baldwin 
16898b201c42SJohn Baldwin 	cp = intrnames;
169019e9205aSJohn Baldwin 	for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
16918b201c42SJohn Baldwin 		if (*cp == '\0')
16928b201c42SJohn Baldwin 			break;
16938b201c42SJohn Baldwin 		if (*i != 0)
16948b201c42SJohn Baldwin 			db_printf("%s\t%lu\n", cp, *i);
16958b201c42SJohn Baldwin 		cp += strlen(cp) + 1;
16968b201c42SJohn Baldwin 	}
16978b201c42SJohn Baldwin }
16988b201c42SJohn Baldwin #endif
1699