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 <sys/cdefs.h>
37 #include <dev/drm2/drmP.h>
38
39 #if defined(__linux__)
40 static int drm_notifier(void *priv);
41 #endif
42
43 static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
44
45 /**
46 * Lock ioctl.
47 *
48 * \param inode device inode.
49 * \param file_priv DRM file private.
50 * \param cmd command.
51 * \param arg user argument, pointing to a drm_lock structure.
52 * \return zero on success or negative number on failure.
53 *
54 * Add the current task to the lock wait queue, and attempt to take to lock.
55 */
drm_lock(struct drm_device * dev,void * data,struct drm_file * file_priv)56 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
57 {
58 struct drm_lock *lock = data;
59 struct drm_master *master = file_priv->master;
60 int ret = 0;
61
62 ++file_priv->lock_count;
63
64 if (lock->context == DRM_KERNEL_CONTEXT) {
65 DRM_ERROR("Process %d using kernel context %d\n",
66 DRM_CURRENTPID, lock->context);
67 return -EINVAL;
68 }
69
70 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
71 lock->context, DRM_CURRENTPID,
72 master->lock.hw_lock->lock, lock->flags);
73
74 mtx_lock(&master->lock.spinlock);
75 master->lock.user_waiters++;
76 mtx_unlock(&master->lock.spinlock);
77
78 for (;;) {
79 #if defined(__linux__)
80 if (!master->lock.hw_lock) {
81 /* Device has been unregistered */
82 send_sig(SIGTERM, current, 0);
83 ret = -EINTR;
84 break;
85 }
86 #endif
87 if (drm_lock_take(&master->lock, lock->context)) {
88 master->lock.file_priv = file_priv;
89 master->lock.lock_time = jiffies;
90 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
91 break; /* Got lock */
92 }
93
94 /* Contention */
95 DRM_UNLOCK_ASSERT(dev);
96 ret = -sx_sleep(&master->lock.lock_queue, &drm_global_mutex,
97 PCATCH, "drmlk2", 0);
98 if (ret == -ERESTART)
99 ret = -ERESTARTSYS;
100 if (ret != 0)
101 break;
102 }
103 mtx_lock(&master->lock.spinlock);
104 master->lock.user_waiters--;
105 mtx_unlock(&master->lock.spinlock);
106
107 DRM_DEBUG("%d %s\n", lock->context,
108 ret ? "interrupted" : "has lock");
109 if (ret) return ret;
110
111 #if defined(__linux__)
112 /* don't set the block all signals on the master process for now
113 * really probably not the correct answer but lets us debug xkb
114 * xserver for now */
115 if (!file_priv->is_master) {
116 sigemptyset(&dev->sigmask);
117 sigaddset(&dev->sigmask, SIGSTOP);
118 sigaddset(&dev->sigmask, SIGTSTP);
119 sigaddset(&dev->sigmask, SIGTTIN);
120 sigaddset(&dev->sigmask, SIGTTOU);
121 dev->sigdata.context = lock->context;
122 dev->sigdata.lock = master->lock.hw_lock;
123 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
124 }
125 #endif
126
127 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
128 {
129 if (dev->driver->dma_quiescent(dev)) {
130 DRM_DEBUG("%d waiting for DMA quiescent\n",
131 lock->context);
132 return -EBUSY;
133 }
134 }
135
136 return 0;
137 }
138
139 /**
140 * Unlock ioctl.
141 *
142 * \param inode device inode.
143 * \param file_priv DRM file private.
144 * \param cmd command.
145 * \param arg user argument, pointing to a drm_lock structure.
146 * \return zero on success or negative number on failure.
147 *
148 * Transfer and free the lock.
149 */
drm_unlock(struct drm_device * dev,void * data,struct drm_file * file_priv)150 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
151 {
152 struct drm_lock *lock = data;
153 struct drm_master *master = file_priv->master;
154
155 if (lock->context == DRM_KERNEL_CONTEXT) {
156 DRM_ERROR("Process %d using kernel context %d\n",
157 DRM_CURRENTPID, lock->context);
158 return -EINVAL;
159 }
160
161 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
162
163 if (drm_lock_free(&master->lock, lock->context)) {
164 /* FIXME: Should really bail out here. */
165 }
166
167 #if defined(__linux__)
168 unblock_all_signals();
169 #endif
170 return 0;
171 }
172
173 /**
174 * Take the heavyweight lock.
175 *
176 * \param lock lock pointer.
177 * \param context locking context.
178 * \return one if the lock is held, or zero otherwise.
179 *
180 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
181 */
182 static
drm_lock_take(struct drm_lock_data * lock_data,unsigned int context)183 int drm_lock_take(struct drm_lock_data *lock_data,
184 unsigned int context)
185 {
186 unsigned int old, new, prev;
187 volatile unsigned int *lock = &lock_data->hw_lock->lock;
188
189 mtx_lock(&lock_data->spinlock);
190 do {
191 old = *lock;
192 if (old & _DRM_LOCK_HELD)
193 new = old | _DRM_LOCK_CONT;
194 else {
195 new = context | _DRM_LOCK_HELD |
196 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
197 _DRM_LOCK_CONT : 0);
198 }
199 prev = cmpxchg(lock, old, new);
200 } while (prev != old);
201 mtx_unlock(&lock_data->spinlock);
202
203 if (_DRM_LOCKING_CONTEXT(old) == context) {
204 if (old & _DRM_LOCK_HELD) {
205 if (context != DRM_KERNEL_CONTEXT) {
206 DRM_ERROR("%d holds heavyweight lock\n",
207 context);
208 }
209 return 0;
210 }
211 }
212
213 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
214 /* Have lock */
215 return 1;
216 }
217 return 0;
218 }
219
220 /**
221 * This takes a lock forcibly and hands it to context. Should ONLY be used
222 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
223 *
224 * \param dev DRM device.
225 * \param lock lock pointer.
226 * \param context locking context.
227 * \return always one.
228 *
229 * Resets the lock file pointer.
230 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
231 */
drm_lock_transfer(struct drm_lock_data * lock_data,unsigned int context)232 static int drm_lock_transfer(struct drm_lock_data *lock_data,
233 unsigned int context)
234 {
235 unsigned int old, new, prev;
236 volatile unsigned int *lock = &lock_data->hw_lock->lock;
237
238 lock_data->file_priv = NULL;
239 do {
240 old = *lock;
241 new = context | _DRM_LOCK_HELD;
242 prev = cmpxchg(lock, old, new);
243 } while (prev != old);
244 return 1;
245 }
246
247 /**
248 * Free lock.
249 *
250 * \param dev DRM device.
251 * \param lock lock.
252 * \param context context.
253 *
254 * Resets the lock file pointer.
255 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
256 * waiting on the lock queue.
257 */
drm_lock_free(struct drm_lock_data * lock_data,unsigned int context)258 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
259 {
260 unsigned int old, new, prev;
261 volatile unsigned int *lock = &lock_data->hw_lock->lock;
262
263 mtx_lock(&lock_data->spinlock);
264 if (lock_data->kernel_waiters != 0) {
265 drm_lock_transfer(lock_data, 0);
266 lock_data->idle_has_lock = 1;
267 mtx_unlock(&lock_data->spinlock);
268 return 1;
269 }
270 mtx_unlock(&lock_data->spinlock);
271
272 do {
273 old = *lock;
274 new = _DRM_LOCKING_CONTEXT(old);
275 prev = cmpxchg(lock, old, new);
276 } while (prev != old);
277
278 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
279 DRM_ERROR("%d freed heavyweight lock held by %d\n",
280 context, _DRM_LOCKING_CONTEXT(old));
281 return 1;
282 }
283 wake_up_interruptible(&lock_data->lock_queue);
284 return 0;
285 }
286
287 #if defined(__linux__)
288 /**
289 * If we get here, it means that the process has called DRM_IOCTL_LOCK
290 * without calling DRM_IOCTL_UNLOCK.
291 *
292 * If the lock is not held, then let the signal proceed as usual. If the lock
293 * is held, then set the contended flag and keep the signal blocked.
294 *
295 * \param priv pointer to a drm_sigdata structure.
296 * \return one if the signal should be delivered normally, or zero if the
297 * signal should be blocked.
298 */
drm_notifier(void * priv)299 static int drm_notifier(void *priv)
300 {
301 struct drm_sigdata *s = (struct drm_sigdata *) priv;
302 unsigned int old, new, prev;
303
304 /* Allow signal delivery if lock isn't held */
305 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
306 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
307 return 1;
308
309 /* Otherwise, set flag to force call to
310 drmUnlock */
311 do {
312 old = s->lock->lock;
313 new = old | _DRM_LOCK_CONT;
314 prev = cmpxchg(&s->lock->lock, old, new);
315 } while (prev != old);
316 return 0;
317 }
318 #endif
319
320 /**
321 * This function returns immediately and takes the hw lock
322 * with the kernel context if it is free, otherwise it gets the highest priority when and if
323 * it is eventually released.
324 *
325 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
326 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
327 * a deadlock, which is why the "idlelock" was invented).
328 *
329 * This should be sufficient to wait for GPU idle without
330 * having to worry about starvation.
331 */
332
drm_idlelock_take(struct drm_lock_data * lock_data)333 void drm_idlelock_take(struct drm_lock_data *lock_data)
334 {
335 int ret;
336
337 mtx_lock(&lock_data->spinlock);
338 lock_data->kernel_waiters++;
339 if (!lock_data->idle_has_lock) {
340
341 mtx_unlock(&lock_data->spinlock);
342 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
343 mtx_lock(&lock_data->spinlock);
344
345 if (ret == 1)
346 lock_data->idle_has_lock = 1;
347 }
348 mtx_unlock(&lock_data->spinlock);
349 }
350 EXPORT_SYMBOL(drm_idlelock_take);
351
drm_idlelock_release(struct drm_lock_data * lock_data)352 void drm_idlelock_release(struct drm_lock_data *lock_data)
353 {
354 unsigned int old, prev;
355 volatile unsigned int *lock = &lock_data->hw_lock->lock;
356
357 mtx_lock(&lock_data->spinlock);
358 if (--lock_data->kernel_waiters == 0) {
359 if (lock_data->idle_has_lock) {
360 do {
361 old = *lock;
362 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
363 } while (prev != old);
364 wake_up_interruptible(&lock_data->lock_queue);
365 lock_data->idle_has_lock = 0;
366 }
367 }
368 mtx_unlock(&lock_data->spinlock);
369 }
370 EXPORT_SYMBOL(drm_idlelock_release);
371
drm_i_have_hw_lock(struct drm_device * dev,struct drm_file * file_priv)372 int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
373 {
374 struct drm_master *master = file_priv->master;
375 return (file_priv->lock_count && master->lock.hw_lock &&
376 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
377 master->lock.file_priv == file_priv);
378 }
379