virtio-pci and virtio-mmiio handle config notifier equally but with different code (mmio adds a separate function, when pci use common function). Let's chose the more compact way (pci) and reuse it for mmio.
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsement...@yandex-team.ru> --- hw/virtio/virtio-mmio.c | 41 +++++------------------------ hw/virtio/virtio-pci.c | 34 +++--------------------- hw/virtio/virtio.c | 48 +++++++++++++++++++++++++++++++--- include/hw/virtio/virtio-pci.h | 3 --- include/hw/virtio/virtio.h | 7 +++-- 5 files changed, 58 insertions(+), 75 deletions(-) diff --git a/hw/virtio/virtio-mmio.c b/hw/virtio/virtio-mmio.c index 532c67107b..18afa1c0d0 100644 --- a/hw/virtio/virtio-mmio.c +++ b/hw/virtio/virtio-mmio.c @@ -658,18 +658,11 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); - VirtQueue *vq = virtio_get_queue(vdev, n); - EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); + int r; - if (assign) { - int r = event_notifier_init(notifier, 0); - if (r < 0) { - return r; - } - virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); - } else { - virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); - event_notifier_cleanup(notifier); + r = virtio_queue_set_guest_notifier(vdev, n, assign, with_irqfd); + if (r < 0) { + return r; } if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) { @@ -678,30 +671,7 @@ static int virtio_mmio_set_guest_notifier(DeviceState *d, int n, bool assign, return 0; } -static int virtio_mmio_set_config_guest_notifier(DeviceState *d, bool assign, - bool with_irqfd) -{ - VirtIOMMIOProxy *proxy = VIRTIO_MMIO(d); - VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); - VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); - EventNotifier *notifier = virtio_config_get_guest_notifier(vdev); - int r = 0; - if (assign) { - r = event_notifier_init(notifier, 0); - if (r < 0) { - return r; - } - virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd); - } else { - virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd); - event_notifier_cleanup(notifier); - } - if (vdc->guest_notifier_mask && vdev->use_guest_notifier_mask) { - vdc->guest_notifier_mask(vdev, VIRTIO_CONFIG_IRQ_IDX, !assign); - } - return r; -} static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs, bool assign) { @@ -723,7 +693,8 @@ static int virtio_mmio_set_guest_notifiers(DeviceState *d, int nvqs, goto assign_error; } } - r = virtio_mmio_set_config_guest_notifier(d, assign, with_irqfd); + r = virtio_mmio_set_guest_notifier(d, VIRTIO_CONFIG_IRQ_IDX, assign, + with_irqfd); if (r < 0) { goto assign_error; } diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c index 767216d795..3eca3fe2c8 100644 --- a/hw/virtio/virtio-pci.c +++ b/hw/virtio/virtio-pci.c @@ -1163,43 +1163,17 @@ static void virtio_pci_vector_poll(PCIDevice *dev, } } -void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, - int n, bool assign, - bool with_irqfd) -{ - if (n == VIRTIO_CONFIG_IRQ_IDX) { - virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd); - } else { - virtio_queue_set_guest_notifier_fd_handler(vq, assign, with_irqfd); - } -} - static int virtio_pci_set_guest_notifier(DeviceState *d, int n, bool assign, bool with_irqfd) { VirtIOPCIProxy *proxy = to_virtio_pci_proxy(d); VirtIODevice *vdev = virtio_bus_get_device(&proxy->bus); VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); - VirtQueue *vq = NULL; - EventNotifier *notifier = NULL; + int r; - if (n == VIRTIO_CONFIG_IRQ_IDX) { - notifier = virtio_config_get_guest_notifier(vdev); - } else { - vq = virtio_get_queue(vdev, n); - notifier = virtio_queue_get_guest_notifier(vq); - } - - if (assign) { - int r = event_notifier_init(notifier, 0); - if (r < 0) { - return r; - } - virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, true, with_irqfd); - } else { - virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, false, - with_irqfd); - event_notifier_cleanup(notifier); + r = virtio_queue_set_guest_notifier(vdev, n, assign, with_irqfd); + if (r < 0) { + return r; } if (!msix_enabled(&proxy->pci_dev) && diff --git a/hw/virtio/virtio.c b/hw/virtio/virtio.c index 9a81ad912e..7880c3bcd9 100644 --- a/hw/virtio/virtio.c +++ b/hw/virtio/virtio.c @@ -3750,8 +3750,10 @@ static void virtio_config_guest_notifier_read(EventNotifier *n) virtio_notify_config(vdev); } } -void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, - bool with_irqfd) + +static void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, + bool assign, + bool with_irqfd) { if (assign && !with_irqfd) { event_notifier_set_handler(&vq->guest_notifier, @@ -3766,7 +3768,7 @@ void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, } } -void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, +static void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, bool assign, bool with_irqfd) { EventNotifier *n; @@ -3783,6 +3785,46 @@ void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, } } +static void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, + VirtQueue *vq, + int n, bool assign, + bool with_irqfd) +{ + if (n == VIRTIO_CONFIG_IRQ_IDX) { + virtio_config_set_guest_notifier_fd_handler(vdev, assign, with_irqfd); + } else { + virtio_queue_set_guest_notifier_fd_handler(vq, assign, with_irqfd); + } +} + +int virtio_queue_set_guest_notifier(VirtIODevice *vdev, int n, bool assign, + bool with_irqfd) +{ + VirtQueue *vq = NULL; + EventNotifier *notifier = NULL; + + if (n == VIRTIO_CONFIG_IRQ_IDX) { + notifier = virtio_config_get_guest_notifier(vdev); + } else { + vq = virtio_get_queue(vdev, n); + notifier = virtio_queue_get_guest_notifier(vq); + } + + if (assign) { + int r = event_notifier_init(notifier, 0); + if (r < 0) { + return r; + } + virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, true, with_irqfd); + } else { + virtio_pci_set_guest_notifier_fd_handler(vdev, vq, n, false, + with_irqfd); + event_notifier_cleanup(notifier); + } + + return 0; +} + EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq) { return &vq->guest_notifier; diff --git a/include/hw/virtio/virtio-pci.h b/include/hw/virtio/virtio-pci.h index eab5394898..02c2dfb3c6 100644 --- a/include/hw/virtio/virtio-pci.h +++ b/include/hw/virtio/virtio-pci.h @@ -263,9 +263,6 @@ void virtio_pci_types_register(const VirtioPCIDeviceTypeInfo *t); * @fixed_queues. */ unsigned virtio_pci_optimal_num_queues(unsigned fixed_queues); -void virtio_pci_set_guest_notifier_fd_handler(VirtIODevice *vdev, VirtQueue *vq, - int n, bool assign, - bool with_irqfd); int virtio_pci_add_shm_cap(VirtIOPCIProxy *proxy, uint8_t bar, uint64_t offset, uint64_t length, uint8_t id); diff --git a/include/hw/virtio/virtio.h b/include/hw/virtio/virtio.h index c594764f23..8b9db08ddf 100644 --- a/include/hw/virtio/virtio.h +++ b/include/hw/virtio/virtio.h @@ -416,8 +416,6 @@ void virtio_queue_update_used_idx(VirtIODevice *vdev, int n); VirtQueue *virtio_get_queue(VirtIODevice *vdev, int n); uint16_t virtio_get_queue_index(VirtQueue *vq); EventNotifier *virtio_queue_get_guest_notifier(VirtQueue *vq); -void virtio_queue_set_guest_notifier_fd_handler(VirtQueue *vq, bool assign, - bool with_irqfd); int virtio_device_start_ioeventfd(VirtIODevice *vdev); int virtio_device_grab_ioeventfd(VirtIODevice *vdev); void virtio_device_release_ioeventfd(VirtIODevice *vdev); @@ -431,8 +429,9 @@ void virtio_queue_aio_detach_host_notifier(VirtQueue *vq, AioContext *ctx); VirtQueue *virtio_vector_first_queue(VirtIODevice *vdev, uint16_t vector); VirtQueue *virtio_vector_next_queue(VirtQueue *vq); EventNotifier *virtio_config_get_guest_notifier(VirtIODevice *vdev); -void virtio_config_set_guest_notifier_fd_handler(VirtIODevice *vdev, - bool assign, bool with_irqfd); + +int virtio_queue_set_guest_notifier(VirtIODevice *vdev, int n, bool assign, + bool with_irqfd); static inline void virtio_add_feature(uint64_t *features, unsigned int fbit) { -- 2.48.1