xref: /freebsd/sys/kern/kern_intr.c (revision 37c841831ff323187d7f749947244f7e278a14ea)
1425f9fdaSStefan Eßer /*
2425f9fdaSStefan Eßer  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3425f9fdaSStefan Eßer  * All rights reserved.
4425f9fdaSStefan Eßer  *
5425f9fdaSStefan Eßer  * Redistribution and use in source and binary forms, with or without
6425f9fdaSStefan Eßer  * modification, are permitted provided that the following conditions
7425f9fdaSStefan Eßer  * are met:
8425f9fdaSStefan Eßer  * 1. Redistributions of source code must retain the above copyright
9425f9fdaSStefan Eßer  *    notice unmodified, this list of conditions, and the following
10425f9fdaSStefan Eßer  *    disclaimer.
11425f9fdaSStefan Eßer  * 2. Redistributions in binary form must reproduce the above copyright
12425f9fdaSStefan Eßer  *    notice, this list of conditions and the following disclaimer in the
13425f9fdaSStefan Eßer  *    documentation and/or other materials provided with the distribution.
14425f9fdaSStefan Eßer  *
15425f9fdaSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16425f9fdaSStefan Eßer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17425f9fdaSStefan Eßer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18425f9fdaSStefan Eßer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19425f9fdaSStefan Eßer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20425f9fdaSStefan Eßer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21425f9fdaSStefan Eßer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22425f9fdaSStefan Eßer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23425f9fdaSStefan Eßer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24425f9fdaSStefan Eßer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25425f9fdaSStefan Eßer  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27425f9fdaSStefan Eßer  *
28425f9fdaSStefan Eßer  */
29425f9fdaSStefan Eßer 
303900ddb2SDoug Rabson 
311c5bb3eaSPeter Wemm #include <sys/param.h>
329a94c9c5SJohn Baldwin #include <sys/bus.h>
339a94c9c5SJohn Baldwin #include <sys/rtprio.h>
34425f9fdaSStefan Eßer #include <sys/systm.h>
3568352337SDoug Rabson #include <sys/interrupt.h>
361931cf94SJohn Baldwin #include <sys/kernel.h>
371931cf94SJohn Baldwin #include <sys/kthread.h>
381931cf94SJohn Baldwin #include <sys/ktr.h>
39f34fa851SJohn Baldwin #include <sys/lock.h>
401931cf94SJohn Baldwin #include <sys/malloc.h>
4135e0e5b3SJohn Baldwin #include <sys/mutex.h>
421931cf94SJohn Baldwin #include <sys/proc.h>
433e5da754SJohn Baldwin #include <sys/random.h>
44b4151f71SJohn Baldwin #include <sys/resourcevar.h>
45d279178dSThomas Moestl #include <sys/sysctl.h>
461931cf94SJohn Baldwin #include <sys/unistd.h>
471931cf94SJohn Baldwin #include <sys/vmmeter.h>
481931cf94SJohn Baldwin #include <machine/atomic.h>
491931cf94SJohn Baldwin #include <machine/cpu.h>
508088699fSJohn Baldwin #include <machine/md_var.h>
51b4151f71SJohn Baldwin #include <machine/stdarg.h>
52425f9fdaSStefan Eßer 
533e5da754SJohn Baldwin struct	int_entropy {
543e5da754SJohn Baldwin 	struct	proc *proc;
553e5da754SJohn Baldwin 	int	vector;
563e5da754SJohn Baldwin };
573e5da754SJohn Baldwin 
58b4151f71SJohn Baldwin void	*vm_ih;
59b4151f71SJohn Baldwin void	*softclock_ih;
608088699fSJohn Baldwin struct	ithd *clk_ithd;
618088699fSJohn Baldwin struct	ithd *tty_ithd;
621931cf94SJohn Baldwin 
63b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
64b4151f71SJohn Baldwin 
65b4151f71SJohn Baldwin static void	ithread_update(struct ithd *);
66b4151f71SJohn Baldwin static void	ithread_loop(void *);
671931cf94SJohn Baldwin static void	start_softintr(void *);
6818c5a6c4SBruce Evans 
69b4151f71SJohn Baldwin u_char
70b4151f71SJohn Baldwin ithread_priority(enum intr_type flags)
719a94c9c5SJohn Baldwin {
72b4151f71SJohn Baldwin 	u_char pri;
739a94c9c5SJohn Baldwin 
74b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
755a280d9cSPeter Wemm 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
769a94c9c5SJohn Baldwin 	switch (flags) {
77b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
789a94c9c5SJohn Baldwin 		pri = PI_TTYLOW;
799a94c9c5SJohn Baldwin 		break;
809a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
819a94c9c5SJohn Baldwin 		/*
829a94c9c5SJohn Baldwin 		 * XXX We need to refine this.  BSD/OS distinguishes
839a94c9c5SJohn Baldwin 		 * between tape and disk priorities.
849a94c9c5SJohn Baldwin 		 */
859a94c9c5SJohn Baldwin 		pri = PI_DISK;
869a94c9c5SJohn Baldwin 		break;
879a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
889a94c9c5SJohn Baldwin 		pri = PI_NET;
899a94c9c5SJohn Baldwin 		break;
909a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
919a94c9c5SJohn Baldwin 		pri = PI_DISK;          /* XXX or PI_CAM? */
929a94c9c5SJohn Baldwin 		break;
935a280d9cSPeter Wemm 	case INTR_TYPE_AV:		/* Audio/video */
945a280d9cSPeter Wemm 		pri = PI_AV;
955a280d9cSPeter Wemm 		break;
96b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
97b4151f71SJohn Baldwin 		pri = PI_REALTIME;
98b4151f71SJohn Baldwin 		break;
999a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1009a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1019a94c9c5SJohn Baldwin 		break;
1029a94c9c5SJohn Baldwin 	default:
103b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
1049a94c9c5SJohn Baldwin 		panic("ithread_priority: no interrupt type in flags");
1059a94c9c5SJohn Baldwin 	}
1069a94c9c5SJohn Baldwin 
1079a94c9c5SJohn Baldwin 	return pri;
1089a94c9c5SJohn Baldwin }
1099a94c9c5SJohn Baldwin 
110b4151f71SJohn Baldwin /*
111b4151f71SJohn Baldwin  * Regenerate the name (p_comm) and priority for a threaded interrupt thread.
112b4151f71SJohn Baldwin  */
113b4151f71SJohn Baldwin static void
114b4151f71SJohn Baldwin ithread_update(struct ithd *ithd)
115b4151f71SJohn Baldwin {
116b4151f71SJohn Baldwin 	struct intrhand *ih;
117b40ce416SJulian Elischer 	struct thread *td;
118b4151f71SJohn Baldwin 	struct proc *p;
119b4151f71SJohn Baldwin 	int entropy;
1208088699fSJohn Baldwin 
1214d29cb2dSJohn Baldwin 	mtx_assert(&ithd->it_lock, MA_OWNED);
122b40ce416SJulian Elischer 	td = ithd->it_td;
123b40ce416SJulian Elischer 	if (td == NULL)
124b4151f71SJohn Baldwin 		return;
125b40ce416SJulian Elischer 	p = td->td_proc;
126b4151f71SJohn Baldwin 
127b4151f71SJohn Baldwin 	strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name));
128b4151f71SJohn Baldwin 	ih = TAILQ_FIRST(&ithd->it_handlers);
129b4151f71SJohn Baldwin 	if (ih == NULL) {
130b106d2f5SJohn Baldwin 		mtx_lock_spin(&sched_lock);
1312c100766SJulian Elischer 		td->td_priority = PRI_MAX_ITHD;
132b106d2f5SJohn Baldwin 		td->td_base_pri = PRI_MAX_ITHD;
133b106d2f5SJohn Baldwin 		mtx_unlock_spin(&sched_lock);
134b4151f71SJohn Baldwin 		ithd->it_flags &= ~IT_ENTROPY;
135b4151f71SJohn Baldwin 		return;
136b4151f71SJohn Baldwin 	}
137b4151f71SJohn Baldwin 	entropy = 0;
138b106d2f5SJohn Baldwin 	mtx_lock_spin(&sched_lock);
1392c100766SJulian Elischer 	td->td_priority = ih->ih_pri;
1402c100766SJulian Elischer 	td->td_base_pri = ih->ih_pri;
141b106d2f5SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
142b4151f71SJohn Baldwin 	TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
143b4151f71SJohn Baldwin 		if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 <
144b4151f71SJohn Baldwin 		    sizeof(p->p_comm)) {
145b4151f71SJohn Baldwin 			strcat(p->p_comm, " ");
146b4151f71SJohn Baldwin 			strcat(p->p_comm, ih->ih_name);
147b4151f71SJohn Baldwin 		} else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) {
148b4151f71SJohn Baldwin 			if (p->p_comm[sizeof(p->p_comm) - 2] == '+')
149b4151f71SJohn Baldwin 				p->p_comm[sizeof(p->p_comm) - 2] = '*';
150b4151f71SJohn Baldwin 			else
151b4151f71SJohn Baldwin 				p->p_comm[sizeof(p->p_comm) - 2] = '+';
152b4151f71SJohn Baldwin 		} else
153b4151f71SJohn Baldwin 			strcat(p->p_comm, "+");
154b4151f71SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY)
155b4151f71SJohn Baldwin 			entropy++;
156b4151f71SJohn Baldwin 	}
1573e5da754SJohn Baldwin 	if (entropy)
158b4151f71SJohn Baldwin 		ithd->it_flags |= IT_ENTROPY;
159b4151f71SJohn Baldwin 	else
160b4151f71SJohn Baldwin 		ithd->it_flags &= ~IT_ENTROPY;
16191f91617SDavid E. O'Brien 	CTR2(KTR_INTR, "%s: updated %s\n", __func__, p->p_comm);
162b4151f71SJohn Baldwin }
163b4151f71SJohn Baldwin 
164b4151f71SJohn Baldwin int
165b4151f71SJohn Baldwin ithread_create(struct ithd **ithread, int vector, int flags,
166b4151f71SJohn Baldwin     void (*disable)(int), void (*enable)(int), const char *fmt, ...)
167b4151f71SJohn Baldwin {
168b4151f71SJohn Baldwin 	struct ithd *ithd;
169b40ce416SJulian Elischer 	struct thread *td;
170b4151f71SJohn Baldwin 	struct proc *p;
171b4151f71SJohn Baldwin 	int error;
172b4151f71SJohn Baldwin 	va_list ap;
173b4151f71SJohn Baldwin 
1743e5da754SJohn Baldwin 	/* The only valid flag during creation is IT_SOFT. */
1753e5da754SJohn Baldwin 	if ((flags & ~IT_SOFT) != 0)
1763e5da754SJohn Baldwin 		return (EINVAL);
1773e5da754SJohn Baldwin 
178b4151f71SJohn Baldwin 	ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO);
179b4151f71SJohn Baldwin 	ithd->it_vector = vector;
180b4151f71SJohn Baldwin 	ithd->it_disable = disable;
181b4151f71SJohn Baldwin 	ithd->it_enable = enable;
182b4151f71SJohn Baldwin 	ithd->it_flags = flags;
183b4151f71SJohn Baldwin 	TAILQ_INIT(&ithd->it_handlers);
1846008862bSJohn Baldwin 	mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF);
185b4151f71SJohn Baldwin 
186b4151f71SJohn Baldwin 	va_start(ap, fmt);
187b4151f71SJohn Baldwin 	vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap);
188b4151f71SJohn Baldwin 	va_end(ap);
189b4151f71SJohn Baldwin 
190b4151f71SJohn Baldwin 	error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
1911f723035SJohn Baldwin 	    "%s", ithd->it_name);
192b4151f71SJohn Baldwin 	if (error) {
1934d29cb2dSJohn Baldwin 		mtx_destroy(&ithd->it_lock);
194b4151f71SJohn Baldwin 		free(ithd, M_ITHREAD);
195b4151f71SJohn Baldwin 		return (error);
196b4151f71SJohn Baldwin 	}
197079b7badSJulian Elischer 	td = FIRST_THREAD_IN_PROC(p);	/* XXXKSE */
1982c100766SJulian Elischer 	td->td_ksegrp->kg_pri_class = PRI_ITHD;
1992c100766SJulian Elischer 	td->td_priority = PRI_MAX_ITHD;
20071fad9fdSJulian Elischer 	TD_SET_IWAIT(td);
201b40ce416SJulian Elischer 	ithd->it_td = td;
202b40ce416SJulian Elischer 	td->td_ithd = ithd;
203b4151f71SJohn Baldwin 	if (ithread != NULL)
204b4151f71SJohn Baldwin 		*ithread = ithd;
205b4151f71SJohn Baldwin 
20691f91617SDavid E. O'Brien 	CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name);
207b4151f71SJohn Baldwin 	return (0);
208b4151f71SJohn Baldwin }
209b4151f71SJohn Baldwin 
210b4151f71SJohn Baldwin int
211b4151f71SJohn Baldwin ithread_destroy(struct ithd *ithread)
212b4151f71SJohn Baldwin {
213b4151f71SJohn Baldwin 
214b40ce416SJulian Elischer 	struct thread *td;
215b40ce416SJulian Elischer 	struct proc *p;
2164d29cb2dSJohn Baldwin 	if (ithread == NULL)
217b4151f71SJohn Baldwin 		return (EINVAL);
218b4151f71SJohn Baldwin 
219b40ce416SJulian Elischer 	td = ithread->it_td;
220b40ce416SJulian Elischer 	p = td->td_proc;
2214d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
2224d29cb2dSJohn Baldwin 	if (!TAILQ_EMPTY(&ithread->it_handlers)) {
2234d29cb2dSJohn Baldwin 		mtx_unlock(&ithread->it_lock);
2244d29cb2dSJohn Baldwin 		return (EINVAL);
2254d29cb2dSJohn Baldwin 	}
226b4151f71SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
2274d29cb2dSJohn Baldwin 	mtx_lock_spin(&sched_lock);
22871fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
22971fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
230b40ce416SJulian Elischer 		setrunqueue(td);
231b4151f71SJohn Baldwin 	}
232b4151f71SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
2334d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
23491f91617SDavid E. O'Brien 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name);
235b4151f71SJohn Baldwin 	return (0);
236b4151f71SJohn Baldwin }
237b4151f71SJohn Baldwin 
238b4151f71SJohn Baldwin int
239b4151f71SJohn Baldwin ithread_add_handler(struct ithd* ithread, const char *name,
240b4151f71SJohn Baldwin     driver_intr_t handler, void *arg, u_char pri, enum intr_type flags,
241b4151f71SJohn Baldwin     void **cookiep)
242b4151f71SJohn Baldwin {
243b4151f71SJohn Baldwin 	struct intrhand *ih, *temp_ih;
244b4151f71SJohn Baldwin 
245b4151f71SJohn Baldwin 	if (ithread == NULL || name == NULL || handler == NULL)
246b4151f71SJohn Baldwin 		return (EINVAL);
247b4151f71SJohn Baldwin 	if ((flags & INTR_FAST) !=0)
248b4151f71SJohn Baldwin 		flags |= INTR_EXCL;
249b4151f71SJohn Baldwin 
250b4151f71SJohn Baldwin 	ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO);
251b4151f71SJohn Baldwin 	ih->ih_handler = handler;
252b4151f71SJohn Baldwin 	ih->ih_argument = arg;
253b4151f71SJohn Baldwin 	ih->ih_name = name;
254b4151f71SJohn Baldwin 	ih->ih_ithread = ithread;
255b4151f71SJohn Baldwin 	ih->ih_pri = pri;
256b4151f71SJohn Baldwin 	if (flags & INTR_FAST)
257b4151f71SJohn Baldwin 		ih->ih_flags = IH_FAST | IH_EXCLUSIVE;
258b4151f71SJohn Baldwin 	else if (flags & INTR_EXCL)
259b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
260b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
261b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
262b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
263b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
264b4151f71SJohn Baldwin 
2654d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
266b4151f71SJohn Baldwin 	if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers))
267b4151f71SJohn Baldwin 		goto fail;
268b4151f71SJohn Baldwin 	if (!TAILQ_EMPTY(&ithread->it_handlers) &&
269b4151f71SJohn Baldwin 	    (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0)
270b4151f71SJohn Baldwin 		goto fail;
271b4151f71SJohn Baldwin 
272b4151f71SJohn Baldwin 	TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next)
273b4151f71SJohn Baldwin 	    if (temp_ih->ih_pri > ih->ih_pri)
274b4151f71SJohn Baldwin 		    break;
275b4151f71SJohn Baldwin 	if (temp_ih == NULL)
276b4151f71SJohn Baldwin 		TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next);
277b4151f71SJohn Baldwin 	else
278b4151f71SJohn Baldwin 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
279b4151f71SJohn Baldwin 	ithread_update(ithread);
2804d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
281b4151f71SJohn Baldwin 
282b4151f71SJohn Baldwin 	if (cookiep != NULL)
283b4151f71SJohn Baldwin 		*cookiep = ih;
28491f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
285addec20cSJohn Baldwin 	    ithread->it_name);
286b4151f71SJohn Baldwin 	return (0);
287b4151f71SJohn Baldwin 
288b4151f71SJohn Baldwin fail:
2894d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
290b4151f71SJohn Baldwin 	free(ih, M_ITHREAD);
291b4151f71SJohn Baldwin 	return (EINVAL);
292b4151f71SJohn Baldwin }
293b4151f71SJohn Baldwin 
294b4151f71SJohn Baldwin int
295b4151f71SJohn Baldwin ithread_remove_handler(void *cookie)
296b4151f71SJohn Baldwin {
297b4151f71SJohn Baldwin 	struct intrhand *handler = (struct intrhand *)cookie;
298b4151f71SJohn Baldwin 	struct ithd *ithread;
299b4151f71SJohn Baldwin #ifdef INVARIANTS
300b4151f71SJohn Baldwin 	struct intrhand *ih;
301b4151f71SJohn Baldwin #endif
302b4151f71SJohn Baldwin 
3033e5da754SJohn Baldwin 	if (handler == NULL)
304b4151f71SJohn Baldwin 		return (EINVAL);
30576bd604eSJohn Baldwin 	ithread = handler->ih_ithread;
30676bd604eSJohn Baldwin 	KASSERT(ithread != NULL,
3073e5da754SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt thread",
3083e5da754SJohn Baldwin 		handler->ih_name));
30991f91617SDavid E. O'Brien 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
310addec20cSJohn Baldwin 	    ithread->it_name);
3114d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
312b4151f71SJohn Baldwin #ifdef INVARIANTS
313b4151f71SJohn Baldwin 	TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next)
3143e5da754SJohn Baldwin 		if (ih == handler)
3153e5da754SJohn Baldwin 			goto ok;
3164d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
3173e5da754SJohn Baldwin 	panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"",
3183e5da754SJohn Baldwin 	    ih->ih_name, ithread->it_name);
3193e5da754SJohn Baldwin ok:
320b4151f71SJohn Baldwin #endif
321de271f01SJohn Baldwin 	/*
322de271f01SJohn Baldwin 	 * If the interrupt thread is already running, then just mark this
323de271f01SJohn Baldwin 	 * handler as being dead and let the ithread do the actual removal.
324de271f01SJohn Baldwin 	 */
325de271f01SJohn Baldwin 	mtx_lock_spin(&sched_lock);
32671fad9fdSJulian Elischer 	if (!TD_AWAITING_INTR(ithread->it_td)) {
327de271f01SJohn Baldwin 		handler->ih_flags |= IH_DEAD;
328de271f01SJohn Baldwin 
329de271f01SJohn Baldwin 		/*
330de271f01SJohn Baldwin 		 * Ensure that the thread will process the handler list
331de271f01SJohn Baldwin 		 * again and remove this handler if it has already passed
332de271f01SJohn Baldwin 		 * it on the list.
333de271f01SJohn Baldwin 		 */
334de271f01SJohn Baldwin 		ithread->it_need = 1;
3354d29cb2dSJohn Baldwin 	} else
336b4151f71SJohn Baldwin 		TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next);
337de271f01SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
3384d29cb2dSJohn Baldwin 	if ((handler->ih_flags & IH_DEAD) != 0)
3394d29cb2dSJohn Baldwin 		msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0);
3404d29cb2dSJohn Baldwin 	ithread_update(ithread);
3414d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
342b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
343b4151f71SJohn Baldwin 	return (0);
344b4151f71SJohn Baldwin }
345b4151f71SJohn Baldwin 
346b4151f71SJohn Baldwin int
3473e5da754SJohn Baldwin ithread_schedule(struct ithd *ithread, int do_switch)
3483e5da754SJohn Baldwin {
3493e5da754SJohn Baldwin 	struct int_entropy entropy;
350b40ce416SJulian Elischer 	struct thread *td;
35104774f23SJulian Elischer 	struct thread *ctd;
3523e5da754SJohn Baldwin 	struct proc *p;
3533e5da754SJohn Baldwin 
3543e5da754SJohn Baldwin 	/*
3553e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
3563e5da754SJohn Baldwin 	 */
3573e5da754SJohn Baldwin 	if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers))
3583e5da754SJohn Baldwin 		return (EINVAL);
3593e5da754SJohn Baldwin 
36004774f23SJulian Elischer 	ctd = curthread;
3613e5da754SJohn Baldwin 	/*
3623e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
3633e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
3643e5da754SJohn Baldwin 	 */
3653e5da754SJohn Baldwin 	if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) {
3663e5da754SJohn Baldwin 		entropy.vector = ithread->it_vector;
36765c17e74SDavid Xu 		entropy.proc = ctd->td_proc;
3683e5da754SJohn Baldwin 		random_harvest(&entropy, sizeof(entropy), 2, 0,
3693e5da754SJohn Baldwin 		    RANDOM_INTERRUPT);
3703e5da754SJohn Baldwin 	}
3713e5da754SJohn Baldwin 
372b40ce416SJulian Elischer 	td = ithread->it_td;
373b40ce416SJulian Elischer 	p = td->td_proc;
374653dd8c2SJohn Baldwin 	KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name));
375e602ba25SJulian Elischer 	CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d",
376e602ba25SJulian Elischer 	    __func__, p->p_pid, p->p_comm, ithread->it_need);
3773e5da754SJohn Baldwin 
3783e5da754SJohn Baldwin 	/*
3793e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
3803e5da754SJohn Baldwin 	 * running.  Then, grab sched_lock and see if we actually need to
3813e5da754SJohn Baldwin 	 * put this thread on the runqueue.  If so and the do_switch flag is
382c86b6ff5SJohn Baldwin 	 * true and it is safe to switch, then switch to the ithread
383c86b6ff5SJohn Baldwin 	 * immediately.  Otherwise, set the needresched flag to guarantee
384c86b6ff5SJohn Baldwin 	 * that this ithread will run before any userland processes.
3853e5da754SJohn Baldwin 	 */
3863e5da754SJohn Baldwin 	ithread->it_need = 1;
3873e5da754SJohn Baldwin 	mtx_lock_spin(&sched_lock);
38871fad9fdSJulian Elischer 	if (TD_AWAITING_INTR(td)) {
38991f91617SDavid E. O'Brien 		CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid);
39071fad9fdSJulian Elischer 		TD_CLR_IWAIT(td);
391e602ba25SJulian Elischer 		setrunqueue(td);
392e602ba25SJulian Elischer 		if (do_switch &&
39304774f23SJulian Elischer 		    (ctd->td_critnest == 1) ) {
39471fad9fdSJulian Elischer 			KASSERT((TD_IS_RUNNING(ctd)),
39504774f23SJulian Elischer 			    ("ithread_schedule: Bad state for curthread."));
39604774f23SJulian Elischer 			ctd->td_proc->p_stats->p_ru.ru_nivcsw++;
39704774f23SJulian Elischer 			if (ctd->td_kse->ke_flags & KEF_IDLEKSE)
39871fad9fdSJulian Elischer 				ctd->td_state = TDS_CAN_RUN; /* XXXKSE */
3993e5da754SJohn Baldwin 			mi_switch();
4002d0231f5SJulian Elischer 		} else {
401b40ce416SJulian Elischer 			curthread->td_kse->ke_flags |= KEF_NEEDRESCHED;
4022d0231f5SJulian Elischer 		}
4033e5da754SJohn Baldwin 	} else {
40491f91617SDavid E. O'Brien 		CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d",
405e602ba25SJulian Elischer 		    __func__, p->p_pid, ithread->it_need, p->p_state);
4063e5da754SJohn Baldwin 	}
4073e5da754SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
4083e5da754SJohn Baldwin 
4093e5da754SJohn Baldwin 	return (0);
4103e5da754SJohn Baldwin }
4113e5da754SJohn Baldwin 
4123e5da754SJohn Baldwin int
413b4151f71SJohn Baldwin swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler,
414b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
4158088699fSJohn Baldwin {
4168088699fSJohn Baldwin 	struct ithd *ithd;
417b4151f71SJohn Baldwin 	int error;
4188088699fSJohn Baldwin 
4193e5da754SJohn Baldwin 	if (flags & (INTR_FAST | INTR_ENTROPY))
4203e5da754SJohn Baldwin 		return (EINVAL);
4213e5da754SJohn Baldwin 
4228088699fSJohn Baldwin 	ithd = (ithdp != NULL) ? *ithdp : NULL;
4238088699fSJohn Baldwin 
4243e5da754SJohn Baldwin 	if (ithd != NULL) {
4253e5da754SJohn Baldwin 		if ((ithd->it_flags & IT_SOFT) == 0)
4263e5da754SJohn Baldwin 			return(EINVAL);
4273e5da754SJohn Baldwin 	} else {
428b4151f71SJohn Baldwin 		error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL,
429b4151f71SJohn Baldwin 		    "swi%d:", pri);
4308088699fSJohn Baldwin 		if (error)
431b4151f71SJohn Baldwin 			return (error);
432b4151f71SJohn Baldwin 
4338088699fSJohn Baldwin 		if (ithdp != NULL)
4348088699fSJohn Baldwin 			*ithdp = ithd;
4358088699fSJohn Baldwin 	}
436d5a08a60SJake Burkholder 	return (ithread_add_handler(ithd, name, handler, arg,
437d5a08a60SJake Burkholder 		    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
4388088699fSJohn Baldwin }
4398088699fSJohn Baldwin 
4408088699fSJohn Baldwin 
4411931cf94SJohn Baldwin /*
4428088699fSJohn Baldwin  * Schedule a heavyweight software interrupt process.
4431931cf94SJohn Baldwin  */
4441931cf94SJohn Baldwin void
445b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
4461931cf94SJohn Baldwin {
447b4151f71SJohn Baldwin 	struct intrhand *ih = (struct intrhand *)cookie;
448b4151f71SJohn Baldwin 	struct ithd *it = ih->ih_ithread;
4493e5da754SJohn Baldwin 	int error;
4508088699fSJohn Baldwin 
4511931cf94SJohn Baldwin 	atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */
4521931cf94SJohn Baldwin 
453b4151f71SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d",
454b40ce416SJulian Elischer 		it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need);
4551931cf94SJohn Baldwin 
4561931cf94SJohn Baldwin 	/*
4573e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
4583e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
4593e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
4601931cf94SJohn Baldwin 	 */
461b4151f71SJohn Baldwin 	atomic_store_rel_int(&ih->ih_need, 1);
462b4151f71SJohn Baldwin 	if (!(flags & SWI_DELAY)) {
463c86b6ff5SJohn Baldwin 		error = ithread_schedule(it, !cold);
4643e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
4658088699fSJohn Baldwin 	}
4668088699fSJohn Baldwin }
4678088699fSJohn Baldwin 
4688088699fSJohn Baldwin /*
469b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
4708088699fSJohn Baldwin  */
47137c84183SPoul-Henning Kamp static void
472b4151f71SJohn Baldwin ithread_loop(void *arg)
4738088699fSJohn Baldwin {
474b4151f71SJohn Baldwin 	struct ithd *ithd;		/* our thread context */
4758088699fSJohn Baldwin 	struct intrhand *ih;		/* and our interrupt handler chain */
476b40ce416SJulian Elischer 	struct thread *td;
477b4151f71SJohn Baldwin 	struct proc *p;
4788088699fSJohn Baldwin 
479b40ce416SJulian Elischer 	td = curthread;
480b40ce416SJulian Elischer 	p = td->td_proc;
481b4151f71SJohn Baldwin 	ithd = (struct ithd *)arg;	/* point to myself */
482b40ce416SJulian Elischer 	KASSERT(ithd->it_td == td && td->td_ithd == ithd,
48391f91617SDavid E. O'Brien 	    ("%s: ithread and proc linkage out of sync", __func__));
4848088699fSJohn Baldwin 
4858088699fSJohn Baldwin 	/*
4868088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
4878088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
4888088699fSJohn Baldwin 	 */
4898088699fSJohn Baldwin 	for (;;) {
490b4151f71SJohn Baldwin 		/*
491b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
492b4151f71SJohn Baldwin 		 */
493b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
49491f91617SDavid E. O'Brien 			CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__,
495b4151f71SJohn Baldwin 			    p->p_pid, p->p_comm);
496b40ce416SJulian Elischer 			td->td_ithd = NULL;
4974d29cb2dSJohn Baldwin 			mtx_destroy(&ithd->it_lock);
498b4151f71SJohn Baldwin 			mtx_lock(&Giant);
499b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
500b4151f71SJohn Baldwin 			kthread_exit(0);
501b4151f71SJohn Baldwin 		}
502b4151f71SJohn Baldwin 
50391f91617SDavid E. O'Brien 		CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__,
504b4151f71SJohn Baldwin 		     p->p_pid, p->p_comm, ithd->it_need);
505b4151f71SJohn Baldwin 		while (ithd->it_need) {
5068088699fSJohn Baldwin 			/*
5078088699fSJohn Baldwin 			 * Service interrupts.  If another interrupt
5088088699fSJohn Baldwin 			 * arrives while we are running, they will set
5098088699fSJohn Baldwin 			 * it_need to denote that we should make
5108088699fSJohn Baldwin 			 * another pass.
5118088699fSJohn Baldwin 			 */
512b4151f71SJohn Baldwin 			atomic_store_rel_int(&ithd->it_need, 0);
513de271f01SJohn Baldwin restart:
514b4151f71SJohn Baldwin 			TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
515b4151f71SJohn Baldwin 				if (ithd->it_flags & IT_SOFT && !ih->ih_need)
5168088699fSJohn Baldwin 					continue;
517b4151f71SJohn Baldwin 				atomic_store_rel_int(&ih->ih_need, 0);
51891f91617SDavid E. O'Brien 				CTR6(KTR_INTR,
51991f91617SDavid E. O'Brien 				    "%s: pid %d ih=%p: %p(%p) flg=%x", __func__,
5208088699fSJohn Baldwin 				    p->p_pid, (void *)ih,
5218088699fSJohn Baldwin 				    (void *)ih->ih_handler, ih->ih_argument,
5228088699fSJohn Baldwin 				    ih->ih_flags);
5238088699fSJohn Baldwin 
524de271f01SJohn Baldwin 				if ((ih->ih_flags & IH_DEAD) != 0) {
5254d29cb2dSJohn Baldwin 					mtx_lock(&ithd->it_lock);
526de271f01SJohn Baldwin 					TAILQ_REMOVE(&ithd->it_handlers, ih,
527de271f01SJohn Baldwin 					    ih_next);
5284d29cb2dSJohn Baldwin 					wakeup(ih);
5294d29cb2dSJohn Baldwin 					mtx_unlock(&ithd->it_lock);
530de271f01SJohn Baldwin 					goto restart;
531de271f01SJohn Baldwin 				}
5324d29cb2dSJohn Baldwin 				if ((ih->ih_flags & IH_MPSAFE) == 0)
5334d29cb2dSJohn Baldwin 					mtx_lock(&Giant);
5348088699fSJohn Baldwin 				ih->ih_handler(ih->ih_argument);
535b4151f71SJohn Baldwin 				if ((ih->ih_flags & IH_MPSAFE) == 0)
5369ed346baSBosko Milekic 					mtx_unlock(&Giant);
5378088699fSJohn Baldwin 			}
5388088699fSJohn Baldwin 		}
5398088699fSJohn Baldwin 
5408088699fSJohn Baldwin 		/*
5418088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
5428088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
5438088699fSJohn Baldwin 		 * set again, so we have to check it again.
5448088699fSJohn Baldwin 		 */
545896c2303SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
5469ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
547b4151f71SJohn Baldwin 		if (!ithd->it_need) {
548b4151f71SJohn Baldwin 			/*
549b4151f71SJohn Baldwin 			 * Should we call this earlier in the loop above?
550b4151f71SJohn Baldwin 			 */
551b4151f71SJohn Baldwin 			if (ithd->it_enable != NULL)
552b4151f71SJohn Baldwin 				ithd->it_enable(ithd->it_vector);
55371fad9fdSJulian Elischer 			TD_SET_IWAIT(td); /* we're idle */
55484bbc4dbSJohn Baldwin 			p->p_stats->p_ru.ru_nvcsw++;
55591f91617SDavid E. O'Brien 			CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid);
5568088699fSJohn Baldwin 			mi_switch();
55791f91617SDavid E. O'Brien 			CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid);
5588088699fSJohn Baldwin 		}
5599ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
5608088699fSJohn Baldwin 	}
5611931cf94SJohn Baldwin }
5621931cf94SJohn Baldwin 
563b4151f71SJohn Baldwin /*
5648088699fSJohn Baldwin  * Start standard software interrupt threads
5651931cf94SJohn Baldwin  */
5661931cf94SJohn Baldwin static void
567b4151f71SJohn Baldwin start_softintr(void *dummy)
5681931cf94SJohn Baldwin {
569b4151f71SJohn Baldwin 
570e3b6e33cSJake Burkholder 	if (swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK,
571b4151f71SJohn Baldwin 		INTR_MPSAFE, &softclock_ih) ||
572b4151f71SJohn Baldwin 	    swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih))
573b4151f71SJohn Baldwin 		panic("died while creating standard software ithreads");
5743e5da754SJohn Baldwin 
575b40ce416SJulian Elischer 	PROC_LOCK(clk_ithd->it_td->td_proc);
576b40ce416SJulian Elischer 	clk_ithd->it_td->td_proc->p_flag |= P_NOLOAD;
577b40ce416SJulian Elischer 	PROC_UNLOCK(clk_ithd->it_td->td_proc);
5781931cf94SJohn Baldwin }
579b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
5801931cf94SJohn Baldwin 
581d279178dSThomas Moestl /*
582d279178dSThomas Moestl  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
583d279178dSThomas Moestl  * The data for this machine dependent, and the declarations are in machine
584d279178dSThomas Moestl  * dependent code.  The layout of intrnames and intrcnt however is machine
585d279178dSThomas Moestl  * independent.
586d279178dSThomas Moestl  *
587d279178dSThomas Moestl  * We do not know the length of intrcnt and intrnames at compile time, so
588d279178dSThomas Moestl  * calculate things at run time.
589d279178dSThomas Moestl  */
590d279178dSThomas Moestl static int
591d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS)
592d279178dSThomas Moestl {
593d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
594d279178dSThomas Moestl 	   req));
595d279178dSThomas Moestl }
596d279178dSThomas Moestl 
597d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
598d279178dSThomas Moestl     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
599d279178dSThomas Moestl 
600d279178dSThomas Moestl static int
601d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
602d279178dSThomas Moestl {
603d279178dSThomas Moestl 	return (sysctl_handle_opaque(oidp, intrcnt,
604d279178dSThomas Moestl 	    (char *)eintrcnt - (char *)intrcnt, req));
605d279178dSThomas Moestl }
606d279178dSThomas Moestl 
607d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
608d279178dSThomas Moestl     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
609