Home | History | Annotate | Line # | Download | only in linux
linux_dma_fence.c revision 1.30
      1  1.30  riastrad /*	$NetBSD: linux_dma_fence.c,v 1.30 2021/12/19 12:31:11 riastradh Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad /*-
      4   1.1  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5   1.1  riastrad  * All rights reserved.
      6   1.1  riastrad  *
      7   1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  riastrad  * by Taylor R. Campbell.
      9   1.1  riastrad  *
     10   1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11   1.1  riastrad  * modification, are permitted provided that the following conditions
     12   1.1  riastrad  * are met:
     13   1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14   1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15   1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18   1.1  riastrad  *
     19   1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  riastrad  */
     31   1.1  riastrad 
     32   1.1  riastrad #include <sys/cdefs.h>
     33  1.30  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_dma_fence.c,v 1.30 2021/12/19 12:31:11 riastradh Exp $");
     34   1.1  riastrad 
     35   1.1  riastrad #include <sys/atomic.h>
     36   1.1  riastrad #include <sys/condvar.h>
     37   1.1  riastrad #include <sys/queue.h>
     38   1.1  riastrad 
     39   1.1  riastrad #include <linux/atomic.h>
     40   1.2  riastrad #include <linux/dma-fence.h>
     41   1.1  riastrad #include <linux/errno.h>
     42   1.1  riastrad #include <linux/kref.h>
     43   1.1  riastrad #include <linux/sched.h>
     44   1.1  riastrad #include <linux/spinlock.h>
     45   1.1  riastrad 
     46  1.24  riastrad #define	FENCE_MAGIC_GOOD	0x607ba424048c37e5ULL
     47  1.24  riastrad #define	FENCE_MAGIC_BAD		0x7641ca721344505fULL
     48  1.24  riastrad 
     49   1.1  riastrad /*
     50   1.2  riastrad  * linux_dma_fence_trace
     51   1.1  riastrad  *
     52   1.2  riastrad  *	True if we print DMA_FENCE_TRACE messages, false if not.  These
     53   1.2  riastrad  *	are extremely noisy, too much even for AB_VERBOSE and AB_DEBUG
     54   1.2  riastrad  *	in boothowto.
     55   1.1  riastrad  */
     56   1.2  riastrad int	linux_dma_fence_trace = 0;
     57   1.1  riastrad 
     58   1.1  riastrad /*
     59   1.2  riastrad  * dma_fence_referenced_p(fence)
     60   1.1  riastrad  *
     61   1.1  riastrad  *	True if fence has a positive reference count.  True after
     62   1.2  riastrad  *	dma_fence_init; after the last dma_fence_put, this becomes
     63  1.24  riastrad  *	false.  The fence must have been initialized and must not have
     64  1.24  riastrad  *	been destroyed.
     65   1.1  riastrad  */
     66   1.1  riastrad static inline bool __diagused
     67   1.2  riastrad dma_fence_referenced_p(struct dma_fence *fence)
     68   1.1  riastrad {
     69   1.1  riastrad 
     70  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
     71  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
     72  1.24  riastrad 
     73   1.1  riastrad 	return kref_referenced_p(&fence->refcount);
     74   1.1  riastrad }
     75   1.1  riastrad 
     76   1.1  riastrad /*
     77   1.2  riastrad  * dma_fence_init(fence, ops, lock, context, seqno)
     78   1.1  riastrad  *
     79   1.2  riastrad  *	Initialize fence.  Caller should call dma_fence_destroy when
     80   1.2  riastrad  *	done, after all references have been released.
     81   1.1  riastrad  */
     82   1.1  riastrad void
     83   1.2  riastrad dma_fence_init(struct dma_fence *fence, const struct dma_fence_ops *ops,
     84   1.2  riastrad     spinlock_t *lock, unsigned context, unsigned seqno)
     85   1.1  riastrad {
     86   1.1  riastrad 
     87   1.1  riastrad 	kref_init(&fence->refcount);
     88   1.1  riastrad 	fence->lock = lock;
     89   1.1  riastrad 	fence->flags = 0;
     90   1.1  riastrad 	fence->context = context;
     91   1.1  riastrad 	fence->seqno = seqno;
     92   1.1  riastrad 	fence->ops = ops;
     93  1.18  riastrad 	fence->error = 0;
     94   1.1  riastrad 	TAILQ_INIT(&fence->f_callbacks);
     95   1.2  riastrad 	cv_init(&fence->f_cv, "dmafence");
     96  1.24  riastrad 
     97  1.24  riastrad #ifdef DIAGNOSTIC
     98  1.24  riastrad 	fence->f_magic = FENCE_MAGIC_GOOD;
     99  1.24  riastrad #endif
    100   1.1  riastrad }
    101   1.1  riastrad 
    102   1.1  riastrad /*
    103  1.18  riastrad  * dma_fence_reset(fence)
    104  1.18  riastrad  *
    105  1.18  riastrad  *	Ensure fence is in a quiescent state.  Allowed either for newly
    106  1.18  riastrad  *	initialized or freed fences, but not fences with more than one
    107  1.18  riastrad  *	reference.
    108  1.18  riastrad  *
    109  1.18  riastrad  *	XXX extension to Linux API
    110  1.18  riastrad  */
    111  1.18  riastrad void
    112  1.18  riastrad dma_fence_reset(struct dma_fence *fence, const struct dma_fence_ops *ops,
    113  1.18  riastrad     spinlock_t *lock, unsigned context, unsigned seqno)
    114  1.18  riastrad {
    115  1.18  riastrad 
    116  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
    117  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
    118  1.18  riastrad 	KASSERT(kref_read(&fence->refcount) == 0 ||
    119  1.18  riastrad 	    kref_read(&fence->refcount) == 1);
    120  1.18  riastrad 	KASSERT(TAILQ_EMPTY(&fence->f_callbacks));
    121  1.18  riastrad 	KASSERT(fence->lock == lock);
    122  1.18  riastrad 	KASSERT(fence->ops == ops);
    123  1.18  riastrad 
    124  1.18  riastrad 	kref_init(&fence->refcount);
    125  1.18  riastrad 	fence->flags = 0;
    126  1.18  riastrad 	fence->context = context;
    127  1.18  riastrad 	fence->seqno = seqno;
    128  1.18  riastrad 	fence->error = 0;
    129  1.18  riastrad }
    130  1.18  riastrad 
    131  1.18  riastrad /*
    132   1.2  riastrad  * dma_fence_destroy(fence)
    133   1.1  riastrad  *
    134   1.2  riastrad  *	Clean up memory initialized with dma_fence_init.  This is meant
    135   1.2  riastrad  *	to be used after a fence release callback.
    136  1.19  riastrad  *
    137  1.19  riastrad  *	XXX extension to Linux API
    138   1.1  riastrad  */
    139   1.1  riastrad void
    140   1.2  riastrad dma_fence_destroy(struct dma_fence *fence)
    141   1.1  riastrad {
    142   1.1  riastrad 
    143   1.2  riastrad 	KASSERT(!dma_fence_referenced_p(fence));
    144   1.1  riastrad 
    145  1.24  riastrad #ifdef DIAGNOSTIC
    146  1.24  riastrad 	fence->f_magic = FENCE_MAGIC_BAD;
    147  1.24  riastrad #endif
    148  1.24  riastrad 
    149   1.1  riastrad 	KASSERT(TAILQ_EMPTY(&fence->f_callbacks));
    150   1.1  riastrad 	cv_destroy(&fence->f_cv);
    151   1.1  riastrad }
    152   1.1  riastrad 
    153   1.1  riastrad static void
    154   1.2  riastrad dma_fence_free_cb(struct rcu_head *rcu)
    155   1.1  riastrad {
    156  1.19  riastrad 	struct dma_fence *fence = container_of(rcu, struct dma_fence, rcu);
    157   1.1  riastrad 
    158   1.2  riastrad 	KASSERT(!dma_fence_referenced_p(fence));
    159   1.1  riastrad 
    160   1.2  riastrad 	dma_fence_destroy(fence);
    161   1.1  riastrad 	kfree(fence);
    162   1.1  riastrad }
    163   1.1  riastrad 
    164   1.1  riastrad /*
    165   1.2  riastrad  * dma_fence_free(fence)
    166   1.1  riastrad  *
    167   1.1  riastrad  *	Schedule fence to be destroyed and then freed with kfree after
    168   1.1  riastrad  *	any pending RCU read sections on all CPUs have completed.
    169   1.1  riastrad  *	Caller must guarantee all references have been released.  This
    170   1.1  riastrad  *	is meant to be used after a fence release callback.
    171   1.1  riastrad  *
    172   1.1  riastrad  *	NOTE: Callers assume kfree will be used.  We don't even use
    173   1.1  riastrad  *	kmalloc to allocate these -- caller is expected to allocate
    174   1.2  riastrad  *	memory with kmalloc to be initialized with dma_fence_init.
    175   1.1  riastrad  */
    176   1.1  riastrad void
    177   1.2  riastrad dma_fence_free(struct dma_fence *fence)
    178   1.1  riastrad {
    179   1.1  riastrad 
    180   1.2  riastrad 	KASSERT(!dma_fence_referenced_p(fence));
    181   1.1  riastrad 
    182  1.19  riastrad 	call_rcu(&fence->rcu, &dma_fence_free_cb);
    183   1.1  riastrad }
    184   1.1  riastrad 
    185   1.1  riastrad /*
    186   1.2  riastrad  * dma_fence_context_alloc(n)
    187   1.1  riastrad  *
    188   1.1  riastrad  *	Return the first of a contiguous sequence of unique
    189   1.1  riastrad  *	identifiers, at least until the system wraps around.
    190   1.1  riastrad  */
    191   1.1  riastrad unsigned
    192   1.2  riastrad dma_fence_context_alloc(unsigned n)
    193   1.1  riastrad {
    194   1.1  riastrad 	static volatile unsigned next_context = 0;
    195   1.1  riastrad 
    196   1.1  riastrad 	return atomic_add_int_nv(&next_context, n) - n;
    197   1.1  riastrad }
    198   1.1  riastrad 
    199   1.1  riastrad /*
    200   1.2  riastrad  * dma_fence_is_later(a, b)
    201   1.1  riastrad  *
    202   1.1  riastrad  *	True if the sequence number of fence a is later than the
    203   1.1  riastrad  *	sequence number of fence b.  Since sequence numbers wrap
    204   1.1  riastrad  *	around, we define this to mean that the sequence number of
    205   1.1  riastrad  *	fence a is no more than INT_MAX past the sequence number of
    206   1.1  riastrad  *	fence b.
    207   1.1  riastrad  *
    208   1.1  riastrad  *	The two fences must have the same context.
    209   1.1  riastrad  */
    210   1.1  riastrad bool
    211   1.2  riastrad dma_fence_is_later(struct dma_fence *a, struct dma_fence *b)
    212   1.1  riastrad {
    213   1.1  riastrad 
    214  1.24  riastrad 	KASSERTMSG(a->f_magic != FENCE_MAGIC_BAD, "fence %p", a);
    215  1.24  riastrad 	KASSERTMSG(a->f_magic == FENCE_MAGIC_GOOD, "fence %p", a);
    216  1.24  riastrad 	KASSERTMSG(b->f_magic != FENCE_MAGIC_BAD, "fence %p", b);
    217  1.24  riastrad 	KASSERTMSG(b->f_magic == FENCE_MAGIC_GOOD, "fence %p", b);
    218   1.1  riastrad 	KASSERTMSG(a->context == b->context, "incommensurate fences"
    219   1.1  riastrad 	    ": %u @ %p =/= %u @ %p", a->context, a, b->context, b);
    220   1.1  riastrad 
    221   1.1  riastrad 	return a->seqno - b->seqno < INT_MAX;
    222   1.1  riastrad }
    223   1.1  riastrad 
    224  1.30  riastrad static const char *dma_fence_stub_name(struct dma_fence *f)
    225  1.30  riastrad {
    226  1.30  riastrad 
    227  1.30  riastrad 	return "stub";
    228  1.30  riastrad }
    229  1.30  riastrad 
    230  1.30  riastrad static const struct dma_fence_ops dma_fence_stub_ops = {
    231  1.30  riastrad 	.get_driver_name = dma_fence_stub_name,
    232  1.30  riastrad 	.get_timeline_name = dma_fence_stub_name,
    233  1.30  riastrad };
    234  1.30  riastrad 
    235   1.1  riastrad /*
    236   1.9  riastrad  * dma_fence_get_stub()
    237   1.9  riastrad  *
    238   1.9  riastrad  *	Return a dma fence that is always already signalled.
    239   1.9  riastrad  */
    240   1.9  riastrad struct dma_fence *
    241   1.9  riastrad dma_fence_get_stub(void)
    242   1.9  riastrad {
    243   1.9  riastrad 	/*
    244   1.9  riastrad 	 * XXX This probably isn't good enough -- caller may try
    245   1.9  riastrad 	 * operations on this that require the lock, which will
    246   1.9  riastrad 	 * require us to create and destroy the lock on module
    247   1.9  riastrad 	 * load/unload.
    248   1.9  riastrad 	 */
    249   1.9  riastrad 	static struct dma_fence fence = {
    250   1.9  riastrad 		.refcount = {1}, /* always referenced */
    251   1.9  riastrad 		.flags = 1u << DMA_FENCE_FLAG_SIGNALED_BIT,
    252  1.30  riastrad 		.ops = &dma_fence_stub_ops,
    253  1.29  riastrad #ifdef DIAGNOSTIC
    254  1.29  riastrad 		.f_magic = FENCE_MAGIC_GOOD,
    255  1.29  riastrad #endif
    256   1.9  riastrad 	};
    257   1.9  riastrad 
    258   1.9  riastrad 	return dma_fence_get(&fence);
    259   1.9  riastrad }
    260   1.9  riastrad 
    261   1.9  riastrad /*
    262   1.2  riastrad  * dma_fence_get(fence)
    263   1.1  riastrad  *
    264  1.26  riastrad  *	Acquire a reference to fence and return it, or return NULL if
    265  1.26  riastrad  *	fence is NULL.  The fence, if nonnull, must not be being
    266  1.26  riastrad  *	destroyed.
    267   1.1  riastrad  */
    268   1.2  riastrad struct dma_fence *
    269   1.2  riastrad dma_fence_get(struct dma_fence *fence)
    270   1.1  riastrad {
    271   1.1  riastrad 
    272  1.26  riastrad 	if (fence == NULL)
    273  1.26  riastrad 		return NULL;
    274  1.26  riastrad 
    275  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
    276  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
    277  1.24  riastrad 
    278  1.26  riastrad 	kref_get(&fence->refcount);
    279   1.1  riastrad 	return fence;
    280   1.1  riastrad }
    281   1.1  riastrad 
    282   1.1  riastrad /*
    283   1.2  riastrad  * dma_fence_get_rcu(fence)
    284   1.1  riastrad  *
    285   1.1  riastrad  *	Attempt to acquire a reference to a fence that may be about to
    286   1.1  riastrad  *	be destroyed, during a read section.  Return the fence on
    287  1.26  riastrad  *	success, or NULL on failure.  The fence must be nonnull.
    288   1.1  riastrad  */
    289   1.2  riastrad struct dma_fence *
    290   1.2  riastrad dma_fence_get_rcu(struct dma_fence *fence)
    291   1.1  riastrad {
    292   1.1  riastrad 
    293   1.8  riastrad 	__insn_barrier();
    294  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
    295  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
    296   1.1  riastrad 	if (!kref_get_unless_zero(&fence->refcount))
    297   1.1  riastrad 		return NULL;
    298   1.1  riastrad 	return fence;
    299   1.1  riastrad }
    300   1.1  riastrad 
    301   1.3  riastrad /*
    302   1.3  riastrad  * dma_fence_get_rcu_safe(fencep)
    303   1.3  riastrad  *
    304   1.3  riastrad  *	Attempt to acquire a reference to the fence *fencep, which may
    305   1.3  riastrad  *	be about to be destroyed, during a read section.  If the value
    306   1.3  riastrad  *	of *fencep changes after we read *fencep but before we
    307   1.3  riastrad  *	increment its reference count, retry.  Return *fencep on
    308   1.3  riastrad  *	success, or NULL on failure.
    309   1.3  riastrad  */
    310   1.3  riastrad struct dma_fence *
    311   1.7  riastrad dma_fence_get_rcu_safe(struct dma_fence *volatile const *fencep)
    312   1.3  riastrad {
    313   1.3  riastrad 	struct dma_fence *fence, *fence0;
    314   1.3  riastrad 
    315   1.3  riastrad retry:
    316   1.3  riastrad 	fence = *fencep;
    317   1.3  riastrad 
    318   1.3  riastrad 	/* Load fence only once.  */
    319   1.3  riastrad 	__insn_barrier();
    320   1.3  riastrad 
    321   1.3  riastrad 	/* If there's nothing there, give up.  */
    322   1.3  riastrad 	if (fence == NULL)
    323   1.3  riastrad 		return NULL;
    324   1.3  riastrad 
    325   1.3  riastrad 	/* Make sure we don't load stale fence guts.  */
    326   1.3  riastrad 	membar_datadep_consumer();
    327   1.3  riastrad 
    328   1.3  riastrad 	/* Try to acquire a reference.  If we can't, try again.  */
    329   1.3  riastrad 	if (!dma_fence_get_rcu(fence))
    330   1.3  riastrad 		goto retry;
    331   1.3  riastrad 
    332   1.3  riastrad 	/*
    333   1.3  riastrad 	 * Confirm that it's still the same fence.  If not, release it
    334   1.3  riastrad 	 * and retry.
    335   1.3  riastrad 	 */
    336   1.3  riastrad 	fence0 = *fencep;
    337   1.3  riastrad 	__insn_barrier();
    338   1.3  riastrad 	if (fence != fence0) {
    339   1.3  riastrad 		dma_fence_put(fence);
    340   1.3  riastrad 		goto retry;
    341   1.3  riastrad 	}
    342   1.3  riastrad 
    343   1.3  riastrad 	/* Success!  */
    344  1.24  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    345   1.3  riastrad 	return fence;
    346   1.3  riastrad }
    347   1.3  riastrad 
    348   1.1  riastrad static void
    349   1.2  riastrad dma_fence_release(struct kref *refcount)
    350   1.1  riastrad {
    351   1.2  riastrad 	struct dma_fence *fence = container_of(refcount, struct dma_fence,
    352   1.2  riastrad 	    refcount);
    353   1.1  riastrad 
    354  1.23  riastrad 	KASSERTMSG(TAILQ_EMPTY(&fence->f_callbacks),
    355  1.23  riastrad 	    "fence %p has pending callbacks", fence);
    356   1.2  riastrad 	KASSERT(!dma_fence_referenced_p(fence));
    357   1.1  riastrad 
    358   1.1  riastrad 	if (fence->ops->release)
    359   1.1  riastrad 		(*fence->ops->release)(fence);
    360   1.1  riastrad 	else
    361   1.2  riastrad 		dma_fence_free(fence);
    362   1.1  riastrad }
    363   1.1  riastrad 
    364   1.1  riastrad /*
    365   1.2  riastrad  * dma_fence_put(fence)
    366   1.1  riastrad  *
    367   1.1  riastrad  *	Release a reference to fence.  If this was the last one, call
    368   1.1  riastrad  *	the fence's release callback.
    369   1.1  riastrad  */
    370   1.1  riastrad void
    371   1.2  riastrad dma_fence_put(struct dma_fence *fence)
    372   1.1  riastrad {
    373   1.1  riastrad 
    374   1.1  riastrad 	if (fence == NULL)
    375   1.1  riastrad 		return;
    376   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    377   1.2  riastrad 	kref_put(&fence->refcount, &dma_fence_release);
    378   1.1  riastrad }
    379   1.1  riastrad 
    380   1.1  riastrad /*
    381   1.2  riastrad  * dma_fence_ensure_signal_enabled(fence)
    382   1.1  riastrad  *
    383   1.1  riastrad  *	Internal subroutine.  If the fence was already signalled,
    384   1.1  riastrad  *	return -ENOENT.  Otherwise, if the enable signalling callback
    385   1.1  riastrad  *	has not been called yet, call it.  If fails, signal the fence
    386   1.1  riastrad  *	and return -ENOENT.  If it succeeds, or if it had already been
    387   1.1  riastrad  *	called, return zero to indicate success.
    388   1.1  riastrad  *
    389   1.1  riastrad  *	Caller must hold the fence's lock.
    390   1.1  riastrad  */
    391   1.1  riastrad static int
    392   1.2  riastrad dma_fence_ensure_signal_enabled(struct dma_fence *fence)
    393   1.1  riastrad {
    394  1.20  riastrad 	bool already_enabled;
    395   1.1  riastrad 
    396   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    397   1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    398   1.1  riastrad 
    399  1.20  riastrad 	/* Determine whether signalling was enabled, and enable it.  */
    400  1.20  riastrad 	already_enabled = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
    401  1.20  riastrad 	    &fence->flags);
    402  1.20  riastrad 
    403   1.1  riastrad 	/* If the fence was already signalled, fail with -ENOENT.  */
    404   1.2  riastrad 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
    405   1.1  riastrad 		return -ENOENT;
    406   1.1  riastrad 
    407   1.1  riastrad 	/*
    408  1.20  riastrad 	 * Otherwise, if it wasn't enabled yet, try to enable
    409  1.20  riastrad 	 * signalling, or fail if the fence doesn't support that.
    410   1.1  riastrad 	 */
    411  1.20  riastrad 	if (!already_enabled) {
    412  1.20  riastrad 		if (fence->ops->enable_signaling == NULL)
    413  1.20  riastrad 			return -ENOENT;
    414  1.20  riastrad 		if (!(*fence->ops->enable_signaling)(fence)) {
    415  1.20  riastrad 			/* If it failed, signal and return -ENOENT.  */
    416  1.20  riastrad 			dma_fence_signal_locked(fence);
    417  1.20  riastrad 			return -ENOENT;
    418  1.20  riastrad 		}
    419   1.1  riastrad 	}
    420   1.1  riastrad 
    421   1.1  riastrad 	/* Success!  */
    422   1.1  riastrad 	return 0;
    423   1.1  riastrad }
    424   1.1  riastrad 
    425   1.1  riastrad /*
    426   1.2  riastrad  * dma_fence_add_callback(fence, fcb, fn)
    427   1.1  riastrad  *
    428   1.1  riastrad  *	If fence has been signalled, return -ENOENT.  If the enable
    429   1.1  riastrad  *	signalling callback hasn't been called yet, call it; if it
    430   1.1  riastrad  *	fails, return -ENOENT.  Otherwise, arrange to call fn(fence,
    431   1.1  riastrad  *	fcb) when it is signalled, and return 0.
    432   1.1  riastrad  *
    433   1.1  riastrad  *	The fence uses memory allocated by the caller in fcb from the
    434   1.2  riastrad  *	time of dma_fence_add_callback either to the time of
    435   1.2  riastrad  *	dma_fence_remove_callback, or just before calling fn.
    436   1.1  riastrad  */
    437   1.1  riastrad int
    438   1.2  riastrad dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *fcb,
    439   1.2  riastrad     dma_fence_func_t fn)
    440   1.1  riastrad {
    441   1.1  riastrad 	int ret;
    442   1.1  riastrad 
    443   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    444   1.1  riastrad 
    445   1.1  riastrad 	/* Optimistically try to skip the lock if it's already signalled.  */
    446   1.2  riastrad 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) {
    447   1.1  riastrad 		ret = -ENOENT;
    448   1.1  riastrad 		goto out0;
    449   1.1  riastrad 	}
    450   1.1  riastrad 
    451   1.1  riastrad 	/* Acquire the lock.  */
    452   1.1  riastrad 	spin_lock(fence->lock);
    453   1.1  riastrad 
    454   1.1  riastrad 	/* Ensure signalling is enabled, or fail if we can't.  */
    455   1.2  riastrad 	ret = dma_fence_ensure_signal_enabled(fence);
    456   1.1  riastrad 	if (ret)
    457   1.1  riastrad 		goto out1;
    458   1.1  riastrad 
    459   1.1  riastrad 	/* Insert the callback.  */
    460   1.4  riastrad 	fcb->func = fn;
    461   1.1  riastrad 	TAILQ_INSERT_TAIL(&fence->f_callbacks, fcb, fcb_entry);
    462   1.1  riastrad 	fcb->fcb_onqueue = true;
    463  1.21  riastrad 	ret = 0;
    464   1.1  riastrad 
    465   1.1  riastrad 	/* Release the lock and we're done.  */
    466   1.1  riastrad out1:	spin_unlock(fence->lock);
    467  1.21  riastrad out0:	if (ret) {
    468  1.21  riastrad 		fcb->func = NULL;
    469  1.21  riastrad 		fcb->fcb_onqueue = false;
    470  1.21  riastrad 	}
    471  1.21  riastrad 	return ret;
    472   1.1  riastrad }
    473   1.1  riastrad 
    474   1.1  riastrad /*
    475   1.2  riastrad  * dma_fence_remove_callback(fence, fcb)
    476   1.1  riastrad  *
    477   1.1  riastrad  *	Remove the callback fcb from fence.  Return true if it was
    478   1.1  riastrad  *	removed from the list, or false if it had already run and so
    479   1.1  riastrad  *	was no longer queued anyway.  Caller must have already called
    480   1.2  riastrad  *	dma_fence_add_callback(fence, fcb).
    481   1.1  riastrad  */
    482   1.1  riastrad bool
    483   1.2  riastrad dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *fcb)
    484   1.1  riastrad {
    485   1.1  riastrad 	bool onqueue;
    486   1.1  riastrad 
    487   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    488   1.1  riastrad 
    489   1.1  riastrad 	spin_lock(fence->lock);
    490   1.1  riastrad 	onqueue = fcb->fcb_onqueue;
    491   1.1  riastrad 	if (onqueue) {
    492   1.1  riastrad 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
    493   1.1  riastrad 		fcb->fcb_onqueue = false;
    494   1.1  riastrad 	}
    495   1.1  riastrad 	spin_unlock(fence->lock);
    496   1.1  riastrad 
    497   1.1  riastrad 	return onqueue;
    498   1.1  riastrad }
    499   1.1  riastrad 
    500   1.1  riastrad /*
    501   1.2  riastrad  * dma_fence_enable_sw_signaling(fence)
    502   1.1  riastrad  *
    503   1.1  riastrad  *	If it hasn't been called yet and the fence hasn't been
    504   1.1  riastrad  *	signalled yet, call the fence's enable_sw_signaling callback.
    505   1.1  riastrad  *	If when that happens, the callback indicates failure by
    506   1.1  riastrad  *	returning false, signal the fence.
    507   1.1  riastrad  */
    508   1.1  riastrad void
    509   1.2  riastrad dma_fence_enable_sw_signaling(struct dma_fence *fence)
    510   1.1  riastrad {
    511   1.1  riastrad 
    512   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    513   1.1  riastrad 
    514   1.1  riastrad 	spin_lock(fence->lock);
    515  1.22  riastrad 	if ((fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) == 0)
    516  1.22  riastrad 		(void)dma_fence_ensure_signal_enabled(fence);
    517   1.1  riastrad 	spin_unlock(fence->lock);
    518   1.1  riastrad }
    519   1.1  riastrad 
    520   1.1  riastrad /*
    521   1.2  riastrad  * dma_fence_is_signaled(fence)
    522   1.1  riastrad  *
    523   1.1  riastrad  *	Test whether the fence has been signalled.  If it has been
    524   1.2  riastrad  *	signalled by dma_fence_signal(_locked), return true.  If the
    525   1.1  riastrad  *	signalled callback returns true indicating that some implicit
    526   1.1  riastrad  *	external condition has changed, call the callbacks as if with
    527   1.2  riastrad  *	dma_fence_signal.
    528   1.1  riastrad  */
    529   1.1  riastrad bool
    530   1.2  riastrad dma_fence_is_signaled(struct dma_fence *fence)
    531   1.1  riastrad {
    532   1.1  riastrad 	bool signaled;
    533   1.1  riastrad 
    534   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    535   1.1  riastrad 
    536   1.1  riastrad 	spin_lock(fence->lock);
    537   1.2  riastrad 	signaled = dma_fence_is_signaled_locked(fence);
    538   1.1  riastrad 	spin_unlock(fence->lock);
    539   1.1  riastrad 
    540   1.1  riastrad 	return signaled;
    541   1.1  riastrad }
    542   1.1  riastrad 
    543   1.1  riastrad /*
    544   1.2  riastrad  * dma_fence_is_signaled_locked(fence)
    545   1.1  riastrad  *
    546   1.1  riastrad  *	Test whether the fence has been signalled.  Like
    547   1.2  riastrad  *	dma_fence_is_signaleed, but caller already holds the fence's lock.
    548   1.1  riastrad  */
    549   1.1  riastrad bool
    550   1.2  riastrad dma_fence_is_signaled_locked(struct dma_fence *fence)
    551   1.1  riastrad {
    552   1.1  riastrad 
    553   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    554   1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    555   1.1  riastrad 
    556   1.1  riastrad 	/* Check whether we already set the signalled bit.  */
    557   1.2  riastrad 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
    558   1.1  riastrad 		return true;
    559   1.1  riastrad 
    560   1.1  riastrad 	/* If there's a signalled callback, test it.  */
    561   1.1  riastrad 	if (fence->ops->signaled) {
    562   1.1  riastrad 		if ((*fence->ops->signaled)(fence)) {
    563   1.1  riastrad 			/*
    564   1.1  riastrad 			 * It's been signalled implicitly by some
    565   1.1  riastrad 			 * external phenomonen.  Act as though someone
    566   1.2  riastrad 			 * has called dma_fence_signal.
    567   1.1  riastrad 			 */
    568   1.2  riastrad 			dma_fence_signal_locked(fence);
    569   1.1  riastrad 			return true;
    570   1.1  riastrad 		}
    571   1.1  riastrad 	}
    572   1.1  riastrad 
    573   1.1  riastrad 	return false;
    574   1.1  riastrad }
    575   1.1  riastrad 
    576   1.1  riastrad /*
    577   1.5  riastrad  * dma_fence_set_error(fence, error)
    578   1.5  riastrad  *
    579   1.5  riastrad  *	Set an error code prior to dma_fence_signal for use by a
    580   1.5  riastrad  *	waiter to learn about success or failure of the fence.
    581   1.5  riastrad  */
    582   1.5  riastrad void
    583   1.5  riastrad dma_fence_set_error(struct dma_fence *fence, int error)
    584   1.5  riastrad {
    585   1.5  riastrad 
    586  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
    587  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
    588   1.5  riastrad 	KASSERT(!(fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)));
    589   1.6  riastrad 	KASSERTMSG(error >= -ELAST, "%d", error);
    590   1.5  riastrad 	KASSERTMSG(error < 0, "%d", error);
    591   1.5  riastrad 
    592   1.5  riastrad 	fence->error = error;
    593   1.5  riastrad }
    594   1.5  riastrad 
    595   1.5  riastrad /*
    596  1.10  riastrad  * dma_fence_get_status(fence)
    597  1.10  riastrad  *
    598  1.10  riastrad  *	Return 0 if fence has yet to be signalled, 1 if it has been
    599  1.10  riastrad  *	signalled without error, or negative error code if
    600  1.10  riastrad  *	dma_fence_set_error was used.
    601  1.10  riastrad  */
    602  1.10  riastrad int
    603  1.10  riastrad dma_fence_get_status(struct dma_fence *fence)
    604  1.10  riastrad {
    605  1.10  riastrad 	int ret;
    606  1.10  riastrad 
    607  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
    608  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
    609  1.24  riastrad 
    610  1.10  riastrad 	spin_lock(fence->lock);
    611  1.10  riastrad 	if (!dma_fence_is_signaled_locked(fence)) {
    612  1.10  riastrad 		ret = 0;
    613  1.10  riastrad 	} else if (fence->error) {
    614  1.10  riastrad 		ret = fence->error;
    615  1.10  riastrad 		KASSERTMSG(ret < 0, "%d", ret);
    616  1.10  riastrad 	} else {
    617  1.10  riastrad 		ret = 1;
    618  1.10  riastrad 	}
    619  1.10  riastrad 	spin_unlock(fence->lock);
    620  1.10  riastrad 
    621  1.10  riastrad 	return ret;
    622  1.10  riastrad }
    623  1.10  riastrad 
    624  1.10  riastrad /*
    625   1.2  riastrad  * dma_fence_signal(fence)
    626   1.1  riastrad  *
    627   1.1  riastrad  *	Signal the fence.  If it has already been signalled, return
    628   1.1  riastrad  *	-EINVAL.  If it has not been signalled, call the enable
    629   1.1  riastrad  *	signalling callback if it hasn't been called yet, and remove
    630   1.1  riastrad  *	each registered callback from the queue and call it; then
    631   1.1  riastrad  *	return 0.
    632   1.1  riastrad  */
    633   1.1  riastrad int
    634   1.2  riastrad dma_fence_signal(struct dma_fence *fence)
    635   1.1  riastrad {
    636   1.1  riastrad 	int ret;
    637   1.1  riastrad 
    638   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    639   1.1  riastrad 
    640   1.1  riastrad 	spin_lock(fence->lock);
    641   1.2  riastrad 	ret = dma_fence_signal_locked(fence);
    642   1.1  riastrad 	spin_unlock(fence->lock);
    643   1.1  riastrad 
    644   1.1  riastrad 	return ret;
    645   1.1  riastrad }
    646   1.1  riastrad 
    647   1.1  riastrad /*
    648   1.2  riastrad  * dma_fence_signal_locked(fence)
    649   1.1  riastrad  *
    650   1.2  riastrad  *	Signal the fence.  Like dma_fence_signal, but caller already
    651   1.2  riastrad  *	holds the fence's lock.
    652   1.1  riastrad  */
    653   1.1  riastrad int
    654   1.2  riastrad dma_fence_signal_locked(struct dma_fence *fence)
    655   1.1  riastrad {
    656   1.2  riastrad 	struct dma_fence_cb *fcb, *next;
    657   1.1  riastrad 
    658   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    659   1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    660   1.1  riastrad 
    661   1.1  riastrad 	/* If it's been signalled, fail; otherwise set the signalled bit.  */
    662   1.2  riastrad 	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
    663   1.1  riastrad 		return -EINVAL;
    664   1.1  riastrad 
    665  1.25  riastrad 	/* Set the timestamp.  */
    666  1.25  riastrad 	fence->timestamp = ktime_get();
    667  1.25  riastrad 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
    668  1.25  riastrad 
    669   1.1  riastrad 	/* Wake waiters.  */
    670   1.1  riastrad 	cv_broadcast(&fence->f_cv);
    671   1.1  riastrad 
    672   1.1  riastrad 	/* Remove and call the callbacks.  */
    673   1.1  riastrad 	TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
    674   1.1  riastrad 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
    675   1.1  riastrad 		fcb->fcb_onqueue = false;
    676   1.4  riastrad 		(*fcb->func)(fence, fcb);
    677   1.1  riastrad 	}
    678   1.1  riastrad 
    679   1.1  riastrad 	/* Success! */
    680   1.1  riastrad 	return 0;
    681   1.1  riastrad }
    682   1.1  riastrad 
    683   1.1  riastrad struct wait_any {
    684   1.2  riastrad 	struct dma_fence_cb	fcb;
    685   1.1  riastrad 	struct wait_any1 {
    686   1.1  riastrad 		kmutex_t	lock;
    687   1.1  riastrad 		kcondvar_t	cv;
    688   1.1  riastrad 		bool		done;
    689  1.11  riastrad 		uint32_t	*ip;
    690  1.11  riastrad 		struct wait_any	*cb;
    691   1.1  riastrad 	}		*common;
    692   1.1  riastrad };
    693   1.1  riastrad 
    694   1.1  riastrad static void
    695   1.2  riastrad wait_any_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
    696   1.1  riastrad {
    697   1.1  riastrad 	struct wait_any *cb = container_of(fcb, struct wait_any, fcb);
    698   1.1  riastrad 
    699   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    700   1.1  riastrad 
    701   1.1  riastrad 	mutex_enter(&cb->common->lock);
    702   1.1  riastrad 	cb->common->done = true;
    703  1.11  riastrad 	if (cb->common->ip)
    704  1.11  riastrad 		*cb->common->ip = cb - cb->common->cb;
    705   1.1  riastrad 	cv_broadcast(&cb->common->cv);
    706   1.1  riastrad 	mutex_exit(&cb->common->lock);
    707   1.1  riastrad }
    708   1.1  riastrad 
    709   1.1  riastrad /*
    710  1.11  riastrad  * dma_fence_wait_any_timeout(fence, nfences, intr, timeout, ip)
    711   1.1  riastrad  *
    712   1.1  riastrad  *	Wait for any of fences[0], fences[1], fences[2], ...,
    713  1.13  riastrad  *	fences[nfences-1] to be signalled.  If ip is nonnull, set *ip
    714  1.13  riastrad  *	to the index of the first one.
    715   1.1  riastrad  */
    716   1.1  riastrad long
    717   1.2  riastrad dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t nfences,
    718  1.11  riastrad     bool intr, long timeout, uint32_t *ip)
    719   1.1  riastrad {
    720   1.1  riastrad 	struct wait_any1 common;
    721   1.1  riastrad 	struct wait_any *cb;
    722   1.1  riastrad 	uint32_t i, j;
    723   1.1  riastrad 	int start, end;
    724   1.1  riastrad 	long ret = 0;
    725   1.1  riastrad 
    726   1.1  riastrad 	/* Allocate an array of callback records.  */
    727   1.1  riastrad 	cb = kcalloc(nfences, sizeof(cb[0]), GFP_KERNEL);
    728   1.1  riastrad 	if (cb == NULL) {
    729   1.1  riastrad 		ret = -ENOMEM;
    730   1.1  riastrad 		goto out0;
    731   1.1  riastrad 	}
    732   1.1  riastrad 
    733   1.1  riastrad 	/* Initialize a mutex and condvar for the common wait.  */
    734   1.1  riastrad 	mutex_init(&common.lock, MUTEX_DEFAULT, IPL_VM);
    735   1.1  riastrad 	cv_init(&common.cv, "fence");
    736   1.1  riastrad 	common.done = false;
    737  1.11  riastrad 	common.ip = ip;
    738  1.11  riastrad 	common.cb = cb;
    739   1.1  riastrad 
    740   1.1  riastrad 	/* Add a callback to each of the fences, or stop here if we can't.  */
    741   1.1  riastrad 	for (i = 0; i < nfences; i++) {
    742   1.1  riastrad 		cb[i].common = &common;
    743   1.2  riastrad 		KASSERT(dma_fence_referenced_p(fences[i]));
    744   1.2  riastrad 		ret = dma_fence_add_callback(fences[i], &cb[i].fcb,
    745   1.2  riastrad 		    &wait_any_cb);
    746   1.1  riastrad 		if (ret)
    747   1.1  riastrad 			goto out1;
    748   1.1  riastrad 	}
    749   1.1  riastrad 
    750   1.1  riastrad 	/*
    751   1.1  riastrad 	 * Test whether any of the fences has been signalled.  If they
    752   1.1  riastrad 	 * have, stop here.  If the haven't, we are guaranteed to be
    753   1.1  riastrad 	 * notified by one of the callbacks when they have.
    754   1.1  riastrad 	 */
    755   1.1  riastrad 	for (j = 0; j < nfences; j++) {
    756  1.11  riastrad 		if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fences[j]->flags)) {
    757  1.11  riastrad 			if (ip)
    758  1.11  riastrad 				*ip = j;
    759  1.11  riastrad 			ret = 0;
    760   1.1  riastrad 			goto out1;
    761  1.11  riastrad 		}
    762   1.1  riastrad 	}
    763   1.1  riastrad 
    764   1.1  riastrad 	/*
    765   1.1  riastrad 	 * None of them was ready immediately.  Wait for one of the
    766   1.1  riastrad 	 * callbacks to notify us when it is done.
    767   1.1  riastrad 	 */
    768   1.1  riastrad 	mutex_enter(&common.lock);
    769   1.1  riastrad 	while (timeout > 0 && !common.done) {
    770   1.1  riastrad 		start = getticks();
    771   1.1  riastrad 		__insn_barrier();
    772   1.1  riastrad 		if (intr) {
    773   1.1  riastrad 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
    774   1.1  riastrad 				ret = -cv_timedwait_sig(&common.cv,
    775   1.1  riastrad 				    &common.lock, MIN(timeout, /* paranoia */
    776   1.1  riastrad 					MAX_SCHEDULE_TIMEOUT));
    777   1.1  riastrad 			} else {
    778   1.1  riastrad 				ret = -cv_wait_sig(&common.cv, &common.lock);
    779   1.1  riastrad 			}
    780   1.1  riastrad 		} else {
    781   1.1  riastrad 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
    782   1.1  riastrad 				ret = -cv_timedwait(&common.cv,
    783   1.1  riastrad 				    &common.lock, MIN(timeout, /* paranoia */
    784   1.1  riastrad 					MAX_SCHEDULE_TIMEOUT));
    785   1.1  riastrad 			} else {
    786   1.1  riastrad 				cv_wait(&common.cv, &common.lock);
    787   1.1  riastrad 				ret = 0;
    788   1.1  riastrad 			}
    789   1.1  riastrad 		}
    790   1.1  riastrad 		end = getticks();
    791   1.1  riastrad 		__insn_barrier();
    792   1.1  riastrad 		if (ret) {
    793   1.1  riastrad 			if (ret == -ERESTART)
    794   1.1  riastrad 				ret = -ERESTARTSYS;
    795   1.1  riastrad 			break;
    796   1.1  riastrad 		}
    797   1.1  riastrad 		timeout -= MIN(timeout, (unsigned)end - (unsigned)start);
    798   1.1  riastrad 	}
    799   1.1  riastrad 	mutex_exit(&common.lock);
    800   1.1  riastrad 
    801   1.1  riastrad 	/*
    802   1.1  riastrad 	 * Massage the return code: if we were interrupted, return
    803   1.1  riastrad 	 * ERESTARTSYS; if cv_timedwait timed out, return 0; otherwise
    804   1.1  riastrad 	 * return the remaining time.
    805   1.1  riastrad 	 */
    806   1.1  riastrad 	if (ret < 0) {
    807   1.1  riastrad 		if (ret == -EINTR || ret == -ERESTART)
    808   1.1  riastrad 			ret = -ERESTARTSYS;
    809   1.1  riastrad 		if (ret == -EWOULDBLOCK)
    810   1.1  riastrad 			ret = 0;
    811   1.1  riastrad 	} else {
    812   1.1  riastrad 		KASSERT(ret == 0);
    813   1.1  riastrad 		ret = timeout;
    814   1.1  riastrad 	}
    815   1.1  riastrad 
    816   1.1  riastrad out1:	while (i --> 0)
    817   1.2  riastrad 		(void)dma_fence_remove_callback(fences[i], &cb[i].fcb);
    818   1.1  riastrad 	cv_destroy(&common.cv);
    819   1.1  riastrad 	mutex_destroy(&common.lock);
    820   1.1  riastrad 	kfree(cb);
    821   1.1  riastrad out0:	return ret;
    822   1.1  riastrad }
    823   1.1  riastrad 
    824   1.1  riastrad /*
    825   1.2  riastrad  * dma_fence_wait_timeout(fence, intr, timeout)
    826   1.1  riastrad  *
    827   1.1  riastrad  *	Wait until fence is signalled; or until interrupt, if intr is
    828   1.1  riastrad  *	true; or until timeout, if positive.  Return -ERESTARTSYS if
    829   1.1  riastrad  *	interrupted, negative error code on any other error, zero on
    830   1.1  riastrad  *	timeout, or positive number of ticks remaining if the fence is
    831   1.1  riastrad  *	signalled before the timeout.  Works by calling the fence wait
    832   1.1  riastrad  *	callback.
    833   1.1  riastrad  *
    834  1.28  riastrad  *	The timeout must be nonnegative and at most
    835  1.28  riastrad  *	MAX_SCHEDULE_TIMEOUT, which means wait indefinitely.
    836   1.1  riastrad  */
    837   1.1  riastrad long
    838   1.2  riastrad dma_fence_wait_timeout(struct dma_fence *fence, bool intr, long timeout)
    839   1.1  riastrad {
    840   1.1  riastrad 
    841   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    842  1.27  riastrad 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
    843  1.28  riastrad 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
    844   1.1  riastrad 
    845  1.14  riastrad 	if (fence->ops->wait)
    846  1.14  riastrad 		return (*fence->ops->wait)(fence, intr, timeout);
    847  1.14  riastrad 	else
    848  1.14  riastrad 		return dma_fence_default_wait(fence, intr, timeout);
    849   1.1  riastrad }
    850   1.1  riastrad 
    851   1.1  riastrad /*
    852   1.2  riastrad  * dma_fence_wait(fence, intr)
    853   1.1  riastrad  *
    854   1.1  riastrad  *	Wait until fence is signalled; or until interrupt, if intr is
    855   1.1  riastrad  *	true.  Return -ERESTARTSYS if interrupted, negative error code
    856   1.1  riastrad  *	on any other error, zero on sucess.  Works by calling the fence
    857   1.1  riastrad  *	wait callback with MAX_SCHEDULE_TIMEOUT.
    858   1.1  riastrad  */
    859   1.1  riastrad long
    860   1.2  riastrad dma_fence_wait(struct dma_fence *fence, bool intr)
    861   1.1  riastrad {
    862   1.1  riastrad 	long ret;
    863   1.1  riastrad 
    864   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    865   1.1  riastrad 
    866  1.15  riastrad 	if (fence->ops->wait)
    867  1.15  riastrad 		ret = (*fence->ops->wait)(fence, intr, MAX_SCHEDULE_TIMEOUT);
    868  1.15  riastrad 	else
    869  1.15  riastrad 		ret = dma_fence_default_wait(fence, intr,
    870  1.15  riastrad 		    MAX_SCHEDULE_TIMEOUT);
    871   1.1  riastrad 	KASSERT(ret != 0);
    872   1.1  riastrad 
    873   1.1  riastrad 	return (ret < 0 ? ret : 0);
    874   1.1  riastrad }
    875   1.1  riastrad 
    876   1.1  riastrad /*
    877   1.2  riastrad  * dma_fence_default_wait(fence, intr, timeout)
    878   1.1  riastrad  *
    879   1.1  riastrad  *	Default implementation of fence wait callback using a condition
    880   1.1  riastrad  *	variable.  If the fence is already signalled, return timeout,
    881  1.16  riastrad  *	or 1 if timeout is zero meaning poll.  If the enable signalling
    882  1.16  riastrad  *	callback hasn't been called, call it, and if it fails, act as
    883  1.16  riastrad  *	if the fence had been signalled.  Otherwise, wait on the
    884  1.16  riastrad  *	internal condvar.  If timeout is MAX_SCHEDULE_TIMEOUT, wait
    885  1.16  riastrad  *	indefinitely.
    886   1.1  riastrad  */
    887   1.1  riastrad long
    888   1.2  riastrad dma_fence_default_wait(struct dma_fence *fence, bool intr, long timeout)
    889   1.1  riastrad {
    890   1.1  riastrad 	int starttime = 0, now = 0, deadline = 0; /* XXXGCC */
    891   1.1  riastrad 	kmutex_t *lock = &fence->lock->sl_lock;
    892   1.1  riastrad 	long ret = 0;
    893   1.1  riastrad 
    894   1.2  riastrad 	KASSERT(dma_fence_referenced_p(fence));
    895   1.1  riastrad 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
    896   1.1  riastrad 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
    897   1.1  riastrad 
    898   1.1  riastrad 	/* Optimistically try to skip the lock if it's already signalled.  */
    899   1.2  riastrad 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
    900  1.16  riastrad 		return (timeout ? timeout : 1);
    901   1.1  riastrad 
    902   1.1  riastrad 	/* Acquire the lock.  */
    903   1.1  riastrad 	spin_lock(fence->lock);
    904   1.1  riastrad 
    905  1.16  riastrad 	/* Ensure signalling is enabled, or stop if already completed.  */
    906  1.17  riastrad 	if (dma_fence_ensure_signal_enabled(fence) != 0) {
    907  1.17  riastrad 		spin_unlock(fence->lock);
    908  1.16  riastrad 		return (timeout ? timeout : 1);
    909  1.17  riastrad 	}
    910  1.16  riastrad 
    911  1.16  riastrad 	/* If merely polling, stop here.  */
    912  1.16  riastrad 	if (timeout == 0) {
    913  1.16  riastrad 		spin_unlock(fence->lock);
    914  1.16  riastrad 		return 0;
    915  1.16  riastrad 	}
    916   1.1  riastrad 
    917   1.1  riastrad 	/* Find out what our deadline is so we can handle spurious wakeup.  */
    918   1.1  riastrad 	if (timeout < MAX_SCHEDULE_TIMEOUT) {
    919   1.1  riastrad 		now = getticks();
    920   1.1  riastrad 		__insn_barrier();
    921   1.1  riastrad 		starttime = now;
    922   1.1  riastrad 		deadline = starttime + timeout;
    923   1.1  riastrad 	}
    924   1.1  riastrad 
    925   1.1  riastrad 	/* Wait until the signalled bit is set.  */
    926   1.2  riastrad 	while (!(fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))) {
    927   1.1  riastrad 		/*
    928   1.1  riastrad 		 * If there's a timeout and we've passed the deadline,
    929   1.1  riastrad 		 * give up.
    930   1.1  riastrad 		 */
    931   1.1  riastrad 		if (timeout < MAX_SCHEDULE_TIMEOUT) {
    932   1.1  riastrad 			now = getticks();
    933   1.1  riastrad 			__insn_barrier();
    934   1.1  riastrad 			if (deadline <= now)
    935   1.1  riastrad 				break;
    936   1.1  riastrad 		}
    937   1.1  riastrad 		if (intr) {
    938   1.1  riastrad 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
    939   1.1  riastrad 				ret = -cv_timedwait_sig(&fence->f_cv, lock,
    940   1.1  riastrad 				    deadline - now);
    941   1.1  riastrad 			} else {
    942   1.1  riastrad 				ret = -cv_wait_sig(&fence->f_cv, lock);
    943   1.1  riastrad 			}
    944   1.1  riastrad 		} else {
    945   1.1  riastrad 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
    946   1.1  riastrad 				ret = -cv_timedwait(&fence->f_cv, lock,
    947   1.1  riastrad 				    deadline - now);
    948   1.1  riastrad 			} else {
    949   1.1  riastrad 				cv_wait(&fence->f_cv, lock);
    950   1.1  riastrad 				ret = 0;
    951   1.1  riastrad 			}
    952   1.1  riastrad 		}
    953   1.1  riastrad 		/* If the wait failed, give up.  */
    954   1.1  riastrad 		if (ret) {
    955   1.1  riastrad 			if (ret == -ERESTART)
    956   1.1  riastrad 				ret = -ERESTARTSYS;
    957   1.1  riastrad 			break;
    958   1.1  riastrad 		}
    959   1.1  riastrad 	}
    960   1.1  riastrad 
    961   1.1  riastrad 	/* All done.  Release the lock.  */
    962   1.1  riastrad 	spin_unlock(fence->lock);
    963   1.1  riastrad 
    964   1.1  riastrad 	/* If cv_timedwait gave up, return 0 meaning timeout.  */
    965   1.1  riastrad 	if (ret == -EWOULDBLOCK) {
    966   1.1  riastrad 		/* Only cv_timedwait and cv_timedwait_sig can return this.  */
    967   1.1  riastrad 		KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
    968   1.1  riastrad 		return 0;
    969   1.1  riastrad 	}
    970   1.1  riastrad 
    971   1.1  riastrad 	/* If there was a timeout and the deadline passed, return 0.  */
    972   1.1  riastrad 	if (timeout < MAX_SCHEDULE_TIMEOUT) {
    973   1.1  riastrad 		if (deadline <= now)
    974   1.1  riastrad 			return 0;
    975   1.1  riastrad 	}
    976   1.1  riastrad 
    977   1.1  riastrad 	/* If we were interrupted, return -ERESTARTSYS.  */
    978   1.1  riastrad 	if (ret == -EINTR || ret == -ERESTART)
    979   1.1  riastrad 		return -ERESTARTSYS;
    980   1.1  riastrad 
    981   1.1  riastrad 	/* If there was any other kind of error, fail.  */
    982   1.1  riastrad 	if (ret)
    983   1.1  riastrad 		return ret;
    984   1.1  riastrad 
    985   1.1  riastrad 	/*
    986   1.1  riastrad 	 * Success!  Return the number of ticks left, at least 1, or 1
    987   1.1  riastrad 	 * if no timeout.
    988   1.1  riastrad 	 */
    989   1.1  riastrad 	return (timeout < MAX_SCHEDULE_TIMEOUT ? MIN(deadline - now, 1) : 1);
    990   1.1  riastrad }
    991  1.12  riastrad 
    992  1.12  riastrad /*
    993  1.12  riastrad  * __dma_fence_signal(fence)
    994  1.12  riastrad  *
    995  1.12  riastrad  *	Set fence's signalled bit, without waking waiters yet.  Return
    996  1.12  riastrad  *	true if it was newly set, false if it was already set.
    997  1.12  riastrad  */
    998  1.12  riastrad bool
    999  1.12  riastrad __dma_fence_signal(struct dma_fence *fence)
   1000  1.12  riastrad {
   1001  1.12  riastrad 
   1002  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
   1003  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
   1004  1.24  riastrad 
   1005  1.12  riastrad 	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
   1006  1.12  riastrad 		return false;
   1007  1.12  riastrad 
   1008  1.12  riastrad 	return true;
   1009  1.12  riastrad }
   1010  1.12  riastrad 
   1011  1.12  riastrad /*
   1012  1.12  riastrad  * __dma_fence_signal_wake(fence)
   1013  1.12  riastrad  *
   1014  1.25  riastrad  *	Set fence's timestamp and wake fence's waiters.  Caller must
   1015  1.25  riastrad  *	have previously called __dma_fence_signal and it must have
   1016  1.25  riastrad  *	previously returned true.
   1017  1.12  riastrad  */
   1018  1.12  riastrad void
   1019  1.12  riastrad __dma_fence_signal_wake(struct dma_fence *fence, ktime_t timestamp)
   1020  1.12  riastrad {
   1021  1.12  riastrad 	struct dma_fence_cb *fcb, *next;
   1022  1.12  riastrad 
   1023  1.24  riastrad 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
   1024  1.24  riastrad 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
   1025  1.24  riastrad 
   1026  1.12  riastrad 	spin_lock(fence->lock);
   1027  1.12  riastrad 
   1028  1.12  riastrad 	KASSERT(fence->flags & DMA_FENCE_FLAG_SIGNALED_BIT);
   1029  1.12  riastrad 
   1030  1.25  riastrad 	/* Set the timestamp.  */
   1031  1.25  riastrad 	fence->timestamp = timestamp;
   1032  1.25  riastrad 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
   1033  1.25  riastrad 
   1034  1.12  riastrad 	/* Wake waiters.  */
   1035  1.12  riastrad 	cv_broadcast(&fence->f_cv);
   1036  1.12  riastrad 
   1037  1.12  riastrad 	/* Remove and call the callbacks.  */
   1038  1.12  riastrad 	TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
   1039  1.12  riastrad 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
   1040  1.12  riastrad 		fcb->fcb_onqueue = false;
   1041  1.12  riastrad 		(*fcb->func)(fence, fcb);
   1042  1.12  riastrad 	}
   1043  1.12  riastrad 
   1044  1.12  riastrad 	spin_unlock(fence->lock);
   1045  1.12  riastrad }
   1046