irq.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/hardirq.h>
  3. #include <asm/x86_init.h>
  4. #include <xen/interface/xen.h>
  5. #include <xen/interface/sched.h>
  6. #include <xen/interface/vcpu.h>
  7. #include <xen/features.h>
  8. #include <xen/events.h>
  9. #include <asm/xen/hypercall.h>
  10. #include <asm/xen/hypervisor.h>
  11. #include "xen-ops.h"
  12. /*
  13. * Force a proper event-channel callback from Xen after clearing the
  14. * callback mask. We do this in a very simple manner, by making a call
  15. * down into Xen. The pending flag will be checked by Xen on return.
  16. */
  17. void xen_force_evtchn_callback(void)
  18. {
  19. (void)HYPERVISOR_xen_version(0, NULL);
  20. }
  21. asmlinkage __visible unsigned long xen_save_fl(void)
  22. {
  23. struct vcpu_info *vcpu;
  24. unsigned long flags;
  25. vcpu = this_cpu_read(xen_vcpu);
  26. /* flag has opposite sense of mask */
  27. flags = !vcpu->evtchn_upcall_mask;
  28. /* convert to IF type flag
  29. -0 -> 0x00000000
  30. -1 -> 0xffffffff
  31. */
  32. return (-flags) & X86_EFLAGS_IF;
  33. }
  34. PV_CALLEE_SAVE_REGS_THUNK(xen_save_fl);
  35. __visible void xen_restore_fl(unsigned long flags)
  36. {
  37. struct vcpu_info *vcpu;
  38. /* convert from IF type flag */
  39. flags = !(flags & X86_EFLAGS_IF);
  40. /* See xen_irq_enable() for why preemption must be disabled. */
  41. preempt_disable();
  42. vcpu = this_cpu_read(xen_vcpu);
  43. vcpu->evtchn_upcall_mask = flags;
  44. if (flags == 0) {
  45. barrier(); /* unmask then check (avoid races) */
  46. if (unlikely(vcpu->evtchn_upcall_pending))
  47. xen_force_evtchn_callback();
  48. preempt_enable();
  49. } else
  50. preempt_enable_no_resched();
  51. }
  52. PV_CALLEE_SAVE_REGS_THUNK(xen_restore_fl);
  53. asmlinkage __visible void xen_irq_disable(void)
  54. {
  55. /* There's a one instruction preempt window here. We need to
  56. make sure we're don't switch CPUs between getting the vcpu
  57. pointer and updating the mask. */
  58. preempt_disable();
  59. this_cpu_read(xen_vcpu)->evtchn_upcall_mask = 1;
  60. preempt_enable_no_resched();
  61. }
  62. PV_CALLEE_SAVE_REGS_THUNK(xen_irq_disable);
  63. asmlinkage __visible void xen_irq_enable(void)
  64. {
  65. struct vcpu_info *vcpu;
  66. /*
  67. * We may be preempted as soon as vcpu->evtchn_upcall_mask is
  68. * cleared, so disable preemption to ensure we check for
  69. * events on the VCPU we are still running on.
  70. */
  71. preempt_disable();
  72. vcpu = this_cpu_read(xen_vcpu);
  73. vcpu->evtchn_upcall_mask = 0;
  74. /* Doesn't matter if we get preempted here, because any
  75. pending event will get dealt with anyway. */
  76. barrier(); /* unmask then check (avoid races) */
  77. if (unlikely(vcpu->evtchn_upcall_pending))
  78. xen_force_evtchn_callback();
  79. preempt_enable();
  80. }
  81. PV_CALLEE_SAVE_REGS_THUNK(xen_irq_enable);
  82. static void xen_safe_halt(void)
  83. {
  84. /* Blocking includes an implicit local_irq_enable(). */
  85. if (HYPERVISOR_sched_op(SCHEDOP_block, NULL) != 0)
  86. BUG();
  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. static const struct pv_irq_ops xen_irq_ops __initconst = {
  97. .save_fl = PV_CALLEE_SAVE(xen_save_fl),
  98. .restore_fl = PV_CALLEE_SAVE(xen_restore_fl),
  99. .irq_disable = PV_CALLEE_SAVE(xen_irq_disable),
  100. .irq_enable = PV_CALLEE_SAVE(xen_irq_enable),
  101. .safe_halt = xen_safe_halt,
  102. .halt = xen_halt,
  103. };
  104. void __init xen_init_irq_ops(void)
  105. {
  106. pv_irq_ops = xen_irq_ops;
  107. x86_init.irqs.intr_init = xen_init_IRQ;
  108. }