Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 1 | #include <linux/hardirq.h> |
| 2 | |
Thomas Gleixner | 66bcaf0 | 2009-08-20 09:59:09 +0200 | [diff] [blame] | 3 | #include <asm/x86_init.h> |
| 4 | |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 5 | #include <xen/interface/xen.h> |
| 6 | #include <xen/interface/sched.h> |
| 7 | #include <xen/interface/vcpu.h> |
Stefano Stabellini | 0ec53ec | 2012-09-14 13:37:32 +0000 | [diff] [blame] | 8 | #include <xen/events.h> |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 9 | |
| 10 | #include <asm/xen/hypercall.h> |
| 11 | #include <asm/xen/hypervisor.h> |
| 12 | |
| 13 | #include "xen-ops.h" |
| 14 | |
| 15 | /* |
| 16 | * Force a proper event-channel callback from Xen after clearing the |
| 17 | * callback mask. We do this in a very simple manner, by making a call |
| 18 | * down into Xen. The pending flag will be checked by Xen on return. |
| 19 | */ |
| 20 | void xen_force_evtchn_callback(void) |
| 21 | { |
| 22 | (void)HYPERVISOR_xen_version(0, NULL); |
| 23 | } |
| 24 | |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 25 | static unsigned long xen_save_fl(void) |
| 26 | { |
| 27 | struct vcpu_info *vcpu; |
| 28 | unsigned long flags; |
| 29 | |
Alex Shi | 2113f46 | 2012-01-13 23:53:35 +0800 | [diff] [blame] | 30 | vcpu = this_cpu_read(xen_vcpu); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 31 | |
| 32 | /* flag has opposite sense of mask */ |
| 33 | flags = !vcpu->evtchn_upcall_mask; |
| 34 | |
| 35 | /* convert to IF type flag |
| 36 | -0 -> 0x00000000 |
| 37 | -1 -> 0xffffffff |
| 38 | */ |
| 39 | return (-flags) & X86_EFLAGS_IF; |
| 40 | } |
Jeremy Fitzhardinge | ecb93d1 | 2009-01-28 14:35:05 -0800 | [diff] [blame] | 41 | PV_CALLEE_SAVE_REGS_THUNK(xen_save_fl); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 42 | |
| 43 | static void xen_restore_fl(unsigned long flags) |
| 44 | { |
| 45 | struct vcpu_info *vcpu; |
| 46 | |
| 47 | /* convert from IF type flag */ |
| 48 | flags = !(flags & X86_EFLAGS_IF); |
| 49 | |
David Vrabel | fb58e30 | 2013-08-15 13:21:04 +0100 | [diff] [blame^] | 50 | /* See xen_irq_enable() for why preemption must be disabled. */ |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 51 | preempt_disable(); |
Alex Shi | 2113f46 | 2012-01-13 23:53:35 +0800 | [diff] [blame] | 52 | vcpu = this_cpu_read(xen_vcpu); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 53 | vcpu->evtchn_upcall_mask = flags; |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 54 | |
| 55 | if (flags == 0) { |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 56 | barrier(); /* unmask then check (avoid races) */ |
| 57 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 58 | xen_force_evtchn_callback(); |
David Vrabel | fb58e30 | 2013-08-15 13:21:04 +0100 | [diff] [blame^] | 59 | preempt_enable(); |
| 60 | } else |
| 61 | preempt_enable_no_resched(); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 62 | } |
Jeremy Fitzhardinge | ecb93d1 | 2009-01-28 14:35:05 -0800 | [diff] [blame] | 63 | PV_CALLEE_SAVE_REGS_THUNK(xen_restore_fl); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 64 | |
| 65 | static void xen_irq_disable(void) |
| 66 | { |
| 67 | /* There's a one instruction preempt window here. We need to |
| 68 | make sure we're don't switch CPUs between getting the vcpu |
| 69 | pointer and updating the mask. */ |
| 70 | preempt_disable(); |
Alex Shi | 2113f46 | 2012-01-13 23:53:35 +0800 | [diff] [blame] | 71 | this_cpu_read(xen_vcpu)->evtchn_upcall_mask = 1; |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 72 | preempt_enable_no_resched(); |
| 73 | } |
Jeremy Fitzhardinge | ecb93d1 | 2009-01-28 14:35:05 -0800 | [diff] [blame] | 74 | PV_CALLEE_SAVE_REGS_THUNK(xen_irq_disable); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 75 | |
| 76 | static void xen_irq_enable(void) |
| 77 | { |
| 78 | struct vcpu_info *vcpu; |
| 79 | |
David Vrabel | fb58e30 | 2013-08-15 13:21:04 +0100 | [diff] [blame^] | 80 | /* |
| 81 | * We may be preempted as soon as vcpu->evtchn_upcall_mask is |
| 82 | * cleared, so disable preemption to ensure we check for |
| 83 | * events on the VCPU we are still running on. |
| 84 | */ |
| 85 | preempt_disable(); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 86 | |
Alex Shi | 2113f46 | 2012-01-13 23:53:35 +0800 | [diff] [blame] | 87 | vcpu = this_cpu_read(xen_vcpu); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 88 | vcpu->evtchn_upcall_mask = 0; |
| 89 | |
| 90 | /* Doesn't matter if we get preempted here, because any |
| 91 | pending event will get dealt with anyway. */ |
| 92 | |
| 93 | barrier(); /* unmask then check (avoid races) */ |
| 94 | if (unlikely(vcpu->evtchn_upcall_pending)) |
| 95 | xen_force_evtchn_callback(); |
David Vrabel | fb58e30 | 2013-08-15 13:21:04 +0100 | [diff] [blame^] | 96 | |
| 97 | preempt_enable(); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 98 | } |
Jeremy Fitzhardinge | ecb93d1 | 2009-01-28 14:35:05 -0800 | [diff] [blame] | 99 | PV_CALLEE_SAVE_REGS_THUNK(xen_irq_enable); |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 100 | |
| 101 | static void xen_safe_halt(void) |
| 102 | { |
| 103 | /* Blocking includes an implicit local_irq_enable(). */ |
| 104 | if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0) |
| 105 | BUG(); |
| 106 | } |
| 107 | |
| 108 | static void xen_halt(void) |
| 109 | { |
| 110 | if (irqs_disabled()) |
| 111 | HYPERVISOR_vcpu_op(VCPUOP_down, smp_processor_id(), NULL); |
| 112 | else |
| 113 | xen_safe_halt(); |
| 114 | } |
| 115 | |
Daniel Kiper | 251511a | 2011-05-04 20:16:07 +0200 | [diff] [blame] | 116 | static const struct pv_irq_ops xen_irq_ops __initconst = { |
Jeremy Fitzhardinge | ecb93d1 | 2009-01-28 14:35:05 -0800 | [diff] [blame] | 117 | .save_fl = PV_CALLEE_SAVE(xen_save_fl), |
| 118 | .restore_fl = PV_CALLEE_SAVE(xen_restore_fl), |
| 119 | .irq_disable = PV_CALLEE_SAVE(xen_irq_disable), |
| 120 | .irq_enable = PV_CALLEE_SAVE(xen_irq_enable), |
| 121 | |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 122 | .safe_halt = xen_safe_halt, |
| 123 | .halt = xen_halt, |
| 124 | #ifdef CONFIG_X86_64 |
| 125 | .adjust_exception_frame = xen_adjust_exception_frame, |
| 126 | #endif |
| 127 | }; |
| 128 | |
Randy Dunlap | 7d81c3b | 2011-01-08 20:00:36 -0800 | [diff] [blame] | 129 | void __init xen_init_irq_ops(void) |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 130 | { |
| 131 | pv_irq_ops = xen_irq_ops; |
Thomas Gleixner | 66bcaf0 | 2009-08-20 09:59:09 +0200 | [diff] [blame] | 132 | x86_init.irqs.intr_init = xen_init_IRQ; |
Jeremy Fitzhardinge | 0d1edf4 | 2008-07-28 11:53:57 -0700 | [diff] [blame] | 133 | } |