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/interrupt.h>
12 #include <linux/smp.h>
13 #include <linux/sched.h>
14 #include <linux/seq_file.h>
15 #include <linux/delay.h>
18 #include <asm/tlbflush.h>
19 #include <asm/cacheflush.h>
21 enum ipi_message_type {
28 unsigned long __cpuid_to_hartid_map[NR_CPUS] = {
29 [0 ... NR_CPUS-1] = INVALID_HARTID
32 void __init smp_setup_processor_id(void)
34 cpuid_to_hartid_map(0) = boot_cpu_hartid;
37 /* A collection of single bit ipi messages. */
39 unsigned long stats[IPI_MAX] ____cacheline_aligned;
40 unsigned long bits ____cacheline_aligned;
41 } ipi_data[NR_CPUS] __cacheline_aligned;
43 int riscv_hartid_to_cpuid(int hartid)
47 for (i = 0; i < NR_CPUS; i++)
48 if (cpuid_to_hartid_map(i) == hartid)
51 pr_err("Couldn't find cpu id for hartid [%d]\n", hartid);
55 void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
61 cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
64 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
66 return phys_id == cpuid_to_hartid_map(cpu);
70 int setup_profiling_timer(unsigned int multiplier)
75 static void ipi_stop(void)
77 set_cpu_online(smp_processor_id(), false);
82 static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
84 struct cpumask hartid_mask;
87 smp_mb__before_atomic();
88 for_each_cpu(cpu, mask)
89 set_bit(op, &ipi_data[cpu].bits);
90 smp_mb__after_atomic();
92 riscv_cpuid_to_hartid_mask(mask, &hartid_mask);
93 sbi_send_ipi(cpumask_bits(&hartid_mask));
96 static void send_ipi_single(int cpu, enum ipi_message_type op)
98 int hartid = cpuid_to_hartid_map(cpu);
100 smp_mb__before_atomic();
101 set_bit(op, &ipi_data[cpu].bits);
102 smp_mb__after_atomic();
104 sbi_send_ipi(cpumask_bits(cpumask_of(hartid)));
107 static inline void clear_ipi(void)
109 csr_clear(CSR_SIP, SIE_SSIE);
112 void riscv_software_interrupt(void)
114 unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
115 unsigned long *stats = ipi_data[smp_processor_id()].stats;
122 /* Order bit clearing and data access. */
125 ops = xchg(pending_ipis, 0);
129 if (ops & (1 << IPI_RESCHEDULE)) {
130 stats[IPI_RESCHEDULE]++;
134 if (ops & (1 << IPI_CALL_FUNC)) {
135 stats[IPI_CALL_FUNC]++;
136 generic_smp_call_function_interrupt();
139 if (ops & (1 << IPI_CPU_STOP)) {
140 stats[IPI_CPU_STOP]++;
144 BUG_ON((ops >> IPI_MAX) != 0);
146 /* Order data access and bit testing. */
151 static const char * const ipi_names[] = {
152 [IPI_RESCHEDULE] = "Rescheduling interrupts",
153 [IPI_CALL_FUNC] = "Function call interrupts",
154 [IPI_CPU_STOP] = "CPU stop interrupts",
157 void show_ipi_stats(struct seq_file *p, int prec)
161 for (i = 0; i < IPI_MAX; i++) {
162 seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
163 prec >= 4 ? " " : "");
164 for_each_online_cpu(cpu)
165 seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
166 seq_printf(p, " %s\n", ipi_names[i]);
170 void arch_send_call_function_ipi_mask(struct cpumask *mask)
172 send_ipi_mask(mask, IPI_CALL_FUNC);
175 void arch_send_call_function_single_ipi(int cpu)
177 send_ipi_single(cpu, IPI_CALL_FUNC);
180 void smp_send_stop(void)
182 unsigned long timeout;
184 if (num_online_cpus() > 1) {
187 cpumask_copy(&mask, cpu_online_mask);
188 cpumask_clear_cpu(smp_processor_id(), &mask);
190 if (system_state <= SYSTEM_RUNNING)
191 pr_crit("SMP: stopping secondary CPUs\n");
192 send_ipi_mask(&mask, IPI_CPU_STOP);
195 /* Wait up to one second for other CPUs to stop */
196 timeout = USEC_PER_SEC;
197 while (num_online_cpus() > 1 && timeout--)
200 if (num_online_cpus() > 1)
201 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
202 cpumask_pr_args(cpu_online_mask));
205 void smp_send_reschedule(int cpu)
207 send_ipi_single(cpu, IPI_RESCHEDULE);