sync.h 10 KB

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