ttm_execbuf_util.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. reservation_object_unlock(bo->resv);
  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->bdev->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. reservation_object_unlock(bo->resv);
  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->bdev->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. reservation_object_unlock(bo->resv);
  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) {
  119. if (intr) {
  120. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  121. ticket);
  122. } else {
  123. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  124. ret = 0;
  125. }
  126. }
  127. if (!ret && entry->shared)
  128. ret = reservation_object_reserve_shared(bo->resv);
  129. if (unlikely(ret != 0)) {
  130. if (ret == -EINTR)
  131. ret = -ERESTARTSYS;
  132. if (ticket) {
  133. ww_acquire_done(ticket);
  134. ww_acquire_fini(ticket);
  135. }
  136. return ret;
  137. }
  138. /* move this item to the front of the list,
  139. * forces correct iteration of the loop without keeping track
  140. */
  141. list_del(&entry->head);
  142. list_add(&entry->head, list);
  143. }
  144. if (ticket)
  145. ww_acquire_done(ticket);
  146. spin_lock(&glob->lru_lock);
  147. ttm_eu_del_from_lru_locked(list);
  148. spin_unlock(&glob->lru_lock);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  152. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  153. struct list_head *list,
  154. struct dma_fence *fence)
  155. {
  156. struct ttm_validate_buffer *entry;
  157. struct ttm_buffer_object *bo;
  158. struct ttm_bo_global *glob;
  159. struct ttm_bo_device *bdev;
  160. struct ttm_bo_driver *driver;
  161. if (list_empty(list))
  162. return;
  163. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  164. bdev = bo->bdev;
  165. driver = bdev->driver;
  166. glob = bo->bdev->glob;
  167. spin_lock(&glob->lru_lock);
  168. list_for_each_entry(entry, list, head) {
  169. bo = entry->bo;
  170. if (entry->shared)
  171. reservation_object_add_shared_fence(bo->resv, fence);
  172. else
  173. reservation_object_add_excl_fence(bo->resv, fence);
  174. ttm_bo_add_to_lru(bo);
  175. reservation_object_unlock(bo->resv);
  176. }
  177. spin_unlock(&glob->lru_lock);
  178. if (ticket)
  179. ww_acquire_fini(ticket);
  180. }
  181. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);