fence.h revision 1.15.10.1 1 /* $NetBSD: fence.h,v 1.15.10.1 2020/02/29 20:20:17 ad 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_FENCE_H_
33 #define _LINUX_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 fence_cb;
45
46 struct fence {
47 struct kref refcount;
48 spinlock_t *lock;
49 volatile unsigned long flags;
50 unsigned context;
51 unsigned seqno;
52 const struct fence_ops *ops;
53
54 TAILQ_HEAD(, fence_cb) f_callbacks;
55 kcondvar_t f_cv;
56 struct rcu_head f_rcu;
57 };
58
59 #define FENCE_FLAG_ENABLE_SIGNAL_BIT 0
60 #define FENCE_FLAG_SIGNALED_BIT 1
61 #define FENCE_FLAG_USER_BITS 2
62
63 struct fence_ops {
64 const char *(*get_driver_name)(struct fence *);
65 const char *(*get_timeline_name)(struct fence *);
66 bool (*enable_signaling)(struct fence *);
67 bool (*signaled)(struct fence *);
68 long (*wait)(struct fence *, bool, long);
69 void (*release)(struct fence *);
70 };
71
72 typedef void (*fence_func_t)(struct fence *, struct fence_cb *);
73
74 struct fence_cb {
75 fence_func_t fcb_func;
76 TAILQ_ENTRY(fence_cb) fcb_entry;
77 bool fcb_onqueue;
78 };
79
80 #define fence_add_callback linux_fence_add_callback
81 #define fence_context_alloc linux_fence_context_alloc
82 #define fence_default_wait linux_fence_default_wait
83 #define fence_destroy linux_fence_destroy
84 #define fence_enable_sw_signaling linux_fence_enable_sw_signaling
85 #define fence_free linux_fence_free
86 #define fence_get linux_fence_get
87 #define fence_get_rcu linux_fence_get_rcu
88 #define fence_init linux_fence_init
89 #define fence_is_later linux_fence_is_later
90 #define fence_is_signaled linux_fence_is_signaled
91 #define fence_is_signaled_locked linux_fence_is_signaled_locked
92 #define fence_put linux_fence_put
93 #define fence_remove_callback linux_fence_remove_callback
94 #define fence_signal linux_fence_signal
95 #define fence_signal_locked linux_fence_signal_locked
96 #define fence_wait linux_fence_wait
97 #define fence_wait_any_timeout linux_fence_wait_any_timeout
98 #define fence_wait_timeout linux_fence_wait_timeout
99
100 extern int linux_fence_trace;
101
102 void fence_init(struct fence *, const struct fence_ops *, spinlock_t *,
103 unsigned, unsigned);
104 void fence_destroy(struct fence *);
105 void fence_free(struct fence *);
106
107 unsigned
108 fence_context_alloc(unsigned);
109 bool fence_is_later(struct fence *, struct fence *);
110
111 struct fence *
112 fence_get(struct fence *);
113 struct fence *
114 fence_get_rcu(struct fence *);
115 void fence_put(struct fence *);
116
117 int fence_add_callback(struct fence *, struct fence_cb *, fence_func_t);
118 bool fence_remove_callback(struct fence *, struct fence_cb *);
119 void fence_enable_sw_signaling(struct fence *);
120
121 bool fence_is_signaled(struct fence *);
122 bool fence_is_signaled_locked(struct fence *);
123 int fence_signal(struct fence *);
124 int fence_signal_locked(struct fence *);
125 long fence_default_wait(struct fence *, bool, long);
126 long fence_wait(struct fence *, bool);
127 long fence_wait_any_timeout(struct fence **, uint32_t, bool, long);
128 long fence_wait_timeout(struct fence *, bool, long);
129
130 static inline void __printflike(2, 3)
131 FENCE_TRACE(struct fence *f, const char *fmt, ...)
132 {
133 va_list va;
134
135 if (__predict_false(linux_fence_trace)) {
136 va_start(va, fmt);
137 printf("fence %u@%u: ", f->context, f->seqno);
138 vprintf(fmt, va);
139 va_end(va);
140 }
141 }
142
143 #endif /* _LINUX_FENCE_H_ */
144