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