dma-fence-array.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * dma-fence-array: aggregate fences to be waited together
  3. *
  4. * Copyright (C) 2016 Collabora Ltd
  5. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  6. * Authors:
  7. * Gustavo Padovan <gustavo@padovan.org>
  8. * Christian König <christian.koenig@amd.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. */
  19. #include <linux/export.h>
  20. #include <linux/slab.h>
  21. #include <linux/dma-fence-array.h>
  22. static const char *dma_fence_array_get_driver_name(struct dma_fence *fence)
  23. {
  24. return "dma_fence_array";
  25. }
  26. static const char *dma_fence_array_get_timeline_name(struct dma_fence *fence)
  27. {
  28. return "unbound";
  29. }
  30. static void irq_dma_fence_array_work(struct irq_work *wrk)
  31. {
  32. struct dma_fence_array *array = container_of(wrk, typeof(*array), work);
  33. dma_fence_signal(&array->base);
  34. dma_fence_put(&array->base);
  35. }
  36. static void dma_fence_array_cb_func(struct dma_fence *f,
  37. struct dma_fence_cb *cb)
  38. {
  39. struct dma_fence_array_cb *array_cb =
  40. container_of(cb, struct dma_fence_array_cb, cb);
  41. struct dma_fence_array *array = array_cb->array;
  42. if (atomic_dec_and_test(&array->num_pending))
  43. irq_work_queue(&array->work);
  44. else
  45. dma_fence_put(&array->base);
  46. }
  47. static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
  48. {
  49. struct dma_fence_array *array = to_dma_fence_array(fence);
  50. struct dma_fence_array_cb *cb = (void *)(&array[1]);
  51. unsigned i;
  52. for (i = 0; i < array->num_fences; ++i) {
  53. cb[i].array = array;
  54. /*
  55. * As we may report that the fence is signaled before all
  56. * callbacks are complete, we need to take an additional
  57. * reference count on the array so that we do not free it too
  58. * early. The core fence handling will only hold the reference
  59. * until we signal the array as complete (but that is now
  60. * insufficient).
  61. */
  62. dma_fence_get(&array->base);
  63. if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
  64. dma_fence_array_cb_func)) {
  65. dma_fence_put(&array->base);
  66. if (atomic_dec_and_test(&array->num_pending))
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. static bool dma_fence_array_signaled(struct dma_fence *fence)
  73. {
  74. struct dma_fence_array *array = to_dma_fence_array(fence);
  75. return atomic_read(&array->num_pending) <= 0;
  76. }
  77. static void dma_fence_array_release(struct dma_fence *fence)
  78. {
  79. struct dma_fence_array *array = to_dma_fence_array(fence);
  80. unsigned i;
  81. for (i = 0; i < array->num_fences; ++i)
  82. dma_fence_put(array->fences[i]);
  83. kfree(array->fences);
  84. dma_fence_free(fence);
  85. }
  86. const struct dma_fence_ops dma_fence_array_ops = {
  87. .get_driver_name = dma_fence_array_get_driver_name,
  88. .get_timeline_name = dma_fence_array_get_timeline_name,
  89. .enable_signaling = dma_fence_array_enable_signaling,
  90. .signaled = dma_fence_array_signaled,
  91. .wait = dma_fence_default_wait,
  92. .release = dma_fence_array_release,
  93. };
  94. EXPORT_SYMBOL(dma_fence_array_ops);
  95. /**
  96. * dma_fence_array_create - Create a custom fence array
  97. * @num_fences: [in] number of fences to add in the array
  98. * @fences: [in] array containing the fences
  99. * @context: [in] fence context to use
  100. * @seqno: [in] sequence number to use
  101. * @signal_on_any: [in] signal on any fence in the array
  102. *
  103. * Allocate a dma_fence_array object and initialize the base fence with
  104. * dma_fence_init().
  105. * In case of error it returns NULL.
  106. *
  107. * The caller should allocate the fences array with num_fences size
  108. * and fill it with the fences it wants to add to the object. Ownership of this
  109. * array is taken and dma_fence_put() is used on each fence on release.
  110. *
  111. * If @signal_on_any is true the fence array signals if any fence in the array
  112. * signals, otherwise it signals when all fences in the array signal.
  113. */
  114. struct dma_fence_array *dma_fence_array_create(int num_fences,
  115. struct dma_fence **fences,
  116. u64 context, unsigned seqno,
  117. bool signal_on_any)
  118. {
  119. struct dma_fence_array *array;
  120. size_t size = sizeof(*array);
  121. /* Allocate the callback structures behind the array. */
  122. size += num_fences * sizeof(struct dma_fence_array_cb);
  123. array = kzalloc(size, GFP_KERNEL);
  124. if (!array)
  125. return NULL;
  126. spin_lock_init(&array->lock);
  127. dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
  128. context, seqno);
  129. init_irq_work(&array->work, irq_dma_fence_array_work);
  130. array->num_fences = num_fences;
  131. atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
  132. array->fences = fences;
  133. return array;
  134. }
  135. EXPORT_SYMBOL(dma_fence_array_create);
  136. /**
  137. * dma_fence_match_context - Check if all fences are from the given context
  138. * @fence: [in] fence or fence array
  139. * @context: [in] fence context to check all fences against
  140. *
  141. * Checks the provided fence or, for a fence array, all fences in the array
  142. * against the given context. Returns false if any fence is from a different
  143. * context.
  144. */
  145. bool dma_fence_match_context(struct dma_fence *fence, u64 context)
  146. {
  147. struct dma_fence_array *array = to_dma_fence_array(fence);
  148. unsigned i;
  149. if (!dma_fence_is_array(fence))
  150. return fence->context == context;
  151. for (i = 0; i < array->num_fences; i++) {
  152. if (array->fences[i]->context != context)
  153. return false;
  154. }
  155. return true;
  156. }
  157. EXPORT_SYMBOL(dma_fence_match_context);