xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_dma_fence.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: linux_dma_fence.c,v 1.39 2021/12/19 12:39:40 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.39 2021/12/19 12:39:40 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;
398 
399 retry:
400 	/*
401 	 * Load the fence, ensuring we observe the fully initialized
402 	 * content.
403 	 */
404 	if ((fence = atomic_load_consume(fencep)) == NULL)
405 		return NULL;
406 
407 	/* Try to acquire a reference.  If we can't, try again.  */
408 	if (!dma_fence_get_rcu(fence))
409 		goto retry;
410 
411 	/*
412 	 * Confirm that it's still the same fence.  If not, release it
413 	 * and retry.
414 	 */
415 	if (fence != atomic_load_relaxed(fencep)) {
416 		dma_fence_put(fence);
417 		goto retry;
418 	}
419 
420 	/* Success!  */
421 	KASSERT(dma_fence_referenced_p(fence));
422 	return fence;
423 }
424 
425 static void
426 dma_fence_release(struct kref *refcount)
427 {
428 	struct dma_fence *fence = container_of(refcount, struct dma_fence,
429 	    refcount);
430 
431 	KASSERTMSG(TAILQ_EMPTY(&fence->f_callbacks),
432 	    "fence %p has pending callbacks", fence);
433 	KASSERT(!dma_fence_referenced_p(fence));
434 
435 	SDT_PROBE1(sdt, drm, fence, release,  fence);
436 
437 	if (fence->ops->release)
438 		(*fence->ops->release)(fence);
439 	else
440 		dma_fence_free(fence);
441 }
442 
443 /*
444  * dma_fence_put(fence)
445  *
446  *	Release a reference to fence.  If this was the last one, call
447  *	the fence's release callback.
448  */
449 void
450 dma_fence_put(struct dma_fence *fence)
451 {
452 
453 	if (fence == NULL)
454 		return;
455 	KASSERT(dma_fence_referenced_p(fence));
456 	kref_put(&fence->refcount, &dma_fence_release);
457 }
458 
459 /*
460  * dma_fence_ensure_signal_enabled(fence)
461  *
462  *	Internal subroutine.  If the fence was already signalled,
463  *	return -ENOENT.  Otherwise, if the enable signalling callback
464  *	has not been called yet, call it.  If fails, signal the fence
465  *	and return -ENOENT.  If it succeeds, or if it had already been
466  *	called, return zero to indicate success.
467  *
468  *	Caller must hold the fence's lock.
469  */
470 static int
471 dma_fence_ensure_signal_enabled(struct dma_fence *fence)
472 {
473 	bool already_enabled;
474 
475 	KASSERT(dma_fence_referenced_p(fence));
476 	KASSERT(spin_is_locked(fence->lock));
477 
478 	/* Determine whether signalling was enabled, and enable it.  */
479 	already_enabled = test_and_set_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT,
480 	    &fence->flags);
481 
482 	/* If the fence was already signalled, fail with -ENOENT.  */
483 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
484 		return -ENOENT;
485 
486 	/*
487 	 * Otherwise, if it wasn't enabled yet, try to enable
488 	 * signalling.
489 	 */
490 	if (!already_enabled && fence->ops->enable_signaling) {
491 		SDT_PROBE1(sdt, drm, fence, enable_signaling,  fence);
492 		if (!(*fence->ops->enable_signaling)(fence)) {
493 			/* If it failed, signal and return -ENOENT.  */
494 			dma_fence_signal_locked(fence);
495 			return -ENOENT;
496 		}
497 	}
498 
499 	/* Success!  */
500 	return 0;
501 }
502 
503 /*
504  * dma_fence_add_callback(fence, fcb, fn)
505  *
506  *	If fence has been signalled, return -ENOENT.  If the enable
507  *	signalling callback hasn't been called yet, call it; if it
508  *	fails, return -ENOENT.  Otherwise, arrange to call fn(fence,
509  *	fcb) when it is signalled, and return 0.
510  *
511  *	The fence uses memory allocated by the caller in fcb from the
512  *	time of dma_fence_add_callback either to the time of
513  *	dma_fence_remove_callback, or just before calling fn.
514  */
515 int
516 dma_fence_add_callback(struct dma_fence *fence, struct dma_fence_cb *fcb,
517     dma_fence_func_t fn)
518 {
519 	int ret;
520 
521 	KASSERT(dma_fence_referenced_p(fence));
522 
523 	/* Optimistically try to skip the lock if it's already signalled.  */
524 	if (atomic_load_relaxed(&fence->flags) &
525 	    (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) {
526 		ret = -ENOENT;
527 		goto out0;
528 	}
529 
530 	/* Acquire the lock.  */
531 	spin_lock(fence->lock);
532 
533 	/* Ensure signalling is enabled, or fail if we can't.  */
534 	ret = dma_fence_ensure_signal_enabled(fence);
535 	if (ret)
536 		goto out1;
537 
538 	/* Insert the callback.  */
539 	SDT_PROBE2(sdt, drm, fence, add_callback,  fence, fcb);
540 	fcb->func = fn;
541 	TAILQ_INSERT_TAIL(&fence->f_callbacks, fcb, fcb_entry);
542 	fcb->fcb_onqueue = true;
543 	ret = 0;
544 
545 	/* Release the lock and we're done.  */
546 out1:	spin_unlock(fence->lock);
547 out0:	if (ret) {
548 		fcb->func = NULL;
549 		fcb->fcb_onqueue = false;
550 	}
551 	return ret;
552 }
553 
554 /*
555  * dma_fence_remove_callback(fence, fcb)
556  *
557  *	Remove the callback fcb from fence.  Return true if it was
558  *	removed from the list, or false if it had already run and so
559  *	was no longer queued anyway.  Caller must have already called
560  *	dma_fence_add_callback(fence, fcb).
561  */
562 bool
563 dma_fence_remove_callback(struct dma_fence *fence, struct dma_fence_cb *fcb)
564 {
565 	bool onqueue;
566 
567 	KASSERT(dma_fence_referenced_p(fence));
568 
569 	spin_lock(fence->lock);
570 	onqueue = fcb->fcb_onqueue;
571 	if (onqueue) {
572 		SDT_PROBE2(sdt, drm, fence, remove_callback,  fence, fcb);
573 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
574 		fcb->fcb_onqueue = false;
575 	}
576 	spin_unlock(fence->lock);
577 
578 	return onqueue;
579 }
580 
581 /*
582  * dma_fence_enable_sw_signaling(fence)
583  *
584  *	If it hasn't been called yet and the fence hasn't been
585  *	signalled yet, call the fence's enable_sw_signaling callback.
586  *	If when that happens, the callback indicates failure by
587  *	returning false, signal the fence.
588  */
589 void
590 dma_fence_enable_sw_signaling(struct dma_fence *fence)
591 {
592 
593 	KASSERT(dma_fence_referenced_p(fence));
594 
595 	spin_lock(fence->lock);
596 	if ((fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) == 0)
597 		(void)dma_fence_ensure_signal_enabled(fence);
598 	spin_unlock(fence->lock);
599 }
600 
601 /*
602  * dma_fence_is_signaled(fence)
603  *
604  *	Test whether the fence has been signalled.  If it has been
605  *	signalled by dma_fence_signal(_locked), return true.  If the
606  *	signalled callback returns true indicating that some implicit
607  *	external condition has changed, call the callbacks as if with
608  *	dma_fence_signal.
609  */
610 bool
611 dma_fence_is_signaled(struct dma_fence *fence)
612 {
613 	bool signaled;
614 
615 	KASSERT(dma_fence_referenced_p(fence));
616 
617 	spin_lock(fence->lock);
618 	signaled = dma_fence_is_signaled_locked(fence);
619 	spin_unlock(fence->lock);
620 
621 	return signaled;
622 }
623 
624 /*
625  * dma_fence_is_signaled_locked(fence)
626  *
627  *	Test whether the fence has been signalled.  Like
628  *	dma_fence_is_signaleed, but caller already holds the fence's lock.
629  */
630 bool
631 dma_fence_is_signaled_locked(struct dma_fence *fence)
632 {
633 
634 	KASSERT(dma_fence_referenced_p(fence));
635 	KASSERT(spin_is_locked(fence->lock));
636 
637 	/* Check whether we already set the signalled bit.  */
638 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
639 		return true;
640 
641 	/* If there's a signalled callback, test it.  */
642 	if (fence->ops->signaled) {
643 		SDT_PROBE1(sdt, drm, fence, test,  fence);
644 		if ((*fence->ops->signaled)(fence)) {
645 			/*
646 			 * It's been signalled implicitly by some
647 			 * external phenomonen.  Act as though someone
648 			 * has called dma_fence_signal.
649 			 */
650 			dma_fence_signal_locked(fence);
651 			return true;
652 		}
653 	}
654 
655 	return false;
656 }
657 
658 /*
659  * dma_fence_set_error(fence, error)
660  *
661  *	Set an error code prior to dma_fence_signal for use by a
662  *	waiter to learn about success or failure of the fence.
663  */
664 void
665 dma_fence_set_error(struct dma_fence *fence, int error)
666 {
667 
668 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
669 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
670 	KASSERT((atomic_load_relaxed(&fence->flags) &
671 		(1u << DMA_FENCE_FLAG_SIGNALED_BIT)) == 0);
672 	KASSERTMSG(error >= -ELAST, "%d", error);
673 	KASSERTMSG(error < 0, "%d", error);
674 
675 	SDT_PROBE2(sdt, drm, fence, set_error,  fence, error);
676 	fence->error = error;
677 }
678 
679 /*
680  * dma_fence_get_status(fence)
681  *
682  *	Return 0 if fence has yet to be signalled, 1 if it has been
683  *	signalled without error, or negative error code if
684  *	dma_fence_set_error was used.
685  */
686 int
687 dma_fence_get_status(struct dma_fence *fence)
688 {
689 	int ret;
690 
691 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
692 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
693 
694 	spin_lock(fence->lock);
695 	if (!dma_fence_is_signaled_locked(fence)) {
696 		ret = 0;
697 	} else if (fence->error) {
698 		ret = fence->error;
699 		KASSERTMSG(ret < 0, "%d", ret);
700 	} else {
701 		ret = 1;
702 	}
703 	spin_unlock(fence->lock);
704 
705 	return ret;
706 }
707 
708 /*
709  * dma_fence_signal(fence)
710  *
711  *	Signal the fence.  If it has already been signalled, return
712  *	-EINVAL.  If it has not been signalled, call the enable
713  *	signalling callback if it hasn't been called yet, and remove
714  *	each registered callback from the queue and call it; then
715  *	return 0.
716  */
717 int
718 dma_fence_signal(struct dma_fence *fence)
719 {
720 	int ret;
721 
722 	KASSERT(dma_fence_referenced_p(fence));
723 
724 	spin_lock(fence->lock);
725 	ret = dma_fence_signal_locked(fence);
726 	spin_unlock(fence->lock);
727 
728 	return ret;
729 }
730 
731 /*
732  * dma_fence_signal_locked(fence)
733  *
734  *	Signal the fence.  Like dma_fence_signal, but caller already
735  *	holds the fence's lock.
736  */
737 int
738 dma_fence_signal_locked(struct dma_fence *fence)
739 {
740 	struct dma_fence_cb *fcb, *next;
741 
742 	KASSERT(dma_fence_referenced_p(fence));
743 	KASSERT(spin_is_locked(fence->lock));
744 
745 	/* If it's been signalled, fail; otherwise set the signalled bit.  */
746 	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
747 		return -EINVAL;
748 
749 	SDT_PROBE1(sdt, drm, fence, signal,  fence);
750 
751 	/* Set the timestamp.  */
752 	fence->timestamp = ktime_get();
753 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
754 
755 	/* Wake waiters.  */
756 	cv_broadcast(&fence->f_cv);
757 
758 	/* Remove and call the callbacks.  */
759 	TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
760 		SDT_PROBE2(sdt, drm, fence, callback,  fence, fcb);
761 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
762 		fcb->fcb_onqueue = false;
763 		(*fcb->func)(fence, fcb);
764 	}
765 
766 	/* Success! */
767 	return 0;
768 }
769 
770 struct wait_any {
771 	struct dma_fence_cb	fcb;
772 	struct wait_any1 {
773 		kmutex_t	lock;
774 		kcondvar_t	cv;
775 		struct wait_any	*cb;
776 		bool		done;
777 	}		*common;
778 };
779 
780 static void
781 wait_any_cb(struct dma_fence *fence, struct dma_fence_cb *fcb)
782 {
783 	struct wait_any *cb = container_of(fcb, struct wait_any, fcb);
784 
785 	KASSERT(dma_fence_referenced_p(fence));
786 
787 	mutex_enter(&cb->common->lock);
788 	cb->common->done = true;
789 	cv_broadcast(&cb->common->cv);
790 	mutex_exit(&cb->common->lock);
791 }
792 
793 /*
794  * dma_fence_wait_any_timeout(fence, nfences, intr, timeout, ip)
795  *
796  *	Wait for any of fences[0], fences[1], fences[2], ...,
797  *	fences[nfences-1] to be signalled.  If ip is nonnull, set *ip
798  *	to the index of the first one.
799  *
800  *	Return -ERESTARTSYS if interrupted, 0 on timeout, or time
801  *	remaining (at least 1) on success.
802  */
803 long
804 dma_fence_wait_any_timeout(struct dma_fence **fences, uint32_t nfences,
805     bool intr, long timeout, uint32_t *ip)
806 {
807 	struct wait_any1 common;
808 	struct wait_any *cb;
809 	uint32_t i, j;
810 	int start, end;
811 	long ret = 0;
812 
813 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
814 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
815 
816 	/* Optimistically check whether any are signalled.  */
817 	for (i = 0; i < nfences; i++) {
818 		KASSERT(dma_fence_referenced_p(fences[i]));
819 		if (dma_fence_is_signaled(fences[i])) {
820 			if (ip)
821 				*ip = i;
822 			return MAX(1, timeout);
823 		}
824 	}
825 
826 	/*
827 	 * If timeout is zero, we're just polling, so stop here as if
828 	 * we timed out instantly.
829 	 */
830 	if (timeout == 0)
831 		return 0;
832 
833 	/* Allocate an array of callback records.  */
834 	cb = kcalloc(nfences, sizeof(cb[0]), GFP_KERNEL);
835 	if (cb == NULL)
836 		return -ENOMEM;
837 
838 	/* Initialize a mutex and condvar for the common wait.  */
839 	mutex_init(&common.lock, MUTEX_DEFAULT, IPL_VM);
840 	cv_init(&common.cv, "fence");
841 	common.cb = cb;
842 	common.done = false;
843 
844 	/*
845 	 * Add a callback to each of the fences, or stop if already
846 	 * signalled.
847 	 */
848 	for (i = 0; i < nfences; i++) {
849 		cb[i].common = &common;
850 		KASSERT(dma_fence_referenced_p(fences[i]));
851 		ret = dma_fence_add_callback(fences[i], &cb[i].fcb,
852 		    &wait_any_cb);
853 		if (ret) {
854 			KASSERT(ret == -ENOENT);
855 			if (ip)
856 				*ip = i;
857 			ret = MAX(1, timeout);
858 			goto out;
859 		}
860 	}
861 
862 	/*
863 	 * None of them was ready immediately.  Wait for one of the
864 	 * callbacks to notify us when it is done.
865 	 */
866 	mutex_enter(&common.lock);
867 	while (!common.done) {
868 		/* Wait for the time remaining.  */
869 		start = getticks();
870 		if (intr) {
871 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
872 				ret = -cv_timedwait_sig(&common.cv,
873 				    &common.lock, MIN(timeout, /* paranoia */
874 					MAX_SCHEDULE_TIMEOUT));
875 			} else {
876 				ret = -cv_wait_sig(&common.cv, &common.lock);
877 			}
878 		} else {
879 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
880 				ret = -cv_timedwait(&common.cv,
881 				    &common.lock, MIN(timeout, /* paranoia */
882 					MAX_SCHEDULE_TIMEOUT));
883 			} else {
884 				cv_wait(&common.cv, &common.lock);
885 				ret = 0;
886 			}
887 		}
888 		end = getticks();
889 
890 		/* Deduct from time remaining.  If none left, time out.  */
891 		if (timeout != MAX_SCHEDULE_TIMEOUT) {
892 			timeout -= MIN(timeout,
893 			    (unsigned)end - (unsigned)start);
894 			if (timeout == 0)
895 				ret = -EWOULDBLOCK;
896 		}
897 
898 		/* If the wait failed, give up.  */
899 		if (ret)
900 			break;
901 	}
902 	mutex_exit(&common.lock);
903 
904 	/*
905 	 * Massage the return code if nonzero:
906 	 * - if we were interrupted, return -ERESTARTSYS;
907 	 * - if we timed out, return 0.
908 	 * No other failure is possible.  On success, ret=0 but we
909 	 * check again below to verify anyway.
910 	 */
911 	if (ret) {
912 		KASSERTMSG((ret == -EINTR || ret == -ERESTART ||
913 			ret == -EWOULDBLOCK), "ret=%ld", ret);
914 		if (ret == -EINTR || ret == -ERESTART) {
915 			ret = -ERESTARTSYS;
916 		} else if (ret == -EWOULDBLOCK) {
917 			KASSERT(timeout != MAX_SCHEDULE_TIMEOUT);
918 			ret = 0;	/* timed out */
919 		}
920 	}
921 
922 	KASSERT(ret != -ERESTART); /* would be confused with time left */
923 
924 	/*
925 	 * Test whether any of the fences has been signalled.  If they
926 	 * have, return success.
927 	 */
928 	for (j = 0; j < nfences; j++) {
929 		if (dma_fence_is_signaled(fences[i])) {
930 			if (ip)
931 				*ip = j;
932 			ret = MAX(1, timeout);
933 			goto out;
934 		}
935 	}
936 
937 	/*
938 	 * If user passed MAX_SCHEDULE_TIMEOUT, we can't return 0
939 	 * meaning timed out because we're supposed to wait forever.
940 	 */
941 	KASSERT(timeout == MAX_SCHEDULE_TIMEOUT ? ret != 0 : 1);
942 
943 out:	while (i --> 0)
944 		(void)dma_fence_remove_callback(fences[i], &cb[i].fcb);
945 	cv_destroy(&common.cv);
946 	mutex_destroy(&common.lock);
947 	kfree(cb);
948 	return ret;
949 }
950 
951 /*
952  * dma_fence_wait_timeout(fence, intr, timeout)
953  *
954  *	Wait until fence is signalled; or until interrupt, if intr is
955  *	true; or until timeout, if positive.  Return -ERESTARTSYS if
956  *	interrupted, negative error code on any other error, zero on
957  *	timeout, or positive number of ticks remaining if the fence is
958  *	signalled before the timeout.  Works by calling the fence wait
959  *	callback.
960  *
961  *	The timeout must be nonnegative and at most
962  *	MAX_SCHEDULE_TIMEOUT, which means wait indefinitely.
963  */
964 long
965 dma_fence_wait_timeout(struct dma_fence *fence, bool intr, long timeout)
966 {
967 	long ret;
968 
969 	KASSERT(dma_fence_referenced_p(fence));
970 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
971 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
972 
973 	SDT_PROBE3(sdt, drm, fence, wait_start,  fence, intr, timeout);
974 	if (fence->ops->wait)
975 		ret = (*fence->ops->wait)(fence, intr, timeout);
976 	else
977 		ret = dma_fence_default_wait(fence, intr, timeout);
978 	SDT_PROBE2(sdt, drm, fence, wait_done,  fence, ret);
979 
980 	return ret;
981 }
982 
983 /*
984  * dma_fence_wait(fence, intr)
985  *
986  *	Wait until fence is signalled; or until interrupt, if intr is
987  *	true.  Return -ERESTARTSYS if interrupted, negative error code
988  *	on any other error, zero on sucess.  Works by calling the fence
989  *	wait callback with MAX_SCHEDULE_TIMEOUT.
990  */
991 long
992 dma_fence_wait(struct dma_fence *fence, bool intr)
993 {
994 	long ret;
995 
996 	KASSERT(dma_fence_referenced_p(fence));
997 
998 	ret = dma_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
999 	KASSERT(ret != 0);
1000 	KASSERTMSG(ret == -ERESTARTSYS || ret == MAX_SCHEDULE_TIMEOUT,
1001 	    "ret=%ld", ret);
1002 
1003 	return (ret < 0 ? ret : 0);
1004 }
1005 
1006 /*
1007  * dma_fence_default_wait(fence, intr, timeout)
1008  *
1009  *	Default implementation of fence wait callback using a condition
1010  *	variable.  If the fence is already signalled, return timeout,
1011  *	or 1 if timeout is zero meaning poll.  If the enable signalling
1012  *	callback hasn't been called, call it, and if it fails, act as
1013  *	if the fence had been signalled.  Otherwise, wait on the
1014  *	internal condvar.  If timeout is MAX_SCHEDULE_TIMEOUT, wait
1015  *	indefinitely.
1016  */
1017 long
1018 dma_fence_default_wait(struct dma_fence *fence, bool intr, long timeout)
1019 {
1020 	int starttime = 0, now = 0, deadline = 0; /* XXXGCC */
1021 	kmutex_t *lock = &fence->lock->sl_lock;
1022 	long ret = 0;
1023 
1024 	KASSERT(dma_fence_referenced_p(fence));
1025 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
1026 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
1027 
1028 	/* Optimistically try to skip the lock if it's already signalled.  */
1029 	if (atomic_load_relaxed(&fence->flags) &
1030 	    (1u << DMA_FENCE_FLAG_SIGNALED_BIT))
1031 		return MAX(1, timeout);
1032 
1033 	/* Acquire the lock.  */
1034 	spin_lock(fence->lock);
1035 
1036 	/* Ensure signalling is enabled, or stop if already completed.  */
1037 	if (dma_fence_ensure_signal_enabled(fence) != 0) {
1038 		ret = MAX(1, timeout);
1039 		goto out;
1040 	}
1041 
1042 	/* If merely polling, stop here.  */
1043 	if (timeout == 0) {
1044 		ret = 0;
1045 		goto out;
1046 	}
1047 
1048 	/* Find out what our deadline is so we can handle spurious wakeup.  */
1049 	if (timeout < MAX_SCHEDULE_TIMEOUT) {
1050 		now = getticks();
1051 		starttime = now;
1052 		deadline = starttime + timeout;
1053 	}
1054 
1055 	/* Wait until the signalled bit is set.  */
1056 	while (!(fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT))) {
1057 		/*
1058 		 * If there's a timeout and we've passed the deadline,
1059 		 * give up.
1060 		 */
1061 		if (timeout < MAX_SCHEDULE_TIMEOUT) {
1062 			now = getticks();
1063 			if (deadline <= now) {
1064 				ret = -EWOULDBLOCK;
1065 				break;
1066 			}
1067 		}
1068 
1069 		/* Wait for the time remaining.  */
1070 		if (intr) {
1071 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
1072 				ret = -cv_timedwait_sig(&fence->f_cv, lock,
1073 				    deadline - now);
1074 			} else {
1075 				ret = -cv_wait_sig(&fence->f_cv, lock);
1076 			}
1077 		} else {
1078 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
1079 				ret = -cv_timedwait(&fence->f_cv, lock,
1080 				    deadline - now);
1081 			} else {
1082 				cv_wait(&fence->f_cv, lock);
1083 				ret = 0;
1084 			}
1085 		}
1086 
1087 		/* If the wait failed, give up.  */
1088 		if (ret)
1089 			break;
1090 	}
1091 
1092 	/*
1093 	 * Massage the return code if nonzero:
1094 	 * - if we were interrupted, return -ERESTARTSYS;
1095 	 * - if we timed out, return 0.
1096 	 * No other failure is possible.  On success, ret=0 but we
1097 	 * check again below to verify anyway.
1098 	 */
1099 	if (ret) {
1100 		KASSERTMSG((ret == -EINTR || ret == -ERESTART ||
1101 			ret == -EWOULDBLOCK), "ret=%ld", ret);
1102 		if (ret == -EINTR || ret == -ERESTART) {
1103 			ret = -ERESTARTSYS;
1104 		} else if (ret == -EWOULDBLOCK) {
1105 			KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
1106 			ret = 0;	/* timed out */
1107 		}
1108 	}
1109 
1110 	KASSERT(ret != -ERESTART); /* would be confused with time left */
1111 
1112 	/* Check again in case it was signalled after a wait.  */
1113 	if (fence->flags & (1u << DMA_FENCE_FLAG_SIGNALED_BIT)) {
1114 		if (timeout < MAX_SCHEDULE_TIMEOUT)
1115 			ret = MAX(1, deadline - now);
1116 		else
1117 			ret = MAX_SCHEDULE_TIMEOUT;
1118 	}
1119 
1120 out:	/* All done.  Release the lock.  */
1121 	spin_unlock(fence->lock);
1122 	return ret;
1123 }
1124 
1125 /*
1126  * __dma_fence_signal(fence)
1127  *
1128  *	Set fence's signalled bit, without waking waiters yet.  Return
1129  *	true if it was newly set, false if it was already set.
1130  */
1131 bool
1132 __dma_fence_signal(struct dma_fence *fence)
1133 {
1134 
1135 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
1136 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
1137 
1138 	if (test_and_set_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags))
1139 		return false;
1140 
1141 	return true;
1142 }
1143 
1144 /*
1145  * __dma_fence_signal_wake(fence)
1146  *
1147  *	Set fence's timestamp and wake fence's waiters.  Caller must
1148  *	have previously called __dma_fence_signal and it must have
1149  *	previously returned true.
1150  */
1151 void
1152 __dma_fence_signal_wake(struct dma_fence *fence, ktime_t timestamp)
1153 {
1154 	struct dma_fence_cb *fcb, *next;
1155 
1156 	KASSERTMSG(fence->f_magic != FENCE_MAGIC_BAD, "fence %p", fence);
1157 	KASSERTMSG(fence->f_magic == FENCE_MAGIC_GOOD, "fence %p", fence);
1158 
1159 	spin_lock(fence->lock);
1160 
1161 	KASSERT(fence->flags & DMA_FENCE_FLAG_SIGNALED_BIT);
1162 
1163 	SDT_PROBE1(sdt, drm, fence, signal,  fence);
1164 
1165 	/* Set the timestamp.  */
1166 	fence->timestamp = timestamp;
1167 	set_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags);
1168 
1169 	/* Wake waiters.  */
1170 	cv_broadcast(&fence->f_cv);
1171 
1172 	/* Remove and call the callbacks.  */
1173 	TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
1174 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
1175 		fcb->fcb_onqueue = false;
1176 		(*fcb->func)(fence, fcb);
1177 	}
1178 
1179 	spin_unlock(fence->lock);
1180 }
1181