ed5979
From 42818e2bc6fa537fe52f7f0e6b094774a1eb00e1 Mon Sep 17 00:00:00 2001
ed5979
From: Cindy Lu <lulu@redhat.com>
ed5979
Date: Thu, 22 Dec 2022 15:04:48 +0800
ed5979
Subject: [PATCH 07/31] vhost: add support for configure interrupt
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Cindy Lu <lulu@redhat.com>
ed5979
RH-MergeRequest: 132: vhost-vdpa: support config interrupt in vhost-vdpa
ed5979
RH-Bugzilla: 1905805
ed5979
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
ed5979
RH-Acked-by: Eugenio PĂ©rez <eperezma@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [7/10] d58b439eb093f5dd3b7ca081af0ab75780e42917 (lulu6/qemu-kvm3)
ed5979
ed5979
https://bugzilla.redhat.com/show_bug.cgi?id=1905805
ed5979
Add functions to support configure interrupt.
ed5979
The configure interrupt process will start in vhost_dev_start
ed5979
and stop in vhost_dev_stop.
ed5979
ed5979
Also add the functions to support vhost_config_pending and
ed5979
vhost_config_mask.
ed5979
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
Message-Id: <20221222070451.936503-8-lulu@redhat.com>
ed5979
Acked-by: Jason Wang <jasowang@redhat.com>
ed5979
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
ed5979
(cherry picked from commit f9a09ca3ea69d108d828b7c82f1bd61b2df6fc96)
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
---
ed5979
 hw/virtio/vhost.c         | 78 ++++++++++++++++++++++++++++++++++++++-
ed5979
 include/hw/virtio/vhost.h |  4 ++
ed5979
 2 files changed, 81 insertions(+), 1 deletion(-)
ed5979
ed5979
diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
ed5979
index 7fb008bc9e..84dbb39e07 100644
ed5979
--- a/hw/virtio/vhost.c
ed5979
+++ b/hw/virtio/vhost.c
ed5979
@@ -1596,7 +1596,68 @@ void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n,
ed5979
     file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n);
ed5979
     r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file;;
ed5979
     if (r < 0) {
ed5979
-        VHOST_OPS_DEBUG(r, "vhost_set_vring_call failed");
ed5979
+        error_report("vhost_set_vring_call failed %d", -r);
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
+bool vhost_config_pending(struct vhost_dev *hdev)
ed5979
+{
ed5979
+    assert(hdev->vhost_ops);
ed5979
+    if ((hdev->started == false) ||
ed5979
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {
ed5979
+        return false;
ed5979
+    }
ed5979
+
ed5979
+    EventNotifier *notifier =
ed5979
+        &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier;
ed5979
+    return event_notifier_test_and_clear(notifier);
ed5979
+}
ed5979
+
ed5979
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask)
ed5979
+{
ed5979
+    int fd;
ed5979
+    int r;
ed5979
+    EventNotifier *notifier =
ed5979
+        &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier;
ed5979
+    EventNotifier *config_notifier = &vdev->config_notifier;
ed5979
+    assert(hdev->vhost_ops);
ed5979
+
ed5979
+    if ((hdev->started == false) ||
ed5979
+        (hdev->vhost_ops->vhost_set_config_call == NULL)) {
ed5979
+        return;
ed5979
+    }
ed5979
+    if (mask) {
ed5979
+        assert(vdev->use_guest_notifier_mask);
ed5979
+        fd = event_notifier_get_fd(notifier);
ed5979
+    } else {
ed5979
+        fd = event_notifier_get_fd(config_notifier);
ed5979
+    }
ed5979
+    r = hdev->vhost_ops->vhost_set_config_call(hdev, fd);
ed5979
+    if (r < 0) {
ed5979
+        error_report("vhost_set_config_call failed %d", -r);
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
+static void vhost_stop_config_intr(struct vhost_dev *dev)
ed5979
+{
ed5979
+    int fd = -1;
ed5979
+    assert(dev->vhost_ops);
ed5979
+    if (dev->vhost_ops->vhost_set_config_call) {
ed5979
+        dev->vhost_ops->vhost_set_config_call(dev, fd);
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
+static void vhost_start_config_intr(struct vhost_dev *dev)
ed5979
+{
ed5979
+    int r;
ed5979
+
ed5979
+    assert(dev->vhost_ops);
ed5979
+    int fd = event_notifier_get_fd(&dev->vdev->config_notifier);
ed5979
+    if (dev->vhost_ops->vhost_set_config_call) {
ed5979
+        r = dev->vhost_ops->vhost_set_config_call(dev, fd);
ed5979
+        if (!r) {
ed5979
+            event_notifier_set(&dev->vdev->config_notifier);
ed5979
+        }
ed5979
     }
ed5979
 }
ed5979
 
ed5979
@@ -1836,6 +1897,16 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
ed5979
         }
ed5979
     }
ed5979
 
ed5979
+    r = event_notifier_init(
ed5979
+        &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier, 0);
ed5979
+    if (r < 0) {
ed5979
+        return r;
ed5979
+    }
ed5979
+    event_notifier_test_and_clear(
ed5979
+        &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier);
ed5979
+    if (!vdev->use_guest_notifier_mask) {
ed5979
+        vhost_config_mask(hdev, vdev, true);
ed5979
+    }
ed5979
     if (hdev->log_enabled) {
ed5979
         uint64_t log_base;
ed5979
 
ed5979
@@ -1874,6 +1945,7 @@ int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
ed5979
             vhost_device_iotlb_miss(hdev, vq->used_phys, true);
ed5979
         }
ed5979
     }
ed5979
+    vhost_start_config_intr(hdev);
ed5979
     return 0;
ed5979
 fail_start:
ed5979
     if (vrings) {
ed5979
@@ -1903,6 +1975,9 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
ed5979
 
ed5979
     /* should only be called after backend is connected */
ed5979
     assert(hdev->vhost_ops);
ed5979
+    event_notifier_test_and_clear(
ed5979
+        &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier);
ed5979
+    event_notifier_test_and_clear(&vdev->config_notifier);
ed5979
 
ed5979
     trace_vhost_dev_stop(hdev, vdev->name, vrings);
ed5979
 
ed5979
@@ -1925,6 +2000,7 @@ void vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings)
ed5979
         }
ed5979
         memory_listener_unregister(&hdev->iommu_listener);
ed5979
     }
ed5979
+    vhost_stop_config_intr(hdev);
ed5979
     vhost_log_put(hdev, true);
ed5979
     hdev->started = false;
ed5979
     vdev->vhost_started = false;
ed5979
diff --git a/include/hw/virtio/vhost.h b/include/hw/virtio/vhost.h
ed5979
index 67a6807fac..05bedb2416 100644
ed5979
--- a/include/hw/virtio/vhost.h
ed5979
+++ b/include/hw/virtio/vhost.h
ed5979
@@ -33,6 +33,7 @@ struct vhost_virtqueue {
ed5979
     unsigned used_size;
ed5979
     EventNotifier masked_notifier;
ed5979
     EventNotifier error_notifier;
ed5979
+    EventNotifier masked_config_notifier;
ed5979
     struct vhost_dev *dev;
ed5979
 };
ed5979
 
ed5979
@@ -41,6 +42,7 @@ typedef unsigned long vhost_log_chunk_t;
ed5979
 #define VHOST_LOG_BITS (8 * sizeof(vhost_log_chunk_t))
ed5979
 #define VHOST_LOG_CHUNK (VHOST_LOG_PAGE * VHOST_LOG_BITS)
ed5979
 #define VHOST_INVALID_FEATURE_BIT   (0xff)
ed5979
+#define VHOST_QUEUE_NUM_CONFIG_INR 0
ed5979
 
ed5979
 struct vhost_log {
ed5979
     unsigned long long size;
ed5979
@@ -168,6 +170,8 @@ int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
ed5979
  * Disable direct notifications to vhost device.
ed5979
  */
ed5979
 void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev);
ed5979
+bool vhost_config_pending(struct vhost_dev *hdev);
ed5979
+void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask);
ed5979
 
ed5979
 /**
ed5979
  * vhost_dev_is_started() - report status of vhost device
ed5979
-- 
ed5979
2.31.1
ed5979