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