xref: /freebsd/sys/dev/drm2/ttm/ttm_lock.h (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /**************************************************************************
2  *
3  * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30 
31 /** @file ttm_lock.h
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.
36  *
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.
43  *
44  * The suspend mode is used to lock out all TTM users when preparing for
45  * and executing suspend operations.
46  *
47  */
48 
49 #ifndef _TTM_LOCK_H_
50 #define _TTM_LOCK_H_
51 
52 #include <dev/drm2/drmP.h>
53 #include <dev/drm2/drm.h>
54 #include <dev/drm2/ttm/ttm_object.h>
55 
56 /**
57  * struct ttm_lock
58  *
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.
67  */
68 
69 struct ttm_lock {
70 	struct ttm_base_object base;
71 	struct mtx lock;
72 	int32_t rw;
73 	uint32_t flags;
74 	bool kill_takers;
75 	int signal;
76 	struct ttm_object_file *vt_holder;
77 };
78 
79 
80 /**
81  * ttm_lock_init
82  *
83  * @lock: Pointer to a struct ttm_lock
84  * Initializes the lock.
85  */
86 extern void ttm_lock_init(struct ttm_lock *lock);
87 
88 /**
89  * ttm_read_unlock
90  *
91  * @lock: Pointer to a struct ttm_lock
92  *
93  * Releases a read lock.
94  */
95 extern void ttm_read_unlock(struct ttm_lock *lock);
96 
97 /**
98  * ttm_read_lock
99  *
100  * @lock: Pointer to a struct ttm_lock
101  * @interruptible: Interruptible sleeping while waiting for a lock.
102  *
103  * Takes the lock in read mode.
104  * Returns:
105  * -ERESTARTSYS If interrupted by a signal and interruptible is true.
106  */
107 extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible);
108 
109 /**
110  * ttm_read_trylock
111  *
112  * @lock: Pointer to a struct ttm_lock
113  * @interruptible: Interruptible sleeping while waiting for a lock.
114  *
115  * Tries to take the lock in read mode. If the lock is already held
116  * in write mode, the function will return -EBUSY. If the lock is held
117  * in vt or suspend mode, the function will sleep until these modes
118  * are unlocked.
119  *
120  * Returns:
121  * -EBUSY The lock was already held in write mode.
122  * -ERESTARTSYS If interrupted by a signal and interruptible is true.
123  */
124 extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible);
125 
126 /**
127  * ttm_lock_downgrade
128  *
129  * @lock: Pointer to a struct ttm_lock
130  *
131  * Downgrades a write lock to a read lock.
132  */
133 extern void ttm_lock_downgrade(struct ttm_lock *lock);
134 
135 /**
136  * ttm_suspend_lock
137  *
138  * @lock: Pointer to a struct ttm_lock
139  *
140  * Takes the lock in suspend mode. Excludes read and write mode.
141  */
142 extern void ttm_suspend_lock(struct ttm_lock *lock);
143 
144 /**
145  * ttm_suspend_unlock
146  *
147  * @lock: Pointer to a struct ttm_lock
148  *
149  * Releases a suspend lock
150  */
151 extern void ttm_suspend_unlock(struct ttm_lock *lock);
152 
153 /**
154  * ttm_vt_lock
155  *
156  * @lock: Pointer to a struct ttm_lock
157  * @interruptible: Interruptible sleeping while waiting for a lock.
158  * @tfile: Pointer to a struct ttm_object_file to register the lock with.
159  *
160  * Takes the lock in vt mode.
161  * Returns:
162  * -ERESTARTSYS If interrupted by a signal and interruptible is true.
163  * -ENOMEM: Out of memory when locking.
164  */
165 extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible,
166 		       struct ttm_object_file *tfile);
167 
168 /**
169  * ttm_vt_unlock
170  *
171  * @lock: Pointer to a struct ttm_lock
172  *
173  * Releases a vt lock.
174  * Returns:
175  * -EINVAL If the lock was not held.
176  */
177 extern int ttm_vt_unlock(struct ttm_lock *lock);
178 
179 /**
180  * ttm_write_unlock
181  *
182  * @lock: Pointer to a struct ttm_lock
183  *
184  * Releases a write lock.
185  */
186 extern void ttm_write_unlock(struct ttm_lock *lock);
187 
188 /**
189  * ttm_write_lock
190  *
191  * @lock: Pointer to a struct ttm_lock
192  * @interruptible: Interruptible sleeping while waiting for a lock.
193  *
194  * Takes the lock in write mode.
195  * Returns:
196  * -ERESTARTSYS If interrupted by a signal and interruptible is true.
197  */
198 extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible);
199 
200 void ttm_write_lock_downgrade(struct ttm_lock *lock);
201 
202 /**
203  * ttm_lock_set_kill
204  *
205  * @lock: Pointer to a struct ttm_lock
206  * @val: Boolean whether to kill processes taking the lock.
207  * @signal: Signal to send to the process taking the lock.
208  *
209  * The kill-when-taking-lock functionality is used to kill processes that keep
210  * on using the TTM functionality when its resources has been taken down, for
211  * example when the X server exits. A typical sequence would look like this:
212  * - X server takes lock in write mode.
213  * - ttm_lock_set_kill() is called with @val set to true.
214  * - As part of X server exit, TTM resources are taken down.
215  * - X server releases the lock on file release.
216  * - Another dri client wants to render, takes the lock and is killed.
217  *
218  */
219 static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val,
220 				     int signal)
221 {
222 	lock->kill_takers = val;
223 	if (val)
224 		lock->signal = signal;
225 }
226 
227 #endif
228