irqchip.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * irqchip.c: Common API for in kernel interrupt controllers
  3. * Copyright (c) 2007, Intel Corporation.
  4. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  5. * Copyright (c) 2013, Alexander Graf <agraf@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  18. * Place - Suite 330, Boston, MA 02111-1307 USA.
  19. *
  20. * This file is derived from virt/kvm/irq_comm.c.
  21. *
  22. * Authors:
  23. * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
  24. * Alexander Graf <agraf@suse.de>
  25. */
  26. #include <linux/kvm_host.h>
  27. #include <linux/slab.h>
  28. #include <linux/export.h>
  29. #include <trace/events/kvm.h>
  30. #include "irq.h"
  31. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
  32. {
  33. struct kvm_irq_ack_notifier *kian;
  34. int gsi;
  35. rcu_read_lock();
  36. gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
  37. if (gsi != -1)
  38. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  39. link)
  40. if (kian->gsi == gsi) {
  41. rcu_read_unlock();
  42. return true;
  43. }
  44. rcu_read_unlock();
  45. return false;
  46. }
  47. EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
  48. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
  49. {
  50. struct kvm_irq_ack_notifier *kian;
  51. int gsi;
  52. trace_kvm_ack_irq(irqchip, pin);
  53. rcu_read_lock();
  54. gsi = rcu_dereference(kvm->irq_routing)->chip[irqchip][pin];
  55. if (gsi != -1)
  56. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  57. link)
  58. if (kian->gsi == gsi)
  59. kian->irq_acked(kian);
  60. rcu_read_unlock();
  61. }
  62. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  63. struct kvm_irq_ack_notifier *kian)
  64. {
  65. mutex_lock(&kvm->irq_lock);
  66. hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
  67. mutex_unlock(&kvm->irq_lock);
  68. #ifdef __KVM_HAVE_IOAPIC
  69. kvm_vcpu_request_scan_ioapic(kvm);
  70. #endif
  71. }
  72. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  73. struct kvm_irq_ack_notifier *kian)
  74. {
  75. mutex_lock(&kvm->irq_lock);
  76. hlist_del_init_rcu(&kian->link);
  77. mutex_unlock(&kvm->irq_lock);
  78. synchronize_rcu();
  79. #ifdef __KVM_HAVE_IOAPIC
  80. kvm_vcpu_request_scan_ioapic(kvm);
  81. #endif
  82. }
  83. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
  84. {
  85. struct kvm_kernel_irq_routing_entry route;
  86. if (!irqchip_in_kernel(kvm) || msi->flags != 0)
  87. return -EINVAL;
  88. route.msi.address_lo = msi->address_lo;
  89. route.msi.address_hi = msi->address_hi;
  90. route.msi.data = msi->data;
  91. return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false);
  92. }
  93. /*
  94. * Return value:
  95. * < 0 Interrupt was ignored (masked or not delivered for other reasons)
  96. * = 0 Interrupt was coalesced (previous irq is still pending)
  97. * > 0 Number of CPUs interrupt was delivered to
  98. */
  99. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  100. bool line_status)
  101. {
  102. struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
  103. int ret = -1, i = 0;
  104. struct kvm_irq_routing_table *irq_rt;
  105. trace_kvm_set_irq(irq, level, irq_source_id);
  106. /* Not possible to detect if the guest uses the PIC or the
  107. * IOAPIC. So set the bit in both. The guest will ignore
  108. * writes to the unused one.
  109. */
  110. rcu_read_lock();
  111. irq_rt = rcu_dereference(kvm->irq_routing);
  112. if (irq < irq_rt->nr_rt_entries)
  113. hlist_for_each_entry(e, &irq_rt->map[irq], link)
  114. irq_set[i++] = *e;
  115. rcu_read_unlock();
  116. while(i--) {
  117. int r;
  118. r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level,
  119. line_status);
  120. if (r < 0)
  121. continue;
  122. ret = r + ((ret < 0) ? 0 : ret);
  123. }
  124. return ret;
  125. }
  126. void kvm_free_irq_routing(struct kvm *kvm)
  127. {
  128. /* Called only during vm destruction. Nobody can use the pointer
  129. at this stage */
  130. kfree(kvm->irq_routing);
  131. }