xref: /openbmc/linux/arch/riscv/kernel/smp.c (revision 9726bfcd)
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/interrupt.h>
12 #include <linux/smp.h>
13 #include <linux/sched.h>
14 #include <linux/seq_file.h>
15 #include <linux/delay.h>
16 
17 #include <asm/sbi.h>
18 #include <asm/tlbflush.h>
19 #include <asm/cacheflush.h>
20 
21 enum ipi_message_type {
22 	IPI_RESCHEDULE,
23 	IPI_CALL_FUNC,
24 	IPI_CPU_STOP,
25 	IPI_MAX
26 };
27 
28 unsigned long __cpuid_to_hartid_map[NR_CPUS] = {
29 	[0 ... NR_CPUS-1] = INVALID_HARTID
30 };
31 
32 void __init smp_setup_processor_id(void)
33 {
34 	cpuid_to_hartid_map(0) = boot_cpu_hartid;
35 }
36 
37 /* A collection of single bit ipi messages.  */
38 static struct {
39 	unsigned long stats[IPI_MAX] ____cacheline_aligned;
40 	unsigned long bits ____cacheline_aligned;
41 } ipi_data[NR_CPUS] __cacheline_aligned;
42 
43 int riscv_hartid_to_cpuid(int hartid)
44 {
45 	int i;
46 
47 	for (i = 0; i < NR_CPUS; i++)
48 		if (cpuid_to_hartid_map(i) == hartid)
49 			return i;
50 
51 	pr_err("Couldn't find cpu id for hartid [%d]\n", hartid);
52 	return i;
53 }
54 
55 void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
56 {
57 	int cpu;
58 
59 	for_each_cpu(cpu, in)
60 		cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
61 }
62 
63 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
64 {
65 	return phys_id == cpuid_to_hartid_map(cpu);
66 }
67 
68 /* Unsupported */
69 int setup_profiling_timer(unsigned int multiplier)
70 {
71 	return -EINVAL;
72 }
73 
74 static void ipi_stop(void)
75 {
76 	set_cpu_online(smp_processor_id(), false);
77 	while (1)
78 		wait_for_interrupt();
79 }
80 
81 void riscv_software_interrupt(void)
82 {
83 	unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
84 	unsigned long *stats = ipi_data[smp_processor_id()].stats;
85 
86 	/* Clear pending IPI */
87 	csr_clear(CSR_SIP, SIE_SSIE);
88 
89 	while (true) {
90 		unsigned long ops;
91 
92 		/* Order bit clearing and data access. */
93 		mb();
94 
95 		ops = xchg(pending_ipis, 0);
96 		if (ops == 0)
97 			return;
98 
99 		if (ops & (1 << IPI_RESCHEDULE)) {
100 			stats[IPI_RESCHEDULE]++;
101 			scheduler_ipi();
102 		}
103 
104 		if (ops & (1 << IPI_CALL_FUNC)) {
105 			stats[IPI_CALL_FUNC]++;
106 			generic_smp_call_function_interrupt();
107 		}
108 
109 		if (ops & (1 << IPI_CPU_STOP)) {
110 			stats[IPI_CPU_STOP]++;
111 			ipi_stop();
112 		}
113 
114 		BUG_ON((ops >> IPI_MAX) != 0);
115 
116 		/* Order data access and bit testing. */
117 		mb();
118 	}
119 }
120 
121 static void
122 send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
123 {
124 	int cpuid, hartid;
125 	struct cpumask hartid_mask;
126 
127 	cpumask_clear(&hartid_mask);
128 	mb();
129 	for_each_cpu(cpuid, to_whom) {
130 		set_bit(operation, &ipi_data[cpuid].bits);
131 		hartid = cpuid_to_hartid_map(cpuid);
132 		cpumask_set_cpu(hartid, &hartid_mask);
133 	}
134 	mb();
135 	sbi_send_ipi(cpumask_bits(&hartid_mask));
136 }
137 
138 static const char * const ipi_names[] = {
139 	[IPI_RESCHEDULE]	= "Rescheduling interrupts",
140 	[IPI_CALL_FUNC]		= "Function call interrupts",
141 	[IPI_CPU_STOP]		= "CPU stop interrupts",
142 };
143 
144 void show_ipi_stats(struct seq_file *p, int prec)
145 {
146 	unsigned int cpu, i;
147 
148 	for (i = 0; i < IPI_MAX; i++) {
149 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
150 			   prec >= 4 ? " " : "");
151 		for_each_online_cpu(cpu)
152 			seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
153 		seq_printf(p, " %s\n", ipi_names[i]);
154 	}
155 }
156 
157 void arch_send_call_function_ipi_mask(struct cpumask *mask)
158 {
159 	send_ipi_message(mask, IPI_CALL_FUNC);
160 }
161 
162 void arch_send_call_function_single_ipi(int cpu)
163 {
164 	send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
165 }
166 
167 void smp_send_stop(void)
168 {
169 	unsigned long timeout;
170 
171 	if (num_online_cpus() > 1) {
172 		cpumask_t mask;
173 
174 		cpumask_copy(&mask, cpu_online_mask);
175 		cpumask_clear_cpu(smp_processor_id(), &mask);
176 
177 		if (system_state <= SYSTEM_RUNNING)
178 			pr_crit("SMP: stopping secondary CPUs\n");
179 		send_ipi_message(&mask, IPI_CPU_STOP);
180 	}
181 
182 	/* Wait up to one second for other CPUs to stop */
183 	timeout = USEC_PER_SEC;
184 	while (num_online_cpus() > 1 && timeout--)
185 		udelay(1);
186 
187 	if (num_online_cpus() > 1)
188 		pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
189 			   cpumask_pr_args(cpu_online_mask));
190 }
191 
192 void smp_send_reschedule(int cpu)
193 {
194 	send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
195 }
196 
197