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