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.
39 int DRM(noop)(struct inode *inode, struct file *filp, unsigned int cmd,
47 * Take the heavyweight lock.
49 * \param lock lock pointer.
50 * \param context locking context.
51 * \return one if the lock is held, or zero otherwise.
53 * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
55 int DRM(lock_take)(__volatile__ unsigned int *lock, unsigned int context)
57 unsigned int old, new, prev;
61 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
62 else new = context | _DRM_LOCK_HELD;
63 prev = cmpxchg(lock, old, new);
64 } while (prev != old);
65 if (_DRM_LOCKING_CONTEXT(old) == context) {
66 if (old & _DRM_LOCK_HELD) {
67 if (context != DRM_KERNEL_CONTEXT) {
68 DRM_ERROR("%d holds heavyweight lock\n",
74 if (new == (context | _DRM_LOCK_HELD)) {
82 * This takes a lock forcibly and hands it to context. Should ONLY be used
83 * inside *_unlock to give lock to kernel before calling *_dma_schedule.
85 * \param dev DRM device.
86 * \param lock lock pointer.
87 * \param context locking context.
90 * Resets the lock file pointer.
91 * Marks the lock as held by the given context, via the \p cmpxchg instruction.
93 int DRM(lock_transfer)(drm_device_t *dev,
94 __volatile__ unsigned int *lock, unsigned int context)
96 unsigned int old, new, prev;
98 dev->lock.filp = NULL;
101 new = context | _DRM_LOCK_HELD;
102 prev = cmpxchg(lock, old, new);
103 } while (prev != old);
110 * \param dev DRM device.
112 * \param context context.
114 * Resets the lock file pointer.
115 * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
116 * waiting on the lock queue.
118 int DRM(lock_free)(drm_device_t *dev,
119 __volatile__ unsigned int *lock, unsigned int context)
121 unsigned int old, new, prev;
123 dev->lock.filp = NULL;
127 prev = cmpxchg(lock, old, new);
128 } while (prev != old);
129 if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
130 DRM_ERROR("%d freed heavyweight lock held by %d\n",
132 _DRM_LOCKING_CONTEXT(old));
135 wake_up_interruptible(&dev->lock.lock_queue);
140 * If we get here, it means that the process has called DRM_IOCTL_LOCK
141 * without calling DRM_IOCTL_UNLOCK.
143 * If the lock is not held, then let the signal proceed as usual. If the lock
144 * is held, then set the contended flag and keep the signal blocked.
146 * \param priv pointer to a drm_sigdata structure.
147 * \return one if the signal should be delivered normally, or zero if the
148 * signal should be blocked.
150 int DRM(notifier)(void *priv)
152 drm_sigdata_t *s = (drm_sigdata_t *)priv;
153 unsigned int old, new, prev;
156 /* Allow signal delivery if lock isn't held */
157 if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
158 || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) return 1;
160 /* Otherwise, set flag to force call to
164 new = old | _DRM_LOCK_CONT;
165 prev = cmpxchg(&s->lock->lock, old, new);
166 } while (prev != old);