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 <linux/atomic.h>
33 #include <linux/errno.h>
34 #include <linux/wait.h>
35 #include <linux/sched/signal.h>
37 #include "ttm_object.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
);
53 void ttm_read_unlock(struct ttm_lock
*lock
)
55 spin_lock(&lock
->lock
);
57 wake_up_all(&lock
->queue
);
58 spin_unlock(&lock
->lock
);
61 static bool __ttm_read_lock(struct ttm_lock
*lock
)
65 spin_lock(&lock
->lock
);
66 if (lock
->rw
>= 0 && lock
->flags
== 0) {
70 spin_unlock(&lock
->lock
);
74 int ttm_read_lock(struct ttm_lock
*lock
, bool interruptible
)
79 ret
= wait_event_interruptible(lock
->queue
,
80 __ttm_read_lock(lock
));
82 wait_event(lock
->queue
, __ttm_read_lock(lock
));
86 static bool __ttm_read_trylock(struct ttm_lock
*lock
, bool *locked
)
92 spin_lock(&lock
->lock
);
93 if (lock
->rw
>= 0 && lock
->flags
== 0) {
97 } else if (lock
->flags
== 0) {
100 spin_unlock(&lock
->lock
);
105 int ttm_read_trylock(struct ttm_lock
*lock
, bool interruptible
)
111 ret
= wait_event_interruptible
112 (lock
->queue
, __ttm_read_trylock(lock
, &locked
));
114 wait_event(lock
->queue
, __ttm_read_trylock(lock
, &locked
));
116 if (unlikely(ret
!= 0)) {
121 return (locked
) ? 0 : -EBUSY
;
124 void ttm_write_unlock(struct ttm_lock
*lock
)
126 spin_lock(&lock
->lock
);
128 wake_up_all(&lock
->queue
);
129 spin_unlock(&lock
->lock
);
132 static bool __ttm_write_lock(struct ttm_lock
*lock
)
136 spin_lock(&lock
->lock
);
137 if (lock
->rw
== 0 && ((lock
->flags
& ~TTM_WRITE_LOCK_PENDING
) == 0)) {
139 lock
->flags
&= ~TTM_WRITE_LOCK_PENDING
;
142 lock
->flags
|= TTM_WRITE_LOCK_PENDING
;
144 spin_unlock(&lock
->lock
);
148 int ttm_write_lock(struct ttm_lock
*lock
, bool interruptible
)
153 ret
= wait_event_interruptible(lock
->queue
,
154 __ttm_write_lock(lock
));
155 if (unlikely(ret
!= 0)) {
156 spin_lock(&lock
->lock
);
157 lock
->flags
&= ~TTM_WRITE_LOCK_PENDING
;
158 wake_up_all(&lock
->queue
);
159 spin_unlock(&lock
->lock
);
162 wait_event(lock
->queue
, __ttm_write_lock(lock
));
167 void ttm_suspend_unlock(struct ttm_lock
*lock
)
169 spin_lock(&lock
->lock
);
170 lock
->flags
&= ~TTM_SUSPEND_LOCK
;
171 wake_up_all(&lock
->queue
);
172 spin_unlock(&lock
->lock
);
175 static bool __ttm_suspend_lock(struct ttm_lock
*lock
)
179 spin_lock(&lock
->lock
);
181 lock
->flags
&= ~TTM_SUSPEND_LOCK_PENDING
;
182 lock
->flags
|= TTM_SUSPEND_LOCK
;
185 lock
->flags
|= TTM_SUSPEND_LOCK_PENDING
;
187 spin_unlock(&lock
->lock
);
191 void ttm_suspend_lock(struct ttm_lock
*lock
)
193 wait_event(lock
->queue
, __ttm_suspend_lock(lock
));