1425f9fdaSStefan Eßer /* 2425f9fdaSStefan Eßer * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 3425f9fdaSStefan Eßer * All rights reserved. 4425f9fdaSStefan Eßer * 5425f9fdaSStefan Eßer * Redistribution and use in source and binary forms, with or without 6425f9fdaSStefan Eßer * modification, are permitted provided that the following conditions 7425f9fdaSStefan Eßer * are met: 8425f9fdaSStefan Eßer * 1. Redistributions of source code must retain the above copyright 9425f9fdaSStefan Eßer * notice unmodified, this list of conditions, and the following 10425f9fdaSStefan Eßer * disclaimer. 11425f9fdaSStefan Eßer * 2. Redistributions in binary form must reproduce the above copyright 12425f9fdaSStefan Eßer * notice, this list of conditions and the following disclaimer in the 13425f9fdaSStefan Eßer * documentation and/or other materials provided with the distribution. 14425f9fdaSStefan Eßer * 15425f9fdaSStefan Eßer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16425f9fdaSStefan Eßer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17425f9fdaSStefan Eßer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18425f9fdaSStefan Eßer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19425f9fdaSStefan Eßer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20425f9fdaSStefan Eßer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21425f9fdaSStefan Eßer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22425f9fdaSStefan Eßer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23425f9fdaSStefan Eßer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24425f9fdaSStefan Eßer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25425f9fdaSStefan Eßer * 26c3aac50fSPeter Wemm * $FreeBSD$ 27425f9fdaSStefan Eßer * 28425f9fdaSStefan Eßer */ 29425f9fdaSStefan Eßer 303900ddb2SDoug Rabson 311c5bb3eaSPeter Wemm #include <sys/param.h> 329a94c9c5SJohn Baldwin #include <sys/bus.h> 339a94c9c5SJohn Baldwin #include <sys/rtprio.h> 34425f9fdaSStefan Eßer #include <sys/systm.h> 3568352337SDoug Rabson #include <sys/interrupt.h> 361931cf94SJohn Baldwin #include <sys/kernel.h> 371931cf94SJohn Baldwin #include <sys/kthread.h> 381931cf94SJohn Baldwin #include <sys/ktr.h> 39f34fa851SJohn Baldwin #include <sys/lock.h> 401931cf94SJohn Baldwin #include <sys/malloc.h> 4135e0e5b3SJohn Baldwin #include <sys/mutex.h> 421931cf94SJohn Baldwin #include <sys/proc.h> 433e5da754SJohn Baldwin #include <sys/random.h> 44b4151f71SJohn Baldwin #include <sys/resourcevar.h> 45d279178dSThomas Moestl #include <sys/sysctl.h> 461931cf94SJohn Baldwin #include <sys/unistd.h> 471931cf94SJohn Baldwin #include <sys/vmmeter.h> 481931cf94SJohn Baldwin #include <machine/atomic.h> 491931cf94SJohn Baldwin #include <machine/cpu.h> 508088699fSJohn Baldwin #include <machine/md_var.h> 51b4151f71SJohn Baldwin #include <machine/stdarg.h> 52425f9fdaSStefan Eßer 533e5da754SJohn Baldwin struct int_entropy { 543e5da754SJohn Baldwin struct proc *proc; 553e5da754SJohn Baldwin int vector; 563e5da754SJohn Baldwin }; 573e5da754SJohn Baldwin 58b4151f71SJohn Baldwin void *vm_ih; 59b4151f71SJohn Baldwin void *softclock_ih; 608088699fSJohn Baldwin struct ithd *clk_ithd; 618088699fSJohn Baldwin struct ithd *tty_ithd; 621931cf94SJohn Baldwin 63b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 64b4151f71SJohn Baldwin 65b4151f71SJohn Baldwin static void ithread_update(struct ithd *); 66b4151f71SJohn Baldwin static void ithread_loop(void *); 671931cf94SJohn Baldwin static void start_softintr(void *); 6818c5a6c4SBruce Evans 69b4151f71SJohn Baldwin u_char 70b4151f71SJohn Baldwin ithread_priority(enum intr_type flags) 719a94c9c5SJohn Baldwin { 72b4151f71SJohn Baldwin u_char pri; 739a94c9c5SJohn Baldwin 74b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 755a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 769a94c9c5SJohn Baldwin switch (flags) { 77b4151f71SJohn Baldwin case INTR_TYPE_TTY: 789a94c9c5SJohn Baldwin pri = PI_TTYLOW; 799a94c9c5SJohn Baldwin break; 809a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 819a94c9c5SJohn Baldwin /* 829a94c9c5SJohn Baldwin * XXX We need to refine this. BSD/OS distinguishes 839a94c9c5SJohn Baldwin * between tape and disk priorities. 849a94c9c5SJohn Baldwin */ 859a94c9c5SJohn Baldwin pri = PI_DISK; 869a94c9c5SJohn Baldwin break; 879a94c9c5SJohn Baldwin case INTR_TYPE_NET: 889a94c9c5SJohn Baldwin pri = PI_NET; 899a94c9c5SJohn Baldwin break; 909a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 919a94c9c5SJohn Baldwin pri = PI_DISK; /* XXX or PI_CAM? */ 929a94c9c5SJohn Baldwin break; 935a280d9cSPeter Wemm case INTR_TYPE_AV: /* Audio/video */ 945a280d9cSPeter Wemm pri = PI_AV; 955a280d9cSPeter Wemm break; 96b4151f71SJohn Baldwin case INTR_TYPE_CLK: 97b4151f71SJohn Baldwin pri = PI_REALTIME; 98b4151f71SJohn Baldwin break; 999a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1009a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1019a94c9c5SJohn Baldwin break; 1029a94c9c5SJohn Baldwin default: 103b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 1049a94c9c5SJohn Baldwin panic("ithread_priority: no interrupt type in flags"); 1059a94c9c5SJohn Baldwin } 1069a94c9c5SJohn Baldwin 1079a94c9c5SJohn Baldwin return pri; 1089a94c9c5SJohn Baldwin } 1099a94c9c5SJohn Baldwin 110b4151f71SJohn Baldwin /* 111b4151f71SJohn Baldwin * Regenerate the name (p_comm) and priority for a threaded interrupt thread. 112b4151f71SJohn Baldwin */ 113b4151f71SJohn Baldwin static void 114b4151f71SJohn Baldwin ithread_update(struct ithd *ithd) 115b4151f71SJohn Baldwin { 116b4151f71SJohn Baldwin struct intrhand *ih; 117b40ce416SJulian Elischer struct thread *td; 118b4151f71SJohn Baldwin struct proc *p; 119b4151f71SJohn Baldwin int entropy; 1208088699fSJohn Baldwin 1214d29cb2dSJohn Baldwin mtx_assert(&ithd->it_lock, MA_OWNED); 122b40ce416SJulian Elischer td = ithd->it_td; 123b40ce416SJulian Elischer if (td == NULL) 124b4151f71SJohn Baldwin return; 125b40ce416SJulian Elischer p = td->td_proc; 126b4151f71SJohn Baldwin 127e80fb434SRobert Drehmel strlcpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name)); 128e80fb434SRobert Drehmel 129b4151f71SJohn Baldwin ih = TAILQ_FIRST(&ithd->it_handlers); 130b4151f71SJohn Baldwin if (ih == NULL) { 131b106d2f5SJohn Baldwin mtx_lock_spin(&sched_lock); 1322c100766SJulian Elischer td->td_priority = PRI_MAX_ITHD; 133b106d2f5SJohn Baldwin td->td_base_pri = PRI_MAX_ITHD; 134b106d2f5SJohn Baldwin mtx_unlock_spin(&sched_lock); 135b4151f71SJohn Baldwin ithd->it_flags &= ~IT_ENTROPY; 136b4151f71SJohn Baldwin return; 137b4151f71SJohn Baldwin } 138b4151f71SJohn Baldwin entropy = 0; 139b106d2f5SJohn Baldwin mtx_lock_spin(&sched_lock); 1402c100766SJulian Elischer td->td_priority = ih->ih_pri; 1412c100766SJulian Elischer td->td_base_pri = ih->ih_pri; 142b106d2f5SJohn Baldwin mtx_unlock_spin(&sched_lock); 143b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 144b4151f71SJohn Baldwin if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 < 145b4151f71SJohn Baldwin sizeof(p->p_comm)) { 146b4151f71SJohn Baldwin strcat(p->p_comm, " "); 147b4151f71SJohn Baldwin strcat(p->p_comm, ih->ih_name); 148b4151f71SJohn Baldwin } else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) { 149b4151f71SJohn Baldwin if (p->p_comm[sizeof(p->p_comm) - 2] == '+') 150b4151f71SJohn Baldwin p->p_comm[sizeof(p->p_comm) - 2] = '*'; 151b4151f71SJohn Baldwin else 152b4151f71SJohn Baldwin p->p_comm[sizeof(p->p_comm) - 2] = '+'; 153b4151f71SJohn Baldwin } else 154b4151f71SJohn Baldwin strcat(p->p_comm, "+"); 155b4151f71SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 156b4151f71SJohn Baldwin entropy++; 157b4151f71SJohn Baldwin } 1583e5da754SJohn Baldwin if (entropy) 159b4151f71SJohn Baldwin ithd->it_flags |= IT_ENTROPY; 160b4151f71SJohn Baldwin else 161b4151f71SJohn Baldwin ithd->it_flags &= ~IT_ENTROPY; 16291f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: updated %s\n", __func__, p->p_comm); 163b4151f71SJohn Baldwin } 164b4151f71SJohn Baldwin 165b4151f71SJohn Baldwin int 166b4151f71SJohn Baldwin ithread_create(struct ithd **ithread, int vector, int flags, 167b4151f71SJohn Baldwin void (*disable)(int), void (*enable)(int), const char *fmt, ...) 168b4151f71SJohn Baldwin { 169b4151f71SJohn Baldwin struct ithd *ithd; 170b40ce416SJulian Elischer struct thread *td; 171b4151f71SJohn Baldwin struct proc *p; 172b4151f71SJohn Baldwin int error; 173b4151f71SJohn Baldwin va_list ap; 174b4151f71SJohn Baldwin 1753e5da754SJohn Baldwin /* The only valid flag during creation is IT_SOFT. */ 1763e5da754SJohn Baldwin if ((flags & ~IT_SOFT) != 0) 1773e5da754SJohn Baldwin return (EINVAL); 1783e5da754SJohn Baldwin 179b4151f71SJohn Baldwin ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO); 180b4151f71SJohn Baldwin ithd->it_vector = vector; 181b4151f71SJohn Baldwin ithd->it_disable = disable; 182b4151f71SJohn Baldwin ithd->it_enable = enable; 183b4151f71SJohn Baldwin ithd->it_flags = flags; 184b4151f71SJohn Baldwin TAILQ_INIT(&ithd->it_handlers); 1856008862bSJohn Baldwin mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF); 186b4151f71SJohn Baldwin 187b4151f71SJohn Baldwin va_start(ap, fmt); 188b4151f71SJohn Baldwin vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap); 189b4151f71SJohn Baldwin va_end(ap); 190b4151f71SJohn Baldwin 191b4151f71SJohn Baldwin error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID, 192316ec49aSScott Long 0, "%s", ithd->it_name); 193b4151f71SJohn Baldwin if (error) { 1944d29cb2dSJohn Baldwin mtx_destroy(&ithd->it_lock); 195b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 196b4151f71SJohn Baldwin return (error); 197b4151f71SJohn Baldwin } 198079b7badSJulian Elischer td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 1992c100766SJulian Elischer td->td_ksegrp->kg_pri_class = PRI_ITHD; 2002c100766SJulian Elischer td->td_priority = PRI_MAX_ITHD; 20171fad9fdSJulian Elischer TD_SET_IWAIT(td); 202b40ce416SJulian Elischer ithd->it_td = td; 203b40ce416SJulian Elischer td->td_ithd = ithd; 204b4151f71SJohn Baldwin if (ithread != NULL) 205b4151f71SJohn Baldwin *ithread = ithd; 206b4151f71SJohn Baldwin 20791f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name); 208b4151f71SJohn Baldwin return (0); 209b4151f71SJohn Baldwin } 210b4151f71SJohn Baldwin 211b4151f71SJohn Baldwin int 212b4151f71SJohn Baldwin ithread_destroy(struct ithd *ithread) 213b4151f71SJohn Baldwin { 214b4151f71SJohn Baldwin 215b40ce416SJulian Elischer struct thread *td; 216b40ce416SJulian Elischer struct proc *p; 2174d29cb2dSJohn Baldwin if (ithread == NULL) 218b4151f71SJohn Baldwin return (EINVAL); 219b4151f71SJohn Baldwin 220b40ce416SJulian Elischer td = ithread->it_td; 221b40ce416SJulian Elischer p = td->td_proc; 2224d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 2234d29cb2dSJohn Baldwin if (!TAILQ_EMPTY(&ithread->it_handlers)) { 2244d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 2254d29cb2dSJohn Baldwin return (EINVAL); 2264d29cb2dSJohn Baldwin } 227b4151f71SJohn Baldwin ithread->it_flags |= IT_DEAD; 2284d29cb2dSJohn Baldwin mtx_lock_spin(&sched_lock); 22971fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 23071fad9fdSJulian Elischer TD_CLR_IWAIT(td); 231b40ce416SJulian Elischer setrunqueue(td); 232b4151f71SJohn Baldwin } 233b4151f71SJohn Baldwin mtx_unlock_spin(&sched_lock); 2344d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 23591f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name); 236b4151f71SJohn Baldwin return (0); 237b4151f71SJohn Baldwin } 238b4151f71SJohn Baldwin 239b4151f71SJohn Baldwin int 240b4151f71SJohn Baldwin ithread_add_handler(struct ithd* ithread, const char *name, 241b4151f71SJohn Baldwin driver_intr_t handler, void *arg, u_char pri, enum intr_type flags, 242b4151f71SJohn Baldwin void **cookiep) 243b4151f71SJohn Baldwin { 244b4151f71SJohn Baldwin struct intrhand *ih, *temp_ih; 245b4151f71SJohn Baldwin 246b4151f71SJohn Baldwin if (ithread == NULL || name == NULL || handler == NULL) 247b4151f71SJohn Baldwin return (EINVAL); 248b4151f71SJohn Baldwin if ((flags & INTR_FAST) !=0) 249b4151f71SJohn Baldwin flags |= INTR_EXCL; 250b4151f71SJohn Baldwin 251b4151f71SJohn Baldwin ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO); 252b4151f71SJohn Baldwin ih->ih_handler = handler; 253b4151f71SJohn Baldwin ih->ih_argument = arg; 254b4151f71SJohn Baldwin ih->ih_name = name; 255b4151f71SJohn Baldwin ih->ih_ithread = ithread; 256b4151f71SJohn Baldwin ih->ih_pri = pri; 257b4151f71SJohn Baldwin if (flags & INTR_FAST) 258b4151f71SJohn Baldwin ih->ih_flags = IH_FAST | IH_EXCLUSIVE; 259b4151f71SJohn Baldwin else if (flags & INTR_EXCL) 260b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 261b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 262b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 263b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 264b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 265b4151f71SJohn Baldwin 2664d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 267b4151f71SJohn Baldwin if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers)) 268b4151f71SJohn Baldwin goto fail; 269b4151f71SJohn Baldwin if (!TAILQ_EMPTY(&ithread->it_handlers) && 270b4151f71SJohn Baldwin (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0) 271b4151f71SJohn Baldwin goto fail; 272b4151f71SJohn Baldwin 273b4151f71SJohn Baldwin TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next) 274b4151f71SJohn Baldwin if (temp_ih->ih_pri > ih->ih_pri) 275b4151f71SJohn Baldwin break; 276b4151f71SJohn Baldwin if (temp_ih == NULL) 277b4151f71SJohn Baldwin TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next); 278b4151f71SJohn Baldwin else 279b4151f71SJohn Baldwin TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 280b4151f71SJohn Baldwin ithread_update(ithread); 2814d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 282b4151f71SJohn Baldwin 283b4151f71SJohn Baldwin if (cookiep != NULL) 284b4151f71SJohn Baldwin *cookiep = ih; 28591f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 286addec20cSJohn Baldwin ithread->it_name); 287b4151f71SJohn Baldwin return (0); 288b4151f71SJohn Baldwin 289b4151f71SJohn Baldwin fail: 2904d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 291b4151f71SJohn Baldwin free(ih, M_ITHREAD); 292b4151f71SJohn Baldwin return (EINVAL); 293b4151f71SJohn Baldwin } 294b4151f71SJohn Baldwin 295b4151f71SJohn Baldwin int 296b4151f71SJohn Baldwin ithread_remove_handler(void *cookie) 297b4151f71SJohn Baldwin { 298b4151f71SJohn Baldwin struct intrhand *handler = (struct intrhand *)cookie; 299b4151f71SJohn Baldwin struct ithd *ithread; 300b4151f71SJohn Baldwin #ifdef INVARIANTS 301b4151f71SJohn Baldwin struct intrhand *ih; 302b4151f71SJohn Baldwin #endif 303b4151f71SJohn Baldwin 3043e5da754SJohn Baldwin if (handler == NULL) 305b4151f71SJohn Baldwin return (EINVAL); 30676bd604eSJohn Baldwin ithread = handler->ih_ithread; 30776bd604eSJohn Baldwin KASSERT(ithread != NULL, 3083e5da754SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt thread", 3093e5da754SJohn Baldwin handler->ih_name)); 31091f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 311addec20cSJohn Baldwin ithread->it_name); 3124d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 313b4151f71SJohn Baldwin #ifdef INVARIANTS 314b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next) 3153e5da754SJohn Baldwin if (ih == handler) 3163e5da754SJohn Baldwin goto ok; 3174d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 3183e5da754SJohn Baldwin panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"", 3193e5da754SJohn Baldwin ih->ih_name, ithread->it_name); 3203e5da754SJohn Baldwin ok: 321b4151f71SJohn Baldwin #endif 322de271f01SJohn Baldwin /* 323de271f01SJohn Baldwin * If the interrupt thread is already running, then just mark this 324de271f01SJohn Baldwin * handler as being dead and let the ithread do the actual removal. 325de271f01SJohn Baldwin */ 326de271f01SJohn Baldwin mtx_lock_spin(&sched_lock); 32771fad9fdSJulian Elischer if (!TD_AWAITING_INTR(ithread->it_td)) { 328de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 329de271f01SJohn Baldwin 330de271f01SJohn Baldwin /* 331de271f01SJohn Baldwin * Ensure that the thread will process the handler list 332de271f01SJohn Baldwin * again and remove this handler if it has already passed 333de271f01SJohn Baldwin * it on the list. 334de271f01SJohn Baldwin */ 335de271f01SJohn Baldwin ithread->it_need = 1; 3364d29cb2dSJohn Baldwin } else 337b4151f71SJohn Baldwin TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next); 338de271f01SJohn Baldwin mtx_unlock_spin(&sched_lock); 3394d29cb2dSJohn Baldwin if ((handler->ih_flags & IH_DEAD) != 0) 3404d29cb2dSJohn Baldwin msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0); 3414d29cb2dSJohn Baldwin ithread_update(ithread); 3424d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 343b4151f71SJohn Baldwin free(handler, M_ITHREAD); 344b4151f71SJohn Baldwin return (0); 345b4151f71SJohn Baldwin } 346b4151f71SJohn Baldwin 347b4151f71SJohn Baldwin int 3483e5da754SJohn Baldwin ithread_schedule(struct ithd *ithread, int do_switch) 3493e5da754SJohn Baldwin { 3503e5da754SJohn Baldwin struct int_entropy entropy; 351b40ce416SJulian Elischer struct thread *td; 35204774f23SJulian Elischer struct thread *ctd; 3533e5da754SJohn Baldwin struct proc *p; 3543e5da754SJohn Baldwin 3553e5da754SJohn Baldwin /* 3563e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 3573e5da754SJohn Baldwin */ 3583e5da754SJohn Baldwin if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers)) 3593e5da754SJohn Baldwin return (EINVAL); 3603e5da754SJohn Baldwin 36104774f23SJulian Elischer ctd = curthread; 3623e5da754SJohn Baldwin /* 3633e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 3643e5da754SJohn Baldwin * sources of entropy, then gather some. 3653e5da754SJohn Baldwin */ 3663e5da754SJohn Baldwin if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) { 3673e5da754SJohn Baldwin entropy.vector = ithread->it_vector; 36865c17e74SDavid Xu entropy.proc = ctd->td_proc; 3693e5da754SJohn Baldwin random_harvest(&entropy, sizeof(entropy), 2, 0, 3703e5da754SJohn Baldwin RANDOM_INTERRUPT); 3713e5da754SJohn Baldwin } 3723e5da754SJohn Baldwin 373b40ce416SJulian Elischer td = ithread->it_td; 374b40ce416SJulian Elischer p = td->td_proc; 375653dd8c2SJohn Baldwin KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name)); 376e602ba25SJulian Elischer CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d", 377e602ba25SJulian Elischer __func__, p->p_pid, p->p_comm, ithread->it_need); 3783e5da754SJohn Baldwin 3793e5da754SJohn Baldwin /* 3803e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 3813e5da754SJohn Baldwin * running. Then, grab sched_lock and see if we actually need to 3823e5da754SJohn Baldwin * put this thread on the runqueue. If so and the do_switch flag is 383c86b6ff5SJohn Baldwin * true and it is safe to switch, then switch to the ithread 384c86b6ff5SJohn Baldwin * immediately. Otherwise, set the needresched flag to guarantee 385c86b6ff5SJohn Baldwin * that this ithread will run before any userland processes. 3863e5da754SJohn Baldwin */ 3873e5da754SJohn Baldwin ithread->it_need = 1; 3883e5da754SJohn Baldwin mtx_lock_spin(&sched_lock); 38971fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 39091f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid); 39171fad9fdSJulian Elischer TD_CLR_IWAIT(td); 392e602ba25SJulian Elischer setrunqueue(td); 393e602ba25SJulian Elischer if (do_switch && 39404774f23SJulian Elischer (ctd->td_critnest == 1) ) { 39571fad9fdSJulian Elischer KASSERT((TD_IS_RUNNING(ctd)), 39604774f23SJulian Elischer ("ithread_schedule: Bad state for curthread.")); 39704774f23SJulian Elischer ctd->td_proc->p_stats->p_ru.ru_nivcsw++; 39804774f23SJulian Elischer if (ctd->td_kse->ke_flags & KEF_IDLEKSE) 39971fad9fdSJulian Elischer ctd->td_state = TDS_CAN_RUN; /* XXXKSE */ 4003e5da754SJohn Baldwin mi_switch(); 4012d0231f5SJulian Elischer } else { 402b40ce416SJulian Elischer curthread->td_kse->ke_flags |= KEF_NEEDRESCHED; 4032d0231f5SJulian Elischer } 4043e5da754SJohn Baldwin } else { 40591f91617SDavid E. O'Brien CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d", 406e602ba25SJulian Elischer __func__, p->p_pid, ithread->it_need, p->p_state); 4073e5da754SJohn Baldwin } 4083e5da754SJohn Baldwin mtx_unlock_spin(&sched_lock); 4093e5da754SJohn Baldwin 4103e5da754SJohn Baldwin return (0); 4113e5da754SJohn Baldwin } 4123e5da754SJohn Baldwin 4133e5da754SJohn Baldwin int 414b4151f71SJohn Baldwin swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler, 415b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 4168088699fSJohn Baldwin { 4178088699fSJohn Baldwin struct ithd *ithd; 418b4151f71SJohn Baldwin int error; 4198088699fSJohn Baldwin 4203e5da754SJohn Baldwin if (flags & (INTR_FAST | INTR_ENTROPY)) 4213e5da754SJohn Baldwin return (EINVAL); 4223e5da754SJohn Baldwin 4238088699fSJohn Baldwin ithd = (ithdp != NULL) ? *ithdp : NULL; 4248088699fSJohn Baldwin 4253e5da754SJohn Baldwin if (ithd != NULL) { 4263e5da754SJohn Baldwin if ((ithd->it_flags & IT_SOFT) == 0) 4273e5da754SJohn Baldwin return(EINVAL); 4283e5da754SJohn Baldwin } else { 429b4151f71SJohn Baldwin error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL, 430b4151f71SJohn Baldwin "swi%d:", pri); 4318088699fSJohn Baldwin if (error) 432b4151f71SJohn Baldwin return (error); 433b4151f71SJohn Baldwin 4348088699fSJohn Baldwin if (ithdp != NULL) 4358088699fSJohn Baldwin *ithdp = ithd; 4368088699fSJohn Baldwin } 437d5a08a60SJake Burkholder return (ithread_add_handler(ithd, name, handler, arg, 438d5a08a60SJake Burkholder (pri * RQ_PPQ) + PI_SOFT, flags, cookiep)); 4398088699fSJohn Baldwin } 4408088699fSJohn Baldwin 4418088699fSJohn Baldwin 4421931cf94SJohn Baldwin /* 4438088699fSJohn Baldwin * Schedule a heavyweight software interrupt process. 4441931cf94SJohn Baldwin */ 4451931cf94SJohn Baldwin void 446b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 4471931cf94SJohn Baldwin { 448b4151f71SJohn Baldwin struct intrhand *ih = (struct intrhand *)cookie; 449b4151f71SJohn Baldwin struct ithd *it = ih->ih_ithread; 4503e5da754SJohn Baldwin int error; 4518088699fSJohn Baldwin 4521931cf94SJohn Baldwin atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */ 4531931cf94SJohn Baldwin 454b4151f71SJohn Baldwin CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d", 455b40ce416SJulian Elischer it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need); 4561931cf94SJohn Baldwin 4571931cf94SJohn Baldwin /* 4583e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 4593e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 4603e5da754SJohn Baldwin * it will execute it the next time it runs. 4611931cf94SJohn Baldwin */ 462b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 1); 463b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 464c86b6ff5SJohn Baldwin error = ithread_schedule(it, !cold); 4653e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 4668088699fSJohn Baldwin } 4678088699fSJohn Baldwin } 4688088699fSJohn Baldwin 4698088699fSJohn Baldwin /* 470b4151f71SJohn Baldwin * This is the main code for interrupt threads. 4718088699fSJohn Baldwin */ 47237c84183SPoul-Henning Kamp static void 473b4151f71SJohn Baldwin ithread_loop(void *arg) 4748088699fSJohn Baldwin { 475b4151f71SJohn Baldwin struct ithd *ithd; /* our thread context */ 4768088699fSJohn Baldwin struct intrhand *ih; /* and our interrupt handler chain */ 477b40ce416SJulian Elischer struct thread *td; 478b4151f71SJohn Baldwin struct proc *p; 4798088699fSJohn Baldwin 480b40ce416SJulian Elischer td = curthread; 481b40ce416SJulian Elischer p = td->td_proc; 482b4151f71SJohn Baldwin ithd = (struct ithd *)arg; /* point to myself */ 483b40ce416SJulian Elischer KASSERT(ithd->it_td == td && td->td_ithd == ithd, 48491f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 4858088699fSJohn Baldwin 4868088699fSJohn Baldwin /* 4878088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 4888088699fSJohn Baldwin * list of handlers, giving each one a go at it. 4898088699fSJohn Baldwin */ 4908088699fSJohn Baldwin for (;;) { 491b4151f71SJohn Baldwin /* 492b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 493b4151f71SJohn Baldwin */ 494b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 49591f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__, 496b4151f71SJohn Baldwin p->p_pid, p->p_comm); 497b40ce416SJulian Elischer td->td_ithd = NULL; 4984d29cb2dSJohn Baldwin mtx_destroy(&ithd->it_lock); 499b4151f71SJohn Baldwin mtx_lock(&Giant); 500b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 501b4151f71SJohn Baldwin kthread_exit(0); 502b4151f71SJohn Baldwin } 503b4151f71SJohn Baldwin 50491f91617SDavid E. O'Brien CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__, 505b4151f71SJohn Baldwin p->p_pid, p->p_comm, ithd->it_need); 506b4151f71SJohn Baldwin while (ithd->it_need) { 5078088699fSJohn Baldwin /* 5088088699fSJohn Baldwin * Service interrupts. If another interrupt 5098088699fSJohn Baldwin * arrives while we are running, they will set 5108088699fSJohn Baldwin * it_need to denote that we should make 5118088699fSJohn Baldwin * another pass. 5128088699fSJohn Baldwin */ 513b4151f71SJohn Baldwin atomic_store_rel_int(&ithd->it_need, 0); 514de271f01SJohn Baldwin restart: 515b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 516b4151f71SJohn Baldwin if (ithd->it_flags & IT_SOFT && !ih->ih_need) 5178088699fSJohn Baldwin continue; 518b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 0); 51991f91617SDavid E. O'Brien CTR6(KTR_INTR, 52091f91617SDavid E. O'Brien "%s: pid %d ih=%p: %p(%p) flg=%x", __func__, 5218088699fSJohn Baldwin p->p_pid, (void *)ih, 5228088699fSJohn Baldwin (void *)ih->ih_handler, ih->ih_argument, 5238088699fSJohn Baldwin ih->ih_flags); 5248088699fSJohn Baldwin 525de271f01SJohn Baldwin if ((ih->ih_flags & IH_DEAD) != 0) { 5264d29cb2dSJohn Baldwin mtx_lock(&ithd->it_lock); 527de271f01SJohn Baldwin TAILQ_REMOVE(&ithd->it_handlers, ih, 528de271f01SJohn Baldwin ih_next); 5294d29cb2dSJohn Baldwin wakeup(ih); 5304d29cb2dSJohn Baldwin mtx_unlock(&ithd->it_lock); 531de271f01SJohn Baldwin goto restart; 532de271f01SJohn Baldwin } 5334d29cb2dSJohn Baldwin if ((ih->ih_flags & IH_MPSAFE) == 0) 5344d29cb2dSJohn Baldwin mtx_lock(&Giant); 5358088699fSJohn Baldwin ih->ih_handler(ih->ih_argument); 536b4151f71SJohn Baldwin if ((ih->ih_flags & IH_MPSAFE) == 0) 5379ed346baSBosko Milekic mtx_unlock(&Giant); 5388088699fSJohn Baldwin } 5398088699fSJohn Baldwin } 5408088699fSJohn Baldwin 5418088699fSJohn Baldwin /* 5428088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 5438088699fSJohn Baldwin * lock. This may take a while and it_need may get 5448088699fSJohn Baldwin * set again, so we have to check it again. 5458088699fSJohn Baldwin */ 546896c2303SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 5479ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 548b4151f71SJohn Baldwin if (!ithd->it_need) { 549b4151f71SJohn Baldwin /* 550b4151f71SJohn Baldwin * Should we call this earlier in the loop above? 551b4151f71SJohn Baldwin */ 552b4151f71SJohn Baldwin if (ithd->it_enable != NULL) 553b4151f71SJohn Baldwin ithd->it_enable(ithd->it_vector); 55471fad9fdSJulian Elischer TD_SET_IWAIT(td); /* we're idle */ 55584bbc4dbSJohn Baldwin p->p_stats->p_ru.ru_nvcsw++; 55691f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid); 5578088699fSJohn Baldwin mi_switch(); 55891f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid); 5598088699fSJohn Baldwin } 5609ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 5618088699fSJohn Baldwin } 5621931cf94SJohn Baldwin } 5631931cf94SJohn Baldwin 564b4151f71SJohn Baldwin /* 5658088699fSJohn Baldwin * Start standard software interrupt threads 5661931cf94SJohn Baldwin */ 5671931cf94SJohn Baldwin static void 568b4151f71SJohn Baldwin start_softintr(void *dummy) 5691931cf94SJohn Baldwin { 570b4151f71SJohn Baldwin 571e3b6e33cSJake Burkholder if (swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK, 572b4151f71SJohn Baldwin INTR_MPSAFE, &softclock_ih) || 573b4151f71SJohn Baldwin swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih)) 574b4151f71SJohn Baldwin panic("died while creating standard software ithreads"); 5753e5da754SJohn Baldwin 576b40ce416SJulian Elischer PROC_LOCK(clk_ithd->it_td->td_proc); 577b40ce416SJulian Elischer clk_ithd->it_td->td_proc->p_flag |= P_NOLOAD; 578b40ce416SJulian Elischer PROC_UNLOCK(clk_ithd->it_td->td_proc); 5791931cf94SJohn Baldwin } 580b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL) 5811931cf94SJohn Baldwin 582d279178dSThomas Moestl /* 583d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 584d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 585d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 586d279178dSThomas Moestl * independent. 587d279178dSThomas Moestl * 588d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 589d279178dSThomas Moestl * calculate things at run time. 590d279178dSThomas Moestl */ 591d279178dSThomas Moestl static int 592d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 593d279178dSThomas Moestl { 594d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 595d279178dSThomas Moestl req)); 596d279178dSThomas Moestl } 597d279178dSThomas Moestl 598d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 599d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 600d279178dSThomas Moestl 601d279178dSThomas Moestl static int 602d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 603d279178dSThomas Moestl { 604d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrcnt, 605d279178dSThomas Moestl (char *)eintrcnt - (char *)intrcnt, req)); 606d279178dSThomas Moestl } 607d279178dSThomas Moestl 608d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 609d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 610