drm_lock.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. unsigned long irqflags;
  142. if (copy_from_user(&lock, (drm_lock_t __user *) arg, sizeof(lock)))
  143. return -EFAULT;
  144. if (lock.context == DRM_KERNEL_CONTEXT) {
  145. DRM_ERROR("Process %d using kernel context %d\n",
  146. current->pid, lock.context);
  147. return -EINVAL;
  148. }
  149. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  150. if (dev->locked_tasklet_func) {
  151. dev->locked_tasklet_func(dev);
  152. dev->locked_tasklet_func = NULL;
  153. }
  154. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  155. atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
  156. /* kernel_context_switch isn't used by any of the x86 drm
  157. * modules but is required by the Sparc driver.
  158. */
  159. if (dev->driver->kernel_context_switch_unlock)
  160. dev->driver->kernel_context_switch_unlock(dev);
  161. else {
  162. drm_lock_transfer(dev, &dev->lock.hw_lock->lock,
  163. DRM_KERNEL_CONTEXT);
  164. if (drm_lock_free(dev, &dev->lock.hw_lock->lock,
  165. DRM_KERNEL_CONTEXT)) {
  166. DRM_ERROR("\n");
  167. }
  168. }
  169. unblock_all_signals();
  170. return 0;
  171. }
  172. /**
  173. * Take the heavyweight lock.
  174. *
  175. * \param lock lock pointer.
  176. * \param context locking context.
  177. * \return one if the lock is held, or zero otherwise.
  178. *
  179. * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
  180. */
  181. int drm_lock_take(__volatile__ unsigned int *lock, unsigned int context)
  182. {
  183. unsigned int old, new, prev;
  184. do {
  185. old = *lock;
  186. if (old & _DRM_LOCK_HELD)
  187. new = old | _DRM_LOCK_CONT;
  188. else
  189. new = context | _DRM_LOCK_HELD;
  190. prev = cmpxchg(lock, old, new);
  191. } while (prev != old);
  192. if (_DRM_LOCKING_CONTEXT(old) == context) {
  193. if (old & _DRM_LOCK_HELD) {
  194. if (context != DRM_KERNEL_CONTEXT) {
  195. DRM_ERROR("%d holds heavyweight lock\n",
  196. context);
  197. }
  198. return 0;
  199. }
  200. }
  201. if (new == (context | _DRM_LOCK_HELD)) {
  202. /* Have lock */
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. /**
  208. * This takes a lock forcibly and hands it to context. Should ONLY be used
  209. * inside *_unlock to give lock to kernel before calling *_dma_schedule.
  210. *
  211. * \param dev DRM device.
  212. * \param lock lock pointer.
  213. * \param context locking context.
  214. * \return always one.
  215. *
  216. * Resets the lock file pointer.
  217. * Marks the lock as held by the given context, via the \p cmpxchg instruction.
  218. */
  219. static int drm_lock_transfer(drm_device_t * dev,
  220. __volatile__ unsigned int *lock,
  221. unsigned int context)
  222. {
  223. unsigned int old, new, prev;
  224. dev->lock.filp = NULL;
  225. do {
  226. old = *lock;
  227. new = context | _DRM_LOCK_HELD;
  228. prev = cmpxchg(lock, old, new);
  229. } while (prev != old);
  230. return 1;
  231. }
  232. /**
  233. * Free lock.
  234. *
  235. * \param dev DRM device.
  236. * \param lock lock.
  237. * \param context context.
  238. *
  239. * Resets the lock file pointer.
  240. * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
  241. * waiting on the lock queue.
  242. */
  243. int drm_lock_free(drm_device_t * dev,
  244. __volatile__ unsigned int *lock, unsigned int context)
  245. {
  246. unsigned int old, new, prev;
  247. dev->lock.filp = NULL;
  248. do {
  249. old = *lock;
  250. new = 0;
  251. prev = cmpxchg(lock, old, new);
  252. } while (prev != old);
  253. if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
  254. DRM_ERROR("%d freed heavyweight lock held by %d\n",
  255. context, _DRM_LOCKING_CONTEXT(old));
  256. return 1;
  257. }
  258. wake_up_interruptible(&dev->lock.lock_queue);
  259. return 0;
  260. }
  261. /**
  262. * If we get here, it means that the process has called DRM_IOCTL_LOCK
  263. * without calling DRM_IOCTL_UNLOCK.
  264. *
  265. * If the lock is not held, then let the signal proceed as usual. If the lock
  266. * is held, then set the contended flag and keep the signal blocked.
  267. *
  268. * \param priv pointer to a drm_sigdata structure.
  269. * \return one if the signal should be delivered normally, or zero if the
  270. * signal should be blocked.
  271. */
  272. static int drm_notifier(void *priv)
  273. {
  274. drm_sigdata_t *s = (drm_sigdata_t *) priv;
  275. unsigned int old, new, prev;
  276. /* Allow signal delivery if lock isn't held */
  277. if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
  278. || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
  279. return 1;
  280. /* Otherwise, set flag to force call to
  281. drmUnlock */
  282. do {
  283. old = s->lock->lock;
  284. new = old | _DRM_LOCK_CONT;
  285. prev = cmpxchg(&s->lock->lock, old, new);
  286. } while (prev != old);
  287. return 0;
  288. }