drm_lock.c 8.5 KB

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