fence.h revision 1.12 1 1.1 riastrad /* $NetBSD: fence.h,v 1.12 2018/08/27 13:36:53 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.1 riastrad #ifndef _LINUX_FENCE_H_
33 1.1 riastrad #define _LINUX_FENCE_H_
34 1.1 riastrad
35 1.7 riastrad #include <sys/types.h>
36 1.11 riastrad #include <sys/condvar.h>
37 1.11 riastrad #include <sys/kernel.h>
38 1.11 riastrad #include <sys/queue.h>
39 1.7 riastrad
40 1.11 riastrad #include <linux/kref.h>
41 1.4 riastrad #include <linux/rcupdate.h>
42 1.11 riastrad #include <linux/spinlock.h>
43 1.4 riastrad
44 1.7 riastrad struct fence_cb;
45 1.7 riastrad
46 1.7 riastrad struct fence {
47 1.7 riastrad struct kref refcount;
48 1.7 riastrad spinlock_t *lock;
49 1.12 riastrad volatile unsigned long flags;
50 1.7 riastrad unsigned context;
51 1.7 riastrad unsigned seqno;
52 1.7 riastrad const struct fence_ops *ops;
53 1.11 riastrad
54 1.11 riastrad TAILQ_HEAD(, fence_cb) f_callbacks;
55 1.11 riastrad kcondvar_t f_cv;
56 1.11 riastrad struct rcu_head f_rcu;
57 1.7 riastrad };
58 1.7 riastrad
59 1.11 riastrad #define FENCE_FLAG_ENABLE_SIGNAL_BIT 0
60 1.11 riastrad #define FENCE_FLAG_SIGNALED_BIT 1
61 1.11 riastrad #define FENCE_FLAG_USER_BITS 2
62 1.7 riastrad
63 1.2 riastrad struct fence_ops {
64 1.7 riastrad const char *(*get_driver_name)(struct fence *);
65 1.7 riastrad const char *(*get_timeline_name)(struct fence *);
66 1.7 riastrad bool (*enable_signaling)(struct fence *);
67 1.7 riastrad bool (*signaled)(struct fence *);
68 1.7 riastrad long (*wait)(struct fence *, bool, long);
69 1.7 riastrad void (*release)(struct fence *);
70 1.2 riastrad };
71 1.2 riastrad
72 1.7 riastrad typedef void (*fence_func_t)(struct fence *, struct fence_cb *);
73 1.7 riastrad
74 1.7 riastrad struct fence_cb {
75 1.11 riastrad fence_func_t fcb_func;
76 1.11 riastrad TAILQ_ENTRY(fence_cb) fcb_entry;
77 1.11 riastrad bool fcb_onqueue;
78 1.2 riastrad };
79 1.2 riastrad
80 1.7 riastrad #define fence_add_callback linux_fence_add_callback
81 1.7 riastrad #define fence_context_alloc linux_fence_context_alloc
82 1.11 riastrad #define fence_destroy linux_fence_destroy
83 1.10 riastrad #define fence_enable_sw_signaling linux_fence_enable_sw_signaling
84 1.6 riastrad #define fence_get linux_fence_get
85 1.7 riastrad #define fence_init linux_fence_init
86 1.11 riastrad #define fence_is_signaled linux_fence_is_signaled
87 1.11 riastrad #define fence_is_signaled_locked linux_fence_is_signaled_locked
88 1.3 riastrad #define fence_put linux_fence_put
89 1.7 riastrad #define fence_remove_callback linux_fence_remove_callback
90 1.9 riastrad #define fence_signal linux_fence_signal
91 1.7 riastrad #define fence_signal_locked linux_fence_signal_locked
92 1.3 riastrad #define fence_wait linux_fence_wait
93 1.7 riastrad #define fence_wait_timeout linux_fence_wait_timeout
94 1.7 riastrad
95 1.7 riastrad void fence_init(struct fence *, const struct fence_ops *, spinlock_t *,
96 1.7 riastrad unsigned, unsigned);
97 1.11 riastrad void fence_destroy(struct fence *);
98 1.7 riastrad void fence_free(struct fence *);
99 1.7 riastrad
100 1.7 riastrad unsigned
101 1.7 riastrad fence_context_alloc(unsigned);
102 1.3 riastrad
103 1.5 riastrad struct fence *
104 1.5 riastrad fence_get(struct fence *);
105 1.11 riastrad struct fence *
106 1.11 riastrad fence_get_rcu(struct fence *);
107 1.3 riastrad void fence_put(struct fence *);
108 1.2 riastrad
109 1.7 riastrad int fence_add_callback(struct fence *, struct fence_cb *, fence_func_t);
110 1.7 riastrad bool fence_remove_callback(struct fence *, struct fence_cb *);
111 1.10 riastrad void fence_enable_sw_signaling(struct fence *);
112 1.7 riastrad
113 1.7 riastrad bool fence_is_signaled(struct fence *);
114 1.7 riastrad bool fence_is_signaled_locked(struct fence *);
115 1.9 riastrad int fence_signal(struct fence *);
116 1.7 riastrad int fence_signal_locked(struct fence *);
117 1.7 riastrad long fence_default_wait(struct fence *, bool, long);
118 1.7 riastrad long fence_wait(struct fence *, bool);
119 1.11 riastrad long fence_wait_timeout(struct fence *, bool, long);
120 1.7 riastrad
121 1.8 riastrad static inline void
122 1.8 riastrad FENCE_TRACE(struct fence *f, const char *fmt, ...)
123 1.8 riastrad {
124 1.8 riastrad va_list va;
125 1.8 riastrad
126 1.8 riastrad va_start(va, fmt);
127 1.8 riastrad printf("fence %u@%u: ", f->context, f->seqno);
128 1.8 riastrad vprintf(fmt, va);
129 1.8 riastrad va_end(va);
130 1.8 riastrad }
131 1.8 riastrad
132 1.1 riastrad #endif /* _LINUX_FENCE_H_ */
133