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 24 #ifndef DRM_MODESET_LOCK_H_ 25 #define DRM_MODESET_LOCK_H_ 26 27 #include <linux/ww_mutex.h> 28 29 struct drm_modeset_lock; 30 31 /** 32 * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx) 33 * @ww_ctx: base acquire ctx 34 * @contended: used internally for -EDEADLK handling 35 * @locked: list of held locks 36 * @trylock_only: trylock mode used in atomic contexts/panic notifiers 37 * @interruptible: whether interruptible locking should be used. 38 * 39 * Each thread competing for a set of locks must use one acquire 40 * ctx. And if any lock fxn returns -EDEADLK, it must backoff and 41 * retry. 42 */ 43 struct drm_modeset_acquire_ctx { 44 45 struct ww_acquire_ctx ww_ctx; 46 47 /* 48 * Contended lock: if a lock is contended you should only call 49 * drm_modeset_backoff() which drops locks and slow-locks the 50 * contended lock. 51 */ 52 struct drm_modeset_lock *contended; 53 54 /* 55 * list of held locks (drm_modeset_lock_info) 56 */ 57 struct list_head locked; 58 59 /* 60 * Trylock mode, use only for panic handlers! 61 */ 62 bool trylock_only; 63 64 /* Perform interruptible waits on this context. */ 65 bool interruptible; 66 }; 67 68 /* 69 * Keep track of extra locks. 70 */ 71 struct drm_modeset_lock_info { 72 struct list_head ctx_entry; 73 struct list_head lock_entry; 74 struct drm_modeset_lock *lock; 75 struct drm_modeset_acquire_ctx *ctx; 76 }; 77 78 /** 79 * struct drm_modeset_lock - used for locking modeset resources. 80 * @mutex: resource locking 81 * @head: used to hold it's place on &drm_atomi_state.locked list when 82 * part of an atomic update 83 * 84 * Used for locking CRTCs and other modeset resources. 85 */ 86 struct drm_modeset_lock { 87 /* 88 * modeset lock 89 */ 90 struct ww_mutex mutex; 91 92 /* 93 * Resources that are locked as part of an atomic update are added 94 * to a list (so we know what to unlock at the end). 95 */ 96 struct list_head head; 97 }; 98 99 #define DRM_MODESET_ACQUIRE_INTERRUPTIBLE BIT(0) 100 101 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx, 102 uint32_t flags); 103 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx); 104 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx); 105 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx); 106 107 void drm_modeset_lock_init(struct drm_modeset_lock *lock); 108 109 /** 110 * drm_modeset_lock_fini - cleanup lock 111 * @lock: lock to cleanup 112 */ 113 static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock) 114 { 115 WARN_ON(!list_empty(&lock->head)); 116 } 117 118 /** 119 * drm_modeset_is_locked - equivalent to mutex_is_locked() 120 * @lock: lock to check 121 */ 122 static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock) 123 { 124 return ww_mutex_is_locked(&lock->mutex); 125 } 126 127 int drm_modeset_lock(struct drm_modeset_lock *lock, 128 struct drm_modeset_acquire_ctx *ctx); 129 int __must_check drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock); 130 void drm_modeset_unlock(struct drm_modeset_lock *lock); 131 132 struct drm_device; 133 struct drm_crtc; 134 struct drm_plane; 135 136 void drm_modeset_lock_all(struct drm_device *dev); 137 void drm_modeset_unlock_all(struct drm_device *dev); 138 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev); 139 140 int drm_modeset_lock_all_ctx(struct drm_device *dev, 141 struct drm_modeset_acquire_ctx *ctx); 142 143 #endif /* DRM_MODESET_LOCK_H_ */ 144