blob: a18d1e3365fbdc2df496c41cbd933019617896d8 [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;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -020028struct sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -080029
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 * @has_signaled: returns:
34 * 1 if pt has signaled
35 * 0 if pt has not signaled
36 * <0 on error
Masanari Iida4e20eff2013-10-31 14:20:25 +090037 * @fill_driver_data: write implementation specific driver data to data.
Erik Gilling79ba1522013-02-28 16:43:02 -080038 * should return an error if there is not enough room
39 * as specified by size. This information is returned
40 * to userspace by SYNC_IOC_FENCE_INFO.
Erik Gillingdbd52392013-02-28 16:43:21 -080041 * @timeline_value_str: fill str with the value of the sync_timeline's counter
42 * @pt_value_str: fill str with the value of the sync_pt
Erik Gilling7ad530b2013-02-28 16:42:57 -080043 */
44struct sync_timeline_ops {
45 const char *driver_name;
46
47 /* required */
Erik Gilling7ad530b2013-02-28 16:42:57 -080048 int (*has_signaled)(struct sync_pt *pt);
49
Erik Gilling79ba1522013-02-28 16:43:02 -080050 /* optional */
51 int (*fill_driver_data)(struct sync_pt *syncpt, void *data, int size);
Erik Gillingdbd52392013-02-28 16:43:21 -080052
53 /* optional */
54 void (*timeline_value_str)(struct sync_timeline *timeline, char *str,
55 int size);
56
57 /* optional */
58 void (*pt_value_str)(struct sync_pt *pt, char *str, int size);
Erik Gilling7ad530b2013-02-28 16:42:57 -080059};
60
61/**
62 * struct sync_timeline - sync object
Erik Gillingc5b86b72013-02-28 16:43:11 -080063 * @kref: reference count on fence.
Masanari Iida4e20eff2013-10-31 14:20:25 +090064 * @ops: ops that define the implementation of the sync_timeline
Erik Gilling7ad530b2013-02-28 16:42:57 -080065 * @name: name of the sync_timeline. Useful for debugging
Masanari Iida4e20eff2013-10-31 14:20:25 +090066 * @destroyed: set when sync_timeline is destroyed
Erik Gilling7ad530b2013-02-28 16:42:57 -080067 * @child_list_head: list of children sync_pts for this sync_timeline
68 * @child_list_lock: lock protecting @child_list_head, destroyed, and
69 * sync_pt.status
70 * @active_list_head: list of active (unsignaled/errored) sync_pts
Erik Gillingaf7582f2013-02-28 16:43:00 -080071 * @sync_timeline_list: membership in global sync_timeline_list
Erik Gilling7ad530b2013-02-28 16:42:57 -080072 */
73struct sync_timeline {
Erik Gillingc5b86b72013-02-28 16:43:11 -080074 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -080075 const struct sync_timeline_ops *ops;
76 char name[32];
77
78 /* protected by child_list_lock */
79 bool destroyed;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020080 int context, value;
Erik Gilling7ad530b2013-02-28 16:42:57 -080081
82 struct list_head child_list_head;
83 spinlock_t child_list_lock;
84
85 struct list_head active_list_head;
Erik Gillingaf7582f2013-02-28 16:43:00 -080086
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020087#ifdef CONFIG_DEBUG_FS
Erik Gillingaf7582f2013-02-28 16:43:00 -080088 struct list_head sync_timeline_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020089#endif
Erik Gilling7ad530b2013-02-28 16:42:57 -080090};
91
92/**
93 * struct sync_pt - sync point
Gustavo Padovan9b323812016-01-21 10:49:14 -020094 * @base: base fence class
Erik Gilling7ad530b2013-02-28 16:42:57 -080095 * @child_list: membership in sync_timeline.child_list_head
96 * @active_list: membership in sync_timeline.active_list_head
Erik Gilling7ad530b2013-02-28 16:42:57 -080097 */
98struct sync_pt {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020099 struct fence base;
100
Erik Gilling7ad530b2013-02-28 16:42:57 -0800101 struct list_head child_list;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800102 struct list_head active_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200103};
Erik Gilling7ad530b2013-02-28 16:42:57 -0800104
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200105static inline struct sync_timeline *sync_pt_parent(struct sync_pt *pt)
106{
107 return container_of(pt->base.lock, struct sync_timeline,
108 child_list_lock);
109}
Erik Gilling7ad530b2013-02-28 16:42:57 -0800110
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200111struct sync_file_cb {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200112 struct fence_cb cb;
Gustavo Padovanc88b26d2016-01-21 10:49:20 -0200113 struct fence *fence;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200114 struct sync_file *sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800115};
116
117/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200118 * struct sync_file - sync file to export to the userspace
Erik Gilling7ad530b2013-02-28 16:42:57 -0800119 * @file: file representing this fence
Masanari Iida4e20eff2013-10-31 14:20:25 +0900120 * @kref: reference count on fence.
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200121 * @name: name of sync_file. Useful for debugging
122 * @sync_file_list: membership in global file list
Gustavo Padovan9b323812016-01-21 10:49:14 -0200123 * @num_fences number of sync_pts in the fence
124 * @wq: wait queue for fence signaling
125 * @status: 0: signaled, >0:active, <0: error
126 * @cbs: sync_pts callback information
Erik Gilling7ad530b2013-02-28 16:42:57 -0800127 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200128struct sync_file {
Erik Gilling7ad530b2013-02-28 16:42:57 -0800129 struct file *file;
Erik Gilling01544172013-02-28 16:43:10 -0800130 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800131 char name[32];
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200132#ifdef CONFIG_DEBUG_FS
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200133 struct list_head sync_file_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200134#endif
135 int num_fences;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800136
137 wait_queue_head_t wq;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200138 atomic_t status;
Erik Gillingaf7582f2013-02-28 16:43:00 -0800139
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200140 struct sync_file_cb cbs[];
Erik Gilling7ad530b2013-02-28 16:42:57 -0800141};
142
Erik Gilling7ad530b2013-02-28 16:42:57 -0800143/*
144 * API for sync_timeline implementers
145 */
146
147/**
148 * sync_timeline_create() - creates a sync object
Masanari Iida4e20eff2013-10-31 14:20:25 +0900149 * @ops: specifies the implementation ops for the object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800150 * @size: size to allocate for this obj
151 * @name: sync_timeline name
152 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900153 * Creates a new sync_timeline which will use the implementation specified by
154 * @ops. @size bytes will be allocated allowing for implementation specific
Gustavo Padovan9b323812016-01-21 10:49:14 -0200155 * data to be kept after the generic sync_timeline struct. Returns the
156 * sync_timeline object or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800157 */
158struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
159 int size, const char *name);
160
161/**
Masanari Iida4e20eff2013-10-31 14:20:25 +0900162 * sync_timeline_destroy() - destroys a sync object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800163 * @obj: sync_timeline to destroy
164 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900165 * A sync implementation should call this when the @obj is going away
166 * (i.e. module unload.) @obj won't actually be freed until all its children
Erik Gilling7ad530b2013-02-28 16:42:57 -0800167 * sync_pts are freed.
168 */
169void sync_timeline_destroy(struct sync_timeline *obj);
170
171/**
172 * sync_timeline_signal() - signal a status change on a sync_timeline
173 * @obj: sync_timeline to signal
174 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900175 * A sync implementation should call this any time one of it's sync_pts
Erik Gilling7ad530b2013-02-28 16:42:57 -0800176 * has signaled or has an error condition.
177 */
178void sync_timeline_signal(struct sync_timeline *obj);
179
180/**
181 * sync_pt_create() - creates a sync pt
182 * @parent: sync_pt's parent sync_timeline
183 * @size: size to allocate for this pt
184 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900185 * Creates a new sync_pt as a child of @parent. @size bytes will be
186 * allocated allowing for implementation specific data to be kept after
Gustavo Padovan9b323812016-01-21 10:49:14 -0200187 * the generic sync_timeline struct. Returns the sync_pt object or
188 * NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800189 */
190struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size);
191
192/**
193 * sync_pt_free() - frees a sync pt
194 * @pt: sync_pt to free
195 *
196 * This should only be called on sync_pts which have been created but
197 * not added to a fence.
198 */
199void sync_pt_free(struct sync_pt *pt);
200
201/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200202 * sync_file_create() - creates a sync file
203 * @name: name of file to create
204 * @pt: sync_pt to add to the file
Erik Gilling7ad530b2013-02-28 16:42:57 -0800205 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200206 * Creates a sync_file containg @pt. Once this is called, the sync_file takes
Erik Gilling7ad530b2013-02-28 16:42:57 -0800207 * ownership of @pt.
208 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200209struct sync_file *sync_file_create(const char *name, struct sync_pt *pt);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800210
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000211/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200212 * sync_file_create_dma() - creates a sync file from dma-fence
213 * @name: name of file to create
214 * @pt: dma-fence to add to the file
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000215 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200216 * Creates a sync_file containg @pt. Once this is called, the fence takes
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000217 * ownership of @pt.
218 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200219struct sync_file *sync_file_create_dma(const char *name, struct fence *pt);
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000220
Erik Gilling7ad530b2013-02-28 16:42:57 -0800221/*
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200222 * API for sync_file consumers
Erik Gilling7ad530b2013-02-28 16:42:57 -0800223 */
224
225/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200226 * sync_file_merge() - merge two sync_files
Erik Gilling7ad530b2013-02-28 16:42:57 -0800227 * @name: name of new fence
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200228 * @a: sync_file a
229 * @b: sync_file b
Erik Gilling7ad530b2013-02-28 16:42:57 -0800230 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200231 * Creates a new sync_file which contains copies of all the sync_pts in both
232 * @a and @b. @a and @b remain valid, independent sync_file. Returns the
233 * new merged sync_file or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800234 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200235struct sync_file *sync_file_merge(const char *name,
236 struct sync_file *a, struct sync_file *b);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800237
238/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200239 * sync_file_fdget() - get a sync_file from an fd
Erik Gilling7ad530b2013-02-28 16:42:57 -0800240 * @fd: fd referencing a fence
241 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200242 * Ensures @fd references a valid sync_file, increments the refcount of the
243 * backing file. Returns the sync_file or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800244 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200245struct sync_file *sync_file_fdget(int fd);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800246
247/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200248 * sync_file_put() - puts a reference of a sync_file
249 * @sync_file: sync_file to put
Erik Gilling7ad530b2013-02-28 16:42:57 -0800250 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200251 * Puts a reference on @sync_fence. If this is the last reference, the
252 * sync_fil and all it's sync_pts will be freed
Erik Gilling7ad530b2013-02-28 16:42:57 -0800253 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200254void sync_file_put(struct sync_file *sync_file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800255
256/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200257 * sync_file_install() - installs a sync_file into a file descriptor
258 * @sync_file: sync_file to install
Erik Gilling7ad530b2013-02-28 16:42:57 -0800259 * @fd: file descriptor in which to install the fence
260 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200261 * Installs @sync_file into @fd. @fd's should be acquired through
Heinrich Schuchardtae664752014-09-27 10:52:37 +0200262 * get_unused_fd_flags(O_CLOEXEC).
Erik Gilling7ad530b2013-02-28 16:42:57 -0800263 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200264void sync_file_install(struct sync_file *sync_file, int fd);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800265
266/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200267 * sync_file_wait() - wait on sync file
268 * @sync_file: file to wait on
Erik Gilling7ad530b2013-02-28 16:42:57 -0800269 * @tiemout: timeout in ms
270 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200271 * Wait for @sync_file to be signaled or have an error. Waits indefinitely
Gustavo Padovan9b323812016-01-21 10:49:14 -0200272 * if @timeout < 0.
273 *
274 * Returns 0 if fence signaled, > 0 if it is still active and <0 on error
Erik Gilling7ad530b2013-02-28 16:42:57 -0800275 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200276int sync_file_wait(struct sync_file *sync_file, long timeout);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800277
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200278#ifdef CONFIG_DEBUG_FS
279
Joe Perchesd30649a2015-08-10 14:51:16 -0700280void sync_timeline_debug_add(struct sync_timeline *obj);
281void sync_timeline_debug_remove(struct sync_timeline *obj);
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200282void sync_file_debug_add(struct sync_file *fence);
283void sync_file_debug_remove(struct sync_file *fence);
Joe Perchesd30649a2015-08-10 14:51:16 -0700284void sync_dump(void);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200285
286#else
287# define sync_timeline_debug_add(obj)
288# define sync_timeline_debug_remove(obj)
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200289# define sync_file_debug_add(fence)
290# define sync_file_debug_remove(fence)
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200291# define sync_dump()
292#endif
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200293
Erik Gilling7ad530b2013-02-28 16:42:57 -0800294#endif /* _LINUX_SYNC_H */