xref: /openbsd-src/sys/dev/pci/drm/drm_syncobj.c (revision 09467b48e8bc8b4905716062da846024139afbf2)
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) provide a
33  * container for a synchronization primitive which can be used by userspace
34  * to explicitly synchronize GPU commands, can be shared between userspace
35  * processes, and can be shared between different DRM drivers.
36  * Their primary use-case is to implement Vulkan fences and semaphores.
37  * The syncobj userspace API provides ioctls for several operations:
38  *
39  *  - Creation and destruction of syncobjs
40  *  - Import and export of syncobjs to/from a syncobj file descriptor
41  *  - Import and export a syncobj's underlying fence to/from a sync file
42  *  - Reset a syncobj (set its fence to NULL)
43  *  - Signal a syncobj (set a trivially signaled fence)
44  *  - Wait for a syncobj's fence to appear and be signaled
45  *
46  * The syncobj userspace API also provides operations to manipulate a syncobj
47  * in terms of a timeline of struct &dma_fence_chain rather than a single
48  * struct &dma_fence, through the following operations:
49  *
50  *   - Signal a given point on the timeline
51  *   - Wait for a given point to appear and/or be signaled
52  *   - Import and export from/to a given point of a timeline
53  *
54  * At it's core, a syncobj is simply a wrapper around a pointer to a struct
55  * &dma_fence which may be NULL.
56  * When a syncobj is first created, its pointer is either NULL or a pointer
57  * to an already signaled fence depending on whether the
58  * &DRM_SYNCOBJ_CREATE_SIGNALED flag is passed to
59  * &DRM_IOCTL_SYNCOBJ_CREATE.
60  *
61  * If the syncobj is considered as a binary (its state is either signaled or
62  * unsignaled) primitive, when GPU work is enqueued in a DRM driver to signal
63  * the syncobj, the syncobj's fence is replaced with a fence which will be
64  * signaled by the completion of that work.
65  * If the syncobj is considered as a timeline primitive, when GPU work is
66  * enqueued in a DRM driver to signal the a given point of the syncobj, a new
67  * struct &dma_fence_chain pointing to the DRM driver's fence and also
68  * pointing to the previous fence that was in the syncobj. The new struct
69  * &dma_fence_chain fence replace the syncobj's fence and will be signaled by
70  * completion of the DRM driver's work and also any work associated with the
71  * fence previously in the syncobj.
72  *
73  * When GPU work which waits on a syncobj is enqueued in a DRM driver, at the
74  * time the work is enqueued, it waits on the syncobj's fence before
75  * submitting the work to hardware. That fence is either :
76  *
77  *    - The syncobj's current fence if the syncobj is considered as a binary
78  *      primitive.
79  *    - The struct &dma_fence associated with a given point if the syncobj is
80  *      considered as a timeline primitive.
81  *
82  * If the syncobj's fence is NULL or not present in the syncobj's timeline,
83  * the enqueue operation is expected to fail.
84  *
85  * With binary syncobj, all manipulation of the syncobjs's fence happens in
86  * terms of the current fence at the time the ioctl is called by userspace
87  * regardless of whether that operation is an immediate host-side operation
88  * (signal or reset) or or an operation which is enqueued in some driver
89  * queue. &DRM_IOCTL_SYNCOBJ_RESET and &DRM_IOCTL_SYNCOBJ_SIGNAL can be used
90  * to manipulate a syncobj from the host by resetting its pointer to NULL or
91  * setting its pointer to a fence which is already signaled.
92  *
93  * With a timeline syncobj, all manipulation of the synobj's fence happens in
94  * terms of a u64 value referring to point in the timeline. See
95  * dma_fence_chain_find_seqno() to see how a given point is found in the
96  * timeline.
97  *
98  * Note that applications should be careful to always use timeline set of
99  * ioctl() when dealing with syncobj considered as timeline. Using a binary
100  * set of ioctl() with a syncobj considered as timeline could result incorrect
101  * synchronization. The use of binary syncobj is supported through the
102  * timeline set of ioctl() by using a point value of 0, this will reproduce
103  * the behavior of the binary set of ioctl() (for example replace the
104  * syncobj's fence when signaling).
105  *
106  *
107  * Host-side wait on syncobjs
108  * --------------------------
109  *
110  * &DRM_IOCTL_SYNCOBJ_WAIT takes an array of syncobj handles and does a
111  * host-side wait on all of the syncobj fences simultaneously.
112  * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL is set, the wait ioctl will wait on
113  * all of the syncobj fences to be signaled before it returns.
114  * Otherwise, it returns once at least one syncobj fence has been signaled
115  * and the index of a signaled fence is written back to the client.
116  *
117  * Unlike the enqueued GPU work dependencies which fail if they see a NULL
118  * fence in a syncobj, if &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is set,
119  * the host-side wait will first wait for the syncobj to receive a non-NULL
120  * fence and then wait on that fence.
121  * If &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT is not set and any one of the
122  * syncobjs in the array has a NULL fence, -EINVAL will be returned.
123  * Assuming the syncobj starts off with a NULL fence, this allows a client
124  * to do a host wait in one thread (or process) which waits on GPU work
125  * submitted in another thread (or process) without having to manually
126  * synchronize between the two.
127  * This requirement is inherited from the Vulkan fence API.
128  *
129  * Similarly, &DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT takes an array of syncobj
130  * handles as well as an array of u64 points and does a host-side wait on all
131  * of syncobj fences at the given points simultaneously.
132  *
133  * &DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT also adds the ability to wait for a given
134  * fence to materialize on the timeline without waiting for the fence to be
135  * signaled by using the &DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE flag. This
136  * requirement is inherited from the wait-before-signal behavior required by
137  * the Vulkan timeline semaphore API.
138  *
139  *
140  * Import/export of syncobjs
141  * -------------------------
142  *
143  * &DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE and &DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
144  * provide two mechanisms for import/export of syncobjs.
145  *
146  * The first lets the client import or export an entire syncobj to a file
147  * descriptor.
148  * These fd's are opaque and have no other use case, except passing the
149  * syncobj between processes.
150  * All exported file descriptors and any syncobj handles created as a
151  * result of importing those file descriptors own a reference to the
152  * same underlying struct &drm_syncobj and the syncobj can be used
153  * persistently across all the processes with which it is shared.
154  * The syncobj is freed only once the last reference is dropped.
155  * Unlike dma-buf, importing a syncobj creates a new handle (with its own
156  * reference) for every import instead of de-duplicating.
157  * The primary use-case of this persistent import/export is for shared
158  * Vulkan fences and semaphores.
159  *
160  * The second import/export mechanism, which is indicated by
161  * &DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE or
162  * &DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE lets the client
163  * import/export the syncobj's current fence from/to a &sync_file.
164  * When a syncobj is exported to a sync file, that sync file wraps the
165  * sycnobj's fence at the time of export and any later signal or reset
166  * operations on the syncobj will not affect the exported sync file.
167  * When a sync file is imported into a syncobj, the syncobj's fence is set
168  * to the fence wrapped by that sync file.
169  * Because sync files are immutable, resetting or signaling the syncobj
170  * will not affect any sync files whose fences have been imported into the
171  * syncobj.
172  *
173  *
174  * Import/export of timeline points in timeline syncobjs
175  * -----------------------------------------------------
176  *
177  * &DRM_IOCTL_SYNCOBJ_TRANSFER provides a mechanism to transfer a struct
178  * &dma_fence_chain of a syncobj at a given u64 point to another u64 point
179  * into another syncobj.
180  *
181  * Note that if you want to transfer a struct &dma_fence_chain from a given
182  * point on a timeline syncobj from/into a binary syncobj, you can use the
183  * point 0 to mean take/replace the fence in the syncobj.
184  */
185 
186 #include <linux/anon_inodes.h>
187 #include <linux/file.h>
188 #include <linux/fs.h>
189 #include <linux/sched/signal.h>
190 #include <linux/sync_file.h>
191 #include <linux/uaccess.h>
192 
193 #include <drm/drm.h>
194 #include <drm/drm_drv.h>
195 #include <drm/drm_file.h>
196 #include <drm/drm_gem.h>
197 #include <drm/drm_print.h>
198 #include <drm/drm_syncobj.h>
199 #include <drm/drm_utils.h>
200 
201 #include "drm_internal.h"
202 
203 struct syncobj_wait_entry {
204 	struct list_head node;
205 #ifdef __linux__
206 	struct task_struct *task;
207 #else
208 	struct proc *task;
209 #endif
210 	struct dma_fence *fence;
211 	struct dma_fence_cb fence_cb;
212 	u64    point;
213 };
214 
215 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
216 				      struct syncobj_wait_entry *wait);
217 
218 /**
219  * drm_syncobj_find - lookup and reference a sync object.
220  * @file_private: drm file private pointer
221  * @handle: sync object handle to lookup.
222  *
223  * Returns a reference to the syncobj pointed to by handle or NULL. The
224  * reference must be released by calling drm_syncobj_put().
225  */
226 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
227 				     u32 handle)
228 {
229 	struct drm_syncobj *syncobj;
230 
231 	spin_lock(&file_private->syncobj_table_lock);
232 
233 	/* Check if we currently have a reference on the object */
234 	syncobj = idr_find(&file_private->syncobj_idr, handle);
235 	if (syncobj)
236 		drm_syncobj_get(syncobj);
237 
238 	spin_unlock(&file_private->syncobj_table_lock);
239 
240 	return syncobj;
241 }
242 EXPORT_SYMBOL(drm_syncobj_find);
243 
244 #ifdef notyet
245 
246 static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
247 				       struct syncobj_wait_entry *wait)
248 {
249 	struct dma_fence *fence;
250 
251 	if (wait->fence)
252 		return;
253 
254 	spin_lock(&syncobj->lock);
255 	/* We've already tried once to get a fence and failed.  Now that we
256 	 * have the lock, try one more time just to be sure we don't add a
257 	 * callback when a fence has already been set.
258 	 */
259 	fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
260 	if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
261 		dma_fence_put(fence);
262 		list_add_tail(&wait->node, &syncobj->cb_list);
263 	} else if (!fence) {
264 		wait->fence = dma_fence_get_stub();
265 	} else {
266 		wait->fence = fence;
267 	}
268 	spin_unlock(&syncobj->lock);
269 }
270 
271 static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
272 				    struct syncobj_wait_entry *wait)
273 {
274 	if (!wait->node.next)
275 		return;
276 
277 	spin_lock(&syncobj->lock);
278 	list_del_init(&wait->node);
279 	spin_unlock(&syncobj->lock);
280 }
281 
282 #endif /* notyet */
283 
284 /**
285  * drm_syncobj_add_point - add new timeline point to the syncobj
286  * @syncobj: sync object to add timeline point do
287  * @chain: chain node to use to add the point
288  * @fence: fence to encapsulate in the chain node
289  * @point: sequence number to use for the point
290  *
291  * Add the chain node as new timeline point to the syncobj.
292  */
293 void drm_syncobj_add_point(struct drm_syncobj *syncobj,
294 			   struct dma_fence_chain *chain,
295 			   struct dma_fence *fence,
296 			   uint64_t point)
297 {
298 	STUB();
299 #ifdef notyet
300 	struct syncobj_wait_entry *cur, *tmp;
301 	struct dma_fence *prev;
302 
303 	dma_fence_get(fence);
304 
305 	spin_lock(&syncobj->lock);
306 
307 	prev = drm_syncobj_fence_get(syncobj);
308 	/* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
309 	if (prev && prev->seqno >= point)
310 		DRM_ERROR("You are adding an unorder point to timeline!\n");
311 	dma_fence_chain_init(chain, prev, fence, point);
312 	rcu_assign_pointer(syncobj->fence, &chain->base);
313 
314 	list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
315 		syncobj_wait_syncobj_func(syncobj, cur);
316 	spin_unlock(&syncobj->lock);
317 
318 	/* Walk the chain once to trigger garbage collection */
319 	dma_fence_chain_for_each(fence, prev);
320 	dma_fence_put(prev);
321 #endif
322 }
323 EXPORT_SYMBOL(drm_syncobj_add_point);
324 
325 /**
326  * drm_syncobj_replace_fence - replace fence in a sync object.
327  * @syncobj: Sync object to replace fence in
328  * @fence: fence to install in sync file.
329  *
330  * This replaces the fence on a sync object.
331  */
332 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
333 			       struct dma_fence *fence)
334 {
335 	struct dma_fence *old_fence;
336 	struct syncobj_wait_entry *cur, *tmp;
337 
338 	if (fence)
339 		dma_fence_get(fence);
340 
341 	spin_lock(&syncobj->lock);
342 
343 	old_fence = rcu_dereference_protected(syncobj->fence,
344 					      lockdep_is_held(&syncobj->lock));
345 	rcu_assign_pointer(syncobj->fence, fence);
346 
347 	if (fence != old_fence) {
348 		list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
349 			syncobj_wait_syncobj_func(syncobj, cur);
350 	}
351 
352 	spin_unlock(&syncobj->lock);
353 
354 	dma_fence_put(old_fence);
355 }
356 EXPORT_SYMBOL(drm_syncobj_replace_fence);
357 
358 /**
359  * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
360  * @syncobj: sync object to assign the fence on
361  *
362  * Assign a already signaled stub fence to the sync object.
363  */
364 static void drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
365 {
366 	STUB();
367 #ifdef notyet
368 	struct dma_fence *fence = dma_fence_get_stub();
369 
370 	drm_syncobj_replace_fence(syncobj, fence);
371 	dma_fence_put(fence);
372 #endif
373 }
374 
375 /* 5s default for wait submission */
376 #define DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT 5000000000ULL
377 /**
378  * drm_syncobj_find_fence - lookup and reference the fence in a sync object
379  * @file_private: drm file private pointer
380  * @handle: sync object handle to lookup.
381  * @point: timeline point
382  * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
383  * @fence: out parameter for the fence
384  *
385  * This is just a convenience function that combines drm_syncobj_find() and
386  * drm_syncobj_fence_get().
387  *
388  * Returns 0 on success or a negative error value on failure. On success @fence
389  * contains a reference to the fence, which must be released by calling
390  * dma_fence_put().
391  */
392 int drm_syncobj_find_fence(struct drm_file *file_private,
393 			   u32 handle, u64 point, u64 flags,
394 			   struct dma_fence **fence)
395 {
396 	STUB();
397 	return -ENOSYS;
398 #ifdef notyet
399 	struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
400 	struct syncobj_wait_entry wait;
401 	u64 timeout = nsecs_to_jiffies64(DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT);
402 	int ret;
403 
404 	if (!syncobj)
405 		return -ENOENT;
406 
407 	*fence = drm_syncobj_fence_get(syncobj);
408 	drm_syncobj_put(syncobj);
409 
410 	if (*fence) {
411 		ret = dma_fence_chain_find_seqno(fence, point);
412 		if (!ret)
413 			return 0;
414 		dma_fence_put(*fence);
415 	} else {
416 		ret = -EINVAL;
417 	}
418 
419 	if (!(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
420 		return ret;
421 
422 	memset(&wait, 0, sizeof(wait));
423 	wait.task = current;
424 	wait.point = point;
425 	drm_syncobj_fence_add_wait(syncobj, &wait);
426 
427 	do {
428 		set_current_state(TASK_INTERRUPTIBLE);
429 		if (wait.fence) {
430 			ret = 0;
431 			break;
432 		}
433                 if (timeout == 0) {
434                         ret = -ETIME;
435                         break;
436                 }
437 
438 		if (signal_pending(current)) {
439 			ret = -ERESTARTSYS;
440 			break;
441 		}
442 
443                 timeout = schedule_timeout(timeout);
444 	} while (1);
445 
446 	__set_current_state(TASK_RUNNING);
447 	*fence = wait.fence;
448 
449 	if (wait.node.next)
450 		drm_syncobj_remove_wait(syncobj, &wait);
451 
452 	return ret;
453 #endif
454 }
455 EXPORT_SYMBOL(drm_syncobj_find_fence);
456 
457 /**
458  * drm_syncobj_free - free a sync object.
459  * @kref: kref to free.
460  *
461  * Only to be called from kref_put in drm_syncobj_put.
462  */
463 void drm_syncobj_free(struct kref *kref)
464 {
465 	struct drm_syncobj *syncobj = container_of(kref,
466 						   struct drm_syncobj,
467 						   refcount);
468 	drm_syncobj_replace_fence(syncobj, NULL);
469 	kfree(syncobj);
470 }
471 EXPORT_SYMBOL(drm_syncobj_free);
472 
473 /**
474  * drm_syncobj_create - create a new syncobj
475  * @out_syncobj: returned syncobj
476  * @flags: DRM_SYNCOBJ_* flags
477  * @fence: if non-NULL, the syncobj will represent this fence
478  *
479  * This is the first function to create a sync object. After creating, drivers
480  * probably want to make it available to userspace, either through
481  * drm_syncobj_get_handle() or drm_syncobj_get_fd().
482  *
483  * Returns 0 on success or a negative error value on failure.
484  */
485 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
486 		       struct dma_fence *fence)
487 {
488 	struct drm_syncobj *syncobj;
489 
490 	syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
491 	if (!syncobj)
492 		return -ENOMEM;
493 
494 	kref_init(&syncobj->refcount);
495 	INIT_LIST_HEAD(&syncobj->cb_list);
496 	mtx_init(&syncobj->lock, IPL_NONE);
497 
498 	if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
499 		drm_syncobj_assign_null_handle(syncobj);
500 
501 	if (fence)
502 		drm_syncobj_replace_fence(syncobj, fence);
503 
504 	*out_syncobj = syncobj;
505 	return 0;
506 }
507 EXPORT_SYMBOL(drm_syncobj_create);
508 
509 /**
510  * drm_syncobj_get_handle - get a handle from a syncobj
511  * @file_private: drm file private pointer
512  * @syncobj: Sync object to export
513  * @handle: out parameter with the new handle
514  *
515  * Exports a sync object created with drm_syncobj_create() as a handle on
516  * @file_private to userspace.
517  *
518  * Returns 0 on success or a negative error value on failure.
519  */
520 int drm_syncobj_get_handle(struct drm_file *file_private,
521 			   struct drm_syncobj *syncobj, u32 *handle)
522 {
523 	int ret;
524 
525 	/* take a reference to put in the idr */
526 	drm_syncobj_get(syncobj);
527 
528 	idr_preload(GFP_KERNEL);
529 	spin_lock(&file_private->syncobj_table_lock);
530 	ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
531 	spin_unlock(&file_private->syncobj_table_lock);
532 
533 	idr_preload_end();
534 
535 	if (ret < 0) {
536 		drm_syncobj_put(syncobj);
537 		return ret;
538 	}
539 
540 	*handle = ret;
541 	return 0;
542 }
543 EXPORT_SYMBOL(drm_syncobj_get_handle);
544 
545 static int drm_syncobj_create_as_handle(struct drm_file *file_private,
546 					u32 *handle, uint32_t flags)
547 {
548 	int ret;
549 	struct drm_syncobj *syncobj;
550 
551 	ret = drm_syncobj_create(&syncobj, flags, NULL);
552 	if (ret)
553 		return ret;
554 
555 	ret = drm_syncobj_get_handle(file_private, syncobj, handle);
556 	drm_syncobj_put(syncobj);
557 	return ret;
558 }
559 
560 static int drm_syncobj_destroy(struct drm_file *file_private,
561 			       u32 handle)
562 {
563 	struct drm_syncobj *syncobj;
564 
565 	spin_lock(&file_private->syncobj_table_lock);
566 	syncobj = idr_remove(&file_private->syncobj_idr, handle);
567 	spin_unlock(&file_private->syncobj_table_lock);
568 
569 	if (!syncobj)
570 		return -EINVAL;
571 
572 	drm_syncobj_put(syncobj);
573 	return 0;
574 }
575 
576 #ifdef notyet
577 static int drm_syncobj_file_release(struct inode *inode, struct file *file)
578 {
579 	struct drm_syncobj *syncobj = file->private_data;
580 
581 	drm_syncobj_put(syncobj);
582 	return 0;
583 }
584 
585 static const struct file_operations drm_syncobj_file_fops = {
586 	.release = drm_syncobj_file_release,
587 };
588 #endif
589 
590 /**
591  * drm_syncobj_get_fd - get a file descriptor from a syncobj
592  * @syncobj: Sync object to export
593  * @p_fd: out parameter with the new file descriptor
594  *
595  * Exports a sync object created with drm_syncobj_create() as a file descriptor.
596  *
597  * Returns 0 on success or a negative error value on failure.
598  */
599 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
600 {
601 	STUB();
602 	return -ENOSYS;
603 #ifdef notyet
604 	struct file *file;
605 	int fd;
606 
607 	fd = get_unused_fd_flags(O_CLOEXEC);
608 	if (fd < 0)
609 		return fd;
610 
611 	file = anon_inode_getfile("syncobj_file",
612 				  &drm_syncobj_file_fops,
613 				  syncobj, 0);
614 	if (IS_ERR(file)) {
615 		put_unused_fd(fd);
616 		return PTR_ERR(file);
617 	}
618 
619 	drm_syncobj_get(syncobj);
620 	fd_install(fd, file);
621 
622 	*p_fd = fd;
623 	return 0;
624 #endif
625 }
626 EXPORT_SYMBOL(drm_syncobj_get_fd);
627 
628 static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
629 				    u32 handle, int *p_fd)
630 {
631 	struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
632 	int ret;
633 
634 	if (!syncobj)
635 		return -EINVAL;
636 
637 	ret = drm_syncobj_get_fd(syncobj, p_fd);
638 	drm_syncobj_put(syncobj);
639 	return ret;
640 }
641 
642 static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
643 				    int fd, u32 *handle)
644 {
645 	STUB();
646 	return -ENOSYS;
647 #ifdef notyet
648 	struct drm_syncobj *syncobj;
649 	struct fd f = fdget(fd);
650 	int ret;
651 
652 	if (!f.file)
653 		return -EINVAL;
654 
655 	if (f.file->f_op != &drm_syncobj_file_fops) {
656 		fdput(f);
657 		return -EINVAL;
658 	}
659 
660 	/* take a reference to put in the idr */
661 	syncobj = f.file->private_data;
662 	drm_syncobj_get(syncobj);
663 
664 	idr_preload(GFP_KERNEL);
665 	spin_lock(&file_private->syncobj_table_lock);
666 	ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
667 	spin_unlock(&file_private->syncobj_table_lock);
668 	idr_preload_end();
669 
670 	if (ret > 0) {
671 		*handle = ret;
672 		ret = 0;
673 	} else
674 		drm_syncobj_put(syncobj);
675 
676 	fdput(f);
677 	return ret;
678 #endif
679 }
680 
681 static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
682 					      int fd, int handle)
683 {
684 	struct dma_fence *fence = sync_file_get_fence(fd);
685 	struct drm_syncobj *syncobj;
686 
687 	if (!fence)
688 		return -EINVAL;
689 
690 	syncobj = drm_syncobj_find(file_private, handle);
691 	if (!syncobj) {
692 		dma_fence_put(fence);
693 		return -ENOENT;
694 	}
695 
696 	drm_syncobj_replace_fence(syncobj, fence);
697 	dma_fence_put(fence);
698 	drm_syncobj_put(syncobj);
699 	return 0;
700 }
701 
702 static int drm_syncobj_export_sync_file(struct drm_file *file_private,
703 					int handle, int *p_fd)
704 {
705 	int ret;
706 	struct dma_fence *fence;
707 	struct sync_file *sync_file;
708 	int fd = get_unused_fd_flags(O_CLOEXEC);
709 
710 	if (fd < 0)
711 		return fd;
712 
713 	ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
714 	if (ret)
715 		goto err_put_fd;
716 
717 	sync_file = sync_file_create(fence);
718 
719 	dma_fence_put(fence);
720 
721 	if (!sync_file) {
722 		ret = -EINVAL;
723 		goto err_put_fd;
724 	}
725 
726 	fd_install(fd, sync_file->file);
727 
728 	*p_fd = fd;
729 	return 0;
730 err_put_fd:
731 	put_unused_fd(fd);
732 	return ret;
733 }
734 /**
735  * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
736  * @file_private: drm file-private structure to set up
737  *
738  * Called at device open time, sets up the structure for handling refcounting
739  * of sync objects.
740  */
741 void
742 drm_syncobj_open(struct drm_file *file_private)
743 {
744 	idr_init_base(&file_private->syncobj_idr, 1);
745 	mtx_init(&file_private->syncobj_table_lock, IPL_NONE);
746 }
747 
748 static int
749 drm_syncobj_release_handle(int id, void *ptr, void *data)
750 {
751 	struct drm_syncobj *syncobj = ptr;
752 
753 	drm_syncobj_put(syncobj);
754 	return 0;
755 }
756 
757 /**
758  * drm_syncobj_release - release file-private sync object resources
759  * @file_private: drm file-private structure to clean up
760  *
761  * Called at close time when the filp is going away.
762  *
763  * Releases any remaining references on objects by this filp.
764  */
765 void
766 drm_syncobj_release(struct drm_file *file_private)
767 {
768 	idr_for_each(&file_private->syncobj_idr,
769 		     &drm_syncobj_release_handle, file_private);
770 	idr_destroy(&file_private->syncobj_idr);
771 }
772 
773 int
774 drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
775 			 struct drm_file *file_private)
776 {
777 	struct drm_syncobj_create *args = data;
778 
779 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
780 		return -EOPNOTSUPP;
781 
782 	/* no valid flags yet */
783 	if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
784 		return -EINVAL;
785 
786 	return drm_syncobj_create_as_handle(file_private,
787 					    &args->handle, args->flags);
788 }
789 
790 int
791 drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
792 			  struct drm_file *file_private)
793 {
794 	struct drm_syncobj_destroy *args = data;
795 
796 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
797 		return -EOPNOTSUPP;
798 
799 	/* make sure padding is empty */
800 	if (args->pad)
801 		return -EINVAL;
802 	return drm_syncobj_destroy(file_private, args->handle);
803 }
804 
805 int
806 drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
807 				   struct drm_file *file_private)
808 {
809 	struct drm_syncobj_handle *args = data;
810 
811 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
812 		return -EOPNOTSUPP;
813 
814 	if (args->pad)
815 		return -EINVAL;
816 
817 	if (args->flags != 0 &&
818 	    args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
819 		return -EINVAL;
820 
821 	if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
822 		return drm_syncobj_export_sync_file(file_private, args->handle,
823 						    &args->fd);
824 
825 	return drm_syncobj_handle_to_fd(file_private, args->handle,
826 					&args->fd);
827 }
828 
829 int
830 drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
831 				   struct drm_file *file_private)
832 {
833 	struct drm_syncobj_handle *args = data;
834 
835 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
836 		return -EOPNOTSUPP;
837 
838 	if (args->pad)
839 		return -EINVAL;
840 
841 	if (args->flags != 0 &&
842 	    args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
843 		return -EINVAL;
844 
845 	if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
846 		return drm_syncobj_import_sync_file_fence(file_private,
847 							  args->fd,
848 							  args->handle);
849 
850 	return drm_syncobj_fd_to_handle(file_private, args->fd,
851 					&args->handle);
852 }
853 
854 static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
855 					    struct drm_syncobj_transfer *args)
856 {
857 	struct drm_syncobj *timeline_syncobj = NULL;
858 	struct dma_fence *fence;
859 	struct dma_fence_chain *chain;
860 	int ret;
861 
862 	timeline_syncobj = drm_syncobj_find(file_private, args->dst_handle);
863 	if (!timeline_syncobj) {
864 		return -ENOENT;
865 	}
866 	ret = drm_syncobj_find_fence(file_private, args->src_handle,
867 				     args->src_point, args->flags,
868 				     &fence);
869 	if (ret)
870 		goto err;
871 	chain = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
872 	if (!chain) {
873 		ret = -ENOMEM;
874 		goto err1;
875 	}
876 	drm_syncobj_add_point(timeline_syncobj, chain, fence, args->dst_point);
877 err1:
878 	dma_fence_put(fence);
879 err:
880 	drm_syncobj_put(timeline_syncobj);
881 
882 	return ret;
883 }
884 
885 static int
886 drm_syncobj_transfer_to_binary(struct drm_file *file_private,
887 			       struct drm_syncobj_transfer *args)
888 {
889 	struct drm_syncobj *binary_syncobj = NULL;
890 	struct dma_fence *fence;
891 	int ret;
892 
893 	binary_syncobj = drm_syncobj_find(file_private, args->dst_handle);
894 	if (!binary_syncobj)
895 		return -ENOENT;
896 	ret = drm_syncobj_find_fence(file_private, args->src_handle,
897 				     args->src_point, args->flags, &fence);
898 	if (ret)
899 		goto err;
900 	drm_syncobj_replace_fence(binary_syncobj, fence);
901 	dma_fence_put(fence);
902 err:
903 	drm_syncobj_put(binary_syncobj);
904 
905 	return ret;
906 }
907 int
908 drm_syncobj_transfer_ioctl(struct drm_device *dev, void *data,
909 			   struct drm_file *file_private)
910 {
911 	struct drm_syncobj_transfer *args = data;
912 	int ret;
913 
914 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
915 		return -EOPNOTSUPP;
916 
917 	if (args->pad)
918 		return -EINVAL;
919 
920 	if (args->dst_point)
921 		ret = drm_syncobj_transfer_to_timeline(file_private, args);
922 	else
923 		ret = drm_syncobj_transfer_to_binary(file_private, args);
924 
925 	return ret;
926 }
927 
928 #ifdef notyet
929 static void syncobj_wait_fence_func(struct dma_fence *fence,
930 				    struct dma_fence_cb *cb)
931 {
932 	struct syncobj_wait_entry *wait =
933 		container_of(cb, struct syncobj_wait_entry, fence_cb);
934 
935 	wake_up_process(wait->task);
936 }
937 #endif
938 
939 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
940 				      struct syncobj_wait_entry *wait)
941 {
942 	STUB();
943 #ifdef notyet
944 	struct dma_fence *fence;
945 
946 	/* This happens inside the syncobj lock */
947 	fence = rcu_dereference_protected(syncobj->fence,
948 					  lockdep_is_held(&syncobj->lock));
949 	dma_fence_get(fence);
950 	if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
951 		dma_fence_put(fence);
952 		return;
953 	} else if (!fence) {
954 		wait->fence = dma_fence_get_stub();
955 	} else {
956 		wait->fence = fence;
957 	}
958 
959 	wake_up_process(wait->task);
960 	list_del_init(&wait->node);
961 #endif
962 }
963 
964 static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
965 						  void __user *user_points,
966 						  uint32_t count,
967 						  uint32_t flags,
968 						  signed long timeout,
969 						  uint32_t *idx)
970 {
971 	STUB();
972 	return -ENOSYS;
973 #ifdef notyet
974 	struct syncobj_wait_entry *entries;
975 	struct dma_fence *fence;
976 	uint64_t *points;
977 	uint32_t signaled_count, i;
978 
979 	points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
980 	if (points == NULL)
981 		return -ENOMEM;
982 
983 	if (!user_points) {
984 		memset(points, 0, count * sizeof(uint64_t));
985 
986 	} else if (copy_from_user(points, user_points,
987 				  sizeof(uint64_t) * count)) {
988 		timeout = -EFAULT;
989 		goto err_free_points;
990 	}
991 
992 	entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
993 	if (!entries) {
994 		timeout = -ENOMEM;
995 		goto err_free_points;
996 	}
997 	/* Walk the list of sync objects and initialize entries.  We do
998 	 * this up-front so that we can properly return -EINVAL if there is
999 	 * a syncobj with a missing fence and then never have the chance of
1000 	 * returning -EINVAL again.
1001 	 */
1002 	signaled_count = 0;
1003 	for (i = 0; i < count; ++i) {
1004 		struct dma_fence *fence;
1005 
1006 #ifdef __linux__
1007 		entries[i].task = current;
1008 #else
1009 		entries[i].task = curproc;
1010 #endif
1011 		entries[i].point = points[i];
1012 		fence = drm_syncobj_fence_get(syncobjs[i]);
1013 		if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
1014 			dma_fence_put(fence);
1015 			if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
1016 				continue;
1017 			} else {
1018 				timeout = -EINVAL;
1019 				goto cleanup_entries;
1020 			}
1021 		}
1022 
1023 		if (fence)
1024 			entries[i].fence = fence;
1025 		else
1026 			entries[i].fence = dma_fence_get_stub();
1027 
1028 		if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
1029 		    dma_fence_is_signaled(entries[i].fence)) {
1030 			if (signaled_count == 0 && idx)
1031 				*idx = i;
1032 			signaled_count++;
1033 		}
1034 	}
1035 
1036 	if (signaled_count == count ||
1037 	    (signaled_count > 0 &&
1038 	     !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
1039 		goto cleanup_entries;
1040 
1041 	/* There's a very annoying laxness in the dma_fence API here, in
1042 	 * that backends are not required to automatically report when a
1043 	 * fence is signaled prior to fence->ops->enable_signaling() being
1044 	 * called.  So here if we fail to match signaled_count, we need to
1045 	 * fallthough and try a 0 timeout wait!
1046 	 */
1047 
1048 	if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
1049 		for (i = 0; i < count; ++i)
1050 			drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
1051 	}
1052 
1053 	do {
1054 		set_current_state(TASK_INTERRUPTIBLE);
1055 
1056 		signaled_count = 0;
1057 		for (i = 0; i < count; ++i) {
1058 			fence = entries[i].fence;
1059 			if (!fence)
1060 				continue;
1061 
1062 			if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
1063 			    dma_fence_is_signaled(fence) ||
1064 			    (!entries[i].fence_cb.func &&
1065 			     dma_fence_add_callback(fence,
1066 						    &entries[i].fence_cb,
1067 						    syncobj_wait_fence_func))) {
1068 				/* The fence has been signaled */
1069 				if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
1070 					signaled_count++;
1071 				} else {
1072 					if (idx)
1073 						*idx = i;
1074 					goto done_waiting;
1075 				}
1076 			}
1077 		}
1078 
1079 		if (signaled_count == count)
1080 			goto done_waiting;
1081 
1082 		if (timeout == 0) {
1083 			timeout = -ETIME;
1084 			goto done_waiting;
1085 		}
1086 
1087 		if (signal_pending(current)) {
1088 			timeout = -ERESTARTSYS;
1089 			goto done_waiting;
1090 		}
1091 
1092 		timeout = schedule_timeout(timeout);
1093 	} while (1);
1094 
1095 done_waiting:
1096 	__set_current_state(TASK_RUNNING);
1097 
1098 cleanup_entries:
1099 	for (i = 0; i < count; ++i) {
1100 		drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
1101 		if (entries[i].fence_cb.func)
1102 			dma_fence_remove_callback(entries[i].fence,
1103 						  &entries[i].fence_cb);
1104 		dma_fence_put(entries[i].fence);
1105 	}
1106 	kfree(entries);
1107 
1108 err_free_points:
1109 	kfree(points);
1110 
1111 	return timeout;
1112 #endif
1113 }
1114 
1115 /**
1116  * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
1117  *
1118  * @timeout_nsec: timeout nsec component in ns, 0 for poll
1119  *
1120  * Calculate the timeout in jiffies from an absolute time in sec/nsec.
1121  */
1122 signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
1123 {
1124 	ktime_t abs_timeout, now;
1125 	u64 timeout_ns, timeout_jiffies64;
1126 
1127 	/* make 0 timeout means poll - absolute 0 doesn't seem valid */
1128 	if (timeout_nsec == 0)
1129 		return 0;
1130 
1131 	abs_timeout = ns_to_ktime(timeout_nsec);
1132 	now = ktime_get();
1133 
1134 	if (!ktime_after(abs_timeout, now))
1135 		return 0;
1136 
1137 	timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
1138 
1139 	timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
1140 	/*  clamp timeout to avoid infinite timeout */
1141 	if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
1142 		return MAX_SCHEDULE_TIMEOUT - 1;
1143 
1144 	return timeout_jiffies64 + 1;
1145 }
1146 EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
1147 
1148 static int drm_syncobj_array_wait(struct drm_device *dev,
1149 				  struct drm_file *file_private,
1150 				  struct drm_syncobj_wait *wait,
1151 				  struct drm_syncobj_timeline_wait *timeline_wait,
1152 				  struct drm_syncobj **syncobjs, bool timeline)
1153 {
1154 	signed long timeout = 0;
1155 	uint32_t first = ~0;
1156 
1157 	if (!timeline) {
1158 		timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
1159 		timeout = drm_syncobj_array_wait_timeout(syncobjs,
1160 							 NULL,
1161 							 wait->count_handles,
1162 							 wait->flags,
1163 							 timeout, &first);
1164 		if (timeout < 0)
1165 			return timeout;
1166 		wait->first_signaled = first;
1167 	} else {
1168 		timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec);
1169 		timeout = drm_syncobj_array_wait_timeout(syncobjs,
1170 							 u64_to_user_ptr(timeline_wait->points),
1171 							 timeline_wait->count_handles,
1172 							 timeline_wait->flags,
1173 							 timeout, &first);
1174 		if (timeout < 0)
1175 			return timeout;
1176 		timeline_wait->first_signaled = first;
1177 	}
1178 	return 0;
1179 }
1180 
1181 static int drm_syncobj_array_find(struct drm_file *file_private,
1182 				  void __user *user_handles,
1183 				  uint32_t count_handles,
1184 				  struct drm_syncobj ***syncobjs_out)
1185 {
1186 	uint32_t i, *handles;
1187 	struct drm_syncobj **syncobjs;
1188 	int ret;
1189 
1190 	handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1191 	if (handles == NULL)
1192 		return -ENOMEM;
1193 
1194 	if (copy_from_user(handles, user_handles,
1195 			   sizeof(uint32_t) * count_handles)) {
1196 		ret = -EFAULT;
1197 		goto err_free_handles;
1198 	}
1199 
1200 	syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1201 	if (syncobjs == NULL) {
1202 		ret = -ENOMEM;
1203 		goto err_free_handles;
1204 	}
1205 
1206 	for (i = 0; i < count_handles; i++) {
1207 		syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1208 		if (!syncobjs[i]) {
1209 			ret = -ENOENT;
1210 			goto err_put_syncobjs;
1211 		}
1212 	}
1213 
1214 	kfree(handles);
1215 	*syncobjs_out = syncobjs;
1216 	return 0;
1217 
1218 err_put_syncobjs:
1219 	while (i-- > 0)
1220 		drm_syncobj_put(syncobjs[i]);
1221 	kfree(syncobjs);
1222 err_free_handles:
1223 	kfree(handles);
1224 
1225 	return ret;
1226 }
1227 
1228 static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1229 				   uint32_t count)
1230 {
1231 	uint32_t i;
1232 	for (i = 0; i < count; i++)
1233 		drm_syncobj_put(syncobjs[i]);
1234 	kfree(syncobjs);
1235 }
1236 
1237 int
1238 drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1239 		       struct drm_file *file_private)
1240 {
1241 	struct drm_syncobj_wait *args = data;
1242 	struct drm_syncobj **syncobjs;
1243 	int ret = 0;
1244 
1245 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1246 		return -EOPNOTSUPP;
1247 
1248 	if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1249 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
1250 		return -EINVAL;
1251 
1252 	if (args->count_handles == 0)
1253 		return -EINVAL;
1254 
1255 	ret = drm_syncobj_array_find(file_private,
1256 				     u64_to_user_ptr(args->handles),
1257 				     args->count_handles,
1258 				     &syncobjs);
1259 	if (ret < 0)
1260 		return ret;
1261 
1262 	ret = drm_syncobj_array_wait(dev, file_private,
1263 				     args, NULL, syncobjs, false);
1264 
1265 	drm_syncobj_array_free(syncobjs, args->count_handles);
1266 
1267 	return ret;
1268 }
1269 
1270 int
1271 drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,
1272 				struct drm_file *file_private)
1273 {
1274 	struct drm_syncobj_timeline_wait *args = data;
1275 	struct drm_syncobj **syncobjs;
1276 	int ret = 0;
1277 
1278 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1279 		return -EOPNOTSUPP;
1280 
1281 	if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1282 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1283 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
1284 		return -EINVAL;
1285 
1286 	if (args->count_handles == 0)
1287 		return -EINVAL;
1288 
1289 	ret = drm_syncobj_array_find(file_private,
1290 				     u64_to_user_ptr(args->handles),
1291 				     args->count_handles,
1292 				     &syncobjs);
1293 	if (ret < 0)
1294 		return ret;
1295 
1296 	ret = drm_syncobj_array_wait(dev, file_private,
1297 				     NULL, args, syncobjs, true);
1298 
1299 	drm_syncobj_array_free(syncobjs, args->count_handles);
1300 
1301 	return ret;
1302 }
1303 
1304 
1305 int
1306 drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1307 			struct drm_file *file_private)
1308 {
1309 	struct drm_syncobj_array *args = data;
1310 	struct drm_syncobj **syncobjs;
1311 	uint32_t i;
1312 	int ret;
1313 
1314 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1315 		return -EOPNOTSUPP;
1316 
1317 	if (args->pad != 0)
1318 		return -EINVAL;
1319 
1320 	if (args->count_handles == 0)
1321 		return -EINVAL;
1322 
1323 	ret = drm_syncobj_array_find(file_private,
1324 				     u64_to_user_ptr(args->handles),
1325 				     args->count_handles,
1326 				     &syncobjs);
1327 	if (ret < 0)
1328 		return ret;
1329 
1330 	for (i = 0; i < args->count_handles; i++)
1331 		drm_syncobj_replace_fence(syncobjs[i], NULL);
1332 
1333 	drm_syncobj_array_free(syncobjs, args->count_handles);
1334 
1335 	return 0;
1336 }
1337 
1338 int
1339 drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1340 			 struct drm_file *file_private)
1341 {
1342 	struct drm_syncobj_array *args = data;
1343 	struct drm_syncobj **syncobjs;
1344 	uint32_t i;
1345 	int ret;
1346 
1347 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1348 		return -EOPNOTSUPP;
1349 
1350 	if (args->pad != 0)
1351 		return -EINVAL;
1352 
1353 	if (args->count_handles == 0)
1354 		return -EINVAL;
1355 
1356 	ret = drm_syncobj_array_find(file_private,
1357 				     u64_to_user_ptr(args->handles),
1358 				     args->count_handles,
1359 				     &syncobjs);
1360 	if (ret < 0)
1361 		return ret;
1362 
1363 	for (i = 0; i < args->count_handles; i++)
1364 		drm_syncobj_assign_null_handle(syncobjs[i]);
1365 
1366 	drm_syncobj_array_free(syncobjs, args->count_handles);
1367 
1368 	return ret;
1369 }
1370 
1371 int
1372 drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
1373 				  struct drm_file *file_private)
1374 {
1375 	return -ENOSYS;
1376 #ifdef notyet
1377 	struct drm_syncobj_timeline_array *args = data;
1378 	struct drm_syncobj **syncobjs;
1379 	struct dma_fence_chain **chains;
1380 	uint64_t *points;
1381 	uint32_t i, j;
1382 	int ret;
1383 
1384 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1385 		return -EOPNOTSUPP;
1386 
1387 	if (args->flags != 0)
1388 		return -EINVAL;
1389 
1390 	if (args->count_handles == 0)
1391 		return -EINVAL;
1392 
1393 	ret = drm_syncobj_array_find(file_private,
1394 				     u64_to_user_ptr(args->handles),
1395 				     args->count_handles,
1396 				     &syncobjs);
1397 	if (ret < 0)
1398 		return ret;
1399 
1400 	points = kmalloc_array(args->count_handles, sizeof(*points),
1401 			       GFP_KERNEL);
1402 	if (!points) {
1403 		ret = -ENOMEM;
1404 		goto out;
1405 	}
1406 	if (!u64_to_user_ptr(args->points)) {
1407 		memset(points, 0, args->count_handles * sizeof(uint64_t));
1408 	} else if (copy_from_user(points, u64_to_user_ptr(args->points),
1409 				  sizeof(uint64_t) * args->count_handles)) {
1410 		ret = -EFAULT;
1411 		goto err_points;
1412 	}
1413 
1414 	chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL);
1415 	if (!chains) {
1416 		ret = -ENOMEM;
1417 		goto err_points;
1418 	}
1419 	for (i = 0; i < args->count_handles; i++) {
1420 		chains[i] = kzalloc(sizeof(struct dma_fence_chain), GFP_KERNEL);
1421 		if (!chains[i]) {
1422 			for (j = 0; j < i; j++)
1423 				kfree(chains[j]);
1424 			ret = -ENOMEM;
1425 			goto err_chains;
1426 		}
1427 	}
1428 
1429 	for (i = 0; i < args->count_handles; i++) {
1430 		struct dma_fence *fence = dma_fence_get_stub();
1431 
1432 		drm_syncobj_add_point(syncobjs[i], chains[i],
1433 				      fence, points[i]);
1434 		dma_fence_put(fence);
1435 	}
1436 err_chains:
1437 	kfree(chains);
1438 err_points:
1439 	kfree(points);
1440 out:
1441 	drm_syncobj_array_free(syncobjs, args->count_handles);
1442 
1443 	return ret;
1444 #endif
1445 }
1446 
1447 int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,
1448 			    struct drm_file *file_private)
1449 {
1450 	STUB();
1451 	return -ENOSYS;
1452 #ifdef notyet
1453 	struct drm_syncobj_timeline_array *args = data;
1454 	struct drm_syncobj **syncobjs;
1455 	uint64_t __user *points = u64_to_user_ptr(args->points);
1456 	uint32_t i;
1457 	int ret;
1458 
1459 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1460 		return -EOPNOTSUPP;
1461 
1462 	if (args->flags & ~DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED)
1463 		return -EINVAL;
1464 
1465 	if (args->count_handles == 0)
1466 		return -EINVAL;
1467 
1468 	ret = drm_syncobj_array_find(file_private,
1469 				     u64_to_user_ptr(args->handles),
1470 				     args->count_handles,
1471 				     &syncobjs);
1472 	if (ret < 0)
1473 		return ret;
1474 
1475 	for (i = 0; i < args->count_handles; i++) {
1476 		struct dma_fence_chain *chain;
1477 		struct dma_fence *fence;
1478 		uint64_t point;
1479 
1480 		fence = drm_syncobj_fence_get(syncobjs[i]);
1481 		chain = to_dma_fence_chain(fence);
1482 		if (chain) {
1483 			struct dma_fence *iter, *last_signaled =
1484 				dma_fence_get(fence);
1485 
1486 			if (args->flags &
1487 			    DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED) {
1488 				point = fence->seqno;
1489 			} else {
1490 				dma_fence_chain_for_each(iter, fence) {
1491 					if (iter->context != fence->context) {
1492 						dma_fence_put(iter);
1493 						/* It is most likely that timeline has
1494 						* unorder points. */
1495 						break;
1496 					}
1497 					dma_fence_put(last_signaled);
1498 					last_signaled = dma_fence_get(iter);
1499 				}
1500 				point = dma_fence_is_signaled(last_signaled) ?
1501 					last_signaled->seqno :
1502 					to_dma_fence_chain(last_signaled)->prev_seqno;
1503 			}
1504 			dma_fence_put(last_signaled);
1505 		} else {
1506 			point = 0;
1507 		}
1508 		dma_fence_put(fence);
1509 		ret = copy_to_user(&points[i], &point, sizeof(uint64_t));
1510 		ret = ret ? -EFAULT : 0;
1511 		if (ret)
1512 			break;
1513 	}
1514 	drm_syncobj_array_free(syncobjs, args->count_handles);
1515 
1516 	return ret;
1517 #endif
1518 }
1519