kern_intr.c revision 240921
1139804Simp/*-
226156Sse * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
326156Sse * All rights reserved.
426156Sse *
526156Sse * Redistribution and use in source and binary forms, with or without
626156Sse * modification, are permitted provided that the following conditions
726156Sse * are met:
826156Sse * 1. Redistributions of source code must retain the above copyright
926156Sse *    notice unmodified, this list of conditions, and the following
1026156Sse *    disclaimer.
1126156Sse * 2. Redistributions in binary form must reproduce the above copyright
1226156Sse *    notice, this list of conditions and the following disclaimer in the
1326156Sse *    documentation and/or other materials provided with the distribution.
1426156Sse *
1526156Sse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1626156Sse * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1726156Sse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1826156Sse * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1926156Sse * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2026156Sse * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2126156Sse * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2226156Sse * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2326156Sse * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2426156Sse * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2526156Sse */
2626156Sse
27116182Sobrien#include <sys/cdefs.h>
28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_intr.c 240921 2012-09-25 14:55:46Z jhb $");
2936887Sdfr
30121482Sjhb#include "opt_ddb.h"
31121482Sjhb
3241059Speter#include <sys/param.h>
3365822Sjhb#include <sys/bus.h>
34110860Salfred#include <sys/conf.h>
35178092Sjeff#include <sys/cpuset.h>
3665822Sjhb#include <sys/rtprio.h>
3741059Speter#include <sys/systm.h>
3866698Sjhb#include <sys/interrupt.h>
3966698Sjhb#include <sys/kernel.h>
4066698Sjhb#include <sys/kthread.h>
4166698Sjhb#include <sys/ktr.h>
42130128Sbde#include <sys/limits.h>
4374914Sjhb#include <sys/lock.h>
4426156Sse#include <sys/malloc.h>
4567365Sjhb#include <sys/mutex.h>
46195249Sjhb#include <sys/priv.h>
4766698Sjhb#include <sys/proc.h>
4872759Sjhb#include <sys/random.h>
4972237Sjhb#include <sys/resourcevar.h>
50139451Sjhb#include <sys/sched.h>
51177181Sjhb#include <sys/smp.h>
5277582Stmm#include <sys/sysctl.h>
53182024Skmacy#include <sys/syslog.h>
5466698Sjhb#include <sys/unistd.h>
5566698Sjhb#include <sys/vmmeter.h>
5666698Sjhb#include <machine/atomic.h>
5766698Sjhb#include <machine/cpu.h>
5867551Sjhb#include <machine/md_var.h>
5972237Sjhb#include <machine/stdarg.h>
60121482Sjhb#ifdef DDB
61121482Sjhb#include <ddb/ddb.h>
62121482Sjhb#include <ddb/db_sym.h>
63121482Sjhb#endif
6426156Sse
65151658Sjhb/*
66151658Sjhb * Describe an interrupt thread.  There is one of these per interrupt event.
67151658Sjhb */
68151658Sjhbstruct intr_thread {
69151658Sjhb	struct intr_event *it_event;
70151658Sjhb	struct thread *it_thread;	/* Kernel thread. */
71151658Sjhb	int	it_flags;		/* (j) IT_* flags. */
72151658Sjhb	int	it_need;		/* Needs service. */
7372759Sjhb};
7472759Sjhb
75151658Sjhb/* Interrupt thread flags kept in it_flags */
76151658Sjhb#define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
77219819Sjeff#define	IT_WAIT		0x000002	/* Thread is waiting for completion. */
78151658Sjhb
79151658Sjhbstruct	intr_entropy {
80151658Sjhb	struct	thread *td;
81151658Sjhb	uintptr_t event;
82151658Sjhb};
83151658Sjhb
84151658Sjhbstruct	intr_event *clk_intr_event;
85151658Sjhbstruct	intr_event *tty_intr_event;
86128339Sbdevoid	*vm_ih;
87173004Sjulianstruct proc *intrproc;
8838244Sbde
8972237Sjhbstatic MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
9072237Sjhb
91168850Snjlstatic int intr_storm_threshold = 1000;
92128331SjhbTUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
93128331SjhbSYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
94128331Sjhb    &intr_storm_threshold, 0,
95128339Sbde    "Number of consecutive interrupts before storm protection is enabled");
96151658Sjhbstatic TAILQ_HEAD(, intr_event) event_list =
97151658Sjhb    TAILQ_HEAD_INITIALIZER(event_list);
98178092Sjeffstatic struct mtx event_lock;
99178092SjeffMTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
100128331Sjhb
101151658Sjhbstatic void	intr_event_update(struct intr_event *ie);
102169320Spiso#ifdef INTR_FILTER
103177940Sjhbstatic int	intr_event_schedule_thread(struct intr_event *ie,
104177940Sjhb		    struct intr_thread *ithd);
105177940Sjhbstatic int	intr_filter_loop(struct intr_event *ie,
106177940Sjhb		    struct trapframe *frame, struct intr_thread **ithd);
107169320Spisostatic struct intr_thread *ithread_create(const char *name,
108169320Spiso			      struct intr_handler *ih);
109169320Spiso#else
110177940Sjhbstatic int	intr_event_schedule_thread(struct intr_event *ie);
111151658Sjhbstatic struct intr_thread *ithread_create(const char *name);
112169320Spiso#endif
113151658Sjhbstatic void	ithread_destroy(struct intr_thread *ithread);
114169320Spisostatic void	ithread_execute_handlers(struct proc *p,
115169320Spiso		    struct intr_event *ie);
116169320Spiso#ifdef INTR_FILTER
117169320Spisostatic void	priv_ithread_execute_handler(struct proc *p,
118169320Spiso		    struct intr_handler *ih);
119169320Spiso#endif
120128339Sbdestatic void	ithread_loop(void *);
121151658Sjhbstatic void	ithread_update(struct intr_thread *ithd);
122128339Sbdestatic void	start_softintr(void *);
123128339Sbde
124165124Sjhb/* Map an interrupt type to an ithread priority. */
12572237Sjhbu_char
126151658Sjhbintr_priority(enum intr_type flags)
12765822Sjhb{
12872237Sjhb	u_char pri;
12965822Sjhb
13072237Sjhb	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
13178365Speter	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
13265822Sjhb	switch (flags) {
13372237Sjhb	case INTR_TYPE_TTY:
134217292Sjhb		pri = PI_TTY;
13565822Sjhb		break;
13665822Sjhb	case INTR_TYPE_BIO:
13765822Sjhb		pri = PI_DISK;
13865822Sjhb		break;
13965822Sjhb	case INTR_TYPE_NET:
14065822Sjhb		pri = PI_NET;
14165822Sjhb		break;
14265822Sjhb	case INTR_TYPE_CAM:
143217292Sjhb		pri = PI_DISK;
14465822Sjhb		break;
145217292Sjhb	case INTR_TYPE_AV:
14678365Speter		pri = PI_AV;
14778365Speter		break;
14872237Sjhb	case INTR_TYPE_CLK:
14972237Sjhb		pri = PI_REALTIME;
15072237Sjhb		break;
15165822Sjhb	case INTR_TYPE_MISC:
15265822Sjhb		pri = PI_DULL;          /* don't care */
15365822Sjhb		break;
15465822Sjhb	default:
15572237Sjhb		/* We didn't specify an interrupt level. */
156151658Sjhb		panic("intr_priority: no interrupt type in flags");
15765822Sjhb	}
15865822Sjhb
15965822Sjhb	return pri;
16065822Sjhb}
16165822Sjhb
16272237Sjhb/*
163151658Sjhb * Update an ithread based on the associated intr_event.
16472237Sjhb */
16572237Sjhbstatic void
166151658Sjhbithread_update(struct intr_thread *ithd)
16772237Sjhb{
168151658Sjhb	struct intr_event *ie;
16983366Sjulian	struct thread *td;
170151658Sjhb	u_char pri;
17167551Sjhb
172151658Sjhb	ie = ithd->it_event;
173151658Sjhb	td = ithd->it_thread;
17472237Sjhb
175151658Sjhb	/* Determine the overall priority of this event. */
176151658Sjhb	if (TAILQ_EMPTY(&ie->ie_handlers))
177151658Sjhb		pri = PRI_MAX_ITHD;
178151658Sjhb	else
179151658Sjhb		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
180105354Srobert
181151658Sjhb	/* Update name and priority. */
182173004Sjulian	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
183232700Sjhb#ifdef KTR
184232700Sjhb	sched_clear_tdname(td);
185232700Sjhb#endif
186170307Sjeff	thread_lock(td);
187151658Sjhb	sched_prio(td, pri);
188170307Sjeff	thread_unlock(td);
189151658Sjhb}
190151658Sjhb
191151658Sjhb/*
192151658Sjhb * Regenerate the full name of an interrupt event and update its priority.
193151658Sjhb */
194151658Sjhbstatic void
195151658Sjhbintr_event_update(struct intr_event *ie)
196151658Sjhb{
197151658Sjhb	struct intr_handler *ih;
198151658Sjhb	char *last;
199151658Sjhb	int missed, space;
200151658Sjhb
201151658Sjhb	/* Start off with no entropy and just the name of the event. */
202151658Sjhb	mtx_assert(&ie->ie_lock, MA_OWNED);
203151658Sjhb	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
204151658Sjhb	ie->ie_flags &= ~IE_ENTROPY;
205137267Sjhb	missed = 0;
206151658Sjhb	space = 1;
207151658Sjhb
208151658Sjhb	/* Run through all the handlers updating values. */
209151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
210151658Sjhb		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
211151658Sjhb		    sizeof(ie->ie_fullname)) {
212151658Sjhb			strcat(ie->ie_fullname, " ");
213151658Sjhb			strcat(ie->ie_fullname, ih->ih_name);
214151658Sjhb			space = 0;
215137267Sjhb		} else
216137267Sjhb			missed++;
217137267Sjhb		if (ih->ih_flags & IH_ENTROPY)
218151658Sjhb			ie->ie_flags |= IE_ENTROPY;
219137267Sjhb	}
220151658Sjhb
221151658Sjhb	/*
222151658Sjhb	 * If the handler names were too long, add +'s to indicate missing
223151658Sjhb	 * names. If we run out of room and still have +'s to add, change
224151658Sjhb	 * the last character from a + to a *.
225151658Sjhb	 */
226151658Sjhb	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
227137267Sjhb	while (missed-- > 0) {
228151658Sjhb		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
229151658Sjhb			if (*last == '+') {
230151658Sjhb				*last = '*';
231151658Sjhb				break;
232151658Sjhb			} else
233151658Sjhb				*last = '+';
234151658Sjhb		} else if (space) {
235151658Sjhb			strcat(ie->ie_fullname, " +");
236151658Sjhb			space = 0;
23772237Sjhb		} else
238151658Sjhb			strcat(ie->ie_fullname, "+");
23972237Sjhb	}
240151658Sjhb
241151658Sjhb	/*
242151658Sjhb	 * If this event has an ithread, update it's priority and
243151658Sjhb	 * name.
244151658Sjhb	 */
245151658Sjhb	if (ie->ie_thread != NULL)
246151658Sjhb		ithread_update(ie->ie_thread);
247151658Sjhb	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
24872237Sjhb}
24972237Sjhb
25072237Sjhbint
251183298Sobrienintr_event_create(struct intr_event **event, void *source, int flags, int irq,
252177940Sjhb    void (*pre_ithread)(void *), void (*post_ithread)(void *),
253177940Sjhb    void (*post_filter)(void *), int (*assign_cpu)(void *, u_char),
254177940Sjhb    const char *fmt, ...)
255169320Spiso{
256169320Spiso	struct intr_event *ie;
257169320Spiso	va_list ap;
25872237Sjhb
259169320Spiso	/* The only valid flag during creation is IE_SOFT. */
260169320Spiso	if ((flags & ~IE_SOFT) != 0)
261169320Spiso		return (EINVAL);
262169320Spiso	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
263169320Spiso	ie->ie_source = source;
264177940Sjhb	ie->ie_pre_ithread = pre_ithread;
265177940Sjhb	ie->ie_post_ithread = post_ithread;
266177940Sjhb	ie->ie_post_filter = post_filter;
267177181Sjhb	ie->ie_assign_cpu = assign_cpu;
268169320Spiso	ie->ie_flags = flags;
269178092Sjeff	ie->ie_irq = irq;
270177181Sjhb	ie->ie_cpu = NOCPU;
271169320Spiso	TAILQ_INIT(&ie->ie_handlers);
272169320Spiso	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
273169320Spiso
274169320Spiso	va_start(ap, fmt);
275169320Spiso	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
276169320Spiso	va_end(ap);
277169320Spiso	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
278178092Sjeff	mtx_lock(&event_lock);
279169320Spiso	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
280178092Sjeff	mtx_unlock(&event_lock);
281169320Spiso	if (event != NULL)
282169320Spiso		*event = ie;
283169320Spiso	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
284169320Spiso	return (0);
285169320Spiso}
286169320Spiso
287177181Sjhb/*
288177181Sjhb * Bind an interrupt event to the specified CPU.  Note that not all
289177181Sjhb * platforms support binding an interrupt to a CPU.  For those
290177181Sjhb * platforms this request will fail.  For supported platforms, any
291177181Sjhb * associated ithreads as well as the primary interrupt context will
292177181Sjhb * be bound to the specificed CPU.  Using a cpu id of NOCPU unbinds
293177181Sjhb * the interrupt event.
294177181Sjhb */
295151658Sjhbint
296177181Sjhbintr_event_bind(struct intr_event *ie, u_char cpu)
297177181Sjhb{
298178092Sjeff	cpuset_t mask;
299178092Sjeff	lwpid_t id;
300177181Sjhb	int error;
301177181Sjhb
302177181Sjhb	/* Need a CPU to bind to. */
303177181Sjhb	if (cpu != NOCPU && CPU_ABSENT(cpu))
304177181Sjhb		return (EINVAL);
305177181Sjhb
306177181Sjhb	if (ie->ie_assign_cpu == NULL)
307177181Sjhb		return (EOPNOTSUPP);
308195249Sjhb
309195249Sjhb	error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
310195249Sjhb	if (error)
311195249Sjhb		return (error);
312195249Sjhb
313178092Sjeff	/*
314195249Sjhb	 * If we have any ithreads try to set their mask first to verify
315195249Sjhb	 * permissions, etc.
316178092Sjeff	 */
317177181Sjhb	mtx_lock(&ie->ie_lock);
318178092Sjeff	if (ie->ie_thread != NULL) {
319178092Sjeff		CPU_ZERO(&mask);
320178092Sjeff		if (cpu == NOCPU)
321178092Sjeff			CPU_COPY(cpuset_root, &mask);
322178092Sjeff		else
323178092Sjeff			CPU_SET(cpu, &mask);
324178092Sjeff		id = ie->ie_thread->it_thread->td_tid;
325177181Sjhb		mtx_unlock(&ie->ie_lock);
326178092Sjeff		error = cpuset_setthread(id, &mask);
327178092Sjeff		if (error)
328178092Sjeff			return (error);
329178092Sjeff	} else
330178092Sjeff		mtx_unlock(&ie->ie_lock);
331177181Sjhb	error = ie->ie_assign_cpu(ie->ie_source, cpu);
332195249Sjhb	if (error) {
333195249Sjhb		mtx_lock(&ie->ie_lock);
334195249Sjhb		if (ie->ie_thread != NULL) {
335195249Sjhb			CPU_ZERO(&mask);
336195249Sjhb			if (ie->ie_cpu == NOCPU)
337195249Sjhb				CPU_COPY(cpuset_root, &mask);
338195249Sjhb			else
339195249Sjhb				CPU_SET(cpu, &mask);
340195249Sjhb			id = ie->ie_thread->it_thread->td_tid;
341195249Sjhb			mtx_unlock(&ie->ie_lock);
342195249Sjhb			(void)cpuset_setthread(id, &mask);
343195249Sjhb		} else
344195249Sjhb			mtx_unlock(&ie->ie_lock);
345177181Sjhb		return (error);
346195249Sjhb	}
347195249Sjhb
348177181Sjhb	mtx_lock(&ie->ie_lock);
349177181Sjhb	ie->ie_cpu = cpu;
350177181Sjhb	mtx_unlock(&ie->ie_lock);
351178092Sjeff
352178092Sjeff	return (error);
353178092Sjeff}
354178092Sjeff
355178092Sjeffstatic struct intr_event *
356178092Sjeffintr_lookup(int irq)
357178092Sjeff{
358178092Sjeff	struct intr_event *ie;
359178092Sjeff
360178092Sjeff	mtx_lock(&event_lock);
361178092Sjeff	TAILQ_FOREACH(ie, &event_list, ie_list)
362178092Sjeff		if (ie->ie_irq == irq &&
363178092Sjeff		    (ie->ie_flags & IE_SOFT) == 0 &&
364178092Sjeff		    TAILQ_FIRST(&ie->ie_handlers) != NULL)
365178092Sjeff			break;
366178092Sjeff	mtx_unlock(&event_lock);
367178092Sjeff	return (ie);
368178092Sjeff}
369178092Sjeff
370178092Sjeffint
371178092Sjeffintr_setaffinity(int irq, void *m)
372178092Sjeff{
373178092Sjeff	struct intr_event *ie;
374178092Sjeff	cpuset_t *mask;
375178092Sjeff	u_char cpu;
376178092Sjeff	int n;
377178092Sjeff
378178092Sjeff	mask = m;
379178092Sjeff	cpu = NOCPU;
380178092Sjeff	/*
381178092Sjeff	 * If we're setting all cpus we can unbind.  Otherwise make sure
382178092Sjeff	 * only one cpu is in the set.
383178092Sjeff	 */
384178092Sjeff	if (CPU_CMP(cpuset_root, mask)) {
385178092Sjeff		for (n = 0; n < CPU_SETSIZE; n++) {
386178092Sjeff			if (!CPU_ISSET(n, mask))
387178092Sjeff				continue;
388178092Sjeff			if (cpu != NOCPU)
389178092Sjeff				return (EINVAL);
390178092Sjeff			cpu = (u_char)n;
391178092Sjeff		}
392178092Sjeff	}
393178092Sjeff	ie = intr_lookup(irq);
394178092Sjeff	if (ie == NULL)
395178092Sjeff		return (ESRCH);
396194987Sjhb	return (intr_event_bind(ie, cpu));
397178092Sjeff}
398178092Sjeff
399178092Sjeffint
400178092Sjeffintr_getaffinity(int irq, void *m)
401178092Sjeff{
402178092Sjeff	struct intr_event *ie;
403178092Sjeff	cpuset_t *mask;
404178092Sjeff
405178092Sjeff	mask = m;
406178092Sjeff	ie = intr_lookup(irq);
407178092Sjeff	if (ie == NULL)
408178092Sjeff		return (ESRCH);
409178092Sjeff	CPU_ZERO(mask);
410178092Sjeff	mtx_lock(&ie->ie_lock);
411178092Sjeff	if (ie->ie_cpu == NOCPU)
412178092Sjeff		CPU_COPY(cpuset_root, mask);
413178092Sjeff	else
414178092Sjeff		CPU_SET(ie->ie_cpu, mask);
415178092Sjeff	mtx_unlock(&ie->ie_lock);
416177181Sjhb	return (0);
417177181Sjhb}
418177181Sjhb
419177181Sjhbint
420151658Sjhbintr_event_destroy(struct intr_event *ie)
421151658Sjhb{
422151658Sjhb
423178092Sjeff	mtx_lock(&event_lock);
424151658Sjhb	mtx_lock(&ie->ie_lock);
425151658Sjhb	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
426151658Sjhb		mtx_unlock(&ie->ie_lock);
427178092Sjeff		mtx_unlock(&event_lock);
428151658Sjhb		return (EBUSY);
429151658Sjhb	}
430151658Sjhb	TAILQ_REMOVE(&event_list, ie, ie_list);
431157728Sjhb#ifndef notyet
432157728Sjhb	if (ie->ie_thread != NULL) {
433157728Sjhb		ithread_destroy(ie->ie_thread);
434157728Sjhb		ie->ie_thread = NULL;
435157728Sjhb	}
436157728Sjhb#endif
437151658Sjhb	mtx_unlock(&ie->ie_lock);
438178092Sjeff	mtx_unlock(&event_lock);
439151658Sjhb	mtx_destroy(&ie->ie_lock);
440151658Sjhb	free(ie, M_ITHREAD);
441151658Sjhb	return (0);
442151658Sjhb}
443151658Sjhb
444169320Spiso#ifndef INTR_FILTER
445151658Sjhbstatic struct intr_thread *
446151658Sjhbithread_create(const char *name)
447151658Sjhb{
448151658Sjhb	struct intr_thread *ithd;
449151658Sjhb	struct thread *td;
450151658Sjhb	int error;
451151658Sjhb
452151658Sjhb	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
453151658Sjhb
454173004Sjulian	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
455173004Sjulian		    &td, RFSTOPPED | RFHIGHPID,
456173051Sjulian	    	    0, "intr", "%s", name);
457151658Sjhb	if (error)
458172836Sjulian		panic("kproc_create() failed with %d", error);
459170307Sjeff	thread_lock(td);
460164936Sjulian	sched_class(td, PRI_ITHD);
461103216Sjulian	TD_SET_IWAIT(td);
462170307Sjeff	thread_unlock(td);
463151658Sjhb	td->td_pflags |= TDP_ITHREAD;
464151658Sjhb	ithd->it_thread = td;
465151658Sjhb	CTR2(KTR_INTR, "%s: created %s", __func__, name);
466151658Sjhb	return (ithd);
46772237Sjhb}
468169320Spiso#else
469169320Spisostatic struct intr_thread *
470169320Spisoithread_create(const char *name, struct intr_handler *ih)
471169320Spiso{
472169320Spiso	struct intr_thread *ithd;
473169320Spiso	struct thread *td;
474169320Spiso	int error;
47572237Sjhb
476169320Spiso	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
477169320Spiso
478173153Sjulian	error = kproc_kthread_add(ithread_loop, ih, &intrproc,
479173004Sjulian		    &td, RFSTOPPED | RFHIGHPID,
480173051Sjulian	    	    0, "intr", "%s", name);
481169320Spiso	if (error)
482172836Sjulian		panic("kproc_create() failed with %d", error);
483170307Sjeff	thread_lock(td);
484169320Spiso	sched_class(td, PRI_ITHD);
485169320Spiso	TD_SET_IWAIT(td);
486170307Sjeff	thread_unlock(td);
487169320Spiso	td->td_pflags |= TDP_ITHREAD;
488169320Spiso	ithd->it_thread = td;
489169320Spiso	CTR2(KTR_INTR, "%s: created %s", __func__, name);
490169320Spiso	return (ithd);
491169320Spiso}
492169320Spiso#endif
493169320Spiso
494151658Sjhbstatic void
495151658Sjhbithread_destroy(struct intr_thread *ithread)
49672237Sjhb{
49783366Sjulian	struct thread *td;
49872237Sjhb
499157784Sscottl	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
500151658Sjhb	td = ithread->it_thread;
501170307Sjeff	thread_lock(td);
50276771Sjhb	ithread->it_flags |= IT_DEAD;
503103216Sjulian	if (TD_AWAITING_INTR(td)) {
504103216Sjulian		TD_CLR_IWAIT(td);
505166188Sjeff		sched_add(td, SRQ_INTR);
50672237Sjhb	}
507170307Sjeff	thread_unlock(td);
50872237Sjhb}
50972237Sjhb
510169320Spiso#ifndef INTR_FILTER
51172237Sjhbint
512151658Sjhbintr_event_add_handler(struct intr_event *ie, const char *name,
513166901Spiso    driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
514166901Spiso    enum intr_type flags, void **cookiep)
51572237Sjhb{
516151658Sjhb	struct intr_handler *ih, *temp_ih;
517151658Sjhb	struct intr_thread *it;
51872237Sjhb
519166901Spiso	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
52072237Sjhb		return (EINVAL);
52172237Sjhb
522151658Sjhb	/* Allocate and populate an interrupt handler structure. */
523151658Sjhb	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
524166901Spiso	ih->ih_filter = filter;
52572237Sjhb	ih->ih_handler = handler;
52672237Sjhb	ih->ih_argument = arg;
527198134Sjhb	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
528151658Sjhb	ih->ih_event = ie;
52972237Sjhb	ih->ih_pri = pri;
530166901Spiso	if (flags & INTR_EXCL)
53172237Sjhb		ih->ih_flags = IH_EXCLUSIVE;
53272237Sjhb	if (flags & INTR_MPSAFE)
53372237Sjhb		ih->ih_flags |= IH_MPSAFE;
53472237Sjhb	if (flags & INTR_ENTROPY)
53572237Sjhb		ih->ih_flags |= IH_ENTROPY;
53672237Sjhb
537151658Sjhb	/* We can only have one exclusive handler in a event. */
538151658Sjhb	mtx_lock(&ie->ie_lock);
539151658Sjhb	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
540151658Sjhb		if ((flags & INTR_EXCL) ||
541151658Sjhb		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
542151658Sjhb			mtx_unlock(&ie->ie_lock);
543151658Sjhb			free(ih, M_ITHREAD);
544151658Sjhb			return (EINVAL);
545151658Sjhb		}
546122002Sjhb	}
54772237Sjhb
548151658Sjhb	/* Create a thread if we need one. */
549166901Spiso	while (ie->ie_thread == NULL && handler != NULL) {
550151658Sjhb		if (ie->ie_flags & IE_ADDING_THREAD)
551157815Sjhb			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
552151658Sjhb		else {
553151658Sjhb			ie->ie_flags |= IE_ADDING_THREAD;
554151658Sjhb			mtx_unlock(&ie->ie_lock);
555151658Sjhb			it = ithread_create("intr: newborn");
556151658Sjhb			mtx_lock(&ie->ie_lock);
557151658Sjhb			ie->ie_flags &= ~IE_ADDING_THREAD;
558151658Sjhb			ie->ie_thread = it;
559151658Sjhb			it->it_event = ie;
560151658Sjhb			ithread_update(it);
561151658Sjhb			wakeup(ie);
562151658Sjhb		}
563151658Sjhb	}
564239095Skan
565239095Skan	/* Add the new handler to the event in priority order. */
566239095Skan	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
567239095Skan		if (temp_ih->ih_pri > ih->ih_pri)
568239095Skan			break;
569239095Skan	}
570239095Skan	if (temp_ih == NULL)
571239095Skan		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
572239095Skan	else
573239095Skan		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
574239095Skan	intr_event_update(ie);
575239095Skan
576151658Sjhb	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
577151658Sjhb	    ie->ie_name);
578151658Sjhb	mtx_unlock(&ie->ie_lock);
579151658Sjhb
58072237Sjhb	if (cookiep != NULL)
58172237Sjhb		*cookiep = ih;
58272237Sjhb	return (0);
58372237Sjhb}
584169320Spiso#else
585169320Spisoint
586169320Spisointr_event_add_handler(struct intr_event *ie, const char *name,
587169320Spiso    driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
588169320Spiso    enum intr_type flags, void **cookiep)
589169320Spiso{
590169320Spiso	struct intr_handler *ih, *temp_ih;
591169320Spiso	struct intr_thread *it;
59272237Sjhb
593169320Spiso	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
594169320Spiso		return (EINVAL);
595169320Spiso
596169320Spiso	/* Allocate and populate an interrupt handler structure. */
597169320Spiso	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
598169320Spiso	ih->ih_filter = filter;
599169320Spiso	ih->ih_handler = handler;
600169320Spiso	ih->ih_argument = arg;
601198134Sjhb	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
602169320Spiso	ih->ih_event = ie;
603169320Spiso	ih->ih_pri = pri;
604169320Spiso	if (flags & INTR_EXCL)
605169320Spiso		ih->ih_flags = IH_EXCLUSIVE;
606169320Spiso	if (flags & INTR_MPSAFE)
607169320Spiso		ih->ih_flags |= IH_MPSAFE;
608169320Spiso	if (flags & INTR_ENTROPY)
609169320Spiso		ih->ih_flags |= IH_ENTROPY;
610169320Spiso
611169320Spiso	/* We can only have one exclusive handler in a event. */
612169320Spiso	mtx_lock(&ie->ie_lock);
613169320Spiso	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
614169320Spiso		if ((flags & INTR_EXCL) ||
615169320Spiso		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
616169320Spiso			mtx_unlock(&ie->ie_lock);
617169320Spiso			free(ih, M_ITHREAD);
618169320Spiso			return (EINVAL);
619169320Spiso		}
620169320Spiso	}
621169320Spiso
622169320Spiso	/* For filtered handlers, create a private ithread to run on. */
623239095Skan	if (filter != NULL && handler != NULL) {
624169320Spiso		mtx_unlock(&ie->ie_lock);
625239095Skan		it = ithread_create("intr: newborn", ih);
626169320Spiso		mtx_lock(&ie->ie_lock);
627239095Skan		it->it_event = ie;
628169320Spiso		ih->ih_thread = it;
629230231Spluknet		ithread_update(it); /* XXX - do we really need this?!?!? */
630169320Spiso	} else { /* Create the global per-event thread if we need one. */
631169320Spiso		while (ie->ie_thread == NULL && handler != NULL) {
632169320Spiso			if (ie->ie_flags & IE_ADDING_THREAD)
633169320Spiso				msleep(ie, &ie->ie_lock, 0, "ithread", 0);
634169320Spiso			else {
635169320Spiso				ie->ie_flags |= IE_ADDING_THREAD;
636169320Spiso				mtx_unlock(&ie->ie_lock);
637169320Spiso				it = ithread_create("intr: newborn", ih);
638169320Spiso				mtx_lock(&ie->ie_lock);
639169320Spiso				ie->ie_flags &= ~IE_ADDING_THREAD;
640169320Spiso				ie->ie_thread = it;
641169320Spiso				it->it_event = ie;
642169320Spiso				ithread_update(it);
643169320Spiso				wakeup(ie);
644169320Spiso			}
645169320Spiso		}
646169320Spiso	}
647239095Skan
648239095Skan	/* Add the new handler to the event in priority order. */
649239095Skan	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
650239095Skan		if (temp_ih->ih_pri > ih->ih_pri)
651239095Skan			break;
652239095Skan	}
653239095Skan	if (temp_ih == NULL)
654239095Skan		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
655239095Skan	else
656239095Skan		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
657239095Skan	intr_event_update(ie);
658239095Skan
659169320Spiso	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
660169320Spiso	    ie->ie_name);
661169320Spiso	mtx_unlock(&ie->ie_lock);
662169320Spiso
663169320Spiso	if (cookiep != NULL)
664169320Spiso		*cookiep = ih;
665169320Spiso	return (0);
666169320Spiso}
667169320Spiso#endif
668169320Spiso
669165125Sjhb/*
670198134Sjhb * Append a description preceded by a ':' to the name of the specified
671198134Sjhb * interrupt handler.
672198134Sjhb */
673198134Sjhbint
674198134Sjhbintr_event_describe_handler(struct intr_event *ie, void *cookie,
675198134Sjhb    const char *descr)
676198134Sjhb{
677198134Sjhb	struct intr_handler *ih;
678198134Sjhb	size_t space;
679198134Sjhb	char *start;
680198134Sjhb
681198134Sjhb	mtx_lock(&ie->ie_lock);
682198134Sjhb#ifdef INVARIANTS
683198134Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
684198134Sjhb		if (ih == cookie)
685198134Sjhb			break;
686198134Sjhb	}
687198134Sjhb	if (ih == NULL) {
688198134Sjhb		mtx_unlock(&ie->ie_lock);
689198149Sjhb		panic("handler %p not found in interrupt event %p", cookie, ie);
690198134Sjhb	}
691198134Sjhb#endif
692198134Sjhb	ih = cookie;
693198134Sjhb
694198134Sjhb	/*
695198134Sjhb	 * Look for an existing description by checking for an
696198134Sjhb	 * existing ":".  This assumes device names do not include
697198134Sjhb	 * colons.  If one is found, prepare to insert the new
698198134Sjhb	 * description at that point.  If one is not found, find the
699198134Sjhb	 * end of the name to use as the insertion point.
700198134Sjhb	 */
701229272Sed	start = strchr(ih->ih_name, ':');
702198134Sjhb	if (start == NULL)
703229272Sed		start = strchr(ih->ih_name, 0);
704198134Sjhb
705198134Sjhb	/*
706198134Sjhb	 * See if there is enough remaining room in the string for the
707198134Sjhb	 * description + ":".  The "- 1" leaves room for the trailing
708198134Sjhb	 * '\0'.  The "+ 1" accounts for the colon.
709198134Sjhb	 */
710198134Sjhb	space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
711198134Sjhb	if (strlen(descr) + 1 > space) {
712198134Sjhb		mtx_unlock(&ie->ie_lock);
713198134Sjhb		return (ENOSPC);
714198134Sjhb	}
715198134Sjhb
716198134Sjhb	/* Append a colon followed by the description. */
717198134Sjhb	*start = ':';
718198134Sjhb	strcpy(start + 1, descr);
719198134Sjhb	intr_event_update(ie);
720198134Sjhb	mtx_unlock(&ie->ie_lock);
721198134Sjhb	return (0);
722198134Sjhb}
723198134Sjhb
724198134Sjhb/*
725165125Sjhb * Return the ie_source field from the intr_event an intr_handler is
726165125Sjhb * associated with.
727165125Sjhb */
728165125Sjhbvoid *
729165125Sjhbintr_handler_source(void *cookie)
730165125Sjhb{
731165125Sjhb	struct intr_handler *ih;
732165125Sjhb	struct intr_event *ie;
733165125Sjhb
734165125Sjhb	ih = (struct intr_handler *)cookie;
735165125Sjhb	if (ih == NULL)
736165125Sjhb		return (NULL);
737165125Sjhb	ie = ih->ih_event;
738165125Sjhb	KASSERT(ie != NULL,
739165125Sjhb	    ("interrupt handler \"%s\" has a NULL interrupt event",
740165125Sjhb	    ih->ih_name));
741165125Sjhb	return (ie->ie_source);
742165125Sjhb}
743165125Sjhb
744219819Sjeff/*
745219819Sjeff * Sleep until an ithread finishes executing an interrupt handler.
746219819Sjeff *
747219819Sjeff * XXX Doesn't currently handle interrupt filters or fast interrupt
748219819Sjeff * handlers.  This is intended for compatibility with linux drivers
749219819Sjeff * only.  Do not use in BSD code.
750219819Sjeff */
751219819Sjeffvoid
752219819Sjeff_intr_drain(int irq)
753219819Sjeff{
754219819Sjeff	struct intr_event *ie;
755219819Sjeff	struct intr_thread *ithd;
756219819Sjeff	struct thread *td;
757219819Sjeff
758219819Sjeff	ie = intr_lookup(irq);
759219819Sjeff	if (ie == NULL)
760219819Sjeff		return;
761219819Sjeff	if (ie->ie_thread == NULL)
762219819Sjeff		return;
763219819Sjeff	ithd = ie->ie_thread;
764219819Sjeff	td = ithd->it_thread;
765221055Sjeff	/*
766221055Sjeff	 * We set the flag and wait for it to be cleared to avoid
767221055Sjeff	 * long delays with potentially busy interrupt handlers
768221055Sjeff	 * were we to only sample TD_AWAITING_INTR() every tick.
769221055Sjeff	 */
770219819Sjeff	thread_lock(td);
771219819Sjeff	if (!TD_AWAITING_INTR(td)) {
772219819Sjeff		ithd->it_flags |= IT_WAIT;
773221055Sjeff		while (ithd->it_flags & IT_WAIT) {
774221055Sjeff			thread_unlock(td);
775221055Sjeff			pause("idrain", 1);
776221055Sjeff			thread_lock(td);
777221055Sjeff		}
778219819Sjeff	}
779221055Sjeff	thread_unlock(td);
780219819Sjeff	return;
781219819Sjeff}
782219819Sjeff
783219819Sjeff
784169320Spiso#ifndef INTR_FILTER
78572237Sjhbint
786151658Sjhbintr_event_remove_handler(void *cookie)
78772237Sjhb{
788151658Sjhb	struct intr_handler *handler = (struct intr_handler *)cookie;
789151658Sjhb	struct intr_event *ie;
79072237Sjhb#ifdef INVARIANTS
791151658Sjhb	struct intr_handler *ih;
79272237Sjhb#endif
793151658Sjhb#ifdef notyet
794151658Sjhb	int dead;
795151658Sjhb#endif
79672237Sjhb
79772759Sjhb	if (handler == NULL)
79872237Sjhb		return (EINVAL);
799151658Sjhb	ie = handler->ih_event;
800151658Sjhb	KASSERT(ie != NULL,
801151658Sjhb	    ("interrupt handler \"%s\" has a NULL interrupt event",
802165124Sjhb	    handler->ih_name));
803151658Sjhb	mtx_lock(&ie->ie_lock);
80487593Sobrien	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
805151658Sjhb	    ie->ie_name);
80672237Sjhb#ifdef INVARIANTS
807151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
80872759Sjhb		if (ih == handler)
80972759Sjhb			goto ok;
810151658Sjhb	mtx_unlock(&ie->ie_lock);
811151658Sjhb	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
812151658Sjhb	    ih->ih_name, ie->ie_name);
81372759Sjhbok:
81472237Sjhb#endif
81572839Sjhb	/*
816151658Sjhb	 * If there is no ithread, then just remove the handler and return.
817151658Sjhb	 * XXX: Note that an INTR_FAST handler might be running on another
818151658Sjhb	 * CPU!
819151658Sjhb	 */
820151658Sjhb	if (ie->ie_thread == NULL) {
821151658Sjhb		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
822151658Sjhb		mtx_unlock(&ie->ie_lock);
823151658Sjhb		free(handler, M_ITHREAD);
824151658Sjhb		return (0);
825151658Sjhb	}
826151658Sjhb
827151658Sjhb	/*
82872839Sjhb	 * If the interrupt thread is already running, then just mark this
82972839Sjhb	 * handler as being dead and let the ithread do the actual removal.
830124505Struckman	 *
831124505Struckman	 * During a cold boot while cold is set, msleep() does not sleep,
832124505Struckman	 * so we have to remove the handler here rather than letting the
833124505Struckman	 * thread do it.
83472839Sjhb	 */
835170307Sjeff	thread_lock(ie->ie_thread->it_thread);
836151658Sjhb	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
83772839Sjhb		handler->ih_flags |= IH_DEAD;
83872839Sjhb
83972839Sjhb		/*
84072839Sjhb		 * Ensure that the thread will process the handler list
84172839Sjhb		 * again and remove this handler if it has already passed
84272839Sjhb		 * it on the list.
84372839Sjhb		 */
844151658Sjhb		ie->ie_thread->it_need = 1;
845151658Sjhb	} else
846151658Sjhb		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
847170307Sjeff	thread_unlock(ie->ie_thread->it_thread);
848151658Sjhb	while (handler->ih_flags & IH_DEAD)
849157815Sjhb		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
850151658Sjhb	intr_event_update(ie);
851151658Sjhb#ifdef notyet
852151658Sjhb	/*
853151658Sjhb	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
854151658Sjhb	 * this could lead to races of stale data when servicing an
855151658Sjhb	 * interrupt.
856151658Sjhb	 */
857151658Sjhb	dead = 1;
858151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
859151658Sjhb		if (!(ih->ih_flags & IH_FAST)) {
860151658Sjhb			dead = 0;
861151658Sjhb			break;
862151658Sjhb		}
863151658Sjhb	}
864151658Sjhb	if (dead) {
865151658Sjhb		ithread_destroy(ie->ie_thread);
866151658Sjhb		ie->ie_thread = NULL;
867151658Sjhb	}
868151658Sjhb#endif
869151658Sjhb	mtx_unlock(&ie->ie_lock);
87076771Sjhb	free(handler, M_ITHREAD);
87172237Sjhb	return (0);
87272237Sjhb}
87372237Sjhb
874177940Sjhbstatic int
875151658Sjhbintr_event_schedule_thread(struct intr_event *ie)
87672759Sjhb{
877151658Sjhb	struct intr_entropy entropy;
878151658Sjhb	struct intr_thread *it;
87983366Sjulian	struct thread *td;
880101176Sjulian	struct thread *ctd;
88172759Sjhb	struct proc *p;
88272759Sjhb
88372759Sjhb	/*
88472759Sjhb	 * If no ithread or no handlers, then we have a stray interrupt.
88572759Sjhb	 */
886151658Sjhb	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
887151658Sjhb	    ie->ie_thread == NULL)
88872759Sjhb		return (EINVAL);
88972759Sjhb
890101176Sjulian	ctd = curthread;
891151658Sjhb	it = ie->ie_thread;
892151658Sjhb	td = it->it_thread;
893133191Srwatson	p = td->td_proc;
894151658Sjhb
89572759Sjhb	/*
89672759Sjhb	 * If any of the handlers for this ithread claim to be good
89772759Sjhb	 * sources of entropy, then gather some.
89872759Sjhb	 */
899151658Sjhb	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
900133191Srwatson		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
901173004Sjulian		    p->p_pid, td->td_name);
902151658Sjhb		entropy.event = (uintptr_t)ie;
903151658Sjhb		entropy.td = ctd;
90472759Sjhb		random_harvest(&entropy, sizeof(entropy), 2, 0,
90572759Sjhb		    RANDOM_INTERRUPT);
90672759Sjhb	}
90772759Sjhb
908151658Sjhb	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
90972759Sjhb
91072759Sjhb	/*
91172759Sjhb	 * Set it_need to tell the thread to keep running if it is already
912170307Sjeff	 * running.  Then, lock the thread and see if we actually need to
913170307Sjeff	 * put it on the runqueue.
91472759Sjhb	 */
915151658Sjhb	it->it_need = 1;
916170307Sjeff	thread_lock(td);
917103216Sjulian	if (TD_AWAITING_INTR(td)) {
918151658Sjhb		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
919173004Sjulian		    td->td_name);
920103216Sjulian		TD_CLR_IWAIT(td);
921166188Sjeff		sched_add(td, SRQ_INTR);
92272759Sjhb	} else {
923151658Sjhb		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
924173004Sjulian		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
92572759Sjhb	}
926170307Sjeff	thread_unlock(td);
92772759Sjhb
92872759Sjhb	return (0);
92972759Sjhb}
930169320Spiso#else
931169320Spisoint
932169320Spisointr_event_remove_handler(void *cookie)
933169320Spiso{
934169320Spiso	struct intr_handler *handler = (struct intr_handler *)cookie;
935169320Spiso	struct intr_event *ie;
936169320Spiso	struct intr_thread *it;
937169320Spiso#ifdef INVARIANTS
938169320Spiso	struct intr_handler *ih;
939169320Spiso#endif
940169320Spiso#ifdef notyet
941169320Spiso	int dead;
942169320Spiso#endif
94372759Sjhb
944169320Spiso	if (handler == NULL)
945169320Spiso		return (EINVAL);
946169320Spiso	ie = handler->ih_event;
947169320Spiso	KASSERT(ie != NULL,
948169320Spiso	    ("interrupt handler \"%s\" has a NULL interrupt event",
949169320Spiso	    handler->ih_name));
950169320Spiso	mtx_lock(&ie->ie_lock);
951169320Spiso	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
952169320Spiso	    ie->ie_name);
953169320Spiso#ifdef INVARIANTS
954169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
955169320Spiso		if (ih == handler)
956169320Spiso			goto ok;
957169320Spiso	mtx_unlock(&ie->ie_lock);
958169320Spiso	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
959169320Spiso	    ih->ih_name, ie->ie_name);
960169320Spisook:
961169320Spiso#endif
962169320Spiso	/*
963169320Spiso	 * If there are no ithreads (per event and per handler), then
964169320Spiso	 * just remove the handler and return.
965169320Spiso	 * XXX: Note that an INTR_FAST handler might be running on another CPU!
966169320Spiso	 */
967169320Spiso	if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
968169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
969169320Spiso		mtx_unlock(&ie->ie_lock);
970169320Spiso		free(handler, M_ITHREAD);
971169320Spiso		return (0);
972169320Spiso	}
973169320Spiso
974169320Spiso	/* Private or global ithread? */
975169320Spiso	it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
976169320Spiso	/*
977169320Spiso	 * If the interrupt thread is already running, then just mark this
978169320Spiso	 * handler as being dead and let the ithread do the actual removal.
979169320Spiso	 *
980169320Spiso	 * During a cold boot while cold is set, msleep() does not sleep,
981169320Spiso	 * so we have to remove the handler here rather than letting the
982169320Spiso	 * thread do it.
983169320Spiso	 */
984170307Sjeff	thread_lock(it->it_thread);
985169320Spiso	if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
986169320Spiso		handler->ih_flags |= IH_DEAD;
987169320Spiso
988169320Spiso		/*
989169320Spiso		 * Ensure that the thread will process the handler list
990169320Spiso		 * again and remove this handler if it has already passed
991169320Spiso		 * it on the list.
992169320Spiso		 */
993169320Spiso		it->it_need = 1;
994169320Spiso	} else
995169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
996170307Sjeff	thread_unlock(it->it_thread);
997169320Spiso	while (handler->ih_flags & IH_DEAD)
998169320Spiso		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
999169320Spiso	/*
1000169320Spiso	 * At this point, the handler has been disconnected from the event,
1001169320Spiso	 * so we can kill the private ithread if any.
1002169320Spiso	 */
1003169320Spiso	if (handler->ih_thread) {
1004169320Spiso		ithread_destroy(handler->ih_thread);
1005169320Spiso		handler->ih_thread = NULL;
1006169320Spiso	}
1007169320Spiso	intr_event_update(ie);
1008169320Spiso#ifdef notyet
1009169320Spiso	/*
1010169320Spiso	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
1011169320Spiso	 * this could lead to races of stale data when servicing an
1012169320Spiso	 * interrupt.
1013169320Spiso	 */
1014169320Spiso	dead = 1;
1015169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1016169320Spiso		if (handler != NULL) {
1017169320Spiso			dead = 0;
1018169320Spiso			break;
1019169320Spiso		}
1020169320Spiso	}
1021169320Spiso	if (dead) {
1022169320Spiso		ithread_destroy(ie->ie_thread);
1023169320Spiso		ie->ie_thread = NULL;
1024169320Spiso	}
1025169320Spiso#endif
1026169320Spiso	mtx_unlock(&ie->ie_lock);
1027169320Spiso	free(handler, M_ITHREAD);
1028169320Spiso	return (0);
1029169320Spiso}
1030169320Spiso
1031177940Sjhbstatic int
1032169320Spisointr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
1033169320Spiso{
1034169320Spiso	struct intr_entropy entropy;
1035169320Spiso	struct thread *td;
1036169320Spiso	struct thread *ctd;
1037169320Spiso	struct proc *p;
1038169320Spiso
1039169320Spiso	/*
1040169320Spiso	 * If no ithread or no handlers, then we have a stray interrupt.
1041169320Spiso	 */
1042169320Spiso	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
1043169320Spiso		return (EINVAL);
1044169320Spiso
1045169320Spiso	ctd = curthread;
1046169320Spiso	td = it->it_thread;
1047169320Spiso	p = td->td_proc;
1048169320Spiso
1049169320Spiso	/*
1050169320Spiso	 * If any of the handlers for this ithread claim to be good
1051169320Spiso	 * sources of entropy, then gather some.
1052169320Spiso	 */
1053169320Spiso	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
1054169320Spiso		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
1055173004Sjulian		    p->p_pid, td->td_name);
1056169320Spiso		entropy.event = (uintptr_t)ie;
1057169320Spiso		entropy.td = ctd;
1058169320Spiso		random_harvest(&entropy, sizeof(entropy), 2, 0,
1059169320Spiso		    RANDOM_INTERRUPT);
1060169320Spiso	}
1061169320Spiso
1062169320Spiso	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
1063169320Spiso
1064169320Spiso	/*
1065169320Spiso	 * Set it_need to tell the thread to keep running if it is already
1066170307Sjeff	 * running.  Then, lock the thread and see if we actually need to
1067170307Sjeff	 * put it on the runqueue.
1068169320Spiso	 */
1069169320Spiso	it->it_need = 1;
1070170307Sjeff	thread_lock(td);
1071169320Spiso	if (TD_AWAITING_INTR(td)) {
1072169320Spiso		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
1073173122Sjulian		    td->td_name);
1074169320Spiso		TD_CLR_IWAIT(td);
1075169320Spiso		sched_add(td, SRQ_INTR);
1076169320Spiso	} else {
1077169320Spiso		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1078173004Sjulian		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
1079169320Spiso	}
1080170307Sjeff	thread_unlock(td);
1081169320Spiso
1082169320Spiso	return (0);
1083169320Spiso}
1084169320Spiso#endif
1085169320Spiso
1086151699Sjhb/*
1087192305Srwatson * Allow interrupt event binding for software interrupt handlers -- a no-op,
1088192305Srwatson * since interrupts are generated in software rather than being directed by
1089192305Srwatson * a PIC.
1090192305Srwatson */
1091192305Srwatsonstatic int
1092192305Srwatsonswi_assign_cpu(void *arg, u_char cpu)
1093192305Srwatson{
1094192305Srwatson
1095192305Srwatson	return (0);
1096192305Srwatson}
1097192305Srwatson
1098192305Srwatson/*
1099151699Sjhb * Add a software interrupt handler to a specified event.  If a given event
1100151699Sjhb * is not specified, then a new event is created.
1101151699Sjhb */
110272759Sjhbint
1103151658Sjhbswi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
110472237Sjhb	    void *arg, int pri, enum intr_type flags, void **cookiep)
110572237Sjhb{
1106198854Sattilio	struct thread *td;
1107151658Sjhb	struct intr_event *ie;
110872237Sjhb	int error;
110966698Sjhb
1110169320Spiso	if (flags & INTR_ENTROPY)
111172759Sjhb		return (EINVAL);
111272759Sjhb
1113151658Sjhb	ie = (eventp != NULL) ? *eventp : NULL;
111466698Sjhb
1115151658Sjhb	if (ie != NULL) {
1116151658Sjhb		if (!(ie->ie_flags & IE_SOFT))
1117151658Sjhb			return (EINVAL);
111872759Sjhb	} else {
1119178092Sjeff		error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1120192305Srwatson		    NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
112167551Sjhb		if (error)
112272237Sjhb			return (error);
1123151658Sjhb		if (eventp != NULL)
1124151658Sjhb			*eventp = ie;
112566698Sjhb	}
1126177859Sjeff	error = intr_event_add_handler(ie, name, NULL, handler, arg,
1127217292Sjhb	    PI_SWI(pri), flags, cookiep);
1128177859Sjeff	if (error)
1129177859Sjeff		return (error);
1130177859Sjeff	if (pri == SWI_CLOCK) {
1131198854Sattilio		td = ie->ie_thread->it_thread;
1132198854Sattilio		thread_lock(td);
1133198854Sattilio		td->td_flags |= TDF_NOLOAD;
1134198854Sattilio		thread_unlock(td);
1135177859Sjeff	}
1136177859Sjeff	return (0);
113766698Sjhb}
113866698Sjhb
113966698Sjhb/*
1140151658Sjhb * Schedule a software interrupt thread.
114166698Sjhb */
114267551Sjhbvoid
114372237Sjhbswi_sched(void *cookie, int flags)
114466698Sjhb{
1145151658Sjhb	struct intr_handler *ih = (struct intr_handler *)cookie;
1146151658Sjhb	struct intr_event *ie = ih->ih_event;
1147240921Sjhb	struct intr_entropy entropy;
114872759Sjhb	int error;
114966698Sjhb
1150151658Sjhb	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1151151658Sjhb	    ih->ih_need);
1152151658Sjhb
1153240921Sjhb	if (harvest.swi) {
1154240921Sjhb		CTR2(KTR_INTR, "swi_sched: pid %d (%s) gathering entropy",
1155240921Sjhb		    curproc->p_pid, curthread->td_name);
1156240921Sjhb		entropy.event = (uintptr_t)ih;
1157240921Sjhb		entropy.td = curthread;
1158240921Sjhb		random_harvest(&entropy, sizeof(entropy), 1, 0,
1159240921Sjhb		    RANDOM_INTERRUPT);
1160240921Sjhb	}
1161240921Sjhb
116267551Sjhb	/*
116372759Sjhb	 * Set ih_need for this handler so that if the ithread is already
116472759Sjhb	 * running it will execute this handler on the next pass.  Otherwise,
116572759Sjhb	 * it will execute it the next time it runs.
116667551Sjhb	 */
116772237Sjhb	atomic_store_rel_int(&ih->ih_need, 1);
1168163474Sbde
116972237Sjhb	if (!(flags & SWI_DELAY)) {
1170170291Sattilio		PCPU_INC(cnt.v_soft);
1171169320Spiso#ifdef INTR_FILTER
1172169320Spiso		error = intr_event_schedule_thread(ie, ie->ie_thread);
1173169320Spiso#else
1174151658Sjhb		error = intr_event_schedule_thread(ie);
1175169320Spiso#endif
117672759Sjhb		KASSERT(error == 0, ("stray software interrupt"));
117766698Sjhb	}
117866698Sjhb}
117966698Sjhb
1180151699Sjhb/*
1181151699Sjhb * Remove a software interrupt handler.  Currently this code does not
1182151699Sjhb * remove the associated interrupt event if it becomes empty.  Calling code
1183151699Sjhb * may do so manually via intr_event_destroy(), but that's not really
1184151699Sjhb * an optimal interface.
1185151699Sjhb */
1186151699Sjhbint
1187151699Sjhbswi_remove(void *cookie)
1188151699Sjhb{
1189151699Sjhb
1190151699Sjhb	return (intr_event_remove_handler(cookie));
1191151699Sjhb}
1192151699Sjhb
1193169320Spiso#ifdef INTR_FILTER
1194151658Sjhbstatic void
1195169320Spisopriv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1196169320Spiso{
1197169320Spiso	struct intr_event *ie;
1198169320Spiso
1199169320Spiso	ie = ih->ih_event;
1200169320Spiso	/*
1201169320Spiso	 * If this handler is marked for death, remove it from
1202169320Spiso	 * the list of handlers and wake up the sleeper.
1203169320Spiso	 */
1204169320Spiso	if (ih->ih_flags & IH_DEAD) {
1205169320Spiso		mtx_lock(&ie->ie_lock);
1206169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1207169320Spiso		ih->ih_flags &= ~IH_DEAD;
1208169320Spiso		wakeup(ih);
1209169320Spiso		mtx_unlock(&ie->ie_lock);
1210169320Spiso		return;
1211169320Spiso	}
1212169320Spiso
1213169320Spiso	/* Execute this handler. */
1214169320Spiso	CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1215169320Spiso	     __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1216169320Spiso	     ih->ih_name, ih->ih_flags);
1217169320Spiso
1218169320Spiso	if (!(ih->ih_flags & IH_MPSAFE))
1219169320Spiso		mtx_lock(&Giant);
1220169320Spiso	ih->ih_handler(ih->ih_argument);
1221169320Spiso	if (!(ih->ih_flags & IH_MPSAFE))
1222169320Spiso		mtx_unlock(&Giant);
1223169320Spiso}
1224169320Spiso#endif
1225169320Spiso
1226183052Sjhb/*
1227183052Sjhb * This is a public function for use by drivers that mux interrupt
1228183052Sjhb * handlers for child devices from their interrupt handler.
1229183052Sjhb */
1230183052Sjhbvoid
1231183052Sjhbintr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1232151658Sjhb{
1233151658Sjhb	struct intr_handler *ih, *ihn;
1234151658Sjhb
1235151658Sjhb	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1236151658Sjhb		/*
1237151658Sjhb		 * If this handler is marked for death, remove it from
1238151658Sjhb		 * the list of handlers and wake up the sleeper.
1239151658Sjhb		 */
1240151658Sjhb		if (ih->ih_flags & IH_DEAD) {
1241151658Sjhb			mtx_lock(&ie->ie_lock);
1242151658Sjhb			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1243151658Sjhb			ih->ih_flags &= ~IH_DEAD;
1244151658Sjhb			wakeup(ih);
1245151658Sjhb			mtx_unlock(&ie->ie_lock);
1246151658Sjhb			continue;
1247151658Sjhb		}
1248151658Sjhb
1249167080Spiso		/* Skip filter only handlers */
1250167080Spiso		if (ih->ih_handler == NULL)
1251167080Spiso			continue;
1252167080Spiso
1253151658Sjhb		/*
1254151658Sjhb		 * For software interrupt threads, we only execute
1255151658Sjhb		 * handlers that have their need flag set.  Hardware
1256151658Sjhb		 * interrupt threads always invoke all of their handlers.
1257151658Sjhb		 */
1258151658Sjhb		if (ie->ie_flags & IE_SOFT) {
1259151658Sjhb			if (!ih->ih_need)
1260151658Sjhb				continue;
1261151658Sjhb			else
1262151658Sjhb				atomic_store_rel_int(&ih->ih_need, 0);
1263151658Sjhb		}
1264151658Sjhb
1265151658Sjhb		/* Execute this handler. */
1266151658Sjhb		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1267169320Spiso		    __func__, p->p_pid, (void *)ih->ih_handler,
1268169320Spiso		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1269151658Sjhb
1270151658Sjhb		if (!(ih->ih_flags & IH_MPSAFE))
1271151658Sjhb			mtx_lock(&Giant);
1272151658Sjhb		ih->ih_handler(ih->ih_argument);
1273151658Sjhb		if (!(ih->ih_flags & IH_MPSAFE))
1274151658Sjhb			mtx_unlock(&Giant);
1275151658Sjhb	}
1276183052Sjhb}
1277183052Sjhb
1278183052Sjhbstatic void
1279183052Sjhbithread_execute_handlers(struct proc *p, struct intr_event *ie)
1280183052Sjhb{
1281183052Sjhb
1282183052Sjhb	/* Interrupt handlers should not sleep. */
1283151658Sjhb	if (!(ie->ie_flags & IE_SOFT))
1284183052Sjhb		THREAD_NO_SLEEPING();
1285183052Sjhb	intr_event_execute_handlers(p, ie);
1286183052Sjhb	if (!(ie->ie_flags & IE_SOFT))
1287151658Sjhb		THREAD_SLEEPING_OK();
1288151658Sjhb
1289151658Sjhb	/*
1290151658Sjhb	 * Interrupt storm handling:
1291151658Sjhb	 *
1292151658Sjhb	 * If this interrupt source is currently storming, then throttle
1293151658Sjhb	 * it to only fire the handler once  per clock tick.
1294151658Sjhb	 *
1295151658Sjhb	 * If this interrupt source is not currently storming, but the
1296151658Sjhb	 * number of back to back interrupts exceeds the storm threshold,
1297151658Sjhb	 * then enter storming mode.
1298151658Sjhb	 */
1299167173Sjhb	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1300167173Sjhb	    !(ie->ie_flags & IE_SOFT)) {
1301168850Snjl		/* Report the message only once every second. */
1302168850Snjl		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1303151658Sjhb			printf(
1304168850Snjl	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1305151658Sjhb			    ie->ie_name);
1306151658Sjhb		}
1307167173Sjhb		pause("istorm", 1);
1308151658Sjhb	} else
1309151658Sjhb		ie->ie_count++;
1310151658Sjhb
1311151658Sjhb	/*
1312151658Sjhb	 * Now that all the handlers have had a chance to run, reenable
1313151658Sjhb	 * the interrupt source.
1314151658Sjhb	 */
1315177940Sjhb	if (ie->ie_post_ithread != NULL)
1316177940Sjhb		ie->ie_post_ithread(ie->ie_source);
1317151658Sjhb}
1318151658Sjhb
1319169320Spiso#ifndef INTR_FILTER
132066698Sjhb/*
132172237Sjhb * This is the main code for interrupt threads.
132266698Sjhb */
1323104094Sphkstatic void
132472237Sjhbithread_loop(void *arg)
132566698Sjhb{
1326151658Sjhb	struct intr_thread *ithd;
1327151658Sjhb	struct intr_event *ie;
132883366Sjulian	struct thread *td;
132972237Sjhb	struct proc *p;
1330219819Sjeff	int wake;
1331151658Sjhb
133283366Sjulian	td = curthread;
133383366Sjulian	p = td->td_proc;
1334151658Sjhb	ithd = (struct intr_thread *)arg;
1335151658Sjhb	KASSERT(ithd->it_thread == td,
133687593Sobrien	    ("%s: ithread and proc linkage out of sync", __func__));
1337151658Sjhb	ie = ithd->it_event;
1338151658Sjhb	ie->ie_count = 0;
1339219819Sjeff	wake = 0;
134066698Sjhb
134167551Sjhb	/*
134267551Sjhb	 * As long as we have interrupts outstanding, go through the
134367551Sjhb	 * list of handlers, giving each one a go at it.
134467551Sjhb	 */
134566698Sjhb	for (;;) {
134672237Sjhb		/*
134772237Sjhb		 * If we are an orphaned thread, then just die.
134872237Sjhb		 */
134972237Sjhb		if (ithd->it_flags & IT_DEAD) {
1350151658Sjhb			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1351173004Sjulian			    p->p_pid, td->td_name);
135272237Sjhb			free(ithd, M_ITHREAD);
1353173044Sjulian			kthread_exit();
135472237Sjhb		}
135572237Sjhb
1356151658Sjhb		/*
1357151658Sjhb		 * Service interrupts.  If another interrupt arrives while
1358151658Sjhb		 * we are running, it will set it_need to note that we
1359151658Sjhb		 * should make another pass.
1360151658Sjhb		 */
136172237Sjhb		while (ithd->it_need) {
136267551Sjhb			/*
1363151658Sjhb			 * This might need a full read and write barrier
1364151658Sjhb			 * to make sure that this write posts before any
1365151658Sjhb			 * of the memory or device accesses in the
1366151658Sjhb			 * handlers.
136767551Sjhb			 */
136872237Sjhb			atomic_store_rel_int(&ithd->it_need, 0);
1369151658Sjhb			ithread_execute_handlers(p, ie);
137066698Sjhb		}
1371128331Sjhb		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1372128331Sjhb		mtx_assert(&Giant, MA_NOTOWNED);
137367551Sjhb
137466698Sjhb		/*
137566698Sjhb		 * Processed all our interrupts.  Now get the sched
137667551Sjhb		 * lock.  This may take a while and it_need may get
137766698Sjhb		 * set again, so we have to check it again.
137866698Sjhb		 */
1379170307Sjeff		thread_lock(td);
1380219819Sjeff		if (!ithd->it_need && !(ithd->it_flags & (IT_DEAD | IT_WAIT))) {
1381128331Sjhb			TD_SET_IWAIT(td);
1382151658Sjhb			ie->ie_count = 0;
1383178272Sjeff			mi_switch(SW_VOL | SWT_IWAIT, NULL);
138466698Sjhb		}
1385219819Sjeff		if (ithd->it_flags & IT_WAIT) {
1386219819Sjeff			wake = 1;
1387219819Sjeff			ithd->it_flags &= ~IT_WAIT;
1388219819Sjeff		}
1389170307Sjeff		thread_unlock(td);
1390219819Sjeff		if (wake) {
1391219819Sjeff			wakeup(ithd);
1392219819Sjeff			wake = 0;
1393219819Sjeff		}
139466698Sjhb	}
139566698Sjhb}
1396177940Sjhb
1397177940Sjhb/*
1398177940Sjhb * Main interrupt handling body.
1399177940Sjhb *
1400177940Sjhb * Input:
1401177940Sjhb * o ie:                        the event connected to this interrupt.
1402177940Sjhb * o frame:                     some archs (i.e. i386) pass a frame to some.
1403177940Sjhb *                              handlers as their main argument.
1404177940Sjhb * Return value:
1405177940Sjhb * o 0:                         everything ok.
1406177940Sjhb * o EINVAL:                    stray interrupt.
1407177940Sjhb */
1408177940Sjhbint
1409177940Sjhbintr_event_handle(struct intr_event *ie, struct trapframe *frame)
1410177940Sjhb{
1411177940Sjhb	struct intr_handler *ih;
1412208988Smav	struct trapframe *oldframe;
1413177940Sjhb	struct thread *td;
1414177940Sjhb	int error, ret, thread;
1415177940Sjhb
1416177940Sjhb	td = curthread;
1417177940Sjhb
1418177940Sjhb	/* An interrupt with no event or handlers is a stray interrupt. */
1419177940Sjhb	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1420177940Sjhb		return (EINVAL);
1421177940Sjhb
1422177940Sjhb	/*
1423177940Sjhb	 * Execute fast interrupt handlers directly.
1424177940Sjhb	 * To support clock handlers, if a handler registers
1425177940Sjhb	 * with a NULL argument, then we pass it a pointer to
1426177940Sjhb	 * a trapframe as its argument.
1427177940Sjhb	 */
1428177940Sjhb	td->td_intr_nesting_level++;
1429177940Sjhb	thread = 0;
1430177940Sjhb	ret = 0;
1431177940Sjhb	critical_enter();
1432208988Smav	oldframe = td->td_intr_frame;
1433208988Smav	td->td_intr_frame = frame;
1434177940Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1435177940Sjhb		if (ih->ih_filter == NULL) {
1436177940Sjhb			thread = 1;
1437177940Sjhb			continue;
1438177940Sjhb		}
1439177940Sjhb		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1440177940Sjhb		    ih->ih_filter, ih->ih_argument == NULL ? frame :
1441177940Sjhb		    ih->ih_argument, ih->ih_name);
1442177940Sjhb		if (ih->ih_argument == NULL)
1443177940Sjhb			ret = ih->ih_filter(frame);
1444177940Sjhb		else
1445177940Sjhb			ret = ih->ih_filter(ih->ih_argument);
1446203061Savg		KASSERT(ret == FILTER_STRAY ||
1447203061Savg		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1448203061Savg		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1449203061Savg		    ("%s: incorrect return value %#x from %s", __func__, ret,
1450203061Savg		    ih->ih_name));
1451203061Savg
1452177940Sjhb		/*
1453177940Sjhb		 * Wrapper handler special handling:
1454177940Sjhb		 *
1455177940Sjhb		 * in some particular cases (like pccard and pccbb),
1456177940Sjhb		 * the _real_ device handler is wrapped in a couple of
1457177940Sjhb		 * functions - a filter wrapper and an ithread wrapper.
1458177940Sjhb		 * In this case (and just in this case), the filter wrapper
1459177940Sjhb		 * could ask the system to schedule the ithread and mask
1460177940Sjhb		 * the interrupt source if the wrapped handler is composed
1461177940Sjhb		 * of just an ithread handler.
1462177940Sjhb		 *
1463177940Sjhb		 * TODO: write a generic wrapper to avoid people rolling
1464177940Sjhb		 * their own
1465177940Sjhb		 */
1466177940Sjhb		if (!thread) {
1467177940Sjhb			if (ret == FILTER_SCHEDULE_THREAD)
1468177940Sjhb				thread = 1;
1469177940Sjhb		}
1470177940Sjhb	}
1471208988Smav	td->td_intr_frame = oldframe;
1472177940Sjhb
1473177940Sjhb	if (thread) {
1474177940Sjhb		if (ie->ie_pre_ithread != NULL)
1475177940Sjhb			ie->ie_pre_ithread(ie->ie_source);
1476177940Sjhb	} else {
1477177940Sjhb		if (ie->ie_post_filter != NULL)
1478177940Sjhb			ie->ie_post_filter(ie->ie_source);
1479177940Sjhb	}
1480177940Sjhb
1481177940Sjhb	/* Schedule the ithread if needed. */
1482177940Sjhb	if (thread) {
1483177940Sjhb		error = intr_event_schedule_thread(ie);
1484182024Skmacy#ifndef XEN
1485177940Sjhb		KASSERT(error == 0, ("bad stray interrupt"));
1486182024Skmacy#else
1487182024Skmacy		if (error != 0)
1488182024Skmacy			log(LOG_WARNING, "bad stray interrupt");
1489182024Skmacy#endif
1490177940Sjhb	}
1491177940Sjhb	critical_exit();
1492177940Sjhb	td->td_intr_nesting_level--;
1493177940Sjhb	return (0);
1494177940Sjhb}
1495169320Spiso#else
1496169320Spiso/*
1497169320Spiso * This is the main code for interrupt threads.
1498169320Spiso */
1499169320Spisostatic void
1500169320Spisoithread_loop(void *arg)
1501169320Spiso{
1502169320Spiso	struct intr_thread *ithd;
1503169320Spiso	struct intr_handler *ih;
1504169320Spiso	struct intr_event *ie;
1505169320Spiso	struct thread *td;
1506169320Spiso	struct proc *p;
1507169320Spiso	int priv;
1508219819Sjeff	int wake;
150966698Sjhb
1510169320Spiso	td = curthread;
1511169320Spiso	p = td->td_proc;
1512169320Spiso	ih = (struct intr_handler *)arg;
1513169320Spiso	priv = (ih->ih_thread != NULL) ? 1 : 0;
1514169320Spiso	ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1515169320Spiso	KASSERT(ithd->it_thread == td,
1516169320Spiso	    ("%s: ithread and proc linkage out of sync", __func__));
1517169320Spiso	ie = ithd->it_event;
1518169320Spiso	ie->ie_count = 0;
1519219819Sjeff	wake = 0;
1520169320Spiso
1521169320Spiso	/*
1522169320Spiso	 * As long as we have interrupts outstanding, go through the
1523169320Spiso	 * list of handlers, giving each one a go at it.
1524169320Spiso	 */
1525169320Spiso	for (;;) {
1526169320Spiso		/*
1527169320Spiso		 * If we are an orphaned thread, then just die.
1528169320Spiso		 */
1529169320Spiso		if (ithd->it_flags & IT_DEAD) {
1530169320Spiso			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1531173004Sjulian			    p->p_pid, td->td_name);
1532169320Spiso			free(ithd, M_ITHREAD);
1533173044Sjulian			kthread_exit();
1534169320Spiso		}
1535169320Spiso
1536169320Spiso		/*
1537169320Spiso		 * Service interrupts.  If another interrupt arrives while
1538169320Spiso		 * we are running, it will set it_need to note that we
1539169320Spiso		 * should make another pass.
1540169320Spiso		 */
1541169320Spiso		while (ithd->it_need) {
1542169320Spiso			/*
1543169320Spiso			 * This might need a full read and write barrier
1544169320Spiso			 * to make sure that this write posts before any
1545169320Spiso			 * of the memory or device accesses in the
1546169320Spiso			 * handlers.
1547169320Spiso			 */
1548169320Spiso			atomic_store_rel_int(&ithd->it_need, 0);
1549169320Spiso			if (priv)
1550169320Spiso				priv_ithread_execute_handler(p, ih);
1551169320Spiso			else
1552169320Spiso				ithread_execute_handlers(p, ie);
1553169320Spiso		}
1554169320Spiso		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1555169320Spiso		mtx_assert(&Giant, MA_NOTOWNED);
1556169320Spiso
1557169320Spiso		/*
1558169320Spiso		 * Processed all our interrupts.  Now get the sched
1559169320Spiso		 * lock.  This may take a while and it_need may get
1560169320Spiso		 * set again, so we have to check it again.
1561169320Spiso		 */
1562170307Sjeff		thread_lock(td);
1563219819Sjeff		if (!ithd->it_need && !(ithd->it_flags & (IT_DEAD | IT_WAIT))) {
1564169320Spiso			TD_SET_IWAIT(td);
1565169320Spiso			ie->ie_count = 0;
1566178272Sjeff			mi_switch(SW_VOL | SWT_IWAIT, NULL);
1567169320Spiso		}
1568219819Sjeff		if (ithd->it_flags & IT_WAIT) {
1569219819Sjeff			wake = 1;
1570219819Sjeff			ithd->it_flags &= ~IT_WAIT;
1571219819Sjeff		}
1572170307Sjeff		thread_unlock(td);
1573219819Sjeff		if (wake) {
1574219819Sjeff			wakeup(ithd);
1575219819Sjeff			wake = 0;
1576219819Sjeff		}
1577169320Spiso	}
1578169320Spiso}
1579169320Spiso
1580169320Spiso/*
1581169320Spiso * Main loop for interrupt filter.
1582169320Spiso *
1583169320Spiso * Some architectures (i386, amd64 and arm) require the optional frame
1584169320Spiso * parameter, and use it as the main argument for fast handler execution
1585169320Spiso * when ih_argument == NULL.
1586169320Spiso *
1587169320Spiso * Return value:
1588169320Spiso * o FILTER_STRAY:              No filter recognized the event, and no
1589169320Spiso *                              filter-less handler is registered on this
1590169320Spiso *                              line.
1591169320Spiso * o FILTER_HANDLED:            A filter claimed the event and served it.
1592169320Spiso * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1593169320Spiso *                              least one filter-less handler on this line.
1594169320Spiso * o FILTER_HANDLED |
1595169320Spiso *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1596169320Spiso *                              scheduling the per-handler ithread.
1597169320Spiso *
1598169320Spiso * In case an ithread has to be scheduled, in *ithd there will be a
1599169320Spiso * pointer to a struct intr_thread containing the thread to be
1600169320Spiso * scheduled.
1601169320Spiso */
1602169320Spiso
1603177940Sjhbstatic int
1604169320Spisointr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1605169320Spiso		 struct intr_thread **ithd)
1606169320Spiso{
1607169320Spiso	struct intr_handler *ih;
1608169320Spiso	void *arg;
1609169320Spiso	int ret, thread_only;
1610169320Spiso
1611169320Spiso	ret = 0;
1612169320Spiso	thread_only = 0;
1613169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1614169320Spiso		/*
1615169320Spiso		 * Execute fast interrupt handlers directly.
1616169320Spiso		 * To support clock handlers, if a handler registers
1617169320Spiso		 * with a NULL argument, then we pass it a pointer to
1618169320Spiso		 * a trapframe as its argument.
1619169320Spiso		 */
1620169320Spiso		arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1621169320Spiso
1622169320Spiso		CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1623169320Spiso		     ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1624169320Spiso
1625169320Spiso		if (ih->ih_filter != NULL)
1626169320Spiso			ret = ih->ih_filter(arg);
1627169320Spiso		else {
1628169320Spiso			thread_only = 1;
1629169320Spiso			continue;
1630169320Spiso		}
1631203061Savg		KASSERT(ret == FILTER_STRAY ||
1632203061Savg		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1633203061Savg		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1634203061Savg		    ("%s: incorrect return value %#x from %s", __func__, ret,
1635203061Savg		    ih->ih_name));
1636169320Spiso		if (ret & FILTER_STRAY)
1637169320Spiso			continue;
1638169320Spiso		else {
1639169320Spiso			*ithd = ih->ih_thread;
1640169320Spiso			return (ret);
1641169320Spiso		}
1642169320Spiso	}
1643169320Spiso
1644169320Spiso	/*
1645169320Spiso	 * No filters handled the interrupt and we have at least
1646169320Spiso	 * one handler without a filter.  In this case, we schedule
1647169320Spiso	 * all of the filter-less handlers to run in the ithread.
1648169320Spiso	 */
1649169320Spiso	if (thread_only) {
1650169320Spiso		*ithd = ie->ie_thread;
1651169320Spiso		return (FILTER_SCHEDULE_THREAD);
1652169320Spiso	}
1653169320Spiso	return (FILTER_STRAY);
1654169320Spiso}
1655169320Spiso
1656169320Spiso/*
1657169320Spiso * Main interrupt handling body.
1658169320Spiso *
1659169320Spiso * Input:
1660169320Spiso * o ie:                        the event connected to this interrupt.
1661169320Spiso * o frame:                     some archs (i.e. i386) pass a frame to some.
1662169320Spiso *                              handlers as their main argument.
1663169320Spiso * Return value:
1664169320Spiso * o 0:                         everything ok.
1665169320Spiso * o EINVAL:                    stray interrupt.
1666169320Spiso */
1667169320Spisoint
1668169320Spisointr_event_handle(struct intr_event *ie, struct trapframe *frame)
1669169320Spiso{
1670169320Spiso	struct intr_thread *ithd;
1671208988Smav	struct trapframe *oldframe;
1672169320Spiso	struct thread *td;
1673169320Spiso	int thread;
1674169320Spiso
1675169320Spiso	ithd = NULL;
1676169320Spiso	td = curthread;
1677169320Spiso
1678169320Spiso	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1679169320Spiso		return (EINVAL);
1680169320Spiso
1681169320Spiso	td->td_intr_nesting_level++;
1682169320Spiso	thread = 0;
1683169320Spiso	critical_enter();
1684208988Smav	oldframe = td->td_intr_frame;
1685208988Smav	td->td_intr_frame = frame;
1686177940Sjhb	thread = intr_filter_loop(ie, frame, &ithd);
1687169320Spiso	if (thread & FILTER_HANDLED) {
1688177940Sjhb		if (ie->ie_post_filter != NULL)
1689177940Sjhb			ie->ie_post_filter(ie->ie_source);
1690169320Spiso	} else {
1691177940Sjhb		if (ie->ie_pre_ithread != NULL)
1692177940Sjhb			ie->ie_pre_ithread(ie->ie_source);
1693169320Spiso	}
1694208988Smav	td->td_intr_frame = oldframe;
1695169320Spiso	critical_exit();
1696169320Spiso
1697169320Spiso	/* Interrupt storm logic */
1698169320Spiso	if (thread & FILTER_STRAY) {
1699169320Spiso		ie->ie_count++;
1700169320Spiso		if (ie->ie_count < intr_storm_threshold)
1701169320Spiso			printf("Interrupt stray detection not present\n");
1702169320Spiso	}
1703169320Spiso
1704169320Spiso	/* Schedule an ithread if needed. */
1705169320Spiso	if (thread & FILTER_SCHEDULE_THREAD) {
1706169320Spiso		if (intr_event_schedule_thread(ie, ithd) != 0)
1707169320Spiso			panic("%s: impossible stray interrupt", __func__);
1708169320Spiso	}
1709169320Spiso	td->td_intr_nesting_level--;
1710169320Spiso	return (0);
1711169320Spiso}
1712169320Spiso#endif
1713169320Spiso
1714121482Sjhb#ifdef DDB
171572237Sjhb/*
1716121482Sjhb * Dump details about an interrupt handler
1717121482Sjhb */
1718121482Sjhbstatic void
1719151658Sjhbdb_dump_intrhand(struct intr_handler *ih)
1720121482Sjhb{
1721121482Sjhb	int comma;
1722121482Sjhb
1723121482Sjhb	db_printf("\t%-10s ", ih->ih_name);
1724121482Sjhb	switch (ih->ih_pri) {
1725121482Sjhb	case PI_REALTIME:
1726121482Sjhb		db_printf("CLK ");
1727121482Sjhb		break;
1728121482Sjhb	case PI_AV:
1729121482Sjhb		db_printf("AV  ");
1730121482Sjhb		break;
1731217292Sjhb	case PI_TTY:
1732121482Sjhb		db_printf("TTY ");
1733121482Sjhb		break;
1734121482Sjhb	case PI_NET:
1735121482Sjhb		db_printf("NET ");
1736121482Sjhb		break;
1737121482Sjhb	case PI_DISK:
1738121482Sjhb		db_printf("DISK");
1739121482Sjhb		break;
1740121482Sjhb	case PI_DULL:
1741121482Sjhb		db_printf("DULL");
1742121482Sjhb		break;
1743121482Sjhb	default:
1744121482Sjhb		if (ih->ih_pri >= PI_SOFT)
1745121482Sjhb			db_printf("SWI ");
1746121482Sjhb		else
1747121482Sjhb			db_printf("%4u", ih->ih_pri);
1748121482Sjhb		break;
1749121482Sjhb	}
1750121482Sjhb	db_printf(" ");
1751121482Sjhb	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1752121482Sjhb	db_printf("(%p)", ih->ih_argument);
1753121482Sjhb	if (ih->ih_need ||
1754166901Spiso	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1755121482Sjhb	    IH_MPSAFE)) != 0) {
1756121482Sjhb		db_printf(" {");
1757121482Sjhb		comma = 0;
1758121482Sjhb		if (ih->ih_flags & IH_EXCLUSIVE) {
1759121482Sjhb			if (comma)
1760121482Sjhb				db_printf(", ");
1761121482Sjhb			db_printf("EXCL");
1762121482Sjhb			comma = 1;
1763121482Sjhb		}
1764121482Sjhb		if (ih->ih_flags & IH_ENTROPY) {
1765121482Sjhb			if (comma)
1766121482Sjhb				db_printf(", ");
1767121482Sjhb			db_printf("ENTROPY");
1768121482Sjhb			comma = 1;
1769121482Sjhb		}
1770121482Sjhb		if (ih->ih_flags & IH_DEAD) {
1771121482Sjhb			if (comma)
1772121482Sjhb				db_printf(", ");
1773121482Sjhb			db_printf("DEAD");
1774121482Sjhb			comma = 1;
1775121482Sjhb		}
1776121482Sjhb		if (ih->ih_flags & IH_MPSAFE) {
1777121482Sjhb			if (comma)
1778121482Sjhb				db_printf(", ");
1779121482Sjhb			db_printf("MPSAFE");
1780121482Sjhb			comma = 1;
1781121482Sjhb		}
1782121482Sjhb		if (ih->ih_need) {
1783121482Sjhb			if (comma)
1784121482Sjhb				db_printf(", ");
1785121482Sjhb			db_printf("NEED");
1786121482Sjhb		}
1787121482Sjhb		db_printf("}");
1788121482Sjhb	}
1789121482Sjhb	db_printf("\n");
1790121482Sjhb}
1791121482Sjhb
1792121482Sjhb/*
1793151658Sjhb * Dump details about a event.
1794121482Sjhb */
1795121482Sjhbvoid
1796151658Sjhbdb_dump_intr_event(struct intr_event *ie, int handlers)
1797121482Sjhb{
1798151658Sjhb	struct intr_handler *ih;
1799151658Sjhb	struct intr_thread *it;
1800121482Sjhb	int comma;
1801121482Sjhb
1802151658Sjhb	db_printf("%s ", ie->ie_fullname);
1803151658Sjhb	it = ie->ie_thread;
1804151658Sjhb	if (it != NULL)
1805151658Sjhb		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1806151658Sjhb	else
1807151658Sjhb		db_printf("(no thread)");
1808151658Sjhb	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1809151658Sjhb	    (it != NULL && it->it_need)) {
1810121482Sjhb		db_printf(" {");
1811121482Sjhb		comma = 0;
1812151658Sjhb		if (ie->ie_flags & IE_SOFT) {
1813121482Sjhb			db_printf("SOFT");
1814121482Sjhb			comma = 1;
1815121482Sjhb		}
1816151658Sjhb		if (ie->ie_flags & IE_ENTROPY) {
1817121482Sjhb			if (comma)
1818121482Sjhb				db_printf(", ");
1819121482Sjhb			db_printf("ENTROPY");
1820121482Sjhb			comma = 1;
1821121482Sjhb		}
1822151658Sjhb		if (ie->ie_flags & IE_ADDING_THREAD) {
1823121482Sjhb			if (comma)
1824121482Sjhb				db_printf(", ");
1825151658Sjhb			db_printf("ADDING_THREAD");
1826121482Sjhb			comma = 1;
1827121482Sjhb		}
1828151658Sjhb		if (it != NULL && it->it_need) {
1829121482Sjhb			if (comma)
1830121482Sjhb				db_printf(", ");
1831121482Sjhb			db_printf("NEED");
1832121482Sjhb		}
1833121482Sjhb		db_printf("}");
1834121482Sjhb	}
1835121482Sjhb	db_printf("\n");
1836121482Sjhb
1837121482Sjhb	if (handlers)
1838151658Sjhb		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1839121482Sjhb		    db_dump_intrhand(ih);
1840121482Sjhb}
1841151658Sjhb
1842151658Sjhb/*
1843151658Sjhb * Dump data about interrupt handlers
1844151658Sjhb */
1845151658SjhbDB_SHOW_COMMAND(intr, db_show_intr)
1846151658Sjhb{
1847151658Sjhb	struct intr_event *ie;
1848160312Sjhb	int all, verbose;
1849151658Sjhb
1850229272Sed	verbose = strchr(modif, 'v') != NULL;
1851229272Sed	all = strchr(modif, 'a') != NULL;
1852151658Sjhb	TAILQ_FOREACH(ie, &event_list, ie_list) {
1853151658Sjhb		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1854151658Sjhb			continue;
1855151658Sjhb		db_dump_intr_event(ie, verbose);
1856160312Sjhb		if (db_pager_quit)
1857160312Sjhb			break;
1858151658Sjhb	}
1859151658Sjhb}
1860121482Sjhb#endif /* DDB */
1861121482Sjhb
1862121482Sjhb/*
186367551Sjhb * Start standard software interrupt threads
186466698Sjhb */
186567551Sjhbstatic void
186672237Sjhbstart_softintr(void *dummy)
186767551Sjhb{
186872237Sjhb
1869177859Sjeff	if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1870177859Sjeff		panic("died while creating vm swi ithread");
187166698Sjhb}
1872177253SrwatsonSYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1873177253Srwatson    NULL);
187466698Sjhb
1875151658Sjhb/*
187677582Stmm * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
187777582Stmm * The data for this machine dependent, and the declarations are in machine
187877582Stmm * dependent code.  The layout of intrnames and intrcnt however is machine
187977582Stmm * independent.
188077582Stmm *
188177582Stmm * We do not know the length of intrcnt and intrnames at compile time, so
188277582Stmm * calculate things at run time.
188377582Stmm */
188477582Stmmstatic int
188577582Stmmsysctl_intrnames(SYSCTL_HANDLER_ARGS)
188677582Stmm{
1887224187Sattilio	return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
188877582Stmm}
188977582Stmm
189077582StmmSYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
189177582Stmm    NULL, 0, sysctl_intrnames, "", "Interrupt Names");
189277582Stmm
189377582Stmmstatic int
189477582Stmmsysctl_intrcnt(SYSCTL_HANDLER_ARGS)
189577582Stmm{
1896232751Sjmallett#ifdef SCTL_MASK32
1897232751Sjmallett	uint32_t *intrcnt32;
1898232751Sjmallett	unsigned i;
1899232751Sjmallett	int error;
1900232751Sjmallett
1901232751Sjmallett	if (req->flags & SCTL_MASK32) {
1902232751Sjmallett		if (!req->oldptr)
1903232751Sjmallett			return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1904232751Sjmallett		intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1905232751Sjmallett		if (intrcnt32 == NULL)
1906232751Sjmallett			return (ENOMEM);
1907232751Sjmallett		for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1908232751Sjmallett			intrcnt32[i] = intrcnt[i];
1909232751Sjmallett		error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1910232751Sjmallett		free(intrcnt32, M_TEMP);
1911232751Sjmallett		return (error);
1912232751Sjmallett	}
1913232751Sjmallett#endif
1914224187Sattilio	return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
191577582Stmm}
191677582Stmm
191777582StmmSYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
191877582Stmm    NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1919121482Sjhb
1920121482Sjhb#ifdef DDB
1921121482Sjhb/*
1922121482Sjhb * DDB command to dump the interrupt statistics.
1923121482Sjhb */
1924121482SjhbDB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1925121482Sjhb{
1926121482Sjhb	u_long *i;
1927121482Sjhb	char *cp;
1928224187Sattilio	u_int j;
1929121482Sjhb
1930121482Sjhb	cp = intrnames;
1931224187Sattilio	j = 0;
1932224187Sattilio	for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1933224187Sattilio	    i++, j++) {
1934121482Sjhb		if (*cp == '\0')
1935121482Sjhb			break;
1936121482Sjhb		if (*i != 0)
1937121482Sjhb			db_printf("%s\t%lu\n", cp, *i);
1938121482Sjhb		cp += strlen(cp) + 1;
1939121482Sjhb	}
1940121482Sjhb}
1941121482Sjhb#endif
1942