Skip to contentSkip to conversation

drm/virtio: Import scanout buffers from other devices

PATCH Superseded Vivek Kasireddy 2 participants 21 messages Aug 13, 2024 – Nov 25, 2024

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v2 cover drm/virtio: Import scanout buffers from other devices

Having virtio-gpu import scanout buffers (via prime) from other devices means that we'd be adding a head to headless GPUs assigned to a Guest VM or additional heads to regular GPU devices that are passthrough'd to the Guest. In these cases, the Guest compositor can render into the scanout buffer using a primary GPU and has the secondary GPU (virtio-gpu) import it for display purposes.

The main advantage with this is that the imported scanout buffer can either be displayed locally on the Host (e.g, using Qemu + GTK UI) or encoded and streamed to a remote client (e.g, Qemu + Spice UI). Note that since Qemu uses udmabuf driver, there would be no copies made of the scanout buffer as it is displayed. This should be possible even when it might reside in device memory such has VRAM.

The specific use-case that can be supported with this series is when running Weston or other guest compositors with "additional-devices" feature (./weston --drm-device=card1 --additional-devices=card0). More info about this feature can be found at:
https://gitlab.freedesktop.org/wayland/weston/-/merge_requests/736

In the above scenario, card1 could be a dGPU or an iGPU and card0 would be virtio-gpu in KMS only mode. However, the case where this patch series could be particularly useful is when card1 is a GPU VF that needs to share its scanout buffer (in a zero-copy way) with the GPU PF on the Host. Or, it can also be useful when the scanout buffer needs to be shared between any two GPU devices (assuming one of them is assigned to a Guest VM) as long as they are P2P DMA compatible.

As part of the import, the virtio-gpu driver shares the dma addresses and lengths with Qemu which then determines whether the memory region they belong to is owned by a VFIO device or whether it is part of the Guest's system ram. If it is the former, it uses the VFIO_DEVICE_FEATURE_DMA_BUF feature flag while invoking the ioctl against the VFIO device fd and gets a dmabuf fd in return. In the latter case, Qemu obtains the dmabuf fd using the udmabuf driver.

Note that the virtio-gpu driver registers a move_notify() callback to track location changes associated with the scanout buffer and sends attach/detach backing cmds to Qemu when appropriate. And, synchronization (that is, ensuring that Guest and Host are not using the scanout buffer at the same time) is ensured by pinning/ unpinning the dmabuf as part of prepare/cleanup fb and using a fence in resource_flush cmd.

Changelog:

v1 -> v2:
- Use a fenced version of VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd (Dmitry)

RFC -> v1:
- Use virtio_gpu_cleanup_object() to cleanup the imported obj
- Do pin/unpin as part of prepare and cleanup fb for the imported dmabuf obj instead doing it as part of plane update
- Tested with gnome-shell/mutter (wayland backend)

This series is available at:
https://gitlab.freedesktop.org/Vivek/drm-tip/-/commits/virtgpu_import_v2

along with additional patches for Qemu and Spice here:
https://gitlab.freedesktop.org/Vivek/qemu/-/commits/vfio_dmabuf_2
https://gitlab.freedesktop.org/Vivek/spice/-/commits/encode_dmabuf_v8

Patchset overview:

Patch 1:   Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd
Patch 2-3: Helpers to initalize, import, free imported object
Patch 4-5: Import and use buffers from other devices for scanout

This series is tested using the following method:
- Run Qemu with the following relevant options:
qemu-system-x86_64 -m 4096m ....
-device vfio-pci,host=0000:03:00.0
-device virtio-vga,max_outputs=1,blob=true,xres=1920,yres=1080 -spice port=3001,gl=on,disable-ticketing=on,preferred-codec=gstreamer:h264 -object memory-backend-memfd,id=mem1,size=4096M
-machine memory-backend=mem1 ...
- Run upstream Weston with the following options in the Guest VM:
./weston --drm-device=card1 --additional-devices=card0
- Run gnome-shell/Mutter (wayland backend) with this additional patch:
https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3745

where card1 is a DG2 dGPU (passthrough'd and using xe driver in Guest VM), card0 is virtio-gpu and the Host is using a RPL iGPU.

Cc Gerd Hoffmann <[email protected]>
Cc Dongwon Kim <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Michal Wajdeczko <[email protected]>
Cc Michael Tretter <[email protected]>

Vivek Kasireddy (5):
drm/virtio: Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd drm/virtio: Add a helper to map and note the dma addrs and lengths drm/virtio: Add helpers to initialize and free the imported object drm/virtio: Import prime buffers from other devices as guest blobs drm/virtio: Add prepare and cleanup routines for imported dmabuf obj

 drivers/gpu/drm/virtio/virtgpu_drv.h    |  10 ++
 drivers/gpu/drm/virtio/virtgpu_object.c |  26 ++++
 drivers/gpu/drm/virtio/virtgpu_plane.c  |  71 +++++++++-
 drivers/gpu/drm/virtio/virtgpu_prime.c  | 170 +++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vq.c     |  25 ++++
 5 files changed, 300 insertions(+), 2 deletions(-)
Signature · 1 line
2.45.1
#1 lore raw

PATCH v2 1/5 drm/virtio: Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd

This cmd is useful to let the VMM (i.e, Qemu) know that the backing store associated with a resource is no longer valid, so that the VMM can perform any cleanup or unmap operations.

The fence related changes and virtio_gpu_object_detach()/ virtio_gpu_detach_object_fenced() routines are extracted from a patch by Dmitry Osipenko <[email protected]>.

Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h    |  5 +++++
 drivers/gpu/drm/virtio/virtgpu_object.c | 23 +++++++++++++++++++++++
 drivers/gpu/drm/virtio/virtgpu_vq.c     | 25 +++++++++++++++++++++++++
 3 files changed, 53 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +5 -0
index 64c236169db8..9fb09486c08c 100644
@@ -92,6 +92,7 @@ struct virtio_gpu_object {
 	uint32_t hw_res_handle;
 	bool dumb;
 	bool created;
+	bool detached;
 	bool host3d_blob, guest_blob;
 	uint32_t blob_mem, blob_flags;
 
@@ -349,6 +350,10 @@ void virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
 			      struct virtio_gpu_object *obj,
 			      struct virtio_gpu_mem_entry *ents,
 			      unsigned int nents);
+void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev,
+			      struct virtio_gpu_object *obj,
+			      struct virtio_gpu_fence *fence);
+int virtio_gpu_detach_object_fenced(struct virtio_gpu_object *bo);
 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
 			    struct virtio_gpu_output *output);
 int virtio_gpu_cmd_get_display_info(struct virtio_gpu_device *vgdev);
drivers/gpu/drm/virtio/virtgpu_object.c +23 -0
index c7e74cf13022..53bb02c8a135 100644
@@ -97,6 +97,29 @@ static void virtio_gpu_free_object(struct drm_gem_object *obj)
 	virtio_gpu_cleanup_object(bo);
 }
 
+int virtio_gpu_detach_object_fenced(struct virtio_gpu_object *bo)
+{
+	struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private;
+	struct virtio_gpu_fence *fence;
+
+	if (bo->detached)
+		return 0;
+
+	fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context, 0);
+	if (!fence)
+		return -ENOMEM;
+
+	virtio_gpu_object_detach(vgdev, bo, fence);
+	virtio_gpu_notify(vgdev);
+
+	dma_fence_wait(&fence->f, false);
+	dma_fence_put(&fence->f);
+
+	bo->detached = true;
+
+	return 0;
+}
+
 static const struct drm_gem_object_funcs virtio_gpu_shmem_funcs = {
 	.free = virtio_gpu_free_object,
 	.open = virtio_gpu_gem_object_open,
drivers/gpu/drm/virtio/virtgpu_vq.c +25 -0
index 0d3d0d09f39b..b8b296b912a1 100644
@@ -645,6 +645,23 @@ virtio_gpu_cmd_resource_attach_backing(struct virtio_gpu_device *vgdev,
 	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
 }
 
+static void
+virtio_gpu_cmd_resource_detach_backing(struct virtio_gpu_device *vgdev,
+				       uint32_t resource_id,
+				       struct virtio_gpu_fence *fence)
+{
+	struct virtio_gpu_resource_detach_backing *cmd_p;
+	struct virtio_gpu_vbuffer *vbuf;
+
+	cmd_p = virtio_gpu_alloc_cmd(vgdev, &vbuf, sizeof(*cmd_p));
+	memset(cmd_p, 0, sizeof(*cmd_p));
+
+	cmd_p->hdr.type = cpu_to_le32(VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING);
+	cmd_p->resource_id = cpu_to_le32(resource_id);
+
+	virtio_gpu_queue_fenced_ctrl_buffer(vgdev, vbuf, fence);
+}
+
 static void virtio_gpu_cmd_get_display_info_cb(struct virtio_gpu_device *vgdev,
 					       struct virtio_gpu_vbuffer *vbuf)
 {
@@ -1107,6 +1124,14 @@ void virtio_gpu_object_attach(struct virtio_gpu_device *vgdev,
 					       ents, nents, NULL);
 }
 
+void virtio_gpu_object_detach(struct virtio_gpu_device *vgdev,
+			      struct virtio_gpu_object *obj,
+			      struct virtio_gpu_fence *fence)
+{
+	virtio_gpu_cmd_resource_detach_backing(vgdev, obj->hw_res_handle,
+					       fence);
+}
+
 void virtio_gpu_cursor_ping(struct virtio_gpu_device *vgdev,
 			    struct virtio_gpu_output *output)
 {
Signature · 1 line
2.45.1
#2 lore raw

PATCH v2 2/5 drm/virtio: Add a helper to map and note the dma addrs and lengths

In reply to Vivek Kasireddy

This helper would be used when first initializing the object as part of import and also when updating the plane where we need to ensure that the imported object's backing is valid.

Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_drv.h   |  5 +++
 drivers/gpu/drm/virtio/virtgpu_prime.c | 42 ++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
drivers/gpu/drm/virtio/virtgpu_drv.h +5 -0
index 9fb09486c08c..6b3b91968298 100644
@@ -89,6 +89,7 @@ struct virtio_gpu_object_params {
 
 struct virtio_gpu_object {
 	struct drm_gem_shmem_object base;
+	struct sg_table *sgt;
 	uint32_t hw_res_handle;
 	bool dumb;
 	bool created;
@@ -473,6 +474,10 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
 struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
 	struct drm_device *dev, struct dma_buf_attachment *attach,
 	struct sg_table *sgt);
+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach);
 
 /* virtgpu_debugfs.c */
 void virtio_gpu_debugfs_init(struct drm_minor *minor);
drivers/gpu/drm/virtio/virtgpu_prime.c +42 -0

54 lines not on this page · raw message

Signature · 1 line
2.45.1
maintainer
#3 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 4 lines · Vivek Kasireddy
drivers/gpu/drm/virtio/virtgpu_prime.c +4 -0
+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 10 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +10 -0
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 10 lines · Vivek Kasireddy
Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +8 -0
+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +6 -0
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

The intention was to wait for any pending operations on the backing object to finish and let it become idle before mapping and accessing the underlying memory. But I suspect this wait may not be necessary given that we would have already called dma_buf_pin() at this point, which would have had the desired effect?

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#5 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 20 lines · Vivek

Hi Dmitry,

Quoted 10 lines · Vivek Kasireddy
Quoted 8 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +8 -0
+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +6 -0
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

Quoted 5 lines

The intention was to wait for any pending operations on the backing object to finish and let it become idle before mapping and accessing the underlying memory. But I suspect this wait may not be necessary given that we would have already called dma_buf_pin() at this point, which would have had the desired effect?

Looking at the dma_buf_map_attachment() code, I see that it does both of pinning and waiting for the fence by itself. Hence should be safe to drop both dma_buf_pin() and dma_resv_wait_timeout(), please test.

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 27 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 5 lines · Vivek

Hi Dmitry,

Quoted 3 lines · Vivek Kasireddy
Quoted 1 line

+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry

**ents,

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +7 -0
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +6 -0
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

Quoted 2 lines

The intention was to wait for any pending operations on the backing object to finish and let it become idle before mapping and accessing the

Quoted 8 lines

underlying

Quoted 3 lines

memory. But I suspect this wait may not be necessary given that we would have already called dma_buf_pin() at this point, which would have had the desired effect?

Looking at the dma_buf_map_attachment() code, I see that it does both of pinning and waiting for the fence by itself. Hence should be safe to drop both dma_buf_pin() and dma_resv_wait_timeout(), please test.

Sure, I'll retest again but it looks like dma_buf_map_attachment() pins and or waits for fence only in specific situations. That is, it pins only if the exporter is non-dynamic and if CONFIG_DMABUF_MOVE_NOTIFY is not enabled. And,
it waits for the fence only if the importer is non-dynamic. Since I have only tested with a dynamic exporter (Xe driver), I did not encounter any issues but it makes sense to augment the code to account for non-dynamic exporters as well.

Quoted 3 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it to the Guest VM. And, if there is an iGPU available on the Host, you can use GTK/SDL UI for local display or Spice UI for remote display if there is no iGPU on the Host. This is exactly how I started testing this patch series but I am now predominantly testing this series with SRIOV enabled iGPUs and dGPUs.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#7 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 37 lines · Vivek

Hi Dmitry,

Quoted 27 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 5 lines · Vivek

Hi Dmitry,

Quoted 3 lines · Vivek Kasireddy
Quoted 1 line

+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry

**ents,

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +7 -0
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
Quoted 8 lines

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 6 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +6 -0
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

Quoted 2 lines

The intention was to wait for any pending operations on the backing object to finish and let it become idle before mapping and accessing the

Quoted 8 lines

underlying

Quoted 3 lines

memory. But I suspect this wait may not be necessary given that we would have already called dma_buf_pin() at this point, which would have had the desired effect?

Looking at the dma_buf_map_attachment() code, I see that it does both of pinning and waiting for the fence by itself. Hence should be safe to drop both dma_buf_pin() and dma_resv_wait_timeout(), please test.

Quoted 6 lines

Sure, I'll retest again but it looks like dma_buf_map_attachment() pins and or waits for fence only in specific situations. That is, it pins only if the exporter is non-dynamic and if CONFIG_DMABUF_MOVE_NOTIFY is not enabled. And,
it waits for the fence only if the importer is non-dynamic. Since I have only tested with a dynamic exporter (Xe driver), I did not encounter any issues but it makes sense to augment the code to account for non-dynamic exporters as well.

If exporter or importer is not dynamic, then dma-buf core pins buffer at the buffer importing time, see dma_buf_attach(). Hence, I expect that everything should work fine.

Quoted 7 lines
Quoted 2 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it to the Guest VM. And, if there is an iGPU available on the Host, you can use GTK/SDL UI for local display or Spice UI for remote display if there is no iGPU on the Host. This is exactly how I started testing this patch series but I am now predominantly testing this series with SRIOV enabled iGPUs and dGPUs.

Was hoping to try out SR-IOV on A750 if it's even possible at all. But passthrough indeed is another an option, will try with passthrough, thanks.

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 39 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 17 lines
Quoted 17 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

+long virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry

**ents,

Quoted 15 lines
Quoted 15 lines

drivers/gpu/drm/virtio/virtgpu_prime.c struct dma_buf *virtgpu_gem_prime_export(struct drm_gem_object *obj, · line 160

Quoted 13 lines
drivers/gpu/drm/virtio/virtgpu_prime.c +13 -0
+				unsigned int *nents,
+				struct virtio_gpu_object *bo,
+				struct dma_buf_attachment *attach)
+{
+	struct scatterlist *sl;
+	struct sg_table *sgt;
+	long i, ret;
+
+	dma_resv_assert_held(attach->dmabuf->resv);
+
+	ret = dma_resv_wait_timeout(attach->dmabuf->resv,
+				    DMA_RESV_USAGE_KERNEL,
+				    false, MAX_SCHEDULE_TIMEOUT);

Why this wait is needed?

Quoted 1 line
Quoted 1 line
Quoted 1 line

The intention was to wait for any pending operations on the backing

object

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

to finish and let it become idle before mapping and accessing the

underlying

Quoted 1 line

memory. But I suspect this wait may not be necessary given that we

would

Quoted 1 line
Quoted 1 line
Quoted 1 line

have already called dma_buf_pin() at this point, which would have had

the

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

desired effect?

Looking at the dma_buf_map_attachment() code, I see that it does both

of

Quoted 4 lines
Quoted 2 lines

pinning and waiting for the fence by itself. Hence should be safe to drop both dma_buf_pin() and dma_resv_wait_timeout(), please test.

Sure, I'll retest again but it looks like dma_buf_map_attachment() pins and or waits for fence only in specific situations. That is, it pins only if the

exporter

Quoted 1 line

is non-dynamic and if CONFIG_DMABUF_MOVE_NOTIFY is not enabled.

And,

Quoted 1 line

it waits for the fence only if the importer is non-dynamic. Since I have only

Quoted 8 lines

tested

Quoted 1 line

with a dynamic exporter (Xe driver), I did not encounter any issues but it

makes

Quoted 1 line

sense to augment the code to account for non-dynamic exporters as well.

If exporter or importer is not dynamic, then dma-buf core pins buffer at the buffer importing time, see dma_buf_attach(). Hence, I expect that everything should work fine.

Given that virtio-gpu would always be a dynamic importer (in other words, dma_buf_attachment_is_dynamic() is true) as proposed in this series, the question really is about whether the exporter is dynamic or not. I tested with both Xe driver (dynamic exporter) and i915 (non-dynamic) and noted the following:

For Xe (dma_buf_is_dynamic() is true):
- dma-buf core calls pin only if CONFIG_DMABUF_MOVE_NOTIFY is not enabled, and extracts the sgt as part of dma_buf_map_attachment()
- It does not wait for the fences as part of map

For i915 (dma_buf_is_dynamic() is false):
- dma-buf core does not call pin anywhere as it is a no-op for non-dynamic exporters, but maps/extracts the sgt as part of dma_buf_dynamic_attach()
- It does not wait for fences anywhere

And, the docs mention the following rules for dynamic importers:
"DYNAMIC IMPORTER RULES:

Dynamic importers, see dma_buf_attachment_is_dynamic(), have additional constraints on how they set up fences:

Dynamic importers must obey the write fences and wait for them to signal before allowing access to the buffer’s underlying storage through the device.

Dynamic importers should set fences for any access that they can’t disable immediately from their dma_buf_attach_ops.move_notify callback."

So, I believe we need to call pin and or dma_resv_wait_timeout(resv, DMA_RESV_USAGE_WRITE,....) at some point, as part of import, given these rules.

Quoted 6 lines
Quoted 4 lines
Quoted 3 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it to

the

Quoted 1 line

Guest VM. And, if there is an iGPU available on the Host, you can use

Quoted 8 lines

GTK/SDL UI

Quoted 1 line

for local display or Spice UI for remote display if there is no iGPU on the

Host.

Quoted 1 line

This is exactly how I started testing this patch series but I am now

predominantly

Quoted 1 line

testing this series with SRIOV enabled iGPUs and dGPUs.

Was hoping to try out SR-IOV on A750 if it's even possible at all.

AFAIK, SRIOV is not supported on any versions of DG2 including A750.

Thanks,
Vivek

Quoted 5 lines

But passthrough indeed is another an option, will try with passthrough, thanks.

--
Best regards,
Dmitry

maintainer
#9 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 5 lines · Vivek
Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it to

the

Quoted 1 line

Guest VM. And, if there is an iGPU available on the Host, you can use

Quoted 8 lines
Quoted 7 lines

GTK/SDL UI

Quoted 1 line

for local display or Spice UI for remote display if there is no iGPU on the

Host.

Quoted 1 line

This is exactly how I started testing this patch series but I am now

predominantly

Quoted 1 line

testing this series with SRIOV enabled iGPUs and dGPUs.

Was hoping to try out SR-IOV on A750 if it's even possible at all.

AFAIK, SRIOV is not supported on any versions of DG2 including A750.

I'm having trouble with getting it to work.

My testing setup:

1. Passthroughed A750 that uses XE driver
2. RaptorLake iGPU on host used for virtio-gpu, uses i915 driver
3. QEMU latest master branch + your QEMU vfio_dmabuf_2 patches applied on top
4. Latest linux-next kernel on host
5. Latest linux-next kernel on guest + this v2 applied

In guest I'm running this:

  seatd-launch -- weston --drm-device=card1 --additional-devices=card0

where card1 is A750 and card0 is virtio-gpu.

I added printk's and see that virtio-gpu imports A750 dma-buf and gets XE's SGT, but nothing shows on the QEMU display. I tried both GTK and SPICE displays. If I connect HDMI display to passthroughed A750 while running weston command above, then I get weston working on the A750 HDMI display and still nothing is shown on virtio-gpu display.

I also had to force virtio-gpu driver to always probe before XE, otherwise virtio-gpu gets PCI read errors and fails to probe because it fails to detect virtio features.

Am I doing anything wrong? Suggestions are welcome.

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 15 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 11 lines · Vivek
Quoted 11 lines
Quoted 3 lines
Quoted 2 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it to

the

Quoted 1 line

Guest VM. And, if there is an iGPU available on the Host, you can use

GTK/SDL UI

Quoted 1 line

for local display or Spice UI for remote display if there is no iGPU on the

Host.

Quoted 1 line

This is exactly how I started testing this patch series but I am now

predominantly

Quoted 1 line

testing this series with SRIOV enabled iGPUs and dGPUs.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Was hoping to try out SR-IOV on A750 if it's even possible at all.

AFAIK, SRIOV is not supported on any versions of DG2 including A750.

I'm having trouble with getting it to work.

My testing setup:

1. Passthroughed A750 that uses XE driver

I tested successfully with both i915 and Xe but DG2 is officially supported
only with i915. Xe officially supports LNL and newer platforms. For older platforms, Mesa throws the following warning:
MESA: warning: Support for this platform is experimental with Xe KMD, bug reports may be ignored.

Quoted 1 line

2. RaptorLake iGPU on host used for virtio-gpu, uses i915 driver

I am testing with a similar setup: RPL-P iGPU + DG2

Quoted 2 lines

3. QEMU latest master branch + your QEMU vfio_dmabuf_2 patches applied on top

I just tried with latest Qemu master and it appears to be very sluggish in my environment (Fedora 39 Guest and Fedora 39 Host). But, few months old
Qemu master + vfio_dmabuf_2 patches (actually, these patches are not needed
to test this patch series with GTK/SDL) seems to work OK for me. And, here are
the relevant Qemu launch options I am using:
qemu-system-x86_64 -m 4096m -enable-kvm -cpu host,host-phys-bits=on,host-phys-bits-limit=39.... -device vfio-pci,host=0000:03:00.0 -device virtio-vga,max_outputs=1,xres=1920,yres=1080,blob=true -display gtk,gl=on -object memory-backend-memfd,id=mem1,size=4096M -machine memory-backend=mem1.......

Quoted 2 lines

4. Latest linux-next kernel on host
5. Latest linux-next kernel on guest + this v2 applied

I just tested successfully with today's drm-tip (6.12) + v2 of this series.

Quoted 9 lines

In guest I'm running this:

  seatd-launch -- weston --drm-device=card1 --additional-devices=card0

where card1 is A750 and card0 is virtio-gpu.

I added printk's and see that virtio-gpu imports A750 dma-buf and gets XE's SGT, but nothing shows on the QEMU display.

Could you please try this test again with i915 instead of Xe? Also, are there any errors seen in Guest or Host logs?

Quoted 1 line

I tried both GTK and SPICE displays.

GTK should be simpler given that there are other components (Gstreamer, etc) needed for SPICE that have their own set of dependencies.

Quoted 4 lines

If I connect HDMI display to passthroughed A750 while running weston command above, then I get weston working on the A750 HDMI
display and still nothing is shown on virtio-gpu display.

I tried this exact same test and it seems to work OK for me. Could you please also test Gnome + Wayland:
XDG_SESSION_TYPE=wayland dbus-run-session -- /usr/bin/gnome-shell --wayland --no-x11 &

Note that, for Weston, the primary device (drm-device=) needs to support KMS and have at-least one connector connected. For Gnome + Wayland, there is no such requirement as the primary device can work with dummy backend and be used only for rendering. Therefore, to test Gnome with DG2 in Guest, I typically do modprobe i915 enable_guc=3 disable_display=true

Quoted 4 lines

I also had to force virtio-gpu driver to always probe before XE, otherwise virtio-gpu gets PCI read errors and fails to probe because it fails to detect virtio features.

That is concerning. I guess we'll need to investigate how to fix this properly. As part of my tests, I typically blacklist Xe and i915 and load them later after booting to the cmd line and before launching the compositors.

Quoted 2 lines

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to recreate the issue you are seeing.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

#11 lore raw

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 31 lines
Quoted 7 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 3 lines · Vivek
Quoted 3 lines
Quoted 3 lines
Quoted 2 lines

BTW, is any DG2 GPU suitable for testing of this patchset? Will I be able to test it using a regular consumer A750 card?

Yes, you can test with any DG2 dGPU as long as you can passthrough it

to

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

the

Quoted 1 line

Guest VM. And, if there is an iGPU available on the Host, you can use

GTK/SDL UI

Quoted 1 line

for local display or Spice UI for remote display if there is no iGPU on the

Host.

Quoted 1 line

This is exactly how I started testing this patch series but I am now

predominantly

Quoted 1 line

testing this series with SRIOV enabled iGPUs and dGPUs.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

Was hoping to try out SR-IOV on A750 if it's even possible at all.

AFAIK, SRIOV is not supported on any versions of DG2 including A750.

I'm having trouble with getting it to work.

My testing setup:

1. Passthroughed A750 that uses XE driver

I tested successfully with both i915 and Xe but DG2 is officially supported only with i915. Xe officially supports LNL and newer platforms. For older platforms,
Mesa throws the following warning:
MESA: warning: Support for this platform is experimental with Xe KMD, bug reports may be ignored.

Quoted 7 lines
Quoted 1 line

2. RaptorLake iGPU on host used for virtio-gpu, uses i915 driver

I am testing with a similar setup: RPL-P iGPU + DG2

Quoted 1 line

3. QEMU latest master branch + your QEMU vfio_dmabuf_2 patches

applied

Quoted 1 line

on top

I just tried with latest Qemu master and it appears to be very sluggish in my

Turns out, the sluggishness is the result of a busy loop triggered by this patch in Qemu:
commit 640f9149c3dcafbfeb495a10f6105c6f71f24d1d

Author: Dmitry Osipenko <[email protected]>
Date:   Fri Oct 25 00:03:08 2024 +0300

virtio-gpu: Support suspension of commands processing

Check whether command processing has been finished; otherwise, stop processing commands and retry the command again next time. This allows us to support asynchronous execution of non-fenced commands needed for unmapping host blobs safely.

The above patch exposes an issue that occurs in very specific cases that make use of single FB (such as fbcon and dirtyfb) where a fence is not added to the (resource_flush) cmd. However, this issue can be fixed in the Guest virtio drm driver by including the following patch:
commit cef51cbd7ac70d225eaa0d4d47e6e29f66ebcedf

Author: Dongwon Kim <[email protected]>
Date:   Mon Oct 21 02:08:03 2024 +0300

drm/virtio: New fence for every plane update

Having a fence linked to a virtio_gpu_framebuffer in the plane update sequence would cause conflict when several planes referencing the same framebuffer (e.g. Xorg screen covering multi-displays configured for an extended mode) and those planes are updated concurrently. So it is needed to allocate a fence for every plane state instead of the framebuffer.

After rebasing v2 of this patch series on top of the above patch, I see that this use-case works as expected with Qemu master. Let me send out v3, which would be a rebase of v2 on top of the above patch.

Thanks,
Vivek

Quoted 73 lines

environment (Fedora 39 Guest and Fedora 39 Host). But, few months old Qemu master + vfio_dmabuf_2 patches (actually, these patches are not needed
to test this patch series with GTK/SDL) seems to work OK for me. And, here are
the relevant Qemu launch options I am using:
qemu-system-x86_64 -m 4096m -enable-kvm -cpu host,host-phys- bits=on,host-phys-bits-limit=39....
-device vfio-pci,host=0000:03:00.0 -device virtio- vga,max_outputs=1,xres=1920,yres=1080,blob=true
-display gtk,gl=on -object memory-backend-memfd,id=mem1,size=4096M - machine memory-backend=mem1.......

Quoted 2 lines

4. Latest linux-next kernel on host
5. Latest linux-next kernel on guest + this v2 applied

I just tested successfully with today's drm-tip (6.12) + v2 of this series.

Quoted 9 lines

In guest I'm running this:

  seatd-launch -- weston --drm-device=card1 --additional-devices=card0

where card1 is A750 and card0 is virtio-gpu.

I added printk's and see that virtio-gpu imports A750 dma-buf and gets XE's SGT, but nothing shows on the QEMU display.

Could you please try this test again with i915 instead of Xe? Also, are there any errors seen in Guest or Host logs?

Quoted 1 line

I tried both GTK and SPICE displays.

GTK should be simpler given that there are other components (Gstreamer, etc)
needed for SPICE that have their own set of dependencies.

Quoted 4 lines

If I connect HDMI display to passthroughed A750 while running weston command above, then I get weston working on the A750 HDMI
display and still nothing is shown on virtio-gpu display.

I tried this exact same test and it seems to work OK for me. Could you please also test
Gnome + Wayland:
XDG_SESSION_TYPE=wayland dbus-run-session -- /usr/bin/gnome-shell -- wayland --no-x11 &

Note that, for Weston, the primary device (drm-device=) needs to support KMS and
have at-least one connector connected. For Gnome + Wayland, there is no such
requirement as the primary device can work with dummy backend and be used
only for rendering. Therefore, to test Gnome with DG2 in Guest, I typically do modprobe i915 enable_guc=3 disable_display=true

Quoted 4 lines

I also had to force virtio-gpu driver to always probe before XE, otherwise virtio-gpu gets PCI read errors and fails to probe because it fails to detect virtio features.

That is concerning. I guess we'll need to investigate how to fix this properly. As part of my tests, I typically blacklist Xe and i915 and load them later after booting to the cmd line and before launching the compositors.

Quoted 2 lines

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to recreate the issue you are seeing.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#12 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi, Vivek

On 11/19/24 09:01, Kasireddy, Vivek wrote:
...

Quoted 3 lines

After rebasing v2 of this patch series on top of the above patch, I see that this use-case works as expected with Qemu master. Let me send out v3, which would be a rebase of v2 on top of the above patch.

...

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to recreate the issue you are seeing.

Thanks a lot for sharing your Qemu command. I haven't used the host-phys-bits flags, will try again soon and then also check host errors if it still won't work.

The new fence for every plane patchset is now applied to drm-misc-next.

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 8 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

...

Quoted 3 lines

After rebasing v2 of this patch series on top of the above patch, I see that this use-case works as expected with Qemu master. Let me send out v3, which would be a rebase of v2 on top of the above patch.

...

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to recreate

the

Quoted 1 line
Quoted 1 line

issue you are seeing.

Thanks a lot for sharing your Qemu command. I haven't used the host-phys-bits flags, will try again soon and then also check host errors if it still won't work.

Any chance you would be able to finish reviewing this patch series before your upcoming vacation?

Quoted 2 lines

The new fence for every plane patchset is now applied to drm-misc-next.

Thank you,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#14 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 12 lines · Vivek

Hi Dmitry,

Quoted 10 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

...

Quoted 3 lines

After rebasing v2 of this patch series on top of the above patch, I see that this use-case works as expected with Qemu master. Let me send out v3, which would be a rebase of v2 on top of the above patch.

...

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to recreate

Quoted 8 lines
Quoted 6 lines

the

Quoted 1 line
Quoted 1 line

issue you are seeing.

Thanks a lot for sharing your Qemu command. I haven't used the host-phys-bits flags, will try again soon and then also check host errors if it still won't work.

Any chance you would be able to finish reviewing this patch series before your upcoming vacation?

Let's try, we have a week.

Good news, I got it to work with latest misc-next+v3 on guest and by using passthroughed AMD RDNA3 card instead of DG2. Haven't yet checked whether DG2 works with this guest kernel combination, maybe it will.

A bit bad news is that display flickers on each framebuffer flip (shows a black screen) using dynamic dmabuf attachment. Pinning imported dma-buf permanently fixes the flicker, i.e. dynamic dmabuf attachment doesn't work as expected. Disabling CONFIG_DMABUF_MOVE_NOTIFY helps.

There are these virtio-gpu errors on each flicker:

[drm:virtio_gpu_dequeue_ctrl_func] *ERROR* response 0x1200 (command 0x106)

Overall, patches are good to me. I'll leave couple comments on the v3. The flickering issue isn't a blocker given that MOVE_NOTIFY is considered to be experimental today, we can fix it later.

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

RE: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Hi Dmitry,

Quoted 13 lines

Subject: Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

Quoted 7 lines
Quoted 7 lines

...

Quoted 3 lines

After rebasing v2 of this patch series on top of the above patch, I see that this use-case works as expected with Qemu master. Let me send out v3, which would be a rebase of v2 on top of the above patch.

...

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Am I doing anything wrong? Suggestions are welcome.

Could you please share your Qemu launch parameters? I'll try to

recreate

Quoted 3 lines
Quoted 3 lines

the

Quoted 1 line
Quoted 1 line

issue you are seeing.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

Thanks a lot for sharing your Qemu command. I haven't used the host-phys-bits flags, will try again soon and then also check host errors if it still won't work.

Any chance you would be able to finish reviewing this patch series before your upcoming vacation?

Let's try, we have a week.

Oh, it is going to be very tight, given that it is a short working week in the U.S (Thanksgiving holidays start on Wed/Thu). But, yeah, let us try.

Quoted 4 lines

Good news, I got it to work with latest misc-next+v3 on guest and by using passthroughed AMD RDNA3 card instead of DG2. Haven't yet checked whether DG2 works with this guest kernel combination, maybe it will.

Yeah, DG2 should work as well with latest drm-tip and Qemu master.

Quoted 5 lines

A bit bad news is that display flickers on each framebuffer flip (shows a black screen) using dynamic dmabuf attachment. Pinning imported dma-buf permanently fixes the flicker, i.e. dynamic dmabuf attachment doesn't work as expected. Disabling CONFIG_DMABUF_MOVE_NOTIFY helps.

I'd like to get this case (dynamic dmabuf attachment) to work successfully as well. If it is not too much trouble, could you please try to test with DG2 to see if this flickering issue happens there as well? Since I have access to a DG2 card, I'd like to recreate this flickering issue in my test environment and try to fix it.

Quoted 9 lines

There are these virtio-gpu errors on each flicker:

[drm:virtio_gpu_dequeue_ctrl_func] *ERROR* response 0x1200 (command 0x106)

Overall, patches are good to me. I'll leave couple comments on the v3. The flickering issue isn't a blocker given that MOVE_NOTIFY is considered to be experimental today, we can fix it later.

Ok, I'll try to address your comments and send out a v4 soon.

Thanks,
Vivek

Quoted 4 lines

--
Best regards,
Dmitry

maintainer
#16 lore raw

Re: [PATCH v2 2/5] drm/virtio: Add a helper to map and note the dma addrs and lengths

In reply to Kasireddy, Vivek
Quoted 3 lines · Vivek

So, I believe we need to call pin and or dma_resv_wait_timeout(resv, DMA_RESV_USAGE_WRITE,....) at some point, as part of import, given these rules.

On a second look, you should be right. That resv-wait should be needed.

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

PATCH v2 3/5 drm/virtio: Add helpers to initialize and free the imported object

In reply to Vivek Kasireddy

The imported object can be considered a guest blob resource; therefore, we use create_blob cmd while creating it. These helpers are used in the next patch which does the actual import.

Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_object.c |  3 ++
 drivers/gpu/drm/virtio/virtgpu_prime.c  | 71 +++++++++++++++++++++++++
 2 files changed, 74 insertions(+)
drivers/gpu/drm/virtio/virtgpu_object.c +3 -0
index 53bb02c8a135..2b19bb8c6ec3 100644
@@ -80,6 +80,9 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
 		drm_gem_free_mmap_offset(&vram->base.base.base);
 		drm_gem_object_release(&vram->base.base.base);
 		kfree(vram);
+	} else {
+		drm_gem_object_release(&bo->base.base);
+		kfree(bo);
 	}
 }
 
drivers/gpu/drm/virtio/virtgpu_prime.c +71 -0

77 lines not on this page · raw message

Signature · 1 line
2.45.1
#18 lore raw

PATCH v2 4/5 drm/virtio: Import prime buffers from other devices as guest blobs

In reply to Vivek Kasireddy

By importing scanout buffers from other devices, we should be able to use the virtio-gpu driver in KMS only mode. Note that we attach dynamically and register a move_notify() callback so that we can let the VMM know of any location changes associated with the backing store of the imported object by sending detach_backing cmd.

Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_prime.c | 57 +++++++++++++++++++++++++-
 1 file changed, 56 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_prime.c +56 -1
index e9b5b5a7f8dc..2fcccfa75eab 100644
@@ -255,10 +255,39 @@ static int virtgpu_dma_buf_init_obj(struct drm_device *dev,
 	return ret;
 }
 
+static const struct drm_gem_object_funcs virtgpu_gem_dma_buf_funcs = {
+	.free = virtgpu_dma_buf_free_obj,
+};
+
+static void virtgpu_dma_buf_move_notify(struct dma_buf_attachment *attach)
+{
+	struct drm_gem_object *obj = attach->importer_priv;
+	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
+
+	if (bo->created) {
+		virtio_gpu_detach_object_fenced(bo);
+
+		if (bo->sgt)
+			dma_buf_unmap_attachment(attach, bo->sgt,
+						 DMA_BIDIRECTIONAL);
+
+		bo->sgt = NULL;
+	}
+}
+
+static const struct dma_buf_attach_ops virtgpu_dma_buf_attach_ops = {
+	.allow_peer2peer = true,
+	.move_notify = virtgpu_dma_buf_move_notify
+};
+
 struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
 						struct dma_buf *buf)
 {
+	struct virtio_gpu_device *vgdev = dev->dev_private;
+	struct dma_buf_attachment *attach;
+	struct virtio_gpu_object *bo;
 	struct drm_gem_object *obj;
+	int ret;
 
 	if (buf->ops == &virtgpu_dmabuf_ops.ops) {
 		obj = buf->priv;
@@ -272,7 +301,32 @@ struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
 		}
 	}
 
-	return drm_gem_prime_import(dev, buf);
+	if (!vgdev->has_resource_blob || vgdev->has_virgl_3d)
+		return drm_gem_prime_import(dev, buf);
+
+	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
+	if (!bo)
+		return ERR_PTR(-ENOMEM);
+
+	obj = &bo->base.base;
+	obj->funcs = &virtgpu_gem_dma_buf_funcs;
+	drm_gem_private_object_init(dev, obj, buf->size);
+
+	attach = dma_buf_dynamic_attach(buf, dev->dev,
+					&virtgpu_dma_buf_attach_ops, obj);
+	if (IS_ERR(attach)) {
+		kfree(bo);
+		return ERR_CAST(attach);
+	}
+
+	obj->import_attach = attach;
+	get_dma_buf(buf);
+
+	ret = virtgpu_dma_buf_init_obj(dev, bo, attach);
+	if (ret < 0)
+		return ERR_PTR(ret);
+
+	return obj;
 }
 
 struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
@@ -281,3 +335,4 @@ struct drm_gem_object *virtgpu_gem_prime_import_sg_table(
 {
 	return ERR_PTR(-ENODEV);
 }
+
Signature · 1 line
2.45.1
#19 lore raw

PATCH v2 5/5 drm/virtio: Add prepare and cleanup routines for imported dmabuf obj

In reply to Vivek Kasireddy

When an imported dmabuf obj is used as part of an atomic commit, we need to pin it as part of prepare and unpin it during cleanup of the associated FB, to make sure that it does not move until the commit is completed (and also while it is being used on the Host).

Cc Gerd Hoffmann <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
---
 drivers/gpu/drm/virtio/virtgpu_plane.c | 71 +++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)
drivers/gpu/drm/virtio/virtgpu_plane.c +70 -1
index a72a2dbda031..5ab0741b67cd 100644
@@ -25,7 +25,9 @@
 
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_damage_helper.h>
+#include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_fourcc.h>
+#include <linux/virtio_dma_buf.h>
 
 #include "virtgpu_drv.h"
 
@@ -241,6 +243,48 @@ static void virtio_gpu_primary_plane_update(struct drm_plane *plane,
 				  rect.y2 - rect.y1);
 }
 
+static int virtio_gpu_prepare_imported_obj(struct drm_plane *plane,
+					   struct drm_plane_state *new_state,
+					   struct drm_gem_object *obj)
+{
+	struct virtio_gpu_device *vgdev = plane->dev->dev_private;
+	struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
+	struct dma_buf_attachment *attach = obj->import_attach;
+	struct dma_resv *resv = attach->dmabuf->resv;
+	struct virtio_gpu_mem_entry *ents = NULL;
+	unsigned int nents;
+	int ret;
+
+	dma_resv_lock(resv, NULL);
+
+	ret = dma_buf_pin(attach);
+	if (ret) {
+		dma_resv_unlock(resv);
+		return ret;
+	}
+
+	ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
+	if (ret)
+		goto err;
+
+	if (!bo->sgt) {
+		ret = virtgpu_dma_buf_import_sgt(&ents, &nents,
+						 bo, attach);
+		if (ret)
+			goto err;
+
+		virtio_gpu_object_attach(vgdev, bo, ents, nents);
+	}
+
+	dma_resv_unlock(resv);
+	return 0;
+
+err:
+	dma_buf_unpin(attach);
+	dma_resv_unlock(resv);
+	return ret;
+}
+
 static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 				       struct drm_plane_state *new_state)
 {
@@ -248,6 +292,8 @@ static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 	struct virtio_gpu_device *vgdev = dev->dev_private;
 	struct virtio_gpu_framebuffer *vgfb;
 	struct virtio_gpu_object *bo;
+	struct drm_gem_object *obj;
+	int ret;
 
 	if (!new_state->fb)
 		return 0;
@@ -257,7 +303,15 @@ static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 	if (!bo || (plane->type == DRM_PLANE_TYPE_PRIMARY && !bo->guest_blob))
 		return 0;
 
-	if (bo->dumb && (plane->state->fb != new_state->fb)) {
+	obj = new_state->fb->obj[0];
+	if (obj->import_attach) {
+		ret = virtio_gpu_prepare_imported_obj(plane, new_state, obj);
+		if (ret)
+			return ret;
+	}
+
+	if ((bo->dumb || obj->import_attach) &&
+	    (plane->state->fb != new_state->fb)) {
 		vgfb->fence = virtio_gpu_fence_alloc(vgdev, vgdev->fence_drv.context,
 						     0);
 		if (!vgfb->fence)
@@ -267,10 +321,21 @@ static int virtio_gpu_plane_prepare_fb(struct drm_plane *plane,
 	return 0;
 }
 
+static void virtio_gpu_cleanup_imported_obj(struct drm_gem_object *obj)
+{
+	struct dma_buf_attachment *attach = obj->import_attach;
+	struct dma_resv *resv = attach->dmabuf->resv;
+
+	dma_resv_lock(resv, NULL);
+	dma_buf_unpin(attach);
+	dma_resv_unlock(resv);
+}
+
 static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
 					struct drm_plane_state *state)
 {
 	struct virtio_gpu_framebuffer *vgfb;
+	struct drm_gem_object *obj;
 
 	if (!state->fb)
 		return;
@@ -280,6 +345,10 @@ static void virtio_gpu_plane_cleanup_fb(struct drm_plane *plane,
 		dma_fence_put(&vgfb->fence->f);
 		vgfb->fence = NULL;
 	}
+
+	obj = state->fb->obj[0];
+	if (obj->import_attach)
+		virtio_gpu_cleanup_imported_obj(obj);
 }
 
 static void virtio_gpu_cursor_plane_update(struct drm_plane *plane,
Signature · 1 line
2.45.1
#20 lore raw
In reply to Vivek Kasireddy

Hi Dmitry,

Quoted 47 lines

Subject: [PATCH v2 0/5] drm/virtio: Import scanout buffers from other devices

Having virtio-gpu import scanout buffers (via prime) from other devices means that we'd be adding a head to headless GPUs assigned to a Guest VM or additional heads to regular GPU devices that are passthrough'd to the Guest. In these cases, the Guest compositor can render into the scanout buffer using a primary GPU and has the secondary GPU (virtio-gpu) import it for display purposes.

The main advantage with this is that the imported scanout buffer can either be displayed locally on the Host (e.g, using Qemu + GTK UI) or encoded and streamed to a remote client (e.g, Qemu + Spice UI). Note that since Qemu uses udmabuf driver, there would be no copies made of the scanout buffer as it is displayed. This should be possible even when it might reside in device memory such has VRAM.

The specific use-case that can be supported with this series is when running Weston or other guest compositors with "additional-devices" feature (./weston --drm-device=card1 --additional-devices=card0). More info about this feature can be found at:
https://gitlab.freedesktop.org/wayland/weston/-/merge_requests/736

In the above scenario, card1 could be a dGPU or an iGPU and card0 would be virtio-gpu in KMS only mode. However, the case where this patch series could be particularly useful is when card1 is a GPU VF that needs to share its scanout buffer (in a zero-copy way) with the GPU PF on the Host. Or, it can also be useful when the scanout buffer needs to be shared between any two GPU devices (assuming one of them is assigned to a Guest VM) as long as they are P2P DMA compatible.

As part of the import, the virtio-gpu driver shares the dma addresses and lengths with Qemu which then determines whether the memory region they belong to is owned by a VFIO device or whether it is part of the Guest's system ram. If it is the former, it uses the VFIO_DEVICE_FEATURE_DMA_BUF feature flag while invoking the ioctl against the VFIO device fd and gets a dmabuf fd in return. In the latter case, Qemu obtains the dmabuf fd using the udmabuf driver.

Note that the virtio-gpu driver registers a move_notify() callback to track location changes associated with the scanout buffer and sends attach/detach backing cmds to Qemu when appropriate. And, synchronization (that is, ensuring that Guest and Host are not using the scanout buffer at the same time) is ensured by pinning/ unpinning the dmabuf as part of prepare/cleanup fb and using a fence in resource_flush cmd.

Quoted 6 lines

Changelog:

v1 -> v2:
- Use a fenced version of VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd
(Dmitry)

Do you have any further comments or suggestions on this series?

Thanks,
Vivek

Quoted 66 lines

RFC -> v1:
- Use virtio_gpu_cleanup_object() to cleanup the imported obj
- Do pin/unpin as part of prepare and cleanup fb for the imported dmabuf obj instead doing it as part of plane update
- Tested with gnome-shell/mutter (wayland backend)

This series is available at:
https://gitlab.freedesktop.org/Vivek/drm-tip/-/commits/virtgpu_import_v2

along with additional patches for Qemu and Spice here:
https://gitlab.freedesktop.org/Vivek/qemu/-/commits/vfio_dmabuf_2
https://gitlab.freedesktop.org/Vivek/spice/-/commits/encode_dmabuf_v8

Patchset overview:

Patch 1:   Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING cmd
Patch 2-3: Helpers to initalize, import, free imported object
Patch 4-5: Import and use buffers from other devices for scanout

This series is tested using the following method:
- Run Qemu with the following relevant options:
qemu-system-x86_64 -m 4096m ....
-device vfio-pci,host=0000:03:00.0
-device virtio-vga,max_outputs=1,blob=true,xres=1920,yres=1080 -spice port=3001,gl=on,disable-ticketing=on,preferred-
codec=gstreamer:h264
-object memory-backend-memfd,id=mem1,size=4096M
-machine memory-backend=mem1 ...
- Run upstream Weston with the following options in the Guest VM:
./weston --drm-device=card1 --additional-devices=card0
- Run gnome-shell/Mutter (wayland backend) with this additional patch:
https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3745

where card1 is a DG2 dGPU (passthrough'd and using xe driver in Guest VM), card0 is virtio-gpu and the Host is using a RPL iGPU.

Cc Gerd Hoffmann <[email protected]>
Cc Dongwon Kim <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc Christian Koenig <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Cc Rob Clark <[email protected]>
Cc Gurchetan Singh <[email protected]>
Cc Chia-I Wu <[email protected]>
Cc Thomas Hellström <[email protected]>
Cc Michal Wajdeczko <[email protected]>
Cc Michael Tretter <[email protected]>

Vivek Kasireddy (5):
drm/virtio: Implement VIRTIO_GPU_CMD_RESOURCE_DETACH_BACKING
cmd
drm/virtio: Add a helper to map and note the dma addrs and lengths drm/virtio: Add helpers to initialize and free the imported object drm/virtio: Import prime buffers from other devices as guest blobs drm/virtio: Add prepare and cleanup routines for imported dmabuf obj

 drivers/gpu/drm/virtio/virtgpu_drv.h    |  10 ++
 drivers/gpu/drm/virtio/virtgpu_object.c |  26 ++++
 drivers/gpu/drm/virtio/virtgpu_plane.c  |  71 +++++++++-
 drivers/gpu/drm/virtio/virtgpu_prime.c  | 170 +++++++++++++++++++++++-
 drivers/gpu/drm/virtio/virtgpu_vq.c     |  25 ++++
 5 files changed, 300 insertions(+), 2 deletions(-)

--
2.45.1