19454b2d8SWarner Losh /*- 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 */ 26425f9fdaSStefan Eßer 27677b542eSDavid E. O'Brien #include <sys/cdefs.h> 28677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 293900ddb2SDoug Rabson 308b201c42SJohn Baldwin #include "opt_ddb.h" 31*b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h" 328b201c42SJohn Baldwin 331c5bb3eaSPeter Wemm #include <sys/param.h> 349a94c9c5SJohn Baldwin #include <sys/bus.h> 35c11110eaSAlfred Perlstein #include <sys/conf.h> 369b33b154SJeff Roberson #include <sys/cpuset.h> 379a94c9c5SJohn Baldwin #include <sys/rtprio.h> 38425f9fdaSStefan Eßer #include <sys/systm.h> 3968352337SDoug Rabson #include <sys/interrupt.h> 401931cf94SJohn Baldwin #include <sys/kernel.h> 411931cf94SJohn Baldwin #include <sys/kthread.h> 421931cf94SJohn Baldwin #include <sys/ktr.h> 4305b2c96fSBruce Evans #include <sys/limits.h> 44f34fa851SJohn Baldwin #include <sys/lock.h> 451931cf94SJohn Baldwin #include <sys/malloc.h> 4635e0e5b3SJohn Baldwin #include <sys/mutex.h> 47cebc7fb1SJohn Baldwin #include <sys/priv.h> 481931cf94SJohn Baldwin #include <sys/proc.h> 493e5da754SJohn Baldwin #include <sys/random.h> 50b4151f71SJohn Baldwin #include <sys/resourcevar.h> 5163710c4dSJohn Baldwin #include <sys/sched.h> 52eaf86d16SJohn Baldwin #include <sys/smp.h> 53d279178dSThomas Moestl #include <sys/sysctl.h> 546205924aSKip Macy #include <sys/syslog.h> 551931cf94SJohn Baldwin #include <sys/unistd.h> 561931cf94SJohn Baldwin #include <sys/vmmeter.h> 571931cf94SJohn Baldwin #include <machine/atomic.h> 581931cf94SJohn Baldwin #include <machine/cpu.h> 598088699fSJohn Baldwin #include <machine/md_var.h> 60b4151f71SJohn Baldwin #include <machine/stdarg.h> 618b201c42SJohn Baldwin #ifdef DDB 628b201c42SJohn Baldwin #include <ddb/ddb.h> 638b201c42SJohn Baldwin #include <ddb/db_sym.h> 648b201c42SJohn Baldwin #endif 65425f9fdaSStefan Eßer 66e0f66ef8SJohn Baldwin /* 67e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 68e0f66ef8SJohn Baldwin */ 69e0f66ef8SJohn Baldwin struct intr_thread { 70e0f66ef8SJohn Baldwin struct intr_event *it_event; 71e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 72e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 73e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 743e5da754SJohn Baldwin }; 753e5da754SJohn Baldwin 76e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 77e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 78e4cd31ddSJeff Roberson #define IT_WAIT 0x000002 /* Thread is waiting for completion. */ 79e0f66ef8SJohn Baldwin 80e0f66ef8SJohn Baldwin struct intr_entropy { 81e0f66ef8SJohn Baldwin struct thread *td; 82e0f66ef8SJohn Baldwin uintptr_t event; 83e0f66ef8SJohn Baldwin }; 84e0f66ef8SJohn Baldwin 85e0f66ef8SJohn Baldwin struct intr_event *clk_intr_event; 86e0f66ef8SJohn Baldwin struct intr_event *tty_intr_event; 877b1fe905SBruce Evans void *vm_ih; 887ab24ea3SJulian Elischer struct proc *intrproc; 891931cf94SJohn Baldwin 90b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 91b4151f71SJohn Baldwin 920ae62c18SNate Lawson static int intr_storm_threshold = 1000; 93af3b2549SHans Petter Selasky SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN, 947870c3c6SJohn Baldwin &intr_storm_threshold, 0, 957b1fe905SBruce Evans "Number of consecutive interrupts before storm protection is enabled"); 96e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 97e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 989b33b154SJeff Roberson static struct mtx event_lock; 999b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 1007b1fe905SBruce Evans 101e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 102bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1031ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie, 1041ee1b687SJohn Baldwin struct intr_thread *ithd); 1051ee1b687SJohn Baldwin static int intr_filter_loop(struct intr_event *ie, 1061ee1b687SJohn Baldwin struct trapframe *frame, struct intr_thread **ithd); 107bafe5a31SPaolo Pisati static struct intr_thread *ithread_create(const char *name, 108bafe5a31SPaolo Pisati struct intr_handler *ih); 109bafe5a31SPaolo Pisati #else 1101ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie); 111e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 112bafe5a31SPaolo Pisati #endif 113e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 114bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 115bafe5a31SPaolo Pisati struct intr_event *ie); 116bafe5a31SPaolo Pisati #ifdef INTR_FILTER 117bafe5a31SPaolo Pisati static void priv_ithread_execute_handler(struct proc *p, 118bafe5a31SPaolo Pisati struct intr_handler *ih); 119bafe5a31SPaolo Pisati #endif 1207b1fe905SBruce Evans static void ithread_loop(void *); 121e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1227b1fe905SBruce Evans static void start_softintr(void *); 1237870c3c6SJohn Baldwin 124bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 125b4151f71SJohn Baldwin u_char 126e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1279a94c9c5SJohn Baldwin { 128b4151f71SJohn Baldwin u_char pri; 1299a94c9c5SJohn Baldwin 130b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1315a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1329a94c9c5SJohn Baldwin switch (flags) { 133b4151f71SJohn Baldwin case INTR_TYPE_TTY: 134d3305205SJohn Baldwin pri = PI_TTY; 1359a94c9c5SJohn Baldwin break; 1369a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1379a94c9c5SJohn Baldwin pri = PI_DISK; 1389a94c9c5SJohn Baldwin break; 1399a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1409a94c9c5SJohn Baldwin pri = PI_NET; 1419a94c9c5SJohn Baldwin break; 1429a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 143d3305205SJohn Baldwin pri = PI_DISK; 1449a94c9c5SJohn Baldwin break; 145d3305205SJohn Baldwin case INTR_TYPE_AV: 1465a280d9cSPeter Wemm pri = PI_AV; 1475a280d9cSPeter Wemm break; 148b4151f71SJohn Baldwin case INTR_TYPE_CLK: 149b4151f71SJohn Baldwin pri = PI_REALTIME; 150b4151f71SJohn Baldwin break; 1519a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1529a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1539a94c9c5SJohn Baldwin break; 1549a94c9c5SJohn Baldwin default: 155b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 156e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1579a94c9c5SJohn Baldwin } 1589a94c9c5SJohn Baldwin 1599a94c9c5SJohn Baldwin return pri; 1609a94c9c5SJohn Baldwin } 1619a94c9c5SJohn Baldwin 162b4151f71SJohn Baldwin /* 163e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 164b4151f71SJohn Baldwin */ 165b4151f71SJohn Baldwin static void 166e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 167b4151f71SJohn Baldwin { 168e0f66ef8SJohn Baldwin struct intr_event *ie; 169b40ce416SJulian Elischer struct thread *td; 170e0f66ef8SJohn Baldwin u_char pri; 1718088699fSJohn Baldwin 172e0f66ef8SJohn Baldwin ie = ithd->it_event; 173e0f66ef8SJohn Baldwin td = ithd->it_thread; 174b4151f71SJohn Baldwin 175e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 176e0f66ef8SJohn Baldwin if (TAILQ_EMPTY(&ie->ie_handlers)) 177e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 178e0f66ef8SJohn Baldwin else 179e0f66ef8SJohn Baldwin pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri; 180e80fb434SRobert Drehmel 181e0f66ef8SJohn Baldwin /* Update name and priority. */ 1827ab24ea3SJulian Elischer strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 18344ad5475SJohn Baldwin #ifdef KTR 18444ad5475SJohn Baldwin sched_clear_tdname(td); 18544ad5475SJohn Baldwin #endif 186982d11f8SJeff Roberson thread_lock(td); 187e0f66ef8SJohn Baldwin sched_prio(td, pri); 188982d11f8SJeff Roberson thread_unlock(td); 189b4151f71SJohn Baldwin } 190e0f66ef8SJohn Baldwin 191e0f66ef8SJohn Baldwin /* 192e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 193e0f66ef8SJohn Baldwin */ 194e0f66ef8SJohn Baldwin static void 195e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 196e0f66ef8SJohn Baldwin { 197e0f66ef8SJohn Baldwin struct intr_handler *ih; 198e0f66ef8SJohn Baldwin char *last; 199e0f66ef8SJohn Baldwin int missed, space; 200e0f66ef8SJohn Baldwin 201e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 202e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 203e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 204e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ENTROPY; 2050811d60aSJohn Baldwin missed = 0; 206e0f66ef8SJohn Baldwin space = 1; 207e0f66ef8SJohn Baldwin 208e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 209e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 210e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 211e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 212e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 213e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 214e0f66ef8SJohn Baldwin space = 0; 2150811d60aSJohn Baldwin } else 2160811d60aSJohn Baldwin missed++; 2170811d60aSJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 218e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ENTROPY; 2190811d60aSJohn Baldwin } 220e0f66ef8SJohn Baldwin 221e0f66ef8SJohn Baldwin /* 222e0f66ef8SJohn Baldwin * If the handler names were too long, add +'s to indicate missing 223e0f66ef8SJohn Baldwin * names. If we run out of room and still have +'s to add, change 224e0f66ef8SJohn Baldwin * the last character from a + to a *. 225e0f66ef8SJohn Baldwin */ 226e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2270811d60aSJohn Baldwin while (missed-- > 0) { 228e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 229e0f66ef8SJohn Baldwin if (*last == '+') { 230e0f66ef8SJohn Baldwin *last = '*'; 231e0f66ef8SJohn Baldwin break; 232b4151f71SJohn Baldwin } else 233e0f66ef8SJohn Baldwin *last = '+'; 234e0f66ef8SJohn Baldwin } else if (space) { 235e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 236e0f66ef8SJohn Baldwin space = 0; 237e0f66ef8SJohn Baldwin } else 238e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 239b4151f71SJohn Baldwin } 240e0f66ef8SJohn Baldwin 241e0f66ef8SJohn Baldwin /* 242e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 243e0f66ef8SJohn Baldwin * name. 244e0f66ef8SJohn Baldwin */ 245e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 246e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 247e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 248b4151f71SJohn Baldwin } 249b4151f71SJohn Baldwin 250b4151f71SJohn Baldwin int 2519b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2521ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 253066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2541ee1b687SJohn Baldwin const char *fmt, ...) 255bafe5a31SPaolo Pisati { 256bafe5a31SPaolo Pisati struct intr_event *ie; 257bafe5a31SPaolo Pisati va_list ap; 258bafe5a31SPaolo Pisati 259bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 260bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 261bafe5a31SPaolo Pisati return (EINVAL); 262bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 263bafe5a31SPaolo Pisati ie->ie_source = source; 2641ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2651ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2661ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2676d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 268bafe5a31SPaolo Pisati ie->ie_flags = flags; 2699b33b154SJeff Roberson ie->ie_irq = irq; 270eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 271bafe5a31SPaolo Pisati TAILQ_INIT(&ie->ie_handlers); 272bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 273bafe5a31SPaolo Pisati 274bafe5a31SPaolo Pisati va_start(ap, fmt); 275bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 276bafe5a31SPaolo Pisati va_end(ap); 277bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2789b33b154SJeff Roberson mtx_lock(&event_lock); 279bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2809b33b154SJeff Roberson mtx_unlock(&event_lock); 281bafe5a31SPaolo Pisati if (event != NULL) 282bafe5a31SPaolo Pisati *event = ie; 283bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 284bafe5a31SPaolo Pisati return (0); 285bafe5a31SPaolo Pisati } 286b4151f71SJohn Baldwin 287eaf86d16SJohn Baldwin /* 288eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 289eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 290eaf86d16SJohn Baldwin * platforms this request will fail. For supported platforms, any 291eaf86d16SJohn Baldwin * associated ithreads as well as the primary interrupt context will 292eaf86d16SJohn Baldwin * be bound to the specificed CPU. Using a cpu id of NOCPU unbinds 293eaf86d16SJohn Baldwin * the interrupt event. 294eaf86d16SJohn Baldwin */ 295eaf86d16SJohn Baldwin int 296066da805SAdrian Chadd intr_event_bind(struct intr_event *ie, int cpu) 297eaf86d16SJohn Baldwin { 2989b33b154SJeff Roberson lwpid_t id; 299eaf86d16SJohn Baldwin int error; 300eaf86d16SJohn Baldwin 301eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 302eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 303eaf86d16SJohn Baldwin return (EINVAL); 304eaf86d16SJohn Baldwin 305eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 306eaf86d16SJohn Baldwin return (EOPNOTSUPP); 307cebc7fb1SJohn Baldwin 308cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 309cebc7fb1SJohn Baldwin if (error) 310cebc7fb1SJohn Baldwin return (error); 311cebc7fb1SJohn Baldwin 3129b33b154SJeff Roberson /* 313cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 314cebc7fb1SJohn Baldwin * permissions, etc. 3159b33b154SJeff Roberson */ 316eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3179b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3189b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 319eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32081198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3219b33b154SJeff Roberson if (error) 3229b33b154SJeff Roberson return (error); 3239b33b154SJeff Roberson } else 324eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 325eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 326cebc7fb1SJohn Baldwin if (error) { 327cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 328cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 32981198539SAlexander V. Chernikov cpu = ie->ie_cpu; 330cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 331cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33281198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 333cebc7fb1SJohn Baldwin } else 334cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 335eaf86d16SJohn Baldwin return (error); 336cebc7fb1SJohn Baldwin } 337cebc7fb1SJohn Baldwin 338eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 339eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3409b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 3419b33b154SJeff Roberson 3429b33b154SJeff Roberson return (error); 3439b33b154SJeff Roberson } 3449b33b154SJeff Roberson 3459b33b154SJeff Roberson static struct intr_event * 3469b33b154SJeff Roberson intr_lookup(int irq) 3479b33b154SJeff Roberson { 3489b33b154SJeff Roberson struct intr_event *ie; 3499b33b154SJeff Roberson 3509b33b154SJeff Roberson mtx_lock(&event_lock); 3519b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 3529b33b154SJeff Roberson if (ie->ie_irq == irq && 3539b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 3549b33b154SJeff Roberson TAILQ_FIRST(&ie->ie_handlers) != NULL) 3559b33b154SJeff Roberson break; 3569b33b154SJeff Roberson mtx_unlock(&event_lock); 3579b33b154SJeff Roberson return (ie); 3589b33b154SJeff Roberson } 3599b33b154SJeff Roberson 3609b33b154SJeff Roberson int 3619b33b154SJeff Roberson intr_setaffinity(int irq, void *m) 3629b33b154SJeff Roberson { 3639b33b154SJeff Roberson struct intr_event *ie; 3649b33b154SJeff Roberson cpuset_t *mask; 3659b33b154SJeff Roberson u_char cpu; 3669b33b154SJeff Roberson int n; 3679b33b154SJeff Roberson 3689b33b154SJeff Roberson mask = m; 3699b33b154SJeff Roberson cpu = NOCPU; 3709b33b154SJeff Roberson /* 3719b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 3729b33b154SJeff Roberson * only one cpu is in the set. 3739b33b154SJeff Roberson */ 3749b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 3759b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 3769b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 3779b33b154SJeff Roberson continue; 3789b33b154SJeff Roberson if (cpu != NOCPU) 3799b33b154SJeff Roberson return (EINVAL); 3809b33b154SJeff Roberson cpu = (u_char)n; 3819b33b154SJeff Roberson } 3829b33b154SJeff Roberson } 3839b33b154SJeff Roberson ie = intr_lookup(irq); 3849b33b154SJeff Roberson if (ie == NULL) 3859b33b154SJeff Roberson return (ESRCH); 3869bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 3879b33b154SJeff Roberson } 3889b33b154SJeff Roberson 3899b33b154SJeff Roberson int 3909b33b154SJeff Roberson intr_getaffinity(int irq, void *m) 3919b33b154SJeff Roberson { 3929b33b154SJeff Roberson struct intr_event *ie; 3939b33b154SJeff Roberson cpuset_t *mask; 3949b33b154SJeff Roberson 3959b33b154SJeff Roberson mask = m; 3969b33b154SJeff Roberson ie = intr_lookup(irq); 3979b33b154SJeff Roberson if (ie == NULL) 3989b33b154SJeff Roberson return (ESRCH); 3999b33b154SJeff Roberson CPU_ZERO(mask); 4009b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4019b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4029b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4039b33b154SJeff Roberson else 4049b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 405eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 406eaf86d16SJohn Baldwin return (0); 407eaf86d16SJohn Baldwin } 408eaf86d16SJohn Baldwin 409b4151f71SJohn Baldwin int 410e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 411b4151f71SJohn Baldwin { 412b4151f71SJohn Baldwin 4139b33b154SJeff Roberson mtx_lock(&event_lock); 414e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 415e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 416e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4179b33b154SJeff Roberson mtx_unlock(&event_lock); 418e0f66ef8SJohn Baldwin return (EBUSY); 4194d29cb2dSJohn Baldwin } 420e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 4219477358dSJohn Baldwin #ifndef notyet 4229477358dSJohn Baldwin if (ie->ie_thread != NULL) { 4239477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 4249477358dSJohn Baldwin ie->ie_thread = NULL; 4259477358dSJohn Baldwin } 4269477358dSJohn Baldwin #endif 427e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4289b33b154SJeff Roberson mtx_unlock(&event_lock); 429e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 430e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 431e0f66ef8SJohn Baldwin return (0); 432e0f66ef8SJohn Baldwin } 433e0f66ef8SJohn Baldwin 434bafe5a31SPaolo Pisati #ifndef INTR_FILTER 435e0f66ef8SJohn Baldwin static struct intr_thread * 436e0f66ef8SJohn Baldwin ithread_create(const char *name) 437e0f66ef8SJohn Baldwin { 438e0f66ef8SJohn Baldwin struct intr_thread *ithd; 439e0f66ef8SJohn Baldwin struct thread *td; 440e0f66ef8SJohn Baldwin int error; 441e0f66ef8SJohn Baldwin 442e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 443e0f66ef8SJohn Baldwin 4447ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 4457ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 4469ef95d01SJulian Elischer 0, "intr", "%s", name); 447e0f66ef8SJohn Baldwin if (error) 4483745c395SJulian Elischer panic("kproc_create() failed with %d", error); 449982d11f8SJeff Roberson thread_lock(td); 450ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 451e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 452982d11f8SJeff Roberson thread_unlock(td); 453e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 454e0f66ef8SJohn Baldwin ithd->it_thread = td; 455e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 456e0f66ef8SJohn Baldwin return (ithd); 457e0f66ef8SJohn Baldwin } 458bafe5a31SPaolo Pisati #else 459bafe5a31SPaolo Pisati static struct intr_thread * 460bafe5a31SPaolo Pisati ithread_create(const char *name, struct intr_handler *ih) 461bafe5a31SPaolo Pisati { 462bafe5a31SPaolo Pisati struct intr_thread *ithd; 463bafe5a31SPaolo Pisati struct thread *td; 464bafe5a31SPaolo Pisati int error; 465bafe5a31SPaolo Pisati 466bafe5a31SPaolo Pisati ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 467bafe5a31SPaolo Pisati 468539976ffSJulian Elischer error = kproc_kthread_add(ithread_loop, ih, &intrproc, 4697ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 4709ef95d01SJulian Elischer 0, "intr", "%s", name); 471bafe5a31SPaolo Pisati if (error) 4723745c395SJulian Elischer panic("kproc_create() failed with %d", error); 473982d11f8SJeff Roberson thread_lock(td); 474bafe5a31SPaolo Pisati sched_class(td, PRI_ITHD); 475bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 476982d11f8SJeff Roberson thread_unlock(td); 477bafe5a31SPaolo Pisati td->td_pflags |= TDP_ITHREAD; 478bafe5a31SPaolo Pisati ithd->it_thread = td; 479bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, name); 480bafe5a31SPaolo Pisati return (ithd); 481bafe5a31SPaolo Pisati } 482bafe5a31SPaolo Pisati #endif 483e0f66ef8SJohn Baldwin 484e0f66ef8SJohn Baldwin static void 485e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 486e0f66ef8SJohn Baldwin { 487e0f66ef8SJohn Baldwin struct thread *td; 488e0f66ef8SJohn Baldwin 489bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 490e0f66ef8SJohn Baldwin td = ithread->it_thread; 491982d11f8SJeff Roberson thread_lock(td); 492e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 49371fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 49471fad9fdSJulian Elischer TD_CLR_IWAIT(td); 495f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 496b4151f71SJohn Baldwin } 497982d11f8SJeff Roberson thread_unlock(td); 498b4151f71SJohn Baldwin } 499b4151f71SJohn Baldwin 500bafe5a31SPaolo Pisati #ifndef INTR_FILTER 501b4151f71SJohn Baldwin int 502e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 503ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 504ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 505b4151f71SJohn Baldwin { 506e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 507e0f66ef8SJohn Baldwin struct intr_thread *it; 508b4151f71SJohn Baldwin 509ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 510b4151f71SJohn Baldwin return (EINVAL); 511b4151f71SJohn Baldwin 512e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 513e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 514ef544f63SPaolo Pisati ih->ih_filter = filter; 515b4151f71SJohn Baldwin ih->ih_handler = handler; 516b4151f71SJohn Baldwin ih->ih_argument = arg; 51737b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 518e0f66ef8SJohn Baldwin ih->ih_event = ie; 519b4151f71SJohn Baldwin ih->ih_pri = pri; 520ef544f63SPaolo Pisati if (flags & INTR_EXCL) 521b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 522b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 523b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 524b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 525b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 526b4151f71SJohn Baldwin 527e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 528e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 529e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 530e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 531e0f66ef8SJohn Baldwin (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 532e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 533b4151f71SJohn Baldwin free(ih, M_ITHREAD); 534b4151f71SJohn Baldwin return (EINVAL); 535b4151f71SJohn Baldwin } 536e0f66ef8SJohn Baldwin } 537e0f66ef8SJohn Baldwin 538e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 539ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 540e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 5410f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 542e0f66ef8SJohn Baldwin else { 543e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 544e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 545e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 546e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 547e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 548e0f66ef8SJohn Baldwin ie->ie_thread = it; 549e0f66ef8SJohn Baldwin it->it_event = ie; 550e0f66ef8SJohn Baldwin ithread_update(it); 551e0f66ef8SJohn Baldwin wakeup(ie); 552e0f66ef8SJohn Baldwin } 553e0f66ef8SJohn Baldwin } 554c9516c94SAlexander Kabaev 555c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 556c9516c94SAlexander Kabaev TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 557c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 558c9516c94SAlexander Kabaev break; 559c9516c94SAlexander Kabaev } 560c9516c94SAlexander Kabaev if (temp_ih == NULL) 561c9516c94SAlexander Kabaev TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 562c9516c94SAlexander Kabaev else 563c9516c94SAlexander Kabaev TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 564c9516c94SAlexander Kabaev intr_event_update(ie); 565c9516c94SAlexander Kabaev 566e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 567e0f66ef8SJohn Baldwin ie->ie_name); 568e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 569e0f66ef8SJohn Baldwin 570e0f66ef8SJohn Baldwin if (cookiep != NULL) 571e0f66ef8SJohn Baldwin *cookiep = ih; 572e0f66ef8SJohn Baldwin return (0); 573e0f66ef8SJohn Baldwin } 574bafe5a31SPaolo Pisati #else 575bafe5a31SPaolo Pisati int 576bafe5a31SPaolo Pisati intr_event_add_handler(struct intr_event *ie, const char *name, 577bafe5a31SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 578bafe5a31SPaolo Pisati enum intr_type flags, void **cookiep) 579bafe5a31SPaolo Pisati { 580bafe5a31SPaolo Pisati struct intr_handler *ih, *temp_ih; 581bafe5a31SPaolo Pisati struct intr_thread *it; 582bafe5a31SPaolo Pisati 583bafe5a31SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 584bafe5a31SPaolo Pisati return (EINVAL); 585bafe5a31SPaolo Pisati 586bafe5a31SPaolo Pisati /* Allocate and populate an interrupt handler structure. */ 587bafe5a31SPaolo Pisati ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 588bafe5a31SPaolo Pisati ih->ih_filter = filter; 589bafe5a31SPaolo Pisati ih->ih_handler = handler; 590bafe5a31SPaolo Pisati ih->ih_argument = arg; 59137b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 592bafe5a31SPaolo Pisati ih->ih_event = ie; 593bafe5a31SPaolo Pisati ih->ih_pri = pri; 594bafe5a31SPaolo Pisati if (flags & INTR_EXCL) 595bafe5a31SPaolo Pisati ih->ih_flags = IH_EXCLUSIVE; 596bafe5a31SPaolo Pisati if (flags & INTR_MPSAFE) 597bafe5a31SPaolo Pisati ih->ih_flags |= IH_MPSAFE; 598bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 599bafe5a31SPaolo Pisati ih->ih_flags |= IH_ENTROPY; 600bafe5a31SPaolo Pisati 601bafe5a31SPaolo Pisati /* We can only have one exclusive handler in a event. */ 602bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 603bafe5a31SPaolo Pisati if (!TAILQ_EMPTY(&ie->ie_handlers)) { 604bafe5a31SPaolo Pisati if ((flags & INTR_EXCL) || 605bafe5a31SPaolo Pisati (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 606bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 607bafe5a31SPaolo Pisati free(ih, M_ITHREAD); 608bafe5a31SPaolo Pisati return (EINVAL); 609bafe5a31SPaolo Pisati } 610bafe5a31SPaolo Pisati } 611bafe5a31SPaolo Pisati 612bafe5a31SPaolo Pisati /* For filtered handlers, create a private ithread to run on. */ 613bafe5a31SPaolo Pisati if (filter != NULL && handler != NULL) { 614bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 615bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 616bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 617bafe5a31SPaolo Pisati it->it_event = ie; 618bafe5a31SPaolo Pisati ih->ih_thread = it; 619037f43d3SSergey Kandaurov ithread_update(it); /* XXX - do we really need this?!?!? */ 620bafe5a31SPaolo Pisati } else { /* Create the global per-event thread if we need one. */ 621bafe5a31SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 622bafe5a31SPaolo Pisati if (ie->ie_flags & IE_ADDING_THREAD) 623bafe5a31SPaolo Pisati msleep(ie, &ie->ie_lock, 0, "ithread", 0); 624bafe5a31SPaolo Pisati else { 625bafe5a31SPaolo Pisati ie->ie_flags |= IE_ADDING_THREAD; 626bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 627bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 628bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 629bafe5a31SPaolo Pisati ie->ie_flags &= ~IE_ADDING_THREAD; 630bafe5a31SPaolo Pisati ie->ie_thread = it; 631bafe5a31SPaolo Pisati it->it_event = ie; 632bafe5a31SPaolo Pisati ithread_update(it); 633bafe5a31SPaolo Pisati wakeup(ie); 634bafe5a31SPaolo Pisati } 635bafe5a31SPaolo Pisati } 636bafe5a31SPaolo Pisati } 637c9516c94SAlexander Kabaev 638c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 639c9516c94SAlexander Kabaev TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 640c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 641c9516c94SAlexander Kabaev break; 642c9516c94SAlexander Kabaev } 643c9516c94SAlexander Kabaev if (temp_ih == NULL) 644c9516c94SAlexander Kabaev TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 645c9516c94SAlexander Kabaev else 646c9516c94SAlexander Kabaev TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 647c9516c94SAlexander Kabaev intr_event_update(ie); 648c9516c94SAlexander Kabaev 649bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 650bafe5a31SPaolo Pisati ie->ie_name); 651bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 652bafe5a31SPaolo Pisati 653bafe5a31SPaolo Pisati if (cookiep != NULL) 654bafe5a31SPaolo Pisati *cookiep = ih; 655bafe5a31SPaolo Pisati return (0); 656bafe5a31SPaolo Pisati } 657bafe5a31SPaolo Pisati #endif 658b4151f71SJohn Baldwin 659c3045318SJohn Baldwin /* 66037b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 66137b8ef16SJohn Baldwin * interrupt handler. 66237b8ef16SJohn Baldwin */ 66337b8ef16SJohn Baldwin int 66437b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 66537b8ef16SJohn Baldwin const char *descr) 66637b8ef16SJohn Baldwin { 66737b8ef16SJohn Baldwin struct intr_handler *ih; 66837b8ef16SJohn Baldwin size_t space; 66937b8ef16SJohn Baldwin char *start; 67037b8ef16SJohn Baldwin 67137b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 67237b8ef16SJohn Baldwin #ifdef INVARIANTS 67337b8ef16SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 67437b8ef16SJohn Baldwin if (ih == cookie) 67537b8ef16SJohn Baldwin break; 67637b8ef16SJohn Baldwin } 67737b8ef16SJohn Baldwin if (ih == NULL) { 67837b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 679d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 68037b8ef16SJohn Baldwin } 68137b8ef16SJohn Baldwin #endif 68237b8ef16SJohn Baldwin ih = cookie; 68337b8ef16SJohn Baldwin 68437b8ef16SJohn Baldwin /* 68537b8ef16SJohn Baldwin * Look for an existing description by checking for an 68637b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 68737b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 68837b8ef16SJohn Baldwin * description at that point. If one is not found, find the 68937b8ef16SJohn Baldwin * end of the name to use as the insertion point. 69037b8ef16SJohn Baldwin */ 691dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 69237b8ef16SJohn Baldwin if (start == NULL) 693dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 69437b8ef16SJohn Baldwin 69537b8ef16SJohn Baldwin /* 69637b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 69737b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 69837b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 69937b8ef16SJohn Baldwin */ 70037b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 70137b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 70237b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 70337b8ef16SJohn Baldwin return (ENOSPC); 70437b8ef16SJohn Baldwin } 70537b8ef16SJohn Baldwin 70637b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 70737b8ef16SJohn Baldwin *start = ':'; 70837b8ef16SJohn Baldwin strcpy(start + 1, descr); 70937b8ef16SJohn Baldwin intr_event_update(ie); 71037b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 71137b8ef16SJohn Baldwin return (0); 71237b8ef16SJohn Baldwin } 71337b8ef16SJohn Baldwin 71437b8ef16SJohn Baldwin /* 715c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 716c3045318SJohn Baldwin * associated with. 717c3045318SJohn Baldwin */ 718c3045318SJohn Baldwin void * 719c3045318SJohn Baldwin intr_handler_source(void *cookie) 720c3045318SJohn Baldwin { 721c3045318SJohn Baldwin struct intr_handler *ih; 722c3045318SJohn Baldwin struct intr_event *ie; 723c3045318SJohn Baldwin 724c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 725c3045318SJohn Baldwin if (ih == NULL) 726c3045318SJohn Baldwin return (NULL); 727c3045318SJohn Baldwin ie = ih->ih_event; 728c3045318SJohn Baldwin KASSERT(ie != NULL, 729c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 730c3045318SJohn Baldwin ih->ih_name)); 731c3045318SJohn Baldwin return (ie->ie_source); 732c3045318SJohn Baldwin } 733c3045318SJohn Baldwin 734e4cd31ddSJeff Roberson /* 735e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 736e4cd31ddSJeff Roberson * 737e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 738e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 739e4cd31ddSJeff Roberson * only. Do not use in BSD code. 740e4cd31ddSJeff Roberson */ 741e4cd31ddSJeff Roberson void 742e4cd31ddSJeff Roberson _intr_drain(int irq) 743e4cd31ddSJeff Roberson { 744e4cd31ddSJeff Roberson struct intr_event *ie; 745e4cd31ddSJeff Roberson struct intr_thread *ithd; 746e4cd31ddSJeff Roberson struct thread *td; 747e4cd31ddSJeff Roberson 748e4cd31ddSJeff Roberson ie = intr_lookup(irq); 749e4cd31ddSJeff Roberson if (ie == NULL) 750e4cd31ddSJeff Roberson return; 751e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 752e4cd31ddSJeff Roberson return; 753e4cd31ddSJeff Roberson ithd = ie->ie_thread; 754e4cd31ddSJeff Roberson td = ithd->it_thread; 7555bd186a6SJeff Roberson /* 7565bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 7575bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 7585bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 7595bd186a6SJeff Roberson */ 760e4cd31ddSJeff Roberson thread_lock(td); 761e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 762e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 7635bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 7645bd186a6SJeff Roberson thread_unlock(td); 7655bd186a6SJeff Roberson pause("idrain", 1); 7665bd186a6SJeff Roberson thread_lock(td); 767e4cd31ddSJeff Roberson } 7685bd186a6SJeff Roberson } 7695bd186a6SJeff Roberson thread_unlock(td); 770e4cd31ddSJeff Roberson return; 771e4cd31ddSJeff Roberson } 772e4cd31ddSJeff Roberson 773e4cd31ddSJeff Roberson 774bafe5a31SPaolo Pisati #ifndef INTR_FILTER 775b4151f71SJohn Baldwin int 776e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 777b4151f71SJohn Baldwin { 778e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 779e0f66ef8SJohn Baldwin struct intr_event *ie; 780b4151f71SJohn Baldwin #ifdef INVARIANTS 781e0f66ef8SJohn Baldwin struct intr_handler *ih; 782e0f66ef8SJohn Baldwin #endif 783e0f66ef8SJohn Baldwin #ifdef notyet 784e0f66ef8SJohn Baldwin int dead; 785b4151f71SJohn Baldwin #endif 786b4151f71SJohn Baldwin 7873e5da754SJohn Baldwin if (handler == NULL) 788b4151f71SJohn Baldwin return (EINVAL); 789e0f66ef8SJohn Baldwin ie = handler->ih_event; 790e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 791e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 7923e5da754SJohn Baldwin handler->ih_name)); 793e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 79491f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 795e0f66ef8SJohn Baldwin ie->ie_name); 796b4151f71SJohn Baldwin #ifdef INVARIANTS 797e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 7983e5da754SJohn Baldwin if (ih == handler) 7993e5da754SJohn Baldwin goto ok; 800e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 801e0f66ef8SJohn Baldwin panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 802e0f66ef8SJohn Baldwin ih->ih_name, ie->ie_name); 8033e5da754SJohn Baldwin ok: 804b4151f71SJohn Baldwin #endif 805de271f01SJohn Baldwin /* 806e0f66ef8SJohn Baldwin * If there is no ithread, then just remove the handler and return. 807e0f66ef8SJohn Baldwin * XXX: Note that an INTR_FAST handler might be running on another 808e0f66ef8SJohn Baldwin * CPU! 809e0f66ef8SJohn Baldwin */ 810e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 811e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 812e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 813e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 814e0f66ef8SJohn Baldwin return (0); 815e0f66ef8SJohn Baldwin } 816e0f66ef8SJohn Baldwin 817e0f66ef8SJohn Baldwin /* 818de271f01SJohn Baldwin * If the interrupt thread is already running, then just mark this 819de271f01SJohn Baldwin * handler as being dead and let the ithread do the actual removal. 820288e351bSDon Lewis * 821288e351bSDon Lewis * During a cold boot while cold is set, msleep() does not sleep, 822288e351bSDon Lewis * so we have to remove the handler here rather than letting the 823288e351bSDon Lewis * thread do it. 824de271f01SJohn Baldwin */ 825982d11f8SJeff Roberson thread_lock(ie->ie_thread->it_thread); 826e0f66ef8SJohn Baldwin if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) { 827de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 828de271f01SJohn Baldwin 829de271f01SJohn Baldwin /* 830de271f01SJohn Baldwin * Ensure that the thread will process the handler list 831de271f01SJohn Baldwin * again and remove this handler if it has already passed 832de271f01SJohn Baldwin * it on the list. 833de271f01SJohn Baldwin */ 8343eebd44dSAlfred Perlstein atomic_store_rel_int(&ie->ie_thread->it_need, 1); 8354d29cb2dSJohn Baldwin } else 836e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 837982d11f8SJeff Roberson thread_unlock(ie->ie_thread->it_thread); 838e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8390f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 840e0f66ef8SJohn Baldwin intr_event_update(ie); 841e0f66ef8SJohn Baldwin #ifdef notyet 842e0f66ef8SJohn Baldwin /* 843e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 844e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 845e0f66ef8SJohn Baldwin * interrupt. 846e0f66ef8SJohn Baldwin */ 847e0f66ef8SJohn Baldwin dead = 1; 848e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 849e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_FAST)) { 850e0f66ef8SJohn Baldwin dead = 0; 851e0f66ef8SJohn Baldwin break; 852e0f66ef8SJohn Baldwin } 853e0f66ef8SJohn Baldwin } 854e0f66ef8SJohn Baldwin if (dead) { 855e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 856e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 857e0f66ef8SJohn Baldwin } 858e0f66ef8SJohn Baldwin #endif 859e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 860b4151f71SJohn Baldwin free(handler, M_ITHREAD); 861b4151f71SJohn Baldwin return (0); 862b4151f71SJohn Baldwin } 863b4151f71SJohn Baldwin 8641ee1b687SJohn Baldwin static int 865e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 8663e5da754SJohn Baldwin { 867e0f66ef8SJohn Baldwin struct intr_entropy entropy; 868e0f66ef8SJohn Baldwin struct intr_thread *it; 869b40ce416SJulian Elischer struct thread *td; 87004774f23SJulian Elischer struct thread *ctd; 8713e5da754SJohn Baldwin struct proc *p; 8723e5da754SJohn Baldwin 8733e5da754SJohn Baldwin /* 8743e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 8753e5da754SJohn Baldwin */ 876e0f66ef8SJohn Baldwin if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || 877e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 8783e5da754SJohn Baldwin return (EINVAL); 8793e5da754SJohn Baldwin 88004774f23SJulian Elischer ctd = curthread; 881e0f66ef8SJohn Baldwin it = ie->ie_thread; 882e0f66ef8SJohn Baldwin td = it->it_thread; 8836f40c417SRobert Watson p = td->td_proc; 884e0f66ef8SJohn Baldwin 8853e5da754SJohn Baldwin /* 8863e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 8873e5da754SJohn Baldwin * sources of entropy, then gather some. 8883e5da754SJohn Baldwin */ 889e0f66ef8SJohn Baldwin if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 8906f40c417SRobert Watson CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 8917ab24ea3SJulian Elischer p->p_pid, td->td_name); 892e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 893e0f66ef8SJohn Baldwin entropy.td = ctd; 894f02e47dcSMark Murray random_harvest(&entropy, sizeof(entropy), 2, 8953e5da754SJohn Baldwin RANDOM_INTERRUPT); 8963e5da754SJohn Baldwin } 8973e5da754SJohn Baldwin 898e0f66ef8SJohn Baldwin KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 8993e5da754SJohn Baldwin 9003e5da754SJohn Baldwin /* 9013e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 902982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 903982d11f8SJeff Roberson * put it on the runqueue. 9043e5da754SJohn Baldwin */ 9053eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 906982d11f8SJeff Roberson thread_lock(td); 90771fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 908e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 9097ab24ea3SJulian Elischer td->td_name); 91071fad9fdSJulian Elischer TD_CLR_IWAIT(td); 911f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 9123e5da754SJohn Baldwin } else { 913e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 9147ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 9153e5da754SJohn Baldwin } 916982d11f8SJeff Roberson thread_unlock(td); 9173e5da754SJohn Baldwin 9183e5da754SJohn Baldwin return (0); 9193e5da754SJohn Baldwin } 920bafe5a31SPaolo Pisati #else 921bafe5a31SPaolo Pisati int 922bafe5a31SPaolo Pisati intr_event_remove_handler(void *cookie) 923bafe5a31SPaolo Pisati { 924bafe5a31SPaolo Pisati struct intr_handler *handler = (struct intr_handler *)cookie; 925bafe5a31SPaolo Pisati struct intr_event *ie; 926bafe5a31SPaolo Pisati struct intr_thread *it; 927bafe5a31SPaolo Pisati #ifdef INVARIANTS 928bafe5a31SPaolo Pisati struct intr_handler *ih; 929bafe5a31SPaolo Pisati #endif 930bafe5a31SPaolo Pisati #ifdef notyet 931bafe5a31SPaolo Pisati int dead; 932bafe5a31SPaolo Pisati #endif 933bafe5a31SPaolo Pisati 934bafe5a31SPaolo Pisati if (handler == NULL) 935bafe5a31SPaolo Pisati return (EINVAL); 936bafe5a31SPaolo Pisati ie = handler->ih_event; 937bafe5a31SPaolo Pisati KASSERT(ie != NULL, 938bafe5a31SPaolo Pisati ("interrupt handler \"%s\" has a NULL interrupt event", 939bafe5a31SPaolo Pisati handler->ih_name)); 940bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 941bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 942bafe5a31SPaolo Pisati ie->ie_name); 943bafe5a31SPaolo Pisati #ifdef INVARIANTS 944bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 945bafe5a31SPaolo Pisati if (ih == handler) 946bafe5a31SPaolo Pisati goto ok; 947bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 948bafe5a31SPaolo Pisati panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 949bafe5a31SPaolo Pisati ih->ih_name, ie->ie_name); 950bafe5a31SPaolo Pisati ok: 951bafe5a31SPaolo Pisati #endif 952bafe5a31SPaolo Pisati /* 953bafe5a31SPaolo Pisati * If there are no ithreads (per event and per handler), then 954bafe5a31SPaolo Pisati * just remove the handler and return. 955bafe5a31SPaolo Pisati * XXX: Note that an INTR_FAST handler might be running on another CPU! 956bafe5a31SPaolo Pisati */ 957bafe5a31SPaolo Pisati if (ie->ie_thread == NULL && handler->ih_thread == NULL) { 958bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 959bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 960bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 961bafe5a31SPaolo Pisati return (0); 962bafe5a31SPaolo Pisati } 963bafe5a31SPaolo Pisati 964bafe5a31SPaolo Pisati /* Private or global ithread? */ 965bafe5a31SPaolo Pisati it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread; 966bafe5a31SPaolo Pisati /* 967bafe5a31SPaolo Pisati * If the interrupt thread is already running, then just mark this 968bafe5a31SPaolo Pisati * handler as being dead and let the ithread do the actual removal. 969bafe5a31SPaolo Pisati * 970bafe5a31SPaolo Pisati * During a cold boot while cold is set, msleep() does not sleep, 971bafe5a31SPaolo Pisati * so we have to remove the handler here rather than letting the 972bafe5a31SPaolo Pisati * thread do it. 973bafe5a31SPaolo Pisati */ 974982d11f8SJeff Roberson thread_lock(it->it_thread); 975bafe5a31SPaolo Pisati if (!TD_AWAITING_INTR(it->it_thread) && !cold) { 976bafe5a31SPaolo Pisati handler->ih_flags |= IH_DEAD; 977bafe5a31SPaolo Pisati 978bafe5a31SPaolo Pisati /* 979bafe5a31SPaolo Pisati * Ensure that the thread will process the handler list 980bafe5a31SPaolo Pisati * again and remove this handler if it has already passed 981bafe5a31SPaolo Pisati * it on the list. 982bafe5a31SPaolo Pisati */ 9833eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 984bafe5a31SPaolo Pisati } else 985bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 986982d11f8SJeff Roberson thread_unlock(it->it_thread); 987bafe5a31SPaolo Pisati while (handler->ih_flags & IH_DEAD) 988bafe5a31SPaolo Pisati msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 989bafe5a31SPaolo Pisati /* 990bafe5a31SPaolo Pisati * At this point, the handler has been disconnected from the event, 991bafe5a31SPaolo Pisati * so we can kill the private ithread if any. 992bafe5a31SPaolo Pisati */ 993bafe5a31SPaolo Pisati if (handler->ih_thread) { 994bafe5a31SPaolo Pisati ithread_destroy(handler->ih_thread); 995bafe5a31SPaolo Pisati handler->ih_thread = NULL; 996bafe5a31SPaolo Pisati } 997bafe5a31SPaolo Pisati intr_event_update(ie); 998bafe5a31SPaolo Pisati #ifdef notyet 999bafe5a31SPaolo Pisati /* 1000bafe5a31SPaolo Pisati * XXX: This could be bad in the case of ppbus(8). Also, I think 1001bafe5a31SPaolo Pisati * this could lead to races of stale data when servicing an 1002bafe5a31SPaolo Pisati * interrupt. 1003bafe5a31SPaolo Pisati */ 1004bafe5a31SPaolo Pisati dead = 1; 1005bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1006bafe5a31SPaolo Pisati if (handler != NULL) { 1007bafe5a31SPaolo Pisati dead = 0; 1008bafe5a31SPaolo Pisati break; 1009bafe5a31SPaolo Pisati } 1010bafe5a31SPaolo Pisati } 1011bafe5a31SPaolo Pisati if (dead) { 1012bafe5a31SPaolo Pisati ithread_destroy(ie->ie_thread); 1013bafe5a31SPaolo Pisati ie->ie_thread = NULL; 1014bafe5a31SPaolo Pisati } 1015bafe5a31SPaolo Pisati #endif 1016bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 1017bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 1018bafe5a31SPaolo Pisati return (0); 1019bafe5a31SPaolo Pisati } 1020bafe5a31SPaolo Pisati 10211ee1b687SJohn Baldwin static int 1022bafe5a31SPaolo Pisati intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it) 1023bafe5a31SPaolo Pisati { 1024bafe5a31SPaolo Pisati struct intr_entropy entropy; 1025bafe5a31SPaolo Pisati struct thread *td; 1026bafe5a31SPaolo Pisati struct thread *ctd; 1027bafe5a31SPaolo Pisati struct proc *p; 1028bafe5a31SPaolo Pisati 1029bafe5a31SPaolo Pisati /* 1030bafe5a31SPaolo Pisati * If no ithread or no handlers, then we have a stray interrupt. 1031bafe5a31SPaolo Pisati */ 1032bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL) 1033bafe5a31SPaolo Pisati return (EINVAL); 1034bafe5a31SPaolo Pisati 1035bafe5a31SPaolo Pisati ctd = curthread; 1036bafe5a31SPaolo Pisati td = it->it_thread; 1037bafe5a31SPaolo Pisati p = td->td_proc; 1038bafe5a31SPaolo Pisati 1039bafe5a31SPaolo Pisati /* 1040bafe5a31SPaolo Pisati * If any of the handlers for this ithread claim to be good 1041bafe5a31SPaolo Pisati * sources of entropy, then gather some. 1042bafe5a31SPaolo Pisati */ 1043bafe5a31SPaolo Pisati if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 1044bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 10457ab24ea3SJulian Elischer p->p_pid, td->td_name); 1046bafe5a31SPaolo Pisati entropy.event = (uintptr_t)ie; 1047bafe5a31SPaolo Pisati entropy.td = ctd; 1048f02e47dcSMark Murray random_harvest(&entropy, sizeof(entropy), 2, 1049bafe5a31SPaolo Pisati RANDOM_INTERRUPT); 1050bafe5a31SPaolo Pisati } 1051bafe5a31SPaolo Pisati 1052bafe5a31SPaolo Pisati KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 1053bafe5a31SPaolo Pisati 1054bafe5a31SPaolo Pisati /* 1055bafe5a31SPaolo Pisati * Set it_need to tell the thread to keep running if it is already 1056982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 1057982d11f8SJeff Roberson * put it on the runqueue. 1058bafe5a31SPaolo Pisati */ 10593eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 1060982d11f8SJeff Roberson thread_lock(td); 1061bafe5a31SPaolo Pisati if (TD_AWAITING_INTR(td)) { 1062bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 10633c1ffc32SJulian Elischer td->td_name); 1064bafe5a31SPaolo Pisati TD_CLR_IWAIT(td); 1065bafe5a31SPaolo Pisati sched_add(td, SRQ_INTR); 1066bafe5a31SPaolo Pisati } else { 1067bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 10687ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 1069bafe5a31SPaolo Pisati } 1070982d11f8SJeff Roberson thread_unlock(td); 1071bafe5a31SPaolo Pisati 1072bafe5a31SPaolo Pisati return (0); 1073bafe5a31SPaolo Pisati } 1074bafe5a31SPaolo Pisati #endif 10753e5da754SJohn Baldwin 1076fe486a37SJohn Baldwin /* 1077e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1078e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 1079e84bcd84SRobert Watson * a PIC. 1080e84bcd84SRobert Watson */ 1081e84bcd84SRobert Watson static int 1082066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 1083e84bcd84SRobert Watson { 1084e84bcd84SRobert Watson 1085e84bcd84SRobert Watson return (0); 1086e84bcd84SRobert Watson } 1087e84bcd84SRobert Watson 1088e84bcd84SRobert Watson /* 1089fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 1090fe486a37SJohn Baldwin * is not specified, then a new event is created. 1091fe486a37SJohn Baldwin */ 10923e5da754SJohn Baldwin int 1093e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 1094b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 10958088699fSJohn Baldwin { 1096e0f66ef8SJohn Baldwin struct intr_event *ie; 1097b4151f71SJohn Baldwin int error; 10988088699fSJohn Baldwin 1099bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 11003e5da754SJohn Baldwin return (EINVAL); 11013e5da754SJohn Baldwin 1102e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 11038088699fSJohn Baldwin 1104e0f66ef8SJohn Baldwin if (ie != NULL) { 1105e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 11063e5da754SJohn Baldwin return (EINVAL); 11073e5da754SJohn Baldwin } else { 11089b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1109e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 11108088699fSJohn Baldwin if (error) 1111b4151f71SJohn Baldwin return (error); 1112e0f66ef8SJohn Baldwin if (eventp != NULL) 1113e0f66ef8SJohn Baldwin *eventp = ie; 11148088699fSJohn Baldwin } 11158d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1116d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 11178d809d50SJeff Roberson return (error); 11188088699fSJohn Baldwin } 11198088699fSJohn Baldwin 11201931cf94SJohn Baldwin /* 1121e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 11221931cf94SJohn Baldwin */ 11231931cf94SJohn Baldwin void 1124b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 11251931cf94SJohn Baldwin { 1126e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1127e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1128d95dca1dSJohn Baldwin struct intr_entropy entropy; 11293e5da754SJohn Baldwin int error; 11308088699fSJohn Baldwin 1131e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1132e0f66ef8SJohn Baldwin ih->ih_need); 11331931cf94SJohn Baldwin 1134d95dca1dSJohn Baldwin if (harvest.swi) { 1135d95dca1dSJohn Baldwin CTR2(KTR_INTR, "swi_sched: pid %d (%s) gathering entropy", 1136d95dca1dSJohn Baldwin curproc->p_pid, curthread->td_name); 1137d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1138d95dca1dSJohn Baldwin entropy.td = curthread; 1139f02e47dcSMark Murray random_harvest(&entropy, sizeof(entropy), 1, 1140c495c935SMark Murray RANDOM_SWI); 1141d95dca1dSJohn Baldwin } 1142d95dca1dSJohn Baldwin 11431931cf94SJohn Baldwin /* 11443e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 11453e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 11463e5da754SJohn Baldwin * it will execute it the next time it runs. 11471931cf94SJohn Baldwin */ 1148b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 1); 11491ca2c018SBruce Evans 1150b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 115167596082SAttilio Rao PCPU_INC(cnt.v_soft); 1152bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1153bafe5a31SPaolo Pisati error = intr_event_schedule_thread(ie, ie->ie_thread); 1154bafe5a31SPaolo Pisati #else 1155e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 1156bafe5a31SPaolo Pisati #endif 11573e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 11588088699fSJohn Baldwin } 11598088699fSJohn Baldwin } 11608088699fSJohn Baldwin 1161fe486a37SJohn Baldwin /* 1162fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1163fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1164fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1165fe486a37SJohn Baldwin * an optimal interface. 1166fe486a37SJohn Baldwin */ 1167fe486a37SJohn Baldwin int 1168fe486a37SJohn Baldwin swi_remove(void *cookie) 1169fe486a37SJohn Baldwin { 1170fe486a37SJohn Baldwin 1171fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1172fe486a37SJohn Baldwin } 1173fe486a37SJohn Baldwin 1174bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1175bafe5a31SPaolo Pisati static void 1176bafe5a31SPaolo Pisati priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih) 1177bafe5a31SPaolo Pisati { 1178bafe5a31SPaolo Pisati struct intr_event *ie; 1179bafe5a31SPaolo Pisati 1180bafe5a31SPaolo Pisati ie = ih->ih_event; 1181bafe5a31SPaolo Pisati /* 1182bafe5a31SPaolo Pisati * If this handler is marked for death, remove it from 1183bafe5a31SPaolo Pisati * the list of handlers and wake up the sleeper. 1184bafe5a31SPaolo Pisati */ 1185bafe5a31SPaolo Pisati if (ih->ih_flags & IH_DEAD) { 1186bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 1187bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1188bafe5a31SPaolo Pisati ih->ih_flags &= ~IH_DEAD; 1189bafe5a31SPaolo Pisati wakeup(ih); 1190bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 1191bafe5a31SPaolo Pisati return; 1192bafe5a31SPaolo Pisati } 1193bafe5a31SPaolo Pisati 1194bafe5a31SPaolo Pisati /* Execute this handler. */ 1195bafe5a31SPaolo Pisati CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1196bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument, 1197bafe5a31SPaolo Pisati ih->ih_name, ih->ih_flags); 1198bafe5a31SPaolo Pisati 1199bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 1200bafe5a31SPaolo Pisati mtx_lock(&Giant); 1201bafe5a31SPaolo Pisati ih->ih_handler(ih->ih_argument); 1202bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 1203bafe5a31SPaolo Pisati mtx_unlock(&Giant); 1204bafe5a31SPaolo Pisati } 1205bafe5a31SPaolo Pisati #endif 1206bafe5a31SPaolo Pisati 120737e9511fSJohn Baldwin /* 120837e9511fSJohn Baldwin * This is a public function for use by drivers that mux interrupt 120937e9511fSJohn Baldwin * handlers for child devices from their interrupt handler. 121037e9511fSJohn Baldwin */ 121137e9511fSJohn Baldwin void 121237e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1213e0f66ef8SJohn Baldwin { 1214e0f66ef8SJohn Baldwin struct intr_handler *ih, *ihn; 1215e0f66ef8SJohn Baldwin 1216e0f66ef8SJohn Baldwin TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1217e0f66ef8SJohn Baldwin /* 1218e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1219e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1220e0f66ef8SJohn Baldwin */ 1221e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1222e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1223e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1224e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1225e0f66ef8SJohn Baldwin wakeup(ih); 1226e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1227e0f66ef8SJohn Baldwin continue; 1228e0f66ef8SJohn Baldwin } 1229e0f66ef8SJohn Baldwin 1230f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1231f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1232f2d619c8SPaolo Pisati continue; 1233f2d619c8SPaolo Pisati 1234e0f66ef8SJohn Baldwin /* 1235e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1236e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1237e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 1238e0f66ef8SJohn Baldwin */ 1239e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 12403eebd44dSAlfred Perlstein if (atomic_load_acq_int(&ih->ih_need) == 0) 1241e0f66ef8SJohn Baldwin continue; 1242e0f66ef8SJohn Baldwin else 1243e0f66ef8SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 0); 1244e0f66ef8SJohn Baldwin } 1245e0f66ef8SJohn Baldwin 1246e0f66ef8SJohn Baldwin /* Execute this handler. */ 1247e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1248bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1249bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1250e0f66ef8SJohn Baldwin 1251e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1252e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1253e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1254e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1255e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1256e0f66ef8SJohn Baldwin } 125737e9511fSJohn Baldwin } 125837e9511fSJohn Baldwin 125937e9511fSJohn Baldwin static void 126037e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 126137e9511fSJohn Baldwin { 126237e9511fSJohn Baldwin 126337e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 126437e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 126537e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 126637e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1267e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1268e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1269e0f66ef8SJohn Baldwin 1270e0f66ef8SJohn Baldwin /* 1271e0f66ef8SJohn Baldwin * Interrupt storm handling: 1272e0f66ef8SJohn Baldwin * 1273e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1274e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1275e0f66ef8SJohn Baldwin * 1276e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1277e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1278e0f66ef8SJohn Baldwin * then enter storming mode. 1279e0f66ef8SJohn Baldwin */ 1280e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1281e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 12820ae62c18SNate Lawson /* Report the message only once every second. */ 12830ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1284e0f66ef8SJohn Baldwin printf( 12850ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1286e0f66ef8SJohn Baldwin ie->ie_name); 1287e0f66ef8SJohn Baldwin } 1288e41bcf3cSJohn Baldwin pause("istorm", 1); 1289e0f66ef8SJohn Baldwin } else 1290e0f66ef8SJohn Baldwin ie->ie_count++; 1291e0f66ef8SJohn Baldwin 1292e0f66ef8SJohn Baldwin /* 1293e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1294e0f66ef8SJohn Baldwin * the interrupt source. 1295e0f66ef8SJohn Baldwin */ 12961ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 12971ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1298e0f66ef8SJohn Baldwin } 1299e0f66ef8SJohn Baldwin 1300bafe5a31SPaolo Pisati #ifndef INTR_FILTER 13018088699fSJohn Baldwin /* 1302b4151f71SJohn Baldwin * This is the main code for interrupt threads. 13038088699fSJohn Baldwin */ 130437c84183SPoul-Henning Kamp static void 1305b4151f71SJohn Baldwin ithread_loop(void *arg) 13068088699fSJohn Baldwin { 1307e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1308e0f66ef8SJohn Baldwin struct intr_event *ie; 1309b40ce416SJulian Elischer struct thread *td; 1310b4151f71SJohn Baldwin struct proc *p; 1311e4cd31ddSJeff Roberson int wake; 13128088699fSJohn Baldwin 1313b40ce416SJulian Elischer td = curthread; 1314b40ce416SJulian Elischer p = td->td_proc; 1315e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1316e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 131791f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1318e0f66ef8SJohn Baldwin ie = ithd->it_event; 1319e0f66ef8SJohn Baldwin ie->ie_count = 0; 1320e4cd31ddSJeff Roberson wake = 0; 13218088699fSJohn Baldwin 13228088699fSJohn Baldwin /* 13238088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 13248088699fSJohn Baldwin * list of handlers, giving each one a go at it. 13258088699fSJohn Baldwin */ 13268088699fSJohn Baldwin for (;;) { 1327b4151f71SJohn Baldwin /* 1328b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1329b4151f71SJohn Baldwin */ 1330b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1331e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 13327ab24ea3SJulian Elischer p->p_pid, td->td_name); 1333b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1334ca9a0ddfSJulian Elischer kthread_exit(); 1335b4151f71SJohn Baldwin } 1336b4151f71SJohn Baldwin 1337e0f66ef8SJohn Baldwin /* 1338e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1339e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1340e0f66ef8SJohn Baldwin * should make another pass. 1341e0f66ef8SJohn Baldwin */ 13423eebd44dSAlfred Perlstein while (atomic_load_acq_int(&ithd->it_need) != 0) { 13438088699fSJohn Baldwin /* 1344e0f66ef8SJohn Baldwin * This might need a full read and write barrier 1345e0f66ef8SJohn Baldwin * to make sure that this write posts before any 1346e0f66ef8SJohn Baldwin * of the memory or device accesses in the 1347e0f66ef8SJohn Baldwin * handlers. 13488088699fSJohn Baldwin */ 1349b4151f71SJohn Baldwin atomic_store_rel_int(&ithd->it_need, 0); 1350e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 13518088699fSJohn Baldwin } 13527870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 13537870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 13548088699fSJohn Baldwin 13558088699fSJohn Baldwin /* 13568088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 13578088699fSJohn Baldwin * lock. This may take a while and it_need may get 13588088699fSJohn Baldwin * set again, so we have to check it again. 13598088699fSJohn Baldwin */ 1360982d11f8SJeff Roberson thread_lock(td); 13613eebd44dSAlfred Perlstein if ((atomic_load_acq_int(&ithd->it_need) == 0) && 13623eebd44dSAlfred Perlstein !(ithd->it_flags & (IT_DEAD | IT_WAIT))) { 13637870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1364e0f66ef8SJohn Baldwin ie->ie_count = 0; 13658df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 13668088699fSJohn Baldwin } 1367e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1368e4cd31ddSJeff Roberson wake = 1; 1369e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1370e4cd31ddSJeff Roberson } 1371982d11f8SJeff Roberson thread_unlock(td); 1372e4cd31ddSJeff Roberson if (wake) { 1373e4cd31ddSJeff Roberson wakeup(ithd); 1374e4cd31ddSJeff Roberson wake = 0; 1375e4cd31ddSJeff Roberson } 13768088699fSJohn Baldwin } 13771931cf94SJohn Baldwin } 13781ee1b687SJohn Baldwin 13791ee1b687SJohn Baldwin /* 13801ee1b687SJohn Baldwin * Main interrupt handling body. 13811ee1b687SJohn Baldwin * 13821ee1b687SJohn Baldwin * Input: 13831ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 13841ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 13851ee1b687SJohn Baldwin * handlers as their main argument. 13861ee1b687SJohn Baldwin * Return value: 13871ee1b687SJohn Baldwin * o 0: everything ok. 13881ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 13891ee1b687SJohn Baldwin */ 13901ee1b687SJohn Baldwin int 13911ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 13921ee1b687SJohn Baldwin { 13931ee1b687SJohn Baldwin struct intr_handler *ih; 13941f255bd3SAlexander Motin struct trapframe *oldframe; 13951ee1b687SJohn Baldwin struct thread *td; 13961ee1b687SJohn Baldwin int error, ret, thread; 13971ee1b687SJohn Baldwin 13981ee1b687SJohn Baldwin td = curthread; 13991ee1b687SJohn Baldwin 1400*b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1401*b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1402*b7627840SKonstantin Belousov #endif 1403*b7627840SKonstantin Belousov 14041ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 14051ee1b687SJohn Baldwin if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 14061ee1b687SJohn Baldwin return (EINVAL); 14071ee1b687SJohn Baldwin 14081ee1b687SJohn Baldwin /* 14091ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 14101ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 14111ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 14121ee1b687SJohn Baldwin * a trapframe as its argument. 14131ee1b687SJohn Baldwin */ 14141ee1b687SJohn Baldwin td->td_intr_nesting_level++; 14151ee1b687SJohn Baldwin thread = 0; 14161ee1b687SJohn Baldwin ret = 0; 14171ee1b687SJohn Baldwin critical_enter(); 14181f255bd3SAlexander Motin oldframe = td->td_intr_frame; 14191f255bd3SAlexander Motin td->td_intr_frame = frame; 14201ee1b687SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 14211ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 14221ee1b687SJohn Baldwin thread = 1; 14231ee1b687SJohn Baldwin continue; 14241ee1b687SJohn Baldwin } 14251ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 14261ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 14271ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 14281ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 14291ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 14301ee1b687SJohn Baldwin else 14311ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 143289fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 143389fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 143489fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 143589fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 143689fc20ccSAndriy Gapon ih->ih_name)); 143789fc20ccSAndriy Gapon 14381ee1b687SJohn Baldwin /* 14391ee1b687SJohn Baldwin * Wrapper handler special handling: 14401ee1b687SJohn Baldwin * 14411ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 14421ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 14431ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 14441ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 14451ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 14461ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 14471ee1b687SJohn Baldwin * of just an ithread handler. 14481ee1b687SJohn Baldwin * 14491ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 14501ee1b687SJohn Baldwin * their own 14511ee1b687SJohn Baldwin */ 14521ee1b687SJohn Baldwin if (!thread) { 14531ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 14541ee1b687SJohn Baldwin thread = 1; 14551ee1b687SJohn Baldwin } 14561ee1b687SJohn Baldwin } 14571f255bd3SAlexander Motin td->td_intr_frame = oldframe; 14581ee1b687SJohn Baldwin 14591ee1b687SJohn Baldwin if (thread) { 14601ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 14611ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 14621ee1b687SJohn Baldwin } else { 14631ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 14641ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 14651ee1b687SJohn Baldwin } 14661ee1b687SJohn Baldwin 14671ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 14681ee1b687SJohn Baldwin if (thread) { 14691ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 14706205924aSKip Macy #ifndef XEN 14711ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 14726205924aSKip Macy #else 14736205924aSKip Macy if (error != 0) 14746205924aSKip Macy log(LOG_WARNING, "bad stray interrupt"); 14756205924aSKip Macy #endif 14761ee1b687SJohn Baldwin } 14771ee1b687SJohn Baldwin critical_exit(); 14781ee1b687SJohn Baldwin td->td_intr_nesting_level--; 14791ee1b687SJohn Baldwin return (0); 14801ee1b687SJohn Baldwin } 1481bafe5a31SPaolo Pisati #else 1482bafe5a31SPaolo Pisati /* 1483bafe5a31SPaolo Pisati * This is the main code for interrupt threads. 1484bafe5a31SPaolo Pisati */ 1485bafe5a31SPaolo Pisati static void 1486bafe5a31SPaolo Pisati ithread_loop(void *arg) 1487bafe5a31SPaolo Pisati { 1488bafe5a31SPaolo Pisati struct intr_thread *ithd; 1489bafe5a31SPaolo Pisati struct intr_handler *ih; 1490bafe5a31SPaolo Pisati struct intr_event *ie; 1491bafe5a31SPaolo Pisati struct thread *td; 1492bafe5a31SPaolo Pisati struct proc *p; 1493bafe5a31SPaolo Pisati int priv; 1494e4cd31ddSJeff Roberson int wake; 1495bafe5a31SPaolo Pisati 1496bafe5a31SPaolo Pisati td = curthread; 1497bafe5a31SPaolo Pisati p = td->td_proc; 1498bafe5a31SPaolo Pisati ih = (struct intr_handler *)arg; 1499bafe5a31SPaolo Pisati priv = (ih->ih_thread != NULL) ? 1 : 0; 1500bafe5a31SPaolo Pisati ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread; 1501bafe5a31SPaolo Pisati KASSERT(ithd->it_thread == td, 1502bafe5a31SPaolo Pisati ("%s: ithread and proc linkage out of sync", __func__)); 1503bafe5a31SPaolo Pisati ie = ithd->it_event; 1504bafe5a31SPaolo Pisati ie->ie_count = 0; 1505e4cd31ddSJeff Roberson wake = 0; 1506bafe5a31SPaolo Pisati 1507bafe5a31SPaolo Pisati /* 1508bafe5a31SPaolo Pisati * As long as we have interrupts outstanding, go through the 1509bafe5a31SPaolo Pisati * list of handlers, giving each one a go at it. 1510bafe5a31SPaolo Pisati */ 1511bafe5a31SPaolo Pisati for (;;) { 1512bafe5a31SPaolo Pisati /* 1513bafe5a31SPaolo Pisati * If we are an orphaned thread, then just die. 1514bafe5a31SPaolo Pisati */ 1515bafe5a31SPaolo Pisati if (ithd->it_flags & IT_DEAD) { 1516bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 15177ab24ea3SJulian Elischer p->p_pid, td->td_name); 1518bafe5a31SPaolo Pisati free(ithd, M_ITHREAD); 1519ca9a0ddfSJulian Elischer kthread_exit(); 1520bafe5a31SPaolo Pisati } 1521bafe5a31SPaolo Pisati 1522bafe5a31SPaolo Pisati /* 1523bafe5a31SPaolo Pisati * Service interrupts. If another interrupt arrives while 1524bafe5a31SPaolo Pisati * we are running, it will set it_need to note that we 1525bafe5a31SPaolo Pisati * should make another pass. 1526bafe5a31SPaolo Pisati */ 15273eebd44dSAlfred Perlstein while (atomic_load_acq_int(&ithd->it_need) != 0) { 1528bafe5a31SPaolo Pisati /* 1529bafe5a31SPaolo Pisati * This might need a full read and write barrier 1530bafe5a31SPaolo Pisati * to make sure that this write posts before any 1531bafe5a31SPaolo Pisati * of the memory or device accesses in the 1532bafe5a31SPaolo Pisati * handlers. 1533bafe5a31SPaolo Pisati */ 1534bafe5a31SPaolo Pisati atomic_store_rel_int(&ithd->it_need, 0); 1535bafe5a31SPaolo Pisati if (priv) 1536bafe5a31SPaolo Pisati priv_ithread_execute_handler(p, ih); 1537bafe5a31SPaolo Pisati else 1538bafe5a31SPaolo Pisati ithread_execute_handlers(p, ie); 1539bafe5a31SPaolo Pisati } 1540bafe5a31SPaolo Pisati WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 1541bafe5a31SPaolo Pisati mtx_assert(&Giant, MA_NOTOWNED); 1542bafe5a31SPaolo Pisati 1543bafe5a31SPaolo Pisati /* 1544bafe5a31SPaolo Pisati * Processed all our interrupts. Now get the sched 1545bafe5a31SPaolo Pisati * lock. This may take a while and it_need may get 1546bafe5a31SPaolo Pisati * set again, so we have to check it again. 1547bafe5a31SPaolo Pisati */ 1548982d11f8SJeff Roberson thread_lock(td); 15493eebd44dSAlfred Perlstein if ((atomic_load_acq_int(&ithd->it_need) == 0) && 15503eebd44dSAlfred Perlstein !(ithd->it_flags & (IT_DEAD | IT_WAIT))) { 1551bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 1552bafe5a31SPaolo Pisati ie->ie_count = 0; 15538df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 1554bafe5a31SPaolo Pisati } 1555e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1556e4cd31ddSJeff Roberson wake = 1; 1557e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1558e4cd31ddSJeff Roberson } 1559982d11f8SJeff Roberson thread_unlock(td); 1560e4cd31ddSJeff Roberson if (wake) { 1561e4cd31ddSJeff Roberson wakeup(ithd); 1562e4cd31ddSJeff Roberson wake = 0; 1563e4cd31ddSJeff Roberson } 1564bafe5a31SPaolo Pisati } 1565bafe5a31SPaolo Pisati } 1566bafe5a31SPaolo Pisati 1567bafe5a31SPaolo Pisati /* 1568bafe5a31SPaolo Pisati * Main loop for interrupt filter. 1569bafe5a31SPaolo Pisati * 1570bafe5a31SPaolo Pisati * Some architectures (i386, amd64 and arm) require the optional frame 1571bafe5a31SPaolo Pisati * parameter, and use it as the main argument for fast handler execution 1572bafe5a31SPaolo Pisati * when ih_argument == NULL. 1573bafe5a31SPaolo Pisati * 1574bafe5a31SPaolo Pisati * Return value: 1575bafe5a31SPaolo Pisati * o FILTER_STRAY: No filter recognized the event, and no 1576bafe5a31SPaolo Pisati * filter-less handler is registered on this 1577bafe5a31SPaolo Pisati * line. 1578bafe5a31SPaolo Pisati * o FILTER_HANDLED: A filter claimed the event and served it. 1579bafe5a31SPaolo Pisati * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at 1580bafe5a31SPaolo Pisati * least one filter-less handler on this line. 1581bafe5a31SPaolo Pisati * o FILTER_HANDLED | 1582bafe5a31SPaolo Pisati * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for 1583bafe5a31SPaolo Pisati * scheduling the per-handler ithread. 1584bafe5a31SPaolo Pisati * 1585bafe5a31SPaolo Pisati * In case an ithread has to be scheduled, in *ithd there will be a 1586bafe5a31SPaolo Pisati * pointer to a struct intr_thread containing the thread to be 1587bafe5a31SPaolo Pisati * scheduled. 1588bafe5a31SPaolo Pisati */ 1589bafe5a31SPaolo Pisati 15901ee1b687SJohn Baldwin static int 1591bafe5a31SPaolo Pisati intr_filter_loop(struct intr_event *ie, struct trapframe *frame, 1592bafe5a31SPaolo Pisati struct intr_thread **ithd) 1593bafe5a31SPaolo Pisati { 1594bafe5a31SPaolo Pisati struct intr_handler *ih; 1595bafe5a31SPaolo Pisati void *arg; 1596bafe5a31SPaolo Pisati int ret, thread_only; 1597bafe5a31SPaolo Pisati 1598bafe5a31SPaolo Pisati ret = 0; 1599bafe5a31SPaolo Pisati thread_only = 0; 1600bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1601bafe5a31SPaolo Pisati /* 1602bafe5a31SPaolo Pisati * Execute fast interrupt handlers directly. 1603bafe5a31SPaolo Pisati * To support clock handlers, if a handler registers 1604bafe5a31SPaolo Pisati * with a NULL argument, then we pass it a pointer to 1605bafe5a31SPaolo Pisati * a trapframe as its argument. 1606bafe5a31SPaolo Pisati */ 1607bafe5a31SPaolo Pisati arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument); 1608bafe5a31SPaolo Pisati 1609bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__, 1610bafe5a31SPaolo Pisati ih->ih_filter, ih->ih_handler, arg, ih->ih_name); 1611bafe5a31SPaolo Pisati 1612bafe5a31SPaolo Pisati if (ih->ih_filter != NULL) 1613bafe5a31SPaolo Pisati ret = ih->ih_filter(arg); 1614bafe5a31SPaolo Pisati else { 1615bafe5a31SPaolo Pisati thread_only = 1; 1616bafe5a31SPaolo Pisati continue; 1617bafe5a31SPaolo Pisati } 161889fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 161989fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 162089fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 162189fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 162289fc20ccSAndriy Gapon ih->ih_name)); 1623bafe5a31SPaolo Pisati if (ret & FILTER_STRAY) 1624bafe5a31SPaolo Pisati continue; 1625bafe5a31SPaolo Pisati else { 1626bafe5a31SPaolo Pisati *ithd = ih->ih_thread; 1627bafe5a31SPaolo Pisati return (ret); 1628bafe5a31SPaolo Pisati } 1629bafe5a31SPaolo Pisati } 1630bafe5a31SPaolo Pisati 1631bafe5a31SPaolo Pisati /* 1632bafe5a31SPaolo Pisati * No filters handled the interrupt and we have at least 1633bafe5a31SPaolo Pisati * one handler without a filter. In this case, we schedule 1634bafe5a31SPaolo Pisati * all of the filter-less handlers to run in the ithread. 1635bafe5a31SPaolo Pisati */ 1636bafe5a31SPaolo Pisati if (thread_only) { 1637bafe5a31SPaolo Pisati *ithd = ie->ie_thread; 1638bafe5a31SPaolo Pisati return (FILTER_SCHEDULE_THREAD); 1639bafe5a31SPaolo Pisati } 1640bafe5a31SPaolo Pisati return (FILTER_STRAY); 1641bafe5a31SPaolo Pisati } 1642bafe5a31SPaolo Pisati 1643bafe5a31SPaolo Pisati /* 1644bafe5a31SPaolo Pisati * Main interrupt handling body. 1645bafe5a31SPaolo Pisati * 1646bafe5a31SPaolo Pisati * Input: 1647bafe5a31SPaolo Pisati * o ie: the event connected to this interrupt. 1648bafe5a31SPaolo Pisati * o frame: some archs (i.e. i386) pass a frame to some. 1649bafe5a31SPaolo Pisati * handlers as their main argument. 1650bafe5a31SPaolo Pisati * Return value: 1651bafe5a31SPaolo Pisati * o 0: everything ok. 1652bafe5a31SPaolo Pisati * o EINVAL: stray interrupt. 1653bafe5a31SPaolo Pisati */ 1654bafe5a31SPaolo Pisati int 1655bafe5a31SPaolo Pisati intr_event_handle(struct intr_event *ie, struct trapframe *frame) 1656bafe5a31SPaolo Pisati { 1657bafe5a31SPaolo Pisati struct intr_thread *ithd; 16581f255bd3SAlexander Motin struct trapframe *oldframe; 1659bafe5a31SPaolo Pisati struct thread *td; 1660bafe5a31SPaolo Pisati int thread; 1661bafe5a31SPaolo Pisati 1662bafe5a31SPaolo Pisati ithd = NULL; 1663bafe5a31SPaolo Pisati td = curthread; 1664bafe5a31SPaolo Pisati 1665bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 1666bafe5a31SPaolo Pisati return (EINVAL); 1667bafe5a31SPaolo Pisati 1668bafe5a31SPaolo Pisati td->td_intr_nesting_level++; 1669bafe5a31SPaolo Pisati thread = 0; 1670bafe5a31SPaolo Pisati critical_enter(); 16711f255bd3SAlexander Motin oldframe = td->td_intr_frame; 16721f255bd3SAlexander Motin td->td_intr_frame = frame; 1673bafe5a31SPaolo Pisati thread = intr_filter_loop(ie, frame, &ithd); 1674bafe5a31SPaolo Pisati if (thread & FILTER_HANDLED) { 16751ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 16761ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 1677bafe5a31SPaolo Pisati } else { 16781ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 16791ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 1680bafe5a31SPaolo Pisati } 16811f255bd3SAlexander Motin td->td_intr_frame = oldframe; 1682bafe5a31SPaolo Pisati critical_exit(); 1683bafe5a31SPaolo Pisati 1684bafe5a31SPaolo Pisati /* Interrupt storm logic */ 1685bafe5a31SPaolo Pisati if (thread & FILTER_STRAY) { 1686bafe5a31SPaolo Pisati ie->ie_count++; 1687bafe5a31SPaolo Pisati if (ie->ie_count < intr_storm_threshold) 1688bafe5a31SPaolo Pisati printf("Interrupt stray detection not present\n"); 1689bafe5a31SPaolo Pisati } 1690bafe5a31SPaolo Pisati 1691bafe5a31SPaolo Pisati /* Schedule an ithread if needed. */ 1692bafe5a31SPaolo Pisati if (thread & FILTER_SCHEDULE_THREAD) { 1693bafe5a31SPaolo Pisati if (intr_event_schedule_thread(ie, ithd) != 0) 1694bafe5a31SPaolo Pisati panic("%s: impossible stray interrupt", __func__); 1695bafe5a31SPaolo Pisati } 1696bafe5a31SPaolo Pisati td->td_intr_nesting_level--; 1697bafe5a31SPaolo Pisati return (0); 1698bafe5a31SPaolo Pisati } 1699bafe5a31SPaolo Pisati #endif 17001931cf94SJohn Baldwin 17018b201c42SJohn Baldwin #ifdef DDB 17028b201c42SJohn Baldwin /* 17038b201c42SJohn Baldwin * Dump details about an interrupt handler 17048b201c42SJohn Baldwin */ 17058b201c42SJohn Baldwin static void 1706e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 17078b201c42SJohn Baldwin { 17088b201c42SJohn Baldwin int comma; 17098b201c42SJohn Baldwin 17108b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 17118b201c42SJohn Baldwin switch (ih->ih_pri) { 17128b201c42SJohn Baldwin case PI_REALTIME: 17138b201c42SJohn Baldwin db_printf("CLK "); 17148b201c42SJohn Baldwin break; 17158b201c42SJohn Baldwin case PI_AV: 17168b201c42SJohn Baldwin db_printf("AV "); 17178b201c42SJohn Baldwin break; 1718d3305205SJohn Baldwin case PI_TTY: 17198b201c42SJohn Baldwin db_printf("TTY "); 17208b201c42SJohn Baldwin break; 17218b201c42SJohn Baldwin case PI_NET: 17228b201c42SJohn Baldwin db_printf("NET "); 17238b201c42SJohn Baldwin break; 17248b201c42SJohn Baldwin case PI_DISK: 17258b201c42SJohn Baldwin db_printf("DISK"); 17268b201c42SJohn Baldwin break; 17278b201c42SJohn Baldwin case PI_DULL: 17288b201c42SJohn Baldwin db_printf("DULL"); 17298b201c42SJohn Baldwin break; 17308b201c42SJohn Baldwin default: 17318b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 17328b201c42SJohn Baldwin db_printf("SWI "); 17338b201c42SJohn Baldwin else 17348b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 17358b201c42SJohn Baldwin break; 17368b201c42SJohn Baldwin } 17378b201c42SJohn Baldwin db_printf(" "); 1738b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1739b887a155SKonstantin Belousov db_printf("[F]"); 1740b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1741b887a155SKonstantin Belousov } 1742b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1743b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1744b887a155SKonstantin Belousov db_printf(","); 1745b887a155SKonstantin Belousov db_printf("[H]"); 17468b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1747b887a155SKonstantin Belousov } 17488b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 17498b201c42SJohn Baldwin if (ih->ih_need || 1750ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 17518b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 17528b201c42SJohn Baldwin db_printf(" {"); 17538b201c42SJohn Baldwin comma = 0; 17548b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 17558b201c42SJohn Baldwin if (comma) 17568b201c42SJohn Baldwin db_printf(", "); 17578b201c42SJohn Baldwin db_printf("EXCL"); 17588b201c42SJohn Baldwin comma = 1; 17598b201c42SJohn Baldwin } 17608b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 17618b201c42SJohn Baldwin if (comma) 17628b201c42SJohn Baldwin db_printf(", "); 17638b201c42SJohn Baldwin db_printf("ENTROPY"); 17648b201c42SJohn Baldwin comma = 1; 17658b201c42SJohn Baldwin } 17668b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 17678b201c42SJohn Baldwin if (comma) 17688b201c42SJohn Baldwin db_printf(", "); 17698b201c42SJohn Baldwin db_printf("DEAD"); 17708b201c42SJohn Baldwin comma = 1; 17718b201c42SJohn Baldwin } 17728b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 17738b201c42SJohn Baldwin if (comma) 17748b201c42SJohn Baldwin db_printf(", "); 17758b201c42SJohn Baldwin db_printf("MPSAFE"); 17768b201c42SJohn Baldwin comma = 1; 17778b201c42SJohn Baldwin } 17788b201c42SJohn Baldwin if (ih->ih_need) { 17798b201c42SJohn Baldwin if (comma) 17808b201c42SJohn Baldwin db_printf(", "); 17818b201c42SJohn Baldwin db_printf("NEED"); 17828b201c42SJohn Baldwin } 17838b201c42SJohn Baldwin db_printf("}"); 17848b201c42SJohn Baldwin } 17858b201c42SJohn Baldwin db_printf("\n"); 17868b201c42SJohn Baldwin } 17878b201c42SJohn Baldwin 17888b201c42SJohn Baldwin /* 1789e0f66ef8SJohn Baldwin * Dump details about a event. 17908b201c42SJohn Baldwin */ 17918b201c42SJohn Baldwin void 1792e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 17938b201c42SJohn Baldwin { 1794e0f66ef8SJohn Baldwin struct intr_handler *ih; 1795e0f66ef8SJohn Baldwin struct intr_thread *it; 17968b201c42SJohn Baldwin int comma; 17978b201c42SJohn Baldwin 1798e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1799e0f66ef8SJohn Baldwin it = ie->ie_thread; 1800e0f66ef8SJohn Baldwin if (it != NULL) 1801e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1802e0f66ef8SJohn Baldwin else 1803e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1804e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1805e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 18068b201c42SJohn Baldwin db_printf(" {"); 18078b201c42SJohn Baldwin comma = 0; 1808e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 18098b201c42SJohn Baldwin db_printf("SOFT"); 18108b201c42SJohn Baldwin comma = 1; 18118b201c42SJohn Baldwin } 1812e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 18138b201c42SJohn Baldwin if (comma) 18148b201c42SJohn Baldwin db_printf(", "); 18158b201c42SJohn Baldwin db_printf("ENTROPY"); 18168b201c42SJohn Baldwin comma = 1; 18178b201c42SJohn Baldwin } 1818e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 18198b201c42SJohn Baldwin if (comma) 18208b201c42SJohn Baldwin db_printf(", "); 1821e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 18228b201c42SJohn Baldwin comma = 1; 18238b201c42SJohn Baldwin } 1824e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 18258b201c42SJohn Baldwin if (comma) 18268b201c42SJohn Baldwin db_printf(", "); 18278b201c42SJohn Baldwin db_printf("NEED"); 18288b201c42SJohn Baldwin } 18298b201c42SJohn Baldwin db_printf("}"); 18308b201c42SJohn Baldwin } 18318b201c42SJohn Baldwin db_printf("\n"); 18328b201c42SJohn Baldwin 18338b201c42SJohn Baldwin if (handlers) 1834e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 18358b201c42SJohn Baldwin db_dump_intrhand(ih); 18368b201c42SJohn Baldwin } 1837e0f66ef8SJohn Baldwin 1838e0f66ef8SJohn Baldwin /* 1839e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1840e0f66ef8SJohn Baldwin */ 1841e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1842e0f66ef8SJohn Baldwin { 1843e0f66ef8SJohn Baldwin struct intr_event *ie; 184419e9205aSJohn Baldwin int all, verbose; 1845e0f66ef8SJohn Baldwin 1846dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1847dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1848e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1849e0f66ef8SJohn Baldwin if (!all && TAILQ_EMPTY(&ie->ie_handlers)) 1850e0f66ef8SJohn Baldwin continue; 1851e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 185219e9205aSJohn Baldwin if (db_pager_quit) 185319e9205aSJohn Baldwin break; 1854e0f66ef8SJohn Baldwin } 1855e0f66ef8SJohn Baldwin } 18568b201c42SJohn Baldwin #endif /* DDB */ 18578b201c42SJohn Baldwin 1858b4151f71SJohn Baldwin /* 18598088699fSJohn Baldwin * Start standard software interrupt threads 18601931cf94SJohn Baldwin */ 18611931cf94SJohn Baldwin static void 1862b4151f71SJohn Baldwin start_softintr(void *dummy) 18631931cf94SJohn Baldwin { 1864b4151f71SJohn Baldwin 18658d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 18668d809d50SJeff Roberson panic("died while creating vm swi ithread"); 18671931cf94SJohn Baldwin } 1868237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1869237fdd78SRobert Watson NULL); 18701931cf94SJohn Baldwin 1871d279178dSThomas Moestl /* 1872d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1873d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1874d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1875d279178dSThomas Moestl * independent. 1876d279178dSThomas Moestl * 1877d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1878d279178dSThomas Moestl * calculate things at run time. 1879d279178dSThomas Moestl */ 1880d279178dSThomas Moestl static int 1881d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1882d279178dSThomas Moestl { 1883521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1884d279178dSThomas Moestl } 1885d279178dSThomas Moestl 1886d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1887d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1888d279178dSThomas Moestl 1889d279178dSThomas Moestl static int 1890d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1891d279178dSThomas Moestl { 189285729c2cSJuli Mallett #ifdef SCTL_MASK32 189385729c2cSJuli Mallett uint32_t *intrcnt32; 189485729c2cSJuli Mallett unsigned i; 189585729c2cSJuli Mallett int error; 189685729c2cSJuli Mallett 189785729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 189885729c2cSJuli Mallett if (!req->oldptr) 189985729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 190085729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 190185729c2cSJuli Mallett if (intrcnt32 == NULL) 190285729c2cSJuli Mallett return (ENOMEM); 190385729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 190485729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 190585729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 190685729c2cSJuli Mallett free(intrcnt32, M_TEMP); 190785729c2cSJuli Mallett return (error); 190885729c2cSJuli Mallett } 190985729c2cSJuli Mallett #endif 1910521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1911d279178dSThomas Moestl } 1912d279178dSThomas Moestl 1913d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1914d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 19158b201c42SJohn Baldwin 19168b201c42SJohn Baldwin #ifdef DDB 19178b201c42SJohn Baldwin /* 19188b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 19198b201c42SJohn Baldwin */ 19208b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 19218b201c42SJohn Baldwin { 19228b201c42SJohn Baldwin u_long *i; 19238b201c42SJohn Baldwin char *cp; 1924521ea19dSAttilio Rao u_int j; 19258b201c42SJohn Baldwin 19268b201c42SJohn Baldwin cp = intrnames; 1927521ea19dSAttilio Rao j = 0; 1928521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1929521ea19dSAttilio Rao i++, j++) { 19308b201c42SJohn Baldwin if (*cp == '\0') 19318b201c42SJohn Baldwin break; 19328b201c42SJohn Baldwin if (*i != 0) 19338b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 19348b201c42SJohn Baldwin cp += strlen(cp) + 1; 19358b201c42SJohn Baldwin } 19368b201c42SJohn Baldwin } 19378b201c42SJohn Baldwin #endif 1938