]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/drm_syncobj.c
drm/syncobj: add new drm_syncobj_add_point interface v4
[linux.git] / drivers / gpu / drm / drm_syncobj.c
1 /*
2  * Copyright 2017 Red Hat
3  * Parts ported from amdgpu (fence wait code).
4  * Copyright 2016 Advanced Micro Devices, Inc.
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  * 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  *
32  * 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.
35  *
36  * syncobj's can be waited upon, where it will wait for the underlying
37  * fence.
38  *
39  * 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>
53 #include <linux/sync_file.h>
54 #include <linux/sched/signal.h>
55
56 #include "drm_internal.h"
57 #include <drm/drm_syncobj.h>
58
59 struct 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;
64 };
65
66 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
67                                       struct syncobj_wait_entry *wait);
68
69 /**
70  * drm_syncobj_find - lookup and reference a sync object.
71  * @file_private: drm file private pointer
72  * @handle: sync object handle to lookup.
73  *
74  * Returns a reference to the syncobj pointed to by handle or NULL. The
75  * reference must be released by calling drm_syncobj_put().
76  */
77 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
78                                      u32 handle)
79 {
80         struct drm_syncobj *syncobj;
81
82         spin_lock(&file_private->syncobj_table_lock);
83
84         /* Check if we currently have a reference on the object */
85         syncobj = idr_find(&file_private->syncobj_idr, handle);
86         if (syncobj)
87                 drm_syncobj_get(syncobj);
88
89         spin_unlock(&file_private->syncobj_table_lock);
90
91         return syncobj;
92 }
93 EXPORT_SYMBOL(drm_syncobj_find);
94
95 static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
96                                        struct syncobj_wait_entry *wait)
97 {
98         if (wait->fence)
99                 return;
100
101         spin_lock(&syncobj->lock);
102         /* We've already tried once to get a fence and failed.  Now that we
103          * have the lock, try one more time just to be sure we don't add a
104          * callback when a fence has already been set.
105          */
106         if (syncobj->fence)
107                 wait->fence = dma_fence_get(
108                         rcu_dereference_protected(syncobj->fence, 1));
109         else
110                 list_add_tail(&wait->node, &syncobj->cb_list);
111         spin_unlock(&syncobj->lock);
112 }
113
114 static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
115                                     struct syncobj_wait_entry *wait)
116 {
117         if (!wait->node.next)
118                 return;
119
120         spin_lock(&syncobj->lock);
121         list_del_init(&wait->node);
122         spin_unlock(&syncobj->lock);
123 }
124
125 /**
126  * drm_syncobj_add_point - add new timeline point to the syncobj
127  * @syncobj: sync object to add timeline point do
128  * @chain: chain node to use to add the point
129  * @fence: fence to encapsulate in the chain node
130  * @point: sequence number to use for the point
131  *
132  * Add the chain node as new timeline point to the syncobj.
133  */
134 void drm_syncobj_add_point(struct drm_syncobj *syncobj,
135                            struct dma_fence_chain *chain,
136                            struct dma_fence *fence,
137                            uint64_t point)
138 {
139         struct syncobj_wait_entry *cur, *tmp;
140         struct dma_fence *prev;
141
142         dma_fence_get(fence);
143
144         spin_lock(&syncobj->lock);
145
146         prev = drm_syncobj_fence_get(syncobj);
147         /* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
148         if (prev && prev->seqno >= point)
149                 DRM_ERROR("You are adding an unorder point to timeline!\n");
150         dma_fence_chain_init(chain, prev, fence, point);
151         rcu_assign_pointer(syncobj->fence, &chain->base);
152
153         list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
154                 list_del_init(&cur->node);
155                 syncobj_wait_syncobj_func(syncobj, cur);
156         }
157         spin_unlock(&syncobj->lock);
158
159         /* Walk the chain once to trigger garbage collection */
160         dma_fence_chain_for_each(fence, prev);
161         dma_fence_put(prev);
162 }
163 EXPORT_SYMBOL(drm_syncobj_add_point);
164
165 /**
166  * drm_syncobj_replace_fence - replace fence in a sync object.
167  * @syncobj: Sync object to replace fence in
168  * @fence: fence to install in sync file.
169  *
170  * This replaces the fence on a sync object.
171  */
172 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
173                                struct dma_fence *fence)
174 {
175         struct dma_fence *old_fence;
176         struct syncobj_wait_entry *cur, *tmp;
177
178         if (fence)
179                 dma_fence_get(fence);
180
181         spin_lock(&syncobj->lock);
182
183         old_fence = rcu_dereference_protected(syncobj->fence,
184                                               lockdep_is_held(&syncobj->lock));
185         rcu_assign_pointer(syncobj->fence, fence);
186
187         if (fence != old_fence) {
188                 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
189                         list_del_init(&cur->node);
190                         syncobj_wait_syncobj_func(syncobj, cur);
191                 }
192         }
193
194         spin_unlock(&syncobj->lock);
195
196         dma_fence_put(old_fence);
197 }
198 EXPORT_SYMBOL(drm_syncobj_replace_fence);
199
200 /**
201  * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
202  * @syncobj: sync object to assign the fence on
203  *
204  * Assign a already signaled stub fence to the sync object.
205  */
206 static void drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
207 {
208         struct dma_fence *fence = dma_fence_get_stub();
209
210         drm_syncobj_replace_fence(syncobj, fence);
211         dma_fence_put(fence);
212 }
213
214 /**
215  * drm_syncobj_find_fence - lookup and reference the fence in a sync object
216  * @file_private: drm file private pointer
217  * @handle: sync object handle to lookup.
218  * @point: timeline point
219  * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
220  * @fence: out parameter for the fence
221  *
222  * This is just a convenience function that combines drm_syncobj_find() and
223  * drm_syncobj_fence_get().
224  *
225  * Returns 0 on success or a negative error value on failure. On success @fence
226  * contains a reference to the fence, which must be released by calling
227  * dma_fence_put().
228  */
229 int drm_syncobj_find_fence(struct drm_file *file_private,
230                            u32 handle, u64 point, u64 flags,
231                            struct dma_fence **fence)
232 {
233         struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
234         int ret = 0;
235
236         if (!syncobj)
237                 return -ENOENT;
238
239         *fence = drm_syncobj_fence_get(syncobj);
240         if (!*fence) {
241                 ret = -EINVAL;
242         }
243         drm_syncobj_put(syncobj);
244         return ret;
245 }
246 EXPORT_SYMBOL(drm_syncobj_find_fence);
247
248 /**
249  * drm_syncobj_free - free a sync object.
250  * @kref: kref to free.
251  *
252  * Only to be called from kref_put in drm_syncobj_put.
253  */
254 void drm_syncobj_free(struct kref *kref)
255 {
256         struct drm_syncobj *syncobj = container_of(kref,
257                                                    struct drm_syncobj,
258                                                    refcount);
259         drm_syncobj_replace_fence(syncobj, NULL);
260         kfree(syncobj);
261 }
262 EXPORT_SYMBOL(drm_syncobj_free);
263
264 /**
265  * drm_syncobj_create - create a new syncobj
266  * @out_syncobj: returned syncobj
267  * @flags: DRM_SYNCOBJ_* flags
268  * @fence: if non-NULL, the syncobj will represent this fence
269  *
270  * This is the first function to create a sync object. After creating, drivers
271  * probably want to make it available to userspace, either through
272  * drm_syncobj_get_handle() or drm_syncobj_get_fd().
273  *
274  * Returns 0 on success or a negative error value on failure.
275  */
276 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
277                        struct dma_fence *fence)
278 {
279         struct drm_syncobj *syncobj;
280
281         syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
282         if (!syncobj)
283                 return -ENOMEM;
284
285         kref_init(&syncobj->refcount);
286         INIT_LIST_HEAD(&syncobj->cb_list);
287         spin_lock_init(&syncobj->lock);
288
289         if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
290                 drm_syncobj_assign_null_handle(syncobj);
291
292         if (fence)
293                 drm_syncobj_replace_fence(syncobj, fence);
294
295         *out_syncobj = syncobj;
296         return 0;
297 }
298 EXPORT_SYMBOL(drm_syncobj_create);
299
300 /**
301  * drm_syncobj_get_handle - get a handle from a syncobj
302  * @file_private: drm file private pointer
303  * @syncobj: Sync object to export
304  * @handle: out parameter with the new handle
305  *
306  * Exports a sync object created with drm_syncobj_create() as a handle on
307  * @file_private to userspace.
308  *
309  * Returns 0 on success or a negative error value on failure.
310  */
311 int drm_syncobj_get_handle(struct drm_file *file_private,
312                            struct drm_syncobj *syncobj, u32 *handle)
313 {
314         int ret;
315
316         /* take a reference to put in the idr */
317         drm_syncobj_get(syncobj);
318
319         idr_preload(GFP_KERNEL);
320         spin_lock(&file_private->syncobj_table_lock);
321         ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
322         spin_unlock(&file_private->syncobj_table_lock);
323
324         idr_preload_end();
325
326         if (ret < 0) {
327                 drm_syncobj_put(syncobj);
328                 return ret;
329         }
330
331         *handle = ret;
332         return 0;
333 }
334 EXPORT_SYMBOL(drm_syncobj_get_handle);
335
336 static int drm_syncobj_create_as_handle(struct drm_file *file_private,
337                                         u32 *handle, uint32_t flags)
338 {
339         int ret;
340         struct drm_syncobj *syncobj;
341
342         ret = drm_syncobj_create(&syncobj, flags, NULL);
343         if (ret)
344                 return ret;
345
346         ret = drm_syncobj_get_handle(file_private, syncobj, handle);
347         drm_syncobj_put(syncobj);
348         return ret;
349 }
350
351 static int drm_syncobj_destroy(struct drm_file *file_private,
352                                u32 handle)
353 {
354         struct drm_syncobj *syncobj;
355
356         spin_lock(&file_private->syncobj_table_lock);
357         syncobj = idr_remove(&file_private->syncobj_idr, handle);
358         spin_unlock(&file_private->syncobj_table_lock);
359
360         if (!syncobj)
361                 return -EINVAL;
362
363         drm_syncobj_put(syncobj);
364         return 0;
365 }
366
367 static int drm_syncobj_file_release(struct inode *inode, struct file *file)
368 {
369         struct drm_syncobj *syncobj = file->private_data;
370
371         drm_syncobj_put(syncobj);
372         return 0;
373 }
374
375 static const struct file_operations drm_syncobj_file_fops = {
376         .release = drm_syncobj_file_release,
377 };
378
379 /**
380  * drm_syncobj_get_fd - get a file descriptor from a syncobj
381  * @syncobj: Sync object to export
382  * @p_fd: out parameter with the new file descriptor
383  *
384  * Exports a sync object created with drm_syncobj_create() as a file descriptor.
385  *
386  * Returns 0 on success or a negative error value on failure.
387  */
388 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
389 {
390         struct file *file;
391         int fd;
392
393         fd = get_unused_fd_flags(O_CLOEXEC);
394         if (fd < 0)
395                 return fd;
396
397         file = anon_inode_getfile("syncobj_file",
398                                   &drm_syncobj_file_fops,
399                                   syncobj, 0);
400         if (IS_ERR(file)) {
401                 put_unused_fd(fd);
402                 return PTR_ERR(file);
403         }
404
405         drm_syncobj_get(syncobj);
406         fd_install(fd, file);
407
408         *p_fd = fd;
409         return 0;
410 }
411 EXPORT_SYMBOL(drm_syncobj_get_fd);
412
413 static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
414                                     u32 handle, int *p_fd)
415 {
416         struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
417         int ret;
418
419         if (!syncobj)
420                 return -EINVAL;
421
422         ret = drm_syncobj_get_fd(syncobj, p_fd);
423         drm_syncobj_put(syncobj);
424         return ret;
425 }
426
427 static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
428                                     int fd, u32 *handle)
429 {
430         struct drm_syncobj *syncobj;
431         struct file *file;
432         int ret;
433
434         file = fget(fd);
435         if (!file)
436                 return -EINVAL;
437
438         if (file->f_op != &drm_syncobj_file_fops) {
439                 fput(file);
440                 return -EINVAL;
441         }
442
443         /* take a reference to put in the idr */
444         syncobj = file->private_data;
445         drm_syncobj_get(syncobj);
446
447         idr_preload(GFP_KERNEL);
448         spin_lock(&file_private->syncobj_table_lock);
449         ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
450         spin_unlock(&file_private->syncobj_table_lock);
451         idr_preload_end();
452
453         if (ret > 0) {
454                 *handle = ret;
455                 ret = 0;
456         } else
457                 drm_syncobj_put(syncobj);
458
459         fput(file);
460         return ret;
461 }
462
463 static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
464                                               int fd, int handle)
465 {
466         struct dma_fence *fence = sync_file_get_fence(fd);
467         struct drm_syncobj *syncobj;
468
469         if (!fence)
470                 return -EINVAL;
471
472         syncobj = drm_syncobj_find(file_private, handle);
473         if (!syncobj) {
474                 dma_fence_put(fence);
475                 return -ENOENT;
476         }
477
478         drm_syncobj_replace_fence(syncobj, fence);
479         dma_fence_put(fence);
480         drm_syncobj_put(syncobj);
481         return 0;
482 }
483
484 static int drm_syncobj_export_sync_file(struct drm_file *file_private,
485                                         int handle, int *p_fd)
486 {
487         int ret;
488         struct dma_fence *fence;
489         struct sync_file *sync_file;
490         int fd = get_unused_fd_flags(O_CLOEXEC);
491
492         if (fd < 0)
493                 return fd;
494
495         ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
496         if (ret)
497                 goto err_put_fd;
498
499         sync_file = sync_file_create(fence);
500
501         dma_fence_put(fence);
502
503         if (!sync_file) {
504                 ret = -EINVAL;
505                 goto err_put_fd;
506         }
507
508         fd_install(fd, sync_file->file);
509
510         *p_fd = fd;
511         return 0;
512 err_put_fd:
513         put_unused_fd(fd);
514         return ret;
515 }
516 /**
517  * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
518  * @file_private: drm file-private structure to set up
519  *
520  * Called at device open time, sets up the structure for handling refcounting
521  * of sync objects.
522  */
523 void
524 drm_syncobj_open(struct drm_file *file_private)
525 {
526         idr_init_base(&file_private->syncobj_idr, 1);
527         spin_lock_init(&file_private->syncobj_table_lock);
528 }
529
530 static int
531 drm_syncobj_release_handle(int id, void *ptr, void *data)
532 {
533         struct drm_syncobj *syncobj = ptr;
534
535         drm_syncobj_put(syncobj);
536         return 0;
537 }
538
539 /**
540  * drm_syncobj_release - release file-private sync object resources
541  * @file_private: drm file-private structure to clean up
542  *
543  * Called at close time when the filp is going away.
544  *
545  * Releases any remaining references on objects by this filp.
546  */
547 void
548 drm_syncobj_release(struct drm_file *file_private)
549 {
550         idr_for_each(&file_private->syncobj_idr,
551                      &drm_syncobj_release_handle, file_private);
552         idr_destroy(&file_private->syncobj_idr);
553 }
554
555 int
556 drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
557                          struct drm_file *file_private)
558 {
559         struct drm_syncobj_create *args = data;
560
561         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
562                 return -EOPNOTSUPP;
563
564         /* no valid flags yet */
565         if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
566                 return -EINVAL;
567
568         return drm_syncobj_create_as_handle(file_private,
569                                             &args->handle, args->flags);
570 }
571
572 int
573 drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
574                           struct drm_file *file_private)
575 {
576         struct drm_syncobj_destroy *args = data;
577
578         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
579                 return -EOPNOTSUPP;
580
581         /* make sure padding is empty */
582         if (args->pad)
583                 return -EINVAL;
584         return drm_syncobj_destroy(file_private, args->handle);
585 }
586
587 int
588 drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
589                                    struct drm_file *file_private)
590 {
591         struct drm_syncobj_handle *args = data;
592
593         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
594                 return -EOPNOTSUPP;
595
596         if (args->pad)
597                 return -EINVAL;
598
599         if (args->flags != 0 &&
600             args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
601                 return -EINVAL;
602
603         if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
604                 return drm_syncobj_export_sync_file(file_private, args->handle,
605                                                     &args->fd);
606
607         return drm_syncobj_handle_to_fd(file_private, args->handle,
608                                         &args->fd);
609 }
610
611 int
612 drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
613                                    struct drm_file *file_private)
614 {
615         struct drm_syncobj_handle *args = data;
616
617         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
618                 return -EOPNOTSUPP;
619
620         if (args->pad)
621                 return -EINVAL;
622
623         if (args->flags != 0 &&
624             args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
625                 return -EINVAL;
626
627         if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
628                 return drm_syncobj_import_sync_file_fence(file_private,
629                                                           args->fd,
630                                                           args->handle);
631
632         return drm_syncobj_fd_to_handle(file_private, args->fd,
633                                         &args->handle);
634 }
635
636 static void syncobj_wait_fence_func(struct dma_fence *fence,
637                                     struct dma_fence_cb *cb)
638 {
639         struct syncobj_wait_entry *wait =
640                 container_of(cb, struct syncobj_wait_entry, fence_cb);
641
642         wake_up_process(wait->task);
643 }
644
645 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
646                                       struct syncobj_wait_entry *wait)
647 {
648         /* This happens inside the syncobj lock */
649         wait->fence = dma_fence_get(rcu_dereference_protected(syncobj->fence,
650                                                               lockdep_is_held(&syncobj->lock)));
651         wake_up_process(wait->task);
652 }
653
654 static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
655                                                   uint32_t count,
656                                                   uint32_t flags,
657                                                   signed long timeout,
658                                                   uint32_t *idx)
659 {
660         struct syncobj_wait_entry *entries;
661         struct dma_fence *fence;
662         uint32_t signaled_count, i;
663
664         entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
665         if (!entries)
666                 return -ENOMEM;
667
668         /* Walk the list of sync objects and initialize entries.  We do
669          * this up-front so that we can properly return -EINVAL if there is
670          * a syncobj with a missing fence and then never have the chance of
671          * returning -EINVAL again.
672          */
673         signaled_count = 0;
674         for (i = 0; i < count; ++i) {
675                 entries[i].task = current;
676                 entries[i].fence = drm_syncobj_fence_get(syncobjs[i]);
677                 if (!entries[i].fence) {
678                         if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
679                                 continue;
680                         } else {
681                                 timeout = -EINVAL;
682                                 goto cleanup_entries;
683                         }
684                 }
685
686                 if (dma_fence_is_signaled(entries[i].fence)) {
687                         if (signaled_count == 0 && idx)
688                                 *idx = i;
689                         signaled_count++;
690                 }
691         }
692
693         if (signaled_count == count ||
694             (signaled_count > 0 &&
695              !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
696                 goto cleanup_entries;
697
698         /* There's a very annoying laxness in the dma_fence API here, in
699          * that backends are not required to automatically report when a
700          * fence is signaled prior to fence->ops->enable_signaling() being
701          * called.  So here if we fail to match signaled_count, we need to
702          * fallthough and try a 0 timeout wait!
703          */
704
705         if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
706                 for (i = 0; i < count; ++i)
707                         drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
708         }
709
710         do {
711                 set_current_state(TASK_INTERRUPTIBLE);
712
713                 signaled_count = 0;
714                 for (i = 0; i < count; ++i) {
715                         fence = entries[i].fence;
716                         if (!fence)
717                                 continue;
718
719                         if (dma_fence_is_signaled(fence) ||
720                             (!entries[i].fence_cb.func &&
721                              dma_fence_add_callback(fence,
722                                                     &entries[i].fence_cb,
723                                                     syncobj_wait_fence_func))) {
724                                 /* The fence has been signaled */
725                                 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
726                                         signaled_count++;
727                                 } else {
728                                         if (idx)
729                                                 *idx = i;
730                                         goto done_waiting;
731                                 }
732                         }
733                 }
734
735                 if (signaled_count == count)
736                         goto done_waiting;
737
738                 if (timeout == 0) {
739                         timeout = -ETIME;
740                         goto done_waiting;
741                 }
742
743                 if (signal_pending(current)) {
744                         timeout = -ERESTARTSYS;
745                         goto done_waiting;
746                 }
747
748                 timeout = schedule_timeout(timeout);
749         } while (1);
750
751 done_waiting:
752         __set_current_state(TASK_RUNNING);
753
754 cleanup_entries:
755         for (i = 0; i < count; ++i) {
756                 drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
757                 if (entries[i].fence_cb.func)
758                         dma_fence_remove_callback(entries[i].fence,
759                                                   &entries[i].fence_cb);
760                 dma_fence_put(entries[i].fence);
761         }
762         kfree(entries);
763
764         return timeout;
765 }
766
767 /**
768  * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
769  *
770  * @timeout_nsec: timeout nsec component in ns, 0 for poll
771  *
772  * Calculate the timeout in jiffies from an absolute time in sec/nsec.
773  */
774 signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
775 {
776         ktime_t abs_timeout, now;
777         u64 timeout_ns, timeout_jiffies64;
778
779         /* make 0 timeout means poll - absolute 0 doesn't seem valid */
780         if (timeout_nsec == 0)
781                 return 0;
782
783         abs_timeout = ns_to_ktime(timeout_nsec);
784         now = ktime_get();
785
786         if (!ktime_after(abs_timeout, now))
787                 return 0;
788
789         timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
790
791         timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
792         /*  clamp timeout to avoid infinite timeout */
793         if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
794                 return MAX_SCHEDULE_TIMEOUT - 1;
795
796         return timeout_jiffies64 + 1;
797 }
798 EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
799
800 static int drm_syncobj_array_wait(struct drm_device *dev,
801                                   struct drm_file *file_private,
802                                   struct drm_syncobj_wait *wait,
803                                   struct drm_syncobj **syncobjs)
804 {
805         signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
806         uint32_t first = ~0;
807
808         timeout = drm_syncobj_array_wait_timeout(syncobjs,
809                                                  wait->count_handles,
810                                                  wait->flags,
811                                                  timeout, &first);
812         if (timeout < 0)
813                 return timeout;
814
815         wait->first_signaled = first;
816         return 0;
817 }
818
819 static int drm_syncobj_array_find(struct drm_file *file_private,
820                                   void __user *user_handles,
821                                   uint32_t count_handles,
822                                   struct drm_syncobj ***syncobjs_out)
823 {
824         uint32_t i, *handles;
825         struct drm_syncobj **syncobjs;
826         int ret;
827
828         handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
829         if (handles == NULL)
830                 return -ENOMEM;
831
832         if (copy_from_user(handles, user_handles,
833                            sizeof(uint32_t) * count_handles)) {
834                 ret = -EFAULT;
835                 goto err_free_handles;
836         }
837
838         syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
839         if (syncobjs == NULL) {
840                 ret = -ENOMEM;
841                 goto err_free_handles;
842         }
843
844         for (i = 0; i < count_handles; i++) {
845                 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
846                 if (!syncobjs[i]) {
847                         ret = -ENOENT;
848                         goto err_put_syncobjs;
849                 }
850         }
851
852         kfree(handles);
853         *syncobjs_out = syncobjs;
854         return 0;
855
856 err_put_syncobjs:
857         while (i-- > 0)
858                 drm_syncobj_put(syncobjs[i]);
859         kfree(syncobjs);
860 err_free_handles:
861         kfree(handles);
862
863         return ret;
864 }
865
866 static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
867                                    uint32_t count)
868 {
869         uint32_t i;
870         for (i = 0; i < count; i++)
871                 drm_syncobj_put(syncobjs[i]);
872         kfree(syncobjs);
873 }
874
875 int
876 drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
877                        struct drm_file *file_private)
878 {
879         struct drm_syncobj_wait *args = data;
880         struct drm_syncobj **syncobjs;
881         int ret = 0;
882
883         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
884                 return -EOPNOTSUPP;
885
886         if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
887                             DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
888                 return -EINVAL;
889
890         if (args->count_handles == 0)
891                 return -EINVAL;
892
893         ret = drm_syncobj_array_find(file_private,
894                                      u64_to_user_ptr(args->handles),
895                                      args->count_handles,
896                                      &syncobjs);
897         if (ret < 0)
898                 return ret;
899
900         ret = drm_syncobj_array_wait(dev, file_private,
901                                      args, syncobjs);
902
903         drm_syncobj_array_free(syncobjs, args->count_handles);
904
905         return ret;
906 }
907
908 int
909 drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
910                         struct drm_file *file_private)
911 {
912         struct drm_syncobj_array *args = data;
913         struct drm_syncobj **syncobjs;
914         uint32_t i;
915         int ret;
916
917         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
918                 return -EOPNOTSUPP;
919
920         if (args->pad != 0)
921                 return -EINVAL;
922
923         if (args->count_handles == 0)
924                 return -EINVAL;
925
926         ret = drm_syncobj_array_find(file_private,
927                                      u64_to_user_ptr(args->handles),
928                                      args->count_handles,
929                                      &syncobjs);
930         if (ret < 0)
931                 return ret;
932
933         for (i = 0; i < args->count_handles; i++)
934                 drm_syncobj_replace_fence(syncobjs[i], NULL);
935
936         drm_syncobj_array_free(syncobjs, args->count_handles);
937
938         return 0;
939 }
940
941 int
942 drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
943                          struct drm_file *file_private)
944 {
945         struct drm_syncobj_array *args = data;
946         struct drm_syncobj **syncobjs;
947         uint32_t i;
948         int ret;
949
950         if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
951                 return -EOPNOTSUPP;
952
953         if (args->pad != 0)
954                 return -EINVAL;
955
956         if (args->count_handles == 0)
957                 return -EINVAL;
958
959         ret = drm_syncobj_array_find(file_private,
960                                      u64_to_user_ptr(args->handles),
961                                      args->count_handles,
962                                      &syncobjs);
963         if (ret < 0)
964                 return ret;
965
966         for (i = 0; i < args->count_handles; i++)
967                 drm_syncobj_assign_null_handle(syncobjs[i]);
968
969         drm_syncobj_array_free(syncobjs, args->count_handles);
970
971         return ret;
972 }