drm_lock.c 10 KB

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