1 // SPDX-License-Identifier: GPL-2.0-only
3 * Fence mechanism for dma-buf and to allow for asynchronous dma access
5 * Copyright (C) 2012 Canonical Ltd
6 * Copyright (C) 2012 Texas Instruments
9 * Rob Clark <robdclark@gmail.com>
10 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
13 #include <linux/slab.h>
14 #include <linux/export.h>
15 #include <linux/atomic.h>
16 #include <linux/dma-fence.h>
17 #include <linux/sched/signal.h>
19 #define CREATE_TRACE_POINTS
20 #include <trace/events/dma_fence.h>
22 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
23 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
24 EXPORT_TRACEPOINT_SYMBOL(dma_fence_signaled);
26 static DEFINE_SPINLOCK(dma_fence_stub_lock);
27 static struct dma_fence dma_fence_stub;
30 * fence context counter: each execution context should have its own
31 * fence context, this allows checking if fences belong to the same
32 * context or not. One device can have multiple separate contexts,
33 * and they're used if some engine can run independently of another.
35 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(1);
38 * DOC: DMA fences overview
40 * DMA fences, represented by &struct dma_fence, are the kernel internal
41 * synchronization primitive for DMA operations like GPU rendering, video
42 * encoding/decoding, or displaying buffers on a screen.
44 * A fence is initialized using dma_fence_init() and completed using
45 * dma_fence_signal(). Fences are associated with a context, allocated through
46 * dma_fence_context_alloc(), and all fences on the same context are
49 * Since the purposes of fences is to facilitate cross-device and
50 * cross-application synchronization, there's multiple ways to use one:
52 * - Individual fences can be exposed as a &sync_file, accessed as a file
53 * descriptor from userspace, created by calling sync_file_create(). This is
54 * called explicit fencing, since userspace passes around explicit
55 * synchronization points.
57 * - Some subsystems also have their own explicit fencing primitives, like
58 * &drm_syncobj. Compared to &sync_file, a &drm_syncobj allows the underlying
59 * fence to be updated.
61 * - Then there's also implicit fencing, where the synchronization points are
62 * implicitly passed around as part of shared &dma_buf instances. Such
63 * implicit fences are stored in &struct dma_resv through the
64 * &dma_buf.resv pointer.
67 static const char *dma_fence_stub_get_name(struct dma_fence *fence)
72 static const struct dma_fence_ops dma_fence_stub_ops = {
73 .get_driver_name = dma_fence_stub_get_name,
74 .get_timeline_name = dma_fence_stub_get_name,
78 * dma_fence_get_stub - return a signaled fence
80 * Return a stub fence which is already signaled.
82 struct dma_fence *dma_fence_get_stub(void)
84 spin_lock(&dma_fence_stub_lock);
85 if (!dma_fence_stub.ops) {
86 dma_fence_init(&dma_fence_stub,
90 dma_fence_signal_locked(&dma_fence_stub);
92 spin_unlock(&dma_fence_stub_lock);
94 return dma_fence_get(&dma_fence_stub);
96 EXPORT_SYMBOL(dma_fence_get_stub);
99 * dma_fence_context_alloc - allocate an array of fence contexts
100 * @num: amount of contexts to allocate
102 * This function will return the first index of the number of fence contexts
103 * allocated. The fence context is used for setting &dma_fence.context to a
104 * unique number by passing the context to dma_fence_init().
106 u64 dma_fence_context_alloc(unsigned num)
109 return atomic64_add_return(num, &dma_fence_context_counter) - num;
111 EXPORT_SYMBOL(dma_fence_context_alloc);
114 * dma_fence_signal_locked - signal completion of a fence
115 * @fence: the fence to signal
117 * Signal completion for software callbacks on a fence, this will unblock
118 * dma_fence_wait() calls and run all the callbacks added with
119 * dma_fence_add_callback(). Can be called multiple times, but since a fence
120 * can only go from the unsignaled to the signaled state and not back, it will
121 * only be effective the first time.
123 * Unlike dma_fence_signal(), this function must be called with &dma_fence.lock
126 * Returns 0 on success and a negative error value when @fence has been
129 int dma_fence_signal_locked(struct dma_fence *fence)
131 struct dma_fence_cb *cur, *tmp;
132 struct list_head cb_list;
134 lockdep_assert_held(fence->lock);
136 if (unlikely(test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
140 /* Stash the cb_list before replacing it with the timestamp */
141 list_replace(&fence->cb_list, &cb_list);
143 fence->timestamp = ktime_get();
144 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
145 trace_dma_fence_signaled(fence);
147 list_for_each_entry_safe(cur, tmp, &cb_list, node) {
148 INIT_LIST_HEAD(&cur->node);
149 cur->func(fence, cur);
154 EXPORT_SYMBOL(dma_fence_signal_locked);
157 * dma_fence_signal - signal completion of a fence
158 * @fence: the fence to signal
160 * Signal completion for software callbacks on a fence, this will unblock
161 * dma_fence_wait() calls and run all the callbacks added with
162 * dma_fence_add_callback(). Can be called multiple times, but since a fence
163 * can only go from the unsignaled to the signaled state and not back, it will
164 * only be effective the first time.
166 * Returns 0 on success and a negative error value when @fence has been
169 int dma_fence_signal(struct dma_fence *fence)
177 spin_lock_irqsave(fence->lock, flags);
178 ret = dma_fence_signal_locked(fence);
179 spin_unlock_irqrestore(fence->lock, flags);
183 EXPORT_SYMBOL(dma_fence_signal);
186 * dma_fence_wait_timeout - sleep until the fence gets signaled
187 * or until timeout elapses
188 * @fence: the fence to wait on
189 * @intr: if true, do an interruptible wait
190 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
192 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
193 * remaining timeout in jiffies on success. Other error values may be
194 * returned on custom implementations.
196 * Performs a synchronous wait on this fence. It is assumed the caller
197 * directly or indirectly (buf-mgr between reservation and committing)
198 * holds a reference to the fence, otherwise the fence might be
199 * freed before return, resulting in undefined behavior.
201 * See also dma_fence_wait() and dma_fence_wait_any_timeout().
204 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
208 if (WARN_ON(timeout < 0))
211 trace_dma_fence_wait_start(fence);
212 if (fence->ops->wait)
213 ret = fence->ops->wait(fence, intr, timeout);
215 ret = dma_fence_default_wait(fence, intr, timeout);
216 trace_dma_fence_wait_end(fence);
219 EXPORT_SYMBOL(dma_fence_wait_timeout);
222 * dma_fence_release - default relese function for fences
223 * @kref: &dma_fence.recfount
225 * This is the default release functions for &dma_fence. Drivers shouldn't call
226 * this directly, but instead call dma_fence_put().
228 void dma_fence_release(struct kref *kref)
230 struct dma_fence *fence =
231 container_of(kref, struct dma_fence, refcount);
233 trace_dma_fence_destroy(fence);
235 if (WARN(!list_empty(&fence->cb_list) &&
236 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags),
237 "Fence %s:%s:%llx:%llx released with pending signals!\n",
238 fence->ops->get_driver_name(fence),
239 fence->ops->get_timeline_name(fence),
240 fence->context, fence->seqno)) {
244 * Failed to signal before release, likely a refcounting issue.
246 * This should never happen, but if it does make sure that we
247 * don't leave chains dangling. We set the error flag first
248 * so that the callbacks know this signal is due to an error.
250 spin_lock_irqsave(fence->lock, flags);
251 fence->error = -EDEADLK;
252 dma_fence_signal_locked(fence);
253 spin_unlock_irqrestore(fence->lock, flags);
256 if (fence->ops->release)
257 fence->ops->release(fence);
259 dma_fence_free(fence);
261 EXPORT_SYMBOL(dma_fence_release);
264 * dma_fence_free - default release function for &dma_fence.
265 * @fence: fence to release
267 * This is the default implementation for &dma_fence_ops.release. It calls
268 * kfree_rcu() on @fence.
270 void dma_fence_free(struct dma_fence *fence)
272 kfree_rcu(fence, rcu);
274 EXPORT_SYMBOL(dma_fence_free);
277 * dma_fence_enable_sw_signaling - enable signaling on fence
278 * @fence: the fence to enable
280 * This will request for sw signaling to be enabled, to make the fence
281 * complete as soon as possible. This calls &dma_fence_ops.enable_signaling
284 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
288 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
290 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) &&
291 fence->ops->enable_signaling) {
292 trace_dma_fence_enable_signal(fence);
294 spin_lock_irqsave(fence->lock, flags);
296 if (!fence->ops->enable_signaling(fence))
297 dma_fence_signal_locked(fence);
299 spin_unlock_irqrestore(fence->lock, flags);
302 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
305 * dma_fence_add_callback - add a callback to be called when the fence
307 * @fence: the fence to wait on
308 * @cb: the callback to register
309 * @func: the function to call
311 * @cb will be initialized by dma_fence_add_callback(), no initialization
312 * by the caller is required. Any number of callbacks can be registered
313 * to a fence, but a callback can only be registered to one fence at a time.
315 * Note that the callback can be called from an atomic context. If
316 * fence is already signaled, this function will return -ENOENT (and
317 * *not* call the callback).
319 * Add a software callback to the fence. Same restrictions apply to
320 * refcount as it does to dma_fence_wait(), however the caller doesn't need to
321 * keep a refcount to fence afterward dma_fence_add_callback() has returned:
322 * when software access is enabled, the creator of the fence is required to keep
323 * the fence alive until after it signals with dma_fence_signal(). The callback
324 * itself can be called from irq context.
326 * Returns 0 in case of success, -ENOENT if the fence is already signaled
327 * and -EINVAL in case of error.
329 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
330 dma_fence_func_t func)
336 if (WARN_ON(!fence || !func))
339 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
340 INIT_LIST_HEAD(&cb->node);
344 spin_lock_irqsave(fence->lock, flags);
346 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
349 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
351 else if (!was_set && fence->ops->enable_signaling) {
352 trace_dma_fence_enable_signal(fence);
354 if (!fence->ops->enable_signaling(fence)) {
355 dma_fence_signal_locked(fence);
362 list_add_tail(&cb->node, &fence->cb_list);
364 INIT_LIST_HEAD(&cb->node);
365 spin_unlock_irqrestore(fence->lock, flags);
369 EXPORT_SYMBOL(dma_fence_add_callback);
372 * dma_fence_get_status - returns the status upon completion
373 * @fence: the dma_fence to query
375 * This wraps dma_fence_get_status_locked() to return the error status
376 * condition on a signaled fence. See dma_fence_get_status_locked() for more
379 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
380 * been signaled without an error condition, or a negative error code
381 * if the fence has been completed in err.
383 int dma_fence_get_status(struct dma_fence *fence)
388 spin_lock_irqsave(fence->lock, flags);
389 status = dma_fence_get_status_locked(fence);
390 spin_unlock_irqrestore(fence->lock, flags);
394 EXPORT_SYMBOL(dma_fence_get_status);
397 * dma_fence_remove_callback - remove a callback from the signaling list
398 * @fence: the fence to wait on
399 * @cb: the callback to remove
401 * Remove a previously queued callback from the fence. This function returns
402 * true if the callback is successfully removed, or false if the fence has
403 * already been signaled.
406 * Cancelling a callback should only be done if you really know what you're
407 * doing, since deadlocks and race conditions could occur all too easily. For
408 * this reason, it should only ever be done on hardware lockup recovery,
409 * with a reference held to the fence.
411 * Behaviour is undefined if @cb has not been added to @fence using
412 * dma_fence_add_callback() beforehand.
415 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
420 spin_lock_irqsave(fence->lock, flags);
422 ret = !list_empty(&cb->node);
424 list_del_init(&cb->node);
426 spin_unlock_irqrestore(fence->lock, flags);
430 EXPORT_SYMBOL(dma_fence_remove_callback);
432 struct default_wait_cb {
433 struct dma_fence_cb base;
434 struct task_struct *task;
438 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
440 struct default_wait_cb *wait =
441 container_of(cb, struct default_wait_cb, base);
443 wake_up_state(wait->task, TASK_NORMAL);
447 * dma_fence_default_wait - default sleep until the fence gets signaled
448 * or until timeout elapses
449 * @fence: the fence to wait on
450 * @intr: if true, do an interruptible wait
451 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
453 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
454 * remaining timeout in jiffies on success. If timeout is zero the value one is
455 * returned if the fence is already signaled for consistency with other
456 * functions taking a jiffies timeout.
459 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
461 struct default_wait_cb cb;
463 signed long ret = timeout ? timeout : 1;
466 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
469 spin_lock_irqsave(fence->lock, flags);
471 if (intr && signal_pending(current)) {
476 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
479 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
482 if (!was_set && fence->ops->enable_signaling) {
483 trace_dma_fence_enable_signal(fence);
485 if (!fence->ops->enable_signaling(fence)) {
486 dma_fence_signal_locked(fence);
496 cb.base.func = dma_fence_default_wait_cb;
498 list_add(&cb.base.node, &fence->cb_list);
500 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
502 __set_current_state(TASK_INTERRUPTIBLE);
504 __set_current_state(TASK_UNINTERRUPTIBLE);
505 spin_unlock_irqrestore(fence->lock, flags);
507 ret = schedule_timeout(ret);
509 spin_lock_irqsave(fence->lock, flags);
510 if (ret > 0 && intr && signal_pending(current))
514 if (!list_empty(&cb.base.node))
515 list_del(&cb.base.node);
516 __set_current_state(TASK_RUNNING);
519 spin_unlock_irqrestore(fence->lock, flags);
522 EXPORT_SYMBOL(dma_fence_default_wait);
525 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
530 for (i = 0; i < count; ++i) {
531 struct dma_fence *fence = fences[i];
532 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
542 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
543 * or until timeout elapses
544 * @fences: array of fences to wait on
545 * @count: number of fences to wait on
546 * @intr: if true, do an interruptible wait
547 * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
548 * @idx: used to store the first signaled fence index, meaningful only on
551 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
552 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
555 * Synchronous waits for the first fence in the array to be signaled. The
556 * caller needs to hold a reference to all fences in the array, otherwise a
557 * fence might be freed before return, resulting in undefined behavior.
559 * See also dma_fence_wait() and dma_fence_wait_timeout().
562 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
563 bool intr, signed long timeout, uint32_t *idx)
565 struct default_wait_cb *cb;
566 signed long ret = timeout;
569 if (WARN_ON(!fences || !count || timeout < 0))
573 for (i = 0; i < count; ++i)
574 if (dma_fence_is_signaled(fences[i])) {
583 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
589 for (i = 0; i < count; ++i) {
590 struct dma_fence *fence = fences[i];
592 cb[i].task = current;
593 if (dma_fence_add_callback(fence, &cb[i].base,
594 dma_fence_default_wait_cb)) {
595 /* This fence is already signaled */
604 set_current_state(TASK_INTERRUPTIBLE);
606 set_current_state(TASK_UNINTERRUPTIBLE);
608 if (dma_fence_test_signaled_any(fences, count, idx))
611 ret = schedule_timeout(ret);
613 if (ret > 0 && intr && signal_pending(current))
617 __set_current_state(TASK_RUNNING);
621 dma_fence_remove_callback(fences[i], &cb[i].base);
628 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
631 * dma_fence_init - Initialize a custom fence.
632 * @fence: the fence to initialize
633 * @ops: the dma_fence_ops for operations on this fence
634 * @lock: the irqsafe spinlock to use for locking this fence
635 * @context: the execution context this fence is run on
636 * @seqno: a linear increasing sequence number for this context
638 * Initializes an allocated fence, the caller doesn't have to keep its
639 * refcount after committing with this fence, but it will need to hold a
640 * refcount again if &dma_fence_ops.enable_signaling gets called.
642 * context and seqno are used for easy comparison between fences, allowing
643 * to check which fence is later by simply using dma_fence_later().
646 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
647 spinlock_t *lock, u64 context, u64 seqno)
650 BUG_ON(!ops || !ops->get_driver_name || !ops->get_timeline_name);
652 kref_init(&fence->refcount);
654 INIT_LIST_HEAD(&fence->cb_list);
656 fence->context = context;
657 fence->seqno = seqno;
661 trace_dma_fence_init(fence);
663 EXPORT_SYMBOL(dma_fence_init);