2 * Fence mechanism for dma-buf and to allow for asynchronous dma access
4 * Copyright (C) 2012 Canonical Ltd
5 * Copyright (C) 2012 Texas Instruments
8 * Rob Clark <robdclark@gmail.com>
9 * Maarten Lankhorst <maarten.lankhorst@canonical.com>
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.
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
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>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/dma_fence.h>
30 EXPORT_TRACEPOINT_SYMBOL(dma_fence_emit);
31 EXPORT_TRACEPOINT_SYMBOL(dma_fence_enable_signal);
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.
39 static atomic64_t dma_fence_context_counter = ATOMIC64_INIT(0);
42 * dma_fence_context_alloc - allocate an array of fence contexts
43 * @num: [in] amount of contexts to allocate
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.
48 u64 dma_fence_context_alloc(unsigned num)
51 return atomic64_add_return(num, &dma_fence_context_counter) - num;
53 EXPORT_SYMBOL(dma_fence_context_alloc);
56 * dma_fence_signal_locked - signal completion of a fence
57 * @fence: the fence to signal
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
65 * Unlike dma_fence_signal, this function must be called with fence->lock held.
67 int dma_fence_signal_locked(struct dma_fence *fence)
69 struct dma_fence_cb *cur, *tmp;
72 lockdep_assert_held(fence->lock);
77 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
81 * we might have raced with the unlocked dma_fence_signal,
82 * still run through all callbacks
85 fence->timestamp = ktime_get();
86 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
87 trace_dma_fence_signaled(fence);
90 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
91 list_del_init(&cur->node);
92 cur->func(fence, cur);
96 EXPORT_SYMBOL(dma_fence_signal_locked);
99 * dma_fence_signal - signal completion of a fence
100 * @fence: the fence to signal
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
108 int dma_fence_signal(struct dma_fence *fence)
115 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
118 fence->timestamp = ktime_get();
119 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
120 trace_dma_fence_signaled(fence);
122 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
123 struct dma_fence_cb *cur, *tmp;
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);
130 spin_unlock_irqrestore(fence->lock, flags);
134 EXPORT_SYMBOL(dma_fence_signal);
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
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.
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.
153 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
157 if (WARN_ON(timeout < 0))
160 trace_dma_fence_wait_start(fence);
161 ret = fence->ops->wait(fence, intr, timeout);
162 trace_dma_fence_wait_end(fence);
165 EXPORT_SYMBOL(dma_fence_wait_timeout);
167 void dma_fence_release(struct kref *kref)
169 struct dma_fence *fence =
170 container_of(kref, struct dma_fence, refcount);
172 trace_dma_fence_destroy(fence);
174 /* Failed to signal before release, could be a refcounting issue */
175 WARN_ON(!list_empty(&fence->cb_list));
177 if (fence->ops->release)
178 fence->ops->release(fence);
180 dma_fence_free(fence);
182 EXPORT_SYMBOL(dma_fence_release);
184 void dma_fence_free(struct dma_fence *fence)
186 kfree_rcu(fence, rcu);
188 EXPORT_SYMBOL(dma_fence_free);
191 * dma_fence_enable_sw_signaling - enable signaling on fence
192 * @fence: [in] the fence to enable
194 * this will request for sw signaling to be enabled, to make the fence
195 * complete as soon as possible
197 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
201 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
203 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
204 trace_dma_fence_enable_signal(fence);
206 spin_lock_irqsave(fence->lock, flags);
208 if (!fence->ops->enable_signaling(fence))
209 dma_fence_signal_locked(fence);
211 spin_unlock_irqrestore(fence->lock, flags);
214 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
217 * dma_fence_add_callback - add a callback to be called when the fence
219 * @fence: [in] the fence to wait on
220 * @cb: [in] the callback to register
221 * @func: [in] the function to call
223 * cb will be initialized by dma_fence_add_callback, no initialization
224 * by the caller is required. Any number of callbacks can be registered
225 * to a fence, but a callback can only be registered to one fence at a time.
227 * Note that the callback can be called from an atomic context. If
228 * fence is already signaled, this function will return -ENOENT (and
229 * *not* call the callback)
231 * Add a software callback to the fence. Same restrictions apply to
232 * refcount as it does to dma_fence_wait, however the caller doesn't need to
233 * keep a refcount to fence afterwards: when software access is enabled,
234 * the creator of the fence is required to keep the fence alive until
235 * after it signals with dma_fence_signal. The callback itself can be called
238 * Returns 0 in case of success, -ENOENT if the fence is already signaled
239 * and -EINVAL in case of error.
241 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
242 dma_fence_func_t func)
248 if (WARN_ON(!fence || !func))
251 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
252 INIT_LIST_HEAD(&cb->node);
256 spin_lock_irqsave(fence->lock, flags);
258 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
261 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
264 trace_dma_fence_enable_signal(fence);
266 if (!fence->ops->enable_signaling(fence)) {
267 dma_fence_signal_locked(fence);
274 list_add_tail(&cb->node, &fence->cb_list);
276 INIT_LIST_HEAD(&cb->node);
277 spin_unlock_irqrestore(fence->lock, flags);
281 EXPORT_SYMBOL(dma_fence_add_callback);
284 * dma_fence_get_status - returns the status upon completion
285 * @fence: [in] the dma_fence to query
287 * This wraps dma_fence_get_status_locked() to return the error status
288 * condition on a signaled fence. See dma_fence_get_status_locked() for more
291 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
292 * been signaled without an error condition, or a negative error code
293 * if the fence has been completed in err.
295 int dma_fence_get_status(struct dma_fence *fence)
300 spin_lock_irqsave(fence->lock, flags);
301 status = dma_fence_get_status_locked(fence);
302 spin_unlock_irqrestore(fence->lock, flags);
306 EXPORT_SYMBOL(dma_fence_get_status);
309 * dma_fence_remove_callback - remove a callback from the signaling list
310 * @fence: [in] the fence to wait on
311 * @cb: [in] the callback to remove
313 * Remove a previously queued callback from the fence. This function returns
314 * true if the callback is successfully removed, or false if the fence has
315 * already been signaled.
318 * Cancelling a callback should only be done if you really know what you're
319 * doing, since deadlocks and race conditions could occur all too easily. For
320 * this reason, it should only ever be done on hardware lockup recovery,
321 * with a reference held to the fence.
324 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
329 spin_lock_irqsave(fence->lock, flags);
331 ret = !list_empty(&cb->node);
333 list_del_init(&cb->node);
335 spin_unlock_irqrestore(fence->lock, flags);
339 EXPORT_SYMBOL(dma_fence_remove_callback);
341 struct default_wait_cb {
342 struct dma_fence_cb base;
343 struct task_struct *task;
347 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
349 struct default_wait_cb *wait =
350 container_of(cb, struct default_wait_cb, base);
352 wake_up_state(wait->task, TASK_NORMAL);
356 * dma_fence_default_wait - default sleep until the fence gets signaled
357 * or until timeout elapses
358 * @fence: [in] the fence to wait on
359 * @intr: [in] if true, do an interruptible wait
360 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
362 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
363 * remaining timeout in jiffies on success. If timeout is zero the value one is
364 * returned if the fence is already signaled for consistency with other
365 * functions taking a jiffies timeout.
368 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
370 struct default_wait_cb cb;
372 signed long ret = timeout ? timeout : 1;
375 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
378 spin_lock_irqsave(fence->lock, flags);
380 if (intr && signal_pending(current)) {
385 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
388 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
392 trace_dma_fence_enable_signal(fence);
394 if (!fence->ops->enable_signaling(fence)) {
395 dma_fence_signal_locked(fence);
405 cb.base.func = dma_fence_default_wait_cb;
407 list_add(&cb.base.node, &fence->cb_list);
409 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
411 __set_current_state(TASK_INTERRUPTIBLE);
413 __set_current_state(TASK_UNINTERRUPTIBLE);
414 spin_unlock_irqrestore(fence->lock, flags);
416 ret = schedule_timeout(ret);
418 spin_lock_irqsave(fence->lock, flags);
419 if (ret > 0 && intr && signal_pending(current))
423 if (!list_empty(&cb.base.node))
424 list_del(&cb.base.node);
425 __set_current_state(TASK_RUNNING);
428 spin_unlock_irqrestore(fence->lock, flags);
431 EXPORT_SYMBOL(dma_fence_default_wait);
434 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
439 for (i = 0; i < count; ++i) {
440 struct dma_fence *fence = fences[i];
441 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
451 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
452 * or until timeout elapses
453 * @fences: [in] array of fences to wait on
454 * @count: [in] number of fences to wait on
455 * @intr: [in] if true, do an interruptible wait
456 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
457 * @idx: [out] the first signaled fence index, meaningful only on
460 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
461 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
464 * Synchronous waits for the first fence in the array to be signaled. The
465 * caller needs to hold a reference to all fences in the array, otherwise a
466 * fence might be freed before return, resulting in undefined behavior.
469 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
470 bool intr, signed long timeout, uint32_t *idx)
472 struct default_wait_cb *cb;
473 signed long ret = timeout;
476 if (WARN_ON(!fences || !count || timeout < 0))
480 for (i = 0; i < count; ++i)
481 if (dma_fence_is_signaled(fences[i])) {
490 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
496 for (i = 0; i < count; ++i) {
497 struct dma_fence *fence = fences[i];
499 if (fence->ops->wait != dma_fence_default_wait) {
504 cb[i].task = current;
505 if (dma_fence_add_callback(fence, &cb[i].base,
506 dma_fence_default_wait_cb)) {
507 /* This fence is already signaled */
516 set_current_state(TASK_INTERRUPTIBLE);
518 set_current_state(TASK_UNINTERRUPTIBLE);
520 if (dma_fence_test_signaled_any(fences, count, idx))
523 ret = schedule_timeout(ret);
525 if (ret > 0 && intr && signal_pending(current))
529 __set_current_state(TASK_RUNNING);
533 dma_fence_remove_callback(fences[i], &cb[i].base);
540 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
543 * dma_fence_init - Initialize a custom fence.
544 * @fence: [in] the fence to initialize
545 * @ops: [in] the dma_fence_ops for operations on this fence
546 * @lock: [in] the irqsafe spinlock to use for locking this fence
547 * @context: [in] the execution context this fence is run on
548 * @seqno: [in] a linear increasing sequence number for this context
550 * Initializes an allocated fence, the caller doesn't have to keep its
551 * refcount after committing with this fence, but it will need to hold a
552 * refcount again if dma_fence_ops.enable_signaling gets called. This can
553 * be used for other implementing other types of fence.
555 * context and seqno are used for easy comparison between fences, allowing
556 * to check which fence is later by simply using dma_fence_later.
559 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
560 spinlock_t *lock, u64 context, unsigned seqno)
563 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
564 !ops->get_driver_name || !ops->get_timeline_name);
566 kref_init(&fence->refcount);
568 INIT_LIST_HEAD(&fence->cb_list);
570 fence->context = context;
571 fence->seqno = seqno;
575 trace_dma_fence_init(fence);
577 EXPORT_SYMBOL(dma_fence_init);