blob: 3cc3b2d130a0c277e6b4cb6f59aa2a391d2253f6 [file] [log] [blame]
Xiantao Zhang3de42dc2008-10-06 13:48:45 +08001/*
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 Kaiser9611c182010-10-06 14:23:22 +020020 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080021 */
22
23#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Yang Zhangc7c9c562013-01-25 10:18:51 +080025#include <linux/export.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010026#include <linux/rculist.h>
27
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080029
Sheng Yang79950e12009-02-10 13:57:06 +080030#include <asm/msidef.h>
Sheng Yang79950e12009-02-10 13:57:06 +080031
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080032#include "irq.h"
33
34#include "ioapic.h"
35
James Sullivand1ebdbf2015-03-18 19:26:04 -060036#include "lapic.h"
37
Andrey Smetanin5c9194122015-11-10 15:36:34 +030038#include "hyperv.h"
Feng Wu520040142016-01-25 16:53:33 +080039#include "x86.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030040
Gleb Natapov49256632009-02-04 17:28:14 +020041static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080042 struct kvm *kvm, int irq_source_id, int level,
43 bool line_status)
Avi Kivity399ec802008-11-19 13:58:46 +020044{
David Hildenbrand90bca052017-04-07 10:50:23 +020045 struct kvm_pic *pic = kvm->arch.vpic;
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030046 return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
Avi Kivity399ec802008-11-19 13:58:46 +020047}
48
Gleb Natapov49256632009-02-04 17:28:14 +020049static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080050 struct kvm *kvm, int irq_source_id, int level,
51 bool line_status)
Avi Kivity399ec802008-11-19 13:58:46 +020052{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030053 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Yang Zhangaa2fbe62013-04-11 19:21:40 +080054 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
55 line_status);
Avi Kivity399ec802008-11-19 13:58:46 +020056}
57
Gleb Natapov58c2dde2009-03-05 16:35:04 +020058int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +010059 struct kvm_lapic_irq *irq, struct dest_map *dest_map)
Gleb Natapov58c2dde2009-03-05 16:35:04 +020060{
61 int i, r = -1;
62 struct kvm_vcpu *vcpu, *lowest = NULL;
Feng Wu520040142016-01-25 16:53:33 +080063 unsigned long dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
64 unsigned int dest_vcpus = 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +020065
66 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
James Sullivand1ebdbf2015-03-18 19:26:04 -060067 kvm_lowest_prio_delivery(irq)) {
Gleb Natapov343f94f2009-03-05 16:34:54 +020068 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
Gleb Natapov1e08ec42012-09-13 17:19:24 +030069 irq->delivery_mode = APIC_DM_FIXED;
70 }
71
Yang Zhangb4f22252013-04-11 19:21:37 +080072 if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
Gleb Natapov1e08ec42012-09-13 17:19:24 +030073 return r;
Gleb Natapov343f94f2009-03-05 16:34:54 +020074
Feng Wu520040142016-01-25 16:53:33 +080075 memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
76
Gleb Natapov988a2ca2009-06-09 15:56:29 +030077 kvm_for_each_vcpu(i, vcpu, kvm) {
78 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020079 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080080
Gleb Natapov58c2dde2009-03-05 16:35:04 +020081 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
82 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020083 continue;
84
James Sullivand1ebdbf2015-03-18 19:26:04 -060085 if (!kvm_lowest_prio_delivery(irq)) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +020086 if (r < 0)
87 r = 0;
Yang Zhangb4f22252013-04-11 19:21:37 +080088 r += kvm_apic_set_irq(vcpu, irq, dest_map);
Chris Lalancetteaefd18f2010-06-16 17:11:13 -040089 } else if (kvm_lapic_enabled(vcpu)) {
Feng Wu520040142016-01-25 16:53:33 +080090 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 Natapove1035712009-03-05 16:34:59 +020099 }
Gleb Natapov343f94f2009-03-05 16:34:54 +0200100 }
101
Feng Wu520040142016-01-25 16:53:33 +0800102 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 Natapov58c2dde2009-03-05 16:35:04 +0200109 if (lowest)
Yang Zhangb4f22252013-04-11 19:21:37 +0800110 r = kvm_apic_set_irq(lowest, irq, dest_map);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200111
112 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800113}
114
Radim Krčmář371313132016-07-12 22:09:27 +0200115void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
Feng Wud84f1e02015-09-18 22:29:49 +0800116 struct kvm_lapic_irq *irq)
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200117{
Radim Krčmář371313132016-07-12 22:09:27 +0200118 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. Tsirkin01f21882012-10-17 18:06:02 +0200121
122 irq->dest_id = (e->msi.address_lo &
123 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Radim Krčmář371313132016-07-12 22:09:27 +0200124 if (kvm->arch.x2apic_format)
125 irq->dest_id |= MSI_ADDR_EXT_DEST_ID(e->msi.address_hi);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200126 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 Sullivan93bbf0b2015-03-18 19:26:03 -0600131 irq->msi_redir_hint = ((e->msi.address_lo
132 & MSI_ADDR_REDIRECTION_LOWPRI) > 0);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200133 irq->level = 1;
134 irq->shorthand = 0;
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200135}
Feng Wud84f1e02015-09-18 22:29:49 +0800136EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200137
Radim Krčmář371313132016-07-12 22:09:27 +0200138static 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. Tsirkinbd2b53b2010-11-18 19:09:08 +0200144int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800145 struct kvm *kvm, int irq_source_id, int level, bool line_status)
Sheng Yang79950e12009-02-10 13:57:06 +0800146{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200147 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800148
Radim Krčmář371313132016-07-12 22:09:27 +0200149 if (kvm_msi_route_invalid(kvm, e))
150 return -EINVAL;
151
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300152 if (!level)
153 return -1;
154
Radim Krčmář371313132016-07-12 22:09:27 +0200155 kvm_set_msi_irq(kvm, e, &irq);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300156
Yang Zhangb4f22252013-04-11 19:21:37 +0800157 return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
Sheng Yang79950e12009-02-10 13:57:06 +0800158}
159
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200160
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100161static 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 Bonzinib97e6de2015-10-28 19:16:47 +0100171int 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. Tsirkin01f21882012-10-17 18:06:02 +0200174{
175 struct kvm_lapic_irq irq;
176 int r;
177
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100178 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 Bonzinib97e6de2015-10-28 19:16:47 +0100182
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100183 case KVM_IRQ_ROUTING_MSI:
184 if (kvm_msi_route_invalid(kvm, e))
185 return -EINVAL;
Radim Krčmář371313132016-07-12 22:09:27 +0200186
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100187 kvm_set_msi_irq(kvm, e, &irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200188
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100189 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. Tsirkin01f21882012-10-17 18:06:02 +0200198}
199
Sheng Yang5550af42008-10-15 20:15:06 +0800200int kvm_request_irq_source_id(struct kvm *kvm)
201{
202 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300203 int irq_source_id;
204
205 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300206 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000207
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300208 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800209 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200210 irq_source_id = -EFAULT;
211 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000212 }
213
214 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600215 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
Mark McLoughlin61552362008-12-01 13:57:48 +0000216 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200217unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300218 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000219
Sheng Yang5550af42008-10-15 20:15:06 +0800220 return irq_source_id;
221}
222
223void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
224{
Mark McLoughlin61552362008-12-01 13:57:48 +0000225 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600226 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
Mark McLoughlin61552362008-12-01 13:57:48 +0000227
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300228 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000229 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300230 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800231 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200232 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800233 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200234 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
David Hildenbrand900ab142017-04-07 10:50:41 +0200235 if (!irqchip_kernel(kvm))
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200236 goto unlock;
237
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300238 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
David Hildenbrand90bca052017-04-07 10:50:23 +0200239 kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200240unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300241 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800242}
Avi Kivity75858a82009-01-04 17:10:50 +0200243
244void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
245 struct kvm_irq_mask_notifier *kimn)
246{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300247 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200248 kimn->irq = irq;
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100249 hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300250 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200251}
252
253void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
254 struct kvm_irq_mask_notifier *kimn)
255{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300256 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300257 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300258 mutex_unlock(&kvm->irq_lock);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100259 synchronize_srcu(&kvm->irq_srcu);
Avi Kivity75858a82009-01-04 17:10:50 +0200260}
261
Gleb Natapov4a994352010-07-11 15:32:23 +0300262void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
263 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200264{
265 struct kvm_irq_mask_notifier *kimn;
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100266 int idx, gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200267
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100268 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +1000269 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
Gleb Natapov4a994352010-07-11 15:32:23 +0300270 if (gsi != -1)
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100271 hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
Gleb Natapov4a994352010-07-11 15:32:23 +0300272 if (kimn->irq == gsi)
273 kimn->func(kimn, mask);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100274 srcu_read_unlock(&kvm->irq_srcu, idx);
Avi Kivity75858a82009-01-04 17:10:50 +0200275}
276
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200277bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
278{
279 return irqchip_in_kernel(kvm);
280}
281
Radim Krčmářc63cf532016-07-12 22:09:26 +0200282int kvm_set_routing_entry(struct kvm *kvm,
283 struct kvm_kernel_irq_routing_entry *e,
Alexander Grafe8cde092013-04-15 23:23:21 +0200284 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200285{
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200286 /* 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 Kivity399ec802008-11-19 13:58:46 +0200290 switch (ue->type) {
291 case KVM_IRQ_ROUTING_IRQCHIP:
David Hildenbrand8bf463f2017-04-07 10:50:20 +0200292 if (irqchip_split(kvm))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200293 return -EINVAL;
David Hildenbrand445ee822017-04-07 10:50:30 +0200294 e->irqchip.pin = ue->u.irqchip.pin;
Avi Kivity399ec802008-11-19 13:58:46 +0200295 switch (ue->u.irqchip.irqchip) {
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100296 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand445ee822017-04-07 10:50:30 +0200297 e->irqchip.pin += PIC_NUM_PINS / 2;
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100298 /* fall through */
Avi Kivity399ec802008-11-19 13:58:46 +0200299 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand445ee822017-04-07 10:50:30 +0200300 if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200301 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200302 e->set = kvm_set_pic_irq;
303 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200304 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand445ee822017-04-07 10:50:30 +0200305 if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200306 return -EINVAL;
Chris Wrightefbc1002009-05-01 14:15:43 -0700307 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200308 break;
309 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200310 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200311 }
312 e->irqchip.irqchip = ue->u.irqchip.irqchip;
Avi Kivity399ec802008-11-19 13:58:46 +0200313 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800314 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ář371313132016-07-12 22:09:27 +0200319
320 if (kvm_msi_route_invalid(kvm, e))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200321 return -EINVAL;
Sheng Yang79950e12009-02-10 13:57:06 +0800322 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300323 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 Kivity399ec802008-11-19 13:58:46 +0200328 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200329 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200330 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300331
David Hildenbrand43ae3122017-04-07 10:50:34 +0200332 return 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200333}
334
Feng Wu8feb4a02015-09-18 22:29:47 +0800335bool 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}
360EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
361
Avi Kivity399ec802008-11-19 13:58:46 +0200362#define IOAPIC_ROUTING_ENTRY(irq) \
363 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700364 .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
Avi Kivity399ec802008-11-19 13:58:46 +0200365#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
366
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100367#define PIC_ROUTING_ENTRY(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200368 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700369 .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100370#define ROUTING_ENTRY2(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200371 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
Avi Kivity399ec802008-11-19 13:58:46 +0200372
373static 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 Kivity399ec802008-11-19 13:58:46 +0200386};
387
388int 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 Rutherford49df6392015-07-29 23:21:40 -0700393
394static const struct kvm_irq_routing_entry empty_routing[] = {};
395
396int kvm_setup_empty_irq_routing(struct kvm *kvm)
397{
398 return kvm_set_irq_routing(kvm, empty_routing, 0, 0);
399}
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700400
Andrey Smetaninabdb0802015-11-10 15:36:31 +0300401void kvm_arch_post_irq_routing_update(struct kvm *kvm)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700402{
Radim Krčmář826da322016-12-16 16:10:06 +0100403 if (!irqchip_split(kvm))
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700404 return;
405 kvm_make_scan_ioapic_request(kvm);
406}
407
Andrey Smetanin63086302015-11-10 15:36:32 +0300408void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
409 ulong *ioapic_handled_vectors)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700410{
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 Rutherfordb053b2a2015-07-29 23:32:35 -0700417 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ář3159d362016-07-12 22:09:21 +0200423 struct kvm_lapic_irq irq;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700424
425 if (entry->type != KVM_IRQ_ROUTING_MSI)
426 continue;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700427
Radim Krčmář371313132016-07-12 22:09:27 +0200428 kvm_set_msi_irq(vcpu->kvm, entry, &irq);
Radim Krčmář3159d362016-07-12 22:09:21 +0200429
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 Rutherfordb053b2a2015-07-29 23:32:35 -0700433 }
434 }
435 srcu_read_unlock(&kvm->irq_srcu, idx);
436}
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300437
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300438void kvm_arch_irq_routing_update(struct kvm *kvm)
439{
440 kvm_hv_irq_routing_update(kvm);
441}