ext2: trivial indentation fix.
[linux-2.6/verdex.git] / drivers / char / drm / drm_lock.c
blobb48a595d54eca0e9c13b0cb269b88fba6dc49f56
1 /**
2 * \file drm_lock.c
3 * IOCTLs for locking
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, lock.context)) {
91 dev->lock.filp = filp;
92 dev->lock.lock_time = jiffies;
93 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
94 break; /* Got lock */
97 /* Contention */
98 schedule();
99 if (signal_pending(current)) {
100 ret = -ERESTARTSYS;
101 break;
104 __set_current_state(TASK_RUNNING);
105 remove_wait_queue(&dev->lock.lock_queue, &entry);
107 DRM_DEBUG("%d %s\n", lock.context, ret ? "interrupted" : "has lock");
108 if (ret)
109 return ret;
111 sigemptyset(&dev->sigmask);
112 sigaddset(&dev->sigmask, SIGSTOP);
113 sigaddset(&dev->sigmask, SIGTSTP);
114 sigaddset(&dev->sigmask, SIGTTIN);
115 sigaddset(&dev->sigmask, SIGTTOU);
116 dev->sigdata.context = lock.context;
117 dev->sigdata.lock = dev->lock.hw_lock;
118 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
120 if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
121 dev->driver->dma_ready(dev);
123 if (dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT)) {
124 if (dev->driver->dma_quiescent(dev)) {
125 DRM_DEBUG("%d waiting for DMA quiescent\n", lock.context);
126 return DRM_ERR(EBUSY);
130 /* dev->driver->kernel_context_switch isn't used by any of the x86
131 * drivers but is used by the Sparc driver.
134 if (dev->driver->kernel_context_switch &&
135 dev->last_context != lock.context) {
136 dev->driver->kernel_context_switch(dev, dev->last_context,
137 lock.context);
139 return 0;
143 * Unlock ioctl.
145 * \param inode device inode.
146 * \param filp file pointer.
147 * \param cmd command.
148 * \param arg user argument, pointing to a drm_lock structure.
149 * \return zero on success or negative number on failure.
151 * Transfer and free the lock.
153 int drm_unlock(struct inode *inode, struct file *filp,
154 unsigned int cmd, unsigned long arg)
156 drm_file_t *priv = filp->private_data;
157 drm_device_t *dev = priv->head->dev;
158 drm_lock_t lock;
160 if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
161 return -EFAULT;
163 if (lock.context == DRM_KERNEL_CONTEXT) {
164 DRM_ERROR("Process %d using kernel context %d\n",
165 current->pid, lock.context);
166 return -EINVAL;
169 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
171 /* kernel_context_switch isn't used by any of the x86 drm
172 * modules but is required by the Sparc driver.
174 if (dev->driver->kernel_context_switch_unlock)
175 dev->driver->kernel_context_switch_unlock(dev, &lock);
176 else {
177 drm_lock_transfer(dev, &dev->lock.hw_lock->lock,
178 DRM_KERNEL_CONTEXT);
180 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
181 DRM_KERNEL_CONTEXT)) {
182 DRM_ERROR("\n");
186 unblock_all_signals();
187 return 0;
191 * Take the heavyweight lock.
193 * \param lock lock pointer.
194 * \param context locking context.
195 * \return one if the lock is held, or zero otherwise.
197 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
199 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
201 unsigned int old, new, prev;
203 do {
204 old = *lock;
205 if (old & _DRM_LOCK_HELD)
206 new = old | _DRM_LOCK_CONT;
207 else
208 new = context | _DRM_LOCK_HELD;
209 prev = cmpxchg(lock, old, new);
210 } while (prev != old);
211 if (_DRM_LOCKING_CONTEXT(old) == context) {
212 if (old & _DRM_LOCK_HELD) {
213 if (context != DRM_KERNEL_CONTEXT) {
214 DRM_ERROR("%d holds heavyweight lock\n",
215 context);
217 return 0;
220 if (new == (context | _DRM_LOCK_HELD)) {
221 /* Have lock */
222 return 1;
224 return 0;
228 * This takes a lock forcibly and hands it to context. Should ONLY be used
229 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
231 * \param dev DRM device.
232 * \param lock lock pointer.
233 * \param context locking context.
234 * \return always one.
236 * Resets the lock file pointer.
237 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
239 static int drm_lock_transfer(drm_device_t * dev,
240 __volatile__ unsigned int *lock,
241 unsigned int context)
243 unsigned int old, new, prev;
245 dev->lock.filp = NULL;
246 do {
247 old = *lock;
248 new = context | _DRM_LOCK_HELD;
249 prev = cmpxchg(lock, old, new);
250 } while (prev != old);
251 return 1;
255 * Free lock.
257 * \param dev DRM device.
258 * \param lock lock.
259 * \param context context.
261 * Resets the lock file pointer.
262 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
263 * waiting on the lock queue.
265 int drm_lock_free(drm_device_t * dev,
266 __volatile__ unsigned int *lock, unsigned int context)
268 unsigned int old, new, prev;
270 dev->lock.filp = NULL;
271 do {
272 old = *lock;
273 new = 0;
274 prev = cmpxchg(lock, old, new);
275 } while (prev != old);
276 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
277 DRM_ERROR("%d freed heavyweight lock held by %d\n",
278 context, _DRM_LOCKING_CONTEXT(old));
279 return 1;
281 wake_up_interruptible(&dev->lock.lock_queue);
282 return 0;
286 * If we get here, it means that the process has called DRM_IOCTL_LOCK
287 * without calling DRM_IOCTL_UNLOCK.
289 * If the lock is not held, then let the signal proceed as usual. If the lock
290 * is held, then set the contended flag and keep the signal blocked.
292 * \param priv pointer to a drm_sigdata structure.
293 * \return one if the signal should be delivered normally, or zero if the
294 * signal should be blocked.
296 static int drm_notifier(void *priv)
298 drm_sigdata_t *s = (drm_sigdata_t *) priv;
299 unsigned int old, new, prev;
301 /* Allow signal delivery if lock isn't held */
302 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
303 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
304 return 1;
306 /* Otherwise, set flag to force call to
307 drmUnlock */
308 do {
309 old = s->lock->lock;
310 new = old | _DRM_LOCK_CONT;
311 prev = cmpxchg(&s->lock->lock, old, new);
312 } while (prev != old);
313 return 0;