amdgpu_sync.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. struct amdgpu_sync_entry {
  34. struct hlist_node node;
  35. struct fence *fence;
  36. };
  37. /**
  38. * amdgpu_sync_create - zero init sync object
  39. *
  40. * @sync: sync object to initialize
  41. *
  42. * Just clear the sync object for now.
  43. */
  44. void amdgpu_sync_create(struct amdgpu_sync *sync)
  45. {
  46. unsigned i;
  47. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  48. sync->semaphores[i] = NULL;
  49. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  50. sync->sync_to[i] = NULL;
  51. hash_init(sync->fences);
  52. sync->last_vm_update = NULL;
  53. }
  54. /**
  55. * amdgpu_sync_fence - remember to sync to this fence
  56. *
  57. * @sync: sync object to add fence to
  58. * @fence: fence to sync to
  59. *
  60. */
  61. int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
  62. struct fence *f)
  63. {
  64. struct amdgpu_sync_entry *e;
  65. struct amdgpu_fence *fence;
  66. struct amdgpu_fence *other;
  67. if (!f)
  68. return 0;
  69. fence = to_amdgpu_fence(f);
  70. if (!fence || fence->ring->adev != adev) {
  71. hash_for_each_possible(sync->fences, e, node, f->context) {
  72. struct fence *new;
  73. if (unlikely(e->fence->context != f->context))
  74. continue;
  75. new = fence_get(fence_later(e->fence, f));
  76. if (new) {
  77. fence_put(e->fence);
  78. e->fence = new;
  79. }
  80. return 0;
  81. }
  82. e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL);
  83. if (!e)
  84. return -ENOMEM;
  85. hash_add(sync->fences, &e->node, f->context);
  86. e->fence = fence_get(f);
  87. return 0;
  88. }
  89. other = sync->sync_to[fence->ring->idx];
  90. sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
  91. amdgpu_fence_later(fence, other));
  92. amdgpu_fence_unref(&other);
  93. if (fence->owner == AMDGPU_FENCE_OWNER_VM) {
  94. other = sync->last_vm_update;
  95. sync->last_vm_update = amdgpu_fence_ref(
  96. amdgpu_fence_later(fence, other));
  97. amdgpu_fence_unref(&other);
  98. }
  99. return 0;
  100. }
  101. /**
  102. * amdgpu_sync_resv - use the semaphores to sync to a reservation object
  103. *
  104. * @sync: sync object to add fences from reservation object to
  105. * @resv: reservation object with embedded fence
  106. * @shared: true if we should only sync to the exclusive fence
  107. *
  108. * Sync to the fence using the semaphore objects
  109. */
  110. int amdgpu_sync_resv(struct amdgpu_device *adev,
  111. struct amdgpu_sync *sync,
  112. struct reservation_object *resv,
  113. void *owner)
  114. {
  115. struct reservation_object_list *flist;
  116. struct fence *f;
  117. struct amdgpu_fence *fence;
  118. unsigned i;
  119. int r = 0;
  120. if (resv == NULL)
  121. return -EINVAL;
  122. /* always sync to the exclusive fence */
  123. f = reservation_object_get_excl(resv);
  124. r = amdgpu_sync_fence(adev, sync, f);
  125. flist = reservation_object_get_list(resv);
  126. if (!flist || r)
  127. return r;
  128. for (i = 0; i < flist->shared_count; ++i) {
  129. f = rcu_dereference_protected(flist->shared[i],
  130. reservation_object_held(resv));
  131. fence = f ? to_amdgpu_fence(f) : NULL;
  132. if (fence && fence->ring->adev == adev) {
  133. /* VM updates are only interesting
  134. * for other VM updates and moves.
  135. */
  136. if ((owner != AMDGPU_FENCE_OWNER_MOVE) &&
  137. (fence->owner != AMDGPU_FENCE_OWNER_MOVE) &&
  138. ((owner == AMDGPU_FENCE_OWNER_VM) !=
  139. (fence->owner == AMDGPU_FENCE_OWNER_VM)))
  140. continue;
  141. /* Ignore fence from the same owner as
  142. * long as it isn't undefined.
  143. */
  144. if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
  145. fence->owner == owner)
  146. continue;
  147. }
  148. r = amdgpu_sync_fence(adev, sync, f);
  149. if (r)
  150. break;
  151. }
  152. return r;
  153. }
  154. int amdgpu_sync_wait(struct amdgpu_sync *sync)
  155. {
  156. struct amdgpu_sync_entry *e;
  157. struct hlist_node *tmp;
  158. int i, r;
  159. hash_for_each_safe(sync->fences, i, tmp, e, node) {
  160. r = fence_wait(e->fence, false);
  161. if (r)
  162. return r;
  163. hash_del(&e->node);
  164. fence_put(e->fence);
  165. kfree(e);
  166. }
  167. return 0;
  168. }
  169. /**
  170. * amdgpu_sync_rings - sync ring to all registered fences
  171. *
  172. * @sync: sync object to use
  173. * @ring: ring that needs sync
  174. *
  175. * Ensure that all registered fences are signaled before letting
  176. * the ring continue. The caller must hold the ring lock.
  177. */
  178. int amdgpu_sync_rings(struct amdgpu_sync *sync,
  179. struct amdgpu_ring *ring)
  180. {
  181. struct amdgpu_device *adev = ring->adev;
  182. unsigned count = 0;
  183. int i, r;
  184. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  185. struct amdgpu_fence *fence = sync->sync_to[i];
  186. struct amdgpu_semaphore *semaphore;
  187. struct amdgpu_ring *other = adev->rings[i];
  188. /* check if we really need to sync */
  189. if (!amdgpu_fence_need_sync(fence, ring))
  190. continue;
  191. /* prevent GPU deadlocks */
  192. if (!other->ready) {
  193. dev_err(adev->dev, "Syncing to a disabled ring!");
  194. return -EINVAL;
  195. }
  196. if (amdgpu_enable_scheduler || (count >= AMDGPU_NUM_SYNCS)) {
  197. /* not enough room, wait manually */
  198. r = fence_wait(&fence->base, false);
  199. if (r)
  200. return r;
  201. continue;
  202. }
  203. r = amdgpu_semaphore_create(adev, &semaphore);
  204. if (r)
  205. return r;
  206. sync->semaphores[count++] = semaphore;
  207. /* allocate enough space for sync command */
  208. r = amdgpu_ring_alloc(other, 16);
  209. if (r)
  210. return r;
  211. /* emit the signal semaphore */
  212. if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
  213. /* signaling wasn't successful wait manually */
  214. amdgpu_ring_undo(other);
  215. r = fence_wait(&fence->base, false);
  216. if (r)
  217. return r;
  218. continue;
  219. }
  220. /* we assume caller has already allocated space on waiters ring */
  221. if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
  222. /* waiting wasn't successful wait manually */
  223. amdgpu_ring_undo(other);
  224. r = fence_wait(&fence->base, false);
  225. if (r)
  226. return r;
  227. continue;
  228. }
  229. amdgpu_ring_commit(other);
  230. amdgpu_fence_note_sync(fence, ring);
  231. }
  232. return 0;
  233. }
  234. /**
  235. * amdgpu_sync_free - free the sync object
  236. *
  237. * @adev: amdgpu_device pointer
  238. * @sync: sync object to use
  239. * @fence: fence to use for the free
  240. *
  241. * Free the sync object by freeing all semaphores in it.
  242. */
  243. void amdgpu_sync_free(struct amdgpu_device *adev,
  244. struct amdgpu_sync *sync,
  245. struct fence *fence)
  246. {
  247. struct amdgpu_sync_entry *e;
  248. struct hlist_node *tmp;
  249. unsigned i;
  250. hash_for_each_safe(sync->fences, i, tmp, e, node) {
  251. hash_del(&e->node);
  252. fence_put(e->fence);
  253. kfree(e);
  254. }
  255. for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
  256. amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
  257. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  258. amdgpu_fence_unref(&sync->sync_to[i]);
  259. amdgpu_fence_unref(&sync->last_vm_update);
  260. }