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