Commit 45ff46c54a31bf8924b61e3e3411654410a3b5c3
Committed by
Dave Airlie
1 parent
4a445f291a
Exists in
master
and in
7 other branches
drm: readd drm_lock_free in drm_unlock
I've accidently killed a little bit too much in commit 1da3f87ebb7edb3e0b829ec4bbe5fb3d9d93986f Author: Daniel Vetter <daniel.vetter@ffwll.ch> Date: Mon Aug 23 22:53:24 2010 +0200 drm: kill kernel_context_switch callbacks Note to self: Next time also test with AIGLX disabled. Reported-and-Tested-by: Andy Furniss <lists@andyfurniss.entadsl.com> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=30374 Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch> Signed-off-by: Dave Airlie <airlied@redhat.com>
Showing 1 changed file with 5 additions and 0 deletions Inline Diff
drivers/gpu/drm/drm_lock.c
1 | /** | 1 | /** |
2 | * \file drm_lock.c | 2 | * \file drm_lock.c |
3 | * IOCTLs for locking | 3 | * IOCTLs for locking |
4 | * | 4 | * |
5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> | 5 | * \author Rickard E. (Rik) Faith <faith@valinux.com> |
6 | * \author Gareth Hughes <gareth@valinux.com> | 6 | * \author Gareth Hughes <gareth@valinux.com> |
7 | */ | 7 | */ |
8 | 8 | ||
9 | /* | 9 | /* |
10 | * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com | 10 | * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com |
11 | * | 11 | * |
12 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. | 12 | * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. |
13 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. | 13 | * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. |
14 | * All Rights Reserved. | 14 | * All Rights Reserved. |
15 | * | 15 | * |
16 | * Permission is hereby granted, free of charge, to any person obtaining a | 16 | * Permission is hereby granted, free of charge, to any person obtaining a |
17 | * copy of this software and associated documentation files (the "Software"), | 17 | * copy of this software and associated documentation files (the "Software"), |
18 | * to deal in the Software without restriction, including without limitation | 18 | * to deal in the Software without restriction, including without limitation |
19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
20 | * and/or sell copies of the Software, and to permit persons to whom the | 20 | * and/or sell copies of the Software, and to permit persons to whom the |
21 | * Software is furnished to do so, subject to the following conditions: | 21 | * Software is furnished to do so, subject to the following conditions: |
22 | * | 22 | * |
23 | * The above copyright notice and this permission notice (including the next | 23 | * The above copyright notice and this permission notice (including the next |
24 | * paragraph) shall be included in all copies or substantial portions of the | 24 | * paragraph) shall be included in all copies or substantial portions of the |
25 | * Software. | 25 | * Software. |
26 | * | 26 | * |
27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 28 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 29 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
30 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR | 30 | * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
31 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | 31 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
32 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 32 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
33 | * OTHER DEALINGS IN THE SOFTWARE. | 33 | * OTHER DEALINGS IN THE SOFTWARE. |
34 | */ | 34 | */ |
35 | 35 | ||
36 | #include "drmP.h" | 36 | #include "drmP.h" |
37 | 37 | ||
38 | static int drm_notifier(void *priv); | 38 | static int drm_notifier(void *priv); |
39 | 39 | ||
40 | static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context); | 40 | static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context); |
41 | 41 | ||
42 | /** | 42 | /** |
43 | * Lock ioctl. | 43 | * Lock ioctl. |
44 | * | 44 | * |
45 | * \param inode device inode. | 45 | * \param inode device inode. |
46 | * \param file_priv DRM file private. | 46 | * \param file_priv DRM file private. |
47 | * \param cmd command. | 47 | * \param cmd command. |
48 | * \param arg user argument, pointing to a drm_lock structure. | 48 | * \param arg user argument, pointing to a drm_lock structure. |
49 | * \return zero on success or negative number on failure. | 49 | * \return zero on success or negative number on failure. |
50 | * | 50 | * |
51 | * Add the current task to the lock wait queue, and attempt to take to lock. | 51 | * Add the current task to the lock wait queue, and attempt to take to lock. |
52 | */ | 52 | */ |
53 | int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv) | 53 | int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv) |
54 | { | 54 | { |
55 | DECLARE_WAITQUEUE(entry, current); | 55 | DECLARE_WAITQUEUE(entry, current); |
56 | struct drm_lock *lock = data; | 56 | struct drm_lock *lock = data; |
57 | struct drm_master *master = file_priv->master; | 57 | struct drm_master *master = file_priv->master; |
58 | int ret = 0; | 58 | int ret = 0; |
59 | 59 | ||
60 | ++file_priv->lock_count; | 60 | ++file_priv->lock_count; |
61 | 61 | ||
62 | if (lock->context == DRM_KERNEL_CONTEXT) { | 62 | if (lock->context == DRM_KERNEL_CONTEXT) { |
63 | DRM_ERROR("Process %d using kernel context %d\n", | 63 | DRM_ERROR("Process %d using kernel context %d\n", |
64 | task_pid_nr(current), lock->context); | 64 | task_pid_nr(current), lock->context); |
65 | return -EINVAL; | 65 | return -EINVAL; |
66 | } | 66 | } |
67 | 67 | ||
68 | DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n", | 68 | DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n", |
69 | lock->context, task_pid_nr(current), | 69 | lock->context, task_pid_nr(current), |
70 | master->lock.hw_lock->lock, lock->flags); | 70 | master->lock.hw_lock->lock, lock->flags); |
71 | 71 | ||
72 | if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE)) | 72 | if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE)) |
73 | if (lock->context < 0) | 73 | if (lock->context < 0) |
74 | return -EINVAL; | 74 | return -EINVAL; |
75 | 75 | ||
76 | add_wait_queue(&master->lock.lock_queue, &entry); | 76 | add_wait_queue(&master->lock.lock_queue, &entry); |
77 | spin_lock_bh(&master->lock.spinlock); | 77 | spin_lock_bh(&master->lock.spinlock); |
78 | master->lock.user_waiters++; | 78 | master->lock.user_waiters++; |
79 | spin_unlock_bh(&master->lock.spinlock); | 79 | spin_unlock_bh(&master->lock.spinlock); |
80 | 80 | ||
81 | for (;;) { | 81 | for (;;) { |
82 | __set_current_state(TASK_INTERRUPTIBLE); | 82 | __set_current_state(TASK_INTERRUPTIBLE); |
83 | if (!master->lock.hw_lock) { | 83 | if (!master->lock.hw_lock) { |
84 | /* Device has been unregistered */ | 84 | /* Device has been unregistered */ |
85 | send_sig(SIGTERM, current, 0); | 85 | send_sig(SIGTERM, current, 0); |
86 | ret = -EINTR; | 86 | ret = -EINTR; |
87 | break; | 87 | break; |
88 | } | 88 | } |
89 | if (drm_lock_take(&master->lock, lock->context)) { | 89 | if (drm_lock_take(&master->lock, lock->context)) { |
90 | master->lock.file_priv = file_priv; | 90 | master->lock.file_priv = file_priv; |
91 | master->lock.lock_time = jiffies; | 91 | master->lock.lock_time = jiffies; |
92 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); | 92 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
93 | break; /* Got lock */ | 93 | break; /* Got lock */ |
94 | } | 94 | } |
95 | 95 | ||
96 | /* Contention */ | 96 | /* Contention */ |
97 | mutex_unlock(&drm_global_mutex); | 97 | mutex_unlock(&drm_global_mutex); |
98 | schedule(); | 98 | schedule(); |
99 | mutex_lock(&drm_global_mutex); | 99 | mutex_lock(&drm_global_mutex); |
100 | if (signal_pending(current)) { | 100 | if (signal_pending(current)) { |
101 | ret = -EINTR; | 101 | ret = -EINTR; |
102 | break; | 102 | break; |
103 | } | 103 | } |
104 | } | 104 | } |
105 | spin_lock_bh(&master->lock.spinlock); | 105 | spin_lock_bh(&master->lock.spinlock); |
106 | master->lock.user_waiters--; | 106 | master->lock.user_waiters--; |
107 | spin_unlock_bh(&master->lock.spinlock); | 107 | spin_unlock_bh(&master->lock.spinlock); |
108 | __set_current_state(TASK_RUNNING); | 108 | __set_current_state(TASK_RUNNING); |
109 | remove_wait_queue(&master->lock.lock_queue, &entry); | 109 | remove_wait_queue(&master->lock.lock_queue, &entry); |
110 | 110 | ||
111 | DRM_DEBUG("%d %s\n", lock->context, | 111 | DRM_DEBUG("%d %s\n", lock->context, |
112 | ret ? "interrupted" : "has lock"); | 112 | ret ? "interrupted" : "has lock"); |
113 | if (ret) return ret; | 113 | if (ret) return ret; |
114 | 114 | ||
115 | /* don't set the block all signals on the master process for now | 115 | /* don't set the block all signals on the master process for now |
116 | * really probably not the correct answer but lets us debug xkb | 116 | * really probably not the correct answer but lets us debug xkb |
117 | * xserver for now */ | 117 | * xserver for now */ |
118 | if (!file_priv->is_master) { | 118 | if (!file_priv->is_master) { |
119 | sigemptyset(&dev->sigmask); | 119 | sigemptyset(&dev->sigmask); |
120 | sigaddset(&dev->sigmask, SIGSTOP); | 120 | sigaddset(&dev->sigmask, SIGSTOP); |
121 | sigaddset(&dev->sigmask, SIGTSTP); | 121 | sigaddset(&dev->sigmask, SIGTSTP); |
122 | sigaddset(&dev->sigmask, SIGTTIN); | 122 | sigaddset(&dev->sigmask, SIGTTIN); |
123 | sigaddset(&dev->sigmask, SIGTTOU); | 123 | sigaddset(&dev->sigmask, SIGTTOU); |
124 | dev->sigdata.context = lock->context; | 124 | dev->sigdata.context = lock->context; |
125 | dev->sigdata.lock = master->lock.hw_lock; | 125 | dev->sigdata.lock = master->lock.hw_lock; |
126 | block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask); | 126 | block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask); |
127 | } | 127 | } |
128 | 128 | ||
129 | if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT)) | 129 | if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT)) |
130 | { | 130 | { |
131 | if (dev->driver->dma_quiescent(dev)) { | 131 | if (dev->driver->dma_quiescent(dev)) { |
132 | DRM_DEBUG("%d waiting for DMA quiescent\n", | 132 | DRM_DEBUG("%d waiting for DMA quiescent\n", |
133 | lock->context); | 133 | lock->context); |
134 | return -EBUSY; | 134 | return -EBUSY; |
135 | } | 135 | } |
136 | } | 136 | } |
137 | 137 | ||
138 | return 0; | 138 | return 0; |
139 | } | 139 | } |
140 | 140 | ||
141 | /** | 141 | /** |
142 | * Unlock ioctl. | 142 | * Unlock ioctl. |
143 | * | 143 | * |
144 | * \param inode device inode. | 144 | * \param inode device inode. |
145 | * \param file_priv DRM file private. | 145 | * \param file_priv DRM file private. |
146 | * \param cmd command. | 146 | * \param cmd command. |
147 | * \param arg user argument, pointing to a drm_lock structure. | 147 | * \param arg user argument, pointing to a drm_lock structure. |
148 | * \return zero on success or negative number on failure. | 148 | * \return zero on success or negative number on failure. |
149 | * | 149 | * |
150 | * Transfer and free the lock. | 150 | * Transfer and free the lock. |
151 | */ | 151 | */ |
152 | int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv) | 152 | int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv) |
153 | { | 153 | { |
154 | struct drm_lock *lock = data; | 154 | struct drm_lock *lock = data; |
155 | struct drm_master *master = file_priv->master; | ||
155 | 156 | ||
156 | if (lock->context == DRM_KERNEL_CONTEXT) { | 157 | if (lock->context == DRM_KERNEL_CONTEXT) { |
157 | DRM_ERROR("Process %d using kernel context %d\n", | 158 | DRM_ERROR("Process %d using kernel context %d\n", |
158 | task_pid_nr(current), lock->context); | 159 | task_pid_nr(current), lock->context); |
159 | return -EINVAL; | 160 | return -EINVAL; |
160 | } | 161 | } |
161 | 162 | ||
162 | atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]); | 163 | atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]); |
164 | |||
165 | if (drm_lock_free(&master->lock, lock->context)) { | ||
166 | /* FIXME: Should really bail out here. */ | ||
167 | } | ||
163 | 168 | ||
164 | unblock_all_signals(); | 169 | unblock_all_signals(); |
165 | return 0; | 170 | return 0; |
166 | } | 171 | } |
167 | 172 | ||
168 | /** | 173 | /** |
169 | * Take the heavyweight lock. | 174 | * Take the heavyweight lock. |
170 | * | 175 | * |
171 | * \param lock lock pointer. | 176 | * \param lock lock pointer. |
172 | * \param context locking context. | 177 | * \param context locking context. |
173 | * \return one if the lock is held, or zero otherwise. | 178 | * \return one if the lock is held, or zero otherwise. |
174 | * | 179 | * |
175 | * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction. | 180 | * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction. |
176 | */ | 181 | */ |
177 | static | 182 | static |
178 | int drm_lock_take(struct drm_lock_data *lock_data, | 183 | int drm_lock_take(struct drm_lock_data *lock_data, |
179 | unsigned int context) | 184 | unsigned int context) |
180 | { | 185 | { |
181 | unsigned int old, new, prev; | 186 | unsigned int old, new, prev; |
182 | volatile unsigned int *lock = &lock_data->hw_lock->lock; | 187 | volatile unsigned int *lock = &lock_data->hw_lock->lock; |
183 | 188 | ||
184 | spin_lock_bh(&lock_data->spinlock); | 189 | spin_lock_bh(&lock_data->spinlock); |
185 | do { | 190 | do { |
186 | old = *lock; | 191 | old = *lock; |
187 | if (old & _DRM_LOCK_HELD) | 192 | if (old & _DRM_LOCK_HELD) |
188 | new = old | _DRM_LOCK_CONT; | 193 | new = old | _DRM_LOCK_CONT; |
189 | else { | 194 | else { |
190 | new = context | _DRM_LOCK_HELD | | 195 | new = context | _DRM_LOCK_HELD | |
191 | ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ? | 196 | ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ? |
192 | _DRM_LOCK_CONT : 0); | 197 | _DRM_LOCK_CONT : 0); |
193 | } | 198 | } |
194 | prev = cmpxchg(lock, old, new); | 199 | prev = cmpxchg(lock, old, new); |
195 | } while (prev != old); | 200 | } while (prev != old); |
196 | spin_unlock_bh(&lock_data->spinlock); | 201 | spin_unlock_bh(&lock_data->spinlock); |
197 | 202 | ||
198 | if (_DRM_LOCKING_CONTEXT(old) == context) { | 203 | if (_DRM_LOCKING_CONTEXT(old) == context) { |
199 | if (old & _DRM_LOCK_HELD) { | 204 | if (old & _DRM_LOCK_HELD) { |
200 | if (context != DRM_KERNEL_CONTEXT) { | 205 | if (context != DRM_KERNEL_CONTEXT) { |
201 | DRM_ERROR("%d holds heavyweight lock\n", | 206 | DRM_ERROR("%d holds heavyweight lock\n", |
202 | context); | 207 | context); |
203 | } | 208 | } |
204 | return 0; | 209 | return 0; |
205 | } | 210 | } |
206 | } | 211 | } |
207 | 212 | ||
208 | if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) { | 213 | if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) { |
209 | /* Have lock */ | 214 | /* Have lock */ |
210 | return 1; | 215 | return 1; |
211 | } | 216 | } |
212 | return 0; | 217 | return 0; |
213 | } | 218 | } |
214 | 219 | ||
215 | /** | 220 | /** |
216 | * This takes a lock forcibly and hands it to context. Should ONLY be used | 221 | * This takes a lock forcibly and hands it to context. Should ONLY be used |
217 | * inside *_unlock to give lock to kernel before calling *_dma_schedule. | 222 | * inside *_unlock to give lock to kernel before calling *_dma_schedule. |
218 | * | 223 | * |
219 | * \param dev DRM device. | 224 | * \param dev DRM device. |
220 | * \param lock lock pointer. | 225 | * \param lock lock pointer. |
221 | * \param context locking context. | 226 | * \param context locking context. |
222 | * \return always one. | 227 | * \return always one. |
223 | * | 228 | * |
224 | * Resets the lock file pointer. | 229 | * Resets the lock file pointer. |
225 | * Marks the lock as held by the given context, via the \p cmpxchg instruction. | 230 | * Marks the lock as held by the given context, via the \p cmpxchg instruction. |
226 | */ | 231 | */ |
227 | static int drm_lock_transfer(struct drm_lock_data *lock_data, | 232 | static int drm_lock_transfer(struct drm_lock_data *lock_data, |
228 | unsigned int context) | 233 | unsigned int context) |
229 | { | 234 | { |
230 | unsigned int old, new, prev; | 235 | unsigned int old, new, prev; |
231 | volatile unsigned int *lock = &lock_data->hw_lock->lock; | 236 | volatile unsigned int *lock = &lock_data->hw_lock->lock; |
232 | 237 | ||
233 | lock_data->file_priv = NULL; | 238 | lock_data->file_priv = NULL; |
234 | do { | 239 | do { |
235 | old = *lock; | 240 | old = *lock; |
236 | new = context | _DRM_LOCK_HELD; | 241 | new = context | _DRM_LOCK_HELD; |
237 | prev = cmpxchg(lock, old, new); | 242 | prev = cmpxchg(lock, old, new); |
238 | } while (prev != old); | 243 | } while (prev != old); |
239 | return 1; | 244 | return 1; |
240 | } | 245 | } |
241 | 246 | ||
242 | /** | 247 | /** |
243 | * Free lock. | 248 | * Free lock. |
244 | * | 249 | * |
245 | * \param dev DRM device. | 250 | * \param dev DRM device. |
246 | * \param lock lock. | 251 | * \param lock lock. |
247 | * \param context context. | 252 | * \param context context. |
248 | * | 253 | * |
249 | * Resets the lock file pointer. | 254 | * Resets the lock file pointer. |
250 | * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task | 255 | * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task |
251 | * waiting on the lock queue. | 256 | * waiting on the lock queue. |
252 | */ | 257 | */ |
253 | int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context) | 258 | int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context) |
254 | { | 259 | { |
255 | unsigned int old, new, prev; | 260 | unsigned int old, new, prev; |
256 | volatile unsigned int *lock = &lock_data->hw_lock->lock; | 261 | volatile unsigned int *lock = &lock_data->hw_lock->lock; |
257 | 262 | ||
258 | spin_lock_bh(&lock_data->spinlock); | 263 | spin_lock_bh(&lock_data->spinlock); |
259 | if (lock_data->kernel_waiters != 0) { | 264 | if (lock_data->kernel_waiters != 0) { |
260 | drm_lock_transfer(lock_data, 0); | 265 | drm_lock_transfer(lock_data, 0); |
261 | lock_data->idle_has_lock = 1; | 266 | lock_data->idle_has_lock = 1; |
262 | spin_unlock_bh(&lock_data->spinlock); | 267 | spin_unlock_bh(&lock_data->spinlock); |
263 | return 1; | 268 | return 1; |
264 | } | 269 | } |
265 | spin_unlock_bh(&lock_data->spinlock); | 270 | spin_unlock_bh(&lock_data->spinlock); |
266 | 271 | ||
267 | do { | 272 | do { |
268 | old = *lock; | 273 | old = *lock; |
269 | new = _DRM_LOCKING_CONTEXT(old); | 274 | new = _DRM_LOCKING_CONTEXT(old); |
270 | prev = cmpxchg(lock, old, new); | 275 | prev = cmpxchg(lock, old, new); |
271 | } while (prev != old); | 276 | } while (prev != old); |
272 | 277 | ||
273 | if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) { | 278 | if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) { |
274 | DRM_ERROR("%d freed heavyweight lock held by %d\n", | 279 | DRM_ERROR("%d freed heavyweight lock held by %d\n", |
275 | context, _DRM_LOCKING_CONTEXT(old)); | 280 | context, _DRM_LOCKING_CONTEXT(old)); |
276 | return 1; | 281 | return 1; |
277 | } | 282 | } |
278 | wake_up_interruptible(&lock_data->lock_queue); | 283 | wake_up_interruptible(&lock_data->lock_queue); |
279 | return 0; | 284 | return 0; |
280 | } | 285 | } |
281 | 286 | ||
282 | /** | 287 | /** |
283 | * If we get here, it means that the process has called DRM_IOCTL_LOCK | 288 | * If we get here, it means that the process has called DRM_IOCTL_LOCK |
284 | * without calling DRM_IOCTL_UNLOCK. | 289 | * without calling DRM_IOCTL_UNLOCK. |
285 | * | 290 | * |
286 | * If the lock is not held, then let the signal proceed as usual. If the lock | 291 | * If the lock is not held, then let the signal proceed as usual. If the lock |
287 | * is held, then set the contended flag and keep the signal blocked. | 292 | * is held, then set the contended flag and keep the signal blocked. |
288 | * | 293 | * |
289 | * \param priv pointer to a drm_sigdata structure. | 294 | * \param priv pointer to a drm_sigdata structure. |
290 | * \return one if the signal should be delivered normally, or zero if the | 295 | * \return one if the signal should be delivered normally, or zero if the |
291 | * signal should be blocked. | 296 | * signal should be blocked. |
292 | */ | 297 | */ |
293 | static int drm_notifier(void *priv) | 298 | static int drm_notifier(void *priv) |
294 | { | 299 | { |
295 | struct drm_sigdata *s = (struct drm_sigdata *) priv; | 300 | struct drm_sigdata *s = (struct drm_sigdata *) priv; |
296 | unsigned int old, new, prev; | 301 | unsigned int old, new, prev; |
297 | 302 | ||
298 | /* Allow signal delivery if lock isn't held */ | 303 | /* Allow signal delivery if lock isn't held */ |
299 | if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock) | 304 | if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock) |
300 | || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) | 305 | || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) |
301 | return 1; | 306 | return 1; |
302 | 307 | ||
303 | /* Otherwise, set flag to force call to | 308 | /* Otherwise, set flag to force call to |
304 | drmUnlock */ | 309 | drmUnlock */ |
305 | do { | 310 | do { |
306 | old = s->lock->lock; | 311 | old = s->lock->lock; |
307 | new = old | _DRM_LOCK_CONT; | 312 | new = old | _DRM_LOCK_CONT; |
308 | prev = cmpxchg(&s->lock->lock, old, new); | 313 | prev = cmpxchg(&s->lock->lock, old, new); |
309 | } while (prev != old); | 314 | } while (prev != old); |
310 | return 0; | 315 | return 0; |
311 | } | 316 | } |
312 | 317 | ||
313 | /** | 318 | /** |
314 | * This function returns immediately and takes the hw lock | 319 | * This function returns immediately and takes the hw lock |
315 | * with the kernel context if it is free, otherwise it gets the highest priority when and if | 320 | * with the kernel context if it is free, otherwise it gets the highest priority when and if |
316 | * it is eventually released. | 321 | * it is eventually released. |
317 | * | 322 | * |
318 | * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held | 323 | * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held |
319 | * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause | 324 | * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause |
320 | * a deadlock, which is why the "idlelock" was invented). | 325 | * a deadlock, which is why the "idlelock" was invented). |
321 | * | 326 | * |
322 | * This should be sufficient to wait for GPU idle without | 327 | * This should be sufficient to wait for GPU idle without |
323 | * having to worry about starvation. | 328 | * having to worry about starvation. |
324 | */ | 329 | */ |
325 | 330 | ||
326 | void drm_idlelock_take(struct drm_lock_data *lock_data) | 331 | void drm_idlelock_take(struct drm_lock_data *lock_data) |
327 | { | 332 | { |
328 | int ret = 0; | 333 | int ret = 0; |
329 | 334 | ||
330 | spin_lock_bh(&lock_data->spinlock); | 335 | spin_lock_bh(&lock_data->spinlock); |
331 | lock_data->kernel_waiters++; | 336 | lock_data->kernel_waiters++; |
332 | if (!lock_data->idle_has_lock) { | 337 | if (!lock_data->idle_has_lock) { |
333 | 338 | ||
334 | spin_unlock_bh(&lock_data->spinlock); | 339 | spin_unlock_bh(&lock_data->spinlock); |
335 | ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT); | 340 | ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT); |
336 | spin_lock_bh(&lock_data->spinlock); | 341 | spin_lock_bh(&lock_data->spinlock); |
337 | 342 | ||
338 | if (ret == 1) | 343 | if (ret == 1) |
339 | lock_data->idle_has_lock = 1; | 344 | lock_data->idle_has_lock = 1; |
340 | } | 345 | } |
341 | spin_unlock_bh(&lock_data->spinlock); | 346 | spin_unlock_bh(&lock_data->spinlock); |
342 | } | 347 | } |
343 | 348 | ||
344 | void drm_idlelock_release(struct drm_lock_data *lock_data) | 349 | void drm_idlelock_release(struct drm_lock_data *lock_data) |
345 | { | 350 | { |
346 | unsigned int old, prev; | 351 | unsigned int old, prev; |
347 | volatile unsigned int *lock = &lock_data->hw_lock->lock; | 352 | volatile unsigned int *lock = &lock_data->hw_lock->lock; |
348 | 353 | ||
349 | spin_lock_bh(&lock_data->spinlock); | 354 | spin_lock_bh(&lock_data->spinlock); |
350 | if (--lock_data->kernel_waiters == 0) { | 355 | if (--lock_data->kernel_waiters == 0) { |
351 | if (lock_data->idle_has_lock) { | 356 | if (lock_data->idle_has_lock) { |
352 | do { | 357 | do { |
353 | old = *lock; | 358 | old = *lock; |
354 | prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT); | 359 | prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT); |
355 | } while (prev != old); | 360 | } while (prev != old); |
356 | wake_up_interruptible(&lock_data->lock_queue); | 361 | wake_up_interruptible(&lock_data->lock_queue); |
357 | lock_data->idle_has_lock = 0; | 362 | lock_data->idle_has_lock = 0; |
358 | } | 363 | } |
359 | } | 364 | } |
360 | spin_unlock_bh(&lock_data->spinlock); | 365 | spin_unlock_bh(&lock_data->spinlock); |
361 | } | 366 | } |
362 | 367 | ||
363 | int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv) | 368 | int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv) |
364 | { | 369 | { |
365 | struct drm_master *master = file_priv->master; | 370 | struct drm_master *master = file_priv->master; |
366 | return (file_priv->lock_count && master->lock.hw_lock && | 371 | return (file_priv->lock_count && master->lock.hw_lock && |
367 | _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) && | 372 | _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) && |
368 | master->lock.file_priv == file_priv); | 373 | master->lock.file_priv == file_priv); |
369 | } | 374 | } |
370 | 375 |