blob: 77c959cf81e7c3fb573066513067c47f2698fedc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07002#include <linux/smp.h>
Ankur Aroraae039002017-06-02 17:06:02 -07003#include <linux/cpu.h>
Vitaly Kuznetsov83b96792017-03-14 18:35:46 +01004#include <linux/slab.h>
5#include <linux/cpumask.h>
6#include <linux/percpu.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07007
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07008#include <xen/events.h>
9
Konrad Rzeszutek Wilked467e62011-09-01 09:48:27 -040010#include <xen/hvc-console.h>
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070011#include "xen-ops.h"
Mukesh Rathora2ef5dc2014-09-10 16:36:06 -070012#include "smp.h"
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070013
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040014static 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 };
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040017static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070018
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070019static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
Jens Axboe3b16cf82008-06-26 11:21:54 +020020static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070021
22/*
Peter Zijlstra184748c2011-04-05 17:23:39 +020023 * Reschedule call back.
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070024 */
25static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
26{
Brian Gerst1b437c82009-01-19 00:38:57 +090027 inc_irq_stat(irq_resched_count);
Peter Zijlstra184748c2011-04-05 17:23:39 +020028 scheduler_ipi();
Jeremy Fitzhardinge38bb5ab2008-05-26 23:31:16 +010029
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070030 return IRQ_HANDLED;
31}
32
Boris Ostrovsky5fc509b2016-08-03 13:22:27 -040033void xen_smp_intr_free(unsigned int cpu)
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040034{
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040035 if (per_cpu(xen_resched_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040036 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040037 per_cpu(xen_resched_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040038 kfree(per_cpu(xen_resched_irq, cpu).name);
39 per_cpu(xen_resched_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040040 }
41 if (per_cpu(xen_callfunc_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040042 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040043 per_cpu(xen_callfunc_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040044 kfree(per_cpu(xen_callfunc_irq, cpu).name);
45 per_cpu(xen_callfunc_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040046 }
47 if (per_cpu(xen_debug_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040048 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040049 per_cpu(xen_debug_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040050 kfree(per_cpu(xen_debug_irq, cpu).name);
51 per_cpu(xen_debug_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040052 }
53 if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0) {
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040054 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040055 NULL);
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040056 per_cpu(xen_callfuncsingle_irq, cpu).irq = -1;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040057 kfree(per_cpu(xen_callfuncsingle_irq, cpu).name);
58 per_cpu(xen_callfuncsingle_irq, cpu).name = NULL;
Konrad Rzeszutek Wilkee336e12013-06-04 16:42:29 -040059 }
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +010060}
Konrad Rzeszutek Wilk53b94fd2013-06-04 16:31:34 -040061
Boris Ostrovsky5fc509b2016-08-03 13:22:27 -040062int xen_smp_intr_init(unsigned int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070063{
64 int rc;
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +010065 char *resched_name, *callfunc_name, *debug_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070066
67 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
68 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
69 cpu,
70 xen_reschedule_interrupt,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020071 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070072 resched_name,
73 NULL);
74 if (rc < 0)
75 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040076 per_cpu(xen_resched_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040077 per_cpu(xen_resched_irq, cpu).name = resched_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070078
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,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020083 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070084 callfunc_name,
85 NULL);
86 if (rc < 0)
87 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040088 per_cpu(xen_callfunc_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040089 per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070090
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070091 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
92 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu, xen_debug_interrupt,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020093 IRQF_PERCPU | IRQF_NOBALANCING,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070094 debug_name, NULL);
95 if (rc < 0)
96 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -040097 per_cpu(xen_debug_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -040098 per_cpu(xen_debug_irq, cpu).name = debug_name;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -070099
Jens Axboe3b16cf82008-06-26 11:21:54 +0200100 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,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +0200104 IRQF_PERCPU|IRQF_NOBALANCING,
Jens Axboe3b16cf82008-06-26 11:21:54 +0200105 callfunc_name,
106 NULL);
107 if (rc < 0)
108 goto fail;
Konrad Rzeszutek Wilk95476892013-06-04 16:37:44 -0400109 per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
Konrad Rzeszutek Wilkb85fffe2013-06-04 16:47:17 -0400110 per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
Jens Axboe3b16cf82008-06-26 11:21:54 +0200111
Vitaly Kuznetsov04e95762017-03-14 18:35:42 +0100112 return 0;
113
114 fail:
115 xen_smp_intr_free(cpu);
116 return rc;
117}
118
Ankur Aroraae039002017-06-02 17:06:02 -0700119void __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
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100149void xen_smp_send_reschedule(int cpu)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700150{
151 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
152}
153
Ben Guthrof447d562012-04-21 00:11:04 +0800154static void __xen_send_IPI_mask(const struct cpumask *mask,
155 int vector)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700156{
157 unsigned cpu;
158
Mike Travisbcda0162008-12-16 17:33:59 -0800159 for_each_cpu_and(cpu, mask, cpu_online_mask)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700160 xen_send_IPI_one(cpu, vector);
161}
162
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100163void xen_smp_send_call_function_ipi(const struct cpumask *mask)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200164{
165 int cpu;
166
Ben Guthrof447d562012-04-21 00:11:04 +0800167 __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200168
169 /* Make sure other vcpus get a chance to run if they need to. */
Mike Travisbcda0162008-12-16 17:33:59 -0800170 for_each_cpu(cpu, mask) {
Jens Axboe3b16cf82008-06-26 11:21:54 +0200171 if (xen_vcpu_stolen(cpu)) {
Hannes Eder1207cf8e2009-03-05 20:13:57 +0100172 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200173 break;
174 }
175 }
176}
177
Vitaly Kuznetsova52482d2017-03-14 18:35:45 +0100178void xen_smp_send_call_function_single_ipi(int cpu)
Jens Axboe3b16cf82008-06-26 11:21:54 +0200179{
Ben Guthrof447d562012-04-21 00:11:04 +0800180 __xen_send_IPI_mask(cpumask_of(cpu),
Mike Travise7986732008-12-16 17:33:52 -0800181 XEN_CALL_FUNCTION_SINGLE_VECTOR);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200182}
183
Ben Guthrof447d562012-04-21 00:11:04 +0800184static 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;
Lin Ming1ff2b0c2012-04-21 00:11:05 +0800198 case IRQ_WORK_VECTOR:
199 xen_vector = XEN_IRQ_WORK_VECTOR;
200 break;
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -0400201#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
Ben Guthrof447d562012-04-21 00:11:04 +0800207 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();
Stefan Bader1db01b42013-05-08 16:37:35 +0200246 int xen_vector = xen_map_vector(vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800247
Stefan Bader1db01b42013-05-08 16:37:35 +0200248 if (!(num_online_cpus() > 1) || (xen_vector < 0))
Ben Guthrof447d562012-04-21 00:11:04 +0800249 return;
250
251 for_each_cpu_and(cpu, mask, cpu_online_mask) {
252 if (this_cpu == cpu)
253 continue;
254
Stefan Bader1db01b42013-05-08 16:37:35 +0200255 xen_send_IPI_one(cpu, xen_vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800256 }
257}
258
259void xen_send_IPI_allbutself(int vector)
260{
Stefan Bader1db01b42013-05-08 16:37:35 +0200261 xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
Ben Guthrof447d562012-04-21 00:11:04 +0800262}
263
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700264static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
265{
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700266 irq_enter();
Jens Axboe3b16cf82008-06-26 11:21:54 +0200267 generic_smp_call_function_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900268 inc_irq_stat(irq_call_count);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700269 irq_exit();
270
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700271 return IRQ_HANDLED;
272}
273
Jens Axboe3b16cf82008-06-26 11:21:54 +0200274static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700275{
Jens Axboe3b16cf82008-06-26 11:21:54 +0200276 irq_enter();
277 generic_smp_call_function_single_interrupt();
Brian Gerst1b437c82009-01-19 00:38:57 +0900278 inc_irq_stat(irq_call_count);
Jens Axboe3b16cf82008-06-26 11:21:54 +0200279 irq_exit();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700280
Jens Axboe3b16cf82008-06-26 11:21:54 +0200281 return IRQ_HANDLED;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700282}