drm_exec.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. #ifndef __DRM_EXEC_H__
  3. #define __DRM_EXEC_H__
  4. #include <linux/compiler.h>
  5. #include <linux/ww_mutex.h>
  6. #define DRM_EXEC_INTERRUPTIBLE_WAIT BIT(0)
  7. #define DRM_EXEC_IGNORE_DUPLICATES BIT(1)
  8. struct drm_gem_object;
  9. /**
  10. * struct drm_exec - Execution context
  11. */
  12. struct drm_exec {
  13. /**
  14. * @flags: Flags to control locking behavior
  15. */
  16. u32 flags;
  17. /**
  18. * @ticket: WW ticket used for acquiring locks
  19. */
  20. struct ww_acquire_ctx ticket;
  21. /**
  22. * @num_objects: number of objects locked
  23. */
  24. unsigned int num_objects;
  25. /**
  26. * @max_objects: maximum objects in array
  27. */
  28. unsigned int max_objects;
  29. /**
  30. * @objects: array of the locked objects
  31. */
  32. struct drm_gem_object **objects;
  33. /**
  34. * @contended: contended GEM object we backed off for
  35. */
  36. struct drm_gem_object *contended;
  37. /**
  38. * @prelocked: already locked GEM object due to contention
  39. */
  40. struct drm_gem_object *prelocked;
  41. };
  42. /**
  43. * drm_exec_obj() - Return the object for a give drm_exec index
  44. * @exec: Pointer to the drm_exec context
  45. * @index: The index.
  46. *
  47. * Return: Pointer to the locked object corresponding to @index if
  48. * index is within the number of locked objects. NULL otherwise.
  49. */
  50. static inline struct drm_gem_object *
  51. drm_exec_obj(struct drm_exec *exec, unsigned long index)
  52. {
  53. return index < exec->num_objects ? exec->objects[index] : NULL;
  54. }
  55. /**
  56. * drm_exec_for_each_locked_object - iterate over all the locked objects
  57. * @exec: drm_exec object
  58. * @index: unsigned long index for the iteration
  59. * @obj: the current GEM object
  60. *
  61. * Iterate over all the locked GEM objects inside the drm_exec object.
  62. */
  63. #define drm_exec_for_each_locked_object(exec, index, obj) \
  64. for ((index) = 0; ((obj) = drm_exec_obj(exec, index)); ++(index))
  65. /**
  66. * drm_exec_for_each_locked_object_reverse - iterate over all the locked
  67. * objects in reverse locking order
  68. * @exec: drm_exec object
  69. * @index: unsigned long index for the iteration
  70. * @obj: the current GEM object
  71. *
  72. * Iterate over all the locked GEM objects inside the drm_exec object in
  73. * reverse locking order. Note that @index may go below zero and wrap,
  74. * but that will be caught by drm_exec_obj(), returning a NULL object.
  75. */
  76. #define drm_exec_for_each_locked_object_reverse(exec, index, obj) \
  77. for ((index) = (exec)->num_objects - 1; \
  78. ((obj) = drm_exec_obj(exec, index)); --(index))
  79. /**
  80. * drm_exec_until_all_locked - loop until all GEM objects are locked
  81. * @exec: drm_exec object
  82. *
  83. * Core functionality of the drm_exec object. Loops until all GEM objects are
  84. * locked and no more contention exists. At the beginning of the loop it is
  85. * guaranteed that no GEM object is locked.
  86. *
  87. * Since labels can't be defined local to the loops body we use a jump pointer
  88. * to make sure that the retry is only used from within the loops body.
  89. */
  90. #define drm_exec_until_all_locked(exec) \
  91. __PASTE(__drm_exec_, __LINE__): \
  92. for (void *__drm_exec_retry_ptr; ({ \
  93. __drm_exec_retry_ptr = &&__PASTE(__drm_exec_, __LINE__);\
  94. (void)__drm_exec_retry_ptr; \
  95. drm_exec_cleanup(exec); \
  96. });)
  97. /**
  98. * drm_exec_retry_on_contention - restart the loop to grap all locks
  99. * @exec: drm_exec object
  100. *
  101. * Control flow helper to continue when a contention was detected and we need to
  102. * clean up and re-start the loop to prepare all GEM objects.
  103. */
  104. #define drm_exec_retry_on_contention(exec) \
  105. do { \
  106. if (unlikely(drm_exec_is_contended(exec))) \
  107. goto *__drm_exec_retry_ptr; \
  108. } while (0)
  109. /**
  110. * drm_exec_is_contended - check for contention
  111. * @exec: drm_exec object
  112. *
  113. * Returns true if the drm_exec object has run into some contention while
  114. * locking a GEM object and needs to clean up.
  115. */
  116. static inline bool drm_exec_is_contended(struct drm_exec *exec)
  117. {
  118. return !!exec->contended;
  119. }
  120. void drm_exec_init(struct drm_exec *exec, u32 flags, unsigned nr);
  121. void drm_exec_fini(struct drm_exec *exec);
  122. bool drm_exec_cleanup(struct drm_exec *exec);
  123. int drm_exec_lock_obj(struct drm_exec *exec, struct drm_gem_object *obj);
  124. void drm_exec_unlock_obj(struct drm_exec *exec, struct drm_gem_object *obj);
  125. int drm_exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
  126. unsigned int num_fences);
  127. int drm_exec_prepare_array(struct drm_exec *exec,
  128. struct drm_gem_object **objects,
  129. unsigned int num_objects,
  130. unsigned int num_fences);
  131. #endif