drm/linux: Add sign_extend64()
[dragonfly.git] / sys / dev / drm / drm_modeset_lock.c
blobd747d3c181d984bb355930d9aec49f8f2f63df48
1 /*
2 * Copyright (C) 2014 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 #include <drm/drmP.h>
25 #include <drm/drm_crtc.h>
26 #include <drm/drm_modeset_lock.h>
28 /**
29 * DOC: kms locking
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.
38 * For basic principles of ww_mutex, see: Documentation/locking/ww-mutex-design.txt
40 * The basic usage pattern is to:
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 * }
52 * ... do stuff ...
54 * drm_modeset_drop_locks(&ctx);
55 * drm_modeset_acquire_fini(&ctx);
58 /**
59 * drm_modeset_lock_all - take all modeset locks
60 * @dev: drm device
62 * This function takes all modeset locks, suitable where a more fine-grained
63 * scheme isn't (yet) implemented. Locks must be dropped with
64 * drm_modeset_unlock_all.
66 void drm_modeset_lock_all(struct drm_device *dev)
68 struct drm_mode_config *config = &dev->mode_config;
69 struct drm_modeset_acquire_ctx *ctx;
70 int ret;
72 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
73 if (WARN_ON(!ctx))
74 return;
76 mutex_lock(&config->mutex);
78 drm_modeset_acquire_init(ctx, 0);
80 retry:
81 ret = drm_modeset_lock(&config->connection_mutex, ctx);
82 if (ret)
83 goto fail;
84 ret = drm_modeset_lock_all_crtcs(dev, ctx);
85 if (ret)
86 goto fail;
88 WARN_ON(config->acquire_ctx);
90 /* now we hold the locks, so now that it is safe, stash the
91 * ctx for drm_modeset_unlock_all():
93 config->acquire_ctx = ctx;
95 drm_warn_on_modeset_not_all_locked(dev);
97 return;
99 fail:
100 if (ret == -EDEADLK) {
101 drm_modeset_backoff(ctx);
102 goto retry;
105 kfree(ctx);
107 EXPORT_SYMBOL(drm_modeset_lock_all);
110 * drm_modeset_unlock_all - drop all modeset locks
111 * @dev: device
113 * This function drop all modeset locks taken by drm_modeset_lock_all.
115 void drm_modeset_unlock_all(struct drm_device *dev)
117 struct drm_mode_config *config = &dev->mode_config;
118 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
120 if (WARN_ON(!ctx))
121 return;
123 config->acquire_ctx = NULL;
124 drm_modeset_drop_locks(ctx);
125 drm_modeset_acquire_fini(ctx);
127 kfree(ctx);
129 mutex_unlock(&dev->mode_config.mutex);
131 EXPORT_SYMBOL(drm_modeset_unlock_all);
134 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx for a plane update
135 * @crtc: DRM CRTC
136 * @plane: DRM plane to be updated on @crtc
138 * This function locks the given crtc and plane (which should be either the
139 * primary or cursor plane) using a hidden acquire context. This is necessary so
140 * that drivers internally using the atomic interfaces can grab further locks
141 * with the lock acquire context.
143 * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
144 * converted to universal planes yet.
146 void drm_modeset_lock_crtc(struct drm_crtc *crtc,
147 struct drm_plane *plane)
149 struct drm_modeset_acquire_ctx *ctx;
150 int ret;
152 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
153 if (WARN_ON(!ctx))
154 return;
156 drm_modeset_acquire_init(ctx, 0);
158 retry:
159 ret = drm_modeset_lock(&crtc->mutex, ctx);
160 if (ret)
161 goto fail;
163 if (plane) {
164 ret = drm_modeset_lock(&plane->mutex, ctx);
165 if (ret)
166 goto fail;
168 if (plane->crtc) {
169 ret = drm_modeset_lock(&plane->crtc->mutex, ctx);
170 if (ret)
171 goto fail;
175 WARN_ON(crtc->acquire_ctx);
177 /* now we hold the locks, so now that it is safe, stash the
178 * ctx for drm_modeset_unlock_crtc():
180 crtc->acquire_ctx = ctx;
182 return;
184 fail:
185 if (ret == -EDEADLK) {
186 drm_modeset_backoff(ctx);
187 goto retry;
190 EXPORT_SYMBOL(drm_modeset_lock_crtc);
193 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
194 * @crtc: drm crtc
196 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
197 * locking, and store the acquire ctx in the corresponding crtc. All other
198 * legacy operations take all locks and use a global acquire context. This
199 * function grabs the right one.
201 struct drm_modeset_acquire_ctx *
202 drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
204 if (crtc->acquire_ctx)
205 return crtc->acquire_ctx;
207 WARN_ON(!crtc->dev->mode_config.acquire_ctx);
209 return crtc->dev->mode_config.acquire_ctx;
211 EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
214 * drm_modeset_unlock_crtc - drop crtc lock
215 * @crtc: drm crtc
217 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
218 * locks acquired through the hidden context.
220 void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
222 struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
224 if (WARN_ON(!ctx))
225 return;
227 crtc->acquire_ctx = NULL;
228 drm_modeset_drop_locks(ctx);
229 drm_modeset_acquire_fini(ctx);
231 kfree(ctx);
233 EXPORT_SYMBOL(drm_modeset_unlock_crtc);
236 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
237 * @dev: device
239 * Useful as a debug assert.
241 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
243 struct drm_crtc *crtc;
245 /* Locking is currently fubar in the panic handler. */
246 #if 0
247 if (oops_in_progress)
248 return;
249 #endif
251 drm_for_each_crtc(crtc, dev)
252 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
254 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
255 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
257 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
260 * drm_modeset_acquire_init - initialize acquire context
261 * @ctx: the acquire context
262 * @flags: for future
264 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
265 uint32_t flags)
267 memset(ctx, 0, sizeof(*ctx));
268 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
269 INIT_LIST_HEAD(&ctx->locked);
271 EXPORT_SYMBOL(drm_modeset_acquire_init);
274 * drm_modeset_acquire_fini - cleanup acquire context
275 * @ctx: the acquire context
277 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
279 ww_acquire_fini(&ctx->ww_ctx);
281 EXPORT_SYMBOL(drm_modeset_acquire_fini);
284 * drm_modeset_drop_locks - drop all locks
285 * @ctx: the acquire context
287 * Drop all locks currently held against this acquire context.
289 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
291 WARN_ON(ctx->contended);
292 while (!list_empty(&ctx->locked)) {
293 struct drm_modeset_lock_info *info;
295 info = list_first_entry(&ctx->locked,
296 struct drm_modeset_lock_info, ctx_entry);
298 drm_modeset_unlock(info->lock);
301 EXPORT_SYMBOL(drm_modeset_drop_locks);
303 static inline int modeset_lock(struct drm_modeset_lock *lock,
304 struct drm_modeset_acquire_ctx *ctx,
305 bool interruptible, bool slow)
307 int ret;
309 WARN_ON(ctx->contended);
311 if (ctx->trylock_only) {
312 #if 0
313 lockdep_assert_held(&ctx->ww_ctx);
314 #endif
316 if (!ww_mutex_trylock(&lock->mutex))
317 return -EBUSY;
318 else
319 return 0;
320 } else if (interruptible && slow) {
321 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
322 } else if (interruptible) {
323 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
324 } else if (slow) {
325 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
326 ret = 0;
327 } else {
328 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
330 if (ret == -EALREADY) {
331 /* we already hold the lock.. this is fine. For atomic
332 * we will need to be able to drm_modeset_lock() things
333 * without having to keep track of what is already locked
334 * or not.
336 ret = 0;
337 } else if (ret == -EDEADLK) {
338 ctx->contended = lock;
340 if (ret == 0) {
341 struct drm_modeset_lock_info *info;
343 info = kzalloc(sizeof(*info), GFP_KERNEL);
344 INIT_LIST_HEAD(&info->ctx_entry);
345 INIT_LIST_HEAD(&info->lock_entry);
346 info->lock = lock;
347 info->ctx = ctx;
348 list_add(&info->ctx_entry, &ctx->locked);
349 list_add(&info->lock_entry, &lock->locked);
352 return ret;
355 static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
356 bool interruptible)
358 struct drm_modeset_lock *contended = ctx->contended;
360 ctx->contended = NULL;
362 if (WARN_ON(!contended))
363 return 0;
365 drm_modeset_drop_locks(ctx);
367 return modeset_lock(contended, ctx, interruptible, true);
371 * drm_modeset_backoff - deadlock avoidance backoff
372 * @ctx: the acquire context
374 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
375 * you must call this function to drop all currently held locks and
376 * block until the contended lock becomes available.
378 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
380 modeset_backoff(ctx, false);
382 EXPORT_SYMBOL(drm_modeset_backoff);
385 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
386 * @ctx: the acquire context
388 * Interruptible version of drm_modeset_backoff()
390 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
392 return modeset_backoff(ctx, true);
394 EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
397 * drm_modeset_lock - take modeset lock
398 * @lock: lock to take
399 * @ctx: acquire ctx
401 * If ctx is not NULL, then its ww acquire context is used and the
402 * lock will be tracked by the context and can be released by calling
403 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
404 * deadlock scenario has been detected and it is an error to attempt
405 * to take any more locks without first calling drm_modeset_backoff().
407 int drm_modeset_lock(struct drm_modeset_lock *lock,
408 struct drm_modeset_acquire_ctx *ctx)
410 if (ctx)
411 return modeset_lock(lock, ctx, false, false);
413 ww_mutex_lock(&lock->mutex, NULL);
414 return 0;
416 EXPORT_SYMBOL(drm_modeset_lock);
419 * drm_modeset_lock_interruptible - take modeset lock
420 * @lock: lock to take
421 * @ctx: acquire ctx
423 * Interruptible version of drm_modeset_lock()
425 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
426 struct drm_modeset_acquire_ctx *ctx)
428 if (ctx)
429 return modeset_lock(lock, ctx, true, false);
431 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
433 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
436 * drm_modeset_unlock - drop modeset lock
437 * @lock: lock to release
439 void drm_modeset_unlock(struct drm_modeset_lock *lock)
441 struct drm_modeset_lock_info *info;
443 /* undo in reverse order */
444 if (!list_empty(&lock->locked)) {
445 info = list_last_entry(&lock->locked,
446 struct drm_modeset_lock_info, lock_entry);
447 list_del_init(&info->lock_entry);
448 if (info->ctx)
449 list_del_init(&info->ctx_entry);
450 kfree(info);
452 ww_mutex_unlock(&lock->mutex);
454 EXPORT_SYMBOL(drm_modeset_unlock);
456 /* In some legacy codepaths it's convenient to just grab all the crtc and plane
457 * related locks. */
458 int drm_modeset_lock_all_crtcs(struct drm_device *dev,
459 struct drm_modeset_acquire_ctx *ctx)
461 struct drm_crtc *crtc;
462 struct drm_plane *plane;
463 int ret = 0;
465 drm_for_each_crtc(crtc, dev) {
466 ret = drm_modeset_lock(&crtc->mutex, ctx);
467 if (ret)
468 return ret;
471 drm_for_each_plane(plane, dev) {
472 ret = drm_modeset_lock(&plane->mutex, ctx);
473 if (ret)
474 return ret;
477 return 0;
479 EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);