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