blob: 4dca5f7e8c4bd3317e06de72bb2f43ea3c97d244 [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
Chris Wilson9cbe67c2018-10-31 12:07:10 +0000126static struct dma_fence *
127drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj,
128 uint64_t point)
Chunming Zhou48197bc2018-10-18 14:18:36 +0800129{
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
Chris Wilson9cbe67c2018-10-31 12:07:10 +0000181static void drm_syncobj_remove_callback(struct drm_syncobj *syncobj,
182 struct drm_syncobj_cb *cb)
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800183{
184 mutex_lock(&syncobj->cb_mutex);
185 list_del_init(&cb->node);
186 mutex_unlock(&syncobj->cb_mutex);
187}
188
189static void drm_syncobj_init(struct drm_syncobj *syncobj)
190{
191 spin_lock(&syncobj->pt_lock);
192 syncobj->timeline_context = dma_fence_context_alloc(1);
193 syncobj->timeline = 0;
194 syncobj->signal_point = 0;
195 init_waitqueue_head(&syncobj->wq);
196
197 INIT_LIST_HEAD(&syncobj->signal_pt_list);
198 spin_unlock(&syncobj->pt_lock);
199}
200
201static void drm_syncobj_fini(struct drm_syncobj *syncobj)
202{
203 struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp;
204
205 spin_lock(&syncobj->pt_lock);
206 list_for_each_entry_safe(signal_pt, tmp,
207 &syncobj->signal_pt_list, list) {
208 list_del(&signal_pt->list);
209 dma_fence_put(&signal_pt->fence_array->base);
210 kfree(signal_pt);
211 }
212 spin_unlock(&syncobj->pt_lock);
213}
214
Chunming Zhou48197bc2018-10-18 14:18:36 +0800215static int drm_syncobj_create_signal_pt(struct drm_syncobj *syncobj,
216 struct dma_fence *fence,
217 u64 point)
218{
219 struct drm_syncobj_signal_pt *signal_pt =
220 kzalloc(sizeof(struct drm_syncobj_signal_pt), GFP_KERNEL);
221 struct drm_syncobj_signal_pt *tail_pt;
222 struct dma_fence **fences;
223 int num_fences = 0;
224 int ret = 0, i;
225
226 if (!signal_pt)
227 return -ENOMEM;
228 if (!fence)
229 goto out;
230
231 fences = kmalloc_array(sizeof(void *), 2, GFP_KERNEL);
232 if (!fences) {
233 ret = -ENOMEM;
234 goto out;
235 }
236 fences[num_fences++] = dma_fence_get(fence);
237 /* timeline syncobj must take this dependency */
238 if (syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) {
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800239 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800240 if (!list_empty(&syncobj->signal_pt_list)) {
241 tail_pt = list_last_entry(&syncobj->signal_pt_list,
242 struct drm_syncobj_signal_pt, list);
243 fences[num_fences++] =
244 dma_fence_get(&tail_pt->fence_array->base);
245 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800246 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800247 }
248 signal_pt->fence_array = dma_fence_array_create(num_fences, fences,
249 syncobj->timeline_context,
250 point, false);
251 if (!signal_pt->fence_array) {
252 ret = -ENOMEM;
253 goto fail;
254 }
255
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800256 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800257 if (syncobj->signal_point >= point) {
258 DRM_WARN("A later signal is ready!");
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800259 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800260 goto exist;
261 }
262 signal_pt->value = point;
263 list_add_tail(&signal_pt->list, &syncobj->signal_pt_list);
264 syncobj->signal_point = point;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800265 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800266 wake_up_all(&syncobj->wq);
267
268 return 0;
269exist:
270 dma_fence_put(&signal_pt->fence_array->base);
271fail:
272 for (i = 0; i < num_fences; i++)
273 dma_fence_put(fences[i]);
274 kfree(fences);
275out:
276 kfree(signal_pt);
277 return ret;
278}
279
280static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj)
281{
282 struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt;
283
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800284 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800285 tail_pt = list_last_entry(&syncobj->signal_pt_list,
286 struct drm_syncobj_signal_pt,
287 list);
288 list_for_each_entry_safe(signal_pt, tmp,
289 &syncobj->signal_pt_list, list) {
290 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY &&
291 signal_pt == tail_pt)
292 continue;
293 if (dma_fence_is_signaled(&signal_pt->fence_array->base)) {
294 syncobj->timeline = signal_pt->value;
295 list_del(&signal_pt->list);
296 dma_fence_put(&signal_pt->fence_array->base);
297 kfree(signal_pt);
298 } else {
299 /*signal_pt is in order in list, from small to big, so
300 * the later must not be signal either */
301 break;
302 }
303 }
304
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800305 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800306}
Dave Airliee9083422017-04-04 13:26:24 +1000307/**
308 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000309 * @syncobj: Sync object to replace fence in
Chunming Zhou9a09a422018-08-30 14:48:30 +0800310 * @point: timeline point
Dave Airliee9083422017-04-04 13:26:24 +1000311 * @fence: fence to install in sync file.
312 *
Chunming Zhou9a09a422018-08-30 14:48:30 +0800313 * This replaces the fence on a sync object, or a timeline point fence.
Dave Airliee9083422017-04-04 13:26:24 +1000314 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100315void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Chunming Zhou9a09a422018-08-30 14:48:30 +0800316 u64 point,
Dave Airliee9083422017-04-04 13:26:24 +1000317 struct dma_fence *fence)
318{
Chunming Zhou48197bc2018-10-18 14:18:36 +0800319 u64 pt_value = point;
Dave Airliee9083422017-04-04 13:26:24 +1000320
Chunming Zhou48197bc2018-10-18 14:18:36 +0800321 drm_syncobj_garbage_collection(syncobj);
322 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
323 if (!fence) {
324 drm_syncobj_fini(syncobj);
325 drm_syncobj_init(syncobj);
326 return;
327 }
328 pt_value = syncobj->signal_point +
329 DRM_SYNCOBJ_BINARY_POINT;
330 }
331 drm_syncobj_create_signal_pt(syncobj, fence, pt_value);
332 if (fence) {
333 struct drm_syncobj_cb *cur, *tmp;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800334 LIST_HEAD(cb_list);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700335
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800336 mutex_lock(&syncobj->cb_mutex);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700337 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) {
338 list_del_init(&cur->node);
339 cur->func(syncobj, cur);
340 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800341 mutex_unlock(&syncobj->cb_mutex);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700342 }
Dave Airliee9083422017-04-04 13:26:24 +1000343}
344EXPORT_SYMBOL(drm_syncobj_replace_fence);
345
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700346static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
347{
Chunming Zhoue28bd102018-08-30 14:48:28 +0800348 struct drm_syncobj_stub_fence *fence;
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700349 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
350 if (fence == NULL)
351 return -ENOMEM;
352
353 spin_lock_init(&fence->lock);
Chunming Zhoue28bd102018-08-30 14:48:28 +0800354 dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops,
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700355 &fence->lock, 0, 0);
356 dma_fence_signal(&fence->base);
357
Chunming Zhou9a09a422018-08-30 14:48:30 +0800358 drm_syncobj_replace_fence(syncobj, 0, &fence->base);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700359
360 dma_fence_put(&fence->base);
361
362 return 0;
363}
364
Chunming Zhou48197bc2018-10-18 14:18:36 +0800365static int
366drm_syncobj_point_get(struct drm_syncobj *syncobj, u64 point, u64 flags,
367 struct dma_fence **fence)
368{
369 int ret = 0;
370
371 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
372 ret = wait_event_interruptible(syncobj->wq,
373 point <= syncobj->signal_point);
374 if (ret < 0)
375 return ret;
376 }
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800377 spin_lock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800378 *fence = drm_syncobj_find_signal_pt_for_point(syncobj, point);
379 if (!*fence)
380 ret = -EINVAL;
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800381 spin_unlock(&syncobj->pt_lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800382 return ret;
383}
384
385/**
386 * drm_syncobj_search_fence - lookup and reference the fence in a sync object or
387 * in a timeline point
388 * @syncobj: sync object pointer
389 * @point: timeline point
390 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
391 * @fence: out parameter for the fence
392 *
393 * if flags is DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, the function will block
394 * here until specific timeline points is reached.
395 * if not, you need a submit thread and block in userspace until all future
396 * timeline points have materialized, only then you can submit to the kernel,
397 * otherwise, function will fail to return fence.
398 *
399 * Returns 0 on success or a negative error value on failure. On success @fence
400 * contains a reference to the fence, which must be released by calling
401 * dma_fence_put().
402 */
403int drm_syncobj_search_fence(struct drm_syncobj *syncobj, u64 point,
404 u64 flags, struct dma_fence **fence)
405{
406 u64 pt_value = point;
407
408 if (!syncobj)
409 return -ENOENT;
410
411 drm_syncobj_garbage_collection(syncobj);
412 if (syncobj->type == DRM_SYNCOBJ_TYPE_BINARY) {
413 /*BINARY syncobj always wait on last pt */
414 pt_value = syncobj->signal_point;
415
416 if (pt_value == 0)
417 pt_value += DRM_SYNCOBJ_BINARY_POINT;
418 }
419 return drm_syncobj_point_get(syncobj, pt_value, flags, fence);
420}
421EXPORT_SYMBOL(drm_syncobj_search_fence);
422
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100423/**
424 * drm_syncobj_find_fence - lookup and reference the fence in a sync object
425 * @file_private: drm file private pointer
426 * @handle: sync object handle to lookup.
Chunming Zhou0a6730e2018-08-30 14:48:29 +0800427 * @point: timeline point
Chunming Zhou871edc92018-10-17 15:03:18 +0800428 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100429 * @fence: out parameter for the fence
430 *
431 * This is just a convenience function that combines drm_syncobj_find() and
Chunming Zhou48197bc2018-10-18 14:18:36 +0800432 * drm_syncobj_lookup_fence().
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100433 *
434 * Returns 0 on success or a negative error value on failure. On success @fence
435 * contains a reference to the fence, which must be released by calling
436 * dma_fence_put().
437 */
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700438int drm_syncobj_find_fence(struct drm_file *file_private,
Chunming Zhou649fdce2018-10-15 16:55:47 +0800439 u32 handle, u64 point, u64 flags,
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700440 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000441{
442 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800443 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000444
Chunming Zhou48197bc2018-10-18 14:18:36 +0800445 ret = drm_syncobj_search_fence(syncobj, point, flags, fence);
Dave Airliee9083422017-04-04 13:26:24 +1000446 drm_syncobj_put(syncobj);
447 return ret;
448}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700449EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000450
451/**
452 * drm_syncobj_free - free a sync object.
453 * @kref: kref to free.
454 *
455 * Only to be called from kref_put in drm_syncobj_put.
456 */
457void drm_syncobj_free(struct kref *kref)
458{
459 struct drm_syncobj *syncobj = container_of(kref,
460 struct drm_syncobj,
461 refcount);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800462 drm_syncobj_fini(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000463 kfree(syncobj);
464}
465EXPORT_SYMBOL(drm_syncobj_free);
466
Marek Olšák1321fd22017-09-12 22:42:12 +0200467/**
468 * drm_syncobj_create - create a new syncobj
469 * @out_syncobj: returned syncobj
470 * @flags: DRM_SYNCOBJ_* flags
471 * @fence: if non-NULL, the syncobj will represent this fence
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100472 *
473 * This is the first function to create a sync object. After creating, drivers
474 * probably want to make it available to userspace, either through
475 * drm_syncobj_get_handle() or drm_syncobj_get_fd().
476 *
477 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200478 */
479int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
480 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000481{
482 int ret;
483 struct drm_syncobj *syncobj;
484
485 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
486 if (!syncobj)
487 return -ENOMEM;
488
489 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700490 INIT_LIST_HEAD(&syncobj->cb_list);
Chunming Zhou43cf1fc2018-10-23 17:37:45 +0800491 spin_lock_init(&syncobj->pt_lock);
492 mutex_init(&syncobj->cb_mutex);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800493 if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)
494 syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE;
495 else
496 syncobj->type = DRM_SYNCOBJ_TYPE_BINARY;
497 drm_syncobj_init(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000498
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700499 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) {
500 ret = drm_syncobj_assign_null_handle(syncobj);
501 if (ret < 0) {
502 drm_syncobj_put(syncobj);
503 return ret;
504 }
505 }
506
Marek Olšák1321fd22017-09-12 22:42:12 +0200507 if (fence)
Chunming Zhou9a09a422018-08-30 14:48:30 +0800508 drm_syncobj_replace_fence(syncobj, 0, fence);
Marek Olšák1321fd22017-09-12 22:42:12 +0200509
510 *out_syncobj = syncobj;
511 return 0;
512}
513EXPORT_SYMBOL(drm_syncobj_create);
514
515/**
516 * drm_syncobj_get_handle - get a handle from a syncobj
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100517 * @file_private: drm file private pointer
518 * @syncobj: Sync object to export
519 * @handle: out parameter with the new handle
520 *
521 * Exports a sync object created with drm_syncobj_create() as a handle on
522 * @file_private to userspace.
523 *
524 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200525 */
526int drm_syncobj_get_handle(struct drm_file *file_private,
527 struct drm_syncobj *syncobj, u32 *handle)
528{
529 int ret;
530
531 /* take a reference to put in the idr */
532 drm_syncobj_get(syncobj);
533
Dave Airliee9083422017-04-04 13:26:24 +1000534 idr_preload(GFP_KERNEL);
535 spin_lock(&file_private->syncobj_table_lock);
536 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
537 spin_unlock(&file_private->syncobj_table_lock);
538
539 idr_preload_end();
540
541 if (ret < 0) {
542 drm_syncobj_put(syncobj);
543 return ret;
544 }
545
546 *handle = ret;
547 return 0;
548}
Marek Olšák1321fd22017-09-12 22:42:12 +0200549EXPORT_SYMBOL(drm_syncobj_get_handle);
550
551static int drm_syncobj_create_as_handle(struct drm_file *file_private,
552 u32 *handle, uint32_t flags)
553{
554 int ret;
555 struct drm_syncobj *syncobj;
556
557 ret = drm_syncobj_create(&syncobj, flags, NULL);
558 if (ret)
559 return ret;
560
561 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
562 drm_syncobj_put(syncobj);
563 return ret;
564}
Dave Airliee9083422017-04-04 13:26:24 +1000565
566static int drm_syncobj_destroy(struct drm_file *file_private,
567 u32 handle)
568{
569 struct drm_syncobj *syncobj;
570
571 spin_lock(&file_private->syncobj_table_lock);
572 syncobj = idr_remove(&file_private->syncobj_idr, handle);
573 spin_unlock(&file_private->syncobj_table_lock);
574
575 if (!syncobj)
576 return -EINVAL;
577
578 drm_syncobj_put(syncobj);
579 return 0;
580}
581
582static int drm_syncobj_file_release(struct inode *inode, struct file *file)
583{
584 struct drm_syncobj *syncobj = file->private_data;
585
586 drm_syncobj_put(syncobj);
587 return 0;
588}
589
590static const struct file_operations drm_syncobj_file_fops = {
591 .release = drm_syncobj_file_release,
592};
593
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100594/**
595 * drm_syncobj_get_fd - get a file descriptor from a syncobj
596 * @syncobj: Sync object to export
597 * @p_fd: out parameter with the new file descriptor
598 *
599 * Exports a sync object created with drm_syncobj_create() as a file descriptor.
600 *
601 * Returns 0 on success or a negative error value on failure.
602 */
Marek Olšák684fd0a2017-09-12 22:42:13 +0200603int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
604{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000605 struct file *file;
Marek Olšák684fd0a2017-09-12 22:42:13 +0200606 int fd;
607
608 fd = get_unused_fd_flags(O_CLOEXEC);
609 if (fd < 0)
610 return fd;
611
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000612 file = anon_inode_getfile("syncobj_file",
613 &drm_syncobj_file_fops,
614 syncobj, 0);
615 if (IS_ERR(file)) {
616 put_unused_fd(fd);
617 return PTR_ERR(file);
Marek Olšák684fd0a2017-09-12 22:42:13 +0200618 }
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000619
620 drm_syncobj_get(syncobj);
621 fd_install(fd, file);
622
Marek Olšák684fd0a2017-09-12 22:42:13 +0200623 *p_fd = fd;
624 return 0;
625}
626EXPORT_SYMBOL(drm_syncobj_get_fd);
627
Dave Airliee9083422017-04-04 13:26:24 +1000628static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
629 u32 handle, int *p_fd)
630{
631 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
632 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000633
634 if (!syncobj)
635 return -EINVAL;
636
Marek Olšák684fd0a2017-09-12 22:42:13 +0200637 ret = drm_syncobj_get_fd(syncobj, p_fd);
Dave Airliee9083422017-04-04 13:26:24 +1000638 drm_syncobj_put(syncobj);
639 return ret;
640}
641
Dave Airliee9083422017-04-04 13:26:24 +1000642static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
643 int fd, u32 *handle)
644{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000645 struct drm_syncobj *syncobj;
646 struct file *file;
Dave Airliee9083422017-04-04 13:26:24 +1000647 int ret;
648
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000649 file = fget(fd);
650 if (!file)
Dave Airliee9083422017-04-04 13:26:24 +1000651 return -EINVAL;
652
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000653 if (file->f_op != &drm_syncobj_file_fops) {
654 fput(file);
655 return -EINVAL;
656 }
657
Dave Airliee9083422017-04-04 13:26:24 +1000658 /* take a reference to put in the idr */
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000659 syncobj = file->private_data;
Dave Airliee9083422017-04-04 13:26:24 +1000660 drm_syncobj_get(syncobj);
661
662 idr_preload(GFP_KERNEL);
663 spin_lock(&file_private->syncobj_table_lock);
664 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
665 spin_unlock(&file_private->syncobj_table_lock);
666 idr_preload_end();
667
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000668 if (ret > 0) {
669 *handle = ret;
670 ret = 0;
671 } else
672 drm_syncobj_put(syncobj);
673
674 fput(file);
675 return ret;
Dave Airliee9083422017-04-04 13:26:24 +1000676}
677
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300678static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
679 int fd, int handle)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100680{
681 struct dma_fence *fence = sync_file_get_fence(fd);
682 struct drm_syncobj *syncobj;
683
684 if (!fence)
685 return -EINVAL;
686
687 syncobj = drm_syncobj_find(file_private, handle);
688 if (!syncobj) {
689 dma_fence_put(fence);
690 return -ENOENT;
691 }
692
Chunming Zhou9a09a422018-08-30 14:48:30 +0800693 drm_syncobj_replace_fence(syncobj, 0, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100694 dma_fence_put(fence);
695 drm_syncobj_put(syncobj);
696 return 0;
697}
698
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300699static int drm_syncobj_export_sync_file(struct drm_file *file_private,
700 int handle, int *p_fd)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100701{
702 int ret;
703 struct dma_fence *fence;
704 struct sync_file *sync_file;
705 int fd = get_unused_fd_flags(O_CLOEXEC);
706
707 if (fd < 0)
708 return fd;
709
Chunming Zhou649fdce2018-10-15 16:55:47 +0800710 ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100711 if (ret)
712 goto err_put_fd;
713
714 sync_file = sync_file_create(fence);
715
716 dma_fence_put(fence);
717
718 if (!sync_file) {
719 ret = -EINVAL;
720 goto err_put_fd;
721 }
722
723 fd_install(fd, sync_file->file);
724
725 *p_fd = fd;
726 return 0;
727err_put_fd:
728 put_unused_fd(fd);
729 return ret;
730}
Dave Airliee9083422017-04-04 13:26:24 +1000731/**
732 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000733 * @file_private: drm file-private structure to set up
734 *
735 * Called at device open time, sets up the structure for handling refcounting
736 * of sync objects.
737 */
738void
739drm_syncobj_open(struct drm_file *file_private)
740{
Chris Wilsone86584c2018-02-12 14:55:33 +0000741 idr_init_base(&file_private->syncobj_idr, 1);
Dave Airliee9083422017-04-04 13:26:24 +1000742 spin_lock_init(&file_private->syncobj_table_lock);
743}
744
745static int
746drm_syncobj_release_handle(int id, void *ptr, void *data)
747{
748 struct drm_syncobj *syncobj = ptr;
749
750 drm_syncobj_put(syncobj);
751 return 0;
752}
753
754/**
755 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000756 * @file_private: drm file-private structure to clean up
757 *
758 * Called at close time when the filp is going away.
759 *
760 * Releases any remaining references on objects by this filp.
761 */
762void
763drm_syncobj_release(struct drm_file *file_private)
764{
765 idr_for_each(&file_private->syncobj_idr,
766 &drm_syncobj_release_handle, file_private);
767 idr_destroy(&file_private->syncobj_idr);
768}
769
770int
771drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
772 struct drm_file *file_private)
773{
774 struct drm_syncobj_create *args = data;
775
776 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100777 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000778
779 /* no valid flags yet */
Chunming Zhou48197bc2018-10-18 14:18:36 +0800780 if (args->flags & ~(DRM_SYNCOBJ_CREATE_SIGNALED |
781 DRM_SYNCOBJ_CREATE_TYPE_TIMELINE))
Dave Airliee9083422017-04-04 13:26:24 +1000782 return -EINVAL;
783
Marek Olšák1321fd22017-09-12 22:42:12 +0200784 return drm_syncobj_create_as_handle(file_private,
785 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000786}
787
788int
789drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
790 struct drm_file *file_private)
791{
792 struct drm_syncobj_destroy *args = data;
793
794 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100795 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000796
797 /* make sure padding is empty */
798 if (args->pad)
799 return -EINVAL;
800 return drm_syncobj_destroy(file_private, args->handle);
801}
802
803int
804drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
805 struct drm_file *file_private)
806{
807 struct drm_syncobj_handle *args = data;
808
809 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100810 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000811
Dave Airlie3ee45a32017-04-26 04:09:02 +0100812 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000813 return -EINVAL;
814
Dave Airlie3ee45a32017-04-26 04:09:02 +0100815 if (args->flags != 0 &&
816 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
817 return -EINVAL;
818
819 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
820 return drm_syncobj_export_sync_file(file_private, args->handle,
821 &args->fd);
822
Dave Airliee9083422017-04-04 13:26:24 +1000823 return drm_syncobj_handle_to_fd(file_private, args->handle,
824 &args->fd);
825}
826
827int
828drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
829 struct drm_file *file_private)
830{
831 struct drm_syncobj_handle *args = data;
832
833 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100834 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000835
Dave Airlie3ee45a32017-04-26 04:09:02 +0100836 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000837 return -EINVAL;
838
Dave Airlie3ee45a32017-04-26 04:09:02 +0100839 if (args->flags != 0 &&
840 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
841 return -EINVAL;
842
843 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
844 return drm_syncobj_import_sync_file_fence(file_private,
845 args->fd,
846 args->handle);
847
Dave Airliee9083422017-04-04 13:26:24 +1000848 return drm_syncobj_fd_to_handle(file_private, args->fd,
849 &args->handle);
850}
Dave Airlie5e60a102017-08-25 10:52:22 -0700851
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700852struct syncobj_wait_entry {
853 struct task_struct *task;
854 struct dma_fence *fence;
855 struct dma_fence_cb fence_cb;
856 struct drm_syncobj_cb syncobj_cb;
857};
858
859static void syncobj_wait_fence_func(struct dma_fence *fence,
860 struct dma_fence_cb *cb)
861{
862 struct syncobj_wait_entry *wait =
863 container_of(cb, struct syncobj_wait_entry, fence_cb);
864
865 wake_up_process(wait->task);
866}
867
868static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
869 struct drm_syncobj_cb *cb)
870{
871 struct syncobj_wait_entry *wait =
872 container_of(cb, struct syncobj_wait_entry, syncobj_cb);
873
Chunming Zhou48197bc2018-10-18 14:18:36 +0800874 drm_syncobj_search_fence(syncobj, 0, 0, &wait->fence);
875
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700876 wake_up_process(wait->task);
877}
878
879static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
880 uint32_t count,
881 uint32_t flags,
882 signed long timeout,
883 uint32_t *idx)
884{
885 struct syncobj_wait_entry *entries;
886 struct dma_fence *fence;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700887 uint32_t signaled_count, i;
888
889 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
890 if (!entries)
891 return -ENOMEM;
892
893 /* Walk the list of sync objects and initialize entries. We do
894 * this up-front so that we can properly return -EINVAL if there is
895 * a syncobj with a missing fence and then never have the chance of
896 * returning -EINVAL again.
897 */
898 signaled_count = 0;
899 for (i = 0; i < count; ++i) {
900 entries[i].task = current;
Chunming Zhou48197bc2018-10-18 14:18:36 +0800901 drm_syncobj_search_fence(syncobjs[i], 0, 0,
902 &entries[i].fence);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700903 if (!entries[i].fence) {
904 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
905 continue;
906 } else {
Chris Wilson12fec622018-09-20 21:05:30 +0100907 timeout = -EINVAL;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700908 goto cleanup_entries;
909 }
910 }
911
912 if (dma_fence_is_signaled(entries[i].fence)) {
913 if (signaled_count == 0 && idx)
914 *idx = i;
915 signaled_count++;
916 }
917 }
918
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700919 if (signaled_count == count ||
920 (signaled_count > 0 &&
921 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
922 goto cleanup_entries;
923
924 /* There's a very annoying laxness in the dma_fence API here, in
925 * that backends are not required to automatically report when a
926 * fence is signaled prior to fence->ops->enable_signaling() being
927 * called. So here if we fail to match signaled_count, we need to
928 * fallthough and try a 0 timeout wait!
929 */
930
931 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
932 for (i = 0; i < count; ++i) {
Jason Ekstrand337fe9f2018-09-26 02:17:03 -0500933 if (entries[i].fence)
934 continue;
935
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700936 drm_syncobj_fence_get_or_add_callback(syncobjs[i],
937 &entries[i].fence,
938 &entries[i].syncobj_cb,
939 syncobj_wait_syncobj_func);
940 }
941 }
942
943 do {
944 set_current_state(TASK_INTERRUPTIBLE);
945
946 signaled_count = 0;
947 for (i = 0; i < count; ++i) {
948 fence = entries[i].fence;
949 if (!fence)
950 continue;
951
952 if (dma_fence_is_signaled(fence) ||
953 (!entries[i].fence_cb.func &&
954 dma_fence_add_callback(fence,
955 &entries[i].fence_cb,
956 syncobj_wait_fence_func))) {
957 /* The fence has been signaled */
958 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
959 signaled_count++;
960 } else {
961 if (idx)
962 *idx = i;
963 goto done_waiting;
964 }
965 }
966 }
967
968 if (signaled_count == count)
969 goto done_waiting;
970
971 if (timeout == 0) {
Chris Wilson12fec622018-09-20 21:05:30 +0100972 timeout = -ETIME;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700973 goto done_waiting;
974 }
975
Chris Wilson12fec622018-09-20 21:05:30 +0100976 if (signal_pending(current)) {
977 timeout = -ERESTARTSYS;
978 goto done_waiting;
979 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700980
Chris Wilson12fec622018-09-20 21:05:30 +0100981 timeout = schedule_timeout(timeout);
982 } while (1);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700983
984done_waiting:
985 __set_current_state(TASK_RUNNING);
986
987cleanup_entries:
988 for (i = 0; i < count; ++i) {
989 if (entries[i].syncobj_cb.func)
990 drm_syncobj_remove_callback(syncobjs[i],
991 &entries[i].syncobj_cb);
992 if (entries[i].fence_cb.func)
993 dma_fence_remove_callback(entries[i].fence,
994 &entries[i].fence_cb);
995 dma_fence_put(entries[i].fence);
996 }
997 kfree(entries);
998
Chris Wilson12fec622018-09-20 21:05:30 +0100999 return timeout;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001000}
1001
Dave Airlie5e60a102017-08-25 10:52:22 -07001002/**
1003 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
1004 *
1005 * @timeout_nsec: timeout nsec component in ns, 0 for poll
1006 *
1007 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
1008 */
1009static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
1010{
1011 ktime_t abs_timeout, now;
1012 u64 timeout_ns, timeout_jiffies64;
1013
1014 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
1015 if (timeout_nsec == 0)
1016 return 0;
1017
1018 abs_timeout = ns_to_ktime(timeout_nsec);
1019 now = ktime_get();
1020
1021 if (!ktime_after(abs_timeout, now))
1022 return 0;
1023
1024 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
1025
1026 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
1027 /* clamp timeout to avoid infinite timeout */
1028 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
1029 return MAX_SCHEDULE_TIMEOUT - 1;
1030
1031 return timeout_jiffies64 + 1;
1032}
1033
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001034static int drm_syncobj_array_wait(struct drm_device *dev,
1035 struct drm_file *file_private,
1036 struct drm_syncobj_wait *wait,
1037 struct drm_syncobj **syncobjs)
Dave Airlie5e60a102017-08-25 10:52:22 -07001038{
1039 signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
Dave Airlie5e60a102017-08-25 10:52:22 -07001040 uint32_t first = ~0;
1041
Chris Wilson12fec622018-09-20 21:05:30 +01001042 timeout = drm_syncobj_array_wait_timeout(syncobjs,
1043 wait->count_handles,
1044 wait->flags,
1045 timeout, &first);
1046 if (timeout < 0)
1047 return timeout;
Dave Airlie5e60a102017-08-25 10:52:22 -07001048
1049 wait->first_signaled = first;
Dave Airlie5e60a102017-08-25 10:52:22 -07001050 return 0;
1051}
1052
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001053static int drm_syncobj_array_find(struct drm_file *file_private,
Ville Syrjälä9e554462017-09-01 19:53:26 +03001054 void __user *user_handles,
1055 uint32_t count_handles,
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001056 struct drm_syncobj ***syncobjs_out)
1057{
1058 uint32_t i, *handles;
1059 struct drm_syncobj **syncobjs;
1060 int ret;
1061
1062 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
1063 if (handles == NULL)
1064 return -ENOMEM;
1065
1066 if (copy_from_user(handles, user_handles,
1067 sizeof(uint32_t) * count_handles)) {
1068 ret = -EFAULT;
1069 goto err_free_handles;
1070 }
1071
1072 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
1073 if (syncobjs == NULL) {
1074 ret = -ENOMEM;
1075 goto err_free_handles;
1076 }
1077
1078 for (i = 0; i < count_handles; i++) {
1079 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
1080 if (!syncobjs[i]) {
1081 ret = -ENOENT;
1082 goto err_put_syncobjs;
1083 }
1084 }
1085
1086 kfree(handles);
1087 *syncobjs_out = syncobjs;
1088 return 0;
1089
1090err_put_syncobjs:
1091 while (i-- > 0)
1092 drm_syncobj_put(syncobjs[i]);
1093 kfree(syncobjs);
1094err_free_handles:
1095 kfree(handles);
1096
1097 return ret;
1098}
1099
1100static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
1101 uint32_t count)
1102{
1103 uint32_t i;
1104 for (i = 0; i < count; i++)
1105 drm_syncobj_put(syncobjs[i]);
1106 kfree(syncobjs);
1107}
1108
Dave Airlie5e60a102017-08-25 10:52:22 -07001109int
1110drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
1111 struct drm_file *file_private)
1112{
1113 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001114 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -07001115 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -07001116
1117 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001118 return -EOPNOTSUPP;
Dave Airlie5e60a102017-08-25 10:52:22 -07001119
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001120 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
1121 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -07001122 return -EINVAL;
1123
1124 if (args->count_handles == 0)
1125 return -EINVAL;
1126
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001127 ret = drm_syncobj_array_find(file_private,
1128 u64_to_user_ptr(args->handles),
1129 args->count_handles,
1130 &syncobjs);
1131 if (ret < 0)
1132 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -07001133
Jason Ekstrande7aca5032017-08-25 10:52:24 -07001134 ret = drm_syncobj_array_wait(dev, file_private,
1135 args, syncobjs);
Dave Airlie5e60a102017-08-25 10:52:22 -07001136
Jason Ekstrand3e6fb722017-08-25 10:52:26 -07001137 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -07001138
1139 return ret;
1140}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001141
1142int
1143drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1144 struct drm_file *file_private)
1145{
1146 struct drm_syncobj_array *args = data;
1147 struct drm_syncobj **syncobjs;
1148 uint32_t i;
1149 int ret;
1150
1151 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001152 return -EOPNOTSUPP;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001153
1154 if (args->pad != 0)
1155 return -EINVAL;
1156
1157 if (args->count_handles == 0)
1158 return -EINVAL;
1159
1160 ret = drm_syncobj_array_find(file_private,
1161 u64_to_user_ptr(args->handles),
1162 args->count_handles,
1163 &syncobjs);
1164 if (ret < 0)
1165 return ret;
1166
Chunming Zhou48197bc2018-10-18 14:18:36 +08001167 for (i = 0; i < args->count_handles; i++) {
1168 drm_syncobj_fini(syncobjs[i]);
1169 drm_syncobj_init(syncobjs[i]);
1170 }
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001171 drm_syncobj_array_free(syncobjs, args->count_handles);
1172
Chunming Zhou48197bc2018-10-18 14:18:36 +08001173 return ret;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001174}
Jason Ekstrandffa94432017-08-28 14:10:28 -07001175
1176int
1177drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1178 struct drm_file *file_private)
1179{
1180 struct drm_syncobj_array *args = data;
1181 struct drm_syncobj **syncobjs;
1182 uint32_t i;
1183 int ret;
1184
1185 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001186 return -EOPNOTSUPP;
Jason Ekstrandffa94432017-08-28 14:10:28 -07001187
1188 if (args->pad != 0)
1189 return -EINVAL;
1190
1191 if (args->count_handles == 0)
1192 return -EINVAL;
1193
1194 ret = drm_syncobj_array_find(file_private,
1195 u64_to_user_ptr(args->handles),
1196 args->count_handles,
1197 &syncobjs);
1198 if (ret < 0)
1199 return ret;
1200
1201 for (i = 0; i < args->count_handles; i++) {
1202 ret = drm_syncobj_assign_null_handle(syncobjs[i]);
1203 if (ret < 0)
1204 break;
1205 }
1206
1207 drm_syncobj_array_free(syncobjs, args->count_handles);
1208
1209 return ret;
1210}