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