amdgpu_sync.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_MAX_RINGS; ++i)
  48. sync->sync_to[i] = NULL;
  49. hash_init(sync->fences);
  50. sync->last_vm_update = NULL;
  51. }
  52. static bool amdgpu_sync_same_dev(struct amdgpu_device *adev, struct fence *f)
  53. {
  54. struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
  55. struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
  56. if (a_fence)
  57. return a_fence->ring->adev == adev;
  58. if (s_fence) {
  59. struct amdgpu_ring *ring;
  60. ring = container_of(s_fence->sched, struct amdgpu_ring, sched);
  61. return ring->adev == adev;
  62. }
  63. return false;
  64. }
  65. static bool amdgpu_sync_test_owner(struct fence *f, void *owner)
  66. {
  67. struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
  68. struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
  69. if (s_fence)
  70. return s_fence->owner == owner;
  71. if (a_fence)
  72. return a_fence->owner == owner;
  73. return false;
  74. }
  75. static void amdgpu_sync_keep_later(struct fence **keep, struct fence *fence)
  76. {
  77. if (*keep && fence_is_later(*keep, fence))
  78. return;
  79. fence_put(*keep);
  80. *keep = fence_get(fence);
  81. }
  82. /**
  83. * amdgpu_sync_fence - remember to sync to this fence
  84. *
  85. * @sync: sync object to add fence to
  86. * @fence: fence to sync to
  87. *
  88. */
  89. int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
  90. struct fence *f)
  91. {
  92. struct amdgpu_sync_entry *e;
  93. struct amdgpu_fence *fence;
  94. if (!f)
  95. return 0;
  96. if (amdgpu_sync_same_dev(adev, f) &&
  97. amdgpu_sync_test_owner(f, AMDGPU_FENCE_OWNER_VM))
  98. amdgpu_sync_keep_later(&sync->last_vm_update, f);
  99. fence = to_amdgpu_fence(f);
  100. if (!fence || fence->ring->adev != adev) {
  101. hash_for_each_possible(sync->fences, e, node, f->context) {
  102. if (unlikely(e->fence->context != f->context))
  103. continue;
  104. amdgpu_sync_keep_later(&e->fence, f);
  105. return 0;
  106. }
  107. e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL);
  108. if (!e)
  109. return -ENOMEM;
  110. hash_add(sync->fences, &e->node, f->context);
  111. e->fence = fence_get(f);
  112. return 0;
  113. }
  114. amdgpu_sync_keep_later(&sync->sync_to[fence->ring->idx], f);
  115. return 0;
  116. }
  117. static void *amdgpu_sync_get_owner(struct fence *f)
  118. {
  119. struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
  120. struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
  121. if (s_fence)
  122. return s_fence->owner;
  123. else if (a_fence)
  124. return a_fence->owner;
  125. return AMDGPU_FENCE_OWNER_UNDEFINED;
  126. }
  127. /**
  128. * amdgpu_sync_resv - sync to a reservation object
  129. *
  130. * @sync: sync object to add fences from reservation object to
  131. * @resv: reservation object with embedded fence
  132. * @shared: true if we should only sync to the exclusive fence
  133. *
  134. * Sync to the fence
  135. */
  136. int amdgpu_sync_resv(struct amdgpu_device *adev,
  137. struct amdgpu_sync *sync,
  138. struct reservation_object *resv,
  139. void *owner)
  140. {
  141. struct reservation_object_list *flist;
  142. struct fence *f;
  143. void *fence_owner;
  144. unsigned i;
  145. int r = 0;
  146. if (resv == NULL)
  147. return -EINVAL;
  148. /* always sync to the exclusive fence */
  149. f = reservation_object_get_excl(resv);
  150. r = amdgpu_sync_fence(adev, sync, f);
  151. flist = reservation_object_get_list(resv);
  152. if (!flist || r)
  153. return r;
  154. for (i = 0; i < flist->shared_count; ++i) {
  155. f = rcu_dereference_protected(flist->shared[i],
  156. reservation_object_held(resv));
  157. if (amdgpu_sync_same_dev(adev, f)) {
  158. /* VM updates are only interesting
  159. * for other VM updates and moves.
  160. */
  161. fence_owner = amdgpu_sync_get_owner(f);
  162. if ((owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
  163. (fence_owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
  164. ((owner == AMDGPU_FENCE_OWNER_VM) !=
  165. (fence_owner == AMDGPU_FENCE_OWNER_VM)))
  166. continue;
  167. /* Ignore fence from the same owner as
  168. * long as it isn't undefined.
  169. */
  170. if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
  171. fence_owner == owner)
  172. continue;
  173. }
  174. r = amdgpu_sync_fence(adev, sync, f);
  175. if (r)
  176. break;
  177. }
  178. return r;
  179. }
  180. struct fence *amdgpu_sync_get_fence(struct amdgpu_sync *sync)
  181. {
  182. struct amdgpu_sync_entry *e;
  183. struct hlist_node *tmp;
  184. struct fence *f;
  185. int i;
  186. hash_for_each_safe(sync->fences, i, tmp, e, node) {
  187. f = e->fence;
  188. hash_del(&e->node);
  189. kfree(e);
  190. if (!fence_is_signaled(f))
  191. return f;
  192. fence_put(f);
  193. }
  194. return NULL;
  195. }
  196. int amdgpu_sync_wait(struct amdgpu_sync *sync)
  197. {
  198. struct amdgpu_sync_entry *e;
  199. struct hlist_node *tmp;
  200. int i, r;
  201. hash_for_each_safe(sync->fences, i, tmp, e, node) {
  202. r = fence_wait(e->fence, false);
  203. if (r)
  204. return r;
  205. hash_del(&e->node);
  206. fence_put(e->fence);
  207. kfree(e);
  208. }
  209. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  210. struct fence *fence = sync->sync_to[i];
  211. if (!fence)
  212. continue;
  213. r = fence_wait(fence, false);
  214. if (r)
  215. return r;
  216. }
  217. return 0;
  218. }
  219. /**
  220. * amdgpu_sync_free - free the sync object
  221. *
  222. * @adev: amdgpu_device pointer
  223. * @sync: sync object to use
  224. * @fence: fence to use for the free
  225. *
  226. * Free the sync object.
  227. */
  228. void amdgpu_sync_free(struct amdgpu_device *adev,
  229. struct amdgpu_sync *sync,
  230. struct fence *fence)
  231. {
  232. struct amdgpu_sync_entry *e;
  233. struct hlist_node *tmp;
  234. unsigned i;
  235. hash_for_each_safe(sync->fences, i, tmp, e, node) {
  236. hash_del(&e->node);
  237. fence_put(e->fence);
  238. kfree(e);
  239. }
  240. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  241. fence_put(sync->sync_to[i]);
  242. fence_put(sync->last_vm_update);
  243. }