ttm_execbuf_util.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 "ttm/ttm_execbuf_util.h"
  28. #include "ttm/ttm_bo_driver.h"
  29. #include "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. if (entry->removed) {
  41. ttm_bo_add_to_lru(bo);
  42. entry->removed = false;
  43. }
  44. entry->reserved = false;
  45. atomic_set(&bo->reserved, 0);
  46. wake_up_all(&bo->event_queue);
  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. static int ttm_eu_wait_unreserved_locked(struct list_head *list,
  74. struct ttm_buffer_object *bo)
  75. {
  76. struct ttm_bo_global *glob = bo->glob;
  77. int ret;
  78. ttm_eu_del_from_lru_locked(list);
  79. spin_unlock(&glob->lru_lock);
  80. ret = ttm_bo_wait_unreserved(bo, true);
  81. spin_lock(&glob->lru_lock);
  82. if (unlikely(ret != 0))
  83. ttm_eu_backoff_reservation_locked(list);
  84. return ret;
  85. }
  86. void ttm_eu_backoff_reservation(struct list_head *list)
  87. {
  88. struct ttm_validate_buffer *entry;
  89. struct ttm_bo_global *glob;
  90. if (list_empty(list))
  91. return;
  92. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  93. glob = entry->bo->glob;
  94. spin_lock(&glob->lru_lock);
  95. ttm_eu_backoff_reservation_locked(list);
  96. spin_unlock(&glob->lru_lock);
  97. }
  98. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  99. /*
  100. * Reserve buffers for validation.
  101. *
  102. * If a buffer in the list is marked for CPU access, we back off and
  103. * wait for that buffer to become free for GPU access.
  104. *
  105. * If a buffer is reserved for another validation, the validator with
  106. * the highest validation sequence backs off and waits for that buffer
  107. * to become unreserved. This prevents deadlocks when validating multiple
  108. * buffers in different orders.
  109. */
  110. int ttm_eu_reserve_buffers(struct list_head *list)
  111. {
  112. struct ttm_bo_global *glob;
  113. struct ttm_validate_buffer *entry;
  114. int ret;
  115. uint32_t val_seq;
  116. if (list_empty(list))
  117. return 0;
  118. list_for_each_entry(entry, list, head) {
  119. entry->reserved = false;
  120. entry->put_count = 0;
  121. entry->removed = false;
  122. }
  123. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  124. glob = entry->bo->glob;
  125. retry:
  126. spin_lock(&glob->lru_lock);
  127. val_seq = entry->bo->bdev->val_seq++;
  128. list_for_each_entry(entry, list, head) {
  129. struct ttm_buffer_object *bo = entry->bo;
  130. retry_this_bo:
  131. ret = ttm_bo_reserve_locked(bo, true, true, true, val_seq);
  132. switch (ret) {
  133. case 0:
  134. break;
  135. case -EBUSY:
  136. ret = ttm_eu_wait_unreserved_locked(list, bo);
  137. if (unlikely(ret != 0)) {
  138. spin_unlock(&glob->lru_lock);
  139. ttm_eu_list_ref_sub(list);
  140. return ret;
  141. }
  142. goto retry_this_bo;
  143. case -EAGAIN:
  144. ttm_eu_backoff_reservation_locked(list);
  145. spin_unlock(&glob->lru_lock);
  146. ttm_eu_list_ref_sub(list);
  147. ret = ttm_bo_wait_unreserved(bo, true);
  148. if (unlikely(ret != 0))
  149. return ret;
  150. goto retry;
  151. default:
  152. ttm_eu_backoff_reservation_locked(list);
  153. spin_unlock(&glob->lru_lock);
  154. ttm_eu_list_ref_sub(list);
  155. return ret;
  156. }
  157. entry->reserved = true;
  158. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  159. ttm_eu_backoff_reservation_locked(list);
  160. spin_unlock(&glob->lru_lock);
  161. ttm_eu_list_ref_sub(list);
  162. ret = ttm_bo_wait_cpu(bo, false);
  163. if (ret)
  164. return ret;
  165. goto retry;
  166. }
  167. }
  168. ttm_eu_del_from_lru_locked(list);
  169. spin_unlock(&glob->lru_lock);
  170. ttm_eu_list_ref_sub(list);
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  174. void ttm_eu_fence_buffer_objects(struct list_head *list, void *sync_obj)
  175. {
  176. struct ttm_validate_buffer *entry;
  177. struct ttm_buffer_object *bo;
  178. struct ttm_bo_global *glob;
  179. struct ttm_bo_device *bdev;
  180. struct ttm_bo_driver *driver;
  181. if (list_empty(list))
  182. return;
  183. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  184. bdev = bo->bdev;
  185. driver = bdev->driver;
  186. glob = bo->glob;
  187. spin_lock(&bdev->fence_lock);
  188. spin_lock(&glob->lru_lock);
  189. list_for_each_entry(entry, list, head) {
  190. bo = entry->bo;
  191. entry->old_sync_obj = bo->sync_obj;
  192. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  193. bo->sync_obj_arg = entry->new_sync_obj_arg;
  194. ttm_bo_unreserve_locked(bo);
  195. entry->reserved = false;
  196. }
  197. spin_unlock(&glob->lru_lock);
  198. spin_unlock(&bdev->fence_lock);
  199. list_for_each_entry(entry, list, head) {
  200. if (entry->old_sync_obj)
  201. driver->sync_obj_unref(&entry->old_sync_obj);
  202. }
  203. }
  204. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);