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