xref: /openbmc/linux/arch/x86/xen/irq.c (revision d9f6e12f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/hardirq.h>
3 
4 #include <asm/x86_init.h>
5 
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>
11 
12 #include <asm/xen/hypercall.h>
13 #include <asm/xen/hypervisor.h>
14 
15 #include "xen-ops.h"
16 
17 /*
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.
21  */
22 void xen_force_evtchn_callback(void)
23 {
24 	(void)HYPERVISOR_xen_version(0, NULL);
25 }
26 
27 asmlinkage __visible unsigned long xen_save_fl(void)
28 {
29 	struct vcpu_info *vcpu;
30 	unsigned long flags;
31 
32 	vcpu = this_cpu_read(xen_vcpu);
33 
34 	/* flag has opposite sense of mask */
35 	flags = !vcpu->evtchn_upcall_mask;
36 
37 	/* convert to IF type flag
38 	   -0 -> 0x00000000
39 	   -1 -> 0xffffffff
40 	*/
41 	return (-flags) & X86_EFLAGS_IF;
42 }
43 PV_CALLEE_SAVE_REGS_THUNK(xen_save_fl);
44 
45 asmlinkage __visible void xen_irq_disable(void)
46 {
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. */
50 	preempt_disable();
51 	this_cpu_read(xen_vcpu)->evtchn_upcall_mask = 1;
52 	preempt_enable_no_resched();
53 }
54 PV_CALLEE_SAVE_REGS_THUNK(xen_irq_disable);
55 
56 asmlinkage __visible void xen_irq_enable(void)
57 {
58 	struct vcpu_info *vcpu;
59 
60 	/*
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.
64 	 */
65 	preempt_disable();
66 
67 	vcpu = this_cpu_read(xen_vcpu);
68 	vcpu->evtchn_upcall_mask = 0;
69 
70 	/* Doesn't matter if we get preempted here, because any
71 	   pending event will get dealt with anyway. */
72 
73 	barrier(); /* unmask then check (avoid races) */
74 	if (unlikely(vcpu->evtchn_upcall_pending))
75 		xen_force_evtchn_callback();
76 
77 	preempt_enable();
78 }
79 PV_CALLEE_SAVE_REGS_THUNK(xen_irq_enable);
80 
81 static void xen_safe_halt(void)
82 {
83 	/* Blocking includes an implicit local_irq_enable(). */
84 	if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
85 		BUG();
86 }
87 
88 static void xen_halt(void)
89 {
90 	if (irqs_disabled())
91 		HYPERVISOR_vcpu_op(VCPUOP_down,
92 				   xen_vcpu_nr(smp_processor_id()), NULL);
93 	else
94 		xen_safe_halt();
95 }
96 
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),
101 
102 	.safe_halt = xen_safe_halt,
103 	.halt = xen_halt,
104 };
105 
106 void __init xen_init_irq_ops(void)
107 {
108 	pv_ops.irq = xen_irq_ops;
109 	x86_init.irqs.intr_init = xen_init_IRQ;
110 }
111