2019-05-30 06:57:35 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-10-06 12:48:45 +07:00
|
|
|
/*
|
|
|
|
* irq_comm.c: Common API for in kernel interrupt controller
|
|
|
|
* Copyright (c) 2007, Intel Corporation.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Yaozu (Eddie) Dong <Eddie.dong@intel.com>
|
|
|
|
*
|
2010-10-06 19:23:22 +07:00
|
|
|
* Copyright 2010 Red Hat, Inc. and/or its affiliates.
|
2008-10-06 12:48:45 +07:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kvm_host.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 15:04:11 +07:00
|
|
|
#include <linux/slab.h>
|
2013-01-25 09:18:51 +07:00
|
|
|
#include <linux/export.h>
|
2017-02-04 07:27:20 +07:00
|
|
|
#include <linux/rculist.h>
|
|
|
|
|
2009-06-17 19:22:14 +07:00
|
|
|
#include <trace/events/kvm.h>
|
2009-02-10 12:57:06 +07:00
|
|
|
|
|
|
|
#include <asm/msidef.h>
|
|
|
|
|
2008-10-06 12:48:45 +07:00
|
|
|
#include "irq.h"
|
|
|
|
|
|
|
|
#include "ioapic.h"
|
|
|
|
|
2015-03-19 08:26:04 +07:00
|
|
|
#include "lapic.h"
|
|
|
|
|
2015-11-10 19:36:34 +07:00
|
|
|
#include "hyperv.h"
|
2016-01-25 15:53:33 +07:00
|
|
|
#include "x86.h"
|
2015-11-10 19:36:34 +07:00
|
|
|
|
2009-02-04 22:28:14 +07:00
|
|
|
static int kvm_set_pic_irq(struct kvm_kernel_irq_routing_entry *e,
|
2013-04-11 18:21:40 +07:00
|
|
|
struct kvm *kvm, int irq_source_id, int level,
|
|
|
|
bool line_status)
|
2008-11-19 18:58:46 +07:00
|
|
|
{
|
2017-04-07 15:50:23 +07:00
|
|
|
struct kvm_pic *pic = kvm->arch.vpic;
|
2012-07-19 17:45:20 +07:00
|
|
|
return kvm_pic_set_irq(pic, e->irqchip.pin, irq_source_id, level);
|
2008-11-19 18:58:46 +07:00
|
|
|
}
|
|
|
|
|
2009-02-04 22:28:14 +07:00
|
|
|
static int kvm_set_ioapic_irq(struct kvm_kernel_irq_routing_entry *e,
|
2013-04-11 18:21:40 +07:00
|
|
|
struct kvm *kvm, int irq_source_id, int level,
|
|
|
|
bool line_status)
|
2008-11-19 18:58:46 +07:00
|
|
|
{
|
2009-08-24 15:54:19 +07:00
|
|
|
struct kvm_ioapic *ioapic = kvm->arch.vioapic;
|
2013-04-11 18:21:40 +07:00
|
|
|
return kvm_ioapic_set_irq(ioapic, e->irqchip.pin, irq_source_id, level,
|
|
|
|
line_status);
|
2008-11-19 18:58:46 +07:00
|
|
|
}
|
|
|
|
|
2009-03-05 21:35:04 +07:00
|
|
|
int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src,
|
2016-02-29 22:04:43 +07:00
|
|
|
struct kvm_lapic_irq *irq, struct dest_map *dest_map)
|
2009-03-05 21:35:04 +07:00
|
|
|
{
|
|
|
|
int i, r = -1;
|
|
|
|
struct kvm_vcpu *vcpu, *lowest = NULL;
|
2016-01-25 15:53:33 +07:00
|
|
|
unsigned long dest_vcpu_bitmap[BITS_TO_LONGS(KVM_MAX_VCPUS)];
|
|
|
|
unsigned int dest_vcpus = 0;
|
2009-03-05 21:35:04 +07:00
|
|
|
|
2019-11-21 10:17:12 +07:00
|
|
|
if (kvm_irq_delivery_to_apic_fast(kvm, src, irq, &r, dest_map))
|
|
|
|
return r;
|
|
|
|
|
2019-12-05 02:07:18 +07:00
|
|
|
if (irq->dest_mode == APIC_DEST_PHYSICAL &&
|
|
|
|
irq->dest_id == 0xff && kvm_lowest_prio_delivery(irq)) {
|
2009-03-05 21:34:54 +07:00
|
|
|
printk(KERN_INFO "kvm: apic: phys broadcast and lowest prio\n");
|
2012-09-13 21:19:24 +07:00
|
|
|
irq->delivery_mode = APIC_DM_FIXED;
|
|
|
|
}
|
|
|
|
|
2016-01-25 15:53:33 +07:00
|
|
|
memset(dest_vcpu_bitmap, 0, sizeof(dest_vcpu_bitmap));
|
|
|
|
|
2009-06-09 19:56:29 +07:00
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
|
|
|
if (!kvm_apic_present(vcpu))
|
2009-03-05 21:34:54 +07:00
|
|
|
continue;
|
|
|
|
|
2009-03-05 21:35:04 +07:00
|
|
|
if (!kvm_apic_match_dest(vcpu, src, irq->shorthand,
|
|
|
|
irq->dest_id, irq->dest_mode))
|
2009-03-05 21:34:54 +07:00
|
|
|
continue;
|
|
|
|
|
2015-03-19 08:26:04 +07:00
|
|
|
if (!kvm_lowest_prio_delivery(irq)) {
|
2009-03-05 21:35:04 +07:00
|
|
|
if (r < 0)
|
|
|
|
r = 0;
|
2013-04-11 18:21:37 +07:00
|
|
|
r += kvm_apic_set_irq(vcpu, irq, dest_map);
|
2019-04-01 09:17:21 +07:00
|
|
|
} else if (kvm_apic_sw_enabled(vcpu->arch.apic)) {
|
2016-01-25 15:53:33 +07:00
|
|
|
if (!kvm_vector_hashing_enabled()) {
|
|
|
|
if (!lowest)
|
|
|
|
lowest = vcpu;
|
|
|
|
else if (kvm_apic_compare_prio(vcpu, lowest) < 0)
|
|
|
|
lowest = vcpu;
|
|
|
|
} else {
|
|
|
|
__set_bit(i, dest_vcpu_bitmap);
|
|
|
|
dest_vcpus++;
|
|
|
|
}
|
2009-03-05 21:34:59 +07:00
|
|
|
}
|
2009-03-05 21:34:54 +07:00
|
|
|
}
|
|
|
|
|
2016-01-25 15:53:33 +07:00
|
|
|
if (dest_vcpus != 0) {
|
|
|
|
int idx = kvm_vector_to_index(irq->vector, dest_vcpus,
|
|
|
|
dest_vcpu_bitmap, KVM_MAX_VCPUS);
|
|
|
|
|
|
|
|
lowest = kvm_get_vcpu(kvm, idx);
|
|
|
|
}
|
|
|
|
|
2009-03-05 21:35:04 +07:00
|
|
|
if (lowest)
|
2013-04-11 18:21:37 +07:00
|
|
|
r = kvm_apic_set_irq(lowest, irq, dest_map);
|
2009-03-05 21:35:04 +07:00
|
|
|
|
|
|
|
return r;
|
2009-02-11 15:03:37 +07:00
|
|
|
}
|
|
|
|
|
2016-07-13 03:09:27 +07:00
|
|
|
void kvm_set_msi_irq(struct kvm *kvm, struct kvm_kernel_irq_routing_entry *e,
|
2015-09-18 21:29:49 +07:00
|
|
|
struct kvm_lapic_irq *irq)
|
2012-10-17 23:06:02 +07:00
|
|
|
{
|
2016-07-13 03:09:27 +07:00
|
|
|
trace_kvm_msi_set_irq(e->msi.address_lo | (kvm->arch.x2apic_format ?
|
|
|
|
(u64)e->msi.address_hi << 32 : 0),
|
|
|
|
e->msi.data);
|
2012-10-17 23:06:02 +07:00
|
|
|
|
|
|
|
irq->dest_id = (e->msi.address_lo &
|
|
|
|
MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
|
2016-07-13 03:09:27 +07:00
|
|
|
if (kvm->arch.x2apic_format)
|
|
|
|
irq->dest_id |= MSI_ADDR_EXT_DEST_ID(e->msi.address_hi);
|
2012-10-17 23:06:02 +07:00
|
|
|
irq->vector = (e->msi.data &
|
|
|
|
MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
|
2019-12-05 02:07:18 +07:00
|
|
|
irq->dest_mode = kvm_lapic_irq_dest_mode(
|
|
|
|
!!((1 << MSI_ADDR_DEST_MODE_SHIFT) & e->msi.address_lo));
|
2012-10-17 23:06:02 +07:00
|
|
|
irq->trig_mode = (1 << MSI_DATA_TRIGGER_SHIFT) & e->msi.data;
|
|
|
|
irq->delivery_mode = e->msi.data & 0x700;
|
2015-03-19 08:26:03 +07:00
|
|
|
irq->msi_redir_hint = ((e->msi.address_lo
|
|
|
|
& MSI_ADDR_REDIRECTION_LOWPRI) > 0);
|
2012-10-17 23:06:02 +07:00
|
|
|
irq->level = 1;
|
2019-12-05 02:07:21 +07:00
|
|
|
irq->shorthand = APIC_DEST_NOSHORT;
|
2012-10-17 23:06:02 +07:00
|
|
|
}
|
2015-09-18 21:29:49 +07:00
|
|
|
EXPORT_SYMBOL_GPL(kvm_set_msi_irq);
|
2012-10-17 23:06:02 +07:00
|
|
|
|
2016-07-13 03:09:27 +07:00
|
|
|
static inline bool kvm_msi_route_invalid(struct kvm *kvm,
|
|
|
|
struct kvm_kernel_irq_routing_entry *e)
|
|
|
|
{
|
|
|
|
return kvm->arch.x2apic_format && (e->msi.address_hi & 0xff);
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:09:08 +07:00
|
|
|
int kvm_set_msi(struct kvm_kernel_irq_routing_entry *e,
|
2013-04-11 18:21:40 +07:00
|
|
|
struct kvm *kvm, int irq_source_id, int level, bool line_status)
|
2009-02-10 12:57:06 +07:00
|
|
|
{
|
2009-03-05 21:35:04 +07:00
|
|
|
struct kvm_lapic_irq irq;
|
2009-02-10 12:57:06 +07:00
|
|
|
|
2016-07-13 03:09:27 +07:00
|
|
|
if (kvm_msi_route_invalid(kvm, e))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2009-08-24 15:54:19 +07:00
|
|
|
if (!level)
|
|
|
|
return -1;
|
|
|
|
|
2016-07-13 03:09:27 +07:00
|
|
|
kvm_set_msi_irq(kvm, e, &irq);
|
2009-02-11 15:03:37 +07:00
|
|
|
|
2013-04-11 18:21:37 +07:00
|
|
|
return kvm_irq_delivery_to_apic(kvm, NULL, &irq, NULL);
|
2009-02-10 12:57:06 +07:00
|
|
|
}
|
|
|
|
|
2012-10-17 23:06:02 +07:00
|
|
|
|
2016-11-17 21:55:47 +07:00
|
|
|
static int kvm_hv_set_sint(struct kvm_kernel_irq_routing_entry *e,
|
|
|
|
struct kvm *kvm, int irq_source_id, int level,
|
|
|
|
bool line_status)
|
|
|
|
{
|
|
|
|
if (!level)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return kvm_hv_synic_set_irq(kvm, e->hv_sint.vcpu, e->hv_sint.sint);
|
|
|
|
}
|
|
|
|
|
2015-10-29 01:16:47 +07:00
|
|
|
int kvm_arch_set_irq_inatomic(struct kvm_kernel_irq_routing_entry *e,
|
|
|
|
struct kvm *kvm, int irq_source_id, int level,
|
|
|
|
bool line_status)
|
2012-10-17 23:06:02 +07:00
|
|
|
{
|
|
|
|
struct kvm_lapic_irq irq;
|
|
|
|
int r;
|
|
|
|
|
2016-11-17 21:55:47 +07:00
|
|
|
switch (e->type) {
|
|
|
|
case KVM_IRQ_ROUTING_HV_SINT:
|
|
|
|
return kvm_hv_set_sint(e, kvm, irq_source_id, level,
|
|
|
|
line_status);
|
2015-10-29 01:16:47 +07:00
|
|
|
|
2016-11-17 21:55:47 +07:00
|
|
|
case KVM_IRQ_ROUTING_MSI:
|
|
|
|
if (kvm_msi_route_invalid(kvm, e))
|
|
|
|
return -EINVAL;
|
2016-07-13 03:09:27 +07:00
|
|
|
|
2016-11-17 21:55:47 +07:00
|
|
|
kvm_set_msi_irq(kvm, e, &irq);
|
2012-10-17 23:06:02 +07:00
|
|
|
|
2016-11-17 21:55:47 +07:00
|
|
|
if (kvm_irq_delivery_to_apic_fast(kvm, NULL, &irq, &r, NULL))
|
|
|
|
return r;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EWOULDBLOCK;
|
2012-10-17 23:06:02 +07:00
|
|
|
}
|
|
|
|
|
2008-10-15 19:15:06 +07:00
|
|
|
int kvm_request_irq_source_id(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
unsigned long *bitmap = &kvm->arch.irq_sources_bitmap;
|
2009-06-05 01:08:24 +07:00
|
|
|
int irq_source_id;
|
|
|
|
|
|
|
|
mutex_lock(&kvm->irq_lock);
|
2009-10-18 08:47:23 +07:00
|
|
|
irq_source_id = find_first_zero_bit(bitmap, BITS_PER_LONG);
|
2008-12-01 20:57:48 +07:00
|
|
|
|
2009-10-18 08:47:23 +07:00
|
|
|
if (irq_source_id >= BITS_PER_LONG) {
|
2008-10-15 19:15:06 +07:00
|
|
|
printk(KERN_WARNING "kvm: exhaust allocatable IRQ sources!\n");
|
2009-09-25 14:33:38 +07:00
|
|
|
irq_source_id = -EFAULT;
|
|
|
|
goto unlock;
|
2008-12-01 20:57:48 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
|
2012-09-22 00:58:03 +07:00
|
|
|
ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
|
2008-12-01 20:57:48 +07:00
|
|
|
set_bit(irq_source_id, bitmap);
|
2009-09-25 14:33:38 +07:00
|
|
|
unlock:
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_unlock(&kvm->irq_lock);
|
2008-12-01 20:57:48 +07:00
|
|
|
|
2008-10-15 19:15:06 +07:00
|
|
|
return irq_source_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_free_irq_source_id(struct kvm *kvm, int irq_source_id)
|
|
|
|
{
|
2008-12-01 20:57:48 +07:00
|
|
|
ASSERT(irq_source_id != KVM_USERSPACE_IRQ_SOURCE_ID);
|
2012-09-22 00:58:03 +07:00
|
|
|
ASSERT(irq_source_id != KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID);
|
2008-12-01 20:57:48 +07:00
|
|
|
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_lock(&kvm->irq_lock);
|
2008-12-01 20:57:48 +07:00
|
|
|
if (irq_source_id < 0 ||
|
2009-10-18 08:47:23 +07:00
|
|
|
irq_source_id >= BITS_PER_LONG) {
|
2008-10-15 19:15:06 +07:00
|
|
|
printk(KERN_ERR "kvm: IRQ source ID out of range!\n");
|
2009-09-25 14:33:38 +07:00
|
|
|
goto unlock;
|
2008-10-15 19:15:06 +07:00
|
|
|
}
|
2009-10-29 22:44:17 +07:00
|
|
|
clear_bit(irq_source_id, &kvm->arch.irq_sources_bitmap);
|
2017-04-07 15:50:41 +07:00
|
|
|
if (!irqchip_kernel(kvm))
|
2009-10-29 22:44:17 +07:00
|
|
|
goto unlock;
|
|
|
|
|
2012-07-19 17:45:20 +07:00
|
|
|
kvm_ioapic_clear_all(kvm->arch.vioapic, irq_source_id);
|
2017-04-07 15:50:23 +07:00
|
|
|
kvm_pic_clear_all(kvm->arch.vpic, irq_source_id);
|
2009-09-25 14:33:38 +07:00
|
|
|
unlock:
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_unlock(&kvm->irq_lock);
|
2008-10-15 19:15:06 +07:00
|
|
|
}
|
2009-01-04 22:10:50 +07:00
|
|
|
|
|
|
|
void kvm_register_irq_mask_notifier(struct kvm *kvm, int irq,
|
|
|
|
struct kvm_irq_mask_notifier *kimn)
|
|
|
|
{
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_lock(&kvm->irq_lock);
|
2009-01-04 22:10:50 +07:00
|
|
|
kimn->irq = irq;
|
2014-11-20 19:45:31 +07:00
|
|
|
hlist_add_head_rcu(&kimn->link, &kvm->arch.mask_notifier_list);
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_unlock(&kvm->irq_lock);
|
2009-01-04 22:10:50 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
void kvm_unregister_irq_mask_notifier(struct kvm *kvm, int irq,
|
|
|
|
struct kvm_irq_mask_notifier *kimn)
|
|
|
|
{
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_lock(&kvm->irq_lock);
|
2009-08-24 15:54:24 +07:00
|
|
|
hlist_del_rcu(&kimn->link);
|
2009-06-05 01:08:24 +07:00
|
|
|
mutex_unlock(&kvm->irq_lock);
|
2014-01-16 19:44:20 +07:00
|
|
|
synchronize_srcu(&kvm->irq_srcu);
|
2009-01-04 22:10:50 +07:00
|
|
|
}
|
|
|
|
|
2010-07-11 19:32:23 +07:00
|
|
|
void kvm_fire_mask_notifiers(struct kvm *kvm, unsigned irqchip, unsigned pin,
|
|
|
|
bool mask)
|
2009-01-04 22:10:50 +07:00
|
|
|
{
|
|
|
|
struct kvm_irq_mask_notifier *kimn;
|
2014-01-16 19:44:20 +07:00
|
|
|
int idx, gsi;
|
2009-01-04 22:10:50 +07:00
|
|
|
|
2014-01-16 19:44:20 +07:00
|
|
|
idx = srcu_read_lock(&kvm->irq_srcu);
|
2014-06-30 17:51:11 +07:00
|
|
|
gsi = kvm_irq_map_chip_pin(kvm, irqchip, pin);
|
2010-07-11 19:32:23 +07:00
|
|
|
if (gsi != -1)
|
2014-11-20 19:45:31 +07:00
|
|
|
hlist_for_each_entry_rcu(kimn, &kvm->arch.mask_notifier_list, link)
|
2010-07-11 19:32:23 +07:00
|
|
|
if (kimn->irq == gsi)
|
|
|
|
kimn->func(kimn, mask);
|
2014-01-16 19:44:20 +07:00
|
|
|
srcu_read_unlock(&kvm->irq_srcu, idx);
|
2009-01-04 22:10:50 +07:00
|
|
|
}
|
|
|
|
|
2017-04-28 22:06:20 +07:00
|
|
|
bool kvm_arch_can_set_irq_routing(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
return irqchip_in_kernel(kvm);
|
|
|
|
}
|
|
|
|
|
2016-07-13 03:09:26 +07:00
|
|
|
int kvm_set_routing_entry(struct kvm *kvm,
|
|
|
|
struct kvm_kernel_irq_routing_entry *e,
|
2013-04-16 04:23:21 +07:00
|
|
|
const struct kvm_irq_routing_entry *ue)
|
2008-11-19 18:58:46 +07:00
|
|
|
{
|
2017-04-28 22:06:20 +07:00
|
|
|
/* We can't check irqchip_in_kernel() here as some callers are
|
|
|
|
* currently inititalizing the irqchip. Other callers should therefore
|
|
|
|
* check kvm_arch_can_set_irq_routing() before calling this function.
|
|
|
|
*/
|
2008-11-19 18:58:46 +07:00
|
|
|
switch (ue->type) {
|
|
|
|
case KVM_IRQ_ROUTING_IRQCHIP:
|
2017-04-07 15:50:20 +07:00
|
|
|
if (irqchip_split(kvm))
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2017-04-07 15:50:30 +07:00
|
|
|
e->irqchip.pin = ue->u.irqchip.pin;
|
2008-11-19 18:58:46 +07:00
|
|
|
switch (ue->u.irqchip.irqchip) {
|
|
|
|
case KVM_IRQCHIP_PIC_SLAVE:
|
2017-04-07 15:50:30 +07:00
|
|
|
e->irqchip.pin += PIC_NUM_PINS / 2;
|
2016-12-16 22:10:04 +07:00
|
|
|
/* fall through */
|
|
|
|
case KVM_IRQCHIP_PIC_MASTER:
|
2017-04-07 15:50:30 +07:00
|
|
|
if (ue->u.irqchip.pin >= PIC_NUM_PINS / 2)
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2009-02-04 22:28:14 +07:00
|
|
|
e->set = kvm_set_pic_irq;
|
2008-11-19 18:58:46 +07:00
|
|
|
break;
|
|
|
|
case KVM_IRQCHIP_IOAPIC:
|
2017-04-07 15:50:30 +07:00
|
|
|
if (ue->u.irqchip.pin >= KVM_IOAPIC_NUM_PINS)
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2009-05-02 04:15:43 +07:00
|
|
|
e->set = kvm_set_ioapic_irq;
|
2008-11-19 18:58:46 +07:00
|
|
|
break;
|
|
|
|
default:
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2008-11-19 18:58:46 +07:00
|
|
|
}
|
|
|
|
e->irqchip.irqchip = ue->u.irqchip.irqchip;
|
|
|
|
break;
|
2009-02-10 12:57:06 +07:00
|
|
|
case KVM_IRQ_ROUTING_MSI:
|
|
|
|
e->set = kvm_set_msi;
|
|
|
|
e->msi.address_lo = ue->u.msi.address_lo;
|
|
|
|
e->msi.address_hi = ue->u.msi.address_hi;
|
|
|
|
e->msi.data = ue->u.msi.data;
|
2016-07-13 03:09:27 +07:00
|
|
|
|
|
|
|
if (kvm_msi_route_invalid(kvm, e))
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2009-02-10 12:57:06 +07:00
|
|
|
break;
|
2015-11-10 19:36:34 +07:00
|
|
|
case KVM_IRQ_ROUTING_HV_SINT:
|
|
|
|
e->set = kvm_hv_set_sint;
|
|
|
|
e->hv_sint.vcpu = ue->u.hv_sint.vcpu;
|
|
|
|
e->hv_sint.sint = ue->u.hv_sint.sint;
|
|
|
|
break;
|
2008-11-19 18:58:46 +07:00
|
|
|
default:
|
2017-04-07 15:50:34 +07:00
|
|
|
return -EINVAL;
|
2008-11-19 18:58:46 +07:00
|
|
|
}
|
2009-08-24 15:54:20 +07:00
|
|
|
|
2017-04-07 15:50:34 +07:00
|
|
|
return 0;
|
2008-11-19 18:58:46 +07:00
|
|
|
}
|
|
|
|
|
2015-09-18 21:29:47 +07:00
|
|
|
bool kvm_intr_is_single_vcpu(struct kvm *kvm, struct kvm_lapic_irq *irq,
|
|
|
|
struct kvm_vcpu **dest_vcpu)
|
|
|
|
{
|
|
|
|
int i, r = 0;
|
|
|
|
struct kvm_vcpu *vcpu;
|
|
|
|
|
|
|
|
if (kvm_intr_is_single_vcpu_fast(kvm, irq, dest_vcpu))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
kvm_for_each_vcpu(i, vcpu, kvm) {
|
|
|
|
if (!kvm_apic_present(vcpu))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!kvm_apic_match_dest(vcpu, NULL, irq->shorthand,
|
|
|
|
irq->dest_id, irq->dest_mode))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (++r == 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*dest_vcpu = vcpu;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r == 1;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kvm_intr_is_single_vcpu);
|
|
|
|
|
2008-11-19 18:58:46 +07:00
|
|
|
#define IOAPIC_ROUTING_ENTRY(irq) \
|
|
|
|
{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
|
2014-07-25 20:27:03 +07:00
|
|
|
.u.irqchip = { .irqchip = KVM_IRQCHIP_IOAPIC, .pin = (irq) } }
|
2008-11-19 18:58:46 +07:00
|
|
|
#define ROUTING_ENTRY1(irq) IOAPIC_ROUTING_ENTRY(irq)
|
|
|
|
|
2014-11-22 00:06:19 +07:00
|
|
|
#define PIC_ROUTING_ENTRY(irq) \
|
2008-11-19 18:58:46 +07:00
|
|
|
{ .gsi = irq, .type = KVM_IRQ_ROUTING_IRQCHIP, \
|
2014-07-25 20:27:03 +07:00
|
|
|
.u.irqchip = { .irqchip = SELECT_PIC(irq), .pin = (irq) % 8 } }
|
2014-11-22 00:06:19 +07:00
|
|
|
#define ROUTING_ENTRY2(irq) \
|
2008-11-19 18:58:46 +07:00
|
|
|
IOAPIC_ROUTING_ENTRY(irq), PIC_ROUTING_ENTRY(irq)
|
|
|
|
|
|
|
|
static const struct kvm_irq_routing_entry default_routing[] = {
|
|
|
|
ROUTING_ENTRY2(0), ROUTING_ENTRY2(1),
|
|
|
|
ROUTING_ENTRY2(2), ROUTING_ENTRY2(3),
|
|
|
|
ROUTING_ENTRY2(4), ROUTING_ENTRY2(5),
|
|
|
|
ROUTING_ENTRY2(6), ROUTING_ENTRY2(7),
|
|
|
|
ROUTING_ENTRY2(8), ROUTING_ENTRY2(9),
|
|
|
|
ROUTING_ENTRY2(10), ROUTING_ENTRY2(11),
|
|
|
|
ROUTING_ENTRY2(12), ROUTING_ENTRY2(13),
|
|
|
|
ROUTING_ENTRY2(14), ROUTING_ENTRY2(15),
|
|
|
|
ROUTING_ENTRY1(16), ROUTING_ENTRY1(17),
|
|
|
|
ROUTING_ENTRY1(18), ROUTING_ENTRY1(19),
|
|
|
|
ROUTING_ENTRY1(20), ROUTING_ENTRY1(21),
|
|
|
|
ROUTING_ENTRY1(22), ROUTING_ENTRY1(23),
|
|
|
|
};
|
|
|
|
|
|
|
|
int kvm_setup_default_irq_routing(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
return kvm_set_irq_routing(kvm, default_routing,
|
|
|
|
ARRAY_SIZE(default_routing), 0);
|
|
|
|
}
|
2015-07-30 13:21:40 +07:00
|
|
|
|
|
|
|
static const struct kvm_irq_routing_entry empty_routing[] = {};
|
|
|
|
|
|
|
|
int kvm_setup_empty_irq_routing(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
return kvm_set_irq_routing(kvm, empty_routing, 0, 0);
|
|
|
|
}
|
2015-07-30 13:32:35 +07:00
|
|
|
|
2015-11-10 19:36:31 +07:00
|
|
|
void kvm_arch_post_irq_routing_update(struct kvm *kvm)
|
2015-07-30 13:32:35 +07:00
|
|
|
{
|
2016-12-16 22:10:06 +07:00
|
|
|
if (!irqchip_split(kvm))
|
2015-07-30 13:32:35 +07:00
|
|
|
return;
|
|
|
|
kvm_make_scan_ioapic_request(kvm);
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:36:32 +07:00
|
|
|
void kvm_scan_ioapic_routes(struct kvm_vcpu *vcpu,
|
|
|
|
ulong *ioapic_handled_vectors)
|
2015-07-30 13:32:35 +07:00
|
|
|
{
|
|
|
|
struct kvm *kvm = vcpu->kvm;
|
|
|
|
struct kvm_kernel_irq_routing_entry *entry;
|
|
|
|
struct kvm_irq_routing_table *table;
|
|
|
|
u32 i, nr_ioapic_pins;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = srcu_read_lock(&kvm->irq_srcu);
|
|
|
|
table = srcu_dereference(kvm->irq_routing, &kvm->irq_srcu);
|
|
|
|
nr_ioapic_pins = min_t(u32, table->nr_rt_entries,
|
|
|
|
kvm->arch.nr_reserved_ioapic_pins);
|
|
|
|
for (i = 0; i < nr_ioapic_pins; ++i) {
|
|
|
|
hlist_for_each_entry(entry, &table->map[i], link) {
|
2016-07-13 03:09:21 +07:00
|
|
|
struct kvm_lapic_irq irq;
|
2015-07-30 13:32:35 +07:00
|
|
|
|
|
|
|
if (entry->type != KVM_IRQ_ROUTING_MSI)
|
|
|
|
continue;
|
2016-07-13 03:09:21 +07:00
|
|
|
|
2016-07-13 03:09:27 +07:00
|
|
|
kvm_set_msi_irq(vcpu->kvm, entry, &irq);
|
2016-07-13 03:09:21 +07:00
|
|
|
|
2020-02-14 09:32:38 +07:00
|
|
|
if (irq.trig_mode &&
|
2019-12-05 02:07:20 +07:00
|
|
|
kvm_apic_match_dest(vcpu, NULL, APIC_DEST_NOSHORT,
|
2016-07-13 03:09:21 +07:00
|
|
|
irq.dest_id, irq.dest_mode))
|
|
|
|
__set_bit(irq.vector, ioapic_handled_vectors);
|
2015-07-30 13:32:35 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
srcu_read_unlock(&kvm->irq_srcu, idx);
|
|
|
|
}
|
2015-11-10 19:36:34 +07:00
|
|
|
|
|
|
|
void kvm_arch_irq_routing_update(struct kvm *kvm)
|
|
|
|
{
|
|
|
|
kvm_hv_irq_routing_update(kvm);
|
|
|
|
}
|