1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/dma-buf.h>
24#include <linux/dma-resv.h>
25
26#include <drm/drm_file.h>
27
28#include "vgem_drv.h"
29
30#define VGEM_FENCE_TIMEOUT (10*HZ)
31
32struct vgem_fence {
33 struct dma_fence base;
34 struct spinlock lock;
35 struct timer_list timer;
36};
37
38static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
39{
40 return "vgem";
41}
42
43static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
44{
45 return "unbound";
46}
47
48static void vgem_fence_release(struct dma_fence *base)
49{
50 struct vgem_fence *fence = container_of(base, typeof(*fence), base);
51
52 del_timer_sync(&fence->timer);
53 dma_fence_free(&fence->base);
54}
55
56static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
57{
58 snprintf(str, size, "%llu", fence->seqno);
59}
60
61static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
62 int size)
63{
64 snprintf(str, size, "%llu",
65 dma_fence_is_signaled(fence) ? fence->seqno : 0);
66}
67
68static const struct dma_fence_ops vgem_fence_ops = {
69 .get_driver_name = vgem_fence_get_driver_name,
70 .get_timeline_name = vgem_fence_get_timeline_name,
71 .release = vgem_fence_release,
72
73 .fence_value_str = vgem_fence_value_str,
74 .timeline_value_str = vgem_fence_timeline_value_str,
75};
76
77static void vgem_fence_timeout(struct timer_list *t)
78{
79 struct vgem_fence *fence = from_timer(fence, t, timer);
80
81 dma_fence_signal(&fence->base);
82}
83
84static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
85 unsigned int flags)
86{
87 struct vgem_fence *fence;
88
89 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
90 if (!fence)
91 return NULL;
92
93 spin_lock_init(&fence->lock);
94 dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
95 dma_fence_context_alloc(1), 1);
96
97 timer_setup(&fence->timer, vgem_fence_timeout, 0);
98
99
100 mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
101
102 return &fence->base;
103}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125int vgem_fence_attach_ioctl(struct drm_device *dev,
126 void *data,
127 struct drm_file *file)
128{
129 struct drm_vgem_fence_attach *arg = data;
130 struct vgem_file *vfile = file->driver_priv;
131 struct dma_resv *resv;
132 struct drm_gem_object *obj;
133 struct dma_fence *fence;
134 int ret;
135
136 if (arg->flags & ~VGEM_FENCE_WRITE)
137 return -EINVAL;
138
139 if (arg->pad)
140 return -EINVAL;
141
142 obj = drm_gem_object_lookup(file, arg->handle);
143 if (!obj)
144 return -ENOENT;
145
146 fence = vgem_fence_create(vfile, arg->flags);
147 if (!fence) {
148 ret = -ENOMEM;
149 goto err;
150 }
151
152
153 resv = obj->resv;
154 if (!dma_resv_test_signaled(resv, arg->flags & VGEM_FENCE_WRITE)) {
155 ret = -EBUSY;
156 goto err_fence;
157 }
158
159
160 ret = 0;
161 dma_resv_lock(resv, NULL);
162 if (arg->flags & VGEM_FENCE_WRITE)
163 dma_resv_add_excl_fence(resv, fence);
164 else if ((ret = dma_resv_reserve_shared(resv, 1)) == 0)
165 dma_resv_add_shared_fence(resv, fence);
166 dma_resv_unlock(resv);
167
168
169 if (ret == 0) {
170 mutex_lock(&vfile->fence_mutex);
171 ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
172 mutex_unlock(&vfile->fence_mutex);
173 if (ret > 0) {
174 arg->out_fence = ret;
175 ret = 0;
176 }
177 }
178err_fence:
179 if (ret) {
180 dma_fence_signal(fence);
181 dma_fence_put(fence);
182 }
183err:
184 drm_gem_object_put(obj);
185 return ret;
186}
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204int vgem_fence_signal_ioctl(struct drm_device *dev,
205 void *data,
206 struct drm_file *file)
207{
208 struct vgem_file *vfile = file->driver_priv;
209 struct drm_vgem_fence_signal *arg = data;
210 struct dma_fence *fence;
211 int ret = 0;
212
213 if (arg->flags)
214 return -EINVAL;
215
216 mutex_lock(&vfile->fence_mutex);
217 fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
218 mutex_unlock(&vfile->fence_mutex);
219 if (!fence)
220 return -ENOENT;
221 if (IS_ERR(fence))
222 return PTR_ERR(fence);
223
224 if (dma_fence_is_signaled(fence))
225 ret = -ETIMEDOUT;
226
227 dma_fence_signal(fence);
228 dma_fence_put(fence);
229 return ret;
230}
231
232int vgem_fence_open(struct vgem_file *vfile)
233{
234 mutex_init(&vfile->fence_mutex);
235 idr_init_base(&vfile->fence_idr, 1);
236
237 return 0;
238}
239
240static int __vgem_fence_idr_fini(int id, void *p, void *data)
241{
242 dma_fence_signal(p);
243 dma_fence_put(p);
244 return 0;
245}
246
247void vgem_fence_close(struct vgem_file *vfile)
248{
249 idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
250 idr_destroy(&vfile->fence_idr);
251}
252