Home | History | Annotate | Line # | Download | only in linux
fence.h revision 1.13
      1 /*	$NetBSD: fence.h,v 1.13 2018/08/27 13:54:59 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 #ifndef	_LINUX_FENCE_H_
     33 #define	_LINUX_FENCE_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/condvar.h>
     37 #include <sys/kernel.h>
     38 #include <sys/queue.h>
     39 
     40 #include <linux/kref.h>
     41 #include <linux/rcupdate.h>
     42 #include <linux/spinlock.h>
     43 
     44 struct fence_cb;
     45 
     46 struct fence {
     47 	struct kref		refcount;
     48 	spinlock_t		*lock;
     49 	volatile unsigned long	flags;
     50 	unsigned		context;
     51 	unsigned		seqno;
     52 	const struct fence_ops	*ops;
     53 
     54 	TAILQ_HEAD(, fence_cb)	f_callbacks;
     55 	kcondvar_t		f_cv;
     56 	struct rcu_head		f_rcu;
     57 };
     58 
     59 #define	FENCE_FLAG_ENABLE_SIGNAL_BIT	0
     60 #define	FENCE_FLAG_SIGNALED_BIT		1
     61 #define	FENCE_FLAG_USER_BITS		2
     62 
     63 struct fence_ops {
     64 	const char	*(*get_driver_name)(struct fence *);
     65 	const char	*(*get_timeline_name)(struct fence *);
     66 	bool		(*enable_signaling)(struct fence *);
     67 	bool		(*signaled)(struct fence *);
     68 	long		(*wait)(struct fence *, bool, long);
     69 	void		(*release)(struct fence *);
     70 };
     71 
     72 typedef void (*fence_func_t)(struct fence *, struct fence_cb *);
     73 
     74 struct fence_cb {
     75 	fence_func_t		fcb_func;
     76 	TAILQ_ENTRY(fence_cb)	fcb_entry;
     77 	bool			fcb_onqueue;
     78 };
     79 
     80 #define	fence_add_callback	linux_fence_add_callback
     81 #define	fence_context_alloc	linux_fence_context_alloc
     82 #define	fence_default_wait	linux_fence_default_wait
     83 #define	fence_destroy		linux_fence_destroy
     84 #define	fence_enable_sw_signaling linux_fence_enable_sw_signaling
     85 #define	fence_free		linux_fence_free
     86 #define	fence_get		linux_fence_get
     87 #define	fence_get_rcu		linux_fence_get_rcu
     88 #define	fence_init		linux_fence_init
     89 #define	fence_is_signaled	linux_fence_is_signaled
     90 #define	fence_is_signaled_locked linux_fence_is_signaled_locked
     91 #define	fence_put		linux_fence_put
     92 #define	fence_remove_callback	linux_fence_remove_callback
     93 #define	fence_signal		linux_fence_signal
     94 #define	fence_signal_locked	linux_fence_signal_locked
     95 #define	fence_wait		linux_fence_wait
     96 #define	fence_wait_timeout	linux_fence_wait_timeout
     97 
     98 void	fence_init(struct fence *, const struct fence_ops *, spinlock_t *,
     99 	    unsigned, unsigned);
    100 void	fence_destroy(struct fence *);
    101 void	fence_free(struct fence *);
    102 
    103 unsigned
    104 	fence_context_alloc(unsigned);
    105 
    106 struct fence *
    107 	fence_get(struct fence *);
    108 struct fence *
    109 	fence_get_rcu(struct fence *);
    110 void	fence_put(struct fence *);
    111 
    112 int	fence_add_callback(struct fence *, struct fence_cb *, fence_func_t);
    113 bool	fence_remove_callback(struct fence *, struct fence_cb *);
    114 void	fence_enable_sw_signaling(struct fence *);
    115 
    116 bool	fence_is_signaled(struct fence *);
    117 bool	fence_is_signaled_locked(struct fence *);
    118 int	fence_signal(struct fence *);
    119 int	fence_signal_locked(struct fence *);
    120 long	fence_default_wait(struct fence *, bool, long);
    121 long	fence_wait(struct fence *, bool);
    122 long	fence_wait_timeout(struct fence *, bool, long);
    123 
    124 static inline void
    125 FENCE_TRACE(struct fence *f, const char *fmt, ...)
    126 {
    127 	va_list va;
    128 
    129 	va_start(va, fmt);
    130 	printf("fence %u@%u: ", f->context, f->seqno);
    131 	vprintf(fmt, va);
    132 	va_end(va);
    133 }
    134 
    135 #endif	/* _LINUX_FENCE_H_ */
    136