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");
110 #define PMC_HOOK_INSTALLED_ANY() __predict_false(pmc_hook != NULL)
112 static TAILQ_HEAD(, intr_event) event_list =
113 TAILQ_HEAD_INITIALIZER(event_list);
114 static struct mtx event_lock;
115 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
117 static void intr_event_update(struct intr_event *ie);
118 static int intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame);
119 static struct intr_thread *ithread_create(const char *name);
120 static void ithread_destroy(struct intr_thread *ithread);
121 static void ithread_execute_handlers(struct proc *p,
122 struct intr_event *ie);
123 static void ithread_loop(void *);
124 static void ithread_update(struct intr_thread *ithd);
125 static void start_softintr(void *);
128 #include <sys/pmckern.h>
129 PMC_SOFT_DEFINE( , , intr, all);
130 PMC_SOFT_DEFINE( , , intr, ithread);
131 PMC_SOFT_DEFINE( , , intr, filter);
132 PMC_SOFT_DEFINE( , , intr, stray);
133 PMC_SOFT_DEFINE( , , intr, schedule);
134 PMC_SOFT_DEFINE( , , intr, waiting);
136 #define PMC_SOFT_CALL_INTR_HLPR(event, frame) \
139 PMC_SOFT_CALL_TF( , , intr, event, frame); \
141 PMC_SOFT_CALL( , , intr, event); \
145 /* Map an interrupt type to an ithread priority. */
147 intr_priority(enum intr_type flags)
151 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
152 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
173 pri = PI_DULL; /* don't care */
176 /* We didn't specify an interrupt level. */
177 panic("intr_priority: no interrupt type in flags");
184 * Update an ithread based on the associated intr_event.
187 ithread_update(struct intr_thread *ithd)
189 struct intr_event *ie;
194 td = ithd->it_thread;
195 mtx_assert(&ie->ie_lock, MA_OWNED);
197 /* Determine the overall priority of this event. */
198 if (CK_SLIST_EMPTY(&ie->ie_handlers))
201 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
203 /* Update name and priority. */
204 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
206 sched_clear_tdname(td);
214 * Regenerate the full name of an interrupt event and update its priority.
217 intr_event_update(struct intr_event *ie)
219 struct intr_handler *ih;
221 int missed, space, flags;
223 /* Start off with no entropy and just the name of the event. */
224 mtx_assert(&ie->ie_lock, MA_OWNED);
225 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
230 /* Run through all the handlers updating values. */
231 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
232 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
233 sizeof(ie->ie_fullname)) {
234 strcat(ie->ie_fullname, " ");
235 strcat(ie->ie_fullname, ih->ih_name);
239 flags |= ih->ih_flags;
241 ie->ie_hflags = flags;
244 * If there is only one handler and its name is too long, just copy in
245 * as much of the end of the name (includes the unit number) as will
246 * fit. Otherwise, we have multiple handlers and not all of the names
247 * will fit. Add +'s to indicate missing names. If we run out of room
248 * and still have +'s to add, change the last character from a + to a *.
250 if (missed == 1 && space == 1) {
251 ih = CK_SLIST_FIRST(&ie->ie_handlers);
252 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
253 sizeof(ie->ie_fullname);
254 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
255 strcat(ie->ie_fullname, &ih->ih_name[missed]);
258 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
259 while (missed-- > 0) {
260 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
267 strcat(ie->ie_fullname, " +");
270 strcat(ie->ie_fullname, "+");
274 * If this event has an ithread, update it's priority and
277 if (ie->ie_thread != NULL)
278 ithread_update(ie->ie_thread);
279 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
283 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
284 void (*pre_ithread)(void *), void (*post_ithread)(void *),
285 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
286 const char *fmt, ...)
288 struct intr_event *ie;
291 /* The only valid flag during creation is IE_SOFT. */
292 if ((flags & ~IE_SOFT) != 0)
294 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
295 ie->ie_source = source;
296 ie->ie_pre_ithread = pre_ithread;
297 ie->ie_post_ithread = post_ithread;
298 ie->ie_post_filter = post_filter;
299 ie->ie_assign_cpu = assign_cpu;
300 ie->ie_flags = flags;
303 CK_SLIST_INIT(&ie->ie_handlers);
304 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
307 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
309 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
310 mtx_lock(&event_lock);
311 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
312 mtx_unlock(&event_lock);
315 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
320 * Bind an interrupt event to the specified CPU. Note that not all
321 * platforms support binding an interrupt to a CPU. For those
322 * platforms this request will fail. Using a cpu id of NOCPU unbinds
323 * the interrupt event.
326 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
331 /* Need a CPU to bind to. */
332 if (cpu != NOCPU && CPU_ABSENT(cpu))
335 if (ie->ie_assign_cpu == NULL)
338 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
343 * If we have any ithreads try to set their mask first to verify
347 mtx_lock(&ie->ie_lock);
348 if (ie->ie_thread != NULL) {
349 id = ie->ie_thread->it_thread->td_tid;
350 mtx_unlock(&ie->ie_lock);
351 error = cpuset_setithread(id, cpu);
355 mtx_unlock(&ie->ie_lock);
358 error = ie->ie_assign_cpu(ie->ie_source, cpu);
361 mtx_lock(&ie->ie_lock);
362 if (ie->ie_thread != NULL) {
364 id = ie->ie_thread->it_thread->td_tid;
365 mtx_unlock(&ie->ie_lock);
366 (void)cpuset_setithread(id, cpu);
368 mtx_unlock(&ie->ie_lock);
374 mtx_lock(&ie->ie_lock);
376 mtx_unlock(&ie->ie_lock);
383 * Bind an interrupt event to the specified CPU. For supported platforms, any
384 * associated ithreads as well as the primary interrupt context will be bound
385 * to the specificed CPU.
388 intr_event_bind(struct intr_event *ie, int cpu)
391 return (_intr_event_bind(ie, cpu, true, true));
395 * Bind an interrupt event to the specified CPU, but do not bind associated
399 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
402 return (_intr_event_bind(ie, cpu, true, false));
406 * Bind an interrupt event's ithread to the specified CPU.
409 intr_event_bind_ithread(struct intr_event *ie, int cpu)
412 return (_intr_event_bind(ie, cpu, false, true));
416 * Bind an interrupt event's ithread to the specified cpuset.
419 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
423 mtx_lock(&ie->ie_lock);
424 if (ie->ie_thread != NULL) {
425 id = ie->ie_thread->it_thread->td_tid;
426 mtx_unlock(&ie->ie_lock);
427 return (cpuset_setthread(id, cs));
429 mtx_unlock(&ie->ie_lock);
434 static struct intr_event *
437 struct intr_event *ie;
439 mtx_lock(&event_lock);
440 TAILQ_FOREACH(ie, &event_list, ie_list)
441 if (ie->ie_irq == irq &&
442 (ie->ie_flags & IE_SOFT) == 0 &&
443 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
445 mtx_unlock(&event_lock);
450 intr_setaffinity(int irq, int mode, void *m)
452 struct intr_event *ie;
459 * If we're setting all cpus we can unbind. Otherwise make sure
460 * only one cpu is in the set.
462 if (CPU_CMP(cpuset_root, mask)) {
463 for (n = 0; n < CPU_SETSIZE; n++) {
464 if (!CPU_ISSET(n, mask))
471 ie = intr_lookup(irq);
476 return (intr_event_bind(ie, cpu));
477 case CPU_WHICH_INTRHANDLER:
478 return (intr_event_bind_irqonly(ie, cpu));
479 case CPU_WHICH_ITHREAD:
480 return (intr_event_bind_ithread(ie, cpu));
487 intr_getaffinity(int irq, int mode, void *m)
489 struct intr_event *ie;
497 ie = intr_lookup(irq);
505 case CPU_WHICH_INTRHANDLER:
506 mtx_lock(&ie->ie_lock);
507 if (ie->ie_cpu == NOCPU)
508 CPU_COPY(cpuset_root, mask);
510 CPU_SET(ie->ie_cpu, mask);
511 mtx_unlock(&ie->ie_lock);
513 case CPU_WHICH_ITHREAD:
514 mtx_lock(&ie->ie_lock);
515 if (ie->ie_thread == NULL) {
516 mtx_unlock(&ie->ie_lock);
517 CPU_COPY(cpuset_root, mask);
519 id = ie->ie_thread->it_thread->td_tid;
520 mtx_unlock(&ie->ie_lock);
521 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
524 CPU_COPY(&td->td_cpuset->cs_mask, mask);
534 intr_event_destroy(struct intr_event *ie)
537 mtx_lock(&event_lock);
538 mtx_lock(&ie->ie_lock);
539 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
540 mtx_unlock(&ie->ie_lock);
541 mtx_unlock(&event_lock);
544 TAILQ_REMOVE(&event_list, ie, ie_list);
546 if (ie->ie_thread != NULL) {
547 ithread_destroy(ie->ie_thread);
548 ie->ie_thread = NULL;
551 mtx_unlock(&ie->ie_lock);
552 mtx_unlock(&event_lock);
553 mtx_destroy(&ie->ie_lock);
558 static struct intr_thread *
559 ithread_create(const char *name)
561 struct intr_thread *ithd;
565 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
567 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
568 &td, RFSTOPPED | RFHIGHPID,
569 0, "intr", "%s", name);
571 panic("kproc_create() failed with %d", error);
573 sched_class(td, PRI_ITHD);
576 td->td_pflags |= TDP_ITHREAD;
577 ithd->it_thread = td;
578 CTR2(KTR_INTR, "%s: created %s", __func__, name);
583 ithread_destroy(struct intr_thread *ithread)
587 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
588 td = ithread->it_thread;
590 ithread->it_flags |= IT_DEAD;
591 if (TD_AWAITING_INTR(td)) {
593 sched_add(td, SRQ_INTR);
599 intr_event_add_handler(struct intr_event *ie, const char *name,
600 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
601 enum intr_type flags, void **cookiep)
603 struct intr_handler *ih, *temp_ih;
604 struct intr_handler **prevptr;
605 struct intr_thread *it;
607 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
610 /* Allocate and populate an interrupt handler structure. */
611 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
612 ih->ih_filter = filter;
613 ih->ih_handler = handler;
614 ih->ih_argument = arg;
615 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
618 if (flags & INTR_EXCL)
619 ih->ih_flags = IH_EXCLUSIVE;
620 if (flags & INTR_MPSAFE)
621 ih->ih_flags |= IH_MPSAFE;
622 if (flags & INTR_ENTROPY)
623 ih->ih_flags |= IH_ENTROPY;
624 if (flags & INTR_TYPE_NET)
625 ih->ih_flags |= IH_NET;
627 /* We can only have one exclusive handler in a event. */
628 mtx_lock(&ie->ie_lock);
629 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
630 if ((flags & INTR_EXCL) ||
631 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
632 mtx_unlock(&ie->ie_lock);
638 /* Create a thread if we need one. */
639 while (ie->ie_thread == NULL && handler != NULL) {
640 if (ie->ie_flags & IE_ADDING_THREAD)
641 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
643 ie->ie_flags |= IE_ADDING_THREAD;
644 mtx_unlock(&ie->ie_lock);
645 it = ithread_create("intr: newborn");
646 mtx_lock(&ie->ie_lock);
647 ie->ie_flags &= ~IE_ADDING_THREAD;
655 /* Add the new handler to the event in priority order. */
656 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
657 if (temp_ih->ih_pri > ih->ih_pri)
660 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
662 intr_event_update(ie);
664 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
666 mtx_unlock(&ie->ie_lock);
674 * Append a description preceded by a ':' to the name of the specified
678 intr_event_describe_handler(struct intr_event *ie, void *cookie,
681 struct intr_handler *ih;
685 mtx_lock(&ie->ie_lock);
687 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
692 mtx_unlock(&ie->ie_lock);
693 panic("handler %p not found in interrupt event %p", cookie, ie);
699 * Look for an existing description by checking for an
700 * existing ":". This assumes device names do not include
701 * colons. If one is found, prepare to insert the new
702 * description at that point. If one is not found, find the
703 * end of the name to use as the insertion point.
705 start = strchr(ih->ih_name, ':');
707 start = strchr(ih->ih_name, 0);
710 * See if there is enough remaining room in the string for the
711 * description + ":". The "- 1" leaves room for the trailing
712 * '\0'. The "+ 1" accounts for the colon.
714 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
715 if (strlen(descr) + 1 > space) {
716 mtx_unlock(&ie->ie_lock);
720 /* Append a colon followed by the description. */
722 strcpy(start + 1, descr);
723 intr_event_update(ie);
724 mtx_unlock(&ie->ie_lock);
729 * Return the ie_source field from the intr_event an intr_handler is
733 intr_handler_source(void *cookie)
735 struct intr_handler *ih;
736 struct intr_event *ie;
738 ih = (struct intr_handler *)cookie;
743 ("interrupt handler \"%s\" has a NULL interrupt event",
745 return (ie->ie_source);
749 * If intr_event_handle() is running in the ISR context at the time of the call,
750 * then wait for it to complete.
753 intr_event_barrier(struct intr_event *ie)
757 mtx_assert(&ie->ie_lock, MA_OWNED);
758 phase = ie->ie_phase;
761 * Switch phase to direct future interrupts to the other active counter.
762 * Make sure that any preceding stores are visible before the switch.
764 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
765 atomic_store_rel_int(&ie->ie_phase, !phase);
768 * This code cooperates with wait-free iteration of ie_handlers
769 * in intr_event_handle.
770 * Make sure that the removal and the phase update are not reordered
771 * with the active count check.
772 * Note that no combination of acquire and release fences can provide
773 * that guarantee as Store->Load sequences can always be reordered.
775 atomic_thread_fence_seq_cst();
778 * Now wait on the inactive phase.
779 * The acquire fence is needed so that that all post-barrier accesses
780 * are after the check.
782 while (ie->ie_active[phase] > 0)
784 atomic_thread_fence_acq();
788 intr_handler_barrier(struct intr_handler *handler)
790 struct intr_event *ie;
792 ie = handler->ih_event;
793 mtx_assert(&ie->ie_lock, MA_OWNED);
794 KASSERT((handler->ih_flags & IH_DEAD) == 0,
795 ("update for a removed handler"));
797 if (ie->ie_thread == NULL) {
798 intr_event_barrier(ie);
801 if ((handler->ih_flags & IH_CHANGED) == 0) {
802 handler->ih_flags |= IH_CHANGED;
803 intr_event_schedule_thread(ie, NULL);
805 while ((handler->ih_flags & IH_CHANGED) != 0)
806 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
810 * Sleep until an ithread finishes executing an interrupt handler.
812 * XXX Doesn't currently handle interrupt filters or fast interrupt
813 * handlers. This is intended for LinuxKPI drivers only.
814 * Do not use in BSD code.
819 struct intr_event *ie;
820 struct intr_thread *ithd;
823 ie = intr_lookup(irq);
826 if (ie->ie_thread == NULL)
828 ithd = ie->ie_thread;
829 td = ithd->it_thread;
831 * We set the flag and wait for it to be cleared to avoid
832 * long delays with potentially busy interrupt handlers
833 * were we to only sample TD_AWAITING_INTR() every tick.
836 if (!TD_AWAITING_INTR(td)) {
837 ithd->it_flags |= IT_WAIT;
838 while (ithd->it_flags & IT_WAIT) {
849 intr_event_remove_handler(void *cookie)
851 struct intr_handler *handler = (struct intr_handler *)cookie;
852 struct intr_event *ie;
853 struct intr_handler *ih;
854 struct intr_handler **prevptr;
861 ie = handler->ih_event;
863 ("interrupt handler \"%s\" has a NULL interrupt event",
866 mtx_lock(&ie->ie_lock);
867 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
869 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
874 panic("interrupt handler \"%s\" not found in "
875 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
879 * If there is no ithread, then directly remove the handler. Note that
880 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
881 * care needs to be taken to keep ie_handlers consistent and to free
882 * the removed handler only when ie_handlers is quiescent.
884 if (ie->ie_thread == NULL) {
885 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
886 intr_event_barrier(ie);
887 intr_event_update(ie);
888 mtx_unlock(&ie->ie_lock);
889 free(handler, M_ITHREAD);
894 * Let the interrupt thread do the job.
895 * The interrupt source is disabled when the interrupt thread is
896 * running, so it does not have to worry about interaction with
897 * intr_event_handle().
899 KASSERT((handler->ih_flags & IH_DEAD) == 0,
900 ("duplicate handle remove"));
901 handler->ih_flags |= IH_DEAD;
902 intr_event_schedule_thread(ie, NULL);
903 while (handler->ih_flags & IH_DEAD)
904 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
905 intr_event_update(ie);
909 * XXX: This could be bad in the case of ppbus(8). Also, I think
910 * this could lead to races of stale data when servicing an
914 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
915 if (ih->ih_handler != NULL) {
921 ithread_destroy(ie->ie_thread);
922 ie->ie_thread = NULL;
925 mtx_unlock(&ie->ie_lock);
926 free(handler, M_ITHREAD);
931 intr_event_suspend_handler(void *cookie)
933 struct intr_handler *handler = (struct intr_handler *)cookie;
934 struct intr_event *ie;
938 ie = handler->ih_event;
940 ("interrupt handler \"%s\" has a NULL interrupt event",
942 mtx_lock(&ie->ie_lock);
943 handler->ih_flags |= IH_SUSP;
944 intr_handler_barrier(handler);
945 mtx_unlock(&ie->ie_lock);
950 intr_event_resume_handler(void *cookie)
952 struct intr_handler *handler = (struct intr_handler *)cookie;
953 struct intr_event *ie;
957 ie = handler->ih_event;
959 ("interrupt handler \"%s\" has a NULL interrupt event",
963 * intr_handler_barrier() acts not only as a barrier,
964 * it also allows to check for any pending interrupts.
966 mtx_lock(&ie->ie_lock);
967 handler->ih_flags &= ~IH_SUSP;
968 intr_handler_barrier(handler);
969 mtx_unlock(&ie->ie_lock);
974 intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame)
976 struct intr_entropy entropy;
977 struct intr_thread *it;
982 * If no ithread or no handlers, then we have a stray interrupt.
984 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
985 ie->ie_thread == NULL)
993 * If any of the handlers for this ithread claim to be good
994 * sources of entropy, then gather some.
996 if (ie->ie_hflags & IH_ENTROPY) {
997 entropy.event = (uintptr_t)ie;
999 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
1002 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
1005 * Set it_need to tell the thread to keep running if it is already
1006 * running. Then, lock the thread and see if we actually need to
1007 * put it on the runqueue.
1009 * Use store_rel to arrange that the store to ih_need in
1010 * swi_sched() is before the store to it_need and prepare for
1011 * transfer of this order to loads in the ithread.
1013 atomic_store_rel_int(&it->it_need, 1);
1015 if (TD_AWAITING_INTR(td)) {
1018 if (PMC_HOOK_INSTALLED_ANY())
1019 PMC_SOFT_CALL_INTR_HLPR(schedule, frame);
1021 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
1024 sched_add(td, SRQ_INTR);
1028 if (PMC_HOOK_INSTALLED_ANY() &&
1029 (it->it_waiting >= intr_hwpmc_waiting_report_threshold))
1030 PMC_SOFT_CALL_INTR_HLPR(waiting, frame);
1032 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1033 __func__, td->td_proc->p_pid, td->td_name, it->it_need, TD_GET_STATE(td));
1041 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1042 * since interrupts are generated in software rather than being directed by
1046 swi_assign_cpu(void *arg, int cpu)
1053 * Add a software interrupt handler to a specified event. If a given event
1054 * is not specified, then a new event is created.
1057 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1058 void *arg, int pri, enum intr_type flags, void **cookiep)
1060 struct intr_event *ie;
1063 if (flags & INTR_ENTROPY)
1066 ie = (eventp != NULL) ? *eventp : NULL;
1069 if (!(ie->ie_flags & IE_SOFT))
1072 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1073 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1079 if (handler != NULL) {
1080 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1081 PI_SWI(pri), flags, cookiep);
1087 * Schedule a software interrupt thread.
1090 swi_sched(void *cookie, int flags)
1092 struct intr_handler *ih = (struct intr_handler *)cookie;
1093 struct intr_event *ie = ih->ih_event;
1094 struct intr_entropy entropy;
1097 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1100 if ((flags & SWI_FROMNMI) == 0) {
1101 entropy.event = (uintptr_t)ih;
1102 entropy.td = curthread;
1103 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1107 * Set ih_need for this handler so that if the ithread is already
1108 * running it will execute this handler on the next pass. Otherwise,
1109 * it will execute it the next time it runs.
1113 if (flags & SWI_DELAY)
1116 if (flags & SWI_FROMNMI) {
1117 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1118 KASSERT(ie == clk_intr_event,
1119 ("SWI_FROMNMI used not with clk_intr_event"));
1120 ipi_self_from_nmi(IPI_SWI);
1124 error = intr_event_schedule_thread(ie, NULL);
1125 KASSERT(error == 0, ("stray software interrupt"));
1130 * Remove a software interrupt handler. Currently this code does not
1131 * remove the associated interrupt event if it becomes empty. Calling code
1132 * may do so manually via intr_event_destroy(), but that's not really
1133 * an optimal interface.
1136 swi_remove(void *cookie)
1139 return (intr_event_remove_handler(cookie));
1143 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1145 struct intr_handler *ih, *ihn, *ihp;
1148 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1150 * If this handler is marked for death, remove it from
1151 * the list of handlers and wake up the sleeper.
1153 if (ih->ih_flags & IH_DEAD) {
1154 mtx_lock(&ie->ie_lock);
1156 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1158 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1159 ih->ih_flags &= ~IH_DEAD;
1161 mtx_unlock(&ie->ie_lock);
1166 * Now that we know that the current element won't be removed
1167 * update the previous element.
1171 if ((ih->ih_flags & IH_CHANGED) != 0) {
1172 mtx_lock(&ie->ie_lock);
1173 ih->ih_flags &= ~IH_CHANGED;
1175 mtx_unlock(&ie->ie_lock);
1178 /* Skip filter only handlers */
1179 if (ih->ih_handler == NULL)
1182 /* Skip suspended handlers */
1183 if ((ih->ih_flags & IH_SUSP) != 0)
1187 * For software interrupt threads, we only execute
1188 * handlers that have their need flag set. Hardware
1189 * interrupt threads always invoke all of their handlers.
1191 * ih_need can only be 0 or 1. Failed cmpset below
1192 * means that there is no request to execute handlers,
1193 * so a retry of the cmpset is not needed.
1195 if ((ie->ie_flags & IE_SOFT) != 0 &&
1196 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1199 /* Execute this handler. */
1200 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1201 __func__, p->p_pid, (void *)ih->ih_handler,
1202 ih->ih_argument, ih->ih_name, ih->ih_flags);
1204 if (!(ih->ih_flags & IH_MPSAFE))
1206 ih->ih_handler(ih->ih_argument);
1207 if (!(ih->ih_flags & IH_MPSAFE))
1213 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1216 /* Interrupt handlers should not sleep. */
1217 if (!(ie->ie_flags & IE_SOFT))
1218 THREAD_NO_SLEEPING();
1219 intr_event_execute_handlers(p, ie);
1220 if (!(ie->ie_flags & IE_SOFT))
1221 THREAD_SLEEPING_OK();
1224 * Interrupt storm handling:
1226 * If this interrupt source is currently storming, then throttle
1227 * it to only fire the handler once per clock tick.
1229 * If this interrupt source is not currently storming, but the
1230 * number of back to back interrupts exceeds the storm threshold,
1231 * then enter storming mode.
1233 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1234 !(ie->ie_flags & IE_SOFT)) {
1235 /* Report the message only once every second. */
1236 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1238 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1246 * Now that all the handlers have had a chance to run, reenable
1247 * the interrupt source.
1249 if (ie->ie_post_ithread != NULL)
1250 ie->ie_post_ithread(ie->ie_source);
1254 * This is the main code for interrupt threads.
1257 ithread_loop(void *arg)
1259 struct epoch_tracker et;
1260 struct intr_thread *ithd;
1261 struct intr_event *ie;
1264 int wake, epoch_count;
1269 ithd = (struct intr_thread *)arg;
1270 KASSERT(ithd->it_thread == td,
1271 ("%s: ithread and proc linkage out of sync", __func__));
1272 ie = ithd->it_event;
1277 * As long as we have interrupts outstanding, go through the
1278 * list of handlers, giving each one a go at it.
1282 * If we are an orphaned thread, then just die.
1284 if (ithd->it_flags & IT_DEAD) {
1285 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1286 p->p_pid, td->td_name);
1287 free(ithd, M_ITHREAD);
1292 * Service interrupts. If another interrupt arrives while
1293 * we are running, it will set it_need to note that we
1294 * should make another pass.
1296 * The load_acq part of the following cmpset ensures
1297 * that the load of ih_need in ithread_execute_handlers()
1298 * is ordered after the load of it_need here.
1301 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1304 NET_EPOCH_ENTER(et);
1306 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1307 ithread_execute_handlers(p, ie);
1309 ++epoch_count >= intr_epoch_batch) {
1312 NET_EPOCH_ENTER(et);
1317 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1318 mtx_assert(&Giant, MA_NOTOWNED);
1321 * Processed all our interrupts. Now get the sched
1322 * lock. This may take a while and it_need may get
1323 * set again, so we have to check it again.
1326 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1327 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1330 mi_switch(SW_VOL | SWT_IWAIT);
1332 if (ithd->it_flags & IT_WAIT) {
1334 ithd->it_flags &= ~IT_WAIT;
1346 * Main interrupt handling body.
1349 * o ie: the event connected to this interrupt.
1350 * o frame: some archs (i.e. i386) pass a frame to some.
1351 * handlers as their main argument.
1353 * o 0: everything ok.
1354 * o EINVAL: stray interrupt.
1357 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1359 struct intr_handler *ih;
1360 struct trapframe *oldframe;
1364 bool filter, thread;
1368 #ifdef KSTACK_USAGE_PROF
1369 intr_prof_stack_use(td, frame);
1372 /* An interrupt with no event or handlers is a stray interrupt. */
1373 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1377 * Execute fast interrupt handlers directly.
1378 * To support clock handlers, if a handler registers
1379 * with a NULL argument, then we pass it a pointer to
1380 * a trapframe as its argument.
1382 td->td_intr_nesting_level++;
1387 oldframe = td->td_intr_frame;
1388 td->td_intr_frame = frame;
1390 phase = ie->ie_phase;
1391 atomic_add_int(&ie->ie_active[phase], 1);
1394 * This fence is required to ensure that no later loads are
1395 * re-ordered before the ie_active store.
1397 atomic_thread_fence_seq_cst();
1399 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1400 if ((ih->ih_flags & IH_SUSP) != 0)
1402 if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1404 if (ih->ih_filter == NULL) {
1408 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1409 ih->ih_filter, ih->ih_argument == NULL ? frame :
1410 ih->ih_argument, ih->ih_name);
1411 if (ih->ih_argument == NULL)
1412 ret = ih->ih_filter(frame);
1414 ret = ih->ih_filter(ih->ih_argument);
1416 PMC_SOFT_CALL_TF( , , intr, all, frame);
1418 KASSERT(ret == FILTER_STRAY ||
1419 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1420 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1421 ("%s: incorrect return value %#x from %s", __func__, ret,
1423 filter = filter || ret == FILTER_HANDLED;
1425 if (ret & FILTER_SCHEDULE_THREAD)
1426 PMC_SOFT_CALL_TF( , , intr, ithread, frame);
1427 else if (ret & FILTER_HANDLED)
1428 PMC_SOFT_CALL_TF( , , intr, filter, frame);
1429 else if (ret == FILTER_STRAY)
1430 PMC_SOFT_CALL_TF( , , intr, stray, frame);
1434 * Wrapper handler special handling:
1436 * in some particular cases (like pccard and pccbb),
1437 * the _real_ device handler is wrapped in a couple of
1438 * functions - a filter wrapper and an ithread wrapper.
1439 * In this case (and just in this case), the filter wrapper
1440 * could ask the system to schedule the ithread and mask
1441 * the interrupt source if the wrapped handler is composed
1442 * of just an ithread handler.
1444 * TODO: write a generic wrapper to avoid people rolling
1448 if (ret == FILTER_SCHEDULE_THREAD)
1452 atomic_add_rel_int(&ie->ie_active[phase], -1);
1454 td->td_intr_frame = oldframe;
1457 if (ie->ie_pre_ithread != NULL)
1458 ie->ie_pre_ithread(ie->ie_source);
1460 if (ie->ie_post_filter != NULL)
1461 ie->ie_post_filter(ie->ie_source);
1464 /* Schedule the ithread if needed. */
1468 error = intr_event_schedule_thread(ie, frame);
1469 KASSERT(error == 0, ("bad stray interrupt"));
1472 td->td_intr_nesting_level--;
1474 /* The interrupt is not aknowledged by any filter and has no ithread. */
1475 if (!thread && !filter)
1483 * Dump details about an interrupt handler
1486 db_dump_intrhand(struct intr_handler *ih)
1490 db_printf("\t%-10s ", ih->ih_name);
1491 switch (ih->ih_pri) {
1511 if (ih->ih_pri >= PI_SOFT)
1514 db_printf("%4u", ih->ih_pri);
1518 if (ih->ih_filter != NULL) {
1520 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1522 if (ih->ih_handler != NULL) {
1523 if (ih->ih_filter != NULL)
1526 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1528 db_printf("(%p)", ih->ih_argument);
1530 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1534 if (ih->ih_flags & IH_EXCLUSIVE) {
1540 if (ih->ih_flags & IH_ENTROPY) {
1543 db_printf("ENTROPY");
1546 if (ih->ih_flags & IH_DEAD) {
1552 if (ih->ih_flags & IH_MPSAFE) {
1555 db_printf("MPSAFE");
1569 * Dump details about a event.
1572 db_dump_intr_event(struct intr_event *ie, int handlers)
1574 struct intr_handler *ih;
1575 struct intr_thread *it;
1578 db_printf("%s ", ie->ie_fullname);
1581 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1583 db_printf("(no thread)");
1584 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1585 (it != NULL && it->it_need)) {
1588 if (ie->ie_flags & IE_SOFT) {
1592 if (ie->ie_flags & IE_ADDING_THREAD) {
1595 db_printf("ADDING_THREAD");
1598 if (it != NULL && it->it_need) {
1608 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1609 db_dump_intrhand(ih);
1613 * Dump data about interrupt handlers
1615 DB_SHOW_COMMAND(intr, db_show_intr)
1617 struct intr_event *ie;
1620 verbose = strchr(modif, 'v') != NULL;
1621 all = strchr(modif, 'a') != NULL;
1622 TAILQ_FOREACH(ie, &event_list, ie_list) {
1623 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1625 db_dump_intr_event(ie, verbose);
1633 * Start standard software interrupt threads
1636 start_softintr(void *dummy)
1639 if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1641 panic("died while creating clk swi ithread");
1642 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1643 panic("died while creating vm swi ithread");
1645 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1649 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1650 * The data for this machine dependent, and the declarations are in machine
1651 * dependent code. The layout of intrnames and intrcnt however is machine
1654 * We do not know the length of intrcnt and intrnames at compile time, so
1655 * calculate things at run time.
1658 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1660 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1663 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1664 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1665 sysctl_intrnames, "",
1669 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1672 uint32_t *intrcnt32;
1676 if (req->flags & SCTL_MASK32) {
1678 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1679 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1680 if (intrcnt32 == NULL)
1682 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1683 intrcnt32[i] = intrcnt[i];
1684 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1685 free(intrcnt32, M_TEMP);
1689 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1692 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1693 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1695 "Interrupt Counts");
1699 * DDB command to dump the interrupt statistics.
1701 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1709 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1714 db_printf("%s\t%lu\n", cp, *i);
1715 cp += strlen(cp) + 1;