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>
32 * This file implements a simple replacement for the buffer manager use
33 * of the DRM heavyweight hardware lock.
34 * The lock is a read-write lock. Taking it in read mode and write mode
35 * is relatively fast, and intended for in-kernel use only.
37 * The vt mode is used only when there is a need to block all
38 * user-space processes from validating buffers.
39 * It's allowed to leave kernel space with the vt lock held.
40 * If a user-space process dies while having the vt-lock,
41 * it will be released during the file descriptor release. The vt lock
42 * excludes write lock and read lock.
44 * The suspend mode is used to lock out all TTM users when preparing for
45 * and executing suspend operations.
52 #include "ttm/ttm_object.h"
53 #include <linux/wait.h>
54 #include <asm/atomic.h>
59 * @base: ttm base object used solely to release the lock if the client
60 * holding the lock dies.
61 * @queue: Queue for processes waiting for lock change-of-status.
62 * @lock: Spinlock protecting some lock members.
63 * @rw: Read-write lock counter. Protected by @lock.
64 * @flags: Lock state. Protected by @lock.
65 * @kill_takers: Boolean whether to kill takers of the lock.
66 * @signal: Signal to send when kill_takers is true.
70 struct ttm_base_object base;
71 wait_queue_head_t queue;
77 struct ttm_object_file *vt_holder;
84 * @lock: Pointer to a struct ttm_lock
85 * Initializes the lock.
87 extern void ttm_lock_init(struct ttm_lock *lock);
92 * @lock: Pointer to a struct ttm_lock
94 * Releases a read lock.
96 extern void ttm_read_unlock(struct ttm_lock *lock);
101 * @lock: Pointer to a struct ttm_lock
102 * @interruptible: Interruptible sleeping while waiting for a lock.
104 * Takes the lock in read mode.
106 * -ERESTARTSYS If interrupted by a signal and interruptible is true.
108 extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible);
113 * @lock: Pointer to a struct ttm_lock
114 * @interruptible: Interruptible sleeping while waiting for a lock.
116 * Tries to take the lock in read mode. If the lock is already held
117 * in write mode, the function will return -EBUSY. If the lock is held
118 * in vt or suspend mode, the function will sleep until these modes
122 * -EBUSY The lock was already held in write mode.
123 * -ERESTARTSYS If interrupted by a signal and interruptible is true.
125 extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible);
130 * @lock: Pointer to a struct ttm_lock
132 * Releases a write lock.
134 extern void ttm_write_unlock(struct ttm_lock *lock);
139 * @lock: Pointer to a struct ttm_lock
140 * @interruptible: Interruptible sleeping while waiting for a lock.
142 * Takes the lock in write mode.
144 * -ERESTARTSYS If interrupted by a signal and interruptible is true.
146 extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
151 * @lock: Pointer to a struct ttm_lock
153 * Downgrades a write lock to a read lock.
155 extern void ttm_lock_downgrade(struct ttm_lock *lock);
160 * @lock: Pointer to a struct ttm_lock
162 * Takes the lock in suspend mode. Excludes read and write mode.
164 extern void ttm_suspend_lock(struct ttm_lock *lock);
169 * @lock: Pointer to a struct ttm_lock
171 * Releases a suspend lock
173 extern void ttm_suspend_unlock(struct ttm_lock *lock);
178 * @lock: Pointer to a struct ttm_lock
179 * @interruptible: Interruptible sleeping while waiting for a lock.
180 * @tfile: Pointer to a struct ttm_object_file to register the lock with.
182 * Takes the lock in vt mode.
184 * -ERESTARTSYS If interrupted by a signal and interruptible is true.
185 * -ENOMEM: Out of memory when locking.
187 extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible,
188 struct ttm_object_file *tfile);
193 * @lock: Pointer to a struct ttm_lock
195 * Releases a vt lock.
197 * -EINVAL If the lock was not held.
199 extern int ttm_vt_unlock(struct ttm_lock *lock);
204 * @lock: Pointer to a struct ttm_lock
206 * Releases a write lock.
208 extern void ttm_write_unlock(struct ttm_lock *lock);
213 * @lock: Pointer to a struct ttm_lock
214 * @interruptible: Interruptible sleeping while waiting for a lock.
216 * Takes the lock in write mode.
218 * -ERESTARTSYS If interrupted by a signal and interruptible is true.
220 extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
225 * @lock: Pointer to a struct ttm_lock
226 * @val: Boolean whether to kill processes taking the lock.
227 * @signal: Signal to send to the process taking the lock.
229 * The kill-when-taking-lock functionality is used to kill processes that keep
230 * on using the TTM functionality when its resources has been taken down, for
231 * example when the X server exits. A typical sequence would look like this:
232 * - X server takes lock in write mode.
233 * - ttm_lock_set_kill() is called with @val set to true.
234 * - As part of X server exit, TTM resources are taken down.
235 * - X server releases the lock on file release.
236 * - Another dri client wants to render, takes the lock and is killed.
239 static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val,
242 lock->kill_takers = val;
244 lock->signal = signal;