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. __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. unsigned put_count = ttm_bo_del_from_lru(bo);
  47. ttm_bo_list_ref_sub(bo, put_count, true);
  48. }
  49. }
  50. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  51. struct list_head *list)
  52. {
  53. struct ttm_validate_buffer *entry;
  54. struct ttm_bo_global *glob;
  55. if (list_empty(list))
  56. return;
  57. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  58. glob = entry->bo->glob;
  59. spin_lock(&glob->lru_lock);
  60. list_for_each_entry(entry, list, head) {
  61. struct ttm_buffer_object *bo = entry->bo;
  62. ttm_bo_add_to_lru(bo);
  63. __ttm_bo_unreserve(bo);
  64. }
  65. spin_unlock(&glob->lru_lock);
  66. if (ticket)
  67. ww_acquire_fini(ticket);
  68. }
  69. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  70. /*
  71. * Reserve buffers for validation.
  72. *
  73. * If a buffer in the list is marked for CPU access, we back off and
  74. * wait for that buffer to become free for GPU access.
  75. *
  76. * If a buffer is reserved for another validation, the validator with
  77. * the highest validation sequence backs off and waits for that buffer
  78. * to become unreserved. This prevents deadlocks when validating multiple
  79. * buffers in different orders.
  80. */
  81. int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  82. struct list_head *list, bool intr,
  83. struct list_head *dups)
  84. {
  85. struct ttm_bo_global *glob;
  86. struct ttm_validate_buffer *entry;
  87. int ret;
  88. if (list_empty(list))
  89. return 0;
  90. entry = list_first_entry(list, struct ttm_validate_buffer, head);
  91. glob = entry->bo->glob;
  92. if (ticket)
  93. ww_acquire_init(ticket, &reservation_ww_class);
  94. list_for_each_entry(entry, list, head) {
  95. struct ttm_buffer_object *bo = entry->bo;
  96. ret = __ttm_bo_reserve(bo, intr, (ticket == NULL), true,
  97. ticket);
  98. if (!ret && unlikely(atomic_read(&bo->cpu_writers) > 0)) {
  99. __ttm_bo_unreserve(bo);
  100. ret = -EBUSY;
  101. } else if (ret == -EALREADY && dups) {
  102. struct ttm_validate_buffer *safe = entry;
  103. entry = list_prev_entry(entry, head);
  104. list_del(&safe->head);
  105. list_add(&safe->head, dups);
  106. continue;
  107. }
  108. if (!ret) {
  109. if (!entry->shared)
  110. continue;
  111. ret = reservation_object_reserve_shared(bo->resv);
  112. if (!ret)
  113. continue;
  114. }
  115. /* uh oh, we lost out, drop every reservation and try
  116. * to only reserve this buffer, then start over if
  117. * this succeeds.
  118. */
  119. ttm_eu_backoff_reservation_reverse(list, entry);
  120. if (ret == -EDEADLK && intr) {
  121. ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
  122. ticket);
  123. } else if (ret == -EDEADLK) {
  124. ww_mutex_lock_slow(&bo->resv->lock, ticket);
  125. ret = 0;
  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, struct fence *fence)
  154. {
  155. struct ttm_validate_buffer *entry;
  156. struct ttm_buffer_object *bo;
  157. struct ttm_bo_global *glob;
  158. struct ttm_bo_device *bdev;
  159. struct ttm_bo_driver *driver;
  160. if (list_empty(list))
  161. return;
  162. bo = list_first_entry(list, struct ttm_validate_buffer, head)->bo;
  163. bdev = bo->bdev;
  164. driver = bdev->driver;
  165. glob = bo->glob;
  166. spin_lock(&glob->lru_lock);
  167. list_for_each_entry(entry, list, head) {
  168. bo = entry->bo;
  169. if (entry->shared)
  170. reservation_object_add_shared_fence(bo->resv, fence);
  171. else
  172. reservation_object_add_excl_fence(bo->resv, fence);
  173. ttm_bo_add_to_lru(bo);
  174. __ttm_bo_unreserve(bo);
  175. }
  176. spin_unlock(&glob->lru_lock);
  177. if (ticket)
  178. ww_acquire_fini(ticket);
  179. }
  180. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);