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