amdgpu_sync.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 - use the semaphore to sync to a fence
  51. *
  52. * @sync: sync object to add fence to
  53. * @fence: fence to sync to
  54. *
  55. * Sync to the fence using the semaphore objects
  56. */
  57. void amdgpu_sync_fence(struct amdgpu_sync *sync,
  58. struct amdgpu_fence *fence)
  59. {
  60. struct amdgpu_fence *other;
  61. if (!fence)
  62. return;
  63. other = sync->sync_to[fence->ring->idx];
  64. sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
  65. amdgpu_fence_later(fence, other));
  66. amdgpu_fence_unref(&other);
  67. if (fence->owner == AMDGPU_FENCE_OWNER_VM) {
  68. other = sync->last_vm_update;
  69. sync->last_vm_update = amdgpu_fence_ref(
  70. amdgpu_fence_later(fence, other));
  71. amdgpu_fence_unref(&other);
  72. }
  73. }
  74. /**
  75. * amdgpu_sync_resv - use the semaphores to sync to a reservation object
  76. *
  77. * @sync: sync object to add fences from reservation object to
  78. * @resv: reservation object with embedded fence
  79. * @shared: true if we should only sync to the exclusive fence
  80. *
  81. * Sync to the fence using the semaphore objects
  82. */
  83. int amdgpu_sync_resv(struct amdgpu_device *adev,
  84. struct amdgpu_sync *sync,
  85. struct reservation_object *resv,
  86. void *owner)
  87. {
  88. struct reservation_object_list *flist;
  89. struct fence *f;
  90. struct amdgpu_fence *fence;
  91. unsigned i;
  92. int r = 0;
  93. if (resv == NULL)
  94. return -EINVAL;
  95. /* always sync to the exclusive fence */
  96. f = reservation_object_get_excl(resv);
  97. fence = f ? to_amdgpu_fence(f) : NULL;
  98. if (fence && fence->ring->adev == adev)
  99. amdgpu_sync_fence(sync, fence);
  100. else if (f)
  101. r = fence_wait(f, true);
  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. if (fence->owner != owner ||
  111. fence->owner == AMDGPU_FENCE_OWNER_UNDEFINED)
  112. amdgpu_sync_fence(sync, fence);
  113. } else if (f) {
  114. r = fence_wait(f, true);
  115. if (r)
  116. break;
  117. }
  118. }
  119. return r;
  120. }
  121. /**
  122. * amdgpu_sync_rings - sync ring to all registered fences
  123. *
  124. * @sync: sync object to use
  125. * @ring: ring that needs sync
  126. *
  127. * Ensure that all registered fences are signaled before letting
  128. * the ring continue. The caller must hold the ring lock.
  129. */
  130. int amdgpu_sync_rings(struct amdgpu_sync *sync,
  131. struct amdgpu_ring *ring)
  132. {
  133. struct amdgpu_device *adev = ring->adev;
  134. unsigned count = 0;
  135. int i, r;
  136. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  137. struct amdgpu_fence *fence = sync->sync_to[i];
  138. struct amdgpu_semaphore *semaphore;
  139. struct amdgpu_ring *other = adev->rings[i];
  140. /* check if we really need to sync */
  141. if (!amdgpu_fence_need_sync(fence, ring))
  142. continue;
  143. /* prevent GPU deadlocks */
  144. if (!other->ready) {
  145. dev_err(adev->dev, "Syncing to a disabled ring!");
  146. return -EINVAL;
  147. }
  148. if (count >= AMDGPU_NUM_SYNCS) {
  149. /* not enough room, wait manually */
  150. r = amdgpu_fence_wait(fence, false);
  151. if (r)
  152. return r;
  153. continue;
  154. }
  155. r = amdgpu_semaphore_create(adev, &semaphore);
  156. if (r)
  157. return r;
  158. sync->semaphores[count++] = semaphore;
  159. /* allocate enough space for sync command */
  160. r = amdgpu_ring_alloc(other, 16);
  161. if (r)
  162. return r;
  163. /* emit the signal semaphore */
  164. if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
  165. /* signaling wasn't successful wait manually */
  166. amdgpu_ring_undo(other);
  167. r = amdgpu_fence_wait(fence, false);
  168. if (r)
  169. return r;
  170. continue;
  171. }
  172. /* we assume caller has already allocated space on waiters ring */
  173. if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
  174. /* waiting wasn't successful wait manually */
  175. amdgpu_ring_undo(other);
  176. r = amdgpu_fence_wait(fence, false);
  177. if (r)
  178. return r;
  179. continue;
  180. }
  181. amdgpu_ring_commit(other);
  182. amdgpu_fence_note_sync(fence, ring);
  183. }
  184. return 0;
  185. }
  186. /**
  187. * amdgpu_sync_free - free the sync object
  188. *
  189. * @adev: amdgpu_device pointer
  190. * @sync: sync object to use
  191. * @fence: fence to use for the free
  192. *
  193. * Free the sync object by freeing all semaphores in it.
  194. */
  195. void amdgpu_sync_free(struct amdgpu_device *adev,
  196. struct amdgpu_sync *sync,
  197. struct amdgpu_fence *fence)
  198. {
  199. unsigned i;
  200. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  201. amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
  202. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  203. amdgpu_fence_unref(&sync->sync_to[i]);
  204. amdgpu_fence_unref(&sync->last_vm_update);
  205. }