]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - drivers/gpu/drm/ttm/ttm_lock.c
1 /**************************************************************************
3 * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
31 #include <drm/ttm/ttm_lock.h>
32 #include <drm/ttm/ttm_module.h>
33 #include <linux/atomic.h>
34 #include <linux/errno.h>
35 #include <linux/wait.h>
36 #include <linux/sched.h>
37 #include <linux/module.h>
39 #define TTM_WRITE_LOCK_PENDING (1 << 0)
40 #define TTM_VT_LOCK_PENDING (1 << 1)
41 #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
42 #define TTM_VT_LOCK (1 << 3)
43 #define TTM_SUSPEND_LOCK (1 << 4)
45 void ttm_lock_init(struct ttm_lock
*lock
)
47 spin_lock_init(&lock
->lock
);
48 init_waitqueue_head(&lock
->queue
);
51 lock
->kill_takers
= false;
52 lock
->signal
= SIGKILL
;
54 EXPORT_SYMBOL(ttm_lock_init
);
56 void ttm_read_unlock(struct ttm_lock
*lock
)
58 spin_lock(&lock
->lock
);
60 wake_up_all(&lock
->queue
);
61 spin_unlock(&lock
->lock
);
63 EXPORT_SYMBOL(ttm_read_unlock
);
65 static bool __ttm_read_lock(struct ttm_lock
*lock
)
69 spin_lock(&lock
->lock
);
70 if (unlikely(lock
->kill_takers
)) {
71 send_sig(lock
->signal
, current
, 0);
72 spin_unlock(&lock
->lock
);
75 if (lock
->rw
>= 0 && lock
->flags
== 0) {
79 spin_unlock(&lock
->lock
);
83 int ttm_read_lock(struct ttm_lock
*lock
, bool interruptible
)
88 ret
= wait_event_interruptible(lock
->queue
,
89 __ttm_read_lock(lock
));
91 wait_event(lock
->queue
, __ttm_read_lock(lock
));
94 EXPORT_SYMBOL(ttm_read_lock
);
96 static bool __ttm_read_trylock(struct ttm_lock
*lock
, bool *locked
)
102 spin_lock(&lock
->lock
);
103 if (unlikely(lock
->kill_takers
)) {
104 send_sig(lock
->signal
, current
, 0);
105 spin_unlock(&lock
->lock
);
108 if (lock
->rw
>= 0 && lock
->flags
== 0) {
112 } else if (lock
->flags
== 0) {
115 spin_unlock(&lock
->lock
);
120 int ttm_read_trylock(struct ttm_lock
*lock
, bool interruptible
)
126 ret
= wait_event_interruptible
127 (lock
->queue
, __ttm_read_trylock(lock
, &locked
));
129 wait_event(lock
->queue
, __ttm_read_trylock(lock
, &locked
));
131 if (unlikely(ret
!= 0)) {
136 return (locked
) ? 0 : -EBUSY
;
139 void ttm_write_unlock(struct ttm_lock
*lock
)
141 spin_lock(&lock
->lock
);
143 wake_up_all(&lock
->queue
);
144 spin_unlock(&lock
->lock
);
146 EXPORT_SYMBOL(ttm_write_unlock
);
148 static bool __ttm_write_lock(struct ttm_lock
*lock
)
152 spin_lock(&lock
->lock
);
153 if (unlikely(lock
->kill_takers
)) {
154 send_sig(lock
->signal
, current
, 0);
155 spin_unlock(&lock
->lock
);
158 if (lock
->rw
== 0 && ((lock
->flags
& ~TTM_WRITE_LOCK_PENDING
) == 0)) {
160 lock
->flags
&= ~TTM_WRITE_LOCK_PENDING
;
163 lock
->flags
|= TTM_WRITE_LOCK_PENDING
;
165 spin_unlock(&lock
->lock
);
169 int ttm_write_lock(struct ttm_lock
*lock
, bool interruptible
)
174 ret
= wait_event_interruptible(lock
->queue
,
175 __ttm_write_lock(lock
));
176 if (unlikely(ret
!= 0)) {
177 spin_lock(&lock
->lock
);
178 lock
->flags
&= ~TTM_WRITE_LOCK_PENDING
;
179 wake_up_all(&lock
->queue
);
180 spin_unlock(&lock
->lock
);
183 wait_event(lock
->queue
, __ttm_read_lock(lock
));
187 EXPORT_SYMBOL(ttm_write_lock
);
189 void ttm_write_lock_downgrade(struct ttm_lock
*lock
)
191 spin_lock(&lock
->lock
);
193 wake_up_all(&lock
->queue
);
194 spin_unlock(&lock
->lock
);
197 static int __ttm_vt_unlock(struct ttm_lock
*lock
)
201 spin_lock(&lock
->lock
);
202 if (unlikely(!(lock
->flags
& TTM_VT_LOCK
)))
204 lock
->flags
&= ~TTM_VT_LOCK
;
205 wake_up_all(&lock
->queue
);
206 spin_unlock(&lock
->lock
);
211 static void ttm_vt_lock_remove(struct ttm_base_object
**p_base
)
213 struct ttm_base_object
*base
= *p_base
;
214 struct ttm_lock
*lock
= container_of(base
, struct ttm_lock
, base
);
218 ret
= __ttm_vt_unlock(lock
);
222 static bool __ttm_vt_lock(struct ttm_lock
*lock
)
226 spin_lock(&lock
->lock
);
228 lock
->flags
&= ~TTM_VT_LOCK_PENDING
;
229 lock
->flags
|= TTM_VT_LOCK
;
232 lock
->flags
|= TTM_VT_LOCK_PENDING
;
234 spin_unlock(&lock
->lock
);
238 int ttm_vt_lock(struct ttm_lock
*lock
,
240 struct ttm_object_file
*tfile
)
245 ret
= wait_event_interruptible(lock
->queue
,
246 __ttm_vt_lock(lock
));
247 if (unlikely(ret
!= 0)) {
248 spin_lock(&lock
->lock
);
249 lock
->flags
&= ~TTM_VT_LOCK_PENDING
;
250 wake_up_all(&lock
->queue
);
251 spin_unlock(&lock
->lock
);
255 wait_event(lock
->queue
, __ttm_vt_lock(lock
));
258 * Add a base-object, the destructor of which will
259 * make sure the lock is released if the client dies
263 ret
= ttm_base_object_init(tfile
, &lock
->base
, false,
264 ttm_lock_type
, &ttm_vt_lock_remove
, NULL
);
266 (void)__ttm_vt_unlock(lock
);
268 lock
->vt_holder
= tfile
;
272 EXPORT_SYMBOL(ttm_vt_lock
);
274 int ttm_vt_unlock(struct ttm_lock
*lock
)
276 return ttm_ref_object_base_unref(lock
->vt_holder
,
277 lock
->base
.hash
.key
, TTM_REF_USAGE
);
279 EXPORT_SYMBOL(ttm_vt_unlock
);
281 void ttm_suspend_unlock(struct ttm_lock
*lock
)
283 spin_lock(&lock
->lock
);
284 lock
->flags
&= ~TTM_SUSPEND_LOCK
;
285 wake_up_all(&lock
->queue
);
286 spin_unlock(&lock
->lock
);
288 EXPORT_SYMBOL(ttm_suspend_unlock
);
290 static bool __ttm_suspend_lock(struct ttm_lock
*lock
)
294 spin_lock(&lock
->lock
);
296 lock
->flags
&= ~TTM_SUSPEND_LOCK_PENDING
;
297 lock
->flags
|= TTM_SUSPEND_LOCK
;
300 lock
->flags
|= TTM_SUSPEND_LOCK_PENDING
;
302 spin_unlock(&lock
->lock
);
306 void ttm_suspend_lock(struct ttm_lock
*lock
)
308 wait_event(lock
->queue
, __ttm_suspend_lock(lock
));
310 EXPORT_SYMBOL(ttm_suspend_lock
);