blob: e7f02eb73727270326705bb28563755a05905f87 [file] [log] [blame]
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001#include <linux/smp.h>
Ankur Aroraae039002017-06-02 17:06:02 -07002#include <linux/cpu.h>
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +01003#include <linux/slab.h>
4#include <linux/cpumask.h>
5#include <linux/percpu.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07006
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07007#include <xen/events.h>
8
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -04009#include <xen/hvc-console.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070010#include "xen-ops.h"
Mukesh Rathora2ef5dc2014-09-10 16:36:06 -070011#include "smp.h"
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070012
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040013static 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 };
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040016static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070017
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070018static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
Jens Axboe3b16cf82008-06-26 11:21:54 +020019static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070020
21/*
Peter Zijlstra184748c2011-04-05 17:23:39 +020022 * Reschedule call back.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070023 */
24static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
25{
Brian Gerst1b437c82009-01-19 00:38:57 +090026 inc_irq_stat(irq_resched_count);
Peter Zijlstra184748c2011-04-05 17:23:39 +020027 scheduler_ipi();
Jeremy Fitzhardinge38bb5ab2008-05-26 23:31:16 +010028
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070029 return IRQ_HANDLED;
30}
31
Boris Ostrovsky5fc509b2016-08-03 13:22:27 -040032void xen_smp_intr_free(unsigned int cpu)
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040033{
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040034 if (per_cpu(xen_resched_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040035 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040036 per_cpu(xen_resched_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040037 kfree(per_cpu(xen_resched_irq, cpu).name);
38 per_cpu(xen_resched_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040039 }
40 if (per_cpu(xen_callfunc_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040041 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040042 per_cpu(xen_callfunc_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040043 kfree(per_cpu(xen_callfunc_irq, cpu).name);
44 per_cpu(xen_callfunc_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040045 }
46 if (per_cpu(xen_debug_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040047 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040048 per_cpu(xen_debug_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040049 kfree(per_cpu(xen_debug_irq, cpu).name);
50 per_cpu(xen_debug_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040051 }
52 if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040053 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040054 NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040055 per_cpu(xen_callfuncsingle_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040056 kfree(per_cpu(xen_callfuncsingle_irq, cpu).name);
57 per_cpu(xen_callfuncsingle_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040058 }
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +010059}
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040060
Boris Ostrovsky5fc509b2016-08-03 13:22:27 -040061int xen_smp_intr_init(unsigned int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070062{
63 int rc;
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +010064 char *resched_name, *callfunc_name, *debug_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070065
66 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
67 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
68 cpu,
69 xen_reschedule_interrupt,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020070 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070071 resched_name,
72 NULL);
73 if (rc < 0)
74 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040075 per_cpu(xen_resched_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040076 per_cpu(xen_resched_irq, cpu).name = resched_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070077
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,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020082 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070083 callfunc_name,
84 NULL);
85 if (rc < 0)
86 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040087 per_cpu(xen_callfunc_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040088 per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070089
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070090 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
91 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020092 IRQF_PERCPU | IRQF_NOBALANCING,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070093 debug_name, NULL);
94 if (rc < 0)
95 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040096 per_cpu(xen_debug_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040097 per_cpu(xen_debug_irq, cpu).name = debug_name;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070098
Jens Axboe3b16cf82008-06-26 11:21:54 +020099 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,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +0200103 IRQF_PERCPU|IRQF_NOBALANCING,
Jens Axboe3b16cf82008-06-26 11:21:54 +0200104 callfunc_name,
105 NULL);
106 if (rc < 0)
107 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400108 per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -0400109 per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
Jens Axboe3b16cf82008-06-26 11:21:54 +0200110
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +0100111 return 0;
112
113 fail:
114 xen_smp_intr_free(cpu);
115 return rc;
116}
117
Ankur Aroraae039002017-06-02 17:06:02 -0700118void __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
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100148void xen_smp_send_reschedule(int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700149{
150 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
151}
152
Ben Guthrof447d562012-04-21 00:11:04 +0800153static void __xen_send_IPI_mask(const struct cpumask *mask,
154 int vector)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700155{
156 unsigned cpu;
157
Mike Travisbcda0162008-12-16 17:33:59 -0800158 for_each_cpu_and(cpu, mask, cpu_online_mask)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700159 xen_send_IPI_one(cpu, vector);
160}
161
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100162void xen_smp_send_call_function_ipi(const struct cpumask *mask)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200163{
164 int cpu;
165
Ben Guthrof447d562012-04-21 00:11:04 +0800166 __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200167
168 /* Make sure other vcpus get a chance to run if they need to. */
Mike Travisbcda0162008-12-16 17:33:59 -0800169 for_each_cpu(cpu, mask) {
Jens Axboe3b16cf82008-06-26 11:21:54 +0200170 if (xen_vcpu_stolen(cpu)) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100171 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200172 break;
173 }
174 }
175}
176
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100177void xen_smp_send_call_function_single_ipi(int cpu)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200178{
Ben Guthrof447d562012-04-21 00:11:04 +0800179 __xen_send_IPI_mask(cpumask_of(cpu),
Mike Travise7986732008-12-16 17:33:52 -0800180 XEN_CALL_FUNCTION_SINGLE_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200181}
182
Ben Guthrof447d562012-04-21 00:11:04 +0800183static 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;
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800197 case IRQ_WORK_VECTOR:
198 xen_vector = XEN_IRQ_WORK_VECTOR;
199 break;
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -0400200#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
Ben Guthrof447d562012-04-21 00:11:04 +0800206 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();
Stefan Bader1db01b42013-05-08 16:37:35 +0200245 int xen_vector = xen_map_vector(vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800246
Stefan Bader1db01b42013-05-08 16:37:35 +0200247 if (!(num_online_cpus() > 1) || (xen_vector < 0))
Ben Guthrof447d562012-04-21 00:11:04 +0800248 return;
249
250 for_each_cpu_and(cpu, mask, cpu_online_mask) {
251 if (this_cpu == cpu)
252 continue;
253
Stefan Bader1db01b42013-05-08 16:37:35 +0200254 xen_send_IPI_one(cpu, xen_vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800255 }
256}
257
258void xen_send_IPI_allbutself(int vector)
259{
Stefan Bader1db01b42013-05-08 16:37:35 +0200260 xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800261}
262
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700263static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
264{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700265 irq_enter();
Jens Axboe3b16cf82008-06-26 11:21:54 +0200266 generic_smp_call_function_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900267 inc_irq_stat(irq_call_count);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700268 irq_exit();
269
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700270 return IRQ_HANDLED;
271}
272
Jens Axboe3b16cf82008-06-26 11:21:54 +0200273static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700274{
Jens Axboe3b16cf82008-06-26 11:21:54 +0200275 irq_enter();
276 generic_smp_call_function_single_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900277 inc_irq_stat(irq_call_count);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200278 irq_exit();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700279
Jens Axboe3b16cf82008-06-26 11:21:54 +0200280 return IRQ_HANDLED;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700281}