amdgpu_sync.c 6.6 KB

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