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