Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 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 | * |
Nicolas Kaiser | 9611c18 | 2010-10-06 14:23:22 +0200 | [diff] [blame] | 20 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <linux/kvm_host.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Yang Zhang | c7c9c56 | 2013-01-25 10:18:51 +0800 | [diff] [blame] | 25 | #include <linux/export.h> |
Marcelo Tosatti | 229456f | 2009-06-17 09:22:14 -0300 | [diff] [blame] | 26 | #include <trace/events/kvm.h> |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 27 | |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 28 | #include <asm/msidef.h> |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 29 | |
Xiantao Zhang | 3de42dc | 2008-10-06 13:48:45 +0800 | [diff] [blame] | 30 | #include "irq.h" |
| 31 | |
| 32 | #include "ioapic.h" |
| 33 | |
James Sullivan | d1ebdbf | 2015-03-18 19:26:04 -0600 | [diff] [blame] | 34 | #include "lapic.h" |
| 35 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 36 | #include "hyperv.h" |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 37 | #include "x86.h" |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 38 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 39 | static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 40 | struct kvm *kvm, int irq_source_id, int level, |
| 41 | bool line_status) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 42 | { |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 43 | struct kvm_pic *pic = pic_irqchip(kvm); |
Michael S. Tsirkin | 1a577b7 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 44 | return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 47 | static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 48 | struct kvm *kvm, int irq_source_id, int level, |
| 49 | bool line_status) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 50 | { |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 51 | struct kvm_ioapic *ioapic = kvm->arch.vioapic; |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 52 | return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level, |
| 53 | line_status); |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 56 | int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, |
Joerg Roedel | 9e4aabe | 2016-02-29 16:04:43 +0100 | [diff] [blame] | 57 | struct kvm_lapic_irq *irq, struct dest_map *dest_map) |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 58 | { |
| 59 | int i, r = -1; |
| 60 | struct kvm_vcpu *vcpu, *lowest = NULL; |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 61 | unsigned long dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)]; |
| 62 | unsigned int dest_vcpus = 0; |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 63 | |
| 64 | if (irq->dest_mode == 0 && irq->dest_id == 0xff && |
James Sullivan | d1ebdbf | 2015-03-18 19:26:04 -0600 | [diff] [blame] | 65 | kvm_lowest_prio_delivery(irq)) { |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 66 | printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n"); |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 67 | irq->delivery_mode = APIC_DM_FIXED; |
| 68 | } |
| 69 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 70 | if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map)) |
Gleb Natapov | 1e08ec4 | 2012-09-13 17:19:24 +0300 | [diff] [blame] | 71 | return r; |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 72 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 73 | memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap)); |
| 74 | |
Gleb Natapov | 988a2ca | 2009-06-09 15:56:29 +0300 | [diff] [blame] | 75 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 76 | if (!kvm_apic_present(vcpu)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 77 | continue; |
Sheng Yang | 74a3a8f | 2009-03-04 13:33:02 +0800 | [diff] [blame] | 78 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 79 | if (!kvm_apic_match_dest(vcpu, src, irq->shorthand, |
| 80 | irq->dest_id, irq->dest_mode)) |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 81 | continue; |
| 82 | |
James Sullivan | d1ebdbf | 2015-03-18 19:26:04 -0600 | [diff] [blame] | 83 | if (!kvm_lowest_prio_delivery(irq)) { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 84 | if (r < 0) |
| 85 | r = 0; |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 86 | r += kvm_apic_set_irq(vcpu, irq, dest_map); |
Chris Lalancette | aefd18f | 2010-06-16 17:11:13 -0400 | [diff] [blame] | 87 | } else if (kvm_lapic_enabled(vcpu)) { |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 88 | if (!kvm_vector_hashing_enabled()) { |
| 89 | if (!lowest) |
| 90 | lowest = vcpu; |
| 91 | else if (kvm_apic_compare_prio(vcpu, lowest) < 0) |
| 92 | lowest = vcpu; |
| 93 | } else { |
| 94 | __set_bit(i, dest_vcpu_bitmap); |
| 95 | dest_vcpus++; |
| 96 | } |
Gleb Natapov | e103571 | 2009-03-05 16:34:59 +0200 | [diff] [blame] | 97 | } |
Gleb Natapov | 343f94f | 2009-03-05 16:34:54 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Feng Wu | 52004014 | 2016-01-25 16:53:33 +0800 | [diff] [blame] | 100 | if (dest_vcpus != 0) { |
| 101 | int idx = kvm_vector_to_index(irq->vector, dest_vcpus, |
| 102 | dest_vcpu_bitmap, KVM_MAX_VCPUS); |
| 103 | |
| 104 | lowest = kvm_get_vcpu(kvm, idx); |
| 105 | } |
| 106 | |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 107 | if (lowest) |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 108 | r = kvm_apic_set_irq(lowest, irq, dest_map); |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 109 | |
| 110 | return r; |
Sheng Yang | 116191b | 2009-02-11 16:03:37 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Feng Wu | d84f1e0 | 2015-09-18 22:29:49 +0800 | [diff] [blame] | 113 | void kvm_set_msi_irq(struct kvm_kernel_irq_routing_entry *e, |
| 114 | struct kvm_lapic_irq *irq) |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 115 | { |
| 116 | trace_kvm_msi_set_irq(e->msi.address_lo, e->msi.data); |
| 117 | |
| 118 | irq->dest_id = (e->msi.address_lo & |
| 119 | MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT; |
| 120 | irq->vector = (e->msi.data & |
| 121 | MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT; |
| 122 | irq->dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo; |
| 123 | irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data; |
| 124 | irq->delivery_mode = e->msi.data & 0x700; |
James Sullivan | 93bbf0b | 2015-03-18 19:26:03 -0600 | [diff] [blame] | 125 | irq->msi_redir_hint = ((e->msi.address_lo |
| 126 | & MSI_ADDR_REDIRECTION_LOWPRI) > 0); |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 127 | irq->level = 1; |
| 128 | irq->shorthand = 0; |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 129 | } |
Feng Wu | d84f1e0 | 2015-09-18 22:29:49 +0800 | [diff] [blame] | 130 | EXPORT_SYMBOL_GPL(kvm_set_msi_irq); |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 131 | |
Michael S. Tsirkin | bd2b53b | 2010-11-18 19:09:08 +0200 | [diff] [blame] | 132 | int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e, |
Yang Zhang | aa2fbe6 | 2013-04-11 19:21:40 +0800 | [diff] [blame] | 133 | struct kvm *kvm, int irq_source_id, int level, bool line_status) |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 134 | { |
Gleb Natapov | 58c2dde | 2009-03-05 16:35:04 +0200 | [diff] [blame] | 135 | struct kvm_lapic_irq irq; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 136 | |
Gleb Natapov | 1a6e4a8 | 2009-08-24 11:54:19 +0300 | [diff] [blame] | 137 | if (!level) |
| 138 | return -1; |
| 139 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 140 | kvm_set_msi_irq(e, &irq); |
Gleb Natapov | 1000ff8 | 2009-07-07 16:00:57 +0300 | [diff] [blame] | 141 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 142 | return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL); |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 145 | |
Paolo Bonzini | b97e6de | 2015-10-28 19:16:47 +0100 | [diff] [blame] | 146 | int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e, |
| 147 | struct kvm *kvm, int irq_source_id, int level, |
| 148 | bool line_status) |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 149 | { |
| 150 | struct kvm_lapic_irq irq; |
| 151 | int r; |
| 152 | |
Paolo Bonzini | b97e6de | 2015-10-28 19:16:47 +0100 | [diff] [blame] | 153 | if (unlikely(e->type != KVM_IRQ_ROUTING_MSI)) |
| 154 | return -EWOULDBLOCK; |
| 155 | |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 156 | kvm_set_msi_irq(e, &irq); |
| 157 | |
Yang Zhang | b4f2225 | 2013-04-11 19:21:37 +0800 | [diff] [blame] | 158 | if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL)) |
Michael S. Tsirkin | 01f2188 | 2012-10-17 18:06:02 +0200 | [diff] [blame] | 159 | return r; |
| 160 | else |
| 161 | return -EWOULDBLOCK; |
| 162 | } |
| 163 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 164 | int kvm_request_irq_source_id(struct kvm *kvm) |
| 165 | { |
| 166 | unsigned long *bitmap = &kvm->arch.irq_sources_bitmap; |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 167 | int irq_source_id; |
| 168 | |
| 169 | mutex_lock(&kvm->irq_lock); |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 170 | irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 171 | |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 172 | if (irq_source_id >= BITS_PER_LONG) { |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 173 | printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n"); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 174 | irq_source_id = -EFAULT; |
| 175 | goto unlock; |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 179 | ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 180 | set_bit(irq_source_id, bitmap); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 181 | unlock: |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 182 | mutex_unlock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 183 | |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 184 | return irq_source_id; |
| 185 | } |
| 186 | |
| 187 | void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id) |
| 188 | { |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 189 | ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID); |
Alex Williamson | 7a84428 | 2012-09-21 11:58:03 -0600 | [diff] [blame] | 190 | ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 191 | |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 192 | mutex_lock(&kvm->irq_lock); |
Mark McLoughlin | 6155236 | 2008-12-01 13:57:48 +0000 | [diff] [blame] | 193 | if (irq_source_id < 0 || |
Marcelo Tosatti | cd5a268 | 2009-10-17 22:47:23 -0300 | [diff] [blame] | 194 | irq_source_id >= BITS_PER_LONG) { |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 195 | printk(KERN_ERR "kvm: IRQ source ID out of range!\n"); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 196 | goto unlock; |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 197 | } |
Marcelo Tosatti | e50212b | 2009-10-29 13:44:17 -0200 | [diff] [blame] | 198 | clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap); |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 199 | if (!ioapic_in_kernel(kvm)) |
Marcelo Tosatti | e50212b | 2009-10-29 13:44:17 -0200 | [diff] [blame] | 200 | goto unlock; |
| 201 | |
Michael S. Tsirkin | 1a577b7 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 202 | kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id); |
Michael S. Tsirkin | 1a577b7 | 2012-07-19 13:45:20 +0300 | [diff] [blame] | 203 | kvm_pic_clear_all(pic_irqchip(kvm), irq_source_id); |
Jiri Slaby | 0c6ddce | 2009-09-25 09:33:38 +0200 | [diff] [blame] | 204 | unlock: |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 205 | mutex_unlock(&kvm->irq_lock); |
Sheng Yang | 5550af4 | 2008-10-15 20:15:06 +0800 | [diff] [blame] | 206 | } |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 207 | |
| 208 | void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq, |
| 209 | struct kvm_irq_mask_notifier *kimn) |
| 210 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 211 | mutex_lock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 212 | kimn->irq = irq; |
Paolo Bonzini | 6ef768f | 2014-11-20 13:45:31 +0100 | [diff] [blame] | 213 | hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 214 | mutex_unlock(&kvm->irq_lock); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq, |
| 218 | struct kvm_irq_mask_notifier *kimn) |
| 219 | { |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 220 | mutex_lock(&kvm->irq_lock); |
Gleb Natapov | 280aa17 | 2009-08-24 11:54:24 +0300 | [diff] [blame] | 221 | hlist_del_rcu(&kimn->link); |
Marcelo Tosatti | fa40a82 | 2009-06-04 15:08:24 -0300 | [diff] [blame] | 222 | mutex_unlock(&kvm->irq_lock); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 223 | synchronize_srcu(&kvm->irq_srcu); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 226 | void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin, |
| 227 | bool mask) |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 228 | { |
| 229 | struct kvm_irq_mask_notifier *kimn; |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 230 | int idx, gsi; |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 231 | |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 232 | idx = srcu_read_lock(&kvm->irq_srcu); |
Paul Mackerras | 9957c86 | 2014-06-30 20:51:11 +1000 | [diff] [blame] | 233 | gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin); |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 234 | if (gsi != -1) |
Paolo Bonzini | 6ef768f | 2014-11-20 13:45:31 +0100 | [diff] [blame] | 235 | hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link) |
Gleb Natapov | 4a99435 | 2010-07-11 15:32:23 +0300 | [diff] [blame] | 236 | if (kimn->irq == gsi) |
| 237 | kimn->func(kimn, mask); |
Christian Borntraeger | 719d93c | 2014-01-16 13:44:20 +0100 | [diff] [blame] | 238 | srcu_read_unlock(&kvm->irq_srcu, idx); |
Avi Kivity | 75858a8 | 2009-01-04 17:10:50 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 241 | static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e, |
| 242 | struct kvm *kvm, int irq_source_id, int level, |
| 243 | bool line_status) |
| 244 | { |
| 245 | if (!level) |
| 246 | return -1; |
| 247 | |
| 248 | return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint); |
| 249 | } |
| 250 | |
Paul Mackerras | 8ba918d | 2014-06-30 20:51:10 +1000 | [diff] [blame] | 251 | int kvm_set_routing_entry(struct kvm_kernel_irq_routing_entry *e, |
Alexander Graf | e8cde09 | 2013-04-15 23:23:21 +0200 | [diff] [blame] | 252 | const struct kvm_irq_routing_entry *ue) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 253 | { |
| 254 | int r = -EINVAL; |
| 255 | int delta; |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 256 | unsigned max_pin; |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 257 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 258 | switch (ue->type) { |
| 259 | case KVM_IRQ_ROUTING_IRQCHIP: |
| 260 | delta = 0; |
| 261 | switch (ue->u.irqchip.irqchip) { |
| 262 | case KVM_IRQCHIP_PIC_MASTER: |
| 263 | e->set = kvm_set_pic_irq; |
Michael S. Tsirkin | 93b6547 | 2012-07-19 13:55:53 +0300 | [diff] [blame] | 264 | max_pin = PIC_NUM_PINS; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 265 | break; |
| 266 | case KVM_IRQCHIP_PIC_SLAVE: |
Gleb Natapov | 4925663 | 2009-02-04 17:28:14 +0200 | [diff] [blame] | 267 | e->set = kvm_set_pic_irq; |
Michael S. Tsirkin | 93b6547 | 2012-07-19 13:55:53 +0300 | [diff] [blame] | 268 | max_pin = PIC_NUM_PINS; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 269 | delta = 8; |
| 270 | break; |
| 271 | case KVM_IRQCHIP_IOAPIC: |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 272 | max_pin = KVM_IOAPIC_NUM_PINS; |
Chris Wright | efbc100 | 2009-05-01 14:15:43 -0700 | [diff] [blame] | 273 | e->set = kvm_set_ioapic_irq; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 274 | break; |
| 275 | default: |
| 276 | goto out; |
| 277 | } |
| 278 | e->irqchip.irqchip = ue->u.irqchip.irqchip; |
| 279 | e->irqchip.pin = ue->u.irqchip.pin + delta; |
Marcelo Tosatti | d72118c | 2010-01-12 16:42:09 -0200 | [diff] [blame] | 280 | if (e->irqchip.pin >= max_pin) |
Gleb Natapov | 3e71f88 | 2009-08-24 11:54:21 +0300 | [diff] [blame] | 281 | goto out; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 282 | break; |
Sheng Yang | 79950e1 | 2009-02-10 13:57:06 +0800 | [diff] [blame] | 283 | case KVM_IRQ_ROUTING_MSI: |
| 284 | e->set = kvm_set_msi; |
| 285 | e->msi.address_lo = ue->u.msi.address_lo; |
| 286 | e->msi.address_hi = ue->u.msi.address_hi; |
| 287 | e->msi.data = ue->u.msi.data; |
| 288 | break; |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 289 | case KVM_IRQ_ROUTING_HV_SINT: |
| 290 | e->set = kvm_hv_set_sint; |
| 291 | e->hv_sint.vcpu = ue->u.hv_sint.vcpu; |
| 292 | e->hv_sint.sint = ue->u.hv_sint.sint; |
| 293 | break; |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 294 | default: |
| 295 | goto out; |
| 296 | } |
Gleb Natapov | 46e624b | 2009-08-24 11:54:20 +0300 | [diff] [blame] | 297 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 298 | r = 0; |
| 299 | out: |
| 300 | return r; |
| 301 | } |
| 302 | |
Feng Wu | 8feb4a0 | 2015-09-18 22:29:47 +0800 | [diff] [blame] | 303 | bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq, |
| 304 | struct kvm_vcpu **dest_vcpu) |
| 305 | { |
| 306 | int i, r = 0; |
| 307 | struct kvm_vcpu *vcpu; |
| 308 | |
| 309 | if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu)) |
| 310 | return true; |
| 311 | |
| 312 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 313 | if (!kvm_apic_present(vcpu)) |
| 314 | continue; |
| 315 | |
| 316 | if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand, |
| 317 | irq->dest_id, irq->dest_mode)) |
| 318 | continue; |
| 319 | |
| 320 | if (++r == 2) |
| 321 | return false; |
| 322 | |
| 323 | *dest_vcpu = vcpu; |
| 324 | } |
| 325 | |
| 326 | return r == 1; |
| 327 | } |
| 328 | EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu); |
| 329 | |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 330 | #define IOAPIC_ROUTING_ENTRY(irq) \ |
| 331 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
Mark Rustad | 25f97ff | 2014-07-25 06:27:03 -0700 | [diff] [blame] | 332 | .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } } |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 333 | #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq) |
| 334 | |
Radim Krcmar | 3bf58e9 | 2014-11-21 18:06:19 +0100 | [diff] [blame] | 335 | #define PIC_ROUTING_ENTRY(irq) \ |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 336 | { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \ |
Mark Rustad | 25f97ff | 2014-07-25 06:27:03 -0700 | [diff] [blame] | 337 | .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } } |
Radim Krcmar | 3bf58e9 | 2014-11-21 18:06:19 +0100 | [diff] [blame] | 338 | #define ROUTING_ENTRY2(irq) \ |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 339 | IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq) |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 340 | |
| 341 | static const struct kvm_irq_routing_entry default_routing[] = { |
| 342 | ROUTING_ENTRY2(0), ROUTING_ENTRY2(1), |
| 343 | ROUTING_ENTRY2(2), ROUTING_ENTRY2(3), |
| 344 | ROUTING_ENTRY2(4), ROUTING_ENTRY2(5), |
| 345 | ROUTING_ENTRY2(6), ROUTING_ENTRY2(7), |
| 346 | ROUTING_ENTRY2(8), ROUTING_ENTRY2(9), |
| 347 | ROUTING_ENTRY2(10), ROUTING_ENTRY2(11), |
| 348 | ROUTING_ENTRY2(12), ROUTING_ENTRY2(13), |
| 349 | ROUTING_ENTRY2(14), ROUTING_ENTRY2(15), |
| 350 | ROUTING_ENTRY1(16), ROUTING_ENTRY1(17), |
| 351 | ROUTING_ENTRY1(18), ROUTING_ENTRY1(19), |
| 352 | ROUTING_ENTRY1(20), ROUTING_ENTRY1(21), |
| 353 | ROUTING_ENTRY1(22), ROUTING_ENTRY1(23), |
Avi Kivity | 399ec80 | 2008-11-19 13:58:46 +0200 | [diff] [blame] | 354 | }; |
| 355 | |
| 356 | int kvm_setup_default_irq_routing(struct kvm *kvm) |
| 357 | { |
| 358 | return kvm_set_irq_routing(kvm, default_routing, |
| 359 | ARRAY_SIZE(default_routing), 0); |
| 360 | } |
Steve Rutherford | 49df639 | 2015-07-29 23:21:40 -0700 | [diff] [blame] | 361 | |
| 362 | static const struct kvm_irq_routing_entry empty_routing[] = {}; |
| 363 | |
| 364 | int kvm_setup_empty_irq_routing(struct kvm *kvm) |
| 365 | { |
| 366 | return kvm_set_irq_routing(kvm, empty_routing, 0, 0); |
| 367 | } |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 368 | |
Andrey Smetanin | abdb080 | 2015-11-10 15:36:31 +0300 | [diff] [blame] | 369 | void kvm_arch_post_irq_routing_update(struct kvm *kvm) |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 370 | { |
| 371 | if (ioapic_in_kernel(kvm) || !irqchip_in_kernel(kvm)) |
| 372 | return; |
| 373 | kvm_make_scan_ioapic_request(kvm); |
| 374 | } |
| 375 | |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 376 | void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu, |
| 377 | ulong *ioapic_handled_vectors) |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 378 | { |
| 379 | struct kvm *kvm = vcpu->kvm; |
| 380 | struct kvm_kernel_irq_routing_entry *entry; |
| 381 | struct kvm_irq_routing_table *table; |
| 382 | u32 i, nr_ioapic_pins; |
| 383 | int idx; |
| 384 | |
| 385 | /* kvm->irq_routing must be read after clearing |
| 386 | * KVM_SCAN_IOAPIC. */ |
| 387 | smp_mb(); |
| 388 | idx = srcu_read_lock(&kvm->irq_srcu); |
| 389 | table = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu); |
| 390 | nr_ioapic_pins = min_t(u32, table->nr_rt_entries, |
| 391 | kvm->arch.nr_reserved_ioapic_pins); |
| 392 | for (i = 0; i < nr_ioapic_pins; ++i) { |
| 393 | hlist_for_each_entry(entry, &table->map[i], link) { |
| 394 | u32 dest_id, dest_mode; |
Radim Krčmář | 13db773 | 2015-10-08 20:30:00 +0200 | [diff] [blame] | 395 | bool level; |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 396 | |
| 397 | if (entry->type != KVM_IRQ_ROUTING_MSI) |
| 398 | continue; |
| 399 | dest_id = (entry->msi.address_lo >> 12) & 0xff; |
| 400 | dest_mode = (entry->msi.address_lo >> 2) & 0x1; |
Radim Krčmář | 13db773 | 2015-10-08 20:30:00 +0200 | [diff] [blame] | 401 | level = entry->msi.data & MSI_DATA_TRIGGER_LEVEL; |
| 402 | if (level && kvm_apic_match_dest(vcpu, NULL, 0, |
| 403 | dest_id, dest_mode)) { |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 404 | u32 vector = entry->msi.data & 0xff; |
| 405 | |
| 406 | __set_bit(vector, |
Andrey Smetanin | 6308630 | 2015-11-10 15:36:32 +0300 | [diff] [blame] | 407 | ioapic_handled_vectors); |
Steve Rutherford | b053b2a | 2015-07-29 23:32:35 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | } |
| 411 | srcu_read_unlock(&kvm->irq_srcu, idx); |
| 412 | } |
Andrey Smetanin | 5c919412 | 2015-11-10 15:36:34 +0300 | [diff] [blame] | 413 | |
| 414 | int kvm_arch_set_irq(struct kvm_kernel_irq_routing_entry *irq, struct kvm *kvm, |
| 415 | int irq_source_id, int level, bool line_status) |
| 416 | { |
| 417 | switch (irq->type) { |
| 418 | case KVM_IRQ_ROUTING_HV_SINT: |
| 419 | return kvm_hv_set_sint(irq, kvm, irq_source_id, level, |
| 420 | line_status); |
| 421 | default: |
| 422 | return -EWOULDBLOCK; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void kvm_arch_irq_routing_update(struct kvm *kvm) |
| 427 | { |
| 428 | kvm_hv_irq_routing_update(kvm); |
| 429 | } |