Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 /** 
 39  * Lock ioctl.
 40  *
 41  * \param inode device inode.
 42  * \param filp file pointer.
 43  * \param cmd command.
 44  * \param arg user argument, pointing to a drm_lock structure.
 45  * \return zero on success or negative number on failure.
 46  *
 47  * Add the current task to the lock wait queue, and attempt to take to lock.
 48  */
 49 int drm_lock( struct inode *inode, struct file *filp,
 50                unsigned int cmd, unsigned long arg )
 51 {
 52         drm_file_t *priv = filp->private_data;
 53         drm_device_t *dev = priv->dev;
 54         DECLARE_WAITQUEUE( entry, current );
 55         drm_lock_t lock;
 56         int ret = 0;
 57 
 58         ++priv->lock_count;
 59 
 60         if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
 61                 return -EFAULT;
 62 
 63         if ( lock.context == DRM_KERNEL_CONTEXT ) {
 64                 DRM_ERROR( "Process %d using kernel context %d\n",
 65                            current->pid, lock.context );
 66                 return -EINVAL;
 67         }
 68 
 69         DRM_DEBUG( "%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
 70                    lock.context, current->pid,
 71                    dev->lock.hw_lock->lock, lock.flags );
 72 
 73         if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
 74                 if ( lock.context < 0 )
 75                         return -EINVAL;
 76 
 77         add_wait_queue( &dev->lock.lock_queue, &entry );
 78         for (;;) {
 79                 __set_current_state(TASK_INTERRUPTIBLE);
 80                 if ( !dev->lock.hw_lock ) {
 81                         /* Device has been unregistered */
 82                         ret = -EINTR;
 83                         break;
 84                 }
 85                 if ( drm_lock_take( &dev->lock.hw_lock->lock,
 86                                      lock.context ) ) {
 87                         dev->lock.filp      = filp;
 88                         dev->lock.lock_time = jiffies;
 89                         atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
 90                         break;  /* Got lock */
 91                 }
 92                 
 93                 /* Contention */
 94                 schedule();
 95                 if ( signal_pending( current ) ) {
 96                         ret = -ERESTARTSYS;
 97                         break;
 98                 }
 99         }
100         __set_current_state(TASK_RUNNING);
101         remove_wait_queue( &dev->lock.lock_queue, &entry );
102 
103         sigemptyset( &dev->sigmask );
104         sigaddset( &dev->sigmask, SIGSTOP );
105         sigaddset( &dev->sigmask, SIGTSTP );
106         sigaddset( &dev->sigmask, SIGTTIN );
107         sigaddset( &dev->sigmask, SIGTTOU );
108         dev->sigdata.context = lock.context;
109         dev->sigdata.lock    = dev->lock.hw_lock;
110         block_all_signals( drm_notifier,
111                            &dev->sigdata, &dev->sigmask );
112         
113         if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
114                 dev->driver->dma_ready(dev);
115         
116         if ( dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT ))
117                 return dev->driver->dma_quiescent(dev);
118         
119         /* dev->driver->kernel_context_switch isn't used by any of the x86 
120          *  drivers but is used by the Sparc driver.
121          */
122         
123         if (dev->driver->kernel_context_switch && 
124             dev->last_context != lock.context) {
125           dev->driver->kernel_context_switch(dev, dev->last_context, 
126                                             lock.context);
127         }
128         DRM_DEBUG( "%d %s\n", lock.context, ret ? "interrupted" : "has lock" );
129 
130         return ret;
131 }
132 
133 /** 
134  * Unlock ioctl.
135  *
136  * \param inode device inode.
137  * \param filp file pointer.
138  * \param cmd command.
139  * \param arg user argument, pointing to a drm_lock structure.
140  * \return zero on success or negative number on failure.
141  *
142  * Transfer and free the lock.
143  */
144 int drm_unlock( struct inode *inode, struct file *filp,
145                  unsigned int cmd, unsigned long arg )
146 {
147         drm_file_t *priv = filp->private_data;
148         drm_device_t *dev = priv->dev;
149         drm_lock_t lock;
150 
151         if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
152                 return -EFAULT;
153 
154         if ( lock.context == DRM_KERNEL_CONTEXT ) {
155                 DRM_ERROR( "Process %d using kernel context %d\n",
156                            current->pid, lock.context );
157                 return -EINVAL;
158         }
159 
160         atomic_inc( &dev->counts[_DRM_STAT_UNLOCKS] );
161 
162         /* kernel_context_switch isn't used by any of the x86 drm
163          * modules but is required by the Sparc driver.
164          */
165         if (dev->driver->kernel_context_switch_unlock)
166                 dev->driver->kernel_context_switch_unlock(dev, &lock);
167         else {
168                 drm_lock_transfer( dev, &dev->lock.hw_lock->lock, 
169                                     DRM_KERNEL_CONTEXT );
170                 
171                 if ( drm_lock_free( dev, &dev->lock.hw_lock->lock,
172                                      DRM_KERNEL_CONTEXT ) ) {
173                         DRM_ERROR( "\n" );
174                 }
175         }
176 
177         unblock_all_signals();
178         return 0;
179 }
180 
181 /**
182  * Take the heavyweight lock.
183  *
184  * \param lock lock pointer.
185  * \param context locking context.
186  * \return one if the lock is held, or zero otherwise.
187  *
188  * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
189  */
190 int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
191 {
192         unsigned int old, new, prev;
193 
194         do {
195                 old = *lock;
196                 if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
197                 else                      new = context | _DRM_LOCK_HELD;
198                 prev = cmpxchg(lock, old, new);
199         } while (prev != old);
200         if (_DRM_LOCKING_CONTEXT(old) == context) {
201                 if (old & _DRM_LOCK_HELD) {
202                         if (context != DRM_KERNEL_CONTEXT) {
203                                 DRM_ERROR("%d holds heavyweight lock\n",
204                                           context);
205                         }
206                         return 0;
207                 }
208         }
209         if (new == (context | _DRM_LOCK_HELD)) {
210                                 /* Have lock */
211                 return 1;
212         }
213         return 0;
214 }
215 
216 /**
217  * This takes a lock forcibly and hands it to context.  Should ONLY be used
218  * inside *_unlock to give lock to kernel before calling *_dma_schedule. 
219  * 
220  * \param dev DRM device.
221  * \param lock lock pointer.
222  * \param context locking context.
223  * \return always one.
224  *
225  * Resets the lock file pointer.
226  * Marks the lock as held by the given context, via the \p cmpxchg instruction.
227  */
228 int drm_lock_transfer(drm_device_t *dev,
229                        __volatile__ unsigned int *lock, unsigned int context)
230 {
231         unsigned int old, new, prev;
232 
233         dev->lock.filp = NULL;
234         do {
235                 old  = *lock;
236                 new  = context | _DRM_LOCK_HELD;
237                 prev = cmpxchg(lock, old, new);
238         } while (prev != old);
239         return 1;
240 }
241 
242 /**
243  * Free lock.
244  * 
245  * \param dev DRM device.
246  * \param lock lock.
247  * \param context context.
248  * 
249  * Resets the lock file pointer.
250  * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
251  * waiting on the lock queue.
252  */
253 int drm_lock_free(drm_device_t *dev,
254                    __volatile__ unsigned int *lock, unsigned int context)
255 {
256         unsigned int old, new, prev;
257 
258         dev->lock.filp = NULL;
259         do {
260                 old  = *lock;
261                 new  = 0;
262                 prev = cmpxchg(lock, old, new);
263         } while (prev != old);
264         if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
265                 DRM_ERROR("%d freed heavyweight lock held by %d\n",
266                           context,
267                           _DRM_LOCKING_CONTEXT(old));
268                 return 1;
269         }
270         wake_up_interruptible(&dev->lock.lock_queue);
271         return 0;
272 }
273 
274 /**
275  * If we get here, it means that the process has called DRM_IOCTL_LOCK
276  * without calling DRM_IOCTL_UNLOCK.
277  *
278  * If the lock is not held, then let the signal proceed as usual.  If the lock
279  * is held, then set the contended flag and keep the signal blocked.
280  *
281  * \param priv pointer to a drm_sigdata structure.
282  * \return one if the signal should be delivered normally, or zero if the
283  * signal should be blocked.
284  */
285 int drm_notifier(void *priv)
286 {
287         drm_sigdata_t *s = (drm_sigdata_t *)priv;
288         unsigned int  old, new, prev;
289 
290 
291                                 /* Allow signal delivery if lock isn't held */
292         if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
293             || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) return 1;
294 
295                                 /* Otherwise, set flag to force call to
296                                    drmUnlock */
297         do {
298                 old  = s->lock->lock;
299                 new  = old | _DRM_LOCK_CONT;
300                 prev = cmpxchg(&s->lock->lock, old, new);
301         } while (prev != old);
302         return 0;
303 }
304 
  This page was automatically generated by the LXR engine.