ttm_execbuf_util.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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_reverse(struct list_head *list,
  34. struct ttm_validate_buffer *entry)
  35. {
  36. list_for_each_entry_continue_reverse(entry, list, head) {
  37. struct ttm_buffer_object *bo = entry->bo;
  38. __ttm_bo_unreserve(bo);
  39. }
  40. }
  41. static void ttm_eu_del_from_lru_locked(struct list_head *list)
  42. {
  43. struct ttm_validate_buffer *entry;
  44. list_for_each_entry(entry, list, head) {
  45. struct ttm_buffer_object *bo = entry->bo;
  46. ttm_bo_del_from_lru(bo);
  47. }
  48. }
  49. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  50. struct list_head *list)
  51. {
  52. struct ttm_validate_buffer *entry;
  53. struct ttm_bo_global *glob;
  54. if (list_empty(list))
  55. return;
  56. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  57. glob = entry->bo->glob;
  58. spin_lock(&glob->lru_lock);
  59. list_for_each_entry(entry, list, head) {
  60. struct ttm_buffer_object *bo = entry->bo;
  61. ttm_bo_add_to_lru(bo);
  62. __ttm_bo_unreserve(bo);
  63. }
  64. spin_unlock(&glob->lru_lock);
  65. if (ticket)
  66. ww_acquire_fini(ticket);
  67. }
  68. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  69. /*
  70. * Reserve buffers for validation.
  71. *
  72. * If a buffer in the list is marked for CPU access, we back off and
  73. * wait for that buffer to become free for GPU access.
  74. *
  75. * If a buffer is reserved for another validation, the validator with
  76. * the highest validation sequence backs off and waits for that buffer
  77. * to become unreserved. This prevents deadlocks when validating multiple
  78. * buffers in different orders.
  79. */
  80. int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  81. struct list_head *list, bool intr,
  82. struct list_head *dups)
  83. {
  84. struct ttm_bo_global *glob;
  85. struct ttm_validate_buffer *entry;
  86. int ret;
  87. if (list_empty(list))
  88. return 0;
  89. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  90. glob = entry->bo->glob;
  91. if (ticket)
  92. ww_acquire_init(ticket, &reservation_ww_class);
  93. list_for_each_entry(entry, list, head) {
  94. struct ttm_buffer_object *bo = entry->bo;
  95. ret = __ttm_bo_reserve(bo, intr, (ticket == NULL), ticket);
  96. if (!ret && unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  97. __ttm_bo_unreserve(bo);
  98. ret = -EBUSY;
  99. } else if (ret == -EALREADY && dups) {
  100. struct ttm_validate_buffer *safe = entry;
  101. entry = list_prev_entry(entry, head);
  102. list_del(&safe->head);
  103. list_add(&safe->head, dups);
  104. continue;
  105. }
  106. if (!ret) {
  107. if (!entry->shared)
  108. continue;
  109. ret = reservation_object_reserve_shared(bo->resv);
  110. if (!ret)
  111. continue;
  112. }
  113. /* uh oh, we lost out, drop every reservation and try
  114. * to only reserve this buffer, then start over if
  115. * this succeeds.
  116. */
  117. ttm_eu_backoff_reservation_reverse(list, entry);
  118. if (ret == -EDEADLK && intr) {
  119. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  120. ticket);
  121. } else if (ret == -EDEADLK) {
  122. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  123. ret = 0;
  124. }
  125. if (!ret && entry->shared)
  126. ret = reservation_object_reserve_shared(bo->resv);
  127. if (unlikely(ret != 0)) {
  128. if (ret == -EINTR)
  129. ret = -ERESTARTSYS;
  130. if (ticket) {
  131. ww_acquire_done(ticket);
  132. ww_acquire_fini(ticket);
  133. }
  134. return ret;
  135. }
  136. /* move this item to the front of the list,
  137. * forces correct iteration of the loop without keeping track
  138. */
  139. list_del(&entry->head);
  140. list_add(&entry->head, list);
  141. }
  142. if (ticket)
  143. ww_acquire_done(ticket);
  144. spin_lock(&glob->lru_lock);
  145. ttm_eu_del_from_lru_locked(list);
  146. spin_unlock(&glob->lru_lock);
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  150. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  151. struct list_head *list,
  152. struct dma_fence *fence)
  153. {
  154. struct ttm_validate_buffer *entry;
  155. struct ttm_buffer_object *bo;
  156. struct ttm_bo_global *glob;
  157. struct ttm_bo_device *bdev;
  158. struct ttm_bo_driver *driver;
  159. if (list_empty(list))
  160. return;
  161. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  162. bdev = bo->bdev;
  163. driver = bdev->driver;
  164. glob = bo->glob;
  165. spin_lock(&glob->lru_lock);
  166. list_for_each_entry(entry, list, head) {
  167. bo = entry->bo;
  168. if (entry->shared)
  169. reservation_object_add_shared_fence(bo->resv, fence);
  170. else
  171. reservation_object_add_excl_fence(bo->resv, fence);
  172. ttm_bo_add_to_lru(bo);
  173. __ttm_bo_unreserve(bo);
  174. }
  175. spin_unlock(&glob->lru_lock);
  176. if (ticket)
  177. ww_acquire_fini(ticket);
  178. }
  179. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);