5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
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
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 <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <dev/drm2/drmP.h>
41 #if defined(__linux__)
42 static int drm_notifier(void *priv);
45 static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
50 * \param inode device inode.
51 * \param file_priv DRM file private.
53 * \param arg user argument, pointing to a drm_lock structure.
54 * \return zero on success or negative number on failure.
56 * Add the current task to the lock wait queue, and attempt to take to lock.
58 int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
60 struct drm_lock *lock = data;
61 struct drm_master *master = file_priv->master;
64 ++file_priv->lock_count;
66 if (lock->context == DRM_KERNEL_CONTEXT) {
67 DRM_ERROR("Process %d using kernel context %d\n",
68 DRM_CURRENTPID, lock->context);
72 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
73 lock->context, DRM_CURRENTPID,
74 master->lock.hw_lock->lock, lock->flags);
76 mtx_lock(&master->lock.spinlock);
77 master->lock.user_waiters++;
78 mtx_unlock(&master->lock.spinlock);
81 #if defined(__linux__)
82 if (!master->lock.hw_lock) {
83 /* Device has been unregistered */
84 send_sig(SIGTERM, current, 0);
89 if (drm_lock_take(&master->lock, lock->context)) {
90 master->lock.file_priv = file_priv;
91 master->lock.lock_time = jiffies;
92 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
97 DRM_UNLOCK_ASSERT(dev);
98 ret = -sx_sleep(&master->lock.lock_queue, &drm_global_mutex,
100 if (ret == -ERESTART)
105 mtx_lock(&master->lock.spinlock);
106 master->lock.user_waiters--;
107 mtx_unlock(&master->lock.spinlock);
109 DRM_DEBUG("%d %s\n", lock->context,
110 ret ? "interrupted" : "has lock");
113 #if defined(__linux__)
114 /* don't set the block all signals on the master process for now
115 * really probably not the correct answer but lets us debug xkb
117 if (!file_priv->is_master) {
118 sigemptyset(&dev->sigmask);
119 sigaddset(&dev->sigmask, SIGSTOP);
120 sigaddset(&dev->sigmask, SIGTSTP);
121 sigaddset(&dev->sigmask, SIGTTIN);
122 sigaddset(&dev->sigmask, SIGTTOU);
123 dev->sigdata.context = lock->context;
124 dev->sigdata.lock = master->lock.hw_lock;
125 block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
129 if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
131 if (dev->driver->dma_quiescent(dev)) {
132 DRM_DEBUG("%d waiting for DMA quiescent\n",
144 * \param inode device inode.
145 * \param file_priv DRM file private.
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.
150 * Transfer and free the lock.
152 int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
154 struct drm_lock *lock = data;
155 struct drm_master *master = file_priv->master;
157 if (lock->context == DRM_KERNEL_CONTEXT) {
158 DRM_ERROR("Process %d using kernel context %d\n",
159 DRM_CURRENTPID, lock->context);
163 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
165 if (drm_lock_free(&master->lock, lock->context)) {
166 /* FIXME: Should really bail out here. */
169 #if defined(__linux__)
170 unblock_all_signals();
176 * Take the heavyweight lock.
178 * \param lock lock pointer.
179 * \param context locking context.
180 * \return one if the lock is held, or zero otherwise.
182 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
185 int drm_lock_take(struct drm_lock_data *lock_data,
186 unsigned int context)
188 unsigned int old, new, prev;
189 volatile unsigned int *lock = &lock_data->hw_lock->lock;
191 mtx_lock(&lock_data->spinlock);
194 if (old & _DRM_LOCK_HELD)
195 new = old | _DRM_LOCK_CONT;
197 new = context | _DRM_LOCK_HELD |
198 ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
201 prev = cmpxchg(lock, old, new);
202 } while (prev != old);
203 mtx_unlock(&lock_data->spinlock);
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",
215 if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
223 * This takes a lock forcibly and hands it to context. Should ONLY be used
224 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
226 * \param dev DRM device.
227 * \param lock lock pointer.
228 * \param context locking context.
229 * \return always one.
231 * Resets the lock file pointer.
232 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
234 static int drm_lock_transfer(struct drm_lock_data *lock_data,
235 unsigned int context)
237 unsigned int old, new, prev;
238 volatile unsigned int *lock = &lock_data->hw_lock->lock;
240 lock_data->file_priv = NULL;
243 new = context | _DRM_LOCK_HELD;
244 prev = cmpxchg(lock, old, new);
245 } while (prev != old);
252 * \param dev DRM device.
254 * \param context context.
256 * Resets the lock file pointer.
257 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
258 * waiting on the lock queue.
260 int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
262 unsigned int old, new, prev;
263 volatile unsigned int *lock = &lock_data->hw_lock->lock;
265 mtx_lock(&lock_data->spinlock);
266 if (lock_data->kernel_waiters != 0) {
267 drm_lock_transfer(lock_data, 0);
268 lock_data->idle_has_lock = 1;
269 mtx_unlock(&lock_data->spinlock);
272 mtx_unlock(&lock_data->spinlock);
276 new = _DRM_LOCKING_CONTEXT(old);
277 prev = cmpxchg(lock, old, new);
278 } while (prev != old);
280 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
281 DRM_ERROR("%d freed heavyweight lock held by %d\n",
282 context, _DRM_LOCKING_CONTEXT(old));
285 wake_up_interruptible(&lock_data->lock_queue);
289 #if defined(__linux__)
291 * If we get here, it means that the process has called DRM_IOCTL_LOCK
292 * without calling DRM_IOCTL_UNLOCK.
294 * If the lock is not held, then let the signal proceed as usual. If the lock
295 * is held, then set the contended flag and keep the signal blocked.
297 * \param priv pointer to a drm_sigdata structure.
298 * \return one if the signal should be delivered normally, or zero if the
299 * signal should be blocked.
301 static int drm_notifier(void *priv)
303 struct drm_sigdata *s = (struct drm_sigdata *) priv;
304 unsigned int old, new, prev;
306 /* Allow signal delivery if lock isn't held */
307 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
308 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
311 /* Otherwise, set flag to force call to
315 new = old | _DRM_LOCK_CONT;
316 prev = cmpxchg(&s->lock->lock, old, new);
317 } while (prev != old);
323 * This function returns immediately and takes the hw lock
324 * with the kernel context if it is free, otherwise it gets the highest priority when and if
325 * it is eventually released.
327 * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
328 * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
329 * a deadlock, which is why the "idlelock" was invented).
331 * This should be sufficient to wait for GPU idle without
332 * having to worry about starvation.
335 void drm_idlelock_take(struct drm_lock_data *lock_data)
339 mtx_lock(&lock_data->spinlock);
340 lock_data->kernel_waiters++;
341 if (!lock_data->idle_has_lock) {
343 mtx_unlock(&lock_data->spinlock);
344 ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
345 mtx_lock(&lock_data->spinlock);
348 lock_data->idle_has_lock = 1;
350 mtx_unlock(&lock_data->spinlock);
352 EXPORT_SYMBOL(drm_idlelock_take);
354 void drm_idlelock_release(struct drm_lock_data *lock_data)
356 unsigned int old, prev;
357 volatile unsigned int *lock = &lock_data->hw_lock->lock;
359 mtx_lock(&lock_data->spinlock);
360 if (--lock_data->kernel_waiters == 0) {
361 if (lock_data->idle_has_lock) {
364 prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
365 } while (prev != old);
366 wake_up_interruptible(&lock_data->lock_queue);
367 lock_data->idle_has_lock = 0;
370 mtx_unlock(&lock_data->spinlock);
372 EXPORT_SYMBOL(drm_idlelock_release);
374 int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
376 struct drm_master *master = file_priv->master;
377 return (file_priv->lock_count && master->lock.hw_lock &&
378 _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
379 master->lock.file_priv == file_priv);