drm_lock.c 9.7 KB

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