ed5979
From e04c76339580effae41617b690b58a6605e0f40b Mon Sep 17 00:00:00 2001
ed5979
From: Cindy Lu <lulu@redhat.com>
ed5979
Date: Thu, 22 Dec 2022 15:04:47 +0800
ed5979
Subject: [PATCH 06/31] virtio: 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: [6/10] 7048eb488b732578686d451684babaf17b582b05 (lulu6/qemu-kvm3)
ed5979
ed5979
https://bugzilla.redhat.com/show_bug.cgi?id=1905805
ed5979
Add the functions to support the configure interrupt in virtio
ed5979
The function virtio_config_guest_notifier_read will notify the
ed5979
guest if there is an configure interrupt.
ed5979
The function virtio_config_set_guest_notifier_fd_handler is
ed5979
to set the fd hander for the notifier
ed5979
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
Message-Id: <20221222070451.936503-7-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 7d847d0c9b93b91160f40d69a65c904d76f1edd8)
ed5979
Signed-off-by: Cindy Lu <lulu@redhat.com>
ed5979
---
ed5979
 hw/virtio/virtio.c         | 29 +++++++++++++++++++++++++++++
ed5979
 include/hw/virtio/virtio.h |  4 ++++
ed5979
 2 files changed, 33 insertions(+)
ed5979
ed5979
diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c
ed5979
index eb6347ab5d..34e9c5d141 100644
ed5979
--- a/hw/virtio/virtio.c
ed5979
+++ b/hw/virtio/virtio.c
ed5979
@@ -4012,7 +4012,14 @@ static void virtio_queue_guest_notifier_read(EventNotifier *n)
ed5979
         virtio_irq(vq);
ed5979
     }
ed5979
 }
ed5979
+static void virtio_config_guest_notifier_read(EventNotifier *n)
ed5979
+{
ed5979
+    VirtIODevice *vdev = container_of(n, VirtIODevice, config_notifier);
ed5979
 
ed5979
+    if (event_notifier_test_and_clear(n)) {
ed5979
+        virtio_notify_config(vdev);
ed5979
+    }
ed5979
+}
ed5979
 void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
ed5979
                                                 bool with_irqfd)
ed5979
 {
ed5979
@@ -4029,6 +4036,23 @@ void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign,
ed5979
     }
ed5979
 }
ed5979
 
ed5979
+void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
ed5979
+                                                 bool assign, bool with_irqfd)
ed5979
+{
ed5979
+    EventNotifier *n;
ed5979
+    n = &vdev->config_notifier;
ed5979
+    if (assign && !with_irqfd) {
ed5979
+        event_notifier_set_handler(n, virtio_config_guest_notifier_read);
ed5979
+    } else {
ed5979
+        event_notifier_set_handler(n, NULL);
ed5979
+    }
ed5979
+    if (!assign) {
ed5979
+        /* Test and clear notifier before closing it,*/
ed5979
+        /* in case poll callback didn't have time to run. */
ed5979
+        virtio_config_guest_notifier_read(n);
ed5979
+    }
ed5979
+}
ed5979
+
ed5979
 EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq)
ed5979
 {
ed5979
     return &vq->guest_notifier;
ed5979
@@ -4109,6 +4133,11 @@ EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
ed5979
     return &vq->host_notifier;
ed5979
 }
ed5979
 
ed5979
+EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev)
ed5979
+{
ed5979
+    return &vdev->config_notifier;
ed5979
+}
ed5979
+
ed5979
 void virtio_queue_set_host_notifier_enabled(VirtQueue *vq, bool enabled)
ed5979
 {
ed5979
     vq->host_notifier_enabled = enabled;
ed5979
diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h
ed5979
index 1f4a41b958..9c3a4642f2 100644
ed5979
--- a/include/hw/virtio/virtio.h
ed5979
+++ b/include/hw/virtio/virtio.h
ed5979
@@ -138,6 +138,7 @@ struct VirtIODevice
ed5979
     AddressSpace *dma_as;
ed5979
     QLIST_HEAD(, VirtQueue) *vector_queues;
ed5979
     QTAILQ_ENTRY(VirtIODevice) next;
ed5979
+    EventNotifier config_notifier;
ed5979
 };
ed5979
 
ed5979
 struct VirtioDeviceClass {
ed5979
@@ -360,6 +361,9 @@ void virtio_queue_aio_attach_host_notifier_no_poll(VirtQueue *vq, AioContext *ct
ed5979
 void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx);
ed5979
 VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector);
ed5979
 VirtQueue *virtio_vector_next_queue(VirtQueue *vq);
ed5979
+EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev);
ed5979
+void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev,
ed5979
+                                                 bool assign, bool with_irqfd);
ed5979
 
ed5979
 static inline void virtio_add_feature(uint64_t *features, unsigned int fbit)
ed5979
 {
ed5979
-- 
ed5979
2.31.1
ed5979