drm: Move ->old_fb from crtc to plane
[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 *
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
a6a8bb84
DV
59/**
60 * 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
d059f652
DV
132/**
133 * drm_modeset_lock_crtc - lock crtc with hidden acquire ctx
134 * @crtc: drm crtc
135 *
136 * This function locks the given crtc using a hidden acquire context. This is
137 * necessary so that drivers internally using the atomic interfaces can grab
138 * further locks with the lock acquire context.
139 */
140void drm_modeset_lock_crtc(struct drm_crtc *crtc)
141{
142 struct drm_modeset_acquire_ctx *ctx;
143 int ret;
144
145 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
146 if (WARN_ON(!ctx))
147 return;
148
149 drm_modeset_acquire_init(ctx, 0);
150
151retry:
152 ret = drm_modeset_lock(&crtc->mutex, ctx);
153 if (ret)
154 goto fail;
155
156 WARN_ON(crtc->acquire_ctx);
157
158 /* now we hold the locks, so now that it is safe, stash the
159 * ctx for drm_modeset_unlock_crtc():
160 */
161 crtc->acquire_ctx = ctx;
162
163 return;
164
165fail:
166 if (ret == -EDEADLK) {
167 drm_modeset_backoff(ctx);
168 goto retry;
169 }
170}
171EXPORT_SYMBOL(drm_modeset_lock_crtc);
172
173/**
174 * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
175 * crtc: drm crtc
176 *
177 * Legacy ioctl operations like cursor updates or page flips only have per-crtc
178 * locking, and store the acquire ctx in the corresponding crtc. All other
179 * legacy operations take all locks and use a global acquire context. This
180 * function grabs the right one.
181 */
182struct drm_modeset_acquire_ctx *
183drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc)
184{
185 if (crtc->acquire_ctx)
186 return crtc->acquire_ctx;
187
188 WARN_ON(!crtc->dev->mode_config.acquire_ctx);
189
190 return crtc->dev->mode_config.acquire_ctx;
191}
192EXPORT_SYMBOL(drm_modeset_legacy_acquire_ctx);
193
194/**
195 * drm_modeset_unlock_crtc - drop crtc lock
196 * @crtc: drm crtc
197 *
198 * This drops the crtc lock acquire with drm_modeset_lock_crtc() and all other
199 * locks acquired through the hidden context.
200 */
201void drm_modeset_unlock_crtc(struct drm_crtc *crtc)
202{
203 struct drm_modeset_acquire_ctx *ctx = crtc->acquire_ctx;
204
205 if (WARN_ON(!ctx))
206 return;
207
208 crtc->acquire_ctx = NULL;
209 drm_modeset_drop_locks(ctx);
210 drm_modeset_acquire_fini(ctx);
211
212 kfree(ctx);
213}
214EXPORT_SYMBOL(drm_modeset_unlock_crtc);
215
a6a8bb84
DV
216/**
217 * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
218 * @dev: device
219 *
220 * Useful as a debug assert.
221 */
222void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
223{
224 struct drm_crtc *crtc;
225
226 /* Locking is currently fubar in the panic handler. */
227 if (oops_in_progress)
228 return;
229
230 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
231 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
232
233 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
234 WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
235}
236EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
237
51fd371b
RC
238/**
239 * drm_modeset_acquire_init - initialize acquire context
240 * @ctx: the acquire context
241 * @flags: for future
242 */
243void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
244 uint32_t flags)
245{
fb54918a 246 memset(ctx, 0, sizeof(*ctx));
51fd371b
RC
247 ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
248 INIT_LIST_HEAD(&ctx->locked);
249}
250EXPORT_SYMBOL(drm_modeset_acquire_init);
251
252/**
253 * drm_modeset_acquire_fini - cleanup acquire context
254 * @ctx: the acquire context
255 */
256void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
257{
258 ww_acquire_fini(&ctx->ww_ctx);
259}
260EXPORT_SYMBOL(drm_modeset_acquire_fini);
261
262/**
263 * drm_modeset_drop_locks - drop all locks
264 * @ctx: the acquire context
265 *
266 * Drop all locks currently held against this acquire context.
267 */
268void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx)
269{
270 WARN_ON(ctx->contended);
271 while (!list_empty(&ctx->locked)) {
272 struct drm_modeset_lock *lock;
273
274 lock = list_first_entry(&ctx->locked,
275 struct drm_modeset_lock, head);
276
277 drm_modeset_unlock(lock);
278 }
279}
280EXPORT_SYMBOL(drm_modeset_drop_locks);
281
282static inline int modeset_lock(struct drm_modeset_lock *lock,
283 struct drm_modeset_acquire_ctx *ctx,
284 bool interruptible, bool slow)
285{
286 int ret;
287
288 WARN_ON(ctx->contended);
289
290 if (interruptible && slow) {
291 ret = ww_mutex_lock_slow_interruptible(&lock->mutex, &ctx->ww_ctx);
292 } else if (interruptible) {
293 ret = ww_mutex_lock_interruptible(&lock->mutex, &ctx->ww_ctx);
294 } else if (slow) {
295 ww_mutex_lock_slow(&lock->mutex, &ctx->ww_ctx);
296 ret = 0;
297 } else {
298 ret = ww_mutex_lock(&lock->mutex, &ctx->ww_ctx);
299 }
300 if (!ret) {
301 WARN_ON(!list_empty(&lock->head));
302 list_add(&lock->head, &ctx->locked);
303 } else if (ret == -EALREADY) {
304 /* we already hold the lock.. this is fine. For atomic
305 * we will need to be able to drm_modeset_lock() things
306 * without having to keep track of what is already locked
307 * or not.
308 */
309 ret = 0;
310 } else if (ret == -EDEADLK) {
311 ctx->contended = lock;
312 }
313
314 return ret;
315}
316
317static int modeset_backoff(struct drm_modeset_acquire_ctx *ctx,
318 bool interruptible)
319{
320 struct drm_modeset_lock *contended = ctx->contended;
321
322 ctx->contended = NULL;
323
324 if (WARN_ON(!contended))
325 return 0;
326
327 drm_modeset_drop_locks(ctx);
328
329 return modeset_lock(contended, ctx, interruptible, true);
330}
331
332/**
333 * drm_modeset_backoff - deadlock avoidance backoff
334 * @ctx: the acquire context
335 *
336 * If deadlock is detected (ie. drm_modeset_lock() returns -EDEADLK),
337 * you must call this function to drop all currently held locks and
338 * block until the contended lock becomes available.
339 */
340void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
341{
342 modeset_backoff(ctx, false);
343}
344EXPORT_SYMBOL(drm_modeset_backoff);
345
346/**
347 * drm_modeset_backoff_interruptible - deadlock avoidance backoff
348 * @ctx: the acquire context
349 *
350 * Interruptible version of drm_modeset_backoff()
351 */
352int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
353{
354 return modeset_backoff(ctx, true);
355}
356EXPORT_SYMBOL(drm_modeset_backoff_interruptible);
357
358/**
359 * drm_modeset_lock - take modeset lock
360 * @lock: lock to take
361 * @ctx: acquire ctx
362 *
363 * If ctx is not NULL, then its ww acquire context is used and the
364 * lock will be tracked by the context and can be released by calling
365 * drm_modeset_drop_locks(). If -EDEADLK is returned, this means a
366 * deadlock scenario has been detected and it is an error to attempt
367 * to take any more locks without first calling drm_modeset_backoff().
368 */
369int drm_modeset_lock(struct drm_modeset_lock *lock,
370 struct drm_modeset_acquire_ctx *ctx)
371{
372 if (ctx)
373 return modeset_lock(lock, ctx, false, false);
374
375 ww_mutex_lock(&lock->mutex, NULL);
376 return 0;
377}
378EXPORT_SYMBOL(drm_modeset_lock);
379
380/**
381 * drm_modeset_lock_interruptible - take modeset lock
382 * @lock: lock to take
383 * @ctx: acquire ctx
384 *
385 * Interruptible version of drm_modeset_lock()
386 */
387int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
388 struct drm_modeset_acquire_ctx *ctx)
389{
390 if (ctx)
391 return modeset_lock(lock, ctx, true, false);
392
393 return ww_mutex_lock_interruptible(&lock->mutex, NULL);
394}
395EXPORT_SYMBOL(drm_modeset_lock_interruptible);
396
397/**
398 * drm_modeset_unlock - drop modeset lock
399 * @lock: lock to release
400 */
401void drm_modeset_unlock(struct drm_modeset_lock *lock)
402{
403 list_del_init(&lock->head);
404 ww_mutex_unlock(&lock->mutex);
405}
406EXPORT_SYMBOL(drm_modeset_unlock);
407
408/* Temporary.. until we have sufficiently fine grained locking, there
409 * are a couple scenarios where it is convenient to grab all crtc locks.
410 * It is planned to remove this:
411 */
412int drm_modeset_lock_all_crtcs(struct drm_device *dev,
413 struct drm_modeset_acquire_ctx *ctx)
414{
415 struct drm_mode_config *config = &dev->mode_config;
416 struct drm_crtc *crtc;
417 int ret = 0;
418
419 list_for_each_entry(crtc, &config->crtc_list, head) {
420 ret = drm_modeset_lock(&crtc->mutex, ctx);
421 if (ret)
422 return ret;
423 }
424
425 return 0;
426}
427EXPORT_SYMBOL(drm_modeset_lock_all_crtcs);
This page took 0.087566 seconds and 5 git commands to generate.