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" 318b201c42SJohn Baldwin 321c5bb3eaSPeter Wemm #include <sys/param.h> 339a94c9c5SJohn Baldwin #include <sys/bus.h> 34c11110eaSAlfred Perlstein #include <sys/conf.h> 359b33b154SJeff Roberson #include <sys/cpuset.h> 369a94c9c5SJohn Baldwin #include <sys/rtprio.h> 37425f9fdaSStefan Eßer #include <sys/systm.h> 3868352337SDoug Rabson #include <sys/interrupt.h> 391931cf94SJohn Baldwin #include <sys/kernel.h> 401931cf94SJohn Baldwin #include <sys/kthread.h> 411931cf94SJohn Baldwin #include <sys/ktr.h> 4205b2c96fSBruce Evans #include <sys/limits.h> 43f34fa851SJohn Baldwin #include <sys/lock.h> 441931cf94SJohn Baldwin #include <sys/malloc.h> 4535e0e5b3SJohn Baldwin #include <sys/mutex.h> 46cebc7fb1SJohn Baldwin #include <sys/priv.h> 471931cf94SJohn Baldwin #include <sys/proc.h> 483e5da754SJohn Baldwin #include <sys/random.h> 49b4151f71SJohn Baldwin #include <sys/resourcevar.h> 5063710c4dSJohn Baldwin #include <sys/sched.h> 51eaf86d16SJohn Baldwin #include <sys/smp.h> 52d279178dSThomas Moestl #include <sys/sysctl.h> 536205924aSKip Macy #include <sys/syslog.h> 541931cf94SJohn Baldwin #include <sys/unistd.h> 551931cf94SJohn Baldwin #include <sys/vmmeter.h> 561931cf94SJohn Baldwin #include <machine/atomic.h> 571931cf94SJohn Baldwin #include <machine/cpu.h> 588088699fSJohn Baldwin #include <machine/md_var.h> 59b4151f71SJohn Baldwin #include <machine/stdarg.h> 608b201c42SJohn Baldwin #ifdef DDB 618b201c42SJohn Baldwin #include <ddb/ddb.h> 628b201c42SJohn Baldwin #include <ddb/db_sym.h> 638b201c42SJohn Baldwin #endif 64425f9fdaSStefan Eßer 65e0f66ef8SJohn Baldwin /* 66e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 67e0f66ef8SJohn Baldwin */ 68e0f66ef8SJohn Baldwin struct intr_thread { 69e0f66ef8SJohn Baldwin struct intr_event *it_event; 70e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 71e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 72e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 733e5da754SJohn Baldwin }; 743e5da754SJohn Baldwin 75e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 76e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 77e0f66ef8SJohn Baldwin 78e0f66ef8SJohn Baldwin struct intr_entropy { 79e0f66ef8SJohn Baldwin struct thread *td; 80e0f66ef8SJohn Baldwin uintptr_t event; 81e0f66ef8SJohn Baldwin }; 82e0f66ef8SJohn Baldwin 83e0f66ef8SJohn Baldwin struct intr_event *clk_intr_event; 84e0f66ef8SJohn Baldwin struct intr_event *tty_intr_event; 857b1fe905SBruce Evans void *vm_ih; 867ab24ea3SJulian Elischer struct proc *intrproc; 871931cf94SJohn Baldwin 88b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 89b4151f71SJohn Baldwin 900ae62c18SNate Lawson static int intr_storm_threshold = 1000; 917870c3c6SJohn Baldwin TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold); 927870c3c6SJohn Baldwin SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW, 937870c3c6SJohn Baldwin &intr_storm_threshold, 0, 947b1fe905SBruce Evans "Number of consecutive interrupts before storm protection is enabled"); 95e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 96e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 979b33b154SJeff Roberson static struct mtx event_lock; 989b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 997b1fe905SBruce Evans 100e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 101bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1021ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie, 1031ee1b687SJohn Baldwin struct intr_thread *ithd); 1041ee1b687SJohn Baldwin static int intr_filter_loop(struct intr_event *ie, 1051ee1b687SJohn Baldwin struct trapframe *frame, struct intr_thread **ithd); 106bafe5a31SPaolo Pisati static struct intr_thread *ithread_create(const char *name, 107bafe5a31SPaolo Pisati struct intr_handler *ih); 108bafe5a31SPaolo Pisati #else 1091ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie); 110e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 111bafe5a31SPaolo Pisati #endif 112e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 113bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 114bafe5a31SPaolo Pisati struct intr_event *ie); 115bafe5a31SPaolo Pisati #ifdef INTR_FILTER 116bafe5a31SPaolo Pisati static void priv_ithread_execute_handler(struct proc *p, 117bafe5a31SPaolo Pisati struct intr_handler *ih); 118bafe5a31SPaolo Pisati #endif 1197b1fe905SBruce Evans static void ithread_loop(void *); 120e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1217b1fe905SBruce Evans static void start_softintr(void *); 1227870c3c6SJohn Baldwin 123bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 124b4151f71SJohn Baldwin u_char 125e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1269a94c9c5SJohn Baldwin { 127b4151f71SJohn Baldwin u_char pri; 1289a94c9c5SJohn Baldwin 129b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1305a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1319a94c9c5SJohn Baldwin switch (flags) { 132b4151f71SJohn Baldwin case INTR_TYPE_TTY: 133*d3305205SJohn Baldwin pri = PI_TTY; 1349a94c9c5SJohn Baldwin break; 1359a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1369a94c9c5SJohn Baldwin pri = PI_DISK; 1379a94c9c5SJohn Baldwin break; 1389a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1399a94c9c5SJohn Baldwin pri = PI_NET; 1409a94c9c5SJohn Baldwin break; 1419a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 142*d3305205SJohn Baldwin pri = PI_DISK; 1439a94c9c5SJohn Baldwin break; 144*d3305205SJohn Baldwin case INTR_TYPE_AV: 1455a280d9cSPeter Wemm pri = PI_AV; 1465a280d9cSPeter Wemm break; 147b4151f71SJohn Baldwin case INTR_TYPE_CLK: 148b4151f71SJohn Baldwin pri = PI_REALTIME; 149b4151f71SJohn Baldwin break; 1509a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1519a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1529a94c9c5SJohn Baldwin break; 1539a94c9c5SJohn Baldwin default: 154b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 155e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1569a94c9c5SJohn Baldwin } 1579a94c9c5SJohn Baldwin 1589a94c9c5SJohn Baldwin return pri; 1599a94c9c5SJohn Baldwin } 1609a94c9c5SJohn Baldwin 161b4151f71SJohn Baldwin /* 162e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 163b4151f71SJohn Baldwin */ 164b4151f71SJohn Baldwin static void 165e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 166b4151f71SJohn Baldwin { 167e0f66ef8SJohn Baldwin struct intr_event *ie; 168b40ce416SJulian Elischer struct thread *td; 169e0f66ef8SJohn Baldwin u_char pri; 1708088699fSJohn Baldwin 171e0f66ef8SJohn Baldwin ie = ithd->it_event; 172e0f66ef8SJohn Baldwin td = ithd->it_thread; 173b4151f71SJohn Baldwin 174e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 175e0f66ef8SJohn Baldwin if (TAILQ_EMPTY(&ie->ie_handlers)) 176e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 177e0f66ef8SJohn Baldwin else 178e0f66ef8SJohn Baldwin pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri; 179e80fb434SRobert Drehmel 180e0f66ef8SJohn Baldwin /* Update name and priority. */ 1817ab24ea3SJulian Elischer strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 182982d11f8SJeff Roberson thread_lock(td); 183e0f66ef8SJohn Baldwin sched_prio(td, pri); 184982d11f8SJeff Roberson thread_unlock(td); 185b4151f71SJohn Baldwin } 186e0f66ef8SJohn Baldwin 187e0f66ef8SJohn Baldwin /* 188e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 189e0f66ef8SJohn Baldwin */ 190e0f66ef8SJohn Baldwin static void 191e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 192e0f66ef8SJohn Baldwin { 193e0f66ef8SJohn Baldwin struct intr_handler *ih; 194e0f66ef8SJohn Baldwin char *last; 195e0f66ef8SJohn Baldwin int missed, space; 196e0f66ef8SJohn Baldwin 197e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 198e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 199e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 200e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ENTROPY; 2010811d60aSJohn Baldwin missed = 0; 202e0f66ef8SJohn Baldwin space = 1; 203e0f66ef8SJohn Baldwin 204e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 205e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 206e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 207e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 208e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 209e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 210e0f66ef8SJohn Baldwin space = 0; 2110811d60aSJohn Baldwin } else 2120811d60aSJohn Baldwin missed++; 2130811d60aSJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 214e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ENTROPY; 2150811d60aSJohn Baldwin } 216e0f66ef8SJohn Baldwin 217e0f66ef8SJohn Baldwin /* 218e0f66ef8SJohn Baldwin * If the handler names were too long, add +'s to indicate missing 219e0f66ef8SJohn Baldwin * names. If we run out of room and still have +'s to add, change 220e0f66ef8SJohn Baldwin * the last character from a + to a *. 221e0f66ef8SJohn Baldwin */ 222e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2230811d60aSJohn Baldwin while (missed-- > 0) { 224e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 225e0f66ef8SJohn Baldwin if (*last == '+') { 226e0f66ef8SJohn Baldwin *last = '*'; 227e0f66ef8SJohn Baldwin break; 228b4151f71SJohn Baldwin } else 229e0f66ef8SJohn Baldwin *last = '+'; 230e0f66ef8SJohn Baldwin } else if (space) { 231e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 232e0f66ef8SJohn Baldwin space = 0; 233e0f66ef8SJohn Baldwin } else 234e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 235b4151f71SJohn Baldwin } 236e0f66ef8SJohn Baldwin 237e0f66ef8SJohn Baldwin /* 238e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 239e0f66ef8SJohn Baldwin * name. 240e0f66ef8SJohn Baldwin */ 241e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 242e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 243e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 244b4151f71SJohn Baldwin } 245b4151f71SJohn Baldwin 246b4151f71SJohn Baldwin int 2479b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2481ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 2491ee1b687SJohn Baldwin void (*post_filter)(void *), int (*assign_cpu)(void *, u_char), 2501ee1b687SJohn Baldwin const char *fmt, ...) 251bafe5a31SPaolo Pisati { 252bafe5a31SPaolo Pisati struct intr_event *ie; 253bafe5a31SPaolo Pisati va_list ap; 254bafe5a31SPaolo Pisati 255bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 256bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 257bafe5a31SPaolo Pisati return (EINVAL); 258bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 259bafe5a31SPaolo Pisati ie->ie_source = source; 2601ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2611ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2621ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2636d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 264bafe5a31SPaolo Pisati ie->ie_flags = flags; 2659b33b154SJeff Roberson ie->ie_irq = irq; 266eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 267bafe5a31SPaolo Pisati TAILQ_INIT(&ie->ie_handlers); 268bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 269bafe5a31SPaolo Pisati 270bafe5a31SPaolo Pisati va_start(ap, fmt); 271bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 272bafe5a31SPaolo Pisati va_end(ap); 273bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2749b33b154SJeff Roberson mtx_lock(&event_lock); 275bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2769b33b154SJeff Roberson mtx_unlock(&event_lock); 277bafe5a31SPaolo Pisati if (event != NULL) 278bafe5a31SPaolo Pisati *event = ie; 279bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 280bafe5a31SPaolo Pisati return (0); 281bafe5a31SPaolo Pisati } 282b4151f71SJohn Baldwin 283eaf86d16SJohn Baldwin /* 284eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 285eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 286eaf86d16SJohn Baldwin * platforms this request will fail. For supported platforms, any 287eaf86d16SJohn Baldwin * associated ithreads as well as the primary interrupt context will 288eaf86d16SJohn Baldwin * be bound to the specificed CPU. Using a cpu id of NOCPU unbinds 289eaf86d16SJohn Baldwin * the interrupt event. 290eaf86d16SJohn Baldwin */ 291eaf86d16SJohn Baldwin int 292eaf86d16SJohn Baldwin intr_event_bind(struct intr_event *ie, u_char cpu) 293eaf86d16SJohn Baldwin { 2949b33b154SJeff Roberson cpuset_t mask; 2959b33b154SJeff Roberson lwpid_t id; 296eaf86d16SJohn Baldwin int error; 297eaf86d16SJohn Baldwin 298eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 299eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 300eaf86d16SJohn Baldwin return (EINVAL); 301eaf86d16SJohn Baldwin 302eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 303eaf86d16SJohn Baldwin return (EOPNOTSUPP); 304cebc7fb1SJohn Baldwin 305cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 306cebc7fb1SJohn Baldwin if (error) 307cebc7fb1SJohn Baldwin return (error); 308cebc7fb1SJohn Baldwin 3099b33b154SJeff Roberson /* 310cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 311cebc7fb1SJohn Baldwin * permissions, etc. 3129b33b154SJeff Roberson */ 313eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3149b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3159b33b154SJeff Roberson CPU_ZERO(&mask); 3169b33b154SJeff Roberson if (cpu == NOCPU) 3179b33b154SJeff Roberson CPU_COPY(cpuset_root, &mask); 3189b33b154SJeff Roberson else 3199b33b154SJeff Roberson CPU_SET(cpu, &mask); 3209b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 321eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 3229b33b154SJeff Roberson error = cpuset_setthread(id, &mask); 3239b33b154SJeff Roberson if (error) 3249b33b154SJeff Roberson return (error); 3259b33b154SJeff Roberson } else 326eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 327eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 328cebc7fb1SJohn Baldwin if (error) { 329cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 330cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 331cebc7fb1SJohn Baldwin CPU_ZERO(&mask); 332cebc7fb1SJohn Baldwin if (ie->ie_cpu == NOCPU) 333cebc7fb1SJohn Baldwin CPU_COPY(cpuset_root, &mask); 334cebc7fb1SJohn Baldwin else 335cebc7fb1SJohn Baldwin CPU_SET(cpu, &mask); 336cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 337cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 338cebc7fb1SJohn Baldwin (void)cpuset_setthread(id, &mask); 339cebc7fb1SJohn Baldwin } else 340cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 341eaf86d16SJohn Baldwin return (error); 342cebc7fb1SJohn Baldwin } 343cebc7fb1SJohn Baldwin 344eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 345eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3469b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 3479b33b154SJeff Roberson 3489b33b154SJeff Roberson return (error); 3499b33b154SJeff Roberson } 3509b33b154SJeff Roberson 3519b33b154SJeff Roberson static struct intr_event * 3529b33b154SJeff Roberson intr_lookup(int irq) 3539b33b154SJeff Roberson { 3549b33b154SJeff Roberson struct intr_event *ie; 3559b33b154SJeff Roberson 3569b33b154SJeff Roberson mtx_lock(&event_lock); 3579b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 3589b33b154SJeff Roberson if (ie->ie_irq == irq && 3599b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 3609b33b154SJeff Roberson TAILQ_FIRST(&ie->ie_handlers) != NULL) 3619b33b154SJeff Roberson break; 3629b33b154SJeff Roberson mtx_unlock(&event_lock); 3639b33b154SJeff Roberson return (ie); 3649b33b154SJeff Roberson } 3659b33b154SJeff Roberson 3669b33b154SJeff Roberson int 3679b33b154SJeff Roberson intr_setaffinity(int irq, void *m) 3689b33b154SJeff Roberson { 3699b33b154SJeff Roberson struct intr_event *ie; 3709b33b154SJeff Roberson cpuset_t *mask; 3719b33b154SJeff Roberson u_char cpu; 3729b33b154SJeff Roberson int n; 3739b33b154SJeff Roberson 3749b33b154SJeff Roberson mask = m; 3759b33b154SJeff Roberson cpu = NOCPU; 3769b33b154SJeff Roberson /* 3779b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 3789b33b154SJeff Roberson * only one cpu is in the set. 3799b33b154SJeff Roberson */ 3809b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 3819b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 3829b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 3839b33b154SJeff Roberson continue; 3849b33b154SJeff Roberson if (cpu != NOCPU) 3859b33b154SJeff Roberson return (EINVAL); 3869b33b154SJeff Roberson cpu = (u_char)n; 3879b33b154SJeff Roberson } 3889b33b154SJeff Roberson } 3899b33b154SJeff Roberson ie = intr_lookup(irq); 3909b33b154SJeff Roberson if (ie == NULL) 3919b33b154SJeff Roberson return (ESRCH); 3929bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 3939b33b154SJeff Roberson } 3949b33b154SJeff Roberson 3959b33b154SJeff Roberson int 3969b33b154SJeff Roberson intr_getaffinity(int irq, void *m) 3979b33b154SJeff Roberson { 3989b33b154SJeff Roberson struct intr_event *ie; 3999b33b154SJeff Roberson cpuset_t *mask; 4009b33b154SJeff Roberson 4019b33b154SJeff Roberson mask = m; 4029b33b154SJeff Roberson ie = intr_lookup(irq); 4039b33b154SJeff Roberson if (ie == NULL) 4049b33b154SJeff Roberson return (ESRCH); 4059b33b154SJeff Roberson CPU_ZERO(mask); 4069b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4079b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4089b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4099b33b154SJeff Roberson else 4109b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 411eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 412eaf86d16SJohn Baldwin return (0); 413eaf86d16SJohn Baldwin } 414eaf86d16SJohn Baldwin 415b4151f71SJohn Baldwin int 416e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 417b4151f71SJohn Baldwin { 418b4151f71SJohn Baldwin 4199b33b154SJeff Roberson mtx_lock(&event_lock); 420e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 421e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 422e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4239b33b154SJeff Roberson mtx_unlock(&event_lock); 424e0f66ef8SJohn Baldwin return (EBUSY); 4254d29cb2dSJohn Baldwin } 426e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 4279477358dSJohn Baldwin #ifndef notyet 4289477358dSJohn Baldwin if (ie->ie_thread != NULL) { 4299477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 4309477358dSJohn Baldwin ie->ie_thread = NULL; 4319477358dSJohn Baldwin } 4329477358dSJohn Baldwin #endif 433e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4349b33b154SJeff Roberson mtx_unlock(&event_lock); 435e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 436e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 437e0f66ef8SJohn Baldwin return (0); 438e0f66ef8SJohn Baldwin } 439e0f66ef8SJohn Baldwin 440bafe5a31SPaolo Pisati #ifndef INTR_FILTER 441e0f66ef8SJohn Baldwin static struct intr_thread * 442e0f66ef8SJohn Baldwin ithread_create(const char *name) 443e0f66ef8SJohn Baldwin { 444e0f66ef8SJohn Baldwin struct intr_thread *ithd; 445e0f66ef8SJohn Baldwin struct thread *td; 446e0f66ef8SJohn Baldwin int error; 447e0f66ef8SJohn Baldwin 448e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 449e0f66ef8SJohn Baldwin 4507ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 4517ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 4529ef95d01SJulian Elischer 0, "intr", "%s", name); 453e0f66ef8SJohn Baldwin if (error) 4543745c395SJulian Elischer panic("kproc_create() failed with %d", error); 455982d11f8SJeff Roberson thread_lock(td); 456ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 457e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 458982d11f8SJeff Roberson thread_unlock(td); 459e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 460e0f66ef8SJohn Baldwin ithd->it_thread = td; 461e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 462e0f66ef8SJohn Baldwin return (ithd); 463e0f66ef8SJohn Baldwin } 464bafe5a31SPaolo Pisati #else 465bafe5a31SPaolo Pisati static struct intr_thread * 466bafe5a31SPaolo Pisati ithread_create(const char *name, struct intr_handler *ih) 467bafe5a31SPaolo Pisati { 468bafe5a31SPaolo Pisati struct intr_thread *ithd; 469bafe5a31SPaolo Pisati struct thread *td; 470bafe5a31SPaolo Pisati int error; 471bafe5a31SPaolo Pisati 472bafe5a31SPaolo Pisati ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 473bafe5a31SPaolo Pisati 474539976ffSJulian Elischer error = kproc_kthread_add(ithread_loop, ih, &intrproc, 4757ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 4769ef95d01SJulian Elischer 0, "intr", "%s", name); 477bafe5a31SPaolo Pisati if (error) 4783745c395SJulian Elischer panic("kproc_create() failed with %d", error); 479982d11f8SJeff Roberson thread_lock(td); 480bafe5a31SPaolo Pisati sched_class(td, PRI_ITHD); 481bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 482982d11f8SJeff Roberson thread_unlock(td); 483bafe5a31SPaolo Pisati td->td_pflags |= TDP_ITHREAD; 484bafe5a31SPaolo Pisati ithd->it_thread = td; 485bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, name); 486bafe5a31SPaolo Pisati return (ithd); 487bafe5a31SPaolo Pisati } 488bafe5a31SPaolo Pisati #endif 489e0f66ef8SJohn Baldwin 490e0f66ef8SJohn Baldwin static void 491e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 492e0f66ef8SJohn Baldwin { 493e0f66ef8SJohn Baldwin struct thread *td; 494e0f66ef8SJohn Baldwin 495bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 496e0f66ef8SJohn Baldwin td = ithread->it_thread; 497982d11f8SJeff Roberson thread_lock(td); 498e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 49971fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 50071fad9fdSJulian Elischer TD_CLR_IWAIT(td); 501f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 502b4151f71SJohn Baldwin } 503982d11f8SJeff Roberson thread_unlock(td); 504b4151f71SJohn Baldwin } 505b4151f71SJohn Baldwin 506bafe5a31SPaolo Pisati #ifndef INTR_FILTER 507b4151f71SJohn Baldwin int 508e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 509ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 510ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 511b4151f71SJohn Baldwin { 512e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 513e0f66ef8SJohn Baldwin struct intr_thread *it; 514b4151f71SJohn Baldwin 515ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 516b4151f71SJohn Baldwin return (EINVAL); 517b4151f71SJohn Baldwin 518e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 519e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 520ef544f63SPaolo Pisati ih->ih_filter = filter; 521b4151f71SJohn Baldwin ih->ih_handler = handler; 522b4151f71SJohn Baldwin ih->ih_argument = arg; 52337b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 524e0f66ef8SJohn Baldwin ih->ih_event = ie; 525b4151f71SJohn Baldwin ih->ih_pri = pri; 526ef544f63SPaolo Pisati if (flags & INTR_EXCL) 527b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 528b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 529b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 530b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 531b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 532b4151f71SJohn Baldwin 533e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 534e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 535e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 536e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 537e0f66ef8SJohn Baldwin (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 538e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 539b4151f71SJohn Baldwin free(ih, M_ITHREAD); 540b4151f71SJohn Baldwin return (EINVAL); 541b4151f71SJohn Baldwin } 542e0f66ef8SJohn Baldwin } 543e0f66ef8SJohn Baldwin 544e0f66ef8SJohn Baldwin /* Add the new handler to the event in priority order. */ 545e0f66ef8SJohn Baldwin TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 546e0f66ef8SJohn Baldwin if (temp_ih->ih_pri > ih->ih_pri) 547e0f66ef8SJohn Baldwin break; 548e0f66ef8SJohn Baldwin } 549e0f66ef8SJohn Baldwin if (temp_ih == NULL) 550e0f66ef8SJohn Baldwin TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 551e0f66ef8SJohn Baldwin else 552e0f66ef8SJohn Baldwin TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 553e0f66ef8SJohn Baldwin intr_event_update(ie); 554e0f66ef8SJohn Baldwin 555e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 556ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 557e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 5580f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 559e0f66ef8SJohn Baldwin else { 560e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 561e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 562e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 563e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 564e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 565e0f66ef8SJohn Baldwin ie->ie_thread = it; 566e0f66ef8SJohn Baldwin it->it_event = ie; 567e0f66ef8SJohn Baldwin ithread_update(it); 568e0f66ef8SJohn Baldwin wakeup(ie); 569e0f66ef8SJohn Baldwin } 570e0f66ef8SJohn Baldwin } 571e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 572e0f66ef8SJohn Baldwin ie->ie_name); 573e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 574e0f66ef8SJohn Baldwin 575e0f66ef8SJohn Baldwin if (cookiep != NULL) 576e0f66ef8SJohn Baldwin *cookiep = ih; 577e0f66ef8SJohn Baldwin return (0); 578e0f66ef8SJohn Baldwin } 579bafe5a31SPaolo Pisati #else 580bafe5a31SPaolo Pisati int 581bafe5a31SPaolo Pisati intr_event_add_handler(struct intr_event *ie, const char *name, 582bafe5a31SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 583bafe5a31SPaolo Pisati enum intr_type flags, void **cookiep) 584bafe5a31SPaolo Pisati { 585bafe5a31SPaolo Pisati struct intr_handler *ih, *temp_ih; 586bafe5a31SPaolo Pisati struct intr_thread *it; 587bafe5a31SPaolo Pisati 588bafe5a31SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 589bafe5a31SPaolo Pisati return (EINVAL); 590bafe5a31SPaolo Pisati 591bafe5a31SPaolo Pisati /* Allocate and populate an interrupt handler structure. */ 592bafe5a31SPaolo Pisati ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 593bafe5a31SPaolo Pisati ih->ih_filter = filter; 594bafe5a31SPaolo Pisati ih->ih_handler = handler; 595bafe5a31SPaolo Pisati ih->ih_argument = arg; 59637b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 597bafe5a31SPaolo Pisati ih->ih_event = ie; 598bafe5a31SPaolo Pisati ih->ih_pri = pri; 599bafe5a31SPaolo Pisati if (flags & INTR_EXCL) 600bafe5a31SPaolo Pisati ih->ih_flags = IH_EXCLUSIVE; 601bafe5a31SPaolo Pisati if (flags & INTR_MPSAFE) 602bafe5a31SPaolo Pisati ih->ih_flags |= IH_MPSAFE; 603bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 604bafe5a31SPaolo Pisati ih->ih_flags |= IH_ENTROPY; 605bafe5a31SPaolo Pisati 606bafe5a31SPaolo Pisati /* We can only have one exclusive handler in a event. */ 607bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 608bafe5a31SPaolo Pisati if (!TAILQ_EMPTY(&ie->ie_handlers)) { 609bafe5a31SPaolo Pisati if ((flags & INTR_EXCL) || 610bafe5a31SPaolo Pisati (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 611bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 612bafe5a31SPaolo Pisati free(ih, M_ITHREAD); 613bafe5a31SPaolo Pisati return (EINVAL); 614bafe5a31SPaolo Pisati } 615bafe5a31SPaolo Pisati } 616bafe5a31SPaolo Pisati 617bafe5a31SPaolo Pisati /* Add the new handler to the event in priority order. */ 618bafe5a31SPaolo Pisati TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 619bafe5a31SPaolo Pisati if (temp_ih->ih_pri > ih->ih_pri) 620bafe5a31SPaolo Pisati break; 621bafe5a31SPaolo Pisati } 622bafe5a31SPaolo Pisati if (temp_ih == NULL) 623bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 624bafe5a31SPaolo Pisati else 625bafe5a31SPaolo Pisati TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 626bafe5a31SPaolo Pisati intr_event_update(ie); 627bafe5a31SPaolo Pisati 628bafe5a31SPaolo Pisati /* For filtered handlers, create a private ithread to run on. */ 629bafe5a31SPaolo Pisati if (filter != NULL && handler != NULL) { 630bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 631bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 632bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 633bafe5a31SPaolo Pisati it->it_event = ie; 634bafe5a31SPaolo Pisati ih->ih_thread = it; 635bafe5a31SPaolo Pisati ithread_update(it); // XXX - do we really need this?!?!? 636bafe5a31SPaolo Pisati } else { /* Create the global per-event thread if we need one. */ 637bafe5a31SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 638bafe5a31SPaolo Pisati if (ie->ie_flags & IE_ADDING_THREAD) 639bafe5a31SPaolo Pisati msleep(ie, &ie->ie_lock, 0, "ithread", 0); 640bafe5a31SPaolo Pisati else { 641bafe5a31SPaolo Pisati ie->ie_flags |= IE_ADDING_THREAD; 642bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 643bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 644bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 645bafe5a31SPaolo Pisati ie->ie_flags &= ~IE_ADDING_THREAD; 646bafe5a31SPaolo Pisati ie->ie_thread = it; 647bafe5a31SPaolo Pisati it->it_event = ie; 648bafe5a31SPaolo Pisati ithread_update(it); 649bafe5a31SPaolo Pisati wakeup(ie); 650bafe5a31SPaolo Pisati } 651bafe5a31SPaolo Pisati } 652bafe5a31SPaolo Pisati } 653bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 654bafe5a31SPaolo Pisati ie->ie_name); 655bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 656bafe5a31SPaolo Pisati 657bafe5a31SPaolo Pisati if (cookiep != NULL) 658bafe5a31SPaolo Pisati *cookiep = ih; 659bafe5a31SPaolo Pisati return (0); 660bafe5a31SPaolo Pisati } 661bafe5a31SPaolo Pisati #endif 662b4151f71SJohn Baldwin 663c3045318SJohn Baldwin /* 66437b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 66537b8ef16SJohn Baldwin * interrupt handler. 66637b8ef16SJohn Baldwin */ 66737b8ef16SJohn Baldwin int 66837b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 66937b8ef16SJohn Baldwin const char *descr) 67037b8ef16SJohn Baldwin { 67137b8ef16SJohn Baldwin struct intr_handler *ih; 67237b8ef16SJohn Baldwin size_t space; 67337b8ef16SJohn Baldwin char *start; 67437b8ef16SJohn Baldwin 67537b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 67637b8ef16SJohn Baldwin #ifdef INVARIANTS 67737b8ef16SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 67837b8ef16SJohn Baldwin if (ih == cookie) 67937b8ef16SJohn Baldwin break; 68037b8ef16SJohn Baldwin } 68137b8ef16SJohn Baldwin if (ih == NULL) { 68237b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 683d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 68437b8ef16SJohn Baldwin } 68537b8ef16SJohn Baldwin #endif 68637b8ef16SJohn Baldwin ih = cookie; 68737b8ef16SJohn Baldwin 68837b8ef16SJohn Baldwin /* 68937b8ef16SJohn Baldwin * Look for an existing description by checking for an 69037b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 69137b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 69237b8ef16SJohn Baldwin * description at that point. If one is not found, find the 69337b8ef16SJohn Baldwin * end of the name to use as the insertion point. 69437b8ef16SJohn Baldwin */ 69537b8ef16SJohn Baldwin start = index(ih->ih_name, ':'); 69637b8ef16SJohn Baldwin if (start == NULL) 69737b8ef16SJohn Baldwin start = index(ih->ih_name, 0); 69837b8ef16SJohn Baldwin 69937b8ef16SJohn Baldwin /* 70037b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 70137b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 70237b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 70337b8ef16SJohn Baldwin */ 70437b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 70537b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 70637b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 70737b8ef16SJohn Baldwin return (ENOSPC); 70837b8ef16SJohn Baldwin } 70937b8ef16SJohn Baldwin 71037b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 71137b8ef16SJohn Baldwin *start = ':'; 71237b8ef16SJohn Baldwin strcpy(start + 1, descr); 71337b8ef16SJohn Baldwin intr_event_update(ie); 71437b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 71537b8ef16SJohn Baldwin return (0); 71637b8ef16SJohn Baldwin } 71737b8ef16SJohn Baldwin 71837b8ef16SJohn Baldwin /* 719c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 720c3045318SJohn Baldwin * associated with. 721c3045318SJohn Baldwin */ 722c3045318SJohn Baldwin void * 723c3045318SJohn Baldwin intr_handler_source(void *cookie) 724c3045318SJohn Baldwin { 725c3045318SJohn Baldwin struct intr_handler *ih; 726c3045318SJohn Baldwin struct intr_event *ie; 727c3045318SJohn Baldwin 728c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 729c3045318SJohn Baldwin if (ih == NULL) 730c3045318SJohn Baldwin return (NULL); 731c3045318SJohn Baldwin ie = ih->ih_event; 732c3045318SJohn Baldwin KASSERT(ie != NULL, 733c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 734c3045318SJohn Baldwin ih->ih_name)); 735c3045318SJohn Baldwin return (ie->ie_source); 736c3045318SJohn Baldwin } 737c3045318SJohn Baldwin 738bafe5a31SPaolo Pisati #ifndef INTR_FILTER 739b4151f71SJohn Baldwin int 740e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 741b4151f71SJohn Baldwin { 742e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 743e0f66ef8SJohn Baldwin struct intr_event *ie; 744b4151f71SJohn Baldwin #ifdef INVARIANTS 745e0f66ef8SJohn Baldwin struct intr_handler *ih; 746e0f66ef8SJohn Baldwin #endif 747e0f66ef8SJohn Baldwin #ifdef notyet 748e0f66ef8SJohn Baldwin int dead; 749b4151f71SJohn Baldwin #endif 750b4151f71SJohn Baldwin 7513e5da754SJohn Baldwin if (handler == NULL) 752b4151f71SJohn Baldwin return (EINVAL); 753e0f66ef8SJohn Baldwin ie = handler->ih_event; 754e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 755e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 7563e5da754SJohn Baldwin handler->ih_name)); 757e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 75891f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 759e0f66ef8SJohn Baldwin ie->ie_name); 760b4151f71SJohn Baldwin #ifdef INVARIANTS 761e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 7623e5da754SJohn Baldwin if (ih == handler) 7633e5da754SJohn Baldwin goto ok; 764e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 765e0f66ef8SJohn Baldwin panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 766e0f66ef8SJohn Baldwin ih->ih_name, ie->ie_name); 7673e5da754SJohn Baldwin ok: 768b4151f71SJohn Baldwin #endif 769de271f01SJohn Baldwin /* 770e0f66ef8SJohn Baldwin * If there is no ithread, then just remove the handler and return. 771e0f66ef8SJohn Baldwin * XXX: Note that an INTR_FAST handler might be running on another 772e0f66ef8SJohn Baldwin * CPU! 773e0f66ef8SJohn Baldwin */ 774e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 775e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 776e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 777e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 778e0f66ef8SJohn Baldwin return (0); 779e0f66ef8SJohn Baldwin } 780e0f66ef8SJohn Baldwin 781e0f66ef8SJohn Baldwin /* 782de271f01SJohn Baldwin * If the interrupt thread is already running, then just mark this 783de271f01SJohn Baldwin * handler as being dead and let the ithread do the actual removal. 784288e351bSDon Lewis * 785288e351bSDon Lewis * During a cold boot while cold is set, msleep() does not sleep, 786288e351bSDon Lewis * so we have to remove the handler here rather than letting the 787288e351bSDon Lewis * thread do it. 788de271f01SJohn Baldwin */ 789982d11f8SJeff Roberson thread_lock(ie->ie_thread->it_thread); 790e0f66ef8SJohn Baldwin if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) { 791de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 792de271f01SJohn Baldwin 793de271f01SJohn Baldwin /* 794de271f01SJohn Baldwin * Ensure that the thread will process the handler list 795de271f01SJohn Baldwin * again and remove this handler if it has already passed 796de271f01SJohn Baldwin * it on the list. 797de271f01SJohn Baldwin */ 798e0f66ef8SJohn Baldwin ie->ie_thread->it_need = 1; 7994d29cb2dSJohn Baldwin } else 800e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 801982d11f8SJeff Roberson thread_unlock(ie->ie_thread->it_thread); 802e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8030f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 804e0f66ef8SJohn Baldwin intr_event_update(ie); 805e0f66ef8SJohn Baldwin #ifdef notyet 806e0f66ef8SJohn Baldwin /* 807e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 808e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 809e0f66ef8SJohn Baldwin * interrupt. 810e0f66ef8SJohn Baldwin */ 811e0f66ef8SJohn Baldwin dead = 1; 812e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 813e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_FAST)) { 814e0f66ef8SJohn Baldwin dead = 0; 815e0f66ef8SJohn Baldwin break; 816e0f66ef8SJohn Baldwin } 817e0f66ef8SJohn Baldwin } 818e0f66ef8SJohn Baldwin if (dead) { 819e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 820e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 821e0f66ef8SJohn Baldwin } 822e0f66ef8SJohn Baldwin #endif 823e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 824b4151f71SJohn Baldwin free(handler, M_ITHREAD); 825b4151f71SJohn Baldwin return (0); 826b4151f71SJohn Baldwin } 827b4151f71SJohn Baldwin 8281ee1b687SJohn Baldwin static int 829e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 8303e5da754SJohn Baldwin { 831e0f66ef8SJohn Baldwin struct intr_entropy entropy; 832e0f66ef8SJohn Baldwin struct intr_thread *it; 833b40ce416SJulian Elischer struct thread *td; 83404774f23SJulian Elischer struct thread *ctd; 8353e5da754SJohn Baldwin struct proc *p; 8363e5da754SJohn Baldwin 8373e5da754SJohn Baldwin /* 8383e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 8393e5da754SJohn Baldwin */ 840e0f66ef8SJohn Baldwin if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || 841e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 8423e5da754SJohn Baldwin return (EINVAL); 8433e5da754SJohn Baldwin 84404774f23SJulian Elischer ctd = curthread; 845e0f66ef8SJohn Baldwin it = ie->ie_thread; 846e0f66ef8SJohn Baldwin td = it->it_thread; 8476f40c417SRobert Watson p = td->td_proc; 848e0f66ef8SJohn Baldwin 8493e5da754SJohn Baldwin /* 8503e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 8513e5da754SJohn Baldwin * sources of entropy, then gather some. 8523e5da754SJohn Baldwin */ 853e0f66ef8SJohn Baldwin if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 8546f40c417SRobert Watson CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 8557ab24ea3SJulian Elischer p->p_pid, td->td_name); 856e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 857e0f66ef8SJohn Baldwin entropy.td = ctd; 8583e5da754SJohn Baldwin random_harvest(&entropy, sizeof(entropy), 2, 0, 8593e5da754SJohn Baldwin RANDOM_INTERRUPT); 8603e5da754SJohn Baldwin } 8613e5da754SJohn Baldwin 862e0f66ef8SJohn Baldwin KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 8633e5da754SJohn Baldwin 8643e5da754SJohn Baldwin /* 8653e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 866982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 867982d11f8SJeff Roberson * put it on the runqueue. 8683e5da754SJohn Baldwin */ 869e0f66ef8SJohn Baldwin it->it_need = 1; 870982d11f8SJeff Roberson thread_lock(td); 87171fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 872e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 8737ab24ea3SJulian Elischer td->td_name); 87471fad9fdSJulian Elischer TD_CLR_IWAIT(td); 875f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 8763e5da754SJohn Baldwin } else { 877e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 8787ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 8793e5da754SJohn Baldwin } 880982d11f8SJeff Roberson thread_unlock(td); 8813e5da754SJohn Baldwin 8823e5da754SJohn Baldwin return (0); 8833e5da754SJohn Baldwin } 884bafe5a31SPaolo Pisati #else 885bafe5a31SPaolo Pisati int 886bafe5a31SPaolo Pisati intr_event_remove_handler(void *cookie) 887bafe5a31SPaolo Pisati { 888bafe5a31SPaolo Pisati struct intr_handler *handler = (struct intr_handler *)cookie; 889bafe5a31SPaolo Pisati struct intr_event *ie; 890bafe5a31SPaolo Pisati struct intr_thread *it; 891bafe5a31SPaolo Pisati #ifdef INVARIANTS 892bafe5a31SPaolo Pisati struct intr_handler *ih; 893bafe5a31SPaolo Pisati #endif 894bafe5a31SPaolo Pisati #ifdef notyet 895bafe5a31SPaolo Pisati int dead; 896bafe5a31SPaolo Pisati #endif 897bafe5a31SPaolo Pisati 898bafe5a31SPaolo Pisati if (handler == NULL) 899bafe5a31SPaolo Pisati return (EINVAL); 900bafe5a31SPaolo Pisati ie = handler->ih_event; 901bafe5a31SPaolo Pisati KASSERT(ie != NULL, 902bafe5a31SPaolo Pisati ("interrupt handler \"%s\" has a NULL interrupt event", 903bafe5a31SPaolo Pisati handler->ih_name)); 904bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 905bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 906bafe5a31SPaolo Pisati ie->ie_name); 907bafe5a31SPaolo Pisati #ifdef INVARIANTS 908bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 909bafe5a31SPaolo Pisati if (ih == handler) 910bafe5a31SPaolo Pisati goto ok; 911bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 912bafe5a31SPaolo Pisati panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 913bafe5a31SPaolo Pisati ih->ih_name, ie->ie_name); 914bafe5a31SPaolo Pisati ok: 915bafe5a31SPaolo Pisati #endif 916bafe5a31SPaolo Pisati /* 917bafe5a31SPaolo Pisati * If there are no ithreads (per event and per handler), then 918bafe5a31SPaolo Pisati * just remove the handler and return. 919bafe5a31SPaolo Pisati * XXX: Note that an INTR_FAST handler might be running on another CPU! 920bafe5a31SPaolo Pisati */ 921bafe5a31SPaolo Pisati if (ie->ie_thread == NULL && handler->ih_thread == NULL) { 922bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 923bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 924bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 925bafe5a31SPaolo Pisati return (0); 926bafe5a31SPaolo Pisati } 927bafe5a31SPaolo Pisati 928bafe5a31SPaolo Pisati /* Private or global ithread? */ 929bafe5a31SPaolo Pisati it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread; 930bafe5a31SPaolo Pisati /* 931bafe5a31SPaolo Pisati * If the interrupt thread is already running, then just mark this 932bafe5a31SPaolo Pisati * handler as being dead and let the ithread do the actual removal. 933bafe5a31SPaolo Pisati * 934bafe5a31SPaolo Pisati * During a cold boot while cold is set, msleep() does not sleep, 935bafe5a31SPaolo Pisati * so we have to remove the handler here rather than letting the 936bafe5a31SPaolo Pisati * thread do it. 937bafe5a31SPaolo Pisati */ 938982d11f8SJeff Roberson thread_lock(it->it_thread); 939bafe5a31SPaolo Pisati if (!TD_AWAITING_INTR(it->it_thread) && !cold) { 940bafe5a31SPaolo Pisati handler->ih_flags |= IH_DEAD; 941bafe5a31SPaolo Pisati 942bafe5a31SPaolo Pisati /* 943bafe5a31SPaolo Pisati * Ensure that the thread will process the handler list 944bafe5a31SPaolo Pisati * again and remove this handler if it has already passed 945bafe5a31SPaolo Pisati * it on the list. 946bafe5a31SPaolo Pisati */ 947bafe5a31SPaolo Pisati it->it_need = 1; 948bafe5a31SPaolo Pisati } else 949bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 950982d11f8SJeff Roberson thread_unlock(it->it_thread); 951bafe5a31SPaolo Pisati while (handler->ih_flags & IH_DEAD) 952bafe5a31SPaolo Pisati msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 953bafe5a31SPaolo Pisati /* 954bafe5a31SPaolo Pisati * At this point, the handler has been disconnected from the event, 955bafe5a31SPaolo Pisati * so we can kill the private ithread if any. 956bafe5a31SPaolo Pisati */ 957bafe5a31SPaolo Pisati if (handler->ih_thread) { 958bafe5a31SPaolo Pisati ithread_destroy(handler->ih_thread); 959bafe5a31SPaolo Pisati handler->ih_thread = NULL; 960bafe5a31SPaolo Pisati } 961bafe5a31SPaolo Pisati intr_event_update(ie); 962bafe5a31SPaolo Pisati #ifdef notyet 963bafe5a31SPaolo Pisati /* 964bafe5a31SPaolo Pisati * XXX: This could be bad in the case of ppbus(8). Also, I think 965bafe5a31SPaolo Pisati * this could lead to races of stale data when servicing an 966bafe5a31SPaolo Pisati * interrupt. 967bafe5a31SPaolo Pisati */ 968bafe5a31SPaolo Pisati dead = 1; 969bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 970bafe5a31SPaolo Pisati if (handler != NULL) { 971bafe5a31SPaolo Pisati dead = 0; 972bafe5a31SPaolo Pisati break; 973bafe5a31SPaolo Pisati } 974bafe5a31SPaolo Pisati } 975bafe5a31SPaolo Pisati if (dead) { 976bafe5a31SPaolo Pisati ithread_destroy(ie->ie_thread); 977bafe5a31SPaolo Pisati ie->ie_thread = NULL; 978bafe5a31SPaolo Pisati } 979bafe5a31SPaolo Pisati #endif 980bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 981bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 982bafe5a31SPaolo Pisati return (0); 983bafe5a31SPaolo Pisati } 984bafe5a31SPaolo Pisati 9851ee1b687SJohn Baldwin static int 986bafe5a31SPaolo Pisati intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it) 987bafe5a31SPaolo Pisati { 988bafe5a31SPaolo Pisati struct intr_entropy entropy; 989bafe5a31SPaolo Pisati struct thread *td; 990bafe5a31SPaolo Pisati struct thread *ctd; 991bafe5a31SPaolo Pisati struct proc *p; 992bafe5a31SPaolo Pisati 993bafe5a31SPaolo Pisati /* 994bafe5a31SPaolo Pisati * If no ithread or no handlers, then we have a stray interrupt. 995bafe5a31SPaolo Pisati */ 996bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL) 997bafe5a31SPaolo Pisati return (EINVAL); 998bafe5a31SPaolo Pisati 999bafe5a31SPaolo Pisati ctd = curthread; 1000bafe5a31SPaolo Pisati td = it->it_thread; 1001bafe5a31SPaolo Pisati p = td->td_proc; 1002bafe5a31SPaolo Pisati 1003bafe5a31SPaolo Pisati /* 1004bafe5a31SPaolo Pisati * If any of the handlers for this ithread claim to be good 1005bafe5a31SPaolo Pisati * sources of entropy, then gather some. 1006bafe5a31SPaolo Pisati */ 1007bafe5a31SPaolo Pisati if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 1008bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 10097ab24ea3SJulian Elischer p->p_pid, td->td_name); 1010bafe5a31SPaolo Pisati entropy.event = (uintptr_t)ie; 1011bafe5a31SPaolo Pisati entropy.td = ctd; 1012bafe5a31SPaolo Pisati random_harvest(&entropy, sizeof(entropy), 2, 0, 1013bafe5a31SPaolo Pisati RANDOM_INTERRUPT); 1014bafe5a31SPaolo Pisati } 1015bafe5a31SPaolo Pisati 1016bafe5a31SPaolo Pisati KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 1017bafe5a31SPaolo Pisati 1018bafe5a31SPaolo Pisati /* 1019bafe5a31SPaolo Pisati * Set it_need to tell the thread to keep running if it is already 1020982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 1021982d11f8SJeff Roberson * put it on the runqueue. 1022bafe5a31SPaolo Pisati */ 1023bafe5a31SPaolo Pisati it->it_need = 1; 1024982d11f8SJeff Roberson thread_lock(td); 1025bafe5a31SPaolo Pisati if (TD_AWAITING_INTR(td)) { 1026bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 10273c1ffc32SJulian Elischer td->td_name); 1028bafe5a31SPaolo Pisati TD_CLR_IWAIT(td); 1029bafe5a31SPaolo Pisati sched_add(td, SRQ_INTR); 1030bafe5a31SPaolo Pisati } else { 1031bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 10327ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 1033bafe5a31SPaolo Pisati } 1034982d11f8SJeff Roberson thread_unlock(td); 1035bafe5a31SPaolo Pisati 1036bafe5a31SPaolo Pisati return (0); 1037bafe5a31SPaolo Pisati } 1038bafe5a31SPaolo Pisati #endif 10393e5da754SJohn Baldwin 1040fe486a37SJohn Baldwin /* 1041e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1042e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 1043e84bcd84SRobert Watson * a PIC. 1044e84bcd84SRobert Watson */ 1045e84bcd84SRobert Watson static int 1046e84bcd84SRobert Watson swi_assign_cpu(void *arg, u_char cpu) 1047e84bcd84SRobert Watson { 1048e84bcd84SRobert Watson 1049e84bcd84SRobert Watson return (0); 1050e84bcd84SRobert Watson } 1051e84bcd84SRobert Watson 1052e84bcd84SRobert Watson /* 1053fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 1054fe486a37SJohn Baldwin * is not specified, then a new event is created. 1055fe486a37SJohn Baldwin */ 10563e5da754SJohn Baldwin int 1057e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 1058b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 10598088699fSJohn Baldwin { 10601b9d701fSAttilio Rao struct thread *td; 1061e0f66ef8SJohn Baldwin struct intr_event *ie; 1062b4151f71SJohn Baldwin int error; 10638088699fSJohn Baldwin 1064bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 10653e5da754SJohn Baldwin return (EINVAL); 10663e5da754SJohn Baldwin 1067e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 10688088699fSJohn Baldwin 1069e0f66ef8SJohn Baldwin if (ie != NULL) { 1070e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 10713e5da754SJohn Baldwin return (EINVAL); 10723e5da754SJohn Baldwin } else { 10739b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1074e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10758088699fSJohn Baldwin if (error) 1076b4151f71SJohn Baldwin return (error); 1077e0f66ef8SJohn Baldwin if (eventp != NULL) 1078e0f66ef8SJohn Baldwin *eventp = ie; 10798088699fSJohn Baldwin } 10808d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1081*d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 10828d809d50SJeff Roberson if (error) 10838d809d50SJeff Roberson return (error); 10848d809d50SJeff Roberson if (pri == SWI_CLOCK) { 10851b9d701fSAttilio Rao td = ie->ie_thread->it_thread; 10861b9d701fSAttilio Rao thread_lock(td); 10871b9d701fSAttilio Rao td->td_flags |= TDF_NOLOAD; 10881b9d701fSAttilio Rao thread_unlock(td); 10898d809d50SJeff Roberson } 10908d809d50SJeff Roberson return (0); 10918088699fSJohn Baldwin } 10928088699fSJohn Baldwin 10931931cf94SJohn Baldwin /* 1094e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10951931cf94SJohn Baldwin */ 10961931cf94SJohn Baldwin void 1097b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10981931cf94SJohn Baldwin { 1099e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1100e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 11013e5da754SJohn Baldwin int error; 11028088699fSJohn Baldwin 1103e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1104e0f66ef8SJohn Baldwin ih->ih_need); 11051931cf94SJohn Baldwin 11061931cf94SJohn Baldwin /* 11073e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 11083e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 11093e5da754SJohn Baldwin * it will execute it the next time it runs. 11101931cf94SJohn Baldwin */ 1111b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 1); 11121ca2c018SBruce Evans 1113b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 111467596082SAttilio Rao PCPU_INC(cnt.v_soft); 1115bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1116bafe5a31SPaolo Pisati error = intr_event_schedule_thread(ie, ie->ie_thread); 1117bafe5a31SPaolo Pisati #else 1118e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 1119bafe5a31SPaolo Pisati #endif 11203e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 11218088699fSJohn Baldwin } 11228088699fSJohn Baldwin } 11238088699fSJohn Baldwin 1124fe486a37SJohn Baldwin /* 1125fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1126fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1127fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1128fe486a37SJohn Baldwin * an optimal interface. 1129fe486a37SJohn Baldwin */ 1130fe486a37SJohn Baldwin int 1131fe486a37SJohn Baldwin swi_remove(void *cookie) 1132fe486a37SJohn Baldwin { 1133fe486a37SJohn Baldwin 1134fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1135fe486a37SJohn Baldwin } 1136fe486a37SJohn Baldwin 1137bafe5a31SPaolo Pisati #ifdef INTR_FILTER 1138bafe5a31SPaolo Pisati static void 1139bafe5a31SPaolo Pisati priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih) 1140bafe5a31SPaolo Pisati { 1141bafe5a31SPaolo Pisati struct intr_event *ie; 1142bafe5a31SPaolo Pisati 1143bafe5a31SPaolo Pisati ie = ih->ih_event; 1144bafe5a31SPaolo Pisati /* 1145bafe5a31SPaolo Pisati * If this handler is marked for death, remove it from 1146bafe5a31SPaolo Pisati * the list of handlers and wake up the sleeper. 1147bafe5a31SPaolo Pisati */ 1148bafe5a31SPaolo Pisati if (ih->ih_flags & IH_DEAD) { 1149bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 1150bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1151bafe5a31SPaolo Pisati ih->ih_flags &= ~IH_DEAD; 1152bafe5a31SPaolo Pisati wakeup(ih); 1153bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 1154bafe5a31SPaolo Pisati return; 1155bafe5a31SPaolo Pisati } 1156bafe5a31SPaolo Pisati 1157bafe5a31SPaolo Pisati /* Execute this handler. */ 1158bafe5a31SPaolo Pisati CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1159bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument, 1160bafe5a31SPaolo Pisati ih->ih_name, ih->ih_flags); 1161bafe5a31SPaolo Pisati 1162bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 1163bafe5a31SPaolo Pisati mtx_lock(&Giant); 1164bafe5a31SPaolo Pisati ih->ih_handler(ih->ih_argument); 1165bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 1166bafe5a31SPaolo Pisati mtx_unlock(&Giant); 1167bafe5a31SPaolo Pisati } 1168bafe5a31SPaolo Pisati #endif 1169bafe5a31SPaolo Pisati 117037e9511fSJohn Baldwin /* 117137e9511fSJohn Baldwin * This is a public function for use by drivers that mux interrupt 117237e9511fSJohn Baldwin * handlers for child devices from their interrupt handler. 117337e9511fSJohn Baldwin */ 117437e9511fSJohn Baldwin void 117537e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1176e0f66ef8SJohn Baldwin { 1177e0f66ef8SJohn Baldwin struct intr_handler *ih, *ihn; 1178e0f66ef8SJohn Baldwin 1179e0f66ef8SJohn Baldwin TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1180e0f66ef8SJohn Baldwin /* 1181e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1182e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1183e0f66ef8SJohn Baldwin */ 1184e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1185e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1186e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1187e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1188e0f66ef8SJohn Baldwin wakeup(ih); 1189e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1190e0f66ef8SJohn Baldwin continue; 1191e0f66ef8SJohn Baldwin } 1192e0f66ef8SJohn Baldwin 1193f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1194f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1195f2d619c8SPaolo Pisati continue; 1196f2d619c8SPaolo Pisati 1197e0f66ef8SJohn Baldwin /* 1198e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1199e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1200e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 1201e0f66ef8SJohn Baldwin */ 1202e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 1203e0f66ef8SJohn Baldwin if (!ih->ih_need) 1204e0f66ef8SJohn Baldwin continue; 1205e0f66ef8SJohn Baldwin else 1206e0f66ef8SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 0); 1207e0f66ef8SJohn Baldwin } 1208e0f66ef8SJohn Baldwin 1209e0f66ef8SJohn Baldwin /* Execute this handler. */ 1210e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1211bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1212bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1213e0f66ef8SJohn Baldwin 1214e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1215e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1216e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1217e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1218e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1219e0f66ef8SJohn Baldwin } 122037e9511fSJohn Baldwin } 122137e9511fSJohn Baldwin 122237e9511fSJohn Baldwin static void 122337e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 122437e9511fSJohn Baldwin { 122537e9511fSJohn Baldwin 122637e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 122737e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 122837e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 122937e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1230e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1231e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1232e0f66ef8SJohn Baldwin 1233e0f66ef8SJohn Baldwin /* 1234e0f66ef8SJohn Baldwin * Interrupt storm handling: 1235e0f66ef8SJohn Baldwin * 1236e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1237e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1238e0f66ef8SJohn Baldwin * 1239e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1240e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1241e0f66ef8SJohn Baldwin * then enter storming mode. 1242e0f66ef8SJohn Baldwin */ 1243e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1244e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 12450ae62c18SNate Lawson /* Report the message only once every second. */ 12460ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1247e0f66ef8SJohn Baldwin printf( 12480ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1249e0f66ef8SJohn Baldwin ie->ie_name); 1250e0f66ef8SJohn Baldwin } 1251e41bcf3cSJohn Baldwin pause("istorm", 1); 1252e0f66ef8SJohn Baldwin } else 1253e0f66ef8SJohn Baldwin ie->ie_count++; 1254e0f66ef8SJohn Baldwin 1255e0f66ef8SJohn Baldwin /* 1256e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1257e0f66ef8SJohn Baldwin * the interrupt source. 1258e0f66ef8SJohn Baldwin */ 12591ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 12601ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1261e0f66ef8SJohn Baldwin } 1262e0f66ef8SJohn Baldwin 1263bafe5a31SPaolo Pisati #ifndef INTR_FILTER 12648088699fSJohn Baldwin /* 1265b4151f71SJohn Baldwin * This is the main code for interrupt threads. 12668088699fSJohn Baldwin */ 126737c84183SPoul-Henning Kamp static void 1268b4151f71SJohn Baldwin ithread_loop(void *arg) 12698088699fSJohn Baldwin { 1270e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1271e0f66ef8SJohn Baldwin struct intr_event *ie; 1272b40ce416SJulian Elischer struct thread *td; 1273b4151f71SJohn Baldwin struct proc *p; 12748088699fSJohn Baldwin 1275b40ce416SJulian Elischer td = curthread; 1276b40ce416SJulian Elischer p = td->td_proc; 1277e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1278e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 127991f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1280e0f66ef8SJohn Baldwin ie = ithd->it_event; 1281e0f66ef8SJohn Baldwin ie->ie_count = 0; 12828088699fSJohn Baldwin 12838088699fSJohn Baldwin /* 12848088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 12858088699fSJohn Baldwin * list of handlers, giving each one a go at it. 12868088699fSJohn Baldwin */ 12878088699fSJohn Baldwin for (;;) { 1288b4151f71SJohn Baldwin /* 1289b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1290b4151f71SJohn Baldwin */ 1291b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1292e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 12937ab24ea3SJulian Elischer p->p_pid, td->td_name); 1294b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1295ca9a0ddfSJulian Elischer kthread_exit(); 1296b4151f71SJohn Baldwin } 1297b4151f71SJohn Baldwin 1298e0f66ef8SJohn Baldwin /* 1299e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1300e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1301e0f66ef8SJohn Baldwin * should make another pass. 1302e0f66ef8SJohn Baldwin */ 1303b4151f71SJohn Baldwin while (ithd->it_need) { 13048088699fSJohn Baldwin /* 1305e0f66ef8SJohn Baldwin * This might need a full read and write barrier 1306e0f66ef8SJohn Baldwin * to make sure that this write posts before any 1307e0f66ef8SJohn Baldwin * of the memory or device accesses in the 1308e0f66ef8SJohn Baldwin * handlers. 13098088699fSJohn Baldwin */ 1310b4151f71SJohn Baldwin atomic_store_rel_int(&ithd->it_need, 0); 1311e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 13128088699fSJohn Baldwin } 13137870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 13147870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 13158088699fSJohn Baldwin 13168088699fSJohn Baldwin /* 13178088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 13188088699fSJohn Baldwin * lock. This may take a while and it_need may get 13198088699fSJohn Baldwin * set again, so we have to check it again. 13208088699fSJohn Baldwin */ 1321982d11f8SJeff Roberson thread_lock(td); 1322e0f66ef8SJohn Baldwin if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 13237870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1324e0f66ef8SJohn Baldwin ie->ie_count = 0; 13258df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 13268088699fSJohn Baldwin } 1327982d11f8SJeff Roberson thread_unlock(td); 13288088699fSJohn Baldwin } 13291931cf94SJohn Baldwin } 13301ee1b687SJohn Baldwin 13311ee1b687SJohn Baldwin /* 13321ee1b687SJohn Baldwin * Main interrupt handling body. 13331ee1b687SJohn Baldwin * 13341ee1b687SJohn Baldwin * Input: 13351ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 13361ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 13371ee1b687SJohn Baldwin * handlers as their main argument. 13381ee1b687SJohn Baldwin * Return value: 13391ee1b687SJohn Baldwin * o 0: everything ok. 13401ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 13411ee1b687SJohn Baldwin */ 13421ee1b687SJohn Baldwin int 13431ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 13441ee1b687SJohn Baldwin { 13451ee1b687SJohn Baldwin struct intr_handler *ih; 13461f255bd3SAlexander Motin struct trapframe *oldframe; 13471ee1b687SJohn Baldwin struct thread *td; 13481ee1b687SJohn Baldwin int error, ret, thread; 13491ee1b687SJohn Baldwin 13501ee1b687SJohn Baldwin td = curthread; 13511ee1b687SJohn Baldwin 13521ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 13531ee1b687SJohn Baldwin if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 13541ee1b687SJohn Baldwin return (EINVAL); 13551ee1b687SJohn Baldwin 13561ee1b687SJohn Baldwin /* 13571ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 13581ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 13591ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 13601ee1b687SJohn Baldwin * a trapframe as its argument. 13611ee1b687SJohn Baldwin */ 13621ee1b687SJohn Baldwin td->td_intr_nesting_level++; 13631ee1b687SJohn Baldwin thread = 0; 13641ee1b687SJohn Baldwin ret = 0; 13651ee1b687SJohn Baldwin critical_enter(); 13661f255bd3SAlexander Motin oldframe = td->td_intr_frame; 13671f255bd3SAlexander Motin td->td_intr_frame = frame; 13681ee1b687SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 13691ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 13701ee1b687SJohn Baldwin thread = 1; 13711ee1b687SJohn Baldwin continue; 13721ee1b687SJohn Baldwin } 13731ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 13741ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 13751ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 13761ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 13771ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 13781ee1b687SJohn Baldwin else 13791ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 138089fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 138189fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 138289fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 138389fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 138489fc20ccSAndriy Gapon ih->ih_name)); 138589fc20ccSAndriy Gapon 13861ee1b687SJohn Baldwin /* 13871ee1b687SJohn Baldwin * Wrapper handler special handling: 13881ee1b687SJohn Baldwin * 13891ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 13901ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 13911ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 13921ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 13931ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 13941ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 13951ee1b687SJohn Baldwin * of just an ithread handler. 13961ee1b687SJohn Baldwin * 13971ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 13981ee1b687SJohn Baldwin * their own 13991ee1b687SJohn Baldwin */ 14001ee1b687SJohn Baldwin if (!thread) { 14011ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 14021ee1b687SJohn Baldwin thread = 1; 14031ee1b687SJohn Baldwin } 14041ee1b687SJohn Baldwin } 14051f255bd3SAlexander Motin td->td_intr_frame = oldframe; 14061ee1b687SJohn Baldwin 14071ee1b687SJohn Baldwin if (thread) { 14081ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 14091ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 14101ee1b687SJohn Baldwin } else { 14111ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 14121ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 14131ee1b687SJohn Baldwin } 14141ee1b687SJohn Baldwin 14151ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 14161ee1b687SJohn Baldwin if (thread) { 14171ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 14186205924aSKip Macy #ifndef XEN 14191ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 14206205924aSKip Macy #else 14216205924aSKip Macy if (error != 0) 14226205924aSKip Macy log(LOG_WARNING, "bad stray interrupt"); 14236205924aSKip Macy #endif 14241ee1b687SJohn Baldwin } 14251ee1b687SJohn Baldwin critical_exit(); 14261ee1b687SJohn Baldwin td->td_intr_nesting_level--; 14271ee1b687SJohn Baldwin return (0); 14281ee1b687SJohn Baldwin } 1429bafe5a31SPaolo Pisati #else 1430bafe5a31SPaolo Pisati /* 1431bafe5a31SPaolo Pisati * This is the main code for interrupt threads. 1432bafe5a31SPaolo Pisati */ 1433bafe5a31SPaolo Pisati static void 1434bafe5a31SPaolo Pisati ithread_loop(void *arg) 1435bafe5a31SPaolo Pisati { 1436bafe5a31SPaolo Pisati struct intr_thread *ithd; 1437bafe5a31SPaolo Pisati struct intr_handler *ih; 1438bafe5a31SPaolo Pisati struct intr_event *ie; 1439bafe5a31SPaolo Pisati struct thread *td; 1440bafe5a31SPaolo Pisati struct proc *p; 1441bafe5a31SPaolo Pisati int priv; 1442bafe5a31SPaolo Pisati 1443bafe5a31SPaolo Pisati td = curthread; 1444bafe5a31SPaolo Pisati p = td->td_proc; 1445bafe5a31SPaolo Pisati ih = (struct intr_handler *)arg; 1446bafe5a31SPaolo Pisati priv = (ih->ih_thread != NULL) ? 1 : 0; 1447bafe5a31SPaolo Pisati ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread; 1448bafe5a31SPaolo Pisati KASSERT(ithd->it_thread == td, 1449bafe5a31SPaolo Pisati ("%s: ithread and proc linkage out of sync", __func__)); 1450bafe5a31SPaolo Pisati ie = ithd->it_event; 1451bafe5a31SPaolo Pisati ie->ie_count = 0; 1452bafe5a31SPaolo Pisati 1453bafe5a31SPaolo Pisati /* 1454bafe5a31SPaolo Pisati * As long as we have interrupts outstanding, go through the 1455bafe5a31SPaolo Pisati * list of handlers, giving each one a go at it. 1456bafe5a31SPaolo Pisati */ 1457bafe5a31SPaolo Pisati for (;;) { 1458bafe5a31SPaolo Pisati /* 1459bafe5a31SPaolo Pisati * If we are an orphaned thread, then just die. 1460bafe5a31SPaolo Pisati */ 1461bafe5a31SPaolo Pisati if (ithd->it_flags & IT_DEAD) { 1462bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 14637ab24ea3SJulian Elischer p->p_pid, td->td_name); 1464bafe5a31SPaolo Pisati free(ithd, M_ITHREAD); 1465ca9a0ddfSJulian Elischer kthread_exit(); 1466bafe5a31SPaolo Pisati } 1467bafe5a31SPaolo Pisati 1468bafe5a31SPaolo Pisati /* 1469bafe5a31SPaolo Pisati * Service interrupts. If another interrupt arrives while 1470bafe5a31SPaolo Pisati * we are running, it will set it_need to note that we 1471bafe5a31SPaolo Pisati * should make another pass. 1472bafe5a31SPaolo Pisati */ 1473bafe5a31SPaolo Pisati while (ithd->it_need) { 1474bafe5a31SPaolo Pisati /* 1475bafe5a31SPaolo Pisati * This might need a full read and write barrier 1476bafe5a31SPaolo Pisati * to make sure that this write posts before any 1477bafe5a31SPaolo Pisati * of the memory or device accesses in the 1478bafe5a31SPaolo Pisati * handlers. 1479bafe5a31SPaolo Pisati */ 1480bafe5a31SPaolo Pisati atomic_store_rel_int(&ithd->it_need, 0); 1481bafe5a31SPaolo Pisati if (priv) 1482bafe5a31SPaolo Pisati priv_ithread_execute_handler(p, ih); 1483bafe5a31SPaolo Pisati else 1484bafe5a31SPaolo Pisati ithread_execute_handlers(p, ie); 1485bafe5a31SPaolo Pisati } 1486bafe5a31SPaolo Pisati WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 1487bafe5a31SPaolo Pisati mtx_assert(&Giant, MA_NOTOWNED); 1488bafe5a31SPaolo Pisati 1489bafe5a31SPaolo Pisati /* 1490bafe5a31SPaolo Pisati * Processed all our interrupts. Now get the sched 1491bafe5a31SPaolo Pisati * lock. This may take a while and it_need may get 1492bafe5a31SPaolo Pisati * set again, so we have to check it again. 1493bafe5a31SPaolo Pisati */ 1494982d11f8SJeff Roberson thread_lock(td); 1495bafe5a31SPaolo Pisati if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 1496bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 1497bafe5a31SPaolo Pisati ie->ie_count = 0; 14988df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 1499bafe5a31SPaolo Pisati } 1500982d11f8SJeff Roberson thread_unlock(td); 1501bafe5a31SPaolo Pisati } 1502bafe5a31SPaolo Pisati } 1503bafe5a31SPaolo Pisati 1504bafe5a31SPaolo Pisati /* 1505bafe5a31SPaolo Pisati * Main loop for interrupt filter. 1506bafe5a31SPaolo Pisati * 1507bafe5a31SPaolo Pisati * Some architectures (i386, amd64 and arm) require the optional frame 1508bafe5a31SPaolo Pisati * parameter, and use it as the main argument for fast handler execution 1509bafe5a31SPaolo Pisati * when ih_argument == NULL. 1510bafe5a31SPaolo Pisati * 1511bafe5a31SPaolo Pisati * Return value: 1512bafe5a31SPaolo Pisati * o FILTER_STRAY: No filter recognized the event, and no 1513bafe5a31SPaolo Pisati * filter-less handler is registered on this 1514bafe5a31SPaolo Pisati * line. 1515bafe5a31SPaolo Pisati * o FILTER_HANDLED: A filter claimed the event and served it. 1516bafe5a31SPaolo Pisati * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at 1517bafe5a31SPaolo Pisati * least one filter-less handler on this line. 1518bafe5a31SPaolo Pisati * o FILTER_HANDLED | 1519bafe5a31SPaolo Pisati * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for 1520bafe5a31SPaolo Pisati * scheduling the per-handler ithread. 1521bafe5a31SPaolo Pisati * 1522bafe5a31SPaolo Pisati * In case an ithread has to be scheduled, in *ithd there will be a 1523bafe5a31SPaolo Pisati * pointer to a struct intr_thread containing the thread to be 1524bafe5a31SPaolo Pisati * scheduled. 1525bafe5a31SPaolo Pisati */ 1526bafe5a31SPaolo Pisati 15271ee1b687SJohn Baldwin static int 1528bafe5a31SPaolo Pisati intr_filter_loop(struct intr_event *ie, struct trapframe *frame, 1529bafe5a31SPaolo Pisati struct intr_thread **ithd) 1530bafe5a31SPaolo Pisati { 1531bafe5a31SPaolo Pisati struct intr_handler *ih; 1532bafe5a31SPaolo Pisati void *arg; 1533bafe5a31SPaolo Pisati int ret, thread_only; 1534bafe5a31SPaolo Pisati 1535bafe5a31SPaolo Pisati ret = 0; 1536bafe5a31SPaolo Pisati thread_only = 0; 1537bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1538bafe5a31SPaolo Pisati /* 1539bafe5a31SPaolo Pisati * Execute fast interrupt handlers directly. 1540bafe5a31SPaolo Pisati * To support clock handlers, if a handler registers 1541bafe5a31SPaolo Pisati * with a NULL argument, then we pass it a pointer to 1542bafe5a31SPaolo Pisati * a trapframe as its argument. 1543bafe5a31SPaolo Pisati */ 1544bafe5a31SPaolo Pisati arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument); 1545bafe5a31SPaolo Pisati 1546bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__, 1547bafe5a31SPaolo Pisati ih->ih_filter, ih->ih_handler, arg, ih->ih_name); 1548bafe5a31SPaolo Pisati 1549bafe5a31SPaolo Pisati if (ih->ih_filter != NULL) 1550bafe5a31SPaolo Pisati ret = ih->ih_filter(arg); 1551bafe5a31SPaolo Pisati else { 1552bafe5a31SPaolo Pisati thread_only = 1; 1553bafe5a31SPaolo Pisati continue; 1554bafe5a31SPaolo Pisati } 155589fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 155689fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 155789fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 155889fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 155989fc20ccSAndriy Gapon ih->ih_name)); 1560bafe5a31SPaolo Pisati if (ret & FILTER_STRAY) 1561bafe5a31SPaolo Pisati continue; 1562bafe5a31SPaolo Pisati else { 1563bafe5a31SPaolo Pisati *ithd = ih->ih_thread; 1564bafe5a31SPaolo Pisati return (ret); 1565bafe5a31SPaolo Pisati } 1566bafe5a31SPaolo Pisati } 1567bafe5a31SPaolo Pisati 1568bafe5a31SPaolo Pisati /* 1569bafe5a31SPaolo Pisati * No filters handled the interrupt and we have at least 1570bafe5a31SPaolo Pisati * one handler without a filter. In this case, we schedule 1571bafe5a31SPaolo Pisati * all of the filter-less handlers to run in the ithread. 1572bafe5a31SPaolo Pisati */ 1573bafe5a31SPaolo Pisati if (thread_only) { 1574bafe5a31SPaolo Pisati *ithd = ie->ie_thread; 1575bafe5a31SPaolo Pisati return (FILTER_SCHEDULE_THREAD); 1576bafe5a31SPaolo Pisati } 1577bafe5a31SPaolo Pisati return (FILTER_STRAY); 1578bafe5a31SPaolo Pisati } 1579bafe5a31SPaolo Pisati 1580bafe5a31SPaolo Pisati /* 1581bafe5a31SPaolo Pisati * Main interrupt handling body. 1582bafe5a31SPaolo Pisati * 1583bafe5a31SPaolo Pisati * Input: 1584bafe5a31SPaolo Pisati * o ie: the event connected to this interrupt. 1585bafe5a31SPaolo Pisati * o frame: some archs (i.e. i386) pass a frame to some. 1586bafe5a31SPaolo Pisati * handlers as their main argument. 1587bafe5a31SPaolo Pisati * Return value: 1588bafe5a31SPaolo Pisati * o 0: everything ok. 1589bafe5a31SPaolo Pisati * o EINVAL: stray interrupt. 1590bafe5a31SPaolo Pisati */ 1591bafe5a31SPaolo Pisati int 1592bafe5a31SPaolo Pisati intr_event_handle(struct intr_event *ie, struct trapframe *frame) 1593bafe5a31SPaolo Pisati { 1594bafe5a31SPaolo Pisati struct intr_thread *ithd; 15951f255bd3SAlexander Motin struct trapframe *oldframe; 1596bafe5a31SPaolo Pisati struct thread *td; 1597bafe5a31SPaolo Pisati int thread; 1598bafe5a31SPaolo Pisati 1599bafe5a31SPaolo Pisati ithd = NULL; 1600bafe5a31SPaolo Pisati td = curthread; 1601bafe5a31SPaolo Pisati 1602bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 1603bafe5a31SPaolo Pisati return (EINVAL); 1604bafe5a31SPaolo Pisati 1605bafe5a31SPaolo Pisati td->td_intr_nesting_level++; 1606bafe5a31SPaolo Pisati thread = 0; 1607bafe5a31SPaolo Pisati critical_enter(); 16081f255bd3SAlexander Motin oldframe = td->td_intr_frame; 16091f255bd3SAlexander Motin td->td_intr_frame = frame; 1610bafe5a31SPaolo Pisati thread = intr_filter_loop(ie, frame, &ithd); 1611bafe5a31SPaolo Pisati if (thread & FILTER_HANDLED) { 16121ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 16131ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 1614bafe5a31SPaolo Pisati } else { 16151ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 16161ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 1617bafe5a31SPaolo Pisati } 16181f255bd3SAlexander Motin td->td_intr_frame = oldframe; 1619bafe5a31SPaolo Pisati critical_exit(); 1620bafe5a31SPaolo Pisati 1621bafe5a31SPaolo Pisati /* Interrupt storm logic */ 1622bafe5a31SPaolo Pisati if (thread & FILTER_STRAY) { 1623bafe5a31SPaolo Pisati ie->ie_count++; 1624bafe5a31SPaolo Pisati if (ie->ie_count < intr_storm_threshold) 1625bafe5a31SPaolo Pisati printf("Interrupt stray detection not present\n"); 1626bafe5a31SPaolo Pisati } 1627bafe5a31SPaolo Pisati 1628bafe5a31SPaolo Pisati /* Schedule an ithread if needed. */ 1629bafe5a31SPaolo Pisati if (thread & FILTER_SCHEDULE_THREAD) { 1630bafe5a31SPaolo Pisati if (intr_event_schedule_thread(ie, ithd) != 0) 1631bafe5a31SPaolo Pisati panic("%s: impossible stray interrupt", __func__); 1632bafe5a31SPaolo Pisati } 1633bafe5a31SPaolo Pisati td->td_intr_nesting_level--; 1634bafe5a31SPaolo Pisati return (0); 1635bafe5a31SPaolo Pisati } 1636bafe5a31SPaolo Pisati #endif 16371931cf94SJohn Baldwin 16388b201c42SJohn Baldwin #ifdef DDB 16398b201c42SJohn Baldwin /* 16408b201c42SJohn Baldwin * Dump details about an interrupt handler 16418b201c42SJohn Baldwin */ 16428b201c42SJohn Baldwin static void 1643e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 16448b201c42SJohn Baldwin { 16458b201c42SJohn Baldwin int comma; 16468b201c42SJohn Baldwin 16478b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 16488b201c42SJohn Baldwin switch (ih->ih_pri) { 16498b201c42SJohn Baldwin case PI_REALTIME: 16508b201c42SJohn Baldwin db_printf("CLK "); 16518b201c42SJohn Baldwin break; 16528b201c42SJohn Baldwin case PI_AV: 16538b201c42SJohn Baldwin db_printf("AV "); 16548b201c42SJohn Baldwin break; 1655*d3305205SJohn Baldwin case PI_TTY: 16568b201c42SJohn Baldwin db_printf("TTY "); 16578b201c42SJohn Baldwin break; 16588b201c42SJohn Baldwin case PI_NET: 16598b201c42SJohn Baldwin db_printf("NET "); 16608b201c42SJohn Baldwin break; 16618b201c42SJohn Baldwin case PI_DISK: 16628b201c42SJohn Baldwin db_printf("DISK"); 16638b201c42SJohn Baldwin break; 16648b201c42SJohn Baldwin case PI_DULL: 16658b201c42SJohn Baldwin db_printf("DULL"); 16668b201c42SJohn Baldwin break; 16678b201c42SJohn Baldwin default: 16688b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 16698b201c42SJohn Baldwin db_printf("SWI "); 16708b201c42SJohn Baldwin else 16718b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 16728b201c42SJohn Baldwin break; 16738b201c42SJohn Baldwin } 16748b201c42SJohn Baldwin db_printf(" "); 16758b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 16768b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 16778b201c42SJohn Baldwin if (ih->ih_need || 1678ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 16798b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 16808b201c42SJohn Baldwin db_printf(" {"); 16818b201c42SJohn Baldwin comma = 0; 16828b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 16838b201c42SJohn Baldwin if (comma) 16848b201c42SJohn Baldwin db_printf(", "); 16858b201c42SJohn Baldwin db_printf("EXCL"); 16868b201c42SJohn Baldwin comma = 1; 16878b201c42SJohn Baldwin } 16888b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 16898b201c42SJohn Baldwin if (comma) 16908b201c42SJohn Baldwin db_printf(", "); 16918b201c42SJohn Baldwin db_printf("ENTROPY"); 16928b201c42SJohn Baldwin comma = 1; 16938b201c42SJohn Baldwin } 16948b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 16958b201c42SJohn Baldwin if (comma) 16968b201c42SJohn Baldwin db_printf(", "); 16978b201c42SJohn Baldwin db_printf("DEAD"); 16988b201c42SJohn Baldwin comma = 1; 16998b201c42SJohn Baldwin } 17008b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 17018b201c42SJohn Baldwin if (comma) 17028b201c42SJohn Baldwin db_printf(", "); 17038b201c42SJohn Baldwin db_printf("MPSAFE"); 17048b201c42SJohn Baldwin comma = 1; 17058b201c42SJohn Baldwin } 17068b201c42SJohn Baldwin if (ih->ih_need) { 17078b201c42SJohn Baldwin if (comma) 17088b201c42SJohn Baldwin db_printf(", "); 17098b201c42SJohn Baldwin db_printf("NEED"); 17108b201c42SJohn Baldwin } 17118b201c42SJohn Baldwin db_printf("}"); 17128b201c42SJohn Baldwin } 17138b201c42SJohn Baldwin db_printf("\n"); 17148b201c42SJohn Baldwin } 17158b201c42SJohn Baldwin 17168b201c42SJohn Baldwin /* 1717e0f66ef8SJohn Baldwin * Dump details about a event. 17188b201c42SJohn Baldwin */ 17198b201c42SJohn Baldwin void 1720e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 17218b201c42SJohn Baldwin { 1722e0f66ef8SJohn Baldwin struct intr_handler *ih; 1723e0f66ef8SJohn Baldwin struct intr_thread *it; 17248b201c42SJohn Baldwin int comma; 17258b201c42SJohn Baldwin 1726e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1727e0f66ef8SJohn Baldwin it = ie->ie_thread; 1728e0f66ef8SJohn Baldwin if (it != NULL) 1729e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1730e0f66ef8SJohn Baldwin else 1731e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1732e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1733e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 17348b201c42SJohn Baldwin db_printf(" {"); 17358b201c42SJohn Baldwin comma = 0; 1736e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 17378b201c42SJohn Baldwin db_printf("SOFT"); 17388b201c42SJohn Baldwin comma = 1; 17398b201c42SJohn Baldwin } 1740e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 17418b201c42SJohn Baldwin if (comma) 17428b201c42SJohn Baldwin db_printf(", "); 17438b201c42SJohn Baldwin db_printf("ENTROPY"); 17448b201c42SJohn Baldwin comma = 1; 17458b201c42SJohn Baldwin } 1746e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 17478b201c42SJohn Baldwin if (comma) 17488b201c42SJohn Baldwin db_printf(", "); 1749e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 17508b201c42SJohn Baldwin comma = 1; 17518b201c42SJohn Baldwin } 1752e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 17538b201c42SJohn Baldwin if (comma) 17548b201c42SJohn Baldwin db_printf(", "); 17558b201c42SJohn Baldwin db_printf("NEED"); 17568b201c42SJohn Baldwin } 17578b201c42SJohn Baldwin db_printf("}"); 17588b201c42SJohn Baldwin } 17598b201c42SJohn Baldwin db_printf("\n"); 17608b201c42SJohn Baldwin 17618b201c42SJohn Baldwin if (handlers) 1762e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 17638b201c42SJohn Baldwin db_dump_intrhand(ih); 17648b201c42SJohn Baldwin } 1765e0f66ef8SJohn Baldwin 1766e0f66ef8SJohn Baldwin /* 1767e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1768e0f66ef8SJohn Baldwin */ 1769e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1770e0f66ef8SJohn Baldwin { 1771e0f66ef8SJohn Baldwin struct intr_event *ie; 177219e9205aSJohn Baldwin int all, verbose; 1773e0f66ef8SJohn Baldwin 1774e0f66ef8SJohn Baldwin verbose = index(modif, 'v') != NULL; 1775e0f66ef8SJohn Baldwin all = index(modif, 'a') != NULL; 1776e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1777e0f66ef8SJohn Baldwin if (!all && TAILQ_EMPTY(&ie->ie_handlers)) 1778e0f66ef8SJohn Baldwin continue; 1779e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 178019e9205aSJohn Baldwin if (db_pager_quit) 178119e9205aSJohn Baldwin break; 1782e0f66ef8SJohn Baldwin } 1783e0f66ef8SJohn Baldwin } 17848b201c42SJohn Baldwin #endif /* DDB */ 17858b201c42SJohn Baldwin 1786b4151f71SJohn Baldwin /* 17878088699fSJohn Baldwin * Start standard software interrupt threads 17881931cf94SJohn Baldwin */ 17891931cf94SJohn Baldwin static void 1790b4151f71SJohn Baldwin start_softintr(void *dummy) 17911931cf94SJohn Baldwin { 1792b4151f71SJohn Baldwin 17938d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 17948d809d50SJeff Roberson panic("died while creating vm swi ithread"); 17951931cf94SJohn Baldwin } 1796237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1797237fdd78SRobert Watson NULL); 17981931cf94SJohn Baldwin 1799d279178dSThomas Moestl /* 1800d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1801d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1802d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1803d279178dSThomas Moestl * independent. 1804d279178dSThomas Moestl * 1805d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1806d279178dSThomas Moestl * calculate things at run time. 1807d279178dSThomas Moestl */ 1808d279178dSThomas Moestl static int 1809d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1810d279178dSThomas Moestl { 1811d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 1812d279178dSThomas Moestl req)); 1813d279178dSThomas Moestl } 1814d279178dSThomas Moestl 1815d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1816d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1817d279178dSThomas Moestl 1818d279178dSThomas Moestl static int 1819d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1820d279178dSThomas Moestl { 1821d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrcnt, 1822d279178dSThomas Moestl (char *)eintrcnt - (char *)intrcnt, req)); 1823d279178dSThomas Moestl } 1824d279178dSThomas Moestl 1825d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1826d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 18278b201c42SJohn Baldwin 18288b201c42SJohn Baldwin #ifdef DDB 18298b201c42SJohn Baldwin /* 18308b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 18318b201c42SJohn Baldwin */ 18328b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 18338b201c42SJohn Baldwin { 18348b201c42SJohn Baldwin u_long *i; 18358b201c42SJohn Baldwin char *cp; 18368b201c42SJohn Baldwin 18378b201c42SJohn Baldwin cp = intrnames; 183819e9205aSJohn Baldwin for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) { 18398b201c42SJohn Baldwin if (*cp == '\0') 18408b201c42SJohn Baldwin break; 18418b201c42SJohn Baldwin if (*i != 0) 18428b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 18438b201c42SJohn Baldwin cp += strlen(cp) + 1; 18448b201c42SJohn Baldwin } 18458b201c42SJohn Baldwin } 18468b201c42SJohn Baldwin #endif 1847