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> 51511d1afbSGleb 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"); 98511d1afbSGleb Smirnoff static int intr_epoch_batch = 1000; 99511d1afbSGleb Smirnoff SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch, 100511d1afbSGleb 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; 192*f912e8f2SHans Petter Selasky int missed, space, flags; 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)); 197*f912e8f2SHans Petter Selasky flags = 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++; 210*f912e8f2SHans Petter Selasky flags |= ih->ih_flags; 2110811d60aSJohn Baldwin } 212*f912e8f2SHans Petter Selasky ie->ie_hflags = flags; 213e0f66ef8SJohn Baldwin 214e0f66ef8SJohn Baldwin /* 21567da50a0SIan Lepore * If there is only one handler and its name is too long, just copy in 21667da50a0SIan Lepore * as much of the end of the name (includes the unit number) as will 21767da50a0SIan Lepore * fit. Otherwise, we have multiple handlers and not all of the names 21867da50a0SIan Lepore * will fit. Add +'s to indicate missing names. If we run out of room 21967da50a0SIan Lepore * and still have +'s to add, change the last character from a + to a *. 220e0f66ef8SJohn Baldwin */ 22167da50a0SIan Lepore if (missed == 1 && space == 1) { 22267da50a0SIan Lepore ih = CK_SLIST_FIRST(&ie->ie_handlers); 22367da50a0SIan Lepore missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 - 22467da50a0SIan Lepore sizeof(ie->ie_fullname); 22567da50a0SIan Lepore strcat(ie->ie_fullname, (missed == 0) ? " " : "-"); 22667da50a0SIan Lepore strcat(ie->ie_fullname, &ih->ih_name[missed]); 22767da50a0SIan Lepore missed = 0; 22867da50a0SIan Lepore } 229e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2300811d60aSJohn Baldwin while (missed-- > 0) { 231e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 232e0f66ef8SJohn Baldwin if (*last == '+') { 233e0f66ef8SJohn Baldwin *last = '*'; 234e0f66ef8SJohn Baldwin break; 235b4151f71SJohn Baldwin } else 236e0f66ef8SJohn Baldwin *last = '+'; 237e0f66ef8SJohn Baldwin } else if (space) { 238e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 239e0f66ef8SJohn Baldwin space = 0; 240e0f66ef8SJohn Baldwin } else 241e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 242b4151f71SJohn Baldwin } 243e0f66ef8SJohn Baldwin 244e0f66ef8SJohn Baldwin /* 245e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 246e0f66ef8SJohn Baldwin * name. 247e0f66ef8SJohn Baldwin */ 248e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 249e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 250e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 251b4151f71SJohn Baldwin } 252b4151f71SJohn Baldwin 253b4151f71SJohn Baldwin int 2549b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2551ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 256066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2571ee1b687SJohn Baldwin const char *fmt, ...) 258bafe5a31SPaolo Pisati { 259bafe5a31SPaolo Pisati struct intr_event *ie; 260bafe5a31SPaolo Pisati va_list ap; 261bafe5a31SPaolo Pisati 262bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 263bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 264bafe5a31SPaolo Pisati return (EINVAL); 265bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 266bafe5a31SPaolo Pisati ie->ie_source = source; 2671ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2681ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2691ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2706d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 271bafe5a31SPaolo Pisati ie->ie_flags = flags; 2729b33b154SJeff Roberson ie->ie_irq = irq; 273eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 274111b043cSAndriy Gapon CK_SLIST_INIT(&ie->ie_handlers); 275bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 276bafe5a31SPaolo Pisati 277bafe5a31SPaolo Pisati va_start(ap, fmt); 278bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 279bafe5a31SPaolo Pisati va_end(ap); 280bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2819b33b154SJeff Roberson mtx_lock(&event_lock); 282bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2839b33b154SJeff Roberson mtx_unlock(&event_lock); 284bafe5a31SPaolo Pisati if (event != NULL) 285bafe5a31SPaolo Pisati *event = ie; 286bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 287bafe5a31SPaolo Pisati return (0); 288bafe5a31SPaolo Pisati } 289b4151f71SJohn Baldwin 290eaf86d16SJohn Baldwin /* 291eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 292eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 29329dfb631SConrad Meyer * platforms this request will fail. Using a cpu id of NOCPU unbinds 294eaf86d16SJohn Baldwin * the interrupt event. 295eaf86d16SJohn Baldwin */ 29629dfb631SConrad Meyer static int 29729dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread) 298eaf86d16SJohn Baldwin { 2999b33b154SJeff Roberson lwpid_t id; 300eaf86d16SJohn Baldwin int error; 301eaf86d16SJohn Baldwin 302eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 303eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 304eaf86d16SJohn Baldwin return (EINVAL); 305eaf86d16SJohn Baldwin 306eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 307eaf86d16SJohn Baldwin return (EOPNOTSUPP); 308cebc7fb1SJohn Baldwin 309cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 310cebc7fb1SJohn Baldwin if (error) 311cebc7fb1SJohn Baldwin return (error); 312cebc7fb1SJohn Baldwin 3139b33b154SJeff Roberson /* 314cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 315cebc7fb1SJohn Baldwin * permissions, etc. 3169b33b154SJeff Roberson */ 31729dfb631SConrad Meyer if (bindithread) { 318eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3199b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3209b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 321eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32281198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3239b33b154SJeff Roberson if (error) 3249b33b154SJeff Roberson return (error); 3259b33b154SJeff Roberson } else 326eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32729dfb631SConrad Meyer } 32829dfb631SConrad Meyer if (bindirq) 329eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 330cebc7fb1SJohn Baldwin if (error) { 33129dfb631SConrad Meyer if (bindithread) { 332cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 333cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 33481198539SAlexander V. Chernikov cpu = ie->ie_cpu; 335cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 336cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33781198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 338cebc7fb1SJohn Baldwin } else 339cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 34029dfb631SConrad Meyer } 341eaf86d16SJohn Baldwin return (error); 342cebc7fb1SJohn Baldwin } 343cebc7fb1SJohn Baldwin 34429dfb631SConrad Meyer if (bindirq) { 345eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 346eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3479b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 34829dfb631SConrad Meyer } 3499b33b154SJeff Roberson 3509b33b154SJeff Roberson return (error); 3519b33b154SJeff Roberson } 3529b33b154SJeff Roberson 35329dfb631SConrad Meyer /* 35429dfb631SConrad Meyer * Bind an interrupt event to the specified CPU. For supported platforms, any 35529dfb631SConrad Meyer * associated ithreads as well as the primary interrupt context will be bound 35629dfb631SConrad Meyer * to the specificed CPU. 35729dfb631SConrad Meyer */ 35829dfb631SConrad Meyer int 35929dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu) 36029dfb631SConrad Meyer { 36129dfb631SConrad Meyer 36229dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, true)); 36329dfb631SConrad Meyer } 36429dfb631SConrad Meyer 36529dfb631SConrad Meyer /* 36629dfb631SConrad Meyer * Bind an interrupt event to the specified CPU, but do not bind associated 36729dfb631SConrad Meyer * ithreads. 36829dfb631SConrad Meyer */ 36929dfb631SConrad Meyer int 37029dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu) 37129dfb631SConrad Meyer { 37229dfb631SConrad Meyer 37329dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, false)); 37429dfb631SConrad Meyer } 37529dfb631SConrad Meyer 37629dfb631SConrad Meyer /* 37729dfb631SConrad Meyer * Bind an interrupt event's ithread to the specified CPU. 37829dfb631SConrad Meyer */ 37929dfb631SConrad Meyer int 38029dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu) 38129dfb631SConrad Meyer { 38229dfb631SConrad Meyer 38329dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, false, true)); 38429dfb631SConrad Meyer } 38529dfb631SConrad Meyer 3864e255d74SAndrew Gallatin /* 3874e255d74SAndrew Gallatin * Bind an interrupt event's ithread to the specified cpuset. 3884e255d74SAndrew Gallatin */ 3894e255d74SAndrew Gallatin int 3904e255d74SAndrew Gallatin intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs) 3914e255d74SAndrew Gallatin { 3924e255d74SAndrew Gallatin lwpid_t id; 3934e255d74SAndrew Gallatin 3944e255d74SAndrew Gallatin mtx_lock(&ie->ie_lock); 3954e255d74SAndrew Gallatin if (ie->ie_thread != NULL) { 3964e255d74SAndrew Gallatin id = ie->ie_thread->it_thread->td_tid; 3974e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 3984e255d74SAndrew Gallatin return (cpuset_setthread(id, cs)); 3994e255d74SAndrew Gallatin } else { 4004e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 4014e255d74SAndrew Gallatin } 4024e255d74SAndrew Gallatin return (ENODEV); 4034e255d74SAndrew Gallatin } 4044e255d74SAndrew Gallatin 4059b33b154SJeff Roberson static struct intr_event * 4069b33b154SJeff Roberson intr_lookup(int irq) 4079b33b154SJeff Roberson { 4089b33b154SJeff Roberson struct intr_event *ie; 4099b33b154SJeff Roberson 4109b33b154SJeff Roberson mtx_lock(&event_lock); 4119b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 4129b33b154SJeff Roberson if (ie->ie_irq == irq && 4139b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 414111b043cSAndriy Gapon CK_SLIST_FIRST(&ie->ie_handlers) != NULL) 4159b33b154SJeff Roberson break; 4169b33b154SJeff Roberson mtx_unlock(&event_lock); 4179b33b154SJeff Roberson return (ie); 4189b33b154SJeff Roberson } 4199b33b154SJeff Roberson 4209b33b154SJeff Roberson int 42129dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m) 4229b33b154SJeff Roberson { 4239b33b154SJeff Roberson struct intr_event *ie; 4249b33b154SJeff Roberson cpuset_t *mask; 4253fe93b94SAdrian Chadd int cpu, n; 4269b33b154SJeff Roberson 4279b33b154SJeff Roberson mask = m; 4289b33b154SJeff Roberson cpu = NOCPU; 4299b33b154SJeff Roberson /* 4309b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 4319b33b154SJeff Roberson * only one cpu is in the set. 4329b33b154SJeff Roberson */ 4339b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 4349b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 4359b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 4369b33b154SJeff Roberson continue; 4379b33b154SJeff Roberson if (cpu != NOCPU) 4389b33b154SJeff Roberson return (EINVAL); 4393fe93b94SAdrian Chadd cpu = n; 4409b33b154SJeff Roberson } 4419b33b154SJeff Roberson } 4429b33b154SJeff Roberson ie = intr_lookup(irq); 4439b33b154SJeff Roberson if (ie == NULL) 4449b33b154SJeff Roberson return (ESRCH); 44529dfb631SConrad Meyer switch (mode) { 44629dfb631SConrad Meyer case CPU_WHICH_IRQ: 4479bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 44829dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 44929dfb631SConrad Meyer return (intr_event_bind_irqonly(ie, cpu)); 45029dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 45129dfb631SConrad Meyer return (intr_event_bind_ithread(ie, cpu)); 45229dfb631SConrad Meyer default: 45329dfb631SConrad Meyer return (EINVAL); 45429dfb631SConrad Meyer } 4559b33b154SJeff Roberson } 4569b33b154SJeff Roberson 4579b33b154SJeff Roberson int 45829dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m) 4599b33b154SJeff Roberson { 4609b33b154SJeff Roberson struct intr_event *ie; 46129dfb631SConrad Meyer struct thread *td; 46229dfb631SConrad Meyer struct proc *p; 4639b33b154SJeff Roberson cpuset_t *mask; 46429dfb631SConrad Meyer lwpid_t id; 46529dfb631SConrad Meyer int error; 4669b33b154SJeff Roberson 4679b33b154SJeff Roberson mask = m; 4689b33b154SJeff Roberson ie = intr_lookup(irq); 4699b33b154SJeff Roberson if (ie == NULL) 4709b33b154SJeff Roberson return (ESRCH); 47129dfb631SConrad Meyer 47229dfb631SConrad Meyer error = 0; 4739b33b154SJeff Roberson CPU_ZERO(mask); 47429dfb631SConrad Meyer switch (mode) { 47529dfb631SConrad Meyer case CPU_WHICH_IRQ: 47629dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 4779b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4789b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4799b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4809b33b154SJeff Roberson else 4819b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 482eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 48329dfb631SConrad Meyer break; 48429dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 48529dfb631SConrad Meyer mtx_lock(&ie->ie_lock); 48629dfb631SConrad Meyer if (ie->ie_thread == NULL) { 48729dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 48829dfb631SConrad Meyer CPU_COPY(cpuset_root, mask); 48929dfb631SConrad Meyer } else { 49029dfb631SConrad Meyer id = ie->ie_thread->it_thread->td_tid; 49129dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 49229dfb631SConrad Meyer error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL); 49329dfb631SConrad Meyer if (error != 0) 49429dfb631SConrad Meyer return (error); 49529dfb631SConrad Meyer CPU_COPY(&td->td_cpuset->cs_mask, mask); 49629dfb631SConrad Meyer PROC_UNLOCK(p); 49729dfb631SConrad Meyer } 49829dfb631SConrad Meyer default: 49929dfb631SConrad Meyer return (EINVAL); 50029dfb631SConrad Meyer } 501eaf86d16SJohn Baldwin return (0); 502eaf86d16SJohn Baldwin } 503eaf86d16SJohn Baldwin 504b4151f71SJohn Baldwin int 505e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 506b4151f71SJohn Baldwin { 507b4151f71SJohn Baldwin 5089b33b154SJeff Roberson mtx_lock(&event_lock); 509e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 510111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 511e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5129b33b154SJeff Roberson mtx_unlock(&event_lock); 513e0f66ef8SJohn Baldwin return (EBUSY); 5144d29cb2dSJohn Baldwin } 515e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 5169477358dSJohn Baldwin #ifndef notyet 5179477358dSJohn Baldwin if (ie->ie_thread != NULL) { 5189477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 5199477358dSJohn Baldwin ie->ie_thread = NULL; 5209477358dSJohn Baldwin } 5219477358dSJohn Baldwin #endif 522e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5239b33b154SJeff Roberson mtx_unlock(&event_lock); 524e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 525e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 526e0f66ef8SJohn Baldwin return (0); 527e0f66ef8SJohn Baldwin } 528e0f66ef8SJohn Baldwin 529e0f66ef8SJohn Baldwin static struct intr_thread * 530e0f66ef8SJohn Baldwin ithread_create(const char *name) 531e0f66ef8SJohn Baldwin { 532e0f66ef8SJohn Baldwin struct intr_thread *ithd; 533e0f66ef8SJohn Baldwin struct thread *td; 534e0f66ef8SJohn Baldwin int error; 535e0f66ef8SJohn Baldwin 536e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 537e0f66ef8SJohn Baldwin 5387ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 5397ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 5409ef95d01SJulian Elischer 0, "intr", "%s", name); 541e0f66ef8SJohn Baldwin if (error) 5423745c395SJulian Elischer panic("kproc_create() failed with %d", error); 543982d11f8SJeff Roberson thread_lock(td); 544ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 545e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 546982d11f8SJeff Roberson thread_unlock(td); 547e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 548e0f66ef8SJohn Baldwin ithd->it_thread = td; 549e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 550e0f66ef8SJohn Baldwin return (ithd); 551e0f66ef8SJohn Baldwin } 552e0f66ef8SJohn Baldwin 553e0f66ef8SJohn Baldwin static void 554e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 555e0f66ef8SJohn Baldwin { 556e0f66ef8SJohn Baldwin struct thread *td; 557e0f66ef8SJohn Baldwin 558bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 559e0f66ef8SJohn Baldwin td = ithread->it_thread; 560982d11f8SJeff Roberson thread_lock(td); 561e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 56271fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 56371fad9fdSJulian Elischer TD_CLR_IWAIT(td); 564f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 56561a74c5cSJeff Roberson } else 566982d11f8SJeff Roberson thread_unlock(td); 567b4151f71SJohn Baldwin } 568b4151f71SJohn Baldwin 569b4151f71SJohn Baldwin int 570e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 571ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 572ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 573b4151f71SJohn Baldwin { 574e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 575111b043cSAndriy Gapon struct intr_handler **prevptr; 576e0f66ef8SJohn Baldwin struct intr_thread *it; 577b4151f71SJohn Baldwin 578ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 579b4151f71SJohn Baldwin return (EINVAL); 580b4151f71SJohn Baldwin 581e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 582e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 583ef544f63SPaolo Pisati ih->ih_filter = filter; 584b4151f71SJohn Baldwin ih->ih_handler = handler; 585b4151f71SJohn Baldwin ih->ih_argument = arg; 58637b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 587e0f66ef8SJohn Baldwin ih->ih_event = ie; 588b4151f71SJohn Baldwin ih->ih_pri = pri; 589ef544f63SPaolo Pisati if (flags & INTR_EXCL) 590b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 591b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 592b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 593b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 594b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 595511d1afbSGleb Smirnoff if (flags & INTR_TYPE_NET) 596511d1afbSGleb Smirnoff ih->ih_flags |= IH_NET; 597b4151f71SJohn Baldwin 598e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 599e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 600111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 601e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 602111b043cSAndriy Gapon (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 603e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 604b4151f71SJohn Baldwin free(ih, M_ITHREAD); 605b4151f71SJohn Baldwin return (EINVAL); 606b4151f71SJohn Baldwin } 607e0f66ef8SJohn Baldwin } 608e0f66ef8SJohn Baldwin 609e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 610ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 611e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 6120f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 613e0f66ef8SJohn Baldwin else { 614e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 615e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 616e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 617e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 618e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 619e0f66ef8SJohn Baldwin ie->ie_thread = it; 620e0f66ef8SJohn Baldwin it->it_event = ie; 621e0f66ef8SJohn Baldwin ithread_update(it); 622e0f66ef8SJohn Baldwin wakeup(ie); 623e0f66ef8SJohn Baldwin } 624e0f66ef8SJohn Baldwin } 625c9516c94SAlexander Kabaev 626c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 627111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) { 628c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 629c9516c94SAlexander Kabaev break; 630c9516c94SAlexander Kabaev } 631111b043cSAndriy Gapon CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next); 632111b043cSAndriy Gapon 633c9516c94SAlexander Kabaev intr_event_update(ie); 634c9516c94SAlexander Kabaev 635e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 636e0f66ef8SJohn Baldwin ie->ie_name); 637e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 638e0f66ef8SJohn Baldwin 639e0f66ef8SJohn Baldwin if (cookiep != NULL) 640e0f66ef8SJohn Baldwin *cookiep = ih; 641e0f66ef8SJohn Baldwin return (0); 642e0f66ef8SJohn Baldwin } 643b4151f71SJohn Baldwin 644c3045318SJohn Baldwin /* 64537b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 64637b8ef16SJohn Baldwin * interrupt handler. 64737b8ef16SJohn Baldwin */ 64837b8ef16SJohn Baldwin int 64937b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 65037b8ef16SJohn Baldwin const char *descr) 65137b8ef16SJohn Baldwin { 65237b8ef16SJohn Baldwin struct intr_handler *ih; 65337b8ef16SJohn Baldwin size_t space; 65437b8ef16SJohn Baldwin char *start; 65537b8ef16SJohn Baldwin 65637b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 65737b8ef16SJohn Baldwin #ifdef INVARIANTS 658111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 65937b8ef16SJohn Baldwin if (ih == cookie) 66037b8ef16SJohn Baldwin break; 66137b8ef16SJohn Baldwin } 66237b8ef16SJohn Baldwin if (ih == NULL) { 66337b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 664d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 66537b8ef16SJohn Baldwin } 66637b8ef16SJohn Baldwin #endif 66737b8ef16SJohn Baldwin ih = cookie; 66837b8ef16SJohn Baldwin 66937b8ef16SJohn Baldwin /* 67037b8ef16SJohn Baldwin * Look for an existing description by checking for an 67137b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 67237b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 67337b8ef16SJohn Baldwin * description at that point. If one is not found, find the 67437b8ef16SJohn Baldwin * end of the name to use as the insertion point. 67537b8ef16SJohn Baldwin */ 676dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 67737b8ef16SJohn Baldwin if (start == NULL) 678dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 67937b8ef16SJohn Baldwin 68037b8ef16SJohn Baldwin /* 68137b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 68237b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 68337b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 68437b8ef16SJohn Baldwin */ 68537b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 68637b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 68737b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 68837b8ef16SJohn Baldwin return (ENOSPC); 68937b8ef16SJohn Baldwin } 69037b8ef16SJohn Baldwin 69137b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 69237b8ef16SJohn Baldwin *start = ':'; 69337b8ef16SJohn Baldwin strcpy(start + 1, descr); 69437b8ef16SJohn Baldwin intr_event_update(ie); 69537b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 69637b8ef16SJohn Baldwin return (0); 69737b8ef16SJohn Baldwin } 69837b8ef16SJohn Baldwin 69937b8ef16SJohn Baldwin /* 700c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 701c3045318SJohn Baldwin * associated with. 702c3045318SJohn Baldwin */ 703c3045318SJohn Baldwin void * 704c3045318SJohn Baldwin intr_handler_source(void *cookie) 705c3045318SJohn Baldwin { 706c3045318SJohn Baldwin struct intr_handler *ih; 707c3045318SJohn Baldwin struct intr_event *ie; 708c3045318SJohn Baldwin 709c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 710c3045318SJohn Baldwin if (ih == NULL) 711c3045318SJohn Baldwin return (NULL); 712c3045318SJohn Baldwin ie = ih->ih_event; 713c3045318SJohn Baldwin KASSERT(ie != NULL, 714c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 715c3045318SJohn Baldwin ih->ih_name)); 716c3045318SJohn Baldwin return (ie->ie_source); 717c3045318SJohn Baldwin } 718c3045318SJohn Baldwin 719e4cd31ddSJeff Roberson /* 720e0fa977eSAndriy Gapon * If intr_event_handle() is running in the ISR context at the time of the call, 721e0fa977eSAndriy Gapon * then wait for it to complete. 722e0fa977eSAndriy Gapon */ 723e0fa977eSAndriy Gapon static void 724e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie) 725e0fa977eSAndriy Gapon { 726e0fa977eSAndriy Gapon int phase; 727e0fa977eSAndriy Gapon 728e0fa977eSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 729e0fa977eSAndriy Gapon phase = ie->ie_phase; 730e0fa977eSAndriy Gapon 731e0fa977eSAndriy Gapon /* 732e0fa977eSAndriy Gapon * Switch phase to direct future interrupts to the other active counter. 733e0fa977eSAndriy Gapon * Make sure that any preceding stores are visible before the switch. 734e0fa977eSAndriy Gapon */ 735e0fa977eSAndriy Gapon KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity")); 736e0fa977eSAndriy Gapon atomic_store_rel_int(&ie->ie_phase, !phase); 737e0fa977eSAndriy Gapon 738e0fa977eSAndriy Gapon /* 739e0fa977eSAndriy Gapon * This code cooperates with wait-free iteration of ie_handlers 740e0fa977eSAndriy Gapon * in intr_event_handle. 741e0fa977eSAndriy Gapon * Make sure that the removal and the phase update are not reordered 742e0fa977eSAndriy Gapon * with the active count check. 743e0fa977eSAndriy Gapon * Note that no combination of acquire and release fences can provide 744e0fa977eSAndriy Gapon * that guarantee as Store->Load sequences can always be reordered. 745e0fa977eSAndriy Gapon */ 746e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 747e0fa977eSAndriy Gapon 748e0fa977eSAndriy Gapon /* 749e0fa977eSAndriy Gapon * Now wait on the inactive phase. 750e0fa977eSAndriy Gapon * The acquire fence is needed so that that all post-barrier accesses 751e0fa977eSAndriy Gapon * are after the check. 752e0fa977eSAndriy Gapon */ 753e0fa977eSAndriy Gapon while (ie->ie_active[phase] > 0) 754e0fa977eSAndriy Gapon cpu_spinwait(); 755e0fa977eSAndriy Gapon atomic_thread_fence_acq(); 756e0fa977eSAndriy Gapon } 757e0fa977eSAndriy Gapon 75882a5a275SAndriy Gapon static void 75982a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler) 76082a5a275SAndriy Gapon { 76182a5a275SAndriy Gapon struct intr_event *ie; 76282a5a275SAndriy Gapon 76382a5a275SAndriy Gapon ie = handler->ih_event; 76482a5a275SAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 76582a5a275SAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 76682a5a275SAndriy Gapon ("update for a removed handler")); 76782a5a275SAndriy Gapon 76882a5a275SAndriy Gapon if (ie->ie_thread == NULL) { 76982a5a275SAndriy Gapon intr_event_barrier(ie); 77082a5a275SAndriy Gapon return; 77182a5a275SAndriy Gapon } 77282a5a275SAndriy Gapon if ((handler->ih_flags & IH_CHANGED) == 0) { 77382a5a275SAndriy Gapon handler->ih_flags |= IH_CHANGED; 77482a5a275SAndriy Gapon intr_event_schedule_thread(ie); 77582a5a275SAndriy Gapon } 77682a5a275SAndriy Gapon while ((handler->ih_flags & IH_CHANGED) != 0) 77782a5a275SAndriy Gapon msleep(handler, &ie->ie_lock, 0, "ih_barr", 0); 77882a5a275SAndriy Gapon } 77982a5a275SAndriy Gapon 780e0fa977eSAndriy Gapon /* 781e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 782e4cd31ddSJeff Roberson * 783e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 784e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 785e4cd31ddSJeff Roberson * only. Do not use in BSD code. 786e4cd31ddSJeff Roberson */ 787e4cd31ddSJeff Roberson void 788e4cd31ddSJeff Roberson _intr_drain(int irq) 789e4cd31ddSJeff Roberson { 790e4cd31ddSJeff Roberson struct intr_event *ie; 791e4cd31ddSJeff Roberson struct intr_thread *ithd; 792e4cd31ddSJeff Roberson struct thread *td; 793e4cd31ddSJeff Roberson 794e4cd31ddSJeff Roberson ie = intr_lookup(irq); 795e4cd31ddSJeff Roberson if (ie == NULL) 796e4cd31ddSJeff Roberson return; 797e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 798e4cd31ddSJeff Roberson return; 799e4cd31ddSJeff Roberson ithd = ie->ie_thread; 800e4cd31ddSJeff Roberson td = ithd->it_thread; 8015bd186a6SJeff Roberson /* 8025bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 8035bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 8045bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 8055bd186a6SJeff Roberson */ 806e4cd31ddSJeff Roberson thread_lock(td); 807e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 808e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 8095bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 8105bd186a6SJeff Roberson thread_unlock(td); 8115bd186a6SJeff Roberson pause("idrain", 1); 8125bd186a6SJeff Roberson thread_lock(td); 813e4cd31ddSJeff Roberson } 8145bd186a6SJeff Roberson } 8155bd186a6SJeff Roberson thread_unlock(td); 816e4cd31ddSJeff Roberson return; 817e4cd31ddSJeff Roberson } 818e4cd31ddSJeff Roberson 819b4151f71SJohn Baldwin int 820e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 821b4151f71SJohn Baldwin { 822e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 823e0f66ef8SJohn Baldwin struct intr_event *ie; 824e0f66ef8SJohn Baldwin struct intr_handler *ih; 825111b043cSAndriy Gapon struct intr_handler **prevptr; 826e0f66ef8SJohn Baldwin #ifdef notyet 827e0f66ef8SJohn Baldwin int dead; 828b4151f71SJohn Baldwin #endif 829b4151f71SJohn Baldwin 8303e5da754SJohn Baldwin if (handler == NULL) 831b4151f71SJohn Baldwin return (EINVAL); 832e0f66ef8SJohn Baldwin ie = handler->ih_event; 833e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 834e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 8353e5da754SJohn Baldwin handler->ih_name)); 836111b043cSAndriy Gapon 837e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 83891f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 839e0f66ef8SJohn Baldwin ie->ie_name); 840111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 8413e5da754SJohn Baldwin if (ih == handler) 842111b043cSAndriy Gapon break; 843111b043cSAndriy Gapon } 844111b043cSAndriy Gapon if (ih == NULL) { 845111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 846111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 847111b043cSAndriy Gapon } 848111b043cSAndriy Gapon 849de271f01SJohn Baldwin /* 850e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 851e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 852e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 853e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 854e0f66ef8SJohn Baldwin */ 855e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 856111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 857e0fa977eSAndriy Gapon intr_event_barrier(ie); 858e0fa977eSAndriy Gapon intr_event_update(ie); 859e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 860e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 861e0f66ef8SJohn Baldwin return (0); 862e0f66ef8SJohn Baldwin } 863e0f66ef8SJohn Baldwin 864e0f66ef8SJohn Baldwin /* 865e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 866e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 867e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 868e0fa977eSAndriy Gapon * intr_event_handle(). 869de271f01SJohn Baldwin */ 870e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 871e0fa977eSAndriy Gapon ("duplicate handle remove")); 872de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 873e0fa977eSAndriy Gapon intr_event_schedule_thread(ie); 874e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8750f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 876e0f66ef8SJohn Baldwin intr_event_update(ie); 877111b043cSAndriy Gapon 878e0f66ef8SJohn Baldwin #ifdef notyet 879e0f66ef8SJohn Baldwin /* 880e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 881e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 882e0f66ef8SJohn Baldwin * interrupt. 883e0f66ef8SJohn Baldwin */ 884e0f66ef8SJohn Baldwin dead = 1; 885111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 886111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 887e0f66ef8SJohn Baldwin dead = 0; 888e0f66ef8SJohn Baldwin break; 889e0f66ef8SJohn Baldwin } 890e0f66ef8SJohn Baldwin } 891e0f66ef8SJohn Baldwin if (dead) { 892e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 893e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 894e0f66ef8SJohn Baldwin } 895e0f66ef8SJohn Baldwin #endif 896e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 897b4151f71SJohn Baldwin free(handler, M_ITHREAD); 898b4151f71SJohn Baldwin return (0); 899b4151f71SJohn Baldwin } 900b4151f71SJohn Baldwin 90182a5a275SAndriy Gapon int 90282a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie) 90382a5a275SAndriy Gapon { 90482a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 90582a5a275SAndriy Gapon struct intr_event *ie; 90682a5a275SAndriy Gapon 90782a5a275SAndriy Gapon if (handler == NULL) 90882a5a275SAndriy Gapon return (EINVAL); 90982a5a275SAndriy Gapon ie = handler->ih_event; 91082a5a275SAndriy Gapon KASSERT(ie != NULL, 91182a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 91282a5a275SAndriy Gapon handler->ih_name)); 91382a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 91482a5a275SAndriy Gapon handler->ih_flags |= IH_SUSP; 91582a5a275SAndriy Gapon intr_handler_barrier(handler); 91682a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 91782a5a275SAndriy Gapon return (0); 91882a5a275SAndriy Gapon } 91982a5a275SAndriy Gapon 92082a5a275SAndriy Gapon int 92182a5a275SAndriy Gapon intr_event_resume_handler(void *cookie) 92282a5a275SAndriy Gapon { 92382a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 92482a5a275SAndriy Gapon struct intr_event *ie; 92582a5a275SAndriy Gapon 92682a5a275SAndriy Gapon if (handler == NULL) 92782a5a275SAndriy Gapon return (EINVAL); 92882a5a275SAndriy Gapon ie = handler->ih_event; 92982a5a275SAndriy Gapon KASSERT(ie != NULL, 93082a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 93182a5a275SAndriy Gapon handler->ih_name)); 93282a5a275SAndriy Gapon 93382a5a275SAndriy Gapon /* 93482a5a275SAndriy Gapon * intr_handler_barrier() acts not only as a barrier, 93582a5a275SAndriy Gapon * it also allows to check for any pending interrupts. 93682a5a275SAndriy Gapon */ 93782a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 93882a5a275SAndriy Gapon handler->ih_flags &= ~IH_SUSP; 93982a5a275SAndriy Gapon intr_handler_barrier(handler); 94082a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 94182a5a275SAndriy Gapon return (0); 94282a5a275SAndriy Gapon } 94382a5a275SAndriy Gapon 9441ee1b687SJohn Baldwin static int 945e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 9463e5da754SJohn Baldwin { 947e0f66ef8SJohn Baldwin struct intr_entropy entropy; 948e0f66ef8SJohn Baldwin struct intr_thread *it; 949b40ce416SJulian Elischer struct thread *td; 95004774f23SJulian Elischer struct thread *ctd; 9513e5da754SJohn Baldwin 9523e5da754SJohn Baldwin /* 9533e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 9543e5da754SJohn Baldwin */ 955111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 956e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 9573e5da754SJohn Baldwin return (EINVAL); 9583e5da754SJohn Baldwin 95904774f23SJulian Elischer ctd = curthread; 960e0f66ef8SJohn Baldwin it = ie->ie_thread; 961e0f66ef8SJohn Baldwin td = it->it_thread; 962e0f66ef8SJohn Baldwin 9633e5da754SJohn Baldwin /* 9643e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 9653e5da754SJohn Baldwin * sources of entropy, then gather some. 9663e5da754SJohn Baldwin */ 967c4eb6630SGleb Smirnoff if (ie->ie_hflags & IH_ENTROPY) { 968e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 969e0f66ef8SJohn Baldwin entropy.td = ctd; 97019fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 9713e5da754SJohn Baldwin } 9723e5da754SJohn Baldwin 973ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 9743e5da754SJohn Baldwin 9753e5da754SJohn Baldwin /* 9763e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 977982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 978982d11f8SJeff Roberson * put it on the runqueue. 979283dfee9SKonstantin Belousov * 980283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 981283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 982283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 9833e5da754SJohn Baldwin */ 9843eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 985982d11f8SJeff Roberson thread_lock(td); 98671fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 987fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 9887ab24ea3SJulian Elischer td->td_name); 98971fad9fdSJulian Elischer TD_CLR_IWAIT(td); 990f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 9913e5da754SJohn Baldwin } else { 992e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 993fc2e87beSMatt Macy __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state); 994982d11f8SJeff Roberson thread_unlock(td); 99561a74c5cSJeff Roberson } 9963e5da754SJohn Baldwin 9973e5da754SJohn Baldwin return (0); 9983e5da754SJohn Baldwin } 9993e5da754SJohn Baldwin 1000fe486a37SJohn Baldwin /* 1001e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1002e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 1003e84bcd84SRobert Watson * a PIC. 1004e84bcd84SRobert Watson */ 1005e84bcd84SRobert Watson static int 1006066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 1007e84bcd84SRobert Watson { 1008e84bcd84SRobert Watson 1009e84bcd84SRobert Watson return (0); 1010e84bcd84SRobert Watson } 1011e84bcd84SRobert Watson 1012e84bcd84SRobert Watson /* 1013fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 1014fe486a37SJohn Baldwin * is not specified, then a new event is created. 1015fe486a37SJohn Baldwin */ 10163e5da754SJohn Baldwin int 1017e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 1018b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 10198088699fSJohn Baldwin { 1020e0f66ef8SJohn Baldwin struct intr_event *ie; 1021b4151f71SJohn Baldwin int error; 10228088699fSJohn Baldwin 1023bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 10243e5da754SJohn Baldwin return (EINVAL); 10253e5da754SJohn Baldwin 1026e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 10278088699fSJohn Baldwin 1028e0f66ef8SJohn Baldwin if (ie != NULL) { 1029e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 10303e5da754SJohn Baldwin return (EINVAL); 10313e5da754SJohn Baldwin } else { 10329b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1033e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10348088699fSJohn Baldwin if (error) 1035b4151f71SJohn Baldwin return (error); 1036e0f66ef8SJohn Baldwin if (eventp != NULL) 1037e0f66ef8SJohn Baldwin *eventp = ie; 10388088699fSJohn Baldwin } 10398d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1040d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 10418d809d50SJeff Roberson return (error); 10428088699fSJohn Baldwin } 10438088699fSJohn Baldwin 10441931cf94SJohn Baldwin /* 1045e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10461931cf94SJohn Baldwin */ 10471931cf94SJohn Baldwin void 1048b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10491931cf94SJohn Baldwin { 1050e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1051e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1052d95dca1dSJohn Baldwin struct intr_entropy entropy; 1053ba3f7276SMatt Macy int error __unused; 10548088699fSJohn Baldwin 1055e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1056e0f66ef8SJohn Baldwin ih->ih_need); 10571931cf94SJohn Baldwin 1058d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1059d95dca1dSJohn Baldwin entropy.td = curthread; 106019fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 1061d95dca1dSJohn Baldwin 10621931cf94SJohn Baldwin /* 10633e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 10643e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 10653e5da754SJohn Baldwin * it will execute it the next time it runs. 10661931cf94SJohn Baldwin */ 1067283dfee9SKonstantin Belousov ih->ih_need = 1; 10681ca2c018SBruce Evans 1069b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 107083c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 1071e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 10723e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 10738088699fSJohn Baldwin } 10748088699fSJohn Baldwin } 10758088699fSJohn Baldwin 1076fe486a37SJohn Baldwin /* 1077fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1078fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1079fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1080fe486a37SJohn Baldwin * an optimal interface. 1081fe486a37SJohn Baldwin */ 1082fe486a37SJohn Baldwin int 1083fe486a37SJohn Baldwin swi_remove(void *cookie) 1084fe486a37SJohn Baldwin { 1085fe486a37SJohn Baldwin 1086fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1087fe486a37SJohn Baldwin } 1088fe486a37SJohn Baldwin 1089111b043cSAndriy Gapon static void 109037e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1091e0f66ef8SJohn Baldwin { 1092111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 1093e0f66ef8SJohn Baldwin 1094111b043cSAndriy Gapon ihp = NULL; 1095111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1096e0f66ef8SJohn Baldwin /* 1097e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1098e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1099e0f66ef8SJohn Baldwin */ 1100e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1101e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1102111b043cSAndriy Gapon if (ihp == NULL) 1103111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1104111b043cSAndriy Gapon else 1105111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1106e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1107e0f66ef8SJohn Baldwin wakeup(ih); 1108e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1109e0f66ef8SJohn Baldwin continue; 1110e0f66ef8SJohn Baldwin } 1111e0f66ef8SJohn Baldwin 1112111b043cSAndriy Gapon /* 1113111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1114111b043cSAndriy Gapon * update the previous element. 1115111b043cSAndriy Gapon */ 1116111b043cSAndriy Gapon ihp = ih; 1117111b043cSAndriy Gapon 111882a5a275SAndriy Gapon if ((ih->ih_flags & IH_CHANGED) != 0) { 111982a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 112082a5a275SAndriy Gapon ih->ih_flags &= ~IH_CHANGED; 112182a5a275SAndriy Gapon wakeup(ih); 112282a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 112382a5a275SAndriy Gapon } 112482a5a275SAndriy Gapon 1125f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1126f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1127f2d619c8SPaolo Pisati continue; 1128f2d619c8SPaolo Pisati 112982a5a275SAndriy Gapon /* Skip suspended handlers */ 113082a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 113182a5a275SAndriy Gapon continue; 113282a5a275SAndriy Gapon 1133e0f66ef8SJohn Baldwin /* 1134e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1135e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1136e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 11371b79b949SKirk McKusick * 11381b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 11391b79b949SKirk McKusick * means that there is no request to execute handlers, 11401b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1141e0f66ef8SJohn Baldwin */ 11421b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 11431b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1144e0f66ef8SJohn Baldwin continue; 1145e0f66ef8SJohn Baldwin 1146e0f66ef8SJohn Baldwin /* Execute this handler. */ 1147e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1148bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1149bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1150e0f66ef8SJohn Baldwin 1151e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1152e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1153e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1154e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1155e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1156e0f66ef8SJohn Baldwin } 115737e9511fSJohn Baldwin } 115837e9511fSJohn Baldwin 115937e9511fSJohn Baldwin static void 116037e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 116137e9511fSJohn Baldwin { 116237e9511fSJohn Baldwin 116337e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 116437e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 116537e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 116637e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1167e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1168e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1169e0f66ef8SJohn Baldwin 1170e0f66ef8SJohn Baldwin /* 1171e0f66ef8SJohn Baldwin * Interrupt storm handling: 1172e0f66ef8SJohn Baldwin * 1173e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1174e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1175e0f66ef8SJohn Baldwin * 1176e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1177e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1178e0f66ef8SJohn Baldwin * then enter storming mode. 1179e0f66ef8SJohn Baldwin */ 1180e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1181e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 11820ae62c18SNate Lawson /* Report the message only once every second. */ 11830ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1184e0f66ef8SJohn Baldwin printf( 11850ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1186e0f66ef8SJohn Baldwin ie->ie_name); 1187e0f66ef8SJohn Baldwin } 1188e41bcf3cSJohn Baldwin pause("istorm", 1); 1189e0f66ef8SJohn Baldwin } else 1190e0f66ef8SJohn Baldwin ie->ie_count++; 1191e0f66ef8SJohn Baldwin 1192e0f66ef8SJohn Baldwin /* 1193e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1194e0f66ef8SJohn Baldwin * the interrupt source. 1195e0f66ef8SJohn Baldwin */ 11961ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 11971ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1198e0f66ef8SJohn Baldwin } 1199e0f66ef8SJohn Baldwin 12008088699fSJohn Baldwin /* 1201b4151f71SJohn Baldwin * This is the main code for interrupt threads. 12028088699fSJohn Baldwin */ 120337c84183SPoul-Henning Kamp static void 1204b4151f71SJohn Baldwin ithread_loop(void *arg) 12058088699fSJohn Baldwin { 1206511d1afbSGleb Smirnoff struct epoch_tracker et; 1207e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1208e0f66ef8SJohn Baldwin struct intr_event *ie; 1209b40ce416SJulian Elischer struct thread *td; 1210b4151f71SJohn Baldwin struct proc *p; 1211511d1afbSGleb Smirnoff int wake, epoch_count; 1212*f912e8f2SHans Petter Selasky bool needs_epoch; 12138088699fSJohn Baldwin 1214b40ce416SJulian Elischer td = curthread; 1215b40ce416SJulian Elischer p = td->td_proc; 1216e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1217e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 121891f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1219e0f66ef8SJohn Baldwin ie = ithd->it_event; 1220e0f66ef8SJohn Baldwin ie->ie_count = 0; 1221e4cd31ddSJeff Roberson wake = 0; 12228088699fSJohn Baldwin 12238088699fSJohn Baldwin /* 12248088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 12258088699fSJohn Baldwin * list of handlers, giving each one a go at it. 12268088699fSJohn Baldwin */ 12278088699fSJohn Baldwin for (;;) { 1228b4151f71SJohn Baldwin /* 1229b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1230b4151f71SJohn Baldwin */ 1231b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1232e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 12337ab24ea3SJulian Elischer p->p_pid, td->td_name); 1234b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1235ca9a0ddfSJulian Elischer kthread_exit(); 1236b4151f71SJohn Baldwin } 1237b4151f71SJohn Baldwin 1238e0f66ef8SJohn Baldwin /* 1239e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1240e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1241e0f66ef8SJohn Baldwin * should make another pass. 1242283dfee9SKonstantin Belousov * 1243283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1244283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1245283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1246e0f66ef8SJohn Baldwin */ 1247*f912e8f2SHans Petter Selasky needs_epoch = 1248*f912e8f2SHans Petter Selasky (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0; 1249*f912e8f2SHans Petter Selasky if (needs_epoch) { 1250511d1afbSGleb Smirnoff epoch_count = 0; 1251511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1252511d1afbSGleb Smirnoff } 1253511d1afbSGleb Smirnoff while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) { 1254e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 1255*f912e8f2SHans Petter Selasky if (needs_epoch && 1256511d1afbSGleb Smirnoff ++epoch_count >= intr_epoch_batch) { 1257511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 1258511d1afbSGleb Smirnoff epoch_count = 0; 1259511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1260511d1afbSGleb Smirnoff } 1261511d1afbSGleb Smirnoff } 1262*f912e8f2SHans Petter Selasky if (needs_epoch) 1263511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 12647870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 12657870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 12668088699fSJohn Baldwin 12678088699fSJohn Baldwin /* 12688088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 12698088699fSJohn Baldwin * lock. This may take a while and it_need may get 12708088699fSJohn Baldwin * set again, so we have to check it again. 12718088699fSJohn Baldwin */ 1272982d11f8SJeff Roberson thread_lock(td); 127303bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 127403bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 12757870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1276e0f66ef8SJohn Baldwin ie->ie_count = 0; 1277686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IWAIT); 1278686bcb5cSJeff Roberson } else { 1279e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1280e4cd31ddSJeff Roberson wake = 1; 1281e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1282e4cd31ddSJeff Roberson } 1283982d11f8SJeff Roberson thread_unlock(td); 1284686bcb5cSJeff Roberson } 1285e4cd31ddSJeff Roberson if (wake) { 1286e4cd31ddSJeff Roberson wakeup(ithd); 1287e4cd31ddSJeff Roberson wake = 0; 1288e4cd31ddSJeff Roberson } 12898088699fSJohn Baldwin } 12901931cf94SJohn Baldwin } 12911ee1b687SJohn Baldwin 12921ee1b687SJohn Baldwin /* 12931ee1b687SJohn Baldwin * Main interrupt handling body. 12941ee1b687SJohn Baldwin * 12951ee1b687SJohn Baldwin * Input: 12961ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 12971ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 12981ee1b687SJohn Baldwin * handlers as their main argument. 12991ee1b687SJohn Baldwin * Return value: 13001ee1b687SJohn Baldwin * o 0: everything ok. 13011ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 13021ee1b687SJohn Baldwin */ 13031ee1b687SJohn Baldwin int 13041ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 13051ee1b687SJohn Baldwin { 13061ee1b687SJohn Baldwin struct intr_handler *ih; 13071f255bd3SAlexander Motin struct trapframe *oldframe; 13081ee1b687SJohn Baldwin struct thread *td; 1309e0fa977eSAndriy Gapon int phase; 131082a5a275SAndriy Gapon int ret; 131182a5a275SAndriy Gapon bool filter, thread; 13121ee1b687SJohn Baldwin 13131ee1b687SJohn Baldwin td = curthread; 13141ee1b687SJohn Baldwin 1315b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1316b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1317b7627840SKonstantin Belousov #endif 1318b7627840SKonstantin Belousov 13191ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1320111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 13211ee1b687SJohn Baldwin return (EINVAL); 13221ee1b687SJohn Baldwin 13231ee1b687SJohn Baldwin /* 13241ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 13251ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 13261ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 13271ee1b687SJohn Baldwin * a trapframe as its argument. 13281ee1b687SJohn Baldwin */ 13291ee1b687SJohn Baldwin td->td_intr_nesting_level++; 133082a5a275SAndriy Gapon filter = false; 133182a5a275SAndriy Gapon thread = false; 13321ee1b687SJohn Baldwin ret = 0; 13331ee1b687SJohn Baldwin critical_enter(); 13341f255bd3SAlexander Motin oldframe = td->td_intr_frame; 13351f255bd3SAlexander Motin td->td_intr_frame = frame; 1336111b043cSAndriy Gapon 1337e0fa977eSAndriy Gapon phase = ie->ie_phase; 1338e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1339e0fa977eSAndriy Gapon 1340e0fa977eSAndriy Gapon /* 1341e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1342e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1343e0fa977eSAndriy Gapon */ 1344e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1345e0fa977eSAndriy Gapon 1346111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 134782a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 134882a5a275SAndriy Gapon continue; 13491ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 135082a5a275SAndriy Gapon thread = true; 13511ee1b687SJohn Baldwin continue; 13521ee1b687SJohn Baldwin } 13531ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 13541ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 13551ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 13561ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 13571ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 13581ee1b687SJohn Baldwin else 13591ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 136089fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 136189fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 136289fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 136389fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 136489fc20ccSAndriy Gapon ih->ih_name)); 136582a5a275SAndriy Gapon filter = filter || ret == FILTER_HANDLED; 136689fc20ccSAndriy Gapon 13671ee1b687SJohn Baldwin /* 13681ee1b687SJohn Baldwin * Wrapper handler special handling: 13691ee1b687SJohn Baldwin * 13701ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 13711ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 13721ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 13731ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 13741ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 13751ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 13761ee1b687SJohn Baldwin * of just an ithread handler. 13771ee1b687SJohn Baldwin * 13781ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 137982a5a275SAndriy Gapon * their own. 13801ee1b687SJohn Baldwin */ 13811ee1b687SJohn Baldwin if (!thread) { 13821ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 138382a5a275SAndriy Gapon thread = true; 13841ee1b687SJohn Baldwin } 13851ee1b687SJohn Baldwin } 1386e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1387e0fa977eSAndriy Gapon 13881f255bd3SAlexander Motin td->td_intr_frame = oldframe; 13891ee1b687SJohn Baldwin 13901ee1b687SJohn Baldwin if (thread) { 13911ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 13921ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 13931ee1b687SJohn Baldwin } else { 13941ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 13951ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 13961ee1b687SJohn Baldwin } 13971ee1b687SJohn Baldwin 13981ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 13991ee1b687SJohn Baldwin if (thread) { 1400ba3f7276SMatt Macy int error __unused; 1401ba3f7276SMatt Macy 14021ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 14031ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 14041ee1b687SJohn Baldwin } 14051ee1b687SJohn Baldwin critical_exit(); 14061ee1b687SJohn Baldwin td->td_intr_nesting_level--; 140782a5a275SAndriy Gapon #ifdef notyet 140882a5a275SAndriy Gapon /* The interrupt is not aknowledged by any filter and has no ithread. */ 140982a5a275SAndriy Gapon if (!thread && !filter) 141082a5a275SAndriy Gapon return (EINVAL); 141182a5a275SAndriy Gapon #endif 14121ee1b687SJohn Baldwin return (0); 14131ee1b687SJohn Baldwin } 14141931cf94SJohn Baldwin 14158b201c42SJohn Baldwin #ifdef DDB 14168b201c42SJohn Baldwin /* 14178b201c42SJohn Baldwin * Dump details about an interrupt handler 14188b201c42SJohn Baldwin */ 14198b201c42SJohn Baldwin static void 1420e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 14218b201c42SJohn Baldwin { 14228b201c42SJohn Baldwin int comma; 14238b201c42SJohn Baldwin 14248b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 14258b201c42SJohn Baldwin switch (ih->ih_pri) { 14268b201c42SJohn Baldwin case PI_REALTIME: 14278b201c42SJohn Baldwin db_printf("CLK "); 14288b201c42SJohn Baldwin break; 14298b201c42SJohn Baldwin case PI_AV: 14308b201c42SJohn Baldwin db_printf("AV "); 14318b201c42SJohn Baldwin break; 1432d3305205SJohn Baldwin case PI_TTY: 14338b201c42SJohn Baldwin db_printf("TTY "); 14348b201c42SJohn Baldwin break; 14358b201c42SJohn Baldwin case PI_NET: 14368b201c42SJohn Baldwin db_printf("NET "); 14378b201c42SJohn Baldwin break; 14388b201c42SJohn Baldwin case PI_DISK: 14398b201c42SJohn Baldwin db_printf("DISK"); 14408b201c42SJohn Baldwin break; 14418b201c42SJohn Baldwin case PI_DULL: 14428b201c42SJohn Baldwin db_printf("DULL"); 14438b201c42SJohn Baldwin break; 14448b201c42SJohn Baldwin default: 14458b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 14468b201c42SJohn Baldwin db_printf("SWI "); 14478b201c42SJohn Baldwin else 14488b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 14498b201c42SJohn Baldwin break; 14508b201c42SJohn Baldwin } 14518b201c42SJohn Baldwin db_printf(" "); 1452b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1453b887a155SKonstantin Belousov db_printf("[F]"); 1454b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1455b887a155SKonstantin Belousov } 1456b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1457b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1458b887a155SKonstantin Belousov db_printf(","); 1459b887a155SKonstantin Belousov db_printf("[H]"); 14608b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1461b887a155SKonstantin Belousov } 14628b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 14638b201c42SJohn Baldwin if (ih->ih_need || 1464ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 14658b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 14668b201c42SJohn Baldwin db_printf(" {"); 14678b201c42SJohn Baldwin comma = 0; 14688b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 14698b201c42SJohn Baldwin if (comma) 14708b201c42SJohn Baldwin db_printf(", "); 14718b201c42SJohn Baldwin db_printf("EXCL"); 14728b201c42SJohn Baldwin comma = 1; 14738b201c42SJohn Baldwin } 14748b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 14758b201c42SJohn Baldwin if (comma) 14768b201c42SJohn Baldwin db_printf(", "); 14778b201c42SJohn Baldwin db_printf("ENTROPY"); 14788b201c42SJohn Baldwin comma = 1; 14798b201c42SJohn Baldwin } 14808b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 14818b201c42SJohn Baldwin if (comma) 14828b201c42SJohn Baldwin db_printf(", "); 14838b201c42SJohn Baldwin db_printf("DEAD"); 14848b201c42SJohn Baldwin comma = 1; 14858b201c42SJohn Baldwin } 14868b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 14878b201c42SJohn Baldwin if (comma) 14888b201c42SJohn Baldwin db_printf(", "); 14898b201c42SJohn Baldwin db_printf("MPSAFE"); 14908b201c42SJohn Baldwin comma = 1; 14918b201c42SJohn Baldwin } 14928b201c42SJohn Baldwin if (ih->ih_need) { 14938b201c42SJohn Baldwin if (comma) 14948b201c42SJohn Baldwin db_printf(", "); 14958b201c42SJohn Baldwin db_printf("NEED"); 14968b201c42SJohn Baldwin } 14978b201c42SJohn Baldwin db_printf("}"); 14988b201c42SJohn Baldwin } 14998b201c42SJohn Baldwin db_printf("\n"); 15008b201c42SJohn Baldwin } 15018b201c42SJohn Baldwin 15028b201c42SJohn Baldwin /* 1503e0f66ef8SJohn Baldwin * Dump details about a event. 15048b201c42SJohn Baldwin */ 15058b201c42SJohn Baldwin void 1506e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 15078b201c42SJohn Baldwin { 1508e0f66ef8SJohn Baldwin struct intr_handler *ih; 1509e0f66ef8SJohn Baldwin struct intr_thread *it; 15108b201c42SJohn Baldwin int comma; 15118b201c42SJohn Baldwin 1512e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1513e0f66ef8SJohn Baldwin it = ie->ie_thread; 1514e0f66ef8SJohn Baldwin if (it != NULL) 1515e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1516e0f66ef8SJohn Baldwin else 1517e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1518c4eb6630SGleb Smirnoff if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 || 1519e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 15208b201c42SJohn Baldwin db_printf(" {"); 15218b201c42SJohn Baldwin comma = 0; 1522e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 15238b201c42SJohn Baldwin db_printf("SOFT"); 15248b201c42SJohn Baldwin comma = 1; 15258b201c42SJohn Baldwin } 1526e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 15278b201c42SJohn Baldwin if (comma) 15288b201c42SJohn Baldwin db_printf(", "); 1529e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 15308b201c42SJohn Baldwin comma = 1; 15318b201c42SJohn Baldwin } 1532e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 15338b201c42SJohn Baldwin if (comma) 15348b201c42SJohn Baldwin db_printf(", "); 15358b201c42SJohn Baldwin db_printf("NEED"); 15368b201c42SJohn Baldwin } 15378b201c42SJohn Baldwin db_printf("}"); 15388b201c42SJohn Baldwin } 15398b201c42SJohn Baldwin db_printf("\n"); 15408b201c42SJohn Baldwin 15418b201c42SJohn Baldwin if (handlers) 1542111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 15438b201c42SJohn Baldwin db_dump_intrhand(ih); 15448b201c42SJohn Baldwin } 1545e0f66ef8SJohn Baldwin 1546e0f66ef8SJohn Baldwin /* 1547e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1548e0f66ef8SJohn Baldwin */ 1549e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1550e0f66ef8SJohn Baldwin { 1551e0f66ef8SJohn Baldwin struct intr_event *ie; 155219e9205aSJohn Baldwin int all, verbose; 1553e0f66ef8SJohn Baldwin 1554dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1555dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1556e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1557111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1558e0f66ef8SJohn Baldwin continue; 1559e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 156019e9205aSJohn Baldwin if (db_pager_quit) 156119e9205aSJohn Baldwin break; 1562e0f66ef8SJohn Baldwin } 1563e0f66ef8SJohn Baldwin } 15648b201c42SJohn Baldwin #endif /* DDB */ 15658b201c42SJohn Baldwin 1566b4151f71SJohn Baldwin /* 15678088699fSJohn Baldwin * Start standard software interrupt threads 15681931cf94SJohn Baldwin */ 15691931cf94SJohn Baldwin static void 1570b4151f71SJohn Baldwin start_softintr(void *dummy) 15711931cf94SJohn Baldwin { 1572b4151f71SJohn Baldwin 15738d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 15748d809d50SJeff Roberson panic("died while creating vm swi ithread"); 15751931cf94SJohn Baldwin } 1576237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1577237fdd78SRobert Watson NULL); 15781931cf94SJohn Baldwin 1579d279178dSThomas Moestl /* 1580d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1581d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1582d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1583d279178dSThomas Moestl * independent. 1584d279178dSThomas Moestl * 1585d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1586d279178dSThomas Moestl * calculate things at run time. 1587d279178dSThomas Moestl */ 1588d279178dSThomas Moestl static int 1589d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1590d279178dSThomas Moestl { 1591521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1592d279178dSThomas Moestl } 1593d279178dSThomas Moestl 1594d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1595d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1596d279178dSThomas Moestl 1597d279178dSThomas Moestl static int 1598d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1599d279178dSThomas Moestl { 160085729c2cSJuli Mallett #ifdef SCTL_MASK32 160185729c2cSJuli Mallett uint32_t *intrcnt32; 160285729c2cSJuli Mallett unsigned i; 160385729c2cSJuli Mallett int error; 160485729c2cSJuli Mallett 160585729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 160685729c2cSJuli Mallett if (!req->oldptr) 160785729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 160885729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 160985729c2cSJuli Mallett if (intrcnt32 == NULL) 161085729c2cSJuli Mallett return (ENOMEM); 161185729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 161285729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 161385729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 161485729c2cSJuli Mallett free(intrcnt32, M_TEMP); 161585729c2cSJuli Mallett return (error); 161685729c2cSJuli Mallett } 161785729c2cSJuli Mallett #endif 1618521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1619d279178dSThomas Moestl } 1620d279178dSThomas Moestl 1621d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1622d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 16238b201c42SJohn Baldwin 16248b201c42SJohn Baldwin #ifdef DDB 16258b201c42SJohn Baldwin /* 16268b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 16278b201c42SJohn Baldwin */ 16288b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 16298b201c42SJohn Baldwin { 16308b201c42SJohn Baldwin u_long *i; 16318b201c42SJohn Baldwin char *cp; 1632521ea19dSAttilio Rao u_int j; 16338b201c42SJohn Baldwin 16348b201c42SJohn Baldwin cp = intrnames; 1635521ea19dSAttilio Rao j = 0; 1636521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1637521ea19dSAttilio Rao i++, j++) { 16388b201c42SJohn Baldwin if (*cp == '\0') 16398b201c42SJohn Baldwin break; 16408b201c42SJohn Baldwin if (*i != 0) 16418b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 16428b201c42SJohn Baldwin cp += strlen(cp) + 1; 16438b201c42SJohn Baldwin } 16448b201c42SJohn Baldwin } 16458b201c42SJohn Baldwin #endif 1646