Remove obsolete keywords: conflicts, controller, disk, tape
[dragonfly.git] / sys / kern / lwkt_ipiq.c
CommitLineData
3b6b7bd1 1/*
8c10bfcf
MD
2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
3b6b7bd1
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
8c10bfcf 10 *
3b6b7bd1
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
8c10bfcf
MD
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3b6b7bd1 32 * SUCH DAMAGE.
8c10bfcf 33 *
866b61fb 34 * $DragonFly: src/sys/kern/lwkt_ipiq.c,v 1.25 2008/05/01 02:11:39 sephe Exp $
3b6b7bd1
MD
35 */
36
37/*
38 * This module implements IPI message queueing and the MI portion of IPI
39 * message processing.
40 */
41
42#ifdef _KERNEL
43
e8f15168
MD
44#include "opt_ddb.h"
45
3b6b7bd1
MD
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/kernel.h>
49#include <sys/proc.h>
50#include <sys/rtprio.h>
51#include <sys/queue.h>
52#include <sys/thread2.h>
53#include <sys/sysctl.h>
ac72c7f4 54#include <sys/ktr.h>
3b6b7bd1
MD
55#include <sys/kthread.h>
56#include <machine/cpu.h>
57#include <sys/lock.h>
58#include <sys/caps.h>
59
60#include <vm/vm.h>
61#include <vm/vm_param.h>
62#include <vm/vm_kern.h>
63#include <vm/vm_object.h>
64#include <vm/vm_page.h>
65#include <vm/vm_map.h>
66#include <vm/vm_pager.h>
67#include <vm/vm_extern.h>
68#include <vm/vm_zone.h>
69
70#include <machine/stdarg.h>
3b6b7bd1
MD
71#include <machine/smp.h>
72#include <machine/atomic.h>
73
3b6b7bd1
MD
74#else
75
76#include <sys/stdint.h>
77#include <libcaps/thread.h>
78#include <sys/thread.h>
79#include <sys/msgport.h>
80#include <sys/errno.h>
81#include <libcaps/globaldata.h>
7e8303ad 82#include <machine/cpufunc.h>
3b6b7bd1
MD
83#include <sys/thread2.h>
84#include <sys/msgport2.h>
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
3b6b7bd1 88#include <machine/lock.h>
5e940450
MD
89#include <machine/cpu.h>
90#include <machine/atomic.h>
3b6b7bd1
MD
91
92#endif
93
94#ifdef SMP
4c9f5a7f
MD
95static __int64_t ipiq_count; /* total calls to lwkt_send_ipiq*() */
96static __int64_t ipiq_fifofull; /* number of fifo full conditions detected */
97static __int64_t ipiq_avoided; /* interlock with target avoids cpu ipi */
98static __int64_t ipiq_passive; /* passive IPI messages */
99static __int64_t ipiq_cscount; /* number of cpu synchronizations */
100static int ipiq_optimized = 1; /* XXX temporary sysctl */
e8f15168
MD
101#ifdef PANIC_DEBUG
102static int panic_ipiq_cpu = -1;
103static int panic_ipiq_count = 100;
104#endif
3b6b7bd1
MD
105#endif
106
107#ifdef _KERNEL
108
109#ifdef SMP
110SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_count, CTLFLAG_RW, &ipiq_count, 0, "");
111SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_fifofull, CTLFLAG_RW, &ipiq_fifofull, 0, "");
4c9f5a7f
MD
112SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_avoided, CTLFLAG_RW, &ipiq_avoided, 0, "");
113SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_passive, CTLFLAG_RW, &ipiq_passive, 0, "");
0f7a3396 114SYSCTL_QUAD(_lwkt, OID_AUTO, ipiq_cscount, CTLFLAG_RW, &ipiq_cscount, 0, "");
4c9f5a7f 115SYSCTL_INT(_lwkt, OID_AUTO, ipiq_optimized, CTLFLAG_RW, &ipiq_optimized, 0, "");
e8f15168
MD
116#ifdef PANIC_DEBUG
117SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_cpu, CTLFLAG_RW, &panic_ipiq_cpu, 0, "");
118SYSCTL_INT(_lwkt, OID_AUTO, panic_ipiq_count, CTLFLAG_RW, &panic_ipiq_count, 0, "");
119#endif
3b6b7bd1 120
a7adb95a 121#define IPIQ_STRING "func=%p arg1=%p arg2=%d scpu=%d dcpu=%d"
5118bbc4 122#define IPIQ_ARG_SIZE (sizeof(void *) * 2 + sizeof(int) * 3)
ac72c7f4
MD
123
124#if !defined(KTR_IPIQ)
125#define KTR_IPIQ KTR_ALL
3b6b7bd1 126#endif
ac72c7f4
MD
127KTR_INFO_MASTER(ipiq);
128KTR_INFO(KTR_IPIQ, ipiq, send_norm, 0, IPIQ_STRING, IPIQ_ARG_SIZE);
129KTR_INFO(KTR_IPIQ, ipiq, send_pasv, 1, IPIQ_STRING, IPIQ_ARG_SIZE);
130KTR_INFO(KTR_IPIQ, ipiq, send_nbio, 2, IPIQ_STRING, IPIQ_ARG_SIZE);
131KTR_INFO(KTR_IPIQ, ipiq, send_fail, 3, IPIQ_STRING, IPIQ_ARG_SIZE);
132KTR_INFO(KTR_IPIQ, ipiq, receive, 4, IPIQ_STRING, IPIQ_ARG_SIZE);
d7ed9e5e
MD
133KTR_INFO(KTR_IPIQ, ipiq, sync_start, 5, "cpumask=%08x", sizeof(cpumask_t));
134KTR_INFO(KTR_IPIQ, ipiq, sync_add, 6, "cpumask=%08x", sizeof(cpumask_t));
866b61fb 135KTR_INFO(KTR_IPIQ, ipiq, cpu_send, 7, IPIQ_STRING, IPIQ_ARG_SIZE);
ac72c7f4 136
a7adb95a
SZ
137#define logipiq(name, func, arg1, arg2, sgd, dgd) \
138 KTR_LOG(ipiq_ ## name, func, arg1, arg2, sgd->gd_cpuid, dgd->gd_cpuid)
d7ed9e5e
MD
139#define logipiq2(name, arg) \
140 KTR_LOG(ipiq_ ## name, arg)
ac72c7f4
MD
141
142#endif /* SMP */
143#endif /* KERNEL */
3b6b7bd1
MD
144
145#ifdef SMP
146
b8a98473
MD
147static int lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
148 struct intrframe *frame);
3b6b7bd1
MD
149static void lwkt_cpusync_remote1(lwkt_cpusync_t poll);
150static void lwkt_cpusync_remote2(lwkt_cpusync_t poll);
151
152/*
153 * Send a function execution request to another cpu. The request is queued
154 * on the cpu<->cpu ipiq matrix. Each cpu owns a unique ipiq FIFO for every
155 * possible target cpu. The FIFO can be written.
156 *
4c9f5a7f
MD
157 * If the FIFO fills up we have to enable interrupts to avoid an APIC
158 * deadlock and process pending IPIQs while waiting for it to empty.
159 * Otherwise we may soft-deadlock with another cpu whos FIFO is also full.
3b6b7bd1
MD
160 *
161 * We can safely bump gd_intr_nesting_level because our crit_exit() at the
162 * end will take care of any pending interrupts.
163 *
4c9f5a7f
MD
164 * The actual hardware IPI is avoided if the target cpu is already processing
165 * the queue from a prior IPI. It is possible to pipeline IPI messages
166 * very quickly between cpus due to the FIFO hysteresis.
167 *
168 * Need not be called from a critical section.
3b6b7bd1
MD
169 */
170int
b8a98473 171lwkt_send_ipiq3(globaldata_t target, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1
MD
172{
173 lwkt_ipiq_t ip;
174 int windex;
175 struct globaldata *gd = mycpu;
176
a7adb95a 177 logipiq(send_norm, func, arg1, arg2, gd, target);
ac72c7f4 178
3b6b7bd1 179 if (target == gd) {
b8a98473 180 func(arg1, arg2, NULL);
3b6b7bd1
MD
181 return(0);
182 }
183 crit_enter();
184 ++gd->gd_intr_nesting_level;
185#ifdef INVARIANTS
186 if (gd->gd_intr_nesting_level > 20)
187 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
188#endif
189 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
190 ++ipiq_count;
191 ip = &gd->gd_ipiq[target->gd_cpuid];
192
193 /*
4c9f5a7f
MD
194 * Do not allow the FIFO to become full. Interrupts must be physically
195 * enabled while we liveloop to avoid deadlocking the APIC.
196 */
197 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
198 unsigned int eflags = read_eflags();
199
866b61fb
SZ
200 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
201 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f 202 cpu_send_ipiq(target->gd_cpuid);
866b61fb 203 }
4c9f5a7f
MD
204 cpu_enable_intr();
205 ++ipiq_fifofull;
206 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
207 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
208 lwkt_process_ipiq();
209 }
210 write_eflags(eflags);
211 }
212
213 /*
214 * Queue the new message
3b6b7bd1 215 */
3b6b7bd1 216 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
217 ip->ip_func[windex] = func;
218 ip->ip_arg1[windex] = arg1;
219 ip->ip_arg2[windex] = arg2;
35238fa5 220 cpu_sfence();
3b6b7bd1 221 ++ip->ip_windex;
4c9f5a7f
MD
222 --gd->gd_intr_nesting_level;
223
224 /*
225 * signal the target cpu that there is work pending.
226 */
227 if (atomic_poll_acquire_int(&ip->ip_npoll)) {
866b61fb 228 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f
MD
229 cpu_send_ipiq(target->gd_cpuid);
230 } else {
866b61fb
SZ
231 if (ipiq_optimized == 0) {
232 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f 233 cpu_send_ipiq(target->gd_cpuid);
866b61fb
SZ
234 } else {
235 ++ipiq_avoided;
236 }
4c9f5a7f
MD
237 }
238 crit_exit();
239 return(ip->ip_windex);
240}
241
242/*
243 * Similar to lwkt_send_ipiq() but this function does not actually initiate
244 * the IPI to the target cpu unless the FIFO has become too full, so it is
245 * very fast.
246 *
247 * This function is used for non-critical IPI messages, such as memory
248 * deallocations. The queue will typically be flushed by the target cpu at
249 * the next clock interrupt.
250 *
251 * Need not be called from a critical section.
252 */
253int
b8a98473
MD
254lwkt_send_ipiq3_passive(globaldata_t target, ipifunc3_t func,
255 void *arg1, int arg2)
4c9f5a7f
MD
256{
257 lwkt_ipiq_t ip;
258 int windex;
259 struct globaldata *gd = mycpu;
260
261 KKASSERT(target != gd);
262 crit_enter();
a7adb95a 263 logipiq(send_pasv, func, arg1, arg2, gd, target);
4c9f5a7f
MD
264 ++gd->gd_intr_nesting_level;
265#ifdef INVARIANTS
266 if (gd->gd_intr_nesting_level > 20)
267 panic("lwkt_send_ipiq: TOO HEAVILY NESTED!");
268#endif
269 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
270 ++ipiq_count;
271 ++ipiq_passive;
272 ip = &gd->gd_ipiq[target->gd_cpuid];
273
274 /*
275 * Do not allow the FIFO to become full. Interrupts must be physically
276 * enabled while we liveloop to avoid deadlocking the APIC.
277 */
3b6b7bd1
MD
278 if (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 2) {
279 unsigned int eflags = read_eflags();
4c9f5a7f 280
866b61fb
SZ
281 if (atomic_poll_acquire_int(&ip->ip_npoll) || ipiq_optimized == 0) {
282 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f 283 cpu_send_ipiq(target->gd_cpuid);
866b61fb 284 }
3b6b7bd1
MD
285 cpu_enable_intr();
286 ++ipiq_fifofull;
287 while (ip->ip_windex - ip->ip_rindex > MAXCPUFIFO / 4) {
288 KKASSERT(ip->ip_windex - ip->ip_rindex != MAXCPUFIFO - 1);
289 lwkt_process_ipiq();
290 }
291 write_eflags(eflags);
292 }
4c9f5a7f
MD
293
294 /*
295 * Queue the new message
296 */
297 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
298 ip->ip_func[windex] = func;
299 ip->ip_arg1[windex] = arg1;
300 ip->ip_arg2[windex] = arg2;
35238fa5 301 cpu_sfence();
4c9f5a7f 302 ++ip->ip_windex;
3b6b7bd1 303 --gd->gd_intr_nesting_level;
4c9f5a7f
MD
304
305 /*
306 * Do not signal the target cpu, it will pick up the IPI when it next
307 * polls (typically on the next tick).
308 */
3b6b7bd1
MD
309 crit_exit();
310 return(ip->ip_windex);
311}
312
41a01a4d 313/*
4c9f5a7f
MD
314 * Send an IPI request without blocking, return 0 on success, ENOENT on
315 * failure. The actual queueing of the hardware IPI may still force us
316 * to spin and process incoming IPIs but that will eventually go away
317 * when we've gotten rid of the other general IPIs.
41a01a4d
MD
318 */
319int
b8a98473
MD
320lwkt_send_ipiq3_nowait(globaldata_t target, ipifunc3_t func,
321 void *arg1, int arg2)
41a01a4d
MD
322{
323 lwkt_ipiq_t ip;
324 int windex;
325 struct globaldata *gd = mycpu;
326
a7adb95a 327 logipiq(send_nbio, func, arg1, arg2, gd, target);
41a01a4d
MD
328 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
329 if (target == gd) {
b8a98473 330 func(arg1, arg2, NULL);
41a01a4d
MD
331 return(0);
332 }
333 ++ipiq_count;
334 ip = &gd->gd_ipiq[target->gd_cpuid];
335
ac72c7f4 336 if (ip->ip_windex - ip->ip_rindex >= MAXCPUFIFO * 2 / 3) {
a7adb95a 337 logipiq(send_fail, func, arg1, arg2, gd, target);
41a01a4d 338 return(ENOENT);
ac72c7f4 339 }
41a01a4d 340 windex = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
341 ip->ip_func[windex] = func;
342 ip->ip_arg1[windex] = arg1;
343 ip->ip_arg2[windex] = arg2;
35238fa5 344 cpu_sfence();
41a01a4d 345 ++ip->ip_windex;
4c9f5a7f 346
41a01a4d 347 /*
4c9f5a7f 348 * This isn't a passive IPI, we still have to signal the target cpu.
41a01a4d 349 */
4c9f5a7f 350 if (atomic_poll_acquire_int(&ip->ip_npoll)) {
866b61fb 351 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f
MD
352 cpu_send_ipiq(target->gd_cpuid);
353 } else {
866b61fb
SZ
354 if (ipiq_optimized == 0) {
355 logipiq(cpu_send, func, arg1, arg2, gd, target);
4c9f5a7f 356 cpu_send_ipiq(target->gd_cpuid);
866b61fb 357 } else {
728f6208 358 ++ipiq_avoided;
866b61fb 359 }
4c9f5a7f 360 }
41a01a4d
MD
361 return(0);
362}
363
3b6b7bd1
MD
364/*
365 * deprecated, used only by fast int forwarding.
366 */
367int
b8a98473 368lwkt_send_ipiq3_bycpu(int dcpu, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1 369{
b8a98473 370 return(lwkt_send_ipiq3(globaldata_find(dcpu), func, arg1, arg2));
3b6b7bd1
MD
371}
372
373/*
374 * Send a message to several target cpus. Typically used for scheduling.
375 * The message will not be sent to stopped cpus.
376 */
377int
b8a98473 378lwkt_send_ipiq3_mask(u_int32_t mask, ipifunc3_t func, void *arg1, int arg2)
3b6b7bd1
MD
379{
380 int cpuid;
381 int count = 0;
382
383 mask &= ~stopped_cpus;
384 while (mask) {
385 cpuid = bsfl(mask);
b8a98473 386 lwkt_send_ipiq3(globaldata_find(cpuid), func, arg1, arg2);
3b6b7bd1
MD
387 mask &= ~(1 << cpuid);
388 ++count;
389 }
390 return(count);
391}
392
393/*
394 * Wait for the remote cpu to finish processing a function.
395 *
396 * YYY we have to enable interrupts and process the IPIQ while waiting
397 * for it to empty or we may deadlock with another cpu. Create a CPU_*()
398 * function to do this! YYY we really should 'block' here.
399 *
400 * MUST be called from a critical section. This routine may be called
401 * from an interrupt (for example, if an interrupt wakes a foreign thread
402 * up).
403 */
404void
405lwkt_wait_ipiq(globaldata_t target, int seq)
406{
407 lwkt_ipiq_t ip;
408 int maxc = 100000000;
409
410 if (target != mycpu) {
411 ip = &mycpu->gd_ipiq[target->gd_cpuid];
412 if ((int)(ip->ip_xindex - seq) < 0) {
413 unsigned int eflags = read_eflags();
414 cpu_enable_intr();
415 while ((int)(ip->ip_xindex - seq) < 0) {
41a01a4d 416 crit_enter();
3b6b7bd1 417 lwkt_process_ipiq();
41a01a4d 418 crit_exit();
3b6b7bd1 419 if (--maxc == 0)
6ea70f76 420 kprintf("LWKT_WAIT_IPIQ WARNING! %d wait %d (%d)\n", mycpu->gd_cpuid, target->gd_cpuid, ip->ip_xindex - seq);
3b6b7bd1
MD
421 if (maxc < -1000000)
422 panic("LWKT_WAIT_IPIQ");
35238fa5
MD
423 /*
424 * xindex may be modified by another cpu, use a load fence
425 * to ensure that the loop does not use a speculative value
426 * (which may improve performance).
427 */
428 cpu_lfence();
3b6b7bd1
MD
429 }
430 write_eflags(eflags);
431 }
432 }
433}
434
41a01a4d
MD
435int
436lwkt_seq_ipiq(globaldata_t target)
437{
438 lwkt_ipiq_t ip;
439
440 ip = &mycpu->gd_ipiq[target->gd_cpuid];
441 return(ip->ip_windex);
442}
443
3b6b7bd1
MD
444/*
445 * Called from IPI interrupt (like a fast interrupt), which has placed
446 * us in a critical section. The MP lock may or may not be held.
447 * May also be called from doreti or splz, or be reentrantly called
448 * indirectly through the ip_func[] we run.
449 *
450 * There are two versions, one where no interrupt frame is available (when
451 * called from the send code and from splz, and one where an interrupt
452 * frame is available.
453 */
454void
455lwkt_process_ipiq(void)
456{
457 globaldata_t gd = mycpu;
ac72c7f4 458 globaldata_t sgd;
3b6b7bd1
MD
459 lwkt_ipiq_t ip;
460 int n;
461
462again:
463 for (n = 0; n < ncpus; ++n) {
464 if (n != gd->gd_cpuid) {
ac72c7f4
MD
465 sgd = globaldata_find(n);
466 ip = sgd->gd_ipiq;
3b6b7bd1 467 if (ip != NULL) {
b8a98473 468 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], NULL))
3b6b7bd1
MD
469 ;
470 }
471 }
472 }
473 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
b8a98473 474 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, NULL)) {
0f7a3396
MD
475 if (gd->gd_curthread->td_cscount == 0)
476 goto again;
477 need_ipiq();
478 }
3b6b7bd1
MD
479 }
480}
481
482#ifdef _KERNEL
483void
c7eb0589 484lwkt_process_ipiq_frame(struct intrframe *frame)
3b6b7bd1
MD
485{
486 globaldata_t gd = mycpu;
ac72c7f4 487 globaldata_t sgd;
3b6b7bd1
MD
488 lwkt_ipiq_t ip;
489 int n;
490
491again:
492 for (n = 0; n < ncpus; ++n) {
493 if (n != gd->gd_cpuid) {
ac72c7f4
MD
494 sgd = globaldata_find(n);
495 ip = sgd->gd_ipiq;
3b6b7bd1 496 if (ip != NULL) {
c7eb0589 497 while (lwkt_process_ipiq_core(sgd, &ip[gd->gd_cpuid], frame))
3b6b7bd1
MD
498 ;
499 }
500 }
501 }
502 if (gd->gd_cpusyncq.ip_rindex != gd->gd_cpusyncq.ip_windex) {
c7eb0589 503 if (lwkt_process_ipiq_core(gd, &gd->gd_cpusyncq, frame)) {
0f7a3396
MD
504 if (gd->gd_curthread->td_cscount == 0)
505 goto again;
506 need_ipiq();
507 }
3b6b7bd1
MD
508 }
509}
510#endif
511
512static int
b8a98473
MD
513lwkt_process_ipiq_core(globaldata_t sgd, lwkt_ipiq_t ip,
514 struct intrframe *frame)
3b6b7bd1
MD
515{
516 int ri;
35238fa5 517 int wi;
b8a98473
MD
518 ipifunc3_t copy_func;
519 void *copy_arg1;
520 int copy_arg2;
35238fa5
MD
521
522 /*
523 * Obtain the current write index, which is modified by a remote cpu.
524 * Issue a load fence to prevent speculative reads of e.g. data written
525 * by the other cpu prior to it updating the index.
526 */
728f6208 527 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
35238fa5
MD
528 wi = ip->ip_windex;
529 cpu_lfence();
530
3b6b7bd1
MD
531 /*
532 * Note: xindex is only updated after we are sure the function has
533 * finished execution. Beware lwkt_process_ipiq() reentrancy! The
534 * function may send an IPI which may block/drain.
d64a7617
MD
535 *
536 * Note: due to additional IPI operations that the callback function
537 * may make, it is possible for both rindex and windex to advance and
538 * thus for rindex to advance passed our cached windex.
3b6b7bd1 539 */
d64a7617 540 while (wi - (ri = ip->ip_rindex) > 0) {
3b6b7bd1 541 ri &= MAXCPUFIFO_MASK;
728f6208 542 copy_func = ip->ip_func[ri];
b8a98473
MD
543 copy_arg1 = ip->ip_arg1[ri];
544 copy_arg2 = ip->ip_arg2[ri];
728f6208
MD
545 cpu_mfence();
546 ++ip->ip_rindex;
547 KKASSERT((ip->ip_rindex & MAXCPUFIFO_MASK) == ((ri + 1) & MAXCPUFIFO_MASK));
a7adb95a 548 logipiq(receive, copy_func, copy_arg1, copy_arg2, sgd, mycpu);
b8a98473 549 copy_func(copy_arg1, copy_arg2, frame);
35238fa5 550 cpu_sfence();
3b6b7bd1 551 ip->ip_xindex = ip->ip_rindex;
e8f15168
MD
552
553#ifdef PANIC_DEBUG
554 /*
555 * Simulate panics during the processing of an IPI
556 */
557 if (mycpu->gd_cpuid == panic_ipiq_cpu && panic_ipiq_count) {
558 if (--panic_ipiq_count == 0) {
559#ifdef DDB
560 Debugger("PANIC_DEBUG");
561#else
562 panic("PANIC_DEBUG");
563#endif
564 }
565 }
566#endif
3b6b7bd1 567 }
4c9f5a7f
MD
568
569 /*
570 * Return non-zero if there are more IPI messages pending on this
571 * ipiq. ip_npoll is left set as long as possible to reduce the
572 * number of IPIs queued by the originating cpu, but must be cleared
573 * *BEFORE* checking windex.
574 */
575 atomic_poll_release_int(&ip->ip_npoll);
3b6b7bd1
MD
576 return(wi != ip->ip_windex);
577}
578
6c92c1f2
SZ
579static void
580lwkt_sync_ipiq(void *arg)
581{
582 cpumask_t *cpumask = arg;
583
584 atomic_clear_int(cpumask, mycpu->gd_cpumask);
585 if (*cpumask == 0)
586 wakeup(cpumask);
587}
588
589void
590lwkt_synchronize_ipiqs(const char *wmesg)
591{
592 cpumask_t other_cpumask;
593
594 other_cpumask = mycpu->gd_other_cpus & smp_active_mask;
595 lwkt_send_ipiq_mask(other_cpumask, lwkt_sync_ipiq, &other_cpumask);
596
597 crit_enter();
598 while (other_cpumask != 0) {
599 tsleep_interlock(&other_cpumask);
600 if (other_cpumask != 0)
601 tsleep(&other_cpumask, 0, wmesg, 0);
602 }
603 crit_exit();
604}
605
0f7a3396
MD
606#endif
607
3b6b7bd1
MD
608/*
609 * CPU Synchronization Support
5c71a36a
MD
610 *
611 * lwkt_cpusync_simple()
612 *
613 * The function is executed synchronously before return on remote cpus.
614 * A lwkt_cpusync_t pointer is passed as an argument. The data can
615 * be accessed via arg->cs_data.
616 *
617 * XXX should I just pass the data as an argument to be consistent?
3b6b7bd1
MD
618 */
619
620void
5c71a36a
MD
621lwkt_cpusync_simple(cpumask_t mask, cpusync_func_t func, void *data)
622{
623 struct lwkt_cpusync cmd;
624
625 cmd.cs_run_func = NULL;
626 cmd.cs_fin1_func = func;
627 cmd.cs_fin2_func = NULL;
628 cmd.cs_data = data;
629 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
630 if (mask & (1 << mycpu->gd_cpuid))
631 func(&cmd);
632 lwkt_cpusync_finish(&cmd);
633}
634
635/*
636 * lwkt_cpusync_fastdata()
637 *
638 * The function is executed in tandem with return on remote cpus.
639 * The data is directly passed as an argument. Do not pass pointers to
640 * temporary storage as the storage might have
641 * gone poof by the time the target cpu executes
642 * the function.
643 *
644 * At the moment lwkt_cpusync is declared on the stack and we must wait
645 * for all remote cpus to ack in lwkt_cpusync_finish(), but as a future
646 * optimization we should be able to put a counter in the globaldata
647 * structure (if it is not otherwise being used) and just poke it and
648 * return without waiting. XXX
649 */
650void
651lwkt_cpusync_fastdata(cpumask_t mask, cpusync_func2_t func, void *data)
3b6b7bd1
MD
652{
653 struct lwkt_cpusync cmd;
3b6b7bd1
MD
654
655 cmd.cs_run_func = NULL;
656 cmd.cs_fin1_func = NULL;
657 cmd.cs_fin2_func = func;
5c71a36a
MD
658 cmd.cs_data = NULL;
659 lwkt_cpusync_start(mask & mycpu->gd_other_cpus, &cmd);
3b6b7bd1
MD
660 if (mask & (1 << mycpu->gd_cpuid))
661 func(data);
5c71a36a 662 lwkt_cpusync_finish(&cmd);
3b6b7bd1
MD
663}
664
665/*
5c71a36a
MD
666 * lwkt_cpusync_start()
667 *
668 * Start synchronization with a set of target cpus, return once they are
669 * known to be in a synchronization loop. The target cpus will execute
670 * poll->cs_run_func() IN TANDEM WITH THE RETURN.
671 *
672 * XXX future: add lwkt_cpusync_start_quick() and require a call to
673 * lwkt_cpusync_add() or lwkt_cpusync_wait(), allowing the caller to
674 * potentially absorb the IPI latency doing something useful.
3b6b7bd1 675 */
5c71a36a 676void
3b6b7bd1
MD
677lwkt_cpusync_start(cpumask_t mask, lwkt_cpusync_t poll)
678{
0f7a3396
MD
679 globaldata_t gd = mycpu;
680
3b6b7bd1 681 poll->cs_count = 0;
5c71a36a 682 poll->cs_mask = mask;
0f7a3396 683#ifdef SMP
d7ed9e5e 684 logipiq2(sync_start, mask & gd->gd_other_cpus);
0f7a3396
MD
685 poll->cs_maxcount = lwkt_send_ipiq_mask(
686 mask & gd->gd_other_cpus & smp_active_mask,
b8a98473 687 (ipifunc1_t)lwkt_cpusync_remote1, poll);
0f7a3396 688#endif
fda1ad89 689 if (mask & gd->gd_cpumask) {
5c71a36a
MD
690 if (poll->cs_run_func)
691 poll->cs_run_func(poll);
692 }
0f7a3396
MD
693#ifdef SMP
694 if (poll->cs_maxcount) {
695 ++ipiq_cscount;
696 ++gd->gd_curthread->td_cscount;
697 while (poll->cs_count != poll->cs_maxcount) {
698 crit_enter();
699 lwkt_process_ipiq();
700 crit_exit();
701 }
5c71a36a 702 }
0f7a3396 703#endif
5c71a36a
MD
704}
705
706void
707lwkt_cpusync_add(cpumask_t mask, lwkt_cpusync_t poll)
708{
0f7a3396 709 globaldata_t gd = mycpu;
41a01a4d 710#ifdef SMP
0f7a3396 711 int count;
41a01a4d 712#endif
0f7a3396 713
5c71a36a
MD
714 mask &= ~poll->cs_mask;
715 poll->cs_mask |= mask;
0f7a3396 716#ifdef SMP
d7ed9e5e 717 logipiq2(sync_add, mask & gd->gd_other_cpus);
0f7a3396
MD
718 count = lwkt_send_ipiq_mask(
719 mask & gd->gd_other_cpus & smp_active_mask,
b8a98473 720 (ipifunc1_t)lwkt_cpusync_remote1, poll);
0f7a3396 721#endif
fda1ad89 722 if (mask & gd->gd_cpumask) {
5c71a36a
MD
723 if (poll->cs_run_func)
724 poll->cs_run_func(poll);
725 }
0f7a3396
MD
726#ifdef SMP
727 poll->cs_maxcount += count;
728 if (poll->cs_maxcount) {
729 if (poll->cs_maxcount == count)
730 ++gd->gd_curthread->td_cscount;
731 while (poll->cs_count != poll->cs_maxcount) {
732 crit_enter();
733 lwkt_process_ipiq();
734 crit_exit();
735 }
3b6b7bd1 736 }
0f7a3396 737#endif
3b6b7bd1
MD
738}
739
740/*
741 * Finish synchronization with a set of target cpus. The target cpus will
742 * execute cs_fin1_func(poll) prior to this function returning, and will
743 * execute cs_fin2_func(data) IN TANDEM WITH THIS FUNCTION'S RETURN.
0f7a3396
MD
744 *
745 * If cs_maxcount is non-zero then we are mastering a cpusync with one or
746 * more remote cpus and must account for it in our thread structure.
3b6b7bd1
MD
747 */
748void
5c71a36a 749lwkt_cpusync_finish(lwkt_cpusync_t poll)
3b6b7bd1 750{
0f7a3396 751 globaldata_t gd = mycpu;
5c71a36a 752
3b6b7bd1 753 poll->cs_count = -1;
fda1ad89 754 if (poll->cs_mask & gd->gd_cpumask) {
5c71a36a
MD
755 if (poll->cs_fin1_func)
756 poll->cs_fin1_func(poll);
757 if (poll->cs_fin2_func)
758 poll->cs_fin2_func(poll->cs_data);
759 }
0f7a3396
MD
760#ifdef SMP
761 if (poll->cs_maxcount) {
762 while (poll->cs_count != -(poll->cs_maxcount + 1)) {
763 crit_enter();
764 lwkt_process_ipiq();
765 crit_exit();
766 }
767 --gd->gd_curthread->td_cscount;
3b6b7bd1 768 }
0f7a3396 769#endif
3b6b7bd1
MD
770}
771
0f7a3396
MD
772#ifdef SMP
773
3b6b7bd1
MD
774/*
775 * helper IPI remote messaging function.
776 *
777 * Called on remote cpu when a new cpu synchronization request has been
778 * sent to us. Execute the run function and adjust cs_count, then requeue
779 * the request so we spin on it.
780 */
781static void
782lwkt_cpusync_remote1(lwkt_cpusync_t poll)
783{
784 atomic_add_int(&poll->cs_count, 1);
785 if (poll->cs_run_func)
786 poll->cs_run_func(poll);
787 lwkt_cpusync_remote2(poll);
788}
789
790/*
791 * helper IPI remote messaging function.
792 *
793 * Poll for the originator telling us to finish. If it hasn't, requeue
794 * our request so we spin on it. When the originator requests that we
795 * finish we execute cs_fin1_func(poll) synchronously and cs_fin2_func(data)
796 * in tandem with the release.
797 */
798static void
799lwkt_cpusync_remote2(lwkt_cpusync_t poll)
800{
801 if (poll->cs_count < 0) {
802 cpusync_func2_t savef;
803 void *saved;
804
805 if (poll->cs_fin1_func)
806 poll->cs_fin1_func(poll);
807 if (poll->cs_fin2_func) {
808 savef = poll->cs_fin2_func;
809 saved = poll->cs_data;
810 atomic_add_int(&poll->cs_count, -1);
811 savef(saved);
812 } else {
813 atomic_add_int(&poll->cs_count, -1);
814 }
815 } else {
816 globaldata_t gd = mycpu;
817 lwkt_ipiq_t ip;
818 int wi;
819
820 ip = &gd->gd_cpusyncq;
821 wi = ip->ip_windex & MAXCPUFIFO_MASK;
b8a98473
MD
822 ip->ip_func[wi] = (ipifunc3_t)(ipifunc1_t)lwkt_cpusync_remote2;
823 ip->ip_arg1[wi] = poll;
824 ip->ip_arg2[wi] = 0;
35238fa5 825 cpu_sfence();
3b6b7bd1
MD
826 ++ip->ip_windex;
827 }
828}
829
3b6b7bd1 830#endif