xref: /netbsd-src/sys/kern/subr_pcu.c (revision daf6c4152fcddc27c445489775ed1f66ab4ea9a9)
1 /*	$NetBSD: subr_pcu.c,v 1.1 2011/02/17 18:32:29 rmind 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.1 2011/02/17 18:32:29 rmind 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 #define	PCU_SAVE		0x01	/* Save PCU state to the LWP. */
69 #define	PCU_RELEASE		0x02	/* Release PCU state on the CPU. */
70 
71 #if 0
72 /*
73  * pcu_init_lwp: initialize PCU structures for LWP.
74  */
75 void
76 pcu_init_lwp(lwp_t *l)
77 {
78 
79 	memset(l->l_pcu_cpu, 0, sizeof(uint32_t) * PCU_UNIT_COUNT);
80 	l->l_pcu_used = 0;
81 }
82 #endif
83 
84 /*
85  * pcu_cpu_op: save/release PCU state on the current CPU.
86  *
87  * => Must be called at IPL_SOFTCLOCK or from the soft-interrupt.
88  */
89 static void
90 pcu_cpu_op(const pcu_ops_t *pcu, const int flags)
91 {
92 	const u_int id = pcu->pcu_id;
93 	struct cpu_info *ci = curcpu();
94 	lwp_t *l = ci->ci_pcu_curlwp[id];
95 
96 	/* If no state - nothing to do. */
97 	if (l == NULL) {
98 		return;
99 	}
100 	if (flags & PCU_SAVE) {
101 		pcu->pcu_state_save(l);
102 	}
103 	if (flags & PCU_RELEASE) {
104 		ci->ci_pcu_curlwp[id] = NULL;
105 		l->l_pcu_cpu[id] = NULL;
106 	}
107 }
108 
109 /*
110  * pcu_lwp_op: perform PCU state save, release or both operations on LWP.
111  */
112 static void
113 pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, int flags)
114 {
115 	const u_int id = pcu->pcu_id;
116 	struct cpu_info *ci;
117 	uint64_t where;
118 	int s;
119 
120 	/*
121 	 * Caller should have re-checked if there is any state to manage.
122 	 * Block the interrupts and inspect again, since cross-call sent
123 	 * by remote CPU could have changed the state.
124 	 */
125 	s = splsoftclock();
126 	ci = l->l_pcu_cpu[id];
127 	if (ci == curcpu()) {
128 		/*
129 		 * State is on the current CPU - just perform the operations.
130 		 */
131 		KASSERT(ci->ci_pcu_curlwp[id] == l);
132 
133 		if (flags & PCU_SAVE) {
134 			pcu->pcu_state_save(l);
135 		}
136 		if (flags & PCU_RELEASE) {
137 			ci->ci_pcu_curlwp[id] = NULL;
138 			l->l_pcu_cpu[id] = NULL;
139 		}
140 		splx(s);
141 		return;
142 	}
143 	splx(s);
144 
145 	if (__predict_false(ci == NULL)) {
146 		/* Cross-call has won the race - no state to manage. */
147 		return;
148 	}
149 
150 	/*
151 	 * State is on the remote CPU - perform the operations there.
152 	 * Note: there is a race condition; see description in the top.
153 	 */
154 	where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
155 	    __UNCONST(pcu), (void *)(uintptr_t)flags, ci);
156 	xc_wait(where);
157 
158 	KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL);
159 }
160 
161 /*
162  * pcu_load: load/initialize the PCU state of current LWP on current CPU.
163  */
164 void
165 pcu_load(const pcu_ops_t *pcu)
166 {
167 	const u_int id = pcu->pcu_id;
168 	struct cpu_info *ci, *curci;
169 	lwp_t *l = curlwp;
170 	uint64_t where;
171 	int s;
172 
173 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
174 
175 	s = splsoftclock();
176 	curci = curcpu();
177 	ci = l->l_pcu_cpu[id];
178 
179 	/* Does this CPU already have our PCU state loaded? */
180 	if (ci == curci) {
181 		KASSERT(curci->ci_pcu_curlwp[id] == l);
182 		splx(s);
183 		return;
184 	}
185 
186 	/* If PCU state of this LWP is on the remote CPU - save it there. */
187 	if (ci) {
188 		splx(s);
189 		/* Note: there is a race; see description in the top. */
190 		where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op,
191 		    __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci);
192 		xc_wait(where);
193 
194 		/* Enter IPL_SOFTCLOCK and re-fetch the current CPU. */
195 		s = splsoftclock();
196 		curci = curcpu();
197 	}
198 	KASSERT(l->l_pcu_cpu[id] == NULL);
199 
200 	/* Save the PCU state on the current CPU, if there is any. */
201 	pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE);
202 	KASSERT(curci->ci_pcu_curlwp[id] == NULL);
203 
204 	/*
205 	 * Finally, load the state for this LWP on this CPU.  Indicate to
206 	 * load function whether PCU was used before.  Note the usage.
207 	 */
208 	pcu->pcu_state_load(l, ((1 << id) & l->l_pcu_used) != 0);
209 	curci->ci_pcu_curlwp[id] = l;
210 	l->l_pcu_cpu[id] = curci;
211 	l->l_pcu_used |= (1 << id);
212 	splx(s);
213 }
214 
215 /*
216  * pcu_discard: discard the PCU state of current LWP.
217  */
218 void
219 pcu_discard(const pcu_ops_t *pcu)
220 {
221 	const u_int id = pcu->pcu_id;
222 	lwp_t *l = curlwp;
223 
224 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
225 
226 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
227 		return;
228 	}
229 	pcu_lwp_op(pcu, l, PCU_RELEASE);
230 	l->l_pcu_used &= ~(1 << id);
231 }
232 
233 /*
234  * pcu_save_lwp: save PCU state to the given LWP.
235  */
236 void
237 pcu_save_lwp(const pcu_ops_t *pcu, lwp_t *l)
238 {
239 	const u_int id = pcu->pcu_id;
240 
241 	KASSERT(!cpu_intr_p() && !cpu_softintr_p());
242 
243 	if (__predict_true(l->l_pcu_cpu[id] == NULL)) {
244 		return;
245 	}
246 	pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE);
247 }
248 
249 /*
250  * pcu_used: return true if PCU was used (pcu_load() case) by the LWP.
251  */
252 bool
253 pcu_used(const pcu_ops_t *pcu, lwp_t *l)
254 {
255 	const u_int id = pcu->pcu_id;
256 
257 	return l->l_pcu_used & (1 << id);
258 }
259