subr_ipi.c revision 1.11 1 1.11 riastrad /* $NetBSD: subr_ipi.c,v 1.11 2023/02/24 11:02:27 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.11 riastrad __KERNEL_RCSID(0, "$NetBSD: subr_ipi.c,v 1.11 2023/02/24 11:02:27 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.10 riastrad membar_release();
193 1.2 rmind atomic_or_32(&ci->ci_ipipend[i], bitm);
194 1.4 thorpej return true;
195 1.4 thorpej }
196 1.4 thorpej return false;
197 1.4 thorpej }
198 1.4 thorpej
199 1.4 thorpej /*
200 1.4 thorpej * ipi_trigger: asynchronously send an IPI to the specified CPU.
201 1.4 thorpej */
202 1.4 thorpej void
203 1.4 thorpej ipi_trigger(u_int ipi_id, struct cpu_info *ci)
204 1.4 thorpej {
205 1.4 thorpej
206 1.4 thorpej KASSERT(curcpu() != ci);
207 1.4 thorpej if (ipi_mark_pending(ipi_id, ci)) {
208 1.2 rmind cpu_ipi(ci);
209 1.2 rmind }
210 1.2 rmind }
211 1.2 rmind
212 1.2 rmind /*
213 1.4 thorpej * ipi_trigger_multi_internal: the guts of ipi_trigger_multi() and
214 1.4 thorpej * ipi_trigger_broadcast().
215 1.3 rmind */
216 1.4 thorpej static void
217 1.4 thorpej ipi_trigger_multi_internal(u_int ipi_id, const kcpuset_t *target,
218 1.4 thorpej bool skip_self)
219 1.3 rmind {
220 1.3 rmind const cpuid_t selfid = cpu_index(curcpu());
221 1.3 rmind CPU_INFO_ITERATOR cii;
222 1.3 rmind struct cpu_info *ci;
223 1.3 rmind
224 1.3 rmind KASSERT(kpreempt_disabled());
225 1.3 rmind KASSERT(target != NULL);
226 1.3 rmind
227 1.3 rmind for (CPU_INFO_FOREACH(cii, ci)) {
228 1.3 rmind const cpuid_t cpuid = cpu_index(ci);
229 1.3 rmind
230 1.3 rmind if (!kcpuset_isset(target, cpuid) || cpuid == selfid) {
231 1.3 rmind continue;
232 1.3 rmind }
233 1.3 rmind ipi_trigger(ipi_id, ci);
234 1.3 rmind }
235 1.4 thorpej if (!skip_self && kcpuset_isset(target, selfid)) {
236 1.4 thorpej ipi_mark_pending(ipi_id, curcpu());
237 1.3 rmind int s = splhigh();
238 1.3 rmind ipi_cpu_handler();
239 1.3 rmind splx(s);
240 1.3 rmind }
241 1.3 rmind }
242 1.3 rmind
243 1.3 rmind /*
244 1.4 thorpej * ipi_trigger_multi: same as ipi_trigger() but sends to the multiple
245 1.4 thorpej * CPUs given the target CPU set.
246 1.4 thorpej */
247 1.4 thorpej void
248 1.4 thorpej ipi_trigger_multi(u_int ipi_id, const kcpuset_t *target)
249 1.4 thorpej {
250 1.4 thorpej ipi_trigger_multi_internal(ipi_id, target, false);
251 1.4 thorpej }
252 1.4 thorpej
253 1.4 thorpej /*
254 1.4 thorpej * ipi_trigger_broadcast: same as ipi_trigger_multi() to kcpuset_attached,
255 1.4 thorpej * optionally skipping the sending CPU.
256 1.4 thorpej */
257 1.4 thorpej void
258 1.4 thorpej ipi_trigger_broadcast(u_int ipi_id, bool skip_self)
259 1.4 thorpej {
260 1.4 thorpej ipi_trigger_multi_internal(ipi_id, kcpuset_attached, skip_self);
261 1.4 thorpej }
262 1.4 thorpej
263 1.4 thorpej /*
264 1.1 rmind * put_msg: insert message into the mailbox.
265 1.9 riastrad *
266 1.10 riastrad * Caller is responsible for issuing membar_release first.
267 1.1 rmind */
268 1.1 rmind static inline void
269 1.1 rmind put_msg(ipi_mbox_t *mbox, ipi_msg_t *msg)
270 1.1 rmind {
271 1.1 rmind int count = SPINLOCK_BACKOFF_MIN;
272 1.1 rmind again:
273 1.1 rmind for (u_int i = 0; i < IPI_MSG_MAX; i++) {
274 1.9 riastrad if (atomic_cas_ptr(&mbox->msg[i], NULL, msg) == NULL) {
275 1.1 rmind return;
276 1.1 rmind }
277 1.1 rmind }
278 1.1 rmind
279 1.1 rmind /* All slots are full: we have to spin-wait. */
280 1.1 rmind ipi_mboxfull_ev.ev_count++;
281 1.1 rmind SPINLOCK_BACKOFF(count);
282 1.1 rmind goto again;
283 1.1 rmind }
284 1.1 rmind
285 1.1 rmind /*
286 1.1 rmind * ipi_cpu_handler: the IPI handler.
287 1.1 rmind */
288 1.1 rmind void
289 1.1 rmind ipi_cpu_handler(void)
290 1.1 rmind {
291 1.2 rmind struct cpu_info * const ci = curcpu();
292 1.2 rmind
293 1.2 rmind /*
294 1.2 rmind * Handle asynchronous IPIs: inspect per-CPU bit field, extract
295 1.2 rmind * IPI ID numbers and execute functions in those slots.
296 1.2 rmind */
297 1.2 rmind for (u_int i = 0; i < IPI_BITWORDS; i++) {
298 1.2 rmind uint32_t pending, bit;
299 1.2 rmind
300 1.9 riastrad if (atomic_load_relaxed(&ci->ci_ipipend[i]) == 0) {
301 1.2 rmind continue;
302 1.2 rmind }
303 1.2 rmind pending = atomic_swap_32(&ci->ci_ipipend[i], 0);
304 1.10 riastrad membar_acquire();
305 1.2 rmind while ((bit = ffs(pending)) != 0) {
306 1.2 rmind const u_int ipi_id = (i << IPI_BITW_SHIFT) | --bit;
307 1.2 rmind ipi_intr_t *ipi_hdl = &ipi_intrs[ipi_id];
308 1.2 rmind
309 1.2 rmind pending &= ~(1U << bit);
310 1.2 rmind KASSERT(ipi_hdl->func != NULL);
311 1.2 rmind ipi_hdl->func(ipi_hdl->arg);
312 1.2 rmind }
313 1.2 rmind }
314 1.2 rmind }
315 1.2 rmind
316 1.2 rmind /*
317 1.2 rmind * ipi_msg_cpu_handler: handle synchronous IPIs - iterate mailbox,
318 1.2 rmind * execute the passed functions and acknowledge the messages.
319 1.2 rmind */
320 1.2 rmind static void
321 1.2 rmind ipi_msg_cpu_handler(void *arg __unused)
322 1.2 rmind {
323 1.1 rmind const struct cpu_info * const ci = curcpu();
324 1.1 rmind ipi_mbox_t *mbox = &ipi_mboxes[cpu_index(ci)];
325 1.1 rmind
326 1.1 rmind for (u_int i = 0; i < IPI_MSG_MAX; i++) {
327 1.1 rmind ipi_msg_t *msg;
328 1.1 rmind
329 1.1 rmind /* Get the message. */
330 1.9 riastrad if ((msg = atomic_load_acquire(&mbox->msg[i])) == NULL) {
331 1.1 rmind continue;
332 1.1 rmind }
333 1.9 riastrad atomic_store_relaxed(&mbox->msg[i], NULL);
334 1.1 rmind
335 1.1 rmind /* Execute the handler. */
336 1.1 rmind KASSERT(msg->func);
337 1.1 rmind msg->func(msg->arg);
338 1.1 rmind
339 1.1 rmind /* Ack the request. */
340 1.10 riastrad membar_release();
341 1.1 rmind atomic_dec_uint(&msg->_pending);
342 1.1 rmind }
343 1.1 rmind }
344 1.1 rmind
345 1.1 rmind /*
346 1.1 rmind * ipi_unicast: send an IPI to a single CPU.
347 1.1 rmind *
348 1.1 rmind * => The CPU must be remote; must not be local.
349 1.1 rmind * => The caller must ipi_wait() on the message for completion.
350 1.1 rmind */
351 1.1 rmind void
352 1.1 rmind ipi_unicast(ipi_msg_t *msg, struct cpu_info *ci)
353 1.1 rmind {
354 1.1 rmind const cpuid_t id = cpu_index(ci);
355 1.1 rmind
356 1.1 rmind KASSERT(msg->func != NULL);
357 1.1 rmind KASSERT(kpreempt_disabled());
358 1.1 rmind KASSERT(curcpu() != ci);
359 1.1 rmind
360 1.1 rmind msg->_pending = 1;
361 1.10 riastrad membar_release();
362 1.1 rmind
363 1.1 rmind put_msg(&ipi_mboxes[id], msg);
364 1.2 rmind ipi_trigger(IPI_SYNCH_ID, ci);
365 1.1 rmind }
366 1.1 rmind
367 1.1 rmind /*
368 1.1 rmind * ipi_multicast: send an IPI to each CPU in the specified set.
369 1.1 rmind *
370 1.1 rmind * => The caller must ipi_wait() on the message for completion.
371 1.1 rmind */
372 1.1 rmind void
373 1.1 rmind ipi_multicast(ipi_msg_t *msg, const kcpuset_t *target)
374 1.1 rmind {
375 1.1 rmind const struct cpu_info * const self = curcpu();
376 1.1 rmind CPU_INFO_ITERATOR cii;
377 1.1 rmind struct cpu_info *ci;
378 1.1 rmind u_int local;
379 1.1 rmind
380 1.1 rmind KASSERT(msg->func != NULL);
381 1.1 rmind KASSERT(kpreempt_disabled());
382 1.1 rmind
383 1.1 rmind local = !!kcpuset_isset(target, cpu_index(self));
384 1.1 rmind msg->_pending = kcpuset_countset(target) - local;
385 1.10 riastrad membar_release();
386 1.1 rmind
387 1.1 rmind for (CPU_INFO_FOREACH(cii, ci)) {
388 1.1 rmind cpuid_t id;
389 1.1 rmind
390 1.1 rmind if (__predict_false(ci == self)) {
391 1.1 rmind continue;
392 1.1 rmind }
393 1.1 rmind id = cpu_index(ci);
394 1.1 rmind if (!kcpuset_isset(target, id)) {
395 1.1 rmind continue;
396 1.1 rmind }
397 1.1 rmind put_msg(&ipi_mboxes[id], msg);
398 1.2 rmind ipi_trigger(IPI_SYNCH_ID, ci);
399 1.1 rmind }
400 1.1 rmind if (local) {
401 1.1 rmind msg->func(msg->arg);
402 1.1 rmind }
403 1.1 rmind }
404 1.1 rmind
405 1.1 rmind /*
406 1.1 rmind * ipi_broadcast: send an IPI to all CPUs.
407 1.1 rmind *
408 1.1 rmind * => The caller must ipi_wait() on the message for completion.
409 1.1 rmind */
410 1.1 rmind void
411 1.4 thorpej ipi_broadcast(ipi_msg_t *msg, bool skip_self)
412 1.1 rmind {
413 1.1 rmind const struct cpu_info * const self = curcpu();
414 1.1 rmind CPU_INFO_ITERATOR cii;
415 1.1 rmind struct cpu_info *ci;
416 1.1 rmind
417 1.1 rmind KASSERT(msg->func != NULL);
418 1.1 rmind KASSERT(kpreempt_disabled());
419 1.1 rmind
420 1.1 rmind msg->_pending = ncpu - 1;
421 1.10 riastrad membar_release();
422 1.1 rmind
423 1.1 rmind /* Broadcast IPIs for remote CPUs. */
424 1.1 rmind for (CPU_INFO_FOREACH(cii, ci)) {
425 1.1 rmind cpuid_t id;
426 1.1 rmind
427 1.1 rmind if (__predict_false(ci == self)) {
428 1.1 rmind continue;
429 1.1 rmind }
430 1.1 rmind id = cpu_index(ci);
431 1.1 rmind put_msg(&ipi_mboxes[id], msg);
432 1.2 rmind ipi_trigger(IPI_SYNCH_ID, ci);
433 1.1 rmind }
434 1.1 rmind
435 1.4 thorpej if (!skip_self) {
436 1.4 thorpej /* Finally, execute locally. */
437 1.4 thorpej msg->func(msg->arg);
438 1.4 thorpej }
439 1.1 rmind }
440 1.1 rmind
441 1.1 rmind /*
442 1.1 rmind * ipi_wait: spin-wait until the message is processed.
443 1.1 rmind */
444 1.1 rmind void
445 1.1 rmind ipi_wait(ipi_msg_t *msg)
446 1.1 rmind {
447 1.1 rmind int count = SPINLOCK_BACKOFF_MIN;
448 1.1 rmind
449 1.9 riastrad while (atomic_load_acquire(&msg->_pending)) {
450 1.9 riastrad KASSERT(atomic_load_relaxed(&msg->_pending) < ncpu);
451 1.1 rmind SPINLOCK_BACKOFF(count);
452 1.1 rmind }
453 1.1 rmind }
454