amdgpu_sync.c 6.2 KB

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