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