drm_lock.c 10 KB

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