1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SMP initialisation and IPI support 4 * Based on arch/arm/kernel/smp.c 5 * 6 * Copyright (C) 2012 ARM Ltd. 7 */ 8 9 #include <linux/acpi.h> 10 #include <linux/arm_sdei.h> 11 #include <linux/delay.h> 12 #include <linux/init.h> 13 #include <linux/spinlock.h> 14 #include <linux/sched/mm.h> 15 #include <linux/sched/hotplug.h> 16 #include <linux/sched/task_stack.h> 17 #include <linux/interrupt.h> 18 #include <linux/cache.h> 19 #include <linux/profile.h> 20 #include <linux/errno.h> 21 #include <linux/mm.h> 22 #include <linux/err.h> 23 #include <linux/cpu.h> 24 #include <linux/smp.h> 25 #include <linux/seq_file.h> 26 #include <linux/irq.h> 27 #include <linux/irqchip/arm-gic-v3.h> 28 #include <linux/percpu.h> 29 #include <linux/clockchips.h> 30 #include <linux/completion.h> 31 #include <linux/of.h> 32 #include <linux/irq_work.h> 33 #include <linux/kernel_stat.h> 34 #include <linux/kexec.h> 35 #include <linux/kgdb.h> 36 #include <linux/kprobes.h> 37 #include <linux/kvm_host.h> 38 #include <linux/nmi.h> 39 40 #include <asm/alternative.h> 41 #include <asm/atomic.h> 42 #include <asm/cacheflush.h> 43 #include <asm/cpu.h> 44 #include <asm/cputype.h> 45 #include <asm/cpu_ops.h> 46 #include <asm/daifflags.h> 47 #include <asm/kvm_mmu.h> 48 #include <asm/mmu_context.h> 49 #include <asm/nmi.h> 50 #include <asm/numa.h> 51 #include <asm/processor.h> 52 #include <asm/smp_plat.h> 53 #include <asm/sections.h> 54 #include <asm/tlbflush.h> 55 #include <asm/ptrace.h> 56 #include <asm/virt.h> 57 58 #include <trace/events/ipi.h> 59 60 /* 61 * as from 2.5, kernels no longer have an init_tasks structure 62 * so we need some other way of telling a new secondary core 63 * where to place its SVC stack 64 */ 65 struct secondary_data secondary_data; 66 /* Number of CPUs which aren't online, but looping in kernel text. */ 67 static int cpus_stuck_in_kernel; 68 69 static int ipi_irq_base __ro_after_init; 70 static int nr_ipi __ro_after_init = NR_IPI; 71 72 struct ipi_descs { 73 struct irq_desc *descs[MAX_IPI]; 74 }; 75 76 static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc); 77 78 #define get_ipi_desc(__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi]) 79 80 static bool percpu_ipi_descs __ro_after_init; 81 82 static bool crash_stop; 83 84 static void ipi_setup(int cpu); 85 86 #ifdef CONFIG_HOTPLUG_CPU 87 static void ipi_teardown(int cpu); 88 static int op_cpu_kill(unsigned int cpu); 89 #else 90 static inline int op_cpu_kill(unsigned int cpu) 91 { 92 return -ENOSYS; 93 } 94 #endif 95 96 97 /* 98 * Boot a secondary CPU, and assign it the specified idle task. 99 * This also gives us the initial stack to use for this CPU. 100 */ 101 static int boot_secondary(unsigned int cpu, struct task_struct *idle) 102 { 103 const struct cpu_operations *ops = get_cpu_ops(cpu); 104 105 if (ops->cpu_boot) 106 return ops->cpu_boot(cpu); 107 108 return -EOPNOTSUPP; 109 } 110 111 static DECLARE_COMPLETION(cpu_running); 112 113 int __cpu_up(unsigned int cpu, struct task_struct *idle) 114 { 115 int ret; 116 long status; 117 118 /* 119 * We need to tell the secondary core where to find its stack and the 120 * page tables. 121 */ 122 secondary_data.task = idle; 123 update_cpu_boot_status(CPU_MMU_OFF); 124 125 /* Now bring the CPU into our world */ 126 ret = boot_secondary(cpu, idle); 127 if (ret) { 128 if (ret != -EPERM) 129 pr_err("CPU%u: failed to boot: %d\n", cpu, ret); 130 return ret; 131 } 132 133 /* 134 * CPU was successfully started, wait for it to come online or 135 * time out. 136 */ 137 wait_for_completion_timeout(&cpu_running, 138 msecs_to_jiffies(5000)); 139 if (cpu_online(cpu)) 140 return 0; 141 142 pr_crit("CPU%u: failed to come online\n", cpu); 143 secondary_data.task = NULL; 144 status = READ_ONCE(secondary_data.status); 145 if (status == CPU_MMU_OFF) 146 status = READ_ONCE(__early_cpu_boot_status); 147 148 switch (status & CPU_BOOT_STATUS_MASK) { 149 default: 150 pr_err("CPU%u: failed in unknown state : 0x%lx\n", 151 cpu, status); 152 cpus_stuck_in_kernel++; 153 break; 154 case CPU_KILL_ME: 155 if (!op_cpu_kill(cpu)) { 156 pr_crit("CPU%u: died during early boot\n", cpu); 157 break; 158 } 159 pr_crit("CPU%u: may not have shut down cleanly\n", cpu); 160 fallthrough; 161 case CPU_STUCK_IN_KERNEL: 162 pr_crit("CPU%u: is stuck in kernel\n", cpu); 163 if (status & CPU_STUCK_REASON_52_BIT_VA) 164 pr_crit("CPU%u: does not support 52-bit VAs\n", cpu); 165 if (status & CPU_STUCK_REASON_NO_GRAN) { 166 pr_crit("CPU%u: does not support %luK granule\n", 167 cpu, PAGE_SIZE / SZ_1K); 168 } 169 cpus_stuck_in_kernel++; 170 break; 171 case CPU_PANIC_KERNEL: 172 panic("CPU%u detected unsupported configuration\n", cpu); 173 } 174 175 return -EIO; 176 } 177 178 static void init_gic_priority_masking(void) 179 { 180 u32 cpuflags; 181 182 if (WARN_ON(!gic_enable_sre())) 183 return; 184 185 cpuflags = read_sysreg(daif); 186 187 WARN_ON(!(cpuflags & PSR_I_BIT)); 188 WARN_ON(!(cpuflags & PSR_F_BIT)); 189 190 gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); 191 } 192 193 /* 194 * This is the secondary CPU boot entry. We're using this CPUs 195 * idle thread stack, but a set of temporary page tables. 196 */ 197 asmlinkage notrace void secondary_start_kernel(void) 198 { 199 u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK; 200 struct mm_struct *mm = &init_mm; 201 const struct cpu_operations *ops; 202 unsigned int cpu = smp_processor_id(); 203 204 /* 205 * All kernel threads share the same mm context; grab a 206 * reference and switch to it. 207 */ 208 mmgrab(mm); 209 current->active_mm = mm; 210 211 /* 212 * TTBR0 is only used for the identity mapping at this stage. Make it 213 * point to zero page to avoid speculatively fetching new entries. 214 */ 215 cpu_uninstall_idmap(); 216 217 if (system_uses_irq_prio_masking()) 218 init_gic_priority_masking(); 219 220 rcutree_report_cpu_starting(cpu); 221 trace_hardirqs_off(); 222 223 /* 224 * If the system has established the capabilities, make sure 225 * this CPU ticks all of those. If it doesn't, the CPU will 226 * fail to come online. 227 */ 228 check_local_cpu_capabilities(); 229 230 ops = get_cpu_ops(cpu); 231 if (ops->cpu_postboot) 232 ops->cpu_postboot(); 233 234 /* 235 * Log the CPU info before it is marked online and might get read. 236 */ 237 cpuinfo_store_cpu(); 238 store_cpu_topology(cpu); 239 240 /* 241 * Enable GIC and timers. 242 */ 243 notify_cpu_starting(cpu); 244 245 ipi_setup(cpu); 246 247 numa_add_cpu(cpu); 248 249 /* 250 * OK, now it's safe to let the boot CPU continue. Wait for 251 * the CPU migration code to notice that the CPU is online 252 * before we continue. 253 */ 254 pr_info("CPU%u: Booted secondary processor 0x%010lx [0x%08x]\n", 255 cpu, (unsigned long)mpidr, 256 read_cpuid_id()); 257 update_cpu_boot_status(CPU_BOOT_SUCCESS); 258 set_cpu_online(cpu, true); 259 complete(&cpu_running); 260 261 /* 262 * Secondary CPUs enter the kernel with all DAIF exceptions masked. 263 * 264 * As with setup_arch() we must unmask Debug and SError exceptions, and 265 * as the root irqchip has already been detected and initialized we can 266 * unmask IRQ and FIQ at the same time. 267 */ 268 local_daif_restore(DAIF_PROCCTX); 269 270 /* 271 * OK, it's off to the idle thread for us 272 */ 273 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE); 274 } 275 276 #ifdef CONFIG_HOTPLUG_CPU 277 static int op_cpu_disable(unsigned int cpu) 278 { 279 const struct cpu_operations *ops = get_cpu_ops(cpu); 280 281 /* 282 * If we don't have a cpu_die method, abort before we reach the point 283 * of no return. CPU0 may not have an cpu_ops, so test for it. 284 */ 285 if (!ops || !ops->cpu_die) 286 return -EOPNOTSUPP; 287 288 /* 289 * We may need to abort a hot unplug for some other mechanism-specific 290 * reason. 291 */ 292 if (ops->cpu_disable) 293 return ops->cpu_disable(cpu); 294 295 return 0; 296 } 297 298 /* 299 * __cpu_disable runs on the processor to be shutdown. 300 */ 301 int __cpu_disable(void) 302 { 303 unsigned int cpu = smp_processor_id(); 304 int ret; 305 306 ret = op_cpu_disable(cpu); 307 if (ret) 308 return ret; 309 310 remove_cpu_topology(cpu); 311 numa_remove_cpu(cpu); 312 313 /* 314 * Take this CPU offline. Once we clear this, we can't return, 315 * and we must not schedule until we're ready to give up the cpu. 316 */ 317 set_cpu_online(cpu, false); 318 ipi_teardown(cpu); 319 320 /* 321 * OK - migrate IRQs away from this CPU 322 */ 323 irq_migrate_all_off_this_cpu(); 324 325 return 0; 326 } 327 328 static int op_cpu_kill(unsigned int cpu) 329 { 330 const struct cpu_operations *ops = get_cpu_ops(cpu); 331 332 /* 333 * If we have no means of synchronising with the dying CPU, then assume 334 * that it is really dead. We can only wait for an arbitrary length of 335 * time and hope that it's dead, so let's skip the wait and just hope. 336 */ 337 if (!ops->cpu_kill) 338 return 0; 339 340 return ops->cpu_kill(cpu); 341 } 342 343 /* 344 * Called on the thread which is asking for a CPU to be shutdown after the 345 * shutdown completed. 346 */ 347 void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu) 348 { 349 int err; 350 351 pr_debug("CPU%u: shutdown\n", cpu); 352 353 /* 354 * Now that the dying CPU is beyond the point of no return w.r.t. 355 * in-kernel synchronisation, try to get the firmware to help us to 356 * verify that it has really left the kernel before we consider 357 * clobbering anything it might still be using. 358 */ 359 err = op_cpu_kill(cpu); 360 if (err) 361 pr_warn("CPU%d may not have shut down cleanly: %d\n", cpu, err); 362 } 363 364 /* 365 * Called from the idle thread for the CPU which has been shutdown. 366 * 367 */ 368 void __noreturn cpu_die(void) 369 { 370 unsigned int cpu = smp_processor_id(); 371 const struct cpu_operations *ops = get_cpu_ops(cpu); 372 373 idle_task_exit(); 374 375 local_daif_mask(); 376 377 /* Tell cpuhp_bp_sync_dead() that this CPU is now safe to dispose of */ 378 cpuhp_ap_report_dead(); 379 380 /* 381 * Actually shutdown the CPU. This must never fail. The specific hotplug 382 * mechanism must perform all required cache maintenance to ensure that 383 * no dirty lines are lost in the process of shutting down the CPU. 384 */ 385 ops->cpu_die(cpu); 386 387 BUG(); 388 } 389 #endif 390 391 static void __cpu_try_die(int cpu) 392 { 393 #ifdef CONFIG_HOTPLUG_CPU 394 const struct cpu_operations *ops = get_cpu_ops(cpu); 395 396 if (ops && ops->cpu_die) 397 ops->cpu_die(cpu); 398 #endif 399 } 400 401 /* 402 * Kill the calling secondary CPU, early in bringup before it is turned 403 * online. 404 */ 405 void __noreturn cpu_die_early(void) 406 { 407 int cpu = smp_processor_id(); 408 409 pr_crit("CPU%d: will not boot\n", cpu); 410 411 /* Mark this CPU absent */ 412 set_cpu_present(cpu, 0); 413 rcutree_report_cpu_dead(); 414 415 if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) { 416 update_cpu_boot_status(CPU_KILL_ME); 417 __cpu_try_die(cpu); 418 } 419 420 update_cpu_boot_status(CPU_STUCK_IN_KERNEL); 421 422 cpu_park_loop(); 423 } 424 425 static void __init hyp_mode_check(void) 426 { 427 if (is_hyp_mode_available()) 428 pr_info("CPU: All CPU(s) started at EL2\n"); 429 else if (is_hyp_mode_mismatched()) 430 WARN_TAINT(1, TAINT_CPU_OUT_OF_SPEC, 431 "CPU: CPUs started in inconsistent modes"); 432 else 433 pr_info("CPU: All CPU(s) started at EL1\n"); 434 if (IS_ENABLED(CONFIG_KVM) && !is_kernel_in_hyp_mode()) { 435 kvm_compute_layout(); 436 kvm_apply_hyp_relocations(); 437 } 438 } 439 440 void __init smp_cpus_done(unsigned int max_cpus) 441 { 442 pr_info("SMP: Total of %d processors activated.\n", num_online_cpus()); 443 hyp_mode_check(); 444 setup_system_features(); 445 setup_user_features(); 446 mark_linear_text_alias_ro(); 447 } 448 449 void __init smp_prepare_boot_cpu(void) 450 { 451 /* 452 * The runtime per-cpu areas have been allocated by 453 * setup_per_cpu_areas(), and CPU0's boot time per-cpu area will be 454 * freed shortly, so we must move over to the runtime per-cpu area. 455 */ 456 set_my_cpu_offset(per_cpu_offset(smp_processor_id())); 457 458 cpuinfo_store_boot_cpu(); 459 setup_boot_cpu_features(); 460 461 /* Conditionally switch to GIC PMR for interrupt masking */ 462 if (system_uses_irq_prio_masking()) 463 init_gic_priority_masking(); 464 465 kasan_init_hw_tags(); 466 /* Init percpu seeds for random tags after cpus are set up. */ 467 kasan_init_sw_tags(); 468 } 469 470 /* 471 * Duplicate MPIDRs are a recipe for disaster. Scan all initialized 472 * entries and check for duplicates. If any is found just ignore the 473 * cpu. cpu_logical_map was initialized to INVALID_HWID to avoid 474 * matching valid MPIDR values. 475 */ 476 static bool __init is_mpidr_duplicate(unsigned int cpu, u64 hwid) 477 { 478 unsigned int i; 479 480 for (i = 1; (i < cpu) && (i < NR_CPUS); i++) 481 if (cpu_logical_map(i) == hwid) 482 return true; 483 return false; 484 } 485 486 /* 487 * Initialize cpu operations for a logical cpu and 488 * set it in the possible mask on success 489 */ 490 static int __init smp_cpu_setup(int cpu) 491 { 492 const struct cpu_operations *ops; 493 494 if (init_cpu_ops(cpu)) 495 return -ENODEV; 496 497 ops = get_cpu_ops(cpu); 498 if (ops->cpu_init(cpu)) 499 return -ENODEV; 500 501 set_cpu_possible(cpu, true); 502 503 return 0; 504 } 505 506 static bool bootcpu_valid __initdata; 507 static unsigned int cpu_count = 1; 508 509 int arch_register_cpu(int cpu) 510 { 511 acpi_handle acpi_handle = acpi_get_processor_handle(cpu); 512 struct cpu *c = &per_cpu(cpu_devices, cpu); 513 514 if (!acpi_disabled && !acpi_handle && 515 IS_ENABLED(CONFIG_ACPI_HOTPLUG_CPU)) 516 return -EPROBE_DEFER; 517 518 #ifdef CONFIG_ACPI_HOTPLUG_CPU 519 /* For now block anything that looks like physical CPU Hotplug */ 520 if (invalid_logical_cpuid(cpu) || !cpu_present(cpu)) { 521 pr_err_once("Changing CPU present bit is not supported\n"); 522 return -ENODEV; 523 } 524 #endif 525 526 /* 527 * Availability of the acpi handle is sufficient to establish 528 * that _STA has already been checked. No need to recheck here. 529 */ 530 c->hotpluggable = arch_cpu_is_hotpluggable(cpu); 531 532 return register_cpu(c, cpu); 533 } 534 535 #ifdef CONFIG_ACPI_HOTPLUG_CPU 536 void arch_unregister_cpu(int cpu) 537 { 538 acpi_handle acpi_handle = acpi_get_processor_handle(cpu); 539 struct cpu *c = &per_cpu(cpu_devices, cpu); 540 unsigned long long sta; 541 acpi_status status; 542 543 status = acpi_evaluate_integer(acpi_handle, "_STA", NULL, &sta); 544 if (!ACPI_FAILURE(status) && 545 cpu_present(cpu) && !(sta & ACPI_STA_DEVICE_PRESENT)) 546 pr_err_once("Changing CPU present bit is not supported\n"); 547 548 unregister_cpu(c); 549 } 550 #endif /* CONFIG_ACPI_HOTPLUG_CPU */ 551 552 #ifdef CONFIG_ACPI 553 static struct acpi_madt_generic_interrupt cpu_madt_gicc[NR_CPUS]; 554 555 struct acpi_madt_generic_interrupt *acpi_cpu_get_madt_gicc(int cpu) 556 { 557 return &cpu_madt_gicc[cpu]; 558 } 559 EXPORT_SYMBOL_GPL(acpi_cpu_get_madt_gicc); 560 561 static bool acpi_cpu_is_present(int cpu) 562 { 563 return acpi_cpu_get_madt_gicc(cpu)->flags & ACPI_MADT_ENABLED; 564 } 565 566 /* 567 * acpi_map_gic_cpu_interface - parse processor MADT entry 568 * 569 * Carry out sanity checks on MADT processor entry and initialize 570 * cpu_logical_map on success 571 */ 572 static void __init 573 acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor) 574 { 575 u64 hwid = processor->arm_mpidr; 576 577 if (!(processor->flags & 578 (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE))) { 579 pr_debug("skipping disabled CPU entry with 0x%llx MPIDR\n", hwid); 580 return; 581 } 582 583 if (hwid & ~MPIDR_HWID_BITMASK || hwid == INVALID_HWID) { 584 pr_err("skipping CPU entry with invalid MPIDR 0x%llx\n", hwid); 585 return; 586 } 587 588 if (is_mpidr_duplicate(cpu_count, hwid)) { 589 pr_err("duplicate CPU MPIDR 0x%llx in MADT\n", hwid); 590 return; 591 } 592 593 /* Check if GICC structure of boot CPU is available in the MADT */ 594 if (cpu_logical_map(0) == hwid) { 595 if (bootcpu_valid) { 596 pr_err("duplicate boot CPU MPIDR: 0x%llx in MADT\n", 597 hwid); 598 return; 599 } 600 bootcpu_valid = true; 601 cpu_madt_gicc[0] = *processor; 602 return; 603 } 604 605 if (cpu_count >= NR_CPUS) 606 return; 607 608 /* map the logical cpu id to cpu MPIDR */ 609 set_cpu_logical_map(cpu_count, hwid); 610 611 cpu_madt_gicc[cpu_count] = *processor; 612 613 /* 614 * Set-up the ACPI parking protocol cpu entries 615 * while initializing the cpu_logical_map to 616 * avoid parsing MADT entries multiple times for 617 * nothing (ie a valid cpu_logical_map entry should 618 * contain a valid parking protocol data set to 619 * initialize the cpu if the parking protocol is 620 * the only available enable method). 621 */ 622 acpi_set_mailbox_entry(cpu_count, processor); 623 624 cpu_count++; 625 } 626 627 static int __init 628 acpi_parse_gic_cpu_interface(union acpi_subtable_headers *header, 629 const unsigned long end) 630 { 631 struct acpi_madt_generic_interrupt *processor; 632 633 processor = (struct acpi_madt_generic_interrupt *)header; 634 if (BAD_MADT_GICC_ENTRY(processor, end)) 635 return -EINVAL; 636 637 acpi_table_print_madt_entry(&header->common); 638 639 acpi_map_gic_cpu_interface(processor); 640 641 return 0; 642 } 643 644 static void __init acpi_parse_and_init_cpus(void) 645 { 646 int i; 647 648 /* 649 * do a walk of MADT to determine how many CPUs 650 * we have including disabled CPUs, and get information 651 * we need for SMP init. 652 */ 653 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, 654 acpi_parse_gic_cpu_interface, 0); 655 656 /* 657 * In ACPI, SMP and CPU NUMA information is provided in separate 658 * static tables, namely the MADT and the SRAT. 659 * 660 * Thus, it is simpler to first create the cpu logical map through 661 * an MADT walk and then map the logical cpus to their node ids 662 * as separate steps. 663 */ 664 acpi_map_cpus_to_nodes(); 665 666 for (i = 0; i < nr_cpu_ids; i++) 667 early_map_cpu_to_node(i, acpi_numa_get_nid(i)); 668 } 669 #else 670 static bool acpi_cpu_is_present(int cpu) 671 { 672 return false; 673 } 674 #define acpi_parse_and_init_cpus(...) do { } while (0) 675 #endif 676 677 /* 678 * Enumerate the possible CPU set from the device tree and build the 679 * cpu logical map array containing MPIDR values related to logical 680 * cpus. Assumes that cpu_logical_map(0) has already been initialized. 681 */ 682 static void __init of_parse_and_init_cpus(void) 683 { 684 struct device_node *dn; 685 686 for_each_of_cpu_node(dn) { 687 u64 hwid = of_get_cpu_hwid(dn, 0); 688 689 if (hwid & ~MPIDR_HWID_BITMASK) 690 goto next; 691 692 if (is_mpidr_duplicate(cpu_count, hwid)) { 693 pr_err("%pOF: duplicate cpu reg properties in the DT\n", 694 dn); 695 goto next; 696 } 697 698 /* 699 * The numbering scheme requires that the boot CPU 700 * must be assigned logical id 0. Record it so that 701 * the logical map built from DT is validated and can 702 * be used. 703 */ 704 if (hwid == cpu_logical_map(0)) { 705 if (bootcpu_valid) { 706 pr_err("%pOF: duplicate boot cpu reg property in DT\n", 707 dn); 708 goto next; 709 } 710 711 bootcpu_valid = true; 712 early_map_cpu_to_node(0, of_node_to_nid(dn)); 713 714 /* 715 * cpu_logical_map has already been 716 * initialized and the boot cpu doesn't need 717 * the enable-method so continue without 718 * incrementing cpu. 719 */ 720 continue; 721 } 722 723 if (cpu_count >= NR_CPUS) 724 goto next; 725 726 pr_debug("cpu logical map 0x%llx\n", hwid); 727 set_cpu_logical_map(cpu_count, hwid); 728 729 early_map_cpu_to_node(cpu_count, of_node_to_nid(dn)); 730 next: 731 cpu_count++; 732 } 733 } 734 735 /* 736 * Enumerate the possible CPU set from the device tree or ACPI and build the 737 * cpu logical map array containing MPIDR values related to logical 738 * cpus. Assumes that cpu_logical_map(0) has already been initialized. 739 */ 740 void __init smp_init_cpus(void) 741 { 742 int i; 743 744 if (acpi_disabled) 745 of_parse_and_init_cpus(); 746 else 747 acpi_parse_and_init_cpus(); 748 749 if (!bootcpu_valid) { 750 pr_err("missing boot CPU MPIDR, not enabling secondaries\n"); 751 return; 752 } 753 754 /* 755 * For the nosmp/maxcpus=0 case, do not mark the secondary CPUs 756 * possible. 757 */ 758 if (!setup_max_cpus) 759 return; 760 761 if (cpu_count > nr_cpu_ids) 762 pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n", 763 cpu_count, nr_cpu_ids); 764 /* 765 * We need to set the cpu_logical_map entries before enabling 766 * the cpus so that cpu processor description entries (DT cpu nodes 767 * and ACPI MADT entries) can be retrieved by matching the cpu hwid 768 * with entries in cpu_logical_map while initializing the cpus. 769 * If the cpu set-up fails, invalidate the cpu_logical_map entry. 770 */ 771 for (i = 1; i < nr_cpu_ids; i++) { 772 if (cpu_logical_map(i) != INVALID_HWID) { 773 if (smp_cpu_setup(i)) 774 set_cpu_logical_map(i, INVALID_HWID); 775 } 776 } 777 } 778 779 void __init smp_prepare_cpus(unsigned int max_cpus) 780 { 781 const struct cpu_operations *ops; 782 int err; 783 unsigned int cpu; 784 unsigned int this_cpu; 785 786 init_cpu_topology(); 787 788 this_cpu = smp_processor_id(); 789 store_cpu_topology(this_cpu); 790 numa_store_cpu_info(this_cpu); 791 numa_add_cpu(this_cpu); 792 793 /* 794 * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set 795 * secondary CPUs present. 796 */ 797 if (max_cpus == 0) 798 return; 799 800 /* 801 * Initialise the present map (which describes the set of CPUs 802 * actually populated at the present time) and release the 803 * secondaries from the bootloader. 804 */ 805 for_each_possible_cpu(cpu) { 806 807 if (cpu == smp_processor_id()) 808 continue; 809 810 ops = get_cpu_ops(cpu); 811 if (!ops) 812 continue; 813 814 err = ops->cpu_prepare(cpu); 815 if (err) 816 continue; 817 818 if (acpi_disabled || acpi_cpu_is_present(cpu)) 819 set_cpu_present(cpu, true); 820 numa_store_cpu_info(cpu); 821 } 822 } 823 824 static const char *ipi_types[MAX_IPI] __tracepoint_string = { 825 [IPI_RESCHEDULE] = "Rescheduling interrupts", 826 [IPI_CALL_FUNC] = "Function call interrupts", 827 [IPI_CPU_STOP] = "CPU stop interrupts", 828 [IPI_CPU_STOP_NMI] = "CPU stop NMIs", 829 [IPI_TIMER] = "Timer broadcast interrupts", 830 [IPI_IRQ_WORK] = "IRQ work interrupts", 831 [IPI_CPU_BACKTRACE] = "CPU backtrace interrupts", 832 [IPI_KGDB_ROUNDUP] = "KGDB roundup interrupts", 833 }; 834 835 static void smp_cross_call(const struct cpumask *target, unsigned int ipinr); 836 837 unsigned long irq_err_count; 838 839 int arch_show_interrupts(struct seq_file *p, int prec) 840 { 841 unsigned int cpu, i; 842 843 for (i = 0; i < MAX_IPI; i++) { 844 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i); 845 for_each_online_cpu(cpu) 846 seq_printf(p, "%10u ", irq_desc_kstat_cpu(get_ipi_desc(cpu, i), cpu)); 847 seq_printf(p, " %s\n", ipi_types[i]); 848 } 849 850 seq_printf(p, "%*s: %10lu\n", prec, "Err", irq_err_count); 851 return 0; 852 } 853 854 void arch_send_call_function_ipi_mask(const struct cpumask *mask) 855 { 856 smp_cross_call(mask, IPI_CALL_FUNC); 857 } 858 859 void arch_send_call_function_single_ipi(int cpu) 860 { 861 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC); 862 } 863 864 #ifdef CONFIG_IRQ_WORK 865 void arch_irq_work_raise(void) 866 { 867 smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK); 868 } 869 #endif 870 871 /** 872 * arm64_nmi_cpu_stop() - stop the local CPU after it is told to stop. 873 * @regs: register state to record in the vmcore on a crash stop, or NULL for 874 * panic_smp_self_stop(), which has no interrupted context to save. 875 * @die_on_crash: on the kdump crash path, power the CPU off via PSCI CPU_OFF 876 * (so a capture kernel can reclaim it) rather than parking it. 877 * 878 * The single point every arm64 stop path funnels through, keeping the 879 * bookkeeping (mask interrupts, save the crash context, mark offline, mask 880 * SDEI, optionally power off) in one place: 881 * 882 * - the regular IPI_CPU_STOP and pseudo-NMI IPI_CPU_STOP_NMI handlers; 883 * - panic_smp_self_stop(), a CPU parking itself on a parallel panic(); 884 * - the SDEI cross-CPU NMI handler (drivers/firmware/arm_sdei_nmi.c), 885 * which reaches CPUs the stop IPIs could not. 886 * 887 * The IPI stop handlers pass @die_on_crash true. The SDEI handler and 888 * panic_smp_self_stop() pass false and only park. For SDEI that is required, 889 * not just conservative: it runs inside an SDEI event that is deliberately 890 * never completed (completing it has firmware resume the wedged context), and 891 * a CPU_OFF from that not-yet-completed context wedges EL3 on some firmware -- 892 * a documented follow-up. Parking also matches this path's own fallback when 893 * CPU_OFF is unavailable. 894 */ 895 void __noreturn arm64_nmi_cpu_stop(struct pt_regs *regs, bool die_on_crash) 896 { 897 unsigned int cpu = smp_processor_id(); 898 bool crash = IS_ENABLED(CONFIG_KEXEC_CORE) && crash_stop; 899 900 /* 901 * Use local_daif_mask() instead of local_irq_disable() to make sure 902 * that pseudo-NMIs are disabled. The "stop" code starts with an IRQ 903 * and falls back to NMI (which might be pseudo). If the IRQ finally 904 * goes through right as we're timing out then the NMI could interrupt 905 * us. It's better to prevent the NMI and let the IRQ finish since the 906 * pt_regs will be better. 907 */ 908 local_daif_mask(); 909 910 #ifdef CONFIG_KEXEC_CORE 911 if (crash && regs) 912 crash_save_cpu(regs, cpu); 913 #endif 914 915 /* the ack a stop requester (e.g. smp_send_stop()) polls for */ 916 set_cpu_online(cpu, false); 917 918 sdei_mask_local_cpu(); 919 920 if (crash && die_on_crash) 921 __cpu_try_die(cpu); 922 923 /* just in case */ 924 cpu_park_loop(); 925 } 926 NOKPROBE_SYMBOL(arm64_nmi_cpu_stop); 927 928 /* 929 * We need to implement panic_smp_self_stop() for parallel panic() calls, so 930 * that cpu_online_mask gets correctly updated and smp_send_stop() can skip 931 * CPUs that have already stopped themselves. 932 */ 933 void __noreturn panic_smp_self_stop(void) 934 { 935 arm64_nmi_cpu_stop(NULL, false); 936 } 937 938 static void arm64_send_ipi(const cpumask_t *mask, unsigned int nr) 939 { 940 unsigned int cpu; 941 942 if (!percpu_ipi_descs) 943 __ipi_send_mask(get_ipi_desc(0, nr), mask); 944 else 945 for_each_cpu(cpu, mask) 946 __ipi_send_single(get_ipi_desc(cpu, nr), cpu); 947 } 948 949 static void arm64_backtrace_ipi(cpumask_t *mask) 950 { 951 arm64_send_ipi(mask, IPI_CPU_BACKTRACE); 952 } 953 954 void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu) 955 { 956 /* 957 * Prefer the SDEI cross-CPU NMI provider when active: firmware 958 * dispatches the event out of EL3 and reaches CPUs that have 959 * interrupts locally masked, without the per-IRQ-mask cost that 960 * pseudo-NMI pays for the same reach. The plain IPI path below 961 * can't reach such a CPU unless pseudo-NMI is enabled. 962 */ 963 if (sdei_nmi_trigger_cpumask_backtrace(mask, exclude_cpu)) 964 return; 965 966 /* 967 * NOTE: though nmi_trigger_cpumask_backtrace() has "nmi_" in the name, 968 * nothing about it truly needs to be implemented using an NMI, it's 969 * just that it's _allowed_ to work with NMIs. If ipi_should_be_nmi() 970 * returned false our backtrace attempt will just use a regular IPI. 971 */ 972 nmi_trigger_cpumask_backtrace(mask, exclude_cpu, arm64_backtrace_ipi); 973 } 974 975 #ifdef CONFIG_KGDB 976 void kgdb_roundup_cpus(void) 977 { 978 int this_cpu = raw_smp_processor_id(); 979 int cpu; 980 981 for_each_online_cpu(cpu) { 982 /* No need to roundup ourselves */ 983 if (cpu == this_cpu) 984 continue; 985 986 __ipi_send_single(get_ipi_desc(cpu, IPI_KGDB_ROUNDUP), cpu); 987 } 988 } 989 #endif 990 991 /* 992 * Main handler for inter-processor interrupts 993 */ 994 static void do_handle_IPI(int ipinr) 995 { 996 unsigned int cpu = smp_processor_id(); 997 998 if ((unsigned)ipinr < NR_IPI) 999 trace_ipi_entry(ipi_types[ipinr]); 1000 1001 switch (ipinr) { 1002 case IPI_RESCHEDULE: 1003 scheduler_ipi(); 1004 break; 1005 1006 case IPI_CALL_FUNC: 1007 generic_smp_call_function_interrupt(); 1008 break; 1009 1010 case IPI_CPU_STOP: 1011 case IPI_CPU_STOP_NMI: 1012 arm64_nmi_cpu_stop(get_irq_regs(), true); 1013 break; 1014 1015 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 1016 case IPI_TIMER: 1017 tick_receive_broadcast(); 1018 break; 1019 #endif 1020 1021 #ifdef CONFIG_IRQ_WORK 1022 case IPI_IRQ_WORK: 1023 irq_work_run(); 1024 break; 1025 #endif 1026 1027 case IPI_CPU_BACKTRACE: 1028 /* 1029 * NOTE: in some cases this _won't_ be NMI context. See the 1030 * comment in arch_trigger_cpumask_backtrace(). 1031 */ 1032 nmi_cpu_backtrace(get_irq_regs()); 1033 break; 1034 1035 case IPI_KGDB_ROUNDUP: 1036 kgdb_nmicallback(cpu, get_irq_regs()); 1037 break; 1038 1039 default: 1040 pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr); 1041 break; 1042 } 1043 1044 if ((unsigned)ipinr < NR_IPI) 1045 trace_ipi_exit(ipi_types[ipinr]); 1046 } 1047 1048 static irqreturn_t ipi_handler(int irq, void *data) 1049 { 1050 unsigned int ipi = (irq - ipi_irq_base) % nr_ipi; 1051 1052 do_handle_IPI(ipi); 1053 return IRQ_HANDLED; 1054 } 1055 1056 static void smp_cross_call(const struct cpumask *target, unsigned int ipinr) 1057 { 1058 trace_ipi_raise(target, ipi_types[ipinr]); 1059 arm64_send_ipi(target, ipinr); 1060 } 1061 1062 static bool ipi_should_be_nmi(enum ipi_msg_type ipi) 1063 { 1064 if (!system_uses_irq_prio_masking()) 1065 return false; 1066 1067 switch (ipi) { 1068 case IPI_CPU_STOP_NMI: 1069 case IPI_CPU_BACKTRACE: 1070 case IPI_KGDB_ROUNDUP: 1071 return true; 1072 default: 1073 return false; 1074 } 1075 } 1076 1077 static void ipi_setup(int cpu) 1078 { 1079 int i; 1080 1081 if (WARN_ON_ONCE(!ipi_irq_base)) 1082 return; 1083 1084 for (i = 0; i < nr_ipi; i++) { 1085 if (!percpu_ipi_descs) { 1086 if (ipi_should_be_nmi(i)) { 1087 prepare_percpu_nmi(ipi_irq_base + i); 1088 enable_percpu_nmi(ipi_irq_base + i, 0); 1089 } else { 1090 enable_percpu_irq(ipi_irq_base + i, 0); 1091 } 1092 } else { 1093 enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, i))); 1094 } 1095 } 1096 } 1097 1098 #ifdef CONFIG_HOTPLUG_CPU 1099 static void ipi_teardown(int cpu) 1100 { 1101 int i; 1102 1103 if (WARN_ON_ONCE(!ipi_irq_base)) 1104 return; 1105 1106 for (i = 0; i < nr_ipi; i++) { 1107 if (!percpu_ipi_descs) { 1108 if (ipi_should_be_nmi(i)) { 1109 disable_percpu_nmi(ipi_irq_base + i); 1110 teardown_percpu_nmi(ipi_irq_base + i); 1111 } else { 1112 disable_percpu_irq(ipi_irq_base + i); 1113 } 1114 } else { 1115 disable_irq_nosync(irq_desc_get_irq(get_ipi_desc(cpu, i))); 1116 } 1117 } 1118 } 1119 #endif 1120 1121 static void ipi_setup_sgi(int ipi) 1122 { 1123 int err, irq, cpu; 1124 1125 irq = ipi_irq_base + ipi; 1126 1127 if (ipi_should_be_nmi(ipi)) { 1128 err = request_percpu_nmi(irq, ipi_handler, "IPI", NULL, &irq_stat); 1129 WARN(err, "Could not request IRQ %d as NMI, err=%d\n", irq, err); 1130 } else { 1131 err = request_percpu_irq(irq, ipi_handler, "IPI", &irq_stat); 1132 WARN(err, "Could not request IRQ %d as IRQ, err=%d\n", irq, err); 1133 } 1134 1135 for_each_possible_cpu(cpu) 1136 get_ipi_desc(cpu, ipi) = irq_to_desc(irq); 1137 1138 irq_set_status_flags(irq, IRQ_HIDDEN); 1139 } 1140 1141 static void ipi_setup_lpi(int ipi, int ncpus) 1142 { 1143 for (int cpu = 0; cpu < ncpus; cpu++) { 1144 int err, irq; 1145 1146 irq = ipi_irq_base + (cpu * nr_ipi) + ipi; 1147 1148 err = irq_force_affinity(irq, cpumask_of(cpu)); 1149 WARN(err, "Could not force affinity IRQ %d, err=%d\n", irq, err); 1150 1151 err = request_irq(irq, ipi_handler, IRQF_NO_AUTOEN, "IPI", 1152 NULL); 1153 WARN(err, "Could not request IRQ %d, err=%d\n", irq, err); 1154 1155 irq_set_status_flags(irq, (IRQ_HIDDEN | IRQ_NO_BALANCING_MASK)); 1156 1157 get_ipi_desc(cpu, ipi) = irq_to_desc(irq); 1158 } 1159 } 1160 1161 void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus) 1162 { 1163 int i; 1164 1165 WARN_ON(n < MAX_IPI); 1166 nr_ipi = min(n, MAX_IPI); 1167 1168 percpu_ipi_descs = !!ncpus; 1169 ipi_irq_base = ipi_base; 1170 1171 for (i = 0; i < nr_ipi; i++) { 1172 if (!percpu_ipi_descs) 1173 ipi_setup_sgi(i); 1174 else 1175 ipi_setup_lpi(i, ncpus); 1176 } 1177 1178 /* Setup the boot CPU immediately */ 1179 ipi_setup(smp_processor_id()); 1180 } 1181 1182 void arch_smp_send_reschedule(int cpu) 1183 { 1184 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE); 1185 } 1186 1187 #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL 1188 void arch_send_wakeup_ipi(unsigned int cpu) 1189 { 1190 /* 1191 * We use a scheduler IPI to wake the CPU as this avoids the need for a 1192 * dedicated IPI and we can safely handle spurious scheduler IPIs. 1193 */ 1194 smp_send_reschedule(cpu); 1195 } 1196 #endif 1197 1198 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 1199 void tick_broadcast(const struct cpumask *mask) 1200 { 1201 smp_cross_call(mask, IPI_TIMER); 1202 } 1203 #endif 1204 1205 /* 1206 * The number of CPUs online, not counting this CPU (which may not be 1207 * fully online and so not counted in num_online_cpus()). 1208 */ 1209 static inline unsigned int num_other_online_cpus(void) 1210 { 1211 unsigned int this_cpu_online = cpu_online(smp_processor_id()); 1212 1213 return num_online_cpus() - this_cpu_online; 1214 } 1215 1216 void smp_send_stop(void) 1217 { 1218 static unsigned long stop_in_progress; 1219 static cpumask_t mask; 1220 unsigned long timeout; 1221 1222 /* 1223 * If this cpu is the only one alive at this point in time, online or 1224 * not, there are no stop messages to be sent around, so just back out. 1225 */ 1226 if (num_other_online_cpus() == 0) 1227 goto skip_ipi; 1228 1229 /* Only proceed if this is the first CPU to reach this code */ 1230 if (test_and_set_bit(0, &stop_in_progress)) 1231 return; 1232 1233 /* 1234 * Send an IPI to all currently online CPUs except the CPU running 1235 * this code. 1236 * 1237 * NOTE: we don't do anything here to prevent other CPUs from coming 1238 * online after we snapshot `cpu_online_mask`. Ideally, the calling code 1239 * should do something to prevent other CPUs from coming up. This code 1240 * can be called in the panic path and thus it doesn't seem wise to 1241 * grab the CPU hotplug mutex ourselves. Worst case: 1242 * - If a CPU comes online as we're running, we'll likely notice it 1243 * during the 1 second wait below and then we'll catch it when we try 1244 * with an NMI (assuming NMIs are enabled) since we re-snapshot the 1245 * mask before sending an NMI. 1246 * - If we leave the function and see that CPUs are still online we'll 1247 * at least print a warning. Especially without NMIs this function 1248 * isn't foolproof anyway so calling code will just have to accept 1249 * the fact that there could be cases where a CPU can't be stopped. 1250 */ 1251 cpumask_copy(&mask, cpu_online_mask); 1252 cpumask_clear_cpu(smp_processor_id(), &mask); 1253 1254 if (system_state <= SYSTEM_RUNNING) 1255 pr_crit("SMP: stopping secondary CPUs\n"); 1256 1257 /* 1258 * Start with a normal IPI and wait up to one second for other CPUs to 1259 * stop. We do this first because it gives other processors a chance 1260 * to exit critical sections / drop locks and makes the rest of the 1261 * stop process (especially console flush) more robust. 1262 */ 1263 smp_cross_call(&mask, IPI_CPU_STOP); 1264 timeout = USEC_PER_SEC; 1265 while (num_other_online_cpus() && timeout--) 1266 udelay(1); 1267 1268 /* 1269 * If CPUs are still online, try an NMI. There's no excuse for this to 1270 * be slow, so we only give them an extra 10 ms to respond. 1271 */ 1272 if (num_other_online_cpus() && ipi_should_be_nmi(IPI_CPU_STOP_NMI)) { 1273 smp_rmb(); 1274 cpumask_copy(&mask, cpu_online_mask); 1275 cpumask_clear_cpu(smp_processor_id(), &mask); 1276 1277 pr_info("SMP: retry stop with NMI for CPUs %*pbl\n", 1278 cpumask_pr_args(&mask)); 1279 1280 smp_cross_call(&mask, IPI_CPU_STOP_NMI); 1281 timeout = USEC_PER_MSEC * 10; 1282 while (num_other_online_cpus() && timeout--) 1283 udelay(1); 1284 } 1285 1286 /* 1287 * If CPUs are *still* online, try the SDEI cross-CPU NMI. Firmware 1288 * delivers it regardless of the target's DAIF state, so it reaches 1289 * a CPU spinning with interrupts masked, which neither rung above 1290 * could (without pseudo-NMI there is no NMI rung at all). Allow 1291 * 100ms: a firmware round-trip per CPU, with headroom. 1292 */ 1293 if (num_other_online_cpus() && sdei_nmi_active()) { 1294 /* re-snapshot after the rungs above took CPUs offline */ 1295 smp_rmb(); 1296 cpumask_copy(&mask, cpu_online_mask); 1297 cpumask_clear_cpu(smp_processor_id(), &mask); 1298 1299 pr_info("SMP: retry stop with SDEI NMI for CPUs %*pbl\n", 1300 cpumask_pr_args(&mask)); 1301 1302 sdei_nmi_stop_cpus(&mask); 1303 timeout = USEC_PER_MSEC * 100; 1304 while (num_other_online_cpus() && timeout--) 1305 udelay(1); 1306 } 1307 1308 if (num_other_online_cpus()) { 1309 smp_rmb(); 1310 cpumask_copy(&mask, cpu_online_mask); 1311 cpumask_clear_cpu(smp_processor_id(), &mask); 1312 1313 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n", 1314 cpumask_pr_args(&mask)); 1315 } 1316 1317 skip_ipi: 1318 sdei_mask_local_cpu(); 1319 } 1320 1321 #ifdef CONFIG_KEXEC_CORE 1322 void crash_smp_send_stop(void) 1323 { 1324 /* 1325 * This function can be called twice in panic path, but obviously 1326 * we execute this only once. 1327 * 1328 * We use this same boolean to tell whether the IPI we send was a 1329 * stop or a "crash stop". 1330 */ 1331 if (crash_stop) 1332 return; 1333 crash_stop = 1; 1334 1335 smp_send_stop(); 1336 1337 sdei_handler_abort(); 1338 } 1339 1340 bool smp_crash_stop_failed(void) 1341 { 1342 return num_other_online_cpus() != 0; 1343 } 1344 #endif 1345 1346 static bool have_cpu_die(void) 1347 { 1348 #ifdef CONFIG_HOTPLUG_CPU 1349 int any_cpu = raw_smp_processor_id(); 1350 const struct cpu_operations *ops = get_cpu_ops(any_cpu); 1351 1352 if (ops && ops->cpu_die) 1353 return true; 1354 #endif 1355 return false; 1356 } 1357 1358 bool cpus_are_stuck_in_kernel(void) 1359 { 1360 bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die()); 1361 1362 return !!cpus_stuck_in_kernel || smp_spin_tables || 1363 is_protected_kvm_enabled(); 1364 } 1365