xref: /linux/arch/riscv/kernel/smp.c (revision 90d32e92011eaae8e70a9169b4e7acf4ca8f9d3a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SMP initialisation and IPI support
4  * Based on arch/arm64/kernel/smp.c
5  *
6  * Copyright (C) 2012 ARM Ltd.
7  * Copyright (C) 2015 Regents of the University of California
8  * Copyright (C) 2017 SiFive
9  */
10 
11 #include <linux/cpu.h>
12 #include <linux/clockchips.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/kexec.h>
16 #include <linux/percpu.h>
17 #include <linux/profile.h>
18 #include <linux/smp.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21 #include <linux/delay.h>
22 #include <linux/irq.h>
23 #include <linux/irq_work.h>
24 
25 #include <asm/tlbflush.h>
26 #include <asm/cacheflush.h>
27 #include <asm/cpu_ops.h>
28 
29 enum ipi_message_type {
30 	IPI_RESCHEDULE,
31 	IPI_CALL_FUNC,
32 	IPI_CPU_STOP,
33 	IPI_CPU_CRASH_STOP,
34 	IPI_IRQ_WORK,
35 	IPI_TIMER,
36 	IPI_MAX
37 };
38 
39 unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
40 	[0 ... NR_CPUS-1] = INVALID_HARTID
41 };
42 
43 void __init smp_setup_processor_id(void)
44 {
45 	cpuid_to_hartid_map(0) = boot_cpu_hartid;
46 }
47 
48 static DEFINE_PER_CPU_READ_MOSTLY(int, ipi_dummy_dev);
49 static int ipi_virq_base __ro_after_init;
50 static int nr_ipi __ro_after_init = IPI_MAX;
51 static struct irq_desc *ipi_desc[IPI_MAX] __read_mostly;
52 
53 int riscv_hartid_to_cpuid(unsigned long hartid)
54 {
55 	int i;
56 
57 	for (i = 0; i < NR_CPUS; i++)
58 		if (cpuid_to_hartid_map(i) == hartid)
59 			return i;
60 
61 	return -ENOENT;
62 }
63 
64 static void ipi_stop(void)
65 {
66 	set_cpu_online(smp_processor_id(), false);
67 	while (1)
68 		wait_for_interrupt();
69 }
70 
71 #ifdef CONFIG_KEXEC_CORE
72 static atomic_t waiting_for_crash_ipi = ATOMIC_INIT(0);
73 
74 static inline void ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
75 {
76 	crash_save_cpu(regs, cpu);
77 
78 	atomic_dec(&waiting_for_crash_ipi);
79 
80 	local_irq_disable();
81 
82 #ifdef CONFIG_HOTPLUG_CPU
83 	if (cpu_has_hotplug(cpu))
84 		cpu_ops->cpu_stop();
85 #endif
86 
87 	for(;;)
88 		wait_for_interrupt();
89 }
90 #else
91 static inline void ipi_cpu_crash_stop(unsigned int cpu, struct pt_regs *regs)
92 {
93 	unreachable();
94 }
95 #endif
96 
97 static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
98 {
99 	__ipi_send_mask(ipi_desc[op], mask);
100 }
101 
102 static void send_ipi_single(int cpu, enum ipi_message_type op)
103 {
104 	__ipi_send_mask(ipi_desc[op], cpumask_of(cpu));
105 }
106 
107 #ifdef CONFIG_IRQ_WORK
108 void arch_irq_work_raise(void)
109 {
110 	send_ipi_single(smp_processor_id(), IPI_IRQ_WORK);
111 }
112 #endif
113 
114 static irqreturn_t handle_IPI(int irq, void *data)
115 {
116 	int ipi = irq - ipi_virq_base;
117 
118 	switch (ipi) {
119 	case IPI_RESCHEDULE:
120 		scheduler_ipi();
121 		break;
122 	case IPI_CALL_FUNC:
123 		generic_smp_call_function_interrupt();
124 		break;
125 	case IPI_CPU_STOP:
126 		ipi_stop();
127 		break;
128 	case IPI_CPU_CRASH_STOP:
129 		ipi_cpu_crash_stop(smp_processor_id(), get_irq_regs());
130 		break;
131 	case IPI_IRQ_WORK:
132 		irq_work_run();
133 		break;
134 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
135 	case IPI_TIMER:
136 		tick_receive_broadcast();
137 		break;
138 #endif
139 	default:
140 		pr_warn("CPU%d: unhandled IPI%d\n", smp_processor_id(), ipi);
141 		break;
142 	}
143 
144 	return IRQ_HANDLED;
145 }
146 
147 void riscv_ipi_enable(void)
148 {
149 	int i;
150 
151 	if (WARN_ON_ONCE(!ipi_virq_base))
152 		return;
153 
154 	for (i = 0; i < nr_ipi; i++)
155 		enable_percpu_irq(ipi_virq_base + i, 0);
156 }
157 
158 void riscv_ipi_disable(void)
159 {
160 	int i;
161 
162 	if (WARN_ON_ONCE(!ipi_virq_base))
163 		return;
164 
165 	for (i = 0; i < nr_ipi; i++)
166 		disable_percpu_irq(ipi_virq_base + i);
167 }
168 
169 bool riscv_ipi_have_virq_range(void)
170 {
171 	return (ipi_virq_base) ? true : false;
172 }
173 
174 void riscv_ipi_set_virq_range(int virq, int nr)
175 {
176 	int i, err;
177 
178 	if (WARN_ON(ipi_virq_base))
179 		return;
180 
181 	WARN_ON(nr < IPI_MAX);
182 	nr_ipi = min(nr, IPI_MAX);
183 	ipi_virq_base = virq;
184 
185 	/* Request IPIs */
186 	for (i = 0; i < nr_ipi; i++) {
187 		err = request_percpu_irq(ipi_virq_base + i, handle_IPI,
188 					 "IPI", &ipi_dummy_dev);
189 		WARN_ON(err);
190 
191 		ipi_desc[i] = irq_to_desc(ipi_virq_base + i);
192 		irq_set_status_flags(ipi_virq_base + i, IRQ_HIDDEN);
193 	}
194 
195 	/* Enabled IPIs for boot CPU immediately */
196 	riscv_ipi_enable();
197 }
198 
199 static const char * const ipi_names[] = {
200 	[IPI_RESCHEDULE]	= "Rescheduling interrupts",
201 	[IPI_CALL_FUNC]		= "Function call interrupts",
202 	[IPI_CPU_STOP]		= "CPU stop interrupts",
203 	[IPI_CPU_CRASH_STOP]	= "CPU stop (for crash dump) interrupts",
204 	[IPI_IRQ_WORK]		= "IRQ work interrupts",
205 	[IPI_TIMER]		= "Timer broadcast interrupts",
206 };
207 
208 void show_ipi_stats(struct seq_file *p, int prec)
209 {
210 	unsigned int cpu, i;
211 
212 	for (i = 0; i < IPI_MAX; i++) {
213 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
214 			   prec >= 4 ? " " : "");
215 		for_each_online_cpu(cpu)
216 			seq_printf(p, "%10u ", irq_desc_kstat_cpu(ipi_desc[i], cpu));
217 		seq_printf(p, " %s\n", ipi_names[i]);
218 	}
219 }
220 
221 void arch_send_call_function_ipi_mask(struct cpumask *mask)
222 {
223 	send_ipi_mask(mask, IPI_CALL_FUNC);
224 }
225 
226 void arch_send_call_function_single_ipi(int cpu)
227 {
228 	send_ipi_single(cpu, IPI_CALL_FUNC);
229 }
230 
231 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
232 void tick_broadcast(const struct cpumask *mask)
233 {
234 	send_ipi_mask(mask, IPI_TIMER);
235 }
236 #endif
237 
238 void smp_send_stop(void)
239 {
240 	unsigned long timeout;
241 
242 	if (num_online_cpus() > 1) {
243 		cpumask_t mask;
244 
245 		cpumask_copy(&mask, cpu_online_mask);
246 		cpumask_clear_cpu(smp_processor_id(), &mask);
247 
248 		if (system_state <= SYSTEM_RUNNING)
249 			pr_crit("SMP: stopping secondary CPUs\n");
250 		send_ipi_mask(&mask, IPI_CPU_STOP);
251 	}
252 
253 	/* Wait up to one second for other CPUs to stop */
254 	timeout = USEC_PER_SEC;
255 	while (num_online_cpus() > 1 && timeout--)
256 		udelay(1);
257 
258 	if (num_online_cpus() > 1)
259 		pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
260 			   cpumask_pr_args(cpu_online_mask));
261 }
262 
263 #ifdef CONFIG_KEXEC_CORE
264 /*
265  * The number of CPUs online, not counting this CPU (which may not be
266  * fully online and so not counted in num_online_cpus()).
267  */
268 static inline unsigned int num_other_online_cpus(void)
269 {
270 	unsigned int this_cpu_online = cpu_online(smp_processor_id());
271 
272 	return num_online_cpus() - this_cpu_online;
273 }
274 
275 void crash_smp_send_stop(void)
276 {
277 	static int cpus_stopped;
278 	cpumask_t mask;
279 	unsigned long timeout;
280 
281 	/*
282 	 * This function can be called twice in panic path, but obviously
283 	 * we execute this only once.
284 	 */
285 	if (cpus_stopped)
286 		return;
287 
288 	cpus_stopped = 1;
289 
290 	/*
291 	 * If this cpu is the only one alive at this point in time, online or
292 	 * not, there are no stop messages to be sent around, so just back out.
293 	 */
294 	if (num_other_online_cpus() == 0)
295 		return;
296 
297 	cpumask_copy(&mask, cpu_online_mask);
298 	cpumask_clear_cpu(smp_processor_id(), &mask);
299 
300 	atomic_set(&waiting_for_crash_ipi, num_other_online_cpus());
301 
302 	pr_crit("SMP: stopping secondary CPUs\n");
303 	send_ipi_mask(&mask, IPI_CPU_CRASH_STOP);
304 
305 	/* Wait up to one second for other CPUs to stop */
306 	timeout = USEC_PER_SEC;
307 	while ((atomic_read(&waiting_for_crash_ipi) > 0) && timeout--)
308 		udelay(1);
309 
310 	if (atomic_read(&waiting_for_crash_ipi) > 0)
311 		pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
312 			cpumask_pr_args(&mask));
313 }
314 
315 bool smp_crash_stop_failed(void)
316 {
317 	return (atomic_read(&waiting_for_crash_ipi) > 0);
318 }
319 #endif
320 
321 void arch_smp_send_reschedule(int cpu)
322 {
323 	send_ipi_single(cpu, IPI_RESCHEDULE);
324 }
325 EXPORT_SYMBOL_GPL(arch_smp_send_reschedule);
326