irq_comm.c 12 KB

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