Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 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 | /** |
Randy Dunlap | f3a8088 | 2014-08-16 14:15:34 -0700 | [diff] [blame] | 32 | * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx) |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 33 | * @ww_ctx: base acquire ctx |
| 34 | * @contended: used internally for -EDEADLK handling |
| 35 | * @locked: list of held locks |
Daniel Vetter | b7a1aaf | 2014-10-27 20:37:37 +0100 | [diff] [blame] | 36 | * @trylock_only: trylock mode used in atomic contexts/panic notifiers |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 37 | * @interruptible: whether interruptible locking should be used. |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 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 | |
Danilo Cesar Lemes de Paula | fe8660a | 2015-08-21 16:46:14 -0300 | [diff] [blame] | 47 | /* |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 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 | |
Danilo Cesar Lemes de Paula | fe8660a | 2015-08-21 16:46:14 -0300 | [diff] [blame] | 54 | /* |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 55 | * list of held locks (drm_modeset_lock) |
| 56 | */ |
| 57 | struct list_head locked; |
Daniel Vetter | cb597bb | 2014-07-27 19:09:33 +0200 | [diff] [blame] | 58 | |
Danilo Cesar Lemes de Paula | fe8660a | 2015-08-21 16:46:14 -0300 | [diff] [blame] | 59 | /* |
Daniel Vetter | cb597bb | 2014-07-27 19:09:33 +0200 | [diff] [blame] | 60 | * Trylock mode, use only for panic handlers! |
| 61 | */ |
| 62 | bool trylock_only; |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 63 | |
| 64 | /* Perform interruptible waits on this context. */ |
| 65 | bool interruptible; |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | /** |
Randy Dunlap | f3a8088 | 2014-08-16 14:15:34 -0700 | [diff] [blame] | 69 | * struct drm_modeset_lock - used for locking modeset resources. |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 70 | * @mutex: resource locking |
Matt Roper | 1e55a53 | 2019-02-01 17:23:26 -0800 | [diff] [blame] | 71 | * @head: used to hold its place on &drm_atomi_state.locked list when |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 72 | * part of an atomic update |
| 73 | * |
| 74 | * Used for locking CRTCs and other modeset resources. |
| 75 | */ |
| 76 | struct drm_modeset_lock { |
Danilo Cesar Lemes de Paula | fe8660a | 2015-08-21 16:46:14 -0300 | [diff] [blame] | 77 | /* |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 78 | * modeset lock |
| 79 | */ |
| 80 | struct ww_mutex mutex; |
| 81 | |
Danilo Cesar Lemes de Paula | fe8660a | 2015-08-21 16:46:14 -0300 | [diff] [blame] | 82 | /* |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 83 | * Resources that are locked as part of an atomic update are added |
| 84 | * to a list (so we know what to unlock at the end). |
| 85 | */ |
| 86 | struct list_head head; |
| 87 | }; |
| 88 | |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 89 | #define DRM_MODESET_ACQUIRE_INTERRUPTIBLE BIT(0) |
| 90 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 91 | void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx, |
| 92 | uint32_t flags); |
| 93 | void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx); |
| 94 | void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx); |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 95 | int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 96 | |
Rob Clark | 35cf035 | 2016-11-14 17:40:57 -0500 | [diff] [blame] | 97 | void drm_modeset_lock_init(struct drm_modeset_lock *lock); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * drm_modeset_lock_fini - cleanup lock |
| 101 | * @lock: lock to cleanup |
| 102 | */ |
| 103 | static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock) |
| 104 | { |
| 105 | WARN_ON(!list_empty(&lock->head)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * drm_modeset_is_locked - equivalent to mutex_is_locked() |
| 110 | * @lock: lock to check |
| 111 | */ |
| 112 | static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock) |
| 113 | { |
| 114 | return ww_mutex_is_locked(&lock->mutex); |
| 115 | } |
| 116 | |
| 117 | int drm_modeset_lock(struct drm_modeset_lock *lock, |
| 118 | struct drm_modeset_acquire_ctx *ctx); |
Maarten Lankhorst | 6f8bcc7 | 2017-09-12 15:37:44 +0200 | [diff] [blame] | 119 | int __must_check drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 120 | void drm_modeset_unlock(struct drm_modeset_lock *lock); |
| 121 | |
| 122 | struct drm_device; |
Daniel Vetter | d059f65 | 2014-07-25 18:07:40 +0200 | [diff] [blame] | 123 | struct drm_crtc; |
Daniel Vetter | 4d02e2d | 2014-11-11 10:12:00 +0100 | [diff] [blame] | 124 | struct drm_plane; |
Daniel Vetter | a6a8bb8 | 2014-07-25 17:47:18 +0200 | [diff] [blame] | 125 | |
| 126 | void drm_modeset_lock_all(struct drm_device *dev); |
| 127 | void drm_modeset_unlock_all(struct drm_device *dev); |
| 128 | void drm_warn_on_modeset_not_all_locked(struct drm_device *dev); |
| 129 | |
Thierry Reding | 06eaae4 | 2015-12-02 17:50:03 +0100 | [diff] [blame] | 130 | int drm_modeset_lock_all_ctx(struct drm_device *dev, |
| 131 | struct drm_modeset_acquire_ctx *ctx); |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 132 | |
Sean Paul | b7ea04d | 2018-11-29 10:04:17 -0500 | [diff] [blame] | 133 | /** |
| 134 | * DRM_MODESET_LOCK_ALL_BEGIN - Helper to acquire modeset locks |
| 135 | * @dev: drm device |
| 136 | * @ctx: local modeset acquire context, will be dereferenced |
| 137 | * @flags: DRM_MODESET_ACQUIRE_* flags to pass to drm_modeset_acquire_init() |
| 138 | * @ret: local ret/err/etc variable to track error status |
| 139 | * |
| 140 | * Use these macros to simplify grabbing all modeset locks using a local |
| 141 | * context. This has the advantage of reducing boilerplate, but also properly |
| 142 | * checking return values where appropriate. |
| 143 | * |
| 144 | * Any code run between BEGIN and END will be holding the modeset locks. |
| 145 | * |
| 146 | * This must be paired with DRM_MODESET_LOCK_ALL_END(). We will jump back and |
| 147 | * forth between the labels on deadlock and error conditions. |
| 148 | * |
| 149 | * Drivers can acquire additional modeset locks. If any lock acquisition |
| 150 | * fails, the control flow needs to jump to DRM_MODESET_LOCK_ALL_END() with |
| 151 | * the @ret parameter containing the return value of drm_modeset_lock(). |
| 152 | * |
| 153 | * Returns: |
| 154 | * The only possible value of ret immediately after DRM_MODESET_LOCK_ALL_BEGIN() |
| 155 | * is 0, so no error checking is necessary |
| 156 | */ |
| 157 | #define DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, flags, ret) \ |
| 158 | drm_modeset_acquire_init(&ctx, flags); \ |
| 159 | modeset_lock_retry: \ |
| 160 | ret = drm_modeset_lock_all_ctx(dev, &ctx); \ |
| 161 | if (ret) \ |
| 162 | goto modeset_lock_fail; |
| 163 | |
| 164 | /** |
| 165 | * DRM_MODESET_LOCK_ALL_END - Helper to release and cleanup modeset locks |
| 166 | * @ctx: local modeset acquire context, will be dereferenced |
| 167 | * @ret: local ret/err/etc variable to track error status |
| 168 | * |
| 169 | * The other side of DRM_MODESET_LOCK_ALL_BEGIN(). It will bounce back to BEGIN |
| 170 | * if ret is -EDEADLK. |
| 171 | * |
| 172 | * It's important that you use the same ret variable for begin and end so |
| 173 | * deadlock conditions are properly handled. |
| 174 | * |
| 175 | * Returns: |
| 176 | * ret will be untouched unless it is -EDEADLK on entry. That means that if you |
| 177 | * successfully acquire the locks, ret will be whatever your code sets it to. If |
| 178 | * there is a deadlock or other failure with acquire or backoff, ret will be set |
| 179 | * to that failure. In both of these cases the code between BEGIN/END will not |
| 180 | * be run, so the failure will reflect the inability to grab the locks. |
| 181 | */ |
| 182 | #define DRM_MODESET_LOCK_ALL_END(ctx, ret) \ |
| 183 | modeset_lock_fail: \ |
| 184 | if (ret == -EDEADLK) { \ |
| 185 | ret = drm_modeset_backoff(&ctx); \ |
| 186 | if (!ret) \ |
| 187 | goto modeset_lock_retry; \ |
| 188 | } \ |
| 189 | drm_modeset_drop_locks(&ctx); \ |
| 190 | drm_modeset_acquire_fini(&ctx); |
| 191 | |
Rob Clark | 51fd371 | 2013-11-19 12:10:12 -0500 | [diff] [blame] | 192 | #endif /* DRM_MODESET_LOCK_H_ */ |