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