Home | History | Annotate | Line # | Download | only in kern
kern_softint.c revision 1.1.2.1
      1  1.1.2.1  ad /*	$NetBSD: kern_softint.c,v 1.1.2.1 2007/06/17 21:31:27 ad Exp $	*/
      2  1.1.2.1  ad 
      3  1.1.2.1  ad /*-
      4  1.1.2.1  ad  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1.2.1  ad  * All rights reserved.
      6  1.1.2.1  ad  *
      7  1.1.2.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1.2.1  ad  * by Andrew Doran.
      9  1.1.2.1  ad  *
     10  1.1.2.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1.2.1  ad  * modification, are permitted provided that the following conditions
     12  1.1.2.1  ad  * are met:
     13  1.1.2.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1.2.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.2.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.2.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1.2.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1.2.1  ad  *    must display the following acknowledgement:
     20  1.1.2.1  ad  *	This product includes software developed by the NetBSD
     21  1.1.2.1  ad  *	Foundation, Inc. and its contributors.
     22  1.1.2.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1.2.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1.2.1  ad  *    from this software without specific prior written permission.
     25  1.1.2.1  ad  *
     26  1.1.2.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1.2.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1.2.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1.2.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1.2.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1.2.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1.2.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1.2.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1.2.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1.2.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1.2.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1.2.1  ad  */
     38  1.1.2.1  ad 
     39  1.1.2.1  ad /*
     40  1.1.2.1  ad  * Soft interrupt implementation.  XXX blurb
     41  1.1.2.1  ad  *
     42  1.1.2.1  ad  * The !__HAVE_FAST_SOFTINTS case assumes splhigh == splsched.
     43  1.1.2.1  ad  */
     44  1.1.2.1  ad 
     45  1.1.2.1  ad #include <sys/cdefs.h>
     46  1.1.2.1  ad __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.1.2.1 2007/06/17 21:31:27 ad Exp $");
     47  1.1.2.1  ad 
     48  1.1.2.1  ad #include <sys/param.h>
     49  1.1.2.1  ad #include <sys/malloc.h>
     50  1.1.2.1  ad #include <sys/proc.h>
     51  1.1.2.1  ad #include <sys/intr.h>
     52  1.1.2.1  ad #include <sys/mutex.h>
     53  1.1.2.1  ad #include <sys/kthread.h>
     54  1.1.2.1  ad #include <sys/evcnt.h>
     55  1.1.2.1  ad #include <sys/cpu.h>
     56  1.1.2.1  ad 
     57  1.1.2.1  ad #include <net/netisr.h>
     58  1.1.2.1  ad 
     59  1.1.2.1  ad #include <uvm/uvm_extern.h>
     60  1.1.2.1  ad 
     61  1.1.2.1  ad #define	PRI_SOFTCLOCK	PRI_INTERRUPT
     62  1.1.2.1  ad #define	PRI_SOFTBIO	(PRI_INTERRUPT + 4)
     63  1.1.2.1  ad #define	PRI_SOFTNET	(PRI_INTERRUPT + 8)
     64  1.1.2.1  ad #define	PRI_SOFTSERIAL	(PRI_INTERRUPT + 12)
     65  1.1.2.1  ad 
     66  1.1.2.1  ad /* This could overlap with signal info in struct lwp. */
     67  1.1.2.1  ad typedef struct softint {
     68  1.1.2.1  ad 	TAILQ_HEAD(, softhand)	si_q;
     69  1.1.2.1  ad 	struct lwp		*si_lwp;
     70  1.1.2.1  ad 	struct cpu_info		*si_cpu;
     71  1.1.2.1  ad 	uintptr_t		si_machdep;
     72  1.1.2.1  ad 	struct evcnt		si_evcnt;
     73  1.1.2.1  ad 	int			si_active;
     74  1.1.2.1  ad } softint_t;
     75  1.1.2.1  ad 
     76  1.1.2.1  ad typedef struct softhand {
     77  1.1.2.1  ad 	TAILQ_ENTRY(softhand)	sh_q;
     78  1.1.2.1  ad 	void			(*sh_func)(void *);
     79  1.1.2.1  ad 	void			*sh_arg;
     80  1.1.2.1  ad 	softint_t		*sh_isr;
     81  1.1.2.1  ad 	u_int			sh_pending;
     82  1.1.2.1  ad 	u_int			sh_flags;
     83  1.1.2.1  ad } softhand_t;
     84  1.1.2.1  ad 
     85  1.1.2.1  ad typedef struct softcpu {
     86  1.1.2.1  ad 	struct cpu_info		*sc_cpu;
     87  1.1.2.1  ad 	softint_t		sc_int[SOFTINT_COUNT];
     88  1.1.2.1  ad 	softhand_t		sc_hand[1];
     89  1.1.2.1  ad } softcpu_t;
     90  1.1.2.1  ad 
     91  1.1.2.1  ad static void	softint_thread(void *);
     92  1.1.2.1  ad static void	softint_netisr(void *);
     93  1.1.2.1  ad 
     94  1.1.2.1  ad u_int		softint_bytes = 8192;
     95  1.1.2.1  ad static u_int	softint_max;
     96  1.1.2.1  ad static kmutex_t	softint_lock;
     97  1.1.2.1  ad static void	*softint_netisr_sih;
     98  1.1.2.1  ad 
     99  1.1.2.1  ad /*
    100  1.1.2.1  ad  * softint_init_isr:
    101  1.1.2.1  ad  *
    102  1.1.2.1  ad  *	Initialize a single interrupt level for a single CPU.
    103  1.1.2.1  ad  */
    104  1.1.2.1  ad static void
    105  1.1.2.1  ad softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
    106  1.1.2.1  ad {
    107  1.1.2.1  ad 	struct cpu_info *ci;
    108  1.1.2.1  ad 	softint_t *si;
    109  1.1.2.1  ad 	int error;
    110  1.1.2.1  ad 
    111  1.1.2.1  ad 	si = &sc->sc_int[level];
    112  1.1.2.1  ad 	ci = sc->sc_cpu;
    113  1.1.2.1  ad 	si->si_cpu = ci;
    114  1.1.2.1  ad 
    115  1.1.2.1  ad 	TAILQ_INIT(&si->si_q);
    116  1.1.2.1  ad 
    117  1.1.2.1  ad 	error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
    118  1.1.2.1  ad 	    KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
    119  1.1.2.1  ad 	    "soft%s/%d", desc, (int)ci->ci_cpuid);
    120  1.1.2.1  ad 	if (error != 0)
    121  1.1.2.1  ad 		panic("softint_init_isr: error %d", error);
    122  1.1.2.1  ad 
    123  1.1.2.1  ad 	evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
    124  1.1.2.1  ad 	   "cpu", si->si_lwp->l_name);
    125  1.1.2.1  ad 
    126  1.1.2.1  ad 	softint_init_md(si->si_lwp, level, &si->si_machdep, si);
    127  1.1.2.1  ad }
    128  1.1.2.1  ad 
    129  1.1.2.1  ad /*
    130  1.1.2.1  ad  * softint_init_md:
    131  1.1.2.1  ad  *
    132  1.1.2.1  ad  *	Perform machine-dependent initialization.  Arguments:
    133  1.1.2.1  ad  *
    134  1.1.2.1  ad  *	l
    135  1.1.2.1  ad  *
    136  1.1.2.1  ad  *	    LWP to handle the interrupt
    137  1.1.2.1  ad  *
    138  1.1.2.1  ad  *	level
    139  1.1.2.1  ad  *
    140  1.1.2.1  ad  *	    Symbolic level: SOFTINT_*
    141  1.1.2.1  ad  *
    142  1.1.2.1  ad  *	machdep
    143  1.1.2.1  ad  *
    144  1.1.2.1  ad  *	    Private value for machine dependent code,
    145  1.1.2.1  ad  *	    passed by MI code to softint_trigger().
    146  1.1.2.1  ad  *
    147  1.1.2.1  ad  *	cookie
    148  1.1.2.1  ad  *
    149  1.1.2.1  ad  *	    Value to be passed to softint_execute() by
    150  1.1.2.1  ad  *	    MD code when an interrupt is being handled.
    151  1.1.2.1  ad  */
    152  1.1.2.1  ad #ifndef __HAVE_FAST_SOFTINTS
    153  1.1.2.1  ad void
    154  1.1.2.1  ad softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep, void *cookie)
    155  1.1.2.1  ad {
    156  1.1.2.1  ad 
    157  1.1.2.1  ad 	*machdep = (lwp_t)l;
    158  1.1.2.1  ad 
    159  1.1.2.1  ad 	lwp_lock(l);
    160  1.1.2.1  ad 	/* Cheat and make the KASSERT in softint_thread() happy. */
    161  1.1.2.1  ad 	((softint_t *)cookie)->si_active = 1;
    162  1.1.2.1  ad 	l->l_stat = LSRUN;
    163  1.1.2.1  ad 	sched_enqueue(l, false);
    164  1.1.2.1  ad 	lwp_unlock(l);
    165  1.1.2.1  ad }
    166  1.1.2.1  ad #endif	/* !__HAVE_FAST_SOFTINTS */
    167  1.1.2.1  ad 
    168  1.1.2.1  ad /*
    169  1.1.2.1  ad  * softint_init:
    170  1.1.2.1  ad  *
    171  1.1.2.1  ad  *	Initialize per-CPU data structures.  Called from mi_cpu_attach().
    172  1.1.2.1  ad  */
    173  1.1.2.1  ad void
    174  1.1.2.1  ad softint_init(struct cpu_info *ci)
    175  1.1.2.1  ad {
    176  1.1.2.1  ad 	static struct cpu_info *first;
    177  1.1.2.1  ad 	softcpu_t *sc, *scfirst;
    178  1.1.2.1  ad 	softhand_t *sh, *shmax;
    179  1.1.2.1  ad 
    180  1.1.2.1  ad 	if (first == NULL) {
    181  1.1.2.1  ad 		first = ci;
    182  1.1.2.1  ad 		mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
    183  1.1.2.1  ad 		softint_bytes = round_page(softint_bytes);
    184  1.1.2.1  ad 		softint_max = (softint_bytes - sizeof(softcpu_t)) /
    185  1.1.2.1  ad 		    sizeof(softhand_t);
    186  1.1.2.1  ad 	}
    187  1.1.2.1  ad 
    188  1.1.2.1  ad 	sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
    189  1.1.2.1  ad 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
    190  1.1.2.1  ad 	if (sc == NULL)
    191  1.1.2.1  ad 		panic("softint_init_cpu: cannot allocate memory");
    192  1.1.2.1  ad 
    193  1.1.2.1  ad 	ci->ci_data.cpu_softcpu = sc;
    194  1.1.2.1  ad 	sc->sc_cpu = ci;
    195  1.1.2.1  ad 
    196  1.1.2.1  ad 	softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET);
    197  1.1.2.1  ad 	softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO);
    198  1.1.2.1  ad 	softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK);
    199  1.1.2.1  ad 	softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL);
    200  1.1.2.1  ad 
    201  1.1.2.1  ad 	if (first != ci) {
    202  1.1.2.1  ad 		/* No need to lock - the system is still cold. */
    203  1.1.2.1  ad 		scfirst = first->ci_data.cpu_softcpu;
    204  1.1.2.1  ad 		sh = sc->sc_hand;
    205  1.1.2.1  ad 		memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
    206  1.1.2.1  ad 
    207  1.1.2.1  ad 		/* Update pointers for this CPU. */
    208  1.1.2.1  ad 		for (shmax = sh + softint_max; sh < shmax; sh++) {
    209  1.1.2.1  ad 			if (sh->sh_func == NULL)
    210  1.1.2.1  ad 				continue;
    211  1.1.2.1  ad 			sh->sh_isr =
    212  1.1.2.1  ad 			    &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
    213  1.1.2.1  ad 		}
    214  1.1.2.1  ad 	} else {
    215  1.1.2.1  ad 		/* Establish a handler for legacy net interrupts. */
    216  1.1.2.1  ad 		softint_netisr_sih = softint_establish(SOFTINT_NET,
    217  1.1.2.1  ad 		    softint_netisr, NULL);
    218  1.1.2.1  ad 		KASSERT(softint_netisr_sih != NULL);
    219  1.1.2.1  ad 	}
    220  1.1.2.1  ad }
    221  1.1.2.1  ad 
    222  1.1.2.1  ad /*
    223  1.1.2.1  ad  * softint_establish:
    224  1.1.2.1  ad  *
    225  1.1.2.1  ad  *	Register a software interrupt handler.
    226  1.1.2.1  ad  */
    227  1.1.2.1  ad void *
    228  1.1.2.1  ad softint_establish(u_int flags, void (*func)(void *), void *arg)
    229  1.1.2.1  ad {
    230  1.1.2.1  ad 	CPU_INFO_ITERATOR cii;
    231  1.1.2.1  ad 	struct cpu_info *ci;
    232  1.1.2.1  ad 	softcpu_t *sc;
    233  1.1.2.1  ad 	softhand_t *sh;
    234  1.1.2.1  ad 	u_int level;
    235  1.1.2.1  ad 	u_int index;
    236  1.1.2.1  ad 
    237  1.1.2.1  ad 	level = (flags & SOFTINT_LVLMASK);
    238  1.1.2.1  ad 	KASSERT(level < SOFTINT_COUNT);
    239  1.1.2.1  ad 
    240  1.1.2.1  ad 	mutex_enter(&softint_lock);
    241  1.1.2.1  ad 
    242  1.1.2.1  ad 	/* Find a free slot. */
    243  1.1.2.1  ad 	sc = curcpu()->ci_data.cpu_softcpu;
    244  1.1.2.1  ad 	for (index = 1; index < softint_max; index++)
    245  1.1.2.1  ad 		if (sc->sc_hand[index].sh_func == NULL)
    246  1.1.2.1  ad 			break;
    247  1.1.2.1  ad 	if (index == softint_max) {
    248  1.1.2.1  ad 		mutex_exit(&softint_lock);
    249  1.1.2.1  ad 		printf("WARNING: softint_establish: table full, "
    250  1.1.2.1  ad 		    "increase softint_bytes\n");
    251  1.1.2.1  ad 		return NULL;
    252  1.1.2.1  ad 	}
    253  1.1.2.1  ad 
    254  1.1.2.1  ad 	/* Set up the handler on each CPU. */
    255  1.1.2.1  ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    256  1.1.2.1  ad 		sc = ci->ci_data.cpu_softcpu;
    257  1.1.2.1  ad 		sh = &sc->sc_hand[index];
    258  1.1.2.1  ad 
    259  1.1.2.1  ad 		sh->sh_isr = &sc->sc_int[level];
    260  1.1.2.1  ad 		sh->sh_func = func;
    261  1.1.2.1  ad 		sh->sh_arg = arg;
    262  1.1.2.1  ad 		sh->sh_flags = flags;
    263  1.1.2.1  ad 		sh->sh_pending = 0;
    264  1.1.2.1  ad 	}
    265  1.1.2.1  ad 
    266  1.1.2.1  ad 	mutex_exit(&softint_lock);
    267  1.1.2.1  ad 
    268  1.1.2.1  ad 	return (void *)index;
    269  1.1.2.1  ad }
    270  1.1.2.1  ad 
    271  1.1.2.1  ad /*
    272  1.1.2.1  ad  * softint_disestablish:
    273  1.1.2.1  ad  *
    274  1.1.2.1  ad  *	Unregister a software interrupt handler.
    275  1.1.2.1  ad  */
    276  1.1.2.1  ad void
    277  1.1.2.1  ad softint_disestablish(void *arg)
    278  1.1.2.1  ad {
    279  1.1.2.1  ad 	CPU_INFO_ITERATOR cii;
    280  1.1.2.1  ad 	struct cpu_info *ci;
    281  1.1.2.1  ad 	softcpu_t *sc;
    282  1.1.2.1  ad 	softhand_t *sh;
    283  1.1.2.1  ad 	u_int index;
    284  1.1.2.1  ad 
    285  1.1.2.1  ad 	index = (u_int)arg;
    286  1.1.2.1  ad 	KASSERT(index != 0 && index < softint_max);
    287  1.1.2.1  ad 
    288  1.1.2.1  ad 	mutex_enter(&softint_lock);
    289  1.1.2.1  ad 
    290  1.1.2.1  ad 	/* Set up the handler on each CPU. */
    291  1.1.2.1  ad 	for (CPU_INFO_FOREACH(cii, ci)) {
    292  1.1.2.1  ad 		sc = ci->ci_data.cpu_softcpu;
    293  1.1.2.1  ad 		sh = &sc->sc_hand[index];
    294  1.1.2.1  ad 		KASSERT(sh->sh_func != NULL);
    295  1.1.2.1  ad 		KASSERT(sh->sh_pending == 0);
    296  1.1.2.1  ad 		sh->sh_func = NULL;
    297  1.1.2.1  ad 	}
    298  1.1.2.1  ad 
    299  1.1.2.1  ad 	mutex_exit(&softint_lock);
    300  1.1.2.1  ad }
    301  1.1.2.1  ad 
    302  1.1.2.1  ad /*
    303  1.1.2.1  ad  * softint_trigger:
    304  1.1.2.1  ad  *
    305  1.1.2.1  ad  *	Cause a soft interrupt handler to begin executing.
    306  1.1.2.1  ad  */
    307  1.1.2.1  ad #ifndef __HAVE_FAST_SOFTINTS
    308  1.1.2.1  ad inline void
    309  1.1.2.1  ad softint_trigger(uintptr_t machdep)
    310  1.1.2.1  ad {
    311  1.1.2.1  ad 	struct cpu_info *ci;
    312  1.1.2.1  ad 	lwp_t *l;
    313  1.1.2.1  ad 
    314  1.1.2.1  ad 	l = (lwp_t *)machdep;
    315  1.1.2.1  ad 	ci = l->l_cpu;
    316  1.1.2.1  ad 
    317  1.1.2.1  ad 	spc_lock(ci);
    318  1.1.2.1  ad 	l->l_mutex = ci->ci_schedstate.spc_mutex;
    319  1.1.2.1  ad 	l->l_stat = LSRUN;
    320  1.1.2.1  ad 	sched_enqueue(l, false);
    321  1.1.2.1  ad 	cpu_need_resched(ci, 1);
    322  1.1.2.1  ad 	spc_unlock(ci);
    323  1.1.2.1  ad }
    324  1.1.2.1  ad #endif	/* __HAVE_FAST_SOFTINTS */
    325  1.1.2.1  ad 
    326  1.1.2.1  ad /*
    327  1.1.2.1  ad  * softint_schedule:
    328  1.1.2.1  ad  *
    329  1.1.2.1  ad  *	Trigger a software interrupt.  Must be called from a hardware
    330  1.1.2.1  ad  *	interrupt handler, or with preemption disabled (since we are
    331  1.1.2.1  ad  *	using the value of curcpu()).
    332  1.1.2.1  ad  */
    333  1.1.2.1  ad void
    334  1.1.2.1  ad softint_schedule(void *arg)
    335  1.1.2.1  ad {
    336  1.1.2.1  ad 	softhand_t *sh;
    337  1.1.2.1  ad 	softint_t *si;
    338  1.1.2.1  ad 	u_int index;
    339  1.1.2.1  ad 	int s;
    340  1.1.2.1  ad 
    341  1.1.2.1  ad 	/* Find the handler record for this CPU. */
    342  1.1.2.1  ad 	index = (u_int)arg;
    343  1.1.2.1  ad 	KASSERT(index != 0 && index < softint_max);
    344  1.1.2.1  ad 	sh = &((softcpu_t *)curcpu()->ci_data.cpu_softcpu)->sc_hand[index];
    345  1.1.2.1  ad 
    346  1.1.2.1  ad 	/* If it's already pending there's nothing to do. */
    347  1.1.2.1  ad 	if (sh->sh_pending)
    348  1.1.2.1  ad 		return;
    349  1.1.2.1  ad 
    350  1.1.2.1  ad 	/*
    351  1.1.2.1  ad 	 * Enqueue the handler into the LWP's pending list.
    352  1.1.2.1  ad 	 * If the LWP is completely idle, then make it run.
    353  1.1.2.1  ad 	 */
    354  1.1.2.1  ad 	s = splhigh();
    355  1.1.2.1  ad 	if (!sh->sh_pending) {
    356  1.1.2.1  ad 		si = sh->sh_isr;
    357  1.1.2.1  ad 		sh->sh_pending = 1;
    358  1.1.2.1  ad 		TAILQ_INSERT_TAIL(&si->si_q, sh, sh_q);
    359  1.1.2.1  ad 		if (si->si_active == 0) {
    360  1.1.2.1  ad 			si->si_active = 1;
    361  1.1.2.1  ad 			softint_trigger(si->si_machdep);
    362  1.1.2.1  ad 		}
    363  1.1.2.1  ad 	}
    364  1.1.2.1  ad 	splx(s);
    365  1.1.2.1  ad }
    366  1.1.2.1  ad 
    367  1.1.2.1  ad /*
    368  1.1.2.1  ad  * softint_thread:
    369  1.1.2.1  ad  *
    370  1.1.2.1  ad  *	MI software interrupt dispatch.  In the __HAVE_FAST_SOFTINTS
    371  1.1.2.1  ad  *	case, the LWP is switched to without restoring any state, so
    372  1.1.2.1  ad  *	we should not arrive here - there is a direct handoff between
    373  1.1.2.1  ad  *	the interrupt stub and softint_execute().
    374  1.1.2.1  ad  */
    375  1.1.2.1  ad void
    376  1.1.2.1  ad softint_thread(void *cookie)
    377  1.1.2.1  ad {
    378  1.1.2.1  ad #ifdef __HAVE_FAST_SOFTINTS
    379  1.1.2.1  ad 	panic("softint_thread");
    380  1.1.2.1  ad #else	/* __HAVE_FAST_SOFTINTS */
    381  1.1.2.1  ad 	lwp_t *l;
    382  1.1.2.1  ad 	int s;
    383  1.1.2.1  ad 
    384  1.1.2.1  ad 	l = curlwp;
    385  1.1.2.1  ad 	s = splhigh();
    386  1.1.2.1  ad 
    387  1.1.2.1  ad 	for (;;) {
    388  1.1.2.1  ad 		softint_execute(cookie, s);
    389  1.1.2.1  ad 
    390  1.1.2.1  ad 		lwp_lock(l);
    391  1.1.2.1  ad 		l->l_stat = LSIDL;
    392  1.1.2.1  ad 		mi_switch(l);
    393  1.1.2.1  ad 	}
    394  1.1.2.1  ad #endif	/* !__HAVE_FAST_SOFTINTS */
    395  1.1.2.1  ad }
    396  1.1.2.1  ad 
    397  1.1.2.1  ad /*
    398  1.1.2.1  ad  * softint_execute:
    399  1.1.2.1  ad  *
    400  1.1.2.1  ad  *	Invoke handlers for the specified soft interrupt.
    401  1.1.2.1  ad  *	Must be entered at splhigh.  Will drop the priority
    402  1.1.2.1  ad  *	to the level specified, but returns back at splhigh.
    403  1.1.2.1  ad  */
    404  1.1.2.1  ad void
    405  1.1.2.1  ad softint_execute(void *cookie, int s)
    406  1.1.2.1  ad {
    407  1.1.2.1  ad 	softint_t *si;
    408  1.1.2.1  ad 	softhand_t *sh;
    409  1.1.2.1  ad 
    410  1.1.2.1  ad 	si = cookie;
    411  1.1.2.1  ad 
    412  1.1.2.1  ad 	KASSERT(si->si_lwp == curlwp);
    413  1.1.2.1  ad 	KASSERT(si->si_cpu == curcpu());
    414  1.1.2.1  ad 	KASSERT(si->si_lwp->l_wchan == NULL);
    415  1.1.2.1  ad 	KASSERT(!TAILQ_EMPTY(&si->si_q));
    416  1.1.2.1  ad 	KASSERT(si->si_active);
    417  1.1.2.1  ad 
    418  1.1.2.1  ad 	while (!TAILQ_EMPTY(&si->si_q)) {
    419  1.1.2.1  ad 		/*
    420  1.1.2.1  ad 		 * Pick the longest waiting handler to run.  We block
    421  1.1.2.1  ad 		 * interrupts but do not lock in order to do this, as
    422  1.1.2.1  ad 		 * we are protecting against the local CPU only.
    423  1.1.2.1  ad 		 */
    424  1.1.2.1  ad 		sh = TAILQ_FIRST(&si->si_q);
    425  1.1.2.1  ad 		TAILQ_REMOVE(&si->si_q, sh, sh_q);
    426  1.1.2.1  ad 		sh->sh_pending = 0;
    427  1.1.2.1  ad 		splx(s);
    428  1.1.2.1  ad 
    429  1.1.2.1  ad 		/* Run the handler. */
    430  1.1.2.1  ad 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
    431  1.1.2.1  ad 			KERNEL_LOCK(1, si->si_lwp);
    432  1.1.2.1  ad 		}
    433  1.1.2.1  ad 		(*sh->sh_func)(sh->sh_arg);
    434  1.1.2.1  ad 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
    435  1.1.2.1  ad 			KERNEL_UNLOCK_ONE(si->si_lwp);
    436  1.1.2.1  ad 		}
    437  1.1.2.1  ad 
    438  1.1.2.1  ad 		(void)splhigh();
    439  1.1.2.1  ad 	}
    440  1.1.2.1  ad 
    441  1.1.2.1  ad 	/*
    442  1.1.2.1  ad 	 * Unlocked, but only for statistics.
    443  1.1.2.1  ad 	 * Should be per-CPU to prevent cache ping-pong.
    444  1.1.2.1  ad 	 */
    445  1.1.2.1  ad 	uvmexp.softs++;
    446  1.1.2.1  ad 
    447  1.1.2.1  ad 	si->si_evcnt.ev_count++;
    448  1.1.2.1  ad 	si->si_active = 0;
    449  1.1.2.1  ad }
    450  1.1.2.1  ad 
    451  1.1.2.1  ad /*
    452  1.1.2.1  ad  * schednetisr:
    453  1.1.2.1  ad  *
    454  1.1.2.1  ad  *	Trigger a legacy network interrupt.  XXX Needs to go away.
    455  1.1.2.1  ad  */
    456  1.1.2.1  ad void
    457  1.1.2.1  ad schednetisr(int isr)
    458  1.1.2.1  ad {
    459  1.1.2.1  ad 	int s;
    460  1.1.2.1  ad 
    461  1.1.2.1  ad 	s = splhigh();
    462  1.1.2.1  ad 	curcpu()->ci_data.cpu_netisrs |= (1 << isr);
    463  1.1.2.1  ad 	softint_schedule(softint_netisr_sih);
    464  1.1.2.1  ad 	splx(s);
    465  1.1.2.1  ad }
    466  1.1.2.1  ad 
    467  1.1.2.1  ad /*
    468  1.1.2.1  ad  * softintr_netisr:
    469  1.1.2.1  ad  *
    470  1.1.2.1  ad  *	Dispatch legacy network interrupts.  XXX Needs to away.
    471  1.1.2.1  ad  */
    472  1.1.2.1  ad static void
    473  1.1.2.1  ad softint_netisr(void *cookie)
    474  1.1.2.1  ad {
    475  1.1.2.1  ad 	struct cpu_info *ci;
    476  1.1.2.1  ad 	int s, bits;
    477  1.1.2.1  ad 
    478  1.1.2.1  ad 	ci = curcpu();
    479  1.1.2.1  ad 
    480  1.1.2.1  ad 	s = splhigh();
    481  1.1.2.1  ad 	bits = ci->ci_data.cpu_netisrs;
    482  1.1.2.1  ad 	ci->ci_data.cpu_netisrs = 0;
    483  1.1.2.1  ad 	splx(s);
    484  1.1.2.1  ad 
    485  1.1.2.1  ad #define	DONETISR(which, func)				\
    486  1.1.2.1  ad 	do {						\
    487  1.1.2.1  ad 		void func(void);			\
    488  1.1.2.1  ad 		if ((bits & (1 << which)) != 0)		\
    489  1.1.2.1  ad 			func();				\
    490  1.1.2.1  ad 	} while(0);
    491  1.1.2.1  ad #include <net/netisr_dispatch.h>
    492  1.1.2.1  ad #undef DONETISR
    493  1.1.2.1  ad }
    494