irq_comm.c 11 KB

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