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