[PATCH v6 19/20] vfio: mdev: Add device request interface

From: Dave Jiang
Date: Fri May 21 2021 - 20:22:42 EST


Similar to commit 6140a8f56238 ("vfio-pci: Add device request interface").
Add request interface for mdev to allow userspace to opt in to receive
a device request notification, indicating that the device should be
released.

Signed-off-by: Dave Jiang <dave.jiang@xxxxxxxxx>
---
drivers/vfio/mdev/mdev_irqs.c | 23 +++++++++++++++++++++++
include/linux/mdev.h | 15 +++++++++++++++
2 files changed, 38 insertions(+)

diff --git a/drivers/vfio/mdev/mdev_irqs.c b/drivers/vfio/mdev/mdev_irqs.c
index ed2d11a7c729..11b1f8df020c 100644
--- a/drivers/vfio/mdev/mdev_irqs.c
+++ b/drivers/vfio/mdev/mdev_irqs.c
@@ -316,3 +316,26 @@ void mdev_irqs_free(struct mdev_device *mdev)
memset(&mdev->mdev_irq, 0, sizeof(mdev->mdev_irq));
}
EXPORT_SYMBOL_GPL(mdev_irqs_free);
+
+void vfio_mdev_request(struct vfio_device *vdev, unsigned int count)
+{
+ struct device *dev = vdev->dev;
+ struct mdev_device *mdev = to_mdev_device(dev);
+
+ if (mdev->req_trigger) {
+ dev_dbg(dev, "Requesting device from user\n");
+ eventfd_signal(mdev->req_trigger, 1);
+ }
+}
+EXPORT_SYMBOL_GPL(vfio_mdev_request);
+
+int vfio_mdev_set_req_trigger(struct mdev_device *mdev, unsigned int index,
+ unsigned int start, unsigned int count, u32 flags,
+ void *data)
+{
+ if (index != VFIO_PCI_REQ_IRQ_INDEX || start != 0 || count != 1)
+ return -EINVAL;
+
+ return vfio_set_ctx_trigger_single(&mdev->req_trigger, count, flags, data);
+}
+EXPORT_SYMBOL_GPL(vfio_mdev_set_req_trigger);
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 035c021e8068..db73d58f5e81 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -11,6 +11,8 @@
#define MDEV_H

#include <linux/irqbypass.h>
+#include <linux/eventfd.h>
+#include <linux/vfio.h>

struct mdev_type;

@@ -38,6 +40,7 @@ struct mdev_device {
struct device *iommu_device;
struct mutex creation_lock;
struct mdev_irq mdev_irq;
+ struct eventfd_ctx *req_trigger;
};

static inline struct mdev_device *irq_to_mdev(struct mdev_irq *mdev_irq)
@@ -131,6 +134,10 @@ void mdev_msix_send_signal(struct mdev_device *mdev, int vector);
int mdev_irqs_init(struct mdev_device *mdev, int num, bool *ims_map);
void mdev_irqs_free(struct mdev_device *mdev);
void mdev_irqs_set_pasid(struct mdev_device *mdev, u32 pasid);
+void vfio_mdev_request(struct vfio_device *vdev, unsigned int count);
+int vfio_mdev_set_req_trigger(struct mdev_device *mdev, unsigned int index,
+ unsigned int start, unsigned int count, u32 flags,
+ void *data);
#else
static inline int mdev_set_msix_trigger(struct mdev_device *mdev, unsigned int index,
unsigned int start, unsigned int count, u32 flags,
@@ -148,6 +155,14 @@ static inline int mdev_irqs_init(struct mdev_device *mdev, int num, bool *ims_ma

void mdev_irqs_free(struct mdev_device *mdev) {}
void mdev_irqs_set_pasid(struct mdev_device *mdev, u32 pasid) {}
+void vfio_mdev_request(struct vfio_device *vdev, unsigned int count) {}
+
+int vfio_mdev_set_req_trigger(struct mdev_device *mdev, unsigned int index,
+ unsigned int start, unsigned int count, u32 flags,
+ void *data)
+{
+ return -EOPNOTSUPP;
+}
#endif /* CONFIG_VFIO_MDEV_IMS */

#endif /* MDEV_H */