blob: 9cb00a5d5d08ca0fab86be865f321a223c01d1cc [file] [log] [blame]
Chris Wilson40777982016-07-15 09:31:11 +01001/*
2 * Copyright 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software")
6 * to deal in the software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * them Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23#include <linux/dma-buf.h>
24#include <linux/reservation.h>
25
26#include "vgem_drv.h"
27
28#define VGEM_FENCE_TIMEOUT (10*HZ)
29
30struct vgem_fence {
Chris Wilsonf54d1862016-10-25 13:00:45 +010031 struct dma_fence base;
Chris Wilson40777982016-07-15 09:31:11 +010032 struct spinlock lock;
33 struct timer_list timer;
34};
35
Chris Wilsonf54d1862016-10-25 13:00:45 +010036static const char *vgem_fence_get_driver_name(struct dma_fence *fence)
Chris Wilson40777982016-07-15 09:31:11 +010037{
38 return "vgem";
39}
40
Chris Wilsonf54d1862016-10-25 13:00:45 +010041static const char *vgem_fence_get_timeline_name(struct dma_fence *fence)
Chris Wilson40777982016-07-15 09:31:11 +010042{
43 return "unbound";
44}
45
Chris Wilsonf54d1862016-10-25 13:00:45 +010046static bool vgem_fence_signaled(struct dma_fence *fence)
Chris Wilson40777982016-07-15 09:31:11 +010047{
48 return false;
49}
50
Chris Wilsonf54d1862016-10-25 13:00:45 +010051static bool vgem_fence_enable_signaling(struct dma_fence *fence)
Chris Wilson40777982016-07-15 09:31:11 +010052{
53 return true;
54}
55
Chris Wilsonf54d1862016-10-25 13:00:45 +010056static void vgem_fence_release(struct dma_fence *base)
Chris Wilson40777982016-07-15 09:31:11 +010057{
58 struct vgem_fence *fence = container_of(base, typeof(*fence), base);
59
60 del_timer_sync(&fence->timer);
Chris Wilsonf54d1862016-10-25 13:00:45 +010061 dma_fence_free(&fence->base);
Chris Wilson40777982016-07-15 09:31:11 +010062}
63
Chris Wilsonf54d1862016-10-25 13:00:45 +010064static void vgem_fence_value_str(struct dma_fence *fence, char *str, int size)
Chris Wilson40777982016-07-15 09:31:11 +010065{
66 snprintf(str, size, "%u", fence->seqno);
67}
68
Chris Wilsonf54d1862016-10-25 13:00:45 +010069static void vgem_fence_timeline_value_str(struct dma_fence *fence, char *str,
Chris Wilson40777982016-07-15 09:31:11 +010070 int size)
71{
Chris Wilsonf54d1862016-10-25 13:00:45 +010072 snprintf(str, size, "%u",
73 dma_fence_is_signaled(fence) ? fence->seqno : 0);
Chris Wilson40777982016-07-15 09:31:11 +010074}
75
Chris Wilsonf54d1862016-10-25 13:00:45 +010076static const struct dma_fence_ops vgem_fence_ops = {
Chris Wilson40777982016-07-15 09:31:11 +010077 .get_driver_name = vgem_fence_get_driver_name,
78 .get_timeline_name = vgem_fence_get_timeline_name,
79 .enable_signaling = vgem_fence_enable_signaling,
80 .signaled = vgem_fence_signaled,
Chris Wilsonf54d1862016-10-25 13:00:45 +010081 .wait = dma_fence_default_wait,
Chris Wilson40777982016-07-15 09:31:11 +010082 .release = vgem_fence_release,
83
84 .fence_value_str = vgem_fence_value_str,
85 .timeline_value_str = vgem_fence_timeline_value_str,
86};
87
88static void vgem_fence_timeout(unsigned long data)
89{
90 struct vgem_fence *fence = (struct vgem_fence *)data;
91
Chris Wilsonf54d1862016-10-25 13:00:45 +010092 dma_fence_signal(&fence->base);
Chris Wilson40777982016-07-15 09:31:11 +010093}
94
Chris Wilsonf54d1862016-10-25 13:00:45 +010095static struct dma_fence *vgem_fence_create(struct vgem_file *vfile,
96 unsigned int flags)
Chris Wilson40777982016-07-15 09:31:11 +010097{
98 struct vgem_fence *fence;
99
100 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
101 if (!fence)
102 return NULL;
103
104 spin_lock_init(&fence->lock);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100105 dma_fence_init(&fence->base, &vgem_fence_ops, &fence->lock,
106 dma_fence_context_alloc(1), 1);
Chris Wilson40777982016-07-15 09:31:11 +0100107
108 setup_timer(&fence->timer, vgem_fence_timeout, (unsigned long)fence);
109
110 /* We force the fence to expire within 10s to prevent driver hangs */
Chris Wilsonbf090172016-07-18 10:31:18 +0100111 mod_timer(&fence->timer, jiffies + VGEM_FENCE_TIMEOUT);
Chris Wilson40777982016-07-15 09:31:11 +0100112
113 return &fence->base;
114}
115
116static int attach_dmabuf(struct drm_device *dev,
117 struct drm_gem_object *obj)
118{
119 struct dma_buf *dmabuf;
120
121 if (obj->dma_buf)
122 return 0;
123
124 dmabuf = dev->driver->gem_prime_export(dev, obj, 0);
125 if (IS_ERR(dmabuf))
126 return PTR_ERR(dmabuf);
127
128 obj->dma_buf = dmabuf;
129 drm_gem_object_reference(obj);
130 return 0;
131}
132
133/*
134 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH):
135 *
136 * Create and attach a fence to the vGEM handle. This fence is then exposed
137 * via the dma-buf reservation object and visible to consumers of the exported
138 * dma-buf. If the flags contain VGEM_FENCE_WRITE, the fence indicates the
139 * vGEM buffer is being written to by the client and is exposed as an exclusive
140 * fence, otherwise the fence indicates the client is current reading from the
141 * buffer and all future writes should wait for the client to signal its
142 * completion. Note that if a conflicting fence is already on the dma-buf (i.e.
143 * an exclusive fence when adding a read, or any fence when adding a write),
144 * -EBUSY is reported. Serialisation between operations should be handled
145 * by waiting upon the dma-buf.
146 *
147 * This returns the handle for the new fence that must be signaled within 10
148 * seconds (or otherwise it will automatically expire). See
149 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL).
150 *
151 * If the vGEM handle does not exist, vgem_fence_attach_ioctl returns -ENOENT.
152 */
153int vgem_fence_attach_ioctl(struct drm_device *dev,
154 void *data,
155 struct drm_file *file)
156{
157 struct drm_vgem_fence_attach *arg = data;
158 struct vgem_file *vfile = file->driver_priv;
159 struct reservation_object *resv;
160 struct drm_gem_object *obj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100161 struct dma_fence *fence;
Chris Wilson40777982016-07-15 09:31:11 +0100162 int ret;
163
164 if (arg->flags & ~VGEM_FENCE_WRITE)
165 return -EINVAL;
166
167 if (arg->pad)
168 return -EINVAL;
169
170 obj = drm_gem_object_lookup(file, arg->handle);
171 if (!obj)
172 return -ENOENT;
173
174 ret = attach_dmabuf(dev, obj);
175 if (ret)
176 goto err;
177
178 fence = vgem_fence_create(vfile, arg->flags);
179 if (!fence) {
180 ret = -ENOMEM;
181 goto err;
182 }
183
184 /* Check for a conflicting fence */
185 resv = obj->dma_buf->resv;
186 if (!reservation_object_test_signaled_rcu(resv,
187 arg->flags & VGEM_FENCE_WRITE)) {
188 ret = -EBUSY;
189 goto err_fence;
190 }
191
192 /* Expose the fence via the dma-buf */
193 ret = 0;
Nicolai Hähnlead123102016-12-01 15:06:44 +0100194 ww_mutex_lock(&resv->lock, NULL);
Chris Wilson40777982016-07-15 09:31:11 +0100195 if (arg->flags & VGEM_FENCE_WRITE)
196 reservation_object_add_excl_fence(resv, fence);
197 else if ((ret = reservation_object_reserve_shared(resv)) == 0)
198 reservation_object_add_shared_fence(resv, fence);
Nicolai Hähnlead123102016-12-01 15:06:44 +0100199 ww_mutex_unlock(&resv->lock);
Chris Wilson40777982016-07-15 09:31:11 +0100200
201 /* Record the fence in our idr for later signaling */
202 if (ret == 0) {
203 mutex_lock(&vfile->fence_mutex);
204 ret = idr_alloc(&vfile->fence_idr, fence, 1, 0, GFP_KERNEL);
205 mutex_unlock(&vfile->fence_mutex);
206 if (ret > 0) {
207 arg->out_fence = ret;
208 ret = 0;
209 }
210 }
211err_fence:
212 if (ret) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100213 dma_fence_signal(fence);
214 dma_fence_put(fence);
Chris Wilson40777982016-07-15 09:31:11 +0100215 }
216err:
217 drm_gem_object_unreference_unlocked(obj);
218 return ret;
219}
220
221/*
222 * vgem_fence_signal_ioctl (DRM_IOCTL_VGEM_FENCE_SIGNAL):
223 *
224 * Signal and consume a fence ealier attached to a vGEM handle using
225 * vgem_fence_attach_ioctl (DRM_IOCTL_VGEM_FENCE_ATTACH).
226 *
227 * All fences must be signaled within 10s of attachment or otherwise they
228 * will automatically expire (and a vgem_fence_signal_ioctl returns -ETIMEDOUT).
229 *
230 * Signaling a fence indicates to all consumers of the dma-buf that the
231 * client has completed the operation associated with the fence, and that the
232 * buffer is then ready for consumption.
233 *
234 * If the fence does not exist (or has already been signaled by the client),
235 * vgem_fence_signal_ioctl returns -ENOENT.
236 */
237int vgem_fence_signal_ioctl(struct drm_device *dev,
238 void *data,
239 struct drm_file *file)
240{
241 struct vgem_file *vfile = file->driver_priv;
242 struct drm_vgem_fence_signal *arg = data;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100243 struct dma_fence *fence;
Chris Wilsonbf090172016-07-18 10:31:18 +0100244 int ret = 0;
Chris Wilson40777982016-07-15 09:31:11 +0100245
246 if (arg->flags)
247 return -EINVAL;
248
249 mutex_lock(&vfile->fence_mutex);
250 fence = idr_replace(&vfile->fence_idr, NULL, arg->fence);
251 mutex_unlock(&vfile->fence_mutex);
252 if (!fence)
253 return -ENOENT;
254 if (IS_ERR(fence))
255 return PTR_ERR(fence);
256
Chris Wilsonf54d1862016-10-25 13:00:45 +0100257 if (dma_fence_is_signaled(fence))
Chris Wilson40777982016-07-15 09:31:11 +0100258 ret = -ETIMEDOUT;
259
Chris Wilsonf54d1862016-10-25 13:00:45 +0100260 dma_fence_signal(fence);
261 dma_fence_put(fence);
Chris Wilson40777982016-07-15 09:31:11 +0100262 return ret;
263}
264
265int vgem_fence_open(struct vgem_file *vfile)
266{
267 mutex_init(&vfile->fence_mutex);
268 idr_init(&vfile->fence_idr);
269
270 return 0;
271}
272
273static int __vgem_fence_idr_fini(int id, void *p, void *data)
274{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100275 dma_fence_signal(p);
276 dma_fence_put(p);
Chris Wilson40777982016-07-15 09:31:11 +0100277 return 0;
278}
279
280void vgem_fence_close(struct vgem_file *vfile)
281{
282 idr_for_each(&vfile->fence_idr, __vgem_fence_idr_fini, vfile);
283 idr_destroy(&vfile->fence_idr);
284}