1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hardirq.h>
4 #include <asm/x86_init.h>
6 #include <xen/interface/xen.h>
7 #include <xen/interface/sched.h>
8 #include <xen/interface/vcpu.h>
9 #include <xen/features.h>
10 #include <xen/events.h>
12 #include <asm/xen/hypercall.h>
13 #include <asm/xen/hypervisor.h>
18 * Force a proper event-channel callback from Xen after clearing the
19 * callback mask. We do this in a very simple manner, by making a call
20 * down into Xen. The pending flag will be checked by Xen on return.
22 void xen_force_evtchn_callback(void)
24 (void)HYPERVISOR_xen_version(0, NULL);
27 asmlinkage __visible unsigned long xen_save_fl(void)
29 struct vcpu_info *vcpu;
32 vcpu = this_cpu_read(xen_vcpu);
34 /* flag has opposite sense of mask */
35 flags = !vcpu->evtchn_upcall_mask;
37 /* convert to IF type flag
41 return (-flags) & X86_EFLAGS_IF;
43 PV_CALLEE_SAVE_REGS_THUNK(xen_save_fl);
45 asmlinkage __visible void xen_irq_disable(void)
47 /* There's a one instruction preempt window here. We need to
48 make sure we're don't switch CPUs between getting the vcpu
49 pointer and updating the mask. */
51 this_cpu_read(xen_vcpu)->evtchn_upcall_mask = 1;
52 preempt_enable_no_resched();
54 PV_CALLEE_SAVE_REGS_THUNK(xen_irq_disable);
56 asmlinkage __visible void xen_irq_enable(void)
58 struct vcpu_info *vcpu;
61 * We may be preempted as soon as vcpu->evtchn_upcall_mask is
62 * cleared, so disable preemption to ensure we check for
63 * events on the VCPU we are still running on.
67 vcpu = this_cpu_read(xen_vcpu);
68 vcpu->evtchn_upcall_mask = 0;
70 /* Doesn't matter if we get preempted here, because any
71 pending event will get dealt with anyway. */
73 barrier(); /* unmask then check (avoid races) */
74 if (unlikely(vcpu->evtchn_upcall_pending))
75 xen_force_evtchn_callback();
79 PV_CALLEE_SAVE_REGS_THUNK(xen_irq_enable);
81 static void xen_safe_halt(void)
83 /* Blocking includes an implicit local_irq_enable(). */
84 if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
88 static void xen_halt(void)
91 HYPERVISOR_vcpu_op(VCPUOP_down,
92 xen_vcpu_nr(smp_processor_id()), NULL);
97 static const struct pv_irq_ops xen_irq_ops __initconst = {
98 .save_fl = PV_CALLEE_SAVE(xen_save_fl),
99 .irq_disable = PV_CALLEE_SAVE(xen_irq_disable),
100 .irq_enable = PV_CALLEE_SAVE(xen_irq_enable),
102 .safe_halt = xen_safe_halt,
106 void __init xen_init_irq_ops(void)
108 pv_ops.irq = xen_irq_ops;
109 x86_init.irqs.intr_init = xen_init_IRQ;