blob: 66b0f431f63e287af2c07645e32696065cc35ae8 [file] [log] [blame]
Erik Gilling7ad530b2013-02-28 16:42:57 -08001/*
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>
Erik Gilling01544172013-02-28 16:43:10 -080017#include <linux/kref.h>
Erik Gilling97a84842013-02-28 16:42:59 -080018#include <linux/ktime.h>
Erik Gilling7ad530b2013-02-28 16:42:57 -080019#include <linux/list.h>
20#include <linux/spinlock.h>
21#include <linux/wait.h>
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020022#include <linux/fence.h>
Erik Gilling7ad530b2013-02-28 16:42:57 -080023
Colin Cross64907b92014-02-17 13:58:32 -080024#include "uapi/sync.h"
25
Erik Gilling7ad530b2013-02-28 16:42:57 -080026struct sync_timeline;
27struct sync_pt;
28struct sync_fence;
29
30/**
31 * struct sync_timeline_ops - sync object implementation ops
Masanari Iida4e20eff2013-10-31 14:20:25 +090032 * @driver_name: name of the implementation
Erik Gilling7ad530b2013-02-28 16:42:57 -080033 * @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
Masanari Iida4e20eff2013-10-31 14:20:25 +090041 * -1 if a will signal before b
Erik Gilling7ad530b2013-02-28 16:42:57 -080042 * @free_pt: called before sync_pt is freed
43 * @release_obj: called before sync_timeline is freed
Masanari Iida4e20eff2013-10-31 14:20:25 +090044 * @fill_driver_data: write implementation specific driver data to data.
Erik Gilling79ba1522013-02-28 16:43:02 -080045 * should return an error if there is not enough room
46 * as specified by size. This information is returned
47 * to userspace by SYNC_IOC_FENCE_INFO.
Erik Gillingdbd52392013-02-28 16:43:21 -080048 * @timeline_value_str: fill str with the value of the sync_timeline's counter
49 * @pt_value_str: fill str with the value of the sync_pt
Erik Gilling7ad530b2013-02-28 16:42:57 -080050 */
51struct sync_timeline_ops {
52 const char *driver_name;
53
54 /* required */
Daeseok Youn393f5392014-02-10 14:36:48 +090055 struct sync_pt * (*dup)(struct sync_pt *pt);
Erik Gilling7ad530b2013-02-28 16:42:57 -080056
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 Gillingaf7582f2013-02-28 16:43:00 -080068
Erik Gilling79ba1522013-02-28 16:43:02 -080069 /* optional */
70 int (*fill_driver_data)(struct sync_pt *syncpt, void *data, int size);
Erik Gillingdbd52392013-02-28 16:43:21 -080071
72 /* optional */
73 void (*timeline_value_str)(struct sync_timeline *timeline, char *str,
74 int size);
75
76 /* optional */
77 void (*pt_value_str)(struct sync_pt *pt, char *str, int size);
Erik Gilling7ad530b2013-02-28 16:42:57 -080078};
79
80/**
81 * struct sync_timeline - sync object
Erik Gillingc5b86b72013-02-28 16:43:11 -080082 * @kref: reference count on fence.
Masanari Iida4e20eff2013-10-31 14:20:25 +090083 * @ops: ops that define the implementation of the sync_timeline
Erik Gilling7ad530b2013-02-28 16:42:57 -080084 * @name: name of the sync_timeline. Useful for debugging
Masanari Iida4e20eff2013-10-31 14:20:25 +090085 * @destroyed: set when sync_timeline is destroyed
Erik Gilling7ad530b2013-02-28 16:42:57 -080086 * @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
Erik Gillingaf7582f2013-02-28 16:43:00 -080090 * @sync_timeline_list: membership in global sync_timeline_list
Erik Gilling7ad530b2013-02-28 16:42:57 -080091 */
92struct sync_timeline {
Erik Gillingc5b86b72013-02-28 16:43:11 -080093 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -080094 const struct sync_timeline_ops *ops;
95 char name[32];
96
97 /* protected by child_list_lock */
98 bool destroyed;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020099 int context, value;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800100
101 struct list_head child_list_head;
102 spinlock_t child_list_lock;
103
104 struct list_head active_list_head;
Erik Gillingaf7582f2013-02-28 16:43:00 -0800105
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200106#ifdef CONFIG_DEBUG_FS
Erik Gillingaf7582f2013-02-28 16:43:00 -0800107 struct list_head sync_timeline_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200108#endif
Erik Gilling7ad530b2013-02-28 16:42:57 -0800109};
110
111/**
112 * struct sync_pt - sync point
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200113 * @fence: base fence class
Erik Gilling7ad530b2013-02-28 16:42:57 -0800114 * @child_list: membership in sync_timeline.child_list_head
115 * @active_list: membership in sync_timeline.active_list_head
Masanari Iida4e20eff2013-10-31 14:20:25 +0900116 * @signaled_list: membership in temporary signaled_list on stack
Erik Gilling7ad530b2013-02-28 16:42:57 -0800117 * @fence: sync_fence to which the sync_pt belongs
118 * @pt_list: membership in sync_fence.pt_list_head
119 * @status: 1: signaled, 0:active, <0: error
Erik Gilling97a84842013-02-28 16:42:59 -0800120 * @timestamp: time which sync_pt status transitioned from active to
Masanari Iida4e20eff2013-10-31 14:20:25 +0900121 * signaled or error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800122 */
123struct sync_pt {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200124 struct fence base;
125
Erik Gilling7ad530b2013-02-28 16:42:57 -0800126 struct list_head child_list;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800127 struct list_head active_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200128};
Erik Gilling7ad530b2013-02-28 16:42:57 -0800129
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200130static inline struct sync_timeline *sync_pt_parent(struct sync_pt *pt)
131{
132 return container_of(pt->base.lock, struct sync_timeline,
133 child_list_lock);
134}
Erik Gilling7ad530b2013-02-28 16:42:57 -0800135
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200136struct sync_fence_cb {
137 struct fence_cb cb;
138 struct fence *sync_pt;
139 struct sync_fence *fence;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800140};
141
142/**
143 * struct sync_fence - sync fence
144 * @file: file representing this fence
Masanari Iida4e20eff2013-10-31 14:20:25 +0900145 * @kref: reference count on fence.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800146 * @name: name of sync_fence. Useful for debugging
Masanari Iida4e20eff2013-10-31 14:20:25 +0900147 * @pt_list_head: list of sync_pts in the fence. immutable once fence
Erik Gilling7ad530b2013-02-28 16:42:57 -0800148 * is created
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200149 * @status: 0: signaled, >0:active, <0: error
Erik Gilling7ad530b2013-02-28 16:42:57 -0800150 *
151 * @wq: wait queue for fence signaling
Erik Gillingaf7582f2013-02-28 16:43:00 -0800152 * @sync_fence_list: membership in global fence list
Erik Gilling7ad530b2013-02-28 16:42:57 -0800153 */
154struct sync_fence {
155 struct file *file;
Erik Gilling01544172013-02-28 16:43:10 -0800156 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800157 char name[32];
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200158#ifdef CONFIG_DEBUG_FS
159 struct list_head sync_fence_list;
160#endif
161 int num_fences;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800162
163 wait_queue_head_t wq;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200164 atomic_t status;
Erik Gillingaf7582f2013-02-28 16:43:00 -0800165
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200166 struct sync_fence_cb cbs[];
Erik Gilling7ad530b2013-02-28 16:42:57 -0800167};
168
Erik Gillingc0f61a42013-02-28 16:43:05 -0800169struct sync_fence_waiter;
170typedef void (*sync_callback_t)(struct sync_fence *fence,
171 struct sync_fence_waiter *waiter);
172
Erik Gilling7ad530b2013-02-28 16:42:57 -0800173/**
174 * struct sync_fence_waiter - metadata for asynchronous waiter on a fence
175 * @waiter_list: membership in sync_fence.waiter_list_head
176 * @callback: function pointer to call when fence signals
177 * @callback_data: pointer to pass to @callback
178 */
179struct sync_fence_waiter {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200180 wait_queue_t work;
181 sync_callback_t callback;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800182};
183
Erik Gillingc0f61a42013-02-28 16:43:05 -0800184static inline void sync_fence_waiter_init(struct sync_fence_waiter *waiter,
185 sync_callback_t callback)
186{
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200187 INIT_LIST_HEAD(&waiter->work.task_list);
Erik Gillingc0f61a42013-02-28 16:43:05 -0800188 waiter->callback = callback;
189}
190
Erik Gilling7ad530b2013-02-28 16:42:57 -0800191/*
192 * API for sync_timeline implementers
193 */
194
195/**
196 * sync_timeline_create() - creates a sync object
Masanari Iida4e20eff2013-10-31 14:20:25 +0900197 * @ops: specifies the implementation ops for the object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800198 * @size: size to allocate for this obj
199 * @name: sync_timeline name
200 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900201 * Creates a new sync_timeline which will use the implementation specified by
202 * @ops. @size bytes will be allocated allowing for implementation specific
203 * data to be kept after the generic sync_timeline struct.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800204 */
205struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
206 int size, const char *name);
207
208/**
Masanari Iida4e20eff2013-10-31 14:20:25 +0900209 * sync_timeline_destroy() - destroys a sync object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800210 * @obj: sync_timeline to destroy
211 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900212 * A sync implementation should call this when the @obj is going away
213 * (i.e. module unload.) @obj won't actually be freed until all its children
Erik Gilling7ad530b2013-02-28 16:42:57 -0800214 * sync_pts are freed.
215 */
216void sync_timeline_destroy(struct sync_timeline *obj);
217
218/**
219 * sync_timeline_signal() - signal a status change on a sync_timeline
220 * @obj: sync_timeline to signal
221 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900222 * A sync implementation should call this any time one of it's sync_pts
Erik Gilling7ad530b2013-02-28 16:42:57 -0800223 * has signaled or has an error condition.
224 */
225void sync_timeline_signal(struct sync_timeline *obj);
226
227/**
228 * sync_pt_create() - creates a sync pt
229 * @parent: sync_pt's parent sync_timeline
230 * @size: size to allocate for this pt
231 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900232 * Creates a new sync_pt as a child of @parent. @size bytes will be
233 * allocated allowing for implementation specific data to be kept after
Erik Gilling7ad530b2013-02-28 16:42:57 -0800234 * the generic sync_timeline struct.
235 */
236struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size);
237
238/**
239 * sync_pt_free() - frees a sync pt
240 * @pt: sync_pt to free
241 *
242 * This should only be called on sync_pts which have been created but
243 * not added to a fence.
244 */
245void sync_pt_free(struct sync_pt *pt);
246
247/**
248 * sync_fence_create() - creates a sync fence
249 * @name: name of fence to create
250 * @pt: sync_pt to add to the fence
251 *
252 * Creates a fence containg @pt. Once this is called, the fence takes
253 * ownership of @pt.
254 */
255struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt);
256
257/*
258 * API for sync_fence consumers
259 */
260
261/**
262 * sync_fence_merge() - merge two fences
263 * @name: name of new fence
264 * @a: fence a
265 * @b: fence b
266 *
267 * Creates a new fence which contains copies of all the sync_pts in both
268 * @a and @b. @a and @b remain valid, independent fences.
269 */
270struct sync_fence *sync_fence_merge(const char *name,
271 struct sync_fence *a, struct sync_fence *b);
272
273/**
274 * sync_fence_fdget() - get a fence from an fd
275 * @fd: fd referencing a fence
276 *
277 * Ensures @fd references a valid fence, increments the refcount of the backing
278 * file, and returns the fence.
279 */
280struct sync_fence *sync_fence_fdget(int fd);
281
282/**
Masanari Iida4e20eff2013-10-31 14:20:25 +0900283 * sync_fence_put() - puts a reference of a sync fence
Erik Gilling7ad530b2013-02-28 16:42:57 -0800284 * @fence: fence to put
285 *
286 * Puts a reference on @fence. If this is the last reference, the fence and
287 * all it's sync_pts will be freed
288 */
289void sync_fence_put(struct sync_fence *fence);
290
291/**
292 * sync_fence_install() - installs a fence into a file descriptor
Masanari Iida4e20eff2013-10-31 14:20:25 +0900293 * @fence: fence to install
Erik Gilling7ad530b2013-02-28 16:42:57 -0800294 * @fd: file descriptor in which to install the fence
295 *
296 * Installs @fence into @fd. @fd's should be acquired through get_unused_fd().
297 */
298void sync_fence_install(struct sync_fence *fence, int fd);
299
300/**
301 * sync_fence_wait_async() - registers and async wait on the fence
302 * @fence: fence to wait on
Erik Gillingc0f61a42013-02-28 16:43:05 -0800303 * @waiter: waiter callback struck
Erik Gilling7ad530b2013-02-28 16:42:57 -0800304 *
305 * Returns 1 if @fence has already signaled.
306 *
Erik Gillingc0f61a42013-02-28 16:43:05 -0800307 * Registers a callback to be called when @fence signals or has an error.
308 * @waiter should be initialized with sync_fence_waiter_init().
Erik Gilling7ad530b2013-02-28 16:42:57 -0800309 */
310int sync_fence_wait_async(struct sync_fence *fence,
Erik Gillingc0f61a42013-02-28 16:43:05 -0800311 struct sync_fence_waiter *waiter);
312
313/**
314 * sync_fence_cancel_async() - cancels an async wait
315 * @fence: fence to wait on
316 * @waiter: waiter callback struck
317 *
318 * returns 0 if waiter was removed from fence's async waiter list.
319 * returns -ENOENT if waiter was not found on fence's async waiter list.
320 *
321 * Cancels a previously registered async wait. Will fail gracefully if
322 * @waiter was never registered or if @fence has already signaled @waiter.
323 */
324int sync_fence_cancel_async(struct sync_fence *fence,
325 struct sync_fence_waiter *waiter);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800326
327/**
328 * sync_fence_wait() - wait on fence
329 * @fence: fence to wait on
330 * @tiemout: timeout in ms
331 *
Erik Gilling3b640f52013-02-28 16:43:14 -0800332 * Wait for @fence to be signaled or have an error. Waits indefinitely
333 * if @timeout < 0
Erik Gilling7ad530b2013-02-28 16:42:57 -0800334 */
335int sync_fence_wait(struct sync_fence *fence, long timeout);
336
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200337#ifdef CONFIG_DEBUG_FS
338
339extern void sync_timeline_debug_add(struct sync_timeline *obj);
340extern void sync_timeline_debug_remove(struct sync_timeline *obj);
341extern void sync_fence_debug_add(struct sync_fence *fence);
342extern void sync_fence_debug_remove(struct sync_fence *fence);
343extern void sync_dump(void);
344
345#else
346# define sync_timeline_debug_add(obj)
347# define sync_timeline_debug_remove(obj)
348# define sync_fence_debug_add(fence)
349# define sync_fence_debug_remove(fence)
350# define sync_dump()
351#endif
352int sync_fence_wake_up_wq(wait_queue_t *curr, unsigned mode,
353 int wake_flags, void *key);
354
Erik Gilling7ad530b2013-02-28 16:42:57 -0800355#endif /* _LINUX_SYNC_H */