irqchip.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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/srcu.h>
  29. #include <linux/export.h>
  30. #include <trace/events/kvm.h>
  31. #include "irq.h"
  32. bool kvm_irq_has_notifier(struct kvm *kvm, unsigned irqchip, unsigned pin)
  33. {
  34. struct kvm_irq_ack_notifier *kian;
  35. int gsi, idx;
  36. idx = srcu_read_lock(&kvm->irq_srcu);
  37. gsi = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu)->chip[irqchip][pin];
  38. if (gsi != -1)
  39. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  40. link)
  41. if (kian->gsi == gsi) {
  42. srcu_read_unlock(&kvm->irq_srcu, idx);
  43. return true;
  44. }
  45. srcu_read_unlock(&kvm->irq_srcu, idx);
  46. return false;
  47. }
  48. EXPORT_SYMBOL_GPL(kvm_irq_has_notifier);
  49. void kvm_notify_acked_irq(struct kvm *kvm, unsigned irqchip, unsigned pin)
  50. {
  51. struct kvm_irq_ack_notifier *kian;
  52. int gsi, idx;
  53. trace_kvm_ack_irq(irqchip, pin);
  54. idx = srcu_read_lock(&kvm->irq_srcu);
  55. gsi = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu)->chip[irqchip][pin];
  56. if (gsi != -1)
  57. hlist_for_each_entry_rcu(kian, &kvm->irq_ack_notifier_list,
  58. link)
  59. if (kian->gsi == gsi)
  60. kian->irq_acked(kian);
  61. srcu_read_unlock(&kvm->irq_srcu, idx);
  62. }
  63. void kvm_register_irq_ack_notifier(struct kvm *kvm,
  64. struct kvm_irq_ack_notifier *kian)
  65. {
  66. mutex_lock(&kvm->irq_lock);
  67. hlist_add_head_rcu(&kian->link, &kvm->irq_ack_notifier_list);
  68. mutex_unlock(&kvm->irq_lock);
  69. #ifdef __KVM_HAVE_IOAPIC
  70. kvm_vcpu_request_scan_ioapic(kvm);
  71. #endif
  72. }
  73. void kvm_unregister_irq_ack_notifier(struct kvm *kvm,
  74. struct kvm_irq_ack_notifier *kian)
  75. {
  76. mutex_lock(&kvm->irq_lock);
  77. hlist_del_init_rcu(&kian->link);
  78. mutex_unlock(&kvm->irq_lock);
  79. synchronize_srcu(&kvm->irq_srcu);
  80. #ifdef __KVM_HAVE_IOAPIC
  81. kvm_vcpu_request_scan_ioapic(kvm);
  82. #endif
  83. }
  84. int kvm_send_userspace_msi(struct kvm *kvm, struct kvm_msi *msi)
  85. {
  86. struct kvm_kernel_irq_routing_entry route;
  87. if (!irqchip_in_kernel(kvm) || msi->flags != 0)
  88. return -EINVAL;
  89. route.msi.address_lo = msi->address_lo;
  90. route.msi.address_hi = msi->address_hi;
  91. route.msi.data = msi->data;
  92. return kvm_set_msi(&route, kvm, KVM_USERSPACE_IRQ_SOURCE_ID, 1, false);
  93. }
  94. /*
  95. * Return value:
  96. * < 0 Interrupt was ignored (masked or not delivered for other reasons)
  97. * = 0 Interrupt was coalesced (previous irq is still pending)
  98. * > 0 Number of CPUs interrupt was delivered to
  99. */
  100. int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
  101. bool line_status)
  102. {
  103. struct kvm_kernel_irq_routing_entry *e, irq_set[KVM_NR_IRQCHIPS];
  104. int ret = -1, i = 0, idx;
  105. struct kvm_irq_routing_table *irq_rt;
  106. trace_kvm_set_irq(irq, level, irq_source_id);
  107. /* Not possible to detect if the guest uses the PIC or the
  108. * IOAPIC. So set the bit in both. The guest will ignore
  109. * writes to the unused one.
  110. */
  111. idx = srcu_read_lock(&kvm->irq_srcu);
  112. irq_rt = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  113. if (irq < irq_rt->nr_rt_entries)
  114. hlist_for_each_entry(e, &irq_rt->map[irq], link)
  115. irq_set[i++] = *e;
  116. srcu_read_unlock(&kvm->irq_srcu, idx);
  117. while(i--) {
  118. int r;
  119. r = irq_set[i].set(&irq_set[i], kvm, irq_source_id, level,
  120. line_status);
  121. if (r < 0)
  122. continue;
  123. ret = r + ((ret < 0) ? 0 : ret);
  124. }
  125. return ret;
  126. }
  127. void kvm_free_irq_routing(struct kvm *kvm)
  128. {
  129. /* Called only during vm destruction. Nobody can use the pointer
  130. at this stage */
  131. kfree(kvm->irq_routing);
  132. }
  133. static int setup_routing_entry(struct kvm_irq_routing_table *rt,
  134. struct kvm_kernel_irq_routing_entry *e,
  135. const struct kvm_irq_routing_entry *ue)
  136. {
  137. int r = -EINVAL;
  138. struct kvm_kernel_irq_routing_entry *ei;
  139. /*
  140. * Do not allow GSI to be mapped to the same irqchip more than once.
  141. * Allow only one to one mapping between GSI and MSI.
  142. */
  143. hlist_for_each_entry(ei, &rt->map[ue->gsi], link)
  144. if (ei->type == KVM_IRQ_ROUTING_MSI ||
  145. ue->type == KVM_IRQ_ROUTING_MSI ||
  146. ue->u.irqchip.irqchip == ei->irqchip.irqchip)
  147. return r;
  148. e->gsi = ue->gsi;
  149. e->type = ue->type;
  150. r = kvm_set_routing_entry(rt, e, ue);
  151. if (r)
  152. goto out;
  153. hlist_add_head(&e->link, &rt->map[e->gsi]);
  154. r = 0;
  155. out:
  156. return r;
  157. }
  158. int kvm_set_irq_routing(struct kvm *kvm,
  159. const struct kvm_irq_routing_entry *ue,
  160. unsigned nr,
  161. unsigned flags)
  162. {
  163. struct kvm_irq_routing_table *new, *old;
  164. u32 i, j, nr_rt_entries = 0;
  165. int r;
  166. for (i = 0; i < nr; ++i) {
  167. if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
  168. return -EINVAL;
  169. nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
  170. }
  171. nr_rt_entries += 1;
  172. new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
  173. + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
  174. GFP_KERNEL);
  175. if (!new)
  176. return -ENOMEM;
  177. new->rt_entries = (void *)&new->map[nr_rt_entries];
  178. new->nr_rt_entries = nr_rt_entries;
  179. for (i = 0; i < KVM_NR_IRQCHIPS; i++)
  180. for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
  181. new->chip[i][j] = -1;
  182. for (i = 0; i < nr; ++i) {
  183. r = -EINVAL;
  184. if (ue->flags)
  185. goto out;
  186. r = setup_routing_entry(new, &new->rt_entries[i], ue);
  187. if (r)
  188. goto out;
  189. ++ue;
  190. }
  191. mutex_lock(&kvm->irq_lock);
  192. old = kvm->irq_routing;
  193. kvm_irq_routing_update(kvm, new);
  194. mutex_unlock(&kvm->irq_lock);
  195. synchronize_srcu_expedited(&kvm->irq_srcu);
  196. new = old;
  197. r = 0;
  198. out:
  199. kfree(new);
  200. return r;
  201. }