Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.19.8.1
      1  1.19.8.1       snj /*	$NetBSD: subr_xcall.c,v 1.19.8.1 2018/02/19 18:33:38 snj Exp $	*/
      2       1.2        ad 
      3       1.2        ad /*-
      4      1.12     rmind  * Copyright (c) 2007-2010 The NetBSD Foundation, Inc.
      5       1.2        ad  * All rights reserved.
      6       1.2        ad  *
      7       1.2        ad  * This code is derived from software contributed to The NetBSD Foundation
      8      1.12     rmind  * by Andrew Doran and Mindaugas Rasiukevicius.
      9       1.2        ad  *
     10       1.2        ad  * Redistribution and use in source and binary forms, with or without
     11       1.2        ad  * modification, are permitted provided that the following conditions
     12       1.2        ad  * are met:
     13       1.2        ad  * 1. Redistributions of source code must retain the above copyright
     14       1.2        ad  *    notice, this list of conditions and the following disclaimer.
     15       1.2        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2        ad  *    notice, this list of conditions and the following disclaimer in the
     17       1.2        ad  *    documentation and/or other materials provided with the distribution.
     18       1.2        ad  *
     19       1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2        ad  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2        ad  */
     31       1.2        ad 
     32       1.2        ad /*
     33       1.2        ad  * Cross call support
     34       1.2        ad  *
     35       1.2        ad  * Background
     36       1.2        ad  *
     37       1.2        ad  *	Sometimes it is necessary to modify hardware state that is tied
     38       1.2        ad  *	directly to individual CPUs (such as a CPU's local timer), and
     39       1.2        ad  *	these updates can not be done remotely by another CPU.  The LWP
     40       1.2        ad  *	requesting the update may be unable to guarantee that it will be
     41       1.2        ad  *	running on the CPU where the update must occur, when the update
     42       1.2        ad  *	occurs.
     43       1.2        ad  *
     44       1.2        ad  *	Additionally, it's sometimes necessary to modify per-CPU software
     45       1.2        ad  *	state from a remote CPU.  Where these update operations are so
     46       1.2        ad  *	rare or the access to the per-CPU data so frequent that the cost
     47       1.2        ad  *	of using locking or atomic operations to provide coherency is
     48       1.4        ad  *	prohibitive, another way must be found.
     49       1.2        ad  *
     50       1.2        ad  *	Cross calls help to solve these types of problem by allowing
     51       1.2        ad  *	any CPU in the system to request that an arbitrary function be
     52       1.2        ad  *	executed on any other CPU.
     53       1.2        ad  *
     54       1.2        ad  * Implementation
     55       1.2        ad  *
     56       1.2        ad  *	A slow mechanism for making 'low priority' cross calls is
     57       1.2        ad  *	provided.  The function to be executed runs on the remote CPU
     58       1.2        ad  *	within a bound kthread.  No queueing is provided, and the
     59       1.2        ad  *	implementation uses global state.  The function being called may
     60       1.2        ad  *	block briefly on locks, but in doing so must be careful to not
     61       1.2        ad  *	interfere with other cross calls in the system.  The function is
     62       1.2        ad  *	called with thread context and not from a soft interrupt, so it
     63       1.2        ad  *	can ensure that it is not interrupting other code running on the
     64       1.2        ad  *	CPU, and so has exclusive access to the CPU.  Since this facility
     65       1.2        ad  *	is heavyweight, it's expected that it will not be used often.
     66       1.2        ad  *
     67       1.4        ad  *	Cross calls must not allocate memory, as the pagedaemon uses
     68       1.4        ad  *	them (and memory allocation may need to wait on the pagedaemon).
     69       1.4        ad  *
     70      1.12     rmind  *	A low-overhead mechanism for high priority calls (XC_HIGHPRI) is
     71      1.12     rmind  *	also provided.  The function to be executed runs on a software
     72      1.17     rmind  *	interrupt context, at IPL_SOFTSERIAL level, and is expected to
     73      1.17     rmind  *	be very lightweight, e.g. avoid blocking.
     74       1.2        ad  */
     75      1.17     rmind 
     76       1.2        ad #include <sys/cdefs.h>
     77  1.19.8.1       snj __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.19.8.1 2018/02/19 18:33:38 snj Exp $");
     78       1.2        ad 
     79       1.2        ad #include <sys/types.h>
     80       1.2        ad #include <sys/param.h>
     81       1.2        ad #include <sys/xcall.h>
     82       1.2        ad #include <sys/mutex.h>
     83       1.2        ad #include <sys/condvar.h>
     84       1.2        ad #include <sys/evcnt.h>
     85       1.2        ad #include <sys/kthread.h>
     86       1.3        ad #include <sys/cpu.h>
     87       1.2        ad 
     88      1.14    martin #ifdef _RUMPKERNEL
     89      1.14    martin #include "rump_private.h"
     90      1.14    martin #endif
     91      1.14    martin 
     92      1.12     rmind /* Cross-call state box. */
     93      1.12     rmind typedef struct {
     94      1.12     rmind 	kmutex_t	xc_lock;
     95      1.12     rmind 	kcondvar_t	xc_busy;
     96      1.12     rmind 	xcfunc_t	xc_func;
     97      1.12     rmind 	void *		xc_arg1;
     98      1.12     rmind 	void *		xc_arg2;
     99      1.12     rmind 	uint64_t	xc_headp;
    100      1.12     rmind 	uint64_t	xc_donep;
    101  1.19.8.1       snj 	unsigned int	xc_ipl;
    102      1.12     rmind } xc_state_t;
    103      1.12     rmind 
    104      1.12     rmind /* Bit indicating high (1) or low (0) priority. */
    105      1.12     rmind #define	XC_PRI_BIT	(1ULL << 63)
    106      1.12     rmind 
    107      1.12     rmind /* Low priority xcall structures. */
    108      1.13     rmind static xc_state_t	xc_low_pri	__cacheline_aligned;
    109      1.12     rmind 
    110      1.12     rmind /* High priority xcall structures. */
    111      1.13     rmind static xc_state_t	xc_high_pri	__cacheline_aligned;
    112  1.19.8.1       snj static void *		xc_sihs[4]	__cacheline_aligned;
    113       1.2        ad 
    114      1.12     rmind /* Event counters. */
    115      1.13     rmind static struct evcnt	xc_unicast_ev	__cacheline_aligned;
    116      1.13     rmind static struct evcnt	xc_broadcast_ev	__cacheline_aligned;
    117      1.12     rmind 
    118      1.12     rmind static void		xc_init(void);
    119      1.12     rmind static void		xc_thread(void *);
    120      1.12     rmind 
    121  1.19.8.1       snj static inline uint64_t	xc_highpri(xcfunc_t, void *, void *, struct cpu_info *,
    122  1.19.8.1       snj 			    unsigned int);
    123      1.12     rmind static inline uint64_t	xc_lowpri(xcfunc_t, void *, void *, struct cpu_info *);
    124      1.12     rmind 
    125  1.19.8.1       snj /* The internal form of IPL */
    126  1.19.8.1       snj #define XC_IPL_MASK		0xff00
    127  1.19.8.1       snj /*
    128  1.19.8.1       snj  * Assign 0 to XC_IPL_SOFTSERIAL to treat IPL_SOFTSERIAL as the default value
    129  1.19.8.1       snj  * (just XC_HIGHPRI).
    130  1.19.8.1       snj  */
    131  1.19.8.1       snj #define XC_IPL_SOFTSERIAL	0
    132  1.19.8.1       snj #define XC_IPL_SOFTNET		1
    133  1.19.8.1       snj #define XC_IPL_SOFTBIO		2
    134  1.19.8.1       snj #define XC_IPL_SOFTCLOCK	3
    135  1.19.8.1       snj #define XC_IPL_MAX		XC_IPL_SOFTCLOCK
    136  1.19.8.1       snj 
    137  1.19.8.1       snj CTASSERT(XC_IPL_MAX <= __arraycount(xc_sihs));
    138  1.19.8.1       snj 
    139      1.12     rmind /*
    140      1.12     rmind  * xc_init:
    141      1.12     rmind  *
    142      1.12     rmind  *	Initialize low and high priority cross-call structures.
    143      1.12     rmind  */
    144      1.12     rmind static void
    145      1.12     rmind xc_init(void)
    146      1.12     rmind {
    147      1.12     rmind 	xc_state_t *xclo = &xc_low_pri, *xchi = &xc_high_pri;
    148      1.12     rmind 
    149      1.12     rmind 	memset(xclo, 0, sizeof(xc_state_t));
    150      1.12     rmind 	mutex_init(&xclo->xc_lock, MUTEX_DEFAULT, IPL_NONE);
    151      1.12     rmind 	cv_init(&xclo->xc_busy, "xclocv");
    152      1.12     rmind 
    153      1.12     rmind 	memset(xchi, 0, sizeof(xc_state_t));
    154      1.17     rmind 	mutex_init(&xchi->xc_lock, MUTEX_DEFAULT, IPL_SOFTSERIAL);
    155      1.12     rmind 	cv_init(&xchi->xc_busy, "xchicv");
    156  1.19.8.1       snj 
    157  1.19.8.1       snj 	/* Set up a softint for each IPL_SOFT*. */
    158  1.19.8.1       snj #define SETUP_SOFTINT(xipl, sipl) do {					\
    159  1.19.8.1       snj 		xc_sihs[(xipl)] = softint_establish( (sipl) | SOFTINT_MPSAFE,\
    160  1.19.8.1       snj 		    xc__highpri_intr, NULL);				\
    161  1.19.8.1       snj 		KASSERT(xc_sihs[(xipl)] != NULL);			\
    162  1.19.8.1       snj 	} while (0)
    163  1.19.8.1       snj 
    164  1.19.8.1       snj 	SETUP_SOFTINT(XC_IPL_SOFTSERIAL, SOFTINT_SERIAL);
    165  1.19.8.1       snj 	/*
    166  1.19.8.1       snj 	 * If a IPL_SOFTXXX have the same value of the previous, we don't use
    167  1.19.8.1       snj 	 * the IPL (see xc_encode_ipl).  So we don't need to allocate a softint
    168  1.19.8.1       snj 	 * for it.
    169  1.19.8.1       snj 	 */
    170  1.19.8.1       snj #if IPL_SOFTNET != IPL_SOFTSERIAL
    171  1.19.8.1       snj 	SETUP_SOFTINT(XC_IPL_SOFTNET, SOFTINT_NET);
    172  1.19.8.1       snj #endif
    173  1.19.8.1       snj #if IPL_SOFTBIO != IPL_SOFTNET
    174  1.19.8.1       snj 	SETUP_SOFTINT(XC_IPL_SOFTBIO, SOFTINT_BIO);
    175  1.19.8.1       snj #endif
    176  1.19.8.1       snj #if IPL_SOFTCLOCK != IPL_SOFTBIO
    177  1.19.8.1       snj 	SETUP_SOFTINT(XC_IPL_SOFTCLOCK, SOFTINT_CLOCK);
    178  1.19.8.1       snj #endif
    179  1.19.8.1       snj 
    180  1.19.8.1       snj #undef SETUP_SOFTINT
    181      1.12     rmind 
    182      1.12     rmind 	evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    183      1.12     rmind 	   "crosscall", "unicast");
    184      1.12     rmind 	evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    185      1.12     rmind 	   "crosscall", "broadcast");
    186      1.12     rmind }
    187       1.2        ad 
    188       1.2        ad /*
    189  1.19.8.1       snj  * Encode an IPL to a form that can be embedded into flags of xc_broadcast
    190  1.19.8.1       snj  * or xc_unicast.
    191  1.19.8.1       snj  */
    192  1.19.8.1       snj unsigned int
    193  1.19.8.1       snj xc_encode_ipl(int ipl)
    194  1.19.8.1       snj {
    195  1.19.8.1       snj 
    196  1.19.8.1       snj 	switch (ipl) {
    197  1.19.8.1       snj 	case IPL_SOFTSERIAL:
    198  1.19.8.1       snj 		return __SHIFTIN(XC_IPL_SOFTSERIAL, XC_IPL_MASK);
    199  1.19.8.1       snj 	/* IPL_SOFT* can be the same value (e.g., on sparc or mips). */
    200  1.19.8.1       snj #if IPL_SOFTNET != IPL_SOFTSERIAL
    201  1.19.8.1       snj 	case IPL_SOFTNET:
    202  1.19.8.1       snj 		return __SHIFTIN(XC_IPL_SOFTNET, XC_IPL_MASK);
    203  1.19.8.1       snj #endif
    204  1.19.8.1       snj #if IPL_SOFTBIO != IPL_SOFTNET
    205  1.19.8.1       snj 	case IPL_SOFTBIO:
    206  1.19.8.1       snj 		return __SHIFTIN(XC_IPL_SOFTBIO, XC_IPL_MASK);
    207  1.19.8.1       snj #endif
    208  1.19.8.1       snj #if IPL_SOFTCLOCK != IPL_SOFTBIO
    209  1.19.8.1       snj 	case IPL_SOFTCLOCK:
    210  1.19.8.1       snj 		return __SHIFTIN(XC_IPL_SOFTCLOCK, XC_IPL_MASK);
    211  1.19.8.1       snj #endif
    212  1.19.8.1       snj 	}
    213  1.19.8.1       snj 
    214  1.19.8.1       snj 	panic("Invalid IPL: %d", ipl);
    215  1.19.8.1       snj }
    216  1.19.8.1       snj 
    217  1.19.8.1       snj /*
    218  1.19.8.1       snj  * Extract an XC_IPL from flags of xc_broadcast or xc_unicast.
    219  1.19.8.1       snj  */
    220  1.19.8.1       snj static inline unsigned int
    221  1.19.8.1       snj xc_extract_ipl(unsigned int flags)
    222  1.19.8.1       snj {
    223  1.19.8.1       snj 
    224  1.19.8.1       snj 	return __SHIFTOUT(flags, XC_IPL_MASK);
    225  1.19.8.1       snj }
    226  1.19.8.1       snj 
    227  1.19.8.1       snj /*
    228       1.2        ad  * xc_init_cpu:
    229       1.2        ad  *
    230       1.2        ad  *	Initialize the cross-call subsystem.  Called once for each CPU
    231       1.2        ad  *	in the system as they are attached.
    232       1.2        ad  */
    233       1.2        ad void
    234       1.2        ad xc_init_cpu(struct cpu_info *ci)
    235       1.2        ad {
    236      1.11     pooka 	static bool again = false;
    237      1.16    martin 	int error __diagused;
    238       1.2        ad 
    239       1.2        ad 	if (!again) {
    240       1.2        ad 		/* Autoconfiguration will prevent re-entry. */
    241      1.12     rmind 		xc_init();
    242       1.2        ad 		again = true;
    243       1.2        ad 	}
    244       1.2        ad 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    245       1.2        ad 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    246       1.6    martin 	    NULL, NULL, "xcall/%u", ci->ci_index);
    247      1.12     rmind 	KASSERT(error == 0);
    248       1.2        ad }
    249       1.2        ad 
    250       1.2        ad /*
    251       1.7        ad  * xc_broadcast:
    252       1.2        ad  *
    253       1.2        ad  *	Trigger a call on all CPUs in the system.
    254       1.2        ad  */
    255       1.2        ad uint64_t
    256  1.19.8.1       snj xc_broadcast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2)
    257       1.2        ad {
    258       1.2        ad 
    259      1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    260      1.12     rmind 
    261       1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    262  1.19.8.1       snj 		int ipl = xc_extract_ipl(flags);
    263  1.19.8.1       snj 		return xc_highpri(func, arg1, arg2, NULL, ipl);
    264       1.2        ad 	} else {
    265      1.12     rmind 		return xc_lowpri(func, arg1, arg2, NULL);
    266       1.2        ad 	}
    267       1.2        ad }
    268       1.2        ad 
    269       1.2        ad /*
    270       1.2        ad  * xc_unicast:
    271       1.2        ad  *
    272       1.2        ad  *	Trigger a call on one CPU.
    273       1.2        ad  */
    274       1.2        ad uint64_t
    275  1.19.8.1       snj xc_unicast(unsigned int flags, xcfunc_t func, void *arg1, void *arg2,
    276       1.2        ad 	   struct cpu_info *ci)
    277       1.2        ad {
    278       1.2        ad 
    279      1.12     rmind 	KASSERT(ci != NULL);
    280      1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    281      1.12     rmind 
    282       1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    283  1.19.8.1       snj 		int ipl = xc_extract_ipl(flags);
    284  1.19.8.1       snj 		return xc_highpri(func, arg1, arg2, ci, ipl);
    285      1.12     rmind 	} else {
    286      1.12     rmind 		return xc_lowpri(func, arg1, arg2, ci);
    287      1.12     rmind 	}
    288      1.12     rmind }
    289      1.12     rmind 
    290      1.12     rmind /*
    291      1.12     rmind  * xc_wait:
    292      1.12     rmind  *
    293      1.12     rmind  *	Wait for a cross call to complete.
    294      1.12     rmind  */
    295      1.12     rmind void
    296      1.12     rmind xc_wait(uint64_t where)
    297      1.12     rmind {
    298      1.12     rmind 	xc_state_t *xc;
    299      1.12     rmind 
    300      1.12     rmind 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
    301      1.12     rmind 
    302      1.12     rmind 	/* Determine whether it is high or low priority cross-call. */
    303      1.12     rmind 	if ((where & XC_PRI_BIT) != 0) {
    304      1.12     rmind 		xc = &xc_high_pri;
    305      1.12     rmind 		where &= ~XC_PRI_BIT;
    306       1.2        ad 	} else {
    307      1.12     rmind 		xc = &xc_low_pri;
    308      1.12     rmind 	}
    309      1.12     rmind 
    310      1.12     rmind 	/* Fast path, if already done. */
    311      1.12     rmind 	if (xc->xc_donep >= where) {
    312      1.12     rmind 		return;
    313      1.12     rmind 	}
    314      1.12     rmind 
    315      1.12     rmind 	/* Slow path: block until awoken. */
    316      1.12     rmind 	mutex_enter(&xc->xc_lock);
    317      1.12     rmind 	while (xc->xc_donep < where) {
    318      1.12     rmind 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    319       1.2        ad 	}
    320      1.12     rmind 	mutex_exit(&xc->xc_lock);
    321       1.2        ad }
    322       1.2        ad 
    323       1.2        ad /*
    324       1.2        ad  * xc_lowpri:
    325       1.2        ad  *
    326       1.2        ad  *	Trigger a low priority call on one or more CPUs.
    327       1.2        ad  */
    328      1.12     rmind static inline uint64_t
    329      1.12     rmind xc_lowpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
    330       1.2        ad {
    331      1.12     rmind 	xc_state_t *xc = &xc_low_pri;
    332       1.2        ad 	CPU_INFO_ITERATOR cii;
    333      1.10  uebayasi 	uint64_t where;
    334       1.2        ad 
    335      1.12     rmind 	mutex_enter(&xc->xc_lock);
    336      1.19     ozaki 	while (xc->xc_headp != xc->xc_donep) {
    337      1.12     rmind 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    338      1.12     rmind 	}
    339      1.12     rmind 	xc->xc_arg1 = arg1;
    340      1.12     rmind 	xc->xc_arg2 = arg2;
    341      1.12     rmind 	xc->xc_func = func;
    342       1.2        ad 	if (ci == NULL) {
    343       1.2        ad 		xc_broadcast_ev.ev_count++;
    344       1.2        ad 		for (CPU_INFO_FOREACH(cii, ci)) {
    345       1.8        ad 			if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
    346       1.8        ad 				continue;
    347      1.12     rmind 			xc->xc_headp += 1;
    348       1.2        ad 			ci->ci_data.cpu_xcall_pending = true;
    349       1.2        ad 			cv_signal(&ci->ci_data.cpu_xcall);
    350       1.2        ad 		}
    351       1.2        ad 	} else {
    352       1.2        ad 		xc_unicast_ev.ev_count++;
    353      1.12     rmind 		xc->xc_headp += 1;
    354       1.2        ad 		ci->ci_data.cpu_xcall_pending = true;
    355       1.2        ad 		cv_signal(&ci->ci_data.cpu_xcall);
    356       1.2        ad 	}
    357      1.19     ozaki 	KASSERT(xc->xc_donep < xc->xc_headp);
    358      1.12     rmind 	where = xc->xc_headp;
    359      1.12     rmind 	mutex_exit(&xc->xc_lock);
    360       1.2        ad 
    361      1.12     rmind 	/* Return a low priority ticket. */
    362      1.12     rmind 	KASSERT((where & XC_PRI_BIT) == 0);
    363       1.2        ad 	return where;
    364       1.2        ad }
    365       1.2        ad 
    366       1.2        ad /*
    367       1.2        ad  * xc_thread:
    368       1.2        ad  *
    369       1.2        ad  *	One thread per-CPU to dispatch low priority calls.
    370       1.2        ad  */
    371       1.2        ad static void
    372       1.2        ad xc_thread(void *cookie)
    373       1.2        ad {
    374      1.12     rmind 	struct cpu_info *ci = curcpu();
    375      1.12     rmind 	xc_state_t *xc = &xc_low_pri;
    376       1.2        ad 	void *arg1, *arg2;
    377       1.2        ad 	xcfunc_t func;
    378       1.2        ad 
    379      1.12     rmind 	mutex_enter(&xc->xc_lock);
    380       1.2        ad 	for (;;) {
    381       1.2        ad 		while (!ci->ci_data.cpu_xcall_pending) {
    382      1.19     ozaki 			if (xc->xc_headp == xc->xc_donep) {
    383      1.12     rmind 				cv_broadcast(&xc->xc_busy);
    384      1.12     rmind 			}
    385      1.12     rmind 			cv_wait(&ci->ci_data.cpu_xcall, &xc->xc_lock);
    386       1.2        ad 			KASSERT(ci == curcpu());
    387       1.2        ad 		}
    388       1.2        ad 		ci->ci_data.cpu_xcall_pending = false;
    389      1.12     rmind 		func = xc->xc_func;
    390      1.12     rmind 		arg1 = xc->xc_arg1;
    391      1.12     rmind 		arg2 = xc->xc_arg2;
    392      1.12     rmind 		mutex_exit(&xc->xc_lock);
    393       1.2        ad 
    394      1.12     rmind 		KASSERT(func != NULL);
    395       1.2        ad 		(*func)(arg1, arg2);
    396       1.2        ad 
    397      1.12     rmind 		mutex_enter(&xc->xc_lock);
    398      1.12     rmind 		xc->xc_donep++;
    399       1.2        ad 	}
    400       1.2        ad 	/* NOTREACHED */
    401       1.2        ad }
    402      1.12     rmind 
    403      1.12     rmind /*
    404      1.12     rmind  * xc_ipi_handler:
    405      1.12     rmind  *
    406      1.12     rmind  *	Handler of cross-call IPI.
    407      1.12     rmind  */
    408      1.12     rmind void
    409      1.12     rmind xc_ipi_handler(void)
    410      1.12     rmind {
    411  1.19.8.1       snj 	xc_state_t *xc = & xc_high_pri;
    412  1.19.8.1       snj 
    413  1.19.8.1       snj 	KASSERT(xc->xc_ipl < __arraycount(xc_sihs));
    414  1.19.8.1       snj 	KASSERT(xc_sihs[xc->xc_ipl] != NULL);
    415  1.19.8.1       snj 
    416      1.14    martin 	/* Executes xc__highpri_intr() via software interrupt. */
    417  1.19.8.1       snj 	softint_schedule(xc_sihs[xc->xc_ipl]);
    418      1.12     rmind }
    419      1.12     rmind 
    420      1.12     rmind /*
    421      1.14    martin  * xc__highpri_intr:
    422      1.12     rmind  *
    423      1.12     rmind  *	A software interrupt handler for high priority calls.
    424      1.12     rmind  */
    425      1.14    martin void
    426      1.14    martin xc__highpri_intr(void *dummy)
    427      1.12     rmind {
    428      1.12     rmind 	xc_state_t *xc = &xc_high_pri;
    429      1.12     rmind 	void *arg1, *arg2;
    430      1.12     rmind 	xcfunc_t func;
    431      1.12     rmind 
    432      1.14    martin 	KASSERT(!cpu_intr_p());
    433      1.12     rmind 	/*
    434      1.12     rmind 	 * Lock-less fetch of function and its arguments.
    435      1.12     rmind 	 * Safe since it cannot change at this point.
    436      1.12     rmind 	 */
    437      1.12     rmind 	KASSERT(xc->xc_donep < xc->xc_headp);
    438      1.12     rmind 	func = xc->xc_func;
    439      1.12     rmind 	arg1 = xc->xc_arg1;
    440      1.12     rmind 	arg2 = xc->xc_arg2;
    441      1.12     rmind 
    442      1.12     rmind 	KASSERT(func != NULL);
    443      1.12     rmind 	(*func)(arg1, arg2);
    444      1.12     rmind 
    445      1.12     rmind 	/*
    446      1.12     rmind 	 * Note the request as done, and if we have reached the head,
    447      1.12     rmind 	 * cross-call has been processed - notify waiters, if any.
    448      1.12     rmind 	 */
    449      1.12     rmind 	mutex_enter(&xc->xc_lock);
    450      1.12     rmind 	if (++xc->xc_donep == xc->xc_headp) {
    451      1.12     rmind 		cv_broadcast(&xc->xc_busy);
    452      1.12     rmind 	}
    453      1.12     rmind 	mutex_exit(&xc->xc_lock);
    454      1.12     rmind }
    455      1.12     rmind 
    456      1.12     rmind /*
    457      1.12     rmind  * xc_highpri:
    458      1.12     rmind  *
    459      1.12     rmind  *	Trigger a high priority call on one or more CPUs.
    460      1.12     rmind  */
    461      1.12     rmind static inline uint64_t
    462  1.19.8.1       snj xc_highpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci,
    463  1.19.8.1       snj     unsigned int ipl)
    464      1.12     rmind {
    465      1.12     rmind 	xc_state_t *xc = &xc_high_pri;
    466      1.12     rmind 	uint64_t where;
    467      1.12     rmind 
    468      1.12     rmind 	mutex_enter(&xc->xc_lock);
    469      1.12     rmind 	while (xc->xc_headp != xc->xc_donep) {
    470      1.12     rmind 		cv_wait(&xc->xc_busy, &xc->xc_lock);
    471      1.12     rmind 	}
    472      1.12     rmind 	xc->xc_func = func;
    473      1.12     rmind 	xc->xc_arg1 = arg1;
    474      1.12     rmind 	xc->xc_arg2 = arg2;
    475      1.12     rmind 	xc->xc_headp += (ci ? 1 : ncpu);
    476  1.19.8.1       snj 	xc->xc_ipl = ipl;
    477      1.12     rmind 	where = xc->xc_headp;
    478      1.12     rmind 	mutex_exit(&xc->xc_lock);
    479      1.12     rmind 
    480      1.12     rmind 	/*
    481      1.12     rmind 	 * Send the IPI once lock is released.
    482      1.12     rmind 	 * Note: it will handle the local CPU case.
    483      1.12     rmind 	 */
    484      1.12     rmind 
    485      1.14    martin #ifdef _RUMPKERNEL
    486      1.14    martin 	rump_xc_highpri(ci);
    487      1.14    martin #else
    488      1.12     rmind #ifdef MULTIPROCESSOR
    489      1.12     rmind 	kpreempt_disable();
    490      1.12     rmind 	if (curcpu() == ci) {
    491      1.12     rmind 		/* Unicast: local CPU. */
    492      1.12     rmind 		xc_ipi_handler();
    493      1.12     rmind 	} else if (ci) {
    494      1.12     rmind 		/* Unicast: remote CPU. */
    495      1.12     rmind 		xc_send_ipi(ci);
    496      1.12     rmind 	} else {
    497      1.12     rmind 		/* Broadcast: all, including local. */
    498      1.12     rmind 		xc_send_ipi(NULL);
    499      1.12     rmind 		xc_ipi_handler();
    500      1.12     rmind 	}
    501      1.12     rmind 	kpreempt_enable();
    502      1.12     rmind #else
    503      1.15     rmind 	KASSERT(ci == NULL || curcpu() == ci);
    504      1.12     rmind 	xc_ipi_handler();
    505      1.12     rmind #endif
    506      1.14    martin #endif
    507      1.12     rmind 
    508      1.12     rmind 	/* Indicate a high priority ticket. */
    509      1.12     rmind 	return (where | XC_PRI_BIT);
    510      1.12     rmind }
    511