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 /* Enable forwarding of roundrobin to all other cpus */
108 static int forward_roundrobin_enabled = 1;
109 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
110 &forward_roundrobin_enabled, 0,
111 "Forwarding of roundrobin to all other CPUs");
113 /* Variables needed for SMP rendezvous. */
114 static volatile int smp_rv_ncpus;
115 static void (*volatile smp_rv_setup_func)(void *arg);
116 static void (*volatile smp_rv_action_func)(void *arg);
117 static void (*volatile smp_rv_teardown_func)(void *arg);
118 static void *volatile smp_rv_func_arg;
119 static volatile int smp_rv_waiters[3];
122 * Shared mutex to restrict busywaits between smp_rendezvous() and
123 * smp(_targeted)_tlb_shootdown(). A deadlock occurs if both of these
124 * functions trigger at once and cause multiple CPUs to busywait with
125 * interrupts disabled.
127 struct mtx smp_ipi_mtx;
130 * Let the MD SMP code initialize mp_maxid very early if it can.
133 mp_setmaxid(void *dummy)
137 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
140 * Call the MD SMP initialization code.
143 mp_start(void *dummy)
146 /* Probe for MP hardware. */
147 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
149 all_cpus = PCPU_GET(cpumask);
153 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
155 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
159 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
162 forward_signal(struct thread *td)
167 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
168 * this thread, so all we need to do is poke it if it is currently
169 * executing so that it executes ast().
171 THREAD_LOCK_ASSERT(td, MA_OWNED);
172 KASSERT(TD_IS_RUNNING(td),
173 ("forward_signal: thread is not TDS_RUNNING"));
175 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
177 if (!smp_started || cold || panicstr)
179 if (!forward_signal_enabled)
182 /* No need to IPI ourself. */
189 ipi_selected(1 << id, IPI_AST);
193 forward_roundrobin(void)
197 cpumask_t id, map, me;
199 CTR0(KTR_SMP, "forward_roundrobin()");
201 if (!smp_started || cold || panicstr)
203 if (!forward_roundrobin_enabled)
206 me = PCPU_GET(cpumask);
207 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
208 td = pc->pc_curthread;
210 if (id != me && (id & stopped_cpus) == 0 &&
211 !TD_IS_IDLETHREAD(td)) {
212 td->td_flags |= TDF_NEEDRESCHED;
216 ipi_selected(map, IPI_AST);
220 * When called the executing CPU will send an IPI to all other CPUs
221 * requesting that they halt execution.
223 * Usually (but not necessarily) called with 'other_cpus' as its arg.
225 * - Signals all CPUs in map to stop.
226 * - Waits for each to stop.
233 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
234 * from executing at same time.
237 stop_cpus(cpumask_t map)
244 CTR1(KTR_SMP, "stop_cpus(%x)", map);
246 /* send the stop IPI to all CPUs in map */
247 ipi_selected(map, IPI_STOP);
250 while ((stopped_cpus & map) != map) {
256 printf("timeout stopping cpus\n");
265 #if defined(__amd64__)
267 * When called the executing CPU will send an IPI to all other CPUs
268 * requesting that they halt execution.
270 * Usually (but not necessarily) called with 'other_cpus' as its arg.
272 * - Signals all CPUs in map to suspend.
273 * - Waits for each to suspend.
280 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
281 * from executing at same time.
284 suspend_cpus(cpumask_t map)
291 CTR1(KTR_SMP, "suspend_cpus(%x)", map);
293 /* send the suspend IPI to all CPUs in map */
294 ipi_selected(map, IPI_SUSPEND);
297 while ((stopped_cpus & map) != map) {
303 printf("timeout suspending cpus\n");
314 * Called by a CPU to restart stopped CPUs.
316 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
318 * - Signals all CPUs in map to restart.
319 * - Waits for each to restart.
327 restart_cpus(cpumask_t map)
333 CTR1(KTR_SMP, "restart_cpus(%x)", map);
335 /* signal other cpus to restart */
336 atomic_store_rel_int(&started_cpus, map);
338 /* wait for each to clear its bit */
339 while ((stopped_cpus & map) != 0)
346 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
347 * (if specified), rendezvous, execute the action function (if specified),
348 * rendezvous again, execute the teardown function (if specified), and then
351 * Note that the supplied external functions _must_ be reentrant and aware
352 * that they are running in parallel and in an unknown lock context.
355 smp_rendezvous_action(void)
357 void* local_func_arg = smp_rv_func_arg;
358 void (*local_setup_func)(void*) = smp_rv_setup_func;
359 void (*local_action_func)(void*) = smp_rv_action_func;
360 void (*local_teardown_func)(void*) = smp_rv_teardown_func;
362 /* Ensure we have up-to-date values. */
363 atomic_add_acq_int(&smp_rv_waiters[0], 1);
364 while (smp_rv_waiters[0] < smp_rv_ncpus)
368 if (local_setup_func != smp_no_rendevous_barrier) {
369 if (smp_rv_setup_func != NULL)
370 smp_rv_setup_func(smp_rv_func_arg);
372 /* spin on entry rendezvous */
373 atomic_add_int(&smp_rv_waiters[1], 1);
374 while (smp_rv_waiters[1] < smp_rv_ncpus)
378 /* action function */
379 if (local_action_func != NULL)
380 local_action_func(local_func_arg);
382 /* spin on exit rendezvous */
383 atomic_add_int(&smp_rv_waiters[2], 1);
384 if (local_teardown_func == smp_no_rendevous_barrier)
386 while (smp_rv_waiters[2] < smp_rv_ncpus)
389 /* teardown function */
390 if (local_teardown_func != NULL)
391 local_teardown_func(local_func_arg);
395 smp_rendezvous_cpus(cpumask_t map,
396 void (* setup_func)(void *),
397 void (* action_func)(void *),
398 void (* teardown_func)(void *),
404 if (setup_func != NULL)
406 if (action_func != NULL)
408 if (teardown_func != NULL)
413 for (i = 0; i <= mp_maxid; i++)
414 if (((1 << i) & map) != 0 && !CPU_ABSENT(i))
417 panic("ncpus is 0 with map=0x%x", map);
419 /* obtain rendezvous lock */
420 mtx_lock_spin(&smp_ipi_mtx);
422 /* set static function pointers */
423 smp_rv_ncpus = ncpus;
424 smp_rv_setup_func = setup_func;
425 smp_rv_action_func = action_func;
426 smp_rv_teardown_func = teardown_func;
427 smp_rv_func_arg = arg;
428 smp_rv_waiters[1] = 0;
429 smp_rv_waiters[2] = 0;
430 atomic_store_rel_int(&smp_rv_waiters[0], 0);
432 /* signal other processors, which will enter the IPI with interrupts off */
433 ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
435 /* Check if the current CPU is in the map */
436 if ((map & (1 << curcpu)) != 0)
437 smp_rendezvous_action();
439 if (teardown_func == smp_no_rendevous_barrier)
440 while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
444 mtx_unlock_spin(&smp_ipi_mtx);
448 smp_rendezvous(void (* setup_func)(void *),
449 void (* action_func)(void *),
450 void (* teardown_func)(void *),
453 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
456 static struct cpu_group group[MAXCPU];
461 struct cpu_group *top;
464 * Check for a fake topology request for debugging purposes.
466 switch (smp_topology) {
468 /* Dual core with no sharing. */
469 top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
472 /* No topology, all cpus are equal. */
473 top = smp_topo_none();
476 /* Dual core with shared L2. */
477 top = smp_topo_1level(CG_SHARE_L2, 2, 0);
480 /* quad core, shared l3 among each package, private l2. */
481 top = smp_topo_1level(CG_SHARE_L3, 4, 0);
484 /* quad core, 2 dualcore parts on each package share l2. */
485 top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
488 /* Single-core 2xHTT */
489 top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
492 /* quad core with a shared l3, 8 threads sharing L2. */
493 top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
497 /* Default, ask the system what it wants. */
502 * Verify the returned topology.
504 if (top->cg_count != mp_ncpus)
505 panic("Built bad topology at %p. CPU count %d != %d",
506 top, top->cg_count, mp_ncpus);
507 if (top->cg_mask != all_cpus)
508 panic("Built bad topology at %p. CPU mask 0x%X != 0x%X",
509 top, top->cg_mask, all_cpus);
516 struct cpu_group *top;
519 top->cg_parent = NULL;
520 top->cg_child = NULL;
521 top->cg_mask = (1 << mp_ncpus) - 1;
522 top->cg_count = mp_ncpus;
523 top->cg_children = 0;
524 top->cg_level = CG_SHARE_NONE;
531 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
532 int count, int flags, int start)
537 for (mask = 0, i = 0; i < count; i++, start++)
538 mask |= (1 << start);
539 child->cg_parent = parent;
540 child->cg_child = NULL;
541 child->cg_children = 0;
542 child->cg_level = share;
543 child->cg_count = count;
544 child->cg_flags = flags;
545 child->cg_mask = mask;
546 parent->cg_children++;
547 for (; parent != NULL; parent = parent->cg_parent) {
548 if ((parent->cg_mask & child->cg_mask) != 0)
549 panic("Duplicate children in %p. mask 0x%X child 0x%X",
550 parent, parent->cg_mask, child->cg_mask);
551 parent->cg_mask |= child->cg_mask;
552 parent->cg_count += child->cg_count;
559 smp_topo_1level(int share, int count, int flags)
561 struct cpu_group *child;
562 struct cpu_group *top;
569 packages = mp_ncpus / count;
570 top->cg_child = child = &group[1];
571 top->cg_level = CG_SHARE_NONE;
572 for (i = 0; i < packages; i++, child++)
573 cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
578 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
581 struct cpu_group *top;
582 struct cpu_group *l1g;
583 struct cpu_group *l2g;
592 top->cg_level = CG_SHARE_NONE;
593 top->cg_children = mp_ncpus / (l2count * l1count);
594 l1g = l2g + top->cg_children;
595 for (i = 0; i < top->cg_children; i++, l2g++) {
596 l2g->cg_parent = top;
598 l2g->cg_level = l2share;
599 for (j = 0; j < l2count; j++, l1g++)
600 cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
608 smp_topo_find(struct cpu_group *top, int cpu)
610 struct cpu_group *cg;
618 if ((cg->cg_mask & mask) == 0)
620 if (cg->cg_children == 0)
622 children = cg->cg_children;
623 for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
624 if ((cg->cg_mask & mask) != 0)
632 smp_rendezvous_cpus(cpumask_t map,
633 void (*setup_func)(void *),
634 void (*action_func)(void *),
635 void (*teardown_func)(void *),
638 if (setup_func != NULL)
640 if (action_func != NULL)
642 if (teardown_func != NULL)
647 smp_rendezvous(void (*setup_func)(void *),
648 void (*action_func)(void *),
649 void (*teardown_func)(void *),
653 if (setup_func != NULL)
655 if (action_func != NULL)
657 if (teardown_func != NULL)
662 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
663 * APIs will still work using this dummy support.
666 mp_setvariables_for_up(void *dummy)
669 mp_maxid = PCPU_GET(cpuid);
670 all_cpus = PCPU_GET(cpumask);
671 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
673 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
674 mp_setvariables_for_up, NULL);
678 smp_no_rendevous_barrier(void *dummy)
681 KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));