ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / drm / drm_lock.h
1 /**
2  * \file drm_lock.h 
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 "drmP.h"
37
38 /** No-op ioctl. */
39 int DRM(noop)(struct inode *inode, struct file *filp, unsigned int cmd,
40                unsigned long arg)
41 {
42         DRM_DEBUG("\n");
43         return 0;
44 }
45
46 /**
47  * Take the heavyweight lock.
48  *
49  * \param lock lock pointer.
50  * \param context locking context.
51  * \return one if the lock is held, or zero otherwise.
52  *
53  * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
54  */
55 int DRM(lock_take)(__volatile__ unsigned int *lock, unsigned int context)
56 {
57         unsigned int old, new, prev;
58
59         do {
60                 old = *lock;
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",
69                                           context);
70                         }
71                         return 0;
72                 }
73         }
74         if (new == (context | _DRM_LOCK_HELD)) {
75                                 /* Have lock */
76                 return 1;
77         }
78         return 0;
79 }
80
81 /**
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. 
84  * 
85  * \param dev DRM device.
86  * \param lock lock pointer.
87  * \param context locking context.
88  * \return always one.
89  *
90  * Resets the lock file pointer.
91  * Marks the lock as held by the given context, via the \p cmpxchg instruction.
92  */
93 int DRM(lock_transfer)(drm_device_t *dev,
94                        __volatile__ unsigned int *lock, unsigned int context)
95 {
96         unsigned int old, new, prev;
97
98         dev->lock.filp = 0;
99         do {
100                 old  = *lock;
101                 new  = context | _DRM_LOCK_HELD;
102                 prev = cmpxchg(lock, old, new);
103         } while (prev != old);
104         return 1;
105 }
106
107 /**
108  * Free lock.
109  * 
110  * \param dev DRM device.
111  * \param lock lock.
112  * \param context context.
113  * 
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.
117  */
118 int DRM(lock_free)(drm_device_t *dev,
119                    __volatile__ unsigned int *lock, unsigned int context)
120 {
121         unsigned int old, new, prev;
122
123         dev->lock.filp = 0;
124         do {
125                 old  = *lock;
126                 new  = 0;
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",
131                           context,
132                           _DRM_LOCKING_CONTEXT(old));
133                 return 1;
134         }
135         wake_up_interruptible(&dev->lock.lock_queue);
136         return 0;
137 }
138
139 /**
140  * If we get here, it means that the process has called DRM_IOCTL_LOCK
141  * without calling DRM_IOCTL_UNLOCK.
142  *
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.
145  *
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.
149  */
150 int DRM(notifier)(void *priv)
151 {
152         drm_sigdata_t *s = (drm_sigdata_t *)priv;
153         unsigned int  old, new, prev;
154
155
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;
159
160                                 /* Otherwise, set flag to force call to
161                                    drmUnlock */
162         do {
163                 old  = s->lock->lock;
164                 new  = old | _DRM_LOCK_CONT;
165                 prev = cmpxchg(&s->lock->lock, old, new);
166         } while (prev != old);
167         return 0;
168 }