blob: d3e2335b88f949eb62111a869d663270b5719b64 [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
Chunming Zhou4fb2c932018-10-26 14:20:27 +080083static DEFINE_SPINLOCK(signaled_fence_lock);
84static struct dma_fence signaled_fence;
85
86static struct dma_fence *drm_syncobj_get_stub_fence(void)
87{
88 spin_lock(&signaled_fence_lock);
89 if (!signaled_fence.ops) {
90 dma_fence_init(&signaled_fence,
91 &drm_syncobj_stub_fence_ops,
92 &signaled_fence_lock,
93 0, 0);
94 dma_fence_signal_locked(&signaled_fence);
95 }
96 spin_unlock(&signaled_fence_lock);
97
98 return dma_fence_get(&signaled_fence);
99}
Dave Airliee9083422017-04-04 13:26:24 +1000100/**
101 * drm_syncobj_find - lookup and reference a sync object.
102 * @file_private: drm file private pointer
103 * @handle: sync object handle to lookup.
104 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100105 * Returns a reference to the syncobj pointed to by handle or NULL. The
106 * reference must be released by calling drm_syncobj_put().
Dave Airliee9083422017-04-04 13:26:24 +1000107 */
108struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
109 u32 handle)
110{
111 struct drm_syncobj *syncobj;
112
113 spin_lock(&file_private->syncobj_table_lock);
114
115 /* Check if we currently have a reference on the object */
116 syncobj = idr_find(&file_private->syncobj_idr, handle);
117 if (syncobj)
118 drm_syncobj_get(syncobj);
119
120 spin_unlock(&file_private->syncobj_table_lock);
121
122 return syncobj;
123}
124EXPORT_SYMBOL(drm_syncobj_find);
125
Chunming Zhou48197bc2018-10-18 14:18:36 +0800126static struct dma_fence
127*drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,
128 uint64_t point)
129{
130 struct drm_syncobj_signal_pt *signal_pt;
131
132 if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) &&
Chunming Zhou4fb2c932018-10-26 14:20:27 +0800133 (point <= syncobj->timeline))
134 return drm_syncobj_get_stub_fence();
Chunming Zhou48197bc2018-10-18 14:18:36 +0800135
136 list_for_each_entry(signal_pt, &syncobj->signal_pt_list, list) {
137 if (point > signal_pt->value)
138 continue;
139 if ((syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) &&
140 (point != signal_pt->value))
141 continue;
142 return dma_fence_get(&signal_pt->fence_array->base);
143 }
144 return NULL;
145}
146
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800147static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj,
148 struct drm_syncobj_cb *cb,
149 drm_syncobj_func_t func)
150{
151 cb->func = func;
152 list_add_tail(&cb->node, &syncobj->cb_list);
153}
154
155static void drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj,
156 struct dma_fence **fence,
157 struct drm_syncobj_cb *cb,
158 drm_syncobj_func_t func)
159{
160 u64 pt_value = 0;
161
Jason Ekstrand337fe9f2018-09-26 02:17:03 -0500162 WARN_ON(*fence);
163
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800164 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
165 /*BINARY syncobj always wait on last pt */
166 pt_value = syncobj->signal_point;
167
168 if (pt_value == 0)
169 pt_value += DRM_SYNCOBJ_BINARY_POINT;
170 }
171
172 mutex_lock(&syncobj->cb_mutex);
173 spin_lock(&syncobj->pt_lock);
174 *fence = drm_syncobj_find_signal_pt_for_point(syncobj, pt_value);
175 spin_unlock(&syncobj->pt_lock);
176 if (!*fence)
177 drm_syncobj_add_callback_locked(syncobj, cb, func);
178 mutex_unlock(&syncobj->cb_mutex);
179}
180
181void drm_syncobj_add_callback(struct drm_syncobj *syncobj,
182 struct drm_syncobj_cb *cb,
183 drm_syncobj_func_t func)
184{
185 mutex_lock(&syncobj->cb_mutex);
186 drm_syncobj_add_callback_locked(syncobj, cb, func);
187 mutex_unlock(&syncobj->cb_mutex);
188}
189
190void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
191 struct drm_syncobj_cb *cb)
192{
193 mutex_lock(&syncobj->cb_mutex);
194 list_del_init(&cb->node);
195 mutex_unlock(&syncobj->cb_mutex);
196}
197
198static void drm_syncobj_init(struct drm_syncobj *syncobj)
199{
200 spin_lock(&syncobj->pt_lock);
201 syncobj->timeline_context = dma_fence_context_alloc(1);
202 syncobj->timeline = 0;
203 syncobj->signal_point = 0;
204 init_waitqueue_head(&syncobj->wq);
205
206 INIT_LIST_HEAD(&syncobj->signal_pt_list);
207 spin_unlock(&syncobj->pt_lock);
208}
209
210static void drm_syncobj_fini(struct drm_syncobj *syncobj)
211{
212 struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp;
213
214 spin_lock(&syncobj->pt_lock);
215 list_for_each_entry_safe(signal_pt, tmp,
216 &syncobj->signal_pt_list, list) {
217 list_del(&signal_pt->list);
218 dma_fence_put(&signal_pt->fence_array->base);
219 kfree(signal_pt);
220 }
221 spin_unlock(&syncobj->pt_lock);
222}
223
Chunming Zhou48197bc2018-10-18 14:18:36 +0800224static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
225 struct dma_fence *fence,
226 u64 point)
227{
228 struct drm_syncobj_signal_pt *signal_pt =
229 kzalloc(sizeof(struct drm_syncobj_signal_pt), GFP_KERNEL);
230 struct drm_syncobj_signal_pt *tail_pt;
231 struct dma_fence **fences;
232 int num_fences = 0;
233 int ret = 0, i;
234
235 if (!signal_pt)
236 return -ENOMEM;
237 if (!fence)
238 goto out;
239
240 fences = kmalloc_array(sizeof(void *), 2, GFP_KERNEL);
241 if (!fences) {
242 ret = -ENOMEM;
243 goto out;
244 }
245 fences[num_fences++] = dma_fence_get(fence);
246 /* timeline syncobj must take this dependency */
247 if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) {
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800248 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800249 if (!list_empty(&syncobj->signal_pt_list)) {
250 tail_pt = list_last_entry(&syncobj->signal_pt_list,
251 struct drm_syncobj_signal_pt, list);
252 fences[num_fences++] =
253 dma_fence_get(&tail_pt->fence_array->base);
254 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800255 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800256 }
257 signal_pt->fence_array = dma_fence_array_create(num_fences, fences,
258 syncobj->timeline_context,
259 point, false);
260 if (!signal_pt->fence_array) {
261 ret = -ENOMEM;
262 goto fail;
263 }
264
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800265 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800266 if (syncobj->signal_point >= point) {
267 DRM_WARN("A later signal is ready!");
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800268 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800269 goto exist;
270 }
271 signal_pt->value = point;
272 list_add_tail(&signal_pt->list, &syncobj->signal_pt_list);
273 syncobj->signal_point = point;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800274 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800275 wake_up_all(&syncobj->wq);
276
277 return 0;
278exist:
279 dma_fence_put(&signal_pt->fence_array->base);
280fail:
281 for (i = 0; i < num_fences; i++)
282 dma_fence_put(fences[i]);
283 kfree(fences);
284out:
285 kfree(signal_pt);
286 return ret;
287}
288
289static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj)
290{
291 struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt;
292
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800293 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800294 tail_pt = list_last_entry(&syncobj->signal_pt_list,
295 struct drm_syncobj_signal_pt,
296 list);
297 list_for_each_entry_safe(signal_pt, tmp,
298 &syncobj->signal_pt_list, list) {
299 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY &&
300 signal_pt == tail_pt)
301 continue;
302 if (dma_fence_is_signaled(&signal_pt->fence_array->base)) {
303 syncobj->timeline = signal_pt->value;
304 list_del(&signal_pt->list);
305 dma_fence_put(&signal_pt->fence_array->base);
306 kfree(signal_pt);
307 } else {
308 /*signal_pt is in order in list, from small to big, so
309 * the later must not be signal either */
310 break;
311 }
312 }
313
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800314 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800315}
Dave Airliee9083422017-04-04 13:26:24 +1000316/**
317 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000318 * @syncobj: Sync object to replace fence in
Chunming Zhou9a09a422018-08-30 14:48:30 +0800319 * @point: timeline point
Dave Airliee9083422017-04-04 13:26:24 +1000320 * @fence: fence to install in sync file.
321 *
Chunming Zhou9a09a422018-08-30 14:48:30 +0800322 * This replaces the fence on a sync object, or a timeline point fence.
Dave Airliee9083422017-04-04 13:26:24 +1000323 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100324void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Chunming Zhou9a09a422018-08-30 14:48:30 +0800325 u64 point,
Dave Airliee9083422017-04-04 13:26:24 +1000326 struct dma_fence *fence)
327{
Chunming Zhou48197bc2018-10-18 14:18:36 +0800328 u64 pt_value = point;
Dave Airliee9083422017-04-04 13:26:24 +1000329
Chunming Zhou48197bc2018-10-18 14:18:36 +0800330 drm_syncobj_garbage_collection(syncobj);
331 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
332 if (!fence) {
333 drm_syncobj_fini(syncobj);
334 drm_syncobj_init(syncobj);
335 return;
336 }
337 pt_value = syncobj->signal_point +
338 DRM_SYNCOBJ_BINARY_POINT;
339 }
340 drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
341 if (fence) {
342 struct drm_syncobj_cb *cur, *tmp;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800343 LIST_HEAD(cb_list);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700344
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800345 mutex_lock(&syncobj->cb_mutex);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700346 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
347 list_del_init(&cur->node);
348 cur->func(syncobj, cur);
349 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800350 mutex_unlock(&syncobj->cb_mutex);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700351 }
Dave Airliee9083422017-04-04 13:26:24 +1000352}
353EXPORT_SYMBOL(drm_syncobj_replace_fence);
354
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700355static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
356{
Chunming Zhoue28bd102018-08-30 14:48:28 +0800357 struct drm_syncobj_stub_fence *fence;
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700358 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
359 if (fence == NULL)
360 return -ENOMEM;
361
362 spin_lock_init(&fence->lock);
Chunming Zhoue28bd102018-08-30 14:48:28 +0800363 dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops,
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700364 &fence->lock, 0, 0);
365 dma_fence_signal(&fence->base);
366
Chunming Zhou9a09a422018-08-30 14:48:30 +0800367 drm_syncobj_replace_fence(syncobj, 0, &fence->base);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700368
369 dma_fence_put(&fence->base);
370
371 return 0;
372}
373
Chunming Zhou48197bc2018-10-18 14:18:36 +0800374static int
375drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
376 struct dma_fence **fence)
377{
378 int ret = 0;
379
380 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
381 ret = wait_event_interruptible(syncobj->wq,
382 point <= syncobj->signal_point);
383 if (ret < 0)
384 return ret;
385 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800386 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800387 *fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
388 if (!*fence)
389 ret = -EINVAL;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800390 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800391 return ret;
392}
393
394/**
395 * drm_syncobj_search_fence - lookup and reference the fence in a sync object or
396 * in a timeline point
397 * @syncobj: sync object pointer
398 * @point: timeline point
399 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
400 * @fence: out parameter for the fence
401 *
402 * if flags is DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, the function will block
403 * here until specific timeline points is reached.
404 * if not, you need a submit thread and block in userspace until all future
405 * timeline points have materialized, only then you can submit to the kernel,
406 * otherwise, function will fail to return fence.
407 *
408 * Returns 0 on success or a negative error value on failure. On success @fence
409 * contains a reference to the fence, which must be released by calling
410 * dma_fence_put().
411 */
412int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point,
413 u64 flags, struct dma_fence **fence)
414{
415 u64 pt_value = point;
416
417 if (!syncobj)
418 return -ENOENT;
419
420 drm_syncobj_garbage_collection(syncobj);
421 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
422 /*BINARY syncobj always wait on last pt */
423 pt_value = syncobj->signal_point;
424
425 if (pt_value == 0)
426 pt_value += DRM_SYNCOBJ_BINARY_POINT;
427 }
428 return drm_syncobj_point_get(syncobj, pt_value, flags, fence);
429}
430EXPORT_SYMBOL(drm_syncobj_search_fence);
431
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100432/**
433 * drm_syncobj_find_fence - lookup and reference the fence in a sync object
434 * @file_private: drm file private pointer
435 * @handle: sync object handle to lookup.
Chunming Zhou0a6730e2018-08-30 14:48:29 +0800436 * @point: timeline point
Chunming Zhou871edc92018-10-17 15:03:18 +0800437 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100438 * @fence: out parameter for the fence
439 *
440 * This is just a convenience function that combines drm_syncobj_find() and
Chunming Zhou48197bc2018-10-18 14:18:36 +0800441 * drm_syncobj_lookup_fence().
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100442 *
443 * Returns 0 on success or a negative error value on failure. On success @fence
444 * contains a reference to the fence, which must be released by calling
445 * dma_fence_put().
446 */
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700447int drm_syncobj_find_fence(struct drm_file *file_private,
Chunming Zhou649fdce2018-10-15 16:55:47 +0800448 u32 handle, u64 point, u64 flags,
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700449 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000450{
451 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800452 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000453
Chunming Zhou48197bc2018-10-18 14:18:36 +0800454 ret = drm_syncobj_search_fence(syncobj, point, flags, fence);
Dave Airliee9083422017-04-04 13:26:24 +1000455 drm_syncobj_put(syncobj);
456 return ret;
457}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700458EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000459
460/**
461 * drm_syncobj_free - free a sync object.
462 * @kref: kref to free.
463 *
464 * Only to be called from kref_put in drm_syncobj_put.
465 */
466void drm_syncobj_free(struct kref *kref)
467{
468 struct drm_syncobj *syncobj = container_of(kref,
469 struct drm_syncobj,
470 refcount);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800471 drm_syncobj_fini(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000472 kfree(syncobj);
473}
474EXPORT_SYMBOL(drm_syncobj_free);
475
Marek Olšák1321fd22017-09-12 22:42:12 +0200476/**
477 * drm_syncobj_create - create a new syncobj
478 * @out_syncobj: returned syncobj
479 * @flags: DRM_SYNCOBJ_* flags
480 * @fence: if non-NULL, the syncobj will represent this fence
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100481 *
482 * This is the first function to create a sync object. After creating, drivers
483 * probably want to make it available to userspace, either through
484 * drm_syncobj_get_handle() or drm_syncobj_get_fd().
485 *
486 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200487 */
488int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
489 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000490{
491 int ret;
492 struct drm_syncobj *syncobj;
493
494 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
495 if (!syncobj)
496 return -ENOMEM;
497
498 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700499 INIT_LIST_HEAD(&syncobj->cb_list);
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800500 spin_lock_init(&syncobj->pt_lock);
501 mutex_init(&syncobj->cb_mutex);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800502 if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
503 syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
504 else
505 syncobj->type = DRM_SYNCOBJ_TYPE_BINARY;
506 drm_syncobj_init(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000507
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700508 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
509 ret = drm_syncobj_assign_null_handle(syncobj);
510 if (ret < 0) {
511 drm_syncobj_put(syncobj);
512 return ret;
513 }
514 }
515
Marek Olšák1321fd22017-09-12 22:42:12 +0200516 if (fence)
Chunming Zhou9a09a422018-08-30 14:48:30 +0800517 drm_syncobj_replace_fence(syncobj, 0, fence);
Marek Olšák1321fd22017-09-12 22:42:12 +0200518
519 *out_syncobj = syncobj;
520 return 0;
521}
522EXPORT_SYMBOL(drm_syncobj_create);
523
524/**
525 * drm_syncobj_get_handle - get a handle from a syncobj
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100526 * @file_private: drm file private pointer
527 * @syncobj: Sync object to export
528 * @handle: out parameter with the new handle
529 *
530 * Exports a sync object created with drm_syncobj_create() as a handle on
531 * @file_private to userspace.
532 *
533 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200534 */
535int drm_syncobj_get_handle(struct drm_file *file_private,
536 struct drm_syncobj *syncobj, u32 *handle)
537{
538 int ret;
539
540 /* take a reference to put in the idr */
541 drm_syncobj_get(syncobj);
542
Dave Airliee9083422017-04-04 13:26:24 +1000543 idr_preload(GFP_KERNEL);
544 spin_lock(&file_private->syncobj_table_lock);
545 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
546 spin_unlock(&file_private->syncobj_table_lock);
547
548 idr_preload_end();
549
550 if (ret < 0) {
551 drm_syncobj_put(syncobj);
552 return ret;
553 }
554
555 *handle = ret;
556 return 0;
557}
Marek Olšák1321fd22017-09-12 22:42:12 +0200558EXPORT_SYMBOL(drm_syncobj_get_handle);
559
560static int drm_syncobj_create_as_handle(struct drm_file *file_private,
561 u32 *handle, uint32_t flags)
562{
563 int ret;
564 struct drm_syncobj *syncobj;
565
566 ret = drm_syncobj_create(&syncobj, flags, NULL);
567 if (ret)
568 return ret;
569
570 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
571 drm_syncobj_put(syncobj);
572 return ret;
573}
Dave Airliee9083422017-04-04 13:26:24 +1000574
575static int drm_syncobj_destroy(struct drm_file *file_private,
576 u32 handle)
577{
578 struct drm_syncobj *syncobj;
579
580 spin_lock(&file_private->syncobj_table_lock);
581 syncobj = idr_remove(&file_private->syncobj_idr, handle);
582 spin_unlock(&file_private->syncobj_table_lock);
583
584 if (!syncobj)
585 return -EINVAL;
586
587 drm_syncobj_put(syncobj);
588 return 0;
589}
590
591static int drm_syncobj_file_release(struct inode *inode, struct file *file)
592{
593 struct drm_syncobj *syncobj = file->private_data;
594
595 drm_syncobj_put(syncobj);
596 return 0;
597}
598
599static const struct file_operations drm_syncobj_file_fops = {
600 .release = drm_syncobj_file_release,
601};
602
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100603/**
604 * drm_syncobj_get_fd - get a file descriptor from a syncobj
605 * @syncobj: Sync object to export
606 * @p_fd: out parameter with the new file descriptor
607 *
608 * Exports a sync object created with drm_syncobj_create() as a file descriptor.
609 *
610 * Returns 0 on success or a negative error value on failure.
611 */
Marek Olšák684fd0a2017-09-12 22:42:13 +0200612int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
613{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000614 struct file *file;
Marek Olšák684fd0a2017-09-12 22:42:13 +0200615 int fd;
616
617 fd = get_unused_fd_flags(O_CLOEXEC);
618 if (fd < 0)
619 return fd;
620
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000621 file = anon_inode_getfile("syncobj_file",
622 &drm_syncobj_file_fops,
623 syncobj, 0);
624 if (IS_ERR(file)) {
625 put_unused_fd(fd);
626 return PTR_ERR(file);
Marek Olšák684fd0a2017-09-12 22:42:13 +0200627 }
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000628
629 drm_syncobj_get(syncobj);
630 fd_install(fd, file);
631
Marek Olšák684fd0a2017-09-12 22:42:13 +0200632 *p_fd = fd;
633 return 0;
634}
635EXPORT_SYMBOL(drm_syncobj_get_fd);
636
Dave Airliee9083422017-04-04 13:26:24 +1000637static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
638 u32 handle, int *p_fd)
639{
640 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
641 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000642
643 if (!syncobj)
644 return -EINVAL;
645
Marek Olšák684fd0a2017-09-12 22:42:13 +0200646 ret = drm_syncobj_get_fd(syncobj, p_fd);
Dave Airliee9083422017-04-04 13:26:24 +1000647 drm_syncobj_put(syncobj);
648 return ret;
649}
650
Dave Airliee9083422017-04-04 13:26:24 +1000651static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
652 int fd, u32 *handle)
653{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000654 struct drm_syncobj *syncobj;
655 struct file *file;
Dave Airliee9083422017-04-04 13:26:24 +1000656 int ret;
657
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000658 file = fget(fd);
659 if (!file)
Dave Airliee9083422017-04-04 13:26:24 +1000660 return -EINVAL;
661
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000662 if (file->f_op != &drm_syncobj_file_fops) {
663 fput(file);
664 return -EINVAL;
665 }
666
Dave Airliee9083422017-04-04 13:26:24 +1000667 /* take a reference to put in the idr */
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000668 syncobj = file->private_data;
Dave Airliee9083422017-04-04 13:26:24 +1000669 drm_syncobj_get(syncobj);
670
671 idr_preload(GFP_KERNEL);
672 spin_lock(&file_private->syncobj_table_lock);
673 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
674 spin_unlock(&file_private->syncobj_table_lock);
675 idr_preload_end();
676
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000677 if (ret > 0) {
678 *handle = ret;
679 ret = 0;
680 } else
681 drm_syncobj_put(syncobj);
682
683 fput(file);
684 return ret;
Dave Airliee9083422017-04-04 13:26:24 +1000685}
686
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300687static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
688 int fd, int handle)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100689{
690 struct dma_fence *fence = sync_file_get_fence(fd);
691 struct drm_syncobj *syncobj;
692
693 if (!fence)
694 return -EINVAL;
695
696 syncobj = drm_syncobj_find(file_private, handle);
697 if (!syncobj) {
698 dma_fence_put(fence);
699 return -ENOENT;
700 }
701
Chunming Zhou9a09a422018-08-30 14:48:30 +0800702 drm_syncobj_replace_fence(syncobj, 0, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100703 dma_fence_put(fence);
704 drm_syncobj_put(syncobj);
705 return 0;
706}
707
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300708static int drm_syncobj_export_sync_file(struct drm_file *file_private,
709 int handle, int *p_fd)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100710{
711 int ret;
712 struct dma_fence *fence;
713 struct sync_file *sync_file;
714 int fd = get_unused_fd_flags(O_CLOEXEC);
715
716 if (fd < 0)
717 return fd;
718
Chunming Zhou649fdce2018-10-15 16:55:47 +0800719 ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100720 if (ret)
721 goto err_put_fd;
722
723 sync_file = sync_file_create(fence);
724
725 dma_fence_put(fence);
726
727 if (!sync_file) {
728 ret = -EINVAL;
729 goto err_put_fd;
730 }
731
732 fd_install(fd, sync_file->file);
733
734 *p_fd = fd;
735 return 0;
736err_put_fd:
737 put_unused_fd(fd);
738 return ret;
739}
Dave Airliee9083422017-04-04 13:26:24 +1000740/**
741 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000742 * @file_private: drm file-private structure to set up
743 *
744 * Called at device open time, sets up the structure for handling refcounting
745 * of sync objects.
746 */
747void
748drm_syncobj_open(struct drm_file *file_private)
749{
Chris Wilsone86584c2018-02-12 14:55:33 +0000750 idr_init_base(&file_private->syncobj_idr, 1);
Dave Airliee9083422017-04-04 13:26:24 +1000751 spin_lock_init(&file_private->syncobj_table_lock);
752}
753
754static int
755drm_syncobj_release_handle(int id, void *ptr, void *data)
756{
757 struct drm_syncobj *syncobj = ptr;
758
759 drm_syncobj_put(syncobj);
760 return 0;
761}
762
763/**
764 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000765 * @file_private: drm file-private structure to clean up
766 *
767 * Called at close time when the filp is going away.
768 *
769 * Releases any remaining references on objects by this filp.
770 */
771void
772drm_syncobj_release(struct drm_file *file_private)
773{
774 idr_for_each(&file_private->syncobj_idr,
775 &drm_syncobj_release_handle, file_private);
776 idr_destroy(&file_private->syncobj_idr);
777}
778
779int
780drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
781 struct drm_file *file_private)
782{
783 struct drm_syncobj_create *args = data;
784
785 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100786 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000787
788 /* no valid flags yet */
Chunming Zhou48197bc2018-10-18 14:18:36 +0800789 if (args->flags & ~(DRM_SYNCOBJ_CREATE_SIGNALED |
790 DRM_SYNCOBJ_CREATE_TYPE_TIMELINE))
Dave Airliee9083422017-04-04 13:26:24 +1000791 return -EINVAL;
792
Marek Olšák1321fd22017-09-12 22:42:12 +0200793 return drm_syncobj_create_as_handle(file_private,
794 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000795}
796
797int
798drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
799 struct drm_file *file_private)
800{
801 struct drm_syncobj_destroy *args = data;
802
803 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100804 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000805
806 /* make sure padding is empty */
807 if (args->pad)
808 return -EINVAL;
809 return drm_syncobj_destroy(file_private, args->handle);
810}
811
812int
813drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
814 struct drm_file *file_private)
815{
816 struct drm_syncobj_handle *args = data;
817
818 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100819 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000820
Dave Airlie3ee45a32017-04-26 04:09:02 +0100821 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000822 return -EINVAL;
823
Dave Airlie3ee45a32017-04-26 04:09:02 +0100824 if (args->flags != 0 &&
825 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
826 return -EINVAL;
827
828 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
829 return drm_syncobj_export_sync_file(file_private, args->handle,
830 &args->fd);
831
Dave Airliee9083422017-04-04 13:26:24 +1000832 return drm_syncobj_handle_to_fd(file_private, args->handle,
833 &args->fd);
834}
835
836int
837drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
838 struct drm_file *file_private)
839{
840 struct drm_syncobj_handle *args = data;
841
842 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100843 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000844
Dave Airlie3ee45a32017-04-26 04:09:02 +0100845 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000846 return -EINVAL;
847
Dave Airlie3ee45a32017-04-26 04:09:02 +0100848 if (args->flags != 0 &&
849 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
850 return -EINVAL;
851
852 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
853 return drm_syncobj_import_sync_file_fence(file_private,
854 args->fd,
855 args->handle);
856
Dave Airliee9083422017-04-04 13:26:24 +1000857 return drm_syncobj_fd_to_handle(file_private, args->fd,
858 &args->handle);
859}
Dave Airlie5e60a102017-08-25 10:52:22 -0700860
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700861struct syncobj_wait_entry {
862 struct task_struct *task;
863 struct dma_fence *fence;
864 struct dma_fence_cb fence_cb;
865 struct drm_syncobj_cb syncobj_cb;
866};
867
868static void syncobj_wait_fence_func(struct dma_fence *fence,
869 struct dma_fence_cb *cb)
870{
871 struct syncobj_wait_entry *wait =
872 container_of(cb, struct syncobj_wait_entry, fence_cb);
873
874 wake_up_process(wait->task);
875}
876
877static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
878 struct drm_syncobj_cb *cb)
879{
880 struct syncobj_wait_entry *wait =
881 container_of(cb, struct syncobj_wait_entry, syncobj_cb);
882
Chunming Zhou48197bc2018-10-18 14:18:36 +0800883 drm_syncobj_search_fence(syncobj, 0, 0, &wait->fence);
884
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700885 wake_up_process(wait->task);
886}
887
888static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
889 uint32_t count,
890 uint32_t flags,
891 signed long timeout,
892 uint32_t *idx)
893{
894 struct syncobj_wait_entry *entries;
895 struct dma_fence *fence;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700896 uint32_t signaled_count, i;
897
898 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
899 if (!entries)
900 return -ENOMEM;
901
902 /* Walk the list of sync objects and initialize entries. We do
903 * this up-front so that we can properly return -EINVAL if there is
904 * a syncobj with a missing fence and then never have the chance of
905 * returning -EINVAL again.
906 */
907 signaled_count = 0;
908 for (i = 0; i < count; ++i) {
909 entries[i].task = current;
Chunming Zhou48197bc2018-10-18 14:18:36 +0800910 drm_syncobj_search_fence(syncobjs[i], 0, 0,
911 &entries[i].fence);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700912 if (!entries[i].fence) {
913 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
914 continue;
915 } else {
Chris Wilson12fec622018-09-20 21:05:30 +0100916 timeout = -EINVAL;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700917 goto cleanup_entries;
918 }
919 }
920
921 if (dma_fence_is_signaled(entries[i].fence)) {
922 if (signaled_count == 0 && idx)
923 *idx = i;
924 signaled_count++;
925 }
926 }
927
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700928 if (signaled_count == count ||
929 (signaled_count > 0 &&
930 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
931 goto cleanup_entries;
932
933 /* There's a very annoying laxness in the dma_fence API here, in
934 * that backends are not required to automatically report when a
935 * fence is signaled prior to fence->ops->enable_signaling() being
936 * called. So here if we fail to match signaled_count, we need to
937 * fallthough and try a 0 timeout wait!
938 */
939
940 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
941 for (i = 0; i < count; ++i) {
Jason Ekstrand337fe9f2018-09-26 02:17:03 -0500942 if (entries[i].fence)
943 continue;
944
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700945 drm_syncobj_fence_get_or_add_callback(syncobjs[i],
946 &entries[i].fence,
947 &entries[i].syncobj_cb,
948 syncobj_wait_syncobj_func);
949 }
950 }
951
952 do {
953 set_current_state(TASK_INTERRUPTIBLE);
954
955 signaled_count = 0;
956 for (i = 0; i < count; ++i) {
957 fence = entries[i].fence;
958 if (!fence)
959 continue;
960
961 if (dma_fence_is_signaled(fence) ||
962 (!entries[i].fence_cb.func &&
963 dma_fence_add_callback(fence,
964 &entries[i].fence_cb,
965 syncobj_wait_fence_func))) {
966 /* The fence has been signaled */
967 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
968 signaled_count++;
969 } else {
970 if (idx)
971 *idx = i;
972 goto done_waiting;
973 }
974 }
975 }
976
977 if (signaled_count == count)
978 goto done_waiting;
979
980 if (timeout == 0) {
Chris Wilson12fec622018-09-20 21:05:30 +0100981 timeout = -ETIME;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700982 goto done_waiting;
983 }
984
Chris Wilson12fec622018-09-20 21:05:30 +0100985 if (signal_pending(current)) {
986 timeout = -ERESTARTSYS;
987 goto done_waiting;
988 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700989
Chris Wilson12fec622018-09-20 21:05:30 +0100990 timeout = schedule_timeout(timeout);
991 } while (1);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700992
993done_waiting:
994 __set_current_state(TASK_RUNNING);
995
996cleanup_entries:
997 for (i = 0; i < count; ++i) {
998 if (entries[i].syncobj_cb.func)
999 drm_syncobj_remove_callback(syncobjs[i],
1000 &entries[i].syncobj_cb);
1001 if (entries[i].fence_cb.func)
1002 dma_fence_remove_callback(entries[i].fence,
1003 &entries[i].fence_cb);
1004 dma_fence_put(entries[i].fence);
1005 }
1006 kfree(entries);
1007
Chris Wilson12fec622018-09-20 21:05:30 +01001008 return timeout;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001009}
1010
Dave Airlie5e60a102017-08-25 10:52:22 -07001011/**
1012 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
1013 *
1014 * @timeout_nsec: timeout nsec component in ns, 0 for poll
1015 *
1016 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
1017 */
1018static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
1019{
1020 ktime_t abs_timeout, now;
1021 u64 timeout_ns, timeout_jiffies64;
1022
1023 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
1024 if (timeout_nsec == 0)
1025 return 0;
1026
1027 abs_timeout = ns_to_ktime(timeout_nsec);
1028 now = ktime_get();
1029
1030 if (!ktime_after(abs_timeout, now))
1031 return 0;
1032
1033 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
1034
1035 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
1036 /* clamp timeout to avoid infinite timeout */
1037 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
1038 return MAX_SCHEDULE_TIMEOUT - 1;
1039
1040 return timeout_jiffies64 + 1;
1041}
1042
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001043static int drm_syncobj_array_wait(struct drm_device *dev,
1044 struct drm_file *file_private,
1045 struct drm_syncobj_wait *wait,
1046 struct drm_syncobj **syncobjs)
Dave Airlie5e60a102017-08-25 10:52:22 -07001047{
1048 signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
Dave Airlie5e60a102017-08-25 10:52:22 -07001049 uint32_t first = ~0;
1050
Chris Wilson12fec622018-09-20 21:05:30 +01001051 timeout = drm_syncobj_array_wait_timeout(syncobjs,
1052 wait->count_handles,
1053 wait->flags,
1054 timeout, &first);
1055 if (timeout < 0)
1056 return timeout;
Dave Airlie5e60a102017-08-25 10:52:22 -07001057
1058 wait->first_signaled = first;
Dave Airlie5e60a102017-08-25 10:52:22 -07001059 return 0;
1060}
1061
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001062static int drm_syncobj_array_find(struct drm_file *file_private,
Ville Syrjälä9e554462017-09-01 19:53:26 +03001063 void __user *user_handles,
1064 uint32_t count_handles,
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001065 struct drm_syncobj ***syncobjs_out)
1066{
1067 uint32_t i, *handles;
1068 struct drm_syncobj **syncobjs;
1069 int ret;
1070
1071 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1072 if (handles == NULL)
1073 return -ENOMEM;
1074
1075 if (copy_from_user(handles, user_handles,
1076 sizeof(uint32_t) * count_handles)) {
1077 ret = -EFAULT;
1078 goto err_free_handles;
1079 }
1080
1081 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1082 if (syncobjs == NULL) {
1083 ret = -ENOMEM;
1084 goto err_free_handles;
1085 }
1086
1087 for (i = 0; i < count_handles; i++) {
1088 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1089 if (!syncobjs[i]) {
1090 ret = -ENOENT;
1091 goto err_put_syncobjs;
1092 }
1093 }
1094
1095 kfree(handles);
1096 *syncobjs_out = syncobjs;
1097 return 0;
1098
1099err_put_syncobjs:
1100 while (i-- > 0)
1101 drm_syncobj_put(syncobjs[i]);
1102 kfree(syncobjs);
1103err_free_handles:
1104 kfree(handles);
1105
1106 return ret;
1107}
1108
1109static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1110 uint32_t count)
1111{
1112 uint32_t i;
1113 for (i = 0; i < count; i++)
1114 drm_syncobj_put(syncobjs[i]);
1115 kfree(syncobjs);
1116}
1117
Dave Airlie5e60a102017-08-25 10:52:22 -07001118int
1119drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1120 struct drm_file *file_private)
1121{
1122 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001123 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -07001124 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -07001125
1126 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001127 return -EOPNOTSUPP;
Dave Airlie5e60a102017-08-25 10:52:22 -07001128
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001129 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1130 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -07001131 return -EINVAL;
1132
1133 if (args->count_handles == 0)
1134 return -EINVAL;
1135
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001136 ret = drm_syncobj_array_find(file_private,
1137 u64_to_user_ptr(args->handles),
1138 args->count_handles,
1139 &syncobjs);
1140 if (ret < 0)
1141 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -07001142
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001143 ret = drm_syncobj_array_wait(dev, file_private,
1144 args, syncobjs);
Dave Airlie5e60a102017-08-25 10:52:22 -07001145
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001146 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -07001147
1148 return ret;
1149}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001150
1151int
1152drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1153 struct drm_file *file_private)
1154{
1155 struct drm_syncobj_array *args = data;
1156 struct drm_syncobj **syncobjs;
1157 uint32_t i;
1158 int ret;
1159
1160 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001161 return -EOPNOTSUPP;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001162
1163 if (args->pad != 0)
1164 return -EINVAL;
1165
1166 if (args->count_handles == 0)
1167 return -EINVAL;
1168
1169 ret = drm_syncobj_array_find(file_private,
1170 u64_to_user_ptr(args->handles),
1171 args->count_handles,
1172 &syncobjs);
1173 if (ret < 0)
1174 return ret;
1175
Chunming Zhou48197bc2018-10-18 14:18:36 +08001176 for (i = 0; i < args->count_handles; i++) {
1177 drm_syncobj_fini(syncobjs[i]);
1178 drm_syncobj_init(syncobjs[i]);
1179 }
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001180 drm_syncobj_array_free(syncobjs, args->count_handles);
1181
Chunming Zhou48197bc2018-10-18 14:18:36 +08001182 return ret;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001183}
Jason Ekstrandffa94432017-08-28 14:10:28 -07001184
1185int
1186drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1187 struct drm_file *file_private)
1188{
1189 struct drm_syncobj_array *args = data;
1190 struct drm_syncobj **syncobjs;
1191 uint32_t i;
1192 int ret;
1193
1194 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001195 return -EOPNOTSUPP;
Jason Ekstrandffa94432017-08-28 14:10:28 -07001196
1197 if (args->pad != 0)
1198 return -EINVAL;
1199
1200 if (args->count_handles == 0)
1201 return -EINVAL;
1202
1203 ret = drm_syncobj_array_find(file_private,
1204 u64_to_user_ptr(args->handles),
1205 args->count_handles,
1206 &syncobjs);
1207 if (ret < 0)
1208 return ret;
1209
1210 for (i = 0; i < args->count_handles; i++) {
1211 ret = drm_syncobj_assign_null_handle(syncobjs[i]);
1212 if (ret < 0)
1213 break;
1214 }
1215
1216 drm_syncobj_array_free(syncobjs, args->count_handles);
1217
1218 return ret;
1219}