blob: a3f736d243826a101d7ea650daa83625f863ca93 [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/**
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 */
41struct 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;
Daniel Vettercb597bb2014-07-27 19:09:33 +020056
57 /**
58 * Trylock mode, use only for panic handlers!
59 */
60 bool trylock_only;
Rob Clark51fd3712013-11-19 12:10:12 -050061};
62
63/**
64 * drm_modeset_lock - used for locking modeset resources.
65 * @mutex: resource locking
66 * @head: used to hold it's place on state->locked list when
67 * part of an atomic update
68 *
69 * Used for locking CRTCs and other modeset resources.
70 */
71struct drm_modeset_lock {
72 /**
73 * modeset lock
74 */
75 struct ww_mutex mutex;
76
77 /**
78 * Resources that are locked as part of an atomic update are added
79 * to a list (so we know what to unlock at the end).
80 */
81 struct list_head head;
82};
83
84extern struct ww_class crtc_ww_class;
85
86void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
87 uint32_t flags);
88void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
89void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
90void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
91int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx);
92
93/**
94 * drm_modeset_lock_init - initialize lock
95 * @lock: lock to init
96 */
97static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock)
98{
99 ww_mutex_init(&lock->mutex, &crtc_ww_class);
100 INIT_LIST_HEAD(&lock->head);
101}
102
103/**
104 * drm_modeset_lock_fini - cleanup lock
105 * @lock: lock to cleanup
106 */
107static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
108{
109 WARN_ON(!list_empty(&lock->head));
110}
111
112/**
113 * drm_modeset_is_locked - equivalent to mutex_is_locked()
114 * @lock: lock to check
115 */
116static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
117{
118 return ww_mutex_is_locked(&lock->mutex);
119}
120
121int drm_modeset_lock(struct drm_modeset_lock *lock,
122 struct drm_modeset_acquire_ctx *ctx);
123int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
124 struct drm_modeset_acquire_ctx *ctx);
125void drm_modeset_unlock(struct drm_modeset_lock *lock);
126
127struct drm_device;
Daniel Vetterd059f652014-07-25 18:07:40 +0200128struct drm_crtc;
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200129
130void drm_modeset_lock_all(struct drm_device *dev);
Daniel Vettercb597bb2014-07-27 19:09:33 +0200131int __drm_modeset_lock_all(struct drm_device *dev, bool trylock);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200132void drm_modeset_unlock_all(struct drm_device *dev);
Daniel Vetterd059f652014-07-25 18:07:40 +0200133void drm_modeset_lock_crtc(struct drm_crtc *crtc);
134void drm_modeset_unlock_crtc(struct drm_crtc *crtc);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200135void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
Daniel Vetterd059f652014-07-25 18:07:40 +0200136struct drm_modeset_acquire_ctx *
137drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200138
Rob Clark51fd3712013-11-19 12:10:12 -0500139int drm_modeset_lock_all_crtcs(struct drm_device *dev,
140 struct drm_modeset_acquire_ctx *ctx);
141
142#endif /* DRM_MODESET_LOCK_H_ */