Merge branch 'core-rcu-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / arch / riscv / kernel / smp.c
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         cpumask_clear(out);
60         for_each_cpu(cpu, in)
61                 cpumask_set_cpu(cpuid_to_hartid_map(cpu), out);
62 }
63
64 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
65 {
66         return phys_id == cpuid_to_hartid_map(cpu);
67 }
68
69 /* Unsupported */
70 int setup_profiling_timer(unsigned int multiplier)
71 {
72         return -EINVAL;
73 }
74
75 static void ipi_stop(void)
76 {
77         set_cpu_online(smp_processor_id(), false);
78         while (1)
79                 wait_for_interrupt();
80 }
81
82 static void send_ipi_mask(const struct cpumask *mask, enum ipi_message_type op)
83 {
84         struct cpumask hartid_mask;
85         int cpu;
86
87         smp_mb__before_atomic();
88         for_each_cpu(cpu, mask)
89                 set_bit(op, &ipi_data[cpu].bits);
90         smp_mb__after_atomic();
91
92         riscv_cpuid_to_hartid_mask(mask, &hartid_mask);
93         sbi_send_ipi(cpumask_bits(&hartid_mask));
94 }
95
96 static void send_ipi_single(int cpu, enum ipi_message_type op)
97 {
98         int hartid = cpuid_to_hartid_map(cpu);
99
100         smp_mb__before_atomic();
101         set_bit(op, &ipi_data[cpu].bits);
102         smp_mb__after_atomic();
103
104         sbi_send_ipi(cpumask_bits(cpumask_of(hartid)));
105 }
106
107 static inline void clear_ipi(void)
108 {
109         csr_clear(CSR_SIP, SIE_SSIE);
110 }
111
112 void riscv_software_interrupt(void)
113 {
114         unsigned long *pending_ipis = &ipi_data[smp_processor_id()].bits;
115         unsigned long *stats = ipi_data[smp_processor_id()].stats;
116
117         clear_ipi();
118
119         while (true) {
120                 unsigned long ops;
121
122                 /* Order bit clearing and data access. */
123                 mb();
124
125                 ops = xchg(pending_ipis, 0);
126                 if (ops == 0)
127                         return;
128
129                 if (ops & (1 << IPI_RESCHEDULE)) {
130                         stats[IPI_RESCHEDULE]++;
131                         scheduler_ipi();
132                 }
133
134                 if (ops & (1 << IPI_CALL_FUNC)) {
135                         stats[IPI_CALL_FUNC]++;
136                         generic_smp_call_function_interrupt();
137                 }
138
139                 if (ops & (1 << IPI_CPU_STOP)) {
140                         stats[IPI_CPU_STOP]++;
141                         ipi_stop();
142                 }
143
144                 BUG_ON((ops >> IPI_MAX) != 0);
145
146                 /* Order data access and bit testing. */
147                 mb();
148         }
149 }
150
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",
155 };
156
157 void show_ipi_stats(struct seq_file *p, int prec)
158 {
159         unsigned int cpu, i;
160
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]);
167         }
168 }
169
170 void arch_send_call_function_ipi_mask(struct cpumask *mask)
171 {
172         send_ipi_mask(mask, IPI_CALL_FUNC);
173 }
174
175 void arch_send_call_function_single_ipi(int cpu)
176 {
177         send_ipi_single(cpu, IPI_CALL_FUNC);
178 }
179
180 void smp_send_stop(void)
181 {
182         unsigned long timeout;
183
184         if (num_online_cpus() > 1) {
185                 cpumask_t mask;
186
187                 cpumask_copy(&mask, cpu_online_mask);
188                 cpumask_clear_cpu(smp_processor_id(), &mask);
189
190                 if (system_state <= SYSTEM_RUNNING)
191                         pr_crit("SMP: stopping secondary CPUs\n");
192                 send_ipi_mask(&mask, IPI_CPU_STOP);
193         }
194
195         /* Wait up to one second for other CPUs to stop */
196         timeout = USEC_PER_SEC;
197         while (num_online_cpus() > 1 && timeout--)
198                 udelay(1);
199
200         if (num_online_cpus() > 1)
201                 pr_warn("SMP: failed to stop secondary CPUs %*pbl\n",
202                            cpumask_pr_args(cpu_online_mask));
203 }
204
205 void smp_send_reschedule(int cpu)
206 {
207         send_ipi_single(cpu, IPI_RESCHEDULE);
208 }