OSDN Git Service

fixes from kernel for 0 vs NULL - mika
[android-x86/external-libdrm.git] / linux-core / drm_lock.c
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 #define __NO_VERSION__
37 #include "drmP.h"
38
39 /** No-op ioctl. */
40 int DRM(noop)(struct inode *inode, struct file *filp, unsigned int cmd,
41                unsigned long arg)
42 {
43         DRM_DEBUG("\n");
44         return 0;
45 }
46
47 /**
48  * Take the heavyweight lock.
49  *
50  * \param lock lock pointer.
51  * \param context locking context.
52  * \return one if the lock is held, or zero otherwise.
53  *
54  * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
55  */
56 int DRM(lock_take)(__volatile__ unsigned int *lock, unsigned int context)
57 {
58         unsigned int old, new, prev;
59
60         do {
61                 old = *lock;
62                 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
63                 else                      new = context | _DRM_LOCK_HELD;
64                 prev = cmpxchg(lock, old, new);
65         } while (prev != old);
66         if (_DRM_LOCKING_CONTEXT(old) == context) {
67                 if (old & _DRM_LOCK_HELD) {
68                         if (context != DRM_KERNEL_CONTEXT) {
69                                 DRM_ERROR("%d holds heavyweight lock\n",
70                                           context);
71                         }
72                         return 0;
73                 }
74         }
75         if (new == (context | _DRM_LOCK_HELD)) {
76                                 /* Have lock */
77                 return 1;
78         }
79         return 0;
80 }
81
82 /**
83  * This takes a lock forcibly and hands it to context.  Should ONLY be used
84  * inside *_unlock to give lock to kernel before calling *_dma_schedule. 
85  * 
86  * \param dev DRM device.
87  * \param lock lock pointer.
88  * \param context locking context.
89  * \return always one.
90  *
91  * Resets the lock file pointer.
92  * Marks the lock as held by the given context, via the \p cmpxchg instruction.
93  */
94 int DRM(lock_transfer)(drm_device_t *dev,
95                        __volatile__ unsigned int *lock, unsigned int context)
96 {
97         unsigned int old, new, prev;
98
99         dev->lock.filp = NULL;
100         do {
101                 old  = *lock;
102                 new  = context | _DRM_LOCK_HELD;
103                 prev = cmpxchg(lock, old, new);
104         } while (prev != old);
105         return 1;
106 }
107
108 /**
109  * Free lock.
110  * 
111  * \param dev DRM device.
112  * \param lock lock.
113  * \param context context.
114  * 
115  * Resets the lock file pointer.
116  * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
117  * waiting on the lock queue.
118  */
119 int DRM(lock_free)(drm_device_t *dev,
120                    __volatile__ unsigned int *lock, unsigned int context)
121 {
122         unsigned int old, new, prev;
123
124         dev->lock.filp = NULL;
125         do {
126                 old  = *lock;
127                 new  = 0;
128                 prev = cmpxchg(lock, old, new);
129         } while (prev != old);
130         if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
131                 DRM_ERROR("%d freed heavyweight lock held by %d\n",
132                           context,
133                           _DRM_LOCKING_CONTEXT(old));
134                 return 1;
135         }
136         wake_up_interruptible(&dev->lock.lock_queue);
137         return 0;
138 }
139
140 /**
141  * If we get here, it means that the process has called DRM_IOCTL_LOCK
142  * without calling DRM_IOCTL_UNLOCK.
143  *
144  * If the lock is not held, then let the signal proceed as usual.  If the lock
145  * is held, then set the contended flag and keep the signal blocked.
146  *
147  * \param priv pointer to a drm_sigdata structure.
148  * \return one if the signal should be delivered normally, or zero if the
149  * signal should be blocked.
150  */
151 int DRM(notifier)(void *priv)
152 {
153         drm_sigdata_t *s = (drm_sigdata_t *)priv;
154         unsigned int  old, new, prev;
155
156
157                                 /* Allow signal delivery if lock isn't held */
158         if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
159             || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) return 1;
160
161                                 /* Otherwise, set flag to force call to
162                                    drmUnlock */
163         do {
164                 old  = s->lock->lock;
165                 new  = old | _DRM_LOCK_CONT;
166                 prev = cmpxchg(&s->lock->lock, old, new);
167         } while (prev != old);
168         return 0;
169 }