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_to - 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_to(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_rings - sync ring to all registered fences 127 * 128 * @rdev: radeon_device pointer 129 * @semaphore: semaphore object to use for sync 130 * @ring: ring that needs sync 131 * 132 * Ensure that all registered fences are signaled before letting 133 * the ring continue. The caller must hold the ring lock. 134 */ 135 int radeon_semaphore_sync_rings(struct radeon_device *rdev, 136 struct radeon_semaphore *semaphore, 137 int ring) 138 { 139 unsigned count = 0; 140 int i, r; 141 142 for (i = 0; i < RADEON_NUM_RINGS; ++i) { 143 struct radeon_fence *fence = semaphore->sync_to[i]; 144 145 /* check if we really need to sync */ 146 if (!radeon_fence_need_sync(fence, ring)) 147 continue; 148 149 /* prevent GPU deadlocks */ 150 if (!rdev->ring[i].ready) { 151 dev_err(rdev->dev, "Syncing to a disabled ring!"); 152 return -EINVAL; 153 } 154 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; 161 } 162 163 /* allocate enough space for sync command */ 164 r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); 165 if (r) { 166 return r; 167 } 168 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; 177 } 178 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; 187 } 188 189 radeon_ring_commit(rdev, &rdev->ring[i], false); 190 radeon_fence_note_sync(fence, ring); 191 192 semaphore->gpu_addr += 8; 193 } 194 195 return 0; 196 } 197 198 void radeon_semaphore_free(struct radeon_device *rdev, 199 struct radeon_semaphore **semaphore, 200 struct radeon_fence *fence) 201 { 202 if (semaphore == NULL || *semaphore == NULL) { 203 return; 204 } 205 if ((*semaphore)->waiters > 0) { 206 dev_err(rdev->dev, "semaphore %p has more waiters than signalers," 207 " hardware lockup imminent!\n", *semaphore); 208 } 209 radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence); 210 kfree(*semaphore); 211 *semaphore = NULL; 212 } 213