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 * Copyright (c) 2013 The FreeBSD Foundation
32 * All rights reserved.
34 * Portions of this software were developed by Konstantin Belousov
35 * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <dev/drm2/ttm/ttm_lock.h>
42 #include <dev/drm2/ttm/ttm_module.h>
44 #define TTM_WRITE_LOCK_PENDING (1 << 0)
45 #define TTM_VT_LOCK_PENDING (1 << 1)
46 #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
47 #define TTM_VT_LOCK (1 << 3)
48 #define TTM_SUSPEND_LOCK (1 << 4)
50 void ttm_lock_init(struct ttm_lock *lock)
52 mtx_init(&lock->lock, "ttmlk", NULL, MTX_DEF);
55 lock->kill_takers = false;
56 lock->signal = SIGKILL;
60 ttm_lock_send_sig(int signo)
64 p = curproc; /* XXXKIB curthread ? */
66 kern_psignal(p, signo);
70 void ttm_read_unlock(struct ttm_lock *lock)
72 mtx_lock(&lock->lock);
75 mtx_unlock(&lock->lock);
78 static bool __ttm_read_lock(struct ttm_lock *lock)
82 if (unlikely(lock->kill_takers)) {
83 ttm_lock_send_sig(lock->signal);
86 if (lock->rw >= 0 && lock->flags == 0) {
94 ttm_read_lock(struct ttm_lock *lock, bool interruptible)
107 mtx_lock(&lock->lock);
108 while (!__ttm_read_lock(lock)) {
109 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
116 static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
122 if (unlikely(lock->kill_takers)) {
123 ttm_lock_send_sig(lock->signal);
126 if (lock->rw >= 0 && lock->flags == 0) {
130 } else if (lock->flags == 0) {
137 int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
151 mtx_lock(&lock->lock);
152 while (!__ttm_read_trylock(lock, &locked)) {
153 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
157 MPASS(!locked || ret == 0);
158 mtx_unlock(&lock->lock);
160 return (locked) ? 0 : -EBUSY;
163 void ttm_write_unlock(struct ttm_lock *lock)
165 mtx_lock(&lock->lock);
168 mtx_unlock(&lock->lock);
171 static bool __ttm_write_lock(struct ttm_lock *lock)
175 if (unlikely(lock->kill_takers)) {
176 ttm_lock_send_sig(lock->signal);
179 if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
181 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
184 lock->flags |= TTM_WRITE_LOCK_PENDING;
190 ttm_write_lock(struct ttm_lock *lock, bool interruptible)
203 mtx_lock(&lock->lock);
204 /* XXXKIB: linux uses __ttm_read_lock for uninterruptible sleeps */
205 while (!__ttm_write_lock(lock)) {
206 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
207 if (interruptible && ret != 0) {
208 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
213 mtx_unlock(&lock->lock);
218 void ttm_write_lock_downgrade(struct ttm_lock *lock)
220 mtx_lock(&lock->lock);
223 mtx_unlock(&lock->lock);
226 static int __ttm_vt_unlock(struct ttm_lock *lock)
230 mtx_lock(&lock->lock);
231 if (unlikely(!(lock->flags & TTM_VT_LOCK)))
233 lock->flags &= ~TTM_VT_LOCK;
235 mtx_unlock(&lock->lock);
240 static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
242 struct ttm_base_object *base = *p_base;
243 struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
247 ret = __ttm_vt_unlock(lock);
251 static bool __ttm_vt_lock(struct ttm_lock *lock)
256 lock->flags &= ~TTM_VT_LOCK_PENDING;
257 lock->flags |= TTM_VT_LOCK;
260 lock->flags |= TTM_VT_LOCK_PENDING;
265 int ttm_vt_lock(struct ttm_lock *lock,
267 struct ttm_object_file *tfile)
280 mtx_lock(&lock->lock);
281 while (!__ttm_vt_lock(lock)) {
282 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
283 if (interruptible && ret != 0) {
284 lock->flags &= ~TTM_VT_LOCK_PENDING;
291 * Add a base-object, the destructor of which will
292 * make sure the lock is released if the client dies
296 ret = ttm_base_object_init(tfile, &lock->base, false,
297 ttm_lock_type, &ttm_vt_lock_remove, NULL);
299 (void)__ttm_vt_unlock(lock);
301 lock->vt_holder = tfile;
306 int ttm_vt_unlock(struct ttm_lock *lock)
308 return ttm_ref_object_base_unref(lock->vt_holder,
309 lock->base.hash.key, TTM_REF_USAGE);
312 void ttm_suspend_unlock(struct ttm_lock *lock)
314 mtx_lock(&lock->lock);
315 lock->flags &= ~TTM_SUSPEND_LOCK;
317 mtx_unlock(&lock->lock);
320 static bool __ttm_suspend_lock(struct ttm_lock *lock)
325 lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
326 lock->flags |= TTM_SUSPEND_LOCK;
329 lock->flags |= TTM_SUSPEND_LOCK_PENDING;
334 void ttm_suspend_lock(struct ttm_lock *lock)
336 mtx_lock(&lock->lock);
337 while (!__ttm_suspend_lock(lock))
338 msleep(lock, &lock->lock, 0, "ttms", 0);
339 mtx_unlock(&lock->lock);