From patchwork Tue Jun 4 09:58:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tina Zhang X-Patchwork-Id: 1109741 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45J6xW2z67z9sBb for ; Tue, 4 Jun 2019 20:05:35 +1000 (AEST) Received: from localhost ([127.0.0.1]:49641 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6Jx-00088B-Bi for incoming@patchwork.ozlabs.org; Tue, 04 Jun 2019 06:05:33 -0400 Received: from eggs.gnu.org ([209.51.188.92]:55915) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6J6-00083R-9I for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hY6J4-0000mz-DL for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:40 -0400 Received: from mga18.intel.com ([134.134.136.126]:33969) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hY6J4-0000eZ-0b for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:38 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jun 2019 03:04:28 -0700 X-ExtLoop1: 1 Received: from gvt.bj.intel.com ([10.238.158.187]) by orsmga007.jf.intel.com with ESMTP; 04 Jun 2019 03:04:26 -0700 From: Tina Zhang To: intel-gvt-dev@lists.freedesktop.org, qemu-devel@nongnu.org Date: Tue, 4 Jun 2019 17:58:45 +0800 Message-Id: <20190604095847.10532-2-tina.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190604095847.10532-1-tina.zhang@intel.com> References: <20190604095847.10532-1-tina.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.126 Subject: [Qemu-devel] [RFC PATCH 1/3] vfio: Add a funtion to return a specific irq capabilities X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, zhenyuw@linux.intel.com, Tina Zhang , alex.williamson@redhat.com, zhiyuan.lv@intel.com, hang.yuan@intel.com, zhi.a.wang@intel.com, kraxel@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" vfio_get_dev_irq_info is introduced to return a specific irq capabilities. Signed-off-by: Tina Zhang --- hw/vfio/common.c | 78 +++++++++++++++++++++++++++++++++++ include/hw/vfio/vfio-common.h | 3 ++ 2 files changed, 81 insertions(+) diff --git a/hw/vfio/common.c b/hw/vfio/common.c index 4374cc6176..928ea54411 100644 --- a/hw/vfio/common.c +++ b/hw/vfio/common.c @@ -748,6 +748,25 @@ vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id) return NULL; } +static struct vfio_info_cap_header * +vfio_get_irq_info_cap(struct vfio_irq_info *info, uint16_t id) +{ + struct vfio_info_cap_header *hdr; + void *ptr = info; + + if (!(info->flags & VFIO_IRQ_INFO_FLAG_CAPS)) { + return NULL; + } + + for (hdr = ptr + info->cap_offset; hdr != ptr; hdr = ptr + hdr->next) { + if (hdr->id == id) { + return hdr; + } + } + + return NULL; +} + static int vfio_setup_region_sparse_mmaps(VFIORegion *region, struct vfio_region_info *info) { @@ -1539,6 +1558,33 @@ retry: return 0; } +static int vfio_get_irq_info(VFIODevice *vbasedev, int index, + struct vfio_irq_info **info) +{ + size_t argsz = sizeof(struct vfio_irq_info); + + *info = g_malloc0(argsz); + + (*info)->index = index; +retry: + (*info)->argsz = argsz; + + if (ioctl(vbasedev->fd, VFIO_DEVICE_GET_IRQ_INFO, *info)) { + g_free(*info); + *info = NULL; + return -errno; + } + + if ((*info)->argsz > argsz) { + argsz = (*info)->argsz; + *info = g_realloc(*info, argsz); + + goto retry; + } + + return 0; +} + int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, uint32_t subtype, struct vfio_region_info **info) { @@ -1574,6 +1620,38 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, return -ENODEV; } +int vfio_get_dev_irq_info(VFIODevice *vbasedev, uint32_t type, + uint32_t subtype, struct vfio_irq_info **info) +{ + int i; + + for (i = 0; i < vbasedev->num_irqs; i++) { + struct vfio_info_cap_header *hdr; + struct vfio_irq_info_cap_type *cap_type; + + if (vfio_get_irq_info(vbasedev, i, info)) { + continue; + } + + hdr = vfio_get_irq_info_cap(*info, VFIO_IRQ_INFO_CAP_TYPE); + if (!hdr) { + g_free(*info); + continue; + } + + cap_type = container_of(hdr, struct vfio_irq_info_cap_type, header); + + if (cap_type->type == type && cap_type->subtype == subtype) { + return 0; + } + + g_free(*info); + } + + *info = NULL; + return -ENODEV; +} + bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type) { struct vfio_region_info *info = NULL; diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 1155b79678..5cab6a1b81 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -195,6 +195,9 @@ int vfio_get_dev_region_info(VFIODevice *vbasedev, uint32_t type, bool vfio_has_region_cap(VFIODevice *vbasedev, int region, uint16_t cap_type); struct vfio_info_cap_header * vfio_get_region_info_cap(struct vfio_region_info *info, uint16_t id); + +int vfio_get_dev_irq_info(VFIODevice *vbasedev, uint32_t type, + uint32_t subtype, struct vfio_irq_info **info); #endif extern const MemoryListener vfio_prereg_listener; From patchwork Tue Jun 4 09:58:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tina Zhang X-Patchwork-Id: 1109740 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45J6xV4MRsz9s9y for ; Tue, 4 Jun 2019 20:05:34 +1000 (AEST) Received: from localhost ([127.0.0.1]:49639 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6Jw-000872-9L for incoming@patchwork.ozlabs.org; Tue, 04 Jun 2019 06:05:32 -0400 Received: from eggs.gnu.org ([209.51.188.92]:55916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6J6-00083S-9V for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hY6J4-0000mv-Cf for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:40 -0400 Received: from mga18.intel.com ([134.134.136.126]:33971) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hY6J4-0000hK-0f for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:38 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jun 2019 03:04:30 -0700 X-ExtLoop1: 1 Received: from gvt.bj.intel.com ([10.238.158.187]) by orsmga007.jf.intel.com with ESMTP; 04 Jun 2019 03:04:28 -0700 From: Tina Zhang To: intel-gvt-dev@lists.freedesktop.org, qemu-devel@nongnu.org Date: Tue, 4 Jun 2019 17:58:46 +0800 Message-Id: <20190604095847.10532-3-tina.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190604095847.10532-1-tina.zhang@intel.com> References: <20190604095847.10532-1-tina.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.126 Subject: [Qemu-devel] [RFC PATCH 2/3] ui/console: Introduce two new APIs X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, zhenyuw@linux.intel.com, Tina Zhang , alex.williamson@redhat.com, zhiyuan.lv@intel.com, hang.yuan@intel.com, zhi.a.wang@intel.com, kraxel@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" graphic_hw_refresh is used by display to invoke console refresh. dpy_update_interval is used by display to update gui timer interval. Signed-off-by: Tina Zhang --- include/ui/console.h | 2 ++ ui/console.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/include/ui/console.h b/include/ui/console.h index fef900db76..3b46264efb 100644 --- a/include/ui/console.h +++ b/include/ui/console.h @@ -275,6 +275,7 @@ void register_displaychangelistener(DisplayChangeListener *dcl); void update_displaychangelistener(DisplayChangeListener *dcl, uint64_t interval); void unregister_displaychangelistener(DisplayChangeListener *dcl); +void dpy_update_interval(QemuConsole *con, uint64_t interval); bool dpy_ui_info_supported(QemuConsole *con); int dpy_set_ui_info(QemuConsole *con, QemuUIInfo *info); @@ -379,6 +380,7 @@ void graphic_console_set_hwops(QemuConsole *con, void graphic_console_close(QemuConsole *con); void graphic_hw_update(QemuConsole *con); +void graphic_hw_refresh(QemuConsole *con); void graphic_hw_invalidate(QemuConsole *con); void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata); void graphic_hw_gl_block(QemuConsole *con, bool block); diff --git a/ui/console.c b/ui/console.c index 6d2282d3e9..3a02cea37d 100644 --- a/ui/console.c +++ b/ui/console.c @@ -268,6 +268,24 @@ void graphic_hw_update(QemuConsole *con) } } +void graphic_hw_refresh(QemuConsole *con) +{ + DisplayState *ds; + DisplayChangeListener *dcl; + + if (!con) { + con = active_console; + } + + ds = con->ds; + + QLIST_FOREACH(dcl, &ds->listeners, next) { + if (dcl->ops->dpy_refresh) { + dcl->ops->dpy_refresh(dcl); + } + } +} + void graphic_hw_gl_block(QemuConsole *con, bool block) { assert(con != NULL); @@ -1480,6 +1498,23 @@ void update_displaychangelistener(DisplayChangeListener *dcl, } } +void dpy_update_interval(QemuConsole *con, uint64_t interval) +{ + DisplayChangeListener *dcl; + DisplayState *ds; + + if (!con) { + return; + } + + ds = con->ds; + + QLIST_FOREACH(dcl, &ds->listeners, next) { + update_displaychangelistener(dcl, interval); + } +} + + void unregister_displaychangelistener(DisplayChangeListener *dcl) { DisplayState *ds = dcl->ds; From patchwork Tue Jun 4 09:58:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tina Zhang X-Patchwork-Id: 1109743 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=intel.com Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 45J70R6lsXz9s9y for ; Tue, 4 Jun 2019 20:08:07 +1000 (AEST) Received: from localhost ([127.0.0.1]:49702 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6MP-0002IP-SM for incoming@patchwork.ozlabs.org; Tue, 04 Jun 2019 06:08:05 -0400 Received: from eggs.gnu.org ([209.51.188.92]:55960) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY6J8-00085K-4F for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hY6J6-0000oP-8X for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:42 -0400 Received: from mga18.intel.com ([134.134.136.126]:33967) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hY6J4-0000aq-K8 for qemu-devel@nongnu.org; Tue, 04 Jun 2019 06:04:39 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga106.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 04 Jun 2019 03:04:33 -0700 X-ExtLoop1: 1 Received: from gvt.bj.intel.com ([10.238.158.187]) by orsmga007.jf.intel.com with ESMTP; 04 Jun 2019 03:04:31 -0700 From: Tina Zhang To: intel-gvt-dev@lists.freedesktop.org, qemu-devel@nongnu.org Date: Tue, 4 Jun 2019 17:58:47 +0800 Message-Id: <20190604095847.10532-4-tina.zhang@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190604095847.10532-1-tina.zhang@intel.com> References: <20190604095847.10532-1-tina.zhang@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.126 Subject: [Qemu-devel] [RFC PATCH 3/3] vfio/display: Refresh display depending on vGPU page flip events X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevin.tian@intel.com, zhenyuw@linux.intel.com, Tina Zhang , alex.williamson@redhat.com, zhiyuan.lv@intel.com, hang.yuan@intel.com, zhi.a.wang@intel.com, kraxel@redhat.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" Use vGPU plane page flip events to refresh display. Signed-off-by: Tina Zhang --- hw/vfio/display.c | 249 +++++++++++++++++++++++++++++----- include/hw/vfio/vfio-common.h | 8 ++ 2 files changed, 225 insertions(+), 32 deletions(-) diff --git a/hw/vfio/display.c b/hw/vfio/display.c index 2c2d3e5b71..6ef383b5e8 100644 --- a/hw/vfio/display.c +++ b/hw/vfio/display.c @@ -288,44 +288,54 @@ static void vfio_display_dmabuf_update(void *opaque) VFIODMABuf *primary, *cursor; bool free_bufs = false, new_cursor = false;; - primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY); - if (primary == NULL) { - if (dpy->ramfb) { - ramfb_display_update(dpy->con, dpy->ramfb); - } - return; + if (dpy->event_flags & VFIO_IRQ_EVENT_ENABLE) { + dpy_update_interval(dpy->con, GUI_REFRESH_INTERVAL_IDLE); } - if (dpy->dmabuf.primary != primary) { - dpy->dmabuf.primary = primary; - qemu_console_resize(dpy->con, - primary->buf.width, primary->buf.height); - dpy_gl_scanout_dmabuf(dpy->con, &primary->buf); - free_bufs = true; - } + if (!dpy->event_flags || + (dpy->event_flags & VFIO_IRQ_EVENT_PRIMRAY_PLANE_FLIP)) { + primary = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_PRIMARY); + if (primary == NULL) { + if (dpy->ramfb) { + ramfb_display_update(dpy->con, dpy->ramfb); + } + return; + } - cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR); - if (dpy->dmabuf.cursor != cursor) { - dpy->dmabuf.cursor = cursor; - new_cursor = true; - free_bufs = true; + if (dpy->dmabuf.primary != primary) { + dpy->dmabuf.primary = primary; + qemu_console_resize(dpy->con, + primary->buf.width, primary->buf.height); + dpy_gl_scanout_dmabuf(dpy->con, &primary->buf); + free_bufs = true; + } } - if (cursor && (new_cursor || cursor->hot_updates)) { - bool have_hot = (cursor->hot_x != 0xffffffff && - cursor->hot_y != 0xffffffff); - dpy_gl_cursor_dmabuf(dpy->con, &cursor->buf, have_hot, - cursor->hot_x, cursor->hot_y); - cursor->hot_updates = 0; - } else if (!cursor && new_cursor) { - dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0); - } + if (!dpy->event_flags || + (dpy->event_flags & VFIO_IRQ_EVENT_CURSOR_PLANE_FLIP)) { + cursor = vfio_display_get_dmabuf(vdev, DRM_PLANE_TYPE_CURSOR); + if (dpy->dmabuf.cursor != cursor) { + dpy->dmabuf.cursor = cursor; + new_cursor = true; + free_bufs = true; + } + + if (cursor && (new_cursor || cursor->hot_updates)) { + bool have_hot = (cursor->hot_x != 0xffffffff && + cursor->hot_y != 0xffffffff); + dpy_gl_cursor_dmabuf(dpy->con, &cursor->buf, have_hot, + cursor->hot_x, cursor->hot_y); + cursor->hot_updates = 0; + } else if (!cursor && new_cursor) { + dpy_gl_cursor_dmabuf(dpy->con, NULL, false, 0, 0); + } - if (cursor && cursor->pos_updates) { - dpy_gl_cursor_position(dpy->con, - cursor->pos_x, - cursor->pos_y); - cursor->pos_updates = 0; + if (cursor && cursor->pos_updates) { + dpy_gl_cursor_position(dpy->con, + cursor->pos_x, + cursor->pos_y); + cursor->pos_updates = 0; + } } dpy_gl_update(dpy->con, 0, 0, primary->buf.width, primary->buf.height); @@ -340,6 +350,7 @@ static const GraphicHwOps vfio_display_dmabuf_ops = { .ui_info = vfio_display_edid_ui_info, }; +static int vfio_register_display_notifier(VFIOPCIDevice *vdev); static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp) { if (!display_opengl) { @@ -355,6 +366,8 @@ static int vfio_display_dmabuf_init(VFIOPCIDevice *vdev, Error **errp) vdev->dpy->ramfb = ramfb_setup(DEVICE(vdev), errp); } vfio_display_edid_init(vdev); + vfio_register_display_notifier(vdev); + return 0; } @@ -495,6 +508,177 @@ static void vfio_display_region_exit(VFIODisplay *dpy) /* ---------------------------------------------------------------------- */ +static void primary_plane_update(void *opaque) +{ + VFIOPCIDevice *vdev = opaque; + VFIODisplay *dpy = vdev->dpy; + + if (!event_notifier_test_and_clear(&dpy->pri_event_notifier)) { + return; + } + + dpy->event_flags |= VFIO_IRQ_EVENT_PRIMRAY_PLANE_FLIP; + graphic_hw_refresh(dpy->con); + dpy->event_flags &= ~VFIO_IRQ_EVENT_PRIMRAY_PLANE_FLIP; +} + +static void cursor_plane_update(void *opaque) +{ + VFIOPCIDevice *vdev = opaque; + VFIODisplay *dpy = vdev->dpy; + static int times; + + if (!event_notifier_test_and_clear(&dpy->cur_event_notifier)) { + return; + } + + /* Have to skip some cursor events due to performance impact */ + if (times++ / 2) { + times = 0; + dpy->event_flags |= VFIO_IRQ_EVENT_CURSOR_PLANE_FLIP; + graphic_hw_refresh(dpy->con); + dpy->event_flags &= ~VFIO_IRQ_EVENT_CURSOR_PLANE_FLIP; + } +} + +static int register_display_notifier(VFIOPCIDevice *vdev, + uint32_t type, uint32_t subtype, + EventNotifier *notifier, + void (*handler)(void *opaque)) +{ + struct vfio_irq_info *irq; + struct vfio_irq_set *irq_set; + int argsz; + int32_t *pfd; + int ret; + + ret = vfio_get_dev_irq_info(&vdev->vbasedev, + type, + subtype, + &irq); + if (ret) { + goto out; + } + + ret = event_notifier_init(notifier, 0); + if (ret) { + error_report("vfio: Unable to init event notifier for device request"); + goto out; + } + + argsz = sizeof(*irq_set) + sizeof(*pfd); + + irq_set = g_malloc0(argsz); + irq_set->argsz = argsz; + irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | + VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = irq->index; + irq_set->start = 0; + irq_set->count = 1; + pfd = (int32_t *)&irq_set->data; + + *pfd = event_notifier_get_fd(notifier); + qemu_set_fd_handler(*pfd, handler, NULL, vdev); + + ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set); + if (ret) { + error_report("vfio: Failed to set up device request notification"); + qemu_set_fd_handler(*pfd, NULL, NULL, vdev); + event_notifier_cleanup(notifier); + } + + g_free(irq_set); + +out: + return ret; +} + +static int vfio_register_display_notifier(VFIOPCIDevice *vdev) +{ + VFIODisplay *dpy = vdev->dpy; + int ret; + + ret = register_display_notifier(vdev, VFIO_IRQ_TYPE_GFX, + VFIO_IRQ_SUBTYPE_GFX_PRI_PLANE_FLIP, + &dpy->pri_event_notifier, + primary_plane_update); + + if (ret) { + goto out; + } + + ret = register_display_notifier(vdev, VFIO_IRQ_TYPE_GFX, + VFIO_IRQ_SUBTYPE_GFX_CUR_PLANE_FLIP, + &dpy->cur_event_notifier, + cursor_plane_update); +out: + if (ret) { + dpy->event_flags = 0; + } else { + dpy->event_flags = VFIO_IRQ_EVENT_ENABLE; + } + + return ret; +} + +static void unregister_display_notifier(VFIOPCIDevice *vdev, + uint32_t type, uint32_t subtype, + EventNotifier *notifier) +{ + VFIODisplay *dpy = vdev->dpy; + int argsz; + struct vfio_irq_info *irq; + struct vfio_irq_set *irq_set; + int32_t *pfd; + int ret; + + if (!dpy->event_flags) { + return; + } + + ret = vfio_get_dev_irq_info(&vdev->vbasedev, + type, + subtype, + &irq); + if (ret) { + return ; + } + + argsz = sizeof(*irq_set) + sizeof(*pfd); + + irq_set = g_malloc0(argsz); + irq_set->argsz = argsz; + irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | + VFIO_IRQ_SET_ACTION_TRIGGER; + irq_set->index = irq->index; + irq_set->start = 0; + irq_set->count = 1; + pfd = (int32_t *)&irq_set->data; + *pfd = -1; + + if (ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) { + error_report("vfio: Failed to de-assign device request fd: %m"); + } + g_free(irq_set); + qemu_set_fd_handler(event_notifier_get_fd(notifier), + NULL, NULL, vdev); + event_notifier_cleanup(notifier); +} + +static void vfio_unregister_display_notifier(VFIOPCIDevice *vdev) +{ + VFIODisplay *dpy = vdev->dpy; + + unregister_display_notifier(vdev, VFIO_IRQ_TYPE_GFX, + VFIO_IRQ_SUBTYPE_GFX_PRI_PLANE_FLIP, + &dpy->pri_event_notifier); + + unregister_display_notifier(vdev, VFIO_IRQ_TYPE_GFX, + VFIO_IRQ_SUBTYPE_GFX_CUR_PLANE_FLIP, + &dpy->cur_event_notifier); + dpy->event_flags = false; +} + int vfio_display_probe(VFIOPCIDevice *vdev, Error **errp) { struct vfio_device_gfx_plane_info probe; @@ -531,6 +715,7 @@ void vfio_display_finalize(VFIOPCIDevice *vdev) return; } + vfio_unregister_display_notifier(vdev); graphic_console_close(vdev->dpy->con); vfio_display_dmabuf_exit(vdev->dpy); vfio_display_region_exit(vdev->dpy); diff --git a/include/hw/vfio/vfio-common.h b/include/hw/vfio/vfio-common.h index 5cab6a1b81..a3f03b20e8 100644 --- a/include/hw/vfio/vfio-common.h +++ b/include/hw/vfio/vfio-common.h @@ -27,6 +27,7 @@ #include "qemu/notify.h" #include "ui/console.h" #include "hw/display/ramfb.h" +#include "qemu/event_notifier.h" #ifdef CONFIG_LINUX #include #endif @@ -145,6 +146,10 @@ typedef struct VFIODMABuf { QTAILQ_ENTRY(VFIODMABuf) next; } VFIODMABuf; +#define VFIO_IRQ_EVENT_ENABLE (1 << 0) +#define VFIO_IRQ_EVENT_PRIMRAY_PLANE_FLIP (1 << 1) +#define VFIO_IRQ_EVENT_CURSOR_PLANE_FLIP (1 << 2) + typedef struct VFIODisplay { QemuConsole *con; RAMFBState *ramfb; @@ -152,6 +157,9 @@ typedef struct VFIODisplay { struct vfio_region_gfx_edid *edid_regs; uint8_t *edid_blob; QEMUTimer *edid_link_timer; + EventNotifier pri_event_notifier; + EventNotifier cur_event_notifier; + uint32_t event_flags; struct { VFIORegion buffer; DisplaySurface *surface;