Lines Matching refs:fence

16 #include <linux/dma-fence.h>
30 * fence context counter: each execution context should have its own
31 * fence context, this allows checking if fences belong to the same
44 * A fence is initialized using dma_fence_init() and completed using
59 * fence to be updated.
68 * DOC: fence cross-driver contract
94 * This means any code required for fence completion cannot acquire a
99 * callbacks. This means any code required for fence completion cannot
104 * for fence completeion cannot allocate memory with GFP_NOFS or GFP_NOIO.
113 static const char *dma_fence_stub_get_name(struct dma_fence *fence)
124 * dma_fence_get_stub - return a signaled fence
126 * Return a stub fence which is already signaled.
142 * dma_fence_context_alloc - allocate an array of fence contexts
145 * This function will return the first index of the number of fence contexts
146 * allocated. The fence context is used for setting &dma_fence.context to a
157 * DOC: fence signalling annotation
197 * prevents it from signalling the fence the previous thread is stuck waiting
218 * point where a fence is accessible to other threads, to the point where
249 * dma_fence_begin_signalling - begin a critical DMA fence signalling section
282 * dma_fence_end_signalling - end a critical DMA fence signalling section
314 * dma_fence_signal_timestamp_locked - signal completion of a fence
315 * @fence: the fence to signal
316 * @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain
318 * Signal completion for software callbacks on a fence, this will unblock
320 * dma_fence_add_callback(). Can be called multiple times, but since a fence
322 * only be effective the first time. Set the timestamp provided as the fence
328 * Returns 0 on success and a negative error value when @fence has been
331 int dma_fence_signal_timestamp_locked(struct dma_fence *fence, ktime_t timestamp)
336 lockdep_assert_held(fence->lock);
339 &fence->flags)))
343 list_replace(&fence->cb_list, &cb_list);
345 fence->timestamp = timestamp;
346 set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
347 trace_dma_fence_signaled(fence);
351 cur->func(fence, cur);
359 * dma_fence_signal_timestamp - signal completion of a fence
360 * @fence: the fence to signal
361 * @timestamp: fence signal timestamp in kernel's CLOCK_MONOTONIC time domain
363 * Signal completion for software callbacks on a fence, this will unblock
365 * dma_fence_add_callback(). Can be called multiple times, but since a fence
367 * only be effective the first time. Set the timestamp provided as the fence
370 * Returns 0 on success and a negative error value when @fence has been
373 int dma_fence_signal_timestamp(struct dma_fence *fence, ktime_t timestamp)
378 if (!fence) {
382 spin_lock_irqsave(fence->lock, flags);
383 ret = dma_fence_signal_timestamp_locked(fence, timestamp);
384 spin_unlock_irqrestore(fence->lock, flags);
390 * dma_fence_signal_locked - signal completion of a fence
391 * @fence: the fence to signal
393 * Signal completion for software callbacks on a fence, this will unblock
395 * dma_fence_add_callback(). Can be called multiple times, but since a fence
402 * Returns 0 on success and a negative error value when @fence has been
405 int dma_fence_signal_locked(struct dma_fence *fence)
407 return dma_fence_signal_timestamp_locked(fence, ktime_get());
412 * dma_fence_signal - signal completion of a fence
413 * @fence: the fence to signal
415 * Signal completion for software callbacks on a fence, this will unblock
417 * dma_fence_add_callback(). Can be called multiple times, but since a fence
421 * Returns 0 on success and a negative error value when @fence has been
424 int dma_fence_signal(struct dma_fence *fence)
430 if (!fence) {
436 spin_lock_irqsave(fence->lock, flags);
437 ret = dma_fence_signal_timestamp_locked(fence, ktime_get());
438 spin_unlock_irqrestore(fence->lock, flags);
447 * dma_fence_wait_timeout - sleep until the fence gets signaled
449 * @fence: the fence to wait on
457 * Performs a synchronous wait on this fence. It is assumed the caller
459 * holds a reference to the fence, otherwise the fence might be
464 signed long dma_fence_wait_timeout(struct dma_fence *fence, bool intr, signed long timeout)
476 trace_dma_fence_wait_start(fence);
477 if (fence->ops->wait) {
478 ret = fence->ops->wait(fence, intr, timeout);
480 ret = dma_fence_default_wait(fence, intr, timeout);
482 trace_dma_fence_wait_end(fence);
496 struct dma_fence *fence = container_of(kref, struct dma_fence, refcount);
498 trace_dma_fence_destroy(fence);
500 if (WARN(!list_empty(&fence->cb_list) && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags),
501 "Fence %s:%s:%llx:%llx released with pending signals!\n", fence->ops->get_driver_name(fence),
502 fence->ops->get_timeline_name(fence), fence->context, fence->seqno)) {
512 spin_lock_irqsave(fence->lock, flags);
513 fence->error = -EDEADLK;
514 dma_fence_signal_locked(fence);
515 spin_unlock_irqrestore(fence->lock, flags);
518 if (fence->ops->release) {
519 fence->ops->release(fence);
521 dma_fence_free(fence);
528 * @fence: fence to release
531 * kfree_rcu() on @fence.
533 void dma_fence_free(struct dma_fence *fence)
535 kfree_rcu(fence, rcu);
539 static bool _dma_fence_enable_signaling(struct dma_fence *fence)
543 lockdep_assert_held(fence->lock);
545 was_set = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags);
547 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
551 if (!was_set && fence->ops->enable_signaling) {
552 trace_dma_fence_enable_signal(fence);
554 if (!fence->ops->enable_signaling(fence)) {
555 dma_fence_signal_locked(fence);
564 * dma_fence_enable_sw_signaling - enable signaling on fence
565 * @fence: the fence to enable
567 * This will request for sw signaling to be enabled, to make the fence
571 void dma_fence_enable_sw_signaling(struct dma_fence *fence)
575 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
579 spin_lock_irqsave(fence->lock, flags);
580 _dma_fence_enable_signaling(fence);
581 spin_unlock_irqrestore(fence->lock, flags);
586 * dma_fence_add_callback - add a callback to be called when the fence
588 * @fence: the fence to wait on
594 * to a fence, but a callback can only be registered to one fence at a time.
597 * fence is already signaled, this function will return -ENOENT (and
600 * Add a software callback to the fence. Same restrictions apply to
602 * keep a refcount to fence afterward dma_fence_add_callback() has returned:
603 * when software access is enabled, the creator of the fence is required to keep
604 * the fence alive until after it signals with dma_fence_signal(). The callback
607 * Returns 0 in case of success, -ENOENT if the fence is already signaled
610 int dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *cb, dma_fence_func_t func)
615 if (WARN_ON(!fence || !func)) {
619 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
624 spin_lock_irqsave(fence->lock, flags);
626 if (_dma_fence_enable_signaling(fence)) {
628 list_add_tail(&cb->node, &fence->cb_list);
634 spin_unlock_irqrestore(fence->lock, flags);
642 * @fence: the dma_fence to query
645 * condition on a signaled fence. See dma_fence_get_status_locked() for more
648 * Returns 0 if the fence has not yet been signaled, 1 if the fence has
650 * if the fence has been completed in err.
652 int dma_fence_get_status(struct dma_fence *fence)
657 spin_lock_irqsave(fence->lock, flags);
658 status = dma_fence_get_status_locked(fence);
659 spin_unlock_irqrestore(fence->lock, flags);
667 * @fence: the fence to wait on
670 * Remove a previously queued callback from the fence. This function returns
671 * true if the callback is successfully removed, or false if the fence has
678 * with a reference held to the fence.
680 * Behaviour is undefined if @cb has not been added to @fence using
683 bool dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *cb)
688 spin_lock_irqsave(fence->lock, flags);
695 spin_unlock_irqrestore(fence->lock, flags);
706 static void dma_fence_default_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
714 * dma_fence_default_wait - default sleep until the fence gets signaled
716 * @fence: the fence to wait on
722 * returned if the fence is already signaled for consistency with other
725 signed long dma_fence_default_wait(struct dma_fence *fence, bool intr, signed long timeout)
731 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
735 spin_lock_irqsave(fence->lock, flags);
742 if (!_dma_fence_enable_signaling(fence)) {
753 list_add(&cb.base.node, &fence->cb_list);
755 while (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags) && ret > 0) {
761 spin_unlock_irqrestore(fence->lock, flags);
765 spin_lock_irqsave(fence->lock, flags);
777 spin_unlock_irqrestore(fence->lock, flags);
787 struct dma_fence *fence = fences[i];
788 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
799 * dma_fence_wait_any_timeout - sleep until any fence gets signaled
805 * @idx: used to store the first signaled fence index, meaningful only on
808 * Returns -EINVAL on custom fence wait implementation, -ERESTARTSYS if
812 * Synchronous waits for the first fence in the array to be signaled. The
814 * fence might be freed before return, resulting in undefined behavior.
846 struct dma_fence *fence = fences[i];
848 if (dma_fence_add_callback(fence, &cb[i].base, dma_fence_default_wait_cb)) {
885 * dma_fence_init - Initialize a custom fence.
886 * @fence: the fence to initialize
887 * @ops: the dma_fence_ops for operations on this fence
888 * @lock: the irqsafe spinlock to use for locking this fence
889 * @context: the execution context this fence is run on
892 * Initializes an allocated fence, the caller doesn't have to keep its
893 * refcount after committing with this fence, but it will need to hold a
897 * to check which fence is later by simply using dma_fence_later().
899 void dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops, spinlock_t *lock, u64 context, u64 seqno)
904 kref_init(&fence->refcount);
905 fence->ops = ops;
906 INIT_LIST_HEAD(&fence->cb_list);
907 fence->lock = lock;
908 fence->context = context;
909 fence->seqno = seqno;
910 fence->flags = 0UL;
911 fence->error = 0;
913 trace_dma_fence_init(fence);