1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2015 Red Hat, Inc.
11 #include <linux/dma-fence-unwrap.h>
12 #include <linux/file.h>
13 #include <linux/sync_file.h>
14 #include <linux/uaccess.h>
16 #include <drm/drm_file.h>
17 #include <drm/drm_syncobj.h>
18 #include <drm/virtgpu_drm.h>
20 #include "virtgpu_drv.h"
22 struct virtio_gpu_submit_post_dep {
23 struct drm_syncobj *syncobj;
24 struct dma_fence_chain *chain;
28 struct virtio_gpu_submit {
29 struct virtio_gpu_submit_post_dep *post_deps;
30 unsigned int num_out_syncobjs;
32 struct drm_syncobj **in_syncobjs;
33 unsigned int num_in_syncobjs;
35 struct virtio_gpu_object_array *buflist;
36 struct drm_virtgpu_execbuffer *exbuf;
37 struct virtio_gpu_fence *out_fence;
38 struct virtio_gpu_fpriv *vfpriv;
39 struct virtio_gpu_device *vgdev;
40 struct sync_file *sync_file;
41 struct drm_file *file;
48 static int virtio_gpu_do_fence_wait(struct virtio_gpu_submit *submit,
49 struct dma_fence *in_fence)
51 u32 context = submit->fence_ctx + submit->ring_idx;
53 if (dma_fence_match_context(in_fence, context))
56 return dma_fence_wait(in_fence, true);
59 static int virtio_gpu_dma_fence_wait(struct virtio_gpu_submit *submit,
60 struct dma_fence *fence)
62 struct dma_fence_unwrap itr;
66 dma_fence_unwrap_for_each(f, &itr, fence) {
67 err = virtio_gpu_do_fence_wait(submit, f);
75 static void virtio_gpu_free_syncobjs(struct drm_syncobj **syncobjs,
82 drm_syncobj_put(syncobjs[i]);
89 virtio_gpu_parse_deps(struct virtio_gpu_submit *submit)
91 struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
92 struct drm_virtgpu_execbuffer_syncobj syncobj_desc;
93 size_t syncobj_stride = exbuf->syncobj_stride;
94 u32 num_in_syncobjs = exbuf->num_in_syncobjs;
95 struct drm_syncobj **syncobjs;
102 * kvalloc at first tries to allocate memory using kmalloc and
103 * falls back to vmalloc only on failure. It also uses __GFP_NOWARN
104 * internally for allocations larger than a page size, preventing
105 * storm of KMSG warnings.
107 syncobjs = kvcalloc(num_in_syncobjs, sizeof(*syncobjs), GFP_KERNEL);
111 for (i = 0; i < num_in_syncobjs; i++) {
112 u64 address = exbuf->in_syncobjs + i * syncobj_stride;
113 struct dma_fence *fence;
115 memset(&syncobj_desc, 0, sizeof(syncobj_desc));
117 if (copy_from_user(&syncobj_desc,
118 u64_to_user_ptr(address),
119 min(syncobj_stride, sizeof(syncobj_desc)))) {
124 if (syncobj_desc.flags & ~VIRTGPU_EXECBUF_SYNCOBJ_FLAGS) {
129 ret = drm_syncobj_find_fence(submit->file, syncobj_desc.handle,
130 syncobj_desc.point, 0, &fence);
134 ret = virtio_gpu_dma_fence_wait(submit, fence);
136 dma_fence_put(fence);
140 if (syncobj_desc.flags & VIRTGPU_EXECBUF_SYNCOBJ_RESET) {
141 syncobjs[i] = drm_syncobj_find(submit->file,
142 syncobj_desc.handle);
151 virtio_gpu_free_syncobjs(syncobjs, i);
155 submit->num_in_syncobjs = num_in_syncobjs;
156 submit->in_syncobjs = syncobjs;
161 static void virtio_gpu_reset_syncobjs(struct drm_syncobj **syncobjs,
166 for (i = 0; i < nr_syncobjs; i++) {
168 drm_syncobj_replace_fence(syncobjs[i], NULL);
173 virtio_gpu_free_post_deps(struct virtio_gpu_submit_post_dep *post_deps,
179 kfree(post_deps[i].chain);
180 drm_syncobj_put(post_deps[i].syncobj);
186 static int virtio_gpu_parse_post_deps(struct virtio_gpu_submit *submit)
188 struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
189 struct drm_virtgpu_execbuffer_syncobj syncobj_desc;
190 struct virtio_gpu_submit_post_dep *post_deps;
191 u32 num_out_syncobjs = exbuf->num_out_syncobjs;
192 size_t syncobj_stride = exbuf->syncobj_stride;
195 if (!num_out_syncobjs)
198 post_deps = kvcalloc(num_out_syncobjs, sizeof(*post_deps), GFP_KERNEL);
202 for (i = 0; i < num_out_syncobjs; i++) {
203 u64 address = exbuf->out_syncobjs + i * syncobj_stride;
205 memset(&syncobj_desc, 0, sizeof(syncobj_desc));
207 if (copy_from_user(&syncobj_desc,
208 u64_to_user_ptr(address),
209 min(syncobj_stride, sizeof(syncobj_desc)))) {
214 post_deps[i].point = syncobj_desc.point;
216 if (syncobj_desc.flags) {
221 if (syncobj_desc.point) {
222 post_deps[i].chain = dma_fence_chain_alloc();
223 if (!post_deps[i].chain) {
229 post_deps[i].syncobj = drm_syncobj_find(submit->file,
230 syncobj_desc.handle);
231 if (!post_deps[i].syncobj) {
232 kfree(post_deps[i].chain);
239 virtio_gpu_free_post_deps(post_deps, i);
243 submit->num_out_syncobjs = num_out_syncobjs;
244 submit->post_deps = post_deps;
250 virtio_gpu_process_post_deps(struct virtio_gpu_submit *submit)
252 struct virtio_gpu_submit_post_dep *post_deps = submit->post_deps;
255 struct dma_fence *fence = &submit->out_fence->f;
258 for (i = 0; i < submit->num_out_syncobjs; i++) {
259 if (post_deps[i].chain) {
260 drm_syncobj_add_point(post_deps[i].syncobj,
262 fence, post_deps[i].point);
263 post_deps[i].chain = NULL;
265 drm_syncobj_replace_fence(post_deps[i].syncobj,
272 static int virtio_gpu_fence_event_create(struct drm_device *dev,
273 struct drm_file *file,
274 struct virtio_gpu_fence *fence,
277 struct virtio_gpu_fence_event *e = NULL;
280 e = kzalloc(sizeof(*e), GFP_KERNEL);
284 e->event.type = VIRTGPU_EVENT_FENCE_SIGNALED;
285 e->event.length = sizeof(e->event);
287 ret = drm_event_reserve_init(dev, file, &e->base, &e->event);
298 static int virtio_gpu_init_submit_buflist(struct virtio_gpu_submit *submit)
300 struct drm_virtgpu_execbuffer *exbuf = submit->exbuf;
303 if (!exbuf->num_bo_handles)
306 bo_handles = kvmalloc_array(exbuf->num_bo_handles, sizeof(*bo_handles),
311 if (copy_from_user(bo_handles, u64_to_user_ptr(exbuf->bo_handles),
312 exbuf->num_bo_handles * sizeof(*bo_handles))) {
317 submit->buflist = virtio_gpu_array_from_handles(submit->file, bo_handles,
318 exbuf->num_bo_handles);
319 if (!submit->buflist) {
329 static void virtio_gpu_cleanup_submit(struct virtio_gpu_submit *submit)
331 virtio_gpu_reset_syncobjs(submit->in_syncobjs, submit->num_in_syncobjs);
332 virtio_gpu_free_syncobjs(submit->in_syncobjs, submit->num_in_syncobjs);
333 virtio_gpu_free_post_deps(submit->post_deps, submit->num_out_syncobjs);
335 if (!IS_ERR(submit->buf))
339 virtio_gpu_array_put_free(submit->buflist);
341 if (submit->out_fence_fd >= 0)
342 put_unused_fd(submit->out_fence_fd);
344 if (submit->out_fence)
345 dma_fence_put(&submit->out_fence->f);
347 if (submit->sync_file)
348 fput(submit->sync_file->file);
351 static void virtio_gpu_submit(struct virtio_gpu_submit *submit)
353 virtio_gpu_cmd_submit(submit->vgdev, submit->buf, submit->exbuf->size,
354 submit->vfpriv->ctx_id, submit->buflist,
356 virtio_gpu_notify(submit->vgdev);
359 static void virtio_gpu_complete_submit(struct virtio_gpu_submit *submit)
362 submit->buflist = NULL;
363 submit->sync_file = NULL;
364 submit->out_fence_fd = -1;
367 static int virtio_gpu_init_submit(struct virtio_gpu_submit *submit,
368 struct drm_virtgpu_execbuffer *exbuf,
369 struct drm_device *dev,
370 struct drm_file *file,
371 u64 fence_ctx, u32 ring_idx)
373 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
374 struct virtio_gpu_device *vgdev = dev->dev_private;
375 struct virtio_gpu_fence *out_fence;
376 bool drm_fence_event;
379 memset(submit, 0, sizeof(*submit));
381 if ((exbuf->flags & VIRTGPU_EXECBUF_RING_IDX) &&
382 (vfpriv->ring_idx_mask & BIT_ULL(ring_idx)))
383 drm_fence_event = true;
385 drm_fence_event = false;
387 if ((exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT) ||
388 exbuf->num_out_syncobjs ||
389 exbuf->num_bo_handles ||
391 out_fence = virtio_gpu_fence_alloc(vgdev, fence_ctx, ring_idx);
395 if (drm_fence_event) {
396 err = virtio_gpu_fence_event_create(dev, file, out_fence, ring_idx);
398 dma_fence_put(&out_fence->f);
403 submit->out_fence = out_fence;
404 submit->fence_ctx = fence_ctx;
405 submit->ring_idx = ring_idx;
406 submit->out_fence_fd = -1;
407 submit->vfpriv = vfpriv;
408 submit->vgdev = vgdev;
409 submit->exbuf = exbuf;
412 err = virtio_gpu_init_submit_buflist(submit);
416 submit->buf = vmemdup_user(u64_to_user_ptr(exbuf->command), exbuf->size);
417 if (IS_ERR(submit->buf))
418 return PTR_ERR(submit->buf);
420 if (exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT) {
421 err = get_unused_fd_flags(O_CLOEXEC);
425 submit->out_fence_fd = err;
427 submit->sync_file = sync_file_create(&out_fence->f);
428 if (!submit->sync_file)
435 static int virtio_gpu_wait_in_fence(struct virtio_gpu_submit *submit)
439 if (submit->exbuf->flags & VIRTGPU_EXECBUF_FENCE_FD_IN) {
440 struct dma_fence *in_fence =
441 sync_file_get_fence(submit->exbuf->fence_fd);
446 * Wait if the fence is from a foreign context, or if the
447 * fence array contains any fence from a foreign context.
449 ret = virtio_gpu_dma_fence_wait(submit, in_fence);
451 dma_fence_put(in_fence);
457 static void virtio_gpu_install_out_fence_fd(struct virtio_gpu_submit *submit)
459 if (submit->sync_file) {
460 submit->exbuf->fence_fd = submit->out_fence_fd;
461 fd_install(submit->out_fence_fd, submit->sync_file->file);
465 static int virtio_gpu_lock_buflist(struct virtio_gpu_submit *submit)
468 return virtio_gpu_array_lock_resv(submit->buflist);
473 int virtio_gpu_execbuffer_ioctl(struct drm_device *dev, void *data,
474 struct drm_file *file)
476 struct virtio_gpu_device *vgdev = dev->dev_private;
477 struct virtio_gpu_fpriv *vfpriv = file->driver_priv;
478 u64 fence_ctx = vgdev->fence_drv.context;
479 struct drm_virtgpu_execbuffer *exbuf = data;
480 struct virtio_gpu_submit submit;
484 if (!vgdev->has_virgl_3d)
487 if (exbuf->flags & ~VIRTGPU_EXECBUF_FLAGS)
490 if (exbuf->flags & VIRTGPU_EXECBUF_RING_IDX) {
491 if (exbuf->ring_idx >= vfpriv->num_rings)
494 if (!vfpriv->base_fence_ctx)
497 fence_ctx = vfpriv->base_fence_ctx;
498 ring_idx = exbuf->ring_idx;
501 virtio_gpu_create_context(dev, file);
503 ret = virtio_gpu_init_submit(&submit, exbuf, dev, file,
504 fence_ctx, ring_idx);
508 ret = virtio_gpu_parse_post_deps(&submit);
512 ret = virtio_gpu_parse_deps(&submit);
517 * Await in-fences in the end of the job submission path to
518 * optimize the path by proceeding directly to the submission
519 * to virtio after the waits.
521 ret = virtio_gpu_wait_in_fence(&submit);
525 ret = virtio_gpu_lock_buflist(&submit);
529 virtio_gpu_submit(&submit);
532 * Set up usr-out data after submitting the job to optimize
533 * the job submission path.
535 virtio_gpu_install_out_fence_fd(&submit);
536 virtio_gpu_process_post_deps(&submit);
537 virtio_gpu_complete_submit(&submit);
539 virtio_gpu_cleanup_submit(&submit);