Skip to content

Commit 2885e57

Browse files
vivekkreddydigetx
authored andcommitted
drm/virtio: Add helpers to initialize and free the imported object
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]> Signed-off-by: Vivek Kasireddy <[email protected]> Tested-by: Dmitry Osipenko <[email protected]> Reviewed-by: Dmitry Osipenko <[email protected]> Signed-off-by: Dmitry Osipenko <[email protected]> Link: https://patchwork.freedesktop.org/patch/msgid/[email protected]
1 parent 25c3fd1 commit 2885e57

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

drivers/gpu/drm/virtio/virtgpu_object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ void virtio_gpu_cleanup_object(struct virtio_gpu_object *bo)
8080
drm_gem_free_mmap_offset(&vram->base.base.base);
8181
drm_gem_object_release(&vram->base.base.base);
8282
kfree(vram);
83+
} else {
84+
drm_gem_object_release(&bo->base.base);
85+
kfree(bo);
8386
}
8487
}
8588

drivers/gpu/drm/virtio/virtgpu_prime.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,81 @@ int virtgpu_dma_buf_import_sgt(struct virtio_gpu_mem_entry **ents,
184184
return 0;
185185
}
186186

187+
static void virtgpu_dma_buf_free_obj(struct drm_gem_object *obj)
188+
{
189+
struct virtio_gpu_object *bo = gem_to_virtio_gpu_obj(obj);
190+
struct virtio_gpu_device *vgdev = obj->dev->dev_private;
191+
struct dma_buf_attachment *attach = obj->import_attach;
192+
193+
if (attach) {
194+
virtio_gpu_detach_object_fenced(bo);
195+
196+
if (bo->sgt)
197+
dma_buf_unmap_attachment_unlocked(attach, bo->sgt,
198+
DMA_BIDIRECTIONAL);
199+
200+
dma_buf_detach(attach->dmabuf, attach);
201+
dma_buf_put(attach->dmabuf);
202+
}
203+
204+
if (bo->created) {
205+
virtio_gpu_cmd_unref_resource(vgdev, bo);
206+
virtio_gpu_notify(vgdev);
207+
return;
208+
}
209+
virtio_gpu_cleanup_object(bo);
210+
}
211+
212+
static int virtgpu_dma_buf_init_obj(struct drm_device *dev,
213+
struct virtio_gpu_object *bo,
214+
struct dma_buf_attachment *attach)
215+
{
216+
struct virtio_gpu_device *vgdev = dev->dev_private;
217+
struct virtio_gpu_object_params params = { 0 };
218+
struct dma_resv *resv = attach->dmabuf->resv;
219+
struct virtio_gpu_mem_entry *ents = NULL;
220+
unsigned int nents;
221+
int ret;
222+
223+
ret = virtio_gpu_resource_id_get(vgdev, &bo->hw_res_handle);
224+
if (ret) {
225+
virtgpu_dma_buf_free_obj(&bo->base.base);
226+
return ret;
227+
}
228+
229+
dma_resv_lock(resv, NULL);
230+
231+
ret = dma_buf_pin(attach);
232+
if (ret)
233+
goto err_pin;
234+
235+
ret = virtgpu_dma_buf_import_sgt(&ents, &nents, bo, attach);
236+
if (ret)
237+
goto err_import;
238+
239+
params.blob = true;
240+
params.blob_mem = VIRTGPU_BLOB_MEM_GUEST;
241+
params.blob_flags = VIRTGPU_BLOB_FLAG_USE_SHAREABLE;
242+
params.size = attach->dmabuf->size;
243+
244+
virtio_gpu_cmd_resource_create_blob(vgdev, bo, &params,
245+
ents, nents);
246+
bo->guest_blob = true;
247+
bo->attached = true;
248+
249+
dma_buf_unpin(attach);
250+
dma_resv_unlock(resv);
251+
252+
return 0;
253+
254+
err_import:
255+
dma_buf_unpin(attach);
256+
err_pin:
257+
dma_resv_unlock(resv);
258+
virtgpu_dma_buf_free_obj(&bo->base.base);
259+
return ret;
260+
}
261+
187262
struct drm_gem_object *virtgpu_gem_prime_import(struct drm_device *dev,
188263
struct dma_buf *buf)
189264
{

0 commit comments

Comments
 (0)