reservation.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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/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 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 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 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. fence_put(excl);
  109. fobj = rcu_dereference_protected(obj->fence, 1);
  110. if (fobj) {
  111. for (i = 0; i < fobj->shared_count; ++i)
  112. 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_get_excl - get the reservation object's
  134. * exclusive fence, with update-side lock held
  135. * @obj: the reservation object
  136. *
  137. * Returns the exclusive fence (if any). Does NOT take a
  138. * reference. The obj->lock must be held.
  139. *
  140. * RETURNS
  141. * The exclusive fence or NULL
  142. */
  143. static inline struct fence *
  144. reservation_object_get_excl(struct reservation_object *obj)
  145. {
  146. return rcu_dereference_protected(obj->fence_excl,
  147. reservation_object_held(obj));
  148. }
  149. /**
  150. * reservation_object_get_excl_rcu - get the reservation object's
  151. * exclusive fence, without lock held.
  152. * @obj: the reservation object
  153. *
  154. * If there is an exclusive fence, this atomically increments it's
  155. * reference count and returns it.
  156. *
  157. * RETURNS
  158. * The exclusive fence or NULL if none
  159. */
  160. static inline struct fence *
  161. reservation_object_get_excl_rcu(struct reservation_object *obj)
  162. {
  163. struct fence *fence;
  164. unsigned seq;
  165. retry:
  166. seq = read_seqcount_begin(&obj->seq);
  167. rcu_read_lock();
  168. fence = rcu_dereference(obj->fence_excl);
  169. if (read_seqcount_retry(&obj->seq, seq)) {
  170. rcu_read_unlock();
  171. goto retry;
  172. }
  173. fence = fence_get(fence);
  174. rcu_read_unlock();
  175. return fence;
  176. }
  177. int reservation_object_reserve_shared(struct reservation_object *obj);
  178. void reservation_object_add_shared_fence(struct reservation_object *obj,
  179. struct fence *fence);
  180. void reservation_object_add_excl_fence(struct reservation_object *obj,
  181. struct fence *fence);
  182. int reservation_object_get_fences_rcu(struct reservation_object *obj,
  183. struct fence **pfence_excl,
  184. unsigned *pshared_count,
  185. struct fence ***pshared);
  186. long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
  187. bool wait_all, bool intr,
  188. unsigned long timeout);
  189. bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
  190. bool test_all);
  191. #endif /* _LINUX_RESERVATION_H */