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