drm_sync_helper.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * drm_sync_helper.h: software fence and helper functions for fences and
  3. * reservations used for dma buffer access synchronization between drivers.
  4. *
  5. * Copyright 2014 Google, Inc.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #ifndef _DRM_SYNC_HELPER_H_
  17. #define _DRM_SYNC_HELPER_H_
  18. #include <linux/fence.h>
  19. #include <linux/reservation.h>
  20. #include <linux/atomic.h>
  21. #include <linux/workqueue.h>
  22. /**
  23. * Create software fence
  24. * @context: execution context
  25. * @seqno: the sequence number of this fence inside the execution context
  26. */
  27. struct fence *drm_sw_fence_new(unsigned int context,
  28. unsigned seqno);
  29. /**
  30. * Signal and decrease reference count for a fence if it exists
  31. * @fence: fence to signal
  32. *
  33. * Utility function called when owner access to object associated with fence is
  34. * finished (e.g. GPU done with rendering).
  35. */
  36. static inline void drm_fence_signal_and_put(struct fence **fence)
  37. {
  38. if (*fence) {
  39. fence_signal(*fence);
  40. fence_put(*fence);
  41. *fence = NULL;
  42. }
  43. }
  44. struct drm_reservation_cb;
  45. struct drm_reservation_fence_cb {
  46. struct fence_cb base;
  47. struct drm_reservation_cb *parent;
  48. struct fence *fence;
  49. };
  50. /**
  51. * Callback executed when all fences in reservation callback are signaled
  52. * @rcb: reservation callback structure
  53. * @context: context provided by user at init time
  54. */
  55. typedef void (*drm_reservation_cb_func_t)(struct drm_reservation_cb *rcb,
  56. void *context);
  57. /**
  58. * Reservation callback structure
  59. * @work: work context in which func is executed
  60. * @fence_cbs: fence callbacks array
  61. * @num_fence_cbs: number of fence callbacks
  62. * @count: count of signaled fences, when it drops to 0 func is called
  63. * @func: callback to execute when all fences are signaled
  64. * @context: context provided by user during initialization
  65. *
  66. * It is safe and expected that func will destroy this structure before
  67. * returning.
  68. */
  69. struct drm_reservation_cb {
  70. struct work_struct work;
  71. struct drm_reservation_fence_cb **fence_cbs;
  72. unsigned num_fence_cbs;
  73. atomic_t count;
  74. void *context;
  75. drm_reservation_cb_func_t func;
  76. };
  77. /**
  78. * Initialize reservation callback
  79. * @rcb: reservation callback structure to initialize
  80. * @func: function to call when all fences are signaled
  81. * @context: parameter to call func with
  82. */
  83. void drm_reservation_cb_init(struct drm_reservation_cb *rcb,
  84. drm_reservation_cb_func_t func,
  85. void *context);
  86. /**
  87. * Add fences from reservation object to callback
  88. * @rcb: reservation callback structure
  89. * @resv: reservation object
  90. * @exclusive: (for exclusive wait) when true add all fences, otherwise only
  91. * exclusive fence
  92. */
  93. int drm_reservation_cb_add(struct drm_reservation_cb *rcb,
  94. struct reservation_object *resv,
  95. bool exclusive);
  96. /**
  97. * Finish adding fences
  98. * @rcb: reservation callback structure
  99. *
  100. * It will trigger callback worker if all fences were signaled before.
  101. */
  102. void drm_reservation_cb_done(struct drm_reservation_cb *rcb);
  103. /**
  104. * Cleanup reservation callback structure
  105. * @rcb: reservation callback structure
  106. *
  107. * Can be called to cancel primed reservation callback.
  108. */
  109. void drm_reservation_cb_fini(struct drm_reservation_cb *rcb);
  110. /**
  111. * Add reservation to array of reservations
  112. * @resv: reservation to add
  113. * @resvs: array of reservations
  114. * @excl_resvs_bitmap: bitmap for exclusive reservations
  115. * @num_resvs: number of reservations in array
  116. * @exclusive: bool to store in excl_resvs_bitmap
  117. */
  118. void
  119. drm_add_reservation(struct reservation_object *resv,
  120. struct reservation_object **resvs,
  121. unsigned long *excl_resvs_bitmap,
  122. unsigned int *num_resvs, bool exclusive);
  123. /**
  124. * Acquire ww_mutex lock on all reservations in the array
  125. * @resvs: array of reservations
  126. * @num_resvs: number of reservations in the array
  127. * @ctx: ww mutex context
  128. */
  129. int drm_lock_reservations(struct reservation_object **resvs,
  130. unsigned int num_resvs, struct ww_acquire_ctx *ctx);
  131. /**
  132. * Release ww_mutex lock on all reservations in the array
  133. * @resvs: array of reservations
  134. * @num_resvs: number of reservations in the array
  135. * @ctx: ww mutex context
  136. */
  137. void drm_unlock_reservations(struct reservation_object **resvs,
  138. unsigned int num_resvs,
  139. struct ww_acquire_ctx *ctx);
  140. #endif