blob: 57bf6006394deb222365104564f613cd4a5e228c [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 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +010032 * DRM synchronisation objects (syncobj, see struct &drm_syncobj) are
33 * persistent objects that contain an optional fence. The fence can be updated
34 * with a new fence, or be NULL.
Dave Airliee9083422017-04-04 13:26:24 +100035 *
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
Chunming Zhou48197bc2018-10-18 14:18:36 +080059/* merge normal syncobj to timeline syncobj, the point interval is 1 */
60#define DRM_SYNCOBJ_BINARY_POINT 1
61
Chunming Zhoue28bd102018-08-30 14:48:28 +080062struct drm_syncobj_stub_fence {
63 struct dma_fence base;
64 spinlock_t lock;
65};
66
67static const char *drm_syncobj_stub_fence_get_name(struct dma_fence *fence)
68{
69 return "syncobjstub";
70}
71
Chunming Zhoue28bd102018-08-30 14:48:28 +080072static const struct dma_fence_ops drm_syncobj_stub_fence_ops = {
73 .get_driver_name = drm_syncobj_stub_fence_get_name,
74 .get_timeline_name = drm_syncobj_stub_fence_get_name,
Chunming Zhoue28bd102018-08-30 14:48:28 +080075};
76
Chunming Zhou48197bc2018-10-18 14:18:36 +080077struct drm_syncobj_signal_pt {
78 struct dma_fence_array *fence_array;
79 u64 value;
80 struct list_head list;
81};
Chunming Zhoue28bd102018-08-30 14:48:28 +080082
Dave Airliee9083422017-04-04 13:26:24 +100083/**
84 * drm_syncobj_find - lookup and reference a sync object.
85 * @file_private: drm file private pointer
86 * @handle: sync object handle to lookup.
87 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +010088 * Returns a reference to the syncobj pointed to by handle or NULL. The
89 * reference must be released by calling drm_syncobj_put().
Dave Airliee9083422017-04-04 13:26:24 +100090 */
91struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
92 u32 handle)
93{
94 struct drm_syncobj *syncobj;
95
96 spin_lock(&file_private->syncobj_table_lock);
97
98 /* Check if we currently have a reference on the object */
99 syncobj = idr_find(&file_private->syncobj_idr, handle);
100 if (syncobj)
101 drm_syncobj_get(syncobj);
102
103 spin_unlock(&file_private->syncobj_table_lock);
104
105 return syncobj;
106}
107EXPORT_SYMBOL(drm_syncobj_find);
108
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700109static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
110 struct drm_syncobj_cb *cb,
111 drm_syncobj_func_t func)
112{
113 cb->func = func;
114 list_add_tail(&cb->node, &syncobj->cb_list);
115}
116
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700117static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
118 struct dma_fence **fence,
119 struct drm_syncobj_cb *cb,
120 drm_syncobj_func_t func)
121{
122 int ret;
123
Chunming Zhou48197bc2018-10-18 14:18:36 +0800124 ret = drm_syncobj_search_fence(syncobj, 0, 0, fence);
125 if (!ret)
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700126 return 1;
127
128 spin_lock(&syncobj->lock);
129 /* We've already tried once to get a fence and failed. Now that we
130 * have the lock, try one more time just to be sure we don't add a
131 * callback when a fence has already been set.
132 */
Chunming Zhou48197bc2018-10-18 14:18:36 +0800133 if (!list_empty(&syncobj->signal_pt_list)) {
134 spin_unlock(&syncobj->lock);
135 drm_syncobj_search_fence(syncobj, 0, 0, fence);
136 if (*fence)
137 return 1;
138 spin_lock(&syncobj->lock);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700139 } else {
140 *fence = NULL;
141 drm_syncobj_add_callback_locked(syncobj, cb, func);
142 ret = 0;
143 }
144 spin_unlock(&syncobj->lock);
145
146 return ret;
147}
148
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700149void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
150 struct drm_syncobj_cb *cb,
151 drm_syncobj_func_t func)
152{
153 spin_lock(&syncobj->lock);
154 drm_syncobj_add_callback_locked(syncobj, cb, func);
155 spin_unlock(&syncobj->lock);
156}
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700157
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700158void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
159 struct drm_syncobj_cb *cb)
160{
161 spin_lock(&syncobj->lock);
162 list_del_init(&cb->node);
163 spin_unlock(&syncobj->lock);
164}
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700165
Chunming Zhou48197bc2018-10-18 14:18:36 +0800166static void drm_syncobj_init(struct drm_syncobj *syncobj)
167{
168 spin_lock(&syncobj->lock);
169 syncobj->timeline_context = dma_fence_context_alloc(1);
170 syncobj->timeline = 0;
171 syncobj->signal_point = 0;
172 init_waitqueue_head(&syncobj->wq);
173
174 INIT_LIST_HEAD(&syncobj->signal_pt_list);
175 spin_unlock(&syncobj->lock);
176}
177
178static void drm_syncobj_fini(struct drm_syncobj *syncobj)
179{
180 struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp;
181
182 spin_lock(&syncobj->lock);
183 list_for_each_entry_safe(signal_pt, tmp,
184 &syncobj->signal_pt_list, list) {
185 list_del(&signal_pt->list);
186 dma_fence_put(&signal_pt->fence_array->base);
187 kfree(signal_pt);
188 }
189 spin_unlock(&syncobj->lock);
190}
191
192static struct dma_fence
193*drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,
194 uint64_t point)
195{
196 struct drm_syncobj_signal_pt *signal_pt;
197
198 if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
199 (point <= syncobj->timeline)) {
200 struct drm_syncobj_stub_fence *fence =
201 kzalloc(sizeof(struct drm_syncobj_stub_fence),
202 GFP_KERNEL);
203
204 if (!fence)
205 return NULL;
206 spin_lock_init(&fence->lock);
207 dma_fence_init(&fence->base,
208 &drm_syncobj_stub_fence_ops,
209 &fence->lock,
210 syncobj->timeline_context,
211 point);
212
213 dma_fence_signal(&fence->base);
214 return &fence->base;
215 }
216
217 list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
218 if (point > signal_pt->value)
219 continue;
220 if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
221 (point != signal_pt->value))
222 continue;
223 return dma_fence_get(&signal_pt->fence_array->base);
224 }
225 return NULL;
226}
227
228static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
229 struct dma_fence *fence,
230 u64 point)
231{
232 struct drm_syncobj_signal_pt *signal_pt =
233 kzalloc(sizeof(struct drm_syncobj_signal_pt), GFP_KERNEL);
234 struct drm_syncobj_signal_pt *tail_pt;
235 struct dma_fence **fences;
236 int num_fences = 0;
237 int ret = 0, i;
238
239 if (!signal_pt)
240 return -ENOMEM;
241 if (!fence)
242 goto out;
243
244 fences = kmalloc_array(sizeof(void *), 2, GFP_KERNEL);
245 if (!fences) {
246 ret = -ENOMEM;
247 goto out;
248 }
249 fences[num_fences++] = dma_fence_get(fence);
250 /* timeline syncobj must take this dependency */
251 if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) {
252 spin_lock(&syncobj->lock);
253 if (!list_empty(&syncobj->signal_pt_list)) {
254 tail_pt = list_last_entry(&syncobj->signal_pt_list,
255 struct drm_syncobj_signal_pt, list);
256 fences[num_fences++] =
257 dma_fence_get(&tail_pt->fence_array->base);
258 }
259 spin_unlock(&syncobj->lock);
260 }
261 signal_pt->fence_array = dma_fence_array_create(num_fences, fences,
262 syncobj->timeline_context,
263 point, false);
264 if (!signal_pt->fence_array) {
265 ret = -ENOMEM;
266 goto fail;
267 }
268
269 spin_lock(&syncobj->lock);
270 if (syncobj->signal_point >= point) {
271 DRM_WARN("A later signal is ready!");
272 spin_unlock(&syncobj->lock);
273 goto exist;
274 }
275 signal_pt->value = point;
276 list_add_tail(&signal_pt->list, &syncobj->signal_pt_list);
277 syncobj->signal_point = point;
278 spin_unlock(&syncobj->lock);
279 wake_up_all(&syncobj->wq);
280
281 return 0;
282exist:
283 dma_fence_put(&signal_pt->fence_array->base);
284fail:
285 for (i = 0; i < num_fences; i++)
286 dma_fence_put(fences[i]);
287 kfree(fences);
288out:
289 kfree(signal_pt);
290 return ret;
291}
292
293static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj)
294{
295 struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt;
296
297 spin_lock(&syncobj->lock);
298 tail_pt = list_last_entry(&syncobj->signal_pt_list,
299 struct drm_syncobj_signal_pt,
300 list);
301 list_for_each_entry_safe(signal_pt, tmp,
302 &syncobj->signal_pt_list, list) {
303 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY &&
304 signal_pt == tail_pt)
305 continue;
306 if (dma_fence_is_signaled(&signal_pt->fence_array->base)) {
307 syncobj->timeline = signal_pt->value;
308 list_del(&signal_pt->list);
309 dma_fence_put(&signal_pt->fence_array->base);
310 kfree(signal_pt);
311 } else {
312 /*signal_pt is in order in list, from small to big, so
313 * the later must not be signal either */
314 break;
315 }
316 }
317
318 spin_unlock(&syncobj->lock);
319}
Dave Airliee9083422017-04-04 13:26:24 +1000320/**
321 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000322 * @syncobj: Sync object to replace fence in
Chunming Zhou9a09a422018-08-30 14:48:30 +0800323 * @point: timeline point
Dave Airliee9083422017-04-04 13:26:24 +1000324 * @fence: fence to install in sync file.
325 *
Chunming Zhou9a09a422018-08-30 14:48:30 +0800326 * This replaces the fence on a sync object, or a timeline point fence.
Dave Airliee9083422017-04-04 13:26:24 +1000327 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100328void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Chunming Zhou9a09a422018-08-30 14:48:30 +0800329 u64 point,
Dave Airliee9083422017-04-04 13:26:24 +1000330 struct dma_fence *fence)
331{
Chunming Zhou48197bc2018-10-18 14:18:36 +0800332 u64 pt_value = point;
Dave Airliee9083422017-04-04 13:26:24 +1000333
Chunming Zhou48197bc2018-10-18 14:18:36 +0800334 drm_syncobj_garbage_collection(syncobj);
335 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
336 if (!fence) {
337 drm_syncobj_fini(syncobj);
338 drm_syncobj_init(syncobj);
339 return;
340 }
341 pt_value = syncobj->signal_point +
342 DRM_SYNCOBJ_BINARY_POINT;
343 }
344 drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
345 if (fence) {
346 struct drm_syncobj_cb *cur, *tmp;
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700347
Chunming Zhou48197bc2018-10-18 14:18:36 +0800348 spin_lock(&syncobj->lock);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700349 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
350 list_del_init(&cur->node);
351 cur->func(syncobj, cur);
352 }
Chunming Zhou48197bc2018-10-18 14:18:36 +0800353 spin_unlock(&syncobj->lock);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700354 }
Dave Airliee9083422017-04-04 13:26:24 +1000355}
356EXPORT_SYMBOL(drm_syncobj_replace_fence);
357
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700358static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
359{
Chunming Zhoue28bd102018-08-30 14:48:28 +0800360 struct drm_syncobj_stub_fence *fence;
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700361 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
362 if (fence == NULL)
363 return -ENOMEM;
364
365 spin_lock_init(&fence->lock);
Chunming Zhoue28bd102018-08-30 14:48:28 +0800366 dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops,
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700367 &fence->lock, 0, 0);
368 dma_fence_signal(&fence->base);
369
Chunming Zhou9a09a422018-08-30 14:48:30 +0800370 drm_syncobj_replace_fence(syncobj, 0, &fence->base);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700371
372 dma_fence_put(&fence->base);
373
374 return 0;
375}
376
Chunming Zhou48197bc2018-10-18 14:18:36 +0800377static int
378drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
379 struct dma_fence **fence)
380{
381 int ret = 0;
382
383 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
384 ret = wait_event_interruptible(syncobj->wq,
385 point <= syncobj->signal_point);
386 if (ret < 0)
387 return ret;
388 }
389 spin_lock(&syncobj->lock);
390 *fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
391 if (!*fence)
392 ret = -EINVAL;
393 spin_unlock(&syncobj->lock);
394 return ret;
395}
396
397/**
398 * drm_syncobj_search_fence - lookup and reference the fence in a sync object or
399 * in a timeline point
400 * @syncobj: sync object pointer
401 * @point: timeline point
402 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
403 * @fence: out parameter for the fence
404 *
405 * if flags is DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, the function will block
406 * here until specific timeline points is reached.
407 * if not, you need a submit thread and block in userspace until all future
408 * timeline points have materialized, only then you can submit to the kernel,
409 * otherwise, function will fail to return fence.
410 *
411 * Returns 0 on success or a negative error value on failure. On success @fence
412 * contains a reference to the fence, which must be released by calling
413 * dma_fence_put().
414 */
415int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point,
416 u64 flags, struct dma_fence **fence)
417{
418 u64 pt_value = point;
419
420 if (!syncobj)
421 return -ENOENT;
422
423 drm_syncobj_garbage_collection(syncobj);
424 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
425 /*BINARY syncobj always wait on last pt */
426 pt_value = syncobj->signal_point;
427
428 if (pt_value == 0)
429 pt_value += DRM_SYNCOBJ_BINARY_POINT;
430 }
431 return drm_syncobj_point_get(syncobj, pt_value, flags, fence);
432}
433EXPORT_SYMBOL(drm_syncobj_search_fence);
434
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100435/**
436 * drm_syncobj_find_fence - lookup and reference the fence in a sync object
437 * @file_private: drm file private pointer
438 * @handle: sync object handle to lookup.
Chunming Zhou0a6730e2018-08-30 14:48:29 +0800439 * @point: timeline point
Chunming Zhou871edc92018-10-17 15:03:18 +0800440 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100441 * @fence: out parameter for the fence
442 *
443 * This is just a convenience function that combines drm_syncobj_find() and
Chunming Zhou48197bc2018-10-18 14:18:36 +0800444 * drm_syncobj_lookup_fence().
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100445 *
446 * Returns 0 on success or a negative error value on failure. On success @fence
447 * contains a reference to the fence, which must be released by calling
448 * dma_fence_put().
449 */
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700450int drm_syncobj_find_fence(struct drm_file *file_private,
Chunming Zhou649fdce2018-10-15 16:55:47 +0800451 u32 handle, u64 point, u64 flags,
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700452 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000453{
454 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800455 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000456
Chunming Zhou48197bc2018-10-18 14:18:36 +0800457 ret = drm_syncobj_search_fence(syncobj, point, flags, fence);
Dave Airliee9083422017-04-04 13:26:24 +1000458 drm_syncobj_put(syncobj);
459 return ret;
460}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700461EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000462
463/**
464 * drm_syncobj_free - free a sync object.
465 * @kref: kref to free.
466 *
467 * Only to be called from kref_put in drm_syncobj_put.
468 */
469void drm_syncobj_free(struct kref *kref)
470{
471 struct drm_syncobj *syncobj = container_of(kref,
472 struct drm_syncobj,
473 refcount);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800474 drm_syncobj_fini(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000475 kfree(syncobj);
476}
477EXPORT_SYMBOL(drm_syncobj_free);
478
Marek Olšák1321fd22017-09-12 22:42:12 +0200479/**
480 * drm_syncobj_create - create a new syncobj
481 * @out_syncobj: returned syncobj
482 * @flags: DRM_SYNCOBJ_* flags
483 * @fence: if non-NULL, the syncobj will represent this fence
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100484 *
485 * This is the first function to create a sync object. After creating, drivers
486 * probably want to make it available to userspace, either through
487 * drm_syncobj_get_handle() or drm_syncobj_get_fd().
488 *
489 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200490 */
491int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
492 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000493{
494 int ret;
495 struct drm_syncobj *syncobj;
496
497 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
498 if (!syncobj)
499 return -ENOMEM;
500
501 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700502 INIT_LIST_HEAD(&syncobj->cb_list);
503 spin_lock_init(&syncobj->lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800504 if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
505 syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
506 else
507 syncobj->type = DRM_SYNCOBJ_TYPE_BINARY;
508 drm_syncobj_init(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000509
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700510 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
511 ret = drm_syncobj_assign_null_handle(syncobj);
512 if (ret < 0) {
513 drm_syncobj_put(syncobj);
514 return ret;
515 }
516 }
517
Marek Olšák1321fd22017-09-12 22:42:12 +0200518 if (fence)
Chunming Zhou9a09a422018-08-30 14:48:30 +0800519 drm_syncobj_replace_fence(syncobj, 0, fence);
Marek Olšák1321fd22017-09-12 22:42:12 +0200520
521 *out_syncobj = syncobj;
522 return 0;
523}
524EXPORT_SYMBOL(drm_syncobj_create);
525
526/**
527 * drm_syncobj_get_handle - get a handle from a syncobj
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100528 * @file_private: drm file private pointer
529 * @syncobj: Sync object to export
530 * @handle: out parameter with the new handle
531 *
532 * Exports a sync object created with drm_syncobj_create() as a handle on
533 * @file_private to userspace.
534 *
535 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200536 */
537int drm_syncobj_get_handle(struct drm_file *file_private,
538 struct drm_syncobj *syncobj, u32 *handle)
539{
540 int ret;
541
542 /* take a reference to put in the idr */
543 drm_syncobj_get(syncobj);
544
Dave Airliee9083422017-04-04 13:26:24 +1000545 idr_preload(GFP_KERNEL);
546 spin_lock(&file_private->syncobj_table_lock);
547 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
548 spin_unlock(&file_private->syncobj_table_lock);
549
550 idr_preload_end();
551
552 if (ret < 0) {
553 drm_syncobj_put(syncobj);
554 return ret;
555 }
556
557 *handle = ret;
558 return 0;
559}
Marek Olšák1321fd22017-09-12 22:42:12 +0200560EXPORT_SYMBOL(drm_syncobj_get_handle);
561
562static int drm_syncobj_create_as_handle(struct drm_file *file_private,
563 u32 *handle, uint32_t flags)
564{
565 int ret;
566 struct drm_syncobj *syncobj;
567
568 ret = drm_syncobj_create(&syncobj, flags, NULL);
569 if (ret)
570 return ret;
571
572 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
573 drm_syncobj_put(syncobj);
574 return ret;
575}
Dave Airliee9083422017-04-04 13:26:24 +1000576
577static int drm_syncobj_destroy(struct drm_file *file_private,
578 u32 handle)
579{
580 struct drm_syncobj *syncobj;
581
582 spin_lock(&file_private->syncobj_table_lock);
583 syncobj = idr_remove(&file_private->syncobj_idr, handle);
584 spin_unlock(&file_private->syncobj_table_lock);
585
586 if (!syncobj)
587 return -EINVAL;
588
589 drm_syncobj_put(syncobj);
590 return 0;
591}
592
593static int drm_syncobj_file_release(struct inode *inode, struct file *file)
594{
595 struct drm_syncobj *syncobj = file->private_data;
596
597 drm_syncobj_put(syncobj);
598 return 0;
599}
600
601static const struct file_operations drm_syncobj_file_fops = {
602 .release = drm_syncobj_file_release,
603};
604
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100605/**
606 * drm_syncobj_get_fd - get a file descriptor from a syncobj
607 * @syncobj: Sync object to export
608 * @p_fd: out parameter with the new file descriptor
609 *
610 * Exports a sync object created with drm_syncobj_create() as a file descriptor.
611 *
612 * Returns 0 on success or a negative error value on failure.
613 */
Marek Olšák684fd0a2017-09-12 22:42:13 +0200614int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
615{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000616 struct file *file;
Marek Olšák684fd0a2017-09-12 22:42:13 +0200617 int fd;
618
619 fd = get_unused_fd_flags(O_CLOEXEC);
620 if (fd < 0)
621 return fd;
622
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000623 file = anon_inode_getfile("syncobj_file",
624 &drm_syncobj_file_fops,
625 syncobj, 0);
626 if (IS_ERR(file)) {
627 put_unused_fd(fd);
628 return PTR_ERR(file);
Marek Olšák684fd0a2017-09-12 22:42:13 +0200629 }
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000630
631 drm_syncobj_get(syncobj);
632 fd_install(fd, file);
633
Marek Olšák684fd0a2017-09-12 22:42:13 +0200634 *p_fd = fd;
635 return 0;
636}
637EXPORT_SYMBOL(drm_syncobj_get_fd);
638
Dave Airliee9083422017-04-04 13:26:24 +1000639static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
640 u32 handle, int *p_fd)
641{
642 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
643 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000644
645 if (!syncobj)
646 return -EINVAL;
647
Marek Olšák684fd0a2017-09-12 22:42:13 +0200648 ret = drm_syncobj_get_fd(syncobj, p_fd);
Dave Airliee9083422017-04-04 13:26:24 +1000649 drm_syncobj_put(syncobj);
650 return ret;
651}
652
Dave Airliee9083422017-04-04 13:26:24 +1000653static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
654 int fd, u32 *handle)
655{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000656 struct drm_syncobj *syncobj;
657 struct file *file;
Dave Airliee9083422017-04-04 13:26:24 +1000658 int ret;
659
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000660 file = fget(fd);
661 if (!file)
Dave Airliee9083422017-04-04 13:26:24 +1000662 return -EINVAL;
663
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000664 if (file->f_op != &drm_syncobj_file_fops) {
665 fput(file);
666 return -EINVAL;
667 }
668
Dave Airliee9083422017-04-04 13:26:24 +1000669 /* take a reference to put in the idr */
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000670 syncobj = file->private_data;
Dave Airliee9083422017-04-04 13:26:24 +1000671 drm_syncobj_get(syncobj);
672
673 idr_preload(GFP_KERNEL);
674 spin_lock(&file_private->syncobj_table_lock);
675 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
676 spin_unlock(&file_private->syncobj_table_lock);
677 idr_preload_end();
678
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000679 if (ret > 0) {
680 *handle = ret;
681 ret = 0;
682 } else
683 drm_syncobj_put(syncobj);
684
685 fput(file);
686 return ret;
Dave Airliee9083422017-04-04 13:26:24 +1000687}
688
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300689static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
690 int fd, int handle)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100691{
692 struct dma_fence *fence = sync_file_get_fence(fd);
693 struct drm_syncobj *syncobj;
694
695 if (!fence)
696 return -EINVAL;
697
698 syncobj = drm_syncobj_find(file_private, handle);
699 if (!syncobj) {
700 dma_fence_put(fence);
701 return -ENOENT;
702 }
703
Chunming Zhou9a09a422018-08-30 14:48:30 +0800704 drm_syncobj_replace_fence(syncobj, 0, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100705 dma_fence_put(fence);
706 drm_syncobj_put(syncobj);
707 return 0;
708}
709
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300710static int drm_syncobj_export_sync_file(struct drm_file *file_private,
711 int handle, int *p_fd)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100712{
713 int ret;
714 struct dma_fence *fence;
715 struct sync_file *sync_file;
716 int fd = get_unused_fd_flags(O_CLOEXEC);
717
718 if (fd < 0)
719 return fd;
720
Chunming Zhou649fdce2018-10-15 16:55:47 +0800721 ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100722 if (ret)
723 goto err_put_fd;
724
725 sync_file = sync_file_create(fence);
726
727 dma_fence_put(fence);
728
729 if (!sync_file) {
730 ret = -EINVAL;
731 goto err_put_fd;
732 }
733
734 fd_install(fd, sync_file->file);
735
736 *p_fd = fd;
737 return 0;
738err_put_fd:
739 put_unused_fd(fd);
740 return ret;
741}
Dave Airliee9083422017-04-04 13:26:24 +1000742/**
743 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000744 * @file_private: drm file-private structure to set up
745 *
746 * Called at device open time, sets up the structure for handling refcounting
747 * of sync objects.
748 */
749void
750drm_syncobj_open(struct drm_file *file_private)
751{
Chris Wilsone86584c2018-02-12 14:55:33 +0000752 idr_init_base(&file_private->syncobj_idr, 1);
Dave Airliee9083422017-04-04 13:26:24 +1000753 spin_lock_init(&file_private->syncobj_table_lock);
754}
755
756static int
757drm_syncobj_release_handle(int id, void *ptr, void *data)
758{
759 struct drm_syncobj *syncobj = ptr;
760
761 drm_syncobj_put(syncobj);
762 return 0;
763}
764
765/**
766 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000767 * @file_private: drm file-private structure to clean up
768 *
769 * Called at close time when the filp is going away.
770 *
771 * Releases any remaining references on objects by this filp.
772 */
773void
774drm_syncobj_release(struct drm_file *file_private)
775{
776 idr_for_each(&file_private->syncobj_idr,
777 &drm_syncobj_release_handle, file_private);
778 idr_destroy(&file_private->syncobj_idr);
779}
780
781int
782drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
783 struct drm_file *file_private)
784{
785 struct drm_syncobj_create *args = data;
786
787 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100788 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000789
790 /* no valid flags yet */
Chunming Zhou48197bc2018-10-18 14:18:36 +0800791 if (args->flags & ~(DRM_SYNCOBJ_CREATE_SIGNALED |
792 DRM_SYNCOBJ_CREATE_TYPE_TIMELINE))
Dave Airliee9083422017-04-04 13:26:24 +1000793 return -EINVAL;
794
Marek Olšák1321fd22017-09-12 22:42:12 +0200795 return drm_syncobj_create_as_handle(file_private,
796 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000797}
798
799int
800drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
801 struct drm_file *file_private)
802{
803 struct drm_syncobj_destroy *args = data;
804
805 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100806 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000807
808 /* make sure padding is empty */
809 if (args->pad)
810 return -EINVAL;
811 return drm_syncobj_destroy(file_private, args->handle);
812}
813
814int
815drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
816 struct drm_file *file_private)
817{
818 struct drm_syncobj_handle *args = data;
819
820 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100821 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000822
Dave Airlie3ee45a32017-04-26 04:09:02 +0100823 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000824 return -EINVAL;
825
Dave Airlie3ee45a32017-04-26 04:09:02 +0100826 if (args->flags != 0 &&
827 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
828 return -EINVAL;
829
830 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
831 return drm_syncobj_export_sync_file(file_private, args->handle,
832 &args->fd);
833
Dave Airliee9083422017-04-04 13:26:24 +1000834 return drm_syncobj_handle_to_fd(file_private, args->handle,
835 &args->fd);
836}
837
838int
839drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
840 struct drm_file *file_private)
841{
842 struct drm_syncobj_handle *args = data;
843
844 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100845 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000846
Dave Airlie3ee45a32017-04-26 04:09:02 +0100847 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000848 return -EINVAL;
849
Dave Airlie3ee45a32017-04-26 04:09:02 +0100850 if (args->flags != 0 &&
851 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
852 return -EINVAL;
853
854 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
855 return drm_syncobj_import_sync_file_fence(file_private,
856 args->fd,
857 args->handle);
858
Dave Airliee9083422017-04-04 13:26:24 +1000859 return drm_syncobj_fd_to_handle(file_private, args->fd,
860 &args->handle);
861}
Dave Airlie5e60a102017-08-25 10:52:22 -0700862
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700863struct syncobj_wait_entry {
864 struct task_struct *task;
865 struct dma_fence *fence;
866 struct dma_fence_cb fence_cb;
867 struct drm_syncobj_cb syncobj_cb;
868};
869
870static void syncobj_wait_fence_func(struct dma_fence *fence,
871 struct dma_fence_cb *cb)
872{
873 struct syncobj_wait_entry *wait =
874 container_of(cb, struct syncobj_wait_entry, fence_cb);
875
876 wake_up_process(wait->task);
877}
878
879static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
880 struct drm_syncobj_cb *cb)
881{
882 struct syncobj_wait_entry *wait =
883 container_of(cb, struct syncobj_wait_entry, syncobj_cb);
884
Chunming Zhou48197bc2018-10-18 14:18:36 +0800885 drm_syncobj_search_fence(syncobj, 0, 0, &wait->fence);
886
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700887 wake_up_process(wait->task);
888}
889
890static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
891 uint32_t count,
892 uint32_t flags,
893 signed long timeout,
894 uint32_t *idx)
895{
896 struct syncobj_wait_entry *entries;
897 struct dma_fence *fence;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700898 uint32_t signaled_count, i;
899
900 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
901 if (!entries)
902 return -ENOMEM;
903
904 /* Walk the list of sync objects and initialize entries. We do
905 * this up-front so that we can properly return -EINVAL if there is
906 * a syncobj with a missing fence and then never have the chance of
907 * returning -EINVAL again.
908 */
909 signaled_count = 0;
910 for (i = 0; i < count; ++i) {
911 entries[i].task = current;
Chunming Zhou48197bc2018-10-18 14:18:36 +0800912 drm_syncobj_search_fence(syncobjs[i], 0, 0,
913 &entries[i].fence);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700914 if (!entries[i].fence) {
915 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
916 continue;
917 } else {
Chris Wilson12fec622018-09-20 21:05:30 +0100918 timeout = -EINVAL;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700919 goto cleanup_entries;
920 }
921 }
922
923 if (dma_fence_is_signaled(entries[i].fence)) {
924 if (signaled_count == 0 && idx)
925 *idx = i;
926 signaled_count++;
927 }
928 }
929
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700930 if (signaled_count == count ||
931 (signaled_count > 0 &&
932 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
933 goto cleanup_entries;
934
935 /* There's a very annoying laxness in the dma_fence API here, in
936 * that backends are not required to automatically report when a
937 * fence is signaled prior to fence->ops->enable_signaling() being
938 * called. So here if we fail to match signaled_count, we need to
939 * fallthough and try a 0 timeout wait!
940 */
941
942 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
943 for (i = 0; i < count; ++i) {
944 drm_syncobj_fence_get_or_add_callback(syncobjs[i],
945 &entries[i].fence,
946 &entries[i].syncobj_cb,
947 syncobj_wait_syncobj_func);
948 }
949 }
950
951 do {
952 set_current_state(TASK_INTERRUPTIBLE);
953
954 signaled_count = 0;
955 for (i = 0; i < count; ++i) {
956 fence = entries[i].fence;
957 if (!fence)
958 continue;
959
960 if (dma_fence_is_signaled(fence) ||
961 (!entries[i].fence_cb.func &&
962 dma_fence_add_callback(fence,
963 &entries[i].fence_cb,
964 syncobj_wait_fence_func))) {
965 /* The fence has been signaled */
966 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
967 signaled_count++;
968 } else {
969 if (idx)
970 *idx = i;
971 goto done_waiting;
972 }
973 }
974 }
975
976 if (signaled_count == count)
977 goto done_waiting;
978
979 if (timeout == 0) {
Chris Wilson12fec622018-09-20 21:05:30 +0100980 timeout = -ETIME;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700981 goto done_waiting;
982 }
983
Chris Wilson12fec622018-09-20 21:05:30 +0100984 if (signal_pending(current)) {
985 timeout = -ERESTARTSYS;
986 goto done_waiting;
987 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700988
Chris Wilson12fec622018-09-20 21:05:30 +0100989 timeout = schedule_timeout(timeout);
990 } while (1);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700991
992done_waiting:
993 __set_current_state(TASK_RUNNING);
994
995cleanup_entries:
996 for (i = 0; i < count; ++i) {
997 if (entries[i].syncobj_cb.func)
998 drm_syncobj_remove_callback(syncobjs[i],
999 &entries[i].syncobj_cb);
1000 if (entries[i].fence_cb.func)
1001 dma_fence_remove_callback(entries[i].fence,
1002 &entries[i].fence_cb);
1003 dma_fence_put(entries[i].fence);
1004 }
1005 kfree(entries);
1006
Chris Wilson12fec622018-09-20 21:05:30 +01001007 return timeout;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001008}
1009
Dave Airlie5e60a102017-08-25 10:52:22 -07001010/**
1011 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
1012 *
1013 * @timeout_nsec: timeout nsec component in ns, 0 for poll
1014 *
1015 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
1016 */
1017static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
1018{
1019 ktime_t abs_timeout, now;
1020 u64 timeout_ns, timeout_jiffies64;
1021
1022 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
1023 if (timeout_nsec == 0)
1024 return 0;
1025
1026 abs_timeout = ns_to_ktime(timeout_nsec);
1027 now = ktime_get();
1028
1029 if (!ktime_after(abs_timeout, now))
1030 return 0;
1031
1032 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
1033
1034 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
1035 /* clamp timeout to avoid infinite timeout */
1036 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
1037 return MAX_SCHEDULE_TIMEOUT - 1;
1038
1039 return timeout_jiffies64 + 1;
1040}
1041
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001042static int drm_syncobj_array_wait(struct drm_device *dev,
1043 struct drm_file *file_private,
1044 struct drm_syncobj_wait *wait,
1045 struct drm_syncobj **syncobjs)
Dave Airlie5e60a102017-08-25 10:52:22 -07001046{
1047 signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
Dave Airlie5e60a102017-08-25 10:52:22 -07001048 uint32_t first = ~0;
1049
Chris Wilson12fec622018-09-20 21:05:30 +01001050 timeout = drm_syncobj_array_wait_timeout(syncobjs,
1051 wait->count_handles,
1052 wait->flags,
1053 timeout, &first);
1054 if (timeout < 0)
1055 return timeout;
Dave Airlie5e60a102017-08-25 10:52:22 -07001056
1057 wait->first_signaled = first;
Dave Airlie5e60a102017-08-25 10:52:22 -07001058 return 0;
1059}
1060
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001061static int drm_syncobj_array_find(struct drm_file *file_private,
Ville Syrjälä9e554462017-09-01 19:53:26 +03001062 void __user *user_handles,
1063 uint32_t count_handles,
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001064 struct drm_syncobj ***syncobjs_out)
1065{
1066 uint32_t i, *handles;
1067 struct drm_syncobj **syncobjs;
1068 int ret;
1069
1070 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1071 if (handles == NULL)
1072 return -ENOMEM;
1073
1074 if (copy_from_user(handles, user_handles,
1075 sizeof(uint32_t) * count_handles)) {
1076 ret = -EFAULT;
1077 goto err_free_handles;
1078 }
1079
1080 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1081 if (syncobjs == NULL) {
1082 ret = -ENOMEM;
1083 goto err_free_handles;
1084 }
1085
1086 for (i = 0; i < count_handles; i++) {
1087 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1088 if (!syncobjs[i]) {
1089 ret = -ENOENT;
1090 goto err_put_syncobjs;
1091 }
1092 }
1093
1094 kfree(handles);
1095 *syncobjs_out = syncobjs;
1096 return 0;
1097
1098err_put_syncobjs:
1099 while (i-- > 0)
1100 drm_syncobj_put(syncobjs[i]);
1101 kfree(syncobjs);
1102err_free_handles:
1103 kfree(handles);
1104
1105 return ret;
1106}
1107
1108static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1109 uint32_t count)
1110{
1111 uint32_t i;
1112 for (i = 0; i < count; i++)
1113 drm_syncobj_put(syncobjs[i]);
1114 kfree(syncobjs);
1115}
1116
Dave Airlie5e60a102017-08-25 10:52:22 -07001117int
1118drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1119 struct drm_file *file_private)
1120{
1121 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001122 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -07001123 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -07001124
1125 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001126 return -EOPNOTSUPP;
Dave Airlie5e60a102017-08-25 10:52:22 -07001127
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001128 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1129 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -07001130 return -EINVAL;
1131
1132 if (args->count_handles == 0)
1133 return -EINVAL;
1134
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001135 ret = drm_syncobj_array_find(file_private,
1136 u64_to_user_ptr(args->handles),
1137 args->count_handles,
1138 &syncobjs);
1139 if (ret < 0)
1140 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -07001141
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001142 ret = drm_syncobj_array_wait(dev, file_private,
1143 args, syncobjs);
Dave Airlie5e60a102017-08-25 10:52:22 -07001144
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001145 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -07001146
1147 return ret;
1148}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001149
1150int
1151drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1152 struct drm_file *file_private)
1153{
1154 struct drm_syncobj_array *args = data;
1155 struct drm_syncobj **syncobjs;
1156 uint32_t i;
1157 int ret;
1158
1159 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001160 return -EOPNOTSUPP;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001161
1162 if (args->pad != 0)
1163 return -EINVAL;
1164
1165 if (args->count_handles == 0)
1166 return -EINVAL;
1167
1168 ret = drm_syncobj_array_find(file_private,
1169 u64_to_user_ptr(args->handles),
1170 args->count_handles,
1171 &syncobjs);
1172 if (ret < 0)
1173 return ret;
1174
Chunming Zhou48197bc2018-10-18 14:18:36 +08001175 for (i = 0; i < args->count_handles; i++) {
1176 drm_syncobj_fini(syncobjs[i]);
1177 drm_syncobj_init(syncobjs[i]);
1178 }
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001179 drm_syncobj_array_free(syncobjs, args->count_handles);
1180
Chunming Zhou48197bc2018-10-18 14:18:36 +08001181 return ret;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001182}
Jason Ekstrandffa94432017-08-28 14:10:28 -07001183
1184int
1185drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1186 struct drm_file *file_private)
1187{
1188 struct drm_syncobj_array *args = data;
1189 struct drm_syncobj **syncobjs;
1190 uint32_t i;
1191 int ret;
1192
1193 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001194 return -EOPNOTSUPP;
Jason Ekstrandffa94432017-08-28 14:10:28 -07001195
1196 if (args->pad != 0)
1197 return -EINVAL;
1198
1199 if (args->count_handles == 0)
1200 return -EINVAL;
1201
1202 ret = drm_syncobj_array_find(file_private,
1203 u64_to_user_ptr(args->handles),
1204 args->count_handles,
1205 &syncobjs);
1206 if (ret < 0)
1207 return ret;
1208
1209 for (i = 0; i < args->count_handles; i++) {
1210 ret = drm_syncobj_assign_null_handle(syncobjs[i]);
1211 if (ret < 0)
1212 break;
1213 }
1214
1215 drm_syncobj_array_free(syncobjs, args->count_handles);
1216
1217 return ret;
1218}