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