2 * Copyright (c) 2001, John Baldwin <jhb@FreeBSD.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 * This module holds the global variables and machine independent functions
32 * used for the kernel SMP support.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
45 #include <sys/mutex.h>
48 #include <sys/sysctl.h>
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
53 #include "opt_sched.h"
56 volatile cpumask_t stopped_cpus;
57 volatile cpumask_t started_cpus;
58 cpumask_t idle_cpus_mask;
59 cpumask_t hlt_cpus_mask;
60 cpumask_t logical_cpus_mask;
62 void (*cpustop_restartfunc)(void);
64 /* This is used in modules that need to work in both SMP and UP. */
68 /* export this for libkvm consumers. */
69 int mp_maxcpus = MAXCPU;
71 volatile int smp_started;
74 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
76 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
79 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
80 "Max number of CPUs that the system was compiled for.");
82 int smp_active = 0; /* are the APs allowed to run? */
83 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
84 "Number of Auxillary Processors (APs) that were successfully started");
86 int smp_disabled = 0; /* has smp been disabled? */
87 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
88 "SMP has been disabled from the loader");
89 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
91 int smp_cpus = 1; /* how many cpu's running */
92 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
93 "Number of CPUs online");
95 int smp_topology = 0; /* Which topology we're using. */
96 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RD, &smp_topology, 0,
97 "Topology override setting; 0 is default provided by hardware.");
98 TUNABLE_INT("kern.smp.topology", &smp_topology);
101 /* Enable forwarding of a signal to a process running on a different CPU */
102 static int forward_signal_enabled = 1;
103 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
104 &forward_signal_enabled, 0,
105 "Forwarding of a signal to a process on a different CPU");
107 /* Variables needed for SMP rendezvous. */
108 static volatile int smp_rv_ncpus;
109 static void (*volatile smp_rv_setup_func)(void *arg);
110 static void (*volatile smp_rv_action_func)(void *arg);
111 static void (*volatile smp_rv_teardown_func)(void *arg);
112 static void *volatile smp_rv_func_arg;
113 static volatile int smp_rv_waiters[3];
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.
121 struct mtx smp_ipi_mtx;
124 * Let the MD SMP code initialize mp_maxid very early if it can.
127 mp_setmaxid(void *dummy)
131 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
134 * Call the MD SMP initialization code.
137 mp_start(void *dummy)
140 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
142 /* Probe for MP hardware. */
143 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
145 all_cpus = PCPU_GET(cpumask);
150 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
154 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
157 forward_signal(struct thread *td)
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().
166 THREAD_LOCK_ASSERT(td, MA_OWNED);
167 KASSERT(TD_IS_RUNNING(td),
168 ("forward_signal: thread is not TDS_RUNNING"));
170 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
172 if (!smp_started || cold || panicstr)
174 if (!forward_signal_enabled)
177 /* No need to IPI ourself. */
184 ipi_selected(1 << id, IPI_AST);
188 * When called the executing CPU will send an IPI to all other CPUs
189 * requesting that they halt execution.
191 * Usually (but not necessarily) called with 'other_cpus' as its arg.
193 * - Signals all CPUs in map to stop.
194 * - Waits for each to stop.
201 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
202 * from executing at same time.
205 generic_stop_cpus(cpumask_t map, u_int type)
209 KASSERT(type == IPI_STOP || type == IPI_STOP_HARD,
210 ("%s: invalid stop type", __func__));
215 CTR2(KTR_SMP, "stop_cpus(%x) with %u type", map, type);
217 /* send the stop IPI to all CPUs in map */
218 ipi_selected(map, type);
221 while ((stopped_cpus & map) != map) {
227 printf("timeout stopping cpus\n");
237 stop_cpus(cpumask_t map)
240 return (generic_stop_cpus(map, IPI_STOP));
244 stop_cpus_hard(cpumask_t map)
247 return (generic_stop_cpus(map, IPI_STOP_HARD));
250 #if defined(__amd64__)
252 * When called the executing CPU will send an IPI to all other CPUs
253 * requesting that they halt execution.
255 * Usually (but not necessarily) called with 'other_cpus' as its arg.
257 * - Signals all CPUs in map to suspend.
258 * - Waits for each to suspend.
265 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
266 * from executing at same time.
269 suspend_cpus(cpumask_t map)
276 CTR1(KTR_SMP, "suspend_cpus(%x)", map);
278 /* send the suspend IPI to all CPUs in map */
279 ipi_selected(map, IPI_SUSPEND);
282 while ((stopped_cpus & map) != map) {
288 printf("timeout suspending cpus\n");
299 * Called by a CPU to restart stopped CPUs.
301 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
303 * - Signals all CPUs in map to restart.
304 * - Waits for each to restart.
312 restart_cpus(cpumask_t map)
318 CTR1(KTR_SMP, "restart_cpus(%x)", map);
320 /* signal other cpus to restart */
321 atomic_store_rel_int(&started_cpus, map);
323 /* wait for each to clear its bit */
324 while ((stopped_cpus & map) != 0)
331 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
332 * (if specified), rendezvous, execute the action function (if specified),
333 * rendezvous again, execute the teardown function (if specified), and then
336 * Note that the supplied external functions _must_ be reentrant and aware
337 * that they are running in parallel and in an unknown lock context.
340 smp_rendezvous_action(void)
342 void* local_func_arg = smp_rv_func_arg;
343 void (*local_setup_func)(void*) = smp_rv_setup_func;
344 void (*local_action_func)(void*) = smp_rv_action_func;
345 void (*local_teardown_func)(void*) = smp_rv_teardown_func;
347 /* Ensure we have up-to-date values. */
348 atomic_add_acq_int(&smp_rv_waiters[0], 1);
349 while (smp_rv_waiters[0] < smp_rv_ncpus)
353 if (local_setup_func != smp_no_rendevous_barrier) {
354 if (smp_rv_setup_func != NULL)
355 smp_rv_setup_func(smp_rv_func_arg);
357 /* spin on entry rendezvous */
358 atomic_add_int(&smp_rv_waiters[1], 1);
359 while (smp_rv_waiters[1] < smp_rv_ncpus)
363 /* action function */
364 if (local_action_func != NULL)
365 local_action_func(local_func_arg);
367 /* spin on exit rendezvous */
368 atomic_add_int(&smp_rv_waiters[2], 1);
369 if (local_teardown_func == smp_no_rendevous_barrier)
371 while (smp_rv_waiters[2] < smp_rv_ncpus)
374 /* teardown function */
375 if (local_teardown_func != NULL)
376 local_teardown_func(local_func_arg);
380 smp_rendezvous_cpus(cpumask_t map,
381 void (* setup_func)(void *),
382 void (* action_func)(void *),
383 void (* teardown_func)(void *),
389 if (setup_func != NULL)
391 if (action_func != NULL)
393 if (teardown_func != NULL)
398 for (i = 0; i <= mp_maxid; i++)
399 if (((1 << i) & map) != 0 && !CPU_ABSENT(i))
402 panic("ncpus is 0 with map=0x%x", map);
404 /* obtain rendezvous lock */
405 mtx_lock_spin(&smp_ipi_mtx);
407 /* set static function pointers */
408 smp_rv_ncpus = ncpus;
409 smp_rv_setup_func = setup_func;
410 smp_rv_action_func = action_func;
411 smp_rv_teardown_func = teardown_func;
412 smp_rv_func_arg = arg;
413 smp_rv_waiters[1] = 0;
414 smp_rv_waiters[2] = 0;
415 atomic_store_rel_int(&smp_rv_waiters[0], 0);
417 /* signal other processors, which will enter the IPI with interrupts off */
418 ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
420 /* Check if the current CPU is in the map */
421 if ((map & (1 << curcpu)) != 0)
422 smp_rendezvous_action();
424 if (teardown_func == smp_no_rendevous_barrier)
425 while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
429 mtx_unlock_spin(&smp_ipi_mtx);
433 smp_rendezvous(void (* setup_func)(void *),
434 void (* action_func)(void *),
435 void (* teardown_func)(void *),
438 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
441 static struct cpu_group group[MAXCPU];
446 struct cpu_group *top;
449 * Check for a fake topology request for debugging purposes.
451 switch (smp_topology) {
453 /* Dual core with no sharing. */
454 top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
457 /* No topology, all cpus are equal. */
458 top = smp_topo_none();
461 /* Dual core with shared L2. */
462 top = smp_topo_1level(CG_SHARE_L2, 2, 0);
465 /* quad core, shared l3 among each package, private l2. */
466 top = smp_topo_1level(CG_SHARE_L3, 4, 0);
469 /* quad core, 2 dualcore parts on each package share l2. */
470 top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
473 /* Single-core 2xHTT */
474 top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
477 /* quad core with a shared l3, 8 threads sharing L2. */
478 top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
482 /* Default, ask the system what it wants. */
487 * Verify the returned topology.
489 if (top->cg_count != mp_ncpus)
490 panic("Built bad topology at %p. CPU count %d != %d",
491 top, top->cg_count, mp_ncpus);
492 if (top->cg_mask != all_cpus)
493 panic("Built bad topology at %p. CPU mask 0x%X != 0x%X",
494 top, top->cg_mask, all_cpus);
501 struct cpu_group *top;
504 top->cg_parent = NULL;
505 top->cg_child = NULL;
506 top->cg_mask = (1 << mp_ncpus) - 1;
507 top->cg_count = mp_ncpus;
508 top->cg_children = 0;
509 top->cg_level = CG_SHARE_NONE;
516 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
517 int count, int flags, int start)
522 for (mask = 0, i = 0; i < count; i++, start++)
523 mask |= (1 << start);
524 child->cg_parent = parent;
525 child->cg_child = NULL;
526 child->cg_children = 0;
527 child->cg_level = share;
528 child->cg_count = count;
529 child->cg_flags = flags;
530 child->cg_mask = mask;
531 parent->cg_children++;
532 for (; parent != NULL; parent = parent->cg_parent) {
533 if ((parent->cg_mask & child->cg_mask) != 0)
534 panic("Duplicate children in %p. mask 0x%X child 0x%X",
535 parent, parent->cg_mask, child->cg_mask);
536 parent->cg_mask |= child->cg_mask;
537 parent->cg_count += child->cg_count;
544 smp_topo_1level(int share, int count, int flags)
546 struct cpu_group *child;
547 struct cpu_group *top;
554 packages = mp_ncpus / count;
555 top->cg_child = child = &group[1];
556 top->cg_level = CG_SHARE_NONE;
557 for (i = 0; i < packages; i++, child++)
558 cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
563 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
566 struct cpu_group *top;
567 struct cpu_group *l1g;
568 struct cpu_group *l2g;
577 top->cg_level = CG_SHARE_NONE;
578 top->cg_children = mp_ncpus / (l2count * l1count);
579 l1g = l2g + top->cg_children;
580 for (i = 0; i < top->cg_children; i++, l2g++) {
581 l2g->cg_parent = top;
583 l2g->cg_level = l2share;
584 for (j = 0; j < l2count; j++, l1g++)
585 cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
593 smp_topo_find(struct cpu_group *top, int cpu)
595 struct cpu_group *cg;
603 if ((cg->cg_mask & mask) == 0)
605 if (cg->cg_children == 0)
607 children = cg->cg_children;
608 for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
609 if ((cg->cg_mask & mask) != 0)
617 smp_rendezvous_cpus(cpumask_t map,
618 void (*setup_func)(void *),
619 void (*action_func)(void *),
620 void (*teardown_func)(void *),
623 if (setup_func != NULL)
625 if (action_func != NULL)
627 if (teardown_func != NULL)
632 smp_rendezvous(void (*setup_func)(void *),
633 void (*action_func)(void *),
634 void (*teardown_func)(void *),
638 if (setup_func != NULL)
640 if (action_func != NULL)
642 if (teardown_func != NULL)
647 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
648 * APIs will still work using this dummy support.
651 mp_setvariables_for_up(void *dummy)
654 mp_maxid = PCPU_GET(cpuid);
655 all_cpus = PCPU_GET(cpumask);
656 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
658 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
659 mp_setvariables_for_up, NULL);
663 smp_no_rendevous_barrier(void *dummy)
666 KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));