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/random.h>
52 #include <sys/resourcevar.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/syslog.h>
57 #include <sys/unistd.h>
58 #include <sys/vmmeter.h>
59 #include <machine/atomic.h>
60 #include <machine/cpu.h>
61 #include <machine/md_var.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 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 TAILQ_HEAD(, intr_event) event_list =
99 TAILQ_HEAD_INITIALIZER(event_list);
100 static struct mtx event_lock;
101 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
103 static void intr_event_update(struct intr_event *ie);
104 static int intr_event_schedule_thread(struct intr_event *ie);
105 static struct intr_thread *ithread_create(const char *name);
106 static void ithread_destroy(struct intr_thread *ithread);
107 static void ithread_execute_handlers(struct proc *p,
108 struct intr_event *ie);
109 static void ithread_loop(void *);
110 static void ithread_update(struct intr_thread *ithd);
111 static void start_softintr(void *);
113 /* Map an interrupt type to an ithread priority. */
115 intr_priority(enum intr_type flags)
119 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
120 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
141 pri = PI_DULL; /* don't care */
144 /* We didn't specify an interrupt level. */
145 panic("intr_priority: no interrupt type in flags");
152 * Update an ithread based on the associated intr_event.
155 ithread_update(struct intr_thread *ithd)
157 struct intr_event *ie;
162 td = ithd->it_thread;
163 mtx_assert(&ie->ie_lock, MA_OWNED);
165 /* Determine the overall priority of this event. */
166 if (CK_SLIST_EMPTY(&ie->ie_handlers))
169 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
171 /* Update name and priority. */
172 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
174 sched_clear_tdname(td);
182 * Regenerate the full name of an interrupt event and update its priority.
185 intr_event_update(struct intr_event *ie)
187 struct intr_handler *ih;
191 /* Start off with no entropy and just the name of the event. */
192 mtx_assert(&ie->ie_lock, MA_OWNED);
193 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
194 ie->ie_flags &= ~IE_ENTROPY;
198 /* Run through all the handlers updating values. */
199 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
200 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
201 sizeof(ie->ie_fullname)) {
202 strcat(ie->ie_fullname, " ");
203 strcat(ie->ie_fullname, ih->ih_name);
207 if (ih->ih_flags & IH_ENTROPY)
208 ie->ie_flags |= IE_ENTROPY;
212 * If there is only one handler and its name is too long, just copy in
213 * as much of the end of the name (includes the unit number) as will
214 * fit. Otherwise, we have multiple handlers and not all of the names
215 * will fit. Add +'s to indicate missing names. If we run out of room
216 * and still have +'s to add, change the last character from a + to a *.
218 if (missed == 1 && space == 1) {
219 ih = CK_SLIST_FIRST(&ie->ie_handlers);
220 missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 -
221 sizeof(ie->ie_fullname);
222 strcat(ie->ie_fullname, (missed == 0) ? " " : "-");
223 strcat(ie->ie_fullname, &ih->ih_name[missed]);
226 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
227 while (missed-- > 0) {
228 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
235 strcat(ie->ie_fullname, " +");
238 strcat(ie->ie_fullname, "+");
242 * If this event has an ithread, update it's priority and
245 if (ie->ie_thread != NULL)
246 ithread_update(ie->ie_thread);
247 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
251 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
252 void (*pre_ithread)(void *), void (*post_ithread)(void *),
253 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
254 const char *fmt, ...)
256 struct intr_event *ie;
259 /* The only valid flag during creation is IE_SOFT. */
260 if ((flags & ~IE_SOFT) != 0)
262 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
263 ie->ie_source = source;
264 ie->ie_pre_ithread = pre_ithread;
265 ie->ie_post_ithread = post_ithread;
266 ie->ie_post_filter = post_filter;
267 ie->ie_assign_cpu = assign_cpu;
268 ie->ie_flags = flags;
271 CK_SLIST_INIT(&ie->ie_handlers);
272 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
275 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
277 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
278 mtx_lock(&event_lock);
279 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
280 mtx_unlock(&event_lock);
283 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
288 * Bind an interrupt event to the specified CPU. Note that not all
289 * platforms support binding an interrupt to a CPU. For those
290 * platforms this request will fail. Using a cpu id of NOCPU unbinds
291 * the interrupt event.
294 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
299 /* Need a CPU to bind to. */
300 if (cpu != NOCPU && CPU_ABSENT(cpu))
303 if (ie->ie_assign_cpu == NULL)
306 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
311 * If we have any ithreads try to set their mask first to verify
315 mtx_lock(&ie->ie_lock);
316 if (ie->ie_thread != NULL) {
317 id = ie->ie_thread->it_thread->td_tid;
318 mtx_unlock(&ie->ie_lock);
319 error = cpuset_setithread(id, cpu);
323 mtx_unlock(&ie->ie_lock);
326 error = ie->ie_assign_cpu(ie->ie_source, cpu);
329 mtx_lock(&ie->ie_lock);
330 if (ie->ie_thread != NULL) {
332 id = ie->ie_thread->it_thread->td_tid;
333 mtx_unlock(&ie->ie_lock);
334 (void)cpuset_setithread(id, cpu);
336 mtx_unlock(&ie->ie_lock);
342 mtx_lock(&ie->ie_lock);
344 mtx_unlock(&ie->ie_lock);
351 * Bind an interrupt event to the specified CPU. For supported platforms, any
352 * associated ithreads as well as the primary interrupt context will be bound
353 * to the specificed CPU.
356 intr_event_bind(struct intr_event *ie, int cpu)
359 return (_intr_event_bind(ie, cpu, true, true));
363 * Bind an interrupt event to the specified CPU, but do not bind associated
367 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
370 return (_intr_event_bind(ie, cpu, true, false));
374 * Bind an interrupt event's ithread to the specified CPU.
377 intr_event_bind_ithread(struct intr_event *ie, int cpu)
380 return (_intr_event_bind(ie, cpu, false, true));
384 * Bind an interrupt event's ithread to the specified cpuset.
387 intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs)
391 mtx_lock(&ie->ie_lock);
392 if (ie->ie_thread != NULL) {
393 id = ie->ie_thread->it_thread->td_tid;
394 mtx_unlock(&ie->ie_lock);
395 return (cpuset_setthread(id, cs));
397 mtx_unlock(&ie->ie_lock);
402 static struct intr_event *
405 struct intr_event *ie;
407 mtx_lock(&event_lock);
408 TAILQ_FOREACH(ie, &event_list, ie_list)
409 if (ie->ie_irq == irq &&
410 (ie->ie_flags & IE_SOFT) == 0 &&
411 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
413 mtx_unlock(&event_lock);
418 intr_setaffinity(int irq, int mode, void *m)
420 struct intr_event *ie;
427 * If we're setting all cpus we can unbind. Otherwise make sure
428 * only one cpu is in the set.
430 if (CPU_CMP(cpuset_root, mask)) {
431 for (n = 0; n < CPU_SETSIZE; n++) {
432 if (!CPU_ISSET(n, mask))
439 ie = intr_lookup(irq);
444 return (intr_event_bind(ie, cpu));
445 case CPU_WHICH_INTRHANDLER:
446 return (intr_event_bind_irqonly(ie, cpu));
447 case CPU_WHICH_ITHREAD:
448 return (intr_event_bind_ithread(ie, cpu));
455 intr_getaffinity(int irq, int mode, void *m)
457 struct intr_event *ie;
465 ie = intr_lookup(irq);
473 case CPU_WHICH_INTRHANDLER:
474 mtx_lock(&ie->ie_lock);
475 if (ie->ie_cpu == NOCPU)
476 CPU_COPY(cpuset_root, mask);
478 CPU_SET(ie->ie_cpu, mask);
479 mtx_unlock(&ie->ie_lock);
481 case CPU_WHICH_ITHREAD:
482 mtx_lock(&ie->ie_lock);
483 if (ie->ie_thread == NULL) {
484 mtx_unlock(&ie->ie_lock);
485 CPU_COPY(cpuset_root, mask);
487 id = ie->ie_thread->it_thread->td_tid;
488 mtx_unlock(&ie->ie_lock);
489 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
492 CPU_COPY(&td->td_cpuset->cs_mask, mask);
502 intr_event_destroy(struct intr_event *ie)
505 mtx_lock(&event_lock);
506 mtx_lock(&ie->ie_lock);
507 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
508 mtx_unlock(&ie->ie_lock);
509 mtx_unlock(&event_lock);
512 TAILQ_REMOVE(&event_list, ie, ie_list);
514 if (ie->ie_thread != NULL) {
515 ithread_destroy(ie->ie_thread);
516 ie->ie_thread = NULL;
519 mtx_unlock(&ie->ie_lock);
520 mtx_unlock(&event_lock);
521 mtx_destroy(&ie->ie_lock);
526 static struct intr_thread *
527 ithread_create(const char *name)
529 struct intr_thread *ithd;
533 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
535 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
536 &td, RFSTOPPED | RFHIGHPID,
537 0, "intr", "%s", name);
539 panic("kproc_create() failed with %d", error);
541 sched_class(td, PRI_ITHD);
544 td->td_pflags |= TDP_ITHREAD;
545 ithd->it_thread = td;
546 CTR2(KTR_INTR, "%s: created %s", __func__, name);
551 ithread_destroy(struct intr_thread *ithread)
555 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
556 td = ithread->it_thread;
558 ithread->it_flags |= IT_DEAD;
559 if (TD_AWAITING_INTR(td)) {
561 sched_add(td, SRQ_INTR);
567 intr_event_add_handler(struct intr_event *ie, const char *name,
568 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
569 enum intr_type flags, void **cookiep)
571 struct intr_handler *ih, *temp_ih;
572 struct intr_handler **prevptr;
573 struct intr_thread *it;
575 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
578 /* Allocate and populate an interrupt handler structure. */
579 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
580 ih->ih_filter = filter;
581 ih->ih_handler = handler;
582 ih->ih_argument = arg;
583 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
586 if (flags & INTR_EXCL)
587 ih->ih_flags = IH_EXCLUSIVE;
588 if (flags & INTR_MPSAFE)
589 ih->ih_flags |= IH_MPSAFE;
590 if (flags & INTR_ENTROPY)
591 ih->ih_flags |= IH_ENTROPY;
593 /* We can only have one exclusive handler in a event. */
594 mtx_lock(&ie->ie_lock);
595 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
596 if ((flags & INTR_EXCL) ||
597 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
598 mtx_unlock(&ie->ie_lock);
604 /* Create a thread if we need one. */
605 while (ie->ie_thread == NULL && handler != NULL) {
606 if (ie->ie_flags & IE_ADDING_THREAD)
607 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
609 ie->ie_flags |= IE_ADDING_THREAD;
610 mtx_unlock(&ie->ie_lock);
611 it = ithread_create("intr: newborn");
612 mtx_lock(&ie->ie_lock);
613 ie->ie_flags &= ~IE_ADDING_THREAD;
621 /* Add the new handler to the event in priority order. */
622 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
623 if (temp_ih->ih_pri > ih->ih_pri)
626 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
628 intr_event_update(ie);
630 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
632 mtx_unlock(&ie->ie_lock);
640 * Append a description preceded by a ':' to the name of the specified
644 intr_event_describe_handler(struct intr_event *ie, void *cookie,
647 struct intr_handler *ih;
651 mtx_lock(&ie->ie_lock);
653 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
658 mtx_unlock(&ie->ie_lock);
659 panic("handler %p not found in interrupt event %p", cookie, ie);
665 * Look for an existing description by checking for an
666 * existing ":". This assumes device names do not include
667 * colons. If one is found, prepare to insert the new
668 * description at that point. If one is not found, find the
669 * end of the name to use as the insertion point.
671 start = strchr(ih->ih_name, ':');
673 start = strchr(ih->ih_name, 0);
676 * See if there is enough remaining room in the string for the
677 * description + ":". The "- 1" leaves room for the trailing
678 * '\0'. The "+ 1" accounts for the colon.
680 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
681 if (strlen(descr) + 1 > space) {
682 mtx_unlock(&ie->ie_lock);
686 /* Append a colon followed by the description. */
688 strcpy(start + 1, descr);
689 intr_event_update(ie);
690 mtx_unlock(&ie->ie_lock);
695 * Return the ie_source field from the intr_event an intr_handler is
699 intr_handler_source(void *cookie)
701 struct intr_handler *ih;
702 struct intr_event *ie;
704 ih = (struct intr_handler *)cookie;
709 ("interrupt handler \"%s\" has a NULL interrupt event",
711 return (ie->ie_source);
715 * If intr_event_handle() is running in the ISR context at the time of the call,
716 * then wait for it to complete.
719 intr_event_barrier(struct intr_event *ie)
723 mtx_assert(&ie->ie_lock, MA_OWNED);
724 phase = ie->ie_phase;
727 * Switch phase to direct future interrupts to the other active counter.
728 * Make sure that any preceding stores are visible before the switch.
730 KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity"));
731 atomic_store_rel_int(&ie->ie_phase, !phase);
734 * This code cooperates with wait-free iteration of ie_handlers
735 * in intr_event_handle.
736 * Make sure that the removal and the phase update are not reordered
737 * with the active count check.
738 * Note that no combination of acquire and release fences can provide
739 * that guarantee as Store->Load sequences can always be reordered.
741 atomic_thread_fence_seq_cst();
744 * Now wait on the inactive phase.
745 * The acquire fence is needed so that that all post-barrier accesses
746 * are after the check.
748 while (ie->ie_active[phase] > 0)
750 atomic_thread_fence_acq();
754 intr_handler_barrier(struct intr_handler *handler)
756 struct intr_event *ie;
758 ie = handler->ih_event;
759 mtx_assert(&ie->ie_lock, MA_OWNED);
760 KASSERT((handler->ih_flags & IH_DEAD) == 0,
761 ("update for a removed handler"));
763 if (ie->ie_thread == NULL) {
764 intr_event_barrier(ie);
767 if ((handler->ih_flags & IH_CHANGED) == 0) {
768 handler->ih_flags |= IH_CHANGED;
769 intr_event_schedule_thread(ie);
771 while ((handler->ih_flags & IH_CHANGED) != 0)
772 msleep(handler, &ie->ie_lock, 0, "ih_barr", 0);
776 * Sleep until an ithread finishes executing an interrupt handler.
778 * XXX Doesn't currently handle interrupt filters or fast interrupt
779 * handlers. This is intended for compatibility with linux drivers
780 * only. Do not use in BSD code.
785 struct intr_event *ie;
786 struct intr_thread *ithd;
789 ie = intr_lookup(irq);
792 if (ie->ie_thread == NULL)
794 ithd = ie->ie_thread;
795 td = ithd->it_thread;
797 * We set the flag and wait for it to be cleared to avoid
798 * long delays with potentially busy interrupt handlers
799 * were we to only sample TD_AWAITING_INTR() every tick.
802 if (!TD_AWAITING_INTR(td)) {
803 ithd->it_flags |= IT_WAIT;
804 while (ithd->it_flags & IT_WAIT) {
815 intr_event_remove_handler(void *cookie)
817 struct intr_handler *handler = (struct intr_handler *)cookie;
818 struct intr_event *ie;
819 struct intr_handler *ih;
820 struct intr_handler **prevptr;
827 ie = handler->ih_event;
829 ("interrupt handler \"%s\" has a NULL interrupt event",
832 mtx_lock(&ie->ie_lock);
833 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
835 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
840 panic("interrupt handler \"%s\" not found in "
841 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
845 * If there is no ithread, then directly remove the handler. Note that
846 * intr_event_handle() iterates ie_handlers in a lock-less fashion, so
847 * care needs to be taken to keep ie_handlers consistent and to free
848 * the removed handler only when ie_handlers is quiescent.
850 if (ie->ie_thread == NULL) {
851 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
852 intr_event_barrier(ie);
853 intr_event_update(ie);
854 mtx_unlock(&ie->ie_lock);
855 free(handler, M_ITHREAD);
860 * Let the interrupt thread do the job.
861 * The interrupt source is disabled when the interrupt thread is
862 * running, so it does not have to worry about interaction with
863 * intr_event_handle().
865 KASSERT((handler->ih_flags & IH_DEAD) == 0,
866 ("duplicate handle remove"));
867 handler->ih_flags |= IH_DEAD;
868 intr_event_schedule_thread(ie);
869 while (handler->ih_flags & IH_DEAD)
870 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
871 intr_event_update(ie);
875 * XXX: This could be bad in the case of ppbus(8). Also, I think
876 * this could lead to races of stale data when servicing an
880 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
881 if (ih->ih_handler != NULL) {
887 ithread_destroy(ie->ie_thread);
888 ie->ie_thread = NULL;
891 mtx_unlock(&ie->ie_lock);
892 free(handler, M_ITHREAD);
897 intr_event_suspend_handler(void *cookie)
899 struct intr_handler *handler = (struct intr_handler *)cookie;
900 struct intr_event *ie;
904 ie = handler->ih_event;
906 ("interrupt handler \"%s\" has a NULL interrupt event",
908 mtx_lock(&ie->ie_lock);
909 handler->ih_flags |= IH_SUSP;
910 intr_handler_barrier(handler);
911 mtx_unlock(&ie->ie_lock);
916 intr_event_resume_handler(void *cookie)
918 struct intr_handler *handler = (struct intr_handler *)cookie;
919 struct intr_event *ie;
923 ie = handler->ih_event;
925 ("interrupt handler \"%s\" has a NULL interrupt event",
929 * intr_handler_barrier() acts not only as a barrier,
930 * it also allows to check for any pending interrupts.
932 mtx_lock(&ie->ie_lock);
933 handler->ih_flags &= ~IH_SUSP;
934 intr_handler_barrier(handler);
935 mtx_unlock(&ie->ie_lock);
940 intr_event_schedule_thread(struct intr_event *ie)
942 struct intr_entropy entropy;
943 struct intr_thread *it;
948 * If no ithread or no handlers, then we have a stray interrupt.
950 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
951 ie->ie_thread == NULL)
959 * If any of the handlers for this ithread claim to be good
960 * sources of entropy, then gather some.
962 if (ie->ie_flags & IE_ENTROPY) {
963 entropy.event = (uintptr_t)ie;
965 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT);
968 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
971 * Set it_need to tell the thread to keep running if it is already
972 * running. Then, lock the thread and see if we actually need to
973 * put it on the runqueue.
975 * Use store_rel to arrange that the store to ih_need in
976 * swi_sched() is before the store to it_need and prepare for
977 * transfer of this order to loads in the ithread.
979 atomic_store_rel_int(&it->it_need, 1);
981 if (TD_AWAITING_INTR(td)) {
982 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
985 sched_add(td, SRQ_INTR);
987 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
988 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
996 * Allow interrupt event binding for software interrupt handlers -- a no-op,
997 * since interrupts are generated in software rather than being directed by
1001 swi_assign_cpu(void *arg, int cpu)
1008 * Add a software interrupt handler to a specified event. If a given event
1009 * is not specified, then a new event is created.
1012 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1013 void *arg, int pri, enum intr_type flags, void **cookiep)
1015 struct intr_event *ie;
1018 if (flags & INTR_ENTROPY)
1021 ie = (eventp != NULL) ? *eventp : NULL;
1024 if (!(ie->ie_flags & IE_SOFT))
1027 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1028 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1034 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1035 PI_SWI(pri), flags, cookiep);
1040 * Schedule a software interrupt thread.
1043 swi_sched(void *cookie, int flags)
1045 struct intr_handler *ih = (struct intr_handler *)cookie;
1046 struct intr_event *ie = ih->ih_event;
1047 struct intr_entropy entropy;
1050 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1053 entropy.event = (uintptr_t)ih;
1054 entropy.td = curthread;
1055 random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI);
1058 * Set ih_need for this handler so that if the ithread is already
1059 * running it will execute this handler on the next pass. Otherwise,
1060 * it will execute it the next time it runs.
1064 if (!(flags & SWI_DELAY)) {
1066 error = intr_event_schedule_thread(ie);
1067 KASSERT(error == 0, ("stray software interrupt"));
1072 * Remove a software interrupt handler. Currently this code does not
1073 * remove the associated interrupt event if it becomes empty. Calling code
1074 * may do so manually via intr_event_destroy(), but that's not really
1075 * an optimal interface.
1078 swi_remove(void *cookie)
1081 return (intr_event_remove_handler(cookie));
1085 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1087 struct intr_handler *ih, *ihn, *ihp;
1090 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1092 * If this handler is marked for death, remove it from
1093 * the list of handlers and wake up the sleeper.
1095 if (ih->ih_flags & IH_DEAD) {
1096 mtx_lock(&ie->ie_lock);
1098 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
1100 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
1101 ih->ih_flags &= ~IH_DEAD;
1103 mtx_unlock(&ie->ie_lock);
1108 * Now that we know that the current element won't be removed
1109 * update the previous element.
1113 if ((ih->ih_flags & IH_CHANGED) != 0) {
1114 mtx_lock(&ie->ie_lock);
1115 ih->ih_flags &= ~IH_CHANGED;
1117 mtx_unlock(&ie->ie_lock);
1120 /* Skip filter only handlers */
1121 if (ih->ih_handler == NULL)
1124 /* Skip suspended handlers */
1125 if ((ih->ih_flags & IH_SUSP) != 0)
1129 * For software interrupt threads, we only execute
1130 * handlers that have their need flag set. Hardware
1131 * interrupt threads always invoke all of their handlers.
1133 * ih_need can only be 0 or 1. Failed cmpset below
1134 * means that there is no request to execute handlers,
1135 * so a retry of the cmpset is not needed.
1137 if ((ie->ie_flags & IE_SOFT) != 0 &&
1138 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1141 /* Execute this handler. */
1142 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1143 __func__, p->p_pid, (void *)ih->ih_handler,
1144 ih->ih_argument, ih->ih_name, ih->ih_flags);
1146 if (!(ih->ih_flags & IH_MPSAFE))
1148 ih->ih_handler(ih->ih_argument);
1149 if (!(ih->ih_flags & IH_MPSAFE))
1155 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1158 /* Interrupt handlers should not sleep. */
1159 if (!(ie->ie_flags & IE_SOFT))
1160 THREAD_NO_SLEEPING();
1161 intr_event_execute_handlers(p, ie);
1162 if (!(ie->ie_flags & IE_SOFT))
1163 THREAD_SLEEPING_OK();
1166 * Interrupt storm handling:
1168 * If this interrupt source is currently storming, then throttle
1169 * it to only fire the handler once per clock tick.
1171 * If this interrupt source is not currently storming, but the
1172 * number of back to back interrupts exceeds the storm threshold,
1173 * then enter storming mode.
1175 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1176 !(ie->ie_flags & IE_SOFT)) {
1177 /* Report the message only once every second. */
1178 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1180 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1188 * Now that all the handlers have had a chance to run, reenable
1189 * the interrupt source.
1191 if (ie->ie_post_ithread != NULL)
1192 ie->ie_post_ithread(ie->ie_source);
1196 * This is the main code for interrupt threads.
1199 ithread_loop(void *arg)
1201 struct intr_thread *ithd;
1202 struct intr_event *ie;
1209 ithd = (struct intr_thread *)arg;
1210 KASSERT(ithd->it_thread == td,
1211 ("%s: ithread and proc linkage out of sync", __func__));
1212 ie = ithd->it_event;
1217 * As long as we have interrupts outstanding, go through the
1218 * list of handlers, giving each one a go at it.
1222 * If we are an orphaned thread, then just die.
1224 if (ithd->it_flags & IT_DEAD) {
1225 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1226 p->p_pid, td->td_name);
1227 free(ithd, M_ITHREAD);
1232 * Service interrupts. If another interrupt arrives while
1233 * we are running, it will set it_need to note that we
1234 * should make another pass.
1236 * The load_acq part of the following cmpset ensures
1237 * that the load of ih_need in ithread_execute_handlers()
1238 * is ordered after the load of it_need here.
1240 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0)
1241 ithread_execute_handlers(p, ie);
1242 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1243 mtx_assert(&Giant, MA_NOTOWNED);
1246 * Processed all our interrupts. Now get the sched
1247 * lock. This may take a while and it_need may get
1248 * set again, so we have to check it again.
1251 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1252 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1255 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1257 if (ithd->it_flags & IT_WAIT) {
1259 ithd->it_flags &= ~IT_WAIT;
1270 * Main interrupt handling body.
1273 * o ie: the event connected to this interrupt.
1274 * o frame: some archs (i.e. i386) pass a frame to some.
1275 * handlers as their main argument.
1277 * o 0: everything ok.
1278 * o EINVAL: stray interrupt.
1281 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1283 struct intr_handler *ih;
1284 struct trapframe *oldframe;
1288 bool filter, thread;
1292 #ifdef KSTACK_USAGE_PROF
1293 intr_prof_stack_use(td, frame);
1296 /* An interrupt with no event or handlers is a stray interrupt. */
1297 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1301 * Execute fast interrupt handlers directly.
1302 * To support clock handlers, if a handler registers
1303 * with a NULL argument, then we pass it a pointer to
1304 * a trapframe as its argument.
1306 td->td_intr_nesting_level++;
1311 oldframe = td->td_intr_frame;
1312 td->td_intr_frame = frame;
1314 phase = ie->ie_phase;
1315 atomic_add_int(&ie->ie_active[phase], 1);
1318 * This fence is required to ensure that no later loads are
1319 * re-ordered before the ie_active store.
1321 atomic_thread_fence_seq_cst();
1323 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1324 if ((ih->ih_flags & IH_SUSP) != 0)
1326 if (ih->ih_filter == NULL) {
1330 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1331 ih->ih_filter, ih->ih_argument == NULL ? frame :
1332 ih->ih_argument, ih->ih_name);
1333 if (ih->ih_argument == NULL)
1334 ret = ih->ih_filter(frame);
1336 ret = ih->ih_filter(ih->ih_argument);
1337 KASSERT(ret == FILTER_STRAY ||
1338 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1339 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1340 ("%s: incorrect return value %#x from %s", __func__, ret,
1342 filter = filter || ret == FILTER_HANDLED;
1345 * Wrapper handler special handling:
1347 * in some particular cases (like pccard and pccbb),
1348 * the _real_ device handler is wrapped in a couple of
1349 * functions - a filter wrapper and an ithread wrapper.
1350 * In this case (and just in this case), the filter wrapper
1351 * could ask the system to schedule the ithread and mask
1352 * the interrupt source if the wrapped handler is composed
1353 * of just an ithread handler.
1355 * TODO: write a generic wrapper to avoid people rolling
1359 if (ret == FILTER_SCHEDULE_THREAD)
1363 atomic_add_rel_int(&ie->ie_active[phase], -1);
1365 td->td_intr_frame = oldframe;
1368 if (ie->ie_pre_ithread != NULL)
1369 ie->ie_pre_ithread(ie->ie_source);
1371 if (ie->ie_post_filter != NULL)
1372 ie->ie_post_filter(ie->ie_source);
1375 /* Schedule the ithread if needed. */
1379 error = intr_event_schedule_thread(ie);
1380 KASSERT(error == 0, ("bad stray interrupt"));
1383 td->td_intr_nesting_level--;
1385 /* The interrupt is not aknowledged by any filter and has no ithread. */
1386 if (!thread && !filter)
1394 * Dump details about an interrupt handler
1397 db_dump_intrhand(struct intr_handler *ih)
1401 db_printf("\t%-10s ", ih->ih_name);
1402 switch (ih->ih_pri) {
1422 if (ih->ih_pri >= PI_SOFT)
1425 db_printf("%4u", ih->ih_pri);
1429 if (ih->ih_filter != NULL) {
1431 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1433 if (ih->ih_handler != NULL) {
1434 if (ih->ih_filter != NULL)
1437 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1439 db_printf("(%p)", ih->ih_argument);
1441 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1445 if (ih->ih_flags & IH_EXCLUSIVE) {
1451 if (ih->ih_flags & IH_ENTROPY) {
1454 db_printf("ENTROPY");
1457 if (ih->ih_flags & IH_DEAD) {
1463 if (ih->ih_flags & IH_MPSAFE) {
1466 db_printf("MPSAFE");
1480 * Dump details about a event.
1483 db_dump_intr_event(struct intr_event *ie, int handlers)
1485 struct intr_handler *ih;
1486 struct intr_thread *it;
1489 db_printf("%s ", ie->ie_fullname);
1492 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1494 db_printf("(no thread)");
1495 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1496 (it != NULL && it->it_need)) {
1499 if (ie->ie_flags & IE_SOFT) {
1503 if (ie->ie_flags & IE_ENTROPY) {
1506 db_printf("ENTROPY");
1509 if (ie->ie_flags & IE_ADDING_THREAD) {
1512 db_printf("ADDING_THREAD");
1515 if (it != NULL && it->it_need) {
1525 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1526 db_dump_intrhand(ih);
1530 * Dump data about interrupt handlers
1532 DB_SHOW_COMMAND(intr, db_show_intr)
1534 struct intr_event *ie;
1537 verbose = strchr(modif, 'v') != NULL;
1538 all = strchr(modif, 'a') != NULL;
1539 TAILQ_FOREACH(ie, &event_list, ie_list) {
1540 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1542 db_dump_intr_event(ie, verbose);
1550 * Start standard software interrupt threads
1553 start_softintr(void *dummy)
1556 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1557 panic("died while creating vm swi ithread");
1559 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1563 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1564 * The data for this machine dependent, and the declarations are in machine
1565 * dependent code. The layout of intrnames and intrcnt however is machine
1568 * We do not know the length of intrcnt and intrnames at compile time, so
1569 * calculate things at run time.
1572 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1574 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1577 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1578 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1581 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1584 uint32_t *intrcnt32;
1588 if (req->flags & SCTL_MASK32) {
1590 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1591 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1592 if (intrcnt32 == NULL)
1594 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1595 intrcnt32[i] = intrcnt[i];
1596 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1597 free(intrcnt32, M_TEMP);
1601 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1604 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1605 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1609 * DDB command to dump the interrupt statistics.
1611 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1619 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1624 db_printf("%s\t%lu\n", cp, *i);
1625 cp += strlen(cp) + 1;