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 538088699fSJohn Baldwin #include <net/netisr.h> /* prototype for legacy_setsoftnet */ 5418c5a6c4SBruce Evans 553e5da754SJohn Baldwin struct int_entropy { 563e5da754SJohn Baldwin struct proc *proc; 573e5da754SJohn Baldwin int vector; 583e5da754SJohn Baldwin }; 593e5da754SJohn Baldwin 60b4151f71SJohn Baldwin void *net_ih; 61b4151f71SJohn Baldwin void *vm_ih; 62b4151f71SJohn Baldwin void *softclock_ih; 638088699fSJohn Baldwin struct ithd *clk_ithd; 648088699fSJohn Baldwin struct ithd *tty_ithd; 651931cf94SJohn Baldwin 66b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 67b4151f71SJohn Baldwin 68b4151f71SJohn Baldwin static void ithread_update(struct ithd *); 69b4151f71SJohn Baldwin static void ithread_loop(void *); 701931cf94SJohn Baldwin static void start_softintr(void *); 718088699fSJohn Baldwin static void swi_net(void *); 7218c5a6c4SBruce Evans 73b4151f71SJohn Baldwin u_char 74b4151f71SJohn Baldwin ithread_priority(enum intr_type flags) 759a94c9c5SJohn Baldwin { 76b4151f71SJohn Baldwin u_char pri; 779a94c9c5SJohn Baldwin 78b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 795a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 809a94c9c5SJohn Baldwin switch (flags) { 81b4151f71SJohn Baldwin case INTR_TYPE_TTY: 829a94c9c5SJohn Baldwin pri = PI_TTYLOW; 839a94c9c5SJohn Baldwin break; 849a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 859a94c9c5SJohn Baldwin /* 869a94c9c5SJohn Baldwin * XXX We need to refine this. BSD/OS distinguishes 879a94c9c5SJohn Baldwin * between tape and disk priorities. 889a94c9c5SJohn Baldwin */ 899a94c9c5SJohn Baldwin pri = PI_DISK; 909a94c9c5SJohn Baldwin break; 919a94c9c5SJohn Baldwin case INTR_TYPE_NET: 929a94c9c5SJohn Baldwin pri = PI_NET; 939a94c9c5SJohn Baldwin break; 949a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 959a94c9c5SJohn Baldwin pri = PI_DISK; /* XXX or PI_CAM? */ 969a94c9c5SJohn Baldwin break; 975a280d9cSPeter Wemm case INTR_TYPE_AV: /* Audio/video */ 985a280d9cSPeter Wemm pri = PI_AV; 995a280d9cSPeter Wemm break; 100b4151f71SJohn Baldwin case INTR_TYPE_CLK: 101b4151f71SJohn Baldwin pri = PI_REALTIME; 102b4151f71SJohn Baldwin break; 1039a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1049a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1059a94c9c5SJohn Baldwin break; 1069a94c9c5SJohn Baldwin default: 107b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 1089a94c9c5SJohn Baldwin panic("ithread_priority: no interrupt type in flags"); 1099a94c9c5SJohn Baldwin } 1109a94c9c5SJohn Baldwin 1119a94c9c5SJohn Baldwin return pri; 1129a94c9c5SJohn Baldwin } 1139a94c9c5SJohn Baldwin 114b4151f71SJohn Baldwin /* 115b4151f71SJohn Baldwin * Regenerate the name (p_comm) and priority for a threaded interrupt thread. 116b4151f71SJohn Baldwin */ 117b4151f71SJohn Baldwin static void 118b4151f71SJohn Baldwin ithread_update(struct ithd *ithd) 119b4151f71SJohn Baldwin { 120b4151f71SJohn Baldwin struct intrhand *ih; 121b40ce416SJulian Elischer struct thread *td; 122b4151f71SJohn Baldwin struct proc *p; 123b4151f71SJohn Baldwin int entropy; 1248088699fSJohn Baldwin 1254d29cb2dSJohn Baldwin mtx_assert(&ithd->it_lock, MA_OWNED); 126b40ce416SJulian Elischer td = ithd->it_td; 127b40ce416SJulian Elischer if (td == NULL) 128b4151f71SJohn Baldwin return; 129b40ce416SJulian Elischer p = td->td_proc; 130b4151f71SJohn Baldwin 131b4151f71SJohn Baldwin strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name)); 132b4151f71SJohn Baldwin ih = TAILQ_FIRST(&ithd->it_handlers); 133b4151f71SJohn Baldwin if (ih == NULL) { 134b40ce416SJulian Elischer td->td_ksegrp->kg_pri.pri_level = PRI_MAX_ITHD; 135b4151f71SJohn Baldwin ithd->it_flags &= ~IT_ENTROPY; 136b4151f71SJohn Baldwin return; 137b4151f71SJohn Baldwin } 138b4151f71SJohn Baldwin 139b4151f71SJohn Baldwin entropy = 0; 140b40ce416SJulian Elischer td->td_ksegrp->kg_pri.pri_level = ih->ih_pri; 141b40ce416SJulian Elischer td->td_ksegrp->kg_pri.pri_native = ih->ih_pri; 142b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 143b4151f71SJohn Baldwin if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 < 144b4151f71SJohn Baldwin sizeof(p->p_comm)) { 145b4151f71SJohn Baldwin strcat(p->p_comm, " "); 146b4151f71SJohn Baldwin strcat(p->p_comm, ih->ih_name); 147b4151f71SJohn Baldwin } else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) { 148b4151f71SJohn Baldwin if (p->p_comm[sizeof(p->p_comm) - 2] == '+') 149b4151f71SJohn Baldwin p->p_comm[sizeof(p->p_comm) - 2] = '*'; 150b4151f71SJohn Baldwin else 151b4151f71SJohn Baldwin p->p_comm[sizeof(p->p_comm) - 2] = '+'; 152b4151f71SJohn Baldwin } else 153b4151f71SJohn Baldwin strcat(p->p_comm, "+"); 154b4151f71SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 155b4151f71SJohn Baldwin entropy++; 156b4151f71SJohn Baldwin } 157b4151f71SJohn Baldwin 1583e5da754SJohn Baldwin if (entropy) 159b4151f71SJohn Baldwin ithd->it_flags |= IT_ENTROPY; 160b4151f71SJohn Baldwin else 161b4151f71SJohn Baldwin ithd->it_flags &= ~IT_ENTROPY; 162addec20cSJohn Baldwin 16391f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: updated %s\n", __func__, p->p_comm); 164b4151f71SJohn Baldwin } 165b4151f71SJohn Baldwin 166b4151f71SJohn Baldwin int 167b4151f71SJohn Baldwin ithread_create(struct ithd **ithread, int vector, int flags, 168b4151f71SJohn Baldwin void (*disable)(int), void (*enable)(int), const char *fmt, ...) 169b4151f71SJohn Baldwin { 170b4151f71SJohn Baldwin struct ithd *ithd; 171b40ce416SJulian Elischer struct thread *td; 172b4151f71SJohn Baldwin struct proc *p; 173b4151f71SJohn Baldwin int error; 174b4151f71SJohn Baldwin va_list ap; 175b4151f71SJohn Baldwin 1763e5da754SJohn Baldwin /* The only valid flag during creation is IT_SOFT. */ 1773e5da754SJohn Baldwin if ((flags & ~IT_SOFT) != 0) 1783e5da754SJohn Baldwin return (EINVAL); 1793e5da754SJohn Baldwin 180b4151f71SJohn Baldwin ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO); 181b4151f71SJohn Baldwin ithd->it_vector = vector; 182b4151f71SJohn Baldwin ithd->it_disable = disable; 183b4151f71SJohn Baldwin ithd->it_enable = enable; 184b4151f71SJohn Baldwin ithd->it_flags = flags; 185b4151f71SJohn Baldwin TAILQ_INIT(&ithd->it_handlers); 1864d29cb2dSJohn Baldwin mtx_init(&ithd->it_lock, "ithread", MTX_DEF); 1874d29cb2dSJohn Baldwin mtx_lock(&ithd->it_lock); 188b4151f71SJohn Baldwin 189b4151f71SJohn Baldwin va_start(ap, fmt); 190b4151f71SJohn Baldwin vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap); 191b4151f71SJohn Baldwin va_end(ap); 192b4151f71SJohn Baldwin 193b4151f71SJohn Baldwin error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID, 1941f723035SJohn Baldwin "%s", ithd->it_name); 195b4151f71SJohn Baldwin if (error) { 1964d29cb2dSJohn Baldwin mtx_destroy(&ithd->it_lock); 197b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 198b4151f71SJohn Baldwin return (error); 199b4151f71SJohn Baldwin } 200b40ce416SJulian Elischer td = &p->p_thread; /* XXXKSE */ 201b40ce416SJulian Elischer td->td_ksegrp->kg_pri.pri_class = PRI_ITHD; 202b40ce416SJulian Elischer td->td_ksegrp->kg_pri.pri_level = PRI_MAX_ITHD; 203b4151f71SJohn Baldwin p->p_stat = SWAIT; 204b40ce416SJulian Elischer ithd->it_td = td; 205b40ce416SJulian Elischer td->td_ithd = ithd; 206b4151f71SJohn Baldwin if (ithread != NULL) 207b4151f71SJohn Baldwin *ithread = ithd; 2084d29cb2dSJohn Baldwin mtx_unlock(&ithd->it_lock); 209b4151f71SJohn Baldwin 21091f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name); 211b4151f71SJohn Baldwin return (0); 212b4151f71SJohn Baldwin } 213b4151f71SJohn Baldwin 214b4151f71SJohn Baldwin int 215b4151f71SJohn Baldwin ithread_destroy(struct ithd *ithread) 216b4151f71SJohn Baldwin { 217b4151f71SJohn Baldwin 218b40ce416SJulian Elischer struct thread *td; 219b40ce416SJulian Elischer struct proc *p; 2204d29cb2dSJohn Baldwin if (ithread == NULL) 221b4151f71SJohn Baldwin return (EINVAL); 222b4151f71SJohn Baldwin 223b40ce416SJulian Elischer td = ithread->it_td; 224b40ce416SJulian Elischer p = td->td_proc; 2254d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 2264d29cb2dSJohn Baldwin if (!TAILQ_EMPTY(&ithread->it_handlers)) { 2274d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 2284d29cb2dSJohn Baldwin return (EINVAL); 2294d29cb2dSJohn Baldwin } 230b4151f71SJohn Baldwin ithread->it_flags |= IT_DEAD; 2314d29cb2dSJohn Baldwin mtx_lock_spin(&sched_lock); 232b40ce416SJulian Elischer if (p->p_stat == SWAIT) { 233b40ce416SJulian Elischer p->p_stat = SRUN; /* XXXKSE */ 234b40ce416SJulian Elischer setrunqueue(td); 235b4151f71SJohn Baldwin } 236b4151f71SJohn Baldwin mtx_unlock_spin(&sched_lock); 2374d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 23891f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name); 239b4151f71SJohn Baldwin return (0); 240b4151f71SJohn Baldwin } 241b4151f71SJohn Baldwin 242b4151f71SJohn Baldwin int 243b4151f71SJohn Baldwin ithread_add_handler(struct ithd* ithread, const char *name, 244b4151f71SJohn Baldwin driver_intr_t handler, void *arg, u_char pri, enum intr_type flags, 245b4151f71SJohn Baldwin void **cookiep) 246b4151f71SJohn Baldwin { 247b4151f71SJohn Baldwin struct intrhand *ih, *temp_ih; 248b4151f71SJohn Baldwin 249b4151f71SJohn Baldwin if (ithread == NULL || name == NULL || handler == NULL) 250b4151f71SJohn Baldwin return (EINVAL); 251b4151f71SJohn Baldwin if ((flags & INTR_FAST) !=0) 252b4151f71SJohn Baldwin flags |= INTR_EXCL; 253b4151f71SJohn Baldwin 254b4151f71SJohn Baldwin ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO); 255b4151f71SJohn Baldwin ih->ih_handler = handler; 256b4151f71SJohn Baldwin ih->ih_argument = arg; 257b4151f71SJohn Baldwin ih->ih_name = name; 258b4151f71SJohn Baldwin ih->ih_ithread = ithread; 259b4151f71SJohn Baldwin ih->ih_pri = pri; 260b4151f71SJohn Baldwin if (flags & INTR_FAST) 261b4151f71SJohn Baldwin ih->ih_flags = IH_FAST | IH_EXCLUSIVE; 262b4151f71SJohn Baldwin else if (flags & INTR_EXCL) 263b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 264b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 265b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 266b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 267b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 268b4151f71SJohn Baldwin 2694d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 270b4151f71SJohn Baldwin if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers)) 271b4151f71SJohn Baldwin goto fail; 272b4151f71SJohn Baldwin if (!TAILQ_EMPTY(&ithread->it_handlers) && 273b4151f71SJohn Baldwin (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0) 274b4151f71SJohn Baldwin goto fail; 275b4151f71SJohn Baldwin 276b4151f71SJohn Baldwin TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next) 277b4151f71SJohn Baldwin if (temp_ih->ih_pri > ih->ih_pri) 278b4151f71SJohn Baldwin break; 279b4151f71SJohn Baldwin if (temp_ih == NULL) 280b4151f71SJohn Baldwin TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next); 281b4151f71SJohn Baldwin else 282b4151f71SJohn Baldwin TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 283b4151f71SJohn Baldwin ithread_update(ithread); 2844d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 285b4151f71SJohn Baldwin 286b4151f71SJohn Baldwin if (cookiep != NULL) 287b4151f71SJohn Baldwin *cookiep = ih; 28891f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 289addec20cSJohn Baldwin ithread->it_name); 290b4151f71SJohn Baldwin return (0); 291b4151f71SJohn Baldwin 292b4151f71SJohn Baldwin fail: 2934d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 294b4151f71SJohn Baldwin free(ih, M_ITHREAD); 295b4151f71SJohn Baldwin return (EINVAL); 296b4151f71SJohn Baldwin } 297b4151f71SJohn Baldwin 298b4151f71SJohn Baldwin int 299b4151f71SJohn Baldwin ithread_remove_handler(void *cookie) 300b4151f71SJohn Baldwin { 301b4151f71SJohn Baldwin struct intrhand *handler = (struct intrhand *)cookie; 302b4151f71SJohn Baldwin struct ithd *ithread; 303b4151f71SJohn Baldwin #ifdef INVARIANTS 304b4151f71SJohn Baldwin struct intrhand *ih; 305b4151f71SJohn Baldwin #endif 306b4151f71SJohn Baldwin 3073e5da754SJohn Baldwin if (handler == NULL) 308b4151f71SJohn Baldwin return (EINVAL); 30976bd604eSJohn Baldwin ithread = handler->ih_ithread; 31076bd604eSJohn Baldwin KASSERT(ithread != NULL, 3113e5da754SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt thread", 3123e5da754SJohn Baldwin handler->ih_name)); 31391f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 314addec20cSJohn Baldwin ithread->it_name); 3154d29cb2dSJohn Baldwin mtx_lock(&ithread->it_lock); 316b4151f71SJohn Baldwin #ifdef INVARIANTS 317b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next) 3183e5da754SJohn Baldwin if (ih == handler) 3193e5da754SJohn Baldwin goto ok; 3204d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 3213e5da754SJohn Baldwin panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"", 3223e5da754SJohn Baldwin ih->ih_name, ithread->it_name); 3233e5da754SJohn Baldwin ok: 324b4151f71SJohn Baldwin #endif 325de271f01SJohn Baldwin /* 326de271f01SJohn Baldwin * If the interrupt thread is already running, then just mark this 327de271f01SJohn Baldwin * handler as being dead and let the ithread do the actual removal. 328de271f01SJohn Baldwin */ 329de271f01SJohn Baldwin mtx_lock_spin(&sched_lock); 330b40ce416SJulian Elischer if (ithread->it_td->td_proc->p_stat != SWAIT) { 331de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 332de271f01SJohn Baldwin 333de271f01SJohn Baldwin /* 334de271f01SJohn Baldwin * Ensure that the thread will process the handler list 335de271f01SJohn Baldwin * again and remove this handler if it has already passed 336de271f01SJohn Baldwin * it on the list. 337de271f01SJohn Baldwin */ 338de271f01SJohn Baldwin ithread->it_need = 1; 3394d29cb2dSJohn Baldwin } else 340b4151f71SJohn Baldwin TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next); 341de271f01SJohn Baldwin mtx_unlock_spin(&sched_lock); 3424d29cb2dSJohn Baldwin if ((handler->ih_flags & IH_DEAD) != 0) 3434d29cb2dSJohn Baldwin msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0); 3444d29cb2dSJohn Baldwin ithread_update(ithread); 3454d29cb2dSJohn Baldwin mtx_unlock(&ithread->it_lock); 346b4151f71SJohn Baldwin free(handler, M_ITHREAD); 347b4151f71SJohn Baldwin return (0); 348b4151f71SJohn Baldwin } 349b4151f71SJohn Baldwin 350b4151f71SJohn Baldwin int 3513e5da754SJohn Baldwin ithread_schedule(struct ithd *ithread, int do_switch) 3523e5da754SJohn Baldwin { 3533e5da754SJohn Baldwin struct int_entropy entropy; 354b40ce416SJulian Elischer struct thread *td; 3553e5da754SJohn Baldwin struct proc *p; 3563e5da754SJohn Baldwin 3573e5da754SJohn Baldwin /* 3583e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 3593e5da754SJohn Baldwin */ 3603e5da754SJohn Baldwin if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers)) 3613e5da754SJohn Baldwin return (EINVAL); 3623e5da754SJohn Baldwin 3633e5da754SJohn Baldwin /* 3643e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 3653e5da754SJohn Baldwin * sources of entropy, then gather some. 3663e5da754SJohn Baldwin */ 3673e5da754SJohn Baldwin if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) { 3683e5da754SJohn Baldwin entropy.vector = ithread->it_vector; 369b40ce416SJulian Elischer entropy.proc = curthread->td_proc;; 3703e5da754SJohn Baldwin random_harvest(&entropy, sizeof(entropy), 2, 0, 3713e5da754SJohn Baldwin RANDOM_INTERRUPT); 3723e5da754SJohn Baldwin } 3733e5da754SJohn Baldwin 374b40ce416SJulian Elischer td = ithread->it_td; 375b40ce416SJulian Elischer p = td->td_proc; 376653dd8c2SJohn Baldwin KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name)); 37791f91617SDavid E. O'Brien CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d", __func__, p->p_pid, p->p_comm, 3783e5da754SJohn Baldwin ithread->it_need); 3793e5da754SJohn Baldwin 3803e5da754SJohn Baldwin /* 3813e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 3823e5da754SJohn Baldwin * running. Then, grab sched_lock and see if we actually need to 3833e5da754SJohn Baldwin * put this thread on the runqueue. If so and the do_switch flag is 384688ebe12SJohn Baldwin * true, then switch to the ithread immediately. Otherwise, set the 385688ebe12SJohn Baldwin * needresched flag to guarantee that this ithread will run before any 3863e5da754SJohn Baldwin * userland processes. 3873e5da754SJohn Baldwin */ 3883e5da754SJohn Baldwin ithread->it_need = 1; 3893e5da754SJohn Baldwin mtx_lock_spin(&sched_lock); 3903e5da754SJohn Baldwin if (p->p_stat == SWAIT) { 39191f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid); 3923e5da754SJohn Baldwin p->p_stat = SRUN; 393b40ce416SJulian Elischer setrunqueue(td); /* XXXKSE */ 394b40ce416SJulian Elischer if (do_switch && curthread->td_proc->p_stat == SRUN) { 395b40ce416SJulian Elischer if (curthread != PCPU_GET(idlethread)) 396b40ce416SJulian Elischer setrunqueue(curthread); 397b40ce416SJulian Elischer curthread->td_proc->p_stats->p_ru.ru_nivcsw++; 3983e5da754SJohn Baldwin mi_switch(); 3993e5da754SJohn Baldwin } else 400b40ce416SJulian Elischer curthread->td_kse->ke_flags |= KEF_NEEDRESCHED; 4013e5da754SJohn Baldwin } else { 40291f91617SDavid E. O'Brien CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d", 40391f91617SDavid E. O'Brien __func__, p->p_pid, ithread->it_need, p->p_stat); 4043e5da754SJohn Baldwin } 4053e5da754SJohn Baldwin mtx_unlock_spin(&sched_lock); 4063e5da754SJohn Baldwin 4073e5da754SJohn Baldwin return (0); 4083e5da754SJohn Baldwin } 4093e5da754SJohn Baldwin 4103e5da754SJohn Baldwin int 411b4151f71SJohn Baldwin swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler, 412b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 4138088699fSJohn Baldwin { 4148088699fSJohn Baldwin struct ithd *ithd; 415b4151f71SJohn Baldwin int error; 4168088699fSJohn Baldwin 4173e5da754SJohn Baldwin if (flags & (INTR_FAST | INTR_ENTROPY)) 4183e5da754SJohn Baldwin return (EINVAL); 4193e5da754SJohn Baldwin 4208088699fSJohn Baldwin ithd = (ithdp != NULL) ? *ithdp : NULL; 4218088699fSJohn Baldwin 4223e5da754SJohn Baldwin if (ithd != NULL) { 4233e5da754SJohn Baldwin if ((ithd->it_flags & IT_SOFT) == 0) 4243e5da754SJohn Baldwin return(EINVAL); 4253e5da754SJohn Baldwin } else { 426b4151f71SJohn Baldwin error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL, 427b4151f71SJohn Baldwin "swi%d:", pri); 4288088699fSJohn Baldwin if (error) 429b4151f71SJohn Baldwin return (error); 430b4151f71SJohn Baldwin 4318088699fSJohn Baldwin if (ithdp != NULL) 4328088699fSJohn Baldwin *ithdp = ithd; 4338088699fSJohn Baldwin } 434d5a08a60SJake Burkholder return (ithread_add_handler(ithd, name, handler, arg, 435d5a08a60SJake Burkholder (pri * RQ_PPQ) + PI_SOFT, flags, cookiep)); 4368088699fSJohn Baldwin } 4378088699fSJohn Baldwin 4388088699fSJohn Baldwin 4391931cf94SJohn Baldwin /* 4408088699fSJohn Baldwin * Schedule a heavyweight software interrupt process. 4411931cf94SJohn Baldwin */ 4421931cf94SJohn Baldwin void 443b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 4441931cf94SJohn Baldwin { 445b4151f71SJohn Baldwin struct intrhand *ih = (struct intrhand *)cookie; 446b4151f71SJohn Baldwin struct ithd *it = ih->ih_ithread; 4473e5da754SJohn Baldwin int error; 4488088699fSJohn Baldwin 4491931cf94SJohn Baldwin atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */ 4501931cf94SJohn Baldwin 451b4151f71SJohn Baldwin CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d", 452b40ce416SJulian Elischer it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need); 4531931cf94SJohn Baldwin 4541931cf94SJohn Baldwin /* 4553e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 4563e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 4573e5da754SJohn Baldwin * it will execute it the next time it runs. 4581931cf94SJohn Baldwin */ 459b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 1); 460b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 4613e5da754SJohn Baldwin error = ithread_schedule(it, !cold && flags & SWI_SWITCH); 4623e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 4638088699fSJohn Baldwin } 4648088699fSJohn Baldwin } 4658088699fSJohn Baldwin 4668088699fSJohn Baldwin /* 467b4151f71SJohn Baldwin * This is the main code for interrupt threads. 4688088699fSJohn Baldwin */ 4698088699fSJohn Baldwin void 470b4151f71SJohn Baldwin ithread_loop(void *arg) 4718088699fSJohn Baldwin { 472b4151f71SJohn Baldwin struct ithd *ithd; /* our thread context */ 4738088699fSJohn Baldwin struct intrhand *ih; /* and our interrupt handler chain */ 474b40ce416SJulian Elischer struct thread *td; 475b4151f71SJohn Baldwin struct proc *p; 4768088699fSJohn Baldwin 477b40ce416SJulian Elischer td = curthread; 478b40ce416SJulian Elischer p = td->td_proc; 479b4151f71SJohn Baldwin ithd = (struct ithd *)arg; /* point to myself */ 480b40ce416SJulian Elischer KASSERT(ithd->it_td == td && td->td_ithd == ithd, 48191f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 4828088699fSJohn Baldwin 4838088699fSJohn Baldwin /* 4848088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 4858088699fSJohn Baldwin * list of handlers, giving each one a go at it. 4868088699fSJohn Baldwin */ 4878088699fSJohn Baldwin for (;;) { 488b4151f71SJohn Baldwin /* 489b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 490b4151f71SJohn Baldwin */ 491b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 49291f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__, 493b4151f71SJohn Baldwin p->p_pid, p->p_comm); 494b40ce416SJulian Elischer td->td_ithd = NULL; 4954d29cb2dSJohn Baldwin mtx_destroy(&ithd->it_lock); 496b4151f71SJohn Baldwin mtx_lock(&Giant); 497b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 498b4151f71SJohn Baldwin kthread_exit(0); 499b4151f71SJohn Baldwin } 500b4151f71SJohn Baldwin 50191f91617SDavid E. O'Brien CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__, 502b4151f71SJohn Baldwin p->p_pid, p->p_comm, ithd->it_need); 503b4151f71SJohn Baldwin while (ithd->it_need) { 5048088699fSJohn Baldwin /* 5058088699fSJohn Baldwin * Service interrupts. If another interrupt 5068088699fSJohn Baldwin * arrives while we are running, they will set 5078088699fSJohn Baldwin * it_need to denote that we should make 5088088699fSJohn Baldwin * another pass. 5098088699fSJohn Baldwin */ 510b4151f71SJohn Baldwin atomic_store_rel_int(&ithd->it_need, 0); 511de271f01SJohn Baldwin restart: 512b4151f71SJohn Baldwin TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 513b4151f71SJohn Baldwin if (ithd->it_flags & IT_SOFT && !ih->ih_need) 5148088699fSJohn Baldwin continue; 515b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 0); 51691f91617SDavid E. O'Brien CTR6(KTR_INTR, 51791f91617SDavid E. O'Brien "%s: pid %d ih=%p: %p(%p) flg=%x", __func__, 5188088699fSJohn Baldwin p->p_pid, (void *)ih, 5198088699fSJohn Baldwin (void *)ih->ih_handler, ih->ih_argument, 5208088699fSJohn Baldwin ih->ih_flags); 5218088699fSJohn Baldwin 522de271f01SJohn Baldwin if ((ih->ih_flags & IH_DEAD) != 0) { 5234d29cb2dSJohn Baldwin mtx_lock(&ithd->it_lock); 524de271f01SJohn Baldwin TAILQ_REMOVE(&ithd->it_handlers, ih, 525de271f01SJohn Baldwin ih_next); 5264d29cb2dSJohn Baldwin wakeup(ih); 5274d29cb2dSJohn Baldwin mtx_unlock(&ithd->it_lock); 528de271f01SJohn Baldwin goto restart; 529de271f01SJohn Baldwin } 5304d29cb2dSJohn Baldwin if ((ih->ih_flags & IH_MPSAFE) == 0) 5314d29cb2dSJohn Baldwin mtx_lock(&Giant); 5328088699fSJohn Baldwin ih->ih_handler(ih->ih_argument); 533b4151f71SJohn Baldwin if ((ih->ih_flags & IH_MPSAFE) == 0) 5349ed346baSBosko Milekic mtx_unlock(&Giant); 5358088699fSJohn Baldwin } 5368088699fSJohn Baldwin } 5378088699fSJohn Baldwin 5388088699fSJohn Baldwin /* 5398088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 5408088699fSJohn Baldwin * lock. This may take a while and it_need may get 5418088699fSJohn Baldwin * set again, so we have to check it again. 5428088699fSJohn Baldwin */ 543896c2303SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 5449ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 545b4151f71SJohn Baldwin if (!ithd->it_need) { 546b4151f71SJohn Baldwin /* 547b4151f71SJohn Baldwin * Should we call this earlier in the loop above? 548b4151f71SJohn Baldwin */ 549b4151f71SJohn Baldwin if (ithd->it_enable != NULL) 550b4151f71SJohn Baldwin ithd->it_enable(ithd->it_vector); 5518088699fSJohn Baldwin p->p_stat = SWAIT; /* we're idle */ 55284bbc4dbSJohn Baldwin p->p_stats->p_ru.ru_nvcsw++; 55391f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid); 5548088699fSJohn Baldwin mi_switch(); 55591f91617SDavid E. O'Brien CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid); 5568088699fSJohn Baldwin } 5579ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 5588088699fSJohn Baldwin } 5591931cf94SJohn Baldwin } 5601931cf94SJohn Baldwin 561b4151f71SJohn Baldwin /* 5628088699fSJohn Baldwin * Start standard software interrupt threads 5631931cf94SJohn Baldwin */ 5641931cf94SJohn Baldwin static void 565b4151f71SJohn Baldwin start_softintr(void *dummy) 5661931cf94SJohn Baldwin { 567b4151f71SJohn Baldwin 568b4151f71SJohn Baldwin if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih) || 569b4151f71SJohn Baldwin swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK, 570b4151f71SJohn Baldwin INTR_MPSAFE, &softclock_ih) || 571b4151f71SJohn Baldwin swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih)) 572b4151f71SJohn Baldwin panic("died while creating standard software ithreads"); 5733e5da754SJohn Baldwin 574b40ce416SJulian Elischer PROC_LOCK(clk_ithd->it_td->td_proc); 575b40ce416SJulian Elischer clk_ithd->it_td->td_proc->p_flag |= P_NOLOAD; 576b40ce416SJulian Elischer PROC_UNLOCK(clk_ithd->it_td->td_proc); 5771931cf94SJohn Baldwin } 578b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL) 5791931cf94SJohn Baldwin 5801931cf94SJohn Baldwin void 581b4151f71SJohn Baldwin legacy_setsoftnet(void) 5821931cf94SJohn Baldwin { 583b4151f71SJohn Baldwin swi_sched(net_ih, SWI_NOSWITCH); 5841931cf94SJohn Baldwin } 5851931cf94SJohn Baldwin 5868088699fSJohn Baldwin /* 5878088699fSJohn Baldwin * XXX: This should really be in the network code somewhere and installed 5888088699fSJohn Baldwin * via a SI_SUB_SOFINTR, SI_ORDER_MIDDLE sysinit. 5898088699fSJohn Baldwin */ 5908088699fSJohn Baldwin void (*netisrs[32]) __P((void)); 5916533ba2eSDavid E. O'Brien volatile unsigned int netisr; /* scheduling bits for network */ 5928088699fSJohn Baldwin 5931eb44f02SJake Burkholder int 5941eb44f02SJake Burkholder register_netisr(num, handler) 5951eb44f02SJake Burkholder int num; 5961eb44f02SJake Burkholder netisr_t *handler; 5971eb44f02SJake Burkholder { 5981eb44f02SJake Burkholder 5991eb44f02SJake Burkholder if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 6001eb44f02SJake Burkholder printf("register_netisr: bad isr number: %d\n", num); 6011eb44f02SJake Burkholder return (EINVAL); 6021eb44f02SJake Burkholder } 6031eb44f02SJake Burkholder netisrs[num] = handler; 6041eb44f02SJake Burkholder return (0); 6051eb44f02SJake Burkholder } 6061eb44f02SJake Burkholder 6071eb44f02SJake Burkholder int 6081eb44f02SJake Burkholder unregister_netisr(num) 6091eb44f02SJake Burkholder int num; 6101eb44f02SJake Burkholder { 6111eb44f02SJake Burkholder 6121eb44f02SJake Burkholder if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 6131eb44f02SJake Burkholder printf("unregister_netisr: bad isr number: %d\n", num); 6141eb44f02SJake Burkholder return (EINVAL); 6151eb44f02SJake Burkholder } 6161eb44f02SJake Burkholder netisrs[num] = NULL; 6171eb44f02SJake Burkholder return (0); 6181eb44f02SJake Burkholder } 6191eb44f02SJake Burkholder 6208088699fSJohn Baldwin static void 6218088699fSJohn Baldwin swi_net(void *dummy) 6221931cf94SJohn Baldwin { 6238088699fSJohn Baldwin u_int bits; 6248088699fSJohn Baldwin int i; 6258088699fSJohn Baldwin 6268088699fSJohn Baldwin bits = atomic_readandclear_int(&netisr); 6278088699fSJohn Baldwin while ((i = ffs(bits)) != 0) { 6288088699fSJohn Baldwin i--; 629338c0bc6SSeigo Tanimura if (netisrs[i] != NULL) 6308088699fSJohn Baldwin netisrs[i](); 631338c0bc6SSeigo Tanimura else 632338c0bc6SSeigo Tanimura printf("swi_net: unregistered isr number: %d.\n", i); 6338088699fSJohn Baldwin bits &= ~(1 << i); 6348088699fSJohn Baldwin } 6351931cf94SJohn Baldwin } 636d279178dSThomas Moestl 637d279178dSThomas Moestl /* 638d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 639d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 640d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 641d279178dSThomas Moestl * independent. 642d279178dSThomas Moestl * 643d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 644d279178dSThomas Moestl * calculate things at run time. 645d279178dSThomas Moestl */ 646d279178dSThomas Moestl static int 647d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 648d279178dSThomas Moestl { 649d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 650d279178dSThomas Moestl req)); 651d279178dSThomas Moestl } 652d279178dSThomas Moestl 653d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 654d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 655d279178dSThomas Moestl 656d279178dSThomas Moestl static int 657d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 658d279178dSThomas Moestl { 659d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrcnt, 660d279178dSThomas Moestl (char *)eintrcnt - (char *)intrcnt, req)); 661d279178dSThomas Moestl } 662d279178dSThomas Moestl 663d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 664d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 665