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 724e0fa977eSAndriy Gapon /* 725e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 726e4cd31ddSJeff Roberson * 727e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 728e4cd31ddSJeff Roberson * handlers. This is intended for compatibility with linux drivers 729e4cd31ddSJeff Roberson * only. Do not use in BSD code. 730e4cd31ddSJeff Roberson */ 731e4cd31ddSJeff Roberson void 732e4cd31ddSJeff Roberson _intr_drain(int irq) 733e4cd31ddSJeff Roberson { 734e4cd31ddSJeff Roberson struct intr_event *ie; 735e4cd31ddSJeff Roberson struct intr_thread *ithd; 736e4cd31ddSJeff Roberson struct thread *td; 737e4cd31ddSJeff Roberson 738e4cd31ddSJeff Roberson ie = intr_lookup(irq); 739e4cd31ddSJeff Roberson if (ie == NULL) 740e4cd31ddSJeff Roberson return; 741e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 742e4cd31ddSJeff Roberson return; 743e4cd31ddSJeff Roberson ithd = ie->ie_thread; 744e4cd31ddSJeff Roberson td = ithd->it_thread; 7455bd186a6SJeff Roberson /* 7465bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 7475bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 7485bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 7495bd186a6SJeff Roberson */ 750e4cd31ddSJeff Roberson thread_lock(td); 751e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 752e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 7535bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 7545bd186a6SJeff Roberson thread_unlock(td); 7555bd186a6SJeff Roberson pause("idrain", 1); 7565bd186a6SJeff Roberson thread_lock(td); 757e4cd31ddSJeff Roberson } 7585bd186a6SJeff Roberson } 7595bd186a6SJeff Roberson thread_unlock(td); 760e4cd31ddSJeff Roberson return; 761e4cd31ddSJeff Roberson } 762e4cd31ddSJeff Roberson 763b4151f71SJohn Baldwin int 764e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 765b4151f71SJohn Baldwin { 766e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 767e0f66ef8SJohn Baldwin struct intr_event *ie; 768e0f66ef8SJohn Baldwin struct intr_handler *ih; 769111b043cSAndriy Gapon struct intr_handler **prevptr; 770e0f66ef8SJohn Baldwin #ifdef notyet 771e0f66ef8SJohn Baldwin int dead; 772b4151f71SJohn Baldwin #endif 773b4151f71SJohn Baldwin 7743e5da754SJohn Baldwin if (handler == NULL) 775b4151f71SJohn Baldwin return (EINVAL); 776e0f66ef8SJohn Baldwin ie = handler->ih_event; 777e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 778e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 7793e5da754SJohn Baldwin handler->ih_name)); 780111b043cSAndriy Gapon 781e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 78291f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 783e0f66ef8SJohn Baldwin ie->ie_name); 784111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 7853e5da754SJohn Baldwin if (ih == handler) 786111b043cSAndriy Gapon break; 787111b043cSAndriy Gapon } 788111b043cSAndriy Gapon if (ih == NULL) { 789111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 790111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 791111b043cSAndriy Gapon } 792111b043cSAndriy Gapon 793de271f01SJohn Baldwin /* 794e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 795e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 796e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 797e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 798e0f66ef8SJohn Baldwin */ 799e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 800111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 801e0fa977eSAndriy Gapon intr_event_barrier(ie); 802e0fa977eSAndriy Gapon intr_event_update(ie); 803e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 804e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 805e0f66ef8SJohn Baldwin return (0); 806e0f66ef8SJohn Baldwin } 807e0f66ef8SJohn Baldwin 808e0f66ef8SJohn Baldwin /* 809e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 810e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 811e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 812e0fa977eSAndriy Gapon * intr_event_handle(). 813de271f01SJohn Baldwin */ 814e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 815e0fa977eSAndriy Gapon ("duplicate handle remove")); 816de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 817e0fa977eSAndriy Gapon intr_event_schedule_thread(ie); 818e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 8190f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 820e0f66ef8SJohn Baldwin intr_event_update(ie); 821111b043cSAndriy Gapon 822e0f66ef8SJohn Baldwin #ifdef notyet 823e0f66ef8SJohn Baldwin /* 824e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 825e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 826e0f66ef8SJohn Baldwin * interrupt. 827e0f66ef8SJohn Baldwin */ 828e0f66ef8SJohn Baldwin dead = 1; 829111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 830111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 831e0f66ef8SJohn Baldwin dead = 0; 832e0f66ef8SJohn Baldwin break; 833e0f66ef8SJohn Baldwin } 834e0f66ef8SJohn Baldwin } 835e0f66ef8SJohn Baldwin if (dead) { 836e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 837e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 838e0f66ef8SJohn Baldwin } 839e0f66ef8SJohn Baldwin #endif 840e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 841b4151f71SJohn Baldwin free(handler, M_ITHREAD); 842b4151f71SJohn Baldwin return (0); 843b4151f71SJohn Baldwin } 844b4151f71SJohn Baldwin 8451ee1b687SJohn Baldwin static int 846e0f66ef8SJohn Baldwin intr_event_schedule_thread(struct intr_event *ie) 8473e5da754SJohn Baldwin { 848e0f66ef8SJohn Baldwin struct intr_entropy entropy; 849e0f66ef8SJohn Baldwin struct intr_thread *it; 850b40ce416SJulian Elischer struct thread *td; 85104774f23SJulian Elischer struct thread *ctd; 8523e5da754SJohn Baldwin 8533e5da754SJohn Baldwin /* 8543e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 8553e5da754SJohn Baldwin */ 856111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 857e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 8583e5da754SJohn Baldwin return (EINVAL); 8593e5da754SJohn Baldwin 86004774f23SJulian Elischer ctd = curthread; 861e0f66ef8SJohn Baldwin it = ie->ie_thread; 862e0f66ef8SJohn Baldwin td = it->it_thread; 863e0f66ef8SJohn Baldwin 8643e5da754SJohn Baldwin /* 8653e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 8663e5da754SJohn Baldwin * sources of entropy, then gather some. 8673e5da754SJohn Baldwin */ 86810cb2424SMark Murray if (ie->ie_flags & IE_ENTROPY) { 869e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 870e0f66ef8SJohn Baldwin entropy.td = ctd; 871*19fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 8723e5da754SJohn Baldwin } 8733e5da754SJohn Baldwin 874ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 8753e5da754SJohn Baldwin 8763e5da754SJohn Baldwin /* 8773e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 878982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 879982d11f8SJeff Roberson * put it on the runqueue. 880283dfee9SKonstantin Belousov * 881283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 882283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 883283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 8843e5da754SJohn Baldwin */ 8853eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 886982d11f8SJeff Roberson thread_lock(td); 88771fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 888fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 8897ab24ea3SJulian Elischer td->td_name); 89071fad9fdSJulian Elischer TD_CLR_IWAIT(td); 891f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 8923e5da754SJohn Baldwin } else { 893e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 894fc2e87beSMatt Macy __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state); 8953e5da754SJohn Baldwin } 896982d11f8SJeff Roberson thread_unlock(td); 8973e5da754SJohn Baldwin 8983e5da754SJohn Baldwin return (0); 8993e5da754SJohn Baldwin } 9003e5da754SJohn Baldwin 901fe486a37SJohn Baldwin /* 902e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 903e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 904e84bcd84SRobert Watson * a PIC. 905e84bcd84SRobert Watson */ 906e84bcd84SRobert Watson static int 907066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 908e84bcd84SRobert Watson { 909e84bcd84SRobert Watson 910e84bcd84SRobert Watson return (0); 911e84bcd84SRobert Watson } 912e84bcd84SRobert Watson 913e84bcd84SRobert Watson /* 914fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 915fe486a37SJohn Baldwin * is not specified, then a new event is created. 916fe486a37SJohn Baldwin */ 9173e5da754SJohn Baldwin int 918e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 919b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 9208088699fSJohn Baldwin { 921e0f66ef8SJohn Baldwin struct intr_event *ie; 922b4151f71SJohn Baldwin int error; 9238088699fSJohn Baldwin 924bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 9253e5da754SJohn Baldwin return (EINVAL); 9263e5da754SJohn Baldwin 927e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 9288088699fSJohn Baldwin 929e0f66ef8SJohn Baldwin if (ie != NULL) { 930e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 9313e5da754SJohn Baldwin return (EINVAL); 9323e5da754SJohn Baldwin } else { 9339b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 934e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 9358088699fSJohn Baldwin if (error) 936b4151f71SJohn Baldwin return (error); 937e0f66ef8SJohn Baldwin if (eventp != NULL) 938e0f66ef8SJohn Baldwin *eventp = ie; 9398088699fSJohn Baldwin } 9408d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 941d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 9428d809d50SJeff Roberson return (error); 9438088699fSJohn Baldwin } 9448088699fSJohn Baldwin 9451931cf94SJohn Baldwin /* 946e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 9471931cf94SJohn Baldwin */ 9481931cf94SJohn Baldwin void 949b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 9501931cf94SJohn Baldwin { 951e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 952e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 953d95dca1dSJohn Baldwin struct intr_entropy entropy; 954ba3f7276SMatt Macy int error __unused; 9558088699fSJohn Baldwin 956e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 957e0f66ef8SJohn Baldwin ih->ih_need); 9581931cf94SJohn Baldwin 959d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 960d95dca1dSJohn Baldwin entropy.td = curthread; 961*19fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 962d95dca1dSJohn Baldwin 9631931cf94SJohn Baldwin /* 9643e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 9653e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 9663e5da754SJohn Baldwin * it will execute it the next time it runs. 9671931cf94SJohn Baldwin */ 968283dfee9SKonstantin Belousov ih->ih_need = 1; 9691ca2c018SBruce Evans 970b4151f71SJohn Baldwin if (!(flags & SWI_DELAY)) { 97183c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 972e0f66ef8SJohn Baldwin error = intr_event_schedule_thread(ie); 9733e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 9748088699fSJohn Baldwin } 9758088699fSJohn Baldwin } 9768088699fSJohn Baldwin 977fe486a37SJohn Baldwin /* 978fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 979fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 980fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 981fe486a37SJohn Baldwin * an optimal interface. 982fe486a37SJohn Baldwin */ 983fe486a37SJohn Baldwin int 984fe486a37SJohn Baldwin swi_remove(void *cookie) 985fe486a37SJohn Baldwin { 986fe486a37SJohn Baldwin 987fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 988fe486a37SJohn Baldwin } 989fe486a37SJohn Baldwin 990111b043cSAndriy Gapon static void 99137e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 992e0f66ef8SJohn Baldwin { 993111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 994e0f66ef8SJohn Baldwin 995111b043cSAndriy Gapon ihp = NULL; 996111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 997e0f66ef8SJohn Baldwin /* 998e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 999e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1000e0f66ef8SJohn Baldwin */ 1001e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1002e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1003111b043cSAndriy Gapon if (ihp == NULL) 1004111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1005111b043cSAndriy Gapon else 1006111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1007e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1008e0f66ef8SJohn Baldwin wakeup(ih); 1009e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1010e0f66ef8SJohn Baldwin continue; 1011e0f66ef8SJohn Baldwin } 1012e0f66ef8SJohn Baldwin 1013111b043cSAndriy Gapon /* 1014111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1015111b043cSAndriy Gapon * update the previous element. 1016111b043cSAndriy Gapon */ 1017111b043cSAndriy Gapon ihp = ih; 1018111b043cSAndriy Gapon 1019f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1020f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1021f2d619c8SPaolo Pisati continue; 1022f2d619c8SPaolo Pisati 1023e0f66ef8SJohn Baldwin /* 1024e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1025e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1026e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 10271b79b949SKirk McKusick * 10281b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 10291b79b949SKirk McKusick * means that there is no request to execute handlers, 10301b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1031e0f66ef8SJohn Baldwin */ 10321b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 10331b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1034e0f66ef8SJohn Baldwin continue; 1035e0f66ef8SJohn Baldwin 1036e0f66ef8SJohn Baldwin /* Execute this handler. */ 1037e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1038bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1039bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1040e0f66ef8SJohn Baldwin 1041e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1042e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1043e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1044e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1045e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1046e0f66ef8SJohn Baldwin } 104737e9511fSJohn Baldwin } 104837e9511fSJohn Baldwin 104937e9511fSJohn Baldwin static void 105037e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 105137e9511fSJohn Baldwin { 105237e9511fSJohn Baldwin 105337e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 105437e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 105537e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 105637e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1057e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1058e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1059e0f66ef8SJohn Baldwin 1060e0f66ef8SJohn Baldwin /* 1061e0f66ef8SJohn Baldwin * Interrupt storm handling: 1062e0f66ef8SJohn Baldwin * 1063e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1064e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1065e0f66ef8SJohn Baldwin * 1066e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1067e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1068e0f66ef8SJohn Baldwin * then enter storming mode. 1069e0f66ef8SJohn Baldwin */ 1070e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1071e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 10720ae62c18SNate Lawson /* Report the message only once every second. */ 10730ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1074e0f66ef8SJohn Baldwin printf( 10750ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1076e0f66ef8SJohn Baldwin ie->ie_name); 1077e0f66ef8SJohn Baldwin } 1078e41bcf3cSJohn Baldwin pause("istorm", 1); 1079e0f66ef8SJohn Baldwin } else 1080e0f66ef8SJohn Baldwin ie->ie_count++; 1081e0f66ef8SJohn Baldwin 1082e0f66ef8SJohn Baldwin /* 1083e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1084e0f66ef8SJohn Baldwin * the interrupt source. 1085e0f66ef8SJohn Baldwin */ 10861ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 10871ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1088e0f66ef8SJohn Baldwin } 1089e0f66ef8SJohn Baldwin 10908088699fSJohn Baldwin /* 1091b4151f71SJohn Baldwin * This is the main code for interrupt threads. 10928088699fSJohn Baldwin */ 109337c84183SPoul-Henning Kamp static void 1094b4151f71SJohn Baldwin ithread_loop(void *arg) 10958088699fSJohn Baldwin { 1096e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1097e0f66ef8SJohn Baldwin struct intr_event *ie; 1098b40ce416SJulian Elischer struct thread *td; 1099b4151f71SJohn Baldwin struct proc *p; 1100e4cd31ddSJeff Roberson int wake; 11018088699fSJohn Baldwin 1102b40ce416SJulian Elischer td = curthread; 1103b40ce416SJulian Elischer p = td->td_proc; 1104e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1105e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 110691f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1107e0f66ef8SJohn Baldwin ie = ithd->it_event; 1108e0f66ef8SJohn Baldwin ie->ie_count = 0; 1109e4cd31ddSJeff Roberson wake = 0; 11108088699fSJohn Baldwin 11118088699fSJohn Baldwin /* 11128088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 11138088699fSJohn Baldwin * list of handlers, giving each one a go at it. 11148088699fSJohn Baldwin */ 11158088699fSJohn Baldwin for (;;) { 1116b4151f71SJohn Baldwin /* 1117b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1118b4151f71SJohn Baldwin */ 1119b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1120e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 11217ab24ea3SJulian Elischer p->p_pid, td->td_name); 1122b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1123ca9a0ddfSJulian Elischer kthread_exit(); 1124b4151f71SJohn Baldwin } 1125b4151f71SJohn Baldwin 1126e0f66ef8SJohn Baldwin /* 1127e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1128e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1129e0f66ef8SJohn Baldwin * should make another pass. 1130283dfee9SKonstantin Belousov * 1131283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1132283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1133283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1134e0f66ef8SJohn Baldwin */ 1135283dfee9SKonstantin Belousov while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) 1136e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 11377870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 11387870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 11398088699fSJohn Baldwin 11408088699fSJohn Baldwin /* 11418088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 11428088699fSJohn Baldwin * lock. This may take a while and it_need may get 11438088699fSJohn Baldwin * set again, so we have to check it again. 11448088699fSJohn Baldwin */ 1145982d11f8SJeff Roberson thread_lock(td); 114603bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 114703bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 11487870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1149e0f66ef8SJohn Baldwin ie->ie_count = 0; 11508df78c41SJeff Roberson mi_switch(SW_VOL | SWT_IWAIT, NULL); 11518088699fSJohn Baldwin } 1152e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1153e4cd31ddSJeff Roberson wake = 1; 1154e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1155e4cd31ddSJeff Roberson } 1156982d11f8SJeff Roberson thread_unlock(td); 1157e4cd31ddSJeff Roberson if (wake) { 1158e4cd31ddSJeff Roberson wakeup(ithd); 1159e4cd31ddSJeff Roberson wake = 0; 1160e4cd31ddSJeff Roberson } 11618088699fSJohn Baldwin } 11621931cf94SJohn Baldwin } 11631ee1b687SJohn Baldwin 11641ee1b687SJohn Baldwin /* 11651ee1b687SJohn Baldwin * Main interrupt handling body. 11661ee1b687SJohn Baldwin * 11671ee1b687SJohn Baldwin * Input: 11681ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 11691ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 11701ee1b687SJohn Baldwin * handlers as their main argument. 11711ee1b687SJohn Baldwin * Return value: 11721ee1b687SJohn Baldwin * o 0: everything ok. 11731ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 11741ee1b687SJohn Baldwin */ 11751ee1b687SJohn Baldwin int 11761ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 11771ee1b687SJohn Baldwin { 11781ee1b687SJohn Baldwin struct intr_handler *ih; 11791f255bd3SAlexander Motin struct trapframe *oldframe; 11801ee1b687SJohn Baldwin struct thread *td; 1181ba3f7276SMatt Macy int ret, thread; 1182e0fa977eSAndriy Gapon int phase; 11831ee1b687SJohn Baldwin 11841ee1b687SJohn Baldwin td = curthread; 11851ee1b687SJohn Baldwin 1186b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1187b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1188b7627840SKonstantin Belousov #endif 1189b7627840SKonstantin Belousov 11901ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1191111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 11921ee1b687SJohn Baldwin return (EINVAL); 11931ee1b687SJohn Baldwin 11941ee1b687SJohn Baldwin /* 11951ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 11961ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 11971ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 11981ee1b687SJohn Baldwin * a trapframe as its argument. 11991ee1b687SJohn Baldwin */ 12001ee1b687SJohn Baldwin td->td_intr_nesting_level++; 12011ee1b687SJohn Baldwin thread = 0; 12021ee1b687SJohn Baldwin ret = 0; 12031ee1b687SJohn Baldwin critical_enter(); 12041f255bd3SAlexander Motin oldframe = td->td_intr_frame; 12051f255bd3SAlexander Motin td->td_intr_frame = frame; 1206111b043cSAndriy Gapon 1207e0fa977eSAndriy Gapon phase = ie->ie_phase; 1208e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1209e0fa977eSAndriy Gapon 1210e0fa977eSAndriy Gapon /* 1211e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1212e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1213e0fa977eSAndriy Gapon */ 1214e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1215e0fa977eSAndriy Gapon 1216111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 12171ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 12181ee1b687SJohn Baldwin thread = 1; 12191ee1b687SJohn Baldwin continue; 12201ee1b687SJohn Baldwin } 12211ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 12221ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 12231ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 12241ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 12251ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 12261ee1b687SJohn Baldwin else 12271ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 122889fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 122989fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 123089fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 123189fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 123289fc20ccSAndriy Gapon ih->ih_name)); 123389fc20ccSAndriy Gapon 12341ee1b687SJohn Baldwin /* 12351ee1b687SJohn Baldwin * Wrapper handler special handling: 12361ee1b687SJohn Baldwin * 12371ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 12381ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 12391ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 12401ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 12411ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 12421ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 12431ee1b687SJohn Baldwin * of just an ithread handler. 12441ee1b687SJohn Baldwin * 12451ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 12461ee1b687SJohn Baldwin * their own 12471ee1b687SJohn Baldwin */ 12481ee1b687SJohn Baldwin if (!thread) { 12491ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 12501ee1b687SJohn Baldwin thread = 1; 12511ee1b687SJohn Baldwin } 12521ee1b687SJohn Baldwin } 1253e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1254e0fa977eSAndriy Gapon 12551f255bd3SAlexander Motin td->td_intr_frame = oldframe; 12561ee1b687SJohn Baldwin 12571ee1b687SJohn Baldwin if (thread) { 12581ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 12591ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 12601ee1b687SJohn Baldwin } else { 12611ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 12621ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 12631ee1b687SJohn Baldwin } 12641ee1b687SJohn Baldwin 12651ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 12661ee1b687SJohn Baldwin if (thread) { 1267ba3f7276SMatt Macy int error __unused; 1268ba3f7276SMatt Macy 12691ee1b687SJohn Baldwin error = intr_event_schedule_thread(ie); 12701ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 12711ee1b687SJohn Baldwin } 12721ee1b687SJohn Baldwin critical_exit(); 12731ee1b687SJohn Baldwin td->td_intr_nesting_level--; 12741ee1b687SJohn Baldwin return (0); 12751ee1b687SJohn Baldwin } 12761931cf94SJohn Baldwin 12778b201c42SJohn Baldwin #ifdef DDB 12788b201c42SJohn Baldwin /* 12798b201c42SJohn Baldwin * Dump details about an interrupt handler 12808b201c42SJohn Baldwin */ 12818b201c42SJohn Baldwin static void 1282e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 12838b201c42SJohn Baldwin { 12848b201c42SJohn Baldwin int comma; 12858b201c42SJohn Baldwin 12868b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 12878b201c42SJohn Baldwin switch (ih->ih_pri) { 12888b201c42SJohn Baldwin case PI_REALTIME: 12898b201c42SJohn Baldwin db_printf("CLK "); 12908b201c42SJohn Baldwin break; 12918b201c42SJohn Baldwin case PI_AV: 12928b201c42SJohn Baldwin db_printf("AV "); 12938b201c42SJohn Baldwin break; 1294d3305205SJohn Baldwin case PI_TTY: 12958b201c42SJohn Baldwin db_printf("TTY "); 12968b201c42SJohn Baldwin break; 12978b201c42SJohn Baldwin case PI_NET: 12988b201c42SJohn Baldwin db_printf("NET "); 12998b201c42SJohn Baldwin break; 13008b201c42SJohn Baldwin case PI_DISK: 13018b201c42SJohn Baldwin db_printf("DISK"); 13028b201c42SJohn Baldwin break; 13038b201c42SJohn Baldwin case PI_DULL: 13048b201c42SJohn Baldwin db_printf("DULL"); 13058b201c42SJohn Baldwin break; 13068b201c42SJohn Baldwin default: 13078b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 13088b201c42SJohn Baldwin db_printf("SWI "); 13098b201c42SJohn Baldwin else 13108b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 13118b201c42SJohn Baldwin break; 13128b201c42SJohn Baldwin } 13138b201c42SJohn Baldwin db_printf(" "); 1314b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1315b887a155SKonstantin Belousov db_printf("[F]"); 1316b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1317b887a155SKonstantin Belousov } 1318b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1319b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1320b887a155SKonstantin Belousov db_printf(","); 1321b887a155SKonstantin Belousov db_printf("[H]"); 13228b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1323b887a155SKonstantin Belousov } 13248b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 13258b201c42SJohn Baldwin if (ih->ih_need || 1326ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 13278b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 13288b201c42SJohn Baldwin db_printf(" {"); 13298b201c42SJohn Baldwin comma = 0; 13308b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 13318b201c42SJohn Baldwin if (comma) 13328b201c42SJohn Baldwin db_printf(", "); 13338b201c42SJohn Baldwin db_printf("EXCL"); 13348b201c42SJohn Baldwin comma = 1; 13358b201c42SJohn Baldwin } 13368b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 13378b201c42SJohn Baldwin if (comma) 13388b201c42SJohn Baldwin db_printf(", "); 13398b201c42SJohn Baldwin db_printf("ENTROPY"); 13408b201c42SJohn Baldwin comma = 1; 13418b201c42SJohn Baldwin } 13428b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 13438b201c42SJohn Baldwin if (comma) 13448b201c42SJohn Baldwin db_printf(", "); 13458b201c42SJohn Baldwin db_printf("DEAD"); 13468b201c42SJohn Baldwin comma = 1; 13478b201c42SJohn Baldwin } 13488b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 13498b201c42SJohn Baldwin if (comma) 13508b201c42SJohn Baldwin db_printf(", "); 13518b201c42SJohn Baldwin db_printf("MPSAFE"); 13528b201c42SJohn Baldwin comma = 1; 13538b201c42SJohn Baldwin } 13548b201c42SJohn Baldwin if (ih->ih_need) { 13558b201c42SJohn Baldwin if (comma) 13568b201c42SJohn Baldwin db_printf(", "); 13578b201c42SJohn Baldwin db_printf("NEED"); 13588b201c42SJohn Baldwin } 13598b201c42SJohn Baldwin db_printf("}"); 13608b201c42SJohn Baldwin } 13618b201c42SJohn Baldwin db_printf("\n"); 13628b201c42SJohn Baldwin } 13638b201c42SJohn Baldwin 13648b201c42SJohn Baldwin /* 1365e0f66ef8SJohn Baldwin * Dump details about a event. 13668b201c42SJohn Baldwin */ 13678b201c42SJohn Baldwin void 1368e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 13698b201c42SJohn Baldwin { 1370e0f66ef8SJohn Baldwin struct intr_handler *ih; 1371e0f66ef8SJohn Baldwin struct intr_thread *it; 13728b201c42SJohn Baldwin int comma; 13738b201c42SJohn Baldwin 1374e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1375e0f66ef8SJohn Baldwin it = ie->ie_thread; 1376e0f66ef8SJohn Baldwin if (it != NULL) 1377e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1378e0f66ef8SJohn Baldwin else 1379e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1380e0f66ef8SJohn Baldwin if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 || 1381e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 13828b201c42SJohn Baldwin db_printf(" {"); 13838b201c42SJohn Baldwin comma = 0; 1384e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 13858b201c42SJohn Baldwin db_printf("SOFT"); 13868b201c42SJohn Baldwin comma = 1; 13878b201c42SJohn Baldwin } 1388e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ENTROPY) { 13898b201c42SJohn Baldwin if (comma) 13908b201c42SJohn Baldwin db_printf(", "); 13918b201c42SJohn Baldwin db_printf("ENTROPY"); 13928b201c42SJohn Baldwin comma = 1; 13938b201c42SJohn Baldwin } 1394e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 13958b201c42SJohn Baldwin if (comma) 13968b201c42SJohn Baldwin db_printf(", "); 1397e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 13988b201c42SJohn Baldwin comma = 1; 13998b201c42SJohn Baldwin } 1400e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 14018b201c42SJohn Baldwin if (comma) 14028b201c42SJohn Baldwin db_printf(", "); 14038b201c42SJohn Baldwin db_printf("NEED"); 14048b201c42SJohn Baldwin } 14058b201c42SJohn Baldwin db_printf("}"); 14068b201c42SJohn Baldwin } 14078b201c42SJohn Baldwin db_printf("\n"); 14088b201c42SJohn Baldwin 14098b201c42SJohn Baldwin if (handlers) 1410111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 14118b201c42SJohn Baldwin db_dump_intrhand(ih); 14128b201c42SJohn Baldwin } 1413e0f66ef8SJohn Baldwin 1414e0f66ef8SJohn Baldwin /* 1415e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1416e0f66ef8SJohn Baldwin */ 1417e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1418e0f66ef8SJohn Baldwin { 1419e0f66ef8SJohn Baldwin struct intr_event *ie; 142019e9205aSJohn Baldwin int all, verbose; 1421e0f66ef8SJohn Baldwin 1422dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1423dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1424e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1425111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1426e0f66ef8SJohn Baldwin continue; 1427e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 142819e9205aSJohn Baldwin if (db_pager_quit) 142919e9205aSJohn Baldwin break; 1430e0f66ef8SJohn Baldwin } 1431e0f66ef8SJohn Baldwin } 14328b201c42SJohn Baldwin #endif /* DDB */ 14338b201c42SJohn Baldwin 1434b4151f71SJohn Baldwin /* 14358088699fSJohn Baldwin * Start standard software interrupt threads 14361931cf94SJohn Baldwin */ 14371931cf94SJohn Baldwin static void 1438b4151f71SJohn Baldwin start_softintr(void *dummy) 14391931cf94SJohn Baldwin { 1440b4151f71SJohn Baldwin 14418d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 14428d809d50SJeff Roberson panic("died while creating vm swi ithread"); 14431931cf94SJohn Baldwin } 1444237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1445237fdd78SRobert Watson NULL); 14461931cf94SJohn Baldwin 1447d279178dSThomas Moestl /* 1448d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1449d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1450d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1451d279178dSThomas Moestl * independent. 1452d279178dSThomas Moestl * 1453d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1454d279178dSThomas Moestl * calculate things at run time. 1455d279178dSThomas Moestl */ 1456d279178dSThomas Moestl static int 1457d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1458d279178dSThomas Moestl { 1459521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1460d279178dSThomas Moestl } 1461d279178dSThomas Moestl 1462d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1463d279178dSThomas Moestl NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1464d279178dSThomas Moestl 1465d279178dSThomas Moestl static int 1466d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1467d279178dSThomas Moestl { 146885729c2cSJuli Mallett #ifdef SCTL_MASK32 146985729c2cSJuli Mallett uint32_t *intrcnt32; 147085729c2cSJuli Mallett unsigned i; 147185729c2cSJuli Mallett int error; 147285729c2cSJuli Mallett 147385729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 147485729c2cSJuli Mallett if (!req->oldptr) 147585729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 147685729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 147785729c2cSJuli Mallett if (intrcnt32 == NULL) 147885729c2cSJuli Mallett return (ENOMEM); 147985729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 148085729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 148185729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 148285729c2cSJuli Mallett free(intrcnt32, M_TEMP); 148385729c2cSJuli Mallett return (error); 148485729c2cSJuli Mallett } 148585729c2cSJuli Mallett #endif 1486521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1487d279178dSThomas Moestl } 1488d279178dSThomas Moestl 1489d279178dSThomas Moestl SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1490d279178dSThomas Moestl NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 14918b201c42SJohn Baldwin 14928b201c42SJohn Baldwin #ifdef DDB 14938b201c42SJohn Baldwin /* 14948b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 14958b201c42SJohn Baldwin */ 14968b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 14978b201c42SJohn Baldwin { 14988b201c42SJohn Baldwin u_long *i; 14998b201c42SJohn Baldwin char *cp; 1500521ea19dSAttilio Rao u_int j; 15018b201c42SJohn Baldwin 15028b201c42SJohn Baldwin cp = intrnames; 1503521ea19dSAttilio Rao j = 0; 1504521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1505521ea19dSAttilio Rao i++, j++) { 15068b201c42SJohn Baldwin if (*cp == '\0') 15078b201c42SJohn Baldwin break; 15088b201c42SJohn Baldwin if (*i != 0) 15098b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 15108b201c42SJohn Baldwin cp += strlen(cp) + 1; 15118b201c42SJohn Baldwin } 15128b201c42SJohn Baldwin } 15138b201c42SJohn Baldwin #endif 1514