ttm_execbuf_util.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* SPDX-License-Identifier: GPL-2.0 OR MIT */
  2. /**************************************************************************
  3. *
  4. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #include <linux/export.h>
  29. #include <drm/ttm/ttm_execbuf_util.h>
  30. #include <drm/ttm/ttm_bo.h>
  31. static void ttm_eu_backoff_reservation_reverse(struct list_head *list,
  32. struct ttm_validate_buffer *entry)
  33. {
  34. list_for_each_entry_continue_reverse(entry, list, head) {
  35. struct ttm_buffer_object *bo = entry->bo;
  36. dma_resv_unlock(bo->base.resv);
  37. }
  38. }
  39. void ttm_eu_backoff_reservation(struct ww_acquire_ctx *ticket,
  40. struct list_head *list)
  41. {
  42. struct ttm_validate_buffer *entry;
  43. if (list_empty(list))
  44. return;
  45. list_for_each_entry(entry, list, head) {
  46. struct ttm_buffer_object *bo = entry->bo;
  47. ttm_bo_move_to_lru_tail_unlocked(bo);
  48. dma_resv_unlock(bo->base.resv);
  49. }
  50. if (ticket)
  51. ww_acquire_fini(ticket);
  52. }
  53. EXPORT_SYMBOL(ttm_eu_backoff_reservation);
  54. /*
  55. * Reserve buffers for validation.
  56. *
  57. * If a buffer in the list is marked for CPU access, we back off and
  58. * wait for that buffer to become free for GPU access.
  59. *
  60. * If a buffer is reserved for another validation, the validator with
  61. * the highest validation sequence backs off and waits for that buffer
  62. * to become unreserved. This prevents deadlocks when validating multiple
  63. * buffers in different orders.
  64. */
  65. int ttm_eu_reserve_buffers(struct ww_acquire_ctx *ticket,
  66. struct list_head *list, bool intr,
  67. struct list_head *dups)
  68. {
  69. struct ttm_validate_buffer *entry;
  70. int ret;
  71. if (list_empty(list))
  72. return 0;
  73. if (ticket)
  74. ww_acquire_init(ticket, &reservation_ww_class);
  75. list_for_each_entry(entry, list, head) {
  76. struct ttm_buffer_object *bo = entry->bo;
  77. unsigned int num_fences;
  78. ret = ttm_bo_reserve(bo, intr, (ticket == NULL), ticket);
  79. if (ret == -EALREADY && dups) {
  80. struct ttm_validate_buffer *safe = entry;
  81. entry = list_prev_entry(entry, head);
  82. list_del(&safe->head);
  83. list_add(&safe->head, dups);
  84. continue;
  85. }
  86. num_fences = max(entry->num_shared, 1u);
  87. if (!ret) {
  88. ret = dma_resv_reserve_fences(bo->base.resv,
  89. num_fences);
  90. if (!ret)
  91. continue;
  92. }
  93. /* uh oh, we lost out, drop every reservation and try
  94. * to only reserve this buffer, then start over if
  95. * this succeeds.
  96. */
  97. ttm_eu_backoff_reservation_reverse(list, entry);
  98. if (ret == -EDEADLK) {
  99. ret = ttm_bo_reserve_slowpath(bo, intr, ticket);
  100. }
  101. if (!ret)
  102. ret = dma_resv_reserve_fences(bo->base.resv,
  103. num_fences);
  104. if (unlikely(ret != 0)) {
  105. if (ticket) {
  106. ww_acquire_done(ticket);
  107. ww_acquire_fini(ticket);
  108. }
  109. return ret;
  110. }
  111. /* move this item to the front of the list,
  112. * forces correct iteration of the loop without keeping track
  113. */
  114. list_del(&entry->head);
  115. list_add(&entry->head, list);
  116. }
  117. return 0;
  118. }
  119. EXPORT_SYMBOL(ttm_eu_reserve_buffers);
  120. void ttm_eu_fence_buffer_objects(struct ww_acquire_ctx *ticket,
  121. struct list_head *list,
  122. struct dma_fence *fence)
  123. {
  124. struct ttm_validate_buffer *entry;
  125. if (list_empty(list))
  126. return;
  127. list_for_each_entry(entry, list, head) {
  128. struct ttm_buffer_object *bo = entry->bo;
  129. dma_resv_add_fence(bo->base.resv, fence, entry->num_shared ?
  130. DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE);
  131. ttm_bo_move_to_lru_tail_unlocked(bo);
  132. dma_resv_unlock(bo->base.resv);
  133. }
  134. if (ticket)
  135. ww_acquire_fini(ticket);
  136. }
  137. EXPORT_SYMBOL(ttm_eu_fence_buffer_objects);