Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.1.2.3
      1  1.1.2.3  ad /*	$NetBSD: subr_xcall.c,v 1.1.2.3 2007/10/09 13:44:29 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  * Cross call support
     41  1.1.2.1  ad  *
     42  1.1.2.1  ad  * Background
     43  1.1.2.1  ad  *
     44  1.1.2.1  ad  *	Sometimes it is necessary to modify hardware state that is tied
     45  1.1.2.1  ad  *	directly to individual CPUs (such as a CPU's local timer), and
     46  1.1.2.1  ad  *	these updates can not be done remotely by another CPU.  The LWP
     47  1.1.2.1  ad  *	requesting the update may be unable to guarantee that it will be
     48  1.1.2.1  ad  *	running on the CPU where the update must occur, when the update
     49  1.1.2.1  ad  *	occurs.
     50  1.1.2.1  ad  *
     51  1.1.2.1  ad  *	Additionally, it's sometimes necessary to modify per-CPU software
     52  1.1.2.1  ad  *	state from a remote CPU.  Where these update operations are so
     53  1.1.2.1  ad  *	rare or the access to the per-CPU data so frequent that the cost
     54  1.1.2.1  ad  *	of using locking or atomic operations to provide coherency is
     55  1.1.2.1  ad  *	prohobitive, another way must be found.
     56  1.1.2.1  ad  *
     57  1.1.2.1  ad  *	Cross calls help to solve these types of problem by allowing
     58  1.1.2.1  ad  *	any CPU in the system to request that an arbitrary function be
     59  1.1.2.1  ad  *	executed on any other CPU.
     60  1.1.2.1  ad  *
     61  1.1.2.1  ad  * Implementation
     62  1.1.2.1  ad  *
     63  1.1.2.1  ad  *	A slow mechanism for making 'low priority' cross calls is
     64  1.1.2.1  ad  *	provided.  The function to be executed runs on the remote CPU
     65  1.1.2.1  ad  *	within a bound kthread.  No queueing is provided, and the
     66  1.1.2.1  ad  *	implementation uses global state.  The function being called may
     67  1.1.2.1  ad  *	block briefly on locks, but in doing so must be careful to not
     68  1.1.2.1  ad  *	interfere with other cross calls in the system.  The function is
     69  1.1.2.1  ad  *	called with thread context and not from a soft interrupt, so it
     70  1.1.2.1  ad  *	can ensure that it is not interrupting other code running on the
     71  1.1.2.1  ad  *	CPU, and so has exclusive access to the CPU.  Since this facility
     72  1.1.2.1  ad  *	is heavyweight, it's expected that it will not be used often.
     73  1.1.2.1  ad  *
     74  1.1.2.1  ad  * Future directions
     75  1.1.2.1  ad  *
     76  1.1.2.1  ad  *	Add a low-overhead mechanism to run cross calls in interrupt
     77  1.1.2.1  ad  *	context (XC_HIGHPRI).
     78  1.1.2.1  ad  */
     79  1.1.2.1  ad 
     80  1.1.2.1  ad #include <sys/cdefs.h>
     81  1.1.2.3  ad __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.1.2.3 2007/10/09 13:44:29 ad Exp $");
     82  1.1.2.3  ad 
     83  1.1.2.3  ad #include <sys/types.h>
     84  1.1.2.3  ad #include <sys/param.h>
     85  1.1.2.3  ad #include <sys/xcall.h>
     86  1.1.2.3  ad #include <sys/mutex.h>
     87  1.1.2.3  ad #include <sys/condvar.h>
     88  1.1.2.3  ad #include <sys/evcnt.h>
     89  1.1.2.3  ad #include <sys/kthread.h>
     90  1.1.2.3  ad #include <sys/cpu.h>
     91  1.1.2.3  ad 
     92  1.1.2.3  ad #define	PRI_XCALL	0
     93  1.1.2.3  ad 
     94  1.1.2.3  ad static void	xc_thread(void *);
     95  1.1.2.3  ad static uint64_t	xc_lowpri(u_int, xcfunc_t, void *, void *, struct cpu_info *);
     96  1.1.2.3  ad 
     97  1.1.2.3  ad static kmutex_t		xc_lock;
     98  1.1.2.3  ad static xcfunc_t		xc_func;
     99  1.1.2.3  ad static void		*xc_arg1;
    100  1.1.2.3  ad static void		*xc_arg2;
    101  1.1.2.3  ad static kcondvar_t	xc_busy;
    102  1.1.2.3  ad static struct evcnt	xc_unicast_ev;
    103  1.1.2.3  ad static struct evcnt	xc_broadcast_ev;
    104  1.1.2.3  ad static uint64_t		xc_headp;
    105  1.1.2.3  ad static uint64_t		xc_tailp;
    106  1.1.2.3  ad static uint64_t		xc_donep;
    107  1.1.2.3  ad 
    108  1.1.2.3  ad /*
    109  1.1.2.3  ad  * xc_init_cpu:
    110  1.1.2.3  ad  *
    111  1.1.2.3  ad  *	Initialize the cross-call subsystem.  Called once for each CPU
    112  1.1.2.3  ad  *	in the system as they are attached.
    113  1.1.2.3  ad  */
    114  1.1.2.3  ad void
    115  1.1.2.3  ad xc_init_cpu(struct cpu_info *ci)
    116  1.1.2.3  ad {
    117  1.1.2.3  ad 	static bool again;
    118  1.1.2.3  ad 	int error;
    119  1.1.2.3  ad 
    120  1.1.2.3  ad 	if (!again) {
    121  1.1.2.3  ad 		/* Autoconfiguration will prevent re-entry. */
    122  1.1.2.3  ad 		again = true;
    123  1.1.2.3  ad 		mutex_init(&xc_lock, MUTEX_DEFAULT, IPL_NONE);
    124  1.1.2.3  ad 		cv_init(&xc_busy, "xcallbsy");
    125  1.1.2.3  ad 		evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    126  1.1.2.3  ad 		   "crosscall", "unicast");
    127  1.1.2.3  ad 		evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    128  1.1.2.3  ad 		   "crosscall", "broadcast");
    129  1.1.2.3  ad 	}
    130  1.1.2.3  ad 
    131  1.1.2.3  ad 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    132  1.1.2.3  ad 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    133  1.1.2.3  ad 	    NULL, NULL, "xcall/%d", (int)ci->ci_cpuid);
    134  1.1.2.3  ad 	if (error != 0)
    135  1.1.2.3  ad 		panic("xc_init_cpu: error %d", error);
    136  1.1.2.3  ad }
    137  1.1.2.3  ad 
    138  1.1.2.3  ad /*
    139  1.1.2.3  ad  * xc_unicast:
    140  1.1.2.3  ad  *
    141  1.1.2.3  ad  *	Trigger a call on all CPUs in the system.
    142  1.1.2.3  ad  */
    143  1.1.2.3  ad uint64_t
    144  1.1.2.3  ad xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
    145  1.1.2.3  ad {
    146  1.1.2.3  ad 
    147  1.1.2.3  ad 	if ((flags & XC_HIGHPRI) != 0) {
    148  1.1.2.3  ad 		panic("xc_unicast: no high priority crosscalls yet");
    149  1.1.2.3  ad 	} else {
    150  1.1.2.3  ad 		return xc_lowpri(flags, func, arg1, arg2, NULL);
    151  1.1.2.3  ad 	}
    152  1.1.2.3  ad }
    153  1.1.2.3  ad 
    154  1.1.2.3  ad /*
    155  1.1.2.3  ad  * xc_unicast:
    156  1.1.2.3  ad  *
    157  1.1.2.3  ad  *	Trigger a call on one CPU.
    158  1.1.2.3  ad  */
    159  1.1.2.3  ad uint64_t
    160  1.1.2.3  ad xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    161  1.1.2.3  ad 	   struct cpu_info *ci)
    162  1.1.2.3  ad {
    163  1.1.2.3  ad 
    164  1.1.2.3  ad 	if ((flags & XC_HIGHPRI) != 0) {
    165  1.1.2.3  ad 		panic("xc_unicast: no high priority crosscalls yet");
    166  1.1.2.3  ad 	} else {
    167  1.1.2.3  ad 		KASSERT(ci != NULL);
    168  1.1.2.3  ad 		return xc_lowpri(flags, func, arg1, arg2, ci);
    169  1.1.2.3  ad 	}
    170  1.1.2.3  ad }
    171  1.1.2.3  ad 
    172  1.1.2.3  ad /*
    173  1.1.2.3  ad  * xc_lowpri:
    174  1.1.2.3  ad  *
    175  1.1.2.3  ad  *	Trigger a low priority call on one or more CPUs.
    176  1.1.2.3  ad  */
    177  1.1.2.3  ad static uint64_t
    178  1.1.2.3  ad xc_lowpri(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    179  1.1.2.3  ad 	  struct cpu_info *ci)
    180  1.1.2.3  ad {
    181  1.1.2.3  ad 	CPU_INFO_ITERATOR cii;
    182  1.1.2.3  ad 	u_int where;
    183  1.1.2.3  ad 
    184  1.1.2.3  ad 	mutex_enter(&xc_lock);
    185  1.1.2.3  ad 	while (xc_headp != xc_tailp)
    186  1.1.2.3  ad 		cv_wait(&xc_busy, &xc_lock);
    187  1.1.2.3  ad 	xc_arg1 = arg1;
    188  1.1.2.3  ad 	xc_arg2 = arg2;
    189  1.1.2.3  ad 	xc_func = func;
    190  1.1.2.3  ad 	if (ci == NULL) {
    191  1.1.2.3  ad 		xc_broadcast_ev.ev_count++;
    192  1.1.2.3  ad 		for (CPU_INFO_FOREACH(cii, ci)) {
    193  1.1.2.3  ad 			xc_headp += 1;
    194  1.1.2.3  ad 			ci->ci_data.cpu_xcall_pending = true;
    195  1.1.2.3  ad 			cv_signal(&ci->ci_data.cpu_xcall);
    196  1.1.2.3  ad 		}
    197  1.1.2.3  ad 	} else {
    198  1.1.2.3  ad 		xc_unicast_ev.ev_count++;
    199  1.1.2.3  ad 		xc_headp += 1;
    200  1.1.2.3  ad 		ci->ci_data.cpu_xcall_pending = true;
    201  1.1.2.3  ad 		cv_signal(&ci->ci_data.cpu_xcall);
    202  1.1.2.3  ad 	}
    203  1.1.2.3  ad 	KASSERT(xc_tailp < xc_headp);
    204  1.1.2.3  ad 	where = xc_headp;
    205  1.1.2.3  ad 	mutex_exit(&xc_lock);
    206  1.1.2.3  ad 
    207  1.1.2.3  ad 	return where;
    208  1.1.2.3  ad }
    209  1.1.2.3  ad 
    210  1.1.2.3  ad /*
    211  1.1.2.3  ad  * xc_wait:
    212  1.1.2.3  ad  *
    213  1.1.2.3  ad  *	Wait for a cross call to complete.
    214  1.1.2.3  ad  */
    215  1.1.2.3  ad void
    216  1.1.2.3  ad xc_wait(uint64_t where)
    217  1.1.2.3  ad {
    218  1.1.2.3  ad 
    219  1.1.2.3  ad 	if (xc_donep >= where)
    220  1.1.2.3  ad 		return;
    221  1.1.2.3  ad 
    222  1.1.2.3  ad 	mutex_enter(&xc_lock);
    223  1.1.2.3  ad 	while (xc_donep < where)
    224  1.1.2.3  ad 		cv_wait(&xc_busy, &xc_lock);
    225  1.1.2.3  ad 	mutex_exit(&xc_lock);
    226  1.1.2.3  ad }
    227  1.1.2.3  ad 
    228  1.1.2.3  ad /*
    229  1.1.2.3  ad  * xc_thread:
    230  1.1.2.3  ad  *
    231  1.1.2.3  ad  *	One thread per-CPU to dispatch low priority calls.
    232  1.1.2.3  ad  */
    233  1.1.2.3  ad static void
    234  1.1.2.3  ad xc_thread(void *cookie)
    235  1.1.2.3  ad {
    236  1.1.2.3  ad 	void *arg1, *arg2;
    237  1.1.2.3  ad 	struct cpu_info *ci;
    238  1.1.2.3  ad 	xcfunc_t func;
    239  1.1.2.3  ad 
    240  1.1.2.3  ad 	ci = curcpu();
    241  1.1.2.3  ad 
    242  1.1.2.3  ad 	mutex_enter(&xc_lock);
    243  1.1.2.3  ad 	for (;;) {
    244  1.1.2.3  ad 		while (!ci->ci_data.cpu_xcall_pending) {
    245  1.1.2.3  ad 			if (xc_headp == xc_tailp)
    246  1.1.2.3  ad 				cv_broadcast(&xc_busy);
    247  1.1.2.3  ad 			cv_wait(&ci->ci_data.cpu_xcall, &xc_lock);
    248  1.1.2.3  ad 			KASSERT(ci == curcpu());
    249  1.1.2.3  ad 		}
    250  1.1.2.3  ad 		ci->ci_data.cpu_xcall_pending = false;
    251  1.1.2.3  ad 		func = xc_func;
    252  1.1.2.3  ad 		arg1 = xc_arg1;
    253  1.1.2.3  ad 		arg2 = xc_arg2;
    254  1.1.2.3  ad 		xc_tailp++;
    255  1.1.2.3  ad 		mutex_exit(&xc_lock);
    256  1.1.2.3  ad 
    257  1.1.2.3  ad 		(*func)(arg1, arg2);
    258  1.1.2.3  ad 
    259  1.1.2.3  ad 		mutex_enter(&xc_lock);
    260  1.1.2.3  ad 		xc_donep++;
    261  1.1.2.3  ad 	}
    262  1.1.2.3  ad 	/* NOTREACHED */
    263  1.1.2.3  ad }
    264  1.1.2.3  ad /*	$NetBSD: subr_xcall.c,v 1.1.2.3 2007/10/09 13:44:29 ad Exp $	*/
    265  1.1.2.3  ad 
    266  1.1.2.3  ad /*-
    267  1.1.2.3  ad  * Copyright (c) 2007 The NetBSD Foundation, Inc.
    268  1.1.2.3  ad  * All rights reserved.
    269  1.1.2.3  ad  *
    270  1.1.2.3  ad  * This code is derived from software contributed to The NetBSD Foundation
    271  1.1.2.3  ad  * by Andrew Doran.
    272  1.1.2.3  ad  *
    273  1.1.2.3  ad  * Redistribution and use in source and binary forms, with or without
    274  1.1.2.3  ad  * modification, are permitted provided that the following conditions
    275  1.1.2.3  ad  * are met:
    276  1.1.2.3  ad  * 1. Redistributions of source code must retain the above copyright
    277  1.1.2.3  ad  *    notice, this list of conditions and the following disclaimer.
    278  1.1.2.3  ad  * 2. Redistributions in binary form must reproduce the above copyright
    279  1.1.2.3  ad  *    notice, this list of conditions and the following disclaimer in the
    280  1.1.2.3  ad  *    documentation and/or other materials provided with the distribution.
    281  1.1.2.3  ad  * 3. All advertising materials mentioning features or use of this software
    282  1.1.2.3  ad  *    must display the following acknowledgement:
    283  1.1.2.3  ad  *	This product includes software developed by the NetBSD
    284  1.1.2.3  ad  *	Foundation, Inc. and its contributors.
    285  1.1.2.3  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
    286  1.1.2.3  ad  *    contributors may be used to endorse or promote products derived
    287  1.1.2.3  ad  *    from this software without specific prior written permission.
    288  1.1.2.3  ad  *
    289  1.1.2.3  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
    290  1.1.2.3  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    291  1.1.2.3  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    292  1.1.2.3  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
    293  1.1.2.3  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    294  1.1.2.3  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    295  1.1.2.3  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    296  1.1.2.3  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    297  1.1.2.3  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    298  1.1.2.3  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    299  1.1.2.3  ad  * POSSIBILITY OF SUCH DAMAGE.
    300  1.1.2.3  ad  */
    301  1.1.2.3  ad 
    302  1.1.2.3  ad /*
    303  1.1.2.3  ad  * Cross call support
    304  1.1.2.3  ad  *
    305  1.1.2.3  ad  * Background
    306  1.1.2.3  ad  *
    307  1.1.2.3  ad  *	Sometimes it is necessary to modify hardware state that is tied
    308  1.1.2.3  ad  *	directly to individual CPUs (such as a CPU's local timer), and
    309  1.1.2.3  ad  *	these updates can not be done remotely by another CPU.  The LWP
    310  1.1.2.3  ad  *	requesting the update may be unable to guarantee that it will be
    311  1.1.2.3  ad  *	running on the CPU where the update must occur, when the update
    312  1.1.2.3  ad  *	occurs.
    313  1.1.2.3  ad  *
    314  1.1.2.3  ad  *	Additionally, it's sometimes necessary to modify per-CPU software
    315  1.1.2.3  ad  *	state from a remote CPU.  Where these update operations are so
    316  1.1.2.3  ad  *	rare or the access to the per-CPU data so frequent that the cost
    317  1.1.2.3  ad  *	of using locking or atomic operations to provide coherency is
    318  1.1.2.3  ad  *	prohobitive, another way must be found.
    319  1.1.2.3  ad  *
    320  1.1.2.3  ad  *	Cross calls help to solve these types of problem by allowing
    321  1.1.2.3  ad  *	any CPU in the system to request that an arbitrary function be
    322  1.1.2.3  ad  *	executed on any other CPU.
    323  1.1.2.3  ad  *
    324  1.1.2.3  ad  * Implementation
    325  1.1.2.3  ad  *
    326  1.1.2.3  ad  *	A slow mechanism for making 'low priority' cross calls is
    327  1.1.2.3  ad  *	provided.  The function to be executed runs on the remote CPU
    328  1.1.2.3  ad  *	within a bound kthread.  No queueing is provided, and the
    329  1.1.2.3  ad  *	implementation uses global state.  The function being called may
    330  1.1.2.3  ad  *	block briefly on locks, but in doing so must be careful to not
    331  1.1.2.3  ad  *	interfere with other cross calls in the system.  The function is
    332  1.1.2.3  ad  *	called with thread context and not from a soft interrupt, so it
    333  1.1.2.3  ad  *	can ensure that it is not interrupting other code running on the
    334  1.1.2.3  ad  *	CPU, and so has exclusive access to the CPU.  Since this facility
    335  1.1.2.3  ad  *	is heavyweight, it's expected that it will not be used often.
    336  1.1.2.3  ad  *
    337  1.1.2.3  ad  * Future directions
    338  1.1.2.3  ad  *
    339  1.1.2.3  ad  *	Add a low-overhead mechanism to run cross calls in interrupt
    340  1.1.2.3  ad  *	context (XC_HIGHPRI).
    341  1.1.2.3  ad  */
    342  1.1.2.3  ad 
    343  1.1.2.3  ad #include <sys/cdefs.h>
    344  1.1.2.3  ad __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.1.2.3 2007/10/09 13:44:29 ad Exp $");
    345  1.1.2.1  ad 
    346  1.1.2.1  ad #include <sys/types.h>
    347  1.1.2.1  ad #include <sys/param.h>
    348  1.1.2.1  ad #include <sys/xcall.h>
    349  1.1.2.1  ad #include <sys/mutex.h>
    350  1.1.2.1  ad #include <sys/condvar.h>
    351  1.1.2.1  ad #include <sys/evcnt.h>
    352  1.1.2.1  ad #include <sys/kthread.h>
    353  1.1.2.1  ad 
    354  1.1.2.1  ad #define	PRI_XCALL	PRI_KERNEL_RT
    355  1.1.2.1  ad 
    356  1.1.2.1  ad static void	xc_thread(void *);
    357  1.1.2.2  ad static uint64_t	xc_lowpri(u_int, xcfunc_t, void *, void *, struct cpu_info *);
    358  1.1.2.1  ad 
    359  1.1.2.1  ad static kmutex_t		xc_lock;
    360  1.1.2.1  ad static xcfunc_t		xc_func;
    361  1.1.2.1  ad static void		*xc_arg1;
    362  1.1.2.1  ad static void		*xc_arg2;
    363  1.1.2.1  ad static kcondvar_t	xc_busy;
    364  1.1.2.1  ad static struct evcnt	xc_unicast_ev;
    365  1.1.2.1  ad static struct evcnt	xc_broadcast_ev;
    366  1.1.2.1  ad static uint64_t		xc_headp;
    367  1.1.2.1  ad static uint64_t		xc_tailp;
    368  1.1.2.1  ad static uint64_t		xc_donep;
    369  1.1.2.1  ad 
    370  1.1.2.1  ad /*
    371  1.1.2.1  ad  * xc_init_cpu:
    372  1.1.2.1  ad  *
    373  1.1.2.1  ad  *	Initialize the cross-call subsystem.  Called once for each CPU
    374  1.1.2.2  ad  *	in the system as they are attached.
    375  1.1.2.1  ad  */
    376  1.1.2.1  ad void
    377  1.1.2.1  ad xc_init_cpu(struct cpu_info *ci)
    378  1.1.2.1  ad {
    379  1.1.2.1  ad 	static bool again;
    380  1.1.2.1  ad 	int error;
    381  1.1.2.1  ad 
    382  1.1.2.1  ad 	if (!again) {
    383  1.1.2.1  ad 		/* Autoconfiguration will prevent re-entry. */
    384  1.1.2.1  ad 		again = true;
    385  1.1.2.1  ad 		mutex_init(&xc_lock, MUTEX_DEFAULT, IPL_NONE);
    386  1.1.2.1  ad 		cv_init(&xc_busy, "xcallbsy");
    387  1.1.2.1  ad 		evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    388  1.1.2.1  ad 		   "crosscall", "unicast");
    389  1.1.2.1  ad 		evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    390  1.1.2.1  ad 		   "crosscall", "broadcast");
    391  1.1.2.1  ad 	}
    392  1.1.2.1  ad 
    393  1.1.2.1  ad 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    394  1.1.2.1  ad 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    395  1.1.2.1  ad 	    NULL, NULL, "xcall/%d", (int)ci->ci_cpuid);
    396  1.1.2.1  ad 	if (error != 0)
    397  1.1.2.1  ad 		panic("xc_init_cpu: error %d", error);
    398  1.1.2.1  ad }
    399  1.1.2.1  ad 
    400  1.1.2.1  ad /*
    401  1.1.2.1  ad  * xc_unicast:
    402  1.1.2.1  ad  *
    403  1.1.2.1  ad  *	Trigger a call on all CPUs in the system.
    404  1.1.2.1  ad  */
    405  1.1.2.2  ad uint64_t
    406  1.1.2.1  ad xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
    407  1.1.2.1  ad {
    408  1.1.2.1  ad 
    409  1.1.2.1  ad 	if ((flags & XC_HIGHPRI) != 0) {
    410  1.1.2.1  ad 		panic("xc_unicast: no high priority crosscalls yet");
    411  1.1.2.1  ad 	} else {
    412  1.1.2.2  ad 		return xc_lowpri(flags, func, arg1, arg2, NULL);
    413  1.1.2.1  ad 	}
    414  1.1.2.1  ad }
    415  1.1.2.1  ad 
    416  1.1.2.1  ad /*
    417  1.1.2.1  ad  * xc_unicast:
    418  1.1.2.1  ad  *
    419  1.1.2.1  ad  *	Trigger a call on one CPU.
    420  1.1.2.1  ad  */
    421  1.1.2.2  ad uint64_t
    422  1.1.2.1  ad xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    423  1.1.2.1  ad 	   struct cpu_info *ci)
    424  1.1.2.1  ad {
    425  1.1.2.1  ad 
    426  1.1.2.1  ad 	if ((flags & XC_HIGHPRI) != 0) {
    427  1.1.2.1  ad 		panic("xc_unicast: no high priority crosscalls yet");
    428  1.1.2.1  ad 	} else {
    429  1.1.2.1  ad 		KASSERT(ci != NULL);
    430  1.1.2.2  ad 		return xc_lowpri(flags, func, arg1, arg2, ci);
    431  1.1.2.1  ad 	}
    432  1.1.2.1  ad }
    433  1.1.2.1  ad 
    434  1.1.2.1  ad /*
    435  1.1.2.1  ad  * xc_lowpri:
    436  1.1.2.1  ad  *
    437  1.1.2.1  ad  *	Trigger a low priority call on one or more CPUs.
    438  1.1.2.1  ad  */
    439  1.1.2.2  ad static uint64_t
    440  1.1.2.1  ad xc_lowpri(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    441  1.1.2.1  ad 	  struct cpu_info *ci)
    442  1.1.2.1  ad {
    443  1.1.2.1  ad 	CPU_INFO_ITERATOR cii;
    444  1.1.2.1  ad 	u_int where;
    445  1.1.2.1  ad 
    446  1.1.2.1  ad 	mutex_enter(&xc_lock);
    447  1.1.2.1  ad 	while (xc_headp != xc_tailp)
    448  1.1.2.1  ad 		cv_wait(&xc_busy, &xc_lock);
    449  1.1.2.1  ad 	xc_arg1 = arg1;
    450  1.1.2.1  ad 	xc_arg2 = arg2;
    451  1.1.2.1  ad 	xc_func = func;
    452  1.1.2.2  ad 	if (ci == NULL) {
    453  1.1.2.1  ad 		xc_broadcast_ev.ev_count++;
    454  1.1.2.1  ad 		for (CPU_INFO_FOREACH(cii, ci)) {
    455  1.1.2.1  ad 			xc_headp += 1;
    456  1.1.2.2  ad 			ci->ci_data.cpu_xcall_pending = true;
    457  1.1.2.1  ad 			cv_signal(&ci->ci_data.cpu_xcall);
    458  1.1.2.1  ad 		}
    459  1.1.2.1  ad 	} else {
    460  1.1.2.1  ad 		xc_unicast_ev.ev_count++;
    461  1.1.2.1  ad 		xc_headp += 1;
    462  1.1.2.2  ad 		ci->ci_data.cpu_xcall_pending = true;
    463  1.1.2.1  ad 		cv_signal(&ci->ci_data.cpu_xcall);
    464  1.1.2.1  ad 	}
    465  1.1.2.1  ad 	KASSERT(xc_tailp < xc_headp);
    466  1.1.2.2  ad 	where = xc_headp;
    467  1.1.2.2  ad 	mutex_exit(&xc_lock);
    468  1.1.2.2  ad 
    469  1.1.2.2  ad 	return where;
    470  1.1.2.2  ad }
    471  1.1.2.2  ad 
    472  1.1.2.2  ad /*
    473  1.1.2.2  ad  * xc_wait:
    474  1.1.2.2  ad  *
    475  1.1.2.2  ad  *	Wait for a cross call to complete.
    476  1.1.2.2  ad  */
    477  1.1.2.2  ad void
    478  1.1.2.2  ad xc_wait(uint64_t where)
    479  1.1.2.2  ad {
    480  1.1.2.2  ad 
    481  1.1.2.2  ad 	if (xc_donep >= where)
    482  1.1.2.2  ad 		return;
    483  1.1.2.2  ad 
    484  1.1.2.2  ad 	mutex_enter(&xc_lock);
    485  1.1.2.2  ad 	while (xc_donep < where)
    486  1.1.2.2  ad 		cv_wait(&xc_busy, &xc_lock);
    487  1.1.2.1  ad 	mutex_exit(&xc_lock);
    488  1.1.2.1  ad }
    489  1.1.2.1  ad 
    490  1.1.2.1  ad /*
    491  1.1.2.1  ad  * xc_thread:
    492  1.1.2.1  ad  *
    493  1.1.2.1  ad  *	One thread per-CPU to dispatch low priority calls.
    494  1.1.2.1  ad  */
    495  1.1.2.1  ad static void
    496  1.1.2.1  ad xc_thread(void *cookie)
    497  1.1.2.1  ad {
    498  1.1.2.1  ad 	void *arg1, *arg2;
    499  1.1.2.1  ad 	struct cpu_info *ci;
    500  1.1.2.1  ad 	xcfunc_t func;
    501  1.1.2.1  ad 
    502  1.1.2.1  ad 	ci = curcpu();
    503  1.1.2.1  ad 
    504  1.1.2.1  ad 	mutex_enter(&xc_lock);
    505  1.1.2.1  ad 	for (;;) {
    506  1.1.2.2  ad 		while (!ci->ci_data.cpu_xcall_pending) {
    507  1.1.2.1  ad 			if (xc_headp == xc_tailp)
    508  1.1.2.1  ad 				cv_broadcast(&xc_busy);
    509  1.1.2.1  ad 			cv_wait(&ci->ci_data.cpu_xcall, &xc_lock);
    510  1.1.2.1  ad 			KASSERT(ci == curcpu());
    511  1.1.2.1  ad 		}
    512  1.1.2.2  ad 		ci->ci_data.cpu_xcall_pending = false;
    513  1.1.2.1  ad 		func = xc_func;
    514  1.1.2.1  ad 		arg1 = xc_arg1;
    515  1.1.2.1  ad 		arg2 = xc_arg2;
    516  1.1.2.1  ad 		xc_tailp++;
    517  1.1.2.1  ad 		mutex_exit(&xc_lock);
    518  1.1.2.1  ad 
    519  1.1.2.1  ad 		(*func)(arg1, arg2);
    520  1.1.2.1  ad 
    521  1.1.2.1  ad 		mutex_enter(&xc_lock);
    522  1.1.2.1  ad 		xc_donep++;
    523  1.1.2.1  ad 	}
    524  1.1.2.1  ad 	/* NOTREACHED */
    525  1.1.2.1  ad }
    526