drm_lock.c 11 KB

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