drm_lock.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <linux/export.h>
  35. #include <drm/drmP.h>
  36. #include "drm_legacy.h"
  37. #include "drm_internal.h"
  38. static int drm_lock_take(struct drm_lock_data *lock_data, unsigned int context);
  39. /**
  40. * Take the heavyweight lock.
  41. *
  42. * \param lock lock pointer.
  43. * \param context locking context.
  44. * \return one if the lock is held, or zero otherwise.
  45. *
  46. * Attempt to mark the lock as held by the given context, via the \p cmpxchg instruction.
  47. */
  48. static
  49. int drm_lock_take(struct drm_lock_data *lock_data,
  50. unsigned int context)
  51. {
  52. unsigned int old, new, prev;
  53. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  54. spin_lock_bh(&lock_data->spinlock);
  55. do {
  56. old = *lock;
  57. if (old & _DRM_LOCK_HELD)
  58. new = old | _DRM_LOCK_CONT;
  59. else {
  60. new = context | _DRM_LOCK_HELD |
  61. ((lock_data->user_waiters + lock_data->kernel_waiters > 1) ?
  62. _DRM_LOCK_CONT : 0);
  63. }
  64. prev = cmpxchg(lock, old, new);
  65. } while (prev != old);
  66. spin_unlock_bh(&lock_data->spinlock);
  67. if (_DRM_LOCKING_CONTEXT(old) == context) {
  68. if (old & _DRM_LOCK_HELD) {
  69. if (context != DRM_KERNEL_CONTEXT) {
  70. DRM_ERROR("%d holds heavyweight lock\n",
  71. context);
  72. }
  73. return 0;
  74. }
  75. }
  76. if ((_DRM_LOCKING_CONTEXT(new)) == context && (new & _DRM_LOCK_HELD)) {
  77. /* Have lock */
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. /**
  83. * This takes a lock forcibly and hands it to context. Should ONLY be used
  84. * inside *_unlock to give lock to kernel before calling *_dma_schedule.
  85. *
  86. * \param dev DRM device.
  87. * \param lock lock pointer.
  88. * \param context locking context.
  89. * \return always one.
  90. *
  91. * Resets the lock file pointer.
  92. * Marks the lock as held by the given context, via the \p cmpxchg instruction.
  93. */
  94. static int drm_lock_transfer(struct drm_lock_data *lock_data,
  95. unsigned int context)
  96. {
  97. unsigned int old, new, prev;
  98. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  99. lock_data->file_priv = NULL;
  100. do {
  101. old = *lock;
  102. new = context | _DRM_LOCK_HELD;
  103. prev = cmpxchg(lock, old, new);
  104. } while (prev != old);
  105. return 1;
  106. }
  107. static int drm_legacy_lock_free(struct drm_lock_data *lock_data,
  108. unsigned int context)
  109. {
  110. unsigned int old, new, prev;
  111. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  112. spin_lock_bh(&lock_data->spinlock);
  113. if (lock_data->kernel_waiters != 0) {
  114. drm_lock_transfer(lock_data, 0);
  115. lock_data->idle_has_lock = 1;
  116. spin_unlock_bh(&lock_data->spinlock);
  117. return 1;
  118. }
  119. spin_unlock_bh(&lock_data->spinlock);
  120. do {
  121. old = *lock;
  122. new = _DRM_LOCKING_CONTEXT(old);
  123. prev = cmpxchg(lock, old, new);
  124. } while (prev != old);
  125. if (_DRM_LOCK_IS_HELD(old) && _DRM_LOCKING_CONTEXT(old) != context) {
  126. DRM_ERROR("%d freed heavyweight lock held by %d\n",
  127. context, _DRM_LOCKING_CONTEXT(old));
  128. return 1;
  129. }
  130. wake_up_interruptible(&lock_data->lock_queue);
  131. return 0;
  132. }
  133. /**
  134. * Lock ioctl.
  135. *
  136. * \param inode device inode.
  137. * \param file_priv DRM file private.
  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. * Add the current task to the lock wait queue, and attempt to take to lock.
  143. */
  144. int drm_legacy_lock(struct drm_device *dev, void *data,
  145. struct drm_file *file_priv)
  146. {
  147. DECLARE_WAITQUEUE(entry, current);
  148. struct drm_lock *lock = data;
  149. struct drm_master *master = file_priv->master;
  150. int ret = 0;
  151. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  152. return -EINVAL;
  153. ++file_priv->lock_count;
  154. if (lock->context == DRM_KERNEL_CONTEXT) {
  155. DRM_ERROR("Process %d using kernel context %d\n",
  156. task_pid_nr(current), lock->context);
  157. return -EINVAL;
  158. }
  159. DRM_DEBUG("%d (pid %d) requests lock (0x%08x), flags = 0x%08x\n",
  160. lock->context, task_pid_nr(current),
  161. master->lock.hw_lock->lock, lock->flags);
  162. add_wait_queue(&master->lock.lock_queue, &entry);
  163. spin_lock_bh(&master->lock.spinlock);
  164. master->lock.user_waiters++;
  165. spin_unlock_bh(&master->lock.spinlock);
  166. for (;;) {
  167. __set_current_state(TASK_INTERRUPTIBLE);
  168. if (!master->lock.hw_lock) {
  169. /* Device has been unregistered */
  170. send_sig(SIGTERM, current, 0);
  171. ret = -EINTR;
  172. break;
  173. }
  174. if (drm_lock_take(&master->lock, lock->context)) {
  175. master->lock.file_priv = file_priv;
  176. master->lock.lock_time = jiffies;
  177. break; /* Got lock */
  178. }
  179. /* Contention */
  180. mutex_unlock(&drm_global_mutex);
  181. schedule();
  182. mutex_lock(&drm_global_mutex);
  183. if (signal_pending(current)) {
  184. ret = -EINTR;
  185. break;
  186. }
  187. }
  188. spin_lock_bh(&master->lock.spinlock);
  189. master->lock.user_waiters--;
  190. spin_unlock_bh(&master->lock.spinlock);
  191. __set_current_state(TASK_RUNNING);
  192. remove_wait_queue(&master->lock.lock_queue, &entry);
  193. DRM_DEBUG("%d %s\n", lock->context,
  194. ret ? "interrupted" : "has lock");
  195. if (ret) return ret;
  196. /* don't set the block all signals on the master process for now
  197. * really probably not the correct answer but lets us debug xkb
  198. * xserver for now */
  199. if (!drm_is_current_master(file_priv)) {
  200. dev->sigdata.context = lock->context;
  201. dev->sigdata.lock = master->lock.hw_lock;
  202. }
  203. if (dev->driver->dma_quiescent && (lock->flags & _DRM_LOCK_QUIESCENT))
  204. {
  205. if (dev->driver->dma_quiescent(dev)) {
  206. DRM_DEBUG("%d waiting for DMA quiescent\n",
  207. lock->context);
  208. return -EBUSY;
  209. }
  210. }
  211. return 0;
  212. }
  213. /**
  214. * Unlock ioctl.
  215. *
  216. * \param inode device inode.
  217. * \param file_priv DRM file private.
  218. * \param cmd command.
  219. * \param arg user argument, pointing to a drm_lock structure.
  220. * \return zero on success or negative number on failure.
  221. *
  222. * Transfer and free the lock.
  223. */
  224. int drm_legacy_unlock(struct drm_device *dev, void *data, struct drm_file *file_priv)
  225. {
  226. struct drm_lock *lock = data;
  227. struct drm_master *master = file_priv->master;
  228. if (!drm_core_check_feature(dev, DRIVER_LEGACY))
  229. return -EINVAL;
  230. if (lock->context == DRM_KERNEL_CONTEXT) {
  231. DRM_ERROR("Process %d using kernel context %d\n",
  232. task_pid_nr(current), lock->context);
  233. return -EINVAL;
  234. }
  235. if (drm_legacy_lock_free(&master->lock, lock->context)) {
  236. /* FIXME: Should really bail out here. */
  237. }
  238. return 0;
  239. }
  240. /**
  241. * This function returns immediately and takes the hw lock
  242. * with the kernel context if it is free, otherwise it gets the highest priority when and if
  243. * it is eventually released.
  244. *
  245. * This guarantees that the kernel will _eventually_ have the lock _unless_ it is held
  246. * by a blocked process. (In the latter case an explicit wait for the hardware lock would cause
  247. * a deadlock, which is why the "idlelock" was invented).
  248. *
  249. * This should be sufficient to wait for GPU idle without
  250. * having to worry about starvation.
  251. */
  252. void drm_legacy_idlelock_take(struct drm_lock_data *lock_data)
  253. {
  254. int ret;
  255. spin_lock_bh(&lock_data->spinlock);
  256. lock_data->kernel_waiters++;
  257. if (!lock_data->idle_has_lock) {
  258. spin_unlock_bh(&lock_data->spinlock);
  259. ret = drm_lock_take(lock_data, DRM_KERNEL_CONTEXT);
  260. spin_lock_bh(&lock_data->spinlock);
  261. if (ret == 1)
  262. lock_data->idle_has_lock = 1;
  263. }
  264. spin_unlock_bh(&lock_data->spinlock);
  265. }
  266. EXPORT_SYMBOL(drm_legacy_idlelock_take);
  267. void drm_legacy_idlelock_release(struct drm_lock_data *lock_data)
  268. {
  269. unsigned int old, prev;
  270. volatile unsigned int *lock = &lock_data->hw_lock->lock;
  271. spin_lock_bh(&lock_data->spinlock);
  272. if (--lock_data->kernel_waiters == 0) {
  273. if (lock_data->idle_has_lock) {
  274. do {
  275. old = *lock;
  276. prev = cmpxchg(lock, old, DRM_KERNEL_CONTEXT);
  277. } while (prev != old);
  278. wake_up_interruptible(&lock_data->lock_queue);
  279. lock_data->idle_has_lock = 0;
  280. }
  281. }
  282. spin_unlock_bh(&lock_data->spinlock);
  283. }
  284. EXPORT_SYMBOL(drm_legacy_idlelock_release);
  285. static int drm_legacy_i_have_hw_lock(struct drm_device *dev,
  286. struct drm_file *file_priv)
  287. {
  288. struct drm_master *master = file_priv->master;
  289. return (file_priv->lock_count && master->lock.hw_lock &&
  290. _DRM_LOCK_IS_HELD(master->lock.hw_lock->lock) &&
  291. master->lock.file_priv == file_priv);
  292. }
  293. void drm_legacy_lock_release(struct drm_device *dev, struct file *filp)
  294. {
  295. struct drm_file *file_priv = filp->private_data;
  296. /* if the master has gone away we can't do anything with the lock */
  297. if (!dev->master)
  298. return;
  299. if (drm_legacy_i_have_hw_lock(dev, file_priv)) {
  300. DRM_DEBUG("File %p released, freeing lock for context %d\n",
  301. filp, _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  302. drm_legacy_lock_free(&file_priv->master->lock,
  303. _DRM_LOCKING_CONTEXT(file_priv->master->lock.hw_lock->lock));
  304. }
  305. }