dma-resv.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /**
  48. * struct dma_resv_list - a list of shared fences
  49. * @rcu: for internal use
  50. * @shared_count: table of shared fences
  51. * @shared_max: for growing shared fence table
  52. * @shared: shared fence table
  53. */
  54. struct dma_resv_list {
  55. struct rcu_head rcu;
  56. u32 shared_count, shared_max;
  57. struct dma_fence __rcu *shared[];
  58. };
  59. /**
  60. * struct dma_resv - a reservation object manages fences for a buffer
  61. * @lock: update side lock
  62. * @seq: sequence count for managing RCU read-side synchronization
  63. * @fence_excl: the exclusive fence, if there is one currently
  64. * @fence: list of current shared fences
  65. */
  66. struct dma_resv {
  67. struct ww_mutex lock;
  68. seqcount_ww_mutex_t seq;
  69. struct dma_fence __rcu *fence_excl;
  70. struct dma_resv_list __rcu *fence;
  71. };
  72. #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
  73. #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
  74. /**
  75. * dma_resv_get_list - get the reservation object's
  76. * shared fence list, with update-side lock held
  77. * @obj: the reservation object
  78. *
  79. * Returns the shared fence list. Does NOT take references to
  80. * the fence. The obj->lock must be held.
  81. */
  82. static inline struct dma_resv_list *dma_resv_get_list(struct dma_resv *obj)
  83. {
  84. return rcu_dereference_protected(obj->fence,
  85. dma_resv_held(obj));
  86. }
  87. /**
  88. * dma_resv_lock - lock the reservation object
  89. * @obj: the reservation object
  90. * @ctx: the locking context
  91. *
  92. * Locks the reservation object for exclusive access and modification. Note,
  93. * that the lock is only against other writers, readers will run concurrently
  94. * with a writer under RCU. The seqlock is used to notify readers if they
  95. * overlap with a writer.
  96. *
  97. * As the reservation object may be locked by multiple parties in an
  98. * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
  99. * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
  100. * object may be locked by itself by passing NULL as @ctx.
  101. */
  102. static inline int dma_resv_lock(struct dma_resv *obj,
  103. struct ww_acquire_ctx *ctx)
  104. {
  105. return ww_mutex_lock(&obj->lock, ctx);
  106. }
  107. /**
  108. * dma_resv_lock_interruptible - lock the reservation object
  109. * @obj: the reservation object
  110. * @ctx: the locking context
  111. *
  112. * Locks the reservation object interruptible for exclusive access and
  113. * modification. Note, that the lock is only against other writers, readers
  114. * will run concurrently with a writer under RCU. The seqlock is used to
  115. * notify readers if they overlap with a writer.
  116. *
  117. * As the reservation object may be locked by multiple parties in an
  118. * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
  119. * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
  120. * object may be locked by itself by passing NULL as @ctx.
  121. */
  122. static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
  123. struct ww_acquire_ctx *ctx)
  124. {
  125. return ww_mutex_lock_interruptible(&obj->lock, ctx);
  126. }
  127. /**
  128. * dma_resv_lock_slow - slowpath lock the reservation object
  129. * @obj: the reservation object
  130. * @ctx: the locking context
  131. *
  132. * Acquires the reservation object after a die case. This function
  133. * will sleep until the lock becomes available. See dma_resv_lock() as
  134. * well.
  135. */
  136. static inline void dma_resv_lock_slow(struct dma_resv *obj,
  137. struct ww_acquire_ctx *ctx)
  138. {
  139. ww_mutex_lock_slow(&obj->lock, ctx);
  140. }
  141. /**
  142. * dma_resv_lock_slow_interruptible - slowpath lock the reservation
  143. * object, interruptible
  144. * @obj: the reservation object
  145. * @ctx: the locking context
  146. *
  147. * Acquires the reservation object interruptible after a die case. This function
  148. * will sleep until the lock becomes available. See
  149. * dma_resv_lock_interruptible() as well.
  150. */
  151. static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
  152. struct ww_acquire_ctx *ctx)
  153. {
  154. return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
  155. }
  156. /**
  157. * dma_resv_trylock - trylock the reservation object
  158. * @obj: the reservation object
  159. *
  160. * Tries to lock the reservation object for exclusive access and modification.
  161. * Note, that the lock is only against other writers, readers will run
  162. * concurrently with a writer under RCU. The seqlock is used to notify readers
  163. * if they overlap with a writer.
  164. *
  165. * Also note that since no context is provided, no deadlock protection is
  166. * possible.
  167. *
  168. * Returns true if the lock was acquired, false otherwise.
  169. */
  170. static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
  171. {
  172. return ww_mutex_trylock(&obj->lock);
  173. }
  174. /**
  175. * dma_resv_is_locked - is the reservation object locked
  176. * @obj: the reservation object
  177. *
  178. * Returns true if the mutex is locked, false if unlocked.
  179. */
  180. static inline bool dma_resv_is_locked(struct dma_resv *obj)
  181. {
  182. return ww_mutex_is_locked(&obj->lock);
  183. }
  184. /**
  185. * dma_resv_locking_ctx - returns the context used to lock the object
  186. * @obj: the reservation object
  187. *
  188. * Returns the context used to lock a reservation object or NULL if no context
  189. * was used or the object is not locked at all.
  190. */
  191. static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
  192. {
  193. return READ_ONCE(obj->lock.ctx);
  194. }
  195. /**
  196. * dma_resv_unlock - unlock the reservation object
  197. * @obj: the reservation object
  198. *
  199. * Unlocks the reservation object following exclusive access.
  200. */
  201. static inline void dma_resv_unlock(struct dma_resv *obj)
  202. {
  203. #ifdef CONFIG_DEBUG_MUTEXES
  204. /* Test shared fence slot reservation */
  205. if (rcu_access_pointer(obj->fence)) {
  206. struct dma_resv_list *fence = dma_resv_get_list(obj);
  207. fence->shared_max = fence->shared_count;
  208. }
  209. #endif
  210. ww_mutex_unlock(&obj->lock);
  211. }
  212. /**
  213. * dma_resv_get_excl - get the reservation object's
  214. * exclusive fence, with update-side lock held
  215. * @obj: the reservation object
  216. *
  217. * Returns the exclusive fence (if any). Does NOT take a
  218. * reference. Writers must hold obj->lock, readers may only
  219. * hold a RCU read side lock.
  220. *
  221. * RETURNS
  222. * The exclusive fence or NULL
  223. */
  224. static inline struct dma_fence *
  225. dma_resv_get_excl(struct dma_resv *obj)
  226. {
  227. return rcu_dereference_protected(obj->fence_excl,
  228. dma_resv_held(obj));
  229. }
  230. /**
  231. * dma_resv_get_excl_rcu - get the reservation object's
  232. * exclusive fence, without lock held.
  233. * @obj: the reservation object
  234. *
  235. * If there is an exclusive fence, this atomically increments it's
  236. * reference count and returns it.
  237. *
  238. * RETURNS
  239. * The exclusive fence or NULL if none
  240. */
  241. static inline struct dma_fence *
  242. dma_resv_get_excl_rcu(struct dma_resv *obj)
  243. {
  244. struct dma_fence *fence;
  245. if (!rcu_access_pointer(obj->fence_excl))
  246. return NULL;
  247. rcu_read_lock();
  248. fence = dma_fence_get_rcu_safe(&obj->fence_excl);
  249. rcu_read_unlock();
  250. return fence;
  251. }
  252. void dma_resv_init(struct dma_resv *obj);
  253. void dma_resv_fini(struct dma_resv *obj);
  254. int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
  255. void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
  256. void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
  257. int dma_resv_get_fences_rcu(struct dma_resv *obj,
  258. struct dma_fence **pfence_excl,
  259. unsigned *pshared_count,
  260. struct dma_fence ***pshared);
  261. int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
  262. long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr,
  263. unsigned long timeout);
  264. bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all);
  265. #endif /* _LINUX_RESERVATION_H */