reservation.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Header file for reservations for dma-buf and ttm
  3. *
  4. * Copyright(C) 2011 Linaro Limited. All rights reserved.
  5. * Copyright (C) 2012-2013 Canonical Ltd
  6. * Copyright (C) 2012 Texas Instruments
  7. *
  8. * Authors:
  9. * Rob Clark <robdclark@gmail.com>
  10. * Maarten Lankhorst <maarten.lankhorst@canonical.com>
  11. * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  12. *
  13. * Based on bo.c which bears the following copyright notice,
  14. * but is dual licensed:
  15. *
  16. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  17. * All Rights Reserved.
  18. *
  19. * Permission is hereby granted, free of charge, to any person obtaining a
  20. * copy of this software and associated documentation files (the
  21. * "Software"), to deal in the Software without restriction, including
  22. * without limitation the rights to use, copy, modify, merge, publish,
  23. * distribute, sub license, and/or sell copies of the Software, and to
  24. * permit persons to whom the Software is furnished to do so, subject to
  25. * the following conditions:
  26. *
  27. * The above copyright notice and this permission notice (including the
  28. * next paragraph) shall be included in all copies or substantial portions
  29. * of the Software.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  32. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  33. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  34. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  35. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  36. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  37. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. */
  39. #ifndef _LINUX_RESERVATION_H
  40. #define _LINUX_RESERVATION_H
  41. #include <linux/ww_mutex.h>
  42. #include <linux/dma-fence.h>
  43. #include <linux/slab.h>
  44. #include <linux/seqlock.h>
  45. #include <linux/rcupdate.h>
  46. extern struct ww_class reservation_ww_class;
  47. extern struct lock_class_key reservation_seqcount_class;
  48. extern const char reservation_seqcount_string[];
  49. /**
  50. * struct reservation_object_list - a list of shared fences
  51. * @rcu: for internal use
  52. * @shared_count: table of shared fences
  53. * @shared_max: for growing shared fence table
  54. * @shared: shared fence table
  55. */
  56. struct reservation_object_list {
  57. struct rcu_head rcu;
  58. u32 shared_count, shared_max;
  59. struct dma_fence __rcu *shared[];
  60. };
  61. /**
  62. * struct reservation_object - a reservation object manages fences for a buffer
  63. * @lock: update side lock
  64. * @seq: sequence count for managing RCU read-side synchronization
  65. * @fence_excl: the exclusive fence, if there is one currently
  66. * @fence: list of current shared fences
  67. * @staged: staged copy of shared fences for RCU updates
  68. */
  69. struct reservation_object {
  70. struct ww_mutex lock;
  71. seqcount_t seq;
  72. struct dma_fence __rcu *fence_excl;
  73. struct reservation_object_list __rcu *fence;
  74. struct reservation_object_list *staged;
  75. };
  76. #define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
  77. #define reservation_object_assert_held(obj) \
  78. lockdep_assert_held(&(obj)->lock.base)
  79. /**
  80. * reservation_object_init - initialize a reservation object
  81. * @obj: the reservation object
  82. */
  83. static inline void
  84. reservation_object_init(struct reservation_object *obj)
  85. {
  86. ww_mutex_init(&obj->lock, &reservation_ww_class);
  87. __seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
  88. RCU_INIT_POINTER(obj->fence, NULL);
  89. RCU_INIT_POINTER(obj->fence_excl, NULL);
  90. obj->staged = NULL;
  91. }
  92. /**
  93. * reservation_object_fini - destroys a reservation object
  94. * @obj: the reservation object
  95. */
  96. static inline void
  97. reservation_object_fini(struct reservation_object *obj)
  98. {
  99. int i;
  100. struct reservation_object_list *fobj;
  101. struct dma_fence *excl;
  102. /*
  103. * This object should be dead and all references must have
  104. * been released to it, so no need to be protected with rcu.
  105. */
  106. excl = rcu_dereference_protected(obj->fence_excl, 1);
  107. if (excl)
  108. dma_fence_put(excl);
  109. fobj = rcu_dereference_protected(obj->fence, 1);
  110. if (fobj) {
  111. for (i = 0; i < fobj->shared_count; ++i)
  112. dma_fence_put(rcu_dereference_protected(fobj->shared[i], 1));
  113. kfree(fobj);
  114. }
  115. kfree(obj->staged);
  116. ww_mutex_destroy(&obj->lock);
  117. }
  118. /**
  119. * reservation_object_get_list - get the reservation object's
  120. * shared fence list, with update-side lock held
  121. * @obj: the reservation object
  122. *
  123. * Returns the shared fence list. Does NOT take references to
  124. * the fence. The obj->lock must be held.
  125. */
  126. static inline struct reservation_object_list *
  127. reservation_object_get_list(struct reservation_object *obj)
  128. {
  129. return rcu_dereference_protected(obj->fence,
  130. reservation_object_held(obj));
  131. }
  132. /**
  133. * reservation_object_lock - lock the reservation object
  134. * @obj: the reservation object
  135. * @ctx: the locking context
  136. *
  137. * Locks the reservation object for exclusive access and modification. Note,
  138. * that the lock is only against other writers, readers will run concurrently
  139. * with a writer under RCU. The seqlock is used to notify readers if they
  140. * overlap with a writer.
  141. *
  142. * As the reservation object may be locked by multiple parties in an
  143. * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
  144. * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
  145. * object may be locked by itself by passing NULL as @ctx.
  146. */
  147. static inline int
  148. reservation_object_lock(struct reservation_object *obj,
  149. struct ww_acquire_ctx *ctx)
  150. {
  151. return ww_mutex_lock(&obj->lock, ctx);
  152. }
  153. /**
  154. * reservation_object_lock_interruptible - lock the reservation object
  155. * @obj: the reservation object
  156. * @ctx: the locking context
  157. *
  158. * Locks the reservation object interruptible for exclusive access and
  159. * modification. Note, that the lock is only against other writers, readers
  160. * will run concurrently with a writer under RCU. The seqlock is used to
  161. * notify readers if they overlap with a writer.
  162. *
  163. * As the reservation object may be locked by multiple parties in an
  164. * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
  165. * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
  166. * object may be locked by itself by passing NULL as @ctx.
  167. */
  168. static inline int
  169. reservation_object_lock_interruptible(struct reservation_object *obj,
  170. struct ww_acquire_ctx *ctx)
  171. {
  172. return ww_mutex_lock_interruptible(&obj->lock, ctx);
  173. }
  174. /**
  175. * reservation_object_trylock - trylock the reservation object
  176. * @obj: the reservation object
  177. *
  178. * Tries to lock the reservation object for exclusive access and modification.
  179. * Note, that the lock is only against other writers, readers will run
  180. * concurrently with a writer under RCU. The seqlock is used to notify readers
  181. * if they overlap with a writer.
  182. *
  183. * Also note that since no context is provided, no deadlock protection is
  184. * possible.
  185. *
  186. * Returns true if the lock was acquired, false otherwise.
  187. */
  188. static inline bool __must_check
  189. reservation_object_trylock(struct reservation_object *obj)
  190. {
  191. return ww_mutex_trylock(&obj->lock);
  192. }
  193. /**
  194. * reservation_object_unlock - unlock the reservation object
  195. * @obj: the reservation object
  196. *
  197. * Unlocks the reservation object following exclusive access.
  198. */
  199. static inline void
  200. reservation_object_unlock(struct reservation_object *obj)
  201. {
  202. ww_mutex_unlock(&obj->lock);
  203. }
  204. /**
  205. * reservation_object_get_excl - get the reservation object's
  206. * exclusive fence, with update-side lock held
  207. * @obj: the reservation object
  208. *
  209. * Returns the exclusive fence (if any). Does NOT take a
  210. * reference. The obj->lock must be held.
  211. *
  212. * RETURNS
  213. * The exclusive fence or NULL
  214. */
  215. static inline struct dma_fence *
  216. reservation_object_get_excl(struct reservation_object *obj)
  217. {
  218. return rcu_dereference_protected(obj->fence_excl,
  219. reservation_object_held(obj));
  220. }
  221. /**
  222. * reservation_object_get_excl_rcu - get the reservation object's
  223. * exclusive fence, without lock held.
  224. * @obj: the reservation object
  225. *
  226. * If there is an exclusive fence, this atomically increments it's
  227. * reference count and returns it.
  228. *
  229. * RETURNS
  230. * The exclusive fence or NULL if none
  231. */
  232. static inline struct dma_fence *
  233. reservation_object_get_excl_rcu(struct reservation_object *obj)
  234. {
  235. struct dma_fence *fence;
  236. if (!rcu_access_pointer(obj->fence_excl))
  237. return NULL;
  238. rcu_read_lock();
  239. fence = dma_fence_get_rcu_safe(&obj->fence_excl);
  240. rcu_read_unlock();
  241. return fence;
  242. }
  243. int reservation_object_reserve_shared(struct reservation_object *obj);
  244. void reservation_object_add_shared_fence(struct reservation_object *obj,
  245. struct dma_fence *fence);
  246. void reservation_object_add_excl_fence(struct reservation_object *obj,
  247. struct dma_fence *fence);
  248. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  249. struct dma_fence **pfence_excl,
  250. unsigned *pshared_count,
  251. struct dma_fence ***pshared);
  252. int reservation_object_copy_fences(struct reservation_object *dst,
  253. struct reservation_object *src);
  254. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  255. bool wait_all, bool intr,
  256. unsigned long timeout);
  257. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  258. bool test_all);
  259. #endif /* _LINUX_RESERVATION_H */