2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_kstack_usage_prof.h"
35 #include <sys/param.h>
38 #include <sys/cpuset.h>
39 #include <sys/rtprio.h>
40 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/epoch.h>
52 #include <sys/random.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
58 #include <sys/unistd.h>
59 #include <sys/vmmeter.h>
60 #include <machine/atomic.h>
61 #include <machine/cpu.h>
62 #include <machine/md_var.h>
63 #include <machine/stdarg.h>
66 #include <ddb/db_sym.h>
70 * Describe an interrupt thread. There is one of these per interrupt event.
73 struct intr_event *it_event;
74 struct thread *it_thread; /* Kernel thread. */
75 int it_flags; /* (j) IT_* flags. */
76 int it_need; /* Needs service. */
79 /* Interrupt thread flags kept in it_flags */
80 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
81 #define IT_WAIT 0x000002 /* Thread is waiting for completion. */
88 struct intr_event *tty_intr_event;
90 struct proc *intrproc;
92 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
94 static int intr_storm_threshold = 0;
95 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
96 &intr_storm_threshold, 0,
97 "Number of consecutive interrupts before storm protection is enabled");
98 static int intr_epoch_batch = 1000;
99 SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch,
100 0, "Maximum interrupt handler executions without re-entering epoch(9)");
101 static TAILQ_HEAD(, intr_event) event_list =
102 TAILQ_HEAD_INITIALIZER(event_list);
103 static struct mtx event_lock;
104 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
106 static void intr_event_update(struct intr_event *ie);
107 static int intr_event_schedule_thread(struct intr_event *ie);
108 static struct intr_thread *ithread_create(const char *name);
109 static void ithread_destroy(struct intr_thread *ithread);
110 static void ithread_execute_handlers(struct proc *p,
111 struct intr_event *ie);
112 static void ithread_loop(void *);
113 static void ithread_update(struct intr_thread *ithd);
114 static void start_softintr(void *);
116 /* Map an interrupt type to an ithread priority. */
118 intr_priority(enum intr_type flags)
122 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
123 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
144 pri = PI_DULL; /* don't care */
147 /* We didn't specify an interrupt level. */
148 panic("intr_priority: no interrupt type in flags");
155 * Update an ithread based on the associated intr_event.
158 ithread_update(struct intr_thread *ithd)
160 struct intr_event *ie;
165 td = ithd->it_thread;
166 mtx_assert(&ie->ie_lock, MA_OWNED);
168 /* Determine the overall priority of this event. */
169 if (CK_SLIST_EMPTY(&ie->ie_handlers))
172 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
174 /* Update name and priority. */
175 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
177 sched_clear_tdname(td);
185 * Regenerate the full name of an interrupt event and update its priority.
188 intr_event_update(struct intr_event *ie)
190 struct intr_handler *ih;
192 int missed, space, flags;
194 /* Start off with no entropy and just the name of the event. */
195 mtx_assert(&ie->ie_lock, MA_OWNED);
196 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
201 /* Run through all the handlers updating values. */
202 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
203 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
204 sizeof(ie->ie_fullname)) {
205 strcat(ie->ie_fullname, " ");
206 strcat(ie->ie_fullname, ih->ih_name);
210 flags |= ih->ih_flags;
212 ie->ie_hflags = flags;
215 * If there is only one handler and its name is too long, just copy in
216 * as much of the end of the name (includes the unit number) as will
217 * fit. Otherwise, we have multiple handlers and not all of the names
218 * will fit. Add +'s to indicate missing names. If we run out of room
219 * and still have +'s to add, change the last character from a + to a *.
221 if (missed == 1 && space == 1) {
222 ih = CK_SLIST_FIRST(&ie->ie_handlers);
223 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
224 sizeof(ie->ie_fullname);
225 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
226 strcat(ie->ie_fullname, &ih->ih_name[missed]);
229 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
230 while (missed-- > 0) {
231 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
238 strcat(ie->ie_fullname, " +");
241 strcat(ie->ie_fullname, "+");
245 * If this event has an ithread, update it's priority and
248 if (ie->ie_thread != NULL)
249 ithread_update(ie->ie_thread);
250 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
254 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
255 void (*pre_ithread)(void *), void (*post_ithread)(void *),
256 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
257 const char *fmt, ...)
259 struct intr_event *ie;
262 /* The only valid flag during creation is IE_SOFT. */
263 if ((flags & ~IE_SOFT) != 0)
265 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
266 ie->ie_source = source;
267 ie->ie_pre_ithread = pre_ithread;
268 ie->ie_post_ithread = post_ithread;
269 ie->ie_post_filter = post_filter;
270 ie->ie_assign_cpu = assign_cpu;
271 ie->ie_flags = flags;
274 CK_SLIST_INIT(&ie->ie_handlers);
275 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
278 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
280 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
281 mtx_lock(&event_lock);
282 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
283 mtx_unlock(&event_lock);
286 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
291 * Bind an interrupt event to the specified CPU. Note that not all
292 * platforms support binding an interrupt to a CPU. For those
293 * platforms this request will fail. Using a cpu id of NOCPU unbinds
294 * the interrupt event.
297 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
302 /* Need a CPU to bind to. */
303 if (cpu != NOCPU && CPU_ABSENT(cpu))
306 if (ie->ie_assign_cpu == NULL)
309 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
314 * If we have any ithreads try to set their mask first to verify
318 mtx_lock(&ie->ie_lock);
319 if (ie->ie_thread != NULL) {
320 id = ie->ie_thread->it_thread->td_tid;
321 mtx_unlock(&ie->ie_lock);
322 error = cpuset_setithread(id, cpu);
326 mtx_unlock(&ie->ie_lock);
329 error = ie->ie_assign_cpu(ie->ie_source, cpu);
332 mtx_lock(&ie->ie_lock);
333 if (ie->ie_thread != NULL) {
335 id = ie->ie_thread->it_thread->td_tid;
336 mtx_unlock(&ie->ie_lock);
337 (void)cpuset_setithread(id, cpu);
339 mtx_unlock(&ie->ie_lock);
345 mtx_lock(&ie->ie_lock);
347 mtx_unlock(&ie->ie_lock);
354 * Bind an interrupt event to the specified CPU. For supported platforms, any
355 * associated ithreads as well as the primary interrupt context will be bound
356 * to the specificed CPU.
359 intr_event_bind(struct intr_event *ie, int cpu)
362 return (_intr_event_bind(ie, cpu, true, true));
366 * Bind an interrupt event to the specified CPU, but do not bind associated
370 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
373 return (_intr_event_bind(ie, cpu, true, false));
377 * Bind an interrupt event's ithread to the specified CPU.
380 intr_event_bind_ithread(struct intr_event *ie, int cpu)
383 return (_intr_event_bind(ie, cpu, false, true));
387 * Bind an interrupt event's ithread to the specified cpuset.
390 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
394 mtx_lock(&ie->ie_lock);
395 if (ie->ie_thread != NULL) {
396 id = ie->ie_thread->it_thread->td_tid;
397 mtx_unlock(&ie->ie_lock);
398 return (cpuset_setthread(id, cs));
400 mtx_unlock(&ie->ie_lock);
405 static struct intr_event *
408 struct intr_event *ie;
410 mtx_lock(&event_lock);
411 TAILQ_FOREACH(ie, &event_list, ie_list)
412 if (ie->ie_irq == irq &&
413 (ie->ie_flags & IE_SOFT) == 0 &&
414 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
416 mtx_unlock(&event_lock);
421 intr_setaffinity(int irq, int mode, void *m)
423 struct intr_event *ie;
430 * If we're setting all cpus we can unbind. Otherwise make sure
431 * only one cpu is in the set.
433 if (CPU_CMP(cpuset_root, mask)) {
434 for (n = 0; n < CPU_SETSIZE; n++) {
435 if (!CPU_ISSET(n, mask))
442 ie = intr_lookup(irq);
447 return (intr_event_bind(ie, cpu));
448 case CPU_WHICH_INTRHANDLER:
449 return (intr_event_bind_irqonly(ie, cpu));
450 case CPU_WHICH_ITHREAD:
451 return (intr_event_bind_ithread(ie, cpu));
458 intr_getaffinity(int irq, int mode, void *m)
460 struct intr_event *ie;
468 ie = intr_lookup(irq);
476 case CPU_WHICH_INTRHANDLER:
477 mtx_lock(&ie->ie_lock);
478 if (ie->ie_cpu == NOCPU)
479 CPU_COPY(cpuset_root, mask);
481 CPU_SET(ie->ie_cpu, mask);
482 mtx_unlock(&ie->ie_lock);
484 case CPU_WHICH_ITHREAD:
485 mtx_lock(&ie->ie_lock);
486 if (ie->ie_thread == NULL) {
487 mtx_unlock(&ie->ie_lock);
488 CPU_COPY(cpuset_root, mask);
490 id = ie->ie_thread->it_thread->td_tid;
491 mtx_unlock(&ie->ie_lock);
492 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
495 CPU_COPY(&td->td_cpuset->cs_mask, mask);
505 intr_event_destroy(struct intr_event *ie)
508 mtx_lock(&event_lock);
509 mtx_lock(&ie->ie_lock);
510 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
511 mtx_unlock(&ie->ie_lock);
512 mtx_unlock(&event_lock);
515 TAILQ_REMOVE(&event_list, ie, ie_list);
517 if (ie->ie_thread != NULL) {
518 ithread_destroy(ie->ie_thread);
519 ie->ie_thread = NULL;
522 mtx_unlock(&ie->ie_lock);
523 mtx_unlock(&event_lock);
524 mtx_destroy(&ie->ie_lock);
529 static struct intr_thread *
530 ithread_create(const char *name)
532 struct intr_thread *ithd;
536 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
538 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
539 &td, RFSTOPPED | RFHIGHPID,
540 0, "intr", "%s", name);
542 panic("kproc_create() failed with %d", error);
544 sched_class(td, PRI_ITHD);
547 td->td_pflags |= TDP_ITHREAD;
548 ithd->it_thread = td;
549 CTR2(KTR_INTR, "%s: created %s", __func__, name);
554 ithread_destroy(struct intr_thread *ithread)
558 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
559 td = ithread->it_thread;
561 ithread->it_flags |= IT_DEAD;
562 if (TD_AWAITING_INTR(td)) {
564 sched_add(td, SRQ_INTR);
570 intr_event_add_handler(struct intr_event *ie, const char *name,
571 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
572 enum intr_type flags, void **cookiep)
574 struct intr_handler *ih, *temp_ih;
575 struct intr_handler **prevptr;
576 struct intr_thread *it;
578 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
581 /* Allocate and populate an interrupt handler structure. */
582 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
583 ih->ih_filter = filter;
584 ih->ih_handler = handler;
585 ih->ih_argument = arg;
586 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
589 if (flags & INTR_EXCL)
590 ih->ih_flags = IH_EXCLUSIVE;
591 if (flags & INTR_MPSAFE)
592 ih->ih_flags |= IH_MPSAFE;
593 if (flags & INTR_ENTROPY)
594 ih->ih_flags |= IH_ENTROPY;
595 if (flags & INTR_TYPE_NET)
596 ih->ih_flags |= IH_NET;
598 /* We can only have one exclusive handler in a event. */
599 mtx_lock(&ie->ie_lock);
600 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
601 if ((flags & INTR_EXCL) ||
602 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
603 mtx_unlock(&ie->ie_lock);
609 /* Create a thread if we need one. */
610 while (ie->ie_thread == NULL && handler != NULL) {
611 if (ie->ie_flags & IE_ADDING_THREAD)
612 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
614 ie->ie_flags |= IE_ADDING_THREAD;
615 mtx_unlock(&ie->ie_lock);
616 it = ithread_create("intr: newborn");
617 mtx_lock(&ie->ie_lock);
618 ie->ie_flags &= ~IE_ADDING_THREAD;
626 /* Add the new handler to the event in priority order. */
627 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
628 if (temp_ih->ih_pri > ih->ih_pri)
631 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
633 intr_event_update(ie);
635 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
637 mtx_unlock(&ie->ie_lock);
645 * Append a description preceded by a ':' to the name of the specified
649 intr_event_describe_handler(struct intr_event *ie, void *cookie,
652 struct intr_handler *ih;
656 mtx_lock(&ie->ie_lock);
658 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
663 mtx_unlock(&ie->ie_lock);
664 panic("handler %p not found in interrupt event %p", cookie, ie);
670 * Look for an existing description by checking for an
671 * existing ":". This assumes device names do not include
672 * colons. If one is found, prepare to insert the new
673 * description at that point. If one is not found, find the
674 * end of the name to use as the insertion point.
676 start = strchr(ih->ih_name, ':');
678 start = strchr(ih->ih_name, 0);
681 * See if there is enough remaining room in the string for the
682 * description + ":". The "- 1" leaves room for the trailing
683 * '\0'. The "+ 1" accounts for the colon.
685 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
686 if (strlen(descr) + 1 > space) {
687 mtx_unlock(&ie->ie_lock);
691 /* Append a colon followed by the description. */
693 strcpy(start + 1, descr);
694 intr_event_update(ie);
695 mtx_unlock(&ie->ie_lock);
700 * Return the ie_source field from the intr_event an intr_handler is
704 intr_handler_source(void *cookie)
706 struct intr_handler *ih;
707 struct intr_event *ie;
709 ih = (struct intr_handler *)cookie;
714 ("interrupt handler \"%s\" has a NULL interrupt event",
716 return (ie->ie_source);
720 * If intr_event_handle() is running in the ISR context at the time of the call,
721 * then wait for it to complete.
724 intr_event_barrier(struct intr_event *ie)
728 mtx_assert(&ie->ie_lock, MA_OWNED);
729 phase = ie->ie_phase;
732 * Switch phase to direct future interrupts to the other active counter.
733 * Make sure that any preceding stores are visible before the switch.
735 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
736 atomic_store_rel_int(&ie->ie_phase, !phase);
739 * This code cooperates with wait-free iteration of ie_handlers
740 * in intr_event_handle.
741 * Make sure that the removal and the phase update are not reordered
742 * with the active count check.
743 * Note that no combination of acquire and release fences can provide
744 * that guarantee as Store->Load sequences can always be reordered.
746 atomic_thread_fence_seq_cst();
749 * Now wait on the inactive phase.
750 * The acquire fence is needed so that that all post-barrier accesses
751 * are after the check.
753 while (ie->ie_active[phase] > 0)
755 atomic_thread_fence_acq();
759 intr_handler_barrier(struct intr_handler *handler)
761 struct intr_event *ie;
763 ie = handler->ih_event;
764 mtx_assert(&ie->ie_lock, MA_OWNED);
765 KASSERT((handler->ih_flags & IH_DEAD) == 0,
766 ("update for a removed handler"));
768 if (ie->ie_thread == NULL) {
769 intr_event_barrier(ie);
772 if ((handler->ih_flags & IH_CHANGED) == 0) {
773 handler->ih_flags |= IH_CHANGED;
774 intr_event_schedule_thread(ie);
776 while ((handler->ih_flags & IH_CHANGED) != 0)
777 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
781 * Sleep until an ithread finishes executing an interrupt handler.
783 * XXX Doesn't currently handle interrupt filters or fast interrupt
784 * handlers. This is intended for compatibility with linux drivers
785 * only. Do not use in BSD code.
790 struct intr_event *ie;
791 struct intr_thread *ithd;
794 ie = intr_lookup(irq);
797 if (ie->ie_thread == NULL)
799 ithd = ie->ie_thread;
800 td = ithd->it_thread;
802 * We set the flag and wait for it to be cleared to avoid
803 * long delays with potentially busy interrupt handlers
804 * were we to only sample TD_AWAITING_INTR() every tick.
807 if (!TD_AWAITING_INTR(td)) {
808 ithd->it_flags |= IT_WAIT;
809 while (ithd->it_flags & IT_WAIT) {
820 intr_event_remove_handler(void *cookie)
822 struct intr_handler *handler = (struct intr_handler *)cookie;
823 struct intr_event *ie;
824 struct intr_handler *ih;
825 struct intr_handler **prevptr;
832 ie = handler->ih_event;
834 ("interrupt handler \"%s\" has a NULL interrupt event",
837 mtx_lock(&ie->ie_lock);
838 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
840 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
845 panic("interrupt handler \"%s\" not found in "
846 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
850 * If there is no ithread, then directly remove the handler. Note that
851 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
852 * care needs to be taken to keep ie_handlers consistent and to free
853 * the removed handler only when ie_handlers is quiescent.
855 if (ie->ie_thread == NULL) {
856 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
857 intr_event_barrier(ie);
858 intr_event_update(ie);
859 mtx_unlock(&ie->ie_lock);
860 free(handler, M_ITHREAD);
865 * Let the interrupt thread do the job.
866 * The interrupt source is disabled when the interrupt thread is
867 * running, so it does not have to worry about interaction with
868 * intr_event_handle().
870 KASSERT((handler->ih_flags & IH_DEAD) == 0,
871 ("duplicate handle remove"));
872 handler->ih_flags |= IH_DEAD;
873 intr_event_schedule_thread(ie);
874 while (handler->ih_flags & IH_DEAD)
875 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
876 intr_event_update(ie);
880 * XXX: This could be bad in the case of ppbus(8). Also, I think
881 * this could lead to races of stale data when servicing an
885 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
886 if (ih->ih_handler != NULL) {
892 ithread_destroy(ie->ie_thread);
893 ie->ie_thread = NULL;
896 mtx_unlock(&ie->ie_lock);
897 free(handler, M_ITHREAD);
902 intr_event_suspend_handler(void *cookie)
904 struct intr_handler *handler = (struct intr_handler *)cookie;
905 struct intr_event *ie;
909 ie = handler->ih_event;
911 ("interrupt handler \"%s\" has a NULL interrupt event",
913 mtx_lock(&ie->ie_lock);
914 handler->ih_flags |= IH_SUSP;
915 intr_handler_barrier(handler);
916 mtx_unlock(&ie->ie_lock);
921 intr_event_resume_handler(void *cookie)
923 struct intr_handler *handler = (struct intr_handler *)cookie;
924 struct intr_event *ie;
928 ie = handler->ih_event;
930 ("interrupt handler \"%s\" has a NULL interrupt event",
934 * intr_handler_barrier() acts not only as a barrier,
935 * it also allows to check for any pending interrupts.
937 mtx_lock(&ie->ie_lock);
938 handler->ih_flags &= ~IH_SUSP;
939 intr_handler_barrier(handler);
940 mtx_unlock(&ie->ie_lock);
945 intr_event_schedule_thread(struct intr_event *ie)
947 struct intr_entropy entropy;
948 struct intr_thread *it;
953 * If no ithread or no handlers, then we have a stray interrupt.
955 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
956 ie->ie_thread == NULL)
964 * If any of the handlers for this ithread claim to be good
965 * sources of entropy, then gather some.
967 if (ie->ie_hflags & IH_ENTROPY) {
968 entropy.event = (uintptr_t)ie;
970 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
973 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
976 * Set it_need to tell the thread to keep running if it is already
977 * running. Then, lock the thread and see if we actually need to
978 * put it on the runqueue.
980 * Use store_rel to arrange that the store to ih_need in
981 * swi_sched() is before the store to it_need and prepare for
982 * transfer of this order to loads in the ithread.
984 atomic_store_rel_int(&it->it_need, 1);
986 if (TD_AWAITING_INTR(td)) {
987 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
990 sched_add(td, SRQ_INTR);
992 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
993 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
1001 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1002 * since interrupts are generated in software rather than being directed by
1006 swi_assign_cpu(void *arg, int cpu)
1013 * Add a software interrupt handler to a specified event. If a given event
1014 * is not specified, then a new event is created.
1017 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1018 void *arg, int pri, enum intr_type flags, void **cookiep)
1020 struct intr_event *ie;
1023 if (flags & INTR_ENTROPY)
1026 ie = (eventp != NULL) ? *eventp : NULL;
1029 if (!(ie->ie_flags & IE_SOFT))
1032 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1033 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1039 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1040 PI_SWI(pri), flags, cookiep);
1045 * Schedule a software interrupt thread.
1048 swi_sched(void *cookie, int flags)
1050 struct intr_handler *ih = (struct intr_handler *)cookie;
1051 struct intr_event *ie = ih->ih_event;
1052 struct intr_entropy entropy;
1055 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1058 entropy.event = (uintptr_t)ih;
1059 entropy.td = curthread;
1060 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1063 * Set ih_need for this handler so that if the ithread is already
1064 * running it will execute this handler on the next pass. Otherwise,
1065 * it will execute it the next time it runs.
1069 if (!(flags & SWI_DELAY)) {
1071 error = intr_event_schedule_thread(ie);
1072 KASSERT(error == 0, ("stray software interrupt"));
1077 * Remove a software interrupt handler. Currently this code does not
1078 * remove the associated interrupt event if it becomes empty. Calling code
1079 * may do so manually via intr_event_destroy(), but that's not really
1080 * an optimal interface.
1083 swi_remove(void *cookie)
1086 return (intr_event_remove_handler(cookie));
1090 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1092 struct intr_handler *ih, *ihn, *ihp;
1095 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1097 * If this handler is marked for death, remove it from
1098 * the list of handlers and wake up the sleeper.
1100 if (ih->ih_flags & IH_DEAD) {
1101 mtx_lock(&ie->ie_lock);
1103 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1105 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1106 ih->ih_flags &= ~IH_DEAD;
1108 mtx_unlock(&ie->ie_lock);
1113 * Now that we know that the current element won't be removed
1114 * update the previous element.
1118 if ((ih->ih_flags & IH_CHANGED) != 0) {
1119 mtx_lock(&ie->ie_lock);
1120 ih->ih_flags &= ~IH_CHANGED;
1122 mtx_unlock(&ie->ie_lock);
1125 /* Skip filter only handlers */
1126 if (ih->ih_handler == NULL)
1129 /* Skip suspended handlers */
1130 if ((ih->ih_flags & IH_SUSP) != 0)
1134 * For software interrupt threads, we only execute
1135 * handlers that have their need flag set. Hardware
1136 * interrupt threads always invoke all of their handlers.
1138 * ih_need can only be 0 or 1. Failed cmpset below
1139 * means that there is no request to execute handlers,
1140 * so a retry of the cmpset is not needed.
1142 if ((ie->ie_flags & IE_SOFT) != 0 &&
1143 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1146 /* Execute this handler. */
1147 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1148 __func__, p->p_pid, (void *)ih->ih_handler,
1149 ih->ih_argument, ih->ih_name, ih->ih_flags);
1151 if (!(ih->ih_flags & IH_MPSAFE))
1153 ih->ih_handler(ih->ih_argument);
1154 if (!(ih->ih_flags & IH_MPSAFE))
1160 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1163 /* Interrupt handlers should not sleep. */
1164 if (!(ie->ie_flags & IE_SOFT))
1165 THREAD_NO_SLEEPING();
1166 intr_event_execute_handlers(p, ie);
1167 if (!(ie->ie_flags & IE_SOFT))
1168 THREAD_SLEEPING_OK();
1171 * Interrupt storm handling:
1173 * If this interrupt source is currently storming, then throttle
1174 * it to only fire the handler once per clock tick.
1176 * If this interrupt source is not currently storming, but the
1177 * number of back to back interrupts exceeds the storm threshold,
1178 * then enter storming mode.
1180 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1181 !(ie->ie_flags & IE_SOFT)) {
1182 /* Report the message only once every second. */
1183 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1185 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1193 * Now that all the handlers have had a chance to run, reenable
1194 * the interrupt source.
1196 if (ie->ie_post_ithread != NULL)
1197 ie->ie_post_ithread(ie->ie_source);
1201 * This is the main code for interrupt threads.
1204 ithread_loop(void *arg)
1206 struct epoch_tracker et;
1207 struct intr_thread *ithd;
1208 struct intr_event *ie;
1211 int wake, epoch_count;
1216 ithd = (struct intr_thread *)arg;
1217 KASSERT(ithd->it_thread == td,
1218 ("%s: ithread and proc linkage out of sync", __func__));
1219 ie = ithd->it_event;
1224 * As long as we have interrupts outstanding, go through the
1225 * list of handlers, giving each one a go at it.
1229 * If we are an orphaned thread, then just die.
1231 if (ithd->it_flags & IT_DEAD) {
1232 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1233 p->p_pid, td->td_name);
1234 free(ithd, M_ITHREAD);
1239 * Service interrupts. If another interrupt arrives while
1240 * we are running, it will set it_need to note that we
1241 * should make another pass.
1243 * The load_acq part of the following cmpset ensures
1244 * that the load of ih_need in ithread_execute_handlers()
1245 * is ordered after the load of it_need here.
1248 (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0;
1251 NET_EPOCH_ENTER(et);
1253 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1254 ithread_execute_handlers(p, ie);
1256 ++epoch_count >= intr_epoch_batch) {
1259 NET_EPOCH_ENTER(et);
1264 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1265 mtx_assert(&Giant, MA_NOTOWNED);
1268 * Processed all our interrupts. Now get the sched
1269 * lock. This may take a while and it_need may get
1270 * set again, so we have to check it again.
1273 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1274 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1277 mi_switch(SW_VOL | SWT_IWAIT);
1279 if (ithd->it_flags & IT_WAIT) {
1281 ithd->it_flags &= ~IT_WAIT;
1293 * Main interrupt handling body.
1296 * o ie: the event connected to this interrupt.
1297 * o frame: some archs (i.e. i386) pass a frame to some.
1298 * handlers as their main argument.
1300 * o 0: everything ok.
1301 * o EINVAL: stray interrupt.
1304 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1306 struct intr_handler *ih;
1307 struct trapframe *oldframe;
1311 bool filter, thread;
1315 #ifdef KSTACK_USAGE_PROF
1316 intr_prof_stack_use(td, frame);
1319 /* An interrupt with no event or handlers is a stray interrupt. */
1320 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1324 * Execute fast interrupt handlers directly.
1325 * To support clock handlers, if a handler registers
1326 * with a NULL argument, then we pass it a pointer to
1327 * a trapframe as its argument.
1329 td->td_intr_nesting_level++;
1334 oldframe = td->td_intr_frame;
1335 td->td_intr_frame = frame;
1337 phase = ie->ie_phase;
1338 atomic_add_int(&ie->ie_active[phase], 1);
1341 * This fence is required to ensure that no later loads are
1342 * re-ordered before the ie_active store.
1344 atomic_thread_fence_seq_cst();
1346 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1347 if ((ih->ih_flags & IH_SUSP) != 0)
1349 if (ih->ih_filter == NULL) {
1353 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1354 ih->ih_filter, ih->ih_argument == NULL ? frame :
1355 ih->ih_argument, ih->ih_name);
1356 if (ih->ih_argument == NULL)
1357 ret = ih->ih_filter(frame);
1359 ret = ih->ih_filter(ih->ih_argument);
1360 KASSERT(ret == FILTER_STRAY ||
1361 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1362 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1363 ("%s: incorrect return value %#x from %s", __func__, ret,
1365 filter = filter || ret == FILTER_HANDLED;
1368 * Wrapper handler special handling:
1370 * in some particular cases (like pccard and pccbb),
1371 * the _real_ device handler is wrapped in a couple of
1372 * functions - a filter wrapper and an ithread wrapper.
1373 * In this case (and just in this case), the filter wrapper
1374 * could ask the system to schedule the ithread and mask
1375 * the interrupt source if the wrapped handler is composed
1376 * of just an ithread handler.
1378 * TODO: write a generic wrapper to avoid people rolling
1382 if (ret == FILTER_SCHEDULE_THREAD)
1386 atomic_add_rel_int(&ie->ie_active[phase], -1);
1388 td->td_intr_frame = oldframe;
1391 if (ie->ie_pre_ithread != NULL)
1392 ie->ie_pre_ithread(ie->ie_source);
1394 if (ie->ie_post_filter != NULL)
1395 ie->ie_post_filter(ie->ie_source);
1398 /* Schedule the ithread if needed. */
1402 error = intr_event_schedule_thread(ie);
1403 KASSERT(error == 0, ("bad stray interrupt"));
1406 td->td_intr_nesting_level--;
1408 /* The interrupt is not aknowledged by any filter and has no ithread. */
1409 if (!thread && !filter)
1417 * Dump details about an interrupt handler
1420 db_dump_intrhand(struct intr_handler *ih)
1424 db_printf("\t%-10s ", ih->ih_name);
1425 switch (ih->ih_pri) {
1445 if (ih->ih_pri >= PI_SOFT)
1448 db_printf("%4u", ih->ih_pri);
1452 if (ih->ih_filter != NULL) {
1454 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1456 if (ih->ih_handler != NULL) {
1457 if (ih->ih_filter != NULL)
1460 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1462 db_printf("(%p)", ih->ih_argument);
1464 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1468 if (ih->ih_flags & IH_EXCLUSIVE) {
1474 if (ih->ih_flags & IH_ENTROPY) {
1477 db_printf("ENTROPY");
1480 if (ih->ih_flags & IH_DEAD) {
1486 if (ih->ih_flags & IH_MPSAFE) {
1489 db_printf("MPSAFE");
1503 * Dump details about a event.
1506 db_dump_intr_event(struct intr_event *ie, int handlers)
1508 struct intr_handler *ih;
1509 struct intr_thread *it;
1512 db_printf("%s ", ie->ie_fullname);
1515 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1517 db_printf("(no thread)");
1518 if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 ||
1519 (it != NULL && it->it_need)) {
1522 if (ie->ie_flags & IE_SOFT) {
1526 if (ie->ie_flags & IE_ADDING_THREAD) {
1529 db_printf("ADDING_THREAD");
1532 if (it != NULL && it->it_need) {
1542 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1543 db_dump_intrhand(ih);
1547 * Dump data about interrupt handlers
1549 DB_SHOW_COMMAND(intr, db_show_intr)
1551 struct intr_event *ie;
1554 verbose = strchr(modif, 'v') != NULL;
1555 all = strchr(modif, 'a') != NULL;
1556 TAILQ_FOREACH(ie, &event_list, ie_list) {
1557 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1559 db_dump_intr_event(ie, verbose);
1567 * Start standard software interrupt threads
1570 start_softintr(void *dummy)
1573 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1574 panic("died while creating vm swi ithread");
1576 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1580 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1581 * The data for this machine dependent, and the declarations are in machine
1582 * dependent code. The layout of intrnames and intrcnt however is machine
1585 * We do not know the length of intrcnt and intrnames at compile time, so
1586 * calculate things at run time.
1589 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1591 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1594 SYSCTL_PROC(_hw, OID_AUTO, intrnames,
1595 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
1596 sysctl_intrnames, "",
1600 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1603 uint32_t *intrcnt32;
1607 if (req->flags & SCTL_MASK32) {
1609 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1610 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1611 if (intrcnt32 == NULL)
1613 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1614 intrcnt32[i] = intrcnt[i];
1615 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1616 free(intrcnt32, M_TEMP);
1620 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1623 SYSCTL_PROC(_hw, OID_AUTO, intrcnt,
1624 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
1626 "Interrupt Counts");
1630 * DDB command to dump the interrupt statistics.
1632 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1640 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1645 db_printf("%s\t%lu\n", cp, *i);
1646 cp += strlen(cp) + 1;