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" 336fa041d7SWojciech Macek #include "opt_hwpmc_hooks.h" 34b7627840SKonstantin Belousov #include "opt_kstack_usage_prof.h" 358b201c42SJohn Baldwin 361c5bb3eaSPeter Wemm #include <sys/param.h> 379a94c9c5SJohn Baldwin #include <sys/bus.h> 38c11110eaSAlfred Perlstein #include <sys/conf.h> 399b33b154SJeff Roberson #include <sys/cpuset.h> 409a94c9c5SJohn Baldwin #include <sys/rtprio.h> 41425f9fdaSStefan Eßer #include <sys/systm.h> 4268352337SDoug Rabson #include <sys/interrupt.h> 431931cf94SJohn Baldwin #include <sys/kernel.h> 441931cf94SJohn Baldwin #include <sys/kthread.h> 451931cf94SJohn Baldwin #include <sys/ktr.h> 4605b2c96fSBruce Evans #include <sys/limits.h> 47f34fa851SJohn Baldwin #include <sys/lock.h> 481931cf94SJohn Baldwin #include <sys/malloc.h> 4935e0e5b3SJohn Baldwin #include <sys/mutex.h> 50cebc7fb1SJohn Baldwin #include <sys/priv.h> 511931cf94SJohn Baldwin #include <sys/proc.h> 52511d1afbSGleb Smirnoff #include <sys/epoch.h> 533e5da754SJohn Baldwin #include <sys/random.h> 54b4151f71SJohn Baldwin #include <sys/resourcevar.h> 5563710c4dSJohn Baldwin #include <sys/sched.h> 56eaf86d16SJohn Baldwin #include <sys/smp.h> 57d279178dSThomas Moestl #include <sys/sysctl.h> 586205924aSKip Macy #include <sys/syslog.h> 591931cf94SJohn Baldwin #include <sys/unistd.h> 601931cf94SJohn Baldwin #include <sys/vmmeter.h> 611931cf94SJohn Baldwin #include <machine/atomic.h> 621931cf94SJohn Baldwin #include <machine/cpu.h> 638088699fSJohn Baldwin #include <machine/md_var.h> 64aba10e13SAlexander Motin #include <machine/smp.h> 65b4151f71SJohn Baldwin #include <machine/stdarg.h> 668b201c42SJohn Baldwin #ifdef DDB 678b201c42SJohn Baldwin #include <ddb/ddb.h> 688b201c42SJohn Baldwin #include <ddb/db_sym.h> 698b201c42SJohn Baldwin #endif 70425f9fdaSStefan Eßer 71e0f66ef8SJohn Baldwin /* 72e0f66ef8SJohn Baldwin * Describe an interrupt thread. There is one of these per interrupt event. 73e0f66ef8SJohn Baldwin */ 74e0f66ef8SJohn Baldwin struct intr_thread { 75e0f66ef8SJohn Baldwin struct intr_event *it_event; 76e0f66ef8SJohn Baldwin struct thread *it_thread; /* Kernel thread. */ 77e0f66ef8SJohn Baldwin int it_flags; /* (j) IT_* flags. */ 78e0f66ef8SJohn Baldwin int it_need; /* Needs service. */ 796fa041d7SWojciech Macek int it_waiting; /* Waiting in the runq. */ 803e5da754SJohn Baldwin }; 813e5da754SJohn Baldwin 82e0f66ef8SJohn Baldwin /* Interrupt thread flags kept in it_flags */ 83e0f66ef8SJohn Baldwin #define IT_DEAD 0x000001 /* Thread is waiting to exit. */ 84e4cd31ddSJeff Roberson #define IT_WAIT 0x000002 /* Thread is waiting for completion. */ 85e0f66ef8SJohn Baldwin 86e0f66ef8SJohn Baldwin struct intr_entropy { 87e0f66ef8SJohn Baldwin struct thread *td; 88e0f66ef8SJohn Baldwin uintptr_t event; 89e0f66ef8SJohn Baldwin }; 90e0f66ef8SJohn Baldwin 91aba10e13SAlexander Motin struct intr_event *clk_intr_event; 92e0f66ef8SJohn Baldwin struct intr_event *tty_intr_event; 937b1fe905SBruce Evans void *vm_ih; 947ab24ea3SJulian Elischer struct proc *intrproc; 951931cf94SJohn Baldwin 96b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 97b4151f71SJohn Baldwin 985d0e8299SConrad Meyer static int intr_storm_threshold = 0; 99af3b2549SHans Petter Selasky SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN, 1007870c3c6SJohn Baldwin &intr_storm_threshold, 0, 1017b1fe905SBruce Evans "Number of consecutive interrupts before storm protection is enabled"); 102511d1afbSGleb Smirnoff static int intr_epoch_batch = 1000; 103511d1afbSGleb Smirnoff SYSCTL_INT(_hw, OID_AUTO, intr_epoch_batch, CTLFLAG_RWTUN, &intr_epoch_batch, 104511d1afbSGleb Smirnoff 0, "Maximum interrupt handler executions without re-entering epoch(9)"); 1056fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 1066fa041d7SWojciech Macek static int intr_hwpmc_waiting_report_threshold = 1; 1076fa041d7SWojciech Macek SYSCTL_INT(_hw, OID_AUTO, intr_hwpmc_waiting_report_threshold, CTLFLAG_RWTUN, 1086fa041d7SWojciech Macek &intr_hwpmc_waiting_report_threshold, 1, 1096fa041d7SWojciech Macek "Threshold for reporting number of events in a workq"); 110*7bc13692SWojciech Macek #define PMC_HOOK_INSTALLED_ANY() __predict_false(pmc_hook != NULL) 1116fa041d7SWojciech Macek #endif 112e0f66ef8SJohn Baldwin static TAILQ_HEAD(, intr_event) event_list = 113e0f66ef8SJohn Baldwin TAILQ_HEAD_INITIALIZER(event_list); 1149b33b154SJeff Roberson static struct mtx event_lock; 1159b33b154SJeff Roberson MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF); 1167b1fe905SBruce Evans 117e0f66ef8SJohn Baldwin static void intr_event_update(struct intr_event *ie); 1186fa041d7SWojciech Macek static int intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame); 119e0f66ef8SJohn Baldwin static struct intr_thread *ithread_create(const char *name); 120e0f66ef8SJohn Baldwin static void ithread_destroy(struct intr_thread *ithread); 121bafe5a31SPaolo Pisati static void ithread_execute_handlers(struct proc *p, 122bafe5a31SPaolo Pisati struct intr_event *ie); 1237b1fe905SBruce Evans static void ithread_loop(void *); 124e0f66ef8SJohn Baldwin static void ithread_update(struct intr_thread *ithd); 1257b1fe905SBruce Evans static void start_softintr(void *); 1267870c3c6SJohn Baldwin 1276fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 1286fa041d7SWojciech Macek #include <sys/pmckern.h> 1296fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, all); 1306fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, ithread); 1316fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, filter); 1326fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, stray); 1336fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, schedule); 1346fa041d7SWojciech Macek PMC_SOFT_DEFINE( , , intr, waiting); 135*7bc13692SWojciech Macek 136*7bc13692SWojciech Macek #define PMC_SOFT_CALL_INTR_HLPR(event, frame) \ 137*7bc13692SWojciech Macek do { \ 138*7bc13692SWojciech Macek if (frame != NULL) \ 139*7bc13692SWojciech Macek PMC_SOFT_CALL_TF( , , intr, event, frame); \ 140*7bc13692SWojciech Macek else \ 141*7bc13692SWojciech Macek PMC_SOFT_CALL( , , intr, event); \ 142*7bc13692SWojciech Macek } while (0) 1436fa041d7SWojciech Macek #endif 1446fa041d7SWojciech Macek 145bc17acb2SJohn Baldwin /* Map an interrupt type to an ithread priority. */ 146b4151f71SJohn Baldwin u_char 147e0f66ef8SJohn Baldwin intr_priority(enum intr_type flags) 1489a94c9c5SJohn Baldwin { 149b4151f71SJohn Baldwin u_char pri; 1509a94c9c5SJohn Baldwin 151b4151f71SJohn Baldwin flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 1525a280d9cSPeter Wemm INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 1539a94c9c5SJohn Baldwin switch (flags) { 154b4151f71SJohn Baldwin case INTR_TYPE_TTY: 155d3305205SJohn Baldwin pri = PI_TTY; 1569a94c9c5SJohn Baldwin break; 1579a94c9c5SJohn Baldwin case INTR_TYPE_BIO: 1589a94c9c5SJohn Baldwin pri = PI_DISK; 1599a94c9c5SJohn Baldwin break; 1609a94c9c5SJohn Baldwin case INTR_TYPE_NET: 1619a94c9c5SJohn Baldwin pri = PI_NET; 1629a94c9c5SJohn Baldwin break; 1639a94c9c5SJohn Baldwin case INTR_TYPE_CAM: 164d3305205SJohn Baldwin pri = PI_DISK; 1659a94c9c5SJohn Baldwin break; 166d3305205SJohn Baldwin case INTR_TYPE_AV: 1675a280d9cSPeter Wemm pri = PI_AV; 1685a280d9cSPeter Wemm break; 169b4151f71SJohn Baldwin case INTR_TYPE_CLK: 170b4151f71SJohn Baldwin pri = PI_REALTIME; 171b4151f71SJohn Baldwin break; 1729a94c9c5SJohn Baldwin case INTR_TYPE_MISC: 1739a94c9c5SJohn Baldwin pri = PI_DULL; /* don't care */ 1749a94c9c5SJohn Baldwin break; 1759a94c9c5SJohn Baldwin default: 176b4151f71SJohn Baldwin /* We didn't specify an interrupt level. */ 177e0f66ef8SJohn Baldwin panic("intr_priority: no interrupt type in flags"); 1789a94c9c5SJohn Baldwin } 1799a94c9c5SJohn Baldwin 1809a94c9c5SJohn Baldwin return pri; 1819a94c9c5SJohn Baldwin } 1829a94c9c5SJohn Baldwin 183b4151f71SJohn Baldwin /* 184e0f66ef8SJohn Baldwin * Update an ithread based on the associated intr_event. 185b4151f71SJohn Baldwin */ 186b4151f71SJohn Baldwin static void 187e0f66ef8SJohn Baldwin ithread_update(struct intr_thread *ithd) 188b4151f71SJohn Baldwin { 189e0f66ef8SJohn Baldwin struct intr_event *ie; 190b40ce416SJulian Elischer struct thread *td; 191e0f66ef8SJohn Baldwin u_char pri; 1928088699fSJohn Baldwin 193e0f66ef8SJohn Baldwin ie = ithd->it_event; 194e0f66ef8SJohn Baldwin td = ithd->it_thread; 195111b043cSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 196b4151f71SJohn Baldwin 197e0f66ef8SJohn Baldwin /* Determine the overall priority of this event. */ 198111b043cSAndriy Gapon if (CK_SLIST_EMPTY(&ie->ie_handlers)) 199e0f66ef8SJohn Baldwin pri = PRI_MAX_ITHD; 200e0f66ef8SJohn Baldwin else 201111b043cSAndriy Gapon pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri; 202e80fb434SRobert Drehmel 203e0f66ef8SJohn Baldwin /* Update name and priority. */ 2047ab24ea3SJulian Elischer strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name)); 20544ad5475SJohn Baldwin #ifdef KTR 20644ad5475SJohn Baldwin sched_clear_tdname(td); 20744ad5475SJohn Baldwin #endif 208982d11f8SJeff Roberson thread_lock(td); 209e0f66ef8SJohn Baldwin sched_prio(td, pri); 210982d11f8SJeff Roberson thread_unlock(td); 211b4151f71SJohn Baldwin } 212e0f66ef8SJohn Baldwin 213e0f66ef8SJohn Baldwin /* 214e0f66ef8SJohn Baldwin * Regenerate the full name of an interrupt event and update its priority. 215e0f66ef8SJohn Baldwin */ 216e0f66ef8SJohn Baldwin static void 217e0f66ef8SJohn Baldwin intr_event_update(struct intr_event *ie) 218e0f66ef8SJohn Baldwin { 219e0f66ef8SJohn Baldwin struct intr_handler *ih; 220e0f66ef8SJohn Baldwin char *last; 221f912e8f2SHans Petter Selasky int missed, space, flags; 222e0f66ef8SJohn Baldwin 223e0f66ef8SJohn Baldwin /* Start off with no entropy and just the name of the event. */ 224e0f66ef8SJohn Baldwin mtx_assert(&ie->ie_lock, MA_OWNED); 225e0f66ef8SJohn Baldwin strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 226f912e8f2SHans Petter Selasky flags = 0; 2270811d60aSJohn Baldwin missed = 0; 228e0f66ef8SJohn Baldwin space = 1; 229e0f66ef8SJohn Baldwin 230e0f66ef8SJohn Baldwin /* Run through all the handlers updating values. */ 231111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 2326d51c0feSIan Lepore if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 < 233e0f66ef8SJohn Baldwin sizeof(ie->ie_fullname)) { 234e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " "); 235e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, ih->ih_name); 236e0f66ef8SJohn Baldwin space = 0; 2370811d60aSJohn Baldwin } else 2380811d60aSJohn Baldwin missed++; 239f912e8f2SHans Petter Selasky flags |= ih->ih_flags; 2400811d60aSJohn Baldwin } 241f912e8f2SHans Petter Selasky ie->ie_hflags = flags; 242e0f66ef8SJohn Baldwin 243e0f66ef8SJohn Baldwin /* 24467da50a0SIan Lepore * If there is only one handler and its name is too long, just copy in 24567da50a0SIan Lepore * as much of the end of the name (includes the unit number) as will 24667da50a0SIan Lepore * fit. Otherwise, we have multiple handlers and not all of the names 24767da50a0SIan Lepore * will fit. Add +'s to indicate missing names. If we run out of room 24867da50a0SIan Lepore * and still have +'s to add, change the last character from a + to a *. 249e0f66ef8SJohn Baldwin */ 25067da50a0SIan Lepore if (missed == 1 && space == 1) { 25167da50a0SIan Lepore ih = CK_SLIST_FIRST(&ie->ie_handlers); 25267da50a0SIan Lepore missed = strlen(ie->ie_fullname) + strlen(ih->ih_name) + 2 - 25367da50a0SIan Lepore sizeof(ie->ie_fullname); 25467da50a0SIan Lepore strcat(ie->ie_fullname, (missed == 0) ? " " : "-"); 25567da50a0SIan Lepore strcat(ie->ie_fullname, &ih->ih_name[missed]); 25667da50a0SIan Lepore missed = 0; 25767da50a0SIan Lepore } 258e0f66ef8SJohn Baldwin last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2]; 2590811d60aSJohn Baldwin while (missed-- > 0) { 260e0f66ef8SJohn Baldwin if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) { 261e0f66ef8SJohn Baldwin if (*last == '+') { 262e0f66ef8SJohn Baldwin *last = '*'; 263e0f66ef8SJohn Baldwin break; 264b4151f71SJohn Baldwin } else 265e0f66ef8SJohn Baldwin *last = '+'; 266e0f66ef8SJohn Baldwin } else if (space) { 267e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, " +"); 268e0f66ef8SJohn Baldwin space = 0; 269e0f66ef8SJohn Baldwin } else 270e0f66ef8SJohn Baldwin strcat(ie->ie_fullname, "+"); 271b4151f71SJohn Baldwin } 272e0f66ef8SJohn Baldwin 273e0f66ef8SJohn Baldwin /* 274e0f66ef8SJohn Baldwin * If this event has an ithread, update it's priority and 275e0f66ef8SJohn Baldwin * name. 276e0f66ef8SJohn Baldwin */ 277e0f66ef8SJohn Baldwin if (ie->ie_thread != NULL) 278e0f66ef8SJohn Baldwin ithread_update(ie->ie_thread); 279e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname); 280b4151f71SJohn Baldwin } 281b4151f71SJohn Baldwin 282b4151f71SJohn Baldwin int 2839b33b154SJeff Roberson intr_event_create(struct intr_event **event, void *source, int flags, int irq, 2841ee1b687SJohn Baldwin void (*pre_ithread)(void *), void (*post_ithread)(void *), 285066da805SAdrian Chadd void (*post_filter)(void *), int (*assign_cpu)(void *, int), 2861ee1b687SJohn Baldwin const char *fmt, ...) 287bafe5a31SPaolo Pisati { 288bafe5a31SPaolo Pisati struct intr_event *ie; 289bafe5a31SPaolo Pisati va_list ap; 290bafe5a31SPaolo Pisati 291bafe5a31SPaolo Pisati /* The only valid flag during creation is IE_SOFT. */ 292bafe5a31SPaolo Pisati if ((flags & ~IE_SOFT) != 0) 293bafe5a31SPaolo Pisati return (EINVAL); 294bafe5a31SPaolo Pisati ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO); 295bafe5a31SPaolo Pisati ie->ie_source = source; 2961ee1b687SJohn Baldwin ie->ie_pre_ithread = pre_ithread; 2971ee1b687SJohn Baldwin ie->ie_post_ithread = post_ithread; 2981ee1b687SJohn Baldwin ie->ie_post_filter = post_filter; 2996d2d1c04SJohn Baldwin ie->ie_assign_cpu = assign_cpu; 300bafe5a31SPaolo Pisati ie->ie_flags = flags; 3019b33b154SJeff Roberson ie->ie_irq = irq; 302eaf86d16SJohn Baldwin ie->ie_cpu = NOCPU; 303111b043cSAndriy Gapon CK_SLIST_INIT(&ie->ie_handlers); 304bafe5a31SPaolo Pisati mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF); 305bafe5a31SPaolo Pisati 306bafe5a31SPaolo Pisati va_start(ap, fmt); 307bafe5a31SPaolo Pisati vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap); 308bafe5a31SPaolo Pisati va_end(ap); 309bafe5a31SPaolo Pisati strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname)); 3109b33b154SJeff Roberson mtx_lock(&event_lock); 311bafe5a31SPaolo Pisati TAILQ_INSERT_TAIL(&event_list, ie, ie_list); 3129b33b154SJeff Roberson mtx_unlock(&event_lock); 313bafe5a31SPaolo Pisati if (event != NULL) 314bafe5a31SPaolo Pisati *event = ie; 315bafe5a31SPaolo Pisati CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name); 316bafe5a31SPaolo Pisati return (0); 317bafe5a31SPaolo Pisati } 318b4151f71SJohn Baldwin 319eaf86d16SJohn Baldwin /* 320eaf86d16SJohn Baldwin * Bind an interrupt event to the specified CPU. Note that not all 321eaf86d16SJohn Baldwin * platforms support binding an interrupt to a CPU. For those 32229dfb631SConrad Meyer * platforms this request will fail. Using a cpu id of NOCPU unbinds 323eaf86d16SJohn Baldwin * the interrupt event. 324eaf86d16SJohn Baldwin */ 32529dfb631SConrad Meyer static int 32629dfb631SConrad Meyer _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread) 327eaf86d16SJohn Baldwin { 3289b33b154SJeff Roberson lwpid_t id; 329eaf86d16SJohn Baldwin int error; 330eaf86d16SJohn Baldwin 331eaf86d16SJohn Baldwin /* Need a CPU to bind to. */ 332eaf86d16SJohn Baldwin if (cpu != NOCPU && CPU_ABSENT(cpu)) 333eaf86d16SJohn Baldwin return (EINVAL); 334eaf86d16SJohn Baldwin 335eaf86d16SJohn Baldwin if (ie->ie_assign_cpu == NULL) 336eaf86d16SJohn Baldwin return (EOPNOTSUPP); 337cebc7fb1SJohn Baldwin 338cebc7fb1SJohn Baldwin error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR); 339cebc7fb1SJohn Baldwin if (error) 340cebc7fb1SJohn Baldwin return (error); 341cebc7fb1SJohn Baldwin 3429b33b154SJeff Roberson /* 343cebc7fb1SJohn Baldwin * If we have any ithreads try to set their mask first to verify 344cebc7fb1SJohn Baldwin * permissions, etc. 3459b33b154SJeff Roberson */ 34629dfb631SConrad Meyer if (bindithread) { 347eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 3489b33b154SJeff Roberson if (ie->ie_thread != NULL) { 3499b33b154SJeff Roberson id = ie->ie_thread->it_thread->td_tid; 350eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 35181198539SAlexander V. Chernikov error = cpuset_setithread(id, cpu); 3529b33b154SJeff Roberson if (error) 3539b33b154SJeff Roberson return (error); 3549b33b154SJeff Roberson } else 355eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 35629dfb631SConrad Meyer } 35729dfb631SConrad Meyer if (bindirq) 358eaf86d16SJohn Baldwin error = ie->ie_assign_cpu(ie->ie_source, cpu); 359cebc7fb1SJohn Baldwin if (error) { 36029dfb631SConrad Meyer if (bindithread) { 361cebc7fb1SJohn Baldwin mtx_lock(&ie->ie_lock); 362cebc7fb1SJohn Baldwin if (ie->ie_thread != NULL) { 36381198539SAlexander V. Chernikov cpu = ie->ie_cpu; 364cebc7fb1SJohn Baldwin id = ie->ie_thread->it_thread->td_tid; 365cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 36681198539SAlexander V. Chernikov (void)cpuset_setithread(id, cpu); 367cebc7fb1SJohn Baldwin } else 368cebc7fb1SJohn Baldwin mtx_unlock(&ie->ie_lock); 36929dfb631SConrad Meyer } 370eaf86d16SJohn Baldwin return (error); 371cebc7fb1SJohn Baldwin } 372cebc7fb1SJohn Baldwin 37329dfb631SConrad Meyer if (bindirq) { 374eaf86d16SJohn Baldwin mtx_lock(&ie->ie_lock); 375eaf86d16SJohn Baldwin ie->ie_cpu = cpu; 3769b33b154SJeff Roberson mtx_unlock(&ie->ie_lock); 37729dfb631SConrad Meyer } 3789b33b154SJeff Roberson 3799b33b154SJeff Roberson return (error); 3809b33b154SJeff Roberson } 3819b33b154SJeff Roberson 38229dfb631SConrad Meyer /* 38329dfb631SConrad Meyer * Bind an interrupt event to the specified CPU. For supported platforms, any 38429dfb631SConrad Meyer * associated ithreads as well as the primary interrupt context will be bound 38529dfb631SConrad Meyer * to the specificed CPU. 38629dfb631SConrad Meyer */ 38729dfb631SConrad Meyer int 38829dfb631SConrad Meyer intr_event_bind(struct intr_event *ie, int cpu) 38929dfb631SConrad Meyer { 39029dfb631SConrad Meyer 39129dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, true)); 39229dfb631SConrad Meyer } 39329dfb631SConrad Meyer 39429dfb631SConrad Meyer /* 39529dfb631SConrad Meyer * Bind an interrupt event to the specified CPU, but do not bind associated 39629dfb631SConrad Meyer * ithreads. 39729dfb631SConrad Meyer */ 39829dfb631SConrad Meyer int 39929dfb631SConrad Meyer intr_event_bind_irqonly(struct intr_event *ie, int cpu) 40029dfb631SConrad Meyer { 40129dfb631SConrad Meyer 40229dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, true, false)); 40329dfb631SConrad Meyer } 40429dfb631SConrad Meyer 40529dfb631SConrad Meyer /* 40629dfb631SConrad Meyer * Bind an interrupt event's ithread to the specified CPU. 40729dfb631SConrad Meyer */ 40829dfb631SConrad Meyer int 40929dfb631SConrad Meyer intr_event_bind_ithread(struct intr_event *ie, int cpu) 41029dfb631SConrad Meyer { 41129dfb631SConrad Meyer 41229dfb631SConrad Meyer return (_intr_event_bind(ie, cpu, false, true)); 41329dfb631SConrad Meyer } 41429dfb631SConrad Meyer 4154e255d74SAndrew Gallatin /* 4164e255d74SAndrew Gallatin * Bind an interrupt event's ithread to the specified cpuset. 4174e255d74SAndrew Gallatin */ 4184e255d74SAndrew Gallatin int 4194e255d74SAndrew Gallatin intr_event_bind_ithread_cpuset(struct intr_event *ie, cpuset_t *cs) 4204e255d74SAndrew Gallatin { 4214e255d74SAndrew Gallatin lwpid_t id; 4224e255d74SAndrew Gallatin 4234e255d74SAndrew Gallatin mtx_lock(&ie->ie_lock); 4244e255d74SAndrew Gallatin if (ie->ie_thread != NULL) { 4254e255d74SAndrew Gallatin id = ie->ie_thread->it_thread->td_tid; 4264e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 4274e255d74SAndrew Gallatin return (cpuset_setthread(id, cs)); 4284e255d74SAndrew Gallatin } else { 4294e255d74SAndrew Gallatin mtx_unlock(&ie->ie_lock); 4304e255d74SAndrew Gallatin } 4314e255d74SAndrew Gallatin return (ENODEV); 4324e255d74SAndrew Gallatin } 4334e255d74SAndrew Gallatin 4349b33b154SJeff Roberson static struct intr_event * 4359b33b154SJeff Roberson intr_lookup(int irq) 4369b33b154SJeff Roberson { 4379b33b154SJeff Roberson struct intr_event *ie; 4389b33b154SJeff Roberson 4399b33b154SJeff Roberson mtx_lock(&event_lock); 4409b33b154SJeff Roberson TAILQ_FOREACH(ie, &event_list, ie_list) 4419b33b154SJeff Roberson if (ie->ie_irq == irq && 4429b33b154SJeff Roberson (ie->ie_flags & IE_SOFT) == 0 && 443111b043cSAndriy Gapon CK_SLIST_FIRST(&ie->ie_handlers) != NULL) 4449b33b154SJeff Roberson break; 4459b33b154SJeff Roberson mtx_unlock(&event_lock); 4469b33b154SJeff Roberson return (ie); 4479b33b154SJeff Roberson } 4489b33b154SJeff Roberson 4499b33b154SJeff Roberson int 45029dfb631SConrad Meyer intr_setaffinity(int irq, int mode, void *m) 4519b33b154SJeff Roberson { 4529b33b154SJeff Roberson struct intr_event *ie; 4539b33b154SJeff Roberson cpuset_t *mask; 4543fe93b94SAdrian Chadd int cpu, n; 4559b33b154SJeff Roberson 4569b33b154SJeff Roberson mask = m; 4579b33b154SJeff Roberson cpu = NOCPU; 4589b33b154SJeff Roberson /* 4599b33b154SJeff Roberson * If we're setting all cpus we can unbind. Otherwise make sure 4609b33b154SJeff Roberson * only one cpu is in the set. 4619b33b154SJeff Roberson */ 4629b33b154SJeff Roberson if (CPU_CMP(cpuset_root, mask)) { 4639b33b154SJeff Roberson for (n = 0; n < CPU_SETSIZE; n++) { 4649b33b154SJeff Roberson if (!CPU_ISSET(n, mask)) 4659b33b154SJeff Roberson continue; 4669b33b154SJeff Roberson if (cpu != NOCPU) 4679b33b154SJeff Roberson return (EINVAL); 4683fe93b94SAdrian Chadd cpu = n; 4699b33b154SJeff Roberson } 4709b33b154SJeff Roberson } 4719b33b154SJeff Roberson ie = intr_lookup(irq); 4729b33b154SJeff Roberson if (ie == NULL) 4739b33b154SJeff Roberson return (ESRCH); 47429dfb631SConrad Meyer switch (mode) { 47529dfb631SConrad Meyer case CPU_WHICH_IRQ: 4769bd55acfSJohn Baldwin return (intr_event_bind(ie, cpu)); 47729dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 47829dfb631SConrad Meyer return (intr_event_bind_irqonly(ie, cpu)); 47929dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 48029dfb631SConrad Meyer return (intr_event_bind_ithread(ie, cpu)); 48129dfb631SConrad Meyer default: 48229dfb631SConrad Meyer return (EINVAL); 48329dfb631SConrad Meyer } 4849b33b154SJeff Roberson } 4859b33b154SJeff Roberson 4869b33b154SJeff Roberson int 48729dfb631SConrad Meyer intr_getaffinity(int irq, int mode, void *m) 4889b33b154SJeff Roberson { 4899b33b154SJeff Roberson struct intr_event *ie; 49029dfb631SConrad Meyer struct thread *td; 49129dfb631SConrad Meyer struct proc *p; 4929b33b154SJeff Roberson cpuset_t *mask; 49329dfb631SConrad Meyer lwpid_t id; 49429dfb631SConrad Meyer int error; 4959b33b154SJeff Roberson 4969b33b154SJeff Roberson mask = m; 4979b33b154SJeff Roberson ie = intr_lookup(irq); 4989b33b154SJeff Roberson if (ie == NULL) 4999b33b154SJeff Roberson return (ESRCH); 50029dfb631SConrad Meyer 50129dfb631SConrad Meyer error = 0; 5029b33b154SJeff Roberson CPU_ZERO(mask); 50329dfb631SConrad Meyer switch (mode) { 50429dfb631SConrad Meyer case CPU_WHICH_IRQ: 50529dfb631SConrad Meyer case CPU_WHICH_INTRHANDLER: 5069b33b154SJeff Roberson mtx_lock(&ie->ie_lock); 5079b33b154SJeff Roberson if (ie->ie_cpu == NOCPU) 5089b33b154SJeff Roberson CPU_COPY(cpuset_root, mask); 5099b33b154SJeff Roberson else 5109b33b154SJeff Roberson CPU_SET(ie->ie_cpu, mask); 511eaf86d16SJohn Baldwin mtx_unlock(&ie->ie_lock); 51229dfb631SConrad Meyer break; 51329dfb631SConrad Meyer case CPU_WHICH_ITHREAD: 51429dfb631SConrad Meyer mtx_lock(&ie->ie_lock); 51529dfb631SConrad Meyer if (ie->ie_thread == NULL) { 51629dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 51729dfb631SConrad Meyer CPU_COPY(cpuset_root, mask); 51829dfb631SConrad Meyer } else { 51929dfb631SConrad Meyer id = ie->ie_thread->it_thread->td_tid; 52029dfb631SConrad Meyer mtx_unlock(&ie->ie_lock); 52129dfb631SConrad Meyer error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL); 52229dfb631SConrad Meyer if (error != 0) 52329dfb631SConrad Meyer return (error); 52429dfb631SConrad Meyer CPU_COPY(&td->td_cpuset->cs_mask, mask); 52529dfb631SConrad Meyer PROC_UNLOCK(p); 52629dfb631SConrad Meyer } 52729dfb631SConrad Meyer default: 52829dfb631SConrad Meyer return (EINVAL); 52929dfb631SConrad Meyer } 530eaf86d16SJohn Baldwin return (0); 531eaf86d16SJohn Baldwin } 532eaf86d16SJohn Baldwin 533b4151f71SJohn Baldwin int 534e0f66ef8SJohn Baldwin intr_event_destroy(struct intr_event *ie) 535b4151f71SJohn Baldwin { 536b4151f71SJohn Baldwin 5379b33b154SJeff Roberson mtx_lock(&event_lock); 538e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 539111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 540e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5419b33b154SJeff Roberson mtx_unlock(&event_lock); 542e0f66ef8SJohn Baldwin return (EBUSY); 5434d29cb2dSJohn Baldwin } 544e0f66ef8SJohn Baldwin TAILQ_REMOVE(&event_list, ie, ie_list); 5459477358dSJohn Baldwin #ifndef notyet 5469477358dSJohn Baldwin if (ie->ie_thread != NULL) { 5479477358dSJohn Baldwin ithread_destroy(ie->ie_thread); 5489477358dSJohn Baldwin ie->ie_thread = NULL; 5499477358dSJohn Baldwin } 5509477358dSJohn Baldwin #endif 551e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 5529b33b154SJeff Roberson mtx_unlock(&event_lock); 553e0f66ef8SJohn Baldwin mtx_destroy(&ie->ie_lock); 554e0f66ef8SJohn Baldwin free(ie, M_ITHREAD); 555e0f66ef8SJohn Baldwin return (0); 556e0f66ef8SJohn Baldwin } 557e0f66ef8SJohn Baldwin 558e0f66ef8SJohn Baldwin static struct intr_thread * 559e0f66ef8SJohn Baldwin ithread_create(const char *name) 560e0f66ef8SJohn Baldwin { 561e0f66ef8SJohn Baldwin struct intr_thread *ithd; 562e0f66ef8SJohn Baldwin struct thread *td; 563e0f66ef8SJohn Baldwin int error; 564e0f66ef8SJohn Baldwin 565e0f66ef8SJohn Baldwin ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO); 566e0f66ef8SJohn Baldwin 5677ab24ea3SJulian Elischer error = kproc_kthread_add(ithread_loop, ithd, &intrproc, 5687ab24ea3SJulian Elischer &td, RFSTOPPED | RFHIGHPID, 5699ef95d01SJulian Elischer 0, "intr", "%s", name); 570e0f66ef8SJohn Baldwin if (error) 5713745c395SJulian Elischer panic("kproc_create() failed with %d", error); 572982d11f8SJeff Roberson thread_lock(td); 573ad1e7d28SJulian Elischer sched_class(td, PRI_ITHD); 574e0f66ef8SJohn Baldwin TD_SET_IWAIT(td); 575982d11f8SJeff Roberson thread_unlock(td); 576e0f66ef8SJohn Baldwin td->td_pflags |= TDP_ITHREAD; 577e0f66ef8SJohn Baldwin ithd->it_thread = td; 578e0f66ef8SJohn Baldwin CTR2(KTR_INTR, "%s: created %s", __func__, name); 579e0f66ef8SJohn Baldwin return (ithd); 580e0f66ef8SJohn Baldwin } 581e0f66ef8SJohn Baldwin 582e0f66ef8SJohn Baldwin static void 583e0f66ef8SJohn Baldwin ithread_destroy(struct intr_thread *ithread) 584e0f66ef8SJohn Baldwin { 585e0f66ef8SJohn Baldwin struct thread *td; 586e0f66ef8SJohn Baldwin 587bb141be1SScott Long CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name); 588e0f66ef8SJohn Baldwin td = ithread->it_thread; 589982d11f8SJeff Roberson thread_lock(td); 590e0f66ef8SJohn Baldwin ithread->it_flags |= IT_DEAD; 59171fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 59271fad9fdSJulian Elischer TD_CLR_IWAIT(td); 593f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 59461a74c5cSJeff Roberson } else 595982d11f8SJeff Roberson thread_unlock(td); 596b4151f71SJohn Baldwin } 597b4151f71SJohn Baldwin 598b4151f71SJohn Baldwin int 599e0f66ef8SJohn Baldwin intr_event_add_handler(struct intr_event *ie, const char *name, 600ef544f63SPaolo Pisati driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri, 601ef544f63SPaolo Pisati enum intr_type flags, void **cookiep) 602b4151f71SJohn Baldwin { 603e0f66ef8SJohn Baldwin struct intr_handler *ih, *temp_ih; 604111b043cSAndriy Gapon struct intr_handler **prevptr; 605e0f66ef8SJohn Baldwin struct intr_thread *it; 606b4151f71SJohn Baldwin 607ef544f63SPaolo Pisati if (ie == NULL || name == NULL || (handler == NULL && filter == NULL)) 608b4151f71SJohn Baldwin return (EINVAL); 609b4151f71SJohn Baldwin 610e0f66ef8SJohn Baldwin /* Allocate and populate an interrupt handler structure. */ 611e0f66ef8SJohn Baldwin ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO); 612ef544f63SPaolo Pisati ih->ih_filter = filter; 613b4151f71SJohn Baldwin ih->ih_handler = handler; 614b4151f71SJohn Baldwin ih->ih_argument = arg; 61537b8ef16SJohn Baldwin strlcpy(ih->ih_name, name, sizeof(ih->ih_name)); 616e0f66ef8SJohn Baldwin ih->ih_event = ie; 617b4151f71SJohn Baldwin ih->ih_pri = pri; 618ef544f63SPaolo Pisati if (flags & INTR_EXCL) 619b4151f71SJohn Baldwin ih->ih_flags = IH_EXCLUSIVE; 620b4151f71SJohn Baldwin if (flags & INTR_MPSAFE) 621b4151f71SJohn Baldwin ih->ih_flags |= IH_MPSAFE; 622b4151f71SJohn Baldwin if (flags & INTR_ENTROPY) 623b4151f71SJohn Baldwin ih->ih_flags |= IH_ENTROPY; 624511d1afbSGleb Smirnoff if (flags & INTR_TYPE_NET) 625511d1afbSGleb Smirnoff ih->ih_flags |= IH_NET; 626b4151f71SJohn Baldwin 627e0f66ef8SJohn Baldwin /* We can only have one exclusive handler in a event. */ 628e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 629111b043cSAndriy Gapon if (!CK_SLIST_EMPTY(&ie->ie_handlers)) { 630e0f66ef8SJohn Baldwin if ((flags & INTR_EXCL) || 631111b043cSAndriy Gapon (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) { 632e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 633b4151f71SJohn Baldwin free(ih, M_ITHREAD); 634b4151f71SJohn Baldwin return (EINVAL); 635b4151f71SJohn Baldwin } 636e0f66ef8SJohn Baldwin } 637e0f66ef8SJohn Baldwin 638e0f66ef8SJohn Baldwin /* Create a thread if we need one. */ 639ef544f63SPaolo Pisati while (ie->ie_thread == NULL && handler != NULL) { 640e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) 6410f180a7cSJohn Baldwin msleep(ie, &ie->ie_lock, 0, "ithread", 0); 642e0f66ef8SJohn Baldwin else { 643e0f66ef8SJohn Baldwin ie->ie_flags |= IE_ADDING_THREAD; 644e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 645e0f66ef8SJohn Baldwin it = ithread_create("intr: newborn"); 646e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 647e0f66ef8SJohn Baldwin ie->ie_flags &= ~IE_ADDING_THREAD; 648e0f66ef8SJohn Baldwin ie->ie_thread = it; 649e0f66ef8SJohn Baldwin it->it_event = ie; 650e0f66ef8SJohn Baldwin ithread_update(it); 651e0f66ef8SJohn Baldwin wakeup(ie); 652e0f66ef8SJohn Baldwin } 653e0f66ef8SJohn Baldwin } 654c9516c94SAlexander Kabaev 655c9516c94SAlexander Kabaev /* Add the new handler to the event in priority order. */ 656111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) { 657c9516c94SAlexander Kabaev if (temp_ih->ih_pri > ih->ih_pri) 658c9516c94SAlexander Kabaev break; 659c9516c94SAlexander Kabaev } 660111b043cSAndriy Gapon CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next); 661111b043cSAndriy Gapon 662c9516c94SAlexander Kabaev intr_event_update(ie); 663c9516c94SAlexander Kabaev 664e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 665e0f66ef8SJohn Baldwin ie->ie_name); 666e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 667e0f66ef8SJohn Baldwin 668e0f66ef8SJohn Baldwin if (cookiep != NULL) 669e0f66ef8SJohn Baldwin *cookiep = ih; 670e0f66ef8SJohn Baldwin return (0); 671e0f66ef8SJohn Baldwin } 672b4151f71SJohn Baldwin 673c3045318SJohn Baldwin /* 67437b8ef16SJohn Baldwin * Append a description preceded by a ':' to the name of the specified 67537b8ef16SJohn Baldwin * interrupt handler. 67637b8ef16SJohn Baldwin */ 67737b8ef16SJohn Baldwin int 67837b8ef16SJohn Baldwin intr_event_describe_handler(struct intr_event *ie, void *cookie, 67937b8ef16SJohn Baldwin const char *descr) 68037b8ef16SJohn Baldwin { 68137b8ef16SJohn Baldwin struct intr_handler *ih; 68237b8ef16SJohn Baldwin size_t space; 68337b8ef16SJohn Baldwin char *start; 68437b8ef16SJohn Baldwin 68537b8ef16SJohn Baldwin mtx_lock(&ie->ie_lock); 68637b8ef16SJohn Baldwin #ifdef INVARIANTS 687111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 68837b8ef16SJohn Baldwin if (ih == cookie) 68937b8ef16SJohn Baldwin break; 69037b8ef16SJohn Baldwin } 69137b8ef16SJohn Baldwin if (ih == NULL) { 69237b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 693d0c9a291SJohn Baldwin panic("handler %p not found in interrupt event %p", cookie, ie); 69437b8ef16SJohn Baldwin } 69537b8ef16SJohn Baldwin #endif 69637b8ef16SJohn Baldwin ih = cookie; 69737b8ef16SJohn Baldwin 69837b8ef16SJohn Baldwin /* 69937b8ef16SJohn Baldwin * Look for an existing description by checking for an 70037b8ef16SJohn Baldwin * existing ":". This assumes device names do not include 70137b8ef16SJohn Baldwin * colons. If one is found, prepare to insert the new 70237b8ef16SJohn Baldwin * description at that point. If one is not found, find the 70337b8ef16SJohn Baldwin * end of the name to use as the insertion point. 70437b8ef16SJohn Baldwin */ 705dc15eac0SEd Schouten start = strchr(ih->ih_name, ':'); 70637b8ef16SJohn Baldwin if (start == NULL) 707dc15eac0SEd Schouten start = strchr(ih->ih_name, 0); 70837b8ef16SJohn Baldwin 70937b8ef16SJohn Baldwin /* 71037b8ef16SJohn Baldwin * See if there is enough remaining room in the string for the 71137b8ef16SJohn Baldwin * description + ":". The "- 1" leaves room for the trailing 71237b8ef16SJohn Baldwin * '\0'. The "+ 1" accounts for the colon. 71337b8ef16SJohn Baldwin */ 71437b8ef16SJohn Baldwin space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1; 71537b8ef16SJohn Baldwin if (strlen(descr) + 1 > space) { 71637b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 71737b8ef16SJohn Baldwin return (ENOSPC); 71837b8ef16SJohn Baldwin } 71937b8ef16SJohn Baldwin 72037b8ef16SJohn Baldwin /* Append a colon followed by the description. */ 72137b8ef16SJohn Baldwin *start = ':'; 72237b8ef16SJohn Baldwin strcpy(start + 1, descr); 72337b8ef16SJohn Baldwin intr_event_update(ie); 72437b8ef16SJohn Baldwin mtx_unlock(&ie->ie_lock); 72537b8ef16SJohn Baldwin return (0); 72637b8ef16SJohn Baldwin } 72737b8ef16SJohn Baldwin 72837b8ef16SJohn Baldwin /* 729c3045318SJohn Baldwin * Return the ie_source field from the intr_event an intr_handler is 730c3045318SJohn Baldwin * associated with. 731c3045318SJohn Baldwin */ 732c3045318SJohn Baldwin void * 733c3045318SJohn Baldwin intr_handler_source(void *cookie) 734c3045318SJohn Baldwin { 735c3045318SJohn Baldwin struct intr_handler *ih; 736c3045318SJohn Baldwin struct intr_event *ie; 737c3045318SJohn Baldwin 738c3045318SJohn Baldwin ih = (struct intr_handler *)cookie; 739c3045318SJohn Baldwin if (ih == NULL) 740c3045318SJohn Baldwin return (NULL); 741c3045318SJohn Baldwin ie = ih->ih_event; 742c3045318SJohn Baldwin KASSERT(ie != NULL, 743c3045318SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 744c3045318SJohn Baldwin ih->ih_name)); 745c3045318SJohn Baldwin return (ie->ie_source); 746c3045318SJohn Baldwin } 747c3045318SJohn Baldwin 748e4cd31ddSJeff Roberson /* 749e0fa977eSAndriy Gapon * If intr_event_handle() is running in the ISR context at the time of the call, 750e0fa977eSAndriy Gapon * then wait for it to complete. 751e0fa977eSAndriy Gapon */ 752e0fa977eSAndriy Gapon static void 753e0fa977eSAndriy Gapon intr_event_barrier(struct intr_event *ie) 754e0fa977eSAndriy Gapon { 755e0fa977eSAndriy Gapon int phase; 756e0fa977eSAndriy Gapon 757e0fa977eSAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 758e0fa977eSAndriy Gapon phase = ie->ie_phase; 759e0fa977eSAndriy Gapon 760e0fa977eSAndriy Gapon /* 761e0fa977eSAndriy Gapon * Switch phase to direct future interrupts to the other active counter. 762e0fa977eSAndriy Gapon * Make sure that any preceding stores are visible before the switch. 763e0fa977eSAndriy Gapon */ 764e0fa977eSAndriy Gapon KASSERT(ie->ie_active[!phase] == 0, ("idle phase has activity")); 765e0fa977eSAndriy Gapon atomic_store_rel_int(&ie->ie_phase, !phase); 766e0fa977eSAndriy Gapon 767e0fa977eSAndriy Gapon /* 768e0fa977eSAndriy Gapon * This code cooperates with wait-free iteration of ie_handlers 769e0fa977eSAndriy Gapon * in intr_event_handle. 770e0fa977eSAndriy Gapon * Make sure that the removal and the phase update are not reordered 771e0fa977eSAndriy Gapon * with the active count check. 772e0fa977eSAndriy Gapon * Note that no combination of acquire and release fences can provide 773e0fa977eSAndriy Gapon * that guarantee as Store->Load sequences can always be reordered. 774e0fa977eSAndriy Gapon */ 775e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 776e0fa977eSAndriy Gapon 777e0fa977eSAndriy Gapon /* 778e0fa977eSAndriy Gapon * Now wait on the inactive phase. 779e0fa977eSAndriy Gapon * The acquire fence is needed so that that all post-barrier accesses 780e0fa977eSAndriy Gapon * are after the check. 781e0fa977eSAndriy Gapon */ 782e0fa977eSAndriy Gapon while (ie->ie_active[phase] > 0) 783e0fa977eSAndriy Gapon cpu_spinwait(); 784e0fa977eSAndriy Gapon atomic_thread_fence_acq(); 785e0fa977eSAndriy Gapon } 786e0fa977eSAndriy Gapon 78782a5a275SAndriy Gapon static void 78882a5a275SAndriy Gapon intr_handler_barrier(struct intr_handler *handler) 78982a5a275SAndriy Gapon { 79082a5a275SAndriy Gapon struct intr_event *ie; 79182a5a275SAndriy Gapon 79282a5a275SAndriy Gapon ie = handler->ih_event; 79382a5a275SAndriy Gapon mtx_assert(&ie->ie_lock, MA_OWNED); 79482a5a275SAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 79582a5a275SAndriy Gapon ("update for a removed handler")); 79682a5a275SAndriy Gapon 79782a5a275SAndriy Gapon if (ie->ie_thread == NULL) { 79882a5a275SAndriy Gapon intr_event_barrier(ie); 79982a5a275SAndriy Gapon return; 80082a5a275SAndriy Gapon } 80182a5a275SAndriy Gapon if ((handler->ih_flags & IH_CHANGED) == 0) { 80282a5a275SAndriy Gapon handler->ih_flags |= IH_CHANGED; 8036fa041d7SWojciech Macek intr_event_schedule_thread(ie, NULL); 80482a5a275SAndriy Gapon } 80582a5a275SAndriy Gapon while ((handler->ih_flags & IH_CHANGED) != 0) 80682a5a275SAndriy Gapon msleep(handler, &ie->ie_lock, 0, "ih_barr", 0); 80782a5a275SAndriy Gapon } 80882a5a275SAndriy Gapon 809e0fa977eSAndriy Gapon /* 810e4cd31ddSJeff Roberson * Sleep until an ithread finishes executing an interrupt handler. 811e4cd31ddSJeff Roberson * 812e4cd31ddSJeff Roberson * XXX Doesn't currently handle interrupt filters or fast interrupt 8136eb60f5bSHans Petter Selasky * handlers. This is intended for LinuxKPI drivers only. 8146eb60f5bSHans Petter Selasky * Do not use in BSD code. 815e4cd31ddSJeff Roberson */ 816e4cd31ddSJeff Roberson void 817e4cd31ddSJeff Roberson _intr_drain(int irq) 818e4cd31ddSJeff Roberson { 819e4cd31ddSJeff Roberson struct intr_event *ie; 820e4cd31ddSJeff Roberson struct intr_thread *ithd; 821e4cd31ddSJeff Roberson struct thread *td; 822e4cd31ddSJeff Roberson 823e4cd31ddSJeff Roberson ie = intr_lookup(irq); 824e4cd31ddSJeff Roberson if (ie == NULL) 825e4cd31ddSJeff Roberson return; 826e4cd31ddSJeff Roberson if (ie->ie_thread == NULL) 827e4cd31ddSJeff Roberson return; 828e4cd31ddSJeff Roberson ithd = ie->ie_thread; 829e4cd31ddSJeff Roberson td = ithd->it_thread; 8305bd186a6SJeff Roberson /* 8315bd186a6SJeff Roberson * We set the flag and wait for it to be cleared to avoid 8325bd186a6SJeff Roberson * long delays with potentially busy interrupt handlers 8335bd186a6SJeff Roberson * were we to only sample TD_AWAITING_INTR() every tick. 8345bd186a6SJeff Roberson */ 835e4cd31ddSJeff Roberson thread_lock(td); 836e4cd31ddSJeff Roberson if (!TD_AWAITING_INTR(td)) { 837e4cd31ddSJeff Roberson ithd->it_flags |= IT_WAIT; 8385bd186a6SJeff Roberson while (ithd->it_flags & IT_WAIT) { 8395bd186a6SJeff Roberson thread_unlock(td); 8405bd186a6SJeff Roberson pause("idrain", 1); 8415bd186a6SJeff Roberson thread_lock(td); 842e4cd31ddSJeff Roberson } 8435bd186a6SJeff Roberson } 8445bd186a6SJeff Roberson thread_unlock(td); 845e4cd31ddSJeff Roberson return; 846e4cd31ddSJeff Roberson } 847e4cd31ddSJeff Roberson 848b4151f71SJohn Baldwin int 849e0f66ef8SJohn Baldwin intr_event_remove_handler(void *cookie) 850b4151f71SJohn Baldwin { 851e0f66ef8SJohn Baldwin struct intr_handler *handler = (struct intr_handler *)cookie; 852e0f66ef8SJohn Baldwin struct intr_event *ie; 853e0f66ef8SJohn Baldwin struct intr_handler *ih; 854111b043cSAndriy Gapon struct intr_handler **prevptr; 855e0f66ef8SJohn Baldwin #ifdef notyet 856e0f66ef8SJohn Baldwin int dead; 857b4151f71SJohn Baldwin #endif 858b4151f71SJohn Baldwin 8593e5da754SJohn Baldwin if (handler == NULL) 860b4151f71SJohn Baldwin return (EINVAL); 861e0f66ef8SJohn Baldwin ie = handler->ih_event; 862e0f66ef8SJohn Baldwin KASSERT(ie != NULL, 863e0f66ef8SJohn Baldwin ("interrupt handler \"%s\" has a NULL interrupt event", 8643e5da754SJohn Baldwin handler->ih_name)); 865111b043cSAndriy Gapon 866e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 86791f91617SDavid E. O'Brien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 868e0f66ef8SJohn Baldwin ie->ie_name); 869111b043cSAndriy Gapon CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) { 8703e5da754SJohn Baldwin if (ih == handler) 871111b043cSAndriy Gapon break; 872111b043cSAndriy Gapon } 873111b043cSAndriy Gapon if (ih == NULL) { 874111b043cSAndriy Gapon panic("interrupt handler \"%s\" not found in " 875111b043cSAndriy Gapon "interrupt event \"%s\"", handler->ih_name, ie->ie_name); 876111b043cSAndriy Gapon } 877111b043cSAndriy Gapon 878de271f01SJohn Baldwin /* 879e0fa977eSAndriy Gapon * If there is no ithread, then directly remove the handler. Note that 880e0fa977eSAndriy Gapon * intr_event_handle() iterates ie_handlers in a lock-less fashion, so 881e0fa977eSAndriy Gapon * care needs to be taken to keep ie_handlers consistent and to free 882e0fa977eSAndriy Gapon * the removed handler only when ie_handlers is quiescent. 883e0f66ef8SJohn Baldwin */ 884e0f66ef8SJohn Baldwin if (ie->ie_thread == NULL) { 885111b043cSAndriy Gapon CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next); 886e0fa977eSAndriy Gapon intr_event_barrier(ie); 887e0fa977eSAndriy Gapon intr_event_update(ie); 888e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 889e0f66ef8SJohn Baldwin free(handler, M_ITHREAD); 890e0f66ef8SJohn Baldwin return (0); 891e0f66ef8SJohn Baldwin } 892e0f66ef8SJohn Baldwin 893e0f66ef8SJohn Baldwin /* 894e0fa977eSAndriy Gapon * Let the interrupt thread do the job. 895e0fa977eSAndriy Gapon * The interrupt source is disabled when the interrupt thread is 896e0fa977eSAndriy Gapon * running, so it does not have to worry about interaction with 897e0fa977eSAndriy Gapon * intr_event_handle(). 898de271f01SJohn Baldwin */ 899e0fa977eSAndriy Gapon KASSERT((handler->ih_flags & IH_DEAD) == 0, 900e0fa977eSAndriy Gapon ("duplicate handle remove")); 901de271f01SJohn Baldwin handler->ih_flags |= IH_DEAD; 9026fa041d7SWojciech Macek intr_event_schedule_thread(ie, NULL); 903e0f66ef8SJohn Baldwin while (handler->ih_flags & IH_DEAD) 9040f180a7cSJohn Baldwin msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0); 905e0f66ef8SJohn Baldwin intr_event_update(ie); 906111b043cSAndriy Gapon 907e0f66ef8SJohn Baldwin #ifdef notyet 908e0f66ef8SJohn Baldwin /* 909e0f66ef8SJohn Baldwin * XXX: This could be bad in the case of ppbus(8). Also, I think 910e0f66ef8SJohn Baldwin * this could lead to races of stale data when servicing an 911e0f66ef8SJohn Baldwin * interrupt. 912e0f66ef8SJohn Baldwin */ 913e0f66ef8SJohn Baldwin dead = 1; 914111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 915111b043cSAndriy Gapon if (ih->ih_handler != NULL) { 916e0f66ef8SJohn Baldwin dead = 0; 917e0f66ef8SJohn Baldwin break; 918e0f66ef8SJohn Baldwin } 919e0f66ef8SJohn Baldwin } 920e0f66ef8SJohn Baldwin if (dead) { 921e0f66ef8SJohn Baldwin ithread_destroy(ie->ie_thread); 922e0f66ef8SJohn Baldwin ie->ie_thread = NULL; 923e0f66ef8SJohn Baldwin } 924e0f66ef8SJohn Baldwin #endif 925e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 926b4151f71SJohn Baldwin free(handler, M_ITHREAD); 927b4151f71SJohn Baldwin return (0); 928b4151f71SJohn Baldwin } 929b4151f71SJohn Baldwin 93082a5a275SAndriy Gapon int 93182a5a275SAndriy Gapon intr_event_suspend_handler(void *cookie) 93282a5a275SAndriy Gapon { 93382a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 93482a5a275SAndriy Gapon struct intr_event *ie; 93582a5a275SAndriy Gapon 93682a5a275SAndriy Gapon if (handler == NULL) 93782a5a275SAndriy Gapon return (EINVAL); 93882a5a275SAndriy Gapon ie = handler->ih_event; 93982a5a275SAndriy Gapon KASSERT(ie != NULL, 94082a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 94182a5a275SAndriy Gapon handler->ih_name)); 94282a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 94382a5a275SAndriy Gapon handler->ih_flags |= IH_SUSP; 94482a5a275SAndriy Gapon intr_handler_barrier(handler); 94582a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 94682a5a275SAndriy Gapon return (0); 94782a5a275SAndriy Gapon } 94882a5a275SAndriy Gapon 94982a5a275SAndriy Gapon int 95082a5a275SAndriy Gapon intr_event_resume_handler(void *cookie) 95182a5a275SAndriy Gapon { 95282a5a275SAndriy Gapon struct intr_handler *handler = (struct intr_handler *)cookie; 95382a5a275SAndriy Gapon struct intr_event *ie; 95482a5a275SAndriy Gapon 95582a5a275SAndriy Gapon if (handler == NULL) 95682a5a275SAndriy Gapon return (EINVAL); 95782a5a275SAndriy Gapon ie = handler->ih_event; 95882a5a275SAndriy Gapon KASSERT(ie != NULL, 95982a5a275SAndriy Gapon ("interrupt handler \"%s\" has a NULL interrupt event", 96082a5a275SAndriy Gapon handler->ih_name)); 96182a5a275SAndriy Gapon 96282a5a275SAndriy Gapon /* 96382a5a275SAndriy Gapon * intr_handler_barrier() acts not only as a barrier, 96482a5a275SAndriy Gapon * it also allows to check for any pending interrupts. 96582a5a275SAndriy Gapon */ 96682a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 96782a5a275SAndriy Gapon handler->ih_flags &= ~IH_SUSP; 96882a5a275SAndriy Gapon intr_handler_barrier(handler); 96982a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 97082a5a275SAndriy Gapon return (0); 97182a5a275SAndriy Gapon } 97282a5a275SAndriy Gapon 9731ee1b687SJohn Baldwin static int 9746fa041d7SWojciech Macek intr_event_schedule_thread(struct intr_event *ie, struct trapframe *frame) 9753e5da754SJohn Baldwin { 976e0f66ef8SJohn Baldwin struct intr_entropy entropy; 977e0f66ef8SJohn Baldwin struct intr_thread *it; 978b40ce416SJulian Elischer struct thread *td; 97904774f23SJulian Elischer struct thread *ctd; 9803e5da754SJohn Baldwin 9813e5da754SJohn Baldwin /* 9823e5da754SJohn Baldwin * If no ithread or no handlers, then we have a stray interrupt. 9833e5da754SJohn Baldwin */ 984111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) || 985e0f66ef8SJohn Baldwin ie->ie_thread == NULL) 9863e5da754SJohn Baldwin return (EINVAL); 9873e5da754SJohn Baldwin 98804774f23SJulian Elischer ctd = curthread; 989e0f66ef8SJohn Baldwin it = ie->ie_thread; 990e0f66ef8SJohn Baldwin td = it->it_thread; 991e0f66ef8SJohn Baldwin 9923e5da754SJohn Baldwin /* 9933e5da754SJohn Baldwin * If any of the handlers for this ithread claim to be good 9943e5da754SJohn Baldwin * sources of entropy, then gather some. 9953e5da754SJohn Baldwin */ 996c4eb6630SGleb Smirnoff if (ie->ie_hflags & IH_ENTROPY) { 997e0f66ef8SJohn Baldwin entropy.event = (uintptr_t)ie; 998e0f66ef8SJohn Baldwin entropy.td = ctd; 99919fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_INTERRUPT); 10003e5da754SJohn Baldwin } 10013e5da754SJohn Baldwin 1002ba3f7276SMatt Macy KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name)); 10033e5da754SJohn Baldwin 10043e5da754SJohn Baldwin /* 10053e5da754SJohn Baldwin * Set it_need to tell the thread to keep running if it is already 1006982d11f8SJeff Roberson * running. Then, lock the thread and see if we actually need to 1007982d11f8SJeff Roberson * put it on the runqueue. 1008283dfee9SKonstantin Belousov * 1009283dfee9SKonstantin Belousov * Use store_rel to arrange that the store to ih_need in 1010283dfee9SKonstantin Belousov * swi_sched() is before the store to it_need and prepare for 1011283dfee9SKonstantin Belousov * transfer of this order to loads in the ithread. 10123e5da754SJohn Baldwin */ 10133eebd44dSAlfred Perlstein atomic_store_rel_int(&it->it_need, 1); 1014982d11f8SJeff Roberson thread_lock(td); 101571fad9fdSJulian Elischer if (TD_AWAITING_INTR(td)) { 10166fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 1017*7bc13692SWojciech Macek it->it_waiting = 0; 1018*7bc13692SWojciech Macek if (PMC_HOOK_INSTALLED_ANY()) 1019*7bc13692SWojciech Macek PMC_SOFT_CALL_INTR_HLPR(schedule, frame); 10206fa041d7SWojciech Macek #endif 1021fc2e87beSMatt Macy CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid, 10227ab24ea3SJulian Elischer td->td_name); 102371fad9fdSJulian Elischer TD_CLR_IWAIT(td); 1024f0393f06SJeff Roberson sched_add(td, SRQ_INTR); 10253e5da754SJohn Baldwin } else { 10266fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 1027*7bc13692SWojciech Macek it->it_waiting++; 1028*7bc13692SWojciech Macek if (PMC_HOOK_INSTALLED_ANY() && 1029*7bc13692SWojciech Macek (it->it_waiting >= intr_hwpmc_waiting_report_threshold)) 1030*7bc13692SWojciech Macek PMC_SOFT_CALL_INTR_HLPR(waiting, frame); 10316fa041d7SWojciech Macek #endif 1032e0f66ef8SJohn Baldwin CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d", 1033fa2528acSAlex Richardson __func__, td->td_proc->p_pid, td->td_name, it->it_need, TD_GET_STATE(td)); 1034982d11f8SJeff Roberson thread_unlock(td); 103561a74c5cSJeff Roberson } 10363e5da754SJohn Baldwin 10373e5da754SJohn Baldwin return (0); 10383e5da754SJohn Baldwin } 10393e5da754SJohn Baldwin 1040fe486a37SJohn Baldwin /* 1041e84bcd84SRobert Watson * Allow interrupt event binding for software interrupt handlers -- a no-op, 1042e84bcd84SRobert Watson * since interrupts are generated in software rather than being directed by 1043e84bcd84SRobert Watson * a PIC. 1044e84bcd84SRobert Watson */ 1045e84bcd84SRobert Watson static int 1046066da805SAdrian Chadd swi_assign_cpu(void *arg, int cpu) 1047e84bcd84SRobert Watson { 1048e84bcd84SRobert Watson 1049e84bcd84SRobert Watson return (0); 1050e84bcd84SRobert Watson } 1051e84bcd84SRobert Watson 1052e84bcd84SRobert Watson /* 1053fe486a37SJohn Baldwin * Add a software interrupt handler to a specified event. If a given event 1054fe486a37SJohn Baldwin * is not specified, then a new event is created. 1055fe486a37SJohn Baldwin */ 10563e5da754SJohn Baldwin int 1057e0f66ef8SJohn Baldwin swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler, 1058b4151f71SJohn Baldwin void *arg, int pri, enum intr_type flags, void **cookiep) 10598088699fSJohn Baldwin { 1060e0f66ef8SJohn Baldwin struct intr_event *ie; 1061aba10e13SAlexander Motin int error = 0; 10628088699fSJohn Baldwin 1063bafe5a31SPaolo Pisati if (flags & INTR_ENTROPY) 10643e5da754SJohn Baldwin return (EINVAL); 10653e5da754SJohn Baldwin 1066e0f66ef8SJohn Baldwin ie = (eventp != NULL) ? *eventp : NULL; 10678088699fSJohn Baldwin 1068e0f66ef8SJohn Baldwin if (ie != NULL) { 1069e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 10703e5da754SJohn Baldwin return (EINVAL); 10713e5da754SJohn Baldwin } else { 10729b33b154SJeff Roberson error = intr_event_create(&ie, NULL, IE_SOFT, 0, 1073e84bcd84SRobert Watson NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri); 10748088699fSJohn Baldwin if (error) 1075b4151f71SJohn Baldwin return (error); 1076e0f66ef8SJohn Baldwin if (eventp != NULL) 1077e0f66ef8SJohn Baldwin *eventp = ie; 10788088699fSJohn Baldwin } 1079aba10e13SAlexander Motin if (handler != NULL) { 10808d809d50SJeff Roberson error = intr_event_add_handler(ie, name, NULL, handler, arg, 1081d3305205SJohn Baldwin PI_SWI(pri), flags, cookiep); 1082aba10e13SAlexander Motin } 10838d809d50SJeff Roberson return (error); 10848088699fSJohn Baldwin } 10858088699fSJohn Baldwin 10861931cf94SJohn Baldwin /* 1087e0f66ef8SJohn Baldwin * Schedule a software interrupt thread. 10881931cf94SJohn Baldwin */ 10891931cf94SJohn Baldwin void 1090b4151f71SJohn Baldwin swi_sched(void *cookie, int flags) 10911931cf94SJohn Baldwin { 1092e0f66ef8SJohn Baldwin struct intr_handler *ih = (struct intr_handler *)cookie; 1093e0f66ef8SJohn Baldwin struct intr_event *ie = ih->ih_event; 1094d95dca1dSJohn Baldwin struct intr_entropy entropy; 1095ba3f7276SMatt Macy int error __unused; 10968088699fSJohn Baldwin 1097e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name, 1098e0f66ef8SJohn Baldwin ih->ih_need); 10991931cf94SJohn Baldwin 1100aba10e13SAlexander Motin if ((flags & SWI_FROMNMI) == 0) { 1101d95dca1dSJohn Baldwin entropy.event = (uintptr_t)ih; 1102d95dca1dSJohn Baldwin entropy.td = curthread; 110319fa89e9SMark Murray random_harvest_queue(&entropy, sizeof(entropy), RANDOM_SWI); 1104aba10e13SAlexander Motin } 1105d95dca1dSJohn Baldwin 11061931cf94SJohn Baldwin /* 11073e5da754SJohn Baldwin * Set ih_need for this handler so that if the ithread is already 11083e5da754SJohn Baldwin * running it will execute this handler on the next pass. Otherwise, 11093e5da754SJohn Baldwin * it will execute it the next time it runs. 11101931cf94SJohn Baldwin */ 1111283dfee9SKonstantin Belousov ih->ih_need = 1; 11121ca2c018SBruce Evans 1113aba10e13SAlexander Motin if (flags & SWI_DELAY) 1114aba10e13SAlexander Motin return; 1115aba10e13SAlexander Motin 1116aba10e13SAlexander Motin if (flags & SWI_FROMNMI) { 1117aba10e13SAlexander Motin #if defined(SMP) && (defined(__i386__) || defined(__amd64__)) 1118aba10e13SAlexander Motin KASSERT(ie == clk_intr_event, 1119aba10e13SAlexander Motin ("SWI_FROMNMI used not with clk_intr_event")); 1120aba10e13SAlexander Motin ipi_self_from_nmi(IPI_SWI); 1121aba10e13SAlexander Motin #endif 1122aba10e13SAlexander Motin } else { 112383c9dea1SGleb Smirnoff VM_CNT_INC(v_soft); 11246fa041d7SWojciech Macek error = intr_event_schedule_thread(ie, NULL); 11253e5da754SJohn Baldwin KASSERT(error == 0, ("stray software interrupt")); 11268088699fSJohn Baldwin } 11278088699fSJohn Baldwin } 11288088699fSJohn Baldwin 1129fe486a37SJohn Baldwin /* 1130fe486a37SJohn Baldwin * Remove a software interrupt handler. Currently this code does not 1131fe486a37SJohn Baldwin * remove the associated interrupt event if it becomes empty. Calling code 1132fe486a37SJohn Baldwin * may do so manually via intr_event_destroy(), but that's not really 1133fe486a37SJohn Baldwin * an optimal interface. 1134fe486a37SJohn Baldwin */ 1135fe486a37SJohn Baldwin int 1136fe486a37SJohn Baldwin swi_remove(void *cookie) 1137fe486a37SJohn Baldwin { 1138fe486a37SJohn Baldwin 1139fe486a37SJohn Baldwin return (intr_event_remove_handler(cookie)); 1140fe486a37SJohn Baldwin } 1141fe486a37SJohn Baldwin 1142111b043cSAndriy Gapon static void 114337e9511fSJohn Baldwin intr_event_execute_handlers(struct proc *p, struct intr_event *ie) 1144e0f66ef8SJohn Baldwin { 1145111b043cSAndriy Gapon struct intr_handler *ih, *ihn, *ihp; 1146e0f66ef8SJohn Baldwin 1147111b043cSAndriy Gapon ihp = NULL; 1148111b043cSAndriy Gapon CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) { 1149e0f66ef8SJohn Baldwin /* 1150e0f66ef8SJohn Baldwin * If this handler is marked for death, remove it from 1151e0f66ef8SJohn Baldwin * the list of handlers and wake up the sleeper. 1152e0f66ef8SJohn Baldwin */ 1153e0f66ef8SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 1154e0f66ef8SJohn Baldwin mtx_lock(&ie->ie_lock); 1155111b043cSAndriy Gapon if (ihp == NULL) 1156111b043cSAndriy Gapon CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next); 1157111b043cSAndriy Gapon else 1158111b043cSAndriy Gapon CK_SLIST_REMOVE_AFTER(ihp, ih_next); 1159e0f66ef8SJohn Baldwin ih->ih_flags &= ~IH_DEAD; 1160e0f66ef8SJohn Baldwin wakeup(ih); 1161e0f66ef8SJohn Baldwin mtx_unlock(&ie->ie_lock); 1162e0f66ef8SJohn Baldwin continue; 1163e0f66ef8SJohn Baldwin } 1164e0f66ef8SJohn Baldwin 1165111b043cSAndriy Gapon /* 1166111b043cSAndriy Gapon * Now that we know that the current element won't be removed 1167111b043cSAndriy Gapon * update the previous element. 1168111b043cSAndriy Gapon */ 1169111b043cSAndriy Gapon ihp = ih; 1170111b043cSAndriy Gapon 117182a5a275SAndriy Gapon if ((ih->ih_flags & IH_CHANGED) != 0) { 117282a5a275SAndriy Gapon mtx_lock(&ie->ie_lock); 117382a5a275SAndriy Gapon ih->ih_flags &= ~IH_CHANGED; 117482a5a275SAndriy Gapon wakeup(ih); 117582a5a275SAndriy Gapon mtx_unlock(&ie->ie_lock); 117682a5a275SAndriy Gapon } 117782a5a275SAndriy Gapon 1178f2d619c8SPaolo Pisati /* Skip filter only handlers */ 1179f2d619c8SPaolo Pisati if (ih->ih_handler == NULL) 1180f2d619c8SPaolo Pisati continue; 1181f2d619c8SPaolo Pisati 118282a5a275SAndriy Gapon /* Skip suspended handlers */ 118382a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 118482a5a275SAndriy Gapon continue; 118582a5a275SAndriy Gapon 1186e0f66ef8SJohn Baldwin /* 1187e0f66ef8SJohn Baldwin * For software interrupt threads, we only execute 1188e0f66ef8SJohn Baldwin * handlers that have their need flag set. Hardware 1189e0f66ef8SJohn Baldwin * interrupt threads always invoke all of their handlers. 11901b79b949SKirk McKusick * 11911b79b949SKirk McKusick * ih_need can only be 0 or 1. Failed cmpset below 11921b79b949SKirk McKusick * means that there is no request to execute handlers, 11931b79b949SKirk McKusick * so a retry of the cmpset is not needed. 1194e0f66ef8SJohn Baldwin */ 11951b79b949SKirk McKusick if ((ie->ie_flags & IE_SOFT) != 0 && 11961b79b949SKirk McKusick atomic_cmpset_int(&ih->ih_need, 1, 0) == 0) 1197e0f66ef8SJohn Baldwin continue; 1198e0f66ef8SJohn Baldwin 1199e0f66ef8SJohn Baldwin /* Execute this handler. */ 1200e0f66ef8SJohn Baldwin CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x", 1201bafe5a31SPaolo Pisati __func__, p->p_pid, (void *)ih->ih_handler, 1202bafe5a31SPaolo Pisati ih->ih_argument, ih->ih_name, ih->ih_flags); 1203e0f66ef8SJohn Baldwin 1204e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1205e0f66ef8SJohn Baldwin mtx_lock(&Giant); 1206e0f66ef8SJohn Baldwin ih->ih_handler(ih->ih_argument); 1207e0f66ef8SJohn Baldwin if (!(ih->ih_flags & IH_MPSAFE)) 1208e0f66ef8SJohn Baldwin mtx_unlock(&Giant); 1209e0f66ef8SJohn Baldwin } 121037e9511fSJohn Baldwin } 121137e9511fSJohn Baldwin 121237e9511fSJohn Baldwin static void 121337e9511fSJohn Baldwin ithread_execute_handlers(struct proc *p, struct intr_event *ie) 121437e9511fSJohn Baldwin { 121537e9511fSJohn Baldwin 121637e9511fSJohn Baldwin /* Interrupt handlers should not sleep. */ 121737e9511fSJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 121837e9511fSJohn Baldwin THREAD_NO_SLEEPING(); 121937e9511fSJohn Baldwin intr_event_execute_handlers(p, ie); 1220e0f66ef8SJohn Baldwin if (!(ie->ie_flags & IE_SOFT)) 1221e0f66ef8SJohn Baldwin THREAD_SLEEPING_OK(); 1222e0f66ef8SJohn Baldwin 1223e0f66ef8SJohn Baldwin /* 1224e0f66ef8SJohn Baldwin * Interrupt storm handling: 1225e0f66ef8SJohn Baldwin * 1226e0f66ef8SJohn Baldwin * If this interrupt source is currently storming, then throttle 1227e0f66ef8SJohn Baldwin * it to only fire the handler once per clock tick. 1228e0f66ef8SJohn Baldwin * 1229e0f66ef8SJohn Baldwin * If this interrupt source is not currently storming, but the 1230e0f66ef8SJohn Baldwin * number of back to back interrupts exceeds the storm threshold, 1231e0f66ef8SJohn Baldwin * then enter storming mode. 1232e0f66ef8SJohn Baldwin */ 1233e41bcf3cSJohn Baldwin if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold && 1234e41bcf3cSJohn Baldwin !(ie->ie_flags & IE_SOFT)) { 12350ae62c18SNate Lawson /* Report the message only once every second. */ 12360ae62c18SNate Lawson if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) { 1237e0f66ef8SJohn Baldwin printf( 12380ae62c18SNate Lawson "interrupt storm detected on \"%s\"; throttling interrupt source\n", 1239e0f66ef8SJohn Baldwin ie->ie_name); 1240e0f66ef8SJohn Baldwin } 1241e41bcf3cSJohn Baldwin pause("istorm", 1); 1242e0f66ef8SJohn Baldwin } else 1243e0f66ef8SJohn Baldwin ie->ie_count++; 1244e0f66ef8SJohn Baldwin 1245e0f66ef8SJohn Baldwin /* 1246e0f66ef8SJohn Baldwin * Now that all the handlers have had a chance to run, reenable 1247e0f66ef8SJohn Baldwin * the interrupt source. 1248e0f66ef8SJohn Baldwin */ 12491ee1b687SJohn Baldwin if (ie->ie_post_ithread != NULL) 12501ee1b687SJohn Baldwin ie->ie_post_ithread(ie->ie_source); 1251e0f66ef8SJohn Baldwin } 1252e0f66ef8SJohn Baldwin 12538088699fSJohn Baldwin /* 1254b4151f71SJohn Baldwin * This is the main code for interrupt threads. 12558088699fSJohn Baldwin */ 125637c84183SPoul-Henning Kamp static void 1257b4151f71SJohn Baldwin ithread_loop(void *arg) 12588088699fSJohn Baldwin { 1259511d1afbSGleb Smirnoff struct epoch_tracker et; 1260e0f66ef8SJohn Baldwin struct intr_thread *ithd; 1261e0f66ef8SJohn Baldwin struct intr_event *ie; 1262b40ce416SJulian Elischer struct thread *td; 1263b4151f71SJohn Baldwin struct proc *p; 1264511d1afbSGleb Smirnoff int wake, epoch_count; 1265f912e8f2SHans Petter Selasky bool needs_epoch; 12668088699fSJohn Baldwin 1267b40ce416SJulian Elischer td = curthread; 1268b40ce416SJulian Elischer p = td->td_proc; 1269e0f66ef8SJohn Baldwin ithd = (struct intr_thread *)arg; 1270e0f66ef8SJohn Baldwin KASSERT(ithd->it_thread == td, 127191f91617SDavid E. O'Brien ("%s: ithread and proc linkage out of sync", __func__)); 1272e0f66ef8SJohn Baldwin ie = ithd->it_event; 1273e0f66ef8SJohn Baldwin ie->ie_count = 0; 1274e4cd31ddSJeff Roberson wake = 0; 12758088699fSJohn Baldwin 12768088699fSJohn Baldwin /* 12778088699fSJohn Baldwin * As long as we have interrupts outstanding, go through the 12788088699fSJohn Baldwin * list of handlers, giving each one a go at it. 12798088699fSJohn Baldwin */ 12808088699fSJohn Baldwin for (;;) { 1281b4151f71SJohn Baldwin /* 1282b4151f71SJohn Baldwin * If we are an orphaned thread, then just die. 1283b4151f71SJohn Baldwin */ 1284b4151f71SJohn Baldwin if (ithd->it_flags & IT_DEAD) { 1285e0f66ef8SJohn Baldwin CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__, 12867ab24ea3SJulian Elischer p->p_pid, td->td_name); 1287b4151f71SJohn Baldwin free(ithd, M_ITHREAD); 1288ca9a0ddfSJulian Elischer kthread_exit(); 1289b4151f71SJohn Baldwin } 1290b4151f71SJohn Baldwin 1291e0f66ef8SJohn Baldwin /* 1292e0f66ef8SJohn Baldwin * Service interrupts. If another interrupt arrives while 1293e0f66ef8SJohn Baldwin * we are running, it will set it_need to note that we 1294e0f66ef8SJohn Baldwin * should make another pass. 1295283dfee9SKonstantin Belousov * 1296283dfee9SKonstantin Belousov * The load_acq part of the following cmpset ensures 1297283dfee9SKonstantin Belousov * that the load of ih_need in ithread_execute_handlers() 1298283dfee9SKonstantin Belousov * is ordered after the load of it_need here. 1299e0f66ef8SJohn Baldwin */ 1300f912e8f2SHans Petter Selasky needs_epoch = 1301f912e8f2SHans Petter Selasky (atomic_load_int(&ie->ie_hflags) & IH_NET) != 0; 1302f912e8f2SHans Petter Selasky if (needs_epoch) { 1303511d1afbSGleb Smirnoff epoch_count = 0; 1304511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1305511d1afbSGleb Smirnoff } 1306511d1afbSGleb Smirnoff while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) { 1307e0f66ef8SJohn Baldwin ithread_execute_handlers(p, ie); 1308f912e8f2SHans Petter Selasky if (needs_epoch && 1309511d1afbSGleb Smirnoff ++epoch_count >= intr_epoch_batch) { 1310511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 1311511d1afbSGleb Smirnoff epoch_count = 0; 1312511d1afbSGleb Smirnoff NET_EPOCH_ENTER(et); 1313511d1afbSGleb Smirnoff } 1314511d1afbSGleb Smirnoff } 1315f912e8f2SHans Petter Selasky if (needs_epoch) 1316511d1afbSGleb Smirnoff NET_EPOCH_EXIT(et); 13177870c3c6SJohn Baldwin WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 13187870c3c6SJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); 13198088699fSJohn Baldwin 13208088699fSJohn Baldwin /* 13218088699fSJohn Baldwin * Processed all our interrupts. Now get the sched 13228088699fSJohn Baldwin * lock. This may take a while and it_need may get 13238088699fSJohn Baldwin * set again, so we have to check it again. 13248088699fSJohn Baldwin */ 1325982d11f8SJeff Roberson thread_lock(td); 132603bbcb2fSKonstantin Belousov if (atomic_load_acq_int(&ithd->it_need) == 0 && 132703bbcb2fSKonstantin Belousov (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) { 13287870c3c6SJohn Baldwin TD_SET_IWAIT(td); 1329e0f66ef8SJohn Baldwin ie->ie_count = 0; 1330686bcb5cSJeff Roberson mi_switch(SW_VOL | SWT_IWAIT); 1331686bcb5cSJeff Roberson } else { 1332e4cd31ddSJeff Roberson if (ithd->it_flags & IT_WAIT) { 1333e4cd31ddSJeff Roberson wake = 1; 1334e4cd31ddSJeff Roberson ithd->it_flags &= ~IT_WAIT; 1335e4cd31ddSJeff Roberson } 1336982d11f8SJeff Roberson thread_unlock(td); 1337686bcb5cSJeff Roberson } 1338e4cd31ddSJeff Roberson if (wake) { 1339e4cd31ddSJeff Roberson wakeup(ithd); 1340e4cd31ddSJeff Roberson wake = 0; 1341e4cd31ddSJeff Roberson } 13428088699fSJohn Baldwin } 13431931cf94SJohn Baldwin } 13441ee1b687SJohn Baldwin 13451ee1b687SJohn Baldwin /* 13461ee1b687SJohn Baldwin * Main interrupt handling body. 13471ee1b687SJohn Baldwin * 13481ee1b687SJohn Baldwin * Input: 13491ee1b687SJohn Baldwin * o ie: the event connected to this interrupt. 13501ee1b687SJohn Baldwin * o frame: some archs (i.e. i386) pass a frame to some. 13511ee1b687SJohn Baldwin * handlers as their main argument. 13521ee1b687SJohn Baldwin * Return value: 13531ee1b687SJohn Baldwin * o 0: everything ok. 13541ee1b687SJohn Baldwin * o EINVAL: stray interrupt. 13551ee1b687SJohn Baldwin */ 13561ee1b687SJohn Baldwin int 13571ee1b687SJohn Baldwin intr_event_handle(struct intr_event *ie, struct trapframe *frame) 13581ee1b687SJohn Baldwin { 13591ee1b687SJohn Baldwin struct intr_handler *ih; 13601f255bd3SAlexander Motin struct trapframe *oldframe; 13611ee1b687SJohn Baldwin struct thread *td; 1362e0fa977eSAndriy Gapon int phase; 136382a5a275SAndriy Gapon int ret; 136482a5a275SAndriy Gapon bool filter, thread; 13651ee1b687SJohn Baldwin 13661ee1b687SJohn Baldwin td = curthread; 13671ee1b687SJohn Baldwin 1368b7627840SKonstantin Belousov #ifdef KSTACK_USAGE_PROF 1369b7627840SKonstantin Belousov intr_prof_stack_use(td, frame); 1370b7627840SKonstantin Belousov #endif 1371b7627840SKonstantin Belousov 13721ee1b687SJohn Baldwin /* An interrupt with no event or handlers is a stray interrupt. */ 1373111b043cSAndriy Gapon if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers)) 13741ee1b687SJohn Baldwin return (EINVAL); 13751ee1b687SJohn Baldwin 13761ee1b687SJohn Baldwin /* 13771ee1b687SJohn Baldwin * Execute fast interrupt handlers directly. 13781ee1b687SJohn Baldwin * To support clock handlers, if a handler registers 13791ee1b687SJohn Baldwin * with a NULL argument, then we pass it a pointer to 13801ee1b687SJohn Baldwin * a trapframe as its argument. 13811ee1b687SJohn Baldwin */ 13821ee1b687SJohn Baldwin td->td_intr_nesting_level++; 138382a5a275SAndriy Gapon filter = false; 138482a5a275SAndriy Gapon thread = false; 13851ee1b687SJohn Baldwin ret = 0; 13861ee1b687SJohn Baldwin critical_enter(); 13871f255bd3SAlexander Motin oldframe = td->td_intr_frame; 13881f255bd3SAlexander Motin td->td_intr_frame = frame; 1389111b043cSAndriy Gapon 1390e0fa977eSAndriy Gapon phase = ie->ie_phase; 1391e0fa977eSAndriy Gapon atomic_add_int(&ie->ie_active[phase], 1); 1392e0fa977eSAndriy Gapon 1393e0fa977eSAndriy Gapon /* 1394e0fa977eSAndriy Gapon * This fence is required to ensure that no later loads are 1395e0fa977eSAndriy Gapon * re-ordered before the ie_active store. 1396e0fa977eSAndriy Gapon */ 1397e0fa977eSAndriy Gapon atomic_thread_fence_seq_cst(); 1398e0fa977eSAndriy Gapon 1399111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) { 140082a5a275SAndriy Gapon if ((ih->ih_flags & IH_SUSP) != 0) 140182a5a275SAndriy Gapon continue; 1402aba10e13SAlexander Motin if ((ie->ie_flags & IE_SOFT) != 0 && ih->ih_need == 0) 1403aba10e13SAlexander Motin continue; 14041ee1b687SJohn Baldwin if (ih->ih_filter == NULL) { 140582a5a275SAndriy Gapon thread = true; 14061ee1b687SJohn Baldwin continue; 14071ee1b687SJohn Baldwin } 14081ee1b687SJohn Baldwin CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__, 14091ee1b687SJohn Baldwin ih->ih_filter, ih->ih_argument == NULL ? frame : 14101ee1b687SJohn Baldwin ih->ih_argument, ih->ih_name); 14111ee1b687SJohn Baldwin if (ih->ih_argument == NULL) 14121ee1b687SJohn Baldwin ret = ih->ih_filter(frame); 14131ee1b687SJohn Baldwin else 14141ee1b687SJohn Baldwin ret = ih->ih_filter(ih->ih_argument); 14156fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 14166fa041d7SWojciech Macek PMC_SOFT_CALL_TF( , , intr, all, frame); 14176fa041d7SWojciech Macek #endif 141889fc20ccSAndriy Gapon KASSERT(ret == FILTER_STRAY || 141989fc20ccSAndriy Gapon ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 && 142089fc20ccSAndriy Gapon (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0), 142189fc20ccSAndriy Gapon ("%s: incorrect return value %#x from %s", __func__, ret, 142289fc20ccSAndriy Gapon ih->ih_name)); 142382a5a275SAndriy Gapon filter = filter || ret == FILTER_HANDLED; 14246fa041d7SWojciech Macek #ifdef HWPMC_HOOKS 14256fa041d7SWojciech Macek if (ret & FILTER_SCHEDULE_THREAD) 14266fa041d7SWojciech Macek PMC_SOFT_CALL_TF( , , intr, ithread, frame); 14276fa041d7SWojciech Macek else if (ret & FILTER_HANDLED) 14286fa041d7SWojciech Macek PMC_SOFT_CALL_TF( , , intr, filter, frame); 14296fa041d7SWojciech Macek else if (ret == FILTER_STRAY) 14306fa041d7SWojciech Macek PMC_SOFT_CALL_TF( , , intr, stray, frame); 14316fa041d7SWojciech Macek #endif 143289fc20ccSAndriy Gapon 14331ee1b687SJohn Baldwin /* 14341ee1b687SJohn Baldwin * Wrapper handler special handling: 14351ee1b687SJohn Baldwin * 14361ee1b687SJohn Baldwin * in some particular cases (like pccard and pccbb), 14371ee1b687SJohn Baldwin * the _real_ device handler is wrapped in a couple of 14381ee1b687SJohn Baldwin * functions - a filter wrapper and an ithread wrapper. 14391ee1b687SJohn Baldwin * In this case (and just in this case), the filter wrapper 14401ee1b687SJohn Baldwin * could ask the system to schedule the ithread and mask 14411ee1b687SJohn Baldwin * the interrupt source if the wrapped handler is composed 14421ee1b687SJohn Baldwin * of just an ithread handler. 14431ee1b687SJohn Baldwin * 14441ee1b687SJohn Baldwin * TODO: write a generic wrapper to avoid people rolling 144582a5a275SAndriy Gapon * their own. 14461ee1b687SJohn Baldwin */ 14471ee1b687SJohn Baldwin if (!thread) { 14481ee1b687SJohn Baldwin if (ret == FILTER_SCHEDULE_THREAD) 144982a5a275SAndriy Gapon thread = true; 14501ee1b687SJohn Baldwin } 14511ee1b687SJohn Baldwin } 1452e0fa977eSAndriy Gapon atomic_add_rel_int(&ie->ie_active[phase], -1); 1453e0fa977eSAndriy Gapon 14541f255bd3SAlexander Motin td->td_intr_frame = oldframe; 14551ee1b687SJohn Baldwin 14561ee1b687SJohn Baldwin if (thread) { 14571ee1b687SJohn Baldwin if (ie->ie_pre_ithread != NULL) 14581ee1b687SJohn Baldwin ie->ie_pre_ithread(ie->ie_source); 14591ee1b687SJohn Baldwin } else { 14601ee1b687SJohn Baldwin if (ie->ie_post_filter != NULL) 14611ee1b687SJohn Baldwin ie->ie_post_filter(ie->ie_source); 14621ee1b687SJohn Baldwin } 14631ee1b687SJohn Baldwin 14641ee1b687SJohn Baldwin /* Schedule the ithread if needed. */ 14651ee1b687SJohn Baldwin if (thread) { 1466ba3f7276SMatt Macy int error __unused; 1467ba3f7276SMatt Macy 14686fa041d7SWojciech Macek error = intr_event_schedule_thread(ie, frame); 14691ee1b687SJohn Baldwin KASSERT(error == 0, ("bad stray interrupt")); 14701ee1b687SJohn Baldwin } 14711ee1b687SJohn Baldwin critical_exit(); 14721ee1b687SJohn Baldwin td->td_intr_nesting_level--; 147382a5a275SAndriy Gapon #ifdef notyet 147482a5a275SAndriy Gapon /* The interrupt is not aknowledged by any filter and has no ithread. */ 147582a5a275SAndriy Gapon if (!thread && !filter) 147682a5a275SAndriy Gapon return (EINVAL); 147782a5a275SAndriy Gapon #endif 14781ee1b687SJohn Baldwin return (0); 14791ee1b687SJohn Baldwin } 14801931cf94SJohn Baldwin 14818b201c42SJohn Baldwin #ifdef DDB 14828b201c42SJohn Baldwin /* 14838b201c42SJohn Baldwin * Dump details about an interrupt handler 14848b201c42SJohn Baldwin */ 14858b201c42SJohn Baldwin static void 1486e0f66ef8SJohn Baldwin db_dump_intrhand(struct intr_handler *ih) 14878b201c42SJohn Baldwin { 14888b201c42SJohn Baldwin int comma; 14898b201c42SJohn Baldwin 14908b201c42SJohn Baldwin db_printf("\t%-10s ", ih->ih_name); 14918b201c42SJohn Baldwin switch (ih->ih_pri) { 14928b201c42SJohn Baldwin case PI_REALTIME: 14938b201c42SJohn Baldwin db_printf("CLK "); 14948b201c42SJohn Baldwin break; 14958b201c42SJohn Baldwin case PI_AV: 14968b201c42SJohn Baldwin db_printf("AV "); 14978b201c42SJohn Baldwin break; 1498d3305205SJohn Baldwin case PI_TTY: 14998b201c42SJohn Baldwin db_printf("TTY "); 15008b201c42SJohn Baldwin break; 15018b201c42SJohn Baldwin case PI_NET: 15028b201c42SJohn Baldwin db_printf("NET "); 15038b201c42SJohn Baldwin break; 15048b201c42SJohn Baldwin case PI_DISK: 15058b201c42SJohn Baldwin db_printf("DISK"); 15068b201c42SJohn Baldwin break; 15078b201c42SJohn Baldwin case PI_DULL: 15088b201c42SJohn Baldwin db_printf("DULL"); 15098b201c42SJohn Baldwin break; 15108b201c42SJohn Baldwin default: 15118b201c42SJohn Baldwin if (ih->ih_pri >= PI_SOFT) 15128b201c42SJohn Baldwin db_printf("SWI "); 15138b201c42SJohn Baldwin else 15148b201c42SJohn Baldwin db_printf("%4u", ih->ih_pri); 15158b201c42SJohn Baldwin break; 15168b201c42SJohn Baldwin } 15178b201c42SJohn Baldwin db_printf(" "); 1518b887a155SKonstantin Belousov if (ih->ih_filter != NULL) { 1519b887a155SKonstantin Belousov db_printf("[F]"); 1520b887a155SKonstantin Belousov db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC); 1521b887a155SKonstantin Belousov } 1522b887a155SKonstantin Belousov if (ih->ih_handler != NULL) { 1523b887a155SKonstantin Belousov if (ih->ih_filter != NULL) 1524b887a155SKonstantin Belousov db_printf(","); 1525b887a155SKonstantin Belousov db_printf("[H]"); 15268b201c42SJohn Baldwin db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 1527b887a155SKonstantin Belousov } 15288b201c42SJohn Baldwin db_printf("(%p)", ih->ih_argument); 15298b201c42SJohn Baldwin if (ih->ih_need || 1530ef544f63SPaolo Pisati (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 15318b201c42SJohn Baldwin IH_MPSAFE)) != 0) { 15328b201c42SJohn Baldwin db_printf(" {"); 15338b201c42SJohn Baldwin comma = 0; 15348b201c42SJohn Baldwin if (ih->ih_flags & IH_EXCLUSIVE) { 15358b201c42SJohn Baldwin if (comma) 15368b201c42SJohn Baldwin db_printf(", "); 15378b201c42SJohn Baldwin db_printf("EXCL"); 15388b201c42SJohn Baldwin comma = 1; 15398b201c42SJohn Baldwin } 15408b201c42SJohn Baldwin if (ih->ih_flags & IH_ENTROPY) { 15418b201c42SJohn Baldwin if (comma) 15428b201c42SJohn Baldwin db_printf(", "); 15438b201c42SJohn Baldwin db_printf("ENTROPY"); 15448b201c42SJohn Baldwin comma = 1; 15458b201c42SJohn Baldwin } 15468b201c42SJohn Baldwin if (ih->ih_flags & IH_DEAD) { 15478b201c42SJohn Baldwin if (comma) 15488b201c42SJohn Baldwin db_printf(", "); 15498b201c42SJohn Baldwin db_printf("DEAD"); 15508b201c42SJohn Baldwin comma = 1; 15518b201c42SJohn Baldwin } 15528b201c42SJohn Baldwin if (ih->ih_flags & IH_MPSAFE) { 15538b201c42SJohn Baldwin if (comma) 15548b201c42SJohn Baldwin db_printf(", "); 15558b201c42SJohn Baldwin db_printf("MPSAFE"); 15568b201c42SJohn Baldwin comma = 1; 15578b201c42SJohn Baldwin } 15588b201c42SJohn Baldwin if (ih->ih_need) { 15598b201c42SJohn Baldwin if (comma) 15608b201c42SJohn Baldwin db_printf(", "); 15618b201c42SJohn Baldwin db_printf("NEED"); 15628b201c42SJohn Baldwin } 15638b201c42SJohn Baldwin db_printf("}"); 15648b201c42SJohn Baldwin } 15658b201c42SJohn Baldwin db_printf("\n"); 15668b201c42SJohn Baldwin } 15678b201c42SJohn Baldwin 15688b201c42SJohn Baldwin /* 1569e0f66ef8SJohn Baldwin * Dump details about a event. 15708b201c42SJohn Baldwin */ 15718b201c42SJohn Baldwin void 1572e0f66ef8SJohn Baldwin db_dump_intr_event(struct intr_event *ie, int handlers) 15738b201c42SJohn Baldwin { 1574e0f66ef8SJohn Baldwin struct intr_handler *ih; 1575e0f66ef8SJohn Baldwin struct intr_thread *it; 15768b201c42SJohn Baldwin int comma; 15778b201c42SJohn Baldwin 1578e0f66ef8SJohn Baldwin db_printf("%s ", ie->ie_fullname); 1579e0f66ef8SJohn Baldwin it = ie->ie_thread; 1580e0f66ef8SJohn Baldwin if (it != NULL) 1581e0f66ef8SJohn Baldwin db_printf("(pid %d)", it->it_thread->td_proc->p_pid); 1582e0f66ef8SJohn Baldwin else 1583e0f66ef8SJohn Baldwin db_printf("(no thread)"); 1584c4eb6630SGleb Smirnoff if ((ie->ie_flags & (IE_SOFT | IE_ADDING_THREAD)) != 0 || 1585e0f66ef8SJohn Baldwin (it != NULL && it->it_need)) { 15868b201c42SJohn Baldwin db_printf(" {"); 15878b201c42SJohn Baldwin comma = 0; 1588e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_SOFT) { 15898b201c42SJohn Baldwin db_printf("SOFT"); 15908b201c42SJohn Baldwin comma = 1; 15918b201c42SJohn Baldwin } 1592e0f66ef8SJohn Baldwin if (ie->ie_flags & IE_ADDING_THREAD) { 15938b201c42SJohn Baldwin if (comma) 15948b201c42SJohn Baldwin db_printf(", "); 1595e0f66ef8SJohn Baldwin db_printf("ADDING_THREAD"); 15968b201c42SJohn Baldwin comma = 1; 15978b201c42SJohn Baldwin } 1598e0f66ef8SJohn Baldwin if (it != NULL && it->it_need) { 15998b201c42SJohn Baldwin if (comma) 16008b201c42SJohn Baldwin db_printf(", "); 16018b201c42SJohn Baldwin db_printf("NEED"); 16028b201c42SJohn Baldwin } 16038b201c42SJohn Baldwin db_printf("}"); 16048b201c42SJohn Baldwin } 16058b201c42SJohn Baldwin db_printf("\n"); 16068b201c42SJohn Baldwin 16078b201c42SJohn Baldwin if (handlers) 1608111b043cSAndriy Gapon CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) 16098b201c42SJohn Baldwin db_dump_intrhand(ih); 16108b201c42SJohn Baldwin } 1611e0f66ef8SJohn Baldwin 1612e0f66ef8SJohn Baldwin /* 1613e0f66ef8SJohn Baldwin * Dump data about interrupt handlers 1614e0f66ef8SJohn Baldwin */ 1615e0f66ef8SJohn Baldwin DB_SHOW_COMMAND(intr, db_show_intr) 1616e0f66ef8SJohn Baldwin { 1617e0f66ef8SJohn Baldwin struct intr_event *ie; 161819e9205aSJohn Baldwin int all, verbose; 1619e0f66ef8SJohn Baldwin 1620dc15eac0SEd Schouten verbose = strchr(modif, 'v') != NULL; 1621dc15eac0SEd Schouten all = strchr(modif, 'a') != NULL; 1622e0f66ef8SJohn Baldwin TAILQ_FOREACH(ie, &event_list, ie_list) { 1623111b043cSAndriy Gapon if (!all && CK_SLIST_EMPTY(&ie->ie_handlers)) 1624e0f66ef8SJohn Baldwin continue; 1625e0f66ef8SJohn Baldwin db_dump_intr_event(ie, verbose); 162619e9205aSJohn Baldwin if (db_pager_quit) 162719e9205aSJohn Baldwin break; 1628e0f66ef8SJohn Baldwin } 1629e0f66ef8SJohn Baldwin } 16308b201c42SJohn Baldwin #endif /* DDB */ 16318b201c42SJohn Baldwin 1632b4151f71SJohn Baldwin /* 16338088699fSJohn Baldwin * Start standard software interrupt threads 16341931cf94SJohn Baldwin */ 16351931cf94SJohn Baldwin static void 1636b4151f71SJohn Baldwin start_softintr(void *dummy) 16371931cf94SJohn Baldwin { 1638b4151f71SJohn Baldwin 1639aba10e13SAlexander Motin if (swi_add(&clk_intr_event, "clk", NULL, NULL, SWI_CLOCK, 1640aba10e13SAlexander Motin INTR_MPSAFE, NULL)) 1641aba10e13SAlexander Motin panic("died while creating clk swi ithread"); 16428d809d50SJeff Roberson if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 16438d809d50SJeff Roberson panic("died while creating vm swi ithread"); 16441931cf94SJohn Baldwin } 1645237fdd78SRobert Watson SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, 1646237fdd78SRobert Watson NULL); 16471931cf94SJohn Baldwin 1648d279178dSThomas Moestl /* 1649d279178dSThomas Moestl * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1650d279178dSThomas Moestl * The data for this machine dependent, and the declarations are in machine 1651d279178dSThomas Moestl * dependent code. The layout of intrnames and intrcnt however is machine 1652d279178dSThomas Moestl * independent. 1653d279178dSThomas Moestl * 1654d279178dSThomas Moestl * We do not know the length of intrcnt and intrnames at compile time, so 1655d279178dSThomas Moestl * calculate things at run time. 1656d279178dSThomas Moestl */ 1657d279178dSThomas Moestl static int 1658d279178dSThomas Moestl sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1659d279178dSThomas Moestl { 1660521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req)); 1661d279178dSThomas Moestl } 1662d279178dSThomas Moestl 16637029da5cSPawel Biernacki SYSCTL_PROC(_hw, OID_AUTO, intrnames, 166467f508dbSAlexander Motin CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 16657029da5cSPawel Biernacki sysctl_intrnames, "", 16667029da5cSPawel Biernacki "Interrupt Names"); 1667d279178dSThomas Moestl 1668d279178dSThomas Moestl static int 1669d279178dSThomas Moestl sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1670d279178dSThomas Moestl { 167185729c2cSJuli Mallett #ifdef SCTL_MASK32 167285729c2cSJuli Mallett uint32_t *intrcnt32; 167385729c2cSJuli Mallett unsigned i; 167485729c2cSJuli Mallett int error; 167585729c2cSJuli Mallett 167685729c2cSJuli Mallett if (req->flags & SCTL_MASK32) { 167785729c2cSJuli Mallett if (!req->oldptr) 167885729c2cSJuli Mallett return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req)); 167985729c2cSJuli Mallett intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT); 168085729c2cSJuli Mallett if (intrcnt32 == NULL) 168185729c2cSJuli Mallett return (ENOMEM); 168285729c2cSJuli Mallett for (i = 0; i < sintrcnt / sizeof (u_long); i++) 168385729c2cSJuli Mallett intrcnt32[i] = intrcnt[i]; 168485729c2cSJuli Mallett error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req); 168585729c2cSJuli Mallett free(intrcnt32, M_TEMP); 168685729c2cSJuli Mallett return (error); 168785729c2cSJuli Mallett } 168885729c2cSJuli Mallett #endif 1689521ea19dSAttilio Rao return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req)); 1690d279178dSThomas Moestl } 1691d279178dSThomas Moestl 16927029da5cSPawel Biernacki SYSCTL_PROC(_hw, OID_AUTO, intrcnt, 169367f508dbSAlexander Motin CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 16947029da5cSPawel Biernacki sysctl_intrcnt, "", 16957029da5cSPawel Biernacki "Interrupt Counts"); 16968b201c42SJohn Baldwin 16978b201c42SJohn Baldwin #ifdef DDB 16988b201c42SJohn Baldwin /* 16998b201c42SJohn Baldwin * DDB command to dump the interrupt statistics. 17008b201c42SJohn Baldwin */ 17018b201c42SJohn Baldwin DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 17028b201c42SJohn Baldwin { 17038b201c42SJohn Baldwin u_long *i; 17048b201c42SJohn Baldwin char *cp; 1705521ea19dSAttilio Rao u_int j; 17068b201c42SJohn Baldwin 17078b201c42SJohn Baldwin cp = intrnames; 1708521ea19dSAttilio Rao j = 0; 1709521ea19dSAttilio Rao for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit; 1710521ea19dSAttilio Rao i++, j++) { 17118b201c42SJohn Baldwin if (*cp == '\0') 17128b201c42SJohn Baldwin break; 17138b201c42SJohn Baldwin if (*i != 0) 17148b201c42SJohn Baldwin db_printf("%s\t%lu\n", cp, *i); 17158b201c42SJohn Baldwin cp += strlen(cp) + 1; 17168b201c42SJohn Baldwin } 17178b201c42SJohn Baldwin } 17188b201c42SJohn Baldwin #endif 1719