blob: 8ecd48d31800a19236e9db8a7db284f65e4a29ad [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
55 if (irq->dest_mode == 0 && irq->dest_id == 0xff &&
James Sullivand1ebdbf2015-03-18 19:26:04 -060056 kvm_lowest_prio_delivery(irq)) {
Gleb Natapov343f94f2009-03-05 16:34:54 +020057 printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
Gleb Natapov1e08ec42012-09-13 17:19:24 +030058 irq->delivery_mode = APIC_DM_FIXED;
59 }
60
Yang Zhangb4f22252013-04-11 19:21:37 +080061 if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
Gleb Natapov1e08ec42012-09-13 17:19:24 +030062 return r;
Gleb Natapov343f94f2009-03-05 16:34:54 +020063
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;
117 irq->dest_mode = (1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo;
118 irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
119 irq->delivery_mode = e->msi.data & 0x700;
James Sullivan93bbf0b2015-03-18 19:26:03 -0600120 irq->msi_redir_hint = ((e->msi.address_lo
121 & MSI_ADDR_REDIRECTION_LOWPRI) > 0);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200122 irq->level = 1;
123 irq->shorthand = 0;
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200124}
Feng Wud84f1e02015-09-18 22:29:49 +0800125EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200126
Radim Krčmář371313132016-07-12 22:09:27 +0200127static inline bool kvm_msi_route_invalid(struct kvm *kvm,
128 struct kvm_kernel_irq_routing_entry *e)
129{
130 return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff);
131}
132
Michael S. Tsirkinbd2b53b2010-11-18 19:09:08 +0200133int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
Yang Zhangaa2fbe62013-04-11 19:21:40 +0800134 struct kvm *kvm, int irq_source_id, int level, bool line_status)
Sheng Yang79950e12009-02-10 13:57:06 +0800135{
Gleb Natapov58c2dde2009-03-05 16:35:04 +0200136 struct kvm_lapic_irq irq;
Sheng Yang79950e12009-02-10 13:57:06 +0800137
Radim Krčmář371313132016-07-12 22:09:27 +0200138 if (kvm_msi_route_invalid(kvm, e))
139 return -EINVAL;
140
Gleb Natapov1a6e4a82009-08-24 11:54:19 +0300141 if (!level)
142 return -1;
143
Radim Krčmář371313132016-07-12 22:09:27 +0200144 kvm_set_msi_irq(kvm, e, &irq);
Gleb Natapov1000ff82009-07-07 16:00:57 +0300145
Yang Zhangb4f22252013-04-11 19:21:37 +0800146 return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
Sheng Yang79950e12009-02-10 13:57:06 +0800147}
148
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200149
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100150static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e,
151 struct kvm *kvm, int irq_source_id, int level,
152 bool line_status)
153{
154 if (!level)
155 return -1;
156
157 return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint);
158}
159
Paolo Bonzinib97e6de2015-10-28 19:16:47 +0100160int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
161 struct kvm *kvm, int irq_source_id, int level,
162 bool line_status)
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200163{
164 struct kvm_lapic_irq irq;
165 int r;
166
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100167 switch (e->type) {
168 case KVM_IRQ_ROUTING_HV_SINT:
169 return kvm_hv_set_sint(e, kvm, irq_source_id, level,
170 line_status);
Paolo Bonzinib97e6de2015-10-28 19:16:47 +0100171
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100172 case KVM_IRQ_ROUTING_MSI:
173 if (kvm_msi_route_invalid(kvm, e))
174 return -EINVAL;
Radim Krčmář371313132016-07-12 22:09:27 +0200175
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100176 kvm_set_msi_irq(kvm, e, &irq);
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200177
Paolo Bonzinia2b07732016-11-17 15:55:47 +0100178 if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL))
179 return r;
180 break;
181
182 default:
183 break;
184 }
185
186 return -EWOULDBLOCK;
Michael S. Tsirkin01f21882012-10-17 18:06:02 +0200187}
188
Sheng Yang5550af42008-10-15 20:15:06 +0800189int kvm_request_irq_source_id(struct kvm *kvm)
190{
191 unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300192 int irq_source_id;
193
194 mutex_lock(&kvm->irq_lock);
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300195 irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
Mark McLoughlin61552362008-12-01 13:57:48 +0000196
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300197 if (irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800198 printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200199 irq_source_id = -EFAULT;
200 goto unlock;
Mark McLoughlin61552362008-12-01 13:57:48 +0000201 }
202
203 ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
Alex Williamson7a844282012-09-21 11:58:03 -0600204 ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
Mark McLoughlin61552362008-12-01 13:57:48 +0000205 set_bit(irq_source_id, bitmap);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200206unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300207 mutex_unlock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000208
Sheng Yang5550af42008-10-15 20:15:06 +0800209 return irq_source_id;
210}
211
212void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
213{
Mark McLoughlin61552362008-12-01 13:57:48 +0000214 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
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300217 mutex_lock(&kvm->irq_lock);
Mark McLoughlin61552362008-12-01 13:57:48 +0000218 if (irq_source_id < 0 ||
Marcelo Tosatticd5a2682009-10-17 22:47:23 -0300219 irq_source_id >= BITS_PER_LONG) {
Sheng Yang5550af42008-10-15 20:15:06 +0800220 printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200221 goto unlock;
Sheng Yang5550af42008-10-15 20:15:06 +0800222 }
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200223 clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
David Hildenbrand900ab142017-04-07 10:50:41 +0200224 if (!irqchip_kernel(kvm))
Marcelo Tosattie50212b2009-10-29 13:44:17 -0200225 goto unlock;
226
Michael S. Tsirkin1a577b722012-07-19 13:45:20 +0300227 kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
David Hildenbrand90bca052017-04-07 10:50:23 +0200228 kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
Jiri Slaby0c6ddce2009-09-25 09:33:38 +0200229unlock:
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300230 mutex_unlock(&kvm->irq_lock);
Sheng Yang5550af42008-10-15 20:15:06 +0800231}
Avi Kivity75858a82009-01-04 17:10:50 +0200232
233void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
234 struct kvm_irq_mask_notifier *kimn)
235{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300236 mutex_lock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200237 kimn->irq = irq;
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100238 hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300239 mutex_unlock(&kvm->irq_lock);
Avi Kivity75858a82009-01-04 17:10:50 +0200240}
241
242void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
243 struct kvm_irq_mask_notifier *kimn)
244{
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300245 mutex_lock(&kvm->irq_lock);
Gleb Natapov280aa172009-08-24 11:54:24 +0300246 hlist_del_rcu(&kimn->link);
Marcelo Tosattifa40a822009-06-04 15:08:24 -0300247 mutex_unlock(&kvm->irq_lock);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100248 synchronize_srcu(&kvm->irq_srcu);
Avi Kivity75858a82009-01-04 17:10:50 +0200249}
250
Gleb Natapov4a994352010-07-11 15:32:23 +0300251void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
252 bool mask)
Avi Kivity75858a82009-01-04 17:10:50 +0200253{
254 struct kvm_irq_mask_notifier *kimn;
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100255 int idx, gsi;
Avi Kivity75858a82009-01-04 17:10:50 +0200256
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100257 idx = srcu_read_lock(&kvm->irq_srcu);
Paul Mackerras9957c862014-06-30 20:51:11 +1000258 gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
Gleb Natapov4a994352010-07-11 15:32:23 +0300259 if (gsi != -1)
Paolo Bonzini6ef768f2014-11-20 13:45:31 +0100260 hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
Gleb Natapov4a994352010-07-11 15:32:23 +0300261 if (kimn->irq == gsi)
262 kimn->func(kimn, mask);
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100263 srcu_read_unlock(&kvm->irq_srcu, idx);
Avi Kivity75858a82009-01-04 17:10:50 +0200264}
265
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200266bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
267{
268 return irqchip_in_kernel(kvm);
269}
270
Radim Krčmářc63cf532016-07-12 22:09:26 +0200271int kvm_set_routing_entry(struct kvm *kvm,
272 struct kvm_kernel_irq_routing_entry *e,
Alexander Grafe8cde092013-04-15 23:23:21 +0200273 const struct kvm_irq_routing_entry *ue)
Avi Kivity399ec802008-11-19 13:58:46 +0200274{
David Hildenbrand5c0aea02017-04-28 17:06:20 +0200275 /* We can't check irqchip_in_kernel() here as some callers are
276 * currently inititalizing the irqchip. Other callers should therefore
277 * check kvm_arch_can_set_irq_routing() before calling this function.
278 */
Avi Kivity399ec802008-11-19 13:58:46 +0200279 switch (ue->type) {
280 case KVM_IRQ_ROUTING_IRQCHIP:
David Hildenbrand8bf463f2017-04-07 10:50:20 +0200281 if (irqchip_split(kvm))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200282 return -EINVAL;
David Hildenbrand445ee822017-04-07 10:50:30 +0200283 e->irqchip.pin = ue->u.irqchip.pin;
Avi Kivity399ec802008-11-19 13:58:46 +0200284 switch (ue->u.irqchip.irqchip) {
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100285 case KVM_IRQCHIP_PIC_SLAVE:
David Hildenbrand445ee822017-04-07 10:50:30 +0200286 e->irqchip.pin += PIC_NUM_PINS / 2;
Radim Krčmáře5dc4872016-12-16 16:10:04 +0100287 /* fall through */
Avi Kivity399ec802008-11-19 13:58:46 +0200288 case KVM_IRQCHIP_PIC_MASTER:
David Hildenbrand445ee822017-04-07 10:50:30 +0200289 if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200290 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200291 e->set = kvm_set_pic_irq;
292 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200293 case KVM_IRQCHIP_IOAPIC:
David Hildenbrand445ee822017-04-07 10:50:30 +0200294 if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
David Hildenbrand43ae3122017-04-07 10:50:34 +0200295 return -EINVAL;
Chris Wrightefbc1002009-05-01 14:15:43 -0700296 e->set = kvm_set_ioapic_irq;
Avi Kivity399ec802008-11-19 13:58:46 +0200297 break;
298 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200299 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200300 }
301 e->irqchip.irqchip = ue->u.irqchip.irqchip;
Avi Kivity399ec802008-11-19 13:58:46 +0200302 break;
Sheng Yang79950e12009-02-10 13:57:06 +0800303 case KVM_IRQ_ROUTING_MSI:
304 e->set = kvm_set_msi;
305 e->msi.address_lo = ue->u.msi.address_lo;
306 e->msi.address_hi = ue->u.msi.address_hi;
307 e->msi.data = ue->u.msi.data;
Radim Krčmář371313132016-07-12 22:09:27 +0200308
309 if (kvm_msi_route_invalid(kvm, e))
David Hildenbrand43ae3122017-04-07 10:50:34 +0200310 return -EINVAL;
Sheng Yang79950e12009-02-10 13:57:06 +0800311 break;
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300312 case KVM_IRQ_ROUTING_HV_SINT:
313 e->set = kvm_hv_set_sint;
314 e->hv_sint.vcpu = ue->u.hv_sint.vcpu;
315 e->hv_sint.sint = ue->u.hv_sint.sint;
316 break;
Avi Kivity399ec802008-11-19 13:58:46 +0200317 default:
David Hildenbrand43ae3122017-04-07 10:50:34 +0200318 return -EINVAL;
Avi Kivity399ec802008-11-19 13:58:46 +0200319 }
Gleb Natapov46e624b2009-08-24 11:54:20 +0300320
David Hildenbrand43ae3122017-04-07 10:50:34 +0200321 return 0;
Avi Kivity399ec802008-11-19 13:58:46 +0200322}
323
Feng Wu8feb4a02015-09-18 22:29:47 +0800324bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
325 struct kvm_vcpu **dest_vcpu)
326{
327 int i, r = 0;
328 struct kvm_vcpu *vcpu;
329
330 if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
331 return true;
332
333 kvm_for_each_vcpu(i, vcpu, kvm) {
334 if (!kvm_apic_present(vcpu))
335 continue;
336
337 if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
338 irq->dest_id, irq->dest_mode))
339 continue;
340
341 if (++r == 2)
342 return false;
343
344 *dest_vcpu = vcpu;
345 }
346
347 return r == 1;
348}
349EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
350
Avi Kivity399ec802008-11-19 13:58:46 +0200351#define IOAPIC_ROUTING_ENTRY(irq) \
352 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700353 .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
Avi Kivity399ec802008-11-19 13:58:46 +0200354#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
355
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100356#define PIC_ROUTING_ENTRY(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200357 { .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
Mark Rustad25f97ff2014-07-25 06:27:03 -0700358 .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
Radim Krcmar3bf58e92014-11-21 18:06:19 +0100359#define ROUTING_ENTRY2(irq) \
Avi Kivity399ec802008-11-19 13:58:46 +0200360 IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
Avi Kivity399ec802008-11-19 13:58:46 +0200361
362static const struct kvm_irq_routing_entry default_routing[] = {
363 ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
364 ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
365 ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
366 ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
367 ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
368 ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
369 ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
370 ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
371 ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
372 ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
373 ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
374 ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
Avi Kivity399ec802008-11-19 13:58:46 +0200375};
376
377int kvm_setup_default_irq_routing(struct kvm *kvm)
378{
379 return kvm_set_irq_routing(kvm, default_routing,
380 ARRAY_SIZE(default_routing), 0);
381}
Steve Rutherford49df6392015-07-29 23:21:40 -0700382
383static const struct kvm_irq_routing_entry empty_routing[] = {};
384
385int kvm_setup_empty_irq_routing(struct kvm *kvm)
386{
387 return kvm_set_irq_routing(kvm, empty_routing, 0, 0);
388}
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700389
Andrey Smetaninabdb0802015-11-10 15:36:31 +0300390void kvm_arch_post_irq_routing_update(struct kvm *kvm)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700391{
Radim Krčmář826da322016-12-16 16:10:06 +0100392 if (!irqchip_split(kvm))
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700393 return;
394 kvm_make_scan_ioapic_request(kvm);
395}
396
Andrey Smetanin63086302015-11-10 15:36:32 +0300397void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
398 ulong *ioapic_handled_vectors)
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700399{
400 struct kvm *kvm = vcpu->kvm;
401 struct kvm_kernel_irq_routing_entry *entry;
402 struct kvm_irq_routing_table *table;
403 u32 i, nr_ioapic_pins;
404 int idx;
405
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700406 idx = srcu_read_lock(&kvm->irq_srcu);
407 table = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
408 nr_ioapic_pins = min_t(u32, table->nr_rt_entries,
409 kvm->arch.nr_reserved_ioapic_pins);
410 for (i = 0; i < nr_ioapic_pins; ++i) {
411 hlist_for_each_entry(entry, &table->map[i], link) {
Radim Krčmář3159d362016-07-12 22:09:21 +0200412 struct kvm_lapic_irq irq;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700413
414 if (entry->type != KVM_IRQ_ROUTING_MSI)
415 continue;
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700416
Radim Krčmář371313132016-07-12 22:09:27 +0200417 kvm_set_msi_irq(vcpu->kvm, entry, &irq);
Radim Krčmář3159d362016-07-12 22:09:21 +0200418
419 if (irq.level && kvm_apic_match_dest(vcpu, NULL, 0,
420 irq.dest_id, irq.dest_mode))
421 __set_bit(irq.vector, ioapic_handled_vectors);
Steve Rutherfordb053b2a2015-07-29 23:32:35 -0700422 }
423 }
424 srcu_read_unlock(&kvm->irq_srcu, idx);
425}
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300426
Andrey Smetanin5c9194122015-11-10 15:36:34 +0300427void kvm_arch_irq_routing_update(struct kvm *kvm)
428{
429 kvm_hv_irq_routing_update(kvm);
430}