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> 359a94c9c5SJohn Baldwin #include <sys/rtprio.h> 36425f9fdaSStefan Eßer #include <sys/systm.h> 3768352337SDoug Rabson #include <sys/interrupt.h> 381931cf94SJohn Baldwin #include <sys/kernel.h> 391931cf94SJohn Baldwin #include <sys/kthread.h> 401931cf94SJohn Baldwin #include <sys/ktr.h> 4105b2c96fSBruce Evans #include <sys/limits.h> 42f34fa851SJohn Baldwin #include <sys/lock.h> 431931cf94SJohn Baldwin #include <sys/malloc.h> 4435e0e5b3SJohn Baldwin #include <sys/mutex.h> 451931cf94SJohn Baldwin #include <sys/proc.h> 463e5da754SJohn Baldwin #include <sys/random.h> 47b4151f71SJohn Baldwin #include <sys/resourcevar.h> 4863710c4dSJohn Baldwin #include <sys/sched.h> 49d279178dSThomas Moestl #include <sys/sysctl.h> 501931cf94SJohn Baldwin #include <sys/unistd.h> 511931cf94SJohn Baldwin #include <sys/vmmeter.h> 521931cf94SJohn Baldwin #include <machine/atomic.h> 531931cf94SJohn Baldwin #include <machine/cpu.h> 548088699fSJohn Baldwin #include <machine/md_var.h> 55b4151f71SJohn Baldwin #include <machine/stdarg.h> 568b201c42SJohn Baldwin #ifdef DDB 578b201c42SJohn Baldwin #include <ddb/ddb.h> 588b201c42SJohn Baldwin #include <ddb/db_sym.h> 598b201c42SJohn Baldwin #endif 60425f9fdaSStefan Eßer 61e0f66ef8SJohn Baldwin /* 62e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 63e0f66ef8SJohn Baldwin */ 64e0f66ef8SJohn Baldwin struct intr_thread { 65e0f66ef8SJohn Baldwin struct intr_event *it_event; 66e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 67e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 68e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 693e5da754SJohn Baldwin }; 703e5da754SJohn Baldwin 71e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 72e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 73e0f66ef8SJohn Baldwin 74e0f66ef8SJohn Baldwin struct intr_entropy { 75e0f66ef8SJohn Baldwin struct thread *td; 76e0f66ef8SJohn Baldwin uintptr_t event; 77e0f66ef8SJohn Baldwin }; 78e0f66ef8SJohn Baldwin 79e0f66ef8SJohn Baldwin struct intr_event *clk_intr_event; 80e0f66ef8SJohn Baldwin struct intr_event *tty_intr_event; 817b1fe905SBruce Evans void *softclock_ih; 827b1fe905SBruce Evans void *vm_ih; 837ab24ea3SJulian Elischer struct proc *intrproc; 841931cf94SJohn Baldwin 85b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 86b4151f71SJohn Baldwin 870ae62c18SNate Lawson static int intr_storm_threshold = 1000; 887870c3c6SJohn Baldwin TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold); 897870c3c6SJohn Baldwin SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW, 907870c3c6SJohn Baldwin &intr_storm_threshold, 0, 917b1fe905SBruce Evans "Number of consecutive interrupts before storm protection is enabled"); 92e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 93e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 947b1fe905SBruce Evans 95e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 96bafe5a31SPaolo Pisati #ifdef INTR_FILTER 97bafe5a31SPaolo Pisati static struct intr_thread *ithread_create(const char *name, 98bafe5a31SPaolo Pisati struct intr_handler *ih); 99bafe5a31SPaolo Pisati #else 100e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 101bafe5a31SPaolo Pisati #endif 102e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 103bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 104bafe5a31SPaolo Pisati struct intr_event *ie); 105bafe5a31SPaolo Pisati #ifdef INTR_FILTER 106bafe5a31SPaolo Pisati static void priv_ithread_execute_handler(struct proc *p, 107bafe5a31SPaolo Pisati struct intr_handler *ih); 108bafe5a31SPaolo Pisati #endif 1097b1fe905SBruce Evans static void ithread_loop(void *); 110e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1117b1fe905SBruce Evans static void start_softintr(void *); 1127870c3c6SJohn Baldwin 113bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 114b4151f71SJohn Baldwin u_char 115e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1169a94c9c5SJohn Baldwin { 117b4151f71SJohn Baldwin u_char pri; 1189a94c9c5SJohn Baldwin 119b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1205a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1219a94c9c5SJohn Baldwin switch (flags) { 122b4151f71SJohn Baldwin case INTR_TYPE_TTY: 1239a94c9c5SJohn Baldwin pri = PI_TTYLOW; 1249a94c9c5SJohn Baldwin break; 1259a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1269a94c9c5SJohn Baldwin /* 1279a94c9c5SJohn Baldwin * XXX We need to refine this. BSD/OS distinguishes 1289a94c9c5SJohn Baldwin * between tape and disk priorities. 1299a94c9c5SJohn Baldwin */ 1309a94c9c5SJohn Baldwin pri = PI_DISK; 1319a94c9c5SJohn Baldwin break; 1329a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1339a94c9c5SJohn Baldwin pri = PI_NET; 1349a94c9c5SJohn Baldwin break; 1359a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 1369a94c9c5SJohn Baldwin pri = PI_DISK; /* XXX or PI_CAM? */ 1379a94c9c5SJohn Baldwin break; 1385a280d9cSPeter Wemm case INTR_TYPE_AV: /* Audio/video */ 1395a280d9cSPeter Wemm pri = PI_AV; 1405a280d9cSPeter Wemm break; 141b4151f71SJohn Baldwin case INTR_TYPE_CLK: 142b4151f71SJohn Baldwin pri = PI_REALTIME; 143b4151f71SJohn Baldwin break; 1449a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1459a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1469a94c9c5SJohn Baldwin break; 1479a94c9c5SJohn Baldwin default: 148b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 149e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1509a94c9c5SJohn Baldwin } 1519a94c9c5SJohn Baldwin 1529a94c9c5SJohn Baldwin return pri; 1539a94c9c5SJohn Baldwin } 1549a94c9c5SJohn Baldwin 155b4151f71SJohn Baldwin /* 156e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 157b4151f71SJohn Baldwin */ 158b4151f71SJohn Baldwin static void 159e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 160b4151f71SJohn Baldwin { 161e0f66ef8SJohn Baldwin struct intr_event *ie; 162b40ce416SJulian Elischer struct thread *td; 163e0f66ef8SJohn Baldwin u_char pri; 1648088699fSJohn Baldwin 165e0f66ef8SJohn Baldwin ie = ithd->it_event; 166e0f66ef8SJohn Baldwin td = ithd->it_thread; 167b4151f71SJohn Baldwin 168e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 169e0f66ef8SJohn Baldwin if (TAILQ_EMPTY(&ie->ie_handlers)) 170e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 171e0f66ef8SJohn Baldwin else 172e0f66ef8SJohn Baldwin pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri; 173e80fb434SRobert Drehmel 174e0f66ef8SJohn Baldwin /* Update name and priority. */ 1757ab24ea3SJulian Elischer strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 176982d11f8SJeff Roberson thread_lock(td); 177e0f66ef8SJohn Baldwin sched_prio(td, pri); 178982d11f8SJeff Roberson thread_unlock(td); 179b4151f71SJohn Baldwin } 180e0f66ef8SJohn Baldwin 181e0f66ef8SJohn Baldwin /* 182e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 183e0f66ef8SJohn Baldwin */ 184e0f66ef8SJohn Baldwin static void 185e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 186e0f66ef8SJohn Baldwin { 187e0f66ef8SJohn Baldwin struct intr_handler *ih; 188e0f66ef8SJohn Baldwin char *last; 189e0f66ef8SJohn Baldwin int missed, space; 190e0f66ef8SJohn Baldwin 191e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 192e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 193e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 194e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ENTROPY; 1950811d60aSJohn Baldwin missed = 0; 196e0f66ef8SJohn Baldwin space = 1; 197e0f66ef8SJohn Baldwin 198e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 199e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 200e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 201e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 202e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 203e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 204e0f66ef8SJohn Baldwin space = 0; 2050811d60aSJohn Baldwin } else 2060811d60aSJohn Baldwin missed++; 2070811d60aSJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 208e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ENTROPY; 2090811d60aSJohn Baldwin } 210e0f66ef8SJohn Baldwin 211e0f66ef8SJohn Baldwin /* 212e0f66ef8SJohn Baldwin * If the handler names were too long, add +'s to indicate missing 213e0f66ef8SJohn Baldwin * names. If we run out of room and still have +'s to add, change 214e0f66ef8SJohn Baldwin * the last character from a + to a *. 215e0f66ef8SJohn Baldwin */ 216e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2170811d60aSJohn Baldwin while (missed-- > 0) { 218e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 219e0f66ef8SJohn Baldwin if (*last == '+') { 220e0f66ef8SJohn Baldwin *last = '*'; 221e0f66ef8SJohn Baldwin break; 222b4151f71SJohn Baldwin } else 223e0f66ef8SJohn Baldwin *last = '+'; 224e0f66ef8SJohn Baldwin } else if (space) { 225e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 226e0f66ef8SJohn Baldwin space = 0; 227e0f66ef8SJohn Baldwin } else 228e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 229b4151f71SJohn Baldwin } 230e0f66ef8SJohn Baldwin 231e0f66ef8SJohn Baldwin /* 232e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 233e0f66ef8SJohn Baldwin * name. 234e0f66ef8SJohn Baldwin */ 235e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 236e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 237e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 238b4151f71SJohn Baldwin } 239b4151f71SJohn Baldwin 240bafe5a31SPaolo Pisati #ifndef INTR_FILTER 241b4151f71SJohn Baldwin int 242e0f66ef8SJohn Baldwin intr_event_create(struct intr_event **event, void *source, int flags, 243e0f66ef8SJohn Baldwin void (*enable)(void *), const char *fmt, ...) 244b4151f71SJohn Baldwin { 245e0f66ef8SJohn Baldwin struct intr_event *ie; 246b4151f71SJohn Baldwin va_list ap; 247b4151f71SJohn Baldwin 248e0f66ef8SJohn Baldwin /* The only valid flag during creation is IE_SOFT. */ 249e0f66ef8SJohn Baldwin if ((flags & ~IE_SOFT) != 0) 2503e5da754SJohn Baldwin return (EINVAL); 251e0f66ef8SJohn Baldwin ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 252e0f66ef8SJohn Baldwin ie->ie_source = source; 253e0f66ef8SJohn Baldwin ie->ie_enable = enable; 254e0f66ef8SJohn Baldwin ie->ie_flags = flags; 255e0f66ef8SJohn Baldwin TAILQ_INIT(&ie->ie_handlers); 256e0f66ef8SJohn Baldwin mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 257b4151f71SJohn Baldwin 258b4151f71SJohn Baldwin va_start(ap, fmt); 259e0f66ef8SJohn Baldwin vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 260b4151f71SJohn Baldwin va_end(ap); 261e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 262e0f66ef8SJohn Baldwin mtx_pool_lock(mtxpool_sleep, &event_list); 263e0f66ef8SJohn Baldwin TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 264e0f66ef8SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, &event_list); 265e0f66ef8SJohn Baldwin if (event != NULL) 266e0f66ef8SJohn Baldwin *event = ie; 267e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 268b4151f71SJohn Baldwin return (0); 269b4151f71SJohn Baldwin } 270bafe5a31SPaolo Pisati #else 271bafe5a31SPaolo Pisati int 272bafe5a31SPaolo Pisati intr_event_create(struct intr_event **event, void *source, int flags, 273bafe5a31SPaolo Pisati void (*enable)(void *), void (*eoi)(void *), void (*disab)(void *), 274bafe5a31SPaolo Pisati const char *fmt, ...) 275bafe5a31SPaolo Pisati { 276bafe5a31SPaolo Pisati struct intr_event *ie; 277bafe5a31SPaolo Pisati va_list ap; 278bafe5a31SPaolo Pisati 279bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 280bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 281bafe5a31SPaolo Pisati return (EINVAL); 282bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 283bafe5a31SPaolo Pisati ie->ie_source = source; 284bafe5a31SPaolo Pisati ie->ie_enable = enable; 285bafe5a31SPaolo Pisati ie->ie_eoi = eoi; 286bafe5a31SPaolo Pisati ie->ie_disab = disab; 287bafe5a31SPaolo Pisati ie->ie_flags = flags; 288bafe5a31SPaolo Pisati TAILQ_INIT(&ie->ie_handlers); 289bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 290bafe5a31SPaolo Pisati 291bafe5a31SPaolo Pisati va_start(ap, fmt); 292bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 293bafe5a31SPaolo Pisati va_end(ap); 294bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 295bafe5a31SPaolo Pisati mtx_pool_lock(mtxpool_sleep, &event_list); 296bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 297bafe5a31SPaolo Pisati mtx_pool_unlock(mtxpool_sleep, &event_list); 298bafe5a31SPaolo Pisati if (event != NULL) 299bafe5a31SPaolo Pisati *event = ie; 300bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 301bafe5a31SPaolo Pisati return (0); 302bafe5a31SPaolo Pisati } 303bafe5a31SPaolo Pisati #endif 304b4151f71SJohn Baldwin 305b4151f71SJohn Baldwin int 306e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 307b4151f71SJohn Baldwin { 308b4151f71SJohn Baldwin 309e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 310e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 311e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 312e0f66ef8SJohn Baldwin return (EBUSY); 3134d29cb2dSJohn Baldwin } 314e0f66ef8SJohn Baldwin mtx_pool_lock(mtxpool_sleep, &event_list); 315e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 316e0f66ef8SJohn Baldwin mtx_pool_unlock(mtxpool_sleep, &event_list); 3179477358dSJohn Baldwin #ifndef notyet 3189477358dSJohn Baldwin if (ie->ie_thread != NULL) { 3199477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 3209477358dSJohn Baldwin ie->ie_thread = NULL; 3219477358dSJohn Baldwin } 3229477358dSJohn Baldwin #endif 323e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 324e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 325e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 326e0f66ef8SJohn Baldwin return (0); 327e0f66ef8SJohn Baldwin } 328e0f66ef8SJohn Baldwin 329bafe5a31SPaolo Pisati #ifndef INTR_FILTER 330e0f66ef8SJohn Baldwin static struct intr_thread * 331e0f66ef8SJohn Baldwin ithread_create(const char *name) 332e0f66ef8SJohn Baldwin { 333e0f66ef8SJohn Baldwin struct intr_thread *ithd; 334e0f66ef8SJohn Baldwin struct thread *td; 335e0f66ef8SJohn Baldwin int error; 336e0f66ef8SJohn Baldwin 337e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 338e0f66ef8SJohn Baldwin 3397ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 3407ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 3419ef95d01SJulian Elischer 0, "intr", "%s", name); 342e0f66ef8SJohn Baldwin if (error) 3433745c395SJulian Elischer panic("kproc_create() failed with %d", error); 344982d11f8SJeff Roberson thread_lock(td); 345ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 346e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 347982d11f8SJeff Roberson thread_unlock(td); 348e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 349e0f66ef8SJohn Baldwin ithd->it_thread = td; 350e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 351e0f66ef8SJohn Baldwin return (ithd); 352e0f66ef8SJohn Baldwin } 353bafe5a31SPaolo Pisati #else 354bafe5a31SPaolo Pisati static struct intr_thread * 355bafe5a31SPaolo Pisati ithread_create(const char *name, struct intr_handler *ih) 356bafe5a31SPaolo Pisati { 357bafe5a31SPaolo Pisati struct intr_thread *ithd; 358bafe5a31SPaolo Pisati struct thread *td; 359bafe5a31SPaolo Pisati int error; 360bafe5a31SPaolo Pisati 361bafe5a31SPaolo Pisati ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 362bafe5a31SPaolo Pisati 3637ab24ea3SJulian Elischer error = kproc_kthread_create(ithread_loop, ih, &intrproc, 3647ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 3659ef95d01SJulian Elischer 0, "intr", "%s", name); 366bafe5a31SPaolo Pisati if (error) 3673745c395SJulian Elischer panic("kproc_create() failed with %d", error); 368982d11f8SJeff Roberson thread_lock(td); 369bafe5a31SPaolo Pisati sched_class(td, PRI_ITHD); 370bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 371982d11f8SJeff Roberson thread_unlock(td); 372bafe5a31SPaolo Pisati td->td_pflags |= TDP_ITHREAD; 373bafe5a31SPaolo Pisati ithd->it_thread = td; 374bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, name); 375bafe5a31SPaolo Pisati return (ithd); 376bafe5a31SPaolo Pisati } 377bafe5a31SPaolo Pisati #endif 378e0f66ef8SJohn Baldwin 379e0f66ef8SJohn Baldwin static void 380e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 381e0f66ef8SJohn Baldwin { 382e0f66ef8SJohn Baldwin struct thread *td; 383e0f66ef8SJohn Baldwin 384bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 385e0f66ef8SJohn Baldwin td = ithread->it_thread; 386982d11f8SJeff Roberson thread_lock(td); 387e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 38871fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 38971fad9fdSJulian Elischer TD_CLR_IWAIT(td); 390f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 391b4151f71SJohn Baldwin } 392982d11f8SJeff Roberson thread_unlock(td); 393b4151f71SJohn Baldwin } 394b4151f71SJohn Baldwin 395bafe5a31SPaolo Pisati #ifndef INTR_FILTER 396b4151f71SJohn Baldwin int 397e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 398ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 399ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 400b4151f71SJohn Baldwin { 401e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 402e0f66ef8SJohn Baldwin struct intr_thread *it; 403b4151f71SJohn Baldwin 404ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 405b4151f71SJohn Baldwin return (EINVAL); 406b4151f71SJohn Baldwin 407e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 408e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 409ef544f63SPaolo Pisati ih->ih_filter = filter; 410b4151f71SJohn Baldwin ih->ih_handler = handler; 411b4151f71SJohn Baldwin ih->ih_argument = arg; 412b4151f71SJohn Baldwin ih->ih_name = name; 413e0f66ef8SJohn Baldwin ih->ih_event = ie; 414b4151f71SJohn Baldwin ih->ih_pri = pri; 415ef544f63SPaolo Pisati if (flags & INTR_EXCL) 416b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 417b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 418b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 419b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 420b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 421b4151f71SJohn Baldwin 422e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 423e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 424e0f66ef8SJohn Baldwin if (!TAILQ_EMPTY(&ie->ie_handlers)) { 425e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 426e0f66ef8SJohn Baldwin (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 427e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 428b4151f71SJohn Baldwin free(ih, M_ITHREAD); 429b4151f71SJohn Baldwin return (EINVAL); 430b4151f71SJohn Baldwin } 431e0f66ef8SJohn Baldwin } 432e0f66ef8SJohn Baldwin 433e0f66ef8SJohn Baldwin /* Add the new handler to the event in priority order. */ 434e0f66ef8SJohn Baldwin TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 435e0f66ef8SJohn Baldwin if (temp_ih->ih_pri > ih->ih_pri) 436e0f66ef8SJohn Baldwin break; 437e0f66ef8SJohn Baldwin } 438e0f66ef8SJohn Baldwin if (temp_ih == NULL) 439e0f66ef8SJohn Baldwin TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 440e0f66ef8SJohn Baldwin else 441e0f66ef8SJohn Baldwin TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 442e0f66ef8SJohn Baldwin intr_event_update(ie); 443e0f66ef8SJohn Baldwin 444e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 445ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 446e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 4470f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 448e0f66ef8SJohn Baldwin else { 449e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 450e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 451e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 452e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 453e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 454e0f66ef8SJohn Baldwin ie->ie_thread = it; 455e0f66ef8SJohn Baldwin it->it_event = ie; 456e0f66ef8SJohn Baldwin ithread_update(it); 457e0f66ef8SJohn Baldwin wakeup(ie); 458e0f66ef8SJohn Baldwin } 459e0f66ef8SJohn Baldwin } 460e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 461e0f66ef8SJohn Baldwin ie->ie_name); 462e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 463e0f66ef8SJohn Baldwin 464e0f66ef8SJohn Baldwin if (cookiep != NULL) 465e0f66ef8SJohn Baldwin *cookiep = ih; 466e0f66ef8SJohn Baldwin return (0); 467e0f66ef8SJohn Baldwin } 468bafe5a31SPaolo Pisati #else 469bafe5a31SPaolo Pisati int 470bafe5a31SPaolo Pisati intr_event_add_handler(struct intr_event *ie, const char *name, 471bafe5a31SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 472bafe5a31SPaolo Pisati enum intr_type flags, void **cookiep) 473bafe5a31SPaolo Pisati { 474bafe5a31SPaolo Pisati struct intr_handler *ih, *temp_ih; 475bafe5a31SPaolo Pisati struct intr_thread *it; 476bafe5a31SPaolo Pisati 477bafe5a31SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 478bafe5a31SPaolo Pisati return (EINVAL); 479bafe5a31SPaolo Pisati 480bafe5a31SPaolo Pisati /* Allocate and populate an interrupt handler structure. */ 481bafe5a31SPaolo Pisati ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 482bafe5a31SPaolo Pisati ih->ih_filter = filter; 483bafe5a31SPaolo Pisati ih->ih_handler = handler; 484bafe5a31SPaolo Pisati ih->ih_argument = arg; 485bafe5a31SPaolo Pisati ih->ih_name = name; 486bafe5a31SPaolo Pisati ih->ih_event = ie; 487bafe5a31SPaolo Pisati ih->ih_pri = pri; 488bafe5a31SPaolo Pisati if (flags & INTR_EXCL) 489bafe5a31SPaolo Pisati ih->ih_flags = IH_EXCLUSIVE; 490bafe5a31SPaolo Pisati if (flags & INTR_MPSAFE) 491bafe5a31SPaolo Pisati ih->ih_flags |= IH_MPSAFE; 492bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 493bafe5a31SPaolo Pisati ih->ih_flags |= IH_ENTROPY; 494bafe5a31SPaolo Pisati 495bafe5a31SPaolo Pisati /* We can only have one exclusive handler in a event. */ 496bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 497bafe5a31SPaolo Pisati if (!TAILQ_EMPTY(&ie->ie_handlers)) { 498bafe5a31SPaolo Pisati if ((flags & INTR_EXCL) || 499bafe5a31SPaolo Pisati (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 500bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 501bafe5a31SPaolo Pisati free(ih, M_ITHREAD); 502bafe5a31SPaolo Pisati return (EINVAL); 503bafe5a31SPaolo Pisati } 504bafe5a31SPaolo Pisati } 505bafe5a31SPaolo Pisati 506bafe5a31SPaolo Pisati /* Add the new handler to the event in priority order. */ 507bafe5a31SPaolo Pisati TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) { 508bafe5a31SPaolo Pisati if (temp_ih->ih_pri > ih->ih_pri) 509bafe5a31SPaolo Pisati break; 510bafe5a31SPaolo Pisati } 511bafe5a31SPaolo Pisati if (temp_ih == NULL) 512bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next); 513bafe5a31SPaolo Pisati else 514bafe5a31SPaolo Pisati TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 515bafe5a31SPaolo Pisati intr_event_update(ie); 516bafe5a31SPaolo Pisati 517bafe5a31SPaolo Pisati /* For filtered handlers, create a private ithread to run on. */ 518bafe5a31SPaolo Pisati if (filter != NULL && handler != NULL) { 519bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 520bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 521bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 522bafe5a31SPaolo Pisati it->it_event = ie; 523bafe5a31SPaolo Pisati ih->ih_thread = it; 524bafe5a31SPaolo Pisati ithread_update(it); // XXX - do we really need this?!?!? 525bafe5a31SPaolo Pisati } else { /* Create the global per-event thread if we need one. */ 526bafe5a31SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 527bafe5a31SPaolo Pisati if (ie->ie_flags & IE_ADDING_THREAD) 528bafe5a31SPaolo Pisati msleep(ie, &ie->ie_lock, 0, "ithread", 0); 529bafe5a31SPaolo Pisati else { 530bafe5a31SPaolo Pisati ie->ie_flags |= IE_ADDING_THREAD; 531bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 532bafe5a31SPaolo Pisati it = ithread_create("intr: newborn", ih); 533bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 534bafe5a31SPaolo Pisati ie->ie_flags &= ~IE_ADDING_THREAD; 535bafe5a31SPaolo Pisati ie->ie_thread = it; 536bafe5a31SPaolo Pisati it->it_event = ie; 537bafe5a31SPaolo Pisati ithread_update(it); 538bafe5a31SPaolo Pisati wakeup(ie); 539bafe5a31SPaolo Pisati } 540bafe5a31SPaolo Pisati } 541bafe5a31SPaolo Pisati } 542bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 543bafe5a31SPaolo Pisati ie->ie_name); 544bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 545bafe5a31SPaolo Pisati 546bafe5a31SPaolo Pisati if (cookiep != NULL) 547bafe5a31SPaolo Pisati *cookiep = ih; 548bafe5a31SPaolo Pisati return (0); 549bafe5a31SPaolo Pisati } 550bafe5a31SPaolo Pisati #endif 551b4151f71SJohn Baldwin 552c3045318SJohn Baldwin /* 553c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 554c3045318SJohn Baldwin * associated with. 555c3045318SJohn Baldwin */ 556c3045318SJohn Baldwin void * 557c3045318SJohn Baldwin intr_handler_source(void *cookie) 558c3045318SJohn Baldwin { 559c3045318SJohn Baldwin struct intr_handler *ih; 560c3045318SJohn Baldwin struct intr_event *ie; 561c3045318SJohn Baldwin 562c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 563c3045318SJohn Baldwin if (ih == NULL) 564c3045318SJohn Baldwin return (NULL); 565c3045318SJohn Baldwin ie = ih->ih_event; 566c3045318SJohn Baldwin KASSERT(ie != NULL, 567c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 568c3045318SJohn Baldwin ih->ih_name)); 569c3045318SJohn Baldwin return (ie->ie_source); 570c3045318SJohn Baldwin } 571c3045318SJohn Baldwin 572bafe5a31SPaolo Pisati #ifndef INTR_FILTER 573b4151f71SJohn Baldwin int 574e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 575b4151f71SJohn Baldwin { 576e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 577e0f66ef8SJohn Baldwin struct intr_event *ie; 578b4151f71SJohn Baldwin #ifdef INVARIANTS 579e0f66ef8SJohn Baldwin struct intr_handler *ih; 580e0f66ef8SJohn Baldwin #endif 581e0f66ef8SJohn Baldwin #ifdef notyet 582e0f66ef8SJohn Baldwin int dead; 583b4151f71SJohn Baldwin #endif 584b4151f71SJohn Baldwin 5853e5da754SJohn Baldwin if (handler == NULL) 586b4151f71SJohn Baldwin return (EINVAL); 587e0f66ef8SJohn Baldwin ie = handler->ih_event; 588e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 589e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 5903e5da754SJohn Baldwin handler->ih_name)); 591e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 59291f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 593e0f66ef8SJohn Baldwin ie->ie_name); 594b4151f71SJohn Baldwin #ifdef INVARIANTS 595e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 5963e5da754SJohn Baldwin if (ih == handler) 5973e5da754SJohn Baldwin goto ok; 598e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 599e0f66ef8SJohn Baldwin panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 600e0f66ef8SJohn Baldwin ih->ih_name, ie->ie_name); 6013e5da754SJohn Baldwin ok: 602b4151f71SJohn Baldwin #endif 603de271f01SJohn Baldwin /* 604e0f66ef8SJohn Baldwin * If there is no ithread, then just remove the handler and return. 605e0f66ef8SJohn Baldwin * XXX: Note that an INTR_FAST handler might be running on another 606e0f66ef8SJohn Baldwin * CPU! 607e0f66ef8SJohn Baldwin */ 608e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 609e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 610e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 611e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 612e0f66ef8SJohn Baldwin return (0); 613e0f66ef8SJohn Baldwin } 614e0f66ef8SJohn Baldwin 615e0f66ef8SJohn Baldwin /* 616de271f01SJohn Baldwin * If the interrupt thread is already running, then just mark this 617de271f01SJohn Baldwin * handler as being dead and let the ithread do the actual removal. 618288e351bSDon Lewis * 619288e351bSDon Lewis * During a cold boot while cold is set, msleep() does not sleep, 620288e351bSDon Lewis * so we have to remove the handler here rather than letting the 621288e351bSDon Lewis * thread do it. 622de271f01SJohn Baldwin */ 623982d11f8SJeff Roberson thread_lock(ie->ie_thread->it_thread); 624e0f66ef8SJohn Baldwin if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) { 625de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 626de271f01SJohn Baldwin 627de271f01SJohn Baldwin /* 628de271f01SJohn Baldwin * Ensure that the thread will process the handler list 629de271f01SJohn Baldwin * again and remove this handler if it has already passed 630de271f01SJohn Baldwin * it on the list. 631de271f01SJohn Baldwin */ 632e0f66ef8SJohn Baldwin ie->ie_thread->it_need = 1; 6334d29cb2dSJohn Baldwin } else 634e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 635982d11f8SJeff Roberson thread_unlock(ie->ie_thread->it_thread); 636e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 6370f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 638e0f66ef8SJohn Baldwin intr_event_update(ie); 639e0f66ef8SJohn Baldwin #ifdef notyet 640e0f66ef8SJohn Baldwin /* 641e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 642e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 643e0f66ef8SJohn Baldwin * interrupt. 644e0f66ef8SJohn Baldwin */ 645e0f66ef8SJohn Baldwin dead = 1; 646e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 647e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_FAST)) { 648e0f66ef8SJohn Baldwin dead = 0; 649e0f66ef8SJohn Baldwin break; 650e0f66ef8SJohn Baldwin } 651e0f66ef8SJohn Baldwin } 652e0f66ef8SJohn Baldwin if (dead) { 653e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 654e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 655e0f66ef8SJohn Baldwin } 656e0f66ef8SJohn Baldwin #endif 657e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 658b4151f71SJohn Baldwin free(handler, M_ITHREAD); 659b4151f71SJohn Baldwin return (0); 660b4151f71SJohn Baldwin } 661b4151f71SJohn Baldwin 662b4151f71SJohn Baldwin int 663e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 6643e5da754SJohn Baldwin { 665e0f66ef8SJohn Baldwin struct intr_entropy entropy; 666e0f66ef8SJohn Baldwin struct intr_thread *it; 667b40ce416SJulian Elischer struct thread *td; 66804774f23SJulian Elischer struct thread *ctd; 6693e5da754SJohn Baldwin struct proc *p; 6703e5da754SJohn Baldwin 6713e5da754SJohn Baldwin /* 6723e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 6733e5da754SJohn Baldwin */ 674e0f66ef8SJohn Baldwin if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || 675e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 6763e5da754SJohn Baldwin return (EINVAL); 6773e5da754SJohn Baldwin 67804774f23SJulian Elischer ctd = curthread; 679e0f66ef8SJohn Baldwin it = ie->ie_thread; 680e0f66ef8SJohn Baldwin td = it->it_thread; 6816f40c417SRobert Watson p = td->td_proc; 682e0f66ef8SJohn Baldwin 6833e5da754SJohn Baldwin /* 6843e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 6853e5da754SJohn Baldwin * sources of entropy, then gather some. 6863e5da754SJohn Baldwin */ 687e0f66ef8SJohn Baldwin if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 6886f40c417SRobert Watson CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 6897ab24ea3SJulian Elischer p->p_pid, td->td_name); 690e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 691e0f66ef8SJohn Baldwin entropy.td = ctd; 6923e5da754SJohn Baldwin random_harvest(&entropy, sizeof(entropy), 2, 0, 6933e5da754SJohn Baldwin RANDOM_INTERRUPT); 6943e5da754SJohn Baldwin } 6953e5da754SJohn Baldwin 696e0f66ef8SJohn Baldwin KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 6973e5da754SJohn Baldwin 6983e5da754SJohn Baldwin /* 6993e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 700982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 701982d11f8SJeff Roberson * put it on the runqueue. 7023e5da754SJohn Baldwin */ 703e0f66ef8SJohn Baldwin it->it_need = 1; 704982d11f8SJeff Roberson thread_lock(td); 70571fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 706e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 7077ab24ea3SJulian Elischer td->td_name); 70871fad9fdSJulian Elischer TD_CLR_IWAIT(td); 709f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 7103e5da754SJohn Baldwin } else { 711e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 7127ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 7133e5da754SJohn Baldwin } 714982d11f8SJeff Roberson thread_unlock(td); 7153e5da754SJohn Baldwin 7163e5da754SJohn Baldwin return (0); 7173e5da754SJohn Baldwin } 718bafe5a31SPaolo Pisati #else 719bafe5a31SPaolo Pisati int 720bafe5a31SPaolo Pisati intr_event_remove_handler(void *cookie) 721bafe5a31SPaolo Pisati { 722bafe5a31SPaolo Pisati struct intr_handler *handler = (struct intr_handler *)cookie; 723bafe5a31SPaolo Pisati struct intr_event *ie; 724bafe5a31SPaolo Pisati struct intr_thread *it; 725bafe5a31SPaolo Pisati #ifdef INVARIANTS 726bafe5a31SPaolo Pisati struct intr_handler *ih; 727bafe5a31SPaolo Pisati #endif 728bafe5a31SPaolo Pisati #ifdef notyet 729bafe5a31SPaolo Pisati int dead; 730bafe5a31SPaolo Pisati #endif 731bafe5a31SPaolo Pisati 732bafe5a31SPaolo Pisati if (handler == NULL) 733bafe5a31SPaolo Pisati return (EINVAL); 734bafe5a31SPaolo Pisati ie = handler->ih_event; 735bafe5a31SPaolo Pisati KASSERT(ie != NULL, 736bafe5a31SPaolo Pisati ("interrupt handler \"%s\" has a NULL interrupt event", 737bafe5a31SPaolo Pisati handler->ih_name)); 738bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 739bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 740bafe5a31SPaolo Pisati ie->ie_name); 741bafe5a31SPaolo Pisati #ifdef INVARIANTS 742bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 743bafe5a31SPaolo Pisati if (ih == handler) 744bafe5a31SPaolo Pisati goto ok; 745bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 746bafe5a31SPaolo Pisati panic("interrupt handler \"%s\" not found in interrupt event \"%s\"", 747bafe5a31SPaolo Pisati ih->ih_name, ie->ie_name); 748bafe5a31SPaolo Pisati ok: 749bafe5a31SPaolo Pisati #endif 750bafe5a31SPaolo Pisati /* 751bafe5a31SPaolo Pisati * If there are no ithreads (per event and per handler), then 752bafe5a31SPaolo Pisati * just remove the handler and return. 753bafe5a31SPaolo Pisati * XXX: Note that an INTR_FAST handler might be running on another CPU! 754bafe5a31SPaolo Pisati */ 755bafe5a31SPaolo Pisati if (ie->ie_thread == NULL && handler->ih_thread == NULL) { 756bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 757bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 758bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 759bafe5a31SPaolo Pisati return (0); 760bafe5a31SPaolo Pisati } 761bafe5a31SPaolo Pisati 762bafe5a31SPaolo Pisati /* Private or global ithread? */ 763bafe5a31SPaolo Pisati it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread; 764bafe5a31SPaolo Pisati /* 765bafe5a31SPaolo Pisati * If the interrupt thread is already running, then just mark this 766bafe5a31SPaolo Pisati * handler as being dead and let the ithread do the actual removal. 767bafe5a31SPaolo Pisati * 768bafe5a31SPaolo Pisati * During a cold boot while cold is set, msleep() does not sleep, 769bafe5a31SPaolo Pisati * so we have to remove the handler here rather than letting the 770bafe5a31SPaolo Pisati * thread do it. 771bafe5a31SPaolo Pisati */ 772982d11f8SJeff Roberson thread_lock(it->it_thread); 773bafe5a31SPaolo Pisati if (!TD_AWAITING_INTR(it->it_thread) && !cold) { 774bafe5a31SPaolo Pisati handler->ih_flags |= IH_DEAD; 775bafe5a31SPaolo Pisati 776bafe5a31SPaolo Pisati /* 777bafe5a31SPaolo Pisati * Ensure that the thread will process the handler list 778bafe5a31SPaolo Pisati * again and remove this handler if it has already passed 779bafe5a31SPaolo Pisati * it on the list. 780bafe5a31SPaolo Pisati */ 781bafe5a31SPaolo Pisati it->it_need = 1; 782bafe5a31SPaolo Pisati } else 783bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next); 784982d11f8SJeff Roberson thread_unlock(it->it_thread); 785bafe5a31SPaolo Pisati while (handler->ih_flags & IH_DEAD) 786bafe5a31SPaolo Pisati msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 787bafe5a31SPaolo Pisati /* 788bafe5a31SPaolo Pisati * At this point, the handler has been disconnected from the event, 789bafe5a31SPaolo Pisati * so we can kill the private ithread if any. 790bafe5a31SPaolo Pisati */ 791bafe5a31SPaolo Pisati if (handler->ih_thread) { 792bafe5a31SPaolo Pisati ithread_destroy(handler->ih_thread); 793bafe5a31SPaolo Pisati handler->ih_thread = NULL; 794bafe5a31SPaolo Pisati } 795bafe5a31SPaolo Pisati intr_event_update(ie); 796bafe5a31SPaolo Pisati #ifdef notyet 797bafe5a31SPaolo Pisati /* 798bafe5a31SPaolo Pisati * XXX: This could be bad in the case of ppbus(8). Also, I think 799bafe5a31SPaolo Pisati * this could lead to races of stale data when servicing an 800bafe5a31SPaolo Pisati * interrupt. 801bafe5a31SPaolo Pisati */ 802bafe5a31SPaolo Pisati dead = 1; 803bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 804bafe5a31SPaolo Pisati if (handler != NULL) { 805bafe5a31SPaolo Pisati dead = 0; 806bafe5a31SPaolo Pisati break; 807bafe5a31SPaolo Pisati } 808bafe5a31SPaolo Pisati } 809bafe5a31SPaolo Pisati if (dead) { 810bafe5a31SPaolo Pisati ithread_destroy(ie->ie_thread); 811bafe5a31SPaolo Pisati ie->ie_thread = NULL; 812bafe5a31SPaolo Pisati } 813bafe5a31SPaolo Pisati #endif 814bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 815bafe5a31SPaolo Pisati free(handler, M_ITHREAD); 816bafe5a31SPaolo Pisati return (0); 817bafe5a31SPaolo Pisati } 818bafe5a31SPaolo Pisati 819bafe5a31SPaolo Pisati int 820bafe5a31SPaolo Pisati intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it) 821bafe5a31SPaolo Pisati { 822bafe5a31SPaolo Pisati struct intr_entropy entropy; 823bafe5a31SPaolo Pisati struct thread *td; 824bafe5a31SPaolo Pisati struct thread *ctd; 825bafe5a31SPaolo Pisati struct proc *p; 826bafe5a31SPaolo Pisati 827bafe5a31SPaolo Pisati /* 828bafe5a31SPaolo Pisati * If no ithread or no handlers, then we have a stray interrupt. 829bafe5a31SPaolo Pisati */ 830bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL) 831bafe5a31SPaolo Pisati return (EINVAL); 832bafe5a31SPaolo Pisati 833bafe5a31SPaolo Pisati ctd = curthread; 834bafe5a31SPaolo Pisati td = it->it_thread; 835bafe5a31SPaolo Pisati p = td->td_proc; 836bafe5a31SPaolo Pisati 837bafe5a31SPaolo Pisati /* 838bafe5a31SPaolo Pisati * If any of the handlers for this ithread claim to be good 839bafe5a31SPaolo Pisati * sources of entropy, then gather some. 840bafe5a31SPaolo Pisati */ 841bafe5a31SPaolo Pisati if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) { 842bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__, 8437ab24ea3SJulian Elischer p->p_pid, td->td_name); 844bafe5a31SPaolo Pisati entropy.event = (uintptr_t)ie; 845bafe5a31SPaolo Pisati entropy.td = ctd; 846bafe5a31SPaolo Pisati random_harvest(&entropy, sizeof(entropy), 2, 0, 847bafe5a31SPaolo Pisati RANDOM_INTERRUPT); 848bafe5a31SPaolo Pisati } 849bafe5a31SPaolo Pisati 850bafe5a31SPaolo Pisati KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name)); 851bafe5a31SPaolo Pisati 852bafe5a31SPaolo Pisati /* 853bafe5a31SPaolo Pisati * Set it_need to tell the thread to keep running if it is already 854982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 855982d11f8SJeff Roberson * put it on the runqueue. 856bafe5a31SPaolo Pisati */ 857bafe5a31SPaolo Pisati it->it_need = 1; 858982d11f8SJeff Roberson thread_lock(td); 859bafe5a31SPaolo Pisati if (TD_AWAITING_INTR(td)) { 860bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid, 8617ab24ea3SJulian Elischer th->th_name); 862bafe5a31SPaolo Pisati TD_CLR_IWAIT(td); 863bafe5a31SPaolo Pisati sched_add(td, SRQ_INTR); 864bafe5a31SPaolo Pisati } else { 865bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 8667ab24ea3SJulian Elischer __func__, p->p_pid, td->td_name, it->it_need, td->td_state); 867bafe5a31SPaolo Pisati } 868982d11f8SJeff Roberson thread_unlock(td); 869bafe5a31SPaolo Pisati 870bafe5a31SPaolo Pisati return (0); 871bafe5a31SPaolo Pisati } 872bafe5a31SPaolo Pisati #endif 8733e5da754SJohn Baldwin 874fe486a37SJohn Baldwin /* 875fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 876fe486a37SJohn Baldwin * is not specified, then a new event is created. 877fe486a37SJohn Baldwin */ 8783e5da754SJohn Baldwin int 879e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 880b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 8818088699fSJohn Baldwin { 882e0f66ef8SJohn Baldwin struct intr_event *ie; 883b4151f71SJohn Baldwin int error; 8848088699fSJohn Baldwin 885bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 8863e5da754SJohn Baldwin return (EINVAL); 8873e5da754SJohn Baldwin 888e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 8898088699fSJohn Baldwin 890e0f66ef8SJohn Baldwin if (ie != NULL) { 891e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 8923e5da754SJohn Baldwin return (EINVAL); 8933e5da754SJohn Baldwin } else { 894bafe5a31SPaolo Pisati #ifdef INTR_FILTER 895bafe5a31SPaolo Pisati error = intr_event_create(&ie, NULL, IE_SOFT, 896bafe5a31SPaolo Pisati NULL, NULL, NULL, "swi%d:", pri); 897bafe5a31SPaolo Pisati #else 898bafe5a31SPaolo Pisati error = intr_event_create(&ie, NULL, IE_SOFT, 899bafe5a31SPaolo Pisati NULL, "swi%d:", pri); 900bafe5a31SPaolo Pisati #endif 9018088699fSJohn Baldwin if (error) 902b4151f71SJohn Baldwin return (error); 903e0f66ef8SJohn Baldwin if (eventp != NULL) 904e0f66ef8SJohn Baldwin *eventp = ie; 9058088699fSJohn Baldwin } 906ef544f63SPaolo Pisati return (intr_event_add_handler(ie, name, NULL, handler, arg, 907d5a08a60SJake Burkholder (pri * RQ_PPQ) + PI_SOFT, flags, cookiep)); 908ed062c8dSJulian Elischer /* XXKSE.. think of a better way to get separate queues */ 9098088699fSJohn Baldwin } 9108088699fSJohn Baldwin 9111931cf94SJohn Baldwin /* 912e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 9131931cf94SJohn Baldwin */ 9141931cf94SJohn Baldwin void 915b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 9161931cf94SJohn Baldwin { 917e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 918e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 9193e5da754SJohn Baldwin int error; 9208088699fSJohn Baldwin 921e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 922e0f66ef8SJohn Baldwin ih->ih_need); 9231931cf94SJohn Baldwin 9241931cf94SJohn Baldwin /* 9253e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 9263e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 9273e5da754SJohn Baldwin * it will execute it the next time it runs. 9281931cf94SJohn Baldwin */ 929b4151f71SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 1); 9301ca2c018SBruce Evans 931b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 93267596082SAttilio Rao PCPU_INC(cnt.v_soft); 933bafe5a31SPaolo Pisati #ifdef INTR_FILTER 934bafe5a31SPaolo Pisati error = intr_event_schedule_thread(ie, ie->ie_thread); 935bafe5a31SPaolo Pisati #else 936e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 937bafe5a31SPaolo Pisati #endif 9383e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 9398088699fSJohn Baldwin } 9408088699fSJohn Baldwin } 9418088699fSJohn Baldwin 942fe486a37SJohn Baldwin /* 943fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 944fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 945fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 946fe486a37SJohn Baldwin * an optimal interface. 947fe486a37SJohn Baldwin */ 948fe486a37SJohn Baldwin int 949fe486a37SJohn Baldwin swi_remove(void *cookie) 950fe486a37SJohn Baldwin { 951fe486a37SJohn Baldwin 952fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 953fe486a37SJohn Baldwin } 954fe486a37SJohn Baldwin 955bafe5a31SPaolo Pisati #ifdef INTR_FILTER 956bafe5a31SPaolo Pisati static void 957bafe5a31SPaolo Pisati priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih) 958bafe5a31SPaolo Pisati { 959bafe5a31SPaolo Pisati struct intr_event *ie; 960bafe5a31SPaolo Pisati 961bafe5a31SPaolo Pisati ie = ih->ih_event; 962bafe5a31SPaolo Pisati /* 963bafe5a31SPaolo Pisati * If this handler is marked for death, remove it from 964bafe5a31SPaolo Pisati * the list of handlers and wake up the sleeper. 965bafe5a31SPaolo Pisati */ 966bafe5a31SPaolo Pisati if (ih->ih_flags & IH_DEAD) { 967bafe5a31SPaolo Pisati mtx_lock(&ie->ie_lock); 968bafe5a31SPaolo Pisati TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 969bafe5a31SPaolo Pisati ih->ih_flags &= ~IH_DEAD; 970bafe5a31SPaolo Pisati wakeup(ih); 971bafe5a31SPaolo Pisati mtx_unlock(&ie->ie_lock); 972bafe5a31SPaolo Pisati return; 973bafe5a31SPaolo Pisati } 974bafe5a31SPaolo Pisati 975bafe5a31SPaolo Pisati /* Execute this handler. */ 976bafe5a31SPaolo Pisati CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 977bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument, 978bafe5a31SPaolo Pisati ih->ih_name, ih->ih_flags); 979bafe5a31SPaolo Pisati 980bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 981bafe5a31SPaolo Pisati mtx_lock(&Giant); 982bafe5a31SPaolo Pisati ih->ih_handler(ih->ih_argument); 983bafe5a31SPaolo Pisati if (!(ih->ih_flags & IH_MPSAFE)) 984bafe5a31SPaolo Pisati mtx_unlock(&Giant); 985bafe5a31SPaolo Pisati } 986bafe5a31SPaolo Pisati #endif 987bafe5a31SPaolo Pisati 988e0f66ef8SJohn Baldwin static void 989e0f66ef8SJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 990e0f66ef8SJohn Baldwin { 991e0f66ef8SJohn Baldwin struct intr_handler *ih, *ihn; 992e0f66ef8SJohn Baldwin 993e0f66ef8SJohn Baldwin /* Interrupt handlers should not sleep. */ 994e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 995e0f66ef8SJohn Baldwin THREAD_NO_SLEEPING(); 996e0f66ef8SJohn Baldwin TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 997e0f66ef8SJohn Baldwin 998e0f66ef8SJohn Baldwin /* 999e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1000e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1001e0f66ef8SJohn Baldwin */ 1002e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1003e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1004e0f66ef8SJohn Baldwin TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next); 1005e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1006e0f66ef8SJohn Baldwin wakeup(ih); 1007e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1008e0f66ef8SJohn Baldwin continue; 1009e0f66ef8SJohn Baldwin } 1010e0f66ef8SJohn Baldwin 1011f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1012f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1013f2d619c8SPaolo Pisati continue; 1014f2d619c8SPaolo Pisati 1015e0f66ef8SJohn Baldwin /* 1016e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1017e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1018e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 1019e0f66ef8SJohn Baldwin */ 1020e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 1021e0f66ef8SJohn Baldwin if (!ih->ih_need) 1022e0f66ef8SJohn Baldwin continue; 1023e0f66ef8SJohn Baldwin else 1024e0f66ef8SJohn Baldwin atomic_store_rel_int(&ih->ih_need, 0); 1025e0f66ef8SJohn Baldwin } 1026e0f66ef8SJohn Baldwin 1027e0f66ef8SJohn Baldwin /* Execute this handler. */ 1028e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1029bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1030bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1031e0f66ef8SJohn Baldwin 1032e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1033e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1034e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1035e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1036e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1037e0f66ef8SJohn Baldwin } 1038e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1039e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1040e0f66ef8SJohn Baldwin 1041e0f66ef8SJohn Baldwin /* 1042e0f66ef8SJohn Baldwin * Interrupt storm handling: 1043e0f66ef8SJohn Baldwin * 1044e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1045e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1046e0f66ef8SJohn Baldwin * 1047e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1048e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1049e0f66ef8SJohn Baldwin * then enter storming mode. 1050e0f66ef8SJohn Baldwin */ 1051e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1052e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 10530ae62c18SNate Lawson /* Report the message only once every second. */ 10540ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1055e0f66ef8SJohn Baldwin printf( 10560ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1057e0f66ef8SJohn Baldwin ie->ie_name); 1058e0f66ef8SJohn Baldwin } 1059e41bcf3cSJohn Baldwin pause("istorm", 1); 1060e0f66ef8SJohn Baldwin } else 1061e0f66ef8SJohn Baldwin ie->ie_count++; 1062e0f66ef8SJohn Baldwin 1063e0f66ef8SJohn Baldwin /* 1064e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1065e0f66ef8SJohn Baldwin * the interrupt source. 1066e0f66ef8SJohn Baldwin */ 1067e0f66ef8SJohn Baldwin if (ie->ie_enable != NULL) 1068e0f66ef8SJohn Baldwin ie->ie_enable(ie->ie_source); 1069e0f66ef8SJohn Baldwin } 1070e0f66ef8SJohn Baldwin 1071bafe5a31SPaolo Pisati #ifndef INTR_FILTER 10728088699fSJohn Baldwin /* 1073b4151f71SJohn Baldwin * This is the main code for interrupt threads. 10748088699fSJohn Baldwin */ 107537c84183SPoul-Henning Kamp static void 1076b4151f71SJohn Baldwin ithread_loop(void *arg) 10778088699fSJohn Baldwin { 1078e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1079e0f66ef8SJohn Baldwin struct intr_event *ie; 1080b40ce416SJulian Elischer struct thread *td; 1081b4151f71SJohn Baldwin struct proc *p; 10828088699fSJohn Baldwin 1083b40ce416SJulian Elischer td = curthread; 1084b40ce416SJulian Elischer p = td->td_proc; 1085e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1086e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 108791f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1088e0f66ef8SJohn Baldwin ie = ithd->it_event; 1089e0f66ef8SJohn Baldwin ie->ie_count = 0; 10908088699fSJohn Baldwin 10918088699fSJohn Baldwin /* 10928088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 10938088699fSJohn Baldwin * list of handlers, giving each one a go at it. 10948088699fSJohn Baldwin */ 10958088699fSJohn Baldwin for (;;) { 1096b4151f71SJohn Baldwin /* 1097b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1098b4151f71SJohn Baldwin */ 1099b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1100e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 11017ab24ea3SJulian Elischer p->p_pid, td->td_name); 1102b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1103ca9a0ddfSJulian Elischer kthread_exit(); 1104b4151f71SJohn Baldwin } 1105b4151f71SJohn Baldwin 1106e0f66ef8SJohn Baldwin /* 1107e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1108e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1109e0f66ef8SJohn Baldwin * should make another pass. 1110e0f66ef8SJohn Baldwin */ 1111b4151f71SJohn Baldwin while (ithd->it_need) { 11128088699fSJohn Baldwin /* 1113e0f66ef8SJohn Baldwin * This might need a full read and write barrier 1114e0f66ef8SJohn Baldwin * to make sure that this write posts before any 1115e0f66ef8SJohn Baldwin * of the memory or device accesses in the 1116e0f66ef8SJohn Baldwin * handlers. 11178088699fSJohn Baldwin */ 1118b4151f71SJohn Baldwin atomic_store_rel_int(&ithd->it_need, 0); 1119e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 11208088699fSJohn Baldwin } 11217870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 11227870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 11238088699fSJohn Baldwin 11248088699fSJohn Baldwin /* 11258088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 11268088699fSJohn Baldwin * lock. This may take a while and it_need may get 11278088699fSJohn Baldwin * set again, so we have to check it again. 11288088699fSJohn Baldwin */ 1129982d11f8SJeff Roberson thread_lock(td); 1130e0f66ef8SJohn Baldwin if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 11317870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1132e0f66ef8SJohn Baldwin ie->ie_count = 0; 1133bf0acc27SJohn Baldwin mi_switch(SW_VOL, NULL); 11348088699fSJohn Baldwin } 1135982d11f8SJeff Roberson thread_unlock(td); 11368088699fSJohn Baldwin } 11371931cf94SJohn Baldwin } 1138bafe5a31SPaolo Pisati #else 1139bafe5a31SPaolo Pisati /* 1140bafe5a31SPaolo Pisati * This is the main code for interrupt threads. 1141bafe5a31SPaolo Pisati */ 1142bafe5a31SPaolo Pisati static void 1143bafe5a31SPaolo Pisati ithread_loop(void *arg) 1144bafe5a31SPaolo Pisati { 1145bafe5a31SPaolo Pisati struct intr_thread *ithd; 1146bafe5a31SPaolo Pisati struct intr_handler *ih; 1147bafe5a31SPaolo Pisati struct intr_event *ie; 1148bafe5a31SPaolo Pisati struct thread *td; 1149bafe5a31SPaolo Pisati struct proc *p; 1150bafe5a31SPaolo Pisati int priv; 1151bafe5a31SPaolo Pisati 1152bafe5a31SPaolo Pisati td = curthread; 1153bafe5a31SPaolo Pisati p = td->td_proc; 1154bafe5a31SPaolo Pisati ih = (struct intr_handler *)arg; 1155bafe5a31SPaolo Pisati priv = (ih->ih_thread != NULL) ? 1 : 0; 1156bafe5a31SPaolo Pisati ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread; 1157bafe5a31SPaolo Pisati KASSERT(ithd->it_thread == td, 1158bafe5a31SPaolo Pisati ("%s: ithread and proc linkage out of sync", __func__)); 1159bafe5a31SPaolo Pisati ie = ithd->it_event; 1160bafe5a31SPaolo Pisati ie->ie_count = 0; 1161bafe5a31SPaolo Pisati 1162bafe5a31SPaolo Pisati /* 1163bafe5a31SPaolo Pisati * As long as we have interrupts outstanding, go through the 1164bafe5a31SPaolo Pisati * list of handlers, giving each one a go at it. 1165bafe5a31SPaolo Pisati */ 1166bafe5a31SPaolo Pisati for (;;) { 1167bafe5a31SPaolo Pisati /* 1168bafe5a31SPaolo Pisati * If we are an orphaned thread, then just die. 1169bafe5a31SPaolo Pisati */ 1170bafe5a31SPaolo Pisati if (ithd->it_flags & IT_DEAD) { 1171bafe5a31SPaolo Pisati CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 11727ab24ea3SJulian Elischer p->p_pid, td->td_name); 1173bafe5a31SPaolo Pisati free(ithd, M_ITHREAD); 1174ca9a0ddfSJulian Elischer kthread_exit(); 1175bafe5a31SPaolo Pisati } 1176bafe5a31SPaolo Pisati 1177bafe5a31SPaolo Pisati /* 1178bafe5a31SPaolo Pisati * Service interrupts. If another interrupt arrives while 1179bafe5a31SPaolo Pisati * we are running, it will set it_need to note that we 1180bafe5a31SPaolo Pisati * should make another pass. 1181bafe5a31SPaolo Pisati */ 1182bafe5a31SPaolo Pisati while (ithd->it_need) { 1183bafe5a31SPaolo Pisati /* 1184bafe5a31SPaolo Pisati * This might need a full read and write barrier 1185bafe5a31SPaolo Pisati * to make sure that this write posts before any 1186bafe5a31SPaolo Pisati * of the memory or device accesses in the 1187bafe5a31SPaolo Pisati * handlers. 1188bafe5a31SPaolo Pisati */ 1189bafe5a31SPaolo Pisati atomic_store_rel_int(&ithd->it_need, 0); 1190bafe5a31SPaolo Pisati if (priv) 1191bafe5a31SPaolo Pisati priv_ithread_execute_handler(p, ih); 1192bafe5a31SPaolo Pisati else 1193bafe5a31SPaolo Pisati ithread_execute_handlers(p, ie); 1194bafe5a31SPaolo Pisati } 1195bafe5a31SPaolo Pisati WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 1196bafe5a31SPaolo Pisati mtx_assert(&Giant, MA_NOTOWNED); 1197bafe5a31SPaolo Pisati 1198bafe5a31SPaolo Pisati /* 1199bafe5a31SPaolo Pisati * Processed all our interrupts. Now get the sched 1200bafe5a31SPaolo Pisati * lock. This may take a while and it_need may get 1201bafe5a31SPaolo Pisati * set again, so we have to check it again. 1202bafe5a31SPaolo Pisati */ 1203982d11f8SJeff Roberson thread_lock(td); 1204bafe5a31SPaolo Pisati if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) { 1205bafe5a31SPaolo Pisati TD_SET_IWAIT(td); 1206bafe5a31SPaolo Pisati ie->ie_count = 0; 1207bafe5a31SPaolo Pisati mi_switch(SW_VOL, NULL); 1208bafe5a31SPaolo Pisati } 1209982d11f8SJeff Roberson thread_unlock(td); 1210bafe5a31SPaolo Pisati } 1211bafe5a31SPaolo Pisati } 1212bafe5a31SPaolo Pisati 1213bafe5a31SPaolo Pisati /* 1214bafe5a31SPaolo Pisati * Main loop for interrupt filter. 1215bafe5a31SPaolo Pisati * 1216bafe5a31SPaolo Pisati * Some architectures (i386, amd64 and arm) require the optional frame 1217bafe5a31SPaolo Pisati * parameter, and use it as the main argument for fast handler execution 1218bafe5a31SPaolo Pisati * when ih_argument == NULL. 1219bafe5a31SPaolo Pisati * 1220bafe5a31SPaolo Pisati * Return value: 1221bafe5a31SPaolo Pisati * o FILTER_STRAY: No filter recognized the event, and no 1222bafe5a31SPaolo Pisati * filter-less handler is registered on this 1223bafe5a31SPaolo Pisati * line. 1224bafe5a31SPaolo Pisati * o FILTER_HANDLED: A filter claimed the event and served it. 1225bafe5a31SPaolo Pisati * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at 1226bafe5a31SPaolo Pisati * least one filter-less handler on this line. 1227bafe5a31SPaolo Pisati * o FILTER_HANDLED | 1228bafe5a31SPaolo Pisati * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for 1229bafe5a31SPaolo Pisati * scheduling the per-handler ithread. 1230bafe5a31SPaolo Pisati * 1231bafe5a31SPaolo Pisati * In case an ithread has to be scheduled, in *ithd there will be a 1232bafe5a31SPaolo Pisati * pointer to a struct intr_thread containing the thread to be 1233bafe5a31SPaolo Pisati * scheduled. 1234bafe5a31SPaolo Pisati */ 1235bafe5a31SPaolo Pisati 1236bafe5a31SPaolo Pisati int 1237bafe5a31SPaolo Pisati intr_filter_loop(struct intr_event *ie, struct trapframe *frame, 1238bafe5a31SPaolo Pisati struct intr_thread **ithd) 1239bafe5a31SPaolo Pisati { 1240bafe5a31SPaolo Pisati struct intr_handler *ih; 1241bafe5a31SPaolo Pisati void *arg; 1242bafe5a31SPaolo Pisati int ret, thread_only; 1243bafe5a31SPaolo Pisati 1244bafe5a31SPaolo Pisati ret = 0; 1245bafe5a31SPaolo Pisati thread_only = 0; 1246bafe5a31SPaolo Pisati TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) { 1247bafe5a31SPaolo Pisati /* 1248bafe5a31SPaolo Pisati * Execute fast interrupt handlers directly. 1249bafe5a31SPaolo Pisati * To support clock handlers, if a handler registers 1250bafe5a31SPaolo Pisati * with a NULL argument, then we pass it a pointer to 1251bafe5a31SPaolo Pisati * a trapframe as its argument. 1252bafe5a31SPaolo Pisati */ 1253bafe5a31SPaolo Pisati arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument); 1254bafe5a31SPaolo Pisati 1255bafe5a31SPaolo Pisati CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__, 1256bafe5a31SPaolo Pisati ih->ih_filter, ih->ih_handler, arg, ih->ih_name); 1257bafe5a31SPaolo Pisati 1258bafe5a31SPaolo Pisati if (ih->ih_filter != NULL) 1259bafe5a31SPaolo Pisati ret = ih->ih_filter(arg); 1260bafe5a31SPaolo Pisati else { 1261bafe5a31SPaolo Pisati thread_only = 1; 1262bafe5a31SPaolo Pisati continue; 1263bafe5a31SPaolo Pisati } 1264bafe5a31SPaolo Pisati 1265bafe5a31SPaolo Pisati if (ret & FILTER_STRAY) 1266bafe5a31SPaolo Pisati continue; 1267bafe5a31SPaolo Pisati else { 1268bafe5a31SPaolo Pisati *ithd = ih->ih_thread; 1269bafe5a31SPaolo Pisati return (ret); 1270bafe5a31SPaolo Pisati } 1271bafe5a31SPaolo Pisati } 1272bafe5a31SPaolo Pisati 1273bafe5a31SPaolo Pisati /* 1274bafe5a31SPaolo Pisati * No filters handled the interrupt and we have at least 1275bafe5a31SPaolo Pisati * one handler without a filter. In this case, we schedule 1276bafe5a31SPaolo Pisati * all of the filter-less handlers to run in the ithread. 1277bafe5a31SPaolo Pisati */ 1278bafe5a31SPaolo Pisati if (thread_only) { 1279bafe5a31SPaolo Pisati *ithd = ie->ie_thread; 1280bafe5a31SPaolo Pisati return (FILTER_SCHEDULE_THREAD); 1281bafe5a31SPaolo Pisati } 1282bafe5a31SPaolo Pisati return (FILTER_STRAY); 1283bafe5a31SPaolo Pisati } 1284bafe5a31SPaolo Pisati 1285bafe5a31SPaolo Pisati /* 1286bafe5a31SPaolo Pisati * Main interrupt handling body. 1287bafe5a31SPaolo Pisati * 1288bafe5a31SPaolo Pisati * Input: 1289bafe5a31SPaolo Pisati * o ie: the event connected to this interrupt. 1290bafe5a31SPaolo Pisati * o frame: some archs (i.e. i386) pass a frame to some. 1291bafe5a31SPaolo Pisati * handlers as their main argument. 1292bafe5a31SPaolo Pisati * Return value: 1293bafe5a31SPaolo Pisati * o 0: everything ok. 1294bafe5a31SPaolo Pisati * o EINVAL: stray interrupt. 1295bafe5a31SPaolo Pisati */ 1296bafe5a31SPaolo Pisati int 1297bafe5a31SPaolo Pisati intr_event_handle(struct intr_event *ie, struct trapframe *frame) 1298bafe5a31SPaolo Pisati { 1299bafe5a31SPaolo Pisati struct intr_thread *ithd; 1300bafe5a31SPaolo Pisati struct thread *td; 1301bafe5a31SPaolo Pisati int thread; 1302bafe5a31SPaolo Pisati 1303bafe5a31SPaolo Pisati ithd = NULL; 1304bafe5a31SPaolo Pisati td = curthread; 1305bafe5a31SPaolo Pisati 1306bafe5a31SPaolo Pisati if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers)) 1307bafe5a31SPaolo Pisati return (EINVAL); 1308bafe5a31SPaolo Pisati 1309bafe5a31SPaolo Pisati td->td_intr_nesting_level++; 1310bafe5a31SPaolo Pisati thread = 0; 1311bafe5a31SPaolo Pisati critical_enter(); 1312bafe5a31SPaolo Pisati thread = intr_filter_loop(ie, frame, &ithd); 1313bafe5a31SPaolo Pisati 1314bafe5a31SPaolo Pisati /* 1315bafe5a31SPaolo Pisati * If the interrupt was fully served, send it an EOI but leave 1316bafe5a31SPaolo Pisati * it unmasked. Otherwise, mask the source as well as sending 1317bafe5a31SPaolo Pisati * it an EOI. 1318bafe5a31SPaolo Pisati */ 1319bafe5a31SPaolo Pisati if (thread & FILTER_HANDLED) { 1320bafe5a31SPaolo Pisati if (ie->ie_eoi != NULL) 1321bafe5a31SPaolo Pisati ie->ie_eoi(ie->ie_source); 1322bafe5a31SPaolo Pisati } else { 1323bafe5a31SPaolo Pisati if (ie->ie_disab != NULL) 1324bafe5a31SPaolo Pisati ie->ie_disab(ie->ie_source); 1325bafe5a31SPaolo Pisati } 1326bafe5a31SPaolo Pisati critical_exit(); 1327bafe5a31SPaolo Pisati 1328bafe5a31SPaolo Pisati /* Interrupt storm logic */ 1329bafe5a31SPaolo Pisati if (thread & FILTER_STRAY) { 1330bafe5a31SPaolo Pisati ie->ie_count++; 1331bafe5a31SPaolo Pisati if (ie->ie_count < intr_storm_threshold) 1332bafe5a31SPaolo Pisati printf("Interrupt stray detection not present\n"); 1333bafe5a31SPaolo Pisati } 1334bafe5a31SPaolo Pisati 1335bafe5a31SPaolo Pisati /* Schedule an ithread if needed. */ 1336bafe5a31SPaolo Pisati if (thread & FILTER_SCHEDULE_THREAD) { 1337bafe5a31SPaolo Pisati if (intr_event_schedule_thread(ie, ithd) != 0) 1338bafe5a31SPaolo Pisati panic("%s: impossible stray interrupt", __func__); 1339bafe5a31SPaolo Pisati } 1340bafe5a31SPaolo Pisati td->td_intr_nesting_level--; 1341bafe5a31SPaolo Pisati return (0); 1342bafe5a31SPaolo Pisati } 1343bafe5a31SPaolo Pisati #endif 13441931cf94SJohn Baldwin 13458b201c42SJohn Baldwin #ifdef DDB 13468b201c42SJohn Baldwin /* 13478b201c42SJohn Baldwin * Dump details about an interrupt handler 13488b201c42SJohn Baldwin */ 13498b201c42SJohn Baldwin static void 1350e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 13518b201c42SJohn Baldwin { 13528b201c42SJohn Baldwin int comma; 13538b201c42SJohn Baldwin 13548b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 13558b201c42SJohn Baldwin switch (ih->ih_pri) { 13568b201c42SJohn Baldwin case PI_REALTIME: 13578b201c42SJohn Baldwin db_printf("CLK "); 13588b201c42SJohn Baldwin break; 13598b201c42SJohn Baldwin case PI_AV: 13608b201c42SJohn Baldwin db_printf("AV "); 13618b201c42SJohn Baldwin break; 13628b201c42SJohn Baldwin case PI_TTYHIGH: 13638b201c42SJohn Baldwin case PI_TTYLOW: 13648b201c42SJohn Baldwin db_printf("TTY "); 13658b201c42SJohn Baldwin break; 13668b201c42SJohn Baldwin case PI_TAPE: 13678b201c42SJohn Baldwin db_printf("TAPE"); 13688b201c42SJohn Baldwin break; 13698b201c42SJohn Baldwin case PI_NET: 13708b201c42SJohn Baldwin db_printf("NET "); 13718b201c42SJohn Baldwin break; 13728b201c42SJohn Baldwin case PI_DISK: 13738b201c42SJohn Baldwin case PI_DISKLOW: 13748b201c42SJohn Baldwin db_printf("DISK"); 13758b201c42SJohn Baldwin break; 13768b201c42SJohn Baldwin case PI_DULL: 13778b201c42SJohn Baldwin db_printf("DULL"); 13788b201c42SJohn Baldwin break; 13798b201c42SJohn Baldwin default: 13808b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 13818b201c42SJohn Baldwin db_printf("SWI "); 13828b201c42SJohn Baldwin else 13838b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 13848b201c42SJohn Baldwin break; 13858b201c42SJohn Baldwin } 13868b201c42SJohn Baldwin db_printf(" "); 13878b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 13888b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 13898b201c42SJohn Baldwin if (ih->ih_need || 1390ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 13918b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 13928b201c42SJohn Baldwin db_printf(" {"); 13938b201c42SJohn Baldwin comma = 0; 13948b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 13958b201c42SJohn Baldwin if (comma) 13968b201c42SJohn Baldwin db_printf(", "); 13978b201c42SJohn Baldwin db_printf("EXCL"); 13988b201c42SJohn Baldwin comma = 1; 13998b201c42SJohn Baldwin } 14008b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 14018b201c42SJohn Baldwin if (comma) 14028b201c42SJohn Baldwin db_printf(", "); 14038b201c42SJohn Baldwin db_printf("ENTROPY"); 14048b201c42SJohn Baldwin comma = 1; 14058b201c42SJohn Baldwin } 14068b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 14078b201c42SJohn Baldwin if (comma) 14088b201c42SJohn Baldwin db_printf(", "); 14098b201c42SJohn Baldwin db_printf("DEAD"); 14108b201c42SJohn Baldwin comma = 1; 14118b201c42SJohn Baldwin } 14128b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 14138b201c42SJohn Baldwin if (comma) 14148b201c42SJohn Baldwin db_printf(", "); 14158b201c42SJohn Baldwin db_printf("MPSAFE"); 14168b201c42SJohn Baldwin comma = 1; 14178b201c42SJohn Baldwin } 14188b201c42SJohn Baldwin if (ih->ih_need) { 14198b201c42SJohn Baldwin if (comma) 14208b201c42SJohn Baldwin db_printf(", "); 14218b201c42SJohn Baldwin db_printf("NEED"); 14228b201c42SJohn Baldwin } 14238b201c42SJohn Baldwin db_printf("}"); 14248b201c42SJohn Baldwin } 14258b201c42SJohn Baldwin db_printf("\n"); 14268b201c42SJohn Baldwin } 14278b201c42SJohn Baldwin 14288b201c42SJohn Baldwin /* 1429e0f66ef8SJohn Baldwin * Dump details about a event. 14308b201c42SJohn Baldwin */ 14318b201c42SJohn Baldwin void 1432e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 14338b201c42SJohn Baldwin { 1434e0f66ef8SJohn Baldwin struct intr_handler *ih; 1435e0f66ef8SJohn Baldwin struct intr_thread *it; 14368b201c42SJohn Baldwin int comma; 14378b201c42SJohn Baldwin 1438e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1439e0f66ef8SJohn Baldwin it = ie->ie_thread; 1440e0f66ef8SJohn Baldwin if (it != NULL) 1441e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1442e0f66ef8SJohn Baldwin else 1443e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1444e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1445e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 14468b201c42SJohn Baldwin db_printf(" {"); 14478b201c42SJohn Baldwin comma = 0; 1448e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 14498b201c42SJohn Baldwin db_printf("SOFT"); 14508b201c42SJohn Baldwin comma = 1; 14518b201c42SJohn Baldwin } 1452e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 14538b201c42SJohn Baldwin if (comma) 14548b201c42SJohn Baldwin db_printf(", "); 14558b201c42SJohn Baldwin db_printf("ENTROPY"); 14568b201c42SJohn Baldwin comma = 1; 14578b201c42SJohn Baldwin } 1458e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 14598b201c42SJohn Baldwin if (comma) 14608b201c42SJohn Baldwin db_printf(", "); 1461e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 14628b201c42SJohn Baldwin comma = 1; 14638b201c42SJohn Baldwin } 1464e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 14658b201c42SJohn Baldwin if (comma) 14668b201c42SJohn Baldwin db_printf(", "); 14678b201c42SJohn Baldwin db_printf("NEED"); 14688b201c42SJohn Baldwin } 14698b201c42SJohn Baldwin db_printf("}"); 14708b201c42SJohn Baldwin } 14718b201c42SJohn Baldwin db_printf("\n"); 14728b201c42SJohn Baldwin 14738b201c42SJohn Baldwin if (handlers) 1474e0f66ef8SJohn Baldwin TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) 14758b201c42SJohn Baldwin db_dump_intrhand(ih); 14768b201c42SJohn Baldwin } 1477e0f66ef8SJohn Baldwin 1478e0f66ef8SJohn Baldwin /* 1479e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1480e0f66ef8SJohn Baldwin */ 1481e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1482e0f66ef8SJohn Baldwin { 1483e0f66ef8SJohn Baldwin struct intr_event *ie; 148419e9205aSJohn Baldwin int all, verbose; 1485e0f66ef8SJohn Baldwin 1486e0f66ef8SJohn Baldwin verbose = index(modif, 'v') != NULL; 1487e0f66ef8SJohn Baldwin all = index(modif, 'a') != NULL; 1488e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1489e0f66ef8SJohn Baldwin if (!all && TAILQ_EMPTY(&ie->ie_handlers)) 1490e0f66ef8SJohn Baldwin continue; 1491e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 149219e9205aSJohn Baldwin if (db_pager_quit) 149319e9205aSJohn Baldwin break; 1494e0f66ef8SJohn Baldwin } 1495e0f66ef8SJohn Baldwin } 14968b201c42SJohn Baldwin #endif /* DDB */ 14978b201c42SJohn Baldwin 1498b4151f71SJohn Baldwin /* 14998088699fSJohn Baldwin * Start standard software interrupt threads 15001931cf94SJohn Baldwin */ 15011931cf94SJohn Baldwin static void 1502b4151f71SJohn Baldwin start_softintr(void *dummy) 15031931cf94SJohn Baldwin { 15048804bf6bSJohn Baldwin struct proc *p; 1505b4151f71SJohn Baldwin 1506e0f66ef8SJohn Baldwin if (swi_add(&clk_intr_event, "clock", softclock, NULL, SWI_CLOCK, 1507b4151f71SJohn Baldwin INTR_MPSAFE, &softclock_ih) || 150879501b66SScott Long swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 1509b4151f71SJohn Baldwin panic("died while creating standard software ithreads"); 15103e5da754SJohn Baldwin 1511e0f66ef8SJohn Baldwin p = clk_intr_event->ie_thread->it_thread->td_proc; 15128804bf6bSJohn Baldwin PROC_LOCK(p); 15138804bf6bSJohn Baldwin p->p_flag |= P_NOLOAD; 15148804bf6bSJohn Baldwin PROC_UNLOCK(p); 15151931cf94SJohn Baldwin } 1516b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL) 15171931cf94SJohn Baldwin 1518d279178dSThomas Moestl /* 1519d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1520d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1521d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1522d279178dSThomas Moestl * independent. 1523d279178dSThomas Moestl * 1524d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1525d279178dSThomas Moestl * calculate things at run time. 1526d279178dSThomas Moestl */ 1527d279178dSThomas Moestl static int 1528d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1529d279178dSThomas Moestl { 1530d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 1531d279178dSThomas Moestl req)); 1532d279178dSThomas Moestl } 1533d279178dSThomas Moestl 1534d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1535d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1536d279178dSThomas Moestl 1537d279178dSThomas Moestl static int 1538d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1539d279178dSThomas Moestl { 1540d279178dSThomas Moestl return (sysctl_handle_opaque(oidp, intrcnt, 1541d279178dSThomas Moestl (char *)eintrcnt - (char *)intrcnt, req)); 1542d279178dSThomas Moestl } 1543d279178dSThomas Moestl 1544d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1545d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 15468b201c42SJohn Baldwin 15478b201c42SJohn Baldwin #ifdef DDB 15488b201c42SJohn Baldwin /* 15498b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 15508b201c42SJohn Baldwin */ 15518b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 15528b201c42SJohn Baldwin { 15538b201c42SJohn Baldwin u_long *i; 15548b201c42SJohn Baldwin char *cp; 15558b201c42SJohn Baldwin 15568b201c42SJohn Baldwin cp = intrnames; 155719e9205aSJohn Baldwin for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) { 15588b201c42SJohn Baldwin if (*cp == '\0') 15598b201c42SJohn Baldwin break; 15608b201c42SJohn Baldwin if (*i != 0) 15618b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 15628b201c42SJohn Baldwin cp += strlen(cp) + 1; 15638b201c42SJohn Baldwin } 15648b201c42SJohn Baldwin } 15658b201c42SJohn Baldwin #endif 1566