1 /**************************************************************************
2  *
3  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Tx., USA
4  * All Rights Reserved.
5  * Copyright 2009 VMware, Inc., Palo Alto, CA., USA
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 /*
30  * Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
31  */
32 
33 #ifndef WSBM_FENCEMGR_H
34 #define WSBM_FENCEMGR_H
35 
36 #include <stdint.h>
37 #include <stdlib.h>
38 
39 struct _WsbmFenceObject;
40 struct _WsbmFenceMgr;
41 
42 /*
43  * Do a quick check to see if the fence manager has registered the fence
44  * object as signaled. Note that this function may return a false negative
45  * answer.
46  */
47 extern uint32_t wsbmFenceSignaledTypeCached(struct _WsbmFenceObject *fence);
48 
49 /*
50  * Check if the fence object is signaled. This function can be substantially
51  * more expensive to call than the above function, but will not return a false
52  * negative answer. The argument "flush_type" sets the types that the
53  * underlying mechanism must make sure will eventually signal.
54  */
55 extern int wsbmFenceSignaledType(struct _WsbmFenceObject *fence,
56 				 uint32_t flush_type, uint32_t * signaled);
57 
58 /*
59  * Convenience functions.
60  */
61 
62 static inline int
wsbmFenceSignaled(struct _WsbmFenceObject * fence,uint32_t flush_type)63 wsbmFenceSignaled(struct _WsbmFenceObject *fence, uint32_t flush_type)
64 {
65     uint32_t signaled_types;
66     int ret = wsbmFenceSignaledType(fence, flush_type, &signaled_types);
67 
68     if (ret)
69 	return 0;
70     return ((signaled_types & flush_type) == flush_type);
71 }
72 
73 static inline int
wsbmFenceSignaledCached(struct _WsbmFenceObject * fence,uint32_t flush_type)74 wsbmFenceSignaledCached(struct _WsbmFenceObject *fence, uint32_t flush_type)
75 {
76     uint32_t signaled_types = wsbmFenceSignaledTypeCached(fence);
77 
78     return ((signaled_types & flush_type) == flush_type);
79 }
80 
81 /*
82  * Reference a fence object.
83  */
84 extern struct _WsbmFenceObject *wsbmFenceReference(struct _WsbmFenceObject
85 						   *fence);
86 
87 /*
88  * Unreference a fence object. The fence object pointer will be reset to NULL.
89  */
90 
91 extern void wsbmFenceUnreference(struct _WsbmFenceObject **pFence);
92 
93 /*
94  * Wait for a fence to signal the indicated fence_type.
95  * If "lazy_hint" is true, it indicates that the wait may sleep to avoid
96  * busy-wait polling.
97  */
98 extern int wsbmFenceFinish(struct _WsbmFenceObject *fence,
99 			   uint32_t fence_type, int lazy_hint);
100 
101 /*
102  * Create a WsbmFenceObject for manager "mgr".
103  *
104  * "private" is a pointer that should be used for the callbacks in
105  * struct _WsbmFenceMgrCreateInfo.
106  *
107  * if private_size is nonzero, then the info stored at *private, with size
108  * private size will be copied and the fence manager will instead use a
109  * pointer to the copied data for the callbacks in
110  * struct _WsbmFenceMgrCreateInfo. In that case, the object pointed to by
111  * "private" may be destroyed after the call to wsbmFenceCreate.
112  */
113 extern struct _WsbmFenceObject *wsbmFenceCreate(struct _WsbmFenceMgr *mgr,
114 						uint32_t fence_class,
115 						uint32_t fence_type,
116 						void *private,
117 						size_t private_size);
118 
119 
120 extern struct _WsbmFenceObject *wsbmFenceCreateSig(struct _WsbmFenceMgr *mgr,
121 						   uint32_t fence_class,
122 						   uint32_t fence_type,
123 						   uint32_t signaled_types,
124 						   void *private,
125 						   size_t private_size);
126 
127 extern uint32_t wsbmFenceType(struct _WsbmFenceObject *fence);
128 
129 /*
130  * Fence creations are ordered. If a fence signals a fence_type,
131  * it is safe to assume that all fences of the same class that was
132  * created before that fence has signaled the same type.
133  */
134 
135 #define WSBM_FENCE_CLASS_ORDERED (1 << 0)
136 
137 struct _WsbmFenceMgrCreateInfo
138 {
139     uint32_t flags;
140     uint32_t num_classes;
141     int (*signaled) (struct _WsbmFenceMgr * mgr, void *private,
142 		     uint32_t flush_type, uint32_t * signaled_type);
143     int (*finish) (struct _WsbmFenceMgr * mgr, void *private,
144 		   uint32_t fence_type, int lazy_hint);
145     int (*unreference) (struct _WsbmFenceMgr * mgr, void **private);
146 };
147 
148 extern struct _WsbmFenceMgr *wsbmFenceMgrCreate(const struct
149 						_WsbmFenceMgrCreateInfo
150 						*info);
151 extern void wsbmFenceCmdLock(struct _WsbmFenceMgr *mgr, uint32_t fence_class);
152 extern void wsbmFenceCmdUnlock(struct _WsbmFenceMgr *mgr,
153 			       uint32_t fence_class);
154 /*
155  * Builtin drivers.
156  */
157 
158 extern struct _WsbmFenceMgr *wsbmFenceMgrTTMInit(int fd,
159 						 unsigned int numClass,
160 						 unsigned int devOffset);
161 extern void wsbmFenceMgrTTMTakedown(struct _WsbmFenceMgr *mgr);
162 #endif
163