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> 513e5da754SJohn Baldwin #include <sys/random.h> 52b4151f71SJohn Baldwin #include <sys/resourcevar.h> 5363710c4dSJohn Baldwin #include <sys/sched.h> 54eaf86d16SJohn Baldwin #include <sys/smp.h> 55d279178dSThomas Moestl #include <sys/sysctl.h> 566205924aSKip Macy #include <sys/syslog.h> 571931cf94SJohn Baldwin #include <sys/unistd.h> 581931cf94SJohn Baldwin #include <sys/vmmeter.h> 591931cf94SJohn Baldwin #include <machine/atomic.h> 601931cf94SJohn Baldwin #include <machine/cpu.h> 618088699fSJohn Baldwin #include <machine/md_var.h> 62b4151f71SJohn Baldwin #include <machine/stdarg.h> 638b201c42SJohn Baldwin #ifdef DDB 648b201c42SJohn Baldwin #include <ddb/ddb.h> 658b201c42SJohn Baldwin #include <ddb/db_sym.h> 668b201c42SJohn Baldwin #endif 67425f9fdaSStefan Eßer 68e0f66ef8SJohn Baldwin /* 69e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 70e0f66ef8SJohn Baldwin */ 71e0f66ef8SJohn Baldwin struct intr_thread { 72e0f66ef8SJohn Baldwin struct intr_event *it_event; 73e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 74e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 75e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 763e5da754SJohn Baldwin }; 773e5da754SJohn Baldwin 78e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 79e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 80e4cd31ddSJeff Roberson #define IT_WAIT 0x000002 /* Thread is waiting for completion. */ 81e0f66ef8SJohn Baldwin 82e0f66ef8SJohn Baldwin struct intr_entropy { 83e0f66ef8SJohn Baldwin struct thread *td; 84e0f66ef8SJohn Baldwin uintptr_t event; 85e0f66ef8SJohn Baldwin }; 86e0f66ef8SJohn Baldwin 87e0f66ef8SJohn Baldwin struct intr_event *clk_intr_event; 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 940ae62c18SNate Lawson static int intr_storm_threshold = 1000; 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"); 98e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 99e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 1009b33b154SJeff Roberson static struct mtx event_lock; 1019b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 1027b1fe905SBruce Evans 103e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 1041ee1b687SJohn Baldwin static int intr_event_schedule_thread(struct intr_event *ie); 105e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 106e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 107bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 108bafe5a31SPaolo Pisati struct intr_event *ie); 1097b1fe905SBruce Evans static void ithread_loop(void *); 110e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1117b1fe905SBruce Evans static void start_softintr(void *); 1127870c3c6SJohn Baldwin 113bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 114b4151f71SJohn Baldwin u_char 115e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1169a94c9c5SJohn Baldwin { 117b4151f71SJohn Baldwin u_char pri; 1189a94c9c5SJohn Baldwin 119b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1205a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1219a94c9c5SJohn Baldwin switch (flags) { 122b4151f71SJohn Baldwin case INTR_TYPE_TTY: 123d3305205SJohn Baldwin pri = PI_TTY; 1249a94c9c5SJohn Baldwin break; 1259a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1269a94c9c5SJohn Baldwin pri = PI_DISK; 1279a94c9c5SJohn Baldwin break; 1289a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1299a94c9c5SJohn Baldwin pri = PI_NET; 1309a94c9c5SJohn Baldwin break; 1319a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 132d3305205SJohn Baldwin pri = PI_DISK; 1339a94c9c5SJohn Baldwin break; 134d3305205SJohn Baldwin case INTR_TYPE_AV: 1355a280d9cSPeter Wemm pri = PI_AV; 1365a280d9cSPeter Wemm break; 137b4151f71SJohn Baldwin case INTR_TYPE_CLK: 138b4151f71SJohn Baldwin pri = PI_REALTIME; 139b4151f71SJohn Baldwin break; 1409a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1419a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1429a94c9c5SJohn Baldwin break; 1439a94c9c5SJohn Baldwin default: 144b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 145e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1469a94c9c5SJohn Baldwin } 1479a94c9c5SJohn Baldwin 1489a94c9c5SJohn Baldwin return pri; 1499a94c9c5SJohn Baldwin } 1509a94c9c5SJohn Baldwin 151b4151f71SJohn Baldwin /* 152e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 153b4151f71SJohn Baldwin */ 154b4151f71SJohn Baldwin static void 155e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 156b4151f71SJohn Baldwin { 157e0f66ef8SJohn Baldwin struct intr_event *ie; 158b40ce416SJulian Elischer struct thread *td; 159e0f66ef8SJohn Baldwin u_char pri; 1608088699fSJohn Baldwin 161e0f66ef8SJohn Baldwin ie = ithd->it_event; 162e0f66ef8SJohn Baldwin td = ithd->it_thread; 163111b043cSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 164b4151f71SJohn Baldwin 165e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 166111b043cSAndriy Gapon if (CK_SLIST_EMPTY(&ie->ie_handlers)) 167e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 168e0f66ef8SJohn Baldwin else 169111b043cSAndriy Gapon pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri; 170e80fb434SRobert Drehmel 171e0f66ef8SJohn Baldwin /* Update name and priority. */ 1727ab24ea3SJulian Elischer strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 17344ad5475SJohn Baldwin #ifdef KTR 17444ad5475SJohn Baldwin sched_clear_tdname(td); 17544ad5475SJohn Baldwin #endif 176982d11f8SJeff Roberson thread_lock(td); 177e0f66ef8SJohn Baldwin sched_prio(td, pri); 178982d11f8SJeff Roberson thread_unlock(td); 179b4151f71SJohn Baldwin } 180e0f66ef8SJohn Baldwin 181e0f66ef8SJohn Baldwin /* 182e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 183e0f66ef8SJohn Baldwin */ 184e0f66ef8SJohn Baldwin static void 185e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 186e0f66ef8SJohn Baldwin { 187e0f66ef8SJohn Baldwin struct intr_handler *ih; 188e0f66ef8SJohn Baldwin char *last; 189e0f66ef8SJohn Baldwin int missed, space; 190e0f66ef8SJohn Baldwin 191e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 192e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 193e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 194e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ENTROPY; 1950811d60aSJohn Baldwin missed = 0; 196e0f66ef8SJohn Baldwin space = 1; 197e0f66ef8SJohn Baldwin 198e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 199111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 200*67da50a0SIan Lepore if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 < 201e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 202e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 203e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 204e0f66ef8SJohn Baldwin space = 0; 2050811d60aSJohn Baldwin } else 2060811d60aSJohn Baldwin missed++; 2070811d60aSJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 208e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ENTROPY; 2090811d60aSJohn Baldwin } 210e0f66ef8SJohn Baldwin 211e0f66ef8SJohn Baldwin /* 212*67da50a0SIan Lepore * If there is only one handler and its name is too long, just copy in 213*67da50a0SIan Lepore * as much of the end of the name (includes the unit number) as will 214*67da50a0SIan Lepore * fit. Otherwise, we have multiple handlers and not all of the names 215*67da50a0SIan Lepore * will fit. Add +'s to indicate missing names. If we run out of room 216*67da50a0SIan Lepore * and still have +'s to add, change the last character from a + to a *. 217e0f66ef8SJohn Baldwin */ 218*67da50a0SIan Lepore if (missed == 1 && space == 1) { 219*67da50a0SIan Lepore ih = CK_SLIST_FIRST(&ie->ie_handlers); 220*67da50a0SIan Lepore missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 - 221*67da50a0SIan Lepore sizeof(ie->ie_fullname); 222*67da50a0SIan Lepore strcat(ie->ie_fullname, (missed == 0) ? " " : "-"); 223*67da50a0SIan Lepore strcat(ie->ie_fullname, &ih->ih_name[missed]); 224*67da50a0SIan Lepore missed = 0; 225*67da50a0SIan Lepore } 226e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2270811d60aSJohn Baldwin while (missed-- > 0) { 228e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 229e0f66ef8SJohn Baldwin if (*last == '+') { 230e0f66ef8SJohn Baldwin *last = '*'; 231e0f66ef8SJohn Baldwin break; 232b4151f71SJohn Baldwin } else 233e0f66ef8SJohn Baldwin *last = '+'; 234e0f66ef8SJohn Baldwin } else if (space) { 235e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 236e0f66ef8SJohn Baldwin space = 0; 237e0f66ef8SJohn Baldwin } else 238e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 239b4151f71SJohn Baldwin } 240e0f66ef8SJohn Baldwin 241e0f66ef8SJohn Baldwin /* 242e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 243e0f66ef8SJohn Baldwin * name. 244e0f66ef8SJohn Baldwin */ 245e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 246e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 247e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 248b4151f71SJohn Baldwin } 249b4151f71SJohn Baldwin 250b4151f71SJohn Baldwin int 2519b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2521ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 253066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2541ee1b687SJohn Baldwin const char *fmt, ...) 255bafe5a31SPaolo Pisati { 256bafe5a31SPaolo Pisati struct intr_event *ie; 257bafe5a31SPaolo Pisati va_list ap; 258bafe5a31SPaolo Pisati 259bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 260bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 261bafe5a31SPaolo Pisati return (EINVAL); 262bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 263bafe5a31SPaolo Pisati ie->ie_source = source; 2641ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2651ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2661ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2676d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 268bafe5a31SPaolo Pisati ie->ie_flags = flags; 2699b33b154SJeff Roberson ie->ie_irq = irq; 270eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 271111b043cSAndriy Gapon CK_SLIST_INIT(&ie->ie_handlers); 272bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 273bafe5a31SPaolo Pisati 274bafe5a31SPaolo Pisati va_start(ap, fmt); 275bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 276bafe5a31SPaolo Pisati va_end(ap); 277bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2789b33b154SJeff Roberson mtx_lock(&event_lock); 279bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2809b33b154SJeff Roberson mtx_unlock(&event_lock); 281bafe5a31SPaolo Pisati if (event != NULL) 282bafe5a31SPaolo Pisati *event = ie; 283bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 284bafe5a31SPaolo Pisati return (0); 285bafe5a31SPaolo Pisati } 286b4151f71SJohn Baldwin 287eaf86d16SJohn Baldwin /* 288eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 289eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 29029dfb631SConrad Meyer * platforms this request will fail. Using a cpu id of NOCPU unbinds 291eaf86d16SJohn Baldwin * the interrupt event. 292eaf86d16SJohn Baldwin */ 29329dfb631SConrad Meyer static int 29429dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread) 295eaf86d16SJohn Baldwin { 2969b33b154SJeff Roberson lwpid_t id; 297eaf86d16SJohn Baldwin int error; 298eaf86d16SJohn Baldwin 299eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 300eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 301eaf86d16SJohn Baldwin return (EINVAL); 302eaf86d16SJohn Baldwin 303eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 304eaf86d16SJohn Baldwin return (EOPNOTSUPP); 305cebc7fb1SJohn Baldwin 306cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 307cebc7fb1SJohn Baldwin if (error) 308cebc7fb1SJohn Baldwin return (error); 309cebc7fb1SJohn Baldwin 3109b33b154SJeff Roberson /* 311cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 312cebc7fb1SJohn Baldwin * permissions, etc. 3139b33b154SJeff Roberson */ 31429dfb631SConrad Meyer if (bindithread) { 315eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3169b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3179b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 318eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 31981198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3209b33b154SJeff Roberson if (error) 3219b33b154SJeff Roberson return (error); 3229b33b154SJeff Roberson } else 323eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 32429dfb631SConrad Meyer } 32529dfb631SConrad Meyer if (bindirq) 326eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 327cebc7fb1SJohn Baldwin if (error) { 32829dfb631SConrad Meyer if (bindithread) { 329cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 330cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 33181198539SAlexander V. Chernikov cpu = ie->ie_cpu; 332cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 333cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33481198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 335cebc7fb1SJohn Baldwin } else 336cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 33729dfb631SConrad Meyer } 338eaf86d16SJohn Baldwin return (error); 339cebc7fb1SJohn Baldwin } 340cebc7fb1SJohn Baldwin 34129dfb631SConrad Meyer if (bindirq) { 342eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 343eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3449b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 34529dfb631SConrad Meyer } 3469b33b154SJeff Roberson 3479b33b154SJeff Roberson return (error); 3489b33b154SJeff Roberson } 3499b33b154SJeff Roberson 35029dfb631SConrad Meyer /* 35129dfb631SConrad Meyer * Bind an interrupt event to the specified CPU. For supported platforms, any 35229dfb631SConrad Meyer * associated ithreads as well as the primary interrupt context will be bound 35329dfb631SConrad Meyer * to the specificed CPU. 35429dfb631SConrad Meyer */ 35529dfb631SConrad Meyer int 35629dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu) 35729dfb631SConrad Meyer { 35829dfb631SConrad Meyer 35929dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, true)); 36029dfb631SConrad Meyer } 36129dfb631SConrad Meyer 36229dfb631SConrad Meyer /* 36329dfb631SConrad Meyer * Bind an interrupt event to the specified CPU, but do not bind associated 36429dfb631SConrad Meyer * ithreads. 36529dfb631SConrad Meyer */ 36629dfb631SConrad Meyer int 36729dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu) 36829dfb631SConrad Meyer { 36929dfb631SConrad Meyer 37029dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, false)); 37129dfb631SConrad Meyer } 37229dfb631SConrad Meyer 37329dfb631SConrad Meyer /* 37429dfb631SConrad Meyer * Bind an interrupt event's ithread to the specified CPU. 37529dfb631SConrad Meyer */ 37629dfb631SConrad Meyer int 37729dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu) 37829dfb631SConrad Meyer { 37929dfb631SConrad Meyer 38029dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, false, true)); 38129dfb631SConrad Meyer } 38229dfb631SConrad Meyer 3839b33b154SJeff Roberson static struct intr_event * 3849b33b154SJeff Roberson intr_lookup(int irq) 3859b33b154SJeff Roberson { 3869b33b154SJeff Roberson struct intr_event *ie; 3879b33b154SJeff Roberson 3889b33b154SJeff Roberson mtx_lock(&event_lock); 3899b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 3909b33b154SJeff Roberson if (ie->ie_irq == irq && 3919b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 392111b043cSAndriy Gapon CK_SLIST_FIRST(&ie->ie_handlers) != NULL) 3939b33b154SJeff Roberson break; 3949b33b154SJeff Roberson mtx_unlock(&event_lock); 3959b33b154SJeff Roberson return (ie); 3969b33b154SJeff Roberson } 3979b33b154SJeff Roberson 3989b33b154SJeff Roberson int 39929dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m) 4009b33b154SJeff Roberson { 4019b33b154SJeff Roberson struct intr_event *ie; 4029b33b154SJeff Roberson cpuset_t *mask; 4033fe93b94SAdrian Chadd int cpu, n; 4049b33b154SJeff Roberson 4059b33b154SJeff Roberson mask = m; 4069b33b154SJeff Roberson cpu = NOCPU; 4079b33b154SJeff Roberson /* 4089b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 4099b33b154SJeff Roberson * only one cpu is in the set. 4109b33b154SJeff Roberson */ 4119b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 4129b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 4139b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 4149b33b154SJeff Roberson continue; 4159b33b154SJeff Roberson if (cpu != NOCPU) 4169b33b154SJeff Roberson return (EINVAL); 4173fe93b94SAdrian Chadd cpu = n; 4189b33b154SJeff Roberson } 4199b33b154SJeff Roberson } 4209b33b154SJeff Roberson ie = intr_lookup(irq); 4219b33b154SJeff Roberson if (ie == NULL) 4229b33b154SJeff Roberson return (ESRCH); 42329dfb631SConrad Meyer switch (mode) { 42429dfb631SConrad Meyer case CPU_WHICH_IRQ: 4259bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 42629dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 42729dfb631SConrad Meyer return (intr_event_bind_irqonly(ie, cpu)); 42829dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 42929dfb631SConrad Meyer return (intr_event_bind_ithread(ie, cpu)); 43029dfb631SConrad Meyer default: 43129dfb631SConrad Meyer return (EINVAL); 43229dfb631SConrad Meyer } 4339b33b154SJeff Roberson } 4349b33b154SJeff Roberson 4359b33b154SJeff Roberson int 43629dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m) 4379b33b154SJeff Roberson { 4389b33b154SJeff Roberson struct intr_event *ie; 43929dfb631SConrad Meyer struct thread *td; 44029dfb631SConrad Meyer struct proc *p; 4419b33b154SJeff Roberson cpuset_t *mask; 44229dfb631SConrad Meyer lwpid_t id; 44329dfb631SConrad Meyer int error; 4449b33b154SJeff Roberson 4459b33b154SJeff Roberson mask = m; 4469b33b154SJeff Roberson ie = intr_lookup(irq); 4479b33b154SJeff Roberson if (ie == NULL) 4489b33b154SJeff Roberson return (ESRCH); 44929dfb631SConrad Meyer 45029dfb631SConrad Meyer error = 0; 4519b33b154SJeff Roberson CPU_ZERO(mask); 45229dfb631SConrad Meyer switch (mode) { 45329dfb631SConrad Meyer case CPU_WHICH_IRQ: 45429dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 4559b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4569b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4579b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4589b33b154SJeff Roberson else 4599b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 460eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 46129dfb631SConrad Meyer break; 46229dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 46329dfb631SConrad Meyer mtx_lock(&ie->ie_lock); 46429dfb631SConrad Meyer if (ie->ie_thread == NULL) { 46529dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 46629dfb631SConrad Meyer CPU_COPY(cpuset_root, mask); 46729dfb631SConrad Meyer } else { 46829dfb631SConrad Meyer id = ie->ie_thread->it_thread->td_tid; 46929dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 47029dfb631SConrad Meyer error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL); 47129dfb631SConrad Meyer if (error != 0) 47229dfb631SConrad Meyer return (error); 47329dfb631SConrad Meyer CPU_COPY(&td->td_cpuset->cs_mask, mask); 47429dfb631SConrad Meyer PROC_UNLOCK(p); 47529dfb631SConrad Meyer } 47629dfb631SConrad Meyer default: 47729dfb631SConrad Meyer return (EINVAL); 47829dfb631SConrad Meyer } 479eaf86d16SJohn Baldwin return (0); 480eaf86d16SJohn Baldwin } 481eaf86d16SJohn Baldwin 482b4151f71SJohn Baldwin int 483e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 484b4151f71SJohn Baldwin { 485b4151f71SJohn Baldwin 4869b33b154SJeff Roberson mtx_lock(&event_lock); 487e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 488111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 489e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4909b33b154SJeff Roberson mtx_unlock(&event_lock); 491e0f66ef8SJohn Baldwin return (EBUSY); 4924d29cb2dSJohn Baldwin } 493e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 4949477358dSJohn Baldwin #ifndef notyet 4959477358dSJohn Baldwin if (ie->ie_thread != NULL) { 4969477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 4979477358dSJohn Baldwin ie->ie_thread = NULL; 4989477358dSJohn Baldwin } 4999477358dSJohn Baldwin #endif 500e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5019b33b154SJeff Roberson mtx_unlock(&event_lock); 502e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 503e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 504e0f66ef8SJohn Baldwin return (0); 505e0f66ef8SJohn Baldwin } 506e0f66ef8SJohn Baldwin 507e0f66ef8SJohn Baldwin static struct intr_thread * 508e0f66ef8SJohn Baldwin ithread_create(const char *name) 509e0f66ef8SJohn Baldwin { 510e0f66ef8SJohn Baldwin struct intr_thread *ithd; 511e0f66ef8SJohn Baldwin struct thread *td; 512e0f66ef8SJohn Baldwin int error; 513e0f66ef8SJohn Baldwin 514e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 515e0f66ef8SJohn Baldwin 5167ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 5177ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 5189ef95d01SJulian Elischer 0, "intr", "%s", name); 519e0f66ef8SJohn Baldwin if (error) 5203745c395SJulian Elischer panic("kproc_create() failed with %d", error); 521982d11f8SJeff Roberson thread_lock(td); 522ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 523e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 524982d11f8SJeff Roberson thread_unlock(td); 525e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 526e0f66ef8SJohn Baldwin ithd->it_thread = td; 527e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 528e0f66ef8SJohn Baldwin return (ithd); 529e0f66ef8SJohn Baldwin } 530e0f66ef8SJohn Baldwin 531e0f66ef8SJohn Baldwin static void 532e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 533e0f66ef8SJohn Baldwin { 534e0f66ef8SJohn Baldwin struct thread *td; 535e0f66ef8SJohn Baldwin 536bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 537e0f66ef8SJohn Baldwin td = ithread->it_thread; 538982d11f8SJeff Roberson thread_lock(td); 539e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 54071fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 54171fad9fdSJulian Elischer TD_CLR_IWAIT(td); 542f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 543b4151f71SJohn Baldwin } 544982d11f8SJeff Roberson thread_unlock(td); 545b4151f71SJohn Baldwin } 546b4151f71SJohn Baldwin 547b4151f71SJohn Baldwin int 548e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 549ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 550ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 551b4151f71SJohn Baldwin { 552e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 553111b043cSAndriy Gapon struct intr_handler **prevptr; 554e0f66ef8SJohn Baldwin struct intr_thread *it; 555b4151f71SJohn Baldwin 556ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 557b4151f71SJohn Baldwin return (EINVAL); 558b4151f71SJohn Baldwin 559e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 560e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 561ef544f63SPaolo Pisati ih->ih_filter = filter; 562b4151f71SJohn Baldwin ih->ih_handler = handler; 563b4151f71SJohn Baldwin ih->ih_argument = arg; 56437b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 565e0f66ef8SJohn Baldwin ih->ih_event = ie; 566b4151f71SJohn Baldwin ih->ih_pri = pri; 567ef544f63SPaolo Pisati if (flags & INTR_EXCL) 568b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 569b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 570b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 571b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 572b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 573b4151f71SJohn Baldwin 574e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 575e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 576111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 577e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 578111b043cSAndriy Gapon (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 579e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 580b4151f71SJohn Baldwin free(ih, M_ITHREAD); 581b4151f71SJohn Baldwin return (EINVAL); 582b4151f71SJohn Baldwin } 583e0f66ef8SJohn Baldwin } 584e0f66ef8SJohn Baldwin 585e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 586ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 587e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 5880f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 589e0f66ef8SJohn Baldwin else { 590e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 591e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 592e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 593e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 594e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 595e0f66ef8SJohn Baldwin ie->ie_thread = it; 596e0f66ef8SJohn Baldwin it->it_event = ie; 597e0f66ef8SJohn Baldwin ithread_update(it); 598e0f66ef8SJohn Baldwin wakeup(ie); 599e0f66ef8SJohn Baldwin } 600e0f66ef8SJohn Baldwin } 601c9516c94SAlexander Kabaev 602c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 603111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) { 604c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 605c9516c94SAlexander Kabaev break; 606c9516c94SAlexander Kabaev } 607111b043cSAndriy Gapon CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next); 608111b043cSAndriy Gapon 609c9516c94SAlexander Kabaev intr_event_update(ie); 610c9516c94SAlexander Kabaev 611e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 612e0f66ef8SJohn Baldwin ie->ie_name); 613e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 614e0f66ef8SJohn Baldwin 615e0f66ef8SJohn Baldwin if (cookiep != NULL) 616e0f66ef8SJohn Baldwin *cookiep = ih; 617e0f66ef8SJohn Baldwin return (0); 618e0f66ef8SJohn Baldwin } 619b4151f71SJohn Baldwin 620c3045318SJohn Baldwin /* 62137b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 62237b8ef16SJohn Baldwin * interrupt handler. 62337b8ef16SJohn Baldwin */ 62437b8ef16SJohn Baldwin int 62537b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 62637b8ef16SJohn Baldwin const char *descr) 62737b8ef16SJohn Baldwin { 62837b8ef16SJohn Baldwin struct intr_handler *ih; 62937b8ef16SJohn Baldwin size_t space; 63037b8ef16SJohn Baldwin char *start; 63137b8ef16SJohn Baldwin 63237b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 63337b8ef16SJohn Baldwin #ifdef INVARIANTS 634111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 63537b8ef16SJohn Baldwin if (ih == cookie) 63637b8ef16SJohn Baldwin break; 63737b8ef16SJohn Baldwin } 63837b8ef16SJohn Baldwin if (ih == NULL) { 63937b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 640d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 64137b8ef16SJohn Baldwin } 64237b8ef16SJohn Baldwin #endif 64337b8ef16SJohn Baldwin ih = cookie; 64437b8ef16SJohn Baldwin 64537b8ef16SJohn Baldwin /* 64637b8ef16SJohn Baldwin * Look for an existing description by checking for an 64737b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 64837b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 64937b8ef16SJohn Baldwin * description at that point. If one is not found, find the 65037b8ef16SJohn Baldwin * end of the name to use as the insertion point. 65137b8ef16SJohn Baldwin */ 652dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 65337b8ef16SJohn Baldwin if (start == NULL) 654dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 65537b8ef16SJohn Baldwin 65637b8ef16SJohn Baldwin /* 65737b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 65837b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 65937b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 66037b8ef16SJohn Baldwin */ 66137b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 66237b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 66337b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 66437b8ef16SJohn Baldwin return (ENOSPC); 66537b8ef16SJohn Baldwin } 66637b8ef16SJohn Baldwin 66737b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 66837b8ef16SJohn Baldwin *start = ':'; 66937b8ef16SJohn Baldwin strcpy(start + 1, descr); 67037b8ef16SJohn Baldwin intr_event_update(ie); 67137b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 67237b8ef16SJohn Baldwin return (0); 67337b8ef16SJohn Baldwin } 67437b8ef16SJohn Baldwin 67537b8ef16SJohn Baldwin /* 676c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 677c3045318SJohn Baldwin * associated with. 678c3045318SJohn Baldwin */ 679c3045318SJohn Baldwin void * 680c3045318SJohn Baldwin intr_handler_source(void *cookie) 681c3045318SJohn Baldwin { 682c3045318SJohn Baldwin struct intr_handler *ih; 683c3045318SJohn Baldwin struct intr_event *ie; 684c3045318SJohn Baldwin 685c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 686c3045318SJohn Baldwin if (ih == NULL) 687c3045318SJohn Baldwin return (NULL); 688c3045318SJohn Baldwin ie = ih->ih_event; 689c3045318SJohn Baldwin KASSERT(ie != NULL, 690c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 691c3045318SJohn Baldwin ih->ih_name)); 692c3045318SJohn Baldwin return (ie->ie_source); 693c3045318SJohn Baldwin } 694c3045318SJohn Baldwin 695e4cd31ddSJeff Roberson /* 696e0fa977eSAndriy Gapon * If intr_event_handle() is running in the ISR context at the time of the call, 697e0fa977eSAndriy Gapon * then wait for it to complete. 698e0fa977eSAndriy Gapon */ 699e0fa977eSAndriy Gapon static void 700e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie) 701e0fa977eSAndriy Gapon { 702e0fa977eSAndriy Gapon int phase; 703e0fa977eSAndriy Gapon 704e0fa977eSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 705e0fa977eSAndriy Gapon phase = ie->ie_phase; 706e0fa977eSAndriy Gapon 707e0fa977eSAndriy Gapon /* 708e0fa977eSAndriy Gapon * Switch phase to direct future interrupts to the other active counter. 709e0fa977eSAndriy Gapon * Make sure that any preceding stores are visible before the switch. 710e0fa977eSAndriy Gapon */ 711e0fa977eSAndriy Gapon KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity")); 712e0fa977eSAndriy Gapon atomic_store_rel_int(&ie->ie_phase, !phase); 713e0fa977eSAndriy Gapon 714e0fa977eSAndriy Gapon /* 715e0fa977eSAndriy Gapon * This code cooperates with wait-free iteration of ie_handlers 716e0fa977eSAndriy Gapon * in intr_event_handle. 717e0fa977eSAndriy Gapon * Make sure that the removal and the phase update are not reordered 718e0fa977eSAndriy Gapon * with the active count check. 719e0fa977eSAndriy Gapon * Note that no combination of acquire and release fences can provide 720e0fa977eSAndriy Gapon * that guarantee as Store->Load sequences can always be reordered. 721e0fa977eSAndriy Gapon */ 722e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 723e0fa977eSAndriy Gapon 724e0fa977eSAndriy Gapon /* 725e0fa977eSAndriy Gapon * Now wait on the inactive phase. 726e0fa977eSAndriy Gapon * The acquire fence is needed so that that all post-barrier accesses 727e0fa977eSAndriy Gapon * are after the check. 728e0fa977eSAndriy Gapon */ 729e0fa977eSAndriy Gapon while (ie->ie_active[phase] > 0) 730e0fa977eSAndriy Gapon cpu_spinwait(); 731e0fa977eSAndriy Gapon atomic_thread_fence_acq(); 732e0fa977eSAndriy Gapon } 733e0fa977eSAndriy Gapon 73482a5a275SAndriy Gapon static void 73582a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler) 73682a5a275SAndriy Gapon { 73782a5a275SAndriy Gapon struct intr_event *ie; 73882a5a275SAndriy Gapon 73982a5a275SAndriy Gapon ie = handler->ih_event; 74082a5a275SAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 74182a5a275SAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 74282a5a275SAndriy Gapon ("update for a removed handler")); 74382a5a275SAndriy Gapon 74482a5a275SAndriy Gapon if (ie->ie_thread == NULL) { 74582a5a275SAndriy Gapon intr_event_barrier(ie); 74682a5a275SAndriy Gapon return; 74782a5a275SAndriy Gapon } 74882a5a275SAndriy Gapon if ((handler->ih_flags & IH_CHANGED) == 0) { 74982a5a275SAndriy Gapon handler->ih_flags |= IH_CHANGED; 75082a5a275SAndriy Gapon intr_event_schedule_thread(ie); 75182a5a275SAndriy Gapon } 75282a5a275SAndriy Gapon while ((handler->ih_flags & IH_CHANGED) != 0) 75382a5a275SAndriy Gapon msleep(handler, &ie->ie_lock, 0, "ih_barr", 0); 75482a5a275SAndriy Gapon } 75582a5a275SAndriy Gapon 756e0fa977eSAndriy Gapon /* 757e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 758e4cd31ddSJeff Roberson * 759e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 760e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 761e4cd31ddSJeff Roberson * only. Do not use in BSD code. 762e4cd31ddSJeff Roberson */ 763e4cd31ddSJeff Roberson void 764e4cd31ddSJeff Roberson _intr_drain(int irq) 765e4cd31ddSJeff Roberson { 766e4cd31ddSJeff Roberson struct intr_event *ie; 767e4cd31ddSJeff Roberson struct intr_thread *ithd; 768e4cd31ddSJeff Roberson struct thread *td; 769e4cd31ddSJeff Roberson 770e4cd31ddSJeff Roberson ie = intr_lookup(irq); 771e4cd31ddSJeff Roberson if (ie == NULL) 772e4cd31ddSJeff Roberson return; 773e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 774e4cd31ddSJeff Roberson return; 775e4cd31ddSJeff Roberson ithd = ie->ie_thread; 776e4cd31ddSJeff Roberson td = ithd->it_thread; 7775bd186a6SJeff Roberson /* 7785bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 7795bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 7805bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 7815bd186a6SJeff Roberson */ 782e4cd31ddSJeff Roberson thread_lock(td); 783e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 784e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 7855bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 7865bd186a6SJeff Roberson thread_unlock(td); 7875bd186a6SJeff Roberson pause("idrain", 1); 7885bd186a6SJeff Roberson thread_lock(td); 789e4cd31ddSJeff Roberson } 7905bd186a6SJeff Roberson } 7915bd186a6SJeff Roberson thread_unlock(td); 792e4cd31ddSJeff Roberson return; 793e4cd31ddSJeff Roberson } 794e4cd31ddSJeff Roberson 795b4151f71SJohn Baldwin int 796e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 797b4151f71SJohn Baldwin { 798e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 799e0f66ef8SJohn Baldwin struct intr_event *ie; 800e0f66ef8SJohn Baldwin struct intr_handler *ih; 801111b043cSAndriy Gapon struct intr_handler **prevptr; 802e0f66ef8SJohn Baldwin #ifdef notyet 803e0f66ef8SJohn Baldwin int dead; 804b4151f71SJohn Baldwin #endif 805b4151f71SJohn Baldwin 8063e5da754SJohn Baldwin if (handler == NULL) 807b4151f71SJohn Baldwin return (EINVAL); 808e0f66ef8SJohn Baldwin ie = handler->ih_event; 809e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 810e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 8113e5da754SJohn Baldwin handler->ih_name)); 812111b043cSAndriy Gapon 813e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 81491f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 815e0f66ef8SJohn Baldwin ie->ie_name); 816111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 8173e5da754SJohn Baldwin if (ih == handler) 818111b043cSAndriy Gapon break; 819111b043cSAndriy Gapon } 820111b043cSAndriy Gapon if (ih == NULL) { 821111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 822111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 823111b043cSAndriy Gapon } 824111b043cSAndriy Gapon 825de271f01SJohn Baldwin /* 826e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 827e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 828e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 829e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 830e0f66ef8SJohn Baldwin */ 831e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 832111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 833e0fa977eSAndriy Gapon intr_event_barrier(ie); 834e0fa977eSAndriy Gapon intr_event_update(ie); 835e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 836e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 837e0f66ef8SJohn Baldwin return (0); 838e0f66ef8SJohn Baldwin } 839e0f66ef8SJohn Baldwin 840e0f66ef8SJohn Baldwin /* 841e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 842e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 843e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 844e0fa977eSAndriy Gapon * intr_event_handle(). 845de271f01SJohn Baldwin */ 846e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 847e0fa977eSAndriy Gapon ("duplicate handle remove")); 848de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 849e0fa977eSAndriy Gapon intr_event_schedule_thread(ie); 850e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8510f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 852e0f66ef8SJohn Baldwin intr_event_update(ie); 853111b043cSAndriy Gapon 854e0f66ef8SJohn Baldwin #ifdef notyet 855e0f66ef8SJohn Baldwin /* 856e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 857e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 858e0f66ef8SJohn Baldwin * interrupt. 859e0f66ef8SJohn Baldwin */ 860e0f66ef8SJohn Baldwin dead = 1; 861111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 862111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 863e0f66ef8SJohn Baldwin dead = 0; 864e0f66ef8SJohn Baldwin break; 865e0f66ef8SJohn Baldwin } 866e0f66ef8SJohn Baldwin } 867e0f66ef8SJohn Baldwin if (dead) { 868e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 869e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 870e0f66ef8SJohn Baldwin } 871e0f66ef8SJohn Baldwin #endif 872e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 873b4151f71SJohn Baldwin free(handler, M_ITHREAD); 874b4151f71SJohn Baldwin return (0); 875b4151f71SJohn Baldwin } 876b4151f71SJohn Baldwin 87782a5a275SAndriy Gapon int 87882a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie) 87982a5a275SAndriy Gapon { 88082a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 88182a5a275SAndriy Gapon struct intr_event *ie; 88282a5a275SAndriy Gapon 88382a5a275SAndriy Gapon if (handler == NULL) 88482a5a275SAndriy Gapon return (EINVAL); 88582a5a275SAndriy Gapon ie = handler->ih_event; 88682a5a275SAndriy Gapon KASSERT(ie != NULL, 88782a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 88882a5a275SAndriy Gapon handler->ih_name)); 88982a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 89082a5a275SAndriy Gapon handler->ih_flags |= IH_SUSP; 89182a5a275SAndriy Gapon intr_handler_barrier(handler); 89282a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 89382a5a275SAndriy Gapon return (0); 89482a5a275SAndriy Gapon } 89582a5a275SAndriy Gapon 89682a5a275SAndriy Gapon int 89782a5a275SAndriy Gapon intr_event_resume_handler(void *cookie) 89882a5a275SAndriy Gapon { 89982a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 90082a5a275SAndriy Gapon struct intr_event *ie; 90182a5a275SAndriy Gapon 90282a5a275SAndriy Gapon if (handler == NULL) 90382a5a275SAndriy Gapon return (EINVAL); 90482a5a275SAndriy Gapon ie = handler->ih_event; 90582a5a275SAndriy Gapon KASSERT(ie != NULL, 90682a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 90782a5a275SAndriy Gapon handler->ih_name)); 90882a5a275SAndriy Gapon 90982a5a275SAndriy Gapon /* 91082a5a275SAndriy Gapon * intr_handler_barrier() acts not only as a barrier, 91182a5a275SAndriy Gapon * it also allows to check for any pending interrupts. 91282a5a275SAndriy Gapon */ 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 9201ee1b687SJohn Baldwin static int 921e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 9223e5da754SJohn Baldwin { 923e0f66ef8SJohn Baldwin struct intr_entropy entropy; 924e0f66ef8SJohn Baldwin struct intr_thread *it; 925b40ce416SJulian Elischer struct thread *td; 92604774f23SJulian Elischer struct thread *ctd; 9273e5da754SJohn Baldwin 9283e5da754SJohn Baldwin /* 9293e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 9303e5da754SJohn Baldwin */ 931111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 932e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 9333e5da754SJohn Baldwin return (EINVAL); 9343e5da754SJohn Baldwin 93504774f23SJulian Elischer ctd = curthread; 936e0f66ef8SJohn Baldwin it = ie->ie_thread; 937e0f66ef8SJohn Baldwin td = it->it_thread; 938e0f66ef8SJohn Baldwin 9393e5da754SJohn Baldwin /* 9403e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 9413e5da754SJohn Baldwin * sources of entropy, then gather some. 9423e5da754SJohn Baldwin */ 94310cb2424SMark Murray if (ie->ie_flags & IE_ENTROPY) { 944e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 945e0f66ef8SJohn Baldwin entropy.td = ctd; 94619fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 9473e5da754SJohn Baldwin } 9483e5da754SJohn Baldwin 949ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 9503e5da754SJohn Baldwin 9513e5da754SJohn Baldwin /* 9523e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 953982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 954982d11f8SJeff Roberson * put it on the runqueue. 955283dfee9SKonstantin Belousov * 956283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 957283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 958283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 9593e5da754SJohn Baldwin */ 9603eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 961982d11f8SJeff Roberson thread_lock(td); 96271fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 963fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 9647ab24ea3SJulian Elischer td->td_name); 96571fad9fdSJulian Elischer TD_CLR_IWAIT(td); 966f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 9673e5da754SJohn Baldwin } else { 968e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 969fc2e87beSMatt Macy __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state); 9703e5da754SJohn Baldwin } 971982d11f8SJeff Roberson thread_unlock(td); 9723e5da754SJohn Baldwin 9733e5da754SJohn Baldwin return (0); 9743e5da754SJohn Baldwin } 9753e5da754SJohn Baldwin 976fe486a37SJohn Baldwin /* 977e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 978e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 979e84bcd84SRobert Watson * a PIC. 980e84bcd84SRobert Watson */ 981e84bcd84SRobert Watson static int 982066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 983e84bcd84SRobert Watson { 984e84bcd84SRobert Watson 985e84bcd84SRobert Watson return (0); 986e84bcd84SRobert Watson } 987e84bcd84SRobert Watson 988e84bcd84SRobert Watson /* 989fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 990fe486a37SJohn Baldwin * is not specified, then a new event is created. 991fe486a37SJohn Baldwin */ 9923e5da754SJohn Baldwin int 993e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 994b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 9958088699fSJohn Baldwin { 996e0f66ef8SJohn Baldwin struct intr_event *ie; 997b4151f71SJohn Baldwin int error; 9988088699fSJohn Baldwin 999bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 10003e5da754SJohn Baldwin return (EINVAL); 10013e5da754SJohn Baldwin 1002e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 10038088699fSJohn Baldwin 1004e0f66ef8SJohn Baldwin if (ie != NULL) { 1005e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 10063e5da754SJohn Baldwin return (EINVAL); 10073e5da754SJohn Baldwin } else { 10089b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1009e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10108088699fSJohn Baldwin if (error) 1011b4151f71SJohn Baldwin return (error); 1012e0f66ef8SJohn Baldwin if (eventp != NULL) 1013e0f66ef8SJohn Baldwin *eventp = ie; 10148088699fSJohn Baldwin } 10158d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1016d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 10178d809d50SJeff Roberson return (error); 10188088699fSJohn Baldwin } 10198088699fSJohn Baldwin 10201931cf94SJohn Baldwin /* 1021e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10221931cf94SJohn Baldwin */ 10231931cf94SJohn Baldwin void 1024b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10251931cf94SJohn Baldwin { 1026e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1027e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1028d95dca1dSJohn Baldwin struct intr_entropy entropy; 1029ba3f7276SMatt Macy int error __unused; 10308088699fSJohn Baldwin 1031e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1032e0f66ef8SJohn Baldwin ih->ih_need); 10331931cf94SJohn Baldwin 1034d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1035d95dca1dSJohn Baldwin entropy.td = curthread; 103619fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 1037d95dca1dSJohn Baldwin 10381931cf94SJohn Baldwin /* 10393e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 10403e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 10413e5da754SJohn Baldwin * it will execute it the next time it runs. 10421931cf94SJohn Baldwin */ 1043283dfee9SKonstantin Belousov ih->ih_need = 1; 10441ca2c018SBruce Evans 1045b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 104683c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 1047e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 10483e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 10498088699fSJohn Baldwin } 10508088699fSJohn Baldwin } 10518088699fSJohn Baldwin 1052fe486a37SJohn Baldwin /* 1053fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1054fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1055fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1056fe486a37SJohn Baldwin * an optimal interface. 1057fe486a37SJohn Baldwin */ 1058fe486a37SJohn Baldwin int 1059fe486a37SJohn Baldwin swi_remove(void *cookie) 1060fe486a37SJohn Baldwin { 1061fe486a37SJohn Baldwin 1062fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1063fe486a37SJohn Baldwin } 1064fe486a37SJohn Baldwin 1065111b043cSAndriy Gapon static void 106637e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1067e0f66ef8SJohn Baldwin { 1068111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 1069e0f66ef8SJohn Baldwin 1070111b043cSAndriy Gapon ihp = NULL; 1071111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1072e0f66ef8SJohn Baldwin /* 1073e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1074e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1075e0f66ef8SJohn Baldwin */ 1076e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1077e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1078111b043cSAndriy Gapon if (ihp == NULL) 1079111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1080111b043cSAndriy Gapon else 1081111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1082e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1083e0f66ef8SJohn Baldwin wakeup(ih); 1084e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1085e0f66ef8SJohn Baldwin continue; 1086e0f66ef8SJohn Baldwin } 1087e0f66ef8SJohn Baldwin 1088111b043cSAndriy Gapon /* 1089111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1090111b043cSAndriy Gapon * update the previous element. 1091111b043cSAndriy Gapon */ 1092111b043cSAndriy Gapon ihp = ih; 1093111b043cSAndriy Gapon 109482a5a275SAndriy Gapon if ((ih->ih_flags & IH_CHANGED) != 0) { 109582a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 109682a5a275SAndriy Gapon ih->ih_flags &= ~IH_CHANGED; 109782a5a275SAndriy Gapon wakeup(ih); 109882a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 109982a5a275SAndriy Gapon } 110082a5a275SAndriy Gapon 1101f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1102f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1103f2d619c8SPaolo Pisati continue; 1104f2d619c8SPaolo Pisati 110582a5a275SAndriy Gapon /* Skip suspended handlers */ 110682a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 110782a5a275SAndriy Gapon continue; 110882a5a275SAndriy Gapon 1109e0f66ef8SJohn Baldwin /* 1110e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1111e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1112e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 11131b79b949SKirk McKusick * 11141b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 11151b79b949SKirk McKusick * means that there is no request to execute handlers, 11161b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1117e0f66ef8SJohn Baldwin */ 11181b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 11191b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1120e0f66ef8SJohn Baldwin continue; 1121e0f66ef8SJohn Baldwin 1122e0f66ef8SJohn Baldwin /* Execute this handler. */ 1123e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1124bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1125bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1126e0f66ef8SJohn Baldwin 1127e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1128e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1129e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1130e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1131e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1132e0f66ef8SJohn Baldwin } 113337e9511fSJohn Baldwin } 113437e9511fSJohn Baldwin 113537e9511fSJohn Baldwin static void 113637e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 113737e9511fSJohn Baldwin { 113837e9511fSJohn Baldwin 113937e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 114037e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 114137e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 114237e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1143e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1144e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1145e0f66ef8SJohn Baldwin 1146e0f66ef8SJohn Baldwin /* 1147e0f66ef8SJohn Baldwin * Interrupt storm handling: 1148e0f66ef8SJohn Baldwin * 1149e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1150e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1151e0f66ef8SJohn Baldwin * 1152e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1153e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1154e0f66ef8SJohn Baldwin * then enter storming mode. 1155e0f66ef8SJohn Baldwin */ 1156e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1157e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 11580ae62c18SNate Lawson /* Report the message only once every second. */ 11590ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1160e0f66ef8SJohn Baldwin printf( 11610ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1162e0f66ef8SJohn Baldwin ie->ie_name); 1163e0f66ef8SJohn Baldwin } 1164e41bcf3cSJohn Baldwin pause("istorm", 1); 1165e0f66ef8SJohn Baldwin } else 1166e0f66ef8SJohn Baldwin ie->ie_count++; 1167e0f66ef8SJohn Baldwin 1168e0f66ef8SJohn Baldwin /* 1169e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1170e0f66ef8SJohn Baldwin * the interrupt source. 1171e0f66ef8SJohn Baldwin */ 11721ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 11731ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1174e0f66ef8SJohn Baldwin } 1175e0f66ef8SJohn Baldwin 11768088699fSJohn Baldwin /* 1177b4151f71SJohn Baldwin * This is the main code for interrupt threads. 11788088699fSJohn Baldwin */ 117937c84183SPoul-Henning Kamp static void 1180b4151f71SJohn Baldwin ithread_loop(void *arg) 11818088699fSJohn Baldwin { 1182e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1183e0f66ef8SJohn Baldwin struct intr_event *ie; 1184b40ce416SJulian Elischer struct thread *td; 1185b4151f71SJohn Baldwin struct proc *p; 1186e4cd31ddSJeff Roberson int wake; 11878088699fSJohn Baldwin 1188b40ce416SJulian Elischer td = curthread; 1189b40ce416SJulian Elischer p = td->td_proc; 1190e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1191e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 119291f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1193e0f66ef8SJohn Baldwin ie = ithd->it_event; 1194e0f66ef8SJohn Baldwin ie->ie_count = 0; 1195e4cd31ddSJeff Roberson wake = 0; 11968088699fSJohn Baldwin 11978088699fSJohn Baldwin /* 11988088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 11998088699fSJohn Baldwin * list of handlers, giving each one a go at it. 12008088699fSJohn Baldwin */ 12018088699fSJohn Baldwin for (;;) { 1202b4151f71SJohn Baldwin /* 1203b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1204b4151f71SJohn Baldwin */ 1205b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1206e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 12077ab24ea3SJulian Elischer p->p_pid, td->td_name); 1208b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1209ca9a0ddfSJulian Elischer kthread_exit(); 1210b4151f71SJohn Baldwin } 1211b4151f71SJohn Baldwin 1212e0f66ef8SJohn Baldwin /* 1213e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1214e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1215e0f66ef8SJohn Baldwin * should make another pass. 1216283dfee9SKonstantin Belousov * 1217283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1218283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1219283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1220e0f66ef8SJohn Baldwin */ 1221283dfee9SKonstantin Belousov while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) 1222e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 12237870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 12247870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 12258088699fSJohn Baldwin 12268088699fSJohn Baldwin /* 12278088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 12288088699fSJohn Baldwin * lock. This may take a while and it_need may get 12298088699fSJohn Baldwin * set again, so we have to check it again. 12308088699fSJohn Baldwin */ 1231982d11f8SJeff Roberson thread_lock(td); 123203bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 123303bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 12347870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1235e0f66ef8SJohn Baldwin ie->ie_count = 0; 12368df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 12378088699fSJohn Baldwin } 1238e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1239e4cd31ddSJeff Roberson wake = 1; 1240e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1241e4cd31ddSJeff Roberson } 1242982d11f8SJeff Roberson thread_unlock(td); 1243e4cd31ddSJeff Roberson if (wake) { 1244e4cd31ddSJeff Roberson wakeup(ithd); 1245e4cd31ddSJeff Roberson wake = 0; 1246e4cd31ddSJeff Roberson } 12478088699fSJohn Baldwin } 12481931cf94SJohn Baldwin } 12491ee1b687SJohn Baldwin 12501ee1b687SJohn Baldwin /* 12511ee1b687SJohn Baldwin * Main interrupt handling body. 12521ee1b687SJohn Baldwin * 12531ee1b687SJohn Baldwin * Input: 12541ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 12551ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 12561ee1b687SJohn Baldwin * handlers as their main argument. 12571ee1b687SJohn Baldwin * Return value: 12581ee1b687SJohn Baldwin * o 0: everything ok. 12591ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 12601ee1b687SJohn Baldwin */ 12611ee1b687SJohn Baldwin int 12621ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 12631ee1b687SJohn Baldwin { 12641ee1b687SJohn Baldwin struct intr_handler *ih; 12651f255bd3SAlexander Motin struct trapframe *oldframe; 12661ee1b687SJohn Baldwin struct thread *td; 1267e0fa977eSAndriy Gapon int phase; 126882a5a275SAndriy Gapon int ret; 126982a5a275SAndriy Gapon bool filter, thread; 12701ee1b687SJohn Baldwin 12711ee1b687SJohn Baldwin td = curthread; 12721ee1b687SJohn Baldwin 1273b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1274b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1275b7627840SKonstantin Belousov #endif 1276b7627840SKonstantin Belousov 12771ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1278111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 12791ee1b687SJohn Baldwin return (EINVAL); 12801ee1b687SJohn Baldwin 12811ee1b687SJohn Baldwin /* 12821ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 12831ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 12841ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 12851ee1b687SJohn Baldwin * a trapframe as its argument. 12861ee1b687SJohn Baldwin */ 12871ee1b687SJohn Baldwin td->td_intr_nesting_level++; 128882a5a275SAndriy Gapon filter = false; 128982a5a275SAndriy Gapon thread = false; 12901ee1b687SJohn Baldwin ret = 0; 12911ee1b687SJohn Baldwin critical_enter(); 12921f255bd3SAlexander Motin oldframe = td->td_intr_frame; 12931f255bd3SAlexander Motin td->td_intr_frame = frame; 1294111b043cSAndriy Gapon 1295e0fa977eSAndriy Gapon phase = ie->ie_phase; 1296e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1297e0fa977eSAndriy Gapon 1298e0fa977eSAndriy Gapon /* 1299e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1300e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1301e0fa977eSAndriy Gapon */ 1302e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1303e0fa977eSAndriy Gapon 1304111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 130582a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 130682a5a275SAndriy Gapon continue; 13071ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 130882a5a275SAndriy Gapon thread = true; 13091ee1b687SJohn Baldwin continue; 13101ee1b687SJohn Baldwin } 13111ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 13121ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 13131ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 13141ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 13151ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 13161ee1b687SJohn Baldwin else 13171ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 131889fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 131989fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 132089fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 132189fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 132289fc20ccSAndriy Gapon ih->ih_name)); 132382a5a275SAndriy Gapon filter = filter || ret == FILTER_HANDLED; 132489fc20ccSAndriy Gapon 13251ee1b687SJohn Baldwin /* 13261ee1b687SJohn Baldwin * Wrapper handler special handling: 13271ee1b687SJohn Baldwin * 13281ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 13291ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 13301ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 13311ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 13321ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 13331ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 13341ee1b687SJohn Baldwin * of just an ithread handler. 13351ee1b687SJohn Baldwin * 13361ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 133782a5a275SAndriy Gapon * their own. 13381ee1b687SJohn Baldwin */ 13391ee1b687SJohn Baldwin if (!thread) { 13401ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 134182a5a275SAndriy Gapon thread = true; 13421ee1b687SJohn Baldwin } 13431ee1b687SJohn Baldwin } 1344e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1345e0fa977eSAndriy Gapon 13461f255bd3SAlexander Motin td->td_intr_frame = oldframe; 13471ee1b687SJohn Baldwin 13481ee1b687SJohn Baldwin if (thread) { 13491ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 13501ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 13511ee1b687SJohn Baldwin } else { 13521ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 13531ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 13541ee1b687SJohn Baldwin } 13551ee1b687SJohn Baldwin 13561ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 13571ee1b687SJohn Baldwin if (thread) { 1358ba3f7276SMatt Macy int error __unused; 1359ba3f7276SMatt Macy 13601ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 13611ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 13621ee1b687SJohn Baldwin } 13631ee1b687SJohn Baldwin critical_exit(); 13641ee1b687SJohn Baldwin td->td_intr_nesting_level--; 136582a5a275SAndriy Gapon #ifdef notyet 136682a5a275SAndriy Gapon /* The interrupt is not aknowledged by any filter and has no ithread. */ 136782a5a275SAndriy Gapon if (!thread && !filter) 136882a5a275SAndriy Gapon return (EINVAL); 136982a5a275SAndriy Gapon #endif 13701ee1b687SJohn Baldwin return (0); 13711ee1b687SJohn Baldwin } 13721931cf94SJohn Baldwin 13738b201c42SJohn Baldwin #ifdef DDB 13748b201c42SJohn Baldwin /* 13758b201c42SJohn Baldwin * Dump details about an interrupt handler 13768b201c42SJohn Baldwin */ 13778b201c42SJohn Baldwin static void 1378e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 13798b201c42SJohn Baldwin { 13808b201c42SJohn Baldwin int comma; 13818b201c42SJohn Baldwin 13828b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 13838b201c42SJohn Baldwin switch (ih->ih_pri) { 13848b201c42SJohn Baldwin case PI_REALTIME: 13858b201c42SJohn Baldwin db_printf("CLK "); 13868b201c42SJohn Baldwin break; 13878b201c42SJohn Baldwin case PI_AV: 13888b201c42SJohn Baldwin db_printf("AV "); 13898b201c42SJohn Baldwin break; 1390d3305205SJohn Baldwin case PI_TTY: 13918b201c42SJohn Baldwin db_printf("TTY "); 13928b201c42SJohn Baldwin break; 13938b201c42SJohn Baldwin case PI_NET: 13948b201c42SJohn Baldwin db_printf("NET "); 13958b201c42SJohn Baldwin break; 13968b201c42SJohn Baldwin case PI_DISK: 13978b201c42SJohn Baldwin db_printf("DISK"); 13988b201c42SJohn Baldwin break; 13998b201c42SJohn Baldwin case PI_DULL: 14008b201c42SJohn Baldwin db_printf("DULL"); 14018b201c42SJohn Baldwin break; 14028b201c42SJohn Baldwin default: 14038b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 14048b201c42SJohn Baldwin db_printf("SWI "); 14058b201c42SJohn Baldwin else 14068b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 14078b201c42SJohn Baldwin break; 14088b201c42SJohn Baldwin } 14098b201c42SJohn Baldwin db_printf(" "); 1410b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1411b887a155SKonstantin Belousov db_printf("[F]"); 1412b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1413b887a155SKonstantin Belousov } 1414b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1415b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1416b887a155SKonstantin Belousov db_printf(","); 1417b887a155SKonstantin Belousov db_printf("[H]"); 14188b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1419b887a155SKonstantin Belousov } 14208b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 14218b201c42SJohn Baldwin if (ih->ih_need || 1422ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 14238b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 14248b201c42SJohn Baldwin db_printf(" {"); 14258b201c42SJohn Baldwin comma = 0; 14268b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 14278b201c42SJohn Baldwin if (comma) 14288b201c42SJohn Baldwin db_printf(", "); 14298b201c42SJohn Baldwin db_printf("EXCL"); 14308b201c42SJohn Baldwin comma = 1; 14318b201c42SJohn Baldwin } 14328b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 14338b201c42SJohn Baldwin if (comma) 14348b201c42SJohn Baldwin db_printf(", "); 14358b201c42SJohn Baldwin db_printf("ENTROPY"); 14368b201c42SJohn Baldwin comma = 1; 14378b201c42SJohn Baldwin } 14388b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 14398b201c42SJohn Baldwin if (comma) 14408b201c42SJohn Baldwin db_printf(", "); 14418b201c42SJohn Baldwin db_printf("DEAD"); 14428b201c42SJohn Baldwin comma = 1; 14438b201c42SJohn Baldwin } 14448b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 14458b201c42SJohn Baldwin if (comma) 14468b201c42SJohn Baldwin db_printf(", "); 14478b201c42SJohn Baldwin db_printf("MPSAFE"); 14488b201c42SJohn Baldwin comma = 1; 14498b201c42SJohn Baldwin } 14508b201c42SJohn Baldwin if (ih->ih_need) { 14518b201c42SJohn Baldwin if (comma) 14528b201c42SJohn Baldwin db_printf(", "); 14538b201c42SJohn Baldwin db_printf("NEED"); 14548b201c42SJohn Baldwin } 14558b201c42SJohn Baldwin db_printf("}"); 14568b201c42SJohn Baldwin } 14578b201c42SJohn Baldwin db_printf("\n"); 14588b201c42SJohn Baldwin } 14598b201c42SJohn Baldwin 14608b201c42SJohn Baldwin /* 1461e0f66ef8SJohn Baldwin * Dump details about a event. 14628b201c42SJohn Baldwin */ 14638b201c42SJohn Baldwin void 1464e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 14658b201c42SJohn Baldwin { 1466e0f66ef8SJohn Baldwin struct intr_handler *ih; 1467e0f66ef8SJohn Baldwin struct intr_thread *it; 14688b201c42SJohn Baldwin int comma; 14698b201c42SJohn Baldwin 1470e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1471e0f66ef8SJohn Baldwin it = ie->ie_thread; 1472e0f66ef8SJohn Baldwin if (it != NULL) 1473e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1474e0f66ef8SJohn Baldwin else 1475e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1476e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1477e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 14788b201c42SJohn Baldwin db_printf(" {"); 14798b201c42SJohn Baldwin comma = 0; 1480e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 14818b201c42SJohn Baldwin db_printf("SOFT"); 14828b201c42SJohn Baldwin comma = 1; 14838b201c42SJohn Baldwin } 1484e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 14858b201c42SJohn Baldwin if (comma) 14868b201c42SJohn Baldwin db_printf(", "); 14878b201c42SJohn Baldwin db_printf("ENTROPY"); 14888b201c42SJohn Baldwin comma = 1; 14898b201c42SJohn Baldwin } 1490e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 14918b201c42SJohn Baldwin if (comma) 14928b201c42SJohn Baldwin db_printf(", "); 1493e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 14948b201c42SJohn Baldwin comma = 1; 14958b201c42SJohn Baldwin } 1496e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 14978b201c42SJohn Baldwin if (comma) 14988b201c42SJohn Baldwin db_printf(", "); 14998b201c42SJohn Baldwin db_printf("NEED"); 15008b201c42SJohn Baldwin } 15018b201c42SJohn Baldwin db_printf("}"); 15028b201c42SJohn Baldwin } 15038b201c42SJohn Baldwin db_printf("\n"); 15048b201c42SJohn Baldwin 15058b201c42SJohn Baldwin if (handlers) 1506111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 15078b201c42SJohn Baldwin db_dump_intrhand(ih); 15088b201c42SJohn Baldwin } 1509e0f66ef8SJohn Baldwin 1510e0f66ef8SJohn Baldwin /* 1511e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1512e0f66ef8SJohn Baldwin */ 1513e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1514e0f66ef8SJohn Baldwin { 1515e0f66ef8SJohn Baldwin struct intr_event *ie; 151619e9205aSJohn Baldwin int all, verbose; 1517e0f66ef8SJohn Baldwin 1518dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1519dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1520e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1521111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1522e0f66ef8SJohn Baldwin continue; 1523e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 152419e9205aSJohn Baldwin if (db_pager_quit) 152519e9205aSJohn Baldwin break; 1526e0f66ef8SJohn Baldwin } 1527e0f66ef8SJohn Baldwin } 15288b201c42SJohn Baldwin #endif /* DDB */ 15298b201c42SJohn Baldwin 1530b4151f71SJohn Baldwin /* 15318088699fSJohn Baldwin * Start standard software interrupt threads 15321931cf94SJohn Baldwin */ 15331931cf94SJohn Baldwin static void 1534b4151f71SJohn Baldwin start_softintr(void *dummy) 15351931cf94SJohn Baldwin { 1536b4151f71SJohn Baldwin 15378d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 15388d809d50SJeff Roberson panic("died while creating vm swi ithread"); 15391931cf94SJohn Baldwin } 1540237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1541237fdd78SRobert Watson NULL); 15421931cf94SJohn Baldwin 1543d279178dSThomas Moestl /* 1544d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1545d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1546d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1547d279178dSThomas Moestl * independent. 1548d279178dSThomas Moestl * 1549d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1550d279178dSThomas Moestl * calculate things at run time. 1551d279178dSThomas Moestl */ 1552d279178dSThomas Moestl static int 1553d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1554d279178dSThomas Moestl { 1555521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1556d279178dSThomas Moestl } 1557d279178dSThomas Moestl 1558d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1559d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1560d279178dSThomas Moestl 1561d279178dSThomas Moestl static int 1562d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1563d279178dSThomas Moestl { 156485729c2cSJuli Mallett #ifdef SCTL_MASK32 156585729c2cSJuli Mallett uint32_t *intrcnt32; 156685729c2cSJuli Mallett unsigned i; 156785729c2cSJuli Mallett int error; 156885729c2cSJuli Mallett 156985729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 157085729c2cSJuli Mallett if (!req->oldptr) 157185729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 157285729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 157385729c2cSJuli Mallett if (intrcnt32 == NULL) 157485729c2cSJuli Mallett return (ENOMEM); 157585729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 157685729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 157785729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 157885729c2cSJuli Mallett free(intrcnt32, M_TEMP); 157985729c2cSJuli Mallett return (error); 158085729c2cSJuli Mallett } 158185729c2cSJuli Mallett #endif 1582521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1583d279178dSThomas Moestl } 1584d279178dSThomas Moestl 1585d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1586d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 15878b201c42SJohn Baldwin 15888b201c42SJohn Baldwin #ifdef DDB 15898b201c42SJohn Baldwin /* 15908b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 15918b201c42SJohn Baldwin */ 15928b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 15938b201c42SJohn Baldwin { 15948b201c42SJohn Baldwin u_long *i; 15958b201c42SJohn Baldwin char *cp; 1596521ea19dSAttilio Rao u_int j; 15978b201c42SJohn Baldwin 15988b201c42SJohn Baldwin cp = intrnames; 1599521ea19dSAttilio Rao j = 0; 1600521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1601521ea19dSAttilio Rao i++, j++) { 16028b201c42SJohn Baldwin if (*cp == '\0') 16038b201c42SJohn Baldwin break; 16048b201c42SJohn Baldwin if (*i != 0) 16058b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 16068b201c42SJohn Baldwin cp += strlen(cp) + 1; 16078b201c42SJohn Baldwin } 16088b201c42SJohn Baldwin } 16098b201c42SJohn Baldwin #endif 1610