amdgpu_sync.c 8.8 KB

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