1 // SPDX-License-Identifier: GPL-2.0-only
3 * SMP initialisation and IPI support
4 * Based on arch/arm64/kernel/smp.c
6 * Copyright (C) 2012 ARM Ltd.
7 * Copyright (C) 2015 Regents of the University of California
8 * Copyright (C) 2017 SiFive
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/kgdb.h>
17 #include <linux/percpu.h>
18 #include <linux/profile.h>
19 #include <linux/smp.h>
20 #include <linux/sched.h>
21 #include <linux/seq_file.h>
22 #include <linux/delay.h>
23 #include <linux/irq.h>
24 #include <linux/irq_work.h>
25 #include <linux/nmi.h>
27 #include <asm/tlbflush.h>
28 #include <asm/cacheflush.h>
29 #include <asm/cpu_ops.h>
31 enum ipi_message_type
{
43 unsigned long __cpuid_to_hartid_map
[NR_CPUS
] __ro_after_init
= {
44 [0 ... NR_CPUS
-1] = INVALID_HARTID
47 void __init
smp_setup_processor_id(void)
49 cpuid_to_hartid_map(0) = boot_cpu_hartid
;
52 static DEFINE_PER_CPU_READ_MOSTLY(int, ipi_dummy_dev
);
53 static int ipi_virq_base __ro_after_init
;
54 static int nr_ipi __ro_after_init
= IPI_MAX
;
55 static struct irq_desc
*ipi_desc
[IPI_MAX
] __read_mostly
;
57 int riscv_hartid_to_cpuid(unsigned long hartid
)
61 for (i
= 0; i
< NR_CPUS
; i
++)
62 if (cpuid_to_hartid_map(i
) == hartid
)
68 static void ipi_stop(void)
70 set_cpu_online(smp_processor_id(), false);
75 #ifdef CONFIG_KEXEC_CORE
76 static atomic_t waiting_for_crash_ipi
= ATOMIC_INIT(0);
78 static inline void ipi_cpu_crash_stop(unsigned int cpu
, struct pt_regs
*regs
)
80 crash_save_cpu(regs
, cpu
);
82 atomic_dec(&waiting_for_crash_ipi
);
86 #ifdef CONFIG_HOTPLUG_CPU
87 if (cpu_has_hotplug(cpu
))
95 static inline void ipi_cpu_crash_stop(unsigned int cpu
, struct pt_regs
*regs
)
101 static void send_ipi_mask(const struct cpumask
*mask
, enum ipi_message_type op
)
103 __ipi_send_mask(ipi_desc
[op
], mask
);
106 static void send_ipi_single(int cpu
, enum ipi_message_type op
)
108 __ipi_send_mask(ipi_desc
[op
], cpumask_of(cpu
));
111 #ifdef CONFIG_IRQ_WORK
112 void arch_irq_work_raise(void)
114 send_ipi_single(smp_processor_id(), IPI_IRQ_WORK
);
118 static irqreturn_t
handle_IPI(int irq
, void *data
)
120 unsigned int cpu
= smp_processor_id();
121 int ipi
= irq
- ipi_virq_base
;
128 generic_smp_call_function_interrupt();
133 case IPI_CPU_CRASH_STOP
:
134 ipi_cpu_crash_stop(cpu
, get_irq_regs());
139 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
141 tick_receive_broadcast();
144 case IPI_CPU_BACKTRACE
:
145 nmi_cpu_backtrace(get_irq_regs());
147 case IPI_KGDB_ROUNDUP
:
148 kgdb_nmicallback(cpu
, get_irq_regs());
151 pr_warn("CPU%d: unhandled IPI%d\n", cpu
, ipi
);
158 void riscv_ipi_enable(void)
162 if (WARN_ON_ONCE(!ipi_virq_base
))
165 for (i
= 0; i
< nr_ipi
; i
++)
166 enable_percpu_irq(ipi_virq_base
+ i
, 0);
169 void riscv_ipi_disable(void)
173 if (WARN_ON_ONCE(!ipi_virq_base
))
176 for (i
= 0; i
< nr_ipi
; i
++)
177 disable_percpu_irq(ipi_virq_base
+ i
);
180 bool riscv_ipi_have_virq_range(void)
182 return (ipi_virq_base
) ? true : false;
185 void riscv_ipi_set_virq_range(int virq
, int nr
)
189 if (WARN_ON(ipi_virq_base
))
192 WARN_ON(nr
< IPI_MAX
);
193 nr_ipi
= min(nr
, IPI_MAX
);
194 ipi_virq_base
= virq
;
197 for (i
= 0; i
< nr_ipi
; i
++) {
198 err
= request_percpu_irq(ipi_virq_base
+ i
, handle_IPI
,
199 "IPI", &ipi_dummy_dev
);
202 ipi_desc
[i
] = irq_to_desc(ipi_virq_base
+ i
);
203 irq_set_status_flags(ipi_virq_base
+ i
, IRQ_HIDDEN
);
206 /* Enabled IPIs for boot CPU immediately */
210 static const char * const ipi_names
[] = {
211 [IPI_RESCHEDULE
] = "Rescheduling interrupts",
212 [IPI_CALL_FUNC
] = "Function call interrupts",
213 [IPI_CPU_STOP
] = "CPU stop interrupts",
214 [IPI_CPU_CRASH_STOP
] = "CPU stop (for crash dump) interrupts",
215 [IPI_IRQ_WORK
] = "IRQ work interrupts",
216 [IPI_TIMER
] = "Timer broadcast interrupts",
217 [IPI_CPU_BACKTRACE
] = "CPU backtrace interrupts",
218 [IPI_KGDB_ROUNDUP
] = "KGDB roundup interrupts",
221 void show_ipi_stats(struct seq_file
*p
, int prec
)
225 for (i
= 0; i
< IPI_MAX
; i
++) {
226 seq_printf(p
, "%*s%u:%s", prec
- 1, "IPI", i
,
227 prec
>= 4 ? " " : "");
228 for_each_online_cpu(cpu
)
229 seq_printf(p
, "%10u ", irq_desc_kstat_cpu(ipi_desc
[i
], cpu
));
230 seq_printf(p
, " %s\n", ipi_names
[i
]);
234 void arch_send_call_function_ipi_mask(struct cpumask
*mask
)
236 send_ipi_mask(mask
, IPI_CALL_FUNC
);
239 void arch_send_call_function_single_ipi(int cpu
)
241 send_ipi_single(cpu
, IPI_CALL_FUNC
);
244 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
245 void tick_broadcast(const struct cpumask
*mask
)
247 send_ipi_mask(mask
, IPI_TIMER
);
251 void smp_send_stop(void)
253 unsigned long timeout
;
255 if (num_online_cpus() > 1) {
258 cpumask_copy(&mask
, cpu_online_mask
);
259 cpumask_clear_cpu(smp_processor_id(), &mask
);
261 if (system_state
<= SYSTEM_RUNNING
)
262 pr_crit("SMP: stopping secondary CPUs\n");
263 send_ipi_mask(&mask
, IPI_CPU_STOP
);
266 /* Wait up to one second for other CPUs to stop */
267 timeout
= USEC_PER_SEC
;
268 while (num_online_cpus() > 1 && timeout
--)
271 if (num_online_cpus() > 1)
272 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
273 cpumask_pr_args(cpu_online_mask
));
276 #ifdef CONFIG_KEXEC_CORE
278 * The number of CPUs online, not counting this CPU (which may not be
279 * fully online and so not counted in num_online_cpus()).
281 static inline unsigned int num_other_online_cpus(void)
283 unsigned int this_cpu_online
= cpu_online(smp_processor_id());
285 return num_online_cpus() - this_cpu_online
;
288 void crash_smp_send_stop(void)
290 static int cpus_stopped
;
292 unsigned long timeout
;
295 * This function can be called twice in panic path, but obviously
296 * we execute this only once.
304 * If this cpu is the only one alive at this point in time, online or
305 * not, there are no stop messages to be sent around, so just back out.
307 if (num_other_online_cpus() == 0)
310 cpumask_copy(&mask
, cpu_online_mask
);
311 cpumask_clear_cpu(smp_processor_id(), &mask
);
313 atomic_set(&waiting_for_crash_ipi
, num_other_online_cpus());
315 pr_crit("SMP: stopping secondary CPUs\n");
316 send_ipi_mask(&mask
, IPI_CPU_CRASH_STOP
);
318 /* Wait up to one second for other CPUs to stop */
319 timeout
= USEC_PER_SEC
;
320 while ((atomic_read(&waiting_for_crash_ipi
) > 0) && timeout
--)
323 if (atomic_read(&waiting_for_crash_ipi
) > 0)
324 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
325 cpumask_pr_args(&mask
));
328 bool smp_crash_stop_failed(void)
330 return (atomic_read(&waiting_for_crash_ipi
) > 0);
334 void arch_smp_send_reschedule(int cpu
)
336 send_ipi_single(cpu
, IPI_RESCHEDULE
);
338 EXPORT_SYMBOL_GPL(arch_smp_send_reschedule
);
340 static void riscv_backtrace_ipi(cpumask_t
*mask
)
342 send_ipi_mask(mask
, IPI_CPU_BACKTRACE
);
345 void arch_trigger_cpumask_backtrace(const cpumask_t
*mask
, int exclude_cpu
)
347 nmi_trigger_cpumask_backtrace(mask
, exclude_cpu
, riscv_backtrace_ipi
);
351 void kgdb_roundup_cpus(void)
353 int this_cpu
= raw_smp_processor_id();
356 for_each_online_cpu(cpu
) {
357 /* No need to roundup ourselves */
361 send_ipi_single(cpu
, IPI_KGDB_ROUNDUP
);