blob: 0bb174109b96988946dc1ce38fb2be21b9343aa8 [file] [log] [blame]
Dave Airliee9083422017-04-04 13:26:24 +10001/*
2 * Copyright 2017 Red Hat
Dave Airlie5e60a102017-08-25 10:52:22 -07003 * Parts ported from amdgpu (fence wait code).
4 * Copyright 2016 Advanced Micro Devices, Inc.
Dave Airliee9083422017-04-04 13:26:24 +10005 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 *
25 * Authors:
26 *
27 */
28
29/**
30 * DOC: Overview
31 *
32 * DRM synchronisation objects (syncobj) are a persistent objects,
33 * that contain an optional fence. The fence can be updated with a new
34 * fence, or be NULL.
35 *
Dave Airlie5e60a102017-08-25 10:52:22 -070036 * syncobj's can be waited upon, where it will wait for the underlying
37 * fence.
38 *
Dave Airliee9083422017-04-04 13:26:24 +100039 * syncobj's can be export to fd's and back, these fd's are opaque and
40 * have no other use case, except passing the syncobj between processes.
41 *
42 * Their primary use-case is to implement Vulkan fences and semaphores.
43 *
44 * syncobj have a kref reference count, but also have an optional file.
45 * The file is only created once the syncobj is exported.
46 * The file takes a reference on the kref.
47 */
48
49#include <drm/drmP.h>
50#include <linux/file.h>
51#include <linux/fs.h>
52#include <linux/anon_inodes.h>
Dave Airlie3ee45a32017-04-26 04:09:02 +010053#include <linux/sync_file.h>
Jason Ekstrande7aca5032017-08-25 10:52:24 -070054#include <linux/sched/signal.h>
Dave Airliee9083422017-04-04 13:26:24 +100055
56#include "drm_internal.h"
57#include <drm/drm_syncobj.h>
58
59/**
60 * drm_syncobj_find - lookup and reference a sync object.
61 * @file_private: drm file private pointer
62 * @handle: sync object handle to lookup.
63 *
64 * Returns a reference to the syncobj pointed to by handle or NULL.
65 */
66struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
67 u32 handle)
68{
69 struct drm_syncobj *syncobj;
70
71 spin_lock(&file_private->syncobj_table_lock);
72
73 /* Check if we currently have a reference on the object */
74 syncobj = idr_find(&file_private->syncobj_idr, handle);
75 if (syncobj)
76 drm_syncobj_get(syncobj);
77
78 spin_unlock(&file_private->syncobj_table_lock);
79
80 return syncobj;
81}
82EXPORT_SYMBOL(drm_syncobj_find);
83
Jason Ekstrand9c19fb12017-08-28 07:39:25 -070084static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
85 struct drm_syncobj_cb *cb,
86 drm_syncobj_func_t func)
87{
88 cb->func = func;
89 list_add_tail(&cb->node, &syncobj->cb_list);
90}
91
Jason Ekstrande7aca5032017-08-25 10:52:24 -070092static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
93 struct dma_fence **fence,
94 struct drm_syncobj_cb *cb,
95 drm_syncobj_func_t func)
96{
97 int ret;
98
99 *fence = drm_syncobj_fence_get(syncobj);
100 if (*fence)
101 return 1;
102
103 spin_lock(&syncobj->lock);
104 /* We've already tried once to get a fence and failed. Now that we
105 * have the lock, try one more time just to be sure we don't add a
106 * callback when a fence has already been set.
107 */
108 if (syncobj->fence) {
109 *fence = dma_fence_get(syncobj->fence);
110 ret = 1;
111 } else {
112 *fence = NULL;
113 drm_syncobj_add_callback_locked(syncobj, cb, func);
114 ret = 0;
115 }
116 spin_unlock(&syncobj->lock);
117
118 return ret;
119}
120
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700121/**
122 * drm_syncobj_add_callback - adds a callback to syncobj::cb_list
123 * @syncobj: Sync object to which to add the callback
124 * @cb: Callback to add
125 * @func: Func to use when initializing the drm_syncobj_cb struct
126 *
127 * This adds a callback to be called next time the fence is replaced
128 */
129void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
130 struct drm_syncobj_cb *cb,
131 drm_syncobj_func_t func)
132{
133 spin_lock(&syncobj->lock);
134 drm_syncobj_add_callback_locked(syncobj, cb, func);
135 spin_unlock(&syncobj->lock);
136}
137EXPORT_SYMBOL(drm_syncobj_add_callback);
138
139/**
140 * drm_syncobj_add_callback - removes a callback to syncobj::cb_list
141 * @syncobj: Sync object from which to remove the callback
142 * @cb: Callback to remove
143 */
144void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
145 struct drm_syncobj_cb *cb)
146{
147 spin_lock(&syncobj->lock);
148 list_del_init(&cb->node);
149 spin_unlock(&syncobj->lock);
150}
151EXPORT_SYMBOL(drm_syncobj_remove_callback);
152
Dave Airliee9083422017-04-04 13:26:24 +1000153/**
154 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000155 * @syncobj: Sync object to replace fence in
156 * @fence: fence to install in sync file.
157 *
158 * This replaces the fence on a sync object.
159 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100160void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Dave Airliee9083422017-04-04 13:26:24 +1000161 struct dma_fence *fence)
162{
Chris Wilson00fc2c22017-07-05 21:12:44 +0100163 struct dma_fence *old_fence;
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700164 struct drm_syncobj_cb *cur, *tmp;
Dave Airliee9083422017-04-04 13:26:24 +1000165
166 if (fence)
167 dma_fence_get(fence);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700168
169 spin_lock(&syncobj->lock);
170
171 old_fence = syncobj->fence;
172 syncobj->fence = fence;
173
174 if (fence != old_fence) {
175 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
176 list_del_init(&cur->node);
177 cur->func(syncobj, cur);
178 }
179 }
180
181 spin_unlock(&syncobj->lock);
Dave Airliee9083422017-04-04 13:26:24 +1000182
183 dma_fence_put(old_fence);
184}
185EXPORT_SYMBOL(drm_syncobj_replace_fence);
186
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700187struct drm_syncobj_null_fence {
188 struct dma_fence base;
189 spinlock_t lock;
190};
191
192static const char *drm_syncobj_null_fence_get_name(struct dma_fence *fence)
193{
194 return "syncobjnull";
195}
196
197static bool drm_syncobj_null_fence_enable_signaling(struct dma_fence *fence)
198{
199 dma_fence_enable_sw_signaling(fence);
200 return !dma_fence_is_signaled(fence);
201}
202
203static const struct dma_fence_ops drm_syncobj_null_fence_ops = {
204 .get_driver_name = drm_syncobj_null_fence_get_name,
205 .get_timeline_name = drm_syncobj_null_fence_get_name,
206 .enable_signaling = drm_syncobj_null_fence_enable_signaling,
207 .wait = dma_fence_default_wait,
208 .release = NULL,
209};
210
211static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
212{
213 struct drm_syncobj_null_fence *fence;
214 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
215 if (fence == NULL)
216 return -ENOMEM;
217
218 spin_lock_init(&fence->lock);
219 dma_fence_init(&fence->base, &drm_syncobj_null_fence_ops,
220 &fence->lock, 0, 0);
221 dma_fence_signal(&fence->base);
222
223 drm_syncobj_replace_fence(syncobj, &fence->base);
224
225 dma_fence_put(&fence->base);
226
227 return 0;
228}
229
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700230int drm_syncobj_find_fence(struct drm_file *file_private,
231 u32 handle,
232 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000233{
234 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
235 int ret = 0;
236
237 if (!syncobj)
238 return -ENOENT;
239
Jason Ekstrand309a5482017-08-25 10:52:20 -0700240 *fence = drm_syncobj_fence_get(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000241 if (!*fence) {
242 ret = -EINVAL;
243 }
244 drm_syncobj_put(syncobj);
245 return ret;
246}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700247EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000248
249/**
250 * drm_syncobj_free - free a sync object.
251 * @kref: kref to free.
252 *
253 * Only to be called from kref_put in drm_syncobj_put.
254 */
255void drm_syncobj_free(struct kref *kref)
256{
257 struct drm_syncobj *syncobj = container_of(kref,
258 struct drm_syncobj,
259 refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700260 drm_syncobj_replace_fence(syncobj, NULL);
Dave Airliee9083422017-04-04 13:26:24 +1000261 kfree(syncobj);
262}
263EXPORT_SYMBOL(drm_syncobj_free);
264
Marek Olšák1321fd22017-09-12 22:42:12 +0200265/**
266 * drm_syncobj_create - create a new syncobj
267 * @out_syncobj: returned syncobj
268 * @flags: DRM_SYNCOBJ_* flags
269 * @fence: if non-NULL, the syncobj will represent this fence
270 */
271int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
272 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000273{
274 int ret;
275 struct drm_syncobj *syncobj;
276
277 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
278 if (!syncobj)
279 return -ENOMEM;
280
281 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700282 INIT_LIST_HEAD(&syncobj->cb_list);
283 spin_lock_init(&syncobj->lock);
Dave Airliee9083422017-04-04 13:26:24 +1000284
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700285 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
286 ret = drm_syncobj_assign_null_handle(syncobj);
287 if (ret < 0) {
288 drm_syncobj_put(syncobj);
289 return ret;
290 }
291 }
292
Marek Olšák1321fd22017-09-12 22:42:12 +0200293 if (fence)
294 drm_syncobj_replace_fence(syncobj, fence);
295
296 *out_syncobj = syncobj;
297 return 0;
298}
299EXPORT_SYMBOL(drm_syncobj_create);
300
301/**
302 * drm_syncobj_get_handle - get a handle from a syncobj
303 */
304int drm_syncobj_get_handle(struct drm_file *file_private,
305 struct drm_syncobj *syncobj, u32 *handle)
306{
307 int ret;
308
309 /* take a reference to put in the idr */
310 drm_syncobj_get(syncobj);
311
Dave Airliee9083422017-04-04 13:26:24 +1000312 idr_preload(GFP_KERNEL);
313 spin_lock(&file_private->syncobj_table_lock);
314 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
315 spin_unlock(&file_private->syncobj_table_lock);
316
317 idr_preload_end();
318
319 if (ret < 0) {
320 drm_syncobj_put(syncobj);
321 return ret;
322 }
323
324 *handle = ret;
325 return 0;
326}
Marek Olšák1321fd22017-09-12 22:42:12 +0200327EXPORT_SYMBOL(drm_syncobj_get_handle);
328
329static int drm_syncobj_create_as_handle(struct drm_file *file_private,
330 u32 *handle, uint32_t flags)
331{
332 int ret;
333 struct drm_syncobj *syncobj;
334
335 ret = drm_syncobj_create(&syncobj, flags, NULL);
336 if (ret)
337 return ret;
338
339 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
340 drm_syncobj_put(syncobj);
341 return ret;
342}
Dave Airliee9083422017-04-04 13:26:24 +1000343
344static int drm_syncobj_destroy(struct drm_file *file_private,
345 u32 handle)
346{
347 struct drm_syncobj *syncobj;
348
349 spin_lock(&file_private->syncobj_table_lock);
350 syncobj = idr_remove(&file_private->syncobj_idr, handle);
351 spin_unlock(&file_private->syncobj_table_lock);
352
353 if (!syncobj)
354 return -EINVAL;
355
356 drm_syncobj_put(syncobj);
357 return 0;
358}
359
360static int drm_syncobj_file_release(struct inode *inode, struct file *file)
361{
362 struct drm_syncobj *syncobj = file->private_data;
363
364 drm_syncobj_put(syncobj);
365 return 0;
366}
367
368static const struct file_operations drm_syncobj_file_fops = {
369 .release = drm_syncobj_file_release,
370};
371
372static int drm_syncobj_alloc_file(struct drm_syncobj *syncobj)
373{
374 struct file *file = anon_inode_getfile("syncobj_file",
375 &drm_syncobj_file_fops,
376 syncobj, 0);
377 if (IS_ERR(file))
378 return PTR_ERR(file);
379
380 drm_syncobj_get(syncobj);
381 if (cmpxchg(&syncobj->file, NULL, file)) {
382 /* lost the race */
383 fput(file);
384 }
385
386 return 0;
387}
388
389static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
390 u32 handle, int *p_fd)
391{
392 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
393 int ret;
394 int fd;
395
396 if (!syncobj)
397 return -EINVAL;
398
399 fd = get_unused_fd_flags(O_CLOEXEC);
400 if (fd < 0) {
401 drm_syncobj_put(syncobj);
402 return fd;
403 }
404
405 if (!syncobj->file) {
406 ret = drm_syncobj_alloc_file(syncobj);
407 if (ret)
408 goto out_put_fd;
409 }
410 fd_install(fd, syncobj->file);
411 drm_syncobj_put(syncobj);
412 *p_fd = fd;
413 return 0;
414out_put_fd:
415 put_unused_fd(fd);
416 drm_syncobj_put(syncobj);
417 return ret;
418}
419
420static struct drm_syncobj *drm_syncobj_fdget(int fd)
421{
422 struct file *file = fget(fd);
423
424 if (!file)
425 return NULL;
426 if (file->f_op != &drm_syncobj_file_fops)
427 goto err;
428
429 return file->private_data;
430err:
431 fput(file);
432 return NULL;
433};
434
435static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
436 int fd, u32 *handle)
437{
438 struct drm_syncobj *syncobj = drm_syncobj_fdget(fd);
439 int ret;
440
441 if (!syncobj)
442 return -EINVAL;
443
444 /* take a reference to put in the idr */
445 drm_syncobj_get(syncobj);
446
447 idr_preload(GFP_KERNEL);
448 spin_lock(&file_private->syncobj_table_lock);
449 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
450 spin_unlock(&file_private->syncobj_table_lock);
451 idr_preload_end();
452
453 if (ret < 0) {
454 fput(syncobj->file);
455 return ret;
456 }
457 *handle = ret;
458 return 0;
459}
460
Dave Airlie3ee45a32017-04-26 04:09:02 +0100461int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
462 int fd, int handle)
463{
464 struct dma_fence *fence = sync_file_get_fence(fd);
465 struct drm_syncobj *syncobj;
466
467 if (!fence)
468 return -EINVAL;
469
470 syncobj = drm_syncobj_find(file_private, handle);
471 if (!syncobj) {
472 dma_fence_put(fence);
473 return -ENOENT;
474 }
475
Chris Wilson00fc2c22017-07-05 21:12:44 +0100476 drm_syncobj_replace_fence(syncobj, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100477 dma_fence_put(fence);
478 drm_syncobj_put(syncobj);
479 return 0;
480}
481
482int drm_syncobj_export_sync_file(struct drm_file *file_private,
483 int handle, int *p_fd)
484{
485 int ret;
486 struct dma_fence *fence;
487 struct sync_file *sync_file;
488 int fd = get_unused_fd_flags(O_CLOEXEC);
489
490 if (fd < 0)
491 return fd;
492
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700493 ret = drm_syncobj_find_fence(file_private, handle, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100494 if (ret)
495 goto err_put_fd;
496
497 sync_file = sync_file_create(fence);
498
499 dma_fence_put(fence);
500
501 if (!sync_file) {
502 ret = -EINVAL;
503 goto err_put_fd;
504 }
505
506 fd_install(fd, sync_file->file);
507
508 *p_fd = fd;
509 return 0;
510err_put_fd:
511 put_unused_fd(fd);
512 return ret;
513}
Dave Airliee9083422017-04-04 13:26:24 +1000514/**
515 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000516 * @file_private: drm file-private structure to set up
517 *
518 * Called at device open time, sets up the structure for handling refcounting
519 * of sync objects.
520 */
521void
522drm_syncobj_open(struct drm_file *file_private)
523{
524 idr_init(&file_private->syncobj_idr);
525 spin_lock_init(&file_private->syncobj_table_lock);
526}
527
528static int
529drm_syncobj_release_handle(int id, void *ptr, void *data)
530{
531 struct drm_syncobj *syncobj = ptr;
532
533 drm_syncobj_put(syncobj);
534 return 0;
535}
536
537/**
538 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000539 * @file_private: drm file-private structure to clean up
540 *
541 * Called at close time when the filp is going away.
542 *
543 * Releases any remaining references on objects by this filp.
544 */
545void
546drm_syncobj_release(struct drm_file *file_private)
547{
548 idr_for_each(&file_private->syncobj_idr,
549 &drm_syncobj_release_handle, file_private);
550 idr_destroy(&file_private->syncobj_idr);
551}
552
553int
554drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
555 struct drm_file *file_private)
556{
557 struct drm_syncobj_create *args = data;
558
559 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
560 return -ENODEV;
561
562 /* no valid flags yet */
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700563 if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
Dave Airliee9083422017-04-04 13:26:24 +1000564 return -EINVAL;
565
Marek Olšák1321fd22017-09-12 22:42:12 +0200566 return drm_syncobj_create_as_handle(file_private,
567 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000568}
569
570int
571drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
572 struct drm_file *file_private)
573{
574 struct drm_syncobj_destroy *args = data;
575
576 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
577 return -ENODEV;
578
579 /* make sure padding is empty */
580 if (args->pad)
581 return -EINVAL;
582 return drm_syncobj_destroy(file_private, args->handle);
583}
584
585int
586drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
587 struct drm_file *file_private)
588{
589 struct drm_syncobj_handle *args = data;
590
591 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
592 return -ENODEV;
593
Dave Airlie3ee45a32017-04-26 04:09:02 +0100594 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000595 return -EINVAL;
596
Dave Airlie3ee45a32017-04-26 04:09:02 +0100597 if (args->flags != 0 &&
598 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
599 return -EINVAL;
600
601 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
602 return drm_syncobj_export_sync_file(file_private, args->handle,
603 &args->fd);
604
Dave Airliee9083422017-04-04 13:26:24 +1000605 return drm_syncobj_handle_to_fd(file_private, args->handle,
606 &args->fd);
607}
608
609int
610drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
611 struct drm_file *file_private)
612{
613 struct drm_syncobj_handle *args = data;
614
615 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
616 return -ENODEV;
617
Dave Airlie3ee45a32017-04-26 04:09:02 +0100618 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000619 return -EINVAL;
620
Dave Airlie3ee45a32017-04-26 04:09:02 +0100621 if (args->flags != 0 &&
622 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
623 return -EINVAL;
624
625 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
626 return drm_syncobj_import_sync_file_fence(file_private,
627 args->fd,
628 args->handle);
629
Dave Airliee9083422017-04-04 13:26:24 +1000630 return drm_syncobj_fd_to_handle(file_private, args->fd,
631 &args->handle);
632}
Dave Airlie5e60a102017-08-25 10:52:22 -0700633
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700634struct syncobj_wait_entry {
635 struct task_struct *task;
636 struct dma_fence *fence;
637 struct dma_fence_cb fence_cb;
638 struct drm_syncobj_cb syncobj_cb;
639};
640
641static void syncobj_wait_fence_func(struct dma_fence *fence,
642 struct dma_fence_cb *cb)
643{
644 struct syncobj_wait_entry *wait =
645 container_of(cb, struct syncobj_wait_entry, fence_cb);
646
647 wake_up_process(wait->task);
648}
649
650static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
651 struct drm_syncobj_cb *cb)
652{
653 struct syncobj_wait_entry *wait =
654 container_of(cb, struct syncobj_wait_entry, syncobj_cb);
655
656 /* This happens inside the syncobj lock */
657 wait->fence = dma_fence_get(syncobj->fence);
658 wake_up_process(wait->task);
659}
660
661static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
662 uint32_t count,
663 uint32_t flags,
664 signed long timeout,
665 uint32_t *idx)
666{
667 struct syncobj_wait_entry *entries;
668 struct dma_fence *fence;
669 signed long ret;
670 uint32_t signaled_count, i;
671
672 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
673 if (!entries)
674 return -ENOMEM;
675
676 /* Walk the list of sync objects and initialize entries. We do
677 * this up-front so that we can properly return -EINVAL if there is
678 * a syncobj with a missing fence and then never have the chance of
679 * returning -EINVAL again.
680 */
681 signaled_count = 0;
682 for (i = 0; i < count; ++i) {
683 entries[i].task = current;
684 entries[i].fence = drm_syncobj_fence_get(syncobjs[i]);
685 if (!entries[i].fence) {
686 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
687 continue;
688 } else {
689 ret = -EINVAL;
690 goto cleanup_entries;
691 }
692 }
693
694 if (dma_fence_is_signaled(entries[i].fence)) {
695 if (signaled_count == 0 && idx)
696 *idx = i;
697 signaled_count++;
698 }
699 }
700
701 /* Initialize ret to the max of timeout and 1. That way, the
702 * default return value indicates a successful wait and not a
703 * timeout.
704 */
705 ret = max_t(signed long, timeout, 1);
706
707 if (signaled_count == count ||
708 (signaled_count > 0 &&
709 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
710 goto cleanup_entries;
711
712 /* There's a very annoying laxness in the dma_fence API here, in
713 * that backends are not required to automatically report when a
714 * fence is signaled prior to fence->ops->enable_signaling() being
715 * called. So here if we fail to match signaled_count, we need to
716 * fallthough and try a 0 timeout wait!
717 */
718
719 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
720 for (i = 0; i < count; ++i) {
721 drm_syncobj_fence_get_or_add_callback(syncobjs[i],
722 &entries[i].fence,
723 &entries[i].syncobj_cb,
724 syncobj_wait_syncobj_func);
725 }
726 }
727
728 do {
729 set_current_state(TASK_INTERRUPTIBLE);
730
731 signaled_count = 0;
732 for (i = 0; i < count; ++i) {
733 fence = entries[i].fence;
734 if (!fence)
735 continue;
736
737 if (dma_fence_is_signaled(fence) ||
738 (!entries[i].fence_cb.func &&
739 dma_fence_add_callback(fence,
740 &entries[i].fence_cb,
741 syncobj_wait_fence_func))) {
742 /* The fence has been signaled */
743 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
744 signaled_count++;
745 } else {
746 if (idx)
747 *idx = i;
748 goto done_waiting;
749 }
750 }
751 }
752
753 if (signaled_count == count)
754 goto done_waiting;
755
756 if (timeout == 0) {
757 /* If we are doing a 0 timeout wait and we got
758 * here, then we just timed out.
759 */
760 ret = 0;
761 goto done_waiting;
762 }
763
764 ret = schedule_timeout(ret);
765
766 if (ret > 0 && signal_pending(current))
767 ret = -ERESTARTSYS;
768 } while (ret > 0);
769
770done_waiting:
771 __set_current_state(TASK_RUNNING);
772
773cleanup_entries:
774 for (i = 0; i < count; ++i) {
775 if (entries[i].syncobj_cb.func)
776 drm_syncobj_remove_callback(syncobjs[i],
777 &entries[i].syncobj_cb);
778 if (entries[i].fence_cb.func)
779 dma_fence_remove_callback(entries[i].fence,
780 &entries[i].fence_cb);
781 dma_fence_put(entries[i].fence);
782 }
783 kfree(entries);
784
785 return ret;
786}
787
Dave Airlie5e60a102017-08-25 10:52:22 -0700788/**
789 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
790 *
791 * @timeout_nsec: timeout nsec component in ns, 0 for poll
792 *
793 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
794 */
795static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
796{
797 ktime_t abs_timeout, now;
798 u64 timeout_ns, timeout_jiffies64;
799
800 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
801 if (timeout_nsec == 0)
802 return 0;
803
804 abs_timeout = ns_to_ktime(timeout_nsec);
805 now = ktime_get();
806
807 if (!ktime_after(abs_timeout, now))
808 return 0;
809
810 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
811
812 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
813 /* clamp timeout to avoid infinite timeout */
814 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
815 return MAX_SCHEDULE_TIMEOUT - 1;
816
817 return timeout_jiffies64 + 1;
818}
819
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700820static int drm_syncobj_array_wait(struct drm_device *dev,
821 struct drm_file *file_private,
822 struct drm_syncobj_wait *wait,
823 struct drm_syncobj **syncobjs)
Dave Airlie5e60a102017-08-25 10:52:22 -0700824{
825 signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
826 signed long ret = 0;
827 uint32_t first = ~0;
828
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700829 ret = drm_syncobj_array_wait_timeout(syncobjs,
830 wait->count_handles,
831 wait->flags,
832 timeout, &first);
Dave Airlie5e60a102017-08-25 10:52:22 -0700833 if (ret < 0)
834 return ret;
835
836 wait->first_signaled = first;
837 if (ret == 0)
838 return -ETIME;
839 return 0;
840}
841
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700842static int drm_syncobj_array_find(struct drm_file *file_private,
843 void *user_handles, uint32_t count_handles,
844 struct drm_syncobj ***syncobjs_out)
845{
846 uint32_t i, *handles;
847 struct drm_syncobj **syncobjs;
848 int ret;
849
850 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
851 if (handles == NULL)
852 return -ENOMEM;
853
854 if (copy_from_user(handles, user_handles,
855 sizeof(uint32_t) * count_handles)) {
856 ret = -EFAULT;
857 goto err_free_handles;
858 }
859
860 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
861 if (syncobjs == NULL) {
862 ret = -ENOMEM;
863 goto err_free_handles;
864 }
865
866 for (i = 0; i < count_handles; i++) {
867 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
868 if (!syncobjs[i]) {
869 ret = -ENOENT;
870 goto err_put_syncobjs;
871 }
872 }
873
874 kfree(handles);
875 *syncobjs_out = syncobjs;
876 return 0;
877
878err_put_syncobjs:
879 while (i-- > 0)
880 drm_syncobj_put(syncobjs[i]);
881 kfree(syncobjs);
882err_free_handles:
883 kfree(handles);
884
885 return ret;
886}
887
888static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
889 uint32_t count)
890{
891 uint32_t i;
892 for (i = 0; i < count; i++)
893 drm_syncobj_put(syncobjs[i]);
894 kfree(syncobjs);
895}
896
Dave Airlie5e60a102017-08-25 10:52:22 -0700897int
898drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
899 struct drm_file *file_private)
900{
901 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700902 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -0700903 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -0700904
905 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
906 return -ENODEV;
907
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700908 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
909 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -0700910 return -EINVAL;
911
912 if (args->count_handles == 0)
913 return -EINVAL;
914
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700915 ret = drm_syncobj_array_find(file_private,
916 u64_to_user_ptr(args->handles),
917 args->count_handles,
918 &syncobjs);
919 if (ret < 0)
920 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -0700921
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700922 ret = drm_syncobj_array_wait(dev, file_private,
923 args, syncobjs);
Dave Airlie5e60a102017-08-25 10:52:22 -0700924
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700925 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -0700926
927 return ret;
928}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -0700929
930int
931drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
932 struct drm_file *file_private)
933{
934 struct drm_syncobj_array *args = data;
935 struct drm_syncobj **syncobjs;
936 uint32_t i;
937 int ret;
938
939 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
940 return -ENODEV;
941
942 if (args->pad != 0)
943 return -EINVAL;
944
945 if (args->count_handles == 0)
946 return -EINVAL;
947
948 ret = drm_syncobj_array_find(file_private,
949 u64_to_user_ptr(args->handles),
950 args->count_handles,
951 &syncobjs);
952 if (ret < 0)
953 return ret;
954
955 for (i = 0; i < args->count_handles; i++)
956 drm_syncobj_replace_fence(syncobjs[i], NULL);
957
958 drm_syncobj_array_free(syncobjs, args->count_handles);
959
960 return 0;
961}
Jason Ekstrandffa94432017-08-28 14:10:28 -0700962
963int
964drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
965 struct drm_file *file_private)
966{
967 struct drm_syncobj_array *args = data;
968 struct drm_syncobj **syncobjs;
969 uint32_t i;
970 int ret;
971
972 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
973 return -ENODEV;
974
975 if (args->pad != 0)
976 return -EINVAL;
977
978 if (args->count_handles == 0)
979 return -EINVAL;
980
981 ret = drm_syncobj_array_find(file_private,
982 u64_to_user_ptr(args->handles),
983 args->count_handles,
984 &syncobjs);
985 if (ret < 0)
986 return ret;
987
988 for (i = 0; i < args->count_handles; i++) {
989 ret = drm_syncobj_assign_null_handle(syncobjs[i]);
990 if (ret < 0)
991 break;
992 }
993
994 drm_syncobj_array_free(syncobjs, args->count_handles);
995
996 return ret;
997}