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