drm_lock.c 9.3 KB

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