2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
35 #include <sys/cpuset.h>
36 #include <sys/rtprio.h>
37 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
42 #include <sys/limits.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <sys/random.h>
48 #include <sys/resourcevar.h>
49 #include <sys/sched.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/vmmeter.h>
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56 #include <machine/md_var.h>
57 #include <machine/stdarg.h>
60 #include <ddb/db_sym.h>
64 * Describe an interrupt thread. There is one of these per interrupt event.
67 struct intr_event *it_event;
68 struct thread *it_thread; /* Kernel thread. */
69 int it_flags; /* (j) IT_* flags. */
70 int it_need; /* Needs service. */
73 /* Interrupt thread flags kept in it_flags */
74 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
81 struct intr_event *clk_intr_event;
82 struct intr_event *tty_intr_event;
84 struct proc *intrproc;
86 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
88 static int intr_storm_threshold = 1000;
89 TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
90 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
91 &intr_storm_threshold, 0,
92 "Number of consecutive interrupts before storm protection is enabled");
93 static TAILQ_HEAD(, intr_event) event_list =
94 TAILQ_HEAD_INITIALIZER(event_list);
95 static struct mtx event_lock;
96 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
98 static void intr_event_update(struct intr_event *ie);
100 static int intr_event_schedule_thread(struct intr_event *ie,
101 struct intr_thread *ithd);
102 static int intr_filter_loop(struct intr_event *ie,
103 struct trapframe *frame, struct intr_thread **ithd);
104 static struct intr_thread *ithread_create(const char *name,
105 struct intr_handler *ih);
107 static int intr_event_schedule_thread(struct intr_event *ie);
108 static struct intr_thread *ithread_create(const char *name);
110 static void ithread_destroy(struct intr_thread *ithread);
111 static void ithread_execute_handlers(struct proc *p,
112 struct intr_event *ie);
114 static void priv_ithread_execute_handler(struct proc *p,
115 struct intr_handler *ih);
117 static void ithread_loop(void *);
118 static void ithread_update(struct intr_thread *ithd);
119 static void start_softintr(void *);
121 /* Map an interrupt type to an ithread priority. */
123 intr_priority(enum intr_type flags)
127 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
128 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
135 * XXX We need to refine this. BSD/OS distinguishes
136 * between tape and disk priorities.
144 pri = PI_DISK; /* XXX or PI_CAM? */
146 case INTR_TYPE_AV: /* Audio/video */
153 pri = PI_DULL; /* don't care */
156 /* We didn't specify an interrupt level. */
157 panic("intr_priority: no interrupt type in flags");
164 * Update an ithread based on the associated intr_event.
167 ithread_update(struct intr_thread *ithd)
169 struct intr_event *ie;
174 td = ithd->it_thread;
176 /* Determine the overall priority of this event. */
177 if (TAILQ_EMPTY(&ie->ie_handlers))
180 pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
182 /* Update name and priority. */
183 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
190 * Regenerate the full name of an interrupt event and update its priority.
193 intr_event_update(struct intr_event *ie)
195 struct intr_handler *ih;
199 /* Start off with no entropy and just the name of the event. */
200 mtx_assert(&ie->ie_lock, MA_OWNED);
201 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
202 ie->ie_flags &= ~IE_ENTROPY;
206 /* Run through all the handlers updating values. */
207 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
208 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
209 sizeof(ie->ie_fullname)) {
210 strcat(ie->ie_fullname, " ");
211 strcat(ie->ie_fullname, ih->ih_name);
215 if (ih->ih_flags & IH_ENTROPY)
216 ie->ie_flags |= IE_ENTROPY;
220 * If the handler names were too long, add +'s to indicate missing
221 * names. If we run out of room and still have +'s to add, change
222 * the last character from a + to a *.
224 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
225 while (missed-- > 0) {
226 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
233 strcat(ie->ie_fullname, " +");
236 strcat(ie->ie_fullname, "+");
240 * If this event has an ithread, update it's priority and
243 if (ie->ie_thread != NULL)
244 ithread_update(ie->ie_thread);
245 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
249 intr_event_create(struct intr_event **event, void *source,int flags, int irq,
250 void (*pre_ithread)(void *), void (*post_ithread)(void *),
251 void (*post_filter)(void *), int (*assign_cpu)(void *, u_char),
252 const char *fmt, ...)
254 struct intr_event *ie;
257 /* The only valid flag during creation is IE_SOFT. */
258 if ((flags & ~IE_SOFT) != 0)
260 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
261 ie->ie_source = source;
262 ie->ie_pre_ithread = pre_ithread;
263 ie->ie_post_ithread = post_ithread;
264 ie->ie_post_filter = post_filter;
265 ie->ie_assign_cpu = assign_cpu;
266 ie->ie_flags = flags;
269 TAILQ_INIT(&ie->ie_handlers);
270 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
273 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
275 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
276 mtx_lock(&event_lock);
277 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
278 mtx_unlock(&event_lock);
281 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
286 * Bind an interrupt event to the specified CPU. Note that not all
287 * platforms support binding an interrupt to a CPU. For those
288 * platforms this request will fail. For supported platforms, any
289 * associated ithreads as well as the primary interrupt context will
290 * be bound to the specificed CPU. Using a cpu id of NOCPU unbinds
291 * the interrupt event.
294 intr_event_bind(struct intr_event *ie, u_char cpu)
300 /* Need a CPU to bind to. */
301 if (cpu != NOCPU && CPU_ABSENT(cpu))
304 if (ie->ie_assign_cpu == NULL)
307 * If we have any ithreads try to set their mask first since this
310 mtx_lock(&ie->ie_lock);
311 if (ie->ie_thread != NULL) {
314 CPU_COPY(cpuset_root, &mask);
317 id = ie->ie_thread->it_thread->td_tid;
318 mtx_unlock(&ie->ie_lock);
319 error = cpuset_setthread(id, &mask);
323 mtx_unlock(&ie->ie_lock);
324 error = ie->ie_assign_cpu(ie->ie_source, cpu);
327 mtx_lock(&ie->ie_lock);
329 mtx_unlock(&ie->ie_lock);
334 static struct intr_event *
337 struct intr_event *ie;
339 mtx_lock(&event_lock);
340 TAILQ_FOREACH(ie, &event_list, ie_list)
341 if (ie->ie_irq == irq &&
342 (ie->ie_flags & IE_SOFT) == 0 &&
343 TAILQ_FIRST(&ie->ie_handlers) != NULL)
345 mtx_unlock(&event_lock);
350 intr_setaffinity(int irq, void *m)
352 struct intr_event *ie;
362 * If we're setting all cpus we can unbind. Otherwise make sure
363 * only one cpu is in the set.
365 if (CPU_CMP(cpuset_root, mask)) {
366 for (n = 0; n < CPU_SETSIZE; n++) {
367 if (!CPU_ISSET(n, mask))
374 ie = intr_lookup(irq);
377 intr_event_bind(ie, cpu);
382 intr_getaffinity(int irq, void *m)
384 struct intr_event *ie;
388 ie = intr_lookup(irq);
392 mtx_lock(&ie->ie_lock);
393 if (ie->ie_cpu == NOCPU)
394 CPU_COPY(cpuset_root, mask);
396 CPU_SET(ie->ie_cpu, mask);
397 mtx_unlock(&ie->ie_lock);
402 intr_event_destroy(struct intr_event *ie)
405 mtx_lock(&event_lock);
406 mtx_lock(&ie->ie_lock);
407 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
408 mtx_unlock(&ie->ie_lock);
409 mtx_unlock(&event_lock);
412 TAILQ_REMOVE(&event_list, ie, ie_list);
414 if (ie->ie_thread != NULL) {
415 ithread_destroy(ie->ie_thread);
416 ie->ie_thread = NULL;
419 mtx_unlock(&ie->ie_lock);
420 mtx_unlock(&event_lock);
421 mtx_destroy(&ie->ie_lock);
427 static struct intr_thread *
428 ithread_create(const char *name)
430 struct intr_thread *ithd;
434 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
436 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
437 &td, RFSTOPPED | RFHIGHPID,
438 0, "intr", "%s", name);
440 panic("kproc_create() failed with %d", error);
442 sched_class(td, PRI_ITHD);
445 td->td_pflags |= TDP_ITHREAD;
446 ithd->it_thread = td;
447 CTR2(KTR_INTR, "%s: created %s", __func__, name);
451 static struct intr_thread *
452 ithread_create(const char *name, struct intr_handler *ih)
454 struct intr_thread *ithd;
458 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
460 error = kproc_kthread_add(ithread_loop, ih, &intrproc,
461 &td, RFSTOPPED | RFHIGHPID,
462 0, "intr", "%s", name);
464 panic("kproc_create() failed with %d", error);
466 sched_class(td, PRI_ITHD);
469 td->td_pflags |= TDP_ITHREAD;
470 ithd->it_thread = td;
471 CTR2(KTR_INTR, "%s: created %s", __func__, name);
477 ithread_destroy(struct intr_thread *ithread)
481 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
482 td = ithread->it_thread;
484 ithread->it_flags |= IT_DEAD;
485 if (TD_AWAITING_INTR(td)) {
487 sched_add(td, SRQ_INTR);
494 intr_event_add_handler(struct intr_event *ie, const char *name,
495 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
496 enum intr_type flags, void **cookiep)
498 struct intr_handler *ih, *temp_ih;
499 struct intr_thread *it;
501 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
504 /* Allocate and populate an interrupt handler structure. */
505 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
506 ih->ih_filter = filter;
507 ih->ih_handler = handler;
508 ih->ih_argument = arg;
512 if (flags & INTR_EXCL)
513 ih->ih_flags = IH_EXCLUSIVE;
514 if (flags & INTR_MPSAFE)
515 ih->ih_flags |= IH_MPSAFE;
516 if (flags & INTR_ENTROPY)
517 ih->ih_flags |= IH_ENTROPY;
519 /* We can only have one exclusive handler in a event. */
520 mtx_lock(&ie->ie_lock);
521 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
522 if ((flags & INTR_EXCL) ||
523 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
524 mtx_unlock(&ie->ie_lock);
530 /* Add the new handler to the event in priority order. */
531 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
532 if (temp_ih->ih_pri > ih->ih_pri)
536 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
538 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
539 intr_event_update(ie);
541 /* Create a thread if we need one. */
542 while (ie->ie_thread == NULL && handler != NULL) {
543 if (ie->ie_flags & IE_ADDING_THREAD)
544 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
546 ie->ie_flags |= IE_ADDING_THREAD;
547 mtx_unlock(&ie->ie_lock);
548 it = ithread_create("intr: newborn");
549 mtx_lock(&ie->ie_lock);
550 ie->ie_flags &= ~IE_ADDING_THREAD;
557 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
559 mtx_unlock(&ie->ie_lock);
567 intr_event_add_handler(struct intr_event *ie, const char *name,
568 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
569 enum intr_type flags, void **cookiep)
571 struct intr_handler *ih, *temp_ih;
572 struct intr_thread *it;
574 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
577 /* Allocate and populate an interrupt handler structure. */
578 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
579 ih->ih_filter = filter;
580 ih->ih_handler = handler;
581 ih->ih_argument = arg;
585 if (flags & INTR_EXCL)
586 ih->ih_flags = IH_EXCLUSIVE;
587 if (flags & INTR_MPSAFE)
588 ih->ih_flags |= IH_MPSAFE;
589 if (flags & INTR_ENTROPY)
590 ih->ih_flags |= IH_ENTROPY;
592 /* We can only have one exclusive handler in a event. */
593 mtx_lock(&ie->ie_lock);
594 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
595 if ((flags & INTR_EXCL) ||
596 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
597 mtx_unlock(&ie->ie_lock);
603 /* Add the new handler to the event in priority order. */
604 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
605 if (temp_ih->ih_pri > ih->ih_pri)
609 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
611 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
612 intr_event_update(ie);
614 /* For filtered handlers, create a private ithread to run on. */
615 if (filter != NULL && handler != NULL) {
616 mtx_unlock(&ie->ie_lock);
617 it = ithread_create("intr: newborn", ih);
618 mtx_lock(&ie->ie_lock);
621 ithread_update(it); // XXX - do we really need this?!?!?
622 } else { /* Create the global per-event thread if we need one. */
623 while (ie->ie_thread == NULL && handler != NULL) {
624 if (ie->ie_flags & IE_ADDING_THREAD)
625 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
627 ie->ie_flags |= IE_ADDING_THREAD;
628 mtx_unlock(&ie->ie_lock);
629 it = ithread_create("intr: newborn", ih);
630 mtx_lock(&ie->ie_lock);
631 ie->ie_flags &= ~IE_ADDING_THREAD;
639 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
641 mtx_unlock(&ie->ie_lock);
650 * Return the ie_source field from the intr_event an intr_handler is
654 intr_handler_source(void *cookie)
656 struct intr_handler *ih;
657 struct intr_event *ie;
659 ih = (struct intr_handler *)cookie;
664 ("interrupt handler \"%s\" has a NULL interrupt event",
666 return (ie->ie_source);
671 intr_event_remove_handler(void *cookie)
673 struct intr_handler *handler = (struct intr_handler *)cookie;
674 struct intr_event *ie;
676 struct intr_handler *ih;
684 ie = handler->ih_event;
686 ("interrupt handler \"%s\" has a NULL interrupt event",
688 mtx_lock(&ie->ie_lock);
689 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
692 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
695 mtx_unlock(&ie->ie_lock);
696 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
697 ih->ih_name, ie->ie_name);
701 * If there is no ithread, then just remove the handler and return.
702 * XXX: Note that an INTR_FAST handler might be running on another
705 if (ie->ie_thread == NULL) {
706 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
707 mtx_unlock(&ie->ie_lock);
708 free(handler, M_ITHREAD);
713 * If the interrupt thread is already running, then just mark this
714 * handler as being dead and let the ithread do the actual removal.
716 * During a cold boot while cold is set, msleep() does not sleep,
717 * so we have to remove the handler here rather than letting the
720 thread_lock(ie->ie_thread->it_thread);
721 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
722 handler->ih_flags |= IH_DEAD;
725 * Ensure that the thread will process the handler list
726 * again and remove this handler if it has already passed
729 ie->ie_thread->it_need = 1;
731 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
732 thread_unlock(ie->ie_thread->it_thread);
733 while (handler->ih_flags & IH_DEAD)
734 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
735 intr_event_update(ie);
738 * XXX: This could be bad in the case of ppbus(8). Also, I think
739 * this could lead to races of stale data when servicing an
743 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
744 if (!(ih->ih_flags & IH_FAST)) {
750 ithread_destroy(ie->ie_thread);
751 ie->ie_thread = NULL;
754 mtx_unlock(&ie->ie_lock);
755 free(handler, M_ITHREAD);
760 intr_event_schedule_thread(struct intr_event *ie)
762 struct intr_entropy entropy;
763 struct intr_thread *it;
769 * If no ithread or no handlers, then we have a stray interrupt.
771 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
772 ie->ie_thread == NULL)
781 * If any of the handlers for this ithread claim to be good
782 * sources of entropy, then gather some.
784 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
785 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
786 p->p_pid, td->td_name);
787 entropy.event = (uintptr_t)ie;
789 random_harvest(&entropy, sizeof(entropy), 2, 0,
793 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
796 * Set it_need to tell the thread to keep running if it is already
797 * running. Then, lock the thread and see if we actually need to
798 * put it on the runqueue.
802 if (TD_AWAITING_INTR(td)) {
803 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
806 sched_add(td, SRQ_INTR);
808 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
809 __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
817 intr_event_remove_handler(void *cookie)
819 struct intr_handler *handler = (struct intr_handler *)cookie;
820 struct intr_event *ie;
821 struct intr_thread *it;
823 struct intr_handler *ih;
831 ie = handler->ih_event;
833 ("interrupt handler \"%s\" has a NULL interrupt event",
835 mtx_lock(&ie->ie_lock);
836 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
839 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
842 mtx_unlock(&ie->ie_lock);
843 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
844 ih->ih_name, ie->ie_name);
848 * If there are no ithreads (per event and per handler), then
849 * just remove the handler and return.
850 * XXX: Note that an INTR_FAST handler might be running on another CPU!
852 if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
853 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
854 mtx_unlock(&ie->ie_lock);
855 free(handler, M_ITHREAD);
859 /* Private or global ithread? */
860 it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
862 * If the interrupt thread is already running, then just mark this
863 * handler as being dead and let the ithread do the actual removal.
865 * During a cold boot while cold is set, msleep() does not sleep,
866 * so we have to remove the handler here rather than letting the
869 thread_lock(it->it_thread);
870 if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
871 handler->ih_flags |= IH_DEAD;
874 * Ensure that the thread will process the handler list
875 * again and remove this handler if it has already passed
880 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
881 thread_unlock(it->it_thread);
882 while (handler->ih_flags & IH_DEAD)
883 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
885 * At this point, the handler has been disconnected from the event,
886 * so we can kill the private ithread if any.
888 if (handler->ih_thread) {
889 ithread_destroy(handler->ih_thread);
890 handler->ih_thread = NULL;
892 intr_event_update(ie);
895 * XXX: This could be bad in the case of ppbus(8). Also, I think
896 * this could lead to races of stale data when servicing an
900 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
901 if (handler != NULL) {
907 ithread_destroy(ie->ie_thread);
908 ie->ie_thread = NULL;
911 mtx_unlock(&ie->ie_lock);
912 free(handler, M_ITHREAD);
917 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
919 struct intr_entropy entropy;
925 * If no ithread or no handlers, then we have a stray interrupt.
927 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
935 * If any of the handlers for this ithread claim to be good
936 * sources of entropy, then gather some.
938 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
939 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
940 p->p_pid, td->td_name);
941 entropy.event = (uintptr_t)ie;
943 random_harvest(&entropy, sizeof(entropy), 2, 0,
947 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
950 * Set it_need to tell the thread to keep running if it is already
951 * running. Then, lock the thread and see if we actually need to
952 * put it on the runqueue.
956 if (TD_AWAITING_INTR(td)) {
957 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
960 sched_add(td, SRQ_INTR);
962 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
963 __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
972 * Add a software interrupt handler to a specified event. If a given event
973 * is not specified, then a new event is created.
976 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
977 void *arg, int pri, enum intr_type flags, void **cookiep)
979 struct intr_event *ie;
982 if (flags & INTR_ENTROPY)
985 ie = (eventp != NULL) ? *eventp : NULL;
988 if (!(ie->ie_flags & IE_SOFT))
991 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
992 NULL, NULL, NULL, NULL, "swi%d:", pri);
998 error = intr_event_add_handler(ie, name, NULL, handler, arg,
999 (pri * RQ_PPQ) + PI_SOFT, flags, cookiep);
1002 if (pri == SWI_CLOCK) {
1004 p = ie->ie_thread->it_thread->td_proc;
1006 p->p_flag |= P_NOLOAD;
1013 * Schedule a software interrupt thread.
1016 swi_sched(void *cookie, int flags)
1018 struct intr_handler *ih = (struct intr_handler *)cookie;
1019 struct intr_event *ie = ih->ih_event;
1022 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1026 * Set ih_need for this handler so that if the ithread is already
1027 * running it will execute this handler on the next pass. Otherwise,
1028 * it will execute it the next time it runs.
1030 atomic_store_rel_int(&ih->ih_need, 1);
1032 if (!(flags & SWI_DELAY)) {
1033 PCPU_INC(cnt.v_soft);
1035 error = intr_event_schedule_thread(ie, ie->ie_thread);
1037 error = intr_event_schedule_thread(ie);
1039 KASSERT(error == 0, ("stray software interrupt"));
1044 * Remove a software interrupt handler. Currently this code does not
1045 * remove the associated interrupt event if it becomes empty. Calling code
1046 * may do so manually via intr_event_destroy(), but that's not really
1047 * an optimal interface.
1050 swi_remove(void *cookie)
1053 return (intr_event_remove_handler(cookie));
1058 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1060 struct intr_event *ie;
1064 * If this handler is marked for death, remove it from
1065 * the list of handlers and wake up the sleeper.
1067 if (ih->ih_flags & IH_DEAD) {
1068 mtx_lock(&ie->ie_lock);
1069 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1070 ih->ih_flags &= ~IH_DEAD;
1072 mtx_unlock(&ie->ie_lock);
1076 /* Execute this handler. */
1077 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1078 __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1079 ih->ih_name, ih->ih_flags);
1081 if (!(ih->ih_flags & IH_MPSAFE))
1083 ih->ih_handler(ih->ih_argument);
1084 if (!(ih->ih_flags & IH_MPSAFE))
1090 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1092 struct intr_handler *ih, *ihn;
1094 /* Interrupt handlers should not sleep. */
1095 if (!(ie->ie_flags & IE_SOFT))
1096 THREAD_NO_SLEEPING();
1097 TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1100 * If this handler is marked for death, remove it from
1101 * the list of handlers and wake up the sleeper.
1103 if (ih->ih_flags & IH_DEAD) {
1104 mtx_lock(&ie->ie_lock);
1105 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1106 ih->ih_flags &= ~IH_DEAD;
1108 mtx_unlock(&ie->ie_lock);
1112 /* Skip filter only handlers */
1113 if (ih->ih_handler == NULL)
1117 * For software interrupt threads, we only execute
1118 * handlers that have their need flag set. Hardware
1119 * interrupt threads always invoke all of their handlers.
1121 if (ie->ie_flags & IE_SOFT) {
1125 atomic_store_rel_int(&ih->ih_need, 0);
1128 /* Execute this handler. */
1129 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1130 __func__, p->p_pid, (void *)ih->ih_handler,
1131 ih->ih_argument, ih->ih_name, ih->ih_flags);
1133 if (!(ih->ih_flags & IH_MPSAFE))
1135 ih->ih_handler(ih->ih_argument);
1136 if (!(ih->ih_flags & IH_MPSAFE))
1139 if (!(ie->ie_flags & IE_SOFT))
1140 THREAD_SLEEPING_OK();
1143 * Interrupt storm handling:
1145 * If this interrupt source is currently storming, then throttle
1146 * it to only fire the handler once per clock tick.
1148 * If this interrupt source is not currently storming, but the
1149 * number of back to back interrupts exceeds the storm threshold,
1150 * then enter storming mode.
1152 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1153 !(ie->ie_flags & IE_SOFT)) {
1154 /* Report the message only once every second. */
1155 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1157 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1165 * Now that all the handlers have had a chance to run, reenable
1166 * the interrupt source.
1168 if (ie->ie_post_ithread != NULL)
1169 ie->ie_post_ithread(ie->ie_source);
1174 * This is the main code for interrupt threads.
1177 ithread_loop(void *arg)
1179 struct intr_thread *ithd;
1180 struct intr_event *ie;
1186 ithd = (struct intr_thread *)arg;
1187 KASSERT(ithd->it_thread == td,
1188 ("%s: ithread and proc linkage out of sync", __func__));
1189 ie = ithd->it_event;
1193 * As long as we have interrupts outstanding, go through the
1194 * list of handlers, giving each one a go at it.
1198 * If we are an orphaned thread, then just die.
1200 if (ithd->it_flags & IT_DEAD) {
1201 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1202 p->p_pid, td->td_name);
1203 free(ithd, M_ITHREAD);
1208 * Service interrupts. If another interrupt arrives while
1209 * we are running, it will set it_need to note that we
1210 * should make another pass.
1212 while (ithd->it_need) {
1214 * This might need a full read and write barrier
1215 * to make sure that this write posts before any
1216 * of the memory or device accesses in the
1219 atomic_store_rel_int(&ithd->it_need, 0);
1220 ithread_execute_handlers(p, ie);
1222 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1223 mtx_assert(&Giant, MA_NOTOWNED);
1226 * Processed all our interrupts. Now get the sched
1227 * lock. This may take a while and it_need may get
1228 * set again, so we have to check it again.
1231 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1234 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1241 * Main interrupt handling body.
1244 * o ie: the event connected to this interrupt.
1245 * o frame: some archs (i.e. i386) pass a frame to some.
1246 * handlers as their main argument.
1248 * o 0: everything ok.
1249 * o EINVAL: stray interrupt.
1252 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1254 struct intr_handler *ih;
1256 int error, ret, thread;
1260 /* An interrupt with no event or handlers is a stray interrupt. */
1261 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1265 * Execute fast interrupt handlers directly.
1266 * To support clock handlers, if a handler registers
1267 * with a NULL argument, then we pass it a pointer to
1268 * a trapframe as its argument.
1270 td->td_intr_nesting_level++;
1274 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1275 if (ih->ih_filter == NULL) {
1279 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1280 ih->ih_filter, ih->ih_argument == NULL ? frame :
1281 ih->ih_argument, ih->ih_name);
1282 if (ih->ih_argument == NULL)
1283 ret = ih->ih_filter(frame);
1285 ret = ih->ih_filter(ih->ih_argument);
1287 * Wrapper handler special handling:
1289 * in some particular cases (like pccard and pccbb),
1290 * the _real_ device handler is wrapped in a couple of
1291 * functions - a filter wrapper and an ithread wrapper.
1292 * In this case (and just in this case), the filter wrapper
1293 * could ask the system to schedule the ithread and mask
1294 * the interrupt source if the wrapped handler is composed
1295 * of just an ithread handler.
1297 * TODO: write a generic wrapper to avoid people rolling
1301 if (ret == FILTER_SCHEDULE_THREAD)
1307 if (ie->ie_pre_ithread != NULL)
1308 ie->ie_pre_ithread(ie->ie_source);
1310 if (ie->ie_post_filter != NULL)
1311 ie->ie_post_filter(ie->ie_source);
1314 /* Schedule the ithread if needed. */
1316 error = intr_event_schedule_thread(ie);
1317 KASSERT(error == 0, ("bad stray interrupt"));
1320 td->td_intr_nesting_level--;
1325 * This is the main code for interrupt threads.
1328 ithread_loop(void *arg)
1330 struct intr_thread *ithd;
1331 struct intr_handler *ih;
1332 struct intr_event *ie;
1339 ih = (struct intr_handler *)arg;
1340 priv = (ih->ih_thread != NULL) ? 1 : 0;
1341 ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1342 KASSERT(ithd->it_thread == td,
1343 ("%s: ithread and proc linkage out of sync", __func__));
1344 ie = ithd->it_event;
1348 * As long as we have interrupts outstanding, go through the
1349 * list of handlers, giving each one a go at it.
1353 * If we are an orphaned thread, then just die.
1355 if (ithd->it_flags & IT_DEAD) {
1356 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1357 p->p_pid, td->td_name);
1358 free(ithd, M_ITHREAD);
1363 * Service interrupts. If another interrupt arrives while
1364 * we are running, it will set it_need to note that we
1365 * should make another pass.
1367 while (ithd->it_need) {
1369 * This might need a full read and write barrier
1370 * to make sure that this write posts before any
1371 * of the memory or device accesses in the
1374 atomic_store_rel_int(&ithd->it_need, 0);
1376 priv_ithread_execute_handler(p, ih);
1378 ithread_execute_handlers(p, ie);
1380 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1381 mtx_assert(&Giant, MA_NOTOWNED);
1384 * Processed all our interrupts. Now get the sched
1385 * lock. This may take a while and it_need may get
1386 * set again, so we have to check it again.
1389 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1392 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1399 * Main loop for interrupt filter.
1401 * Some architectures (i386, amd64 and arm) require the optional frame
1402 * parameter, and use it as the main argument for fast handler execution
1403 * when ih_argument == NULL.
1406 * o FILTER_STRAY: No filter recognized the event, and no
1407 * filter-less handler is registered on this
1409 * o FILTER_HANDLED: A filter claimed the event and served it.
1410 * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at
1411 * least one filter-less handler on this line.
1412 * o FILTER_HANDLED |
1413 * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for
1414 * scheduling the per-handler ithread.
1416 * In case an ithread has to be scheduled, in *ithd there will be a
1417 * pointer to a struct intr_thread containing the thread to be
1422 intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1423 struct intr_thread **ithd)
1425 struct intr_handler *ih;
1427 int ret, thread_only;
1431 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1433 * Execute fast interrupt handlers directly.
1434 * To support clock handlers, if a handler registers
1435 * with a NULL argument, then we pass it a pointer to
1436 * a trapframe as its argument.
1438 arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1440 CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1441 ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1443 if (ih->ih_filter != NULL)
1444 ret = ih->ih_filter(arg);
1450 if (ret & FILTER_STRAY)
1453 *ithd = ih->ih_thread;
1459 * No filters handled the interrupt and we have at least
1460 * one handler without a filter. In this case, we schedule
1461 * all of the filter-less handlers to run in the ithread.
1464 *ithd = ie->ie_thread;
1465 return (FILTER_SCHEDULE_THREAD);
1467 return (FILTER_STRAY);
1471 * Main interrupt handling body.
1474 * o ie: the event connected to this interrupt.
1475 * o frame: some archs (i.e. i386) pass a frame to some.
1476 * handlers as their main argument.
1478 * o 0: everything ok.
1479 * o EINVAL: stray interrupt.
1482 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1484 struct intr_thread *ithd;
1491 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1494 td->td_intr_nesting_level++;
1497 thread = intr_filter_loop(ie, frame, &ithd);
1498 if (thread & FILTER_HANDLED) {
1499 if (ie->ie_post_filter != NULL)
1500 ie->ie_post_filter(ie->ie_source);
1502 if (ie->ie_pre_ithread != NULL)
1503 ie->ie_pre_ithread(ie->ie_source);
1507 /* Interrupt storm logic */
1508 if (thread & FILTER_STRAY) {
1510 if (ie->ie_count < intr_storm_threshold)
1511 printf("Interrupt stray detection not present\n");
1514 /* Schedule an ithread if needed. */
1515 if (thread & FILTER_SCHEDULE_THREAD) {
1516 if (intr_event_schedule_thread(ie, ithd) != 0)
1517 panic("%s: impossible stray interrupt", __func__);
1519 td->td_intr_nesting_level--;
1526 * Dump details about an interrupt handler
1529 db_dump_intrhand(struct intr_handler *ih)
1533 db_printf("\t%-10s ", ih->ih_name);
1534 switch (ih->ih_pri) {
1559 if (ih->ih_pri >= PI_SOFT)
1562 db_printf("%4u", ih->ih_pri);
1566 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1567 db_printf("(%p)", ih->ih_argument);
1569 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1573 if (ih->ih_flags & IH_EXCLUSIVE) {
1579 if (ih->ih_flags & IH_ENTROPY) {
1582 db_printf("ENTROPY");
1585 if (ih->ih_flags & IH_DEAD) {
1591 if (ih->ih_flags & IH_MPSAFE) {
1594 db_printf("MPSAFE");
1608 * Dump details about a event.
1611 db_dump_intr_event(struct intr_event *ie, int handlers)
1613 struct intr_handler *ih;
1614 struct intr_thread *it;
1617 db_printf("%s ", ie->ie_fullname);
1620 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1622 db_printf("(no thread)");
1623 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1624 (it != NULL && it->it_need)) {
1627 if (ie->ie_flags & IE_SOFT) {
1631 if (ie->ie_flags & IE_ENTROPY) {
1634 db_printf("ENTROPY");
1637 if (ie->ie_flags & IE_ADDING_THREAD) {
1640 db_printf("ADDING_THREAD");
1643 if (it != NULL && it->it_need) {
1653 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1654 db_dump_intrhand(ih);
1658 * Dump data about interrupt handlers
1660 DB_SHOW_COMMAND(intr, db_show_intr)
1662 struct intr_event *ie;
1665 verbose = index(modif, 'v') != NULL;
1666 all = index(modif, 'a') != NULL;
1667 TAILQ_FOREACH(ie, &event_list, ie_list) {
1668 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1670 db_dump_intr_event(ie, verbose);
1678 * Start standard software interrupt threads
1681 start_softintr(void *dummy)
1684 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1685 panic("died while creating vm swi ithread");
1687 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1691 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1692 * The data for this machine dependent, and the declarations are in machine
1693 * dependent code. The layout of intrnames and intrcnt however is machine
1696 * We do not know the length of intrcnt and intrnames at compile time, so
1697 * calculate things at run time.
1700 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1702 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1706 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1707 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1710 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1712 return (sysctl_handle_opaque(oidp, intrcnt,
1713 (char *)eintrcnt - (char *)intrcnt, req));
1716 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1717 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1721 * DDB command to dump the interrupt statistics.
1723 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1729 for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
1733 db_printf("%s\t%lu\n", cp, *i);
1734 cp += strlen(cp) + 1;