amdgpu_sync.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. /*
  27. * Authors:
  28. * Christian König <christian.koenig@amd.com>
  29. */
  30. #include <drm/drmP.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_trace.h"
  33. /**
  34. * amdgpu_sync_create - zero init sync object
  35. *
  36. * @sync: sync object to initialize
  37. *
  38. * Just clear the sync object for now.
  39. */
  40. void amdgpu_sync_create(struct amdgpu_sync *sync)
  41. {
  42. unsigned i;
  43. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  44. sync->semaphores[i] = NULL;
  45. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  46. sync->sync_to[i] = NULL;
  47. sync->last_vm_update = NULL;
  48. }
  49. /**
  50. * amdgpu_sync_fence - remember to sync to this fence
  51. *
  52. * @sync: sync object to add fence to
  53. * @fence: fence to sync to
  54. *
  55. */
  56. int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
  57. struct fence *f)
  58. {
  59. struct amdgpu_fence *fence;
  60. struct amdgpu_fence *other;
  61. if (!f)
  62. return 0;
  63. fence = to_amdgpu_fence(f);
  64. if (!fence || fence->ring->adev != adev)
  65. return fence_wait(f, true);
  66. other = sync->sync_to[fence->ring->idx];
  67. sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
  68. amdgpu_fence_later(fence, other));
  69. amdgpu_fence_unref(&other);
  70. if (fence->owner == AMDGPU_FENCE_OWNER_VM) {
  71. other = sync->last_vm_update;
  72. sync->last_vm_update = amdgpu_fence_ref(
  73. amdgpu_fence_later(fence, other));
  74. amdgpu_fence_unref(&other);
  75. }
  76. return 0;
  77. }
  78. /**
  79. * amdgpu_sync_resv - use the semaphores to sync to a reservation object
  80. *
  81. * @sync: sync object to add fences from reservation object to
  82. * @resv: reservation object with embedded fence
  83. * @shared: true if we should only sync to the exclusive fence
  84. *
  85. * Sync to the fence using the semaphore objects
  86. */
  87. int amdgpu_sync_resv(struct amdgpu_device *adev,
  88. struct amdgpu_sync *sync,
  89. struct reservation_object *resv,
  90. void *owner)
  91. {
  92. struct reservation_object_list *flist;
  93. struct fence *f;
  94. struct amdgpu_fence *fence;
  95. unsigned i;
  96. int r = 0;
  97. if (resv == NULL)
  98. return -EINVAL;
  99. /* always sync to the exclusive fence */
  100. f = reservation_object_get_excl(resv);
  101. r = amdgpu_sync_fence(adev, sync, f);
  102. flist = reservation_object_get_list(resv);
  103. if (!flist || r)
  104. return r;
  105. for (i = 0; i < flist->shared_count; ++i) {
  106. f = rcu_dereference_protected(flist->shared[i],
  107. reservation_object_held(resv));
  108. fence = f ? to_amdgpu_fence(f) : NULL;
  109. if (fence && fence->ring->adev == adev) {
  110. /* VM updates are only interesting
  111. * for other VM updates and moves.
  112. */
  113. if ((owner != AMDGPU_FENCE_OWNER_MOVE) &&
  114. (fence->owner != AMDGPU_FENCE_OWNER_MOVE) &&
  115. ((owner == AMDGPU_FENCE_OWNER_VM) !=
  116. (fence->owner == AMDGPU_FENCE_OWNER_VM)))
  117. continue;
  118. /* Ignore fence from the same owner as
  119. * long as it isn't undefined.
  120. */
  121. if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
  122. fence->owner == owner)
  123. continue;
  124. }
  125. r = amdgpu_sync_fence(adev, sync, f);
  126. if (r)
  127. break;
  128. }
  129. return r;
  130. }
  131. /**
  132. * amdgpu_sync_rings - sync ring to all registered fences
  133. *
  134. * @sync: sync object to use
  135. * @ring: ring that needs sync
  136. *
  137. * Ensure that all registered fences are signaled before letting
  138. * the ring continue. The caller must hold the ring lock.
  139. */
  140. int amdgpu_sync_rings(struct amdgpu_sync *sync,
  141. struct amdgpu_ring *ring)
  142. {
  143. struct amdgpu_device *adev = ring->adev;
  144. unsigned count = 0;
  145. int i, r;
  146. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  147. struct amdgpu_fence *fence = sync->sync_to[i];
  148. struct amdgpu_semaphore *semaphore;
  149. struct amdgpu_ring *other = adev->rings[i];
  150. /* check if we really need to sync */
  151. if (!amdgpu_fence_need_sync(fence, ring))
  152. continue;
  153. /* prevent GPU deadlocks */
  154. if (!other->ready) {
  155. dev_err(adev->dev, "Syncing to a disabled ring!");
  156. return -EINVAL;
  157. }
  158. if (amdgpu_enable_scheduler || (count >= AMDGPU_NUM_SYNCS)) {
  159. /* not enough room, wait manually */
  160. r = amdgpu_fence_wait(fence, false);
  161. if (r)
  162. return r;
  163. continue;
  164. }
  165. r = amdgpu_semaphore_create(adev, &semaphore);
  166. if (r)
  167. return r;
  168. sync->semaphores[count++] = semaphore;
  169. /* allocate enough space for sync command */
  170. r = amdgpu_ring_alloc(other, 16);
  171. if (r)
  172. return r;
  173. /* emit the signal semaphore */
  174. if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
  175. /* signaling wasn't successful wait manually */
  176. amdgpu_ring_undo(other);
  177. r = amdgpu_fence_wait(fence, false);
  178. if (r)
  179. return r;
  180. continue;
  181. }
  182. /* we assume caller has already allocated space on waiters ring */
  183. if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
  184. /* waiting wasn't successful wait manually */
  185. amdgpu_ring_undo(other);
  186. r = amdgpu_fence_wait(fence, false);
  187. if (r)
  188. return r;
  189. continue;
  190. }
  191. amdgpu_ring_commit(other);
  192. amdgpu_fence_note_sync(fence, ring);
  193. }
  194. return 0;
  195. }
  196. /**
  197. * amdgpu_sync_free - free the sync object
  198. *
  199. * @adev: amdgpu_device pointer
  200. * @sync: sync object to use
  201. * @fence: fence to use for the free
  202. *
  203. * Free the sync object by freeing all semaphores in it.
  204. */
  205. void amdgpu_sync_free(struct amdgpu_device *adev,
  206. struct amdgpu_sync *sync,
  207. struct amdgpu_fence *fence)
  208. {
  209. unsigned i;
  210. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  211. amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
  212. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  213. amdgpu_fence_unref(&sync->sync_to[i]);
  214. amdgpu_fence_unref(&sync->last_vm_update);
  215. }