2 * Copyright 2011 Christian König.
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
28 * Christian König <deathsimple@vodafone.de>
33 #include "radeon_trace.h"
36 int radeon_semaphore_create(struct radeon_device
*rdev
,
37 struct radeon_semaphore
**semaphore
)
42 *semaphore
= kmalloc(sizeof(struct radeon_semaphore
), M_DRM
,
44 if (*semaphore
== NULL
) {
47 r
= radeon_sa_bo_new(rdev
, &rdev
->ring_tmp_bo
, &(*semaphore
)->sa_bo
,
48 8 * RADEON_NUM_SYNCS
, 8);
54 (*semaphore
)->waiters
= 0;
55 (*semaphore
)->gpu_addr
= radeon_sa_bo_gpu_addr((*semaphore
)->sa_bo
);
57 cpu_addr
= radeon_sa_bo_cpu_addr((*semaphore
)->sa_bo
);
58 for (i
= 0; i
< RADEON_NUM_SYNCS
; ++i
)
61 for (i
= 0; i
< RADEON_NUM_RINGS
; ++i
)
62 (*semaphore
)->sync_to
[i
] = NULL
;
67 bool radeon_semaphore_emit_signal(struct radeon_device
*rdev
, int ridx
,
68 struct radeon_semaphore
*semaphore
)
70 struct radeon_ring
*ring
= &rdev
->ring
[ridx
];
73 trace_radeon_semaphore_signale(ridx
, semaphore
);
76 if (radeon_semaphore_ring_emit(rdev
, ridx
, ring
, semaphore
, false)) {
79 /* for debugging lockup only, used by sysfs debug files */
80 ring
->last_semaphore_signal_addr
= semaphore
->gpu_addr
;
86 bool radeon_semaphore_emit_wait(struct radeon_device
*rdev
, int ridx
,
87 struct radeon_semaphore
*semaphore
)
89 struct radeon_ring
*ring
= &rdev
->ring
[ridx
];
92 trace_radeon_semaphore_wait(ridx
, semaphore
);
95 if (radeon_semaphore_ring_emit(rdev
, ridx
, ring
, semaphore
, true)) {
98 /* for debugging lockup only, used by sysfs debug files */
99 ring
->last_semaphore_wait_addr
= semaphore
->gpu_addr
;
106 * radeon_semaphore_sync_to - use the semaphore to sync to a fence
108 * @semaphore: semaphore object to add fence to
109 * @fence: fence to sync to
111 * Sync to the fence using this semaphore object
113 void radeon_semaphore_sync_to(struct radeon_semaphore
*semaphore
,
114 struct radeon_fence
*fence
)
116 struct radeon_fence
*other
;
121 other
= semaphore
->sync_to
[fence
->ring
];
122 semaphore
->sync_to
[fence
->ring
] = radeon_fence_later(fence
, other
);
126 * radeon_semaphore_sync_rings - sync ring to all registered fences
128 * @rdev: radeon_device pointer
129 * @semaphore: semaphore object to use for sync
130 * @ring: ring that needs sync
132 * Ensure that all registered fences are signaled before letting
133 * the ring continue. The caller must hold the ring lock.
135 int radeon_semaphore_sync_rings(struct radeon_device
*rdev
,
136 struct radeon_semaphore
*semaphore
,
142 for (i
= 0; i
< RADEON_NUM_RINGS
; ++i
) {
143 struct radeon_fence
*fence
= semaphore
->sync_to
[i
];
145 /* check if we really need to sync */
146 if (!radeon_fence_need_sync(fence
, ring
))
149 /* prevent GPU deadlocks */
150 if (!rdev
->ring
[i
].ready
) {
151 dev_err(rdev
->dev
, "Syncing to a disabled ring!");
155 if (++count
> RADEON_NUM_SYNCS
) {
156 /* not enough room, wait manually */
157 r
= radeon_fence_wait(fence
, false);
163 /* allocate enough space for sync command */
164 r
= radeon_ring_alloc(rdev
, &rdev
->ring
[i
], 16);
169 /* emit the signal semaphore */
170 if (!radeon_semaphore_emit_signal(rdev
, i
, semaphore
)) {
171 /* signaling wasn't successful wait manually */
172 radeon_ring_undo(&rdev
->ring
[i
]);
173 r
= radeon_fence_wait(fence
, false);
179 /* we assume caller has already allocated space on waiters ring */
180 if (!radeon_semaphore_emit_wait(rdev
, ring
, semaphore
)) {
181 /* waiting wasn't successful wait manually */
182 radeon_ring_undo(&rdev
->ring
[i
]);
183 r
= radeon_fence_wait(fence
, false);
189 radeon_ring_commit(rdev
, &rdev
->ring
[i
], false);
190 radeon_fence_note_sync(fence
, ring
);
192 semaphore
->gpu_addr
+= 8;
198 void radeon_semaphore_free(struct radeon_device
*rdev
,
199 struct radeon_semaphore
**semaphore
,
200 struct radeon_fence
*fence
)
202 if (semaphore
== NULL
|| *semaphore
== NULL
) {
205 if ((*semaphore
)->waiters
> 0) {
206 dev_err(rdev
->dev
, "semaphore %p has more waiters than signalers,"
207 " hardware lockup imminent!\n", *semaphore
);
209 radeon_sa_bo_free(rdev
, &(*semaphore
)->sa_bo
, fence
);