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