blob: 73e6534fd0aa5cc2c58b62cdb5238cb3ca38f9f1 [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#include <drm/drmP.h>
25#include <drm/drm_crtc.h>
26#include <drm/drm_modeset_lock.h>
27
28/**
29 * DOC: kms locking
30 *
31 * As KMS moves toward more fine grained locking, and atomic ioctl where
32 * userspace can indirectly control locking order, it becomes necessary
33 * to use ww_mutex and acquire-contexts to avoid deadlocks. But because
34 * the locking is more distributed around the driver code, we want a bit
35 * of extra utility/tracking out of our acquire-ctx. This is provided
36 * by drm_modeset_lock / drm_modeset_acquire_ctx.
37 *
38 * For basic principles of ww_mutex, see: Documentation/ww-mutex-design.txt
39 *
40 * The basic usage pattern is to:
41 *
42 * drm_modeset_acquire_init(&ctx)
43 * retry:
44 * foreach (lock in random_ordered_set_of_locks) {
45 * ret = drm_modeset_lock(lock, &ctx)
46 * if (ret == -EDEADLK) {
47 * drm_modeset_backoff(&ctx);
48 * goto retry;
49 * }
50 * }
51 *
52 * ... do stuff ...
53 *
54 * drm_modeset_drop_locks(&ctx);
55 * drm_modeset_acquire_fini(&ctx);
56 */
57
58
59/**
Daniel Vettera6a8bb82014-07-25 17:47:18 +020060 * drm_modeset_lock_all - take all modeset locks
61 * @dev: drm device
62 *
63 * This function takes all modeset locks, suitable where a more fine-grained
64 * scheme isn't (yet) implemented. Locks must be dropped with
65 * drm_modeset_unlock_all.
66 */
67void drm_modeset_lock_all(struct drm_device *dev)
68{
69 struct drm_mode_config *config = &dev->mode_config;
70 struct drm_modeset_acquire_ctx *ctx;
71 int ret;
72
73 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
74 if (WARN_ON(!ctx))
75 return;
76
77 mutex_lock(&config->mutex);
78
79 drm_modeset_acquire_init(ctx, 0);
80
81retry:
82 ret = drm_modeset_lock(&config->connection_mutex, ctx);
83 if (ret)
84 goto fail;
85 ret = drm_modeset_lock_all_crtcs(dev, ctx);
86 if (ret)
87 goto fail;
88
89 WARN_ON(config->acquire_ctx);
90
91 /* now we hold the locks, so now that it is safe, stash the
92 * ctx for drm_modeset_unlock_all():
93 */
94 config->acquire_ctx = ctx;
95
96 drm_warn_on_modeset_not_all_locked(dev);
97
98 return;
99
100fail:
101 if (ret == -EDEADLK) {
102 drm_modeset_backoff(ctx);
103 goto retry;
104 }
105}
106EXPORT_SYMBOL(drm_modeset_lock_all);
107
108/**
109 * drm_modeset_unlock_all - drop all modeset locks
110 * @dev: device
111 *
112 * This function drop all modeset locks taken by drm_modeset_lock_all.
113 */
114void drm_modeset_unlock_all(struct drm_device *dev)
115{
116 struct drm_mode_config *config = &dev->mode_config;
117 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
118
119 if (WARN_ON(!ctx))
120 return;
121
122 config->acquire_ctx = NULL;
123 drm_modeset_drop_locks(ctx);
124 drm_modeset_acquire_fini(ctx);
125
126 kfree(ctx);
127
128 mutex_unlock(&dev->mode_config.mutex);
129}
130EXPORT_SYMBOL(drm_modeset_unlock_all);
131
132/**
133 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
134 * @dev: device
135 *
136 * Useful as a debug assert.
137 */
138void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
139{
140 struct drm_crtc *crtc;
141
142 /* Locking is currently fubar in the panic handler. */
143 if (oops_in_progress)
144 return;
145
146 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
147 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
148
149 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
150 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
151}
152EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
153
154/**
Rob Clark51fd3712013-11-19 12:10:12 -0500155 * drm_modeset_acquire_init - initialize acquire context
156 * @ctx: the acquire context
157 * @flags: for future
158 */
159void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
160 uint32_t flags)
161{
Rob Clarkfb549182014-06-07 10:55:39 -0400162 memset(ctx, 0, sizeof(*ctx));
Rob Clark51fd3712013-11-19 12:10:12 -0500163 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
164 INIT_LIST_HEAD(&ctx->locked);
165}
166EXPORT_SYMBOL(drm_modeset_acquire_init);
167
168/**
169 * drm_modeset_acquire_fini - cleanup acquire context
170 * @ctx: the acquire context
171 */
172void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
173{
174 ww_acquire_fini(&ctx->ww_ctx);
175}
176EXPORT_SYMBOL(drm_modeset_acquire_fini);
177
178/**
179 * drm_modeset_drop_locks - drop all locks
180 * @ctx: the acquire context
181 *
182 * Drop all locks currently held against this acquire context.
183 */
184void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
185{
186 WARN_ON(ctx->contended);
187 while (!list_empty(&ctx->locked)) {
188 struct drm_modeset_lock *lock;
189
190 lock = list_first_entry(&ctx->locked,
191 struct drm_modeset_lock, head);
192
193 drm_modeset_unlock(lock);
194 }
195}
196EXPORT_SYMBOL(drm_modeset_drop_locks);
197
198static inline int modeset_lock(struct drm_modeset_lock *lock,
199 struct drm_modeset_acquire_ctx *ctx,
200 bool interruptible, bool slow)
201{
202 int ret;
203
204 WARN_ON(ctx->contended);
205
206 if (interruptible && slow) {
207 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
208 } else if (interruptible) {
209 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
210 } else if (slow) {
211 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
212 ret = 0;
213 } else {
214 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
215 }
216 if (!ret) {
217 WARN_ON(!list_empty(&lock->head));
218 list_add(&lock->head, &ctx->locked);
219 } else if (ret == -EALREADY) {
220 /* we already hold the lock.. this is fine. For atomic
221 * we will need to be able to drm_modeset_lock() things
222 * without having to keep track of what is already locked
223 * or not.
224 */
225 ret = 0;
226 } else if (ret == -EDEADLK) {
227 ctx->contended = lock;
228 }
229
230 return ret;
231}
232
233static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
234 bool interruptible)
235{
236 struct drm_modeset_lock *contended = ctx->contended;
237
238 ctx->contended = NULL;
239
240 if (WARN_ON(!contended))
241 return 0;
242
243 drm_modeset_drop_locks(ctx);
244
245 return modeset_lock(contended, ctx, interruptible, true);
246}
247
248/**
249 * drm_modeset_backoff - deadlock avoidance backoff
250 * @ctx: the acquire context
251 *
252 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
253 * you must call this function to drop all currently held locks and
254 * block until the contended lock becomes available.
255 */
256void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
257{
258 modeset_backoff(ctx, false);
259}
260EXPORT_SYMBOL(drm_modeset_backoff);
261
262/**
263 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
264 * @ctx: the acquire context
265 *
266 * Interruptible version of drm_modeset_backoff()
267 */
268int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
269{
270 return modeset_backoff(ctx, true);
271}
272EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
273
274/**
275 * drm_modeset_lock - take modeset lock
276 * @lock: lock to take
277 * @ctx: acquire ctx
278 *
279 * If ctx is not NULL, then its ww acquire context is used and the
280 * lock will be tracked by the context and can be released by calling
281 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
282 * deadlock scenario has been detected and it is an error to attempt
283 * to take any more locks without first calling drm_modeset_backoff().
284 */
285int drm_modeset_lock(struct drm_modeset_lock *lock,
286 struct drm_modeset_acquire_ctx *ctx)
287{
288 if (ctx)
289 return modeset_lock(lock, ctx, false, false);
290
291 ww_mutex_lock(&lock->mutex, NULL);
292 return 0;
293}
294EXPORT_SYMBOL(drm_modeset_lock);
295
296/**
297 * drm_modeset_lock_interruptible - take modeset lock
298 * @lock: lock to take
299 * @ctx: acquire ctx
300 *
301 * Interruptible version of drm_modeset_lock()
302 */
303int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
304 struct drm_modeset_acquire_ctx *ctx)
305{
306 if (ctx)
307 return modeset_lock(lock, ctx, true, false);
308
309 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
310}
311EXPORT_SYMBOL(drm_modeset_lock_interruptible);
312
313/**
314 * drm_modeset_unlock - drop modeset lock
315 * @lock: lock to release
316 */
317void drm_modeset_unlock(struct drm_modeset_lock *lock)
318{
319 list_del_init(&lock->head);
320 ww_mutex_unlock(&lock->mutex);
321}
322EXPORT_SYMBOL(drm_modeset_unlock);
323
324/* Temporary.. until we have sufficiently fine grained locking, there
325 * are a couple scenarios where it is convenient to grab all crtc locks.
326 * It is planned to remove this:
327 */
328int drm_modeset_lock_all_crtcs(struct drm_device *dev,
329 struct drm_modeset_acquire_ctx *ctx)
330{
331 struct drm_mode_config *config = &dev->mode_config;
332 struct drm_crtc *crtc;
333 int ret = 0;
334
335 list_for_each_entry(crtc, &config->crtc_list, head) {
336 ret = drm_modeset_lock(&crtc->mutex, ctx);
337 if (ret)
338 return ret;
339 }
340
341 return 0;
342}
343EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);