Home | History | Annotate | Line # | Download | only in linux
dma-fence.h revision 1.11
      1 /*	$NetBSD: dma-fence.h,v 1.11 2021/12/19 10:58:04 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_DMA_FENCE_H_
     33 #define	_LINUX_DMA_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/err.h>
     41 #include <linux/kref.h>
     42 #include <linux/rcupdate.h>
     43 #include <linux/sched.h>
     44 #include <linux/spinlock.h>
     45 
     46 struct dma_fence_cb;
     47 
     48 struct dma_fence {
     49 	struct kref			refcount;
     50 	spinlock_t			*lock;
     51 	volatile unsigned long		flags;
     52 	unsigned			context;
     53 	unsigned			seqno;
     54 	const struct dma_fence_ops	*ops;
     55 	int				error;
     56 
     57 	TAILQ_HEAD(, dma_fence_cb)	f_callbacks;
     58 	kcondvar_t			f_cv;
     59 	struct rcu_head			f_rcu;
     60 };
     61 
     62 #define	DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT	0
     63 #define	DMA_FENCE_FLAG_SIGNALED_BIT		1
     64 #define	DMA_FENCE_FLAG_USER_BITS		2
     65 
     66 struct dma_fence_ops {
     67 	const char	*(*get_driver_name)(struct dma_fence *);
     68 	const char	*(*get_timeline_name)(struct dma_fence *);
     69 	bool		(*enable_signaling)(struct dma_fence *);
     70 	bool		(*signaled)(struct dma_fence *);
     71 	long		(*wait)(struct dma_fence *, bool, long);
     72 	void		(*release)(struct dma_fence *);
     73 };
     74 
     75 typedef void (*dma_fence_func_t)(struct dma_fence *, struct dma_fence_cb *);
     76 
     77 struct dma_fence_cb {
     78 	dma_fence_func_t		func; /* Linux API name */
     79 	TAILQ_ENTRY(dma_fence_cb)	fcb_entry;
     80 	bool				fcb_onqueue;
     81 };
     82 
     83 #define	dma_fence_add_callback		linux_dma_fence_add_callback
     84 #define	dma_fence_context_alloc		linux_dma_fence_context_alloc
     85 #define	dma_fence_default_wait		linux_dma_fence_default_wait
     86 #define	dma_fence_destroy		linux_dma_fence_destroy
     87 #define	dma_fence_enable_sw_signaling	linux_dma_fence_enable_sw_signaling
     88 #define	dma_fence_free			linux_dma_fence_free
     89 #define	dma_fence_get			linux_dma_fence_get
     90 #define	dma_fence_get_rcu		linux_dma_fence_get_rcu
     91 #define	dma_fence_get_rcu_safe		linux_dma_fence_get_rcu_safe
     92 #define	dma_fence_get_status		linux_dma_fence_get_status
     93 #define	dma_fence_get_stub		linux_dma_fence_get_stub
     94 #define	dma_fence_init			linux_dma_fence_init
     95 #define	dma_fence_is_later		linux_dma_fence_is_later
     96 #define	dma_fence_is_signaled		linux_dma_fence_is_signaled
     97 #define	dma_fence_is_signaled_locked	linux_dma_fence_is_signaled_locked
     98 #define	dma_fence_put			linux_dma_fence_put
     99 #define	dma_fence_remove_callback	linux_dma_fence_remove_callback
    100 #define	dma_fence_set_error		linux_dma_fence_set_error
    101 #define	dma_fence_signal		linux_dma_fence_signal
    102 #define	dma_fence_signal_locked		linux_dma_fence_signal_locked
    103 #define	dma_fence_wait			linux_dma_fence_wait
    104 #define	dma_fence_wait_any_timeout	linux_dma_fence_wait_any_timeout
    105 #define	dma_fence_wait_timeout		linux_dma_fence_wait_timeout
    106 
    107 extern int	linux_dma_fence_trace;
    108 
    109 void	dma_fence_init(struct dma_fence *, const struct dma_fence_ops *,
    110 	    spinlock_t *, unsigned, unsigned);
    111 void	dma_fence_destroy(struct dma_fence *);
    112 void	dma_fence_free(struct dma_fence *);
    113 
    114 unsigned
    115 	dma_fence_context_alloc(unsigned);
    116 bool	dma_fence_is_later(struct dma_fence *, struct dma_fence *);
    117 
    118 struct dma_fence *
    119 	dma_fence_get_stub(void);
    120 
    121 struct dma_fence *
    122 	dma_fence_get(struct dma_fence *);
    123 struct dma_fence *
    124 	dma_fence_get_rcu(struct dma_fence *);
    125 struct dma_fence *
    126 	dma_fence_get_rcu_safe(struct dma_fence *volatile const *);
    127 void	dma_fence_put(struct dma_fence *);
    128 
    129 int	dma_fence_add_callback(struct dma_fence *, struct dma_fence_cb *,
    130 	    dma_fence_func_t);
    131 bool	dma_fence_remove_callback(struct dma_fence *, struct dma_fence_cb *);
    132 void	dma_fence_enable_sw_signaling(struct dma_fence *);
    133 
    134 bool	dma_fence_is_signaled(struct dma_fence *);
    135 bool	dma_fence_is_signaled_locked(struct dma_fence *);
    136 void	dma_fence_set_error(struct dma_fence *, int);
    137 int	dma_fence_get_status(struct dma_fence *);
    138 int	dma_fence_signal(struct dma_fence *);
    139 int	dma_fence_signal_locked(struct dma_fence *);
    140 long	dma_fence_default_wait(struct dma_fence *, bool, long);
    141 long	dma_fence_wait(struct dma_fence *, bool);
    142 long	dma_fence_wait_any_timeout(struct dma_fence **, uint32_t, bool, long,
    143 	    uint32_t *);
    144 long	dma_fence_wait_timeout(struct dma_fence *, bool, long);
    145 
    146 static inline void __printflike(2, 3)
    147 DMA_FENCE_TRACE(struct dma_fence *f, const char *fmt, ...)
    148 {
    149 	va_list va;
    150 
    151 	if (__predict_false(linux_dma_fence_trace)) {
    152 		va_start(va, fmt);
    153 		printf("fence %u@%u: ", f->context, f->seqno);
    154 		vprintf(fmt, va);
    155 		va_end(va);
    156 	}
    157 }
    158 
    159 #endif	/* _LINUX_DMA_FENCE_H_ */
    160