amdgpu_sync.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. fence->owner == owner &&
  111. fence->owner != AMDGPU_FENCE_OWNER_UNDEFINED)
  112. continue;
  113. r = amdgpu_sync_fence(adev, sync, f);
  114. if (r)
  115. break;
  116. }
  117. return r;
  118. }
  119. /**
  120. * amdgpu_sync_rings - sync ring to all registered fences
  121. *
  122. * @sync: sync object to use
  123. * @ring: ring that needs sync
  124. *
  125. * Ensure that all registered fences are signaled before letting
  126. * the ring continue. The caller must hold the ring lock.
  127. */
  128. int amdgpu_sync_rings(struct amdgpu_sync *sync,
  129. struct amdgpu_ring *ring)
  130. {
  131. struct amdgpu_device *adev = ring->adev;
  132. unsigned count = 0;
  133. int i, r;
  134. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  135. struct amdgpu_fence *fence = sync->sync_to[i];
  136. struct amdgpu_semaphore *semaphore;
  137. struct amdgpu_ring *other = adev->rings[i];
  138. /* check if we really need to sync */
  139. if (!amdgpu_fence_need_sync(fence, ring))
  140. continue;
  141. /* prevent GPU deadlocks */
  142. if (!other->ready) {
  143. dev_err(adev->dev, "Syncing to a disabled ring!");
  144. return -EINVAL;
  145. }
  146. if (count >= AMDGPU_NUM_SYNCS) {
  147. /* not enough room, wait manually */
  148. r = amdgpu_fence_wait(fence, false);
  149. if (r)
  150. return r;
  151. continue;
  152. }
  153. r = amdgpu_semaphore_create(adev, &semaphore);
  154. if (r)
  155. return r;
  156. sync->semaphores[count++] = semaphore;
  157. /* allocate enough space for sync command */
  158. r = amdgpu_ring_alloc(other, 16);
  159. if (r)
  160. return r;
  161. /* emit the signal semaphore */
  162. if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
  163. /* signaling wasn't successful wait manually */
  164. amdgpu_ring_undo(other);
  165. r = amdgpu_fence_wait(fence, false);
  166. if (r)
  167. return r;
  168. continue;
  169. }
  170. /* we assume caller has already allocated space on waiters ring */
  171. if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
  172. /* waiting wasn't successful wait manually */
  173. amdgpu_ring_undo(other);
  174. r = amdgpu_fence_wait(fence, false);
  175. if (r)
  176. return r;
  177. continue;
  178. }
  179. amdgpu_ring_commit(other);
  180. amdgpu_fence_note_sync(fence, ring);
  181. }
  182. return 0;
  183. }
  184. /**
  185. * amdgpu_sync_free - free the sync object
  186. *
  187. * @adev: amdgpu_device pointer
  188. * @sync: sync object to use
  189. * @fence: fence to use for the free
  190. *
  191. * Free the sync object by freeing all semaphores in it.
  192. */
  193. void amdgpu_sync_free(struct amdgpu_device *adev,
  194. struct amdgpu_sync *sync,
  195. struct amdgpu_fence *fence)
  196. {
  197. unsigned i;
  198. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  199. amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
  200. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  201. amdgpu_fence_unref(&sync->sync_to[i]);
  202. amdgpu_fence_unref(&sync->last_vm_update);
  203. }