1 /* 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 */ 30 #include <drm/drmP.h> 31 #include "radeon.h" 32 #ifdef TRACE_TODO 33 #include "radeon_trace.h" 34 #endif 35 36 int radeon_semaphore_create(struct radeon_device *rdev, 37 struct radeon_semaphore **semaphore) 38 { 39 uint64_t *cpu_addr; 40 int i, r; 41 42 *semaphore = kmalloc(sizeof(struct radeon_semaphore), M_DRM, 43 M_WAITOK); 44 if (*semaphore == NULL) { 45 return -ENOMEM; 46 } 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; 53 } 54 (*semaphore)->waiters = 0; 55 (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo); 56 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; 60 61 for (i = 0; i < RADEON_NUM_RINGS; ++i) 62 (*semaphore)->sync_to[i] = NULL; 63 64 return 0; 65 } 66 67 bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx, 68 struct radeon_semaphore *semaphore) 69 { 70 struct radeon_ring *ring = &rdev->ring[ridx]; 71 72 #ifdef TRACE_TODO 73 trace_radeon_semaphore_signale(ridx, semaphore); 74 #endif 75 76 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) { 77 --semaphore->waiters; 78 79 /* for debugging lockup only, used by sysfs debug files */ 80 ring->last_semaphore_signal_addr = semaphore->gpu_addr; 81 return true; 82 } 83 return false; 84 } 85 86 bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx, 87 struct radeon_semaphore *semaphore) 88 { 89 struct radeon_ring *ring = &rdev->ring[ridx]; 90 91 #ifdef TRACE_TODO 92 trace_radeon_semaphore_wait(ridx, semaphore); 93 #endif 94 95 if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) { 96 ++semaphore->waiters; 97 98 /* for debugging lockup only, used by sysfs debug files */ 99 ring->last_semaphore_wait_addr = semaphore->gpu_addr; 100 return true; 101 } 102 return false; 103 } 104 105 /** 106 * radeon_semaphore_sync_fence - use the semaphore to sync to a fence 107 * 108 * @semaphore: semaphore object to add fence to 109 * @fence: fence to sync to 110 * 111 * Sync to the fence using this semaphore object 112 */ 113 void radeon_semaphore_sync_fence(struct radeon_semaphore *semaphore, 114 struct radeon_fence *fence) 115 { 116 struct radeon_fence *other; 117 118 if (!fence) 119 return; 120 121 other = semaphore->sync_to[fence->ring]; 122 semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other); 123 } 124 125 /** 126 * radeon_semaphore_sync_to - use the semaphore to sync to a reservation object 127 * 128 * @sema: semaphore object to add fence from reservation object to 129 * @resv: reservation object with embedded fence 130 * @shared: true if we should onyl sync to the exclusive fence 131 * 132 * Sync to the fence using this semaphore object 133 */ 134 int radeon_semaphore_sync_resv(struct radeon_device *rdev, 135 struct radeon_semaphore *sema, 136 struct reservation_object *resv, 137 bool shared) 138 { 139 struct reservation_object_list *flist; 140 struct fence *f; 141 struct radeon_fence *fence; 142 unsigned i; 143 int r = 0; 144 145 /* always sync to the exclusive fence */ 146 f = reservation_object_get_excl(resv); 147 fence = f ? to_radeon_fence(f) : NULL; 148 if (fence && fence->rdev == rdev) 149 radeon_semaphore_sync_fence(sema, fence); 150 else if (f) 151 r = fence_wait(f, true); 152 153 flist = reservation_object_get_list(resv); 154 if (shared || !flist || r) 155 return r; 156 157 for (i = 0; i < flist->shared_count; ++i) { 158 f = rcu_dereference_protected(flist->shared[i], 159 reservation_object_held(resv)); 160 fence = to_radeon_fence(f); 161 if (fence && fence->rdev == rdev) 162 radeon_semaphore_sync_fence(sema, fence); 163 else 164 r = fence_wait(f, true); 165 166 if (r) 167 break; 168 } 169 return r; 170 } 171 172 /** 173 * radeon_semaphore_sync_rings - sync ring to all registered fences 174 * 175 * @rdev: radeon_device pointer 176 * @semaphore: semaphore object to use for sync 177 * @ring: ring that needs sync 178 * 179 * Ensure that all registered fences are signaled before letting 180 * the ring continue. The caller must hold the ring lock. 181 */ 182 int radeon_semaphore_sync_rings(struct radeon_device *rdev, 183 struct radeon_semaphore *semaphore, 184 int ring) 185 { 186 unsigned count = 0; 187 int i, r; 188 189 for (i = 0; i < RADEON_NUM_RINGS; ++i) { 190 struct radeon_fence *fence = semaphore->sync_to[i]; 191 192 /* check if we really need to sync */ 193 if (!radeon_fence_need_sync(fence, ring)) 194 continue; 195 196 /* prevent GPU deadlocks */ 197 if (!rdev->ring[i].ready) { 198 dev_err(rdev->dev, "Syncing to a disabled ring!"); 199 return -EINVAL; 200 } 201 202 if (++count > RADEON_NUM_SYNCS) { 203 /* not enough room, wait manually */ 204 r = radeon_fence_wait(fence, false); 205 if (r) 206 return r; 207 continue; 208 } 209 210 /* allocate enough space for sync command */ 211 r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); 212 if (r) { 213 return r; 214 } 215 216 /* emit the signal semaphore */ 217 if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { 218 /* signaling wasn't successful wait manually */ 219 radeon_ring_undo(&rdev->ring[i]); 220 r = radeon_fence_wait(fence, false); 221 if (r) 222 return r; 223 continue; 224 } 225 226 /* we assume caller has already allocated space on waiters ring */ 227 if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { 228 /* waiting wasn't successful wait manually */ 229 radeon_ring_undo(&rdev->ring[i]); 230 r = radeon_fence_wait(fence, false); 231 if (r) 232 return r; 233 continue; 234 } 235 236 radeon_ring_commit(rdev, &rdev->ring[i], false); 237 radeon_fence_note_sync(fence, ring); 238 239 semaphore->gpu_addr += 8; 240 } 241 242 return 0; 243 } 244 245 void radeon_semaphore_free(struct radeon_device *rdev, 246 struct radeon_semaphore **semaphore, 247 struct radeon_fence *fence) 248 { 249 if (semaphore == NULL || *semaphore == NULL) { 250 return; 251 } 252 if ((*semaphore)->waiters > 0) { 253 dev_err(rdev->dev, "semaphore %p has more waiters than signalers," 254 " hardware lockup imminent!\n", *semaphore); 255 } 256 radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence); 257 kfree(*semaphore); 258 *semaphore = NULL; 259 } 260