blob: 2c42f50912b98f7f3305528888f9eb248641b04b [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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
31#include <drm/drmP.h>
32#include "amdgpu.h"
33#include "amdgpu_trace.h"
34
35/**
36 * amdgpu_sync_create - zero init sync object
37 *
38 * @sync: sync object to initialize
39 *
40 * Just clear the sync object for now.
41 */
42void amdgpu_sync_create(struct amdgpu_sync *sync)
43{
44 unsigned i;
45
46 for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
47 sync->semaphores[i] = NULL;
48
49 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
50 sync->sync_to[i] = NULL;
51
52 sync->last_vm_update = NULL;
53}
54
55/**
Christian König91e1a522015-07-06 22:06:40 +020056 * amdgpu_sync_fence - remember to sync to this fence
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057 *
58 * @sync: sync object to add fence to
59 * @fence: fence to sync to
60 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -040061 */
Christian König91e1a522015-07-06 22:06:40 +020062int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync,
63 struct fence *f)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064{
Christian König91e1a522015-07-06 22:06:40 +020065 struct amdgpu_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066 struct amdgpu_fence *other;
67
Christian König91e1a522015-07-06 22:06:40 +020068 if (!f)
69 return 0;
70
71 fence = to_amdgpu_fence(f);
72 if (!fence || fence->ring->adev != adev)
73 return fence_wait(f, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074
75 other = sync->sync_to[fence->ring->idx];
76 sync->sync_to[fence->ring->idx] = amdgpu_fence_ref(
77 amdgpu_fence_later(fence, other));
78 amdgpu_fence_unref(&other);
79
80 if (fence->owner == AMDGPU_FENCE_OWNER_VM) {
81 other = sync->last_vm_update;
82 sync->last_vm_update = amdgpu_fence_ref(
83 amdgpu_fence_later(fence, other));
84 amdgpu_fence_unref(&other);
85 }
Christian König91e1a522015-07-06 22:06:40 +020086
87 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088}
89
90/**
91 * amdgpu_sync_resv - use the semaphores to sync to a reservation object
92 *
93 * @sync: sync object to add fences from reservation object to
94 * @resv: reservation object with embedded fence
95 * @shared: true if we should only sync to the exclusive fence
96 *
97 * Sync to the fence using the semaphore objects
98 */
99int amdgpu_sync_resv(struct amdgpu_device *adev,
100 struct amdgpu_sync *sync,
101 struct reservation_object *resv,
102 void *owner)
103{
104 struct reservation_object_list *flist;
105 struct fence *f;
106 struct amdgpu_fence *fence;
107 unsigned i;
108 int r = 0;
109
Jammy Zhou4b095302015-05-12 23:17:19 +0800110 if (resv == NULL)
111 return -EINVAL;
112
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 /* always sync to the exclusive fence */
114 f = reservation_object_get_excl(resv);
Christian König91e1a522015-07-06 22:06:40 +0200115 r = amdgpu_sync_fence(adev, sync, f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116
117 flist = reservation_object_get_list(resv);
118 if (!flist || r)
119 return r;
120
121 for (i = 0; i < flist->shared_count; ++i) {
122 f = rcu_dereference_protected(flist->shared[i],
123 reservation_object_held(resv));
Jammy Zhou4b095302015-05-12 23:17:19 +0800124 fence = f ? to_amdgpu_fence(f) : NULL;
Christian König1d3897e2015-07-27 15:40:35 +0200125 if (fence && fence->ring->adev == adev) {
126 /* VM updates are only interesting
127 * for other VM updates and moves.
128 */
129 if ((owner != AMDGPU_FENCE_OWNER_MOVE) &&
130 (fence->owner != AMDGPU_FENCE_OWNER_MOVE) &&
131 ((owner == AMDGPU_FENCE_OWNER_VM) !=
132 (fence->owner == AMDGPU_FENCE_OWNER_VM)))
Christian König91e1a522015-07-06 22:06:40 +0200133 continue;
134
Christian König1d3897e2015-07-27 15:40:35 +0200135 /* Ignore fence from the same owner as
136 * long as it isn't undefined.
137 */
138 if (owner != AMDGPU_FENCE_OWNER_UNDEFINED &&
139 fence->owner == owner)
140 continue;
141 }
142
Christian König91e1a522015-07-06 22:06:40 +0200143 r = amdgpu_sync_fence(adev, sync, f);
144 if (r)
145 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 }
147 return r;
148}
149
150/**
151 * amdgpu_sync_rings - sync ring to all registered fences
152 *
153 * @sync: sync object to use
154 * @ring: ring that needs sync
155 *
156 * Ensure that all registered fences are signaled before letting
157 * the ring continue. The caller must hold the ring lock.
158 */
159int amdgpu_sync_rings(struct amdgpu_sync *sync,
160 struct amdgpu_ring *ring)
161{
162 struct amdgpu_device *adev = ring->adev;
163 unsigned count = 0;
164 int i, r;
165
166 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
167 struct amdgpu_fence *fence = sync->sync_to[i];
168 struct amdgpu_semaphore *semaphore;
169 struct amdgpu_ring *other = adev->rings[i];
170
171 /* check if we really need to sync */
172 if (!amdgpu_fence_need_sync(fence, ring))
173 continue;
174
175 /* prevent GPU deadlocks */
176 if (!other->ready) {
177 dev_err(adev->dev, "Syncing to a disabled ring!");
178 return -EINVAL;
179 }
180
Chunming Zhou2c4888a2015-07-21 13:19:45 +0800181 if (amdgpu_enable_scheduler || (count >= AMDGPU_NUM_SYNCS)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182 /* not enough room, wait manually */
183 r = amdgpu_fence_wait(fence, false);
184 if (r)
185 return r;
186 continue;
187 }
188 r = amdgpu_semaphore_create(adev, &semaphore);
189 if (r)
190 return r;
191
192 sync->semaphores[count++] = semaphore;
193
194 /* allocate enough space for sync command */
195 r = amdgpu_ring_alloc(other, 16);
196 if (r)
197 return r;
198
199 /* emit the signal semaphore */
200 if (!amdgpu_semaphore_emit_signal(other, semaphore)) {
201 /* signaling wasn't successful wait manually */
202 amdgpu_ring_undo(other);
203 r = amdgpu_fence_wait(fence, false);
204 if (r)
205 return r;
206 continue;
207 }
208
209 /* we assume caller has already allocated space on waiters ring */
210 if (!amdgpu_semaphore_emit_wait(ring, semaphore)) {
211 /* waiting wasn't successful wait manually */
212 amdgpu_ring_undo(other);
213 r = amdgpu_fence_wait(fence, false);
214 if (r)
215 return r;
216 continue;
217 }
218
219 amdgpu_ring_commit(other);
220 amdgpu_fence_note_sync(fence, ring);
221 }
222
223 return 0;
224}
225
226/**
227 * amdgpu_sync_free - free the sync object
228 *
229 * @adev: amdgpu_device pointer
230 * @sync: sync object to use
231 * @fence: fence to use for the free
232 *
233 * Free the sync object by freeing all semaphores in it.
234 */
235void amdgpu_sync_free(struct amdgpu_device *adev,
236 struct amdgpu_sync *sync,
237 struct amdgpu_fence *fence)
238{
239 unsigned i;
240
241 for (i = 0; i < AMDGPU_NUM_SYNCS; ++i)
242 amdgpu_semaphore_free(adev, &sync->semaphores[i], fence);
243
244 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
245 amdgpu_fence_unref(&sync->sync_to[i]);
246
247 amdgpu_fence_unref(&sync->last_vm_update);
248}