drm/linux: Implement more kobject parts
[dragonfly.git] / sys / dev / drm / include / drm / drm_modeset_lock.h
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 #ifndef DRM_MODESET_LOCK_H_
25 #define DRM_MODESET_LOCK_H_
26
27 #include <linux/ww_mutex.h>
28
29 struct drm_modeset_lock;
30
31 /**
32  * struct drm_modeset_acquire_ctx - locking context (see ww_acquire_ctx)
33  * @ww_ctx: base acquire ctx
34  * @contended: used internally for -EDEADLK handling
35  * @locked: list of held locks
36  * @trylock_only: trylock mode used in atomic contexts/panic notifiers
37  *
38  * Each thread competing for a set of locks must use one acquire
39  * ctx.  And if any lock fxn returns -EDEADLK, it must backoff and
40  * retry.
41  */
42 struct drm_modeset_acquire_ctx {
43
44         struct ww_acquire_ctx ww_ctx;
45
46         /*
47          * Contended lock: if a lock is contended you should only call
48          * drm_modeset_backoff() which drops locks and slow-locks the
49          * contended lock.
50          */
51         struct drm_modeset_lock *contended;
52
53         /*
54          * list of held locks (drm_modeset_lock_info)
55          */
56         struct list_head locked;
57
58         /**
59          * Trylock mode, use only for panic handlers!
60          */
61         bool trylock_only;
62 };
63
64 /*
65  * Keep track of extra locks.
66  */
67 struct drm_modeset_lock_info {
68         struct list_head ctx_entry;
69         struct list_head lock_entry;
70         struct drm_modeset_lock *lock;
71         struct drm_modeset_acquire_ctx *ctx;
72 };
73
74 /**
75  * struct drm_modeset_lock - used for locking modeset resources.
76  * @mutex: resource locking
77  * @head: used to hold it's place on state->locked list when
78  *    part of an atomic update
79  *
80  * Used for locking CRTCs and other modeset resources.
81  */
82 struct drm_modeset_lock {
83         /*
84          * modeset lock
85          */
86         struct ww_mutex mutex;
87
88         /*
89          * Resources that are locked as part of an atomic update are added
90          * to a list (so we know what to unlock at the end).
91          */
92         struct list_head locked;
93 };
94
95 extern struct ww_class crtc_ww_class;
96
97 void drm_modeset_acquire_init(struct drm_modeset_acquire_ctx *ctx,
98                 uint32_t flags);
99 void drm_modeset_acquire_fini(struct drm_modeset_acquire_ctx *ctx);
100 void drm_modeset_drop_locks(struct drm_modeset_acquire_ctx *ctx);
101 void drm_modeset_backoff(struct drm_modeset_acquire_ctx *ctx);
102 int drm_modeset_backoff_interruptible(struct drm_modeset_acquire_ctx *ctx);
103
104 /**
105  * drm_modeset_lock_init - initialize lock
106  * @lock: lock to init
107  */
108 static inline void drm_modeset_lock_init(struct drm_modeset_lock *lock)
109 {
110         ww_mutex_init(&lock->mutex, &crtc_ww_class);
111         INIT_LIST_HEAD(&lock->locked);
112 }
113
114 /**
115  * drm_modeset_lock_fini - cleanup lock
116  * @lock: lock to cleanup
117  */
118 static inline void drm_modeset_lock_fini(struct drm_modeset_lock *lock)
119 {
120         WARN_ON(!list_empty(&lock->locked));
121 }
122
123 /**
124  * drm_modeset_is_locked - equivalent to mutex_is_locked()
125  * @lock: lock to check
126  */
127 static inline bool drm_modeset_is_locked(struct drm_modeset_lock *lock)
128 {
129         return ww_mutex_is_locked(&lock->mutex);
130 }
131
132 int drm_modeset_lock(struct drm_modeset_lock *lock,
133                 struct drm_modeset_acquire_ctx *ctx);
134 int drm_modeset_lock_interruptible(struct drm_modeset_lock *lock,
135                 struct drm_modeset_acquire_ctx *ctx);
136 void drm_modeset_unlock(struct drm_modeset_lock *lock);
137
138 struct drm_device;
139 struct drm_crtc;
140 struct drm_plane;
141
142 void drm_modeset_lock_all(struct drm_device *dev);
143 int __drm_modeset_lock_all(struct drm_device *dev, bool trylock);
144 void drm_modeset_unlock_all(struct drm_device *dev);
145 void drm_modeset_lock_crtc(struct drm_crtc *crtc,
146                            struct drm_plane *plane);
147 void drm_modeset_unlock_crtc(struct drm_crtc *crtc);
148 void drm_warn_on_modeset_not_all_locked(struct drm_device *dev);
149 struct drm_modeset_acquire_ctx *
150 drm_modeset_legacy_acquire_ctx(struct drm_crtc *crtc);
151
152 int drm_modeset_lock_all_ctx(struct drm_device *dev,
153                              struct drm_modeset_acquire_ctx *ctx);
154
155 #endif /* DRM_MODESET_LOCK_H_ */