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>
31 #include "opt_kstack_usage_prof.h"
33 #include <sys/param.h>
36 #include <sys/cpuset.h>
37 #include <sys/rtprio.h>
38 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
43 #include <sys/limits.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
49 #include <sys/epoch.h>
50 #include <sys/random.h>
51 #include <sys/resourcevar.h>
52 #include <sys/sched.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 #include <sys/unistd.h>
57 #include <sys/vmmeter.h>
58 #include <machine/atomic.h>
59 #include <machine/cpu.h>
60 #include <machine/md_var.h>
61 #include <machine/smp.h>
62 #include <machine/stdarg.h>
65 #include <ddb/db_sym.h>
69 * Describe an interrupt thread. There is one of these per interrupt event.
72 struct intr_event *it_event;
73 struct thread *it_thread; /* Kernel thread. */
74 int it_flags; /* (j) IT_* flags. */
75 int it_need; /* Needs service. */
78 /* Interrupt thread flags kept in it_flags */
79 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
80 #define IT_WAIT 0x000002 /* Thread is waiting for completion. */
87 struct intr_event *clk_intr_event;
88 struct proc *intrproc;
90 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
92 static int intr_storm_threshold = 0;
93 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
94 &intr_storm_threshold, 0,
95 "Number of consecutive interrupts before storm protection is enabled");
96 static int intr_epoch_batch = 1000;
97 SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch,
98 0, "Maximum interrupt handler executions without re-entering epoch(9)");
99 static TAILQ_HEAD(, intr_event) event_list =
100 TAILQ_HEAD_INITIALIZER(event_list);
101 static struct mtx event_lock;
102 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
104 static void intr_event_update(struct intr_event *ie);
105 static int intr_event_schedule_thread(struct intr_event *ie);
106 static struct intr_thread *ithread_create(const char *name);
107 static void ithread_destroy(struct intr_thread *ithread);
108 static void ithread_execute_handlers(struct proc *p,
109 struct intr_event *ie);
110 static void ithread_loop(void *);
111 static void ithread_update(struct intr_thread *ithd);
112 static void start_softintr(void *);
114 /* Map an interrupt type to an ithread priority. */
116 intr_priority(enum intr_type flags)
120 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
121 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
142 pri = PI_DULL; /* don't care */
145 /* We didn't specify an interrupt level. */
146 panic("intr_priority: no interrupt type in flags");
153 * Update an ithread based on the associated intr_event.
156 ithread_update(struct intr_thread *ithd)
158 struct intr_event *ie;
163 td = ithd->it_thread;
164 mtx_assert(&ie->ie_lock, MA_OWNED);
166 /* Determine the overall priority of this event. */
167 if (CK_SLIST_EMPTY(&ie->ie_handlers))
170 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
172 /* Update name and priority. */
173 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
175 sched_clear_tdname(td);
183 * Regenerate the full name of an interrupt event and update its priority.
186 intr_event_update(struct intr_event *ie)
188 struct intr_handler *ih;
190 int missed, space, flags;
192 /* Start off with no entropy and just the name of the event. */
193 mtx_assert(&ie->ie_lock, MA_OWNED);
194 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
199 /* Run through all the handlers updating values. */
200 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
201 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
202 sizeof(ie->ie_fullname)) {
203 strcat(ie->ie_fullname, " ");
204 strcat(ie->ie_fullname, ih->ih_name);
208 flags |= ih->ih_flags;
210 ie->ie_hflags = flags;
213 * If there is only one handler and its name is too long, just copy in
214 * as much of the end of the name (includes the unit number) as will
215 * fit. Otherwise, we have multiple handlers and not all of the names
216 * will fit. Add +'s to indicate missing names. If we run out of room
217 * and still have +'s to add, change the last character from a + to a *.
219 if (missed == 1 && space == 1) {
220 ih = CK_SLIST_FIRST(&ie->ie_handlers);
221 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
222 sizeof(ie->ie_fullname);
223 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
224 strcat(ie->ie_fullname, &ih->ih_name[missed]);
227 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
228 while (missed-- > 0) {
229 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
236 strcat(ie->ie_fullname, " +");
239 strcat(ie->ie_fullname, "+");
243 * If this event has an ithread, update it's priority and
246 if (ie->ie_thread != NULL)
247 ithread_update(ie->ie_thread);
248 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
252 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
253 void (*pre_ithread)(void *), void (*post_ithread)(void *),
254 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
255 const char *fmt, ...)
257 struct intr_event *ie;
260 /* The only valid flag during creation is IE_SOFT. */
261 if ((flags & ~IE_SOFT) != 0)
263 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
264 ie->ie_source = source;
265 ie->ie_pre_ithread = pre_ithread;
266 ie->ie_post_ithread = post_ithread;
267 ie->ie_post_filter = post_filter;
268 ie->ie_assign_cpu = assign_cpu;
269 ie->ie_flags = flags;
272 CK_SLIST_INIT(&ie->ie_handlers);
273 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
276 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
278 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
279 mtx_lock(&event_lock);
280 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
281 mtx_unlock(&event_lock);
284 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
289 * Bind an interrupt event to the specified CPU. Note that not all
290 * platforms support binding an interrupt to a CPU. For those
291 * platforms this request will fail. Using a cpu id of NOCPU unbinds
292 * the interrupt event.
295 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
300 /* Need a CPU to bind to. */
301 if (cpu != NOCPU && CPU_ABSENT(cpu))
304 if (ie->ie_assign_cpu == NULL)
307 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
312 * If we have any ithreads try to set their mask first to verify
316 mtx_lock(&ie->ie_lock);
317 if (ie->ie_thread != NULL) {
318 id = ie->ie_thread->it_thread->td_tid;
319 mtx_unlock(&ie->ie_lock);
320 error = cpuset_setithread(id, cpu);
324 mtx_unlock(&ie->ie_lock);
327 error = ie->ie_assign_cpu(ie->ie_source, cpu);
330 mtx_lock(&ie->ie_lock);
331 if (ie->ie_thread != NULL) {
333 id = ie->ie_thread->it_thread->td_tid;
334 mtx_unlock(&ie->ie_lock);
335 (void)cpuset_setithread(id, cpu);
337 mtx_unlock(&ie->ie_lock);
343 mtx_lock(&ie->ie_lock);
345 mtx_unlock(&ie->ie_lock);
352 * Bind an interrupt event to the specified CPU. For supported platforms, any
353 * associated ithreads as well as the primary interrupt context will be bound
354 * to the specificed CPU.
357 intr_event_bind(struct intr_event *ie, int cpu)
360 return (_intr_event_bind(ie, cpu, true, true));
364 * Bind an interrupt event to the specified CPU, but do not bind associated
368 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
371 return (_intr_event_bind(ie, cpu, true, false));
375 * Bind an interrupt event's ithread to the specified CPU.
378 intr_event_bind_ithread(struct intr_event *ie, int cpu)
381 return (_intr_event_bind(ie, cpu, false, true));
385 * Bind an interrupt event's ithread to the specified cpuset.
388 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
392 mtx_lock(&ie->ie_lock);
393 if (ie->ie_thread != NULL) {
394 id = ie->ie_thread->it_thread->td_tid;
395 mtx_unlock(&ie->ie_lock);
396 return (cpuset_setthread(id, cs));
398 mtx_unlock(&ie->ie_lock);
403 static struct intr_event *
406 struct intr_event *ie;
408 mtx_lock(&event_lock);
409 TAILQ_FOREACH(ie, &event_list, ie_list)
410 if (ie->ie_irq == irq &&
411 (ie->ie_flags & IE_SOFT) == 0 &&
412 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
414 mtx_unlock(&event_lock);
419 intr_setaffinity(int irq, int mode, void *m)
421 struct intr_event *ie;
428 * If we're setting all cpus we can unbind. Otherwise make sure
429 * only one cpu is in the set.
431 if (CPU_CMP(cpuset_root, mask)) {
432 for (n = 0; n < CPU_SETSIZE; n++) {
433 if (!CPU_ISSET(n, mask))
440 ie = intr_lookup(irq);
445 return (intr_event_bind(ie, cpu));
446 case CPU_WHICH_INTRHANDLER:
447 return (intr_event_bind_irqonly(ie, cpu));
448 case CPU_WHICH_ITHREAD:
449 return (intr_event_bind_ithread(ie, cpu));
456 intr_getaffinity(int irq, int mode, void *m)
458 struct intr_event *ie;
466 ie = intr_lookup(irq);
474 case CPU_WHICH_INTRHANDLER:
475 mtx_lock(&ie->ie_lock);
476 if (ie->ie_cpu == NOCPU)
477 CPU_COPY(cpuset_root, mask);
479 CPU_SET(ie->ie_cpu, mask);
480 mtx_unlock(&ie->ie_lock);
482 case CPU_WHICH_ITHREAD:
483 mtx_lock(&ie->ie_lock);
484 if (ie->ie_thread == NULL) {
485 mtx_unlock(&ie->ie_lock);
486 CPU_COPY(cpuset_root, mask);
488 id = ie->ie_thread->it_thread->td_tid;
489 mtx_unlock(&ie->ie_lock);
490 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
493 CPU_COPY(&td->td_cpuset->cs_mask, mask);
503 intr_event_destroy(struct intr_event *ie)
509 mtx_lock(&event_lock);
510 mtx_lock(&ie->ie_lock);
511 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
512 mtx_unlock(&ie->ie_lock);
513 mtx_unlock(&event_lock);
516 TAILQ_REMOVE(&event_list, ie, ie_list);
518 if (ie->ie_thread != NULL) {
519 ithread_destroy(ie->ie_thread);
520 ie->ie_thread = NULL;
523 mtx_unlock(&ie->ie_lock);
524 mtx_unlock(&event_lock);
525 mtx_destroy(&ie->ie_lock);
530 static struct intr_thread *
531 ithread_create(const char *name)
533 struct intr_thread *ithd;
537 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
539 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
540 &td, RFSTOPPED | RFHIGHPID,
541 0, "intr", "%s", name);
543 panic("kproc_create() failed with %d", error);
545 sched_class(td, PRI_ITHD);
548 td->td_pflags |= TDP_ITHREAD;
549 ithd->it_thread = td;
550 CTR2(KTR_INTR, "%s: created %s", __func__, name);
555 ithread_destroy(struct intr_thread *ithread)
559 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
560 td = ithread->it_thread;
562 ithread->it_flags |= IT_DEAD;
563 if (TD_AWAITING_INTR(td)) {
565 sched_add(td, SRQ_INTR);
571 intr_event_add_handler(struct intr_event *ie, const char *name,
572 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
573 enum intr_type flags, void **cookiep)
575 struct intr_handler *ih, *temp_ih;
576 struct intr_handler **prevptr;
577 struct intr_thread *it;
579 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
582 /* Allocate and populate an interrupt handler structure. */
583 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
584 ih->ih_filter = filter;
585 ih->ih_handler = handler;
586 ih->ih_argument = arg;
587 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
590 if (flags & INTR_EXCL)
591 ih->ih_flags = IH_EXCLUSIVE;
592 if (flags & INTR_MPSAFE)
593 ih->ih_flags |= IH_MPSAFE;
594 if (flags & INTR_ENTROPY)
595 ih->ih_flags |= IH_ENTROPY;
596 if (flags & INTR_TYPE_NET)
597 ih->ih_flags |= IH_NET;
599 /* We can only have one exclusive handler in a event. */
600 mtx_lock(&ie->ie_lock);
601 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
602 if ((flags & INTR_EXCL) ||
603 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
604 mtx_unlock(&ie->ie_lock);
610 /* Create a thread if we need one. */
611 while (ie->ie_thread == NULL && handler != NULL) {
612 if (ie->ie_flags & IE_ADDING_THREAD)
613 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
615 ie->ie_flags |= IE_ADDING_THREAD;
616 mtx_unlock(&ie->ie_lock);
617 it = ithread_create("intr: newborn");
618 mtx_lock(&ie->ie_lock);
619 ie->ie_flags &= ~IE_ADDING_THREAD;
627 /* Add the new handler to the event in priority order. */
628 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
629 if (temp_ih->ih_pri > ih->ih_pri)
632 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
634 intr_event_update(ie);
636 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
638 mtx_unlock(&ie->ie_lock);
646 * Append a description preceded by a ':' to the name of the specified
650 intr_event_describe_handler(struct intr_event *ie, void *cookie,
653 struct intr_handler *ih;
657 mtx_lock(&ie->ie_lock);
659 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
664 mtx_unlock(&ie->ie_lock);
665 panic("handler %p not found in interrupt event %p", cookie, ie);
671 * Look for an existing description by checking for an
672 * existing ":". This assumes device names do not include
673 * colons. If one is found, prepare to insert the new
674 * description at that point. If one is not found, find the
675 * end of the name to use as the insertion point.
677 start = strchr(ih->ih_name, ':');
679 start = strchr(ih->ih_name, 0);
682 * See if there is enough remaining room in the string for the
683 * description + ":". The "- 1" leaves room for the trailing
684 * '\0'. The "+ 1" accounts for the colon.
686 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
687 if (strlen(descr) + 1 > space) {
688 mtx_unlock(&ie->ie_lock);
692 /* Append a colon followed by the description. */
694 strcpy(start + 1, descr);
695 intr_event_update(ie);
696 mtx_unlock(&ie->ie_lock);
701 * Return the ie_source field from the intr_event an intr_handler is
705 intr_handler_source(void *cookie)
707 struct intr_handler *ih;
708 struct intr_event *ie;
710 ih = (struct intr_handler *)cookie;
715 ("interrupt handler \"%s\" has a NULL interrupt event",
717 return (ie->ie_source);
721 * If intr_event_handle() is running in the ISR context at the time of the call,
722 * then wait for it to complete.
725 intr_event_barrier(struct intr_event *ie)
729 mtx_assert(&ie->ie_lock, MA_OWNED);
730 phase = ie->ie_phase;
733 * Switch phase to direct future interrupts to the other active counter.
734 * Make sure that any preceding stores are visible before the switch.
736 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
737 atomic_store_rel_int(&ie->ie_phase, !phase);
740 * This code cooperates with wait-free iteration of ie_handlers
741 * in intr_event_handle.
742 * Make sure that the removal and the phase update are not reordered
743 * with the active count check.
744 * Note that no combination of acquire and release fences can provide
745 * that guarantee as Store->Load sequences can always be reordered.
747 atomic_thread_fence_seq_cst();
750 * Now wait on the inactive phase.
751 * The acquire fence is needed so that all post-barrier accesses
752 * are after the check.
754 while (ie->ie_active[phase] > 0)
756 atomic_thread_fence_acq();
760 intr_handler_barrier(struct intr_handler *handler)
762 struct intr_event *ie;
764 ie = handler->ih_event;
765 mtx_assert(&ie->ie_lock, MA_OWNED);
766 KASSERT((handler->ih_flags & IH_DEAD) == 0,
767 ("update for a removed handler"));
769 if (ie->ie_thread == NULL) {
770 intr_event_barrier(ie);
773 if ((handler->ih_flags & IH_CHANGED) == 0) {
774 handler->ih_flags |= IH_CHANGED;
775 intr_event_schedule_thread(ie);
777 while ((handler->ih_flags & IH_CHANGED) != 0)
778 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
782 * Sleep until an ithread finishes executing an interrupt handler.
784 * XXX Doesn't currently handle interrupt filters or fast interrupt
785 * handlers. This is intended for LinuxKPI drivers only.
786 * Do not use in BSD code.
791 struct intr_event *ie;
792 struct intr_thread *ithd;
795 ie = intr_lookup(irq);
798 if (ie->ie_thread == NULL)
800 ithd = ie->ie_thread;
801 td = ithd->it_thread;
803 * We set the flag and wait for it to be cleared to avoid
804 * long delays with potentially busy interrupt handlers
805 * were we to only sample TD_AWAITING_INTR() every tick.
808 if (!TD_AWAITING_INTR(td)) {
809 ithd->it_flags |= IT_WAIT;
810 while (ithd->it_flags & IT_WAIT) {
821 intr_event_remove_handler(void *cookie)
823 struct intr_handler *handler = (struct intr_handler *)cookie;
824 struct intr_event *ie;
825 struct intr_handler *ih;
826 struct intr_handler **prevptr;
833 ie = handler->ih_event;
835 ("interrupt handler \"%s\" has a NULL interrupt event",
838 mtx_lock(&ie->ie_lock);
839 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
841 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
846 panic("interrupt handler \"%s\" not found in "
847 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
851 * If there is no ithread, then directly remove the handler. Note that
852 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
853 * care needs to be taken to keep ie_handlers consistent and to free
854 * the removed handler only when ie_handlers is quiescent.
856 if (ie->ie_thread == NULL) {
857 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
858 intr_event_barrier(ie);
859 intr_event_update(ie);
860 mtx_unlock(&ie->ie_lock);
861 free(handler, M_ITHREAD);
866 * Let the interrupt thread do the job.
867 * The interrupt source is disabled when the interrupt thread is
868 * running, so it does not have to worry about interaction with
869 * intr_event_handle().
871 KASSERT((handler->ih_flags & IH_DEAD) == 0,
872 ("duplicate handle remove"));
873 handler->ih_flags |= IH_DEAD;
874 intr_event_schedule_thread(ie);
875 while (handler->ih_flags & IH_DEAD)
876 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
877 intr_event_update(ie);
881 * XXX: This could be bad in the case of ppbus(8). Also, I think
882 * this could lead to races of stale data when servicing an
886 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
887 if (ih->ih_handler != NULL) {
893 ithread_destroy(ie->ie_thread);
894 ie->ie_thread = NULL;
897 mtx_unlock(&ie->ie_lock);
898 free(handler, M_ITHREAD);
903 intr_event_suspend_handler(void *cookie)
905 struct intr_handler *handler = (struct intr_handler *)cookie;
906 struct intr_event *ie;
910 ie = handler->ih_event;
912 ("interrupt handler \"%s\" has a NULL interrupt event",
914 mtx_lock(&ie->ie_lock);
915 handler->ih_flags |= IH_SUSP;
916 intr_handler_barrier(handler);
917 mtx_unlock(&ie->ie_lock);
922 intr_event_resume_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",
935 * intr_handler_barrier() acts not only as a barrier,
936 * it also allows to check for any pending interrupts.
938 mtx_lock(&ie->ie_lock);
939 handler->ih_flags &= ~IH_SUSP;
940 intr_handler_barrier(handler);
941 mtx_unlock(&ie->ie_lock);
946 intr_event_schedule_thread(struct intr_event *ie)
948 struct intr_entropy entropy;
949 struct intr_thread *it;
954 * If no ithread or no handlers, then we have a stray interrupt.
956 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
957 ie->ie_thread == NULL)
965 * If any of the handlers for this ithread claim to be good
966 * sources of entropy, then gather some.
968 if (ie->ie_hflags & IH_ENTROPY) {
969 entropy.event = (uintptr_t)ie;
971 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
974 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
977 * Set it_need to tell the thread to keep running if it is already
978 * running. Then, lock the thread and see if we actually need to
979 * put it on the runqueue.
981 * Use store_rel to arrange that the store to ih_need in
982 * swi_sched() is before the store to it_need and prepare for
983 * transfer of this order to loads in the ithread.
985 atomic_store_rel_int(&it->it_need, 1);
987 if (TD_AWAITING_INTR(td)) {
988 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
991 sched_add(td, SRQ_INTR);
993 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
994 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
1002 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1003 * since interrupts are generated in software rather than being directed by
1007 swi_assign_cpu(void *arg, int cpu)
1014 * Add a software interrupt handler to a specified event. If a given event
1015 * is not specified, then a new event is created.
1018 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1019 void *arg, int pri, enum intr_type flags, void **cookiep)
1021 struct intr_event *ie;
1024 if (flags & INTR_ENTROPY)
1027 ie = (eventp != NULL) ? *eventp : NULL;
1030 if (!(ie->ie_flags & IE_SOFT))
1033 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1034 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1040 if (handler != NULL) {
1041 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1042 PI_SWI(pri), flags, cookiep);
1048 * Schedule a software interrupt thread.
1051 swi_sched(void *cookie, int flags)
1053 struct intr_handler *ih = (struct intr_handler *)cookie;
1054 struct intr_event *ie = ih->ih_event;
1055 struct intr_entropy entropy;
1058 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1061 if ((flags & SWI_FROMNMI) == 0) {
1062 entropy.event = (uintptr_t)ih;
1063 entropy.td = curthread;
1064 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1068 * Set ih_need for this handler so that if the ithread is already
1069 * running it will execute this handler on the next pass. Otherwise,
1070 * it will execute it the next time it runs.
1074 if (flags & SWI_DELAY)
1077 if (flags & SWI_FROMNMI) {
1078 #if defined(SMP) && (defined(__i386__) || defined(__amd64__))
1079 KASSERT(ie == clk_intr_event,
1080 ("SWI_FROMNMI used not with clk_intr_event"));
1081 ipi_self_from_nmi(IPI_SWI);
1085 error = intr_event_schedule_thread(ie);
1086 KASSERT(error == 0, ("stray software interrupt"));
1091 * Remove a software interrupt handler. Currently this code does not
1092 * remove the associated interrupt event if it becomes empty. Calling code
1093 * may do so manually via intr_event_destroy(), but that's not really
1094 * an optimal interface.
1097 swi_remove(void *cookie)
1100 return (intr_event_remove_handler(cookie));
1104 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1106 struct intr_handler *ih, *ihn, *ihp;
1109 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1111 * If this handler is marked for death, remove it from
1112 * the list of handlers and wake up the sleeper.
1114 if (ih->ih_flags & IH_DEAD) {
1115 mtx_lock(&ie->ie_lock);
1117 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1119 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1120 ih->ih_flags &= ~IH_DEAD;
1122 mtx_unlock(&ie->ie_lock);
1127 * Now that we know that the current element won't be removed
1128 * update the previous element.
1132 if ((ih->ih_flags & IH_CHANGED) != 0) {
1133 mtx_lock(&ie->ie_lock);
1134 ih->ih_flags &= ~IH_CHANGED;
1136 mtx_unlock(&ie->ie_lock);
1139 /* Skip filter only handlers */
1140 if (ih->ih_handler == NULL)
1143 /* Skip suspended handlers */
1144 if ((ih->ih_flags & IH_SUSP) != 0)
1148 * For software interrupt threads, we only execute
1149 * handlers that have their need flag set. Hardware
1150 * interrupt threads always invoke all of their handlers.
1152 * ih_need can only be 0 or 1. Failed cmpset below
1153 * means that there is no request to execute handlers,
1154 * so a retry of the cmpset is not needed.
1156 if ((ie->ie_flags & IE_SOFT) != 0 &&
1157 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1160 /* Execute this handler. */
1161 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1162 __func__, p->p_pid, (void *)ih->ih_handler,
1163 ih->ih_argument, ih->ih_name, ih->ih_flags);
1165 if (!(ih->ih_flags & IH_MPSAFE))
1167 ih->ih_handler(ih->ih_argument);
1168 if (!(ih->ih_flags & IH_MPSAFE))
1174 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1177 /* Interrupt handlers should not sleep. */
1178 if (!(ie->ie_flags & IE_SOFT))
1179 THREAD_NO_SLEEPING();
1180 intr_event_execute_handlers(p, ie);
1181 if (!(ie->ie_flags & IE_SOFT))
1182 THREAD_SLEEPING_OK();
1185 * Interrupt storm handling:
1187 * If this interrupt source is currently storming, then throttle
1188 * it to only fire the handler once per clock tick.
1190 * If this interrupt source is not currently storming, but the
1191 * number of back to back interrupts exceeds the storm threshold,
1192 * then enter storming mode.
1194 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1195 !(ie->ie_flags & IE_SOFT)) {
1196 /* Report the message only once every second. */
1197 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1199 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1207 * Now that all the handlers have had a chance to run, reenable
1208 * the interrupt source.
1210 if (ie->ie_post_ithread != NULL)
1211 ie->ie_post_ithread(ie->ie_source);
1215 * This is the main code for interrupt threads.
1218 ithread_loop(void *arg)
1220 struct epoch_tracker et;
1221 struct intr_thread *ithd;
1222 struct intr_event *ie;
1225 int wake, epoch_count;
1230 ithd = (struct intr_thread *)arg;
1231 KASSERT(ithd->it_thread == td,
1232 ("%s: ithread and proc linkage out of sync", __func__));
1233 ie = ithd->it_event;
1238 * As long as we have interrupts outstanding, go through the
1239 * list of handlers, giving each one a go at it.
1243 * If we are an orphaned thread, then just die.
1245 if (ithd->it_flags & IT_DEAD) {
1246 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1247 p->p_pid, td->td_name);
1248 free(ithd, M_ITHREAD);
1253 * Service interrupts. If another interrupt arrives while
1254 * we are running, it will set it_need to note that we
1255 * should make another pass.
1257 * The load_acq part of the following cmpset ensures
1258 * that the load of ih_need in ithread_execute_handlers()
1259 * is ordered after the load of it_need here.
1262 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1265 NET_EPOCH_ENTER(et);
1267 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1268 ithread_execute_handlers(p, ie);
1270 ++epoch_count >= intr_epoch_batch) {
1273 NET_EPOCH_ENTER(et);
1278 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1279 mtx_assert(&Giant, MA_NOTOWNED);
1282 * Processed all our interrupts. Now get the sched
1283 * lock. This may take a while and it_need may get
1284 * set again, so we have to check it again.
1287 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1288 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1291 mi_switch(SW_VOL | SWT_IWAIT);
1293 if (ithd->it_flags & IT_WAIT) {
1295 ithd->it_flags &= ~IT_WAIT;
1307 * Main interrupt handling body.
1310 * o ie: the event connected to this interrupt.
1311 * o frame: some archs (i.e. i386) pass a frame to some.
1312 * handlers as their main argument.
1314 * o 0: everything ok.
1315 * o EINVAL: stray interrupt.
1318 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1320 struct intr_handler *ih;
1321 struct trapframe *oldframe;
1325 bool filter, thread;
1329 #ifdef KSTACK_USAGE_PROF
1330 intr_prof_stack_use(td, frame);
1333 /* An interrupt with no event or handlers is a stray interrupt. */
1334 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1338 * Execute fast interrupt handlers directly.
1339 * To support clock handlers, if a handler registers
1340 * with a NULL argument, then we pass it a pointer to
1341 * a trapframe as its argument.
1343 td->td_intr_nesting_level++;
1348 oldframe = td->td_intr_frame;
1349 td->td_intr_frame = frame;
1351 phase = ie->ie_phase;
1352 atomic_add_int(&ie->ie_active[phase], 1);
1355 * This fence is required to ensure that no later loads are
1356 * re-ordered before the ie_active store.
1358 atomic_thread_fence_seq_cst();
1360 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1361 if ((ih->ih_flags & IH_SUSP) != 0)
1363 if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0)
1365 if (ih->ih_filter == NULL) {
1369 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1370 ih->ih_filter, ih->ih_argument == NULL ? frame :
1371 ih->ih_argument, ih->ih_name);
1372 if (ih->ih_argument == NULL)
1373 ret = ih->ih_filter(frame);
1375 ret = ih->ih_filter(ih->ih_argument);
1376 KASSERT(ret == FILTER_STRAY ||
1377 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1378 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1379 ("%s: incorrect return value %#x from %s", __func__, ret,
1381 filter = filter || ret == FILTER_HANDLED;
1384 * Wrapper handler special handling:
1386 * in some particular cases (like pccard and pccbb),
1387 * the _real_ device handler is wrapped in a couple of
1388 * functions - a filter wrapper and an ithread wrapper.
1389 * In this case (and just in this case), the filter wrapper
1390 * could ask the system to schedule the ithread and mask
1391 * the interrupt source if the wrapped handler is composed
1392 * of just an ithread handler.
1394 * TODO: write a generic wrapper to avoid people rolling
1398 if (ret == FILTER_SCHEDULE_THREAD)
1402 atomic_add_rel_int(&ie->ie_active[phase], -1);
1404 td->td_intr_frame = oldframe;
1407 if (ie->ie_pre_ithread != NULL)
1408 ie->ie_pre_ithread(ie->ie_source);
1410 if (ie->ie_post_filter != NULL)
1411 ie->ie_post_filter(ie->ie_source);
1414 /* Schedule the ithread if needed. */
1418 error = intr_event_schedule_thread(ie);
1419 KASSERT(error == 0, ("bad stray interrupt"));
1422 td->td_intr_nesting_level--;
1424 /* The interrupt is not aknowledged by any filter and has no ithread. */
1425 if (!thread && !filter)
1433 * Dump details about an interrupt handler
1436 db_dump_intrhand(struct intr_handler *ih)
1440 db_printf("\t%-10s ", ih->ih_name);
1441 switch (ih->ih_pri) {
1461 if (ih->ih_pri >= PI_SOFT)
1464 db_printf("%4u", ih->ih_pri);
1468 if (ih->ih_filter != NULL) {
1470 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1472 if (ih->ih_handler != NULL) {
1473 if (ih->ih_filter != NULL)
1476 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1478 db_printf("(%p)", ih->ih_argument);
1480 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1484 if (ih->ih_flags & IH_EXCLUSIVE) {
1490 if (ih->ih_flags & IH_ENTROPY) {
1493 db_printf("ENTROPY");
1496 if (ih->ih_flags & IH_DEAD) {
1502 if (ih->ih_flags & IH_MPSAFE) {
1505 db_printf("MPSAFE");
1519 * Dump details about a event.
1522 db_dump_intr_event(struct intr_event *ie, int handlers)
1524 struct intr_handler *ih;
1525 struct intr_thread *it;
1528 db_printf("%s ", ie->ie_fullname);
1531 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1533 db_printf("(no thread)");
1534 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1535 (it != NULL && it->it_need)) {
1538 if (ie->ie_flags & IE_SOFT) {
1542 if (ie->ie_flags & IE_ADDING_THREAD) {
1545 db_printf("ADDING_THREAD");
1548 if (it != NULL && it->it_need) {
1558 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1559 db_dump_intrhand(ih);
1563 * Dump data about interrupt handlers
1565 DB_SHOW_COMMAND(intr, db_show_intr)
1567 struct intr_event *ie;
1570 verbose = strchr(modif, 'v') != NULL;
1571 all = strchr(modif, 'a') != NULL;
1572 TAILQ_FOREACH(ie, &event_list, ie_list) {
1573 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1575 db_dump_intr_event(ie, verbose);
1583 * Start standard software interrupt threads
1586 start_softintr(void *dummy)
1589 if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK,
1591 panic("died while creating clk swi ithread");
1593 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1597 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1598 * The data for this machine dependent, and the declarations are in machine
1599 * dependent code. The layout of intrnames and intrcnt however is machine
1602 * We do not know the length of intrcnt and intrnames at compile time, so
1603 * calculate things at run time.
1606 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1608 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1611 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1612 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1613 sysctl_intrnames, "",
1617 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1620 uint32_t *intrcnt32;
1624 if (req->flags & SCTL_MASK32) {
1626 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1627 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1628 if (intrcnt32 == NULL)
1630 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1631 intrcnt32[i] = intrcnt[i];
1632 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1633 free(intrcnt32, M_TEMP);
1637 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1640 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1641 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1643 "Interrupt Counts");
1647 * DDB command to dump the interrupt statistics.
1649 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1657 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1662 db_printf("%s\t%lu\n", cp, *i);
1663 cp += strlen(cp) + 1;