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