xref: /openbsd/sys/dev/pci/drm/drm_syncobj.c (revision c9ffe515)
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  * Alternatively, &DRM_IOCTL_SYNCOBJ_EVENTFD can be used to wait without
140  * blocking: an eventfd will be signaled when the syncobj is. This is useful to
141  * integrate the wait in an event loop.
142  *
143  *
144  * Import/export of syncobjs
145  * -------------------------
146  *
147  * &DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE and &DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD
148  * provide two mechanisms for import/export of syncobjs.
149  *
150  * The first lets the client import or export an entire syncobj to a file
151  * descriptor.
152  * These fd's are opaque and have no other use case, except passing the
153  * syncobj between processes.
154  * All exported file descriptors and any syncobj handles created as a
155  * result of importing those file descriptors own a reference to the
156  * same underlying struct &drm_syncobj and the syncobj can be used
157  * persistently across all the processes with which it is shared.
158  * The syncobj is freed only once the last reference is dropped.
159  * Unlike dma-buf, importing a syncobj creates a new handle (with its own
160  * reference) for every import instead of de-duplicating.
161  * The primary use-case of this persistent import/export is for shared
162  * Vulkan fences and semaphores.
163  *
164  * The second import/export mechanism, which is indicated by
165  * &DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE or
166  * &DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE lets the client
167  * import/export the syncobj's current fence from/to a &sync_file.
168  * When a syncobj is exported to a sync file, that sync file wraps the
169  * sycnobj's fence at the time of export and any later signal or reset
170  * operations on the syncobj will not affect the exported sync file.
171  * When a sync file is imported into a syncobj, the syncobj's fence is set
172  * to the fence wrapped by that sync file.
173  * Because sync files are immutable, resetting or signaling the syncobj
174  * will not affect any sync files whose fences have been imported into the
175  * syncobj.
176  *
177  *
178  * Import/export of timeline points in timeline syncobjs
179  * -----------------------------------------------------
180  *
181  * &DRM_IOCTL_SYNCOBJ_TRANSFER provides a mechanism to transfer a struct
182  * &dma_fence_chain of a syncobj at a given u64 point to another u64 point
183  * into another syncobj.
184  *
185  * Note that if you want to transfer a struct &dma_fence_chain from a given
186  * point on a timeline syncobj from/into a binary syncobj, you can use the
187  * point 0 to mean take/replace the fence in the syncobj.
188  */
189 
190 #include <linux/anon_inodes.h>
191 #include <linux/dma-fence-unwrap.h>
192 #include <linux/eventfd.h>
193 #include <linux/file.h>
194 #include <linux/fs.h>
195 #include <linux/sched/signal.h>
196 #include <linux/sync_file.h>
197 #include <linux/uaccess.h>
198 
199 #include <drm/drm.h>
200 #include <drm/drm_drv.h>
201 #include <drm/drm_file.h>
202 #include <drm/drm_gem.h>
203 #include <drm/drm_print.h>
204 #include <drm/drm_syncobj.h>
205 #include <drm/drm_utils.h>
206 
207 #include "drm_internal.h"
208 
209 struct syncobj_wait_entry {
210 	struct list_head node;
211 #ifdef __linux__
212 	struct task_struct *task;
213 #else
214 	struct proc *task;
215 #endif
216 	struct dma_fence *fence;
217 	struct dma_fence_cb fence_cb;
218 	u64    point;
219 };
220 
221 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
222 				      struct syncobj_wait_entry *wait);
223 
224 struct syncobj_eventfd_entry {
225 	struct list_head node;
226 	struct dma_fence *fence;
227 	struct dma_fence_cb fence_cb;
228 	struct drm_syncobj *syncobj;
229 	struct eventfd_ctx *ev_fd_ctx;
230 	u64 point;
231 	u32 flags;
232 };
233 
234 static void
235 syncobj_eventfd_entry_func(struct drm_syncobj *syncobj,
236 			   struct syncobj_eventfd_entry *entry);
237 
238 /**
239  * drm_syncobj_find - lookup and reference a sync object.
240  * @file_private: drm file private pointer
241  * @handle: sync object handle to lookup.
242  *
243  * Returns a reference to the syncobj pointed to by handle or NULL. The
244  * reference must be released by calling drm_syncobj_put().
245  */
drm_syncobj_find(struct drm_file * file_private,u32 handle)246 struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
247 				     u32 handle)
248 {
249 	struct drm_syncobj *syncobj;
250 
251 	spin_lock(&file_private->syncobj_table_lock);
252 
253 	/* Check if we currently have a reference on the object */
254 	syncobj = idr_find(&file_private->syncobj_idr, handle);
255 	if (syncobj)
256 		drm_syncobj_get(syncobj);
257 
258 	spin_unlock(&file_private->syncobj_table_lock);
259 
260 	return syncobj;
261 }
262 EXPORT_SYMBOL(drm_syncobj_find);
263 
drm_syncobj_fence_add_wait(struct drm_syncobj * syncobj,struct syncobj_wait_entry * wait)264 static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
265 				       struct syncobj_wait_entry *wait)
266 {
267 	struct dma_fence *fence;
268 
269 	if (wait->fence)
270 		return;
271 
272 	spin_lock(&syncobj->lock);
273 	/* We've already tried once to get a fence and failed.  Now that we
274 	 * have the lock, try one more time just to be sure we don't add a
275 	 * callback when a fence has already been set.
276 	 */
277 	fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
278 	if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
279 		dma_fence_put(fence);
280 		list_add_tail(&wait->node, &syncobj->cb_list);
281 	} else if (!fence) {
282 		wait->fence = dma_fence_get_stub();
283 	} else {
284 		wait->fence = fence;
285 	}
286 	spin_unlock(&syncobj->lock);
287 }
288 
drm_syncobj_remove_wait(struct drm_syncobj * syncobj,struct syncobj_wait_entry * wait)289 static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
290 				    struct syncobj_wait_entry *wait)
291 {
292 	if (!wait->node.next)
293 		return;
294 
295 	spin_lock(&syncobj->lock);
296 	list_del_init(&wait->node);
297 	spin_unlock(&syncobj->lock);
298 }
299 
300 static void
syncobj_eventfd_entry_free(struct syncobj_eventfd_entry * entry)301 syncobj_eventfd_entry_free(struct syncobj_eventfd_entry *entry)
302 {
303 	eventfd_ctx_put(entry->ev_fd_ctx);
304 	dma_fence_put(entry->fence);
305 	/* This happens either inside the syncobj lock, or after the node has
306 	 * already been removed from the list.
307 	 */
308 	list_del(&entry->node);
309 	kfree(entry);
310 }
311 
312 #ifdef notyet
313 static void
drm_syncobj_add_eventfd(struct drm_syncobj * syncobj,struct syncobj_eventfd_entry * entry)314 drm_syncobj_add_eventfd(struct drm_syncobj *syncobj,
315 			struct syncobj_eventfd_entry *entry)
316 {
317 	spin_lock(&syncobj->lock);
318 	list_add_tail(&entry->node, &syncobj->ev_fd_list);
319 	syncobj_eventfd_entry_func(syncobj, entry);
320 	spin_unlock(&syncobj->lock);
321 }
322 #endif
323 
324 /**
325  * drm_syncobj_add_point - add new timeline point to the syncobj
326  * @syncobj: sync object to add timeline point do
327  * @chain: chain node to use to add the point
328  * @fence: fence to encapsulate in the chain node
329  * @point: sequence number to use for the point
330  *
331  * Add the chain node as new timeline point to the syncobj.
332  */
drm_syncobj_add_point(struct drm_syncobj * syncobj,struct dma_fence_chain * chain,struct dma_fence * fence,uint64_t point)333 void drm_syncobj_add_point(struct drm_syncobj *syncobj,
334 			   struct dma_fence_chain *chain,
335 			   struct dma_fence *fence,
336 			   uint64_t point)
337 {
338 	struct syncobj_wait_entry *wait_cur, *wait_tmp;
339 	struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
340 	struct dma_fence *prev;
341 
342 	dma_fence_get(fence);
343 
344 	spin_lock(&syncobj->lock);
345 
346 	prev = drm_syncobj_fence_get(syncobj);
347 	/* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
348 	if (prev && prev->seqno >= point)
349 		DRM_DEBUG("You are adding an unorder point to timeline!\n");
350 	dma_fence_chain_init(chain, prev, fence, point);
351 	rcu_assign_pointer(syncobj->fence, &chain->base);
352 
353 	list_for_each_entry_safe(wait_cur, wait_tmp, &syncobj->cb_list, node)
354 		syncobj_wait_syncobj_func(syncobj, wait_cur);
355 	list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
356 		syncobj_eventfd_entry_func(syncobj, ev_fd_cur);
357 	spin_unlock(&syncobj->lock);
358 
359 	/* Walk the chain once to trigger garbage collection */
360 	dma_fence_chain_for_each(fence, prev);
361 	dma_fence_put(prev);
362 }
363 EXPORT_SYMBOL(drm_syncobj_add_point);
364 
365 /**
366  * drm_syncobj_replace_fence - replace fence in a sync object.
367  * @syncobj: Sync object to replace fence in
368  * @fence: fence to install in sync file.
369  *
370  * This replaces the fence on a sync object.
371  */
drm_syncobj_replace_fence(struct drm_syncobj * syncobj,struct dma_fence * fence)372 void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
373 			       struct dma_fence *fence)
374 {
375 	struct dma_fence *old_fence;
376 	struct syncobj_wait_entry *wait_cur, *wait_tmp;
377 	struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
378 
379 	if (fence)
380 		dma_fence_get(fence);
381 
382 	spin_lock(&syncobj->lock);
383 
384 	old_fence = rcu_dereference_protected(syncobj->fence,
385 					      lockdep_is_held(&syncobj->lock));
386 	rcu_assign_pointer(syncobj->fence, fence);
387 
388 	if (fence != old_fence) {
389 		list_for_each_entry_safe(wait_cur, wait_tmp, &syncobj->cb_list, node)
390 			syncobj_wait_syncobj_func(syncobj, wait_cur);
391 		list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
392 			syncobj_eventfd_entry_func(syncobj, ev_fd_cur);
393 	}
394 
395 	spin_unlock(&syncobj->lock);
396 
397 	dma_fence_put(old_fence);
398 }
399 EXPORT_SYMBOL(drm_syncobj_replace_fence);
400 
401 /**
402  * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
403  * @syncobj: sync object to assign the fence on
404  *
405  * Assign a already signaled stub fence to the sync object.
406  */
drm_syncobj_assign_null_handle(struct drm_syncobj * syncobj)407 static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
408 {
409 	struct dma_fence *fence = dma_fence_allocate_private_stub(ktime_get());
410 
411 	if (!fence)
412 		return -ENOMEM;
413 
414 	drm_syncobj_replace_fence(syncobj, fence);
415 	dma_fence_put(fence);
416 	return 0;
417 }
418 
419 /* 5s default for wait submission */
420 #define DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT 5000000000ULL
421 /**
422  * drm_syncobj_find_fence - lookup and reference the fence in a sync object
423  * @file_private: drm file private pointer
424  * @handle: sync object handle to lookup.
425  * @point: timeline point
426  * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
427  * @fence: out parameter for the fence
428  *
429  * This is just a convenience function that combines drm_syncobj_find() and
430  * drm_syncobj_fence_get().
431  *
432  * Returns 0 on success or a negative error value on failure. On success @fence
433  * contains a reference to the fence, which must be released by calling
434  * dma_fence_put().
435  */
drm_syncobj_find_fence(struct drm_file * file_private,u32 handle,u64 point,u64 flags,struct dma_fence ** fence)436 int drm_syncobj_find_fence(struct drm_file *file_private,
437 			   u32 handle, u64 point, u64 flags,
438 			   struct dma_fence **fence)
439 {
440 	struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
441 	struct syncobj_wait_entry wait;
442 	u64 timeout = nsecs_to_jiffies64(DRM_SYNCOBJ_WAIT_FOR_SUBMIT_TIMEOUT);
443 	int ret;
444 
445 	if (!syncobj)
446 		return -ENOENT;
447 
448 	/* Waiting for userspace with locks help is illegal cause that can
449 	 * trivial deadlock with page faults for example. Make lockdep complain
450 	 * about it early on.
451 	 */
452 	if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
453 		might_sleep();
454 		lockdep_assert_none_held_once();
455 	}
456 
457 	*fence = drm_syncobj_fence_get(syncobj);
458 
459 	if (*fence) {
460 		ret = dma_fence_chain_find_seqno(fence, point);
461 		if (!ret) {
462 			/* If the requested seqno is already signaled
463 			 * drm_syncobj_find_fence may return a NULL
464 			 * fence. To make sure the recipient gets
465 			 * signalled, use a new fence instead.
466 			 */
467 			if (!*fence)
468 				*fence = dma_fence_get_stub();
469 
470 			goto out;
471 		}
472 		dma_fence_put(*fence);
473 	} else {
474 		ret = -EINVAL;
475 	}
476 
477 	if (!(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
478 		goto out;
479 
480 	memset(&wait, 0, sizeof(wait));
481 #ifdef __linux__
482 	wait.task = current;
483 #else
484 	wait.task = curproc;
485 #endif
486 	wait.point = point;
487 	drm_syncobj_fence_add_wait(syncobj, &wait);
488 
489 	do {
490 		set_current_state(TASK_INTERRUPTIBLE);
491 		if (wait.fence) {
492 			ret = 0;
493 			break;
494 		}
495                 if (timeout == 0) {
496                         ret = -ETIME;
497                         break;
498                 }
499 
500 		if (signal_pending(current)) {
501 			ret = -ERESTARTSYS;
502 			break;
503 		}
504 
505                 timeout = schedule_timeout(timeout);
506 	} while (1);
507 
508 	__set_current_state(TASK_RUNNING);
509 	*fence = wait.fence;
510 
511 	if (wait.node.next)
512 		drm_syncobj_remove_wait(syncobj, &wait);
513 
514 out:
515 	drm_syncobj_put(syncobj);
516 
517 	return ret;
518 }
519 EXPORT_SYMBOL(drm_syncobj_find_fence);
520 
521 /**
522  * drm_syncobj_free - free a sync object.
523  * @kref: kref to free.
524  *
525  * Only to be called from kref_put in drm_syncobj_put.
526  */
drm_syncobj_free(struct kref * kref)527 void drm_syncobj_free(struct kref *kref)
528 {
529 	struct drm_syncobj *syncobj = container_of(kref,
530 						   struct drm_syncobj,
531 						   refcount);
532 	struct syncobj_eventfd_entry *ev_fd_cur, *ev_fd_tmp;
533 
534 	drm_syncobj_replace_fence(syncobj, NULL);
535 
536 	list_for_each_entry_safe(ev_fd_cur, ev_fd_tmp, &syncobj->ev_fd_list, node)
537 		syncobj_eventfd_entry_free(ev_fd_cur);
538 
539 	kfree(syncobj);
540 }
541 EXPORT_SYMBOL(drm_syncobj_free);
542 
543 /**
544  * drm_syncobj_create - create a new syncobj
545  * @out_syncobj: returned syncobj
546  * @flags: DRM_SYNCOBJ_* flags
547  * @fence: if non-NULL, the syncobj will represent this fence
548  *
549  * This is the first function to create a sync object. After creating, drivers
550  * probably want to make it available to userspace, either through
551  * drm_syncobj_get_handle() or drm_syncobj_get_fd().
552  *
553  * Returns 0 on success or a negative error value on failure.
554  */
drm_syncobj_create(struct drm_syncobj ** out_syncobj,uint32_t flags,struct dma_fence * fence)555 int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
556 		       struct dma_fence *fence)
557 {
558 	int ret;
559 	struct drm_syncobj *syncobj;
560 
561 	syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
562 	if (!syncobj)
563 		return -ENOMEM;
564 
565 	kref_init(&syncobj->refcount);
566 	INIT_LIST_HEAD(&syncobj->cb_list);
567 	INIT_LIST_HEAD(&syncobj->ev_fd_list);
568 	mtx_init(&syncobj->lock, IPL_NONE);
569 
570 	if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
571 		ret = drm_syncobj_assign_null_handle(syncobj);
572 		if (ret < 0) {
573 			drm_syncobj_put(syncobj);
574 			return ret;
575 		}
576 	}
577 
578 	if (fence)
579 		drm_syncobj_replace_fence(syncobj, fence);
580 
581 	*out_syncobj = syncobj;
582 	return 0;
583 }
584 EXPORT_SYMBOL(drm_syncobj_create);
585 
586 /**
587  * drm_syncobj_get_handle - get a handle from a syncobj
588  * @file_private: drm file private pointer
589  * @syncobj: Sync object to export
590  * @handle: out parameter with the new handle
591  *
592  * Exports a sync object created with drm_syncobj_create() as a handle on
593  * @file_private to userspace.
594  *
595  * Returns 0 on success or a negative error value on failure.
596  */
drm_syncobj_get_handle(struct drm_file * file_private,struct drm_syncobj * syncobj,u32 * handle)597 int drm_syncobj_get_handle(struct drm_file *file_private,
598 			   struct drm_syncobj *syncobj, u32 *handle)
599 {
600 	int ret;
601 
602 	/* take a reference to put in the idr */
603 	drm_syncobj_get(syncobj);
604 
605 	idr_preload(GFP_KERNEL);
606 	spin_lock(&file_private->syncobj_table_lock);
607 	ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
608 	spin_unlock(&file_private->syncobj_table_lock);
609 
610 	idr_preload_end();
611 
612 	if (ret < 0) {
613 		drm_syncobj_put(syncobj);
614 		return ret;
615 	}
616 
617 	*handle = ret;
618 	return 0;
619 }
620 EXPORT_SYMBOL(drm_syncobj_get_handle);
621 
drm_syncobj_create_as_handle(struct drm_file * file_private,u32 * handle,uint32_t flags)622 static int drm_syncobj_create_as_handle(struct drm_file *file_private,
623 					u32 *handle, uint32_t flags)
624 {
625 	int ret;
626 	struct drm_syncobj *syncobj;
627 
628 	ret = drm_syncobj_create(&syncobj, flags, NULL);
629 	if (ret)
630 		return ret;
631 
632 	ret = drm_syncobj_get_handle(file_private, syncobj, handle);
633 	drm_syncobj_put(syncobj);
634 	return ret;
635 }
636 
drm_syncobj_destroy(struct drm_file * file_private,u32 handle)637 static int drm_syncobj_destroy(struct drm_file *file_private,
638 			       u32 handle)
639 {
640 	struct drm_syncobj *syncobj;
641 
642 	spin_lock(&file_private->syncobj_table_lock);
643 	syncobj = idr_remove(&file_private->syncobj_idr, handle);
644 	spin_unlock(&file_private->syncobj_table_lock);
645 
646 	if (!syncobj)
647 		return -EINVAL;
648 
649 	drm_syncobj_put(syncobj);
650 	return 0;
651 }
652 
653 #ifdef notyet
drm_syncobj_file_release(struct inode * inode,struct file * file)654 static int drm_syncobj_file_release(struct inode *inode, struct file *file)
655 {
656 	struct drm_syncobj *syncobj = file->private_data;
657 
658 	drm_syncobj_put(syncobj);
659 	return 0;
660 }
661 
662 static const struct file_operations drm_syncobj_file_fops = {
663 	.release = drm_syncobj_file_release,
664 };
665 #endif
666 
667 /**
668  * drm_syncobj_get_fd - get a file descriptor from a syncobj
669  * @syncobj: Sync object to export
670  * @p_fd: out parameter with the new file descriptor
671  *
672  * Exports a sync object created with drm_syncobj_create() as a file descriptor.
673  *
674  * Returns 0 on success or a negative error value on failure.
675  */
drm_syncobj_get_fd(struct drm_syncobj * syncobj,int * p_fd)676 int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
677 {
678 	STUB();
679 	return -ENOSYS;
680 #ifdef notyet
681 	struct file *file;
682 	int fd;
683 
684 	fd = get_unused_fd_flags(O_CLOEXEC);
685 	if (fd < 0)
686 		return fd;
687 
688 	file = anon_inode_getfile("syncobj_file",
689 				  &drm_syncobj_file_fops,
690 				  syncobj, 0);
691 	if (IS_ERR(file)) {
692 		put_unused_fd(fd);
693 		return PTR_ERR(file);
694 	}
695 
696 	drm_syncobj_get(syncobj);
697 	fd_install(fd, file);
698 
699 	*p_fd = fd;
700 	return 0;
701 #endif
702 }
703 EXPORT_SYMBOL(drm_syncobj_get_fd);
704 
drm_syncobj_handle_to_fd(struct drm_file * file_private,u32 handle,int * p_fd)705 static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
706 				    u32 handle, int *p_fd)
707 {
708 	struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
709 	int ret;
710 
711 	if (!syncobj)
712 		return -EINVAL;
713 
714 	ret = drm_syncobj_get_fd(syncobj, p_fd);
715 	drm_syncobj_put(syncobj);
716 	return ret;
717 }
718 
drm_syncobj_fd_to_handle(struct drm_file * file_private,int fd,u32 * handle)719 static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
720 				    int fd, u32 *handle)
721 {
722 	STUB();
723 	return -ENOSYS;
724 #ifdef notyet
725 	struct drm_syncobj *syncobj;
726 	struct fd f = fdget(fd);
727 	int ret;
728 
729 	if (!f.file)
730 		return -EINVAL;
731 
732 	if (f.file->f_op != &drm_syncobj_file_fops) {
733 		fdput(f);
734 		return -EINVAL;
735 	}
736 
737 	/* take a reference to put in the idr */
738 	syncobj = f.file->private_data;
739 	drm_syncobj_get(syncobj);
740 
741 	idr_preload(GFP_KERNEL);
742 	spin_lock(&file_private->syncobj_table_lock);
743 	ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
744 	spin_unlock(&file_private->syncobj_table_lock);
745 	idr_preload_end();
746 
747 	if (ret > 0) {
748 		*handle = ret;
749 		ret = 0;
750 	} else
751 		drm_syncobj_put(syncobj);
752 
753 	fdput(f);
754 	return ret;
755 #endif
756 }
757 
drm_syncobj_import_sync_file_fence(struct drm_file * file_private,int fd,int handle)758 static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
759 					      int fd, int handle)
760 {
761 	struct dma_fence *fence = sync_file_get_fence(fd);
762 	struct drm_syncobj *syncobj;
763 
764 	if (!fence)
765 		return -EINVAL;
766 
767 	syncobj = drm_syncobj_find(file_private, handle);
768 	if (!syncobj) {
769 		dma_fence_put(fence);
770 		return -ENOENT;
771 	}
772 
773 	drm_syncobj_replace_fence(syncobj, fence);
774 	dma_fence_put(fence);
775 	drm_syncobj_put(syncobj);
776 	return 0;
777 }
778 
drm_syncobj_export_sync_file(struct drm_file * file_private,int handle,int * p_fd)779 static int drm_syncobj_export_sync_file(struct drm_file *file_private,
780 					int handle, int *p_fd)
781 {
782 	int ret;
783 	struct dma_fence *fence;
784 	struct sync_file *sync_file;
785 	int fd = get_unused_fd_flags(O_CLOEXEC);
786 
787 	if (fd < 0)
788 		return fd;
789 
790 	ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
791 	if (ret)
792 		goto err_put_fd;
793 
794 	sync_file = sync_file_create(fence);
795 
796 	dma_fence_put(fence);
797 
798 	if (!sync_file) {
799 		ret = -EINVAL;
800 		goto err_put_fd;
801 	}
802 
803 	fd_install(fd, sync_file->file);
804 
805 	*p_fd = fd;
806 	return 0;
807 err_put_fd:
808 	put_unused_fd(fd);
809 	return ret;
810 }
811 /**
812  * drm_syncobj_open - initializes syncobj file-private structures at devnode open time
813  * @file_private: drm file-private structure to set up
814  *
815  * Called at device open time, sets up the structure for handling refcounting
816  * of sync objects.
817  */
818 void
drm_syncobj_open(struct drm_file * file_private)819 drm_syncobj_open(struct drm_file *file_private)
820 {
821 	idr_init_base(&file_private->syncobj_idr, 1);
822 	mtx_init(&file_private->syncobj_table_lock, IPL_NONE);
823 }
824 
825 static int
drm_syncobj_release_handle(int id,void * ptr,void * data)826 drm_syncobj_release_handle(int id, void *ptr, void *data)
827 {
828 	struct drm_syncobj *syncobj = ptr;
829 
830 	drm_syncobj_put(syncobj);
831 	return 0;
832 }
833 
834 /**
835  * drm_syncobj_release - release file-private sync object resources
836  * @file_private: drm file-private structure to clean up
837  *
838  * Called at close time when the filp is going away.
839  *
840  * Releases any remaining references on objects by this filp.
841  */
842 void
drm_syncobj_release(struct drm_file * file_private)843 drm_syncobj_release(struct drm_file *file_private)
844 {
845 	idr_for_each(&file_private->syncobj_idr,
846 		     &drm_syncobj_release_handle, file_private);
847 	idr_destroy(&file_private->syncobj_idr);
848 }
849 
850 int
drm_syncobj_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)851 drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
852 			 struct drm_file *file_private)
853 {
854 	struct drm_syncobj_create *args = data;
855 
856 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
857 		return -EOPNOTSUPP;
858 
859 	/* no valid flags yet */
860 	if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
861 		return -EINVAL;
862 
863 	return drm_syncobj_create_as_handle(file_private,
864 					    &args->handle, args->flags);
865 }
866 
867 int
drm_syncobj_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)868 drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
869 			  struct drm_file *file_private)
870 {
871 	struct drm_syncobj_destroy *args = data;
872 
873 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
874 		return -EOPNOTSUPP;
875 
876 	/* make sure padding is empty */
877 	if (args->pad)
878 		return -EINVAL;
879 	return drm_syncobj_destroy(file_private, args->handle);
880 }
881 
882 int
drm_syncobj_handle_to_fd_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)883 drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
884 				   struct drm_file *file_private)
885 {
886 	struct drm_syncobj_handle *args = data;
887 
888 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
889 		return -EOPNOTSUPP;
890 
891 	if (args->pad)
892 		return -EINVAL;
893 
894 	if (args->flags != 0 &&
895 	    args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
896 		return -EINVAL;
897 
898 	if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
899 		return drm_syncobj_export_sync_file(file_private, args->handle,
900 						    &args->fd);
901 
902 	return drm_syncobj_handle_to_fd(file_private, args->handle,
903 					&args->fd);
904 }
905 
906 int
drm_syncobj_fd_to_handle_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)907 drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
908 				   struct drm_file *file_private)
909 {
910 	struct drm_syncobj_handle *args = data;
911 
912 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
913 		return -EOPNOTSUPP;
914 
915 	if (args->pad)
916 		return -EINVAL;
917 
918 	if (args->flags != 0 &&
919 	    args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
920 		return -EINVAL;
921 
922 	if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
923 		return drm_syncobj_import_sync_file_fence(file_private,
924 							  args->fd,
925 							  args->handle);
926 
927 	return drm_syncobj_fd_to_handle(file_private, args->fd,
928 					&args->handle);
929 }
930 
931 
932 /*
933  * Try to flatten a dma_fence_chain into a dma_fence_array so that it can be
934  * added as timeline fence to a chain again.
935  */
drm_syncobj_flatten_chain(struct dma_fence ** f)936 static int drm_syncobj_flatten_chain(struct dma_fence **f)
937 {
938 	struct dma_fence_chain *chain = to_dma_fence_chain(*f);
939 	struct dma_fence *tmp, **fences;
940 	struct dma_fence_array *array;
941 	unsigned int count;
942 
943 	if (!chain)
944 		return 0;
945 
946 	count = 0;
947 	dma_fence_chain_for_each(tmp, &chain->base)
948 		++count;
949 
950 	fences = kmalloc_array(count, sizeof(*fences), GFP_KERNEL);
951 	if (!fences)
952 		return -ENOMEM;
953 
954 	count = 0;
955 	dma_fence_chain_for_each(tmp, &chain->base)
956 		fences[count++] = dma_fence_get(tmp);
957 
958 	array = dma_fence_array_create(count, fences,
959 				       dma_fence_context_alloc(1),
960 				       1, false);
961 	if (!array)
962 		goto free_fences;
963 
964 	dma_fence_put(*f);
965 	*f = &array->base;
966 	return 0;
967 
968 free_fences:
969 	while (count--)
970 		dma_fence_put(fences[count]);
971 
972 	kfree(fences);
973 	return -ENOMEM;
974 }
975 
drm_syncobj_transfer_to_timeline(struct drm_file * file_private,struct drm_syncobj_transfer * args)976 static int drm_syncobj_transfer_to_timeline(struct drm_file *file_private,
977 					    struct drm_syncobj_transfer *args)
978 {
979 	struct drm_syncobj *timeline_syncobj = NULL;
980 	struct dma_fence_chain *chain;
981 	struct dma_fence *fence;
982 	int ret;
983 
984 	timeline_syncobj = drm_syncobj_find(file_private, args->dst_handle);
985 	if (!timeline_syncobj) {
986 		return -ENOENT;
987 	}
988 	ret = drm_syncobj_find_fence(file_private, args->src_handle,
989 				     args->src_point, args->flags,
990 				     &fence);
991 	if (ret)
992 		goto err_put_timeline;
993 
994 	ret = drm_syncobj_flatten_chain(&fence);
995 	if (ret)
996 		goto err_free_fence;
997 
998 	chain = dma_fence_chain_alloc();
999 	if (!chain) {
1000 		ret = -ENOMEM;
1001 		goto err_free_fence;
1002 	}
1003 
1004 	drm_syncobj_add_point(timeline_syncobj, chain, fence, args->dst_point);
1005 err_free_fence:
1006 	dma_fence_put(fence);
1007 err_put_timeline:
1008 	drm_syncobj_put(timeline_syncobj);
1009 
1010 	return ret;
1011 }
1012 
1013 static int
drm_syncobj_transfer_to_binary(struct drm_file * file_private,struct drm_syncobj_transfer * args)1014 drm_syncobj_transfer_to_binary(struct drm_file *file_private,
1015 			       struct drm_syncobj_transfer *args)
1016 {
1017 	struct drm_syncobj *binary_syncobj = NULL;
1018 	struct dma_fence *fence;
1019 	int ret;
1020 
1021 	binary_syncobj = drm_syncobj_find(file_private, args->dst_handle);
1022 	if (!binary_syncobj)
1023 		return -ENOENT;
1024 	ret = drm_syncobj_find_fence(file_private, args->src_handle,
1025 				     args->src_point, args->flags, &fence);
1026 	if (ret)
1027 		goto err;
1028 	drm_syncobj_replace_fence(binary_syncobj, fence);
1029 	dma_fence_put(fence);
1030 err:
1031 	drm_syncobj_put(binary_syncobj);
1032 
1033 	return ret;
1034 }
1035 int
drm_syncobj_transfer_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1036 drm_syncobj_transfer_ioctl(struct drm_device *dev, void *data,
1037 			   struct drm_file *file_private)
1038 {
1039 	struct drm_syncobj_transfer *args = data;
1040 	int ret;
1041 
1042 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1043 		return -EOPNOTSUPP;
1044 
1045 	if (args->pad)
1046 		return -EINVAL;
1047 
1048 	if (args->dst_point)
1049 		ret = drm_syncobj_transfer_to_timeline(file_private, args);
1050 	else
1051 		ret = drm_syncobj_transfer_to_binary(file_private, args);
1052 
1053 	return ret;
1054 }
1055 
syncobj_wait_fence_func(struct dma_fence * fence,struct dma_fence_cb * cb)1056 static void syncobj_wait_fence_func(struct dma_fence *fence,
1057 				    struct dma_fence_cb *cb)
1058 {
1059 	struct syncobj_wait_entry *wait =
1060 		container_of(cb, struct syncobj_wait_entry, fence_cb);
1061 
1062 	wake_up_process(wait->task);
1063 }
1064 
syncobj_wait_syncobj_func(struct drm_syncobj * syncobj,struct syncobj_wait_entry * wait)1065 static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
1066 				      struct syncobj_wait_entry *wait)
1067 {
1068 	struct dma_fence *fence;
1069 
1070 	/* This happens inside the syncobj lock */
1071 	fence = rcu_dereference_protected(syncobj->fence,
1072 					  lockdep_is_held(&syncobj->lock));
1073 	dma_fence_get(fence);
1074 	if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
1075 		dma_fence_put(fence);
1076 		return;
1077 	} else if (!fence) {
1078 		wait->fence = dma_fence_get_stub();
1079 	} else {
1080 		wait->fence = fence;
1081 	}
1082 
1083 	wake_up_process(wait->task);
1084 	list_del_init(&wait->node);
1085 }
1086 
drm_syncobj_array_wait_timeout(struct drm_syncobj ** syncobjs,void __user * user_points,uint32_t count,uint32_t flags,signed long timeout,uint32_t * idx)1087 static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
1088 						  void __user *user_points,
1089 						  uint32_t count,
1090 						  uint32_t flags,
1091 						  signed long timeout,
1092 						  uint32_t *idx)
1093 {
1094 	struct syncobj_wait_entry *entries;
1095 	struct dma_fence *fence;
1096 	uint64_t *points;
1097 	uint32_t signaled_count, i;
1098 
1099 	if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1100 		     DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
1101 		lockdep_assert_none_held_once();
1102 
1103 	points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
1104 	if (points == NULL)
1105 		return -ENOMEM;
1106 
1107 	if (!user_points) {
1108 		memset(points, 0, count * sizeof(uint64_t));
1109 
1110 	} else if (copy_from_user(points, user_points,
1111 				  sizeof(uint64_t) * count)) {
1112 		timeout = -EFAULT;
1113 		goto err_free_points;
1114 	}
1115 
1116 	entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
1117 	if (!entries) {
1118 		timeout = -ENOMEM;
1119 		goto err_free_points;
1120 	}
1121 	/* Walk the list of sync objects and initialize entries.  We do
1122 	 * this up-front so that we can properly return -EINVAL if there is
1123 	 * a syncobj with a missing fence and then never have the chance of
1124 	 * returning -EINVAL again.
1125 	 */
1126 	signaled_count = 0;
1127 	for (i = 0; i < count; ++i) {
1128 		struct dma_fence *fence;
1129 
1130 #ifdef __linux__
1131 		entries[i].task = current;
1132 #else
1133 		entries[i].task = curproc;
1134 #endif
1135 		entries[i].point = points[i];
1136 		fence = drm_syncobj_fence_get(syncobjs[i]);
1137 		if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
1138 			dma_fence_put(fence);
1139 			if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1140 				     DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
1141 				continue;
1142 			} else {
1143 				timeout = -EINVAL;
1144 				goto cleanup_entries;
1145 			}
1146 		}
1147 
1148 		if (fence)
1149 			entries[i].fence = fence;
1150 		else
1151 			entries[i].fence = dma_fence_get_stub();
1152 
1153 		if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
1154 		    dma_fence_is_signaled(entries[i].fence)) {
1155 			if (signaled_count == 0 && idx)
1156 				*idx = i;
1157 			signaled_count++;
1158 		}
1159 	}
1160 
1161 	if (signaled_count == count ||
1162 	    (signaled_count > 0 &&
1163 	     !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
1164 		goto cleanup_entries;
1165 
1166 	/* There's a very annoying laxness in the dma_fence API here, in
1167 	 * that backends are not required to automatically report when a
1168 	 * fence is signaled prior to fence->ops->enable_signaling() being
1169 	 * called.  So here if we fail to match signaled_count, we need to
1170 	 * fallthough and try a 0 timeout wait!
1171 	 */
1172 
1173 	if (flags & (DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1174 		     DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)) {
1175 		for (i = 0; i < count; ++i)
1176 			drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
1177 	}
1178 
1179 	do {
1180 		set_current_state(TASK_INTERRUPTIBLE);
1181 
1182 		signaled_count = 0;
1183 		for (i = 0; i < count; ++i) {
1184 			fence = entries[i].fence;
1185 			if (!fence)
1186 				continue;
1187 
1188 			if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
1189 			    dma_fence_is_signaled(fence) ||
1190 			    (!entries[i].fence_cb.func &&
1191 			     dma_fence_add_callback(fence,
1192 						    &entries[i].fence_cb,
1193 						    syncobj_wait_fence_func))) {
1194 				/* The fence has been signaled */
1195 				if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
1196 					signaled_count++;
1197 				} else {
1198 					if (idx)
1199 						*idx = i;
1200 					goto done_waiting;
1201 				}
1202 			}
1203 		}
1204 
1205 		if (signaled_count == count)
1206 			goto done_waiting;
1207 
1208 		if (timeout == 0) {
1209 			timeout = -ETIME;
1210 			goto done_waiting;
1211 		}
1212 
1213 		if (signal_pending(current)) {
1214 			timeout = -ERESTARTSYS;
1215 			goto done_waiting;
1216 		}
1217 
1218 		timeout = schedule_timeout(timeout);
1219 	} while (1);
1220 
1221 done_waiting:
1222 	__set_current_state(TASK_RUNNING);
1223 
1224 cleanup_entries:
1225 	for (i = 0; i < count; ++i) {
1226 		drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
1227 		if (entries[i].fence_cb.func)
1228 			dma_fence_remove_callback(entries[i].fence,
1229 						  &entries[i].fence_cb);
1230 		dma_fence_put(entries[i].fence);
1231 	}
1232 	kfree(entries);
1233 
1234 err_free_points:
1235 	kfree(points);
1236 
1237 	return timeout;
1238 }
1239 
1240 /**
1241  * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
1242  *
1243  * @timeout_nsec: timeout nsec component in ns, 0 for poll
1244  *
1245  * Calculate the timeout in jiffies from an absolute time in sec/nsec.
1246  */
drm_timeout_abs_to_jiffies(int64_t timeout_nsec)1247 signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
1248 {
1249 	ktime_t abs_timeout, now;
1250 	u64 timeout_ns, timeout_jiffies64;
1251 
1252 	/* make 0 timeout means poll - absolute 0 doesn't seem valid */
1253 	if (timeout_nsec == 0)
1254 		return 0;
1255 
1256 	abs_timeout = ns_to_ktime(timeout_nsec);
1257 	now = ktime_get();
1258 
1259 	if (!ktime_after(abs_timeout, now))
1260 		return 0;
1261 
1262 	timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
1263 
1264 	timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
1265 	/*  clamp timeout to avoid infinite timeout */
1266 	if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
1267 		return MAX_SCHEDULE_TIMEOUT - 1;
1268 
1269 	return timeout_jiffies64 + 1;
1270 }
1271 EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
1272 
drm_syncobj_array_wait(struct drm_device * dev,struct drm_file * file_private,struct drm_syncobj_wait * wait,struct drm_syncobj_timeline_wait * timeline_wait,struct drm_syncobj ** syncobjs,bool timeline)1273 static int drm_syncobj_array_wait(struct drm_device *dev,
1274 				  struct drm_file *file_private,
1275 				  struct drm_syncobj_wait *wait,
1276 				  struct drm_syncobj_timeline_wait *timeline_wait,
1277 				  struct drm_syncobj **syncobjs, bool timeline)
1278 {
1279 	signed long timeout = 0;
1280 	uint32_t first = ~0;
1281 
1282 	if (!timeline) {
1283 		timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
1284 		timeout = drm_syncobj_array_wait_timeout(syncobjs,
1285 							 NULL,
1286 							 wait->count_handles,
1287 							 wait->flags,
1288 							 timeout, &first);
1289 		if (timeout < 0)
1290 			return timeout;
1291 		wait->first_signaled = first;
1292 	} else {
1293 		timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec);
1294 		timeout = drm_syncobj_array_wait_timeout(syncobjs,
1295 							 u64_to_user_ptr(timeline_wait->points),
1296 							 timeline_wait->count_handles,
1297 							 timeline_wait->flags,
1298 							 timeout, &first);
1299 		if (timeout < 0)
1300 			return timeout;
1301 		timeline_wait->first_signaled = first;
1302 	}
1303 	return 0;
1304 }
1305 
drm_syncobj_array_find(struct drm_file * file_private,void __user * user_handles,uint32_t count_handles,struct drm_syncobj *** syncobjs_out)1306 static int drm_syncobj_array_find(struct drm_file *file_private,
1307 				  void __user *user_handles,
1308 				  uint32_t count_handles,
1309 				  struct drm_syncobj ***syncobjs_out)
1310 {
1311 	uint32_t i, *handles;
1312 	struct drm_syncobj **syncobjs;
1313 	int ret;
1314 
1315 	handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1316 	if (handles == NULL)
1317 		return -ENOMEM;
1318 
1319 	if (copy_from_user(handles, user_handles,
1320 			   sizeof(uint32_t) * count_handles)) {
1321 		ret = -EFAULT;
1322 		goto err_free_handles;
1323 	}
1324 
1325 	syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1326 	if (syncobjs == NULL) {
1327 		ret = -ENOMEM;
1328 		goto err_free_handles;
1329 	}
1330 
1331 	for (i = 0; i < count_handles; i++) {
1332 		syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1333 		if (!syncobjs[i]) {
1334 			ret = -ENOENT;
1335 			goto err_put_syncobjs;
1336 		}
1337 	}
1338 
1339 	kfree(handles);
1340 	*syncobjs_out = syncobjs;
1341 	return 0;
1342 
1343 err_put_syncobjs:
1344 	while (i-- > 0)
1345 		drm_syncobj_put(syncobjs[i]);
1346 	kfree(syncobjs);
1347 err_free_handles:
1348 	kfree(handles);
1349 
1350 	return ret;
1351 }
1352 
drm_syncobj_array_free(struct drm_syncobj ** syncobjs,uint32_t count)1353 static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1354 				   uint32_t count)
1355 {
1356 	uint32_t i;
1357 
1358 	for (i = 0; i < count; i++)
1359 		drm_syncobj_put(syncobjs[i]);
1360 	kfree(syncobjs);
1361 }
1362 
1363 int
drm_syncobj_wait_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1364 drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1365 		       struct drm_file *file_private)
1366 {
1367 	struct drm_syncobj_wait *args = data;
1368 	struct drm_syncobj **syncobjs;
1369 	int ret = 0;
1370 
1371 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1372 		return -EOPNOTSUPP;
1373 
1374 	if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1375 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
1376 		return -EINVAL;
1377 
1378 	if (args->count_handles == 0)
1379 		return -EINVAL;
1380 
1381 	ret = drm_syncobj_array_find(file_private,
1382 				     u64_to_user_ptr(args->handles),
1383 				     args->count_handles,
1384 				     &syncobjs);
1385 	if (ret < 0)
1386 		return ret;
1387 
1388 	ret = drm_syncobj_array_wait(dev, file_private,
1389 				     args, NULL, syncobjs, false);
1390 
1391 	drm_syncobj_array_free(syncobjs, args->count_handles);
1392 
1393 	return ret;
1394 }
1395 
1396 int
drm_syncobj_timeline_wait_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1397 drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,
1398 				struct drm_file *file_private)
1399 {
1400 	struct drm_syncobj_timeline_wait *args = data;
1401 	struct drm_syncobj **syncobjs;
1402 	int ret = 0;
1403 
1404 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1405 		return -EOPNOTSUPP;
1406 
1407 	if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1408 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
1409 			    DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
1410 		return -EINVAL;
1411 
1412 	if (args->count_handles == 0)
1413 		return -EINVAL;
1414 
1415 	ret = drm_syncobj_array_find(file_private,
1416 				     u64_to_user_ptr(args->handles),
1417 				     args->count_handles,
1418 				     &syncobjs);
1419 	if (ret < 0)
1420 		return ret;
1421 
1422 	ret = drm_syncobj_array_wait(dev, file_private,
1423 				     NULL, args, syncobjs, true);
1424 
1425 	drm_syncobj_array_free(syncobjs, args->count_handles);
1426 
1427 	return ret;
1428 }
1429 
syncobj_eventfd_entry_fence_func(struct dma_fence * fence,struct dma_fence_cb * cb)1430 static void syncobj_eventfd_entry_fence_func(struct dma_fence *fence,
1431 					     struct dma_fence_cb *cb)
1432 {
1433 	struct syncobj_eventfd_entry *entry =
1434 		container_of(cb, struct syncobj_eventfd_entry, fence_cb);
1435 
1436 	eventfd_signal(entry->ev_fd_ctx, 1);
1437 	syncobj_eventfd_entry_free(entry);
1438 }
1439 
1440 static void
syncobj_eventfd_entry_func(struct drm_syncobj * syncobj,struct syncobj_eventfd_entry * entry)1441 syncobj_eventfd_entry_func(struct drm_syncobj *syncobj,
1442 			   struct syncobj_eventfd_entry *entry)
1443 {
1444 	int ret;
1445 	struct dma_fence *fence;
1446 
1447 	/* This happens inside the syncobj lock */
1448 	fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
1449 	if (!fence)
1450 		return;
1451 
1452 	ret = dma_fence_chain_find_seqno(&fence, entry->point);
1453 	if (ret != 0) {
1454 		/* The given seqno has not been submitted yet. */
1455 		dma_fence_put(fence);
1456 		return;
1457 	} else if (!fence) {
1458 		/* If dma_fence_chain_find_seqno returns 0 but sets the fence
1459 		 * to NULL, it implies that the given seqno is signaled and a
1460 		 * later seqno has already been submitted. Assign a stub fence
1461 		 * so that the eventfd still gets signaled below.
1462 		 */
1463 		fence = dma_fence_get_stub();
1464 	}
1465 
1466 	list_del_init(&entry->node);
1467 	entry->fence = fence;
1468 
1469 	if (entry->flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) {
1470 		eventfd_signal(entry->ev_fd_ctx, 1);
1471 		syncobj_eventfd_entry_free(entry);
1472 	} else {
1473 		ret = dma_fence_add_callback(fence, &entry->fence_cb,
1474 					     syncobj_eventfd_entry_fence_func);
1475 		if (ret == -ENOENT) {
1476 			eventfd_signal(entry->ev_fd_ctx, 1);
1477 			syncobj_eventfd_entry_free(entry);
1478 		}
1479 	}
1480 }
1481 
1482 int
drm_syncobj_eventfd_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1483 drm_syncobj_eventfd_ioctl(struct drm_device *dev, void *data,
1484 			  struct drm_file *file_private)
1485 {
1486 	return -EOPNOTSUPP;
1487 #ifdef notyet
1488 	struct drm_syncobj_eventfd *args = data;
1489 	struct drm_syncobj *syncobj;
1490 	struct eventfd_ctx *ev_fd_ctx;
1491 	struct syncobj_eventfd_entry *entry;
1492 
1493 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1494 		return -EOPNOTSUPP;
1495 
1496 	if (args->flags & ~DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE)
1497 		return -EINVAL;
1498 
1499 	if (args->pad)
1500 		return -EINVAL;
1501 
1502 	syncobj = drm_syncobj_find(file_private, args->handle);
1503 	if (!syncobj)
1504 		return -ENOENT;
1505 
1506 	ev_fd_ctx = eventfd_ctx_fdget(args->fd);
1507 	if (IS_ERR(ev_fd_ctx))
1508 		return PTR_ERR(ev_fd_ctx);
1509 
1510 	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1511 	if (!entry) {
1512 		eventfd_ctx_put(ev_fd_ctx);
1513 		return -ENOMEM;
1514 	}
1515 	entry->syncobj = syncobj;
1516 	entry->ev_fd_ctx = ev_fd_ctx;
1517 	entry->point = args->point;
1518 	entry->flags = args->flags;
1519 
1520 	drm_syncobj_add_eventfd(syncobj, entry);
1521 	drm_syncobj_put(syncobj);
1522 
1523 	return 0;
1524 #endif
1525 }
1526 
1527 int
drm_syncobj_reset_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1528 drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1529 			struct drm_file *file_private)
1530 {
1531 	struct drm_syncobj_array *args = data;
1532 	struct drm_syncobj **syncobjs;
1533 	uint32_t i;
1534 	int ret;
1535 
1536 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1537 		return -EOPNOTSUPP;
1538 
1539 	if (args->pad != 0)
1540 		return -EINVAL;
1541 
1542 	if (args->count_handles == 0)
1543 		return -EINVAL;
1544 
1545 	ret = drm_syncobj_array_find(file_private,
1546 				     u64_to_user_ptr(args->handles),
1547 				     args->count_handles,
1548 				     &syncobjs);
1549 	if (ret < 0)
1550 		return ret;
1551 
1552 	for (i = 0; i < args->count_handles; i++)
1553 		drm_syncobj_replace_fence(syncobjs[i], NULL);
1554 
1555 	drm_syncobj_array_free(syncobjs, args->count_handles);
1556 
1557 	return 0;
1558 }
1559 
1560 int
drm_syncobj_signal_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1561 drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1562 			 struct drm_file *file_private)
1563 {
1564 	struct drm_syncobj_array *args = data;
1565 	struct drm_syncobj **syncobjs;
1566 	uint32_t i;
1567 	int ret;
1568 
1569 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1570 		return -EOPNOTSUPP;
1571 
1572 	if (args->pad != 0)
1573 		return -EINVAL;
1574 
1575 	if (args->count_handles == 0)
1576 		return -EINVAL;
1577 
1578 	ret = drm_syncobj_array_find(file_private,
1579 				     u64_to_user_ptr(args->handles),
1580 				     args->count_handles,
1581 				     &syncobjs);
1582 	if (ret < 0)
1583 		return ret;
1584 
1585 	for (i = 0; i < args->count_handles; i++) {
1586 		ret = drm_syncobj_assign_null_handle(syncobjs[i]);
1587 		if (ret < 0)
1588 			break;
1589 	}
1590 
1591 	drm_syncobj_array_free(syncobjs, args->count_handles);
1592 
1593 	return ret;
1594 }
1595 
1596 int
drm_syncobj_timeline_signal_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1597 drm_syncobj_timeline_signal_ioctl(struct drm_device *dev, void *data,
1598 				  struct drm_file *file_private)
1599 {
1600 	struct drm_syncobj_timeline_array *args = data;
1601 	struct drm_syncobj **syncobjs;
1602 	struct dma_fence_chain **chains;
1603 	uint64_t *points;
1604 	uint32_t i, j;
1605 	int ret;
1606 
1607 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1608 		return -EOPNOTSUPP;
1609 
1610 	if (args->flags != 0)
1611 		return -EINVAL;
1612 
1613 	if (args->count_handles == 0)
1614 		return -EINVAL;
1615 
1616 	ret = drm_syncobj_array_find(file_private,
1617 				     u64_to_user_ptr(args->handles),
1618 				     args->count_handles,
1619 				     &syncobjs);
1620 	if (ret < 0)
1621 		return ret;
1622 
1623 	points = kmalloc_array(args->count_handles, sizeof(*points),
1624 			       GFP_KERNEL);
1625 	if (!points) {
1626 		ret = -ENOMEM;
1627 		goto out;
1628 	}
1629 	if (!u64_to_user_ptr(args->points)) {
1630 		memset(points, 0, args->count_handles * sizeof(uint64_t));
1631 	} else if (copy_from_user(points, u64_to_user_ptr(args->points),
1632 				  sizeof(uint64_t) * args->count_handles)) {
1633 		ret = -EFAULT;
1634 		goto err_points;
1635 	}
1636 
1637 	chains = kmalloc_array(args->count_handles, sizeof(void *), GFP_KERNEL);
1638 	if (!chains) {
1639 		ret = -ENOMEM;
1640 		goto err_points;
1641 	}
1642 	for (i = 0; i < args->count_handles; i++) {
1643 		chains[i] = dma_fence_chain_alloc();
1644 		if (!chains[i]) {
1645 			for (j = 0; j < i; j++)
1646 				dma_fence_chain_free(chains[j]);
1647 			ret = -ENOMEM;
1648 			goto err_chains;
1649 		}
1650 	}
1651 
1652 	for (i = 0; i < args->count_handles; i++) {
1653 		struct dma_fence *fence = dma_fence_get_stub();
1654 
1655 		drm_syncobj_add_point(syncobjs[i], chains[i],
1656 				      fence, points[i]);
1657 		dma_fence_put(fence);
1658 	}
1659 err_chains:
1660 	kfree(chains);
1661 err_points:
1662 	kfree(points);
1663 out:
1664 	drm_syncobj_array_free(syncobjs, args->count_handles);
1665 
1666 	return ret;
1667 }
1668 
drm_syncobj_query_ioctl(struct drm_device * dev,void * data,struct drm_file * file_private)1669 int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,
1670 			    struct drm_file *file_private)
1671 {
1672 	struct drm_syncobj_timeline_array *args = data;
1673 	struct drm_syncobj **syncobjs;
1674 	uint64_t __user *points = u64_to_user_ptr(args->points);
1675 	uint32_t i;
1676 	int ret;
1677 
1678 	if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ_TIMELINE))
1679 		return -EOPNOTSUPP;
1680 
1681 	if (args->flags & ~DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED)
1682 		return -EINVAL;
1683 
1684 	if (args->count_handles == 0)
1685 		return -EINVAL;
1686 
1687 	ret = drm_syncobj_array_find(file_private,
1688 				     u64_to_user_ptr(args->handles),
1689 				     args->count_handles,
1690 				     &syncobjs);
1691 	if (ret < 0)
1692 		return ret;
1693 
1694 	for (i = 0; i < args->count_handles; i++) {
1695 		struct dma_fence_chain *chain;
1696 		struct dma_fence *fence;
1697 		uint64_t point;
1698 
1699 		fence = drm_syncobj_fence_get(syncobjs[i]);
1700 		chain = to_dma_fence_chain(fence);
1701 		if (chain) {
1702 			struct dma_fence *iter, *last_signaled =
1703 				dma_fence_get(fence);
1704 
1705 			if (args->flags &
1706 			    DRM_SYNCOBJ_QUERY_FLAGS_LAST_SUBMITTED) {
1707 				point = fence->seqno;
1708 			} else {
1709 				dma_fence_chain_for_each(iter, fence) {
1710 					if (iter->context != fence->context) {
1711 						dma_fence_put(iter);
1712 						/* It is most likely that timeline has
1713 						* unorder points. */
1714 						break;
1715 					}
1716 					dma_fence_put(last_signaled);
1717 					last_signaled = dma_fence_get(iter);
1718 				}
1719 				point = dma_fence_is_signaled(last_signaled) ?
1720 					last_signaled->seqno :
1721 					to_dma_fence_chain(last_signaled)->prev_seqno;
1722 			}
1723 			dma_fence_put(last_signaled);
1724 		} else {
1725 			point = 0;
1726 		}
1727 		dma_fence_put(fence);
1728 		ret = copy_to_user(&points[i], &point, sizeof(uint64_t));
1729 		ret = ret ? -EFAULT : 0;
1730 		if (ret)
1731 			break;
1732 	}
1733 	drm_syncobj_array_free(syncobjs, args->count_handles);
1734 
1735 	return ret;
1736 }
1737