ttm_lock.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2007-2009 VMware, Inc., Palo Alto, CA., USA
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. **************************************************************************/
  27. /*
  28. * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
  29. */
  30. #include <drm/ttm/ttm_lock.h>
  31. #include <drm/ttm/ttm_module.h>
  32. #include <linux/atomic.h>
  33. #include <linux/errno.h>
  34. #include <linux/wait.h>
  35. #include <linux/sched.h>
  36. #include <linux/module.h>
  37. #define TTM_WRITE_LOCK_PENDING (1 << 0)
  38. #define TTM_VT_LOCK_PENDING (1 << 1)
  39. #define TTM_SUSPEND_LOCK_PENDING (1 << 2)
  40. #define TTM_VT_LOCK (1 << 3)
  41. #define TTM_SUSPEND_LOCK (1 << 4)
  42. void ttm_lock_init(struct ttm_lock *lock)
  43. {
  44. spin_lock_init(&lock->lock);
  45. init_waitqueue_head(&lock->queue);
  46. lock->rw = 0;
  47. lock->flags = 0;
  48. lock->kill_takers = false;
  49. lock->signal = SIGKILL;
  50. }
  51. EXPORT_SYMBOL(ttm_lock_init);
  52. void ttm_read_unlock(struct ttm_lock *lock)
  53. {
  54. spin_lock(&lock->lock);
  55. if (--lock->rw == 0)
  56. wake_up_all(&lock->queue);
  57. spin_unlock(&lock->lock);
  58. }
  59. EXPORT_SYMBOL(ttm_read_unlock);
  60. static bool __ttm_read_lock(struct ttm_lock *lock)
  61. {
  62. bool locked = false;
  63. spin_lock(&lock->lock);
  64. if (unlikely(lock->kill_takers)) {
  65. send_sig(lock->signal, current, 0);
  66. spin_unlock(&lock->lock);
  67. return false;
  68. }
  69. if (lock->rw >= 0 && lock->flags == 0) {
  70. ++lock->rw;
  71. locked = true;
  72. }
  73. spin_unlock(&lock->lock);
  74. return locked;
  75. }
  76. int ttm_read_lock(struct ttm_lock *lock, bool interruptible)
  77. {
  78. int ret = 0;
  79. if (interruptible)
  80. ret = wait_event_interruptible(lock->queue,
  81. __ttm_read_lock(lock));
  82. else
  83. wait_event(lock->queue, __ttm_read_lock(lock));
  84. return ret;
  85. }
  86. EXPORT_SYMBOL(ttm_read_lock);
  87. static bool __ttm_read_trylock(struct ttm_lock *lock, bool *locked)
  88. {
  89. bool block = true;
  90. *locked = false;
  91. spin_lock(&lock->lock);
  92. if (unlikely(lock->kill_takers)) {
  93. send_sig(lock->signal, current, 0);
  94. spin_unlock(&lock->lock);
  95. return false;
  96. }
  97. if (lock->rw >= 0 && lock->flags == 0) {
  98. ++lock->rw;
  99. block = false;
  100. *locked = true;
  101. } else if (lock->flags == 0) {
  102. block = false;
  103. }
  104. spin_unlock(&lock->lock);
  105. return !block;
  106. }
  107. int ttm_read_trylock(struct ttm_lock *lock, bool interruptible)
  108. {
  109. int ret = 0;
  110. bool locked;
  111. if (interruptible)
  112. ret = wait_event_interruptible
  113. (lock->queue, __ttm_read_trylock(lock, &locked));
  114. else
  115. wait_event(lock->queue, __ttm_read_trylock(lock, &locked));
  116. if (unlikely(ret != 0)) {
  117. BUG_ON(locked);
  118. return ret;
  119. }
  120. return (locked) ? 0 : -EBUSY;
  121. }
  122. void ttm_write_unlock(struct ttm_lock *lock)
  123. {
  124. spin_lock(&lock->lock);
  125. lock->rw = 0;
  126. wake_up_all(&lock->queue);
  127. spin_unlock(&lock->lock);
  128. }
  129. EXPORT_SYMBOL(ttm_write_unlock);
  130. static bool __ttm_write_lock(struct ttm_lock *lock)
  131. {
  132. bool locked = false;
  133. spin_lock(&lock->lock);
  134. if (unlikely(lock->kill_takers)) {
  135. send_sig(lock->signal, current, 0);
  136. spin_unlock(&lock->lock);
  137. return false;
  138. }
  139. if (lock->rw == 0 && ((lock->flags & ~TTM_WRITE_LOCK_PENDING) == 0)) {
  140. lock->rw = -1;
  141. lock->flags &= ~TTM_WRITE_LOCK_PENDING;
  142. locked = true;
  143. } else {
  144. lock->flags |= TTM_WRITE_LOCK_PENDING;
  145. }
  146. spin_unlock(&lock->lock);
  147. return locked;
  148. }
  149. int ttm_write_lock(struct ttm_lock *lock, bool interruptible)
  150. {
  151. int ret = 0;
  152. if (interruptible) {
  153. ret = wait_event_interruptible(lock->queue,
  154. __ttm_write_lock(lock));
  155. if (unlikely(ret != 0)) {
  156. spin_lock(&lock->lock);
  157. lock->flags &= ~TTM_WRITE_LOCK_PENDING;
  158. wake_up_all(&lock->queue);
  159. spin_unlock(&lock->lock);
  160. }
  161. } else
  162. wait_event(lock->queue, __ttm_write_lock(lock));
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(ttm_write_lock);
  166. static int __ttm_vt_unlock(struct ttm_lock *lock)
  167. {
  168. int ret = 0;
  169. spin_lock(&lock->lock);
  170. if (unlikely(!(lock->flags & TTM_VT_LOCK)))
  171. ret = -EINVAL;
  172. lock->flags &= ~TTM_VT_LOCK;
  173. wake_up_all(&lock->queue);
  174. spin_unlock(&lock->lock);
  175. return ret;
  176. }
  177. static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
  178. {
  179. struct ttm_base_object *base = *p_base;
  180. struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
  181. int ret;
  182. *p_base = NULL;
  183. ret = __ttm_vt_unlock(lock);
  184. BUG_ON(ret != 0);
  185. }
  186. static bool __ttm_vt_lock(struct ttm_lock *lock)
  187. {
  188. bool locked = false;
  189. spin_lock(&lock->lock);
  190. if (lock->rw == 0) {
  191. lock->flags &= ~TTM_VT_LOCK_PENDING;
  192. lock->flags |= TTM_VT_LOCK;
  193. locked = true;
  194. } else {
  195. lock->flags |= TTM_VT_LOCK_PENDING;
  196. }
  197. spin_unlock(&lock->lock);
  198. return locked;
  199. }
  200. int ttm_vt_lock(struct ttm_lock *lock,
  201. bool interruptible,
  202. struct ttm_object_file *tfile)
  203. {
  204. int ret = 0;
  205. if (interruptible) {
  206. ret = wait_event_interruptible(lock->queue,
  207. __ttm_vt_lock(lock));
  208. if (unlikely(ret != 0)) {
  209. spin_lock(&lock->lock);
  210. lock->flags &= ~TTM_VT_LOCK_PENDING;
  211. wake_up_all(&lock->queue);
  212. spin_unlock(&lock->lock);
  213. return ret;
  214. }
  215. } else
  216. wait_event(lock->queue, __ttm_vt_lock(lock));
  217. /*
  218. * Add a base-object, the destructor of which will
  219. * make sure the lock is released if the client dies
  220. * while holding it.
  221. */
  222. ret = ttm_base_object_init(tfile, &lock->base, false,
  223. ttm_lock_type, &ttm_vt_lock_remove, NULL);
  224. if (ret)
  225. (void)__ttm_vt_unlock(lock);
  226. else
  227. lock->vt_holder = tfile;
  228. return ret;
  229. }
  230. EXPORT_SYMBOL(ttm_vt_lock);
  231. int ttm_vt_unlock(struct ttm_lock *lock)
  232. {
  233. return ttm_ref_object_base_unref(lock->vt_holder,
  234. lock->base.hash.key, TTM_REF_USAGE);
  235. }
  236. EXPORT_SYMBOL(ttm_vt_unlock);
  237. void ttm_suspend_unlock(struct ttm_lock *lock)
  238. {
  239. spin_lock(&lock->lock);
  240. lock->flags &= ~TTM_SUSPEND_LOCK;
  241. wake_up_all(&lock->queue);
  242. spin_unlock(&lock->lock);
  243. }
  244. EXPORT_SYMBOL(ttm_suspend_unlock);
  245. static bool __ttm_suspend_lock(struct ttm_lock *lock)
  246. {
  247. bool locked = false;
  248. spin_lock(&lock->lock);
  249. if (lock->rw == 0) {
  250. lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
  251. lock->flags |= TTM_SUSPEND_LOCK;
  252. locked = true;
  253. } else {
  254. lock->flags |= TTM_SUSPEND_LOCK_PENDING;
  255. }
  256. spin_unlock(&lock->lock);
  257. return locked;
  258. }
  259. void ttm_suspend_lock(struct ttm_lock *lock)
  260. {
  261. wait_event(lock->queue, __ttm_suspend_lock(lock));
  262. }
  263. EXPORT_SYMBOL(ttm_suspend_lock);