1 /* $NetBSD: vgem_fence.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $ */
2
3 /*
4 * Copyright 2016 Intel Corporation
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software")
8 * to deal in the software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * them Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <sys/cdefs.h>
26 __KERNEL_RCSID(0, "$NetBSD: vgem_fence.c,v 1.3 2021/12/19 11:06:55 riastradh Exp $");
27
28 #include <linux/dma-buf.h>
29 #include <linux/dma-resv.h>
30
31 #include <drm/drm_file.h>
32
33 #include "vgem_drv.h"
34
35 #define VGEM_FENCE_TIMEOUT (10*HZ)
36
37 struct vgem_fence {
38 struct dma_fence base;
39 struct spinlock lock;
40 struct timer_list timer;
41 };
42
vgem_fence_get_driver_name(struct dma_fence * fence)43 static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
44 {
45 return "vgem";
46 }
47
vgem_fence_get_timeline_name(struct dma_fence * fence)48 static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
49 {
50 return "unbound";
51 }
52
vgem_fence_release(struct dma_fence * base)53 static void vgem_fence_release(struct dma_fence *base)
54 {
55 struct vgem_fence *fence = container_of(base, typeof(*fence), base);
56
57 del_timer_sync(&fence->timer);
58 dma_fence_free(&fence->base);
59 }
60
vgem_fence_value_str(struct dma_fence * fence,char * str,int size)61 static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
62 {
63 snprintf(str, size, "%llu", fence->seqno);
64 }
65
vgem_fence_timeline_value_str(struct dma_fence * fence,char * str,int size)66 static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
67 int size)
68 {
69 snprintf(str, size, "%llu",
70 dma_fence_is_signaled(fence) ? fence->seqno : 0);
71 }
72
73 static const struct dma_fence_ops vgem_fence_ops = {
74 .get_driver_name = vgem_fence_get_driver_name,
75 .get_timeline_name = vgem_fence_get_timeline_name,
76 .release = vgem_fence_release,
77
78 .fence_value_str = vgem_fence_value_str,
79 .timeline_value_str = vgem_fence_timeline_value_str,
80 };
81
vgem_fence_timeout(struct timer_list * t)82 static void vgem_fence_timeout(struct timer_list *t)
83 {
84 struct vgem_fence *fence = from_timer(fence, t, timer);
85
86 dma_fence_signal(&fence->base);
87 }
88
vgem_fence_create(struct vgem_file * vfile,unsigned int flags)89 static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
90 unsigned int flags)
91 {
92 struct vgem_fence *fence;
93
94 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
95 if (!fence)
96 return NULL;
97
98 spin_lock_init(&fence->lock);
99 dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
100 dma_fence_context_alloc(1), 1);
101
102 timer_setup(&fence->timer, vgem_fence_timeout, 0);
103
104 /* We force the fence to expire within 10s to prevent driver hangs */
105 mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
106
107 return &fence->base;
108 }
109
110 /*
111 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
112 *
113 * Create and attach a fence to the vGEM handle. This fence is then exposed
114 * via the dma-buf reservation object and visible to consumers of the exported
115 * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
116 * vGEM buffer is being written to by the client and is exposed as an exclusive
117 * fence, otherwise the fence indicates the client is current reading from the
118 * buffer and all future writes should wait for the client to signal its
119 * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
120 * an exclusive fence when adding a read, or any fence when adding a write),
121 * -EBUSY is reported. Serialisation between operations should be handled
122 * by waiting upon the dma-buf.
123 *
124 * This returns the handle for the new fence that must be signaled within 10
125 * seconds (or otherwise it will automatically expire). See
126 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
127 *
128 * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
129 */
vgem_fence_attach_ioctl(struct drm_device * dev,void * data,struct drm_file * file)130 int vgem_fence_attach_ioctl(struct drm_device *dev,
131 void *data,
132 struct drm_file *file)
133 {
134 struct drm_vgem_fence_attach *arg = data;
135 struct vgem_file *vfile = file->driver_priv;
136 struct dma_resv *resv;
137 struct drm_gem_object *obj;
138 struct dma_fence *fence;
139 int ret;
140
141 if (arg->flags & ~VGEM_FENCE_WRITE)
142 return -EINVAL;
143
144 if (arg->pad)
145 return -EINVAL;
146
147 obj = drm_gem_object_lookup(file, arg->handle);
148 if (!obj)
149 return -ENOENT;
150
151 fence = vgem_fence_create(vfile, arg->flags);
152 if (!fence) {
153 ret = -ENOMEM;
154 goto err;
155 }
156
157 /* Check for a conflicting fence */
158 resv = obj->resv;
159 if (!dma_resv_test_signaled_rcu(resv,
160 arg->flags & VGEM_FENCE_WRITE)) {
161 ret = -EBUSY;
162 goto err_fence;
163 }
164
165 /* Expose the fence via the dma-buf */
166 ret = 0;
167 dma_resv_lock(resv, NULL);
168 if (arg->flags & VGEM_FENCE_WRITE)
169 dma_resv_add_excl_fence(resv, fence);
170 else if ((ret = dma_resv_reserve_shared(resv, 1)) == 0)
171 dma_resv_add_shared_fence(resv, fence);
172 dma_resv_unlock(resv);
173
174 /* Record the fence in our idr for later signaling */
175 if (ret == 0) {
176 idr_preload(GFP_KERNEL);
177 mutex_lock(&vfile->fence_mutex);
178 ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
179 mutex_unlock(&vfile->fence_mutex);
180 idr_preload_end();
181 if (ret > 0) {
182 arg->out_fence = ret;
183 ret = 0;
184 }
185 }
186 err_fence:
187 if (ret) {
188 dma_fence_signal(fence);
189 dma_fence_put(fence);
190 }
191 err:
192 drm_gem_object_put_unlocked(obj);
193 return ret;
194 }
195
196 /*
197 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
198 *
199 * Signal and consume a fence ealier attached to a vGEM handle using
200 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
201 *
202 * All fences must be signaled within 10s of attachment or otherwise they
203 * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
204 *
205 * Signaling a fence indicates to all consumers of the dma-buf that the
206 * client has completed the operation associated with the fence, and that the
207 * buffer is then ready for consumption.
208 *
209 * If the fence does not exist (or has already been signaled by the client),
210 * vgem_fence_signal_ioctl returns -ENOENT.
211 */
vgem_fence_signal_ioctl(struct drm_device * dev,void * data,struct drm_file * file)212 int vgem_fence_signal_ioctl(struct drm_device *dev,
213 void *data,
214 struct drm_file *file)
215 {
216 struct vgem_file *vfile = file->driver_priv;
217 struct drm_vgem_fence_signal *arg = data;
218 struct dma_fence *fence;
219 int ret = 0;
220
221 if (arg->flags)
222 return -EINVAL;
223
224 mutex_lock(&vfile->fence_mutex);
225 fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
226 mutex_unlock(&vfile->fence_mutex);
227 if (!fence)
228 return -ENOENT;
229 if (IS_ERR(fence))
230 return PTR_ERR(fence);
231
232 if (dma_fence_is_signaled(fence))
233 ret = -ETIMEDOUT;
234
235 dma_fence_signal(fence);
236 dma_fence_put(fence);
237 return ret;
238 }
239
vgem_fence_open(struct vgem_file * vfile)240 int vgem_fence_open(struct vgem_file *vfile)
241 {
242 mutex_init(&vfile->fence_mutex);
243 idr_init(&vfile->fence_idr);
244
245 return 0;
246 }
247
__vgem_fence_idr_fini(int id,void * p,void * data)248 static int __vgem_fence_idr_fini(int id, void *p, void *data)
249 {
250 dma_fence_signal(p);
251 dma_fence_put(p);
252 return 0;
253 }
254
vgem_fence_close(struct vgem_file * vfile)255 void vgem_fence_close(struct vgem_file *vfile)
256 {
257 idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
258 idr_destroy(&vfile->fence_idr);
259 }
260