drm: Define struct page and use it everywhere
[dragonfly.git] / sys / dev / drm / radeon / radeon_semaphore.c
blob076b823b32742f0bdaef92dabba044c65f2c0028
1 /*
2 * Copyright 2011 Christian König.
3 * All Rights Reserved.
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
23 * of the Software.
27 * Authors:
28 * Christian König <deathsimple@vodafone.de>
30 #include <drm/drmP.h>
31 #include "radeon.h"
32 #ifdef TRACE_TODO
33 #include "radeon_trace.h"
34 #endif
36 int radeon_semaphore_create(struct radeon_device *rdev,
37 struct radeon_semaphore **semaphore)
39 uint64_t *cpu_addr;
40 int i, r;
42 *semaphore = kmalloc(sizeof(struct radeon_semaphore), M_DRM,
43 M_WAITOK);
44 if (*semaphore == NULL) {
45 return -ENOMEM;
47 r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*semaphore)->sa_bo,
48 8 * RADEON_NUM_SYNCS, 8);
49 if (r) {
50 kfree(*semaphore);
51 *semaphore = NULL;
52 return r;
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)
59 cpu_addr[i] = 0;
61 for (i = 0; i < RADEON_NUM_RINGS; ++i)
62 (*semaphore)->sync_to[i] = NULL;
64 return 0;
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];
72 #ifdef TRACE_TODO
73 trace_radeon_semaphore_signale(ridx, semaphore);
74 #endif
76 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) {
77 --semaphore->waiters;
79 /* for debugging lockup only, used by sysfs debug files */
80 ring->last_semaphore_signal_addr = semaphore->gpu_addr;
81 return true;
83 return false;
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];
91 #ifdef TRACE_TODO
92 trace_radeon_semaphore_wait(ridx, semaphore);
93 #endif
95 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) {
96 ++semaphore->waiters;
98 /* for debugging lockup only, used by sysfs debug files */
99 ring->last_semaphore_wait_addr = semaphore->gpu_addr;
100 return true;
102 return false;
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;
118 if (!fence)
119 return;
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,
137 int ring)
139 unsigned count = 0;
140 int i, r;
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))
147 continue;
149 /* prevent GPU deadlocks */
150 if (!rdev->ring[i].ready) {
151 dev_err(rdev->dev, "Syncing to a disabled ring!");
152 return -EINVAL;
155 if (++count > RADEON_NUM_SYNCS) {
156 /* not enough room, wait manually */
157 r = radeon_fence_wait(fence, false);
158 if (r)
159 return r;
160 continue;
163 /* allocate enough space for sync command */
164 r = radeon_ring_alloc(rdev, &rdev->ring[i], 16);
165 if (r) {
166 return r;
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);
174 if (r)
175 return r;
176 continue;
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);
184 if (r)
185 return r;
186 continue;
189 radeon_ring_commit(rdev, &rdev->ring[i], false);
190 radeon_fence_note_sync(fence, ring);
192 semaphore->gpu_addr += 8;
195 return 0;
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) {
203 return;
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);
210 kfree(*semaphore);
211 *semaphore = NULL;