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