xref: /freebsd/sys/kern/kern_intr.c (revision c84c5e00ac28c8e00a56019031d1eaec74428b54)
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"
336fa041d7SWojciech Macek #include "opt_hwpmc_hooks.h"
34b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h"
358b201c42SJohn Baldwin 
361c5bb3eaSPeter Wemm #include <sys/param.h>
379a94c9c5SJohn Baldwin #include <sys/bus.h>
38c11110eaSAlfred Perlstein #include <sys/conf.h>
399b33b154SJeff Roberson #include <sys/cpuset.h>
409a94c9c5SJohn Baldwin #include <sys/rtprio.h>
41425f9fdaSStefan Eßer #include <sys/systm.h>
4268352337SDoug Rabson #include <sys/interrupt.h>
431931cf94SJohn Baldwin #include <sys/kernel.h>
441931cf94SJohn Baldwin #include <sys/kthread.h>
451931cf94SJohn Baldwin #include <sys/ktr.h>
4605b2c96fSBruce Evans #include <sys/limits.h>
47f34fa851SJohn Baldwin #include <sys/lock.h>
481931cf94SJohn Baldwin #include <sys/malloc.h>
4935e0e5b3SJohn Baldwin #include <sys/mutex.h>
50cebc7fb1SJohn Baldwin #include <sys/priv.h>
511931cf94SJohn Baldwin #include <sys/proc.h>
52511d1afbSGleb Smirnoff #include <sys/epoch.h>
533e5da754SJohn Baldwin #include <sys/random.h>
54b4151f71SJohn Baldwin #include <sys/resourcevar.h>
5563710c4dSJohn Baldwin #include <sys/sched.h>
56eaf86d16SJohn Baldwin #include <sys/smp.h>
57d279178dSThomas Moestl #include <sys/sysctl.h>
586205924aSKip Macy #include <sys/syslog.h>
591931cf94SJohn Baldwin #include <sys/unistd.h>
601931cf94SJohn Baldwin #include <sys/vmmeter.h>
611931cf94SJohn Baldwin #include <machine/atomic.h>
621931cf94SJohn Baldwin #include <machine/cpu.h>
638088699fSJohn Baldwin #include <machine/md_var.h>
64aba10e13SAlexander Motin #include <machine/smp.h>
65b4151f71SJohn Baldwin #include <machine/stdarg.h>
668b201c42SJohn Baldwin #ifdef DDB
678b201c42SJohn Baldwin #include <ddb/ddb.h>
688b201c42SJohn Baldwin #include <ddb/db_sym.h>
698b201c42SJohn Baldwin #endif
70425f9fdaSStefan Eßer 
71e0f66ef8SJohn Baldwin /*
72e0f66ef8SJohn Baldwin  * Describe an interrupt thread.  There is one of these per interrupt event.
73e0f66ef8SJohn Baldwin  */
74e0f66ef8SJohn Baldwin struct intr_thread {
75e0f66ef8SJohn Baldwin 	struct intr_event *it_event;
76e0f66ef8SJohn Baldwin 	struct thread *it_thread;	/* Kernel thread. */
77e0f66ef8SJohn Baldwin 	int	it_flags;		/* (j) IT_* flags. */
78e0f66ef8SJohn Baldwin 	int	it_need;		/* Needs service. */
796fa041d7SWojciech Macek 	int	it_waiting;		/* Waiting in the runq. */
803e5da754SJohn Baldwin };
813e5da754SJohn Baldwin 
82e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */
83e0f66ef8SJohn Baldwin #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
84e4cd31ddSJeff Roberson #define	IT_WAIT		0x000002	/* Thread is waiting for completion. */
85e0f66ef8SJohn Baldwin 
86e0f66ef8SJohn Baldwin struct	intr_entropy {
87e0f66ef8SJohn Baldwin 	struct	thread *td;
88e0f66ef8SJohn Baldwin 	uintptr_t event;
89e0f66ef8SJohn Baldwin };
90e0f66ef8SJohn Baldwin 
91aba10e13SAlexander Motin struct	intr_event *clk_intr_event;
92e0f66ef8SJohn Baldwin struct	intr_event *tty_intr_event;
937ab24ea3SJulian Elischer struct proc *intrproc;
941931cf94SJohn Baldwin 
95b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
96b4151f71SJohn Baldwin 
975d0e8299SConrad Meyer static int intr_storm_threshold = 0;
98af3b2549SHans Petter Selasky SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
997870c3c6SJohn Baldwin     &intr_storm_threshold, 0,
1007b1fe905SBruce Evans     "Number of consecutive interrupts before storm protection is enabled");
101511d1afbSGleb Smirnoff static int intr_epoch_batch = 1000;
102511d1afbSGleb Smirnoff SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch,
103511d1afbSGleb Smirnoff     0, "Maximum interrupt handler executions without re-entering epoch(9)");
1046fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
1056fa041d7SWojciech Macek static int intr_hwpmc_waiting_report_threshold = 1;
1066fa041d7SWojciech Macek SYSCTL_INT(_hw, OID_AUTO, intr_hwpmc_waiting_report_threshold, CTLFLAG_RWTUN,
1076fa041d7SWojciech Macek     &intr_hwpmc_waiting_report_threshold, 1,
1086fa041d7SWojciech Macek     "Threshold for reporting number of events in a workq");
1097bc13692SWojciech Macek #define	PMC_HOOK_INSTALLED_ANY() __predict_false(pmc_hook != NULL)
1106fa041d7SWojciech Macek #endif
111e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list =
112e0f66ef8SJohn Baldwin     TAILQ_HEAD_INITIALIZER(event_list);
1139b33b154SJeff Roberson static struct mtx event_lock;
1149b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
1157b1fe905SBruce Evans 
116e0f66ef8SJohn Baldwin static void	intr_event_update(struct intr_event *ie);
1176fa041d7SWojciech Macek static int	intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame);
118e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name);
119e0f66ef8SJohn Baldwin static void	ithread_destroy(struct intr_thread *ithread);
120bafe5a31SPaolo Pisati static void	ithread_execute_handlers(struct proc *p,
121bafe5a31SPaolo Pisati 		    struct intr_event *ie);
1227b1fe905SBruce Evans static void	ithread_loop(void *);
123e0f66ef8SJohn Baldwin static void	ithread_update(struct intr_thread *ithd);
1247b1fe905SBruce Evans static void	start_softintr(void *);
1257870c3c6SJohn Baldwin 
1266fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
1276fa041d7SWojciech Macek #include <sys/pmckern.h>
1286fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, all);
1296fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, ithread);
1306fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, filter);
1316fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, stray);
1326fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, schedule);
1336fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, waiting);
1347bc13692SWojciech Macek 
1357bc13692SWojciech Macek #define PMC_SOFT_CALL_INTR_HLPR(event, frame)			\
1367bc13692SWojciech Macek do {					\
1377bc13692SWojciech Macek 	if (frame != NULL)					\
1387bc13692SWojciech Macek 		PMC_SOFT_CALL_TF( , , intr, event, frame);	\
1397bc13692SWojciech Macek 	else							\
1407bc13692SWojciech Macek 		PMC_SOFT_CALL( , , intr, event);		\
1417bc13692SWojciech Macek } while (0)
1426fa041d7SWojciech Macek #endif
1436fa041d7SWojciech Macek 
144bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */
145b4151f71SJohn Baldwin u_char
146e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags)
1479a94c9c5SJohn Baldwin {
148b4151f71SJohn Baldwin 	u_char pri;
1499a94c9c5SJohn Baldwin 
150b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
1515a280d9cSPeter Wemm 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
1529a94c9c5SJohn Baldwin 	switch (flags) {
153b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
154d3305205SJohn Baldwin 		pri = PI_TTY;
1559a94c9c5SJohn Baldwin 		break;
1569a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
1579a94c9c5SJohn Baldwin 		pri = PI_DISK;
1589a94c9c5SJohn Baldwin 		break;
1599a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
1609a94c9c5SJohn Baldwin 		pri = PI_NET;
1619a94c9c5SJohn Baldwin 		break;
1629a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
163d3305205SJohn Baldwin 		pri = PI_DISK;
1649a94c9c5SJohn Baldwin 		break;
165d3305205SJohn Baldwin 	case INTR_TYPE_AV:
1665a280d9cSPeter Wemm 		pri = PI_AV;
1675a280d9cSPeter Wemm 		break;
168b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
169b4151f71SJohn Baldwin 		pri = PI_REALTIME;
170b4151f71SJohn Baldwin 		break;
1719a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1729a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1739a94c9c5SJohn Baldwin 		break;
1749a94c9c5SJohn Baldwin 	default:
175b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
176e0f66ef8SJohn Baldwin 		panic("intr_priority: no interrupt type in flags");
1779a94c9c5SJohn Baldwin 	}
1789a94c9c5SJohn Baldwin 
1799a94c9c5SJohn Baldwin 	return pri;
1809a94c9c5SJohn Baldwin }
1819a94c9c5SJohn Baldwin 
182b4151f71SJohn Baldwin /*
183e0f66ef8SJohn Baldwin  * Update an ithread based on the associated intr_event.
184b4151f71SJohn Baldwin  */
185b4151f71SJohn Baldwin static void
186e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd)
187b4151f71SJohn Baldwin {
188e0f66ef8SJohn Baldwin 	struct intr_event *ie;
189b40ce416SJulian Elischer 	struct thread *td;
190e0f66ef8SJohn Baldwin 	u_char pri;
1918088699fSJohn Baldwin 
192e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
193e0f66ef8SJohn Baldwin 	td = ithd->it_thread;
194111b043cSAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
195b4151f71SJohn Baldwin 
196e0f66ef8SJohn Baldwin 	/* Determine the overall priority of this event. */
197111b043cSAndriy Gapon 	if (CK_SLIST_EMPTY(&ie->ie_handlers))
198e0f66ef8SJohn Baldwin 		pri = PRI_MAX_ITHD;
199e0f66ef8SJohn Baldwin 	else
200111b043cSAndriy Gapon 		pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
201e80fb434SRobert Drehmel 
202e0f66ef8SJohn Baldwin 	/* Update name and priority. */
2037ab24ea3SJulian Elischer 	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
20444ad5475SJohn Baldwin #ifdef KTR
20544ad5475SJohn Baldwin 	sched_clear_tdname(td);
20644ad5475SJohn Baldwin #endif
207982d11f8SJeff Roberson 	thread_lock(td);
208fea89a28SJohn Baldwin 	sched_ithread_prio(td, pri);
209982d11f8SJeff Roberson 	thread_unlock(td);
210b4151f71SJohn Baldwin }
211e0f66ef8SJohn Baldwin 
212e0f66ef8SJohn Baldwin /*
213e0f66ef8SJohn Baldwin  * Regenerate the full name of an interrupt event and update its priority.
214e0f66ef8SJohn Baldwin  */
215e0f66ef8SJohn Baldwin static void
216e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie)
217e0f66ef8SJohn Baldwin {
218e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
219e0f66ef8SJohn Baldwin 	char *last;
220f912e8f2SHans Petter Selasky 	int missed, space, flags;
221e0f66ef8SJohn Baldwin 
222e0f66ef8SJohn Baldwin 	/* Start off with no entropy and just the name of the event. */
223e0f66ef8SJohn Baldwin 	mtx_assert(&ie->ie_lock, MA_OWNED);
224e0f66ef8SJohn Baldwin 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
225f912e8f2SHans Petter Selasky 	flags = 0;
2260811d60aSJohn Baldwin 	missed = 0;
227e0f66ef8SJohn Baldwin 	space = 1;
228e0f66ef8SJohn Baldwin 
229e0f66ef8SJohn Baldwin 	/* Run through all the handlers updating values. */
230111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
2316d51c0feSIan Lepore 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
232e0f66ef8SJohn Baldwin 		    sizeof(ie->ie_fullname)) {
233e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " ");
234e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, ih->ih_name);
235e0f66ef8SJohn Baldwin 			space = 0;
2360811d60aSJohn Baldwin 		} else
2370811d60aSJohn Baldwin 			missed++;
238f912e8f2SHans Petter Selasky 		flags |= ih->ih_flags;
2390811d60aSJohn Baldwin 	}
240f912e8f2SHans Petter Selasky 	ie->ie_hflags = flags;
241e0f66ef8SJohn Baldwin 
242e0f66ef8SJohn Baldwin 	/*
24367da50a0SIan Lepore 	 * If there is only one handler and its name is too long, just copy in
24467da50a0SIan Lepore 	 * as much of the end of the name (includes the unit number) as will
24567da50a0SIan Lepore 	 * fit.  Otherwise, we have multiple handlers and not all of the names
24667da50a0SIan Lepore 	 * will fit.  Add +'s to indicate missing names.  If we run out of room
24767da50a0SIan Lepore 	 * and still have +'s to add, change the last character from a + to a *.
248e0f66ef8SJohn Baldwin 	 */
24967da50a0SIan Lepore 	if (missed == 1 && space == 1) {
25067da50a0SIan Lepore 		ih = CK_SLIST_FIRST(&ie->ie_handlers);
25167da50a0SIan Lepore 		missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
25267da50a0SIan Lepore 		    sizeof(ie->ie_fullname);
25367da50a0SIan Lepore 		strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
25467da50a0SIan Lepore 		strcat(ie->ie_fullname, &ih->ih_name[missed]);
25567da50a0SIan Lepore 		missed = 0;
25667da50a0SIan Lepore 	}
257e0f66ef8SJohn Baldwin 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
2580811d60aSJohn Baldwin 	while (missed-- > 0) {
259e0f66ef8SJohn Baldwin 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
260e0f66ef8SJohn Baldwin 			if (*last == '+') {
261e0f66ef8SJohn Baldwin 				*last = '*';
262e0f66ef8SJohn Baldwin 				break;
263b4151f71SJohn Baldwin 			} else
264e0f66ef8SJohn Baldwin 				*last = '+';
265e0f66ef8SJohn Baldwin 		} else if (space) {
266e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, " +");
267e0f66ef8SJohn Baldwin 			space = 0;
268e0f66ef8SJohn Baldwin 		} else
269e0f66ef8SJohn Baldwin 			strcat(ie->ie_fullname, "+");
270b4151f71SJohn Baldwin 	}
271e0f66ef8SJohn Baldwin 
272e0f66ef8SJohn Baldwin 	/*
273e0f66ef8SJohn Baldwin 	 * If this event has an ithread, update it's priority and
274e0f66ef8SJohn Baldwin 	 * name.
275e0f66ef8SJohn Baldwin 	 */
276e0f66ef8SJohn Baldwin 	if (ie->ie_thread != NULL)
277e0f66ef8SJohn Baldwin 		ithread_update(ie->ie_thread);
278e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
279b4151f71SJohn Baldwin }
280b4151f71SJohn Baldwin 
281b4151f71SJohn Baldwin int
2829b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq,
2831ee1b687SJohn Baldwin     void (*pre_ithread)(void *), void (*post_ithread)(void *),
284066da805SAdrian Chadd     void (*post_filter)(void *), int (*assign_cpu)(void *, int),
2851ee1b687SJohn Baldwin     const char *fmt, ...)
286bafe5a31SPaolo Pisati {
287bafe5a31SPaolo Pisati 	struct intr_event *ie;
288bafe5a31SPaolo Pisati 	va_list ap;
289bafe5a31SPaolo Pisati 
290bafe5a31SPaolo Pisati 	/* The only valid flag during creation is IE_SOFT. */
291bafe5a31SPaolo Pisati 	if ((flags & ~IE_SOFT) != 0)
292bafe5a31SPaolo Pisati 		return (EINVAL);
293bafe5a31SPaolo Pisati 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
294bafe5a31SPaolo Pisati 	ie->ie_source = source;
2951ee1b687SJohn Baldwin 	ie->ie_pre_ithread = pre_ithread;
2961ee1b687SJohn Baldwin 	ie->ie_post_ithread = post_ithread;
2971ee1b687SJohn Baldwin 	ie->ie_post_filter = post_filter;
2986d2d1c04SJohn Baldwin 	ie->ie_assign_cpu = assign_cpu;
299bafe5a31SPaolo Pisati 	ie->ie_flags = flags;
3009b33b154SJeff Roberson 	ie->ie_irq = irq;
301eaf86d16SJohn Baldwin 	ie->ie_cpu = NOCPU;
302111b043cSAndriy Gapon 	CK_SLIST_INIT(&ie->ie_handlers);
303bafe5a31SPaolo Pisati 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
304bafe5a31SPaolo Pisati 
305bafe5a31SPaolo Pisati 	va_start(ap, fmt);
306bafe5a31SPaolo Pisati 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
307bafe5a31SPaolo Pisati 	va_end(ap);
308bafe5a31SPaolo Pisati 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
3099b33b154SJeff Roberson 	mtx_lock(&event_lock);
310bafe5a31SPaolo Pisati 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
3119b33b154SJeff Roberson 	mtx_unlock(&event_lock);
312bafe5a31SPaolo Pisati 	if (event != NULL)
313bafe5a31SPaolo Pisati 		*event = ie;
314bafe5a31SPaolo Pisati 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
315bafe5a31SPaolo Pisati 	return (0);
316bafe5a31SPaolo Pisati }
317b4151f71SJohn Baldwin 
318eaf86d16SJohn Baldwin /*
319eaf86d16SJohn Baldwin  * Bind an interrupt event to the specified CPU.  Note that not all
320eaf86d16SJohn Baldwin  * platforms support binding an interrupt to a CPU.  For those
32129dfb631SConrad Meyer  * platforms this request will fail.  Using a cpu id of NOCPU unbinds
322eaf86d16SJohn Baldwin  * the interrupt event.
323eaf86d16SJohn Baldwin  */
32429dfb631SConrad Meyer static int
32529dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
326eaf86d16SJohn Baldwin {
3279b33b154SJeff Roberson 	lwpid_t id;
328eaf86d16SJohn Baldwin 	int error;
329eaf86d16SJohn Baldwin 
330eaf86d16SJohn Baldwin 	/* Need a CPU to bind to. */
331eaf86d16SJohn Baldwin 	if (cpu != NOCPU && CPU_ABSENT(cpu))
332eaf86d16SJohn Baldwin 		return (EINVAL);
333eaf86d16SJohn Baldwin 
334eaf86d16SJohn Baldwin 	if (ie->ie_assign_cpu == NULL)
335eaf86d16SJohn Baldwin 		return (EOPNOTSUPP);
336cebc7fb1SJohn Baldwin 
337cebc7fb1SJohn Baldwin 	error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
338cebc7fb1SJohn Baldwin 	if (error)
339cebc7fb1SJohn Baldwin 		return (error);
340cebc7fb1SJohn Baldwin 
3419b33b154SJeff Roberson 	/*
342cebc7fb1SJohn Baldwin 	 * If we have any ithreads try to set their mask first to verify
343cebc7fb1SJohn Baldwin 	 * permissions, etc.
3449b33b154SJeff Roberson 	 */
34529dfb631SConrad Meyer 	if (bindithread) {
346eaf86d16SJohn Baldwin 		mtx_lock(&ie->ie_lock);
3479b33b154SJeff Roberson 		if (ie->ie_thread != NULL) {
3489b33b154SJeff Roberson 			id = ie->ie_thread->it_thread->td_tid;
349eaf86d16SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
35081198539SAlexander V. Chernikov 			error = cpuset_setithread(id, cpu);
3519b33b154SJeff Roberson 			if (error)
3529b33b154SJeff Roberson 				return (error);
3539b33b154SJeff Roberson 		} else
354eaf86d16SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
35529dfb631SConrad Meyer 	}
35629dfb631SConrad Meyer 	if (bindirq)
357eaf86d16SJohn Baldwin 		error = ie->ie_assign_cpu(ie->ie_source, cpu);
358cebc7fb1SJohn Baldwin 	if (error) {
35929dfb631SConrad Meyer 		if (bindithread) {
360cebc7fb1SJohn Baldwin 			mtx_lock(&ie->ie_lock);
361cebc7fb1SJohn Baldwin 			if (ie->ie_thread != NULL) {
36281198539SAlexander V. Chernikov 				cpu = ie->ie_cpu;
363cebc7fb1SJohn Baldwin 				id = ie->ie_thread->it_thread->td_tid;
364cebc7fb1SJohn Baldwin 				mtx_unlock(&ie->ie_lock);
36581198539SAlexander V. Chernikov 				(void)cpuset_setithread(id, cpu);
366cebc7fb1SJohn Baldwin 			} else
367cebc7fb1SJohn Baldwin 				mtx_unlock(&ie->ie_lock);
36829dfb631SConrad Meyer 		}
369eaf86d16SJohn Baldwin 		return (error);
370cebc7fb1SJohn Baldwin 	}
371cebc7fb1SJohn Baldwin 
37229dfb631SConrad Meyer 	if (bindirq) {
373eaf86d16SJohn Baldwin 		mtx_lock(&ie->ie_lock);
374eaf86d16SJohn Baldwin 		ie->ie_cpu = cpu;
3759b33b154SJeff Roberson 		mtx_unlock(&ie->ie_lock);
37629dfb631SConrad Meyer 	}
3779b33b154SJeff Roberson 
3789b33b154SJeff Roberson 	return (error);
3799b33b154SJeff Roberson }
3809b33b154SJeff Roberson 
38129dfb631SConrad Meyer /*
38229dfb631SConrad Meyer  * Bind an interrupt event to the specified CPU.  For supported platforms, any
38329dfb631SConrad Meyer  * associated ithreads as well as the primary interrupt context will be bound
38429dfb631SConrad Meyer  * to the specificed CPU.
38529dfb631SConrad Meyer  */
38629dfb631SConrad Meyer int
38729dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu)
38829dfb631SConrad Meyer {
38929dfb631SConrad Meyer 
39029dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, true, true));
39129dfb631SConrad Meyer }
39229dfb631SConrad Meyer 
39329dfb631SConrad Meyer /*
39429dfb631SConrad Meyer  * Bind an interrupt event to the specified CPU, but do not bind associated
39529dfb631SConrad Meyer  * ithreads.
39629dfb631SConrad Meyer  */
39729dfb631SConrad Meyer int
39829dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu)
39929dfb631SConrad Meyer {
40029dfb631SConrad Meyer 
40129dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, true, false));
40229dfb631SConrad Meyer }
40329dfb631SConrad Meyer 
40429dfb631SConrad Meyer /*
40529dfb631SConrad Meyer  * Bind an interrupt event's ithread to the specified CPU.
40629dfb631SConrad Meyer  */
40729dfb631SConrad Meyer int
40829dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu)
40929dfb631SConrad Meyer {
41029dfb631SConrad Meyer 
41129dfb631SConrad Meyer 	return (_intr_event_bind(ie, cpu, false, true));
41229dfb631SConrad Meyer }
41329dfb631SConrad Meyer 
4144e255d74SAndrew Gallatin /*
4154e255d74SAndrew Gallatin  * Bind an interrupt event's ithread to the specified cpuset.
4164e255d74SAndrew Gallatin  */
4174e255d74SAndrew Gallatin int
4184e255d74SAndrew Gallatin intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
4194e255d74SAndrew Gallatin {
4204e255d74SAndrew Gallatin 	lwpid_t id;
4214e255d74SAndrew Gallatin 
4224e255d74SAndrew Gallatin 	mtx_lock(&ie->ie_lock);
4234e255d74SAndrew Gallatin 	if (ie->ie_thread != NULL) {
4244e255d74SAndrew Gallatin 		id = ie->ie_thread->it_thread->td_tid;
4254e255d74SAndrew Gallatin 		mtx_unlock(&ie->ie_lock);
4264e255d74SAndrew Gallatin 		return (cpuset_setthread(id, cs));
4274e255d74SAndrew Gallatin 	} else {
4284e255d74SAndrew Gallatin 		mtx_unlock(&ie->ie_lock);
4294e255d74SAndrew Gallatin 	}
4304e255d74SAndrew Gallatin 	return (ENODEV);
4314e255d74SAndrew Gallatin }
4324e255d74SAndrew Gallatin 
4339b33b154SJeff Roberson static struct intr_event *
4349b33b154SJeff Roberson intr_lookup(int irq)
4359b33b154SJeff Roberson {
4369b33b154SJeff Roberson 	struct intr_event *ie;
4379b33b154SJeff Roberson 
4389b33b154SJeff Roberson 	mtx_lock(&event_lock);
4399b33b154SJeff Roberson 	TAILQ_FOREACH(ie, &event_list, ie_list)
4409b33b154SJeff Roberson 		if (ie->ie_irq == irq &&
4419b33b154SJeff Roberson 		    (ie->ie_flags & IE_SOFT) == 0 &&
442111b043cSAndriy Gapon 		    CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
4439b33b154SJeff Roberson 			break;
4449b33b154SJeff Roberson 	mtx_unlock(&event_lock);
4459b33b154SJeff Roberson 	return (ie);
4469b33b154SJeff Roberson }
4479b33b154SJeff Roberson 
4489b33b154SJeff Roberson int
44929dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m)
4509b33b154SJeff Roberson {
4519b33b154SJeff Roberson 	struct intr_event *ie;
4529b33b154SJeff Roberson 	cpuset_t *mask;
4533fe93b94SAdrian Chadd 	int cpu, n;
4549b33b154SJeff Roberson 
4559b33b154SJeff Roberson 	mask = m;
4569b33b154SJeff Roberson 	cpu = NOCPU;
4579b33b154SJeff Roberson 	/*
4589b33b154SJeff Roberson 	 * If we're setting all cpus we can unbind.  Otherwise make sure
4599b33b154SJeff Roberson 	 * only one cpu is in the set.
4609b33b154SJeff Roberson 	 */
4619b33b154SJeff Roberson 	if (CPU_CMP(cpuset_root, mask)) {
4629b33b154SJeff Roberson 		for (n = 0; n < CPU_SETSIZE; n++) {
4639b33b154SJeff Roberson 			if (!CPU_ISSET(n, mask))
4649b33b154SJeff Roberson 				continue;
4659b33b154SJeff Roberson 			if (cpu != NOCPU)
4669b33b154SJeff Roberson 				return (EINVAL);
4673fe93b94SAdrian Chadd 			cpu = n;
4689b33b154SJeff Roberson 		}
4699b33b154SJeff Roberson 	}
4709b33b154SJeff Roberson 	ie = intr_lookup(irq);
4719b33b154SJeff Roberson 	if (ie == NULL)
4729b33b154SJeff Roberson 		return (ESRCH);
47329dfb631SConrad Meyer 	switch (mode) {
47429dfb631SConrad Meyer 	case CPU_WHICH_IRQ:
4759bd55acfSJohn Baldwin 		return (intr_event_bind(ie, cpu));
47629dfb631SConrad Meyer 	case CPU_WHICH_INTRHANDLER:
47729dfb631SConrad Meyer 		return (intr_event_bind_irqonly(ie, cpu));
47829dfb631SConrad Meyer 	case CPU_WHICH_ITHREAD:
47929dfb631SConrad Meyer 		return (intr_event_bind_ithread(ie, cpu));
48029dfb631SConrad Meyer 	default:
48129dfb631SConrad Meyer 		return (EINVAL);
48229dfb631SConrad Meyer 	}
4839b33b154SJeff Roberson }
4849b33b154SJeff Roberson 
4859b33b154SJeff Roberson int
48629dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m)
4879b33b154SJeff Roberson {
4889b33b154SJeff Roberson 	struct intr_event *ie;
48929dfb631SConrad Meyer 	struct thread *td;
49029dfb631SConrad Meyer 	struct proc *p;
4919b33b154SJeff Roberson 	cpuset_t *mask;
49229dfb631SConrad Meyer 	lwpid_t id;
49329dfb631SConrad Meyer 	int error;
4949b33b154SJeff Roberson 
4959b33b154SJeff Roberson 	mask = m;
4969b33b154SJeff Roberson 	ie = intr_lookup(irq);
4979b33b154SJeff Roberson 	if (ie == NULL)
4989b33b154SJeff Roberson 		return (ESRCH);
49929dfb631SConrad Meyer 
50029dfb631SConrad Meyer 	error = 0;
5019b33b154SJeff Roberson 	CPU_ZERO(mask);
50229dfb631SConrad Meyer 	switch (mode) {
50329dfb631SConrad Meyer 	case CPU_WHICH_IRQ:
50429dfb631SConrad Meyer 	case CPU_WHICH_INTRHANDLER:
5059b33b154SJeff Roberson 		mtx_lock(&ie->ie_lock);
5069b33b154SJeff Roberson 		if (ie->ie_cpu == NOCPU)
5079b33b154SJeff Roberson 			CPU_COPY(cpuset_root, mask);
5089b33b154SJeff Roberson 		else
5099b33b154SJeff Roberson 			CPU_SET(ie->ie_cpu, mask);
510eaf86d16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
51129dfb631SConrad Meyer 		break;
51229dfb631SConrad Meyer 	case CPU_WHICH_ITHREAD:
51329dfb631SConrad Meyer 		mtx_lock(&ie->ie_lock);
51429dfb631SConrad Meyer 		if (ie->ie_thread == NULL) {
51529dfb631SConrad Meyer 			mtx_unlock(&ie->ie_lock);
51629dfb631SConrad Meyer 			CPU_COPY(cpuset_root, mask);
51729dfb631SConrad Meyer 		} else {
51829dfb631SConrad Meyer 			id = ie->ie_thread->it_thread->td_tid;
51929dfb631SConrad Meyer 			mtx_unlock(&ie->ie_lock);
52029dfb631SConrad Meyer 			error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
52129dfb631SConrad Meyer 			if (error != 0)
52229dfb631SConrad Meyer 				return (error);
52329dfb631SConrad Meyer 			CPU_COPY(&td->td_cpuset->cs_mask, mask);
52429dfb631SConrad Meyer 			PROC_UNLOCK(p);
52529dfb631SConrad Meyer 		}
52629dfb631SConrad Meyer 	default:
52729dfb631SConrad Meyer 		return (EINVAL);
52829dfb631SConrad Meyer 	}
529eaf86d16SJohn Baldwin 	return (0);
530eaf86d16SJohn Baldwin }
531eaf86d16SJohn Baldwin 
532b4151f71SJohn Baldwin int
533e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie)
534b4151f71SJohn Baldwin {
535b4151f71SJohn Baldwin 
5369b33b154SJeff Roberson 	mtx_lock(&event_lock);
537e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
538111b043cSAndriy Gapon 	if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
539e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
5409b33b154SJeff Roberson 		mtx_unlock(&event_lock);
541e0f66ef8SJohn Baldwin 		return (EBUSY);
5424d29cb2dSJohn Baldwin 	}
543e0f66ef8SJohn Baldwin 	TAILQ_REMOVE(&event_list, ie, ie_list);
5449477358dSJohn Baldwin #ifndef notyet
5459477358dSJohn Baldwin 	if (ie->ie_thread != NULL) {
5469477358dSJohn Baldwin 		ithread_destroy(ie->ie_thread);
5479477358dSJohn Baldwin 		ie->ie_thread = NULL;
5489477358dSJohn Baldwin 	}
5499477358dSJohn Baldwin #endif
550e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
5519b33b154SJeff Roberson 	mtx_unlock(&event_lock);
552e0f66ef8SJohn Baldwin 	mtx_destroy(&ie->ie_lock);
553e0f66ef8SJohn Baldwin 	free(ie, M_ITHREAD);
554e0f66ef8SJohn Baldwin 	return (0);
555e0f66ef8SJohn Baldwin }
556e0f66ef8SJohn Baldwin 
557e0f66ef8SJohn Baldwin static struct intr_thread *
558e0f66ef8SJohn Baldwin ithread_create(const char *name)
559e0f66ef8SJohn Baldwin {
560e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
561e0f66ef8SJohn Baldwin 	struct thread *td;
562e0f66ef8SJohn Baldwin 	int error;
563e0f66ef8SJohn Baldwin 
564e0f66ef8SJohn Baldwin 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
565e0f66ef8SJohn Baldwin 
5667ab24ea3SJulian Elischer 	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
5677ab24ea3SJulian Elischer 		    &td, RFSTOPPED | RFHIGHPID,
5689ef95d01SJulian Elischer 		    0, "intr", "%s", name);
569e0f66ef8SJohn Baldwin 	if (error)
5703745c395SJulian Elischer 		panic("kproc_create() failed with %d", error);
571982d11f8SJeff Roberson 	thread_lock(td);
572ad1e7d28SJulian Elischer 	sched_class(td, PRI_ITHD);
573e0f66ef8SJohn Baldwin 	TD_SET_IWAIT(td);
574982d11f8SJeff Roberson 	thread_unlock(td);
575e0f66ef8SJohn Baldwin 	td->td_pflags |= TDP_ITHREAD;
576e0f66ef8SJohn Baldwin 	ithd->it_thread = td;
577e0f66ef8SJohn Baldwin 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
578e0f66ef8SJohn Baldwin 	return (ithd);
579e0f66ef8SJohn Baldwin }
580e0f66ef8SJohn Baldwin 
581e0f66ef8SJohn Baldwin static void
582e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread)
583e0f66ef8SJohn Baldwin {
584e0f66ef8SJohn Baldwin 	struct thread *td;
585e0f66ef8SJohn Baldwin 
586bb141be1SScott Long 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
587e0f66ef8SJohn Baldwin 	td = ithread->it_thread;
588982d11f8SJeff Roberson 	thread_lock(td);
589e0f66ef8SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
59071fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
59171fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
592ed998d1cSJohn Baldwin 		sched_wakeup(td, SRQ_INTR);
59361a74c5cSJeff Roberson 	} else
594982d11f8SJeff Roberson 		thread_unlock(td);
595b4151f71SJohn Baldwin }
596b4151f71SJohn Baldwin 
597b4151f71SJohn Baldwin int
598e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name,
599ef544f63SPaolo Pisati     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
600ef544f63SPaolo Pisati     enum intr_type flags, void **cookiep)
601b4151f71SJohn Baldwin {
602e0f66ef8SJohn Baldwin 	struct intr_handler *ih, *temp_ih;
603111b043cSAndriy Gapon 	struct intr_handler **prevptr;
604e0f66ef8SJohn Baldwin 	struct intr_thread *it;
605b4151f71SJohn Baldwin 
606ef544f63SPaolo Pisati 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
607b4151f71SJohn Baldwin 		return (EINVAL);
608b4151f71SJohn Baldwin 
609e0f66ef8SJohn Baldwin 	/* Allocate and populate an interrupt handler structure. */
610e0f66ef8SJohn Baldwin 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
611ef544f63SPaolo Pisati 	ih->ih_filter = filter;
612b4151f71SJohn Baldwin 	ih->ih_handler = handler;
613b4151f71SJohn Baldwin 	ih->ih_argument = arg;
61437b8ef16SJohn Baldwin 	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
615e0f66ef8SJohn Baldwin 	ih->ih_event = ie;
616b4151f71SJohn Baldwin 	ih->ih_pri = pri;
617ef544f63SPaolo Pisati 	if (flags & INTR_EXCL)
618b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
619b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
620b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
621b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
622b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
623511d1afbSGleb Smirnoff 	if (flags & INTR_TYPE_NET)
624511d1afbSGleb Smirnoff 		ih->ih_flags |= IH_NET;
625b4151f71SJohn Baldwin 
626e0f66ef8SJohn Baldwin 	/* We can only have one exclusive handler in a event. */
627e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
628111b043cSAndriy Gapon 	if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
629e0f66ef8SJohn Baldwin 		if ((flags & INTR_EXCL) ||
630111b043cSAndriy Gapon 		    (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
631e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
632b4151f71SJohn Baldwin 			free(ih, M_ITHREAD);
633b4151f71SJohn Baldwin 			return (EINVAL);
634b4151f71SJohn Baldwin 		}
635e0f66ef8SJohn Baldwin 	}
636e0f66ef8SJohn Baldwin 
637e0f66ef8SJohn Baldwin 	/* Create a thread if we need one. */
638ef544f63SPaolo Pisati 	while (ie->ie_thread == NULL && handler != NULL) {
639e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD)
6400f180a7cSJohn Baldwin 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
641e0f66ef8SJohn Baldwin 		else {
642e0f66ef8SJohn Baldwin 			ie->ie_flags |= IE_ADDING_THREAD;
643e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
644e0f66ef8SJohn Baldwin 			it = ithread_create("intr: newborn");
645e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
646e0f66ef8SJohn Baldwin 			ie->ie_flags &= ~IE_ADDING_THREAD;
647e0f66ef8SJohn Baldwin 			ie->ie_thread = it;
648e0f66ef8SJohn Baldwin 			it->it_event = ie;
649e0f66ef8SJohn Baldwin 			ithread_update(it);
650e0f66ef8SJohn Baldwin 			wakeup(ie);
651e0f66ef8SJohn Baldwin 		}
652e0f66ef8SJohn Baldwin 	}
653c9516c94SAlexander Kabaev 
654c9516c94SAlexander Kabaev 	/* Add the new handler to the event in priority order. */
655111b043cSAndriy Gapon 	CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
656c9516c94SAlexander Kabaev 		if (temp_ih->ih_pri > ih->ih_pri)
657c9516c94SAlexander Kabaev 			break;
658c9516c94SAlexander Kabaev 	}
659111b043cSAndriy Gapon 	CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
660111b043cSAndriy Gapon 
661c9516c94SAlexander Kabaev 	intr_event_update(ie);
662c9516c94SAlexander Kabaev 
663e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
664e0f66ef8SJohn Baldwin 	    ie->ie_name);
665e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
666e0f66ef8SJohn Baldwin 
667e0f66ef8SJohn Baldwin 	if (cookiep != NULL)
668e0f66ef8SJohn Baldwin 		*cookiep = ih;
669e0f66ef8SJohn Baldwin 	return (0);
670e0f66ef8SJohn Baldwin }
671b4151f71SJohn Baldwin 
672c3045318SJohn Baldwin /*
67337b8ef16SJohn Baldwin  * Append a description preceded by a ':' to the name of the specified
67437b8ef16SJohn Baldwin  * interrupt handler.
67537b8ef16SJohn Baldwin  */
67637b8ef16SJohn Baldwin int
67737b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie,
67837b8ef16SJohn Baldwin     const char *descr)
67937b8ef16SJohn Baldwin {
68037b8ef16SJohn Baldwin 	struct intr_handler *ih;
68137b8ef16SJohn Baldwin 	size_t space;
68237b8ef16SJohn Baldwin 	char *start;
68337b8ef16SJohn Baldwin 
68437b8ef16SJohn Baldwin 	mtx_lock(&ie->ie_lock);
68537b8ef16SJohn Baldwin #ifdef INVARIANTS
686111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
68737b8ef16SJohn Baldwin 		if (ih == cookie)
68837b8ef16SJohn Baldwin 			break;
68937b8ef16SJohn Baldwin 	}
69037b8ef16SJohn Baldwin 	if (ih == NULL) {
69137b8ef16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
692d0c9a291SJohn Baldwin 		panic("handler %p not found in interrupt event %p", cookie, ie);
69337b8ef16SJohn Baldwin 	}
69437b8ef16SJohn Baldwin #endif
69537b8ef16SJohn Baldwin 	ih = cookie;
69637b8ef16SJohn Baldwin 
69737b8ef16SJohn Baldwin 	/*
69837b8ef16SJohn Baldwin 	 * Look for an existing description by checking for an
69937b8ef16SJohn Baldwin 	 * existing ":".  This assumes device names do not include
70037b8ef16SJohn Baldwin 	 * colons.  If one is found, prepare to insert the new
70137b8ef16SJohn Baldwin 	 * description at that point.  If one is not found, find the
70237b8ef16SJohn Baldwin 	 * end of the name to use as the insertion point.
70337b8ef16SJohn Baldwin 	 */
704dc15eac0SEd Schouten 	start = strchr(ih->ih_name, ':');
70537b8ef16SJohn Baldwin 	if (start == NULL)
706dc15eac0SEd Schouten 		start = strchr(ih->ih_name, 0);
70737b8ef16SJohn Baldwin 
70837b8ef16SJohn Baldwin 	/*
70937b8ef16SJohn Baldwin 	 * See if there is enough remaining room in the string for the
71037b8ef16SJohn Baldwin 	 * description + ":".  The "- 1" leaves room for the trailing
71137b8ef16SJohn Baldwin 	 * '\0'.  The "+ 1" accounts for the colon.
71237b8ef16SJohn Baldwin 	 */
71337b8ef16SJohn Baldwin 	space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
71437b8ef16SJohn Baldwin 	if (strlen(descr) + 1 > space) {
71537b8ef16SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
71637b8ef16SJohn Baldwin 		return (ENOSPC);
71737b8ef16SJohn Baldwin 	}
71837b8ef16SJohn Baldwin 
71937b8ef16SJohn Baldwin 	/* Append a colon followed by the description. */
72037b8ef16SJohn Baldwin 	*start = ':';
72137b8ef16SJohn Baldwin 	strcpy(start + 1, descr);
72237b8ef16SJohn Baldwin 	intr_event_update(ie);
72337b8ef16SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
72437b8ef16SJohn Baldwin 	return (0);
72537b8ef16SJohn Baldwin }
72637b8ef16SJohn Baldwin 
72737b8ef16SJohn Baldwin /*
728c3045318SJohn Baldwin  * Return the ie_source field from the intr_event an intr_handler is
729c3045318SJohn Baldwin  * associated with.
730c3045318SJohn Baldwin  */
731c3045318SJohn Baldwin void *
732c3045318SJohn Baldwin intr_handler_source(void *cookie)
733c3045318SJohn Baldwin {
734c3045318SJohn Baldwin 	struct intr_handler *ih;
735c3045318SJohn Baldwin 	struct intr_event *ie;
736c3045318SJohn Baldwin 
737c3045318SJohn Baldwin 	ih = (struct intr_handler *)cookie;
738c3045318SJohn Baldwin 	if (ih == NULL)
739c3045318SJohn Baldwin 		return (NULL);
740c3045318SJohn Baldwin 	ie = ih->ih_event;
741c3045318SJohn Baldwin 	KASSERT(ie != NULL,
742c3045318SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
743c3045318SJohn Baldwin 	    ih->ih_name));
744c3045318SJohn Baldwin 	return (ie->ie_source);
745c3045318SJohn Baldwin }
746c3045318SJohn Baldwin 
747e4cd31ddSJeff Roberson /*
748e0fa977eSAndriy Gapon  * If intr_event_handle() is running in the ISR context at the time of the call,
749e0fa977eSAndriy Gapon  * then wait for it to complete.
750e0fa977eSAndriy Gapon  */
751e0fa977eSAndriy Gapon static void
752e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie)
753e0fa977eSAndriy Gapon {
754e0fa977eSAndriy Gapon 	int phase;
755e0fa977eSAndriy Gapon 
756e0fa977eSAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
757e0fa977eSAndriy Gapon 	phase = ie->ie_phase;
758e0fa977eSAndriy Gapon 
759e0fa977eSAndriy Gapon 	/*
760e0fa977eSAndriy Gapon 	 * Switch phase to direct future interrupts to the other active counter.
761e0fa977eSAndriy Gapon 	 * Make sure that any preceding stores are visible before the switch.
762e0fa977eSAndriy Gapon 	 */
763e0fa977eSAndriy Gapon 	KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
764e0fa977eSAndriy Gapon 	atomic_store_rel_int(&ie->ie_phase, !phase);
765e0fa977eSAndriy Gapon 
766e0fa977eSAndriy Gapon 	/*
767e0fa977eSAndriy Gapon 	 * This code cooperates with wait-free iteration of ie_handlers
768e0fa977eSAndriy Gapon 	 * in intr_event_handle.
769e0fa977eSAndriy Gapon 	 * Make sure that the removal and the phase update are not reordered
770e0fa977eSAndriy Gapon 	 * with the active count check.
771e0fa977eSAndriy Gapon 	 * Note that no combination of acquire and release fences can provide
772e0fa977eSAndriy Gapon 	 * that guarantee as Store->Load sequences can always be reordered.
773e0fa977eSAndriy Gapon 	 */
774e0fa977eSAndriy Gapon 	atomic_thread_fence_seq_cst();
775e0fa977eSAndriy Gapon 
776e0fa977eSAndriy Gapon 	/*
777e0fa977eSAndriy Gapon 	 * Now wait on the inactive phase.
778e0fa977eSAndriy Gapon 	 * The acquire fence is needed so that that all post-barrier accesses
779e0fa977eSAndriy Gapon 	 * are after the check.
780e0fa977eSAndriy Gapon 	 */
781e0fa977eSAndriy Gapon 	while (ie->ie_active[phase] > 0)
782e0fa977eSAndriy Gapon 		cpu_spinwait();
783e0fa977eSAndriy Gapon 	atomic_thread_fence_acq();
784e0fa977eSAndriy Gapon }
785e0fa977eSAndriy Gapon 
78682a5a275SAndriy Gapon static void
78782a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler)
78882a5a275SAndriy Gapon {
78982a5a275SAndriy Gapon 	struct intr_event *ie;
79082a5a275SAndriy Gapon 
79182a5a275SAndriy Gapon 	ie = handler->ih_event;
79282a5a275SAndriy Gapon 	mtx_assert(&ie->ie_lock, MA_OWNED);
79382a5a275SAndriy Gapon 	KASSERT((handler->ih_flags & IH_DEAD) == 0,
79482a5a275SAndriy Gapon 	    ("update for a removed handler"));
79582a5a275SAndriy Gapon 
79682a5a275SAndriy Gapon 	if (ie->ie_thread == NULL) {
79782a5a275SAndriy Gapon 		intr_event_barrier(ie);
79882a5a275SAndriy Gapon 		return;
79982a5a275SAndriy Gapon 	}
80082a5a275SAndriy Gapon 	if ((handler->ih_flags & IH_CHANGED) == 0) {
80182a5a275SAndriy Gapon 		handler->ih_flags |= IH_CHANGED;
8026fa041d7SWojciech Macek 		intr_event_schedule_thread(ie, NULL);
80382a5a275SAndriy Gapon 	}
80482a5a275SAndriy Gapon 	while ((handler->ih_flags & IH_CHANGED) != 0)
80582a5a275SAndriy Gapon 		msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
80682a5a275SAndriy Gapon }
80782a5a275SAndriy Gapon 
808e0fa977eSAndriy Gapon /*
809e4cd31ddSJeff Roberson  * Sleep until an ithread finishes executing an interrupt handler.
810e4cd31ddSJeff Roberson  *
811e4cd31ddSJeff Roberson  * XXX Doesn't currently handle interrupt filters or fast interrupt
8126eb60f5bSHans Petter Selasky  * handlers. This is intended for LinuxKPI drivers only.
8136eb60f5bSHans Petter Selasky  * Do not use in BSD code.
814e4cd31ddSJeff Roberson  */
815e4cd31ddSJeff Roberson void
816e4cd31ddSJeff Roberson _intr_drain(int irq)
817e4cd31ddSJeff Roberson {
818e4cd31ddSJeff Roberson 	struct intr_event *ie;
819e4cd31ddSJeff Roberson 	struct intr_thread *ithd;
820e4cd31ddSJeff Roberson 	struct thread *td;
821e4cd31ddSJeff Roberson 
822e4cd31ddSJeff Roberson 	ie = intr_lookup(irq);
823e4cd31ddSJeff Roberson 	if (ie == NULL)
824e4cd31ddSJeff Roberson 		return;
825e4cd31ddSJeff Roberson 	if (ie->ie_thread == NULL)
826e4cd31ddSJeff Roberson 		return;
827e4cd31ddSJeff Roberson 	ithd = ie->ie_thread;
828e4cd31ddSJeff Roberson 	td = ithd->it_thread;
8295bd186a6SJeff Roberson 	/*
8305bd186a6SJeff Roberson 	 * We set the flag and wait for it to be cleared to avoid
8315bd186a6SJeff Roberson 	 * long delays with potentially busy interrupt handlers
8325bd186a6SJeff Roberson 	 * were we to only sample TD_AWAITING_INTR() every tick.
8335bd186a6SJeff Roberson 	 */
834e4cd31ddSJeff Roberson 	thread_lock(td);
835e4cd31ddSJeff Roberson 	if (!TD_AWAITING_INTR(td)) {
836e4cd31ddSJeff Roberson 		ithd->it_flags |= IT_WAIT;
8375bd186a6SJeff Roberson 		while (ithd->it_flags & IT_WAIT) {
8385bd186a6SJeff Roberson 			thread_unlock(td);
8395bd186a6SJeff Roberson 			pause("idrain", 1);
8405bd186a6SJeff Roberson 			thread_lock(td);
841e4cd31ddSJeff Roberson 		}
8425bd186a6SJeff Roberson 	}
8435bd186a6SJeff Roberson 	thread_unlock(td);
844e4cd31ddSJeff Roberson 	return;
845e4cd31ddSJeff Roberson }
846e4cd31ddSJeff Roberson 
847b4151f71SJohn Baldwin int
848e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie)
849b4151f71SJohn Baldwin {
850e0f66ef8SJohn Baldwin 	struct intr_handler *handler = (struct intr_handler *)cookie;
851e0f66ef8SJohn Baldwin 	struct intr_event *ie;
852e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
853111b043cSAndriy Gapon 	struct intr_handler **prevptr;
854e0f66ef8SJohn Baldwin #ifdef notyet
855e0f66ef8SJohn Baldwin 	int dead;
856b4151f71SJohn Baldwin #endif
857b4151f71SJohn Baldwin 
8583e5da754SJohn Baldwin 	if (handler == NULL)
859b4151f71SJohn Baldwin 		return (EINVAL);
860e0f66ef8SJohn Baldwin 	ie = handler->ih_event;
861e0f66ef8SJohn Baldwin 	KASSERT(ie != NULL,
862e0f66ef8SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt event",
8633e5da754SJohn Baldwin 	    handler->ih_name));
864111b043cSAndriy Gapon 
865e0f66ef8SJohn Baldwin 	mtx_lock(&ie->ie_lock);
86691f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
867e0f66ef8SJohn Baldwin 	    ie->ie_name);
868111b043cSAndriy Gapon 	CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
8693e5da754SJohn Baldwin 		if (ih == handler)
870111b043cSAndriy Gapon 			break;
871111b043cSAndriy Gapon 	}
872111b043cSAndriy Gapon 	if (ih == NULL) {
873111b043cSAndriy Gapon 		panic("interrupt handler \"%s\" not found in "
874111b043cSAndriy Gapon 		    "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
875111b043cSAndriy Gapon 	}
876111b043cSAndriy Gapon 
877de271f01SJohn Baldwin 	/*
878e0fa977eSAndriy Gapon 	 * If there is no ithread, then directly remove the handler.  Note that
879e0fa977eSAndriy Gapon 	 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
880e0fa977eSAndriy Gapon 	 * care needs to be taken to keep ie_handlers consistent and to free
881e0fa977eSAndriy Gapon 	 * the removed handler only when ie_handlers is quiescent.
882e0f66ef8SJohn Baldwin 	 */
883e0f66ef8SJohn Baldwin 	if (ie->ie_thread == NULL) {
884111b043cSAndriy Gapon 		CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
885e0fa977eSAndriy Gapon 		intr_event_barrier(ie);
886e0fa977eSAndriy Gapon 		intr_event_update(ie);
887e0f66ef8SJohn Baldwin 		mtx_unlock(&ie->ie_lock);
888e0f66ef8SJohn Baldwin 		free(handler, M_ITHREAD);
889e0f66ef8SJohn Baldwin 		return (0);
890e0f66ef8SJohn Baldwin 	}
891e0f66ef8SJohn Baldwin 
892e0f66ef8SJohn Baldwin 	/*
893e0fa977eSAndriy Gapon 	 * Let the interrupt thread do the job.
894e0fa977eSAndriy Gapon 	 * The interrupt source is disabled when the interrupt thread is
895e0fa977eSAndriy Gapon 	 * running, so it does not have to worry about interaction with
896e0fa977eSAndriy Gapon 	 * intr_event_handle().
897de271f01SJohn Baldwin 	 */
898e0fa977eSAndriy Gapon 	KASSERT((handler->ih_flags & IH_DEAD) == 0,
899e0fa977eSAndriy Gapon 	    ("duplicate handle remove"));
900de271f01SJohn Baldwin 	handler->ih_flags |= IH_DEAD;
9016fa041d7SWojciech Macek 	intr_event_schedule_thread(ie, NULL);
902e0f66ef8SJohn Baldwin 	while (handler->ih_flags & IH_DEAD)
9030f180a7cSJohn Baldwin 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
904e0f66ef8SJohn Baldwin 	intr_event_update(ie);
905111b043cSAndriy Gapon 
906e0f66ef8SJohn Baldwin #ifdef notyet
907e0f66ef8SJohn Baldwin 	/*
908e0f66ef8SJohn Baldwin 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
909e0f66ef8SJohn Baldwin 	 * this could lead to races of stale data when servicing an
910e0f66ef8SJohn Baldwin 	 * interrupt.
911e0f66ef8SJohn Baldwin 	 */
912e0f66ef8SJohn Baldwin 	dead = 1;
913111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
914111b043cSAndriy Gapon 		if (ih->ih_handler != NULL) {
915e0f66ef8SJohn Baldwin 			dead = 0;
916e0f66ef8SJohn Baldwin 			break;
917e0f66ef8SJohn Baldwin 		}
918e0f66ef8SJohn Baldwin 	}
919e0f66ef8SJohn Baldwin 	if (dead) {
920e0f66ef8SJohn Baldwin 		ithread_destroy(ie->ie_thread);
921e0f66ef8SJohn Baldwin 		ie->ie_thread = NULL;
922e0f66ef8SJohn Baldwin 	}
923e0f66ef8SJohn Baldwin #endif
924e0f66ef8SJohn Baldwin 	mtx_unlock(&ie->ie_lock);
925b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
926b4151f71SJohn Baldwin 	return (0);
927b4151f71SJohn Baldwin }
928b4151f71SJohn Baldwin 
92982a5a275SAndriy Gapon int
93082a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie)
93182a5a275SAndriy Gapon {
93282a5a275SAndriy Gapon 	struct intr_handler *handler = (struct intr_handler *)cookie;
93382a5a275SAndriy Gapon 	struct intr_event *ie;
93482a5a275SAndriy Gapon 
93582a5a275SAndriy Gapon 	if (handler == NULL)
93682a5a275SAndriy Gapon 		return (EINVAL);
93782a5a275SAndriy Gapon 	ie = handler->ih_event;
93882a5a275SAndriy Gapon 	KASSERT(ie != NULL,
93982a5a275SAndriy Gapon 	    ("interrupt handler \"%s\" has a NULL interrupt event",
94082a5a275SAndriy Gapon 	    handler->ih_name));
94182a5a275SAndriy Gapon 	mtx_lock(&ie->ie_lock);
94282a5a275SAndriy Gapon 	handler->ih_flags |= IH_SUSP;
94382a5a275SAndriy Gapon 	intr_handler_barrier(handler);
94482a5a275SAndriy Gapon 	mtx_unlock(&ie->ie_lock);
94582a5a275SAndriy Gapon 	return (0);
94682a5a275SAndriy Gapon }
94782a5a275SAndriy Gapon 
94882a5a275SAndriy Gapon int
94982a5a275SAndriy Gapon intr_event_resume_handler(void *cookie)
95082a5a275SAndriy Gapon {
95182a5a275SAndriy Gapon 	struct intr_handler *handler = (struct intr_handler *)cookie;
95282a5a275SAndriy Gapon 	struct intr_event *ie;
95382a5a275SAndriy Gapon 
95482a5a275SAndriy Gapon 	if (handler == NULL)
95582a5a275SAndriy Gapon 		return (EINVAL);
95682a5a275SAndriy Gapon 	ie = handler->ih_event;
95782a5a275SAndriy Gapon 	KASSERT(ie != NULL,
95882a5a275SAndriy Gapon 	    ("interrupt handler \"%s\" has a NULL interrupt event",
95982a5a275SAndriy Gapon 	    handler->ih_name));
96082a5a275SAndriy Gapon 
96182a5a275SAndriy Gapon 	/*
96282a5a275SAndriy Gapon 	 * intr_handler_barrier() acts not only as a barrier,
96382a5a275SAndriy Gapon 	 * it also allows to check for any pending interrupts.
96482a5a275SAndriy Gapon 	 */
96582a5a275SAndriy Gapon 	mtx_lock(&ie->ie_lock);
96682a5a275SAndriy Gapon 	handler->ih_flags &= ~IH_SUSP;
96782a5a275SAndriy Gapon 	intr_handler_barrier(handler);
96882a5a275SAndriy Gapon 	mtx_unlock(&ie->ie_lock);
96982a5a275SAndriy Gapon 	return (0);
97082a5a275SAndriy Gapon }
97182a5a275SAndriy Gapon 
9721ee1b687SJohn Baldwin static int
9736fa041d7SWojciech Macek intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame)
9743e5da754SJohn Baldwin {
975e0f66ef8SJohn Baldwin 	struct intr_entropy entropy;
976e0f66ef8SJohn Baldwin 	struct intr_thread *it;
977b40ce416SJulian Elischer 	struct thread *td;
97804774f23SJulian Elischer 	struct thread *ctd;
9793e5da754SJohn Baldwin 
9803e5da754SJohn Baldwin 	/*
9813e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
9823e5da754SJohn Baldwin 	 */
983111b043cSAndriy Gapon 	if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
984e0f66ef8SJohn Baldwin 	    ie->ie_thread == NULL)
9853e5da754SJohn Baldwin 		return (EINVAL);
9863e5da754SJohn Baldwin 
98704774f23SJulian Elischer 	ctd = curthread;
988e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
989e0f66ef8SJohn Baldwin 	td = it->it_thread;
990e0f66ef8SJohn Baldwin 
9913e5da754SJohn Baldwin 	/*
9923e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
9933e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
9943e5da754SJohn Baldwin 	 */
995c4eb6630SGleb Smirnoff 	if (ie->ie_hflags & IH_ENTROPY) {
996e0f66ef8SJohn Baldwin 		entropy.event = (uintptr_t)ie;
997e0f66ef8SJohn Baldwin 		entropy.td = ctd;
99819fa89e9SMark Murray 		random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
9993e5da754SJohn Baldwin 	}
10003e5da754SJohn Baldwin 
1001ba3f7276SMatt Macy 	KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
10023e5da754SJohn Baldwin 
10033e5da754SJohn Baldwin 	/*
10043e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
1005982d11f8SJeff Roberson 	 * running.  Then, lock the thread and see if we actually need to
1006982d11f8SJeff Roberson 	 * put it on the runqueue.
1007283dfee9SKonstantin Belousov 	 *
1008283dfee9SKonstantin Belousov 	 * Use store_rel to arrange that the store to ih_need in
1009283dfee9SKonstantin Belousov 	 * swi_sched() is before the store to it_need and prepare for
1010283dfee9SKonstantin Belousov 	 * transfer of this order to loads in the ithread.
10113e5da754SJohn Baldwin 	 */
10123eebd44dSAlfred Perlstein 	atomic_store_rel_int(&it->it_need, 1);
1013982d11f8SJeff Roberson 	thread_lock(td);
101471fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
10156fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
10167bc13692SWojciech Macek 		it->it_waiting = 0;
10177bc13692SWojciech Macek 		if (PMC_HOOK_INSTALLED_ANY())
10187bc13692SWojciech Macek 			PMC_SOFT_CALL_INTR_HLPR(schedule, frame);
10196fa041d7SWojciech Macek #endif
1020fc2e87beSMatt Macy 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
10217ab24ea3SJulian Elischer 		    td->td_name);
102271fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
1023ed998d1cSJohn Baldwin 		sched_wakeup(td, SRQ_INTR);
10243e5da754SJohn Baldwin 	} else {
10256fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
10267bc13692SWojciech Macek 		it->it_waiting++;
10277bc13692SWojciech Macek 		if (PMC_HOOK_INSTALLED_ANY() &&
10287bc13692SWojciech Macek 		    (it->it_waiting >= intr_hwpmc_waiting_report_threshold))
10297bc13692SWojciech Macek 			PMC_SOFT_CALL_INTR_HLPR(waiting, frame);
10306fa041d7SWojciech Macek #endif
1031e0f66ef8SJohn Baldwin 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1032fa2528acSAlex Richardson 		    __func__, td->td_proc->p_pid, td->td_name, it->it_need, TD_GET_STATE(td));
1033982d11f8SJeff Roberson 		thread_unlock(td);
103461a74c5cSJeff Roberson 	}
10353e5da754SJohn Baldwin 
10363e5da754SJohn Baldwin 	return (0);
10373e5da754SJohn Baldwin }
10383e5da754SJohn Baldwin 
1039fe486a37SJohn Baldwin /*
1040e84bcd84SRobert Watson  * Allow interrupt event binding for software interrupt handlers -- a no-op,
1041e84bcd84SRobert Watson  * since interrupts are generated in software rather than being directed by
1042e84bcd84SRobert Watson  * a PIC.
1043e84bcd84SRobert Watson  */
1044e84bcd84SRobert Watson static int
1045066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu)
1046e84bcd84SRobert Watson {
1047e84bcd84SRobert Watson 
1048e84bcd84SRobert Watson 	return (0);
1049e84bcd84SRobert Watson }
1050e84bcd84SRobert Watson 
1051e84bcd84SRobert Watson /*
1052fe486a37SJohn Baldwin  * Add a software interrupt handler to a specified event.  If a given event
1053fe486a37SJohn Baldwin  * is not specified, then a new event is created.
1054fe486a37SJohn Baldwin  */
10553e5da754SJohn Baldwin int
1056e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1057b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
10588088699fSJohn Baldwin {
1059e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1060aba10e13SAlexander Motin 	int error = 0;
10618088699fSJohn Baldwin 
1062bafe5a31SPaolo Pisati 	if (flags & INTR_ENTROPY)
10633e5da754SJohn Baldwin 		return (EINVAL);
10643e5da754SJohn Baldwin 
1065e0f66ef8SJohn Baldwin 	ie = (eventp != NULL) ? *eventp : NULL;
10668088699fSJohn Baldwin 
1067e0f66ef8SJohn Baldwin 	if (ie != NULL) {
1068e0f66ef8SJohn Baldwin 		if (!(ie->ie_flags & IE_SOFT))
10693e5da754SJohn Baldwin 			return (EINVAL);
10703e5da754SJohn Baldwin 	} else {
10719b33b154SJeff Roberson 		error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1072e84bcd84SRobert Watson 		    NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
10738088699fSJohn Baldwin 		if (error)
1074b4151f71SJohn Baldwin 			return (error);
1075e0f66ef8SJohn Baldwin 		if (eventp != NULL)
1076e0f66ef8SJohn Baldwin 			*eventp = ie;
10778088699fSJohn Baldwin 	}
1078aba10e13SAlexander Motin 	if (handler != NULL) {
10798d809d50SJeff Roberson 		error = intr_event_add_handler(ie, name, NULL, handler, arg,
1080d3305205SJohn Baldwin 		    PI_SWI(pri), flags, cookiep);
1081aba10e13SAlexander Motin 	}
10828d809d50SJeff Roberson 	return (error);
10838088699fSJohn Baldwin }
10848088699fSJohn Baldwin 
10851931cf94SJohn Baldwin /*
1086e0f66ef8SJohn Baldwin  * Schedule a software interrupt thread.
10871931cf94SJohn Baldwin  */
10881931cf94SJohn Baldwin void
1089b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
10901931cf94SJohn Baldwin {
1091e0f66ef8SJohn Baldwin 	struct intr_handler *ih = (struct intr_handler *)cookie;
1092e0f66ef8SJohn Baldwin 	struct intr_event *ie = ih->ih_event;
1093d95dca1dSJohn Baldwin 	struct intr_entropy entropy;
1094ba3f7276SMatt Macy 	int error __unused;
10958088699fSJohn Baldwin 
1096e0f66ef8SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1097e0f66ef8SJohn Baldwin 	    ih->ih_need);
10981931cf94SJohn Baldwin 
1099aba10e13SAlexander Motin 	if ((flags & SWI_FROMNMI) == 0) {
1100d95dca1dSJohn Baldwin 		entropy.event = (uintptr_t)ih;
1101d95dca1dSJohn Baldwin 		entropy.td = curthread;
110219fa89e9SMark Murray 		random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1103aba10e13SAlexander Motin 	}
1104d95dca1dSJohn Baldwin 
11051931cf94SJohn Baldwin 	/*
11063e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
11073e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
11083e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
11091931cf94SJohn Baldwin 	 */
1110283dfee9SKonstantin Belousov 	ih->ih_need = 1;
11111ca2c018SBruce Evans 
1112aba10e13SAlexander Motin 	if (flags & SWI_DELAY)
1113aba10e13SAlexander Motin 		return;
1114aba10e13SAlexander Motin 
1115aba10e13SAlexander Motin 	if (flags & SWI_FROMNMI) {
1116aba10e13SAlexander Motin #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1117aba10e13SAlexander Motin 		KASSERT(ie == clk_intr_event,
1118aba10e13SAlexander Motin 		    ("SWI_FROMNMI used not with clk_intr_event"));
1119aba10e13SAlexander Motin 		ipi_self_from_nmi(IPI_SWI);
1120aba10e13SAlexander Motin #endif
1121aba10e13SAlexander Motin 	} else {
112283c9dea1SGleb Smirnoff 		VM_CNT_INC(v_soft);
11236fa041d7SWojciech Macek 		error = intr_event_schedule_thread(ie, NULL);
11243e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
11258088699fSJohn Baldwin 	}
11268088699fSJohn Baldwin }
11278088699fSJohn Baldwin 
1128fe486a37SJohn Baldwin /*
1129fe486a37SJohn Baldwin  * Remove a software interrupt handler.  Currently this code does not
1130fe486a37SJohn Baldwin  * remove the associated interrupt event if it becomes empty.  Calling code
1131fe486a37SJohn Baldwin  * may do so manually via intr_event_destroy(), but that's not really
1132fe486a37SJohn Baldwin  * an optimal interface.
1133fe486a37SJohn Baldwin  */
1134fe486a37SJohn Baldwin int
1135fe486a37SJohn Baldwin swi_remove(void *cookie)
1136fe486a37SJohn Baldwin {
1137fe486a37SJohn Baldwin 
1138fe486a37SJohn Baldwin 	return (intr_event_remove_handler(cookie));
1139fe486a37SJohn Baldwin }
1140fe486a37SJohn Baldwin 
1141111b043cSAndriy Gapon static void
114237e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1143e0f66ef8SJohn Baldwin {
1144111b043cSAndriy Gapon 	struct intr_handler *ih, *ihn, *ihp;
1145e0f66ef8SJohn Baldwin 
1146111b043cSAndriy Gapon 	ihp = NULL;
1147111b043cSAndriy Gapon 	CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1148e0f66ef8SJohn Baldwin 		/*
1149e0f66ef8SJohn Baldwin 		 * If this handler is marked for death, remove it from
1150e0f66ef8SJohn Baldwin 		 * the list of handlers and wake up the sleeper.
1151e0f66ef8SJohn Baldwin 		 */
1152e0f66ef8SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
1153e0f66ef8SJohn Baldwin 			mtx_lock(&ie->ie_lock);
1154111b043cSAndriy Gapon 			if (ihp == NULL)
1155111b043cSAndriy Gapon 				CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1156111b043cSAndriy Gapon 			else
1157111b043cSAndriy Gapon 				CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1158e0f66ef8SJohn Baldwin 			ih->ih_flags &= ~IH_DEAD;
1159e0f66ef8SJohn Baldwin 			wakeup(ih);
1160e0f66ef8SJohn Baldwin 			mtx_unlock(&ie->ie_lock);
1161e0f66ef8SJohn Baldwin 			continue;
1162e0f66ef8SJohn Baldwin 		}
1163e0f66ef8SJohn Baldwin 
1164111b043cSAndriy Gapon 		/*
1165111b043cSAndriy Gapon 		 * Now that we know that the current element won't be removed
1166111b043cSAndriy Gapon 		 * update the previous element.
1167111b043cSAndriy Gapon 		 */
1168111b043cSAndriy Gapon 		ihp = ih;
1169111b043cSAndriy Gapon 
117082a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_CHANGED) != 0) {
117182a5a275SAndriy Gapon 			mtx_lock(&ie->ie_lock);
117282a5a275SAndriy Gapon 			ih->ih_flags &= ~IH_CHANGED;
117382a5a275SAndriy Gapon 			wakeup(ih);
117482a5a275SAndriy Gapon 			mtx_unlock(&ie->ie_lock);
117582a5a275SAndriy Gapon 		}
117682a5a275SAndriy Gapon 
1177f2d619c8SPaolo Pisati 		/* Skip filter only handlers */
1178f2d619c8SPaolo Pisati 		if (ih->ih_handler == NULL)
1179f2d619c8SPaolo Pisati 			continue;
1180f2d619c8SPaolo Pisati 
118182a5a275SAndriy Gapon 		/* Skip suspended handlers */
118282a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_SUSP) != 0)
118382a5a275SAndriy Gapon 			continue;
118482a5a275SAndriy Gapon 
1185e0f66ef8SJohn Baldwin 		/*
1186e0f66ef8SJohn Baldwin 		 * For software interrupt threads, we only execute
1187e0f66ef8SJohn Baldwin 		 * handlers that have their need flag set.  Hardware
1188e0f66ef8SJohn Baldwin 		 * interrupt threads always invoke all of their handlers.
11891b79b949SKirk McKusick 		 *
11901b79b949SKirk McKusick 		 * ih_need can only be 0 or 1.  Failed cmpset below
11911b79b949SKirk McKusick 		 * means that there is no request to execute handlers,
11921b79b949SKirk McKusick 		 * so a retry of the cmpset is not needed.
1193e0f66ef8SJohn Baldwin 		 */
11941b79b949SKirk McKusick 		if ((ie->ie_flags & IE_SOFT) != 0 &&
11951b79b949SKirk McKusick 		    atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1196e0f66ef8SJohn Baldwin 			continue;
1197e0f66ef8SJohn Baldwin 
1198e0f66ef8SJohn Baldwin 		/* Execute this handler. */
1199e0f66ef8SJohn Baldwin 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1200bafe5a31SPaolo Pisati 		    __func__, p->p_pid, (void *)ih->ih_handler,
1201bafe5a31SPaolo Pisati 		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1202e0f66ef8SJohn Baldwin 
1203e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1204e0f66ef8SJohn Baldwin 			mtx_lock(&Giant);
1205e0f66ef8SJohn Baldwin 		ih->ih_handler(ih->ih_argument);
1206e0f66ef8SJohn Baldwin 		if (!(ih->ih_flags & IH_MPSAFE))
1207e0f66ef8SJohn Baldwin 			mtx_unlock(&Giant);
1208e0f66ef8SJohn Baldwin 	}
120937e9511fSJohn Baldwin }
121037e9511fSJohn Baldwin 
121137e9511fSJohn Baldwin static void
121237e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie)
121337e9511fSJohn Baldwin {
121437e9511fSJohn Baldwin 
121537e9511fSJohn Baldwin 	/* Interrupt handlers should not sleep. */
121637e9511fSJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
121737e9511fSJohn Baldwin 		THREAD_NO_SLEEPING();
121837e9511fSJohn Baldwin 	intr_event_execute_handlers(p, ie);
1219e0f66ef8SJohn Baldwin 	if (!(ie->ie_flags & IE_SOFT))
1220e0f66ef8SJohn Baldwin 		THREAD_SLEEPING_OK();
1221e0f66ef8SJohn Baldwin 
1222e0f66ef8SJohn Baldwin 	/*
1223e0f66ef8SJohn Baldwin 	 * Interrupt storm handling:
1224e0f66ef8SJohn Baldwin 	 *
1225e0f66ef8SJohn Baldwin 	 * If this interrupt source is currently storming, then throttle
1226e0f66ef8SJohn Baldwin 	 * it to only fire the handler once  per clock tick.
1227e0f66ef8SJohn Baldwin 	 *
1228e0f66ef8SJohn Baldwin 	 * If this interrupt source is not currently storming, but the
1229e0f66ef8SJohn Baldwin 	 * number of back to back interrupts exceeds the storm threshold,
1230e0f66ef8SJohn Baldwin 	 * then enter storming mode.
1231e0f66ef8SJohn Baldwin 	 */
1232e41bcf3cSJohn Baldwin 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1233e41bcf3cSJohn Baldwin 	    !(ie->ie_flags & IE_SOFT)) {
12340ae62c18SNate Lawson 		/* Report the message only once every second. */
12350ae62c18SNate Lawson 		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1236e0f66ef8SJohn Baldwin 			printf(
12370ae62c18SNate Lawson 	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1238e0f66ef8SJohn Baldwin 			    ie->ie_name);
1239e0f66ef8SJohn Baldwin 		}
1240e41bcf3cSJohn Baldwin 		pause("istorm", 1);
1241e0f66ef8SJohn Baldwin 	} else
1242e0f66ef8SJohn Baldwin 		ie->ie_count++;
1243e0f66ef8SJohn Baldwin 
1244e0f66ef8SJohn Baldwin 	/*
1245e0f66ef8SJohn Baldwin 	 * Now that all the handlers have had a chance to run, reenable
1246e0f66ef8SJohn Baldwin 	 * the interrupt source.
1247e0f66ef8SJohn Baldwin 	 */
12481ee1b687SJohn Baldwin 	if (ie->ie_post_ithread != NULL)
12491ee1b687SJohn Baldwin 		ie->ie_post_ithread(ie->ie_source);
1250e0f66ef8SJohn Baldwin }
1251e0f66ef8SJohn Baldwin 
12528088699fSJohn Baldwin /*
1253b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
12548088699fSJohn Baldwin  */
125537c84183SPoul-Henning Kamp static void
1256b4151f71SJohn Baldwin ithread_loop(void *arg)
12578088699fSJohn Baldwin {
1258511d1afbSGleb Smirnoff 	struct epoch_tracker et;
1259e0f66ef8SJohn Baldwin 	struct intr_thread *ithd;
1260e0f66ef8SJohn Baldwin 	struct intr_event *ie;
1261b40ce416SJulian Elischer 	struct thread *td;
1262b4151f71SJohn Baldwin 	struct proc *p;
1263511d1afbSGleb Smirnoff 	int wake, epoch_count;
1264f912e8f2SHans Petter Selasky 	bool needs_epoch;
12658088699fSJohn Baldwin 
1266b40ce416SJulian Elischer 	td = curthread;
1267b40ce416SJulian Elischer 	p = td->td_proc;
1268e0f66ef8SJohn Baldwin 	ithd = (struct intr_thread *)arg;
1269e0f66ef8SJohn Baldwin 	KASSERT(ithd->it_thread == td,
127091f91617SDavid E. O'Brien 	    ("%s: ithread and proc linkage out of sync", __func__));
1271e0f66ef8SJohn Baldwin 	ie = ithd->it_event;
1272e0f66ef8SJohn Baldwin 	ie->ie_count = 0;
1273e4cd31ddSJeff Roberson 	wake = 0;
12748088699fSJohn Baldwin 
12758088699fSJohn Baldwin 	/*
12768088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
12778088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
12788088699fSJohn Baldwin 	 */
12798088699fSJohn Baldwin 	for (;;) {
1280b4151f71SJohn Baldwin 		/*
1281b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
1282b4151f71SJohn Baldwin 		 */
1283b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
1284e0f66ef8SJohn Baldwin 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
12857ab24ea3SJulian Elischer 			    p->p_pid, td->td_name);
1286b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
1287ca9a0ddfSJulian Elischer 			kthread_exit();
1288b4151f71SJohn Baldwin 		}
1289b4151f71SJohn Baldwin 
1290e0f66ef8SJohn Baldwin 		/*
1291e0f66ef8SJohn Baldwin 		 * Service interrupts.  If another interrupt arrives while
1292e0f66ef8SJohn Baldwin 		 * we are running, it will set it_need to note that we
1293e0f66ef8SJohn Baldwin 		 * should make another pass.
1294283dfee9SKonstantin Belousov 		 *
1295283dfee9SKonstantin Belousov 		 * The load_acq part of the following cmpset ensures
1296283dfee9SKonstantin Belousov 		 * that the load of ih_need in ithread_execute_handlers()
1297283dfee9SKonstantin Belousov 		 * is ordered after the load of it_need here.
1298e0f66ef8SJohn Baldwin 		 */
1299f912e8f2SHans Petter Selasky 		needs_epoch =
1300f912e8f2SHans Petter Selasky 		    (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1301f912e8f2SHans Petter Selasky 		if (needs_epoch) {
1302511d1afbSGleb Smirnoff 			epoch_count = 0;
1303511d1afbSGleb Smirnoff 			NET_EPOCH_ENTER(et);
1304511d1afbSGleb Smirnoff 		}
1305511d1afbSGleb Smirnoff 		while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1306e0f66ef8SJohn Baldwin 			ithread_execute_handlers(p, ie);
1307f912e8f2SHans Petter Selasky 			if (needs_epoch &&
1308511d1afbSGleb Smirnoff 			    ++epoch_count >= intr_epoch_batch) {
1309511d1afbSGleb Smirnoff 				NET_EPOCH_EXIT(et);
1310511d1afbSGleb Smirnoff 				epoch_count = 0;
1311511d1afbSGleb Smirnoff 				NET_EPOCH_ENTER(et);
1312511d1afbSGleb Smirnoff 			}
1313511d1afbSGleb Smirnoff 		}
1314f912e8f2SHans Petter Selasky 		if (needs_epoch)
1315511d1afbSGleb Smirnoff 			NET_EPOCH_EXIT(et);
13167870c3c6SJohn Baldwin 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
13177870c3c6SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
13188088699fSJohn Baldwin 
13198088699fSJohn Baldwin 		/*
13208088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
13218088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
13228088699fSJohn Baldwin 		 * set again, so we have to check it again.
13238088699fSJohn Baldwin 		 */
1324982d11f8SJeff Roberson 		thread_lock(td);
132503bbcb2fSKonstantin Belousov 		if (atomic_load_acq_int(&ithd->it_need) == 0 &&
132603bbcb2fSKonstantin Belousov 		    (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
13277870c3c6SJohn Baldwin 			TD_SET_IWAIT(td);
1328e0f66ef8SJohn Baldwin 			ie->ie_count = 0;
1329686bcb5cSJeff Roberson 			mi_switch(SW_VOL | SWT_IWAIT);
1330686bcb5cSJeff Roberson 		} else {
1331e4cd31ddSJeff Roberson 			if (ithd->it_flags & IT_WAIT) {
1332e4cd31ddSJeff Roberson 				wake = 1;
1333e4cd31ddSJeff Roberson 				ithd->it_flags &= ~IT_WAIT;
1334e4cd31ddSJeff Roberson 			}
1335982d11f8SJeff Roberson 			thread_unlock(td);
1336686bcb5cSJeff Roberson 		}
1337e4cd31ddSJeff Roberson 		if (wake) {
1338e4cd31ddSJeff Roberson 			wakeup(ithd);
1339e4cd31ddSJeff Roberson 			wake = 0;
1340e4cd31ddSJeff Roberson 		}
13418088699fSJohn Baldwin 	}
13421931cf94SJohn Baldwin }
13431ee1b687SJohn Baldwin 
13441ee1b687SJohn Baldwin /*
13451ee1b687SJohn Baldwin  * Main interrupt handling body.
13461ee1b687SJohn Baldwin  *
13471ee1b687SJohn Baldwin  * Input:
13481ee1b687SJohn Baldwin  * o ie:                        the event connected to this interrupt.
13491ee1b687SJohn Baldwin  * o frame:                     some archs (i.e. i386) pass a frame to some.
13501ee1b687SJohn Baldwin  *                              handlers as their main argument.
13511ee1b687SJohn Baldwin  * Return value:
13521ee1b687SJohn Baldwin  * o 0:                         everything ok.
13531ee1b687SJohn Baldwin  * o EINVAL:                    stray interrupt.
13541ee1b687SJohn Baldwin  */
13551ee1b687SJohn Baldwin int
13561ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame)
13571ee1b687SJohn Baldwin {
13581ee1b687SJohn Baldwin 	struct intr_handler *ih;
13591f255bd3SAlexander Motin 	struct trapframe *oldframe;
13601ee1b687SJohn Baldwin 	struct thread *td;
1361e0fa977eSAndriy Gapon 	int phase;
136282a5a275SAndriy Gapon 	int ret;
136382a5a275SAndriy Gapon 	bool filter, thread;
13641ee1b687SJohn Baldwin 
13651ee1b687SJohn Baldwin 	td = curthread;
13661ee1b687SJohn Baldwin 
1367b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF
1368b7627840SKonstantin Belousov 	intr_prof_stack_use(td, frame);
1369b7627840SKonstantin Belousov #endif
1370b7627840SKonstantin Belousov 
13711ee1b687SJohn Baldwin 	/* An interrupt with no event or handlers is a stray interrupt. */
1372111b043cSAndriy Gapon 	if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
13731ee1b687SJohn Baldwin 		return (EINVAL);
13741ee1b687SJohn Baldwin 
13751ee1b687SJohn Baldwin 	/*
13761ee1b687SJohn Baldwin 	 * Execute fast interrupt handlers directly.
13771ee1b687SJohn Baldwin 	 * To support clock handlers, if a handler registers
13781ee1b687SJohn Baldwin 	 * with a NULL argument, then we pass it a pointer to
13791ee1b687SJohn Baldwin 	 * a trapframe as its argument.
13801ee1b687SJohn Baldwin 	 */
13811ee1b687SJohn Baldwin 	td->td_intr_nesting_level++;
138282a5a275SAndriy Gapon 	filter = false;
138382a5a275SAndriy Gapon 	thread = false;
13841ee1b687SJohn Baldwin 	ret = 0;
13851ee1b687SJohn Baldwin 	critical_enter();
13861f255bd3SAlexander Motin 	oldframe = td->td_intr_frame;
13871f255bd3SAlexander Motin 	td->td_intr_frame = frame;
1388111b043cSAndriy Gapon 
1389e0fa977eSAndriy Gapon 	phase = ie->ie_phase;
1390e0fa977eSAndriy Gapon 	atomic_add_int(&ie->ie_active[phase], 1);
1391e0fa977eSAndriy Gapon 
1392e0fa977eSAndriy Gapon 	/*
1393e0fa977eSAndriy Gapon 	 * This fence is required to ensure that no later loads are
1394e0fa977eSAndriy Gapon 	 * re-ordered before the ie_active store.
1395e0fa977eSAndriy Gapon 	 */
1396e0fa977eSAndriy Gapon 	atomic_thread_fence_seq_cst();
1397e0fa977eSAndriy Gapon 
1398111b043cSAndriy Gapon 	CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
139982a5a275SAndriy Gapon 		if ((ih->ih_flags & IH_SUSP) != 0)
140082a5a275SAndriy Gapon 			continue;
1401aba10e13SAlexander Motin 		if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1402aba10e13SAlexander Motin 			continue;
14031ee1b687SJohn Baldwin 		if (ih->ih_filter == NULL) {
140482a5a275SAndriy Gapon 			thread = true;
14051ee1b687SJohn Baldwin 			continue;
14061ee1b687SJohn Baldwin 		}
14071ee1b687SJohn Baldwin 		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
14081ee1b687SJohn Baldwin 		    ih->ih_filter, ih->ih_argument == NULL ? frame :
14091ee1b687SJohn Baldwin 		    ih->ih_argument, ih->ih_name);
14101ee1b687SJohn Baldwin 		if (ih->ih_argument == NULL)
14111ee1b687SJohn Baldwin 			ret = ih->ih_filter(frame);
14121ee1b687SJohn Baldwin 		else
14131ee1b687SJohn Baldwin 			ret = ih->ih_filter(ih->ih_argument);
14146fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
14156fa041d7SWojciech Macek 		PMC_SOFT_CALL_TF( , , intr, all, frame);
14166fa041d7SWojciech Macek #endif
141789fc20ccSAndriy Gapon 		KASSERT(ret == FILTER_STRAY ||
141889fc20ccSAndriy Gapon 		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
141989fc20ccSAndriy Gapon 		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
142089fc20ccSAndriy Gapon 		    ("%s: incorrect return value %#x from %s", __func__, ret,
142189fc20ccSAndriy Gapon 		    ih->ih_name));
142282a5a275SAndriy Gapon 		filter = filter || ret == FILTER_HANDLED;
14236fa041d7SWojciech Macek #ifdef HWPMC_HOOKS
14246fa041d7SWojciech Macek 		if (ret & FILTER_SCHEDULE_THREAD)
14256fa041d7SWojciech Macek 			PMC_SOFT_CALL_TF( , , intr, ithread, frame);
14266fa041d7SWojciech Macek 		else if (ret & FILTER_HANDLED)
14276fa041d7SWojciech Macek 			PMC_SOFT_CALL_TF( , , intr, filter, frame);
14286fa041d7SWojciech Macek 		else if (ret == FILTER_STRAY)
14296fa041d7SWojciech Macek 			PMC_SOFT_CALL_TF( , , intr, stray, frame);
14306fa041d7SWojciech Macek #endif
143189fc20ccSAndriy Gapon 
14321ee1b687SJohn Baldwin 		/*
14331ee1b687SJohn Baldwin 		 * Wrapper handler special handling:
14341ee1b687SJohn Baldwin 		 *
14351ee1b687SJohn Baldwin 		 * in some particular cases (like pccard and pccbb),
14361ee1b687SJohn Baldwin 		 * the _real_ device handler is wrapped in a couple of
14371ee1b687SJohn Baldwin 		 * functions - a filter wrapper and an ithread wrapper.
14381ee1b687SJohn Baldwin 		 * In this case (and just in this case), the filter wrapper
14391ee1b687SJohn Baldwin 		 * could ask the system to schedule the ithread and mask
14401ee1b687SJohn Baldwin 		 * the interrupt source if the wrapped handler is composed
14411ee1b687SJohn Baldwin 		 * of just an ithread handler.
14421ee1b687SJohn Baldwin 		 *
14431ee1b687SJohn Baldwin 		 * TODO: write a generic wrapper to avoid people rolling
144482a5a275SAndriy Gapon 		 * their own.
14451ee1b687SJohn Baldwin 		 */
14461ee1b687SJohn Baldwin 		if (!thread) {
14471ee1b687SJohn Baldwin 			if (ret == FILTER_SCHEDULE_THREAD)
144882a5a275SAndriy Gapon 				thread = true;
14491ee1b687SJohn Baldwin 		}
14501ee1b687SJohn Baldwin 	}
1451e0fa977eSAndriy Gapon 	atomic_add_rel_int(&ie->ie_active[phase], -1);
1452e0fa977eSAndriy Gapon 
14531f255bd3SAlexander Motin 	td->td_intr_frame = oldframe;
14541ee1b687SJohn Baldwin 
14551ee1b687SJohn Baldwin 	if (thread) {
14561ee1b687SJohn Baldwin 		if (ie->ie_pre_ithread != NULL)
14571ee1b687SJohn Baldwin 			ie->ie_pre_ithread(ie->ie_source);
14581ee1b687SJohn Baldwin 	} else {
14591ee1b687SJohn Baldwin 		if (ie->ie_post_filter != NULL)
14601ee1b687SJohn Baldwin 			ie->ie_post_filter(ie->ie_source);
14611ee1b687SJohn Baldwin 	}
14621ee1b687SJohn Baldwin 
14631ee1b687SJohn Baldwin 	/* Schedule the ithread if needed. */
14641ee1b687SJohn Baldwin 	if (thread) {
1465ba3f7276SMatt Macy 		int error __unused;
1466ba3f7276SMatt Macy 
14676fa041d7SWojciech Macek 		error =  intr_event_schedule_thread(ie, frame);
14681ee1b687SJohn Baldwin 		KASSERT(error == 0, ("bad stray interrupt"));
14691ee1b687SJohn Baldwin 	}
14701ee1b687SJohn Baldwin 	critical_exit();
14711ee1b687SJohn Baldwin 	td->td_intr_nesting_level--;
147282a5a275SAndriy Gapon #ifdef notyet
147382a5a275SAndriy Gapon 	/* The interrupt is not aknowledged by any filter and has no ithread. */
147482a5a275SAndriy Gapon 	if (!thread && !filter)
147582a5a275SAndriy Gapon 		return (EINVAL);
147682a5a275SAndriy Gapon #endif
14771ee1b687SJohn Baldwin 	return (0);
14781ee1b687SJohn Baldwin }
14791931cf94SJohn Baldwin 
14808b201c42SJohn Baldwin #ifdef DDB
14818b201c42SJohn Baldwin /*
14828b201c42SJohn Baldwin  * Dump details about an interrupt handler
14838b201c42SJohn Baldwin  */
14848b201c42SJohn Baldwin static void
1485e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih)
14868b201c42SJohn Baldwin {
14878b201c42SJohn Baldwin 	int comma;
14888b201c42SJohn Baldwin 
14898b201c42SJohn Baldwin 	db_printf("\t%-10s ", ih->ih_name);
14908b201c42SJohn Baldwin 	switch (ih->ih_pri) {
14918b201c42SJohn Baldwin 	case PI_REALTIME:
14928b201c42SJohn Baldwin 		db_printf("CLK ");
14938b201c42SJohn Baldwin 		break;
14942cf78708SJohn Baldwin 	case PI_INTR:
14952cf78708SJohn Baldwin 		db_printf("INTR");
14968b201c42SJohn Baldwin 		break;
14978b201c42SJohn Baldwin 	default:
14988b201c42SJohn Baldwin 		if (ih->ih_pri >= PI_SOFT)
14998b201c42SJohn Baldwin 			db_printf("SWI ");
15008b201c42SJohn Baldwin 		else
15018b201c42SJohn Baldwin 			db_printf("%4u", ih->ih_pri);
15028b201c42SJohn Baldwin 		break;
15038b201c42SJohn Baldwin 	}
15048b201c42SJohn Baldwin 	db_printf(" ");
1505b887a155SKonstantin Belousov 	if (ih->ih_filter != NULL) {
1506b887a155SKonstantin Belousov 		db_printf("[F]");
1507b887a155SKonstantin Belousov 		db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1508b887a155SKonstantin Belousov 	}
1509b887a155SKonstantin Belousov 	if (ih->ih_handler != NULL) {
1510b887a155SKonstantin Belousov 		if (ih->ih_filter != NULL)
1511b887a155SKonstantin Belousov 			db_printf(",");
1512b887a155SKonstantin Belousov 		db_printf("[H]");
15138b201c42SJohn Baldwin 		db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1514b887a155SKonstantin Belousov 	}
15158b201c42SJohn Baldwin 	db_printf("(%p)", ih->ih_argument);
15168b201c42SJohn Baldwin 	if (ih->ih_need ||
1517ef544f63SPaolo Pisati 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
15188b201c42SJohn Baldwin 	    IH_MPSAFE)) != 0) {
15198b201c42SJohn Baldwin 		db_printf(" {");
15208b201c42SJohn Baldwin 		comma = 0;
15218b201c42SJohn Baldwin 		if (ih->ih_flags & IH_EXCLUSIVE) {
15228b201c42SJohn Baldwin 			if (comma)
15238b201c42SJohn Baldwin 				db_printf(", ");
15248b201c42SJohn Baldwin 			db_printf("EXCL");
15258b201c42SJohn Baldwin 			comma = 1;
15268b201c42SJohn Baldwin 		}
15278b201c42SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY) {
15288b201c42SJohn Baldwin 			if (comma)
15298b201c42SJohn Baldwin 				db_printf(", ");
15308b201c42SJohn Baldwin 			db_printf("ENTROPY");
15318b201c42SJohn Baldwin 			comma = 1;
15328b201c42SJohn Baldwin 		}
15338b201c42SJohn Baldwin 		if (ih->ih_flags & IH_DEAD) {
15348b201c42SJohn Baldwin 			if (comma)
15358b201c42SJohn Baldwin 				db_printf(", ");
15368b201c42SJohn Baldwin 			db_printf("DEAD");
15378b201c42SJohn Baldwin 			comma = 1;
15388b201c42SJohn Baldwin 		}
15398b201c42SJohn Baldwin 		if (ih->ih_flags & IH_MPSAFE) {
15408b201c42SJohn Baldwin 			if (comma)
15418b201c42SJohn Baldwin 				db_printf(", ");
15428b201c42SJohn Baldwin 			db_printf("MPSAFE");
15438b201c42SJohn Baldwin 			comma = 1;
15448b201c42SJohn Baldwin 		}
15458b201c42SJohn Baldwin 		if (ih->ih_need) {
15468b201c42SJohn Baldwin 			if (comma)
15478b201c42SJohn Baldwin 				db_printf(", ");
15488b201c42SJohn Baldwin 			db_printf("NEED");
15498b201c42SJohn Baldwin 		}
15508b201c42SJohn Baldwin 		db_printf("}");
15518b201c42SJohn Baldwin 	}
15528b201c42SJohn Baldwin 	db_printf("\n");
15538b201c42SJohn Baldwin }
15548b201c42SJohn Baldwin 
15558b201c42SJohn Baldwin /*
1556e0f66ef8SJohn Baldwin  * Dump details about a event.
15578b201c42SJohn Baldwin  */
15588b201c42SJohn Baldwin void
1559e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers)
15608b201c42SJohn Baldwin {
1561e0f66ef8SJohn Baldwin 	struct intr_handler *ih;
1562e0f66ef8SJohn Baldwin 	struct intr_thread *it;
15638b201c42SJohn Baldwin 	int comma;
15648b201c42SJohn Baldwin 
1565e0f66ef8SJohn Baldwin 	db_printf("%s ", ie->ie_fullname);
1566e0f66ef8SJohn Baldwin 	it = ie->ie_thread;
1567e0f66ef8SJohn Baldwin 	if (it != NULL)
1568e0f66ef8SJohn Baldwin 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1569e0f66ef8SJohn Baldwin 	else
1570e0f66ef8SJohn Baldwin 		db_printf("(no thread)");
1571c4eb6630SGleb Smirnoff 	if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1572e0f66ef8SJohn Baldwin 	    (it != NULL && it->it_need)) {
15738b201c42SJohn Baldwin 		db_printf(" {");
15748b201c42SJohn Baldwin 		comma = 0;
1575e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_SOFT) {
15768b201c42SJohn Baldwin 			db_printf("SOFT");
15778b201c42SJohn Baldwin 			comma = 1;
15788b201c42SJohn Baldwin 		}
1579e0f66ef8SJohn Baldwin 		if (ie->ie_flags & IE_ADDING_THREAD) {
15808b201c42SJohn Baldwin 			if (comma)
15818b201c42SJohn Baldwin 				db_printf(", ");
1582e0f66ef8SJohn Baldwin 			db_printf("ADDING_THREAD");
15838b201c42SJohn Baldwin 			comma = 1;
15848b201c42SJohn Baldwin 		}
1585e0f66ef8SJohn Baldwin 		if (it != NULL && it->it_need) {
15868b201c42SJohn Baldwin 			if (comma)
15878b201c42SJohn Baldwin 				db_printf(", ");
15888b201c42SJohn Baldwin 			db_printf("NEED");
15898b201c42SJohn Baldwin 		}
15908b201c42SJohn Baldwin 		db_printf("}");
15918b201c42SJohn Baldwin 	}
15928b201c42SJohn Baldwin 	db_printf("\n");
15938b201c42SJohn Baldwin 
15948b201c42SJohn Baldwin 	if (handlers)
1595111b043cSAndriy Gapon 		CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
15968b201c42SJohn Baldwin 		    db_dump_intrhand(ih);
15978b201c42SJohn Baldwin }
1598e0f66ef8SJohn Baldwin 
1599e0f66ef8SJohn Baldwin /*
1600e0f66ef8SJohn Baldwin  * Dump data about interrupt handlers
1601e0f66ef8SJohn Baldwin  */
1602*c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(intr, db_show_intr, DB_CMD_MEMSAFE)
1603e0f66ef8SJohn Baldwin {
1604e0f66ef8SJohn Baldwin 	struct intr_event *ie;
160519e9205aSJohn Baldwin 	int all, verbose;
1606e0f66ef8SJohn Baldwin 
1607dc15eac0SEd Schouten 	verbose = strchr(modif, 'v') != NULL;
1608dc15eac0SEd Schouten 	all = strchr(modif, 'a') != NULL;
1609e0f66ef8SJohn Baldwin 	TAILQ_FOREACH(ie, &event_list, ie_list) {
1610111b043cSAndriy Gapon 		if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1611e0f66ef8SJohn Baldwin 			continue;
1612e0f66ef8SJohn Baldwin 		db_dump_intr_event(ie, verbose);
161319e9205aSJohn Baldwin 		if (db_pager_quit)
161419e9205aSJohn Baldwin 			break;
1615e0f66ef8SJohn Baldwin 	}
1616e0f66ef8SJohn Baldwin }
16178b201c42SJohn Baldwin #endif /* DDB */
16188b201c42SJohn Baldwin 
1619b4151f71SJohn Baldwin /*
16208088699fSJohn Baldwin  * Start standard software interrupt threads
16211931cf94SJohn Baldwin  */
16221931cf94SJohn Baldwin static void
1623b4151f71SJohn Baldwin start_softintr(void *dummy)
16241931cf94SJohn Baldwin {
1625b4151f71SJohn Baldwin 
1626aba10e13SAlexander Motin 	if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1627aba10e13SAlexander Motin 	    INTR_MPSAFE, NULL))
1628aba10e13SAlexander Motin 		panic("died while creating clk swi ithread");
16291931cf94SJohn Baldwin }
1630237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1631237fdd78SRobert Watson     NULL);
16321931cf94SJohn Baldwin 
1633d279178dSThomas Moestl /*
1634d279178dSThomas Moestl  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1635d279178dSThomas Moestl  * The data for this machine dependent, and the declarations are in machine
1636d279178dSThomas Moestl  * dependent code.  The layout of intrnames and intrcnt however is machine
1637d279178dSThomas Moestl  * independent.
1638d279178dSThomas Moestl  *
1639d279178dSThomas Moestl  * We do not know the length of intrcnt and intrnames at compile time, so
1640d279178dSThomas Moestl  * calculate things at run time.
1641d279178dSThomas Moestl  */
1642d279178dSThomas Moestl static int
1643d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1644d279178dSThomas Moestl {
1645521ea19dSAttilio Rao 	return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1646d279178dSThomas Moestl }
1647d279178dSThomas Moestl 
16487029da5cSPawel Biernacki SYSCTL_PROC(_hw, OID_AUTO, intrnames,
164967f508dbSAlexander Motin     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
16507029da5cSPawel Biernacki     sysctl_intrnames, "",
16517029da5cSPawel Biernacki     "Interrupt Names");
1652d279178dSThomas Moestl 
1653d279178dSThomas Moestl static int
1654d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1655d279178dSThomas Moestl {
165685729c2cSJuli Mallett #ifdef SCTL_MASK32
165785729c2cSJuli Mallett 	uint32_t *intrcnt32;
165885729c2cSJuli Mallett 	unsigned i;
165985729c2cSJuli Mallett 	int error;
166085729c2cSJuli Mallett 
166185729c2cSJuli Mallett 	if (req->flags & SCTL_MASK32) {
166285729c2cSJuli Mallett 		if (!req->oldptr)
166385729c2cSJuli Mallett 			return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
166485729c2cSJuli Mallett 		intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
166585729c2cSJuli Mallett 		if (intrcnt32 == NULL)
166685729c2cSJuli Mallett 			return (ENOMEM);
166785729c2cSJuli Mallett 		for (i = 0; i < sintrcnt / sizeof (u_long); i++)
166885729c2cSJuli Mallett 			intrcnt32[i] = intrcnt[i];
166985729c2cSJuli Mallett 		error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
167085729c2cSJuli Mallett 		free(intrcnt32, M_TEMP);
167185729c2cSJuli Mallett 		return (error);
167285729c2cSJuli Mallett 	}
167385729c2cSJuli Mallett #endif
1674521ea19dSAttilio Rao 	return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1675d279178dSThomas Moestl }
1676d279178dSThomas Moestl 
16777029da5cSPawel Biernacki SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
167867f508dbSAlexander Motin     CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
16797029da5cSPawel Biernacki     sysctl_intrcnt, "",
16807029da5cSPawel Biernacki     "Interrupt Counts");
16818b201c42SJohn Baldwin 
16828b201c42SJohn Baldwin #ifdef DDB
16838b201c42SJohn Baldwin /*
16848b201c42SJohn Baldwin  * DDB command to dump the interrupt statistics.
16858b201c42SJohn Baldwin  */
1686*c84c5e00SMitchell Horne DB_SHOW_COMMAND_FLAGS(intrcnt, db_show_intrcnt, DB_CMD_MEMSAFE)
16878b201c42SJohn Baldwin {
16888b201c42SJohn Baldwin 	u_long *i;
16898b201c42SJohn Baldwin 	char *cp;
1690521ea19dSAttilio Rao 	u_int j;
16918b201c42SJohn Baldwin 
16928b201c42SJohn Baldwin 	cp = intrnames;
1693521ea19dSAttilio Rao 	j = 0;
1694521ea19dSAttilio Rao 	for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1695521ea19dSAttilio Rao 	    i++, j++) {
16968b201c42SJohn Baldwin 		if (*cp == '\0')
16978b201c42SJohn Baldwin 			break;
16988b201c42SJohn Baldwin 		if (*i != 0)
16998b201c42SJohn Baldwin 			db_printf("%s\t%lu\n", cp, *i);
17008b201c42SJohn Baldwin 		cp += strlen(cp) + 1;
17018b201c42SJohn Baldwin 	}
17028b201c42SJohn Baldwin }
17038b201c42SJohn Baldwin #endif
1704