1*04615d56Smrg /* $NetBSD: kern_softint.c,v 1.76 2024/03/01 04:32:38 mrg Exp $ */
2342d5fc9Sad
3342d5fc9Sad /*-
4cddaba8dSad * Copyright (c) 2007, 2008, 2019, 2020 The NetBSD Foundation, Inc.
5342d5fc9Sad * All rights reserved.
6342d5fc9Sad *
7342d5fc9Sad * This code is derived from software contributed to The NetBSD Foundation
8342d5fc9Sad * by Andrew Doran.
9342d5fc9Sad *
10342d5fc9Sad * Redistribution and use in source and binary forms, with or without
11342d5fc9Sad * modification, are permitted provided that the following conditions
12342d5fc9Sad * are met:
13342d5fc9Sad * 1. Redistributions of source code must retain the above copyright
14342d5fc9Sad * notice, this list of conditions and the following disclaimer.
15342d5fc9Sad * 2. Redistributions in binary form must reproduce the above copyright
16342d5fc9Sad * notice, this list of conditions and the following disclaimer in the
17342d5fc9Sad * documentation and/or other materials provided with the distribution.
18342d5fc9Sad *
19342d5fc9Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20342d5fc9Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21342d5fc9Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22342d5fc9Sad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23342d5fc9Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24342d5fc9Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25342d5fc9Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26342d5fc9Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27342d5fc9Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28342d5fc9Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29342d5fc9Sad * POSSIBILITY OF SUCH DAMAGE.
30342d5fc9Sad */
31342d5fc9Sad
32342d5fc9Sad /*
334b293a84Sad * Generic software interrupt framework.
344b293a84Sad *
354b293a84Sad * Overview
364b293a84Sad *
374b293a84Sad * The soft interrupt framework provides a mechanism to schedule a
384b293a84Sad * low priority callback that runs with thread context. It allows
394b293a84Sad * for dynamic registration of software interrupts, and for fair
404b293a84Sad * queueing and prioritization of those interrupts. The callbacks
414b293a84Sad * can be scheduled to run from nearly any point in the kernel: by
424b293a84Sad * code running with thread context, by code running from a
434b293a84Sad * hardware interrupt handler, and at any interrupt priority
444b293a84Sad * level.
454b293a84Sad *
464b293a84Sad * Priority levels
474b293a84Sad *
484b293a84Sad * Since soft interrupt dispatch can be tied to the underlying
494b293a84Sad * architecture's interrupt dispatch code, it can be limited
504b293a84Sad * both by the capabilities of the hardware and the capabilities
514b293a84Sad * of the interrupt dispatch code itself. The number of priority
524b293a84Sad * levels is restricted to four. In order of priority (lowest to
534b293a84Sad * highest) the levels are: clock, bio, net, serial.
544b293a84Sad *
554b293a84Sad * The names are symbolic and in isolation do not have any direct
564b293a84Sad * connection with a particular kind of device activity: they are
574b293a84Sad * only meant as a guide.
584b293a84Sad *
594b293a84Sad * The four priority levels map directly to scheduler priority
604b293a84Sad * levels, and where the architecture implements 'fast' software
614b293a84Sad * interrupts, they also map onto interrupt priorities. The
624b293a84Sad * interrupt priorities are intended to be hidden from machine
634b293a84Sad * independent code, which should use thread-safe mechanisms to
644b293a84Sad * synchronize with software interrupts (for example: mutexes).
654b293a84Sad *
664b293a84Sad * Capabilities
674b293a84Sad *
684b293a84Sad * Software interrupts run with limited machine context. In
694b293a84Sad * particular, they do not posess any address space context. They
704b293a84Sad * should not try to operate on user space addresses, or to use
714b293a84Sad * virtual memory facilities other than those noted as interrupt
724b293a84Sad * safe.
734b293a84Sad *
744b293a84Sad * Unlike hardware interrupts, software interrupts do have thread
754b293a84Sad * context. They may block on synchronization objects, sleep, and
764b293a84Sad * resume execution at a later time.
774b293a84Sad *
784b293a84Sad * Since software interrupts are a limited resource and run with
794b293a84Sad * higher priority than most other LWPs in the system, all
804b293a84Sad * block-and-resume activity by a software interrupt must be kept
81c40d6e65Smsaitoh * short to allow further processing at that level to continue. By
824b293a84Sad * extension, code running with process context must take care to
834b293a84Sad * ensure that any lock that may be taken from a software interrupt
844b293a84Sad * can not be held for more than a short period of time.
854b293a84Sad *
864b293a84Sad * The kernel does not allow software interrupts to use facilities
874b293a84Sad * or perform actions that may block for a significant amount of
884b293a84Sad * time. This means that it's not valid for a software interrupt
891b6e5c8bSad * to sleep on condition variables or wait for resources to become
901b6e5c8bSad * available (for example, memory).
914b293a84Sad *
924b293a84Sad * Per-CPU operation
934b293a84Sad *
944b293a84Sad * If a soft interrupt is triggered on a CPU, it can only be
954b293a84Sad * dispatched on the same CPU. Each LWP dedicated to handling a
964b293a84Sad * soft interrupt is bound to its home CPU, so if the LWP blocks
974b293a84Sad * and needs to run again, it can only run there. Nearly all data
984b293a84Sad * structures used to manage software interrupts are per-CPU.
994b293a84Sad *
1004b293a84Sad * The per-CPU requirement is intended to reduce "ping-pong" of
1014b293a84Sad * cache lines between CPUs: lines occupied by data structures
1024b293a84Sad * used to manage the soft interrupts, and lines occupied by data
1034b293a84Sad * items being passed down to the soft interrupt. As a positive
1044b293a84Sad * side effect, this also means that the soft interrupt dispatch
1054b293a84Sad * code does not need to to use spinlocks to synchronize.
1064b293a84Sad *
1074b293a84Sad * Generic implementation
1084b293a84Sad *
1094b293a84Sad * A generic, low performance implementation is provided that
1104b293a84Sad * works across all architectures, with no machine-dependent
1114b293a84Sad * modifications needed. This implementation uses the scheduler,
1124b293a84Sad * and so has a number of restrictions:
1134b293a84Sad *
1144b293a84Sad * 1) The software interrupts are not currently preemptive, so
1154b293a84Sad * must wait for the currently executing LWP to yield the CPU.
1164b293a84Sad * This can introduce latency.
1174b293a84Sad *
1184b293a84Sad * 2) An expensive context switch is required for a software
1194b293a84Sad * interrupt to be handled.
1204b293a84Sad *
1214b293a84Sad * 'Fast' software interrupts
1224b293a84Sad *
1234b293a84Sad * If an architectures defines __HAVE_FAST_SOFTINTS, it implements
1244b293a84Sad * the fast mechanism. Threads running either in the kernel or in
1254b293a84Sad * userspace will be interrupted, but will not be preempted. When
1264b293a84Sad * the soft interrupt completes execution, the interrupted LWP
1274b293a84Sad * is resumed. Interrupt dispatch code must provide the minimum
1284b293a84Sad * level of context necessary for the soft interrupt to block and
1294b293a84Sad * be resumed at a later time. The machine-dependent dispatch
1304b293a84Sad * path looks something like the following:
1314b293a84Sad *
1324b293a84Sad * softintr()
1334b293a84Sad * {
1344b293a84Sad * go to IPL_HIGH if necessary for switch;
1354b293a84Sad * save any necessary registers in a format that can be
1364b293a84Sad * restored by cpu_switchto if the softint blocks;
1374b293a84Sad * arrange for cpu_switchto() to restore into the
1384b293a84Sad * trampoline function;
1394b293a84Sad * identify LWP to handle this interrupt;
1404b293a84Sad * switch to the LWP's stack;
1414b293a84Sad * switch register stacks, if necessary;
1424b293a84Sad * assign new value of curlwp;
1434b293a84Sad * call MI softint_dispatch, passing old curlwp and IPL
1444b293a84Sad * to execute interrupt at;
1454b293a84Sad * switch back to old stack;
1464b293a84Sad * switch back to old register stack, if necessary;
1474b293a84Sad * restore curlwp;
1484b293a84Sad * return to interrupted LWP;
1494b293a84Sad * }
1504b293a84Sad *
1514b293a84Sad * If the soft interrupt blocks, a trampoline function is returned
1524b293a84Sad * to in the context of the interrupted LWP, as arranged for by
1534b293a84Sad * softint():
1544b293a84Sad *
1554b293a84Sad * softint_ret()
1564b293a84Sad * {
1574b293a84Sad * unlock soft interrupt LWP;
1584b293a84Sad * resume interrupt processing, likely returning to
1594b293a84Sad * interrupted LWP or dispatching another, different
1604b293a84Sad * interrupt;
1614b293a84Sad * }
1624b293a84Sad *
1634b293a84Sad * Once the soft interrupt has fired (and even if it has blocked),
1644b293a84Sad * no further soft interrupts at that level will be triggered by
1654b293a84Sad * MI code until the soft interrupt handler has ceased execution.
1664b293a84Sad * If a soft interrupt handler blocks and is resumed, it resumes
1674b293a84Sad * execution as a normal LWP (kthread) and gains VM context. Only
1684b293a84Sad * when it has completed and is ready to fire again will it
1694b293a84Sad * interrupt other threads.
170342d5fc9Sad */
171342d5fc9Sad
172342d5fc9Sad #include <sys/cdefs.h>
173*04615d56Smrg __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.76 2024/03/01 04:32:38 mrg Exp $");
174342d5fc9Sad
175342d5fc9Sad #include <sys/param.h>
1764b293a84Sad #include <sys/proc.h>
177342d5fc9Sad #include <sys/intr.h>
1780132815bSrmind #include <sys/ipi.h>
17973d972e3Sad #include <sys/lock.h>
1804b293a84Sad #include <sys/mutex.h>
181412ac21eSmsaitoh #include <sys/kernel.h>
1824b293a84Sad #include <sys/kthread.h>
1834b293a84Sad #include <sys/evcnt.h>
1844b293a84Sad #include <sys/cpu.h>
18570a01ab8Sad #include <sys/xcall.h>
1866ffff4f2Sthorpej #include <sys/psref.h>
187dfbd550eSriastradh #include <sys/sdt.h>
1884b293a84Sad
1894b293a84Sad #include <uvm/uvm_extern.h>
1904b293a84Sad
1914b293a84Sad /* This could overlap with signal info in struct lwp. */
1924b293a84Sad typedef struct softint {
1934b293a84Sad SIMPLEQ_HEAD(, softhand) si_q;
1944b293a84Sad struct lwp *si_lwp;
1954b293a84Sad struct cpu_info *si_cpu;
1964b293a84Sad uintptr_t si_machdep;
1974b293a84Sad struct evcnt si_evcnt;
1984b293a84Sad struct evcnt si_evcnt_block;
199a977daeaSad volatile int si_active;
200972d99ceSad int si_ipl;
2014b293a84Sad char si_name[8];
2024b293a84Sad char si_name_block[8+6];
2034b293a84Sad } softint_t;
2044b293a84Sad
2054b293a84Sad typedef struct softhand {
2064b293a84Sad SIMPLEQ_ENTRY(softhand) sh_q;
2074b293a84Sad void (*sh_func)(void *);
2084b293a84Sad void *sh_arg;
2094b293a84Sad softint_t *sh_isr;
2108ebd73cdSbouyer u_int sh_flags;
2110132815bSrmind u_int sh_ipi_id;
2124b293a84Sad } softhand_t;
2134b293a84Sad
2144b293a84Sad typedef struct softcpu {
2154b293a84Sad struct cpu_info *sc_cpu;
2164b293a84Sad softint_t sc_int[SOFTINT_COUNT];
2174b293a84Sad softhand_t sc_hand[1];
2184b293a84Sad } softcpu_t;
2194b293a84Sad
2204b293a84Sad static void softint_thread(void *);
2214b293a84Sad
222575c8638Smsaitoh u_int softint_bytes = 32768;
22336a17127Sad u_int softint_timing;
2244b293a84Sad static u_int softint_max;
2254b293a84Sad static kmutex_t softint_lock;
22636a17127Sad
227dfbd550eSriastradh SDT_PROBE_DEFINE4(sdt, kernel, softint, establish,
228dfbd550eSriastradh "void *"/*sih*/,
229dfbd550eSriastradh "void (*)(void *)"/*func*/,
230dfbd550eSriastradh "void *"/*arg*/,
231dfbd550eSriastradh "unsigned"/*flags*/);
232dfbd550eSriastradh
233dfbd550eSriastradh SDT_PROBE_DEFINE1(sdt, kernel, softint, disestablish,
234dfbd550eSriastradh "void *"/*sih*/);
235dfbd550eSriastradh
236dfbd550eSriastradh SDT_PROBE_DEFINE2(sdt, kernel, softint, schedule,
237dfbd550eSriastradh "void *"/*sih*/,
238dfbd550eSriastradh "struct cpu_info *"/*ci*/);
239dfbd550eSriastradh
240dfbd550eSriastradh SDT_PROBE_DEFINE4(sdt, kernel, softint, entry,
241dfbd550eSriastradh "void *"/*sih*/,
242dfbd550eSriastradh "void (*)(void *)"/*func*/,
243dfbd550eSriastradh "void *"/*arg*/,
244dfbd550eSriastradh "unsigned"/*flags*/);
245dfbd550eSriastradh
246dfbd550eSriastradh SDT_PROBE_DEFINE4(sdt, kernel, softint, return,
247dfbd550eSriastradh "void *"/*sih*/,
248dfbd550eSriastradh "void (*)(void *)"/*func*/,
249dfbd550eSriastradh "void *"/*arg*/,
250dfbd550eSriastradh "unsigned"/*flags*/);
251dfbd550eSriastradh
252342d5fc9Sad /*
2534b293a84Sad * softint_init_isr:
2544b293a84Sad *
2554b293a84Sad * Initialize a single interrupt level for a single CPU.
2564b293a84Sad */
2574b293a84Sad static void
softint_init_isr(softcpu_t * sc,const char * desc,pri_t pri,u_int level,int ipl)258972d99ceSad softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level,
259972d99ceSad int ipl)
2604b293a84Sad {
2614b293a84Sad struct cpu_info *ci;
2624b293a84Sad softint_t *si;
2634b293a84Sad int error;
2644b293a84Sad
2654b293a84Sad si = &sc->sc_int[level];
2664b293a84Sad ci = sc->sc_cpu;
2674b293a84Sad si->si_cpu = ci;
2684b293a84Sad
2694b293a84Sad SIMPLEQ_INIT(&si->si_q);
2704b293a84Sad
2714b293a84Sad error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
2724b293a84Sad KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
273d8788e7fSmartin "soft%s/%u", desc, ci->ci_index);
2744b293a84Sad if (error != 0)
2754b293a84Sad panic("softint_init_isr: error %d", error);
2764b293a84Sad
277d8788e7fSmartin snprintf(si->si_name, sizeof(si->si_name), "%s/%u", desc,
278d8788e7fSmartin ci->ci_index);
279ffc4969fSad evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_MISC, NULL,
2804b293a84Sad "softint", si->si_name);
281d8788e7fSmartin snprintf(si->si_name_block, sizeof(si->si_name_block), "%s block/%u",
282d8788e7fSmartin desc, ci->ci_index);
283ffc4969fSad evcnt_attach_dynamic(&si->si_evcnt_block, EVCNT_TYPE_MISC, NULL,
2844b293a84Sad "softint", si->si_name_block);
2854b293a84Sad
286972d99ceSad si->si_ipl = ipl;
2874b293a84Sad si->si_lwp->l_private = si;
2884b293a84Sad softint_init_md(si->si_lwp, level, &si->si_machdep);
2894b293a84Sad }
2907a938297Suebayasi
2914b293a84Sad /*
292342d5fc9Sad * softint_init:
293342d5fc9Sad *
294342d5fc9Sad * Initialize per-CPU data structures. Called from mi_cpu_attach().
295342d5fc9Sad */
296342d5fc9Sad void
softint_init(struct cpu_info * ci)297342d5fc9Sad softint_init(struct cpu_info *ci)
298342d5fc9Sad {
2994b293a84Sad static struct cpu_info *first;
3004b293a84Sad softcpu_t *sc, *scfirst;
3014b293a84Sad softhand_t *sh, *shmax;
302342d5fc9Sad
3034b293a84Sad if (first == NULL) {
3044b293a84Sad /* Boot CPU. */
3054b293a84Sad first = ci;
3064b293a84Sad mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
3074b293a84Sad softint_bytes = round_page(softint_bytes);
3084b293a84Sad softint_max = (softint_bytes - sizeof(softcpu_t)) /
3094b293a84Sad sizeof(softhand_t);
3104b293a84Sad }
3114b293a84Sad
3127a938297Suebayasi /* Use uvm_km(9) for persistent, page-aligned allocation. */
3137a938297Suebayasi sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
3147a938297Suebayasi UVM_KMF_WIRED | UVM_KMF_ZERO);
3154b293a84Sad if (sc == NULL)
3164b293a84Sad panic("softint_init_cpu: cannot allocate memory");
3174b293a84Sad
3184b293a84Sad ci->ci_data.cpu_softcpu = sc;
3194b293a84Sad ci->ci_data.cpu_softints = 0;
3204b293a84Sad sc->sc_cpu = ci;
3214b293a84Sad
322972d99ceSad softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET,
323972d99ceSad IPL_SOFTNET);
324972d99ceSad softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO,
325972d99ceSad IPL_SOFTBIO);
326972d99ceSad softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK,
327972d99ceSad IPL_SOFTCLOCK);
328972d99ceSad softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL,
329972d99ceSad IPL_SOFTSERIAL);
3304b293a84Sad
3314b293a84Sad if (first != ci) {
3324b293a84Sad mutex_enter(&softint_lock);
3334b293a84Sad scfirst = first->ci_data.cpu_softcpu;
3344b293a84Sad sh = sc->sc_hand;
3354b293a84Sad memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
3364b293a84Sad /* Update pointers for this CPU. */
3374b293a84Sad for (shmax = sh + softint_max; sh < shmax; sh++) {
3384b293a84Sad if (sh->sh_func == NULL)
3394b293a84Sad continue;
3404b293a84Sad sh->sh_isr =
3414b293a84Sad &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
3424b293a84Sad }
3434b293a84Sad mutex_exit(&softint_lock);
3444b293a84Sad }
345342d5fc9Sad }
346342d5fc9Sad
347342d5fc9Sad /*
348342d5fc9Sad * softint_establish:
349342d5fc9Sad *
350342d5fc9Sad * Register a software interrupt handler.
351342d5fc9Sad */
352342d5fc9Sad void *
softint_establish(u_int flags,void (* func)(void *),void * arg)353342d5fc9Sad softint_establish(u_int flags, void (*func)(void *), void *arg)
354342d5fc9Sad {
3554b293a84Sad CPU_INFO_ITERATOR cii;
3564b293a84Sad struct cpu_info *ci;
3574b293a84Sad softcpu_t *sc;
3584b293a84Sad softhand_t *sh;
3594b293a84Sad u_int level, index;
3600132815bSrmind u_int ipi_id = 0;
3610132815bSrmind void *sih;
362342d5fc9Sad
363342d5fc9Sad level = (flags & SOFTINT_LVLMASK);
364342d5fc9Sad KASSERT(level < SOFTINT_COUNT);
36570a01ab8Sad KASSERT((flags & SOFTINT_IMPMASK) == 0);
366342d5fc9Sad
3674b293a84Sad mutex_enter(&softint_lock);
3684b293a84Sad
3694b293a84Sad /* Find a free slot. */
3704b293a84Sad sc = curcpu()->ci_data.cpu_softcpu;
371b7a7b9b8Smatt for (index = 1; index < softint_max; index++) {
3724b293a84Sad if (sc->sc_hand[index].sh_func == NULL)
373342d5fc9Sad break;
374b7a7b9b8Smatt }
3754b293a84Sad if (index == softint_max) {
3764b293a84Sad mutex_exit(&softint_lock);
3774b293a84Sad printf("WARNING: softint_establish: table full, "
3784b293a84Sad "increase softint_bytes\n");
3794b293a84Sad return NULL;
380342d5fc9Sad }
3810132815bSrmind sih = (void *)((uint8_t *)&sc->sc_hand[index] - (uint8_t *)sc);
3820132815bSrmind
3830132815bSrmind if (flags & SOFTINT_RCPU) {
3840132815bSrmind if ((ipi_id = ipi_register(softint_schedule, sih)) == 0) {
3850132815bSrmind mutex_exit(&softint_lock);
3860132815bSrmind return NULL;
3870132815bSrmind }
3880132815bSrmind }
389342d5fc9Sad
3904b293a84Sad /* Set up the handler on each CPU. */
3917b8aa25aSad if (ncpu < 2) {
392eb83663dSad /* XXX hack for machines with no CPU_INFO_FOREACH() early on */
393eb83663dSad sc = curcpu()->ci_data.cpu_softcpu;
394eb83663dSad sh = &sc->sc_hand[index];
395eb83663dSad sh->sh_isr = &sc->sc_int[level];
396eb83663dSad sh->sh_func = func;
397eb83663dSad sh->sh_arg = arg;
398eb83663dSad sh->sh_flags = flags;
3990132815bSrmind sh->sh_ipi_id = ipi_id;
400eb83663dSad } else for (CPU_INFO_FOREACH(cii, ci)) {
4014b293a84Sad sc = ci->ci_data.cpu_softcpu;
4024b293a84Sad sh = &sc->sc_hand[index];
4034b293a84Sad sh->sh_isr = &sc->sc_int[level];
4044b293a84Sad sh->sh_func = func;
4054b293a84Sad sh->sh_arg = arg;
4064b293a84Sad sh->sh_flags = flags;
4070132815bSrmind sh->sh_ipi_id = ipi_id;
4084b293a84Sad }
4094b293a84Sad mutex_exit(&softint_lock);
4104b293a84Sad
411dfbd550eSriastradh SDT_PROBE4(sdt, kernel, softint, establish, sih, func, arg, flags);
412dfbd550eSriastradh
4130132815bSrmind return sih;
414342d5fc9Sad }
415342d5fc9Sad
416342d5fc9Sad /*
417342d5fc9Sad * softint_disestablish:
418342d5fc9Sad *
41970a01ab8Sad * Unregister a software interrupt handler. The soft interrupt could
42070a01ab8Sad * still be active at this point, but the caller commits not to try
42170a01ab8Sad * and trigger it again once this call is made. The caller must not
42270a01ab8Sad * hold any locks that could be taken from soft interrupt context,
42370a01ab8Sad * because we will wait for the softint to complete if it's still
42470a01ab8Sad * running.
425342d5fc9Sad */
426342d5fc9Sad void
softint_disestablish(void * arg)427342d5fc9Sad softint_disestablish(void *arg)
428342d5fc9Sad {
4294b293a84Sad CPU_INFO_ITERATOR cii;
4304b293a84Sad struct cpu_info *ci;
4314b293a84Sad softcpu_t *sc;
4324b293a84Sad softhand_t *sh;
4334b293a84Sad uintptr_t offset;
434342d5fc9Sad
4354b293a84Sad offset = (uintptr_t)arg;
4360dec6ba3Sriastradh KASSERT(offset != 0);
4370dec6ba3Sriastradh KASSERTMSG(offset < softint_bytes, "%"PRIuPTR" %u",
43825fcead2Smatt offset, softint_bytes);
4394b293a84Sad
44070a01ab8Sad /*
441972d99ceSad * Unregister IPI handler if there is any. Note: there is no need
442972d99ceSad * to disable preemption here - ID is stable.
4430132815bSrmind */
4440132815bSrmind sc = curcpu()->ci_data.cpu_softcpu;
4450132815bSrmind sh = (softhand_t *)((uint8_t *)sc + offset);
4460132815bSrmind if (sh->sh_ipi_id) {
4470132815bSrmind ipi_unregister(sh->sh_ipi_id);
4480132815bSrmind }
4490132815bSrmind
4500132815bSrmind /*
451972d99ceSad * Run a dummy softint at the same level on all CPUs and wait for
452972d99ceSad * completion, to make sure this softint is no longer running
453972d99ceSad * anywhere.
45470a01ab8Sad */
455972d99ceSad xc_barrier(XC_HIGHPRI_IPL(sh->sh_isr->si_ipl));
4564b293a84Sad
457dfbd550eSriastradh /*
458dfbd550eSriastradh * Notify dtrace probe when the old softint can't be running
459dfbd550eSriastradh * any more, but before it can be recycled for a new softint.
460dfbd550eSriastradh */
461dfbd550eSriastradh SDT_PROBE1(sdt, kernel, softint, disestablish, arg);
462dfbd550eSriastradh
46370a01ab8Sad /* Clear the handler on each CPU. */
46470a01ab8Sad mutex_enter(&softint_lock);
46570a01ab8Sad for (CPU_INFO_FOREACH(cii, ci)) {
46670a01ab8Sad sc = ci->ci_data.cpu_softcpu;
46770a01ab8Sad sh = (softhand_t *)((uint8_t *)sc + offset);
46870a01ab8Sad KASSERT(sh->sh_func != NULL);
46970a01ab8Sad sh->sh_func = NULL;
47070a01ab8Sad }
4714b293a84Sad mutex_exit(&softint_lock);
472342d5fc9Sad }
473342d5fc9Sad
474342d5fc9Sad /*
475342d5fc9Sad * softint_schedule:
476342d5fc9Sad *
477342d5fc9Sad * Trigger a software interrupt. Must be called from a hardware
478342d5fc9Sad * interrupt handler, or with preemption disabled (since we are
479342d5fc9Sad * using the value of curcpu()).
480342d5fc9Sad */
481342d5fc9Sad void
softint_schedule(void * arg)482342d5fc9Sad softint_schedule(void *arg)
483342d5fc9Sad {
4844b293a84Sad softhand_t *sh;
4854b293a84Sad softint_t *si;
4864b293a84Sad uintptr_t offset;
4874b293a84Sad int s;
488342d5fc9Sad
489dfbd550eSriastradh SDT_PROBE2(sdt, kernel, softint, schedule, arg, /*ci*/NULL);
490dfbd550eSriastradh
4919e1828ffSad /*
4929e1828ffSad * If this assert fires, rather than disabling preemption explicitly
4939e1828ffSad * to make it stop, consider that you are probably using a softint
4949e1828ffSad * when you don't need to.
4959e1828ffSad */
4964c7ba244Sad KASSERT(kpreempt_disabled());
4974c7ba244Sad
4984b293a84Sad /* Find the handler record for this CPU. */
4994b293a84Sad offset = (uintptr_t)arg;
5000dec6ba3Sriastradh KASSERT(offset != 0);
5010dec6ba3Sriastradh KASSERTMSG(offset < softint_bytes, "%"PRIuPTR" %u",
50225fcead2Smatt offset, softint_bytes);
5034b293a84Sad sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset);
5044b293a84Sad
5054b293a84Sad /* If it's already pending there's nothing to do. */
506b7a7b9b8Smatt if ((sh->sh_flags & SOFTINT_PENDING) != 0) {
5074b293a84Sad return;
508b7a7b9b8Smatt }
5094b293a84Sad
5104b293a84Sad /*
5114b293a84Sad * Enqueue the handler into the LWP's pending list.
5124b293a84Sad * If the LWP is completely idle, then make it run.
5134b293a84Sad */
5144b293a84Sad s = splhigh();
51570a01ab8Sad if ((sh->sh_flags & SOFTINT_PENDING) == 0) {
5164b293a84Sad si = sh->sh_isr;
51770a01ab8Sad sh->sh_flags |= SOFTINT_PENDING;
5184b293a84Sad SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q);
5194b293a84Sad if (si->si_active == 0) {
5204b293a84Sad si->si_active = 1;
5214b293a84Sad softint_trigger(si->si_machdep);
5224b293a84Sad }
5234b293a84Sad }
5244b293a84Sad splx(s);
5254b293a84Sad }
5264b293a84Sad
5274b293a84Sad /*
5280132815bSrmind * softint_schedule_cpu:
5290132815bSrmind *
5300132815bSrmind * Trigger a software interrupt on a target CPU. This invokes
5310132815bSrmind * softint_schedule() for the local CPU or send an IPI to invoke
5320132815bSrmind * this routine on the remote CPU. Preemption must be disabled.
5330132815bSrmind */
5340132815bSrmind void
softint_schedule_cpu(void * arg,struct cpu_info * ci)5350132815bSrmind softint_schedule_cpu(void *arg, struct cpu_info *ci)
5360132815bSrmind {
5370132815bSrmind KASSERT(kpreempt_disabled());
5380132815bSrmind
5390132815bSrmind if (curcpu() != ci) {
5400132815bSrmind const softcpu_t *sc = ci->ci_data.cpu_softcpu;
5410132815bSrmind const uintptr_t offset = (uintptr_t)arg;
5420132815bSrmind const softhand_t *sh;
5430132815bSrmind
544dfbd550eSriastradh SDT_PROBE2(sdt, kernel, softint, schedule, arg, ci);
5450132815bSrmind sh = (const softhand_t *)((const uint8_t *)sc + offset);
5460132815bSrmind KASSERT((sh->sh_flags & SOFTINT_RCPU) != 0);
5470132815bSrmind ipi_trigger(sh->sh_ipi_id, ci);
5480132815bSrmind return;
5490132815bSrmind }
5500132815bSrmind
5510132815bSrmind /* Just a local CPU. */
5520132815bSrmind softint_schedule(arg);
5530132815bSrmind }
5540132815bSrmind
5550132815bSrmind /*
5564b293a84Sad * softint_execute:
5574b293a84Sad *
5584b293a84Sad * Invoke handlers for the specified soft interrupt.
5594b293a84Sad * Must be entered at splhigh. Will drop the priority
5604b293a84Sad * to the level specified, but returns back at splhigh.
5614b293a84Sad */
5624b293a84Sad static inline void
softint_execute(lwp_t * l,int s)563972d99ceSad softint_execute(lwp_t *l, int s)
5644b293a84Sad {
565972d99ceSad softint_t *si = l->l_private;
5664b293a84Sad softhand_t *sh;
5674b293a84Sad
5684b293a84Sad KASSERT(si->si_lwp == curlwp);
5694b293a84Sad KASSERT(si->si_cpu == curcpu());
5704b293a84Sad KASSERT(si->si_lwp->l_wchan == NULL);
5714b293a84Sad KASSERT(si->si_active);
572*04615d56Smrg KASSERTMSG(l->l_nopreempt == 0, "lwp %p nopreempt %d",
573*04615d56Smrg l, l->l_nopreempt);
5744b293a84Sad
5754b293a84Sad /*
5764b293a84Sad * Note: due to priority inheritance we may have interrupted a
5774b293a84Sad * higher priority LWP. Since the soft interrupt must be quick
5784b293a84Sad * and is non-preemptable, we don't bother yielding.
5794b293a84Sad */
5804b293a84Sad
5814b293a84Sad while (!SIMPLEQ_EMPTY(&si->si_q)) {
5824b293a84Sad /*
5834b293a84Sad * Pick the longest waiting handler to run. We block
5844b293a84Sad * interrupts but do not lock in order to do this, as
5854b293a84Sad * we are protecting against the local CPU only.
5864b293a84Sad */
5874b293a84Sad sh = SIMPLEQ_FIRST(&si->si_q);
5884b293a84Sad SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q);
58970a01ab8Sad KASSERT((sh->sh_flags & SOFTINT_PENDING) != 0);
590972d99ceSad sh->sh_flags ^= SOFTINT_PENDING;
5914b293a84Sad splx(s);
5924b293a84Sad
5934b293a84Sad /* Run the handler. */
594dfbd550eSriastradh SDT_PROBE4(sdt, kernel, softint, entry,
595dfbd550eSriastradh ((const char *)sh -
596dfbd550eSriastradh (const char *)curcpu()->ci_data.cpu_softcpu),
597dfbd550eSriastradh sh->sh_func, sh->sh_arg, sh->sh_flags);
598cddaba8dSad if (__predict_true((sh->sh_flags & SOFTINT_MPSAFE) != 0)) {
5994b293a84Sad (*sh->sh_func)(sh->sh_arg);
600cddaba8dSad } else {
601cddaba8dSad KERNEL_LOCK(1, l);
602cddaba8dSad (*sh->sh_func)(sh->sh_arg);
603cddaba8dSad KERNEL_UNLOCK_ONE(l);
604cddaba8dSad }
605dfbd550eSriastradh SDT_PROBE4(sdt, kernel, softint, return,
606dfbd550eSriastradh ((const char *)sh -
607dfbd550eSriastradh (const char *)curcpu()->ci_data.cpu_softcpu),
608dfbd550eSriastradh sh->sh_func, sh->sh_arg, sh->sh_flags);
6094b293a84Sad
6102ca6f25dSrmind /* Diagnostic: check that spin-locks have not leaked. */
6112ca6f25dSrmind KASSERTMSG(curcpu()->ci_mtx_count == 0,
612325494feSjym "%s: ci_mtx_count (%d) != 0, sh_func %p\n",
613325494feSjym __func__, curcpu()->ci_mtx_count, sh->sh_func);
6143843688cSozaki-r /* Diagnostic: check that psrefs have not leaked. */
6153843688cSozaki-r KASSERTMSG(l->l_psrefs == 0, "%s: l_psrefs=%d, sh_func=%p\n",
6163843688cSozaki-r __func__, l->l_psrefs, sh->sh_func);
617fb698d22Sriastradh /* Diagnostic: check that biglocks have not leaked. */
618fb698d22Sriastradh KASSERTMSG(l->l_blcnt == 0,
619fb698d22Sriastradh "%s: sh_func=%p leaked %d biglocks",
620fb698d22Sriastradh __func__, sh->sh_func, curlwp->l_blcnt);
621*04615d56Smrg /* Diagnostic: check that LWP nopreempt remains zero. */
622*04615d56Smrg KASSERTMSG(l->l_nopreempt == 0,
623*04615d56Smrg "%s: lwp %p nopreempt %d func %p",
624*04615d56Smrg __func__, l, l->l_nopreempt, sh->sh_func);
6252ca6f25dSrmind
6264b293a84Sad (void)splhigh();
6274b293a84Sad }
6284b293a84Sad
6297fc219a5Sozaki-r PSREF_DEBUG_BARRIER();
6307fc219a5Sozaki-r
631a98966d3Sad CPU_COUNT(CPU_COUNT_NSOFT, 1);
6324b293a84Sad
633a0bce9fdSad KASSERT(si->si_cpu == curcpu());
634a0bce9fdSad KASSERT(si->si_lwp->l_wchan == NULL);
635a0bce9fdSad KASSERT(si->si_active);
6364b293a84Sad si->si_evcnt.ev_count++;
6374b293a84Sad si->si_active = 0;
638342d5fc9Sad }
639342d5fc9Sad
640342d5fc9Sad /*
641342d5fc9Sad * softint_block:
642342d5fc9Sad *
643342d5fc9Sad * Update statistics when the soft interrupt blocks.
644342d5fc9Sad */
645342d5fc9Sad void
softint_block(lwp_t * l)646342d5fc9Sad softint_block(lwp_t *l)
647342d5fc9Sad {
6484b293a84Sad softint_t *si = l->l_private;
649342d5fc9Sad
6504b293a84Sad KASSERT((l->l_pflag & LP_INTR) != 0);
6514b293a84Sad si->si_evcnt_block.ev_count++;
6524b293a84Sad }
6534b293a84Sad
6544b293a84Sad #ifndef __HAVE_FAST_SOFTINTS
6554b293a84Sad
656b96eb5aeSad #ifdef __HAVE_PREEMPTION
657b96eb5aeSad #error __HAVE_PREEMPTION requires __HAVE_FAST_SOFTINTS
6584c7ba244Sad #endif
6594c7ba244Sad
6604b293a84Sad /*
6614b293a84Sad * softint_init_md:
6624b293a84Sad *
6634b293a84Sad * Slow path: perform machine-dependent initialization.
6644b293a84Sad */
6654b293a84Sad void
softint_init_md(lwp_t * l,u_int level,uintptr_t * machdep)6664b293a84Sad softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
6674b293a84Sad {
66811ba4e18Sad struct proc *p;
6694b293a84Sad softint_t *si;
6704b293a84Sad
6714b293a84Sad *machdep = (1 << level);
6724b293a84Sad si = l->l_private;
67311ba4e18Sad p = l->l_proc;
6744b293a84Sad
67511ba4e18Sad mutex_enter(p->p_lock);
6764b293a84Sad lwp_lock(l);
6774b293a84Sad /* Cheat and make the KASSERT in softint_thread() happy. */
6784b293a84Sad si->si_active = 1;
67911ba4e18Sad setrunnable(l);
68011ba4e18Sad /* LWP now unlocked */
68111ba4e18Sad mutex_exit(p->p_lock);
6824b293a84Sad }
6834b293a84Sad
6844b293a84Sad /*
6854b293a84Sad * softint_trigger:
6864b293a84Sad *
6874b293a84Sad * Slow path: cause a soft interrupt handler to begin executing.
6884b293a84Sad * Called at IPL_HIGH.
6894b293a84Sad */
6904b293a84Sad void
softint_trigger(uintptr_t machdep)6914b293a84Sad softint_trigger(uintptr_t machdep)
6924b293a84Sad {
6934b293a84Sad struct cpu_info *ci;
6944b293a84Sad lwp_t *l;
6954b293a84Sad
69665e19688Sad ci = curcpu();
6974b293a84Sad ci->ci_data.cpu_softints |= machdep;
69857eb66c6Sad l = ci->ci_onproc;
699a977daeaSad
700a977daeaSad /*
701a977daeaSad * Arrange for mi_switch() to be called. If called from interrupt
702a977daeaSad * mode, we don't know if curlwp is executing in kernel or user, so
703a977daeaSad * post an AST and have it take a trip through userret(). If not in
704a977daeaSad * interrupt mode, curlwp is running in kernel and will notice the
705a977daeaSad * resched soon enough; avoid the AST.
706a977daeaSad */
7074b293a84Sad if (l == ci->ci_data.cpu_idlelwp) {
708a730537eSad atomic_or_uint(&ci->ci_want_resched,
709a730537eSad RESCHED_IDLE | RESCHED_UPREEMPT);
7104b293a84Sad } else {
711a977daeaSad atomic_or_uint(&ci->ci_want_resched, RESCHED_UPREEMPT);
712a977daeaSad if (cpu_intr_p()) {
71365e19688Sad cpu_signotify(l);
7144b293a84Sad }
7154b293a84Sad }
716a977daeaSad }
7174b293a84Sad
7184b293a84Sad /*
7194b293a84Sad * softint_thread:
7204b293a84Sad *
7214b293a84Sad * Slow path: MI software interrupt dispatch.
7224b293a84Sad */
7234b293a84Sad void
softint_thread(void * cookie)7244b293a84Sad softint_thread(void *cookie)
7254b293a84Sad {
7264b293a84Sad softint_t *si;
7274b293a84Sad lwp_t *l;
7284b293a84Sad int s;
7294b293a84Sad
7304b293a84Sad l = curlwp;
7314b293a84Sad si = l->l_private;
7324b293a84Sad
7334b293a84Sad for (;;) {
7349e1828ffSad /* Clear pending status and run it. */
7354b293a84Sad s = splhigh();
7364b293a84Sad l->l_cpu->ci_data.cpu_softints &= ~si->si_machdep;
737972d99ceSad softint_execute(l, s);
7384b293a84Sad splx(s);
7394b293a84Sad
7409e1828ffSad /* Interrupts allowed to run again before switching. */
7414b293a84Sad lwp_lock(l);
7424b293a84Sad l->l_stat = LSIDL;
7434477d28dSad spc_lock(l->l_cpu);
7444b293a84Sad mi_switch(l);
7454b293a84Sad }
746342d5fc9Sad }
747d831186dSad
748d831186dSad /*
749d831186dSad * softint_picklwp:
750d831186dSad *
751d831186dSad * Slow path: called from mi_switch() to pick the highest priority
752d831186dSad * soft interrupt LWP that needs to run.
753d831186dSad */
754d831186dSad lwp_t *
softint_picklwp(void)755d831186dSad softint_picklwp(void)
756d831186dSad {
7574b293a84Sad struct cpu_info *ci;
7584b293a84Sad u_int mask;
7594b293a84Sad softint_t *si;
7604b293a84Sad lwp_t *l;
761d831186dSad
7624b293a84Sad ci = curcpu();
7634b293a84Sad si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
7644b293a84Sad mask = ci->ci_data.cpu_softints;
7654b293a84Sad
7664b293a84Sad if ((mask & (1 << SOFTINT_SERIAL)) != 0) {
7674b293a84Sad l = si[SOFTINT_SERIAL].si_lwp;
7684b293a84Sad } else if ((mask & (1 << SOFTINT_NET)) != 0) {
7694b293a84Sad l = si[SOFTINT_NET].si_lwp;
7704b293a84Sad } else if ((mask & (1 << SOFTINT_BIO)) != 0) {
7714b293a84Sad l = si[SOFTINT_BIO].si_lwp;
7724b293a84Sad } else if ((mask & (1 << SOFTINT_CLOCK)) != 0) {
7734b293a84Sad l = si[SOFTINT_CLOCK].si_lwp;
7744b293a84Sad } else {
775d831186dSad panic("softint_picklwp");
776d831186dSad }
777d831186dSad
7784b293a84Sad return l;
7794b293a84Sad }
7804b293a84Sad
7814b293a84Sad #else /* !__HAVE_FAST_SOFTINTS */
7824b293a84Sad
7834b293a84Sad /*
7844b293a84Sad * softint_thread:
7854b293a84Sad *
7864b293a84Sad * Fast path: the LWP is switched to without restoring any state,
7874b293a84Sad * so we should not arrive here - there is a direct handoff between
7884b293a84Sad * the interrupt stub and softint_dispatch().
7894b293a84Sad */
7904b293a84Sad void
softint_thread(void * cookie)7914b293a84Sad softint_thread(void *cookie)
7924b293a84Sad {
7934b293a84Sad
7944b293a84Sad panic("softint_thread");
7954b293a84Sad }
7964b293a84Sad
7974b293a84Sad /*
7984b293a84Sad * softint_dispatch:
7994b293a84Sad *
8004b293a84Sad * Fast path: entry point from machine-dependent code.
8014b293a84Sad */
8024b293a84Sad void
softint_dispatch(lwp_t * pinned,int s)8034b293a84Sad softint_dispatch(lwp_t *pinned, int s)
8044b293a84Sad {
805949e16d9Syamt struct bintime now;
8064b293a84Sad u_int timing;
8074b293a84Sad lwp_t *l;
8084b293a84Sad
809f934237fSad #ifdef DIAGNOSTIC
81082002773Sad if ((pinned->l_pflag & LP_RUNNING) == 0 || curlwp->l_stat != LSIDL) {
811f934237fSad struct lwp *onproc = curcpu()->ci_onproc;
812f934237fSad int s2 = splhigh();
813f934237fSad printf("curcpu=%d, spl=%d curspl=%d\n"
814f934237fSad "onproc=%p => l_stat=%d l_flag=%08x l_cpu=%d\n"
815f934237fSad "curlwp=%p => l_stat=%d l_flag=%08x l_cpu=%d\n"
816f934237fSad "pinned=%p => l_stat=%d l_flag=%08x l_cpu=%d\n",
817f934237fSad cpu_index(curcpu()), s, s2, onproc, onproc->l_stat,
818f934237fSad onproc->l_flag, cpu_index(onproc->l_cpu), curlwp,
819f934237fSad curlwp->l_stat, curlwp->l_flag,
820f934237fSad cpu_index(curlwp->l_cpu), pinned, pinned->l_stat,
821f934237fSad pinned->l_flag, cpu_index(pinned->l_cpu));
822f934237fSad splx(s2);
823f934237fSad panic("softint screwup");
824f934237fSad }
825f934237fSad #endif
826f934237fSad
8274b293a84Sad /*
8284b293a84Sad * Note the interrupted LWP, and mark the current LWP as running
8294b293a84Sad * before proceeding. Although this must as a rule be done with
8304b293a84Sad * the LWP locked, at this point no external agents will want to
8314b293a84Sad * modify the interrupt LWP's state.
8324b293a84Sad */
8332ddceed1Sad timing = softint_timing;
834972d99ceSad l = curlwp;
8354b293a84Sad l->l_switchto = pinned;
8364b293a84Sad l->l_stat = LSONPROC;
8374b293a84Sad
8384b293a84Sad /*
8394b293a84Sad * Dispatch the interrupt. If softints are being timed, charge
8404b293a84Sad * for it.
8414b293a84Sad */
842b2d41f4aSad if (timing) {
843caca6daaSyamt binuptime(&l->l_stime);
844b2d41f4aSad membar_producer(); /* for calcru */
8452ddceed1Sad l->l_pflag |= LP_TIMEINTR;
846b2d41f4aSad }
84782002773Sad l->l_pflag |= LP_RUNNING;
848972d99ceSad softint_execute(l, s);
8494b293a84Sad if (timing) {
850caca6daaSyamt binuptime(&now);
8514b293a84Sad updatertime(l, &now);
8522feabc38Sad l->l_pflag &= ~LP_TIMEINTR;
8534b293a84Sad }
8544b293a84Sad
8554b293a84Sad /*
8564b293a84Sad * If we blocked while handling the interrupt, the pinned LWP is
857972d99ceSad * gone and we are now running as a kthread, so find another LWP to
858972d99ceSad * run. softint_dispatch() won't be reentered until the priority is
859972d99ceSad * finally dropped to IPL_NONE on entry to the next LWP on this CPU.
8604b293a84Sad */
8614b293a84Sad l->l_stat = LSIDL;
8624b293a84Sad if (l->l_switchto == NULL) {
8632ddceed1Sad lwp_lock(l);
8642ddceed1Sad spc_lock(l->l_cpu);
8652ddceed1Sad mi_switch(l);
8664b293a84Sad /* NOTREACHED */
8674b293a84Sad }
8684b293a84Sad l->l_switchto = NULL;
86982002773Sad l->l_pflag &= ~LP_RUNNING;
8704b293a84Sad }
8714b293a84Sad
8724b293a84Sad #endif /* !__HAVE_FAST_SOFTINTS */
873