drm_modeset_lock.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <[email protected]>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef DRM_MODESET_LOCK_H_
  24. #define DRM_MODESET_LOCK_H_
  25. #include <linux/types.h> /* stackdepot.h is not self-contained */
  26. #include <linux/stackdepot.h>
  27. #include <linux/ww_mutex.h>
  28. struct drm_modeset_lock;
  29. /**
  30. * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx)
  31. * @ww_ctx: base acquire ctx
  32. * @contended: used internally for -EDEADLK handling
  33. * @stack_depot: used internally for contention debugging
  34. * @locked: list of held locks
  35. * @trylock_only: trylock mode used in atomic contexts/panic notifiers
  36. * @interruptible: whether interruptible locking should be used.
  37. *
  38. * Each thread competing for a set of locks must use one acquire
  39. * ctx. And if any lock fxn returns -EDEADLK, it must backoff and
  40. * retry.
  41. */
  42. struct drm_modeset_acquire_ctx {
  43. struct ww_acquire_ctx ww_ctx;
  44. /*
  45. * Contended lock: if a lock is contended you should only call
  46. * drm_modeset_backoff() which drops locks and slow-locks the
  47. * contended lock.
  48. */
  49. struct drm_modeset_lock *contended;
  50. /*
  51. * Stack depot for debugging when a contended lock was not backed off
  52. * from.
  53. */
  54. depot_stack_handle_t stack_depot;
  55. /*
  56. * list of held locks (drm_modeset_lock)
  57. */
  58. struct list_head locked;
  59. /*
  60. * Trylock mode, use only for panic handlers!
  61. */
  62. bool trylock_only;
  63. /* Perform interruptible waits on this context. */
  64. bool interruptible;
  65. };
  66. /**
  67. * struct drm_modeset_lock - used for locking modeset resources.
  68. * @mutex: resource locking
  69. * @head: used to hold its place on &drm_atomi_state.locked list when
  70. * part of an atomic update
  71. *
  72. * Used for locking CRTCs and other modeset resources.
  73. */
  74. struct drm_modeset_lock {
  75. /*
  76. * modeset lock
  77. */
  78. struct ww_mutex mutex;
  79. /*
  80. * Resources that are locked as part of an atomic update are added
  81. * to a list (so we know what to unlock at the end).
  82. */
  83. struct list_head head;
  84. };
  85. #define DRM_MODESET_ACQUIRE_INTERRUPTIBLE BIT(0)
  86. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  87. uint32_t flags);
  88. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
  89. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
  90. int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
  91. void drm_modeset_lock_init(struct drm_modeset_lock *lock);
  92. /**
  93. * drm_modeset_lock_fini - cleanup lock
  94. * @lock: lock to cleanup
  95. */
  96. static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
  97. {
  98. WARN_ON(!list_empty(&lock->head));
  99. }
  100. /**
  101. * drm_modeset_is_locked - equivalent to mutex_is_locked()
  102. * @lock: lock to check
  103. */
  104. static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
  105. {
  106. return ww_mutex_is_locked(&lock->mutex);
  107. }
  108. /**
  109. * drm_modeset_lock_assert_held - equivalent to lockdep_assert_held()
  110. * @lock: lock to check
  111. */
  112. static inline void drm_modeset_lock_assert_held(struct drm_modeset_lock *lock)
  113. {
  114. lockdep_assert_held(&lock->mutex.base);
  115. }
  116. int drm_modeset_lock(struct drm_modeset_lock *lock,
  117. struct drm_modeset_acquire_ctx *ctx);
  118. int __must_check drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock);
  119. void drm_modeset_unlock(struct drm_modeset_lock *lock);
  120. struct drm_device;
  121. struct drm_crtc;
  122. struct drm_plane;
  123. void drm_modeset_lock_all(struct drm_device *dev);
  124. void drm_modeset_unlock_all(struct drm_device *dev);
  125. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
  126. int drm_modeset_lock_all_ctx(struct drm_device *dev,
  127. struct drm_modeset_acquire_ctx *ctx);
  128. /**
  129. * DRM_MODESET_LOCK_ALL_BEGIN - Helper to acquire modeset locks
  130. * @dev: drm device
  131. * @ctx: local modeset acquire context, will be dereferenced
  132. * @flags: DRM_MODESET_ACQUIRE_* flags to pass to drm_modeset_acquire_init()
  133. * @ret: local ret/err/etc variable to track error status
  134. *
  135. * Use these macros to simplify grabbing all modeset locks using a local
  136. * context. This has the advantage of reducing boilerplate, but also properly
  137. * checking return values where appropriate.
  138. *
  139. * Any code run between BEGIN and END will be holding the modeset locks.
  140. *
  141. * This must be paired with DRM_MODESET_LOCK_ALL_END(). We will jump back and
  142. * forth between the labels on deadlock and error conditions.
  143. *
  144. * Drivers can acquire additional modeset locks. If any lock acquisition
  145. * fails, the control flow needs to jump to DRM_MODESET_LOCK_ALL_END() with
  146. * the @ret parameter containing the return value of drm_modeset_lock().
  147. *
  148. * Returns:
  149. * The only possible value of ret immediately after DRM_MODESET_LOCK_ALL_BEGIN()
  150. * is 0, so no error checking is necessary
  151. */
  152. #define DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, flags, ret) \
  153. if (!drm_drv_uses_atomic_modeset(dev)) \
  154. mutex_lock(&dev->mode_config.mutex); \
  155. drm_modeset_acquire_init(&ctx, flags); \
  156. modeset_lock_retry: \
  157. ret = drm_modeset_lock_all_ctx(dev, &ctx); \
  158. if (ret) \
  159. goto modeset_lock_fail;
  160. /**
  161. * DRM_MODESET_LOCK_ALL_END - Helper to release and cleanup modeset locks
  162. * @dev: drm device
  163. * @ctx: local modeset acquire context, will be dereferenced
  164. * @ret: local ret/err/etc variable to track error status
  165. *
  166. * The other side of DRM_MODESET_LOCK_ALL_BEGIN(). It will bounce back to BEGIN
  167. * if ret is -EDEADLK.
  168. *
  169. * It's important that you use the same ret variable for begin and end so
  170. * deadlock conditions are properly handled.
  171. *
  172. * Returns:
  173. * ret will be untouched unless it is -EDEADLK on entry. That means that if you
  174. * successfully acquire the locks, ret will be whatever your code sets it to. If
  175. * there is a deadlock or other failure with acquire or backoff, ret will be set
  176. * to that failure. In both of these cases the code between BEGIN/END will not
  177. * be run, so the failure will reflect the inability to grab the locks.
  178. */
  179. #define DRM_MODESET_LOCK_ALL_END(dev, ctx, ret) \
  180. modeset_lock_fail: \
  181. if (ret == -EDEADLK) { \
  182. ret = drm_modeset_backoff(&ctx); \
  183. if (!ret) \
  184. goto modeset_lock_retry; \
  185. } \
  186. drm_modeset_drop_locks(&ctx); \
  187. drm_modeset_acquire_fini(&ctx); \
  188. if (!drm_drv_uses_atomic_modeset(dev)) \
  189. mutex_unlock(&dev->mode_config.mutex);
  190. #endif /* DRM_MODESET_LOCK_H_ */