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/rtprio.h>
36 #include <sys/systm.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
41 #include <sys/limits.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
46 #include <sys/random.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sched.h>
50 #include <sys/sysctl.h>
51 #include <sys/unistd.h>
52 #include <sys/vmmeter.h>
53 #include <machine/atomic.h>
54 #include <machine/cpu.h>
55 #include <machine/md_var.h>
56 #include <machine/stdarg.h>
59 #include <ddb/db_sym.h>
63 * Describe an interrupt thread. There is one of these per interrupt event.
66 struct intr_event *it_event;
67 struct thread *it_thread; /* Kernel thread. */
68 int it_flags; /* (j) IT_* flags. */
69 int it_need; /* Needs service. */
72 /* Interrupt thread flags kept in it_flags */
73 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
80 struct intr_event *clk_intr_event;
81 struct intr_event *tty_intr_event;
85 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
87 static int intr_storm_threshold = 1000;
88 TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
89 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
90 &intr_storm_threshold, 0,
91 "Number of consecutive interrupts before storm protection is enabled");
92 static TAILQ_HEAD(, intr_event) event_list =
93 TAILQ_HEAD_INITIALIZER(event_list);
95 static void intr_event_update(struct intr_event *ie);
97 static struct intr_thread *ithread_create(const char *name,
98 struct intr_handler *ih);
100 static struct intr_thread *ithread_create(const char *name);
102 static void ithread_destroy(struct intr_thread *ithread);
103 static void ithread_execute_handlers(struct proc *p,
104 struct intr_event *ie);
106 static void priv_ithread_execute_handler(struct proc *p,
107 struct intr_handler *ih);
109 static void ithread_loop(void *);
110 static void ithread_update(struct intr_thread *ithd);
111 static void start_softintr(void *);
113 /* Map an interrupt type to an ithread priority. */
115 intr_priority(enum intr_type flags)
119 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
120 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
127 * XXX We need to refine this. BSD/OS distinguishes
128 * between tape and disk priorities.
136 pri = PI_DISK; /* XXX or PI_CAM? */
138 case INTR_TYPE_AV: /* Audio/video */
145 pri = PI_DULL; /* don't care */
148 /* We didn't specify an interrupt level. */
149 panic("intr_priority: no interrupt type in flags");
156 * Update an ithread based on the associated intr_event.
159 ithread_update(struct intr_thread *ithd)
161 struct intr_event *ie;
166 td = ithd->it_thread;
168 /* Determine the overall priority of this event. */
169 if (TAILQ_EMPTY(&ie->ie_handlers))
172 pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
174 /* Update name and priority. */
175 strlcpy(td->td_proc->p_comm, ie->ie_fullname,
176 sizeof(td->td_proc->p_comm));
183 * Regenerate the full name of an interrupt event and update its priority.
186 intr_event_update(struct intr_event *ie)
188 struct intr_handler *ih;
192 /* Start off with no entropy and just the name of the event. */
193 mtx_assert(&ie->ie_lock, MA_OWNED);
194 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
195 ie->ie_flags &= ~IE_ENTROPY;
199 /* Run through all the handlers updating values. */
200 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
201 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
202 sizeof(ie->ie_fullname)) {
203 strcat(ie->ie_fullname, " ");
204 strcat(ie->ie_fullname, ih->ih_name);
208 if (ih->ih_flags & IH_ENTROPY)
209 ie->ie_flags |= IE_ENTROPY;
213 * If the handler names were too long, add +'s to indicate missing
214 * names. If we run out of room and still have +'s to add, change
215 * the last character from a + to a *.
217 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
218 while (missed-- > 0) {
219 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
226 strcat(ie->ie_fullname, " +");
229 strcat(ie->ie_fullname, "+");
233 * If this event has an ithread, update it's priority and
236 if (ie->ie_thread != NULL)
237 ithread_update(ie->ie_thread);
238 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
243 intr_event_create(struct intr_event **event, void *source, int flags,
244 void (*enable)(void *), int (*assign_cpu)(void *, u_char), const char *fmt,
247 struct intr_event *ie;
250 /* The only valid flag during creation is IE_SOFT. */
251 if ((flags & ~IE_SOFT) != 0)
253 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
254 ie->ie_source = source;
255 ie->ie_enable = enable;
256 ie->ie_assign_cpu = assign_cpu;
257 ie->ie_flags = flags;
259 TAILQ_INIT(&ie->ie_handlers);
260 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
263 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
265 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
266 mtx_pool_lock(mtxpool_sleep, &event_list);
267 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
268 mtx_pool_unlock(mtxpool_sleep, &event_list);
271 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
276 intr_event_create(struct intr_event **event, void *source, int flags,
277 void (*enable)(void *), void (*eoi)(void *), void (*disab)(void *),
278 int (*assign_cpu)(void *, u_char), const char *fmt, ...)
280 struct intr_event *ie;
283 /* The only valid flag during creation is IE_SOFT. */
284 if ((flags & ~IE_SOFT) != 0)
286 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
287 ie->ie_source = source;
288 ie->ie_enable = enable;
289 ie->ie_assign_cpu = assign_cpu;
291 ie->ie_disab = disab;
292 ie->ie_flags = flags;
294 TAILQ_INIT(&ie->ie_handlers);
295 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
298 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
300 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
301 mtx_pool_lock(mtxpool_sleep, &event_list);
302 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
303 mtx_pool_unlock(mtxpool_sleep, &event_list);
306 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
312 * Bind an interrupt event to the specified CPU. Note that not all
313 * platforms support binding an interrupt to a CPU. For those
314 * platforms this request will fail. For supported platforms, any
315 * associated ithreads as well as the primary interrupt context will
316 * be bound to the specificed CPU. Using a cpu id of NOCPU unbinds
317 * the interrupt event.
320 intr_event_bind(struct intr_event *ie, u_char cpu)
325 /* Need a CPU to bind to. */
326 if (cpu != NOCPU && CPU_ABSENT(cpu))
329 if (ie->ie_assign_cpu == NULL)
332 /* Don't allow a bind request if the interrupt is already bound. */
333 mtx_lock(&ie->ie_lock);
334 if (ie->ie_cpu != NOCPU && cpu != NOCPU) {
335 mtx_unlock(&ie->ie_lock);
338 mtx_unlock(&ie->ie_lock);
340 error = ie->ie_assign_cpu(ie->ie_source, cpu);
343 mtx_lock(&ie->ie_lock);
344 if (ie->ie_thread != NULL)
345 td = ie->ie_thread->it_thread;
353 mtx_unlock(&ie->ie_lock);
358 intr_event_destroy(struct intr_event *ie)
361 mtx_lock(&ie->ie_lock);
362 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
363 mtx_unlock(&ie->ie_lock);
366 mtx_pool_lock(mtxpool_sleep, &event_list);
367 TAILQ_REMOVE(&event_list, ie, ie_list);
368 mtx_pool_unlock(mtxpool_sleep, &event_list);
370 if (ie->ie_thread != NULL) {
371 ithread_destroy(ie->ie_thread);
372 ie->ie_thread = NULL;
375 mtx_unlock(&ie->ie_lock);
376 mtx_destroy(&ie->ie_lock);
382 static struct intr_thread *
383 ithread_create(const char *name)
385 struct intr_thread *ithd;
390 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
392 error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
395 panic("kthread_create() failed with %d", error);
396 td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */
398 sched_class(td, PRI_ITHD);
401 td->td_pflags |= TDP_ITHREAD;
402 ithd->it_thread = td;
403 CTR2(KTR_INTR, "%s: created %s", __func__, name);
407 static struct intr_thread *
408 ithread_create(const char *name, struct intr_handler *ih)
410 struct intr_thread *ithd;
415 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
417 error = kthread_create(ithread_loop, ih, &p, RFSTOPPED | RFHIGHPID,
420 panic("kthread_create() failed with %d", error);
421 td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */
423 sched_class(td, PRI_ITHD);
426 td->td_pflags |= TDP_ITHREAD;
427 ithd->it_thread = td;
428 CTR2(KTR_INTR, "%s: created %s", __func__, name);
434 ithread_destroy(struct intr_thread *ithread)
438 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
439 td = ithread->it_thread;
441 ithread->it_flags |= IT_DEAD;
442 if (TD_AWAITING_INTR(td)) {
444 sched_add(td, SRQ_INTR);
451 intr_event_add_handler(struct intr_event *ie, const char *name,
452 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
453 enum intr_type flags, void **cookiep)
455 struct intr_handler *ih, *temp_ih;
456 struct intr_thread *it;
458 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
461 /* Allocate and populate an interrupt handler structure. */
462 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
463 ih->ih_filter = filter;
464 ih->ih_handler = handler;
465 ih->ih_argument = arg;
469 if (flags & INTR_EXCL)
470 ih->ih_flags = IH_EXCLUSIVE;
471 if (flags & INTR_MPSAFE)
472 ih->ih_flags |= IH_MPSAFE;
473 if (flags & INTR_ENTROPY)
474 ih->ih_flags |= IH_ENTROPY;
476 /* We can only have one exclusive handler in a event. */
477 mtx_lock(&ie->ie_lock);
478 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
479 if ((flags & INTR_EXCL) ||
480 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
481 mtx_unlock(&ie->ie_lock);
487 /* Add the new handler to the event in priority order. */
488 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
489 if (temp_ih->ih_pri > ih->ih_pri)
493 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
495 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
496 intr_event_update(ie);
498 /* Create a thread if we need one. */
499 while (ie->ie_thread == NULL && handler != NULL) {
500 if (ie->ie_flags & IE_ADDING_THREAD)
501 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
503 ie->ie_flags |= IE_ADDING_THREAD;
504 mtx_unlock(&ie->ie_lock);
505 it = ithread_create("intr: newborn");
506 mtx_lock(&ie->ie_lock);
507 ie->ie_flags &= ~IE_ADDING_THREAD;
514 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
516 mtx_unlock(&ie->ie_lock);
524 intr_event_add_handler(struct intr_event *ie, const char *name,
525 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
526 enum intr_type flags, void **cookiep)
528 struct intr_handler *ih, *temp_ih;
529 struct intr_thread *it;
531 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
534 /* Allocate and populate an interrupt handler structure. */
535 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
536 ih->ih_filter = filter;
537 ih->ih_handler = handler;
538 ih->ih_argument = arg;
542 if (flags & INTR_EXCL)
543 ih->ih_flags = IH_EXCLUSIVE;
544 if (flags & INTR_MPSAFE)
545 ih->ih_flags |= IH_MPSAFE;
546 if (flags & INTR_ENTROPY)
547 ih->ih_flags |= IH_ENTROPY;
549 /* We can only have one exclusive handler in a event. */
550 mtx_lock(&ie->ie_lock);
551 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
552 if ((flags & INTR_EXCL) ||
553 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
554 mtx_unlock(&ie->ie_lock);
560 /* Add the new handler to the event in priority order. */
561 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
562 if (temp_ih->ih_pri > ih->ih_pri)
566 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
568 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
569 intr_event_update(ie);
571 /* For filtered handlers, create a private ithread to run on. */
572 if (filter != NULL && handler != NULL) {
573 mtx_unlock(&ie->ie_lock);
574 it = ithread_create("intr: newborn", ih);
575 mtx_lock(&ie->ie_lock);
578 ithread_update(it); // XXX - do we really need this?!?!?
579 } else { /* Create the global per-event thread if we need one. */
580 while (ie->ie_thread == NULL && handler != NULL) {
581 if (ie->ie_flags & IE_ADDING_THREAD)
582 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
584 ie->ie_flags |= IE_ADDING_THREAD;
585 mtx_unlock(&ie->ie_lock);
586 it = ithread_create("intr: newborn", ih);
587 mtx_lock(&ie->ie_lock);
588 ie->ie_flags &= ~IE_ADDING_THREAD;
596 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
598 mtx_unlock(&ie->ie_lock);
607 * Return the ie_source field from the intr_event an intr_handler is
611 intr_handler_source(void *cookie)
613 struct intr_handler *ih;
614 struct intr_event *ie;
616 ih = (struct intr_handler *)cookie;
621 ("interrupt handler \"%s\" has a NULL interrupt event",
623 return (ie->ie_source);
628 intr_event_remove_handler(void *cookie)
630 struct intr_handler *handler = (struct intr_handler *)cookie;
631 struct intr_event *ie;
633 struct intr_handler *ih;
641 ie = handler->ih_event;
643 ("interrupt handler \"%s\" has a NULL interrupt event",
645 mtx_lock(&ie->ie_lock);
646 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
649 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
652 mtx_unlock(&ie->ie_lock);
653 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
654 ih->ih_name, ie->ie_name);
658 * If there is no ithread, then just remove the handler and return.
659 * XXX: Note that an INTR_FAST handler might be running on another
662 if (ie->ie_thread == NULL) {
663 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
664 mtx_unlock(&ie->ie_lock);
665 free(handler, M_ITHREAD);
670 * If the interrupt thread is already running, then just mark this
671 * handler as being dead and let the ithread do the actual removal.
673 * During a cold boot while cold is set, msleep() does not sleep,
674 * so we have to remove the handler here rather than letting the
677 thread_lock(ie->ie_thread->it_thread);
678 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
679 handler->ih_flags |= IH_DEAD;
682 * Ensure that the thread will process the handler list
683 * again and remove this handler if it has already passed
686 ie->ie_thread->it_need = 1;
688 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
689 thread_unlock(ie->ie_thread->it_thread);
690 while (handler->ih_flags & IH_DEAD)
691 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
692 intr_event_update(ie);
695 * XXX: This could be bad in the case of ppbus(8). Also, I think
696 * this could lead to races of stale data when servicing an
700 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
701 if (!(ih->ih_flags & IH_FAST)) {
707 ithread_destroy(ie->ie_thread);
708 ie->ie_thread = NULL;
711 mtx_unlock(&ie->ie_lock);
712 free(handler, M_ITHREAD);
717 intr_event_schedule_thread(struct intr_event *ie)
719 struct intr_entropy entropy;
720 struct intr_thread *it;
726 * If no ithread or no handlers, then we have a stray interrupt.
728 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
729 ie->ie_thread == NULL)
738 * If any of the handlers for this ithread claim to be good
739 * sources of entropy, then gather some.
741 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
742 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
743 p->p_pid, p->p_comm);
744 entropy.event = (uintptr_t)ie;
746 random_harvest(&entropy, sizeof(entropy), 2, 0,
750 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
753 * Set it_need to tell the thread to keep running if it is already
754 * running. Then, lock the thread and see if we actually need to
755 * put it on the runqueue.
759 if (TD_AWAITING_INTR(td)) {
760 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
763 sched_add(td, SRQ_INTR);
765 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
766 __func__, p->p_pid, p->p_comm, it->it_need, td->td_state);
774 intr_event_remove_handler(void *cookie)
776 struct intr_handler *handler = (struct intr_handler *)cookie;
777 struct intr_event *ie;
778 struct intr_thread *it;
780 struct intr_handler *ih;
788 ie = handler->ih_event;
790 ("interrupt handler \"%s\" has a NULL interrupt event",
792 mtx_lock(&ie->ie_lock);
793 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
796 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
799 mtx_unlock(&ie->ie_lock);
800 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
801 ih->ih_name, ie->ie_name);
805 * If there are no ithreads (per event and per handler), then
806 * just remove the handler and return.
807 * XXX: Note that an INTR_FAST handler might be running on another CPU!
809 if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
810 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
811 mtx_unlock(&ie->ie_lock);
812 free(handler, M_ITHREAD);
816 /* Private or global ithread? */
817 it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
819 * If the interrupt thread is already running, then just mark this
820 * handler as being dead and let the ithread do the actual removal.
822 * During a cold boot while cold is set, msleep() does not sleep,
823 * so we have to remove the handler here rather than letting the
826 thread_lock(it->it_thread);
827 if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
828 handler->ih_flags |= IH_DEAD;
831 * Ensure that the thread will process the handler list
832 * again and remove this handler if it has already passed
837 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
838 thread_unlock(it->it_thread);
839 while (handler->ih_flags & IH_DEAD)
840 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
842 * At this point, the handler has been disconnected from the event,
843 * so we can kill the private ithread if any.
845 if (handler->ih_thread) {
846 ithread_destroy(handler->ih_thread);
847 handler->ih_thread = NULL;
849 intr_event_update(ie);
852 * XXX: This could be bad in the case of ppbus(8). Also, I think
853 * this could lead to races of stale data when servicing an
857 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
858 if (handler != NULL) {
864 ithread_destroy(ie->ie_thread);
865 ie->ie_thread = NULL;
868 mtx_unlock(&ie->ie_lock);
869 free(handler, M_ITHREAD);
874 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
876 struct intr_entropy entropy;
882 * If no ithread or no handlers, then we have a stray interrupt.
884 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
892 * If any of the handlers for this ithread claim to be good
893 * sources of entropy, then gather some.
895 if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
896 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
897 p->p_pid, p->p_comm);
898 entropy.event = (uintptr_t)ie;
900 random_harvest(&entropy, sizeof(entropy), 2, 0,
904 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
907 * Set it_need to tell the thread to keep running if it is already
908 * running. Then, lock the thread and see if we actually need to
909 * put it on the runqueue.
913 if (TD_AWAITING_INTR(td)) {
914 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
917 sched_add(td, SRQ_INTR);
919 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
920 __func__, p->p_pid, p->p_comm, it->it_need, td->td_state);
929 * Add a software interrupt handler to a specified event. If a given event
930 * is not specified, then a new event is created.
933 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
934 void *arg, int pri, enum intr_type flags, void **cookiep)
936 struct intr_event *ie;
939 if (flags & INTR_ENTROPY)
942 ie = (eventp != NULL) ? *eventp : NULL;
945 if (!(ie->ie_flags & IE_SOFT))
949 error = intr_event_create(&ie, NULL, IE_SOFT,
950 NULL, NULL, NULL, NULL, "swi%d:", pri);
952 error = intr_event_create(&ie, NULL, IE_SOFT,
953 NULL, NULL, "swi%d:", pri);
960 return (intr_event_add_handler(ie, name, NULL, handler, arg,
961 (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
962 /* XXKSE.. think of a better way to get separate queues */
966 * Schedule a software interrupt thread.
969 swi_sched(void *cookie, int flags)
971 struct intr_handler *ih = (struct intr_handler *)cookie;
972 struct intr_event *ie = ih->ih_event;
975 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
979 * Set ih_need for this handler so that if the ithread is already
980 * running it will execute this handler on the next pass. Otherwise,
981 * it will execute it the next time it runs.
983 atomic_store_rel_int(&ih->ih_need, 1);
985 if (!(flags & SWI_DELAY)) {
986 PCPU_INC(cnt.v_soft);
988 error = intr_event_schedule_thread(ie, ie->ie_thread);
990 error = intr_event_schedule_thread(ie);
992 KASSERT(error == 0, ("stray software interrupt"));
997 * Remove a software interrupt handler. Currently this code does not
998 * remove the associated interrupt event if it becomes empty. Calling code
999 * may do so manually via intr_event_destroy(), but that's not really
1000 * an optimal interface.
1003 swi_remove(void *cookie)
1006 return (intr_event_remove_handler(cookie));
1011 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1013 struct intr_event *ie;
1017 * If this handler is marked for death, remove it from
1018 * the list of handlers and wake up the sleeper.
1020 if (ih->ih_flags & IH_DEAD) {
1021 mtx_lock(&ie->ie_lock);
1022 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1023 ih->ih_flags &= ~IH_DEAD;
1025 mtx_unlock(&ie->ie_lock);
1029 /* Execute this handler. */
1030 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1031 __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1032 ih->ih_name, ih->ih_flags);
1034 if (!(ih->ih_flags & IH_MPSAFE))
1036 ih->ih_handler(ih->ih_argument);
1037 if (!(ih->ih_flags & IH_MPSAFE))
1043 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1045 struct intr_handler *ih, *ihn;
1047 /* Interrupt handlers should not sleep. */
1048 if (!(ie->ie_flags & IE_SOFT))
1049 THREAD_NO_SLEEPING();
1050 TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1053 * If this handler is marked for death, remove it from
1054 * the list of handlers and wake up the sleeper.
1056 if (ih->ih_flags & IH_DEAD) {
1057 mtx_lock(&ie->ie_lock);
1058 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1059 ih->ih_flags &= ~IH_DEAD;
1061 mtx_unlock(&ie->ie_lock);
1065 /* Skip filter only handlers */
1066 if (ih->ih_handler == NULL)
1070 * For software interrupt threads, we only execute
1071 * handlers that have their need flag set. Hardware
1072 * interrupt threads always invoke all of their handlers.
1074 if (ie->ie_flags & IE_SOFT) {
1078 atomic_store_rel_int(&ih->ih_need, 0);
1081 /* Execute this handler. */
1082 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1083 __func__, p->p_pid, (void *)ih->ih_handler,
1084 ih->ih_argument, ih->ih_name, ih->ih_flags);
1086 if (!(ih->ih_flags & IH_MPSAFE))
1088 ih->ih_handler(ih->ih_argument);
1089 if (!(ih->ih_flags & IH_MPSAFE))
1092 if (!(ie->ie_flags & IE_SOFT))
1093 THREAD_SLEEPING_OK();
1096 * Interrupt storm handling:
1098 * If this interrupt source is currently storming, then throttle
1099 * it to only fire the handler once per clock tick.
1101 * If this interrupt source is not currently storming, but the
1102 * number of back to back interrupts exceeds the storm threshold,
1103 * then enter storming mode.
1105 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1106 !(ie->ie_flags & IE_SOFT)) {
1107 /* Report the message only once every second. */
1108 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1110 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1118 * Now that all the handlers have had a chance to run, reenable
1119 * the interrupt source.
1121 if (ie->ie_enable != NULL)
1122 ie->ie_enable(ie->ie_source);
1127 * This is the main code for interrupt threads.
1130 ithread_loop(void *arg)
1132 struct intr_thread *ithd;
1133 struct intr_event *ie;
1140 ithd = (struct intr_thread *)arg;
1141 KASSERT(ithd->it_thread == td,
1142 ("%s: ithread and proc linkage out of sync", __func__));
1143 ie = ithd->it_event;
1148 * As long as we have interrupts outstanding, go through the
1149 * list of handlers, giving each one a go at it.
1153 * If we are an orphaned thread, then just die.
1155 if (ithd->it_flags & IT_DEAD) {
1156 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1157 p->p_pid, p->p_comm);
1158 free(ithd, M_ITHREAD);
1163 * Service interrupts. If another interrupt arrives while
1164 * we are running, it will set it_need to note that we
1165 * should make another pass.
1167 while (ithd->it_need) {
1169 * This might need a full read and write barrier
1170 * to make sure that this write posts before any
1171 * of the memory or device accesses in the
1174 atomic_store_rel_int(&ithd->it_need, 0);
1175 ithread_execute_handlers(p, ie);
1177 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1178 mtx_assert(&Giant, MA_NOTOWNED);
1181 * Processed all our interrupts. Now get the sched
1182 * lock. This may take a while and it_need may get
1183 * set again, so we have to check it again.
1186 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1189 mi_switch(SW_VOL, NULL);
1194 * Ensure we are bound to the correct CPU. We can't
1195 * move ithreads until SMP is running however, so just
1196 * leave interrupts on the boor CPU during boot.
1198 if (ie->ie_cpu != cpu && smp_started) {
1203 sched_bind(td, cpu);
1211 * This is the main code for interrupt threads.
1214 ithread_loop(void *arg)
1216 struct intr_thread *ithd;
1217 struct intr_handler *ih;
1218 struct intr_event *ie;
1226 ih = (struct intr_handler *)arg;
1227 priv = (ih->ih_thread != NULL) ? 1 : 0;
1228 ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1229 KASSERT(ithd->it_thread == td,
1230 ("%s: ithread and proc linkage out of sync", __func__));
1231 ie = ithd->it_event;
1236 * As long as we have interrupts outstanding, go through the
1237 * list of handlers, giving each one a go at it.
1241 * If we are an orphaned thread, then just die.
1243 if (ithd->it_flags & IT_DEAD) {
1244 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1245 p->p_pid, p->p_comm);
1246 free(ithd, M_ITHREAD);
1251 * Service interrupts. If another interrupt arrives while
1252 * we are running, it will set it_need to note that we
1253 * should make another pass.
1255 while (ithd->it_need) {
1257 * This might need a full read and write barrier
1258 * to make sure that this write posts before any
1259 * of the memory or device accesses in the
1262 atomic_store_rel_int(&ithd->it_need, 0);
1264 priv_ithread_execute_handler(p, ih);
1266 ithread_execute_handlers(p, ie);
1268 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1269 mtx_assert(&Giant, MA_NOTOWNED);
1272 * Processed all our interrupts. Now get the sched
1273 * lock. This may take a while and it_need may get
1274 * set again, so we have to check it again.
1277 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1280 mi_switch(SW_VOL, NULL);
1285 * Ensure we are bound to the correct CPU. We can't
1286 * move ithreads until SMP is running however, so just
1287 * leave interrupts on the boor CPU during boot.
1289 if (!priv && ie->ie_cpu != cpu && smp_started) {
1294 sched_bind(td, cpu);
1302 * Main loop for interrupt filter.
1304 * Some architectures (i386, amd64 and arm) require the optional frame
1305 * parameter, and use it as the main argument for fast handler execution
1306 * when ih_argument == NULL.
1309 * o FILTER_STRAY: No filter recognized the event, and no
1310 * filter-less handler is registered on this
1312 * o FILTER_HANDLED: A filter claimed the event and served it.
1313 * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at
1314 * least one filter-less handler on this line.
1315 * o FILTER_HANDLED |
1316 * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for
1317 * scheduling the per-handler ithread.
1319 * In case an ithread has to be scheduled, in *ithd there will be a
1320 * pointer to a struct intr_thread containing the thread to be
1325 intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1326 struct intr_thread **ithd)
1328 struct intr_handler *ih;
1330 int ret, thread_only;
1334 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1336 * Execute fast interrupt handlers directly.
1337 * To support clock handlers, if a handler registers
1338 * with a NULL argument, then we pass it a pointer to
1339 * a trapframe as its argument.
1341 arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1343 CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1344 ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1346 if (ih->ih_filter != NULL)
1347 ret = ih->ih_filter(arg);
1353 if (ret & FILTER_STRAY)
1356 *ithd = ih->ih_thread;
1362 * No filters handled the interrupt and we have at least
1363 * one handler without a filter. In this case, we schedule
1364 * all of the filter-less handlers to run in the ithread.
1367 *ithd = ie->ie_thread;
1368 return (FILTER_SCHEDULE_THREAD);
1370 return (FILTER_STRAY);
1374 * Main interrupt handling body.
1377 * o ie: the event connected to this interrupt.
1378 * o frame: some archs (i.e. i386) pass a frame to some.
1379 * handlers as their main argument.
1381 * o 0: everything ok.
1382 * o EINVAL: stray interrupt.
1385 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1387 struct intr_thread *ithd;
1394 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1397 td->td_intr_nesting_level++;
1400 thread = intr_filter_loop(ie, frame, &ithd);
1403 * If the interrupt was fully served, send it an EOI but leave
1404 * it unmasked. Otherwise, mask the source as well as sending
1407 if (thread & FILTER_HANDLED) {
1408 if (ie->ie_eoi != NULL)
1409 ie->ie_eoi(ie->ie_source);
1411 if (ie->ie_disab != NULL)
1412 ie->ie_disab(ie->ie_source);
1416 /* Interrupt storm logic */
1417 if (thread & FILTER_STRAY) {
1419 if (ie->ie_count < intr_storm_threshold)
1420 printf("Interrupt stray detection not present\n");
1423 /* Schedule an ithread if needed. */
1424 if (thread & FILTER_SCHEDULE_THREAD) {
1425 if (intr_event_schedule_thread(ie, ithd) != 0)
1426 panic("%s: impossible stray interrupt", __func__);
1428 td->td_intr_nesting_level--;
1435 * Dump details about an interrupt handler
1438 db_dump_intrhand(struct intr_handler *ih)
1442 db_printf("\t%-10s ", ih->ih_name);
1443 switch (ih->ih_pri) {
1468 if (ih->ih_pri >= PI_SOFT)
1471 db_printf("%4u", ih->ih_pri);
1475 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1476 db_printf("(%p)", ih->ih_argument);
1478 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1482 if (ih->ih_flags & IH_EXCLUSIVE) {
1488 if (ih->ih_flags & IH_ENTROPY) {
1491 db_printf("ENTROPY");
1494 if (ih->ih_flags & IH_DEAD) {
1500 if (ih->ih_flags & IH_MPSAFE) {
1503 db_printf("MPSAFE");
1517 * Dump details about a event.
1520 db_dump_intr_event(struct intr_event *ie, int handlers)
1522 struct intr_handler *ih;
1523 struct intr_thread *it;
1526 db_printf("%s ", ie->ie_fullname);
1529 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1531 db_printf("(no thread)");
1532 if (ie->ie_cpu != NOCPU)
1533 db_printf(" (CPU %d)", ie->ie_cpu);
1534 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1535 (it != NULL && it->it_need)) {
1538 if (ie->ie_flags & IE_SOFT) {
1542 if (ie->ie_flags & IE_ENTROPY) {
1545 db_printf("ENTROPY");
1548 if (ie->ie_flags & IE_ADDING_THREAD) {
1551 db_printf("ADDING_THREAD");
1554 if (it != NULL && it->it_need) {
1564 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1565 db_dump_intrhand(ih);
1569 * Dump data about interrupt handlers
1571 DB_SHOW_COMMAND(intr, db_show_intr)
1573 struct intr_event *ie;
1576 verbose = index(modif, 'v') != NULL;
1577 all = index(modif, 'a') != NULL;
1578 TAILQ_FOREACH(ie, &event_list, ie_list) {
1579 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1581 db_dump_intr_event(ie, verbose);
1589 * Start standard software interrupt threads
1592 start_softintr(void *dummy)
1596 if (swi_add(&clk_intr_event, "clock", softclock, NULL, SWI_CLOCK,
1597 INTR_MPSAFE, &softclock_ih) ||
1598 swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1599 panic("died while creating standard software ithreads");
1601 p = clk_intr_event->ie_thread->it_thread->td_proc;
1603 p->p_flag |= P_NOLOAD;
1606 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1610 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1611 * The data for this machine dependent, and the declarations are in machine
1612 * dependent code. The layout of intrnames and intrcnt however is machine
1615 * We do not know the length of intrcnt and intrnames at compile time, so
1616 * calculate things at run time.
1619 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1621 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1625 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1626 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1629 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1631 return (sysctl_handle_opaque(oidp, intrcnt,
1632 (char *)eintrcnt - (char *)intrcnt, req));
1635 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1636 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1640 * DDB command to dump the interrupt statistics.
1642 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1648 for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
1652 db_printf("%s\t%lu\n", cp, *i);
1653 cp += strlen(cp) + 1;