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>
14 static DEFINE_PER_CPU(struct xen_common_irq, xen_resched_irq) = { .irq = -1 };
15 static DEFINE_PER_CPU(struct xen_common_irq, xen_callfunc_irq) = { .irq = -1 };
16 static DEFINE_PER_CPU(struct xen_common_irq, xen_callfuncsingle_irq) = { .irq = -1 };
17 static DEFINE_PER_CPU(struct xen_common_irq, xen_debug_irq) = { .irq = -1 };
19 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id);
20 static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id);
23 * Reschedule call back.
25 static irqreturn_t xen_reschedule_interrupt(int irq, void *dev_id)
27 inc_irq_stat(irq_resched_count);
33 void xen_smp_intr_free(unsigned int cpu)
35 if (per_cpu(xen_resched_irq, cpu).irq >= 0) {
36 unbind_from_irqhandler(per_cpu(xen_resched_irq, cpu).irq, NULL);
37 per_cpu(xen_resched_irq, cpu).irq = -1;
38 kfree(per_cpu(xen_resched_irq, cpu).name);
39 per_cpu(xen_resched_irq, cpu).name = NULL;
41 if (per_cpu(xen_callfunc_irq, cpu).irq >= 0) {
42 unbind_from_irqhandler(per_cpu(xen_callfunc_irq, cpu).irq, NULL);
43 per_cpu(xen_callfunc_irq, cpu).irq = -1;
44 kfree(per_cpu(xen_callfunc_irq, cpu).name);
45 per_cpu(xen_callfunc_irq, cpu).name = NULL;
47 if (per_cpu(xen_debug_irq, cpu).irq >= 0) {
48 unbind_from_irqhandler(per_cpu(xen_debug_irq, cpu).irq, NULL);
49 per_cpu(xen_debug_irq, cpu).irq = -1;
50 kfree(per_cpu(xen_debug_irq, cpu).name);
51 per_cpu(xen_debug_irq, cpu).name = NULL;
53 if (per_cpu(xen_callfuncsingle_irq, cpu).irq >= 0) {
54 unbind_from_irqhandler(per_cpu(xen_callfuncsingle_irq, cpu).irq,
56 per_cpu(xen_callfuncsingle_irq, cpu).irq = -1;
57 kfree(per_cpu(xen_callfuncsingle_irq, cpu).name);
58 per_cpu(xen_callfuncsingle_irq, cpu).name = NULL;
62 int xen_smp_intr_init(unsigned int cpu)
65 char *resched_name, *callfunc_name, *debug_name;
67 resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
68 rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
70 xen_reschedule_interrupt,
71 IRQF_PERCPU|IRQF_NOBALANCING,
76 per_cpu(xen_resched_irq, cpu).irq = rc;
77 per_cpu(xen_resched_irq, cpu).name = resched_name;
79 callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
80 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
82 xen_call_function_interrupt,
83 IRQF_PERCPU|IRQF_NOBALANCING,
88 per_cpu(xen_callfunc_irq, cpu).irq = rc;
89 per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
91 if (!xen_fifo_events) {
92 debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
93 rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu,
95 IRQF_PERCPU | IRQF_NOBALANCING,
99 per_cpu(xen_debug_irq, cpu).irq = rc;
100 per_cpu(xen_debug_irq, cpu).name = debug_name;
103 callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
104 rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
106 xen_call_function_single_interrupt,
107 IRQF_PERCPU|IRQF_NOBALANCING,
112 per_cpu(xen_callfuncsingle_irq, cpu).irq = rc;
113 per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
118 xen_smp_intr_free(cpu);
122 void __init xen_smp_cpus_done(unsigned int max_cpus)
124 if (xen_hvm_domain())
125 native_smp_cpus_done(max_cpus);
127 calculate_max_logical_packages();
130 void xen_smp_send_reschedule(int cpu)
132 xen_send_IPI_one(cpu, XEN_RESCHEDULE_VECTOR);
135 static void __xen_send_IPI_mask(const struct cpumask *mask,
140 for_each_cpu_and(cpu, mask, cpu_online_mask)
141 xen_send_IPI_one(cpu, vector);
144 void xen_smp_send_call_function_ipi(const struct cpumask *mask)
148 __xen_send_IPI_mask(mask, XEN_CALL_FUNCTION_VECTOR);
150 /* Make sure other vcpus get a chance to run if they need to. */
151 for_each_cpu(cpu, mask) {
152 if (xen_vcpu_stolen(cpu)) {
153 HYPERVISOR_sched_op(SCHEDOP_yield, NULL);
159 void xen_smp_send_call_function_single_ipi(int cpu)
161 __xen_send_IPI_mask(cpumask_of(cpu),
162 XEN_CALL_FUNCTION_SINGLE_VECTOR);
165 static inline int xen_map_vector(int vector)
170 case RESCHEDULE_VECTOR:
171 xen_vector = XEN_RESCHEDULE_VECTOR;
173 case CALL_FUNCTION_VECTOR:
174 xen_vector = XEN_CALL_FUNCTION_VECTOR;
176 case CALL_FUNCTION_SINGLE_VECTOR:
177 xen_vector = XEN_CALL_FUNCTION_SINGLE_VECTOR;
179 case IRQ_WORK_VECTOR:
180 xen_vector = XEN_IRQ_WORK_VECTOR;
184 case APIC_DM_NMI: /* Some use that instead of NMI_VECTOR */
185 xen_vector = XEN_NMI_VECTOR;
190 printk(KERN_ERR "xen: vector 0x%x is not implemented\n",
197 void xen_send_IPI_mask(const struct cpumask *mask,
200 int xen_vector = xen_map_vector(vector);
203 __xen_send_IPI_mask(mask, xen_vector);
206 void xen_send_IPI_all(int vector)
208 int xen_vector = xen_map_vector(vector);
211 __xen_send_IPI_mask(cpu_online_mask, xen_vector);
214 void xen_send_IPI_self(int vector)
216 int xen_vector = xen_map_vector(vector);
219 xen_send_IPI_one(smp_processor_id(), xen_vector);
222 void xen_send_IPI_mask_allbutself(const struct cpumask *mask,
226 unsigned int this_cpu = smp_processor_id();
227 int xen_vector = xen_map_vector(vector);
229 if (!(num_online_cpus() > 1) || (xen_vector < 0))
232 for_each_cpu_and(cpu, mask, cpu_online_mask) {
236 xen_send_IPI_one(cpu, xen_vector);
240 void xen_send_IPI_allbutself(int vector)
242 xen_send_IPI_mask_allbutself(cpu_online_mask, vector);
245 static irqreturn_t xen_call_function_interrupt(int irq, void *dev_id)
247 generic_smp_call_function_interrupt();
248 inc_irq_stat(irq_call_count);
253 static irqreturn_t xen_call_function_single_interrupt(int irq, void *dev_id)
255 generic_smp_call_function_single_interrupt();
256 inc_irq_stat(irq_call_count);