blob: c47d2acec52934eae639a8c4b347fbfa660caca6 [file] [log] [blame]
Thomas Gleixner3b20eb22019-05-29 16:57:35 -07001// SPDX-License-Identifier: GPL-2.0-only
Xiantao Zhang3de42dc2008-10-06 13:48:45 +08002/*
3 * irq_comm.c: Common API for in kernel interrupt controller
4 * Copyright (c) 2007, Intel Corporation.
5 *
Xiantao Zhang3de42dc2008-10-06 13:48:45 +08006 * Authors:
7 * Yaozu (Eddie) Dong <Eddie.dong@intel.com>
8 *
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080010 */
11
12#include <linux/kvm_host.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Yang Zhangc7c9c562013-01-25 10:18:51 +080014#include <linux/export.h>
Ingo Molnarb2d09102017-02-04 01:27:20 +010015#include <linux/rculist.h>
16
Marcelo Tosatti229456f2009-06-17 09:22:14 -030017#include <trace/events/kvm.h>
Sheng Yang79950e12009-02-10 13:57:06 +080018
Sheng Yang79950e12009-02-10 13:57:06 +080019#include <asm/msidef.h>
Sheng Yang79950e12009-02-10 13:57:06 +080020
Xiantao Zhang3de42dc2008-10-06 13:48:45 +080021#include "irq.h"
22
23#include "ioapic.h"
24
James Sullivand1ebdbf2015-03-18 19:26:04 -060025#include "lapic.h"
26
Andrey Smetanin5c9194122015-11-10 15:36:34 +030027#include "hyperv.h"
Feng Wu520040142016-01-25 16:53:33 +080028#include "x86.h"
Andrey Smetanin5c9194122015-11-10 15:36:34 +030029
Gleb Natapov49256632009-02-04 17:28:14 +020030static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080031 struct kvm *kvm, int irq_source_id, int level,
32 bool line_status)
Avi Kivity399ec802008-11-19 13:58:46 +020033{
David Hildenbrand90bca052017-04-07 10:50:23 +020034 struct kvm_pic *pic = kvm->arch.vpic;
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +030035 return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
Avi Kivity399ec802008-11-19 13:58:46 +020036}
37
Gleb Natapov49256632009-02-04 17:28:14 +020038static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +080039 struct kvm *kvm, int irq_source_id, int level,
40 bool line_status)
Avi Kivity399ec802008-11-19 13:58:46 +020041{
Gleb Natapov1a6e4a82009-08-24 11:54:19 +030042 struct kvm_ioapic *ioapic = kvm->arch.vioapic;
Yang Zhangaa2fbe62013-04-11 19:21:40 +080043 return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
44 line_status);
Avi Kivity399ec802008-11-19 13:58:46 +020045}
46
Gleb Natapov58c2dde2009-03-05 16:35:04 +020047int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
Joerg Roedel9e4aabe2016-02-29 16:04:43 +010048 struct kvm_lapic_irq *irq, struct dest_map *dest_map)
Gleb Natapov58c2dde2009-03-05 16:35:04 +020049{
50 int i, r = -1;
51 struct kvm_vcpu *vcpu, *lowest = NULL;
Feng Wu520040142016-01-25 16:53:33 +080052 unsigned long dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
53 unsigned int dest_vcpus = 0;
Gleb Natapov58c2dde2009-03-05 16:35:04 +020054
Wanpeng Lidfd146f2019-11-21 11:17:12 +080055 if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
56 return r;
57
Peter Xuc96001c2019-12-04 20:07:18 +010058 if (irq->dest_mode == APIC_DEST_PHYSICAL &&
59 irq->dest_id == 0xff && kvm_lowest_prio_delivery(irq)) {
Gleb Natapov343f94f2009-03-05 16:34:54 +020060 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
Gleb Natapov1e08ec42012-09-13 17:19:24 +030061 irq->delivery_mode = APIC_DM_FIXED;
62 }
63
Feng Wu520040142016-01-25 16:53:33 +080064 memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
65
Gleb Natapov988a2ca2009-06-09 15:56:29 +030066 kvm_for_each_vcpu(i, vcpu, kvm) {
67 if (!kvm_apic_present(vcpu))
Gleb Natapov343f94f2009-03-05 16:34:54 +020068 continue;
Sheng Yang74a3a8f2009-03-04 13:33:02 +080069
Gleb Natapov58c2dde2009-03-05 16:35:04 +020070 if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
71 irq->dest_id, irq->dest_mode))
Gleb Natapov343f94f2009-03-05 16:34:54 +020072 continue;
73
James Sullivand1ebdbf2015-03-18 19:26:04 -060074 if (!kvm_lowest_prio_delivery(irq)) {
Gleb Natapov58c2dde2009-03-05 16:35:04 +020075 if (r < 0)
76 r = 0;
Yang Zhangb4f22252013-04-11 19:21:37 +080077 r += kvm_apic_set_irq(vcpu, irq, dest_map);
Wei Yang7d2296b2019-03-31 19:17:21 -070078 } else if (kvm_apic_sw_enabled(vcpu->arch.apic)) {
Feng Wu520040142016-01-25 16:53:33 +080079 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 Natapove1035712009-03-05 16:34:59 +020088 }
Gleb Natapov343f94f2009-03-05 16:34:54 +020089 }
90
Feng Wu520040142016-01-25 16:53:33 +080091 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 Natapov58c2dde2009-03-05 16:35:04 +020098 if (lowest)
Yang Zhangb4f22252013-04-11 19:21:37 +080099 r = kvm_apic_set_irq(lowest, irq, dest_map);
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200100
101 return r;
Sheng Yang116191b2009-02-11 16:03:37 +0800102}
103
Radim Krčmář371313132016-07-12 22:09:27 +0200104void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
Feng Wud84f1e02015-09-18 22:29:49 +0800105 struct kvm_lapic_irq *irq)
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200106{
Radim Krčmář371313132016-07-12 22:09:27 +0200107 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. Tsirkin01f21882012-10-17 18:06:02 +0200110
111 irq->dest_id = (e->msi.address_lo &
112 MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
Radim Krčmář371313132016-07-12 22:09:27 +0200113 if (kvm->arch.x2apic_format)
114 irq->dest_id |= MSI_ADDR_EXT_DEST_ID(e->msi.address_hi);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200115 irq->vector = (e->msi.data &
116 MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
Peter Xuc96001c2019-12-04 20:07:18 +0100117 irq->dest_mode = kvm_lapic_irq_dest_mode(
118 !!((1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo));
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200119 irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
120 irq->delivery_mode = e->msi.data & 0x700;
James Sullivan93bbf0b2015-03-18 19:26:03 -0600121 irq->msi_redir_hint = ((e->msi.address_lo
122 & MSI_ADDR_REDIRECTION_LOWPRI) > 0);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200123 irq->level = 1;
Peter Xu150a84f2019-12-04 20:07:21 +0100124 irq->shorthand = APIC_DEST_NOSHORT;
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200125}
Feng Wud84f1e02015-09-18 22:29:49 +0800126EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200127
Radim Krčmář371313132016-07-12 22:09:27 +0200128static 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. Tsirkinbd2b53b2010-11-18 19:09:08 +0200134int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800135 struct kvm *kvm, int irq_source_id, int level, bool line_status)
Sheng Yang79950e12009-02-10 13:57:06 +0800136{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200137 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800138
Radim Krčmář371313132016-07-12 22:09:27 +0200139 if (kvm_msi_route_invalid(kvm, e))
140 return -EINVAL;
141
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300142 if (!level)
143 return -1;
144
Radim Krčmář371313132016-07-12 22:09:27 +0200145 kvm_set_msi_irq(kvm, e, &irq);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300146
Yang Zhangb4f22252013-04-11 19:21:37 +0800147 return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
Sheng Yang79950e12009-02-10 13:57:06 +0800148}
149
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200150
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100151static 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 Bonzinib97e6de2015-10-28 19:16:47 +0100161int 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. Tsirkin01f21882012-10-17 18:06:02 +0200164{
165 struct kvm_lapic_irq irq;
166 int r;
167
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100168 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 Bonzinib97e6de2015-10-28 19:16:47 +0100172
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100173 case KVM_IRQ_ROUTING_MSI:
174 if (kvm_msi_route_invalid(kvm, e))
175 return -EINVAL;
Radim Krčmář371313132016-07-12 22:09:27 +0200176
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100177 kvm_set_msi_irq(kvm, e, &irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200178
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100179 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. Tsirkin01f21882012-10-17 18:06:02 +0200188}
189
Sheng Yang5550af42008-10-15 20:15:06 +0800190int kvm_request_irq_source_id(struct kvm *kvm)
191{
192 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300193 int irq_source_id;
194
195 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300196 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000197
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300198 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800199 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200200 irq_source_id = -EFAULT;
201 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000202 }
203
204 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600205 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
Mark McLoughlin61552362008-12-01 13:57:48 +0000206 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200207unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300208 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000209
Sheng Yang5550af42008-10-15 20:15:06 +0800210 return irq_source_id;
211}
212
213void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
214{
Mark McLoughlin61552362008-12-01 13:57:48 +0000215 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600216 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
Mark McLoughlin61552362008-12-01 13:57:48 +0000217
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300218 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000219 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300220 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800221 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200222 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800223 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200224 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
David Hildenbrand900ab142017-04-07 10:50:41 +0200225 if (!irqchip_kernel(kvm))
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200226 goto unlock;
227
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300228 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
David Hildenbrand90bca052017-04-07 10:50:23 +0200229 kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200230unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300231 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800232}
Avi Kivity75858a82009-01-04 17:10:50 +0200233
234void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
235 struct kvm_irq_mask_notifier *kimn)
236{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300237 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200238 kimn->irq = irq;
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100239 hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300240 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200241}
242
243void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
244 struct kvm_irq_mask_notifier *kimn)
245{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300246 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300247 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300248 mutex_unlock(&kvm->irq_lock);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100249 synchronize_srcu(&kvm->irq_srcu);
Avi Kivity75858a82009-01-04 17:10:50 +0200250}
251
Gleb Natapov4a994352010-07-11 15:32:23 +0300252void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
253 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200254{
255 struct kvm_irq_mask_notifier *kimn;
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100256 int idx, gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200257
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100258 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +1000259 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
Gleb Natapov4a994352010-07-11 15:32:23 +0300260 if (gsi != -1)
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100261 hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
Gleb Natapov4a994352010-07-11 15:32:23 +0300262 if (kimn->irq == gsi)
263 kimn->func(kimn, mask);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100264 srcu_read_unlock(&kvm->irq_srcu, idx);
Avi Kivity75858a82009-01-04 17:10:50 +0200265}
266
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200267bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
268{
269 return irqchip_in_kernel(kvm);
270}
271
Radim Krčmářc63cf532016-07-12 22:09:26 +0200272int kvm_set_routing_entry(struct kvm *kvm,
273 struct kvm_kernel_irq_routing_entry *e,
Alexander Grafe8cde092013-04-15 23:23:21 +0200274 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200275{
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200276 /* 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 Kivity399ec802008-11-19 13:58:46 +0200280 switch (ue->type) {
281 case KVM_IRQ_ROUTING_IRQCHIP:
David Hildenbrand8bf463f2017-04-07 10:50:20 +0200282 if (irqchip_split(kvm))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200283 return -EINVAL;
David Hildenbrand445ee822017-04-07 10:50:30 +0200284 e->irqchip.pin = ue->u.irqchip.pin;
Avi Kivity399ec802008-11-19 13:58:46 +0200285 switch (ue->u.irqchip.irqchip) {
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100286 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand445ee822017-04-07 10:50:30 +0200287 e->irqchip.pin += PIC_NUM_PINS / 2;
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100288 /* fall through */
Avi Kivity399ec802008-11-19 13:58:46 +0200289 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand445ee822017-04-07 10:50:30 +0200290 if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200291 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200292 e->set = kvm_set_pic_irq;
293 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200294 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand445ee822017-04-07 10:50:30 +0200295 if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200296 return -EINVAL;
Chris Wrightefbc1002009-05-01 14:15:43 -0700297 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200298 break;
299 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200300 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200301 }
302 e->irqchip.irqchip = ue->u.irqchip.irqchip;
Avi Kivity399ec802008-11-19 13:58:46 +0200303 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800304 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ář371313132016-07-12 22:09:27 +0200309
310 if (kvm_msi_route_invalid(kvm, e))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200311 return -EINVAL;
Sheng Yang79950e12009-02-10 13:57:06 +0800312 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300313 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 Kivity399ec802008-11-19 13:58:46 +0200318 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200319 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200320 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300321
David Hildenbrand43ae3122017-04-07 10:50:34 +0200322 return 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200323}
324
Feng Wu8feb4a02015-09-18 22:29:47 +0800325bool 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}
350EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
351
Avi Kivity399ec802008-11-19 13:58:46 +0200352#define IOAPIC_ROUTING_ENTRY(irq) \
353 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700354 .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
Avi Kivity399ec802008-11-19 13:58:46 +0200355#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
356
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100357#define PIC_ROUTING_ENTRY(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200358 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700359 .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100360#define ROUTING_ENTRY2(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200361 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
Avi Kivity399ec802008-11-19 13:58:46 +0200362
363static 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 Kivity399ec802008-11-19 13:58:46 +0200376};
377
378int 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 Rutherford49df6392015-07-29 23:21:40 -0700383
384static const struct kvm_irq_routing_entry empty_routing[] = {};
385
386int kvm_setup_empty_irq_routing(struct kvm *kvm)
387{
388 return kvm_set_irq_routing(kvm, empty_routing, 0, 0);
389}
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700390
Andrey Smetaninabdb0802015-11-10 15:36:31 +0300391void kvm_arch_post_irq_routing_update(struct kvm *kvm)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700392{
Radim Krčmář826da322016-12-16 16:10:06 +0100393 if (!irqchip_split(kvm))
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700394 return;
395 kvm_make_scan_ioapic_request(kvm);
396}
397
Andrey Smetanin63086302015-11-10 15:36:32 +0300398void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
399 ulong *ioapic_handled_vectors)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700400{
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 Rutherfordb053b2a2015-07-29 23:32:35 -0700407 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ář3159d362016-07-12 22:09:21 +0200413 struct kvm_lapic_irq irq;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700414
415 if (entry->type != KVM_IRQ_ROUTING_MSI)
416 continue;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700417
Radim Krčmář371313132016-07-12 22:09:27 +0200418 kvm_set_msi_irq(vcpu->kvm, entry, &irq);
Radim Krčmář3159d362016-07-12 22:09:21 +0200419
Miaohe Lin7455a832020-02-14 10:32:38 +0800420 if (irq.trig_mode &&
Peter Xu5c69d5c2019-12-04 20:07:20 +0100421 kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
Radim Krčmář3159d362016-07-12 22:09:21 +0200422 irq.dest_id, irq.dest_mode))
423 __set_bit(irq.vector, ioapic_handled_vectors);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700424 }
425 }
426 srcu_read_unlock(&kvm->irq_srcu, idx);
427}
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300428
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300429void kvm_arch_irq_routing_update(struct kvm *kvm)
430{
431 kvm_hv_irq_routing_update(kvm);
432}