xref: /linux/arch/x86/kvm/irq_comm.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
26ef768faSPaolo Bonzini /*
36ef768faSPaolo Bonzini  * irq_comm.c: Common API for in kernel interrupt controller
46ef768faSPaolo Bonzini  * Copyright (c) 2007, Intel Corporation.
56ef768faSPaolo Bonzini  *
66ef768faSPaolo Bonzini  * Authors:
76ef768faSPaolo Bonzini  *   Yaozu (Eddie) Dong <Eddie.dong@intel.com>
86ef768faSPaolo Bonzini  *
96ef768faSPaolo Bonzini  * Copyright 2010 Red Hat, Inc. and/or its affiliates.
106ef768faSPaolo Bonzini  */
118d20bd63SSean Christopherson #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
126ef768faSPaolo Bonzini 
136ef768faSPaolo Bonzini #include <linux/kvm_host.h>
146ef768faSPaolo Bonzini #include <linux/slab.h>
156ef768faSPaolo Bonzini #include <linux/export.h>
16b2d09103SIngo Molnar #include <linux/rculist.h>
17b2d09103SIngo Molnar 
186ef768faSPaolo Bonzini #include <trace/events/kvm.h>
196ef768faSPaolo Bonzini 
206ef768faSPaolo Bonzini #include "irq.h"
216ef768faSPaolo Bonzini 
226ef768faSPaolo Bonzini #include "ioapic.h"
236ef768faSPaolo Bonzini 
24d1ebdbf9SJames Sullivan #include "lapic.h"
25d1ebdbf9SJames Sullivan 
265c919412SAndrey Smetanin #include "hyperv.h"
2752004014SFeng Wu #include "x86.h"
2814243b38SDavid Woodhouse #include "xen.h"
295c919412SAndrey Smetanin 
kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)306ef768faSPaolo Bonzini static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
316ef768faSPaolo Bonzini 			   struct kvm *kvm, int irq_source_id, int level,
326ef768faSPaolo Bonzini 			   bool line_status)
336ef768faSPaolo Bonzini {
3490bca052SDavid Hildenbrand 	struct kvm_pic *pic = kvm->arch.vpic;
356ef768faSPaolo Bonzini 	return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
366ef768faSPaolo Bonzini }
376ef768faSPaolo Bonzini 
kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)386ef768faSPaolo Bonzini static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
396ef768faSPaolo Bonzini 			      struct kvm *kvm, int irq_source_id, int level,
406ef768faSPaolo Bonzini 			      bool line_status)
416ef768faSPaolo Bonzini {
426ef768faSPaolo Bonzini 	struct kvm_ioapic *ioapic = kvm->arch.vioapic;
436ef768faSPaolo Bonzini 	return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
446ef768faSPaolo Bonzini 				line_status);
456ef768faSPaolo Bonzini }
466ef768faSPaolo Bonzini 
kvm_irq_delivery_to_apic(struct kvm * kvm,struct kvm_lapic * src,struct kvm_lapic_irq * irq,struct dest_map * dest_map)476ef768faSPaolo Bonzini int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
489e4aabe2SJoerg Roedel 		struct kvm_lapic_irq *irq, struct dest_map *dest_map)
496ef768faSPaolo Bonzini {
5046808a4cSMarc Zyngier 	int r = -1;
516ef768faSPaolo Bonzini 	struct kvm_vcpu *vcpu, *lowest = NULL;
5246808a4cSMarc Zyngier 	unsigned long i, dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
5352004014SFeng Wu 	unsigned int dest_vcpus = 0;
546ef768faSPaolo Bonzini 
55dfd146fcSWanpeng Li 	if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
56dfd146fcSWanpeng Li 		return r;
57dfd146fcSWanpeng Li 
58c96001c5SPeter Xu 	if (irq->dest_mode == APIC_DEST_PHYSICAL &&
59c96001c5SPeter Xu 	    irq->dest_id == 0xff && kvm_lowest_prio_delivery(irq)) {
608d20bd63SSean Christopherson 		pr_info("apic: phys broadcast and lowest prio\n");
616ef768faSPaolo Bonzini 		irq->delivery_mode = APIC_DM_FIXED;
626ef768faSPaolo Bonzini 	}
636ef768faSPaolo Bonzini 
6452004014SFeng Wu 	memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
6552004014SFeng Wu 
666ef768faSPaolo Bonzini 	kvm_for_each_vcpu(i, vcpu, kvm) {
676ef768faSPaolo Bonzini 		if (!kvm_apic_present(vcpu))
686ef768faSPaolo Bonzini 			continue;
696ef768faSPaolo Bonzini 
706ef768faSPaolo Bonzini 		if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
716ef768faSPaolo Bonzini 					irq->dest_id, irq->dest_mode))
726ef768faSPaolo Bonzini 			continue;
736ef768faSPaolo Bonzini 
74d1ebdbf9SJames Sullivan 		if (!kvm_lowest_prio_delivery(irq)) {
756ef768faSPaolo Bonzini 			if (r < 0)
766ef768faSPaolo Bonzini 				r = 0;
776ef768faSPaolo Bonzini 			r += kvm_apic_set_irq(vcpu, irq, dest_map);
787d2296bfSWei Yang 		} else if (kvm_apic_sw_enabled(vcpu->arch.apic)) {
7952004014SFeng Wu 			if (!kvm_vector_hashing_enabled()) {
806ef768faSPaolo Bonzini 				if (!lowest)
816ef768faSPaolo Bonzini 					lowest = vcpu;
826ef768faSPaolo Bonzini 				else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
836ef768faSPaolo Bonzini 					lowest = vcpu;
8452004014SFeng Wu 			} else {
8552004014SFeng Wu 				__set_bit(i, dest_vcpu_bitmap);
8652004014SFeng Wu 				dest_vcpus++;
876ef768faSPaolo Bonzini 			}
886ef768faSPaolo Bonzini 		}
8952004014SFeng Wu 	}
9052004014SFeng Wu 
9152004014SFeng Wu 	if (dest_vcpus != 0) {
9252004014SFeng Wu 		int idx = kvm_vector_to_index(irq->vector, dest_vcpus,
9352004014SFeng Wu 					dest_vcpu_bitmap, KVM_MAX_VCPUS);
9452004014SFeng Wu 
9552004014SFeng Wu 		lowest = kvm_get_vcpu(kvm, idx);
9652004014SFeng Wu 	}
976ef768faSPaolo Bonzini 
986ef768faSPaolo Bonzini 	if (lowest)
996ef768faSPaolo Bonzini 		r = kvm_apic_set_irq(lowest, irq, dest_map);
1006ef768faSPaolo Bonzini 
1016ef768faSPaolo Bonzini 	return r;
1026ef768faSPaolo Bonzini }
1036ef768faSPaolo Bonzini 
kvm_set_msi_irq(struct kvm * kvm,struct kvm_kernel_irq_routing_entry * e,struct kvm_lapic_irq * irq)10437131313SRadim Krčmář void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
1056ef768faSPaolo Bonzini 		     struct kvm_lapic_irq *irq)
1066ef768faSPaolo Bonzini {
107485940e0SThomas Gleixner 	struct msi_msg msg = { .address_lo = e->msi.address_lo,
108485940e0SThomas Gleixner 			       .address_hi = e->msi.address_hi,
109485940e0SThomas Gleixner 			       .data = e->msi.data };
1106ef768faSPaolo Bonzini 
111485940e0SThomas Gleixner 	trace_kvm_msi_set_irq(msg.address_lo | (kvm->arch.x2apic_format ?
112485940e0SThomas Gleixner 			      (u64)msg.address_hi << 32 : 0), msg.data);
113485940e0SThomas Gleixner 
114485940e0SThomas Gleixner 	irq->dest_id = x86_msi_msg_get_destid(&msg, kvm->arch.x2apic_format);
115485940e0SThomas Gleixner 	irq->vector = msg.arch_data.vector;
116485940e0SThomas Gleixner 	irq->dest_mode = kvm_lapic_irq_dest_mode(msg.arch_addr_lo.dest_mode_logical);
117485940e0SThomas Gleixner 	irq->trig_mode = msg.arch_data.is_level;
118485940e0SThomas Gleixner 	irq->delivery_mode = msg.arch_data.delivery_mode << 8;
119485940e0SThomas Gleixner 	irq->msi_redir_hint = msg.arch_addr_lo.redirect_hint;
1206ef768faSPaolo Bonzini 	irq->level = 1;
121150a84feSPeter Xu 	irq->shorthand = APIC_DEST_NOSHORT;
1226ef768faSPaolo Bonzini }
123d84f1e07SFeng Wu EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
1246ef768faSPaolo Bonzini 
kvm_msi_route_invalid(struct kvm * kvm,struct kvm_kernel_irq_routing_entry * e)12537131313SRadim Krčmář static inline bool kvm_msi_route_invalid(struct kvm *kvm,
12637131313SRadim Krčmář 		struct kvm_kernel_irq_routing_entry *e)
12737131313SRadim Krčmář {
12837131313SRadim Krčmář 	return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff);
12937131313SRadim Krčmář }
13037131313SRadim Krčmář 
kvm_set_msi(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)1316ef768faSPaolo Bonzini int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
1326ef768faSPaolo Bonzini 		struct kvm *kvm, int irq_source_id, int level, bool line_status)
1336ef768faSPaolo Bonzini {
1346ef768faSPaolo Bonzini 	struct kvm_lapic_irq irq;
1356ef768faSPaolo Bonzini 
13637131313SRadim Krčmář 	if (kvm_msi_route_invalid(kvm, e))
13737131313SRadim Krčmář 		return -EINVAL;
13837131313SRadim Krčmář 
1396ef768faSPaolo Bonzini 	if (!level)
1406ef768faSPaolo Bonzini 		return -1;
1416ef768faSPaolo Bonzini 
14237131313SRadim Krčmář 	kvm_set_msi_irq(kvm, e, &irq);
1436ef768faSPaolo Bonzini 
1446ef768faSPaolo Bonzini 	return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
1456ef768faSPaolo Bonzini }
1466ef768faSPaolo Bonzini 
147*b4f69df0SVitaly Kuznetsov #ifdef CONFIG_KVM_HYPERV
kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)148a2b07739SPaolo Bonzini static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e,
149a2b07739SPaolo Bonzini 		    struct kvm *kvm, int irq_source_id, int level,
150a2b07739SPaolo Bonzini 		    bool line_status)
151a2b07739SPaolo Bonzini {
152a2b07739SPaolo Bonzini 	if (!level)
153a2b07739SPaolo Bonzini 		return -1;
154a2b07739SPaolo Bonzini 
155a2b07739SPaolo Bonzini 	return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint);
156a2b07739SPaolo Bonzini }
157*b4f69df0SVitaly Kuznetsov #endif
158a2b07739SPaolo Bonzini 
kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry * e,struct kvm * kvm,int irq_source_id,int level,bool line_status)159b97e6de9SPaolo Bonzini int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
160b97e6de9SPaolo Bonzini 			      struct kvm *kvm, int irq_source_id, int level,
161b97e6de9SPaolo Bonzini 			      bool line_status)
1626ef768faSPaolo Bonzini {
1636ef768faSPaolo Bonzini 	struct kvm_lapic_irq irq;
1646ef768faSPaolo Bonzini 	int r;
1656ef768faSPaolo Bonzini 
166a2b07739SPaolo Bonzini 	switch (e->type) {
167*b4f69df0SVitaly Kuznetsov #ifdef CONFIG_KVM_HYPERV
168a2b07739SPaolo Bonzini 	case KVM_IRQ_ROUTING_HV_SINT:
169a2b07739SPaolo Bonzini 		return kvm_hv_set_sint(e, kvm, irq_source_id, level,
170a2b07739SPaolo Bonzini 				       line_status);
171*b4f69df0SVitaly Kuznetsov #endif
172b97e6de9SPaolo Bonzini 
173a2b07739SPaolo Bonzini 	case KVM_IRQ_ROUTING_MSI:
17437131313SRadim Krčmář 		if (kvm_msi_route_invalid(kvm, e))
17537131313SRadim Krčmář 			return -EINVAL;
17637131313SRadim Krčmář 
17737131313SRadim Krčmář 		kvm_set_msi_irq(kvm, e, &irq);
1786ef768faSPaolo Bonzini 
1796ef768faSPaolo Bonzini 		if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL))
1806ef768faSPaolo Bonzini 			return r;
181a2b07739SPaolo Bonzini 		break;
182a2b07739SPaolo Bonzini 
18314243b38SDavid Woodhouse #ifdef CONFIG_KVM_XEN
18414243b38SDavid Woodhouse 	case KVM_IRQ_ROUTING_XEN_EVTCHN:
18514243b38SDavid Woodhouse 		if (!level)
18614243b38SDavid Woodhouse 			return -1;
18714243b38SDavid Woodhouse 
1888733068bSDavid Woodhouse 		return kvm_xen_set_evtchn_fast(&e->xen_evtchn, kvm);
18914243b38SDavid Woodhouse #endif
190a2b07739SPaolo Bonzini 	default:
191a2b07739SPaolo Bonzini 		break;
192a2b07739SPaolo Bonzini 	}
193a2b07739SPaolo Bonzini 
1946ef768faSPaolo Bonzini 	return -EWOULDBLOCK;
1956ef768faSPaolo Bonzini }
1966ef768faSPaolo Bonzini 
kvm_request_irq_source_id(struct kvm * kvm)1976ef768faSPaolo Bonzini int kvm_request_irq_source_id(struct kvm *kvm)
1986ef768faSPaolo Bonzini {
1996ef768faSPaolo Bonzini 	unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
2006ef768faSPaolo Bonzini 	int irq_source_id;
2016ef768faSPaolo Bonzini 
2026ef768faSPaolo Bonzini 	mutex_lock(&kvm->irq_lock);
2036ef768faSPaolo Bonzini 	irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
2046ef768faSPaolo Bonzini 
2056ef768faSPaolo Bonzini 	if (irq_source_id >= BITS_PER_LONG) {
2068d20bd63SSean Christopherson 		pr_warn("exhausted allocatable IRQ sources!\n");
2076ef768faSPaolo Bonzini 		irq_source_id = -EFAULT;
2086ef768faSPaolo Bonzini 		goto unlock;
2096ef768faSPaolo Bonzini 	}
2106ef768faSPaolo Bonzini 
2116ef768faSPaolo Bonzini 	ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
2126ef768faSPaolo Bonzini 	ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
2136ef768faSPaolo Bonzini 	set_bit(irq_source_id, bitmap);
2146ef768faSPaolo Bonzini unlock:
2156ef768faSPaolo Bonzini 	mutex_unlock(&kvm->irq_lock);
2166ef768faSPaolo Bonzini 
2176ef768faSPaolo Bonzini 	return irq_source_id;
2186ef768faSPaolo Bonzini }
2196ef768faSPaolo Bonzini 
kvm_free_irq_source_id(struct kvm * kvm,int irq_source_id)2206ef768faSPaolo Bonzini void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
2216ef768faSPaolo Bonzini {
2226ef768faSPaolo Bonzini 	ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
2236ef768faSPaolo Bonzini 	ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
2246ef768faSPaolo Bonzini 
2256ef768faSPaolo Bonzini 	mutex_lock(&kvm->irq_lock);
2266ef768faSPaolo Bonzini 	if (irq_source_id < 0 ||
2276ef768faSPaolo Bonzini 	    irq_source_id >= BITS_PER_LONG) {
2288d20bd63SSean Christopherson 		pr_err("IRQ source ID out of range!\n");
2296ef768faSPaolo Bonzini 		goto unlock;
2306ef768faSPaolo Bonzini 	}
2316ef768faSPaolo Bonzini 	clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
232900ab14cSDavid Hildenbrand 	if (!irqchip_kernel(kvm))
2336ef768faSPaolo Bonzini 		goto unlock;
2346ef768faSPaolo Bonzini 
2356ef768faSPaolo Bonzini 	kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
23690bca052SDavid Hildenbrand 	kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
2376ef768faSPaolo Bonzini unlock:
2386ef768faSPaolo Bonzini 	mutex_unlock(&kvm->irq_lock);
2396ef768faSPaolo Bonzini }
2406ef768faSPaolo Bonzini 
kvm_register_irq_mask_notifier(struct kvm * kvm,int irq,struct kvm_irq_mask_notifier * kimn)2416ef768faSPaolo Bonzini void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
2426ef768faSPaolo Bonzini 				    struct kvm_irq_mask_notifier *kimn)
2436ef768faSPaolo Bonzini {
2446ef768faSPaolo Bonzini 	mutex_lock(&kvm->irq_lock);
2456ef768faSPaolo Bonzini 	kimn->irq = irq;
2466ef768faSPaolo Bonzini 	hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
2476ef768faSPaolo Bonzini 	mutex_unlock(&kvm->irq_lock);
2486ef768faSPaolo Bonzini }
2496ef768faSPaolo Bonzini 
kvm_unregister_irq_mask_notifier(struct kvm * kvm,int irq,struct kvm_irq_mask_notifier * kimn)2506ef768faSPaolo Bonzini void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
2516ef768faSPaolo Bonzini 				      struct kvm_irq_mask_notifier *kimn)
2526ef768faSPaolo Bonzini {
2536ef768faSPaolo Bonzini 	mutex_lock(&kvm->irq_lock);
2546ef768faSPaolo Bonzini 	hlist_del_rcu(&kimn->link);
2556ef768faSPaolo Bonzini 	mutex_unlock(&kvm->irq_lock);
2566ef768faSPaolo Bonzini 	synchronize_srcu(&kvm->irq_srcu);
2576ef768faSPaolo Bonzini }
2586ef768faSPaolo Bonzini 
kvm_fire_mask_notifiers(struct kvm * kvm,unsigned irqchip,unsigned pin,bool mask)2596ef768faSPaolo Bonzini void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
2606ef768faSPaolo Bonzini 			     bool mask)
2616ef768faSPaolo Bonzini {
2626ef768faSPaolo Bonzini 	struct kvm_irq_mask_notifier *kimn;
2636ef768faSPaolo Bonzini 	int idx, gsi;
2646ef768faSPaolo Bonzini 
2656ef768faSPaolo Bonzini 	idx = srcu_read_lock(&kvm->irq_srcu);
2666ef768faSPaolo Bonzini 	gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
2676ef768faSPaolo Bonzini 	if (gsi != -1)
2686ef768faSPaolo Bonzini 		hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
2696ef768faSPaolo Bonzini 			if (kimn->irq == gsi)
2706ef768faSPaolo Bonzini 				kimn->func(kimn, mask);
2716ef768faSPaolo Bonzini 	srcu_read_unlock(&kvm->irq_srcu, idx);
2726ef768faSPaolo Bonzini }
2736ef768faSPaolo Bonzini 
kvm_arch_can_set_irq_routing(struct kvm * kvm)2745c0aea0eSDavid Hildenbrand bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
2755c0aea0eSDavid Hildenbrand {
2765c0aea0eSDavid Hildenbrand 	return irqchip_in_kernel(kvm);
2775c0aea0eSDavid Hildenbrand }
2785c0aea0eSDavid Hildenbrand 
kvm_set_routing_entry(struct kvm * kvm,struct kvm_kernel_irq_routing_entry * e,const struct kvm_irq_routing_entry * ue)279c63cf538SRadim Krčmář int kvm_set_routing_entry(struct kvm *kvm,
280c63cf538SRadim Krčmář 			  struct kvm_kernel_irq_routing_entry *e,
2816ef768faSPaolo Bonzini 			  const struct kvm_irq_routing_entry *ue)
2826ef768faSPaolo Bonzini {
2835c0aea0eSDavid Hildenbrand 	/* We can't check irqchip_in_kernel() here as some callers are
284d9f6e12fSIngo Molnar 	 * currently initializing the irqchip. Other callers should therefore
2855c0aea0eSDavid Hildenbrand 	 * check kvm_arch_can_set_irq_routing() before calling this function.
2865c0aea0eSDavid Hildenbrand 	 */
2876ef768faSPaolo Bonzini 	switch (ue->type) {
2886ef768faSPaolo Bonzini 	case KVM_IRQ_ROUTING_IRQCHIP:
2898bf463f3SDavid Hildenbrand 		if (irqchip_split(kvm))
29043ae312cSDavid Hildenbrand 			return -EINVAL;
291445ee82dSDavid Hildenbrand 		e->irqchip.pin = ue->u.irqchip.pin;
2926ef768faSPaolo Bonzini 		switch (ue->u.irqchip.irqchip) {
293e5dc4877SRadim Krčmář 		case KVM_IRQCHIP_PIC_SLAVE:
294445ee82dSDavid Hildenbrand 			e->irqchip.pin += PIC_NUM_PINS / 2;
295df561f66SGustavo A. R. Silva 			fallthrough;
2966ef768faSPaolo Bonzini 		case KVM_IRQCHIP_PIC_MASTER:
297445ee82dSDavid Hildenbrand 			if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
29843ae312cSDavid Hildenbrand 				return -EINVAL;
2996ef768faSPaolo Bonzini 			e->set = kvm_set_pic_irq;
3006ef768faSPaolo Bonzini 			break;
3016ef768faSPaolo Bonzini 		case KVM_IRQCHIP_IOAPIC:
302445ee82dSDavid Hildenbrand 			if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
30343ae312cSDavid Hildenbrand 				return -EINVAL;
3046ef768faSPaolo Bonzini 			e->set = kvm_set_ioapic_irq;
3056ef768faSPaolo Bonzini 			break;
3066ef768faSPaolo Bonzini 		default:
30743ae312cSDavid Hildenbrand 			return -EINVAL;
3086ef768faSPaolo Bonzini 		}
3096ef768faSPaolo Bonzini 		e->irqchip.irqchip = ue->u.irqchip.irqchip;
3106ef768faSPaolo Bonzini 		break;
3116ef768faSPaolo Bonzini 	case KVM_IRQ_ROUTING_MSI:
3126ef768faSPaolo Bonzini 		e->set = kvm_set_msi;
3136ef768faSPaolo Bonzini 		e->msi.address_lo = ue->u.msi.address_lo;
3146ef768faSPaolo Bonzini 		e->msi.address_hi = ue->u.msi.address_hi;
3156ef768faSPaolo Bonzini 		e->msi.data = ue->u.msi.data;
31637131313SRadim Krčmář 
31737131313SRadim Krčmář 		if (kvm_msi_route_invalid(kvm, e))
31843ae312cSDavid Hildenbrand 			return -EINVAL;
3196ef768faSPaolo Bonzini 		break;
320*b4f69df0SVitaly Kuznetsov #ifdef CONFIG_KVM_HYPERV
3215c919412SAndrey Smetanin 	case KVM_IRQ_ROUTING_HV_SINT:
3225c919412SAndrey Smetanin 		e->set = kvm_hv_set_sint;
3235c919412SAndrey Smetanin 		e->hv_sint.vcpu = ue->u.hv_sint.vcpu;
3245c919412SAndrey Smetanin 		e->hv_sint.sint = ue->u.hv_sint.sint;
3255c919412SAndrey Smetanin 		break;
326*b4f69df0SVitaly Kuznetsov #endif
32714243b38SDavid Woodhouse #ifdef CONFIG_KVM_XEN
32814243b38SDavid Woodhouse 	case KVM_IRQ_ROUTING_XEN_EVTCHN:
32914243b38SDavid Woodhouse 		return kvm_xen_setup_evtchn(kvm, e, ue);
33014243b38SDavid Woodhouse #endif
3316ef768faSPaolo Bonzini 	default:
33243ae312cSDavid Hildenbrand 		return -EINVAL;
3336ef768faSPaolo Bonzini 	}
3346ef768faSPaolo Bonzini 
33543ae312cSDavid Hildenbrand 	return 0;
3366ef768faSPaolo Bonzini }
3376ef768faSPaolo Bonzini 
kvm_intr_is_single_vcpu(struct kvm * kvm,struct kvm_lapic_irq * irq,struct kvm_vcpu ** dest_vcpu)3388feb4a04SFeng Wu bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
3398feb4a04SFeng Wu 			     struct kvm_vcpu **dest_vcpu)
3408feb4a04SFeng Wu {
34146808a4cSMarc Zyngier 	int r = 0;
34246808a4cSMarc Zyngier 	unsigned long i;
3438feb4a04SFeng Wu 	struct kvm_vcpu *vcpu;
3448feb4a04SFeng Wu 
3458feb4a04SFeng Wu 	if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
3468feb4a04SFeng Wu 		return true;
3478feb4a04SFeng Wu 
3488feb4a04SFeng Wu 	kvm_for_each_vcpu(i, vcpu, kvm) {
3498feb4a04SFeng Wu 		if (!kvm_apic_present(vcpu))
3508feb4a04SFeng Wu 			continue;
3518feb4a04SFeng Wu 
3528feb4a04SFeng Wu 		if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
3538feb4a04SFeng Wu 					irq->dest_id, irq->dest_mode))
3548feb4a04SFeng Wu 			continue;
3558feb4a04SFeng Wu 
3568feb4a04SFeng Wu 		if (++r == 2)
3578feb4a04SFeng Wu 			return false;
3588feb4a04SFeng Wu 
3598feb4a04SFeng Wu 		*dest_vcpu = vcpu;
3608feb4a04SFeng Wu 	}
3618feb4a04SFeng Wu 
3628feb4a04SFeng Wu 	return r == 1;
3638feb4a04SFeng Wu }
3648feb4a04SFeng Wu EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
3658feb4a04SFeng Wu 
3666ef768faSPaolo Bonzini #define IOAPIC_ROUTING_ENTRY(irq) \
3676ef768faSPaolo Bonzini 	{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,	\
3686ef768faSPaolo Bonzini 	  .u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
3696ef768faSPaolo Bonzini #define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
3706ef768faSPaolo Bonzini 
3716ef768faSPaolo Bonzini #define PIC_ROUTING_ENTRY(irq) \
3726ef768faSPaolo Bonzini 	{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP,	\
3736ef768faSPaolo Bonzini 	  .u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
3746ef768faSPaolo Bonzini #define ROUTING_ENTRY2(irq) \
3756ef768faSPaolo Bonzini 	IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
3766ef768faSPaolo Bonzini 
3776ef768faSPaolo Bonzini static const struct kvm_irq_routing_entry default_routing[] = {
3786ef768faSPaolo Bonzini 	ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
3796ef768faSPaolo Bonzini 	ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
3806ef768faSPaolo Bonzini 	ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
3816ef768faSPaolo Bonzini 	ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
3826ef768faSPaolo Bonzini 	ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
3836ef768faSPaolo Bonzini 	ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
3846ef768faSPaolo Bonzini 	ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
3856ef768faSPaolo Bonzini 	ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
3866ef768faSPaolo Bonzini 	ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
3876ef768faSPaolo Bonzini 	ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
3886ef768faSPaolo Bonzini 	ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
3896ef768faSPaolo Bonzini 	ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
3906ef768faSPaolo Bonzini };
3916ef768faSPaolo Bonzini 
kvm_setup_default_irq_routing(struct kvm * kvm)3926ef768faSPaolo Bonzini int kvm_setup_default_irq_routing(struct kvm *kvm)
3936ef768faSPaolo Bonzini {
3946ef768faSPaolo Bonzini 	return kvm_set_irq_routing(kvm, default_routing,
3956ef768faSPaolo Bonzini 				   ARRAY_SIZE(default_routing), 0);
3966ef768faSPaolo Bonzini }
39749df6397SSteve Rutherford 
kvm_arch_post_irq_routing_update(struct kvm * kvm)398abdb080fSAndrey Smetanin void kvm_arch_post_irq_routing_update(struct kvm *kvm)
399b053b2aeSSteve Rutherford {
400826da321SRadim Krčmář 	if (!irqchip_split(kvm))
401b053b2aeSSteve Rutherford 		return;
402b053b2aeSSteve Rutherford 	kvm_make_scan_ioapic_request(kvm);
403b053b2aeSSteve Rutherford }
404b053b2aeSSteve Rutherford 
kvm_scan_ioapic_routes(struct kvm_vcpu * vcpu,ulong * ioapic_handled_vectors)4056308630bSAndrey Smetanin void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
4066308630bSAndrey Smetanin 			    ulong *ioapic_handled_vectors)
407b053b2aeSSteve Rutherford {
408b053b2aeSSteve Rutherford 	struct kvm *kvm = vcpu->kvm;
409b053b2aeSSteve Rutherford 	struct kvm_kernel_irq_routing_entry *entry;
410b053b2aeSSteve Rutherford 	struct kvm_irq_routing_table *table;
411b053b2aeSSteve Rutherford 	u32 i, nr_ioapic_pins;
412b053b2aeSSteve Rutherford 	int idx;
413b053b2aeSSteve Rutherford 
414b053b2aeSSteve Rutherford 	idx = srcu_read_lock(&kvm->irq_srcu);
415b053b2aeSSteve Rutherford 	table = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
416b053b2aeSSteve Rutherford 	nr_ioapic_pins = min_t(u32, table->nr_rt_entries,
417b053b2aeSSteve Rutherford 			       kvm->arch.nr_reserved_ioapic_pins);
418b053b2aeSSteve Rutherford 	for (i = 0; i < nr_ioapic_pins; ++i) {
419b053b2aeSSteve Rutherford 		hlist_for_each_entry(entry, &table->map[i], link) {
4203159d36aSRadim Krčmář 			struct kvm_lapic_irq irq;
421b053b2aeSSteve Rutherford 
422b053b2aeSSteve Rutherford 			if (entry->type != KVM_IRQ_ROUTING_MSI)
423b053b2aeSSteve Rutherford 				continue;
424b053b2aeSSteve Rutherford 
42537131313SRadim Krčmář 			kvm_set_msi_irq(vcpu->kvm, entry, &irq);
4263159d36aSRadim Krčmář 
4277455a832SMiaohe Lin 			if (irq.trig_mode &&
428fceb3a36SAdamos Ttofari 			    (kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
429fceb3a36SAdamos Ttofari 						 irq.dest_id, irq.dest_mode) ||
430fceb3a36SAdamos Ttofari 			     kvm_apic_pending_eoi(vcpu, irq.vector)))
4313159d36aSRadim Krčmář 				__set_bit(irq.vector, ioapic_handled_vectors);
432b053b2aeSSteve Rutherford 		}
433b053b2aeSSteve Rutherford 	}
434b053b2aeSSteve Rutherford 	srcu_read_unlock(&kvm->irq_srcu, idx);
435b053b2aeSSteve Rutherford }
4365c919412SAndrey Smetanin 
kvm_arch_irq_routing_update(struct kvm * kvm)4375c919412SAndrey Smetanin void kvm_arch_irq_routing_update(struct kvm *kvm)
4385c919412SAndrey Smetanin {
439*b4f69df0SVitaly Kuznetsov #ifdef CONFIG_KVM_HYPERV
4405c919412SAndrey Smetanin 	kvm_hv_irq_routing_update(kvm);
441*b4f69df0SVitaly Kuznetsov #endif
4425c919412SAndrey Smetanin }
443