Commit | Line | Data |
---|---|---|
1e494cf4 JB |
1 | /* |
2 | * include/linux/sync.h | |
3 | * | |
4 | * Copyright (C) 2012 Google, Inc. | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | */ | |
12 | ||
13 | #ifndef _LINUX_SYNC_H | |
14 | #define _LINUX_SYNC_H | |
15 | ||
16 | #include <linux/types.h> | |
17 | #ifdef __KERNEL__ | |
18 | ||
19 | #include <linux/kref.h> | |
20 | #include <linux/ktime.h> | |
21 | #include <linux/list.h> | |
22 | #include <linux/spinlock.h> | |
23 | #include <linux/wait.h> | |
24 | ||
25 | struct sync_timeline; | |
26 | struct sync_pt; | |
27 | struct sync_fence; | |
28 | struct seq_file; | |
29 | ||
30 | /** | |
31 | * struct sync_timeline_ops - sync object implementation ops | |
32 | * @driver_name: name of the implentation | |
33 | * @dup: duplicate a sync_pt | |
34 | * @has_signaled: returns: | |
35 | * 1 if pt has signaled | |
36 | * 0 if pt has not signaled | |
37 | * <0 on error | |
38 | * @compare: returns: | |
39 | * 1 if b will signal before a | |
40 | * 0 if a and b will signal at the same time | |
41 | * -1 if a will signabl before b | |
42 | * @free_pt: called before sync_pt is freed | |
43 | * @release_obj: called before sync_timeline is freed | |
44 | * @print_obj: deprecated | |
45 | * @print_pt: deprecated | |
46 | * @fill_driver_data: write implmentation specific driver data to data. | |
47 | * should return an error if there is not enough room | |
48 | * as specified by size. This information is returned | |
49 | * to userspace by SYNC_IOC_FENCE_INFO. | |
50 | * @timeline_value_str: fill str with the value of the sync_timeline's counter | |
51 | * @pt_value_str: fill str with the value of the sync_pt | |
52 | */ | |
53 | struct sync_timeline_ops { | |
54 | const char *driver_name; | |
55 | ||
56 | /* required */ | |
57 | struct sync_pt *(*dup)(struct sync_pt *pt); | |
58 | ||
59 | /* required */ | |
60 | int (*has_signaled)(struct sync_pt *pt); | |
61 | ||
62 | /* required */ | |
63 | int (*compare)(struct sync_pt *a, struct sync_pt *b); | |
64 | ||
65 | /* optional */ | |
66 | void (*free_pt)(struct sync_pt *sync_pt); | |
67 | ||
68 | /* optional */ | |
69 | void (*release_obj)(struct sync_timeline *sync_timeline); | |
70 | ||
71 | /* deprecated */ | |
72 | void (*print_obj)(struct seq_file *s, | |
73 | struct sync_timeline *sync_timeline); | |
74 | ||
75 | /* deprecated */ | |
76 | void (*print_pt)(struct seq_file *s, struct sync_pt *sync_pt); | |
77 | ||
78 | /* optional */ | |
79 | int (*fill_driver_data)(struct sync_pt *syncpt, void *data, int size); | |
80 | ||
81 | /* optional */ | |
82 | void (*timeline_value_str)(struct sync_timeline *timeline, char *str, | |
83 | int size); | |
84 | ||
85 | /* optional */ | |
86 | void (*pt_value_str)(struct sync_pt *pt, char *str, int size); | |
87 | }; | |
88 | ||
89 | /** | |
90 | * struct sync_timeline - sync object | |
91 | * @kref: reference count on fence. | |
92 | * @ops: ops that define the implementaiton of the sync_timeline | |
93 | * @name: name of the sync_timeline. Useful for debugging | |
94 | * @destoryed: set when sync_timeline is destroyed | |
95 | * @child_list_head: list of children sync_pts for this sync_timeline | |
96 | * @child_list_lock: lock protecting @child_list_head, destroyed, and | |
97 | * sync_pt.status | |
98 | * @active_list_head: list of active (unsignaled/errored) sync_pts | |
99 | * @sync_timeline_list: membership in global sync_timeline_list | |
100 | */ | |
101 | struct sync_timeline { | |
102 | struct kref kref; | |
103 | const struct sync_timeline_ops *ops; | |
104 | char name[32]; | |
105 | ||
106 | /* protected by child_list_lock */ | |
107 | bool destroyed; | |
108 | ||
109 | struct list_head child_list_head; | |
110 | spinlock_t child_list_lock; | |
111 | ||
112 | struct list_head active_list_head; | |
113 | spinlock_t active_list_lock; | |
114 | ||
115 | struct list_head sync_timeline_list; | |
116 | }; | |
117 | ||
118 | /** | |
119 | * struct sync_pt - sync point | |
120 | * @parent: sync_timeline to which this sync_pt belongs | |
121 | * @child_list: membership in sync_timeline.child_list_head | |
122 | * @active_list: membership in sync_timeline.active_list_head | |
123 | * @signaled_list: membership in temorary signaled_list on stack | |
124 | * @fence: sync_fence to which the sync_pt belongs | |
125 | * @pt_list: membership in sync_fence.pt_list_head | |
126 | * @status: 1: signaled, 0:active, <0: error | |
127 | * @timestamp: time which sync_pt status transitioned from active to | |
128 | * singaled or error. | |
129 | */ | |
130 | struct sync_pt { | |
131 | struct sync_timeline *parent; | |
132 | struct list_head child_list; | |
133 | ||
134 | struct list_head active_list; | |
135 | struct list_head signaled_list; | |
136 | ||
137 | struct sync_fence *fence; | |
138 | struct list_head pt_list; | |
139 | ||
140 | /* protected by parent->active_list_lock */ | |
141 | int status; | |
142 | ||
143 | ktime_t timestamp; | |
144 | }; | |
145 | ||
146 | /** | |
147 | * struct sync_fence - sync fence | |
148 | * @file: file representing this fence | |
149 | * @kref: referenace count on fence. | |
150 | * @name: name of sync_fence. Useful for debugging | |
151 | * @pt_list_head: list of sync_pts in ths fence. immutable once fence | |
152 | * is created | |
153 | * @waiter_list_head: list of asynchronous waiters on this fence | |
154 | * @waiter_list_lock: lock protecting @waiter_list_head and @status | |
155 | * @status: 1: signaled, 0:active, <0: error | |
156 | * | |
157 | * @wq: wait queue for fence signaling | |
158 | * @sync_fence_list: membership in global fence list | |
159 | */ | |
160 | struct sync_fence { | |
161 | struct file *file; | |
162 | struct kref kref; | |
163 | char name[32]; | |
164 | ||
165 | /* this list is immutable once the fence is created */ | |
166 | struct list_head pt_list_head; | |
167 | ||
168 | struct list_head waiter_list_head; | |
169 | spinlock_t waiter_list_lock; /* also protects status */ | |
170 | int status; | |
171 | ||
172 | wait_queue_head_t wq; | |
173 | ||
174 | struct list_head sync_fence_list; | |
175 | }; | |
176 | ||
177 | struct sync_fence_waiter; | |
178 | typedef void (*sync_callback_t)(struct sync_fence *fence, | |
179 | struct sync_fence_waiter *waiter); | |
180 | ||
181 | /** | |
182 | * struct sync_fence_waiter - metadata for asynchronous waiter on a fence | |
183 | * @waiter_list: membership in sync_fence.waiter_list_head | |
184 | * @callback: function pointer to call when fence signals | |
185 | * @callback_data: pointer to pass to @callback | |
186 | */ | |
187 | struct sync_fence_waiter { | |
188 | struct list_head waiter_list; | |
189 | ||
190 | sync_callback_t callback; | |
191 | }; | |
192 | ||
193 | static inline void sync_fence_waiter_init(struct sync_fence_waiter *waiter, | |
194 | sync_callback_t callback) | |
195 | { | |
196 | waiter->callback = callback; | |
197 | } | |
198 | ||
199 | /* | |
200 | * API for sync_timeline implementers | |
201 | */ | |
202 | ||
203 | /** | |
204 | * sync_timeline_create() - creates a sync object | |
205 | * @ops: specifies the implemention ops for the object | |
206 | * @size: size to allocate for this obj | |
207 | * @name: sync_timeline name | |
208 | * | |
209 | * Creates a new sync_timeline which will use the implemetation specified by | |
210 | * @ops. @size bytes will be allocated allowing for implemntation specific | |
211 | * data to be kept after the generic sync_timeline stuct. | |
212 | */ | |
213 | struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops, | |
214 | int size, const char *name); | |
215 | ||
216 | /** | |
217 | * sync_timeline_destory() - destorys a sync object | |
218 | * @obj: sync_timeline to destroy | |
219 | * | |
220 | * A sync implemntation should call this when the @obj is going away | |
221 | * (i.e. module unload.) @obj won't actually be freed until all its childern | |
222 | * sync_pts are freed. | |
223 | */ | |
224 | void sync_timeline_destroy(struct sync_timeline *obj); | |
225 | ||
226 | /** | |
227 | * sync_timeline_signal() - signal a status change on a sync_timeline | |
228 | * @obj: sync_timeline to signal | |
229 | * | |
230 | * A sync implemntation should call this any time one of it's sync_pts | |
231 | * has signaled or has an error condition. | |
232 | */ | |
233 | void sync_timeline_signal(struct sync_timeline *obj); | |
234 | ||
235 | /** | |
236 | * sync_pt_create() - creates a sync pt | |
237 | * @parent: sync_pt's parent sync_timeline | |
238 | * @size: size to allocate for this pt | |
239 | * | |
240 | * Creates a new sync_pt as a chiled of @parent. @size bytes will be | |
241 | * allocated allowing for implemntation specific data to be kept after | |
242 | * the generic sync_timeline struct. | |
243 | */ | |
244 | struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size); | |
245 | ||
246 | /** | |
247 | * sync_pt_free() - frees a sync pt | |
248 | * @pt: sync_pt to free | |
249 | * | |
250 | * This should only be called on sync_pts which have been created but | |
251 | * not added to a fence. | |
252 | */ | |
253 | void sync_pt_free(struct sync_pt *pt); | |
254 | ||
255 | /** | |
256 | * sync_fence_create() - creates a sync fence | |
257 | * @name: name of fence to create | |
258 | * @pt: sync_pt to add to the fence | |
259 | * | |
260 | * Creates a fence containg @pt. Once this is called, the fence takes | |
261 | * ownership of @pt. | |
262 | */ | |
263 | struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt); | |
264 | ||
265 | /* | |
266 | * API for sync_fence consumers | |
267 | */ | |
268 | ||
269 | /** | |
270 | * sync_fence_merge() - merge two fences | |
271 | * @name: name of new fence | |
272 | * @a: fence a | |
273 | * @b: fence b | |
274 | * | |
275 | * Creates a new fence which contains copies of all the sync_pts in both | |
276 | * @a and @b. @a and @b remain valid, independent fences. | |
277 | */ | |
278 | struct sync_fence *sync_fence_merge(const char *name, | |
279 | struct sync_fence *a, struct sync_fence *b); | |
280 | ||
281 | /** | |
282 | * sync_fence_fdget() - get a fence from an fd | |
283 | * @fd: fd referencing a fence | |
284 | * | |
285 | * Ensures @fd references a valid fence, increments the refcount of the backing | |
286 | * file, and returns the fence. | |
287 | */ | |
288 | struct sync_fence *sync_fence_fdget(int fd); | |
289 | ||
290 | /** | |
291 | * sync_fence_put() - puts a refernnce of a sync fence | |
292 | * @fence: fence to put | |
293 | * | |
294 | * Puts a reference on @fence. If this is the last reference, the fence and | |
295 | * all it's sync_pts will be freed | |
296 | */ | |
297 | void sync_fence_put(struct sync_fence *fence); | |
298 | ||
299 | /** | |
300 | * sync_fence_install() - installs a fence into a file descriptor | |
301 | * @fence: fence to instal | |
302 | * @fd: file descriptor in which to install the fence | |
303 | * | |
304 | * Installs @fence into @fd. @fd's should be acquired through get_unused_fd(). | |
305 | */ | |
306 | void sync_fence_install(struct sync_fence *fence, int fd); | |
307 | ||
308 | /** | |
309 | * sync_fence_wait_async() - registers and async wait on the fence | |
310 | * @fence: fence to wait on | |
311 | * @waiter: waiter callback struck | |
312 | * | |
313 | * Returns 1 if @fence has already signaled. | |
314 | * | |
315 | * Registers a callback to be called when @fence signals or has an error. | |
316 | * @waiter should be initialized with sync_fence_waiter_init(). | |
317 | */ | |
318 | int sync_fence_wait_async(struct sync_fence *fence, | |
319 | struct sync_fence_waiter *waiter); | |
320 | ||
321 | /** | |
322 | * sync_fence_cancel_async() - cancels an async wait | |
323 | * @fence: fence to wait on | |
324 | * @waiter: waiter callback struck | |
325 | * | |
326 | * returns 0 if waiter was removed from fence's async waiter list. | |
327 | * returns -ENOENT if waiter was not found on fence's async waiter list. | |
328 | * | |
329 | * Cancels a previously registered async wait. Will fail gracefully if | |
330 | * @waiter was never registered or if @fence has already signaled @waiter. | |
331 | */ | |
332 | int sync_fence_cancel_async(struct sync_fence *fence, | |
333 | struct sync_fence_waiter *waiter); | |
334 | ||
335 | /** | |
336 | * sync_fence_wait() - wait on fence | |
337 | * @fence: fence to wait on | |
338 | * @tiemout: timeout in ms | |
339 | * | |
340 | * Wait for @fence to be signaled or have an error. Waits indefinitely | |
341 | * if @timeout < 0 | |
342 | */ | |
343 | int sync_fence_wait(struct sync_fence *fence, long timeout); | |
344 | ||
345 | #endif /* __KERNEL__ */ | |
346 | ||
347 | /** | |
348 | * struct sync_merge_data - data passed to merge ioctl | |
349 | * @fd2: file descriptor of second fence | |
350 | * @name: name of new fence | |
351 | * @fence: returns the fd of the new fence to userspace | |
352 | */ | |
353 | struct sync_merge_data { | |
354 | __s32 fd2; /* fd of second fence */ | |
355 | char name[32]; /* name of new fence */ | |
356 | __s32 fence; /* fd on newly created fence */ | |
357 | }; | |
358 | ||
359 | /** | |
360 | * struct sync_pt_info - detailed sync_pt information | |
361 | * @len: length of sync_pt_info including any driver_data | |
362 | * @obj_name: name of parent sync_timeline | |
363 | * @driver_name: name of driver implmenting the parent | |
364 | * @status: status of the sync_pt 0:active 1:signaled <0:error | |
365 | * @timestamp_ns: timestamp of status change in nanoseconds | |
366 | * @driver_data: any driver dependant data | |
367 | */ | |
368 | struct sync_pt_info { | |
369 | __u32 len; | |
370 | char obj_name[32]; | |
371 | char driver_name[32]; | |
372 | __s32 status; | |
373 | __u64 timestamp_ns; | |
374 | ||
375 | __u8 driver_data[0]; | |
376 | }; | |
377 | ||
378 | /** | |
379 | * struct sync_fence_info_data - data returned from fence info ioctl | |
380 | * @len: ioctl caller writes the size of the buffer its passing in. | |
381 | * ioctl returns length of sync_fence_data reutnred to userspace | |
382 | * including pt_info. | |
383 | * @name: name of fence | |
384 | * @status: status of fence. 1: signaled 0:active <0:error | |
385 | * @pt_info: a sync_pt_info struct for every sync_pt in the fence | |
386 | */ | |
387 | struct sync_fence_info_data { | |
388 | __u32 len; | |
389 | char name[32]; | |
390 | __s32 status; | |
391 | ||
392 | __u8 pt_info[0]; | |
393 | }; | |
394 | ||
395 | #define SYNC_IOC_MAGIC '>' | |
396 | ||
397 | /** | |
398 | * DOC: SYNC_IOC_WAIT - wait for a fence to signal | |
399 | * | |
400 | * pass timeout in milliseconds. Waits indefinitely timeout < 0. | |
401 | */ | |
402 | #define SYNC_IOC_WAIT _IOW(SYNC_IOC_MAGIC, 0, __s32) | |
403 | ||
404 | /** | |
405 | * DOC: SYNC_IOC_MERGE - merge two fences | |
406 | * | |
407 | * Takes a struct sync_merge_data. Creates a new fence containing copies of | |
408 | * the sync_pts in both the calling fd and sync_merge_data.fd2. Returns the | |
409 | * new fence's fd in sync_merge_data.fence | |
410 | */ | |
411 | #define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 1, struct sync_merge_data) | |
412 | ||
413 | /** | |
414 | * DOC: SYNC_IOC_FENCE_INFO - get detailed information on a fence | |
415 | * | |
416 | * Takes a struct sync_fence_info_data with extra space allocated for pt_info. | |
417 | * Caller should write the size of the buffer into len. On return, len is | |
418 | * updated to reflect the total size of the sync_fence_info_data including | |
419 | * pt_info. | |
420 | * | |
421 | * pt_info is a buffer containing sync_pt_infos for every sync_pt in the fence. | |
422 | * To itterate over the sync_pt_infos, use the sync_pt_info.len field. | |
423 | */ | |
424 | #define SYNC_IOC_FENCE_INFO _IOWR(SYNC_IOC_MAGIC, 2,\ | |
425 | struct sync_fence_info_data) | |
426 | ||
427 | #endif /* _LINUX_SYNC_H */ |