irq_comm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * irq_comm.c: Common API for in kernel interrupt controller
  3. * Copyright (c) 2007, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  16. * Place - Suite 330, Boston, MA 02111-1307 USA.
  17. * Authors:
  18. * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
  19. *
  20. * Copyright 2010 Red Hat, Inc. and/or its affiliates.
  21. */
  22. #include <linux/kvm_host.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <linux/rculist.h>
  26. #include <trace/events/kvm.h>
  27. #include <asm/msidef.h>
  28. #include "irq.h"
  29. #include "ioapic.h"
  30. #include "lapic.h"
  31. #include "hyperv.h"
  32. #include "x86.h"
  33. static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
  34. struct kvm *kvm, int irq_source_id, int level,
  35. bool line_status)
  36. {
  37. struct kvm_pic *pic = kvm->arch.vpic;
  38. return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
  39. }
  40. static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
  41. struct kvm *kvm, int irq_source_id, int level,
  42. bool line_status)
  43. {
  44. struct kvm_ioapic *ioapic = kvm->arch.vioapic;
  45. return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
  46. line_status);
  47. }
  48. int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
  49. struct kvm_lapic_irq *irq, struct dest_map *dest_map)
  50. {
  51. int i, r = -1;
  52. struct kvm_vcpu *vcpu, *lowest = NULL;
  53. unsigned long dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
  54. unsigned int dest_vcpus = 0;
  55. if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
  56. kvm_lowest_prio_delivery(irq)) {
  57. printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
  58. irq->delivery_mode = APIC_DM_FIXED;
  59. }
  60. if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
  61. return r;
  62. memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
  63. kvm_for_each_vcpu(i, vcpu, kvm) {
  64. if (!kvm_apic_present(vcpu))
  65. continue;
  66. if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
  67. irq->dest_id, irq->dest_mode))
  68. continue;
  69. if (!kvm_lowest_prio_delivery(irq)) {
  70. if (r < 0)
  71. r = 0;
  72. r += kvm_apic_set_irq(vcpu, irq, dest_map);
  73. } else if (kvm_lapic_enabled(vcpu)) {
  74. if (!kvm_vector_hashing_enabled()) {
  75. if (!lowest)
  76. lowest = vcpu;
  77. else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
  78. lowest = vcpu;
  79. } else {
  80. __set_bit(i, dest_vcpu_bitmap);
  81. dest_vcpus++;
  82. }
  83. }
  84. }
  85. if (dest_vcpus != 0) {
  86. int idx = kvm_vector_to_index(irq->vector, dest_vcpus,
  87. dest_vcpu_bitmap, KVM_MAX_VCPUS);
  88. lowest = kvm_get_vcpu(kvm, idx);
  89. }
  90. if (lowest)
  91. r = kvm_apic_set_irq(lowest, irq, dest_map);
  92. return r;
  93. }
  94. void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
  95. struct kvm_lapic_irq *irq)
  96. {
  97. trace_kvm_msi_set_irq(e->msi.address_lo | (kvm->arch.x2apic_format ?
  98. (u64)e->msi.address_hi << 32 : 0),
  99. e->msi.data);
  100. irq->dest_id = (e->msi.address_lo &
  101. MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
  102. if (kvm->arch.x2apic_format)
  103. irq->dest_id |= MSI_ADDR_EXT_DEST_ID(e->msi.address_hi);
  104. irq->vector = (e->msi.data &
  105. MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
  106. irq->dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
  107. irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
  108. irq->delivery_mode = e->msi.data & 0x700;
  109. irq->msi_redir_hint = ((e->msi.address_lo
  110. & MSI_ADDR_REDIRECTION_LOWPRI) > 0);
  111. irq->level = 1;
  112. irq->shorthand = 0;
  113. }
  114. EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
  115. static inline bool kvm_msi_route_invalid(struct kvm *kvm,
  116. struct kvm_kernel_irq_routing_entry *e)
  117. {
  118. return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff);
  119. }
  120. int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
  121. struct kvm *kvm, int irq_source_id, int level, bool line_status)
  122. {
  123. struct kvm_lapic_irq irq;
  124. if (kvm_msi_route_invalid(kvm, e))
  125. return -EINVAL;
  126. if (!level)
  127. return -1;
  128. kvm_set_msi_irq(kvm, e, &irq);
  129. return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
  130. }
  131. static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e,
  132. struct kvm *kvm, int irq_source_id, int level,
  133. bool line_status)
  134. {
  135. if (!level)
  136. return -1;
  137. return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint);
  138. }
  139. int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
  140. struct kvm *kvm, int irq_source_id, int level,
  141. bool line_status)
  142. {
  143. struct kvm_lapic_irq irq;
  144. int r;
  145. switch (e->type) {
  146. case KVM_IRQ_ROUTING_HV_SINT:
  147. return kvm_hv_set_sint(e, kvm, irq_source_id, level,
  148. line_status);
  149. case KVM_IRQ_ROUTING_MSI:
  150. if (kvm_msi_route_invalid(kvm, e))
  151. return -EINVAL;
  152. kvm_set_msi_irq(kvm, e, &irq);
  153. if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL))
  154. return r;
  155. break;
  156. default:
  157. break;
  158. }
  159. return -EWOULDBLOCK;
  160. }
  161. int kvm_request_irq_source_id(struct kvm *kvm)
  162. {
  163. unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
  164. int irq_source_id;
  165. mutex_lock(&kvm->irq_lock);
  166. irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
  167. if (irq_source_id >= BITS_PER_LONG) {
  168. printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
  169. irq_source_id = -EFAULT;
  170. goto unlock;
  171. }
  172. ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
  173. ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
  174. set_bit(irq_source_id, bitmap);
  175. unlock:
  176. mutex_unlock(&kvm->irq_lock);
  177. return irq_source_id;
  178. }
  179. void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
  180. {
  181. ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
  182. ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
  183. mutex_lock(&kvm->irq_lock);
  184. if (irq_source_id < 0 ||
  185. irq_source_id >= BITS_PER_LONG) {
  186. printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
  187. goto unlock;
  188. }
  189. clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
  190. if (!irqchip_kernel(kvm))
  191. goto unlock;
  192. kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
  193. kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
  194. unlock:
  195. mutex_unlock(&kvm->irq_lock);
  196. }
  197. void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
  198. struct kvm_irq_mask_notifier *kimn)
  199. {
  200. mutex_lock(&kvm->irq_lock);
  201. kimn->irq = irq;
  202. hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
  203. mutex_unlock(&kvm->irq_lock);
  204. }
  205. void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
  206. struct kvm_irq_mask_notifier *kimn)
  207. {
  208. mutex_lock(&kvm->irq_lock);
  209. hlist_del_rcu(&kimn->link);
  210. mutex_unlock(&kvm->irq_lock);
  211. synchronize_srcu(&kvm->irq_srcu);
  212. }
  213. void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
  214. bool mask)
  215. {
  216. struct kvm_irq_mask_notifier *kimn;
  217. int idx, gsi;
  218. idx = srcu_read_lock(&kvm->irq_srcu);
  219. gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
  220. if (gsi != -1)
  221. hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
  222. if (kimn->irq == gsi)
  223. kimn->func(kimn, mask);
  224. srcu_read_unlock(&kvm->irq_srcu, idx);
  225. }
  226. bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
  227. {
  228. return irqchip_in_kernel(kvm);
  229. }
  230. int kvm_set_routing_entry(struct kvm *kvm,
  231. struct kvm_kernel_irq_routing_entry *e,
  232. const struct kvm_irq_routing_entry *ue)
  233. {
  234. /* We can't check irqchip_in_kernel() here as some callers are
  235. * currently inititalizing the irqchip. Other callers should therefore
  236. * check kvm_arch_can_set_irq_routing() before calling this function.
  237. */
  238. switch (ue->type) {
  239. case KVM_IRQ_ROUTING_IRQCHIP:
  240. if (irqchip_split(kvm))
  241. return -EINVAL;
  242. e->irqchip.pin = ue->u.irqchip.pin;
  243. switch (ue->u.irqchip.irqchip) {
  244. case KVM_IRQCHIP_PIC_SLAVE:
  245. e->irqchip.pin += PIC_NUM_PINS / 2;
  246. /* fall through */
  247. case KVM_IRQCHIP_PIC_MASTER:
  248. if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
  249. return -EINVAL;
  250. e->set = kvm_set_pic_irq;
  251. break;
  252. case KVM_IRQCHIP_IOAPIC:
  253. if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
  254. return -EINVAL;
  255. e->set = kvm_set_ioapic_irq;
  256. break;
  257. default:
  258. return -EINVAL;
  259. }
  260. e->irqchip.irqchip = ue->u.irqchip.irqchip;
  261. break;
  262. case KVM_IRQ_ROUTING_MSI:
  263. e->set = kvm_set_msi;
  264. e->msi.address_lo = ue->u.msi.address_lo;
  265. e->msi.address_hi = ue->u.msi.address_hi;
  266. e->msi.data = ue->u.msi.data;
  267. if (kvm_msi_route_invalid(kvm, e))
  268. return -EINVAL;
  269. break;
  270. case KVM_IRQ_ROUTING_HV_SINT:
  271. e->set = kvm_hv_set_sint;
  272. e->hv_sint.vcpu = ue->u.hv_sint.vcpu;
  273. e->hv_sint.sint = ue->u.hv_sint.sint;
  274. break;
  275. default:
  276. return -EINVAL;
  277. }
  278. return 0;
  279. }
  280. bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
  281. struct kvm_vcpu **dest_vcpu)
  282. {
  283. int i, r = 0;
  284. struct kvm_vcpu *vcpu;
  285. if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
  286. return true;
  287. kvm_for_each_vcpu(i, vcpu, kvm) {
  288. if (!kvm_apic_present(vcpu))
  289. continue;
  290. if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
  291. irq->dest_id, irq->dest_mode))
  292. continue;
  293. if (++r == 2)
  294. return false;
  295. *dest_vcpu = vcpu;
  296. }
  297. return r == 1;
  298. }
  299. EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
  300. #define IOAPIC_ROUTING_ENTRY(irq) \
  301. { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
  302. .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
  303. #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
  304. #define PIC_ROUTING_ENTRY(irq) \
  305. { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
  306. .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
  307. #define ROUTING_ENTRY2(irq) \
  308. IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
  309. static const struct kvm_irq_routing_entry default_routing[] = {
  310. ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
  311. ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
  312. ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
  313. ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
  314. ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
  315. ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
  316. ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
  317. ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
  318. ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
  319. ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
  320. ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
  321. ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
  322. };
  323. int kvm_setup_default_irq_routing(struct kvm *kvm)
  324. {
  325. return kvm_set_irq_routing(kvm, default_routing,
  326. ARRAY_SIZE(default_routing), 0);
  327. }
  328. static const struct kvm_irq_routing_entry empty_routing[] = {};
  329. int kvm_setup_empty_irq_routing(struct kvm *kvm)
  330. {
  331. return kvm_set_irq_routing(kvm, empty_routing, 0, 0);
  332. }
  333. void kvm_arch_post_irq_routing_update(struct kvm *kvm)
  334. {
  335. if (!irqchip_split(kvm))
  336. return;
  337. kvm_make_scan_ioapic_request(kvm);
  338. }
  339. void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
  340. ulong *ioapic_handled_vectors)
  341. {
  342. struct kvm *kvm = vcpu->kvm;
  343. struct kvm_kernel_irq_routing_entry *entry;
  344. struct kvm_irq_routing_table *table;
  345. u32 i, nr_ioapic_pins;
  346. int idx;
  347. idx = srcu_read_lock(&kvm->irq_srcu);
  348. table = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
  349. nr_ioapic_pins = min_t(u32, table->nr_rt_entries,
  350. kvm->arch.nr_reserved_ioapic_pins);
  351. for (i = 0; i < nr_ioapic_pins; ++i) {
  352. hlist_for_each_entry(entry, &table->map[i], link) {
  353. struct kvm_lapic_irq irq;
  354. if (entry->type != KVM_IRQ_ROUTING_MSI)
  355. continue;
  356. kvm_set_msi_irq(vcpu->kvm, entry, &irq);
  357. if (irq.trig_mode && kvm_apic_match_dest(vcpu, NULL, 0,
  358. irq.dest_id, irq.dest_mode))
  359. __set_bit(irq.vector, ioapic_handled_vectors);
  360. }
  361. }
  362. srcu_read_unlock(&kvm->irq_srcu, idx);
  363. }
  364. void kvm_arch_irq_routing_update(struct kvm *kvm)
  365. {
  366. kvm_hv_irq_routing_update(kvm);
  367. }