1 /* $NetBSD: subr_pcu.c,v 1.6 2011/05/02 06:33:16 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.6 2011/05/02 06:33:16 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) or 129 * by pcu_load. 130 */ 131 static void 132 pcu_cpu_op(const pcu_ops_t *pcu, const int flags) 133 { 134 const u_int id = pcu->pcu_id; 135 lwp_t * const l = curcpu()->ci_pcu_curlwp[id]; 136 137 //KASSERT(cpu_softintr_p()); 138 139 /* If no state - nothing to do. */ 140 if (l == NULL) { 141 return; 142 } 143 pcu_do_op(pcu, l, flags); 144 } 145 146 /* 147 * pcu_lwp_op: perform PCU state save, release or both operations on LWP. 148 */ 149 static void 150 pcu_lwp_op(const pcu_ops_t *pcu, lwp_t *l, int flags) 151 { 152 const u_int id = pcu->pcu_id; 153 struct cpu_info *ci; 154 uint64_t where; 155 int s; 156 157 /* 158 * Caller should have re-checked if there is any state to manage. 159 * Block the interrupts and inspect again, since cross-call sent 160 * by remote CPU could have changed the state. 161 */ 162 s = splsoftclock(); 163 ci = l->l_pcu_cpu[id]; 164 if (ci == curcpu()) { 165 /* 166 * State is on the current CPU - just perform the operations. 167 */ 168 KASSERTMSG(ci->ci_pcu_curlwp[id] == l, 169 ("%s: cpu%u: pcu_curlwp[%u] (%p) != l (%p)", 170 __func__, cpu_index(ci), id, ci->ci_pcu_curlwp[id], l)); 171 pcu_do_op(pcu, l, flags); 172 splx(s); 173 return; 174 } 175 splx(s); 176 177 if (__predict_false(ci == NULL)) { 178 /* Cross-call has won the race - no state to manage. */ 179 return; 180 } 181 182 /* 183 * State is on the remote CPU - perform the operations there. 184 * Note: there is a race condition; see description in the top. 185 */ 186 where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op, 187 __UNCONST(pcu), (void *)(uintptr_t)flags, ci); 188 xc_wait(where); 189 190 KASSERT((flags & PCU_RELEASE) == 0 || l->l_pcu_cpu[id] == NULL); 191 } 192 193 /* 194 * pcu_load: load/initialize the PCU state of current LWP on current CPU. 195 */ 196 void 197 pcu_load(const pcu_ops_t *pcu) 198 { 199 const u_int id = pcu->pcu_id; 200 struct cpu_info *ci, *curci; 201 lwp_t * const l = curlwp; 202 uint64_t where; 203 int s; 204 205 KASSERT(!cpu_intr_p() && !cpu_softintr_p()); 206 207 s = splsoftclock(); 208 curci = curcpu(); 209 ci = l->l_pcu_cpu[id]; 210 211 /* Does this CPU already have our PCU state loaded? */ 212 if (ci == curci) { 213 KASSERT(curci->ci_pcu_curlwp[id] == l); 214 splx(s); 215 return; 216 } 217 218 /* If PCU state of this LWP is on the remote CPU - save it there. */ 219 if (ci) { 220 splx(s); 221 /* Note: there is a race; see description in the top. */ 222 where = xc_unicast(XC_HIGHPRI, (xcfunc_t)pcu_cpu_op, 223 __UNCONST(pcu), (void *)(PCU_SAVE | PCU_RELEASE), ci); 224 xc_wait(where); 225 226 /* Enter IPL_SOFTCLOCK and re-fetch the current CPU. */ 227 s = splsoftclock(); 228 curci = curcpu(); 229 } 230 KASSERT(l->l_pcu_cpu[id] == NULL); 231 232 /* Save the PCU state on the current CPU, if there is any. */ 233 pcu_cpu_op(pcu, PCU_SAVE | PCU_RELEASE); 234 KASSERT(curci->ci_pcu_curlwp[id] == NULL); 235 236 /* 237 * Finally, load the state for this LWP on this CPU. Indicate to 238 * load function whether PCU was used before. Note the usage. 239 */ 240 pcu->pcu_state_load(l, ((1 << id) & l->l_pcu_used) != 0); 241 curci->ci_pcu_curlwp[id] = l; 242 l->l_pcu_cpu[id] = curci; 243 l->l_pcu_used |= (1 << id); 244 splx(s); 245 } 246 247 /* 248 * pcu_discard: discard the PCU state of current LWP. 249 */ 250 void 251 pcu_discard(const pcu_ops_t *pcu) 252 { 253 const u_int id = pcu->pcu_id; 254 lwp_t * const l = curlwp; 255 256 KASSERT(!cpu_intr_p() && !cpu_softintr_p()); 257 258 if (__predict_true(l->l_pcu_cpu[id] == NULL)) { 259 return; 260 } 261 pcu_lwp_op(pcu, l, PCU_RELEASE); 262 l->l_pcu_used &= ~(1 << id); 263 } 264 265 /* 266 * pcu_save_lwp: save PCU state to the given LWP. 267 */ 268 void 269 pcu_save(const pcu_ops_t *pcu) 270 { 271 const u_int id = pcu->pcu_id; 272 lwp_t * const l = curlwp; 273 274 KASSERT(!cpu_intr_p() && !cpu_softintr_p()); 275 276 if (__predict_true(l->l_pcu_cpu[id] == NULL)) { 277 return; 278 } 279 pcu_lwp_op(pcu, l, PCU_SAVE | PCU_RELEASE); 280 } 281 282 /* 283 * pcu_used: return true if PCU was used (pcu_load() case) by the LWP. 284 */ 285 bool 286 pcu_used_p(const pcu_ops_t *pcu) 287 { 288 const u_int id = pcu->pcu_id; 289 lwp_t * const l = curlwp; 290 291 return l->l_pcu_used & (1 << id); 292 } 293 294 #endif /* PCU_UNIT_COUNT > 0 */ 295