dma-fence-array.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dma-fence-array: aggregate fences to be waited together
  4. *
  5. * Copyright (C) 2016 Collabora Ltd
  6. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  7. * Authors:
  8. * Gustavo Padovan <gustavo@padovan.org>
  9. * Christian König <christian.koenig@amd.com>
  10. */
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/dma-fence-array.h>
  14. #define PENDING_ERROR 1
  15. static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
  16. {
  17. return "dma_fence_array";
  18. }
  19. static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
  20. {
  21. return "unbound";
  22. }
  23. static void dma_fence_array_set_pending_error(struct dma_fence_array *array,
  24. int error)
  25. {
  26. /*
  27. * Propagate the first error reported by any of our fences, but only
  28. * before we ourselves are signaled.
  29. */
  30. if (error)
  31. cmpxchg(&array->base.error, PENDING_ERROR, error);
  32. }
  33. static void dma_fence_array_clear_pending_error(struct dma_fence_array *array)
  34. {
  35. /* Clear the error flag if not actually set. */
  36. cmpxchg(&array->base.error, PENDING_ERROR, 0);
  37. }
  38. static void irq_dma_fence_array_work(struct irq_work *wrk)
  39. {
  40. struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
  41. dma_fence_array_clear_pending_error(array);
  42. dma_fence_signal(&array->base);
  43. dma_fence_put(&array->base);
  44. }
  45. static void dma_fence_array_cb_func(struct dma_fence *f,
  46. struct dma_fence_cb *cb)
  47. {
  48. struct dma_fence_array_cb *array_cb =
  49. container_of(cb, struct dma_fence_array_cb, cb);
  50. struct dma_fence_array *array = array_cb->array;
  51. dma_fence_array_set_pending_error(array, f->error);
  52. if (atomic_dec_and_test(&array->num_pending))
  53. irq_work_queue(&array->work);
  54. else
  55. dma_fence_put(&array->base);
  56. }
  57. static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
  58. {
  59. struct dma_fence_array *array = to_dma_fence_array(fence);
  60. struct dma_fence_array_cb *cb = array->callbacks;
  61. unsigned i;
  62. for (i = 0; i < array->num_fences; ++i) {
  63. cb[i].array = array;
  64. /*
  65. * As we may report that the fence is signaled before all
  66. * callbacks are complete, we need to take an additional
  67. * reference count on the array so that we do not free it too
  68. * early. The core fence handling will only hold the reference
  69. * until we signal the array as complete (but that is now
  70. * insufficient).
  71. */
  72. dma_fence_get(&array->base);
  73. if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
  74. dma_fence_array_cb_func)) {
  75. int error = array->fences[i]->error;
  76. dma_fence_array_set_pending_error(array, error);
  77. dma_fence_put(&array->base);
  78. if (atomic_dec_and_test(&array->num_pending)) {
  79. dma_fence_array_clear_pending_error(array);
  80. return false;
  81. }
  82. }
  83. }
  84. return true;
  85. }
  86. static bool dma_fence_array_signaled(struct dma_fence *fence)
  87. {
  88. struct dma_fence_array *array = to_dma_fence_array(fence);
  89. int num_pending;
  90. unsigned int i;
  91. /*
  92. * We need to read num_pending before checking the enable_signal bit
  93. * to avoid racing with the enable_signaling() implementation, which
  94. * might decrement the counter, and cause a partial check.
  95. * atomic_read_acquire() pairs with atomic_dec_and_test() in
  96. * dma_fence_array_enable_signaling()
  97. *
  98. * The !--num_pending check is here to account for the any_signaled case
  99. * if we race with enable_signaling(), that means the !num_pending check
  100. * in the is_signalling_enabled branch might be outdated (num_pending
  101. * might have been decremented), but that's fine. The user will get the
  102. * right value when testing again later.
  103. */
  104. num_pending = atomic_read_acquire(&array->num_pending);
  105. if (test_bit(DMA_FENCE_FLAG_ENABLE_SIGNAL_BIT, &array->base.flags)) {
  106. if (num_pending <= 0)
  107. goto signal;
  108. return false;
  109. }
  110. for (i = 0; i < array->num_fences; ++i) {
  111. if (dma_fence_is_signaled(array->fences[i]) && !--num_pending)
  112. goto signal;
  113. }
  114. return false;
  115. signal:
  116. dma_fence_array_clear_pending_error(array);
  117. return true;
  118. }
  119. static void dma_fence_array_release(struct dma_fence *fence)
  120. {
  121. struct dma_fence_array *array = to_dma_fence_array(fence);
  122. unsigned i;
  123. for (i = 0; i < array->num_fences; ++i)
  124. dma_fence_put(array->fences[i]);
  125. kfree(array->fences);
  126. dma_fence_free(fence);
  127. }
  128. static void dma_fence_array_set_deadline(struct dma_fence *fence,
  129. ktime_t deadline)
  130. {
  131. struct dma_fence_array *array = to_dma_fence_array(fence);
  132. unsigned i;
  133. for (i = 0; i < array->num_fences; ++i)
  134. dma_fence_set_deadline(array->fences[i], deadline);
  135. }
  136. const struct dma_fence_ops dma_fence_array_ops = {
  137. .get_driver_name = dma_fence_array_get_driver_name,
  138. .get_timeline_name = dma_fence_array_get_timeline_name,
  139. .enable_signaling = dma_fence_array_enable_signaling,
  140. .signaled = dma_fence_array_signaled,
  141. .release = dma_fence_array_release,
  142. .set_deadline = dma_fence_array_set_deadline,
  143. };
  144. EXPORT_SYMBOL(dma_fence_array_ops);
  145. /**
  146. * dma_fence_array_alloc - Allocate a custom fence array
  147. * @num_fences: [in] number of fences to add in the array
  148. *
  149. * Return dma fence array on success, NULL on failure
  150. */
  151. struct dma_fence_array *dma_fence_array_alloc(int num_fences)
  152. {
  153. struct dma_fence_array *array;
  154. return kzalloc_flex(*array, callbacks, num_fences);
  155. }
  156. EXPORT_SYMBOL(dma_fence_array_alloc);
  157. /**
  158. * dma_fence_array_init - Init a custom fence array
  159. * @array: [in] dma fence array to arm
  160. * @num_fences: [in] number of fences to add in the array
  161. * @fences: [in] array containing the fences
  162. * @context: [in] fence context to use
  163. * @seqno: [in] sequence number to use
  164. * @signal_on_any: [in] signal on any fence in the array
  165. *
  166. * Implementation of @dma_fence_array_create without allocation. Useful to init
  167. * a preallocated dma fence array in the path of reclaim or dma fence signaling.
  168. */
  169. void dma_fence_array_init(struct dma_fence_array *array,
  170. int num_fences, struct dma_fence **fences,
  171. u64 context, unsigned seqno,
  172. bool signal_on_any)
  173. {
  174. WARN_ON(!num_fences || !fences);
  175. array->num_fences = num_fences;
  176. spin_lock_init(&array->lock);
  177. dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
  178. context, seqno);
  179. init_irq_work(&array->work, irq_dma_fence_array_work);
  180. atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
  181. array->fences = fences;
  182. array->base.error = PENDING_ERROR;
  183. /*
  184. * dma_fence_array objects should never contain any other fence
  185. * containers or otherwise we run into recursion and potential kernel
  186. * stack overflow on operations on the dma_fence_array.
  187. *
  188. * The correct way of handling this is to flatten out the array by the
  189. * caller instead.
  190. *
  191. * Enforce this here by checking that we don't create a dma_fence_array
  192. * with any container inside.
  193. */
  194. while (num_fences--)
  195. WARN_ON(dma_fence_is_container(fences[num_fences]));
  196. }
  197. EXPORT_SYMBOL(dma_fence_array_init);
  198. /**
  199. * dma_fence_array_create - Create a custom fence array
  200. * @num_fences: [in] number of fences to add in the array
  201. * @fences: [in] array containing the fences
  202. * @context: [in] fence context to use
  203. * @seqno: [in] sequence number to use
  204. * @signal_on_any: [in] signal on any fence in the array
  205. *
  206. * Allocate a dma_fence_array object and initialize the base fence with
  207. * dma_fence_init().
  208. * In case of error it returns NULL.
  209. *
  210. * The caller should allocate the fences array with num_fences size
  211. * and fill it with the fences it wants to add to the object. Ownership of this
  212. * array is taken and dma_fence_put() is used on each fence on release.
  213. *
  214. * If @signal_on_any is true the fence array signals if any fence in the array
  215. * signals, otherwise it signals when all fences in the array signal.
  216. */
  217. struct dma_fence_array *dma_fence_array_create(int num_fences,
  218. struct dma_fence **fences,
  219. u64 context, unsigned seqno,
  220. bool signal_on_any)
  221. {
  222. struct dma_fence_array *array;
  223. array = dma_fence_array_alloc(num_fences);
  224. if (!array)
  225. return NULL;
  226. dma_fence_array_init(array, num_fences, fences,
  227. context, seqno, signal_on_any);
  228. return array;
  229. }
  230. EXPORT_SYMBOL(dma_fence_array_create);
  231. /**
  232. * dma_fence_match_context - Check if all fences are from the given context
  233. * @fence: [in] fence or fence array
  234. * @context: [in] fence context to check all fences against
  235. *
  236. * Checks the provided fence or, for a fence array, all fences in the array
  237. * against the given context. Returns false if any fence is from a different
  238. * context.
  239. */
  240. bool dma_fence_match_context(struct dma_fence *fence, u64 context)
  241. {
  242. struct dma_fence_array *array = to_dma_fence_array(fence);
  243. unsigned i;
  244. if (!dma_fence_is_array(fence))
  245. return fence->context == context;
  246. for (i = 0; i < array->num_fences; i++) {
  247. if (array->fences[i]->context != context)
  248. return false;
  249. }
  250. return true;
  251. }
  252. EXPORT_SYMBOL(dma_fence_match_context);
  253. struct dma_fence *dma_fence_array_first(struct dma_fence *head)
  254. {
  255. struct dma_fence_array *array;
  256. if (!head)
  257. return NULL;
  258. array = to_dma_fence_array(head);
  259. if (!array)
  260. return head;
  261. if (!array->num_fences)
  262. return NULL;
  263. return array->fences[0];
  264. }
  265. EXPORT_SYMBOL(dma_fence_array_first);
  266. struct dma_fence *dma_fence_array_next(struct dma_fence *head,
  267. unsigned int index)
  268. {
  269. struct dma_fence_array *array = to_dma_fence_array(head);
  270. if (!array || index >= array->num_fences)
  271. return NULL;
  272. return array->fences[index];
  273. }
  274. EXPORT_SYMBOL(dma_fence_array_next);