Home | History | Annotate | Line # | Download | only in kern
subr_xcall.c revision 1.10
      1  1.10  uebayasi /*	$NetBSD: subr_xcall.c,v 1.10 2009/03/05 13:18:51 uebayasi Exp $	*/
      2   1.2        ad 
      3   1.2        ad /*-
      4   1.8        ad  * Copyright (c) 2007, 2008 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.2        ad  * by Andrew Doran.
      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.2        ad  * Future directions
     71   1.2        ad  *
     72   1.2        ad  *	Add a low-overhead mechanism to run cross calls in interrupt
     73   1.2        ad  *	context (XC_HIGHPRI).
     74   1.2        ad  */
     75   1.2        ad 
     76   1.2        ad #include <sys/cdefs.h>
     77  1.10  uebayasi __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.10 2009/03/05 13:18:51 uebayasi 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.2        ad static void	xc_thread(void *);
     89   1.2        ad static uint64_t	xc_lowpri(u_int, xcfunc_t, void *, void *, struct cpu_info *);
     90   1.2        ad 
     91   1.2        ad static kmutex_t		xc_lock;
     92   1.2        ad static xcfunc_t		xc_func;
     93   1.2        ad static void		*xc_arg1;
     94   1.2        ad static void		*xc_arg2;
     95   1.2        ad static kcondvar_t	xc_busy;
     96   1.2        ad static struct evcnt	xc_unicast_ev;
     97   1.2        ad static struct evcnt	xc_broadcast_ev;
     98   1.2        ad static uint64_t		xc_headp;
     99   1.2        ad static uint64_t		xc_tailp;
    100   1.2        ad static uint64_t		xc_donep;
    101   1.2        ad 
    102   1.2        ad /*
    103   1.2        ad  * xc_init_cpu:
    104   1.2        ad  *
    105   1.2        ad  *	Initialize the cross-call subsystem.  Called once for each CPU
    106   1.2        ad  *	in the system as they are attached.
    107   1.2        ad  */
    108   1.2        ad void
    109   1.2        ad xc_init_cpu(struct cpu_info *ci)
    110   1.2        ad {
    111   1.2        ad 	static bool again;
    112   1.2        ad 	int error;
    113   1.2        ad 
    114   1.2        ad 	if (!again) {
    115   1.2        ad 		/* Autoconfiguration will prevent re-entry. */
    116   1.2        ad 		again = true;
    117   1.2        ad 		mutex_init(&xc_lock, MUTEX_DEFAULT, IPL_NONE);
    118   1.2        ad 		cv_init(&xc_busy, "xcallbsy");
    119   1.2        ad 		evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
    120   1.2        ad 		   "crosscall", "unicast");
    121   1.2        ad 		evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
    122   1.2        ad 		   "crosscall", "broadcast");
    123   1.2        ad 	}
    124   1.2        ad 
    125   1.2        ad 	cv_init(&ci->ci_data.cpu_xcall, "xcall");
    126   1.2        ad 	error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
    127   1.6    martin 	    NULL, NULL, "xcall/%u", ci->ci_index);
    128   1.2        ad 	if (error != 0)
    129   1.2        ad 		panic("xc_init_cpu: error %d", error);
    130   1.2        ad }
    131   1.2        ad 
    132   1.2        ad /*
    133   1.7        ad  * xc_broadcast:
    134   1.2        ad  *
    135   1.2        ad  *	Trigger a call on all CPUs in the system.
    136   1.2        ad  */
    137   1.2        ad uint64_t
    138   1.2        ad xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
    139   1.2        ad {
    140   1.2        ad 
    141   1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    142   1.7        ad 		panic("xc_broadcast: no high priority crosscalls yet");
    143   1.2        ad 	} else {
    144   1.2        ad 		return xc_lowpri(flags, func, arg1, arg2, NULL);
    145   1.2        ad 	}
    146   1.2        ad }
    147   1.2        ad 
    148   1.2        ad /*
    149   1.2        ad  * xc_unicast:
    150   1.2        ad  *
    151   1.2        ad  *	Trigger a call on one CPU.
    152   1.2        ad  */
    153   1.2        ad uint64_t
    154   1.2        ad xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    155   1.2        ad 	   struct cpu_info *ci)
    156   1.2        ad {
    157   1.2        ad 
    158   1.2        ad 	if ((flags & XC_HIGHPRI) != 0) {
    159   1.2        ad 		panic("xc_unicast: no high priority crosscalls yet");
    160   1.2        ad 	} else {
    161   1.2        ad 		KASSERT(ci != NULL);
    162   1.2        ad 		return xc_lowpri(flags, func, arg1, arg2, ci);
    163   1.2        ad 	}
    164   1.2        ad }
    165   1.2        ad 
    166   1.2        ad /*
    167   1.2        ad  * xc_lowpri:
    168   1.2        ad  *
    169   1.2        ad  *	Trigger a low priority call on one or more CPUs.
    170   1.2        ad  */
    171   1.2        ad static uint64_t
    172   1.2        ad xc_lowpri(u_int flags, xcfunc_t func, void *arg1, void *arg2,
    173   1.2        ad 	  struct cpu_info *ci)
    174   1.2        ad {
    175   1.2        ad 	CPU_INFO_ITERATOR cii;
    176  1.10  uebayasi 	uint64_t where;
    177   1.2        ad 
    178   1.2        ad 	mutex_enter(&xc_lock);
    179   1.2        ad 	while (xc_headp != xc_tailp)
    180   1.2        ad 		cv_wait(&xc_busy, &xc_lock);
    181   1.2        ad 	xc_arg1 = arg1;
    182   1.2        ad 	xc_arg2 = arg2;
    183   1.2        ad 	xc_func = func;
    184   1.2        ad 	if (ci == NULL) {
    185   1.2        ad 		xc_broadcast_ev.ev_count++;
    186   1.2        ad 		for (CPU_INFO_FOREACH(cii, ci)) {
    187   1.8        ad 			if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
    188   1.8        ad 				continue;
    189   1.2        ad 			xc_headp += 1;
    190   1.2        ad 			ci->ci_data.cpu_xcall_pending = true;
    191   1.2        ad 			cv_signal(&ci->ci_data.cpu_xcall);
    192   1.2        ad 		}
    193   1.2        ad 	} else {
    194   1.2        ad 		xc_unicast_ev.ev_count++;
    195   1.2        ad 		xc_headp += 1;
    196   1.2        ad 		ci->ci_data.cpu_xcall_pending = true;
    197   1.2        ad 		cv_signal(&ci->ci_data.cpu_xcall);
    198   1.2        ad 	}
    199   1.2        ad 	KASSERT(xc_tailp < xc_headp);
    200   1.2        ad 	where = xc_headp;
    201   1.2        ad 	mutex_exit(&xc_lock);
    202   1.2        ad 
    203   1.2        ad 	return where;
    204   1.2        ad }
    205   1.2        ad 
    206   1.2        ad /*
    207   1.2        ad  * xc_wait:
    208   1.2        ad  *
    209   1.2        ad  *	Wait for a cross call to complete.
    210   1.2        ad  */
    211   1.2        ad void
    212   1.2        ad xc_wait(uint64_t where)
    213   1.2        ad {
    214   1.2        ad 
    215   1.2        ad 	if (xc_donep >= where)
    216   1.2        ad 		return;
    217   1.2        ad 
    218   1.2        ad 	mutex_enter(&xc_lock);
    219   1.2        ad 	while (xc_donep < where)
    220   1.2        ad 		cv_wait(&xc_busy, &xc_lock);
    221   1.2        ad 	mutex_exit(&xc_lock);
    222   1.2        ad }
    223   1.2        ad 
    224   1.2        ad /*
    225   1.2        ad  * xc_thread:
    226   1.2        ad  *
    227   1.2        ad  *	One thread per-CPU to dispatch low priority calls.
    228   1.2        ad  */
    229   1.2        ad static void
    230   1.2        ad xc_thread(void *cookie)
    231   1.2        ad {
    232   1.2        ad 	void *arg1, *arg2;
    233   1.2        ad 	struct cpu_info *ci;
    234   1.2        ad 	xcfunc_t func;
    235   1.2        ad 
    236   1.2        ad 	ci = curcpu();
    237   1.2        ad 
    238   1.2        ad 	mutex_enter(&xc_lock);
    239   1.2        ad 	for (;;) {
    240   1.2        ad 		while (!ci->ci_data.cpu_xcall_pending) {
    241   1.2        ad 			if (xc_headp == xc_tailp)
    242   1.2        ad 				cv_broadcast(&xc_busy);
    243   1.2        ad 			cv_wait(&ci->ci_data.cpu_xcall, &xc_lock);
    244   1.2        ad 			KASSERT(ci == curcpu());
    245   1.2        ad 		}
    246   1.2        ad 		ci->ci_data.cpu_xcall_pending = false;
    247   1.2        ad 		func = xc_func;
    248   1.2        ad 		arg1 = xc_arg1;
    249   1.2        ad 		arg2 = xc_arg2;
    250   1.2        ad 		xc_tailp++;
    251   1.2        ad 		mutex_exit(&xc_lock);
    252   1.2        ad 
    253   1.2        ad 		(*func)(arg1, arg2);
    254   1.2        ad 
    255   1.2        ad 		mutex_enter(&xc_lock);
    256   1.2        ad 		xc_donep++;
    257   1.2        ad 	}
    258   1.2        ad 	/* NOTREACHED */
    259   1.2        ad }
    260