blob: 61146f5b4f56a90dc7dc371289ce11a23d38b9e6 [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
Daniel Vetter0645de52016-05-30 11:10:49 +020033 * to use &ww_mutex and acquire-contexts to avoid deadlocks. But because
Rob Clark51fd3712013-11-19 12:10:12 -050034 * 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 *
Daniel Vetter0645de52016-05-30 11:10:49 +020038 * For basic principles of &ww_mutex, see: Documentation/locking/ww-mutex-design.txt
Rob Clark51fd3712013-11-19 12:10:12 -050039 *
Daniel Vetterda5335b2016-05-31 22:55:13 +020040 * The basic usage pattern is to::
Rob Clark51fd3712013-11-19 12:10:12 -050041 *
42 * drm_modeset_acquire_init(&ctx)
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +010043 * retry:
Rob Clark51fd3712013-11-19 12:10:12 -050044 * foreach (lock in random_ordered_set_of_locks) {
Danilo Cesar Lemes de Paulaf03d8ed2015-11-25 18:07:55 +010045 * ret = drm_modeset_lock(lock, &ctx)
46 * if (ret == -EDEADLK) {
47 * drm_modeset_backoff(&ctx);
48 * goto retry;
49 * }
Rob Clark51fd3712013-11-19 12:10:12 -050050 * }
Rob Clark51fd3712013-11-19 12:10:12 -050051 * ... do stuff ...
Rob Clark51fd3712013-11-19 12:10:12 -050052 * drm_modeset_drop_locks(&ctx);
53 * drm_modeset_acquire_fini(&ctx);
Daniel Vetter0645de52016-05-30 11:10:49 +020054 *
55 * On top of of these per-object locks using &ww_mutex there's also an overall
56 * dev->mode_config.lock, for protecting everything else. Mostly this means
57 * probe state of connectors, and preventing hotplug add/removal of connectors.
58 *
59 * Finally there's a bunch of dedicated locks to protect drm core internal
60 * lists and lookup data structures.
Rob Clark51fd3712013-11-19 12:10:12 -050061 */
62
Rob Clark51fd3712013-11-19 12:10:12 -050063/**
Daniel Vetterbf9e37b2015-07-28 13:18:42 +020064 * drm_modeset_lock_all - take all modeset locks
Thierry Reding06eaae42015-12-02 17:50:03 +010065 * @dev: DRM device
Daniel Vettera6a8bb82014-07-25 17:47:18 +020066 *
Daniel Vetterbf9e37b2015-07-28 13:18:42 +020067 * This function takes all modeset locks, suitable where a more fine-grained
Thierry Reding06eaae42015-12-02 17:50:03 +010068 * scheme isn't (yet) implemented. Locks must be dropped by calling the
69 * drm_modeset_unlock_all() function.
70 *
71 * This function is deprecated. It allocates a lock acquisition context and
72 * stores it in the DRM device's ->mode_config. This facilitate conversion of
73 * existing code because it removes the need to manually deal with the
74 * acquisition context, but it is also brittle because the context is global
75 * and care must be taken not to nest calls. New code should use the
76 * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
Daniel Vettera6a8bb82014-07-25 17:47:18 +020077 */
Daniel Vetterbf9e37b2015-07-28 13:18:42 +020078void drm_modeset_lock_all(struct drm_device *dev)
Daniel Vettera6a8bb82014-07-25 17:47:18 +020079{
80 struct drm_mode_config *config = &dev->mode_config;
81 struct drm_modeset_acquire_ctx *ctx;
82 int ret;
83
Daniel Vetterbf9e37b2015-07-28 13:18:42 +020084 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
85 if (WARN_ON(!ctx))
86 return;
Daniel Vettera6a8bb82014-07-25 17:47:18 +020087
Daniel Vetterbf9e37b2015-07-28 13:18:42 +020088 mutex_lock(&config->mutex);
Daniel Vettera6a8bb82014-07-25 17:47:18 +020089
90 drm_modeset_acquire_init(ctx, 0);
91
92retry:
Thierry Reding06eaae42015-12-02 17:50:03 +010093 ret = drm_modeset_lock_all_ctx(dev, ctx);
94 if (ret < 0) {
95 if (ret == -EDEADLK) {
96 drm_modeset_backoff(ctx);
97 goto retry;
98 }
99
100 drm_modeset_acquire_fini(ctx);
101 kfree(ctx);
102 return;
103 }
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200104
105 WARN_ON(config->acquire_ctx);
106
Thierry Reding06eaae42015-12-02 17:50:03 +0100107 /*
108 * We hold the locks now, so it is safe to stash the acquisition
109 * context for drm_modeset_unlock_all().
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200110 */
111 config->acquire_ctx = ctx;
112
113 drm_warn_on_modeset_not_all_locked(dev);
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200114}
115EXPORT_SYMBOL(drm_modeset_lock_all);
116
117/**
118 * drm_modeset_unlock_all - drop all modeset locks
Thierry Reding06eaae42015-12-02 17:50:03 +0100119 * @dev: DRM device
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200120 *
Thierry Reding06eaae42015-12-02 17:50:03 +0100121 * This function drops all modeset locks taken by a previous call to the
122 * drm_modeset_lock_all() function.
123 *
124 * This function is deprecated. It uses the lock acquisition context stored
125 * in the DRM device's ->mode_config. This facilitates conversion of existing
126 * code because it removes the need to manually deal with the acquisition
127 * context, but it is also brittle because the context is global and care must
128 * be taken not to nest calls. New code should pass the acquisition context
129 * directly to the drm_modeset_drop_locks() function.
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200130 */
131void drm_modeset_unlock_all(struct drm_device *dev)
132{
133 struct drm_mode_config *config = &dev->mode_config;
134 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
135
136 if (WARN_ON(!ctx))
137 return;
138
139 config->acquire_ctx = NULL;
140 drm_modeset_drop_locks(ctx);
141 drm_modeset_acquire_fini(ctx);
142
143 kfree(ctx);
144
145 mutex_unlock(&dev->mode_config.mutex);
146}
147EXPORT_SYMBOL(drm_modeset_unlock_all);
148
149/**
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100150 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
151 * @crtc: DRM CRTC
152 * @plane: DRM plane to be updated on @crtc
Daniel Vetterd059f652014-07-25 18:07:40 +0200153 *
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100154 * This function locks the given crtc and plane (which should be either the
155 * primary or cursor plane) using a hidden acquire context. This is necessary so
156 * that drivers internally using the atomic interfaces can grab further locks
157 * with the lock acquire context.
158 *
159 * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
160 * converted to universal planes yet.
Daniel Vetterd059f652014-07-25 18:07:40 +0200161 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100162void drm_modeset_lock_crtc(struct drm_crtc *crtc,
163 struct drm_plane *plane)
Daniel Vetterd059f652014-07-25 18:07:40 +0200164{
165 struct drm_modeset_acquire_ctx *ctx;
166 int ret;
167
168 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
169 if (WARN_ON(!ctx))
170 return;
171
172 drm_modeset_acquire_init(ctx, 0);
173
174retry:
175 ret = drm_modeset_lock(&crtc->mutex, ctx);
176 if (ret)
177 goto fail;
178
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100179 if (plane) {
180 ret = drm_modeset_lock(&plane->mutex, ctx);
181 if (ret)
182 goto fail;
183
184 if (plane->crtc) {
185 ret = drm_modeset_lock(&plane->crtc->mutex, ctx);
186 if (ret)
187 goto fail;
188 }
189 }
190
Daniel Vetterd059f652014-07-25 18:07:40 +0200191 WARN_ON(crtc->acquire_ctx);
192
193 /* now we hold the locks, so now that it is safe, stash the
194 * ctx for drm_modeset_unlock_crtc():
195 */
196 crtc->acquire_ctx = ctx;
197
198 return;
199
200fail:
201 if (ret == -EDEADLK) {
202 drm_modeset_backoff(ctx);
203 goto retry;
204 }
205}
206EXPORT_SYMBOL(drm_modeset_lock_crtc);
207
208/**
209 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
Daniel Vetter295ee852014-07-30 14:23:44 +0200210 * @crtc: drm crtc
Daniel Vetterd059f652014-07-25 18:07:40 +0200211 *
212 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
213 * locking, and store the acquire ctx in the corresponding crtc. All other
214 * legacy operations take all locks and use a global acquire context. This
215 * function grabs the right one.
216 */
217struct drm_modeset_acquire_ctx *
218drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
219{
220 if (crtc->acquire_ctx)
221 return crtc->acquire_ctx;
222
223 WARN_ON(!crtc->dev->mode_config.acquire_ctx);
224
225 return crtc->dev->mode_config.acquire_ctx;
226}
227EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
228
229/**
230 * drm_modeset_unlock_crtc - drop crtc lock
231 * @crtc: drm crtc
232 *
233 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
234 * locks acquired through the hidden context.
235 */
236void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
237{
238 struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
239
240 if (WARN_ON(!ctx))
241 return;
242
243 crtc->acquire_ctx = NULL;
244 drm_modeset_drop_locks(ctx);
245 drm_modeset_acquire_fini(ctx);
246
247 kfree(ctx);
248}
249EXPORT_SYMBOL(drm_modeset_unlock_crtc);
250
251/**
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200252 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
253 * @dev: device
254 *
255 * Useful as a debug assert.
256 */
257void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
258{
259 struct drm_crtc *crtc;
260
261 /* Locking is currently fubar in the panic handler. */
262 if (oops_in_progress)
263 return;
264
Daniel Vettere4f62542015-07-09 23:44:35 +0200265 drm_for_each_crtc(crtc, dev)
Daniel Vettera6a8bb82014-07-25 17:47:18 +0200266 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
267
268 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
269 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
270}
271EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
272
273/**
Rob Clark51fd3712013-11-19 12:10:12 -0500274 * drm_modeset_acquire_init - initialize acquire context
275 * @ctx: the acquire context
276 * @flags: for future
277 */
278void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
279 uint32_t flags)
280{
Rob Clarkfb549182014-06-07 10:55:39 -0400281 memset(ctx, 0, sizeof(*ctx));
Rob Clark51fd3712013-11-19 12:10:12 -0500282 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
283 INIT_LIST_HEAD(&ctx->locked);
284}
285EXPORT_SYMBOL(drm_modeset_acquire_init);
286
287/**
288 * drm_modeset_acquire_fini - cleanup acquire context
289 * @ctx: the acquire context
290 */
291void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
292{
293 ww_acquire_fini(&ctx->ww_ctx);
294}
295EXPORT_SYMBOL(drm_modeset_acquire_fini);
296
297/**
298 * drm_modeset_drop_locks - drop all locks
299 * @ctx: the acquire context
300 *
301 * Drop all locks currently held against this acquire context.
302 */
303void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
304{
305 WARN_ON(ctx->contended);
306 while (!list_empty(&ctx->locked)) {
307 struct drm_modeset_lock *lock;
308
309 lock = list_first_entry(&ctx->locked,
310 struct drm_modeset_lock, head);
311
312 drm_modeset_unlock(lock);
313 }
314}
315EXPORT_SYMBOL(drm_modeset_drop_locks);
316
317static inline int modeset_lock(struct drm_modeset_lock *lock,
318 struct drm_modeset_acquire_ctx *ctx,
319 bool interruptible, bool slow)
320{
321 int ret;
322
323 WARN_ON(ctx->contended);
324
Daniel Vettercb597bb2014-07-27 19:09:33 +0200325 if (ctx->trylock_only) {
Maarten Lankhorst825926d2015-08-27 13:58:09 +0200326 lockdep_assert_held(&ctx->ww_ctx);
327
Daniel Vettercb597bb2014-07-27 19:09:33 +0200328 if (!ww_mutex_trylock(&lock->mutex))
329 return -EBUSY;
330 else
331 return 0;
332 } else if (interruptible && slow) {
Rob Clark51fd3712013-11-19 12:10:12 -0500333 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
334 } else if (interruptible) {
335 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
336 } else if (slow) {
337 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
338 ret = 0;
339 } else {
340 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
341 }
342 if (!ret) {
343 WARN_ON(!list_empty(&lock->head));
344 list_add(&lock->head, &ctx->locked);
345 } else if (ret == -EALREADY) {
346 /* we already hold the lock.. this is fine. For atomic
347 * we will need to be able to drm_modeset_lock() things
348 * without having to keep track of what is already locked
349 * or not.
350 */
351 ret = 0;
352 } else if (ret == -EDEADLK) {
353 ctx->contended = lock;
354 }
355
356 return ret;
357}
358
359static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
360 bool interruptible)
361{
362 struct drm_modeset_lock *contended = ctx->contended;
363
364 ctx->contended = NULL;
365
366 if (WARN_ON(!contended))
367 return 0;
368
369 drm_modeset_drop_locks(ctx);
370
371 return modeset_lock(contended, ctx, interruptible, true);
372}
373
374/**
375 * drm_modeset_backoff - deadlock avoidance backoff
376 * @ctx: the acquire context
377 *
378 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
379 * you must call this function to drop all currently held locks and
380 * block until the contended lock becomes available.
381 */
382void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
383{
384 modeset_backoff(ctx, false);
385}
386EXPORT_SYMBOL(drm_modeset_backoff);
387
388/**
389 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
390 * @ctx: the acquire context
391 *
392 * Interruptible version of drm_modeset_backoff()
393 */
394int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
395{
396 return modeset_backoff(ctx, true);
397}
398EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
399
400/**
401 * drm_modeset_lock - take modeset lock
402 * @lock: lock to take
403 * @ctx: acquire ctx
404 *
405 * If ctx is not NULL, then its ww acquire context is used and the
406 * lock will be tracked by the context and can be released by calling
407 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
408 * deadlock scenario has been detected and it is an error to attempt
409 * to take any more locks without first calling drm_modeset_backoff().
410 */
411int drm_modeset_lock(struct drm_modeset_lock *lock,
412 struct drm_modeset_acquire_ctx *ctx)
413{
414 if (ctx)
415 return modeset_lock(lock, ctx, false, false);
416
417 ww_mutex_lock(&lock->mutex, NULL);
418 return 0;
419}
420EXPORT_SYMBOL(drm_modeset_lock);
421
422/**
423 * drm_modeset_lock_interruptible - take modeset lock
424 * @lock: lock to take
425 * @ctx: acquire ctx
426 *
427 * Interruptible version of drm_modeset_lock()
428 */
429int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
430 struct drm_modeset_acquire_ctx *ctx)
431{
432 if (ctx)
433 return modeset_lock(lock, ctx, true, false);
434
435 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
436}
437EXPORT_SYMBOL(drm_modeset_lock_interruptible);
438
439/**
440 * drm_modeset_unlock - drop modeset lock
441 * @lock: lock to release
442 */
443void drm_modeset_unlock(struct drm_modeset_lock *lock)
444{
445 list_del_init(&lock->head);
446 ww_mutex_unlock(&lock->mutex);
447}
448EXPORT_SYMBOL(drm_modeset_unlock);
449
Thierry Reding06eaae42015-12-02 17:50:03 +0100450/**
451 * drm_modeset_lock_all_ctx - take all modeset locks
452 * @dev: DRM device
453 * @ctx: lock acquisition context
454 *
455 * This function takes all modeset locks, suitable where a more fine-grained
456 * scheme isn't (yet) implemented.
457 *
458 * Unlike drm_modeset_lock_all(), it doesn't take the dev->mode_config.mutex
459 * since that lock isn't required for modeset state changes. Callers which
460 * need to grab that lock too need to do so outside of the acquire context
461 * @ctx.
462 *
463 * Locks acquired with this function should be released by calling the
464 * drm_modeset_drop_locks() function on @ctx.
465 *
466 * Returns: 0 on success or a negative error-code on failure.
467 */
468int drm_modeset_lock_all_ctx(struct drm_device *dev,
469 struct drm_modeset_acquire_ctx *ctx)
Rob Clark51fd3712013-11-19 12:10:12 -0500470{
Rob Clark51fd3712013-11-19 12:10:12 -0500471 struct drm_crtc *crtc;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100472 struct drm_plane *plane;
Thierry Reding06eaae42015-12-02 17:50:03 +0100473 int ret;
474
475 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
476 if (ret)
477 return ret;
Rob Clark51fd3712013-11-19 12:10:12 -0500478
Daniel Vettere4f62542015-07-09 23:44:35 +0200479 drm_for_each_crtc(crtc, dev) {
Rob Clark51fd3712013-11-19 12:10:12 -0500480 ret = drm_modeset_lock(&crtc->mutex, ctx);
481 if (ret)
482 return ret;
483 }
484
Daniel Vettere4f62542015-07-09 23:44:35 +0200485 drm_for_each_plane(plane, dev) {
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100486 ret = drm_modeset_lock(&plane->mutex, ctx);
487 if (ret)
488 return ret;
489 }
490
Rob Clark51fd3712013-11-19 12:10:12 -0500491 return 0;
492}
Thierry Reding06eaae42015-12-02 17:50:03 +0100493EXPORT_SYMBOL(drm_modeset_lock_all_ctx);