2 * Copyright (C) 2013-2016 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/fence.h>
21 #include "msm_fence.h"
24 struct msm_fence_context *
25 msm_fence_context_alloc(struct drm_device *dev, const char *name)
27 struct msm_fence_context *fctx;
29 fctx = kzalloc(sizeof(*fctx), GFP_KERNEL);
31 return ERR_PTR(-ENOMEM);
35 fctx->context = fence_context_alloc(1);
36 init_waitqueue_head(&fctx->event);
37 spin_lock_init(&fctx->spinlock);
42 void msm_fence_context_free(struct msm_fence_context *fctx)
47 static inline bool fence_completed(struct msm_fence_context *fctx, uint32_t fence)
49 return (int32_t)(fctx->completed_fence - fence) >= 0;
52 /* legacy path for WAIT_FENCE ioctl: */
53 int msm_wait_fence(struct msm_fence_context *fctx, uint32_t fence,
54 ktime_t *timeout, bool interruptible)
58 if (fence > fctx->last_fence) {
59 DRM_ERROR_RATELIMITED("%s: waiting on invalid fence: %u (of %u)\n",
60 fctx->name, fence, fctx->last_fence);
66 ret = fence_completed(fctx, fence) ? 0 : -EBUSY;
68 unsigned long remaining_jiffies = timeout_to_jiffies(timeout);
71 ret = wait_event_interruptible_timeout(fctx->event,
72 fence_completed(fctx, fence),
75 ret = wait_event_timeout(fctx->event,
76 fence_completed(fctx, fence),
80 DBG("timeout waiting for fence: %u (completed: %u)",
81 fence, fctx->completed_fence);
83 } else if (ret != -ERESTARTSYS) {
91 /* called from workqueue */
92 void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
94 spin_lock(&fctx->spinlock);
95 fctx->completed_fence = max(fence, fctx->completed_fence);
96 spin_unlock(&fctx->spinlock);
98 wake_up_all(&fctx->event);
102 struct msm_fence_context *fctx;
106 static inline struct msm_fence *to_msm_fence(struct fence *fence)
108 return container_of(fence, struct msm_fence, base);
111 static const char *msm_fence_get_driver_name(struct fence *fence)
116 static const char *msm_fence_get_timeline_name(struct fence *fence)
118 struct msm_fence *f = to_msm_fence(fence);
119 return f->fctx->name;
122 static bool msm_fence_enable_signaling(struct fence *fence)
127 static bool msm_fence_signaled(struct fence *fence)
129 struct msm_fence *f = to_msm_fence(fence);
130 return fence_completed(f->fctx, f->base.seqno);
133 static void msm_fence_release(struct fence *fence)
135 struct msm_fence *f = to_msm_fence(fence);
136 kfree_rcu(f, base.rcu);
139 static const struct fence_ops msm_fence_ops = {
140 .get_driver_name = msm_fence_get_driver_name,
141 .get_timeline_name = msm_fence_get_timeline_name,
142 .enable_signaling = msm_fence_enable_signaling,
143 .signaled = msm_fence_signaled,
144 .wait = fence_default_wait,
145 .release = msm_fence_release,
149 msm_fence_alloc(struct msm_fence_context *fctx)
153 f = kzalloc(sizeof(*f), GFP_KERNEL);
155 return ERR_PTR(-ENOMEM);
159 fence_init(&f->base, &msm_fence_ops, &fctx->spinlock,
160 fctx->context, ++fctx->last_fence);