Home | History | Annotate | Line # | Download | only in linux
linux_dma_fence.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: linux_dma_fence.c,v 1.1 2021/12/19 00:27:01 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: linux_dma_fence.c,v 1.1 2021/12/19 00:27:01 riastradh Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/atomic.h>
     36  1.1  riastrad #include <sys/condvar.h>
     37  1.1  riastrad #include <sys/queue.h>
     38  1.1  riastrad 
     39  1.1  riastrad #include <linux/atomic.h>
     40  1.1  riastrad #include <linux/errno.h>
     41  1.1  riastrad #include <linux/kref.h>
     42  1.1  riastrad #include <linux/fence.h>
     43  1.1  riastrad #include <linux/sched.h>
     44  1.1  riastrad #include <linux/spinlock.h>
     45  1.1  riastrad 
     46  1.1  riastrad /*
     47  1.1  riastrad  * linux_fence_trace
     48  1.1  riastrad  *
     49  1.1  riastrad  *	True if we print FENCE_TRACE messages, false if not.  These are
     50  1.1  riastrad  *	extremely noisy, too much even for AB_VERBOSE and AB_DEBUG in
     51  1.1  riastrad  *	boothowto.
     52  1.1  riastrad  */
     53  1.1  riastrad int	linux_fence_trace = 0;
     54  1.1  riastrad 
     55  1.1  riastrad /*
     56  1.1  riastrad  * fence_referenced_p(fence)
     57  1.1  riastrad  *
     58  1.1  riastrad  *	True if fence has a positive reference count.  True after
     59  1.1  riastrad  *	fence_init; after the last fence_put, this becomes false.
     60  1.1  riastrad  */
     61  1.1  riastrad static inline bool __diagused
     62  1.1  riastrad fence_referenced_p(struct fence *fence)
     63  1.1  riastrad {
     64  1.1  riastrad 
     65  1.1  riastrad 	return kref_referenced_p(&fence->refcount);
     66  1.1  riastrad }
     67  1.1  riastrad 
     68  1.1  riastrad /*
     69  1.1  riastrad  * fence_init(fence, ops, lock, context, seqno)
     70  1.1  riastrad  *
     71  1.1  riastrad  *	Initialize fence.  Caller should call fence_destroy when done,
     72  1.1  riastrad  *	after all references have been released.
     73  1.1  riastrad  */
     74  1.1  riastrad void
     75  1.1  riastrad fence_init(struct fence *fence, const struct fence_ops *ops, spinlock_t *lock,
     76  1.1  riastrad     unsigned context, unsigned seqno)
     77  1.1  riastrad {
     78  1.1  riastrad 
     79  1.1  riastrad 	kref_init(&fence->refcount);
     80  1.1  riastrad 	fence->lock = lock;
     81  1.1  riastrad 	fence->flags = 0;
     82  1.1  riastrad 	fence->context = context;
     83  1.1  riastrad 	fence->seqno = seqno;
     84  1.1  riastrad 	fence->ops = ops;
     85  1.1  riastrad 	TAILQ_INIT(&fence->f_callbacks);
     86  1.1  riastrad 	cv_init(&fence->f_cv, "fence");
     87  1.1  riastrad }
     88  1.1  riastrad 
     89  1.1  riastrad /*
     90  1.1  riastrad  * fence_destroy(fence)
     91  1.1  riastrad  *
     92  1.1  riastrad  *	Clean up memory initialized with fence_init.  This is meant to
     93  1.1  riastrad  *	be used after a fence release callback.
     94  1.1  riastrad  */
     95  1.1  riastrad void
     96  1.1  riastrad fence_destroy(struct fence *fence)
     97  1.1  riastrad {
     98  1.1  riastrad 
     99  1.1  riastrad 	KASSERT(!fence_referenced_p(fence));
    100  1.1  riastrad 
    101  1.1  riastrad 	KASSERT(TAILQ_EMPTY(&fence->f_callbacks));
    102  1.1  riastrad 	cv_destroy(&fence->f_cv);
    103  1.1  riastrad }
    104  1.1  riastrad 
    105  1.1  riastrad static void
    106  1.1  riastrad fence_free_cb(struct rcu_head *rcu)
    107  1.1  riastrad {
    108  1.1  riastrad 	struct fence *fence = container_of(rcu, struct fence, f_rcu);
    109  1.1  riastrad 
    110  1.1  riastrad 	KASSERT(!fence_referenced_p(fence));
    111  1.1  riastrad 
    112  1.1  riastrad 	fence_destroy(fence);
    113  1.1  riastrad 	kfree(fence);
    114  1.1  riastrad }
    115  1.1  riastrad 
    116  1.1  riastrad /*
    117  1.1  riastrad  * fence_free(fence)
    118  1.1  riastrad  *
    119  1.1  riastrad  *	Schedule fence to be destroyed and then freed with kfree after
    120  1.1  riastrad  *	any pending RCU read sections on all CPUs have completed.
    121  1.1  riastrad  *	Caller must guarantee all references have been released.  This
    122  1.1  riastrad  *	is meant to be used after a fence release callback.
    123  1.1  riastrad  *
    124  1.1  riastrad  *	NOTE: Callers assume kfree will be used.  We don't even use
    125  1.1  riastrad  *	kmalloc to allocate these -- caller is expected to allocate
    126  1.1  riastrad  *	memory with kmalloc to be initialized with fence_init.
    127  1.1  riastrad  */
    128  1.1  riastrad void
    129  1.1  riastrad fence_free(struct fence *fence)
    130  1.1  riastrad {
    131  1.1  riastrad 
    132  1.1  riastrad 	KASSERT(!fence_referenced_p(fence));
    133  1.1  riastrad 
    134  1.1  riastrad 	call_rcu(&fence->f_rcu, &fence_free_cb);
    135  1.1  riastrad }
    136  1.1  riastrad 
    137  1.1  riastrad /*
    138  1.1  riastrad  * fence_context_alloc(n)
    139  1.1  riastrad  *
    140  1.1  riastrad  *	Return the first of a contiguous sequence of unique
    141  1.1  riastrad  *	identifiers, at least until the system wraps around.
    142  1.1  riastrad  */
    143  1.1  riastrad unsigned
    144  1.1  riastrad fence_context_alloc(unsigned n)
    145  1.1  riastrad {
    146  1.1  riastrad 	static volatile unsigned next_context = 0;
    147  1.1  riastrad 
    148  1.1  riastrad 	return atomic_add_int_nv(&next_context, n) - n;
    149  1.1  riastrad }
    150  1.1  riastrad 
    151  1.1  riastrad /*
    152  1.1  riastrad  * fence_is_later(a, b)
    153  1.1  riastrad  *
    154  1.1  riastrad  *	True if the sequence number of fence a is later than the
    155  1.1  riastrad  *	sequence number of fence b.  Since sequence numbers wrap
    156  1.1  riastrad  *	around, we define this to mean that the sequence number of
    157  1.1  riastrad  *	fence a is no more than INT_MAX past the sequence number of
    158  1.1  riastrad  *	fence b.
    159  1.1  riastrad  *
    160  1.1  riastrad  *	The two fences must have the same context.
    161  1.1  riastrad  */
    162  1.1  riastrad bool
    163  1.1  riastrad fence_is_later(struct fence *a, struct fence *b)
    164  1.1  riastrad {
    165  1.1  riastrad 
    166  1.1  riastrad 	KASSERTMSG(a->context == b->context, "incommensurate fences"
    167  1.1  riastrad 	    ": %u @ %p =/= %u @ %p", a->context, a, b->context, b);
    168  1.1  riastrad 
    169  1.1  riastrad 	return a->seqno - b->seqno < INT_MAX;
    170  1.1  riastrad }
    171  1.1  riastrad 
    172  1.1  riastrad /*
    173  1.1  riastrad  * fence_get(fence)
    174  1.1  riastrad  *
    175  1.1  riastrad  *	Acquire a reference to fence.  The fence must not be being
    176  1.1  riastrad  *	destroyed.  Return the fence.
    177  1.1  riastrad  */
    178  1.1  riastrad struct fence *
    179  1.1  riastrad fence_get(struct fence *fence)
    180  1.1  riastrad {
    181  1.1  riastrad 
    182  1.1  riastrad 	if (fence)
    183  1.1  riastrad 		kref_get(&fence->refcount);
    184  1.1  riastrad 	return fence;
    185  1.1  riastrad }
    186  1.1  riastrad 
    187  1.1  riastrad /*
    188  1.1  riastrad  * fence_get_rcu(fence)
    189  1.1  riastrad  *
    190  1.1  riastrad  *	Attempt to acquire a reference to a fence that may be about to
    191  1.1  riastrad  *	be destroyed, during a read section.  Return the fence on
    192  1.1  riastrad  *	success, or NULL on failure.
    193  1.1  riastrad  */
    194  1.1  riastrad struct fence *
    195  1.1  riastrad fence_get_rcu(struct fence *fence)
    196  1.1  riastrad {
    197  1.1  riastrad 
    198  1.1  riastrad 	if (!kref_get_unless_zero(&fence->refcount))
    199  1.1  riastrad 		return NULL;
    200  1.1  riastrad 	return fence;
    201  1.1  riastrad }
    202  1.1  riastrad 
    203  1.1  riastrad static void
    204  1.1  riastrad fence_release(struct kref *refcount)
    205  1.1  riastrad {
    206  1.1  riastrad 	struct fence *fence = container_of(refcount, struct fence, refcount);
    207  1.1  riastrad 
    208  1.1  riastrad 	KASSERT(!fence_referenced_p(fence));
    209  1.1  riastrad 
    210  1.1  riastrad 	if (fence->ops->release)
    211  1.1  riastrad 		(*fence->ops->release)(fence);
    212  1.1  riastrad 	else
    213  1.1  riastrad 		fence_free(fence);
    214  1.1  riastrad }
    215  1.1  riastrad 
    216  1.1  riastrad /*
    217  1.1  riastrad  * fence_put(fence)
    218  1.1  riastrad  *
    219  1.1  riastrad  *	Release a reference to fence.  If this was the last one, call
    220  1.1  riastrad  *	the fence's release callback.
    221  1.1  riastrad  */
    222  1.1  riastrad void
    223  1.1  riastrad fence_put(struct fence *fence)
    224  1.1  riastrad {
    225  1.1  riastrad 
    226  1.1  riastrad 	if (fence == NULL)
    227  1.1  riastrad 		return;
    228  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    229  1.1  riastrad 	kref_put(&fence->refcount, &fence_release);
    230  1.1  riastrad }
    231  1.1  riastrad 
    232  1.1  riastrad /*
    233  1.1  riastrad  * fence_ensure_signal_enabled(fence)
    234  1.1  riastrad  *
    235  1.1  riastrad  *	Internal subroutine.  If the fence was already signalled,
    236  1.1  riastrad  *	return -ENOENT.  Otherwise, if the enable signalling callback
    237  1.1  riastrad  *	has not been called yet, call it.  If fails, signal the fence
    238  1.1  riastrad  *	and return -ENOENT.  If it succeeds, or if it had already been
    239  1.1  riastrad  *	called, return zero to indicate success.
    240  1.1  riastrad  *
    241  1.1  riastrad  *	Caller must hold the fence's lock.
    242  1.1  riastrad  */
    243  1.1  riastrad static int
    244  1.1  riastrad fence_ensure_signal_enabled(struct fence *fence)
    245  1.1  riastrad {
    246  1.1  riastrad 
    247  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    248  1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    249  1.1  riastrad 
    250  1.1  riastrad 	/* If the fence was already signalled, fail with -ENOENT.  */
    251  1.1  riastrad 	if (fence->flags & (1u << FENCE_FLAG_SIGNALED_BIT))
    252  1.1  riastrad 		return -ENOENT;
    253  1.1  riastrad 
    254  1.1  riastrad 	/*
    255  1.1  riastrad 	 * If the enable signaling callback has been called, success.
    256  1.1  riastrad 	 * Otherwise, set the bit indicating it.
    257  1.1  riastrad 	 */
    258  1.1  riastrad 	if (test_and_set_bit(FENCE_FLAG_ENABLE_SIGNAL_BIT, &fence->flags))
    259  1.1  riastrad 		return 0;
    260  1.1  riastrad 
    261  1.1  riastrad 	/* Otherwise, note that we've called it and call it.  */
    262  1.1  riastrad 	if (!(*fence->ops->enable_signaling)(fence)) {
    263  1.1  riastrad 		/* If it failed, signal and return -ENOENT.  */
    264  1.1  riastrad 		fence_signal_locked(fence);
    265  1.1  riastrad 		return -ENOENT;
    266  1.1  riastrad 	}
    267  1.1  riastrad 
    268  1.1  riastrad 	/* Success!  */
    269  1.1  riastrad 	return 0;
    270  1.1  riastrad }
    271  1.1  riastrad 
    272  1.1  riastrad /*
    273  1.1  riastrad  * fence_add_callback(fence, fcb, fn)
    274  1.1  riastrad  *
    275  1.1  riastrad  *	If fence has been signalled, return -ENOENT.  If the enable
    276  1.1  riastrad  *	signalling callback hasn't been called yet, call it; if it
    277  1.1  riastrad  *	fails, return -ENOENT.  Otherwise, arrange to call fn(fence,
    278  1.1  riastrad  *	fcb) when it is signalled, and return 0.
    279  1.1  riastrad  *
    280  1.1  riastrad  *	The fence uses memory allocated by the caller in fcb from the
    281  1.1  riastrad  *	time of fence_add_callback either to the time of
    282  1.1  riastrad  *	fence_remove_callback, or just before calling fn.
    283  1.1  riastrad  */
    284  1.1  riastrad int
    285  1.1  riastrad fence_add_callback(struct fence *fence, struct fence_cb *fcb, fence_func_t fn)
    286  1.1  riastrad {
    287  1.1  riastrad 	int ret;
    288  1.1  riastrad 
    289  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    290  1.1  riastrad 
    291  1.1  riastrad 	/* Optimistically try to skip the lock if it's already signalled.  */
    292  1.1  riastrad 	if (fence->flags & (1u << FENCE_FLAG_SIGNALED_BIT)) {
    293  1.1  riastrad 		ret = -ENOENT;
    294  1.1  riastrad 		goto out0;
    295  1.1  riastrad 	}
    296  1.1  riastrad 
    297  1.1  riastrad 	/* Acquire the lock.  */
    298  1.1  riastrad 	spin_lock(fence->lock);
    299  1.1  riastrad 
    300  1.1  riastrad 	/* Ensure signalling is enabled, or fail if we can't.  */
    301  1.1  riastrad 	ret = fence_ensure_signal_enabled(fence);
    302  1.1  riastrad 	if (ret)
    303  1.1  riastrad 		goto out1;
    304  1.1  riastrad 
    305  1.1  riastrad 	/* Insert the callback.  */
    306  1.1  riastrad 	fcb->fcb_func = fn;
    307  1.1  riastrad 	TAILQ_INSERT_TAIL(&fence->f_callbacks, fcb, fcb_entry);
    308  1.1  riastrad 	fcb->fcb_onqueue = true;
    309  1.1  riastrad 
    310  1.1  riastrad 	/* Release the lock and we're done.  */
    311  1.1  riastrad out1:	spin_unlock(fence->lock);
    312  1.1  riastrad out0:	return ret;
    313  1.1  riastrad }
    314  1.1  riastrad 
    315  1.1  riastrad /*
    316  1.1  riastrad  * fence_remove_callback(fence, fcb)
    317  1.1  riastrad  *
    318  1.1  riastrad  *	Remove the callback fcb from fence.  Return true if it was
    319  1.1  riastrad  *	removed from the list, or false if it had already run and so
    320  1.1  riastrad  *	was no longer queued anyway.  Caller must have already called
    321  1.1  riastrad  *	fence_add_callback(fence, fcb).
    322  1.1  riastrad  */
    323  1.1  riastrad bool
    324  1.1  riastrad fence_remove_callback(struct fence *fence, struct fence_cb *fcb)
    325  1.1  riastrad {
    326  1.1  riastrad 	bool onqueue;
    327  1.1  riastrad 
    328  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    329  1.1  riastrad 
    330  1.1  riastrad 	spin_lock(fence->lock);
    331  1.1  riastrad 	onqueue = fcb->fcb_onqueue;
    332  1.1  riastrad 	if (onqueue) {
    333  1.1  riastrad 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
    334  1.1  riastrad 		fcb->fcb_onqueue = false;
    335  1.1  riastrad 	}
    336  1.1  riastrad 	spin_unlock(fence->lock);
    337  1.1  riastrad 
    338  1.1  riastrad 	return onqueue;
    339  1.1  riastrad }
    340  1.1  riastrad 
    341  1.1  riastrad /*
    342  1.1  riastrad  * fence_enable_sw_signaling(fence)
    343  1.1  riastrad  *
    344  1.1  riastrad  *	If it hasn't been called yet and the fence hasn't been
    345  1.1  riastrad  *	signalled yet, call the fence's enable_sw_signaling callback.
    346  1.1  riastrad  *	If when that happens, the callback indicates failure by
    347  1.1  riastrad  *	returning false, signal the fence.
    348  1.1  riastrad  */
    349  1.1  riastrad void
    350  1.1  riastrad fence_enable_sw_signaling(struct fence *fence)
    351  1.1  riastrad {
    352  1.1  riastrad 
    353  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    354  1.1  riastrad 
    355  1.1  riastrad 	spin_lock(fence->lock);
    356  1.1  riastrad 	(void)fence_ensure_signal_enabled(fence);
    357  1.1  riastrad 	spin_unlock(fence->lock);
    358  1.1  riastrad }
    359  1.1  riastrad 
    360  1.1  riastrad /*
    361  1.1  riastrad  * fence_is_signaled(fence)
    362  1.1  riastrad  *
    363  1.1  riastrad  *	Test whether the fence has been signalled.  If it has been
    364  1.1  riastrad  *	signalled by fence_signal(_locked), return true.  If the
    365  1.1  riastrad  *	signalled callback returns true indicating that some implicit
    366  1.1  riastrad  *	external condition has changed, call the callbacks as if with
    367  1.1  riastrad  *	fence_signal.
    368  1.1  riastrad  */
    369  1.1  riastrad bool
    370  1.1  riastrad fence_is_signaled(struct fence *fence)
    371  1.1  riastrad {
    372  1.1  riastrad 	bool signaled;
    373  1.1  riastrad 
    374  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    375  1.1  riastrad 
    376  1.1  riastrad 	spin_lock(fence->lock);
    377  1.1  riastrad 	signaled = fence_is_signaled_locked(fence);
    378  1.1  riastrad 	spin_unlock(fence->lock);
    379  1.1  riastrad 
    380  1.1  riastrad 	return signaled;
    381  1.1  riastrad }
    382  1.1  riastrad 
    383  1.1  riastrad /*
    384  1.1  riastrad  * fence_is_signaled_locked(fence)
    385  1.1  riastrad  *
    386  1.1  riastrad  *	Test whether the fence has been signalled.  Like
    387  1.1  riastrad  *	fence_is_signaleed, but caller already holds the fence's lock.
    388  1.1  riastrad  */
    389  1.1  riastrad bool
    390  1.1  riastrad fence_is_signaled_locked(struct fence *fence)
    391  1.1  riastrad {
    392  1.1  riastrad 
    393  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    394  1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    395  1.1  riastrad 
    396  1.1  riastrad 	/* Check whether we already set the signalled bit.  */
    397  1.1  riastrad 	if (fence->flags & (1u << FENCE_FLAG_SIGNALED_BIT))
    398  1.1  riastrad 		return true;
    399  1.1  riastrad 
    400  1.1  riastrad 	/* If there's a signalled callback, test it.  */
    401  1.1  riastrad 	if (fence->ops->signaled) {
    402  1.1  riastrad 		if ((*fence->ops->signaled)(fence)) {
    403  1.1  riastrad 			/*
    404  1.1  riastrad 			 * It's been signalled implicitly by some
    405  1.1  riastrad 			 * external phenomonen.  Act as though someone
    406  1.1  riastrad 			 * has called fence_signal.
    407  1.1  riastrad 			 */
    408  1.1  riastrad 			fence_signal_locked(fence);
    409  1.1  riastrad 			return true;
    410  1.1  riastrad 		}
    411  1.1  riastrad 	}
    412  1.1  riastrad 
    413  1.1  riastrad 	return false;
    414  1.1  riastrad }
    415  1.1  riastrad 
    416  1.1  riastrad /*
    417  1.1  riastrad  * fence_signal(fence)
    418  1.1  riastrad  *
    419  1.1  riastrad  *	Signal the fence.  If it has already been signalled, return
    420  1.1  riastrad  *	-EINVAL.  If it has not been signalled, call the enable
    421  1.1  riastrad  *	signalling callback if it hasn't been called yet, and remove
    422  1.1  riastrad  *	each registered callback from the queue and call it; then
    423  1.1  riastrad  *	return 0.
    424  1.1  riastrad  */
    425  1.1  riastrad int
    426  1.1  riastrad fence_signal(struct fence *fence)
    427  1.1  riastrad {
    428  1.1  riastrad 	int ret;
    429  1.1  riastrad 
    430  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    431  1.1  riastrad 
    432  1.1  riastrad 	spin_lock(fence->lock);
    433  1.1  riastrad 	ret = fence_signal_locked(fence);
    434  1.1  riastrad 	spin_unlock(fence->lock);
    435  1.1  riastrad 
    436  1.1  riastrad 	return ret;
    437  1.1  riastrad }
    438  1.1  riastrad 
    439  1.1  riastrad /*
    440  1.1  riastrad  * fence_signal_locked(fence)
    441  1.1  riastrad  *
    442  1.1  riastrad  *	Signal the fence.  Like fence_signal, but caller already holds
    443  1.1  riastrad  *	the fence's lock.
    444  1.1  riastrad  */
    445  1.1  riastrad int
    446  1.1  riastrad fence_signal_locked(struct fence *fence)
    447  1.1  riastrad {
    448  1.1  riastrad 	struct fence_cb *fcb, *next;
    449  1.1  riastrad 
    450  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    451  1.1  riastrad 	KASSERT(spin_is_locked(fence->lock));
    452  1.1  riastrad 
    453  1.1  riastrad 	/* If it's been signalled, fail; otherwise set the signalled bit.  */
    454  1.1  riastrad 	if (test_and_set_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
    455  1.1  riastrad 		return -EINVAL;
    456  1.1  riastrad 
    457  1.1  riastrad 	/* Wake waiters.  */
    458  1.1  riastrad 	cv_broadcast(&fence->f_cv);
    459  1.1  riastrad 
    460  1.1  riastrad 	/* Remove and call the callbacks.  */
    461  1.1  riastrad 	TAILQ_FOREACH_SAFE(fcb, &fence->f_callbacks, fcb_entry, next) {
    462  1.1  riastrad 		TAILQ_REMOVE(&fence->f_callbacks, fcb, fcb_entry);
    463  1.1  riastrad 		fcb->fcb_onqueue = false;
    464  1.1  riastrad 		(*fcb->fcb_func)(fence, fcb);
    465  1.1  riastrad 	}
    466  1.1  riastrad 
    467  1.1  riastrad 	/* Success! */
    468  1.1  riastrad 	return 0;
    469  1.1  riastrad }
    470  1.1  riastrad 
    471  1.1  riastrad struct wait_any {
    472  1.1  riastrad 	struct fence_cb	fcb;
    473  1.1  riastrad 	struct wait_any1 {
    474  1.1  riastrad 		kmutex_t	lock;
    475  1.1  riastrad 		kcondvar_t	cv;
    476  1.1  riastrad 		bool		done;
    477  1.1  riastrad 	}		*common;
    478  1.1  riastrad };
    479  1.1  riastrad 
    480  1.1  riastrad static void
    481  1.1  riastrad wait_any_cb(struct fence *fence, struct fence_cb *fcb)
    482  1.1  riastrad {
    483  1.1  riastrad 	struct wait_any *cb = container_of(fcb, struct wait_any, fcb);
    484  1.1  riastrad 
    485  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    486  1.1  riastrad 
    487  1.1  riastrad 	mutex_enter(&cb->common->lock);
    488  1.1  riastrad 	cb->common->done = true;
    489  1.1  riastrad 	cv_broadcast(&cb->common->cv);
    490  1.1  riastrad 	mutex_exit(&cb->common->lock);
    491  1.1  riastrad }
    492  1.1  riastrad 
    493  1.1  riastrad /*
    494  1.1  riastrad  * fence_wait_any_timeout(fence, nfences, intr, timeout)
    495  1.1  riastrad  *
    496  1.1  riastrad  *	Wait for any of fences[0], fences[1], fences[2], ...,
    497  1.1  riastrad  *	fences[nfences-1] to be signaled.
    498  1.1  riastrad  */
    499  1.1  riastrad long
    500  1.1  riastrad fence_wait_any_timeout(struct fence **fences, uint32_t nfences, bool intr,
    501  1.1  riastrad     long timeout)
    502  1.1  riastrad {
    503  1.1  riastrad 	struct wait_any1 common;
    504  1.1  riastrad 	struct wait_any *cb;
    505  1.1  riastrad 	uint32_t i, j;
    506  1.1  riastrad 	int start, end;
    507  1.1  riastrad 	long ret = 0;
    508  1.1  riastrad 
    509  1.1  riastrad 	/* Allocate an array of callback records.  */
    510  1.1  riastrad 	cb = kcalloc(nfences, sizeof(cb[0]), GFP_KERNEL);
    511  1.1  riastrad 	if (cb == NULL) {
    512  1.1  riastrad 		ret = -ENOMEM;
    513  1.1  riastrad 		goto out0;
    514  1.1  riastrad 	}
    515  1.1  riastrad 
    516  1.1  riastrad 	/* Initialize a mutex and condvar for the common wait.  */
    517  1.1  riastrad 	mutex_init(&common.lock, MUTEX_DEFAULT, IPL_VM);
    518  1.1  riastrad 	cv_init(&common.cv, "fence");
    519  1.1  riastrad 	common.done = false;
    520  1.1  riastrad 
    521  1.1  riastrad 	/* Add a callback to each of the fences, or stop here if we can't.  */
    522  1.1  riastrad 	for (i = 0; i < nfences; i++) {
    523  1.1  riastrad 		cb[i].common = &common;
    524  1.1  riastrad 		KASSERT(fence_referenced_p(fences[i]));
    525  1.1  riastrad 		ret = fence_add_callback(fences[i], &cb[i].fcb, &wait_any_cb);
    526  1.1  riastrad 		if (ret)
    527  1.1  riastrad 			goto out1;
    528  1.1  riastrad 	}
    529  1.1  riastrad 
    530  1.1  riastrad 	/*
    531  1.1  riastrad 	 * Test whether any of the fences has been signalled.  If they
    532  1.1  riastrad 	 * have, stop here.  If the haven't, we are guaranteed to be
    533  1.1  riastrad 	 * notified by one of the callbacks when they have.
    534  1.1  riastrad 	 */
    535  1.1  riastrad 	for (j = 0; j < nfences; j++) {
    536  1.1  riastrad 		if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fences[j]->flags))
    537  1.1  riastrad 			goto out1;
    538  1.1  riastrad 	}
    539  1.1  riastrad 
    540  1.1  riastrad 	/*
    541  1.1  riastrad 	 * None of them was ready immediately.  Wait for one of the
    542  1.1  riastrad 	 * callbacks to notify us when it is done.
    543  1.1  riastrad 	 */
    544  1.1  riastrad 	mutex_enter(&common.lock);
    545  1.1  riastrad 	while (timeout > 0 && !common.done) {
    546  1.1  riastrad 		start = getticks();
    547  1.1  riastrad 		__insn_barrier();
    548  1.1  riastrad 		if (intr) {
    549  1.1  riastrad 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
    550  1.1  riastrad 				ret = -cv_timedwait_sig(&common.cv,
    551  1.1  riastrad 				    &common.lock, MIN(timeout, /* paranoia */
    552  1.1  riastrad 					MAX_SCHEDULE_TIMEOUT));
    553  1.1  riastrad 			} else {
    554  1.1  riastrad 				ret = -cv_wait_sig(&common.cv, &common.lock);
    555  1.1  riastrad 			}
    556  1.1  riastrad 		} else {
    557  1.1  riastrad 			if (timeout != MAX_SCHEDULE_TIMEOUT) {
    558  1.1  riastrad 				ret = -cv_timedwait(&common.cv,
    559  1.1  riastrad 				    &common.lock, MIN(timeout, /* paranoia */
    560  1.1  riastrad 					MAX_SCHEDULE_TIMEOUT));
    561  1.1  riastrad 			} else {
    562  1.1  riastrad 				cv_wait(&common.cv, &common.lock);
    563  1.1  riastrad 				ret = 0;
    564  1.1  riastrad 			}
    565  1.1  riastrad 		}
    566  1.1  riastrad 		end = getticks();
    567  1.1  riastrad 		__insn_barrier();
    568  1.1  riastrad 		if (ret) {
    569  1.1  riastrad 			if (ret == -ERESTART)
    570  1.1  riastrad 				ret = -ERESTARTSYS;
    571  1.1  riastrad 			break;
    572  1.1  riastrad 		}
    573  1.1  riastrad 		timeout -= MIN(timeout, (unsigned)end - (unsigned)start);
    574  1.1  riastrad 	}
    575  1.1  riastrad 	mutex_exit(&common.lock);
    576  1.1  riastrad 
    577  1.1  riastrad 	/*
    578  1.1  riastrad 	 * Massage the return code: if we were interrupted, return
    579  1.1  riastrad 	 * ERESTARTSYS; if cv_timedwait timed out, return 0; otherwise
    580  1.1  riastrad 	 * return the remaining time.
    581  1.1  riastrad 	 */
    582  1.1  riastrad 	if (ret < 0) {
    583  1.1  riastrad 		if (ret == -EINTR || ret == -ERESTART)
    584  1.1  riastrad 			ret = -ERESTARTSYS;
    585  1.1  riastrad 		if (ret == -EWOULDBLOCK)
    586  1.1  riastrad 			ret = 0;
    587  1.1  riastrad 	} else {
    588  1.1  riastrad 		KASSERT(ret == 0);
    589  1.1  riastrad 		ret = timeout;
    590  1.1  riastrad 	}
    591  1.1  riastrad 
    592  1.1  riastrad out1:	while (i --> 0)
    593  1.1  riastrad 		(void)fence_remove_callback(fences[i], &cb[i].fcb);
    594  1.1  riastrad 	cv_destroy(&common.cv);
    595  1.1  riastrad 	mutex_destroy(&common.lock);
    596  1.1  riastrad 	kfree(cb);
    597  1.1  riastrad out0:	return ret;
    598  1.1  riastrad }
    599  1.1  riastrad 
    600  1.1  riastrad /*
    601  1.1  riastrad  * fence_wait_timeout(fence, intr, timeout)
    602  1.1  riastrad  *
    603  1.1  riastrad  *	Wait until fence is signalled; or until interrupt, if intr is
    604  1.1  riastrad  *	true; or until timeout, if positive.  Return -ERESTARTSYS if
    605  1.1  riastrad  *	interrupted, negative error code on any other error, zero on
    606  1.1  riastrad  *	timeout, or positive number of ticks remaining if the fence is
    607  1.1  riastrad  *	signalled before the timeout.  Works by calling the fence wait
    608  1.1  riastrad  *	callback.
    609  1.1  riastrad  *
    610  1.1  riastrad  *	The timeout must be nonnegative and less than
    611  1.1  riastrad  *	MAX_SCHEDULE_TIMEOUT.
    612  1.1  riastrad  */
    613  1.1  riastrad long
    614  1.1  riastrad fence_wait_timeout(struct fence *fence, bool intr, long timeout)
    615  1.1  riastrad {
    616  1.1  riastrad 
    617  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    618  1.1  riastrad 	KASSERT(timeout >= 0);
    619  1.1  riastrad 	KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
    620  1.1  riastrad 
    621  1.1  riastrad 	return (*fence->ops->wait)(fence, intr, timeout);
    622  1.1  riastrad }
    623  1.1  riastrad 
    624  1.1  riastrad /*
    625  1.1  riastrad  * fence_wait(fence, intr)
    626  1.1  riastrad  *
    627  1.1  riastrad  *	Wait until fence is signalled; or until interrupt, if intr is
    628  1.1  riastrad  *	true.  Return -ERESTARTSYS if interrupted, negative error code
    629  1.1  riastrad  *	on any other error, zero on sucess.  Works by calling the fence
    630  1.1  riastrad  *	wait callback with MAX_SCHEDULE_TIMEOUT.
    631  1.1  riastrad  */
    632  1.1  riastrad long
    633  1.1  riastrad fence_wait(struct fence *fence, bool intr)
    634  1.1  riastrad {
    635  1.1  riastrad 	long ret;
    636  1.1  riastrad 
    637  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    638  1.1  riastrad 
    639  1.1  riastrad 	ret = (*fence->ops->wait)(fence, intr, MAX_SCHEDULE_TIMEOUT);
    640  1.1  riastrad 	KASSERT(ret != 0);
    641  1.1  riastrad 
    642  1.1  riastrad 	return (ret < 0 ? ret : 0);
    643  1.1  riastrad }
    644  1.1  riastrad 
    645  1.1  riastrad /*
    646  1.1  riastrad  * fence_default_wait(fence, intr, timeout)
    647  1.1  riastrad  *
    648  1.1  riastrad  *	Default implementation of fence wait callback using a condition
    649  1.1  riastrad  *	variable.  If the fence is already signalled, return timeout,
    650  1.1  riastrad  *	or 1 if no timeout.  If the enable signalling callback hasn't
    651  1.1  riastrad  *	been called, call it, and if it fails, act as if the fence had
    652  1.1  riastrad  *	been signalled.  Otherwise, wait on the internal condvar.  If
    653  1.1  riastrad  *	timeout is MAX_SCHEDULE_TIMEOUT, treat it as no timeout.
    654  1.1  riastrad  */
    655  1.1  riastrad long
    656  1.1  riastrad fence_default_wait(struct fence *fence, bool intr, long timeout)
    657  1.1  riastrad {
    658  1.1  riastrad 	int starttime = 0, now = 0, deadline = 0; /* XXXGCC */
    659  1.1  riastrad 	kmutex_t *lock = &fence->lock->sl_lock;
    660  1.1  riastrad 	long ret = 0;
    661  1.1  riastrad 
    662  1.1  riastrad 	KASSERT(fence_referenced_p(fence));
    663  1.1  riastrad 	KASSERTMSG(timeout >= 0, "timeout %ld", timeout);
    664  1.1  riastrad 	KASSERTMSG(timeout <= MAX_SCHEDULE_TIMEOUT, "timeout %ld", timeout);
    665  1.1  riastrad 
    666  1.1  riastrad 	/* Optimistically try to skip the lock if it's already signalled.  */
    667  1.1  riastrad 	if (fence->flags & (1u << FENCE_FLAG_SIGNALED_BIT))
    668  1.1  riastrad 		return (timeout < MAX_SCHEDULE_TIMEOUT ? timeout : 1);
    669  1.1  riastrad 
    670  1.1  riastrad 	/* Acquire the lock.  */
    671  1.1  riastrad 	spin_lock(fence->lock);
    672  1.1  riastrad 
    673  1.1  riastrad 	/* Ensure signalling is enabled, or fail if we can't.  */
    674  1.1  riastrad 	ret = fence_ensure_signal_enabled(fence);
    675  1.1  riastrad 	if (ret)
    676  1.1  riastrad 		goto out;
    677  1.1  riastrad 
    678  1.1  riastrad 	/* Find out what our deadline is so we can handle spurious wakeup.  */
    679  1.1  riastrad 	if (timeout < MAX_SCHEDULE_TIMEOUT) {
    680  1.1  riastrad 		now = getticks();
    681  1.1  riastrad 		__insn_barrier();
    682  1.1  riastrad 		starttime = now;
    683  1.1  riastrad 		deadline = starttime + timeout;
    684  1.1  riastrad 	}
    685  1.1  riastrad 
    686  1.1  riastrad 	/* Wait until the signalled bit is set.  */
    687  1.1  riastrad 	while (!(fence->flags & (1u << FENCE_FLAG_SIGNALED_BIT))) {
    688  1.1  riastrad 		/*
    689  1.1  riastrad 		 * If there's a timeout and we've passed the deadline,
    690  1.1  riastrad 		 * give up.
    691  1.1  riastrad 		 */
    692  1.1  riastrad 		if (timeout < MAX_SCHEDULE_TIMEOUT) {
    693  1.1  riastrad 			now = getticks();
    694  1.1  riastrad 			__insn_barrier();
    695  1.1  riastrad 			if (deadline <= now)
    696  1.1  riastrad 				break;
    697  1.1  riastrad 		}
    698  1.1  riastrad 		if (intr) {
    699  1.1  riastrad 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
    700  1.1  riastrad 				ret = -cv_timedwait_sig(&fence->f_cv, lock,
    701  1.1  riastrad 				    deadline - now);
    702  1.1  riastrad 			} else {
    703  1.1  riastrad 				ret = -cv_wait_sig(&fence->f_cv, lock);
    704  1.1  riastrad 			}
    705  1.1  riastrad 		} else {
    706  1.1  riastrad 			if (timeout < MAX_SCHEDULE_TIMEOUT) {
    707  1.1  riastrad 				ret = -cv_timedwait(&fence->f_cv, lock,
    708  1.1  riastrad 				    deadline - now);
    709  1.1  riastrad 			} else {
    710  1.1  riastrad 				cv_wait(&fence->f_cv, lock);
    711  1.1  riastrad 				ret = 0;
    712  1.1  riastrad 			}
    713  1.1  riastrad 		}
    714  1.1  riastrad 		/* If the wait failed, give up.  */
    715  1.1  riastrad 		if (ret) {
    716  1.1  riastrad 			if (ret == -ERESTART)
    717  1.1  riastrad 				ret = -ERESTARTSYS;
    718  1.1  riastrad 			break;
    719  1.1  riastrad 		}
    720  1.1  riastrad 	}
    721  1.1  riastrad 
    722  1.1  riastrad out:
    723  1.1  riastrad 	/* All done.  Release the lock.  */
    724  1.1  riastrad 	spin_unlock(fence->lock);
    725  1.1  riastrad 
    726  1.1  riastrad 	/* If cv_timedwait gave up, return 0 meaning timeout.  */
    727  1.1  riastrad 	if (ret == -EWOULDBLOCK) {
    728  1.1  riastrad 		/* Only cv_timedwait and cv_timedwait_sig can return this.  */
    729  1.1  riastrad 		KASSERT(timeout < MAX_SCHEDULE_TIMEOUT);
    730  1.1  riastrad 		return 0;
    731  1.1  riastrad 	}
    732  1.1  riastrad 
    733  1.1  riastrad 	/* If there was a timeout and the deadline passed, return 0.  */
    734  1.1  riastrad 	if (timeout < MAX_SCHEDULE_TIMEOUT) {
    735  1.1  riastrad 		if (deadline <= now)
    736  1.1  riastrad 			return 0;
    737  1.1  riastrad 	}
    738  1.1  riastrad 
    739  1.1  riastrad 	/* If we were interrupted, return -ERESTARTSYS.  */
    740  1.1  riastrad 	if (ret == -EINTR || ret == -ERESTART)
    741  1.1  riastrad 		return -ERESTARTSYS;
    742  1.1  riastrad 
    743  1.1  riastrad 	/* If there was any other kind of error, fail.  */
    744  1.1  riastrad 	if (ret)
    745  1.1  riastrad 		return ret;
    746  1.1  riastrad 
    747  1.1  riastrad 	/*
    748  1.1  riastrad 	 * Success!  Return the number of ticks left, at least 1, or 1
    749  1.1  riastrad 	 * if no timeout.
    750  1.1  riastrad 	 */
    751  1.1  riastrad 	return (timeout < MAX_SCHEDULE_TIMEOUT ? MIN(deadline - now, 1) : 1);
    752  1.1  riastrad }
    753