drm_modeset_lock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2014 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  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/ww_mutex.h>
  26. struct drm_modeset_lock;
  27. /**
  28. * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx)
  29. * @ww_ctx: base acquire ctx
  30. * @contended: used internally for -EDEADLK handling
  31. * @locked: list of held locks
  32. * @trylock_only: trylock mode used in atomic contexts/panic notifiers
  33. *
  34. * Each thread competing for a set of locks must use one acquire
  35. * ctx. And if any lock fxn returns -EDEADLK, it must backoff and
  36. * retry.
  37. */
  38. struct drm_modeset_acquire_ctx {
  39. struct ww_acquire_ctx ww_ctx;
  40. /*
  41. * Contended lock: if a lock is contended you should only call
  42. * drm_modeset_backoff() which drops locks and slow-locks the
  43. * contended lock.
  44. */
  45. struct drm_modeset_lock *contended;
  46. /*
  47. * list of held locks (drm_modeset_lock)
  48. */
  49. struct list_head locked;
  50. /*
  51. * Trylock mode, use only for panic handlers!
  52. */
  53. bool trylock_only;
  54. };
  55. /**
  56. * struct drm_modeset_lock - used for locking modeset resources.
  57. * @mutex: resource locking
  58. * @head: used to hold it's place on state->locked list when
  59. * part of an atomic update
  60. *
  61. * Used for locking CRTCs and other modeset resources.
  62. */
  63. struct drm_modeset_lock {
  64. /*
  65. * modeset lock
  66. */
  67. struct ww_mutex mutex;
  68. /*
  69. * Resources that are locked as part of an atomic update are added
  70. * to a list (so we know what to unlock at the end).
  71. */
  72. struct list_head head;
  73. };
  74. extern struct ww_class crtc_ww_class;
  75. void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
  76. uint32_t flags);
  77. void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
  78. void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
  79. void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
  80. int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx);
  81. /**
  82. * drm_modeset_lock_init - initialize lock
  83. * @lock: lock to init
  84. */
  85. static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock)
  86. {
  87. ww_mutex_init(&lock->mutex, &crtc_ww_class);
  88. INIT_LIST_HEAD(&lock->head);
  89. }
  90. /**
  91. * drm_modeset_lock_fini - cleanup lock
  92. * @lock: lock to cleanup
  93. */
  94. static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
  95. {
  96. WARN_ON(!list_empty(&lock->head));
  97. }
  98. /**
  99. * drm_modeset_is_locked - equivalent to mutex_is_locked()
  100. * @lock: lock to check
  101. */
  102. static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
  103. {
  104. return ww_mutex_is_locked(&lock->mutex);
  105. }
  106. int drm_modeset_lock(struct drm_modeset_lock *lock,
  107. struct drm_modeset_acquire_ctx *ctx);
  108. int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
  109. struct drm_modeset_acquire_ctx *ctx);
  110. void drm_modeset_unlock(struct drm_modeset_lock *lock);
  111. struct drm_device;
  112. struct drm_crtc;
  113. struct drm_plane;
  114. void drm_modeset_lock_all(struct drm_device *dev);
  115. void drm_modeset_unlock_all(struct drm_device *dev);
  116. void drm_modeset_lock_crtc(struct drm_crtc *crtc,
  117. struct drm_plane *plane);
  118. void drm_modeset_unlock_crtc(struct drm_crtc *crtc);
  119. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
  120. struct drm_modeset_acquire_ctx *
  121. drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc);
  122. int drm_modeset_lock_all_crtcs(struct drm_device *dev,
  123. struct drm_modeset_acquire_ctx *ctx);
  124. #endif /* DRM_MODESET_LOCK_H_ */