ttm_execbuf_util.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_unreserve_ticket_locked(bo, ticket);
  44. entry->removed = false;
  45. } else {
  46. atomic_set(&bo->reserved, 0);
  47. wake_up_all(&bo->event_queue);
  48. }
  49. }
  50. }
  51. static void ttm_eu_del_from_lru_locked(struct list_head *list)
  52. {
  53. struct ttm_validate_buffer *entry;
  54. list_for_each_entry(entry, list, head) {
  55. struct ttm_buffer_object *bo = entry->bo;
  56. if (!entry->reserved)
  57. continue;
  58. if (!entry->removed) {
  59. entry->put_count = ttm_bo_del_from_lru(bo);
  60. entry->removed = true;
  61. }
  62. }
  63. }
  64. static void ttm_eu_list_ref_sub(struct list_head *list)
  65. {
  66. struct ttm_validate_buffer *entry;
  67. list_for_each_entry(entry, list, head) {
  68. struct ttm_buffer_object *bo = entry->bo;
  69. if (entry->put_count) {
  70. ttm_bo_list_ref_sub(bo, entry->put_count, true);
  71. entry->put_count = 0;
  72. }
  73. }
  74. }
  75. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  76. struct list_head *list)
  77. {
  78. struct ttm_validate_buffer *entry;
  79. struct ttm_bo_global *glob;
  80. if (list_empty(list))
  81. return;
  82. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  83. glob = entry->bo->glob;
  84. spin_lock(&glob->lru_lock);
  85. ttm_eu_backoff_reservation_locked(list, ticket);
  86. ww_acquire_fini(ticket);
  87. spin_unlock(&glob->lru_lock);
  88. }
  89. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  90. /*
  91. * Reserve buffers for validation.
  92. *
  93. * If a buffer in the list is marked for CPU access, we back off and
  94. * wait for that buffer to become free for GPU access.
  95. *
  96. * If a buffer is reserved for another validation, the validator with
  97. * the highest validation sequence backs off and waits for that buffer
  98. * to become unreserved. This prevents deadlocks when validating multiple
  99. * buffers in different orders.
  100. */
  101. int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  102. struct list_head *list)
  103. {
  104. struct ttm_bo_global *glob;
  105. struct ttm_validate_buffer *entry;
  106. int ret;
  107. if (list_empty(list))
  108. return 0;
  109. list_for_each_entry(entry, list, head) {
  110. entry->reserved = false;
  111. entry->put_count = 0;
  112. entry->removed = false;
  113. }
  114. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  115. glob = entry->bo->glob;
  116. ww_acquire_init(ticket, &reservation_ww_class);
  117. spin_lock(&glob->lru_lock);
  118. retry:
  119. list_for_each_entry(entry, list, head) {
  120. struct ttm_buffer_object *bo = entry->bo;
  121. /* already slowpath reserved? */
  122. if (entry->reserved)
  123. continue;
  124. ret = ttm_bo_reserve_nolru(bo, true, true, true, ticket);
  125. switch (ret) {
  126. case 0:
  127. break;
  128. case -EBUSY:
  129. ttm_eu_del_from_lru_locked(list);
  130. spin_unlock(&glob->lru_lock);
  131. ret = ttm_bo_reserve_nolru(bo, true, false,
  132. true, ticket);
  133. spin_lock(&glob->lru_lock);
  134. if (!ret)
  135. break;
  136. if (unlikely(ret != -EAGAIN))
  137. goto err;
  138. /* fallthrough */
  139. case -EAGAIN:
  140. ttm_eu_backoff_reservation_locked(list, ticket);
  141. spin_unlock(&glob->lru_lock);
  142. ttm_eu_list_ref_sub(list);
  143. ret = ttm_bo_reserve_slowpath_nolru(bo, true, ticket);
  144. if (unlikely(ret != 0))
  145. goto err_fini;
  146. spin_lock(&glob->lru_lock);
  147. entry->reserved = true;
  148. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  149. ret = -EBUSY;
  150. goto err;
  151. }
  152. goto retry;
  153. default:
  154. goto err;
  155. }
  156. entry->reserved = true;
  157. if (unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  158. ret = -EBUSY;
  159. goto err;
  160. }
  161. }
  162. ww_acquire_done(ticket);
  163. ttm_eu_del_from_lru_locked(list);
  164. spin_unlock(&glob->lru_lock);
  165. ttm_eu_list_ref_sub(list);
  166. return 0;
  167. err:
  168. ttm_eu_backoff_reservation_locked(list, ticket);
  169. spin_unlock(&glob->lru_lock);
  170. ttm_eu_list_ref_sub(list);
  171. err_fini:
  172. ww_acquire_done(ticket);
  173. ww_acquire_fini(ticket);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  177. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  178. struct list_head *list, void *sync_obj)
  179. {
  180. struct ttm_validate_buffer *entry;
  181. struct ttm_buffer_object *bo;
  182. struct ttm_bo_global *glob;
  183. struct ttm_bo_device *bdev;
  184. struct ttm_bo_driver *driver;
  185. if (list_empty(list))
  186. return;
  187. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  188. bdev = bo->bdev;
  189. driver = bdev->driver;
  190. glob = bo->glob;
  191. spin_lock(&glob->lru_lock);
  192. spin_lock(&bdev->fence_lock);
  193. list_for_each_entry(entry, list, head) {
  194. bo = entry->bo;
  195. entry->old_sync_obj = bo->sync_obj;
  196. bo->sync_obj = driver->sync_obj_ref(sync_obj);
  197. ttm_bo_unreserve_ticket_locked(bo, ticket);
  198. entry->reserved = false;
  199. }
  200. spin_unlock(&bdev->fence_lock);
  201. spin_unlock(&glob->lru_lock);
  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);