xref: /freebsd/sys/kern/kern_intr.c (revision c4eb66309fb2d27bb1051f5a5aa326f9a8067d3f)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4425f9fdaSStefan Eßer  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5425f9fdaSStefan Eßer  * All rights reserved.
6425f9fdaSStefan Eßer  *
7425f9fdaSStefan Eßer  * Redistribution and use in source and binary forms, with or without
8425f9fdaSStefan Eßer  * modification, are permitted provided that the following conditions
9425f9fdaSStefan Eßer  * are met:
10425f9fdaSStefan Eßer  * 1. Redistributions of source code must retain the above copyright
11425f9fdaSStefan Eßer  *    notice unmodified, this list of conditions, and the following
12425f9fdaSStefan Eßer  *    disclaimer.
13425f9fdaSStefan Eßer  * 2. Redistributions in binary form must reproduce the above copyright
14425f9fdaSStefan Eßer  *    notice, this list of conditions and the following disclaimer in the
15425f9fdaSStefan Eßer  *    documentation and/or other materials provided with the distribution.
16425f9fdaSStefan Eßer  *
17425f9fdaSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18425f9fdaSStefan Eßer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19425f9fdaSStefan Eßer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20425f9fdaSStefan Eßer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21425f9fdaSStefan Eßer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22425f9fdaSStefan Eßer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23425f9fdaSStefan Eßer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24425f9fdaSStefan Eßer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25425f9fdaSStefan Eßer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26425f9fdaSStefan Eßer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27425f9fdaSStefan Eßer  */
28425f9fdaSStefan Eßer 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
313900ddb2SDoug Rabson 
328b201c42SJohn Baldwin #include "opt_ddb.h"
33b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h"
348b201c42SJohn Baldwin 
351c5bb3eaSPeter Wemm #include <sys/param.h>
369a94c9c5SJohn Baldwin #include <sys/bus.h>
37c11110eaSAlfred Perlstein #include <sys/conf.h>
389b33b154SJeff Roberson #include <sys/cpuset.h>
399a94c9c5SJohn Baldwin #include <sys/rtprio.h>
40425f9fdaSStefan Eßer #include <sys/systm.h>
4168352337SDoug Rabson #include <sys/interrupt.h>
421931cf94SJohn Baldwin #include <sys/kernel.h>
431931cf94SJohn Baldwin #include <sys/kthread.h>
441931cf94SJohn Baldwin #include <sys/ktr.h>
4505b2c96fSBruce Evans #include <sys/limits.h>
46f34fa851SJohn Baldwin #include <sys/lock.h>
471931cf94SJohn Baldwin #include <sys/malloc.h>
4835e0e5b3SJohn Baldwin #include <sys/mutex.h>
49cebc7fb1SJohn Baldwin #include <sys/priv.h>
501931cf94SJohn Baldwin #include <sys/proc.h>
513e5da754SJohn Baldwin #include <sys/random.h>
52b4151f71SJohn Baldwin #include <sys/resourcevar.h>
5363710c4dSJohn Baldwin #include <sys/sched.h>
54eaf86d16SJohn Baldwin #include <sys/smp.h>
55d279178dSThomas Moestl #include <sys/sysctl.h>
566205924aSKip Macy #include <sys/syslog.h>
571931cf94SJohn Baldwin #include <sys/unistd.h>
581931cf94SJohn Baldwin #include <sys/vmmeter.h>
591931cf94SJohn Baldwin #include <machine/atomic.h>
601931cf94SJohn Baldwin #include <machine/cpu.h>
618088699fSJohn Baldwin #include <machine/md_var.h>
62b4151f71SJohn Baldwin #include <machine/stdarg.h>
638b201c42SJohn Baldwin #ifdef DDB
648b201c42SJohn Baldwin #include <ddb/ddb.h>
658b201c42SJohn Baldwin #include <ddb/db_sym.h>
668b201c42SJohn Baldwin #endif
67425f9fdaSStefan Eßer 
68e0f66ef8SJohn Baldwin /*
69e0f66ef8SJohn Baldwin  * Describe an interrupt thread.  There is one of these per interrupt event.
70e0f66ef8SJohn Baldwin  */
71e0f66ef8SJohn Baldwin struct intr_thread {
72e0f66ef8SJohn Baldwin 	struct intr_event *it_event;
73e0f66ef8SJohn Baldwin 	struct thread *it_thread;	/* Kernel thread. */
74e0f66ef8SJohn Baldwin 	int	it_flags;		/* (j) IT_* flags. */
75e0f66ef8SJohn Baldwin 	int	it_need;		/* Needs service. */
763e5da754SJohn Baldwin };
773e5da754SJohn Baldwin 
78e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */
79e0f66ef8SJohn Baldwin #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
80e4cd31ddSJeff Roberson #define	IT_WAIT		0x000002	/* Thread is waiting for completion. */
81e0f66ef8SJohn Baldwin 
82e0f66ef8SJohn Baldwin struct	intr_entropy {
83e0f66ef8SJohn Baldwin 	struct	thread *td;
84e0f66ef8SJohn Baldwin 	uintptr_t event;
85e0f66ef8SJohn Baldwin };
86e0f66ef8SJohn Baldwin 
87e0f66ef8SJohn Baldwin struct	intr_event *tty_intr_event;
887b1fe905SBruce Evans void	*vm_ih;
897ab24ea3SJulian Elischer struct proc *intrproc;
901931cf94SJohn Baldwin 
91b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
92b4151f71SJohn Baldwin 
935d0e8299SConrad Meyer static int intr_storm_threshold = 0;
94af3b2549SHans Petter Selasky SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
957870c3c6SJohn Baldwin     &intr_storm_threshold, 0,
967b1fe905SBruce Evans     "Number of consecutive interrupts before storm protection is enabled");
97e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list =
98e0f66ef8SJohn Baldwin     TAILQ_HEAD_INITIALIZER(event_list);
999b33b154SJeff Roberson static struct mtx event_lock;
1009b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
1017b1fe905SBruce Evans 
102e0f66ef8SJohn Baldwin static void	intr_event_update(struct intr_event *ie);
1031ee1b687SJohn Baldwin static int	intr_event_schedule_thread(struct intr_event *ie);
104e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name);
105e0f66ef8SJohn Baldwin static void	ithread_destroy(struct intr_thread *ithread);
106bafe5a31SPaolo Pisati static void	ithread_execute_handlers(struct proc *p,
107bafe5a31SPaolo Pisati 		    struct intr_event *ie);
1087b1fe905SBruce Evans static void	ithread_loop(void *);
109e0f66ef8SJohn Baldwin static void	ithread_update(struct intr_thread *ithd);
1107b1fe905SBruce Evans static void	start_softintr(void *);
1117870c3c6SJohn Baldwin 
112bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */
113b4151f71SJohn Baldwin u_char
114e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags)
1159a94c9c5SJohn Baldwin {
116b4151f71SJohn Baldwin 	u_char pri;
1179a94c9c5SJohn Baldwin 
118b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
1195a280d9cSPeter Wemm 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
1209a94c9c5SJohn Baldwin 	switch (flags) {
121b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
122d3305205SJohn Baldwin 		pri = PI_TTY;
1239a94c9c5SJohn Baldwin 		break;
1249a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
1259a94c9c5SJohn Baldwin 		pri = PI_DISK;
1269a94c9c5SJohn Baldwin 		break;
1279a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
1289a94c9c5SJohn Baldwin 		pri = PI_NET;
1299a94c9c5SJohn Baldwin 		break;
1309a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
131d3305205SJohn Baldwin 		pri = PI_DISK;
1329a94c9c5SJohn Baldwin 		break;
133d3305205SJohn Baldwin 	case INTR_TYPE_AV:
1345a280d9cSPeter Wemm 		pri = PI_AV;
1355a280d9cSPeter Wemm 		break;
136b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
137b4151f71SJohn Baldwin 		pri = PI_REALTIME;
138b4151f71SJohn Baldwin 		break;
1399a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1409a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1419a94c9c5SJohn Baldwin 		break;
1429a94c9c5SJohn Baldwin 	default:
143b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
144e0f66ef8SJohn Baldwin 		panic("intr_priority: no interrupt type in flags");
1459a94c9c5SJohn Baldwin 	}
1469a94c9c5SJohn Baldwin 
1479a94c9c5SJohn Baldwin 	return pri;
1489a94c9c5SJohn Baldwin }
1499a94c9c5SJohn Baldwin 
150b4151f71SJohn Baldwin /*
151e0f66ef8SJohn Baldwin  * Update an ithread based on the associated intr_event.
152b4151f71SJohn Baldwin  */
153b4151f71SJohn Baldwin static void
154e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd)
155b4151f71SJohn Baldwin {
156e0f66ef8SJohn Baldwin 	struct intr_event *ie;
157b40ce416SJulian Elischer 	struct thread *td;
158e0f66ef8SJohn Baldwin 	u_char pri;
1598088699fSJohn Baldwin 
160e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
161e0f66ef8SJohn Baldwin 	td = ithd->it_thread;
162111b043cSAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
163b4151f71SJohn Baldwin 
164e0f66ef8SJohn Baldwin 	/* Determine the overall priority of this event. */
165111b043cSAndriy Gapon 	if (CK_SLIST_EMPTY(&ie->ie_handlers))
166e0f66ef8SJohn Baldwin 		pri = PRI_MAX_ITHD;
167e0f66ef8SJohn Baldwin 	else
168111b043cSAndriy Gapon 		pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
169e80fb434SRobert Drehmel 
170e0f66ef8SJohn Baldwin 	/* Update name and priority. */
1717ab24ea3SJulian Elischer 	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
17244ad5475SJohn Baldwin #ifdef KTR
17344ad5475SJohn Baldwin 	sched_clear_tdname(td);
17444ad5475SJohn Baldwin #endif
175982d11f8SJeff Roberson 	thread_lock(td);
176e0f66ef8SJohn Baldwin 	sched_prio(td, pri);
177982d11f8SJeff Roberson 	thread_unlock(td);
178b4151f71SJohn Baldwin }
179e0f66ef8SJohn Baldwin 
180e0f66ef8SJohn Baldwin /*
181e0f66ef8SJohn Baldwin  * Regenerate the full name of an interrupt event and update its priority.
182e0f66ef8SJohn Baldwin  */
183e0f66ef8SJohn Baldwin static void
184e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie)
185e0f66ef8SJohn Baldwin {
186e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
187e0f66ef8SJohn Baldwin 	char *last;
188e0f66ef8SJohn Baldwin 	int missed, space;
189e0f66ef8SJohn Baldwin 
190e0f66ef8SJohn Baldwin 	/* Start off with no entropy and just the name of the event. */
191e0f66ef8SJohn Baldwin 	mtx_assert(&ie->ie_lock, MA_OWNED);
192e0f66ef8SJohn Baldwin 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
193*c4eb6630SGleb Smirnoff 	ie->ie_hflags = 0;
1940811d60aSJohn Baldwin 	missed = 0;
195e0f66ef8SJohn Baldwin 	space = 1;
196e0f66ef8SJohn Baldwin 
197e0f66ef8SJohn Baldwin 	/* Run through all the handlers updating values. */
198111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1996d51c0feSIan Lepore 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
200e0f66ef8SJohn Baldwin 		    sizeof(ie->ie_fullname)) {
201e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " ");
202e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, ih->ih_name);
203e0f66ef8SJohn Baldwin 			space = 0;
2040811d60aSJohn Baldwin 		} else
2050811d60aSJohn Baldwin 			missed++;
206*c4eb6630SGleb Smirnoff 		ie->ie_hflags |= ih->ih_flags;
2070811d60aSJohn Baldwin 	}
208e0f66ef8SJohn Baldwin 
209e0f66ef8SJohn Baldwin 	/*
21067da50a0SIan Lepore 	 * If there is only one handler and its name is too long, just copy in
21167da50a0SIan Lepore 	 * as much of the end of the name (includes the unit number) as will
21267da50a0SIan Lepore 	 * fit.  Otherwise, we have multiple handlers and not all of the names
21367da50a0SIan Lepore 	 * will fit.  Add +'s to indicate missing names.  If we run out of room
21467da50a0SIan Lepore 	 * and still have +'s to add, change the last character from a + to a *.
215e0f66ef8SJohn Baldwin 	 */
21667da50a0SIan Lepore 	if (missed == 1 && space == 1) {
21767da50a0SIan Lepore 		ih = CK_SLIST_FIRST(&ie->ie_handlers);
21867da50a0SIan Lepore 		missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
21967da50a0SIan Lepore 		    sizeof(ie->ie_fullname);
22067da50a0SIan Lepore 		strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
22167da50a0SIan Lepore 		strcat(ie->ie_fullname, &ih->ih_name[missed]);
22267da50a0SIan Lepore 		missed = 0;
22367da50a0SIan Lepore 	}
224e0f66ef8SJohn Baldwin 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
2250811d60aSJohn Baldwin 	while (missed-- > 0) {
226e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
227e0f66ef8SJohn Baldwin 			if (*last == '+') {
228e0f66ef8SJohn Baldwin 				*last = '*';
229e0f66ef8SJohn Baldwin 				break;
230b4151f71SJohn Baldwin 			} else
231e0f66ef8SJohn Baldwin 				*last = '+';
232e0f66ef8SJohn Baldwin 		} else if (space) {
233e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " +");
234e0f66ef8SJohn Baldwin 			space = 0;
235e0f66ef8SJohn Baldwin 		} else
236e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, "+");
237b4151f71SJohn Baldwin 	}
238e0f66ef8SJohn Baldwin 
239e0f66ef8SJohn Baldwin 	/*
240e0f66ef8SJohn Baldwin 	 * If this event has an ithread, update it's priority and
241e0f66ef8SJohn Baldwin 	 * name.
242e0f66ef8SJohn Baldwin 	 */
243e0f66ef8SJohn Baldwin 	if (ie->ie_thread != NULL)
244e0f66ef8SJohn Baldwin 		ithread_update(ie->ie_thread);
245e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
246b4151f71SJohn Baldwin }
247b4151f71SJohn Baldwin 
248b4151f71SJohn Baldwin int
2499b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq,
2501ee1b687SJohn Baldwin     void (*pre_ithread)(void *), void (*post_ithread)(void *),
251066da805SAdrian Chadd     void (*post_filter)(void *), int (*assign_cpu)(void *, int),
2521ee1b687SJohn Baldwin     const char *fmt, ...)
253bafe5a31SPaolo Pisati {
254bafe5a31SPaolo Pisati 	struct intr_event *ie;
255bafe5a31SPaolo Pisati 	va_list ap;
256bafe5a31SPaolo Pisati 
257bafe5a31SPaolo Pisati 	/* The only valid flag during creation is IE_SOFT. */
258bafe5a31SPaolo Pisati 	if ((flags & ~IE_SOFT) != 0)
259bafe5a31SPaolo Pisati 		return (EINVAL);
260bafe5a31SPaolo Pisati 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
261bafe5a31SPaolo Pisati 	ie->ie_source = source;
2621ee1b687SJohn Baldwin 	ie->ie_pre_ithread = pre_ithread;
2631ee1b687SJohn Baldwin 	ie->ie_post_ithread = post_ithread;
2641ee1b687SJohn Baldwin 	ie->ie_post_filter = post_filter;
2656d2d1c04SJohn Baldwin 	ie->ie_assign_cpu = assign_cpu;
266bafe5a31SPaolo Pisati 	ie->ie_flags = flags;
2679b33b154SJeff Roberson 	ie->ie_irq = irq;
268eaf86d16SJohn Baldwin 	ie->ie_cpu = NOCPU;
269111b043cSAndriy Gapon 	CK_SLIST_INIT(&ie->ie_handlers);
270bafe5a31SPaolo Pisati 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
271bafe5a31SPaolo Pisati 
272bafe5a31SPaolo Pisati 	va_start(ap, fmt);
273bafe5a31SPaolo Pisati 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
274bafe5a31SPaolo Pisati 	va_end(ap);
275bafe5a31SPaolo Pisati 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
2769b33b154SJeff Roberson 	mtx_lock(&event_lock);
277bafe5a31SPaolo Pisati 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
2789b33b154SJeff Roberson 	mtx_unlock(&event_lock);
279bafe5a31SPaolo Pisati 	if (event != NULL)
280bafe5a31SPaolo Pisati 		*event = ie;
281bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
282bafe5a31SPaolo Pisati 	return (0);
283bafe5a31SPaolo Pisati }
284b4151f71SJohn Baldwin 
285eaf86d16SJohn Baldwin /*
286eaf86d16SJohn Baldwin  * Bind an interrupt event to the specified CPU.  Note that not all
287eaf86d16SJohn Baldwin  * platforms support binding an interrupt to a CPU.  For those
28829dfb631SConrad Meyer  * platforms this request will fail.  Using a cpu id of NOCPU unbinds
289eaf86d16SJohn Baldwin  * the interrupt event.
290eaf86d16SJohn Baldwin  */
29129dfb631SConrad Meyer static int
29229dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
293eaf86d16SJohn Baldwin {
2949b33b154SJeff Roberson 	lwpid_t id;
295eaf86d16SJohn Baldwin 	int error;
296eaf86d16SJohn Baldwin 
297eaf86d16SJohn Baldwin 	/* Need a CPU to bind to. */
298eaf86d16SJohn Baldwin 	if (cpu != NOCPU && CPU_ABSENT(cpu))
299eaf86d16SJohn Baldwin 		return (EINVAL);
300eaf86d16SJohn Baldwin 
301eaf86d16SJohn Baldwin 	if (ie->ie_assign_cpu == NULL)
302eaf86d16SJohn Baldwin 		return (EOPNOTSUPP);
303cebc7fb1SJohn Baldwin 
304cebc7fb1SJohn Baldwin 	error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
305cebc7fb1SJohn Baldwin 	if (error)
306cebc7fb1SJohn Baldwin 		return (error);
307cebc7fb1SJohn Baldwin 
3089b33b154SJeff Roberson 	/*
309cebc7fb1SJohn Baldwin 	 * If we have any ithreads try to set their mask first to verify
310cebc7fb1SJohn Baldwin 	 * permissions, etc.
3119b33b154SJeff Roberson 	 */
31229dfb631SConrad Meyer 	if (bindithread) {
313eaf86d16SJohn Baldwin 		mtx_lock(&ie->ie_lock);
3149b33b154SJeff Roberson 		if (ie->ie_thread != NULL) {
3159b33b154SJeff Roberson 			id = ie->ie_thread->it_thread->td_tid;
316eaf86d16SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
31781198539SAlexander V. Chernikov 			error = cpuset_setithread(id, cpu);
3189b33b154SJeff Roberson 			if (error)
3199b33b154SJeff Roberson 				return (error);
3209b33b154SJeff Roberson 		} else
321eaf86d16SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
32229dfb631SConrad Meyer 	}
32329dfb631SConrad Meyer 	if (bindirq)
324eaf86d16SJohn Baldwin 		error = ie->ie_assign_cpu(ie->ie_source, cpu);
325cebc7fb1SJohn Baldwin 	if (error) {
32629dfb631SConrad Meyer 		if (bindithread) {
327cebc7fb1SJohn Baldwin 			mtx_lock(&ie->ie_lock);
328cebc7fb1SJohn Baldwin 			if (ie->ie_thread != NULL) {
32981198539SAlexander V. Chernikov 				cpu = ie->ie_cpu;
330cebc7fb1SJohn Baldwin 				id = ie->ie_thread->it_thread->td_tid;
331cebc7fb1SJohn Baldwin 				mtx_unlock(&ie->ie_lock);
33281198539SAlexander V. Chernikov 				(void)cpuset_setithread(id, cpu);
333cebc7fb1SJohn Baldwin 			} else
334cebc7fb1SJohn Baldwin 				mtx_unlock(&ie->ie_lock);
33529dfb631SConrad Meyer 		}
336eaf86d16SJohn Baldwin 		return (error);
337cebc7fb1SJohn Baldwin 	}
338cebc7fb1SJohn Baldwin 
33929dfb631SConrad Meyer 	if (bindirq) {
340eaf86d16SJohn Baldwin 		mtx_lock(&ie->ie_lock);
341eaf86d16SJohn Baldwin 		ie->ie_cpu = cpu;
3429b33b154SJeff Roberson 		mtx_unlock(&ie->ie_lock);
34329dfb631SConrad Meyer 	}
3449b33b154SJeff Roberson 
3459b33b154SJeff Roberson 	return (error);
3469b33b154SJeff Roberson }
3479b33b154SJeff Roberson 
34829dfb631SConrad Meyer /*
34929dfb631SConrad Meyer  * Bind an interrupt event to the specified CPU.  For supported platforms, any
35029dfb631SConrad Meyer  * associated ithreads as well as the primary interrupt context will be bound
35129dfb631SConrad Meyer  * to the specificed CPU.
35229dfb631SConrad Meyer  */
35329dfb631SConrad Meyer int
35429dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu)
35529dfb631SConrad Meyer {
35629dfb631SConrad Meyer 
35729dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, true, true));
35829dfb631SConrad Meyer }
35929dfb631SConrad Meyer 
36029dfb631SConrad Meyer /*
36129dfb631SConrad Meyer  * Bind an interrupt event to the specified CPU, but do not bind associated
36229dfb631SConrad Meyer  * ithreads.
36329dfb631SConrad Meyer  */
36429dfb631SConrad Meyer int
36529dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu)
36629dfb631SConrad Meyer {
36729dfb631SConrad Meyer 
36829dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, true, false));
36929dfb631SConrad Meyer }
37029dfb631SConrad Meyer 
37129dfb631SConrad Meyer /*
37229dfb631SConrad Meyer  * Bind an interrupt event's ithread to the specified CPU.
37329dfb631SConrad Meyer  */
37429dfb631SConrad Meyer int
37529dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu)
37629dfb631SConrad Meyer {
37729dfb631SConrad Meyer 
37829dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, false, true));
37929dfb631SConrad Meyer }
38029dfb631SConrad Meyer 
3814e255d74SAndrew Gallatin /*
3824e255d74SAndrew Gallatin  * Bind an interrupt event's ithread to the specified cpuset.
3834e255d74SAndrew Gallatin  */
3844e255d74SAndrew Gallatin int
3854e255d74SAndrew Gallatin intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
3864e255d74SAndrew Gallatin {
3874e255d74SAndrew Gallatin 	lwpid_t id;
3884e255d74SAndrew Gallatin 
3894e255d74SAndrew Gallatin 	mtx_lock(&ie->ie_lock);
3904e255d74SAndrew Gallatin 	if (ie->ie_thread != NULL) {
3914e255d74SAndrew Gallatin 		id = ie->ie_thread->it_thread->td_tid;
3924e255d74SAndrew Gallatin 		mtx_unlock(&ie->ie_lock);
3934e255d74SAndrew Gallatin 		return (cpuset_setthread(id, cs));
3944e255d74SAndrew Gallatin 	} else {
3954e255d74SAndrew Gallatin 		mtx_unlock(&ie->ie_lock);
3964e255d74SAndrew Gallatin 	}
3974e255d74SAndrew Gallatin 	return (ENODEV);
3984e255d74SAndrew Gallatin }
3994e255d74SAndrew Gallatin 
4009b33b154SJeff Roberson static struct intr_event *
4019b33b154SJeff Roberson intr_lookup(int irq)
4029b33b154SJeff Roberson {
4039b33b154SJeff Roberson 	struct intr_event *ie;
4049b33b154SJeff Roberson 
4059b33b154SJeff Roberson 	mtx_lock(&event_lock);
4069b33b154SJeff Roberson 	TAILQ_FOREACH(ie, &event_list, ie_list)
4079b33b154SJeff Roberson 		if (ie->ie_irq == irq &&
4089b33b154SJeff Roberson 		    (ie->ie_flags & IE_SOFT) == 0 &&
409111b043cSAndriy Gapon 		    CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
4109b33b154SJeff Roberson 			break;
4119b33b154SJeff Roberson 	mtx_unlock(&event_lock);
4129b33b154SJeff Roberson 	return (ie);
4139b33b154SJeff Roberson }
4149b33b154SJeff Roberson 
4159b33b154SJeff Roberson int
41629dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m)
4179b33b154SJeff Roberson {
4189b33b154SJeff Roberson 	struct intr_event *ie;
4199b33b154SJeff Roberson 	cpuset_t *mask;
4203fe93b94SAdrian Chadd 	int cpu, n;
4219b33b154SJeff Roberson 
4229b33b154SJeff Roberson 	mask = m;
4239b33b154SJeff Roberson 	cpu = NOCPU;
4249b33b154SJeff Roberson 	/*
4259b33b154SJeff Roberson 	 * If we're setting all cpus we can unbind.  Otherwise make sure
4269b33b154SJeff Roberson 	 * only one cpu is in the set.
4279b33b154SJeff Roberson 	 */
4289b33b154SJeff Roberson 	if (CPU_CMP(cpuset_root, mask)) {
4299b33b154SJeff Roberson 		for (n = 0; n < CPU_SETSIZE; n++) {
4309b33b154SJeff Roberson 			if (!CPU_ISSET(n, mask))
4319b33b154SJeff Roberson 				continue;
4329b33b154SJeff Roberson 			if (cpu != NOCPU)
4339b33b154SJeff Roberson 				return (EINVAL);
4343fe93b94SAdrian Chadd 			cpu = n;
4359b33b154SJeff Roberson 		}
4369b33b154SJeff Roberson 	}
4379b33b154SJeff Roberson 	ie = intr_lookup(irq);
4389b33b154SJeff Roberson 	if (ie == NULL)
4399b33b154SJeff Roberson 		return (ESRCH);
44029dfb631SConrad Meyer 	switch (mode) {
44129dfb631SConrad Meyer 	case CPU_WHICH_IRQ:
4429bd55acfSJohn Baldwin 		return (intr_event_bind(ie, cpu));
44329dfb631SConrad Meyer 	case CPU_WHICH_INTRHANDLER:
44429dfb631SConrad Meyer 		return (intr_event_bind_irqonly(ie, cpu));
44529dfb631SConrad Meyer 	case CPU_WHICH_ITHREAD:
44629dfb631SConrad Meyer 		return (intr_event_bind_ithread(ie, cpu));
44729dfb631SConrad Meyer 	default:
44829dfb631SConrad Meyer 		return (EINVAL);
44929dfb631SConrad Meyer 	}
4509b33b154SJeff Roberson }
4519b33b154SJeff Roberson 
4529b33b154SJeff Roberson int
45329dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m)
4549b33b154SJeff Roberson {
4559b33b154SJeff Roberson 	struct intr_event *ie;
45629dfb631SConrad Meyer 	struct thread *td;
45729dfb631SConrad Meyer 	struct proc *p;
4589b33b154SJeff Roberson 	cpuset_t *mask;
45929dfb631SConrad Meyer 	lwpid_t id;
46029dfb631SConrad Meyer 	int error;
4619b33b154SJeff Roberson 
4629b33b154SJeff Roberson 	mask = m;
4639b33b154SJeff Roberson 	ie = intr_lookup(irq);
4649b33b154SJeff Roberson 	if (ie == NULL)
4659b33b154SJeff Roberson 		return (ESRCH);
46629dfb631SConrad Meyer 
46729dfb631SConrad Meyer 	error = 0;
4689b33b154SJeff Roberson 	CPU_ZERO(mask);
46929dfb631SConrad Meyer 	switch (mode) {
47029dfb631SConrad Meyer 	case CPU_WHICH_IRQ:
47129dfb631SConrad Meyer 	case CPU_WHICH_INTRHANDLER:
4729b33b154SJeff Roberson 		mtx_lock(&ie->ie_lock);
4739b33b154SJeff Roberson 		if (ie->ie_cpu == NOCPU)
4749b33b154SJeff Roberson 			CPU_COPY(cpuset_root, mask);
4759b33b154SJeff Roberson 		else
4769b33b154SJeff Roberson 			CPU_SET(ie->ie_cpu, mask);
477eaf86d16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
47829dfb631SConrad Meyer 		break;
47929dfb631SConrad Meyer 	case CPU_WHICH_ITHREAD:
48029dfb631SConrad Meyer 		mtx_lock(&ie->ie_lock);
48129dfb631SConrad Meyer 		if (ie->ie_thread == NULL) {
48229dfb631SConrad Meyer 			mtx_unlock(&ie->ie_lock);
48329dfb631SConrad Meyer 			CPU_COPY(cpuset_root, mask);
48429dfb631SConrad Meyer 		} else {
48529dfb631SConrad Meyer 			id = ie->ie_thread->it_thread->td_tid;
48629dfb631SConrad Meyer 			mtx_unlock(&ie->ie_lock);
48729dfb631SConrad Meyer 			error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
48829dfb631SConrad Meyer 			if (error != 0)
48929dfb631SConrad Meyer 				return (error);
49029dfb631SConrad Meyer 			CPU_COPY(&td->td_cpuset->cs_mask, mask);
49129dfb631SConrad Meyer 			PROC_UNLOCK(p);
49229dfb631SConrad Meyer 		}
49329dfb631SConrad Meyer 	default:
49429dfb631SConrad Meyer 		return (EINVAL);
49529dfb631SConrad Meyer 	}
496eaf86d16SJohn Baldwin 	return (0);
497eaf86d16SJohn Baldwin }
498eaf86d16SJohn Baldwin 
499b4151f71SJohn Baldwin int
500e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie)
501b4151f71SJohn Baldwin {
502b4151f71SJohn Baldwin 
5039b33b154SJeff Roberson 	mtx_lock(&event_lock);
504e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
505111b043cSAndriy Gapon 	if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
506e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
5079b33b154SJeff Roberson 		mtx_unlock(&event_lock);
508e0f66ef8SJohn Baldwin 		return (EBUSY);
5094d29cb2dSJohn Baldwin 	}
510e0f66ef8SJohn Baldwin 	TAILQ_REMOVE(&event_list, ie, ie_list);
5119477358dSJohn Baldwin #ifndef notyet
5129477358dSJohn Baldwin 	if (ie->ie_thread != NULL) {
5139477358dSJohn Baldwin 		ithread_destroy(ie->ie_thread);
5149477358dSJohn Baldwin 		ie->ie_thread = NULL;
5159477358dSJohn Baldwin 	}
5169477358dSJohn Baldwin #endif
517e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
5189b33b154SJeff Roberson 	mtx_unlock(&event_lock);
519e0f66ef8SJohn Baldwin 	mtx_destroy(&ie->ie_lock);
520e0f66ef8SJohn Baldwin 	free(ie, M_ITHREAD);
521e0f66ef8SJohn Baldwin 	return (0);
522e0f66ef8SJohn Baldwin }
523e0f66ef8SJohn Baldwin 
524e0f66ef8SJohn Baldwin static struct intr_thread *
525e0f66ef8SJohn Baldwin ithread_create(const char *name)
526e0f66ef8SJohn Baldwin {
527e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
528e0f66ef8SJohn Baldwin 	struct thread *td;
529e0f66ef8SJohn Baldwin 	int error;
530e0f66ef8SJohn Baldwin 
531e0f66ef8SJohn Baldwin 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
532e0f66ef8SJohn Baldwin 
5337ab24ea3SJulian Elischer 	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
5347ab24ea3SJulian Elischer 		    &td, RFSTOPPED | RFHIGHPID,
5359ef95d01SJulian Elischer 		    0, "intr", "%s", name);
536e0f66ef8SJohn Baldwin 	if (error)
5373745c395SJulian Elischer 		panic("kproc_create() failed with %d", error);
538982d11f8SJeff Roberson 	thread_lock(td);
539ad1e7d28SJulian Elischer 	sched_class(td, PRI_ITHD);
540e0f66ef8SJohn Baldwin 	TD_SET_IWAIT(td);
541982d11f8SJeff Roberson 	thread_unlock(td);
542e0f66ef8SJohn Baldwin 	td->td_pflags |= TDP_ITHREAD;
543e0f66ef8SJohn Baldwin 	ithd->it_thread = td;
544e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
545e0f66ef8SJohn Baldwin 	return (ithd);
546e0f66ef8SJohn Baldwin }
547e0f66ef8SJohn Baldwin 
548e0f66ef8SJohn Baldwin static void
549e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread)
550e0f66ef8SJohn Baldwin {
551e0f66ef8SJohn Baldwin 	struct thread *td;
552e0f66ef8SJohn Baldwin 
553bb141be1SScott Long 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
554e0f66ef8SJohn Baldwin 	td = ithread->it_thread;
555982d11f8SJeff Roberson 	thread_lock(td);
556e0f66ef8SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
55771fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
55871fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
559f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
56061a74c5cSJeff Roberson 	} else
561982d11f8SJeff Roberson 		thread_unlock(td);
562b4151f71SJohn Baldwin }
563b4151f71SJohn Baldwin 
564b4151f71SJohn Baldwin int
565e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name,
566ef544f63SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
567ef544f63SPaolo Pisati     enum intr_type flags, void **cookiep)
568b4151f71SJohn Baldwin {
569e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *temp_ih;
570111b043cSAndriy Gapon 	struct intr_handler **prevptr;
571e0f66ef8SJohn Baldwin 	struct intr_thread *it;
572b4151f71SJohn Baldwin 
573ef544f63SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
574b4151f71SJohn Baldwin 		return (EINVAL);
575b4151f71SJohn Baldwin 
576e0f66ef8SJohn Baldwin 	/* Allocate and populate an interrupt handler structure. */
577e0f66ef8SJohn Baldwin 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
578ef544f63SPaolo Pisati 	ih->ih_filter = filter;
579b4151f71SJohn Baldwin 	ih->ih_handler = handler;
580b4151f71SJohn Baldwin 	ih->ih_argument = arg;
58137b8ef16SJohn Baldwin 	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
582e0f66ef8SJohn Baldwin 	ih->ih_event = ie;
583b4151f71SJohn Baldwin 	ih->ih_pri = pri;
584ef544f63SPaolo Pisati 	if (flags & INTR_EXCL)
585b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
586b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
587b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
588b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
589b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
590b4151f71SJohn Baldwin 
591e0f66ef8SJohn Baldwin 	/* We can only have one exclusive handler in a event. */
592e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
593111b043cSAndriy Gapon 	if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
594e0f66ef8SJohn Baldwin 		if ((flags & INTR_EXCL) ||
595111b043cSAndriy Gapon 		    (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
596e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
597b4151f71SJohn Baldwin 			free(ih, M_ITHREAD);
598b4151f71SJohn Baldwin 			return (EINVAL);
599b4151f71SJohn Baldwin 		}
600e0f66ef8SJohn Baldwin 	}
601e0f66ef8SJohn Baldwin 
602e0f66ef8SJohn Baldwin 	/* Create a thread if we need one. */
603ef544f63SPaolo Pisati 	while (ie->ie_thread == NULL && handler != NULL) {
604e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD)
6050f180a7cSJohn Baldwin 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
606e0f66ef8SJohn Baldwin 		else {
607e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ADDING_THREAD;
608e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
609e0f66ef8SJohn Baldwin 			it = ithread_create("intr: newborn");
610e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
611e0f66ef8SJohn Baldwin 			ie->ie_flags &= ~IE_ADDING_THREAD;
612e0f66ef8SJohn Baldwin 			ie->ie_thread = it;
613e0f66ef8SJohn Baldwin 			it->it_event = ie;
614e0f66ef8SJohn Baldwin 			ithread_update(it);
615e0f66ef8SJohn Baldwin 			wakeup(ie);
616e0f66ef8SJohn Baldwin 		}
617e0f66ef8SJohn Baldwin 	}
618c9516c94SAlexander Kabaev 
619c9516c94SAlexander Kabaev 	/* Add the new handler to the event in priority order. */
620111b043cSAndriy Gapon 	CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
621c9516c94SAlexander Kabaev 		if (temp_ih->ih_pri > ih->ih_pri)
622c9516c94SAlexander Kabaev 			break;
623c9516c94SAlexander Kabaev 	}
624111b043cSAndriy Gapon 	CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
625111b043cSAndriy Gapon 
626c9516c94SAlexander Kabaev 	intr_event_update(ie);
627c9516c94SAlexander Kabaev 
628e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
629e0f66ef8SJohn Baldwin 	    ie->ie_name);
630e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
631e0f66ef8SJohn Baldwin 
632e0f66ef8SJohn Baldwin 	if (cookiep != NULL)
633e0f66ef8SJohn Baldwin 		*cookiep = ih;
634e0f66ef8SJohn Baldwin 	return (0);
635e0f66ef8SJohn Baldwin }
636b4151f71SJohn Baldwin 
637c3045318SJohn Baldwin /*
63837b8ef16SJohn Baldwin  * Append a description preceded by a ':' to the name of the specified
63937b8ef16SJohn Baldwin  * interrupt handler.
64037b8ef16SJohn Baldwin  */
64137b8ef16SJohn Baldwin int
64237b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie,
64337b8ef16SJohn Baldwin     const char *descr)
64437b8ef16SJohn Baldwin {
64537b8ef16SJohn Baldwin 	struct intr_handler *ih;
64637b8ef16SJohn Baldwin 	size_t space;
64737b8ef16SJohn Baldwin 	char *start;
64837b8ef16SJohn Baldwin 
64937b8ef16SJohn Baldwin 	mtx_lock(&ie->ie_lock);
65037b8ef16SJohn Baldwin #ifdef INVARIANTS
651111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
65237b8ef16SJohn Baldwin 		if (ih == cookie)
65337b8ef16SJohn Baldwin 			break;
65437b8ef16SJohn Baldwin 	}
65537b8ef16SJohn Baldwin 	if (ih == NULL) {
65637b8ef16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
657d0c9a291SJohn Baldwin 		panic("handler %p not found in interrupt event %p", cookie, ie);
65837b8ef16SJohn Baldwin 	}
65937b8ef16SJohn Baldwin #endif
66037b8ef16SJohn Baldwin 	ih = cookie;
66137b8ef16SJohn Baldwin 
66237b8ef16SJohn Baldwin 	/*
66337b8ef16SJohn Baldwin 	 * Look for an existing description by checking for an
66437b8ef16SJohn Baldwin 	 * existing ":".  This assumes device names do not include
66537b8ef16SJohn Baldwin 	 * colons.  If one is found, prepare to insert the new
66637b8ef16SJohn Baldwin 	 * description at that point.  If one is not found, find the
66737b8ef16SJohn Baldwin 	 * end of the name to use as the insertion point.
66837b8ef16SJohn Baldwin 	 */
669dc15eac0SEd Schouten 	start = strchr(ih->ih_name, ':');
67037b8ef16SJohn Baldwin 	if (start == NULL)
671dc15eac0SEd Schouten 		start = strchr(ih->ih_name, 0);
67237b8ef16SJohn Baldwin 
67337b8ef16SJohn Baldwin 	/*
67437b8ef16SJohn Baldwin 	 * See if there is enough remaining room in the string for the
67537b8ef16SJohn Baldwin 	 * description + ":".  The "- 1" leaves room for the trailing
67637b8ef16SJohn Baldwin 	 * '\0'.  The "+ 1" accounts for the colon.
67737b8ef16SJohn Baldwin 	 */
67837b8ef16SJohn Baldwin 	space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
67937b8ef16SJohn Baldwin 	if (strlen(descr) + 1 > space) {
68037b8ef16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
68137b8ef16SJohn Baldwin 		return (ENOSPC);
68237b8ef16SJohn Baldwin 	}
68337b8ef16SJohn Baldwin 
68437b8ef16SJohn Baldwin 	/* Append a colon followed by the description. */
68537b8ef16SJohn Baldwin 	*start = ':';
68637b8ef16SJohn Baldwin 	strcpy(start + 1, descr);
68737b8ef16SJohn Baldwin 	intr_event_update(ie);
68837b8ef16SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
68937b8ef16SJohn Baldwin 	return (0);
69037b8ef16SJohn Baldwin }
69137b8ef16SJohn Baldwin 
69237b8ef16SJohn Baldwin /*
693c3045318SJohn Baldwin  * Return the ie_source field from the intr_event an intr_handler is
694c3045318SJohn Baldwin  * associated with.
695c3045318SJohn Baldwin  */
696c3045318SJohn Baldwin void *
697c3045318SJohn Baldwin intr_handler_source(void *cookie)
698c3045318SJohn Baldwin {
699c3045318SJohn Baldwin 	struct intr_handler *ih;
700c3045318SJohn Baldwin 	struct intr_event *ie;
701c3045318SJohn Baldwin 
702c3045318SJohn Baldwin 	ih = (struct intr_handler *)cookie;
703c3045318SJohn Baldwin 	if (ih == NULL)
704c3045318SJohn Baldwin 		return (NULL);
705c3045318SJohn Baldwin 	ie = ih->ih_event;
706c3045318SJohn Baldwin 	KASSERT(ie != NULL,
707c3045318SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
708c3045318SJohn Baldwin 	    ih->ih_name));
709c3045318SJohn Baldwin 	return (ie->ie_source);
710c3045318SJohn Baldwin }
711c3045318SJohn Baldwin 
712e4cd31ddSJeff Roberson /*
713e0fa977eSAndriy Gapon  * If intr_event_handle() is running in the ISR context at the time of the call,
714e0fa977eSAndriy Gapon  * then wait for it to complete.
715e0fa977eSAndriy Gapon  */
716e0fa977eSAndriy Gapon static void
717e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie)
718e0fa977eSAndriy Gapon {
719e0fa977eSAndriy Gapon 	int phase;
720e0fa977eSAndriy Gapon 
721e0fa977eSAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
722e0fa977eSAndriy Gapon 	phase = ie->ie_phase;
723e0fa977eSAndriy Gapon 
724e0fa977eSAndriy Gapon 	/*
725e0fa977eSAndriy Gapon 	 * Switch phase to direct future interrupts to the other active counter.
726e0fa977eSAndriy Gapon 	 * Make sure that any preceding stores are visible before the switch.
727e0fa977eSAndriy Gapon 	 */
728e0fa977eSAndriy Gapon 	KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
729e0fa977eSAndriy Gapon 	atomic_store_rel_int(&ie->ie_phase, !phase);
730e0fa977eSAndriy Gapon 
731e0fa977eSAndriy Gapon 	/*
732e0fa977eSAndriy Gapon 	 * This code cooperates with wait-free iteration of ie_handlers
733e0fa977eSAndriy Gapon 	 * in intr_event_handle.
734e0fa977eSAndriy Gapon 	 * Make sure that the removal and the phase update are not reordered
735e0fa977eSAndriy Gapon 	 * with the active count check.
736e0fa977eSAndriy Gapon 	 * Note that no combination of acquire and release fences can provide
737e0fa977eSAndriy Gapon 	 * that guarantee as Store->Load sequences can always be reordered.
738e0fa977eSAndriy Gapon 	 */
739e0fa977eSAndriy Gapon 	atomic_thread_fence_seq_cst();
740e0fa977eSAndriy Gapon 
741e0fa977eSAndriy Gapon 	/*
742e0fa977eSAndriy Gapon 	 * Now wait on the inactive phase.
743e0fa977eSAndriy Gapon 	 * The acquire fence is needed so that that all post-barrier accesses
744e0fa977eSAndriy Gapon 	 * are after the check.
745e0fa977eSAndriy Gapon 	 */
746e0fa977eSAndriy Gapon 	while (ie->ie_active[phase] > 0)
747e0fa977eSAndriy Gapon 		cpu_spinwait();
748e0fa977eSAndriy Gapon 	atomic_thread_fence_acq();
749e0fa977eSAndriy Gapon }
750e0fa977eSAndriy Gapon 
75182a5a275SAndriy Gapon static void
75282a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler)
75382a5a275SAndriy Gapon {
75482a5a275SAndriy Gapon 	struct intr_event *ie;
75582a5a275SAndriy Gapon 
75682a5a275SAndriy Gapon 	ie = handler->ih_event;
75782a5a275SAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
75882a5a275SAndriy Gapon 	KASSERT((handler->ih_flags & IH_DEAD) == 0,
75982a5a275SAndriy Gapon 	    ("update for a removed handler"));
76082a5a275SAndriy Gapon 
76182a5a275SAndriy Gapon 	if (ie->ie_thread == NULL) {
76282a5a275SAndriy Gapon 		intr_event_barrier(ie);
76382a5a275SAndriy Gapon 		return;
76482a5a275SAndriy Gapon 	}
76582a5a275SAndriy Gapon 	if ((handler->ih_flags & IH_CHANGED) == 0) {
76682a5a275SAndriy Gapon 		handler->ih_flags |= IH_CHANGED;
76782a5a275SAndriy Gapon 		intr_event_schedule_thread(ie);
76882a5a275SAndriy Gapon 	}
76982a5a275SAndriy Gapon 	while ((handler->ih_flags & IH_CHANGED) != 0)
77082a5a275SAndriy Gapon 		msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
77182a5a275SAndriy Gapon }
77282a5a275SAndriy Gapon 
773e0fa977eSAndriy Gapon /*
774e4cd31ddSJeff Roberson  * Sleep until an ithread finishes executing an interrupt handler.
775e4cd31ddSJeff Roberson  *
776e4cd31ddSJeff Roberson  * XXX Doesn't currently handle interrupt filters or fast interrupt
777e4cd31ddSJeff Roberson  * handlers.  This is intended for compatibility with linux drivers
778e4cd31ddSJeff Roberson  * only.  Do not use in BSD code.
779e4cd31ddSJeff Roberson  */
780e4cd31ddSJeff Roberson void
781e4cd31ddSJeff Roberson _intr_drain(int irq)
782e4cd31ddSJeff Roberson {
783e4cd31ddSJeff Roberson 	struct intr_event *ie;
784e4cd31ddSJeff Roberson 	struct intr_thread *ithd;
785e4cd31ddSJeff Roberson 	struct thread *td;
786e4cd31ddSJeff Roberson 
787e4cd31ddSJeff Roberson 	ie = intr_lookup(irq);
788e4cd31ddSJeff Roberson 	if (ie == NULL)
789e4cd31ddSJeff Roberson 		return;
790e4cd31ddSJeff Roberson 	if (ie->ie_thread == NULL)
791e4cd31ddSJeff Roberson 		return;
792e4cd31ddSJeff Roberson 	ithd = ie->ie_thread;
793e4cd31ddSJeff Roberson 	td = ithd->it_thread;
7945bd186a6SJeff Roberson 	/*
7955bd186a6SJeff Roberson 	 * We set the flag and wait for it to be cleared to avoid
7965bd186a6SJeff Roberson 	 * long delays with potentially busy interrupt handlers
7975bd186a6SJeff Roberson 	 * were we to only sample TD_AWAITING_INTR() every tick.
7985bd186a6SJeff Roberson 	 */
799e4cd31ddSJeff Roberson 	thread_lock(td);
800e4cd31ddSJeff Roberson 	if (!TD_AWAITING_INTR(td)) {
801e4cd31ddSJeff Roberson 		ithd->it_flags |= IT_WAIT;
8025bd186a6SJeff Roberson 		while (ithd->it_flags & IT_WAIT) {
8035bd186a6SJeff Roberson 			thread_unlock(td);
8045bd186a6SJeff Roberson 			pause("idrain", 1);
8055bd186a6SJeff Roberson 			thread_lock(td);
806e4cd31ddSJeff Roberson 		}
8075bd186a6SJeff Roberson 	}
8085bd186a6SJeff Roberson 	thread_unlock(td);
809e4cd31ddSJeff Roberson 	return;
810e4cd31ddSJeff Roberson }
811e4cd31ddSJeff Roberson 
812b4151f71SJohn Baldwin int
813e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie)
814b4151f71SJohn Baldwin {
815e0f66ef8SJohn Baldwin 	struct intr_handler *handler = (struct intr_handler *)cookie;
816e0f66ef8SJohn Baldwin 	struct intr_event *ie;
817e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
818111b043cSAndriy Gapon 	struct intr_handler **prevptr;
819e0f66ef8SJohn Baldwin #ifdef notyet
820e0f66ef8SJohn Baldwin 	int dead;
821b4151f71SJohn Baldwin #endif
822b4151f71SJohn Baldwin 
8233e5da754SJohn Baldwin 	if (handler == NULL)
824b4151f71SJohn Baldwin 		return (EINVAL);
825e0f66ef8SJohn Baldwin 	ie = handler->ih_event;
826e0f66ef8SJohn Baldwin 	KASSERT(ie != NULL,
827e0f66ef8SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
8283e5da754SJohn Baldwin 	    handler->ih_name));
829111b043cSAndriy Gapon 
830e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
83191f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
832e0f66ef8SJohn Baldwin 	    ie->ie_name);
833111b043cSAndriy Gapon 	CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
8343e5da754SJohn Baldwin 		if (ih == handler)
835111b043cSAndriy Gapon 			break;
836111b043cSAndriy Gapon 	}
837111b043cSAndriy Gapon 	if (ih == NULL) {
838111b043cSAndriy Gapon 		panic("interrupt handler \"%s\" not found in "
839111b043cSAndriy Gapon 		    "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
840111b043cSAndriy Gapon 	}
841111b043cSAndriy Gapon 
842de271f01SJohn Baldwin 	/*
843e0fa977eSAndriy Gapon 	 * If there is no ithread, then directly remove the handler.  Note that
844e0fa977eSAndriy Gapon 	 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
845e0fa977eSAndriy Gapon 	 * care needs to be taken to keep ie_handlers consistent and to free
846e0fa977eSAndriy Gapon 	 * the removed handler only when ie_handlers is quiescent.
847e0f66ef8SJohn Baldwin 	 */
848e0f66ef8SJohn Baldwin 	if (ie->ie_thread == NULL) {
849111b043cSAndriy Gapon 		CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
850e0fa977eSAndriy Gapon 		intr_event_barrier(ie);
851e0fa977eSAndriy Gapon 		intr_event_update(ie);
852e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
853e0f66ef8SJohn Baldwin 		free(handler, M_ITHREAD);
854e0f66ef8SJohn Baldwin 		return (0);
855e0f66ef8SJohn Baldwin 	}
856e0f66ef8SJohn Baldwin 
857e0f66ef8SJohn Baldwin 	/*
858e0fa977eSAndriy Gapon 	 * Let the interrupt thread do the job.
859e0fa977eSAndriy Gapon 	 * The interrupt source is disabled when the interrupt thread is
860e0fa977eSAndriy Gapon 	 * running, so it does not have to worry about interaction with
861e0fa977eSAndriy Gapon 	 * intr_event_handle().
862de271f01SJohn Baldwin 	 */
863e0fa977eSAndriy Gapon 	KASSERT((handler->ih_flags & IH_DEAD) == 0,
864e0fa977eSAndriy Gapon 	    ("duplicate handle remove"));
865de271f01SJohn Baldwin 	handler->ih_flags |= IH_DEAD;
866e0fa977eSAndriy Gapon 	intr_event_schedule_thread(ie);
867e0f66ef8SJohn Baldwin 	while (handler->ih_flags & IH_DEAD)
8680f180a7cSJohn Baldwin 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
869e0f66ef8SJohn Baldwin 	intr_event_update(ie);
870111b043cSAndriy Gapon 
871e0f66ef8SJohn Baldwin #ifdef notyet
872e0f66ef8SJohn Baldwin 	/*
873e0f66ef8SJohn Baldwin 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
874e0f66ef8SJohn Baldwin 	 * this could lead to races of stale data when servicing an
875e0f66ef8SJohn Baldwin 	 * interrupt.
876e0f66ef8SJohn Baldwin 	 */
877e0f66ef8SJohn Baldwin 	dead = 1;
878111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
879111b043cSAndriy Gapon 		if (ih->ih_handler != NULL) {
880e0f66ef8SJohn Baldwin 			dead = 0;
881e0f66ef8SJohn Baldwin 			break;
882e0f66ef8SJohn Baldwin 		}
883e0f66ef8SJohn Baldwin 	}
884e0f66ef8SJohn Baldwin 	if (dead) {
885e0f66ef8SJohn Baldwin 		ithread_destroy(ie->ie_thread);
886e0f66ef8SJohn Baldwin 		ie->ie_thread = NULL;
887e0f66ef8SJohn Baldwin 	}
888e0f66ef8SJohn Baldwin #endif
889e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
890b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
891b4151f71SJohn Baldwin 	return (0);
892b4151f71SJohn Baldwin }
893b4151f71SJohn Baldwin 
89482a5a275SAndriy Gapon int
89582a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie)
89682a5a275SAndriy Gapon {
89782a5a275SAndriy Gapon 	struct intr_handler *handler = (struct intr_handler *)cookie;
89882a5a275SAndriy Gapon 	struct intr_event *ie;
89982a5a275SAndriy Gapon 
90082a5a275SAndriy Gapon 	if (handler == NULL)
90182a5a275SAndriy Gapon 		return (EINVAL);
90282a5a275SAndriy Gapon 	ie = handler->ih_event;
90382a5a275SAndriy Gapon 	KASSERT(ie != NULL,
90482a5a275SAndriy Gapon 	    ("interrupt handler \"%s\" has a NULL interrupt event",
90582a5a275SAndriy Gapon 	    handler->ih_name));
90682a5a275SAndriy Gapon 	mtx_lock(&ie->ie_lock);
90782a5a275SAndriy Gapon 	handler->ih_flags |= IH_SUSP;
90882a5a275SAndriy Gapon 	intr_handler_barrier(handler);
90982a5a275SAndriy Gapon 	mtx_unlock(&ie->ie_lock);
91082a5a275SAndriy Gapon 	return (0);
91182a5a275SAndriy Gapon }
91282a5a275SAndriy Gapon 
91382a5a275SAndriy Gapon int
91482a5a275SAndriy Gapon intr_event_resume_handler(void *cookie)
91582a5a275SAndriy Gapon {
91682a5a275SAndriy Gapon 	struct intr_handler *handler = (struct intr_handler *)cookie;
91782a5a275SAndriy Gapon 	struct intr_event *ie;
91882a5a275SAndriy Gapon 
91982a5a275SAndriy Gapon 	if (handler == NULL)
92082a5a275SAndriy Gapon 		return (EINVAL);
92182a5a275SAndriy Gapon 	ie = handler->ih_event;
92282a5a275SAndriy Gapon 	KASSERT(ie != NULL,
92382a5a275SAndriy Gapon 	    ("interrupt handler \"%s\" has a NULL interrupt event",
92482a5a275SAndriy Gapon 	    handler->ih_name));
92582a5a275SAndriy Gapon 
92682a5a275SAndriy Gapon 	/*
92782a5a275SAndriy Gapon 	 * intr_handler_barrier() acts not only as a barrier,
92882a5a275SAndriy Gapon 	 * it also allows to check for any pending interrupts.
92982a5a275SAndriy Gapon 	 */
93082a5a275SAndriy Gapon 	mtx_lock(&ie->ie_lock);
93182a5a275SAndriy Gapon 	handler->ih_flags &= ~IH_SUSP;
93282a5a275SAndriy Gapon 	intr_handler_barrier(handler);
93382a5a275SAndriy Gapon 	mtx_unlock(&ie->ie_lock);
93482a5a275SAndriy Gapon 	return (0);
93582a5a275SAndriy Gapon }
93682a5a275SAndriy Gapon 
9371ee1b687SJohn Baldwin static int
938e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie)
9393e5da754SJohn Baldwin {
940e0f66ef8SJohn Baldwin 	struct intr_entropy entropy;
941e0f66ef8SJohn Baldwin 	struct intr_thread *it;
942b40ce416SJulian Elischer 	struct thread *td;
94304774f23SJulian Elischer 	struct thread *ctd;
9443e5da754SJohn Baldwin 
9453e5da754SJohn Baldwin 	/*
9463e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
9473e5da754SJohn Baldwin 	 */
948111b043cSAndriy Gapon 	if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
949e0f66ef8SJohn Baldwin 	    ie->ie_thread == NULL)
9503e5da754SJohn Baldwin 		return (EINVAL);
9513e5da754SJohn Baldwin 
95204774f23SJulian Elischer 	ctd = curthread;
953e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
954e0f66ef8SJohn Baldwin 	td = it->it_thread;
955e0f66ef8SJohn Baldwin 
9563e5da754SJohn Baldwin 	/*
9573e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
9583e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
9593e5da754SJohn Baldwin 	 */
960*c4eb6630SGleb Smirnoff 	if (ie->ie_hflags & IH_ENTROPY) {
961e0f66ef8SJohn Baldwin 		entropy.event = (uintptr_t)ie;
962e0f66ef8SJohn Baldwin 		entropy.td = ctd;
96319fa89e9SMark Murray 		random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
9643e5da754SJohn Baldwin 	}
9653e5da754SJohn Baldwin 
966ba3f7276SMatt Macy 	KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
9673e5da754SJohn Baldwin 
9683e5da754SJohn Baldwin 	/*
9693e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
970982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
971982d11f8SJeff Roberson 	 * put it on the runqueue.
972283dfee9SKonstantin Belousov 	 *
973283dfee9SKonstantin Belousov 	 * Use store_rel to arrange that the store to ih_need in
974283dfee9SKonstantin Belousov 	 * swi_sched() is before the store to it_need and prepare for
975283dfee9SKonstantin Belousov 	 * transfer of this order to loads in the ithread.
9763e5da754SJohn Baldwin 	 */
9773eebd44dSAlfred Perlstein 	atomic_store_rel_int(&it->it_need, 1);
978982d11f8SJeff Roberson 	thread_lock(td);
97971fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
980fc2e87beSMatt Macy 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
9817ab24ea3SJulian Elischer 		    td->td_name);
98271fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
983f0393f06SJeff Roberson 		sched_add(td, SRQ_INTR);
9843e5da754SJohn Baldwin 	} else {
985e0f66ef8SJohn Baldwin 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
986fc2e87beSMatt Macy 		    __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
987982d11f8SJeff Roberson 		thread_unlock(td);
98861a74c5cSJeff Roberson 	}
9893e5da754SJohn Baldwin 
9903e5da754SJohn Baldwin 	return (0);
9913e5da754SJohn Baldwin }
9923e5da754SJohn Baldwin 
993fe486a37SJohn Baldwin /*
994e84bcd84SRobert Watson  * Allow interrupt event binding for software interrupt handlers -- a no-op,
995e84bcd84SRobert Watson  * since interrupts are generated in software rather than being directed by
996e84bcd84SRobert Watson  * a PIC.
997e84bcd84SRobert Watson  */
998e84bcd84SRobert Watson static int
999066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu)
1000e84bcd84SRobert Watson {
1001e84bcd84SRobert Watson 
1002e84bcd84SRobert Watson 	return (0);
1003e84bcd84SRobert Watson }
1004e84bcd84SRobert Watson 
1005e84bcd84SRobert Watson /*
1006fe486a37SJohn Baldwin  * Add a software interrupt handler to a specified event.  If a given event
1007fe486a37SJohn Baldwin  * is not specified, then a new event is created.
1008fe486a37SJohn Baldwin  */
10093e5da754SJohn Baldwin int
1010e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1011b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
10128088699fSJohn Baldwin {
1013e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1014b4151f71SJohn Baldwin 	int error;
10158088699fSJohn Baldwin 
1016bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
10173e5da754SJohn Baldwin 		return (EINVAL);
10183e5da754SJohn Baldwin 
1019e0f66ef8SJohn Baldwin 	ie = (eventp != NULL) ? *eventp : NULL;
10208088699fSJohn Baldwin 
1021e0f66ef8SJohn Baldwin 	if (ie != NULL) {
1022e0f66ef8SJohn Baldwin 		if (!(ie->ie_flags & IE_SOFT))
10233e5da754SJohn Baldwin 			return (EINVAL);
10243e5da754SJohn Baldwin 	} else {
10259b33b154SJeff Roberson 		error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1026e84bcd84SRobert Watson 		    NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
10278088699fSJohn Baldwin 		if (error)
1028b4151f71SJohn Baldwin 			return (error);
1029e0f66ef8SJohn Baldwin 		if (eventp != NULL)
1030e0f66ef8SJohn Baldwin 			*eventp = ie;
10318088699fSJohn Baldwin 	}
10328d809d50SJeff Roberson 	error = intr_event_add_handler(ie, name, NULL, handler, arg,
1033d3305205SJohn Baldwin 	    PI_SWI(pri), flags, cookiep);
10348d809d50SJeff Roberson 	return (error);
10358088699fSJohn Baldwin }
10368088699fSJohn Baldwin 
10371931cf94SJohn Baldwin /*
1038e0f66ef8SJohn Baldwin  * Schedule a software interrupt thread.
10391931cf94SJohn Baldwin  */
10401931cf94SJohn Baldwin void
1041b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
10421931cf94SJohn Baldwin {
1043e0f66ef8SJohn Baldwin 	struct intr_handler *ih = (struct intr_handler *)cookie;
1044e0f66ef8SJohn Baldwin 	struct intr_event *ie = ih->ih_event;
1045d95dca1dSJohn Baldwin 	struct intr_entropy entropy;
1046ba3f7276SMatt Macy 	int error __unused;
10478088699fSJohn Baldwin 
1048e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1049e0f66ef8SJohn Baldwin 	    ih->ih_need);
10501931cf94SJohn Baldwin 
1051d95dca1dSJohn Baldwin 	entropy.event = (uintptr_t)ih;
1052d95dca1dSJohn Baldwin 	entropy.td = curthread;
105319fa89e9SMark Murray 	random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1054d95dca1dSJohn Baldwin 
10551931cf94SJohn Baldwin 	/*
10563e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
10573e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
10583e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
10591931cf94SJohn Baldwin 	 */
1060283dfee9SKonstantin Belousov 	ih->ih_need = 1;
10611ca2c018SBruce Evans 
1062b4151f71SJohn Baldwin 	if (!(flags & SWI_DELAY)) {
106383c9dea1SGleb Smirnoff 		VM_CNT_INC(v_soft);
1064e0f66ef8SJohn Baldwin 		error = intr_event_schedule_thread(ie);
10653e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
10668088699fSJohn Baldwin 	}
10678088699fSJohn Baldwin }
10688088699fSJohn Baldwin 
1069fe486a37SJohn Baldwin /*
1070fe486a37SJohn Baldwin  * Remove a software interrupt handler.  Currently this code does not
1071fe486a37SJohn Baldwin  * remove the associated interrupt event if it becomes empty.  Calling code
1072fe486a37SJohn Baldwin  * may do so manually via intr_event_destroy(), but that's not really
1073fe486a37SJohn Baldwin  * an optimal interface.
1074fe486a37SJohn Baldwin  */
1075fe486a37SJohn Baldwin int
1076fe486a37SJohn Baldwin swi_remove(void *cookie)
1077fe486a37SJohn Baldwin {
1078fe486a37SJohn Baldwin 
1079fe486a37SJohn Baldwin 	return (intr_event_remove_handler(cookie));
1080fe486a37SJohn Baldwin }
1081fe486a37SJohn Baldwin 
1082111b043cSAndriy Gapon static void
108337e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1084e0f66ef8SJohn Baldwin {
1085111b043cSAndriy Gapon 	struct intr_handler *ih, *ihn, *ihp;
1086e0f66ef8SJohn Baldwin 
1087111b043cSAndriy Gapon 	ihp = NULL;
1088111b043cSAndriy Gapon 	CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1089e0f66ef8SJohn Baldwin 		/*
1090e0f66ef8SJohn Baldwin 		 * If this handler is marked for death, remove it from
1091e0f66ef8SJohn Baldwin 		 * the list of handlers and wake up the sleeper.
1092e0f66ef8SJohn Baldwin 		 */
1093e0f66ef8SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
1094e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
1095111b043cSAndriy Gapon 			if (ihp == NULL)
1096111b043cSAndriy Gapon 				CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1097111b043cSAndriy Gapon 			else
1098111b043cSAndriy Gapon 				CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1099e0f66ef8SJohn Baldwin 			ih->ih_flags &= ~IH_DEAD;
1100e0f66ef8SJohn Baldwin 			wakeup(ih);
1101e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
1102e0f66ef8SJohn Baldwin 			continue;
1103e0f66ef8SJohn Baldwin 		}
1104e0f66ef8SJohn Baldwin 
1105111b043cSAndriy Gapon 		/*
1106111b043cSAndriy Gapon 		 * Now that we know that the current element won't be removed
1107111b043cSAndriy Gapon 		 * update the previous element.
1108111b043cSAndriy Gapon 		 */
1109111b043cSAndriy Gapon 		ihp = ih;
1110111b043cSAndriy Gapon 
111182a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_CHANGED) != 0) {
111282a5a275SAndriy Gapon 			mtx_lock(&ie->ie_lock);
111382a5a275SAndriy Gapon 			ih->ih_flags &= ~IH_CHANGED;
111482a5a275SAndriy Gapon 			wakeup(ih);
111582a5a275SAndriy Gapon 			mtx_unlock(&ie->ie_lock);
111682a5a275SAndriy Gapon 		}
111782a5a275SAndriy Gapon 
1118f2d619c8SPaolo Pisati 		/* Skip filter only handlers */
1119f2d619c8SPaolo Pisati 		if (ih->ih_handler == NULL)
1120f2d619c8SPaolo Pisati 			continue;
1121f2d619c8SPaolo Pisati 
112282a5a275SAndriy Gapon 		/* Skip suspended handlers */
112382a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_SUSP) != 0)
112482a5a275SAndriy Gapon 			continue;
112582a5a275SAndriy Gapon 
1126e0f66ef8SJohn Baldwin 		/*
1127e0f66ef8SJohn Baldwin 		 * For software interrupt threads, we only execute
1128e0f66ef8SJohn Baldwin 		 * handlers that have their need flag set.  Hardware
1129e0f66ef8SJohn Baldwin 		 * interrupt threads always invoke all of their handlers.
11301b79b949SKirk McKusick 		 *
11311b79b949SKirk McKusick 		 * ih_need can only be 0 or 1.  Failed cmpset below
11321b79b949SKirk McKusick 		 * means that there is no request to execute handlers,
11331b79b949SKirk McKusick 		 * so a retry of the cmpset is not needed.
1134e0f66ef8SJohn Baldwin 		 */
11351b79b949SKirk McKusick 		if ((ie->ie_flags & IE_SOFT) != 0 &&
11361b79b949SKirk McKusick 		    atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1137e0f66ef8SJohn Baldwin 			continue;
1138e0f66ef8SJohn Baldwin 
1139e0f66ef8SJohn Baldwin 		/* Execute this handler. */
1140e0f66ef8SJohn Baldwin 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1141bafe5a31SPaolo Pisati 		    __func__, p->p_pid, (void *)ih->ih_handler,
1142bafe5a31SPaolo Pisati 		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1143e0f66ef8SJohn Baldwin 
1144e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1145e0f66ef8SJohn Baldwin 			mtx_lock(&Giant);
1146e0f66ef8SJohn Baldwin 		ih->ih_handler(ih->ih_argument);
1147e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1148e0f66ef8SJohn Baldwin 			mtx_unlock(&Giant);
1149e0f66ef8SJohn Baldwin 	}
115037e9511fSJohn Baldwin }
115137e9511fSJohn Baldwin 
115237e9511fSJohn Baldwin static void
115337e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie)
115437e9511fSJohn Baldwin {
115537e9511fSJohn Baldwin 
115637e9511fSJohn Baldwin 	/* Interrupt handlers should not sleep. */
115737e9511fSJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
115837e9511fSJohn Baldwin 		THREAD_NO_SLEEPING();
115937e9511fSJohn Baldwin 	intr_event_execute_handlers(p, ie);
1160e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
1161e0f66ef8SJohn Baldwin 		THREAD_SLEEPING_OK();
1162e0f66ef8SJohn Baldwin 
1163e0f66ef8SJohn Baldwin 	/*
1164e0f66ef8SJohn Baldwin 	 * Interrupt storm handling:
1165e0f66ef8SJohn Baldwin 	 *
1166e0f66ef8SJohn Baldwin 	 * If this interrupt source is currently storming, then throttle
1167e0f66ef8SJohn Baldwin 	 * it to only fire the handler once  per clock tick.
1168e0f66ef8SJohn Baldwin 	 *
1169e0f66ef8SJohn Baldwin 	 * If this interrupt source is not currently storming, but the
1170e0f66ef8SJohn Baldwin 	 * number of back to back interrupts exceeds the storm threshold,
1171e0f66ef8SJohn Baldwin 	 * then enter storming mode.
1172e0f66ef8SJohn Baldwin 	 */
1173e41bcf3cSJohn Baldwin 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1174e41bcf3cSJohn Baldwin 	    !(ie->ie_flags & IE_SOFT)) {
11750ae62c18SNate Lawson 		/* Report the message only once every second. */
11760ae62c18SNate Lawson 		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1177e0f66ef8SJohn Baldwin 			printf(
11780ae62c18SNate Lawson 	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1179e0f66ef8SJohn Baldwin 			    ie->ie_name);
1180e0f66ef8SJohn Baldwin 		}
1181e41bcf3cSJohn Baldwin 		pause("istorm", 1);
1182e0f66ef8SJohn Baldwin 	} else
1183e0f66ef8SJohn Baldwin 		ie->ie_count++;
1184e0f66ef8SJohn Baldwin 
1185e0f66ef8SJohn Baldwin 	/*
1186e0f66ef8SJohn Baldwin 	 * Now that all the handlers have had a chance to run, reenable
1187e0f66ef8SJohn Baldwin 	 * the interrupt source.
1188e0f66ef8SJohn Baldwin 	 */
11891ee1b687SJohn Baldwin 	if (ie->ie_post_ithread != NULL)
11901ee1b687SJohn Baldwin 		ie->ie_post_ithread(ie->ie_source);
1191e0f66ef8SJohn Baldwin }
1192e0f66ef8SJohn Baldwin 
11938088699fSJohn Baldwin /*
1194b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
11958088699fSJohn Baldwin  */
119637c84183SPoul-Henning Kamp static void
1197b4151f71SJohn Baldwin ithread_loop(void *arg)
11988088699fSJohn Baldwin {
1199e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
1200e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1201b40ce416SJulian Elischer 	struct thread *td;
1202b4151f71SJohn Baldwin 	struct proc *p;
1203e4cd31ddSJeff Roberson 	int wake;
12048088699fSJohn Baldwin 
1205b40ce416SJulian Elischer 	td = curthread;
1206b40ce416SJulian Elischer 	p = td->td_proc;
1207e0f66ef8SJohn Baldwin 	ithd = (struct intr_thread *)arg;
1208e0f66ef8SJohn Baldwin 	KASSERT(ithd->it_thread == td,
120991f91617SDavid E. O'Brien 	    ("%s: ithread and proc linkage out of sync", __func__));
1210e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
1211e0f66ef8SJohn Baldwin 	ie->ie_count = 0;
1212e4cd31ddSJeff Roberson 	wake = 0;
12138088699fSJohn Baldwin 
12148088699fSJohn Baldwin 	/*
12158088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
12168088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
12178088699fSJohn Baldwin 	 */
12188088699fSJohn Baldwin 	for (;;) {
1219b4151f71SJohn Baldwin 		/*
1220b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
1221b4151f71SJohn Baldwin 		 */
1222b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
1223e0f66ef8SJohn Baldwin 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
12247ab24ea3SJulian Elischer 			    p->p_pid, td->td_name);
1225b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
1226ca9a0ddfSJulian Elischer 			kthread_exit();
1227b4151f71SJohn Baldwin 		}
1228b4151f71SJohn Baldwin 
1229e0f66ef8SJohn Baldwin 		/*
1230e0f66ef8SJohn Baldwin 		 * Service interrupts.  If another interrupt arrives while
1231e0f66ef8SJohn Baldwin 		 * we are running, it will set it_need to note that we
1232e0f66ef8SJohn Baldwin 		 * should make another pass.
1233283dfee9SKonstantin Belousov 		 *
1234283dfee9SKonstantin Belousov 		 * The load_acq part of the following cmpset ensures
1235283dfee9SKonstantin Belousov 		 * that the load of ih_need in ithread_execute_handlers()
1236283dfee9SKonstantin Belousov 		 * is ordered after the load of it_need here.
1237e0f66ef8SJohn Baldwin 		 */
1238283dfee9SKonstantin Belousov 		while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0)
1239e0f66ef8SJohn Baldwin 			ithread_execute_handlers(p, ie);
12407870c3c6SJohn Baldwin 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
12417870c3c6SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
12428088699fSJohn Baldwin 
12438088699fSJohn Baldwin 		/*
12448088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
12458088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
12468088699fSJohn Baldwin 		 * set again, so we have to check it again.
12478088699fSJohn Baldwin 		 */
1248982d11f8SJeff Roberson 		thread_lock(td);
124903bbcb2fSKonstantin Belousov 		if (atomic_load_acq_int(&ithd->it_need) == 0 &&
125003bbcb2fSKonstantin Belousov 		    (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
12517870c3c6SJohn Baldwin 			TD_SET_IWAIT(td);
1252e0f66ef8SJohn Baldwin 			ie->ie_count = 0;
1253686bcb5cSJeff Roberson 			mi_switch(SW_VOL | SWT_IWAIT);
1254686bcb5cSJeff Roberson 		} else {
1255e4cd31ddSJeff Roberson 			if (ithd->it_flags & IT_WAIT) {
1256e4cd31ddSJeff Roberson 				wake = 1;
1257e4cd31ddSJeff Roberson 				ithd->it_flags &= ~IT_WAIT;
1258e4cd31ddSJeff Roberson 			}
1259982d11f8SJeff Roberson 			thread_unlock(td);
1260686bcb5cSJeff Roberson 		}
1261e4cd31ddSJeff Roberson 		if (wake) {
1262e4cd31ddSJeff Roberson 			wakeup(ithd);
1263e4cd31ddSJeff Roberson 			wake = 0;
1264e4cd31ddSJeff Roberson 		}
12658088699fSJohn Baldwin 	}
12661931cf94SJohn Baldwin }
12671ee1b687SJohn Baldwin 
12681ee1b687SJohn Baldwin /*
12691ee1b687SJohn Baldwin  * Main interrupt handling body.
12701ee1b687SJohn Baldwin  *
12711ee1b687SJohn Baldwin  * Input:
12721ee1b687SJohn Baldwin  * o ie:                        the event connected to this interrupt.
12731ee1b687SJohn Baldwin  * o frame:                     some archs (i.e. i386) pass a frame to some.
12741ee1b687SJohn Baldwin  *                              handlers as their main argument.
12751ee1b687SJohn Baldwin  * Return value:
12761ee1b687SJohn Baldwin  * o 0:                         everything ok.
12771ee1b687SJohn Baldwin  * o EINVAL:                    stray interrupt.
12781ee1b687SJohn Baldwin  */
12791ee1b687SJohn Baldwin int
12801ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame)
12811ee1b687SJohn Baldwin {
12821ee1b687SJohn Baldwin 	struct intr_handler *ih;
12831f255bd3SAlexander Motin 	struct trapframe *oldframe;
12841ee1b687SJohn Baldwin 	struct thread *td;
1285e0fa977eSAndriy Gapon 	int phase;
128682a5a275SAndriy Gapon 	int ret;
128782a5a275SAndriy Gapon 	bool filter, thread;
12881ee1b687SJohn Baldwin 
12891ee1b687SJohn Baldwin 	td = curthread;
12901ee1b687SJohn Baldwin 
1291b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF
1292b7627840SKonstantin Belousov 	intr_prof_stack_use(td, frame);
1293b7627840SKonstantin Belousov #endif
1294b7627840SKonstantin Belousov 
12951ee1b687SJohn Baldwin 	/* An interrupt with no event or handlers is a stray interrupt. */
1296111b043cSAndriy Gapon 	if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
12971ee1b687SJohn Baldwin 		return (EINVAL);
12981ee1b687SJohn Baldwin 
12991ee1b687SJohn Baldwin 	/*
13001ee1b687SJohn Baldwin 	 * Execute fast interrupt handlers directly.
13011ee1b687SJohn Baldwin 	 * To support clock handlers, if a handler registers
13021ee1b687SJohn Baldwin 	 * with a NULL argument, then we pass it a pointer to
13031ee1b687SJohn Baldwin 	 * a trapframe as its argument.
13041ee1b687SJohn Baldwin 	 */
13051ee1b687SJohn Baldwin 	td->td_intr_nesting_level++;
130682a5a275SAndriy Gapon 	filter = false;
130782a5a275SAndriy Gapon 	thread = false;
13081ee1b687SJohn Baldwin 	ret = 0;
13091ee1b687SJohn Baldwin 	critical_enter();
13101f255bd3SAlexander Motin 	oldframe = td->td_intr_frame;
13111f255bd3SAlexander Motin 	td->td_intr_frame = frame;
1312111b043cSAndriy Gapon 
1313e0fa977eSAndriy Gapon 	phase = ie->ie_phase;
1314e0fa977eSAndriy Gapon 	atomic_add_int(&ie->ie_active[phase], 1);
1315e0fa977eSAndriy Gapon 
1316e0fa977eSAndriy Gapon 	/*
1317e0fa977eSAndriy Gapon 	 * This fence is required to ensure that no later loads are
1318e0fa977eSAndriy Gapon 	 * re-ordered before the ie_active store.
1319e0fa977eSAndriy Gapon 	 */
1320e0fa977eSAndriy Gapon 	atomic_thread_fence_seq_cst();
1321e0fa977eSAndriy Gapon 
1322111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
132382a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_SUSP) != 0)
132482a5a275SAndriy Gapon 			continue;
13251ee1b687SJohn Baldwin 		if (ih->ih_filter == NULL) {
132682a5a275SAndriy Gapon 			thread = true;
13271ee1b687SJohn Baldwin 			continue;
13281ee1b687SJohn Baldwin 		}
13291ee1b687SJohn Baldwin 		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
13301ee1b687SJohn Baldwin 		    ih->ih_filter, ih->ih_argument == NULL ? frame :
13311ee1b687SJohn Baldwin 		    ih->ih_argument, ih->ih_name);
13321ee1b687SJohn Baldwin 		if (ih->ih_argument == NULL)
13331ee1b687SJohn Baldwin 			ret = ih->ih_filter(frame);
13341ee1b687SJohn Baldwin 		else
13351ee1b687SJohn Baldwin 			ret = ih->ih_filter(ih->ih_argument);
133689fc20ccSAndriy Gapon 		KASSERT(ret == FILTER_STRAY ||
133789fc20ccSAndriy Gapon 		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
133889fc20ccSAndriy Gapon 		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
133989fc20ccSAndriy Gapon 		    ("%s: incorrect return value %#x from %s", __func__, ret,
134089fc20ccSAndriy Gapon 		    ih->ih_name));
134182a5a275SAndriy Gapon 		filter = filter || ret == FILTER_HANDLED;
134289fc20ccSAndriy Gapon 
13431ee1b687SJohn Baldwin 		/*
13441ee1b687SJohn Baldwin 		 * Wrapper handler special handling:
13451ee1b687SJohn Baldwin 		 *
13461ee1b687SJohn Baldwin 		 * in some particular cases (like pccard and pccbb),
13471ee1b687SJohn Baldwin 		 * the _real_ device handler is wrapped in a couple of
13481ee1b687SJohn Baldwin 		 * functions - a filter wrapper and an ithread wrapper.
13491ee1b687SJohn Baldwin 		 * In this case (and just in this case), the filter wrapper
13501ee1b687SJohn Baldwin 		 * could ask the system to schedule the ithread and mask
13511ee1b687SJohn Baldwin 		 * the interrupt source if the wrapped handler is composed
13521ee1b687SJohn Baldwin 		 * of just an ithread handler.
13531ee1b687SJohn Baldwin 		 *
13541ee1b687SJohn Baldwin 		 * TODO: write a generic wrapper to avoid people rolling
135582a5a275SAndriy Gapon 		 * their own.
13561ee1b687SJohn Baldwin 		 */
13571ee1b687SJohn Baldwin 		if (!thread) {
13581ee1b687SJohn Baldwin 			if (ret == FILTER_SCHEDULE_THREAD)
135982a5a275SAndriy Gapon 				thread = true;
13601ee1b687SJohn Baldwin 		}
13611ee1b687SJohn Baldwin 	}
1362e0fa977eSAndriy Gapon 	atomic_add_rel_int(&ie->ie_active[phase], -1);
1363e0fa977eSAndriy Gapon 
13641f255bd3SAlexander Motin 	td->td_intr_frame = oldframe;
13651ee1b687SJohn Baldwin 
13661ee1b687SJohn Baldwin 	if (thread) {
13671ee1b687SJohn Baldwin 		if (ie->ie_pre_ithread != NULL)
13681ee1b687SJohn Baldwin 			ie->ie_pre_ithread(ie->ie_source);
13691ee1b687SJohn Baldwin 	} else {
13701ee1b687SJohn Baldwin 		if (ie->ie_post_filter != NULL)
13711ee1b687SJohn Baldwin 			ie->ie_post_filter(ie->ie_source);
13721ee1b687SJohn Baldwin 	}
13731ee1b687SJohn Baldwin 
13741ee1b687SJohn Baldwin 	/* Schedule the ithread if needed. */
13751ee1b687SJohn Baldwin 	if (thread) {
1376ba3f7276SMatt Macy 		int error __unused;
1377ba3f7276SMatt Macy 
13781ee1b687SJohn Baldwin 		error =  intr_event_schedule_thread(ie);
13791ee1b687SJohn Baldwin 		KASSERT(error == 0, ("bad stray interrupt"));
13801ee1b687SJohn Baldwin 	}
13811ee1b687SJohn Baldwin 	critical_exit();
13821ee1b687SJohn Baldwin 	td->td_intr_nesting_level--;
138382a5a275SAndriy Gapon #ifdef notyet
138482a5a275SAndriy Gapon 	/* The interrupt is not aknowledged by any filter and has no ithread. */
138582a5a275SAndriy Gapon 	if (!thread && !filter)
138682a5a275SAndriy Gapon 		return (EINVAL);
138782a5a275SAndriy Gapon #endif
13881ee1b687SJohn Baldwin 	return (0);
13891ee1b687SJohn Baldwin }
13901931cf94SJohn Baldwin 
13918b201c42SJohn Baldwin #ifdef DDB
13928b201c42SJohn Baldwin /*
13938b201c42SJohn Baldwin  * Dump details about an interrupt handler
13948b201c42SJohn Baldwin  */
13958b201c42SJohn Baldwin static void
1396e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih)
13978b201c42SJohn Baldwin {
13988b201c42SJohn Baldwin 	int comma;
13998b201c42SJohn Baldwin 
14008b201c42SJohn Baldwin 	db_printf("\t%-10s ", ih->ih_name);
14018b201c42SJohn Baldwin 	switch (ih->ih_pri) {
14028b201c42SJohn Baldwin 	case PI_REALTIME:
14038b201c42SJohn Baldwin 		db_printf("CLK ");
14048b201c42SJohn Baldwin 		break;
14058b201c42SJohn Baldwin 	case PI_AV:
14068b201c42SJohn Baldwin 		db_printf("AV  ");
14078b201c42SJohn Baldwin 		break;
1408d3305205SJohn Baldwin 	case PI_TTY:
14098b201c42SJohn Baldwin 		db_printf("TTY ");
14108b201c42SJohn Baldwin 		break;
14118b201c42SJohn Baldwin 	case PI_NET:
14128b201c42SJohn Baldwin 		db_printf("NET ");
14138b201c42SJohn Baldwin 		break;
14148b201c42SJohn Baldwin 	case PI_DISK:
14158b201c42SJohn Baldwin 		db_printf("DISK");
14168b201c42SJohn Baldwin 		break;
14178b201c42SJohn Baldwin 	case PI_DULL:
14188b201c42SJohn Baldwin 		db_printf("DULL");
14198b201c42SJohn Baldwin 		break;
14208b201c42SJohn Baldwin 	default:
14218b201c42SJohn Baldwin 		if (ih->ih_pri >= PI_SOFT)
14228b201c42SJohn Baldwin 			db_printf("SWI ");
14238b201c42SJohn Baldwin 		else
14248b201c42SJohn Baldwin 			db_printf("%4u", ih->ih_pri);
14258b201c42SJohn Baldwin 		break;
14268b201c42SJohn Baldwin 	}
14278b201c42SJohn Baldwin 	db_printf(" ");
1428b887a155SKonstantin Belousov 	if (ih->ih_filter != NULL) {
1429b887a155SKonstantin Belousov 		db_printf("[F]");
1430b887a155SKonstantin Belousov 		db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1431b887a155SKonstantin Belousov 	}
1432b887a155SKonstantin Belousov 	if (ih->ih_handler != NULL) {
1433b887a155SKonstantin Belousov 		if (ih->ih_filter != NULL)
1434b887a155SKonstantin Belousov 			db_printf(",");
1435b887a155SKonstantin Belousov 		db_printf("[H]");
14368b201c42SJohn Baldwin 		db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1437b887a155SKonstantin Belousov 	}
14388b201c42SJohn Baldwin 	db_printf("(%p)", ih->ih_argument);
14398b201c42SJohn Baldwin 	if (ih->ih_need ||
1440ef544f63SPaolo Pisati 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
14418b201c42SJohn Baldwin 	    IH_MPSAFE)) != 0) {
14428b201c42SJohn Baldwin 		db_printf(" {");
14438b201c42SJohn Baldwin 		comma = 0;
14448b201c42SJohn Baldwin 		if (ih->ih_flags & IH_EXCLUSIVE) {
14458b201c42SJohn Baldwin 			if (comma)
14468b201c42SJohn Baldwin 				db_printf(", ");
14478b201c42SJohn Baldwin 			db_printf("EXCL");
14488b201c42SJohn Baldwin 			comma = 1;
14498b201c42SJohn Baldwin 		}
14508b201c42SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY) {
14518b201c42SJohn Baldwin 			if (comma)
14528b201c42SJohn Baldwin 				db_printf(", ");
14538b201c42SJohn Baldwin 			db_printf("ENTROPY");
14548b201c42SJohn Baldwin 			comma = 1;
14558b201c42SJohn Baldwin 		}
14568b201c42SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
14578b201c42SJohn Baldwin 			if (comma)
14588b201c42SJohn Baldwin 				db_printf(", ");
14598b201c42SJohn Baldwin 			db_printf("DEAD");
14608b201c42SJohn Baldwin 			comma = 1;
14618b201c42SJohn Baldwin 		}
14628b201c42SJohn Baldwin 		if (ih->ih_flags & IH_MPSAFE) {
14638b201c42SJohn Baldwin 			if (comma)
14648b201c42SJohn Baldwin 				db_printf(", ");
14658b201c42SJohn Baldwin 			db_printf("MPSAFE");
14668b201c42SJohn Baldwin 			comma = 1;
14678b201c42SJohn Baldwin 		}
14688b201c42SJohn Baldwin 		if (ih->ih_need) {
14698b201c42SJohn Baldwin 			if (comma)
14708b201c42SJohn Baldwin 				db_printf(", ");
14718b201c42SJohn Baldwin 			db_printf("NEED");
14728b201c42SJohn Baldwin 		}
14738b201c42SJohn Baldwin 		db_printf("}");
14748b201c42SJohn Baldwin 	}
14758b201c42SJohn Baldwin 	db_printf("\n");
14768b201c42SJohn Baldwin }
14778b201c42SJohn Baldwin 
14788b201c42SJohn Baldwin /*
1479e0f66ef8SJohn Baldwin  * Dump details about a event.
14808b201c42SJohn Baldwin  */
14818b201c42SJohn Baldwin void
1482e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers)
14838b201c42SJohn Baldwin {
1484e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
1485e0f66ef8SJohn Baldwin 	struct intr_thread *it;
14868b201c42SJohn Baldwin 	int comma;
14878b201c42SJohn Baldwin 
1488e0f66ef8SJohn Baldwin 	db_printf("%s ", ie->ie_fullname);
1489e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
1490e0f66ef8SJohn Baldwin 	if (it != NULL)
1491e0f66ef8SJohn Baldwin 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1492e0f66ef8SJohn Baldwin 	else
1493e0f66ef8SJohn Baldwin 		db_printf("(no thread)");
1494*c4eb6630SGleb Smirnoff 	if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1495e0f66ef8SJohn Baldwin 	    (it != NULL && it->it_need)) {
14968b201c42SJohn Baldwin 		db_printf(" {");
14978b201c42SJohn Baldwin 		comma = 0;
1498e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
14998b201c42SJohn Baldwin 			db_printf("SOFT");
15008b201c42SJohn Baldwin 			comma = 1;
15018b201c42SJohn Baldwin 		}
1502e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD) {
15038b201c42SJohn Baldwin 			if (comma)
15048b201c42SJohn Baldwin 				db_printf(", ");
1505e0f66ef8SJohn Baldwin 			db_printf("ADDING_THREAD");
15068b201c42SJohn Baldwin 			comma = 1;
15078b201c42SJohn Baldwin 		}
1508e0f66ef8SJohn Baldwin 		if (it != NULL && it->it_need) {
15098b201c42SJohn Baldwin 			if (comma)
15108b201c42SJohn Baldwin 				db_printf(", ");
15118b201c42SJohn Baldwin 			db_printf("NEED");
15128b201c42SJohn Baldwin 		}
15138b201c42SJohn Baldwin 		db_printf("}");
15148b201c42SJohn Baldwin 	}
15158b201c42SJohn Baldwin 	db_printf("\n");
15168b201c42SJohn Baldwin 
15178b201c42SJohn Baldwin 	if (handlers)
1518111b043cSAndriy Gapon 		CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
15198b201c42SJohn Baldwin 		    db_dump_intrhand(ih);
15208b201c42SJohn Baldwin }
1521e0f66ef8SJohn Baldwin 
1522e0f66ef8SJohn Baldwin /*
1523e0f66ef8SJohn Baldwin  * Dump data about interrupt handlers
1524e0f66ef8SJohn Baldwin  */
1525e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr)
1526e0f66ef8SJohn Baldwin {
1527e0f66ef8SJohn Baldwin 	struct intr_event *ie;
152819e9205aSJohn Baldwin 	int all, verbose;
1529e0f66ef8SJohn Baldwin 
1530dc15eac0SEd Schouten 	verbose = strchr(modif, 'v') != NULL;
1531dc15eac0SEd Schouten 	all = strchr(modif, 'a') != NULL;
1532e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ie, &event_list, ie_list) {
1533111b043cSAndriy Gapon 		if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1534e0f66ef8SJohn Baldwin 			continue;
1535e0f66ef8SJohn Baldwin 		db_dump_intr_event(ie, verbose);
153619e9205aSJohn Baldwin 		if (db_pager_quit)
153719e9205aSJohn Baldwin 			break;
1538e0f66ef8SJohn Baldwin 	}
1539e0f66ef8SJohn Baldwin }
15408b201c42SJohn Baldwin #endif /* DDB */
15418b201c42SJohn Baldwin 
1542b4151f71SJohn Baldwin /*
15438088699fSJohn Baldwin  * Start standard software interrupt threads
15441931cf94SJohn Baldwin  */
15451931cf94SJohn Baldwin static void
1546b4151f71SJohn Baldwin start_softintr(void *dummy)
15471931cf94SJohn Baldwin {
1548b4151f71SJohn Baldwin 
15498d809d50SJeff Roberson 	if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
15508d809d50SJeff Roberson 		panic("died while creating vm swi ithread");
15511931cf94SJohn Baldwin }
1552237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1553237fdd78SRobert Watson     NULL);
15541931cf94SJohn Baldwin 
1555d279178dSThomas Moestl /*
1556d279178dSThomas Moestl  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1557d279178dSThomas Moestl  * The data for this machine dependent, and the declarations are in machine
1558d279178dSThomas Moestl  * dependent code.  The layout of intrnames and intrcnt however is machine
1559d279178dSThomas Moestl  * independent.
1560d279178dSThomas Moestl  *
1561d279178dSThomas Moestl  * We do not know the length of intrcnt and intrnames at compile time, so
1562d279178dSThomas Moestl  * calculate things at run time.
1563d279178dSThomas Moestl  */
1564d279178dSThomas Moestl static int
1565d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1566d279178dSThomas Moestl {
1567521ea19dSAttilio Rao 	return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1568d279178dSThomas Moestl }
1569d279178dSThomas Moestl 
1570d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1571d279178dSThomas Moestl     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1572d279178dSThomas Moestl 
1573d279178dSThomas Moestl static int
1574d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1575d279178dSThomas Moestl {
157685729c2cSJuli Mallett #ifdef SCTL_MASK32
157785729c2cSJuli Mallett 	uint32_t *intrcnt32;
157885729c2cSJuli Mallett 	unsigned i;
157985729c2cSJuli Mallett 	int error;
158085729c2cSJuli Mallett 
158185729c2cSJuli Mallett 	if (req->flags & SCTL_MASK32) {
158285729c2cSJuli Mallett 		if (!req->oldptr)
158385729c2cSJuli Mallett 			return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
158485729c2cSJuli Mallett 		intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
158585729c2cSJuli Mallett 		if (intrcnt32 == NULL)
158685729c2cSJuli Mallett 			return (ENOMEM);
158785729c2cSJuli Mallett 		for (i = 0; i < sintrcnt / sizeof (u_long); i++)
158885729c2cSJuli Mallett 			intrcnt32[i] = intrcnt[i];
158985729c2cSJuli Mallett 		error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
159085729c2cSJuli Mallett 		free(intrcnt32, M_TEMP);
159185729c2cSJuli Mallett 		return (error);
159285729c2cSJuli Mallett 	}
159385729c2cSJuli Mallett #endif
1594521ea19dSAttilio Rao 	return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1595d279178dSThomas Moestl }
1596d279178dSThomas Moestl 
1597d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1598d279178dSThomas Moestl     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
15998b201c42SJohn Baldwin 
16008b201c42SJohn Baldwin #ifdef DDB
16018b201c42SJohn Baldwin /*
16028b201c42SJohn Baldwin  * DDB command to dump the interrupt statistics.
16038b201c42SJohn Baldwin  */
16048b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
16058b201c42SJohn Baldwin {
16068b201c42SJohn Baldwin 	u_long *i;
16078b201c42SJohn Baldwin 	char *cp;
1608521ea19dSAttilio Rao 	u_int j;
16098b201c42SJohn Baldwin 
16108b201c42SJohn Baldwin 	cp = intrnames;
1611521ea19dSAttilio Rao 	j = 0;
1612521ea19dSAttilio Rao 	for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1613521ea19dSAttilio Rao 	    i++, j++) {
16148b201c42SJohn Baldwin 		if (*cp == '\0')
16158b201c42SJohn Baldwin 			break;
16168b201c42SJohn Baldwin 		if (*i != 0)
16178b201c42SJohn Baldwin 			db_printf("%s\t%lu\n", cp, *i);
16188b201c42SJohn Baldwin 		cp += strlen(cp) + 1;
16198b201c42SJohn Baldwin 	}
16208b201c42SJohn Baldwin }
16218b201c42SJohn Baldwin #endif
1622