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;
360 * If we're setting all cpus we can unbind. Otherwise make sure
361 * only one cpu is in the set.
363 if (CPU_CMP(cpuset_root, mask)) {
364 for (n = 0; n < CPU_SETSIZE; n++) {
365 if (!CPU_ISSET(n, mask))
372 ie = intr_lookup(irq);
375 intr_event_bind(ie, cpu);
380 intr_getaffinity(int irq, void *m)
382 struct intr_event *ie;
386 ie = intr_lookup(irq);
390 mtx_lock(&ie->ie_lock);
391 if (ie->ie_cpu == NOCPU)
392 CPU_COPY(cpuset_root, mask);
394 CPU_SET(ie->ie_cpu, mask);
395 mtx_unlock(&ie->ie_lock);
400 intr_event_destroy(struct intr_event *ie)
403 mtx_lock(&event_lock);
404 mtx_lock(&ie->ie_lock);
405 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
406 mtx_unlock(&ie->ie_lock);
407 mtx_unlock(&event_lock);
410 TAILQ_REMOVE(&event_list, ie, ie_list);
412 if (ie->ie_thread != NULL) {
413 ithread_destroy(ie->ie_thread);
414 ie->ie_thread = NULL;
417 mtx_unlock(&ie->ie_lock);
418 mtx_unlock(&event_lock);
419 mtx_destroy(&ie->ie_lock);
425 static struct intr_thread *
426 ithread_create(const char *name)
428 struct intr_thread *ithd;
432 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
434 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
435 &td, RFSTOPPED | RFHIGHPID,
436 0, "intr", "%s", name);
438 panic("kproc_create() failed with %d", error);
440 sched_class(td, PRI_ITHD);
443 td->td_pflags |= TDP_ITHREAD;
444 ithd->it_thread = td;
445 CTR2(KTR_INTR, "%s: created %s", __func__, name);
449 static struct intr_thread *
450 ithread_create(const char *name, struct intr_handler *ih)
452 struct intr_thread *ithd;
456 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
458 error = kproc_kthread_add(ithread_loop, ih, &intrproc,
459 &td, RFSTOPPED | RFHIGHPID,
460 0, "intr", "%s", name);
462 panic("kproc_create() failed with %d", error);
464 sched_class(td, PRI_ITHD);
467 td->td_pflags |= TDP_ITHREAD;
468 ithd->it_thread = td;
469 CTR2(KTR_INTR, "%s: created %s", __func__, name);
475 ithread_destroy(struct intr_thread *ithread)
479 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
480 td = ithread->it_thread;
482 ithread->it_flags |= IT_DEAD;
483 if (TD_AWAITING_INTR(td)) {
485 sched_add(td, SRQ_INTR);
492 intr_event_add_handler(struct intr_event *ie, const char *name,
493 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
494 enum intr_type flags, void **cookiep)
496 struct intr_handler *ih, *temp_ih;
497 struct intr_thread *it;
499 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
502 /* Allocate and populate an interrupt handler structure. */
503 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
504 ih->ih_filter = filter;
505 ih->ih_handler = handler;
506 ih->ih_argument = arg;
510 if (flags & INTR_EXCL)
511 ih->ih_flags = IH_EXCLUSIVE;
512 if (flags & INTR_MPSAFE)
513 ih->ih_flags |= IH_MPSAFE;
514 if (flags & INTR_ENTROPY)
515 ih->ih_flags |= IH_ENTROPY;
517 /* We can only have one exclusive handler in a event. */
518 mtx_lock(&ie->ie_lock);
519 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
520 if ((flags & INTR_EXCL) ||
521 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
522 mtx_unlock(&ie->ie_lock);
528 /* Add the new handler to the event in priority order. */
529 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
530 if (temp_ih->ih_pri > ih->ih_pri)
534 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
536 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
537 intr_event_update(ie);
539 /* Create a thread if we need one. */
540 while (ie->ie_thread == NULL && handler != NULL) {
541 if (ie->ie_flags & IE_ADDING_THREAD)
542 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
544 ie->ie_flags |= IE_ADDING_THREAD;
545 mtx_unlock(&ie->ie_lock);
546 it = ithread_create("intr: newborn");
547 mtx_lock(&ie->ie_lock);
548 ie->ie_flags &= ~IE_ADDING_THREAD;
555 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
557 mtx_unlock(&ie->ie_lock);
565 intr_event_add_handler(struct intr_event *ie, const char *name,
566 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
567 enum intr_type flags, void **cookiep)
569 struct intr_handler *ih, *temp_ih;
570 struct intr_thread *it;
572 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
575 /* Allocate and populate an interrupt handler structure. */
576 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
577 ih->ih_filter = filter;
578 ih->ih_handler = handler;
579 ih->ih_argument = arg;
583 if (flags & INTR_EXCL)
584 ih->ih_flags = IH_EXCLUSIVE;
585 if (flags & INTR_MPSAFE)
586 ih->ih_flags |= IH_MPSAFE;
587 if (flags & INTR_ENTROPY)
588 ih->ih_flags |= IH_ENTROPY;
590 /* We can only have one exclusive handler in a event. */
591 mtx_lock(&ie->ie_lock);
592 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
593 if ((flags & INTR_EXCL) ||
594 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
595 mtx_unlock(&ie->ie_lock);
601 /* Add the new handler to the event in priority order. */
602 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
603 if (temp_ih->ih_pri > ih->ih_pri)
607 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
609 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
610 intr_event_update(ie);
612 /* For filtered handlers, create a private ithread to run on. */
613 if (filter != NULL && handler != NULL) {
614 mtx_unlock(&ie->ie_lock);
615 it = ithread_create("intr: newborn", ih);
616 mtx_lock(&ie->ie_lock);
619 ithread_update(it); // XXX - do we really need this?!?!?
620 } else { /* Create the global per-event thread if we need one. */
621 while (ie->ie_thread == NULL && handler != NULL) {
622 if (ie->ie_flags & IE_ADDING_THREAD)
623 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
625 ie->ie_flags |= IE_ADDING_THREAD;
626 mtx_unlock(&ie->ie_lock);
627 it = ithread_create("intr: newborn", ih);
628 mtx_lock(&ie->ie_lock);
629 ie->ie_flags &= ~IE_ADDING_THREAD;
637 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
639 mtx_unlock(&ie->ie_lock);
648 * Return the ie_source field from the intr_event an intr_handler is
652 intr_handler_source(void *cookie)
654 struct intr_handler *ih;
655 struct intr_event *ie;
657 ih = (struct intr_handler *)cookie;
662 ("interrupt handler \"%s\" has a NULL interrupt event",
664 return (ie->ie_source);
669 intr_event_remove_handler(void *cookie)
671 struct intr_handler *handler = (struct intr_handler *)cookie;
672 struct intr_event *ie;
674 struct intr_handler *ih;
682 ie = handler->ih_event;
684 ("interrupt handler \"%s\" has a NULL interrupt event",
686 mtx_lock(&ie->ie_lock);
687 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
690 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
693 mtx_unlock(&ie->ie_lock);
694 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
695 ih->ih_name, ie->ie_name);
699 * If there is no ithread, then just remove the handler and return.
700 * XXX: Note that an INTR_FAST handler might be running on another
703 if (ie->ie_thread == NULL) {
704 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
705 mtx_unlock(&ie->ie_lock);
706 free(handler, M_ITHREAD);
711 * If the interrupt thread is already running, then just mark this
712 * handler as being dead and let the ithread do the actual removal.
714 * During a cold boot while cold is set, msleep() does not sleep,
715 * so we have to remove the handler here rather than letting the
718 thread_lock(ie->ie_thread->it_thread);
719 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
720 handler->ih_flags |= IH_DEAD;
723 * Ensure that the thread will process the handler list
724 * again and remove this handler if it has already passed
727 ie->ie_thread->it_need = 1;
729 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
730 thread_unlock(ie->ie_thread->it_thread);
731 while (handler->ih_flags & IH_DEAD)
732 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
733 intr_event_update(ie);
736 * XXX: This could be bad in the case of ppbus(8). Also, I think
737 * this could lead to races of stale data when servicing an
741 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
742 if (!(ih->ih_flags & IH_FAST)) {
748 ithread_destroy(ie->ie_thread);
749 ie->ie_thread = NULL;
752 mtx_unlock(&ie->ie_lock);
753 free(handler, M_ITHREAD);
758 intr_event_schedule_thread(struct intr_event *ie)
760 struct intr_entropy entropy;
761 struct intr_thread *it;
767 * If no ithread or no handlers, then we have a stray interrupt.
769 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
770 ie->ie_thread == NULL)
779 * If any of the handlers for this ithread claim to be good
780 * sources of entropy, then gather some.
782 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
783 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
784 p->p_pid, td->td_name);
785 entropy.event = (uintptr_t)ie;
787 random_harvest(&entropy, sizeof(entropy), 2, 0,
791 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
794 * Set it_need to tell the thread to keep running if it is already
795 * running. Then, lock the thread and see if we actually need to
796 * put it on the runqueue.
800 if (TD_AWAITING_INTR(td)) {
801 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
804 sched_add(td, SRQ_INTR);
806 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
807 __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
815 intr_event_remove_handler(void *cookie)
817 struct intr_handler *handler = (struct intr_handler *)cookie;
818 struct intr_event *ie;
819 struct intr_thread *it;
821 struct intr_handler *ih;
829 ie = handler->ih_event;
831 ("interrupt handler \"%s\" has a NULL interrupt event",
833 mtx_lock(&ie->ie_lock);
834 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
837 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
840 mtx_unlock(&ie->ie_lock);
841 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
842 ih->ih_name, ie->ie_name);
846 * If there are no ithreads (per event and per handler), then
847 * just remove the handler and return.
848 * XXX: Note that an INTR_FAST handler might be running on another CPU!
850 if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
851 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
852 mtx_unlock(&ie->ie_lock);
853 free(handler, M_ITHREAD);
857 /* Private or global ithread? */
858 it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
860 * If the interrupt thread is already running, then just mark this
861 * handler as being dead and let the ithread do the actual removal.
863 * During a cold boot while cold is set, msleep() does not sleep,
864 * so we have to remove the handler here rather than letting the
867 thread_lock(it->it_thread);
868 if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
869 handler->ih_flags |= IH_DEAD;
872 * Ensure that the thread will process the handler list
873 * again and remove this handler if it has already passed
878 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
879 thread_unlock(it->it_thread);
880 while (handler->ih_flags & IH_DEAD)
881 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
883 * At this point, the handler has been disconnected from the event,
884 * so we can kill the private ithread if any.
886 if (handler->ih_thread) {
887 ithread_destroy(handler->ih_thread);
888 handler->ih_thread = NULL;
890 intr_event_update(ie);
893 * XXX: This could be bad in the case of ppbus(8). Also, I think
894 * this could lead to races of stale data when servicing an
898 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
899 if (handler != NULL) {
905 ithread_destroy(ie->ie_thread);
906 ie->ie_thread = NULL;
909 mtx_unlock(&ie->ie_lock);
910 free(handler, M_ITHREAD);
915 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
917 struct intr_entropy entropy;
923 * If no ithread or no handlers, then we have a stray interrupt.
925 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
933 * If any of the handlers for this ithread claim to be good
934 * sources of entropy, then gather some.
936 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
937 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
938 p->p_pid, td->td_name);
939 entropy.event = (uintptr_t)ie;
941 random_harvest(&entropy, sizeof(entropy), 2, 0,
945 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
948 * Set it_need to tell the thread to keep running if it is already
949 * running. Then, lock the thread and see if we actually need to
950 * put it on the runqueue.
954 if (TD_AWAITING_INTR(td)) {
955 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
958 sched_add(td, SRQ_INTR);
960 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
961 __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
970 * Add a software interrupt handler to a specified event. If a given event
971 * is not specified, then a new event is created.
974 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
975 void *arg, int pri, enum intr_type flags, void **cookiep)
977 struct intr_event *ie;
980 if (flags & INTR_ENTROPY)
983 ie = (eventp != NULL) ? *eventp : NULL;
986 if (!(ie->ie_flags & IE_SOFT))
989 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
990 NULL, NULL, NULL, NULL, "swi%d:", pri);
996 error = intr_event_add_handler(ie, name, NULL, handler, arg,
997 (pri * RQ_PPQ) + PI_SOFT, flags, cookiep);
1000 if (pri == SWI_CLOCK) {
1002 p = ie->ie_thread->it_thread->td_proc;
1004 p->p_flag |= P_NOLOAD;
1011 * Schedule a software interrupt thread.
1014 swi_sched(void *cookie, int flags)
1016 struct intr_handler *ih = (struct intr_handler *)cookie;
1017 struct intr_event *ie = ih->ih_event;
1020 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1024 * Set ih_need for this handler so that if the ithread is already
1025 * running it will execute this handler on the next pass. Otherwise,
1026 * it will execute it the next time it runs.
1028 atomic_store_rel_int(&ih->ih_need, 1);
1030 if (!(flags & SWI_DELAY)) {
1031 PCPU_INC(cnt.v_soft);
1033 error = intr_event_schedule_thread(ie, ie->ie_thread);
1035 error = intr_event_schedule_thread(ie);
1037 KASSERT(error == 0, ("stray software interrupt"));
1042 * Remove a software interrupt handler. Currently this code does not
1043 * remove the associated interrupt event if it becomes empty. Calling code
1044 * may do so manually via intr_event_destroy(), but that's not really
1045 * an optimal interface.
1048 swi_remove(void *cookie)
1051 return (intr_event_remove_handler(cookie));
1056 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1058 struct intr_event *ie;
1062 * If this handler is marked for death, remove it from
1063 * the list of handlers and wake up the sleeper.
1065 if (ih->ih_flags & IH_DEAD) {
1066 mtx_lock(&ie->ie_lock);
1067 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1068 ih->ih_flags &= ~IH_DEAD;
1070 mtx_unlock(&ie->ie_lock);
1074 /* Execute this handler. */
1075 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1076 __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1077 ih->ih_name, ih->ih_flags);
1079 if (!(ih->ih_flags & IH_MPSAFE))
1081 ih->ih_handler(ih->ih_argument);
1082 if (!(ih->ih_flags & IH_MPSAFE))
1088 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1090 struct intr_handler *ih, *ihn;
1092 /* Interrupt handlers should not sleep. */
1093 if (!(ie->ie_flags & IE_SOFT))
1094 THREAD_NO_SLEEPING();
1095 TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1098 * If this handler is marked for death, remove it from
1099 * the list of handlers and wake up the sleeper.
1101 if (ih->ih_flags & IH_DEAD) {
1102 mtx_lock(&ie->ie_lock);
1103 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1104 ih->ih_flags &= ~IH_DEAD;
1106 mtx_unlock(&ie->ie_lock);
1110 /* Skip filter only handlers */
1111 if (ih->ih_handler == NULL)
1115 * For software interrupt threads, we only execute
1116 * handlers that have their need flag set. Hardware
1117 * interrupt threads always invoke all of their handlers.
1119 if (ie->ie_flags & IE_SOFT) {
1123 atomic_store_rel_int(&ih->ih_need, 0);
1126 /* Execute this handler. */
1127 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1128 __func__, p->p_pid, (void *)ih->ih_handler,
1129 ih->ih_argument, ih->ih_name, ih->ih_flags);
1131 if (!(ih->ih_flags & IH_MPSAFE))
1133 ih->ih_handler(ih->ih_argument);
1134 if (!(ih->ih_flags & IH_MPSAFE))
1137 if (!(ie->ie_flags & IE_SOFT))
1138 THREAD_SLEEPING_OK();
1141 * Interrupt storm handling:
1143 * If this interrupt source is currently storming, then throttle
1144 * it to only fire the handler once per clock tick.
1146 * If this interrupt source is not currently storming, but the
1147 * number of back to back interrupts exceeds the storm threshold,
1148 * then enter storming mode.
1150 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1151 !(ie->ie_flags & IE_SOFT)) {
1152 /* Report the message only once every second. */
1153 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1155 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1163 * Now that all the handlers have had a chance to run, reenable
1164 * the interrupt source.
1166 if (ie->ie_post_ithread != NULL)
1167 ie->ie_post_ithread(ie->ie_source);
1172 * This is the main code for interrupt threads.
1175 ithread_loop(void *arg)
1177 struct intr_thread *ithd;
1178 struct intr_event *ie;
1184 ithd = (struct intr_thread *)arg;
1185 KASSERT(ithd->it_thread == td,
1186 ("%s: ithread and proc linkage out of sync", __func__));
1187 ie = ithd->it_event;
1191 * As long as we have interrupts outstanding, go through the
1192 * list of handlers, giving each one a go at it.
1196 * If we are an orphaned thread, then just die.
1198 if (ithd->it_flags & IT_DEAD) {
1199 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1200 p->p_pid, td->td_name);
1201 free(ithd, M_ITHREAD);
1206 * Service interrupts. If another interrupt arrives while
1207 * we are running, it will set it_need to note that we
1208 * should make another pass.
1210 while (ithd->it_need) {
1212 * This might need a full read and write barrier
1213 * to make sure that this write posts before any
1214 * of the memory or device accesses in the
1217 atomic_store_rel_int(&ithd->it_need, 0);
1218 ithread_execute_handlers(p, ie);
1220 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1221 mtx_assert(&Giant, MA_NOTOWNED);
1224 * Processed all our interrupts. Now get the sched
1225 * lock. This may take a while and it_need may get
1226 * set again, so we have to check it again.
1229 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1232 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1239 * Main interrupt handling body.
1242 * o ie: the event connected to this interrupt.
1243 * o frame: some archs (i.e. i386) pass a frame to some.
1244 * handlers as their main argument.
1246 * o 0: everything ok.
1247 * o EINVAL: stray interrupt.
1250 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1252 struct intr_handler *ih;
1254 int error, ret, thread;
1258 /* An interrupt with no event or handlers is a stray interrupt. */
1259 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1263 * Execute fast interrupt handlers directly.
1264 * To support clock handlers, if a handler registers
1265 * with a NULL argument, then we pass it a pointer to
1266 * a trapframe as its argument.
1268 td->td_intr_nesting_level++;
1272 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1273 if (ih->ih_filter == NULL) {
1277 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1278 ih->ih_filter, ih->ih_argument == NULL ? frame :
1279 ih->ih_argument, ih->ih_name);
1280 if (ih->ih_argument == NULL)
1281 ret = ih->ih_filter(frame);
1283 ret = ih->ih_filter(ih->ih_argument);
1285 * Wrapper handler special handling:
1287 * in some particular cases (like pccard and pccbb),
1288 * the _real_ device handler is wrapped in a couple of
1289 * functions - a filter wrapper and an ithread wrapper.
1290 * In this case (and just in this case), the filter wrapper
1291 * could ask the system to schedule the ithread and mask
1292 * the interrupt source if the wrapped handler is composed
1293 * of just an ithread handler.
1295 * TODO: write a generic wrapper to avoid people rolling
1299 if (ret == FILTER_SCHEDULE_THREAD)
1305 if (ie->ie_pre_ithread != NULL)
1306 ie->ie_pre_ithread(ie->ie_source);
1308 if (ie->ie_post_filter != NULL)
1309 ie->ie_post_filter(ie->ie_source);
1312 /* Schedule the ithread if needed. */
1314 error = intr_event_schedule_thread(ie);
1315 KASSERT(error == 0, ("bad stray interrupt"));
1318 td->td_intr_nesting_level--;
1323 * This is the main code for interrupt threads.
1326 ithread_loop(void *arg)
1328 struct intr_thread *ithd;
1329 struct intr_handler *ih;
1330 struct intr_event *ie;
1337 ih = (struct intr_handler *)arg;
1338 priv = (ih->ih_thread != NULL) ? 1 : 0;
1339 ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1340 KASSERT(ithd->it_thread == td,
1341 ("%s: ithread and proc linkage out of sync", __func__));
1342 ie = ithd->it_event;
1346 * As long as we have interrupts outstanding, go through the
1347 * list of handlers, giving each one a go at it.
1351 * If we are an orphaned thread, then just die.
1353 if (ithd->it_flags & IT_DEAD) {
1354 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1355 p->p_pid, td->td_name);
1356 free(ithd, M_ITHREAD);
1361 * Service interrupts. If another interrupt arrives while
1362 * we are running, it will set it_need to note that we
1363 * should make another pass.
1365 while (ithd->it_need) {
1367 * This might need a full read and write barrier
1368 * to make sure that this write posts before any
1369 * of the memory or device accesses in the
1372 atomic_store_rel_int(&ithd->it_need, 0);
1374 priv_ithread_execute_handler(p, ih);
1376 ithread_execute_handlers(p, ie);
1378 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1379 mtx_assert(&Giant, MA_NOTOWNED);
1382 * Processed all our interrupts. Now get the sched
1383 * lock. This may take a while and it_need may get
1384 * set again, so we have to check it again.
1387 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1390 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1397 * Main loop for interrupt filter.
1399 * Some architectures (i386, amd64 and arm) require the optional frame
1400 * parameter, and use it as the main argument for fast handler execution
1401 * when ih_argument == NULL.
1404 * o FILTER_STRAY: No filter recognized the event, and no
1405 * filter-less handler is registered on this
1407 * o FILTER_HANDLED: A filter claimed the event and served it.
1408 * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at
1409 * least one filter-less handler on this line.
1410 * o FILTER_HANDLED |
1411 * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for
1412 * scheduling the per-handler ithread.
1414 * In case an ithread has to be scheduled, in *ithd there will be a
1415 * pointer to a struct intr_thread containing the thread to be
1420 intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1421 struct intr_thread **ithd)
1423 struct intr_handler *ih;
1425 int ret, thread_only;
1429 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1431 * Execute fast interrupt handlers directly.
1432 * To support clock handlers, if a handler registers
1433 * with a NULL argument, then we pass it a pointer to
1434 * a trapframe as its argument.
1436 arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1438 CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1439 ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1441 if (ih->ih_filter != NULL)
1442 ret = ih->ih_filter(arg);
1448 if (ret & FILTER_STRAY)
1451 *ithd = ih->ih_thread;
1457 * No filters handled the interrupt and we have at least
1458 * one handler without a filter. In this case, we schedule
1459 * all of the filter-less handlers to run in the ithread.
1462 *ithd = ie->ie_thread;
1463 return (FILTER_SCHEDULE_THREAD);
1465 return (FILTER_STRAY);
1469 * Main interrupt handling body.
1472 * o ie: the event connected to this interrupt.
1473 * o frame: some archs (i.e. i386) pass a frame to some.
1474 * handlers as their main argument.
1476 * o 0: everything ok.
1477 * o EINVAL: stray interrupt.
1480 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1482 struct intr_thread *ithd;
1489 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1492 td->td_intr_nesting_level++;
1495 thread = intr_filter_loop(ie, frame, &ithd);
1496 if (thread & FILTER_HANDLED) {
1497 if (ie->ie_post_filter != NULL)
1498 ie->ie_post_filter(ie->ie_source);
1500 if (ie->ie_pre_ithread != NULL)
1501 ie->ie_pre_ithread(ie->ie_source);
1505 /* Interrupt storm logic */
1506 if (thread & FILTER_STRAY) {
1508 if (ie->ie_count < intr_storm_threshold)
1509 printf("Interrupt stray detection not present\n");
1512 /* Schedule an ithread if needed. */
1513 if (thread & FILTER_SCHEDULE_THREAD) {
1514 if (intr_event_schedule_thread(ie, ithd) != 0)
1515 panic("%s: impossible stray interrupt", __func__);
1517 td->td_intr_nesting_level--;
1524 * Dump details about an interrupt handler
1527 db_dump_intrhand(struct intr_handler *ih)
1531 db_printf("\t%-10s ", ih->ih_name);
1532 switch (ih->ih_pri) {
1557 if (ih->ih_pri >= PI_SOFT)
1560 db_printf("%4u", ih->ih_pri);
1564 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1565 db_printf("(%p)", ih->ih_argument);
1567 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1571 if (ih->ih_flags & IH_EXCLUSIVE) {
1577 if (ih->ih_flags & IH_ENTROPY) {
1580 db_printf("ENTROPY");
1583 if (ih->ih_flags & IH_DEAD) {
1589 if (ih->ih_flags & IH_MPSAFE) {
1592 db_printf("MPSAFE");
1606 * Dump details about a event.
1609 db_dump_intr_event(struct intr_event *ie, int handlers)
1611 struct intr_handler *ih;
1612 struct intr_thread *it;
1615 db_printf("%s ", ie->ie_fullname);
1618 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1620 db_printf("(no thread)");
1621 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1622 (it != NULL && it->it_need)) {
1625 if (ie->ie_flags & IE_SOFT) {
1629 if (ie->ie_flags & IE_ENTROPY) {
1632 db_printf("ENTROPY");
1635 if (ie->ie_flags & IE_ADDING_THREAD) {
1638 db_printf("ADDING_THREAD");
1641 if (it != NULL && it->it_need) {
1651 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1652 db_dump_intrhand(ih);
1656 * Dump data about interrupt handlers
1658 DB_SHOW_COMMAND(intr, db_show_intr)
1660 struct intr_event *ie;
1663 verbose = index(modif, 'v') != NULL;
1664 all = index(modif, 'a') != NULL;
1665 TAILQ_FOREACH(ie, &event_list, ie_list) {
1666 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1668 db_dump_intr_event(ie, verbose);
1676 * Start standard software interrupt threads
1679 start_softintr(void *dummy)
1682 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1683 panic("died while creating vm swi ithread");
1685 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1689 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1690 * The data for this machine dependent, and the declarations are in machine
1691 * dependent code. The layout of intrnames and intrcnt however is machine
1694 * We do not know the length of intrcnt and intrnames at compile time, so
1695 * calculate things at run time.
1698 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1700 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1704 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1705 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1708 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1710 return (sysctl_handle_opaque(oidp, intrcnt,
1711 (char *)eintrcnt - (char *)intrcnt, req));
1714 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1715 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1719 * DDB command to dump the interrupt statistics.
1721 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1727 for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
1731 db_printf("%s\t%lu\n", cp, *i);
1732 cp += strlen(cp) + 1;