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 = 1000;
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;
164 /* Determine the overall priority of this event. */
165 if (TAILQ_EMPTY(&ie->ie_handlers))
168 pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
170 /* Update name and priority. */
171 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
173 sched_clear_tdname(td);
181 * Regenerate the full name of an interrupt event and update its priority.
184 intr_event_update(struct intr_event *ie)
186 struct intr_handler *ih;
190 /* Start off with no entropy and just the name of the event. */
191 mtx_assert(&ie->ie_lock, MA_OWNED);
192 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
193 ie->ie_flags &= ~IE_ENTROPY;
197 /* Run through all the handlers updating values. */
198 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
199 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
200 sizeof(ie->ie_fullname)) {
201 strcat(ie->ie_fullname, " ");
202 strcat(ie->ie_fullname, ih->ih_name);
206 if (ih->ih_flags & IH_ENTROPY)
207 ie->ie_flags |= IE_ENTROPY;
211 * If the handler names were too long, add +'s to indicate missing
212 * names. If we run out of room and still have +'s to add, change
213 * the last character from a + to a *.
215 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
216 while (missed-- > 0) {
217 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
224 strcat(ie->ie_fullname, " +");
227 strcat(ie->ie_fullname, "+");
231 * If this event has an ithread, update it's priority and
234 if (ie->ie_thread != NULL)
235 ithread_update(ie->ie_thread);
236 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
240 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
241 void (*pre_ithread)(void *), void (*post_ithread)(void *),
242 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
243 const char *fmt, ...)
245 struct intr_event *ie;
248 /* The only valid flag during creation is IE_SOFT. */
249 if ((flags & ~IE_SOFT) != 0)
251 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
252 ie->ie_source = source;
253 ie->ie_pre_ithread = pre_ithread;
254 ie->ie_post_ithread = post_ithread;
255 ie->ie_post_filter = post_filter;
256 ie->ie_assign_cpu = assign_cpu;
257 ie->ie_flags = flags;
260 TAILQ_INIT(&ie->ie_handlers);
261 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
264 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
266 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
267 mtx_lock(&event_lock);
268 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
269 mtx_unlock(&event_lock);
272 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
277 * Bind an interrupt event to the specified CPU. Note that not all
278 * platforms support binding an interrupt to a CPU. For those
279 * platforms this request will fail. Using a cpu id of NOCPU unbinds
280 * the interrupt event.
283 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
288 /* Need a CPU to bind to. */
289 if (cpu != NOCPU && CPU_ABSENT(cpu))
292 if (ie->ie_assign_cpu == NULL)
295 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
300 * If we have any ithreads try to set their mask first to verify
304 mtx_lock(&ie->ie_lock);
305 if (ie->ie_thread != NULL) {
306 id = ie->ie_thread->it_thread->td_tid;
307 mtx_unlock(&ie->ie_lock);
308 error = cpuset_setithread(id, cpu);
312 mtx_unlock(&ie->ie_lock);
315 error = ie->ie_assign_cpu(ie->ie_source, cpu);
318 mtx_lock(&ie->ie_lock);
319 if (ie->ie_thread != NULL) {
321 id = ie->ie_thread->it_thread->td_tid;
322 mtx_unlock(&ie->ie_lock);
323 (void)cpuset_setithread(id, cpu);
325 mtx_unlock(&ie->ie_lock);
331 mtx_lock(&ie->ie_lock);
333 mtx_unlock(&ie->ie_lock);
340 * Bind an interrupt event to the specified CPU. For supported platforms, any
341 * associated ithreads as well as the primary interrupt context will be bound
342 * to the specificed CPU.
345 intr_event_bind(struct intr_event *ie, int cpu)
348 return (_intr_event_bind(ie, cpu, true, true));
352 * Bind an interrupt event to the specified CPU, but do not bind associated
356 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
359 return (_intr_event_bind(ie, cpu, true, false));
363 * Bind an interrupt event's ithread to the specified CPU.
366 intr_event_bind_ithread(struct intr_event *ie, int cpu)
369 return (_intr_event_bind(ie, cpu, false, true));
372 static struct intr_event *
375 struct intr_event *ie;
377 mtx_lock(&event_lock);
378 TAILQ_FOREACH(ie, &event_list, ie_list)
379 if (ie->ie_irq == irq &&
380 (ie->ie_flags & IE_SOFT) == 0 &&
381 TAILQ_FIRST(&ie->ie_handlers) != NULL)
383 mtx_unlock(&event_lock);
388 intr_setaffinity(int irq, int mode, void *m)
390 struct intr_event *ie;
397 * If we're setting all cpus we can unbind. Otherwise make sure
398 * only one cpu is in the set.
400 if (CPU_CMP(cpuset_root, mask)) {
401 for (n = 0; n < CPU_SETSIZE; n++) {
402 if (!CPU_ISSET(n, mask))
409 ie = intr_lookup(irq);
414 return (intr_event_bind(ie, cpu));
415 case CPU_WHICH_INTRHANDLER:
416 return (intr_event_bind_irqonly(ie, cpu));
417 case CPU_WHICH_ITHREAD:
418 return (intr_event_bind_ithread(ie, cpu));
425 intr_getaffinity(int irq, int mode, void *m)
427 struct intr_event *ie;
435 ie = intr_lookup(irq);
443 case CPU_WHICH_INTRHANDLER:
444 mtx_lock(&ie->ie_lock);
445 if (ie->ie_cpu == NOCPU)
446 CPU_COPY(cpuset_root, mask);
448 CPU_SET(ie->ie_cpu, mask);
449 mtx_unlock(&ie->ie_lock);
451 case CPU_WHICH_ITHREAD:
452 mtx_lock(&ie->ie_lock);
453 if (ie->ie_thread == NULL) {
454 mtx_unlock(&ie->ie_lock);
455 CPU_COPY(cpuset_root, mask);
457 id = ie->ie_thread->it_thread->td_tid;
458 mtx_unlock(&ie->ie_lock);
459 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
462 CPU_COPY(&td->td_cpuset->cs_mask, mask);
472 intr_event_destroy(struct intr_event *ie)
475 mtx_lock(&event_lock);
476 mtx_lock(&ie->ie_lock);
477 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
478 mtx_unlock(&ie->ie_lock);
479 mtx_unlock(&event_lock);
482 TAILQ_REMOVE(&event_list, ie, ie_list);
484 if (ie->ie_thread != NULL) {
485 ithread_destroy(ie->ie_thread);
486 ie->ie_thread = NULL;
489 mtx_unlock(&ie->ie_lock);
490 mtx_unlock(&event_lock);
491 mtx_destroy(&ie->ie_lock);
496 static struct intr_thread *
497 ithread_create(const char *name)
499 struct intr_thread *ithd;
503 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
505 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
506 &td, RFSTOPPED | RFHIGHPID,
507 0, "intr", "%s", name);
509 panic("kproc_create() failed with %d", error);
511 sched_class(td, PRI_ITHD);
514 td->td_pflags |= TDP_ITHREAD;
515 ithd->it_thread = td;
516 CTR2(KTR_INTR, "%s: created %s", __func__, name);
521 ithread_destroy(struct intr_thread *ithread)
525 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
526 td = ithread->it_thread;
528 ithread->it_flags |= IT_DEAD;
529 if (TD_AWAITING_INTR(td)) {
531 sched_add(td, SRQ_INTR);
537 intr_event_add_handler(struct intr_event *ie, const char *name,
538 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
539 enum intr_type flags, void **cookiep)
541 struct intr_handler *ih, *temp_ih;
542 struct intr_thread *it;
544 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
547 /* Allocate and populate an interrupt handler structure. */
548 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
549 ih->ih_filter = filter;
550 ih->ih_handler = handler;
551 ih->ih_argument = arg;
552 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
555 if (flags & INTR_EXCL)
556 ih->ih_flags = IH_EXCLUSIVE;
557 if (flags & INTR_MPSAFE)
558 ih->ih_flags |= IH_MPSAFE;
559 if (flags & INTR_ENTROPY)
560 ih->ih_flags |= IH_ENTROPY;
562 /* We can only have one exclusive handler in a event. */
563 mtx_lock(&ie->ie_lock);
564 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
565 if ((flags & INTR_EXCL) ||
566 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
567 mtx_unlock(&ie->ie_lock);
573 /* Create a thread if we need one. */
574 while (ie->ie_thread == NULL && handler != NULL) {
575 if (ie->ie_flags & IE_ADDING_THREAD)
576 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
578 ie->ie_flags |= IE_ADDING_THREAD;
579 mtx_unlock(&ie->ie_lock);
580 it = ithread_create("intr: newborn");
581 mtx_lock(&ie->ie_lock);
582 ie->ie_flags &= ~IE_ADDING_THREAD;
590 /* Add the new handler to the event in priority order. */
591 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
592 if (temp_ih->ih_pri > ih->ih_pri)
596 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
598 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
599 intr_event_update(ie);
601 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
603 mtx_unlock(&ie->ie_lock);
611 * Append a description preceded by a ':' to the name of the specified
615 intr_event_describe_handler(struct intr_event *ie, void *cookie,
618 struct intr_handler *ih;
622 mtx_lock(&ie->ie_lock);
624 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
629 mtx_unlock(&ie->ie_lock);
630 panic("handler %p not found in interrupt event %p", cookie, ie);
636 * Look for an existing description by checking for an
637 * existing ":". This assumes device names do not include
638 * colons. If one is found, prepare to insert the new
639 * description at that point. If one is not found, find the
640 * end of the name to use as the insertion point.
642 start = strchr(ih->ih_name, ':');
644 start = strchr(ih->ih_name, 0);
647 * See if there is enough remaining room in the string for the
648 * description + ":". The "- 1" leaves room for the trailing
649 * '\0'. The "+ 1" accounts for the colon.
651 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
652 if (strlen(descr) + 1 > space) {
653 mtx_unlock(&ie->ie_lock);
657 /* Append a colon followed by the description. */
659 strcpy(start + 1, descr);
660 intr_event_update(ie);
661 mtx_unlock(&ie->ie_lock);
666 * Return the ie_source field from the intr_event an intr_handler is
670 intr_handler_source(void *cookie)
672 struct intr_handler *ih;
673 struct intr_event *ie;
675 ih = (struct intr_handler *)cookie;
680 ("interrupt handler \"%s\" has a NULL interrupt event",
682 return (ie->ie_source);
686 * Sleep until an ithread finishes executing an interrupt handler.
688 * XXX Doesn't currently handle interrupt filters or fast interrupt
689 * handlers. This is intended for compatibility with linux drivers
690 * only. Do not use in BSD code.
695 struct intr_event *ie;
696 struct intr_thread *ithd;
699 ie = intr_lookup(irq);
702 if (ie->ie_thread == NULL)
704 ithd = ie->ie_thread;
705 td = ithd->it_thread;
707 * We set the flag and wait for it to be cleared to avoid
708 * long delays with potentially busy interrupt handlers
709 * were we to only sample TD_AWAITING_INTR() every tick.
712 if (!TD_AWAITING_INTR(td)) {
713 ithd->it_flags |= IT_WAIT;
714 while (ithd->it_flags & IT_WAIT) {
726 intr_event_remove_handler(void *cookie)
728 struct intr_handler *handler = (struct intr_handler *)cookie;
729 struct intr_event *ie;
731 struct intr_handler *ih;
739 ie = handler->ih_event;
741 ("interrupt handler \"%s\" has a NULL interrupt event",
743 mtx_lock(&ie->ie_lock);
744 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
747 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
750 mtx_unlock(&ie->ie_lock);
751 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
752 ih->ih_name, ie->ie_name);
756 * If there is no ithread, then just remove the handler and return.
757 * XXX: Note that an INTR_FAST handler might be running on another
760 if (ie->ie_thread == NULL) {
761 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
762 mtx_unlock(&ie->ie_lock);
763 free(handler, M_ITHREAD);
768 * If the interrupt thread is already running, then just mark this
769 * handler as being dead and let the ithread do the actual removal.
771 * During a cold boot while cold is set, msleep() does not sleep,
772 * so we have to remove the handler here rather than letting the
775 thread_lock(ie->ie_thread->it_thread);
776 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
777 handler->ih_flags |= IH_DEAD;
780 * Ensure that the thread will process the handler list
781 * again and remove this handler if it has already passed
784 * The release part of the following store ensures
785 * that the update of ih_flags is ordered before the
786 * it_need setting. See the comment before
787 * atomic_cmpset_acq(&ithd->it_need, ...) operation in
788 * the ithread_execute_handlers().
790 atomic_store_rel_int(&ie->ie_thread->it_need, 1);
792 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
793 thread_unlock(ie->ie_thread->it_thread);
794 while (handler->ih_flags & IH_DEAD)
795 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
796 intr_event_update(ie);
799 * XXX: This could be bad in the case of ppbus(8). Also, I think
800 * this could lead to races of stale data when servicing an
804 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
805 if (!(ih->ih_flags & IH_FAST)) {
811 ithread_destroy(ie->ie_thread);
812 ie->ie_thread = NULL;
815 mtx_unlock(&ie->ie_lock);
816 free(handler, M_ITHREAD);
821 intr_event_schedule_thread(struct intr_event *ie)
823 struct intr_entropy entropy;
824 struct intr_thread *it;
829 * If no ithread or no handlers, then we have a stray interrupt.
831 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
832 ie->ie_thread == NULL)
840 * If any of the handlers for this ithread claim to be good
841 * sources of entropy, then gather some.
843 if (ie->ie_flags & IE_ENTROPY) {
844 entropy.event = (uintptr_t)ie;
846 random_harvest_queue(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT);
849 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
852 * Set it_need to tell the thread to keep running if it is already
853 * running. Then, lock the thread and see if we actually need to
854 * put it on the runqueue.
856 * Use store_rel to arrange that the store to ih_need in
857 * swi_sched() is before the store to it_need and prepare for
858 * transfer of this order to loads in the ithread.
860 atomic_store_rel_int(&it->it_need, 1);
862 if (TD_AWAITING_INTR(td)) {
863 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
866 sched_add(td, SRQ_INTR);
868 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
869 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
877 * Allow interrupt event binding for software interrupt handlers -- a no-op,
878 * since interrupts are generated in software rather than being directed by
882 swi_assign_cpu(void *arg, int cpu)
889 * Add a software interrupt handler to a specified event. If a given event
890 * is not specified, then a new event is created.
893 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
894 void *arg, int pri, enum intr_type flags, void **cookiep)
896 struct intr_event *ie;
899 if (flags & INTR_ENTROPY)
902 ie = (eventp != NULL) ? *eventp : NULL;
905 if (!(ie->ie_flags & IE_SOFT))
908 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
909 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
915 error = intr_event_add_handler(ie, name, NULL, handler, arg,
916 PI_SWI(pri), flags, cookiep);
921 * Schedule a software interrupt thread.
924 swi_sched(void *cookie, int flags)
926 struct intr_handler *ih = (struct intr_handler *)cookie;
927 struct intr_event *ie = ih->ih_event;
928 struct intr_entropy entropy;
931 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
934 entropy.event = (uintptr_t)ih;
935 entropy.td = curthread;
936 random_harvest_queue(&entropy, sizeof(entropy), 1, RANDOM_SWI);
939 * Set ih_need for this handler so that if the ithread is already
940 * running it will execute this handler on the next pass. Otherwise,
941 * it will execute it the next time it runs.
945 if (!(flags & SWI_DELAY)) {
947 error = intr_event_schedule_thread(ie);
948 KASSERT(error == 0, ("stray software interrupt"));
953 * Remove a software interrupt handler. Currently this code does not
954 * remove the associated interrupt event if it becomes empty. Calling code
955 * may do so manually via intr_event_destroy(), but that's not really
956 * an optimal interface.
959 swi_remove(void *cookie)
962 return (intr_event_remove_handler(cookie));
967 * This is a public function for use by drivers that mux interrupt
968 * handlers for child devices from their interrupt handler.
971 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
973 struct intr_handler *ih, *ihn;
975 TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
977 * If this handler is marked for death, remove it from
978 * the list of handlers and wake up the sleeper.
980 if (ih->ih_flags & IH_DEAD) {
981 mtx_lock(&ie->ie_lock);
982 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
983 ih->ih_flags &= ~IH_DEAD;
985 mtx_unlock(&ie->ie_lock);
989 /* Skip filter only handlers */
990 if (ih->ih_handler == NULL)
994 * For software interrupt threads, we only execute
995 * handlers that have their need flag set. Hardware
996 * interrupt threads always invoke all of their handlers.
998 * ih_need can only be 0 or 1. Failed cmpset below
999 * means that there is no request to execute handlers,
1000 * so a retry of the cmpset is not needed.
1002 if ((ie->ie_flags & IE_SOFT) != 0 &&
1003 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1006 /* Execute this handler. */
1007 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1008 __func__, p->p_pid, (void *)ih->ih_handler,
1009 ih->ih_argument, ih->ih_name, ih->ih_flags);
1011 if (!(ih->ih_flags & IH_MPSAFE))
1013 ih->ih_handler(ih->ih_argument);
1014 if (!(ih->ih_flags & IH_MPSAFE))
1020 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1023 /* Interrupt handlers should not sleep. */
1024 if (!(ie->ie_flags & IE_SOFT))
1025 THREAD_NO_SLEEPING();
1026 intr_event_execute_handlers(p, ie);
1027 if (!(ie->ie_flags & IE_SOFT))
1028 THREAD_SLEEPING_OK();
1031 * Interrupt storm handling:
1033 * If this interrupt source is currently storming, then throttle
1034 * it to only fire the handler once per clock tick.
1036 * If this interrupt source is not currently storming, but the
1037 * number of back to back interrupts exceeds the storm threshold,
1038 * then enter storming mode.
1040 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1041 !(ie->ie_flags & IE_SOFT)) {
1042 /* Report the message only once every second. */
1043 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1045 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1053 * Now that all the handlers have had a chance to run, reenable
1054 * the interrupt source.
1056 if (ie->ie_post_ithread != NULL)
1057 ie->ie_post_ithread(ie->ie_source);
1061 * This is the main code for interrupt threads.
1064 ithread_loop(void *arg)
1066 struct intr_thread *ithd;
1067 struct intr_event *ie;
1074 ithd = (struct intr_thread *)arg;
1075 KASSERT(ithd->it_thread == td,
1076 ("%s: ithread and proc linkage out of sync", __func__));
1077 ie = ithd->it_event;
1082 * As long as we have interrupts outstanding, go through the
1083 * list of handlers, giving each one a go at it.
1087 * If we are an orphaned thread, then just die.
1089 if (ithd->it_flags & IT_DEAD) {
1090 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1091 p->p_pid, td->td_name);
1092 free(ithd, M_ITHREAD);
1097 * Service interrupts. If another interrupt arrives while
1098 * we are running, it will set it_need to note that we
1099 * should make another pass.
1101 * The load_acq part of the following cmpset ensures
1102 * that the load of ih_need in ithread_execute_handlers()
1103 * is ordered after the load of it_need here.
1105 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0)
1106 ithread_execute_handlers(p, ie);
1107 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1108 mtx_assert(&Giant, MA_NOTOWNED);
1111 * Processed all our interrupts. Now get the sched
1112 * lock. This may take a while and it_need may get
1113 * set again, so we have to check it again.
1116 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1117 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1120 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1122 if (ithd->it_flags & IT_WAIT) {
1124 ithd->it_flags &= ~IT_WAIT;
1135 * Main interrupt handling body.
1138 * o ie: the event connected to this interrupt.
1139 * o frame: some archs (i.e. i386) pass a frame to some.
1140 * handlers as their main argument.
1142 * o 0: everything ok.
1143 * o EINVAL: stray interrupt.
1146 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1148 struct intr_handler *ih;
1149 struct trapframe *oldframe;
1155 #ifdef KSTACK_USAGE_PROF
1156 intr_prof_stack_use(td, frame);
1159 /* An interrupt with no event or handlers is a stray interrupt. */
1160 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1164 * Execute fast interrupt handlers directly.
1165 * To support clock handlers, if a handler registers
1166 * with a NULL argument, then we pass it a pointer to
1167 * a trapframe as its argument.
1169 td->td_intr_nesting_level++;
1173 oldframe = td->td_intr_frame;
1174 td->td_intr_frame = frame;
1175 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1176 if (ih->ih_filter == NULL) {
1180 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1181 ih->ih_filter, ih->ih_argument == NULL ? frame :
1182 ih->ih_argument, ih->ih_name);
1183 if (ih->ih_argument == NULL)
1184 ret = ih->ih_filter(frame);
1186 ret = ih->ih_filter(ih->ih_argument);
1187 KASSERT(ret == FILTER_STRAY ||
1188 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1189 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1190 ("%s: incorrect return value %#x from %s", __func__, ret,
1194 * Wrapper handler special handling:
1196 * in some particular cases (like pccard and pccbb),
1197 * the _real_ device handler is wrapped in a couple of
1198 * functions - a filter wrapper and an ithread wrapper.
1199 * In this case (and just in this case), the filter wrapper
1200 * could ask the system to schedule the ithread and mask
1201 * the interrupt source if the wrapped handler is composed
1202 * of just an ithread handler.
1204 * TODO: write a generic wrapper to avoid people rolling
1208 if (ret == FILTER_SCHEDULE_THREAD)
1212 td->td_intr_frame = oldframe;
1215 if (ie->ie_pre_ithread != NULL)
1216 ie->ie_pre_ithread(ie->ie_source);
1218 if (ie->ie_post_filter != NULL)
1219 ie->ie_post_filter(ie->ie_source);
1222 /* Schedule the ithread if needed. */
1226 error = intr_event_schedule_thread(ie);
1227 KASSERT(error == 0, ("bad stray interrupt"));
1230 td->td_intr_nesting_level--;
1236 * Dump details about an interrupt handler
1239 db_dump_intrhand(struct intr_handler *ih)
1243 db_printf("\t%-10s ", ih->ih_name);
1244 switch (ih->ih_pri) {
1264 if (ih->ih_pri >= PI_SOFT)
1267 db_printf("%4u", ih->ih_pri);
1271 if (ih->ih_filter != NULL) {
1273 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1275 if (ih->ih_handler != NULL) {
1276 if (ih->ih_filter != NULL)
1279 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1281 db_printf("(%p)", ih->ih_argument);
1283 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1287 if (ih->ih_flags & IH_EXCLUSIVE) {
1293 if (ih->ih_flags & IH_ENTROPY) {
1296 db_printf("ENTROPY");
1299 if (ih->ih_flags & IH_DEAD) {
1305 if (ih->ih_flags & IH_MPSAFE) {
1308 db_printf("MPSAFE");
1322 * Dump details about a event.
1325 db_dump_intr_event(struct intr_event *ie, int handlers)
1327 struct intr_handler *ih;
1328 struct intr_thread *it;
1331 db_printf("%s ", ie->ie_fullname);
1334 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1336 db_printf("(no thread)");
1337 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1338 (it != NULL && it->it_need)) {
1341 if (ie->ie_flags & IE_SOFT) {
1345 if (ie->ie_flags & IE_ENTROPY) {
1348 db_printf("ENTROPY");
1351 if (ie->ie_flags & IE_ADDING_THREAD) {
1354 db_printf("ADDING_THREAD");
1357 if (it != NULL && it->it_need) {
1367 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1368 db_dump_intrhand(ih);
1372 * Dump data about interrupt handlers
1374 DB_SHOW_COMMAND(intr, db_show_intr)
1376 struct intr_event *ie;
1379 verbose = strchr(modif, 'v') != NULL;
1380 all = strchr(modif, 'a') != NULL;
1381 TAILQ_FOREACH(ie, &event_list, ie_list) {
1382 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1384 db_dump_intr_event(ie, verbose);
1392 * Start standard software interrupt threads
1395 start_softintr(void *dummy)
1398 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1399 panic("died while creating vm swi ithread");
1401 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1405 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1406 * The data for this machine dependent, and the declarations are in machine
1407 * dependent code. The layout of intrnames and intrcnt however is machine
1410 * We do not know the length of intrcnt and intrnames at compile time, so
1411 * calculate things at run time.
1414 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1416 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1419 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1420 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1423 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1426 uint32_t *intrcnt32;
1430 if (req->flags & SCTL_MASK32) {
1432 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1433 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1434 if (intrcnt32 == NULL)
1436 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1437 intrcnt32[i] = intrcnt[i];
1438 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1439 free(intrcnt32, M_TEMP);
1443 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1446 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1447 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1451 * DDB command to dump the interrupt statistics.
1453 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1461 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1466 db_printf("%s\t%lu\n", cp, *i);
1467 cp += strlen(cp) + 1;