]>
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 | ||
48197bc5 CZ |
59 | /* merge normal syncobj to timeline syncobj, the point interval is 1 */ |
60 | #define DRM_SYNCOBJ_BINARY_POINT 1 | |
61 | ||
e28bd101 CZ |
62 | struct drm_syncobj_stub_fence { |
63 | struct dma_fence base; | |
64 | spinlock_t lock; | |
65 | }; | |
66 | ||
67 | static const char *drm_syncobj_stub_fence_get_name(struct dma_fence *fence) | |
68 | { | |
69 | return "syncobjstub"; | |
70 | } | |
71 | ||
e28bd101 CZ |
72 | static 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, | |
e28bd101 CZ |
75 | }; |
76 | ||
48197bc5 CZ |
77 | struct drm_syncobj_signal_pt { |
78 | struct dma_fence_array *fence_array; | |
79 | u64 value; | |
80 | struct list_head list; | |
81 | }; | |
e28bd101 | 82 | |
4fb2c933 CZ |
83 | static DEFINE_SPINLOCK(signaled_fence_lock); |
84 | static struct dma_fence signaled_fence; | |
85 | ||
86 | static 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 | } | |
e9083420 DA |
100 | /** |
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 | * | |
924fe8df DV |
105 | * Returns a reference to the syncobj pointed to by handle or NULL. The |
106 | * reference must be released by calling drm_syncobj_put(). | |
e9083420 DA |
107 | */ |
108 | struct 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 | } | |
124 | EXPORT_SYMBOL(drm_syncobj_find); | |
125 | ||
9cbe67c5 CW |
126 | static struct dma_fence * |
127 | drm_syncobj_find_signal_pt_for_point(struct drm_syncobj *syncobj, | |
128 | uint64_t point) | |
43cf1fc0 CZ |
129 | { |
130 | struct drm_syncobj_signal_pt *signal_pt; | |
131 | ||
132 | if ((syncobj->type == DRM_SYNCOBJ_TYPE_TIMELINE) && | |
4fb2c933 CZ |
133 | (point <= syncobj->timeline)) |
134 | return drm_syncobj_get_stub_fence(); | |
43cf1fc0 CZ |
135 | |
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 | ||
9c19fb10 FE |
147 | static 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 | ||
43cf1fc0 CZ |
155 | static 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) | |
e7aca503 | 159 | { |
43cf1fc0 | 160 | u64 pt_value = 0; |
e7aca503 | 161 | |
337fe9f5 FE |
162 | WARN_ON(*fence); |
163 | ||
43cf1fc0 CZ |
164 | 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; | |
e7aca503 | 170 | } |
e7aca503 | 171 | |
43cf1fc0 CZ |
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); | |
e7aca503 FE |
179 | } |
180 | ||
9cbe67c5 CW |
181 | static void drm_syncobj_remove_callback(struct drm_syncobj *syncobj, |
182 | struct drm_syncobj_cb *cb) | |
9c19fb10 | 183 | { |
43cf1fc0 | 184 | mutex_lock(&syncobj->cb_mutex); |
9c19fb10 | 185 | list_del_init(&cb->node); |
43cf1fc0 | 186 | mutex_unlock(&syncobj->cb_mutex); |
9c19fb10 | 187 | } |
9c19fb10 | 188 | |
48197bc5 CZ |
189 | static void drm_syncobj_init(struct drm_syncobj *syncobj) |
190 | { | |
43cf1fc0 | 191 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
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); | |
43cf1fc0 | 198 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
199 | } |
200 | ||
201 | static void drm_syncobj_fini(struct drm_syncobj *syncobj) | |
202 | { | |
203 | struct drm_syncobj_signal_pt *signal_pt = NULL, *tmp; | |
204 | ||
43cf1fc0 | 205 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
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 | } | |
43cf1fc0 | 212 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
213 | } |
214 | ||
215 | static 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) { | |
43cf1fc0 | 239 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
240 | 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 | } | |
43cf1fc0 | 246 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
247 | } |
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 | ||
43cf1fc0 | 256 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
257 | if (syncobj->signal_point >= point) { |
258 | DRM_WARN("A later signal is ready!"); | |
43cf1fc0 | 259 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
260 | goto exist; |
261 | } | |
262 | signal_pt->value = point; | |
263 | list_add_tail(&signal_pt->list, &syncobj->signal_pt_list); | |
264 | syncobj->signal_point = point; | |
43cf1fc0 | 265 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
266 | wake_up_all(&syncobj->wq); |
267 | ||
268 | return 0; | |
269 | exist: | |
270 | dma_fence_put(&signal_pt->fence_array->base); | |
271 | fail: | |
272 | for (i = 0; i < num_fences; i++) | |
273 | dma_fence_put(fences[i]); | |
274 | kfree(fences); | |
275 | out: | |
276 | kfree(signal_pt); | |
277 | return ret; | |
278 | } | |
279 | ||
280 | static void drm_syncobj_garbage_collection(struct drm_syncobj *syncobj) | |
281 | { | |
282 | struct drm_syncobj_signal_pt *signal_pt, *tmp, *tail_pt; | |
283 | ||
43cf1fc0 | 284 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
285 | 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 | ||
43cf1fc0 | 305 | spin_unlock(&syncobj->pt_lock); |
48197bc5 | 306 | } |
e9083420 DA |
307 | /** |
308 | * drm_syncobj_replace_fence - replace fence in a sync object. | |
e9083420 | 309 | * @syncobj: Sync object to replace fence in |
9a09a423 | 310 | * @point: timeline point |
e9083420 DA |
311 | * @fence: fence to install in sync file. |
312 | * | |
9a09a423 | 313 | * This replaces the fence on a sync object, or a timeline point fence. |
e9083420 | 314 | */ |
00fc2c26 | 315 | void drm_syncobj_replace_fence(struct drm_syncobj *syncobj, |
9a09a423 | 316 | u64 point, |
e9083420 DA |
317 | struct dma_fence *fence) |
318 | { | |
48197bc5 CZ |
319 | u64 pt_value = point; |
320 | ||
321 | 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; | |
43cf1fc0 | 334 | LIST_HEAD(cb_list); |
9c19fb10 | 335 | |
43cf1fc0 | 336 | mutex_lock(&syncobj->cb_mutex); |
9c19fb10 FE |
337 | list_for_each_entry_safe(cur, tmp, &syncobj->cb_list, node) { |
338 | list_del_init(&cur->node); | |
339 | cur->func(syncobj, cur); | |
340 | } | |
43cf1fc0 | 341 | mutex_unlock(&syncobj->cb_mutex); |
9c19fb10 | 342 | } |
e9083420 DA |
343 | } |
344 | EXPORT_SYMBOL(drm_syncobj_replace_fence); | |
345 | ||
1fc08218 FE |
346 | static int drm_syncobj_assign_null_handle(struct drm_syncobj *syncobj) |
347 | { | |
e28bd101 | 348 | struct drm_syncobj_stub_fence *fence; |
1fc08218 FE |
349 | fence = kzalloc(sizeof(*fence), GFP_KERNEL); |
350 | if (fence == NULL) | |
351 | return -ENOMEM; | |
352 | ||
353 | spin_lock_init(&fence->lock); | |
e28bd101 | 354 | dma_fence_init(&fence->base, &drm_syncobj_stub_fence_ops, |
1fc08218 FE |
355 | &fence->lock, 0, 0); |
356 | dma_fence_signal(&fence->base); | |
357 | ||
9a09a423 | 358 | drm_syncobj_replace_fence(syncobj, 0, &fence->base); |
1fc08218 FE |
359 | |
360 | dma_fence_put(&fence->base); | |
361 | ||
362 | return 0; | |
363 | } | |
364 | ||
48197bc5 CZ |
365 | static int |
366 | drm_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 | } | |
43cf1fc0 | 377 | spin_lock(&syncobj->pt_lock); |
48197bc5 CZ |
378 | *fence = drm_syncobj_find_signal_pt_for_point(syncobj, point); |
379 | if (!*fence) | |
380 | ret = -EINVAL; | |
43cf1fc0 | 381 | spin_unlock(&syncobj->pt_lock); |
48197bc5 CZ |
382 | 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 | */ | |
403 | int 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 | } | |
421 | EXPORT_SYMBOL(drm_syncobj_search_fence); | |
422 | ||
924fe8df DV |
423 | /** |
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. | |
0a6730ea | 427 | * @point: timeline point |
871edc96 | 428 | * @flags: DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT or not |
924fe8df DV |
429 | * @fence: out parameter for the fence |
430 | * | |
431 | * This is just a convenience function that combines drm_syncobj_find() and | |
48197bc5 | 432 | * drm_syncobj_lookup_fence(). |
924fe8df DV |
433 | * |
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 | */ | |
afaf5923 | 438 | int drm_syncobj_find_fence(struct drm_file *file_private, |
649fdce2 | 439 | u32 handle, u64 point, u64 flags, |
afaf5923 | 440 | struct dma_fence **fence) |
e9083420 DA |
441 | { |
442 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); | |
48197bc5 | 443 | int ret; |
e9083420 | 444 | |
48197bc5 | 445 | ret = drm_syncobj_search_fence(syncobj, point, flags, fence); |
aecbde63 EA |
446 | if (syncobj) |
447 | drm_syncobj_put(syncobj); | |
e9083420 DA |
448 | return ret; |
449 | } | |
afaf5923 | 450 | EXPORT_SYMBOL(drm_syncobj_find_fence); |
e9083420 DA |
451 | |
452 | /** | |
453 | * drm_syncobj_free - free a sync object. | |
454 | * @kref: kref to free. | |
455 | * | |
456 | * Only to be called from kref_put in drm_syncobj_put. | |
457 | */ | |
458 | void drm_syncobj_free(struct kref *kref) | |
459 | { | |
460 | struct drm_syncobj *syncobj = container_of(kref, | |
461 | struct drm_syncobj, | |
462 | refcount); | |
48197bc5 | 463 | drm_syncobj_fini(syncobj); |
e9083420 DA |
464 | kfree(syncobj); |
465 | } | |
466 | EXPORT_SYMBOL(drm_syncobj_free); | |
467 | ||
1321fd2c MO |
468 | /** |
469 | * drm_syncobj_create - create a new syncobj | |
470 | * @out_syncobj: returned syncobj | |
471 | * @flags: DRM_SYNCOBJ_* flags | |
472 | * @fence: if non-NULL, the syncobj will represent this fence | |
924fe8df DV |
473 | * |
474 | * This is the first function to create a sync object. After creating, drivers | |
475 | * probably want to make it available to userspace, either through | |
476 | * drm_syncobj_get_handle() or drm_syncobj_get_fd(). | |
477 | * | |
478 | * Returns 0 on success or a negative error value on failure. | |
1321fd2c MO |
479 | */ |
480 | int drm_syncobj_create(struct drm_syncobj **out_syncobj, uint32_t flags, | |
481 | struct dma_fence *fence) | |
e9083420 DA |
482 | { |
483 | int ret; | |
484 | struct drm_syncobj *syncobj; | |
485 | ||
783195ec CK |
486 | /* Disabled for now */ |
487 | if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE) | |
488 | return -EINVAL; | |
489 | ||
e9083420 DA |
490 | syncobj = kzalloc(sizeof(struct drm_syncobj), GFP_KERNEL); |
491 | if (!syncobj) | |
492 | return -ENOMEM; | |
493 | ||
494 | kref_init(&syncobj->refcount); | |
9c19fb10 | 495 | INIT_LIST_HEAD(&syncobj->cb_list); |
43cf1fc0 CZ |
496 | spin_lock_init(&syncobj->pt_lock); |
497 | mutex_init(&syncobj->cb_mutex); | |
48197bc5 CZ |
498 | if (flags & DRM_SYNCOBJ_CREATE_TYPE_TIMELINE) |
499 | syncobj->type = DRM_SYNCOBJ_TYPE_TIMELINE; | |
500 | else | |
501 | syncobj->type = DRM_SYNCOBJ_TYPE_BINARY; | |
502 | drm_syncobj_init(syncobj); | |
e9083420 | 503 | |
1fc08218 FE |
504 | if (flags & DRM_SYNCOBJ_CREATE_SIGNALED) { |
505 | ret = drm_syncobj_assign_null_handle(syncobj); | |
506 | if (ret < 0) { | |
507 | drm_syncobj_put(syncobj); | |
508 | return ret; | |
509 | } | |
510 | } | |
511 | ||
1321fd2c | 512 | if (fence) |
9a09a423 | 513 | drm_syncobj_replace_fence(syncobj, 0, fence); |
1321fd2c MO |
514 | |
515 | *out_syncobj = syncobj; | |
516 | return 0; | |
517 | } | |
518 | EXPORT_SYMBOL(drm_syncobj_create); | |
519 | ||
520 | /** | |
521 | * drm_syncobj_get_handle - get a handle from a syncobj | |
924fe8df DV |
522 | * @file_private: drm file private pointer |
523 | * @syncobj: Sync object to export | |
524 | * @handle: out parameter with the new handle | |
525 | * | |
526 | * Exports a sync object created with drm_syncobj_create() as a handle on | |
527 | * @file_private to userspace. | |
528 | * | |
529 | * Returns 0 on success or a negative error value on failure. | |
1321fd2c MO |
530 | */ |
531 | int drm_syncobj_get_handle(struct drm_file *file_private, | |
532 | struct drm_syncobj *syncobj, u32 *handle) | |
533 | { | |
534 | int ret; | |
535 | ||
536 | /* take a reference to put in the idr */ | |
537 | drm_syncobj_get(syncobj); | |
538 | ||
e9083420 DA |
539 | idr_preload(GFP_KERNEL); |
540 | spin_lock(&file_private->syncobj_table_lock); | |
541 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); | |
542 | spin_unlock(&file_private->syncobj_table_lock); | |
543 | ||
544 | idr_preload_end(); | |
545 | ||
546 | if (ret < 0) { | |
547 | drm_syncobj_put(syncobj); | |
548 | return ret; | |
549 | } | |
550 | ||
551 | *handle = ret; | |
552 | return 0; | |
553 | } | |
1321fd2c MO |
554 | EXPORT_SYMBOL(drm_syncobj_get_handle); |
555 | ||
556 | static int drm_syncobj_create_as_handle(struct drm_file *file_private, | |
557 | u32 *handle, uint32_t flags) | |
558 | { | |
559 | int ret; | |
560 | struct drm_syncobj *syncobj; | |
561 | ||
562 | ret = drm_syncobj_create(&syncobj, flags, NULL); | |
563 | if (ret) | |
564 | return ret; | |
565 | ||
566 | ret = drm_syncobj_get_handle(file_private, syncobj, handle); | |
567 | drm_syncobj_put(syncobj); | |
568 | return ret; | |
569 | } | |
e9083420 DA |
570 | |
571 | static int drm_syncobj_destroy(struct drm_file *file_private, | |
572 | u32 handle) | |
573 | { | |
574 | struct drm_syncobj *syncobj; | |
575 | ||
576 | spin_lock(&file_private->syncobj_table_lock); | |
577 | syncobj = idr_remove(&file_private->syncobj_idr, handle); | |
578 | spin_unlock(&file_private->syncobj_table_lock); | |
579 | ||
580 | if (!syncobj) | |
581 | return -EINVAL; | |
582 | ||
583 | drm_syncobj_put(syncobj); | |
584 | return 0; | |
585 | } | |
586 | ||
587 | static int drm_syncobj_file_release(struct inode *inode, struct file *file) | |
588 | { | |
589 | struct drm_syncobj *syncobj = file->private_data; | |
590 | ||
591 | drm_syncobj_put(syncobj); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | static const struct file_operations drm_syncobj_file_fops = { | |
596 | .release = drm_syncobj_file_release, | |
597 | }; | |
598 | ||
924fe8df DV |
599 | /** |
600 | * drm_syncobj_get_fd - get a file descriptor from a syncobj | |
601 | * @syncobj: Sync object to export | |
602 | * @p_fd: out parameter with the new file descriptor | |
603 | * | |
604 | * Exports a sync object created with drm_syncobj_create() as a file descriptor. | |
605 | * | |
606 | * Returns 0 on success or a negative error value on failure. | |
607 | */ | |
684fd0af | 608 | int drm_syncobj_get_fd(struct drm_syncobj *syncobj, int *p_fd) |
e9083420 | 609 | { |
e7cdf5c8 | 610 | struct file *file; |
e9083420 DA |
611 | int fd; |
612 | ||
e9083420 | 613 | fd = get_unused_fd_flags(O_CLOEXEC); |
684fd0af | 614 | if (fd < 0) |
e9083420 | 615 | return fd; |
e9083420 | 616 | |
e7cdf5c8 CW |
617 | file = anon_inode_getfile("syncobj_file", |
618 | &drm_syncobj_file_fops, | |
619 | syncobj, 0); | |
620 | if (IS_ERR(file)) { | |
621 | put_unused_fd(fd); | |
622 | return PTR_ERR(file); | |
e9083420 | 623 | } |
e7cdf5c8 CW |
624 | |
625 | drm_syncobj_get(syncobj); | |
626 | fd_install(fd, file); | |
627 | ||
e9083420 DA |
628 | *p_fd = fd; |
629 | return 0; | |
684fd0af MO |
630 | } |
631 | EXPORT_SYMBOL(drm_syncobj_get_fd); | |
632 | ||
633 | static int drm_syncobj_handle_to_fd(struct drm_file *file_private, | |
634 | u32 handle, int *p_fd) | |
635 | { | |
636 | struct drm_syncobj *syncobj = drm_syncobj_find(file_private, handle); | |
637 | int ret; | |
638 | ||
639 | if (!syncobj) | |
640 | return -EINVAL; | |
641 | ||
642 | ret = drm_syncobj_get_fd(syncobj, p_fd); | |
e9083420 DA |
643 | drm_syncobj_put(syncobj); |
644 | return ret; | |
645 | } | |
646 | ||
e9083420 DA |
647 | static int drm_syncobj_fd_to_handle(struct drm_file *file_private, |
648 | int fd, u32 *handle) | |
649 | { | |
e7cdf5c8 CW |
650 | struct drm_syncobj *syncobj; |
651 | struct file *file; | |
e9083420 DA |
652 | int ret; |
653 | ||
e7cdf5c8 CW |
654 | file = fget(fd); |
655 | if (!file) | |
e9083420 DA |
656 | return -EINVAL; |
657 | ||
e7cdf5c8 CW |
658 | if (file->f_op != &drm_syncobj_file_fops) { |
659 | fput(file); | |
660 | return -EINVAL; | |
661 | } | |
662 | ||
e9083420 | 663 | /* take a reference to put in the idr */ |
e7cdf5c8 | 664 | syncobj = file->private_data; |
e9083420 DA |
665 | drm_syncobj_get(syncobj); |
666 | ||
667 | idr_preload(GFP_KERNEL); | |
668 | spin_lock(&file_private->syncobj_table_lock); | |
669 | ret = idr_alloc(&file_private->syncobj_idr, syncobj, 1, 0, GFP_NOWAIT); | |
670 | spin_unlock(&file_private->syncobj_table_lock); | |
671 | idr_preload_end(); | |
672 | ||
e7cdf5c8 CW |
673 | if (ret > 0) { |
674 | *handle = ret; | |
675 | ret = 0; | |
676 | } else | |
677 | drm_syncobj_put(syncobj); | |
678 | ||
679 | fput(file); | |
680 | return ret; | |
e9083420 DA |
681 | } |
682 | ||
a32c94af VS |
683 | static int drm_syncobj_import_sync_file_fence(struct drm_file *file_private, |
684 | int fd, int handle) | |
3ee45a3b DA |
685 | { |
686 | struct dma_fence *fence = sync_file_get_fence(fd); | |
687 | struct drm_syncobj *syncobj; | |
688 | ||
689 | if (!fence) | |
690 | return -EINVAL; | |
691 | ||
692 | syncobj = drm_syncobj_find(file_private, handle); | |
693 | if (!syncobj) { | |
694 | dma_fence_put(fence); | |
695 | return -ENOENT; | |
696 | } | |
697 | ||
9a09a423 | 698 | drm_syncobj_replace_fence(syncobj, 0, fence); |
3ee45a3b DA |
699 | dma_fence_put(fence); |
700 | drm_syncobj_put(syncobj); | |
701 | return 0; | |
702 | } | |
703 | ||
a32c94af VS |
704 | static int drm_syncobj_export_sync_file(struct drm_file *file_private, |
705 | int handle, int *p_fd) | |
3ee45a3b DA |
706 | { |
707 | int ret; | |
708 | struct dma_fence *fence; | |
709 | struct sync_file *sync_file; | |
710 | int fd = get_unused_fd_flags(O_CLOEXEC); | |
711 | ||
712 | if (fd < 0) | |
713 | return fd; | |
714 | ||
649fdce2 | 715 | ret = drm_syncobj_find_fence(file_private, handle, 0, 0, &fence); |
3ee45a3b DA |
716 | if (ret) |
717 | goto err_put_fd; | |
718 | ||
719 | sync_file = sync_file_create(fence); | |
720 | ||
721 | dma_fence_put(fence); | |
722 | ||
723 | if (!sync_file) { | |
724 | ret = -EINVAL; | |
725 | goto err_put_fd; | |
726 | } | |
727 | ||
728 | fd_install(fd, sync_file->file); | |
729 | ||
730 | *p_fd = fd; | |
731 | return 0; | |
732 | err_put_fd: | |
733 | put_unused_fd(fd); | |
734 | return ret; | |
735 | } | |
e9083420 DA |
736 | /** |
737 | * drm_syncobj_open - initalizes syncobj file-private structures at devnode open time | |
e9083420 DA |
738 | * @file_private: drm file-private structure to set up |
739 | * | |
740 | * Called at device open time, sets up the structure for handling refcounting | |
741 | * of sync objects. | |
742 | */ | |
743 | void | |
744 | drm_syncobj_open(struct drm_file *file_private) | |
745 | { | |
e86584c5 | 746 | idr_init_base(&file_private->syncobj_idr, 1); |
e9083420 DA |
747 | spin_lock_init(&file_private->syncobj_table_lock); |
748 | } | |
749 | ||
750 | static int | |
751 | drm_syncobj_release_handle(int id, void *ptr, void *data) | |
752 | { | |
753 | struct drm_syncobj *syncobj = ptr; | |
754 | ||
755 | drm_syncobj_put(syncobj); | |
756 | return 0; | |
757 | } | |
758 | ||
759 | /** | |
760 | * drm_syncobj_release - release file-private sync object resources | |
e9083420 DA |
761 | * @file_private: drm file-private structure to clean up |
762 | * | |
763 | * Called at close time when the filp is going away. | |
764 | * | |
765 | * Releases any remaining references on objects by this filp. | |
766 | */ | |
767 | void | |
768 | drm_syncobj_release(struct drm_file *file_private) | |
769 | { | |
770 | idr_for_each(&file_private->syncobj_idr, | |
771 | &drm_syncobj_release_handle, file_private); | |
772 | idr_destroy(&file_private->syncobj_idr); | |
773 | } | |
774 | ||
775 | int | |
776 | drm_syncobj_create_ioctl(struct drm_device *dev, void *data, | |
777 | struct drm_file *file_private) | |
778 | { | |
779 | struct drm_syncobj_create *args = data; | |
780 | ||
781 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 782 | return -EOPNOTSUPP; |
e9083420 DA |
783 | |
784 | /* no valid flags yet */ | |
48197bc5 CZ |
785 | if (args->flags & ~(DRM_SYNCOBJ_CREATE_SIGNALED | |
786 | DRM_SYNCOBJ_CREATE_TYPE_TIMELINE)) | |
e9083420 DA |
787 | return -EINVAL; |
788 | ||
1321fd2c MO |
789 | return drm_syncobj_create_as_handle(file_private, |
790 | &args->handle, args->flags); | |
e9083420 DA |
791 | } |
792 | ||
793 | int | |
794 | drm_syncobj_destroy_ioctl(struct drm_device *dev, void *data, | |
795 | struct drm_file *file_private) | |
796 | { | |
797 | struct drm_syncobj_destroy *args = data; | |
798 | ||
799 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 800 | return -EOPNOTSUPP; |
e9083420 DA |
801 | |
802 | /* make sure padding is empty */ | |
803 | if (args->pad) | |
804 | return -EINVAL; | |
805 | return drm_syncobj_destroy(file_private, args->handle); | |
806 | } | |
807 | ||
808 | int | |
809 | drm_syncobj_handle_to_fd_ioctl(struct drm_device *dev, void *data, | |
810 | struct drm_file *file_private) | |
811 | { | |
812 | struct drm_syncobj_handle *args = data; | |
813 | ||
814 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 815 | return -EOPNOTSUPP; |
e9083420 | 816 | |
3ee45a3b | 817 | if (args->pad) |
e9083420 DA |
818 | return -EINVAL; |
819 | ||
3ee45a3b DA |
820 | if (args->flags != 0 && |
821 | args->flags != DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) | |
822 | return -EINVAL; | |
823 | ||
824 | if (args->flags & DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE) | |
825 | return drm_syncobj_export_sync_file(file_private, args->handle, | |
826 | &args->fd); | |
827 | ||
e9083420 DA |
828 | return drm_syncobj_handle_to_fd(file_private, args->handle, |
829 | &args->fd); | |
830 | } | |
831 | ||
832 | int | |
833 | drm_syncobj_fd_to_handle_ioctl(struct drm_device *dev, void *data, | |
834 | struct drm_file *file_private) | |
835 | { | |
836 | struct drm_syncobj_handle *args = data; | |
837 | ||
838 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 839 | return -EOPNOTSUPP; |
e9083420 | 840 | |
3ee45a3b DA |
841 | if (args->pad) |
842 | return -EINVAL; | |
843 | ||
844 | if (args->flags != 0 && | |
845 | args->flags != DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) | |
e9083420 DA |
846 | return -EINVAL; |
847 | ||
3ee45a3b DA |
848 | if (args->flags & DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE) |
849 | return drm_syncobj_import_sync_file_fence(file_private, | |
850 | args->fd, | |
851 | args->handle); | |
852 | ||
e9083420 DA |
853 | return drm_syncobj_fd_to_handle(file_private, args->fd, |
854 | &args->handle); | |
855 | } | |
5e60a10e | 856 | |
e7aca503 FE |
857 | struct syncobj_wait_entry { |
858 | struct task_struct *task; | |
859 | struct dma_fence *fence; | |
860 | struct dma_fence_cb fence_cb; | |
861 | struct drm_syncobj_cb syncobj_cb; | |
862 | }; | |
863 | ||
864 | static void syncobj_wait_fence_func(struct dma_fence *fence, | |
865 | struct dma_fence_cb *cb) | |
866 | { | |
867 | struct syncobj_wait_entry *wait = | |
868 | container_of(cb, struct syncobj_wait_entry, fence_cb); | |
869 | ||
870 | wake_up_process(wait->task); | |
871 | } | |
872 | ||
873 | static void syncobj_wait_syncobj_func(struct drm_syncobj *syncobj, | |
874 | struct drm_syncobj_cb *cb) | |
875 | { | |
876 | struct syncobj_wait_entry *wait = | |
877 | container_of(cb, struct syncobj_wait_entry, syncobj_cb); | |
878 | ||
48197bc5 CZ |
879 | drm_syncobj_search_fence(syncobj, 0, 0, &wait->fence); |
880 | ||
e7aca503 FE |
881 | wake_up_process(wait->task); |
882 | } | |
883 | ||
884 | static signed long drm_syncobj_array_wait_timeout(struct drm_syncobj **syncobjs, | |
885 | uint32_t count, | |
886 | uint32_t flags, | |
887 | signed long timeout, | |
888 | uint32_t *idx) | |
889 | { | |
890 | struct syncobj_wait_entry *entries; | |
891 | struct dma_fence *fence; | |
e7aca503 FE |
892 | uint32_t signaled_count, i; |
893 | ||
894 | entries = kcalloc(count, sizeof(*entries), GFP_KERNEL); | |
895 | if (!entries) | |
896 | return -ENOMEM; | |
897 | ||
898 | /* Walk the list of sync objects and initialize entries. We do | |
899 | * this up-front so that we can properly return -EINVAL if there is | |
900 | * a syncobj with a missing fence and then never have the chance of | |
901 | * returning -EINVAL again. | |
902 | */ | |
903 | signaled_count = 0; | |
904 | for (i = 0; i < count; ++i) { | |
905 | entries[i].task = current; | |
48197bc5 CZ |
906 | drm_syncobj_search_fence(syncobjs[i], 0, 0, |
907 | &entries[i].fence); | |
e7aca503 FE |
908 | if (!entries[i].fence) { |
909 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { | |
910 | continue; | |
911 | } else { | |
12fec62a | 912 | timeout = -EINVAL; |
e7aca503 FE |
913 | goto cleanup_entries; |
914 | } | |
915 | } | |
916 | ||
917 | if (dma_fence_is_signaled(entries[i].fence)) { | |
918 | if (signaled_count == 0 && idx) | |
919 | *idx = i; | |
920 | signaled_count++; | |
921 | } | |
922 | } | |
923 | ||
e7aca503 FE |
924 | if (signaled_count == count || |
925 | (signaled_count > 0 && | |
926 | !(flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL))) | |
927 | goto cleanup_entries; | |
928 | ||
929 | /* There's a very annoying laxness in the dma_fence API here, in | |
930 | * that backends are not required to automatically report when a | |
931 | * fence is signaled prior to fence->ops->enable_signaling() being | |
932 | * called. So here if we fail to match signaled_count, we need to | |
933 | * fallthough and try a 0 timeout wait! | |
934 | */ | |
935 | ||
936 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT) { | |
937 | for (i = 0; i < count; ++i) { | |
337fe9f5 FE |
938 | if (entries[i].fence) |
939 | continue; | |
940 | ||
e7aca503 FE |
941 | drm_syncobj_fence_get_or_add_callback(syncobjs[i], |
942 | &entries[i].fence, | |
943 | &entries[i].syncobj_cb, | |
944 | syncobj_wait_syncobj_func); | |
945 | } | |
946 | } | |
947 | ||
948 | do { | |
949 | set_current_state(TASK_INTERRUPTIBLE); | |
950 | ||
951 | signaled_count = 0; | |
952 | for (i = 0; i < count; ++i) { | |
953 | fence = entries[i].fence; | |
954 | if (!fence) | |
955 | continue; | |
956 | ||
957 | if (dma_fence_is_signaled(fence) || | |
958 | (!entries[i].fence_cb.func && | |
959 | dma_fence_add_callback(fence, | |
960 | &entries[i].fence_cb, | |
961 | syncobj_wait_fence_func))) { | |
962 | /* The fence has been signaled */ | |
963 | if (flags & DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL) { | |
964 | signaled_count++; | |
965 | } else { | |
966 | if (idx) | |
967 | *idx = i; | |
968 | goto done_waiting; | |
969 | } | |
970 | } | |
971 | } | |
972 | ||
973 | if (signaled_count == count) | |
974 | goto done_waiting; | |
975 | ||
976 | if (timeout == 0) { | |
12fec62a | 977 | timeout = -ETIME; |
e7aca503 FE |
978 | goto done_waiting; |
979 | } | |
980 | ||
12fec62a CW |
981 | if (signal_pending(current)) { |
982 | timeout = -ERESTARTSYS; | |
983 | goto done_waiting; | |
984 | } | |
e7aca503 | 985 | |
12fec62a CW |
986 | timeout = schedule_timeout(timeout); |
987 | } while (1); | |
e7aca503 FE |
988 | |
989 | done_waiting: | |
990 | __set_current_state(TASK_RUNNING); | |
991 | ||
992 | cleanup_entries: | |
993 | for (i = 0; i < count; ++i) { | |
994 | if (entries[i].syncobj_cb.func) | |
995 | drm_syncobj_remove_callback(syncobjs[i], | |
996 | &entries[i].syncobj_cb); | |
997 | if (entries[i].fence_cb.func) | |
998 | dma_fence_remove_callback(entries[i].fence, | |
999 | &entries[i].fence_cb); | |
1000 | dma_fence_put(entries[i].fence); | |
1001 | } | |
1002 | kfree(entries); | |
1003 | ||
12fec62a | 1004 | return timeout; |
e7aca503 FE |
1005 | } |
1006 | ||
5e60a10e DA |
1007 | /** |
1008 | * drm_timeout_abs_to_jiffies - calculate jiffies timeout from absolute value | |
1009 | * | |
1010 | * @timeout_nsec: timeout nsec component in ns, 0 for poll | |
1011 | * | |
1012 | * Calculate the timeout in jiffies from an absolute time in sec/nsec. | |
1013 | */ | |
1014 | static signed long drm_timeout_abs_to_jiffies(int64_t timeout_nsec) | |
1015 | { | |
1016 | ktime_t abs_timeout, now; | |
1017 | u64 timeout_ns, timeout_jiffies64; | |
1018 | ||
1019 | /* make 0 timeout means poll - absolute 0 doesn't seem valid */ | |
1020 | if (timeout_nsec == 0) | |
1021 | return 0; | |
1022 | ||
1023 | abs_timeout = ns_to_ktime(timeout_nsec); | |
1024 | now = ktime_get(); | |
1025 | ||
1026 | if (!ktime_after(abs_timeout, now)) | |
1027 | return 0; | |
1028 | ||
1029 | timeout_ns = ktime_to_ns(ktime_sub(abs_timeout, now)); | |
1030 | ||
1031 | timeout_jiffies64 = nsecs_to_jiffies64(timeout_ns); | |
1032 | /* clamp timeout to avoid infinite timeout */ | |
1033 | if (timeout_jiffies64 >= MAX_SCHEDULE_TIMEOUT - 1) | |
1034 | return MAX_SCHEDULE_TIMEOUT - 1; | |
1035 | ||
1036 | return timeout_jiffies64 + 1; | |
1037 | } | |
1038 | ||
e7aca503 FE |
1039 | static int drm_syncobj_array_wait(struct drm_device *dev, |
1040 | struct drm_file *file_private, | |
1041 | struct drm_syncobj_wait *wait, | |
1042 | struct drm_syncobj **syncobjs) | |
5e60a10e DA |
1043 | { |
1044 | signed long timeout = drm_timeout_abs_to_jiffies(wait->timeout_nsec); | |
5e60a10e DA |
1045 | uint32_t first = ~0; |
1046 | ||
12fec62a CW |
1047 | timeout = drm_syncobj_array_wait_timeout(syncobjs, |
1048 | wait->count_handles, | |
1049 | wait->flags, | |
1050 | timeout, &first); | |
1051 | if (timeout < 0) | |
1052 | return timeout; | |
5e60a10e DA |
1053 | |
1054 | wait->first_signaled = first; | |
5e60a10e DA |
1055 | return 0; |
1056 | } | |
1057 | ||
3e6fb72d | 1058 | static int drm_syncobj_array_find(struct drm_file *file_private, |
9e554462 VS |
1059 | void __user *user_handles, |
1060 | uint32_t count_handles, | |
3e6fb72d | 1061 | struct drm_syncobj ***syncobjs_out) |
5e60a10e | 1062 | { |
3e6fb72d | 1063 | uint32_t i, *handles; |
e7aca503 | 1064 | struct drm_syncobj **syncobjs; |
3e6fb72d | 1065 | int ret; |
5e60a10e | 1066 | |
3e6fb72d | 1067 | handles = kmalloc_array(count_handles, sizeof(*handles), GFP_KERNEL); |
5e60a10e DA |
1068 | if (handles == NULL) |
1069 | return -ENOMEM; | |
1070 | ||
3e6fb72d FE |
1071 | if (copy_from_user(handles, user_handles, |
1072 | sizeof(uint32_t) * count_handles)) { | |
5e60a10e DA |
1073 | ret = -EFAULT; |
1074 | goto err_free_handles; | |
1075 | } | |
1076 | ||
3e6fb72d FE |
1077 | syncobjs = kmalloc_array(count_handles, sizeof(*syncobjs), GFP_KERNEL); |
1078 | if (syncobjs == NULL) { | |
5e60a10e DA |
1079 | ret = -ENOMEM; |
1080 | goto err_free_handles; | |
1081 | } | |
1082 | ||
3e6fb72d | 1083 | for (i = 0; i < count_handles; i++) { |
e7aca503 FE |
1084 | syncobjs[i] = drm_syncobj_find(file_private, handles[i]); |
1085 | if (!syncobjs[i]) { | |
1086 | ret = -ENOENT; | |
3e6fb72d | 1087 | goto err_put_syncobjs; |
e7aca503 | 1088 | } |
5e60a10e DA |
1089 | } |
1090 | ||
3e6fb72d FE |
1091 | kfree(handles); |
1092 | *syncobjs_out = syncobjs; | |
1093 | return 0; | |
5e60a10e | 1094 | |
3e6fb72d | 1095 | err_put_syncobjs: |
e7aca503 FE |
1096 | while (i-- > 0) |
1097 | drm_syncobj_put(syncobjs[i]); | |
1098 | kfree(syncobjs); | |
5e60a10e DA |
1099 | err_free_handles: |
1100 | kfree(handles); | |
1101 | ||
1102 | return ret; | |
1103 | } | |
3e6fb72d FE |
1104 | |
1105 | static void drm_syncobj_array_free(struct drm_syncobj **syncobjs, | |
1106 | uint32_t count) | |
1107 | { | |
1108 | uint32_t i; | |
1109 | for (i = 0; i < count; i++) | |
1110 | drm_syncobj_put(syncobjs[i]); | |
1111 | kfree(syncobjs); | |
1112 | } | |
1113 | ||
1114 | int | |
1115 | drm_syncobj_wait_ioctl(struct drm_device *dev, void *data, | |
1116 | struct drm_file *file_private) | |
1117 | { | |
1118 | struct drm_syncobj_wait *args = data; | |
1119 | struct drm_syncobj **syncobjs; | |
1120 | int ret = 0; | |
1121 | ||
1122 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 1123 | return -EOPNOTSUPP; |
3e6fb72d FE |
1124 | |
1125 | if (args->flags & ~(DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL | | |
1126 | DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT)) | |
1127 | return -EINVAL; | |
1128 | ||
1129 | if (args->count_handles == 0) | |
1130 | return -EINVAL; | |
1131 | ||
1132 | ret = drm_syncobj_array_find(file_private, | |
1133 | u64_to_user_ptr(args->handles), | |
1134 | args->count_handles, | |
1135 | &syncobjs); | |
1136 | if (ret < 0) | |
1137 | return ret; | |
1138 | ||
1139 | ret = drm_syncobj_array_wait(dev, file_private, | |
1140 | args, syncobjs); | |
1141 | ||
1142 | drm_syncobj_array_free(syncobjs, args->count_handles); | |
1143 | ||
1144 | return ret; | |
1145 | } | |
aa4035d2 FE |
1146 | |
1147 | int | |
1148 | drm_syncobj_reset_ioctl(struct drm_device *dev, void *data, | |
1149 | struct drm_file *file_private) | |
1150 | { | |
1151 | struct drm_syncobj_array *args = data; | |
1152 | struct drm_syncobj **syncobjs; | |
1153 | uint32_t i; | |
1154 | int ret; | |
1155 | ||
1156 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 1157 | return -EOPNOTSUPP; |
aa4035d2 FE |
1158 | |
1159 | if (args->pad != 0) | |
1160 | return -EINVAL; | |
1161 | ||
1162 | if (args->count_handles == 0) | |
1163 | return -EINVAL; | |
1164 | ||
1165 | ret = drm_syncobj_array_find(file_private, | |
1166 | u64_to_user_ptr(args->handles), | |
1167 | args->count_handles, | |
1168 | &syncobjs); | |
1169 | if (ret < 0) | |
1170 | return ret; | |
1171 | ||
48197bc5 CZ |
1172 | for (i = 0; i < args->count_handles; i++) { |
1173 | drm_syncobj_fini(syncobjs[i]); | |
1174 | drm_syncobj_init(syncobjs[i]); | |
1175 | } | |
aa4035d2 FE |
1176 | drm_syncobj_array_free(syncobjs, args->count_handles); |
1177 | ||
48197bc5 | 1178 | return ret; |
aa4035d2 | 1179 | } |
ffa9443f FE |
1180 | |
1181 | int | |
1182 | drm_syncobj_signal_ioctl(struct drm_device *dev, void *data, | |
1183 | struct drm_file *file_private) | |
1184 | { | |
1185 | struct drm_syncobj_array *args = data; | |
1186 | struct drm_syncobj **syncobjs; | |
1187 | uint32_t i; | |
1188 | int ret; | |
1189 | ||
1190 | if (!drm_core_check_feature(dev, DRIVER_SYNCOBJ)) | |
69fdf420 | 1191 | return -EOPNOTSUPP; |
ffa9443f FE |
1192 | |
1193 | if (args->pad != 0) | |
1194 | return -EINVAL; | |
1195 | ||
1196 | if (args->count_handles == 0) | |
1197 | return -EINVAL; | |
1198 | ||
1199 | ret = drm_syncobj_array_find(file_private, | |
1200 | u64_to_user_ptr(args->handles), | |
1201 | args->count_handles, | |
1202 | &syncobjs); | |
1203 | if (ret < 0) | |
1204 | return ret; | |
1205 | ||
1206 | for (i = 0; i < args->count_handles; i++) { | |
1207 | ret = drm_syncobj_assign_null_handle(syncobjs[i]); | |
1208 | if (ret < 0) | |
1209 | break; | |
1210 | } | |
1211 | ||
1212 | drm_syncobj_array_free(syncobjs, args->count_handles); | |
1213 | ||
1214 | return ret; | |
1215 | } |