drm_lock.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. * Created: Tue Feb 2 08:37:54 1999 by faith@valinux.com
  10. *
  11. * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
  12. * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
  13. * All Rights Reserved.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a
  16. * copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice (including the next
  23. * paragraph) shall be included in all copies or substantial portions of the
  24. * Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  30. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  31. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  32. * OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. #include "drmP.h"
  35. /**
  36. * Lock ioctl.
  37. *
  38. * \param inode device inode.
  39. * \param filp file pointer.
  40. * \param cmd command.
  41. * \param arg user argument, pointing to a drm_lock structure.
  42. * \return zero on success or negative number on failure.
  43. *
  44. * Add the current task to the lock wait queue, and attempt to take to lock.
  45. */
  46. int drm_lock( struct inode *inode, struct file *filp,
  47. unsigned int cmd, unsigned long arg )
  48. {
  49. drm_file_t *priv = filp->private_data;
  50. drm_device_t *dev = priv->head->dev;
  51. DECLARE_WAITQUEUE( entry, current );
  52. drm_lock_t lock;
  53. int ret = 0;
  54. ++priv->lock_count;
  55. if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
  56. return -EFAULT;
  57. if ( lock.context == DRM_KERNEL_CONTEXT ) {
  58. DRM_ERROR( "Process %d using kernel context %d\n",
  59. current->pid, lock.context );
  60. return -EINVAL;
  61. }
  62. DRM_DEBUG( "%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
  63. lock.context, current->pid,
  64. dev->lock.hw_lock->lock, lock.flags );
  65. if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
  66. if ( lock.context < 0 )
  67. return -EINVAL;
  68. add_wait_queue( &dev->lock.lock_queue, &entry );
  69. for (;;) {
  70. __set_current_state(TASK_INTERRUPTIBLE);
  71. if ( !dev->lock.hw_lock ) {
  72. /* Device has been unregistered */
  73. ret = -EINTR;
  74. break;
  75. }
  76. if ( drm_lock_take( &dev->lock.hw_lock->lock,
  77. lock.context ) ) {
  78. dev->lock.filp = filp;
  79. dev->lock.lock_time = jiffies;
  80. atomic_inc( &dev->counts[_DRM_STAT_LOCKS] );
  81. break; /* Got lock */
  82. }
  83. /* Contention */
  84. schedule();
  85. if ( signal_pending( current ) ) {
  86. ret = -ERESTARTSYS;
  87. break;
  88. }
  89. }
  90. __set_current_state(TASK_RUNNING);
  91. remove_wait_queue( &dev->lock.lock_queue, &entry );
  92. sigemptyset( &dev->sigmask );
  93. sigaddset( &dev->sigmask, SIGSTOP );
  94. sigaddset( &dev->sigmask, SIGTSTP );
  95. sigaddset( &dev->sigmask, SIGTTIN );
  96. sigaddset( &dev->sigmask, SIGTTOU );
  97. dev->sigdata.context = lock.context;
  98. dev->sigdata.lock = dev->lock.hw_lock;
  99. block_all_signals( drm_notifier,
  100. &dev->sigdata, &dev->sigmask );
  101. if (dev->driver->dma_ready && (lock.flags & _DRM_LOCK_READY))
  102. dev->driver->dma_ready(dev);
  103. if ( dev->driver->dma_quiescent && (lock.flags & _DRM_LOCK_QUIESCENT ))
  104. return dev->driver->dma_quiescent(dev);
  105. /* dev->driver->kernel_context_switch isn't used by any of the x86
  106. * drivers but is used by the Sparc driver.
  107. */
  108. if (dev->driver->kernel_context_switch &&
  109. dev->last_context != lock.context) {
  110. dev->driver->kernel_context_switch(dev, dev->last_context,
  111. lock.context);
  112. }
  113. DRM_DEBUG( "%d %s\n", lock.context, ret ? "interrupted" : "has lock" );
  114. return ret;
  115. }
  116. /**
  117. * Unlock ioctl.
  118. *
  119. * \param inode device inode.
  120. * \param filp file pointer.
  121. * \param cmd command.
  122. * \param arg user argument, pointing to a drm_lock structure.
  123. * \return zero on success or negative number on failure.
  124. *
  125. * Transfer and free the lock.
  126. */
  127. int drm_unlock( struct inode *inode, struct file *filp,
  128. unsigned int cmd, unsigned long arg )
  129. {
  130. drm_file_t *priv = filp->private_data;
  131. drm_device_t *dev = priv->head->dev;
  132. drm_lock_t lock;
  133. if ( copy_from_user( &lock, (drm_lock_t __user *)arg, sizeof(lock) ) )
  134. return -EFAULT;
  135. if ( lock.context == DRM_KERNEL_CONTEXT ) {
  136. DRM_ERROR( "Process %d using kernel context %d\n",
  137. current->pid, lock.context );
  138. return -EINVAL;
  139. }
  140. atomic_inc( &dev->counts[_DRM_STAT_UNLOCKS] );
  141. /* kernel_context_switch isn't used by any of the x86 drm
  142. * modules but is required by the Sparc driver.
  143. */
  144. if (dev->driver->kernel_context_switch_unlock)
  145. dev->driver->kernel_context_switch_unlock(dev, &lock);
  146. else {
  147. drm_lock_transfer( dev, &dev->lock.hw_lock->lock,
  148. DRM_KERNEL_CONTEXT );
  149. if ( drm_lock_free( dev, &dev->lock.hw_lock->lock,
  150. DRM_KERNEL_CONTEXT ) ) {
  151. DRM_ERROR( "\n" );
  152. }
  153. }
  154. unblock_all_signals();
  155. return 0;
  156. }
  157. /**
  158. * Take the heavyweight lock.
  159. *
  160. * \param lock lock pointer.
  161. * \param context locking context.
  162. * \return one if the lock is held, or zero otherwise.
  163. *
  164. * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
  165. */
  166. int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
  167. {
  168. unsigned int old, new, prev;
  169. do {
  170. old = *lock;
  171. if (old & _DRM_LOCK_HELD) new = old | _DRM_LOCK_CONT;
  172. else new = context | _DRM_LOCK_HELD;
  173. prev = cmpxchg(lock, old, new);
  174. } while (prev != old);
  175. if (_DRM_LOCKING_CONTEXT(old) == context) {
  176. if (old & _DRM_LOCK_HELD) {
  177. if (context != DRM_KERNEL_CONTEXT) {
  178. DRM_ERROR("%d holds heavyweight lock\n",
  179. context);
  180. }
  181. return 0;
  182. }
  183. }
  184. if (new == (context | _DRM_LOCK_HELD)) {
  185. /* Have lock */
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. /**
  191. * This takes a lock forcibly and hands it to context. Should ONLY be used
  192. * inside *_unlock to give lock to kernel before calling *_dma_schedule.
  193. *
  194. * \param dev DRM device.
  195. * \param lock lock pointer.
  196. * \param context locking context.
  197. * \return always one.
  198. *
  199. * Resets the lock file pointer.
  200. * Marks the lock as held by the given context, via the \p cmpxchg instruction.
  201. */
  202. int drm_lock_transfer(drm_device_t *dev,
  203. __volatile__ unsigned int *lock, unsigned int context)
  204. {
  205. unsigned int old, new, prev;
  206. dev->lock.filp = NULL;
  207. do {
  208. old = *lock;
  209. new = context | _DRM_LOCK_HELD;
  210. prev = cmpxchg(lock, old, new);
  211. } while (prev != old);
  212. return 1;
  213. }
  214. /**
  215. * Free lock.
  216. *
  217. * \param dev DRM device.
  218. * \param lock lock.
  219. * \param context context.
  220. *
  221. * Resets the lock file pointer.
  222. * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
  223. * waiting on the lock queue.
  224. */
  225. int drm_lock_free(drm_device_t *dev,
  226. __volatile__ unsigned int *lock, unsigned int context)
  227. {
  228. unsigned int old, new, prev;
  229. dev->lock.filp = NULL;
  230. do {
  231. old = *lock;
  232. new = 0;
  233. prev = cmpxchg(lock, old, new);
  234. } while (prev != old);
  235. if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
  236. DRM_ERROR("%d freed heavyweight lock held by %d\n",
  237. context,
  238. _DRM_LOCKING_CONTEXT(old));
  239. return 1;
  240. }
  241. wake_up_interruptible(&dev->lock.lock_queue);
  242. return 0;
  243. }
  244. /**
  245. * If we get here, it means that the process has called DRM_IOCTL_LOCK
  246. * without calling DRM_IOCTL_UNLOCK.
  247. *
  248. * If the lock is not held, then let the signal proceed as usual. If the lock
  249. * is held, then set the contended flag and keep the signal blocked.
  250. *
  251. * \param priv pointer to a drm_sigdata structure.
  252. * \return one if the signal should be delivered normally, or zero if the
  253. * signal should be blocked.
  254. */
  255. int drm_notifier(void *priv)
  256. {
  257. drm_sigdata_t *s = (drm_sigdata_t *)priv;
  258. unsigned int old, new, prev;
  259. /* Allow signal delivery if lock isn't held */
  260. if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
  261. || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context) return 1;
  262. /* Otherwise, set flag to force call to
  263. drmUnlock */
  264. do {
  265. old = s->lock->lock;
  266. new = old | _DRM_LOCK_CONT;
  267. prev = cmpxchg(&s->lock->lock, old, new);
  268. } while (prev != old);
  269. return 0;
  270. }