blob: f970dc36c18f84011dfe97fba6dfadb71448e713 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002 * \file drm_lock.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * IOCTLs for locking
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * \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
Dave Airlieb5e89ed2005-09-25 14:28:13 +100038static int drm_lock_transfer(drm_device_t * dev,
Dave Airliec94f7022005-07-07 21:03:38 +100039 __volatile__ unsigned int *lock,
40 unsigned int context);
41static int drm_notifier(void *priv);
42
Dave Airlieb5e89ed2005-09-25 14:28:13 +100043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * 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 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100054int drm_lock(struct inode *inode, struct file *filp,
55 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 ++priv->lock_count;
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return -EFAULT;
67
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Dave Airlieb5e89ed2005-09-25 14:28:13 +100074 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
Dave Airlieb5e89ed2005-09-25 14:28:13 +100079 if (lock.context < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return -EINVAL;
81
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 add_wait_queue(&dev->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 for (;;) {
84 __set_current_state(TASK_INTERRUPTIBLE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 if (!dev->lock.hw_lock) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 /* Device has been unregistered */
87 ret = -EINTR;
88 break;
89 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100090 if (drm_lock_take(&dev->lock.hw_lock->lock, lock.context)) {
91 dev->lock.filp = filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 dev->lock.lock_time = jiffies;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
94 break; /* Got lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* Contention */
98 schedule();
Dave Airlieb5e89ed2005-09-25 14:28:13 +100099 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 ret = -ERESTARTSYS;
101 break;
102 }
103 }
104 __set_current_state(TASK_RUNNING);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000105 remove_wait_queue(&dev->lock.lock_queue, &entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Dave Airlied985c102006-01-02 21:32:48 +1100107 DRM_DEBUG( "%d %s\n", lock.context, ret ? "interrupted" : "has lock" );
108 if (ret) return ret;
109
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 sigemptyset(&dev->sigmask);
111 sigaddset(&dev->sigmask, SIGSTOP);
112 sigaddset(&dev->sigmask, SIGTSTP);
113 sigaddset(&dev->sigmask, SIGTTIN);
114 sigaddset(&dev->sigmask, SIGTTOU);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 dev->sigdata.context = lock.context;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000116 dev->sigdata.lock = dev->lock.hw_lock;
117 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
120 dev->driver->dma_ready(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121
Dave Airlied985c102006-01-02 21:32:48 +1100122 if (dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT)) {
123 if (dev->driver->dma_quiescent(dev)) {
124 DRM_DEBUG( "%d waiting for DMA quiescent\n", lock.context);
125 return DRM_ERR(EBUSY);
126 }
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 if (dev->driver->kernel_context_switch &&
130 dev->last_context != lock.context) {
131 dev->driver->kernel_context_switch(dev, dev->last_context,
132 lock.context);
133 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134
Dave Airlied985c102006-01-02 21:32:48 +1100135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Unlock ioctl.
140 *
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.
146 *
147 * Transfer and free the lock.
148 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149int drm_unlock(struct inode *inode, struct file *filp,
150 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 drm_file_t *priv = filp->private_data;
153 drm_device_t *dev = priv->head->dev;
154 drm_lock_t lock;
155
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EFAULT;
158
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 if (lock.context == DRM_KERNEL_CONTEXT) {
160 DRM_ERROR("Process %d using kernel context %d\n",
161 current->pid, lock.context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return -EINVAL;
163 }
164
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* kernel_context_switch isn't used by any of the x86 drm
168 * modules but is required by the Sparc driver.
169 */
170 if (dev->driver->kernel_context_switch_unlock)
171 dev->driver->kernel_context_switch_unlock(dev, &lock);
172 else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 drm_lock_transfer(dev, &dev->lock.hw_lock->lock,
174 DRM_KERNEL_CONTEXT);
175
176 if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
177 DRM_KERNEL_CONTEXT)) {
178 DRM_ERROR("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 }
181
182 unblock_all_signals();
183 return 0;
184}
185
186/**
187 * Take the heavyweight lock.
188 *
189 * \param lock lock pointer.
190 * \param context locking context.
191 * \return one if the lock is held, or zero otherwise.
192 *
193 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
194 */
195int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
196{
197 unsigned int old, new, prev;
198
199 do {
200 old = *lock;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201 if (old & _DRM_LOCK_HELD)
202 new = old | _DRM_LOCK_CONT;
203 else
204 new = context | _DRM_LOCK_HELD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 prev = cmpxchg(lock, old, new);
206 } while (prev != old);
207 if (_DRM_LOCKING_CONTEXT(old) == context) {
208 if (old & _DRM_LOCK_HELD) {
209 if (context != DRM_KERNEL_CONTEXT) {
210 DRM_ERROR("%d holds heavyweight lock\n",
211 context);
212 }
213 return 0;
214 }
215 }
216 if (new == (context | _DRM_LOCK_HELD)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000217 /* Have lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 1;
219 }
220 return 0;
221}
222
223/**
224 * This takes a lock forcibly and hands it to context. Should ONLY be used
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
226 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * \param dev DRM device.
228 * \param lock lock pointer.
229 * \param context locking context.
230 * \return always one.
231 *
232 * Resets the lock file pointer.
233 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
234 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235static int drm_lock_transfer(drm_device_t * dev,
Dave Airliec94f7022005-07-07 21:03:38 +1000236 __volatile__ unsigned int *lock,
237 unsigned int context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 unsigned int old, new, prev;
240
241 dev->lock.filp = NULL;
242 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 old = *lock;
244 new = context | _DRM_LOCK_HELD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 prev = cmpxchg(lock, old, new);
246 } while (prev != old);
247 return 1;
248}
249
250/**
251 * Free lock.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * \param dev DRM device.
254 * \param lock lock.
255 * \param context context.
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * Resets the lock file pointer.
258 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
259 * waiting on the lock queue.
260 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261int drm_lock_free(drm_device_t * dev,
262 __volatile__ unsigned int *lock, unsigned int context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 unsigned int old, new, prev;
265
266 dev->lock.filp = NULL;
267 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000268 old = *lock;
269 new = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 prev = cmpxchg(lock, old, new);
271 } while (prev != old);
272 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
273 DRM_ERROR("%d freed heavyweight lock held by %d\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 context, _DRM_LOCKING_CONTEXT(old));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return 1;
276 }
277 wake_up_interruptible(&dev->lock.lock_queue);
278 return 0;
279}
280
281/**
282 * If we get here, it means that the process has called DRM_IOCTL_LOCK
283 * without calling DRM_IOCTL_UNLOCK.
284 *
285 * If the lock is not held, then let the signal proceed as usual. If the lock
286 * is held, then set the contended flag and keep the signal blocked.
287 *
288 * \param priv pointer to a drm_sigdata structure.
289 * \return one if the signal should be delivered normally, or zero if the
290 * signal should be blocked.
291 */
Dave Airliec94f7022005-07-07 21:03:38 +1000292static int drm_notifier(void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 drm_sigdata_t *s = (drm_sigdata_t *) priv;
295 unsigned int old, new, prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 /* Allow signal delivery if lock isn't held */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
300 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 /* Otherwise, set flag to force call to
303 drmUnlock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 do {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000305 old = s->lock->lock;
306 new = old | _DRM_LOCK_CONT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 prev = cmpxchg(&s->lock->lock, old, new);
308 } while (prev != old);
309 return 0;
310}