[PATCH] gfp_t: drivers/scsi
[linux-2.6/mini2440.git] / drivers / char / drm / drm_lock.c
blob4702d863bcc62b11759739818e142c26480b49d2
1 /**
2 * \file drm_lock.h
3 * IOCTLs for locking
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
9 /*
10 * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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.
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.
36 #include "drmP.h"
38 static int drm_lock_transfer(drm_device_t *dev,
39 __volatile__ unsigned int *lock,
40 unsigned int context);
41 static int drm_notifier(void *priv);
43 /**
44 * Lock ioctl.
46 * \param inode device inode.
47 * \param filp file pointer.
48 * \param cmd command.
49 * \param arg user argument, pointing to a drm_lock structure.
50 * \return zero on success or negative number on failure.
52 * Add the current task to the lock wait queue, and attempt to take to lock.
54 int drm_lock( struct inode *inode, struct file *filp,
55 unsigned int cmd, unsigned long arg )
57 drm_file_t *priv = filp->private_data;
58 drm_device_t *dev = priv->head->dev;
59 DECLARE_WAITQUEUE( entry, current );
60 drm_lock_t lock;
61 int ret = 0;
63 ++priv->lock_count;
65 if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
66 return -EFAULT;
68 if ( lock.context == DRM_KERNEL_CONTEXT ) {
69 DRM_ERROR( "Process %d using kernel context %d\n",
70 current->pid, lock.context );
71 return -EINVAL;
74 DRM_DEBUG( "%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
75 lock.context, current->pid,
76 dev->lock.hw_lock->lock, lock.flags );
78 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
79 if ( lock.context < 0 )
80 return -EINVAL;
82 add_wait_queue( &dev->lock.lock_queue, &entry );
83 for (;;) {
84 __set_current_state(TASK_INTERRUPTIBLE);
85 if ( !dev->lock.hw_lock ) {
86 /* Device has been unregistered */
87 ret = -EINTR;
88 break;
90 if ( drm_lock_take( &dev->lock.hw_lock->lock,
91 lock.context ) ) {
92 dev->lock.filp = filp;
93 dev->lock.lock_time = jiffies;
94 atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
95 break; /* Got lock */
98 /* Contention */
99 schedule();
100 if ( signal_pending( current ) ) {
101 ret = -ERESTARTSYS;
102 break;
105 __set_current_state(TASK_RUNNING);
106 remove_wait_queue( &dev->lock.lock_queue, &entry );
108 sigemptyset( &dev->sigmask );
109 sigaddset( &dev->sigmask, SIGSTOP );
110 sigaddset( &dev->sigmask, SIGTSTP );
111 sigaddset( &dev->sigmask, SIGTTIN );
112 sigaddset( &dev->sigmask, SIGTTOU );
113 dev->sigdata.context = lock.context;
114 dev->sigdata.lock = dev->lock.hw_lock;
115 block_all_signals( drm_notifier,
116 &dev->sigdata, &dev->sigmask );
118 if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
119 dev->driver->dma_ready(dev);
121 if ( dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT ))
122 return dev->driver->dma_quiescent(dev);
124 /* dev->driver->kernel_context_switch isn't used by any of the x86
125 * drivers but is used by the Sparc driver.
128 if (dev->driver->kernel_context_switch &&
129 dev->last_context != lock.context) {
130 dev->driver->kernel_context_switch(dev, dev->last_context,
131 lock.context);
133 DRM_DEBUG( "%d %s\n", lock.context, ret ? "interrupted" : "has lock" );
135 return ret;
138 /**
139 * Unlock ioctl.
141 * \param inode device inode.
142 * \param filp file pointer.
143 * \param cmd command.
144 * \param arg user argument, pointing to a drm_lock structure.
145 * \return zero on success or negative number on failure.
147 * Transfer and free the lock.
149 int drm_unlock( struct inode *inode, struct file *filp,
150 unsigned int cmd, unsigned long arg )
152 drm_file_t *priv = filp->private_data;
153 drm_device_t *dev = priv->head->dev;
154 drm_lock_t lock;
156 if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
157 return -EFAULT;
159 if ( lock.context == DRM_KERNEL_CONTEXT ) {
160 DRM_ERROR( "Process %d using kernel context %d\n",
161 current->pid, lock.context );
162 return -EINVAL;
165 atomic_inc( &dev->counts[_DRM_STAT_UNLOCKS] );
167 /* kernel_context_switch isn't used by any of the x86 drm
168 * modules but is required by the Sparc driver.
170 if (dev->driver->kernel_context_switch_unlock)
171 dev->driver->kernel_context_switch_unlock(dev, &lock);
172 else {
173 drm_lock_transfer( dev, &dev->lock.hw_lock->lock,
174 DRM_KERNEL_CONTEXT );
176 if ( drm_lock_free( dev, &dev->lock.hw_lock->lock,
177 DRM_KERNEL_CONTEXT ) ) {
178 DRM_ERROR( "\n" );
182 unblock_all_signals();
183 return 0;
187 * Take the heavyweight lock.
189 * \param lock lock pointer.
190 * \param context locking context.
191 * \return one if the lock is held, or zero otherwise.
193 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
195 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
197 unsigned int old, new, prev;
199 do {
200 old = *lock;
201 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
202 else new = context | _DRM_LOCK_HELD;
203 prev = cmpxchg(lock, old, new);
204 } while (prev != old);
205 if (_DRM_LOCKING_CONTEXT(old) == context) {
206 if (old & _DRM_LOCK_HELD) {
207 if (context != DRM_KERNEL_CONTEXT) {
208 DRM_ERROR("%d holds heavyweight lock\n",
209 context);
211 return 0;
214 if (new == (context | _DRM_LOCK_HELD)) {
215 /* Have lock */
216 return 1;
218 return 0;
222 * This takes a lock forcibly and hands it to context. Should ONLY be used
223 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
225 * \param dev DRM device.
226 * \param lock lock pointer.
227 * \param context locking context.
228 * \return always one.
230 * Resets the lock file pointer.
231 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
233 static int drm_lock_transfer(drm_device_t *dev,
234 __volatile__ unsigned int *lock,
235 unsigned int context)
237 unsigned int old, new, prev;
239 dev->lock.filp = NULL;
240 do {
241 old = *lock;
242 new = context | _DRM_LOCK_HELD;
243 prev = cmpxchg(lock, old, new);
244 } while (prev != old);
245 return 1;
249 * Free lock.
251 * \param dev DRM device.
252 * \param lock lock.
253 * \param context context.
255 * Resets the lock file pointer.
256 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
257 * waiting on the lock queue.
259 int drm_lock_free(drm_device_t *dev,
260 __volatile__ unsigned int *lock, unsigned int context)
262 unsigned int old, new, prev;
264 dev->lock.filp = NULL;
265 do {
266 old = *lock;
267 new = 0;
268 prev = cmpxchg(lock, old, new);
269 } while (prev != old);
270 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
271 DRM_ERROR("%d freed heavyweight lock held by %d\n",
272 context,
273 _DRM_LOCKING_CONTEXT(old));
274 return 1;
276 wake_up_interruptible(&dev->lock.lock_queue);
277 return 0;
281 * If we get here, it means that the process has called DRM_IOCTL_LOCK
282 * without calling DRM_IOCTL_UNLOCK.
284 * If the lock is not held, then let the signal proceed as usual. If the lock
285 * is held, then set the contended flag and keep the signal blocked.
287 * \param priv pointer to a drm_sigdata structure.
288 * \return one if the signal should be delivered normally, or zero if the
289 * signal should be blocked.
291 static int drm_notifier(void *priv)
293 drm_sigdata_t *s = (drm_sigdata_t *)priv;
294 unsigned int old, new, prev;
297 /* Allow signal delivery if lock isn't held */
298 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
299 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) return 1;
301 /* Otherwise, set flag to force call to
302 drmUnlock */
303 do {
304 old = s->lock->lock;
305 new = old | _DRM_LOCK_CONT;
306 prev = cmpxchg(&s->lock->lock, old, new);
307 } while (prev != old);
308 return 0;