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) { 200e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 201e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 202e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 203e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 204e0f66ef8SJohn Baldwin space = 0; 2050811d60aSJohn Baldwin } else 2060811d60aSJohn Baldwin missed++; 2070811d60aSJohn Baldwin if (ih->ih_flags & IH_ENTROPY) 208e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ENTROPY; 2090811d60aSJohn Baldwin } 210e0f66ef8SJohn Baldwin 211e0f66ef8SJohn Baldwin /* 212e0f66ef8SJohn Baldwin * If the handler names were too long, add +'s to indicate missing 213e0f66ef8SJohn Baldwin * names. If we run out of room and still have +'s to add, change 214e0f66ef8SJohn Baldwin * the last character from a + to a *. 215e0f66ef8SJohn Baldwin */ 216e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2170811d60aSJohn Baldwin while (missed-- > 0) { 218e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 219e0f66ef8SJohn Baldwin if (*last == '+') { 220e0f66ef8SJohn Baldwin *last = '*'; 221e0f66ef8SJohn Baldwin break; 222b4151f71SJohn Baldwin } else 223e0f66ef8SJohn Baldwin *last = '+'; 224e0f66ef8SJohn Baldwin } else if (space) { 225e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 226e0f66ef8SJohn Baldwin space = 0; 227e0f66ef8SJohn Baldwin } else 228e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 229b4151f71SJohn Baldwin } 230e0f66ef8SJohn Baldwin 231e0f66ef8SJohn Baldwin /* 232e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 233e0f66ef8SJohn Baldwin * name. 234e0f66ef8SJohn Baldwin */ 235e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 236e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 237e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 238b4151f71SJohn Baldwin } 239b4151f71SJohn Baldwin 240b4151f71SJohn Baldwin int 2419b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2421ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 243066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2441ee1b687SJohn Baldwin const char *fmt, ...) 245bafe5a31SPaolo Pisati { 246bafe5a31SPaolo Pisati struct intr_event *ie; 247bafe5a31SPaolo Pisati va_list ap; 248bafe5a31SPaolo Pisati 249bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 250bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 251bafe5a31SPaolo Pisati return (EINVAL); 252bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 253bafe5a31SPaolo Pisati ie->ie_source = source; 2541ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2551ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2561ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2576d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 258bafe5a31SPaolo Pisati ie->ie_flags = flags; 2599b33b154SJeff Roberson ie->ie_irq = irq; 260eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 261111b043cSAndriy Gapon CK_SLIST_INIT(&ie->ie_handlers); 262bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 263bafe5a31SPaolo Pisati 264bafe5a31SPaolo Pisati va_start(ap, fmt); 265bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 266bafe5a31SPaolo Pisati va_end(ap); 267bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 2689b33b154SJeff Roberson mtx_lock(&event_lock); 269bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 2709b33b154SJeff Roberson mtx_unlock(&event_lock); 271bafe5a31SPaolo Pisati if (event != NULL) 272bafe5a31SPaolo Pisati *event = ie; 273bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 274bafe5a31SPaolo Pisati return (0); 275bafe5a31SPaolo Pisati } 276b4151f71SJohn Baldwin 277eaf86d16SJohn Baldwin /* 278eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 279eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 28029dfb631SConrad Meyer * platforms this request will fail. Using a cpu id of NOCPU unbinds 281eaf86d16SJohn Baldwin * the interrupt event. 282eaf86d16SJohn Baldwin */ 28329dfb631SConrad Meyer static int 28429dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread) 285eaf86d16SJohn Baldwin { 2869b33b154SJeff Roberson lwpid_t id; 287eaf86d16SJohn Baldwin int error; 288eaf86d16SJohn Baldwin 289eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 290eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 291eaf86d16SJohn Baldwin return (EINVAL); 292eaf86d16SJohn Baldwin 293eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 294eaf86d16SJohn Baldwin return (EOPNOTSUPP); 295cebc7fb1SJohn Baldwin 296cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 297cebc7fb1SJohn Baldwin if (error) 298cebc7fb1SJohn Baldwin return (error); 299cebc7fb1SJohn Baldwin 3009b33b154SJeff Roberson /* 301cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 302cebc7fb1SJohn Baldwin * permissions, etc. 3039b33b154SJeff Roberson */ 30429dfb631SConrad Meyer if (bindithread) { 305eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3069b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3079b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 308eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 30981198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3109b33b154SJeff Roberson if (error) 3119b33b154SJeff Roberson return (error); 3129b33b154SJeff Roberson } else 313eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 31429dfb631SConrad Meyer } 31529dfb631SConrad Meyer if (bindirq) 316eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 317cebc7fb1SJohn Baldwin if (error) { 31829dfb631SConrad Meyer if (bindithread) { 319cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 320cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 32181198539SAlexander V. Chernikov cpu = ie->ie_cpu; 322cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 323cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 32481198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 325cebc7fb1SJohn Baldwin } else 326cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 32729dfb631SConrad Meyer } 328eaf86d16SJohn Baldwin return (error); 329cebc7fb1SJohn Baldwin } 330cebc7fb1SJohn Baldwin 33129dfb631SConrad Meyer if (bindirq) { 332eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 333eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3349b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 33529dfb631SConrad Meyer } 3369b33b154SJeff Roberson 3379b33b154SJeff Roberson return (error); 3389b33b154SJeff Roberson } 3399b33b154SJeff Roberson 34029dfb631SConrad Meyer /* 34129dfb631SConrad Meyer * Bind an interrupt event to the specified CPU. For supported platforms, any 34229dfb631SConrad Meyer * associated ithreads as well as the primary interrupt context will be bound 34329dfb631SConrad Meyer * to the specificed CPU. 34429dfb631SConrad Meyer */ 34529dfb631SConrad Meyer int 34629dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu) 34729dfb631SConrad Meyer { 34829dfb631SConrad Meyer 34929dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, true)); 35029dfb631SConrad Meyer } 35129dfb631SConrad Meyer 35229dfb631SConrad Meyer /* 35329dfb631SConrad Meyer * Bind an interrupt event to the specified CPU, but do not bind associated 35429dfb631SConrad Meyer * ithreads. 35529dfb631SConrad Meyer */ 35629dfb631SConrad Meyer int 35729dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu) 35829dfb631SConrad Meyer { 35929dfb631SConrad Meyer 36029dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, false)); 36129dfb631SConrad Meyer } 36229dfb631SConrad Meyer 36329dfb631SConrad Meyer /* 36429dfb631SConrad Meyer * Bind an interrupt event's ithread to the specified CPU. 36529dfb631SConrad Meyer */ 36629dfb631SConrad Meyer int 36729dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu) 36829dfb631SConrad Meyer { 36929dfb631SConrad Meyer 37029dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, false, true)); 37129dfb631SConrad Meyer } 37229dfb631SConrad Meyer 3739b33b154SJeff Roberson static struct intr_event * 3749b33b154SJeff Roberson intr_lookup(int irq) 3759b33b154SJeff Roberson { 3769b33b154SJeff Roberson struct intr_event *ie; 3779b33b154SJeff Roberson 3789b33b154SJeff Roberson mtx_lock(&event_lock); 3799b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 3809b33b154SJeff Roberson if (ie->ie_irq == irq && 3819b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 382111b043cSAndriy Gapon CK_SLIST_FIRST(&ie->ie_handlers) != NULL) 3839b33b154SJeff Roberson break; 3849b33b154SJeff Roberson mtx_unlock(&event_lock); 3859b33b154SJeff Roberson return (ie); 3869b33b154SJeff Roberson } 3879b33b154SJeff Roberson 3889b33b154SJeff Roberson int 38929dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m) 3909b33b154SJeff Roberson { 3919b33b154SJeff Roberson struct intr_event *ie; 3929b33b154SJeff Roberson cpuset_t *mask; 3933fe93b94SAdrian Chadd int cpu, n; 3949b33b154SJeff Roberson 3959b33b154SJeff Roberson mask = m; 3969b33b154SJeff Roberson cpu = NOCPU; 3979b33b154SJeff Roberson /* 3989b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 3999b33b154SJeff Roberson * only one cpu is in the set. 4009b33b154SJeff Roberson */ 4019b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 4029b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 4039b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 4049b33b154SJeff Roberson continue; 4059b33b154SJeff Roberson if (cpu != NOCPU) 4069b33b154SJeff Roberson return (EINVAL); 4073fe93b94SAdrian Chadd cpu = n; 4089b33b154SJeff Roberson } 4099b33b154SJeff Roberson } 4109b33b154SJeff Roberson ie = intr_lookup(irq); 4119b33b154SJeff Roberson if (ie == NULL) 4129b33b154SJeff Roberson return (ESRCH); 41329dfb631SConrad Meyer switch (mode) { 41429dfb631SConrad Meyer case CPU_WHICH_IRQ: 4159bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 41629dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 41729dfb631SConrad Meyer return (intr_event_bind_irqonly(ie, cpu)); 41829dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 41929dfb631SConrad Meyer return (intr_event_bind_ithread(ie, cpu)); 42029dfb631SConrad Meyer default: 42129dfb631SConrad Meyer return (EINVAL); 42229dfb631SConrad Meyer } 4239b33b154SJeff Roberson } 4249b33b154SJeff Roberson 4259b33b154SJeff Roberson int 42629dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m) 4279b33b154SJeff Roberson { 4289b33b154SJeff Roberson struct intr_event *ie; 42929dfb631SConrad Meyer struct thread *td; 43029dfb631SConrad Meyer struct proc *p; 4319b33b154SJeff Roberson cpuset_t *mask; 43229dfb631SConrad Meyer lwpid_t id; 43329dfb631SConrad Meyer int error; 4349b33b154SJeff Roberson 4359b33b154SJeff Roberson mask = m; 4369b33b154SJeff Roberson ie = intr_lookup(irq); 4379b33b154SJeff Roberson if (ie == NULL) 4389b33b154SJeff Roberson return (ESRCH); 43929dfb631SConrad Meyer 44029dfb631SConrad Meyer error = 0; 4419b33b154SJeff Roberson CPU_ZERO(mask); 44229dfb631SConrad Meyer switch (mode) { 44329dfb631SConrad Meyer case CPU_WHICH_IRQ: 44429dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 4459b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 4469b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 4479b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 4489b33b154SJeff Roberson else 4499b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 450eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 45129dfb631SConrad Meyer break; 45229dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 45329dfb631SConrad Meyer mtx_lock(&ie->ie_lock); 45429dfb631SConrad Meyer if (ie->ie_thread == NULL) { 45529dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 45629dfb631SConrad Meyer CPU_COPY(cpuset_root, mask); 45729dfb631SConrad Meyer } else { 45829dfb631SConrad Meyer id = ie->ie_thread->it_thread->td_tid; 45929dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 46029dfb631SConrad Meyer error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL); 46129dfb631SConrad Meyer if (error != 0) 46229dfb631SConrad Meyer return (error); 46329dfb631SConrad Meyer CPU_COPY(&td->td_cpuset->cs_mask, mask); 46429dfb631SConrad Meyer PROC_UNLOCK(p); 46529dfb631SConrad Meyer } 46629dfb631SConrad Meyer default: 46729dfb631SConrad Meyer return (EINVAL); 46829dfb631SConrad Meyer } 469eaf86d16SJohn Baldwin return (0); 470eaf86d16SJohn Baldwin } 471eaf86d16SJohn Baldwin 472b4151f71SJohn Baldwin int 473e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 474b4151f71SJohn Baldwin { 475b4151f71SJohn Baldwin 4769b33b154SJeff Roberson mtx_lock(&event_lock); 477e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 478111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 479e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4809b33b154SJeff Roberson mtx_unlock(&event_lock); 481e0f66ef8SJohn Baldwin return (EBUSY); 4824d29cb2dSJohn Baldwin } 483e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 4849477358dSJohn Baldwin #ifndef notyet 4859477358dSJohn Baldwin if (ie->ie_thread != NULL) { 4869477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 4879477358dSJohn Baldwin ie->ie_thread = NULL; 4889477358dSJohn Baldwin } 4899477358dSJohn Baldwin #endif 490e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 4919b33b154SJeff Roberson mtx_unlock(&event_lock); 492e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 493e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 494e0f66ef8SJohn Baldwin return (0); 495e0f66ef8SJohn Baldwin } 496e0f66ef8SJohn Baldwin 497e0f66ef8SJohn Baldwin static struct intr_thread * 498e0f66ef8SJohn Baldwin ithread_create(const char *name) 499e0f66ef8SJohn Baldwin { 500e0f66ef8SJohn Baldwin struct intr_thread *ithd; 501e0f66ef8SJohn Baldwin struct thread *td; 502e0f66ef8SJohn Baldwin int error; 503e0f66ef8SJohn Baldwin 504e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 505e0f66ef8SJohn Baldwin 5067ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 5077ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 5089ef95d01SJulian Elischer 0, "intr", "%s", name); 509e0f66ef8SJohn Baldwin if (error) 5103745c395SJulian Elischer panic("kproc_create() failed with %d", error); 511982d11f8SJeff Roberson thread_lock(td); 512ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 513e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 514982d11f8SJeff Roberson thread_unlock(td); 515e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 516e0f66ef8SJohn Baldwin ithd->it_thread = td; 517e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 518e0f66ef8SJohn Baldwin return (ithd); 519e0f66ef8SJohn Baldwin } 520e0f66ef8SJohn Baldwin 521e0f66ef8SJohn Baldwin static void 522e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 523e0f66ef8SJohn Baldwin { 524e0f66ef8SJohn Baldwin struct thread *td; 525e0f66ef8SJohn Baldwin 526bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 527e0f66ef8SJohn Baldwin td = ithread->it_thread; 528982d11f8SJeff Roberson thread_lock(td); 529e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 53071fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 53171fad9fdSJulian Elischer TD_CLR_IWAIT(td); 532f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 533b4151f71SJohn Baldwin } 534982d11f8SJeff Roberson thread_unlock(td); 535b4151f71SJohn Baldwin } 536b4151f71SJohn Baldwin 537b4151f71SJohn Baldwin int 538e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 539ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 540ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 541b4151f71SJohn Baldwin { 542e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 543111b043cSAndriy Gapon struct intr_handler **prevptr; 544e0f66ef8SJohn Baldwin struct intr_thread *it; 545b4151f71SJohn Baldwin 546ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 547b4151f71SJohn Baldwin return (EINVAL); 548b4151f71SJohn Baldwin 549e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 550e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 551ef544f63SPaolo Pisati ih->ih_filter = filter; 552b4151f71SJohn Baldwin ih->ih_handler = handler; 553b4151f71SJohn Baldwin ih->ih_argument = arg; 55437b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 555e0f66ef8SJohn Baldwin ih->ih_event = ie; 556b4151f71SJohn Baldwin ih->ih_pri = pri; 557ef544f63SPaolo Pisati if (flags & INTR_EXCL) 558b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 559b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 560b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 561b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 562b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 563b4151f71SJohn Baldwin 564e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 565e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 566111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 567e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 568111b043cSAndriy Gapon (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 569e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 570b4151f71SJohn Baldwin free(ih, M_ITHREAD); 571b4151f71SJohn Baldwin return (EINVAL); 572b4151f71SJohn Baldwin } 573e0f66ef8SJohn Baldwin } 574e0f66ef8SJohn Baldwin 575e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 576ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 577e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 5780f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 579e0f66ef8SJohn Baldwin else { 580e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 581e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 582e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 583e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 584e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 585e0f66ef8SJohn Baldwin ie->ie_thread = it; 586e0f66ef8SJohn Baldwin it->it_event = ie; 587e0f66ef8SJohn Baldwin ithread_update(it); 588e0f66ef8SJohn Baldwin wakeup(ie); 589e0f66ef8SJohn Baldwin } 590e0f66ef8SJohn Baldwin } 591c9516c94SAlexander Kabaev 592c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 593111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) { 594c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 595c9516c94SAlexander Kabaev break; 596c9516c94SAlexander Kabaev } 597111b043cSAndriy Gapon CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next); 598111b043cSAndriy Gapon 599c9516c94SAlexander Kabaev intr_event_update(ie); 600c9516c94SAlexander Kabaev 601e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 602e0f66ef8SJohn Baldwin ie->ie_name); 603e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 604e0f66ef8SJohn Baldwin 605e0f66ef8SJohn Baldwin if (cookiep != NULL) 606e0f66ef8SJohn Baldwin *cookiep = ih; 607e0f66ef8SJohn Baldwin return (0); 608e0f66ef8SJohn Baldwin } 609b4151f71SJohn Baldwin 610c3045318SJohn Baldwin /* 61137b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 61237b8ef16SJohn Baldwin * interrupt handler. 61337b8ef16SJohn Baldwin */ 61437b8ef16SJohn Baldwin int 61537b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 61637b8ef16SJohn Baldwin const char *descr) 61737b8ef16SJohn Baldwin { 61837b8ef16SJohn Baldwin struct intr_handler *ih; 61937b8ef16SJohn Baldwin size_t space; 62037b8ef16SJohn Baldwin char *start; 62137b8ef16SJohn Baldwin 62237b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 62337b8ef16SJohn Baldwin #ifdef INVARIANTS 624111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 62537b8ef16SJohn Baldwin if (ih == cookie) 62637b8ef16SJohn Baldwin break; 62737b8ef16SJohn Baldwin } 62837b8ef16SJohn Baldwin if (ih == NULL) { 62937b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 630d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 63137b8ef16SJohn Baldwin } 63237b8ef16SJohn Baldwin #endif 63337b8ef16SJohn Baldwin ih = cookie; 63437b8ef16SJohn Baldwin 63537b8ef16SJohn Baldwin /* 63637b8ef16SJohn Baldwin * Look for an existing description by checking for an 63737b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 63837b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 63937b8ef16SJohn Baldwin * description at that point. If one is not found, find the 64037b8ef16SJohn Baldwin * end of the name to use as the insertion point. 64137b8ef16SJohn Baldwin */ 642dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 64337b8ef16SJohn Baldwin if (start == NULL) 644dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 64537b8ef16SJohn Baldwin 64637b8ef16SJohn Baldwin /* 64737b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 64837b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 64937b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 65037b8ef16SJohn Baldwin */ 65137b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 65237b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 65337b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 65437b8ef16SJohn Baldwin return (ENOSPC); 65537b8ef16SJohn Baldwin } 65637b8ef16SJohn Baldwin 65737b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 65837b8ef16SJohn Baldwin *start = ':'; 65937b8ef16SJohn Baldwin strcpy(start + 1, descr); 66037b8ef16SJohn Baldwin intr_event_update(ie); 66137b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 66237b8ef16SJohn Baldwin return (0); 66337b8ef16SJohn Baldwin } 66437b8ef16SJohn Baldwin 66537b8ef16SJohn Baldwin /* 666c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 667c3045318SJohn Baldwin * associated with. 668c3045318SJohn Baldwin */ 669c3045318SJohn Baldwin void * 670c3045318SJohn Baldwin intr_handler_source(void *cookie) 671c3045318SJohn Baldwin { 672c3045318SJohn Baldwin struct intr_handler *ih; 673c3045318SJohn Baldwin struct intr_event *ie; 674c3045318SJohn Baldwin 675c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 676c3045318SJohn Baldwin if (ih == NULL) 677c3045318SJohn Baldwin return (NULL); 678c3045318SJohn Baldwin ie = ih->ih_event; 679c3045318SJohn Baldwin KASSERT(ie != NULL, 680c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 681c3045318SJohn Baldwin ih->ih_name)); 682c3045318SJohn Baldwin return (ie->ie_source); 683c3045318SJohn Baldwin } 684c3045318SJohn Baldwin 685e4cd31ddSJeff Roberson /* 686e0fa977eSAndriy Gapon * If intr_event_handle() is running in the ISR context at the time of the call, 687e0fa977eSAndriy Gapon * then wait for it to complete. 688e0fa977eSAndriy Gapon */ 689e0fa977eSAndriy Gapon static void 690e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie) 691e0fa977eSAndriy Gapon { 692e0fa977eSAndriy Gapon int phase; 693e0fa977eSAndriy Gapon 694e0fa977eSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 695e0fa977eSAndriy Gapon phase = ie->ie_phase; 696e0fa977eSAndriy Gapon 697e0fa977eSAndriy Gapon /* 698e0fa977eSAndriy Gapon * Switch phase to direct future interrupts to the other active counter. 699e0fa977eSAndriy Gapon * Make sure that any preceding stores are visible before the switch. 700e0fa977eSAndriy Gapon */ 701e0fa977eSAndriy Gapon KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity")); 702e0fa977eSAndriy Gapon atomic_store_rel_int(&ie->ie_phase, !phase); 703e0fa977eSAndriy Gapon 704e0fa977eSAndriy Gapon /* 705e0fa977eSAndriy Gapon * This code cooperates with wait-free iteration of ie_handlers 706e0fa977eSAndriy Gapon * in intr_event_handle. 707e0fa977eSAndriy Gapon * Make sure that the removal and the phase update are not reordered 708e0fa977eSAndriy Gapon * with the active count check. 709e0fa977eSAndriy Gapon * Note that no combination of acquire and release fences can provide 710e0fa977eSAndriy Gapon * that guarantee as Store->Load sequences can always be reordered. 711e0fa977eSAndriy Gapon */ 712e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 713e0fa977eSAndriy Gapon 714e0fa977eSAndriy Gapon /* 715e0fa977eSAndriy Gapon * Now wait on the inactive phase. 716e0fa977eSAndriy Gapon * The acquire fence is needed so that that all post-barrier accesses 717e0fa977eSAndriy Gapon * are after the check. 718e0fa977eSAndriy Gapon */ 719e0fa977eSAndriy Gapon while (ie->ie_active[phase] > 0) 720e0fa977eSAndriy Gapon cpu_spinwait(); 721e0fa977eSAndriy Gapon atomic_thread_fence_acq(); 722e0fa977eSAndriy Gapon } 723e0fa977eSAndriy Gapon 724*82a5a275SAndriy Gapon static void 725*82a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler) 726*82a5a275SAndriy Gapon { 727*82a5a275SAndriy Gapon struct intr_event *ie; 728*82a5a275SAndriy Gapon 729*82a5a275SAndriy Gapon ie = handler->ih_event; 730*82a5a275SAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 731*82a5a275SAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 732*82a5a275SAndriy Gapon ("update for a removed handler")); 733*82a5a275SAndriy Gapon 734*82a5a275SAndriy Gapon if (ie->ie_thread == NULL) { 735*82a5a275SAndriy Gapon intr_event_barrier(ie); 736*82a5a275SAndriy Gapon return; 737*82a5a275SAndriy Gapon } 738*82a5a275SAndriy Gapon if ((handler->ih_flags & IH_CHANGED) == 0) { 739*82a5a275SAndriy Gapon handler->ih_flags |= IH_CHANGED; 740*82a5a275SAndriy Gapon intr_event_schedule_thread(ie); 741*82a5a275SAndriy Gapon } 742*82a5a275SAndriy Gapon while ((handler->ih_flags & IH_CHANGED) != 0) 743*82a5a275SAndriy Gapon msleep(handler, &ie->ie_lock, 0, "ih_barr", 0); 744*82a5a275SAndriy Gapon } 745*82a5a275SAndriy Gapon 746e0fa977eSAndriy Gapon /* 747e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 748e4cd31ddSJeff Roberson * 749e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 750e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 751e4cd31ddSJeff Roberson * only. Do not use in BSD code. 752e4cd31ddSJeff Roberson */ 753e4cd31ddSJeff Roberson void 754e4cd31ddSJeff Roberson _intr_drain(int irq) 755e4cd31ddSJeff Roberson { 756e4cd31ddSJeff Roberson struct intr_event *ie; 757e4cd31ddSJeff Roberson struct intr_thread *ithd; 758e4cd31ddSJeff Roberson struct thread *td; 759e4cd31ddSJeff Roberson 760e4cd31ddSJeff Roberson ie = intr_lookup(irq); 761e4cd31ddSJeff Roberson if (ie == NULL) 762e4cd31ddSJeff Roberson return; 763e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 764e4cd31ddSJeff Roberson return; 765e4cd31ddSJeff Roberson ithd = ie->ie_thread; 766e4cd31ddSJeff Roberson td = ithd->it_thread; 7675bd186a6SJeff Roberson /* 7685bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 7695bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 7705bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 7715bd186a6SJeff Roberson */ 772e4cd31ddSJeff Roberson thread_lock(td); 773e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 774e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 7755bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 7765bd186a6SJeff Roberson thread_unlock(td); 7775bd186a6SJeff Roberson pause("idrain", 1); 7785bd186a6SJeff Roberson thread_lock(td); 779e4cd31ddSJeff Roberson } 7805bd186a6SJeff Roberson } 7815bd186a6SJeff Roberson thread_unlock(td); 782e4cd31ddSJeff Roberson return; 783e4cd31ddSJeff Roberson } 784e4cd31ddSJeff Roberson 785b4151f71SJohn Baldwin int 786e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 787b4151f71SJohn Baldwin { 788e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 789e0f66ef8SJohn Baldwin struct intr_event *ie; 790e0f66ef8SJohn Baldwin struct intr_handler *ih; 791111b043cSAndriy Gapon struct intr_handler **prevptr; 792e0f66ef8SJohn Baldwin #ifdef notyet 793e0f66ef8SJohn Baldwin int dead; 794b4151f71SJohn Baldwin #endif 795b4151f71SJohn Baldwin 7963e5da754SJohn Baldwin if (handler == NULL) 797b4151f71SJohn Baldwin return (EINVAL); 798e0f66ef8SJohn Baldwin ie = handler->ih_event; 799e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 800e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 8013e5da754SJohn Baldwin handler->ih_name)); 802111b043cSAndriy Gapon 803e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 80491f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 805e0f66ef8SJohn Baldwin ie->ie_name); 806111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 8073e5da754SJohn Baldwin if (ih == handler) 808111b043cSAndriy Gapon break; 809111b043cSAndriy Gapon } 810111b043cSAndriy Gapon if (ih == NULL) { 811111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 812111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 813111b043cSAndriy Gapon } 814111b043cSAndriy Gapon 815de271f01SJohn Baldwin /* 816e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 817e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 818e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 819e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 820e0f66ef8SJohn Baldwin */ 821e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 822111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 823e0fa977eSAndriy Gapon intr_event_barrier(ie); 824e0fa977eSAndriy Gapon intr_event_update(ie); 825e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 826e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 827e0f66ef8SJohn Baldwin return (0); 828e0f66ef8SJohn Baldwin } 829e0f66ef8SJohn Baldwin 830e0f66ef8SJohn Baldwin /* 831e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 832e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 833e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 834e0fa977eSAndriy Gapon * intr_event_handle(). 835de271f01SJohn Baldwin */ 836e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 837e0fa977eSAndriy Gapon ("duplicate handle remove")); 838de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 839e0fa977eSAndriy Gapon intr_event_schedule_thread(ie); 840e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8410f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 842e0f66ef8SJohn Baldwin intr_event_update(ie); 843111b043cSAndriy Gapon 844e0f66ef8SJohn Baldwin #ifdef notyet 845e0f66ef8SJohn Baldwin /* 846e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 847e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 848e0f66ef8SJohn Baldwin * interrupt. 849e0f66ef8SJohn Baldwin */ 850e0f66ef8SJohn Baldwin dead = 1; 851111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 852111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 853e0f66ef8SJohn Baldwin dead = 0; 854e0f66ef8SJohn Baldwin break; 855e0f66ef8SJohn Baldwin } 856e0f66ef8SJohn Baldwin } 857e0f66ef8SJohn Baldwin if (dead) { 858e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 859e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 860e0f66ef8SJohn Baldwin } 861e0f66ef8SJohn Baldwin #endif 862e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 863b4151f71SJohn Baldwin free(handler, M_ITHREAD); 864b4151f71SJohn Baldwin return (0); 865b4151f71SJohn Baldwin } 866b4151f71SJohn Baldwin 867*82a5a275SAndriy Gapon int 868*82a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie) 869*82a5a275SAndriy Gapon { 870*82a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 871*82a5a275SAndriy Gapon struct intr_event *ie; 872*82a5a275SAndriy Gapon 873*82a5a275SAndriy Gapon if (handler == NULL) 874*82a5a275SAndriy Gapon return (EINVAL); 875*82a5a275SAndriy Gapon ie = handler->ih_event; 876*82a5a275SAndriy Gapon KASSERT(ie != NULL, 877*82a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 878*82a5a275SAndriy Gapon handler->ih_name)); 879*82a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 880*82a5a275SAndriy Gapon handler->ih_flags |= IH_SUSP; 881*82a5a275SAndriy Gapon intr_handler_barrier(handler); 882*82a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 883*82a5a275SAndriy Gapon return (0); 884*82a5a275SAndriy Gapon } 885*82a5a275SAndriy Gapon 886*82a5a275SAndriy Gapon int 887*82a5a275SAndriy Gapon intr_event_resume_handler(void *cookie) 888*82a5a275SAndriy Gapon { 889*82a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 890*82a5a275SAndriy Gapon struct intr_event *ie; 891*82a5a275SAndriy Gapon 892*82a5a275SAndriy Gapon if (handler == NULL) 893*82a5a275SAndriy Gapon return (EINVAL); 894*82a5a275SAndriy Gapon ie = handler->ih_event; 895*82a5a275SAndriy Gapon KASSERT(ie != NULL, 896*82a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 897*82a5a275SAndriy Gapon handler->ih_name)); 898*82a5a275SAndriy Gapon 899*82a5a275SAndriy Gapon /* 900*82a5a275SAndriy Gapon * intr_handler_barrier() acts not only as a barrier, 901*82a5a275SAndriy Gapon * it also allows to check for any pending interrupts. 902*82a5a275SAndriy Gapon */ 903*82a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 904*82a5a275SAndriy Gapon handler->ih_flags &= ~IH_SUSP; 905*82a5a275SAndriy Gapon intr_handler_barrier(handler); 906*82a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 907*82a5a275SAndriy Gapon return (0); 908*82a5a275SAndriy Gapon } 909*82a5a275SAndriy Gapon 9101ee1b687SJohn Baldwin static int 911e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 9123e5da754SJohn Baldwin { 913e0f66ef8SJohn Baldwin struct intr_entropy entropy; 914e0f66ef8SJohn Baldwin struct intr_thread *it; 915b40ce416SJulian Elischer struct thread *td; 91604774f23SJulian Elischer struct thread *ctd; 9173e5da754SJohn Baldwin 9183e5da754SJohn Baldwin /* 9193e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 9203e5da754SJohn Baldwin */ 921111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 922e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 9233e5da754SJohn Baldwin return (EINVAL); 9243e5da754SJohn Baldwin 92504774f23SJulian Elischer ctd = curthread; 926e0f66ef8SJohn Baldwin it = ie->ie_thread; 927e0f66ef8SJohn Baldwin td = it->it_thread; 928e0f66ef8SJohn Baldwin 9293e5da754SJohn Baldwin /* 9303e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 9313e5da754SJohn Baldwin * sources of entropy, then gather some. 9323e5da754SJohn Baldwin */ 93310cb2424SMark Murray if (ie->ie_flags & IE_ENTROPY) { 934e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 935e0f66ef8SJohn Baldwin entropy.td = ctd; 93619fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 9373e5da754SJohn Baldwin } 9383e5da754SJohn Baldwin 939ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 9403e5da754SJohn Baldwin 9413e5da754SJohn Baldwin /* 9423e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 943982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 944982d11f8SJeff Roberson * put it on the runqueue. 945283dfee9SKonstantin Belousov * 946283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 947283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 948283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 9493e5da754SJohn Baldwin */ 9503eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 951982d11f8SJeff Roberson thread_lock(td); 95271fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 953fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 9547ab24ea3SJulian Elischer td->td_name); 95571fad9fdSJulian Elischer TD_CLR_IWAIT(td); 956f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 9573e5da754SJohn Baldwin } else { 958e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 959fc2e87beSMatt Macy __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state); 9603e5da754SJohn Baldwin } 961982d11f8SJeff Roberson thread_unlock(td); 9623e5da754SJohn Baldwin 9633e5da754SJohn Baldwin return (0); 9643e5da754SJohn Baldwin } 9653e5da754SJohn Baldwin 966fe486a37SJohn Baldwin /* 967e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 968e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 969e84bcd84SRobert Watson * a PIC. 970e84bcd84SRobert Watson */ 971e84bcd84SRobert Watson static int 972066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 973e84bcd84SRobert Watson { 974e84bcd84SRobert Watson 975e84bcd84SRobert Watson return (0); 976e84bcd84SRobert Watson } 977e84bcd84SRobert Watson 978e84bcd84SRobert Watson /* 979fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 980fe486a37SJohn Baldwin * is not specified, then a new event is created. 981fe486a37SJohn Baldwin */ 9823e5da754SJohn Baldwin int 983e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 984b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 9858088699fSJohn Baldwin { 986e0f66ef8SJohn Baldwin struct intr_event *ie; 987b4151f71SJohn Baldwin int error; 9888088699fSJohn Baldwin 989bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 9903e5da754SJohn Baldwin return (EINVAL); 9913e5da754SJohn Baldwin 992e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 9938088699fSJohn Baldwin 994e0f66ef8SJohn Baldwin if (ie != NULL) { 995e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 9963e5da754SJohn Baldwin return (EINVAL); 9973e5da754SJohn Baldwin } else { 9989b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 999e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10008088699fSJohn Baldwin if (error) 1001b4151f71SJohn Baldwin return (error); 1002e0f66ef8SJohn Baldwin if (eventp != NULL) 1003e0f66ef8SJohn Baldwin *eventp = ie; 10048088699fSJohn Baldwin } 10058d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1006d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 10078d809d50SJeff Roberson return (error); 10088088699fSJohn Baldwin } 10098088699fSJohn Baldwin 10101931cf94SJohn Baldwin /* 1011e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10121931cf94SJohn Baldwin */ 10131931cf94SJohn Baldwin void 1014b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10151931cf94SJohn Baldwin { 1016e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1017e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1018d95dca1dSJohn Baldwin struct intr_entropy entropy; 1019ba3f7276SMatt Macy int error __unused; 10208088699fSJohn Baldwin 1021e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1022e0f66ef8SJohn Baldwin ih->ih_need); 10231931cf94SJohn Baldwin 1024d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1025d95dca1dSJohn Baldwin entropy.td = curthread; 102619fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 1027d95dca1dSJohn Baldwin 10281931cf94SJohn Baldwin /* 10293e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 10303e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 10313e5da754SJohn Baldwin * it will execute it the next time it runs. 10321931cf94SJohn Baldwin */ 1033283dfee9SKonstantin Belousov ih->ih_need = 1; 10341ca2c018SBruce Evans 1035b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 103683c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 1037e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 10383e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 10398088699fSJohn Baldwin } 10408088699fSJohn Baldwin } 10418088699fSJohn Baldwin 1042fe486a37SJohn Baldwin /* 1043fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1044fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1045fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1046fe486a37SJohn Baldwin * an optimal interface. 1047fe486a37SJohn Baldwin */ 1048fe486a37SJohn Baldwin int 1049fe486a37SJohn Baldwin swi_remove(void *cookie) 1050fe486a37SJohn Baldwin { 1051fe486a37SJohn Baldwin 1052fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1053fe486a37SJohn Baldwin } 1054fe486a37SJohn Baldwin 1055111b043cSAndriy Gapon static void 105637e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1057e0f66ef8SJohn Baldwin { 1058111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 1059e0f66ef8SJohn Baldwin 1060111b043cSAndriy Gapon ihp = NULL; 1061111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1062e0f66ef8SJohn Baldwin /* 1063e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1064e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1065e0f66ef8SJohn Baldwin */ 1066e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1067e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1068111b043cSAndriy Gapon if (ihp == NULL) 1069111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1070111b043cSAndriy Gapon else 1071111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1072e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1073e0f66ef8SJohn Baldwin wakeup(ih); 1074e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1075e0f66ef8SJohn Baldwin continue; 1076e0f66ef8SJohn Baldwin } 1077e0f66ef8SJohn Baldwin 1078111b043cSAndriy Gapon /* 1079111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1080111b043cSAndriy Gapon * update the previous element. 1081111b043cSAndriy Gapon */ 1082111b043cSAndriy Gapon ihp = ih; 1083111b043cSAndriy Gapon 1084*82a5a275SAndriy Gapon if ((ih->ih_flags & IH_CHANGED) != 0) { 1085*82a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 1086*82a5a275SAndriy Gapon ih->ih_flags &= ~IH_CHANGED; 1087*82a5a275SAndriy Gapon wakeup(ih); 1088*82a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 1089*82a5a275SAndriy Gapon } 1090*82a5a275SAndriy Gapon 1091f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1092f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1093f2d619c8SPaolo Pisati continue; 1094f2d619c8SPaolo Pisati 1095*82a5a275SAndriy Gapon /* Skip suspended handlers */ 1096*82a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 1097*82a5a275SAndriy Gapon continue; 1098*82a5a275SAndriy Gapon 1099e0f66ef8SJohn Baldwin /* 1100e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1101e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1102e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 11031b79b949SKirk McKusick * 11041b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 11051b79b949SKirk McKusick * means that there is no request to execute handlers, 11061b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1107e0f66ef8SJohn Baldwin */ 11081b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 11091b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1110e0f66ef8SJohn Baldwin continue; 1111e0f66ef8SJohn Baldwin 1112e0f66ef8SJohn Baldwin /* Execute this handler. */ 1113e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1114bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1115bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1116e0f66ef8SJohn Baldwin 1117e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1118e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1119e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1120e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1121e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1122e0f66ef8SJohn Baldwin } 112337e9511fSJohn Baldwin } 112437e9511fSJohn Baldwin 112537e9511fSJohn Baldwin static void 112637e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 112737e9511fSJohn Baldwin { 112837e9511fSJohn Baldwin 112937e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 113037e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 113137e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 113237e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1133e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1134e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1135e0f66ef8SJohn Baldwin 1136e0f66ef8SJohn Baldwin /* 1137e0f66ef8SJohn Baldwin * Interrupt storm handling: 1138e0f66ef8SJohn Baldwin * 1139e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1140e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1141e0f66ef8SJohn Baldwin * 1142e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1143e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1144e0f66ef8SJohn Baldwin * then enter storming mode. 1145e0f66ef8SJohn Baldwin */ 1146e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1147e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 11480ae62c18SNate Lawson /* Report the message only once every second. */ 11490ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1150e0f66ef8SJohn Baldwin printf( 11510ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1152e0f66ef8SJohn Baldwin ie->ie_name); 1153e0f66ef8SJohn Baldwin } 1154e41bcf3cSJohn Baldwin pause("istorm", 1); 1155e0f66ef8SJohn Baldwin } else 1156e0f66ef8SJohn Baldwin ie->ie_count++; 1157e0f66ef8SJohn Baldwin 1158e0f66ef8SJohn Baldwin /* 1159e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1160e0f66ef8SJohn Baldwin * the interrupt source. 1161e0f66ef8SJohn Baldwin */ 11621ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 11631ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1164e0f66ef8SJohn Baldwin } 1165e0f66ef8SJohn Baldwin 11668088699fSJohn Baldwin /* 1167b4151f71SJohn Baldwin * This is the main code for interrupt threads. 11688088699fSJohn Baldwin */ 116937c84183SPoul-Henning Kamp static void 1170b4151f71SJohn Baldwin ithread_loop(void *arg) 11718088699fSJohn Baldwin { 1172e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1173e0f66ef8SJohn Baldwin struct intr_event *ie; 1174b40ce416SJulian Elischer struct thread *td; 1175b4151f71SJohn Baldwin struct proc *p; 1176e4cd31ddSJeff Roberson int wake; 11778088699fSJohn Baldwin 1178b40ce416SJulian Elischer td = curthread; 1179b40ce416SJulian Elischer p = td->td_proc; 1180e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1181e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 118291f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1183e0f66ef8SJohn Baldwin ie = ithd->it_event; 1184e0f66ef8SJohn Baldwin ie->ie_count = 0; 1185e4cd31ddSJeff Roberson wake = 0; 11868088699fSJohn Baldwin 11878088699fSJohn Baldwin /* 11888088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 11898088699fSJohn Baldwin * list of handlers, giving each one a go at it. 11908088699fSJohn Baldwin */ 11918088699fSJohn Baldwin for (;;) { 1192b4151f71SJohn Baldwin /* 1193b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1194b4151f71SJohn Baldwin */ 1195b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1196e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 11977ab24ea3SJulian Elischer p->p_pid, td->td_name); 1198b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1199ca9a0ddfSJulian Elischer kthread_exit(); 1200b4151f71SJohn Baldwin } 1201b4151f71SJohn Baldwin 1202e0f66ef8SJohn Baldwin /* 1203e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1204e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1205e0f66ef8SJohn Baldwin * should make another pass. 1206283dfee9SKonstantin Belousov * 1207283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1208283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1209283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1210e0f66ef8SJohn Baldwin */ 1211283dfee9SKonstantin Belousov while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) 1212e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 12137870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 12147870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 12158088699fSJohn Baldwin 12168088699fSJohn Baldwin /* 12178088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 12188088699fSJohn Baldwin * lock. This may take a while and it_need may get 12198088699fSJohn Baldwin * set again, so we have to check it again. 12208088699fSJohn Baldwin */ 1221982d11f8SJeff Roberson thread_lock(td); 122203bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 122303bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 12247870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1225e0f66ef8SJohn Baldwin ie->ie_count = 0; 12268df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 12278088699fSJohn Baldwin } 1228e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1229e4cd31ddSJeff Roberson wake = 1; 1230e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1231e4cd31ddSJeff Roberson } 1232982d11f8SJeff Roberson thread_unlock(td); 1233e4cd31ddSJeff Roberson if (wake) { 1234e4cd31ddSJeff Roberson wakeup(ithd); 1235e4cd31ddSJeff Roberson wake = 0; 1236e4cd31ddSJeff Roberson } 12378088699fSJohn Baldwin } 12381931cf94SJohn Baldwin } 12391ee1b687SJohn Baldwin 12401ee1b687SJohn Baldwin /* 12411ee1b687SJohn Baldwin * Main interrupt handling body. 12421ee1b687SJohn Baldwin * 12431ee1b687SJohn Baldwin * Input: 12441ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 12451ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 12461ee1b687SJohn Baldwin * handlers as their main argument. 12471ee1b687SJohn Baldwin * Return value: 12481ee1b687SJohn Baldwin * o 0: everything ok. 12491ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 12501ee1b687SJohn Baldwin */ 12511ee1b687SJohn Baldwin int 12521ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 12531ee1b687SJohn Baldwin { 12541ee1b687SJohn Baldwin struct intr_handler *ih; 12551f255bd3SAlexander Motin struct trapframe *oldframe; 12561ee1b687SJohn Baldwin struct thread *td; 1257e0fa977eSAndriy Gapon int phase; 1258*82a5a275SAndriy Gapon int ret; 1259*82a5a275SAndriy Gapon bool filter, thread; 12601ee1b687SJohn Baldwin 12611ee1b687SJohn Baldwin td = curthread; 12621ee1b687SJohn Baldwin 1263b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1264b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1265b7627840SKonstantin Belousov #endif 1266b7627840SKonstantin Belousov 12671ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1268111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 12691ee1b687SJohn Baldwin return (EINVAL); 12701ee1b687SJohn Baldwin 12711ee1b687SJohn Baldwin /* 12721ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 12731ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 12741ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 12751ee1b687SJohn Baldwin * a trapframe as its argument. 12761ee1b687SJohn Baldwin */ 12771ee1b687SJohn Baldwin td->td_intr_nesting_level++; 1278*82a5a275SAndriy Gapon filter = false; 1279*82a5a275SAndriy Gapon thread = false; 12801ee1b687SJohn Baldwin ret = 0; 12811ee1b687SJohn Baldwin critical_enter(); 12821f255bd3SAlexander Motin oldframe = td->td_intr_frame; 12831f255bd3SAlexander Motin td->td_intr_frame = frame; 1284111b043cSAndriy Gapon 1285e0fa977eSAndriy Gapon phase = ie->ie_phase; 1286e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1287e0fa977eSAndriy Gapon 1288e0fa977eSAndriy Gapon /* 1289e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1290e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1291e0fa977eSAndriy Gapon */ 1292e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1293e0fa977eSAndriy Gapon 1294111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 1295*82a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 1296*82a5a275SAndriy Gapon continue; 12971ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 1298*82a5a275SAndriy Gapon thread = true; 12991ee1b687SJohn Baldwin continue; 13001ee1b687SJohn Baldwin } 13011ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 13021ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 13031ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 13041ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 13051ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 13061ee1b687SJohn Baldwin else 13071ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 130889fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 130989fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 131089fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 131189fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 131289fc20ccSAndriy Gapon ih->ih_name)); 1313*82a5a275SAndriy Gapon filter = filter || ret == FILTER_HANDLED; 131489fc20ccSAndriy Gapon 13151ee1b687SJohn Baldwin /* 13161ee1b687SJohn Baldwin * Wrapper handler special handling: 13171ee1b687SJohn Baldwin * 13181ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 13191ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 13201ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 13211ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 13221ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 13231ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 13241ee1b687SJohn Baldwin * of just an ithread handler. 13251ee1b687SJohn Baldwin * 13261ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 1327*82a5a275SAndriy Gapon * their own. 13281ee1b687SJohn Baldwin */ 13291ee1b687SJohn Baldwin if (!thread) { 13301ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 1331*82a5a275SAndriy Gapon thread = true; 13321ee1b687SJohn Baldwin } 13331ee1b687SJohn Baldwin } 1334e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1335e0fa977eSAndriy Gapon 13361f255bd3SAlexander Motin td->td_intr_frame = oldframe; 13371ee1b687SJohn Baldwin 13381ee1b687SJohn Baldwin if (thread) { 13391ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 13401ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 13411ee1b687SJohn Baldwin } else { 13421ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 13431ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 13441ee1b687SJohn Baldwin } 13451ee1b687SJohn Baldwin 13461ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 13471ee1b687SJohn Baldwin if (thread) { 1348ba3f7276SMatt Macy int error __unused; 1349ba3f7276SMatt Macy 13501ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 13511ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 13521ee1b687SJohn Baldwin } 13531ee1b687SJohn Baldwin critical_exit(); 13541ee1b687SJohn Baldwin td->td_intr_nesting_level--; 1355*82a5a275SAndriy Gapon #ifdef notyet 1356*82a5a275SAndriy Gapon /* The interrupt is not aknowledged by any filter and has no ithread. */ 1357*82a5a275SAndriy Gapon if (!thread && !filter) 1358*82a5a275SAndriy Gapon return (EINVAL); 1359*82a5a275SAndriy Gapon #endif 13601ee1b687SJohn Baldwin return (0); 13611ee1b687SJohn Baldwin } 13621931cf94SJohn Baldwin 13638b201c42SJohn Baldwin #ifdef DDB 13648b201c42SJohn Baldwin /* 13658b201c42SJohn Baldwin * Dump details about an interrupt handler 13668b201c42SJohn Baldwin */ 13678b201c42SJohn Baldwin static void 1368e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 13698b201c42SJohn Baldwin { 13708b201c42SJohn Baldwin int comma; 13718b201c42SJohn Baldwin 13728b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 13738b201c42SJohn Baldwin switch (ih->ih_pri) { 13748b201c42SJohn Baldwin case PI_REALTIME: 13758b201c42SJohn Baldwin db_printf("CLK "); 13768b201c42SJohn Baldwin break; 13778b201c42SJohn Baldwin case PI_AV: 13788b201c42SJohn Baldwin db_printf("AV "); 13798b201c42SJohn Baldwin break; 1380d3305205SJohn Baldwin case PI_TTY: 13818b201c42SJohn Baldwin db_printf("TTY "); 13828b201c42SJohn Baldwin break; 13838b201c42SJohn Baldwin case PI_NET: 13848b201c42SJohn Baldwin db_printf("NET "); 13858b201c42SJohn Baldwin break; 13868b201c42SJohn Baldwin case PI_DISK: 13878b201c42SJohn Baldwin db_printf("DISK"); 13888b201c42SJohn Baldwin break; 13898b201c42SJohn Baldwin case PI_DULL: 13908b201c42SJohn Baldwin db_printf("DULL"); 13918b201c42SJohn Baldwin break; 13928b201c42SJohn Baldwin default: 13938b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 13948b201c42SJohn Baldwin db_printf("SWI "); 13958b201c42SJohn Baldwin else 13968b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 13978b201c42SJohn Baldwin break; 13988b201c42SJohn Baldwin } 13998b201c42SJohn Baldwin db_printf(" "); 1400b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1401b887a155SKonstantin Belousov db_printf("[F]"); 1402b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1403b887a155SKonstantin Belousov } 1404b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1405b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1406b887a155SKonstantin Belousov db_printf(","); 1407b887a155SKonstantin Belousov db_printf("[H]"); 14088b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1409b887a155SKonstantin Belousov } 14108b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 14118b201c42SJohn Baldwin if (ih->ih_need || 1412ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 14138b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 14148b201c42SJohn Baldwin db_printf(" {"); 14158b201c42SJohn Baldwin comma = 0; 14168b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 14178b201c42SJohn Baldwin if (comma) 14188b201c42SJohn Baldwin db_printf(", "); 14198b201c42SJohn Baldwin db_printf("EXCL"); 14208b201c42SJohn Baldwin comma = 1; 14218b201c42SJohn Baldwin } 14228b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 14238b201c42SJohn Baldwin if (comma) 14248b201c42SJohn Baldwin db_printf(", "); 14258b201c42SJohn Baldwin db_printf("ENTROPY"); 14268b201c42SJohn Baldwin comma = 1; 14278b201c42SJohn Baldwin } 14288b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 14298b201c42SJohn Baldwin if (comma) 14308b201c42SJohn Baldwin db_printf(", "); 14318b201c42SJohn Baldwin db_printf("DEAD"); 14328b201c42SJohn Baldwin comma = 1; 14338b201c42SJohn Baldwin } 14348b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 14358b201c42SJohn Baldwin if (comma) 14368b201c42SJohn Baldwin db_printf(", "); 14378b201c42SJohn Baldwin db_printf("MPSAFE"); 14388b201c42SJohn Baldwin comma = 1; 14398b201c42SJohn Baldwin } 14408b201c42SJohn Baldwin if (ih->ih_need) { 14418b201c42SJohn Baldwin if (comma) 14428b201c42SJohn Baldwin db_printf(", "); 14438b201c42SJohn Baldwin db_printf("NEED"); 14448b201c42SJohn Baldwin } 14458b201c42SJohn Baldwin db_printf("}"); 14468b201c42SJohn Baldwin } 14478b201c42SJohn Baldwin db_printf("\n"); 14488b201c42SJohn Baldwin } 14498b201c42SJohn Baldwin 14508b201c42SJohn Baldwin /* 1451e0f66ef8SJohn Baldwin * Dump details about a event. 14528b201c42SJohn Baldwin */ 14538b201c42SJohn Baldwin void 1454e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 14558b201c42SJohn Baldwin { 1456e0f66ef8SJohn Baldwin struct intr_handler *ih; 1457e0f66ef8SJohn Baldwin struct intr_thread *it; 14588b201c42SJohn Baldwin int comma; 14598b201c42SJohn Baldwin 1460e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1461e0f66ef8SJohn Baldwin it = ie->ie_thread; 1462e0f66ef8SJohn Baldwin if (it != NULL) 1463e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1464e0f66ef8SJohn Baldwin else 1465e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1466e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1467e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 14688b201c42SJohn Baldwin db_printf(" {"); 14698b201c42SJohn Baldwin comma = 0; 1470e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 14718b201c42SJohn Baldwin db_printf("SOFT"); 14728b201c42SJohn Baldwin comma = 1; 14738b201c42SJohn Baldwin } 1474e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 14758b201c42SJohn Baldwin if (comma) 14768b201c42SJohn Baldwin db_printf(", "); 14778b201c42SJohn Baldwin db_printf("ENTROPY"); 14788b201c42SJohn Baldwin comma = 1; 14798b201c42SJohn Baldwin } 1480e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 14818b201c42SJohn Baldwin if (comma) 14828b201c42SJohn Baldwin db_printf(", "); 1483e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 14848b201c42SJohn Baldwin comma = 1; 14858b201c42SJohn Baldwin } 1486e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 14878b201c42SJohn Baldwin if (comma) 14888b201c42SJohn Baldwin db_printf(", "); 14898b201c42SJohn Baldwin db_printf("NEED"); 14908b201c42SJohn Baldwin } 14918b201c42SJohn Baldwin db_printf("}"); 14928b201c42SJohn Baldwin } 14938b201c42SJohn Baldwin db_printf("\n"); 14948b201c42SJohn Baldwin 14958b201c42SJohn Baldwin if (handlers) 1496111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 14978b201c42SJohn Baldwin db_dump_intrhand(ih); 14988b201c42SJohn Baldwin } 1499e0f66ef8SJohn Baldwin 1500e0f66ef8SJohn Baldwin /* 1501e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1502e0f66ef8SJohn Baldwin */ 1503e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1504e0f66ef8SJohn Baldwin { 1505e0f66ef8SJohn Baldwin struct intr_event *ie; 150619e9205aSJohn Baldwin int all, verbose; 1507e0f66ef8SJohn Baldwin 1508dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1509dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1510e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1511111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1512e0f66ef8SJohn Baldwin continue; 1513e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 151419e9205aSJohn Baldwin if (db_pager_quit) 151519e9205aSJohn Baldwin break; 1516e0f66ef8SJohn Baldwin } 1517e0f66ef8SJohn Baldwin } 15188b201c42SJohn Baldwin #endif /* DDB */ 15198b201c42SJohn Baldwin 1520b4151f71SJohn Baldwin /* 15218088699fSJohn Baldwin * Start standard software interrupt threads 15221931cf94SJohn Baldwin */ 15231931cf94SJohn Baldwin static void 1524b4151f71SJohn Baldwin start_softintr(void *dummy) 15251931cf94SJohn Baldwin { 1526b4151f71SJohn Baldwin 15278d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 15288d809d50SJeff Roberson panic("died while creating vm swi ithread"); 15291931cf94SJohn Baldwin } 1530237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1531237fdd78SRobert Watson NULL); 15321931cf94SJohn Baldwin 1533d279178dSThomas Moestl /* 1534d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1535d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1536d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1537d279178dSThomas Moestl * independent. 1538d279178dSThomas Moestl * 1539d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1540d279178dSThomas Moestl * calculate things at run time. 1541d279178dSThomas Moestl */ 1542d279178dSThomas Moestl static int 1543d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1544d279178dSThomas Moestl { 1545521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1546d279178dSThomas Moestl } 1547d279178dSThomas Moestl 1548d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1549d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1550d279178dSThomas Moestl 1551d279178dSThomas Moestl static int 1552d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1553d279178dSThomas Moestl { 155485729c2cSJuli Mallett #ifdef SCTL_MASK32 155585729c2cSJuli Mallett uint32_t *intrcnt32; 155685729c2cSJuli Mallett unsigned i; 155785729c2cSJuli Mallett int error; 155885729c2cSJuli Mallett 155985729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 156085729c2cSJuli Mallett if (!req->oldptr) 156185729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 156285729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 156385729c2cSJuli Mallett if (intrcnt32 == NULL) 156485729c2cSJuli Mallett return (ENOMEM); 156585729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 156685729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 156785729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 156885729c2cSJuli Mallett free(intrcnt32, M_TEMP); 156985729c2cSJuli Mallett return (error); 157085729c2cSJuli Mallett } 157185729c2cSJuli Mallett #endif 1572521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1573d279178dSThomas Moestl } 1574d279178dSThomas Moestl 1575d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1576d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 15778b201c42SJohn Baldwin 15788b201c42SJohn Baldwin #ifdef DDB 15798b201c42SJohn Baldwin /* 15808b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 15818b201c42SJohn Baldwin */ 15828b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 15838b201c42SJohn Baldwin { 15848b201c42SJohn Baldwin u_long *i; 15858b201c42SJohn Baldwin char *cp; 1586521ea19dSAttilio Rao u_int j; 15878b201c42SJohn Baldwin 15888b201c42SJohn Baldwin cp = intrnames; 1589521ea19dSAttilio Rao j = 0; 1590521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1591521ea19dSAttilio Rao i++, j++) { 15928b201c42SJohn Baldwin if (*cp == '\0') 15938b201c42SJohn Baldwin break; 15948b201c42SJohn Baldwin if (*i != 0) 15958b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 15968b201c42SJohn Baldwin cp += strlen(cp) + 1; 15978b201c42SJohn Baldwin } 15988b201c42SJohn Baldwin } 15998b201c42SJohn Baldwin #endif 1600