drm_lock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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_notifier(void *priv);
  36. /**
  37. * Lock ioctl.
  38. *
  39. * \param inode device inode.
  40. * \param file_priv DRM file private.
  41. * \param cmd command.
  42. * \param arg user argument, pointing to a drm_lock structure.
  43. * \return zero on success or negative number on failure.
  44. *
  45. * Add the current task to the lock wait queue, and attempt to take to lock.
  46. */
  47. int drm_lock(struct drm_device *dev, void *data, struct drm_file *file_priv)
  48. {
  49. DECLARE_WAITQUEUE(entry, current);
  50. struct drm_lock *lock = data;
  51. int ret = 0;
  52. ++file_priv->lock_count;
  53. if (lock->context == DRM_KERNEL_CONTEXT) {
  54. DRM_ERROR("Process %d using kernel context %d\n",
  55. task_pid_nr(current), lock->context);
  56. return -EINVAL;
  57. }
  58. DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
  59. lock->context, task_pid_nr(current),
  60. dev->lock.hw_lock->lock, lock->flags);
  61. if (drm_core_check_feature(dev, DRIVER_DMA_QUEUE))
  62. if (lock->context < 0)
  63. return -EINVAL;
  64. add_wait_queue(&dev->lock.lock_queue, &entry);
  65. spin_lock(&dev->lock.spinlock);
  66. dev->lock.user_waiters++;
  67. spin_unlock(&dev->lock.spinlock);
  68. for (;;) {
  69. __set_current_state(TASK_INTERRUPTIBLE);
  70. if (!dev->lock.hw_lock) {
  71. /* Device has been unregistered */
  72. ret = -EINTR;
  73. break;
  74. }
  75. if (drm_lock_take(&dev->lock, lock->context)) {
  76. dev->lock.file_priv = file_priv;
  77. dev->lock.lock_time = jiffies;
  78. atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
  79. break; /* Got lock */
  80. }
  81. /* Contention */
  82. schedule();
  83. if (signal_pending(current)) {
  84. ret = -ERESTARTSYS;
  85. break;
  86. }
  87. }
  88. spin_lock(&dev->lock.spinlock);
  89. dev->lock.user_waiters--;
  90. spin_unlock(&dev->lock.spinlock);
  91. __set_current_state(TASK_RUNNING);
  92. remove_wait_queue(&dev->lock.lock_queue, &entry);
  93. DRM_DEBUG("%d %s\n", lock->context,
  94. ret ? "interrupted" : "has lock");
  95. if (ret) return ret;
  96. sigemptyset(&dev->sigmask);
  97. sigaddset(&dev->sigmask, SIGSTOP);
  98. sigaddset(&dev->sigmask, SIGTSTP);
  99. sigaddset(&dev->sigmask, SIGTTIN);
  100. sigaddset(&dev->sigmask, SIGTTOU);
  101. dev->sigdata.context = lock->context;
  102. dev->sigdata.lock = dev->lock.hw_lock;
  103. block_all_signals(drm_notifier, &dev->sigdata, &dev->sigmask);
  104. if (dev->driver->dma_ready && (lock->flags & _DRM_LOCK_READY))
  105. dev->driver->dma_ready(dev);
  106. if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
  107. {
  108. if (dev->driver->dma_quiescent(dev)) {
  109. DRM_DEBUG("%d waiting for DMA quiescent\n",
  110. lock->context);
  111. return -EBUSY;
  112. }
  113. }
  114. if (dev->driver->kernel_context_switch &&
  115. dev->last_context != lock->context) {
  116. dev->driver->kernel_context_switch(dev, dev->last_context,
  117. lock->context);
  118. }
  119. return 0;
  120. }
  121. /**
  122. * Unlock ioctl.
  123. *
  124. * \param inode device inode.
  125. * \param file_priv DRM file private.
  126. * \param cmd command.
  127. * \param arg user argument, pointing to a drm_lock structure.
  128. * \return zero on success or negative number on failure.
  129. *
  130. * Transfer and free the lock.
  131. */
  132. int drm_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
  133. {
  134. struct drm_lock *lock = data;
  135. unsigned long irqflags;
  136. if (lock->context == DRM_KERNEL_CONTEXT) {
  137. DRM_ERROR("Process %d using kernel context %d\n",
  138. task_pid_nr(current), lock->context);
  139. return -EINVAL;
  140. }
  141. spin_lock_irqsave(&dev->tasklet_lock, irqflags);
  142. if (dev->locked_tasklet_func) {
  143. dev->locked_tasklet_func(dev);
  144. dev->locked_tasklet_func = NULL;
  145. }
  146. spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
  147. atomic_inc(&dev->counts[_DRM_STAT_UNLOCKS]);
  148. /* kernel_context_switch isn't used by any of the x86 drm
  149. * modules but is required by the Sparc driver.
  150. */
  151. if (dev->driver->kernel_context_switch_unlock)
  152. dev->driver->kernel_context_switch_unlock(dev);
  153. else {
  154. if (drm_lock_free(&dev->lock,lock->context)) {
  155. /* FIXME: Should really bail out here. */
  156. }
  157. }
  158. unblock_all_signals();
  159. return 0;
  160. }
  161. /**
  162. * Take the heavyweight lock.
  163. *
  164. * \param lock lock pointer.
  165. * \param context locking context.
  166. * \return one if the lock is held, or zero otherwise.
  167. *
  168. * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
  169. */
  170. int drm_lock_take(struct drm_lock_data *lock_data,
  171. unsigned int context)
  172. {
  173. unsigned int old, new, prev;
  174. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  175. spin_lock(&lock_data->spinlock);
  176. do {
  177. old = *lock;
  178. if (old & _DRM_LOCK_HELD)
  179. new = old | _DRM_LOCK_CONT;
  180. else {
  181. new = context | _DRM_LOCK_HELD |
  182. ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
  183. _DRM_LOCK_CONT : 0);
  184. }
  185. prev = cmpxchg(lock, old, new);
  186. } while (prev != old);
  187. spin_unlock(&lock_data->spinlock);
  188. if (_DRM_LOCKING_CONTEXT(old) == context) {
  189. if (old & _DRM_LOCK_HELD) {
  190. if (context != DRM_KERNEL_CONTEXT) {
  191. DRM_ERROR("%d holds heavyweight lock\n",
  192. context);
  193. }
  194. return 0;
  195. }
  196. }
  197. if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
  198. /* Have lock */
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. /**
  204. * This takes a lock forcibly and hands it to context. Should ONLY be used
  205. * inside *_unlock to give lock to kernel before calling *_dma_schedule.
  206. *
  207. * \param dev DRM device.
  208. * \param lock lock pointer.
  209. * \param context locking context.
  210. * \return always one.
  211. *
  212. * Resets the lock file pointer.
  213. * Marks the lock as held by the given context, via the \p cmpxchg instruction.
  214. */
  215. static int drm_lock_transfer(struct drm_lock_data *lock_data,
  216. unsigned int context)
  217. {
  218. unsigned int old, new, prev;
  219. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  220. lock_data->file_priv = NULL;
  221. do {
  222. old = *lock;
  223. new = context | _DRM_LOCK_HELD;
  224. prev = cmpxchg(lock, old, new);
  225. } while (prev != old);
  226. return 1;
  227. }
  228. /**
  229. * Free lock.
  230. *
  231. * \param dev DRM device.
  232. * \param lock lock.
  233. * \param context context.
  234. *
  235. * Resets the lock file pointer.
  236. * Marks the lock as not held, via the \p cmpxchg instruction. Wakes any task
  237. * waiting on the lock queue.
  238. */
  239. int drm_lock_free(struct drm_lock_data *lock_data, unsigned int context)
  240. {
  241. unsigned int old, new, prev;
  242. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  243. spin_lock(&lock_data->spinlock);
  244. if (lock_data->kernel_waiters != 0) {
  245. drm_lock_transfer(lock_data, 0);
  246. lock_data->idle_has_lock = 1;
  247. spin_unlock(&lock_data->spinlock);
  248. return 1;
  249. }
  250. spin_unlock(&lock_data->spinlock);
  251. do {
  252. old = *lock;
  253. new = _DRM_LOCKING_CONTEXT(old);
  254. prev = cmpxchg(lock, old, new);
  255. } while (prev != old);
  256. if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
  257. DRM_ERROR("%d freed heavyweight lock held by %d\n",
  258. context, _DRM_LOCKING_CONTEXT(old));
  259. return 1;
  260. }
  261. wake_up_interruptible(&lock_data->lock_queue);
  262. return 0;
  263. }
  264. /**
  265. * If we get here, it means that the process has called DRM_IOCTL_LOCK
  266. * without calling DRM_IOCTL_UNLOCK.
  267. *
  268. * If the lock is not held, then let the signal proceed as usual. If the lock
  269. * is held, then set the contended flag and keep the signal blocked.
  270. *
  271. * \param priv pointer to a drm_sigdata structure.
  272. * \return one if the signal should be delivered normally, or zero if the
  273. * signal should be blocked.
  274. */
  275. static int drm_notifier(void *priv)
  276. {
  277. struct drm_sigdata *s = (struct drm_sigdata *) priv;
  278. unsigned int old, new, prev;
  279. /* Allow signal delivery if lock isn't held */
  280. if (!s->lock || !_DRM_LOCK_IS_HELD(s->lock->lock)
  281. || _DRM_LOCKING_CONTEXT(s->lock->lock) != s->context)
  282. return 1;
  283. /* Otherwise, set flag to force call to
  284. drmUnlock */
  285. do {
  286. old = s->lock->lock;
  287. new = old | _DRM_LOCK_CONT;
  288. prev = cmpxchg(&s->lock->lock, old, new);
  289. } while (prev != old);
  290. return 0;
  291. }
  292. /**
  293. * This function returns immediately and takes the hw lock
  294. * with the kernel context if it is free, otherwise it gets the highest priority when and if
  295. * it is eventually released.
  296. *
  297. * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
  298. * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
  299. * a deadlock, which is why the "idlelock" was invented).
  300. *
  301. * This should be sufficient to wait for GPU idle without
  302. * having to worry about starvation.
  303. */
  304. void drm_idlelock_take(struct drm_lock_data *lock_data)
  305. {
  306. int ret = 0;
  307. spin_lock(&lock_data->spinlock);
  308. lock_data->kernel_waiters++;
  309. if (!lock_data->idle_has_lock) {
  310. spin_unlock(&lock_data->spinlock);
  311. ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
  312. spin_lock(&lock_data->spinlock);
  313. if (ret == 1)
  314. lock_data->idle_has_lock = 1;
  315. }
  316. spin_unlock(&lock_data->spinlock);
  317. }
  318. EXPORT_SYMBOL(drm_idlelock_take);
  319. void drm_idlelock_release(struct drm_lock_data *lock_data)
  320. {
  321. unsigned int old, prev;
  322. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  323. spin_lock(&lock_data->spinlock);
  324. if (--lock_data->kernel_waiters == 0) {
  325. if (lock_data->idle_has_lock) {
  326. do {
  327. old = *lock;
  328. prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
  329. } while (prev != old);
  330. wake_up_interruptible(&lock_data->lock_queue);
  331. lock_data->idle_has_lock = 0;
  332. }
  333. }
  334. spin_unlock(&lock_data->spinlock);
  335. }
  336. EXPORT_SYMBOL(drm_idlelock_release);
  337. int drm_i_have_hw_lock(struct drm_device *dev, struct drm_file *file_priv)
  338. {
  339. return (file_priv->lock_count && dev->lock.hw_lock &&
  340. _DRM_LOCK_IS_HELD(dev->lock.hw_lock->lock) &&
  341. dev->lock.file_priv == file_priv);
  342. }
  343. EXPORT_SYMBOL(drm_i_have_hw_lock);