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