blob: 8cdac1a12e4fc9bc6d3c1834372c9643644f809d [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;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -020027struct sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -080028
29/**
30 * struct sync_timeline_ops - sync object implementation ops
Masanari Iida4e20eff2013-10-31 14:20:25 +090031 * @driver_name: name of the implementation
Erik Gilling7ad530b2013-02-28 16:42:57 -080032 * @has_signaled: returns:
33 * 1 if pt has signaled
34 * 0 if pt has not signaled
35 * <0 on error
Masanari Iida4e20eff2013-10-31 14:20:25 +090036 * @fill_driver_data: write implementation specific driver data to data.
Erik Gilling79ba1522013-02-28 16:43:02 -080037 * should return an error if there is not enough room
38 * as specified by size. This information is returned
39 * to userspace by SYNC_IOC_FENCE_INFO.
Erik Gillingdbd52392013-02-28 16:43:21 -080040 * @timeline_value_str: fill str with the value of the sync_timeline's counter
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020041 * @fence_value_str: fill str with the value of the fence
Erik Gilling7ad530b2013-02-28 16:42:57 -080042 */
43struct sync_timeline_ops {
44 const char *driver_name;
45
46 /* required */
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020047 int (*has_signaled)(struct fence *fence);
Erik Gilling7ad530b2013-02-28 16:42:57 -080048
Erik Gilling79ba1522013-02-28 16:43:02 -080049 /* optional */
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020050 int (*fill_driver_data)(struct fence *fence, void *data, int size);
Erik Gillingdbd52392013-02-28 16:43:21 -080051
52 /* optional */
53 void (*timeline_value_str)(struct sync_timeline *timeline, char *str,
54 int size);
55
56 /* optional */
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020057 void (*fence_value_str)(struct fence *fence, char *str, int size);
Erik Gilling7ad530b2013-02-28 16:42:57 -080058};
59
60/**
61 * struct sync_timeline - sync object
Erik Gillingc5b86b72013-02-28 16:43:11 -080062 * @kref: reference count on fence.
Masanari Iida4e20eff2013-10-31 14:20:25 +090063 * @ops: ops that define the implementation of the sync_timeline
Erik Gilling7ad530b2013-02-28 16:42:57 -080064 * @name: name of the sync_timeline. Useful for debugging
Masanari Iida4e20eff2013-10-31 14:20:25 +090065 * @destroyed: set when sync_timeline is destroyed
Erik Gilling7ad530b2013-02-28 16:42:57 -080066 * @child_list_head: list of children sync_pts for this sync_timeline
67 * @child_list_lock: lock protecting @child_list_head, destroyed, and
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020068 * fence.status
Erik Gilling7ad530b2013-02-28 16:42:57 -080069 * @active_list_head: list of active (unsignaled/errored) sync_pts
Erik Gillingaf7582f2013-02-28 16:43:00 -080070 * @sync_timeline_list: membership in global sync_timeline_list
Erik Gilling7ad530b2013-02-28 16:42:57 -080071 */
72struct sync_timeline {
Erik Gillingc5b86b72013-02-28 16:43:11 -080073 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -080074 const struct sync_timeline_ops *ops;
75 char name[32];
76
77 /* protected by child_list_lock */
78 bool destroyed;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020079 int context, value;
Erik Gilling7ad530b2013-02-28 16:42:57 -080080
81 struct list_head child_list_head;
82 spinlock_t child_list_lock;
83
84 struct list_head active_list_head;
Erik Gillingaf7582f2013-02-28 16:43:00 -080085
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020086#ifdef CONFIG_DEBUG_FS
Erik Gillingaf7582f2013-02-28 16:43:00 -080087 struct list_head sync_timeline_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020088#endif
Erik Gilling7ad530b2013-02-28 16:42:57 -080089};
90
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020091static inline struct sync_timeline *fence_parent(struct fence *fence)
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020092{
Gustavo Padovanb55b54b2016-01-21 10:49:21 -020093 return container_of(fence->lock, struct sync_timeline,
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020094 child_list_lock);
95}
Erik Gilling7ad530b2013-02-28 16:42:57 -080096
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -020097struct sync_file_cb {
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +020098 struct fence_cb cb;
Gustavo Padovanc88b26d2016-01-21 10:49:20 -020099 struct fence *fence;
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200100 struct sync_file *sync_file;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800101};
102
103/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200104 * struct sync_file - sync file to export to the userspace
Erik Gilling7ad530b2013-02-28 16:42:57 -0800105 * @file: file representing this fence
Masanari Iida4e20eff2013-10-31 14:20:25 +0900106 * @kref: reference count on fence.
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200107 * @name: name of sync_file. Useful for debugging
108 * @sync_file_list: membership in global file list
Gustavo Padovan9b323812016-01-21 10:49:14 -0200109 * @num_fences number of sync_pts in the fence
110 * @wq: wait queue for fence signaling
111 * @status: 0: signaled, >0:active, <0: error
112 * @cbs: sync_pts callback information
Erik Gilling7ad530b2013-02-28 16:42:57 -0800113 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200114struct sync_file {
Erik Gilling7ad530b2013-02-28 16:42:57 -0800115 struct file *file;
Erik Gilling01544172013-02-28 16:43:10 -0800116 struct kref kref;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800117 char name[32];
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200118#ifdef CONFIG_DEBUG_FS
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200119 struct list_head sync_file_list;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200120#endif
121 int num_fences;
Erik Gilling7ad530b2013-02-28 16:42:57 -0800122
123 wait_queue_head_t wq;
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200124 atomic_t status;
Erik Gillingaf7582f2013-02-28 16:43:00 -0800125
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200126 struct sync_file_cb cbs[];
Erik Gilling7ad530b2013-02-28 16:42:57 -0800127};
128
Erik Gilling7ad530b2013-02-28 16:42:57 -0800129/*
130 * API for sync_timeline implementers
131 */
132
133/**
134 * sync_timeline_create() - creates a sync object
Masanari Iida4e20eff2013-10-31 14:20:25 +0900135 * @ops: specifies the implementation ops for the object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800136 * @size: size to allocate for this obj
137 * @name: sync_timeline name
138 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900139 * Creates a new sync_timeline which will use the implementation specified by
140 * @ops. @size bytes will be allocated allowing for implementation specific
Gustavo Padovan9b323812016-01-21 10:49:14 -0200141 * data to be kept after the generic sync_timeline struct. Returns the
142 * sync_timeline object or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800143 */
144struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops,
145 int size, const char *name);
146
147/**
Masanari Iida4e20eff2013-10-31 14:20:25 +0900148 * sync_timeline_destroy() - destroys a sync object
Erik Gilling7ad530b2013-02-28 16:42:57 -0800149 * @obj: sync_timeline to destroy
150 *
Masanari Iida4e20eff2013-10-31 14:20:25 +0900151 * A sync implementation should call this when the @obj is going away
152 * (i.e. module unload.) @obj won't actually be freed until all its children
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200153 * fences are freed.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800154 */
155void sync_timeline_destroy(struct sync_timeline *obj);
156
157/**
158 * sync_timeline_signal() - signal a status change on a sync_timeline
159 * @obj: sync_timeline to signal
160 *
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200161 * A sync implementation should call this any time one of it's fences
Erik Gilling7ad530b2013-02-28 16:42:57 -0800162 * has signaled or has an error condition.
163 */
164void sync_timeline_signal(struct sync_timeline *obj);
165
166/**
167 * sync_pt_create() - creates a sync pt
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200168 * @parent: fence's parent sync_timeline
Erik Gilling7ad530b2013-02-28 16:42:57 -0800169 * @size: size to allocate for this pt
170 *
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200171 * Creates a new fence as a child of @parent. @size bytes will be
Masanari Iida4e20eff2013-10-31 14:20:25 +0900172 * allocated allowing for implementation specific data to be kept after
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200173 * the generic sync_timeline struct. Returns the fence object or
Gustavo Padovan9b323812016-01-21 10:49:14 -0200174 * NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800175 */
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200176struct fence *sync_pt_create(struct sync_timeline *parent, int size);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800177
178/**
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200179 * sync_fence_create() - creates a sync fence
180 * @name: name of fence to create
181 * @fence: fence to add to the sync_fence
Erik Gilling7ad530b2013-02-28 16:42:57 -0800182 *
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200183 * Creates a sync_file containg @fence. Once this is called, the sync_file
184 * takes ownership of @fence.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800185 */
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200186struct sync_file *sync_file_create(const char *name, struct fence *fence);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800187
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000188/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200189 * sync_file_create_dma() - creates a sync file from dma-fence
190 * @name: name of file to create
191 * @pt: dma-fence to add to the file
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000192 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200193 * Creates a sync_file containg @pt. Once this is called, the fence takes
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000194 * ownership of @pt.
195 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200196struct sync_file *sync_file_create_dma(const char *name, struct fence *pt);
Maarten Lankhorst0f477c62015-12-11 13:11:50 +0000197
Erik Gilling7ad530b2013-02-28 16:42:57 -0800198/*
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200199 * API for sync_file consumers
Erik Gilling7ad530b2013-02-28 16:42:57 -0800200 */
201
202/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200203 * sync_file_merge() - merge two sync_files
Erik Gilling7ad530b2013-02-28 16:42:57 -0800204 * @name: name of new fence
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200205 * @a: sync_file a
206 * @b: sync_file b
Erik Gilling7ad530b2013-02-28 16:42:57 -0800207 *
Gustavo Padovanb55b54b2016-01-21 10:49:21 -0200208 * Creates a new sync_file which contains copies of all the fences in both
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200209 * @a and @b. @a and @b remain valid, independent sync_file. Returns the
210 * new merged sync_file or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800211 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200212struct sync_file *sync_file_merge(const char *name,
213 struct sync_file *a, struct sync_file *b);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800214
215/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200216 * sync_file_fdget() - get a sync_file from an fd
Erik Gilling7ad530b2013-02-28 16:42:57 -0800217 * @fd: fd referencing a fence
218 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200219 * Ensures @fd references a valid sync_file, increments the refcount of the
220 * backing file. Returns the sync_file or NULL in case of error.
Erik Gilling7ad530b2013-02-28 16:42:57 -0800221 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200222struct sync_file *sync_file_fdget(int fd);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800223
224/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200225 * sync_file_put() - puts a reference of a sync_file
226 * @sync_file: sync_file to put
Erik Gilling7ad530b2013-02-28 16:42:57 -0800227 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200228 * Puts a reference on @sync_fence. If this is the last reference, the
229 * sync_fil and all it's sync_pts will be freed
Erik Gilling7ad530b2013-02-28 16:42:57 -0800230 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200231void sync_file_put(struct sync_file *sync_file);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800232
233/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200234 * sync_file_install() - installs a sync_file into a file descriptor
235 * @sync_file: sync_file to install
Erik Gilling7ad530b2013-02-28 16:42:57 -0800236 * @fd: file descriptor in which to install the fence
237 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200238 * Installs @sync_file into @fd. @fd's should be acquired through
Heinrich Schuchardtae664752014-09-27 10:52:37 +0200239 * get_unused_fd_flags(O_CLOEXEC).
Erik Gilling7ad530b2013-02-28 16:42:57 -0800240 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200241void sync_file_install(struct sync_file *sync_file, int fd);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800242
243/**
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200244 * sync_file_wait() - wait on sync file
245 * @sync_file: file to wait on
Erik Gilling7ad530b2013-02-28 16:42:57 -0800246 * @tiemout: timeout in ms
247 *
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200248 * Wait for @sync_file to be signaled or have an error. Waits indefinitely
Gustavo Padovan9b323812016-01-21 10:49:14 -0200249 * if @timeout < 0.
250 *
251 * Returns 0 if fence signaled, > 0 if it is still active and <0 on error
Erik Gilling7ad530b2013-02-28 16:42:57 -0800252 */
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200253int sync_file_wait(struct sync_file *sync_file, long timeout);
Erik Gilling7ad530b2013-02-28 16:42:57 -0800254
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200255#ifdef CONFIG_DEBUG_FS
256
Joe Perchesd30649a2015-08-10 14:51:16 -0700257void sync_timeline_debug_add(struct sync_timeline *obj);
258void sync_timeline_debug_remove(struct sync_timeline *obj);
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200259void sync_file_debug_add(struct sync_file *fence);
260void sync_file_debug_remove(struct sync_file *fence);
Joe Perchesd30649a2015-08-10 14:51:16 -0700261void sync_dump(void);
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200262
263#else
264# define sync_timeline_debug_add(obj)
265# define sync_timeline_debug_remove(obj)
Gustavo Padovand7fdb0a2016-01-21 10:49:19 -0200266# define sync_file_debug_add(fence)
267# define sync_file_debug_remove(fence)
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200268# define sync_dump()
269#endif
Maarten Lankhorst0f0d8402014-07-01 12:57:31 +0200270
Erik Gilling7ad530b2013-02-28 16:42:57 -0800271#endif /* _LINUX_SYNC_H */