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)
51 for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
52 sync->semaphores[i] = NULL;
54 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
55 sync->sync_to[i] = NULL;
57 hash_init(sync->fences);
58 sync->last_vm_update = NULL;
61 static bool amdgpu_sync_same_dev(struct amdgpu_device *adev, struct fence *f)
63 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
64 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
67 return a_fence->ring->adev == adev;
69 return (struct amdgpu_device *)s_fence->scheduler->priv == adev;
73 static bool amdgpu_sync_test_owner(struct fence *f, void *owner)
75 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
76 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
78 return s_fence->owner == owner;
80 return a_fence->owner == owner;
85 * amdgpu_sync_fence - remember to sync to this fence
87 * @sync: sync object to add fence to
88 * @fence: fence to sync to
91 int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
94 struct amdgpu_sync_entry *e;
95 struct amdgpu_fence *fence;
96 struct amdgpu_fence *other;
97 struct fence *tmp, *later;
102 if (amdgpu_sync_same_dev(adev, f) &&
103 amdgpu_sync_test_owner(f, AMDGPU_FENCE_OWNER_VM)) {
104 if (sync->last_vm_update) {
105 tmp = sync->last_vm_update;
106 BUG_ON(f->context != tmp->context);
107 later = (f->seqno - tmp->seqno <= INT_MAX) ? f : tmp;
108 sync->last_vm_update = fence_get(later);
111 sync->last_vm_update = fence_get(f);
114 fence = to_amdgpu_fence(f);
115 if (!fence || fence->ring->adev != adev) {
116 hash_for_each_possible(sync->fences, e, node, f->context) {
118 if (unlikely(e->fence->context != f->context))
120 new = fence_get(fence_later(e->fence, f));
128 e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL);
132 hash_add(sync->fences, &e->node, f->context);
133 e->fence = fence_get(f);
137 other = sync->sync_to[fence->ring->idx];
138 sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
139 amdgpu_fence_later(fence, other));
140 amdgpu_fence_unref(&other);
146 * amdgpu_sync_resv - use the semaphores to sync to a reservation object
148 * @sync: sync object to add fences from reservation object to
149 * @resv: reservation object with embedded fence
150 * @shared: true if we should only sync to the exclusive fence
152 * Sync to the fence using the semaphore objects
154 int amdgpu_sync_resv(struct amdgpu_device *adev,
155 struct amdgpu_sync *sync,
156 struct reservation_object *resv,
159 struct reservation_object_list *flist;
161 struct amdgpu_fence *fence;
168 /* always sync to the exclusive fence */
169 f = reservation_object_get_excl(resv);
170 r = amdgpu_sync_fence(adev, sync, f);
172 flist = reservation_object_get_list(resv);
176 for (i = 0; i < flist->shared_count; ++i) {
177 f = rcu_dereference_protected(flist->shared[i],
178 reservation_object_held(resv));
179 fence = f ? to_amdgpu_fence(f) : NULL;
180 if (fence && fence->ring->adev == adev) {
181 /* VM updates are only interesting
182 * for other VM updates and moves.
184 if ((owner != AMDGPU_FENCE_OWNER_MOVE) &&
185 (fence->owner != AMDGPU_FENCE_OWNER_MOVE) &&
186 ((owner == AMDGPU_FENCE_OWNER_VM) !=
187 (fence->owner == AMDGPU_FENCE_OWNER_VM)))
190 /* Ignore fence from the same owner as
191 * long as it isn't undefined.
193 if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
194 fence->owner == owner)
198 r = amdgpu_sync_fence(adev, sync, f);
205 int amdgpu_sync_wait(struct amdgpu_sync *sync)
207 struct amdgpu_sync_entry *e;
208 struct hlist_node *tmp;
211 hash_for_each_safe(sync->fences, i, tmp, e, node) {
212 r = fence_wait(e->fence, false);
224 * amdgpu_sync_rings - sync ring to all registered fences
226 * @sync: sync object to use
227 * @ring: ring that needs sync
229 * Ensure that all registered fences are signaled before letting
230 * the ring continue. The caller must hold the ring lock.
232 int amdgpu_sync_rings(struct amdgpu_sync *sync,
233 struct amdgpu_ring *ring)
235 struct amdgpu_device *adev = ring->adev;
239 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
240 struct amdgpu_fence *fence = sync->sync_to[i];
241 struct amdgpu_semaphore *semaphore;
242 struct amdgpu_ring *other = adev->rings[i];
244 /* check if we really need to sync */
245 if (!amdgpu_fence_need_sync(fence, ring))
248 /* prevent GPU deadlocks */
250 dev_err(adev->dev, "Syncing to a disabled ring!");
254 if (amdgpu_enable_scheduler || (count >= AMDGPU_NUM_SYNCS)) {
255 /* not enough room, wait manually */
256 r = fence_wait(&fence->base, false);
261 r = amdgpu_semaphore_create(adev, &semaphore);
265 sync->semaphores[count++] = semaphore;
267 /* allocate enough space for sync command */
268 r = amdgpu_ring_alloc(other, 16);
272 /* emit the signal semaphore */
273 if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
274 /* signaling wasn't successful wait manually */
275 amdgpu_ring_undo(other);
276 r = fence_wait(&fence->base, false);
282 /* we assume caller has already allocated space on waiters ring */
283 if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
284 /* waiting wasn't successful wait manually */
285 amdgpu_ring_undo(other);
286 r = fence_wait(&fence->base, false);
292 amdgpu_ring_commit(other);
293 amdgpu_fence_note_sync(fence, ring);
300 * amdgpu_sync_free - free the sync object
302 * @adev: amdgpu_device pointer
303 * @sync: sync object to use
304 * @fence: fence to use for the free
306 * Free the sync object by freeing all semaphores in it.
308 void amdgpu_sync_free(struct amdgpu_device *adev,
309 struct amdgpu_sync *sync,
312 struct amdgpu_sync_entry *e;
313 struct hlist_node *tmp;
316 hash_for_each_safe(sync->fences, i, tmp, e, node) {
322 for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
323 amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
325 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
326 amdgpu_fence_unref(&sync->sync_to[i]);
328 fence_put(sync->last_vm_update);