2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_kstack_usage_prof.h"
35 #include <sys/param.h>
38 #include <sys/cpuset.h>
39 #include <sys/rtprio.h>
40 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/epoch.h>
52 #include <sys/random.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 #include <sys/unistd.h>
59 #include <sys/vmmeter.h>
60 #include <machine/atomic.h>
61 #include <machine/cpu.h>
62 #include <machine/md_var.h>
63 #include <machine/smp.h>
64 #include <machine/stdarg.h>
67 #include <ddb/db_sym.h>
71 * Describe an interrupt thread. There is one of these per interrupt event.
74 struct intr_event *it_event;
75 struct thread *it_thread; /* Kernel thread. */
76 int it_flags; /* (j) IT_* flags. */
77 int it_need; /* Needs service. */
80 /* Interrupt thread flags kept in it_flags */
81 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
82 #define IT_WAIT 0x000002 /* Thread is waiting for completion. */
89 struct intr_event *clk_intr_event;
90 struct intr_event *tty_intr_event;
92 struct proc *intrproc;
94 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
96 static int intr_storm_threshold = 0;
97 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
98 &intr_storm_threshold, 0,
99 "Number of consecutive interrupts before storm protection is enabled");
100 static int intr_epoch_batch = 1000;
101 SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch,
102 0, "Maximum interrupt handler executions without re-entering epoch(9)");
103 static TAILQ_HEAD(, intr_event) event_list =
104 TAILQ_HEAD_INITIALIZER(event_list);
105 static struct mtx event_lock;
106 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
108 static void intr_event_update(struct intr_event *ie);
109 static int intr_event_schedule_thread(struct intr_event *ie);
110 static struct intr_thread *ithread_create(const char *name);
111 static void ithread_destroy(struct intr_thread *ithread);
112 static void ithread_execute_handlers(struct proc *p,
113 struct intr_event *ie);
114 static void ithread_loop(void *);
115 static void ithread_update(struct intr_thread *ithd);
116 static void start_softintr(void *);
118 /* Map an interrupt type to an ithread priority. */
120 intr_priority(enum intr_type flags)
124 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
125 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
146 pri = PI_DULL; /* don't care */
149 /* We didn't specify an interrupt level. */
150 panic("intr_priority: no interrupt type in flags");
157 * Update an ithread based on the associated intr_event.
160 ithread_update(struct intr_thread *ithd)
162 struct intr_event *ie;
167 td = ithd->it_thread;
168 mtx_assert(&ie->ie_lock, MA_OWNED);
170 /* Determine the overall priority of this event. */
171 if (CK_SLIST_EMPTY(&ie->ie_handlers))
174 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
176 /* Update name and priority. */
177 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
179 sched_clear_tdname(td);
187 * Regenerate the full name of an interrupt event and update its priority.
190 intr_event_update(struct intr_event *ie)
192 struct intr_handler *ih;
194 int missed, space, flags;
196 /* Start off with no entropy and just the name of the event. */
197 mtx_assert(&ie->ie_lock, MA_OWNED);
198 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
203 /* Run through all the handlers updating values. */
204 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
205 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
206 sizeof(ie->ie_fullname)) {
207 strcat(ie->ie_fullname, " ");
208 strcat(ie->ie_fullname, ih->ih_name);
212 flags |= ih->ih_flags;
214 ie->ie_hflags = flags;
217 * If there is only one handler and its name is too long, just copy in
218 * as much of the end of the name (includes the unit number) as will
219 * fit. Otherwise, we have multiple handlers and not all of the names
220 * will fit. Add +'s to indicate missing names. If we run out of room
221 * and still have +'s to add, change the last character from a + to a *.
223 if (missed == 1 && space == 1) {
224 ih = CK_SLIST_FIRST(&ie->ie_handlers);
225 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
226 sizeof(ie->ie_fullname);
227 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
228 strcat(ie->ie_fullname, &ih->ih_name[missed]);
231 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
232 while (missed-- > 0) {
233 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
240 strcat(ie->ie_fullname, " +");
243 strcat(ie->ie_fullname, "+");
247 * If this event has an ithread, update it's priority and
250 if (ie->ie_thread != NULL)
251 ithread_update(ie->ie_thread);
252 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
256 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
257 void (*pre_ithread)(void *), void (*post_ithread)(void *),
258 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
259 const char *fmt, ...)
261 struct intr_event *ie;
264 /* The only valid flag during creation is IE_SOFT. */
265 if ((flags & ~IE_SOFT) != 0)
267 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
268 ie->ie_source = source;
269 ie->ie_pre_ithread = pre_ithread;
270 ie->ie_post_ithread = post_ithread;
271 ie->ie_post_filter = post_filter;
272 ie->ie_assign_cpu = assign_cpu;
273 ie->ie_flags = flags;
276 CK_SLIST_INIT(&ie->ie_handlers);
277 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
280 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
282 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
283 mtx_lock(&event_lock);
284 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
285 mtx_unlock(&event_lock);
288 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
293 * Bind an interrupt event to the specified CPU. Note that not all
294 * platforms support binding an interrupt to a CPU. For those
295 * platforms this request will fail. Using a cpu id of NOCPU unbinds
296 * the interrupt event.
299 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
304 /* Need a CPU to bind to. */
305 if (cpu != NOCPU && CPU_ABSENT(cpu))
308 if (ie->ie_assign_cpu == NULL)
311 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
316 * If we have any ithreads try to set their mask first to verify
320 mtx_lock(&ie->ie_lock);
321 if (ie->ie_thread != NULL) {
322 id = ie->ie_thread->it_thread->td_tid;
323 mtx_unlock(&ie->ie_lock);
324 error = cpuset_setithread(id, cpu);
328 mtx_unlock(&ie->ie_lock);
331 error = ie->ie_assign_cpu(ie->ie_source, cpu);
334 mtx_lock(&ie->ie_lock);
335 if (ie->ie_thread != NULL) {
337 id = ie->ie_thread->it_thread->td_tid;
338 mtx_unlock(&ie->ie_lock);
339 (void)cpuset_setithread(id, cpu);
341 mtx_unlock(&ie->ie_lock);
347 mtx_lock(&ie->ie_lock);
349 mtx_unlock(&ie->ie_lock);
356 * Bind an interrupt event to the specified CPU. For supported platforms, any
357 * associated ithreads as well as the primary interrupt context will be bound
358 * to the specificed CPU.
361 intr_event_bind(struct intr_event *ie, int cpu)
364 return (_intr_event_bind(ie, cpu, true, true));
368 * Bind an interrupt event to the specified CPU, but do not bind associated
372 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
375 return (_intr_event_bind(ie, cpu, true, false));
379 * Bind an interrupt event's ithread to the specified CPU.
382 intr_event_bind_ithread(struct intr_event *ie, int cpu)
385 return (_intr_event_bind(ie, cpu, false, true));
389 * Bind an interrupt event's ithread to the specified cpuset.
392 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
396 mtx_lock(&ie->ie_lock);
397 if (ie->ie_thread != NULL) {
398 id = ie->ie_thread->it_thread->td_tid;
399 mtx_unlock(&ie->ie_lock);
400 return (cpuset_setthread(id, cs));
402 mtx_unlock(&ie->ie_lock);
407 static struct intr_event *
410 struct intr_event *ie;
412 mtx_lock(&event_lock);
413 TAILQ_FOREACH(ie, &event_list, ie_list)
414 if (ie->ie_irq == irq &&
415 (ie->ie_flags & IE_SOFT) == 0 &&
416 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
418 mtx_unlock(&event_lock);
423 intr_setaffinity(int irq, int mode, void *m)
425 struct intr_event *ie;
432 * If we're setting all cpus we can unbind. Otherwise make sure
433 * only one cpu is in the set.
435 if (CPU_CMP(cpuset_root, mask)) {
436 for (n = 0; n < CPU_SETSIZE; n++) {
437 if (!CPU_ISSET(n, mask))
444 ie = intr_lookup(irq);
449 return (intr_event_bind(ie, cpu));
450 case CPU_WHICH_INTRHANDLER:
451 return (intr_event_bind_irqonly(ie, cpu));
452 case CPU_WHICH_ITHREAD:
453 return (intr_event_bind_ithread(ie, cpu));
460 intr_getaffinity(int irq, int mode, void *m)
462 struct intr_event *ie;
470 ie = intr_lookup(irq);
478 case CPU_WHICH_INTRHANDLER:
479 mtx_lock(&ie->ie_lock);
480 if (ie->ie_cpu == NOCPU)
481 CPU_COPY(cpuset_root, mask);
483 CPU_SET(ie->ie_cpu, mask);
484 mtx_unlock(&ie->ie_lock);
486 case CPU_WHICH_ITHREAD:
487 mtx_lock(&ie->ie_lock);
488 if (ie->ie_thread == NULL) {
489 mtx_unlock(&ie->ie_lock);
490 CPU_COPY(cpuset_root, mask);
492 id = ie->ie_thread->it_thread->td_tid;
493 mtx_unlock(&ie->ie_lock);
494 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
497 CPU_COPY(&td->td_cpuset->cs_mask, mask);
507 intr_event_destroy(struct intr_event *ie)
510 mtx_lock(&event_lock);
511 mtx_lock(&ie->ie_lock);
512 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
513 mtx_unlock(&ie->ie_lock);
514 mtx_unlock(&event_lock);
517 TAILQ_REMOVE(&event_list, ie, ie_list);
519 if (ie->ie_thread != NULL) {
520 ithread_destroy(ie->ie_thread);
521 ie->ie_thread = NULL;
524 mtx_unlock(&ie->ie_lock);
525 mtx_unlock(&event_lock);
526 mtx_destroy(&ie->ie_lock);
531 static struct intr_thread *
532 ithread_create(const char *name)
534 struct intr_thread *ithd;
538 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
540 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
541 &td, RFSTOPPED | RFHIGHPID,
542 0, "intr", "%s", name);
544 panic("kproc_create() failed with %d", error);
546 sched_class(td, PRI_ITHD);
549 td->td_pflags |= TDP_ITHREAD;
550 ithd->it_thread = td;
551 CTR2(KTR_INTR, "%s: created %s", __func__, name);
556 ithread_destroy(struct intr_thread *ithread)
560 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
561 td = ithread->it_thread;
563 ithread->it_flags |= IT_DEAD;
564 if (TD_AWAITING_INTR(td)) {
566 sched_add(td, SRQ_INTR);
572 intr_event_add_handler(struct intr_event *ie, const char *name,
573 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
574 enum intr_type flags, void **cookiep)
576 struct intr_handler *ih, *temp_ih;
577 struct intr_handler **prevptr;
578 struct intr_thread *it;
580 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
583 /* Allocate and populate an interrupt handler structure. */
584 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
585 ih->ih_filter = filter;
586 ih->ih_handler = handler;
587 ih->ih_argument = arg;
588 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
591 if (flags & INTR_EXCL)
592 ih->ih_flags = IH_EXCLUSIVE;
593 if (flags & INTR_MPSAFE)
594 ih->ih_flags |= IH_MPSAFE;
595 if (flags & INTR_ENTROPY)
596 ih->ih_flags |= IH_ENTROPY;
597 if (flags & INTR_TYPE_NET)
598 ih->ih_flags |= IH_NET;
600 /* We can only have one exclusive handler in a event. */
601 mtx_lock(&ie->ie_lock);
602 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
603 if ((flags & INTR_EXCL) ||
604 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
605 mtx_unlock(&ie->ie_lock);
611 /* Create a thread if we need one. */
612 while (ie->ie_thread == NULL && handler != NULL) {
613 if (ie->ie_flags & IE_ADDING_THREAD)
614 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
616 ie->ie_flags |= IE_ADDING_THREAD;
617 mtx_unlock(&ie->ie_lock);
618 it = ithread_create("intr: newborn");
619 mtx_lock(&ie->ie_lock);
620 ie->ie_flags &= ~IE_ADDING_THREAD;
628 /* Add the new handler to the event in priority order. */
629 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
630 if (temp_ih->ih_pri > ih->ih_pri)
633 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
635 intr_event_update(ie);
637 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
639 mtx_unlock(&ie->ie_lock);
647 * Append a description preceded by a ':' to the name of the specified
651 intr_event_describe_handler(struct intr_event *ie, void *cookie,
654 struct intr_handler *ih;
658 mtx_lock(&ie->ie_lock);
660 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
665 mtx_unlock(&ie->ie_lock);
666 panic("handler %p not found in interrupt event %p", cookie, ie);
672 * Look for an existing description by checking for an
673 * existing ":". This assumes device names do not include
674 * colons. If one is found, prepare to insert the new
675 * description at that point. If one is not found, find the
676 * end of the name to use as the insertion point.
678 start = strchr(ih->ih_name, ':');
680 start = strchr(ih->ih_name, 0);
683 * See if there is enough remaining room in the string for the
684 * description + ":". The "- 1" leaves room for the trailing
685 * '\0'. The "+ 1" accounts for the colon.
687 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
688 if (strlen(descr) + 1 > space) {
689 mtx_unlock(&ie->ie_lock);
693 /* Append a colon followed by the description. */
695 strcpy(start + 1, descr);
696 intr_event_update(ie);
697 mtx_unlock(&ie->ie_lock);
702 * Return the ie_source field from the intr_event an intr_handler is
706 intr_handler_source(void *cookie)
708 struct intr_handler *ih;
709 struct intr_event *ie;
711 ih = (struct intr_handler *)cookie;
716 ("interrupt handler \"%s\" has a NULL interrupt event",
718 return (ie->ie_source);
722 * If intr_event_handle() is running in the ISR context at the time of the call,
723 * then wait for it to complete.
726 intr_event_barrier(struct intr_event *ie)
730 mtx_assert(&ie->ie_lock, MA_OWNED);
731 phase = ie->ie_phase;
734 * Switch phase to direct future interrupts to the other active counter.
735 * Make sure that any preceding stores are visible before the switch.
737 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
738 atomic_store_rel_int(&ie->ie_phase, !phase);
741 * This code cooperates with wait-free iteration of ie_handlers
742 * in intr_event_handle.
743 * Make sure that the removal and the phase update are not reordered
744 * with the active count check.
745 * Note that no combination of acquire and release fences can provide
746 * that guarantee as Store->Load sequences can always be reordered.
748 atomic_thread_fence_seq_cst();
751 * Now wait on the inactive phase.
752 * The acquire fence is needed so that that all post-barrier accesses
753 * are after the check.
755 while (ie->ie_active[phase] > 0)
757 atomic_thread_fence_acq();
761 intr_handler_barrier(struct intr_handler *handler)
763 struct intr_event *ie;
765 ie = handler->ih_event;
766 mtx_assert(&ie->ie_lock, MA_OWNED);
767 KASSERT((handler->ih_flags & IH_DEAD) == 0,
768 ("update for a removed handler"));
770 if (ie->ie_thread == NULL) {
771 intr_event_barrier(ie);
774 if ((handler->ih_flags & IH_CHANGED) == 0) {
775 handler->ih_flags |= IH_CHANGED;
776 intr_event_schedule_thread(ie);
778 while ((handler->ih_flags & IH_CHANGED) != 0)
779 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
783 * Sleep until an ithread finishes executing an interrupt handler.
785 * XXX Doesn't currently handle interrupt filters or fast interrupt
786 * handlers. This is intended for compatibility with linux drivers
787 * only. Do not use in BSD code.
792 struct intr_event *ie;
793 struct intr_thread *ithd;
796 ie = intr_lookup(irq);
799 if (ie->ie_thread == NULL)
801 ithd = ie->ie_thread;
802 td = ithd->it_thread;
804 * We set the flag and wait for it to be cleared to avoid
805 * long delays with potentially busy interrupt handlers
806 * were we to only sample TD_AWAITING_INTR() every tick.
809 if (!TD_AWAITING_INTR(td)) {
810 ithd->it_flags |= IT_WAIT;
811 while (ithd->it_flags & IT_WAIT) {
822 intr_event_remove_handler(void *cookie)
824 struct intr_handler *handler = (struct intr_handler *)cookie;
825 struct intr_event *ie;
826 struct intr_handler *ih;
827 struct intr_handler **prevptr;
834 ie = handler->ih_event;
836 ("interrupt handler \"%s\" has a NULL interrupt event",
839 mtx_lock(&ie->ie_lock);
840 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
842 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
847 panic("interrupt handler \"%s\" not found in "
848 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
852 * If there is no ithread, then directly remove the handler. Note that
853 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
854 * care needs to be taken to keep ie_handlers consistent and to free
855 * the removed handler only when ie_handlers is quiescent.
857 if (ie->ie_thread == NULL) {
858 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
859 intr_event_barrier(ie);
860 intr_event_update(ie);
861 mtx_unlock(&ie->ie_lock);
862 free(handler, M_ITHREAD);
867 * Let the interrupt thread do the job.
868 * The interrupt source is disabled when the interrupt thread is
869 * running, so it does not have to worry about interaction with
870 * intr_event_handle().
872 KASSERT((handler->ih_flags & IH_DEAD) == 0,
873 ("duplicate handle remove"));
874 handler->ih_flags |= IH_DEAD;
875 intr_event_schedule_thread(ie);
876 while (handler->ih_flags & IH_DEAD)
877 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
878 intr_event_update(ie);
882 * XXX: This could be bad in the case of ppbus(8). Also, I think
883 * this could lead to races of stale data when servicing an
887 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
888 if (ih->ih_handler != NULL) {
894 ithread_destroy(ie->ie_thread);
895 ie->ie_thread = NULL;
898 mtx_unlock(&ie->ie_lock);
899 free(handler, M_ITHREAD);
904 intr_event_suspend_handler(void *cookie)
906 struct intr_handler *handler = (struct intr_handler *)cookie;
907 struct intr_event *ie;
911 ie = handler->ih_event;
913 ("interrupt handler \"%s\" has a NULL interrupt event",
915 mtx_lock(&ie->ie_lock);
916 handler->ih_flags |= IH_SUSP;
917 intr_handler_barrier(handler);
918 mtx_unlock(&ie->ie_lock);
923 intr_event_resume_handler(void *cookie)
925 struct intr_handler *handler = (struct intr_handler *)cookie;
926 struct intr_event *ie;
930 ie = handler->ih_event;
932 ("interrupt handler \"%s\" has a NULL interrupt event",
936 * intr_handler_barrier() acts not only as a barrier,
937 * it also allows to check for any pending interrupts.
939 mtx_lock(&ie->ie_lock);
940 handler->ih_flags &= ~IH_SUSP;
941 intr_handler_barrier(handler);
942 mtx_unlock(&ie->ie_lock);
947 intr_event_schedule_thread(struct intr_event *ie)
949 struct intr_entropy entropy;
950 struct intr_thread *it;
955 * If no ithread or no handlers, then we have a stray interrupt.
957 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
958 ie->ie_thread == NULL)
966 * If any of the handlers for this ithread claim to be good
967 * sources of entropy, then gather some.
969 if (ie->ie_hflags & IH_ENTROPY) {
970 entropy.event = (uintptr_t)ie;
972 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
975 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
978 * Set it_need to tell the thread to keep running if it is already
979 * running. Then, lock the thread and see if we actually need to
980 * put it on the runqueue.
982 * Use store_rel to arrange that the store to ih_need in
983 * swi_sched() is before the store to it_need and prepare for
984 * transfer of this order to loads in the ithread.
986 atomic_store_rel_int(&it->it_need, 1);
988 if (TD_AWAITING_INTR(td)) {
989 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
992 sched_add(td, SRQ_INTR);
994 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
995 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
1003 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1004 * since interrupts are generated in software rather than being directed by
1008 swi_assign_cpu(void *arg, int cpu)
1015 * Add a software interrupt handler to a specified event. If a given event
1016 * is not specified, then a new event is created.
1019 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1020 void *arg, int pri, enum intr_type flags, void **cookiep)
1022 struct intr_event *ie;
1025 if (flags & INTR_ENTROPY)
1028 ie = (eventp != NULL) ? *eventp : NULL;
1031 if (!(ie->ie_flags & IE_SOFT))
1034 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1035 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1041 if (handler != NULL) {
1042 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1043 PI_SWI(pri), flags, cookiep);
1049 * Schedule a software interrupt thread.
1052 swi_sched(void *cookie, int flags)
1054 struct intr_handler *ih = (struct intr_handler *)cookie;
1055 struct intr_event *ie = ih->ih_event;
1056 struct intr_entropy entropy;
1059 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1062 if ((flags & SWI_FROMNMI) == 0) {
1063 entropy.event = (uintptr_t)ih;
1064 entropy.td = curthread;
1065 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1069 * Set ih_need for this handler so that if the ithread is already
1070 * running it will execute this handler on the next pass. Otherwise,
1071 * it will execute it the next time it runs.
1075 if (flags & SWI_DELAY)
1078 if (flags & SWI_FROMNMI) {
1079 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1080 KASSERT(ie == clk_intr_event,
1081 ("SWI_FROMNMI used not with clk_intr_event"));
1082 ipi_self_from_nmi(IPI_SWI);
1086 error = intr_event_schedule_thread(ie);
1087 KASSERT(error == 0, ("stray software interrupt"));
1092 * Remove a software interrupt handler. Currently this code does not
1093 * remove the associated interrupt event if it becomes empty. Calling code
1094 * may do so manually via intr_event_destroy(), but that's not really
1095 * an optimal interface.
1098 swi_remove(void *cookie)
1101 return (intr_event_remove_handler(cookie));
1105 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1107 struct intr_handler *ih, *ihn, *ihp;
1110 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1112 * If this handler is marked for death, remove it from
1113 * the list of handlers and wake up the sleeper.
1115 if (ih->ih_flags & IH_DEAD) {
1116 mtx_lock(&ie->ie_lock);
1118 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1120 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1121 ih->ih_flags &= ~IH_DEAD;
1123 mtx_unlock(&ie->ie_lock);
1128 * Now that we know that the current element won't be removed
1129 * update the previous element.
1133 if ((ih->ih_flags & IH_CHANGED) != 0) {
1134 mtx_lock(&ie->ie_lock);
1135 ih->ih_flags &= ~IH_CHANGED;
1137 mtx_unlock(&ie->ie_lock);
1140 /* Skip filter only handlers */
1141 if (ih->ih_handler == NULL)
1144 /* Skip suspended handlers */
1145 if ((ih->ih_flags & IH_SUSP) != 0)
1149 * For software interrupt threads, we only execute
1150 * handlers that have their need flag set. Hardware
1151 * interrupt threads always invoke all of their handlers.
1153 * ih_need can only be 0 or 1. Failed cmpset below
1154 * means that there is no request to execute handlers,
1155 * so a retry of the cmpset is not needed.
1157 if ((ie->ie_flags & IE_SOFT) != 0 &&
1158 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1161 /* Execute this handler. */
1162 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1163 __func__, p->p_pid, (void *)ih->ih_handler,
1164 ih->ih_argument, ih->ih_name, ih->ih_flags);
1166 if (!(ih->ih_flags & IH_MPSAFE))
1168 ih->ih_handler(ih->ih_argument);
1169 if (!(ih->ih_flags & IH_MPSAFE))
1175 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1178 /* Interrupt handlers should not sleep. */
1179 if (!(ie->ie_flags & IE_SOFT))
1180 THREAD_NO_SLEEPING();
1181 intr_event_execute_handlers(p, ie);
1182 if (!(ie->ie_flags & IE_SOFT))
1183 THREAD_SLEEPING_OK();
1186 * Interrupt storm handling:
1188 * If this interrupt source is currently storming, then throttle
1189 * it to only fire the handler once per clock tick.
1191 * If this interrupt source is not currently storming, but the
1192 * number of back to back interrupts exceeds the storm threshold,
1193 * then enter storming mode.
1195 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1196 !(ie->ie_flags & IE_SOFT)) {
1197 /* Report the message only once every second. */
1198 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1200 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1208 * Now that all the handlers have had a chance to run, reenable
1209 * the interrupt source.
1211 if (ie->ie_post_ithread != NULL)
1212 ie->ie_post_ithread(ie->ie_source);
1216 * This is the main code for interrupt threads.
1219 ithread_loop(void *arg)
1221 struct epoch_tracker et;
1222 struct intr_thread *ithd;
1223 struct intr_event *ie;
1226 int wake, epoch_count;
1231 ithd = (struct intr_thread *)arg;
1232 KASSERT(ithd->it_thread == td,
1233 ("%s: ithread and proc linkage out of sync", __func__));
1234 ie = ithd->it_event;
1239 * As long as we have interrupts outstanding, go through the
1240 * list of handlers, giving each one a go at it.
1244 * If we are an orphaned thread, then just die.
1246 if (ithd->it_flags & IT_DEAD) {
1247 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1248 p->p_pid, td->td_name);
1249 free(ithd, M_ITHREAD);
1254 * Service interrupts. If another interrupt arrives while
1255 * we are running, it will set it_need to note that we
1256 * should make another pass.
1258 * The load_acq part of the following cmpset ensures
1259 * that the load of ih_need in ithread_execute_handlers()
1260 * is ordered after the load of it_need here.
1263 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1266 NET_EPOCH_ENTER(et);
1268 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1269 ithread_execute_handlers(p, ie);
1271 ++epoch_count >= intr_epoch_batch) {
1274 NET_EPOCH_ENTER(et);
1279 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1280 mtx_assert(&Giant, MA_NOTOWNED);
1283 * Processed all our interrupts. Now get the sched
1284 * lock. This may take a while and it_need may get
1285 * set again, so we have to check it again.
1288 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1289 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1292 mi_switch(SW_VOL | SWT_IWAIT);
1294 if (ithd->it_flags & IT_WAIT) {
1296 ithd->it_flags &= ~IT_WAIT;
1308 * Main interrupt handling body.
1311 * o ie: the event connected to this interrupt.
1312 * o frame: some archs (i.e. i386) pass a frame to some.
1313 * handlers as their main argument.
1315 * o 0: everything ok.
1316 * o EINVAL: stray interrupt.
1319 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1321 struct intr_handler *ih;
1322 struct trapframe *oldframe;
1326 bool filter, thread;
1330 #ifdef KSTACK_USAGE_PROF
1331 intr_prof_stack_use(td, frame);
1334 /* An interrupt with no event or handlers is a stray interrupt. */
1335 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1339 * Execute fast interrupt handlers directly.
1340 * To support clock handlers, if a handler registers
1341 * with a NULL argument, then we pass it a pointer to
1342 * a trapframe as its argument.
1344 td->td_intr_nesting_level++;
1349 oldframe = td->td_intr_frame;
1350 td->td_intr_frame = frame;
1352 phase = ie->ie_phase;
1353 atomic_add_int(&ie->ie_active[phase], 1);
1356 * This fence is required to ensure that no later loads are
1357 * re-ordered before the ie_active store.
1359 atomic_thread_fence_seq_cst();
1361 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1362 if ((ih->ih_flags & IH_SUSP) != 0)
1364 if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1366 if (ih->ih_filter == NULL) {
1370 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1371 ih->ih_filter, ih->ih_argument == NULL ? frame :
1372 ih->ih_argument, ih->ih_name);
1373 if (ih->ih_argument == NULL)
1374 ret = ih->ih_filter(frame);
1376 ret = ih->ih_filter(ih->ih_argument);
1377 KASSERT(ret == FILTER_STRAY ||
1378 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1379 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1380 ("%s: incorrect return value %#x from %s", __func__, ret,
1382 filter = filter || ret == FILTER_HANDLED;
1385 * Wrapper handler special handling:
1387 * in some particular cases (like pccard and pccbb),
1388 * the _real_ device handler is wrapped in a couple of
1389 * functions - a filter wrapper and an ithread wrapper.
1390 * In this case (and just in this case), the filter wrapper
1391 * could ask the system to schedule the ithread and mask
1392 * the interrupt source if the wrapped handler is composed
1393 * of just an ithread handler.
1395 * TODO: write a generic wrapper to avoid people rolling
1399 if (ret == FILTER_SCHEDULE_THREAD)
1403 atomic_add_rel_int(&ie->ie_active[phase], -1);
1405 td->td_intr_frame = oldframe;
1408 if (ie->ie_pre_ithread != NULL)
1409 ie->ie_pre_ithread(ie->ie_source);
1411 if (ie->ie_post_filter != NULL)
1412 ie->ie_post_filter(ie->ie_source);
1415 /* Schedule the ithread if needed. */
1419 error = intr_event_schedule_thread(ie);
1420 KASSERT(error == 0, ("bad stray interrupt"));
1423 td->td_intr_nesting_level--;
1425 /* The interrupt is not aknowledged by any filter and has no ithread. */
1426 if (!thread && !filter)
1434 * Dump details about an interrupt handler
1437 db_dump_intrhand(struct intr_handler *ih)
1441 db_printf("\t%-10s ", ih->ih_name);
1442 switch (ih->ih_pri) {
1462 if (ih->ih_pri >= PI_SOFT)
1465 db_printf("%4u", ih->ih_pri);
1469 if (ih->ih_filter != NULL) {
1471 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1473 if (ih->ih_handler != NULL) {
1474 if (ih->ih_filter != NULL)
1477 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1479 db_printf("(%p)", ih->ih_argument);
1481 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1485 if (ih->ih_flags & IH_EXCLUSIVE) {
1491 if (ih->ih_flags & IH_ENTROPY) {
1494 db_printf("ENTROPY");
1497 if (ih->ih_flags & IH_DEAD) {
1503 if (ih->ih_flags & IH_MPSAFE) {
1506 db_printf("MPSAFE");
1520 * Dump details about a event.
1523 db_dump_intr_event(struct intr_event *ie, int handlers)
1525 struct intr_handler *ih;
1526 struct intr_thread *it;
1529 db_printf("%s ", ie->ie_fullname);
1532 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1534 db_printf("(no thread)");
1535 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1536 (it != NULL && it->it_need)) {
1539 if (ie->ie_flags & IE_SOFT) {
1543 if (ie->ie_flags & IE_ADDING_THREAD) {
1546 db_printf("ADDING_THREAD");
1549 if (it != NULL && it->it_need) {
1559 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1560 db_dump_intrhand(ih);
1564 * Dump data about interrupt handlers
1566 DB_SHOW_COMMAND(intr, db_show_intr)
1568 struct intr_event *ie;
1571 verbose = strchr(modif, 'v') != NULL;
1572 all = strchr(modif, 'a') != NULL;
1573 TAILQ_FOREACH(ie, &event_list, ie_list) {
1574 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1576 db_dump_intr_event(ie, verbose);
1584 * Start standard software interrupt threads
1587 start_softintr(void *dummy)
1590 if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1592 panic("died while creating clk swi ithread");
1593 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1594 panic("died while creating vm swi ithread");
1596 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1600 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1601 * The data for this machine dependent, and the declarations are in machine
1602 * dependent code. The layout of intrnames and intrcnt however is machine
1605 * We do not know the length of intrcnt and intrnames at compile time, so
1606 * calculate things at run time.
1609 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1611 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1614 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1615 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
1616 sysctl_intrnames, "",
1620 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1623 uint32_t *intrcnt32;
1627 if (req->flags & SCTL_MASK32) {
1629 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1630 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1631 if (intrcnt32 == NULL)
1633 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1634 intrcnt32[i] = intrcnt[i];
1635 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1636 free(intrcnt32, M_TEMP);
1640 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1643 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1644 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
1646 "Interrupt Counts");
1650 * DDB command to dump the interrupt statistics.
1652 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1660 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1665 db_printf("%s\t%lu\n", cp, *i);
1666 cp += strlen(cp) + 1;