xref: /freebsd/sys/dev/drm2/ttm/ttm_lock.c (revision c9130a46bca272492b4f29429ed340504b8eced7)
1592ffb21SWarner Losh /**************************************************************************
2592ffb21SWarner Losh  *
3592ffb21SWarner Losh  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4592ffb21SWarner Losh  * All Rights Reserved.
5592ffb21SWarner Losh  *
6592ffb21SWarner Losh  * Permission is hereby granted, free of charge, to any person obtaining a
7592ffb21SWarner Losh  * copy of this software and associated documentation files (the
8592ffb21SWarner Losh  * "Software"), to deal in the Software without restriction, including
9592ffb21SWarner Losh  * without limitation the rights to use, copy, modify, merge, publish,
10592ffb21SWarner Losh  * distribute, sub license, and/or sell copies of the Software, and to
11592ffb21SWarner Losh  * permit persons to whom the Software is furnished to do so, subject to
12592ffb21SWarner Losh  * the following conditions:
13592ffb21SWarner Losh  *
14592ffb21SWarner Losh  * The above copyright notice and this permission notice (including the
15592ffb21SWarner Losh  * next paragraph) shall be included in all copies or substantial portions
16592ffb21SWarner Losh  * of the Software.
17592ffb21SWarner Losh  *
18592ffb21SWarner Losh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19592ffb21SWarner Losh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20592ffb21SWarner Losh  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21592ffb21SWarner Losh  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22592ffb21SWarner Losh  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23592ffb21SWarner Losh  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24592ffb21SWarner Losh  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25592ffb21SWarner Losh  *
26592ffb21SWarner Losh  **************************************************************************/
27592ffb21SWarner Losh /*
28592ffb21SWarner Losh  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29592ffb21SWarner Losh  */
30592ffb21SWarner Losh /*
31592ffb21SWarner Losh  * Copyright (c) 2013 The FreeBSD Foundation
32592ffb21SWarner Losh  * All rights reserved.
33592ffb21SWarner Losh  *
34592ffb21SWarner Losh  * Portions of this software were developed by Konstantin Belousov
35592ffb21SWarner Losh  * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
36592ffb21SWarner Losh  */
37592ffb21SWarner Losh 
38592ffb21SWarner Losh #include <sys/cdefs.h>
39592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_lock.h>
40592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_module.h>
41592ffb21SWarner Losh 
42592ffb21SWarner Losh #define TTM_WRITE_LOCK_PENDING    (1 << 0)
43592ffb21SWarner Losh #define TTM_VT_LOCK_PENDING       (1 << 1)
44592ffb21SWarner Losh #define TTM_SUSPEND_LOCK_PENDING  (1 << 2)
45592ffb21SWarner Losh #define TTM_VT_LOCK               (1 << 3)
46592ffb21SWarner Losh #define TTM_SUSPEND_LOCK          (1 << 4)
47592ffb21SWarner Losh 
ttm_lock_init(struct ttm_lock * lock)48592ffb21SWarner Losh void ttm_lock_init(struct ttm_lock *lock)
49592ffb21SWarner Losh {
50592ffb21SWarner Losh 	mtx_init(&lock->lock, "ttmlk", NULL, MTX_DEF);
51592ffb21SWarner Losh 	lock->rw = 0;
52592ffb21SWarner Losh 	lock->flags = 0;
53592ffb21SWarner Losh 	lock->kill_takers = false;
54592ffb21SWarner Losh 	lock->signal = SIGKILL;
55592ffb21SWarner Losh }
56592ffb21SWarner Losh 
57592ffb21SWarner Losh static void
ttm_lock_send_sig(int signo)58592ffb21SWarner Losh ttm_lock_send_sig(int signo)
59592ffb21SWarner Losh {
60592ffb21SWarner Losh 	struct proc *p;
61592ffb21SWarner Losh 
62592ffb21SWarner Losh 	p = curproc;	/* XXXKIB curthread ? */
63592ffb21SWarner Losh 	PROC_LOCK(p);
64592ffb21SWarner Losh 	kern_psignal(p, signo);
65592ffb21SWarner Losh 	PROC_UNLOCK(p);
66592ffb21SWarner Losh }
67592ffb21SWarner Losh 
ttm_read_unlock(struct ttm_lock * lock)68592ffb21SWarner Losh void ttm_read_unlock(struct ttm_lock *lock)
69592ffb21SWarner Losh {
70592ffb21SWarner Losh 	mtx_lock(&lock->lock);
71592ffb21SWarner Losh 	if (--lock->rw == 0)
72592ffb21SWarner Losh 		wakeup(lock);
73592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
74592ffb21SWarner Losh }
75592ffb21SWarner Losh 
__ttm_read_lock(struct ttm_lock * lock)76592ffb21SWarner Losh static bool __ttm_read_lock(struct ttm_lock *lock)
77592ffb21SWarner Losh {
78592ffb21SWarner Losh 	bool locked = false;
79592ffb21SWarner Losh 
80592ffb21SWarner Losh 	if (unlikely(lock->kill_takers)) {
81592ffb21SWarner Losh 		ttm_lock_send_sig(lock->signal);
82592ffb21SWarner Losh 		return false;
83592ffb21SWarner Losh 	}
84592ffb21SWarner Losh 	if (lock->rw >= 0 && lock->flags == 0) {
85592ffb21SWarner Losh 		++lock->rw;
86592ffb21SWarner Losh 		locked = true;
87592ffb21SWarner Losh 	}
88592ffb21SWarner Losh 	return locked;
89592ffb21SWarner Losh }
90592ffb21SWarner Losh 
91592ffb21SWarner Losh int
ttm_read_lock(struct ttm_lock * lock,bool interruptible)92592ffb21SWarner Losh ttm_read_lock(struct ttm_lock *lock, bool interruptible)
93592ffb21SWarner Losh {
94592ffb21SWarner Losh 	const char *wmsg;
95592ffb21SWarner Losh 	int flags, ret;
96592ffb21SWarner Losh 
97592ffb21SWarner Losh 	ret = 0;
98592ffb21SWarner Losh 	if (interruptible) {
99592ffb21SWarner Losh 		flags = PCATCH;
100592ffb21SWarner Losh 		wmsg = "ttmri";
101592ffb21SWarner Losh 	} else {
102592ffb21SWarner Losh 		flags = 0;
103592ffb21SWarner Losh 		wmsg = "ttmr";
104592ffb21SWarner Losh 	}
105592ffb21SWarner Losh 	mtx_lock(&lock->lock);
106592ffb21SWarner Losh 	while (!__ttm_read_lock(lock)) {
107592ffb21SWarner Losh 		ret = -msleep(lock, &lock->lock, flags, wmsg, 0);
108592ffb21SWarner Losh 		if (ret == -EINTR || ret == -ERESTART)
109592ffb21SWarner Losh 			ret = -ERESTARTSYS;
110592ffb21SWarner Losh 		if (ret != 0)
111592ffb21SWarner Losh 			break;
112592ffb21SWarner Losh 	}
113592ffb21SWarner Losh 	return (ret);
114592ffb21SWarner Losh }
115592ffb21SWarner Losh 
__ttm_read_trylock(struct ttm_lock * lock,bool * locked)116592ffb21SWarner Losh static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
117592ffb21SWarner Losh {
118592ffb21SWarner Losh 	bool block = true;
119592ffb21SWarner Losh 
120592ffb21SWarner Losh 	*locked = false;
121592ffb21SWarner Losh 
122592ffb21SWarner Losh 	if (unlikely(lock->kill_takers)) {
123592ffb21SWarner Losh 		ttm_lock_send_sig(lock->signal);
124592ffb21SWarner Losh 		return false;
125592ffb21SWarner Losh 	}
126592ffb21SWarner Losh 	if (lock->rw >= 0 && lock->flags == 0) {
127592ffb21SWarner Losh 		++lock->rw;
128592ffb21SWarner Losh 		block = false;
129592ffb21SWarner Losh 		*locked = true;
130592ffb21SWarner Losh 	} else if (lock->flags == 0) {
131592ffb21SWarner Losh 		block = false;
132592ffb21SWarner Losh 	}
133592ffb21SWarner Losh 
134592ffb21SWarner Losh 	return !block;
135592ffb21SWarner Losh }
136592ffb21SWarner Losh 
ttm_read_trylock(struct ttm_lock * lock,bool interruptible)137592ffb21SWarner Losh int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
138592ffb21SWarner Losh {
139592ffb21SWarner Losh 	const char *wmsg;
140592ffb21SWarner Losh 	int flags, ret;
141592ffb21SWarner Losh 	bool locked;
142592ffb21SWarner Losh 
143592ffb21SWarner Losh 	ret = 0;
144592ffb21SWarner Losh 	if (interruptible) {
145592ffb21SWarner Losh 		flags = PCATCH;
146592ffb21SWarner Losh 		wmsg = "ttmrti";
147592ffb21SWarner Losh 	} else {
148592ffb21SWarner Losh 		flags = 0;
149592ffb21SWarner Losh 		wmsg = "ttmrt";
150592ffb21SWarner Losh 	}
151592ffb21SWarner Losh 	mtx_lock(&lock->lock);
152592ffb21SWarner Losh 	while (!__ttm_read_trylock(lock, &locked)) {
153592ffb21SWarner Losh 		ret = -msleep(lock, &lock->lock, flags, wmsg, 0);
154592ffb21SWarner Losh 		if (ret == -EINTR || ret == -ERESTART)
155592ffb21SWarner Losh 			ret = -ERESTARTSYS;
156592ffb21SWarner Losh 		if (ret != 0)
157592ffb21SWarner Losh 			break;
158592ffb21SWarner Losh 	}
159592ffb21SWarner Losh 	MPASS(!locked || ret == 0);
160592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
161592ffb21SWarner Losh 
162592ffb21SWarner Losh 	return (locked) ? 0 : -EBUSY;
163592ffb21SWarner Losh }
164592ffb21SWarner Losh 
ttm_write_unlock(struct ttm_lock * lock)165592ffb21SWarner Losh void ttm_write_unlock(struct ttm_lock *lock)
166592ffb21SWarner Losh {
167592ffb21SWarner Losh 	mtx_lock(&lock->lock);
168592ffb21SWarner Losh 	lock->rw = 0;
169592ffb21SWarner Losh 	wakeup(lock);
170592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
171592ffb21SWarner Losh }
172592ffb21SWarner Losh 
__ttm_write_lock(struct ttm_lock * lock)173592ffb21SWarner Losh static bool __ttm_write_lock(struct ttm_lock *lock)
174592ffb21SWarner Losh {
175592ffb21SWarner Losh 	bool locked = false;
176592ffb21SWarner Losh 
177592ffb21SWarner Losh 	if (unlikely(lock->kill_takers)) {
178592ffb21SWarner Losh 		ttm_lock_send_sig(lock->signal);
179592ffb21SWarner Losh 		return false;
180592ffb21SWarner Losh 	}
181592ffb21SWarner Losh 	if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
182592ffb21SWarner Losh 		lock->rw = -1;
183592ffb21SWarner Losh 		lock->flags &= ~TTM_WRITE_LOCK_PENDING;
184592ffb21SWarner Losh 		locked = true;
185592ffb21SWarner Losh 	} else {
186592ffb21SWarner Losh 		lock->flags |= TTM_WRITE_LOCK_PENDING;
187592ffb21SWarner Losh 	}
188592ffb21SWarner Losh 	return locked;
189592ffb21SWarner Losh }
190592ffb21SWarner Losh 
191592ffb21SWarner Losh int
ttm_write_lock(struct ttm_lock * lock,bool interruptible)192592ffb21SWarner Losh ttm_write_lock(struct ttm_lock *lock, bool interruptible)
193592ffb21SWarner Losh {
194592ffb21SWarner Losh 	const char *wmsg;
195592ffb21SWarner Losh 	int flags, ret;
196592ffb21SWarner Losh 
197592ffb21SWarner Losh 	ret = 0;
198592ffb21SWarner Losh 	if (interruptible) {
199592ffb21SWarner Losh 		flags = PCATCH;
200592ffb21SWarner Losh 		wmsg = "ttmwi";
201592ffb21SWarner Losh 	} else {
202592ffb21SWarner Losh 		flags = 0;
203592ffb21SWarner Losh 		wmsg = "ttmw";
204592ffb21SWarner Losh 	}
205592ffb21SWarner Losh 	mtx_lock(&lock->lock);
206592ffb21SWarner Losh 	/* XXXKIB: linux uses __ttm_read_lock for uninterruptible sleeps */
207592ffb21SWarner Losh 	while (!__ttm_write_lock(lock)) {
208592ffb21SWarner Losh 		ret = -msleep(lock, &lock->lock, flags, wmsg, 0);
209592ffb21SWarner Losh 		if (ret == -EINTR || ret == -ERESTART)
210592ffb21SWarner Losh 			ret = -ERESTARTSYS;
211592ffb21SWarner Losh 		if (interruptible && ret != 0) {
212592ffb21SWarner Losh 			lock->flags &= ~TTM_WRITE_LOCK_PENDING;
213592ffb21SWarner Losh 			wakeup(lock);
214592ffb21SWarner Losh 			break;
215592ffb21SWarner Losh 		}
216592ffb21SWarner Losh 	}
217592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
218592ffb21SWarner Losh 
219592ffb21SWarner Losh 	return (ret);
220592ffb21SWarner Losh }
221592ffb21SWarner Losh 
ttm_write_lock_downgrade(struct ttm_lock * lock)222592ffb21SWarner Losh void ttm_write_lock_downgrade(struct ttm_lock *lock)
223592ffb21SWarner Losh {
224592ffb21SWarner Losh 	mtx_lock(&lock->lock);
225592ffb21SWarner Losh 	lock->rw = 1;
226592ffb21SWarner Losh 	wakeup(lock);
227592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
228592ffb21SWarner Losh }
229592ffb21SWarner Losh 
__ttm_vt_unlock(struct ttm_lock * lock)230592ffb21SWarner Losh static int __ttm_vt_unlock(struct ttm_lock *lock)
231592ffb21SWarner Losh {
232592ffb21SWarner Losh 	int ret = 0;
233592ffb21SWarner Losh 
234592ffb21SWarner Losh 	mtx_lock(&lock->lock);
235592ffb21SWarner Losh 	if (unlikely(!(lock->flags & TTM_VT_LOCK)))
236592ffb21SWarner Losh 		ret = -EINVAL;
237592ffb21SWarner Losh 	lock->flags &= ~TTM_VT_LOCK;
238592ffb21SWarner Losh 	wakeup(lock);
239592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
240592ffb21SWarner Losh 
241592ffb21SWarner Losh 	return ret;
242592ffb21SWarner Losh }
243592ffb21SWarner Losh 
ttm_vt_lock_remove(struct ttm_base_object ** p_base)244592ffb21SWarner Losh static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
245592ffb21SWarner Losh {
246592ffb21SWarner Losh 	struct ttm_base_object *base = *p_base;
247592ffb21SWarner Losh 	struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
248*c9130a46SMateusz Guzik 	int ret __diagused;
249592ffb21SWarner Losh 
250592ffb21SWarner Losh 	*p_base = NULL;
251592ffb21SWarner Losh 	ret = __ttm_vt_unlock(lock);
252592ffb21SWarner Losh 	MPASS(ret == 0);
253592ffb21SWarner Losh }
254592ffb21SWarner Losh 
__ttm_vt_lock(struct ttm_lock * lock)255592ffb21SWarner Losh static bool __ttm_vt_lock(struct ttm_lock *lock)
256592ffb21SWarner Losh {
257592ffb21SWarner Losh 	bool locked = false;
258592ffb21SWarner Losh 
259592ffb21SWarner Losh 	if (lock->rw == 0) {
260592ffb21SWarner Losh 		lock->flags &= ~TTM_VT_LOCK_PENDING;
261592ffb21SWarner Losh 		lock->flags |= TTM_VT_LOCK;
262592ffb21SWarner Losh 		locked = true;
263592ffb21SWarner Losh 	} else {
264592ffb21SWarner Losh 		lock->flags |= TTM_VT_LOCK_PENDING;
265592ffb21SWarner Losh 	}
266592ffb21SWarner Losh 	return locked;
267592ffb21SWarner Losh }
268592ffb21SWarner Losh 
ttm_vt_lock(struct ttm_lock * lock,bool interruptible,struct ttm_object_file * tfile)269592ffb21SWarner Losh int ttm_vt_lock(struct ttm_lock *lock,
270592ffb21SWarner Losh 		bool interruptible,
271592ffb21SWarner Losh 		struct ttm_object_file *tfile)
272592ffb21SWarner Losh {
273592ffb21SWarner Losh 	const char *wmsg;
274592ffb21SWarner Losh 	int flags, ret;
275592ffb21SWarner Losh 
276592ffb21SWarner Losh 	ret = 0;
277592ffb21SWarner Losh 	if (interruptible) {
278592ffb21SWarner Losh 		flags = PCATCH;
279592ffb21SWarner Losh 		wmsg = "ttmwi";
280592ffb21SWarner Losh 	} else {
281592ffb21SWarner Losh 		flags = 0;
282592ffb21SWarner Losh 		wmsg = "ttmw";
283592ffb21SWarner Losh 	}
284592ffb21SWarner Losh 	mtx_lock(&lock->lock);
285592ffb21SWarner Losh 	while (!__ttm_vt_lock(lock)) {
286592ffb21SWarner Losh 		ret = -msleep(lock, &lock->lock, flags, wmsg, 0);
287592ffb21SWarner Losh 		if (ret == -EINTR || ret == -ERESTART)
288592ffb21SWarner Losh 			ret = -ERESTARTSYS;
289592ffb21SWarner Losh 		if (interruptible && ret != 0) {
290592ffb21SWarner Losh 			lock->flags &= ~TTM_VT_LOCK_PENDING;
291592ffb21SWarner Losh 			wakeup(lock);
292592ffb21SWarner Losh 			break;
293592ffb21SWarner Losh 		}
294592ffb21SWarner Losh 	}
295592ffb21SWarner Losh 
296592ffb21SWarner Losh 	/*
297592ffb21SWarner Losh 	 * Add a base-object, the destructor of which will
298592ffb21SWarner Losh 	 * make sure the lock is released if the client dies
299592ffb21SWarner Losh 	 * while holding it.
300592ffb21SWarner Losh 	 */
301592ffb21SWarner Losh 
302592ffb21SWarner Losh 	ret = ttm_base_object_init(tfile, &lock->base, false,
303592ffb21SWarner Losh 				   ttm_lock_type, &ttm_vt_lock_remove, NULL);
304592ffb21SWarner Losh 	if (ret)
305592ffb21SWarner Losh 		(void)__ttm_vt_unlock(lock);
306592ffb21SWarner Losh 	else
307592ffb21SWarner Losh 		lock->vt_holder = tfile;
308592ffb21SWarner Losh 
309592ffb21SWarner Losh 	return (ret);
310592ffb21SWarner Losh }
311592ffb21SWarner Losh 
ttm_vt_unlock(struct ttm_lock * lock)312592ffb21SWarner Losh int ttm_vt_unlock(struct ttm_lock *lock)
313592ffb21SWarner Losh {
314592ffb21SWarner Losh 	return ttm_ref_object_base_unref(lock->vt_holder,
315592ffb21SWarner Losh 					 lock->base.hash.key, TTM_REF_USAGE);
316592ffb21SWarner Losh }
317592ffb21SWarner Losh 
ttm_suspend_unlock(struct ttm_lock * lock)318592ffb21SWarner Losh void ttm_suspend_unlock(struct ttm_lock *lock)
319592ffb21SWarner Losh {
320592ffb21SWarner Losh 	mtx_lock(&lock->lock);
321592ffb21SWarner Losh 	lock->flags &= ~TTM_SUSPEND_LOCK;
322592ffb21SWarner Losh 	wakeup(lock);
323592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
324592ffb21SWarner Losh }
325592ffb21SWarner Losh 
__ttm_suspend_lock(struct ttm_lock * lock)326592ffb21SWarner Losh static bool __ttm_suspend_lock(struct ttm_lock *lock)
327592ffb21SWarner Losh {
328592ffb21SWarner Losh 	bool locked = false;
329592ffb21SWarner Losh 
330592ffb21SWarner Losh 	if (lock->rw == 0) {
331592ffb21SWarner Losh 		lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
332592ffb21SWarner Losh 		lock->flags |= TTM_SUSPEND_LOCK;
333592ffb21SWarner Losh 		locked = true;
334592ffb21SWarner Losh 	} else {
335592ffb21SWarner Losh 		lock->flags |= TTM_SUSPEND_LOCK_PENDING;
336592ffb21SWarner Losh 	}
337592ffb21SWarner Losh 	return locked;
338592ffb21SWarner Losh }
339592ffb21SWarner Losh 
ttm_suspend_lock(struct ttm_lock * lock)340592ffb21SWarner Losh void ttm_suspend_lock(struct ttm_lock *lock)
341592ffb21SWarner Losh {
342592ffb21SWarner Losh 	mtx_lock(&lock->lock);
343592ffb21SWarner Losh 	while (!__ttm_suspend_lock(lock))
344592ffb21SWarner Losh 		msleep(lock, &lock->lock, 0, "ttms", 0);
345592ffb21SWarner Losh 	mtx_unlock(&lock->lock);
346592ffb21SWarner Losh }
347