1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013-2016 Red Hat
7 #include <linux/dma-fence.h>
10 #include "msm_fence.h"
13 struct msm_fence_context *
14 msm_fence_context_alloc(struct drm_device *dev, const char *name)
16 struct msm_fence_context *fctx;
18 fctx = kzalloc(sizeof(*fctx), GFP_KERNEL);
20 return ERR_PTR(-ENOMEM);
23 strncpy(fctx->name, name, sizeof(fctx->name));
24 fctx->context = dma_fence_context_alloc(1);
25 init_waitqueue_head(&fctx->event);
26 spin_lock_init(&fctx->spinlock);
31 void msm_fence_context_free(struct msm_fence_context *fctx)
36 static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fence)
38 return (int32_t)(fctx->completed_fence - fence) >= 0;
41 /* legacy path for WAIT_FENCE ioctl: */
42 int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
43 ktime_t *timeout, bool interruptible)
47 if (fence > fctx->last_fence) {
48 DRM_ERROR("%s: waiting on invalid fence: %u (of %u)\n",
49 fctx->name, fence, fctx->last_fence);
55 ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
57 unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
60 ret = wait_event_interruptible_timeout(fctx->event,
61 fence_completed(fctx, fence),
64 ret = wait_event_timeout(fctx->event,
65 fence_completed(fctx, fence),
69 DBG("timeout waiting for fence: %u (completed: %u)",
70 fence, fctx->completed_fence);
72 } else if (ret != -ERESTARTSYS) {
80 /* called from workqueue */
81 void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
83 spin_lock(&fctx->spinlock);
84 fctx->completed_fence = max(fence, fctx->completed_fence);
85 spin_unlock(&fctx->spinlock);
87 wake_up_all(&fctx->event);
91 struct dma_fence base;
92 struct msm_fence_context *fctx;
95 static inline struct msm_fence *to_msm_fence(struct dma_fence *fence)
97 return container_of(fence, struct msm_fence, base);
100 static const char *msm_fence_get_driver_name(struct dma_fence *fence)
105 static const char *msm_fence_get_timeline_name(struct dma_fence *fence)
107 struct msm_fence *f = to_msm_fence(fence);
108 return f->fctx->name;
111 static bool msm_fence_signaled(struct dma_fence *fence)
113 struct msm_fence *f = to_msm_fence(fence);
114 return fence_completed(f->fctx, f->base.seqno);
117 static const struct dma_fence_ops msm_fence_ops = {
118 .get_driver_name = msm_fence_get_driver_name,
119 .get_timeline_name = msm_fence_get_timeline_name,
120 .signaled = msm_fence_signaled,
124 msm_fence_alloc(struct msm_fence_context *fctx)
128 f = kzalloc(sizeof(*f), GFP_KERNEL);
130 return ERR_PTR(-ENOMEM);
134 dma_fence_init(&f->base, &msm_fence_ops, &fctx->spinlock,
135 fctx->context, ++fctx->last_fence);