xref: /freebsd/sys/kern/subr_smp.c (revision 6f9c8e5b074419423648ffb89b83fd2f257e90b7)
1 /*-
2  * Copyright (c) 2001, John Baldwin <jhb@FreeBSD.org>.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * This module holds the global variables and machine independent functions
32  * used for the kernel SMP support.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/proc.h>
43 #include <sys/bus.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/pcpu.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49 
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
52 
53 #include "opt_sched.h"
54 
55 #ifdef SMP
56 volatile cpuset_t stopped_cpus;
57 volatile cpuset_t started_cpus;
58 cpuset_t hlt_cpus_mask;
59 cpuset_t logical_cpus_mask;
60 
61 void (*cpustop_restartfunc)(void);
62 #endif
63 /* This is used in modules that need to work in both SMP and UP. */
64 cpuset_t all_cpus;
65 
66 int mp_ncpus;
67 /* export this for libkvm consumers. */
68 int mp_maxcpus = MAXCPU;
69 
70 volatile int smp_started;
71 u_int mp_maxid;
72 
73 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
74 
75 SYSCTL_UINT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
76     "Max CPU ID.");
77 
78 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
79     "Max number of CPUs that the system was compiled for.");
80 
81 int smp_active = 0;	/* are the APs allowed to run? */
82 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
83     "Number of Auxillary Processors (APs) that were successfully started");
84 
85 int smp_disabled = 0;	/* has smp been disabled? */
86 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
87     "SMP has been disabled from the loader");
88 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
89 
90 int smp_cpus = 1;	/* how many cpu's running */
91 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
92     "Number of CPUs online");
93 
94 int smp_topology = 0;	/* Which topology we're using. */
95 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RD, &smp_topology, 0,
96     "Topology override setting; 0 is default provided by hardware.");
97 TUNABLE_INT("kern.smp.topology", &smp_topology);
98 
99 #ifdef SMP
100 /* Enable forwarding of a signal to a process running on a different CPU */
101 static int forward_signal_enabled = 1;
102 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
103 	   &forward_signal_enabled, 0,
104 	   "Forwarding of a signal to a process on a different CPU");
105 
106 /* Variables needed for SMP rendezvous. */
107 static volatile int smp_rv_ncpus;
108 static void (*volatile smp_rv_setup_func)(void *arg);
109 static void (*volatile smp_rv_action_func)(void *arg);
110 static void (*volatile smp_rv_teardown_func)(void *arg);
111 static void *volatile smp_rv_func_arg;
112 static volatile int smp_rv_waiters[3];
113 static volatile int smp_rv_generation;
114 
115 /*
116  * Shared mutex to restrict busywaits between smp_rendezvous() and
117  * smp(_targeted)_tlb_shootdown().  A deadlock occurs if both of these
118  * functions trigger at once and cause multiple CPUs to busywait with
119  * interrupts disabled.
120  */
121 struct mtx smp_ipi_mtx;
122 
123 /*
124  * Let the MD SMP code initialize mp_maxid very early if it can.
125  */
126 static void
127 mp_setmaxid(void *dummy)
128 {
129 	cpu_mp_setmaxid();
130 }
131 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
132 
133 /*
134  * Call the MD SMP initialization code.
135  */
136 static void
137 mp_start(void *dummy)
138 {
139 
140 	mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
141 
142 	/* Probe for MP hardware. */
143 	if (smp_disabled != 0 || cpu_mp_probe() == 0) {
144 		mp_ncpus = 1;
145 		all_cpus = PCPU_GET(cpumask);
146 		return;
147 	}
148 
149 	cpu_mp_start();
150 	printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
151 	    mp_ncpus);
152 	cpu_mp_announce();
153 }
154 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
155 
156 void
157 forward_signal(struct thread *td)
158 {
159 	int id;
160 
161 	/*
162 	 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
163 	 * this thread, so all we need to do is poke it if it is currently
164 	 * executing so that it executes ast().
165 	 */
166 	THREAD_LOCK_ASSERT(td, MA_OWNED);
167 	KASSERT(TD_IS_RUNNING(td),
168 	    ("forward_signal: thread is not TDS_RUNNING"));
169 
170 	CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
171 
172 	if (!smp_started || cold || panicstr)
173 		return;
174 	if (!forward_signal_enabled)
175 		return;
176 
177 	/* No need to IPI ourself. */
178 	if (td == curthread)
179 		return;
180 
181 	id = td->td_oncpu;
182 	if (id == NOCPU)
183 		return;
184 	ipi_cpu(id, IPI_AST);
185 }
186 
187 /*
188  * When called the executing CPU will send an IPI to all other CPUs
189  *  requesting that they halt execution.
190  *
191  * Usually (but not necessarily) called with 'other_cpus' as its arg.
192  *
193  *  - Signals all CPUs in map to stop.
194  *  - Waits for each to stop.
195  *
196  * Returns:
197  *  -1: error
198  *   0: NA
199  *   1: ok
200  *
201  */
202 static int
203 generic_stop_cpus(cpuset_t map, u_int type)
204 {
205 #ifdef KTR
206 	char cpusetbuf[CPUSETBUFSIZ];
207 #endif
208 	static volatile u_int stopping_cpu = NOCPU;
209 	int i;
210 
211 	KASSERT(
212 #if defined(__amd64__)
213 	    type == IPI_STOP || type == IPI_STOP_HARD || type == IPI_SUSPEND,
214 #else
215 	    type == IPI_STOP || type == IPI_STOP_HARD,
216 #endif
217 	    ("%s: invalid stop type", __func__));
218 
219 	if (!smp_started)
220 		return (0);
221 
222 	CTR2(KTR_SMP, "stop_cpus(%s) with %u type",
223 	    cpusetobj_strprint(cpusetbuf, &map), type);
224 
225 	if (stopping_cpu != PCPU_GET(cpuid))
226 		while (atomic_cmpset_int(&stopping_cpu, NOCPU,
227 		    PCPU_GET(cpuid)) == 0)
228 			while (stopping_cpu != NOCPU)
229 				cpu_spinwait(); /* spin */
230 
231 	/* send the stop IPI to all CPUs in map */
232 	ipi_selected(map, type);
233 
234 	i = 0;
235 	while (!CPU_SUBSET(&stopped_cpus, &map)) {
236 		/* spin */
237 		cpu_spinwait();
238 		i++;
239 #ifdef DIAGNOSTIC
240 		if (i == 100000) {
241 			printf("timeout stopping cpus\n");
242 			break;
243 		}
244 #endif
245 	}
246 
247 	stopping_cpu = NOCPU;
248 	return (1);
249 }
250 
251 int
252 stop_cpus(cpuset_t map)
253 {
254 
255 	return (generic_stop_cpus(map, IPI_STOP));
256 }
257 
258 int
259 stop_cpus_hard(cpuset_t map)
260 {
261 
262 	return (generic_stop_cpus(map, IPI_STOP_HARD));
263 }
264 
265 #if defined(__amd64__)
266 int
267 suspend_cpus(cpuset_t map)
268 {
269 
270 	return (generic_stop_cpus(map, IPI_SUSPEND));
271 }
272 #endif
273 
274 /*
275  * Called by a CPU to restart stopped CPUs.
276  *
277  * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
278  *
279  *  - Signals all CPUs in map to restart.
280  *  - Waits for each to restart.
281  *
282  * Returns:
283  *  -1: error
284  *   0: NA
285  *   1: ok
286  */
287 int
288 restart_cpus(cpuset_t map)
289 {
290 #ifdef KTR
291 	char cpusetbuf[CPUSETBUFSIZ];
292 #endif
293 
294 	if (!smp_started)
295 		return 0;
296 
297 	CTR1(KTR_SMP, "restart_cpus(%s)", cpusetobj_strprint(cpusetbuf, &map));
298 
299 	/* signal other cpus to restart */
300 	CPU_COPY_STORE_REL(&map, &started_cpus);
301 
302 	/* wait for each to clear its bit */
303 	while (CPU_OVERLAP(&stopped_cpus, &map))
304 		cpu_spinwait();
305 
306 	return 1;
307 }
308 
309 /*
310  * All-CPU rendezvous.  CPUs are signalled, all execute the setup function
311  * (if specified), rendezvous, execute the action function (if specified),
312  * rendezvous again, execute the teardown function (if specified), and then
313  * resume.
314  *
315  * Note that the supplied external functions _must_ be reentrant and aware
316  * that they are running in parallel and in an unknown lock context.
317  */
318 void
319 smp_rendezvous_action(void)
320 {
321 	struct thread *td;
322 	void *local_func_arg;
323 	void (*local_setup_func)(void*);
324 	void (*local_action_func)(void*);
325 	void (*local_teardown_func)(void*);
326 	int generation;
327 #ifdef INVARIANTS
328 	int owepreempt;
329 #endif
330 
331 	/* Ensure we have up-to-date values. */
332 	atomic_add_acq_int(&smp_rv_waiters[0], 1);
333 	while (smp_rv_waiters[0] < smp_rv_ncpus)
334 		cpu_spinwait();
335 
336 	/* Fetch rendezvous parameters after acquire barrier. */
337 	local_func_arg = smp_rv_func_arg;
338 	local_setup_func = smp_rv_setup_func;
339 	local_action_func = smp_rv_action_func;
340 	local_teardown_func = smp_rv_teardown_func;
341 	generation = smp_rv_generation;
342 
343 	/*
344 	 * Use a nested critical section to prevent any preemptions
345 	 * from occurring during a rendezvous action routine.
346 	 * Specifically, if a rendezvous handler is invoked via an IPI
347 	 * and the interrupted thread was in the critical_exit()
348 	 * function after setting td_critnest to 0 but before
349 	 * performing a deferred preemption, this routine can be
350 	 * invoked with td_critnest set to 0 and td_owepreempt true.
351 	 * In that case, a critical_exit() during the rendezvous
352 	 * action would trigger a preemption which is not permitted in
353 	 * a rendezvous action.  To fix this, wrap all of the
354 	 * rendezvous action handlers in a critical section.  We
355 	 * cannot use a regular critical section however as having
356 	 * critical_exit() preempt from this routine would also be
357 	 * problematic (the preemption must not occur before the IPI
358 	 * has been acknowledged via an EOI).  Instead, we
359 	 * intentionally ignore td_owepreempt when leaving the
360 	 * critical section.  This should be harmless because we do
361 	 * not permit rendezvous action routines to schedule threads,
362 	 * and thus td_owepreempt should never transition from 0 to 1
363 	 * during this routine.
364 	 */
365 	td = curthread;
366 	td->td_critnest++;
367 #ifdef INVARIANTS
368 	owepreempt = td->td_owepreempt;
369 #endif
370 
371 	/*
372 	 * If requested, run a setup function before the main action
373 	 * function.  Ensure all CPUs have completed the setup
374 	 * function before moving on to the action function.
375 	 */
376 	if (local_setup_func != smp_no_rendevous_barrier) {
377 		if (smp_rv_setup_func != NULL)
378 			smp_rv_setup_func(smp_rv_func_arg);
379 		atomic_add_int(&smp_rv_waiters[1], 1);
380 		while (smp_rv_waiters[1] < smp_rv_ncpus)
381                 	cpu_spinwait();
382 	}
383 
384 	if (local_action_func != NULL)
385 		local_action_func(local_func_arg);
386 
387 	/*
388 	 * Signal that the main action has been completed.  If a
389 	 * full exit rendezvous is requested, then all CPUs will
390 	 * wait here until all CPUs have finished the main action.
391 	 *
392 	 * Note that the write by the last CPU to finish the action
393 	 * may become visible to different CPUs at different times.
394 	 * As a result, the CPU that initiated the rendezvous may
395 	 * exit the rendezvous and drop the lock allowing another
396 	 * rendezvous to be initiated on the same CPU or a different
397 	 * CPU.  In that case the exit sentinel may be cleared before
398 	 * all CPUs have noticed causing those CPUs to hang forever.
399 	 * Workaround this by using a generation count to notice when
400 	 * this race occurs and to exit the rendezvous in that case.
401 	 */
402 	MPASS(generation == smp_rv_generation);
403 	atomic_add_int(&smp_rv_waiters[2], 1);
404 	if (local_teardown_func != smp_no_rendevous_barrier) {
405 		while (smp_rv_waiters[2] < smp_rv_ncpus &&
406 		    generation == smp_rv_generation)
407 			cpu_spinwait();
408 
409 		if (local_teardown_func != NULL)
410 			local_teardown_func(local_func_arg);
411 	}
412 
413 	td->td_critnest--;
414 	KASSERT(owepreempt == td->td_owepreempt,
415 	    ("rendezvous action changed td_owepreempt"));
416 }
417 
418 void
419 smp_rendezvous_cpus(cpuset_t map,
420 	void (* setup_func)(void *),
421 	void (* action_func)(void *),
422 	void (* teardown_func)(void *),
423 	void *arg)
424 {
425 	int curcpumap, i, ncpus = 0;
426 
427 	if (!smp_started) {
428 		if (setup_func != NULL)
429 			setup_func(arg);
430 		if (action_func != NULL)
431 			action_func(arg);
432 		if (teardown_func != NULL)
433 			teardown_func(arg);
434 		return;
435 	}
436 
437 	CPU_FOREACH(i) {
438 		if (CPU_ISSET(i, &map))
439 			ncpus++;
440 	}
441 	if (ncpus == 0)
442 		panic("ncpus is 0 with non-zero map");
443 
444 	mtx_lock_spin(&smp_ipi_mtx);
445 
446 	atomic_add_acq_int(&smp_rv_generation, 1);
447 
448 	/* Pass rendezvous parameters via global variables. */
449 	smp_rv_ncpus = ncpus;
450 	smp_rv_setup_func = setup_func;
451 	smp_rv_action_func = action_func;
452 	smp_rv_teardown_func = teardown_func;
453 	smp_rv_func_arg = arg;
454 	smp_rv_waiters[1] = 0;
455 	smp_rv_waiters[2] = 0;
456 	atomic_store_rel_int(&smp_rv_waiters[0], 0);
457 
458 	/*
459 	 * Signal other processors, which will enter the IPI with
460 	 * interrupts off.
461 	 */
462 	curcpumap = CPU_ISSET(curcpu, &map);
463 	CPU_CLR(curcpu, &map);
464 	ipi_selected(map, IPI_RENDEZVOUS);
465 
466 	/* Check if the current CPU is in the map */
467 	if (curcpumap != 0)
468 		smp_rendezvous_action();
469 
470 	/*
471 	 * If the caller did not request an exit barrier to be enforced
472 	 * on each CPU, ensure that this CPU waits for all the other
473 	 * CPUs to finish the rendezvous.
474 	 */
475 	if (teardown_func == smp_no_rendevous_barrier)
476 		while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
477 			cpu_spinwait();
478 
479 	mtx_unlock_spin(&smp_ipi_mtx);
480 }
481 
482 void
483 smp_rendezvous(void (* setup_func)(void *),
484 	       void (* action_func)(void *),
485 	       void (* teardown_func)(void *),
486 	       void *arg)
487 {
488 	smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
489 }
490 
491 static struct cpu_group group[MAXCPU];
492 
493 struct cpu_group *
494 smp_topo(void)
495 {
496 	char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
497 	struct cpu_group *top;
498 
499 	/*
500 	 * Check for a fake topology request for debugging purposes.
501 	 */
502 	switch (smp_topology) {
503 	case 1:
504 		/* Dual core with no sharing.  */
505 		top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
506 		break;
507 	case 2:
508 		/* No topology, all cpus are equal. */
509 		top = smp_topo_none();
510 		break;
511 	case 3:
512 		/* Dual core with shared L2.  */
513 		top = smp_topo_1level(CG_SHARE_L2, 2, 0);
514 		break;
515 	case 4:
516 		/* quad core, shared l3 among each package, private l2.  */
517 		top = smp_topo_1level(CG_SHARE_L3, 4, 0);
518 		break;
519 	case 5:
520 		/* quad core,  2 dualcore parts on each package share l2.  */
521 		top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
522 		break;
523 	case 6:
524 		/* Single-core 2xHTT */
525 		top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
526 		break;
527 	case 7:
528 		/* quad core with a shared l3, 8 threads sharing L2.  */
529 		top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
530 		    CG_FLAG_SMT);
531 		break;
532 	default:
533 		/* Default, ask the system what it wants. */
534 		top = cpu_topo();
535 		break;
536 	}
537 	/*
538 	 * Verify the returned topology.
539 	 */
540 	if (top->cg_count != mp_ncpus)
541 		panic("Built bad topology at %p.  CPU count %d != %d",
542 		    top, top->cg_count, mp_ncpus);
543 	if (CPU_CMP(&top->cg_mask, &all_cpus))
544 		panic("Built bad topology at %p.  CPU mask (%s) != (%s)",
545 		    top, cpusetobj_strprint(cpusetbuf, &top->cg_mask),
546 		    cpusetobj_strprint(cpusetbuf2, &all_cpus));
547 	return (top);
548 }
549 
550 struct cpu_group *
551 smp_topo_none(void)
552 {
553 	struct cpu_group *top;
554 
555 	top = &group[0];
556 	top->cg_parent = NULL;
557 	top->cg_child = NULL;
558 	top->cg_mask = all_cpus;
559 	top->cg_count = mp_ncpus;
560 	top->cg_children = 0;
561 	top->cg_level = CG_SHARE_NONE;
562 	top->cg_flags = 0;
563 
564 	return (top);
565 }
566 
567 static int
568 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
569     int count, int flags, int start)
570 {
571 	char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
572 	cpuset_t mask;
573 	int i;
574 
575 	CPU_ZERO(&mask);
576 	for (i = 0; i < count; i++, start++)
577 		CPU_SET(start, &mask);
578 	child->cg_parent = parent;
579 	child->cg_child = NULL;
580 	child->cg_children = 0;
581 	child->cg_level = share;
582 	child->cg_count = count;
583 	child->cg_flags = flags;
584 	child->cg_mask = mask;
585 	parent->cg_children++;
586 	for (; parent != NULL; parent = parent->cg_parent) {
587 		if (CPU_OVERLAP(&parent->cg_mask, &child->cg_mask))
588 			panic("Duplicate children in %p.  mask (%s) child (%s)",
589 			    parent,
590 			    cpusetobj_strprint(cpusetbuf, &parent->cg_mask),
591 			    cpusetobj_strprint(cpusetbuf2, &child->cg_mask));
592 		CPU_OR(&parent->cg_mask, &child->cg_mask);
593 		parent->cg_count += child->cg_count;
594 	}
595 
596 	return (start);
597 }
598 
599 struct cpu_group *
600 smp_topo_1level(int share, int count, int flags)
601 {
602 	struct cpu_group *child;
603 	struct cpu_group *top;
604 	int packages;
605 	int cpu;
606 	int i;
607 
608 	cpu = 0;
609 	top = &group[0];
610 	packages = mp_ncpus / count;
611 	top->cg_child = child = &group[1];
612 	top->cg_level = CG_SHARE_NONE;
613 	for (i = 0; i < packages; i++, child++)
614 		cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
615 	return (top);
616 }
617 
618 struct cpu_group *
619 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
620     int l1flags)
621 {
622 	struct cpu_group *top;
623 	struct cpu_group *l1g;
624 	struct cpu_group *l2g;
625 	int cpu;
626 	int i;
627 	int j;
628 
629 	cpu = 0;
630 	top = &group[0];
631 	l2g = &group[1];
632 	top->cg_child = l2g;
633 	top->cg_level = CG_SHARE_NONE;
634 	top->cg_children = mp_ncpus / (l2count * l1count);
635 	l1g = l2g + top->cg_children;
636 	for (i = 0; i < top->cg_children; i++, l2g++) {
637 		l2g->cg_parent = top;
638 		l2g->cg_child = l1g;
639 		l2g->cg_level = l2share;
640 		for (j = 0; j < l2count; j++, l1g++)
641 			cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
642 			    l1flags, cpu);
643 	}
644 	return (top);
645 }
646 
647 
648 struct cpu_group *
649 smp_topo_find(struct cpu_group *top, int cpu)
650 {
651 	struct cpu_group *cg;
652 	cpuset_t mask;
653 	int children;
654 	int i;
655 
656 	CPU_SETOF(cpu, &mask);
657 	cg = top;
658 	for (;;) {
659 		if (!CPU_OVERLAP(&cg->cg_mask, &mask))
660 			return (NULL);
661 		if (cg->cg_children == 0)
662 			return (cg);
663 		children = cg->cg_children;
664 		for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
665 			if (CPU_OVERLAP(&cg->cg_mask, &mask))
666 				break;
667 	}
668 	return (NULL);
669 }
670 #else /* !SMP */
671 
672 void
673 smp_rendezvous_cpus(cpuset_t map,
674 	void (*setup_func)(void *),
675 	void (*action_func)(void *),
676 	void (*teardown_func)(void *),
677 	void *arg)
678 {
679 	if (setup_func != NULL)
680 		setup_func(arg);
681 	if (action_func != NULL)
682 		action_func(arg);
683 	if (teardown_func != NULL)
684 		teardown_func(arg);
685 }
686 
687 void
688 smp_rendezvous(void (*setup_func)(void *),
689 	       void (*action_func)(void *),
690 	       void (*teardown_func)(void *),
691 	       void *arg)
692 {
693 
694 	if (setup_func != NULL)
695 		setup_func(arg);
696 	if (action_func != NULL)
697 		action_func(arg);
698 	if (teardown_func != NULL)
699 		teardown_func(arg);
700 }
701 
702 /*
703  * Provide dummy SMP support for UP kernels.  Modules that need to use SMP
704  * APIs will still work using this dummy support.
705  */
706 static void
707 mp_setvariables_for_up(void *dummy)
708 {
709 	mp_ncpus = 1;
710 	mp_maxid = PCPU_GET(cpuid);
711 	all_cpus = PCPU_GET(cpumask);
712 	KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
713 }
714 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
715     mp_setvariables_for_up, NULL);
716 #endif /* SMP */
717 
718 void
719 smp_no_rendevous_barrier(void *dummy)
720 {
721 #ifdef SMP
722 	KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
723 #endif
724 }
725