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