blob: 366cb87c0e2efdf9998efadcb4f375f73bfed5e0 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07002/*
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
Palmer Dabbelt76d2a042017-07-10 18:00:26 -07009 */
10
Paul Walmsley5ed881b2019-10-17 15:21:28 -070011#include <linux/cpu.h>
Guo Ren2f100582021-03-07 10:24:46 +080012#include <linux/clockchips.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070013#include <linux/interrupt.h>
Anup Patel7391efa2020-04-24 10:29:26 +053014#include <linux/module.h>
Paul Walmsley5ed881b2019-10-17 15:21:28 -070015#include <linux/profile.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070016#include <linux/smp.h>
17#include <linux/sched.h>
Anup Patel8b20d2d2018-10-02 12:15:07 -070018#include <linux/seq_file.h>
Andreas Schwab37a107f2018-12-11 11:20:40 +010019#include <linux/delay.h>
Greentime Hu29844792020-06-24 17:03:15 +080020#include <linux/irq_work.h>
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070021
22#include <asm/sbi.h>
23#include <asm/tlbflush.h>
24#include <asm/cacheflush.h>
25
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070026enum ipi_message_type {
27 IPI_RESCHEDULE,
28 IPI_CALL_FUNC,
Andreas Schwab37a107f2018-12-11 11:20:40 +010029 IPI_CPU_STOP,
Greentime Hu29844792020-06-24 17:03:15 +080030 IPI_IRQ_WORK,
Guo Ren2f100582021-03-07 10:24:46 +080031 IPI_TIMER,
Palmer Dabbelt76d2a042017-07-10 18:00:26 -070032 IPI_MAX
33};
34
Jisheng Zhangde31ea42021-03-30 02:22:51 +080035unsigned long __cpuid_to_hartid_map[NR_CPUS] __ro_after_init = {
Atish Patra78d1daa2019-02-22 11:41:36 -080036 [0 ... NR_CPUS-1] = INVALID_HARTID
37};
38
39void __init smp_setup_processor_id(void)
40{
Atish Patraf1f47c62019-04-24 14:48:01 -070041 cpuid_to_hartid_map(0) = boot_cpu_hartid;
Atish Patra78d1daa2019-02-22 11:41:36 -080042}
43
Anup Patel8b20d2d2018-10-02 12:15:07 -070044/* A collection of single bit ipi messages. */
45static struct {
46 unsigned long stats[IPI_MAX] ____cacheline_aligned;
47 unsigned long bits ____cacheline_aligned;
48} ipi_data[NR_CPUS] __cacheline_aligned;
49
Atish Patra6825c7a2018-10-02 12:15:04 -070050int riscv_hartid_to_cpuid(int hartid)
51{
Atish Patraf1f47c62019-04-24 14:48:01 -070052 int i;
Olof Johansson4bde6322017-11-29 17:55:17 -080053
Atish Patra6825c7a2018-10-02 12:15:04 -070054 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);
Atish Patra6825c7a2018-10-02 12:15:04 -070059 return i;
60}
61
62void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
63{
64 int cpu;
65
Christoph Hellwigf5bf6452019-08-21 23:58:35 +090066 cpumask_clear(out);
Atish Patra6825c7a2018-10-02 12:15:04 -070067 for_each_cpu(cpu, in)
68 cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
69}
Anup Patel7391efa2020-04-24 10:29:26 +053070EXPORT_SYMBOL_GPL(riscv_cpuid_to_hartid_mask);
Atish Patra70114562019-04-24 14:47:58 -070071
72bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
73{
74 return phys_id == cpuid_to_hartid_map(cpu);
75}
76
Olof Johansson4bde6322017-11-29 17:55:17 -080077/* Unsupported */
78int setup_profiling_timer(unsigned int multiplier)
79{
80 return -EINVAL;
81}
82
Andreas Schwab37a107f2018-12-11 11:20:40 +010083static void ipi_stop(void)
84{
85 set_cpu_online(smp_processor_id(), false);
86 while (1)
87 wait_for_interrupt();
88}
89
Jisheng Zhang300f62c2021-03-30 02:23:54 +080090static const struct riscv_ipi_ops *ipi_ops __ro_after_init;
Anup Patelcc7f3f72020-08-17 18:12:48 +053091
Jisheng Zhang300f62c2021-03-30 02:23:54 +080092void riscv_set_ipi_ops(const struct riscv_ipi_ops *ops)
Anup Patelcc7f3f72020-08-17 18:12:48 +053093{
94 ipi_ops = ops;
95}
96EXPORT_SYMBOL_GPL(riscv_set_ipi_ops);
97
98void riscv_clear_ipi(void)
99{
100 if (ipi_ops && ipi_ops->ipi_clear)
101 ipi_ops->ipi_clear();
102
103 csr_clear(CSR_IP, IE_SIE);
104}
105EXPORT_SYMBOL_GPL(riscv_clear_ipi);
106
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900107static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
108{
Christoph Hellwig1db7a7c2019-08-21 23:58:33 +0900109 int cpu;
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900110
Christoph Hellwig1db7a7c2019-08-21 23:58:33 +0900111 smp_mb__before_atomic();
112 for_each_cpu(cpu, mask)
113 set_bit(op, &ipi_data[cpu].bits);
114 smp_mb__after_atomic();
115
Anup Patelcc7f3f72020-08-17 18:12:48 +0530116 if (ipi_ops && ipi_ops->ipi_inject)
117 ipi_ops->ipi_inject(mask);
Christoph Hellwigfcdc6532019-10-28 13:10:38 +0100118 else
Anup Patelcc7f3f72020-08-17 18:12:48 +0530119 pr_warn("SMP: IPI inject method not available\n");
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900120}
121
122static void send_ipi_single(int cpu, enum ipi_message_type op)
123{
Christoph Hellwige11ea2a2019-08-21 23:58:34 +0900124 smp_mb__before_atomic();
125 set_bit(op, &ipi_data[cpu].bits);
126 smp_mb__after_atomic();
127
Anup Patelcc7f3f72020-08-17 18:12:48 +0530128 if (ipi_ops && ipi_ops->ipi_inject)
129 ipi_ops->ipi_inject(cpumask_of(cpu));
Christoph Hellwigfcdc6532019-10-28 13:10:38 +0100130 else
Anup Patelcc7f3f72020-08-17 18:12:48 +0530131 pr_warn("SMP: IPI inject method not available\n");
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900132}
133
Greentime Hu29844792020-06-24 17:03:15 +0800134#ifdef CONFIG_IRQ_WORK
135void arch_irq_work_raise(void)
136{
137 send_ipi_single(smp_processor_id(), IPI_IRQ_WORK);
138}
139#endif
140
Anup Patel5cf998b2020-06-01 14:45:38 +0530141void handle_IPI(struct pt_regs *regs)
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700142{
Anup Patel5cf998b2020-06-01 14:45:38 +0530143 struct pt_regs *old_regs = set_irq_regs(regs);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700144 unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
Anup Patel8b20d2d2018-10-02 12:15:07 -0700145 unsigned long *stats = ipi_data[smp_processor_id()].stats;
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700146
Anup Patel5cf998b2020-06-01 14:45:38 +0530147 irq_enter();
148
Anup Patelcc7f3f72020-08-17 18:12:48 +0530149 riscv_clear_ipi();
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700150
151 while (true) {
152 unsigned long ops;
153
154 /* Order bit clearing and data access. */
155 mb();
156
157 ops = xchg(pending_ipis, 0);
158 if (ops == 0)
Anup Patel5cf998b2020-06-01 14:45:38 +0530159 goto done;
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700160
Anup Patel8b20d2d2018-10-02 12:15:07 -0700161 if (ops & (1 << IPI_RESCHEDULE)) {
162 stats[IPI_RESCHEDULE]++;
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700163 scheduler_ipi();
Anup Patel8b20d2d2018-10-02 12:15:07 -0700164 }
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700165
Anup Patel8b20d2d2018-10-02 12:15:07 -0700166 if (ops & (1 << IPI_CALL_FUNC)) {
167 stats[IPI_CALL_FUNC]++;
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700168 generic_smp_call_function_interrupt();
Anup Patel8b20d2d2018-10-02 12:15:07 -0700169 }
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700170
Andreas Schwab37a107f2018-12-11 11:20:40 +0100171 if (ops & (1 << IPI_CPU_STOP)) {
172 stats[IPI_CPU_STOP]++;
173 ipi_stop();
174 }
175
Greentime Hu29844792020-06-24 17:03:15 +0800176 if (ops & (1 << IPI_IRQ_WORK)) {
177 stats[IPI_IRQ_WORK]++;
178 irq_work_run();
179 }
180
Guo Ren2f100582021-03-07 10:24:46 +0800181#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
182 if (ops & (1 << IPI_TIMER)) {
183 stats[IPI_TIMER]++;
184 tick_receive_broadcast();
185 }
186#endif
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700187 BUG_ON((ops >> IPI_MAX) != 0);
188
189 /* Order data access and bit testing. */
190 mb();
191 }
Anup Patel5cf998b2020-06-01 14:45:38 +0530192
193done:
194 irq_exit();
195 set_irq_regs(old_regs);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700196}
197
Anup Patel8b20d2d2018-10-02 12:15:07 -0700198static const char * const ipi_names[] = {
199 [IPI_RESCHEDULE] = "Rescheduling interrupts",
200 [IPI_CALL_FUNC] = "Function call interrupts",
Andreas Schwab37a107f2018-12-11 11:20:40 +0100201 [IPI_CPU_STOP] = "CPU stop interrupts",
Greentime Hu29844792020-06-24 17:03:15 +0800202 [IPI_IRQ_WORK] = "IRQ work interrupts",
Guo Ren2f100582021-03-07 10:24:46 +0800203 [IPI_TIMER] = "Timer broadcast interrupts",
Anup Patel8b20d2d2018-10-02 12:15:07 -0700204};
205
206void show_ipi_stats(struct seq_file *p, int prec)
207{
208 unsigned int cpu, i;
209
210 for (i = 0; i < IPI_MAX; i++) {
211 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
212 prec >= 4 ? " " : "");
213 for_each_online_cpu(cpu)
214 seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
215 seq_printf(p, " %s\n", ipi_names[i]);
216 }
217}
218
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700219void arch_send_call_function_ipi_mask(struct cpumask *mask)
220{
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900221 send_ipi_mask(mask, IPI_CALL_FUNC);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700222}
223
224void arch_send_call_function_single_ipi(int cpu)
225{
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900226 send_ipi_single(cpu, IPI_CALL_FUNC);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700227}
228
Guo Ren2f100582021-03-07 10:24:46 +0800229#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
230void tick_broadcast(const struct cpumask *mask)
231{
232 send_ipi_mask(mask, IPI_TIMER);
233}
234#endif
235
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700236void smp_send_stop(void)
237{
Andreas Schwab37a107f2018-12-11 11:20:40 +0100238 unsigned long timeout;
239
240 if (num_online_cpus() > 1) {
241 cpumask_t mask;
242
243 cpumask_copy(&mask, cpu_online_mask);
244 cpumask_clear_cpu(smp_processor_id(), &mask);
245
246 if (system_state <= SYSTEM_RUNNING)
247 pr_crit("SMP: stopping secondary CPUs\n");
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900248 send_ipi_mask(&mask, IPI_CPU_STOP);
Andreas Schwab37a107f2018-12-11 11:20:40 +0100249 }
250
251 /* Wait up to one second for other CPUs to stop */
252 timeout = USEC_PER_SEC;
253 while (num_online_cpus() > 1 && timeout--)
254 udelay(1);
255
256 if (num_online_cpus() > 1)
257 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
258 cpumask_pr_args(cpu_online_mask));
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700259}
260
261void smp_send_reschedule(int cpu)
262{
Christoph Hellwig7e0e5082019-08-21 23:58:32 +0900263 send_ipi_single(cpu, IPI_RESCHEDULE);
Palmer Dabbelt76d2a042017-07-10 18:00:26 -0700264}
Atish Patrad3d7a0c2019-09-04 16:14:06 +0000265EXPORT_SYMBOL_GPL(smp_send_reschedule);