Merge branches 'topic/slob/cleanups', 'topic/slob/fixes', 'topic/slub/core', 'topic...
[linux-block.git] / drivers / gpu / drm / drm_lock.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_lock.c
1da177e4 3 * IOCTLs for locking
b5e89ed5 4 *
1da177e4
LT
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
11 *
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
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
21 * Software is furnished to do so, subject to the following conditions:
22 *
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
25 * Software.
26 *
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,
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
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
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include "drmP.h"
37
c94f7029
DA
38static int drm_notifier(void *priv);
39
b5e89ed5 40/**
1da177e4
LT
41 * Lock ioctl.
42 *
43 * \param inode device inode.
6c340eac 44 * \param file_priv DRM file private.
1da177e4
LT
45 * \param cmd command.
46 * \param arg user argument, pointing to a drm_lock structure.
47 * \return zero on success or negative number on failure.
48 *
49 * Add the current task to the lock wait queue, and attempt to take to lock.
50 */
c153f45f 51int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 52{
b5e89ed5 53 DECLARE_WAITQUEUE(entry, current);
c153f45f 54 struct drm_lock *lock = data;
7c1c2871 55 struct drm_master *master = file_priv->master;
b5e89ed5 56 int ret = 0;
1da177e4 57
6c340eac 58 ++file_priv->lock_count;
1da177e4 59
c153f45f 60 if (lock->context == DRM_KERNEL_CONTEXT) {
b5e89ed5 61 DRM_ERROR("Process %d using kernel context %d\n",
ba25f9dc 62 task_pid_nr(current), lock->context);
b5e89ed5
DA
63 return -EINVAL;
64 }
1da177e4 65
b5e89ed5 66 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
ba25f9dc 67 lock->context, task_pid_nr(current),
7c1c2871 68 master->lock.hw_lock->lock, lock->flags);
1da177e4
LT
69
70 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
c153f45f 71 if (lock->context < 0)
1da177e4
LT
72 return -EINVAL;
73
7c1c2871
DA
74 add_wait_queue(&master->lock.lock_queue, &entry);
75 spin_lock_bh(&master->lock.spinlock);
76 master->lock.user_waiters++;
77 spin_unlock_bh(&master->lock.spinlock);
78
1da177e4
LT
79 for (;;) {
80 __set_current_state(TASK_INTERRUPTIBLE);
7c1c2871 81 if (!master->lock.hw_lock) {
1da177e4 82 /* Device has been unregistered */
fda714c2 83 send_sig(SIGTERM, current, 0);
1da177e4
LT
84 ret = -EINTR;
85 break;
86 }
7c1c2871
DA
87 if (drm_lock_take(&master->lock, lock->context)) {
88 master->lock.file_priv = file_priv;
89 master->lock.lock_time = jiffies;
b5e89ed5
DA
90 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
91 break; /* Got lock */
1da177e4 92 }
b5e89ed5 93
1da177e4
LT
94 /* Contention */
95 schedule();
b5e89ed5 96 if (signal_pending(current)) {
4d77c88e 97 ret = -EINTR;
1da177e4
LT
98 break;
99 }
100 }
7c1c2871
DA
101 spin_lock_bh(&master->lock.spinlock);
102 master->lock.user_waiters--;
103 spin_unlock_bh(&master->lock.spinlock);
1da177e4 104 __set_current_state(TASK_RUNNING);
7c1c2871 105 remove_wait_queue(&master->lock.lock_queue, &entry);
1da177e4 106
c153f45f
EA
107 DRM_DEBUG("%d %s\n", lock->context,
108 ret ? "interrupted" : "has lock");
040ac320 109 if (ret) return ret;
d985c108 110
3e5fc80a
DA
111 /* don't set the block all signals on the master process for now
112 * really probably not the correct answer but lets us debug xkb
113 * xserver for now */
7c1c2871 114 if (!file_priv->is_master) {
3e5fc80a
DA
115 sigemptyset(&dev->sigmask);
116 sigaddset(&dev->sigmask, SIGSTOP);
117 sigaddset(&dev->sigmask, SIGTSTP);
118 sigaddset(&dev->sigmask, SIGTTIN);
119 sigaddset(&dev->sigmask, SIGTTOU);
120 dev->sigdata.context = lock->context;
7c1c2871 121 dev->sigdata.lock = master->lock.hw_lock;
3e5fc80a
DA
122 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
123 }
b5e89ed5 124
c153f45f 125 if (dev->driver->dma_ready && (lock->flags & _DRM_LOCK_READY))
1da177e4 126 dev->driver->dma_ready(dev);
b5e89ed5 127
c153f45f
EA
128 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
129 {
d985c108 130 if (dev->driver->dma_quiescent(dev)) {
c153f45f
EA
131 DRM_DEBUG("%d waiting for DMA quiescent\n",
132 lock->context);
20caafa6 133 return -EBUSY;
d985c108
DA
134 }
135 }
b5e89ed5
DA
136
137 if (dev->driver->kernel_context_switch &&
c153f45f 138 dev->last_context != lock->context) {
b5e89ed5 139 dev->driver->kernel_context_switch(dev, dev->last_context,
c153f45f 140 lock->context);
1da177e4 141 }
040ac320 142
d985c108 143 return 0;
1da177e4
LT
144}
145
b5e89ed5 146/**
1da177e4
LT
147 * Unlock ioctl.
148 *
149 * \param inode device inode.
6c340eac 150 * \param file_priv DRM file private.
1da177e4
LT
151 * \param cmd command.
152 * \param arg user argument, pointing to a drm_lock structure.
153 * \return zero on success or negative number on failure.
154 *
155 * Transfer and free the lock.
156 */
c153f45f 157int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
1da177e4 158{
c153f45f 159 struct drm_lock *lock = data;
7c1c2871 160 struct drm_master *master = file_priv->master;
1da177e4 161
c153f45f 162 if (lock->context == DRM_KERNEL_CONTEXT) {
b5e89ed5 163 DRM_ERROR("Process %d using kernel context %d\n",
ba25f9dc 164 task_pid_nr(current), lock->context);
1da177e4
LT
165 return -EINVAL;
166 }
167
b5e89ed5 168 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
1da177e4
LT
169
170 /* kernel_context_switch isn't used by any of the x86 drm
171 * modules but is required by the Sparc driver.
172 */
173 if (dev->driver->kernel_context_switch_unlock)
0c4dd906 174 dev->driver->kernel_context_switch_unlock(dev);
1da177e4 175 else {
7c1c2871 176 if (drm_lock_free(&master->lock, lock->context)) {
040ac320 177 /* FIXME: Should really bail out here. */
1da177e4
LT
178 }
179 }
180
181 unblock_all_signals();
182 return 0;
183}
184
185/**
186 * Take the heavyweight lock.
187 *
188 * \param lock lock pointer.
189 * \param context locking context.
190 * \return one if the lock is held, or zero otherwise.
191 *
192 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
193 */
55910517 194int drm_lock_take(struct drm_lock_data *lock_data,
040ac320 195 unsigned int context)
1da177e4
LT
196{
197 unsigned int old, new, prev;
040ac320 198 volatile unsigned int *lock = &lock_data->hw_lock->lock;
1da177e4 199
f116cc56 200 spin_lock_bh(&lock_data->spinlock);
1da177e4
LT
201 do {
202 old = *lock;
b5e89ed5
DA
203 if (old & _DRM_LOCK_HELD)
204 new = old | _DRM_LOCK_CONT;
040ac320
TH
205 else {
206 new = context | _DRM_LOCK_HELD |
207 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
208 _DRM_LOCK_CONT : 0);
209 }
1da177e4
LT
210 prev = cmpxchg(lock, old, new);
211 } while (prev != old);
f116cc56 212 spin_unlock_bh(&lock_data->spinlock);
040ac320 213
1da177e4
LT
214 if (_DRM_LOCKING_CONTEXT(old) == context) {
215 if (old & _DRM_LOCK_HELD) {
216 if (context != DRM_KERNEL_CONTEXT) {
217 DRM_ERROR("%d holds heavyweight lock\n",
218 context);
219 }
220 return 0;
221 }
222 }
040ac320
TH
223
224 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
b5e89ed5 225 /* Have lock */
1da177e4
LT
226 return 1;
227 }
228 return 0;
229}
9e44af79 230EXPORT_SYMBOL(drm_lock_take);
1da177e4
LT
231
232/**
233 * This takes a lock forcibly and hands it to context. Should ONLY be used
b5e89ed5
DA
234 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
235 *
1da177e4
LT
236 * \param dev DRM device.
237 * \param lock lock pointer.
238 * \param context locking context.
239 * \return always one.
240 *
241 * Resets the lock file pointer.
242 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
243 */
55910517 244static int drm_lock_transfer(struct drm_lock_data *lock_data,
c94f7029 245 unsigned int context)
1da177e4
LT
246{
247 unsigned int old, new, prev;
040ac320 248 volatile unsigned int *lock = &lock_data->hw_lock->lock;
1da177e4 249
6c340eac 250 lock_data->file_priv = NULL;
1da177e4 251 do {
b5e89ed5
DA
252 old = *lock;
253 new = context | _DRM_LOCK_HELD;
1da177e4
LT
254 prev = cmpxchg(lock, old, new);
255 } while (prev != old);
256 return 1;
257}
258
259/**
260 * Free lock.
b5e89ed5 261 *
1da177e4
LT
262 * \param dev DRM device.
263 * \param lock lock.
264 * \param context context.
b5e89ed5 265 *
1da177e4
LT
266 * Resets the lock file pointer.
267 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
268 * waiting on the lock queue.
269 */
55910517 270int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
1da177e4
LT
271{
272 unsigned int old, new, prev;
040ac320
TH
273 volatile unsigned int *lock = &lock_data->hw_lock->lock;
274
f116cc56 275 spin_lock_bh(&lock_data->spinlock);
040ac320
TH
276 if (lock_data->kernel_waiters != 0) {
277 drm_lock_transfer(lock_data, 0);
278 lock_data->idle_has_lock = 1;
f116cc56 279 spin_unlock_bh(&lock_data->spinlock);
040ac320
TH
280 return 1;
281 }
f116cc56 282 spin_unlock_bh(&lock_data->spinlock);
1da177e4 283
1da177e4 284 do {
b5e89ed5 285 old = *lock;
040ac320 286 new = _DRM_LOCKING_CONTEXT(old);
1da177e4
LT
287 prev = cmpxchg(lock, old, new);
288 } while (prev != old);
040ac320 289
1da177e4
LT
290 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
291 DRM_ERROR("%d freed heavyweight lock held by %d\n",
b5e89ed5 292 context, _DRM_LOCKING_CONTEXT(old));
1da177e4
LT
293 return 1;
294 }
040ac320 295 wake_up_interruptible(&lock_data->lock_queue);
1da177e4
LT
296 return 0;
297}
9e44af79 298EXPORT_SYMBOL(drm_lock_free);
1da177e4
LT
299
300/**
301 * If we get here, it means that the process has called DRM_IOCTL_LOCK
302 * without calling DRM_IOCTL_UNLOCK.
303 *
304 * If the lock is not held, then let the signal proceed as usual. If the lock
305 * is held, then set the contended flag and keep the signal blocked.
306 *
307 * \param priv pointer to a drm_sigdata structure.
308 * \return one if the signal should be delivered normally, or zero if the
309 * signal should be blocked.
310 */
c94f7029 311static int drm_notifier(void *priv)
1da177e4 312{
55910517 313 struct drm_sigdata *s = (struct drm_sigdata *) priv;
b5e89ed5 314 unsigned int old, new, prev;
1da177e4 315
b5e89ed5 316 /* Allow signal delivery if lock isn't held */
1da177e4 317 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
b5e89ed5
DA
318 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
319 return 1;
1da177e4 320
b5e89ed5
DA
321 /* Otherwise, set flag to force call to
322 drmUnlock */
1da177e4 323 do {
b5e89ed5
DA
324 old = s->lock->lock;
325 new = old | _DRM_LOCK_CONT;
1da177e4
LT
326 prev = cmpxchg(&s->lock->lock, old, new);
327 } while (prev != old);
328 return 0;
329}
040ac320
TH
330
331/**
332 * This function returns immediately and takes the hw lock
333 * with the kernel context if it is free, otherwise it gets the highest priority when and if
334 * it is eventually released.
335 *
336 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
337 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
338 * a deadlock, which is why the "idlelock" was invented).
339 *
340 * This should be sufficient to wait for GPU idle without
341 * having to worry about starvation.
342 */
343
55910517 344void drm_idlelock_take(struct drm_lock_data *lock_data)
040ac320
TH
345{
346 int ret = 0;
347
f116cc56 348 spin_lock_bh(&lock_data->spinlock);
040ac320
TH
349 lock_data->kernel_waiters++;
350 if (!lock_data->idle_has_lock) {
351
f116cc56 352 spin_unlock_bh(&lock_data->spinlock);
040ac320 353 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
f116cc56 354 spin_lock_bh(&lock_data->spinlock);
040ac320
TH
355
356 if (ret == 1)
357 lock_data->idle_has_lock = 1;
358 }
f116cc56 359 spin_unlock_bh(&lock_data->spinlock);
040ac320
TH
360}
361EXPORT_SYMBOL(drm_idlelock_take);
362
55910517 363void drm_idlelock_release(struct drm_lock_data *lock_data)
040ac320
TH
364{
365 unsigned int old, prev;
366 volatile unsigned int *lock = &lock_data->hw_lock->lock;
367
f116cc56 368 spin_lock_bh(&lock_data->spinlock);
040ac320
TH
369 if (--lock_data->kernel_waiters == 0) {
370 if (lock_data->idle_has_lock) {
371 do {
372 old = *lock;
373 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
374 } while (prev != old);
375 wake_up_interruptible(&lock_data->lock_queue);
376 lock_data->idle_has_lock = 0;
377 }
378 }
f116cc56 379 spin_unlock_bh(&lock_data->spinlock);
040ac320
TH
380}
381EXPORT_SYMBOL(drm_idlelock_release);
382
383
c153f45f 384int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
040ac320 385{
7c1c2871
DA
386 struct drm_master *master = file_priv->master;
387 return (file_priv->lock_count && master->lock.hw_lock &&
388 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
389 master->lock.file_priv == file_priv);
040ac320
TH
390}
391
392EXPORT_SYMBOL(drm_i_have_hw_lock);