Merge branch 'upstream'
[linux-2.6] / drivers / char / drm / drm_lock.c
1 /**
2  * \file drm_lock.c
3  * IOCTLs for locking
4  *
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
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);
42
43 /**
44  * Lock ioctl.
45  *
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.
51  *
52  * Add the current task to the lock wait queue, and attempt to take to lock.
53  */
54 int drm_lock(struct inode *inode, struct file *filp,
55              unsigned int cmd, unsigned long arg)
56 {
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;
62
63         ++priv->lock_count;
64
65         if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
66                 return -EFAULT;
67
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;
72         }
73
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);
77
78         if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
79                 if (lock.context < 0)
80                         return -EINVAL;
81
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;
89                 }
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 */
95                 }
96
97                 /* Contention */
98                 schedule();
99                 if (signal_pending(current)) {
100                         ret = -ERESTARTSYS;
101                         break;
102                 }
103         }
104         __set_current_state(TASK_RUNNING);
105         remove_wait_queue(&dev->lock.lock_queue, &entry);
106
107         DRM_DEBUG("%d %s\n", lock.context, ret ? "interrupted" : "has lock");
108         if (ret)
109                 return ret;
110
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);
119
120         if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
121                 dev->driver->dma_ready(dev);
122
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);
127                 }
128         }
129
130         /* dev->driver->kernel_context_switch isn't used by any of the x86
131          *  drivers but is used by the Sparc driver.
132          */
133         if (dev->driver->kernel_context_switch &&
134             dev->last_context != lock.context) {
135                 dev->driver->kernel_context_switch(dev, dev->last_context,
136                                                    lock.context);
137         }
138         return 0;
139 }
140
141 /**
142  * Unlock ioctl.
143  *
144  * \param inode device inode.
145  * \param filp file pointer.
146  * \param cmd command.
147  * \param arg user argument, pointing to a drm_lock structure.
148  * \return zero on success or negative number on failure.
149  *
150  * Transfer and free the lock.
151  */
152 int drm_unlock(struct inode *inode, struct file *filp,
153                unsigned int cmd, unsigned long arg)
154 {
155         drm_file_t *priv = filp->private_data;
156         drm_device_t *dev = priv->head->dev;
157         drm_lock_t lock;
158
159         if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
160                 return -EFAULT;
161
162         if (lock.context == DRM_KERNEL_CONTEXT) {
163                 DRM_ERROR("Process %d using kernel context %d\n",
164                           current->pid, lock.context);
165                 return -EINVAL;
166         }
167
168         atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
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)
174                 dev->driver->kernel_context_switch_unlock(dev, &lock);
175         else {
176                 drm_lock_transfer(dev, &dev->lock.hw_lock->lock,
177                                   DRM_KERNEL_CONTEXT);
178
179                 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
180                                   DRM_KERNEL_CONTEXT)) {
181                         DRM_ERROR("\n");
182                 }
183         }
184
185         unblock_all_signals();
186         return 0;
187 }
188
189 /**
190  * Take the heavyweight lock.
191  *
192  * \param lock lock pointer.
193  * \param context locking context.
194  * \return one if the lock is held, or zero otherwise.
195  *
196  * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
197  */
198 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
199 {
200         unsigned int old, new, prev;
201
202         do {
203                 old = *lock;
204                 if (old & _DRM_LOCK_HELD)
205                         new = old | _DRM_LOCK_CONT;
206                 else
207                         new = context | _DRM_LOCK_HELD;
208                 prev = cmpxchg(lock, old, new);
209         } while (prev != old);
210         if (_DRM_LOCKING_CONTEXT(old) == context) {
211                 if (old & _DRM_LOCK_HELD) {
212                         if (context != DRM_KERNEL_CONTEXT) {
213                                 DRM_ERROR("%d holds heavyweight lock\n",
214                                           context);
215                         }
216                         return 0;
217                 }
218         }
219         if (new == (context | _DRM_LOCK_HELD)) {
220                 /* Have lock */
221                 return 1;
222         }
223         return 0;
224 }
225
226 /**
227  * This takes a lock forcibly and hands it to context.  Should ONLY be used
228  * inside *_unlock to give lock to kernel before calling *_dma_schedule.
229  *
230  * \param dev DRM device.
231  * \param lock lock pointer.
232  * \param context locking context.
233  * \return always one.
234  *
235  * Resets the lock file pointer.
236  * Marks the lock as held by the given context, via the \p cmpxchg instruction.
237  */
238 static int drm_lock_transfer(drm_device_t * dev,
239                              __volatile__ unsigned int *lock,
240                              unsigned int context)
241 {
242         unsigned int old, new, prev;
243
244         dev->lock.filp = NULL;
245         do {
246                 old = *lock;
247                 new = context | _DRM_LOCK_HELD;
248                 prev = cmpxchg(lock, old, new);
249         } while (prev != old);
250         return 1;
251 }
252
253 /**
254  * Free lock.
255  *
256  * \param dev DRM device.
257  * \param lock lock.
258  * \param context context.
259  *
260  * Resets the lock file pointer.
261  * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
262  * waiting on the lock queue.
263  */
264 int drm_lock_free(drm_device_t * dev,
265                   __volatile__ unsigned int *lock, unsigned int context)
266 {
267         unsigned int old, new, prev;
268
269         dev->lock.filp = NULL;
270         do {
271                 old = *lock;
272                 new = 0;
273                 prev = cmpxchg(lock, old, new);
274         } while (prev != old);
275         if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
276                 DRM_ERROR("%d freed heavyweight lock held by %d\n",
277                           context, _DRM_LOCKING_CONTEXT(old));
278                 return 1;
279         }
280         wake_up_interruptible(&dev->lock.lock_queue);
281         return 0;
282 }
283
284 /**
285  * If we get here, it means that the process has called DRM_IOCTL_LOCK
286  * without calling DRM_IOCTL_UNLOCK.
287  *
288  * If the lock is not held, then let the signal proceed as usual.  If the lock
289  * is held, then set the contended flag and keep the signal blocked.
290  *
291  * \param priv pointer to a drm_sigdata structure.
292  * \return one if the signal should be delivered normally, or zero if the
293  * signal should be blocked.
294  */
295 static int drm_notifier(void *priv)
296 {
297         drm_sigdata_t *s = (drm_sigdata_t *) priv;
298         unsigned int old, new, prev;
299
300         /* Allow signal delivery if lock isn't held */
301         if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
302             || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
303                 return 1;
304
305         /* Otherwise, set flag to force call to
306            drmUnlock */
307         do {
308                 old = s->lock->lock;
309                 new = old | _DRM_LOCK_CONT;
310                 prev = cmpxchg(&s->lock->lock, old, new);
311         } while (prev != old);
312         return 0;
313 }