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