1bf215546Sopenharmony_ciVirtio-GPU Venus 2bf215546Sopenharmony_ci================ 3bf215546Sopenharmony_ci 4bf215546Sopenharmony_ciVenus is a Virtio-GPU protocol for Vulkan command serialization. The protocol 5bf215546Sopenharmony_cidefinition and codegen are hosted at `venus-protocol 6bf215546Sopenharmony_ci<https://gitlab.freedesktop.org/olv/venus-protocol>`__. The renderer is 7bf215546Sopenharmony_cihosted at `virglrenderer 8bf215546Sopenharmony_ci<https://gitlab.freedesktop.org/virgl/virglrenderer>`__. 9bf215546Sopenharmony_ci 10bf215546Sopenharmony_ciThe protocol is still under development. This driver and the renderer are 11bf215546Sopenharmony_ciboth considered experimental. 12bf215546Sopenharmony_ci 13bf215546Sopenharmony_ciRequirements 14bf215546Sopenharmony_ci------------ 15bf215546Sopenharmony_ci 16bf215546Sopenharmony_ciThe Venus renderer requires 17bf215546Sopenharmony_ci 18bf215546Sopenharmony_ci- Vulkan 1.1 19bf215546Sopenharmony_ci- ``VK_EXT_external_memory_dma_buf`` 20bf215546Sopenharmony_ci- ``VK_EXT_image_drm_format_modifier`` 21bf215546Sopenharmony_ci- ``VK_EXT_queue_family_foreign`` 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_cifrom the host driver. However, it violates the spec in some places currently 24bf215546Sopenharmony_ciand also relies on implementation-defined behaviors in others. It is not 25bf215546Sopenharmony_ciexpected to work on all drivers meeting the requirements. It has only been 26bf215546Sopenharmony_citested with 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci- ANV 21.1 or later 29bf215546Sopenharmony_ci- RADV 21.1 or later (the host kernel must have 30bf215546Sopenharmony_ci ``CONFIG_TRANSPARENT_HUGEPAGE`` disabled because of this `KVM issue 31bf215546Sopenharmony_ci <https://github.com/google/security-research/security/advisories/GHSA-7wq5-phmq-m584>`__) 32bf215546Sopenharmony_ci- TURNIP 22.0 or later 33bf215546Sopenharmony_ci- Mali r32p0 or later 34bf215546Sopenharmony_ci 35bf215546Sopenharmony_ciThe Venus driver requires supports for 36bf215546Sopenharmony_ci 37bf215546Sopenharmony_ci- ``VIRTGPU_PARAM_RESOURCE_BLOB`` 38bf215546Sopenharmony_ci- ``VIRTGPU_PARAM_HOST_VISIBLE`` 39bf215546Sopenharmony_ci- ``VIRTGPU_PARAM_CROSS_DEVICE`` 40bf215546Sopenharmony_ci- ``VIRTGPU_PARAM_CONTEXT_INIT`` 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_cifrom the virtio-gpu kernel driver, unless vtest is used. That usually means 43bf215546Sopenharmony_cithe guest kernel should be at least 5.16 or have the parameters back ported, 44bf215546Sopenharmony_cipaired with hypervisors such as `crosvm 45bf215546Sopenharmony_ci<https://chromium.googlesource.com/chromiumos/platform/crosvm>`__, or `patched 46bf215546Sopenharmony_ciqemu 47bf215546Sopenharmony_ci<https://www.collabora.com/news-and-blog/blog/2021/11/26/venus-on-qemu-enabling-new-virtual-vulkan-driver/>`__. 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_civtest 50bf215546Sopenharmony_ci----- 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ciThe simplest way to test Venus is to use virglrenderer's vtest server. To 53bf215546Sopenharmony_cibuild virglrenderer with Venus support and to start the vtest server, 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci.. code-block:: console 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci $ git clone https://gitlab.freedesktop.org/virgl/virglrenderer.git 58bf215546Sopenharmony_ci $ cd virglrenderer 59bf215546Sopenharmony_ci $ meson out -Dvenus-experimental=true 60bf215546Sopenharmony_ci $ ninja -C out 61bf215546Sopenharmony_ci $ ./out/vtest/virgl_test_server --venus 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ciIn another shell, 64bf215546Sopenharmony_ci 65bf215546Sopenharmony_ci.. code-block:: console 66bf215546Sopenharmony_ci 67bf215546Sopenharmony_ci $ export VK_ICD_FILENAMES=<path-to-virtio_icd.x86_64.json> 68bf215546Sopenharmony_ci $ export VN_DEBUG=vtest 69bf215546Sopenharmony_ci $ vulkaninfo 70bf215546Sopenharmony_ci $ vkcube 71bf215546Sopenharmony_ci 72bf215546Sopenharmony_ciIf the host driver of the system is not new enough, it is a good idea to build 73bf215546Sopenharmony_cithe host driver as well when building the Venus driver. Just remember to set 74bf215546Sopenharmony_ci:envvar:`VK_ICD_FILENAMES` when starting the vtest server so that the vtest 75bf215546Sopenharmony_ciserver finds the locally built host driver. 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ciVirtio-GPU 78bf215546Sopenharmony_ci---------- 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ciThe driver requires ``VIRTGPU_PARAM_CONTEXT_INIT`` from the virtio-gpu kernel 81bf215546Sopenharmony_cidriver, which was upstreamed in kernel 5.16. 82bf215546Sopenharmony_ci 83bf215546Sopenharmony_cicrosvm is written in Rust. To build crosvm, make sure Rust has been installed 84bf215546Sopenharmony_ciand 85bf215546Sopenharmony_ci 86bf215546Sopenharmony_ci.. code-block:: console 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci $ git clone --recurse-submodules \ 89bf215546Sopenharmony_ci https://chromium.googlesource.com/chromiumos/platform/crosvm 90bf215546Sopenharmony_ci $ cd crosvm 91bf215546Sopenharmony_ci $ RUSTFLAGS="-L<path-to-virglrenderer>/out/src" cargo build \ 92bf215546Sopenharmony_ci --features "x wl-dmabuf virgl_renderer virgl_renderer_next default-no-sandbox" 93bf215546Sopenharmony_ci 94bf215546Sopenharmony_ciNote that crosvm must be built with ``default-no-sandbox`` or started with 95bf215546Sopenharmony_ci``--disable-sandbox`` in this setup. 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ciThis is how one might want to start crosvm 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci.. code-block:: console 100bf215546Sopenharmony_ci 101bf215546Sopenharmony_ci $ sudo LD_LIBRARY_PATH=<...> VK_ICD_FILENAMES=<...> ./target/debug/crosvm run \ 102bf215546Sopenharmony_ci --gpu vulkan=true \ 103bf215546Sopenharmony_ci --display-window-keyboard \ 104bf215546Sopenharmony_ci --display-window-mouse \ 105bf215546Sopenharmony_ci --host_ip 192.168.0.1 \ 106bf215546Sopenharmony_ci --netmask 255.255.255.0 \ 107bf215546Sopenharmony_ci --mac 12:34:56:78:9a:bc \ 108bf215546Sopenharmony_ci --rwdisk disk.img \ 109bf215546Sopenharmony_ci -p root=/dev/vda1 \ 110bf215546Sopenharmony_ci <path-to-bzImage> 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ciassuming a working system is installed to partition 1 of ``disk.img``. 113bf215546Sopenharmony_ci``sudo`` or ``CAP_NET_ADMIN`` is needed to set up the TAP network device. 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ciVirtio-GPU and Virtio-WL 116bf215546Sopenharmony_ci------------------------ 117bf215546Sopenharmony_ci 118bf215546Sopenharmony_ciIn this setup, the guest userspace uses Xwayland and a special Wayland 119bf215546Sopenharmony_cicompositor to connect guest X11/Wayland clients to the host Wayland 120bf215546Sopenharmony_cicompositor, using Virtio-WL as the transport. This setup is more tedious, but 121bf215546Sopenharmony_cithat should hopefully change over time. 122bf215546Sopenharmony_ci 123bf215546Sopenharmony_ciFor now, the guest kernel must be built from the ``chromeos-5.10`` branch of 124bf215546Sopenharmony_cithe `Chrome OS kernel 125bf215546Sopenharmony_ci<https://chromium.googlesource.com/chromiumos/third_party/kernel>`__. 126bf215546Sopenharmony_ci 127bf215546Sopenharmony_ciTo build minigbm and to enable minigbm support in virglrenderer, 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci.. code-block:: console 130bf215546Sopenharmony_ci 131bf215546Sopenharmony_ci $ git clone https://chromium.googlesource.com/chromiumos/platform/minigbm 132bf215546Sopenharmony_ci $ cd minigbm 133bf215546Sopenharmony_ci $ CFLAGS=-DDRV_<I915-or-your-driver> OUT=out DESTDIR=out/install make install 134bf215546Sopenharmony_ci $ cd ../virglrenderer 135bf215546Sopenharmony_ci $ meson configure out -Dminigbm_allocation=true 136bf215546Sopenharmony_ci $ ninja -C out 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ciMake sure a host Wayland compositor is running. Replace 139bf215546Sopenharmony_ci``--display-window-keyboard --display-window-mouse`` by 140bf215546Sopenharmony_ci``--wayland-sock=<path-to-wayland-socket>`` when starting crosvm. 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ciIn the guest, build and start sommelier, the special Wayland compositor, 143bf215546Sopenharmony_ci 144bf215546Sopenharmony_ci.. code-block:: console 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci $ git clone https://chromium.googlesource.com/chromiumos/platform2 147bf215546Sopenharmony_ci $ cd platform2/vm_tools/sommelier 148bf215546Sopenharmony_ci $ meson out -Dxwayland_path=/usr/bin/Xwayland -Dxwayland_gl_driver_path=/usr/lib/dri 149bf215546Sopenharmony_ci $ ninja -C out 150bf215546Sopenharmony_ci $ sudo chmod 777 /dev/wl0 151bf215546Sopenharmony_ci $ ./out/sommelier -X --glamor 152bf215546Sopenharmony_ci --xwayland-gl-driver-path=<path-to-locally-built-gl-driver> \ 153bf215546Sopenharmony_ci sleep infinity 154bf215546Sopenharmony_ci 155bf215546Sopenharmony_ciOptional Requirements 156bf215546Sopenharmony_ci--------------------- 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_ciWhen virglrenderer is built with ``-Dminigbm_allocation=true``, the Venus 159bf215546Sopenharmony_cirenderer might need to import GBM BOs. The imports will fail unless the host 160bf215546Sopenharmony_cidriver supports the formats, especially multi-planar ones, and the DRM format 161bf215546Sopenharmony_cimodifiers of the GBM BOs. 162bf215546Sopenharmony_ci 163bf215546Sopenharmony_ciIn the future, if virglrenderer's ``virgl_renderer_export_fence`` is 164bf215546Sopenharmony_cisupported, the Venus renderer will require ``VK_KHR_external_fence_fd`` with 165bf215546Sopenharmony_ci``VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT`` from the host driver. 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ciVK_MEMORY_PROPERTY_HOST_VISIBLE_BIT 168bf215546Sopenharmony_ci----------------------------------- 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_ciThe Venus renderer makes assumptions about ``VkDeviceMemory`` that has 171bf215546Sopenharmony_ci``VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT``. The assumptions are illegal and rely 172bf215546Sopenharmony_cion the current behaviors of the host drivers. It should be possible to remove 173bf215546Sopenharmony_cisome of the assumptions and incrementally improve compatibilities with more 174bf215546Sopenharmony_cihost drivers by imposing platform-specific requirements. But the long-term 175bf215546Sopenharmony_ciplan is to create a new Vulkan extension for the host drivers to address this 176bf215546Sopenharmony_cispecific use case. 177bf215546Sopenharmony_ci 178bf215546Sopenharmony_ciThe Venus renderer assumes a device memory that has 179bf215546Sopenharmony_ci``VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT`` can be exported as a mmapable dma-buf 180bf215546Sopenharmony_ci(in the future, the plan is to export the device memory as an opaque fd). It 181bf215546Sopenharmony_cichains ``VkExportMemoryAllocateInfo`` to ``VkMemoryAllocateInfo`` without 182bf215546Sopenharmony_cichecking if the host driver can export the device memory. 183bf215546Sopenharmony_ci 184bf215546Sopenharmony_ciThe dma-buf is mapped (in the future, the plan is to import the opaque fd and 185bf215546Sopenharmony_cicall ``vkMapMemory``) but the mapping is not accessed. Instead, the mapping 186bf215546Sopenharmony_ciis passed to ``KVM_SET_USER_MEMORY_REGION``. The hypervisor, host KVM, and 187bf215546Sopenharmony_cithe guest kernel work together to set up a write-back or write-combined guest 188bf215546Sopenharmony_cimapping (see ``virtio_gpu_vram_mmap`` of the virtio-gpu kernel driver). CPU 189bf215546Sopenharmony_ciaccesses to the device memory are via the guest mapping, and are assumed to be 190bf215546Sopenharmony_cicoherent when the device memory also has 191bf215546Sopenharmony_ci``VK_MEMORY_PROPERTY_HOST_COHERENT_BIT``. 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_ciWhile the Venus renderer can force a ``VkDeviceMemory`` external, it does not 194bf215546Sopenharmony_ciforce a ``VkImage`` or a ``VkBuffer`` external. As a result, it can bind an 195bf215546Sopenharmony_ciexternal device memory to a non-external resource. 196