dma-fence-array.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 dma_fence_array_cb_func(struct dma_fence *f,
  31. struct dma_fence_cb *cb)
  32. {
  33. struct dma_fence_array_cb *array_cb =
  34. container_of(cb, struct dma_fence_array_cb, cb);
  35. struct dma_fence_array *array = array_cb->array;
  36. if (atomic_dec_and_test(&array->num_pending))
  37. dma_fence_signal(&array->base);
  38. dma_fence_put(&array->base);
  39. }
  40. static bool dma_fence_array_enable_signaling(struct dma_fence *fence)
  41. {
  42. struct dma_fence_array *array = to_dma_fence_array(fence);
  43. struct dma_fence_array_cb *cb = (void *)(&array[1]);
  44. unsigned i;
  45. for (i = 0; i < array->num_fences; ++i) {
  46. cb[i].array = array;
  47. /*
  48. * As we may report that the fence is signaled before all
  49. * callbacks are complete, we need to take an additional
  50. * reference count on the array so that we do not free it too
  51. * early. The core fence handling will only hold the reference
  52. * until we signal the array as complete (but that is now
  53. * insufficient).
  54. */
  55. dma_fence_get(&array->base);
  56. if (dma_fence_add_callback(array->fences[i], &cb[i].cb,
  57. dma_fence_array_cb_func)) {
  58. dma_fence_put(&array->base);
  59. if (atomic_dec_and_test(&array->num_pending))
  60. return false;
  61. }
  62. }
  63. return true;
  64. }
  65. static bool dma_fence_array_signaled(struct dma_fence *fence)
  66. {
  67. struct dma_fence_array *array = to_dma_fence_array(fence);
  68. return atomic_read(&array->num_pending) <= 0;
  69. }
  70. static void dma_fence_array_release(struct dma_fence *fence)
  71. {
  72. struct dma_fence_array *array = to_dma_fence_array(fence);
  73. unsigned i;
  74. for (i = 0; i < array->num_fences; ++i)
  75. dma_fence_put(array->fences[i]);
  76. kfree(array->fences);
  77. dma_fence_free(fence);
  78. }
  79. const struct dma_fence_ops dma_fence_array_ops = {
  80. .get_driver_name = dma_fence_array_get_driver_name,
  81. .get_timeline_name = dma_fence_array_get_timeline_name,
  82. .enable_signaling = dma_fence_array_enable_signaling,
  83. .signaled = dma_fence_array_signaled,
  84. .wait = dma_fence_default_wait,
  85. .release = dma_fence_array_release,
  86. };
  87. EXPORT_SYMBOL(dma_fence_array_ops);
  88. /**
  89. * dma_fence_array_create - Create a custom fence array
  90. * @num_fences: [in] number of fences to add in the array
  91. * @fences: [in] array containing the fences
  92. * @context: [in] fence context to use
  93. * @seqno: [in] sequence number to use
  94. * @signal_on_any: [in] signal on any fence in the array
  95. *
  96. * Allocate a dma_fence_array object and initialize the base fence with
  97. * dma_fence_init().
  98. * In case of error it returns NULL.
  99. *
  100. * The caller should allocate the fences array with num_fences size
  101. * and fill it with the fences it wants to add to the object. Ownership of this
  102. * array is taken and dma_fence_put() is used on each fence on release.
  103. *
  104. * If @signal_on_any is true the fence array signals if any fence in the array
  105. * signals, otherwise it signals when all fences in the array signal.
  106. */
  107. struct dma_fence_array *dma_fence_array_create(int num_fences,
  108. struct dma_fence **fences,
  109. u64 context, unsigned seqno,
  110. bool signal_on_any)
  111. {
  112. struct dma_fence_array *array;
  113. size_t size = sizeof(*array);
  114. /* Allocate the callback structures behind the array. */
  115. size += num_fences * sizeof(struct dma_fence_array_cb);
  116. array = kzalloc(size, GFP_KERNEL);
  117. if (!array)
  118. return NULL;
  119. spin_lock_init(&array->lock);
  120. dma_fence_init(&array->base, &dma_fence_array_ops, &array->lock,
  121. context, seqno);
  122. array->num_fences = num_fences;
  123. atomic_set(&array->num_pending, signal_on_any ? 1 : num_fences);
  124. array->fences = fences;
  125. return array;
  126. }
  127. EXPORT_SYMBOL(dma_fence_array_create);