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_hwpmc_hooks.h"
34 #include "opt_kstack_usage_prof.h"
36 #include <sys/param.h>
39 #include <sys/cpuset.h>
40 #include <sys/rtprio.h>
41 #include <sys/systm.h>
42 #include <sys/interrupt.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
46 #include <sys/limits.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/epoch.h>
53 #include <sys/random.h>
54 #include <sys/resourcevar.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/unistd.h>
60 #include <sys/vmmeter.h>
61 #include <machine/atomic.h>
62 #include <machine/cpu.h>
63 #include <machine/md_var.h>
64 #include <machine/smp.h>
65 #include <machine/stdarg.h>
68 #include <ddb/db_sym.h>
72 * Describe an interrupt thread. There is one of these per interrupt event.
75 struct intr_event *it_event;
76 struct thread *it_thread; /* Kernel thread. */
77 int it_flags; /* (j) IT_* flags. */
78 int it_need; /* Needs service. */
79 int it_waiting; /* Waiting in the runq. */
82 /* Interrupt thread flags kept in it_flags */
83 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
84 #define IT_WAIT 0x000002 /* Thread is waiting for completion. */
91 struct intr_event *clk_intr_event;
92 struct intr_event *tty_intr_event;
94 struct proc *intrproc;
96 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
98 static int intr_storm_threshold = 0;
99 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
100 &intr_storm_threshold, 0,
101 "Number of consecutive interrupts before storm protection is enabled");
102 static int intr_epoch_batch = 1000;
103 SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch,
104 0, "Maximum interrupt handler executions without re-entering epoch(9)");
106 static int intr_hwpmc_waiting_report_threshold = 1;
107 SYSCTL_INT(_hw, OID_AUTO, intr_hwpmc_waiting_report_threshold, CTLFLAG_RWTUN,
108 &intr_hwpmc_waiting_report_threshold, 1,
109 "Threshold for reporting number of events in a workq");
111 static TAILQ_HEAD(, intr_event) event_list =
112 TAILQ_HEAD_INITIALIZER(event_list);
113 static struct mtx event_lock;
114 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
116 static void intr_event_update(struct intr_event *ie);
117 static int intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame);
118 static struct intr_thread *ithread_create(const char *name);
119 static void ithread_destroy(struct intr_thread *ithread);
120 static void ithread_execute_handlers(struct proc *p,
121 struct intr_event *ie);
122 static void ithread_loop(void *);
123 static void ithread_update(struct intr_thread *ithd);
124 static void start_softintr(void *);
127 #include <sys/pmckern.h>
128 PMC_SOFT_DEFINE( , , intr, all);
129 PMC_SOFT_DEFINE( , , intr, ithread);
130 PMC_SOFT_DEFINE( , , intr, filter);
131 PMC_SOFT_DEFINE( , , intr, stray);
132 PMC_SOFT_DEFINE( , , intr, schedule);
133 PMC_SOFT_DEFINE( , , intr, waiting);
136 /* Map an interrupt type to an ithread priority. */
138 intr_priority(enum intr_type flags)
142 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
143 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
164 pri = PI_DULL; /* don't care */
167 /* We didn't specify an interrupt level. */
168 panic("intr_priority: no interrupt type in flags");
175 * Update an ithread based on the associated intr_event.
178 ithread_update(struct intr_thread *ithd)
180 struct intr_event *ie;
185 td = ithd->it_thread;
186 mtx_assert(&ie->ie_lock, MA_OWNED);
188 /* Determine the overall priority of this event. */
189 if (CK_SLIST_EMPTY(&ie->ie_handlers))
192 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
194 /* Update name and priority. */
195 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
197 sched_clear_tdname(td);
205 * Regenerate the full name of an interrupt event and update its priority.
208 intr_event_update(struct intr_event *ie)
210 struct intr_handler *ih;
212 int missed, space, flags;
214 /* Start off with no entropy and just the name of the event. */
215 mtx_assert(&ie->ie_lock, MA_OWNED);
216 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
221 /* Run through all the handlers updating values. */
222 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
223 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
224 sizeof(ie->ie_fullname)) {
225 strcat(ie->ie_fullname, " ");
226 strcat(ie->ie_fullname, ih->ih_name);
230 flags |= ih->ih_flags;
232 ie->ie_hflags = flags;
235 * If there is only one handler and its name is too long, just copy in
236 * as much of the end of the name (includes the unit number) as will
237 * fit. Otherwise, we have multiple handlers and not all of the names
238 * will fit. Add +'s to indicate missing names. If we run out of room
239 * and still have +'s to add, change the last character from a + to a *.
241 if (missed == 1 && space == 1) {
242 ih = CK_SLIST_FIRST(&ie->ie_handlers);
243 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
244 sizeof(ie->ie_fullname);
245 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
246 strcat(ie->ie_fullname, &ih->ih_name[missed]);
249 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
250 while (missed-- > 0) {
251 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
258 strcat(ie->ie_fullname, " +");
261 strcat(ie->ie_fullname, "+");
265 * If this event has an ithread, update it's priority and
268 if (ie->ie_thread != NULL)
269 ithread_update(ie->ie_thread);
270 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
274 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
275 void (*pre_ithread)(void *), void (*post_ithread)(void *),
276 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
277 const char *fmt, ...)
279 struct intr_event *ie;
282 /* The only valid flag during creation is IE_SOFT. */
283 if ((flags & ~IE_SOFT) != 0)
285 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
286 ie->ie_source = source;
287 ie->ie_pre_ithread = pre_ithread;
288 ie->ie_post_ithread = post_ithread;
289 ie->ie_post_filter = post_filter;
290 ie->ie_assign_cpu = assign_cpu;
291 ie->ie_flags = flags;
294 CK_SLIST_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_lock(&event_lock);
302 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
303 mtx_unlock(&event_lock);
306 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
311 * Bind an interrupt event to the specified CPU. Note that not all
312 * platforms support binding an interrupt to a CPU. For those
313 * platforms this request will fail. Using a cpu id of NOCPU unbinds
314 * the interrupt event.
317 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
322 /* Need a CPU to bind to. */
323 if (cpu != NOCPU && CPU_ABSENT(cpu))
326 if (ie->ie_assign_cpu == NULL)
329 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
334 * If we have any ithreads try to set their mask first to verify
338 mtx_lock(&ie->ie_lock);
339 if (ie->ie_thread != NULL) {
340 id = ie->ie_thread->it_thread->td_tid;
341 mtx_unlock(&ie->ie_lock);
342 error = cpuset_setithread(id, cpu);
346 mtx_unlock(&ie->ie_lock);
349 error = ie->ie_assign_cpu(ie->ie_source, cpu);
352 mtx_lock(&ie->ie_lock);
353 if (ie->ie_thread != NULL) {
355 id = ie->ie_thread->it_thread->td_tid;
356 mtx_unlock(&ie->ie_lock);
357 (void)cpuset_setithread(id, cpu);
359 mtx_unlock(&ie->ie_lock);
365 mtx_lock(&ie->ie_lock);
367 mtx_unlock(&ie->ie_lock);
374 * Bind an interrupt event to the specified CPU. For supported platforms, any
375 * associated ithreads as well as the primary interrupt context will be bound
376 * to the specificed CPU.
379 intr_event_bind(struct intr_event *ie, int cpu)
382 return (_intr_event_bind(ie, cpu, true, true));
386 * Bind an interrupt event to the specified CPU, but do not bind associated
390 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
393 return (_intr_event_bind(ie, cpu, true, false));
397 * Bind an interrupt event's ithread to the specified CPU.
400 intr_event_bind_ithread(struct intr_event *ie, int cpu)
403 return (_intr_event_bind(ie, cpu, false, true));
407 * Bind an interrupt event's ithread to the specified cpuset.
410 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
414 mtx_lock(&ie->ie_lock);
415 if (ie->ie_thread != NULL) {
416 id = ie->ie_thread->it_thread->td_tid;
417 mtx_unlock(&ie->ie_lock);
418 return (cpuset_setthread(id, cs));
420 mtx_unlock(&ie->ie_lock);
425 static struct intr_event *
428 struct intr_event *ie;
430 mtx_lock(&event_lock);
431 TAILQ_FOREACH(ie, &event_list, ie_list)
432 if (ie->ie_irq == irq &&
433 (ie->ie_flags & IE_SOFT) == 0 &&
434 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
436 mtx_unlock(&event_lock);
441 intr_setaffinity(int irq, int mode, void *m)
443 struct intr_event *ie;
450 * If we're setting all cpus we can unbind. Otherwise make sure
451 * only one cpu is in the set.
453 if (CPU_CMP(cpuset_root, mask)) {
454 for (n = 0; n < CPU_SETSIZE; n++) {
455 if (!CPU_ISSET(n, mask))
462 ie = intr_lookup(irq);
467 return (intr_event_bind(ie, cpu));
468 case CPU_WHICH_INTRHANDLER:
469 return (intr_event_bind_irqonly(ie, cpu));
470 case CPU_WHICH_ITHREAD:
471 return (intr_event_bind_ithread(ie, cpu));
478 intr_getaffinity(int irq, int mode, void *m)
480 struct intr_event *ie;
488 ie = intr_lookup(irq);
496 case CPU_WHICH_INTRHANDLER:
497 mtx_lock(&ie->ie_lock);
498 if (ie->ie_cpu == NOCPU)
499 CPU_COPY(cpuset_root, mask);
501 CPU_SET(ie->ie_cpu, mask);
502 mtx_unlock(&ie->ie_lock);
504 case CPU_WHICH_ITHREAD:
505 mtx_lock(&ie->ie_lock);
506 if (ie->ie_thread == NULL) {
507 mtx_unlock(&ie->ie_lock);
508 CPU_COPY(cpuset_root, mask);
510 id = ie->ie_thread->it_thread->td_tid;
511 mtx_unlock(&ie->ie_lock);
512 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
515 CPU_COPY(&td->td_cpuset->cs_mask, mask);
525 intr_event_destroy(struct intr_event *ie)
528 mtx_lock(&event_lock);
529 mtx_lock(&ie->ie_lock);
530 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
531 mtx_unlock(&ie->ie_lock);
532 mtx_unlock(&event_lock);
535 TAILQ_REMOVE(&event_list, ie, ie_list);
537 if (ie->ie_thread != NULL) {
538 ithread_destroy(ie->ie_thread);
539 ie->ie_thread = NULL;
542 mtx_unlock(&ie->ie_lock);
543 mtx_unlock(&event_lock);
544 mtx_destroy(&ie->ie_lock);
549 static struct intr_thread *
550 ithread_create(const char *name)
552 struct intr_thread *ithd;
556 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
558 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
559 &td, RFSTOPPED | RFHIGHPID,
560 0, "intr", "%s", name);
562 panic("kproc_create() failed with %d", error);
564 sched_class(td, PRI_ITHD);
567 td->td_pflags |= TDP_ITHREAD;
568 ithd->it_thread = td;
569 CTR2(KTR_INTR, "%s: created %s", __func__, name);
574 ithread_destroy(struct intr_thread *ithread)
578 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
579 td = ithread->it_thread;
581 ithread->it_flags |= IT_DEAD;
582 if (TD_AWAITING_INTR(td)) {
584 sched_add(td, SRQ_INTR);
590 intr_event_add_handler(struct intr_event *ie, const char *name,
591 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
592 enum intr_type flags, void **cookiep)
594 struct intr_handler *ih, *temp_ih;
595 struct intr_handler **prevptr;
596 struct intr_thread *it;
598 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
601 /* Allocate and populate an interrupt handler structure. */
602 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
603 ih->ih_filter = filter;
604 ih->ih_handler = handler;
605 ih->ih_argument = arg;
606 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
609 if (flags & INTR_EXCL)
610 ih->ih_flags = IH_EXCLUSIVE;
611 if (flags & INTR_MPSAFE)
612 ih->ih_flags |= IH_MPSAFE;
613 if (flags & INTR_ENTROPY)
614 ih->ih_flags |= IH_ENTROPY;
615 if (flags & INTR_TYPE_NET)
616 ih->ih_flags |= IH_NET;
618 /* We can only have one exclusive handler in a event. */
619 mtx_lock(&ie->ie_lock);
620 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
621 if ((flags & INTR_EXCL) ||
622 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
623 mtx_unlock(&ie->ie_lock);
629 /* Create a thread if we need one. */
630 while (ie->ie_thread == NULL && handler != NULL) {
631 if (ie->ie_flags & IE_ADDING_THREAD)
632 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
634 ie->ie_flags |= IE_ADDING_THREAD;
635 mtx_unlock(&ie->ie_lock);
636 it = ithread_create("intr: newborn");
637 mtx_lock(&ie->ie_lock);
638 ie->ie_flags &= ~IE_ADDING_THREAD;
646 /* Add the new handler to the event in priority order. */
647 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
648 if (temp_ih->ih_pri > ih->ih_pri)
651 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
653 intr_event_update(ie);
655 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
657 mtx_unlock(&ie->ie_lock);
665 * Append a description preceded by a ':' to the name of the specified
669 intr_event_describe_handler(struct intr_event *ie, void *cookie,
672 struct intr_handler *ih;
676 mtx_lock(&ie->ie_lock);
678 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
683 mtx_unlock(&ie->ie_lock);
684 panic("handler %p not found in interrupt event %p", cookie, ie);
690 * Look for an existing description by checking for an
691 * existing ":". This assumes device names do not include
692 * colons. If one is found, prepare to insert the new
693 * description at that point. If one is not found, find the
694 * end of the name to use as the insertion point.
696 start = strchr(ih->ih_name, ':');
698 start = strchr(ih->ih_name, 0);
701 * See if there is enough remaining room in the string for the
702 * description + ":". The "- 1" leaves room for the trailing
703 * '\0'. The "+ 1" accounts for the colon.
705 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
706 if (strlen(descr) + 1 > space) {
707 mtx_unlock(&ie->ie_lock);
711 /* Append a colon followed by the description. */
713 strcpy(start + 1, descr);
714 intr_event_update(ie);
715 mtx_unlock(&ie->ie_lock);
720 * Return the ie_source field from the intr_event an intr_handler is
724 intr_handler_source(void *cookie)
726 struct intr_handler *ih;
727 struct intr_event *ie;
729 ih = (struct intr_handler *)cookie;
734 ("interrupt handler \"%s\" has a NULL interrupt event",
736 return (ie->ie_source);
740 * If intr_event_handle() is running in the ISR context at the time of the call,
741 * then wait for it to complete.
744 intr_event_barrier(struct intr_event *ie)
748 mtx_assert(&ie->ie_lock, MA_OWNED);
749 phase = ie->ie_phase;
752 * Switch phase to direct future interrupts to the other active counter.
753 * Make sure that any preceding stores are visible before the switch.
755 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
756 atomic_store_rel_int(&ie->ie_phase, !phase);
759 * This code cooperates with wait-free iteration of ie_handlers
760 * in intr_event_handle.
761 * Make sure that the removal and the phase update are not reordered
762 * with the active count check.
763 * Note that no combination of acquire and release fences can provide
764 * that guarantee as Store->Load sequences can always be reordered.
766 atomic_thread_fence_seq_cst();
769 * Now wait on the inactive phase.
770 * The acquire fence is needed so that that all post-barrier accesses
771 * are after the check.
773 while (ie->ie_active[phase] > 0)
775 atomic_thread_fence_acq();
779 intr_handler_barrier(struct intr_handler *handler)
781 struct intr_event *ie;
783 ie = handler->ih_event;
784 mtx_assert(&ie->ie_lock, MA_OWNED);
785 KASSERT((handler->ih_flags & IH_DEAD) == 0,
786 ("update for a removed handler"));
788 if (ie->ie_thread == NULL) {
789 intr_event_barrier(ie);
792 if ((handler->ih_flags & IH_CHANGED) == 0) {
793 handler->ih_flags |= IH_CHANGED;
794 intr_event_schedule_thread(ie, NULL);
796 while ((handler->ih_flags & IH_CHANGED) != 0)
797 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
801 * Sleep until an ithread finishes executing an interrupt handler.
803 * XXX Doesn't currently handle interrupt filters or fast interrupt
804 * handlers. This is intended for LinuxKPI drivers only.
805 * Do not use in BSD code.
810 struct intr_event *ie;
811 struct intr_thread *ithd;
814 ie = intr_lookup(irq);
817 if (ie->ie_thread == NULL)
819 ithd = ie->ie_thread;
820 td = ithd->it_thread;
822 * We set the flag and wait for it to be cleared to avoid
823 * long delays with potentially busy interrupt handlers
824 * were we to only sample TD_AWAITING_INTR() every tick.
827 if (!TD_AWAITING_INTR(td)) {
828 ithd->it_flags |= IT_WAIT;
829 while (ithd->it_flags & IT_WAIT) {
840 intr_event_remove_handler(void *cookie)
842 struct intr_handler *handler = (struct intr_handler *)cookie;
843 struct intr_event *ie;
844 struct intr_handler *ih;
845 struct intr_handler **prevptr;
852 ie = handler->ih_event;
854 ("interrupt handler \"%s\" has a NULL interrupt event",
857 mtx_lock(&ie->ie_lock);
858 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
860 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
865 panic("interrupt handler \"%s\" not found in "
866 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
870 * If there is no ithread, then directly remove the handler. Note that
871 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
872 * care needs to be taken to keep ie_handlers consistent and to free
873 * the removed handler only when ie_handlers is quiescent.
875 if (ie->ie_thread == NULL) {
876 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
877 intr_event_barrier(ie);
878 intr_event_update(ie);
879 mtx_unlock(&ie->ie_lock);
880 free(handler, M_ITHREAD);
885 * Let the interrupt thread do the job.
886 * The interrupt source is disabled when the interrupt thread is
887 * running, so it does not have to worry about interaction with
888 * intr_event_handle().
890 KASSERT((handler->ih_flags & IH_DEAD) == 0,
891 ("duplicate handle remove"));
892 handler->ih_flags |= IH_DEAD;
893 intr_event_schedule_thread(ie, NULL);
894 while (handler->ih_flags & IH_DEAD)
895 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
896 intr_event_update(ie);
900 * XXX: This could be bad in the case of ppbus(8). Also, I think
901 * this could lead to races of stale data when servicing an
905 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
906 if (ih->ih_handler != NULL) {
912 ithread_destroy(ie->ie_thread);
913 ie->ie_thread = NULL;
916 mtx_unlock(&ie->ie_lock);
917 free(handler, M_ITHREAD);
922 intr_event_suspend_handler(void *cookie)
924 struct intr_handler *handler = (struct intr_handler *)cookie;
925 struct intr_event *ie;
929 ie = handler->ih_event;
931 ("interrupt handler \"%s\" has a NULL interrupt event",
933 mtx_lock(&ie->ie_lock);
934 handler->ih_flags |= IH_SUSP;
935 intr_handler_barrier(handler);
936 mtx_unlock(&ie->ie_lock);
941 intr_event_resume_handler(void *cookie)
943 struct intr_handler *handler = (struct intr_handler *)cookie;
944 struct intr_event *ie;
948 ie = handler->ih_event;
950 ("interrupt handler \"%s\" has a NULL interrupt event",
954 * intr_handler_barrier() acts not only as a barrier,
955 * it also allows to check for any pending interrupts.
957 mtx_lock(&ie->ie_lock);
958 handler->ih_flags &= ~IH_SUSP;
959 intr_handler_barrier(handler);
960 mtx_unlock(&ie->ie_lock);
965 intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame)
967 struct intr_entropy entropy;
968 struct intr_thread *it;
973 * If no ithread or no handlers, then we have a stray interrupt.
975 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
976 ie->ie_thread == NULL)
984 * If any of the handlers for this ithread claim to be good
985 * sources of entropy, then gather some.
987 if (ie->ie_hflags & IH_ENTROPY) {
988 entropy.event = (uintptr_t)ie;
990 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
993 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
996 * Set it_need to tell the thread to keep running if it is already
997 * running. Then, lock the thread and see if we actually need to
998 * put it on the runqueue.
1000 * Use store_rel to arrange that the store to ih_need in
1001 * swi_sched() is before the store to it_need and prepare for
1002 * transfer of this order to loads in the ithread.
1004 atomic_store_rel_int(&it->it_need, 1);
1006 if (TD_AWAITING_INTR(td)) {
1008 atomic_set_int(&it->it_waiting, 0);
1010 PMC_SOFT_CALL_TF( , , intr, schedule, frame);
1012 PMC_SOFT_CALL( , , intr, schedule);
1014 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
1017 sched_add(td, SRQ_INTR);
1020 atomic_add_int(&it->it_waiting, 1);
1022 if (atomic_load_int(&it->it_waiting) >= intr_hwpmc_waiting_report_threshold) {
1024 PMC_SOFT_CALL_TF( , , intr, waiting, frame);
1026 PMC_SOFT_CALL( , , intr, waiting);
1029 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1030 __func__, td->td_proc->p_pid, td->td_name, it->it_need, TD_GET_STATE(td));
1038 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1039 * since interrupts are generated in software rather than being directed by
1043 swi_assign_cpu(void *arg, int cpu)
1050 * Add a software interrupt handler to a specified event. If a given event
1051 * is not specified, then a new event is created.
1054 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1055 void *arg, int pri, enum intr_type flags, void **cookiep)
1057 struct intr_event *ie;
1060 if (flags & INTR_ENTROPY)
1063 ie = (eventp != NULL) ? *eventp : NULL;
1066 if (!(ie->ie_flags & IE_SOFT))
1069 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1070 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1076 if (handler != NULL) {
1077 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1078 PI_SWI(pri), flags, cookiep);
1084 * Schedule a software interrupt thread.
1087 swi_sched(void *cookie, int flags)
1089 struct intr_handler *ih = (struct intr_handler *)cookie;
1090 struct intr_event *ie = ih->ih_event;
1091 struct intr_entropy entropy;
1094 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1097 if ((flags & SWI_FROMNMI) == 0) {
1098 entropy.event = (uintptr_t)ih;
1099 entropy.td = curthread;
1100 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1104 * Set ih_need for this handler so that if the ithread is already
1105 * running it will execute this handler on the next pass. Otherwise,
1106 * it will execute it the next time it runs.
1110 if (flags & SWI_DELAY)
1113 if (flags & SWI_FROMNMI) {
1114 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1115 KASSERT(ie == clk_intr_event,
1116 ("SWI_FROMNMI used not with clk_intr_event"));
1117 ipi_self_from_nmi(IPI_SWI);
1121 error = intr_event_schedule_thread(ie, NULL);
1122 KASSERT(error == 0, ("stray software interrupt"));
1127 * Remove a software interrupt handler. Currently this code does not
1128 * remove the associated interrupt event if it becomes empty. Calling code
1129 * may do so manually via intr_event_destroy(), but that's not really
1130 * an optimal interface.
1133 swi_remove(void *cookie)
1136 return (intr_event_remove_handler(cookie));
1140 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1142 struct intr_handler *ih, *ihn, *ihp;
1145 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1147 * If this handler is marked for death, remove it from
1148 * the list of handlers and wake up the sleeper.
1150 if (ih->ih_flags & IH_DEAD) {
1151 mtx_lock(&ie->ie_lock);
1153 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1155 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1156 ih->ih_flags &= ~IH_DEAD;
1158 mtx_unlock(&ie->ie_lock);
1163 * Now that we know that the current element won't be removed
1164 * update the previous element.
1168 if ((ih->ih_flags & IH_CHANGED) != 0) {
1169 mtx_lock(&ie->ie_lock);
1170 ih->ih_flags &= ~IH_CHANGED;
1172 mtx_unlock(&ie->ie_lock);
1175 /* Skip filter only handlers */
1176 if (ih->ih_handler == NULL)
1179 /* Skip suspended handlers */
1180 if ((ih->ih_flags & IH_SUSP) != 0)
1184 * For software interrupt threads, we only execute
1185 * handlers that have their need flag set. Hardware
1186 * interrupt threads always invoke all of their handlers.
1188 * ih_need can only be 0 or 1. Failed cmpset below
1189 * means that there is no request to execute handlers,
1190 * so a retry of the cmpset is not needed.
1192 if ((ie->ie_flags & IE_SOFT) != 0 &&
1193 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1196 /* Execute this handler. */
1197 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1198 __func__, p->p_pid, (void *)ih->ih_handler,
1199 ih->ih_argument, ih->ih_name, ih->ih_flags);
1201 if (!(ih->ih_flags & IH_MPSAFE))
1203 ih->ih_handler(ih->ih_argument);
1204 if (!(ih->ih_flags & IH_MPSAFE))
1210 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1213 /* Interrupt handlers should not sleep. */
1214 if (!(ie->ie_flags & IE_SOFT))
1215 THREAD_NO_SLEEPING();
1216 intr_event_execute_handlers(p, ie);
1217 if (!(ie->ie_flags & IE_SOFT))
1218 THREAD_SLEEPING_OK();
1221 * Interrupt storm handling:
1223 * If this interrupt source is currently storming, then throttle
1224 * it to only fire the handler once per clock tick.
1226 * If this interrupt source is not currently storming, but the
1227 * number of back to back interrupts exceeds the storm threshold,
1228 * then enter storming mode.
1230 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1231 !(ie->ie_flags & IE_SOFT)) {
1232 /* Report the message only once every second. */
1233 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1235 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1243 * Now that all the handlers have had a chance to run, reenable
1244 * the interrupt source.
1246 if (ie->ie_post_ithread != NULL)
1247 ie->ie_post_ithread(ie->ie_source);
1251 * This is the main code for interrupt threads.
1254 ithread_loop(void *arg)
1256 struct epoch_tracker et;
1257 struct intr_thread *ithd;
1258 struct intr_event *ie;
1261 int wake, epoch_count;
1266 ithd = (struct intr_thread *)arg;
1267 KASSERT(ithd->it_thread == td,
1268 ("%s: ithread and proc linkage out of sync", __func__));
1269 ie = ithd->it_event;
1274 * As long as we have interrupts outstanding, go through the
1275 * list of handlers, giving each one a go at it.
1279 * If we are an orphaned thread, then just die.
1281 if (ithd->it_flags & IT_DEAD) {
1282 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1283 p->p_pid, td->td_name);
1284 free(ithd, M_ITHREAD);
1289 * Service interrupts. If another interrupt arrives while
1290 * we are running, it will set it_need to note that we
1291 * should make another pass.
1293 * The load_acq part of the following cmpset ensures
1294 * that the load of ih_need in ithread_execute_handlers()
1295 * is ordered after the load of it_need here.
1298 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1301 NET_EPOCH_ENTER(et);
1303 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1304 ithread_execute_handlers(p, ie);
1306 ++epoch_count >= intr_epoch_batch) {
1309 NET_EPOCH_ENTER(et);
1314 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1315 mtx_assert(&Giant, MA_NOTOWNED);
1318 * Processed all our interrupts. Now get the sched
1319 * lock. This may take a while and it_need may get
1320 * set again, so we have to check it again.
1323 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1324 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1327 mi_switch(SW_VOL | SWT_IWAIT);
1329 if (ithd->it_flags & IT_WAIT) {
1331 ithd->it_flags &= ~IT_WAIT;
1343 * Main interrupt handling body.
1346 * o ie: the event connected to this interrupt.
1347 * o frame: some archs (i.e. i386) pass a frame to some.
1348 * handlers as their main argument.
1350 * o 0: everything ok.
1351 * o EINVAL: stray interrupt.
1354 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1356 struct intr_handler *ih;
1357 struct trapframe *oldframe;
1361 bool filter, thread;
1365 #ifdef KSTACK_USAGE_PROF
1366 intr_prof_stack_use(td, frame);
1369 /* An interrupt with no event or handlers is a stray interrupt. */
1370 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1374 * Execute fast interrupt handlers directly.
1375 * To support clock handlers, if a handler registers
1376 * with a NULL argument, then we pass it a pointer to
1377 * a trapframe as its argument.
1379 td->td_intr_nesting_level++;
1384 oldframe = td->td_intr_frame;
1385 td->td_intr_frame = frame;
1387 phase = ie->ie_phase;
1388 atomic_add_int(&ie->ie_active[phase], 1);
1391 * This fence is required to ensure that no later loads are
1392 * re-ordered before the ie_active store.
1394 atomic_thread_fence_seq_cst();
1396 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1397 if ((ih->ih_flags & IH_SUSP) != 0)
1399 if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1401 if (ih->ih_filter == NULL) {
1405 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1406 ih->ih_filter, ih->ih_argument == NULL ? frame :
1407 ih->ih_argument, ih->ih_name);
1408 if (ih->ih_argument == NULL)
1409 ret = ih->ih_filter(frame);
1411 ret = ih->ih_filter(ih->ih_argument);
1413 PMC_SOFT_CALL_TF( , , intr, all, frame);
1415 KASSERT(ret == FILTER_STRAY ||
1416 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1417 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1418 ("%s: incorrect return value %#x from %s", __func__, ret,
1420 filter = filter || ret == FILTER_HANDLED;
1422 if (ret & FILTER_SCHEDULE_THREAD)
1423 PMC_SOFT_CALL_TF( , , intr, ithread, frame);
1424 else if (ret & FILTER_HANDLED)
1425 PMC_SOFT_CALL_TF( , , intr, filter, frame);
1426 else if (ret == FILTER_STRAY)
1427 PMC_SOFT_CALL_TF( , , intr, stray, frame);
1431 * Wrapper handler special handling:
1433 * in some particular cases (like pccard and pccbb),
1434 * the _real_ device handler is wrapped in a couple of
1435 * functions - a filter wrapper and an ithread wrapper.
1436 * In this case (and just in this case), the filter wrapper
1437 * could ask the system to schedule the ithread and mask
1438 * the interrupt source if the wrapped handler is composed
1439 * of just an ithread handler.
1441 * TODO: write a generic wrapper to avoid people rolling
1445 if (ret == FILTER_SCHEDULE_THREAD)
1449 atomic_add_rel_int(&ie->ie_active[phase], -1);
1451 td->td_intr_frame = oldframe;
1454 if (ie->ie_pre_ithread != NULL)
1455 ie->ie_pre_ithread(ie->ie_source);
1457 if (ie->ie_post_filter != NULL)
1458 ie->ie_post_filter(ie->ie_source);
1461 /* Schedule the ithread if needed. */
1465 error = intr_event_schedule_thread(ie, frame);
1466 KASSERT(error == 0, ("bad stray interrupt"));
1469 td->td_intr_nesting_level--;
1471 /* The interrupt is not aknowledged by any filter and has no ithread. */
1472 if (!thread && !filter)
1480 * Dump details about an interrupt handler
1483 db_dump_intrhand(struct intr_handler *ih)
1487 db_printf("\t%-10s ", ih->ih_name);
1488 switch (ih->ih_pri) {
1508 if (ih->ih_pri >= PI_SOFT)
1511 db_printf("%4u", ih->ih_pri);
1515 if (ih->ih_filter != NULL) {
1517 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1519 if (ih->ih_handler != NULL) {
1520 if (ih->ih_filter != NULL)
1523 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1525 db_printf("(%p)", ih->ih_argument);
1527 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1531 if (ih->ih_flags & IH_EXCLUSIVE) {
1537 if (ih->ih_flags & IH_ENTROPY) {
1540 db_printf("ENTROPY");
1543 if (ih->ih_flags & IH_DEAD) {
1549 if (ih->ih_flags & IH_MPSAFE) {
1552 db_printf("MPSAFE");
1566 * Dump details about a event.
1569 db_dump_intr_event(struct intr_event *ie, int handlers)
1571 struct intr_handler *ih;
1572 struct intr_thread *it;
1575 db_printf("%s ", ie->ie_fullname);
1578 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1580 db_printf("(no thread)");
1581 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1582 (it != NULL && it->it_need)) {
1585 if (ie->ie_flags & IE_SOFT) {
1589 if (ie->ie_flags & IE_ADDING_THREAD) {
1592 db_printf("ADDING_THREAD");
1595 if (it != NULL && it->it_need) {
1605 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1606 db_dump_intrhand(ih);
1610 * Dump data about interrupt handlers
1612 DB_SHOW_COMMAND(intr, db_show_intr)
1614 struct intr_event *ie;
1617 verbose = strchr(modif, 'v') != NULL;
1618 all = strchr(modif, 'a') != NULL;
1619 TAILQ_FOREACH(ie, &event_list, ie_list) {
1620 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1622 db_dump_intr_event(ie, verbose);
1630 * Start standard software interrupt threads
1633 start_softintr(void *dummy)
1636 if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1638 panic("died while creating clk swi ithread");
1639 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1640 panic("died while creating vm swi ithread");
1642 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1646 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1647 * The data for this machine dependent, and the declarations are in machine
1648 * dependent code. The layout of intrnames and intrcnt however is machine
1651 * We do not know the length of intrcnt and intrnames at compile time, so
1652 * calculate things at run time.
1655 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1657 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1660 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1661 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1662 sysctl_intrnames, "",
1666 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1669 uint32_t *intrcnt32;
1673 if (req->flags & SCTL_MASK32) {
1675 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1676 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1677 if (intrcnt32 == NULL)
1679 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1680 intrcnt32[i] = intrcnt[i];
1681 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1682 free(intrcnt32, M_TEMP);
1686 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1689 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1690 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1692 "Interrupt Counts");
1696 * DDB command to dump the interrupt statistics.
1698 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1706 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1711 db_printf("%s\t%lu\n", cp, *i);
1712 cp += strlen(cp) + 1;