2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
29 * $DragonFly: src/sys/dev/drm/drm_lock.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
33 * Implementation of the ioctls and other support code for dealing with the
36 * The DRM hardware lock is a shared structure between the kernel and userland.
38 * On uncontended access where the new context was the last context, the
39 * client may take the lock without dropping down into the kernel, using atomic
42 * If the client finds during compare-and-set that it was not the last owner
43 * of the lock, it calls the DRM lock ioctl, which may sleep waiting for the
44 * lock, and may have side-effects of kernel-managed context switching.
46 * When the client releases the lock, if the lock is marked as being contended
47 * by another client, then the DRM unlock ioctl is called so that the
48 * contending client may be woken up.
53 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
55 unsigned int old, new;
59 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
60 else new = context | _DRM_LOCK_HELD;
61 } while (!atomic_cmpset_int(lock, old, new));
63 if (_DRM_LOCKING_CONTEXT(old) == context) {
64 if (old & _DRM_LOCK_HELD) {
65 if (context != DRM_KERNEL_CONTEXT) {
66 DRM_ERROR("%d holds heavyweight lock\n",
72 if (new == (context | _DRM_LOCK_HELD)) {
79 /* This takes a lock forcibly and hands it to context. Should ONLY be used
80 inside *_unlock to give lock to kernel before calling *_dma_schedule. */
81 int drm_lock_transfer(drm_device_t *dev,
82 __volatile__ unsigned int *lock, unsigned int context)
84 unsigned int old, new;
86 dev->lock.file_priv = NULL;
89 new = context | _DRM_LOCK_HELD;
90 } while (!atomic_cmpset_int(lock, old, new));
95 int drm_lock_free(drm_device_t *dev,
96 __volatile__ unsigned int *lock, unsigned int context)
98 unsigned int old, new;
100 dev->lock.file_priv = NULL;
104 } while (!atomic_cmpset_int(lock, old, new));
106 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
107 DRM_ERROR("%d freed heavyweight lock held by %d\n",
108 context, _DRM_LOCKING_CONTEXT(old));
111 DRM_WAKEUP_INT((void *)&dev->lock.lock_queue);
115 int drm_lock(drm_device_t *dev, void *data, struct drm_file *file_priv)
117 drm_lock_t *lock = data;
120 if (lock->context == DRM_KERNEL_CONTEXT) {
121 DRM_ERROR("Process %d using kernel context %d\n",
122 DRM_CURRENTPID, lock->context);
126 DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
127 lock->context, DRM_CURRENTPID, dev->lock.hw_lock->lock,
130 if (dev->driver.use_dma_queue && lock->context < 0)
135 if (drm_lock_take(&dev->lock.hw_lock->lock, lock->context)) {
136 dev->lock.file_priv = file_priv;
137 dev->lock.lock_time = jiffies;
138 atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
139 break; /* Got lock */
143 #if defined(__FreeBSD__) && __FreeBSD_version > 500000
144 ret = mtx_sleep((void *)&dev->lock.lock_queue, &dev->dev_lock,
145 PZERO | PCATCH, "drmlk2", 0);
146 #elif defined(__DragonFly__)
148 tsleep_interlock((void *)&dev->lock.lock_queue);
150 ret = tsleep((void *)&dev->lock.lock_queue, PCATCH,
155 ret = tsleep((void *)&dev->lock.lock_queue, PZERO | PCATCH,
162 DRM_DEBUG("%d %s\n", lock->context, ret ? "interrupted" : "has lock");
167 /* XXX: Add signal blocking here */
169 if (dev->driver.dma_quiescent != NULL &&
170 (lock->flags & _DRM_LOCK_QUIESCENT))
171 dev->driver.dma_quiescent(dev);
176 int drm_unlock(drm_device_t *dev, void *data, struct drm_file *file_priv)
178 drm_lock_t *lock = data;
180 if (lock->context == DRM_KERNEL_CONTEXT) {
181 DRM_ERROR("Process %d using kernel context %d\n",
182 DRM_CURRENTPID, lock->context);
185 /* Check that the context unlock being requested actually matches
186 * who currently holds the lock.
188 if (!_DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) ||
189 _DRM_LOCKING_CONTEXT(dev->lock.hw_lock->lock) != lock->context)
192 atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
195 drm_lock_transfer(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT);
197 if (drm_lock_free(dev, &dev->lock.hw_lock->lock, DRM_KERNEL_CONTEXT)) {