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>
26 #define CREATE_TRACE_POINTS
27 #include <trace/events/dma_fence.h>
29 EXPORT_TRACEPOINT_SYMBOL(dma_fence_annotate_wait_on);
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 (!ktime_to_ns(fence->timestamp)) {
78 fence->timestamp = ktime_get();
79 smp_mb__before_atomic();
82 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
86 * we might have raced with the unlocked dma_fence_signal,
87 * still run through all callbacks
90 trace_dma_fence_signaled(fence);
92 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
93 list_del_init(&cur->node);
94 cur->func(fence, cur);
98 EXPORT_SYMBOL(dma_fence_signal_locked);
101 * dma_fence_signal - signal completion of a fence
102 * @fence: the fence to signal
104 * Signal completion for software callbacks on a fence, this will unblock
105 * dma_fence_wait() calls and run all the callbacks added with
106 * dma_fence_add_callback(). Can be called multiple times, but since a fence
107 * can only go from unsignaled to signaled state, it will only be effective
110 int dma_fence_signal(struct dma_fence *fence)
117 if (!ktime_to_ns(fence->timestamp)) {
118 fence->timestamp = ktime_get();
119 smp_mb__before_atomic();
122 if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
125 trace_dma_fence_signaled(fence);
127 if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags)) {
128 struct dma_fence_cb *cur, *tmp;
130 spin_lock_irqsave(fence->lock, flags);
131 list_for_each_entry_safe(cur, tmp, &fence->cb_list, node) {
132 list_del_init(&cur->node);
133 cur->func(fence, cur);
135 spin_unlock_irqrestore(fence->lock, flags);
139 EXPORT_SYMBOL(dma_fence_signal);
142 * dma_fence_wait_timeout - sleep until the fence gets signaled
143 * or until timeout elapses
144 * @fence: [in] the fence to wait on
145 * @intr: [in] if true, do an interruptible wait
146 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
148 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
149 * remaining timeout in jiffies on success. Other error values may be
150 * returned on custom implementations.
152 * Performs a synchronous wait on this fence. It is assumed the caller
153 * directly or indirectly (buf-mgr between reservation and committing)
154 * holds a reference to the fence, otherwise the fence might be
155 * freed before return, resulting in undefined behavior.
158 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
162 if (WARN_ON(timeout < 0))
165 trace_dma_fence_wait_start(fence);
166 ret = fence->ops->wait(fence, intr, timeout);
167 trace_dma_fence_wait_end(fence);
170 EXPORT_SYMBOL(dma_fence_wait_timeout);
172 void dma_fence_release(struct kref *kref)
174 struct dma_fence *fence =
175 container_of(kref, struct dma_fence, refcount);
177 trace_dma_fence_destroy(fence);
179 BUG_ON(!list_empty(&fence->cb_list));
181 if (fence->ops->release)
182 fence->ops->release(fence);
184 dma_fence_free(fence);
186 EXPORT_SYMBOL(dma_fence_release);
188 void dma_fence_free(struct dma_fence *fence)
190 kfree_rcu(fence, rcu);
192 EXPORT_SYMBOL(dma_fence_free);
195 * dma_fence_enable_sw_signaling - enable signaling on fence
196 * @fence: [in] the fence to enable
198 * this will request for sw signaling to be enabled, to make the fence
199 * complete as soon as possible
201 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
205 if (!test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
207 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
208 trace_dma_fence_enable_signal(fence);
210 spin_lock_irqsave(fence->lock, flags);
212 if (!fence->ops->enable_signaling(fence))
213 dma_fence_signal_locked(fence);
215 spin_unlock_irqrestore(fence->lock, flags);
218 EXPORT_SYMBOL(dma_fence_enable_sw_signaling);
221 * dma_fence_add_callback - add a callback to be called when the fence
223 * @fence: [in] the fence to wait on
224 * @cb: [in] the callback to register
225 * @func: [in] the function to call
227 * cb will be initialized by dma_fence_add_callback, no initialization
228 * by the caller is required. Any number of callbacks can be registered
229 * to a fence, but a callback can only be registered to one fence at a time.
231 * Note that the callback can be called from an atomic context. If
232 * fence is already signaled, this function will return -ENOENT (and
233 * *not* call the callback)
235 * Add a software callback to the fence. Same restrictions apply to
236 * refcount as it does to dma_fence_wait, however the caller doesn't need to
237 * keep a refcount to fence afterwards: when software access is enabled,
238 * the creator of the fence is required to keep the fence alive until
239 * after it signals with dma_fence_signal. The callback itself can be called
243 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb,
244 dma_fence_func_t func)
250 if (WARN_ON(!fence || !func))
253 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
254 INIT_LIST_HEAD(&cb->node);
258 spin_lock_irqsave(fence->lock, flags);
260 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
263 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
266 trace_dma_fence_enable_signal(fence);
268 if (!fence->ops->enable_signaling(fence)) {
269 dma_fence_signal_locked(fence);
276 list_add_tail(&cb->node, &fence->cb_list);
278 INIT_LIST_HEAD(&cb->node);
279 spin_unlock_irqrestore(fence->lock, flags);
283 EXPORT_SYMBOL(dma_fence_add_callback);
286 * dma_fence_get_status - returns the status upon completion
287 * @fence: [in] the dma_fence to query
289 * This wraps dma_fence_get_status_locked() to return the error status
290 * condition on a signaled fence. See dma_fence_get_status_locked() for more
293 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
294 * been signaled without an error condition, or a negative error code
295 * if the fence has been completed in err.
297 int dma_fence_get_status(struct dma_fence *fence)
302 spin_lock_irqsave(fence->lock, flags);
303 status = dma_fence_get_status_locked(fence);
304 spin_unlock_irqrestore(fence->lock, flags);
308 EXPORT_SYMBOL(dma_fence_get_status);
311 * dma_fence_remove_callback - remove a callback from the signaling list
312 * @fence: [in] the fence to wait on
313 * @cb: [in] the callback to remove
315 * Remove a previously queued callback from the fence. This function returns
316 * true if the callback is successfully removed, or false if the fence has
317 * already been signaled.
320 * Cancelling a callback should only be done if you really know what you're
321 * doing, since deadlocks and race conditions could occur all too easily. For
322 * this reason, it should only ever be done on hardware lockup recovery,
323 * with a reference held to the fence.
326 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
331 spin_lock_irqsave(fence->lock, flags);
333 ret = !list_empty(&cb->node);
335 list_del_init(&cb->node);
337 spin_unlock_irqrestore(fence->lock, flags);
341 EXPORT_SYMBOL(dma_fence_remove_callback);
343 struct default_wait_cb {
344 struct dma_fence_cb base;
345 struct task_struct *task;
349 dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
351 struct default_wait_cb *wait =
352 container_of(cb, struct default_wait_cb, base);
354 wake_up_state(wait->task, TASK_NORMAL);
358 * dma_fence_default_wait - default sleep until the fence gets signaled
359 * or until timeout elapses
360 * @fence: [in] the fence to wait on
361 * @intr: [in] if true, do an interruptible wait
362 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
364 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or the
365 * remaining timeout in jiffies on success. If timeout is zero the value one is
366 * returned if the fence is already signaled for consistency with other
367 * functions taking a jiffies timeout.
370 dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
372 struct default_wait_cb cb;
374 signed long ret = timeout ? timeout : 1;
377 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
380 spin_lock_irqsave(fence->lock, flags);
382 if (intr && signal_pending(current)) {
387 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
390 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
394 trace_dma_fence_enable_signal(fence);
396 if (!fence->ops->enable_signaling(fence)) {
397 dma_fence_signal_locked(fence);
402 cb.base.func = dma_fence_default_wait_cb;
404 list_add(&cb.base.node, &fence->cb_list);
406 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
408 __set_current_state(TASK_INTERRUPTIBLE);
410 __set_current_state(TASK_UNINTERRUPTIBLE);
411 spin_unlock_irqrestore(fence->lock, flags);
413 ret = schedule_timeout(ret);
415 spin_lock_irqsave(fence->lock, flags);
416 if (ret > 0 && intr && signal_pending(current))
420 if (!list_empty(&cb.base.node))
421 list_del(&cb.base.node);
422 __set_current_state(TASK_RUNNING);
425 spin_unlock_irqrestore(fence->lock, flags);
428 EXPORT_SYMBOL(dma_fence_default_wait);
431 dma_fence_test_signaled_any(struct dma_fence **fences, uint32_t count,
436 for (i = 0; i < count; ++i) {
437 struct dma_fence *fence = fences[i];
438 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
448 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
449 * or until timeout elapses
450 * @fences: [in] array of fences to wait on
451 * @count: [in] number of fences to wait on
452 * @intr: [in] if true, do an interruptible wait
453 * @timeout: [in] timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
454 * @idx: [out] the first signaled fence index, meaningful only on
457 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
458 * interrupted, 0 if the wait timed out, or the remaining timeout in jiffies
461 * Synchronous waits for the first fence in the array to be signaled. The
462 * caller needs to hold a reference to all fences in the array, otherwise a
463 * fence might be freed before return, resulting in undefined behavior.
466 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t count,
467 bool intr, signed long timeout, uint32_t *idx)
469 struct default_wait_cb *cb;
470 signed long ret = timeout;
473 if (WARN_ON(!fences || !count || timeout < 0))
477 for (i = 0; i < count; ++i)
478 if (dma_fence_is_signaled(fences[i])) {
487 cb = kcalloc(count, sizeof(struct default_wait_cb), GFP_KERNEL);
493 for (i = 0; i < count; ++i) {
494 struct dma_fence *fence = fences[i];
496 if (fence->ops->wait != dma_fence_default_wait) {
501 cb[i].task = current;
502 if (dma_fence_add_callback(fence, &cb[i].base,
503 dma_fence_default_wait_cb)) {
504 /* This fence is already signaled */
513 set_current_state(TASK_INTERRUPTIBLE);
515 set_current_state(TASK_UNINTERRUPTIBLE);
517 if (dma_fence_test_signaled_any(fences, count, idx))
520 ret = schedule_timeout(ret);
522 if (ret > 0 && intr && signal_pending(current))
526 __set_current_state(TASK_RUNNING);
530 dma_fence_remove_callback(fences[i], &cb[i].base);
537 EXPORT_SYMBOL(dma_fence_wait_any_timeout);
540 * dma_fence_init - Initialize a custom fence.
541 * @fence: [in] the fence to initialize
542 * @ops: [in] the dma_fence_ops for operations on this fence
543 * @lock: [in] the irqsafe spinlock to use for locking this fence
544 * @context: [in] the execution context this fence is run on
545 * @seqno: [in] a linear increasing sequence number for this context
547 * Initializes an allocated fence, the caller doesn't have to keep its
548 * refcount after committing with this fence, but it will need to hold a
549 * refcount again if dma_fence_ops.enable_signaling gets called. This can
550 * be used for other implementing other types of fence.
552 * context and seqno are used for easy comparison between fences, allowing
553 * to check which fence is later by simply using dma_fence_later.
556 dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
557 spinlock_t *lock, u64 context, unsigned seqno)
560 BUG_ON(!ops || !ops->wait || !ops->enable_signaling ||
561 !ops->get_driver_name || !ops->get_timeline_name);
563 kref_init(&fence->refcount);
565 INIT_LIST_HEAD(&fence->cb_list);
567 fence->context = context;
568 fence->seqno = seqno;
572 trace_dma_fence_init(fence);
574 EXPORT_SYMBOL(dma_fence_init);