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, volatile uint32_t *fenceptr,
17 struct msm_fence_context *fctx;
20 fctx = kzalloc(sizeof(*fctx), GFP_KERNEL);
22 return ERR_PTR(-ENOMEM);
25 strncpy(fctx->name, name, sizeof(fctx->name));
26 fctx->context = dma_fence_context_alloc(1);
27 fctx->index = index++;
28 fctx->fenceptr = fenceptr;
29 spin_lock_init(&fctx->spinlock);
32 * Start out close to the 32b fence rollover point, so we can
33 * catch bugs with fence comparisons.
35 fctx->last_fence = 0xffffff00;
36 fctx->completed_fence = fctx->last_fence;
37 *fctx->fenceptr = fctx->last_fence;
42 void msm_fence_context_free(struct msm_fence_context *fctx)
47 bool msm_fence_completed(struct msm_fence_context *fctx, uint32_t fence)
50 * Note: Check completed_fence first, as fenceptr is in a write-combine
51 * mapping, so it will be more expensive to read.
53 return (int32_t)(fctx->completed_fence - fence) >= 0 ||
54 (int32_t)(*fctx->fenceptr - fence) >= 0;
57 /* called from irq handler and workqueue (in recover path) */
58 void msm_update_fence(struct msm_fence_context *fctx, uint32_t fence)
62 spin_lock_irqsave(&fctx->spinlock, flags);
63 if (fence_after(fence, fctx->completed_fence))
64 fctx->completed_fence = fence;
65 spin_unlock_irqrestore(&fctx->spinlock, flags);
69 struct dma_fence base;
70 struct msm_fence_context *fctx;
73 static inline struct msm_fence *to_msm_fence(struct dma_fence *fence)
75 return container_of(fence, struct msm_fence, base);
78 static const char *msm_fence_get_driver_name(struct dma_fence *fence)
83 static const char *msm_fence_get_timeline_name(struct dma_fence *fence)
85 struct msm_fence *f = to_msm_fence(fence);
89 static bool msm_fence_signaled(struct dma_fence *fence)
91 struct msm_fence *f = to_msm_fence(fence);
92 return msm_fence_completed(f->fctx, f->base.seqno);
95 static const struct dma_fence_ops msm_fence_ops = {
96 .get_driver_name = msm_fence_get_driver_name,
97 .get_timeline_name = msm_fence_get_timeline_name,
98 .signaled = msm_fence_signaled,
102 msm_fence_alloc(struct msm_fence_context *fctx)
106 f = kzalloc(sizeof(*f), GFP_KERNEL);
108 return ERR_PTR(-ENOMEM);
112 dma_fence_init(&f->base, &msm_fence_ops, &fctx->spinlock,
113 fctx->context, ++fctx->last_fence);