subr_pcu.c revision 1.3.4.4 1 1.3.4.2 rmind /* $NetBSD: subr_pcu.c,v 1.3.4.4 2011/06/12 00:24:29 rmind Exp $ */
2 1.3.4.2 rmind
3 1.3.4.2 rmind /*-
4 1.3.4.2 rmind * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.3.4.2 rmind * All rights reserved.
6 1.3.4.2 rmind *
7 1.3.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.3.4.2 rmind * by Mindaugas Rasiukevicius.
9 1.3.4.2 rmind *
10 1.3.4.2 rmind * Redistribution and use in source and binary forms, with or without
11 1.3.4.2 rmind * modification, are permitted provided that the following conditions
12 1.3.4.2 rmind * are met:
13 1.3.4.2 rmind * 1. Redistributions of source code must retain the above copyright
14 1.3.4.2 rmind * notice, this list of conditions and the following disclaimer.
15 1.3.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.4.2 rmind * notice, this list of conditions and the following disclaimer in the
17 1.3.4.2 rmind * documentation and/or other materials provided with the distribution.
18 1.3.4.2 rmind *
19 1.3.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
30 1.3.4.2 rmind */
31 1.3.4.2 rmind
32 1.3.4.2 rmind /*
33 1.3.4.2 rmind * Per CPU Unit (PCU) - is an interface to manage synchronization of any
34 1.3.4.2 rmind * per CPU context (unit) tied with LWP context. Typical use: FPU state.
35 1.3.4.2 rmind *
36 1.3.4.2 rmind * Concurrency notes:
37 1.3.4.2 rmind *
38 1.3.4.2 rmind * PCU state may be loaded only by the current LWP, that is, curlwp.
39 1.3.4.2 rmind * Therefore, only LWP itself can set a CPU for lwp_t::l_pcu_cpu[id].
40 1.3.4.2 rmind *
41 1.3.4.2 rmind * Request for a PCU release can be from owner LWP (whether PCU state
42 1.3.4.2 rmind * is on current CPU or remote CPU) or any other LWP running on that
43 1.3.4.2 rmind * CPU (in such case, owner LWP is on a remote CPU or sleeping).
44 1.3.4.2 rmind *
45 1.3.4.2 rmind * In any case, PCU state can only be changed from the running CPU.
46 1.3.4.2 rmind * If said PCU state is on the remote CPU, a cross-call will be sent
47 1.3.4.2 rmind * by the owner LWP. Therefore struct cpu_info::ci_pcu_curlwp[id]
48 1.3.4.2 rmind * may only be changed by current CPU, and lwp_t::l_pcu_cpu[id] may
49 1.3.4.2 rmind * only be unset by the CPU which has PCU state loaded.
50 1.3.4.2 rmind *
51 1.3.4.2 rmind * There is a race condition: LWP may have a PCU state on a remote CPU,
52 1.3.4.2 rmind * which it requests to be released via cross-call. At the same time,
53 1.3.4.2 rmind * other LWP on remote CPU might release existing PCU state and load
54 1.3.4.2 rmind * its own one. Cross-call may arrive after this and release different
55 1.3.4.2 rmind * PCU state than intended. In such case, such LWP would re-load its
56 1.3.4.2 rmind * PCU state again.
57 1.3.4.2 rmind */
58 1.3.4.2 rmind
59 1.3.4.2 rmind #include <sys/cdefs.h>
60 1.3.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: subr_pcu.c,v 1.3.4.4 2011/06/12 00:24:29 rmind Exp $");
61 1.3.4.2 rmind
62 1.3.4.2 rmind #include <sys/param.h>
63 1.3.4.2 rmind #include <sys/cpu.h>
64 1.3.4.2 rmind #include <sys/lwp.h>
65 1.3.4.2 rmind #include <sys/pcu.h>
66 1.3.4.2 rmind #include <sys/xcall.h>
67 1.3.4.2 rmind
68 1.3.4.2 rmind #if PCU_UNIT_COUNT > 0
69 1.3.4.2 rmind
70 1.3.4.4 rmind static void pcu_lwp_op(const pcu_ops_t *, lwp_t *, int);
71 1.3.4.4 rmind
72 1.3.4.2 rmind #define PCU_SAVE 0x01 /* Save PCU state to the LWP. */
73 1.3.4.2 rmind #define PCU_RELEASE 0x02 /* Release PCU state on the CPU. */
74 1.3.4.2 rmind
75 1.3.4.3 rmind /* XXX */
76 1.3.4.3 rmind extern const pcu_ops_t * const pcu_ops_md_defs[];
77 1.3.4.3 rmind
78 1.3.4.2 rmind void
79 1.3.4.3 rmind pcu_switchpoint(lwp_t *l)
80 1.3.4.2 rmind {
81 1.3.4.3 rmind const uint32_t pcu_inuse = l->l_pcu_used;
82 1.3.4.3 rmind u_int id;
83 1.3.4.3 rmind /* int s; */
84 1.3.4.2 rmind
85 1.3.4.3 rmind KASSERT(l == curlwp);
86 1.3.4.3 rmind
87 1.3.4.3 rmind if (__predict_true(pcu_inuse == 0)) {
88 1.3.4.3 rmind /* PCUs are not in use. */
89 1.3.4.3 rmind return;
90 1.3.4.3 rmind }
91 1.3.4.3 rmind /* s = splsoftclock(); */
92 1.3.4.3 rmind for (id = 0; id < PCU_UNIT_COUNT; id++) {
93 1.3.4.3 rmind if ((pcu_inuse & (1 << id)) == 0) {
94 1.3.4.3 rmind continue;
95 1.3.4.3 rmind }
96 1.3.4.3 rmind struct cpu_info * const pcu_ci = l->l_pcu_cpu[id];
97 1.3.4.3 rmind if (pcu_ci == NULL || pcu_ci == l->l_cpu) {
98 1.3.4.3 rmind continue;
99 1.3.4.3 rmind }
100 1.3.4.3 rmind const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
101 1.3.4.3 rmind pcu->pcu_state_release(l);
102 1.3.4.3 rmind }
103 1.3.4.3 rmind /* splx(s); */
104 1.3.4.2 rmind }
105 1.3.4.2 rmind
106 1.3.4.4 rmind void
107 1.3.4.4 rmind pcu_discard_all(lwp_t *l)
108 1.3.4.4 rmind {
109 1.3.4.4 rmind const uint32_t pcu_inuse = l->l_pcu_used;
110 1.3.4.4 rmind
111 1.3.4.4 rmind KASSERT(l == curlwp || ((l->l_flag & LW_SYSTEM) && pcu_inuse == 0));
112 1.3.4.4 rmind
113 1.3.4.4 rmind if (__predict_true(pcu_inuse == 0)) {
114 1.3.4.4 rmind /* PCUs are not in use. */
115 1.3.4.4 rmind return;
116 1.3.4.4 rmind }
117 1.3.4.4 rmind const int s = splsoftclock();
118 1.3.4.4 rmind for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
119 1.3.4.4 rmind if ((pcu_inuse & (1 << id)) == 0) {
120 1.3.4.4 rmind continue;
121 1.3.4.4 rmind }
122 1.3.4.4 rmind if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
123 1.3.4.4 rmind continue;
124 1.3.4.4 rmind }
125 1.3.4.4 rmind const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
126 1.3.4.4 rmind /*
127 1.3.4.4 rmind * We aren't releasing since this LWP isn't giving up PCU,
128 1.3.4.4 rmind * just saving it.
129 1.3.4.4 rmind */
130 1.3.4.4 rmind pcu_lwp_op(pcu, l, PCU_RELEASE);
131 1.3.4.4 rmind }
132 1.3.4.4 rmind l->l_pcu_used = 0;
133 1.3.4.4 rmind splx(s);
134 1.3.4.4 rmind }
135 1.3.4.4 rmind
136 1.3.4.4 rmind void
137 1.3.4.4 rmind pcu_save_all(lwp_t *l)
138 1.3.4.4 rmind {
139 1.3.4.4 rmind const uint32_t pcu_inuse = l->l_pcu_used;
140 1.3.4.4 rmind
141 1.3.4.4 rmind KASSERT(l == curlwp || ((l->l_flag & LW_SYSTEM) && pcu_inuse == 0));
142 1.3.4.4 rmind
143 1.3.4.4 rmind if (__predict_true(pcu_inuse == 0)) {
144 1.3.4.4 rmind /* PCUs are not in use. */
145 1.3.4.4 rmind return;
146 1.3.4.4 rmind }
147 1.3.4.4 rmind const int s = splsoftclock();
148 1.3.4.4 rmind for (u_int id = 0; id < PCU_UNIT_COUNT; id++) {
149 1.3.4.4 rmind if ((pcu_inuse & (1 << id)) == 0) {
150 1.3.4.4 rmind continue;
151 1.3.4.4 rmind }
152 1.3.4.4 rmind if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
153 1.3.4.4 rmind continue;
154 1.3.4.4 rmind }
155 1.3.4.4 rmind const pcu_ops_t * const pcu = pcu_ops_md_defs[id];
156 1.3.4.4 rmind /*
157 1.3.4.4 rmind * We aren't releasing since this LWP isn't giving up PCU,
158 1.3.4.4 rmind * just saving it.
159 1.3.4.4 rmind */
160 1.3.4.4 rmind pcu_lwp_op(pcu, l, PCU_SAVE);
161 1.3.4.4 rmind }
162 1.3.4.4 rmind splx(s);
163 1.3.4.4 rmind }
164 1.3.4.4 rmind
165 1.3.4.2 rmind /*
166 1.3.4.3 rmind * pcu_do_op: save/release PCU state on the current CPU.
167 1.3.4.2 rmind *
168 1.3.4.2 rmind * => Must be called at IPL_SOFTCLOCK or from the soft-interrupt.
169 1.3.4.2 rmind */
170 1.3.4.3 rmind static inline void
171 1.3.4.3 rmind pcu_do_op(const pcu_ops_t *pcu, lwp_t * const l, const int flags)
172 1.3.4.2 rmind {
173 1.3.4.3 rmind struct cpu_info * const ci = curcpu();
174 1.3.4.2 rmind const u_int id = pcu->pcu_id;
175 1.3.4.2 rmind
176 1.3.4.4 rmind KASSERT(l->l_pcu_cpu[id] == ci);
177 1.3.4.3 rmind
178 1.3.4.2 rmind if (flags & PCU_SAVE) {
179 1.3.4.3 rmind pcu->pcu_state_save(l);
180 1.3.4.2 rmind }
181 1.3.4.2 rmind if (flags & PCU_RELEASE) {
182 1.3.4.3 rmind pcu->pcu_state_release(l);
183 1.3.4.2 rmind ci->ci_pcu_curlwp[id] = NULL;
184 1.3.4.2 rmind l->l_pcu_cpu[id] = NULL;
185 1.3.4.2 rmind }
186 1.3.4.2 rmind }
187 1.3.4.2 rmind
188 1.3.4.2 rmind /*
189 1.3.4.3 rmind * pcu_cpu_op: helper routine to call pcu_do_op() via xcall(9) or
190 1.3.4.3 rmind * by pcu_load.
191 1.3.4.3 rmind */
192 1.3.4.3 rmind static void
193 1.3.4.3 rmind pcu_cpu_op(const pcu_ops_t *pcu, const int flags)
194 1.3.4.3 rmind {
195 1.3.4.3 rmind const u_int id = pcu->pcu_id;
196 1.3.4.3 rmind lwp_t * const l = curcpu()->ci_pcu_curlwp[id];
197 1.3.4.3 rmind
198 1.3.4.3 rmind //KASSERT(cpu_softintr_p());
199 1.3.4.3 rmind
200 1.3.4.3 rmind /* If no state - nothing to do. */
201 1.3.4.3 rmind if (l == NULL) {
202 1.3.4.3 rmind return;
203 1.3.4.3 rmind }
204 1.3.4.3 rmind pcu_do_op(pcu, l, flags);
205 1.3.4.3 rmind }
206 1.3.4.3 rmind
207 1.3.4.3 rmind /*
208 1.3.4.2 rmind * pcu_lwp_op: perform PCU state save, release or both operations on LWP.
209 1.3.4.2 rmind */
210 1.3.4.2 rmind static void
211 1.3.4.2 rmind pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, int flags)
212 1.3.4.2 rmind {
213 1.3.4.2 rmind const u_int id = pcu->pcu_id;
214 1.3.4.2 rmind struct cpu_info *ci;
215 1.3.4.2 rmind uint64_t where;
216 1.3.4.2 rmind int s;
217 1.3.4.2 rmind
218 1.3.4.2 rmind /*
219 1.3.4.2 rmind * Caller should have re-checked if there is any state to manage.
220 1.3.4.2 rmind * Block the interrupts and inspect again, since cross-call sent
221 1.3.4.2 rmind * by remote CPU could have changed the state.
222 1.3.4.2 rmind */
223 1.3.4.2 rmind s = splsoftclock();
224 1.3.4.2 rmind ci = l->l_pcu_cpu[id];
225 1.3.4.2 rmind if (ci == curcpu()) {
226 1.3.4.2 rmind /*
227 1.3.4.2 rmind * State is on the current CPU - just perform the operations.
228 1.3.4.2 rmind */
229 1.3.4.3 rmind KASSERTMSG(ci->ci_pcu_curlwp[id] == l,
230 1.3.4.3 rmind ("%s: cpu%u: pcu_curlwp[%u] (%p) != l (%p)",
231 1.3.4.3 rmind __func__, cpu_index(ci), id, ci->ci_pcu_curlwp[id], l));
232 1.3.4.3 rmind pcu_do_op(pcu, l, flags);
233 1.3.4.2 rmind splx(s);
234 1.3.4.2 rmind return;
235 1.3.4.2 rmind }
236 1.3.4.2 rmind splx(s);
237 1.3.4.2 rmind
238 1.3.4.2 rmind if (__predict_false(ci == NULL)) {
239 1.3.4.2 rmind /* Cross-call has won the race - no state to manage. */
240 1.3.4.2 rmind return;
241 1.3.4.2 rmind }
242 1.3.4.2 rmind
243 1.3.4.2 rmind /*
244 1.3.4.2 rmind * State is on the remote CPU - perform the operations there.
245 1.3.4.2 rmind * Note: there is a race condition; see description in the top.
246 1.3.4.2 rmind */
247 1.3.4.2 rmind where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
248 1.3.4.2 rmind __UNCONST(pcu), (void *)(uintptr_t)flags, ci);
249 1.3.4.2 rmind xc_wait(where);
250 1.3.4.2 rmind
251 1.3.4.2 rmind KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL);
252 1.3.4.2 rmind }
253 1.3.4.2 rmind
254 1.3.4.2 rmind /*
255 1.3.4.2 rmind * pcu_load: load/initialize the PCU state of current LWP on current CPU.
256 1.3.4.2 rmind */
257 1.3.4.2 rmind void
258 1.3.4.2 rmind pcu_load(const pcu_ops_t *pcu)
259 1.3.4.2 rmind {
260 1.3.4.2 rmind const u_int id = pcu->pcu_id;
261 1.3.4.2 rmind struct cpu_info *ci, *curci;
262 1.3.4.3 rmind lwp_t * const l = curlwp;
263 1.3.4.2 rmind uint64_t where;
264 1.3.4.2 rmind int s;
265 1.3.4.2 rmind
266 1.3.4.2 rmind KASSERT(!cpu_intr_p() && !cpu_softintr_p());
267 1.3.4.2 rmind
268 1.3.4.2 rmind s = splsoftclock();
269 1.3.4.2 rmind curci = curcpu();
270 1.3.4.2 rmind ci = l->l_pcu_cpu[id];
271 1.3.4.2 rmind
272 1.3.4.2 rmind /* Does this CPU already have our PCU state loaded? */
273 1.3.4.2 rmind if (ci == curci) {
274 1.3.4.2 rmind KASSERT(curci->ci_pcu_curlwp[id] == l);
275 1.3.4.2 rmind splx(s);
276 1.3.4.2 rmind return;
277 1.3.4.2 rmind }
278 1.3.4.2 rmind
279 1.3.4.2 rmind /* If PCU state of this LWP is on the remote CPU - save it there. */
280 1.3.4.2 rmind if (ci) {
281 1.3.4.2 rmind splx(s);
282 1.3.4.2 rmind /* Note: there is a race; see description in the top. */
283 1.3.4.2 rmind where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
284 1.3.4.2 rmind __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci);
285 1.3.4.2 rmind xc_wait(where);
286 1.3.4.2 rmind
287 1.3.4.2 rmind /* Enter IPL_SOFTCLOCK and re-fetch the current CPU. */
288 1.3.4.2 rmind s = splsoftclock();
289 1.3.4.2 rmind curci = curcpu();
290 1.3.4.2 rmind }
291 1.3.4.2 rmind KASSERT(l->l_pcu_cpu[id] == NULL);
292 1.3.4.2 rmind
293 1.3.4.2 rmind /* Save the PCU state on the current CPU, if there is any. */
294 1.3.4.2 rmind pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
295 1.3.4.2 rmind KASSERT(curci->ci_pcu_curlwp[id] == NULL);
296 1.3.4.2 rmind
297 1.3.4.2 rmind /*
298 1.3.4.2 rmind * Finally, load the state for this LWP on this CPU. Indicate to
299 1.3.4.2 rmind * load function whether PCU was used before. Note the usage.
300 1.3.4.2 rmind */
301 1.3.4.2 rmind pcu->pcu_state_load(l, ((1 << id) & l->l_pcu_used) != 0);
302 1.3.4.2 rmind curci->ci_pcu_curlwp[id] = l;
303 1.3.4.2 rmind l->l_pcu_cpu[id] = curci;
304 1.3.4.2 rmind l->l_pcu_used |= (1 << id);
305 1.3.4.2 rmind splx(s);
306 1.3.4.2 rmind }
307 1.3.4.2 rmind
308 1.3.4.2 rmind /*
309 1.3.4.2 rmind * pcu_discard: discard the PCU state of current LWP.
310 1.3.4.2 rmind */
311 1.3.4.2 rmind void
312 1.3.4.2 rmind pcu_discard(const pcu_ops_t *pcu)
313 1.3.4.2 rmind {
314 1.3.4.2 rmind const u_int id = pcu->pcu_id;
315 1.3.4.3 rmind lwp_t * const l = curlwp;
316 1.3.4.2 rmind
317 1.3.4.2 rmind KASSERT(!cpu_intr_p() && !cpu_softintr_p());
318 1.3.4.2 rmind
319 1.3.4.2 rmind if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
320 1.3.4.2 rmind return;
321 1.3.4.2 rmind }
322 1.3.4.2 rmind pcu_lwp_op(pcu, l, PCU_RELEASE);
323 1.3.4.2 rmind l->l_pcu_used &= ~(1 << id);
324 1.3.4.2 rmind }
325 1.3.4.2 rmind
326 1.3.4.2 rmind /*
327 1.3.4.2 rmind * pcu_save_lwp: save PCU state to the given LWP.
328 1.3.4.2 rmind */
329 1.3.4.2 rmind void
330 1.3.4.3 rmind pcu_save(const pcu_ops_t *pcu)
331 1.3.4.2 rmind {
332 1.3.4.2 rmind const u_int id = pcu->pcu_id;
333 1.3.4.3 rmind lwp_t * const l = curlwp;
334 1.3.4.2 rmind
335 1.3.4.2 rmind KASSERT(!cpu_intr_p() && !cpu_softintr_p());
336 1.3.4.2 rmind
337 1.3.4.2 rmind if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
338 1.3.4.2 rmind return;
339 1.3.4.2 rmind }
340 1.3.4.2 rmind pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE);
341 1.3.4.2 rmind }
342 1.3.4.2 rmind
343 1.3.4.2 rmind /*
344 1.3.4.2 rmind * pcu_used: return true if PCU was used (pcu_load() case) by the LWP.
345 1.3.4.2 rmind */
346 1.3.4.2 rmind bool
347 1.3.4.3 rmind pcu_used_p(const pcu_ops_t *pcu)
348 1.3.4.2 rmind {
349 1.3.4.2 rmind const u_int id = pcu->pcu_id;
350 1.3.4.3 rmind lwp_t * const l = curlwp;
351 1.3.4.2 rmind
352 1.3.4.2 rmind return l->l_pcu_used & (1 << id);
353 1.3.4.2 rmind }
354 1.3.4.2 rmind
355 1.3.4.2 rmind #endif /* PCU_UNIT_COUNT > 0 */
356