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/profile.h>
16 #include <linux/smp.h>
17 #include <linux/sched.h>
18 #include <linux/seq_file.h>
19 #include <linux/delay.h>
20 #include <linux/irq_work.h>
23 #include <asm/tlbflush.h>
24 #include <asm/cacheflush.h>
26 enum ipi_message_type {
35 unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
36 [0 ... NR_CPUS-1] = INVALID_HARTID
39 void __init smp_setup_processor_id(void)
41 cpuid_to_hartid_map(0) = boot_cpu_hartid;
44 /* A collection of single bit ipi messages. */
46 unsigned long stats[IPI_MAX] ____cacheline_aligned;
47 unsigned long bits ____cacheline_aligned;
48 } ipi_data[NR_CPUS] __cacheline_aligned;
50 int riscv_hartid_to_cpuid(int hartid)
54 for (i = 0; i < NR_CPUS; i++)
55 if (cpuid_to_hartid_map(i) == hartid)
58 pr_err("Couldn't find cpu id for hartid [%d]\n", hartid);
62 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
64 return phys_id == cpuid_to_hartid_map(cpu);
68 int setup_profiling_timer(unsigned int multiplier)
73 static void ipi_stop(void)
75 set_cpu_online(smp_processor_id(), false);
80 static const struct riscv_ipi_ops *ipi_ops __ro_after_init;
82 void riscv_set_ipi_ops(const struct riscv_ipi_ops *ops)
86 EXPORT_SYMBOL_GPL(riscv_set_ipi_ops);
88 void riscv_clear_ipi(void)
90 if (ipi_ops && ipi_ops->ipi_clear)
93 csr_clear(CSR_IP, IE_SIE);
95 EXPORT_SYMBOL_GPL(riscv_clear_ipi);
97 static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
101 smp_mb__before_atomic();
102 for_each_cpu(cpu, mask)
103 set_bit(op, &ipi_data[cpu].bits);
104 smp_mb__after_atomic();
106 if (ipi_ops && ipi_ops->ipi_inject)
107 ipi_ops->ipi_inject(mask);
109 pr_warn("SMP: IPI inject method not available\n");
112 static void send_ipi_single(int cpu, enum ipi_message_type op)
114 smp_mb__before_atomic();
115 set_bit(op, &ipi_data[cpu].bits);
116 smp_mb__after_atomic();
118 if (ipi_ops && ipi_ops->ipi_inject)
119 ipi_ops->ipi_inject(cpumask_of(cpu));
121 pr_warn("SMP: IPI inject method not available\n");
124 #ifdef CONFIG_IRQ_WORK
125 void arch_irq_work_raise(void)
127 send_ipi_single(smp_processor_id(), IPI_IRQ_WORK);
131 void handle_IPI(struct pt_regs *regs)
133 unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
134 unsigned long *stats = ipi_data[smp_processor_id()].stats;
141 /* Order bit clearing and data access. */
144 ops = xchg(pending_ipis, 0);
148 if (ops & (1 << IPI_RESCHEDULE)) {
149 stats[IPI_RESCHEDULE]++;
153 if (ops & (1 << IPI_CALL_FUNC)) {
154 stats[IPI_CALL_FUNC]++;
155 generic_smp_call_function_interrupt();
158 if (ops & (1 << IPI_CPU_STOP)) {
159 stats[IPI_CPU_STOP]++;
163 if (ops & (1 << IPI_IRQ_WORK)) {
164 stats[IPI_IRQ_WORK]++;
168 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
169 if (ops & (1 << IPI_TIMER)) {
171 tick_receive_broadcast();
174 BUG_ON((ops >> IPI_MAX) != 0);
176 /* Order data access and bit testing. */
181 static const char * const ipi_names[] = {
182 [IPI_RESCHEDULE] = "Rescheduling interrupts",
183 [IPI_CALL_FUNC] = "Function call interrupts",
184 [IPI_CPU_STOP] = "CPU stop interrupts",
185 [IPI_IRQ_WORK] = "IRQ work interrupts",
186 [IPI_TIMER] = "Timer broadcast interrupts",
189 void show_ipi_stats(struct seq_file *p, int prec)
193 for (i = 0; i < IPI_MAX; i++) {
194 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
195 prec >= 4 ? " " : "");
196 for_each_online_cpu(cpu)
197 seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
198 seq_printf(p, " %s\n", ipi_names[i]);
202 void arch_send_call_function_ipi_mask(struct cpumask *mask)
204 send_ipi_mask(mask, IPI_CALL_FUNC);
207 void arch_send_call_function_single_ipi(int cpu)
209 send_ipi_single(cpu, IPI_CALL_FUNC);
212 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
213 void tick_broadcast(const struct cpumask *mask)
215 send_ipi_mask(mask, IPI_TIMER);
219 void smp_send_stop(void)
221 unsigned long timeout;
223 if (num_online_cpus() > 1) {
226 cpumask_copy(&mask, cpu_online_mask);
227 cpumask_clear_cpu(smp_processor_id(), &mask);
229 if (system_state <= SYSTEM_RUNNING)
230 pr_crit("SMP: stopping secondary CPUs\n");
231 send_ipi_mask(&mask, IPI_CPU_STOP);
234 /* Wait up to one second for other CPUs to stop */
235 timeout = USEC_PER_SEC;
236 while (num_online_cpus() > 1 && timeout--)
239 if (num_online_cpus() > 1)
240 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
241 cpumask_pr_args(cpu_online_mask));
244 void smp_send_reschedule(int cpu)
246 send_ipi_single(cpu, IPI_RESCHEDULE);
248 EXPORT_SYMBOL_GPL(smp_send_reschedule);