2 * Copyright 2014 Advanced Micro Devices, Inc.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
33 #include "amdgpu_trace.h"
35 struct amdgpu_sync_entry {
36 struct hlist_node node;
41 * amdgpu_sync_create - zero init sync object
43 * @sync: sync object to initialize
45 * Just clear the sync object for now.
47 void amdgpu_sync_create(struct amdgpu_sync *sync)
49 hash_init(sync->fences);
50 sync->last_vm_update = NULL;
53 static bool amdgpu_sync_same_dev(struct amdgpu_device *adev, struct fence *f)
55 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
56 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
59 return a_fence->ring->adev == adev;
62 struct amdgpu_ring *ring;
64 ring = container_of(s_fence->sched, struct amdgpu_ring, sched);
65 return ring->adev == adev;
71 static bool amdgpu_sync_test_owner(struct fence *f, void *owner)
73 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
74 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
76 return s_fence->owner == owner;
78 return a_fence->owner == owner;
82 static void amdgpu_sync_keep_later(struct fence **keep, struct fence *fence)
84 if (*keep && fence_is_later(*keep, fence))
88 *keep = fence_get(fence);
92 * amdgpu_sync_fence - remember to sync to this fence
94 * @sync: sync object to add fence to
95 * @fence: fence to sync to
98 int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
101 struct amdgpu_sync_entry *e;
106 if (amdgpu_sync_same_dev(adev, f) &&
107 amdgpu_sync_test_owner(f, AMDGPU_FENCE_OWNER_VM))
108 amdgpu_sync_keep_later(&sync->last_vm_update, f);
110 hash_for_each_possible(sync->fences, e, node, f->context) {
111 if (unlikely(e->fence->context != f->context))
114 amdgpu_sync_keep_later(&e->fence, f);
118 e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL);
122 hash_add(sync->fences, &e->node, f->context);
123 e->fence = fence_get(f);
127 static void *amdgpu_sync_get_owner(struct fence *f)
129 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
130 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
133 return s_fence->owner;
135 return a_fence->owner;
136 return AMDGPU_FENCE_OWNER_UNDEFINED;
140 * amdgpu_sync_resv - sync to a reservation object
142 * @sync: sync object to add fences from reservation object to
143 * @resv: reservation object with embedded fence
144 * @shared: true if we should only sync to the exclusive fence
148 int amdgpu_sync_resv(struct amdgpu_device *adev,
149 struct amdgpu_sync *sync,
150 struct reservation_object *resv,
153 struct reservation_object_list *flist;
162 /* always sync to the exclusive fence */
163 f = reservation_object_get_excl(resv);
164 r = amdgpu_sync_fence(adev, sync, f);
166 flist = reservation_object_get_list(resv);
170 for (i = 0; i < flist->shared_count; ++i) {
171 f = rcu_dereference_protected(flist->shared[i],
172 reservation_object_held(resv));
173 if (amdgpu_sync_same_dev(adev, f)) {
174 /* VM updates are only interesting
175 * for other VM updates and moves.
177 fence_owner = amdgpu_sync_get_owner(f);
178 if ((owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
179 (fence_owner != AMDGPU_FENCE_OWNER_UNDEFINED) &&
180 ((owner == AMDGPU_FENCE_OWNER_VM) !=
181 (fence_owner == AMDGPU_FENCE_OWNER_VM)))
184 /* Ignore fence from the same owner as
185 * long as it isn't undefined.
187 if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
188 fence_owner == owner)
192 r = amdgpu_sync_fence(adev, sync, f);
199 struct fence *amdgpu_sync_get_fence(struct amdgpu_sync *sync)
201 struct amdgpu_sync_entry *e;
202 struct hlist_node *tmp;
206 hash_for_each_safe(sync->fences, i, tmp, e, node) {
213 if (!fence_is_signaled(f))
221 int amdgpu_sync_wait(struct amdgpu_sync *sync)
223 struct amdgpu_sync_entry *e;
224 struct hlist_node *tmp;
227 hash_for_each_safe(sync->fences, i, tmp, e, node) {
228 r = fence_wait(e->fence, false);
241 * amdgpu_sync_free - free the sync object
243 * @sync: sync object to use
245 * Free the sync object.
247 void amdgpu_sync_free(struct amdgpu_sync *sync)
249 struct amdgpu_sync_entry *e;
250 struct hlist_node *tmp;
253 hash_for_each_safe(sync->fences, i, tmp, e, node) {
259 fence_put(sync->last_vm_update);