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