xref: /openbmc/linux/arch/riscv/kernel/smp.c (revision 457c8996)
1 /*
2  * SMP initialisation and IPI support
3  * Based on arch/arm64/kernel/smp.c
4  *
5  * Copyright (C) 2012 ARM Ltd.
6  * Copyright (C) 2015 Regents of the University of California
7  * Copyright (C) 2017 SiFive
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <linux/interrupt.h>
23 #include <linux/smp.h>
24 #include <linux/sched.h>
25 #include <linux/seq_file.h>
26 #include <linux/delay.h>
27 
28 #include <asm/sbi.h>
29 #include <asm/tlbflush.h>
30 #include <asm/cacheflush.h>
31 
32 enum ipi_message_type {
33 	IPI_RESCHEDULE,
34 	IPI_CALL_FUNC,
35 	IPI_CPU_STOP,
36 	IPI_MAX
37 };
38 
39 unsigned long __cpuid_to_hartid_map[NR_CPUS] = {
40 	[0 ... NR_CPUS-1] = INVALID_HARTID
41 };
42 
43 void __init smp_setup_processor_id(void)
44 {
45 	cpuid_to_hartid_map(0) = boot_cpu_hartid;
46 }
47 
48 /* A collection of single bit ipi messages.  */
49 static struct {
50 	unsigned long stats[IPI_MAX] ____cacheline_aligned;
51 	unsigned long bits ____cacheline_aligned;
52 } ipi_data[NR_CPUS] __cacheline_aligned;
53 
54 int riscv_hartid_to_cpuid(int hartid)
55 {
56 	int i;
57 
58 	for (i = 0; i < NR_CPUS; i++)
59 		if (cpuid_to_hartid_map(i) == hartid)
60 			return i;
61 
62 	pr_err("Couldn't find cpu id for hartid [%d]\n", hartid);
63 	return i;
64 }
65 
66 void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out)
67 {
68 	int cpu;
69 
70 	for_each_cpu(cpu, in)
71 		cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
72 }
73 
74 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
75 {
76 	return phys_id == cpuid_to_hartid_map(cpu);
77 }
78 
79 /* Unsupported */
80 int setup_profiling_timer(unsigned int multiplier)
81 {
82 	return -EINVAL;
83 }
84 
85 static void ipi_stop(void)
86 {
87 	set_cpu_online(smp_processor_id(), false);
88 	while (1)
89 		wait_for_interrupt();
90 }
91 
92 void riscv_software_interrupt(void)
93 {
94 	unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
95 	unsigned long *stats = ipi_data[smp_processor_id()].stats;
96 
97 	/* Clear pending IPI */
98 	csr_clear(CSR_SIP, SIE_SSIE);
99 
100 	while (true) {
101 		unsigned long ops;
102 
103 		/* Order bit clearing and data access. */
104 		mb();
105 
106 		ops = xchg(pending_ipis, 0);
107 		if (ops == 0)
108 			return;
109 
110 		if (ops & (1 << IPI_RESCHEDULE)) {
111 			stats[IPI_RESCHEDULE]++;
112 			scheduler_ipi();
113 		}
114 
115 		if (ops & (1 << IPI_CALL_FUNC)) {
116 			stats[IPI_CALL_FUNC]++;
117 			generic_smp_call_function_interrupt();
118 		}
119 
120 		if (ops & (1 << IPI_CPU_STOP)) {
121 			stats[IPI_CPU_STOP]++;
122 			ipi_stop();
123 		}
124 
125 		BUG_ON((ops >> IPI_MAX) != 0);
126 
127 		/* Order data access and bit testing. */
128 		mb();
129 	}
130 }
131 
132 static void
133 send_ipi_message(const struct cpumask *to_whom, enum ipi_message_type operation)
134 {
135 	int cpuid, hartid;
136 	struct cpumask hartid_mask;
137 
138 	cpumask_clear(&hartid_mask);
139 	mb();
140 	for_each_cpu(cpuid, to_whom) {
141 		set_bit(operation, &ipi_data[cpuid].bits);
142 		hartid = cpuid_to_hartid_map(cpuid);
143 		cpumask_set_cpu(hartid, &hartid_mask);
144 	}
145 	mb();
146 	sbi_send_ipi(cpumask_bits(&hartid_mask));
147 }
148 
149 static const char * const ipi_names[] = {
150 	[IPI_RESCHEDULE]	= "Rescheduling interrupts",
151 	[IPI_CALL_FUNC]		= "Function call interrupts",
152 	[IPI_CPU_STOP]		= "CPU stop interrupts",
153 };
154 
155 void show_ipi_stats(struct seq_file *p, int prec)
156 {
157 	unsigned int cpu, i;
158 
159 	for (i = 0; i < IPI_MAX; i++) {
160 		seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
161 			   prec >= 4 ? " " : "");
162 		for_each_online_cpu(cpu)
163 			seq_printf(p, "%10lu ", ipi_data[cpu].stats[i]);
164 		seq_printf(p, " %s\n", ipi_names[i]);
165 	}
166 }
167 
168 void arch_send_call_function_ipi_mask(struct cpumask *mask)
169 {
170 	send_ipi_message(mask, IPI_CALL_FUNC);
171 }
172 
173 void arch_send_call_function_single_ipi(int cpu)
174 {
175 	send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
176 }
177 
178 void smp_send_stop(void)
179 {
180 	unsigned long timeout;
181 
182 	if (num_online_cpus() > 1) {
183 		cpumask_t mask;
184 
185 		cpumask_copy(&mask, cpu_online_mask);
186 		cpumask_clear_cpu(smp_processor_id(), &mask);
187 
188 		if (system_state <= SYSTEM_RUNNING)
189 			pr_crit("SMP: stopping secondary CPUs\n");
190 		send_ipi_message(&mask, IPI_CPU_STOP);
191 	}
192 
193 	/* Wait up to one second for other CPUs to stop */
194 	timeout = USEC_PER_SEC;
195 	while (num_online_cpus() > 1 && timeout--)
196 		udelay(1);
197 
198 	if (num_online_cpus() > 1)
199 		pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
200 			   cpumask_pr_args(cpu_online_mask));
201 }
202 
203 void smp_send_reschedule(int cpu)
204 {
205 	send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
206 }
207 
208