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