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