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