subr_xcall.c revision 1.8.2.4 1 1.8.2.4 yamt /* $NetBSD: subr_xcall.c,v 1.8.2.4 2010/08/11 22:54:42 yamt Exp $ */
2 1.2 ad
3 1.2 ad /*-
4 1.8.2.4 yamt * 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.8.2.4 yamt * 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.8.2.4 yamt * A low-overhead mechanism for high priority calls (XC_HIGHPRI) is
71 1.8.2.4 yamt * also provided. The function to be executed runs on a software
72 1.8.2.4 yamt * interrupt context, at SOFTINT_CLOCK level, and is expected to be
73 1.8.2.4 yamt * very lightweight, e.g. avoid blocking.
74 1.2 ad */
75 1.2 ad
76 1.2 ad #include <sys/cdefs.h>
77 1.8.2.4 yamt __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.8.2.4 2010/08/11 22:54:42 yamt 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.8.2.4 yamt /* Cross-call state box. */
89 1.8.2.4 yamt typedef struct {
90 1.8.2.4 yamt kmutex_t xc_lock;
91 1.8.2.4 yamt kcondvar_t xc_busy;
92 1.8.2.4 yamt xcfunc_t xc_func;
93 1.8.2.4 yamt void * xc_arg1;
94 1.8.2.4 yamt void * xc_arg2;
95 1.8.2.4 yamt uint64_t xc_headp;
96 1.8.2.4 yamt uint64_t xc_donep;
97 1.8.2.4 yamt } xc_state_t;
98 1.8.2.4 yamt
99 1.8.2.4 yamt /* Bit indicating high (1) or low (0) priority. */
100 1.8.2.4 yamt #define XC_PRI_BIT (1ULL << 63)
101 1.8.2.4 yamt
102 1.8.2.4 yamt /* Low priority xcall structures. */
103 1.8.2.4 yamt static xc_state_t xc_low_pri;
104 1.8.2.4 yamt static uint64_t xc_tailp;
105 1.8.2.4 yamt
106 1.8.2.4 yamt /* High priority xcall structures. */
107 1.8.2.4 yamt static xc_state_t xc_high_pri;
108 1.8.2.4 yamt static void * xc_sih;
109 1.2 ad
110 1.8.2.4 yamt /* Event counters. */
111 1.2 ad static struct evcnt xc_unicast_ev;
112 1.2 ad static struct evcnt xc_broadcast_ev;
113 1.8.2.4 yamt
114 1.8.2.4 yamt static void xc_init(void);
115 1.8.2.4 yamt static void xc_thread(void *);
116 1.8.2.4 yamt static void xc_highpri_intr(void *);
117 1.8.2.4 yamt
118 1.8.2.4 yamt static inline uint64_t xc_highpri(xcfunc_t, void *, void *, struct cpu_info *);
119 1.8.2.4 yamt static inline uint64_t xc_lowpri(xcfunc_t, void *, void *, struct cpu_info *);
120 1.8.2.4 yamt
121 1.8.2.4 yamt /*
122 1.8.2.4 yamt * xc_init:
123 1.8.2.4 yamt *
124 1.8.2.4 yamt * Initialize low and high priority cross-call structures.
125 1.8.2.4 yamt */
126 1.8.2.4 yamt static void
127 1.8.2.4 yamt xc_init(void)
128 1.8.2.4 yamt {
129 1.8.2.4 yamt xc_state_t *xclo = &xc_low_pri, *xchi = &xc_high_pri;
130 1.8.2.4 yamt
131 1.8.2.4 yamt memset(xclo, 0, sizeof(xc_state_t));
132 1.8.2.4 yamt mutex_init(&xclo->xc_lock, MUTEX_DEFAULT, IPL_NONE);
133 1.8.2.4 yamt cv_init(&xclo->xc_busy, "xclocv");
134 1.8.2.4 yamt xc_tailp = 0;
135 1.8.2.4 yamt
136 1.8.2.4 yamt memset(xchi, 0, sizeof(xc_state_t));
137 1.8.2.4 yamt mutex_init(&xchi->xc_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
138 1.8.2.4 yamt cv_init(&xchi->xc_busy, "xchicv");
139 1.8.2.4 yamt xc_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
140 1.8.2.4 yamt xc_highpri_intr, NULL);
141 1.8.2.4 yamt KASSERT(xc_sih != NULL);
142 1.8.2.4 yamt
143 1.8.2.4 yamt evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
144 1.8.2.4 yamt "crosscall", "unicast");
145 1.8.2.4 yamt evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
146 1.8.2.4 yamt "crosscall", "broadcast");
147 1.8.2.4 yamt }
148 1.2 ad
149 1.2 ad /*
150 1.2 ad * xc_init_cpu:
151 1.2 ad *
152 1.2 ad * Initialize the cross-call subsystem. Called once for each CPU
153 1.2 ad * in the system as they are attached.
154 1.2 ad */
155 1.2 ad void
156 1.2 ad xc_init_cpu(struct cpu_info *ci)
157 1.2 ad {
158 1.8.2.3 yamt static bool again = false;
159 1.2 ad int error;
160 1.2 ad
161 1.2 ad if (!again) {
162 1.2 ad /* Autoconfiguration will prevent re-entry. */
163 1.8.2.4 yamt xc_init();
164 1.2 ad again = true;
165 1.2 ad }
166 1.2 ad cv_init(&ci->ci_data.cpu_xcall, "xcall");
167 1.2 ad error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
168 1.6 martin NULL, NULL, "xcall/%u", ci->ci_index);
169 1.8.2.4 yamt KASSERT(error == 0);
170 1.2 ad }
171 1.2 ad
172 1.2 ad /*
173 1.7 ad * xc_broadcast:
174 1.2 ad *
175 1.2 ad * Trigger a call on all CPUs in the system.
176 1.2 ad */
177 1.2 ad uint64_t
178 1.2 ad xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
179 1.2 ad {
180 1.2 ad
181 1.8.2.4 yamt KASSERT(!cpu_intr_p() && !cpu_softintr_p());
182 1.8.2.4 yamt
183 1.2 ad if ((flags & XC_HIGHPRI) != 0) {
184 1.8.2.4 yamt return xc_highpri(func, arg1, arg2, NULL);
185 1.2 ad } else {
186 1.8.2.4 yamt return xc_lowpri(func, arg1, arg2, NULL);
187 1.2 ad }
188 1.2 ad }
189 1.2 ad
190 1.2 ad /*
191 1.2 ad * xc_unicast:
192 1.2 ad *
193 1.2 ad * Trigger a call on one CPU.
194 1.2 ad */
195 1.2 ad uint64_t
196 1.2 ad xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
197 1.2 ad struct cpu_info *ci)
198 1.2 ad {
199 1.2 ad
200 1.8.2.4 yamt KASSERT(ci != NULL);
201 1.8.2.4 yamt KASSERT(!cpu_intr_p() && !cpu_softintr_p());
202 1.8.2.4 yamt
203 1.2 ad if ((flags & XC_HIGHPRI) != 0) {
204 1.8.2.4 yamt return xc_highpri(func, arg1, arg2, ci);
205 1.8.2.4 yamt } else {
206 1.8.2.4 yamt return xc_lowpri(func, arg1, arg2, ci);
207 1.8.2.4 yamt }
208 1.8.2.4 yamt }
209 1.8.2.4 yamt
210 1.8.2.4 yamt /*
211 1.8.2.4 yamt * xc_wait:
212 1.8.2.4 yamt *
213 1.8.2.4 yamt * Wait for a cross call to complete.
214 1.8.2.4 yamt */
215 1.8.2.4 yamt void
216 1.8.2.4 yamt xc_wait(uint64_t where)
217 1.8.2.4 yamt {
218 1.8.2.4 yamt xc_state_t *xc;
219 1.8.2.4 yamt
220 1.8.2.4 yamt KASSERT(!cpu_intr_p() && !cpu_softintr_p());
221 1.8.2.4 yamt
222 1.8.2.4 yamt /* Determine whether it is high or low priority cross-call. */
223 1.8.2.4 yamt if ((where & XC_PRI_BIT) != 0) {
224 1.8.2.4 yamt xc = &xc_high_pri;
225 1.8.2.4 yamt where &= ~XC_PRI_BIT;
226 1.2 ad } else {
227 1.8.2.4 yamt xc = &xc_low_pri;
228 1.8.2.4 yamt }
229 1.8.2.4 yamt
230 1.8.2.4 yamt /* Fast path, if already done. */
231 1.8.2.4 yamt if (xc->xc_donep >= where) {
232 1.8.2.4 yamt return;
233 1.8.2.4 yamt }
234 1.8.2.4 yamt
235 1.8.2.4 yamt /* Slow path: block until awoken. */
236 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
237 1.8.2.4 yamt while (xc->xc_donep < where) {
238 1.8.2.4 yamt cv_wait(&xc->xc_busy, &xc->xc_lock);
239 1.2 ad }
240 1.8.2.4 yamt mutex_exit(&xc->xc_lock);
241 1.2 ad }
242 1.2 ad
243 1.2 ad /*
244 1.2 ad * xc_lowpri:
245 1.2 ad *
246 1.2 ad * Trigger a low priority call on one or more CPUs.
247 1.2 ad */
248 1.8.2.4 yamt static inline uint64_t
249 1.8.2.4 yamt xc_lowpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
250 1.2 ad {
251 1.8.2.4 yamt xc_state_t *xc = &xc_low_pri;
252 1.2 ad CPU_INFO_ITERATOR cii;
253 1.8.2.2 yamt uint64_t where;
254 1.2 ad
255 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
256 1.8.2.4 yamt while (xc->xc_headp != xc_tailp) {
257 1.8.2.4 yamt cv_wait(&xc->xc_busy, &xc->xc_lock);
258 1.8.2.4 yamt }
259 1.8.2.4 yamt xc->xc_arg1 = arg1;
260 1.8.2.4 yamt xc->xc_arg2 = arg2;
261 1.8.2.4 yamt xc->xc_func = func;
262 1.2 ad if (ci == NULL) {
263 1.2 ad xc_broadcast_ev.ev_count++;
264 1.2 ad for (CPU_INFO_FOREACH(cii, ci)) {
265 1.8 ad if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
266 1.8 ad continue;
267 1.8.2.4 yamt xc->xc_headp += 1;
268 1.2 ad ci->ci_data.cpu_xcall_pending = true;
269 1.2 ad cv_signal(&ci->ci_data.cpu_xcall);
270 1.2 ad }
271 1.2 ad } else {
272 1.2 ad xc_unicast_ev.ev_count++;
273 1.8.2.4 yamt xc->xc_headp += 1;
274 1.2 ad ci->ci_data.cpu_xcall_pending = true;
275 1.2 ad cv_signal(&ci->ci_data.cpu_xcall);
276 1.2 ad }
277 1.8.2.4 yamt KASSERT(xc_tailp < xc->xc_headp);
278 1.8.2.4 yamt where = xc->xc_headp;
279 1.8.2.4 yamt mutex_exit(&xc->xc_lock);
280 1.2 ad
281 1.8.2.4 yamt /* Return a low priority ticket. */
282 1.8.2.4 yamt KASSERT((where & XC_PRI_BIT) == 0);
283 1.2 ad return where;
284 1.2 ad }
285 1.2 ad
286 1.2 ad /*
287 1.2 ad * xc_thread:
288 1.2 ad *
289 1.2 ad * One thread per-CPU to dispatch low priority calls.
290 1.2 ad */
291 1.2 ad static void
292 1.2 ad xc_thread(void *cookie)
293 1.2 ad {
294 1.8.2.4 yamt struct cpu_info *ci = curcpu();
295 1.8.2.4 yamt xc_state_t *xc = &xc_low_pri;
296 1.2 ad void *arg1, *arg2;
297 1.2 ad xcfunc_t func;
298 1.2 ad
299 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
300 1.2 ad for (;;) {
301 1.2 ad while (!ci->ci_data.cpu_xcall_pending) {
302 1.8.2.4 yamt if (xc->xc_headp == xc_tailp) {
303 1.8.2.4 yamt cv_broadcast(&xc->xc_busy);
304 1.8.2.4 yamt }
305 1.8.2.4 yamt cv_wait(&ci->ci_data.cpu_xcall, &xc->xc_lock);
306 1.2 ad KASSERT(ci == curcpu());
307 1.2 ad }
308 1.2 ad ci->ci_data.cpu_xcall_pending = false;
309 1.8.2.4 yamt func = xc->xc_func;
310 1.8.2.4 yamt arg1 = xc->xc_arg1;
311 1.8.2.4 yamt arg2 = xc->xc_arg2;
312 1.2 ad xc_tailp++;
313 1.8.2.4 yamt mutex_exit(&xc->xc_lock);
314 1.2 ad
315 1.8.2.4 yamt KASSERT(func != NULL);
316 1.2 ad (*func)(arg1, arg2);
317 1.2 ad
318 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
319 1.8.2.4 yamt xc->xc_donep++;
320 1.2 ad }
321 1.2 ad /* NOTREACHED */
322 1.2 ad }
323 1.8.2.4 yamt
324 1.8.2.4 yamt /*
325 1.8.2.4 yamt * xc_ipi_handler:
326 1.8.2.4 yamt *
327 1.8.2.4 yamt * Handler of cross-call IPI.
328 1.8.2.4 yamt */
329 1.8.2.4 yamt void
330 1.8.2.4 yamt xc_ipi_handler(void)
331 1.8.2.4 yamt {
332 1.8.2.4 yamt /* Executes xc_highpri_intr() via software interrupt. */
333 1.8.2.4 yamt softint_schedule(xc_sih);
334 1.8.2.4 yamt }
335 1.8.2.4 yamt
336 1.8.2.4 yamt /*
337 1.8.2.4 yamt * xc_highpri_intr:
338 1.8.2.4 yamt *
339 1.8.2.4 yamt * A software interrupt handler for high priority calls.
340 1.8.2.4 yamt */
341 1.8.2.4 yamt static void
342 1.8.2.4 yamt xc_highpri_intr(void *dummy)
343 1.8.2.4 yamt {
344 1.8.2.4 yamt xc_state_t *xc = &xc_high_pri;
345 1.8.2.4 yamt void *arg1, *arg2;
346 1.8.2.4 yamt xcfunc_t func;
347 1.8.2.4 yamt
348 1.8.2.4 yamt KASSERT(cpu_softintr_p());
349 1.8.2.4 yamt /*
350 1.8.2.4 yamt * Lock-less fetch of function and its arguments.
351 1.8.2.4 yamt * Safe since it cannot change at this point.
352 1.8.2.4 yamt */
353 1.8.2.4 yamt KASSERT(xc->xc_donep < xc->xc_headp);
354 1.8.2.4 yamt func = xc->xc_func;
355 1.8.2.4 yamt arg1 = xc->xc_arg1;
356 1.8.2.4 yamt arg2 = xc->xc_arg2;
357 1.8.2.4 yamt
358 1.8.2.4 yamt KASSERT(func != NULL);
359 1.8.2.4 yamt (*func)(arg1, arg2);
360 1.8.2.4 yamt
361 1.8.2.4 yamt /*
362 1.8.2.4 yamt * Note the request as done, and if we have reached the head,
363 1.8.2.4 yamt * cross-call has been processed - notify waiters, if any.
364 1.8.2.4 yamt */
365 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
366 1.8.2.4 yamt if (++xc->xc_donep == xc->xc_headp) {
367 1.8.2.4 yamt cv_broadcast(&xc->xc_busy);
368 1.8.2.4 yamt }
369 1.8.2.4 yamt mutex_exit(&xc->xc_lock);
370 1.8.2.4 yamt }
371 1.8.2.4 yamt
372 1.8.2.4 yamt /*
373 1.8.2.4 yamt * xc_highpri:
374 1.8.2.4 yamt *
375 1.8.2.4 yamt * Trigger a high priority call on one or more CPUs.
376 1.8.2.4 yamt */
377 1.8.2.4 yamt static inline uint64_t
378 1.8.2.4 yamt xc_highpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
379 1.8.2.4 yamt {
380 1.8.2.4 yamt xc_state_t *xc = &xc_high_pri;
381 1.8.2.4 yamt uint64_t where;
382 1.8.2.4 yamt
383 1.8.2.4 yamt mutex_enter(&xc->xc_lock);
384 1.8.2.4 yamt while (xc->xc_headp != xc->xc_donep) {
385 1.8.2.4 yamt cv_wait(&xc->xc_busy, &xc->xc_lock);
386 1.8.2.4 yamt }
387 1.8.2.4 yamt xc->xc_func = func;
388 1.8.2.4 yamt xc->xc_arg1 = arg1;
389 1.8.2.4 yamt xc->xc_arg2 = arg2;
390 1.8.2.4 yamt xc->xc_headp += (ci ? 1 : ncpu);
391 1.8.2.4 yamt where = xc->xc_headp;
392 1.8.2.4 yamt mutex_exit(&xc->xc_lock);
393 1.8.2.4 yamt
394 1.8.2.4 yamt /*
395 1.8.2.4 yamt * Send the IPI once lock is released.
396 1.8.2.4 yamt * Note: it will handle the local CPU case.
397 1.8.2.4 yamt */
398 1.8.2.4 yamt
399 1.8.2.4 yamt #ifdef MULTIPROCESSOR
400 1.8.2.4 yamt kpreempt_disable();
401 1.8.2.4 yamt if (curcpu() == ci) {
402 1.8.2.4 yamt /* Unicast: local CPU. */
403 1.8.2.4 yamt xc_ipi_handler();
404 1.8.2.4 yamt } else if (ci) {
405 1.8.2.4 yamt /* Unicast: remote CPU. */
406 1.8.2.4 yamt xc_send_ipi(ci);
407 1.8.2.4 yamt } else {
408 1.8.2.4 yamt /* Broadcast: all, including local. */
409 1.8.2.4 yamt xc_send_ipi(NULL);
410 1.8.2.4 yamt xc_ipi_handler();
411 1.8.2.4 yamt }
412 1.8.2.4 yamt kpreempt_enable();
413 1.8.2.4 yamt #else
414 1.8.2.4 yamt KASSERT(curcpu() == ci);
415 1.8.2.4 yamt xc_ipi_handler();
416 1.8.2.4 yamt #endif
417 1.8.2.4 yamt
418 1.8.2.4 yamt /* Indicate a high priority ticket. */
419 1.8.2.4 yamt return (where | XC_PRI_BIT);
420 1.8.2.4 yamt }
421