Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.4
      1 /*	$NetBSD: subr_xcall.c,v 1.4 2007/10/27 01:22:53 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Cross call support
     41  *
     42  * Background
     43  *
     44  *	Sometimes it is necessary to modify hardware state that is tied
     45  *	directly to individual CPUs (such as a CPU's local timer), and
     46  *	these updates can not be done remotely by another CPU.  The LWP
     47  *	requesting the update may be unable to guarantee that it will be
     48  *	running on the CPU where the update must occur, when the update
     49  *	occurs.
     50  *
     51  *	Additionally, it's sometimes necessary to modify per-CPU software
     52  *	state from a remote CPU.  Where these update operations are so
     53  *	rare or the access to the per-CPU data so frequent that the cost
     54  *	of using locking or atomic operations to provide coherency is
     55  *	prohibitive, another way must be found.
     56  *
     57  *	Cross calls help to solve these types of problem by allowing
     58  *	any CPU in the system to request that an arbitrary function be
     59  *	executed on any other CPU.
     60  *
     61  * Implementation
     62  *
     63  *	A slow mechanism for making 'low priority' cross calls is
     64  *	provided.  The function to be executed runs on the remote CPU
     65  *	within a bound kthread.  No queueing is provided, and the
     66  *	implementation uses global state.  The function being called may
     67  *	block briefly on locks, but in doing so must be careful to not
     68  *	interfere with other cross calls in the system.  The function is
     69  *	called with thread context and not from a soft interrupt, so it
     70  *	can ensure that it is not interrupting other code running on the
     71  *	CPU, and so has exclusive access to the CPU.  Since this facility
     72  *	is heavyweight, it's expected that it will not be used often.
     73  *
     74  *	Cross calls must not allocate memory, as the pagedaemon uses
     75  *	them (and memory allocation may need to wait on the pagedaemon).
     76  *
     77  * Future directions
     78  *
     79  *	Add a low-overhead mechanism to run cross calls in interrupt
     80  *	context (XC_HIGHPRI).
     81  */
     82 
     83 #include <sys/cdefs.h>
     84 __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.4 2007/10/27 01:22:53 ad Exp $");
     85 
     86 #include <sys/types.h>
     87 #include <sys/param.h>
     88 #include <sys/xcall.h>
     89 #include <sys/mutex.h>
     90 #include <sys/condvar.h>
     91 #include <sys/evcnt.h>
     92 #include <sys/kthread.h>
     93 #include <sys/cpu.h>
     94 
     95 #define	PRI_XCALL	0
     96 
     97 static void	xc_thread(void *);
     98 static uint64_t	xc_lowpri(u_int, xcfunc_t, void *, void *, struct cpu_info *);
     99 
    100 static kmutex_t		xc_lock;
    101 static xcfunc_t		xc_func;
    102 static void		*xc_arg1;
    103 static void		*xc_arg2;
    104 static kcondvar_t	xc_busy;
    105 static struct evcnt	xc_unicast_ev;
    106 static struct evcnt	xc_broadcast_ev;
    107 static uint64_t		xc_headp;
    108 static uint64_t		xc_tailp;
    109 static uint64_t		xc_donep;
    110 
    111 /*
    112  * xc_init_cpu:
    113  *
    114  *	Initialize the cross-call subsystem.  Called once for each CPU
    115  *	in the system as they are attached.
    116  */
    117 void
    118 xc_init_cpu(struct cpu_info *ci)
    119 {
    120 	static bool again;
    121 	int error;
    122 
    123 	if (!again) {
    124 		/* Autoconfiguration will prevent re-entry. */
    125 		again = true;
    126 		mutex_init(&xc_lock, MUTEX_DEFAULT, IPL_NONE);
    127 		cv_init(&xc_busy, "xcallbsy");
    128 		evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    129 		   "crosscall", "unicast");
    130 		evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    131 		   "crosscall", "broadcast");
    132 	}
    133 
    134 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    135 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    136 	    NULL, NULL, "xcall/%d", (int)ci->ci_cpuid);
    137 	if (error != 0)
    138 		panic("xc_init_cpu: error %d", error);
    139 }
    140 
    141 /*
    142  * xc_unicast:
    143  *
    144  *	Trigger a call on all CPUs in the system.
    145  */
    146 uint64_t
    147 xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
    148 {
    149 
    150 	if ((flags & XC_HIGHPRI) != 0) {
    151 		panic("xc_unicast: no high priority crosscalls yet");
    152 	} else {
    153 		return xc_lowpri(flags, func, arg1, arg2, NULL);
    154 	}
    155 }
    156 
    157 /*
    158  * xc_unicast:
    159  *
    160  *	Trigger a call on one CPU.
    161  */
    162 uint64_t
    163 xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    164 	   struct cpu_info *ci)
    165 {
    166 
    167 	if ((flags & XC_HIGHPRI) != 0) {
    168 		panic("xc_unicast: no high priority crosscalls yet");
    169 	} else {
    170 		KASSERT(ci != NULL);
    171 		return xc_lowpri(flags, func, arg1, arg2, ci);
    172 	}
    173 }
    174 
    175 /*
    176  * xc_lowpri:
    177  *
    178  *	Trigger a low priority call on one or more CPUs.
    179  */
    180 static uint64_t
    181 xc_lowpri(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    182 	  struct cpu_info *ci)
    183 {
    184 	CPU_INFO_ITERATOR cii;
    185 	u_int where;
    186 
    187 	mutex_enter(&xc_lock);
    188 	while (xc_headp != xc_tailp)
    189 		cv_wait(&xc_busy, &xc_lock);
    190 	xc_arg1 = arg1;
    191 	xc_arg2 = arg2;
    192 	xc_func = func;
    193 	if (ci == NULL) {
    194 		xc_broadcast_ev.ev_count++;
    195 		for (CPU_INFO_FOREACH(cii, ci)) {
    196 			xc_headp += 1;
    197 			ci->ci_data.cpu_xcall_pending = true;
    198 			cv_signal(&ci->ci_data.cpu_xcall);
    199 		}
    200 	} else {
    201 		xc_unicast_ev.ev_count++;
    202 		xc_headp += 1;
    203 		ci->ci_data.cpu_xcall_pending = true;
    204 		cv_signal(&ci->ci_data.cpu_xcall);
    205 	}
    206 	KASSERT(xc_tailp < xc_headp);
    207 	where = xc_headp;
    208 	mutex_exit(&xc_lock);
    209 
    210 	return where;
    211 }
    212 
    213 /*
    214  * xc_wait:
    215  *
    216  *	Wait for a cross call to complete.
    217  */
    218 void
    219 xc_wait(uint64_t where)
    220 {
    221 
    222 	if (xc_donep >= where)
    223 		return;
    224 
    225 	mutex_enter(&xc_lock);
    226 	while (xc_donep < where)
    227 		cv_wait(&xc_busy, &xc_lock);
    228 	mutex_exit(&xc_lock);
    229 }
    230 
    231 /*
    232  * xc_thread:
    233  *
    234  *	One thread per-CPU to dispatch low priority calls.
    235  */
    236 static void
    237 xc_thread(void *cookie)
    238 {
    239 	void *arg1, *arg2;
    240 	struct cpu_info *ci;
    241 	xcfunc_t func;
    242 
    243 	ci = curcpu();
    244 
    245 	mutex_enter(&xc_lock);
    246 	for (;;) {
    247 		while (!ci->ci_data.cpu_xcall_pending) {
    248 			if (xc_headp == xc_tailp)
    249 				cv_broadcast(&xc_busy);
    250 			cv_wait(&ci->ci_data.cpu_xcall, &xc_lock);
    251 			KASSERT(ci == curcpu());
    252 		}
    253 		ci->ci_data.cpu_xcall_pending = false;
    254 		func = xc_func;
    255 		arg1 = xc_arg1;
    256 		arg2 = xc_arg2;
    257 		xc_tailp++;
    258 		mutex_exit(&xc_lock);
    259 
    260 		(*func)(arg1, arg2);
    261 
    262 		mutex_enter(&xc_lock);
    263 		xc_donep++;
    264 	}
    265 	/* NOTREACHED */
    266 }
    267