ttm_execbuf_util.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. 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. void ttm_eu_backoff_reservation(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. spin_unlock(&glob->lru_lock);
  84. }
  85. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  86. /*
  87. * Reserve buffers for validation.
  88. *
  89. * If a buffer in the list is marked for CPU access, we back off and
  90. * wait for that buffer to become free for GPU access.
  91. *
  92. * If a buffer is reserved for another validation, the validator with
  93. * the highest validation sequence backs off and waits for that buffer
  94. * to become unreserved. This prevents deadlocks when validating multiple
  95. * buffers in different orders.
  96. */
  97. int ttm_eu_reserve_buffers(struct list_head *list)
  98. {
  99. struct ttm_bo_global *glob;
  100. struct ttm_validate_buffer *entry;
  101. int ret;
  102. uint32_t val_seq;
  103. if (list_empty(list))
  104. return 0;
  105. list_for_each_entry(entry, list, head) {
  106. entry->reserved = false;
  107. entry->put_count = 0;
  108. entry->removed = false;
  109. }
  110. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  111. glob = entry->bo->glob;
  112. spin_lock(&glob->lru_lock);
  113. val_seq = entry->bo->bdev->val_seq++;
  114. retry:
  115. list_for_each_entry(entry, list, head) {
  116. struct ttm_buffer_object *bo = entry->bo;
  117. /* already slowpath reserved? */
  118. if (entry->reserved)
  119. continue;
  120. ret = ttm_bo_reserve_nolru(bo, true, true, true, val_seq);
  121. switch (ret) {
  122. case 0:
  123. break;
  124. case -EBUSY:
  125. ttm_eu_del_from_lru_locked(list);
  126. spin_unlock(&glob->lru_lock);
  127. ret = ttm_bo_reserve_nolru(bo, true, false,
  128. true, val_seq);
  129. spin_lock(&glob->lru_lock);
  130. if (!ret)
  131. break;
  132. if (unlikely(ret != -EAGAIN))
  133. goto err;
  134. /* fallthrough */
  135. case -EAGAIN:
  136. ttm_eu_backoff_reservation_locked(list);
  137. /*
  138. * temporarily increase sequence number every retry,
  139. * to prevent us from seeing our old reservation
  140. * sequence when someone else reserved the buffer,
  141. * but hasn't updated the seq_valid/seqno members yet.
  142. */
  143. val_seq = entry->bo->bdev->val_seq++;
  144. spin_unlock(&glob->lru_lock);
  145. ttm_eu_list_ref_sub(list);
  146. ret = ttm_bo_reserve_slowpath_nolru(bo, true, val_seq);
  147. if (unlikely(ret != 0))
  148. return ret;
  149. spin_lock(&glob->lru_lock);
  150. entry->reserved = true;
  151. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  152. ret = -EBUSY;
  153. goto err;
  154. }
  155. goto retry;
  156. default:
  157. goto err;
  158. }
  159. entry->reserved = true;
  160. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  161. ret = -EBUSY;
  162. goto err;
  163. }
  164. }
  165. ttm_eu_del_from_lru_locked(list);
  166. spin_unlock(&glob->lru_lock);
  167. ttm_eu_list_ref_sub(list);
  168. return 0;
  169. err:
  170. ttm_eu_backoff_reservation_locked(list);
  171. spin_unlock(&glob->lru_lock);
  172. ttm_eu_list_ref_sub(list);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  176. void ttm_eu_fence_buffer_objects(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_unreserve_locked(bo);
  196. entry->reserved = false;
  197. }
  198. spin_unlock(&bdev->fence_lock);
  199. spin_unlock(&glob->lru_lock);
  200. list_for_each_entry(entry, list, head) {
  201. if (entry->old_sync_obj)
  202. driver->sync_obj_unref(&entry->old_sync_obj);
  203. }
  204. }
  205. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);