blob: 6deb08f045b758a9cbaefec886177b86c950a567 [file] [log] [blame]
Christian König15d33322011-09-15 19:02:22 +02001/*
2 * Copyright 2011 Christian König.
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 <deathsimple@vodafone.de>
29 */
David Howells760285e2012-10-02 18:01:07 +010030#include <drm/drmP.h>
Christian König15d33322011-09-15 19:02:22 +020031#include "radeon.h"
Christian Königbd80c8b2013-11-12 12:58:04 +010032#include "radeon_trace.h"
Jerome Glissec1341e52011-12-21 12:13:47 -050033
Christian König15d33322011-09-15 19:02:22 +020034int radeon_semaphore_create(struct radeon_device *rdev,
35 struct radeon_semaphore **semaphore)
36{
Christian Königf2294072014-09-07 12:06:52 +020037 uint64_t *cpu_addr;
Christian König1654b812013-11-12 12:58:05 +010038 int i, r;
Christian König15d33322011-09-15 19:02:22 +020039
Jerome Glissea8c05942012-05-09 15:34:57 +020040 *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL);
Jerome Glissec1341e52011-12-21 12:13:47 -050041 if (*semaphore == NULL) {
Jerome Glissec1341e52011-12-21 12:13:47 -050042 return -ENOMEM;
Christian König15d33322011-09-15 19:02:22 +020043 }
Christian König8f534922014-02-18 11:37:20 +010044 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*semaphore)->sa_bo,
Christian König4d152642014-02-20 21:48:00 +010045 8 * RADEON_NUM_SYNCS, 8);
Jerome Glissea8c05942012-05-09 15:34:57 +020046 if (r) {
47 kfree(*semaphore);
48 *semaphore = NULL;
49 return r;
50 }
51 (*semaphore)->waiters = 0;
52 (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo);
Christian König8f534922014-02-18 11:37:20 +010053
54 cpu_addr = radeon_sa_bo_cpu_addr((*semaphore)->sa_bo);
55 for (i = 0; i < RADEON_NUM_SYNCS; ++i)
56 cpu_addr[i] = 0;
Christian König1654b812013-11-12 12:58:05 +010057
58 for (i = 0; i < RADEON_NUM_RINGS; ++i)
59 (*semaphore)->sync_to[i] = NULL;
60
Christian König15d33322011-09-15 19:02:22 +020061 return 0;
62}
63
Christian König1654b812013-11-12 12:58:05 +010064bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx,
Christian König15d33322011-09-15 19:02:22 +020065 struct radeon_semaphore *semaphore)
66{
Christian König1654b812013-11-12 12:58:05 +010067 struct radeon_ring *ring = &rdev->ring[ridx];
Christian Königbd80c8b2013-11-12 12:58:04 +010068
Christian König1654b812013-11-12 12:58:05 +010069 trace_radeon_semaphore_signale(ridx, semaphore);
70
71 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
72 --semaphore->waiters;
73
74 /* for debugging lockup only, used by sysfs debug files */
75 ring->last_semaphore_signal_addr = semaphore->gpu_addr;
76 return true;
77 }
78 return false;
Christian König15d33322011-09-15 19:02:22 +020079}
80
Christian König1654b812013-11-12 12:58:05 +010081bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx,
Christian König15d33322011-09-15 19:02:22 +020082 struct radeon_semaphore *semaphore)
83{
Christian König1654b812013-11-12 12:58:05 +010084 struct radeon_ring *ring = &rdev->ring[ridx];
Christian Königbd80c8b2013-11-12 12:58:04 +010085
Christian König1654b812013-11-12 12:58:05 +010086 trace_radeon_semaphore_wait(ridx, semaphore);
87
88 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
89 ++semaphore->waiters;
90
91 /* for debugging lockup only, used by sysfs debug files */
92 ring->last_semaphore_wait_addr = semaphore->gpu_addr;
93 return true;
94 }
95 return false;
Christian König15d33322011-09-15 19:02:22 +020096}
97
Christian König1654b812013-11-12 12:58:05 +010098/**
Christian König57d20a42014-09-04 20:01:53 +020099 * radeon_semaphore_sync_fence - use the semaphore to sync to a fence
Christian König1654b812013-11-12 12:58:05 +0100100 *
101 * @semaphore: semaphore object to add fence to
102 * @fence: fence to sync to
103 *
104 * Sync to the fence using this semaphore object
105 */
Christian König57d20a42014-09-04 20:01:53 +0200106void radeon_semaphore_sync_fence(struct radeon_semaphore *semaphore,
107 struct radeon_fence *fence)
Christian König1654b812013-11-12 12:58:05 +0100108{
109 struct radeon_fence *other;
110
111 if (!fence)
112 return;
113
114 other = semaphore->sync_to[fence->ring];
115 semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other);
116}
117
118/**
Christian König57d20a42014-09-04 20:01:53 +0200119 * radeon_semaphore_sync_to - use the semaphore to sync to a reservation object
120 *
121 * @sema: semaphore object to add fence from reservation object to
122 * @resv: reservation object with embedded fence
123 * @shared: true if we should onyl sync to the exclusive fence
124 *
125 * Sync to the fence using this semaphore object
126 */
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200127int radeon_semaphore_sync_resv(struct radeon_device *rdev,
128 struct radeon_semaphore *sema,
129 struct reservation_object *resv,
130 bool shared)
Christian König57d20a42014-09-04 20:01:53 +0200131{
132 struct reservation_object_list *flist;
133 struct fence *f;
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200134 struct radeon_fence *fence;
Christian König57d20a42014-09-04 20:01:53 +0200135 unsigned i;
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200136 int r = 0;
Christian König57d20a42014-09-04 20:01:53 +0200137
138 /* always sync to the exclusive fence */
139 f = reservation_object_get_excl(resv);
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200140 fence = f ? to_radeon_fence(f) : NULL;
141 if (fence && fence->rdev == rdev)
142 radeon_semaphore_sync_fence(sema, fence);
143 else if (f)
144 r = fence_wait(f, true);
Christian König57d20a42014-09-04 20:01:53 +0200145
146 flist = reservation_object_get_list(resv);
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200147 if (shared || !flist || r)
148 return r;
Christian König57d20a42014-09-04 20:01:53 +0200149
150 for (i = 0; i < flist->shared_count; ++i) {
151 f = rcu_dereference_protected(flist->shared[i],
152 reservation_object_held(resv));
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200153 fence = to_radeon_fence(f);
154 if (fence && fence->rdev == rdev)
155 radeon_semaphore_sync_fence(sema, fence);
156 else
157 r = fence_wait(f, true);
158
159 if (r)
160 break;
Christian König57d20a42014-09-04 20:01:53 +0200161 }
Maarten Lankhorst392a2502014-09-25 12:39:38 +0200162 return r;
Christian König57d20a42014-09-04 20:01:53 +0200163}
164
165/**
Christian König1654b812013-11-12 12:58:05 +0100166 * radeon_semaphore_sync_rings - sync ring to all registered fences
167 *
168 * @rdev: radeon_device pointer
169 * @semaphore: semaphore object to use for sync
170 * @ring: ring that needs sync
171 *
172 * Ensure that all registered fences are signaled before letting
173 * the ring continue. The caller must hold the ring lock.
174 */
Christian König8f676c42012-05-02 15:11:18 +0200175int radeon_semaphore_sync_rings(struct radeon_device *rdev,
176 struct radeon_semaphore *semaphore,
Christian König1654b812013-11-12 12:58:05 +0100177 int ring)
Christian König8f676c42012-05-02 15:11:18 +0200178{
Christian König8f534922014-02-18 11:37:20 +0100179 unsigned count = 0;
Christian König1654b812013-11-12 12:58:05 +0100180 int i, r;
Christian Königd6999bc2012-05-09 15:34:45 +0200181
Christian König1654b812013-11-12 12:58:05 +0100182 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
183 struct radeon_fence *fence = semaphore->sync_to[i];
184
185 /* check if we really need to sync */
186 if (!radeon_fence_need_sync(fence, ring))
187 continue;
188
189 /* prevent GPU deadlocks */
190 if (!rdev->ring[i].ready) {
191 dev_err(rdev->dev, "Syncing to a disabled ring!");
192 return -EINVAL;
193 }
194
Christian König8f534922014-02-18 11:37:20 +0100195 if (++count > RADEON_NUM_SYNCS) {
196 /* not enough room, wait manually */
Christian König37615522014-02-18 15:58:31 +0100197 r = radeon_fence_wait(fence, false);
198 if (r)
199 return r;
Christian König8f534922014-02-18 11:37:20 +0100200 continue;
201 }
202
Christian König1654b812013-11-12 12:58:05 +0100203 /* allocate enough space for sync command */
204 r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
205 if (r) {
206 return r;
207 }
208
209 /* emit the signal semaphore */
210 if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) {
211 /* signaling wasn't successful wait manually */
212 radeon_ring_undo(&rdev->ring[i]);
Christian König37615522014-02-18 15:58:31 +0100213 r = radeon_fence_wait(fence, false);
214 if (r)
215 return r;
Christian König1654b812013-11-12 12:58:05 +0100216 continue;
217 }
218
219 /* we assume caller has already allocated space on waiters ring */
220 if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) {
221 /* waiting wasn't successful wait manually */
222 radeon_ring_undo(&rdev->ring[i]);
Christian König37615522014-02-18 15:58:31 +0100223 r = radeon_fence_wait(fence, false);
224 if (r)
225 return r;
Christian König1654b812013-11-12 12:58:05 +0100226 continue;
227 }
228
Michel Dänzer1538a9e2014-08-18 17:34:55 +0900229 radeon_ring_commit(rdev, &rdev->ring[i], false);
Christian König1654b812013-11-12 12:58:05 +0100230 radeon_fence_note_sync(fence, ring);
Christian König8f534922014-02-18 11:37:20 +0100231
232 semaphore->gpu_addr += 8;
Christian König220907d2012-05-10 16:46:43 +0200233 }
234
Christian König8f676c42012-05-02 15:11:18 +0200235 return 0;
Christian König8f676c42012-05-02 15:11:18 +0200236}
237
Christian König15d33322011-09-15 19:02:22 +0200238void radeon_semaphore_free(struct radeon_device *rdev,
Christian König220907d2012-05-10 16:46:43 +0200239 struct radeon_semaphore **semaphore,
Jerome Glissea8c05942012-05-09 15:34:57 +0200240 struct radeon_fence *fence)
Christian König15d33322011-09-15 19:02:22 +0200241{
Christian König220907d2012-05-10 16:46:43 +0200242 if (semaphore == NULL || *semaphore == NULL) {
Jerome Glissea8c05942012-05-09 15:34:57 +0200243 return;
Christian König15d33322011-09-15 19:02:22 +0200244 }
Christian König220907d2012-05-10 16:46:43 +0200245 if ((*semaphore)->waiters > 0) {
Jerome Glissea8c05942012-05-09 15:34:57 +0200246 dev_err(rdev->dev, "semaphore %p has more waiters than signalers,"
Christian König220907d2012-05-10 16:46:43 +0200247 " hardware lockup imminent!\n", *semaphore);
Jerome Glissea8c05942012-05-09 15:34:57 +0200248 }
Christian König220907d2012-05-10 16:46:43 +0200249 radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence);
250 kfree(*semaphore);
251 *semaphore = NULL;
Christian König15d33322011-09-15 19:02:22 +0200252}