1 /* $NetBSD: drm_modeset_lock.h,v 1.4 2018/08/27 14:39:59 riastradh Exp $ */ 2 3 /* 4 * Copyright (C) 2014 Red Hat 5 * Author: Rob Clark <robdclark@gmail.com> 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice shall be included in 15 * all copies or substantial portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #ifndef DRM_MODESET_LOCK_H_ 27 #define DRM_MODESET_LOCK_H_ 28 29 #include <linux/ww_mutex.h> 30 #include <asm/bug.h> 31 32 struct drm_modeset_lock; 33 34 /** 35 * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx) 36 * @ww_ctx: base acquire ctx 37 * @contended: used internally for -EDEADLK handling 38 * @locked: list of held locks 39 * @trylock_only: trylock mode used in atomic contexts/panic notifiers 40 * 41 * Each thread competing for a set of locks must use one acquire 42 * ctx. And if any lock fxn returns -EDEADLK, it must backoff and 43 * retry. 44 */ 45 struct drm_modeset_acquire_ctx { 46 47 struct ww_acquire_ctx ww_ctx; 48 49 /* 50 * Contended lock: if a lock is contended you should only call 51 * drm_modeset_backoff() which drops locks and slow-locks the 52 * contended lock. 53 */ 54 struct drm_modeset_lock *contended; 55 56 /* 57 * list of held locks (drm_modeset_lock) 58 */ 59 struct list_head locked; 60 61 /* 62 * Trylock mode, use only for panic handlers! 63 */ 64 bool trylock_only; 65 }; 66 67 /** 68 * struct drm_modeset_lock - used for locking modeset resources. 69 * @mutex: resource locking 70 * @head: used to hold it's place on state->locked list when 71 * part of an atomic update 72 * 73 * Used for locking CRTCs and other modeset resources. 74 */ 75 struct drm_modeset_lock { 76 /* 77 * modeset lock 78 */ 79 struct ww_mutex mutex; 80 81 /* 82 * Resources that are locked as part of an atomic update are added 83 * to a list (so we know what to unlock at the end). 84 */ 85 struct list_head head; 86 }; 87 88 extern struct ww_class crtc_ww_class; 89 90 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx, 91 uint32_t flags); 92 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx); 93 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx); 94 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx); 95 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx); 96 97 /** 98 * drm_modeset_lock_init - initialize lock 99 * @lock: lock to init 100 */ 101 static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock) 102 { 103 ww_mutex_init(&lock->mutex, &crtc_ww_class); 104 INIT_LIST_HEAD(&lock->head); 105 } 106 107 /** 108 * drm_modeset_lock_fini - cleanup lock 109 * @lock: lock to cleanup 110 */ 111 static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock) 112 { 113 WARN_ON(!list_empty(&lock->head)); 114 ww_mutex_destroy(&lock->mutex); 115 } 116 117 /** 118 * drm_modeset_is_locked - equivalent to mutex_is_locked() 119 * @lock: lock to check 120 */ 121 static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock) 122 { 123 return ww_mutex_is_locked(&lock->mutex); 124 } 125 126 int drm_modeset_lock(struct drm_modeset_lock *lock, 127 struct drm_modeset_acquire_ctx *ctx); 128 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock, 129 struct drm_modeset_acquire_ctx *ctx); 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_modeset_lock_crtc(struct drm_crtc *crtc, 139 struct drm_plane *plane); 140 void drm_modeset_unlock_crtc(struct drm_crtc *crtc); 141 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev); 142 struct drm_modeset_acquire_ctx * 143 drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc); 144 145 int drm_modeset_lock_all_crtcs(struct drm_device *dev, 146 struct drm_modeset_acquire_ctx *ctx); 147 148 #endif /* DRM_MODESET_LOCK_H_ */ 149