2 * SPDX-License-Identifier: BSD-2-Clause
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 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)");
104 static int intr_hwpmc_waiting_report_threshold = 1;
105 SYSCTL_INT(_hw, OID_AUTO, intr_hwpmc_waiting_report_threshold, CTLFLAG_RWTUN,
106 &intr_hwpmc_waiting_report_threshold, 1,
107 "Threshold for reporting number of events in a workq");
108 #define PMC_HOOK_INSTALLED_ANY() __predict_false(pmc_hook != NULL)
110 static TAILQ_HEAD(, intr_event) event_list =
111 TAILQ_HEAD_INITIALIZER(event_list);
112 static struct mtx event_lock;
113 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
115 static void intr_event_update(struct intr_event *ie);
116 static int intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame);
117 static struct intr_thread *ithread_create(const char *name);
118 static void ithread_destroy(struct intr_thread *ithread);
119 static void ithread_execute_handlers(struct proc *p,
120 struct intr_event *ie);
121 static void ithread_loop(void *);
122 static void ithread_update(struct intr_thread *ithd);
123 static void start_softintr(void *);
126 #include <sys/pmckern.h>
127 PMC_SOFT_DEFINE( , , intr, all);
128 PMC_SOFT_DEFINE( , , intr, ithread);
129 PMC_SOFT_DEFINE( , , intr, filter);
130 PMC_SOFT_DEFINE( , , intr, stray);
131 PMC_SOFT_DEFINE( , , intr, schedule);
132 PMC_SOFT_DEFINE( , , intr, waiting);
134 #define PMC_SOFT_CALL_INTR_HLPR(event, frame) \
137 PMC_SOFT_CALL_TF( , , intr, event, frame); \
139 PMC_SOFT_CALL( , , intr, event); \
143 /* Map an interrupt type to an ithread priority. */
145 intr_priority(enum intr_type flags)
149 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
150 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
171 pri = PI_DULL; /* don't care */
174 /* We didn't specify an interrupt level. */
175 panic("intr_priority: no interrupt type in flags");
182 * Update an ithread based on the associated intr_event.
185 ithread_update(struct intr_thread *ithd)
187 struct intr_event *ie;
192 td = ithd->it_thread;
193 mtx_assert(&ie->ie_lock, MA_OWNED);
195 /* Determine the overall priority of this event. */
196 if (CK_SLIST_EMPTY(&ie->ie_handlers))
199 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
201 /* Update name and priority. */
202 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
204 sched_clear_tdname(td);
207 sched_ithread_prio(td, pri);
212 * Regenerate the full name of an interrupt event and update its priority.
215 intr_event_update(struct intr_event *ie)
217 struct intr_handler *ih;
219 int missed, space, flags;
221 /* Start off with no entropy and just the name of the event. */
222 mtx_assert(&ie->ie_lock, MA_OWNED);
223 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
228 /* Run through all the handlers updating values. */
229 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
230 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
231 sizeof(ie->ie_fullname)) {
232 strcat(ie->ie_fullname, " ");
233 strcat(ie->ie_fullname, ih->ih_name);
237 flags |= ih->ih_flags;
239 ie->ie_hflags = flags;
242 * If there is only one handler and its name is too long, just copy in
243 * as much of the end of the name (includes the unit number) as will
244 * fit. Otherwise, we have multiple handlers and not all of the names
245 * will fit. Add +'s to indicate missing names. If we run out of room
246 * and still have +'s to add, change the last character from a + to a *.
248 if (missed == 1 && space == 1) {
249 ih = CK_SLIST_FIRST(&ie->ie_handlers);
250 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
251 sizeof(ie->ie_fullname);
252 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
253 strcat(ie->ie_fullname, &ih->ih_name[missed]);
256 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
257 while (missed-- > 0) {
258 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
265 strcat(ie->ie_fullname, " +");
268 strcat(ie->ie_fullname, "+");
272 * If this event has an ithread, update it's priority and
275 if (ie->ie_thread != NULL)
276 ithread_update(ie->ie_thread);
277 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
281 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
282 void (*pre_ithread)(void *), void (*post_ithread)(void *),
283 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
284 const char *fmt, ...)
286 struct intr_event *ie;
289 /* The only valid flag during creation is IE_SOFT. */
290 if ((flags & ~IE_SOFT) != 0)
292 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
293 ie->ie_source = source;
294 ie->ie_pre_ithread = pre_ithread;
295 ie->ie_post_ithread = post_ithread;
296 ie->ie_post_filter = post_filter;
297 ie->ie_assign_cpu = assign_cpu;
298 ie->ie_flags = flags;
301 CK_SLIST_INIT(&ie->ie_handlers);
302 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
305 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
307 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
308 mtx_lock(&event_lock);
309 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
310 mtx_unlock(&event_lock);
313 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
318 * Bind an interrupt event to the specified CPU. Note that not all
319 * platforms support binding an interrupt to a CPU. For those
320 * platforms this request will fail. Using a cpu id of NOCPU unbinds
321 * the interrupt event.
324 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
329 /* Need a CPU to bind to. */
330 if (cpu != NOCPU && CPU_ABSENT(cpu))
333 if (ie->ie_assign_cpu == NULL)
336 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
341 * If we have any ithreads try to set their mask first to verify
345 mtx_lock(&ie->ie_lock);
346 if (ie->ie_thread != NULL) {
347 id = ie->ie_thread->it_thread->td_tid;
348 mtx_unlock(&ie->ie_lock);
349 error = cpuset_setithread(id, cpu);
353 mtx_unlock(&ie->ie_lock);
356 error = ie->ie_assign_cpu(ie->ie_source, cpu);
359 mtx_lock(&ie->ie_lock);
360 if (ie->ie_thread != NULL) {
362 id = ie->ie_thread->it_thread->td_tid;
363 mtx_unlock(&ie->ie_lock);
364 (void)cpuset_setithread(id, cpu);
366 mtx_unlock(&ie->ie_lock);
372 mtx_lock(&ie->ie_lock);
374 mtx_unlock(&ie->ie_lock);
381 * Bind an interrupt event to the specified CPU. For supported platforms, any
382 * associated ithreads as well as the primary interrupt context will be bound
383 * to the specificed CPU.
386 intr_event_bind(struct intr_event *ie, int cpu)
389 return (_intr_event_bind(ie, cpu, true, true));
393 * Bind an interrupt event to the specified CPU, but do not bind associated
397 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
400 return (_intr_event_bind(ie, cpu, true, false));
404 * Bind an interrupt event's ithread to the specified CPU.
407 intr_event_bind_ithread(struct intr_event *ie, int cpu)
410 return (_intr_event_bind(ie, cpu, false, true));
414 * Bind an interrupt event's ithread to the specified cpuset.
417 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
421 mtx_lock(&ie->ie_lock);
422 if (ie->ie_thread != NULL) {
423 id = ie->ie_thread->it_thread->td_tid;
424 mtx_unlock(&ie->ie_lock);
425 return (cpuset_setthread(id, cs));
427 mtx_unlock(&ie->ie_lock);
432 static struct intr_event *
435 struct intr_event *ie;
437 mtx_lock(&event_lock);
438 TAILQ_FOREACH(ie, &event_list, ie_list)
439 if (ie->ie_irq == irq &&
440 (ie->ie_flags & IE_SOFT) == 0 &&
441 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
443 mtx_unlock(&event_lock);
448 intr_setaffinity(int irq, int mode, void *m)
450 struct intr_event *ie;
457 * If we're setting all cpus we can unbind. Otherwise make sure
458 * only one cpu is in the set.
460 if (CPU_CMP(cpuset_root, mask)) {
461 for (n = 0; n < CPU_SETSIZE; n++) {
462 if (!CPU_ISSET(n, mask))
469 ie = intr_lookup(irq);
474 return (intr_event_bind(ie, cpu));
475 case CPU_WHICH_INTRHANDLER:
476 return (intr_event_bind_irqonly(ie, cpu));
477 case CPU_WHICH_ITHREAD:
478 return (intr_event_bind_ithread(ie, cpu));
485 intr_getaffinity(int irq, int mode, void *m)
487 struct intr_event *ie;
495 ie = intr_lookup(irq);
503 case CPU_WHICH_INTRHANDLER:
504 mtx_lock(&ie->ie_lock);
505 if (ie->ie_cpu == NOCPU)
506 CPU_COPY(cpuset_root, mask);
508 CPU_SET(ie->ie_cpu, mask);
509 mtx_unlock(&ie->ie_lock);
511 case CPU_WHICH_ITHREAD:
512 mtx_lock(&ie->ie_lock);
513 if (ie->ie_thread == NULL) {
514 mtx_unlock(&ie->ie_lock);
515 CPU_COPY(cpuset_root, mask);
517 id = ie->ie_thread->it_thread->td_tid;
518 mtx_unlock(&ie->ie_lock);
519 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
522 CPU_COPY(&td->td_cpuset->cs_mask, mask);
532 intr_event_destroy(struct intr_event *ie)
538 mtx_lock(&event_lock);
539 mtx_lock(&ie->ie_lock);
540 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
541 mtx_unlock(&ie->ie_lock);
542 mtx_unlock(&event_lock);
545 TAILQ_REMOVE(&event_list, ie, ie_list);
547 if (ie->ie_thread != NULL) {
548 ithread_destroy(ie->ie_thread);
549 ie->ie_thread = NULL;
552 mtx_unlock(&ie->ie_lock);
553 mtx_unlock(&event_lock);
554 mtx_destroy(&ie->ie_lock);
559 static struct intr_thread *
560 ithread_create(const char *name)
562 struct intr_thread *ithd;
566 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
568 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
569 &td, RFSTOPPED | RFHIGHPID,
570 0, "intr", "%s", name);
572 panic("kproc_create() failed with %d", error);
574 sched_class(td, PRI_ITHD);
577 td->td_pflags |= TDP_ITHREAD;
578 ithd->it_thread = td;
579 CTR2(KTR_INTR, "%s: created %s", __func__, name);
584 ithread_destroy(struct intr_thread *ithread)
588 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
589 td = ithread->it_thread;
591 ithread->it_flags |= IT_DEAD;
592 if (TD_AWAITING_INTR(td)) {
594 sched_wakeup(td, SRQ_INTR);
600 intr_event_add_handler(struct intr_event *ie, const char *name,
601 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
602 enum intr_type flags, void **cookiep)
604 struct intr_handler *ih, *temp_ih;
605 struct intr_handler **prevptr;
606 struct intr_thread *it;
608 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
611 /* Allocate and populate an interrupt handler structure. */
612 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
613 ih->ih_filter = filter;
614 ih->ih_handler = handler;
615 ih->ih_argument = arg;
616 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
619 if (flags & INTR_EXCL)
620 ih->ih_flags = IH_EXCLUSIVE;
621 if (flags & INTR_MPSAFE)
622 ih->ih_flags |= IH_MPSAFE;
623 if (flags & INTR_ENTROPY)
624 ih->ih_flags |= IH_ENTROPY;
625 if (flags & INTR_TYPE_NET)
626 ih->ih_flags |= IH_NET;
628 /* We can only have one exclusive handler in a event. */
629 mtx_lock(&ie->ie_lock);
630 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
631 if ((flags & INTR_EXCL) ||
632 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
633 mtx_unlock(&ie->ie_lock);
639 /* Create a thread if we need one. */
640 while (ie->ie_thread == NULL && handler != NULL) {
641 if (ie->ie_flags & IE_ADDING_THREAD)
642 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
644 ie->ie_flags |= IE_ADDING_THREAD;
645 mtx_unlock(&ie->ie_lock);
646 it = ithread_create("intr: newborn");
647 mtx_lock(&ie->ie_lock);
648 ie->ie_flags &= ~IE_ADDING_THREAD;
656 /* Add the new handler to the event in priority order. */
657 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
658 if (temp_ih->ih_pri > ih->ih_pri)
661 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
663 intr_event_update(ie);
665 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
667 mtx_unlock(&ie->ie_lock);
675 * Append a description preceded by a ':' to the name of the specified
679 intr_event_describe_handler(struct intr_event *ie, void *cookie,
682 struct intr_handler *ih;
686 mtx_lock(&ie->ie_lock);
688 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
693 mtx_unlock(&ie->ie_lock);
694 panic("handler %p not found in interrupt event %p", cookie, ie);
700 * Look for an existing description by checking for an
701 * existing ":". This assumes device names do not include
702 * colons. If one is found, prepare to insert the new
703 * description at that point. If one is not found, find the
704 * end of the name to use as the insertion point.
706 start = strchr(ih->ih_name, ':');
708 start = strchr(ih->ih_name, 0);
711 * See if there is enough remaining room in the string for the
712 * description + ":". The "- 1" leaves room for the trailing
713 * '\0'. The "+ 1" accounts for the colon.
715 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
716 if (strlen(descr) + 1 > space) {
717 mtx_unlock(&ie->ie_lock);
721 /* Append a colon followed by the description. */
723 strcpy(start + 1, descr);
724 intr_event_update(ie);
725 mtx_unlock(&ie->ie_lock);
730 * Return the ie_source field from the intr_event an intr_handler is
734 intr_handler_source(void *cookie)
736 struct intr_handler *ih;
737 struct intr_event *ie;
739 ih = (struct intr_handler *)cookie;
744 ("interrupt handler \"%s\" has a NULL interrupt event",
746 return (ie->ie_source);
750 * If intr_event_handle() is running in the ISR context at the time of the call,
751 * then wait for it to complete.
754 intr_event_barrier(struct intr_event *ie)
758 mtx_assert(&ie->ie_lock, MA_OWNED);
759 phase = ie->ie_phase;
762 * Switch phase to direct future interrupts to the other active counter.
763 * Make sure that any preceding stores are visible before the switch.
765 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
766 atomic_store_rel_int(&ie->ie_phase, !phase);
769 * This code cooperates with wait-free iteration of ie_handlers
770 * in intr_event_handle.
771 * Make sure that the removal and the phase update are not reordered
772 * with the active count check.
773 * Note that no combination of acquire and release fences can provide
774 * that guarantee as Store->Load sequences can always be reordered.
776 atomic_thread_fence_seq_cst();
779 * Now wait on the inactive phase.
780 * The acquire fence is needed so that all post-barrier accesses
781 * are after the check.
783 while (ie->ie_active[phase] > 0)
785 atomic_thread_fence_acq();
789 intr_handler_barrier(struct intr_handler *handler)
791 struct intr_event *ie;
793 ie = handler->ih_event;
794 mtx_assert(&ie->ie_lock, MA_OWNED);
795 KASSERT((handler->ih_flags & IH_DEAD) == 0,
796 ("update for a removed handler"));
798 if (ie->ie_thread == NULL) {
799 intr_event_barrier(ie);
802 if ((handler->ih_flags & IH_CHANGED) == 0) {
803 handler->ih_flags |= IH_CHANGED;
804 intr_event_schedule_thread(ie, NULL);
806 while ((handler->ih_flags & IH_CHANGED) != 0)
807 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
811 * Sleep until an ithread finishes executing an interrupt handler.
813 * XXX Doesn't currently handle interrupt filters or fast interrupt
814 * handlers. This is intended for LinuxKPI drivers only.
815 * Do not use in BSD code.
820 struct intr_event *ie;
821 struct intr_thread *ithd;
824 ie = intr_lookup(irq);
827 if (ie->ie_thread == NULL)
829 ithd = ie->ie_thread;
830 td = ithd->it_thread;
832 * We set the flag and wait for it to be cleared to avoid
833 * long delays with potentially busy interrupt handlers
834 * were we to only sample TD_AWAITING_INTR() every tick.
837 if (!TD_AWAITING_INTR(td)) {
838 ithd->it_flags |= IT_WAIT;
839 while (ithd->it_flags & IT_WAIT) {
850 intr_event_remove_handler(void *cookie)
852 struct intr_handler *handler = (struct intr_handler *)cookie;
853 struct intr_event *ie;
854 struct intr_handler *ih;
855 struct intr_handler **prevptr;
862 ie = handler->ih_event;
864 ("interrupt handler \"%s\" has a NULL interrupt event",
867 mtx_lock(&ie->ie_lock);
868 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
870 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
875 panic("interrupt handler \"%s\" not found in "
876 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
880 * If there is no ithread, then directly remove the handler. Note that
881 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
882 * care needs to be taken to keep ie_handlers consistent and to free
883 * the removed handler only when ie_handlers is quiescent.
885 if (ie->ie_thread == NULL) {
886 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
887 intr_event_barrier(ie);
888 intr_event_update(ie);
889 mtx_unlock(&ie->ie_lock);
890 free(handler, M_ITHREAD);
895 * Let the interrupt thread do the job.
896 * The interrupt source is disabled when the interrupt thread is
897 * running, so it does not have to worry about interaction with
898 * intr_event_handle().
900 KASSERT((handler->ih_flags & IH_DEAD) == 0,
901 ("duplicate handle remove"));
902 handler->ih_flags |= IH_DEAD;
903 intr_event_schedule_thread(ie, NULL);
904 while (handler->ih_flags & IH_DEAD)
905 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
906 intr_event_update(ie);
910 * XXX: This could be bad in the case of ppbus(8). Also, I think
911 * this could lead to races of stale data when servicing an
915 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
916 if (ih->ih_handler != NULL) {
922 ithread_destroy(ie->ie_thread);
923 ie->ie_thread = NULL;
926 mtx_unlock(&ie->ie_lock);
927 free(handler, M_ITHREAD);
932 intr_event_suspend_handler(void *cookie)
934 struct intr_handler *handler = (struct intr_handler *)cookie;
935 struct intr_event *ie;
939 ie = handler->ih_event;
941 ("interrupt handler \"%s\" has a NULL interrupt event",
943 mtx_lock(&ie->ie_lock);
944 handler->ih_flags |= IH_SUSP;
945 intr_handler_barrier(handler);
946 mtx_unlock(&ie->ie_lock);
951 intr_event_resume_handler(void *cookie)
953 struct intr_handler *handler = (struct intr_handler *)cookie;
954 struct intr_event *ie;
958 ie = handler->ih_event;
960 ("interrupt handler \"%s\" has a NULL interrupt event",
964 * intr_handler_barrier() acts not only as a barrier,
965 * it also allows to check for any pending interrupts.
967 mtx_lock(&ie->ie_lock);
968 handler->ih_flags &= ~IH_SUSP;
969 intr_handler_barrier(handler);
970 mtx_unlock(&ie->ie_lock);
975 intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame)
977 struct intr_entropy entropy;
978 struct intr_thread *it;
983 * If no ithread or no handlers, then we have a stray interrupt.
985 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
986 ie->ie_thread == NULL)
994 * If any of the handlers for this ithread claim to be good
995 * sources of entropy, then gather some.
997 if (ie->ie_hflags & IH_ENTROPY) {
998 entropy.event = (uintptr_t)ie;
1000 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
1003 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
1006 * Set it_need to tell the thread to keep running if it is already
1007 * running. Then, lock the thread and see if we actually need to
1008 * put it on the runqueue.
1010 * Use store_rel to arrange that the store to ih_need in
1011 * swi_sched() is before the store to it_need and prepare for
1012 * transfer of this order to loads in the ithread.
1014 atomic_store_rel_int(&it->it_need, 1);
1016 if (TD_AWAITING_INTR(td)) {
1019 if (PMC_HOOK_INSTALLED_ANY())
1020 PMC_SOFT_CALL_INTR_HLPR(schedule, frame);
1022 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
1025 sched_wakeup(td, SRQ_INTR);
1029 if (PMC_HOOK_INSTALLED_ANY() &&
1030 (it->it_waiting >= intr_hwpmc_waiting_report_threshold))
1031 PMC_SOFT_CALL_INTR_HLPR(waiting, frame);
1033 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1034 __func__, td->td_proc->p_pid, td->td_name, it->it_need, TD_GET_STATE(td));
1042 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1043 * since interrupts are generated in software rather than being directed by
1047 swi_assign_cpu(void *arg, int cpu)
1054 * Add a software interrupt handler to a specified event. If a given event
1055 * is not specified, then a new event is created.
1058 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1059 void *arg, int pri, enum intr_type flags, void **cookiep)
1061 struct intr_event *ie;
1064 if (flags & INTR_ENTROPY)
1067 ie = (eventp != NULL) ? *eventp : NULL;
1070 if (!(ie->ie_flags & IE_SOFT))
1073 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1074 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1080 if (handler != NULL) {
1081 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1082 PI_SWI(pri), flags, cookiep);
1088 * Schedule a software interrupt thread.
1091 swi_sched(void *cookie, int flags)
1093 struct intr_handler *ih = (struct intr_handler *)cookie;
1094 struct intr_event *ie = ih->ih_event;
1095 struct intr_entropy entropy;
1098 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1101 if ((flags & SWI_FROMNMI) == 0) {
1102 entropy.event = (uintptr_t)ih;
1103 entropy.td = curthread;
1104 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1108 * Set ih_need for this handler so that if the ithread is already
1109 * running it will execute this handler on the next pass. Otherwise,
1110 * it will execute it the next time it runs.
1114 if (flags & SWI_DELAY)
1117 if (flags & SWI_FROMNMI) {
1118 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1119 KASSERT(ie == clk_intr_event,
1120 ("SWI_FROMNMI used not with clk_intr_event"));
1121 ipi_self_from_nmi(IPI_SWI);
1125 error = intr_event_schedule_thread(ie, NULL);
1126 KASSERT(error == 0, ("stray software interrupt"));
1131 * Remove a software interrupt handler. Currently this code does not
1132 * remove the associated interrupt event if it becomes empty. Calling code
1133 * may do so manually via intr_event_destroy(), but that's not really
1134 * an optimal interface.
1137 swi_remove(void *cookie)
1140 return (intr_event_remove_handler(cookie));
1144 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1146 struct intr_handler *ih, *ihn, *ihp;
1149 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1151 * If this handler is marked for death, remove it from
1152 * the list of handlers and wake up the sleeper.
1154 if (ih->ih_flags & IH_DEAD) {
1155 mtx_lock(&ie->ie_lock);
1157 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1159 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1160 ih->ih_flags &= ~IH_DEAD;
1162 mtx_unlock(&ie->ie_lock);
1167 * Now that we know that the current element won't be removed
1168 * update the previous element.
1172 if ((ih->ih_flags & IH_CHANGED) != 0) {
1173 mtx_lock(&ie->ie_lock);
1174 ih->ih_flags &= ~IH_CHANGED;
1176 mtx_unlock(&ie->ie_lock);
1179 /* Skip filter only handlers */
1180 if (ih->ih_handler == NULL)
1183 /* Skip suspended handlers */
1184 if ((ih->ih_flags & IH_SUSP) != 0)
1188 * For software interrupt threads, we only execute
1189 * handlers that have their need flag set. Hardware
1190 * interrupt threads always invoke all of their handlers.
1192 * ih_need can only be 0 or 1. Failed cmpset below
1193 * means that there is no request to execute handlers,
1194 * so a retry of the cmpset is not needed.
1196 if ((ie->ie_flags & IE_SOFT) != 0 &&
1197 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1200 /* Execute this handler. */
1201 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1202 __func__, p->p_pid, (void *)ih->ih_handler,
1203 ih->ih_argument, ih->ih_name, ih->ih_flags);
1205 if (!(ih->ih_flags & IH_MPSAFE))
1207 ih->ih_handler(ih->ih_argument);
1208 if (!(ih->ih_flags & IH_MPSAFE))
1214 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1217 /* Interrupt handlers should not sleep. */
1218 if (!(ie->ie_flags & IE_SOFT))
1219 THREAD_NO_SLEEPING();
1220 intr_event_execute_handlers(p, ie);
1221 if (!(ie->ie_flags & IE_SOFT))
1222 THREAD_SLEEPING_OK();
1225 * Interrupt storm handling:
1227 * If this interrupt source is currently storming, then throttle
1228 * it to only fire the handler once per clock tick.
1230 * If this interrupt source is not currently storming, but the
1231 * number of back to back interrupts exceeds the storm threshold,
1232 * then enter storming mode.
1234 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1235 !(ie->ie_flags & IE_SOFT)) {
1236 /* Report the message only once every second. */
1237 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1239 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1247 * Now that all the handlers have had a chance to run, reenable
1248 * the interrupt source.
1250 if (ie->ie_post_ithread != NULL)
1251 ie->ie_post_ithread(ie->ie_source);
1255 * This is the main code for interrupt threads.
1258 ithread_loop(void *arg)
1260 struct epoch_tracker et;
1261 struct intr_thread *ithd;
1262 struct intr_event *ie;
1265 int wake, epoch_count;
1270 ithd = (struct intr_thread *)arg;
1271 KASSERT(ithd->it_thread == td,
1272 ("%s: ithread and proc linkage out of sync", __func__));
1273 ie = ithd->it_event;
1278 * As long as we have interrupts outstanding, go through the
1279 * list of handlers, giving each one a go at it.
1283 * If we are an orphaned thread, then just die.
1285 if (ithd->it_flags & IT_DEAD) {
1286 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1287 p->p_pid, td->td_name);
1288 free(ithd, M_ITHREAD);
1293 * Service interrupts. If another interrupt arrives while
1294 * we are running, it will set it_need to note that we
1295 * should make another pass.
1297 * The load_acq part of the following cmpset ensures
1298 * that the load of ih_need in ithread_execute_handlers()
1299 * is ordered after the load of it_need here.
1302 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1305 NET_EPOCH_ENTER(et);
1307 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1308 ithread_execute_handlers(p, ie);
1310 ++epoch_count >= intr_epoch_batch) {
1313 NET_EPOCH_ENTER(et);
1318 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1319 mtx_assert(&Giant, MA_NOTOWNED);
1322 * Processed all our interrupts. Now get the sched
1323 * lock. This may take a while and it_need may get
1324 * set again, so we have to check it again.
1327 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1328 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1331 mi_switch(SW_VOL | SWT_IWAIT);
1333 if (ithd->it_flags & IT_WAIT) {
1335 ithd->it_flags &= ~IT_WAIT;
1347 * Main interrupt handling body.
1350 * o ie: the event connected to this interrupt.
1351 * o frame: some archs (i.e. i386) pass a frame to some.
1352 * handlers as their main argument.
1354 * o 0: everything ok.
1355 * o EINVAL: stray interrupt.
1358 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1360 struct intr_handler *ih;
1361 struct trapframe *oldframe;
1365 bool filter, thread;
1369 #ifdef KSTACK_USAGE_PROF
1370 intr_prof_stack_use(td, frame);
1373 /* An interrupt with no event or handlers is a stray interrupt. */
1374 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1378 * Execute fast interrupt handlers directly.
1379 * To support clock handlers, if a handler registers
1380 * with a NULL argument, then we pass it a pointer to
1381 * a trapframe as its argument.
1383 td->td_intr_nesting_level++;
1388 oldframe = td->td_intr_frame;
1389 td->td_intr_frame = frame;
1391 phase = ie->ie_phase;
1392 atomic_add_int(&ie->ie_active[phase], 1);
1395 * This fence is required to ensure that no later loads are
1396 * re-ordered before the ie_active store.
1398 atomic_thread_fence_seq_cst();
1400 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1401 if ((ih->ih_flags & IH_SUSP) != 0)
1403 if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1405 if (ih->ih_filter == NULL) {
1409 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1410 ih->ih_filter, ih->ih_argument == NULL ? frame :
1411 ih->ih_argument, ih->ih_name);
1412 if (ih->ih_argument == NULL)
1413 ret = ih->ih_filter(frame);
1415 ret = ih->ih_filter(ih->ih_argument);
1417 PMC_SOFT_CALL_TF( , , intr, all, frame);
1419 KASSERT(ret == FILTER_STRAY ||
1420 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1421 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1422 ("%s: incorrect return value %#x from %s", __func__, ret,
1424 filter = filter || ret == FILTER_HANDLED;
1426 if (ret & FILTER_SCHEDULE_THREAD)
1427 PMC_SOFT_CALL_TF( , , intr, ithread, frame);
1428 else if (ret & FILTER_HANDLED)
1429 PMC_SOFT_CALL_TF( , , intr, filter, frame);
1430 else if (ret == FILTER_STRAY)
1431 PMC_SOFT_CALL_TF( , , intr, stray, frame);
1435 * Wrapper handler special handling:
1437 * in some particular cases (like pccard and pccbb),
1438 * the _real_ device handler is wrapped in a couple of
1439 * functions - a filter wrapper and an ithread wrapper.
1440 * In this case (and just in this case), the filter wrapper
1441 * could ask the system to schedule the ithread and mask
1442 * the interrupt source if the wrapped handler is composed
1443 * of just an ithread handler.
1445 * TODO: write a generic wrapper to avoid people rolling
1449 if (ret == FILTER_SCHEDULE_THREAD)
1453 atomic_add_rel_int(&ie->ie_active[phase], -1);
1455 td->td_intr_frame = oldframe;
1458 if (ie->ie_pre_ithread != NULL)
1459 ie->ie_pre_ithread(ie->ie_source);
1461 if (ie->ie_post_filter != NULL)
1462 ie->ie_post_filter(ie->ie_source);
1465 /* Schedule the ithread if needed. */
1469 error = intr_event_schedule_thread(ie, frame);
1470 KASSERT(error == 0, ("bad stray interrupt"));
1473 td->td_intr_nesting_level--;
1475 /* The interrupt is not aknowledged by any filter and has no ithread. */
1476 if (!thread && !filter)
1484 * Dump details about an interrupt handler
1487 db_dump_intrhand(struct intr_handler *ih)
1491 db_printf("\t%-10s ", ih->ih_name);
1492 switch (ih->ih_pri) {
1500 if (ih->ih_pri >= PI_SOFT)
1503 db_printf("%4u", ih->ih_pri);
1507 if (ih->ih_filter != NULL) {
1509 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1511 if (ih->ih_handler != NULL) {
1512 if (ih->ih_filter != NULL)
1515 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1517 db_printf("(%p)", ih->ih_argument);
1519 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1523 if (ih->ih_flags & IH_EXCLUSIVE) {
1529 if (ih->ih_flags & IH_ENTROPY) {
1532 db_printf("ENTROPY");
1535 if (ih->ih_flags & IH_DEAD) {
1541 if (ih->ih_flags & IH_MPSAFE) {
1544 db_printf("MPSAFE");
1558 * Dump details about a event.
1561 db_dump_intr_event(struct intr_event *ie, int handlers)
1563 struct intr_handler *ih;
1564 struct intr_thread *it;
1567 db_printf("%s ", ie->ie_fullname);
1570 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1572 db_printf("(no thread)");
1573 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1574 (it != NULL && it->it_need)) {
1577 if (ie->ie_flags & IE_SOFT) {
1581 if (ie->ie_flags & IE_ADDING_THREAD) {
1584 db_printf("ADDING_THREAD");
1587 if (it != NULL && it->it_need) {
1597 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1598 db_dump_intrhand(ih);
1602 * Dump data about interrupt handlers
1604 DB_SHOW_COMMAND_FLAGS(intr, db_show_intr, DB_CMD_MEMSAFE)
1606 struct intr_event *ie;
1609 verbose = strchr(modif, 'v') != NULL;
1610 all = strchr(modif, 'a') != NULL;
1611 TAILQ_FOREACH(ie, &event_list, ie_list) {
1612 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1614 db_dump_intr_event(ie, verbose);
1622 * Start standard software interrupt threads
1625 start_softintr(void *dummy)
1628 if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1630 panic("died while creating clk swi ithread");
1632 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1636 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1637 * The data for this machine dependent, and the declarations are in machine
1638 * dependent code. The layout of intrnames and intrcnt however is machine
1641 * We do not know the length of intrcnt and intrnames at compile time, so
1642 * calculate things at run time.
1645 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1647 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1650 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1651 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1652 sysctl_intrnames, "",
1656 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1659 uint32_t *intrcnt32;
1663 if (req->flags & SCTL_MASK32) {
1665 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1666 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1667 if (intrcnt32 == NULL)
1669 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1670 intrcnt32[i] = intrcnt[i];
1671 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1672 free(intrcnt32, M_TEMP);
1676 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1679 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1680 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1682 "Interrupt Counts");
1686 * DDB command to dump the interrupt statistics.
1688 DB_SHOW_COMMAND_FLAGS(intrcnt, db_show_intrcnt, DB_CMD_MEMSAFE)
1696 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1701 db_printf("%s\t%lu\n", cp, *i);
1702 cp += strlen(cp) + 1;