]>
Commit | Line | Data |
---|---|---|
e9083420 DA |
1 | /* |
2 | * Copyright 2017 Red Hat | |
5e60a10e DA |
3 | * Parts ported from amdgpu (fence wait code). |
4 | * Copyright 2016 Advanced Micro Devices, Inc. | |
e9083420 DA |
5 | * |
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 | * | |
924fe8df DV |
32 | * 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. | |
e9083420 | 35 | * |
5e60a10e DA |
36 | * syncobj's can be waited upon, where it will wait for the underlying |
37 | * fence. | |
38 | * | |
e9083420 DA |
39 | * 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> | |
3ee45a3b | 53 | #include <linux/sync_file.h> |
e7aca503 | 54 | #include <linux/sched/signal.h> |
e9083420 DA |
55 | |
56 | #include "drm_internal.h" | |
57 | #include <drm/drm_syncobj.h> | |
58 | ||
e28bd101 CZ |
59 | struct drm_syncobj_stub_fence { |
60 | struct dma_fence base; | |
61 | spinlock_t lock; | |
62 | }; | |
63 | ||
64 | static const char *drm_syncobj_stub_fence_get_name(struct dma_fence *fence) | |
65 | { | |
66 | return "syncobjstub"; | |
67 | } | |
68 | ||
e28bd101 CZ |
69 | static const struct dma_fence_ops drm_syncobj_stub_fence_ops = { |
70 | .get_driver_name = drm_syncobj_stub_fence_get_name, | |
71 | .get_timeline_name = drm_syncobj_stub_fence_get_name, | |
e28bd101 CZ |
72 | }; |
73 | ||
74 | ||
e9083420 DA |
75 | /** |
76 | * drm_syncobj_find - lookup and reference a sync object. | |
77 | * @file_private: drm file private pointer | |
78 | * @handle: sync object handle to lookup. | |
79 | * | |
924fe8df DV |
80 | * Returns a reference to the syncobj pointed to by handle or NULL. The |
81 | * reference must be released by calling drm_syncobj_put(). | |
e9083420 DA |
82 | */ |
83 | struct drm_syncobj *drm_syncobj_find(struct drm_file *file_private, | |
84 | u32 handle) | |
85 | { | |
86 | struct drm_syncobj *syncobj; | |
87 | ||
88 | spin_lock(&file_private->syncobj_table_lock); | |
89 | ||
90 | /* Check if we currently have a reference on the object */ | |
91 | syncobj = idr_find(&file_private->syncobj_idr, handle); | |
92 | if (syncobj) | |
93 | drm_syncobj_get(syncobj); | |
94 | ||
95 | spin_unlock(&file_private->syncobj_table_lock); | |
96 | ||
97 | return syncobj; | |
98 | } | |
99 | EXPORT_SYMBOL(drm_syncobj_find); | |
100 | ||
9c19fb10 JE |
101 | static void drm_syncobj_add_callback_locked(struct drm_syncobj *syncobj, |
102 | struct drm_syncobj_cb *cb, | |
103 | drm_syncobj_func_t func) | |
104 | { | |
105 | cb->func = func; | |
106 | list_add_tail(&cb->node, &syncobj->cb_list); | |
107 | } | |
108 | ||
e7aca503 JE |
109 | static int drm_syncobj_fence_get_or_add_callback(struct drm_syncobj *syncobj, |
110 | struct dma_fence **fence, | |
111 | struct drm_syncobj_cb *cb, | |
112 | drm_syncobj_func_t func) | |
113 | { | |
114 | int ret; | |
115 | ||
116 | *fence = drm_syncobj_fence_get(syncobj); | |
117 | if (*fence) | |
118 | return 1; | |
119 | ||
120 | spin_lock(&syncobj->lock); | |
121 | /* We've already tried once to get a fence and failed. Now that we | |
122 | * have the lock, try one more time just to be sure we don't add a | |
123 | * callback when a fence has already been set. | |
124 | */ | |
125 | if (syncobj->fence) { | |
563eaf53 VS |
126 | *fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, |
127 | lockdep_is_held(&syncobj->lock))); | |
e7aca503 JE |
128 | ret = 1; |
129 | } else { | |
130 | *fence = NULL; | |
131 | drm_syncobj_add_callback_locked(syncobj, cb, func); | |
132 | ret = 0; | |
133 | } | |
134 | spin_unlock(&syncobj->lock); | |
135 | ||
136 | return ret; | |
137 | } | |
138 | ||
9c19fb10 JE |
139 | void drm_syncobj_add_callback(struct drm_syncobj *syncobj, |
140 | struct drm_syncobj_cb *cb, | |
141 | drm_syncobj_func_t func) | |
142 | { | |
143 | spin_lock(&syncobj->lock); | |
144 | drm_syncobj_add_callback_locked(syncobj, cb, func); | |
145 | spin_unlock(&syncobj->lock); | |
146 | } | |
9c19fb10 | 147 | |
9c19fb10 JE |
148 | void drm_syncobj_remove_callback(struct drm_syncobj *syncobj, |
149 | struct drm_syncobj_cb *cb) | |
150 | { | |
151 | spin_lock(&syncobj->lock); | |
152 | list_del_init(&cb->node); | |
153 | spin_unlock(&syncobj->lock); | |
154 | } | |
9c19fb10 | 155 | |
e9083420 DA |
156 | /** |
157 | * drm_syncobj_replace_fence - replace fence in a sync object. | |
e9083420 | 158 | * @syncobj: Sync object to replace fence in |
9a09a423 | 159 | * @point: timeline point |
e9083420 DA |
160 | * @fence: fence to install in sync file. |
161 | * | |
9a09a423 | 162 | * This replaces the fence on a sync object, or a timeline point fence. |
e9083420 | 163 | */ |
00fc2c26 | 164 | void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, |
9a09a423 | 165 | u64 point, |
e9083420 DA |
166 | struct dma_fence *fence) |
167 | { | |
00fc2c26 | 168 | struct dma_fence *old_fence; |
9c19fb10 | 169 | struct drm_syncobj_cb *cur, *tmp; |
e9083420 DA |
170 | |
171 | if (fence) | |
172 | dma_fence_get(fence); | |
9c19fb10 JE |
173 | |
174 | spin_lock(&syncobj->lock); | |
175 | ||
563eaf53 VS |
176 | old_fence = rcu_dereference_protected(syncobj->fence, |
177 | lockdep_is_held(&syncobj->lock)); | |
178 | rcu_assign_pointer(syncobj->fence, fence); | |
9c19fb10 JE |
179 | |
180 | if (fence != old_fence) { | |
181 | list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) { | |
182 | list_del_init(&cur->node); | |
183 | cur->func(syncobj, cur); | |
184 | } | |
185 | } | |
186 | ||
187 | spin_unlock(&syncobj->lock); | |
e9083420 DA |
188 | |
189 | dma_fence_put(old_fence); | |
190 | } | |
191 | EXPORT_SYMBOL(drm_syncobj_replace_fence); | |
192 | ||
1fc08218 JE |
193 | static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj) |
194 | { | |
e28bd101 | 195 | struct drm_syncobj_stub_fence *fence; |
1fc08218 JE |
196 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
197 | if (fence == NULL) | |
198 | return -ENOMEM; | |
199 | ||
200 | spin_lock_init(&fence->lock); | |
e28bd101 | 201 | dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops, |
1fc08218 JE |
202 | &fence->lock, 0, 0); |
203 | dma_fence_signal(&fence->base); | |
204 | ||
9a09a423 | 205 | drm_syncobj_replace_fence(syncobj, 0, &fence->base); |
1fc08218 JE |
206 | |
207 | dma_fence_put(&fence->base); | |
208 | ||
209 | return 0; | |
210 | } | |
211 | ||
924fe8df DV |
212 | /** |
213 | * drm_syncobj_find_fence - lookup and reference the fence in a sync object | |
214 | * @file_private: drm file private pointer | |
215 | * @handle: sync object handle to lookup. | |
0a6730ea | 216 | * @point: timeline point |
924fe8df DV |
217 | * @fence: out parameter for the fence |
218 | * | |
219 | * This is just a convenience function that combines drm_syncobj_find() and | |
220 | * drm_syncobj_fence_get(). | |
221 | * | |
222 | * Returns 0 on success or a negative error value on failure. On success @fence | |
223 | * contains a reference to the fence, which must be released by calling | |
224 | * dma_fence_put(). | |
225 | */ | |
afaf5923 | 226 | int drm_syncobj_find_fence(struct drm_file *file_private, |
0a6730ea | 227 | u32 handle, u64 point, |
afaf5923 | 228 | struct dma_fence **fence) |
e9083420 DA |
229 | { |
230 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); | |
231 | int ret = 0; | |
232 | ||
233 | if (!syncobj) | |
234 | return -ENOENT; | |
235 | ||
309a5482 | 236 | *fence = drm_syncobj_fence_get(syncobj); |
e9083420 DA |
237 | if (!*fence) { |
238 | ret = -EINVAL; | |
239 | } | |
240 | drm_syncobj_put(syncobj); | |
241 | return ret; | |
242 | } | |
afaf5923 | 243 | EXPORT_SYMBOL(drm_syncobj_find_fence); |
e9083420 DA |
244 | |
245 | /** | |
246 | * drm_syncobj_free - free a sync object. | |
247 | * @kref: kref to free. | |
248 | * | |
249 | * Only to be called from kref_put in drm_syncobj_put. | |
250 | */ | |
251 | void drm_syncobj_free(struct kref *kref) | |
252 | { | |
253 | struct drm_syncobj *syncobj = container_of(kref, | |
254 | struct drm_syncobj, | |
255 | refcount); | |
9a09a423 | 256 | drm_syncobj_replace_fence(syncobj, 0, NULL); |
e9083420 DA |
257 | kfree(syncobj); |
258 | } | |
259 | EXPORT_SYMBOL(drm_syncobj_free); | |
260 | ||
1321fd2c MO |
261 | /** |
262 | * drm_syncobj_create - create a new syncobj | |
263 | * @out_syncobj: returned syncobj | |
264 | * @flags: DRM_SYNCOBJ_* flags | |
265 | * @fence: if non-NULL, the syncobj will represent this fence | |
924fe8df DV |
266 | * |
267 | * This is the first function to create a sync object. After creating, drivers | |
268 | * probably want to make it available to userspace, either through | |
269 | * drm_syncobj_get_handle() or drm_syncobj_get_fd(). | |
270 | * | |
271 | * Returns 0 on success or a negative error value on failure. | |
1321fd2c MO |
272 | */ |
273 | int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags, | |
274 | struct dma_fence *fence) | |
e9083420 DA |
275 | { |
276 | int ret; | |
277 | struct drm_syncobj *syncobj; | |
278 | ||
279 | syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL); | |
280 | if (!syncobj) | |
281 | return -ENOMEM; | |
282 | ||
283 | kref_init(&syncobj->refcount); | |
9c19fb10 JE |
284 | INIT_LIST_HEAD(&syncobj->cb_list); |
285 | spin_lock_init(&syncobj->lock); | |
e9083420 | 286 | |
1fc08218 JE |
287 | if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) { |
288 | ret = drm_syncobj_assign_null_handle(syncobj); | |
289 | if (ret < 0) { | |
290 | drm_syncobj_put(syncobj); | |
291 | return ret; | |
292 | } | |
293 | } | |
294 | ||
1321fd2c | 295 | if (fence) |
9a09a423 | 296 | drm_syncobj_replace_fence(syncobj, 0, fence); |
1321fd2c MO |
297 | |
298 | *out_syncobj = syncobj; | |
299 | return 0; | |
300 | } | |
301 | EXPORT_SYMBOL(drm_syncobj_create); | |
302 | ||
303 | /** | |
304 | * drm_syncobj_get_handle - get a handle from a syncobj | |
924fe8df DV |
305 | * @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. | |
1321fd2c MO |
313 | */ |
314 | int 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 | ||
e9083420 DA |
322 | 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 | } | |
1321fd2c MO |
337 | EXPORT_SYMBOL(drm_syncobj_get_handle); |
338 | ||
339 | static 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 | } | |
e9083420 DA |
353 | |
354 | static 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 | ||
370 | static 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 | ||
378 | static const struct file_operations drm_syncobj_file_fops = { | |
379 | .release = drm_syncobj_file_release, | |
380 | }; | |
381 | ||
924fe8df DV |
382 | /** |
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 | */ | |
684fd0af | 391 | int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd) |
e9083420 | 392 | { |
e7cdf5c8 | 393 | struct file *file; |
e9083420 DA |
394 | int fd; |
395 | ||
e9083420 | 396 | fd = get_unused_fd_flags(O_CLOEXEC); |
684fd0af | 397 | if (fd < 0) |
e9083420 | 398 | return fd; |
e9083420 | 399 | |
e7cdf5c8 CW |
400 | 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); | |
e9083420 | 406 | } |
e7cdf5c8 CW |
407 | |
408 | drm_syncobj_get(syncobj); | |
409 | fd_install(fd, file); | |
410 | ||
e9083420 DA |
411 | *p_fd = fd; |
412 | return 0; | |
684fd0af MO |
413 | } |
414 | EXPORT_SYMBOL(drm_syncobj_get_fd); | |
415 | ||
416 | static 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; | |
421 | ||
422 | if (!syncobj) | |
423 | return -EINVAL; | |
424 | ||
425 | ret = drm_syncobj_get_fd(syncobj, p_fd); | |
e9083420 DA |
426 | drm_syncobj_put(syncobj); |
427 | return ret; | |
428 | } | |
429 | ||
e9083420 DA |
430 | static int drm_syncobj_fd_to_handle(struct drm_file *file_private, |
431 | int fd, u32 *handle) | |
432 | { | |
e7cdf5c8 CW |
433 | struct drm_syncobj *syncobj; |
434 | struct file *file; | |
e9083420 DA |
435 | int ret; |
436 | ||
e7cdf5c8 CW |
437 | file = fget(fd); |
438 | if (!file) | |
e9083420 DA |
439 | return -EINVAL; |
440 | ||
e7cdf5c8 CW |
441 | if (file->f_op != &drm_syncobj_file_fops) { |
442 | fput(file); | |
443 | return -EINVAL; | |
444 | } | |
445 | ||
e9083420 | 446 | /* take a reference to put in the idr */ |
e7cdf5c8 | 447 | syncobj = file->private_data; |
e9083420 DA |
448 | 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 | ||
e7cdf5c8 CW |
456 | if (ret > 0) { |
457 | *handle = ret; | |
458 | ret = 0; | |
459 | } else | |
460 | drm_syncobj_put(syncobj); | |
461 | ||
462 | fput(file); | |
463 | return ret; | |
e9083420 DA |
464 | } |
465 | ||
a32c94af VS |
466 | static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private, |
467 | int fd, int handle) | |
3ee45a3b DA |
468 | { |
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 | ||
9a09a423 | 481 | drm_syncobj_replace_fence(syncobj, 0, fence); |
3ee45a3b DA |
482 | dma_fence_put(fence); |
483 | drm_syncobj_put(syncobj); | |
484 | return 0; | |
485 | } | |
486 | ||
a32c94af VS |
487 | static int drm_syncobj_export_sync_file(struct drm_file *file_private, |
488 | int handle, int *p_fd) | |
3ee45a3b DA |
489 | { |
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 | ||
0a6730ea | 498 | ret = drm_syncobj_find_fence(file_private, handle, 0, &fence); |
3ee45a3b DA |
499 | 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; | |
515 | err_put_fd: | |
516 | put_unused_fd(fd); | |
517 | return ret; | |
518 | } | |
e9083420 DA |
519 | /** |
520 | * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time | |
e9083420 DA |
521 | * @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 | */ | |
526 | void | |
527 | drm_syncobj_open(struct drm_file *file_private) | |
528 | { | |
e86584c5 | 529 | idr_init_base(&file_private->syncobj_idr, 1); |
e9083420 DA |
530 | spin_lock_init(&file_private->syncobj_table_lock); |
531 | } | |
532 | ||
533 | static int | |
534 | drm_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 | |
e9083420 DA |
544 | * @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 | */ | |
550 | void | |
551 | drm_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 | ||
558 | int | |
559 | drm_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)) | |
69fdf420 | 565 | return -EOPNOTSUPP; |
e9083420 DA |
566 | |
567 | /* no valid flags yet */ | |
1fc08218 | 568 | if (args->flags & ~DRM_SYNCOBJ_CREATE_SIGNALED) |
e9083420 DA |
569 | return -EINVAL; |
570 | ||
1321fd2c MO |
571 | return drm_syncobj_create_as_handle(file_private, |
572 | &args->handle, args->flags); | |
e9083420 DA |
573 | } |
574 | ||
575 | int | |
576 | drm_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)) | |
69fdf420 | 582 | return -EOPNOTSUPP; |
e9083420 DA |
583 | |
584 | /* make sure padding is empty */ | |
585 | if (args->pad) | |
586 | return -EINVAL; | |
587 | return drm_syncobj_destroy(file_private, args->handle); | |
588 | } | |
589 | ||
590 | int | |
591 | drm_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)) | |
69fdf420 | 597 | return -EOPNOTSUPP; |
e9083420 | 598 | |
3ee45a3b | 599 | if (args->pad) |
e9083420 DA |
600 | return -EINVAL; |
601 | ||
3ee45a3b DA |
602 | 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 | ||
e9083420 DA |
610 | return drm_syncobj_handle_to_fd(file_private, args->handle, |
611 | &args->fd); | |
612 | } | |
613 | ||
614 | int | |
615 | drm_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)) | |
69fdf420 | 621 | return -EOPNOTSUPP; |
e9083420 | 622 | |
3ee45a3b DA |
623 | if (args->pad) |
624 | return -EINVAL; | |
625 | ||
626 | if (args->flags != 0 && | |
627 | args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) | |
e9083420 DA |
628 | return -EINVAL; |
629 | ||
3ee45a3b DA |
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 | ||
e9083420 DA |
635 | return drm_syncobj_fd_to_handle(file_private, args->fd, |
636 | &args->handle); | |
637 | } | |
5e60a10e | 638 | |
e7aca503 JE |
639 | struct syncobj_wait_entry { |
640 | struct task_struct *task; | |
641 | struct dma_fence *fence; | |
642 | struct dma_fence_cb fence_cb; | |
643 | struct drm_syncobj_cb syncobj_cb; | |
644 | }; | |
645 | ||
646 | static void syncobj_wait_fence_func(struct dma_fence *fence, | |
647 | struct dma_fence_cb *cb) | |
648 | { | |
649 | struct syncobj_wait_entry *wait = | |
650 | container_of(cb, struct syncobj_wait_entry, fence_cb); | |
651 | ||
652 | wake_up_process(wait->task); | |
653 | } | |
654 | ||
655 | static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, | |
656 | struct drm_syncobj_cb *cb) | |
657 | { | |
658 | struct syncobj_wait_entry *wait = | |
659 | container_of(cb, struct syncobj_wait_entry, syncobj_cb); | |
660 | ||
661 | /* This happens inside the syncobj lock */ | |
563eaf53 VS |
662 | wait->fence = dma_fence_get(rcu_dereference_protected(syncobj->fence, |
663 | lockdep_is_held(&syncobj->lock))); | |
e7aca503 JE |
664 | wake_up_process(wait->task); |
665 | } | |
666 | ||
667 | static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, | |
668 | uint32_t count, | |
669 | uint32_t flags, | |
670 | signed long timeout, | |
671 | uint32_t *idx) | |
672 | { | |
673 | struct syncobj_wait_entry *entries; | |
674 | struct dma_fence *fence; | |
e7aca503 JE |
675 | uint32_t signaled_count, i; |
676 | ||
677 | entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); | |
678 | if (!entries) | |
679 | return -ENOMEM; | |
680 | ||
681 | /* Walk the list of sync objects and initialize entries. We do | |
682 | * this up-front so that we can properly return -EINVAL if there is | |
683 | * a syncobj with a missing fence and then never have the chance of | |
684 | * returning -EINVAL again. | |
685 | */ | |
686 | signaled_count = 0; | |
687 | for (i = 0; i < count; ++i) { | |
688 | entries[i].task = current; | |
689 | entries[i].fence = drm_syncobj_fence_get(syncobjs[i]); | |
690 | if (!entries[i].fence) { | |
691 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { | |
692 | continue; | |
693 | } else { | |
12fec62a | 694 | timeout = -EINVAL; |
e7aca503 JE |
695 | goto cleanup_entries; |
696 | } | |
697 | } | |
698 | ||
699 | if (dma_fence_is_signaled(entries[i].fence)) { | |
700 | if (signaled_count == 0 && idx) | |
701 | *idx = i; | |
702 | signaled_count++; | |
703 | } | |
704 | } | |
705 | ||
e7aca503 JE |
706 | if (signaled_count == count || |
707 | (signaled_count > 0 && | |
708 | !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL))) | |
709 | goto cleanup_entries; | |
710 | ||
711 | /* There's a very annoying laxness in the dma_fence API here, in | |
712 | * that backends are not required to automatically report when a | |
713 | * fence is signaled prior to fence->ops->enable_signaling() being | |
714 | * called. So here if we fail to match signaled_count, we need to | |
715 | * fallthough and try a 0 timeout wait! | |
716 | */ | |
717 | ||
718 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { | |
719 | for (i = 0; i < count; ++i) { | |
720 | drm_syncobj_fence_get_or_add_callback(syncobjs[i], | |
721 | &entries[i].fence, | |
722 | &entries[i].syncobj_cb, | |
723 | syncobj_wait_syncobj_func); | |
724 | } | |
725 | } | |
726 | ||
727 | do { | |
728 | set_current_state(TASK_INTERRUPTIBLE); | |
729 | ||
730 | signaled_count = 0; | |
731 | for (i = 0; i < count; ++i) { | |
732 | fence = entries[i].fence; | |
733 | if (!fence) | |
734 | continue; | |
735 | ||
736 | if (dma_fence_is_signaled(fence) || | |
737 | (!entries[i].fence_cb.func && | |
738 | dma_fence_add_callback(fence, | |
739 | &entries[i].fence_cb, | |
740 | syncobj_wait_fence_func))) { | |
741 | /* The fence has been signaled */ | |
742 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) { | |
743 | signaled_count++; | |
744 | } else { | |
745 | if (idx) | |
746 | *idx = i; | |
747 | goto done_waiting; | |
748 | } | |
749 | } | |
750 | } | |
751 | ||
752 | if (signaled_count == count) | |
753 | goto done_waiting; | |
754 | ||
755 | if (timeout == 0) { | |
12fec62a | 756 | timeout = -ETIME; |
e7aca503 JE |
757 | goto done_waiting; |
758 | } | |
759 | ||
12fec62a CW |
760 | if (signal_pending(current)) { |
761 | timeout = -ERESTARTSYS; | |
762 | goto done_waiting; | |
763 | } | |
e7aca503 | 764 | |
12fec62a CW |
765 | timeout = schedule_timeout(timeout); |
766 | } while (1); | |
e7aca503 JE |
767 | |
768 | done_waiting: | |
769 | __set_current_state(TASK_RUNNING); | |
770 | ||
771 | cleanup_entries: | |
772 | for (i = 0; i < count; ++i) { | |
773 | if (entries[i].syncobj_cb.func) | |
774 | drm_syncobj_remove_callback(syncobjs[i], | |
775 | &entries[i].syncobj_cb); | |
776 | if (entries[i].fence_cb.func) | |
777 | dma_fence_remove_callback(entries[i].fence, | |
778 | &entries[i].fence_cb); | |
779 | dma_fence_put(entries[i].fence); | |
780 | } | |
781 | kfree(entries); | |
782 | ||
12fec62a | 783 | return timeout; |
e7aca503 JE |
784 | } |
785 | ||
5e60a10e DA |
786 | /** |
787 | * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value | |
788 | * | |
789 | * @timeout_nsec: timeout nsec component in ns, 0 for poll | |
790 | * | |
791 | * Calculate the timeout in jiffies from an absolute time in sec/nsec. | |
792 | */ | |
793 | static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec) | |
794 | { | |
795 | ktime_t abs_timeout, now; | |
796 | u64 timeout_ns, timeout_jiffies64; | |
797 | ||
798 | /* make 0 timeout means poll - absolute 0 doesn't seem valid */ | |
799 | if (timeout_nsec == 0) | |
800 | return 0; | |
801 | ||
802 | abs_timeout = ns_to_ktime(timeout_nsec); | |
803 | now = ktime_get(); | |
804 | ||
805 | if (!ktime_after(abs_timeout, now)) | |
806 | return 0; | |
807 | ||
808 | timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now)); | |
809 | ||
810 | timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns); | |
811 | /* clamp timeout to avoid infinite timeout */ | |
812 | if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1) | |
813 | return MAX_SCHEDULE_TIMEOUT - 1; | |
814 | ||
815 | return timeout_jiffies64 + 1; | |
816 | } | |
817 | ||
e7aca503 JE |
818 | static int drm_syncobj_array_wait(struct drm_device *dev, |
819 | struct drm_file *file_private, | |
820 | struct drm_syncobj_wait *wait, | |
821 | struct drm_syncobj **syncobjs) | |
5e60a10e DA |
822 | { |
823 | signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec); | |
5e60a10e DA |
824 | uint32_t first = ~0; |
825 | ||
12fec62a CW |
826 | timeout = drm_syncobj_array_wait_timeout(syncobjs, |
827 | wait->count_handles, | |
828 | wait->flags, | |
829 | timeout, &first); | |
830 | if (timeout < 0) | |
831 | return timeout; | |
5e60a10e DA |
832 | |
833 | wait->first_signaled = first; | |
5e60a10e DA |
834 | return 0; |
835 | } | |
836 | ||
3e6fb72d | 837 | static int drm_syncobj_array_find(struct drm_file *file_private, |
9e554462 VS |
838 | void __user *user_handles, |
839 | uint32_t count_handles, | |
3e6fb72d | 840 | struct drm_syncobj ***syncobjs_out) |
5e60a10e | 841 | { |
3e6fb72d | 842 | uint32_t i, *handles; |
e7aca503 | 843 | struct drm_syncobj **syncobjs; |
3e6fb72d | 844 | int ret; |
5e60a10e | 845 | |
3e6fb72d | 846 | handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); |
5e60a10e DA |
847 | if (handles == NULL) |
848 | return -ENOMEM; | |
849 | ||
3e6fb72d JE |
850 | if (copy_from_user(handles, user_handles, |
851 | sizeof(uint32_t) * count_handles)) { | |
5e60a10e DA |
852 | ret = -EFAULT; |
853 | goto err_free_handles; | |
854 | } | |
855 | ||
3e6fb72d JE |
856 | syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); |
857 | if (syncobjs == NULL) { | |
5e60a10e DA |
858 | ret = -ENOMEM; |
859 | goto err_free_handles; | |
860 | } | |
861 | ||
3e6fb72d | 862 | for (i = 0; i < count_handles; i++) { |
e7aca503 JE |
863 | syncobjs[i] = drm_syncobj_find(file_private, handles[i]); |
864 | if (!syncobjs[i]) { | |
865 | ret = -ENOENT; | |
3e6fb72d | 866 | goto err_put_syncobjs; |
e7aca503 | 867 | } |
5e60a10e DA |
868 | } |
869 | ||
3e6fb72d JE |
870 | kfree(handles); |
871 | *syncobjs_out = syncobjs; | |
872 | return 0; | |
5e60a10e | 873 | |
3e6fb72d | 874 | err_put_syncobjs: |
e7aca503 JE |
875 | while (i-- > 0) |
876 | drm_syncobj_put(syncobjs[i]); | |
877 | kfree(syncobjs); | |
5e60a10e DA |
878 | err_free_handles: |
879 | kfree(handles); | |
880 | ||
881 | return ret; | |
882 | } | |
3e6fb72d JE |
883 | |
884 | static void drm_syncobj_array_free(struct drm_syncobj **syncobjs, | |
885 | uint32_t count) | |
886 | { | |
887 | uint32_t i; | |
888 | for (i = 0; i < count; i++) | |
889 | drm_syncobj_put(syncobjs[i]); | |
890 | kfree(syncobjs); | |
891 | } | |
892 | ||
893 | int | |
894 | drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, | |
895 | struct drm_file *file_private) | |
896 | { | |
897 | struct drm_syncobj_wait *args = data; | |
898 | struct drm_syncobj **syncobjs; | |
899 | int ret = 0; | |
900 | ||
901 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 902 | return -EOPNOTSUPP; |
3e6fb72d JE |
903 | |
904 | if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | | |
905 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) | |
906 | return -EINVAL; | |
907 | ||
908 | if (args->count_handles == 0) | |
909 | return -EINVAL; | |
910 | ||
911 | ret = drm_syncobj_array_find(file_private, | |
912 | u64_to_user_ptr(args->handles), | |
913 | args->count_handles, | |
914 | &syncobjs); | |
915 | if (ret < 0) | |
916 | return ret; | |
917 | ||
918 | ret = drm_syncobj_array_wait(dev, file_private, | |
919 | args, syncobjs); | |
920 | ||
921 | drm_syncobj_array_free(syncobjs, args->count_handles); | |
922 | ||
923 | return ret; | |
924 | } | |
aa4035d2 JE |
925 | |
926 | int | |
927 | drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, | |
928 | struct drm_file *file_private) | |
929 | { | |
930 | struct drm_syncobj_array *args = data; | |
931 | struct drm_syncobj **syncobjs; | |
932 | uint32_t i; | |
933 | int ret; | |
934 | ||
935 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 936 | return -EOPNOTSUPP; |
aa4035d2 JE |
937 | |
938 | if (args->pad != 0) | |
939 | return -EINVAL; | |
940 | ||
941 | if (args->count_handles == 0) | |
942 | return -EINVAL; | |
943 | ||
944 | ret = drm_syncobj_array_find(file_private, | |
945 | u64_to_user_ptr(args->handles), | |
946 | args->count_handles, | |
947 | &syncobjs); | |
948 | if (ret < 0) | |
949 | return ret; | |
950 | ||
951 | for (i = 0; i < args->count_handles; i++) | |
9a09a423 | 952 | drm_syncobj_replace_fence(syncobjs[i], 0, NULL); |
aa4035d2 JE |
953 | |
954 | drm_syncobj_array_free(syncobjs, args->count_handles); | |
955 | ||
956 | return 0; | |
957 | } | |
ffa9443f JE |
958 | |
959 | int | |
960 | drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, | |
961 | struct drm_file *file_private) | |
962 | { | |
963 | struct drm_syncobj_array *args = data; | |
964 | struct drm_syncobj **syncobjs; | |
965 | uint32_t i; | |
966 | int ret; | |
967 | ||
968 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 969 | return -EOPNOTSUPP; |
ffa9443f JE |
970 | |
971 | if (args->pad != 0) | |
972 | return -EINVAL; | |
973 | ||
974 | if (args->count_handles == 0) | |
975 | return -EINVAL; | |
976 | ||
977 | ret = drm_syncobj_array_find(file_private, | |
978 | u64_to_user_ptr(args->handles), | |
979 | args->count_handles, | |
980 | &syncobjs); | |
981 | if (ret < 0) | |
982 | return ret; | |
983 | ||
984 | for (i = 0; i < args->count_handles; i++) { | |
985 | ret = drm_syncobj_assign_null_handle(syncobjs[i]); | |
986 | if (ret < 0) | |
987 | break; | |
988 | } | |
989 | ||
990 | drm_syncobj_array_free(syncobjs, args->count_handles); | |
991 | ||
992 | return ret; | |
993 | } |