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.
37 * $FreeBSD: head/sys/dev/drm2/ttm/ttm_lock.c 247835 2013-03-05 09:49:34Z kib $
40 #include <dev/drm2/ttm/ttm_lock.h>
41 #include <dev/drm2/ttm/ttm_module.h>
43 #define TTM_WRITE_LOCK_PENDING (1 << 0)
44 #define TTM_VT_LOCK_PENDING (1 << 1)
45 #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
46 #define TTM_VT_LOCK (1 << 3)
47 #define TTM_SUSPEND_LOCK (1 << 4)
49 void ttm_lock_init(struct ttm_lock *lock)
51 mtx_init(&lock->lock, "ttmlk", NULL, MTX_DEF);
54 lock->kill_takers = false;
55 lock->signal = SIGKILL;
59 ttm_lock_send_sig(int signo)
63 p = curproc; /* XXXKIB curthread ? */
65 kern_psignal(p, signo);
69 void ttm_read_unlock(struct ttm_lock *lock)
71 mtx_lock(&lock->lock);
74 mtx_unlock(&lock->lock);
77 static bool __ttm_read_lock(struct ttm_lock *lock)
81 if (unlikely(lock->kill_takers)) {
82 ttm_lock_send_sig(lock->signal);
85 if (lock->rw >= 0 && lock->flags == 0) {
93 ttm_read_lock(struct ttm_lock *lock, bool interruptible)
106 mtx_lock(&lock->lock);
107 while (!__ttm_read_lock(lock)) {
108 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
115 static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
121 if (unlikely(lock->kill_takers)) {
122 ttm_lock_send_sig(lock->signal);
125 if (lock->rw >= 0 && lock->flags == 0) {
129 } else if (lock->flags == 0) {
136 int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
150 mtx_lock(&lock->lock);
151 while (!__ttm_read_trylock(lock, &locked)) {
152 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
156 MPASS(!locked || ret == 0);
157 mtx_unlock(&lock->lock);
159 return (locked) ? 0 : -EBUSY;
162 void ttm_write_unlock(struct ttm_lock *lock)
164 mtx_lock(&lock->lock);
167 mtx_unlock(&lock->lock);
170 static bool __ttm_write_lock(struct ttm_lock *lock)
174 if (unlikely(lock->kill_takers)) {
175 ttm_lock_send_sig(lock->signal);
178 if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
180 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
183 lock->flags |= TTM_WRITE_LOCK_PENDING;
189 ttm_write_lock(struct ttm_lock *lock, bool interruptible)
202 mtx_lock(&lock->lock);
203 /* XXXKIB: linux uses __ttm_read_lock for uninterruptible sleeps */
204 while (!__ttm_write_lock(lock)) {
205 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
206 if (interruptible && ret != 0) {
207 lock->flags &= ~TTM_WRITE_LOCK_PENDING;
212 mtx_unlock(&lock->lock);
217 void ttm_write_lock_downgrade(struct ttm_lock *lock)
219 mtx_lock(&lock->lock);
222 mtx_unlock(&lock->lock);
225 static int __ttm_vt_unlock(struct ttm_lock *lock)
229 mtx_lock(&lock->lock);
230 if (unlikely(!(lock->flags & TTM_VT_LOCK)))
232 lock->flags &= ~TTM_VT_LOCK;
234 mtx_unlock(&lock->lock);
239 static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
241 struct ttm_base_object *base = *p_base;
242 struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
246 ret = __ttm_vt_unlock(lock);
250 static bool __ttm_vt_lock(struct ttm_lock *lock)
255 lock->flags &= ~TTM_VT_LOCK_PENDING;
256 lock->flags |= TTM_VT_LOCK;
259 lock->flags |= TTM_VT_LOCK_PENDING;
264 int ttm_vt_lock(struct ttm_lock *lock,
266 struct ttm_object_file *tfile)
279 mtx_lock(&lock->lock);
280 while (!__ttm_vt_lock(lock)) {
281 ret = msleep(lock, &lock->lock, flags, wmsg, 0);
282 if (interruptible && ret != 0) {
283 lock->flags &= ~TTM_VT_LOCK_PENDING;
290 * Add a base-object, the destructor of which will
291 * make sure the lock is released if the client dies
295 ret = ttm_base_object_init(tfile, &lock->base, false,
296 ttm_lock_type, &ttm_vt_lock_remove, NULL);
298 (void)__ttm_vt_unlock(lock);
300 lock->vt_holder = tfile;
305 int ttm_vt_unlock(struct ttm_lock *lock)
307 return ttm_ref_object_base_unref(lock->vt_holder,
308 lock->base.hash.key, TTM_REF_USAGE);
311 void ttm_suspend_unlock(struct ttm_lock *lock)
313 mtx_lock(&lock->lock);
314 lock->flags &= ~TTM_SUSPEND_LOCK;
316 mtx_unlock(&lock->lock);
319 static bool __ttm_suspend_lock(struct ttm_lock *lock)
324 lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
325 lock->flags |= TTM_SUSPEND_LOCK;
328 lock->flags |= TTM_SUSPEND_LOCK_PENDING;
333 void ttm_suspend_lock(struct ttm_lock *lock)
335 mtx_lock(&lock->lock);
336 while (!__ttm_suspend_lock(lock))
337 msleep(lock, &lock->lock, 0, "ttms", 0);
338 mtx_unlock(&lock->lock);