Skip to contentSkip to conversation

Support virtio-gpu DRM native context

PATCH Superseded Dmitry Osipenko 3 participants 20 messages Oct 24, 2024 – Nov 13, 2024

Subsystems (2)

v3 of 22 Superseded Latest is v22 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support virtio-gpu DRM native context

This patchset adds DRM native context support to VirtIO-GPU on Qemu. It's based on the pending Venus v18 patches [1] that bring host blobs support to virtio-gpu-gl device.

Based-on: [email protected]

[1] https://lore.kernel.org/qemu-devel/[email protected]/

Contarary to Virgl and Venus contexts which mediate high level GFX APIs, DRM native context [2] mediates lower level kernel driver UAPI, which reflects in a less CPU overhead and less/simpler code needed to support it. DRM context consists of a host and guest parts that have to be implemented for each GPU driver. On a guest side, DRM context presents a virtual GPU as a real/native host GPU device for GL/VK applications.

[2] https://www.youtube.com/watch?v=9sFP_yddLLQ

Today there are four known DRM native context drivers existing in a wild:

- Freedreno (Qualcomm SoC GPUs), completely upstreamed
- AMDGPU, mostly merged into upstreams
- Intel (i915), merge requests are opened
- Asahi (Apple SoC GPUs), WIP status

# How to try out DRM context:

1. Like Venus and Virgl context, DRM context requires applying WIP KVM patches [3] to host kernel, otherwise mapping of GPU memory blobs will likely fail.

[3] https://lore.kernel.org/all/[email protected]/

2. Use latest libvirglrenderer from upstream git/main for Freedreno and AMDGPU native contexts. For Intel use patches [4].

[4] https://gitlab.freedesktop.org/virgl/virglrenderer/-/merge_requests/1384

3. On guest, use latest Mesa version for Freedreno. For AMDGPU use Mesa patches [5], for Intel [6].

[5] https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21658
[6] https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/29870

4. On guest, use latest Linux kernel v6.6+. Apply patch [7] if you're running Xorg in guest.

[7] https://lore.kernel.org/dri-devel/[email protected]/

Example Qemu cmdline that enables DRM context:

  qemu-system-x86_64 -device virtio-vga-gl,hostmem=4G,blob=on,drm=on \
      -machine q35,accel=kvm,memory-backend=mem1 \
      -object memory-backend-memfd,id=mem1,size=8G -m 8G

# Note about known performance problem in Qemu:

DRM contexts are mapping host blobs extensively and these mapping operations work slowly in Qemu. Exact reason is unknown. Mappings work fast on Crosvm For DRM contexts this problem is more visible than for Venus/Virgl.

Changelog:

v3: - Improved EGL presence-check code on X11 systems for the SDL2
hint that prefers EGL over GLX by using better ifdefs and checking Xlib presence at a build time to avoid build failure if lib SDL2 and system are configured with a disabled X11 support. Also added clarifying comment telling that X11 hint doesn't affect Wayland systems. Suggested by Akihiko Odaki.

- Corrected strerror(err) that used negative error where it should be positive and vice versa that was caught by Akihiko Odaki. Added clarifying comment for the case where we get positive error code from virglrenderer that differs from other virglrenderer API functions.

- Improved QSLIST usage by dropping mutex protecting the async fence list and using atomic variant of QSLIST helpers instead. Switched away from using FOREACH helper to improve readability of the code, showing that we don't precess list in unoptimal way. Like was suggested by Akihiko Odaki.

- Updated patchset base to Venus v18.

v2: - Updated SDL2-dmabuf patch by making use of error_report() and
checking presense of X11+EGL in the system before making SDL2 to prefer EGL backend over GLX, suggested by Akihiko Odaki.

- Improved SDL2's dmabuf-presence check that wasn't done properly in v1, where EGL was set up only after first console was fully inited, and thus, SDL's display .has_dmabuf callback didn't work for the first console. Now dmabuf support status is pre-checked before console is registered.

- Updated commit description of the patch that fixes SDL2's context switching logic with a more detailed explanation of the problem. Suggested by Akihiko Odaki.

- Corrected rebase typo in the async-fencing patch and switched async-fencing to use a sigle-linked list instead of the double, as was suggested by Akihiko Odaki.

- Replaced "=true" with "=on" in the DRM native context documentation example and made virtio_gpu_virgl_init() to fail with a error message if DRM context can't be initialized instead of giving a warning message, as was suggested by Akihiko Odaki.

- Added patchew's dependecy tag to the cover letter as was suggested by Akihiko Odaki.

Dmitry Osipenko (5):
ui/sdl2: Restore original context after new context creation
linux-headers: Update to Linux v6.12-rc1
virtio-gpu: Handle virgl fence creation errors
virtio-gpu: Support asynchronous fencing
virtio-gpu: Support DRM native context

Pierre-Eric Pelloux-Prayer (1):
ui/sdl2: Implement dpy dmabuf functions

 docs/system/devices/virtio-gpu.rst            |  11 +
 hw/display/virtio-gpu-gl.c                    |   5 +
 hw/display/virtio-gpu-virgl.c                 | 154 ++++++++++--
 hw/display/virtio-gpu.c                       |  15 ++
 include/hw/virtio/virtio-gpu.h                |  16 ++
 include/standard-headers/drm/drm_fourcc.h     |  43 ++++
 include/standard-headers/linux/const.h        |  17 ++
 include/standard-headers/linux/ethtool.h      | 226 ++++++++++++++++++
 include/standard-headers/linux/fuse.h         |  22 +-
 .../linux/input-event-codes.h                 |   2 +
 include/standard-headers/linux/pci_regs.h     |  41 +++-
 .../standard-headers/linux/virtio_balloon.h   |  16 +-
 include/standard-headers/linux/virtio_gpu.h   |   1 +
 include/ui/sdl2.h                             |   7 +
 linux-headers/asm-arm64/mman.h                |   9 +
 linux-headers/asm-arm64/unistd.h              |  25 +-
 linux-headers/asm-generic/unistd.h            |   6 +-
 linux-headers/asm-loongarch/kvm.h             |  24 ++
 linux-headers/asm-loongarch/unistd.h          |   4 +-
 linux-headers/asm-riscv/kvm.h                 |   7 +
 linux-headers/asm-riscv/unistd.h              |  41 +---
 linux-headers/asm-x86/kvm.h                   |   2 +
 linux-headers/asm-x86/unistd_64.h             |   1 +
 linux-headers/asm-x86/unistd_x32.h            |   1 +
 linux-headers/linux/bits.h                    |   3 +
 linux-headers/linux/const.h                   |  17 ++
 linux-headers/linux/iommufd.h                 | 143 +++++++++--
 linux-headers/linux/kvm.h                     |  23 +-
 linux-headers/linux/mman.h                    |   1 +
 linux-headers/linux/psp-sev.h                 |  28 +++
 meson.build                                   |   4 +
 ui/sdl2-gl.c                                  |  66 +++++
 ui/sdl2.c                                     |  40 ++++
 33 files changed, 914 insertions(+), 107 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH v3 1/6 ui/sdl2: Restore original context after new context creation

SDL API changes GL context to a newly created GL context, which differs from other GL providers that don't switch context. Change SDL backend to restore the original GL context. This allows Qemu's virtio-gpu to support new virglrenderer async-fencing feature for Virgl contexts, otherwise virglrenderer's vrend creates a fence-sync context on the Qemu's main-loop thread that erroneously stays in-use by the main-loop after creation, not allowing vrend's fence-sync thread switch to this new context that belongs to it.

---
 ui/sdl2-gl.c | 3 +++
 1 file changed, 3 insertions(+)
ui/sdl2-gl.c +3 -0
index e01d9ab0c7bf..b1fe96d6af22 100644
@@ -168,6 +168,9 @@ QEMUGLContext sdl2_gl_create_context(DisplayGLCtx *dgc,
                             SDL_GL_CONTEXT_PROFILE_ES);
         ctx = SDL_GL_CreateContext(scon->real_window);
     }
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+
     return (QEMUGLContext)ctx;
 }
 
Signature · 1 line
2.47.0
#2 lore raw

PATCH v3 2/6 ui/sdl2: Implement dpy dmabuf functions

In reply to Dmitry Osipenko

From: Pierre-Eric Pelloux-Prayer <[email protected]>

If EGL is used, we can rely on dmabuf to import textures without doing copies.

To get this working on X11, we use the existing SDL hint:
SDL_HINT_VIDEO_X11_FORCE_EGL (because dmabuf can't be used with GLX).

---
 include/ui/sdl2.h |  7 ++++++
 meson.build       |  4 +++
 ui/sdl2-gl.c      | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 ui/sdl2.c         | 40 ++++++++++++++++++++++++++++++
 4 files changed, 114 insertions(+)
include/ui/sdl2.h +7 -0
index dbe6e3d9739b..9daf5ecffae7 100644
@@ -45,6 +45,7 @@ struct sdl2_console {
     bool gui_keysym;
     SDL_GLContext winctx;
     QKbdState *kbd;
+    bool has_dmabuf;
 #ifdef CONFIG_OPENGL
     QemuGLShader *gls;
     egl_fb guest_fb;
@@ -96,5 +97,11 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                              void *d3d_tex2d);
 void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                            uint32_t x, uint32_t y, uint32_t w, uint32_t h);
+void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf);
+void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf);
+bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl);
+void sdl2_gl_console_init(struct sdl2_console *scon);
 
 #endif /* SDL2_H */
meson.build +4 -0
index 089cbd2bb364..1ace2bfa0ebe 100644
@@ -1531,6 +1531,9 @@ else
   sdl_image = not_found
 endif
 
+# libx11 presents together with SDL or GTK libs on systems that support X11
+xlib = dependency('x11', required: false)
+
 rbd = not_found
 if not get_option('rbd').auto() or have_block
   librados = cc.find_library('rados', required: get_option('rbd'))
@@ -2397,6 +2400,7 @@ config_host_data.set('CONFIG_RELOCATABLE', get_option('relocatable'))
 config_host_data.set('CONFIG_SAFESTACK', get_option('safe_stack'))
 config_host_data.set('CONFIG_SDL', sdl.found())
 config_host_data.set('CONFIG_SDL_IMAGE', sdl_image.found())
+config_host_data.set('CONFIG_XLIB', xlib.found())
 config_host_data.set('CONFIG_SECCOMP', seccomp.found())
 if seccomp.found()
   config_host_data.set('CONFIG_SECCOMP_SYSRAWRC', seccomp_has_sysrawrc)
ui/sdl2-gl.c +63 -0

72 lines not on this page · raw message

ui/sdl2.c +40 -0
index bd4f5a9da14a..5a1e5940c66a 100644
@@ -35,6 +35,10 @@
 #include "ui/win32-kbd-hook.h"
 #include "qemu/log.h"
 
+#ifdef CONFIG_XLIB
+#include <X11/Xlib.h>
+#endif
+
 static int sdl2_num_outputs;
 static struct sdl2_console *sdl2_console;
 
@@ -120,6 +124,9 @@ void sdl2_window_create(struct sdl2_console *scon)
         /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */
         scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
     }
+
+    qemu_egl_display = eglGetCurrentDisplay();
+
     sdl_update_caption(scon);
 }
 
@@ -820,6 +827,10 @@ static const DisplayChangeListenerOps dcl_gl_ops = {
     .dpy_gl_scanout_disable  = sdl2_gl_scanout_disable,
     .dpy_gl_scanout_texture  = sdl2_gl_scanout_texture,
     .dpy_gl_update           = sdl2_gl_scanout_flush,
+
+    .dpy_gl_scanout_dmabuf   = sdl2_gl_scanout_dmabuf,
+    .dpy_gl_release_dmabuf   = sdl2_gl_release_dmabuf,
+    .dpy_has_dmabuf          = sdl2_gl_has_dmabuf,
 };
 
 static bool
@@ -847,6 +858,33 @@ static void sdl2_display_early_init(DisplayOptions *o)
     }
 }
 
+static void sdl2_set_hint_x11_force_egl(void)
+{
+#if defined(SDL_HINT_VIDEO_X11_FORCE_EGL) && defined(CONFIG_OPENGL) && \
+    defined(CONFIG_XLIB)
+    Display *x_disp = XOpenDisplay(NULL);
+    EGLDisplay egl_display;
+
+    if (!x_disp) {
+        return;
+    }
+
+    /* Prefer EGL over GLX to get dma-buf support. */
+    egl_display = eglGetDisplay((EGLNativeDisplayType)x_disp);
+
+    if (egl_display != EGL_NO_DISPLAY) {
+        /*
+         * Setting X11_FORCE_EGL hint doesn't make SDL to prefer 11 over
+         * Wayland. I.e. SDL will use Wayland driver even if XWayland presents.
+         */
+        SDL_SetHint(SDL_HINT_VIDEO_X11_FORCE_EGL, "1");
+        eglTerminate(egl_display);
+    }
+
+    XCloseDisplay(x_disp);
+#endif
+}
+
 static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
 {
     uint8_t data = 0;
@@ -877,6 +915,7 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
     SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
 #endif
     SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1");
+    sdl2_set_hint_x11_force_egl();
     SDL_EnableScreenSaver();
     memset(&info, 0, sizeof(info));
     SDL_VERSION(&info.version);
@@ -923,6 +962,7 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
         sdl2_console[i].kbd = qkbd_state_init(con);
         if (display_opengl) {
             qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
+            sdl2_gl_console_init(&sdl2_console[i]);
         }
         register_displaychangelistener(&sdl2_console[i].dcl);
 
Signature · 1 line
2.47.0
#3 lore raw

Re: [PATCH v3 2/6] ui/sdl2: Implement dpy dmabuf functions

Quoted 46 lines · Dmitry Osipenko

From: Pierre-Eric Pelloux-Prayer <[email protected]>

If EGL is used, we can rely on dmabuf to import textures without doing copies.

To get this working on X11, we use the existing SDL hint:
SDL_HINT_VIDEO_X11_FORCE_EGL (because dmabuf can't be used with GLX).

---
  include/ui/sdl2.h |  7 ++++++
  meson.build       |  4 +++
  ui/sdl2-gl.c      | 63 +++++++++++++++++++++++++++++++++++++++++++++++
  ui/sdl2.c         | 40 ++++++++++++++++++++++++++++++
  4 files changed, 114 insertions(+)
include/ui/sdl2.h +7 -0
index dbe6e3d9739b..9daf5ecffae7 100644
@@ -45,6 +45,7 @@ struct sdl2_console {
      bool gui_keysym;
      SDL_GLContext winctx;
      QKbdState *kbd;
+    bool has_dmabuf;
  #ifdef CONFIG_OPENGL
      QemuGLShader *gls;
      egl_fb guest_fb;
@@ -96,5 +97,11 @@ void sdl2_gl_scanout_texture(DisplayChangeListener *dcl,
                               void *d3d_tex2d);
  void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
                             uint32_t x, uint32_t y, uint32_t w, uint32_t h);
+void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf);
+void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf);
+bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl);
+void sdl2_gl_console_init(struct sdl2_console *scon);
 
  #endif /* SDL2_H */
meson.build +0 -0
index 089cbd2bb364..1ace2bfa0ebe 100644

meson.build else · line 1535

Quoted 5 lines
meson.build +2 -0
@@ -1531,6 +1531,9 @@ else
    sdl_image = not_found
  endif
 
+# libx11 presents together with SDL or GTK libs on systems that support X11
+xlib = dependency('x11', required: false)

There is a line saying:
x11 = dependency('x11', method: 'pkg-config', required: gtkx11.found())

Please reuse it.

Quoted 31 lines
meson.build +2 -0
+
  rbd = not_found
  if not get_option('rbd').auto() or have_block
    librados = cc.find_library('rados', required: get_option('rbd'))
@@ -2397,6 +2400,7 @@ config_host_data.set('CONFIG_RELOCATABLE', get_option('relocatable'))
  config_host_data.set('CONFIG_SAFESTACK', get_option('safe_stack'))
  config_host_data.set('CONFIG_SDL', sdl.found())
  config_host_data.set('CONFIG_SDL_IMAGE', sdl_image.found())
+config_host_data.set('CONFIG_XLIB', xlib.found())
  config_host_data.set('CONFIG_SECCOMP', seccomp.found())
  if seccomp.found()
    config_host_data.set('CONFIG_SECCOMP_SYSRAWRC', seccomp_has_sysrawrc)
ui/sdl2-gl.c +4 -0
index b1fe96d6af22..7612af18292c 100644
@@ -26,6 +26,8 @@
   */
 
  #include "qemu/osdep.h"
+#include "qemu/main-loop.h"
+#include "qemu/error-report.h"
  #include "ui/console.h"
  #include "ui/input.h"
  #include "ui/sdl2.h"
@@ -249,3 +251,64 @@ void sdl2_gl_scanout_flush(DisplayChangeListener *dcl,
 
      SDL_GL_SwapWindow(scon->real_window);
  }
+
+void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,

ui/sdl2-gl.c void sdl2_gl_scanout_flush(DisplayChangeListener *dcl, · line 265

Quoted 10 lines
ui/sdl2-gl.c +10 -0
+                            QemuDmaBuf *dmabuf)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
+    assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+
+    egl_dmabuf_import_texture(dmabuf);
+    if (!qemu_dmabuf_get_texture(dmabuf)) {
+        error_report("%s: failed fd=%d", __func__, qemu_dmabuf_get_fd(dmabuf));

It still continues to call sdl2_gl_scanout_texture() and I doubt that's what you meant.

Quoted 96 lines
ui/sdl2-gl.c +49 -0
+    }
+
+    sdl2_gl_scanout_texture(dcl, qemu_dmabuf_get_texture(dmabuf), false,
+                            qemu_dmabuf_get_width(dmabuf),
+                            qemu_dmabuf_get_height(dmabuf),
+                            0, 0,
+                            qemu_dmabuf_get_width(dmabuf),
+                            qemu_dmabuf_get_height(dmabuf),
+                            NULL);
+
+    if (qemu_dmabuf_get_allow_fences(dmabuf)) {
+        scon->guest_fb.dmabuf = dmabuf;
+    }
+}
+
+void sdl2_gl_release_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf)
+{
+    egl_dmabuf_release_texture(dmabuf);
+}
+
+bool sdl2_gl_has_dmabuf(DisplayChangeListener *dcl)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
+    return scon->has_dmabuf;
+}
+
+void sdl2_gl_console_init(struct sdl2_console *scon)
+{
+    bool hidden = scon->hidden;
+
+    scon->hidden = true;
+    scon->surface = qemu_create_displaysurface(1, 1);
+    sdl2_window_create(scon);
+
+    /*
+     * QEMU checks whether console supports dma-buf before switching
+     * to the console.  To break this chicken-egg problem we pre-check
+     * dma-buf availability beforehand using a dummy SDL window.
+     */
+    scon->has_dmabuf = qemu_egl_has_dmabuf();
+
+    sdl2_window_destroy(scon);
+    qemu_free_displaysurface(scon->surface);
+
+    scon->surface = NULL;
+    scon->hidden = hidden;
+}
ui/sdl2.c +18 -0
index bd4f5a9da14a..5a1e5940c66a 100644
@@ -35,6 +35,10 @@
  #include "ui/win32-kbd-hook.h"
  #include "qemu/log.h"
 
+#ifdef CONFIG_XLIB
+#include <X11/Xlib.h>
+#endif
+
  static int sdl2_num_outputs;
  static struct sdl2_console *sdl2_console;
 
@@ -120,6 +124,9 @@ void sdl2_window_create(struct sdl2_console *scon)
          /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */
          scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
      }
+
+    qemu_egl_display = eglGetCurrentDisplay();
+
      sdl_update_caption(scon);
  }
 
@@ -820,6 +827,10 @@ static const DisplayChangeListenerOps dcl_gl_ops = {
      .dpy_gl_scanout_disable  = sdl2_gl_scanout_disable,
      .dpy_gl_scanout_texture  = sdl2_gl_scanout_texture,
      .dpy_gl_update           = sdl2_gl_scanout_flush,
+
+    .dpy_gl_scanout_dmabuf   = sdl2_gl_scanout_dmabuf,
+    .dpy_gl_release_dmabuf   = sdl2_gl_release_dmabuf,
+    .dpy_has_dmabuf          = sdl2_gl_has_dmabuf,
  };
 
  static bool
@@ -847,6 +858,33 @@ static void sdl2_display_early_init(DisplayOptions *o)
      }
  }
 
+static void sdl2_set_hint_x11_force_egl(void)
+{
+#if defined(SDL_HINT_VIDEO_X11_FORCE_EGL) && defined(CONFIG_OPENGL) && \
+    defined(CONFIG_XLIB)
+    Display *x_disp = XOpenDisplay(NULL);
+    EGLDisplay egl_display;
+

ui/sdl2.c static void sdl2_display_early_init(DisplayOptions *o) · line 877

Quoted 10 lines
ui/sdl2.c +10 -0
+    if (!x_disp) {
+        return;
+    }
+
+    /* Prefer EGL over GLX to get dma-buf support. */
+    egl_display = eglGetDisplay((EGLNativeDisplayType)x_disp);
+
+    if (egl_display != EGL_NO_DISPLAY) {
+        /*
+         * Setting X11_FORCE_EGL hint doesn't make SDL to prefer 11 over

s/prefer 11 over/prefer X11 over/

Personally, I'm more concerned whether setting that hint will make an invalid argument error or something.

Quoted 29 lines
ui/sdl2.c +12 -0
+         * Wayland. I.e. SDL will use Wayland driver even if XWayland presents.
+         */
+        SDL_SetHint(SDL_HINT_VIDEO_X11_FORCE_EGL, "1");
+        eglTerminate(egl_display);
+    }
+
+    XCloseDisplay(x_disp);
+#endif
+}
+
  static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
  {
      uint8_t data = 0;
@@ -877,6 +915,7 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
      SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
  #endif
      SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1");
+    sdl2_set_hint_x11_force_egl();
      SDL_EnableScreenSaver();
      memset(&info, 0, sizeof(info));
      SDL_VERSION(&info.version);
@@ -923,6 +962,7 @@ static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
          sdl2_console[i].kbd = qkbd_state_init(con);
          if (display_opengl) {
              qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
+            sdl2_gl_console_init(&sdl2_console[i]);
          }
          register_displaychangelistener(&sdl2_console[i].dcl);
 
#4 lore raw

Re: [PATCH v3 2/6] ui/sdl2: Implement dpy dmabuf functions

Accidentally missed this email a week ago. Thanks again for all the reviews!

On 10/31/24 10:32, Akihiko Odaki wrote:
...

Quoted 8 lines
Quoted 3 lines

  +# libx11 presents together with SDL or GTK libs on systems that support X11
+xlib = dependency('x11', required: false)

There is a line saying:
x11 = dependency('x11', method: 'pkg-config', required: gtkx11.found())

Please reuse it.

I've seen this option and choose not to use it because despite the brief 'X11' name, it's about X11 support specifically for GTK and not SDL.

Though, we can use this GTK/X11 for now and improve Meson dependencies later on because in practice GTK is enabled for all distro-built QEMUs. Will switch to it in v3.

...

Quoted 8 lines
Quoted 8 lines
ui/sdl2-gl.c +4 -0
+void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf)
+{
+    struct sdl2_console *scon = container_of(dcl, struct

sdl2_console, dcl);
+
+    assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);

Quoted 8 lines
Quoted 5 lines

+
+    egl_dmabuf_import_texture(dmabuf);
+    if (!qemu_dmabuf_get_texture(dmabuf)) { +        error_report("%s: failed fd=%d", __func__, qemu_dmabuf_get_fd(dmabuf));

It still continues to call sdl2_gl_scanout_texture() and I doubt that's what you meant.

Indeed, better to bail out early on a error.

...

Quoted 20 lines
Quoted 7 lines

  +static void sdl2_set_hint_x11_force_egl(void)

ui/sdl2.c +6 -0
+{
+#if defined(SDL_HINT_VIDEO_X11_FORCE_EGL) && defined(CONFIG_OPENGL) && \
+    defined(CONFIG_XLIB)
+    Display *x_disp = XOpenDisplay(NULL);
+    EGLDisplay egl_display;
+

ui/sdl2.c static void sdl2_display_early_init(DisplayOptions *o) · line 877

Quoted 10 lines
ui/sdl2.c +10 -0
+    if (!x_disp) {
+        return;
+    }
+
+    /* Prefer EGL over GLX to get dma-buf support. */
+    egl_display = eglGetDisplay((EGLNativeDisplayType)x_disp);
+
+    if (egl_display != EGL_NO_DISPLAY) {
+        /*
+         * Setting X11_FORCE_EGL hint doesn't make SDL to prefer 11 over

s/prefer 11 over/prefer X11 over/

Quoted 2 lines

Personally, I'm more concerned whether setting that hint will make an invalid argument error or something.

There are no known side effects from setting that hint for QEMU and libsdl code looks sane. AFAICT, EGL is not enabled by default in SDL only because there are older SDL applications that use GLX features and they will be broken if SDL will switch to EGL by default.

Technically, should be possible to make SDL use EGL on demand, like only when QEMU runs with enabled native-context for example. But there is no point in doing that today since there are no known problems with the EGL hint, IMO. We will be able to address problems if somebody will report a bug.

Signature · 2 lines
Best regards,
Dmitry
#5 lore raw

Re: [PATCH v3 2/6] ui/sdl2: Implement dpy dmabuf functions

Quoted 11 lines · Dmitry Osipenko

Accidentally missed this email a week ago. Thanks again for all the reviews!

On 10/31/24 10:32, Akihiko Odaki wrote:
...

Quoted 7 lines
Quoted 3 lines

  +# libx11 presents together with SDL or GTK libs on systems that
support X11
+xlib = dependency('x11', required: false)

There is a line saying:
x11 = dependency('x11', method: 'pkg-config', required: gtkx11.found())

Quoted 8 lines
Quoted 1 line

Please reuse it.

I've seen this option and choose not to use it because despite the brief 'X11' name, it's about X11 support specifically for GTK and not SDL.

Though, we can use this GTK/X11 for now and improve Meson dependencies later on because in practice GTK is enabled for all distro-built QEMUs. Will switch to it in v3.

I think you can just remove "if gtkx11.found()" to use it for SDL.

Quoted 49 lines

...

Quoted 8 lines
Quoted 8 lines
ui/sdl2-gl.c +4 -0
+void sdl2_gl_scanout_dmabuf(DisplayChangeListener *dcl,
+                            QemuDmaBuf *dmabuf)
+{
+    struct sdl2_console *scon = container_of(dcl, struct

sdl2_console, dcl);
+
+    assert(scon->opengl);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);

Quoted 8 lines
Quoted 5 lines

+
+    egl_dmabuf_import_texture(dmabuf);
+    if (!qemu_dmabuf_get_texture(dmabuf)) { +        error_report("%s: failed fd=%d", __func__, qemu_dmabuf_get_fd(dmabuf));

It still continues to call sdl2_gl_scanout_texture() and I doubt that's what you meant.

Indeed, better to bail out early on a error.

...

Quoted 20 lines
Quoted 7 lines

  +static void sdl2_set_hint_x11_force_egl(void)

ui/sdl2.c +6 -0
+{
+#if defined(SDL_HINT_VIDEO_X11_FORCE_EGL) && defined(CONFIG_OPENGL) && \
+    defined(CONFIG_XLIB)
+    Display *x_disp = XOpenDisplay(NULL);
+    EGLDisplay egl_display;
+

ui/sdl2.c static void sdl2_display_early_init(DisplayOptions *o) · line 877

Quoted 10 lines
ui/sdl2.c +10 -0
+    if (!x_disp) {
+        return;
+    }
+
+    /* Prefer EGL over GLX to get dma-buf support. */
+    egl_display = eglGetDisplay((EGLNativeDisplayType)x_disp);
+
+    if (egl_display != EGL_NO_DISPLAY) {
+        /*
+         * Setting X11_FORCE_EGL hint doesn't make SDL to prefer 11 over

s/prefer 11 over/prefer X11 over/

Quoted 2 lines

Personally, I'm more concerned whether setting that hint will make an invalid argument error or something.

There are no known side effects from setting that hint for QEMU and libsdl code looks sane. AFAICT, EGL is not enabled by default in SDL only because there are older SDL applications that use GLX features and they will be broken if SDL will switch to EGL by default.

Quoted 6 lines
Quoted 1 line

Technically, should be possible to make SDL use EGL on demand, like only

when QEMU runs with enabled native-context for example. But there is no point in doing that today since there are no known problems with the EGL hint, IMO. We will be able to address problems if somebody will report a bug.

I was thinking of scenarios where X11 is not used. A convincing scenario
of failure is that SDL emits an error for the flag and stops working.
The fact that this code just works implies it is not the case, but it's worth noting if you leave a comment anyway.

Regards,
Akihiko Odaki

#6 lore raw

Re: [PATCH v3 2/6] ui/sdl2: Implement dpy dmabuf functions

Quoted 15 lines · Akihiko Odaki
Quoted 14 lines · Dmitry Osipenko

Accidentally missed this email a week ago. Thanks again for all the reviews!

On 10/31/24 10:32, Akihiko Odaki wrote:
...

Quoted 8 lines
Quoted 3 lines

   +# libx11 presents together with SDL or GTK libs on systems that support X11
+xlib = dependency('x11', required: false)

There is a line saying:
x11 = dependency('x11', method: 'pkg-config', required: gtkx11.found())

Please reuse it.

Quoted 8 lines
Quoted 6 lines

I've seen this option and choose not to use it because despite the brief 'X11' name, it's about X11 support specifically for GTK and not SDL.

Though, we can use this GTK/X11 for now and improve Meson dependencies later on because in practice GTK is enabled for all distro-built QEMUs. Will switch to it in v3.

I think you can just remove "if gtkx11.found()" to use it for SDL.

Right, thanks.

Quoted 38 lines
Quoted 23 lines

...

Quoted 22 lines
Quoted 18 lines

   +static void sdl2_set_hint_x11_force_egl(void)

(quoted diff) +2 -0
+{
+#if defined(SDL_HINT_VIDEO_X11_FORCE_EGL) && defined(CONFIG_OPENGL)

&& \
+    defined(CONFIG_XLIB)
+    Display *x_disp = XOpenDisplay(NULL);
+    EGLDisplay egl_display;

ui/sdl2.c +11 -0
+
+    if (!x_disp) {
+        return;
+    }
+
+    /* Prefer EGL over GLX to get dma-buf support. */
+    egl_display = eglGetDisplay((EGLNativeDisplayType)x_disp);
+
+    if (egl_display != EGL_NO_DISPLAY) {
+        /*
+         * Setting X11_FORCE_EGL hint doesn't make SDL to prefer 11
Quoted 1 line

over

s/prefer 11 over/prefer X11 over/

Quoted 8 lines
Quoted 2 lines

Personally, I'm more concerned whether setting that hint will make an invalid argument error or something.

There are no known side effects from setting that hint for QEMU and libsdl code looks sane. AFAICT, EGL is not enabled by default in SDL only because there are older SDL applications that use GLX features and they will be broken if SDL will switch to EGL by default.

Quoted 1 line

Technically, should be possible to make SDL use EGL on demand, like

only

Quoted 5 lines

when QEMU runs with enabled native-context for example. But there is no point in doing that today since there are no known problems with the EGL hint, IMO. We will be able to address problems if somebody will report a bug.

Quoted 4 lines

I was thinking of scenarios where X11 is not used. A convincing scenario of failure is that SDL emits an error for the flag and stops working. The fact that this code just works implies it is not the case, but it's worth noting if you leave a comment anyway.

We check X11 presence using XOpenDisplay() and hint won't be set if Xorg is unavailable.

For the case of XWayland, there is already a comment in this patch telling that flag has no effect on Wayland. Will extend this comment, thanks.

Signature · 2 lines
Best regards,
Dmitry
#7 lore raw

PATCH v3 3/6 linux-headers: Update to Linux v6.12-rc1

In reply to Dmitry Osipenko

Update kernel headers to bring new VirtIO-GPU DRM capset.

---
 include/standard-headers/drm/drm_fourcc.h     |  43 ++++
 include/standard-headers/linux/const.h        |  17 ++
 include/standard-headers/linux/ethtool.h      | 226 ++++++++++++++++++
 include/standard-headers/linux/fuse.h         |  22 +-
 .../linux/input-event-codes.h                 |   2 +
 include/standard-headers/linux/pci_regs.h     |  41 +++-
 .../standard-headers/linux/virtio_balloon.h   |  16 +-
 include/standard-headers/linux/virtio_gpu.h   |   1 +
 linux-headers/asm-arm64/mman.h                |   9 +
 linux-headers/asm-arm64/unistd.h              |  25 +-
 linux-headers/asm-generic/unistd.h            |   6 +-
 linux-headers/asm-loongarch/kvm.h             |  24 ++
 linux-headers/asm-loongarch/unistd.h          |   4 +-
 linux-headers/asm-riscv/kvm.h                 |   7 +
 linux-headers/asm-riscv/unistd.h              |  41 +---
 linux-headers/asm-x86/kvm.h                   |   2 +
 linux-headers/asm-x86/unistd_64.h             |   1 +
 linux-headers/asm-x86/unistd_x32.h            |   1 +
 linux-headers/linux/bits.h                    |   3 +
 linux-headers/linux/const.h                   |  17 ++
 linux-headers/linux/iommufd.h                 | 143 +++++++++--
 linux-headers/linux/kvm.h                     |  23 +-
 linux-headers/linux/mman.h                    |   1 +
 linux-headers/linux/psp-sev.h                 |  28 +++
 24 files changed, 610 insertions(+), 93 deletions(-)
include/standard-headers/drm/drm_fourcc.h +43 -0
index b72917073d8d..d4a2231306e3 100644
@@ -701,6 +701,31 @@ extern "C" {
  */
 #define I915_FORMAT_MOD_4_TILED_MTL_RC_CCS_CC fourcc_mod_code(INTEL, 15)
 
+/*
+ * Intel Color Control Surfaces (CCS) for graphics ver. 20 unified compression
+ * on integrated graphics
+ *
+ * The main surface is Tile 4 and at plane index 0. For semi-planar formats
+ * like NV12, the Y and UV planes are Tile 4 and are located at plane indices
+ * 0 and 1, respectively. The CCS for all planes are stored outside of the
+ * GEM object in a reserved memory area dedicated for the storage of the
+ * CCS data for all compressible GEM objects.
+ */
+#define I915_FORMAT_MOD_4_TILED_LNL_CCS fourcc_mod_code(INTEL, 16)
+
+/*
+ * Intel Color Control Surfaces (CCS) for graphics ver. 20 unified compression
+ * on discrete graphics
+ *
+ * The main surface is Tile 4 and at plane index 0. For semi-planar formats
+ * like NV12, the Y and UV planes are Tile 4 and are located at plane indices
+ * 0 and 1, respectively. The CCS for all planes are stored outside of the
+ * GEM object in a reserved memory area dedicated for the storage of the
+ * CCS data for all compressible GEM objects. The GEM object must be stored in
+ * contiguous memory with a size aligned to 64KB
+ */
+#define I915_FORMAT_MOD_4_TILED_BMG_CCS fourcc_mod_code(INTEL, 17)
+
 /*
  * Tiled, NV12MT, grouped in 64 (pixels) x 32 (lines) -sized macroblocks
  *
@@ -1475,6 +1500,7 @@ drm_fourcc_canonicalize_nvidia_format_mod(uint64_t modifier)
 #define AMD_FMT_MOD_TILE_VER_GFX10 2
 #define AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS 3
 #define AMD_FMT_MOD_TILE_VER_GFX11 4
+#define AMD_FMT_MOD_TILE_VER_GFX12 5
 
 /*
  * 64K_S is the same for GFX9/GFX10/GFX10_RBPLUS and hence has GFX9 as canonical
@@ -1485,6 +1511,8 @@ drm_fourcc_canonicalize_nvidia_format_mod(uint64_t modifier)
 /*
  * 64K_D for non-32 bpp is the same for GFX9/GFX10/GFX10_RBPLUS and hence has
  * GFX9 as canonical version.
+ *
+ * 64K_D_2D on GFX12 is identical to 64K_D on GFX11.
  */
 #define AMD_FMT_MOD_TILE_GFX9_64K_D 10
 #define AMD_FMT_MOD_TILE_GFX9_64K_S_X 25
@@ -1492,6 +1520,21 @@ drm_fourcc_canonicalize_nvidia_format_mod(uint64_t modifier)
 #define AMD_FMT_MOD_TILE_GFX9_64K_R_X 27
 #define AMD_FMT_MOD_TILE_GFX11_256K_R_X 31
 
+/* Gfx12 swizzle modes:
+ *    0 - LINEAR
+ *    1 - 256B_2D  - 2D block dimensions
+ *    2 - 4KB_2D
+ *    3 - 64KB_2D
+ *    4 - 256KB_2D
+ *    5 - 4KB_3D   - 3D block dimensions
+ *    6 - 64KB_3D
+ *    7 - 256KB_3D
+ */
+#define AMD_FMT_MOD_TILE_GFX12_256B_2D 1
+#define AMD_FMT_MOD_TILE_GFX12_4K_2D 2
+#define AMD_FMT_MOD_TILE_GFX12_64K_2D 3
+#define AMD_FMT_MOD_TILE_GFX12_256K_2D 4
+
 #define AMD_FMT_MOD_DCC_BLOCK_64B 0
 #define AMD_FMT_MOD_DCC_BLOCK_128B 1
 #define AMD_FMT_MOD_DCC_BLOCK_256B 2
include/standard-headers/linux/const.h +17 -0
index 1eb84b5087f8..2122610de7f9 100644
@@ -28,6 +28,23 @@
 #define _BITUL(x)	(_UL(1) << (x))
 #define _BITULL(x)	(_ULL(1) << (x))
 
+#if !defined(__ASSEMBLY__)
+/*
+ * Missing __asm__ support
+ *
+ * __BIT128() would not work in the __asm__ code, as it shifts an
+ * 'unsigned __init128' data type as direct representation of
+ * 128 bit constants is not supported in the gcc compiler, as
+ * they get silently truncated.
+ *
+ * TODO: Please revisit this implementation when gcc compiler
+ * starts representing 128 bit constants directly like long
+ * and unsigned long etc. Subsequently drop the comment for
+ * GENMASK_U128() which would then start supporting __asm__ code.
+ */
+#define _BIT128(x)	((unsigned __int128)(1) << (x))
+#endif
+
 #define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (__typeof__(x))(a) - 1)
 #define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
 
include/standard-headers/linux/ethtool.h +226 -0

248 lines not on this page · raw message

include/standard-headers/linux/fuse.h +21 -1
index bac9dbc49f80..889e12ad1552 100644
@@ -217,6 +217,9 @@
  *  - add backing_id to fuse_open_out, add FOPEN_PASSTHROUGH open flag
  *  - add FUSE_NO_EXPORT_SUPPORT init flag
  *  - add FUSE_NOTIFY_RESEND, add FUSE_HAS_RESEND init flag
+ *
+ *  7.41
+ *  - add FUSE_ALLOW_IDMAP
  */
 
 #ifndef _LINUX_FUSE_H
@@ -248,7 +251,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 40
+#define FUSE_KERNEL_MINOR_VERSION 41
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
@@ -417,6 +420,7 @@ struct fuse_file_lock {
  * FUSE_NO_EXPORT_SUPPORT: explicitly disable export support
  * FUSE_HAS_RESEND: kernel supports resending pending requests, and the high bit
  *		    of the request ID indicates resend requests
+ * FUSE_ALLOW_IDMAP: allow creation of idmapped mounts
  */
 #define FUSE_ASYNC_READ		(1 << 0)
 #define FUSE_POSIX_LOCKS	(1 << 1)
@@ -462,6 +466,7 @@ struct fuse_file_lock {
 
 /* Obsolete alias for FUSE_DIRECT_IO_ALLOW_MMAP */
 #define FUSE_DIRECT_IO_RELAX	FUSE_DIRECT_IO_ALLOW_MMAP
+#define FUSE_ALLOW_IDMAP	(1ULL << 40)
 
 /**
  * CUSE INIT request/reply flags
@@ -980,6 +985,21 @@ struct fuse_fallocate_in {
  */
 #define FUSE_UNIQUE_RESEND (1ULL << 63)
 
+/**
+ * This value will be set by the kernel to
+ * (struct fuse_in_header).{uid,gid} fields in
+ * case when:
+ * - fuse daemon enabled FUSE_ALLOW_IDMAP
+ * - idmapping information is not available and uid/gid
+ *   can not be mapped in accordance with an idmapping.
+ *
+ * Note: an idmapping information always available
+ * for inode creation operations like:
+ * FUSE_MKNOD, FUSE_SYMLINK, FUSE_MKDIR, FUSE_TMPFILE,
+ * FUSE_CREATE and FUSE_RENAME2 (with RENAME_WHITEOUT).
+ */
+#define FUSE_INVALID_UIDGID ((uint32_t)(-1))
+
 struct fuse_in_header {
 	uint32_t	len;
 	uint32_t	opcode;
include/standard-headers/linux/input-event-codes.h +2 -0
index 2221b0c38348..50b2b7497ef3 100644
@@ -618,6 +618,8 @@
 #define KEY_CAMERA_ACCESS_ENABLE	0x24b	/* Enables programmatic access to camera devices. (HUTRR72) */
 #define KEY_CAMERA_ACCESS_DISABLE	0x24c	/* Disables programmatic access to camera devices. (HUTRR72) */
 #define KEY_CAMERA_ACCESS_TOGGLE	0x24d	/* Toggles the current state of the camera access control. (HUTRR72) */
+#define KEY_ACCESSIBILITY		0x24e	/* Toggles the system bound accessibility UI/command (HUTRR116) */
+#define KEY_DO_NOT_DISTURB		0x24f	/* Toggles the system-wide "Do Not Disturb" control (HUTRR94)*/
 
 #define KEY_BRIGHTNESS_MIN		0x250	/* Set Brightness to Minimum */
 #define KEY_BRIGHTNESS_MAX		0x251	/* Set Brightness to Maximum */
include/standard-headers/linux/pci_regs.h +39 -2

60 lines not on this page · raw message

include/standard-headers/linux/virtio_balloon.h +14 -2
index f343bfefd82c..3121cd2e0e19 100644
@@ -71,7 +71,13 @@ struct virtio_balloon_config {
 #define VIRTIO_BALLOON_S_CACHES   7   /* Disk caches */
 #define VIRTIO_BALLOON_S_HTLB_PGALLOC  8  /* Hugetlb page allocations */
 #define VIRTIO_BALLOON_S_HTLB_PGFAIL   9  /* Hugetlb page allocation failures */
-#define VIRTIO_BALLOON_S_NR       10
+#define VIRTIO_BALLOON_S_OOM_KILL      10 /* OOM killer invocations */
+#define VIRTIO_BALLOON_S_ALLOC_STALL   11 /* Stall count of memory allocatoin */
+#define VIRTIO_BALLOON_S_ASYNC_SCAN    12 /* Amount of memory scanned asynchronously */
+#define VIRTIO_BALLOON_S_DIRECT_SCAN   13 /* Amount of memory scanned directly */
+#define VIRTIO_BALLOON_S_ASYNC_RECLAIM 14 /* Amount of memory reclaimed asynchronously */
+#define VIRTIO_BALLOON_S_DIRECT_RECLAIM 15 /* Amount of memory reclaimed directly */
+#define VIRTIO_BALLOON_S_NR       16
 
 #define VIRTIO_BALLOON_S_NAMES_WITH_PREFIX(VIRTIO_BALLOON_S_NAMES_prefix) { \
 	VIRTIO_BALLOON_S_NAMES_prefix "swap-in", \
@@ -83,7 +89,13 @@ struct virtio_balloon_config {
 	VIRTIO_BALLOON_S_NAMES_prefix "available-memory", \
 	VIRTIO_BALLOON_S_NAMES_prefix "disk-caches", \
 	VIRTIO_BALLOON_S_NAMES_prefix "hugetlb-allocations", \
-	VIRTIO_BALLOON_S_NAMES_prefix "hugetlb-failures" \
+	VIRTIO_BALLOON_S_NAMES_prefix "hugetlb-failures", \
+	VIRTIO_BALLOON_S_NAMES_prefix "oom-kills", \
+	VIRTIO_BALLOON_S_NAMES_prefix "alloc-stalls", \
+	VIRTIO_BALLOON_S_NAMES_prefix "async-scans", \
+	VIRTIO_BALLOON_S_NAMES_prefix "direct-scans", \
+	VIRTIO_BALLOON_S_NAMES_prefix "async-reclaims", \
+	VIRTIO_BALLOON_S_NAMES_prefix "direct-reclaims" \
 }
 
 #define VIRTIO_BALLOON_S_NAMES VIRTIO_BALLOON_S_NAMES_WITH_PREFIX("")
include/standard-headers/linux/virtio_gpu.h +1 -0
index 2db643ed8fbf..6459fdb9fb5d 100644
@@ -311,6 +311,7 @@ struct virtio_gpu_cmd_submit {
 #define VIRTIO_GPU_CAPSET_VIRGL2 2
 /* 3 is reserved for gfxstream */
 #define VIRTIO_GPU_CAPSET_VENUS 4
+#define VIRTIO_GPU_CAPSET_DRM 6
 
 /* VIRTIO_GPU_CMD_GET_CAPSET_INFO */
 struct virtio_gpu_get_capset_info {
linux-headers/asm-arm64/mman.h +9 -0
index d0dbfe958789..7b500a3a7be7 100644
@@ -7,4 +7,13 @@
 #define PROT_BTI	0x10		/* BTI guarded page */
 #define PROT_MTE	0x20		/* Normal Tagged mapping */
 
+/* Override any generic PKEY permission defines */
+#define PKEY_DISABLE_EXECUTE	0x4
+#define PKEY_DISABLE_READ	0x8
+#undef PKEY_ACCESS_MASK
+#define PKEY_ACCESS_MASK       (PKEY_DISABLE_ACCESS |\
+				PKEY_DISABLE_WRITE  |\
+				PKEY_DISABLE_READ   |\
+				PKEY_DISABLE_EXECUTE)
+
 #endif /* ! _UAPI__ASM_MMAN_H */
linux-headers/asm-arm64/unistd.h +1 -24
index ce2ee8f1e361..df36f23876e8 100644
@@ -1,25 +1,2 @@
 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
-/*
- * Copyright (C) 2012 ARM Ltd.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-#define __ARCH_WANT_RENAMEAT
-#define __ARCH_WANT_NEW_STAT
-#define __ARCH_WANT_SET_GET_RLIMIT
-#define __ARCH_WANT_TIME32_SYSCALLS
-#define __ARCH_WANT_SYS_CLONE3
-#define __ARCH_WANT_MEMFD_SECRET
-
-#include <asm-generic/unistd.h>
+#include <asm/unistd_64.h>
linux-headers/asm-generic/unistd.h +1 -5
index d983c48a3b6a..5bf6148cac2b 100644
@@ -737,7 +737,7 @@ __SC_COMP(__NR_pselect6_time64, sys_pselect6, compat_sys_pselect6_time64)
 #define __NR_ppoll_time64 414
 __SC_COMP(__NR_ppoll_time64, sys_ppoll, compat_sys_ppoll_time64)
 #define __NR_io_pgetevents_time64 416
-__SYSCALL(__NR_io_pgetevents_time64, sys_io_pgetevents)
+__SC_COMP(__NR_io_pgetevents_time64, sys_io_pgetevents, compat_sys_io_pgetevents_time64)
 #define __NR_recvmmsg_time64 417
 __SC_COMP(__NR_recvmmsg_time64, sys_recvmmsg, compat_sys_recvmmsg_time64)
 #define __NR_mq_timedsend_time64 418
@@ -776,12 +776,8 @@ __SYSCALL(__NR_fsmount, sys_fsmount)
 __SYSCALL(__NR_fspick, sys_fspick)
 #define __NR_pidfd_open 434
 __SYSCALL(__NR_pidfd_open, sys_pidfd_open)
-
-#ifdef __ARCH_WANT_SYS_CLONE3
 #define __NR_clone3 435
 __SYSCALL(__NR_clone3, sys_clone3)
-#endif
-
 #define __NR_close_range 436
 __SYSCALL(__NR_close_range, sys_close_range)
 #define __NR_openat2 437
linux-headers/asm-loongarch/kvm.h +24 -0
index f9abef382317..70d89070bfeb 100644
@@ -64,6 +64,7 @@ struct kvm_fpu {
 #define KVM_REG_LOONGARCH_KVM		(KVM_REG_LOONGARCH | 0x20000ULL)
 #define KVM_REG_LOONGARCH_FPSIMD	(KVM_REG_LOONGARCH | 0x30000ULL)
 #define KVM_REG_LOONGARCH_CPUCFG	(KVM_REG_LOONGARCH | 0x40000ULL)
+#define KVM_REG_LOONGARCH_LBT		(KVM_REG_LOONGARCH | 0x50000ULL)
 #define KVM_REG_LOONGARCH_MASK		(KVM_REG_LOONGARCH | 0x70000ULL)
 #define KVM_CSR_IDX_MASK		0x7fff
 #define KVM_CPUCFG_IDX_MASK		0x7fff
@@ -77,11 +78,34 @@ struct kvm_fpu {
 /* Debugging: Special instruction for software breakpoint */
 #define KVM_REG_LOONGARCH_DEBUG_INST	(KVM_REG_LOONGARCH_KVM | KVM_REG_SIZE_U64 | 3)
 
+/* LBT registers */
+#define KVM_REG_LOONGARCH_LBT_SCR0	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 1)
+#define KVM_REG_LOONGARCH_LBT_SCR1	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 2)
+#define KVM_REG_LOONGARCH_LBT_SCR2	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 3)
+#define KVM_REG_LOONGARCH_LBT_SCR3	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 4)
+#define KVM_REG_LOONGARCH_LBT_EFLAGS	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 5)
+#define KVM_REG_LOONGARCH_LBT_FTOP	(KVM_REG_LOONGARCH_LBT | KVM_REG_SIZE_U64 | 6)
+
 #define LOONGARCH_REG_SHIFT		3
 #define LOONGARCH_REG_64(TYPE, REG)	(TYPE | KVM_REG_SIZE_U64 | (REG << LOONGARCH_REG_SHIFT))
 #define KVM_IOC_CSRID(REG)		LOONGARCH_REG_64(KVM_REG_LOONGARCH_CSR, REG)
 #define KVM_IOC_CPUCFG(REG)		LOONGARCH_REG_64(KVM_REG_LOONGARCH_CPUCFG, REG)
+
+/* Device Control API on vm fd */
+#define KVM_LOONGARCH_VM_FEAT_CTRL		0
+#define  KVM_LOONGARCH_VM_FEAT_LSX		0
+#define  KVM_LOONGARCH_VM_FEAT_LASX		1
+#define  KVM_LOONGARCH_VM_FEAT_X86BT		2
+#define  KVM_LOONGARCH_VM_FEAT_ARMBT		3
+#define  KVM_LOONGARCH_VM_FEAT_MIPSBT		4
+#define  KVM_LOONGARCH_VM_FEAT_PMU		5
+#define  KVM_LOONGARCH_VM_FEAT_PV_IPI		6
+#define  KVM_LOONGARCH_VM_FEAT_PV_STEALTIME	7
+
+/* Device Control API on vcpu fd */
 #define KVM_LOONGARCH_VCPU_CPUCFG	0
+#define KVM_LOONGARCH_VCPU_PVTIME_CTRL	1
+#define  KVM_LOONGARCH_VCPU_PVTIME_GPA	0
 
 struct kvm_debug_exit_arch {
 };
linux-headers/asm-loongarch/unistd.h +1 -3
index fcb668984f03..1f01980f9c94 100644
@@ -1,5 +1,3 @@
 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
-#define __ARCH_WANT_SYS_CLONE
-#define __ARCH_WANT_SYS_CLONE3
 
-#include <asm-generic/unistd.h>
+#include <asm/unistd_64.h>
linux-headers/asm-riscv/kvm.h +7 -0
index e878e7cc3978..e97db3296456 100644
@@ -168,6 +168,13 @@ enum KVM_RISCV_ISA_EXT_ID {
 	KVM_RISCV_ISA_EXT_ZTSO,
 	KVM_RISCV_ISA_EXT_ZACAS,
 	KVM_RISCV_ISA_EXT_SSCOFPMF,
+	KVM_RISCV_ISA_EXT_ZIMOP,
+	KVM_RISCV_ISA_EXT_ZCA,
+	KVM_RISCV_ISA_EXT_ZCB,
+	KVM_RISCV_ISA_EXT_ZCD,
+	KVM_RISCV_ISA_EXT_ZCF,
+	KVM_RISCV_ISA_EXT_ZCMOP,
+	KVM_RISCV_ISA_EXT_ZAWRS,
 	KVM_RISCV_ISA_EXT_MAX,
 };
 
linux-headers/asm-riscv/unistd.h +5 -36

46 lines not on this page · raw message

linux-headers/asm-x86/kvm.h +2 -0
index 1c8f91823481..4711ef2c3d01 100644
@@ -106,6 +106,7 @@ struct kvm_ioapic_state {
 
 #define KVM_RUN_X86_SMM		 (1 << 0)
 #define KVM_RUN_X86_BUS_LOCK     (1 << 1)
+#define KVM_RUN_X86_GUEST_MODE   (1 << 2)
 
 /* for KVM_GET_REGS and KVM_SET_REGS */
 struct kvm_regs {
@@ -436,6 +437,7 @@ struct kvm_sync_regs {
 #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT	(1 << 4)
 #define KVM_X86_QUIRK_FIX_HYPERCALL_INSN	(1 << 5)
 #define KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS	(1 << 6)
+#define KVM_X86_QUIRK_SLOT_ZAP_ALL		(1 << 7)
 
 #define KVM_STATE_NESTED_FORMAT_VMX	0
 #define KVM_STATE_NESTED_FORMAT_SVM	1
linux-headers/asm-x86/unistd_64.h +1 -0
index da439afee1cf..24c979be540e 100644
@@ -336,6 +336,7 @@
 #define __NR_statx 332
 #define __NR_io_pgetevents 333
 #define __NR_rseq 334
+#define __NR_uretprobe 335
 #define __NR_pidfd_send_signal 424
 #define __NR_io_uring_setup 425
 #define __NR_io_uring_enter 426
linux-headers/asm-x86/unistd_x32.h +1 -0
index 4fcb607c724b..c23dd21a2dd5 100644
@@ -289,6 +289,7 @@
 #define __NR_statx (__X32_SYSCALL_BIT + 332)
 #define __NR_io_pgetevents (__X32_SYSCALL_BIT + 333)
 #define __NR_rseq (__X32_SYSCALL_BIT + 334)
+#define __NR_uretprobe (__X32_SYSCALL_BIT + 335)
 #define __NR_pidfd_send_signal (__X32_SYSCALL_BIT + 424)
 #define __NR_io_uring_setup (__X32_SYSCALL_BIT + 425)
 #define __NR_io_uring_enter (__X32_SYSCALL_BIT + 426)
linux-headers/linux/bits.h +3 -0
index d9897771be8c..c0d00c0a9868 100644
@@ -12,4 +12,7 @@
         (((~_ULL(0)) - (_ULL(1) << (l)) + 1) & \
          (~_ULL(0) >> (__BITS_PER_LONG_LONG - 1 - (h))))
 
+#define __GENMASK_U128(h, l) \
+	((_BIT128((h)) << 1) - (_BIT128(l)))
+
 #endif /* _LINUX_BITS_H */
linux-headers/linux/const.h +17 -0
index 1eb84b5087f8..2122610de7f9 100644
@@ -28,6 +28,23 @@
 #define _BITUL(x)	(_UL(1) << (x))
 #define _BITULL(x)	(_ULL(1) << (x))
 
+#if !defined(__ASSEMBLY__)
+/*
+ * Missing __asm__ support
+ *
+ * __BIT128() would not work in the __asm__ code, as it shifts an
+ * 'unsigned __init128' data type as direct representation of
+ * 128 bit constants is not supported in the gcc compiler, as
+ * they get silently truncated.
+ *
+ * TODO: Please revisit this implementation when gcc compiler
+ * starts representing 128 bit constants directly like long
+ * and unsigned long etc. Subsequently drop the comment for
+ * GENMASK_U128() which would then start supporting __asm__ code.
+ */
+#define _BIT128(x)	((unsigned __int128)(1) << (x))
+#endif
+
 #define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (__typeof__(x))(a) - 1)
 #define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
 
linux-headers/linux/iommufd.h +124 -19

200 lines not on this page · raw message

linux-headers/linux/kvm.h +22 -1
index c93876ca0ba9..49dd1b30ce9e 100644
@@ -192,11 +192,20 @@ struct kvm_xen_exit {
 /* Flags that describe what fields in emulation_failure hold valid data. */
 #define KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES (1ULL << 0)
 
+/*
+ * struct kvm_run can be modified by userspace at any time, so KVM must be
+ * careful to avoid TOCTOU bugs. In order to protect KVM, HINT_UNSAFE_IN_KVM()
+ * renames fields in struct kvm_run from <symbol> to <symbol>__unsafe when
+ * compiled into the kernel, ensuring that any use within KVM is obvious and
+ * gets extra scrutiny.
+ */
+#define HINT_UNSAFE_IN_KVM(_symbol) _symbol
+
 /* for KVM_RUN, returned by mmap(vcpu_fd, offset=0) */
 struct kvm_run {
 	/* in */
 	__u8 request_interrupt_window;
-	__u8 immediate_exit;
+	__u8 HINT_UNSAFE_IN_KVM(immediate_exit);
 	__u8 padding1[6];
 
 	/* out */
@@ -913,6 +922,9 @@ struct kvm_enable_cap {
 #define KVM_CAP_MEMORY_ATTRIBUTES 233
 #define KVM_CAP_GUEST_MEMFD 234
 #define KVM_CAP_VM_TYPES 235
+#define KVM_CAP_PRE_FAULT_MEMORY 236
+#define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237
+#define KVM_CAP_X86_GUEST_MODE 238
 
 struct kvm_irq_routing_irqchip {
 	__u32 irqchip;
@@ -1544,4 +1556,13 @@ struct kvm_create_guest_memfd {
 	__u64 reserved[6];
 };
 
+#define KVM_PRE_FAULT_MEMORY	_IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory)
+
+struct kvm_pre_fault_memory {
+	__u64 gpa;
+	__u64 size;
+	__u64 flags;
+	__u64 padding[5];
+};
+
 #endif /* __LINUX_KVM_H */
linux-headers/linux/mman.h +1 -0
index 4e8cb6078058..2b83059586e9 100644
@@ -17,6 +17,7 @@
 #define MAP_SHARED	0x01		/* Share changes */
 #define MAP_PRIVATE	0x02		/* Changes are private */
 #define MAP_SHARED_VALIDATE 0x03	/* share + validate extension flags */
+#define MAP_DROPPABLE	0x08		/* Zero memory under memory pressure. */
 
 /*
  * Huge page size encoding when MAP_HUGETLB is specified, and a huge page
linux-headers/linux/psp-sev.h +28 -0
index c3046c6bfff5..17bf19157329 100644
@@ -31,6 +31,7 @@ enum {
 	SNP_PLATFORM_STATUS,
 	SNP_COMMIT,
 	SNP_SET_CONFIG,
+	SNP_VLEK_LOAD,
 
 	SEV_MAX,
 };
@@ -50,6 +51,7 @@ typedef enum {
 	SEV_RET_INVALID_PLATFORM_STATE,
 	SEV_RET_INVALID_GUEST_STATE,
 	SEV_RET_INAVLID_CONFIG,
+	SEV_RET_INVALID_CONFIG = SEV_RET_INAVLID_CONFIG,
 	SEV_RET_INVALID_LEN,
 	SEV_RET_ALREADY_OWNED,
 	SEV_RET_INVALID_CERTIFICATE,
@@ -214,6 +216,32 @@ struct sev_user_data_snp_config {
 	__u8 rsvd1[52];
 } __attribute__((packed));
 
+/**
+ * struct sev_data_snp_vlek_load - SNP_VLEK_LOAD structure
+ *
+ * @len: length of the command buffer read by the PSP
+ * @vlek_wrapped_version: version of wrapped VLEK hashstick (Must be 0h)
+ * @rsvd: reserved
+ * @vlek_wrapped_address: address of a wrapped VLEK hashstick
+ *                        (struct sev_user_data_snp_wrapped_vlek_hashstick)
+ */
+struct sev_user_data_snp_vlek_load {
+	__u32 len;				/* In */
+	__u8 vlek_wrapped_version;		/* In */
+	__u8 rsvd[3];				/* In */
+	__u64 vlek_wrapped_address;		/* In */
+} __attribute__((packed));
+
+/**
+ * struct sev_user_data_snp_vlek_wrapped_vlek_hashstick - Wrapped VLEK data
+ *
+ * @data: Opaque data provided by AMD KDS (as described in SEV-SNP Firmware ABI
+ *        1.54, SNP_VLEK_LOAD)
+ */
+struct sev_user_data_snp_wrapped_vlek_hashstick {
+	__u8 data[432];				/* In */
+} __attribute__((packed));
+
 /**
  * struct sev_issue_cmd - SEV ioctl parameters
  *
Signature · 1 line
2.47.0
#8 lore raw

PATCH v3 4/6 virtio-gpu: Handle virgl fence creation errors

In reply to Dmitry Osipenko

Print out error messages when virgl fence creation fails to aid debugging of the fence-related bugs.

---
 hw/display/virtio-gpu-virgl.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +11 -1
index eedae7357f1a..3c564683820b 100644
@@ -892,6 +892,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
     bool cmd_suspended = false;
+    int ret;
 
     VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
 
@@ -990,7 +991,16 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
     }
 
     trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
-    virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
+
+    /*
+     * Unlike other virglrenderer functions, this one returns a positive
+     * error code.
+     */
+    ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0);
+    if (ret)
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: virgl_renderer_create_fence error: %s",
+                      __func__, strerror(ret));
 }
 
 static void virgl_write_fence(void *opaque, uint32_t fence)
Signature · 1 line
2.47.0
#9 lore raw

Re: [PATCH v3 4/6] virtio-gpu: Handle virgl fence creation errors

Quoted 25 lines · Dmitry Osipenko

Print out error messages when virgl fence creation fails to aid debugging of the fence-related bugs.

---
 hw/display/virtio-gpu-virgl.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +1 -1
index eedae7357f1a..3c564683820b 100644
@@ -892,6 +892,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
     bool cmd_suspended = false;
+    int ret;
 
     VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
 
@@ -990,7 +991,16 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
     }
 
     trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
-    virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1003

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+
+    /*
+     * Unlike other virglrenderer functions, this one returns a positive
+     * error code.
+     */
+    ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0);
+    if (ret)
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: virgl_renderer_create_fence error: %s",
+                      __func__, strerror(ret));

braces please. Also if ret is only used for the if leg you might as well combine it:

  if (virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0) != 0) {
    qemu_log_mask(...) 
  }
Quoted 3 lines
hw/display/virtio-gpu-virgl.c +0 -0
 }
 
 static void virgl_write_fence(void *opaque, uint32_t fence)
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#10 lore raw

Re: [PATCH v3 4/6] virtio-gpu: Handle virgl fence creation errors

Quoted 41 lines · Alex Bennée
Quoted 25 lines · Dmitry Osipenko

Print out error messages when virgl fence creation fails to aid debugging of the fence-related bugs.

---
 hw/display/virtio-gpu-virgl.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +1 -1
index eedae7357f1a..3c564683820b 100644
@@ -892,6 +892,7 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                       struct virtio_gpu_ctrl_command *cmd)
 {
     bool cmd_suspended = false;
+    int ret;
 
     VIRTIO_GPU_FILL_CMD(cmd->cmd_hdr);
 
@@ -990,7 +991,16 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
     }
 
     trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
-    virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);

hw/display/virtio-gpu-virgl.c void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, · line 1003

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +10 -0
+
+    /*
+     * Unlike other virglrenderer functions, this one returns a positive
+     * error code.
+     */
+    ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0);
+    if (ret)
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: virgl_renderer_create_fence error: %s",
+                      __func__, strerror(ret));

braces please. Also if ret is only used for the if leg you might as well combine it:

Quoted 3 lines
  if (virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0) != 0) {
    qemu_log_mask(...) 
  }

Ack, wish checkpatch warned about braces

Signature · 2 lines
Best regards,
Dmitry
#11 lore raw

PATCH v3 5/6 virtio-gpu: Support asynchronous fencing

In reply to Dmitry Osipenko

Support asynchronous fencing feature of virglrenderer. It allows Qemu to handle fence as soon as it's signalled instead of periodically polling the fence status. This feature is required for enabling DRM context support in Qemu because legacy fencing mode isn't supported for DRM contexts in virglrenderer.

---
 hw/display/virtio-gpu-gl.c     |   3 +
 hw/display/virtio-gpu-virgl.c  | 138 +++++++++++++++++++++++++++------
 include/hw/virtio/virtio-gpu.h |  13 ++++
 3 files changed, 132 insertions(+), 22 deletions(-)
hw/display/virtio-gpu-gl.c +3 -0
index 7c0e448b4661..53d938f23f20 100644
@@ -170,6 +170,9 @@ static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
     if (gl->renderer_state >= RS_INITED) {
 #if VIRGL_VERSION_MAJOR >= 1
         qemu_bh_delete(gl->cmdq_resume_bh);
+
+        virtio_gpu_virgl_reset_async_fences(g);
+        qemu_bh_delete(gl->async_fence_bh);
 #endif
         if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
             timer_free(gl->print_stats);
hw/display/virtio-gpu-virgl.c +116 -22

180 lines not on this page · raw message

include/hw/virtio/virtio-gpu.h +13 -0
index 553799b8cc72..99cc6286f473 100644
@@ -227,6 +227,13 @@ struct VirtIOGPUClass {
                              Error **errp);
 };
 
+struct virtio_gpu_virgl_context_fence {
+    uint32_t ctx_id;
+    int64_t ring_idx;
+    uint64_t fence_id;
+    QSLIST_ENTRY(virtio_gpu_virgl_context_fence) next;
+};
+
 /* VirtIOGPUGL renderer states */
 typedef enum {
     RS_START,       /* starting state */
@@ -244,6 +251,11 @@ struct VirtIOGPUGL {
     QEMUTimer *print_stats;
 
     QEMUBH *cmdq_resume_bh;
+
+    QEMUBH *async_fence_bh;
+    QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq;
+
+    bool context_fence_enabled;
 };
 
 struct VhostUserGPU {
@@ -358,5 +370,6 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g);
 void virtio_gpu_virgl_reset(VirtIOGPU *g);
 int virtio_gpu_virgl_init(VirtIOGPU *g);
 GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g);
+void virtio_gpu_virgl_reset_async_fences(VirtIOGPU *g);
 
 #endif
Signature · 1 line
2.47.0
#12 lore raw

Re: [PATCH v3 5/6] virtio-gpu: Support asynchronous fencing

Quoted 135 lines · Dmitry Osipenko

Support asynchronous fencing feature of virglrenderer. It allows Qemu to handle fence as soon as it's signalled instead of periodically polling the fence status. This feature is required for enabling DRM context support in Qemu because legacy fencing mode isn't supported for DRM contexts in virglrenderer.

---
  hw/display/virtio-gpu-gl.c     |   3 +
  hw/display/virtio-gpu-virgl.c  | 138 +++++++++++++++++++++++++++------
  include/hw/virtio/virtio-gpu.h |  13 ++++
  3 files changed, 132 insertions(+), 22 deletions(-)
hw/display/virtio-gpu-gl.c +3 -0
index 7c0e448b4661..53d938f23f20 100644
@@ -170,6 +170,9 @@ static void virtio_gpu_gl_device_unrealize(DeviceState *qdev)
      if (gl->renderer_state >= RS_INITED) {
  #if VIRGL_VERSION_MAJOR >= 1
          qemu_bh_delete(gl->cmdq_resume_bh);
+
+        virtio_gpu_virgl_reset_async_fences(g);
+        qemu_bh_delete(gl->async_fence_bh);
  #endif
          if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
              timer_free(gl->print_stats);
hw/display/virtio-gpu-virgl.c +60 -22
index 3c564683820b..37b40e258398 100644
@@ -891,6 +891,7 @@ static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
  void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
                                        struct virtio_gpu_ctrl_command *cmd)
  {
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
      bool cmd_suspended = false;
      int ret;
 
@@ -992,34 +993,71 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,
 
      trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd->cmd_hdr.type);
 
-    /*
-     * Unlike other virglrenderer functions, this one returns a positive
-     * error code.
-     */
-    ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0);
-    if (ret)
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: virgl_renderer_create_fence error: %s",
-                      __func__, strerror(ret));
+    if (gl->context_fence_enabled &&
+        (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_INFO_RING_IDX)) {
+        uint32_t flags = 0;
+
+        ret = virgl_renderer_context_create_fence(cmd->cmd_hdr.ctx_id, flags,
+                                                  cmd->cmd_hdr.ring_idx,
+                                                  cmd->cmd_hdr.fence_id);
+        if (ret)
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: virgl_renderer_context_create_fence error: %s",
+                          __func__, strerror(-ret));
+    } else {
+        /*
+         * Unlike other virglrenderer functions, this one returns a positive
+         * error code.
+         */
+        ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0);
+        if (ret)
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: virgl_renderer_create_fence error: %s",
+                          __func__, strerror(ret));
+    }
  }
 
-static void virgl_write_fence(void *opaque, uint32_t fence)
+static void virtio_gpu_virgl_async_fence_bh(void *opaque)
  {
-    VirtIOGPU *g = opaque;
+    QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq;
      struct virtio_gpu_ctrl_command *cmd, *tmp;
+    struct virtio_gpu_virgl_context_fence *f;
+    VirtIOGPU *g = opaque;
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
 
-    QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
-        /*
-         * the guest can end up emitting fences out of order
-         * so we should check all fenced cmds not just the first one.
-         */
-        if (cmd->cmd_hdr.fence_id > fence) {
-            continue;
+    QSLIST_MOVE_ATOMIC(&async_fenceq, &gl->async_fenceq);
+
+    while (!QSLIST_EMPTY(&async_fenceq)) {
+        f = QSLIST_FIRST(&async_fenceq);
+
+        QSLIST_REMOVE_HEAD(&async_fenceq, next);
+
+        QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
+            /*
+             * the guest can end up emitting fences out of order
+             * so we should check all fenced cmds not just the first one.
+             */
+            if (cmd->cmd_hdr.fence_id > f->fence_id) {
+                continue;
+            }
+            if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_INFO_RING_IDX) {
+                if (cmd->cmd_hdr.ring_idx != f->ring_idx) {
+                    continue;
+                }
+                if (cmd->cmd_hdr.ctx_id != f->ctx_id) {
+                    continue;
+                }
+            } else if (f->ring_idx >= 0) {
+                /* ctx0 GL-query fences don't have ring info */
+                continue;
+            }
+            virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
+            QTAILQ_REMOVE(&g->fenceq, cmd, next);
+            g_free(cmd);
          }
-        trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id);
-        virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
-        QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+
+        trace_virtio_gpu_fence_resp(f->fence_id);
+        g_free(f);
          g->inflight--;
          if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {
              trace_virtio_gpu_dec_inflight_fences(g->inflight);
@@ -1027,6 +1065,50 @@ static void virgl_write_fence(void *opaque, uint32_t fence)
      }

hw/display/virtio-gpu-virgl.c static void virgl_write_fence(void *opaque, uint32_t fence) · line 1075

Quoted 10 lines
hw/display/virtio-gpu-virgl.c +8 -0
  }
 
+void virtio_gpu_virgl_reset_async_fences(VirtIOGPU *g)
+{
+    struct virtio_gpu_virgl_context_fence *f, *f_tmp;
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+
+    QSLIST_FOREACH_SAFE(f, &gl->async_fenceq, next, f_tmp) {
+        QSLIST_REMOVE(&gl->async_fenceq, f, virtio_gpu_virgl_context_fence,
+                      next);

Replace QSLIST_FOREACH_SAFE() and QSLIST_REMOVE() with QSLIST_EMPTY(), QSLIST_FIRST() and QSLIST_REMOVE_HEAD() as you have done for virtio_gpu_virgl_async_fence_bh().

Quoted 109 lines
hw/display/virtio-gpu-virgl.c +48 -0
+        g_free(f);
+    }
+}
+
+static void
+virtio_gpu_virgl_push_async_fence(VirtIOGPU *g, uint32_t ctx_id,
+                                  int64_t ring_idx, uint64_t fence_id)
+{
+    struct virtio_gpu_virgl_context_fence *f;
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+
+    f = g_new(struct virtio_gpu_virgl_context_fence, 1);
+    f->ctx_id = ctx_id;
+    f->ring_idx = ring_idx;
+    f->fence_id = fence_id;
+
+    QSLIST_INSERT_HEAD_ATOMIC(&gl->async_fenceq, f, next);
+
+    qemu_bh_schedule(gl->async_fence_bh);
+}
+
+static void virgl_write_fence(void *opaque, uint32_t fence)
+{
+    VirtIOGPU *g = opaque;
+
+    virtio_gpu_virgl_push_async_fence(g, 0, -1, fence);
+}
+
+static void virgl_write_context_fence(void *opaque, uint32_t ctx_id,
+                                      uint32_t ring_idx, uint64_t fence)
+{
+    VirtIOGPU *g = opaque;
+
+    virtio_gpu_virgl_push_async_fence(g, ctx_id, ring_idx, fence);
+}
+
  static virgl_renderer_gl_context
  virgl_create_context(void *opaque, int scanout_idx,
                       struct virgl_renderer_gl_ctx_param *params)
@@ -1114,6 +1196,8 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g)
          dpy_gfx_replace_surface(g->parent_obj.scanout[i].con, NULL);
          dpy_gl_scanout_disable(g->parent_obj.scanout[i].con);
      }
+
+    virtio_gpu_virgl_reset_async_fences(g);
  }
 
  void virtio_gpu_virgl_reset(VirtIOGPU *g)
@@ -1131,6 +1215,12 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      if (qemu_egl_display) {
          virtio_gpu_3d_cbs.version = 4;
          virtio_gpu_3d_cbs.get_egl_display = virgl_get_egl_display;
+#if VIRGL_VERSION_MAJOR >= 1
+        virtio_gpu_3d_cbs.write_context_fence = virgl_write_context_fence;
+        flags |= VIRGL_RENDERER_ASYNC_FENCE_CB;
+        flags |= VIRGL_RENDERER_THREAD_SYNC;
+        gl->context_fence_enabled = true;
+#endif
      }
  #endif
  #ifdef VIRGL_RENDERER_D3D11_SHARE_TEXTURE
@@ -1164,6 +1254,10 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      gl->cmdq_resume_bh = aio_bh_new(qemu_get_aio_context(),
                                      virtio_gpu_virgl_resume_cmdq_bh,
                                      g);
+
+    gl->async_fence_bh = aio_bh_new(qemu_get_aio_context(),
+                                    virtio_gpu_virgl_async_fence_bh,
+                                    g);
  #endif
 
      return 0;
include/hw/virtio/virtio-gpu.h +13 -0
index 553799b8cc72..99cc6286f473 100644
@@ -227,6 +227,13 @@ struct VirtIOGPUClass {
                               Error **errp);
  };
 
+struct virtio_gpu_virgl_context_fence {
+    uint32_t ctx_id;
+    int64_t ring_idx;
+    uint64_t fence_id;
+    QSLIST_ENTRY(virtio_gpu_virgl_context_fence) next;
+};
+
  /* VirtIOGPUGL renderer states */
  typedef enum {
      RS_START,       /* starting state */
@@ -244,6 +251,11 @@ struct VirtIOGPUGL {
      QEMUTimer *print_stats;
 
      QEMUBH *cmdq_resume_bh;
+
+    QEMUBH *async_fence_bh;
+    QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq;
+
+    bool context_fence_enabled;
  };
 
  struct VhostUserGPU {
@@ -358,5 +370,6 @@ void virtio_gpu_virgl_reset_scanout(VirtIOGPU *g);
  void virtio_gpu_virgl_reset(VirtIOGPU *g);
  int virtio_gpu_virgl_init(VirtIOGPU *g);
  GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g);
+void virtio_gpu_virgl_reset_async_fences(VirtIOGPU *g);
 
  #endif
#13 lore raw

Re: [PATCH v3 5/6] virtio-gpu: Support asynchronous fencing

Quoted 149 lines · Akihiko Odaki
Quoted 148 lines · Dmitry Osipenko

Support asynchronous fencing feature of virglrenderer. It allows Qemu to handle fence as soon as it's signalled instead of periodically polling the fence status. This feature is required for enabling DRM context support in Qemu because legacy fencing mode isn't supported for DRM contexts in virglrenderer.

---
  hw/display/virtio-gpu-gl.c     |   3 + hw/display/virtio-gpu-virgl.c  | 138 +++++++++++++++++++++++++++------ include/hw/virtio/virtio-gpu.h |  13 ++++

  3 files changed, 132 insertions(+), 22 deletions(-)
hw/display/virtio-gpu-gl.c +3 -0
index 7c0e448b4661..53d938f23f20 100644
@@ -170,6 +170,9 @@ static void
 virtio_gpu_gl_device_unrealize(DeviceState *qdev)
       if (gl->renderer_state >= RS_INITED) {
   #if VIRGL_VERSION_MAJOR >= 1
           qemu_bh_delete(gl->cmdq_resume_bh);
+
+        virtio_gpu_virgl_reset_async_fences(g);
+        qemu_bh_delete(gl->async_fence_bh);
   #endif
           if (virtio_gpu_stats_enabled(g->parent_obj.conf)) {

              timer_free(gl->print_stats);

hw/display/virtio-gpu- renamed from hw/display/virtio-gpu-virgl.c +0 -0

virgl.c
index 3c564683820b..37b40e258398 100644

hw/display/virtio-gpu-virgl.c +0 -0
@@ -891,6 +891,7 @@ static void virgl_cmd_set_scanout_blob(VirtIOGPU *g,
   void virtio_gpu_virgl_process_cmd(VirtIOGPU *g,

                                        struct virtio_gpu_ctrl_command *cmd)
  {
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
      bool cmd_suspended = false;
      int ret;
  @@ -992,34 +993,71 @@ void virtio_gpu_virgl_process_cmd(VirtIOGPU *g, trace_virtio_gpu_fence_ctrl(cmd->cmd_hdr.fence_id, cmd-

Quoted 1 line

cmd_hdr.type);

  -    /*
-     * Unlike other virglrenderer functions, this one returns a positive -     * error code.
-     */
-    ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0); -    if (ret)
-        qemu_log_mask(LOG_GUEST_ERROR,
-                      "%s: virgl_renderer_create_fence error: %s", -                      __func__, strerror(ret));
+    if (gl->context_fence_enabled &&
+        (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_INFO_RING_IDX)) { +        uint32_t flags = 0;
+
+        ret = virgl_renderer_context_create_fence(cmd-

Quoted 1 line

cmd_hdr.ctx_id, flags,

+                                                  cmd->cmd_hdr.ring_idx, +                                                  cmd-

Quoted 1 line

cmd_hdr.fence_id);

+        if (ret)
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: virgl_renderer_context_create_fence
error: %s",
+                          __func__, strerror(-ret));
+    } else {
+        /*
+         * Unlike other virglrenderer functions, this one returns a positive
+         * error code.
+         */
+        ret = virgl_renderer_create_fence(cmd->cmd_hdr.fence_id, 0); +        if (ret)
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: virgl_renderer_create_fence error: %s", +                          __func__, strerror(ret));
+    }
  }
  -static void virgl_write_fence(void *opaque, uint32_t fence) +static void virtio_gpu_virgl_async_fence_bh(void *opaque)
  {
-    VirtIOGPU *g = opaque;
+    QSLIST_HEAD(, virtio_gpu_virgl_context_fence) async_fenceq; struct virtio_gpu_ctrl_command *cmd, *tmp;
+    struct virtio_gpu_virgl_context_fence *f;
+    VirtIOGPU *g = opaque;
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
  -    QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
-        /*
-         * the guest can end up emitting fences out of order -         * so we should check all fenced cmds not just the first one. -         */
-        if (cmd->cmd_hdr.fence_id > fence) {
-            continue;
+    QSLIST_MOVE_ATOMIC(&async_fenceq, &gl->async_fenceq);

hw/display/virtio-gpu-virgl.c +10 -0
+
+    while (!QSLIST_EMPTY(&async_fenceq)) {
+        f = QSLIST_FIRST(&async_fenceq);
+
+        QSLIST_REMOVE_HEAD(&async_fenceq, next);
+
+        QTAILQ_FOREACH_SAFE(cmd, &g->fenceq, next, tmp) {
+            /*
+             * the guest can end up emitting fences out of order
+             * so we should check all fenced cmds not just the first

one.
+             */
+            if (cmd->cmd_hdr.fence_id > f->fence_id) {
+                continue;
+            }
+            if (cmd->cmd_hdr.flags & VIRTIO_GPU_FLAG_INFO_RING_IDX) { +                if (cmd->cmd_hdr.ring_idx != f->ring_idx) { +                    continue;
+                }
+                if (cmd->cmd_hdr.ctx_id != f->ctx_id) {
+                    continue;
+                }
+            } else if (f->ring_idx >= 0) {
+                /* ctx0 GL-query fences don't have ring info */ +                continue;
+            }
+            virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
+            QTAILQ_REMOVE(&g->fenceq, cmd, next);
+            g_free(cmd);
          }
-        trace_virtio_gpu_fence_resp(cmd->cmd_hdr.fence_id); -        virtio_gpu_ctrl_response_nodata(g, cmd, VIRTIO_GPU_RESP_OK_NODATA);
-        QTAILQ_REMOVE(&g->fenceq, cmd, next);
-        g_free(cmd);
+
+        trace_virtio_gpu_fence_resp(f->fence_id);
+        g_free(f);
          g->inflight--;
          if (virtio_gpu_stats_enabled(g->parent_obj.conf)) { trace_virtio_gpu_dec_inflight_fences(g->inflight);

hw/display/virtio-gpu-virgl.c +4 -0
@@ -1027,6 +1065,50 @@ static void virgl_write_fence(void *opaque,
 uint32_t fence)
       }
   }
   +void virtio_gpu_virgl_reset_async_fences(VirtIOGPU *g)
+{
+    struct virtio_gpu_virgl_context_fence *f, *f_tmp;
+    VirtIOGPUGL *gl = VIRTIO_GPU_GL(g);
+
Quoted 8 lines

hw/display/virtio-gpu-virgl.c static void virgl_write_fence(void *opaque, uint32_t fence) · line 1075

Quoted 4 lines
hw/display/virtio-gpu-virgl.c +3 -0
+    QSLIST_FOREACH_SAFE(f, &gl->async_fenceq, next, f_tmp) {
+        QSLIST_REMOVE(&gl->async_fenceq, f,
 virtio_gpu_virgl_context_fence,
+                      next);

Replace QSLIST_FOREACH_SAFE() and QSLIST_REMOVE() with QSLIST_EMPTY(), QSLIST_FIRST() and QSLIST_REMOVE_HEAD() as you have done for virtio_gpu_virgl_async_fence_bh().

Ack

Signature · 2 lines
Best regards,
Dmitry
#14 lore raw

PATCH v3 6/6 virtio-gpu: Support DRM native context

In reply to Dmitry Osipenko

Add support for DRM native contexts to VirtIO-GPU. DRM context is enabled using a new virtio-gpu-gl device option "drm=on".

Unlike Virgl and Venus contexts that operate on application API level, DRM native contexts work on a kernel UAPI level. This lower level results in a lightweight context implementations that yield better performance.

---
 docs/system/devices/virtio-gpu.rst | 11 +++++++++++
 hw/display/virtio-gpu-gl.c         |  2 ++
 hw/display/virtio-gpu-virgl.c      | 22 ++++++++++++++++++++++
 hw/display/virtio-gpu.c            | 15 +++++++++++++++
 include/hw/virtio/virtio-gpu.h     |  3 +++
 5 files changed, 53 insertions(+)
docs/system/devices/virtio-gpu.rst +11 -0
index b7eb0fc0e727..49a75138f7ef 100644
@@ -82,6 +82,17 @@ of virtio-gpu host memory window. This is typically between 256M and 8G.
 
 .. _venus: https://gitlab.freedesktop.org/virgl/venus-protocol/
 
+DRM native context is supported since release of `virglrenderer`_ v1.0.0
+using `drm`_ protocol. ``DRM`` virtio-gpu capability set ("capset") requires
+host blob support (``hostmem`` and ``blob`` fields) and should be enabled
+using ``drm`` field. The ``hostmem`` field specifies the size of virtio-gpu
+host memory window. This is typically between 256M and 8G.
+
+.. parsed-literal::
+    -device virtio-gpu-gl,hostmem=8G,blob=on,drm=on
+
+.. _drm: https://gitlab.freedesktop.org/virgl/virglrenderer/-/tree/main/src/drm
+
 virtio-gpu rutabaga
 -------------------
 
hw/display/virtio-gpu-gl.c +2 -0
index 53d938f23f20..bd0c0692a5c4 100644
@@ -159,6 +159,8 @@ static Property virtio_gpu_gl_properties[] = {
                     VIRTIO_GPU_FLAG_STATS_ENABLED, false),
     DEFINE_PROP_BIT("venus", VirtIOGPU, parent_obj.conf.flags,
                     VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
+    DEFINE_PROP_BIT("drm", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_DRM_ENABLED, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/display/virtio-gpu-virgl.c +22 -0
index 37b40e258398..89ca1878fb7c 100644
@@ -1232,6 +1232,19 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
         flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
     }
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        flags |= VIRGL_RENDERER_DRM;
+
+        if (!gl->context_fence_enabled) {
+            /*
+             * Virglrenderer skips enabling DRM context support without
+             * enabled async-fence feature. VirtIO-GPU will initialize
+             * successfully, but DRM context won't be available in guest.
+             */
+            error_report("DRM native context requires EGL display");
+            return -EINVAL;
+        }
+    }
 #endif
 
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
@@ -1294,5 +1307,14 @@ GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g)
         }
     }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_DRM,
+                                   &capset_max_ver,
+                                   &capset_max_size);
+        if (capset_max_size) {
+            virtio_gpu_virgl_add_capset(capset_ids, VIRTIO_GPU_CAPSET_DRM);
+        }
+    }
+
     return capset_ids;
 }
hw/display/virtio-gpu.c +15 -0
index c0570ef8565a..c1acafe6246b 100644
@@ -1492,6 +1492,21 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
 #endif
     }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+#ifdef VIRGL_VERSION_MAJOR
+    #if VIRGL_VERSION_MAJOR >= 1
+        if (!virtio_gpu_blob_enabled(g->parent_obj.conf) ||
+            !virtio_gpu_hostmem_enabled(g->parent_obj.conf)) {
+            error_setg(errp, "drm requires enabled blob and hostmem options");
+            return;
+        }
+    #else
+        error_setg(errp, "old virglrenderer, drm unsupported");
+        return;
+    #endif
+#endif
+    }
+
     if (!virtio_gpu_base_device_realize(qdev,
                                         virtio_gpu_handle_ctrl_cb,
                                         virtio_gpu_handle_cursor_cb,
include/hw/virtio/virtio-gpu.h +3 -0
index 99cc6286f473..f1799fcb6eee 100644
@@ -98,6 +98,7 @@ enum virtio_gpu_base_conf_flags {
     VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
     VIRTIO_GPU_FLAG_RUTABAGA_ENABLED,
     VIRTIO_GPU_FLAG_VENUS_ENABLED,
+    VIRTIO_GPU_FLAG_DRM_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -118,6 +119,8 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.hostmem > 0)
 #define virtio_gpu_venus_enabled(_cfg) \
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
+#define virtio_gpu_drm_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DRM_ENABLED))
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
Signature · 1 line
2.47.0
#15 lore raw

Re: [PATCH v3 6/6] virtio-gpu: Support DRM native context

Quoted 2 lines · Dmitry Osipenko

Add support for DRM native contexts to VirtIO-GPU. DRM context is enabled using a new virtio-gpu-gl device option "drm=on".

I feel like using "drm" is confusing in this context because drm exists for the host and guest. What about "native-context" or even "context=[opengl|vulkan|wayland|native]"?

The GPU command line is already getting complex so now might be the time to rationalise it.

As an aside can mesa build the intel drivers on non-x86 systems as now I could potentially pass my native intel context to my emulated aarch64 guests?

Quoted 135 lines

Unlike Virgl and Venus contexts that operate on application API level, DRM native contexts work on a kernel UAPI level. This lower level results in a lightweight context implementations that yield better performance.

---
 docs/system/devices/virtio-gpu.rst | 11 +++++++++++
 hw/display/virtio-gpu-gl.c         |  2 ++
 hw/display/virtio-gpu-virgl.c      | 22 ++++++++++++++++++++++
 hw/display/virtio-gpu.c            | 15 +++++++++++++++
 include/hw/virtio/virtio-gpu.h     |  3 +++
 5 files changed, 53 insertions(+)
docs/system/devices/virtio-gpu.rst +11 -0
index b7eb0fc0e727..49a75138f7ef 100644
@@ -82,6 +82,17 @@ of virtio-gpu host memory window. This is typically between 256M and 8G.
 
 .. _venus: https://gitlab.freedesktop.org/virgl/venus-protocol/
 
+DRM native context is supported since release of `virglrenderer`_ v1.0.0
+using `drm`_ protocol. ``DRM`` virtio-gpu capability set ("capset") requires
+host blob support (``hostmem`` and ``blob`` fields) and should be enabled
+using ``drm`` field. The ``hostmem`` field specifies the size of virtio-gpu
+host memory window. This is typically between 256M and 8G.
+
+.. parsed-literal::
+    -device virtio-gpu-gl,hostmem=8G,blob=on,drm=on
+
+.. _drm: https://gitlab.freedesktop.org/virgl/virglrenderer/-/tree/main/src/drm
+
 virtio-gpu rutabaga
 -------------------
 
hw/display/virtio-gpu-gl.c +2 -0
index 53d938f23f20..bd0c0692a5c4 100644
@@ -159,6 +159,8 @@ static Property virtio_gpu_gl_properties[] = {
                     VIRTIO_GPU_FLAG_STATS_ENABLED, false),
     DEFINE_PROP_BIT("venus", VirtIOGPU, parent_obj.conf.flags,
                     VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
+    DEFINE_PROP_BIT("drm", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_DRM_ENABLED, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
hw/display/virtio-gpu-virgl.c +22 -0
index 37b40e258398..89ca1878fb7c 100644
@@ -1232,6 +1232,19 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
     if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
         flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
     }
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        flags |= VIRGL_RENDERER_DRM;
+
+        if (!gl->context_fence_enabled) {
+            /*
+             * Virglrenderer skips enabling DRM context support without
+             * enabled async-fence feature. VirtIO-GPU will initialize
+             * successfully, but DRM context won't be available in guest.
+             */
+            error_report("DRM native context requires EGL display");
+            return -EINVAL;
+        }
+    }
 #endif
 
     ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
@@ -1294,5 +1307,14 @@ GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g)
         }
     }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_DRM,
+                                   &capset_max_ver,
+                                   &capset_max_size);
+        if (capset_max_size) {
+            virtio_gpu_virgl_add_capset(capset_ids, VIRTIO_GPU_CAPSET_DRM);
+        }
+    }
+
     return capset_ids;
 }
hw/display/virtio-gpu.c +15 -0
index c0570ef8565a..c1acafe6246b 100644
@@ -1492,6 +1492,21 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
 #endif
     }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+#ifdef VIRGL_VERSION_MAJOR
+    #if VIRGL_VERSION_MAJOR >= 1
+        if (!virtio_gpu_blob_enabled(g->parent_obj.conf) ||
+            !virtio_gpu_hostmem_enabled(g->parent_obj.conf)) {
+            error_setg(errp, "drm requires enabled blob and hostmem options");
+            return;
+        }
+    #else
+        error_setg(errp, "old virglrenderer, drm unsupported");
+        return;
+    #endif
+#endif
+    }
+
     if (!virtio_gpu_base_device_realize(qdev,
                                         virtio_gpu_handle_ctrl_cb,
                                         virtio_gpu_handle_cursor_cb,
include/hw/virtio/virtio-gpu.h +3 -0
index 99cc6286f473..f1799fcb6eee 100644
@@ -98,6 +98,7 @@ enum virtio_gpu_base_conf_flags {
     VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
     VIRTIO_GPU_FLAG_RUTABAGA_ENABLED,
     VIRTIO_GPU_FLAG_VENUS_ENABLED,
+    VIRTIO_GPU_FLAG_DRM_ENABLED,
 };
 
 #define virtio_gpu_virgl_enabled(_cfg) \
@@ -118,6 +119,8 @@ enum virtio_gpu_base_conf_flags {
     (_cfg.hostmem > 0)
 #define virtio_gpu_venus_enabled(_cfg) \
     (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
+#define virtio_gpu_drm_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DRM_ENABLED))
 
 struct virtio_gpu_base_conf {
     uint32_t max_outputs;
Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#16 lore raw

Re: [PATCH v3 6/6] virtio-gpu: Support DRM native context

Quoted 10 lines · Alex Bennée
Quoted 2 lines · Dmitry Osipenko

Add support for DRM native contexts to VirtIO-GPU. DRM context is enabled using a new virtio-gpu-gl device option "drm=on".

I feel like using "drm" is confusing in this context because drm exists for the host and guest. What about "native-context" or even "context=[opengl|vulkan|wayland|native]"?
The GPU command line is already getting complex so now might be the time to rationalise it.

Will think on it for v4, thanks.

Quoted 3 lines

As an aside can mesa build the intel drivers on non-x86 systems as now I could potentially pass my native intel context to my emulated aarch64 guests?

In general it should work. Don't know for sure how well Intel Mesa driver works on ARM, suppose it should work okay because Intel now has dGPUs. Intel Mesa driver has x86-specific assumptions, though maybe they are optional for dGPUs. You may try to plug a DG2 card into your AVA machine and report back :)

Signature · 2 lines
Best regards,
Dmitry
#17 lore raw

Re: [PATCH v3 6/6] virtio-gpu: Support DRM native context

Quoted 8 lines · Dmitry Osipenko
Quoted 3 lines

As an aside can mesa build the intel drivers on non-x86 systems as now I could potentially pass my native intel context to my emulated aarch64 guests?

In general it should work. Don't know for sure how well Intel Mesa driver works on ARM, suppose it should work okay because Intel now has dGPUs. Intel Mesa driver has x86-specific assumptions, though maybe they are optional for dGPUs. You may try to plug a DG2 card into your AVA machine and report back 🙂

On the other hand, you're right, intel-nctx should work with ARM guest without KVM. But it should be dead slow without KVM accel, similarly or worth than it was with Venus when I was testing it on ARM guest.

Signature · 2 lines
Best regards,
Dmitry
#18 lore raw

Re: [PATCH v3 6/6] virtio-gpu: Support DRM native context

In reply to Alex Bennée
Quoted 8 lines · Alex Bennée
Quoted 2 lines · Dmitry Osipenko

Add support for DRM native contexts to VirtIO-GPU. DRM context is enabled using a new virtio-gpu-gl device option "drm=on".

I feel like using "drm" is confusing in this context because drm exists for the host and guest. What about "native-context" or even "context=[opengl|vulkan|wayland|native]"?

The terminology in the specification is "capset". Multiple capabilities can be enabled at a time (e.g., DRM and VENUS) and we don't have a means to express that with one property.

The best option I came up with is to strip the VIRTIO_GPU_ prefix from definitions in the specification. i.e.,:

VIRTIO_GPU_CAPSET_VIRGL -> capset-virgl VIRTIO_GPU_CAPSET_VIRGL2 -> capset-virgl2 VIRTIO_GPU_CAPSET_GFXSTREAM -> capset-gfxstream VIRTIO_GPU_CAPSET_GFXSTREAM_VULKAN -> capset-gfxstream-vulkan VIRTIO_GPU_CAPSET_VENUS -> capset-venus VIRTIO_GPU_CAPSET_CROSS_DOMAIN -> capset-cross-domain VIRTIO_GPU_CAPSET_DRM -> capset-drm

Quoted 144 lines

The GPU command line is already getting complex so now might be the time to rationalise it.

As an aside can mesa build the intel drivers on non-x86 systems as now I could potentially pass my native intel context to my emulated aarch64 guests?

Quoted 135 lines

Unlike Virgl and Venus contexts that operate on application API level, DRM native contexts work on a kernel UAPI level. This lower level results in a lightweight context implementations that yield better performance.

---
  docs/system/devices/virtio-gpu.rst | 11 +++++++++++
  hw/display/virtio-gpu-gl.c         |  2 ++
  hw/display/virtio-gpu-virgl.c      | 22 ++++++++++++++++++++++
  hw/display/virtio-gpu.c            | 15 +++++++++++++++
  include/hw/virtio/virtio-gpu.h     |  3 +++
  5 files changed, 53 insertions(+)
docs/system/devices/virtio-gpu.rst +11 -0
index b7eb0fc0e727..49a75138f7ef 100644
@@ -82,6 +82,17 @@ of virtio-gpu host memory window. This is typically between 256M and 8G.
 
  .. _venus: https://gitlab.freedesktop.org/virgl/venus-protocol/
 
+DRM native context is supported since release of `virglrenderer`_ v1.0.0
+using `drm`_ protocol. ``DRM`` virtio-gpu capability set ("capset") requires
+host blob support (``hostmem`` and ``blob`` fields) and should be enabled
+using ``drm`` field. The ``hostmem`` field specifies the size of virtio-gpu
+host memory window. This is typically between 256M and 8G.
+
+.. parsed-literal::
+    -device virtio-gpu-gl,hostmem=8G,blob=on,drm=on
+
+.. _drm: https://gitlab.freedesktop.org/virgl/virglrenderer/-/tree/main/src/drm
+
  virtio-gpu rutabaga
  -------------------
 
hw/display/virtio-gpu-gl.c +2 -0
index 53d938f23f20..bd0c0692a5c4 100644
@@ -159,6 +159,8 @@ static Property virtio_gpu_gl_properties[] = {
                      VIRTIO_GPU_FLAG_STATS_ENABLED, false),
      DEFINE_PROP_BIT("venus", VirtIOGPU, parent_obj.conf.flags,
                      VIRTIO_GPU_FLAG_VENUS_ENABLED, false),
+    DEFINE_PROP_BIT("drm", VirtIOGPU, parent_obj.conf.flags,
+                    VIRTIO_GPU_FLAG_DRM_ENABLED, false),
      DEFINE_PROP_END_OF_LIST(),
  };
 
hw/display/virtio-gpu-virgl.c +22 -0
index 37b40e258398..89ca1878fb7c 100644
@@ -1232,6 +1232,19 @@ int virtio_gpu_virgl_init(VirtIOGPU *g)
      if (virtio_gpu_venus_enabled(g->parent_obj.conf)) {
          flags |= VIRGL_RENDERER_VENUS | VIRGL_RENDERER_RENDER_SERVER;
      }
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        flags |= VIRGL_RENDERER_DRM;
+
+        if (!gl->context_fence_enabled) {
+            /*
+             * Virglrenderer skips enabling DRM context support without
+             * enabled async-fence feature. VirtIO-GPU will initialize
+             * successfully, but DRM context won't be available in guest.
+             */
+            error_report("DRM native context requires EGL display");
+            return -EINVAL;
+        }
+    }
  #endif
 
      ret = virgl_renderer_init(g, flags, &virtio_gpu_3d_cbs);
@@ -1294,5 +1307,14 @@ GArray *virtio_gpu_virgl_get_capsets(VirtIOGPU *g)
          }
      }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+        virgl_renderer_get_cap_set(VIRTIO_GPU_CAPSET_DRM,
+                                   &capset_max_ver,
+                                   &capset_max_size);
+        if (capset_max_size) {
+            virtio_gpu_virgl_add_capset(capset_ids, VIRTIO_GPU_CAPSET_DRM);
+        }
+    }
+
      return capset_ids;
  }
hw/display/virtio-gpu.c +15 -0
index c0570ef8565a..c1acafe6246b 100644
@@ -1492,6 +1492,21 @@ void virtio_gpu_device_realize(DeviceState *qdev, Error **errp)
  #endif
      }
 
+    if (virtio_gpu_drm_enabled(g->parent_obj.conf)) {
+#ifdef VIRGL_VERSION_MAJOR
+    #if VIRGL_VERSION_MAJOR >= 1
+        if (!virtio_gpu_blob_enabled(g->parent_obj.conf) ||
+            !virtio_gpu_hostmem_enabled(g->parent_obj.conf)) {
+            error_setg(errp, "drm requires enabled blob and hostmem options");
+            return;
+        }
+    #else
+        error_setg(errp, "old virglrenderer, drm unsupported");
+        return;
+    #endif
+#endif
+    }
+
      if (!virtio_gpu_base_device_realize(qdev,
                                          virtio_gpu_handle_ctrl_cb,
                                          virtio_gpu_handle_cursor_cb,
include/hw/virtio/virtio-gpu.h +3 -0
index 99cc6286f473..f1799fcb6eee 100644
@@ -98,6 +98,7 @@ enum virtio_gpu_base_conf_flags {
      VIRTIO_GPU_FLAG_CONTEXT_INIT_ENABLED,
      VIRTIO_GPU_FLAG_RUTABAGA_ENABLED,
      VIRTIO_GPU_FLAG_VENUS_ENABLED,
+    VIRTIO_GPU_FLAG_DRM_ENABLED,
  };
 
  #define virtio_gpu_virgl_enabled(_cfg) \
@@ -118,6 +119,8 @@ enum virtio_gpu_base_conf_flags {
      (_cfg.hostmem > 0)
  #define virtio_gpu_venus_enabled(_cfg) \
      (_cfg.flags & (1 << VIRTIO_GPU_FLAG_VENUS_ENABLED))
+#define virtio_gpu_drm_enabled(_cfg) \
+    (_cfg.flags & (1 << VIRTIO_GPU_FLAG_DRM_ENABLED))
 
  struct virtio_gpu_base_conf {
      uint32_t max_outputs;
#19 lore raw

Re: [PATCH v3 6/6] virtio-gpu: Support DRM native context

Quoted 12 lines · Akihiko Odaki
Quoted 4 lines

I feel like using "drm" is confusing in this context because drm exists for the host and guest. What about "native-context" or even "context=[opengl|vulkan|wayland|native]"?

The terminology in the specification is "capset". Multiple capabilities can be enabled at a time (e.g., DRM and VENUS) and we don't have a means to express that with one property.

The best option I came up with is to strip the VIRTIO_GPU_ prefix from definitions in the specification. i.e.,:

Quoted 7 lines

VIRTIO_GPU_CAPSET_VIRGL -> capset-virgl VIRTIO_GPU_CAPSET_VIRGL2 -> capset-virgl2 VIRTIO_GPU_CAPSET_GFXSTREAM -> capset-gfxstream VIRTIO_GPU_CAPSET_GFXSTREAM_VULKAN -> capset-gfxstream-vulkan VIRTIO_GPU_CAPSET_VENUS -> capset-venus VIRTIO_GPU_CAPSET_CROSS_DOMAIN -> capset-cross-domain VIRTIO_GPU_CAPSET_DRM -> capset-drm

Reasonable, thanks for the suggestion.

Signature · 2 lines
Best regards,
Dmitry