[RFC PATCH 3/3] vfio/ims: Add helper that returns IMS index

From: Reinette Chatre
Date: Thu Aug 24 2023 - 12:17:44 EST


A virtual device driver needs to facilitate translation between
the guest's MSI-X interrupt and the backing IMS interrupt with
which the physical device is programmed. For example, the
guest may need to obtain the IMS index from the virtual device driver
that it needs to program into descriptors submitted to the device
to ensure that the completion interrupts are generated correctly.

Introduce vfio_pci_ims_hwirq() to VFIO PCI IMS as a helper that
returns the IMS interrupt index backing a provided MSI-X
interrupt index belonging to a guest.

Originally-by: Dave Jiang <dave.jiang@xxxxxxxxx>
Signed-off-by: Reinette Chatre <reinette.chatre@xxxxxxxxx>
---
drivers/vfio/pci/vfio_pci_ims.c | 21 +++++++++++++++++++++
include/linux/vfio.h | 7 +++++++
2 files changed, 28 insertions(+)

diff --git a/drivers/vfio/pci/vfio_pci_ims.c b/drivers/vfio/pci/vfio_pci_ims.c
index fe5b3484ad34..27711bc7a6c2 100644
--- a/drivers/vfio/pci/vfio_pci_ims.c
+++ b/drivers/vfio/pci/vfio_pci_ims.c
@@ -37,6 +37,27 @@ struct vfio_pci_ims_ctx {
union msi_instance_cookie icookie;
};

+/*
+ * Return IMS index of IMS interrupt backing MSI-X interrupt @vector
+ */
+int vfio_pci_ims_hwirq(struct vfio_device *vdev, unsigned int vector)
+{
+ struct vfio_pci_ims *ims = &vdev->ims;
+ struct vfio_pci_ims_ctx *ctx;
+ int id;
+
+ mutex_lock(&ims->ctx_mutex);
+ ctx = xa_load(&ims->ctx, vector);
+ if (!ctx || ctx->emulated)
+ id = -EINVAL;
+ else
+ id = ctx->ims_id;
+ mutex_unlock(&ims->ctx_mutex);
+
+ return id;
+}
+EXPORT_SYMBOL_GPL(vfio_pci_ims_hwirq);
+
/*
* Send signal to the eventfd.
* @vdev: VFIO device
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index 906220002ff4..1c0e1bddd95b 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -339,6 +339,7 @@ int vfio_pci_ims_set_emulated(struct vfio_device *vdev, unsigned int start,
void vfio_pci_ims_send_signal(struct vfio_device *vdev, unsigned int vector);
int vfio_pci_ims_set_cookie(struct vfio_device *vdev, unsigned int vector,
union msi_instance_cookie *icookie);
+int vfio_pci_ims_hwirq(struct vfio_device *vdev, unsigned int vector);
#else
static inline int vfio_pci_set_ims_trigger(struct vfio_device *vdev,
unsigned int index,
@@ -374,6 +375,12 @@ static inline int vfio_pci_ims_set_cookie(struct vfio_device *vdev,
return -EOPNOTSUPP;
}

+static inline int vfio_pci_ims_hwirq(struct vfio_device *vdev,
+ unsigned int index)
+{
+ return -EOPNOTSUPP;
+}
+
#endif /* CONFIG_VFIO_PCI_IMS */

#endif /* VFIO_H */
--
2.34.1