Skip to content

Commit 07700a9

Browse files
jan-kiszkaavikivity
authored andcommitted
KVM: Allow host IRQ sharing for assigned PCI 2.3 devices
PCI 2.3 allows to generically disable IRQ sources at device level. This enables us to share legacy IRQs of such devices with other host devices when passing them to a guest. The new IRQ sharing feature introduced here is optional, user space has to request it explicitly. Moreover, user space can inform us about its view of PCI_COMMAND_INTX_DISABLE so that we can avoid unmasking the interrupt and signaling it if the guest masked it via the virtualized PCI config space. Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com> Acked-by: Alex Williamson <alex.williamson@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Avi Kivity <avi@redhat.com>
1 parent 3e51570 commit 07700a9

5 files changed

Lines changed: 230 additions & 29 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,14 @@ following flags are specified:
11691169

11701170
/* Depends on KVM_CAP_IOMMU */
11711171
#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
1172+
/* The following two depend on KVM_CAP_PCI_2_3 */
1173+
#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
1174+
#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
1175+
1176+
If KVM_DEV_ASSIGN_PCI_2_3 is set, the kernel will manage legacy INTx interrupts
1177+
via the PCI-2.3-compliant device-level mask, thus enable IRQ sharing with other
1178+
assigned devices or host devices. KVM_DEV_ASSIGN_MASK_INTX specifies the
1179+
guest's view on the INTx mask, see KVM_ASSIGN_SET_INTX_MASK for details.
11721180

11731181
The KVM_DEV_ASSIGN_ENABLE_IOMMU flag is a mandatory option to ensure
11741182
isolation of the device. Usages not specifying this flag are deprecated.
@@ -1441,6 +1449,39 @@ The "num_dirty" field is a performance hint for KVM to determine whether it
14411449
should skip processing the bitmap and just invalidate everything. It must
14421450
be set to the number of set bits in the bitmap.
14431451

1452+
4.60 KVM_ASSIGN_SET_INTX_MASK
1453+
1454+
Capability: KVM_CAP_PCI_2_3
1455+
Architectures: x86
1456+
Type: vm ioctl
1457+
Parameters: struct kvm_assigned_pci_dev (in)
1458+
Returns: 0 on success, -1 on error
1459+
1460+
Allows userspace to mask PCI INTx interrupts from the assigned device. The
1461+
kernel will not deliver INTx interrupts to the guest between setting and
1462+
clearing of KVM_ASSIGN_SET_INTX_MASK via this interface. This enables use of
1463+
and emulation of PCI 2.3 INTx disable command register behavior.
1464+
1465+
This may be used for both PCI 2.3 devices supporting INTx disable natively and
1466+
older devices lacking this support. Userspace is responsible for emulating the
1467+
read value of the INTx disable bit in the guest visible PCI command register.
1468+
When modifying the INTx disable state, userspace should precede updating the
1469+
physical device command register by calling this ioctl to inform the kernel of
1470+
the new intended INTx mask state.
1471+
1472+
Note that the kernel uses the device INTx disable bit to internally manage the
1473+
device interrupt state for PCI 2.3 devices. Reads of this register may
1474+
therefore not match the expected value. Writes should always use the guest
1475+
intended INTx disable value rather than attempting to read-copy-update the
1476+
current physical device state. Races between user and kernel updates to the
1477+
INTx disable bit are handled lazily in the kernel. It's possible the device
1478+
may generate unintended interrupts, but they will not be injected into the
1479+
guest.
1480+
1481+
See KVM_ASSIGN_DEV_IRQ for the data structure. The target device is specified
1482+
by assigned_dev_id. In the flags field, only KVM_DEV_ASSIGN_MASK_INTX is
1483+
evaluated.
1484+
14441485
4.62 KVM_CREATE_SPAPR_TCE
14451486

14461487
Capability: KVM_CAP_SPAPR_TCE

‎arch/x86/kvm/x86.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,7 @@ int kvm_dev_ioctl_check_extension(long ext)
21432143
case KVM_CAP_XSAVE:
21442144
case KVM_CAP_ASYNC_PF:
21452145
case KVM_CAP_GET_TSC_KHZ:
2146+
case KVM_CAP_PCI_2_3:
21462147
r = 1;
21472148
break;
21482149
case KVM_CAP_COALESCED_MMIO:

‎include/linux/kvm.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ struct kvm_ppc_pvinfo {
588588
#define KVM_CAP_TSC_DEADLINE_TIMER 72
589589
#define KVM_CAP_S390_UCONTROL 73
590590
#define KVM_CAP_SYNC_REGS 74
591+
#define KVM_CAP_PCI_2_3 75
591592

592593
#ifdef KVM_CAP_IRQ_ROUTING
593594

@@ -784,6 +785,9 @@ struct kvm_s390_ucas_mapping {
784785
/* Available with KVM_CAP_TSC_CONTROL */
785786
#define KVM_SET_TSC_KHZ _IO(KVMIO, 0xa2)
786787
#define KVM_GET_TSC_KHZ _IO(KVMIO, 0xa3)
788+
/* Available with KVM_CAP_PCI_2_3 */
789+
#define KVM_ASSIGN_SET_INTX_MASK _IOW(KVMIO, 0xa4, \
790+
struct kvm_assigned_pci_dev)
787791

788792
/*
789793
* ioctls for vcpu fds
@@ -857,6 +861,8 @@ struct kvm_s390_ucas_mapping {
857861
#define KVM_SET_ONE_REG _IOW(KVMIO, 0xac, struct kvm_one_reg)
858862

859863
#define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0)
864+
#define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1)
865+
#define KVM_DEV_ASSIGN_MASK_INTX (1 << 2)
860866

861867
struct kvm_assigned_pci_dev {
862868
__u32 assigned_dev_id;

‎include/linux/kvm_host.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ struct kvm_assigned_dev_kernel {
546546
unsigned int entries_nr;
547547
int host_irq;
548548
bool host_irq_disabled;
549+
bool pci_2_3;
549550
struct msix_entry *host_msix_entries;
550551
int guest_irq;
551552
struct msix_entry *guest_msix_entries;
@@ -555,6 +556,7 @@ struct kvm_assigned_dev_kernel {
555556
struct pci_dev *dev;
556557
struct kvm *kvm;
557558
spinlock_t intx_lock;
559+
struct mutex intx_mask_lock;
558560
char irq_name[32];
559561
struct pci_saved_state *pci_saved_state;
560562
};

0 commit comments

Comments
 (0)