xref: /linux/arch/arm64/kernel/smp.c (revision 03c11eb3b16dc0058589751dfd91f254be2be613)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
208e875c1SCatalin Marinas /*
308e875c1SCatalin Marinas  * SMP initialisation and IPI support
408e875c1SCatalin Marinas  * Based on arch/arm/kernel/smp.c
508e875c1SCatalin Marinas  *
608e875c1SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
708e875c1SCatalin Marinas  */
808e875c1SCatalin Marinas 
90f078336SLorenzo Pieralisi #include <linux/acpi.h>
10f5df2696SJames Morse #include <linux/arm_sdei.h>
1108e875c1SCatalin Marinas #include <linux/delay.h>
1208e875c1SCatalin Marinas #include <linux/init.h>
1308e875c1SCatalin Marinas #include <linux/spinlock.h>
1468e21be2SIngo Molnar #include <linux/sched/mm.h>
15ef8bd77fSIngo Molnar #include <linux/sched/hotplug.h>
1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
1708e875c1SCatalin Marinas #include <linux/interrupt.h>
1808e875c1SCatalin Marinas #include <linux/cache.h>
1908e875c1SCatalin Marinas #include <linux/profile.h>
2008e875c1SCatalin Marinas #include <linux/errno.h>
2108e875c1SCatalin Marinas #include <linux/mm.h>
2208e875c1SCatalin Marinas #include <linux/err.h>
2308e875c1SCatalin Marinas #include <linux/cpu.h>
2408e875c1SCatalin Marinas #include <linux/smp.h>
2508e875c1SCatalin Marinas #include <linux/seq_file.h>
2608e875c1SCatalin Marinas #include <linux/irq.h>
27e7932188SJulien Thierry #include <linux/irqchip/arm-gic-v3.h>
2808e875c1SCatalin Marinas #include <linux/percpu.h>
2908e875c1SCatalin Marinas #include <linux/clockchips.h>
3008e875c1SCatalin Marinas #include <linux/completion.h>
3108e875c1SCatalin Marinas #include <linux/of.h>
32eb631bb5SLarry Bassel #include <linux/irq_work.h>
33a2638815SMarc Zyngier #include <linux/kernel_stat.h>
3478fd584cSAKASHI Takahiro #include <linux/kexec.h>
352f5cd0c7SDouglas Anderson #include <linux/kgdb.h>
360492747cSSebastian Andrzej Siewior #include <linux/kvm_host.h>
37331a1b3aSDouglas Anderson #include <linux/nmi.h>
3808e875c1SCatalin Marinas 
39e039ee4eSAndre Przywara #include <asm/alternative.h>
4008e875c1SCatalin Marinas #include <asm/atomic.h>
4108e875c1SCatalin Marinas #include <asm/cacheflush.h>
42df857416SMark Rutland #include <asm/cpu.h>
4308e875c1SCatalin Marinas #include <asm/cputype.h>
44cd1aebf5SMark Rutland #include <asm/cpu_ops.h>
450fbeb318SJames Morse #include <asm/daifflags.h>
460492747cSSebastian Andrzej Siewior #include <asm/kvm_mmu.h>
4708e875c1SCatalin Marinas #include <asm/mmu_context.h>
481a2db300SGanapatrao Kulkarni #include <asm/numa.h>
4908e875c1SCatalin Marinas #include <asm/processor.h>
504c7aa002SJavi Merino #include <asm/smp_plat.h>
5108e875c1SCatalin Marinas #include <asm/sections.h>
5208e875c1SCatalin Marinas #include <asm/tlbflush.h>
5308e875c1SCatalin Marinas #include <asm/ptrace.h>
54377bcff9SJonas Rabenstein #include <asm/virt.h>
5508e875c1SCatalin Marinas 
5645ed695aSNicolas Pitre #include <trace/events/ipi.h>
5745ed695aSNicolas Pitre 
5857c82954SMark Rutland /*
5957c82954SMark Rutland  * as from 2.5, kernels no longer have an init_tasks structure
6057c82954SMark Rutland  * so we need some other way of telling a new secondary core
6108e875c1SCatalin Marinas  * where to place its SVC stack
6208e875c1SCatalin Marinas  */
6308e875c1SCatalin Marinas struct secondary_data secondary_data;
6408e875c1SCatalin Marinas /* Number of CPUs which aren't online, but looping in kernel text. */
6508e875c1SCatalin Marinas static int cpus_stuck_in_kernel;
6608e875c1SCatalin Marinas 
67bb905274SSuzuki K Poulose static int ipi_irq_base __ro_after_init;
682eaf63baSZou Wei static int nr_ipi __ro_after_init = NR_IPI;
6908e875c1SCatalin Marinas 
7008e875c1SCatalin Marinas struct ipi_descs {
7108e875c1SCatalin Marinas 	struct irq_desc *descs[MAX_IPI];
7208e875c1SCatalin Marinas };
7308e875c1SCatalin Marinas 
7478fd584cSAKASHI Takahiro static DEFINE_PER_CPU_READ_MOSTLY(struct ipi_descs, pcpu_ipi_desc);
751f85008eSLorenzo Pieralisi 
76eb631bb5SLarry Bassel #define get_ipi_desc(__cpu, __ipi) (per_cpu_ptr(&pcpu_ipi_desc, __cpu)->descs[__ipi])
77331a1b3aSDouglas Anderson 
78331a1b3aSDouglas Anderson static bool percpu_ipi_descs __ro_after_init;
79331a1b3aSDouglas Anderson 
80331a1b3aSDouglas Anderson static bool crash_stop;
81331a1b3aSDouglas Anderson 
82331a1b3aSDouglas Anderson static void ipi_setup(int cpu);
832f5cd0c7SDouglas Anderson 
84331a1b3aSDouglas Anderson #ifdef CONFIG_HOTPLUG_CPU
8508e875c1SCatalin Marinas static void ipi_teardown(int cpu);
8608e875c1SCatalin Marinas static int op_cpu_kill(unsigned int cpu);
8762817d5bSDouglas Anderson #else
op_cpu_kill(unsigned int cpu)8862817d5bSDouglas Anderson static inline int op_cpu_kill(unsigned int cpu)
8962817d5bSDouglas Anderson {
90d3afc7f1SMarc Zyngier 	return -ENOSYS;
91d3afc7f1SMarc Zyngier }
92d3afc7f1SMarc Zyngier #endif
93bb905274SSuzuki K Poulose 
949d9edb96SYueHaibing 
95bb905274SSuzuki K Poulose /*
96bb905274SSuzuki K Poulose  * Boot a secondary CPU, and assign it the specified idle task.
97bb905274SSuzuki K Poulose  * This also gives us the initial stack to use for this CPU.
98bb905274SSuzuki K Poulose  */
boot_secondary(unsigned int cpu,struct task_struct * idle)99bb905274SSuzuki K Poulose static int boot_secondary(unsigned int cpu, struct task_struct *idle)
100bb905274SSuzuki K Poulose {
101bb905274SSuzuki K Poulose 	const struct cpu_operations *ops = get_cpu_ops(cpu);
102bb905274SSuzuki K Poulose 
103bb905274SSuzuki K Poulose 	if (ops->cpu_boot)
10408e875c1SCatalin Marinas 		return ops->cpu_boot(cpu);
10508e875c1SCatalin Marinas 
10608e875c1SCatalin Marinas 	return -EOPNOTSUPP;
10708e875c1SCatalin Marinas }
108b8c6453aSPaul Gortmaker 
10908e875c1SCatalin Marinas static DECLARE_COMPLETION(cpu_running);
110de58ed5eSGavin Shan 
__cpu_up(unsigned int cpu,struct task_struct * idle)111de58ed5eSGavin Shan int __cpu_up(unsigned int cpu, struct task_struct *idle)
112de58ed5eSGavin Shan {
113de58ed5eSGavin Shan 	int ret;
11408e875c1SCatalin Marinas 	long status;
115652af899SMark Rutland 
11608e875c1SCatalin Marinas 	/*
11708e875c1SCatalin Marinas 	 * We need to tell the secondary core where to find its stack and the
11808e875c1SCatalin Marinas 	 * page tables.
11908e875c1SCatalin Marinas 	 */
120b8c6453aSPaul Gortmaker 	secondary_data.task = idle;
12108e875c1SCatalin Marinas 	update_cpu_boot_status(CPU_MMU_OFF);
12208e875c1SCatalin Marinas 
123bb905274SSuzuki K Poulose 	/* Now bring the CPU into our world */
12408e875c1SCatalin Marinas 	ret = boot_secondary(cpu, idle);
12508e875c1SCatalin Marinas 	if (ret) {
12608e875c1SCatalin Marinas 		if (ret != -EPERM)
12708e875c1SCatalin Marinas 			pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
12808e875c1SCatalin Marinas 		return ret;
129c02433ddSMark Rutland 	}
130bb905274SSuzuki K Poulose 
13108e875c1SCatalin Marinas 	/*
132d22b115cSGavin Shan 	 * CPU was successfully started, wait for it to come online or
13308e875c1SCatalin Marinas 	 * time out.
134d22b115cSGavin Shan 	 */
135d22b115cSGavin Shan 	wait_for_completion_timeout(&cpu_running,
136d22b115cSGavin Shan 				    msecs_to_jiffies(5000));
137d22b115cSGavin Shan 	if (cpu_online(cpu))
138d22b115cSGavin Shan 		return 0;
13908e875c1SCatalin Marinas 
14008e875c1SCatalin Marinas 	pr_crit("CPU%u: failed to come online\n", cpu);
14108e875c1SCatalin Marinas 	secondary_data.task = NULL;
14208e875c1SCatalin Marinas 	status = READ_ONCE(secondary_data.status);
14308e875c1SCatalin Marinas 	if (status == CPU_MMU_OFF)
1440e164555SWill Deacon 		status = READ_ONCE(__early_cpu_boot_status);
145d22b115cSGavin Shan 
146d22b115cSGavin Shan 	switch (status & CPU_BOOT_STATUS_MASK) {
14708e875c1SCatalin Marinas 	default:
14808e875c1SCatalin Marinas 		pr_err("CPU%u: failed in unknown state : 0x%lx\n",
149c02433ddSMark Rutland 		       cpu, status);
150bb905274SSuzuki K Poulose 		cpus_stuck_in_kernel++;
151bb905274SSuzuki K Poulose 		break;
152bb905274SSuzuki K Poulose 	case CPU_KILL_ME:
153bb905274SSuzuki K Poulose 		if (!op_cpu_kill(cpu)) {
15466f16a24SWill Deacon 			pr_crit("CPU%u: died during early boot\n", cpu);
155bb905274SSuzuki K Poulose 			break;
156bb905274SSuzuki K Poulose 		}
157bb905274SSuzuki K Poulose 		pr_crit("CPU%u: may not have shut down cleanly\n", cpu);
158ebef7465SWill Deacon 		fallthrough;
159bb905274SSuzuki K Poulose 	case CPU_STUCK_IN_KERNEL:
160bb905274SSuzuki K Poulose 		pr_crit("CPU%u: is stuck in kernel\n", cpu);
161bb905274SSuzuki K Poulose 		if (status & CPU_STUCK_REASON_52_BIT_VA)
162bb905274SSuzuki K Poulose 			pr_crit("CPU%u: does not support 52-bit VAs\n", cpu);
163bb905274SSuzuki K Poulose 		if (status & CPU_STUCK_REASON_NO_GRAN) {
164bb905274SSuzuki K Poulose 			pr_crit("CPU%u: does not support %luK granule\n",
165bb905274SSuzuki K Poulose 				cpu, PAGE_SIZE / SZ_1K);
166df561f66SGustavo A. R. Silva 		}
167bb905274SSuzuki K Poulose 		cpus_stuck_in_kernel++;
168bb905274SSuzuki K Poulose 		break;
16966f16a24SWill Deacon 	case CPU_PANIC_KERNEL:
17066f16a24SWill Deacon 		panic("CPU%u detected unsupported configuration\n", cpu);
171d22b115cSGavin Shan 	}
172d22b115cSGavin Shan 
173d22b115cSGavin Shan 	return -EIO;
174d22b115cSGavin Shan }
175bb905274SSuzuki K Poulose 
init_gic_priority_masking(void)176bb905274SSuzuki K Poulose static void init_gic_priority_masking(void)
177bb905274SSuzuki K Poulose {
178bb905274SSuzuki K Poulose 	u32 cpuflags;
179bb905274SSuzuki K Poulose 
18008e875c1SCatalin Marinas 	if (WARN_ON(!gic_enable_sre()))
181ba051f09SNobuhiro Iwamatsu 		return;
18208e875c1SCatalin Marinas 
18308e875c1SCatalin Marinas 	cpuflags = read_sysreg(daif);
184e7932188SJulien Thierry 
185e7932188SJulien Thierry 	WARN_ON(!(cpuflags & PSR_I_BIT));
186e7932188SJulien Thierry 	WARN_ON(!(cpuflags & PSR_F_BIT));
187e7932188SJulien Thierry 
188e7932188SJulien Thierry 	gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
189e7932188SJulien Thierry }
190e7932188SJulien Thierry 
191e7932188SJulien Thierry /*
192e7932188SJulien Thierry  * This is the secondary CPU boot entry.  We're using this CPUs
193e7932188SJulien Thierry  * idle thread stack, but a set of temporary page tables.
194f0098155SHector Martin  */
secondary_start_kernel(void)195e7932188SJulien Thierry asmlinkage notrace void secondary_start_kernel(void)
196bd82d4bdSJulien Thierry {
197e7932188SJulien Thierry 	u64 mpidr = read_cpuid_mpidr() & MPIDR_HWID_BITMASK;
198e7932188SJulien Thierry 	struct mm_struct *mm = &init_mm;
19908e875c1SCatalin Marinas 	const struct cpu_operations *ops;
20008e875c1SCatalin Marinas 	unsigned int cpu = smp_processor_id();
20108e875c1SCatalin Marinas 
20208e875c1SCatalin Marinas 	/*
203b154886fSZhizhou Zhang 	 * All kernel threads share the same mm context; grab a
20408e875c1SCatalin Marinas 	 * reference and switch to it.
205ccaac162SMark Rutland 	 */
20608e875c1SCatalin Marinas 	mmgrab(mm);
207de58ed5eSGavin Shan 	current->active_mm = mm;
2083d8c1a01SMark Rutland 
20908e875c1SCatalin Marinas 	/*
21008e875c1SCatalin Marinas 	 * TTBR0 is only used for the identity mapping at this stage. Make it
21108e875c1SCatalin Marinas 	 * point to zero page to avoid speculatively fetching new entries.
21208e875c1SCatalin Marinas 	 */
21308e875c1SCatalin Marinas 	cpu_uninstall_idmap();
214f1f10076SVegard Nossum 
21508e875c1SCatalin Marinas 	if (system_uses_irq_prio_masking())
21608e875c1SCatalin Marinas 		init_gic_priority_masking();
21708e875c1SCatalin Marinas 
21808e875c1SCatalin Marinas 	rcutree_report_cpu_starting(cpu);
21908e875c1SCatalin Marinas 	trace_hardirqs_off();
22008e875c1SCatalin Marinas 
2219e8e865bSMark Rutland 	/*
22208e875c1SCatalin Marinas 	 * If the system has established the capabilities, make sure
223e7932188SJulien Thierry 	 * this CPU ticks all of those. If it doesn't, the CPU will
224e7932188SJulien Thierry 	 * fail to come online.
225e7932188SJulien Thierry 	 */
226448e9f34SFrederic Weisbecker 	check_local_cpu_capabilities();
22708e875c1SCatalin Marinas 
22808e875c1SCatalin Marinas 	ops = get_cpu_ops(cpu);
229dbb4e152SSuzuki K. Poulose 	if (ops->cpu_postboot)
230dbb4e152SSuzuki K. Poulose 		ops->cpu_postboot();
231dbb4e152SSuzuki K. Poulose 
232dbb4e152SSuzuki K. Poulose 	/*
233dbb4e152SSuzuki K. Poulose 	 * Log the CPU info before it is marked online and might get read.
234c47a1900SSuzuki K Poulose 	 */
235dbb4e152SSuzuki K. Poulose 	cpuinfo_store_cpu();
236de58ed5eSGavin Shan 	store_cpu_topology(cpu);
237de58ed5eSGavin Shan 
238de58ed5eSGavin Shan 	/*
23908e875c1SCatalin Marinas 	 * Enable GIC and timers.
24008e875c1SCatalin Marinas 	 */
241df857416SMark Rutland 	notify_cpu_starting(cpu);
242df857416SMark Rutland 
243df857416SMark Rutland 	ipi_setup(cpu);
2445524cbb1SPhil Auld 
245df857416SMark Rutland 	numa_add_cpu(cpu);
246df857416SMark Rutland 
2477ade67b5SMarc Zyngier 	/*
2487ade67b5SMarc Zyngier 	 * OK, now it's safe to let the boot CPU continue.  Wait for
2497ade67b5SMarc Zyngier 	 * the CPU migration code to notice that the CPU is online
2507ade67b5SMarc Zyngier 	 * before we continue.
251d3afc7f1SMarc Zyngier 	 */
252d3afc7f1SMarc Zyngier 	pr_info("CPU%u: Booted secondary processor 0x%010lx [0x%08x]\n",
25397fd6016SSudeep Holla 					 cpu, (unsigned long)mpidr,
254f6e763b9SMark Brown 					 read_cpuid_id());
2557ade67b5SMarc Zyngier 	update_cpu_boot_status(CPU_BOOT_SUCCESS);
25608e875c1SCatalin Marinas 	set_cpu_online(cpu, true);
25708e875c1SCatalin Marinas 	complete(&cpu_running);
25808e875c1SCatalin Marinas 
25908e875c1SCatalin Marinas 	/*
260ccaac162SMark Rutland 	 * Secondary CPUs enter the kernel with all DAIF exceptions masked.
261ccaac162SMark Rutland 	 *
262ccaac162SMark Rutland 	 * As with setup_arch() we must unmask Debug and SError exceptions, and
263bb905274SSuzuki K Poulose 	 * as the root irqchip has already been detected and initialized we can
26408e875c1SCatalin Marinas 	 * unmask IRQ and FIQ at the same time.
265b3770b32SWill Deacon 	 */
26608e875c1SCatalin Marinas 	local_daif_restore(DAIF_PROCCTX);
26741bd5b5dSJames Morse 
26853ae3acdSCatalin Marinas 	/*
26953ae3acdSCatalin Marinas 	 * OK, it's off to the idle thread for us
27008e875c1SCatalin Marinas 	 */
27108e875c1SCatalin Marinas 	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
272fc6d73d6SThomas Gleixner }
27308e875c1SCatalin Marinas 
27408e875c1SCatalin Marinas #ifdef CONFIG_HOTPLUG_CPU
op_cpu_disable(unsigned int cpu)2759327e2c6SMark Rutland static int op_cpu_disable(unsigned int cpu)
2769327e2c6SMark Rutland {
2779327e2c6SMark Rutland 	const struct cpu_operations *ops = get_cpu_ops(cpu);
278de58ed5eSGavin Shan 
279de58ed5eSGavin Shan 	/*
2809327e2c6SMark Rutland 	 * If we don't have a cpu_die method, abort before we reach the point
2819327e2c6SMark Rutland 	 * of no return. CPU0 may not have an cpu_ops, so test for it.
2829327e2c6SMark Rutland 	 */
2839327e2c6SMark Rutland 	if (!ops || !ops->cpu_die)
284de58ed5eSGavin Shan 		return -EOPNOTSUPP;
2859327e2c6SMark Rutland 
2869327e2c6SMark Rutland 	/*
2879327e2c6SMark Rutland 	 * We may need to abort a hot unplug for some other mechanism-specific
2889327e2c6SMark Rutland 	 * reason.
2899327e2c6SMark Rutland 	 */
2909327e2c6SMark Rutland 	if (ops->cpu_disable)
291de58ed5eSGavin Shan 		return ops->cpu_disable(cpu);
292de58ed5eSGavin Shan 
2939327e2c6SMark Rutland 	return 0;
2949327e2c6SMark Rutland }
2959327e2c6SMark Rutland 
2969327e2c6SMark Rutland /*
2979327e2c6SMark Rutland  * __cpu_disable runs on the processor to be shutdown.
2989327e2c6SMark Rutland  */
__cpu_disable(void)2999327e2c6SMark Rutland int __cpu_disable(void)
3009327e2c6SMark Rutland {
3019327e2c6SMark Rutland 	unsigned int cpu = smp_processor_id();
3029327e2c6SMark Rutland 	int ret;
3039327e2c6SMark Rutland 
3049327e2c6SMark Rutland 	ret = op_cpu_disable(cpu);
3059327e2c6SMark Rutland 	if (ret)
3069327e2c6SMark Rutland 		return ret;
3079327e2c6SMark Rutland 
3089327e2c6SMark Rutland 	remove_cpu_topology(cpu);
3097f9545aaSSudeep Holla 	numa_remove_cpu(cpu);
3107f9545aaSSudeep Holla 
3117f9545aaSSudeep Holla 	/*
3129327e2c6SMark Rutland 	 * Take this CPU offline.  Once we clear this, we can't return,
3139327e2c6SMark Rutland 	 * and we must not schedule until we're ready to give up the cpu.
3149327e2c6SMark Rutland 	 */
3159327e2c6SMark Rutland 	set_cpu_online(cpu, false);
3169327e2c6SMark Rutland 	ipi_teardown(cpu);
317d3afc7f1SMarc Zyngier 
3189327e2c6SMark Rutland 	/*
3199327e2c6SMark Rutland 	 * OK - migrate IRQs away from this CPU
3209327e2c6SMark Rutland 	 */
3219327e2c6SMark Rutland 	irq_migrate_all_off_this_cpu();
322217d453dSYang Yingliang 
323217d453dSYang Yingliang 	return 0;
3249327e2c6SMark Rutland }
3259327e2c6SMark Rutland 
op_cpu_kill(unsigned int cpu)3269327e2c6SMark Rutland static int op_cpu_kill(unsigned int cpu)
327c814ca02SAshwin Chaugule {
328c814ca02SAshwin Chaugule 	const struct cpu_operations *ops = get_cpu_ops(cpu);
329de58ed5eSGavin Shan 
330de58ed5eSGavin Shan 	/*
331c814ca02SAshwin Chaugule 	 * If we have no means of synchronising with the dying CPU, then assume
332c814ca02SAshwin Chaugule 	 * that it is really dead. We can only wait for an arbitrary length of
333c814ca02SAshwin Chaugule 	 * time and hope that it's dead, so let's skip the wait and just hope.
334c814ca02SAshwin Chaugule 	 */
335c814ca02SAshwin Chaugule 	if (!ops->cpu_kill)
336de58ed5eSGavin Shan 		return 0;
3376b99c68cSMark Rutland 
338c814ca02SAshwin Chaugule 	return ops->cpu_kill(cpu);
339de58ed5eSGavin Shan }
340c814ca02SAshwin Chaugule 
341c814ca02SAshwin Chaugule /*
3429327e2c6SMark Rutland  * Called on the thread which is asking for a CPU to be shutdown after the
343b3091f17SThomas Gleixner  * shutdown completed.
344b3091f17SThomas Gleixner  */
arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)3459327e2c6SMark Rutland void arch_cpuhp_cleanup_dead_cpu(unsigned int cpu)
346b3091f17SThomas Gleixner {
3479327e2c6SMark Rutland 	int err;
3486b99c68cSMark Rutland 
3496b99c68cSMark Rutland 	pr_debug("CPU%u: shutdown\n", cpu);
350cf814bcfSLee Jones 
351c814ca02SAshwin Chaugule 	/*
352c814ca02SAshwin Chaugule 	 * Now that the dying CPU is beyond the point of no return w.r.t.
353c814ca02SAshwin Chaugule 	 * in-kernel synchronisation, try to get the firwmare to help us to
354c814ca02SAshwin Chaugule 	 * verify that it has really left the kernel before we consider
355c814ca02SAshwin Chaugule 	 * clobbering anything it might still be using.
356c814ca02SAshwin Chaugule 	 */
357c814ca02SAshwin Chaugule 	err = op_cpu_kill(cpu);
3586b99c68cSMark Rutland 	if (err)
3596b99c68cSMark Rutland 		pr_warn("CPU%d may not have shut down cleanly: %d\n", cpu, err);
360a74ec64aSKefeng Wang }
3619327e2c6SMark Rutland 
3629327e2c6SMark Rutland /*
3639327e2c6SMark Rutland  * Called from the idle thread for the CPU which has been shutdown.
3649327e2c6SMark Rutland  *
3659327e2c6SMark Rutland  */
cpu_die(void)3669327e2c6SMark Rutland void __noreturn cpu_die(void)
3679bdc61efSJosh Poimboeuf {
3689327e2c6SMark Rutland 	unsigned int cpu = smp_processor_id();
3699327e2c6SMark Rutland 	const struct cpu_operations *ops = get_cpu_ops(cpu);
370de58ed5eSGavin Shan 
3719327e2c6SMark Rutland 	idle_task_exit();
3729327e2c6SMark Rutland 
3739327e2c6SMark Rutland 	local_daif_mask();
3740fbeb318SJames Morse 
3759327e2c6SMark Rutland 	/* Tell cpuhp_bp_sync_dead() that this CPU is now safe to dispose of */
376b3091f17SThomas Gleixner 	cpuhp_ap_report_dead();
377b3091f17SThomas Gleixner 
3789327e2c6SMark Rutland 	/*
3799327e2c6SMark Rutland 	 * Actually shutdown the CPU. This must never fail. The specific hotplug
3809327e2c6SMark Rutland 	 * mechanism must perform all required cache maintenance to ensure that
3819327e2c6SMark Rutland 	 * no dirty lines are lost in the process of shutting down the CPU.
3829327e2c6SMark Rutland 	 */
3839327e2c6SMark Rutland 	ops->cpu_die(cpu);
384de58ed5eSGavin Shan 
3859327e2c6SMark Rutland 	BUG();
3869327e2c6SMark Rutland }
3879327e2c6SMark Rutland #endif
3889327e2c6SMark Rutland 
__cpu_try_die(int cpu)3899327e2c6SMark Rutland static void __cpu_try_die(int cpu)
390de58ed5eSGavin Shan {
391de58ed5eSGavin Shan #ifdef CONFIG_HOTPLUG_CPU
392de58ed5eSGavin Shan 	const struct cpu_operations *ops = get_cpu_ops(cpu);
393de58ed5eSGavin Shan 
394de58ed5eSGavin Shan 	if (ops && ops->cpu_die)
395de58ed5eSGavin Shan 		ops->cpu_die(cpu);
396de58ed5eSGavin Shan #endif
397de58ed5eSGavin Shan }
398de58ed5eSGavin Shan 
399de58ed5eSGavin Shan /*
400fce6361fSSuzuki K Poulose  * Kill the calling secondary CPU, early in bringup before it is turned
401fce6361fSSuzuki K Poulose  * online.
402fce6361fSSuzuki K Poulose  */
cpu_die_early(void)403fce6361fSSuzuki K Poulose void __noreturn cpu_die_early(void)
4045ab6876cSJosh Poimboeuf {
405fce6361fSSuzuki K Poulose 	int cpu = smp_processor_id();
406fce6361fSSuzuki K Poulose 
407fce6361fSSuzuki K Poulose 	pr_crit("CPU%d: will not boot\n", cpu);
408fce6361fSSuzuki K Poulose 
409fce6361fSSuzuki K Poulose 	/* Mark this CPU absent */
410fce6361fSSuzuki K Poulose 	set_cpu_present(cpu, 0);
411fce6361fSSuzuki K Poulose 	rcutree_report_cpu_dead();
412448e9f34SFrederic Weisbecker 
413fce6361fSSuzuki K Poulose 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU)) {
414de58ed5eSGavin Shan 		update_cpu_boot_status(CPU_KILL_ME);
415bb905274SSuzuki K Poulose 		__cpu_try_die(cpu);
416de58ed5eSGavin Shan 	}
417de58ed5eSGavin Shan 
418de58ed5eSGavin Shan 	update_cpu_boot_status(CPU_STUCK_IN_KERNEL);
419bb905274SSuzuki K Poulose 
420fce6361fSSuzuki K Poulose 	cpu_park_loop();
421fce6361fSSuzuki K Poulose }
422fce6361fSSuzuki K Poulose 
hyp_mode_check(void)423fce6361fSSuzuki K Poulose static void __init hyp_mode_check(void)
424377bcff9SJonas Rabenstein {
425377bcff9SJonas Rabenstein 	if (is_hyp_mode_available())
426377bcff9SJonas Rabenstein 		pr_info("CPU: All CPU(s) started at EL2\n");
427377bcff9SJonas Rabenstein 	else if (is_hyp_mode_mismatched())
428377bcff9SJonas Rabenstein 		WARN_TAINT(1, TAINT_CPU_OUT_OF_SPEC,
429377bcff9SJonas Rabenstein 			   "CPU: CPUs started in inconsistent modes");
430377bcff9SJonas Rabenstein 	else
431377bcff9SJonas Rabenstein 		pr_info("CPU: All CPU(s) started at EL1\n");
432377bcff9SJonas Rabenstein 	if (IS_ENABLED(CONFIG_KVM) && !is_kernel_in_hyp_mode()) {
4336ec6259dSDavid Brazdil 		kvm_compute_layout();
4340492747cSSebastian Andrzej Siewior 		kvm_apply_hyp_relocations();
4356ec6259dSDavid Brazdil 	}
4366ec6259dSDavid Brazdil }
437377bcff9SJonas Rabenstein 
smp_cpus_done(unsigned int max_cpus)438377bcff9SJonas Rabenstein void __init smp_cpus_done(unsigned int max_cpus)
43908e875c1SCatalin Marinas {
44008e875c1SCatalin Marinas 	pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
441326b16dbSWill Deacon 	hyp_mode_check();
442377bcff9SJonas Rabenstein 	setup_system_features();
44363a2d92eSMark Rutland 	setup_user_features();
444075f48c9SMark Rutland 	mark_linear_text_alias_ro();
4455ea5306cSArd Biesheuvel }
44608e875c1SCatalin Marinas 
smp_prepare_boot_cpu(void)44708e875c1SCatalin Marinas void __init smp_prepare_boot_cpu(void)
44808e875c1SCatalin Marinas {
44908e875c1SCatalin Marinas 	/*
4503d8c1a01SMark Rutland 	 * The runtime per-cpu areas have been allocated by
4513d8c1a01SMark Rutland 	 * setup_per_cpu_areas(), and CPU0's boot time per-cpu area will be
4523d8c1a01SMark Rutland 	 * freed shortly, so we must move over to the runtime per-cpu area.
4533d8c1a01SMark Rutland 	 */
4543d8c1a01SMark Rutland 	set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
4559113c2aaSSuzuki K Poulose 
4560ceb0d56SDaniel Thompson 	cpuinfo_store_boot_cpu();
457*eb15d707SMark Rutland 	setup_boot_cpu_features();
458*eb15d707SMark Rutland 
459e7932188SJulien Thierry 	/* Conditionally switch to GIC PMR for interrupt masking */
460e7932188SJulien Thierry 	if (system_uses_irq_prio_masking())
461e7932188SJulien Thierry 		init_gic_priority_masking();
462e7932188SJulien Thierry 
4632e903b91SAndrey Konovalov 	kasan_init_hw_tags();
4642e903b91SAndrey Konovalov 	/* Init percpu seeds for random tags after cpus are set up. */
46508e875c1SCatalin Marinas 	kasan_init_sw_tags();
46608e875c1SCatalin Marinas }
4670f078336SLorenzo Pieralisi 
4680f078336SLorenzo Pieralisi /*
4690f078336SLorenzo Pieralisi  * Duplicate MPIDRs are a recipe for disaster. Scan all initialized
4700f078336SLorenzo Pieralisi  * entries and check for duplicates. If any is found just ignore the
4710f078336SLorenzo Pieralisi  * cpu. cpu_logical_map was initialized to INVALID_HWID to avoid
4720f078336SLorenzo Pieralisi  * matching valid MPIDR values.
4730f078336SLorenzo Pieralisi  */
is_mpidr_duplicate(unsigned int cpu,u64 hwid)4740f078336SLorenzo Pieralisi static bool __init is_mpidr_duplicate(unsigned int cpu, u64 hwid)
4750f078336SLorenzo Pieralisi {
4760f078336SLorenzo Pieralisi 	unsigned int i;
4770f078336SLorenzo Pieralisi 
4780f078336SLorenzo Pieralisi 	for (i = 1; (i < cpu) && (i < NR_CPUS); i++)
4790f078336SLorenzo Pieralisi 		if (cpu_logical_map(i) == hwid)
4800f078336SLorenzo Pieralisi 			return true;
4810f078336SLorenzo Pieralisi 	return false;
4820f078336SLorenzo Pieralisi }
48308e875c1SCatalin Marinas 
484819a8826SLorenzo Pieralisi /*
485819a8826SLorenzo Pieralisi  * Initialize cpu operations for a logical cpu and
486819a8826SLorenzo Pieralisi  * set it in the possible mask on success
487819a8826SLorenzo Pieralisi  */
smp_cpu_setup(int cpu)488819a8826SLorenzo Pieralisi static int __init smp_cpu_setup(int cpu)
489de58ed5eSGavin Shan {
490de58ed5eSGavin Shan 	const struct cpu_operations *ops;
4916885fb12SGavin Shan 
492819a8826SLorenzo Pieralisi 	if (init_cpu_ops(cpu))
493819a8826SLorenzo Pieralisi 		return -ENODEV;
494de58ed5eSGavin Shan 
495de58ed5eSGavin Shan 	ops = get_cpu_ops(cpu);
496819a8826SLorenzo Pieralisi 	if (ops->cpu_init(cpu))
497819a8826SLorenzo Pieralisi 		return -ENODEV;
498819a8826SLorenzo Pieralisi 
499819a8826SLorenzo Pieralisi 	set_cpu_possible(cpu, true);
500819a8826SLorenzo Pieralisi 
501819a8826SLorenzo Pieralisi 	return 0;
502819a8826SLorenzo Pieralisi }
5030f078336SLorenzo Pieralisi 
5040f078336SLorenzo Pieralisi static bool bootcpu_valid __initdata;
5050f078336SLorenzo Pieralisi static unsigned int cpu_count = 1;
5060f078336SLorenzo Pieralisi 
arch_register_cpu(int cpu)507e0013aedSMark Rutland int arch_register_cpu(int cpu)
508e0013aedSMark Rutland {
509e0013aedSMark Rutland 	acpi_handle acpi_handle = acpi_get_processor_handle(cpu);
510e0013aedSMark Rutland 	struct cpu *c = &per_cpu(cpu_devices, cpu);
511e0013aedSMark Rutland 
512e0013aedSMark Rutland 	if (!acpi_disabled && !acpi_handle &&
513d3c3db41SPierre Gondois 	    IS_ENABLED(CONFIG_ACPI_HOTPLUG_CPU))
514e0013aedSMark Rutland 		return -EPROBE_DEFER;
5150f078336SLorenzo Pieralisi 
5160f078336SLorenzo Pieralisi #ifdef CONFIG_ACPI_HOTPLUG_CPU
5170f078336SLorenzo Pieralisi 	/* For now block anything that looks like physical CPU Hotplug */
5180f078336SLorenzo Pieralisi 	if (invalid_logical_cpuid(cpu) || !cpu_present(cpu)) {
5190f078336SLorenzo Pieralisi 		pr_err_once("Changing CPU present bit is not supported\n");
5200f078336SLorenzo Pieralisi 		return -ENODEV;
5210f078336SLorenzo Pieralisi 	}
5220f078336SLorenzo Pieralisi #endif
5230f078336SLorenzo Pieralisi 
5240f078336SLorenzo Pieralisi 	/*
5250f078336SLorenzo Pieralisi 	 * Availability of the acpi handle is sufficient to establish
526c54e52f8SJames Morse 	 * that _STA has aleady been checked. No need to recheck here.
527f9058929SHanjun Guo 	 */
5280f078336SLorenzo Pieralisi 	c->hotpluggable = arch_cpu_is_hotpluggable(cpu);
5290f078336SLorenzo Pieralisi 
5300f078336SLorenzo Pieralisi 	return register_cpu(c, cpu);
531f9058929SHanjun Guo }
532f9058929SHanjun Guo 
5330f078336SLorenzo Pieralisi #ifdef CONFIG_ACPI_HOTPLUG_CPU
arch_unregister_cpu(int cpu)5340f078336SLorenzo Pieralisi void arch_unregister_cpu(int cpu)
5350f078336SLorenzo Pieralisi {
5360f078336SLorenzo Pieralisi 	acpi_handle acpi_handle = acpi_get_processor_handle(cpu);
5370f078336SLorenzo Pieralisi 	struct cpu *c = &per_cpu(cpu_devices, cpu);
5380f078336SLorenzo Pieralisi 	acpi_status status;
5390f078336SLorenzo Pieralisi 	unsigned long long sta;
5400f078336SLorenzo Pieralisi 
5410f078336SLorenzo Pieralisi 	if (!acpi_handle) {
5420f078336SLorenzo Pieralisi 		pr_err_once("Removing a CPU without associated ACPI handle\n");
5430f078336SLorenzo Pieralisi 		return;
5440f078336SLorenzo Pieralisi 	}
5450f078336SLorenzo Pieralisi 
5460f078336SLorenzo Pieralisi 	status = acpi_evaluate_integer(acpi_handle, "_STA", NULL, &sta);
5470f078336SLorenzo Pieralisi 	if (ACPI_FAILURE(status))
5480f078336SLorenzo Pieralisi 		return;
549e0013aedSMark Rutland 
5500f078336SLorenzo Pieralisi 	/* For now do not allow anything that looks like physical CPU HP */
5510f078336SLorenzo Pieralisi 	if (cpu_present(cpu) && !(sta & ACPI_STA_DEVICE_PRESENT)) {
5520f078336SLorenzo Pieralisi 		pr_err_once("Changing CPU present bit is not supported\n");
5530f078336SLorenzo Pieralisi 		return;
5540f078336SLorenzo Pieralisi 	}
5550f078336SLorenzo Pieralisi 
5560f078336SLorenzo Pieralisi 	unregister_cpu(c);
557eaecca9eSKefeng Wang }
5580f078336SLorenzo Pieralisi #endif /* CONFIG_ACPI_HOTPLUG_CPU */
559e0013aedSMark Rutland 
560e0013aedSMark Rutland #ifdef CONFIG_ACPI
5615e89c55eSLorenzo Pieralisi static struct acpi_madt_generic_interrupt cpu_madt_gicc[NR_CPUS];
5625e89c55eSLorenzo Pieralisi 
acpi_cpu_get_madt_gicc(int cpu)5635e89c55eSLorenzo Pieralisi struct acpi_madt_generic_interrupt *acpi_cpu_get_madt_gicc(int cpu)
5645e89c55eSLorenzo Pieralisi {
5655e89c55eSLorenzo Pieralisi 	return &cpu_madt_gicc[cpu];
5665e89c55eSLorenzo Pieralisi }
5675e89c55eSLorenzo Pieralisi EXPORT_SYMBOL_GPL(acpi_cpu_get_madt_gicc);
5685e89c55eSLorenzo Pieralisi 
5695e89c55eSLorenzo Pieralisi /*
5705e89c55eSLorenzo Pieralisi  * acpi_map_gic_cpu_interface - parse processor MADT entry
5715e89c55eSLorenzo Pieralisi  *
5720f078336SLorenzo Pieralisi  * Carry out sanity checks on MADT processor entry and initialize
5730f078336SLorenzo Pieralisi  * cpu_logical_map on success
5740f078336SLorenzo Pieralisi  */
5750f078336SLorenzo Pieralisi static void __init
acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt * processor)57660574d1eSKeith Busch acpi_map_gic_cpu_interface(struct acpi_madt_generic_interrupt *processor)
5770f078336SLorenzo Pieralisi {
5780f078336SLorenzo Pieralisi 	u64 hwid = processor->arm_mpidr;
5790f078336SLorenzo Pieralisi 
5800f078336SLorenzo Pieralisi 	if (!(processor->flags &
5810f078336SLorenzo Pieralisi 	      (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE))) {
58299e3e3aeSAl Stone 		pr_debug("skipping disabled CPU entry with 0x%llx MPIDR\n", hwid);
5830f078336SLorenzo Pieralisi 		return;
5840f078336SLorenzo Pieralisi 	}
58560574d1eSKeith Busch 
5860f078336SLorenzo Pieralisi 	if (hwid & ~MPIDR_HWID_BITMASK || hwid == INVALID_HWID) {
5870f078336SLorenzo Pieralisi 		pr_err("skipping CPU entry with invalid MPIDR 0x%llx\n", hwid);
5880f078336SLorenzo Pieralisi 		return;
5890f078336SLorenzo Pieralisi 	}
5900f078336SLorenzo Pieralisi 
591e1896249SLorenzo Pieralisi 	if (is_mpidr_duplicate(cpu_count, hwid)) {
592e1896249SLorenzo Pieralisi 		pr_err("duplicate CPU MPIDR 0x%llx in MADT\n", hwid);
593e1896249SLorenzo Pieralisi 		return;
594e1896249SLorenzo Pieralisi 	}
595e1896249SLorenzo Pieralisi 
596e1896249SLorenzo Pieralisi 	/* Check if GICC structure of boot CPU is available in the MADT */
597e1896249SLorenzo Pieralisi 	if (cpu_logical_map(0) == hwid) {
598e1896249SLorenzo Pieralisi 		if (bootcpu_valid) {
599e1896249SLorenzo Pieralisi 			pr_err("duplicate boot CPU MPIDR: 0x%llx in MADT\n",
600e1896249SLorenzo Pieralisi 			       hwid);
601e1896249SLorenzo Pieralisi 			return;
602e1896249SLorenzo Pieralisi 		}
603e1896249SLorenzo Pieralisi 		bootcpu_valid = true;
604e1896249SLorenzo Pieralisi 		cpu_madt_gicc[0] = *processor;
605e1896249SLorenzo Pieralisi 		return;
606e1896249SLorenzo Pieralisi 	}
607e1896249SLorenzo Pieralisi 
608e1896249SLorenzo Pieralisi 	if (cpu_count >= NR_CPUS)
609e1896249SLorenzo Pieralisi 		return;
610e1896249SLorenzo Pieralisi 
611e1896249SLorenzo Pieralisi 	/* map the logical cpu id to cpu MPIDR */
612e1896249SLorenzo Pieralisi 	set_cpu_logical_map(cpu_count, hwid);
613e1896249SLorenzo Pieralisi 
614e1896249SLorenzo Pieralisi 	cpu_madt_gicc[cpu_count] = *processor;
615e1896249SLorenzo Pieralisi 
616e1896249SLorenzo Pieralisi 	/*
6170f078336SLorenzo Pieralisi 	 * Set-up the ACPI parking protocol cpu entries
618e1896249SLorenzo Pieralisi 	 * while initializing the cpu_logical_map to
6190f078336SLorenzo Pieralisi 	 * avoid parsing MADT entries multiple times for
6200f078336SLorenzo Pieralisi 	 * nothing (ie a valid cpu_logical_map entry should
621819a8826SLorenzo Pieralisi 	 * contain a valid parking protocol data set to
6224c7aa002SJavi Merino 	 * initialize the cpu if the parking protocol is
6234c7aa002SJavi Merino 	 * the only available enable method).
6244c7aa002SJavi Merino 	 */
62508e875c1SCatalin Marinas 	acpi_set_mailbox_entry(cpu_count, processor);
62629b8302bSJisheng Zhang 
62708e875c1SCatalin Marinas 	cpu_count++;
6283d29a9a0SDmitry Torokhov }
62908e875c1SCatalin Marinas 
630de76e70aSRob Herring static int __init
acpi_parse_gic_cpu_interface(union acpi_subtable_headers * header,const unsigned long end)6314d97b929SRob Herring acpi_parse_gic_cpu_interface(union acpi_subtable_headers *header,
6324c7aa002SJavi Merino 			     const unsigned long end)
6334d97b929SRob Herring {
6344c7aa002SJavi Merino 	struct acpi_madt_generic_interrupt *processor;
6354c7aa002SJavi Merino 
6360f078336SLorenzo Pieralisi 	processor = (struct acpi_madt_generic_interrupt *)header;
637a270f327SRob Herring 	if (BAD_MADT_GICC_ENTRY(processor, end))
638a270f327SRob Herring 		return -EINVAL;
6394c7aa002SJavi Merino 
6404c7aa002SJavi Merino 	acpi_table_print_madt_entry(&header->common);
6414c7aa002SJavi Merino 
6424c7aa002SJavi Merino 	acpi_map_gic_cpu_interface(processor);
6434c7aa002SJavi Merino 
6444c7aa002SJavi Merino 	return 0;
6454c7aa002SJavi Merino }
6464c7aa002SJavi Merino 
acpi_parse_and_init_cpus(void)6474c7aa002SJavi Merino static void __init acpi_parse_and_init_cpus(void)
6484c7aa002SJavi Merino {
6494c7aa002SJavi Merino 	int i;
650a270f327SRob Herring 
651a270f327SRob Herring 	/*
6524c7aa002SJavi Merino 	 * do a walk of MADT to determine how many CPUs
6534c7aa002SJavi Merino 	 * we have including disabled CPUs, and get information
6544c7aa002SJavi Merino 	 * we need for SMP init.
6554c7aa002SJavi Merino 	 */
6567ba5f605SZhen Lei 	acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
6574c7aa002SJavi Merino 				      acpi_parse_gic_cpu_interface, 0);
6584c7aa002SJavi Merino 
6594c7aa002SJavi Merino 	/*
6604c7aa002SJavi Merino 	 * In ACPI, SMP and CPU NUMA information is provided in separate
6614c7aa002SJavi Merino 	 * static tables, namely the MADT and the SRAT.
6624c7aa002SJavi Merino 	 *
6634c7aa002SJavi Merino 	 * Thus, it is simpler to first create the cpu logical map through
6644c7aa002SJavi Merino 	 * an MADT walk and then map the logical cpus to their node ids
6654c7aa002SJavi Merino 	 * as separate steps.
6664c7aa002SJavi Merino 	 */
6670f078336SLorenzo Pieralisi 	acpi_map_cpus_to_nodes();
66808e875c1SCatalin Marinas 
66908e875c1SCatalin Marinas 	for (i = 0; i < nr_cpu_ids; i++)
6704c7aa002SJavi Merino 		early_map_cpu_to_node(i, acpi_numa_get_nid(i));
671eaecca9eSKefeng Wang }
6721a2db300SGanapatrao Kulkarni #else
6731a2db300SGanapatrao Kulkarni #define acpi_parse_and_init_cpus(...)	do { } while (0)
67408e875c1SCatalin Marinas #endif
6750f078336SLorenzo Pieralisi 
6760f078336SLorenzo Pieralisi /*
67708e875c1SCatalin Marinas  * Enumerate the possible CPU set from the device tree and build the
67808e875c1SCatalin Marinas  * cpu logical map array containing MPIDR values related to logical
6790f078336SLorenzo Pieralisi  * cpus. Assumes that cpu_logical_map(0) has already been initialized.
6800f078336SLorenzo Pieralisi  */
of_parse_and_init_cpus(void)6810f078336SLorenzo Pieralisi static void __init of_parse_and_init_cpus(void)
6820f078336SLorenzo Pieralisi {
6830f078336SLorenzo Pieralisi 	struct device_node *dn;
6840f078336SLorenzo Pieralisi 
6850f078336SLorenzo Pieralisi 	for_each_of_cpu_node(dn) {
6860f078336SLorenzo Pieralisi 		u64 hwid = of_get_cpu_hwid(dn, 0);
6870f078336SLorenzo Pieralisi 
6880f078336SLorenzo Pieralisi 		if (hwid & ~MPIDR_HWID_BITMASK)
6890f078336SLorenzo Pieralisi 			goto next;
6900f078336SLorenzo Pieralisi 
691e1896249SLorenzo Pieralisi 		if (is_mpidr_duplicate(cpu_count, hwid)) {
6920f078336SLorenzo Pieralisi 			pr_err("%pOF: duplicate cpu reg properties in the DT\n",
69350ee91bdSKefeng Wang 				dn);
6949b130ad5SAlexey Dobriyan 			goto next;
69550ee91bdSKefeng Wang 		}
6964c7aa002SJavi Merino 
6974c7aa002SJavi Merino 		/*
6980f078336SLorenzo Pieralisi 		 * The numbering scheme requires that the boot CPU
6994c7aa002SJavi Merino 		 * must be assigned logical id 0. Record it so that
7004c7aa002SJavi Merino 		 * the logical map built from DT is validated and can
7014c7aa002SJavi Merino 		 * be used.
7024c7aa002SJavi Merino 		 */
703819a8826SLorenzo Pieralisi 		if (hwid == cpu_logical_map(0)) {
704819a8826SLorenzo Pieralisi 			if (bootcpu_valid) {
705819a8826SLorenzo Pieralisi 				pr_err("%pOF: duplicate boot cpu reg property in DT\n",
706819a8826SLorenzo Pieralisi 					dn);
707819a8826SLorenzo Pieralisi 				goto next;
7084c7aa002SJavi Merino 			}
70950ee91bdSKefeng Wang 
710819a8826SLorenzo Pieralisi 			bootcpu_valid = true;
711819a8826SLorenzo Pieralisi 			early_map_cpu_to_node(0, of_node_to_nid(dn));
712eaecca9eSKefeng Wang 
713819a8826SLorenzo Pieralisi 			/*
714819a8826SLorenzo Pieralisi 			 * cpu_logical_map has already been
71508e875c1SCatalin Marinas 			 * initialized and the boot cpu doesn't need
71608e875c1SCatalin Marinas 			 * the enable-method so continue without
71708e875c1SCatalin Marinas 			 * incrementing cpu.
71808e875c1SCatalin Marinas 			 */
719de58ed5eSGavin Shan 			continue;
720cd1aebf5SMark Rutland 		}
72144dbcc93SSuzuki K Poulose 
722c18df0adSDavid Daney 		if (cpu_count >= NR_CPUS)
72308e875c1SCatalin Marinas 			goto next;
724f6e763b9SMark Brown 
725f6e763b9SMark Brown 		pr_debug("cpu logical map 0x%llx\n", hwid);
726c18df0adSDavid Daney 		set_cpu_logical_map(cpu_count, hwid);
727c18df0adSDavid Daney 
728c18df0adSDavid Daney 		early_map_cpu_to_node(cpu_count, of_node_to_nid(dn));
72997fd6016SSudeep Holla next:
730f6e763b9SMark Brown 		cpu_count++;
73108e875c1SCatalin Marinas 	}
732e75118a7SSuzuki K Poulose }
733e75118a7SSuzuki K Poulose 
734e75118a7SSuzuki K Poulose /*
735e75118a7SSuzuki K Poulose  * Enumerate the possible CPU set from the device tree or ACPI and build the
736e75118a7SSuzuki K Poulose  * cpu logical map array containing MPIDR values related to logical
737e75118a7SSuzuki K Poulose  * cpus. Assumes that cpu_logical_map(0) has already been initialized.
738e75118a7SSuzuki K Poulose  */
smp_init_cpus(void)73908e875c1SCatalin Marinas void __init smp_init_cpus(void)
74008e875c1SCatalin Marinas {
74108e875c1SCatalin Marinas 	int i;
74208e875c1SCatalin Marinas 
74308e875c1SCatalin Marinas 	if (acpi_disabled)
74408e875c1SCatalin Marinas 		of_parse_and_init_cpus();
74557c82954SMark Rutland 	else
74657c82954SMark Rutland 		acpi_parse_and_init_cpus();
747d329de3fSMarc Zyngier 
74808e875c1SCatalin Marinas 	if (cpu_count > nr_cpu_ids)
74908e875c1SCatalin Marinas 		pr_warn("Number of cores (%d) exceeds configured maximum of %u - clipping\n",
750de58ed5eSGavin Shan 			cpu_count, nr_cpu_ids);
751de58ed5eSGavin Shan 
752d329de3fSMarc Zyngier 	if (!bootcpu_valid) {
753d329de3fSMarc Zyngier 		pr_err("missing boot CPU MPIDR, not enabling secondaries\n");
754de58ed5eSGavin Shan 		return;
755d329de3fSMarc Zyngier 	}
756d329de3fSMarc Zyngier 
75708e875c1SCatalin Marinas 	/*
75808e875c1SCatalin Marinas 	 * We need to set the cpu_logical_map entries before enabling
759c18df0adSDavid Daney 	 * the cpus so that cpu processor description entries (DT cpu nodes
76008e875c1SCatalin Marinas 	 * and ACPI MADT entries) can be retrieved by matching the cpu hwid
76108e875c1SCatalin Marinas 	 * with entries in cpu_logical_map while initializing the cpus.
76208e875c1SCatalin Marinas 	 * If the cpu set-up fails, invalidate the cpu_logical_map entry.
76345ed695aSNicolas Pitre 	 */
7640edaee42SAnshuman Khandual 	for (i = 1; i < nr_cpu_ids; i++) {
7650edaee42SAnshuman Khandual 		if (cpu_logical_map(i) != INVALID_HWID) {
7660edaee42SAnshuman Khandual 			if (smp_cpu_setup(i))
7670edaee42SAnshuman Khandual 				set_cpu_logical_map(i, INVALID_HWID);
7680edaee42SAnshuman Khandual 		}
7690edaee42SAnshuman Khandual 	}
77008e875c1SCatalin Marinas }
77108e875c1SCatalin Marinas 
smp_prepare_cpus(unsigned int max_cpus)7725cebfd2dSMarc Zyngier void __init smp_prepare_cpus(unsigned int max_cpus)
77345ed695aSNicolas Pitre {
774a2638815SMarc Zyngier 	const struct cpu_operations *ops;
775a2638815SMarc Zyngier 	int err;
776a2638815SMarc Zyngier 	unsigned int cpu;
77708e875c1SCatalin Marinas 	unsigned int this_cpu;
77808e875c1SCatalin Marinas 
77908e875c1SCatalin Marinas 	init_cpu_topology();
78008e875c1SCatalin Marinas 
78145ed695aSNicolas Pitre 	this_cpu = smp_processor_id();
78208e875c1SCatalin Marinas 	store_cpu_topology(this_cpu);
78367317c26SSudeep KarkadaNagesha 	numa_store_cpu_info(this_cpu);
7845089bc51SThomas Gleixner 	numa_add_cpu(this_cpu);
78508e875c1SCatalin Marinas 
78608e875c1SCatalin Marinas 	/*
78708e875c1SCatalin Marinas 	 * If UP is mandated by "nosmp" (which implies "maxcpus=0"), don't set
788a2638815SMarc Zyngier 	 * secondary CPUs present.
789a2638815SMarc Zyngier 	 */
79008e875c1SCatalin Marinas 	if (max_cpus == 0)
79108e875c1SCatalin Marinas 		return;
79245ed695aSNicolas Pitre 
79345ed695aSNicolas Pitre 	/*
79445ed695aSNicolas Pitre 	 * Initialise the present map (which describes the set of CPUs
79545ed695aSNicolas Pitre 	 * actually populated at the present time) and release the
79645ed695aSNicolas Pitre 	 * secondaries from the bootloader.
79745ed695aSNicolas Pitre 	 */
79845ed695aSNicolas Pitre 	for_each_possible_cpu(cpu) {
7990aaf0daeSJiang Liu 
80045ed695aSNicolas Pitre 		if (cpu == smp_processor_id())
80145ed695aSNicolas Pitre 			continue;
80245ed695aSNicolas Pitre 
80345ed695aSNicolas Pitre 		ops = get_cpu_ops(cpu);
80445ed695aSNicolas Pitre 		if (!ops)
80545ed695aSNicolas Pitre 			continue;
80645ed695aSNicolas Pitre 
80745ed695aSNicolas Pitre 		err = ops->cpu_prepare(cpu);
80845ed695aSNicolas Pitre 		if (err)
8095ab6876cSJosh Poimboeuf 			continue;
81008e875c1SCatalin Marinas 
811d914d4d4SAaro Koskinen 		set_cpu_present(cpu, true);
81208e875c1SCatalin Marinas 		numa_store_cpu_info(cpu);
8130fbeb318SJames Morse 	}
814f5df2696SJames Morse }
815dccc9da2SJayachandran C 
81608e875c1SCatalin Marinas static const char *ipi_types[MAX_IPI] __tracepoint_string = {
81708e875c1SCatalin Marinas 	[IPI_RESCHEDULE]	= "Rescheduling interrupts",
818d914d4d4SAaro Koskinen 	[IPI_CALL_FUNC]		= "Function call interrupts",
819d914d4d4SAaro Koskinen 	[IPI_CPU_STOP]		= "CPU stop interrupts",
820d914d4d4SAaro Koskinen 	[IPI_CPU_STOP_NMI]	= "CPU stop NMIs",
821d914d4d4SAaro Koskinen 	[IPI_TIMER]		= "Timer broadcast interrupts",
822d914d4d4SAaro Koskinen 	[IPI_IRQ_WORK]		= "IRQ work interrupts",
8237412a60dSJosh Poimboeuf 	[IPI_CPU_BACKTRACE]	= "CPU backtrace interrupts",
824d914d4d4SAaro Koskinen 	[IPI_KGDB_ROUNDUP]	= "KGDB roundup interrupts",
825d914d4d4SAaro Koskinen };
82608e875c1SCatalin Marinas 
82708e875c1SCatalin Marinas static void smp_cross_call(const struct cpumask *target, unsigned int ipinr);
82878fd584cSAKASHI Takahiro 
82978fd584cSAKASHI Takahiro unsigned long irq_err_count;
83078fd584cSAKASHI Takahiro 
arch_show_interrupts(struct seq_file * p,int prec)83178fd584cSAKASHI Takahiro int arch_show_interrupts(struct seq_file *p, int prec)
8325ab6876cSJosh Poimboeuf {
83378fd584cSAKASHI Takahiro 	unsigned int cpu, i;
83478fd584cSAKASHI Takahiro 
83578fd584cSAKASHI Takahiro 	for (i = 0; i < MAX_IPI; i++) {
83678fd584cSAKASHI Takahiro 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
83778fd584cSAKASHI Takahiro 			   prec >= 4 ? " " : "");
83878fd584cSAKASHI Takahiro 		for_each_online_cpu(cpu)
83978fd584cSAKASHI Takahiro 			seq_printf(p, "%10u ", irq_desc_kstat_cpu(get_ipi_desc(cpu, i), cpu));
840f5df2696SJames Morse 		seq_printf(p, "      %s\n", ipi_types[i]);
84178fd584cSAKASHI Takahiro 	}
842de58ed5eSGavin Shan 
843de58ed5eSGavin Shan 	seq_printf(p, "%*s: %10lu\n", prec, "Err", irq_err_count);
84478fd584cSAKASHI Takahiro 	return 0;
84578fd584cSAKASHI Takahiro }
84678fd584cSAKASHI Takahiro 
arch_send_call_function_ipi_mask(const struct cpumask * mask)8475ab6876cSJosh Poimboeuf void arch_send_call_function_ipi_mask(const struct cpumask *mask)
8485ab6876cSJosh Poimboeuf {
84978fd584cSAKASHI Takahiro 	smp_cross_call(mask, IPI_CALL_FUNC);
85078fd584cSAKASHI Takahiro }
85178fd584cSAKASHI Takahiro 
arch_send_call_function_single_ipi(int cpu)852331a1b3aSDouglas Anderson void arch_send_call_function_single_ipi(int cpu)
853331a1b3aSDouglas Anderson {
854331a1b3aSDouglas Anderson 	smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC);
855331a1b3aSDouglas Anderson }
856331a1b3aSDouglas Anderson 
857331a1b3aSDouglas Anderson #ifdef CONFIG_IRQ_WORK
arch_irq_work_raise(void)858331a1b3aSDouglas Anderson void arch_irq_work_raise(void)
859331a1b3aSDouglas Anderson {
860331a1b3aSDouglas Anderson 	smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
861331a1b3aSDouglas Anderson }
862331a1b3aSDouglas Anderson #endif
863331a1b3aSDouglas Anderson 
local_cpu_stop(unsigned int cpu)864331a1b3aSDouglas Anderson static void __noreturn local_cpu_stop(unsigned int cpu)
865331a1b3aSDouglas Anderson {
866331a1b3aSDouglas Anderson 	set_cpu_online(cpu, false);
867331a1b3aSDouglas Anderson 
8682f5cd0c7SDouglas Anderson 	local_daif_mask();
8692f5cd0c7SDouglas Anderson 	sdei_mask_local_cpu();
8702f5cd0c7SDouglas Anderson 	cpu_park_loop();
8712f5cd0c7SDouglas Anderson }
8722f5cd0c7SDouglas Anderson 
8732f5cd0c7SDouglas Anderson /*
8742f5cd0c7SDouglas Anderson  * We need to implement panic_smp_self_stop() for parallel panic() calls, so
8752f5cd0c7SDouglas Anderson  * that cpu_online_mask gets correctly updated and smp_send_stop() can skip
8762f5cd0c7SDouglas Anderson  * CPUs that have already stopped themselves.
8772f5cd0c7SDouglas Anderson  */
panic_smp_self_stop(void)8782f5cd0c7SDouglas Anderson void __noreturn panic_smp_self_stop(void)
8792f5cd0c7SDouglas Anderson {
8802f5cd0c7SDouglas Anderson 	local_cpu_stop(smp_processor_id());
8812f5cd0c7SDouglas Anderson }
8822f5cd0c7SDouglas Anderson 
ipi_cpu_crash_stop(unsigned int cpu,struct pt_regs * regs)8832f5cd0c7SDouglas Anderson static void __noreturn ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
88408e875c1SCatalin Marinas {
88508e875c1SCatalin Marinas #ifdef CONFIG_KEXEC_CORE
88608e875c1SCatalin Marinas 	/*
887d3afc7f1SMarc Zyngier 	 * Use local_daif_mask() instead of local_irq_disable() to make sure
88808e875c1SCatalin Marinas 	 * that pseudo-NMIs are disabled. The "crash stop" code starts with
88908e875c1SCatalin Marinas 	 * an IRQ and falls back to NMI (which might be pseudo). If the IRQ
89008e875c1SCatalin Marinas 	 * finally goes through right as we're timing out then the NMI could
891a2638815SMarc Zyngier 	 * interrupt us. It's better to prevent the NMI and let the IRQ
8924a3182e6SPeter Zijlstra 	 * finish since the pt_regs will be better.
89308e875c1SCatalin Marinas 	 */
89408e875c1SCatalin Marinas 	local_daif_mask();
89508e875c1SCatalin Marinas 
89608e875c1SCatalin Marinas 	crash_save_cpu(regs, cpu);
89708e875c1SCatalin Marinas 
89808e875c1SCatalin Marinas 	set_cpu_online(cpu, false);
89908e875c1SCatalin Marinas 
90008e875c1SCatalin Marinas 	sdei_mask_local_cpu();
90108e875c1SCatalin Marinas 
90208e875c1SCatalin Marinas 	if (IS_ENABLED(CONFIG_HOTPLUG_CPU))
90308e875c1SCatalin Marinas 		__cpu_try_die(cpu);
904d914d4d4SAaro Koskinen 
90508e875c1SCatalin Marinas 	/* just in case */
90608e875c1SCatalin Marinas 	cpu_park_loop();
90778fd584cSAKASHI Takahiro #else
90878fd584cSAKASHI Takahiro 	BUG();
909d3afc7f1SMarc Zyngier #endif
91078fd584cSAKASHI Takahiro }
91178fd584cSAKASHI Takahiro 
arm64_send_ipi(const cpumask_t * mask,unsigned int nr)91278fd584cSAKASHI Takahiro static void arm64_send_ipi(const cpumask_t *mask, unsigned int nr)
91378fd584cSAKASHI Takahiro {
91478fd584cSAKASHI Takahiro 	unsigned int cpu;
9151f85008eSLorenzo Pieralisi 
9161f85008eSLorenzo Pieralisi 	if (!percpu_ipi_descs)
9171f85008eSLorenzo Pieralisi 		__ipi_send_mask(get_ipi_desc(0, nr), mask);
9181f85008eSLorenzo Pieralisi 	else
9191f85008eSLorenzo Pieralisi 		for_each_cpu(cpu, mask)
9201f85008eSLorenzo Pieralisi 			__ipi_send_single(get_ipi_desc(cpu, nr), cpu);
921eb631bb5SLarry Bassel }
922eb631bb5SLarry Bassel 
arm64_backtrace_ipi(cpumask_t * mask)923eb631bb5SLarry Bassel static void arm64_backtrace_ipi(cpumask_t *mask)
924eb631bb5SLarry Bassel {
925eb631bb5SLarry Bassel 	arm64_send_ipi(mask, IPI_CPU_BACKTRACE);
926eb631bb5SLarry Bassel }
927331a1b3aSDouglas Anderson 
arch_trigger_cpumask_backtrace(const cpumask_t * mask,int exclude_cpu)928331a1b3aSDouglas Anderson void arch_trigger_cpumask_backtrace(const cpumask_t *mask, int exclude_cpu)
929331a1b3aSDouglas Anderson {
930331a1b3aSDouglas Anderson 	/*
931331a1b3aSDouglas Anderson 	 * NOTE: though nmi_trigger_cpumask_backtrace() has "nmi_" in the name,
932331a1b3aSDouglas Anderson 	 * nothing about it truly needs to be implemented using an NMI, it's
9335e89c55eSLorenzo Pieralisi 	 * just that it's _allowed_ to work with NMIs. If ipi_should_be_nmi()
934331a1b3aSDouglas Anderson 	 * returned false our backtrace attempt will just use a regular IPI.
9352f5cd0c7SDouglas Anderson 	 */
9362f5cd0c7SDouglas Anderson 	nmi_trigger_cpumask_backtrace(mask, exclude_cpu, arm64_backtrace_ipi);
9372f5cd0c7SDouglas Anderson }
9385e89c55eSLorenzo Pieralisi 
93908e875c1SCatalin Marinas #ifdef CONFIG_KGDB
kgdb_roundup_cpus(void)94008e875c1SCatalin Marinas void kgdb_roundup_cpus(void)
94108e875c1SCatalin Marinas {
94208e875c1SCatalin Marinas 	int this_cpu = raw_smp_processor_id();
94345ed695aSNicolas Pitre 	int cpu;
94445ed695aSNicolas Pitre 
9454a3182e6SPeter Zijlstra 	for_each_online_cpu(cpu) {
946d3afc7f1SMarc Zyngier 		/* No need to roundup ourselves */
947d3afc7f1SMarc Zyngier 		if (cpu == this_cpu)
948d3afc7f1SMarc Zyngier 			continue;
949d3afc7f1SMarc Zyngier 
950d3afc7f1SMarc Zyngier 		__ipi_send_single(get_ipi_desc(cpu, IPI_KGDB_ROUNDUP), cpu);
951d3afc7f1SMarc Zyngier 	}
952d3afc7f1SMarc Zyngier }
953d3afc7f1SMarc Zyngier #endif
9545cebfd2dSMarc Zyngier 
955d3afc7f1SMarc Zyngier /*
9565cebfd2dSMarc Zyngier  * Main handler for inter-processor interrupts
9575cebfd2dSMarc Zyngier  */
do_handle_IPI(int ipinr)958d3afc7f1SMarc Zyngier static void do_handle_IPI(int ipinr)
959d3afc7f1SMarc Zyngier {
960331a1b3aSDouglas Anderson 	unsigned int cpu = smp_processor_id();
961331a1b3aSDouglas Anderson 
9624bb49009SDouglas Anderson 	if ((unsigned)ipinr < NR_IPI)
963331a1b3aSDouglas Anderson 		trace_ipi_entry(ipi_types[ipinr]);
964331a1b3aSDouglas Anderson 
965331a1b3aSDouglas Anderson 	switch (ipinr) {
966d7402513SDouglas Anderson 	case IPI_RESCHEDULE:
967d7402513SDouglas Anderson 		scheduler_ipi();
968331a1b3aSDouglas Anderson 		break;
9692f5cd0c7SDouglas Anderson 
970331a1b3aSDouglas Anderson 	case IPI_CALL_FUNC:
971331a1b3aSDouglas Anderson 		generic_smp_call_function_interrupt();
972331a1b3aSDouglas Anderson 		break;
973331a1b3aSDouglas Anderson 
974331a1b3aSDouglas Anderson 	case IPI_CPU_STOP:
975331a1b3aSDouglas Anderson 	case IPI_CPU_STOP_NMI:
976d3afc7f1SMarc Zyngier 		if (IS_ENABLED(CONFIG_KEXEC_CORE) && crash_stop) {
977d3afc7f1SMarc Zyngier 			ipi_cpu_crash_stop(cpu, get_irq_regs());
978d3afc7f1SMarc Zyngier 			unreachable();
979d3afc7f1SMarc Zyngier 		} else {
9805cebfd2dSMarc Zyngier 			local_cpu_stop(cpu);
981d3afc7f1SMarc Zyngier 		}
982d3afc7f1SMarc Zyngier 		break;
983331a1b3aSDouglas Anderson 
984331a1b3aSDouglas Anderson #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
985331a1b3aSDouglas Anderson 	case IPI_TIMER:
986331a1b3aSDouglas Anderson 		tick_receive_broadcast();
987331a1b3aSDouglas Anderson 		break;
988d3afc7f1SMarc Zyngier #endif
989d3afc7f1SMarc Zyngier 
990331a1b3aSDouglas Anderson #ifdef CONFIG_IRQ_WORK
991331a1b3aSDouglas Anderson 	case IPI_IRQ_WORK:
992d3afc7f1SMarc Zyngier 		irq_work_run();
9939d9edb96SYueHaibing 		break;
994d3afc7f1SMarc Zyngier #endif
995d3afc7f1SMarc Zyngier 
996d3afc7f1SMarc Zyngier 	case IPI_CPU_BACKTRACE:
997d3afc7f1SMarc Zyngier 		/*
9985cebfd2dSMarc Zyngier 		 * NOTE: in some cases this _won't_ be NMI context. See the
999d3afc7f1SMarc Zyngier 		 * comment in arch_trigger_cpumask_backtrace().
1000d3afc7f1SMarc Zyngier 		 */
1001331a1b3aSDouglas Anderson 		nmi_cpu_backtrace(get_irq_regs());
1002331a1b3aSDouglas Anderson 		break;
1003331a1b3aSDouglas Anderson 
1004331a1b3aSDouglas Anderson 	case IPI_KGDB_ROUNDUP:
1005331a1b3aSDouglas Anderson 		kgdb_nmicallback(cpu, get_irq_regs());
1006d3afc7f1SMarc Zyngier 		break;
1007d3afc7f1SMarc Zyngier 
1008331a1b3aSDouglas Anderson 	default:
1009331a1b3aSDouglas Anderson 		pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
10109d9edb96SYueHaibing 		break;
1011d3afc7f1SMarc Zyngier 	}
1012d3afc7f1SMarc Zyngier 
1013d3afc7f1SMarc Zyngier 	if ((unsigned)ipinr < NR_IPI)
1014d3afc7f1SMarc Zyngier 		trace_ipi_exit(ipi_types[ipinr]);
1015d3afc7f1SMarc Zyngier }
1016331a1b3aSDouglas Anderson 
ipi_handler(int irq,void * data)1017331a1b3aSDouglas Anderson static irqreturn_t ipi_handler(int irq, void *data)
1018d3afc7f1SMarc Zyngier {
1019d3afc7f1SMarc Zyngier 	unsigned int ipi = (irq - ipi_irq_base) % nr_ipi;
1020d3afc7f1SMarc Zyngier 
1021d3afc7f1SMarc Zyngier 	do_handle_IPI(ipi);
1022331a1b3aSDouglas Anderson 	return IRQ_HANDLED;
1023331a1b3aSDouglas Anderson }
1024331a1b3aSDouglas Anderson 
smp_cross_call(const struct cpumask * target,unsigned int ipinr)1025331a1b3aSDouglas Anderson static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
1026331a1b3aSDouglas Anderson {
1027331a1b3aSDouglas Anderson 	trace_ipi_raise(target, ipi_types[ipinr]);
1028d3afc7f1SMarc Zyngier 	arm64_send_ipi(target, ipinr);
1029a2638815SMarc Zyngier }
1030331a1b3aSDouglas Anderson 
ipi_should_be_nmi(enum ipi_msg_type ipi)1031331a1b3aSDouglas Anderson static bool ipi_should_be_nmi(enum ipi_msg_type ipi)
1032331a1b3aSDouglas Anderson {
1033d3afc7f1SMarc Zyngier 	if (!system_uses_irq_prio_masking())
1034d3afc7f1SMarc Zyngier 		return false;
1035d3afc7f1SMarc Zyngier 
1036d3afc7f1SMarc Zyngier 	switch (ipi) {
1037d3afc7f1SMarc Zyngier 	case IPI_CPU_STOP_NMI:
1038d3afc7f1SMarc Zyngier 	case IPI_CPU_BACKTRACE:
1039d3afc7f1SMarc Zyngier 	case IPI_KGDB_ROUNDUP:
1040d3afc7f1SMarc Zyngier 		return true;
1041d3afc7f1SMarc Zyngier 	default:
1042d3afc7f1SMarc Zyngier 		return false;
1043d3afc7f1SMarc Zyngier 	}
10444c8c3c7fSValentin Schneider }
104508e875c1SCatalin Marinas 
ipi_setup(int cpu)104608e875c1SCatalin Marinas static void ipi_setup(int cpu)
104708e875c1SCatalin Marinas {
104808e875c1SCatalin Marinas 	int i;
10492b2d0a7aSMark Rutland 
10502b2d0a7aSMark Rutland 	if (WARN_ON_ONCE(!ipi_irq_base))
10512b2d0a7aSMark Rutland 		return;
10522b2d0a7aSMark Rutland 
10532b2d0a7aSMark Rutland 	for (i = 0; i < nr_ipi; i++) {
10542b2d0a7aSMark Rutland 		if (!percpu_ipi_descs) {
10552b2d0a7aSMark Rutland 			if (ipi_should_be_nmi(i)) {
1056ef31b8ceSDouglas Anderson 				prepare_percpu_nmi(ipi_irq_base + i);
10572b2d0a7aSMark Rutland 				enable_percpu_nmi(ipi_irq_base + i, 0);
10582b2d0a7aSMark Rutland 			} else {
10592b2d0a7aSMark Rutland 				enable_percpu_irq(ipi_irq_base + i, 0);
10601f85008eSLorenzo Pieralisi 			}
10611f85008eSLorenzo Pieralisi 		} else {
10621f85008eSLorenzo Pieralisi 			enable_irq(irq_desc_get_irq(get_ipi_desc(cpu, i)));
10631f85008eSLorenzo Pieralisi 		}
10641f85008eSLorenzo Pieralisi 	}
10651f85008eSLorenzo Pieralisi }
10661f85008eSLorenzo Pieralisi 
1067d0bab0c3SCristian Marussi #ifdef CONFIG_HOTPLUG_CPU
ipi_teardown(int cpu)1068d0bab0c3SCristian Marussi static void ipi_teardown(int cpu)
1069d0bab0c3SCristian Marussi {
1070d0bab0c3SCristian Marussi 	int i;
1071d0bab0c3SCristian Marussi 
1072d0bab0c3SCristian Marussi 	if (WARN_ON_ONCE(!ipi_irq_base))
1073d0bab0c3SCristian Marussi 		return;
1074d0bab0c3SCristian Marussi 
1075d0bab0c3SCristian Marussi 	for (i = 0; i < nr_ipi; i++) {
1076d0bab0c3SCristian Marussi 		if (!percpu_ipi_descs) {
1077d0bab0c3SCristian Marussi 			if (ipi_should_be_nmi(i)) {
107808e875c1SCatalin Marinas 				disable_percpu_nmi(ipi_irq_base + i);
107908e875c1SCatalin Marinas 				teardown_percpu_nmi(ipi_irq_base + i);
108008e875c1SCatalin Marinas 			} else {
108108e875c1SCatalin Marinas 				disable_percpu_irq(ipi_irq_base + i);
1082d0bab0c3SCristian Marussi 			}
108308e875c1SCatalin Marinas 		} else {
108408e875c1SCatalin Marinas 			disable_irq(irq_desc_get_irq(get_ipi_desc(cpu, i)));
108508e875c1SCatalin Marinas 		}
1086434ed7f4SRusty Russell 	}
108708e875c1SCatalin Marinas }
1088ef284f5cSThomas Gleixner #endif
108982611c14SJan Glauber 
ipi_setup_sgi(int ipi)109008e875c1SCatalin Marinas static void ipi_setup_sgi(int ipi)
109108e875c1SCatalin Marinas {
109208e875c1SCatalin Marinas 	int err, irq, cpu;
109308e875c1SCatalin Marinas 
109408e875c1SCatalin Marinas 	irq = ipi_irq_base + ipi;
1095d0bab0c3SCristian Marussi 
109608e875c1SCatalin Marinas 	if (ipi_should_be_nmi(ipi)) {
109708e875c1SCatalin Marinas 		err = request_percpu_nmi(irq, ipi_handler, "IPI", &irq_stat);
1098d0bab0c3SCristian Marussi 		WARN(err, "Could not request IRQ %d as NMI, err=%d\n", irq, err);
1099a74ec64aSKefeng Wang 	} else {
110082611c14SJan Glauber 		err = request_percpu_irq(irq, ipi_handler, "IPI", &irq_stat);
1101f5df2696SJames Morse 		WARN(err, "Could not request IRQ %d as IRQ, err=%d\n", irq, err);
1102f5df2696SJames Morse 	}
110308e875c1SCatalin Marinas 
110408e875c1SCatalin Marinas 	for_each_possible_cpu(cpu)
110578fd584cSAKASHI Takahiro 		get_ipi_desc(cpu, ipi) = irq_to_desc(irq);
1106a88ce63bSHoeun Ryu 
110778fd584cSAKASHI Takahiro 	irq_set_status_flags(irq, IRQ_HIDDEN);
1108a88ce63bSHoeun Ryu }
110978fd584cSAKASHI Takahiro 
ipi_setup_lpi(int ipi,int ncpus)111078fd584cSAKASHI Takahiro static void ipi_setup_lpi(int ipi, int ncpus)
111178fd584cSAKASHI Takahiro {
1112a88ce63bSHoeun Ryu 	for (int cpu = 0; cpu < ncpus; cpu++) {
1113a88ce63bSHoeun Ryu 		int err, irq;
1114a88ce63bSHoeun Ryu 
1115a88ce63bSHoeun Ryu 		irq = ipi_irq_base + (cpu * nr_ipi) + ipi;
1116a88ce63bSHoeun Ryu 
1117a88ce63bSHoeun Ryu 		err = irq_force_affinity(irq, cpumask_of(cpu));
1118a88ce63bSHoeun Ryu 		WARN(err, "Could not force affinity IRQ %d, err=%d\n", irq, err);
1119a88ce63bSHoeun Ryu 
1120a88ce63bSHoeun Ryu 		err = request_irq(irq, ipi_handler, IRQF_NO_AUTOEN, "IPI",
1121f50b7dacSCristian Marussi 				  NULL);
1122f50b7dacSCristian Marussi 		WARN(err, "Could not request IRQ %d, err=%d\n", irq, err);
1123f50b7dacSCristian Marussi 
1124f50b7dacSCristian Marussi 		irq_set_status_flags(irq, (IRQ_HIDDEN | IRQ_NO_BALANCING_MASK));
11255cd474e5SD Scott Phillips 
11265cd474e5SD Scott Phillips 		get_ipi_desc(cpu, ipi) = irq_to_desc(irq);
112778fd584cSAKASHI Takahiro 	}
112878fd584cSAKASHI Takahiro }
112978fd584cSAKASHI Takahiro 
set_smp_ipi_range_percpu(int ipi_base,int n,int ncpus)113078fd584cSAKASHI Takahiro void __init set_smp_ipi_range_percpu(int ipi_base, int n, int ncpus)
1131f50b7dacSCristian Marussi {
113278fd584cSAKASHI Takahiro 	int i;
113378fd584cSAKASHI Takahiro 
113478fd584cSAKASHI Takahiro 	WARN_ON(n < MAX_IPI);
113578fd584cSAKASHI Takahiro 	nr_ipi = min(n, MAX_IPI);
113678fd584cSAKASHI Takahiro 
113778fd584cSAKASHI Takahiro 	percpu_ipi_descs = !!ncpus;
113878fd584cSAKASHI Takahiro 	ipi_irq_base = ipi_base;
113978fd584cSAKASHI Takahiro 
114078fd584cSAKASHI Takahiro 	for (i = 0; i < nr_ipi; i++) {
114178fd584cSAKASHI Takahiro 		if (!percpu_ipi_descs)
1142a74ec64aSKefeng Wang 			ipi_setup_sgi(i);
114378fd584cSAKASHI Takahiro 		else
1144f5df2696SJames Morse 			ipi_setup_lpi(i, ncpus);
11455cd474e5SD Scott Phillips 	}
1146f5df2696SJames Morse 
11475cd474e5SD Scott Phillips 	/* Setup the boot CPU immediately */
114878fd584cSAKASHI Takahiro 	ipi_setup(smp_processor_id());
114978fd584cSAKASHI Takahiro }
115078fd584cSAKASHI Takahiro 
arch_smp_send_reschedule(int cpu)115178fd584cSAKASHI Takahiro void arch_smp_send_reschedule(int cpu)
115278fd584cSAKASHI Takahiro {
115378fd584cSAKASHI Takahiro 	smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
115478fd584cSAKASHI Takahiro }
115578fd584cSAKASHI Takahiro 
11565c492c3fSJames Morse #ifdef CONFIG_ARM64_ACPI_PARKING_PROTOCOL
arch_send_wakeup_ipi(unsigned int cpu)11575c492c3fSJames Morse void arch_send_wakeup_ipi(unsigned int cpu)
11585c492c3fSJames Morse {
11595c492c3fSJames Morse 	/*
1160de58ed5eSGavin Shan 	 * We use a scheduler IPI to wake the CPU as this avoids the need for a
11615c492c3fSJames Morse 	 * dedicated IPI and we can safely handle spurious scheduler IPIs.
1162de58ed5eSGavin Shan 	 */
11635c492c3fSJames Morse 	smp_send_reschedule(cpu);
11645c492c3fSJames Morse }
11655c492c3fSJames Morse #endif
11665c492c3fSJames Morse 
11675c492c3fSJames Morse #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
tick_broadcast(const struct cpumask * mask)11685c492c3fSJames Morse void tick_broadcast(const struct cpumask *mask)
11695c492c3fSJames Morse {
11705c492c3fSJames Morse 	smp_cross_call(mask, IPI_TIMER);
11715c492c3fSJames Morse }
11728f4566f1SWill Deacon #endif
11738f4566f1SWill Deacon 
11745c492c3fSJames Morse /*
1175  * The number of CPUs online, not counting this CPU (which may not be
1176  * fully online and so not counted in num_online_cpus()).
1177  */
num_other_online_cpus(void)1178 static inline unsigned int num_other_online_cpus(void)
1179 {
1180 	unsigned int this_cpu_online = cpu_online(smp_processor_id());
1181 
1182 	return num_online_cpus() - this_cpu_online;
1183 }
1184 
smp_send_stop(void)1185 void smp_send_stop(void)
1186 {
1187 	static unsigned long stop_in_progress;
1188 	static cpumask_t mask;
1189 	unsigned long timeout;
1190 
1191 	/*
1192 	 * If this cpu is the only one alive at this point in time, online or
1193 	 * not, there are no stop messages to be sent around, so just back out.
1194 	 */
1195 	if (num_other_online_cpus() == 0)
1196 		goto skip_ipi;
1197 
1198 	/* Only proceed if this is the first CPU to reach this code */
1199 	if (test_and_set_bit(0, &stop_in_progress))
1200 		return;
1201 
1202 	/*
1203 	 * Send an IPI to all currently online CPUs except the CPU running
1204 	 * this code.
1205 	 *
1206 	 * NOTE: we don't do anything here to prevent other CPUs from coming
1207 	 * online after we snapshot `cpu_online_mask`. Ideally, the calling code
1208 	 * should do something to prevent other CPUs from coming up. This code
1209 	 * can be called in the panic path and thus it doesn't seem wise to
1210 	 * grab the CPU hotplug mutex ourselves. Worst case:
1211 	 * - If a CPU comes online as we're running, we'll likely notice it
1212 	 *   during the 1 second wait below and then we'll catch it when we try
1213 	 *   with an NMI (assuming NMIs are enabled) since we re-snapshot the
1214 	 *   mask before sending an NMI.
1215 	 * - If we leave the function and see that CPUs are still online we'll
1216 	 *   at least print a warning. Especially without NMIs this function
1217 	 *   isn't foolproof anyway so calling code will just have to accept
1218 	 *   the fact that there could be cases where a CPU can't be stopped.
1219 	 */
1220 	cpumask_copy(&mask, cpu_online_mask);
1221 	cpumask_clear_cpu(smp_processor_id(), &mask);
1222 
1223 	if (system_state <= SYSTEM_RUNNING)
1224 		pr_crit("SMP: stopping secondary CPUs\n");
1225 
1226 	/*
1227 	 * Start with a normal IPI and wait up to one second for other CPUs to
1228 	 * stop. We do this first because it gives other processors a chance
1229 	 * to exit critical sections / drop locks and makes the rest of the
1230 	 * stop process (especially console flush) more robust.
1231 	 */
1232 	smp_cross_call(&mask, IPI_CPU_STOP);
1233 	timeout = USEC_PER_SEC;
1234 	while (num_other_online_cpus() && timeout--)
1235 		udelay(1);
1236 
1237 	/*
1238 	 * If CPUs are still online, try an NMI. There's no excuse for this to
1239 	 * be slow, so we only give them an extra 10 ms to respond.
1240 	 */
1241 	if (num_other_online_cpus() && ipi_should_be_nmi(IPI_CPU_STOP_NMI)) {
1242 		smp_rmb();
1243 		cpumask_copy(&mask, cpu_online_mask);
1244 		cpumask_clear_cpu(smp_processor_id(), &mask);
1245 
1246 		pr_info("SMP: retry stop with NMI for CPUs %*pbl\n",
1247 			cpumask_pr_args(&mask));
1248 
1249 		smp_cross_call(&mask, IPI_CPU_STOP_NMI);
1250 		timeout = USEC_PER_MSEC * 10;
1251 		while (num_other_online_cpus() && timeout--)
1252 			udelay(1);
1253 	}
1254 
1255 	if (num_other_online_cpus()) {
1256 		smp_rmb();
1257 		cpumask_copy(&mask, cpu_online_mask);
1258 		cpumask_clear_cpu(smp_processor_id(), &mask);
1259 
1260 		pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
1261 			cpumask_pr_args(&mask));
1262 	}
1263 
1264 skip_ipi:
1265 	sdei_mask_local_cpu();
1266 }
1267 
1268 #ifdef CONFIG_KEXEC_CORE
crash_smp_send_stop(void)1269 void crash_smp_send_stop(void)
1270 {
1271 	/*
1272 	 * This function can be called twice in panic path, but obviously
1273 	 * we execute this only once.
1274 	 *
1275 	 * We use this same boolean to tell whether the IPI we send was a
1276 	 * stop or a "crash stop".
1277 	 */
1278 	if (crash_stop)
1279 		return;
1280 	crash_stop = 1;
1281 
1282 	smp_send_stop();
1283 
1284 	sdei_handler_abort();
1285 }
1286 
smp_crash_stop_failed(void)1287 bool smp_crash_stop_failed(void)
1288 {
1289 	return num_other_online_cpus() != 0;
1290 }
1291 #endif
1292 
have_cpu_die(void)1293 static bool have_cpu_die(void)
1294 {
1295 #ifdef CONFIG_HOTPLUG_CPU
1296 	int any_cpu = raw_smp_processor_id();
1297 	const struct cpu_operations *ops = get_cpu_ops(any_cpu);
1298 
1299 	if (ops && ops->cpu_die)
1300 		return true;
1301 #endif
1302 	return false;
1303 }
1304 
cpus_are_stuck_in_kernel(void)1305 bool cpus_are_stuck_in_kernel(void)
1306 {
1307 	bool smp_spin_tables = (num_possible_cpus() > 1 && !have_cpu_die());
1308 
1309 	return !!cpus_stuck_in_kernel || smp_spin_tables ||
1310 		is_protected_kvm_enabled();
1311 }
1312