irqchip.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. }
  132. static int setup_routing_entry(struct kvm_irq_routing_table *rt,
  133. struct kvm_kernel_irq_routing_entry *e,
  134. const struct kvm_irq_routing_entry *ue)
  135. {
  136. int r = -EINVAL;
  137. struct kvm_kernel_irq_routing_entry *ei;
  138. /*
  139. * Do not allow GSI to be mapped to the same irqchip more than once.
  140. * Allow only one to one mapping between GSI and MSI.
  141. */
  142. hlist_for_each_entry(ei, &rt->map[ue->gsi], link)
  143. if (ei->type == KVM_IRQ_ROUTING_MSI ||
  144. ue->type == KVM_IRQ_ROUTING_MSI ||
  145. ue->u.irqchip.irqchip == ei->irqchip.irqchip)
  146. return r;
  147. e->gsi = ue->gsi;
  148. e->type = ue->type;
  149. r = kvm_set_routing_entry(rt, e, ue);
  150. if (r)
  151. goto out;
  152. hlist_add_head(&e->link, &rt->map[e->gsi]);
  153. r = 0;
  154. out:
  155. return r;
  156. }
  157. int kvm_set_irq_routing(struct kvm *kvm,
  158. const struct kvm_irq_routing_entry *ue,
  159. unsigned nr,
  160. unsigned flags)
  161. {
  162. struct kvm_irq_routing_table *new, *old;
  163. u32 i, j, nr_rt_entries = 0;
  164. int r;
  165. for (i = 0; i < nr; ++i) {
  166. if (ue[i].gsi >= KVM_MAX_IRQ_ROUTES)
  167. return -EINVAL;
  168. nr_rt_entries = max(nr_rt_entries, ue[i].gsi);
  169. }
  170. nr_rt_entries += 1;
  171. new = kzalloc(sizeof(*new) + (nr_rt_entries * sizeof(struct hlist_head))
  172. + (nr * sizeof(struct kvm_kernel_irq_routing_entry)),
  173. GFP_KERNEL);
  174. if (!new)
  175. return -ENOMEM;
  176. new->rt_entries = (void *)&new->map[nr_rt_entries];
  177. new->nr_rt_entries = nr_rt_entries;
  178. for (i = 0; i < KVM_NR_IRQCHIPS; i++)
  179. for (j = 0; j < KVM_IRQCHIP_NUM_PINS; j++)
  180. new->chip[i][j] = -1;
  181. for (i = 0; i < nr; ++i) {
  182. r = -EINVAL;
  183. if (ue->flags)
  184. goto out;
  185. r = setup_routing_entry(new, &new->rt_entries[i], ue);
  186. if (r)
  187. goto out;
  188. ++ue;
  189. }
  190. mutex_lock(&kvm->irq_lock);
  191. old = kvm->irq_routing;
  192. kvm_irq_routing_update(kvm, new);
  193. mutex_unlock(&kvm->irq_lock);
  194. synchronize_rcu();
  195. new = old;
  196. r = 0;
  197. out:
  198. kfree(new);
  199. return r;
  200. }