Home | History | Annotate | Line # | Download | only in kern
subr_ipi.c revision 1.9
      1  1.9  riastrad /*	$NetBSD: subr_ipi.c,v 1.9 2020/11/27 20:11:33 riastradh Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.1     rmind  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1     rmind  * All rights reserved.
      6  1.1     rmind  *
      7  1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     rmind  * by Mindaugas Rasiukevicius.
      9  1.1     rmind  *
     10  1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1     rmind  * modification, are permitted provided that the following conditions
     12  1.1     rmind  * are met:
     13  1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1     rmind  *
     19  1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     rmind  */
     31  1.1     rmind 
     32  1.1     rmind /*
     33  1.2     rmind  * Inter-processor interrupt (IPI) interface: asynchronous IPIs to
     34  1.2     rmind  * invoke functions with a constant argument and synchronous IPIs
     35  1.2     rmind  * with the cross-call support.
     36  1.1     rmind  */
     37  1.1     rmind 
     38  1.1     rmind #include <sys/cdefs.h>
     39  1.9  riastrad __KERNEL_RCSID(0, "$NetBSD: subr_ipi.c,v 1.9 2020/11/27 20:11:33 riastradh Exp $");
     40  1.1     rmind 
     41  1.1     rmind #include <sys/param.h>
     42  1.1     rmind #include <sys/types.h>
     43  1.1     rmind 
     44  1.1     rmind #include <sys/atomic.h>
     45  1.1     rmind #include <sys/evcnt.h>
     46  1.1     rmind #include <sys/cpu.h>
     47  1.1     rmind #include <sys/ipi.h>
     48  1.3     rmind #include <sys/intr.h>
     49  1.1     rmind #include <sys/kcpuset.h>
     50  1.1     rmind #include <sys/kmem.h>
     51  1.1     rmind #include <sys/lock.h>
     52  1.2     rmind #include <sys/mutex.h>
     53  1.2     rmind 
     54  1.2     rmind /*
     55  1.2     rmind  * An array of the IPI handlers used for asynchronous invocation.
     56  1.2     rmind  * The lock protects the slot allocation.
     57  1.2     rmind  */
     58  1.2     rmind 
     59  1.2     rmind typedef struct {
     60  1.2     rmind 	ipi_func_t	func;
     61  1.2     rmind 	void *		arg;
     62  1.2     rmind } ipi_intr_t;
     63  1.2     rmind 
     64  1.2     rmind static kmutex_t		ipi_mngmt_lock;
     65  1.2     rmind static ipi_intr_t	ipi_intrs[IPI_MAXREG]	__cacheline_aligned;
     66  1.1     rmind 
     67  1.1     rmind /*
     68  1.1     rmind  * Per-CPU mailbox for IPI messages: it is a single cache line storing
     69  1.2     rmind  * up to IPI_MSG_MAX messages.  This interface is built on top of the
     70  1.2     rmind  * synchronous IPIs.
     71  1.1     rmind  */
     72  1.1     rmind 
     73  1.1     rmind #define	IPI_MSG_SLOTS	(CACHE_LINE_SIZE / sizeof(ipi_msg_t *))
     74  1.1     rmind #define	IPI_MSG_MAX	IPI_MSG_SLOTS
     75  1.1     rmind 
     76  1.1     rmind typedef struct {
     77  1.1     rmind 	ipi_msg_t *	msg[IPI_MSG_SLOTS];
     78  1.1     rmind } ipi_mbox_t;
     79  1.1     rmind 
     80  1.2     rmind 
     81  1.2     rmind /* Mailboxes for the synchronous IPIs. */
     82  1.1     rmind static ipi_mbox_t *	ipi_mboxes	__read_mostly;
     83  1.1     rmind static struct evcnt	ipi_mboxfull_ev	__cacheline_aligned;
     84  1.2     rmind static void		ipi_msg_cpu_handler(void *);
     85  1.2     rmind 
     86  1.2     rmind /* Handler for the synchronous IPIs - it must be zero. */
     87  1.2     rmind #define	IPI_SYNCH_ID	0
     88  1.1     rmind 
     89  1.1     rmind #ifndef MULTIPROCESSOR
     90  1.1     rmind #define	cpu_ipi(ci)	KASSERT(ci == NULL)
     91  1.1     rmind #endif
     92  1.1     rmind 
     93  1.1     rmind void
     94  1.1     rmind ipi_sysinit(void)
     95  1.1     rmind {
     96  1.1     rmind 
     97  1.2     rmind 	mutex_init(&ipi_mngmt_lock, MUTEX_DEFAULT, IPL_NONE);
     98  1.2     rmind 	memset(ipi_intrs, 0, sizeof(ipi_intrs));
     99  1.2     rmind 
    100  1.2     rmind 	/*
    101  1.2     rmind 	 * Register the handler for synchronous IPIs.  This mechanism
    102  1.2     rmind 	 * is built on top of the asynchronous interface.  Slot zero is
    103  1.2     rmind 	 * reserved permanently; it is also handy to use zero as a failure
    104  1.2     rmind 	 * for other registers (as it is potentially less error-prone).
    105  1.2     rmind 	 */
    106  1.2     rmind 	ipi_intrs[IPI_SYNCH_ID].func = ipi_msg_cpu_handler;
    107  1.2     rmind 
    108  1.1     rmind 	evcnt_attach_dynamic(&ipi_mboxfull_ev, EVCNT_TYPE_MISC, NULL,
    109  1.1     rmind 	   "ipi", "full");
    110  1.1     rmind }
    111  1.1     rmind 
    112  1.8  riastrad void
    113  1.8  riastrad ipi_percpu_init(void)
    114  1.8  riastrad {
    115  1.8  riastrad 	const size_t len = ncpu * sizeof(ipi_mbox_t);
    116  1.8  riastrad 
    117  1.8  riastrad 	/* Initialise the per-CPU bit fields. */
    118  1.8  riastrad 	for (u_int i = 0; i < ncpu; i++) {
    119  1.8  riastrad 		struct cpu_info *ci = cpu_lookup(i);
    120  1.8  riastrad 		memset(&ci->ci_ipipend, 0, sizeof(ci->ci_ipipend));
    121  1.8  riastrad 	}
    122  1.8  riastrad 
    123  1.8  riastrad 	/* Allocate per-CPU IPI mailboxes. */
    124  1.8  riastrad 	ipi_mboxes = kmem_zalloc(len, KM_SLEEP);
    125  1.8  riastrad 	KASSERT(ipi_mboxes != NULL);
    126  1.8  riastrad }
    127  1.8  riastrad 
    128  1.1     rmind /*
    129  1.2     rmind  * ipi_register: register an asynchronous IPI handler.
    130  1.2     rmind  *
    131  1.2     rmind  * => Returns IPI ID which is greater than zero; on failure - zero.
    132  1.2     rmind  */
    133  1.2     rmind u_int
    134  1.2     rmind ipi_register(ipi_func_t func, void *arg)
    135  1.2     rmind {
    136  1.2     rmind 	mutex_enter(&ipi_mngmt_lock);
    137  1.2     rmind 	for (u_int i = 0; i < IPI_MAXREG; i++) {
    138  1.2     rmind 		if (ipi_intrs[i].func == NULL) {
    139  1.2     rmind 			/* Register the function. */
    140  1.2     rmind 			ipi_intrs[i].func = func;
    141  1.2     rmind 			ipi_intrs[i].arg = arg;
    142  1.2     rmind 			mutex_exit(&ipi_mngmt_lock);
    143  1.2     rmind 
    144  1.2     rmind 			KASSERT(i != IPI_SYNCH_ID);
    145  1.2     rmind 			return i;
    146  1.2     rmind 		}
    147  1.2     rmind 	}
    148  1.2     rmind 	mutex_exit(&ipi_mngmt_lock);
    149  1.2     rmind 	printf("WARNING: ipi_register: table full, increase IPI_MAXREG\n");
    150  1.2     rmind 	return 0;
    151  1.2     rmind }
    152  1.2     rmind 
    153  1.2     rmind /*
    154  1.2     rmind  * ipi_unregister: release the IPI handler given the ID.
    155  1.2     rmind  */
    156  1.2     rmind void
    157  1.2     rmind ipi_unregister(u_int ipi_id)
    158  1.2     rmind {
    159  1.7  christos 	ipi_msg_t ipimsg = { .func = __FPTRCAST(ipi_func_t, nullop) };
    160  1.2     rmind 
    161  1.2     rmind 	KASSERT(ipi_id != IPI_SYNCH_ID);
    162  1.2     rmind 	KASSERT(ipi_id < IPI_MAXREG);
    163  1.2     rmind 
    164  1.2     rmind 	/* Release the slot. */
    165  1.2     rmind 	mutex_enter(&ipi_mngmt_lock);
    166  1.2     rmind 	KASSERT(ipi_intrs[ipi_id].func != NULL);
    167  1.2     rmind 	ipi_intrs[ipi_id].func = NULL;
    168  1.2     rmind 
    169  1.2     rmind 	/* Ensure that there are no IPIs in flight. */
    170  1.2     rmind 	kpreempt_disable();
    171  1.4   thorpej 	ipi_broadcast(&ipimsg, false);
    172  1.2     rmind 	ipi_wait(&ipimsg);
    173  1.2     rmind 	kpreempt_enable();
    174  1.2     rmind 	mutex_exit(&ipi_mngmt_lock);
    175  1.2     rmind }
    176  1.2     rmind 
    177  1.2     rmind /*
    178  1.4   thorpej  * ipi_mark_pending: internal routine to mark an IPI pending on the
    179  1.4   thorpej  * specified CPU (which might be curcpu()).
    180  1.2     rmind  */
    181  1.4   thorpej static bool
    182  1.4   thorpej ipi_mark_pending(u_int ipi_id, struct cpu_info *ci)
    183  1.2     rmind {
    184  1.2     rmind 	const u_int i = ipi_id >> IPI_BITW_SHIFT;
    185  1.2     rmind 	const uint32_t bitm = 1U << (ipi_id & IPI_BITW_MASK);
    186  1.2     rmind 
    187  1.2     rmind 	KASSERT(ipi_id < IPI_MAXREG);
    188  1.2     rmind 	KASSERT(kpreempt_disabled());
    189  1.2     rmind 
    190  1.9  riastrad 	/* Mark as pending and return true if not previously marked. */
    191  1.9  riastrad 	if ((atomic_load_acquire(&ci->ci_ipipend[i]) & bitm) == 0) {
    192  1.9  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    193  1.9  riastrad 		membar_exit();
    194  1.9  riastrad #endif
    195  1.2     rmind 		atomic_or_32(&ci->ci_ipipend[i], bitm);
    196  1.4   thorpej 		return true;
    197  1.4   thorpej 	}
    198  1.4   thorpej 	return false;
    199  1.4   thorpej }
    200  1.4   thorpej 
    201  1.4   thorpej /*
    202  1.4   thorpej  * ipi_trigger: asynchronously send an IPI to the specified CPU.
    203  1.4   thorpej  */
    204  1.4   thorpej void
    205  1.4   thorpej ipi_trigger(u_int ipi_id, struct cpu_info *ci)
    206  1.4   thorpej {
    207  1.4   thorpej 
    208  1.4   thorpej 	KASSERT(curcpu() != ci);
    209  1.4   thorpej 	if (ipi_mark_pending(ipi_id, ci)) {
    210  1.2     rmind 		cpu_ipi(ci);
    211  1.2     rmind 	}
    212  1.2     rmind }
    213  1.2     rmind 
    214  1.2     rmind /*
    215  1.4   thorpej  * ipi_trigger_multi_internal: the guts of ipi_trigger_multi() and
    216  1.4   thorpej  * ipi_trigger_broadcast().
    217  1.3     rmind  */
    218  1.4   thorpej static void
    219  1.4   thorpej ipi_trigger_multi_internal(u_int ipi_id, const kcpuset_t *target,
    220  1.4   thorpej     bool skip_self)
    221  1.3     rmind {
    222  1.3     rmind 	const cpuid_t selfid = cpu_index(curcpu());
    223  1.3     rmind 	CPU_INFO_ITERATOR cii;
    224  1.3     rmind 	struct cpu_info *ci;
    225  1.3     rmind 
    226  1.3     rmind 	KASSERT(kpreempt_disabled());
    227  1.3     rmind 	KASSERT(target != NULL);
    228  1.3     rmind 
    229  1.3     rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    230  1.3     rmind 		const cpuid_t cpuid = cpu_index(ci);
    231  1.3     rmind 
    232  1.3     rmind 		if (!kcpuset_isset(target, cpuid) || cpuid == selfid) {
    233  1.3     rmind 			continue;
    234  1.3     rmind 		}
    235  1.3     rmind 		ipi_trigger(ipi_id, ci);
    236  1.3     rmind 	}
    237  1.4   thorpej 	if (!skip_self && kcpuset_isset(target, selfid)) {
    238  1.4   thorpej 		ipi_mark_pending(ipi_id, curcpu());
    239  1.3     rmind 		int s = splhigh();
    240  1.3     rmind 		ipi_cpu_handler();
    241  1.3     rmind 		splx(s);
    242  1.3     rmind 	}
    243  1.3     rmind }
    244  1.3     rmind 
    245  1.3     rmind /*
    246  1.4   thorpej  * ipi_trigger_multi: same as ipi_trigger() but sends to the multiple
    247  1.4   thorpej  * CPUs given the target CPU set.
    248  1.4   thorpej  */
    249  1.4   thorpej void
    250  1.4   thorpej ipi_trigger_multi(u_int ipi_id, const kcpuset_t *target)
    251  1.4   thorpej {
    252  1.4   thorpej 	ipi_trigger_multi_internal(ipi_id, target, false);
    253  1.4   thorpej }
    254  1.4   thorpej 
    255  1.4   thorpej /*
    256  1.4   thorpej  * ipi_trigger_broadcast: same as ipi_trigger_multi() to kcpuset_attached,
    257  1.4   thorpej  * optionally skipping the sending CPU.
    258  1.4   thorpej  */
    259  1.4   thorpej void
    260  1.4   thorpej ipi_trigger_broadcast(u_int ipi_id, bool skip_self)
    261  1.4   thorpej {
    262  1.4   thorpej 	ipi_trigger_multi_internal(ipi_id, kcpuset_attached, skip_self);
    263  1.4   thorpej }
    264  1.4   thorpej 
    265  1.4   thorpej /*
    266  1.1     rmind  * put_msg: insert message into the mailbox.
    267  1.9  riastrad  *
    268  1.9  riastrad  * Caller is responsible for issuing membar_exit first.
    269  1.1     rmind  */
    270  1.1     rmind static inline void
    271  1.1     rmind put_msg(ipi_mbox_t *mbox, ipi_msg_t *msg)
    272  1.1     rmind {
    273  1.1     rmind 	int count = SPINLOCK_BACKOFF_MIN;
    274  1.1     rmind again:
    275  1.1     rmind 	for (u_int i = 0; i < IPI_MSG_MAX; i++) {
    276  1.9  riastrad 		if (atomic_cas_ptr(&mbox->msg[i], NULL, msg) == NULL) {
    277  1.1     rmind 			return;
    278  1.1     rmind 		}
    279  1.1     rmind 	}
    280  1.1     rmind 
    281  1.1     rmind 	/* All slots are full: we have to spin-wait. */
    282  1.1     rmind 	ipi_mboxfull_ev.ev_count++;
    283  1.1     rmind 	SPINLOCK_BACKOFF(count);
    284  1.1     rmind 	goto again;
    285  1.1     rmind }
    286  1.1     rmind 
    287  1.1     rmind /*
    288  1.1     rmind  * ipi_cpu_handler: the IPI handler.
    289  1.1     rmind  */
    290  1.1     rmind void
    291  1.1     rmind ipi_cpu_handler(void)
    292  1.1     rmind {
    293  1.2     rmind 	struct cpu_info * const ci = curcpu();
    294  1.2     rmind 
    295  1.2     rmind 	/*
    296  1.2     rmind 	 * Handle asynchronous IPIs: inspect per-CPU bit field, extract
    297  1.2     rmind 	 * IPI ID numbers and execute functions in those slots.
    298  1.2     rmind 	 */
    299  1.2     rmind 	for (u_int i = 0; i < IPI_BITWORDS; i++) {
    300  1.2     rmind 		uint32_t pending, bit;
    301  1.2     rmind 
    302  1.9  riastrad 		if (atomic_load_relaxed(&ci->ci_ipipend[i]) == 0) {
    303  1.2     rmind 			continue;
    304  1.2     rmind 		}
    305  1.2     rmind 		pending = atomic_swap_32(&ci->ci_ipipend[i], 0);
    306  1.2     rmind #ifndef __HAVE_ATOMIC_AS_MEMBAR
    307  1.9  riastrad 		membar_enter();
    308  1.2     rmind #endif
    309  1.2     rmind 		while ((bit = ffs(pending)) != 0) {
    310  1.2     rmind 			const u_int ipi_id = (i << IPI_BITW_SHIFT) | --bit;
    311  1.2     rmind 			ipi_intr_t *ipi_hdl = &ipi_intrs[ipi_id];
    312  1.2     rmind 
    313  1.2     rmind 			pending &= ~(1U << bit);
    314  1.2     rmind 			KASSERT(ipi_hdl->func != NULL);
    315  1.2     rmind 			ipi_hdl->func(ipi_hdl->arg);
    316  1.2     rmind 		}
    317  1.2     rmind 	}
    318  1.2     rmind }
    319  1.2     rmind 
    320  1.2     rmind /*
    321  1.2     rmind  * ipi_msg_cpu_handler: handle synchronous IPIs - iterate mailbox,
    322  1.2     rmind  * execute the passed functions and acknowledge the messages.
    323  1.2     rmind  */
    324  1.2     rmind static void
    325  1.2     rmind ipi_msg_cpu_handler(void *arg __unused)
    326  1.2     rmind {
    327  1.1     rmind 	const struct cpu_info * const ci = curcpu();
    328  1.1     rmind 	ipi_mbox_t *mbox = &ipi_mboxes[cpu_index(ci)];
    329  1.1     rmind 
    330  1.1     rmind 	for (u_int i = 0; i < IPI_MSG_MAX; i++) {
    331  1.1     rmind 		ipi_msg_t *msg;
    332  1.1     rmind 
    333  1.1     rmind 		/* Get the message. */
    334  1.9  riastrad 		if ((msg = atomic_load_acquire(&mbox->msg[i])) == NULL) {
    335  1.1     rmind 			continue;
    336  1.1     rmind 		}
    337  1.9  riastrad 		atomic_store_relaxed(&mbox->msg[i], NULL);
    338  1.1     rmind 
    339  1.1     rmind 		/* Execute the handler. */
    340  1.1     rmind 		KASSERT(msg->func);
    341  1.1     rmind 		msg->func(msg->arg);
    342  1.1     rmind 
    343  1.1     rmind 		/* Ack the request. */
    344  1.5       ryo #ifndef __HAVE_ATOMIC_AS_MEMBAR
    345  1.9  riastrad 		membar_exit();
    346  1.5       ryo #endif
    347  1.1     rmind 		atomic_dec_uint(&msg->_pending);
    348  1.1     rmind 	}
    349  1.1     rmind }
    350  1.1     rmind 
    351  1.1     rmind /*
    352  1.1     rmind  * ipi_unicast: send an IPI to a single CPU.
    353  1.1     rmind  *
    354  1.1     rmind  * => The CPU must be remote; must not be local.
    355  1.1     rmind  * => The caller must ipi_wait() on the message for completion.
    356  1.1     rmind  */
    357  1.1     rmind void
    358  1.1     rmind ipi_unicast(ipi_msg_t *msg, struct cpu_info *ci)
    359  1.1     rmind {
    360  1.1     rmind 	const cpuid_t id = cpu_index(ci);
    361  1.1     rmind 
    362  1.1     rmind 	KASSERT(msg->func != NULL);
    363  1.1     rmind 	KASSERT(kpreempt_disabled());
    364  1.1     rmind 	KASSERT(curcpu() != ci);
    365  1.1     rmind 
    366  1.1     rmind 	msg->_pending = 1;
    367  1.9  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    368  1.9  riastrad 	membar_exit();
    369  1.9  riastrad #endif
    370  1.1     rmind 
    371  1.1     rmind 	put_msg(&ipi_mboxes[id], msg);
    372  1.2     rmind 	ipi_trigger(IPI_SYNCH_ID, ci);
    373  1.1     rmind }
    374  1.1     rmind 
    375  1.1     rmind /*
    376  1.1     rmind  * ipi_multicast: send an IPI to each CPU in the specified set.
    377  1.1     rmind  *
    378  1.1     rmind  * => The caller must ipi_wait() on the message for completion.
    379  1.1     rmind  */
    380  1.1     rmind void
    381  1.1     rmind ipi_multicast(ipi_msg_t *msg, const kcpuset_t *target)
    382  1.1     rmind {
    383  1.1     rmind 	const struct cpu_info * const self = curcpu();
    384  1.1     rmind 	CPU_INFO_ITERATOR cii;
    385  1.1     rmind 	struct cpu_info *ci;
    386  1.1     rmind 	u_int local;
    387  1.1     rmind 
    388  1.1     rmind 	KASSERT(msg->func != NULL);
    389  1.1     rmind 	KASSERT(kpreempt_disabled());
    390  1.1     rmind 
    391  1.1     rmind 	local = !!kcpuset_isset(target, cpu_index(self));
    392  1.1     rmind 	msg->_pending = kcpuset_countset(target) - local;
    393  1.9  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    394  1.9  riastrad 	membar_exit();
    395  1.9  riastrad #endif
    396  1.1     rmind 
    397  1.1     rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    398  1.1     rmind 		cpuid_t id;
    399  1.1     rmind 
    400  1.1     rmind 		if (__predict_false(ci == self)) {
    401  1.1     rmind 			continue;
    402  1.1     rmind 		}
    403  1.1     rmind 		id = cpu_index(ci);
    404  1.1     rmind 		if (!kcpuset_isset(target, id)) {
    405  1.1     rmind 			continue;
    406  1.1     rmind 		}
    407  1.1     rmind 		put_msg(&ipi_mboxes[id], msg);
    408  1.2     rmind 		ipi_trigger(IPI_SYNCH_ID, ci);
    409  1.1     rmind 	}
    410  1.1     rmind 	if (local) {
    411  1.1     rmind 		msg->func(msg->arg);
    412  1.1     rmind 	}
    413  1.1     rmind }
    414  1.1     rmind 
    415  1.1     rmind /*
    416  1.1     rmind  * ipi_broadcast: send an IPI to all CPUs.
    417  1.1     rmind  *
    418  1.1     rmind  * => The caller must ipi_wait() on the message for completion.
    419  1.1     rmind  */
    420  1.1     rmind void
    421  1.4   thorpej ipi_broadcast(ipi_msg_t *msg, bool skip_self)
    422  1.1     rmind {
    423  1.1     rmind 	const struct cpu_info * const self = curcpu();
    424  1.1     rmind 	CPU_INFO_ITERATOR cii;
    425  1.1     rmind 	struct cpu_info *ci;
    426  1.1     rmind 
    427  1.1     rmind 	KASSERT(msg->func != NULL);
    428  1.1     rmind 	KASSERT(kpreempt_disabled());
    429  1.1     rmind 
    430  1.1     rmind 	msg->_pending = ncpu - 1;
    431  1.9  riastrad #ifndef __HAVE_ATOMIC_AS_MEMBAR
    432  1.9  riastrad 	membar_exit();
    433  1.9  riastrad #endif
    434  1.1     rmind 
    435  1.1     rmind 	/* Broadcast IPIs for remote CPUs. */
    436  1.1     rmind 	for (CPU_INFO_FOREACH(cii, ci)) {
    437  1.1     rmind 		cpuid_t id;
    438  1.1     rmind 
    439  1.1     rmind 		if (__predict_false(ci == self)) {
    440  1.1     rmind 			continue;
    441  1.1     rmind 		}
    442  1.1     rmind 		id = cpu_index(ci);
    443  1.1     rmind 		put_msg(&ipi_mboxes[id], msg);
    444  1.2     rmind 		ipi_trigger(IPI_SYNCH_ID, ci);
    445  1.1     rmind 	}
    446  1.1     rmind 
    447  1.4   thorpej 	if (!skip_self) {
    448  1.4   thorpej 		/* Finally, execute locally. */
    449  1.4   thorpej 		msg->func(msg->arg);
    450  1.4   thorpej 	}
    451  1.1     rmind }
    452  1.1     rmind 
    453  1.1     rmind /*
    454  1.1     rmind  * ipi_wait: spin-wait until the message is processed.
    455  1.1     rmind  */
    456  1.1     rmind void
    457  1.1     rmind ipi_wait(ipi_msg_t *msg)
    458  1.1     rmind {
    459  1.1     rmind 	int count = SPINLOCK_BACKOFF_MIN;
    460  1.1     rmind 
    461  1.9  riastrad 	while (atomic_load_acquire(&msg->_pending)) {
    462  1.9  riastrad 		KASSERT(atomic_load_relaxed(&msg->_pending) < ncpu);
    463  1.1     rmind 		SPINLOCK_BACKOFF(count);
    464  1.1     rmind 	}
    465  1.1     rmind }
    466