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