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/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>
21
22 #include <asm/sbi.h>
23 #include <asm/tlbflush.h>
24 #include <asm/cacheflush.h>
25
26 enum ipi_message_type {
27 IPI_RESCHEDULE,
28 IPI_CALL_FUNC,
29 IPI_CPU_STOP,
30 IPI_IRQ_WORK,
31 IPI_TIMER,
32 IPI_MAX
33 };
34
35 unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
36 [0 ... NR_CPUS-1] = INVALID_HARTID
37 };
38
smp_setup_processor_id(void)39 void __init smp_setup_processor_id(void)
40 {
41 cpuid_to_hartid_map(0) = boot_cpu_hartid;
42 }
43
44 /* A collection of single bit ipi messages. */
45 static struct {
46 unsigned long stats[IPI_MAX] ____cacheline_aligned;
47 unsigned long bits ____cacheline_aligned;
48 } ipi_data[NR_CPUS] __cacheline_aligned;
49
riscv_hartid_to_cpuid(int hartid)50 int riscv_hartid_to_cpuid(int hartid)
51 {
52 int i;
53
54 for (i = 0; i < NR_CPUS; i++)
55 if (cpuid_to_hartid_map(i) == hartid)
56 return i;
57
58 pr_err("Couldn't find cpu id for hartid [%d]\n", hartid);
59 return -ENOENT;
60 }
61
arch_match_cpu_phys_id(int cpu,u64 phys_id)62 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
63 {
64 return phys_id == cpuid_to_hartid_map(cpu);
65 }
66
67 /* Unsupported */
setup_profiling_timer(unsigned int multiplier)68 int setup_profiling_timer(unsigned int multiplier)
69 {
70 return -EINVAL;
71 }
72
ipi_stop(void)73 static void ipi_stop(void)
74 {
75 set_cpu_online(smp_processor_id(), false);
76 while (1)
77 wait_for_interrupt();
78 }
79
80 static const struct riscv_ipi_ops *ipi_ops __ro_after_init;
81
riscv_set_ipi_ops(const struct riscv_ipi_ops * ops)82 void riscv_set_ipi_ops(const struct riscv_ipi_ops *ops)
83 {
84 ipi_ops = ops;
85 }
86 EXPORT_SYMBOL_GPL(riscv_set_ipi_ops);
87
riscv_clear_ipi(void)88 void riscv_clear_ipi(void)
89 {
90 if (ipi_ops && ipi_ops->ipi_clear)
91 ipi_ops->ipi_clear();
92
93 csr_clear(CSR_IP, IE_SIE);
94 }
95 EXPORT_SYMBOL_GPL(riscv_clear_ipi);
96
send_ipi_mask(const struct cpumask * mask,enum ipi_message_type op)97 static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
98 {
99 int cpu;
100
101 smp_mb__before_atomic();
102 for_each_cpu(cpu, mask)
103 set_bit(op, &ipi_data[cpu].bits);
104 smp_mb__after_atomic();
105
106 if (ipi_ops && ipi_ops->ipi_inject)
107 ipi_ops->ipi_inject(mask);
108 else
109 pr_warn("SMP: IPI inject method not available\n");
110 }
111
send_ipi_single(int cpu,enum ipi_message_type op)112 static void send_ipi_single(int cpu, enum ipi_message_type op)
113 {
114 smp_mb__before_atomic();
115 set_bit(op, &ipi_data[cpu].bits);
116 smp_mb__after_atomic();
117
118 if (ipi_ops && ipi_ops->ipi_inject)
119 ipi_ops->ipi_inject(cpumask_of(cpu));
120 else
121 pr_warn("SMP: IPI inject method not available\n");
122 }
123
124 #ifdef CONFIG_IRQ_WORK
arch_irq_work_raise(void)125 void arch_irq_work_raise(void)
126 {
127 send_ipi_single(smp_processor_id(), IPI_IRQ_WORK);
128 }
129 #endif
130
handle_IPI(struct pt_regs * regs)131 void handle_IPI(struct pt_regs *regs)
132 {
133 struct pt_regs *old_regs = set_irq_regs(regs);
134 unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
135 unsigned long *stats = ipi_data[smp_processor_id()].stats;
136
137 irq_enter();
138
139 riscv_clear_ipi();
140
141 while (true) {
142 unsigned long ops;
143
144 /* Order bit clearing and data access. */
145 mb();
146
147 ops = xchg(pending_ipis, 0);
148 if (ops == 0)
149 goto done;
150
151 if (ops & (1 << IPI_RESCHEDULE)) {
152 stats[IPI_RESCHEDULE]++;
153 scheduler_ipi();
154 }
155
156 if (ops & (1 << IPI_CALL_FUNC)) {
157 stats[IPI_CALL_FUNC]++;
158 generic_smp_call_function_interrupt();
159 }
160
161 if (ops & (1 << IPI_CPU_STOP)) {
162 stats[IPI_CPU_STOP]++;
163 ipi_stop();
164 }
165
166 if (ops & (1 << IPI_IRQ_WORK)) {
167 stats[IPI_IRQ_WORK]++;
168 irq_work_run();
169 }
170
171 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
172 if (ops & (1 << IPI_TIMER)) {
173 stats[IPI_TIMER]++;
174 tick_receive_broadcast();
175 }
176 #endif
177 BUG_ON((ops >> IPI_MAX) != 0);
178
179 /* Order data access and bit testing. */
180 mb();
181 }
182
183 done:
184 irq_exit();
185 set_irq_regs(old_regs);
186 }
187
188 static const char * const ipi_names[] = {
189 [IPI_RESCHEDULE] = "Rescheduling interrupts",
190 [IPI_CALL_FUNC] = "Function call interrupts",
191 [IPI_CPU_STOP] = "CPU stop interrupts",
192 [IPI_IRQ_WORK] = "IRQ work interrupts",
193 [IPI_TIMER] = "Timer broadcast interrupts",
194 };
195
show_ipi_stats(struct seq_file * p,int prec)196 void show_ipi_stats(struct seq_file *p, int prec)
197 {
198 unsigned int cpu, i;
199
200 for (i = 0; i < IPI_MAX; i++) {
201 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
202 prec >= 4 ? " " : "");
203 for_each_online_cpu(cpu)
204 seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
205 seq_printf(p, " %s\n", ipi_names[i]);
206 }
207 }
208
arch_send_call_function_ipi_mask(struct cpumask * mask)209 void arch_send_call_function_ipi_mask(struct cpumask *mask)
210 {
211 send_ipi_mask(mask, IPI_CALL_FUNC);
212 }
213
arch_send_call_function_single_ipi(int cpu)214 void arch_send_call_function_single_ipi(int cpu)
215 {
216 send_ipi_single(cpu, IPI_CALL_FUNC);
217 }
218
219 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
tick_broadcast(const struct cpumask * mask)220 void tick_broadcast(const struct cpumask *mask)
221 {
222 send_ipi_mask(mask, IPI_TIMER);
223 }
224 #endif
225
smp_send_stop(void)226 void smp_send_stop(void)
227 {
228 unsigned long timeout;
229
230 if (num_online_cpus() > 1) {
231 cpumask_t mask;
232
233 cpumask_copy(&mask, cpu_online_mask);
234 cpumask_clear_cpu(smp_processor_id(), &mask);
235
236 if (system_state <= SYSTEM_RUNNING)
237 pr_crit("SMP: stopping secondary CPUs\n");
238 send_ipi_mask(&mask, IPI_CPU_STOP);
239 }
240
241 /* Wait up to one second for other CPUs to stop */
242 timeout = USEC_PER_SEC;
243 while (num_online_cpus() > 1 && timeout--)
244 udelay(1);
245
246 if (num_online_cpus() > 1)
247 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
248 cpumask_pr_args(cpu_online_mask));
249 }
250
smp_send_reschedule(int cpu)251 void smp_send_reschedule(int cpu)
252 {
253 send_ipi_single(cpu, IPI_RESCHEDULE);
254 }
255 EXPORT_SYMBOL_GPL(smp_send_reschedule);
256