Home | History | Annotate | Line # | Download | only in linux
dma-fence.h revision 1.16
      1  1.16  riastrad /*	$NetBSD: dma-fence.h,v 1.16 2021/12/19 12:39:24 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.2  riastrad #ifndef	_LINUX_DMA_FENCE_H_
     33   1.2  riastrad #define	_LINUX_DMA_FENCE_H_
     34   1.1  riastrad 
     35   1.1  riastrad #include <sys/types.h>
     36   1.1  riastrad #include <sys/condvar.h>
     37   1.1  riastrad #include <sys/kernel.h>
     38   1.1  riastrad #include <sys/queue.h>
     39   1.1  riastrad 
     40   1.9  riastrad #include <linux/err.h>
     41   1.1  riastrad #include <linux/kref.h>
     42  1.12  riastrad #include <linux/ktime.h>
     43   1.1  riastrad #include <linux/rcupdate.h>
     44   1.9  riastrad #include <linux/sched.h>
     45   1.1  riastrad #include <linux/spinlock.h>
     46   1.1  riastrad 
     47   1.2  riastrad struct dma_fence_cb;
     48   1.1  riastrad 
     49   1.2  riastrad struct dma_fence {
     50   1.2  riastrad 	struct kref			refcount;
     51   1.2  riastrad 	spinlock_t			*lock;
     52   1.2  riastrad 	volatile unsigned long		flags;
     53  1.16  riastrad 	uint64_t			context;
     54  1.16  riastrad 	uint64_t			seqno;
     55   1.2  riastrad 	const struct dma_fence_ops	*ops;
     56   1.5  riastrad 	int				error;
     57  1.12  riastrad 	ktime_t				timestamp;
     58  1.14  riastrad 	struct rcu_head			rcu;
     59   1.2  riastrad 
     60   1.2  riastrad 	TAILQ_HEAD(, dma_fence_cb)	f_callbacks;
     61   1.2  riastrad 	kcondvar_t			f_cv;
     62  1.15  riastrad 	uint64_t			f_magic;
     63   1.1  riastrad };
     64   1.1  riastrad 
     65   1.2  riastrad #define	DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT	0
     66   1.2  riastrad #define	DMA_FENCE_FLAG_SIGNALED_BIT		1
     67  1.12  riastrad #define	DMA_FENCE_FLAG_TIMESTAMP_BIT		2
     68  1.12  riastrad #define	DMA_FENCE_FLAG_USER_BITS		3
     69   1.2  riastrad 
     70   1.2  riastrad struct dma_fence_ops {
     71  1.16  riastrad 	bool		use_64bit_seqno;
     72   1.2  riastrad 	const char	*(*get_driver_name)(struct dma_fence *);
     73   1.2  riastrad 	const char	*(*get_timeline_name)(struct dma_fence *);
     74   1.2  riastrad 	bool		(*enable_signaling)(struct dma_fence *);
     75   1.2  riastrad 	bool		(*signaled)(struct dma_fence *);
     76   1.2  riastrad 	long		(*wait)(struct dma_fence *, bool, long);
     77   1.2  riastrad 	void		(*release)(struct dma_fence *);
     78   1.1  riastrad };
     79   1.1  riastrad 
     80   1.2  riastrad typedef void (*dma_fence_func_t)(struct dma_fence *, struct dma_fence_cb *);
     81   1.1  riastrad 
     82   1.2  riastrad struct dma_fence_cb {
     83   1.4  riastrad 	dma_fence_func_t		func; /* Linux API name */
     84   1.2  riastrad 	TAILQ_ENTRY(dma_fence_cb)	fcb_entry;
     85   1.2  riastrad 	bool				fcb_onqueue;
     86   1.1  riastrad };
     87   1.1  riastrad 
     88  1.16  riastrad #define	__dma_fence_is_later		linux___dma_fence_is_later
     89  1.12  riastrad #define	__dma_fence_signal		linux___dma_fence_signal
     90  1.12  riastrad #define	__dma_fence_signal_wake		linux___dma_fence_signal_wake
     91   1.2  riastrad #define	dma_fence_add_callback		linux_dma_fence_add_callback
     92   1.2  riastrad #define	dma_fence_context_alloc		linux_dma_fence_context_alloc
     93   1.2  riastrad #define	dma_fence_default_wait		linux_dma_fence_default_wait
     94   1.2  riastrad #define	dma_fence_destroy		linux_dma_fence_destroy
     95   1.2  riastrad #define	dma_fence_enable_sw_signaling	linux_dma_fence_enable_sw_signaling
     96   1.2  riastrad #define	dma_fence_free			linux_dma_fence_free
     97   1.2  riastrad #define	dma_fence_get			linux_dma_fence_get
     98   1.2  riastrad #define	dma_fence_get_rcu		linux_dma_fence_get_rcu
     99   1.6  riastrad #define	dma_fence_get_rcu_safe		linux_dma_fence_get_rcu_safe
    100  1.10  riastrad #define	dma_fence_get_status		linux_dma_fence_get_status
    101   1.8  riastrad #define	dma_fence_get_stub		linux_dma_fence_get_stub
    102   1.2  riastrad #define	dma_fence_init			linux_dma_fence_init
    103   1.2  riastrad #define	dma_fence_is_later		linux_dma_fence_is_later
    104   1.2  riastrad #define	dma_fence_is_signaled		linux_dma_fence_is_signaled
    105   1.2  riastrad #define	dma_fence_is_signaled_locked	linux_dma_fence_is_signaled_locked
    106   1.2  riastrad #define	dma_fence_put			linux_dma_fence_put
    107   1.2  riastrad #define	dma_fence_remove_callback	linux_dma_fence_remove_callback
    108  1.13  riastrad #define	dma_fence_reset			linux_dma_fence_reset
    109   1.5  riastrad #define	dma_fence_set_error		linux_dma_fence_set_error
    110   1.2  riastrad #define	dma_fence_signal		linux_dma_fence_signal
    111   1.2  riastrad #define	dma_fence_signal_locked		linux_dma_fence_signal_locked
    112   1.2  riastrad #define	dma_fence_wait			linux_dma_fence_wait
    113   1.2  riastrad #define	dma_fence_wait_any_timeout	linux_dma_fence_wait_any_timeout
    114   1.2  riastrad #define	dma_fence_wait_timeout		linux_dma_fence_wait_timeout
    115   1.2  riastrad 
    116   1.2  riastrad extern int	linux_dma_fence_trace;
    117   1.2  riastrad 
    118   1.2  riastrad void	dma_fence_init(struct dma_fence *, const struct dma_fence_ops *,
    119  1.16  riastrad 	    spinlock_t *, uint64_t, uint64_t);
    120  1.13  riastrad void	dma_fence_reset(struct dma_fence *, const struct dma_fence_ops *,
    121  1.16  riastrad 	    spinlock_t *, uint64_t, uint64_t); /* XXX extension */
    122   1.2  riastrad void	dma_fence_destroy(struct dma_fence *);
    123   1.2  riastrad void	dma_fence_free(struct dma_fence *);
    124   1.1  riastrad 
    125  1.16  riastrad uint64_t
    126   1.2  riastrad 	dma_fence_context_alloc(unsigned);
    127  1.16  riastrad bool	__dma_fence_is_later(uint64_t, uint64_t, const struct dma_fence_ops *);
    128   1.2  riastrad bool	dma_fence_is_later(struct dma_fence *, struct dma_fence *);
    129   1.1  riastrad 
    130   1.2  riastrad struct dma_fence *
    131   1.8  riastrad 	dma_fence_get_stub(void);
    132   1.8  riastrad 
    133   1.8  riastrad struct dma_fence *
    134   1.2  riastrad 	dma_fence_get(struct dma_fence *);
    135   1.2  riastrad struct dma_fence *
    136   1.2  riastrad 	dma_fence_get_rcu(struct dma_fence *);
    137   1.3  riastrad struct dma_fence *
    138   1.7  riastrad 	dma_fence_get_rcu_safe(struct dma_fence *volatile const *);
    139   1.2  riastrad void	dma_fence_put(struct dma_fence *);
    140   1.2  riastrad 
    141   1.2  riastrad int	dma_fence_add_callback(struct dma_fence *, struct dma_fence_cb *,
    142   1.2  riastrad 	    dma_fence_func_t);
    143   1.2  riastrad bool	dma_fence_remove_callback(struct dma_fence *, struct dma_fence_cb *);
    144   1.2  riastrad void	dma_fence_enable_sw_signaling(struct dma_fence *);
    145   1.2  riastrad 
    146   1.2  riastrad bool	dma_fence_is_signaled(struct dma_fence *);
    147   1.2  riastrad bool	dma_fence_is_signaled_locked(struct dma_fence *);
    148   1.5  riastrad void	dma_fence_set_error(struct dma_fence *, int);
    149  1.10  riastrad int	dma_fence_get_status(struct dma_fence *);
    150   1.2  riastrad int	dma_fence_signal(struct dma_fence *);
    151   1.2  riastrad int	dma_fence_signal_locked(struct dma_fence *);
    152   1.2  riastrad long	dma_fence_default_wait(struct dma_fence *, bool, long);
    153   1.2  riastrad long	dma_fence_wait(struct dma_fence *, bool);
    154  1.11  riastrad long	dma_fence_wait_any_timeout(struct dma_fence **, uint32_t, bool, long,
    155  1.11  riastrad 	    uint32_t *);
    156   1.2  riastrad long	dma_fence_wait_timeout(struct dma_fence *, bool, long);
    157   1.1  riastrad 
    158  1.12  riastrad /* i915 hacks */
    159  1.12  riastrad bool	__dma_fence_signal(struct dma_fence *);
    160  1.12  riastrad void	__dma_fence_signal_wake(struct dma_fence *, ktime_t);
    161  1.12  riastrad 
    162   1.1  riastrad static inline void __printflike(2, 3)
    163   1.2  riastrad DMA_FENCE_TRACE(struct dma_fence *f, const char *fmt, ...)
    164   1.1  riastrad {
    165   1.1  riastrad 	va_list va;
    166   1.1  riastrad 
    167   1.2  riastrad 	if (__predict_false(linux_dma_fence_trace)) {
    168   1.1  riastrad 		va_start(va, fmt);
    169  1.16  riastrad 		printf("fence %"PRIu64"@%"PRIu64": ", f->context, f->seqno);
    170   1.1  riastrad 		vprintf(fmt, va);
    171   1.1  riastrad 		va_end(va);
    172   1.1  riastrad 	}
    173   1.1  riastrad }
    174   1.1  riastrad 
    175   1.2  riastrad #endif	/* _LINUX_DMA_FENCE_H_ */
    176