Christian König | 975700d2 | 2014-11-19 14:01:22 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 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 <christian.koenig@amd.com> |
| 29 | */ |
| 30 | |
| 31 | #include <drm/drmP.h> |
| 32 | #include "radeon.h" |
| 33 | #include "radeon_trace.h" |
| 34 | |
| 35 | /** |
| 36 | * radeon_sync_create - zero init sync object |
| 37 | * |
| 38 | * @sync: sync object to initialize |
| 39 | * |
| 40 | * Just clear the sync object for now. |
| 41 | */ |
| 42 | void radeon_sync_create(struct radeon_sync *sync) |
| 43 | { |
| 44 | unsigned i; |
| 45 | |
| 46 | for (i = 0; i < RADEON_NUM_SYNCS; ++i) |
| 47 | sync->semaphores[i] = NULL; |
| 48 | |
| 49 | for (i = 0; i < RADEON_NUM_RINGS; ++i) |
| 50 | sync->sync_to[i] = NULL; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * radeon_sync_fence - use the semaphore to sync to a fence |
| 55 | * |
| 56 | * @sync: sync object to add fence to |
| 57 | * @fence: fence to sync to |
| 58 | * |
| 59 | * Sync to the fence using the semaphore objects |
| 60 | */ |
| 61 | void radeon_sync_fence(struct radeon_sync *sync, |
| 62 | struct radeon_fence *fence) |
| 63 | { |
| 64 | struct radeon_fence *other; |
| 65 | |
| 66 | if (!fence) |
| 67 | return; |
| 68 | |
| 69 | other = sync->sync_to[fence->ring]; |
| 70 | sync->sync_to[fence->ring] = radeon_fence_later(fence, other); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * radeon_sync_resv - use the semaphores to sync to a reservation object |
| 75 | * |
| 76 | * @sync: sync object to add fences from reservation object to |
| 77 | * @resv: reservation object with embedded fence |
| 78 | * @shared: true if we should only sync to the exclusive fence |
| 79 | * |
| 80 | * Sync to the fence using the semaphore objects |
| 81 | */ |
| 82 | int radeon_sync_resv(struct radeon_device *rdev, |
| 83 | struct radeon_sync *sync, |
| 84 | struct reservation_object *resv, |
| 85 | bool shared) |
| 86 | { |
| 87 | struct reservation_object_list *flist; |
| 88 | struct fence *f; |
| 89 | struct radeon_fence *fence; |
| 90 | unsigned i; |
| 91 | int r = 0; |
| 92 | |
| 93 | /* always sync to the exclusive fence */ |
| 94 | f = reservation_object_get_excl(resv); |
| 95 | fence = f ? to_radeon_fence(f) : NULL; |
| 96 | if (fence && fence->rdev == rdev) |
| 97 | radeon_sync_fence(sync, fence); |
| 98 | else if (f) |
| 99 | r = fence_wait(f, true); |
| 100 | |
| 101 | flist = reservation_object_get_list(resv); |
| 102 | if (shared || !flist || r) |
| 103 | return r; |
| 104 | |
| 105 | for (i = 0; i < flist->shared_count; ++i) { |
| 106 | f = rcu_dereference_protected(flist->shared[i], |
| 107 | reservation_object_held(resv)); |
| 108 | fence = to_radeon_fence(f); |
| 109 | if (fence && fence->rdev == rdev) |
| 110 | radeon_sync_fence(sync, fence); |
| 111 | else |
| 112 | r = fence_wait(f, true); |
| 113 | |
| 114 | if (r) |
| 115 | break; |
| 116 | } |
| 117 | return r; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * radeon_sync_rings - sync ring to all registered fences |
| 122 | * |
| 123 | * @rdev: radeon_device pointer |
| 124 | * @sync: sync object to use |
| 125 | * @ring: ring that needs sync |
| 126 | * |
| 127 | * Ensure that all registered fences are signaled before letting |
| 128 | * the ring continue. The caller must hold the ring lock. |
| 129 | */ |
| 130 | int radeon_sync_rings(struct radeon_device *rdev, |
| 131 | struct radeon_sync *sync, |
| 132 | int ring) |
| 133 | { |
| 134 | unsigned count = 0; |
| 135 | int i, r; |
| 136 | |
| 137 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 138 | struct radeon_fence *fence = sync->sync_to[i]; |
| 139 | struct radeon_semaphore *semaphore; |
| 140 | |
| 141 | /* check if we really need to sync */ |
| 142 | if (!radeon_fence_need_sync(fence, ring)) |
| 143 | continue; |
| 144 | |
| 145 | /* prevent GPU deadlocks */ |
| 146 | if (!rdev->ring[i].ready) { |
| 147 | dev_err(rdev->dev, "Syncing to a disabled ring!"); |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
| 151 | if (count >= RADEON_NUM_SYNCS) { |
| 152 | /* not enough room, wait manually */ |
| 153 | r = radeon_fence_wait(fence, false); |
| 154 | if (r) |
| 155 | return r; |
| 156 | continue; |
| 157 | } |
| 158 | r = radeon_semaphore_create(rdev, &semaphore); |
| 159 | if (r) |
| 160 | return r; |
| 161 | |
| 162 | sync->semaphores[count++] = semaphore; |
| 163 | |
| 164 | /* allocate enough space for sync command */ |
| 165 | r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); |
| 166 | if (r) |
| 167 | return r; |
| 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 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * radeon_sync_free - free the sync object |
| 198 | * |
| 199 | * @rdev: radeon_device pointer |
| 200 | * @sync: sync object to use |
| 201 | * @fence: fence to use for the free |
| 202 | * |
| 203 | * Free the sync object by freeing all semaphores in it. |
| 204 | */ |
| 205 | void radeon_sync_free(struct radeon_device *rdev, |
| 206 | struct radeon_sync *sync, |
| 207 | struct radeon_fence *fence) |
| 208 | { |
| 209 | unsigned i; |
| 210 | |
| 211 | for (i = 0; i < RADEON_NUM_SYNCS; ++i) |
| 212 | radeon_semaphore_free(rdev, &sync->semaphores[i], fence); |
| 213 | } |