blob: 361a01a08c1856532a29460e5e21e8a63cbaaf24 [file] [log] [blame]
Dave Airliee9083422017-04-04 13:26:24 +10001/*
2 * Copyright 2017 Red Hat
Dave Airlie5e60a102017-08-25 10:52:22 -07003 * Parts ported from amdgpu (fence wait code).
4 * Copyright 2016 Advanced Micro Devices, Inc.
Dave Airliee9083422017-04-04 13:26:24 +10005 *
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * 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 MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 *
27 */
28
29/**
30 * DOC: Overview
31 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +010032 * DRM synchronisation objects (syncobj, see struct &drm_syncobj) are
33 * persistent objects that contain an optional fence. The fence can be updated
34 * with a new fence, or be NULL.
Dave Airliee9083422017-04-04 13:26:24 +100035 *
Dave Airlie5e60a102017-08-25 10:52:22 -070036 * syncobj's can be waited upon, where it will wait for the underlying
37 * fence.
38 *
Dave Airliee9083422017-04-04 13:26:24 +100039 * syncobj's can be export to fd's and back, these fd's are opaque and
40 * have no other use case, except passing the syncobj between processes.
41 *
42 * Their primary use-case is to implement Vulkan fences and semaphores.
43 *
44 * syncobj have a kref reference count, but also have an optional file.
45 * The file is only created once the syncobj is exported.
46 * The file takes a reference on the kref.
47 */
48
49#include <drm/drmP.h>
50#include <linux/file.h>
51#include <linux/fs.h>
52#include <linux/anon_inodes.h>
Dave Airlie3ee45a32017-04-26 04:09:02 +010053#include <linux/sync_file.h>
Jason Ekstrande7aca5032017-08-25 10:52:24 -070054#include <linux/sched/signal.h>
Dave Airliee9083422017-04-04 13:26:24 +100055
56#include "drm_internal.h"
57#include <drm/drm_syncobj.h>
58
Christian König61a98b12018-12-11 18:34:41 +080059struct syncobj_wait_entry {
60 struct list_head node;
61 struct task_struct *task;
62 struct dma_fence *fence;
63 struct dma_fence_cb fence_cb;
Chunming Zhou01d6c352019-04-01 17:50:57 +080064 u64 point;
Christian König61a98b12018-12-11 18:34:41 +080065};
66
67static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
68 struct syncobj_wait_entry *wait);
69
Dave Airliee9083422017-04-04 13:26:24 +100070/**
71 * drm_syncobj_find - lookup and reference a sync object.
72 * @file_private: drm file private pointer
73 * @handle: sync object handle to lookup.
74 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +010075 * Returns a reference to the syncobj pointed to by handle or NULL. The
76 * reference must be released by calling drm_syncobj_put().
Dave Airliee9083422017-04-04 13:26:24 +100077 */
78struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
79 u32 handle)
80{
81 struct drm_syncobj *syncobj;
82
83 spin_lock(&file_private->syncobj_table_lock);
84
85 /* Check if we currently have a reference on the object */
86 syncobj = idr_find(&file_private->syncobj_idr, handle);
87 if (syncobj)
88 drm_syncobj_get(syncobj);
89
90 spin_unlock(&file_private->syncobj_table_lock);
91
92 return syncobj;
93}
94EXPORT_SYMBOL(drm_syncobj_find);
95
Christian König61a98b12018-12-11 18:34:41 +080096static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
97 struct syncobj_wait_entry *wait)
Chunming Zhou43cf1fc2018-10-23 17:37:45 +080098{
Chunming Zhou01d6c352019-04-01 17:50:57 +080099 struct dma_fence *fence;
100
Christian König61a98b12018-12-11 18:34:41 +0800101 if (wait->fence)
102 return;
Jason Ekstrand337fe9f2018-09-26 02:17:03 -0500103
Eric Anholt131280a2018-11-08 08:04:22 -0800104 spin_lock(&syncobj->lock);
105 /* We've already tried once to get a fence and failed. Now that we
106 * have the lock, try one more time just to be sure we don't add a
107 * callback when a fence has already been set.
108 */
Chunming Zhou01d6c352019-04-01 17:50:57 +0800109 fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
110 if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
111 dma_fence_put(fence);
Christian König61a98b12018-12-11 18:34:41 +0800112 list_add_tail(&wait->node, &syncobj->cb_list);
Chunming Zhou01d6c352019-04-01 17:50:57 +0800113 } else if (!fence) {
114 wait->fence = dma_fence_get_stub();
115 } else {
116 wait->fence = fence;
117 }
Eric Anholt131280a2018-11-08 08:04:22 -0800118 spin_unlock(&syncobj->lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800119}
Eric Anholt131280a2018-11-08 08:04:22 -0800120
Christian König61a98b12018-12-11 18:34:41 +0800121static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
122 struct syncobj_wait_entry *wait)
Eric Anholt131280a2018-11-08 08:04:22 -0800123{
Christian König61a98b12018-12-11 18:34:41 +0800124 if (!wait->node.next)
125 return;
126
Eric Anholt131280a2018-11-08 08:04:22 -0800127 spin_lock(&syncobj->lock);
Christian König61a98b12018-12-11 18:34:41 +0800128 list_del_init(&wait->node);
Eric Anholt131280a2018-11-08 08:04:22 -0800129 spin_unlock(&syncobj->lock);
130}
131
Dave Airliee9083422017-04-04 13:26:24 +1000132/**
Christian König44f8a132019-04-01 17:50:56 +0800133 * drm_syncobj_add_point - add new timeline point to the syncobj
134 * @syncobj: sync object to add timeline point do
135 * @chain: chain node to use to add the point
136 * @fence: fence to encapsulate in the chain node
137 * @point: sequence number to use for the point
138 *
139 * Add the chain node as new timeline point to the syncobj.
140 */
141void drm_syncobj_add_point(struct drm_syncobj *syncobj,
142 struct dma_fence_chain *chain,
143 struct dma_fence *fence,
144 uint64_t point)
145{
146 struct syncobj_wait_entry *cur, *tmp;
147 struct dma_fence *prev;
148
149 dma_fence_get(fence);
150
151 spin_lock(&syncobj->lock);
152
153 prev = drm_syncobj_fence_get(syncobj);
154 /* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
155 if (prev && prev->seqno >= point)
156 DRM_ERROR("You are adding an unorder point to timeline!\n");
157 dma_fence_chain_init(chain, prev, fence, point);
158 rcu_assign_pointer(syncobj->fence, &chain->base);
159
Chunming Zhou01d6c352019-04-01 17:50:57 +0800160 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
Christian König44f8a132019-04-01 17:50:56 +0800161 syncobj_wait_syncobj_func(syncobj, cur);
Christian König44f8a132019-04-01 17:50:56 +0800162 spin_unlock(&syncobj->lock);
163
164 /* Walk the chain once to trigger garbage collection */
165 dma_fence_chain_for_each(fence, prev);
166 dma_fence_put(prev);
167}
168EXPORT_SYMBOL(drm_syncobj_add_point);
169
170/**
Dave Airliee9083422017-04-04 13:26:24 +1000171 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000172 * @syncobj: Sync object to replace fence in
173 * @fence: fence to install in sync file.
174 *
Christian König0b258ed2018-11-14 14:24:27 +0100175 * This replaces the fence on a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000176 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100177void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Dave Airliee9083422017-04-04 13:26:24 +1000178 struct dma_fence *fence)
179{
Eric Anholt131280a2018-11-08 08:04:22 -0800180 struct dma_fence *old_fence;
Christian König61a98b12018-12-11 18:34:41 +0800181 struct syncobj_wait_entry *cur, *tmp;
Dave Airliee9083422017-04-04 13:26:24 +1000182
Eric Anholt131280a2018-11-08 08:04:22 -0800183 if (fence)
184 dma_fence_get(fence);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700185
Eric Anholt131280a2018-11-08 08:04:22 -0800186 spin_lock(&syncobj->lock);
187
188 old_fence = rcu_dereference_protected(syncobj->fence,
189 lockdep_is_held(&syncobj->lock));
190 rcu_assign_pointer(syncobj->fence, fence);
191
192 if (fence != old_fence) {
Chunming Zhou01d6c352019-04-01 17:50:57 +0800193 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
Christian König61a98b12018-12-11 18:34:41 +0800194 syncobj_wait_syncobj_func(syncobj, cur);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700195 }
Eric Anholt131280a2018-11-08 08:04:22 -0800196
197 spin_unlock(&syncobj->lock);
198
199 dma_fence_put(old_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000200}
201EXPORT_SYMBOL(drm_syncobj_replace_fence);
202
Christian König86bbd892018-11-13 14:14:00 +0100203/**
204 * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
205 * @syncobj: sync object to assign the fence on
206 *
207 * Assign a already signaled stub fence to the sync object.
208 */
209static void drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700210{
Christian König86bbd892018-11-13 14:14:00 +0100211 struct dma_fence *fence = dma_fence_get_stub();
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700212
Christian König0b258ed2018-11-14 14:24:27 +0100213 drm_syncobj_replace_fence(syncobj, fence);
Christian König86bbd892018-11-13 14:14:00 +0100214 dma_fence_put(fence);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700215}
216
Christian Königbc9c80f2019-04-01 17:50:59 +0800217/* 5s default for wait submission */
218#define DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT 5000000000ULL
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100219/**
220 * drm_syncobj_find_fence - lookup and reference the fence in a sync object
221 * @file_private: drm file private pointer
222 * @handle: sync object handle to lookup.
Chunming Zhou0a6730e2018-08-30 14:48:29 +0800223 * @point: timeline point
Chunming Zhou871edc92018-10-17 15:03:18 +0800224 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100225 * @fence: out parameter for the fence
226 *
227 * This is just a convenience function that combines drm_syncobj_find() and
Eric Anholt131280a2018-11-08 08:04:22 -0800228 * drm_syncobj_fence_get().
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100229 *
230 * Returns 0 on success or a negative error value on failure. On success @fence
231 * contains a reference to the fence, which must be released by calling
232 * dma_fence_put().
233 */
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700234int drm_syncobj_find_fence(struct drm_file *file_private,
Chunming Zhou649fdce2018-10-15 16:55:47 +0800235 u32 handle, u64 point, u64 flags,
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700236 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000237{
238 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
Christian Königbc9c80f2019-04-01 17:50:59 +0800239 struct syncobj_wait_entry wait;
240 u64 timeout = nsecs_to_jiffies64(DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT);
241 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000242
Eric Anholt131280a2018-11-08 08:04:22 -0800243 if (!syncobj)
244 return -ENOENT;
245
246 *fence = drm_syncobj_fence_get(syncobj);
Christian Königbc9c80f2019-04-01 17:50:59 +0800247 drm_syncobj_put(syncobj);
248
249 if (*fence) {
250 ret = dma_fence_chain_find_seqno(fence, point);
251 if (!ret)
252 return 0;
253 dma_fence_put(*fence);
254 } else {
Eric Anholt131280a2018-11-08 08:04:22 -0800255 ret = -EINVAL;
256 }
Christian Königbc9c80f2019-04-01 17:50:59 +0800257
258 if (!(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
259 return ret;
260
261 memset(&wait, 0, sizeof(wait));
262 wait.task = current;
263 wait.point = point;
264 drm_syncobj_fence_add_wait(syncobj, &wait);
265
266 do {
267 set_current_state(TASK_INTERRUPTIBLE);
268 if (wait.fence) {
269 ret = 0;
270 break;
271 }
272 if (timeout == 0) {
273 ret = -ETIME;
274 break;
275 }
276
277 if (signal_pending(current)) {
278 ret = -ERESTARTSYS;
279 break;
280 }
281
282 timeout = schedule_timeout(timeout);
283 } while (1);
284
285 __set_current_state(TASK_RUNNING);
286 *fence = wait.fence;
287
288 if (wait.node.next)
289 drm_syncobj_remove_wait(syncobj, &wait);
290
Dave Airliee9083422017-04-04 13:26:24 +1000291 return ret;
292}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700293EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000294
295/**
296 * drm_syncobj_free - free a sync object.
297 * @kref: kref to free.
298 *
299 * Only to be called from kref_put in drm_syncobj_put.
300 */
301void drm_syncobj_free(struct kref *kref)
302{
303 struct drm_syncobj *syncobj = container_of(kref,
304 struct drm_syncobj,
305 refcount);
Christian König0b258ed2018-11-14 14:24:27 +0100306 drm_syncobj_replace_fence(syncobj, NULL);
Dave Airliee9083422017-04-04 13:26:24 +1000307 kfree(syncobj);
308}
309EXPORT_SYMBOL(drm_syncobj_free);
310
Marek Olšák1321fd22017-09-12 22:42:12 +0200311/**
312 * drm_syncobj_create - create a new syncobj
313 * @out_syncobj: returned syncobj
314 * @flags: DRM_SYNCOBJ_* flags
315 * @fence: if non-NULL, the syncobj will represent this fence
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100316 *
317 * This is the first function to create a sync object. After creating, drivers
318 * probably want to make it available to userspace, either through
319 * drm_syncobj_get_handle() or drm_syncobj_get_fd().
320 *
321 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200322 */
323int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
324 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000325{
Dave Airliee9083422017-04-04 13:26:24 +1000326 struct drm_syncobj *syncobj;
327
328 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
329 if (!syncobj)
330 return -ENOMEM;
331
332 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700333 INIT_LIST_HEAD(&syncobj->cb_list);
Eric Anholt131280a2018-11-08 08:04:22 -0800334 spin_lock_init(&syncobj->lock);
Dave Airliee9083422017-04-04 13:26:24 +1000335
Christian König86bbd892018-11-13 14:14:00 +0100336 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
337 drm_syncobj_assign_null_handle(syncobj);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700338
Marek Olšák1321fd22017-09-12 22:42:12 +0200339 if (fence)
Christian König0b258ed2018-11-14 14:24:27 +0100340 drm_syncobj_replace_fence(syncobj, fence);
Marek Olšák1321fd22017-09-12 22:42:12 +0200341
342 *out_syncobj = syncobj;
343 return 0;
344}
345EXPORT_SYMBOL(drm_syncobj_create);
346
347/**
348 * drm_syncobj_get_handle - get a handle from a syncobj
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100349 * @file_private: drm file private pointer
350 * @syncobj: Sync object to export
351 * @handle: out parameter with the new handle
352 *
353 * Exports a sync object created with drm_syncobj_create() as a handle on
354 * @file_private to userspace.
355 *
356 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200357 */
358int drm_syncobj_get_handle(struct drm_file *file_private,
359 struct drm_syncobj *syncobj, u32 *handle)
360{
361 int ret;
362
363 /* take a reference to put in the idr */
364 drm_syncobj_get(syncobj);
365
Dave Airliee9083422017-04-04 13:26:24 +1000366 idr_preload(GFP_KERNEL);
367 spin_lock(&file_private->syncobj_table_lock);
368 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
369 spin_unlock(&file_private->syncobj_table_lock);
370
371 idr_preload_end();
372
373 if (ret < 0) {
374 drm_syncobj_put(syncobj);
375 return ret;
376 }
377
378 *handle = ret;
379 return 0;
380}
Marek Olšák1321fd22017-09-12 22:42:12 +0200381EXPORT_SYMBOL(drm_syncobj_get_handle);
382
383static int drm_syncobj_create_as_handle(struct drm_file *file_private,
384 u32 *handle, uint32_t flags)
385{
386 int ret;
387 struct drm_syncobj *syncobj;
388
389 ret = drm_syncobj_create(&syncobj, flags, NULL);
390 if (ret)
391 return ret;
392
393 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
394 drm_syncobj_put(syncobj);
395 return ret;
396}
Dave Airliee9083422017-04-04 13:26:24 +1000397
398static int drm_syncobj_destroy(struct drm_file *file_private,
399 u32 handle)
400{
401 struct drm_syncobj *syncobj;
402
403 spin_lock(&file_private->syncobj_table_lock);
404 syncobj = idr_remove(&file_private->syncobj_idr, handle);
405 spin_unlock(&file_private->syncobj_table_lock);
406
407 if (!syncobj)
408 return -EINVAL;
409
410 drm_syncobj_put(syncobj);
411 return 0;
412}
413
414static int drm_syncobj_file_release(struct inode *inode, struct file *file)
415{
416 struct drm_syncobj *syncobj = file->private_data;
417
418 drm_syncobj_put(syncobj);
419 return 0;
420}
421
422static const struct file_operations drm_syncobj_file_fops = {
423 .release = drm_syncobj_file_release,
424};
425
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100426/**
427 * drm_syncobj_get_fd - get a file descriptor from a syncobj
428 * @syncobj: Sync object to export
429 * @p_fd: out parameter with the new file descriptor
430 *
431 * Exports a sync object created with drm_syncobj_create() as a file descriptor.
432 *
433 * Returns 0 on success or a negative error value on failure.
434 */
Marek Olšák684fd0a2017-09-12 22:42:13 +0200435int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
436{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000437 struct file *file;
Marek Olšák684fd0a2017-09-12 22:42:13 +0200438 int fd;
439
440 fd = get_unused_fd_flags(O_CLOEXEC);
441 if (fd < 0)
442 return fd;
443
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000444 file = anon_inode_getfile("syncobj_file",
445 &drm_syncobj_file_fops,
446 syncobj, 0);
447 if (IS_ERR(file)) {
448 put_unused_fd(fd);
449 return PTR_ERR(file);
Marek Olšák684fd0a2017-09-12 22:42:13 +0200450 }
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000451
452 drm_syncobj_get(syncobj);
453 fd_install(fd, file);
454
Marek Olšák684fd0a2017-09-12 22:42:13 +0200455 *p_fd = fd;
456 return 0;
457}
458EXPORT_SYMBOL(drm_syncobj_get_fd);
459
Dave Airliee9083422017-04-04 13:26:24 +1000460static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
461 u32 handle, int *p_fd)
462{
463 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
464 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000465
466 if (!syncobj)
467 return -EINVAL;
468
Marek Olšák684fd0a2017-09-12 22:42:13 +0200469 ret = drm_syncobj_get_fd(syncobj, p_fd);
Dave Airliee9083422017-04-04 13:26:24 +1000470 drm_syncobj_put(syncobj);
471 return ret;
472}
473
Dave Airliee9083422017-04-04 13:26:24 +1000474static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
475 int fd, u32 *handle)
476{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000477 struct drm_syncobj *syncobj;
478 struct file *file;
Dave Airliee9083422017-04-04 13:26:24 +1000479 int ret;
480
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000481 file = fget(fd);
482 if (!file)
Dave Airliee9083422017-04-04 13:26:24 +1000483 return -EINVAL;
484
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000485 if (file->f_op != &drm_syncobj_file_fops) {
486 fput(file);
487 return -EINVAL;
488 }
489
Dave Airliee9083422017-04-04 13:26:24 +1000490 /* take a reference to put in the idr */
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000491 syncobj = file->private_data;
Dave Airliee9083422017-04-04 13:26:24 +1000492 drm_syncobj_get(syncobj);
493
494 idr_preload(GFP_KERNEL);
495 spin_lock(&file_private->syncobj_table_lock);
496 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
497 spin_unlock(&file_private->syncobj_table_lock);
498 idr_preload_end();
499
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000500 if (ret > 0) {
501 *handle = ret;
502 ret = 0;
503 } else
504 drm_syncobj_put(syncobj);
505
506 fput(file);
507 return ret;
Dave Airliee9083422017-04-04 13:26:24 +1000508}
509
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300510static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
511 int fd, int handle)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100512{
513 struct dma_fence *fence = sync_file_get_fence(fd);
514 struct drm_syncobj *syncobj;
515
516 if (!fence)
517 return -EINVAL;
518
519 syncobj = drm_syncobj_find(file_private, handle);
520 if (!syncobj) {
521 dma_fence_put(fence);
522 return -ENOENT;
523 }
524
Christian König0b258ed2018-11-14 14:24:27 +0100525 drm_syncobj_replace_fence(syncobj, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100526 dma_fence_put(fence);
527 drm_syncobj_put(syncobj);
528 return 0;
529}
530
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300531static int drm_syncobj_export_sync_file(struct drm_file *file_private,
532 int handle, int *p_fd)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100533{
534 int ret;
535 struct dma_fence *fence;
536 struct sync_file *sync_file;
537 int fd = get_unused_fd_flags(O_CLOEXEC);
538
539 if (fd < 0)
540 return fd;
541
Chunming Zhou649fdce2018-10-15 16:55:47 +0800542 ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100543 if (ret)
544 goto err_put_fd;
545
546 sync_file = sync_file_create(fence);
547
548 dma_fence_put(fence);
549
550 if (!sync_file) {
551 ret = -EINVAL;
552 goto err_put_fd;
553 }
554
555 fd_install(fd, sync_file->file);
556
557 *p_fd = fd;
558 return 0;
559err_put_fd:
560 put_unused_fd(fd);
561 return ret;
562}
Dave Airliee9083422017-04-04 13:26:24 +1000563/**
564 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000565 * @file_private: drm file-private structure to set up
566 *
567 * Called at device open time, sets up the structure for handling refcounting
568 * of sync objects.
569 */
570void
571drm_syncobj_open(struct drm_file *file_private)
572{
Chris Wilsone86584c2018-02-12 14:55:33 +0000573 idr_init_base(&file_private->syncobj_idr, 1);
Dave Airliee9083422017-04-04 13:26:24 +1000574 spin_lock_init(&file_private->syncobj_table_lock);
575}
576
577static int
578drm_syncobj_release_handle(int id, void *ptr, void *data)
579{
580 struct drm_syncobj *syncobj = ptr;
581
582 drm_syncobj_put(syncobj);
583 return 0;
584}
585
586/**
587 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000588 * @file_private: drm file-private structure to clean up
589 *
590 * Called at close time when the filp is going away.
591 *
592 * Releases any remaining references on objects by this filp.
593 */
594void
595drm_syncobj_release(struct drm_file *file_private)
596{
597 idr_for_each(&file_private->syncobj_idr,
598 &drm_syncobj_release_handle, file_private);
599 idr_destroy(&file_private->syncobj_idr);
600}
601
602int
603drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
604 struct drm_file *file_private)
605{
606 struct drm_syncobj_create *args = data;
607
608 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100609 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000610
611 /* no valid flags yet */
Eric Anholt131280a2018-11-08 08:04:22 -0800612 if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
Dave Airliee9083422017-04-04 13:26:24 +1000613 return -EINVAL;
614
Marek Olšák1321fd22017-09-12 22:42:12 +0200615 return drm_syncobj_create_as_handle(file_private,
616 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000617}
618
619int
620drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
621 struct drm_file *file_private)
622{
623 struct drm_syncobj_destroy *args = data;
624
625 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100626 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000627
628 /* make sure padding is empty */
629 if (args->pad)
630 return -EINVAL;
631 return drm_syncobj_destroy(file_private, args->handle);
632}
633
634int
635drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
636 struct drm_file *file_private)
637{
638 struct drm_syncobj_handle *args = data;
639
640 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100641 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000642
Dave Airlie3ee45a32017-04-26 04:09:02 +0100643 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000644 return -EINVAL;
645
Dave Airlie3ee45a32017-04-26 04:09:02 +0100646 if (args->flags != 0 &&
647 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
648 return -EINVAL;
649
650 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
651 return drm_syncobj_export_sync_file(file_private, args->handle,
652 &args->fd);
653
Dave Airliee9083422017-04-04 13:26:24 +1000654 return drm_syncobj_handle_to_fd(file_private, args->handle,
655 &args->fd);
656}
657
658int
659drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
660 struct drm_file *file_private)
661{
662 struct drm_syncobj_handle *args = data;
663
664 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100665 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000666
Dave Airlie3ee45a32017-04-26 04:09:02 +0100667 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000668 return -EINVAL;
669
Dave Airlie3ee45a32017-04-26 04:09:02 +0100670 if (args->flags != 0 &&
671 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
672 return -EINVAL;
673
674 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
675 return drm_syncobj_import_sync_file_fence(file_private,
676 args->fd,
677 args->handle);
678
Dave Airliee9083422017-04-04 13:26:24 +1000679 return drm_syncobj_fd_to_handle(file_private, args->fd,
680 &args->handle);
681}
Dave Airlie5e60a102017-08-25 10:52:22 -0700682
Chunming Zhouea569912019-04-01 17:51:01 +0800683static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
684 struct drm_syncobj_transfer *args)
685{
686 struct drm_syncobj *timeline_syncobj = NULL;
687 struct dma_fence *fence;
688 struct dma_fence_chain *chain;
689 int ret;
690
691 timeline_syncobj = drm_syncobj_find(file_private, args->dst_handle);
692 if (!timeline_syncobj) {
693 return -ENOENT;
694 }
695 ret = drm_syncobj_find_fence(file_private, args->src_handle,
696 args->src_point, args->flags,
697 &fence);
698 if (ret)
699 goto err;
700 chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
701 if (!chain) {
702 ret = -ENOMEM;
703 goto err1;
704 }
705 drm_syncobj_add_point(timeline_syncobj, chain, fence, args->dst_point);
706err1:
707 dma_fence_put(fence);
708err:
709 drm_syncobj_put(timeline_syncobj);
710
711 return ret;
712}
713
714static int
715drm_syncobj_transfer_to_binary(struct drm_file *file_private,
716 struct drm_syncobj_transfer *args)
717{
718 struct drm_syncobj *binary_syncobj = NULL;
719 struct dma_fence *fence;
720 int ret;
721
722 binary_syncobj = drm_syncobj_find(file_private, args->dst_handle);
723 if (!binary_syncobj)
724 return -ENOENT;
725 ret = drm_syncobj_find_fence(file_private, args->src_handle,
726 args->src_point, args->flags, &fence);
727 if (ret)
728 goto err;
729 drm_syncobj_replace_fence(binary_syncobj, fence);
730 dma_fence_put(fence);
731err:
732 drm_syncobj_put(binary_syncobj);
733
734 return ret;
735}
736int
737drm_syncobj_transfer_ioctl(struct drm_device *dev, void *data,
738 struct drm_file *file_private)
739{
740 struct drm_syncobj_transfer *args = data;
741 int ret;
742
Lionel Landwerlin060cebb2019-04-16 13:57:50 +0100743 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
Lionel Landwerlin5ec77632019-04-16 13:30:47 +0100744 return -EOPNOTSUPP;
Chunming Zhouea569912019-04-01 17:51:01 +0800745
746 if (args->pad)
747 return -EINVAL;
748
749 if (args->dst_point)
750 ret = drm_syncobj_transfer_to_timeline(file_private, args);
751 else
752 ret = drm_syncobj_transfer_to_binary(file_private, args);
753
754 return ret;
755}
756
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700757static void syncobj_wait_fence_func(struct dma_fence *fence,
758 struct dma_fence_cb *cb)
759{
760 struct syncobj_wait_entry *wait =
761 container_of(cb, struct syncobj_wait_entry, fence_cb);
762
763 wake_up_process(wait->task);
764}
765
766static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
Christian König61a98b12018-12-11 18:34:41 +0800767 struct syncobj_wait_entry *wait)
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700768{
Chunming Zhou01d6c352019-04-01 17:50:57 +0800769 struct dma_fence *fence;
770
Eric Anholt131280a2018-11-08 08:04:22 -0800771 /* This happens inside the syncobj lock */
Chunming Zhou01d6c352019-04-01 17:50:57 +0800772 fence = rcu_dereference_protected(syncobj->fence,
773 lockdep_is_held(&syncobj->lock));
774 dma_fence_get(fence);
775 if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
776 dma_fence_put(fence);
777 return;
778 } else if (!fence) {
779 wait->fence = dma_fence_get_stub();
780 } else {
781 wait->fence = fence;
782 }
783
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700784 wake_up_process(wait->task);
Chunming Zhou01d6c352019-04-01 17:50:57 +0800785 list_del_init(&wait->node);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700786}
787
788static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
Chunming Zhou01d6c352019-04-01 17:50:57 +0800789 void __user *user_points,
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700790 uint32_t count,
791 uint32_t flags,
792 signed long timeout,
793 uint32_t *idx)
794{
795 struct syncobj_wait_entry *entries;
796 struct dma_fence *fence;
Chunming Zhou01d6c352019-04-01 17:50:57 +0800797 uint64_t *points;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700798 uint32_t signaled_count, i;
799
Chunming Zhou01d6c352019-04-01 17:50:57 +0800800 points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
801 if (points == NULL)
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700802 return -ENOMEM;
803
Chunming Zhou01d6c352019-04-01 17:50:57 +0800804 if (!user_points) {
805 memset(points, 0, count * sizeof(uint64_t));
806
807 } else if (copy_from_user(points, user_points,
808 sizeof(uint64_t) * count)) {
809 timeout = -EFAULT;
810 goto err_free_points;
811 }
812
813 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
814 if (!entries) {
815 timeout = -ENOMEM;
816 goto err_free_points;
817 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700818 /* Walk the list of sync objects and initialize entries. We do
819 * this up-front so that we can properly return -EINVAL if there is
820 * a syncobj with a missing fence and then never have the chance of
821 * returning -EINVAL again.
822 */
823 signaled_count = 0;
824 for (i = 0; i < count; ++i) {
Chunming Zhou01d6c352019-04-01 17:50:57 +0800825 struct dma_fence *fence;
826
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700827 entries[i].task = current;
Chunming Zhou01d6c352019-04-01 17:50:57 +0800828 entries[i].point = points[i];
829 fence = drm_syncobj_fence_get(syncobjs[i]);
830 if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
831 dma_fence_put(fence);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700832 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
833 continue;
834 } else {
Chris Wilson12fec622018-09-20 21:05:30 +0100835 timeout = -EINVAL;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700836 goto cleanup_entries;
837 }
838 }
839
Chunming Zhou01d6c352019-04-01 17:50:57 +0800840 if (fence)
841 entries[i].fence = fence;
842 else
843 entries[i].fence = dma_fence_get_stub();
844
845 if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
846 dma_fence_is_signaled(entries[i].fence)) {
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700847 if (signaled_count == 0 && idx)
848 *idx = i;
849 signaled_count++;
850 }
851 }
852
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700853 if (signaled_count == count ||
854 (signaled_count > 0 &&
855 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
856 goto cleanup_entries;
857
858 /* There's a very annoying laxness in the dma_fence API here, in
859 * that backends are not required to automatically report when a
860 * fence is signaled prior to fence->ops->enable_signaling() being
861 * called. So here if we fail to match signaled_count, we need to
862 * fallthough and try a 0 timeout wait!
863 */
864
865 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
Christian König61a98b12018-12-11 18:34:41 +0800866 for (i = 0; i < count; ++i)
867 drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700868 }
869
870 do {
871 set_current_state(TASK_INTERRUPTIBLE);
872
873 signaled_count = 0;
874 for (i = 0; i < count; ++i) {
875 fence = entries[i].fence;
876 if (!fence)
877 continue;
878
Chunming Zhou01d6c352019-04-01 17:50:57 +0800879 if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
880 dma_fence_is_signaled(fence) ||
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700881 (!entries[i].fence_cb.func &&
882 dma_fence_add_callback(fence,
883 &entries[i].fence_cb,
884 syncobj_wait_fence_func))) {
885 /* The fence has been signaled */
886 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
887 signaled_count++;
888 } else {
889 if (idx)
890 *idx = i;
891 goto done_waiting;
892 }
893 }
894 }
895
896 if (signaled_count == count)
897 goto done_waiting;
898
899 if (timeout == 0) {
Chris Wilson12fec622018-09-20 21:05:30 +0100900 timeout = -ETIME;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700901 goto done_waiting;
902 }
903
Chris Wilson12fec622018-09-20 21:05:30 +0100904 if (signal_pending(current)) {
905 timeout = -ERESTARTSYS;
906 goto done_waiting;
907 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700908
Chris Wilson12fec622018-09-20 21:05:30 +0100909 timeout = schedule_timeout(timeout);
910 } while (1);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700911
912done_waiting:
913 __set_current_state(TASK_RUNNING);
914
915cleanup_entries:
916 for (i = 0; i < count; ++i) {
Christian König61a98b12018-12-11 18:34:41 +0800917 drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700918 if (entries[i].fence_cb.func)
919 dma_fence_remove_callback(entries[i].fence,
920 &entries[i].fence_cb);
921 dma_fence_put(entries[i].fence);
922 }
923 kfree(entries);
924
Chunming Zhou01d6c352019-04-01 17:50:57 +0800925err_free_points:
926 kfree(points);
927
Chris Wilson12fec622018-09-20 21:05:30 +0100928 return timeout;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700929}
930
Dave Airlie5e60a102017-08-25 10:52:22 -0700931/**
932 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
933 *
934 * @timeout_nsec: timeout nsec component in ns, 0 for poll
935 *
936 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
937 */
Qiang Yu877b372992019-02-25 22:07:16 +0800938signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
Dave Airlie5e60a102017-08-25 10:52:22 -0700939{
940 ktime_t abs_timeout, now;
941 u64 timeout_ns, timeout_jiffies64;
942
943 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
944 if (timeout_nsec == 0)
945 return 0;
946
947 abs_timeout = ns_to_ktime(timeout_nsec);
948 now = ktime_get();
949
950 if (!ktime_after(abs_timeout, now))
951 return 0;
952
953 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
954
955 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
956 /* clamp timeout to avoid infinite timeout */
957 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
958 return MAX_SCHEDULE_TIMEOUT - 1;
959
960 return timeout_jiffies64 + 1;
961}
Qiang Yu877b372992019-02-25 22:07:16 +0800962EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
Dave Airlie5e60a102017-08-25 10:52:22 -0700963
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700964static int drm_syncobj_array_wait(struct drm_device *dev,
965 struct drm_file *file_private,
966 struct drm_syncobj_wait *wait,
Chunming Zhou01d6c352019-04-01 17:50:57 +0800967 struct drm_syncobj_timeline_wait *timeline_wait,
968 struct drm_syncobj **syncobjs, bool timeline)
Dave Airlie5e60a102017-08-25 10:52:22 -0700969{
Chunming Zhou01d6c352019-04-01 17:50:57 +0800970 signed long timeout = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -0700971 uint32_t first = ~0;
972
Chunming Zhou01d6c352019-04-01 17:50:57 +0800973 if (!timeline) {
974 timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
975 timeout = drm_syncobj_array_wait_timeout(syncobjs,
976 NULL,
977 wait->count_handles,
978 wait->flags,
979 timeout, &first);
980 if (timeout < 0)
981 return timeout;
982 wait->first_signaled = first;
983 } else {
984 timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec);
985 timeout = drm_syncobj_array_wait_timeout(syncobjs,
986 u64_to_user_ptr(timeline_wait->points),
987 timeline_wait->count_handles,
988 timeline_wait->flags,
989 timeout, &first);
990 if (timeout < 0)
991 return timeout;
992 timeline_wait->first_signaled = first;
993 }
Dave Airlie5e60a102017-08-25 10:52:22 -0700994 return 0;
995}
996
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700997static int drm_syncobj_array_find(struct drm_file *file_private,
Ville Syrjälä9e554462017-09-01 19:53:26 +0300998 void __user *user_handles,
999 uint32_t count_handles,
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001000 struct drm_syncobj ***syncobjs_out)
1001{
1002 uint32_t i, *handles;
1003 struct drm_syncobj **syncobjs;
1004 int ret;
1005
1006 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1007 if (handles == NULL)
1008 return -ENOMEM;
1009
1010 if (copy_from_user(handles, user_handles,
1011 sizeof(uint32_t) * count_handles)) {
1012 ret = -EFAULT;
1013 goto err_free_handles;
1014 }
1015
1016 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1017 if (syncobjs == NULL) {
1018 ret = -ENOMEM;
1019 goto err_free_handles;
1020 }
1021
1022 for (i = 0; i < count_handles; i++) {
1023 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1024 if (!syncobjs[i]) {
1025 ret = -ENOENT;
1026 goto err_put_syncobjs;
1027 }
1028 }
1029
1030 kfree(handles);
1031 *syncobjs_out = syncobjs;
1032 return 0;
1033
1034err_put_syncobjs:
1035 while (i-- > 0)
1036 drm_syncobj_put(syncobjs[i]);
1037 kfree(syncobjs);
1038err_free_handles:
1039 kfree(handles);
1040
1041 return ret;
1042}
1043
1044static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1045 uint32_t count)
1046{
1047 uint32_t i;
1048 for (i = 0; i < count; i++)
1049 drm_syncobj_put(syncobjs[i]);
1050 kfree(syncobjs);
1051}
1052
Dave Airlie5e60a102017-08-25 10:52:22 -07001053int
1054drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1055 struct drm_file *file_private)
1056{
1057 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001058 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -07001059 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -07001060
1061 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001062 return -EOPNOTSUPP;
Dave Airlie5e60a102017-08-25 10:52:22 -07001063
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001064 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1065 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -07001066 return -EINVAL;
1067
1068 if (args->count_handles == 0)
1069 return -EINVAL;
1070
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001071 ret = drm_syncobj_array_find(file_private,
1072 u64_to_user_ptr(args->handles),
1073 args->count_handles,
1074 &syncobjs);
1075 if (ret < 0)
1076 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -07001077
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001078 ret = drm_syncobj_array_wait(dev, file_private,
Chunming Zhou01d6c352019-04-01 17:50:57 +08001079 args, NULL, syncobjs, false);
Dave Airlie5e60a102017-08-25 10:52:22 -07001080
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001081 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -07001082
1083 return ret;
1084}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001085
1086int
Chunming Zhou01d6c352019-04-01 17:50:57 +08001087drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,
1088 struct drm_file *file_private)
1089{
1090 struct drm_syncobj_timeline_wait *args = data;
1091 struct drm_syncobj **syncobjs;
1092 int ret = 0;
1093
Lionel Landwerlin060cebb2019-04-16 13:57:50 +01001094 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
Lionel Landwerlin5ec77632019-04-16 13:30:47 +01001095 return -EOPNOTSUPP;
Chunming Zhou01d6c352019-04-01 17:50:57 +08001096
1097 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1098 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1099 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
1100 return -EINVAL;
1101
1102 if (args->count_handles == 0)
1103 return -EINVAL;
1104
1105 ret = drm_syncobj_array_find(file_private,
1106 u64_to_user_ptr(args->handles),
1107 args->count_handles,
1108 &syncobjs);
1109 if (ret < 0)
1110 return ret;
1111
1112 ret = drm_syncobj_array_wait(dev, file_private,
1113 NULL, args, syncobjs, true);
1114
1115 drm_syncobj_array_free(syncobjs, args->count_handles);
1116
1117 return ret;
1118}
1119
1120
1121int
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001122drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1123 struct drm_file *file_private)
1124{
1125 struct drm_syncobj_array *args = data;
1126 struct drm_syncobj **syncobjs;
1127 uint32_t i;
1128 int ret;
1129
1130 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001131 return -EOPNOTSUPP;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001132
1133 if (args->pad != 0)
1134 return -EINVAL;
1135
1136 if (args->count_handles == 0)
1137 return -EINVAL;
1138
1139 ret = drm_syncobj_array_find(file_private,
1140 u64_to_user_ptr(args->handles),
1141 args->count_handles,
1142 &syncobjs);
1143 if (ret < 0)
1144 return ret;
1145
Eric Anholt131280a2018-11-08 08:04:22 -08001146 for (i = 0; i < args->count_handles; i++)
Christian König0b258ed2018-11-14 14:24:27 +01001147 drm_syncobj_replace_fence(syncobjs[i], NULL);
Eric Anholt131280a2018-11-08 08:04:22 -08001148
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001149 drm_syncobj_array_free(syncobjs, args->count_handles);
1150
Eric Anholt131280a2018-11-08 08:04:22 -08001151 return 0;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001152}
Jason Ekstrandffa94432017-08-28 14:10:28 -07001153
1154int
1155drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1156 struct drm_file *file_private)
1157{
1158 struct drm_syncobj_array *args = data;
1159 struct drm_syncobj **syncobjs;
1160 uint32_t i;
1161 int ret;
1162
1163 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001164 return -EOPNOTSUPP;
Jason Ekstrandffa94432017-08-28 14:10:28 -07001165
1166 if (args->pad != 0)
1167 return -EINVAL;
1168
1169 if (args->count_handles == 0)
1170 return -EINVAL;
1171
1172 ret = drm_syncobj_array_find(file_private,
1173 u64_to_user_ptr(args->handles),
1174 args->count_handles,
1175 &syncobjs);
1176 if (ret < 0)
1177 return ret;
1178
Christian König86bbd892018-11-13 14:14:00 +01001179 for (i = 0; i < args->count_handles; i++)
1180 drm_syncobj_assign_null_handle(syncobjs[i]);
Jason Ekstrandffa94432017-08-28 14:10:28 -07001181
1182 drm_syncobj_array_free(syncobjs, args->count_handles);
1183
1184 return ret;
1185}
Chunming Zhou27b575a2019-04-01 17:50:58 +08001186
Chunming Zhou50d1ebe2019-04-01 17:51:02 +08001187int
1188drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
1189 struct drm_file *file_private)
1190{
1191 struct drm_syncobj_timeline_array *args = data;
1192 struct drm_syncobj **syncobjs;
1193 struct dma_fence_chain **chains;
1194 uint64_t *points;
1195 uint32_t i, j;
1196 int ret;
1197
Lionel Landwerlin060cebb2019-04-16 13:57:50 +01001198 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
Chunming Zhou50d1ebe2019-04-01 17:51:02 +08001199 return -EOPNOTSUPP;
1200
1201 if (args->pad != 0)
1202 return -EINVAL;
1203
1204 if (args->count_handles == 0)
1205 return -EINVAL;
1206
1207 ret = drm_syncobj_array_find(file_private,
1208 u64_to_user_ptr(args->handles),
1209 args->count_handles,
1210 &syncobjs);
1211 if (ret < 0)
1212 return ret;
1213
1214 points = kmalloc_array(args->count_handles, sizeof(*points),
1215 GFP_KERNEL);
1216 if (!points) {
1217 ret = -ENOMEM;
1218 goto out;
1219 }
1220 if (!u64_to_user_ptr(args->points)) {
1221 memset(points, 0, args->count_handles * sizeof(uint64_t));
1222 } else if (copy_from_user(points, u64_to_user_ptr(args->points),
1223 sizeof(uint64_t) * args->count_handles)) {
1224 ret = -EFAULT;
1225 goto err_points;
1226 }
1227
1228 chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL);
1229 if (!chains) {
1230 ret = -ENOMEM;
1231 goto err_points;
1232 }
1233 for (i = 0; i < args->count_handles; i++) {
1234 chains[i] = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
1235 if (!chains[i]) {
1236 for (j = 0; j < i; j++)
1237 kfree(chains[j]);
1238 ret = -ENOMEM;
1239 goto err_chains;
1240 }
1241 }
1242
1243 for (i = 0; i < args->count_handles; i++) {
1244 struct dma_fence *fence = dma_fence_get_stub();
1245
1246 drm_syncobj_add_point(syncobjs[i], chains[i],
1247 fence, points[i]);
1248 dma_fence_put(fence);
1249 }
1250err_chains:
1251 kfree(chains);
1252err_points:
1253 kfree(points);
1254out:
1255 drm_syncobj_array_free(syncobjs, args->count_handles);
1256
1257 return ret;
1258}
1259
Chunming Zhou27b575a2019-04-01 17:50:58 +08001260int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,
1261 struct drm_file *file_private)
1262{
1263 struct drm_syncobj_timeline_array *args = data;
1264 struct drm_syncobj **syncobjs;
1265 uint64_t __user *points = u64_to_user_ptr(args->points);
1266 uint32_t i;
1267 int ret;
1268
Lionel Landwerlin060cebb2019-04-16 13:57:50 +01001269 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1270 return -EOPNOTSUPP;
Chunming Zhou27b575a2019-04-01 17:50:58 +08001271
1272 if (args->pad != 0)
1273 return -EINVAL;
1274
1275 if (args->count_handles == 0)
1276 return -EINVAL;
1277
1278 ret = drm_syncobj_array_find(file_private,
1279 u64_to_user_ptr(args->handles),
1280 args->count_handles,
1281 &syncobjs);
1282 if (ret < 0)
1283 return ret;
1284
1285 for (i = 0; i < args->count_handles; i++) {
1286 struct dma_fence_chain *chain;
1287 struct dma_fence *fence;
1288 uint64_t point;
1289
1290 fence = drm_syncobj_fence_get(syncobjs[i]);
1291 chain = to_dma_fence_chain(fence);
1292 if (chain) {
1293 struct dma_fence *iter, *last_signaled = NULL;
1294
1295 dma_fence_chain_for_each(iter, fence) {
1296 if (!iter)
1297 break;
1298 dma_fence_put(last_signaled);
1299 last_signaled = dma_fence_get(iter);
1300 if (!to_dma_fence_chain(last_signaled)->prev_seqno)
1301 /* It is most likely that timeline has
1302 * unorder points. */
1303 break;
1304 }
1305 point = dma_fence_is_signaled(last_signaled) ?
1306 last_signaled->seqno :
1307 to_dma_fence_chain(last_signaled)->prev_seqno;
1308 dma_fence_put(last_signaled);
1309 } else {
1310 point = 0;
1311 }
1312 ret = copy_to_user(&points[i], &point, sizeof(uint64_t));
1313 ret = ret ? -EFAULT : 0;
1314 if (ret)
1315 break;
1316 }
1317 drm_syncobj_array_free(syncobjs, args->count_handles);
1318
1319 return ret;
1320}