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