blob: c5576fbcb909f925a241a4f1ff9736bc8c8ad119 [file] [log] [blame]
Rob Clark51fd3712013-11-19 12:10:12 -05001/*
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
29struct drm_modeset_lock;
30
31/**
Randy Dunlapf3a80882014-08-16 14:15:34 -070032 * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx)
Rob Clark51fd3712013-11-19 12:10:12 -050033 * @ww_ctx: base acquire ctx
34 * @contended: used internally for -EDEADLK handling
35 * @locked: list of held locks
Daniel Vetterb7a1aaf2014-10-27 20:37:37 +010036 * @trylock_only: trylock mode used in atomic contexts/panic notifiers
Rob Clark51fd3712013-11-19 12:10:12 -050037 *
38 * Each thread competing for a set of locks must use one acquire
39 * ctx. And if any lock fxn returns -EDEADLK, it must backoff and
40 * retry.
41 */
42struct drm_modeset_acquire_ctx {
43
44 struct ww_acquire_ctx ww_ctx;
45
Danilo Cesar Lemes de Paulafe8660a2015-08-21 16:46:14 -030046 /*
Rob Clark51fd3712013-11-19 12:10:12 -050047 * Contended lock: if a lock is contended you should only call
48 * drm_modeset_backoff() which drops locks and slow-locks the
49 * contended lock.
50 */
51 struct drm_modeset_lock *contended;
52
Danilo Cesar Lemes de Paulafe8660a2015-08-21 16:46:14 -030053 /*
Rob Clark51fd3712013-11-19 12:10:12 -050054 * list of held locks (drm_modeset_lock)
55 */
56 struct list_head locked;
Daniel Vettercb597bb2014-07-27 19:09:33 +020057
Danilo Cesar Lemes de Paulafe8660a2015-08-21 16:46:14 -030058 /*
Daniel Vettercb597bb2014-07-27 19:09:33 +020059 * Trylock mode, use only for panic handlers!
60 */
61 bool trylock_only;
Rob Clark51fd3712013-11-19 12:10:12 -050062};
63
64/**
Randy Dunlapf3a80882014-08-16 14:15:34 -070065 * struct drm_modeset_lock - used for locking modeset resources.
Rob Clark51fd3712013-11-19 12:10:12 -050066 * @mutex: resource locking
67 * @head: used to hold it's place on state->locked list when
68 * part of an atomic update
69 *
70 * Used for locking CRTCs and other modeset resources.
71 */
72struct drm_modeset_lock {
Danilo Cesar Lemes de Paulafe8660a2015-08-21 16:46:14 -030073 /*
Rob Clark51fd3712013-11-19 12:10:12 -050074 * modeset lock
75 */
76 struct ww_mutex mutex;
77
Danilo Cesar Lemes de Paulafe8660a2015-08-21 16:46:14 -030078 /*
Rob Clark51fd3712013-11-19 12:10:12 -050079 * Resources that are locked as part of an atomic update are added
80 * to a list (so we know what to unlock at the end).
81 */
82 struct list_head head;
83};
84
85extern struct ww_class crtc_ww_class;
86
87void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
88 uint32_t flags);
89void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
90void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
91void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
92int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx);
93
94/**
95 * drm_modeset_lock_init - initialize lock
96 * @lock: lock to init
97 */
98static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock)
99{
100 ww_mutex_init(&lock->mutex, &crtc_ww_class);
101 INIT_LIST_HEAD(&lock->head);
102}
103
104/**
105 * drm_modeset_lock_fini - cleanup lock
106 * @lock: lock to cleanup
107 */
108static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
109{
110 WARN_ON(!list_empty(&lock->head));
111}
112
113/**
114 * drm_modeset_is_locked - equivalent to mutex_is_locked()
115 * @lock: lock to check
116 */
117static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
118{
119 return ww_mutex_is_locked(&lock->mutex);
120}
121
122int drm_modeset_lock(struct drm_modeset_lock *lock,
123 struct drm_modeset_acquire_ctx *ctx);
124int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
125 struct drm_modeset_acquire_ctx *ctx);
126void drm_modeset_unlock(struct drm_modeset_lock *lock);
127
128struct drm_device;
Daniel Vetterd059f652014-07-25 18:07:40 +0200129struct drm_crtc;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100130struct drm_plane;
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200131
132void drm_modeset_lock_all(struct drm_device *dev);
133void drm_modeset_unlock_all(struct drm_device *dev);
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100134void drm_modeset_lock_crtc(struct drm_crtc *crtc,
135 struct drm_plane *plane);
Daniel Vetterd059f652014-07-25 18:07:40 +0200136void drm_modeset_unlock_crtc(struct drm_crtc *crtc);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200137void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
Daniel Vetterd059f652014-07-25 18:07:40 +0200138struct drm_modeset_acquire_ctx *
139drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200140
Thierry Reding06eaae42015-12-02 17:50:03 +0100141int drm_modeset_lock_all_ctx(struct drm_device *dev,
142 struct drm_modeset_acquire_ctx *ctx);
Rob Clark51fd3712013-11-19 12:10:12 -0500143
144#endif /* DRM_MODESET_LOCK_H_ */