1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /**************************************************************************
4 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
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
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
);
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
);
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
)
70 spin_lock(&lock
->lock
);
71 if (unlikely(lock
->kill_takers
)) {
72 send_sig(lock
->signal
, current
, 0);
73 spin_unlock(&lock
->lock
);
76 if (lock
->rw
>= 0 && lock
->flags
== 0) {
80 spin_unlock(&lock
->lock
);
84 int ttm_read_lock(struct ttm_lock
*lock
, bool interruptible
)
89 ret
= wait_event_interruptible(lock
->queue
,
90 __ttm_read_lock(lock
));
92 wait_event(lock
->queue
, __ttm_read_lock(lock
));
95 EXPORT_SYMBOL(ttm_read_lock
);
97 static bool __ttm_read_trylock(struct ttm_lock
*lock
, bool *locked
)
103 spin_lock(&lock
->lock
);
104 if (unlikely(lock
->kill_takers
)) {
105 send_sig(lock
->signal
, current
, 0);
106 spin_unlock(&lock
->lock
);
109 if (lock
->rw
>= 0 && lock
->flags
== 0) {
113 } else if (lock
->flags
== 0) {
116 spin_unlock(&lock
->lock
);
121 int ttm_read_trylock(struct ttm_lock
*lock
, bool interruptible
)
127 ret
= wait_event_interruptible
128 (lock
->queue
, __ttm_read_trylock(lock
, &locked
));
130 wait_event(lock
->queue
, __ttm_read_trylock(lock
, &locked
));
132 if (unlikely(ret
!= 0)) {
137 return (locked
) ? 0 : -EBUSY
;
140 void ttm_write_unlock(struct ttm_lock
*lock
)
142 spin_lock(&lock
->lock
);
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
)
153 spin_lock(&lock
->lock
);
154 if (unlikely(lock
->kill_takers
)) {
155 send_sig(lock
->signal
, current
, 0);
156 spin_unlock(&lock
->lock
);
159 if (lock
->rw
== 0 && ((lock
->flags
& ~TTM_WRITE_LOCK_PENDING
) == 0)) {
161 lock
->flags
&= ~TTM_WRITE_LOCK_PENDING
;
164 lock
->flags
|= TTM_WRITE_LOCK_PENDING
;
166 spin_unlock(&lock
->lock
);
170 int ttm_write_lock(struct ttm_lock
*lock
, bool 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
);
184 wait_event(lock
->queue
, __ttm_write_lock(lock
));
188 EXPORT_SYMBOL(ttm_write_lock
);
190 static int __ttm_vt_unlock(struct ttm_lock
*lock
)
194 spin_lock(&lock
->lock
);
195 if (unlikely(!(lock
->flags
& TTM_VT_LOCK
)))
197 lock
->flags
&= ~TTM_VT_LOCK
;
198 wake_up_all(&lock
->queue
);
199 spin_unlock(&lock
->lock
);
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
);
211 ret
= __ttm_vt_unlock(lock
);
215 static bool __ttm_vt_lock(struct ttm_lock
*lock
)
219 spin_lock(&lock
->lock
);
221 lock
->flags
&= ~TTM_VT_LOCK_PENDING
;
222 lock
->flags
|= TTM_VT_LOCK
;
225 lock
->flags
|= TTM_VT_LOCK_PENDING
;
227 spin_unlock(&lock
->lock
);
231 int ttm_vt_lock(struct ttm_lock
*lock
,
233 struct ttm_object_file
*tfile
)
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
);
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
256 ret
= ttm_base_object_init(tfile
, &lock
->base
, false,
257 ttm_lock_type
, &ttm_vt_lock_remove
, NULL
);
259 (void)__ttm_vt_unlock(lock
);
261 lock
->vt_holder
= tfile
;
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
)
287 spin_lock(&lock
->lock
);
289 lock
->flags
&= ~TTM_SUSPEND_LOCK_PENDING
;
290 lock
->flags
|= TTM_SUSPEND_LOCK
;
293 lock
->flags
|= TTM_SUSPEND_LOCK_PENDING
;
295 spin_unlock(&lock
->lock
);
299 void ttm_suspend_lock(struct ttm_lock
*lock
)
301 wait_event(lock
->queue
, __ttm_suspend_lock(lock
));
303 EXPORT_SYMBOL(ttm_suspend_lock
);