License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / arch / x86 / xen / smp.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
f87e4cac 2#include <linux/smp.h>
ae039001 3#include <linux/cpu.h>
83b96794
VK
4#include <linux/slab.h>
5#include <linux/cpumask.h>
6#include <linux/percpu.h>
f87e4cac 7
f87e4cac
JF
8#include <xen/events.h>
9
ed467e69 10#include <xen/hvc-console.h>
f87e4cac 11#include "xen-ops.h"
a2ef5dc2 12#include "smp.h"
f87e4cac 13
ee336e10
KRW
14static DEFINE_PER_CPU(struct xen_common_irq, xen_resched_irq) = { .irq = -1 };
15static DEFINE_PER_CPU(struct xen_common_irq, xen_callfunc_irq) = { .irq = -1 };
16static DEFINE_PER_CPU(struct xen_common_irq, xen_callfuncsingle_irq) = { .irq = -1 };
9547689f 17static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
f87e4cac
JF
18
19static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
3b16cf87 20static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
f87e4cac
JF
21
22/*
184748cc 23 * Reschedule call back.
f87e4cac
JF
24 */
25static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
26{
1b437c8c 27 inc_irq_stat(irq_resched_count);
184748cc 28 scheduler_ipi();
38bb5ab4 29
f87e4cac
JF
30 return IRQ_HANDLED;
31}
32
5fc509bc 33void xen_smp_intr_free(unsigned int cpu)
53b94fdc 34{
ee336e10 35 if (per_cpu(xen_resched_irq, cpu).irq >= 0) {
9547689f 36 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
ee336e10 37 per_cpu(xen_resched_irq, cpu).irq = -1;
b85fffec
KRW
38 kfree(per_cpu(xen_resched_irq, cpu).name);
39 per_cpu(xen_resched_irq, cpu).name = NULL;
ee336e10
KRW
40 }
41 if (per_cpu(xen_callfunc_irq, cpu).irq >= 0) {
9547689f 42 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
ee336e10 43 per_cpu(xen_callfunc_irq, cpu).irq = -1;
b85fffec
KRW
44 kfree(per_cpu(xen_callfunc_irq, cpu).name);
45 per_cpu(xen_callfunc_irq, cpu).name = NULL;
ee336e10
KRW
46 }
47 if (per_cpu(xen_debug_irq, cpu).irq >= 0) {
9547689f 48 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
ee336e10 49 per_cpu(xen_debug_irq, cpu).irq = -1;
b85fffec
KRW
50 kfree(per_cpu(xen_debug_irq, cpu).name);
51 per_cpu(xen_debug_irq, cpu).name = NULL;
ee336e10
KRW
52 }
53 if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0) {
9547689f 54 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
53b94fdc 55 NULL);
ee336e10 56 per_cpu(xen_callfuncsingle_irq, cpu).irq = -1;
b85fffec
KRW
57 kfree(per_cpu(xen_callfuncsingle_irq, cpu).name);
58 per_cpu(xen_callfuncsingle_irq, cpu).name = NULL;
ee336e10 59 }
04e95761 60}
53b94fdc 61
5fc509bc 62int xen_smp_intr_init(unsigned int cpu)
f87e4cac
JF
63{
64 int rc;
04e95761 65 char *resched_name, *callfunc_name, *debug_name;
f87e4cac
JF
66
67 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
68 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
69 cpu,
70 xen_reschedule_interrupt,
9d71cee6 71 IRQF_PERCPU|IRQF_NOBALANCING,
f87e4cac
JF
72 resched_name,
73 NULL);
74 if (rc < 0)
75 goto fail;
9547689f 76 per_cpu(xen_resched_irq, cpu).irq = rc;
b85fffec 77 per_cpu(xen_resched_irq, cpu).name = resched_name;
f87e4cac
JF
78
79 callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
80 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
81 cpu,
82 xen_call_function_interrupt,
9d71cee6 83 IRQF_PERCPU|IRQF_NOBALANCING,
f87e4cac
JF
84 callfunc_name,
85 NULL);
86 if (rc < 0)
87 goto fail;
9547689f 88 per_cpu(xen_callfunc_irq, cpu).irq = rc;
b85fffec 89 per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
f87e4cac 90
ee523ca1
JF
91 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
92 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
9d71cee6 93 IRQF_PERCPU | IRQF_NOBALANCING,
ee523ca1
JF
94 debug_name, NULL);
95 if (rc < 0)
96 goto fail;
9547689f 97 per_cpu(xen_debug_irq, cpu).irq = rc;
b85fffec 98 per_cpu(xen_debug_irq, cpu).name = debug_name;
ee523ca1 99
3b16cf87
JA
100 callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
101 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
102 cpu,
103 xen_call_function_single_interrupt,
9d71cee6 104 IRQF_PERCPU|IRQF_NOBALANCING,
3b16cf87
JA
105 callfunc_name,
106 NULL);
107 if (rc < 0)
108 goto fail;
9547689f 109 per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
b85fffec 110 per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
3b16cf87 111
04e95761
VK
112 return 0;
113
114 fail:
115 xen_smp_intr_free(cpu);
116 return rc;
117}
118
ae039001
AA
119void __init xen_smp_cpus_done(unsigned int max_cpus)
120{
121 int cpu, rc, count = 0;
122
123 if (xen_hvm_domain())
124 native_smp_cpus_done(max_cpus);
125
126 if (xen_have_vcpu_info_placement)
127 return;
128
129 for_each_online_cpu(cpu) {
130 if (xen_vcpu_nr(cpu) < MAX_VIRT_CPUS)
131 continue;
132
133 rc = cpu_down(cpu);
134
135 if (rc == 0) {
136 /*
137 * Reset vcpu_info so this cpu cannot be onlined again.
138 */
139 xen_vcpu_info_reset(cpu);
140 count++;
141 } else {
142 pr_warn("%s: failed to bring CPU %d down, error %d\n",
143 __func__, cpu, rc);
144 }
145 }
146 WARN(count, "%s: brought %d CPUs offline\n", __func__, count);
147}
148
a52482d9 149void xen_smp_send_reschedule(int cpu)
f87e4cac
JF
150{
151 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
152}
153
f447d56d
BG
154static void __xen_send_IPI_mask(const struct cpumask *mask,
155 int vector)
f87e4cac
JF
156{
157 unsigned cpu;
158
bcda016e 159 for_each_cpu_and(cpu, mask, cpu_online_mask)
f87e4cac
JF
160 xen_send_IPI_one(cpu, vector);
161}
162
a52482d9 163void xen_smp_send_call_function_ipi(const struct cpumask *mask)
3b16cf87
JA
164{
165 int cpu;
166
f447d56d 167 __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
3b16cf87
JA
168
169 /* Make sure other vcpus get a chance to run if they need to. */
bcda016e 170 for_each_cpu(cpu, mask) {
3b16cf87 171 if (xen_vcpu_stolen(cpu)) {
1207cf8e 172 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
3b16cf87
JA
173 break;
174 }
175 }
176}
177
a52482d9 178void xen_smp_send_call_function_single_ipi(int cpu)
3b16cf87 179{
f447d56d 180 __xen_send_IPI_mask(cpumask_of(cpu),
e7986739 181 XEN_CALL_FUNCTION_SINGLE_VECTOR);
3b16cf87
JA
182}
183
f447d56d
BG
184static inline int xen_map_vector(int vector)
185{
186 int xen_vector;
187
188 switch (vector) {
189 case RESCHEDULE_VECTOR:
190 xen_vector = XEN_RESCHEDULE_VECTOR;
191 break;
192 case CALL_FUNCTION_VECTOR:
193 xen_vector = XEN_CALL_FUNCTION_VECTOR;
194 break;
195 case CALL_FUNCTION_SINGLE_VECTOR:
196 xen_vector = XEN_CALL_FUNCTION_SINGLE_VECTOR;
197 break;
1ff2b0c3
LM
198 case IRQ_WORK_VECTOR:
199 xen_vector = XEN_IRQ_WORK_VECTOR;
200 break;
6efa20e4
KRW
201#ifdef CONFIG_X86_64
202 case NMI_VECTOR:
203 case APIC_DM_NMI: /* Some use that instead of NMI_VECTOR */
204 xen_vector = XEN_NMI_VECTOR;
205 break;
206#endif
f447d56d
BG
207 default:
208 xen_vector = -1;
209 printk(KERN_ERR "xen: vector 0x%x is not implemented\n",
210 vector);
211 }
212
213 return xen_vector;
214}
215
216void xen_send_IPI_mask(const struct cpumask *mask,
217 int vector)
218{
219 int xen_vector = xen_map_vector(vector);
220
221 if (xen_vector >= 0)
222 __xen_send_IPI_mask(mask, xen_vector);
223}
224
225void xen_send_IPI_all(int vector)
226{
227 int xen_vector = xen_map_vector(vector);
228
229 if (xen_vector >= 0)
230 __xen_send_IPI_mask(cpu_online_mask, xen_vector);
231}
232
233void xen_send_IPI_self(int vector)
234{
235 int xen_vector = xen_map_vector(vector);
236
237 if (xen_vector >= 0)
238 xen_send_IPI_one(smp_processor_id(), xen_vector);
239}
240
241void xen_send_IPI_mask_allbutself(const struct cpumask *mask,
242 int vector)
243{
244 unsigned cpu;
245 unsigned int this_cpu = smp_processor_id();
1db01b49 246 int xen_vector = xen_map_vector(vector);
f447d56d 247
1db01b49 248 if (!(num_online_cpus() > 1) || (xen_vector < 0))
f447d56d
BG
249 return;
250
251 for_each_cpu_and(cpu, mask, cpu_online_mask) {
252 if (this_cpu == cpu)
253 continue;
254
1db01b49 255 xen_send_IPI_one(cpu, xen_vector);
f447d56d
BG
256 }
257}
258
259void xen_send_IPI_allbutself(int vector)
260{
1db01b49 261 xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
f447d56d
BG
262}
263
f87e4cac
JF
264static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
265{
f87e4cac 266 irq_enter();
3b16cf87 267 generic_smp_call_function_interrupt();
1b437c8c 268 inc_irq_stat(irq_call_count);
f87e4cac
JF
269 irq_exit();
270
f87e4cac
JF
271 return IRQ_HANDLED;
272}
273
3b16cf87 274static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
f87e4cac 275{
3b16cf87
JA
276 irq_enter();
277 generic_smp_call_function_single_interrupt();
1b437c8c 278 inc_irq_stat(irq_call_count);
3b16cf87 279 irq_exit();
f87e4cac 280
3b16cf87 281 return IRQ_HANDLED;
f87e4cac 282}