19454b2d8SWarner Losh /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4425f9fdaSStefan Eßer * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 5425f9fdaSStefan Eßer * All rights reserved. 6425f9fdaSStefan Eßer * 7425f9fdaSStefan Eßer * Redistribution and use in source and binary forms, with or without 8425f9fdaSStefan Eßer * modification, are permitted provided that the following conditions 9425f9fdaSStefan Eßer * are met: 10425f9fdaSStefan Eßer * 1. Redistributions of source code must retain the above copyright 11425f9fdaSStefan Eßer * notice unmodified, this list of conditions, and the following 12425f9fdaSStefan Eßer * disclaimer. 13425f9fdaSStefan Eßer * 2. Redistributions in binary form must reproduce the above copyright 14425f9fdaSStefan Eßer * notice, this list of conditions and the following disclaimer in the 15425f9fdaSStefan Eßer * documentation and/or other materials provided with the distribution. 16425f9fdaSStefan Eßer * 17425f9fdaSStefan Eßer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18425f9fdaSStefan Eßer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19425f9fdaSStefan Eßer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20425f9fdaSStefan Eßer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21425f9fdaSStefan Eßer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22425f9fdaSStefan Eßer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23425f9fdaSStefan Eßer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24425f9fdaSStefan Eßer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25425f9fdaSStefan Eßer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26425f9fdaSStefan Eßer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27425f9fdaSStefan Eßer */ 28425f9fdaSStefan Eßer 29677b542eSDavid E. O'Brien #include <sys/cdefs.h> 30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 313900ddb2SDoug Rabson 328b201c42SJohn Baldwin #include "opt_ddb.h" 33b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h" 348b201c42SJohn Baldwin 351c5bb3eaSPeter Wemm #include <sys/param.h> 369a94c9c5SJohn Baldwin #include <sys/bus.h> 37c11110eaSAlfred Perlstein #include <sys/conf.h> 389b33b154SJeff Roberson #include <sys/cpuset.h> 399a94c9c5SJohn Baldwin #include <sys/rtprio.h> 40425f9fdaSStefan Eßer #include <sys/systm.h> 4168352337SDoug Rabson #include <sys/interrupt.h> 421931cf94SJohn Baldwin #include <sys/kernel.h> 431931cf94SJohn Baldwin #include <sys/kthread.h> 441931cf94SJohn Baldwin #include <sys/ktr.h> 4505b2c96fSBruce Evans #include <sys/limits.h> 46f34fa851SJohn Baldwin #include <sys/lock.h> 471931cf94SJohn Baldwin #include <sys/malloc.h> 4835e0e5b3SJohn Baldwin #include <sys/mutex.h> 49cebc7fb1SJohn Baldwin #include <sys/priv.h> 501931cf94SJohn Baldwin #include <sys/proc.h> 51*511d1afbSGleb Smirnoff #include <sys/epoch.h> 523e5da754SJohn Baldwin #include <sys/random.h> 53b4151f71SJohn Baldwin #include <sys/resourcevar.h> 5463710c4dSJohn Baldwin #include <sys/sched.h> 55eaf86d16SJohn Baldwin #include <sys/smp.h> 56d279178dSThomas Moestl #include <sys/sysctl.h> 576205924aSKip Macy #include <sys/syslog.h> 581931cf94SJohn Baldwin #include <sys/unistd.h> 591931cf94SJohn Baldwin #include <sys/vmmeter.h> 601931cf94SJohn Baldwin #include <machine/atomic.h> 611931cf94SJohn Baldwin #include <machine/cpu.h> 628088699fSJohn Baldwin #include <machine/md_var.h> 63b4151f71SJohn Baldwin #include <machine/stdarg.h> 648b201c42SJohn Baldwin #ifdef DDB 658b201c42SJohn Baldwin #include <ddb/ddb.h> 668b201c42SJohn Baldwin #include <ddb/db_sym.h> 678b201c42SJohn Baldwin #endif 68425f9fdaSStefan Eßer 69e0f66ef8SJohn Baldwin /* 70e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 71e0f66ef8SJohn Baldwin */ 72e0f66ef8SJohn Baldwin struct intr_thread { 73e0f66ef8SJohn Baldwin struct intr_event *it_event; 74e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 75e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 76e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 773e5da754SJohn Baldwin }; 783e5da754SJohn Baldwin 79e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 80e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 81e4cd31ddSJeff Roberson #define IT_WAIT 0x000002 /* Thread is waiting for completion. */ 82e0f66ef8SJohn Baldwin 83e0f66ef8SJohn Baldwin struct intr_entropy { 84e0f66ef8SJohn Baldwin struct thread *td; 85e0f66ef8SJohn Baldwin uintptr_t event; 86e0f66ef8SJohn Baldwin }; 87e0f66ef8SJohn Baldwin 88e0f66ef8SJohn Baldwin struct intr_event *tty_intr_event; 897b1fe905SBruce Evans void *vm_ih; 907ab24ea3SJulian Elischer struct proc *intrproc; 911931cf94SJohn Baldwin 92b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 93b4151f71SJohn Baldwin 945d0e8299SConrad Meyer static int intr_storm_threshold = 0; 95af3b2549SHans Petter Selasky SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN, 967870c3c6SJohn Baldwin &intr_storm_threshold, 0, 977b1fe905SBruce Evans "Number of consecutive interrupts before storm protection is enabled"); 98*511d1afbSGleb Smirnoff static int intr_epoch_batch = 1000; 99*511d1afbSGleb Smirnoff SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch, 100*511d1afbSGleb Smirnoff 0, "Maximum interrupt handler executions without re-entering epoch(9)"); 101e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 102e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 1039b33b154SJeff Roberson static struct mtx event_lock; 1049b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 1057b1fe905SBruce Evans 106e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 1071ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie); 108e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 109e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 110bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 111bafe5a31SPaolo Pisati struct intr_event *ie); 1127b1fe905SBruce Evans static void ithread_loop(void *); 113e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1147b1fe905SBruce Evans static void start_softintr(void *); 1157870c3c6SJohn Baldwin 116bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 117b4151f71SJohn Baldwin u_char 118e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1199a94c9c5SJohn Baldwin { 120b4151f71SJohn Baldwin u_char pri; 1219a94c9c5SJohn Baldwin 122b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1235a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1249a94c9c5SJohn Baldwin switch (flags) { 125b4151f71SJohn Baldwin case INTR_TYPE_TTY: 126d3305205SJohn Baldwin pri = PI_TTY; 1279a94c9c5SJohn Baldwin break; 1289a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1299a94c9c5SJohn Baldwin pri = PI_DISK; 1309a94c9c5SJohn Baldwin break; 1319a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1329a94c9c5SJohn Baldwin pri = PI_NET; 1339a94c9c5SJohn Baldwin break; 1349a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 135d3305205SJohn Baldwin pri = PI_DISK; 1369a94c9c5SJohn Baldwin break; 137d3305205SJohn Baldwin case INTR_TYPE_AV: 1385a280d9cSPeter Wemm pri = PI_AV; 1395a280d9cSPeter Wemm break; 140b4151f71SJohn Baldwin case INTR_TYPE_CLK: 141b4151f71SJohn Baldwin pri = PI_REALTIME; 142b4151f71SJohn Baldwin break; 1439a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1449a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1459a94c9c5SJohn Baldwin break; 1469a94c9c5SJohn Baldwin default: 147b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 148e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1499a94c9c5SJohn Baldwin } 1509a94c9c5SJohn Baldwin 1519a94c9c5SJohn Baldwin return pri; 1529a94c9c5SJohn Baldwin } 1539a94c9c5SJohn Baldwin 154b4151f71SJohn Baldwin /* 155e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 156b4151f71SJohn Baldwin */ 157b4151f71SJohn Baldwin static void 158e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 159b4151f71SJohn Baldwin { 160e0f66ef8SJohn Baldwin struct intr_event *ie; 161b40ce416SJulian Elischer struct thread *td; 162e0f66ef8SJohn Baldwin u_char pri; 1638088699fSJohn Baldwin 164e0f66ef8SJohn Baldwin ie = ithd->it_event; 165e0f66ef8SJohn Baldwin td = ithd->it_thread; 166111b043cSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 167b4151f71SJohn Baldwin 168e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 169111b043cSAndriy Gapon if (CK_SLIST_EMPTY(&ie->ie_handlers)) 170e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 171e0f66ef8SJohn Baldwin else 172111b043cSAndriy Gapon pri = CK_SLIST_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)); 17644ad5475SJohn Baldwin #ifdef KTR 17744ad5475SJohn Baldwin sched_clear_tdname(td); 17844ad5475SJohn Baldwin #endif 179982d11f8SJeff Roberson thread_lock(td); 180e0f66ef8SJohn Baldwin sched_prio(td, pri); 181982d11f8SJeff Roberson thread_unlock(td); 182b4151f71SJohn Baldwin } 183e0f66ef8SJohn Baldwin 184e0f66ef8SJohn Baldwin /* 185e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 186e0f66ef8SJohn Baldwin */ 187e0f66ef8SJohn Baldwin static void 188e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 189e0f66ef8SJohn Baldwin { 190e0f66ef8SJohn Baldwin struct intr_handler *ih; 191e0f66ef8SJohn Baldwin char *last; 192e0f66ef8SJohn Baldwin int missed, space; 193e0f66ef8SJohn Baldwin 194e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 195e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 196e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 197c4eb6630SGleb Smirnoff ie->ie_hflags = 0; 1980811d60aSJohn Baldwin missed = 0; 199e0f66ef8SJohn Baldwin space = 1; 200e0f66ef8SJohn Baldwin 201e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 202111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 2036d51c0feSIan Lepore if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 204e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 205e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 206e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 207e0f66ef8SJohn Baldwin space = 0; 2080811d60aSJohn Baldwin } else 2090811d60aSJohn Baldwin missed++; 210c4eb6630SGleb Smirnoff ie->ie_hflags |= ih->ih_flags; 2110811d60aSJohn Baldwin } 212e0f66ef8SJohn Baldwin 213e0f66ef8SJohn Baldwin /* 21467da50a0SIan Lepore * If there is only one handler and its name is too long, just copy in 21567da50a0SIan Lepore * as much of the end of the name (includes the unit number) as will 21667da50a0SIan Lepore * fit. Otherwise, we have multiple handlers and not all of the names 21767da50a0SIan Lepore * will fit. Add +'s to indicate missing names. If we run out of room 21867da50a0SIan Lepore * and still have +'s to add, change the last character from a + to a *. 219e0f66ef8SJohn Baldwin */ 22067da50a0SIan Lepore if (missed == 1 && space == 1) { 22167da50a0SIan Lepore ih = CK_SLIST_FIRST(&ie->ie_handlers); 22267da50a0SIan Lepore missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 - 22367da50a0SIan Lepore sizeof(ie->ie_fullname); 22467da50a0SIan Lepore strcat(ie->ie_fullname, (missed == 0) ? " " : "-"); 22567da50a0SIan Lepore strcat(ie->ie_fullname, &ih->ih_name[missed]); 22667da50a0SIan Lepore missed = 0; 22767da50a0SIan Lepore } 228e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2290811d60aSJohn Baldwin while (missed-- > 0) { 230e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 231e0f66ef8SJohn Baldwin if (*last == '+') { 232e0f66ef8SJohn Baldwin *last = '*'; 233e0f66ef8SJohn Baldwin break; 234b4151f71SJohn Baldwin } else 235e0f66ef8SJohn Baldwin *last = '+'; 236e0f66ef8SJohn Baldwin } else if (space) { 237e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 238e0f66ef8SJohn Baldwin space = 0; 239e0f66ef8SJohn Baldwin } else 240e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 241b4151f71SJohn Baldwin } 242e0f66ef8SJohn Baldwin 243e0f66ef8SJohn Baldwin /* 244e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 245e0f66ef8SJohn Baldwin * name. 246e0f66ef8SJohn Baldwin */ 247e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 248e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 249e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 250b4151f71SJohn Baldwin } 251b4151f71SJohn Baldwin 252b4151f71SJohn Baldwin int 2539b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2541ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 255066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2561ee1b687SJohn Baldwin const char *fmt, ...) 257bafe5a31SPaolo Pisati { 258bafe5a31SPaolo Pisati struct intr_event *ie; 259bafe5a31SPaolo Pisati va_list ap; 260bafe5a31SPaolo Pisati 261bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 262bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 263bafe5a31SPaolo Pisati return (EINVAL); 264bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 265bafe5a31SPaolo Pisati ie->ie_source = source; 2661ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2671ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2681ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2696d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 270bafe5a31SPaolo Pisati ie->ie_flags = flags; 2719b33b154SJeff Roberson ie->ie_irq = irq; 272eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 273111b043cSAndriy Gapon CK_SLIST_INIT(&ie->ie_handlers); 274bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 275bafe5a31SPaolo Pisati 276bafe5a31SPaolo Pisati va_start(ap, fmt); 277bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 278bafe5a31SPaolo Pisati va_end(ap); 279bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2809b33b154SJeff Roberson mtx_lock(&event_lock); 281bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2829b33b154SJeff Roberson mtx_unlock(&event_lock); 283bafe5a31SPaolo Pisati if (event != NULL) 284bafe5a31SPaolo Pisati *event = ie; 285bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 286bafe5a31SPaolo Pisati return (0); 287bafe5a31SPaolo Pisati } 288b4151f71SJohn Baldwin 289eaf86d16SJohn Baldwin /* 290eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 291eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 29229dfb631SConrad Meyer * platforms this request will fail. Using a cpu id of NOCPU unbinds 293eaf86d16SJohn Baldwin * the interrupt event. 294eaf86d16SJohn Baldwin */ 29529dfb631SConrad Meyer static int 29629dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread) 297eaf86d16SJohn Baldwin { 2989b33b154SJeff Roberson lwpid_t id; 299eaf86d16SJohn Baldwin int error; 300eaf86d16SJohn Baldwin 301eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 302eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 303eaf86d16SJohn Baldwin return (EINVAL); 304eaf86d16SJohn Baldwin 305eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 306eaf86d16SJohn Baldwin return (EOPNOTSUPP); 307cebc7fb1SJohn Baldwin 308cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 309cebc7fb1SJohn Baldwin if (error) 310cebc7fb1SJohn Baldwin return (error); 311cebc7fb1SJohn Baldwin 3129b33b154SJeff Roberson /* 313cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 314cebc7fb1SJohn Baldwin * permissions, etc. 3159b33b154SJeff Roberson */ 31629dfb631SConrad Meyer if (bindithread) { 317eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3189b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3199b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 320eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32181198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3229b33b154SJeff Roberson if (error) 3239b33b154SJeff Roberson return (error); 3249b33b154SJeff Roberson } else 325eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32629dfb631SConrad Meyer } 32729dfb631SConrad Meyer if (bindirq) 328eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 329cebc7fb1SJohn Baldwin if (error) { 33029dfb631SConrad Meyer if (bindithread) { 331cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 332cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 33381198539SAlexander V. Chernikov cpu = ie->ie_cpu; 334cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 335cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33681198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 337cebc7fb1SJohn Baldwin } else 338cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33929dfb631SConrad Meyer } 340eaf86d16SJohn Baldwin return (error); 341cebc7fb1SJohn Baldwin } 342cebc7fb1SJohn Baldwin 34329dfb631SConrad Meyer if (bindirq) { 344eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 345eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3469b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 34729dfb631SConrad Meyer } 3489b33b154SJeff Roberson 3499b33b154SJeff Roberson return (error); 3509b33b154SJeff Roberson } 3519b33b154SJeff Roberson 35229dfb631SConrad Meyer /* 35329dfb631SConrad Meyer * Bind an interrupt event to the specified CPU. For supported platforms, any 35429dfb631SConrad Meyer * associated ithreads as well as the primary interrupt context will be bound 35529dfb631SConrad Meyer * to the specificed CPU. 35629dfb631SConrad Meyer */ 35729dfb631SConrad Meyer int 35829dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu) 35929dfb631SConrad Meyer { 36029dfb631SConrad Meyer 36129dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, true)); 36229dfb631SConrad Meyer } 36329dfb631SConrad Meyer 36429dfb631SConrad Meyer /* 36529dfb631SConrad Meyer * Bind an interrupt event to the specified CPU, but do not bind associated 36629dfb631SConrad Meyer * ithreads. 36729dfb631SConrad Meyer */ 36829dfb631SConrad Meyer int 36929dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu) 37029dfb631SConrad Meyer { 37129dfb631SConrad Meyer 37229dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, false)); 37329dfb631SConrad Meyer } 37429dfb631SConrad Meyer 37529dfb631SConrad Meyer /* 37629dfb631SConrad Meyer * Bind an interrupt event's ithread to the specified CPU. 37729dfb631SConrad Meyer */ 37829dfb631SConrad Meyer int 37929dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu) 38029dfb631SConrad Meyer { 38129dfb631SConrad Meyer 38229dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, false, true)); 38329dfb631SConrad Meyer } 38429dfb631SConrad Meyer 3854e255d74SAndrew Gallatin /* 3864e255d74SAndrew Gallatin * Bind an interrupt event's ithread to the specified cpuset. 3874e255d74SAndrew Gallatin */ 3884e255d74SAndrew Gallatin int 3894e255d74SAndrew Gallatin intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs) 3904e255d74SAndrew Gallatin { 3914e255d74SAndrew Gallatin lwpid_t id; 3924e255d74SAndrew Gallatin 3934e255d74SAndrew Gallatin mtx_lock(&ie->ie_lock); 3944e255d74SAndrew Gallatin if (ie->ie_thread != NULL) { 3954e255d74SAndrew Gallatin id = ie->ie_thread->it_thread->td_tid; 3964e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 3974e255d74SAndrew Gallatin return (cpuset_setthread(id, cs)); 3984e255d74SAndrew Gallatin } else { 3994e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 4004e255d74SAndrew Gallatin } 4014e255d74SAndrew Gallatin return (ENODEV); 4024e255d74SAndrew Gallatin } 4034e255d74SAndrew Gallatin 4049b33b154SJeff Roberson static struct intr_event * 4059b33b154SJeff Roberson intr_lookup(int irq) 4069b33b154SJeff Roberson { 4079b33b154SJeff Roberson struct intr_event *ie; 4089b33b154SJeff Roberson 4099b33b154SJeff Roberson mtx_lock(&event_lock); 4109b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 4119b33b154SJeff Roberson if (ie->ie_irq == irq && 4129b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 413111b043cSAndriy Gapon CK_SLIST_FIRST(&ie->ie_handlers) != NULL) 4149b33b154SJeff Roberson break; 4159b33b154SJeff Roberson mtx_unlock(&event_lock); 4169b33b154SJeff Roberson return (ie); 4179b33b154SJeff Roberson } 4189b33b154SJeff Roberson 4199b33b154SJeff Roberson int 42029dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m) 4219b33b154SJeff Roberson { 4229b33b154SJeff Roberson struct intr_event *ie; 4239b33b154SJeff Roberson cpuset_t *mask; 4243fe93b94SAdrian Chadd int cpu, n; 4259b33b154SJeff Roberson 4269b33b154SJeff Roberson mask = m; 4279b33b154SJeff Roberson cpu = NOCPU; 4289b33b154SJeff Roberson /* 4299b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 4309b33b154SJeff Roberson * only one cpu is in the set. 4319b33b154SJeff Roberson */ 4329b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 4339b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 4349b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 4359b33b154SJeff Roberson continue; 4369b33b154SJeff Roberson if (cpu != NOCPU) 4379b33b154SJeff Roberson return (EINVAL); 4383fe93b94SAdrian Chadd cpu = n; 4399b33b154SJeff Roberson } 4409b33b154SJeff Roberson } 4419b33b154SJeff Roberson ie = intr_lookup(irq); 4429b33b154SJeff Roberson if (ie == NULL) 4439b33b154SJeff Roberson return (ESRCH); 44429dfb631SConrad Meyer switch (mode) { 44529dfb631SConrad Meyer case CPU_WHICH_IRQ: 4469bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 44729dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 44829dfb631SConrad Meyer return (intr_event_bind_irqonly(ie, cpu)); 44929dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 45029dfb631SConrad Meyer return (intr_event_bind_ithread(ie, cpu)); 45129dfb631SConrad Meyer default: 45229dfb631SConrad Meyer return (EINVAL); 45329dfb631SConrad Meyer } 4549b33b154SJeff Roberson } 4559b33b154SJeff Roberson 4569b33b154SJeff Roberson int 45729dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m) 4589b33b154SJeff Roberson { 4599b33b154SJeff Roberson struct intr_event *ie; 46029dfb631SConrad Meyer struct thread *td; 46129dfb631SConrad Meyer struct proc *p; 4629b33b154SJeff Roberson cpuset_t *mask; 46329dfb631SConrad Meyer lwpid_t id; 46429dfb631SConrad Meyer int error; 4659b33b154SJeff Roberson 4669b33b154SJeff Roberson mask = m; 4679b33b154SJeff Roberson ie = intr_lookup(irq); 4689b33b154SJeff Roberson if (ie == NULL) 4699b33b154SJeff Roberson return (ESRCH); 47029dfb631SConrad Meyer 47129dfb631SConrad Meyer error = 0; 4729b33b154SJeff Roberson CPU_ZERO(mask); 47329dfb631SConrad Meyer switch (mode) { 47429dfb631SConrad Meyer case CPU_WHICH_IRQ: 47529dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 4769b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4779b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4789b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4799b33b154SJeff Roberson else 4809b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 481eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 48229dfb631SConrad Meyer break; 48329dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 48429dfb631SConrad Meyer mtx_lock(&ie->ie_lock); 48529dfb631SConrad Meyer if (ie->ie_thread == NULL) { 48629dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 48729dfb631SConrad Meyer CPU_COPY(cpuset_root, mask); 48829dfb631SConrad Meyer } else { 48929dfb631SConrad Meyer id = ie->ie_thread->it_thread->td_tid; 49029dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 49129dfb631SConrad Meyer error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL); 49229dfb631SConrad Meyer if (error != 0) 49329dfb631SConrad Meyer return (error); 49429dfb631SConrad Meyer CPU_COPY(&td->td_cpuset->cs_mask, mask); 49529dfb631SConrad Meyer PROC_UNLOCK(p); 49629dfb631SConrad Meyer } 49729dfb631SConrad Meyer default: 49829dfb631SConrad Meyer return (EINVAL); 49929dfb631SConrad Meyer } 500eaf86d16SJohn Baldwin return (0); 501eaf86d16SJohn Baldwin } 502eaf86d16SJohn Baldwin 503b4151f71SJohn Baldwin int 504e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 505b4151f71SJohn Baldwin { 506b4151f71SJohn Baldwin 5079b33b154SJeff Roberson mtx_lock(&event_lock); 508e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 509111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 510e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5119b33b154SJeff Roberson mtx_unlock(&event_lock); 512e0f66ef8SJohn Baldwin return (EBUSY); 5134d29cb2dSJohn Baldwin } 514e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 5159477358dSJohn Baldwin #ifndef notyet 5169477358dSJohn Baldwin if (ie->ie_thread != NULL) { 5179477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 5189477358dSJohn Baldwin ie->ie_thread = NULL; 5199477358dSJohn Baldwin } 5209477358dSJohn Baldwin #endif 521e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5229b33b154SJeff Roberson mtx_unlock(&event_lock); 523e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 524e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 525e0f66ef8SJohn Baldwin return (0); 526e0f66ef8SJohn Baldwin } 527e0f66ef8SJohn Baldwin 528e0f66ef8SJohn Baldwin static struct intr_thread * 529e0f66ef8SJohn Baldwin ithread_create(const char *name) 530e0f66ef8SJohn Baldwin { 531e0f66ef8SJohn Baldwin struct intr_thread *ithd; 532e0f66ef8SJohn Baldwin struct thread *td; 533e0f66ef8SJohn Baldwin int error; 534e0f66ef8SJohn Baldwin 535e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 536e0f66ef8SJohn Baldwin 5377ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 5387ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 5399ef95d01SJulian Elischer 0, "intr", "%s", name); 540e0f66ef8SJohn Baldwin if (error) 5413745c395SJulian Elischer panic("kproc_create() failed with %d", error); 542982d11f8SJeff Roberson thread_lock(td); 543ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 544e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 545982d11f8SJeff Roberson thread_unlock(td); 546e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 547e0f66ef8SJohn Baldwin ithd->it_thread = td; 548e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 549e0f66ef8SJohn Baldwin return (ithd); 550e0f66ef8SJohn Baldwin } 551e0f66ef8SJohn Baldwin 552e0f66ef8SJohn Baldwin static void 553e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 554e0f66ef8SJohn Baldwin { 555e0f66ef8SJohn Baldwin struct thread *td; 556e0f66ef8SJohn Baldwin 557bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 558e0f66ef8SJohn Baldwin td = ithread->it_thread; 559982d11f8SJeff Roberson thread_lock(td); 560e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 56171fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 56271fad9fdSJulian Elischer TD_CLR_IWAIT(td); 563f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 56461a74c5cSJeff Roberson } else 565982d11f8SJeff Roberson thread_unlock(td); 566b4151f71SJohn Baldwin } 567b4151f71SJohn Baldwin 568b4151f71SJohn Baldwin int 569e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 570ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 571ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 572b4151f71SJohn Baldwin { 573e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 574111b043cSAndriy Gapon struct intr_handler **prevptr; 575e0f66ef8SJohn Baldwin struct intr_thread *it; 576b4151f71SJohn Baldwin 577ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 578b4151f71SJohn Baldwin return (EINVAL); 579b4151f71SJohn Baldwin 580e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 581e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 582ef544f63SPaolo Pisati ih->ih_filter = filter; 583b4151f71SJohn Baldwin ih->ih_handler = handler; 584b4151f71SJohn Baldwin ih->ih_argument = arg; 58537b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 586e0f66ef8SJohn Baldwin ih->ih_event = ie; 587b4151f71SJohn Baldwin ih->ih_pri = pri; 588ef544f63SPaolo Pisati if (flags & INTR_EXCL) 589b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 590b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 591b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 592b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 593b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 594*511d1afbSGleb Smirnoff if (flags & INTR_TYPE_NET) 595*511d1afbSGleb Smirnoff ih->ih_flags |= IH_NET; 596b4151f71SJohn Baldwin 597e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 598e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 599111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 600e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 601111b043cSAndriy Gapon (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 602e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 603b4151f71SJohn Baldwin free(ih, M_ITHREAD); 604b4151f71SJohn Baldwin return (EINVAL); 605b4151f71SJohn Baldwin } 606e0f66ef8SJohn Baldwin } 607e0f66ef8SJohn Baldwin 608e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 609ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 610e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 6110f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 612e0f66ef8SJohn Baldwin else { 613e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 614e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 615e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 616e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 617e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 618e0f66ef8SJohn Baldwin ie->ie_thread = it; 619e0f66ef8SJohn Baldwin it->it_event = ie; 620e0f66ef8SJohn Baldwin ithread_update(it); 621e0f66ef8SJohn Baldwin wakeup(ie); 622e0f66ef8SJohn Baldwin } 623e0f66ef8SJohn Baldwin } 624c9516c94SAlexander Kabaev 625c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 626111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) { 627c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 628c9516c94SAlexander Kabaev break; 629c9516c94SAlexander Kabaev } 630111b043cSAndriy Gapon CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next); 631111b043cSAndriy Gapon 632c9516c94SAlexander Kabaev intr_event_update(ie); 633c9516c94SAlexander Kabaev 634e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 635e0f66ef8SJohn Baldwin ie->ie_name); 636e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 637e0f66ef8SJohn Baldwin 638e0f66ef8SJohn Baldwin if (cookiep != NULL) 639e0f66ef8SJohn Baldwin *cookiep = ih; 640e0f66ef8SJohn Baldwin return (0); 641e0f66ef8SJohn Baldwin } 642b4151f71SJohn Baldwin 643c3045318SJohn Baldwin /* 64437b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 64537b8ef16SJohn Baldwin * interrupt handler. 64637b8ef16SJohn Baldwin */ 64737b8ef16SJohn Baldwin int 64837b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 64937b8ef16SJohn Baldwin const char *descr) 65037b8ef16SJohn Baldwin { 65137b8ef16SJohn Baldwin struct intr_handler *ih; 65237b8ef16SJohn Baldwin size_t space; 65337b8ef16SJohn Baldwin char *start; 65437b8ef16SJohn Baldwin 65537b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 65637b8ef16SJohn Baldwin #ifdef INVARIANTS 657111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 65837b8ef16SJohn Baldwin if (ih == cookie) 65937b8ef16SJohn Baldwin break; 66037b8ef16SJohn Baldwin } 66137b8ef16SJohn Baldwin if (ih == NULL) { 66237b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 663d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 66437b8ef16SJohn Baldwin } 66537b8ef16SJohn Baldwin #endif 66637b8ef16SJohn Baldwin ih = cookie; 66737b8ef16SJohn Baldwin 66837b8ef16SJohn Baldwin /* 66937b8ef16SJohn Baldwin * Look for an existing description by checking for an 67037b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 67137b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 67237b8ef16SJohn Baldwin * description at that point. If one is not found, find the 67337b8ef16SJohn Baldwin * end of the name to use as the insertion point. 67437b8ef16SJohn Baldwin */ 675dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 67637b8ef16SJohn Baldwin if (start == NULL) 677dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 67837b8ef16SJohn Baldwin 67937b8ef16SJohn Baldwin /* 68037b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 68137b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 68237b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 68337b8ef16SJohn Baldwin */ 68437b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 68537b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 68637b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 68737b8ef16SJohn Baldwin return (ENOSPC); 68837b8ef16SJohn Baldwin } 68937b8ef16SJohn Baldwin 69037b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 69137b8ef16SJohn Baldwin *start = ':'; 69237b8ef16SJohn Baldwin strcpy(start + 1, descr); 69337b8ef16SJohn Baldwin intr_event_update(ie); 69437b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 69537b8ef16SJohn Baldwin return (0); 69637b8ef16SJohn Baldwin } 69737b8ef16SJohn Baldwin 69837b8ef16SJohn Baldwin /* 699c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 700c3045318SJohn Baldwin * associated with. 701c3045318SJohn Baldwin */ 702c3045318SJohn Baldwin void * 703c3045318SJohn Baldwin intr_handler_source(void *cookie) 704c3045318SJohn Baldwin { 705c3045318SJohn Baldwin struct intr_handler *ih; 706c3045318SJohn Baldwin struct intr_event *ie; 707c3045318SJohn Baldwin 708c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 709c3045318SJohn Baldwin if (ih == NULL) 710c3045318SJohn Baldwin return (NULL); 711c3045318SJohn Baldwin ie = ih->ih_event; 712c3045318SJohn Baldwin KASSERT(ie != NULL, 713c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 714c3045318SJohn Baldwin ih->ih_name)); 715c3045318SJohn Baldwin return (ie->ie_source); 716c3045318SJohn Baldwin } 717c3045318SJohn Baldwin 718e4cd31ddSJeff Roberson /* 719e0fa977eSAndriy Gapon * If intr_event_handle() is running in the ISR context at the time of the call, 720e0fa977eSAndriy Gapon * then wait for it to complete. 721e0fa977eSAndriy Gapon */ 722e0fa977eSAndriy Gapon static void 723e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie) 724e0fa977eSAndriy Gapon { 725e0fa977eSAndriy Gapon int phase; 726e0fa977eSAndriy Gapon 727e0fa977eSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 728e0fa977eSAndriy Gapon phase = ie->ie_phase; 729e0fa977eSAndriy Gapon 730e0fa977eSAndriy Gapon /* 731e0fa977eSAndriy Gapon * Switch phase to direct future interrupts to the other active counter. 732e0fa977eSAndriy Gapon * Make sure that any preceding stores are visible before the switch. 733e0fa977eSAndriy Gapon */ 734e0fa977eSAndriy Gapon KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity")); 735e0fa977eSAndriy Gapon atomic_store_rel_int(&ie->ie_phase, !phase); 736e0fa977eSAndriy Gapon 737e0fa977eSAndriy Gapon /* 738e0fa977eSAndriy Gapon * This code cooperates with wait-free iteration of ie_handlers 739e0fa977eSAndriy Gapon * in intr_event_handle. 740e0fa977eSAndriy Gapon * Make sure that the removal and the phase update are not reordered 741e0fa977eSAndriy Gapon * with the active count check. 742e0fa977eSAndriy Gapon * Note that no combination of acquire and release fences can provide 743e0fa977eSAndriy Gapon * that guarantee as Store->Load sequences can always be reordered. 744e0fa977eSAndriy Gapon */ 745e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 746e0fa977eSAndriy Gapon 747e0fa977eSAndriy Gapon /* 748e0fa977eSAndriy Gapon * Now wait on the inactive phase. 749e0fa977eSAndriy Gapon * The acquire fence is needed so that that all post-barrier accesses 750e0fa977eSAndriy Gapon * are after the check. 751e0fa977eSAndriy Gapon */ 752e0fa977eSAndriy Gapon while (ie->ie_active[phase] > 0) 753e0fa977eSAndriy Gapon cpu_spinwait(); 754e0fa977eSAndriy Gapon atomic_thread_fence_acq(); 755e0fa977eSAndriy Gapon } 756e0fa977eSAndriy Gapon 75782a5a275SAndriy Gapon static void 75882a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler) 75982a5a275SAndriy Gapon { 76082a5a275SAndriy Gapon struct intr_event *ie; 76182a5a275SAndriy Gapon 76282a5a275SAndriy Gapon ie = handler->ih_event; 76382a5a275SAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 76482a5a275SAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 76582a5a275SAndriy Gapon ("update for a removed handler")); 76682a5a275SAndriy Gapon 76782a5a275SAndriy Gapon if (ie->ie_thread == NULL) { 76882a5a275SAndriy Gapon intr_event_barrier(ie); 76982a5a275SAndriy Gapon return; 77082a5a275SAndriy Gapon } 77182a5a275SAndriy Gapon if ((handler->ih_flags & IH_CHANGED) == 0) { 77282a5a275SAndriy Gapon handler->ih_flags |= IH_CHANGED; 77382a5a275SAndriy Gapon intr_event_schedule_thread(ie); 77482a5a275SAndriy Gapon } 77582a5a275SAndriy Gapon while ((handler->ih_flags & IH_CHANGED) != 0) 77682a5a275SAndriy Gapon msleep(handler, &ie->ie_lock, 0, "ih_barr", 0); 77782a5a275SAndriy Gapon } 77882a5a275SAndriy Gapon 779e0fa977eSAndriy Gapon /* 780e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 781e4cd31ddSJeff Roberson * 782e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 783e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 784e4cd31ddSJeff Roberson * only. Do not use in BSD code. 785e4cd31ddSJeff Roberson */ 786e4cd31ddSJeff Roberson void 787e4cd31ddSJeff Roberson _intr_drain(int irq) 788e4cd31ddSJeff Roberson { 789e4cd31ddSJeff Roberson struct intr_event *ie; 790e4cd31ddSJeff Roberson struct intr_thread *ithd; 791e4cd31ddSJeff Roberson struct thread *td; 792e4cd31ddSJeff Roberson 793e4cd31ddSJeff Roberson ie = intr_lookup(irq); 794e4cd31ddSJeff Roberson if (ie == NULL) 795e4cd31ddSJeff Roberson return; 796e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 797e4cd31ddSJeff Roberson return; 798e4cd31ddSJeff Roberson ithd = ie->ie_thread; 799e4cd31ddSJeff Roberson td = ithd->it_thread; 8005bd186a6SJeff Roberson /* 8015bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 8025bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 8035bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 8045bd186a6SJeff Roberson */ 805e4cd31ddSJeff Roberson thread_lock(td); 806e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 807e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 8085bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 8095bd186a6SJeff Roberson thread_unlock(td); 8105bd186a6SJeff Roberson pause("idrain", 1); 8115bd186a6SJeff Roberson thread_lock(td); 812e4cd31ddSJeff Roberson } 8135bd186a6SJeff Roberson } 8145bd186a6SJeff Roberson thread_unlock(td); 815e4cd31ddSJeff Roberson return; 816e4cd31ddSJeff Roberson } 817e4cd31ddSJeff Roberson 818b4151f71SJohn Baldwin int 819e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 820b4151f71SJohn Baldwin { 821e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 822e0f66ef8SJohn Baldwin struct intr_event *ie; 823e0f66ef8SJohn Baldwin struct intr_handler *ih; 824111b043cSAndriy Gapon struct intr_handler **prevptr; 825e0f66ef8SJohn Baldwin #ifdef notyet 826e0f66ef8SJohn Baldwin int dead; 827b4151f71SJohn Baldwin #endif 828b4151f71SJohn Baldwin 8293e5da754SJohn Baldwin if (handler == NULL) 830b4151f71SJohn Baldwin return (EINVAL); 831e0f66ef8SJohn Baldwin ie = handler->ih_event; 832e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 833e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 8343e5da754SJohn Baldwin handler->ih_name)); 835111b043cSAndriy Gapon 836e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 83791f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 838e0f66ef8SJohn Baldwin ie->ie_name); 839111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 8403e5da754SJohn Baldwin if (ih == handler) 841111b043cSAndriy Gapon break; 842111b043cSAndriy Gapon } 843111b043cSAndriy Gapon if (ih == NULL) { 844111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 845111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 846111b043cSAndriy Gapon } 847111b043cSAndriy Gapon 848de271f01SJohn Baldwin /* 849e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 850e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 851e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 852e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 853e0f66ef8SJohn Baldwin */ 854e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 855111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 856e0fa977eSAndriy Gapon intr_event_barrier(ie); 857e0fa977eSAndriy Gapon intr_event_update(ie); 858e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 859e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 860e0f66ef8SJohn Baldwin return (0); 861e0f66ef8SJohn Baldwin } 862e0f66ef8SJohn Baldwin 863e0f66ef8SJohn Baldwin /* 864e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 865e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 866e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 867e0fa977eSAndriy Gapon * intr_event_handle(). 868de271f01SJohn Baldwin */ 869e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 870e0fa977eSAndriy Gapon ("duplicate handle remove")); 871de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 872e0fa977eSAndriy Gapon intr_event_schedule_thread(ie); 873e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8740f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 875e0f66ef8SJohn Baldwin intr_event_update(ie); 876111b043cSAndriy Gapon 877e0f66ef8SJohn Baldwin #ifdef notyet 878e0f66ef8SJohn Baldwin /* 879e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 880e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 881e0f66ef8SJohn Baldwin * interrupt. 882e0f66ef8SJohn Baldwin */ 883e0f66ef8SJohn Baldwin dead = 1; 884111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 885111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 886e0f66ef8SJohn Baldwin dead = 0; 887e0f66ef8SJohn Baldwin break; 888e0f66ef8SJohn Baldwin } 889e0f66ef8SJohn Baldwin } 890e0f66ef8SJohn Baldwin if (dead) { 891e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 892e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 893e0f66ef8SJohn Baldwin } 894e0f66ef8SJohn Baldwin #endif 895e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 896b4151f71SJohn Baldwin free(handler, M_ITHREAD); 897b4151f71SJohn Baldwin return (0); 898b4151f71SJohn Baldwin } 899b4151f71SJohn Baldwin 90082a5a275SAndriy Gapon int 90182a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie) 90282a5a275SAndriy Gapon { 90382a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 90482a5a275SAndriy Gapon struct intr_event *ie; 90582a5a275SAndriy Gapon 90682a5a275SAndriy Gapon if (handler == NULL) 90782a5a275SAndriy Gapon return (EINVAL); 90882a5a275SAndriy Gapon ie = handler->ih_event; 90982a5a275SAndriy Gapon KASSERT(ie != NULL, 91082a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 91182a5a275SAndriy Gapon handler->ih_name)); 91282a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 91382a5a275SAndriy Gapon handler->ih_flags |= IH_SUSP; 91482a5a275SAndriy Gapon intr_handler_barrier(handler); 91582a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 91682a5a275SAndriy Gapon return (0); 91782a5a275SAndriy Gapon } 91882a5a275SAndriy Gapon 91982a5a275SAndriy Gapon int 92082a5a275SAndriy Gapon intr_event_resume_handler(void *cookie) 92182a5a275SAndriy Gapon { 92282a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 92382a5a275SAndriy Gapon struct intr_event *ie; 92482a5a275SAndriy Gapon 92582a5a275SAndriy Gapon if (handler == NULL) 92682a5a275SAndriy Gapon return (EINVAL); 92782a5a275SAndriy Gapon ie = handler->ih_event; 92882a5a275SAndriy Gapon KASSERT(ie != NULL, 92982a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 93082a5a275SAndriy Gapon handler->ih_name)); 93182a5a275SAndriy Gapon 93282a5a275SAndriy Gapon /* 93382a5a275SAndriy Gapon * intr_handler_barrier() acts not only as a barrier, 93482a5a275SAndriy Gapon * it also allows to check for any pending interrupts. 93582a5a275SAndriy Gapon */ 93682a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 93782a5a275SAndriy Gapon handler->ih_flags &= ~IH_SUSP; 93882a5a275SAndriy Gapon intr_handler_barrier(handler); 93982a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 94082a5a275SAndriy Gapon return (0); 94182a5a275SAndriy Gapon } 94282a5a275SAndriy Gapon 9431ee1b687SJohn Baldwin static int 944e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 9453e5da754SJohn Baldwin { 946e0f66ef8SJohn Baldwin struct intr_entropy entropy; 947e0f66ef8SJohn Baldwin struct intr_thread *it; 948b40ce416SJulian Elischer struct thread *td; 94904774f23SJulian Elischer struct thread *ctd; 9503e5da754SJohn Baldwin 9513e5da754SJohn Baldwin /* 9523e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 9533e5da754SJohn Baldwin */ 954111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 955e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 9563e5da754SJohn Baldwin return (EINVAL); 9573e5da754SJohn Baldwin 95804774f23SJulian Elischer ctd = curthread; 959e0f66ef8SJohn Baldwin it = ie->ie_thread; 960e0f66ef8SJohn Baldwin td = it->it_thread; 961e0f66ef8SJohn Baldwin 9623e5da754SJohn Baldwin /* 9633e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 9643e5da754SJohn Baldwin * sources of entropy, then gather some. 9653e5da754SJohn Baldwin */ 966c4eb6630SGleb Smirnoff if (ie->ie_hflags & IH_ENTROPY) { 967e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 968e0f66ef8SJohn Baldwin entropy.td = ctd; 96919fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 9703e5da754SJohn Baldwin } 9713e5da754SJohn Baldwin 972ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 9733e5da754SJohn Baldwin 9743e5da754SJohn Baldwin /* 9753e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 976982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 977982d11f8SJeff Roberson * put it on the runqueue. 978283dfee9SKonstantin Belousov * 979283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 980283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 981283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 9823e5da754SJohn Baldwin */ 9833eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 984982d11f8SJeff Roberson thread_lock(td); 98571fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 986fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 9877ab24ea3SJulian Elischer td->td_name); 98871fad9fdSJulian Elischer TD_CLR_IWAIT(td); 989f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 9903e5da754SJohn Baldwin } else { 991e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 992fc2e87beSMatt Macy __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state); 993982d11f8SJeff Roberson thread_unlock(td); 99461a74c5cSJeff Roberson } 9953e5da754SJohn Baldwin 9963e5da754SJohn Baldwin return (0); 9973e5da754SJohn Baldwin } 9983e5da754SJohn Baldwin 999fe486a37SJohn Baldwin /* 1000e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1001e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 1002e84bcd84SRobert Watson * a PIC. 1003e84bcd84SRobert Watson */ 1004e84bcd84SRobert Watson static int 1005066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 1006e84bcd84SRobert Watson { 1007e84bcd84SRobert Watson 1008e84bcd84SRobert Watson return (0); 1009e84bcd84SRobert Watson } 1010e84bcd84SRobert Watson 1011e84bcd84SRobert Watson /* 1012fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 1013fe486a37SJohn Baldwin * is not specified, then a new event is created. 1014fe486a37SJohn Baldwin */ 10153e5da754SJohn Baldwin int 1016e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 1017b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 10188088699fSJohn Baldwin { 1019e0f66ef8SJohn Baldwin struct intr_event *ie; 1020b4151f71SJohn Baldwin int error; 10218088699fSJohn Baldwin 1022bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 10233e5da754SJohn Baldwin return (EINVAL); 10243e5da754SJohn Baldwin 1025e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 10268088699fSJohn Baldwin 1027e0f66ef8SJohn Baldwin if (ie != NULL) { 1028e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 10293e5da754SJohn Baldwin return (EINVAL); 10303e5da754SJohn Baldwin } else { 10319b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1032e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10338088699fSJohn Baldwin if (error) 1034b4151f71SJohn Baldwin return (error); 1035e0f66ef8SJohn Baldwin if (eventp != NULL) 1036e0f66ef8SJohn Baldwin *eventp = ie; 10378088699fSJohn Baldwin } 10388d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1039d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 10408d809d50SJeff Roberson return (error); 10418088699fSJohn Baldwin } 10428088699fSJohn Baldwin 10431931cf94SJohn Baldwin /* 1044e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10451931cf94SJohn Baldwin */ 10461931cf94SJohn Baldwin void 1047b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10481931cf94SJohn Baldwin { 1049e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1050e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1051d95dca1dSJohn Baldwin struct intr_entropy entropy; 1052ba3f7276SMatt Macy int error __unused; 10538088699fSJohn Baldwin 1054e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1055e0f66ef8SJohn Baldwin ih->ih_need); 10561931cf94SJohn Baldwin 1057d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1058d95dca1dSJohn Baldwin entropy.td = curthread; 105919fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 1060d95dca1dSJohn Baldwin 10611931cf94SJohn Baldwin /* 10623e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 10633e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 10643e5da754SJohn Baldwin * it will execute it the next time it runs. 10651931cf94SJohn Baldwin */ 1066283dfee9SKonstantin Belousov ih->ih_need = 1; 10671ca2c018SBruce Evans 1068b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 106983c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 1070e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 10713e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 10728088699fSJohn Baldwin } 10738088699fSJohn Baldwin } 10748088699fSJohn Baldwin 1075fe486a37SJohn Baldwin /* 1076fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1077fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1078fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1079fe486a37SJohn Baldwin * an optimal interface. 1080fe486a37SJohn Baldwin */ 1081fe486a37SJohn Baldwin int 1082fe486a37SJohn Baldwin swi_remove(void *cookie) 1083fe486a37SJohn Baldwin { 1084fe486a37SJohn Baldwin 1085fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1086fe486a37SJohn Baldwin } 1087fe486a37SJohn Baldwin 1088111b043cSAndriy Gapon static void 108937e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1090e0f66ef8SJohn Baldwin { 1091111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 1092e0f66ef8SJohn Baldwin 1093111b043cSAndriy Gapon ihp = NULL; 1094111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1095e0f66ef8SJohn Baldwin /* 1096e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1097e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1098e0f66ef8SJohn Baldwin */ 1099e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1100e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1101111b043cSAndriy Gapon if (ihp == NULL) 1102111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1103111b043cSAndriy Gapon else 1104111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1105e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1106e0f66ef8SJohn Baldwin wakeup(ih); 1107e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1108e0f66ef8SJohn Baldwin continue; 1109e0f66ef8SJohn Baldwin } 1110e0f66ef8SJohn Baldwin 1111111b043cSAndriy Gapon /* 1112111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1113111b043cSAndriy Gapon * update the previous element. 1114111b043cSAndriy Gapon */ 1115111b043cSAndriy Gapon ihp = ih; 1116111b043cSAndriy Gapon 111782a5a275SAndriy Gapon if ((ih->ih_flags & IH_CHANGED) != 0) { 111882a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 111982a5a275SAndriy Gapon ih->ih_flags &= ~IH_CHANGED; 112082a5a275SAndriy Gapon wakeup(ih); 112182a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 112282a5a275SAndriy Gapon } 112382a5a275SAndriy Gapon 1124f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1125f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1126f2d619c8SPaolo Pisati continue; 1127f2d619c8SPaolo Pisati 112882a5a275SAndriy Gapon /* Skip suspended handlers */ 112982a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 113082a5a275SAndriy Gapon continue; 113182a5a275SAndriy Gapon 1132e0f66ef8SJohn Baldwin /* 1133e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1134e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1135e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 11361b79b949SKirk McKusick * 11371b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 11381b79b949SKirk McKusick * means that there is no request to execute handlers, 11391b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1140e0f66ef8SJohn Baldwin */ 11411b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 11421b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1143e0f66ef8SJohn Baldwin continue; 1144e0f66ef8SJohn Baldwin 1145e0f66ef8SJohn Baldwin /* Execute this handler. */ 1146e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1147bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1148bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1149e0f66ef8SJohn Baldwin 1150e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1151e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1152e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1153e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1154e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1155e0f66ef8SJohn Baldwin } 115637e9511fSJohn Baldwin } 115737e9511fSJohn Baldwin 115837e9511fSJohn Baldwin static void 115937e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 116037e9511fSJohn Baldwin { 116137e9511fSJohn Baldwin 116237e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 116337e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 116437e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 116537e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1166e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1167e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1168e0f66ef8SJohn Baldwin 1169e0f66ef8SJohn Baldwin /* 1170e0f66ef8SJohn Baldwin * Interrupt storm handling: 1171e0f66ef8SJohn Baldwin * 1172e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1173e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1174e0f66ef8SJohn Baldwin * 1175e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1176e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1177e0f66ef8SJohn Baldwin * then enter storming mode. 1178e0f66ef8SJohn Baldwin */ 1179e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1180e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 11810ae62c18SNate Lawson /* Report the message only once every second. */ 11820ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1183e0f66ef8SJohn Baldwin printf( 11840ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1185e0f66ef8SJohn Baldwin ie->ie_name); 1186e0f66ef8SJohn Baldwin } 1187e41bcf3cSJohn Baldwin pause("istorm", 1); 1188e0f66ef8SJohn Baldwin } else 1189e0f66ef8SJohn Baldwin ie->ie_count++; 1190e0f66ef8SJohn Baldwin 1191e0f66ef8SJohn Baldwin /* 1192e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1193e0f66ef8SJohn Baldwin * the interrupt source. 1194e0f66ef8SJohn Baldwin */ 11951ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 11961ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1197e0f66ef8SJohn Baldwin } 1198e0f66ef8SJohn Baldwin 11998088699fSJohn Baldwin /* 1200b4151f71SJohn Baldwin * This is the main code for interrupt threads. 12018088699fSJohn Baldwin */ 120237c84183SPoul-Henning Kamp static void 1203b4151f71SJohn Baldwin ithread_loop(void *arg) 12048088699fSJohn Baldwin { 1205*511d1afbSGleb Smirnoff struct epoch_tracker et; 1206e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1207e0f66ef8SJohn Baldwin struct intr_event *ie; 1208b40ce416SJulian Elischer struct thread *td; 1209b4151f71SJohn Baldwin struct proc *p; 1210*511d1afbSGleb Smirnoff int wake, epoch_count; 12118088699fSJohn Baldwin 1212b40ce416SJulian Elischer td = curthread; 1213b40ce416SJulian Elischer p = td->td_proc; 1214e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1215e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 121691f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1217e0f66ef8SJohn Baldwin ie = ithd->it_event; 1218e0f66ef8SJohn Baldwin ie->ie_count = 0; 1219e4cd31ddSJeff Roberson wake = 0; 12208088699fSJohn Baldwin 12218088699fSJohn Baldwin /* 12228088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 12238088699fSJohn Baldwin * list of handlers, giving each one a go at it. 12248088699fSJohn Baldwin */ 12258088699fSJohn Baldwin for (;;) { 1226b4151f71SJohn Baldwin /* 1227b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1228b4151f71SJohn Baldwin */ 1229b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1230e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 12317ab24ea3SJulian Elischer p->p_pid, td->td_name); 1232b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1233ca9a0ddfSJulian Elischer kthread_exit(); 1234b4151f71SJohn Baldwin } 1235b4151f71SJohn Baldwin 1236e0f66ef8SJohn Baldwin /* 1237e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1238e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1239e0f66ef8SJohn Baldwin * should make another pass. 1240283dfee9SKonstantin Belousov * 1241283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1242283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1243283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1244e0f66ef8SJohn Baldwin */ 1245*511d1afbSGleb Smirnoff if (ie->ie_hflags & IH_NET) { 1246*511d1afbSGleb Smirnoff epoch_count = 0; 1247*511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1248*511d1afbSGleb Smirnoff } 1249*511d1afbSGleb Smirnoff while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) { 1250e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 1251*511d1afbSGleb Smirnoff if ((ie->ie_hflags & IH_NET) && 1252*511d1afbSGleb Smirnoff ++epoch_count >= intr_epoch_batch) { 1253*511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 1254*511d1afbSGleb Smirnoff epoch_count = 0; 1255*511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1256*511d1afbSGleb Smirnoff } 1257*511d1afbSGleb Smirnoff } 1258*511d1afbSGleb Smirnoff if (ie->ie_hflags & IH_NET) 1259*511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 12607870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 12617870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 12628088699fSJohn Baldwin 12638088699fSJohn Baldwin /* 12648088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 12658088699fSJohn Baldwin * lock. This may take a while and it_need may get 12668088699fSJohn Baldwin * set again, so we have to check it again. 12678088699fSJohn Baldwin */ 1268982d11f8SJeff Roberson thread_lock(td); 126903bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 127003bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 12717870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1272e0f66ef8SJohn Baldwin ie->ie_count = 0; 1273686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IWAIT); 1274686bcb5cSJeff Roberson } else { 1275e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1276e4cd31ddSJeff Roberson wake = 1; 1277e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1278e4cd31ddSJeff Roberson } 1279982d11f8SJeff Roberson thread_unlock(td); 1280686bcb5cSJeff Roberson } 1281e4cd31ddSJeff Roberson if (wake) { 1282e4cd31ddSJeff Roberson wakeup(ithd); 1283e4cd31ddSJeff Roberson wake = 0; 1284e4cd31ddSJeff Roberson } 12858088699fSJohn Baldwin } 12861931cf94SJohn Baldwin } 12871ee1b687SJohn Baldwin 12881ee1b687SJohn Baldwin /* 12891ee1b687SJohn Baldwin * Main interrupt handling body. 12901ee1b687SJohn Baldwin * 12911ee1b687SJohn Baldwin * Input: 12921ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 12931ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 12941ee1b687SJohn Baldwin * handlers as their main argument. 12951ee1b687SJohn Baldwin * Return value: 12961ee1b687SJohn Baldwin * o 0: everything ok. 12971ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 12981ee1b687SJohn Baldwin */ 12991ee1b687SJohn Baldwin int 13001ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 13011ee1b687SJohn Baldwin { 13021ee1b687SJohn Baldwin struct intr_handler *ih; 13031f255bd3SAlexander Motin struct trapframe *oldframe; 13041ee1b687SJohn Baldwin struct thread *td; 1305e0fa977eSAndriy Gapon int phase; 130682a5a275SAndriy Gapon int ret; 130782a5a275SAndriy Gapon bool filter, thread; 13081ee1b687SJohn Baldwin 13091ee1b687SJohn Baldwin td = curthread; 13101ee1b687SJohn Baldwin 1311b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1312b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1313b7627840SKonstantin Belousov #endif 1314b7627840SKonstantin Belousov 13151ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1316111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 13171ee1b687SJohn Baldwin return (EINVAL); 13181ee1b687SJohn Baldwin 13191ee1b687SJohn Baldwin /* 13201ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 13211ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 13221ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 13231ee1b687SJohn Baldwin * a trapframe as its argument. 13241ee1b687SJohn Baldwin */ 13251ee1b687SJohn Baldwin td->td_intr_nesting_level++; 132682a5a275SAndriy Gapon filter = false; 132782a5a275SAndriy Gapon thread = false; 13281ee1b687SJohn Baldwin ret = 0; 13291ee1b687SJohn Baldwin critical_enter(); 13301f255bd3SAlexander Motin oldframe = td->td_intr_frame; 13311f255bd3SAlexander Motin td->td_intr_frame = frame; 1332111b043cSAndriy Gapon 1333e0fa977eSAndriy Gapon phase = ie->ie_phase; 1334e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1335e0fa977eSAndriy Gapon 1336e0fa977eSAndriy Gapon /* 1337e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1338e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1339e0fa977eSAndriy Gapon */ 1340e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1341e0fa977eSAndriy Gapon 1342111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 134382a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 134482a5a275SAndriy Gapon continue; 13451ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 134682a5a275SAndriy Gapon thread = true; 13471ee1b687SJohn Baldwin continue; 13481ee1b687SJohn Baldwin } 13491ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 13501ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 13511ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 13521ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 13531ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 13541ee1b687SJohn Baldwin else 13551ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 135689fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 135789fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 135889fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 135989fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 136089fc20ccSAndriy Gapon ih->ih_name)); 136182a5a275SAndriy Gapon filter = filter || ret == FILTER_HANDLED; 136289fc20ccSAndriy Gapon 13631ee1b687SJohn Baldwin /* 13641ee1b687SJohn Baldwin * Wrapper handler special handling: 13651ee1b687SJohn Baldwin * 13661ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 13671ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 13681ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 13691ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 13701ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 13711ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 13721ee1b687SJohn Baldwin * of just an ithread handler. 13731ee1b687SJohn Baldwin * 13741ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 137582a5a275SAndriy Gapon * their own. 13761ee1b687SJohn Baldwin */ 13771ee1b687SJohn Baldwin if (!thread) { 13781ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 137982a5a275SAndriy Gapon thread = true; 13801ee1b687SJohn Baldwin } 13811ee1b687SJohn Baldwin } 1382e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1383e0fa977eSAndriy Gapon 13841f255bd3SAlexander Motin td->td_intr_frame = oldframe; 13851ee1b687SJohn Baldwin 13861ee1b687SJohn Baldwin if (thread) { 13871ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 13881ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 13891ee1b687SJohn Baldwin } else { 13901ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 13911ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 13921ee1b687SJohn Baldwin } 13931ee1b687SJohn Baldwin 13941ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 13951ee1b687SJohn Baldwin if (thread) { 1396ba3f7276SMatt Macy int error __unused; 1397ba3f7276SMatt Macy 13981ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 13991ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 14001ee1b687SJohn Baldwin } 14011ee1b687SJohn Baldwin critical_exit(); 14021ee1b687SJohn Baldwin td->td_intr_nesting_level--; 140382a5a275SAndriy Gapon #ifdef notyet 140482a5a275SAndriy Gapon /* The interrupt is not aknowledged by any filter and has no ithread. */ 140582a5a275SAndriy Gapon if (!thread && !filter) 140682a5a275SAndriy Gapon return (EINVAL); 140782a5a275SAndriy Gapon #endif 14081ee1b687SJohn Baldwin return (0); 14091ee1b687SJohn Baldwin } 14101931cf94SJohn Baldwin 14118b201c42SJohn Baldwin #ifdef DDB 14128b201c42SJohn Baldwin /* 14138b201c42SJohn Baldwin * Dump details about an interrupt handler 14148b201c42SJohn Baldwin */ 14158b201c42SJohn Baldwin static void 1416e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 14178b201c42SJohn Baldwin { 14188b201c42SJohn Baldwin int comma; 14198b201c42SJohn Baldwin 14208b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 14218b201c42SJohn Baldwin switch (ih->ih_pri) { 14228b201c42SJohn Baldwin case PI_REALTIME: 14238b201c42SJohn Baldwin db_printf("CLK "); 14248b201c42SJohn Baldwin break; 14258b201c42SJohn Baldwin case PI_AV: 14268b201c42SJohn Baldwin db_printf("AV "); 14278b201c42SJohn Baldwin break; 1428d3305205SJohn Baldwin case PI_TTY: 14298b201c42SJohn Baldwin db_printf("TTY "); 14308b201c42SJohn Baldwin break; 14318b201c42SJohn Baldwin case PI_NET: 14328b201c42SJohn Baldwin db_printf("NET "); 14338b201c42SJohn Baldwin break; 14348b201c42SJohn Baldwin case PI_DISK: 14358b201c42SJohn Baldwin db_printf("DISK"); 14368b201c42SJohn Baldwin break; 14378b201c42SJohn Baldwin case PI_DULL: 14388b201c42SJohn Baldwin db_printf("DULL"); 14398b201c42SJohn Baldwin break; 14408b201c42SJohn Baldwin default: 14418b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 14428b201c42SJohn Baldwin db_printf("SWI "); 14438b201c42SJohn Baldwin else 14448b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 14458b201c42SJohn Baldwin break; 14468b201c42SJohn Baldwin } 14478b201c42SJohn Baldwin db_printf(" "); 1448b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1449b887a155SKonstantin Belousov db_printf("[F]"); 1450b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1451b887a155SKonstantin Belousov } 1452b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1453b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1454b887a155SKonstantin Belousov db_printf(","); 1455b887a155SKonstantin Belousov db_printf("[H]"); 14568b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1457b887a155SKonstantin Belousov } 14588b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 14598b201c42SJohn Baldwin if (ih->ih_need || 1460ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 14618b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 14628b201c42SJohn Baldwin db_printf(" {"); 14638b201c42SJohn Baldwin comma = 0; 14648b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 14658b201c42SJohn Baldwin if (comma) 14668b201c42SJohn Baldwin db_printf(", "); 14678b201c42SJohn Baldwin db_printf("EXCL"); 14688b201c42SJohn Baldwin comma = 1; 14698b201c42SJohn Baldwin } 14708b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 14718b201c42SJohn Baldwin if (comma) 14728b201c42SJohn Baldwin db_printf(", "); 14738b201c42SJohn Baldwin db_printf("ENTROPY"); 14748b201c42SJohn Baldwin comma = 1; 14758b201c42SJohn Baldwin } 14768b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 14778b201c42SJohn Baldwin if (comma) 14788b201c42SJohn Baldwin db_printf(", "); 14798b201c42SJohn Baldwin db_printf("DEAD"); 14808b201c42SJohn Baldwin comma = 1; 14818b201c42SJohn Baldwin } 14828b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 14838b201c42SJohn Baldwin if (comma) 14848b201c42SJohn Baldwin db_printf(", "); 14858b201c42SJohn Baldwin db_printf("MPSAFE"); 14868b201c42SJohn Baldwin comma = 1; 14878b201c42SJohn Baldwin } 14888b201c42SJohn Baldwin if (ih->ih_need) { 14898b201c42SJohn Baldwin if (comma) 14908b201c42SJohn Baldwin db_printf(", "); 14918b201c42SJohn Baldwin db_printf("NEED"); 14928b201c42SJohn Baldwin } 14938b201c42SJohn Baldwin db_printf("}"); 14948b201c42SJohn Baldwin } 14958b201c42SJohn Baldwin db_printf("\n"); 14968b201c42SJohn Baldwin } 14978b201c42SJohn Baldwin 14988b201c42SJohn Baldwin /* 1499e0f66ef8SJohn Baldwin * Dump details about a event. 15008b201c42SJohn Baldwin */ 15018b201c42SJohn Baldwin void 1502e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 15038b201c42SJohn Baldwin { 1504e0f66ef8SJohn Baldwin struct intr_handler *ih; 1505e0f66ef8SJohn Baldwin struct intr_thread *it; 15068b201c42SJohn Baldwin int comma; 15078b201c42SJohn Baldwin 1508e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1509e0f66ef8SJohn Baldwin it = ie->ie_thread; 1510e0f66ef8SJohn Baldwin if (it != NULL) 1511e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1512e0f66ef8SJohn Baldwin else 1513e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1514c4eb6630SGleb Smirnoff if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 || 1515e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 15168b201c42SJohn Baldwin db_printf(" {"); 15178b201c42SJohn Baldwin comma = 0; 1518e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 15198b201c42SJohn Baldwin db_printf("SOFT"); 15208b201c42SJohn Baldwin comma = 1; 15218b201c42SJohn Baldwin } 1522e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 15238b201c42SJohn Baldwin if (comma) 15248b201c42SJohn Baldwin db_printf(", "); 1525e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 15268b201c42SJohn Baldwin comma = 1; 15278b201c42SJohn Baldwin } 1528e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 15298b201c42SJohn Baldwin if (comma) 15308b201c42SJohn Baldwin db_printf(", "); 15318b201c42SJohn Baldwin db_printf("NEED"); 15328b201c42SJohn Baldwin } 15338b201c42SJohn Baldwin db_printf("}"); 15348b201c42SJohn Baldwin } 15358b201c42SJohn Baldwin db_printf("\n"); 15368b201c42SJohn Baldwin 15378b201c42SJohn Baldwin if (handlers) 1538111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 15398b201c42SJohn Baldwin db_dump_intrhand(ih); 15408b201c42SJohn Baldwin } 1541e0f66ef8SJohn Baldwin 1542e0f66ef8SJohn Baldwin /* 1543e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1544e0f66ef8SJohn Baldwin */ 1545e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1546e0f66ef8SJohn Baldwin { 1547e0f66ef8SJohn Baldwin struct intr_event *ie; 154819e9205aSJohn Baldwin int all, verbose; 1549e0f66ef8SJohn Baldwin 1550dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1551dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1552e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1553111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1554e0f66ef8SJohn Baldwin continue; 1555e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 155619e9205aSJohn Baldwin if (db_pager_quit) 155719e9205aSJohn Baldwin break; 1558e0f66ef8SJohn Baldwin } 1559e0f66ef8SJohn Baldwin } 15608b201c42SJohn Baldwin #endif /* DDB */ 15618b201c42SJohn Baldwin 1562b4151f71SJohn Baldwin /* 15638088699fSJohn Baldwin * Start standard software interrupt threads 15641931cf94SJohn Baldwin */ 15651931cf94SJohn Baldwin static void 1566b4151f71SJohn Baldwin start_softintr(void *dummy) 15671931cf94SJohn Baldwin { 1568b4151f71SJohn Baldwin 15698d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 15708d809d50SJeff Roberson panic("died while creating vm swi ithread"); 15711931cf94SJohn Baldwin } 1572237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1573237fdd78SRobert Watson NULL); 15741931cf94SJohn Baldwin 1575d279178dSThomas Moestl /* 1576d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1577d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1578d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1579d279178dSThomas Moestl * independent. 1580d279178dSThomas Moestl * 1581d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1582d279178dSThomas Moestl * calculate things at run time. 1583d279178dSThomas Moestl */ 1584d279178dSThomas Moestl static int 1585d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1586d279178dSThomas Moestl { 1587521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1588d279178dSThomas Moestl } 1589d279178dSThomas Moestl 1590d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1591d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1592d279178dSThomas Moestl 1593d279178dSThomas Moestl static int 1594d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1595d279178dSThomas Moestl { 159685729c2cSJuli Mallett #ifdef SCTL_MASK32 159785729c2cSJuli Mallett uint32_t *intrcnt32; 159885729c2cSJuli Mallett unsigned i; 159985729c2cSJuli Mallett int error; 160085729c2cSJuli Mallett 160185729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 160285729c2cSJuli Mallett if (!req->oldptr) 160385729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 160485729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 160585729c2cSJuli Mallett if (intrcnt32 == NULL) 160685729c2cSJuli Mallett return (ENOMEM); 160785729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 160885729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 160985729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 161085729c2cSJuli Mallett free(intrcnt32, M_TEMP); 161185729c2cSJuli Mallett return (error); 161285729c2cSJuli Mallett } 161385729c2cSJuli Mallett #endif 1614521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1615d279178dSThomas Moestl } 1616d279178dSThomas Moestl 1617d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1618d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 16198b201c42SJohn Baldwin 16208b201c42SJohn Baldwin #ifdef DDB 16218b201c42SJohn Baldwin /* 16228b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 16238b201c42SJohn Baldwin */ 16248b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 16258b201c42SJohn Baldwin { 16268b201c42SJohn Baldwin u_long *i; 16278b201c42SJohn Baldwin char *cp; 1628521ea19dSAttilio Rao u_int j; 16298b201c42SJohn Baldwin 16308b201c42SJohn Baldwin cp = intrnames; 1631521ea19dSAttilio Rao j = 0; 1632521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1633521ea19dSAttilio Rao i++, j++) { 16348b201c42SJohn Baldwin if (*cp == '\0') 16358b201c42SJohn Baldwin break; 16368b201c42SJohn Baldwin if (*i != 0) 16378b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 16388b201c42SJohn Baldwin cp += strlen(cp) + 1; 16398b201c42SJohn Baldwin } 16408b201c42SJohn Baldwin } 16418b201c42SJohn Baldwin #endif 1642