drm/exynos: dp: add of_graph dt binding support for panel
[deliverable/linux.git] / drivers / gpu / drm / drm_modeset_lock.c
CommitLineData
51fd371b
RC
1/*
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 *
214e0aed 38 * For basic principles of ww_mutex, see: Documentation/locking/ww-mutex-design.txt
51fd371b
RC
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
a6a8bb84 58/**
bf9e37ba 59 * drm_modeset_lock_all - take all modeset locks
06eaae46 60 * @dev: DRM device
cb597bb3 61 *
bf9e37ba 62 * This function takes all modeset locks, suitable where a more fine-grained
06eaae46
TR
63 * scheme isn't (yet) implemented. Locks must be dropped by calling the
64 * drm_modeset_unlock_all() function.
65 *
66 * This function is deprecated. It allocates a lock acquisition context and
67 * stores it in the DRM device's ->mode_config. This facilitate conversion of
68 * existing code because it removes the need to manually deal with the
69 * acquisition context, but it is also brittle because the context is global
70 * and care must be taken not to nest calls. New code should use the
71 * drm_modeset_lock_all_ctx() function and pass in the context explicitly.
a6a8bb84 72 */
bf9e37ba 73void drm_modeset_lock_all(struct drm_device *dev)
a6a8bb84
DV
74{
75 struct drm_mode_config *config = &dev->mode_config;
76 struct drm_modeset_acquire_ctx *ctx;
77 int ret;
78
bf9e37ba
DV
79 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
80 if (WARN_ON(!ctx))
81 return;
82
83 mutex_lock(&config->mutex);
a6a8bb84
DV
84
85 drm_modeset_acquire_init(ctx, 0);
86
87retry:
06eaae46
TR
88 ret = drm_modeset_lock_all_ctx(dev, ctx);
89 if (ret < 0) {
90 if (ret == -EDEADLK) {
91 drm_modeset_backoff(ctx);
92 goto retry;
93 }
94
95 drm_modeset_acquire_fini(ctx);
96 kfree(ctx);
97 return;
98 }
a6a8bb84
DV
99
100 WARN_ON(config->acquire_ctx);
101
06eaae46
TR
102 /*
103 * We hold the locks now, so it is safe to stash the acquisition
104 * context for drm_modeset_unlock_all().
a6a8bb84
DV
105 */
106 config->acquire_ctx = ctx;
107
108 drm_warn_on_modeset_not_all_locked(dev);
a6a8bb84
DV
109}
110EXPORT_SYMBOL(drm_modeset_lock_all);
111
112/**
113 * drm_modeset_unlock_all - drop all modeset locks
06eaae46 114 * @dev: DRM device
a6a8bb84 115 *
06eaae46
TR
116 * This function drops all modeset locks taken by a previous call to the
117 * drm_modeset_lock_all() function.
118 *
119 * This function is deprecated. It uses the lock acquisition context stored
120 * in the DRM device's ->mode_config. This facilitates conversion of existing
121 * code because it removes the need to manually deal with the acquisition
122 * context, but it is also brittle because the context is global and care must
123 * be taken not to nest calls. New code should pass the acquisition context
124 * directly to the drm_modeset_drop_locks() function.
a6a8bb84
DV
125 */
126void drm_modeset_unlock_all(struct drm_device *dev)
127{
128 struct drm_mode_config *config = &dev->mode_config;
129 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
130
131 if (WARN_ON(!ctx))
132 return;
133
134 config->acquire_ctx = NULL;
135 drm_modeset_drop_locks(ctx);
136 drm_modeset_acquire_fini(ctx);
137
138 kfree(ctx);
139
140 mutex_unlock(&dev->mode_config.mutex);
141}
142EXPORT_SYMBOL(drm_modeset_unlock_all);
143
d059f652 144/**
4d02e2de
DV
145 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
146 * @crtc: DRM CRTC
147 * @plane: DRM plane to be updated on @crtc
148 *
149 * This function locks the given crtc and plane (which should be either the
150 * primary or cursor plane) using a hidden acquire context. This is necessary so
151 * that drivers internally using the atomic interfaces can grab further locks
152 * with the lock acquire context.
d059f652 153 *
4d02e2de
DV
154 * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
155 * converted to universal planes yet.
d059f652 156 */
4d02e2de
DV
157void drm_modeset_lock_crtc(struct drm_crtc *crtc,
158 struct drm_plane *plane)
d059f652
DV
159{
160 struct drm_modeset_acquire_ctx *ctx;
161 int ret;
162
163 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
164 if (WARN_ON(!ctx))
165 return;
166
167 drm_modeset_acquire_init(ctx, 0);
168
169retry:
170 ret = drm_modeset_lock(&crtc->mutex, ctx);
171 if (ret)
172 goto fail;
173
4d02e2de
DV
174 if (plane) {
175 ret = drm_modeset_lock(&plane->mutex, ctx);
176 if (ret)
177 goto fail;
178
179 if (plane->crtc) {
180 ret = drm_modeset_lock(&plane->crtc->mutex, ctx);
181 if (ret)
182 goto fail;
183 }
184 }
185
d059f652
DV
186 WARN_ON(crtc->acquire_ctx);
187
188 /* now we hold the locks, so now that it is safe, stash the
189 * ctx for drm_modeset_unlock_crtc():
190 */
191 crtc->acquire_ctx = ctx;
192
193 return;
194
195fail:
196 if (ret == -EDEADLK) {
197 drm_modeset_backoff(ctx);
198 goto retry;
199 }
200}
201EXPORT_SYMBOL(drm_modeset_lock_crtc);
202
203/**
204 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
295ee853 205 * @crtc: drm crtc
d059f652
DV
206 *
207 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
208 * locking, and store the acquire ctx in the corresponding crtc. All other
209 * legacy operations take all locks and use a global acquire context. This
210 * function grabs the right one.
211 */
212struct drm_modeset_acquire_ctx *
213drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
214{
215 if (crtc->acquire_ctx)
216 return crtc->acquire_ctx;
217
218 WARN_ON(!crtc->dev->mode_config.acquire_ctx);
219
220 return crtc->dev->mode_config.acquire_ctx;
221}
222EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
223
224/**
225 * drm_modeset_unlock_crtc - drop crtc lock
226 * @crtc: drm crtc
227 *
228 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
229 * locks acquired through the hidden context.
230 */
231void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
232{
233 struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
234
235 if (WARN_ON(!ctx))
236 return;
237
238 crtc->acquire_ctx = NULL;
239 drm_modeset_drop_locks(ctx);
240 drm_modeset_acquire_fini(ctx);
241
242 kfree(ctx);
243}
244EXPORT_SYMBOL(drm_modeset_unlock_crtc);
245
a6a8bb84
DV
246/**
247 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
248 * @dev: device
249 *
250 * Useful as a debug assert.
251 */
252void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
253{
254 struct drm_crtc *crtc;
255
256 /* Locking is currently fubar in the panic handler. */
257 if (oops_in_progress)
258 return;
259
e4f62546 260 drm_for_each_crtc(crtc, dev)
a6a8bb84
DV
261 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
262
263 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
264 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
265}
266EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
267
51fd371b
RC
268/**
269 * drm_modeset_acquire_init - initialize acquire context
270 * @ctx: the acquire context
271 * @flags: for future
272 */
273void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
274 uint32_t flags)
275{
fb54918a 276 memset(ctx, 0, sizeof(*ctx));
51fd371b
RC
277 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
278 INIT_LIST_HEAD(&ctx->locked);
279}
280EXPORT_SYMBOL(drm_modeset_acquire_init);
281
282/**
283 * drm_modeset_acquire_fini - cleanup acquire context
284 * @ctx: the acquire context
285 */
286void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
287{
288 ww_acquire_fini(&ctx->ww_ctx);
289}
290EXPORT_SYMBOL(drm_modeset_acquire_fini);
291
292/**
293 * drm_modeset_drop_locks - drop all locks
294 * @ctx: the acquire context
295 *
296 * Drop all locks currently held against this acquire context.
297 */
298void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
299{
300 WARN_ON(ctx->contended);
301 while (!list_empty(&ctx->locked)) {
302 struct drm_modeset_lock *lock;
303
304 lock = list_first_entry(&ctx->locked,
305 struct drm_modeset_lock, head);
306
307 drm_modeset_unlock(lock);
308 }
309}
310EXPORT_SYMBOL(drm_modeset_drop_locks);
311
312static inline int modeset_lock(struct drm_modeset_lock *lock,
313 struct drm_modeset_acquire_ctx *ctx,
314 bool interruptible, bool slow)
315{
316 int ret;
317
318 WARN_ON(ctx->contended);
319
cb597bb3 320 if (ctx->trylock_only) {
825926d8
ML
321 lockdep_assert_held(&ctx->ww_ctx);
322
cb597bb3
DV
323 if (!ww_mutex_trylock(&lock->mutex))
324 return -EBUSY;
325 else
326 return 0;
327 } else if (interruptible && slow) {
51fd371b
RC
328 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
329 } else if (interruptible) {
330 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
331 } else if (slow) {
332 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
333 ret = 0;
334 } else {
335 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
336 }
337 if (!ret) {
338 WARN_ON(!list_empty(&lock->head));
339 list_add(&lock->head, &ctx->locked);
340 } else if (ret == -EALREADY) {
341 /* we already hold the lock.. this is fine. For atomic
342 * we will need to be able to drm_modeset_lock() things
343 * without having to keep track of what is already locked
344 * or not.
345 */
346 ret = 0;
347 } else if (ret == -EDEADLK) {
348 ctx->contended = lock;
349 }
350
351 return ret;
352}
353
354static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
355 bool interruptible)
356{
357 struct drm_modeset_lock *contended = ctx->contended;
358
359 ctx->contended = NULL;
360
361 if (WARN_ON(!contended))
362 return 0;
363
364 drm_modeset_drop_locks(ctx);
365
366 return modeset_lock(contended, ctx, interruptible, true);
367}
368
369/**
370 * drm_modeset_backoff - deadlock avoidance backoff
371 * @ctx: the acquire context
372 *
373 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
374 * you must call this function to drop all currently held locks and
375 * block until the contended lock becomes available.
376 */
377void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
378{
379 modeset_backoff(ctx, false);
380}
381EXPORT_SYMBOL(drm_modeset_backoff);
382
383/**
384 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
385 * @ctx: the acquire context
386 *
387 * Interruptible version of drm_modeset_backoff()
388 */
389int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
390{
391 return modeset_backoff(ctx, true);
392}
393EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
394
395/**
396 * drm_modeset_lock - take modeset lock
397 * @lock: lock to take
398 * @ctx: acquire ctx
399 *
400 * If ctx is not NULL, then its ww acquire context is used and the
401 * lock will be tracked by the context and can be released by calling
402 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
403 * deadlock scenario has been detected and it is an error to attempt
404 * to take any more locks without first calling drm_modeset_backoff().
405 */
406int drm_modeset_lock(struct drm_modeset_lock *lock,
407 struct drm_modeset_acquire_ctx *ctx)
408{
409 if (ctx)
410 return modeset_lock(lock, ctx, false, false);
411
412 ww_mutex_lock(&lock->mutex, NULL);
413 return 0;
414}
415EXPORT_SYMBOL(drm_modeset_lock);
416
417/**
418 * drm_modeset_lock_interruptible - take modeset lock
419 * @lock: lock to take
420 * @ctx: acquire ctx
421 *
422 * Interruptible version of drm_modeset_lock()
423 */
424int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
425 struct drm_modeset_acquire_ctx *ctx)
426{
427 if (ctx)
428 return modeset_lock(lock, ctx, true, false);
429
430 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
431}
432EXPORT_SYMBOL(drm_modeset_lock_interruptible);
433
434/**
435 * drm_modeset_unlock - drop modeset lock
436 * @lock: lock to release
437 */
438void drm_modeset_unlock(struct drm_modeset_lock *lock)
439{
440 list_del_init(&lock->head);
441 ww_mutex_unlock(&lock->mutex);
442}
443EXPORT_SYMBOL(drm_modeset_unlock);
444
06eaae46
TR
445/**
446 * drm_modeset_lock_all_ctx - take all modeset locks
447 * @dev: DRM device
448 * @ctx: lock acquisition context
449 *
450 * This function takes all modeset locks, suitable where a more fine-grained
451 * scheme isn't (yet) implemented.
452 *
453 * Unlike drm_modeset_lock_all(), it doesn't take the dev->mode_config.mutex
454 * since that lock isn't required for modeset state changes. Callers which
455 * need to grab that lock too need to do so outside of the acquire context
456 * @ctx.
457 *
458 * Locks acquired with this function should be released by calling the
459 * drm_modeset_drop_locks() function on @ctx.
460 *
461 * Returns: 0 on success or a negative error-code on failure.
462 */
463int drm_modeset_lock_all_ctx(struct drm_device *dev,
464 struct drm_modeset_acquire_ctx *ctx)
51fd371b 465{
51fd371b 466 struct drm_crtc *crtc;
4d02e2de 467 struct drm_plane *plane;
06eaae46
TR
468 int ret;
469
470 ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
471 if (ret)
472 return ret;
51fd371b 473
e4f62546 474 drm_for_each_crtc(crtc, dev) {
51fd371b
RC
475 ret = drm_modeset_lock(&crtc->mutex, ctx);
476 if (ret)
477 return ret;
478 }
479
e4f62546 480 drm_for_each_plane(plane, dev) {
4d02e2de
DV
481 ret = drm_modeset_lock(&plane->mutex, ctx);
482 if (ret)
483 return ret;
484 }
485
51fd371b
RC
486 return 0;
487}
06eaae46 488EXPORT_SYMBOL(drm_modeset_lock_all_ctx);
This page took 0.09834 seconds and 5 git commands to generate.