amdgpu_sync.c 6.2 KB

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