]>
Commit | Line | Data |
---|---|---|
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 | ||
b5e89ed5 | 38 | static int drm_lock_transfer(drm_device_t * dev, |
c94f7029 DA |
39 | __volatile__ unsigned int *lock, |
40 | unsigned int context); | |
41 | static int drm_notifier(void *priv); | |
42 | ||
b5e89ed5 | 43 | /** |
1da177e4 LT |
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 | */ | |
b5e89ed5 DA |
54 | int drm_lock(struct inode *inode, struct file *filp, |
55 | unsigned int cmd, unsigned long arg) | |
1da177e4 | 56 | { |
b5e89ed5 DA |
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; | |
1da177e4 LT |
62 | |
63 | ++priv->lock_count; | |
64 | ||
b5e89ed5 | 65 | if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock))) |
1da177e4 LT |
66 | return -EFAULT; |
67 | ||
b5e89ed5 DA |
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 | } | |
1da177e4 | 73 | |
b5e89ed5 DA |
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); | |
1da177e4 LT |
77 | |
78 | if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE)) | |
b5e89ed5 | 79 | if (lock.context < 0) |
1da177e4 LT |
80 | return -EINVAL; |
81 | ||
b5e89ed5 | 82 | add_wait_queue(&dev->lock.lock_queue, &entry); |
1da177e4 LT |
83 | for (;;) { |
84 | __set_current_state(TASK_INTERRUPTIBLE); | |
b5e89ed5 | 85 | if (!dev->lock.hw_lock) { |
1da177e4 LT |
86 | /* Device has been unregistered */ |
87 | ret = -EINTR; | |
88 | break; | |
89 | } | |
b5e89ed5 DA |
90 | if (drm_lock_take(&dev->lock.hw_lock->lock, lock.context)) { |
91 | dev->lock.filp = filp; | |
1da177e4 | 92 | dev->lock.lock_time = jiffies; |
b5e89ed5 DA |
93 | atomic_inc(&dev->counts[_DRM_STAT_LOCKS]); |
94 | break; /* Got lock */ | |
1da177e4 | 95 | } |
b5e89ed5 | 96 | |
1da177e4 LT |
97 | /* Contention */ |
98 | schedule(); | |
b5e89ed5 | 99 | if (signal_pending(current)) { |
1da177e4 LT |
100 | ret = -ERESTARTSYS; |
101 | break; | |
102 | } | |
103 | } | |
104 | __set_current_state(TASK_RUNNING); | |
b5e89ed5 | 105 | remove_wait_queue(&dev->lock.lock_queue, &entry); |
1da177e4 | 106 | |
b5e89ed5 DA |
107 | sigemptyset(&dev->sigmask); |
108 | sigaddset(&dev->sigmask, SIGSTOP); | |
109 | sigaddset(&dev->sigmask, SIGTSTP); | |
110 | sigaddset(&dev->sigmask, SIGTTIN); | |
111 | sigaddset(&dev->sigmask, SIGTTOU); | |
1da177e4 | 112 | dev->sigdata.context = lock.context; |
b5e89ed5 DA |
113 | dev->sigdata.lock = dev->lock.hw_lock; |
114 | block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask); | |
115 | ||
1da177e4 LT |
116 | if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY)) |
117 | dev->driver->dma_ready(dev); | |
b5e89ed5 DA |
118 | |
119 | if (dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT)) | |
1da177e4 | 120 | return dev->driver->dma_quiescent(dev); |
b5e89ed5 DA |
121 | |
122 | /* dev->driver->kernel_context_switch isn't used by any of the x86 | |
1da177e4 LT |
123 | * drivers but is used by the Sparc driver. |
124 | */ | |
b5e89ed5 DA |
125 | |
126 | if (dev->driver->kernel_context_switch && | |
1da177e4 | 127 | dev->last_context != lock.context) { |
b5e89ed5 DA |
128 | dev->driver->kernel_context_switch(dev, dev->last_context, |
129 | lock.context); | |
1da177e4 | 130 | } |
b5e89ed5 | 131 | DRM_DEBUG("%d %s\n", lock.context, ret ? "interrupted" : "has lock"); |
1da177e4 | 132 | |
b5e89ed5 | 133 | return ret; |
1da177e4 LT |
134 | } |
135 | ||
b5e89ed5 | 136 | /** |
1da177e4 LT |
137 | * Unlock ioctl. |
138 | * | |
139 | * \param inode device inode. | |
140 | * \param filp file pointer. | |
141 | * \param cmd command. | |
142 | * \param arg user argument, pointing to a drm_lock structure. | |
143 | * \return zero on success or negative number on failure. | |
144 | * | |
145 | * Transfer and free the lock. | |
146 | */ | |
b5e89ed5 DA |
147 | int drm_unlock(struct inode *inode, struct file *filp, |
148 | unsigned int cmd, unsigned long arg) | |
1da177e4 LT |
149 | { |
150 | drm_file_t *priv = filp->private_data; | |
151 | drm_device_t *dev = priv->head->dev; | |
152 | drm_lock_t lock; | |
153 | ||
b5e89ed5 | 154 | if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock))) |
1da177e4 LT |
155 | return -EFAULT; |
156 | ||
b5e89ed5 DA |
157 | if (lock.context == DRM_KERNEL_CONTEXT) { |
158 | DRM_ERROR("Process %d using kernel context %d\n", | |
159 | current->pid, lock.context); | |
1da177e4 LT |
160 | return -EINVAL; |
161 | } | |
162 | ||
b5e89ed5 | 163 | atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]); |
1da177e4 LT |
164 | |
165 | /* kernel_context_switch isn't used by any of the x86 drm | |
166 | * modules but is required by the Sparc driver. | |
167 | */ | |
168 | if (dev->driver->kernel_context_switch_unlock) | |
169 | dev->driver->kernel_context_switch_unlock(dev, &lock); | |
170 | else { | |
b5e89ed5 DA |
171 | drm_lock_transfer(dev, &dev->lock.hw_lock->lock, |
172 | DRM_KERNEL_CONTEXT); | |
173 | ||
174 | if (drm_lock_free(dev, &dev->lock.hw_lock->lock, | |
175 | DRM_KERNEL_CONTEXT)) { | |
176 | DRM_ERROR("\n"); | |
1da177e4 LT |
177 | } |
178 | } | |
179 | ||
180 | unblock_all_signals(); | |
181 | return 0; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Take the heavyweight lock. | |
186 | * | |
187 | * \param lock lock pointer. | |
188 | * \param context locking context. | |
189 | * \return one if the lock is held, or zero otherwise. | |
190 | * | |
191 | * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction. | |
192 | */ | |
193 | int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context) | |
194 | { | |
195 | unsigned int old, new, prev; | |
196 | ||
197 | do { | |
198 | old = *lock; | |
b5e89ed5 DA |
199 | if (old & _DRM_LOCK_HELD) |
200 | new = old | _DRM_LOCK_CONT; | |
201 | else | |
202 | new = context | _DRM_LOCK_HELD; | |
1da177e4 LT |
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); | |
210 | } | |
211 | return 0; | |
212 | } | |
213 | } | |
214 | if (new == (context | _DRM_LOCK_HELD)) { | |
b5e89ed5 | 215 | /* Have lock */ |
1da177e4 LT |
216 | return 1; |
217 | } | |
218 | return 0; | |
219 | } | |
220 | ||
221 | /** | |
222 | * This takes a lock forcibly and hands it to context. Should ONLY be used | |
b5e89ed5 DA |
223 | * inside *_unlock to give lock to kernel before calling *_dma_schedule. |
224 | * | |
1da177e4 LT |
225 | * \param dev DRM device. |
226 | * \param lock lock pointer. | |
227 | * \param context locking context. | |
228 | * \return always one. | |
229 | * | |
230 | * Resets the lock file pointer. | |
231 | * Marks the lock as held by the given context, via the \p cmpxchg instruction. | |
232 | */ | |
b5e89ed5 | 233 | static int drm_lock_transfer(drm_device_t * dev, |
c94f7029 DA |
234 | __volatile__ unsigned int *lock, |
235 | unsigned int context) | |
1da177e4 LT |
236 | { |
237 | unsigned int old, new, prev; | |
238 | ||
239 | dev->lock.filp = NULL; | |
240 | do { | |
b5e89ed5 DA |
241 | old = *lock; |
242 | new = context | _DRM_LOCK_HELD; | |
1da177e4 LT |
243 | prev = cmpxchg(lock, old, new); |
244 | } while (prev != old); | |
245 | return 1; | |
246 | } | |
247 | ||
248 | /** | |
249 | * Free lock. | |
b5e89ed5 | 250 | * |
1da177e4 LT |
251 | * \param dev DRM device. |
252 | * \param lock lock. | |
253 | * \param context context. | |
b5e89ed5 | 254 | * |
1da177e4 LT |
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. | |
258 | */ | |
b5e89ed5 DA |
259 | int drm_lock_free(drm_device_t * dev, |
260 | __volatile__ unsigned int *lock, unsigned int context) | |
1da177e4 LT |
261 | { |
262 | unsigned int old, new, prev; | |
263 | ||
264 | dev->lock.filp = NULL; | |
265 | do { | |
b5e89ed5 DA |
266 | old = *lock; |
267 | new = 0; | |
1da177e4 LT |
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", | |
b5e89ed5 | 272 | context, _DRM_LOCKING_CONTEXT(old)); |
1da177e4 LT |
273 | return 1; |
274 | } | |
275 | wake_up_interruptible(&dev->lock.lock_queue); | |
276 | return 0; | |
277 | } | |
278 | ||
279 | /** | |
280 | * If we get here, it means that the process has called DRM_IOCTL_LOCK | |
281 | * without calling DRM_IOCTL_UNLOCK. | |
282 | * | |
283 | * If the lock is not held, then let the signal proceed as usual. If the lock | |
284 | * is held, then set the contended flag and keep the signal blocked. | |
285 | * | |
286 | * \param priv pointer to a drm_sigdata structure. | |
287 | * \return one if the signal should be delivered normally, or zero if the | |
288 | * signal should be blocked. | |
289 | */ | |
c94f7029 | 290 | static int drm_notifier(void *priv) |
1da177e4 | 291 | { |
b5e89ed5 DA |
292 | drm_sigdata_t *s = (drm_sigdata_t *) priv; |
293 | unsigned int old, new, prev; | |
1da177e4 | 294 | |
b5e89ed5 | 295 | /* Allow signal delivery if lock isn't held */ |
1da177e4 | 296 | if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock) |
b5e89ed5 DA |
297 | || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) |
298 | return 1; | |
1da177e4 | 299 | |
b5e89ed5 DA |
300 | /* Otherwise, set flag to force call to |
301 | drmUnlock */ | |
1da177e4 | 302 | do { |
b5e89ed5 DA |
303 | old = s->lock->lock; |
304 | new = old | _DRM_LOCK_CONT; | |
1da177e4 LT |
305 | prev = cmpxchg(&s->lock->lock, old, new); |
306 | } while (prev != old); | |
307 | return 0; | |
308 | } |