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 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; 61 62 void (*cpustop_restartfunc)(void); 63 #endif 64 /* This is used in modules that need to work in both SMP and UP. */ 65 cpumask_t all_cpus; 66 67 int mp_ncpus; 68 /* export this for libkvm consumers. */ 69 int mp_maxcpus = MAXCPU; 70 71 volatile int smp_started; 72 u_int mp_maxid; 73 74 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP"); 75 76 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0, 77 "Max CPU ID."); 78 79 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0, 80 "Max number of CPUs that the system was compiled for."); 81 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"); 85 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); 90 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"); 94 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); 99 100 #ifdef SMP 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"); 106 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"); 112 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]; 120 121 /* 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. 126 */ 127 struct mtx smp_ipi_mtx; 128 129 /* 130 * Let the MD SMP code initialize mp_maxid very early if it can. 131 */ 132 static void 133 mp_setmaxid(void *dummy) 134 { 135 cpu_mp_setmaxid(); 136 } 137 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL); 138 139 /* 140 * Call the MD SMP initialization code. 141 */ 142 static void 143 mp_start(void *dummy) 144 { 145 146 /* Probe for MP hardware. */ 147 if (smp_disabled != 0 || cpu_mp_probe() == 0) { 148 mp_ncpus = 1; 149 all_cpus = PCPU_GET(cpumask); 150 return; 151 } 152 153 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN); 154 cpu_mp_start(); 155 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n", 156 mp_ncpus); 157 cpu_mp_announce(); 158 } 159 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL); 160 161 void 162 forward_signal(struct thread *td) 163 { 164 int id; 165 166 /* 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(). 170 */ 171 THREAD_LOCK_ASSERT(td, MA_OWNED); 172 KASSERT(TD_IS_RUNNING(td), 173 ("forward_signal: thread is not TDS_RUNNING")); 174 175 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc); 176 177 if (!smp_started || cold || panicstr) 178 return; 179 if (!forward_signal_enabled) 180 return; 181 182 /* No need to IPI ourself. */ 183 if (td == curthread) 184 return; 185 186 id = td->td_oncpu; 187 if (id == NOCPU) 188 return; 189 ipi_selected(1 << id, IPI_AST); 190 } 191 192 void 193 forward_roundrobin(void) 194 { 195 struct pcpu *pc; 196 struct thread *td; 197 cpumask_t id, map, me; 198 199 CTR0(KTR_SMP, "forward_roundrobin()"); 200 201 if (!smp_started || cold || panicstr) 202 return; 203 if (!forward_roundrobin_enabled) 204 return; 205 map = 0; 206 me = PCPU_GET(cpumask); 207 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) { 208 td = pc->pc_curthread; 209 id = pc->pc_cpumask; 210 if (id != me && (id & stopped_cpus) == 0 && 211 !TD_IS_IDLETHREAD(td)) { 212 td->td_flags |= TDF_NEEDRESCHED; 213 map |= id; 214 } 215 } 216 ipi_selected(map, IPI_AST); 217 } 218 219 /* 220 * When called the executing CPU will send an IPI to all other CPUs 221 * requesting that they halt execution. 222 * 223 * Usually (but not necessarily) called with 'other_cpus' as its arg. 224 * 225 * - Signals all CPUs in map to stop. 226 * - Waits for each to stop. 227 * 228 * Returns: 229 * -1: error 230 * 0: NA 231 * 1: ok 232 * 233 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs 234 * from executing at same time. 235 */ 236 static int 237 generic_stop_cpus(cpumask_t map, u_int type) 238 { 239 int i; 240 241 KASSERT(type == IPI_STOP || type == IPI_STOP_HARD, 242 ("%s: invalid stop type", __func__)); 243 244 if (!smp_started) 245 return 0; 246 247 CTR2(KTR_SMP, "stop_cpus(%x) with %u type", map, type); 248 249 /* send the stop IPI to all CPUs in map */ 250 ipi_selected(map, type); 251 252 i = 0; 253 while ((stopped_cpus & map) != map) { 254 /* spin */ 255 cpu_spinwait(); 256 i++; 257 #ifdef DIAGNOSTIC 258 if (i == 100000) { 259 printf("timeout stopping cpus\n"); 260 break; 261 } 262 #endif 263 } 264 265 return 1; 266 } 267 268 int 269 stop_cpus(cpumask_t map) 270 { 271 272 return (generic_stop_cpus(map, IPI_STOP)); 273 } 274 275 int 276 stop_cpus_hard(cpumask_t map) 277 { 278 279 return (generic_stop_cpus(map, IPI_STOP_HARD)); 280 } 281 282 #if defined(__amd64__) 283 /* 284 * When called the executing CPU will send an IPI to all other CPUs 285 * requesting that they halt execution. 286 * 287 * Usually (but not necessarily) called with 'other_cpus' as its arg. 288 * 289 * - Signals all CPUs in map to suspend. 290 * - Waits for each to suspend. 291 * 292 * Returns: 293 * -1: error 294 * 0: NA 295 * 1: ok 296 * 297 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs 298 * from executing at same time. 299 */ 300 int 301 suspend_cpus(cpumask_t map) 302 { 303 int i; 304 305 if (!smp_started) 306 return (0); 307 308 CTR1(KTR_SMP, "suspend_cpus(%x)", map); 309 310 /* send the suspend IPI to all CPUs in map */ 311 ipi_selected(map, IPI_SUSPEND); 312 313 i = 0; 314 while ((stopped_cpus & map) != map) { 315 /* spin */ 316 cpu_spinwait(); 317 i++; 318 #ifdef DIAGNOSTIC 319 if (i == 100000) { 320 printf("timeout suspending cpus\n"); 321 break; 322 } 323 #endif 324 } 325 326 return (1); 327 } 328 #endif 329 330 /* 331 * Called by a CPU to restart stopped CPUs. 332 * 333 * Usually (but not necessarily) called with 'stopped_cpus' as its arg. 334 * 335 * - Signals all CPUs in map to restart. 336 * - Waits for each to restart. 337 * 338 * Returns: 339 * -1: error 340 * 0: NA 341 * 1: ok 342 */ 343 int 344 restart_cpus(cpumask_t map) 345 { 346 347 if (!smp_started) 348 return 0; 349 350 CTR1(KTR_SMP, "restart_cpus(%x)", map); 351 352 /* signal other cpus to restart */ 353 atomic_store_rel_int(&started_cpus, map); 354 355 /* wait for each to clear its bit */ 356 while ((stopped_cpus & map) != 0) 357 cpu_spinwait(); 358 359 return 1; 360 } 361 362 /* 363 * All-CPU rendezvous. CPUs are signalled, all execute the setup function 364 * (if specified), rendezvous, execute the action function (if specified), 365 * rendezvous again, execute the teardown function (if specified), and then 366 * resume. 367 * 368 * Note that the supplied external functions _must_ be reentrant and aware 369 * that they are running in parallel and in an unknown lock context. 370 */ 371 void 372 smp_rendezvous_action(void) 373 { 374 void* local_func_arg = smp_rv_func_arg; 375 void (*local_setup_func)(void*) = smp_rv_setup_func; 376 void (*local_action_func)(void*) = smp_rv_action_func; 377 void (*local_teardown_func)(void*) = smp_rv_teardown_func; 378 379 /* Ensure we have up-to-date values. */ 380 atomic_add_acq_int(&smp_rv_waiters[0], 1); 381 while (smp_rv_waiters[0] < smp_rv_ncpus) 382 cpu_spinwait(); 383 384 /* setup function */ 385 if (local_setup_func != smp_no_rendevous_barrier) { 386 if (smp_rv_setup_func != NULL) 387 smp_rv_setup_func(smp_rv_func_arg); 388 389 /* spin on entry rendezvous */ 390 atomic_add_int(&smp_rv_waiters[1], 1); 391 while (smp_rv_waiters[1] < smp_rv_ncpus) 392 cpu_spinwait(); 393 } 394 395 /* action function */ 396 if (local_action_func != NULL) 397 local_action_func(local_func_arg); 398 399 /* spin on exit rendezvous */ 400 atomic_add_int(&smp_rv_waiters[2], 1); 401 if (local_teardown_func == smp_no_rendevous_barrier) 402 return; 403 while (smp_rv_waiters[2] < smp_rv_ncpus) 404 cpu_spinwait(); 405 406 /* teardown function */ 407 if (local_teardown_func != NULL) 408 local_teardown_func(local_func_arg); 409 } 410 411 void 412 smp_rendezvous_cpus(cpumask_t map, 413 void (* setup_func)(void *), 414 void (* action_func)(void *), 415 void (* teardown_func)(void *), 416 void *arg) 417 { 418 int i, ncpus = 0; 419 420 if (!smp_started) { 421 if (setup_func != NULL) 422 setup_func(arg); 423 if (action_func != NULL) 424 action_func(arg); 425 if (teardown_func != NULL) 426 teardown_func(arg); 427 return; 428 } 429 430 for (i = 0; i <= mp_maxid; i++) 431 if (((1 << i) & map) != 0 && !CPU_ABSENT(i)) 432 ncpus++; 433 if (ncpus == 0) 434 panic("ncpus is 0 with map=0x%x", map); 435 436 /* obtain rendezvous lock */ 437 mtx_lock_spin(&smp_ipi_mtx); 438 439 /* set static function pointers */ 440 smp_rv_ncpus = ncpus; 441 smp_rv_setup_func = setup_func; 442 smp_rv_action_func = action_func; 443 smp_rv_teardown_func = teardown_func; 444 smp_rv_func_arg = arg; 445 smp_rv_waiters[1] = 0; 446 smp_rv_waiters[2] = 0; 447 atomic_store_rel_int(&smp_rv_waiters[0], 0); 448 449 /* signal other processors, which will enter the IPI with interrupts off */ 450 ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS); 451 452 /* Check if the current CPU is in the map */ 453 if ((map & (1 << curcpu)) != 0) 454 smp_rendezvous_action(); 455 456 if (teardown_func == smp_no_rendevous_barrier) 457 while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus) 458 cpu_spinwait(); 459 460 /* release lock */ 461 mtx_unlock_spin(&smp_ipi_mtx); 462 } 463 464 void 465 smp_rendezvous(void (* setup_func)(void *), 466 void (* action_func)(void *), 467 void (* teardown_func)(void *), 468 void *arg) 469 { 470 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg); 471 } 472 473 static struct cpu_group group[MAXCPU]; 474 475 struct cpu_group * 476 smp_topo(void) 477 { 478 struct cpu_group *top; 479 480 /* 481 * Check for a fake topology request for debugging purposes. 482 */ 483 switch (smp_topology) { 484 case 1: 485 /* Dual core with no sharing. */ 486 top = smp_topo_1level(CG_SHARE_NONE, 2, 0); 487 break; 488 case 2: 489 /* No topology, all cpus are equal. */ 490 top = smp_topo_none(); 491 break; 492 case 3: 493 /* Dual core with shared L2. */ 494 top = smp_topo_1level(CG_SHARE_L2, 2, 0); 495 break; 496 case 4: 497 /* quad core, shared l3 among each package, private l2. */ 498 top = smp_topo_1level(CG_SHARE_L3, 4, 0); 499 break; 500 case 5: 501 /* quad core, 2 dualcore parts on each package share l2. */ 502 top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0); 503 break; 504 case 6: 505 /* Single-core 2xHTT */ 506 top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT); 507 break; 508 case 7: 509 /* quad core with a shared l3, 8 threads sharing L2. */ 510 top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8, 511 CG_FLAG_SMT); 512 break; 513 default: 514 /* Default, ask the system what it wants. */ 515 top = cpu_topo(); 516 break; 517 } 518 /* 519 * Verify the returned topology. 520 */ 521 if (top->cg_count != mp_ncpus) 522 panic("Built bad topology at %p. CPU count %d != %d", 523 top, top->cg_count, mp_ncpus); 524 if (top->cg_mask != all_cpus) 525 panic("Built bad topology at %p. CPU mask 0x%X != 0x%X", 526 top, top->cg_mask, all_cpus); 527 return (top); 528 } 529 530 struct cpu_group * 531 smp_topo_none(void) 532 { 533 struct cpu_group *top; 534 535 top = &group[0]; 536 top->cg_parent = NULL; 537 top->cg_child = NULL; 538 top->cg_mask = (1 << mp_ncpus) - 1; 539 top->cg_count = mp_ncpus; 540 top->cg_children = 0; 541 top->cg_level = CG_SHARE_NONE; 542 top->cg_flags = 0; 543 544 return (top); 545 } 546 547 static int 548 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share, 549 int count, int flags, int start) 550 { 551 cpumask_t mask; 552 int i; 553 554 for (mask = 0, i = 0; i < count; i++, start++) 555 mask |= (1 << start); 556 child->cg_parent = parent; 557 child->cg_child = NULL; 558 child->cg_children = 0; 559 child->cg_level = share; 560 child->cg_count = count; 561 child->cg_flags = flags; 562 child->cg_mask = mask; 563 parent->cg_children++; 564 for (; parent != NULL; parent = parent->cg_parent) { 565 if ((parent->cg_mask & child->cg_mask) != 0) 566 panic("Duplicate children in %p. mask 0x%X child 0x%X", 567 parent, parent->cg_mask, child->cg_mask); 568 parent->cg_mask |= child->cg_mask; 569 parent->cg_count += child->cg_count; 570 } 571 572 return (start); 573 } 574 575 struct cpu_group * 576 smp_topo_1level(int share, int count, int flags) 577 { 578 struct cpu_group *child; 579 struct cpu_group *top; 580 int packages; 581 int cpu; 582 int i; 583 584 cpu = 0; 585 top = &group[0]; 586 packages = mp_ncpus / count; 587 top->cg_child = child = &group[1]; 588 top->cg_level = CG_SHARE_NONE; 589 for (i = 0; i < packages; i++, child++) 590 cpu = smp_topo_addleaf(top, child, share, count, flags, cpu); 591 return (top); 592 } 593 594 struct cpu_group * 595 smp_topo_2level(int l2share, int l2count, int l1share, int l1count, 596 int l1flags) 597 { 598 struct cpu_group *top; 599 struct cpu_group *l1g; 600 struct cpu_group *l2g; 601 int cpu; 602 int i; 603 int j; 604 605 cpu = 0; 606 top = &group[0]; 607 l2g = &group[1]; 608 top->cg_child = l2g; 609 top->cg_level = CG_SHARE_NONE; 610 top->cg_children = mp_ncpus / (l2count * l1count); 611 l1g = l2g + top->cg_children; 612 for (i = 0; i < top->cg_children; i++, l2g++) { 613 l2g->cg_parent = top; 614 l2g->cg_child = l1g; 615 l2g->cg_level = l2share; 616 for (j = 0; j < l2count; j++, l1g++) 617 cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count, 618 l1flags, cpu); 619 } 620 return (top); 621 } 622 623 624 struct cpu_group * 625 smp_topo_find(struct cpu_group *top, int cpu) 626 { 627 struct cpu_group *cg; 628 cpumask_t mask; 629 int children; 630 int i; 631 632 mask = (1 << cpu); 633 cg = top; 634 for (;;) { 635 if ((cg->cg_mask & mask) == 0) 636 return (NULL); 637 if (cg->cg_children == 0) 638 return (cg); 639 children = cg->cg_children; 640 for (i = 0, cg = cg->cg_child; i < children; cg++, i++) 641 if ((cg->cg_mask & mask) != 0) 642 break; 643 } 644 return (NULL); 645 } 646 #else /* !SMP */ 647 648 void 649 smp_rendezvous_cpus(cpumask_t map, 650 void (*setup_func)(void *), 651 void (*action_func)(void *), 652 void (*teardown_func)(void *), 653 void *arg) 654 { 655 if (setup_func != NULL) 656 setup_func(arg); 657 if (action_func != NULL) 658 action_func(arg); 659 if (teardown_func != NULL) 660 teardown_func(arg); 661 } 662 663 void 664 smp_rendezvous(void (*setup_func)(void *), 665 void (*action_func)(void *), 666 void (*teardown_func)(void *), 667 void *arg) 668 { 669 670 if (setup_func != NULL) 671 setup_func(arg); 672 if (action_func != NULL) 673 action_func(arg); 674 if (teardown_func != NULL) 675 teardown_func(arg); 676 } 677 678 /* 679 * Provide dummy SMP support for UP kernels. Modules that need to use SMP 680 * APIs will still work using this dummy support. 681 */ 682 static void 683 mp_setvariables_for_up(void *dummy) 684 { 685 mp_ncpus = 1; 686 mp_maxid = PCPU_GET(cpuid); 687 all_cpus = PCPU_GET(cpumask); 688 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero")); 689 } 690 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST, 691 mp_setvariables_for_up, NULL); 692 #endif /* SMP */ 693 694 void 695 smp_no_rendevous_barrier(void *dummy) 696 { 697 #ifdef SMP 698 KASSERT((!smp_started),("smp_no_rendevous called and smp is started")); 699 #endif 700 } 701