kern_intr.c revision 183298
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 183298 2008-09-23 14:25:56Z obrien $");
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>
4666698Sjhb#include <sys/proc.h>
4772759Sjhb#include <sys/random.h>
4872237Sjhb#include <sys/resourcevar.h>
49139451Sjhb#include <sys/sched.h>
50177181Sjhb#include <sys/smp.h>
5177582Stmm#include <sys/sysctl.h>
52182024Skmacy#include <sys/syslog.h>
5366698Sjhb#include <sys/unistd.h>
5466698Sjhb#include <sys/vmmeter.h>
5566698Sjhb#include <machine/atomic.h>
5666698Sjhb#include <machine/cpu.h>
5767551Sjhb#include <machine/md_var.h>
5872237Sjhb#include <machine/stdarg.h>
59121482Sjhb#ifdef DDB
60121482Sjhb#include <ddb/ddb.h>
61121482Sjhb#include <ddb/db_sym.h>
62121482Sjhb#endif
6326156Sse
64151658Sjhb/*
65151658Sjhb * Describe an interrupt thread.  There is one of these per interrupt event.
66151658Sjhb */
67151658Sjhbstruct intr_thread {
68151658Sjhb	struct intr_event *it_event;
69151658Sjhb	struct thread *it_thread;	/* Kernel thread. */
70151658Sjhb	int	it_flags;		/* (j) IT_* flags. */
71151658Sjhb	int	it_need;		/* Needs service. */
7272759Sjhb};
7372759Sjhb
74151658Sjhb/* Interrupt thread flags kept in it_flags */
75151658Sjhb#define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
76151658Sjhb
77151658Sjhbstruct	intr_entropy {
78151658Sjhb	struct	thread *td;
79151658Sjhb	uintptr_t event;
80151658Sjhb};
81151658Sjhb
82151658Sjhbstruct	intr_event *clk_intr_event;
83151658Sjhbstruct	intr_event *tty_intr_event;
84128339Sbdevoid	*vm_ih;
85173004Sjulianstruct proc *intrproc;
8638244Sbde
8772237Sjhbstatic MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
8872237Sjhb
89168850Snjlstatic int intr_storm_threshold = 1000;
90128331SjhbTUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
91128331SjhbSYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
92128331Sjhb    &intr_storm_threshold, 0,
93128339Sbde    "Number of consecutive interrupts before storm protection is enabled");
94151658Sjhbstatic TAILQ_HEAD(, intr_event) event_list =
95151658Sjhb    TAILQ_HEAD_INITIALIZER(event_list);
96178092Sjeffstatic struct mtx event_lock;
97178092SjeffMTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
98128331Sjhb
99151658Sjhbstatic void	intr_event_update(struct intr_event *ie);
100169320Spiso#ifdef INTR_FILTER
101177940Sjhbstatic int	intr_event_schedule_thread(struct intr_event *ie,
102177940Sjhb		    struct intr_thread *ithd);
103177940Sjhbstatic int	intr_filter_loop(struct intr_event *ie,
104177940Sjhb		    struct trapframe *frame, struct intr_thread **ithd);
105169320Spisostatic struct intr_thread *ithread_create(const char *name,
106169320Spiso			      struct intr_handler *ih);
107169320Spiso#else
108177940Sjhbstatic int	intr_event_schedule_thread(struct intr_event *ie);
109151658Sjhbstatic struct intr_thread *ithread_create(const char *name);
110169320Spiso#endif
111151658Sjhbstatic void	ithread_destroy(struct intr_thread *ithread);
112169320Spisostatic void	ithread_execute_handlers(struct proc *p,
113169320Spiso		    struct intr_event *ie);
114169320Spiso#ifdef INTR_FILTER
115169320Spisostatic void	priv_ithread_execute_handler(struct proc *p,
116169320Spiso		    struct intr_handler *ih);
117169320Spiso#endif
118128339Sbdestatic void	ithread_loop(void *);
119151658Sjhbstatic void	ithread_update(struct intr_thread *ithd);
120128339Sbdestatic void	start_softintr(void *);
121128339Sbde
122165124Sjhb/* Map an interrupt type to an ithread priority. */
12372237Sjhbu_char
124151658Sjhbintr_priority(enum intr_type flags)
12565822Sjhb{
12672237Sjhb	u_char pri;
12765822Sjhb
12872237Sjhb	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
12978365Speter	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
13065822Sjhb	switch (flags) {
13172237Sjhb	case INTR_TYPE_TTY:
13265822Sjhb		pri = PI_TTYLOW;
13365822Sjhb		break;
13465822Sjhb	case INTR_TYPE_BIO:
13565822Sjhb		/*
13665822Sjhb		 * XXX We need to refine this.  BSD/OS distinguishes
13765822Sjhb		 * between tape and disk priorities.
13865822Sjhb		 */
13965822Sjhb		pri = PI_DISK;
14065822Sjhb		break;
14165822Sjhb	case INTR_TYPE_NET:
14265822Sjhb		pri = PI_NET;
14365822Sjhb		break;
14465822Sjhb	case INTR_TYPE_CAM:
14565822Sjhb		pri = PI_DISK;          /* XXX or PI_CAM? */
14665822Sjhb		break;
14778365Speter	case INTR_TYPE_AV:		/* Audio/video */
14878365Speter		pri = PI_AV;
14978365Speter		break;
15072237Sjhb	case INTR_TYPE_CLK:
15172237Sjhb		pri = PI_REALTIME;
15272237Sjhb		break;
15365822Sjhb	case INTR_TYPE_MISC:
15465822Sjhb		pri = PI_DULL;          /* don't care */
15565822Sjhb		break;
15665822Sjhb	default:
15772237Sjhb		/* We didn't specify an interrupt level. */
158151658Sjhb		panic("intr_priority: no interrupt type in flags");
15965822Sjhb	}
16065822Sjhb
16165822Sjhb	return pri;
16265822Sjhb}
16365822Sjhb
16472237Sjhb/*
165151658Sjhb * Update an ithread based on the associated intr_event.
16672237Sjhb */
16772237Sjhbstatic void
168151658Sjhbithread_update(struct intr_thread *ithd)
16972237Sjhb{
170151658Sjhb	struct intr_event *ie;
17183366Sjulian	struct thread *td;
172151658Sjhb	u_char pri;
17367551Sjhb
174151658Sjhb	ie = ithd->it_event;
175151658Sjhb	td = ithd->it_thread;
17672237Sjhb
177151658Sjhb	/* Determine the overall priority of this event. */
178151658Sjhb	if (TAILQ_EMPTY(&ie->ie_handlers))
179151658Sjhb		pri = PRI_MAX_ITHD;
180151658Sjhb	else
181151658Sjhb		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
182105354Srobert
183151658Sjhb	/* Update name and priority. */
184173004Sjulian	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
185170307Sjeff	thread_lock(td);
186151658Sjhb	sched_prio(td, pri);
187170307Sjeff	thread_unlock(td);
188151658Sjhb}
189151658Sjhb
190151658Sjhb/*
191151658Sjhb * Regenerate the full name of an interrupt event and update its priority.
192151658Sjhb */
193151658Sjhbstatic void
194151658Sjhbintr_event_update(struct intr_event *ie)
195151658Sjhb{
196151658Sjhb	struct intr_handler *ih;
197151658Sjhb	char *last;
198151658Sjhb	int missed, space;
199151658Sjhb
200151658Sjhb	/* Start off with no entropy and just the name of the event. */
201151658Sjhb	mtx_assert(&ie->ie_lock, MA_OWNED);
202151658Sjhb	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
203151658Sjhb	ie->ie_flags &= ~IE_ENTROPY;
204137267Sjhb	missed = 0;
205151658Sjhb	space = 1;
206151658Sjhb
207151658Sjhb	/* Run through all the handlers updating values. */
208151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
209151658Sjhb		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
210151658Sjhb		    sizeof(ie->ie_fullname)) {
211151658Sjhb			strcat(ie->ie_fullname, " ");
212151658Sjhb			strcat(ie->ie_fullname, ih->ih_name);
213151658Sjhb			space = 0;
214137267Sjhb		} else
215137267Sjhb			missed++;
216137267Sjhb		if (ih->ih_flags & IH_ENTROPY)
217151658Sjhb			ie->ie_flags |= IE_ENTROPY;
218137267Sjhb	}
219151658Sjhb
220151658Sjhb	/*
221151658Sjhb	 * If the handler names were too long, add +'s to indicate missing
222151658Sjhb	 * names. If we run out of room and still have +'s to add, change
223151658Sjhb	 * the last character from a + to a *.
224151658Sjhb	 */
225151658Sjhb	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
226137267Sjhb	while (missed-- > 0) {
227151658Sjhb		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
228151658Sjhb			if (*last == '+') {
229151658Sjhb				*last = '*';
230151658Sjhb				break;
231151658Sjhb			} else
232151658Sjhb				*last = '+';
233151658Sjhb		} else if (space) {
234151658Sjhb			strcat(ie->ie_fullname, " +");
235151658Sjhb			space = 0;
23672237Sjhb		} else
237151658Sjhb			strcat(ie->ie_fullname, "+");
23872237Sjhb	}
239151658Sjhb
240151658Sjhb	/*
241151658Sjhb	 * If this event has an ithread, update it's priority and
242151658Sjhb	 * name.
243151658Sjhb	 */
244151658Sjhb	if (ie->ie_thread != NULL)
245151658Sjhb		ithread_update(ie->ie_thread);
246151658Sjhb	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
24772237Sjhb}
24872237Sjhb
24972237Sjhbint
250183298Sobrienintr_event_create(struct intr_event **event, void *source, int flags, int irq,
251177940Sjhb    void (*pre_ithread)(void *), void (*post_ithread)(void *),
252177940Sjhb    void (*post_filter)(void *), int (*assign_cpu)(void *, u_char),
253177940Sjhb    const char *fmt, ...)
254169320Spiso{
255169320Spiso	struct intr_event *ie;
256169320Spiso	va_list ap;
25772237Sjhb
258169320Spiso	/* The only valid flag during creation is IE_SOFT. */
259169320Spiso	if ((flags & ~IE_SOFT) != 0)
260169320Spiso		return (EINVAL);
261169320Spiso	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
262169320Spiso	ie->ie_source = source;
263177940Sjhb	ie->ie_pre_ithread = pre_ithread;
264177940Sjhb	ie->ie_post_ithread = post_ithread;
265177940Sjhb	ie->ie_post_filter = post_filter;
266177181Sjhb	ie->ie_assign_cpu = assign_cpu;
267169320Spiso	ie->ie_flags = flags;
268178092Sjeff	ie->ie_irq = irq;
269177181Sjhb	ie->ie_cpu = NOCPU;
270169320Spiso	TAILQ_INIT(&ie->ie_handlers);
271169320Spiso	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
272169320Spiso
273169320Spiso	va_start(ap, fmt);
274169320Spiso	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
275169320Spiso	va_end(ap);
276169320Spiso	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
277178092Sjeff	mtx_lock(&event_lock);
278169320Spiso	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
279178092Sjeff	mtx_unlock(&event_lock);
280169320Spiso	if (event != NULL)
281169320Spiso		*event = ie;
282169320Spiso	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
283169320Spiso	return (0);
284169320Spiso}
285169320Spiso
286177181Sjhb/*
287177181Sjhb * Bind an interrupt event to the specified CPU.  Note that not all
288177181Sjhb * platforms support binding an interrupt to a CPU.  For those
289177181Sjhb * platforms this request will fail.  For supported platforms, any
290177181Sjhb * associated ithreads as well as the primary interrupt context will
291177181Sjhb * be bound to the specificed CPU.  Using a cpu id of NOCPU unbinds
292177181Sjhb * the interrupt event.
293177181Sjhb */
294151658Sjhbint
295177181Sjhbintr_event_bind(struct intr_event *ie, u_char cpu)
296177181Sjhb{
297178092Sjeff	cpuset_t mask;
298178092Sjeff	lwpid_t id;
299177181Sjhb	int error;
300177181Sjhb
301177181Sjhb	/* Need a CPU to bind to. */
302177181Sjhb	if (cpu != NOCPU && CPU_ABSENT(cpu))
303177181Sjhb		return (EINVAL);
304177181Sjhb
305177181Sjhb	if (ie->ie_assign_cpu == NULL)
306177181Sjhb		return (EOPNOTSUPP);
307178092Sjeff	/*
308178092Sjeff	 * If we have any ithreads try to set their mask first since this
309178092Sjeff	 * can fail.
310178092Sjeff	 */
311177181Sjhb	mtx_lock(&ie->ie_lock);
312178092Sjeff	if (ie->ie_thread != NULL) {
313178092Sjeff		CPU_ZERO(&mask);
314178092Sjeff		if (cpu == NOCPU)
315178092Sjeff			CPU_COPY(cpuset_root, &mask);
316178092Sjeff		else
317178092Sjeff			CPU_SET(cpu, &mask);
318178092Sjeff		id = ie->ie_thread->it_thread->td_tid;
319177181Sjhb		mtx_unlock(&ie->ie_lock);
320178092Sjeff		error = cpuset_setthread(id, &mask);
321178092Sjeff		if (error)
322178092Sjeff			return (error);
323178092Sjeff	} else
324178092Sjeff		mtx_unlock(&ie->ie_lock);
325177181Sjhb	error = ie->ie_assign_cpu(ie->ie_source, cpu);
326177181Sjhb	if (error)
327177181Sjhb		return (error);
328177181Sjhb	mtx_lock(&ie->ie_lock);
329177181Sjhb	ie->ie_cpu = cpu;
330177181Sjhb	mtx_unlock(&ie->ie_lock);
331178092Sjeff
332178092Sjeff	return (error);
333178092Sjeff}
334178092Sjeff
335178092Sjeffstatic struct intr_event *
336178092Sjeffintr_lookup(int irq)
337178092Sjeff{
338178092Sjeff	struct intr_event *ie;
339178092Sjeff
340178092Sjeff	mtx_lock(&event_lock);
341178092Sjeff	TAILQ_FOREACH(ie, &event_list, ie_list)
342178092Sjeff		if (ie->ie_irq == irq &&
343178092Sjeff		    (ie->ie_flags & IE_SOFT) == 0 &&
344178092Sjeff		    TAILQ_FIRST(&ie->ie_handlers) != NULL)
345178092Sjeff			break;
346178092Sjeff	mtx_unlock(&event_lock);
347178092Sjeff	return (ie);
348178092Sjeff}
349178092Sjeff
350178092Sjeffint
351178092Sjeffintr_setaffinity(int irq, void *m)
352178092Sjeff{
353178092Sjeff	struct intr_event *ie;
354178092Sjeff	cpuset_t *mask;
355178092Sjeff	u_char cpu;
356178092Sjeff	int n;
357178092Sjeff
358178092Sjeff	mask = m;
359178092Sjeff	cpu = NOCPU;
360178092Sjeff	/*
361178092Sjeff	 * If we're setting all cpus we can unbind.  Otherwise make sure
362178092Sjeff	 * only one cpu is in the set.
363178092Sjeff	 */
364178092Sjeff	if (CPU_CMP(cpuset_root, mask)) {
365178092Sjeff		for (n = 0; n < CPU_SETSIZE; n++) {
366178092Sjeff			if (!CPU_ISSET(n, mask))
367178092Sjeff				continue;
368178092Sjeff			if (cpu != NOCPU)
369178092Sjeff				return (EINVAL);
370178092Sjeff			cpu = (u_char)n;
371178092Sjeff		}
372178092Sjeff	}
373178092Sjeff	ie = intr_lookup(irq);
374178092Sjeff	if (ie == NULL)
375178092Sjeff		return (ESRCH);
376178092Sjeff	intr_event_bind(ie, cpu);
377180099Sbz	return (0);
378178092Sjeff}
379178092Sjeff
380178092Sjeffint
381178092Sjeffintr_getaffinity(int irq, void *m)
382178092Sjeff{
383178092Sjeff	struct intr_event *ie;
384178092Sjeff	cpuset_t *mask;
385178092Sjeff
386178092Sjeff	mask = m;
387178092Sjeff	ie = intr_lookup(irq);
388178092Sjeff	if (ie == NULL)
389178092Sjeff		return (ESRCH);
390178092Sjeff	CPU_ZERO(mask);
391178092Sjeff	mtx_lock(&ie->ie_lock);
392178092Sjeff	if (ie->ie_cpu == NOCPU)
393178092Sjeff		CPU_COPY(cpuset_root, mask);
394178092Sjeff	else
395178092Sjeff		CPU_SET(ie->ie_cpu, mask);
396178092Sjeff	mtx_unlock(&ie->ie_lock);
397177181Sjhb	return (0);
398177181Sjhb}
399177181Sjhb
400177181Sjhbint
401151658Sjhbintr_event_destroy(struct intr_event *ie)
402151658Sjhb{
403151658Sjhb
404178092Sjeff	mtx_lock(&event_lock);
405151658Sjhb	mtx_lock(&ie->ie_lock);
406151658Sjhb	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
407151658Sjhb		mtx_unlock(&ie->ie_lock);
408178092Sjeff		mtx_unlock(&event_lock);
409151658Sjhb		return (EBUSY);
410151658Sjhb	}
411151658Sjhb	TAILQ_REMOVE(&event_list, ie, ie_list);
412157728Sjhb#ifndef notyet
413157728Sjhb	if (ie->ie_thread != NULL) {
414157728Sjhb		ithread_destroy(ie->ie_thread);
415157728Sjhb		ie->ie_thread = NULL;
416157728Sjhb	}
417157728Sjhb#endif
418151658Sjhb	mtx_unlock(&ie->ie_lock);
419178092Sjeff	mtx_unlock(&event_lock);
420151658Sjhb	mtx_destroy(&ie->ie_lock);
421151658Sjhb	free(ie, M_ITHREAD);
422151658Sjhb	return (0);
423151658Sjhb}
424151658Sjhb
425169320Spiso#ifndef INTR_FILTER
426151658Sjhbstatic struct intr_thread *
427151658Sjhbithread_create(const char *name)
428151658Sjhb{
429151658Sjhb	struct intr_thread *ithd;
430151658Sjhb	struct thread *td;
431151658Sjhb	int error;
432151658Sjhb
433151658Sjhb	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
434151658Sjhb
435173004Sjulian	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
436173004Sjulian		    &td, RFSTOPPED | RFHIGHPID,
437173051Sjulian	    	    0, "intr", "%s", name);
438151658Sjhb	if (error)
439172836Sjulian		panic("kproc_create() failed with %d", error);
440170307Sjeff	thread_lock(td);
441164936Sjulian	sched_class(td, PRI_ITHD);
442103216Sjulian	TD_SET_IWAIT(td);
443170307Sjeff	thread_unlock(td);
444151658Sjhb	td->td_pflags |= TDP_ITHREAD;
445151658Sjhb	ithd->it_thread = td;
446151658Sjhb	CTR2(KTR_INTR, "%s: created %s", __func__, name);
447151658Sjhb	return (ithd);
44872237Sjhb}
449169320Spiso#else
450169320Spisostatic struct intr_thread *
451169320Spisoithread_create(const char *name, struct intr_handler *ih)
452169320Spiso{
453169320Spiso	struct intr_thread *ithd;
454169320Spiso	struct thread *td;
455169320Spiso	int error;
45672237Sjhb
457169320Spiso	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
458169320Spiso
459173153Sjulian	error = kproc_kthread_add(ithread_loop, ih, &intrproc,
460173004Sjulian		    &td, RFSTOPPED | RFHIGHPID,
461173051Sjulian	    	    0, "intr", "%s", name);
462169320Spiso	if (error)
463172836Sjulian		panic("kproc_create() failed with %d", error);
464170307Sjeff	thread_lock(td);
465169320Spiso	sched_class(td, PRI_ITHD);
466169320Spiso	TD_SET_IWAIT(td);
467170307Sjeff	thread_unlock(td);
468169320Spiso	td->td_pflags |= TDP_ITHREAD;
469169320Spiso	ithd->it_thread = td;
470169320Spiso	CTR2(KTR_INTR, "%s: created %s", __func__, name);
471169320Spiso	return (ithd);
472169320Spiso}
473169320Spiso#endif
474169320Spiso
475151658Sjhbstatic void
476151658Sjhbithread_destroy(struct intr_thread *ithread)
47772237Sjhb{
47883366Sjulian	struct thread *td;
47972237Sjhb
480157784Sscottl	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
481151658Sjhb	td = ithread->it_thread;
482170307Sjeff	thread_lock(td);
48376771Sjhb	ithread->it_flags |= IT_DEAD;
484103216Sjulian	if (TD_AWAITING_INTR(td)) {
485103216Sjulian		TD_CLR_IWAIT(td);
486166188Sjeff		sched_add(td, SRQ_INTR);
48772237Sjhb	}
488170307Sjeff	thread_unlock(td);
48972237Sjhb}
49072237Sjhb
491169320Spiso#ifndef INTR_FILTER
49272237Sjhbint
493151658Sjhbintr_event_add_handler(struct intr_event *ie, const char *name,
494166901Spiso    driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
495166901Spiso    enum intr_type flags, void **cookiep)
49672237Sjhb{
497151658Sjhb	struct intr_handler *ih, *temp_ih;
498151658Sjhb	struct intr_thread *it;
49972237Sjhb
500166901Spiso	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
50172237Sjhb		return (EINVAL);
50272237Sjhb
503151658Sjhb	/* Allocate and populate an interrupt handler structure. */
504151658Sjhb	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
505166901Spiso	ih->ih_filter = filter;
50672237Sjhb	ih->ih_handler = handler;
50772237Sjhb	ih->ih_argument = arg;
50872237Sjhb	ih->ih_name = name;
509151658Sjhb	ih->ih_event = ie;
51072237Sjhb	ih->ih_pri = pri;
511166901Spiso	if (flags & INTR_EXCL)
51272237Sjhb		ih->ih_flags = IH_EXCLUSIVE;
51372237Sjhb	if (flags & INTR_MPSAFE)
51472237Sjhb		ih->ih_flags |= IH_MPSAFE;
51572237Sjhb	if (flags & INTR_ENTROPY)
51672237Sjhb		ih->ih_flags |= IH_ENTROPY;
51772237Sjhb
518151658Sjhb	/* We can only have one exclusive handler in a event. */
519151658Sjhb	mtx_lock(&ie->ie_lock);
520151658Sjhb	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
521151658Sjhb		if ((flags & INTR_EXCL) ||
522151658Sjhb		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
523151658Sjhb			mtx_unlock(&ie->ie_lock);
524151658Sjhb			free(ih, M_ITHREAD);
525151658Sjhb			return (EINVAL);
526151658Sjhb		}
527122002Sjhb	}
52872237Sjhb
529151658Sjhb	/* Add the new handler to the event in priority order. */
530151658Sjhb	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
531151658Sjhb		if (temp_ih->ih_pri > ih->ih_pri)
532151658Sjhb			break;
533151658Sjhb	}
53472237Sjhb	if (temp_ih == NULL)
535151658Sjhb		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
53672237Sjhb	else
53772237Sjhb		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
538151658Sjhb	intr_event_update(ie);
53972237Sjhb
540151658Sjhb	/* Create a thread if we need one. */
541166901Spiso	while (ie->ie_thread == NULL && handler != NULL) {
542151658Sjhb		if (ie->ie_flags & IE_ADDING_THREAD)
543157815Sjhb			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
544151658Sjhb		else {
545151658Sjhb			ie->ie_flags |= IE_ADDING_THREAD;
546151658Sjhb			mtx_unlock(&ie->ie_lock);
547151658Sjhb			it = ithread_create("intr: newborn");
548151658Sjhb			mtx_lock(&ie->ie_lock);
549151658Sjhb			ie->ie_flags &= ~IE_ADDING_THREAD;
550151658Sjhb			ie->ie_thread = it;
551151658Sjhb			it->it_event = ie;
552151658Sjhb			ithread_update(it);
553151658Sjhb			wakeup(ie);
554151658Sjhb		}
555151658Sjhb	}
556151658Sjhb	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
557151658Sjhb	    ie->ie_name);
558151658Sjhb	mtx_unlock(&ie->ie_lock);
559151658Sjhb
56072237Sjhb	if (cookiep != NULL)
56172237Sjhb		*cookiep = ih;
56272237Sjhb	return (0);
56372237Sjhb}
564169320Spiso#else
565169320Spisoint
566169320Spisointr_event_add_handler(struct intr_event *ie, const char *name,
567169320Spiso    driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
568169320Spiso    enum intr_type flags, void **cookiep)
569169320Spiso{
570169320Spiso	struct intr_handler *ih, *temp_ih;
571169320Spiso	struct intr_thread *it;
57272237Sjhb
573169320Spiso	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
574169320Spiso		return (EINVAL);
575169320Spiso
576169320Spiso	/* Allocate and populate an interrupt handler structure. */
577169320Spiso	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
578169320Spiso	ih->ih_filter = filter;
579169320Spiso	ih->ih_handler = handler;
580169320Spiso	ih->ih_argument = arg;
581169320Spiso	ih->ih_name = name;
582169320Spiso	ih->ih_event = ie;
583169320Spiso	ih->ih_pri = pri;
584169320Spiso	if (flags & INTR_EXCL)
585169320Spiso		ih->ih_flags = IH_EXCLUSIVE;
586169320Spiso	if (flags & INTR_MPSAFE)
587169320Spiso		ih->ih_flags |= IH_MPSAFE;
588169320Spiso	if (flags & INTR_ENTROPY)
589169320Spiso		ih->ih_flags |= IH_ENTROPY;
590169320Spiso
591169320Spiso	/* We can only have one exclusive handler in a event. */
592169320Spiso	mtx_lock(&ie->ie_lock);
593169320Spiso	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
594169320Spiso		if ((flags & INTR_EXCL) ||
595169320Spiso		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
596169320Spiso			mtx_unlock(&ie->ie_lock);
597169320Spiso			free(ih, M_ITHREAD);
598169320Spiso			return (EINVAL);
599169320Spiso		}
600169320Spiso	}
601169320Spiso
602169320Spiso	/* Add the new handler to the event in priority order. */
603169320Spiso	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
604169320Spiso		if (temp_ih->ih_pri > ih->ih_pri)
605169320Spiso			break;
606169320Spiso	}
607169320Spiso	if (temp_ih == NULL)
608169320Spiso		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
609169320Spiso	else
610169320Spiso		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
611169320Spiso	intr_event_update(ie);
612169320Spiso
613169320Spiso	/* For filtered handlers, create a private ithread to run on. */
614169320Spiso	if (filter != NULL && handler != NULL) {
615169320Spiso		mtx_unlock(&ie->ie_lock);
616169320Spiso		it = ithread_create("intr: newborn", ih);
617169320Spiso		mtx_lock(&ie->ie_lock);
618169320Spiso		it->it_event = ie;
619169320Spiso		ih->ih_thread = it;
620169320Spiso		ithread_update(it); // XXX - do we really need this?!?!?
621169320Spiso	} else { /* Create the global per-event thread if we need one. */
622169320Spiso		while (ie->ie_thread == NULL && handler != NULL) {
623169320Spiso			if (ie->ie_flags & IE_ADDING_THREAD)
624169320Spiso				msleep(ie, &ie->ie_lock, 0, "ithread", 0);
625169320Spiso			else {
626169320Spiso				ie->ie_flags |= IE_ADDING_THREAD;
627169320Spiso				mtx_unlock(&ie->ie_lock);
628169320Spiso				it = ithread_create("intr: newborn", ih);
629169320Spiso				mtx_lock(&ie->ie_lock);
630169320Spiso				ie->ie_flags &= ~IE_ADDING_THREAD;
631169320Spiso				ie->ie_thread = it;
632169320Spiso				it->it_event = ie;
633169320Spiso				ithread_update(it);
634169320Spiso				wakeup(ie);
635169320Spiso			}
636169320Spiso		}
637169320Spiso	}
638169320Spiso	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
639169320Spiso	    ie->ie_name);
640169320Spiso	mtx_unlock(&ie->ie_lock);
641169320Spiso
642169320Spiso	if (cookiep != NULL)
643169320Spiso		*cookiep = ih;
644169320Spiso	return (0);
645169320Spiso}
646169320Spiso#endif
647169320Spiso
648165125Sjhb/*
649165125Sjhb * Return the ie_source field from the intr_event an intr_handler is
650165125Sjhb * associated with.
651165125Sjhb */
652165125Sjhbvoid *
653165125Sjhbintr_handler_source(void *cookie)
654165125Sjhb{
655165125Sjhb	struct intr_handler *ih;
656165125Sjhb	struct intr_event *ie;
657165125Sjhb
658165125Sjhb	ih = (struct intr_handler *)cookie;
659165125Sjhb	if (ih == NULL)
660165125Sjhb		return (NULL);
661165125Sjhb	ie = ih->ih_event;
662165125Sjhb	KASSERT(ie != NULL,
663165125Sjhb	    ("interrupt handler \"%s\" has a NULL interrupt event",
664165125Sjhb	    ih->ih_name));
665165125Sjhb	return (ie->ie_source);
666165125Sjhb}
667165125Sjhb
668169320Spiso#ifndef INTR_FILTER
66972237Sjhbint
670151658Sjhbintr_event_remove_handler(void *cookie)
67172237Sjhb{
672151658Sjhb	struct intr_handler *handler = (struct intr_handler *)cookie;
673151658Sjhb	struct intr_event *ie;
67472237Sjhb#ifdef INVARIANTS
675151658Sjhb	struct intr_handler *ih;
67672237Sjhb#endif
677151658Sjhb#ifdef notyet
678151658Sjhb	int dead;
679151658Sjhb#endif
68072237Sjhb
68172759Sjhb	if (handler == NULL)
68272237Sjhb		return (EINVAL);
683151658Sjhb	ie = handler->ih_event;
684151658Sjhb	KASSERT(ie != NULL,
685151658Sjhb	    ("interrupt handler \"%s\" has a NULL interrupt event",
686165124Sjhb	    handler->ih_name));
687151658Sjhb	mtx_lock(&ie->ie_lock);
68887593Sobrien	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
689151658Sjhb	    ie->ie_name);
69072237Sjhb#ifdef INVARIANTS
691151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
69272759Sjhb		if (ih == handler)
69372759Sjhb			goto ok;
694151658Sjhb	mtx_unlock(&ie->ie_lock);
695151658Sjhb	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
696151658Sjhb	    ih->ih_name, ie->ie_name);
69772759Sjhbok:
69872237Sjhb#endif
69972839Sjhb	/*
700151658Sjhb	 * If there is no ithread, then just remove the handler and return.
701151658Sjhb	 * XXX: Note that an INTR_FAST handler might be running on another
702151658Sjhb	 * CPU!
703151658Sjhb	 */
704151658Sjhb	if (ie->ie_thread == NULL) {
705151658Sjhb		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
706151658Sjhb		mtx_unlock(&ie->ie_lock);
707151658Sjhb		free(handler, M_ITHREAD);
708151658Sjhb		return (0);
709151658Sjhb	}
710151658Sjhb
711151658Sjhb	/*
71272839Sjhb	 * If the interrupt thread is already running, then just mark this
71372839Sjhb	 * handler as being dead and let the ithread do the actual removal.
714124505Struckman	 *
715124505Struckman	 * During a cold boot while cold is set, msleep() does not sleep,
716124505Struckman	 * so we have to remove the handler here rather than letting the
717124505Struckman	 * thread do it.
71872839Sjhb	 */
719170307Sjeff	thread_lock(ie->ie_thread->it_thread);
720151658Sjhb	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
72172839Sjhb		handler->ih_flags |= IH_DEAD;
72272839Sjhb
72372839Sjhb		/*
72472839Sjhb		 * Ensure that the thread will process the handler list
72572839Sjhb		 * again and remove this handler if it has already passed
72672839Sjhb		 * it on the list.
72772839Sjhb		 */
728151658Sjhb		ie->ie_thread->it_need = 1;
729151658Sjhb	} else
730151658Sjhb		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
731170307Sjeff	thread_unlock(ie->ie_thread->it_thread);
732151658Sjhb	while (handler->ih_flags & IH_DEAD)
733157815Sjhb		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
734151658Sjhb	intr_event_update(ie);
735151658Sjhb#ifdef notyet
736151658Sjhb	/*
737151658Sjhb	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
738151658Sjhb	 * this could lead to races of stale data when servicing an
739151658Sjhb	 * interrupt.
740151658Sjhb	 */
741151658Sjhb	dead = 1;
742151658Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
743151658Sjhb		if (!(ih->ih_flags & IH_FAST)) {
744151658Sjhb			dead = 0;
745151658Sjhb			break;
746151658Sjhb		}
747151658Sjhb	}
748151658Sjhb	if (dead) {
749151658Sjhb		ithread_destroy(ie->ie_thread);
750151658Sjhb		ie->ie_thread = NULL;
751151658Sjhb	}
752151658Sjhb#endif
753151658Sjhb	mtx_unlock(&ie->ie_lock);
75476771Sjhb	free(handler, M_ITHREAD);
75572237Sjhb	return (0);
75672237Sjhb}
75772237Sjhb
758177940Sjhbstatic int
759151658Sjhbintr_event_schedule_thread(struct intr_event *ie)
76072759Sjhb{
761151658Sjhb	struct intr_entropy entropy;
762151658Sjhb	struct intr_thread *it;
76383366Sjulian	struct thread *td;
764101176Sjulian	struct thread *ctd;
76572759Sjhb	struct proc *p;
76672759Sjhb
76772759Sjhb	/*
76872759Sjhb	 * If no ithread or no handlers, then we have a stray interrupt.
76972759Sjhb	 */
770151658Sjhb	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
771151658Sjhb	    ie->ie_thread == NULL)
77272759Sjhb		return (EINVAL);
77372759Sjhb
774101176Sjulian	ctd = curthread;
775151658Sjhb	it = ie->ie_thread;
776151658Sjhb	td = it->it_thread;
777133191Srwatson	p = td->td_proc;
778151658Sjhb
77972759Sjhb	/*
78072759Sjhb	 * If any of the handlers for this ithread claim to be good
78172759Sjhb	 * sources of entropy, then gather some.
78272759Sjhb	 */
783151658Sjhb	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
784133191Srwatson		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
785173004Sjulian		    p->p_pid, td->td_name);
786151658Sjhb		entropy.event = (uintptr_t)ie;
787151658Sjhb		entropy.td = ctd;
78872759Sjhb		random_harvest(&entropy, sizeof(entropy), 2, 0,
78972759Sjhb		    RANDOM_INTERRUPT);
79072759Sjhb	}
79172759Sjhb
792151658Sjhb	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
79372759Sjhb
79472759Sjhb	/*
79572759Sjhb	 * Set it_need to tell the thread to keep running if it is already
796170307Sjeff	 * running.  Then, lock the thread and see if we actually need to
797170307Sjeff	 * put it on the runqueue.
79872759Sjhb	 */
799151658Sjhb	it->it_need = 1;
800170307Sjeff	thread_lock(td);
801103216Sjulian	if (TD_AWAITING_INTR(td)) {
802151658Sjhb		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
803173004Sjulian		    td->td_name);
804103216Sjulian		TD_CLR_IWAIT(td);
805166188Sjeff		sched_add(td, SRQ_INTR);
80672759Sjhb	} else {
807151658Sjhb		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
808173004Sjulian		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
80972759Sjhb	}
810170307Sjeff	thread_unlock(td);
81172759Sjhb
81272759Sjhb	return (0);
81372759Sjhb}
814169320Spiso#else
815169320Spisoint
816169320Spisointr_event_remove_handler(void *cookie)
817169320Spiso{
818169320Spiso	struct intr_handler *handler = (struct intr_handler *)cookie;
819169320Spiso	struct intr_event *ie;
820169320Spiso	struct intr_thread *it;
821169320Spiso#ifdef INVARIANTS
822169320Spiso	struct intr_handler *ih;
823169320Spiso#endif
824169320Spiso#ifdef notyet
825169320Spiso	int dead;
826169320Spiso#endif
82772759Sjhb
828169320Spiso	if (handler == NULL)
829169320Spiso		return (EINVAL);
830169320Spiso	ie = handler->ih_event;
831169320Spiso	KASSERT(ie != NULL,
832169320Spiso	    ("interrupt handler \"%s\" has a NULL interrupt event",
833169320Spiso	    handler->ih_name));
834169320Spiso	mtx_lock(&ie->ie_lock);
835169320Spiso	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
836169320Spiso	    ie->ie_name);
837169320Spiso#ifdef INVARIANTS
838169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
839169320Spiso		if (ih == handler)
840169320Spiso			goto ok;
841169320Spiso	mtx_unlock(&ie->ie_lock);
842169320Spiso	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
843169320Spiso	    ih->ih_name, ie->ie_name);
844169320Spisook:
845169320Spiso#endif
846169320Spiso	/*
847169320Spiso	 * If there are no ithreads (per event and per handler), then
848169320Spiso	 * just remove the handler and return.
849169320Spiso	 * XXX: Note that an INTR_FAST handler might be running on another CPU!
850169320Spiso	 */
851169320Spiso	if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
852169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
853169320Spiso		mtx_unlock(&ie->ie_lock);
854169320Spiso		free(handler, M_ITHREAD);
855169320Spiso		return (0);
856169320Spiso	}
857169320Spiso
858169320Spiso	/* Private or global ithread? */
859169320Spiso	it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
860169320Spiso	/*
861169320Spiso	 * If the interrupt thread is already running, then just mark this
862169320Spiso	 * handler as being dead and let the ithread do the actual removal.
863169320Spiso	 *
864169320Spiso	 * During a cold boot while cold is set, msleep() does not sleep,
865169320Spiso	 * so we have to remove the handler here rather than letting the
866169320Spiso	 * thread do it.
867169320Spiso	 */
868170307Sjeff	thread_lock(it->it_thread);
869169320Spiso	if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
870169320Spiso		handler->ih_flags |= IH_DEAD;
871169320Spiso
872169320Spiso		/*
873169320Spiso		 * Ensure that the thread will process the handler list
874169320Spiso		 * again and remove this handler if it has already passed
875169320Spiso		 * it on the list.
876169320Spiso		 */
877169320Spiso		it->it_need = 1;
878169320Spiso	} else
879169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
880170307Sjeff	thread_unlock(it->it_thread);
881169320Spiso	while (handler->ih_flags & IH_DEAD)
882169320Spiso		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
883169320Spiso	/*
884169320Spiso	 * At this point, the handler has been disconnected from the event,
885169320Spiso	 * so we can kill the private ithread if any.
886169320Spiso	 */
887169320Spiso	if (handler->ih_thread) {
888169320Spiso		ithread_destroy(handler->ih_thread);
889169320Spiso		handler->ih_thread = NULL;
890169320Spiso	}
891169320Spiso	intr_event_update(ie);
892169320Spiso#ifdef notyet
893169320Spiso	/*
894169320Spiso	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
895169320Spiso	 * this could lead to races of stale data when servicing an
896169320Spiso	 * interrupt.
897169320Spiso	 */
898169320Spiso	dead = 1;
899169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
900169320Spiso		if (handler != NULL) {
901169320Spiso			dead = 0;
902169320Spiso			break;
903169320Spiso		}
904169320Spiso	}
905169320Spiso	if (dead) {
906169320Spiso		ithread_destroy(ie->ie_thread);
907169320Spiso		ie->ie_thread = NULL;
908169320Spiso	}
909169320Spiso#endif
910169320Spiso	mtx_unlock(&ie->ie_lock);
911169320Spiso	free(handler, M_ITHREAD);
912169320Spiso	return (0);
913169320Spiso}
914169320Spiso
915177940Sjhbstatic int
916169320Spisointr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
917169320Spiso{
918169320Spiso	struct intr_entropy entropy;
919169320Spiso	struct thread *td;
920169320Spiso	struct thread *ctd;
921169320Spiso	struct proc *p;
922169320Spiso
923169320Spiso	/*
924169320Spiso	 * If no ithread or no handlers, then we have a stray interrupt.
925169320Spiso	 */
926169320Spiso	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
927169320Spiso		return (EINVAL);
928169320Spiso
929169320Spiso	ctd = curthread;
930169320Spiso	td = it->it_thread;
931169320Spiso	p = td->td_proc;
932169320Spiso
933169320Spiso	/*
934169320Spiso	 * If any of the handlers for this ithread claim to be good
935169320Spiso	 * sources of entropy, then gather some.
936169320Spiso	 */
937169320Spiso	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
938169320Spiso		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
939173004Sjulian		    p->p_pid, td->td_name);
940169320Spiso		entropy.event = (uintptr_t)ie;
941169320Spiso		entropy.td = ctd;
942169320Spiso		random_harvest(&entropy, sizeof(entropy), 2, 0,
943169320Spiso		    RANDOM_INTERRUPT);
944169320Spiso	}
945169320Spiso
946169320Spiso	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
947169320Spiso
948169320Spiso	/*
949169320Spiso	 * Set it_need to tell the thread to keep running if it is already
950170307Sjeff	 * running.  Then, lock the thread and see if we actually need to
951170307Sjeff	 * put it on the runqueue.
952169320Spiso	 */
953169320Spiso	it->it_need = 1;
954170307Sjeff	thread_lock(td);
955169320Spiso	if (TD_AWAITING_INTR(td)) {
956169320Spiso		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
957173122Sjulian		    td->td_name);
958169320Spiso		TD_CLR_IWAIT(td);
959169320Spiso		sched_add(td, SRQ_INTR);
960169320Spiso	} else {
961169320Spiso		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
962173004Sjulian		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
963169320Spiso	}
964170307Sjeff	thread_unlock(td);
965169320Spiso
966169320Spiso	return (0);
967169320Spiso}
968169320Spiso#endif
969169320Spiso
970151699Sjhb/*
971151699Sjhb * Add a software interrupt handler to a specified event.  If a given event
972151699Sjhb * is not specified, then a new event is created.
973151699Sjhb */
97472759Sjhbint
975151658Sjhbswi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
97672237Sjhb	    void *arg, int pri, enum intr_type flags, void **cookiep)
97772237Sjhb{
978151658Sjhb	struct intr_event *ie;
97972237Sjhb	int error;
98066698Sjhb
981169320Spiso	if (flags & INTR_ENTROPY)
98272759Sjhb		return (EINVAL);
98372759Sjhb
984151658Sjhb	ie = (eventp != NULL) ? *eventp : NULL;
98566698Sjhb
986151658Sjhb	if (ie != NULL) {
987151658Sjhb		if (!(ie->ie_flags & IE_SOFT))
988151658Sjhb			return (EINVAL);
98972759Sjhb	} else {
990178092Sjeff		error = intr_event_create(&ie, NULL, IE_SOFT, 0,
991177181Sjhb		    NULL, NULL, NULL, NULL, "swi%d:", pri);
99267551Sjhb		if (error)
99372237Sjhb			return (error);
994151658Sjhb		if (eventp != NULL)
995151658Sjhb			*eventp = ie;
99666698Sjhb	}
997177859Sjeff	error = intr_event_add_handler(ie, name, NULL, handler, arg,
998177859Sjeff	    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep);
999177859Sjeff	if (error)
1000177859Sjeff		return (error);
1001177859Sjeff	if (pri == SWI_CLOCK) {
1002177859Sjeff		struct proc *p;
1003177859Sjeff		p = ie->ie_thread->it_thread->td_proc;
1004177859Sjeff		PROC_LOCK(p);
1005177859Sjeff		p->p_flag |= P_NOLOAD;
1006177859Sjeff		PROC_UNLOCK(p);
1007177859Sjeff	}
1008177859Sjeff	return (0);
100966698Sjhb}
101066698Sjhb
101166698Sjhb/*
1012151658Sjhb * Schedule a software interrupt thread.
101366698Sjhb */
101467551Sjhbvoid
101572237Sjhbswi_sched(void *cookie, int flags)
101666698Sjhb{
1017151658Sjhb	struct intr_handler *ih = (struct intr_handler *)cookie;
1018151658Sjhb	struct intr_event *ie = ih->ih_event;
101972759Sjhb	int error;
102066698Sjhb
1021151658Sjhb	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1022151658Sjhb	    ih->ih_need);
1023151658Sjhb
102467551Sjhb	/*
102572759Sjhb	 * Set ih_need for this handler so that if the ithread is already
102672759Sjhb	 * running it will execute this handler on the next pass.  Otherwise,
102772759Sjhb	 * it will execute it the next time it runs.
102867551Sjhb	 */
102972237Sjhb	atomic_store_rel_int(&ih->ih_need, 1);
1030163474Sbde
103172237Sjhb	if (!(flags & SWI_DELAY)) {
1032170291Sattilio		PCPU_INC(cnt.v_soft);
1033169320Spiso#ifdef INTR_FILTER
1034169320Spiso		error = intr_event_schedule_thread(ie, ie->ie_thread);
1035169320Spiso#else
1036151658Sjhb		error = intr_event_schedule_thread(ie);
1037169320Spiso#endif
103872759Sjhb		KASSERT(error == 0, ("stray software interrupt"));
103966698Sjhb	}
104066698Sjhb}
104166698Sjhb
1042151699Sjhb/*
1043151699Sjhb * Remove a software interrupt handler.  Currently this code does not
1044151699Sjhb * remove the associated interrupt event if it becomes empty.  Calling code
1045151699Sjhb * may do so manually via intr_event_destroy(), but that's not really
1046151699Sjhb * an optimal interface.
1047151699Sjhb */
1048151699Sjhbint
1049151699Sjhbswi_remove(void *cookie)
1050151699Sjhb{
1051151699Sjhb
1052151699Sjhb	return (intr_event_remove_handler(cookie));
1053151699Sjhb}
1054151699Sjhb
1055169320Spiso#ifdef INTR_FILTER
1056151658Sjhbstatic void
1057169320Spisopriv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1058169320Spiso{
1059169320Spiso	struct intr_event *ie;
1060169320Spiso
1061169320Spiso	ie = ih->ih_event;
1062169320Spiso	/*
1063169320Spiso	 * If this handler is marked for death, remove it from
1064169320Spiso	 * the list of handlers and wake up the sleeper.
1065169320Spiso	 */
1066169320Spiso	if (ih->ih_flags & IH_DEAD) {
1067169320Spiso		mtx_lock(&ie->ie_lock);
1068169320Spiso		TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1069169320Spiso		ih->ih_flags &= ~IH_DEAD;
1070169320Spiso		wakeup(ih);
1071169320Spiso		mtx_unlock(&ie->ie_lock);
1072169320Spiso		return;
1073169320Spiso	}
1074169320Spiso
1075169320Spiso	/* Execute this handler. */
1076169320Spiso	CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1077169320Spiso	     __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1078169320Spiso	     ih->ih_name, ih->ih_flags);
1079169320Spiso
1080169320Spiso	if (!(ih->ih_flags & IH_MPSAFE))
1081169320Spiso		mtx_lock(&Giant);
1082169320Spiso	ih->ih_handler(ih->ih_argument);
1083169320Spiso	if (!(ih->ih_flags & IH_MPSAFE))
1084169320Spiso		mtx_unlock(&Giant);
1085169320Spiso}
1086169320Spiso#endif
1087169320Spiso
1088183052Sjhb/*
1089183052Sjhb * This is a public function for use by drivers that mux interrupt
1090183052Sjhb * handlers for child devices from their interrupt handler.
1091183052Sjhb */
1092183052Sjhbvoid
1093183052Sjhbintr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1094151658Sjhb{
1095151658Sjhb	struct intr_handler *ih, *ihn;
1096151658Sjhb
1097151658Sjhb	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1098151658Sjhb		/*
1099151658Sjhb		 * If this handler is marked for death, remove it from
1100151658Sjhb		 * the list of handlers and wake up the sleeper.
1101151658Sjhb		 */
1102151658Sjhb		if (ih->ih_flags & IH_DEAD) {
1103151658Sjhb			mtx_lock(&ie->ie_lock);
1104151658Sjhb			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1105151658Sjhb			ih->ih_flags &= ~IH_DEAD;
1106151658Sjhb			wakeup(ih);
1107151658Sjhb			mtx_unlock(&ie->ie_lock);
1108151658Sjhb			continue;
1109151658Sjhb		}
1110151658Sjhb
1111167080Spiso		/* Skip filter only handlers */
1112167080Spiso		if (ih->ih_handler == NULL)
1113167080Spiso			continue;
1114167080Spiso
1115151658Sjhb		/*
1116151658Sjhb		 * For software interrupt threads, we only execute
1117151658Sjhb		 * handlers that have their need flag set.  Hardware
1118151658Sjhb		 * interrupt threads always invoke all of their handlers.
1119151658Sjhb		 */
1120151658Sjhb		if (ie->ie_flags & IE_SOFT) {
1121151658Sjhb			if (!ih->ih_need)
1122151658Sjhb				continue;
1123151658Sjhb			else
1124151658Sjhb				atomic_store_rel_int(&ih->ih_need, 0);
1125151658Sjhb		}
1126151658Sjhb
1127151658Sjhb		/* Execute this handler. */
1128151658Sjhb		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1129169320Spiso		    __func__, p->p_pid, (void *)ih->ih_handler,
1130169320Spiso		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1131151658Sjhb
1132151658Sjhb		if (!(ih->ih_flags & IH_MPSAFE))
1133151658Sjhb			mtx_lock(&Giant);
1134151658Sjhb		ih->ih_handler(ih->ih_argument);
1135151658Sjhb		if (!(ih->ih_flags & IH_MPSAFE))
1136151658Sjhb			mtx_unlock(&Giant);
1137151658Sjhb	}
1138183052Sjhb}
1139183052Sjhb
1140183052Sjhbstatic void
1141183052Sjhbithread_execute_handlers(struct proc *p, struct intr_event *ie)
1142183052Sjhb{
1143183052Sjhb
1144183052Sjhb	/* Interrupt handlers should not sleep. */
1145151658Sjhb	if (!(ie->ie_flags & IE_SOFT))
1146183052Sjhb		THREAD_NO_SLEEPING();
1147183052Sjhb	intr_event_execute_handlers(p, ie);
1148183052Sjhb	if (!(ie->ie_flags & IE_SOFT))
1149151658Sjhb		THREAD_SLEEPING_OK();
1150151658Sjhb
1151151658Sjhb	/*
1152151658Sjhb	 * Interrupt storm handling:
1153151658Sjhb	 *
1154151658Sjhb	 * If this interrupt source is currently storming, then throttle
1155151658Sjhb	 * it to only fire the handler once  per clock tick.
1156151658Sjhb	 *
1157151658Sjhb	 * If this interrupt source is not currently storming, but the
1158151658Sjhb	 * number of back to back interrupts exceeds the storm threshold,
1159151658Sjhb	 * then enter storming mode.
1160151658Sjhb	 */
1161167173Sjhb	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1162167173Sjhb	    !(ie->ie_flags & IE_SOFT)) {
1163168850Snjl		/* Report the message only once every second. */
1164168850Snjl		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1165151658Sjhb			printf(
1166168850Snjl	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1167151658Sjhb			    ie->ie_name);
1168151658Sjhb		}
1169167173Sjhb		pause("istorm", 1);
1170151658Sjhb	} else
1171151658Sjhb		ie->ie_count++;
1172151658Sjhb
1173151658Sjhb	/*
1174151658Sjhb	 * Now that all the handlers have had a chance to run, reenable
1175151658Sjhb	 * the interrupt source.
1176151658Sjhb	 */
1177177940Sjhb	if (ie->ie_post_ithread != NULL)
1178177940Sjhb		ie->ie_post_ithread(ie->ie_source);
1179151658Sjhb}
1180151658Sjhb
1181169320Spiso#ifndef INTR_FILTER
118266698Sjhb/*
118372237Sjhb * This is the main code for interrupt threads.
118466698Sjhb */
1185104094Sphkstatic void
118672237Sjhbithread_loop(void *arg)
118766698Sjhb{
1188151658Sjhb	struct intr_thread *ithd;
1189151658Sjhb	struct intr_event *ie;
119083366Sjulian	struct thread *td;
119172237Sjhb	struct proc *p;
1192151658Sjhb
119383366Sjulian	td = curthread;
119483366Sjulian	p = td->td_proc;
1195151658Sjhb	ithd = (struct intr_thread *)arg;
1196151658Sjhb	KASSERT(ithd->it_thread == td,
119787593Sobrien	    ("%s: ithread and proc linkage out of sync", __func__));
1198151658Sjhb	ie = ithd->it_event;
1199151658Sjhb	ie->ie_count = 0;
120066698Sjhb
120167551Sjhb	/*
120267551Sjhb	 * As long as we have interrupts outstanding, go through the
120367551Sjhb	 * list of handlers, giving each one a go at it.
120467551Sjhb	 */
120566698Sjhb	for (;;) {
120672237Sjhb		/*
120772237Sjhb		 * If we are an orphaned thread, then just die.
120872237Sjhb		 */
120972237Sjhb		if (ithd->it_flags & IT_DEAD) {
1210151658Sjhb			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1211173004Sjulian			    p->p_pid, td->td_name);
121272237Sjhb			free(ithd, M_ITHREAD);
1213173044Sjulian			kthread_exit();
121472237Sjhb		}
121572237Sjhb
1216151658Sjhb		/*
1217151658Sjhb		 * Service interrupts.  If another interrupt arrives while
1218151658Sjhb		 * we are running, it will set it_need to note that we
1219151658Sjhb		 * should make another pass.
1220151658Sjhb		 */
122172237Sjhb		while (ithd->it_need) {
122267551Sjhb			/*
1223151658Sjhb			 * This might need a full read and write barrier
1224151658Sjhb			 * to make sure that this write posts before any
1225151658Sjhb			 * of the memory or device accesses in the
1226151658Sjhb			 * handlers.
122767551Sjhb			 */
122872237Sjhb			atomic_store_rel_int(&ithd->it_need, 0);
1229151658Sjhb			ithread_execute_handlers(p, ie);
123066698Sjhb		}
1231128331Sjhb		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1232128331Sjhb		mtx_assert(&Giant, MA_NOTOWNED);
123367551Sjhb
123466698Sjhb		/*
123566698Sjhb		 * Processed all our interrupts.  Now get the sched
123667551Sjhb		 * lock.  This may take a while and it_need may get
123766698Sjhb		 * set again, so we have to check it again.
123866698Sjhb		 */
1239170307Sjeff		thread_lock(td);
1240151658Sjhb		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1241128331Sjhb			TD_SET_IWAIT(td);
1242151658Sjhb			ie->ie_count = 0;
1243178272Sjeff			mi_switch(SW_VOL | SWT_IWAIT, NULL);
124466698Sjhb		}
1245170307Sjeff		thread_unlock(td);
124666698Sjhb	}
124766698Sjhb}
1248177940Sjhb
1249177940Sjhb/*
1250177940Sjhb * Main interrupt handling body.
1251177940Sjhb *
1252177940Sjhb * Input:
1253177940Sjhb * o ie:                        the event connected to this interrupt.
1254177940Sjhb * o frame:                     some archs (i.e. i386) pass a frame to some.
1255177940Sjhb *                              handlers as their main argument.
1256177940Sjhb * Return value:
1257177940Sjhb * o 0:                         everything ok.
1258177940Sjhb * o EINVAL:                    stray interrupt.
1259177940Sjhb */
1260177940Sjhbint
1261177940Sjhbintr_event_handle(struct intr_event *ie, struct trapframe *frame)
1262177940Sjhb{
1263177940Sjhb	struct intr_handler *ih;
1264177940Sjhb	struct thread *td;
1265177940Sjhb	int error, ret, thread;
1266177940Sjhb
1267177940Sjhb	td = curthread;
1268177940Sjhb
1269177940Sjhb	/* An interrupt with no event or handlers is a stray interrupt. */
1270177940Sjhb	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1271177940Sjhb		return (EINVAL);
1272177940Sjhb
1273177940Sjhb	/*
1274177940Sjhb	 * Execute fast interrupt handlers directly.
1275177940Sjhb	 * To support clock handlers, if a handler registers
1276177940Sjhb	 * with a NULL argument, then we pass it a pointer to
1277177940Sjhb	 * a trapframe as its argument.
1278177940Sjhb	 */
1279177940Sjhb	td->td_intr_nesting_level++;
1280177940Sjhb	thread = 0;
1281177940Sjhb	ret = 0;
1282177940Sjhb	critical_enter();
1283177940Sjhb	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1284177940Sjhb		if (ih->ih_filter == NULL) {
1285177940Sjhb			thread = 1;
1286177940Sjhb			continue;
1287177940Sjhb		}
1288177940Sjhb		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1289177940Sjhb		    ih->ih_filter, ih->ih_argument == NULL ? frame :
1290177940Sjhb		    ih->ih_argument, ih->ih_name);
1291177940Sjhb		if (ih->ih_argument == NULL)
1292177940Sjhb			ret = ih->ih_filter(frame);
1293177940Sjhb		else
1294177940Sjhb			ret = ih->ih_filter(ih->ih_argument);
1295177940Sjhb		/*
1296177940Sjhb		 * Wrapper handler special handling:
1297177940Sjhb		 *
1298177940Sjhb		 * in some particular cases (like pccard and pccbb),
1299177940Sjhb		 * the _real_ device handler is wrapped in a couple of
1300177940Sjhb		 * functions - a filter wrapper and an ithread wrapper.
1301177940Sjhb		 * In this case (and just in this case), the filter wrapper
1302177940Sjhb		 * could ask the system to schedule the ithread and mask
1303177940Sjhb		 * the interrupt source if the wrapped handler is composed
1304177940Sjhb		 * of just an ithread handler.
1305177940Sjhb		 *
1306177940Sjhb		 * TODO: write a generic wrapper to avoid people rolling
1307177940Sjhb		 * their own
1308177940Sjhb		 */
1309177940Sjhb		if (!thread) {
1310177940Sjhb			if (ret == FILTER_SCHEDULE_THREAD)
1311177940Sjhb				thread = 1;
1312177940Sjhb		}
1313177940Sjhb	}
1314177940Sjhb
1315177940Sjhb	if (thread) {
1316177940Sjhb		if (ie->ie_pre_ithread != NULL)
1317177940Sjhb			ie->ie_pre_ithread(ie->ie_source);
1318177940Sjhb	} else {
1319177940Sjhb		if (ie->ie_post_filter != NULL)
1320177940Sjhb			ie->ie_post_filter(ie->ie_source);
1321177940Sjhb	}
1322177940Sjhb
1323177940Sjhb	/* Schedule the ithread if needed. */
1324177940Sjhb	if (thread) {
1325177940Sjhb		error = intr_event_schedule_thread(ie);
1326182024Skmacy#ifndef XEN
1327177940Sjhb		KASSERT(error == 0, ("bad stray interrupt"));
1328182024Skmacy#else
1329182024Skmacy		if (error != 0)
1330182024Skmacy			log(LOG_WARNING, "bad stray interrupt");
1331182024Skmacy#endif
1332177940Sjhb	}
1333177940Sjhb	critical_exit();
1334177940Sjhb	td->td_intr_nesting_level--;
1335177940Sjhb	return (0);
1336177940Sjhb}
1337169320Spiso#else
1338169320Spiso/*
1339169320Spiso * This is the main code for interrupt threads.
1340169320Spiso */
1341169320Spisostatic void
1342169320Spisoithread_loop(void *arg)
1343169320Spiso{
1344169320Spiso	struct intr_thread *ithd;
1345169320Spiso	struct intr_handler *ih;
1346169320Spiso	struct intr_event *ie;
1347169320Spiso	struct thread *td;
1348169320Spiso	struct proc *p;
1349169320Spiso	int priv;
135066698Sjhb
1351169320Spiso	td = curthread;
1352169320Spiso	p = td->td_proc;
1353169320Spiso	ih = (struct intr_handler *)arg;
1354169320Spiso	priv = (ih->ih_thread != NULL) ? 1 : 0;
1355169320Spiso	ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1356169320Spiso	KASSERT(ithd->it_thread == td,
1357169320Spiso	    ("%s: ithread and proc linkage out of sync", __func__));
1358169320Spiso	ie = ithd->it_event;
1359169320Spiso	ie->ie_count = 0;
1360169320Spiso
1361169320Spiso	/*
1362169320Spiso	 * As long as we have interrupts outstanding, go through the
1363169320Spiso	 * list of handlers, giving each one a go at it.
1364169320Spiso	 */
1365169320Spiso	for (;;) {
1366169320Spiso		/*
1367169320Spiso		 * If we are an orphaned thread, then just die.
1368169320Spiso		 */
1369169320Spiso		if (ithd->it_flags & IT_DEAD) {
1370169320Spiso			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1371173004Sjulian			    p->p_pid, td->td_name);
1372169320Spiso			free(ithd, M_ITHREAD);
1373173044Sjulian			kthread_exit();
1374169320Spiso		}
1375169320Spiso
1376169320Spiso		/*
1377169320Spiso		 * Service interrupts.  If another interrupt arrives while
1378169320Spiso		 * we are running, it will set it_need to note that we
1379169320Spiso		 * should make another pass.
1380169320Spiso		 */
1381169320Spiso		while (ithd->it_need) {
1382169320Spiso			/*
1383169320Spiso			 * This might need a full read and write barrier
1384169320Spiso			 * to make sure that this write posts before any
1385169320Spiso			 * of the memory or device accesses in the
1386169320Spiso			 * handlers.
1387169320Spiso			 */
1388169320Spiso			atomic_store_rel_int(&ithd->it_need, 0);
1389169320Spiso			if (priv)
1390169320Spiso				priv_ithread_execute_handler(p, ih);
1391169320Spiso			else
1392169320Spiso				ithread_execute_handlers(p, ie);
1393169320Spiso		}
1394169320Spiso		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1395169320Spiso		mtx_assert(&Giant, MA_NOTOWNED);
1396169320Spiso
1397169320Spiso		/*
1398169320Spiso		 * Processed all our interrupts.  Now get the sched
1399169320Spiso		 * lock.  This may take a while and it_need may get
1400169320Spiso		 * set again, so we have to check it again.
1401169320Spiso		 */
1402170307Sjeff		thread_lock(td);
1403169320Spiso		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1404169320Spiso			TD_SET_IWAIT(td);
1405169320Spiso			ie->ie_count = 0;
1406178272Sjeff			mi_switch(SW_VOL | SWT_IWAIT, NULL);
1407169320Spiso		}
1408170307Sjeff		thread_unlock(td);
1409169320Spiso	}
1410169320Spiso}
1411169320Spiso
1412169320Spiso/*
1413169320Spiso * Main loop for interrupt filter.
1414169320Spiso *
1415169320Spiso * Some architectures (i386, amd64 and arm) require the optional frame
1416169320Spiso * parameter, and use it as the main argument for fast handler execution
1417169320Spiso * when ih_argument == NULL.
1418169320Spiso *
1419169320Spiso * Return value:
1420169320Spiso * o FILTER_STRAY:              No filter recognized the event, and no
1421169320Spiso *                              filter-less handler is registered on this
1422169320Spiso *                              line.
1423169320Spiso * o FILTER_HANDLED:            A filter claimed the event and served it.
1424169320Spiso * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1425169320Spiso *                              least one filter-less handler on this line.
1426169320Spiso * o FILTER_HANDLED |
1427169320Spiso *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1428169320Spiso *                              scheduling the per-handler ithread.
1429169320Spiso *
1430169320Spiso * In case an ithread has to be scheduled, in *ithd there will be a
1431169320Spiso * pointer to a struct intr_thread containing the thread to be
1432169320Spiso * scheduled.
1433169320Spiso */
1434169320Spiso
1435177940Sjhbstatic int
1436169320Spisointr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1437169320Spiso		 struct intr_thread **ithd)
1438169320Spiso{
1439169320Spiso	struct intr_handler *ih;
1440169320Spiso	void *arg;
1441169320Spiso	int ret, thread_only;
1442169320Spiso
1443169320Spiso	ret = 0;
1444169320Spiso	thread_only = 0;
1445169320Spiso	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1446169320Spiso		/*
1447169320Spiso		 * Execute fast interrupt handlers directly.
1448169320Spiso		 * To support clock handlers, if a handler registers
1449169320Spiso		 * with a NULL argument, then we pass it a pointer to
1450169320Spiso		 * a trapframe as its argument.
1451169320Spiso		 */
1452169320Spiso		arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1453169320Spiso
1454169320Spiso		CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1455169320Spiso		     ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1456169320Spiso
1457169320Spiso		if (ih->ih_filter != NULL)
1458169320Spiso			ret = ih->ih_filter(arg);
1459169320Spiso		else {
1460169320Spiso			thread_only = 1;
1461169320Spiso			continue;
1462169320Spiso		}
1463169320Spiso
1464169320Spiso		if (ret & FILTER_STRAY)
1465169320Spiso			continue;
1466169320Spiso		else {
1467169320Spiso			*ithd = ih->ih_thread;
1468169320Spiso			return (ret);
1469169320Spiso		}
1470169320Spiso	}
1471169320Spiso
1472169320Spiso	/*
1473169320Spiso	 * No filters handled the interrupt and we have at least
1474169320Spiso	 * one handler without a filter.  In this case, we schedule
1475169320Spiso	 * all of the filter-less handlers to run in the ithread.
1476169320Spiso	 */
1477169320Spiso	if (thread_only) {
1478169320Spiso		*ithd = ie->ie_thread;
1479169320Spiso		return (FILTER_SCHEDULE_THREAD);
1480169320Spiso	}
1481169320Spiso	return (FILTER_STRAY);
1482169320Spiso}
1483169320Spiso
1484169320Spiso/*
1485169320Spiso * Main interrupt handling body.
1486169320Spiso *
1487169320Spiso * Input:
1488169320Spiso * o ie:                        the event connected to this interrupt.
1489169320Spiso * o frame:                     some archs (i.e. i386) pass a frame to some.
1490169320Spiso *                              handlers as their main argument.
1491169320Spiso * Return value:
1492169320Spiso * o 0:                         everything ok.
1493169320Spiso * o EINVAL:                    stray interrupt.
1494169320Spiso */
1495169320Spisoint
1496169320Spisointr_event_handle(struct intr_event *ie, struct trapframe *frame)
1497169320Spiso{
1498169320Spiso	struct intr_thread *ithd;
1499169320Spiso	struct thread *td;
1500169320Spiso	int thread;
1501169320Spiso
1502169320Spiso	ithd = NULL;
1503169320Spiso	td = curthread;
1504169320Spiso
1505169320Spiso	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1506169320Spiso		return (EINVAL);
1507169320Spiso
1508169320Spiso	td->td_intr_nesting_level++;
1509169320Spiso	thread = 0;
1510169320Spiso	critical_enter();
1511177940Sjhb	thread = intr_filter_loop(ie, frame, &ithd);
1512169320Spiso	if (thread & FILTER_HANDLED) {
1513177940Sjhb		if (ie->ie_post_filter != NULL)
1514177940Sjhb			ie->ie_post_filter(ie->ie_source);
1515169320Spiso	} else {
1516177940Sjhb		if (ie->ie_pre_ithread != NULL)
1517177940Sjhb			ie->ie_pre_ithread(ie->ie_source);
1518169320Spiso	}
1519169320Spiso	critical_exit();
1520169320Spiso
1521169320Spiso	/* Interrupt storm logic */
1522169320Spiso	if (thread & FILTER_STRAY) {
1523169320Spiso		ie->ie_count++;
1524169320Spiso		if (ie->ie_count < intr_storm_threshold)
1525169320Spiso			printf("Interrupt stray detection not present\n");
1526169320Spiso	}
1527169320Spiso
1528169320Spiso	/* Schedule an ithread if needed. */
1529169320Spiso	if (thread & FILTER_SCHEDULE_THREAD) {
1530169320Spiso		if (intr_event_schedule_thread(ie, ithd) != 0)
1531169320Spiso			panic("%s: impossible stray interrupt", __func__);
1532169320Spiso	}
1533169320Spiso	td->td_intr_nesting_level--;
1534169320Spiso	return (0);
1535169320Spiso}
1536169320Spiso#endif
1537169320Spiso
1538121482Sjhb#ifdef DDB
153972237Sjhb/*
1540121482Sjhb * Dump details about an interrupt handler
1541121482Sjhb */
1542121482Sjhbstatic void
1543151658Sjhbdb_dump_intrhand(struct intr_handler *ih)
1544121482Sjhb{
1545121482Sjhb	int comma;
1546121482Sjhb
1547121482Sjhb	db_printf("\t%-10s ", ih->ih_name);
1548121482Sjhb	switch (ih->ih_pri) {
1549121482Sjhb	case PI_REALTIME:
1550121482Sjhb		db_printf("CLK ");
1551121482Sjhb		break;
1552121482Sjhb	case PI_AV:
1553121482Sjhb		db_printf("AV  ");
1554121482Sjhb		break;
1555121482Sjhb	case PI_TTYHIGH:
1556121482Sjhb	case PI_TTYLOW:
1557121482Sjhb		db_printf("TTY ");
1558121482Sjhb		break;
1559121482Sjhb	case PI_TAPE:
1560121482Sjhb		db_printf("TAPE");
1561121482Sjhb		break;
1562121482Sjhb	case PI_NET:
1563121482Sjhb		db_printf("NET ");
1564121482Sjhb		break;
1565121482Sjhb	case PI_DISK:
1566121482Sjhb	case PI_DISKLOW:
1567121482Sjhb		db_printf("DISK");
1568121482Sjhb		break;
1569121482Sjhb	case PI_DULL:
1570121482Sjhb		db_printf("DULL");
1571121482Sjhb		break;
1572121482Sjhb	default:
1573121482Sjhb		if (ih->ih_pri >= PI_SOFT)
1574121482Sjhb			db_printf("SWI ");
1575121482Sjhb		else
1576121482Sjhb			db_printf("%4u", ih->ih_pri);
1577121482Sjhb		break;
1578121482Sjhb	}
1579121482Sjhb	db_printf(" ");
1580121482Sjhb	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1581121482Sjhb	db_printf("(%p)", ih->ih_argument);
1582121482Sjhb	if (ih->ih_need ||
1583166901Spiso	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1584121482Sjhb	    IH_MPSAFE)) != 0) {
1585121482Sjhb		db_printf(" {");
1586121482Sjhb		comma = 0;
1587121482Sjhb		if (ih->ih_flags & IH_EXCLUSIVE) {
1588121482Sjhb			if (comma)
1589121482Sjhb				db_printf(", ");
1590121482Sjhb			db_printf("EXCL");
1591121482Sjhb			comma = 1;
1592121482Sjhb		}
1593121482Sjhb		if (ih->ih_flags & IH_ENTROPY) {
1594121482Sjhb			if (comma)
1595121482Sjhb				db_printf(", ");
1596121482Sjhb			db_printf("ENTROPY");
1597121482Sjhb			comma = 1;
1598121482Sjhb		}
1599121482Sjhb		if (ih->ih_flags & IH_DEAD) {
1600121482Sjhb			if (comma)
1601121482Sjhb				db_printf(", ");
1602121482Sjhb			db_printf("DEAD");
1603121482Sjhb			comma = 1;
1604121482Sjhb		}
1605121482Sjhb		if (ih->ih_flags & IH_MPSAFE) {
1606121482Sjhb			if (comma)
1607121482Sjhb				db_printf(", ");
1608121482Sjhb			db_printf("MPSAFE");
1609121482Sjhb			comma = 1;
1610121482Sjhb		}
1611121482Sjhb		if (ih->ih_need) {
1612121482Sjhb			if (comma)
1613121482Sjhb				db_printf(", ");
1614121482Sjhb			db_printf("NEED");
1615121482Sjhb		}
1616121482Sjhb		db_printf("}");
1617121482Sjhb	}
1618121482Sjhb	db_printf("\n");
1619121482Sjhb}
1620121482Sjhb
1621121482Sjhb/*
1622151658Sjhb * Dump details about a event.
1623121482Sjhb */
1624121482Sjhbvoid
1625151658Sjhbdb_dump_intr_event(struct intr_event *ie, int handlers)
1626121482Sjhb{
1627151658Sjhb	struct intr_handler *ih;
1628151658Sjhb	struct intr_thread *it;
1629121482Sjhb	int comma;
1630121482Sjhb
1631151658Sjhb	db_printf("%s ", ie->ie_fullname);
1632151658Sjhb	it = ie->ie_thread;
1633151658Sjhb	if (it != NULL)
1634151658Sjhb		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1635151658Sjhb	else
1636151658Sjhb		db_printf("(no thread)");
1637151658Sjhb	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1638151658Sjhb	    (it != NULL && it->it_need)) {
1639121482Sjhb		db_printf(" {");
1640121482Sjhb		comma = 0;
1641151658Sjhb		if (ie->ie_flags & IE_SOFT) {
1642121482Sjhb			db_printf("SOFT");
1643121482Sjhb			comma = 1;
1644121482Sjhb		}
1645151658Sjhb		if (ie->ie_flags & IE_ENTROPY) {
1646121482Sjhb			if (comma)
1647121482Sjhb				db_printf(", ");
1648121482Sjhb			db_printf("ENTROPY");
1649121482Sjhb			comma = 1;
1650121482Sjhb		}
1651151658Sjhb		if (ie->ie_flags & IE_ADDING_THREAD) {
1652121482Sjhb			if (comma)
1653121482Sjhb				db_printf(", ");
1654151658Sjhb			db_printf("ADDING_THREAD");
1655121482Sjhb			comma = 1;
1656121482Sjhb		}
1657151658Sjhb		if (it != NULL && it->it_need) {
1658121482Sjhb			if (comma)
1659121482Sjhb				db_printf(", ");
1660121482Sjhb			db_printf("NEED");
1661121482Sjhb		}
1662121482Sjhb		db_printf("}");
1663121482Sjhb	}
1664121482Sjhb	db_printf("\n");
1665121482Sjhb
1666121482Sjhb	if (handlers)
1667151658Sjhb		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1668121482Sjhb		    db_dump_intrhand(ih);
1669121482Sjhb}
1670151658Sjhb
1671151658Sjhb/*
1672151658Sjhb * Dump data about interrupt handlers
1673151658Sjhb */
1674151658SjhbDB_SHOW_COMMAND(intr, db_show_intr)
1675151658Sjhb{
1676151658Sjhb	struct intr_event *ie;
1677160312Sjhb	int all, verbose;
1678151658Sjhb
1679151658Sjhb	verbose = index(modif, 'v') != NULL;
1680151658Sjhb	all = index(modif, 'a') != NULL;
1681151658Sjhb	TAILQ_FOREACH(ie, &event_list, ie_list) {
1682151658Sjhb		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1683151658Sjhb			continue;
1684151658Sjhb		db_dump_intr_event(ie, verbose);
1685160312Sjhb		if (db_pager_quit)
1686160312Sjhb			break;
1687151658Sjhb	}
1688151658Sjhb}
1689121482Sjhb#endif /* DDB */
1690121482Sjhb
1691121482Sjhb/*
169267551Sjhb * Start standard software interrupt threads
169366698Sjhb */
169467551Sjhbstatic void
169572237Sjhbstart_softintr(void *dummy)
169667551Sjhb{
169772237Sjhb
1698177859Sjeff	if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1699177859Sjeff		panic("died while creating vm swi ithread");
170066698Sjhb}
1701177253SrwatsonSYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1702177253Srwatson    NULL);
170366698Sjhb
1704151658Sjhb/*
170577582Stmm * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
170677582Stmm * The data for this machine dependent, and the declarations are in machine
170777582Stmm * dependent code.  The layout of intrnames and intrcnt however is machine
170877582Stmm * independent.
170977582Stmm *
171077582Stmm * We do not know the length of intrcnt and intrnames at compile time, so
171177582Stmm * calculate things at run time.
171277582Stmm */
171377582Stmmstatic int
171477582Stmmsysctl_intrnames(SYSCTL_HANDLER_ARGS)
171577582Stmm{
1716151658Sjhb	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
171777582Stmm	   req));
171877582Stmm}
171977582Stmm
172077582StmmSYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
172177582Stmm    NULL, 0, sysctl_intrnames, "", "Interrupt Names");
172277582Stmm
172377582Stmmstatic int
172477582Stmmsysctl_intrcnt(SYSCTL_HANDLER_ARGS)
172577582Stmm{
1726151658Sjhb	return (sysctl_handle_opaque(oidp, intrcnt,
172777582Stmm	    (char *)eintrcnt - (char *)intrcnt, req));
172877582Stmm}
172977582Stmm
173077582StmmSYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
173177582Stmm    NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1732121482Sjhb
1733121482Sjhb#ifdef DDB
1734121482Sjhb/*
1735121482Sjhb * DDB command to dump the interrupt statistics.
1736121482Sjhb */
1737121482SjhbDB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1738121482Sjhb{
1739121482Sjhb	u_long *i;
1740121482Sjhb	char *cp;
1741121482Sjhb
1742121482Sjhb	cp = intrnames;
1743160312Sjhb	for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
1744121482Sjhb		if (*cp == '\0')
1745121482Sjhb			break;
1746121482Sjhb		if (*i != 0)
1747121482Sjhb			db_printf("%s\t%lu\n", cp, *i);
1748121482Sjhb		cp += strlen(cp) + 1;
1749121482Sjhb	}
1750121482Sjhb}
1751121482Sjhb#endif
1752