irq.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * irq.c: API for in kernel interrupt controller
  4. * Copyright (c) 2007, Intel Corporation.
  5. * Copyright 2009 Red Hat, Inc. and/or its affiliates.
  6. *
  7. * Authors:
  8. * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/export.h>
  12. #include <linux/kvm_host.h>
  13. #include "irq.h"
  14. #include "i8254.h"
  15. #include "x86.h"
  16. #include "xen.h"
  17. /*
  18. * check if there are pending timer events
  19. * to be processed.
  20. */
  21. int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
  22. {
  23. int r = 0;
  24. if (lapic_in_kernel(vcpu))
  25. r = apic_has_pending_timer(vcpu);
  26. if (kvm_xen_timer_enabled(vcpu))
  27. r += kvm_xen_has_pending_timer(vcpu);
  28. return r;
  29. }
  30. /*
  31. * check if there is a pending userspace external interrupt
  32. */
  33. static int pending_userspace_extint(struct kvm_vcpu *v)
  34. {
  35. return v->arch.pending_external_vector != -1;
  36. }
  37. /*
  38. * check if there is pending interrupt from
  39. * non-APIC source without intack.
  40. */
  41. int kvm_cpu_has_extint(struct kvm_vcpu *v)
  42. {
  43. /*
  44. * FIXME: interrupt.injected represents an interrupt whose
  45. * side-effects have already been applied (e.g. bit from IRR
  46. * already moved to ISR). Therefore, it is incorrect to rely
  47. * on interrupt.injected to know if there is a pending
  48. * interrupt in the user-mode LAPIC.
  49. * This leads to nVMX/nSVM not be able to distinguish
  50. * if it should exit from L2 to L1 on EXTERNAL_INTERRUPT on
  51. * pending interrupt or should re-inject an injected
  52. * interrupt.
  53. */
  54. if (!lapic_in_kernel(v))
  55. return v->arch.interrupt.injected;
  56. if (kvm_xen_has_interrupt(v))
  57. return 1;
  58. if (!kvm_apic_accept_pic_intr(v))
  59. return 0;
  60. if (irqchip_split(v->kvm))
  61. return pending_userspace_extint(v);
  62. else
  63. return v->kvm->arch.vpic->output;
  64. }
  65. /*
  66. * check if there is injectable interrupt:
  67. * when virtual interrupt delivery enabled,
  68. * interrupt from apic will handled by hardware,
  69. * we don't need to check it here.
  70. */
  71. int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v)
  72. {
  73. if (kvm_cpu_has_extint(v))
  74. return 1;
  75. if (!is_guest_mode(v) && kvm_vcpu_apicv_active(v))
  76. return 0;
  77. return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
  78. }
  79. EXPORT_SYMBOL_GPL(kvm_cpu_has_injectable_intr);
  80. /*
  81. * check if there is pending interrupt without
  82. * intack.
  83. */
  84. int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
  85. {
  86. if (kvm_cpu_has_extint(v))
  87. return 1;
  88. return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
  89. }
  90. EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
  91. /*
  92. * Read pending interrupt(from non-APIC source)
  93. * vector and intack.
  94. */
  95. int kvm_cpu_get_extint(struct kvm_vcpu *v)
  96. {
  97. if (!kvm_cpu_has_extint(v)) {
  98. WARN_ON(!lapic_in_kernel(v));
  99. return -1;
  100. }
  101. if (!lapic_in_kernel(v))
  102. return v->arch.interrupt.nr;
  103. #ifdef CONFIG_KVM_XEN
  104. if (kvm_xen_has_interrupt(v))
  105. return v->kvm->arch.xen.upcall_vector;
  106. #endif
  107. if (irqchip_split(v->kvm)) {
  108. int vector = v->arch.pending_external_vector;
  109. v->arch.pending_external_vector = -1;
  110. return vector;
  111. } else
  112. return kvm_pic_read_irq(v->kvm); /* PIC */
  113. }
  114. EXPORT_SYMBOL_GPL(kvm_cpu_get_extint);
  115. /*
  116. * Read pending interrupt vector and intack.
  117. */
  118. int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
  119. {
  120. int vector = kvm_cpu_get_extint(v);
  121. if (vector != -1)
  122. return vector; /* PIC */
  123. vector = kvm_apic_has_interrupt(v); /* APIC */
  124. if (vector != -1)
  125. kvm_apic_ack_interrupt(v, vector);
  126. return vector;
  127. }
  128. void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
  129. {
  130. if (lapic_in_kernel(vcpu))
  131. kvm_inject_apic_timer_irqs(vcpu);
  132. if (kvm_xen_timer_enabled(vcpu))
  133. kvm_xen_inject_timer_irqs(vcpu);
  134. }
  135. void __kvm_migrate_timers(struct kvm_vcpu *vcpu)
  136. {
  137. __kvm_migrate_apic_timer(vcpu);
  138. __kvm_migrate_pit_timer(vcpu);
  139. kvm_x86_call(migrate_timers)(vcpu);
  140. }
  141. bool kvm_arch_irqfd_allowed(struct kvm *kvm, struct kvm_irqfd *args)
  142. {
  143. bool resample = args->flags & KVM_IRQFD_FLAG_RESAMPLE;
  144. return resample ? irqchip_kernel(kvm) : irqchip_in_kernel(kvm);
  145. }
  146. bool kvm_arch_irqchip_in_kernel(struct kvm *kvm)
  147. {
  148. return irqchip_in_kernel(kvm);
  149. }