drm/msm/hdmi: Enable HPD after HDMI IRQ is set up
[linux/fpc-iii.git] / drivers / gpu / drm / ttm / ttm_lock.c
blob20694b8a01cac415581c181f9a4f0005d3517586
1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
5 * All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32 #include <drm/ttm/ttm_lock.h>
33 #include <drm/ttm/ttm_module.h>
34 #include <linux/atomic.h>
35 #include <linux/errno.h>
36 #include <linux/wait.h>
37 #include <linux/sched/signal.h>
38 #include <linux/module.h>
40 #define TTM_WRITE_LOCK_PENDING (1 << 0)
41 #define TTM_VT_LOCK_PENDING (1 << 1)
42 #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
43 #define TTM_VT_LOCK (1 << 3)
44 #define TTM_SUSPEND_LOCK (1 << 4)
46 void ttm_lock_init(struct ttm_lock *lock)
48 spin_lock_init(&lock->lock);
49 init_waitqueue_head(&lock->queue);
50 lock->rw = 0;
51 lock->flags = 0;
52 lock->kill_takers = false;
53 lock->signal = SIGKILL;
55 EXPORT_SYMBOL(ttm_lock_init);
57 void ttm_read_unlock(struct ttm_lock *lock)
59 spin_lock(&lock->lock);
60 if (--lock->rw == 0)
61 wake_up_all(&lock->queue);
62 spin_unlock(&lock->lock);
64 EXPORT_SYMBOL(ttm_read_unlock);
66 static bool __ttm_read_lock(struct ttm_lock *lock)
68 bool locked = false;
70 spin_lock(&lock->lock);
71 if (unlikely(lock->kill_takers)) {
72 send_sig(lock->signal, current, 0);
73 spin_unlock(&lock->lock);
74 return false;
76 if (lock->rw >= 0 && lock->flags == 0) {
77 ++lock->rw;
78 locked = true;
80 spin_unlock(&lock->lock);
81 return locked;
84 int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
86 int ret = 0;
88 if (interruptible)
89 ret = wait_event_interruptible(lock->queue,
90 __ttm_read_lock(lock));
91 else
92 wait_event(lock->queue, __ttm_read_lock(lock));
93 return ret;
95 EXPORT_SYMBOL(ttm_read_lock);
97 static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
99 bool block = true;
101 *locked = false;
103 spin_lock(&lock->lock);
104 if (unlikely(lock->kill_takers)) {
105 send_sig(lock->signal, current, 0);
106 spin_unlock(&lock->lock);
107 return false;
109 if (lock->rw >= 0 && lock->flags == 0) {
110 ++lock->rw;
111 block = false;
112 *locked = true;
113 } else if (lock->flags == 0) {
114 block = false;
116 spin_unlock(&lock->lock);
118 return !block;
121 int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
123 int ret = 0;
124 bool locked;
126 if (interruptible)
127 ret = wait_event_interruptible
128 (lock->queue, __ttm_read_trylock(lock, &locked));
129 else
130 wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
132 if (unlikely(ret != 0)) {
133 BUG_ON(locked);
134 return ret;
137 return (locked) ? 0 : -EBUSY;
140 void ttm_write_unlock(struct ttm_lock *lock)
142 spin_lock(&lock->lock);
143 lock->rw = 0;
144 wake_up_all(&lock->queue);
145 spin_unlock(&lock->lock);
147 EXPORT_SYMBOL(ttm_write_unlock);
149 static bool __ttm_write_lock(struct ttm_lock *lock)
151 bool locked = false;
153 spin_lock(&lock->lock);
154 if (unlikely(lock->kill_takers)) {
155 send_sig(lock->signal, current, 0);
156 spin_unlock(&lock->lock);
157 return false;
159 if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
160 lock->rw = -1;
161 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
162 locked = true;
163 } else {
164 lock->flags |= TTM_WRITE_LOCK_PENDING;
166 spin_unlock(&lock->lock);
167 return locked;
170 int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
172 int ret = 0;
174 if (interruptible) {
175 ret = wait_event_interruptible(lock->queue,
176 __ttm_write_lock(lock));
177 if (unlikely(ret != 0)) {
178 spin_lock(&lock->lock);
179 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
180 wake_up_all(&lock->queue);
181 spin_unlock(&lock->lock);
183 } else
184 wait_event(lock->queue, __ttm_write_lock(lock));
186 return ret;
188 EXPORT_SYMBOL(ttm_write_lock);
190 static int __ttm_vt_unlock(struct ttm_lock *lock)
192 int ret = 0;
194 spin_lock(&lock->lock);
195 if (unlikely(!(lock->flags & TTM_VT_LOCK)))
196 ret = -EINVAL;
197 lock->flags &= ~TTM_VT_LOCK;
198 wake_up_all(&lock->queue);
199 spin_unlock(&lock->lock);
201 return ret;
204 static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
206 struct ttm_base_object *base = *p_base;
207 struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
208 int ret;
210 *p_base = NULL;
211 ret = __ttm_vt_unlock(lock);
212 BUG_ON(ret != 0);
215 static bool __ttm_vt_lock(struct ttm_lock *lock)
217 bool locked = false;
219 spin_lock(&lock->lock);
220 if (lock->rw == 0) {
221 lock->flags &= ~TTM_VT_LOCK_PENDING;
222 lock->flags |= TTM_VT_LOCK;
223 locked = true;
224 } else {
225 lock->flags |= TTM_VT_LOCK_PENDING;
227 spin_unlock(&lock->lock);
228 return locked;
231 int ttm_vt_lock(struct ttm_lock *lock,
232 bool interruptible,
233 struct ttm_object_file *tfile)
235 int ret = 0;
237 if (interruptible) {
238 ret = wait_event_interruptible(lock->queue,
239 __ttm_vt_lock(lock));
240 if (unlikely(ret != 0)) {
241 spin_lock(&lock->lock);
242 lock->flags &= ~TTM_VT_LOCK_PENDING;
243 wake_up_all(&lock->queue);
244 spin_unlock(&lock->lock);
245 return ret;
247 } else
248 wait_event(lock->queue, __ttm_vt_lock(lock));
251 * Add a base-object, the destructor of which will
252 * make sure the lock is released if the client dies
253 * while holding it.
256 ret = ttm_base_object_init(tfile, &lock->base, false,
257 ttm_lock_type, &ttm_vt_lock_remove, NULL);
258 if (ret)
259 (void)__ttm_vt_unlock(lock);
260 else
261 lock->vt_holder = tfile;
263 return ret;
265 EXPORT_SYMBOL(ttm_vt_lock);
267 int ttm_vt_unlock(struct ttm_lock *lock)
269 return ttm_ref_object_base_unref(lock->vt_holder,
270 lock->base.hash.key, TTM_REF_USAGE);
272 EXPORT_SYMBOL(ttm_vt_unlock);
274 void ttm_suspend_unlock(struct ttm_lock *lock)
276 spin_lock(&lock->lock);
277 lock->flags &= ~TTM_SUSPEND_LOCK;
278 wake_up_all(&lock->queue);
279 spin_unlock(&lock->lock);
281 EXPORT_SYMBOL(ttm_suspend_unlock);
283 static bool __ttm_suspend_lock(struct ttm_lock *lock)
285 bool locked = false;
287 spin_lock(&lock->lock);
288 if (lock->rw == 0) {
289 lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
290 lock->flags |= TTM_SUSPEND_LOCK;
291 locked = true;
292 } else {
293 lock->flags |= TTM_SUSPEND_LOCK_PENDING;
295 spin_unlock(&lock->lock);
296 return locked;
299 void ttm_suspend_lock(struct ttm_lock *lock)
301 wait_event(lock->queue, __ttm_suspend_lock(lock));
303 EXPORT_SYMBOL(ttm_suspend_lock);