Home | History | Annotate | Line # | Download | only in drm
drm_modeset_lock.h revision 1.1.1.2
      1 /*	$NetBSD: drm_modeset_lock.h,v 1.1.1.2 2021/12/18 20:15:57 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 2014 Red Hat
      5  * Author: Rob Clark <robdclark (at) 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 
     31 struct drm_modeset_lock;
     32 
     33 /**
     34  * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx)
     35  * @ww_ctx: base acquire ctx
     36  * @contended: used internally for -EDEADLK handling
     37  * @locked: list of held locks
     38  * @trylock_only: trylock mode used in atomic contexts/panic notifiers
     39  * @interruptible: whether interruptible locking should be used.
     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 	/* Perform interruptible waits on this context. */
     67 	bool interruptible;
     68 };
     69 
     70 /**
     71  * struct drm_modeset_lock - used for locking modeset resources.
     72  * @mutex: resource locking
     73  * @head: used to hold its place on &drm_atomi_state.locked list when
     74  *    part of an atomic update
     75  *
     76  * Used for locking CRTCs and other modeset resources.
     77  */
     78 struct drm_modeset_lock {
     79 	/*
     80 	 * modeset lock
     81 	 */
     82 	struct ww_mutex mutex;
     83 
     84 	/*
     85 	 * Resources that are locked as part of an atomic update are added
     86 	 * to a list (so we know what to unlock at the end).
     87 	 */
     88 	struct list_head head;
     89 };
     90 
     91 #define DRM_MODESET_ACQUIRE_INTERRUPTIBLE BIT(0)
     92 
     93 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
     94 		uint32_t flags);
     95 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
     96 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
     97 int drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
     98 
     99 void drm_modeset_lock_init(struct drm_modeset_lock *lock);
    100 
    101 /**
    102  * drm_modeset_lock_fini - cleanup lock
    103  * @lock: lock to cleanup
    104  */
    105 static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
    106 {
    107 	WARN_ON(!list_empty(&lock->head));
    108 }
    109 
    110 /**
    111  * drm_modeset_is_locked - equivalent to mutex_is_locked()
    112  * @lock: lock to check
    113  */
    114 static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
    115 {
    116 	return ww_mutex_is_locked(&lock->mutex);
    117 }
    118 
    119 /**
    120  * drm_modeset_lock_assert_held - equivalent to lockdep_assert_held()
    121  * @lock: lock to check
    122  */
    123 static inline void drm_modeset_lock_assert_held(struct drm_modeset_lock *lock)
    124 {
    125 	lockdep_assert_held(&lock->mutex.base);
    126 }
    127 
    128 int drm_modeset_lock(struct drm_modeset_lock *lock,
    129 		struct drm_modeset_acquire_ctx *ctx);
    130 int __must_check drm_modeset_lock_single_interruptible(struct drm_modeset_lock *lock);
    131 void drm_modeset_unlock(struct drm_modeset_lock *lock);
    132 
    133 struct drm_device;
    134 struct drm_crtc;
    135 struct drm_plane;
    136 
    137 void drm_modeset_lock_all(struct drm_device *dev);
    138 void drm_modeset_unlock_all(struct drm_device *dev);
    139 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
    140 
    141 int drm_modeset_lock_all_ctx(struct drm_device *dev,
    142 			     struct drm_modeset_acquire_ctx *ctx);
    143 
    144 /**
    145  * DRM_MODESET_LOCK_ALL_BEGIN - Helper to acquire modeset locks
    146  * @dev: drm device
    147  * @ctx: local modeset acquire context, will be dereferenced
    148  * @flags: DRM_MODESET_ACQUIRE_* flags to pass to drm_modeset_acquire_init()
    149  * @ret: local ret/err/etc variable to track error status
    150  *
    151  * Use these macros to simplify grabbing all modeset locks using a local
    152  * context. This has the advantage of reducing boilerplate, but also properly
    153  * checking return values where appropriate.
    154  *
    155  * Any code run between BEGIN and END will be holding the modeset locks.
    156  *
    157  * This must be paired with DRM_MODESET_LOCK_ALL_END(). We will jump back and
    158  * forth between the labels on deadlock and error conditions.
    159  *
    160  * Drivers can acquire additional modeset locks. If any lock acquisition
    161  * fails, the control flow needs to jump to DRM_MODESET_LOCK_ALL_END() with
    162  * the @ret parameter containing the return value of drm_modeset_lock().
    163  *
    164  * Returns:
    165  * The only possible value of ret immediately after DRM_MODESET_LOCK_ALL_BEGIN()
    166  * is 0, so no error checking is necessary
    167  */
    168 #define DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, flags, ret)		\
    169 	drm_modeset_acquire_init(&ctx, flags);				\
    170 modeset_lock_retry:							\
    171 	ret = drm_modeset_lock_all_ctx(dev, &ctx);			\
    172 	if (ret)							\
    173 		goto modeset_lock_fail;
    174 
    175 /**
    176  * DRM_MODESET_LOCK_ALL_END - Helper to release and cleanup modeset locks
    177  * @ctx: local modeset acquire context, will be dereferenced
    178  * @ret: local ret/err/etc variable to track error status
    179  *
    180  * The other side of DRM_MODESET_LOCK_ALL_BEGIN(). It will bounce back to BEGIN
    181  * if ret is -EDEADLK.
    182  *
    183  * It's important that you use the same ret variable for begin and end so
    184  * deadlock conditions are properly handled.
    185  *
    186  * Returns:
    187  * ret will be untouched unless it is -EDEADLK on entry. That means that if you
    188  * successfully acquire the locks, ret will be whatever your code sets it to. If
    189  * there is a deadlock or other failure with acquire or backoff, ret will be set
    190  * to that failure. In both of these cases the code between BEGIN/END will not
    191  * be run, so the failure will reflect the inability to grab the locks.
    192  */
    193 #define DRM_MODESET_LOCK_ALL_END(ctx, ret)				\
    194 modeset_lock_fail:							\
    195 	if (ret == -EDEADLK) {						\
    196 		ret = drm_modeset_backoff(&ctx);			\
    197 		if (!ret)						\
    198 			goto modeset_lock_retry;			\
    199 	}								\
    200 	drm_modeset_drop_locks(&ctx);					\
    201 	drm_modeset_acquire_fini(&ctx);
    202 
    203 #endif /* DRM_MODESET_LOCK_H_ */
    204