ttm_execbuf_util.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. struct ww_acquire_ctx *ticket)
  35. {
  36. struct ttm_validate_buffer *entry;
  37. list_for_each_entry(entry, list, head) {
  38. struct ttm_buffer_object *bo = entry->bo;
  39. if (!entry->reserved)
  40. continue;
  41. entry->reserved = false;
  42. if (entry->removed) {
  43. ttm_bo_add_to_lru(bo);
  44. entry->removed = false;
  45. }
  46. ww_mutex_unlock(&bo->resv->lock);
  47. }
  48. }
  49. static void ttm_eu_del_from_lru_locked(struct list_head *list)
  50. {
  51. struct ttm_validate_buffer *entry;
  52. list_for_each_entry(entry, list, head) {
  53. struct ttm_buffer_object *bo = entry->bo;
  54. if (!entry->reserved)
  55. continue;
  56. if (!entry->removed) {
  57. entry->put_count = ttm_bo_del_from_lru(bo);
  58. entry->removed = true;
  59. }
  60. }
  61. }
  62. static void ttm_eu_list_ref_sub(struct list_head *list)
  63. {
  64. struct ttm_validate_buffer *entry;
  65. list_for_each_entry(entry, list, head) {
  66. struct ttm_buffer_object *bo = entry->bo;
  67. if (entry->put_count) {
  68. ttm_bo_list_ref_sub(bo, entry->put_count, true);
  69. entry->put_count = 0;
  70. }
  71. }
  72. }
  73. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  74. struct list_head *list)
  75. {
  76. struct ttm_validate_buffer *entry;
  77. struct ttm_bo_global *glob;
  78. if (list_empty(list))
  79. return;
  80. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  81. glob = entry->bo->glob;
  82. spin_lock(&glob->lru_lock);
  83. ttm_eu_backoff_reservation_locked(list, 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. ww_acquire_init(ticket, &reservation_ww_class);
  115. retry:
  116. list_for_each_entry(entry, list, head) {
  117. struct ttm_buffer_object *bo = entry->bo;
  118. /* already slowpath reserved? */
  119. if (entry->reserved)
  120. continue;
  121. ret = ttm_bo_reserve_nolru(bo, true, false, true, ticket);
  122. if (ret == -EDEADLK) {
  123. /* uh oh, we lost out, drop every reservation and try
  124. * to only reserve this buffer, then start over if
  125. * this succeeds.
  126. */
  127. spin_lock(&glob->lru_lock);
  128. ttm_eu_backoff_reservation_locked(list, ticket);
  129. spin_unlock(&glob->lru_lock);
  130. ttm_eu_list_ref_sub(list);
  131. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  132. ticket);
  133. if (unlikely(ret != 0)) {
  134. if (ret == -EINTR)
  135. ret = -ERESTARTSYS;
  136. goto err_fini;
  137. }
  138. entry->reserved = true;
  139. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  140. ret = -EBUSY;
  141. goto err;
  142. }
  143. goto retry;
  144. } else if (ret)
  145. goto err;
  146. entry->reserved = true;
  147. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  148. ret = -EBUSY;
  149. goto err;
  150. }
  151. }
  152. ww_acquire_done(ticket);
  153. spin_lock(&glob->lru_lock);
  154. ttm_eu_del_from_lru_locked(list);
  155. spin_unlock(&glob->lru_lock);
  156. ttm_eu_list_ref_sub(list);
  157. return 0;
  158. err:
  159. spin_lock(&glob->lru_lock);
  160. ttm_eu_backoff_reservation_locked(list, ticket);
  161. spin_unlock(&glob->lru_lock);
  162. ttm_eu_list_ref_sub(list);
  163. err_fini:
  164. ww_acquire_done(ticket);
  165. ww_acquire_fini(ticket);
  166. return ret;
  167. }
  168. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  169. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  170. struct list_head *list, void *sync_obj)
  171. {
  172. struct ttm_validate_buffer *entry;
  173. struct ttm_buffer_object *bo;
  174. struct ttm_bo_global *glob;
  175. struct ttm_bo_device *bdev;
  176. struct ttm_bo_driver *driver;
  177. if (list_empty(list))
  178. return;
  179. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  180. bdev = bo->bdev;
  181. driver = bdev->driver;
  182. glob = bo->glob;
  183. spin_lock(&glob->lru_lock);
  184. spin_lock(&bdev->fence_lock);
  185. list_for_each_entry(entry, list, head) {
  186. bo = entry->bo;
  187. entry->old_sync_obj = bo->sync_obj;
  188. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  189. ttm_bo_add_to_lru(bo);
  190. ww_mutex_unlock(&bo->resv->lock);
  191. entry->reserved = false;
  192. }
  193. spin_unlock(&bdev->fence_lock);
  194. spin_unlock(&glob->lru_lock);
  195. ww_acquire_fini(ticket);
  196. list_for_each_entry(entry, list, head) {
  197. if (entry->old_sync_obj)
  198. driver->sync_obj_unref(&entry->old_sync_obj);
  199. }
  200. }
  201. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);