1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/slab.h>
5 #include <linux/cpumask.h>
6 #include <linux/percpu.h>
8 #include <xen/events.h>
10 #include <xen/hvc-console.h>
13 static DEFINE_PER_CPU(struct xen_common_irq
, xen_resched_irq
) = { .irq
= -1 };
14 static DEFINE_PER_CPU(struct xen_common_irq
, xen_callfunc_irq
) = { .irq
= -1 };
15 static DEFINE_PER_CPU(struct xen_common_irq
, xen_callfuncsingle_irq
) = { .irq
= -1 };
16 static DEFINE_PER_CPU(struct xen_common_irq
, xen_debug_irq
) = { .irq
= -1 };
18 static irqreturn_t
xen_call_function_interrupt(int irq
, void *dev_id
);
19 static irqreturn_t
xen_call_function_single_interrupt(int irq
, void *dev_id
);
22 * Reschedule call back.
24 static irqreturn_t
xen_reschedule_interrupt(int irq
, void *dev_id
)
26 inc_irq_stat(irq_resched_count
);
32 void xen_smp_intr_free(unsigned int cpu
)
34 kfree(per_cpu(xen_resched_irq
, cpu
).name
);
35 per_cpu(xen_resched_irq
, cpu
).name
= NULL
;
36 if (per_cpu(xen_resched_irq
, cpu
).irq
>= 0) {
37 unbind_from_irqhandler(per_cpu(xen_resched_irq
, cpu
).irq
, NULL
);
38 per_cpu(xen_resched_irq
, cpu
).irq
= -1;
40 kfree(per_cpu(xen_callfunc_irq
, cpu
).name
);
41 per_cpu(xen_callfunc_irq
, cpu
).name
= NULL
;
42 if (per_cpu(xen_callfunc_irq
, cpu
).irq
>= 0) {
43 unbind_from_irqhandler(per_cpu(xen_callfunc_irq
, cpu
).irq
, NULL
);
44 per_cpu(xen_callfunc_irq
, cpu
).irq
= -1;
46 kfree(per_cpu(xen_debug_irq
, cpu
).name
);
47 per_cpu(xen_debug_irq
, cpu
).name
= NULL
;
48 if (per_cpu(xen_debug_irq
, cpu
).irq
>= 0) {
49 unbind_from_irqhandler(per_cpu(xen_debug_irq
, cpu
).irq
, NULL
);
50 per_cpu(xen_debug_irq
, cpu
).irq
= -1;
52 kfree(per_cpu(xen_callfuncsingle_irq
, cpu
).name
);
53 per_cpu(xen_callfuncsingle_irq
, cpu
).name
= NULL
;
54 if (per_cpu(xen_callfuncsingle_irq
, cpu
).irq
>= 0) {
55 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq
, cpu
).irq
,
57 per_cpu(xen_callfuncsingle_irq
, cpu
).irq
= -1;
61 int xen_smp_intr_init(unsigned int cpu
)
64 char *resched_name
, *callfunc_name
, *debug_name
;
66 resched_name
= kasprintf(GFP_KERNEL
, "resched%d", cpu
);
69 per_cpu(xen_resched_irq
, cpu
).name
= resched_name
;
70 rc
= bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR
,
72 xen_reschedule_interrupt
,
73 IRQF_PERCPU
|IRQF_NOBALANCING
,
78 per_cpu(xen_resched_irq
, cpu
).irq
= rc
;
80 callfunc_name
= kasprintf(GFP_KERNEL
, "callfunc%d", cpu
);
83 per_cpu(xen_callfunc_irq
, cpu
).name
= callfunc_name
;
84 rc
= bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR
,
86 xen_call_function_interrupt
,
87 IRQF_PERCPU
|IRQF_NOBALANCING
,
92 per_cpu(xen_callfunc_irq
, cpu
).irq
= rc
;
94 if (!xen_fifo_events
) {
95 debug_name
= kasprintf(GFP_KERNEL
, "debug%d", cpu
);
99 per_cpu(xen_debug_irq
, cpu
).name
= debug_name
;
100 rc
= bind_virq_to_irqhandler(VIRQ_DEBUG
, cpu
,
102 IRQF_PERCPU
| IRQF_NOBALANCING
,
106 per_cpu(xen_debug_irq
, cpu
).irq
= rc
;
109 callfunc_name
= kasprintf(GFP_KERNEL
, "callfuncsingle%d", cpu
);
113 per_cpu(xen_callfuncsingle_irq
, cpu
).name
= callfunc_name
;
114 rc
= bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR
,
116 xen_call_function_single_interrupt
,
117 IRQF_PERCPU
|IRQF_NOBALANCING
,
122 per_cpu(xen_callfuncsingle_irq
, cpu
).irq
= rc
;
129 xen_smp_intr_free(cpu
);
133 void __init
xen_smp_cpus_done(unsigned int max_cpus
)
135 if (xen_hvm_domain())
136 native_smp_cpus_done(max_cpus
);
139 void xen_smp_send_reschedule(int cpu
)
141 xen_send_IPI_one(cpu
, XEN_RESCHEDULE_VECTOR
);
144 static void __xen_send_IPI_mask(const struct cpumask
*mask
,
149 for_each_cpu_and(cpu
, mask
, cpu_online_mask
)
150 xen_send_IPI_one(cpu
, vector
);
153 void xen_smp_send_call_function_ipi(const struct cpumask
*mask
)
157 __xen_send_IPI_mask(mask
, XEN_CALL_FUNCTION_VECTOR
);
159 /* Make sure other vcpus get a chance to run if they need to. */
160 for_each_cpu(cpu
, mask
) {
161 if (xen_vcpu_stolen(cpu
)) {
162 HYPERVISOR_sched_op(SCHEDOP_yield
, NULL
);
168 void xen_smp_send_call_function_single_ipi(int cpu
)
170 __xen_send_IPI_mask(cpumask_of(cpu
),
171 XEN_CALL_FUNCTION_SINGLE_VECTOR
);
174 static inline int xen_map_vector(int vector
)
179 case RESCHEDULE_VECTOR
:
180 xen_vector
= XEN_RESCHEDULE_VECTOR
;
182 case CALL_FUNCTION_VECTOR
:
183 xen_vector
= XEN_CALL_FUNCTION_VECTOR
;
185 case CALL_FUNCTION_SINGLE_VECTOR
:
186 xen_vector
= XEN_CALL_FUNCTION_SINGLE_VECTOR
;
188 case IRQ_WORK_VECTOR
:
189 xen_vector
= XEN_IRQ_WORK_VECTOR
;
193 case APIC_DM_NMI
: /* Some use that instead of NMI_VECTOR */
194 xen_vector
= XEN_NMI_VECTOR
;
199 printk(KERN_ERR
"xen: vector 0x%x is not implemented\n",
206 void xen_send_IPI_mask(const struct cpumask
*mask
,
209 int xen_vector
= xen_map_vector(vector
);
212 __xen_send_IPI_mask(mask
, xen_vector
);
215 void xen_send_IPI_all(int vector
)
217 int xen_vector
= xen_map_vector(vector
);
220 __xen_send_IPI_mask(cpu_online_mask
, xen_vector
);
223 void xen_send_IPI_self(int vector
)
225 int xen_vector
= xen_map_vector(vector
);
228 xen_send_IPI_one(smp_processor_id(), xen_vector
);
231 void xen_send_IPI_mask_allbutself(const struct cpumask
*mask
,
235 unsigned int this_cpu
= smp_processor_id();
236 int xen_vector
= xen_map_vector(vector
);
238 if (!(num_online_cpus() > 1) || (xen_vector
< 0))
241 for_each_cpu_and(cpu
, mask
, cpu_online_mask
) {
245 xen_send_IPI_one(cpu
, xen_vector
);
249 void xen_send_IPI_allbutself(int vector
)
251 xen_send_IPI_mask_allbutself(cpu_online_mask
, vector
);
254 static irqreturn_t
xen_call_function_interrupt(int irq
, void *dev_id
)
256 generic_smp_call_function_interrupt();
257 inc_irq_stat(irq_call_count
);
262 static irqreturn_t
xen_call_function_single_interrupt(int irq
, void *dev_id
)
264 generic_smp_call_function_single_interrupt();
265 inc_irq_stat(irq_call_count
);