]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/dma-buf/dma-fence.c
dma-fence: Make ->enable_signaling optional
[mirror_ubuntu-hirsute-kernel.git] / drivers / dma-buf / dma-fence.c
1 /*
2 * Fence mechanism for dma-buf and to allow for asynchronous dma access
3 *
4 * Copyright (C) 2012 Canonical Ltd
5 * Copyright (C) 2012 Texas Instruments
6 *
7 * Authors:
8 * Rob Clark <robdclark@gmail.com>
9 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published by
13 * the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 */
20
21 #include <linux/slab.h>
22 #include <linux/export.h>
23 #include <linux/atomic.h>
24 #include <linux/dma-fence.h>
25 #include <linux/sched/signal.h>
26
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/dma_fence.h>
29
30 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
31 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
32
33 /*
34 * fence context counter: each execution context should have its own
35 * fence context, this allows checking if fences belong to the same
36 * context or not. One device can have multiple separate contexts,
37 * and they're used if some engine can run independently of another.
38 */
39 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
40
41 /**
42 * dma_fence_context_alloc - allocate an array of fence contexts
43 * @num: [in] amount of contexts to allocate
44 *
45 * This function will return the first index of the number of fences allocated.
46 * The fence context is used for setting fence->context to a unique number.
47 */
48 u64 dma_fence_context_alloc(unsigned num)
49 {
50 WARN_ON(!num);
51 return atomic64_add_return(num, &dma_fence_context_counter) - num;
52 }
53 EXPORT_SYMBOL(dma_fence_context_alloc);
54
55 /**
56 * dma_fence_signal_locked - signal completion of a fence
57 * @fence: the fence to signal
58 *
59 * Signal completion for software callbacks on a fence, this will unblock
60 * dma_fence_wait() calls and run all the callbacks added with
61 * dma_fence_add_callback(). Can be called multiple times, but since a fence
62 * can only go from unsignaled to signaled state, it will only be effective
63 * the first time.
64 *
65 * Unlike dma_fence_signal, this function must be called with fence->lock held.
66 */
67 int dma_fence_signal_locked(struct dma_fence *fence)
68 {
69 struct dma_fence_cb *cur, *tmp;
70 int ret = 0;
71
72 lockdep_assert_held(fence->lock);
73
74 if (WARN_ON(!fence))
75 return -EINVAL;
76
77 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
78 ret = -EINVAL;
79
80 /*
81 * we might have raced with the unlocked dma_fence_signal,
82 * still run through all callbacks
83 */
84 } else {
85 fence->timestamp = ktime_get();
86 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
87 trace_dma_fence_signaled(fence);
88 }
89
90 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
91 list_del_init(&cur->node);
92 cur->func(fence, cur);
93 }
94 return ret;
95 }
96 EXPORT_SYMBOL(dma_fence_signal_locked);
97
98 /**
99 * dma_fence_signal - signal completion of a fence
100 * @fence: the fence to signal
101 *
102 * Signal completion for software callbacks on a fence, this will unblock
103 * dma_fence_wait() calls and run all the callbacks added with
104 * dma_fence_add_callback(). Can be called multiple times, but since a fence
105 * can only go from unsignaled to signaled state, it will only be effective
106 * the first time.
107 */
108 int dma_fence_signal(struct dma_fence *fence)
109 {
110 unsigned long flags;
111
112 if (!fence)
113 return -EINVAL;
114
115 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
116 return -EINVAL;
117
118 fence->timestamp = ktime_get();
119 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
120 trace_dma_fence_signaled(fence);
121
122 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
123 struct dma_fence_cb *cur, *tmp;
124
125 spin_lock_irqsave(fence->lock, flags);
126 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
127 list_del_init(&cur->node);
128 cur->func(fence, cur);
129 }
130 spin_unlock_irqrestore(fence->lock, flags);
131 }
132 return 0;
133 }
134 EXPORT_SYMBOL(dma_fence_signal);
135
136 /**
137 * dma_fence_wait_timeout - sleep until the fence gets signaled
138 * or until timeout elapses
139 * @fence: [in] the fence to wait on
140 * @intr: [in] if true, do an interruptible wait
141 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
142 *
143 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
144 * remaining timeout in jiffies on success. Other error values may be
145 * returned on custom implementations.
146 *
147 * Performs a synchronous wait on this fence. It is assumed the caller
148 * directly or indirectly (buf-mgr between reservation and committing)
149 * holds a reference to the fence, otherwise the fence might be
150 * freed before return, resulting in undefined behavior.
151 */
152 signed long
153 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
154 {
155 signed long ret;
156
157 if (WARN_ON(timeout < 0))
158 return -EINVAL;
159
160 trace_dma_fence_wait_start(fence);
161 ret = fence->ops->wait(fence, intr, timeout);
162 trace_dma_fence_wait_end(fence);
163 return ret;
164 }
165 EXPORT_SYMBOL(dma_fence_wait_timeout);
166
167 void dma_fence_release(struct kref *kref)
168 {
169 struct dma_fence *fence =
170 container_of(kref, struct dma_fence, refcount);
171
172 trace_dma_fence_destroy(fence);
173
174 /* Failed to signal before release, could be a refcounting issue */
175 WARN_ON(!list_empty(&fence->cb_list));
176
177 if (fence->ops->release)
178 fence->ops->release(fence);
179 else
180 dma_fence_free(fence);
181 }
182 EXPORT_SYMBOL(dma_fence_release);
183
184 /**
185 * dma_fence_free - default release function for &dma_fence.
186 * @fence: fence to release
187 *
188 * This is the default implementation for &dma_fence_ops.release. It calls
189 * kfree_rcu() on @fence.
190 */
191 void dma_fence_free(struct dma_fence *fence)
192 {
193 kfree_rcu(fence, rcu);
194 }
195 EXPORT_SYMBOL(dma_fence_free);
196
197 /**
198 * dma_fence_enable_sw_signaling - enable signaling on fence
199 * @fence: [in] the fence to enable
200 *
201 * this will request for sw signaling to be enabled, to make the fence
202 * complete as soon as possible
203 */
204 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
205 {
206 unsigned long flags;
207
208 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
209 &fence->flags) &&
210 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
211 trace_dma_fence_enable_signal(fence);
212
213 spin_lock_irqsave(fence->lock, flags);
214
215 if (!fence->ops->enable_signaling(fence))
216 dma_fence_signal_locked(fence);
217
218 spin_unlock_irqrestore(fence->lock, flags);
219 }
220 }
221 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
222
223 /**
224 * dma_fence_add_callback - add a callback to be called when the fence
225 * is signaled
226 * @fence: [in] the fence to wait on
227 * @cb: [in] the callback to register
228 * @func: [in] the function to call
229 *
230 * cb will be initialized by dma_fence_add_callback, no initialization
231 * by the caller is required. Any number of callbacks can be registered
232 * to a fence, but a callback can only be registered to one fence at a time.
233 *
234 * Note that the callback can be called from an atomic context. If
235 * fence is already signaled, this function will return -ENOENT (and
236 * *not* call the callback)
237 *
238 * Add a software callback to the fence. Same restrictions apply to
239 * refcount as it does to dma_fence_wait, however the caller doesn't need to
240 * keep a refcount to fence afterwards: when software access is enabled,
241 * the creator of the fence is required to keep the fence alive until
242 * after it signals with dma_fence_signal. The callback itself can be called
243 * from irq context.
244 *
245 * Returns 0 in case of success, -ENOENT if the fence is already signaled
246 * and -EINVAL in case of error.
247 */
248 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
249 dma_fence_func_t func)
250 {
251 unsigned long flags;
252 int ret = 0;
253 bool was_set;
254
255 if (WARN_ON(!fence || !func))
256 return -EINVAL;
257
258 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
259 INIT_LIST_HEAD(&cb->node);
260 return -ENOENT;
261 }
262
263 spin_lock_irqsave(fence->lock, flags);
264
265 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
266 &fence->flags);
267
268 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
269 ret = -ENOENT;
270 else if (!was_set) {
271 trace_dma_fence_enable_signal(fence);
272
273 if (!fence->ops->enable_signaling(fence)) {
274 dma_fence_signal_locked(fence);
275 ret = -ENOENT;
276 }
277 }
278
279 if (!ret) {
280 cb->func = func;
281 list_add_tail(&cb->node, &fence->cb_list);
282 } else
283 INIT_LIST_HEAD(&cb->node);
284 spin_unlock_irqrestore(fence->lock, flags);
285
286 return ret;
287 }
288 EXPORT_SYMBOL(dma_fence_add_callback);
289
290 /**
291 * dma_fence_get_status - returns the status upon completion
292 * @fence: [in] the dma_fence to query
293 *
294 * This wraps dma_fence_get_status_locked() to return the error status
295 * condition on a signaled fence. See dma_fence_get_status_locked() for more
296 * details.
297 *
298 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
299 * been signaled without an error condition, or a negative error code
300 * if the fence has been completed in err.
301 */
302 int dma_fence_get_status(struct dma_fence *fence)
303 {
304 unsigned long flags;
305 int status;
306
307 spin_lock_irqsave(fence->lock, flags);
308 status = dma_fence_get_status_locked(fence);
309 spin_unlock_irqrestore(fence->lock, flags);
310
311 return status;
312 }
313 EXPORT_SYMBOL(dma_fence_get_status);
314
315 /**
316 * dma_fence_remove_callback - remove a callback from the signaling list
317 * @fence: [in] the fence to wait on
318 * @cb: [in] the callback to remove
319 *
320 * Remove a previously queued callback from the fence. This function returns
321 * true if the callback is successfully removed, or false if the fence has
322 * already been signaled.
323 *
324 * *WARNING*:
325 * Cancelling a callback should only be done if you really know what you're
326 * doing, since deadlocks and race conditions could occur all too easily. For
327 * this reason, it should only ever be done on hardware lockup recovery,
328 * with a reference held to the fence.
329 */
330 bool
331 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
332 {
333 unsigned long flags;
334 bool ret;
335
336 spin_lock_irqsave(fence->lock, flags);
337
338 ret = !list_empty(&cb->node);
339 if (ret)
340 list_del_init(&cb->node);
341
342 spin_unlock_irqrestore(fence->lock, flags);
343
344 return ret;
345 }
346 EXPORT_SYMBOL(dma_fence_remove_callback);
347
348 struct default_wait_cb {
349 struct dma_fence_cb base;
350 struct task_struct *task;
351 };
352
353 static void
354 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
355 {
356 struct default_wait_cb *wait =
357 container_of(cb, struct default_wait_cb, base);
358
359 wake_up_state(wait->task, TASK_NORMAL);
360 }
361
362 /**
363 * dma_fence_default_wait - default sleep until the fence gets signaled
364 * or until timeout elapses
365 * @fence: [in] the fence to wait on
366 * @intr: [in] if true, do an interruptible wait
367 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
368 *
369 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
370 * remaining timeout in jiffies on success. If timeout is zero the value one is
371 * returned if the fence is already signaled for consistency with other
372 * functions taking a jiffies timeout.
373 */
374 signed long
375 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
376 {
377 struct default_wait_cb cb;
378 unsigned long flags;
379 signed long ret = timeout ? timeout : 1;
380 bool was_set;
381
382 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
383 return ret;
384
385 spin_lock_irqsave(fence->lock, flags);
386
387 if (intr && signal_pending(current)) {
388 ret = -ERESTARTSYS;
389 goto out;
390 }
391
392 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
393 &fence->flags);
394
395 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
396 goto out;
397
398 if (!was_set) {
399 trace_dma_fence_enable_signal(fence);
400
401 if (!fence->ops->enable_signaling(fence)) {
402 dma_fence_signal_locked(fence);
403 goto out;
404 }
405 }
406
407 if (!timeout) {
408 ret = 0;
409 goto out;
410 }
411
412 cb.base.func = dma_fence_default_wait_cb;
413 cb.task = current;
414 list_add(&cb.base.node, &fence->cb_list);
415
416 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
417 if (intr)
418 __set_current_state(TASK_INTERRUPTIBLE);
419 else
420 __set_current_state(TASK_UNINTERRUPTIBLE);
421 spin_unlock_irqrestore(fence->lock, flags);
422
423 ret = schedule_timeout(ret);
424
425 spin_lock_irqsave(fence->lock, flags);
426 if (ret > 0 && intr && signal_pending(current))
427 ret = -ERESTARTSYS;
428 }
429
430 if (!list_empty(&cb.base.node))
431 list_del(&cb.base.node);
432 __set_current_state(TASK_RUNNING);
433
434 out:
435 spin_unlock_irqrestore(fence->lock, flags);
436 return ret;
437 }
438 EXPORT_SYMBOL(dma_fence_default_wait);
439
440 static bool
441 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
442 uint32_t *idx)
443 {
444 int i;
445
446 for (i = 0; i < count; ++i) {
447 struct dma_fence *fence = fences[i];
448 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
449 if (idx)
450 *idx = i;
451 return true;
452 }
453 }
454 return false;
455 }
456
457 /**
458 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
459 * or until timeout elapses
460 * @fences: [in] array of fences to wait on
461 * @count: [in] number of fences to wait on
462 * @intr: [in] if true, do an interruptible wait
463 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
464 * @idx: [out] the first signaled fence index, meaningful only on
465 * positive return
466 *
467 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
468 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
469 * on success.
470 *
471 * Synchronous waits for the first fence in the array to be signaled. The
472 * caller needs to hold a reference to all fences in the array, otherwise a
473 * fence might be freed before return, resulting in undefined behavior.
474 */
475 signed long
476 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
477 bool intr, signed long timeout, uint32_t *idx)
478 {
479 struct default_wait_cb *cb;
480 signed long ret = timeout;
481 unsigned i;
482
483 if (WARN_ON(!fences || !count || timeout < 0))
484 return -EINVAL;
485
486 if (timeout == 0) {
487 for (i = 0; i < count; ++i)
488 if (dma_fence_is_signaled(fences[i])) {
489 if (idx)
490 *idx = i;
491 return 1;
492 }
493
494 return 0;
495 }
496
497 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
498 if (cb == NULL) {
499 ret = -ENOMEM;
500 goto err_free_cb;
501 }
502
503 for (i = 0; i < count; ++i) {
504 struct dma_fence *fence = fences[i];
505
506 if (fence->ops->wait != dma_fence_default_wait) {
507 ret = -EINVAL;
508 goto fence_rm_cb;
509 }
510
511 cb[i].task = current;
512 if (dma_fence_add_callback(fence, &cb[i].base,
513 dma_fence_default_wait_cb)) {
514 /* This fence is already signaled */
515 if (idx)
516 *idx = i;
517 goto fence_rm_cb;
518 }
519 }
520
521 while (ret > 0) {
522 if (intr)
523 set_current_state(TASK_INTERRUPTIBLE);
524 else
525 set_current_state(TASK_UNINTERRUPTIBLE);
526
527 if (dma_fence_test_signaled_any(fences, count, idx))
528 break;
529
530 ret = schedule_timeout(ret);
531
532 if (ret > 0 && intr && signal_pending(current))
533 ret = -ERESTARTSYS;
534 }
535
536 __set_current_state(TASK_RUNNING);
537
538 fence_rm_cb:
539 while (i-- > 0)
540 dma_fence_remove_callback(fences[i], &cb[i].base);
541
542 err_free_cb:
543 kfree(cb);
544
545 return ret;
546 }
547 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
548
549 /**
550 * dma_fence_init - Initialize a custom fence.
551 * @fence: [in] the fence to initialize
552 * @ops: [in] the dma_fence_ops for operations on this fence
553 * @lock: [in] the irqsafe spinlock to use for locking this fence
554 * @context: [in] the execution context this fence is run on
555 * @seqno: [in] a linear increasing sequence number for this context
556 *
557 * Initializes an allocated fence, the caller doesn't have to keep its
558 * refcount after committing with this fence, but it will need to hold a
559 * refcount again if dma_fence_ops.enable_signaling gets called. This can
560 * be used for other implementing other types of fence.
561 *
562 * context and seqno are used for easy comparison between fences, allowing
563 * to check which fence is later by simply using dma_fence_later.
564 */
565 void
566 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
567 spinlock_t *lock, u64 context, unsigned seqno)
568 {
569 BUG_ON(!lock);
570 BUG_ON(!ops || !ops->wait ||
571 !ops->get_driver_name || !ops->get_timeline_name);
572
573 kref_init(&fence->refcount);
574 fence->ops = ops;
575 INIT_LIST_HEAD(&fence->cb_list);
576 fence->lock = lock;
577 fence->context = context;
578 fence->seqno = seqno;
579 fence->flags = 0UL;
580 fence->error = 0;
581
582 if (!ops->enable_signaling)
583 set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
584 &fence->flags);
585
586 trace_dma_fence_init(fence);
587 }
588 EXPORT_SYMBOL(dma_fence_init);