blob: 3a1be7f79de14dbc8da56b1c4b5e3da7ee0a0864 [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
Christian König61a98b12018-12-11 18:34:41 +080059struct syncobj_wait_entry {
60 struct list_head node;
61 struct task_struct *task;
62 struct dma_fence *fence;
63 struct dma_fence_cb fence_cb;
Chunming Zhou01d6c352019-04-01 17:50:57 +080064 u64 point;
Christian König61a98b12018-12-11 18:34:41 +080065};
66
67static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
68 struct syncobj_wait_entry *wait);
69
Dave Airliee9083422017-04-04 13:26:24 +100070/**
71 * drm_syncobj_find - lookup and reference a sync object.
72 * @file_private: drm file private pointer
73 * @handle: sync object handle to lookup.
74 *
Daniel Vetter924fe8d2017-12-14 21:30:52 +010075 * Returns a reference to the syncobj pointed to by handle or NULL. The
76 * reference must be released by calling drm_syncobj_put().
Dave Airliee9083422017-04-04 13:26:24 +100077 */
78struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private,
79 u32 handle)
80{
81 struct drm_syncobj *syncobj;
82
83 spin_lock(&file_private->syncobj_table_lock);
84
85 /* Check if we currently have a reference on the object */
86 syncobj = idr_find(&file_private->syncobj_idr, handle);
87 if (syncobj)
88 drm_syncobj_get(syncobj);
89
90 spin_unlock(&file_private->syncobj_table_lock);
91
92 return syncobj;
93}
94EXPORT_SYMBOL(drm_syncobj_find);
95
Christian König61a98b12018-12-11 18:34:41 +080096static void drm_syncobj_fence_add_wait(struct drm_syncobj *syncobj,
97 struct syncobj_wait_entry *wait)
Chunming Zhou43cf1fc2018-10-23 17:37:45 +080098{
Chunming Zhou01d6c352019-04-01 17:50:57 +080099 struct dma_fence *fence;
100
Christian König61a98b12018-12-11 18:34:41 +0800101 if (wait->fence)
102 return;
Jason Ekstrand337fe9f2018-09-26 02:17:03 -0500103
Eric Anholt131280a2018-11-08 08:04:22 -0800104 spin_lock(&syncobj->lock);
105 /* We've already tried once to get a fence and failed. Now that we
106 * have the lock, try one more time just to be sure we don't add a
107 * callback when a fence has already been set.
108 */
Chunming Zhou01d6c352019-04-01 17:50:57 +0800109 fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, 1));
110 if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
111 dma_fence_put(fence);
Christian König61a98b12018-12-11 18:34:41 +0800112 list_add_tail(&wait->node, &syncobj->cb_list);
Chunming Zhou01d6c352019-04-01 17:50:57 +0800113 } else if (!fence) {
114 wait->fence = dma_fence_get_stub();
115 } else {
116 wait->fence = fence;
117 }
Eric Anholt131280a2018-11-08 08:04:22 -0800118 spin_unlock(&syncobj->lock);
Chunming Zhou48197bc2018-10-18 14:18:36 +0800119}
Eric Anholt131280a2018-11-08 08:04:22 -0800120
Christian König61a98b12018-12-11 18:34:41 +0800121static void drm_syncobj_remove_wait(struct drm_syncobj *syncobj,
122 struct syncobj_wait_entry *wait)
Eric Anholt131280a2018-11-08 08:04:22 -0800123{
Christian König61a98b12018-12-11 18:34:41 +0800124 if (!wait->node.next)
125 return;
126
Eric Anholt131280a2018-11-08 08:04:22 -0800127 spin_lock(&syncobj->lock);
Christian König61a98b12018-12-11 18:34:41 +0800128 list_del_init(&wait->node);
Eric Anholt131280a2018-11-08 08:04:22 -0800129 spin_unlock(&syncobj->lock);
130}
131
Dave Airliee9083422017-04-04 13:26:24 +1000132/**
Christian König44f8a132019-04-01 17:50:56 +0800133 * drm_syncobj_add_point - add new timeline point to the syncobj
134 * @syncobj: sync object to add timeline point do
135 * @chain: chain node to use to add the point
136 * @fence: fence to encapsulate in the chain node
137 * @point: sequence number to use for the point
138 *
139 * Add the chain node as new timeline point to the syncobj.
140 */
141void drm_syncobj_add_point(struct drm_syncobj *syncobj,
142 struct dma_fence_chain *chain,
143 struct dma_fence *fence,
144 uint64_t point)
145{
146 struct syncobj_wait_entry *cur, *tmp;
147 struct dma_fence *prev;
148
149 dma_fence_get(fence);
150
151 spin_lock(&syncobj->lock);
152
153 prev = drm_syncobj_fence_get(syncobj);
154 /* You are adding an unorder point to timeline, which could cause payload returned from query_ioctl is 0! */
155 if (prev && prev->seqno >= point)
156 DRM_ERROR("You are adding an unorder point to timeline!\n");
157 dma_fence_chain_init(chain, prev, fence, point);
158 rcu_assign_pointer(syncobj->fence, &chain->base);
159
Chunming Zhou01d6c352019-04-01 17:50:57 +0800160 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
Christian König44f8a132019-04-01 17:50:56 +0800161 syncobj_wait_syncobj_func(syncobj, cur);
Christian König44f8a132019-04-01 17:50:56 +0800162 spin_unlock(&syncobj->lock);
163
164 /* Walk the chain once to trigger garbage collection */
165 dma_fence_chain_for_each(fence, prev);
166 dma_fence_put(prev);
167}
168EXPORT_SYMBOL(drm_syncobj_add_point);
169
170/**
Dave Airliee9083422017-04-04 13:26:24 +1000171 * drm_syncobj_replace_fence - replace fence in a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000172 * @syncobj: Sync object to replace fence in
173 * @fence: fence to install in sync file.
174 *
Christian König0b258ed2018-11-14 14:24:27 +0100175 * This replaces the fence on a sync object.
Dave Airliee9083422017-04-04 13:26:24 +1000176 */
Chris Wilson00fc2c22017-07-05 21:12:44 +0100177void drm_syncobj_replace_fence(struct drm_syncobj *syncobj,
Dave Airliee9083422017-04-04 13:26:24 +1000178 struct dma_fence *fence)
179{
Eric Anholt131280a2018-11-08 08:04:22 -0800180 struct dma_fence *old_fence;
Christian König61a98b12018-12-11 18:34:41 +0800181 struct syncobj_wait_entry *cur, *tmp;
Dave Airliee9083422017-04-04 13:26:24 +1000182
Eric Anholt131280a2018-11-08 08:04:22 -0800183 if (fence)
184 dma_fence_get(fence);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700185
Eric Anholt131280a2018-11-08 08:04:22 -0800186 spin_lock(&syncobj->lock);
187
188 old_fence = rcu_dereference_protected(syncobj->fence,
189 lockdep_is_held(&syncobj->lock));
190 rcu_assign_pointer(syncobj->fence, fence);
191
192 if (fence != old_fence) {
Chunming Zhou01d6c352019-04-01 17:50:57 +0800193 list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node)
Christian König61a98b12018-12-11 18:34:41 +0800194 syncobj_wait_syncobj_func(syncobj, cur);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700195 }
Eric Anholt131280a2018-11-08 08:04:22 -0800196
197 spin_unlock(&syncobj->lock);
198
199 dma_fence_put(old_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000200}
201EXPORT_SYMBOL(drm_syncobj_replace_fence);
202
Christian König86bbd892018-11-13 14:14:00 +0100203/**
204 * drm_syncobj_assign_null_handle - assign a stub fence to the sync object
205 * @syncobj: sync object to assign the fence on
206 *
207 * Assign a already signaled stub fence to the sync object.
208 */
209static void drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj)
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700210{
Christian König86bbd892018-11-13 14:14:00 +0100211 struct dma_fence *fence = dma_fence_get_stub();
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700212
Christian König0b258ed2018-11-14 14:24:27 +0100213 drm_syncobj_replace_fence(syncobj, fence);
Christian König86bbd892018-11-13 14:14:00 +0100214 dma_fence_put(fence);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700215}
216
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100217/**
218 * drm_syncobj_find_fence - lookup and reference the fence in a sync object
219 * @file_private: drm file private pointer
220 * @handle: sync object handle to lookup.
Chunming Zhou0a6730e2018-08-30 14:48:29 +0800221 * @point: timeline point
Chunming Zhou871edc92018-10-17 15:03:18 +0800222 * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100223 * @fence: out parameter for the fence
224 *
225 * This is just a convenience function that combines drm_syncobj_find() and
Eric Anholt131280a2018-11-08 08:04:22 -0800226 * drm_syncobj_fence_get().
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100227 *
228 * Returns 0 on success or a negative error value on failure. On success @fence
229 * contains a reference to the fence, which must be released by calling
230 * dma_fence_put().
231 */
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700232int drm_syncobj_find_fence(struct drm_file *file_private,
Chunming Zhou649fdce2018-10-15 16:55:47 +0800233 u32 handle, u64 point, u64 flags,
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700234 struct dma_fence **fence)
Dave Airliee9083422017-04-04 13:26:24 +1000235{
236 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
Eric Anholt131280a2018-11-08 08:04:22 -0800237 int ret = 0;
Dave Airliee9083422017-04-04 13:26:24 +1000238
Eric Anholt131280a2018-11-08 08:04:22 -0800239 if (!syncobj)
240 return -ENOENT;
241
242 *fence = drm_syncobj_fence_get(syncobj);
243 if (!*fence) {
244 ret = -EINVAL;
245 }
246 drm_syncobj_put(syncobj);
Dave Airliee9083422017-04-04 13:26:24 +1000247 return ret;
248}
Jason Ekstrandafaf5922017-08-25 10:52:19 -0700249EXPORT_SYMBOL(drm_syncobj_find_fence);
Dave Airliee9083422017-04-04 13:26:24 +1000250
251/**
252 * drm_syncobj_free - free a sync object.
253 * @kref: kref to free.
254 *
255 * Only to be called from kref_put in drm_syncobj_put.
256 */
257void drm_syncobj_free(struct kref *kref)
258{
259 struct drm_syncobj *syncobj = container_of(kref,
260 struct drm_syncobj,
261 refcount);
Christian König0b258ed2018-11-14 14:24:27 +0100262 drm_syncobj_replace_fence(syncobj, NULL);
Dave Airliee9083422017-04-04 13:26:24 +1000263 kfree(syncobj);
264}
265EXPORT_SYMBOL(drm_syncobj_free);
266
Marek Olšák1321fd22017-09-12 22:42:12 +0200267/**
268 * drm_syncobj_create - create a new syncobj
269 * @out_syncobj: returned syncobj
270 * @flags: DRM_SYNCOBJ_* flags
271 * @fence: if non-NULL, the syncobj will represent this fence
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100272 *
273 * This is the first function to create a sync object. After creating, drivers
274 * probably want to make it available to userspace, either through
275 * drm_syncobj_get_handle() or drm_syncobj_get_fd().
276 *
277 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200278 */
279int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags,
280 struct dma_fence *fence)
Dave Airliee9083422017-04-04 13:26:24 +1000281{
Dave Airliee9083422017-04-04 13:26:24 +1000282 struct drm_syncobj *syncobj;
283
284 syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL);
285 if (!syncobj)
286 return -ENOMEM;
287
288 kref_init(&syncobj->refcount);
Jason Ekstrand9c19fb12017-08-28 07:39:25 -0700289 INIT_LIST_HEAD(&syncobj->cb_list);
Eric Anholt131280a2018-11-08 08:04:22 -0800290 spin_lock_init(&syncobj->lock);
Dave Airliee9083422017-04-04 13:26:24 +1000291
Christian König86bbd892018-11-13 14:14:00 +0100292 if (flags & DRM_SYNCOBJ_CREATE_SIGNALED)
293 drm_syncobj_assign_null_handle(syncobj);
Jason Ekstrand1fc08212017-08-25 10:52:25 -0700294
Marek Olšák1321fd22017-09-12 22:42:12 +0200295 if (fence)
Christian König0b258ed2018-11-14 14:24:27 +0100296 drm_syncobj_replace_fence(syncobj, fence);
Marek Olšák1321fd22017-09-12 22:42:12 +0200297
298 *out_syncobj = syncobj;
299 return 0;
300}
301EXPORT_SYMBOL(drm_syncobj_create);
302
303/**
304 * drm_syncobj_get_handle - get a handle from a syncobj
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100305 * @file_private: drm file private pointer
306 * @syncobj: Sync object to export
307 * @handle: out parameter with the new handle
308 *
309 * Exports a sync object created with drm_syncobj_create() as a handle on
310 * @file_private to userspace.
311 *
312 * Returns 0 on success or a negative error value on failure.
Marek Olšák1321fd22017-09-12 22:42:12 +0200313 */
314int drm_syncobj_get_handle(struct drm_file *file_private,
315 struct drm_syncobj *syncobj, u32 *handle)
316{
317 int ret;
318
319 /* take a reference to put in the idr */
320 drm_syncobj_get(syncobj);
321
Dave Airliee9083422017-04-04 13:26:24 +1000322 idr_preload(GFP_KERNEL);
323 spin_lock(&file_private->syncobj_table_lock);
324 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
325 spin_unlock(&file_private->syncobj_table_lock);
326
327 idr_preload_end();
328
329 if (ret < 0) {
330 drm_syncobj_put(syncobj);
331 return ret;
332 }
333
334 *handle = ret;
335 return 0;
336}
Marek Olšák1321fd22017-09-12 22:42:12 +0200337EXPORT_SYMBOL(drm_syncobj_get_handle);
338
339static int drm_syncobj_create_as_handle(struct drm_file *file_private,
340 u32 *handle, uint32_t flags)
341{
342 int ret;
343 struct drm_syncobj *syncobj;
344
345 ret = drm_syncobj_create(&syncobj, flags, NULL);
346 if (ret)
347 return ret;
348
349 ret = drm_syncobj_get_handle(file_private, syncobj, handle);
350 drm_syncobj_put(syncobj);
351 return ret;
352}
Dave Airliee9083422017-04-04 13:26:24 +1000353
354static int drm_syncobj_destroy(struct drm_file *file_private,
355 u32 handle)
356{
357 struct drm_syncobj *syncobj;
358
359 spin_lock(&file_private->syncobj_table_lock);
360 syncobj = idr_remove(&file_private->syncobj_idr, handle);
361 spin_unlock(&file_private->syncobj_table_lock);
362
363 if (!syncobj)
364 return -EINVAL;
365
366 drm_syncobj_put(syncobj);
367 return 0;
368}
369
370static int drm_syncobj_file_release(struct inode *inode, struct file *file)
371{
372 struct drm_syncobj *syncobj = file->private_data;
373
374 drm_syncobj_put(syncobj);
375 return 0;
376}
377
378static const struct file_operations drm_syncobj_file_fops = {
379 .release = drm_syncobj_file_release,
380};
381
Daniel Vetter924fe8d2017-12-14 21:30:52 +0100382/**
383 * drm_syncobj_get_fd - get a file descriptor from a syncobj
384 * @syncobj: Sync object to export
385 * @p_fd: out parameter with the new file descriptor
386 *
387 * Exports a sync object created with drm_syncobj_create() as a file descriptor.
388 *
389 * Returns 0 on success or a negative error value on failure.
390 */
Marek Olšák684fd0a2017-09-12 22:42:13 +0200391int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd)
392{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000393 struct file *file;
Marek Olšák684fd0a2017-09-12 22:42:13 +0200394 int fd;
395
396 fd = get_unused_fd_flags(O_CLOEXEC);
397 if (fd < 0)
398 return fd;
399
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000400 file = anon_inode_getfile("syncobj_file",
401 &drm_syncobj_file_fops,
402 syncobj, 0);
403 if (IS_ERR(file)) {
404 put_unused_fd(fd);
405 return PTR_ERR(file);
Marek Olšák684fd0a2017-09-12 22:42:13 +0200406 }
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000407
408 drm_syncobj_get(syncobj);
409 fd_install(fd, file);
410
Marek Olšák684fd0a2017-09-12 22:42:13 +0200411 *p_fd = fd;
412 return 0;
413}
414EXPORT_SYMBOL(drm_syncobj_get_fd);
415
Dave Airliee9083422017-04-04 13:26:24 +1000416static int drm_syncobj_handle_to_fd(struct drm_file *file_private,
417 u32 handle, int *p_fd)
418{
419 struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle);
420 int ret;
Dave Airliee9083422017-04-04 13:26:24 +1000421
422 if (!syncobj)
423 return -EINVAL;
424
Marek Olšák684fd0a2017-09-12 22:42:13 +0200425 ret = drm_syncobj_get_fd(syncobj, p_fd);
Dave Airliee9083422017-04-04 13:26:24 +1000426 drm_syncobj_put(syncobj);
427 return ret;
428}
429
Dave Airliee9083422017-04-04 13:26:24 +1000430static int drm_syncobj_fd_to_handle(struct drm_file *file_private,
431 int fd, u32 *handle)
432{
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000433 struct drm_syncobj *syncobj;
434 struct file *file;
Dave Airliee9083422017-04-04 13:26:24 +1000435 int ret;
436
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000437 file = fget(fd);
438 if (!file)
Dave Airliee9083422017-04-04 13:26:24 +1000439 return -EINVAL;
440
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000441 if (file->f_op != &drm_syncobj_file_fops) {
442 fput(file);
443 return -EINVAL;
444 }
445
Dave Airliee9083422017-04-04 13:26:24 +1000446 /* take a reference to put in the idr */
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000447 syncobj = file->private_data;
Dave Airliee9083422017-04-04 13:26:24 +1000448 drm_syncobj_get(syncobj);
449
450 idr_preload(GFP_KERNEL);
451 spin_lock(&file_private->syncobj_table_lock);
452 ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT);
453 spin_unlock(&file_private->syncobj_table_lock);
454 idr_preload_end();
455
Chris Wilsone7cdf5c2017-12-19 12:07:00 +0000456 if (ret > 0) {
457 *handle = ret;
458 ret = 0;
459 } else
460 drm_syncobj_put(syncobj);
461
462 fput(file);
463 return ret;
Dave Airliee9083422017-04-04 13:26:24 +1000464}
465
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300466static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private,
467 int fd, int handle)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100468{
469 struct dma_fence *fence = sync_file_get_fence(fd);
470 struct drm_syncobj *syncobj;
471
472 if (!fence)
473 return -EINVAL;
474
475 syncobj = drm_syncobj_find(file_private, handle);
476 if (!syncobj) {
477 dma_fence_put(fence);
478 return -ENOENT;
479 }
480
Christian König0b258ed2018-11-14 14:24:27 +0100481 drm_syncobj_replace_fence(syncobj, fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100482 dma_fence_put(fence);
483 drm_syncobj_put(syncobj);
484 return 0;
485}
486
Ville Syrjäläa32c94a2017-09-01 19:53:25 +0300487static int drm_syncobj_export_sync_file(struct drm_file *file_private,
488 int handle, int *p_fd)
Dave Airlie3ee45a32017-04-26 04:09:02 +0100489{
490 int ret;
491 struct dma_fence *fence;
492 struct sync_file *sync_file;
493 int fd = get_unused_fd_flags(O_CLOEXEC);
494
495 if (fd < 0)
496 return fd;
497
Chunming Zhou649fdce2018-10-15 16:55:47 +0800498 ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence);
Dave Airlie3ee45a32017-04-26 04:09:02 +0100499 if (ret)
500 goto err_put_fd;
501
502 sync_file = sync_file_create(fence);
503
504 dma_fence_put(fence);
505
506 if (!sync_file) {
507 ret = -EINVAL;
508 goto err_put_fd;
509 }
510
511 fd_install(fd, sync_file->file);
512
513 *p_fd = fd;
514 return 0;
515err_put_fd:
516 put_unused_fd(fd);
517 return ret;
518}
Dave Airliee9083422017-04-04 13:26:24 +1000519/**
520 * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time
Dave Airliee9083422017-04-04 13:26:24 +1000521 * @file_private: drm file-private structure to set up
522 *
523 * Called at device open time, sets up the structure for handling refcounting
524 * of sync objects.
525 */
526void
527drm_syncobj_open(struct drm_file *file_private)
528{
Chris Wilsone86584c2018-02-12 14:55:33 +0000529 idr_init_base(&file_private->syncobj_idr, 1);
Dave Airliee9083422017-04-04 13:26:24 +1000530 spin_lock_init(&file_private->syncobj_table_lock);
531}
532
533static int
534drm_syncobj_release_handle(int id, void *ptr, void *data)
535{
536 struct drm_syncobj *syncobj = ptr;
537
538 drm_syncobj_put(syncobj);
539 return 0;
540}
541
542/**
543 * drm_syncobj_release - release file-private sync object resources
Dave Airliee9083422017-04-04 13:26:24 +1000544 * @file_private: drm file-private structure to clean up
545 *
546 * Called at close time when the filp is going away.
547 *
548 * Releases any remaining references on objects by this filp.
549 */
550void
551drm_syncobj_release(struct drm_file *file_private)
552{
553 idr_for_each(&file_private->syncobj_idr,
554 &drm_syncobj_release_handle, file_private);
555 idr_destroy(&file_private->syncobj_idr);
556}
557
558int
559drm_syncobj_create_ioctl(struct drm_device *dev, void *data,
560 struct drm_file *file_private)
561{
562 struct drm_syncobj_create *args = data;
563
564 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100565 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000566
567 /* no valid flags yet */
Eric Anholt131280a2018-11-08 08:04:22 -0800568 if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED)
Dave Airliee9083422017-04-04 13:26:24 +1000569 return -EINVAL;
570
Marek Olšák1321fd22017-09-12 22:42:12 +0200571 return drm_syncobj_create_as_handle(file_private,
572 &args->handle, args->flags);
Dave Airliee9083422017-04-04 13:26:24 +1000573}
574
575int
576drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data,
577 struct drm_file *file_private)
578{
579 struct drm_syncobj_destroy *args = data;
580
581 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100582 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000583
584 /* make sure padding is empty */
585 if (args->pad)
586 return -EINVAL;
587 return drm_syncobj_destroy(file_private, args->handle);
588}
589
590int
591drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data,
592 struct drm_file *file_private)
593{
594 struct drm_syncobj_handle *args = data;
595
596 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100597 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000598
Dave Airlie3ee45a32017-04-26 04:09:02 +0100599 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000600 return -EINVAL;
601
Dave Airlie3ee45a32017-04-26 04:09:02 +0100602 if (args->flags != 0 &&
603 args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
604 return -EINVAL;
605
606 if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE)
607 return drm_syncobj_export_sync_file(file_private, args->handle,
608 &args->fd);
609
Dave Airliee9083422017-04-04 13:26:24 +1000610 return drm_syncobj_handle_to_fd(file_private, args->handle,
611 &args->fd);
612}
613
614int
615drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data,
616 struct drm_file *file_private)
617{
618 struct drm_syncobj_handle *args = data;
619
620 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100621 return -EOPNOTSUPP;
Dave Airliee9083422017-04-04 13:26:24 +1000622
Dave Airlie3ee45a32017-04-26 04:09:02 +0100623 if (args->pad)
Dave Airliee9083422017-04-04 13:26:24 +1000624 return -EINVAL;
625
Dave Airlie3ee45a32017-04-26 04:09:02 +0100626 if (args->flags != 0 &&
627 args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
628 return -EINVAL;
629
630 if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE)
631 return drm_syncobj_import_sync_file_fence(file_private,
632 args->fd,
633 args->handle);
634
Dave Airliee9083422017-04-04 13:26:24 +1000635 return drm_syncobj_fd_to_handle(file_private, args->fd,
636 &args->handle);
637}
Dave Airlie5e60a102017-08-25 10:52:22 -0700638
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700639static void syncobj_wait_fence_func(struct dma_fence *fence,
640 struct dma_fence_cb *cb)
641{
642 struct syncobj_wait_entry *wait =
643 container_of(cb, struct syncobj_wait_entry, fence_cb);
644
645 wake_up_process(wait->task);
646}
647
648static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj,
Christian König61a98b12018-12-11 18:34:41 +0800649 struct syncobj_wait_entry *wait)
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700650{
Chunming Zhou01d6c352019-04-01 17:50:57 +0800651 struct dma_fence *fence;
652
Eric Anholt131280a2018-11-08 08:04:22 -0800653 /* This happens inside the syncobj lock */
Chunming Zhou01d6c352019-04-01 17:50:57 +0800654 fence = rcu_dereference_protected(syncobj->fence,
655 lockdep_is_held(&syncobj->lock));
656 dma_fence_get(fence);
657 if (!fence || dma_fence_chain_find_seqno(&fence, wait->point)) {
658 dma_fence_put(fence);
659 return;
660 } else if (!fence) {
661 wait->fence = dma_fence_get_stub();
662 } else {
663 wait->fence = fence;
664 }
665
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700666 wake_up_process(wait->task);
Chunming Zhou01d6c352019-04-01 17:50:57 +0800667 list_del_init(&wait->node);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700668}
669
670static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs,
Chunming Zhou01d6c352019-04-01 17:50:57 +0800671 void __user *user_points,
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700672 uint32_t count,
673 uint32_t flags,
674 signed long timeout,
675 uint32_t *idx)
676{
677 struct syncobj_wait_entry *entries;
678 struct dma_fence *fence;
Chunming Zhou01d6c352019-04-01 17:50:57 +0800679 uint64_t *points;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700680 uint32_t signaled_count, i;
681
Chunming Zhou01d6c352019-04-01 17:50:57 +0800682 points = kmalloc_array(count, sizeof(*points), GFP_KERNEL);
683 if (points == NULL)
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700684 return -ENOMEM;
685
Chunming Zhou01d6c352019-04-01 17:50:57 +0800686 if (!user_points) {
687 memset(points, 0, count * sizeof(uint64_t));
688
689 } else if (copy_from_user(points, user_points,
690 sizeof(uint64_t) * count)) {
691 timeout = -EFAULT;
692 goto err_free_points;
693 }
694
695 entries = kcalloc(count, sizeof(*entries), GFP_KERNEL);
696 if (!entries) {
697 timeout = -ENOMEM;
698 goto err_free_points;
699 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700700 /* Walk the list of sync objects and initialize entries. We do
701 * this up-front so that we can properly return -EINVAL if there is
702 * a syncobj with a missing fence and then never have the chance of
703 * returning -EINVAL again.
704 */
705 signaled_count = 0;
706 for (i = 0; i < count; ++i) {
Chunming Zhou01d6c352019-04-01 17:50:57 +0800707 struct dma_fence *fence;
708
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700709 entries[i].task = current;
Chunming Zhou01d6c352019-04-01 17:50:57 +0800710 entries[i].point = points[i];
711 fence = drm_syncobj_fence_get(syncobjs[i]);
712 if (!fence || dma_fence_chain_find_seqno(&fence, points[i])) {
713 dma_fence_put(fence);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700714 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
715 continue;
716 } else {
Chris Wilson12fec622018-09-20 21:05:30 +0100717 timeout = -EINVAL;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700718 goto cleanup_entries;
719 }
720 }
721
Chunming Zhou01d6c352019-04-01 17:50:57 +0800722 if (fence)
723 entries[i].fence = fence;
724 else
725 entries[i].fence = dma_fence_get_stub();
726
727 if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
728 dma_fence_is_signaled(entries[i].fence)) {
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700729 if (signaled_count == 0 && idx)
730 *idx = i;
731 signaled_count++;
732 }
733 }
734
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700735 if (signaled_count == count ||
736 (signaled_count > 0 &&
737 !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL)))
738 goto cleanup_entries;
739
740 /* There's a very annoying laxness in the dma_fence API here, in
741 * that backends are not required to automatically report when a
742 * fence is signaled prior to fence->ops->enable_signaling() being
743 * called. So here if we fail to match signaled_count, we need to
744 * fallthough and try a 0 timeout wait!
745 */
746
747 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) {
Christian König61a98b12018-12-11 18:34:41 +0800748 for (i = 0; i < count; ++i)
749 drm_syncobj_fence_add_wait(syncobjs[i], &entries[i]);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700750 }
751
752 do {
753 set_current_state(TASK_INTERRUPTIBLE);
754
755 signaled_count = 0;
756 for (i = 0; i < count; ++i) {
757 fence = entries[i].fence;
758 if (!fence)
759 continue;
760
Chunming Zhou01d6c352019-04-01 17:50:57 +0800761 if ((flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE) ||
762 dma_fence_is_signaled(fence) ||
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700763 (!entries[i].fence_cb.func &&
764 dma_fence_add_callback(fence,
765 &entries[i].fence_cb,
766 syncobj_wait_fence_func))) {
767 /* The fence has been signaled */
768 if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) {
769 signaled_count++;
770 } else {
771 if (idx)
772 *idx = i;
773 goto done_waiting;
774 }
775 }
776 }
777
778 if (signaled_count == count)
779 goto done_waiting;
780
781 if (timeout == 0) {
Chris Wilson12fec622018-09-20 21:05:30 +0100782 timeout = -ETIME;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700783 goto done_waiting;
784 }
785
Chris Wilson12fec622018-09-20 21:05:30 +0100786 if (signal_pending(current)) {
787 timeout = -ERESTARTSYS;
788 goto done_waiting;
789 }
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700790
Chris Wilson12fec622018-09-20 21:05:30 +0100791 timeout = schedule_timeout(timeout);
792 } while (1);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700793
794done_waiting:
795 __set_current_state(TASK_RUNNING);
796
797cleanup_entries:
798 for (i = 0; i < count; ++i) {
Christian König61a98b12018-12-11 18:34:41 +0800799 drm_syncobj_remove_wait(syncobjs[i], &entries[i]);
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700800 if (entries[i].fence_cb.func)
801 dma_fence_remove_callback(entries[i].fence,
802 &entries[i].fence_cb);
803 dma_fence_put(entries[i].fence);
804 }
805 kfree(entries);
806
Chunming Zhou01d6c352019-04-01 17:50:57 +0800807err_free_points:
808 kfree(points);
809
Chris Wilson12fec622018-09-20 21:05:30 +0100810 return timeout;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700811}
812
Dave Airlie5e60a102017-08-25 10:52:22 -0700813/**
814 * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value
815 *
816 * @timeout_nsec: timeout nsec component in ns, 0 for poll
817 *
818 * Calculate the timeout in jiffies from an absolute time in sec/nsec.
819 */
Qiang Yu877b372992019-02-25 22:07:16 +0800820signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec)
Dave Airlie5e60a102017-08-25 10:52:22 -0700821{
822 ktime_t abs_timeout, now;
823 u64 timeout_ns, timeout_jiffies64;
824
825 /* make 0 timeout means poll - absolute 0 doesn't seem valid */
826 if (timeout_nsec == 0)
827 return 0;
828
829 abs_timeout = ns_to_ktime(timeout_nsec);
830 now = ktime_get();
831
832 if (!ktime_after(abs_timeout, now))
833 return 0;
834
835 timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now));
836
837 timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns);
838 /* clamp timeout to avoid infinite timeout */
839 if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1)
840 return MAX_SCHEDULE_TIMEOUT - 1;
841
842 return timeout_jiffies64 + 1;
843}
Qiang Yu877b372992019-02-25 22:07:16 +0800844EXPORT_SYMBOL(drm_timeout_abs_to_jiffies);
Dave Airlie5e60a102017-08-25 10:52:22 -0700845
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700846static int drm_syncobj_array_wait(struct drm_device *dev,
847 struct drm_file *file_private,
848 struct drm_syncobj_wait *wait,
Chunming Zhou01d6c352019-04-01 17:50:57 +0800849 struct drm_syncobj_timeline_wait *timeline_wait,
850 struct drm_syncobj **syncobjs, bool timeline)
Dave Airlie5e60a102017-08-25 10:52:22 -0700851{
Chunming Zhou01d6c352019-04-01 17:50:57 +0800852 signed long timeout = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -0700853 uint32_t first = ~0;
854
Chunming Zhou01d6c352019-04-01 17:50:57 +0800855 if (!timeline) {
856 timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec);
857 timeout = drm_syncobj_array_wait_timeout(syncobjs,
858 NULL,
859 wait->count_handles,
860 wait->flags,
861 timeout, &first);
862 if (timeout < 0)
863 return timeout;
864 wait->first_signaled = first;
865 } else {
866 timeout = drm_timeout_abs_to_jiffies(timeline_wait->timeout_nsec);
867 timeout = drm_syncobj_array_wait_timeout(syncobjs,
868 u64_to_user_ptr(timeline_wait->points),
869 timeline_wait->count_handles,
870 timeline_wait->flags,
871 timeout, &first);
872 if (timeout < 0)
873 return timeout;
874 timeline_wait->first_signaled = first;
875 }
Dave Airlie5e60a102017-08-25 10:52:22 -0700876 return 0;
877}
878
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700879static int drm_syncobj_array_find(struct drm_file *file_private,
Ville Syrjälä9e554462017-09-01 19:53:26 +0300880 void __user *user_handles,
881 uint32_t count_handles,
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700882 struct drm_syncobj ***syncobjs_out)
883{
884 uint32_t i, *handles;
885 struct drm_syncobj **syncobjs;
886 int ret;
887
888 handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL);
889 if (handles == NULL)
890 return -ENOMEM;
891
892 if (copy_from_user(handles, user_handles,
893 sizeof(uint32_t) * count_handles)) {
894 ret = -EFAULT;
895 goto err_free_handles;
896 }
897
898 syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL);
899 if (syncobjs == NULL) {
900 ret = -ENOMEM;
901 goto err_free_handles;
902 }
903
904 for (i = 0; i < count_handles; i++) {
905 syncobjs[i] = drm_syncobj_find(file_private, handles[i]);
906 if (!syncobjs[i]) {
907 ret = -ENOENT;
908 goto err_put_syncobjs;
909 }
910 }
911
912 kfree(handles);
913 *syncobjs_out = syncobjs;
914 return 0;
915
916err_put_syncobjs:
917 while (i-- > 0)
918 drm_syncobj_put(syncobjs[i]);
919 kfree(syncobjs);
920err_free_handles:
921 kfree(handles);
922
923 return ret;
924}
925
926static void drm_syncobj_array_free(struct drm_syncobj **syncobjs,
927 uint32_t count)
928{
929 uint32_t i;
930 for (i = 0; i < count; i++)
931 drm_syncobj_put(syncobjs[i]);
932 kfree(syncobjs);
933}
934
Dave Airlie5e60a102017-08-25 10:52:22 -0700935int
936drm_syncobj_wait_ioctl(struct drm_device *dev, void *data,
937 struct drm_file *file_private)
938{
939 struct drm_syncobj_wait *args = data;
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700940 struct drm_syncobj **syncobjs;
Dave Airlie5e60a102017-08-25 10:52:22 -0700941 int ret = 0;
Dave Airlie5e60a102017-08-25 10:52:22 -0700942
943 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +0100944 return -EOPNOTSUPP;
Dave Airlie5e60a102017-08-25 10:52:22 -0700945
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700946 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
947 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT))
Dave Airlie5e60a102017-08-25 10:52:22 -0700948 return -EINVAL;
949
950 if (args->count_handles == 0)
951 return -EINVAL;
952
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700953 ret = drm_syncobj_array_find(file_private,
954 u64_to_user_ptr(args->handles),
955 args->count_handles,
956 &syncobjs);
957 if (ret < 0)
958 return ret;
Dave Airlie5e60a102017-08-25 10:52:22 -0700959
Jason Ekstrande7aca5032017-08-25 10:52:24 -0700960 ret = drm_syncobj_array_wait(dev, file_private,
Chunming Zhou01d6c352019-04-01 17:50:57 +0800961 args, NULL, syncobjs, false);
Dave Airlie5e60a102017-08-25 10:52:22 -0700962
Jason Ekstrand3e6fb722017-08-25 10:52:26 -0700963 drm_syncobj_array_free(syncobjs, args->count_handles);
Dave Airlie5e60a102017-08-25 10:52:22 -0700964
965 return ret;
966}
Jason Ekstrandaa4035d2017-08-28 14:10:27 -0700967
968int
Chunming Zhou01d6c352019-04-01 17:50:57 +0800969drm_syncobj_timeline_wait_ioctl(struct drm_device *dev, void *data,
970 struct drm_file *file_private)
971{
972 struct drm_syncobj_timeline_wait *args = data;
973 struct drm_syncobj **syncobjs;
974 int ret = 0;
975
976 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
977 return -ENODEV;
978
979 if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL |
980 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT |
981 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE))
982 return -EINVAL;
983
984 if (args->count_handles == 0)
985 return -EINVAL;
986
987 ret = drm_syncobj_array_find(file_private,
988 u64_to_user_ptr(args->handles),
989 args->count_handles,
990 &syncobjs);
991 if (ret < 0)
992 return ret;
993
994 ret = drm_syncobj_array_wait(dev, file_private,
995 NULL, args, syncobjs, true);
996
997 drm_syncobj_array_free(syncobjs, args->count_handles);
998
999 return ret;
1000}
1001
1002
1003int
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001004drm_syncobj_reset_ioctl(struct drm_device *dev, void *data,
1005 struct drm_file *file_private)
1006{
1007 struct drm_syncobj_array *args = data;
1008 struct drm_syncobj **syncobjs;
1009 uint32_t i;
1010 int ret;
1011
1012 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001013 return -EOPNOTSUPP;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001014
1015 if (args->pad != 0)
1016 return -EINVAL;
1017
1018 if (args->count_handles == 0)
1019 return -EINVAL;
1020
1021 ret = drm_syncobj_array_find(file_private,
1022 u64_to_user_ptr(args->handles),
1023 args->count_handles,
1024 &syncobjs);
1025 if (ret < 0)
1026 return ret;
1027
Eric Anholt131280a2018-11-08 08:04:22 -08001028 for (i = 0; i < args->count_handles; i++)
Christian König0b258ed2018-11-14 14:24:27 +01001029 drm_syncobj_replace_fence(syncobjs[i], NULL);
Eric Anholt131280a2018-11-08 08:04:22 -08001030
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001031 drm_syncobj_array_free(syncobjs, args->count_handles);
1032
Eric Anholt131280a2018-11-08 08:04:22 -08001033 return 0;
Jason Ekstrandaa4035d2017-08-28 14:10:27 -07001034}
Jason Ekstrandffa94432017-08-28 14:10:28 -07001035
1036int
1037drm_syncobj_signal_ioctl(struct drm_device *dev, void *data,
1038 struct drm_file *file_private)
1039{
1040 struct drm_syncobj_array *args = data;
1041 struct drm_syncobj **syncobjs;
1042 uint32_t i;
1043 int ret;
1044
1045 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
Chris Wilson69fdf422018-09-13 20:20:50 +01001046 return -EOPNOTSUPP;
Jason Ekstrandffa94432017-08-28 14:10:28 -07001047
1048 if (args->pad != 0)
1049 return -EINVAL;
1050
1051 if (args->count_handles == 0)
1052 return -EINVAL;
1053
1054 ret = drm_syncobj_array_find(file_private,
1055 u64_to_user_ptr(args->handles),
1056 args->count_handles,
1057 &syncobjs);
1058 if (ret < 0)
1059 return ret;
1060
Christian König86bbd892018-11-13 14:14:00 +01001061 for (i = 0; i < args->count_handles; i++)
1062 drm_syncobj_assign_null_handle(syncobjs[i]);
Jason Ekstrandffa94432017-08-28 14:10:28 -07001063
1064 drm_syncobj_array_free(syncobjs, args->count_handles);
1065
1066 return ret;
1067}
Chunming Zhou27b575a2019-04-01 17:50:58 +08001068
1069int drm_syncobj_query_ioctl(struct drm_device *dev, void *data,
1070 struct drm_file *file_private)
1071{
1072 struct drm_syncobj_timeline_array *args = data;
1073 struct drm_syncobj **syncobjs;
1074 uint64_t __user *points = u64_to_user_ptr(args->points);
1075 uint32_t i;
1076 int ret;
1077
1078 if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ))
1079 return -ENODEV;
1080
1081 if (args->pad != 0)
1082 return -EINVAL;
1083
1084 if (args->count_handles == 0)
1085 return -EINVAL;
1086
1087 ret = drm_syncobj_array_find(file_private,
1088 u64_to_user_ptr(args->handles),
1089 args->count_handles,
1090 &syncobjs);
1091 if (ret < 0)
1092 return ret;
1093
1094 for (i = 0; i < args->count_handles; i++) {
1095 struct dma_fence_chain *chain;
1096 struct dma_fence *fence;
1097 uint64_t point;
1098
1099 fence = drm_syncobj_fence_get(syncobjs[i]);
1100 chain = to_dma_fence_chain(fence);
1101 if (chain) {
1102 struct dma_fence *iter, *last_signaled = NULL;
1103
1104 dma_fence_chain_for_each(iter, fence) {
1105 if (!iter)
1106 break;
1107 dma_fence_put(last_signaled);
1108 last_signaled = dma_fence_get(iter);
1109 if (!to_dma_fence_chain(last_signaled)->prev_seqno)
1110 /* It is most likely that timeline has
1111 * unorder points. */
1112 break;
1113 }
1114 point = dma_fence_is_signaled(last_signaled) ?
1115 last_signaled->seqno :
1116 to_dma_fence_chain(last_signaled)->prev_seqno;
1117 dma_fence_put(last_signaled);
1118 } else {
1119 point = 0;
1120 }
1121 ret = copy_to_user(&points[i], &point, sizeof(uint64_t));
1122 ret = ret ? -EFAULT : 0;
1123 if (ret)
1124 break;
1125 }
1126 drm_syncobj_array_free(syncobjs, args->count_handles);
1127
1128 return ret;
1129}