drm/rockchip: add rk3399 vop big csc support
[firefly-linux-kernel-4.4.55.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 drm_modeset_lock / 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  *
52  *     ... do stuff ...
53  *
54  *     drm_modeset_drop_locks(&ctx);
55  *     drm_modeset_acquire_fini(&ctx);
56  */
57
58 /**
59  * drm_modeset_lock_all - take all modeset locks
60  * @dev: DRM device
61  *
62  * This function takes all modeset locks, suitable where a more fine-grained
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.
72  */
73 void drm_modeset_lock_all(struct drm_device *dev)
74 {
75         struct drm_mode_config *config = &dev->mode_config;
76         struct drm_modeset_acquire_ctx *ctx;
77         int ret;
78
79         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
80         if (WARN_ON(!ctx))
81                 return;
82
83         mutex_lock(&config->mutex);
84
85         drm_modeset_acquire_init(ctx, 0);
86
87 retry:
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         }
99
100         WARN_ON(config->acquire_ctx);
101
102         /*
103          * We hold the locks now, so it is safe to stash the acquisition
104          * context for drm_modeset_unlock_all().
105          */
106         config->acquire_ctx = ctx;
107
108         drm_warn_on_modeset_not_all_locked(dev);
109 }
110 EXPORT_SYMBOL(drm_modeset_lock_all);
111
112 /**
113  * drm_modeset_unlock_all - drop all modeset locks
114  * @dev: DRM device
115  *
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.
125  */
126 void 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 }
142 EXPORT_SYMBOL(drm_modeset_unlock_all);
143
144 /**
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.
153  *
154  * Note that @plane can be NULL, e.g. when the cursor support hasn't yet been
155  * converted to universal planes yet.
156  */
157 void drm_modeset_lock_crtc(struct drm_crtc *crtc,
158                            struct drm_plane *plane)
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
169 retry:
170         ret = drm_modeset_lock(&crtc->mutex, ctx);
171         if (ret)
172                 goto fail;
173
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
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
195 fail:
196         if (ret == -EDEADLK) {
197                 drm_modeset_backoff(ctx);
198                 goto retry;
199         }
200 }
201 EXPORT_SYMBOL(drm_modeset_lock_crtc);
202
203 /**
204  * drm_modeset_legacy_acquire_ctx - find acquire ctx for legacy ioctls
205  * @crtc: drm crtc
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  */
212 struct drm_modeset_acquire_ctx *
213 drm_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 }
222 EXPORT_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  */
231 void 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 }
244 EXPORT_SYMBOL(drm_modeset_unlock_crtc);
245
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  */
252 void 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
260         drm_for_each_crtc(crtc, dev)
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 }
266 EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
267
268 /**
269  * drm_modeset_acquire_init - initialize acquire context
270  * @ctx: the acquire context
271  * @flags: for future
272  */
273 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
274                 uint32_t flags)
275 {
276         memset(ctx, 0, sizeof(*ctx));
277         ww_acquire_init(&ctx->ww_ctx, &crtc_ww_class);
278         INIT_LIST_HEAD(&ctx->locked);
279 }
280 EXPORT_SYMBOL(drm_modeset_acquire_init);
281
282 /**
283  * drm_modeset_acquire_fini - cleanup acquire context
284  * @ctx: the acquire context
285  */
286 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx)
287 {
288         ww_acquire_fini(&ctx->ww_ctx);
289 }
290 EXPORT_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  */
298 void 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 }
310 EXPORT_SYMBOL(drm_modeset_drop_locks);
311
312 static 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
320         if (ctx->trylock_only) {
321                 lockdep_assert_held(&ctx->ww_ctx);
322
323                 if (!ww_mutex_trylock(&lock->mutex))
324                         return -EBUSY;
325                 else
326                         return 0;
327         } else if (interruptible && slow) {
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
354 static 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  */
377 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx)
378 {
379         modeset_backoff(ctx, false);
380 }
381 EXPORT_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  */
389 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx)
390 {
391         return modeset_backoff(ctx, true);
392 }
393 EXPORT_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  */
406 int 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 }
415 EXPORT_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  */
424 int 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 }
432 EXPORT_SYMBOL(drm_modeset_lock_interruptible);
433
434 /**
435  * drm_modeset_unlock - drop modeset lock
436  * @lock: lock to release
437  */
438 void drm_modeset_unlock(struct drm_modeset_lock *lock)
439 {
440         list_del_init(&lock->head);
441         ww_mutex_unlock(&lock->mutex);
442 }
443 EXPORT_SYMBOL(drm_modeset_unlock);
444
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  */
463 int drm_modeset_lock_all_ctx(struct drm_device *dev,
464                              struct drm_modeset_acquire_ctx *ctx)
465 {
466         struct drm_crtc *crtc;
467         struct drm_plane *plane;
468         int ret;
469
470         ret = drm_modeset_lock(&dev->mode_config.connection_mutex, ctx);
471         if (ret)
472                 return ret;
473
474         drm_for_each_crtc(crtc, dev) {
475                 ret = drm_modeset_lock(&crtc->mutex, ctx);
476                 if (ret)
477                         return ret;
478         }
479
480         drm_for_each_plane(plane, dev) {
481                 ret = drm_modeset_lock(&plane->mutex, ctx);
482                 if (ret)
483                         return ret;
484         }
485
486         return 0;
487 }
488 EXPORT_SYMBOL(drm_modeset_lock_all_ctx);