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