ttm_execbuf_util.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-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. #include <drm/ttm/ttm_execbuf_util.h>
  28. #include <drm/ttm/ttm_bo_driver.h>
  29. #include <drm/ttm/ttm_placement.h>
  30. #include <linux/wait.h>
  31. #include <linux/sched.h>
  32. #include <linux/module.h>
  33. static void ttm_eu_backoff_reservation_locked(struct list_head *list)
  34. {
  35. struct ttm_validate_buffer *entry;
  36. list_for_each_entry(entry, list, head) {
  37. struct ttm_buffer_object *bo = entry->bo;
  38. if (!entry->reserved)
  39. continue;
  40. entry->reserved = false;
  41. if (entry->removed) {
  42. ttm_bo_add_to_lru(bo);
  43. entry->removed = false;
  44. }
  45. ww_mutex_unlock(&bo->resv->lock);
  46. }
  47. }
  48. static void ttm_eu_del_from_lru_locked(struct list_head *list)
  49. {
  50. struct ttm_validate_buffer *entry;
  51. list_for_each_entry(entry, list, head) {
  52. struct ttm_buffer_object *bo = entry->bo;
  53. if (!entry->reserved)
  54. continue;
  55. if (!entry->removed) {
  56. entry->put_count = ttm_bo_del_from_lru(bo);
  57. entry->removed = true;
  58. }
  59. }
  60. }
  61. static void ttm_eu_list_ref_sub(struct list_head *list)
  62. {
  63. struct ttm_validate_buffer *entry;
  64. list_for_each_entry(entry, list, head) {
  65. struct ttm_buffer_object *bo = entry->bo;
  66. if (entry->put_count) {
  67. ttm_bo_list_ref_sub(bo, entry->put_count, true);
  68. entry->put_count = 0;
  69. }
  70. }
  71. }
  72. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  73. struct list_head *list)
  74. {
  75. struct ttm_validate_buffer *entry;
  76. struct ttm_bo_global *glob;
  77. if (list_empty(list))
  78. return;
  79. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  80. glob = entry->bo->glob;
  81. spin_lock(&glob->lru_lock);
  82. ttm_eu_backoff_reservation_locked(list);
  83. if (ticket)
  84. ww_acquire_fini(ticket);
  85. spin_unlock(&glob->lru_lock);
  86. }
  87. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  88. /*
  89. * Reserve buffers for validation.
  90. *
  91. * If a buffer in the list is marked for CPU access, we back off and
  92. * wait for that buffer to become free for GPU access.
  93. *
  94. * If a buffer is reserved for another validation, the validator with
  95. * the highest validation sequence backs off and waits for that buffer
  96. * to become unreserved. This prevents deadlocks when validating multiple
  97. * buffers in different orders.
  98. */
  99. int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  100. struct list_head *list)
  101. {
  102. struct ttm_bo_global *glob;
  103. struct ttm_validate_buffer *entry;
  104. int ret;
  105. if (list_empty(list))
  106. return 0;
  107. list_for_each_entry(entry, list, head) {
  108. entry->reserved = false;
  109. entry->put_count = 0;
  110. entry->removed = false;
  111. }
  112. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  113. glob = entry->bo->glob;
  114. if (ticket)
  115. ww_acquire_init(ticket, &reservation_ww_class);
  116. retry:
  117. list_for_each_entry(entry, list, head) {
  118. struct ttm_buffer_object *bo = entry->bo;
  119. /* already slowpath reserved? */
  120. if (entry->reserved)
  121. continue;
  122. ret = ttm_bo_reserve_nolru(bo, true, (ticket == NULL), true,
  123. ticket);
  124. if (ret == -EDEADLK) {
  125. /* uh oh, we lost out, drop every reservation and try
  126. * to only reserve this buffer, then start over if
  127. * this succeeds.
  128. */
  129. BUG_ON(ticket == NULL);
  130. spin_lock(&glob->lru_lock);
  131. ttm_eu_backoff_reservation_locked(list);
  132. spin_unlock(&glob->lru_lock);
  133. ttm_eu_list_ref_sub(list);
  134. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  135. ticket);
  136. if (unlikely(ret != 0)) {
  137. if (ret == -EINTR)
  138. ret = -ERESTARTSYS;
  139. goto err_fini;
  140. }
  141. entry->reserved = true;
  142. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  143. ret = -EBUSY;
  144. goto err;
  145. }
  146. goto retry;
  147. } else if (ret)
  148. goto err;
  149. entry->reserved = true;
  150. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  151. ret = -EBUSY;
  152. goto err;
  153. }
  154. }
  155. if (ticket)
  156. ww_acquire_done(ticket);
  157. spin_lock(&glob->lru_lock);
  158. ttm_eu_del_from_lru_locked(list);
  159. spin_unlock(&glob->lru_lock);
  160. ttm_eu_list_ref_sub(list);
  161. return 0;
  162. err:
  163. spin_lock(&glob->lru_lock);
  164. ttm_eu_backoff_reservation_locked(list);
  165. spin_unlock(&glob->lru_lock);
  166. ttm_eu_list_ref_sub(list);
  167. err_fini:
  168. if (ticket) {
  169. ww_acquire_done(ticket);
  170. ww_acquire_fini(ticket);
  171. }
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  175. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  176. struct list_head *list, void *sync_obj)
  177. {
  178. struct ttm_validate_buffer *entry;
  179. struct ttm_buffer_object *bo;
  180. struct ttm_bo_global *glob;
  181. struct ttm_bo_device *bdev;
  182. struct ttm_bo_driver *driver;
  183. if (list_empty(list))
  184. return;
  185. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  186. bdev = bo->bdev;
  187. driver = bdev->driver;
  188. glob = bo->glob;
  189. spin_lock(&glob->lru_lock);
  190. spin_lock(&bdev->fence_lock);
  191. list_for_each_entry(entry, list, head) {
  192. bo = entry->bo;
  193. entry->old_sync_obj = bo->sync_obj;
  194. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  195. ttm_bo_add_to_lru(bo);
  196. ww_mutex_unlock(&bo->resv->lock);
  197. entry->reserved = false;
  198. }
  199. spin_unlock(&bdev->fence_lock);
  200. spin_unlock(&glob->lru_lock);
  201. if (ticket)
  202. ww_acquire_fini(ticket);
  203. list_for_each_entry(entry, list, head) {
  204. if (entry->old_sync_obj)
  205. driver->sync_obj_unref(&entry->old_sync_obj);
  206. }
  207. }
  208. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);