Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 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 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 30 | #include <drm/drmP.h> |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 31 | #include "radeon.h" |
Christian König | bd80c8b | 2013-11-12 12:58:04 +0100 | [diff] [blame] | 32 | #include "radeon_trace.h" |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 33 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 34 | int radeon_semaphore_create(struct radeon_device *rdev, |
| 35 | struct radeon_semaphore **semaphore) |
| 36 | { |
Christian König | f229407 | 2014-09-07 12:06:52 +0200 | [diff] [blame] | 37 | uint64_t *cpu_addr; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 38 | int i, r; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 39 | |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 40 | *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 41 | if (*semaphore == NULL) { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 42 | return -ENOMEM; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 43 | } |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 44 | r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &(*semaphore)->sa_bo, |
Christian König | 4d15264 | 2014-02-20 21:48:00 +0100 | [diff] [blame] | 45 | 8 * RADEON_NUM_SYNCS, 8); |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 46 | if (r) { |
| 47 | kfree(*semaphore); |
| 48 | *semaphore = NULL; |
| 49 | return r; |
| 50 | } |
| 51 | (*semaphore)->waiters = 0; |
| 52 | (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo); |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 53 | |
| 54 | cpu_addr = radeon_sa_bo_cpu_addr((*semaphore)->sa_bo); |
| 55 | for (i = 0; i < RADEON_NUM_SYNCS; ++i) |
| 56 | cpu_addr[i] = 0; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 57 | |
| 58 | for (i = 0; i < RADEON_NUM_RINGS; ++i) |
| 59 | (*semaphore)->sync_to[i] = NULL; |
| 60 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 61 | return 0; |
| 62 | } |
| 63 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 64 | bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx, |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 65 | struct radeon_semaphore *semaphore) |
| 66 | { |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 67 | struct radeon_ring *ring = &rdev->ring[ridx]; |
Christian König | bd80c8b | 2013-11-12 12:58:04 +0100 | [diff] [blame] | 68 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 69 | trace_radeon_semaphore_signale(ridx, semaphore); |
| 70 | |
| 71 | if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) { |
| 72 | --semaphore->waiters; |
| 73 | |
| 74 | /* for debugging lockup only, used by sysfs debug files */ |
| 75 | ring->last_semaphore_signal_addr = semaphore->gpu_addr; |
| 76 | return true; |
| 77 | } |
| 78 | return false; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 81 | bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx, |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 82 | struct radeon_semaphore *semaphore) |
| 83 | { |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 84 | struct radeon_ring *ring = &rdev->ring[ridx]; |
Christian König | bd80c8b | 2013-11-12 12:58:04 +0100 | [diff] [blame] | 85 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 86 | trace_radeon_semaphore_wait(ridx, semaphore); |
| 87 | |
| 88 | if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) { |
| 89 | ++semaphore->waiters; |
| 90 | |
| 91 | /* for debugging lockup only, used by sysfs debug files */ |
| 92 | ring->last_semaphore_wait_addr = semaphore->gpu_addr; |
| 93 | return true; |
| 94 | } |
| 95 | return false; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 98 | /** |
Christian König | 57d20a4 | 2014-09-04 20:01:53 +0200 | [diff] [blame] | 99 | * radeon_semaphore_sync_fence - use the semaphore to sync to a fence |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 100 | * |
| 101 | * @semaphore: semaphore object to add fence to |
| 102 | * @fence: fence to sync to |
| 103 | * |
| 104 | * Sync to the fence using this semaphore object |
| 105 | */ |
Christian König | 57d20a4 | 2014-09-04 20:01:53 +0200 | [diff] [blame] | 106 | void radeon_semaphore_sync_fence(struct radeon_semaphore *semaphore, |
| 107 | struct radeon_fence *fence) |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 108 | { |
| 109 | struct radeon_fence *other; |
| 110 | |
| 111 | if (!fence) |
| 112 | return; |
| 113 | |
| 114 | other = semaphore->sync_to[fence->ring]; |
| 115 | semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other); |
| 116 | } |
| 117 | |
| 118 | /** |
Christian König | 57d20a4 | 2014-09-04 20:01:53 +0200 | [diff] [blame] | 119 | * radeon_semaphore_sync_to - use the semaphore to sync to a reservation object |
| 120 | * |
| 121 | * @sema: semaphore object to add fence from reservation object to |
| 122 | * @resv: reservation object with embedded fence |
| 123 | * @shared: true if we should onyl sync to the exclusive fence |
| 124 | * |
| 125 | * Sync to the fence using this semaphore object |
| 126 | */ |
| 127 | void radeon_semaphore_sync_resv(struct radeon_semaphore *sema, |
| 128 | struct reservation_object *resv, |
| 129 | bool shared) |
| 130 | { |
| 131 | struct reservation_object_list *flist; |
| 132 | struct fence *f; |
| 133 | unsigned i; |
| 134 | |
| 135 | /* always sync to the exclusive fence */ |
| 136 | f = reservation_object_get_excl(resv); |
| 137 | radeon_semaphore_sync_fence(sema, (struct radeon_fence*)f); |
| 138 | |
| 139 | flist = reservation_object_get_list(resv); |
| 140 | if (shared || !flist) |
| 141 | return; |
| 142 | |
| 143 | for (i = 0; i < flist->shared_count; ++i) { |
| 144 | f = rcu_dereference_protected(flist->shared[i], |
| 145 | reservation_object_held(resv)); |
| 146 | radeon_semaphore_sync_fence(sema, (struct radeon_fence*)f); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 151 | * radeon_semaphore_sync_rings - sync ring to all registered fences |
| 152 | * |
| 153 | * @rdev: radeon_device pointer |
| 154 | * @semaphore: semaphore object to use for sync |
| 155 | * @ring: ring that needs sync |
| 156 | * |
| 157 | * Ensure that all registered fences are signaled before letting |
| 158 | * the ring continue. The caller must hold the ring lock. |
| 159 | */ |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 160 | int radeon_semaphore_sync_rings(struct radeon_device *rdev, |
| 161 | struct radeon_semaphore *semaphore, |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 162 | int ring) |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 163 | { |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 164 | unsigned count = 0; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 165 | int i, r; |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 166 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 167 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 168 | struct radeon_fence *fence = semaphore->sync_to[i]; |
| 169 | |
| 170 | /* check if we really need to sync */ |
| 171 | if (!radeon_fence_need_sync(fence, ring)) |
| 172 | continue; |
| 173 | |
| 174 | /* prevent GPU deadlocks */ |
| 175 | if (!rdev->ring[i].ready) { |
| 176 | dev_err(rdev->dev, "Syncing to a disabled ring!"); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 180 | if (++count > RADEON_NUM_SYNCS) { |
| 181 | /* not enough room, wait manually */ |
Christian König | 3761552 | 2014-02-18 15:58:31 +0100 | [diff] [blame] | 182 | r = radeon_fence_wait(fence, false); |
| 183 | if (r) |
| 184 | return r; |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 185 | continue; |
| 186 | } |
| 187 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 188 | /* allocate enough space for sync command */ |
| 189 | r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); |
| 190 | if (r) { |
| 191 | return r; |
| 192 | } |
| 193 | |
| 194 | /* emit the signal semaphore */ |
| 195 | if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { |
| 196 | /* signaling wasn't successful wait manually */ |
| 197 | radeon_ring_undo(&rdev->ring[i]); |
Christian König | 3761552 | 2014-02-18 15:58:31 +0100 | [diff] [blame] | 198 | r = radeon_fence_wait(fence, false); |
| 199 | if (r) |
| 200 | return r; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 201 | continue; |
| 202 | } |
| 203 | |
| 204 | /* we assume caller has already allocated space on waiters ring */ |
| 205 | if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { |
| 206 | /* waiting wasn't successful wait manually */ |
| 207 | radeon_ring_undo(&rdev->ring[i]); |
Christian König | 3761552 | 2014-02-18 15:58:31 +0100 | [diff] [blame] | 208 | r = radeon_fence_wait(fence, false); |
| 209 | if (r) |
| 210 | return r; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 211 | continue; |
| 212 | } |
| 213 | |
Michel Dänzer | 1538a9e | 2014-08-18 17:34:55 +0900 | [diff] [blame] | 214 | radeon_ring_commit(rdev, &rdev->ring[i], false); |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame] | 215 | radeon_fence_note_sync(fence, ring); |
Christian König | 8f53492 | 2014-02-18 11:37:20 +0100 | [diff] [blame] | 216 | |
| 217 | semaphore->gpu_addr += 8; |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 220 | return 0; |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 223 | void radeon_semaphore_free(struct radeon_device *rdev, |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 224 | struct radeon_semaphore **semaphore, |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 225 | struct radeon_fence *fence) |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 226 | { |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 227 | if (semaphore == NULL || *semaphore == NULL) { |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 228 | return; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 229 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 230 | if ((*semaphore)->waiters > 0) { |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 231 | dev_err(rdev->dev, "semaphore %p has more waiters than signalers," |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 232 | " hardware lockup imminent!\n", *semaphore); |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 233 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 234 | radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence); |
| 235 | kfree(*semaphore); |
| 236 | *semaphore = NULL; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 237 | } |