PageRenderTime 322ms CodeModel.GetById 21ms RepoModel.GetById 14ms app.codeStats 0ms

/drivers/gpu/drm/ttm/ttm_lock.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 310 lines | 236 code | 40 blank | 34 comment | 38 complexity | db7736255178afbabdc029ee34bccc15 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 "ttm/ttm_lock.h"
  31. #include "ttm/ttm_module.h"
  32. #include <asm/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_read_lock(lock));
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(ttm_write_lock);
  166. void ttm_write_lock_downgrade(struct ttm_lock *lock)
  167. {
  168. spin_lock(&lock->lock);
  169. lock->rw = 1;
  170. wake_up_all(&lock->queue);
  171. spin_unlock(&lock->lock);
  172. }
  173. static int __ttm_vt_unlock(struct ttm_lock *lock)
  174. {
  175. int ret = 0;
  176. spin_lock(&lock->lock);
  177. if (unlikely(!(lock->flags & TTM_VT_LOCK)))
  178. ret = -EINVAL;
  179. lock->flags &= ~TTM_VT_LOCK;
  180. wake_up_all(&lock->queue);
  181. spin_unlock(&lock->lock);
  182. return ret;
  183. }
  184. static void ttm_vt_lock_remove(struct ttm_base_object **p_base)
  185. {
  186. struct ttm_base_object *base = *p_base;
  187. struct ttm_lock *lock = container_of(base, struct ttm_lock, base);
  188. int ret;
  189. *p_base = NULL;
  190. ret = __ttm_vt_unlock(lock);
  191. BUG_ON(ret != 0);
  192. }
  193. static bool __ttm_vt_lock(struct ttm_lock *lock)
  194. {
  195. bool locked = false;
  196. spin_lock(&lock->lock);
  197. if (lock->rw == 0) {
  198. lock->flags &= ~TTM_VT_LOCK_PENDING;
  199. lock->flags |= TTM_VT_LOCK;
  200. locked = true;
  201. } else {
  202. lock->flags |= TTM_VT_LOCK_PENDING;
  203. }
  204. spin_unlock(&lock->lock);
  205. return locked;
  206. }
  207. int ttm_vt_lock(struct ttm_lock *lock,
  208. bool interruptible,
  209. struct ttm_object_file *tfile)
  210. {
  211. int ret = 0;
  212. if (interruptible) {
  213. ret = wait_event_interruptible(lock->queue,
  214. __ttm_vt_lock(lock));
  215. if (unlikely(ret != 0)) {
  216. spin_lock(&lock->lock);
  217. lock->flags &= ~TTM_VT_LOCK_PENDING;
  218. wake_up_all(&lock->queue);
  219. spin_unlock(&lock->lock);
  220. return ret;
  221. }
  222. } else
  223. wait_event(lock->queue, __ttm_vt_lock(lock));
  224. /*
  225. * Add a base-object, the destructor of which will
  226. * make sure the lock is released if the client dies
  227. * while holding it.
  228. */
  229. ret = ttm_base_object_init(tfile, &lock->base, false,
  230. ttm_lock_type, &ttm_vt_lock_remove, NULL);
  231. if (ret)
  232. (void)__ttm_vt_unlock(lock);
  233. else
  234. lock->vt_holder = tfile;
  235. return ret;
  236. }
  237. EXPORT_SYMBOL(ttm_vt_lock);
  238. int ttm_vt_unlock(struct ttm_lock *lock)
  239. {
  240. return ttm_ref_object_base_unref(lock->vt_holder,
  241. lock->base.hash.key, TTM_REF_USAGE);
  242. }
  243. EXPORT_SYMBOL(ttm_vt_unlock);
  244. void ttm_suspend_unlock(struct ttm_lock *lock)
  245. {
  246. spin_lock(&lock->lock);
  247. lock->flags &= ~TTM_SUSPEND_LOCK;
  248. wake_up_all(&lock->queue);
  249. spin_unlock(&lock->lock);
  250. }
  251. EXPORT_SYMBOL(ttm_suspend_unlock);
  252. static bool __ttm_suspend_lock(struct ttm_lock *lock)
  253. {
  254. bool locked = false;
  255. spin_lock(&lock->lock);
  256. if (lock->rw == 0) {
  257. lock->flags &= ~TTM_SUSPEND_LOCK_PENDING;
  258. lock->flags |= TTM_SUSPEND_LOCK;
  259. locked = true;
  260. } else {
  261. lock->flags |= TTM_SUSPEND_LOCK_PENDING;
  262. }
  263. spin_unlock(&lock->lock);
  264. return locked;
  265. }
  266. void ttm_suspend_lock(struct ttm_lock *lock)
  267. {
  268. wait_event(lock->queue, __ttm_suspend_lock(lock));
  269. }
  270. EXPORT_SYMBOL(ttm_suspend_lock);