Skip to contentSkip to conversation

vfio: check if host device supports INTx

PATCH In review Frank Blaschka 2 participants 5 messages Oct 22, 2014 – Oct 24, 2014 VFIO driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio: check if host device supports INTx

From: Frank Blaschka <[email protected]>

Let the kernel announce if INTx is available. Yes, there are platforms (e.g. s390) which do not support INTx.

---
 hw/misc/vfio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
hw/misc/vfio.c +17 -1
index d66f3d2..3e9600b 100644
@@ -109,6 +109,7 @@ typedef struct VFIOVGA {
 } VFIOVGA;
 
 typedef struct VFIOINTx {
+    bool available; /* intx available */
     bool pending; /* interrupt pending */
     bool kvm_accel; /* set when QEMU bypass through KVM enabled */
     uint8_t pin; /* which pin to pull for qemu_set_irq */
@@ -554,7 +555,7 @@ static int vfio_enable_intx(VFIODevice *vdev)
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
 
-    if (!pin) {
+    if (!pin || !vdev->intx.available) {
         return 0;
     }
 
@@ -4032,6 +4033,21 @@ static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
                      vdev->host.function);
     }
 
+    irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
+    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret) {
+        /*
+         * This can fail for an old kernel or legacy PCI dev
+         * we assume intx is available
+         */
+        vdev->intx.available = true;
+        ret = 0;
+    } else if (irq_info.count == 0) {
+        vdev->intx.available = false;
+    } else {
+        vdev->intx.available = true;
+    }
+
 error:
     if (ret) {
         QLIST_REMOVE(vdev, next);
Signature · 1 line
1.8.5.5
#1 lore raw
Quoted 32 lines · Frank Blaschka

From: Frank Blaschka <[email protected]>

Let the kernel announce if INTx is available. Yes, there are platforms (e.g. s390) which do not support INTx.

---
 hw/misc/vfio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
hw/misc/vfio.c +2 -1
index d66f3d2..3e9600b 100644
@@ -109,6 +109,7 @@ typedef struct VFIOVGA {
 } VFIOVGA;
 
 typedef struct VFIOINTx {
+    bool available; /* intx available */
     bool pending; /* interrupt pending */
     bool kvm_accel; /* set when QEMU bypass through KVM enabled */
     uint8_t pin; /* which pin to pull for qemu_set_irq */
@@ -554,7 +555,7 @@ static int vfio_enable_intx(VFIODevice *vdev)
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
 
-    if (!pin) {
+    if (!pin || !vdev->intx.available) {
         return 0;
     }
 

hw/misc/vfio.c static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev) · line 4042

Quoted 10 lines
hw/misc/vfio.c +7 -0
@@ -4032,6 +4033,21 @@ static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
                      vdev->host.function);
     }
 
+    irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
+    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret) {
+        /*
+         * This can fail for an old kernel or legacy PCI dev
+         * we assume intx is available
+         */

Is this true? It's unfortunately from an ABI stability standpoint that we weren't calling this, but the ioctl should have always been there and worked. I'd rather error out here than add a fallback if this is just paranoia. Note that SR-IOV VFs will also report count=0 but their IRQ pin register will read 0. We do also have the option to virtualize the IRQ pin register for s390 devices which would make them look the same as an SR-IOV VF from an INTx perspective to the user. That may be the more consistent option so that the VFIO IRQ INFO aligns with config space. Thanks,

Alex

Quoted 11 lines
hw/misc/vfio.c +8 -0
+        vdev->intx.available = true;
+        ret = 0;
+    } else if (irq_info.count == 0) {
+        vdev->intx.available = false;
+    } else {
+        vdev->intx.available = true;
+    }
+
 error:
     if (ret) {
         QLIST_REMOVE(vdev, next);
#2 lore raw
Quoted 36 lines · Alex Williamson
Quoted 35 lines · Frank Blaschka

From: Frank Blaschka <[email protected]>

Let the kernel announce if INTx is available. Yes, there are platforms (e.g. s390) which do not support INTx.

---
 hw/misc/vfio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
hw/misc/vfio.c +2 -1
index d66f3d2..3e9600b 100644
@@ -109,6 +109,7 @@ typedef struct VFIOVGA {
 } VFIOVGA;
 
 typedef struct VFIOINTx {
+    bool available; /* intx available */
     bool pending; /* interrupt pending */
     bool kvm_accel; /* set when QEMU bypass through KVM enabled */
     uint8_t pin; /* which pin to pull for qemu_set_irq */
@@ -554,7 +555,7 @@ static int vfio_enable_intx(VFIODevice *vdev)
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
 
-    if (!pin) {
+    if (!pin || !vdev->intx.available) {
         return 0;
     }
 
@@ -4032,6 +4033,21 @@ static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
                      vdev->host.function);
     }
 
Quoted 11 lines

hw/misc/vfio.c static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev) · line 4042

Quoted 7 lines
hw/misc/vfio.c +7 -0
+    irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
+    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret) {
+        /*
+         * This can fail for an old kernel or legacy PCI dev
+         * we assume intx is available
+         */

Is this true? It's unfortunately from an ABI stability standpoint that we weren't calling this, but the ioctl should have always been there and worked. I'd rather error out here than add a fallback if this is just

ok

Quoted 3 lines

paranoia. Note that SR-IOV VFs will also report count=0 but their IRQ pin register will read 0. We do also have the option to virtualize the IRQ pin register for s390 devices which would make them look the same as

sounds interesting, can you elaborate a little bit more on this? At what point can we hook in and modify the pci device config space?

Quoted 21 lines

an SR-IOV VF from an INTx perspective to the user. That may be the more consistent option so that the VFIO IRQ INFO aligns with config space. Thanks,

Alex

Quoted 11 lines
hw/misc/vfio.c +8 -0
+        vdev->intx.available = true;
+        ret = 0;
+    } else if (irq_info.count == 0) {
+        vdev->intx.available = false;
+    } else {
+        vdev->intx.available = true;
+    }
+
 error:
     if (ret) {
         QLIST_REMOVE(vdev, next);
#3 lore raw
Quoted 49 lines · Frank Blaschka
Quoted 36 lines · Alex Williamson
Quoted 35 lines · Frank Blaschka

From: Frank Blaschka <[email protected]>

Let the kernel announce if INTx is available. Yes, there are platforms (e.g. s390) which do not support INTx.

---
 hw/misc/vfio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
hw/misc/vfio.c +2 -1
index d66f3d2..3e9600b 100644
@@ -109,6 +109,7 @@ typedef struct VFIOVGA {
 } VFIOVGA;
 
 typedef struct VFIOINTx {
+    bool available; /* intx available */
     bool pending; /* interrupt pending */
     bool kvm_accel; /* set when QEMU bypass through KVM enabled */
     uint8_t pin; /* which pin to pull for qemu_set_irq */
@@ -554,7 +555,7 @@ static int vfio_enable_intx(VFIODevice *vdev)
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
 
-    if (!pin) {
+    if (!pin || !vdev->intx.available) {
         return 0;
     }
 
@@ -4032,6 +4033,21 @@ static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
                      vdev->host.function);
     }
 
Quoted 11 lines

hw/misc/vfio.c static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev) · line 4042

Quoted 7 lines
hw/misc/vfio.c +7 -0
+    irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
+    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret) {
+        /*
+         * This can fail for an old kernel or legacy PCI dev
+         * we assume intx is available
+         */

Is this true? It's unfortunately from an ABI stability standpoint that we weren't calling this, but the ioctl should have always been there and worked. I'd rather error out here than add a fallback if this is just

Quoted 8 lines

ok

Quoted 3 lines

paranoia. Note that SR-IOV VFs will also report count=0 but their IRQ pin register will read 0. We do also have the option to virtualize the IRQ pin register for s390 devices which would make them look the same as

sounds interesting, can you elaborate a little bit more on this? At what point can we hook in and modify the pci device config space?

Untested, but I think it would be something like this:

drivers/vfio/pci/vfio_pci_config.c +7 -0
@@ -609,6 +609,10 @@ static int __init init_pci_cap_basic_perm(struct perm_bits 
 
        /* Sometimes used by sw, just virtualize */
        p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
+
+       /* Virtualize interrupt pin to allow hiding INTx */
+       p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
+
        return 0;
 }
 
@@ -1445,6 +1449,9 @@ int vfio_config_init(struct vfio_pci_device *vdev)
                *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
        }
 
+       if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX))
+               vconfig[PCI_INTERRUPT_PIN] = 0;
+
        ret = vfio_cap_init(vdev);
        if (ret)
                goto out;
#4 lore raw
Quoted 52 lines · Alex Williamson
Quoted 51 lines · Frank Blaschka
Quoted 36 lines · Alex Williamson
Quoted 35 lines · Frank Blaschka

From: Frank Blaschka <[email protected]>

Let the kernel announce if INTx is available. Yes, there are platforms (e.g. s390) which do not support INTx.

---
 hw/misc/vfio.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
hw/misc/vfio.c +2 -1
index d66f3d2..3e9600b 100644
@@ -109,6 +109,7 @@ typedef struct VFIOVGA {
 } VFIOVGA;
 
 typedef struct VFIOINTx {
+    bool available; /* intx available */
     bool pending; /* interrupt pending */
     bool kvm_accel; /* set when QEMU bypass through KVM enabled */
     uint8_t pin; /* which pin to pull for qemu_set_irq */
@@ -554,7 +555,7 @@ static int vfio_enable_intx(VFIODevice *vdev)
     struct vfio_irq_set *irq_set;
     int32_t *pfd;
 
-    if (!pin) {
+    if (!pin || !vdev->intx.available) {
         return 0;
     }
 
@@ -4032,6 +4033,21 @@ static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)
                      vdev->host.function);
     }
 
Quoted 11 lines

hw/misc/vfio.c static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev) · line 4042

Quoted 7 lines
hw/misc/vfio.c +7 -0
+    irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
+    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
+    if (ret) {
+        /*
+         * This can fail for an old kernel or legacy PCI dev
+         * we assume intx is available
+         */

Is this true? It's unfortunately from an ABI stability standpoint that we weren't calling this, but the ioctl should have always been there and worked. I'd rather error out here than add a fallback if this is just

ok

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

paranoia. Note that SR-IOV VFs will also report count=0 but their IRQ pin register will read 0. We do also have the option to virtualize the IRQ pin register for s390 devices which would make them look the same as

sounds interesting, can you elaborate a little bit more on this? At what point can we hook in and modify the pci device config space?

Untested, but I think it would be something like this:

This is great, works perfect and there is no need to do any qemu vfio changes anymore. You can forget about this patch.

Thx,
Frank

Quoted 25 lines
drivers/vfio/pci/vfio_pci_config.c +7 -0
@@ -609,6 +609,10 @@ static int __init init_pci_cap_basic_perm(struct perm_bits 
 
        /* Sometimes used by sw, just virtualize */
        p_setb(perm, PCI_INTERRUPT_LINE, (u8)ALL_VIRT, (u8)ALL_WRITE);
+
+       /* Virtualize interrupt pin to allow hiding INTx */
+       p_setb(perm, PCI_INTERRUPT_PIN, (u8)ALL_VIRT, (u8)NO_WRITE);
+
        return 0;
 }
 
@@ -1445,6 +1449,9 @@ int vfio_config_init(struct vfio_pci_device *vdev)
                *(__le16 *)&vconfig[PCI_DEVICE_ID] = cpu_to_le16(pdev->device);
        }
 
+       if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX))
+               vconfig[PCI_INTERRUPT_PIN] = 0;
+
        ret = vfio_cap_init(vdev);
        if (ret)
                goto out;