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;
70 struct amdgpu_ring *ring;
72 ring = container_of(s_fence->sched, struct amdgpu_ring, sched);
73 return ring->adev == adev;
79 static bool amdgpu_sync_test_owner(struct fence *f, void *owner)
81 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
82 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
84 return s_fence->owner == owner;
86 return a_fence->owner == owner;
91 * amdgpu_sync_fence - remember to sync to this fence
93 * @sync: sync object to add fence to
94 * @fence: fence to sync to
97 int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
100 struct amdgpu_sync_entry *e;
101 struct amdgpu_fence *fence;
102 struct amdgpu_fence *other;
103 struct fence *tmp, *later;
108 if (amdgpu_sync_same_dev(adev, f) &&
109 amdgpu_sync_test_owner(f, AMDGPU_FENCE_OWNER_VM)) {
110 if (sync->last_vm_update) {
111 tmp = sync->last_vm_update;
112 BUG_ON(f->context != tmp->context);
113 later = (f->seqno - tmp->seqno <= INT_MAX) ? f : tmp;
114 sync->last_vm_update = fence_get(later);
117 sync->last_vm_update = fence_get(f);
120 fence = to_amdgpu_fence(f);
121 if (!fence || fence->ring->adev != adev) {
122 hash_for_each_possible(sync->fences, e, node, f->context) {
124 if (unlikely(e->fence->context != f->context))
126 new = fence_get(fence_later(e->fence, f));
134 e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL);
138 hash_add(sync->fences, &e->node, f->context);
139 e->fence = fence_get(f);
143 other = sync->sync_to[fence->ring->idx];
144 sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
145 amdgpu_fence_later(fence, other));
146 amdgpu_fence_unref(&other);
151 static void *amdgpu_sync_get_owner(struct fence *f)
153 struct amdgpu_fence *a_fence = to_amdgpu_fence(f);
154 struct amd_sched_fence *s_fence = to_amd_sched_fence(f);
157 return s_fence->owner;
159 return a_fence->owner;
160 return AMDGPU_FENCE_OWNER_UNDEFINED;
164 * amdgpu_sync_resv - use the semaphores to sync to a reservation object
166 * @sync: sync object to add fences from reservation object to
167 * @resv: reservation object with embedded fence
168 * @shared: true if we should only sync to the exclusive fence
170 * Sync to the fence using the semaphore objects
172 int amdgpu_sync_resv(struct amdgpu_device *adev,
173 struct amdgpu_sync *sync,
174 struct reservation_object *resv,
177 struct reservation_object_list *flist;
186 /* always sync to the exclusive fence */
187 f = reservation_object_get_excl(resv);
188 r = amdgpu_sync_fence(adev, sync, f);
190 flist = reservation_object_get_list(resv);
194 for (i = 0; i < flist->shared_count; ++i) {
195 f = rcu_dereference_protected(flist->shared[i],
196 reservation_object_held(resv));
197 if (amdgpu_sync_same_dev(adev, f)) {
198 /* VM updates are only interesting
199 * for other VM updates and moves.
201 fence_owner = amdgpu_sync_get_owner(f);
202 if ((owner != AMDGPU_FENCE_OWNER_MOVE) &&
203 (fence_owner != AMDGPU_FENCE_OWNER_MOVE) &&
204 ((owner == AMDGPU_FENCE_OWNER_VM) !=
205 (fence_owner == AMDGPU_FENCE_OWNER_VM)))
208 /* Ignore fence from the same owner as
209 * long as it isn't undefined.
211 if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
212 fence_owner == owner)
216 r = amdgpu_sync_fence(adev, sync, f);
223 struct fence *amdgpu_sync_get_fence(struct amdgpu_sync *sync)
225 struct amdgpu_sync_entry *e;
226 struct hlist_node *tmp;
230 hash_for_each_safe(sync->fences, i, tmp, e, node) {
237 if (!fence_is_signaled(f))
245 int amdgpu_sync_wait(struct amdgpu_sync *sync)
247 struct amdgpu_sync_entry *e;
248 struct hlist_node *tmp;
251 hash_for_each_safe(sync->fences, i, tmp, e, node) {
252 r = fence_wait(e->fence, false);
261 if (amdgpu_enable_semaphores)
264 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
265 struct amdgpu_fence *fence = sync->sync_to[i];
269 r = fence_wait(&fence->base, false);
278 * amdgpu_sync_rings - sync ring to all registered fences
280 * @sync: sync object to use
281 * @ring: ring that needs sync
283 * Ensure that all registered fences are signaled before letting
284 * the ring continue. The caller must hold the ring lock.
286 int amdgpu_sync_rings(struct amdgpu_sync *sync,
287 struct amdgpu_ring *ring)
289 struct amdgpu_device *adev = ring->adev;
293 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
294 struct amdgpu_fence *fence = sync->sync_to[i];
295 struct amdgpu_semaphore *semaphore;
296 struct amdgpu_ring *other = adev->rings[i];
298 /* check if we really need to sync */
299 if (!amdgpu_fence_need_sync(fence, ring))
302 /* prevent GPU deadlocks */
304 dev_err(adev->dev, "Syncing to a disabled ring!");
308 if (amdgpu_enable_scheduler || !amdgpu_enable_semaphores ||
309 (count >= AMDGPU_NUM_SYNCS)) {
310 /* not enough room, wait manually */
311 r = fence_wait(&fence->base, false);
316 r = amdgpu_semaphore_create(adev, &semaphore);
320 sync->semaphores[count++] = semaphore;
322 /* allocate enough space for sync command */
323 r = amdgpu_ring_alloc(other, 16);
327 /* emit the signal semaphore */
328 if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
329 /* signaling wasn't successful wait manually */
330 amdgpu_ring_undo(other);
331 r = fence_wait(&fence->base, false);
337 /* we assume caller has already allocated space on waiters ring */
338 if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
339 /* waiting wasn't successful wait manually */
340 amdgpu_ring_undo(other);
341 r = fence_wait(&fence->base, false);
347 amdgpu_ring_commit(other);
348 amdgpu_fence_note_sync(fence, ring);
355 * amdgpu_sync_free - free the sync object
357 * @adev: amdgpu_device pointer
358 * @sync: sync object to use
359 * @fence: fence to use for the free
361 * Free the sync object by freeing all semaphores in it.
363 void amdgpu_sync_free(struct amdgpu_device *adev,
364 struct amdgpu_sync *sync,
367 struct amdgpu_sync_entry *e;
368 struct hlist_node *tmp;
371 hash_for_each_safe(sync->fences, i, tmp, e, node) {
377 for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
378 amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
380 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
381 amdgpu_fence_unref(&sync->sync_to[i]);
383 fence_put(sync->last_vm_update);