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