Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 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> |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 17 | #include <linux/kref.h> |
Erik Gilling | 97a8484 | 2013-02-28 16:42:59 -0800 | [diff] [blame] | 18 | #include <linux/ktime.h> |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 19 | #include <linux/list.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/wait.h> |
| 22 | |
Colin Cross | 64907b9 | 2014-02-17 13:58:32 -0800 | [diff] [blame^] | 23 | #include "uapi/sync.h" |
| 24 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 25 | struct sync_timeline; |
| 26 | struct sync_pt; |
| 27 | struct sync_fence; |
| 28 | |
| 29 | /** |
| 30 | * struct sync_timeline_ops - sync object implementation ops |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 31 | * @driver_name: name of the implementation |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 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 |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 40 | * -1 if a will signal before b |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 41 | * @free_pt: called before sync_pt is freed |
| 42 | * @release_obj: called before sync_timeline is freed |
Erik Gilling | dbd5239 | 2013-02-28 16:43:21 -0800 | [diff] [blame] | 43 | * @print_obj: deprecated |
| 44 | * @print_pt: deprecated |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 45 | * @fill_driver_data: write implementation specific driver data to data. |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 46 | * should return an error if there is not enough room |
| 47 | * as specified by size. This information is returned |
| 48 | * to userspace by SYNC_IOC_FENCE_INFO. |
Erik Gilling | dbd5239 | 2013-02-28 16:43:21 -0800 | [diff] [blame] | 49 | * @timeline_value_str: fill str with the value of the sync_timeline's counter |
| 50 | * @pt_value_str: fill str with the value of the sync_pt |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 51 | */ |
| 52 | struct sync_timeline_ops { |
| 53 | const char *driver_name; |
| 54 | |
| 55 | /* required */ |
Daeseok Youn | 393f539 | 2014-02-10 14:36:48 +0900 | [diff] [blame] | 56 | struct sync_pt * (*dup)(struct sync_pt *pt); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 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); |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 69 | |
Erik Gilling | dbd5239 | 2013-02-28 16:43:21 -0800 | [diff] [blame] | 70 | /* deprecated */ |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 71 | void (*print_obj)(struct seq_file *s, |
| 72 | struct sync_timeline *sync_timeline); |
| 73 | |
Erik Gilling | dbd5239 | 2013-02-28 16:43:21 -0800 | [diff] [blame] | 74 | /* deprecated */ |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 75 | void (*print_pt)(struct seq_file *s, struct sync_pt *sync_pt); |
Erik Gilling | 79ba152 | 2013-02-28 16:43:02 -0800 | [diff] [blame] | 76 | |
| 77 | /* optional */ |
| 78 | int (*fill_driver_data)(struct sync_pt *syncpt, void *data, int size); |
Erik Gilling | dbd5239 | 2013-02-28 16:43:21 -0800 | [diff] [blame] | 79 | |
| 80 | /* optional */ |
| 81 | void (*timeline_value_str)(struct sync_timeline *timeline, char *str, |
| 82 | int size); |
| 83 | |
| 84 | /* optional */ |
| 85 | void (*pt_value_str)(struct sync_pt *pt, char *str, int size); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | /** |
| 89 | * struct sync_timeline - sync object |
Erik Gilling | c5b86b7 | 2013-02-28 16:43:11 -0800 | [diff] [blame] | 90 | * @kref: reference count on fence. |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 91 | * @ops: ops that define the implementation of the sync_timeline |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 92 | * @name: name of the sync_timeline. Useful for debugging |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 93 | * @destroyed: set when sync_timeline is destroyed |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 94 | * @child_list_head: list of children sync_pts for this sync_timeline |
| 95 | * @child_list_lock: lock protecting @child_list_head, destroyed, and |
| 96 | * sync_pt.status |
| 97 | * @active_list_head: list of active (unsignaled/errored) sync_pts |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 98 | * @sync_timeline_list: membership in global sync_timeline_list |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 99 | */ |
| 100 | struct sync_timeline { |
Erik Gilling | c5b86b7 | 2013-02-28 16:43:11 -0800 | [diff] [blame] | 101 | struct kref kref; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 102 | const struct sync_timeline_ops *ops; |
| 103 | char name[32]; |
| 104 | |
| 105 | /* protected by child_list_lock */ |
| 106 | bool destroyed; |
| 107 | |
| 108 | struct list_head child_list_head; |
| 109 | spinlock_t child_list_lock; |
| 110 | |
| 111 | struct list_head active_list_head; |
| 112 | spinlock_t active_list_lock; |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 113 | |
| 114 | struct list_head sync_timeline_list; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | /** |
| 118 | * struct sync_pt - sync point |
| 119 | * @parent: sync_timeline to which this sync_pt belongs |
| 120 | * @child_list: membership in sync_timeline.child_list_head |
| 121 | * @active_list: membership in sync_timeline.active_list_head |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 122 | * @signaled_list: membership in temporary signaled_list on stack |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 123 | * @fence: sync_fence to which the sync_pt belongs |
| 124 | * @pt_list: membership in sync_fence.pt_list_head |
| 125 | * @status: 1: signaled, 0:active, <0: error |
Erik Gilling | 97a8484 | 2013-02-28 16:42:59 -0800 | [diff] [blame] | 126 | * @timestamp: time which sync_pt status transitioned from active to |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 127 | * signaled or error. |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 128 | */ |
| 129 | struct sync_pt { |
| 130 | struct sync_timeline *parent; |
| 131 | struct list_head child_list; |
| 132 | |
| 133 | struct list_head active_list; |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 134 | struct list_head signaled_list; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 135 | |
| 136 | struct sync_fence *fence; |
| 137 | struct list_head pt_list; |
| 138 | |
| 139 | /* protected by parent->active_list_lock */ |
| 140 | int status; |
Erik Gilling | 97a8484 | 2013-02-28 16:42:59 -0800 | [diff] [blame] | 141 | |
| 142 | ktime_t timestamp; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | /** |
| 146 | * struct sync_fence - sync fence |
| 147 | * @file: file representing this fence |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 148 | * @kref: reference count on fence. |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 149 | * @name: name of sync_fence. Useful for debugging |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 150 | * @pt_list_head: list of sync_pts in the fence. immutable once fence |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 151 | * is created |
| 152 | * @waiter_list_head: list of asynchronous waiters on this fence |
| 153 | * @waiter_list_lock: lock protecting @waiter_list_head and @status |
| 154 | * @status: 1: signaled, 0:active, <0: error |
| 155 | * |
| 156 | * @wq: wait queue for fence signaling |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 157 | * @sync_fence_list: membership in global fence list |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 158 | */ |
| 159 | struct sync_fence { |
| 160 | struct file *file; |
Erik Gilling | 0154417 | 2013-02-28 16:43:10 -0800 | [diff] [blame] | 161 | struct kref kref; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 162 | char name[32]; |
| 163 | |
| 164 | /* this list is immutable once the fence is created */ |
| 165 | struct list_head pt_list_head; |
| 166 | |
| 167 | struct list_head waiter_list_head; |
| 168 | spinlock_t waiter_list_lock; /* also protects status */ |
| 169 | int status; |
| 170 | |
| 171 | wait_queue_head_t wq; |
Erik Gilling | af7582f | 2013-02-28 16:43:00 -0800 | [diff] [blame] | 172 | |
| 173 | struct list_head sync_fence_list; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 176 | struct sync_fence_waiter; |
| 177 | typedef void (*sync_callback_t)(struct sync_fence *fence, |
| 178 | struct sync_fence_waiter *waiter); |
| 179 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 180 | /** |
| 181 | * struct sync_fence_waiter - metadata for asynchronous waiter on a fence |
| 182 | * @waiter_list: membership in sync_fence.waiter_list_head |
| 183 | * @callback: function pointer to call when fence signals |
| 184 | * @callback_data: pointer to pass to @callback |
| 185 | */ |
| 186 | struct sync_fence_waiter { |
| 187 | struct list_head waiter_list; |
| 188 | |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 189 | sync_callback_t callback; |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 190 | }; |
| 191 | |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 192 | static inline void sync_fence_waiter_init(struct sync_fence_waiter *waiter, |
| 193 | sync_callback_t callback) |
| 194 | { |
| 195 | waiter->callback = callback; |
| 196 | } |
| 197 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 198 | /* |
| 199 | * API for sync_timeline implementers |
| 200 | */ |
| 201 | |
| 202 | /** |
| 203 | * sync_timeline_create() - creates a sync object |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 204 | * @ops: specifies the implementation ops for the object |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 205 | * @size: size to allocate for this obj |
| 206 | * @name: sync_timeline name |
| 207 | * |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 208 | * Creates a new sync_timeline which will use the implementation specified by |
| 209 | * @ops. @size bytes will be allocated allowing for implementation specific |
| 210 | * data to be kept after the generic sync_timeline struct. |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 211 | */ |
| 212 | struct sync_timeline *sync_timeline_create(const struct sync_timeline_ops *ops, |
| 213 | int size, const char *name); |
| 214 | |
| 215 | /** |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 216 | * sync_timeline_destroy() - destroys a sync object |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 217 | * @obj: sync_timeline to destroy |
| 218 | * |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 219 | * A sync implementation should call this when the @obj is going away |
| 220 | * (i.e. module unload.) @obj won't actually be freed until all its children |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 221 | * sync_pts are freed. |
| 222 | */ |
| 223 | void sync_timeline_destroy(struct sync_timeline *obj); |
| 224 | |
| 225 | /** |
| 226 | * sync_timeline_signal() - signal a status change on a sync_timeline |
| 227 | * @obj: sync_timeline to signal |
| 228 | * |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 229 | * A sync implementation should call this any time one of it's sync_pts |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 230 | * has signaled or has an error condition. |
| 231 | */ |
| 232 | void sync_timeline_signal(struct sync_timeline *obj); |
| 233 | |
| 234 | /** |
| 235 | * sync_pt_create() - creates a sync pt |
| 236 | * @parent: sync_pt's parent sync_timeline |
| 237 | * @size: size to allocate for this pt |
| 238 | * |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 239 | * Creates a new sync_pt as a child of @parent. @size bytes will be |
| 240 | * allocated allowing for implementation specific data to be kept after |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 241 | * the generic sync_timeline struct. |
| 242 | */ |
| 243 | struct sync_pt *sync_pt_create(struct sync_timeline *parent, int size); |
| 244 | |
| 245 | /** |
| 246 | * sync_pt_free() - frees a sync pt |
| 247 | * @pt: sync_pt to free |
| 248 | * |
| 249 | * This should only be called on sync_pts which have been created but |
| 250 | * not added to a fence. |
| 251 | */ |
| 252 | void sync_pt_free(struct sync_pt *pt); |
| 253 | |
| 254 | /** |
| 255 | * sync_fence_create() - creates a sync fence |
| 256 | * @name: name of fence to create |
| 257 | * @pt: sync_pt to add to the fence |
| 258 | * |
| 259 | * Creates a fence containg @pt. Once this is called, the fence takes |
| 260 | * ownership of @pt. |
| 261 | */ |
| 262 | struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt); |
| 263 | |
| 264 | /* |
| 265 | * API for sync_fence consumers |
| 266 | */ |
| 267 | |
| 268 | /** |
| 269 | * sync_fence_merge() - merge two fences |
| 270 | * @name: name of new fence |
| 271 | * @a: fence a |
| 272 | * @b: fence b |
| 273 | * |
| 274 | * Creates a new fence which contains copies of all the sync_pts in both |
| 275 | * @a and @b. @a and @b remain valid, independent fences. |
| 276 | */ |
| 277 | struct sync_fence *sync_fence_merge(const char *name, |
| 278 | struct sync_fence *a, struct sync_fence *b); |
| 279 | |
| 280 | /** |
| 281 | * sync_fence_fdget() - get a fence from an fd |
| 282 | * @fd: fd referencing a fence |
| 283 | * |
| 284 | * Ensures @fd references a valid fence, increments the refcount of the backing |
| 285 | * file, and returns the fence. |
| 286 | */ |
| 287 | struct sync_fence *sync_fence_fdget(int fd); |
| 288 | |
| 289 | /** |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 290 | * sync_fence_put() - puts a reference of a sync fence |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 291 | * @fence: fence to put |
| 292 | * |
| 293 | * Puts a reference on @fence. If this is the last reference, the fence and |
| 294 | * all it's sync_pts will be freed |
| 295 | */ |
| 296 | void sync_fence_put(struct sync_fence *fence); |
| 297 | |
| 298 | /** |
| 299 | * sync_fence_install() - installs a fence into a file descriptor |
Masanari Iida | 4e20eff | 2013-10-31 14:20:25 +0900 | [diff] [blame] | 300 | * @fence: fence to install |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 301 | * @fd: file descriptor in which to install the fence |
| 302 | * |
| 303 | * Installs @fence into @fd. @fd's should be acquired through get_unused_fd(). |
| 304 | */ |
| 305 | void sync_fence_install(struct sync_fence *fence, int fd); |
| 306 | |
| 307 | /** |
| 308 | * sync_fence_wait_async() - registers and async wait on the fence |
| 309 | * @fence: fence to wait on |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 310 | * @waiter: waiter callback struck |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 311 | * |
| 312 | * Returns 1 if @fence has already signaled. |
| 313 | * |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 314 | * Registers a callback to be called when @fence signals or has an error. |
| 315 | * @waiter should be initialized with sync_fence_waiter_init(). |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 316 | */ |
| 317 | int sync_fence_wait_async(struct sync_fence *fence, |
Erik Gilling | c0f61a4 | 2013-02-28 16:43:05 -0800 | [diff] [blame] | 318 | struct sync_fence_waiter *waiter); |
| 319 | |
| 320 | /** |
| 321 | * sync_fence_cancel_async() - cancels an async wait |
| 322 | * @fence: fence to wait on |
| 323 | * @waiter: waiter callback struck |
| 324 | * |
| 325 | * returns 0 if waiter was removed from fence's async waiter list. |
| 326 | * returns -ENOENT if waiter was not found on fence's async waiter list. |
| 327 | * |
| 328 | * Cancels a previously registered async wait. Will fail gracefully if |
| 329 | * @waiter was never registered or if @fence has already signaled @waiter. |
| 330 | */ |
| 331 | int sync_fence_cancel_async(struct sync_fence *fence, |
| 332 | struct sync_fence_waiter *waiter); |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 333 | |
| 334 | /** |
| 335 | * sync_fence_wait() - wait on fence |
| 336 | * @fence: fence to wait on |
| 337 | * @tiemout: timeout in ms |
| 338 | * |
Erik Gilling | 3b640f5 | 2013-02-28 16:43:14 -0800 | [diff] [blame] | 339 | * Wait for @fence to be signaled or have an error. Waits indefinitely |
| 340 | * if @timeout < 0 |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 341 | */ |
| 342 | int sync_fence_wait(struct sync_fence *fence, long timeout); |
| 343 | |
Erik Gilling | 7ad530b | 2013-02-28 16:42:57 -0800 | [diff] [blame] | 344 | #endif /* _LINUX_SYNC_H */ |