Rev 5056 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 5056 | Rev 6082 | ||
---|---|---|---|
1 | /************************************************************************** |
1 | /************************************************************************** |
2 | * |
2 | * |
3 | * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA |
3 | * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA |
4 | * All Rights Reserved. |
4 | * All Rights Reserved. |
5 | * |
5 | * |
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
7 | * copy of this software and associated documentation files (the |
7 | * copy of this software and associated documentation files (the |
8 | * "Software"), to deal in the Software without restriction, including |
8 | * "Software"), to deal in the Software without restriction, including |
9 | * without limitation the rights to use, copy, modify, merge, publish, |
9 | * without limitation the rights to use, copy, modify, merge, publish, |
10 | * distribute, sub license, and/or sell copies of the Software, and to |
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 |
11 | * permit persons to whom the Software is furnished to do so, subject to |
12 | * the following conditions: |
12 | * the following conditions: |
13 | * |
13 | * |
14 | * The above copyright notice and this permission notice (including the |
14 | * The above copyright notice and this permission notice (including the |
15 | * next paragraph) shall be included in all copies or substantial portions |
15 | * next paragraph) shall be included in all copies or substantial portions |
16 | * of the Software. |
16 | * of the Software. |
17 | * |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
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, |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
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, |
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 |
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 |
23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
25 | * |
25 | * |
26 | **************************************************************************/ |
26 | **************************************************************************/ |
27 | /* |
27 | /* |
28 | * Authors: Thomas Hellstrom |
28 | * Authors: Thomas Hellstrom |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /** @file ttm_lock.h |
31 | /** @file ttm_lock.h |
32 | * This file implements a simple replacement for the buffer manager use |
32 | * This file implements a simple replacement for the buffer manager use |
33 | * of the DRM heavyweight hardware lock. |
33 | * of the DRM heavyweight hardware lock. |
34 | * The lock is a read-write lock. Taking it in read mode and write mode |
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. |
35 | * is relatively fast, and intended for in-kernel use only. |
36 | * |
36 | * |
37 | * The vt mode is used only when there is a need to block all |
37 | * The vt mode is used only when there is a need to block all |
38 | * user-space processes from validating buffers. |
38 | * user-space processes from validating buffers. |
39 | * It's allowed to leave kernel space with the vt lock held. |
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, |
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 |
41 | * it will be released during the file descriptor release. The vt lock |
42 | * excludes write lock and read lock. |
42 | * excludes write lock and read lock. |
43 | * |
43 | * |
44 | * The suspend mode is used to lock out all TTM users when preparing for |
44 | * The suspend mode is used to lock out all TTM users when preparing for |
45 | * and executing suspend operations. |
45 | * and executing suspend operations. |
46 | * |
46 | * |
47 | */ |
47 | */ |
48 | 48 | ||
49 | #ifndef _TTM_LOCK_H_ |
49 | #ifndef _TTM_LOCK_H_ |
50 | #define _TTM_LOCK_H_ |
50 | #define _TTM_LOCK_H_ |
51 | 51 | ||
52 | #include |
52 | #include |
53 | #include |
53 | #include |
54 | //#include |
54 | #include |
55 | 55 | ||
56 | /** |
56 | /** |
57 | * struct ttm_lock |
57 | * struct ttm_lock |
58 | * |
58 | * |
59 | * @base: ttm base object used solely to release the lock if the client |
59 | * @base: ttm base object used solely to release the lock if the client |
60 | * holding the lock dies. |
60 | * holding the lock dies. |
61 | * @queue: Queue for processes waiting for lock change-of-status. |
61 | * @queue: Queue for processes waiting for lock change-of-status. |
62 | * @lock: Spinlock protecting some lock members. |
62 | * @lock: Spinlock protecting some lock members. |
63 | * @rw: Read-write lock counter. Protected by @lock. |
63 | * @rw: Read-write lock counter. Protected by @lock. |
64 | * @flags: Lock state. Protected by @lock. |
64 | * @flags: Lock state. Protected by @lock. |
65 | * @kill_takers: Boolean whether to kill takers of the lock. |
65 | * @kill_takers: Boolean whether to kill takers of the lock. |
66 | * @signal: Signal to send when kill_takers is true. |
66 | * @signal: Signal to send when kill_takers is true. |
67 | */ |
67 | */ |
68 | 68 | ||
69 | struct ttm_lock { |
69 | struct ttm_lock { |
70 | struct ttm_base_object base; |
70 | struct ttm_base_object base; |
71 | wait_queue_head_t queue; |
71 | wait_queue_head_t queue; |
72 | spinlock_t lock; |
72 | spinlock_t lock; |
73 | int32_t rw; |
73 | int32_t rw; |
74 | uint32_t flags; |
74 | uint32_t flags; |
75 | bool kill_takers; |
75 | bool kill_takers; |
76 | int signal; |
76 | int signal; |
77 | struct ttm_object_file *vt_holder; |
77 | struct ttm_object_file *vt_holder; |
78 | }; |
78 | }; |
79 | 79 | ||
80 | 80 | ||
81 | /** |
81 | /** |
82 | * ttm_lock_init |
82 | * ttm_lock_init |
83 | * |
83 | * |
84 | * @lock: Pointer to a struct ttm_lock |
84 | * @lock: Pointer to a struct ttm_lock |
85 | * Initializes the lock. |
85 | * Initializes the lock. |
86 | */ |
86 | */ |
87 | extern void ttm_lock_init(struct ttm_lock *lock); |
87 | extern void ttm_lock_init(struct ttm_lock *lock); |
88 | 88 | ||
89 | /** |
89 | /** |
90 | * ttm_read_unlock |
90 | * ttm_read_unlock |
91 | * |
91 | * |
92 | * @lock: Pointer to a struct ttm_lock |
92 | * @lock: Pointer to a struct ttm_lock |
93 | * |
93 | * |
94 | * Releases a read lock. |
94 | * Releases a read lock. |
95 | */ |
95 | */ |
96 | extern void ttm_read_unlock(struct ttm_lock *lock); |
96 | extern void ttm_read_unlock(struct ttm_lock *lock); |
97 | 97 | ||
98 | /** |
98 | /** |
99 | * ttm_read_lock |
99 | * ttm_read_lock |
100 | * |
100 | * |
101 | * @lock: Pointer to a struct ttm_lock |
101 | * @lock: Pointer to a struct ttm_lock |
102 | * @interruptible: Interruptible sleeping while waiting for a lock. |
102 | * @interruptible: Interruptible sleeping while waiting for a lock. |
103 | * |
103 | * |
104 | * Takes the lock in read mode. |
104 | * Takes the lock in read mode. |
105 | * Returns: |
105 | * Returns: |
106 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
106 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
107 | */ |
107 | */ |
108 | extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible); |
108 | extern int ttm_read_lock(struct ttm_lock *lock, bool interruptible); |
109 | 109 | ||
110 | /** |
110 | /** |
111 | * ttm_read_trylock |
111 | * ttm_read_trylock |
112 | * |
112 | * |
113 | * @lock: Pointer to a struct ttm_lock |
113 | * @lock: Pointer to a struct ttm_lock |
114 | * @interruptible: Interruptible sleeping while waiting for a lock. |
114 | * @interruptible: Interruptible sleeping while waiting for a lock. |
115 | * |
115 | * |
116 | * Tries to take the lock in read mode. If the lock is already held |
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 |
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 |
118 | * in vt or suspend mode, the function will sleep until these modes |
119 | * are unlocked. |
119 | * are unlocked. |
120 | * |
120 | * |
121 | * Returns: |
121 | * Returns: |
122 | * -EBUSY The lock was already held in write mode. |
122 | * -EBUSY The lock was already held in write mode. |
123 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
123 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
124 | */ |
124 | */ |
125 | extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible); |
125 | extern int ttm_read_trylock(struct ttm_lock *lock, bool interruptible); |
126 | 126 | ||
127 | /** |
127 | /** |
128 | * ttm_write_unlock |
128 | * ttm_write_unlock |
129 | * |
129 | * |
130 | * @lock: Pointer to a struct ttm_lock |
130 | * @lock: Pointer to a struct ttm_lock |
131 | * |
131 | * |
132 | * Releases a write lock. |
132 | * Releases a write lock. |
133 | */ |
133 | */ |
134 | extern void ttm_write_unlock(struct ttm_lock *lock); |
134 | extern void ttm_write_unlock(struct ttm_lock *lock); |
135 | 135 | ||
136 | /** |
136 | /** |
137 | * ttm_write_lock |
137 | * ttm_write_lock |
138 | * |
138 | * |
139 | * @lock: Pointer to a struct ttm_lock |
139 | * @lock: Pointer to a struct ttm_lock |
140 | * @interruptible: Interruptible sleeping while waiting for a lock. |
140 | * @interruptible: Interruptible sleeping while waiting for a lock. |
141 | * |
141 | * |
142 | * Takes the lock in write mode. |
142 | * Takes the lock in write mode. |
143 | * Returns: |
143 | * Returns: |
144 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
144 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
145 | */ |
145 | */ |
146 | extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); |
146 | extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); |
147 | 147 | ||
148 | /** |
148 | /** |
149 | * ttm_lock_downgrade |
149 | * ttm_lock_downgrade |
150 | * |
150 | * |
151 | * @lock: Pointer to a struct ttm_lock |
151 | * @lock: Pointer to a struct ttm_lock |
152 | * |
152 | * |
153 | * Downgrades a write lock to a read lock. |
153 | * Downgrades a write lock to a read lock. |
154 | */ |
154 | */ |
155 | extern void ttm_lock_downgrade(struct ttm_lock *lock); |
155 | extern void ttm_lock_downgrade(struct ttm_lock *lock); |
156 | 156 | ||
157 | /** |
157 | /** |
158 | * ttm_suspend_lock |
158 | * ttm_suspend_lock |
159 | * |
159 | * |
160 | * @lock: Pointer to a struct ttm_lock |
160 | * @lock: Pointer to a struct ttm_lock |
161 | * |
161 | * |
162 | * Takes the lock in suspend mode. Excludes read and write mode. |
162 | * Takes the lock in suspend mode. Excludes read and write mode. |
163 | */ |
163 | */ |
164 | extern void ttm_suspend_lock(struct ttm_lock *lock); |
164 | extern void ttm_suspend_lock(struct ttm_lock *lock); |
165 | 165 | ||
166 | /** |
166 | /** |
167 | * ttm_suspend_unlock |
167 | * ttm_suspend_unlock |
168 | * |
168 | * |
169 | * @lock: Pointer to a struct ttm_lock |
169 | * @lock: Pointer to a struct ttm_lock |
170 | * |
170 | * |
171 | * Releases a suspend lock |
171 | * Releases a suspend lock |
172 | */ |
172 | */ |
173 | extern void ttm_suspend_unlock(struct ttm_lock *lock); |
173 | extern void ttm_suspend_unlock(struct ttm_lock *lock); |
174 | 174 | ||
175 | /** |
175 | /** |
176 | * ttm_vt_lock |
176 | * ttm_vt_lock |
177 | * |
177 | * |
178 | * @lock: Pointer to a struct ttm_lock |
178 | * @lock: Pointer to a struct ttm_lock |
179 | * @interruptible: Interruptible sleeping while waiting for a lock. |
179 | * @interruptible: Interruptible sleeping while waiting for a lock. |
180 | * @tfile: Pointer to a struct ttm_object_file to register the lock with. |
180 | * @tfile: Pointer to a struct ttm_object_file to register the lock with. |
181 | * |
181 | * |
182 | * Takes the lock in vt mode. |
182 | * Takes the lock in vt mode. |
183 | * Returns: |
183 | * Returns: |
184 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
184 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
185 | * -ENOMEM: Out of memory when locking. |
185 | * -ENOMEM: Out of memory when locking. |
186 | */ |
186 | */ |
187 | extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible, |
187 | extern int ttm_vt_lock(struct ttm_lock *lock, bool interruptible, |
188 | struct ttm_object_file *tfile); |
188 | struct ttm_object_file *tfile); |
189 | 189 | ||
190 | /** |
190 | /** |
191 | * ttm_vt_unlock |
191 | * ttm_vt_unlock |
192 | * |
192 | * |
193 | * @lock: Pointer to a struct ttm_lock |
193 | * @lock: Pointer to a struct ttm_lock |
194 | * |
194 | * |
195 | * Releases a vt lock. |
195 | * Releases a vt lock. |
196 | * Returns: |
196 | * Returns: |
197 | * -EINVAL If the lock was not held. |
197 | * -EINVAL If the lock was not held. |
198 | */ |
198 | */ |
199 | extern int ttm_vt_unlock(struct ttm_lock *lock); |
199 | extern int ttm_vt_unlock(struct ttm_lock *lock); |
200 | 200 | ||
201 | /** |
201 | /** |
202 | * ttm_write_unlock |
202 | * ttm_write_unlock |
203 | * |
203 | * |
204 | * @lock: Pointer to a struct ttm_lock |
204 | * @lock: Pointer to a struct ttm_lock |
205 | * |
205 | * |
206 | * Releases a write lock. |
206 | * Releases a write lock. |
207 | */ |
207 | */ |
208 | extern void ttm_write_unlock(struct ttm_lock *lock); |
208 | extern void ttm_write_unlock(struct ttm_lock *lock); |
209 | 209 | ||
210 | /** |
210 | /** |
211 | * ttm_write_lock |
211 | * ttm_write_lock |
212 | * |
212 | * |
213 | * @lock: Pointer to a struct ttm_lock |
213 | * @lock: Pointer to a struct ttm_lock |
214 | * @interruptible: Interruptible sleeping while waiting for a lock. |
214 | * @interruptible: Interruptible sleeping while waiting for a lock. |
215 | * |
215 | * |
216 | * Takes the lock in write mode. |
216 | * Takes the lock in write mode. |
217 | * Returns: |
217 | * Returns: |
218 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
218 | * -ERESTARTSYS If interrupted by a signal and interruptible is true. |
219 | */ |
219 | */ |
220 | extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); |
220 | extern int ttm_write_lock(struct ttm_lock *lock, bool interruptible); |
221 | 221 | ||
222 | /** |
222 | /** |
223 | * ttm_lock_set_kill |
223 | * ttm_lock_set_kill |
224 | * |
224 | * |
225 | * @lock: Pointer to a struct ttm_lock |
225 | * @lock: Pointer to a struct ttm_lock |
226 | * @val: Boolean whether to kill processes taking the lock. |
226 | * @val: Boolean whether to kill processes taking the lock. |
227 | * @signal: Signal to send to the process taking the lock. |
227 | * @signal: Signal to send to the process taking the lock. |
228 | * |
228 | * |
229 | * The kill-when-taking-lock functionality is used to kill processes that keep |
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 |
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: |
231 | * example when the X server exits. A typical sequence would look like this: |
232 | * - X server takes lock in write mode. |
232 | * - X server takes lock in write mode. |
233 | * - ttm_lock_set_kill() is called with @val set to true. |
233 | * - ttm_lock_set_kill() is called with @val set to true. |
234 | * - As part of X server exit, TTM resources are taken down. |
234 | * - As part of X server exit, TTM resources are taken down. |
235 | * - X server releases the lock on file release. |
235 | * - X server releases the lock on file release. |
236 | * - Another dri client wants to render, takes the lock and is killed. |
236 | * - Another dri client wants to render, takes the lock and is killed. |
237 | * |
237 | * |
238 | */ |
238 | */ |
239 | static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val, |
239 | static inline void ttm_lock_set_kill(struct ttm_lock *lock, bool val, |
240 | int signal) |
240 | int signal) |
241 | { |
241 | { |
242 | lock->kill_takers = val; |
242 | lock->kill_takers = val; |
243 | if (val) |
243 | if (val) |
244 | lock->signal = signal; |
244 | lock->signal = signal; |
245 | } |
245 | } |
246 | 246 | ||
247 | #endif |
247 | #endif |