kern_intr.c revision 198149
1139804Simp/*- 226156Sse * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 326156Sse * All rights reserved. 426156Sse * 526156Sse * Redistribution and use in source and binary forms, with or without 626156Sse * modification, are permitted provided that the following conditions 726156Sse * are met: 826156Sse * 1. Redistributions of source code must retain the above copyright 926156Sse * notice unmodified, this list of conditions, and the following 1026156Sse * disclaimer. 1126156Sse * 2. Redistributions in binary form must reproduce the above copyright 1226156Sse * notice, this list of conditions and the following disclaimer in the 1326156Sse * documentation and/or other materials provided with the distribution. 1426156Sse * 1526156Sse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1626156Sse * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1726156Sse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1826156Sse * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1926156Sse * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2026156Sse * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2126156Sse * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2226156Sse * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2326156Sse * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2426156Sse * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2526156Sse */ 2626156Sse 27116182Sobrien#include <sys/cdefs.h> 28116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_intr.c 198149 2009-10-15 18:51:19Z jhb $"); 2936887Sdfr 30121482Sjhb#include "opt_ddb.h" 31121482Sjhb 3241059Speter#include <sys/param.h> 3365822Sjhb#include <sys/bus.h> 34110860Salfred#include <sys/conf.h> 35178092Sjeff#include <sys/cpuset.h> 3665822Sjhb#include <sys/rtprio.h> 3741059Speter#include <sys/systm.h> 3866698Sjhb#include <sys/interrupt.h> 3966698Sjhb#include <sys/kernel.h> 4066698Sjhb#include <sys/kthread.h> 4166698Sjhb#include <sys/ktr.h> 42130128Sbde#include <sys/limits.h> 4374914Sjhb#include <sys/lock.h> 4426156Sse#include <sys/malloc.h> 4567365Sjhb#include <sys/mutex.h> 46195249Sjhb#include <sys/priv.h> 4766698Sjhb#include <sys/proc.h> 4872759Sjhb#include <sys/random.h> 4972237Sjhb#include <sys/resourcevar.h> 50139451Sjhb#include <sys/sched.h> 51177181Sjhb#include <sys/smp.h> 5277582Stmm#include <sys/sysctl.h> 53182024Skmacy#include <sys/syslog.h> 5466698Sjhb#include <sys/unistd.h> 5566698Sjhb#include <sys/vmmeter.h> 5666698Sjhb#include <machine/atomic.h> 5766698Sjhb#include <machine/cpu.h> 5867551Sjhb#include <machine/md_var.h> 5972237Sjhb#include <machine/stdarg.h> 60121482Sjhb#ifdef DDB 61121482Sjhb#include <ddb/ddb.h> 62121482Sjhb#include <ddb/db_sym.h> 63121482Sjhb#endif 6426156Sse 65151658Sjhb/* 66151658Sjhb * Describe an interrupt thread. There is one of these per interrupt event. 67151658Sjhb */ 68151658Sjhbstruct intr_thread { 69151658Sjhb struct intr_event *it_event; 70151658Sjhb struct thread *it_thread; /* Kernel thread. */ 71151658Sjhb int it_flags; /* (j) IT_* flags. */ 72151658Sjhb int it_need; /* Needs service. */ 7372759Sjhb}; 7472759Sjhb 75151658Sjhb/* Interrupt thread flags kept in it_flags */ 76151658Sjhb#define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 77151658Sjhb 78151658Sjhbstruct intr_entropy { 79151658Sjhb struct thread *td; 80151658Sjhb uintptr_t event; 81151658Sjhb}; 82151658Sjhb 83151658Sjhbstruct intr_event *clk_intr_event; 84151658Sjhbstruct intr_event *tty_intr_event; 85128339Sbdevoid *vm_ih; 86173004Sjulianstruct proc *intrproc; 8738244Sbde 8872237Sjhbstatic MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 8972237Sjhb 90168850Snjlstatic int intr_storm_threshold = 1000; 91128331SjhbTUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold); 92128331SjhbSYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW, 93128331Sjhb &intr_storm_threshold, 0, 94128339Sbde "Number of consecutive interrupts before storm protection is enabled"); 95151658Sjhbstatic TAILQ_HEAD(, intr_event) event_list = 96151658Sjhb TAILQ_HEAD_INITIALIZER(event_list); 97178092Sjeffstatic struct mtx event_lock; 98178092SjeffMTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 99128331Sjhb 100151658Sjhbstatic void intr_event_update(struct intr_event *ie); 101169320Spiso#ifdef INTR_FILTER 102177940Sjhbstatic int intr_event_schedule_thread(struct intr_event *ie, 103177940Sjhb struct intr_thread *ithd); 104177940Sjhbstatic int intr_filter_loop(struct intr_event *ie, 105177940Sjhb struct trapframe *frame, struct intr_thread **ithd); 106169320Spisostatic struct intr_thread *ithread_create(const char *name, 107169320Spiso struct intr_handler *ih); 108169320Spiso#else 109177940Sjhbstatic int intr_event_schedule_thread(struct intr_event *ie); 110151658Sjhbstatic struct intr_thread *ithread_create(const char *name); 111169320Spiso#endif 112151658Sjhbstatic void ithread_destroy(struct intr_thread *ithread); 113169320Spisostatic void ithread_execute_handlers(struct proc *p, 114169320Spiso struct intr_event *ie); 115169320Spiso#ifdef INTR_FILTER 116169320Spisostatic void priv_ithread_execute_handler(struct proc *p, 117169320Spiso struct intr_handler *ih); 118169320Spiso#endif 119128339Sbdestatic void ithread_loop(void *); 120151658Sjhbstatic void ithread_update(struct intr_thread *ithd); 121128339Sbdestatic void start_softintr(void *); 122128339Sbde 123165124Sjhb/* Map an interrupt type to an ithread priority. */ 12472237Sjhbu_char 125151658Sjhbintr_priority(enum intr_type flags) 12665822Sjhb{ 12772237Sjhb u_char pri; 12865822Sjhb 12972237Sjhb flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 13078365Speter INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 13165822Sjhb switch (flags) { 13272237Sjhb case INTR_TYPE_TTY: 13365822Sjhb pri = PI_TTYLOW; 13465822Sjhb break; 13565822Sjhb case INTR_TYPE_BIO: 13665822Sjhb /* 13765822Sjhb * XXX We need to refine this. BSD/OS distinguishes 13865822Sjhb * between tape and disk priorities. 13965822Sjhb */ 14065822Sjhb pri = PI_DISK; 14165822Sjhb break; 14265822Sjhb case INTR_TYPE_NET: 14365822Sjhb pri = PI_NET; 14465822Sjhb break; 14565822Sjhb case INTR_TYPE_CAM: 14665822Sjhb pri = PI_DISK; /* XXX or PI_CAM? */ 14765822Sjhb break; 14878365Speter case INTR_TYPE_AV: /* Audio/video */ 14978365Speter pri = PI_AV; 15078365Speter break; 15172237Sjhb case INTR_TYPE_CLK: 15272237Sjhb pri = PI_REALTIME; 15372237Sjhb break; 15465822Sjhb case INTR_TYPE_MISC: 15565822Sjhb pri = PI_DULL; /* don't care */ 15665822Sjhb break; 15765822Sjhb default: 15872237Sjhb /* We didn't specify an interrupt level. */ 159151658Sjhb panic("intr_priority: no interrupt type in flags"); 16065822Sjhb } 16165822Sjhb 16265822Sjhb return pri; 16365822Sjhb} 16465822Sjhb 16572237Sjhb/* 166151658Sjhb * Update an ithread based on the associated intr_event. 16772237Sjhb */ 16872237Sjhbstatic void 169151658Sjhbithread_update(struct intr_thread *ithd) 17072237Sjhb{ 171151658Sjhb struct intr_event *ie; 17283366Sjulian struct thread *td; 173151658Sjhb u_char pri; 17467551Sjhb 175151658Sjhb ie = ithd->it_event; 176151658Sjhb td = ithd->it_thread; 17772237Sjhb 178151658Sjhb /* Determine the overall priority of this event. */ 179151658Sjhb if (TAILQ_EMPTY(&ie->ie_handlers)) 180151658Sjhb pri = PRI_MAX_ITHD; 181151658Sjhb else 182151658Sjhb pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri; 183105354Srobert 184151658Sjhb /* Update name and priority. */ 185173004Sjulian strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 186170307Sjeff thread_lock(td); 187151658Sjhb sched_prio(td, pri); 188170307Sjeff thread_unlock(td); 189151658Sjhb} 190151658Sjhb 191151658Sjhb/* 192151658Sjhb * Regenerate the full name of an interrupt event and update its priority. 193151658Sjhb */ 194151658Sjhbstatic void 195151658Sjhbintr_event_update(struct intr_event *ie) 196151658Sjhb{ 197151658Sjhb struct intr_handler *ih; 198151658Sjhb char *last; 199151658Sjhb int missed, space; 200151658Sjhb 201151658Sjhb /* Start off with no entropy and just the name of the event. */ 202151658Sjhb mtx_assert(&ie->ie_lock, MA_OWNED); 203151658Sjhb strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 204151658Sjhb ie->ie_flags &= ~IE_ENTROPY; 205137267Sjhb missed = 0; 206151658Sjhb space = 1; 207151658Sjhb 208151658Sjhb /* Run through all the handlers updating values. */ 209151658Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 210151658Sjhb if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 211151658Sjhb sizeof(ie->ie_fullname)) { 212151658Sjhb strcat(ie->ie_fullname, " "); 213151658Sjhb strcat(ie->ie_fullname, ih->ih_name); 214151658Sjhb space = 0; 215137267Sjhb } else 216137267Sjhb missed++; 217137267Sjhb if (ih->ih_flags & IH_ENTROPY) 218151658Sjhb ie->ie_flags |= IE_ENTROPY; 219137267Sjhb } 220151658Sjhb 221151658Sjhb /* 222151658Sjhb * If the handler names were too long, add +'s to indicate missing 223151658Sjhb * names. If we run out of room and still have +'s to add, change 224151658Sjhb * the last character from a + to a *. 225151658Sjhb */ 226151658Sjhb last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 227137267Sjhb while (missed-- > 0) { 228151658Sjhb if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 229151658Sjhb if (*last == '+') { 230151658Sjhb *last = '*'; 231151658Sjhb break; 232151658Sjhb } else 233151658Sjhb *last = '+'; 234151658Sjhb } else if (space) { 235151658Sjhb strcat(ie->ie_fullname, " +"); 236151658Sjhb space = 0; 23772237Sjhb } else 238151658Sjhb strcat(ie->ie_fullname, "+"); 23972237Sjhb } 240151658Sjhb 241151658Sjhb /* 242151658Sjhb * If this event has an ithread, update it's priority and 243151658Sjhb * name. 244151658Sjhb */ 245151658Sjhb if (ie->ie_thread != NULL) 246151658Sjhb ithread_update(ie->ie_thread); 247151658Sjhb CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 24872237Sjhb} 24972237Sjhb 25072237Sjhbint 251183298Sobrienintr_event_create(struct intr_event **event, void *source, int flags, int irq, 252177940Sjhb void (*pre_ithread)(void *), void (*post_ithread)(void *), 253177940Sjhb void (*post_filter)(void *), int (*assign_cpu)(void *, u_char), 254177940Sjhb const char *fmt, ...) 255169320Spiso{ 256169320Spiso struct intr_event *ie; 257169320Spiso va_list ap; 25872237Sjhb 259169320Spiso /* The only valid flag during creation is IE_SOFT. */ 260169320Spiso if ((flags & ~IE_SOFT) != 0) 261169320Spiso return (EINVAL); 262169320Spiso ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 263169320Spiso ie->ie_source = source; 264177940Sjhb ie->ie_pre_ithread = pre_ithread; 265177940Sjhb ie->ie_post_ithread = post_ithread; 266177940Sjhb ie->ie_post_filter = post_filter; 267177181Sjhb ie->ie_assign_cpu = assign_cpu; 268169320Spiso ie->ie_flags = flags; 269178092Sjeff ie->ie_irq = irq; 270177181Sjhb ie->ie_cpu = NOCPU; 271169320Spiso TAILQ_INIT(&ie->ie_handlers); 272169320Spiso mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 273169320Spiso 274169320Spiso va_start(ap, fmt); 275169320Spiso vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 276169320Spiso va_end(ap); 277169320Spiso strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 278178092Sjeff mtx_lock(&event_lock); 279169320Spiso TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 280178092Sjeff mtx_unlock(&event_lock); 281169320Spiso if (event != NULL) 282169320Spiso *event = ie; 283169320Spiso CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 284169320Spiso return (0); 285169320Spiso} 286169320Spiso 287177181Sjhb/* 288177181Sjhb * Bind an interrupt event to the specified CPU. Note that not all 289177181Sjhb * platforms support binding an interrupt to a CPU. For those 290177181Sjhb * platforms this request will fail. For supported platforms, any 291177181Sjhb * associated ithreads as well as the primary interrupt context will 292177181Sjhb * be bound to the specificed CPU. Using a cpu id of NOCPU unbinds 293177181Sjhb * the interrupt event. 294177181Sjhb */ 295151658Sjhbint 296177181Sjhbintr_event_bind(struct intr_event *ie, u_char cpu) 297177181Sjhb{ 298178092Sjeff cpuset_t mask; 299178092Sjeff lwpid_t id; 300177181Sjhb int error; 301177181Sjhb 302177181Sjhb /* Need a CPU to bind to. */ 303177181Sjhb if (cpu != NOCPU && CPU_ABSENT(cpu)) 304177181Sjhb return (EINVAL); 305177181Sjhb 306177181Sjhb if (ie->ie_assign_cpu == NULL) 307177181Sjhb return (EOPNOTSUPP); 308195249Sjhb 309195249Sjhb error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 310195249Sjhb if (error) 311195249Sjhb return (error); 312195249Sjhb 313178092Sjeff /* 314195249Sjhb * If we have any ithreads try to set their mask first to verify 315195249Sjhb * permissions, etc. 316178092Sjeff */ 317177181Sjhb mtx_lock(&ie->ie_lock); 318178092Sjeff if (ie->ie_thread != NULL) { 319178092Sjeff CPU_ZERO(&mask); 320178092Sjeff if (cpu == NOCPU) 321178092Sjeff CPU_COPY(cpuset_root, &mask); 322178092Sjeff else 323178092Sjeff CPU_SET(cpu, &mask); 324178092Sjeff id = ie->ie_thread->it_thread->td_tid; 325177181Sjhb mtx_unlock(&ie->ie_lock); 326178092Sjeff error = cpuset_setthread(id, &mask); 327178092Sjeff if (error) 328178092Sjeff return (error); 329178092Sjeff } else 330178092Sjeff mtx_unlock(&ie->ie_lock); 331177181Sjhb error = ie->ie_assign_cpu(ie->ie_source, cpu); 332195249Sjhb if (error) { 333195249Sjhb mtx_lock(&ie->ie_lock); 334195249Sjhb if (ie->ie_thread != NULL) { 335195249Sjhb CPU_ZERO(&mask); 336195249Sjhb if (ie->ie_cpu == NOCPU) 337195249Sjhb CPU_COPY(cpuset_root, &mask); 338195249Sjhb else 339195249Sjhb CPU_SET(cpu, &mask); 340195249Sjhb id = ie->ie_thread->it_thread->td_tid; 341195249Sjhb mtx_unlock(&ie->ie_lock); 342195249Sjhb (void)cpuset_setthread(id, &mask); 343195249Sjhb } else 344195249Sjhb mtx_unlock(&ie->ie_lock); 345177181Sjhb return (error); 346195249Sjhb } 347195249Sjhb 348177181Sjhb mtx_lock(&ie->ie_lock); 349177181Sjhb ie->ie_cpu = cpu; 350177181Sjhb mtx_unlock(&ie->ie_lock); 351178092Sjeff 352178092Sjeff return (error); 353178092Sjeff} 354178092Sjeff 355178092Sjeffstatic struct intr_event * 356178092Sjeffintr_lookup(int irq) 357178092Sjeff{ 358178092Sjeff struct intr_event *ie; 359178092Sjeff 360178092Sjeff mtx_lock(&event_lock); 361178092Sjeff TAILQ_FOREACH(ie, &event_list, ie_list) 362178092Sjeff if (ie->ie_irq == irq && 363178092Sjeff (ie->ie_flags & IE_SOFT) == 0 && 364178092Sjeff TAILQ_FIRST(&ie->ie_handlers) != NULL) 365178092Sjeff break; 366178092Sjeff mtx_unlock(&event_lock); 367178092Sjeff return (ie); 368178092Sjeff} 369178092Sjeff 370178092Sjeffint 371178092Sjeffintr_setaffinity(int irq, void *m) 372178092Sjeff{ 373178092Sjeff struct intr_event *ie; 374178092Sjeff cpuset_t *mask; 375178092Sjeff u_char cpu; 376178092Sjeff int n; 377178092Sjeff 378178092Sjeff mask = m; 379178092Sjeff cpu = NOCPU; 380178092Sjeff /* 381178092Sjeff * If we're setting all cpus we can unbind. Otherwise make sure 382178092Sjeff * only one cpu is in the set. 383178092Sjeff */ 384178092Sjeff if (CPU_CMP(cpuset_root, mask)) { 385178092Sjeff for (n = 0; n < CPU_SETSIZE; n++) { 386178092Sjeff if (!CPU_ISSET(n, mask)) 387178092Sjeff continue; 388178092Sjeff if (cpu != NOCPU) 389178092Sjeff return (EINVAL); 390178092Sjeff cpu = (u_char)n; 391178092Sjeff } 392178092Sjeff } 393178092Sjeff ie = intr_lookup(irq); 394178092Sjeff if (ie == NULL) 395178092Sjeff return (ESRCH); 396194987Sjhb return (intr_event_bind(ie, cpu)); 397178092Sjeff} 398178092Sjeff 399178092Sjeffint 400178092Sjeffintr_getaffinity(int irq, void *m) 401178092Sjeff{ 402178092Sjeff struct intr_event *ie; 403178092Sjeff cpuset_t *mask; 404178092Sjeff 405178092Sjeff mask = m; 406178092Sjeff ie = intr_lookup(irq); 407178092Sjeff if (ie == NULL) 408178092Sjeff return (ESRCH); 409178092Sjeff CPU_ZERO(mask); 410178092Sjeff mtx_lock(&ie->ie_lock); 411178092Sjeff if (ie->ie_cpu == NOCPU) 412178092Sjeff CPU_COPY(cpuset_root, mask); 413178092Sjeff else 414178092Sjeff CPU_SET(ie->ie_cpu, mask); 415178092Sjeff mtx_unlock(&ie->ie_lock); 416177181Sjhb return (0); 417177181Sjhb} 418177181Sjhb 419177181Sjhbint 420151658Sjhbintr_event_destroy(struct intr_event *ie) 421151658Sjhb{ 422151658Sjhb 423178092Sjeff mtx_lock(&event_lock); 424151658Sjhb mtx_lock(&ie->ie_lock); 425151658Sjhb if (!TAILQ_EMPTY(&ie->ie_handlers)) { 426151658Sjhb mtx_unlock(&ie->ie_lock); 427178092Sjeff mtx_unlock(&event_lock); 428151658Sjhb return (EBUSY); 429151658Sjhb } 430151658Sjhb TAILQ_REMOVE(&event_list, ie, ie_list); 431157728Sjhb#ifndef notyet 432157728Sjhb if (ie->ie_thread != NULL) { 433157728Sjhb ithread_destroy(ie->ie_thread); 434157728Sjhb ie->ie_thread = NULL; 435157728Sjhb } 436157728Sjhb#endif 437151658Sjhb mtx_unlock(&ie->ie_lock); 438178092Sjeff mtx_unlock(&event_lock); 439151658Sjhb mtx_destroy(&ie->ie_lock); 440151658Sjhb free(ie, M_ITHREAD); 441151658Sjhb return (0); 442151658Sjhb} 443151658Sjhb 444169320Spiso#ifndef INTR_FILTER 445151658Sjhbstatic struct intr_thread * 446151658Sjhbithread_create(const char *name) 447151658Sjhb{ 448151658Sjhb struct intr_thread *ithd; 449151658Sjhb struct thread *td; 450151658Sjhb int error; 451151658Sjhb 452151658Sjhb ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 453151658Sjhb 454173004Sjulian error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 455173004Sjulian &td, RFSTOPPED | RFHIGHPID, 456173051Sjulian 0, "intr", "%s", name); 457151658Sjhb if (error) 458172836Sjulian panic("kproc_create() failed with %d", error); 459170307Sjeff thread_lock(td); 460164936Sjulian sched_class(td, PRI_ITHD); 461103216Sjulian TD_SET_IWAIT(td); 462170307Sjeff thread_unlock(td); 463151658Sjhb td->td_pflags |= TDP_ITHREAD; 464151658Sjhb ithd->it_thread = td; 465151658Sjhb CTR2(KTR_INTR, "%s: created %s", __func__, name); 466151658Sjhb return (ithd); 46772237Sjhb} 468169320Spiso#else 469169320Spisostatic struct intr_thread * 470169320Spisoithread_create(const char *name, struct intr_handler *ih) 471169320Spiso{ 472169320Spiso struct intr_thread *ithd; 473169320Spiso struct thread *td; 474169320Spiso int error; 47572237Sjhb 476169320Spiso ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 477169320Spiso 478173153Sjulian error = kproc_kthread_add(ithread_loop, ih, &intrproc, 479173004Sjulian &td, RFSTOPPED | RFHIGHPID, 480173051Sjulian 0, "intr", "%s", name); 481169320Spiso if (error) 482172836Sjulian panic("kproc_create() failed with %d", error); 483170307Sjeff thread_lock(td); 484169320Spiso sched_class(td, PRI_ITHD); 485169320Spiso TD_SET_IWAIT(td); 486170307Sjeff thread_unlock(td); 487169320Spiso td->td_pflags |= TDP_ITHREAD; 488169320Spiso ithd->it_thread = td; 489169320Spiso CTR2(KTR_INTR, "%s: created %s", __func__, name); 490169320Spiso return (ithd); 491169320Spiso} 492169320Spiso#endif 493169320Spiso 494151658Sjhbstatic void 495151658Sjhbithread_destroy(struct intr_thread *ithread) 49672237Sjhb{ 49783366Sjulian struct thread *td; 49872237Sjhb 499157784Sscottl CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 500151658Sjhb td = ithread->it_thread; 501170307Sjeff thread_lock(td); 50276771Sjhb ithread->it_flags |= IT_DEAD; 503103216Sjulian if (TD_AWAITING_INTR(td)) { 504103216Sjulian TD_CLR_IWAIT(td); 505166188Sjeff sched_add(td, SRQ_INTR); 50672237Sjhb } 507170307Sjeff thread_unlock(td); 50872237Sjhb} 50972237Sjhb 510169320Spiso#ifndef INTR_FILTER 51172237Sjhbint 512151658Sjhbintr_event_add_handler(struct intr_event *ie, const char *name, 513166901Spiso driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 514166901Spiso enum intr_type flags, void **cookiep) 51572237Sjhb{ 516151658Sjhb struct intr_handler *ih, *temp_ih; 517151658Sjhb struct intr_thread *it; 51872237Sjhb 519166901Spiso if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 52072237Sjhb return (EINVAL); 52172237Sjhb 522151658Sjhb /* Allocate and populate an interrupt handler structure. */ 523151658Sjhb ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 524166901Spiso ih->ih_filter = filter; 52572237Sjhb ih->ih_handler = handler; 52672237Sjhb ih->ih_argument = arg; 527198134Sjhb strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 528151658Sjhb ih->ih_event = ie; 52972237Sjhb ih->ih_pri = pri; 530166901Spiso if (flags & INTR_EXCL) 53172237Sjhb ih->ih_flags = IH_EXCLUSIVE; 53272237Sjhb if (flags & INTR_MPSAFE) 53372237Sjhb ih->ih_flags |= IH_MPSAFE; 53472237Sjhb if (flags & INTR_ENTROPY) 53572237Sjhb ih->ih_flags |= IH_ENTROPY; 53672237Sjhb 537151658Sjhb /* We can only have one exclusive handler in a event. */ 538151658Sjhb mtx_lock(&ie->ie_lock); 539151658Sjhb if (!TAILQ_EMPTY(&ie->ie_handlers)) { 540151658Sjhb if ((flags & INTR_EXCL) || 541151658Sjhb (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 542151658Sjhb mtx_unlock(&ie->ie_lock); 543151658Sjhb free(ih, M_ITHREAD); 544151658Sjhb return (EINVAL); 545151658Sjhb } 546122002Sjhb } 54772237Sjhb 548151658Sjhb /* Add the new handler to the event in priority order. */ 549151658Sjhb TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 550151658Sjhb if (temp_ih->ih_pri > ih->ih_pri) 551151658Sjhb break; 552151658Sjhb } 55372237Sjhb if (temp_ih == NULL) 554151658Sjhb TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 55572237Sjhb else 55672237Sjhb TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 557151658Sjhb intr_event_update(ie); 55872237Sjhb 559151658Sjhb /* Create a thread if we need one. */ 560166901Spiso while (ie->ie_thread == NULL && handler != NULL) { 561151658Sjhb if (ie->ie_flags & IE_ADDING_THREAD) 562157815Sjhb msleep(ie, &ie->ie_lock, 0, "ithread", 0); 563151658Sjhb else { 564151658Sjhb ie->ie_flags |= IE_ADDING_THREAD; 565151658Sjhb mtx_unlock(&ie->ie_lock); 566151658Sjhb it = ithread_create("intr: newborn"); 567151658Sjhb mtx_lock(&ie->ie_lock); 568151658Sjhb ie->ie_flags &= ~IE_ADDING_THREAD; 569151658Sjhb ie->ie_thread = it; 570151658Sjhb it->it_event = ie; 571151658Sjhb ithread_update(it); 572151658Sjhb wakeup(ie); 573151658Sjhb } 574151658Sjhb } 575151658Sjhb CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 576151658Sjhb ie->ie_name); 577151658Sjhb mtx_unlock(&ie->ie_lock); 578151658Sjhb 57972237Sjhb if (cookiep != NULL) 58072237Sjhb *cookiep = ih; 58172237Sjhb return (0); 58272237Sjhb} 583169320Spiso#else 584169320Spisoint 585169320Spisointr_event_add_handler(struct intr_event *ie, const char *name, 586169320Spiso driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 587169320Spiso enum intr_type flags, void **cookiep) 588169320Spiso{ 589169320Spiso struct intr_handler *ih, *temp_ih; 590169320Spiso struct intr_thread *it; 59172237Sjhb 592169320Spiso if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 593169320Spiso return (EINVAL); 594169320Spiso 595169320Spiso /* Allocate and populate an interrupt handler structure. */ 596169320Spiso ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 597169320Spiso ih->ih_filter = filter; 598169320Spiso ih->ih_handler = handler; 599169320Spiso ih->ih_argument = arg; 600198134Sjhb strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 601169320Spiso ih->ih_event = ie; 602169320Spiso ih->ih_pri = pri; 603169320Spiso if (flags & INTR_EXCL) 604169320Spiso ih->ih_flags = IH_EXCLUSIVE; 605169320Spiso if (flags & INTR_MPSAFE) 606169320Spiso ih->ih_flags |= IH_MPSAFE; 607169320Spiso if (flags & INTR_ENTROPY) 608169320Spiso ih->ih_flags |= IH_ENTROPY; 609169320Spiso 610169320Spiso /* We can only have one exclusive handler in a event. */ 611169320Spiso mtx_lock(&ie->ie_lock); 612169320Spiso if (!TAILQ_EMPTY(&ie->ie_handlers)) { 613169320Spiso if ((flags & INTR_EXCL) || 614169320Spiso (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 615169320Spiso mtx_unlock(&ie->ie_lock); 616169320Spiso free(ih, M_ITHREAD); 617169320Spiso return (EINVAL); 618169320Spiso } 619169320Spiso } 620169320Spiso 621169320Spiso /* Add the new handler to the event in priority order. */ 622169320Spiso TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 623169320Spiso if (temp_ih->ih_pri > ih->ih_pri) 624169320Spiso break; 625169320Spiso } 626169320Spiso if (temp_ih == NULL) 627169320Spiso TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 628169320Spiso else 629169320Spiso TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 630169320Spiso intr_event_update(ie); 631169320Spiso 632169320Spiso /* For filtered handlers, create a private ithread to run on. */ 633169320Spiso if (filter != NULL && handler != NULL) { 634169320Spiso mtx_unlock(&ie->ie_lock); 635169320Spiso it = ithread_create("intr: newborn", ih); 636169320Spiso mtx_lock(&ie->ie_lock); 637169320Spiso it->it_event = ie; 638169320Spiso ih->ih_thread = it; 639169320Spiso ithread_update(it); // XXX - do we really need this?!?!? 640169320Spiso } else { /* Create the global per-event thread if we need one. */ 641169320Spiso while (ie->ie_thread == NULL && handler != NULL) { 642169320Spiso if (ie->ie_flags & IE_ADDING_THREAD) 643169320Spiso msleep(ie, &ie->ie_lock, 0, "ithread", 0); 644169320Spiso else { 645169320Spiso ie->ie_flags |= IE_ADDING_THREAD; 646169320Spiso mtx_unlock(&ie->ie_lock); 647169320Spiso it = ithread_create("intr: newborn", ih); 648169320Spiso mtx_lock(&ie->ie_lock); 649169320Spiso ie->ie_flags &= ~IE_ADDING_THREAD; 650169320Spiso ie->ie_thread = it; 651169320Spiso it->it_event = ie; 652169320Spiso ithread_update(it); 653169320Spiso wakeup(ie); 654169320Spiso } 655169320Spiso } 656169320Spiso } 657169320Spiso CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 658169320Spiso ie->ie_name); 659169320Spiso mtx_unlock(&ie->ie_lock); 660169320Spiso 661169320Spiso if (cookiep != NULL) 662169320Spiso *cookiep = ih; 663169320Spiso return (0); 664169320Spiso} 665169320Spiso#endif 666169320Spiso 667165125Sjhb/* 668198134Sjhb * Append a description preceded by a ':' to the name of the specified 669198134Sjhb * interrupt handler. 670198134Sjhb */ 671198134Sjhbint 672198134Sjhbintr_event_describe_handler(struct intr_event *ie, void *cookie, 673198134Sjhb const char *descr) 674198134Sjhb{ 675198134Sjhb struct intr_handler *ih; 676198134Sjhb size_t space; 677198134Sjhb char *start; 678198134Sjhb 679198134Sjhb mtx_lock(&ie->ie_lock); 680198134Sjhb#ifdef INVARIANTS 681198134Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 682198134Sjhb if (ih == cookie) 683198134Sjhb break; 684198134Sjhb } 685198134Sjhb if (ih == NULL) { 686198134Sjhb mtx_unlock(&ie->ie_lock); 687198149Sjhb panic("handler %p not found in interrupt event %p", cookie, ie); 688198134Sjhb } 689198134Sjhb#endif 690198134Sjhb ih = cookie; 691198134Sjhb 692198134Sjhb /* 693198134Sjhb * Look for an existing description by checking for an 694198134Sjhb * existing ":". This assumes device names do not include 695198134Sjhb * colons. If one is found, prepare to insert the new 696198134Sjhb * description at that point. If one is not found, find the 697198134Sjhb * end of the name to use as the insertion point. 698198134Sjhb */ 699198134Sjhb start = index(ih->ih_name, ':'); 700198134Sjhb if (start == NULL) 701198134Sjhb start = index(ih->ih_name, 0); 702198134Sjhb 703198134Sjhb /* 704198134Sjhb * See if there is enough remaining room in the string for the 705198134Sjhb * description + ":". The "- 1" leaves room for the trailing 706198134Sjhb * '\0'. The "+ 1" accounts for the colon. 707198134Sjhb */ 708198134Sjhb space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 709198134Sjhb if (strlen(descr) + 1 > space) { 710198134Sjhb mtx_unlock(&ie->ie_lock); 711198134Sjhb return (ENOSPC); 712198134Sjhb } 713198134Sjhb 714198134Sjhb /* Append a colon followed by the description. */ 715198134Sjhb *start = ':'; 716198134Sjhb strcpy(start + 1, descr); 717198134Sjhb intr_event_update(ie); 718198134Sjhb mtx_unlock(&ie->ie_lock); 719198134Sjhb return (0); 720198134Sjhb} 721198134Sjhb 722198134Sjhb/* 723165125Sjhb * Return the ie_source field from the intr_event an intr_handler is 724165125Sjhb * associated with. 725165125Sjhb */ 726165125Sjhbvoid * 727165125Sjhbintr_handler_source(void *cookie) 728165125Sjhb{ 729165125Sjhb struct intr_handler *ih; 730165125Sjhb struct intr_event *ie; 731165125Sjhb 732165125Sjhb ih = (struct intr_handler *)cookie; 733165125Sjhb if (ih == NULL) 734165125Sjhb return (NULL); 735165125Sjhb ie = ih->ih_event; 736165125Sjhb KASSERT(ie != NULL, 737165125Sjhb ("interrupt handler \"%s\" has a NULL interrupt event", 738165125Sjhb ih->ih_name)); 739165125Sjhb return (ie->ie_source); 740165125Sjhb} 741165125Sjhb 742169320Spiso#ifndef INTR_FILTER 74372237Sjhbint 744151658Sjhbintr_event_remove_handler(void *cookie) 74572237Sjhb{ 746151658Sjhb struct intr_handler *handler = (struct intr_handler *)cookie; 747151658Sjhb struct intr_event *ie; 74872237Sjhb#ifdef INVARIANTS 749151658Sjhb struct intr_handler *ih; 75072237Sjhb#endif 751151658Sjhb#ifdef notyet 752151658Sjhb int dead; 753151658Sjhb#endif 75472237Sjhb 75572759Sjhb if (handler == NULL) 75672237Sjhb return (EINVAL); 757151658Sjhb ie = handler->ih_event; 758151658Sjhb KASSERT(ie != NULL, 759151658Sjhb ("interrupt handler \"%s\" has a NULL interrupt event", 760165124Sjhb handler->ih_name)); 761151658Sjhb mtx_lock(&ie->ie_lock); 76287593Sobrien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 763151658Sjhb ie->ie_name); 76472237Sjhb#ifdef INVARIANTS 765151658Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 76672759Sjhb if (ih == handler) 76772759Sjhb goto ok; 768151658Sjhb mtx_unlock(&ie->ie_lock); 769151658Sjhb panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 770151658Sjhb ih->ih_name, ie->ie_name); 77172759Sjhbok: 77272237Sjhb#endif 77372839Sjhb /* 774151658Sjhb * If there is no ithread, then just remove the handler and return. 775151658Sjhb * XXX: Note that an INTR_FAST handler might be running on another 776151658Sjhb * CPU! 777151658Sjhb */ 778151658Sjhb if (ie->ie_thread == NULL) { 779151658Sjhb TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 780151658Sjhb mtx_unlock(&ie->ie_lock); 781151658Sjhb free(handler, M_ITHREAD); 782151658Sjhb return (0); 783151658Sjhb } 784151658Sjhb 785151658Sjhb /* 78672839Sjhb * If the interrupt thread is already running, then just mark this 78772839Sjhb * handler as being dead and let the ithread do the actual removal. 788124505Struckman * 789124505Struckman * During a cold boot while cold is set, msleep() does not sleep, 790124505Struckman * so we have to remove the handler here rather than letting the 791124505Struckman * thread do it. 79272839Sjhb */ 793170307Sjeff thread_lock(ie->ie_thread->it_thread); 794151658Sjhb if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) { 79572839Sjhb handler->ih_flags |= IH_DEAD; 79672839Sjhb 79772839Sjhb /* 79872839Sjhb * Ensure that the thread will process the handler list 79972839Sjhb * again and remove this handler if it has already passed 80072839Sjhb * it on the list. 80172839Sjhb */ 802151658Sjhb ie->ie_thread->it_need = 1; 803151658Sjhb } else 804151658Sjhb TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 805170307Sjeff thread_unlock(ie->ie_thread->it_thread); 806151658Sjhb while (handler->ih_flags & IH_DEAD) 807157815Sjhb msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 808151658Sjhb intr_event_update(ie); 809151658Sjhb#ifdef notyet 810151658Sjhb /* 811151658Sjhb * XXX: This could be bad in the case of ppbus(8). Also, I think 812151658Sjhb * this could lead to races of stale data when servicing an 813151658Sjhb * interrupt. 814151658Sjhb */ 815151658Sjhb dead = 1; 816151658Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 817151658Sjhb if (!(ih->ih_flags & IH_FAST)) { 818151658Sjhb dead = 0; 819151658Sjhb break; 820151658Sjhb } 821151658Sjhb } 822151658Sjhb if (dead) { 823151658Sjhb ithread_destroy(ie->ie_thread); 824151658Sjhb ie->ie_thread = NULL; 825151658Sjhb } 826151658Sjhb#endif 827151658Sjhb mtx_unlock(&ie->ie_lock); 82876771Sjhb free(handler, M_ITHREAD); 82972237Sjhb return (0); 83072237Sjhb} 83172237Sjhb 832177940Sjhbstatic int 833151658Sjhbintr_event_schedule_thread(struct intr_event *ie) 83472759Sjhb{ 835151658Sjhb struct intr_entropy entropy; 836151658Sjhb struct intr_thread *it; 83783366Sjulian struct thread *td; 838101176Sjulian struct thread *ctd; 83972759Sjhb struct proc *p; 84072759Sjhb 84172759Sjhb /* 84272759Sjhb * If no ithread or no handlers, then we have a stray interrupt. 84372759Sjhb */ 844151658Sjhb if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || 845151658Sjhb ie->ie_thread == NULL) 84672759Sjhb return (EINVAL); 84772759Sjhb 848101176Sjulian ctd = curthread; 849151658Sjhb it = ie->ie_thread; 850151658Sjhb td = it->it_thread; 851133191Srwatson p = td->td_proc; 852151658Sjhb 85372759Sjhb /* 85472759Sjhb * If any of the handlers for this ithread claim to be good 85572759Sjhb * sources of entropy, then gather some. 85672759Sjhb */ 857151658Sjhb if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 858133191Srwatson CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 859173004Sjulian p->p_pid, td->td_name); 860151658Sjhb entropy.event = (uintptr_t)ie; 861151658Sjhb entropy.td = ctd; 86272759Sjhb random_harvest(&entropy, sizeof(entropy), 2, 0, 86372759Sjhb RANDOM_INTERRUPT); 86472759Sjhb } 86572759Sjhb 866151658Sjhb KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 86772759Sjhb 86872759Sjhb /* 86972759Sjhb * Set it_need to tell the thread to keep running if it is already 870170307Sjeff * running. Then, lock the thread and see if we actually need to 871170307Sjeff * put it on the runqueue. 87272759Sjhb */ 873151658Sjhb it->it_need = 1; 874170307Sjeff thread_lock(td); 875103216Sjulian if (TD_AWAITING_INTR(td)) { 876151658Sjhb CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 877173004Sjulian td->td_name); 878103216Sjulian TD_CLR_IWAIT(td); 879166188Sjeff sched_add(td, SRQ_INTR); 88072759Sjhb } else { 881151658Sjhb CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 882173004Sjulian __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 88372759Sjhb } 884170307Sjeff thread_unlock(td); 88572759Sjhb 88672759Sjhb return (0); 88772759Sjhb} 888169320Spiso#else 889169320Spisoint 890169320Spisointr_event_remove_handler(void *cookie) 891169320Spiso{ 892169320Spiso struct intr_handler *handler = (struct intr_handler *)cookie; 893169320Spiso struct intr_event *ie; 894169320Spiso struct intr_thread *it; 895169320Spiso#ifdef INVARIANTS 896169320Spiso struct intr_handler *ih; 897169320Spiso#endif 898169320Spiso#ifdef notyet 899169320Spiso int dead; 900169320Spiso#endif 90172759Sjhb 902169320Spiso if (handler == NULL) 903169320Spiso return (EINVAL); 904169320Spiso ie = handler->ih_event; 905169320Spiso KASSERT(ie != NULL, 906169320Spiso ("interrupt handler \"%s\" has a NULL interrupt event", 907169320Spiso handler->ih_name)); 908169320Spiso mtx_lock(&ie->ie_lock); 909169320Spiso CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 910169320Spiso ie->ie_name); 911169320Spiso#ifdef INVARIANTS 912169320Spiso TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 913169320Spiso if (ih == handler) 914169320Spiso goto ok; 915169320Spiso mtx_unlock(&ie->ie_lock); 916169320Spiso panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 917169320Spiso ih->ih_name, ie->ie_name); 918169320Spisook: 919169320Spiso#endif 920169320Spiso /* 921169320Spiso * If there are no ithreads (per event and per handler), then 922169320Spiso * just remove the handler and return. 923169320Spiso * XXX: Note that an INTR_FAST handler might be running on another CPU! 924169320Spiso */ 925169320Spiso if (ie->ie_thread == NULL && handler->ih_thread == NULL) { 926169320Spiso TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 927169320Spiso mtx_unlock(&ie->ie_lock); 928169320Spiso free(handler, M_ITHREAD); 929169320Spiso return (0); 930169320Spiso } 931169320Spiso 932169320Spiso /* Private or global ithread? */ 933169320Spiso it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread; 934169320Spiso /* 935169320Spiso * If the interrupt thread is already running, then just mark this 936169320Spiso * handler as being dead and let the ithread do the actual removal. 937169320Spiso * 938169320Spiso * During a cold boot while cold is set, msleep() does not sleep, 939169320Spiso * so we have to remove the handler here rather than letting the 940169320Spiso * thread do it. 941169320Spiso */ 942170307Sjeff thread_lock(it->it_thread); 943169320Spiso if (!TD_AWAITING_INTR(it->it_thread) && !cold) { 944169320Spiso handler->ih_flags |= IH_DEAD; 945169320Spiso 946169320Spiso /* 947169320Spiso * Ensure that the thread will process the handler list 948169320Spiso * again and remove this handler if it has already passed 949169320Spiso * it on the list. 950169320Spiso */ 951169320Spiso it->it_need = 1; 952169320Spiso } else 953169320Spiso TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 954170307Sjeff thread_unlock(it->it_thread); 955169320Spiso while (handler->ih_flags & IH_DEAD) 956169320Spiso msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 957169320Spiso /* 958169320Spiso * At this point, the handler has been disconnected from the event, 959169320Spiso * so we can kill the private ithread if any. 960169320Spiso */ 961169320Spiso if (handler->ih_thread) { 962169320Spiso ithread_destroy(handler->ih_thread); 963169320Spiso handler->ih_thread = NULL; 964169320Spiso } 965169320Spiso intr_event_update(ie); 966169320Spiso#ifdef notyet 967169320Spiso /* 968169320Spiso * XXX: This could be bad in the case of ppbus(8). Also, I think 969169320Spiso * this could lead to races of stale data when servicing an 970169320Spiso * interrupt. 971169320Spiso */ 972169320Spiso dead = 1; 973169320Spiso TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 974169320Spiso if (handler != NULL) { 975169320Spiso dead = 0; 976169320Spiso break; 977169320Spiso } 978169320Spiso } 979169320Spiso if (dead) { 980169320Spiso ithread_destroy(ie->ie_thread); 981169320Spiso ie->ie_thread = NULL; 982169320Spiso } 983169320Spiso#endif 984169320Spiso mtx_unlock(&ie->ie_lock); 985169320Spiso free(handler, M_ITHREAD); 986169320Spiso return (0); 987169320Spiso} 988169320Spiso 989177940Sjhbstatic int 990169320Spisointr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it) 991169320Spiso{ 992169320Spiso struct intr_entropy entropy; 993169320Spiso struct thread *td; 994169320Spiso struct thread *ctd; 995169320Spiso struct proc *p; 996169320Spiso 997169320Spiso /* 998169320Spiso * If no ithread or no handlers, then we have a stray interrupt. 999169320Spiso */ 1000169320Spiso if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL) 1001169320Spiso return (EINVAL); 1002169320Spiso 1003169320Spiso ctd = curthread; 1004169320Spiso td = it->it_thread; 1005169320Spiso p = td->td_proc; 1006169320Spiso 1007169320Spiso /* 1008169320Spiso * If any of the handlers for this ithread claim to be good 1009169320Spiso * sources of entropy, then gather some. 1010169320Spiso */ 1011169320Spiso if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 1012169320Spiso CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 1013173004Sjulian p->p_pid, td->td_name); 1014169320Spiso entropy.event = (uintptr_t)ie; 1015169320Spiso entropy.td = ctd; 1016169320Spiso random_harvest(&entropy, sizeof(entropy), 2, 0, 1017169320Spiso RANDOM_INTERRUPT); 1018169320Spiso } 1019169320Spiso 1020169320Spiso KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 1021169320Spiso 1022169320Spiso /* 1023169320Spiso * Set it_need to tell the thread to keep running if it is already 1024170307Sjeff * running. Then, lock the thread and see if we actually need to 1025170307Sjeff * put it on the runqueue. 1026169320Spiso */ 1027169320Spiso it->it_need = 1; 1028170307Sjeff thread_lock(td); 1029169320Spiso if (TD_AWAITING_INTR(td)) { 1030169320Spiso CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 1031173122Sjulian td->td_name); 1032169320Spiso TD_CLR_IWAIT(td); 1033169320Spiso sched_add(td, SRQ_INTR); 1034169320Spiso } else { 1035169320Spiso CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 1036173004Sjulian __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 1037169320Spiso } 1038170307Sjeff thread_unlock(td); 1039169320Spiso 1040169320Spiso return (0); 1041169320Spiso} 1042169320Spiso#endif 1043169320Spiso 1044151699Sjhb/* 1045192305Srwatson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1046192305Srwatson * since interrupts are generated in software rather than being directed by 1047192305Srwatson * a PIC. 1048192305Srwatson */ 1049192305Srwatsonstatic int 1050192305Srwatsonswi_assign_cpu(void *arg, u_char cpu) 1051192305Srwatson{ 1052192305Srwatson 1053192305Srwatson return (0); 1054192305Srwatson} 1055192305Srwatson 1056192305Srwatson/* 1057151699Sjhb * Add a software interrupt handler to a specified event. If a given event 1058151699Sjhb * is not specified, then a new event is created. 1059151699Sjhb */ 106072759Sjhbint 1061151658Sjhbswi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 106272237Sjhb void *arg, int pri, enum intr_type flags, void **cookiep) 106372237Sjhb{ 1064151658Sjhb struct intr_event *ie; 106572237Sjhb int error; 106666698Sjhb 1067169320Spiso if (flags & INTR_ENTROPY) 106872759Sjhb return (EINVAL); 106972759Sjhb 1070151658Sjhb ie = (eventp != NULL) ? *eventp : NULL; 107166698Sjhb 1072151658Sjhb if (ie != NULL) { 1073151658Sjhb if (!(ie->ie_flags & IE_SOFT)) 1074151658Sjhb return (EINVAL); 107572759Sjhb } else { 1076178092Sjeff error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1077192305Srwatson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 107867551Sjhb if (error) 107972237Sjhb return (error); 1080151658Sjhb if (eventp != NULL) 1081151658Sjhb *eventp = ie; 108266698Sjhb } 1083177859Sjeff error = intr_event_add_handler(ie, name, NULL, handler, arg, 1084177859Sjeff (pri * RQ_PPQ) + PI_SOFT, flags, cookiep); 1085177859Sjeff if (error) 1086177859Sjeff return (error); 1087177859Sjeff if (pri == SWI_CLOCK) { 1088177859Sjeff struct proc *p; 1089177859Sjeff p = ie->ie_thread->it_thread->td_proc; 1090177859Sjeff PROC_LOCK(p); 1091177859Sjeff p->p_flag |= P_NOLOAD; 1092177859Sjeff PROC_UNLOCK(p); 1093177859Sjeff } 1094177859Sjeff return (0); 109566698Sjhb} 109666698Sjhb 109766698Sjhb/* 1098151658Sjhb * Schedule a software interrupt thread. 109966698Sjhb */ 110067551Sjhbvoid 110172237Sjhbswi_sched(void *cookie, int flags) 110266698Sjhb{ 1103151658Sjhb struct intr_handler *ih = (struct intr_handler *)cookie; 1104151658Sjhb struct intr_event *ie = ih->ih_event; 110572759Sjhb int error; 110666698Sjhb 1107151658Sjhb CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1108151658Sjhb ih->ih_need); 1109151658Sjhb 111067551Sjhb /* 111172759Sjhb * Set ih_need for this handler so that if the ithread is already 111272759Sjhb * running it will execute this handler on the next pass. Otherwise, 111372759Sjhb * it will execute it the next time it runs. 111467551Sjhb */ 111572237Sjhb atomic_store_rel_int(&ih->ih_need, 1); 1116163474Sbde 111772237Sjhb if (!(flags & SWI_DELAY)) { 1118170291Sattilio PCPU_INC(cnt.v_soft); 1119169320Spiso#ifdef INTR_FILTER 1120169320Spiso error = intr_event_schedule_thread(ie, ie->ie_thread); 1121169320Spiso#else 1122151658Sjhb error = intr_event_schedule_thread(ie); 1123169320Spiso#endif 112472759Sjhb KASSERT(error == 0, ("stray software interrupt")); 112566698Sjhb } 112666698Sjhb} 112766698Sjhb 1128151699Sjhb/* 1129151699Sjhb * Remove a software interrupt handler. Currently this code does not 1130151699Sjhb * remove the associated interrupt event if it becomes empty. Calling code 1131151699Sjhb * may do so manually via intr_event_destroy(), but that's not really 1132151699Sjhb * an optimal interface. 1133151699Sjhb */ 1134151699Sjhbint 1135151699Sjhbswi_remove(void *cookie) 1136151699Sjhb{ 1137151699Sjhb 1138151699Sjhb return (intr_event_remove_handler(cookie)); 1139151699Sjhb} 1140151699Sjhb 1141169320Spiso#ifdef INTR_FILTER 1142151658Sjhbstatic void 1143169320Spisopriv_ithread_execute_handler(struct proc *p, struct intr_handler *ih) 1144169320Spiso{ 1145169320Spiso struct intr_event *ie; 1146169320Spiso 1147169320Spiso ie = ih->ih_event; 1148169320Spiso /* 1149169320Spiso * If this handler is marked for death, remove it from 1150169320Spiso * the list of handlers and wake up the sleeper. 1151169320Spiso */ 1152169320Spiso if (ih->ih_flags & IH_DEAD) { 1153169320Spiso mtx_lock(&ie->ie_lock); 1154169320Spiso TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1155169320Spiso ih->ih_flags &= ~IH_DEAD; 1156169320Spiso wakeup(ih); 1157169320Spiso mtx_unlock(&ie->ie_lock); 1158169320Spiso return; 1159169320Spiso } 1160169320Spiso 1161169320Spiso /* Execute this handler. */ 1162169320Spiso CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1163169320Spiso __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument, 1164169320Spiso ih->ih_name, ih->ih_flags); 1165169320Spiso 1166169320Spiso if (!(ih->ih_flags & IH_MPSAFE)) 1167169320Spiso mtx_lock(&Giant); 1168169320Spiso ih->ih_handler(ih->ih_argument); 1169169320Spiso if (!(ih->ih_flags & IH_MPSAFE)) 1170169320Spiso mtx_unlock(&Giant); 1171169320Spiso} 1172169320Spiso#endif 1173169320Spiso 1174183052Sjhb/* 1175183052Sjhb * This is a public function for use by drivers that mux interrupt 1176183052Sjhb * handlers for child devices from their interrupt handler. 1177183052Sjhb */ 1178183052Sjhbvoid 1179183052Sjhbintr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1180151658Sjhb{ 1181151658Sjhb struct intr_handler *ih, *ihn; 1182151658Sjhb 1183151658Sjhb TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1184151658Sjhb /* 1185151658Sjhb * If this handler is marked for death, remove it from 1186151658Sjhb * the list of handlers and wake up the sleeper. 1187151658Sjhb */ 1188151658Sjhb if (ih->ih_flags & IH_DEAD) { 1189151658Sjhb mtx_lock(&ie->ie_lock); 1190151658Sjhb TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1191151658Sjhb ih->ih_flags &= ~IH_DEAD; 1192151658Sjhb wakeup(ih); 1193151658Sjhb mtx_unlock(&ie->ie_lock); 1194151658Sjhb continue; 1195151658Sjhb } 1196151658Sjhb 1197167080Spiso /* Skip filter only handlers */ 1198167080Spiso if (ih->ih_handler == NULL) 1199167080Spiso continue; 1200167080Spiso 1201151658Sjhb /* 1202151658Sjhb * For software interrupt threads, we only execute 1203151658Sjhb * handlers that have their need flag set. Hardware 1204151658Sjhb * interrupt threads always invoke all of their handlers. 1205151658Sjhb */ 1206151658Sjhb if (ie->ie_flags & IE_SOFT) { 1207151658Sjhb if (!ih->ih_need) 1208151658Sjhb continue; 1209151658Sjhb else 1210151658Sjhb atomic_store_rel_int(&ih->ih_need, 0); 1211151658Sjhb } 1212151658Sjhb 1213151658Sjhb /* Execute this handler. */ 1214151658Sjhb CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1215169320Spiso __func__, p->p_pid, (void *)ih->ih_handler, 1216169320Spiso ih->ih_argument, ih->ih_name, ih->ih_flags); 1217151658Sjhb 1218151658Sjhb if (!(ih->ih_flags & IH_MPSAFE)) 1219151658Sjhb mtx_lock(&Giant); 1220151658Sjhb ih->ih_handler(ih->ih_argument); 1221151658Sjhb if (!(ih->ih_flags & IH_MPSAFE)) 1222151658Sjhb mtx_unlock(&Giant); 1223151658Sjhb } 1224183052Sjhb} 1225183052Sjhb 1226183052Sjhbstatic void 1227183052Sjhbithread_execute_handlers(struct proc *p, struct intr_event *ie) 1228183052Sjhb{ 1229183052Sjhb 1230183052Sjhb /* Interrupt handlers should not sleep. */ 1231151658Sjhb if (!(ie->ie_flags & IE_SOFT)) 1232183052Sjhb THREAD_NO_SLEEPING(); 1233183052Sjhb intr_event_execute_handlers(p, ie); 1234183052Sjhb if (!(ie->ie_flags & IE_SOFT)) 1235151658Sjhb THREAD_SLEEPING_OK(); 1236151658Sjhb 1237151658Sjhb /* 1238151658Sjhb * Interrupt storm handling: 1239151658Sjhb * 1240151658Sjhb * If this interrupt source is currently storming, then throttle 1241151658Sjhb * it to only fire the handler once per clock tick. 1242151658Sjhb * 1243151658Sjhb * If this interrupt source is not currently storming, but the 1244151658Sjhb * number of back to back interrupts exceeds the storm threshold, 1245151658Sjhb * then enter storming mode. 1246151658Sjhb */ 1247167173Sjhb if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1248167173Sjhb !(ie->ie_flags & IE_SOFT)) { 1249168850Snjl /* Report the message only once every second. */ 1250168850Snjl if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1251151658Sjhb printf( 1252168850Snjl "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1253151658Sjhb ie->ie_name); 1254151658Sjhb } 1255167173Sjhb pause("istorm", 1); 1256151658Sjhb } else 1257151658Sjhb ie->ie_count++; 1258151658Sjhb 1259151658Sjhb /* 1260151658Sjhb * Now that all the handlers have had a chance to run, reenable 1261151658Sjhb * the interrupt source. 1262151658Sjhb */ 1263177940Sjhb if (ie->ie_post_ithread != NULL) 1264177940Sjhb ie->ie_post_ithread(ie->ie_source); 1265151658Sjhb} 1266151658Sjhb 1267169320Spiso#ifndef INTR_FILTER 126866698Sjhb/* 126972237Sjhb * This is the main code for interrupt threads. 127066698Sjhb */ 1271104094Sphkstatic void 127272237Sjhbithread_loop(void *arg) 127366698Sjhb{ 1274151658Sjhb struct intr_thread *ithd; 1275151658Sjhb struct intr_event *ie; 127683366Sjulian struct thread *td; 127772237Sjhb struct proc *p; 1278151658Sjhb 127983366Sjulian td = curthread; 128083366Sjulian p = td->td_proc; 1281151658Sjhb ithd = (struct intr_thread *)arg; 1282151658Sjhb KASSERT(ithd->it_thread == td, 128387593Sobrien ("%s: ithread and proc linkage out of sync", __func__)); 1284151658Sjhb ie = ithd->it_event; 1285151658Sjhb ie->ie_count = 0; 128666698Sjhb 128767551Sjhb /* 128867551Sjhb * As long as we have interrupts outstanding, go through the 128967551Sjhb * list of handlers, giving each one a go at it. 129067551Sjhb */ 129166698Sjhb for (;;) { 129272237Sjhb /* 129372237Sjhb * If we are an orphaned thread, then just die. 129472237Sjhb */ 129572237Sjhb if (ithd->it_flags & IT_DEAD) { 1296151658Sjhb CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 1297173004Sjulian p->p_pid, td->td_name); 129872237Sjhb free(ithd, M_ITHREAD); 1299173044Sjulian kthread_exit(); 130072237Sjhb } 130172237Sjhb 1302151658Sjhb /* 1303151658Sjhb * Service interrupts. If another interrupt arrives while 1304151658Sjhb * we are running, it will set it_need to note that we 1305151658Sjhb * should make another pass. 1306151658Sjhb */ 130772237Sjhb while (ithd->it_need) { 130867551Sjhb /* 1309151658Sjhb * This might need a full read and write barrier 1310151658Sjhb * to make sure that this write posts before any 1311151658Sjhb * of the memory or device accesses in the 1312151658Sjhb * handlers. 131367551Sjhb */ 131472237Sjhb atomic_store_rel_int(&ithd->it_need, 0); 1315151658Sjhb ithread_execute_handlers(p, ie); 131666698Sjhb } 1317128331Sjhb WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 1318128331Sjhb mtx_assert(&Giant, MA_NOTOWNED); 131967551Sjhb 132066698Sjhb /* 132166698Sjhb * Processed all our interrupts. Now get the sched 132267551Sjhb * lock. This may take a while and it_need may get 132366698Sjhb * set again, so we have to check it again. 132466698Sjhb */ 1325170307Sjeff thread_lock(td); 1326151658Sjhb if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 1327128331Sjhb TD_SET_IWAIT(td); 1328151658Sjhb ie->ie_count = 0; 1329178272Sjeff mi_switch(SW_VOL | SWT_IWAIT, NULL); 133066698Sjhb } 1331170307Sjeff thread_unlock(td); 133266698Sjhb } 133366698Sjhb} 1334177940Sjhb 1335177940Sjhb/* 1336177940Sjhb * Main interrupt handling body. 1337177940Sjhb * 1338177940Sjhb * Input: 1339177940Sjhb * o ie: the event connected to this interrupt. 1340177940Sjhb * o frame: some archs (i.e. i386) pass a frame to some. 1341177940Sjhb * handlers as their main argument. 1342177940Sjhb * Return value: 1343177940Sjhb * o 0: everything ok. 1344177940Sjhb * o EINVAL: stray interrupt. 1345177940Sjhb */ 1346177940Sjhbint 1347177940Sjhbintr_event_handle(struct intr_event *ie, struct trapframe *frame) 1348177940Sjhb{ 1349177940Sjhb struct intr_handler *ih; 1350177940Sjhb struct thread *td; 1351177940Sjhb int error, ret, thread; 1352177940Sjhb 1353177940Sjhb td = curthread; 1354177940Sjhb 1355177940Sjhb /* An interrupt with no event or handlers is a stray interrupt. */ 1356177940Sjhb if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 1357177940Sjhb return (EINVAL); 1358177940Sjhb 1359177940Sjhb /* 1360177940Sjhb * Execute fast interrupt handlers directly. 1361177940Sjhb * To support clock handlers, if a handler registers 1362177940Sjhb * with a NULL argument, then we pass it a pointer to 1363177940Sjhb * a trapframe as its argument. 1364177940Sjhb */ 1365177940Sjhb td->td_intr_nesting_level++; 1366177940Sjhb thread = 0; 1367177940Sjhb ret = 0; 1368177940Sjhb critical_enter(); 1369177940Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1370177940Sjhb if (ih->ih_filter == NULL) { 1371177940Sjhb thread = 1; 1372177940Sjhb continue; 1373177940Sjhb } 1374177940Sjhb CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 1375177940Sjhb ih->ih_filter, ih->ih_argument == NULL ? frame : 1376177940Sjhb ih->ih_argument, ih->ih_name); 1377177940Sjhb if (ih->ih_argument == NULL) 1378177940Sjhb ret = ih->ih_filter(frame); 1379177940Sjhb else 1380177940Sjhb ret = ih->ih_filter(ih->ih_argument); 1381177940Sjhb /* 1382177940Sjhb * Wrapper handler special handling: 1383177940Sjhb * 1384177940Sjhb * in some particular cases (like pccard and pccbb), 1385177940Sjhb * the _real_ device handler is wrapped in a couple of 1386177940Sjhb * functions - a filter wrapper and an ithread wrapper. 1387177940Sjhb * In this case (and just in this case), the filter wrapper 1388177940Sjhb * could ask the system to schedule the ithread and mask 1389177940Sjhb * the interrupt source if the wrapped handler is composed 1390177940Sjhb * of just an ithread handler. 1391177940Sjhb * 1392177940Sjhb * TODO: write a generic wrapper to avoid people rolling 1393177940Sjhb * their own 1394177940Sjhb */ 1395177940Sjhb if (!thread) { 1396177940Sjhb if (ret == FILTER_SCHEDULE_THREAD) 1397177940Sjhb thread = 1; 1398177940Sjhb } 1399177940Sjhb } 1400177940Sjhb 1401177940Sjhb if (thread) { 1402177940Sjhb if (ie->ie_pre_ithread != NULL) 1403177940Sjhb ie->ie_pre_ithread(ie->ie_source); 1404177940Sjhb } else { 1405177940Sjhb if (ie->ie_post_filter != NULL) 1406177940Sjhb ie->ie_post_filter(ie->ie_source); 1407177940Sjhb } 1408177940Sjhb 1409177940Sjhb /* Schedule the ithread if needed. */ 1410177940Sjhb if (thread) { 1411177940Sjhb error = intr_event_schedule_thread(ie); 1412182024Skmacy#ifndef XEN 1413177940Sjhb KASSERT(error == 0, ("bad stray interrupt")); 1414182024Skmacy#else 1415182024Skmacy if (error != 0) 1416182024Skmacy log(LOG_WARNING, "bad stray interrupt"); 1417182024Skmacy#endif 1418177940Sjhb } 1419177940Sjhb critical_exit(); 1420177940Sjhb td->td_intr_nesting_level--; 1421177940Sjhb return (0); 1422177940Sjhb} 1423169320Spiso#else 1424169320Spiso/* 1425169320Spiso * This is the main code for interrupt threads. 1426169320Spiso */ 1427169320Spisostatic void 1428169320Spisoithread_loop(void *arg) 1429169320Spiso{ 1430169320Spiso struct intr_thread *ithd; 1431169320Spiso struct intr_handler *ih; 1432169320Spiso struct intr_event *ie; 1433169320Spiso struct thread *td; 1434169320Spiso struct proc *p; 1435169320Spiso int priv; 143666698Sjhb 1437169320Spiso td = curthread; 1438169320Spiso p = td->td_proc; 1439169320Spiso ih = (struct intr_handler *)arg; 1440169320Spiso priv = (ih->ih_thread != NULL) ? 1 : 0; 1441169320Spiso ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread; 1442169320Spiso KASSERT(ithd->it_thread == td, 1443169320Spiso ("%s: ithread and proc linkage out of sync", __func__)); 1444169320Spiso ie = ithd->it_event; 1445169320Spiso ie->ie_count = 0; 1446169320Spiso 1447169320Spiso /* 1448169320Spiso * As long as we have interrupts outstanding, go through the 1449169320Spiso * list of handlers, giving each one a go at it. 1450169320Spiso */ 1451169320Spiso for (;;) { 1452169320Spiso /* 1453169320Spiso * If we are an orphaned thread, then just die. 1454169320Spiso */ 1455169320Spiso if (ithd->it_flags & IT_DEAD) { 1456169320Spiso CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 1457173004Sjulian p->p_pid, td->td_name); 1458169320Spiso free(ithd, M_ITHREAD); 1459173044Sjulian kthread_exit(); 1460169320Spiso } 1461169320Spiso 1462169320Spiso /* 1463169320Spiso * Service interrupts. If another interrupt arrives while 1464169320Spiso * we are running, it will set it_need to note that we 1465169320Spiso * should make another pass. 1466169320Spiso */ 1467169320Spiso while (ithd->it_need) { 1468169320Spiso /* 1469169320Spiso * This might need a full read and write barrier 1470169320Spiso * to make sure that this write posts before any 1471169320Spiso * of the memory or device accesses in the 1472169320Spiso * handlers. 1473169320Spiso */ 1474169320Spiso atomic_store_rel_int(&ithd->it_need, 0); 1475169320Spiso if (priv) 1476169320Spiso priv_ithread_execute_handler(p, ih); 1477169320Spiso else 1478169320Spiso ithread_execute_handlers(p, ie); 1479169320Spiso } 1480169320Spiso WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 1481169320Spiso mtx_assert(&Giant, MA_NOTOWNED); 1482169320Spiso 1483169320Spiso /* 1484169320Spiso * Processed all our interrupts. Now get the sched 1485169320Spiso * lock. This may take a while and it_need may get 1486169320Spiso * set again, so we have to check it again. 1487169320Spiso */ 1488170307Sjeff thread_lock(td); 1489169320Spiso if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 1490169320Spiso TD_SET_IWAIT(td); 1491169320Spiso ie->ie_count = 0; 1492178272Sjeff mi_switch(SW_VOL | SWT_IWAIT, NULL); 1493169320Spiso } 1494170307Sjeff thread_unlock(td); 1495169320Spiso } 1496169320Spiso} 1497169320Spiso 1498169320Spiso/* 1499169320Spiso * Main loop for interrupt filter. 1500169320Spiso * 1501169320Spiso * Some architectures (i386, amd64 and arm) require the optional frame 1502169320Spiso * parameter, and use it as the main argument for fast handler execution 1503169320Spiso * when ih_argument == NULL. 1504169320Spiso * 1505169320Spiso * Return value: 1506169320Spiso * o FILTER_STRAY: No filter recognized the event, and no 1507169320Spiso * filter-less handler is registered on this 1508169320Spiso * line. 1509169320Spiso * o FILTER_HANDLED: A filter claimed the event and served it. 1510169320Spiso * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at 1511169320Spiso * least one filter-less handler on this line. 1512169320Spiso * o FILTER_HANDLED | 1513169320Spiso * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for 1514169320Spiso * scheduling the per-handler ithread. 1515169320Spiso * 1516169320Spiso * In case an ithread has to be scheduled, in *ithd there will be a 1517169320Spiso * pointer to a struct intr_thread containing the thread to be 1518169320Spiso * scheduled. 1519169320Spiso */ 1520169320Spiso 1521177940Sjhbstatic int 1522169320Spisointr_filter_loop(struct intr_event *ie, struct trapframe *frame, 1523169320Spiso struct intr_thread **ithd) 1524169320Spiso{ 1525169320Spiso struct intr_handler *ih; 1526169320Spiso void *arg; 1527169320Spiso int ret, thread_only; 1528169320Spiso 1529169320Spiso ret = 0; 1530169320Spiso thread_only = 0; 1531169320Spiso TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1532169320Spiso /* 1533169320Spiso * Execute fast interrupt handlers directly. 1534169320Spiso * To support clock handlers, if a handler registers 1535169320Spiso * with a NULL argument, then we pass it a pointer to 1536169320Spiso * a trapframe as its argument. 1537169320Spiso */ 1538169320Spiso arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument); 1539169320Spiso 1540169320Spiso CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__, 1541169320Spiso ih->ih_filter, ih->ih_handler, arg, ih->ih_name); 1542169320Spiso 1543169320Spiso if (ih->ih_filter != NULL) 1544169320Spiso ret = ih->ih_filter(arg); 1545169320Spiso else { 1546169320Spiso thread_only = 1; 1547169320Spiso continue; 1548169320Spiso } 1549169320Spiso 1550169320Spiso if (ret & FILTER_STRAY) 1551169320Spiso continue; 1552169320Spiso else { 1553169320Spiso *ithd = ih->ih_thread; 1554169320Spiso return (ret); 1555169320Spiso } 1556169320Spiso } 1557169320Spiso 1558169320Spiso /* 1559169320Spiso * No filters handled the interrupt and we have at least 1560169320Spiso * one handler without a filter. In this case, we schedule 1561169320Spiso * all of the filter-less handlers to run in the ithread. 1562169320Spiso */ 1563169320Spiso if (thread_only) { 1564169320Spiso *ithd = ie->ie_thread; 1565169320Spiso return (FILTER_SCHEDULE_THREAD); 1566169320Spiso } 1567169320Spiso return (FILTER_STRAY); 1568169320Spiso} 1569169320Spiso 1570169320Spiso/* 1571169320Spiso * Main interrupt handling body. 1572169320Spiso * 1573169320Spiso * Input: 1574169320Spiso * o ie: the event connected to this interrupt. 1575169320Spiso * o frame: some archs (i.e. i386) pass a frame to some. 1576169320Spiso * handlers as their main argument. 1577169320Spiso * Return value: 1578169320Spiso * o 0: everything ok. 1579169320Spiso * o EINVAL: stray interrupt. 1580169320Spiso */ 1581169320Spisoint 1582169320Spisointr_event_handle(struct intr_event *ie, struct trapframe *frame) 1583169320Spiso{ 1584169320Spiso struct intr_thread *ithd; 1585169320Spiso struct thread *td; 1586169320Spiso int thread; 1587169320Spiso 1588169320Spiso ithd = NULL; 1589169320Spiso td = curthread; 1590169320Spiso 1591169320Spiso if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 1592169320Spiso return (EINVAL); 1593169320Spiso 1594169320Spiso td->td_intr_nesting_level++; 1595169320Spiso thread = 0; 1596169320Spiso critical_enter(); 1597177940Sjhb thread = intr_filter_loop(ie, frame, &ithd); 1598169320Spiso if (thread & FILTER_HANDLED) { 1599177940Sjhb if (ie->ie_post_filter != NULL) 1600177940Sjhb ie->ie_post_filter(ie->ie_source); 1601169320Spiso } else { 1602177940Sjhb if (ie->ie_pre_ithread != NULL) 1603177940Sjhb ie->ie_pre_ithread(ie->ie_source); 1604169320Spiso } 1605169320Spiso critical_exit(); 1606169320Spiso 1607169320Spiso /* Interrupt storm logic */ 1608169320Spiso if (thread & FILTER_STRAY) { 1609169320Spiso ie->ie_count++; 1610169320Spiso if (ie->ie_count < intr_storm_threshold) 1611169320Spiso printf("Interrupt stray detection not present\n"); 1612169320Spiso } 1613169320Spiso 1614169320Spiso /* Schedule an ithread if needed. */ 1615169320Spiso if (thread & FILTER_SCHEDULE_THREAD) { 1616169320Spiso if (intr_event_schedule_thread(ie, ithd) != 0) 1617169320Spiso panic("%s: impossible stray interrupt", __func__); 1618169320Spiso } 1619169320Spiso td->td_intr_nesting_level--; 1620169320Spiso return (0); 1621169320Spiso} 1622169320Spiso#endif 1623169320Spiso 1624121482Sjhb#ifdef DDB 162572237Sjhb/* 1626121482Sjhb * Dump details about an interrupt handler 1627121482Sjhb */ 1628121482Sjhbstatic void 1629151658Sjhbdb_dump_intrhand(struct intr_handler *ih) 1630121482Sjhb{ 1631121482Sjhb int comma; 1632121482Sjhb 1633121482Sjhb db_printf("\t%-10s ", ih->ih_name); 1634121482Sjhb switch (ih->ih_pri) { 1635121482Sjhb case PI_REALTIME: 1636121482Sjhb db_printf("CLK "); 1637121482Sjhb break; 1638121482Sjhb case PI_AV: 1639121482Sjhb db_printf("AV "); 1640121482Sjhb break; 1641121482Sjhb case PI_TTYHIGH: 1642121482Sjhb case PI_TTYLOW: 1643121482Sjhb db_printf("TTY "); 1644121482Sjhb break; 1645121482Sjhb case PI_TAPE: 1646121482Sjhb db_printf("TAPE"); 1647121482Sjhb break; 1648121482Sjhb case PI_NET: 1649121482Sjhb db_printf("NET "); 1650121482Sjhb break; 1651121482Sjhb case PI_DISK: 1652121482Sjhb case PI_DISKLOW: 1653121482Sjhb db_printf("DISK"); 1654121482Sjhb break; 1655121482Sjhb case PI_DULL: 1656121482Sjhb db_printf("DULL"); 1657121482Sjhb break; 1658121482Sjhb default: 1659121482Sjhb if (ih->ih_pri >= PI_SOFT) 1660121482Sjhb db_printf("SWI "); 1661121482Sjhb else 1662121482Sjhb db_printf("%4u", ih->ih_pri); 1663121482Sjhb break; 1664121482Sjhb } 1665121482Sjhb db_printf(" "); 1666121482Sjhb db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1667121482Sjhb db_printf("(%p)", ih->ih_argument); 1668121482Sjhb if (ih->ih_need || 1669166901Spiso (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 1670121482Sjhb IH_MPSAFE)) != 0) { 1671121482Sjhb db_printf(" {"); 1672121482Sjhb comma = 0; 1673121482Sjhb if (ih->ih_flags & IH_EXCLUSIVE) { 1674121482Sjhb if (comma) 1675121482Sjhb db_printf(", "); 1676121482Sjhb db_printf("EXCL"); 1677121482Sjhb comma = 1; 1678121482Sjhb } 1679121482Sjhb if (ih->ih_flags & IH_ENTROPY) { 1680121482Sjhb if (comma) 1681121482Sjhb db_printf(", "); 1682121482Sjhb db_printf("ENTROPY"); 1683121482Sjhb comma = 1; 1684121482Sjhb } 1685121482Sjhb if (ih->ih_flags & IH_DEAD) { 1686121482Sjhb if (comma) 1687121482Sjhb db_printf(", "); 1688121482Sjhb db_printf("DEAD"); 1689121482Sjhb comma = 1; 1690121482Sjhb } 1691121482Sjhb if (ih->ih_flags & IH_MPSAFE) { 1692121482Sjhb if (comma) 1693121482Sjhb db_printf(", "); 1694121482Sjhb db_printf("MPSAFE"); 1695121482Sjhb comma = 1; 1696121482Sjhb } 1697121482Sjhb if (ih->ih_need) { 1698121482Sjhb if (comma) 1699121482Sjhb db_printf(", "); 1700121482Sjhb db_printf("NEED"); 1701121482Sjhb } 1702121482Sjhb db_printf("}"); 1703121482Sjhb } 1704121482Sjhb db_printf("\n"); 1705121482Sjhb} 1706121482Sjhb 1707121482Sjhb/* 1708151658Sjhb * Dump details about a event. 1709121482Sjhb */ 1710121482Sjhbvoid 1711151658Sjhbdb_dump_intr_event(struct intr_event *ie, int handlers) 1712121482Sjhb{ 1713151658Sjhb struct intr_handler *ih; 1714151658Sjhb struct intr_thread *it; 1715121482Sjhb int comma; 1716121482Sjhb 1717151658Sjhb db_printf("%s ", ie->ie_fullname); 1718151658Sjhb it = ie->ie_thread; 1719151658Sjhb if (it != NULL) 1720151658Sjhb db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1721151658Sjhb else 1722151658Sjhb db_printf("(no thread)"); 1723151658Sjhb if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1724151658Sjhb (it != NULL && it->it_need)) { 1725121482Sjhb db_printf(" {"); 1726121482Sjhb comma = 0; 1727151658Sjhb if (ie->ie_flags & IE_SOFT) { 1728121482Sjhb db_printf("SOFT"); 1729121482Sjhb comma = 1; 1730121482Sjhb } 1731151658Sjhb if (ie->ie_flags & IE_ENTROPY) { 1732121482Sjhb if (comma) 1733121482Sjhb db_printf(", "); 1734121482Sjhb db_printf("ENTROPY"); 1735121482Sjhb comma = 1; 1736121482Sjhb } 1737151658Sjhb if (ie->ie_flags & IE_ADDING_THREAD) { 1738121482Sjhb if (comma) 1739121482Sjhb db_printf(", "); 1740151658Sjhb db_printf("ADDING_THREAD"); 1741121482Sjhb comma = 1; 1742121482Sjhb } 1743151658Sjhb if (it != NULL && it->it_need) { 1744121482Sjhb if (comma) 1745121482Sjhb db_printf(", "); 1746121482Sjhb db_printf("NEED"); 1747121482Sjhb } 1748121482Sjhb db_printf("}"); 1749121482Sjhb } 1750121482Sjhb db_printf("\n"); 1751121482Sjhb 1752121482Sjhb if (handlers) 1753151658Sjhb TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 1754121482Sjhb db_dump_intrhand(ih); 1755121482Sjhb} 1756151658Sjhb 1757151658Sjhb/* 1758151658Sjhb * Dump data about interrupt handlers 1759151658Sjhb */ 1760151658SjhbDB_SHOW_COMMAND(intr, db_show_intr) 1761151658Sjhb{ 1762151658Sjhb struct intr_event *ie; 1763160312Sjhb int all, verbose; 1764151658Sjhb 1765151658Sjhb verbose = index(modif, 'v') != NULL; 1766151658Sjhb all = index(modif, 'a') != NULL; 1767151658Sjhb TAILQ_FOREACH(ie, &event_list, ie_list) { 1768151658Sjhb if (!all && TAILQ_EMPTY(&ie->ie_handlers)) 1769151658Sjhb continue; 1770151658Sjhb db_dump_intr_event(ie, verbose); 1771160312Sjhb if (db_pager_quit) 1772160312Sjhb break; 1773151658Sjhb } 1774151658Sjhb} 1775121482Sjhb#endif /* DDB */ 1776121482Sjhb 1777121482Sjhb/* 177867551Sjhb * Start standard software interrupt threads 177966698Sjhb */ 178067551Sjhbstatic void 178172237Sjhbstart_softintr(void *dummy) 178267551Sjhb{ 178372237Sjhb 1784177859Sjeff if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 1785177859Sjeff panic("died while creating vm swi ithread"); 178666698Sjhb} 1787177253SrwatsonSYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1788177253Srwatson NULL); 178966698Sjhb 1790151658Sjhb/* 179177582Stmm * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 179277582Stmm * The data for this machine dependent, and the declarations are in machine 179377582Stmm * dependent code. The layout of intrnames and intrcnt however is machine 179477582Stmm * independent. 179577582Stmm * 179677582Stmm * We do not know the length of intrcnt and intrnames at compile time, so 179777582Stmm * calculate things at run time. 179877582Stmm */ 179977582Stmmstatic int 180077582Stmmsysctl_intrnames(SYSCTL_HANDLER_ARGS) 180177582Stmm{ 1802151658Sjhb return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 180377582Stmm req)); 180477582Stmm} 180577582Stmm 180677582StmmSYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 180777582Stmm NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 180877582Stmm 180977582Stmmstatic int 181077582Stmmsysctl_intrcnt(SYSCTL_HANDLER_ARGS) 181177582Stmm{ 1812151658Sjhb return (sysctl_handle_opaque(oidp, intrcnt, 181377582Stmm (char *)eintrcnt - (char *)intrcnt, req)); 181477582Stmm} 181577582Stmm 181677582StmmSYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 181777582Stmm NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 1818121482Sjhb 1819121482Sjhb#ifdef DDB 1820121482Sjhb/* 1821121482Sjhb * DDB command to dump the interrupt statistics. 1822121482Sjhb */ 1823121482SjhbDB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 1824121482Sjhb{ 1825121482Sjhb u_long *i; 1826121482Sjhb char *cp; 1827121482Sjhb 1828121482Sjhb cp = intrnames; 1829160312Sjhb for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) { 1830121482Sjhb if (*cp == '\0') 1831121482Sjhb break; 1832121482Sjhb if (*i != 0) 1833121482Sjhb db_printf("%s\t%lu\n", cp, *i); 1834121482Sjhb cp += strlen(cp) + 1; 1835121482Sjhb } 1836121482Sjhb} 1837121482Sjhb#endif 1838