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 | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 37 | int i, r; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 38 | |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 39 | *semaphore = kmalloc(sizeof(struct radeon_semaphore), GFP_KERNEL); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 40 | if (*semaphore == NULL) { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 41 | return -ENOMEM; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 42 | } |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 43 | r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 44 | &(*semaphore)->sa_bo, 8, 8, true); |
| 45 | if (r) { |
| 46 | kfree(*semaphore); |
| 47 | *semaphore = NULL; |
| 48 | return r; |
| 49 | } |
| 50 | (*semaphore)->waiters = 0; |
| 51 | (*semaphore)->gpu_addr = radeon_sa_bo_gpu_addr((*semaphore)->sa_bo); |
| 52 | *((uint64_t*)radeon_sa_bo_cpu_addr((*semaphore)->sa_bo)) = 0; |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 53 | |
| 54 | for (i = 0; i < RADEON_NUM_RINGS; ++i) |
| 55 | (*semaphore)->sync_to[i] = NULL; |
| 56 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 57 | return 0; |
| 58 | } |
| 59 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 60 | bool radeon_semaphore_emit_signal(struct radeon_device *rdev, int ridx, |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 61 | struct radeon_semaphore *semaphore) |
| 62 | { |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 63 | struct radeon_ring *ring = &rdev->ring[ridx]; |
Christian König | bd80c8b | 2013-11-12 12:58:04 +0100 | [diff] [blame] | 64 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 65 | trace_radeon_semaphore_signale(ridx, semaphore); |
| 66 | |
| 67 | if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, false)) { |
| 68 | --semaphore->waiters; |
| 69 | |
| 70 | /* for debugging lockup only, used by sysfs debug files */ |
| 71 | ring->last_semaphore_signal_addr = semaphore->gpu_addr; |
| 72 | return true; |
| 73 | } |
| 74 | return false; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 77 | bool radeon_semaphore_emit_wait(struct radeon_device *rdev, int ridx, |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 78 | struct radeon_semaphore *semaphore) |
| 79 | { |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 80 | struct radeon_ring *ring = &rdev->ring[ridx]; |
Christian König | bd80c8b | 2013-11-12 12:58:04 +0100 | [diff] [blame] | 81 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 82 | trace_radeon_semaphore_wait(ridx, semaphore); |
| 83 | |
| 84 | if (radeon_semaphore_ring_emit(rdev, ridx, ring, semaphore, true)) { |
| 85 | ++semaphore->waiters; |
| 86 | |
| 87 | /* for debugging lockup only, used by sysfs debug files */ |
| 88 | ring->last_semaphore_wait_addr = semaphore->gpu_addr; |
| 89 | return true; |
| 90 | } |
| 91 | return false; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 94 | /** |
| 95 | * radeon_semaphore_sync_to - use the semaphore to sync to a fence |
| 96 | * |
| 97 | * @semaphore: semaphore object to add fence to |
| 98 | * @fence: fence to sync to |
| 99 | * |
| 100 | * Sync to the fence using this semaphore object |
| 101 | */ |
| 102 | void radeon_semaphore_sync_to(struct radeon_semaphore *semaphore, |
| 103 | struct radeon_fence *fence) |
| 104 | { |
| 105 | struct radeon_fence *other; |
| 106 | |
| 107 | if (!fence) |
| 108 | return; |
| 109 | |
| 110 | other = semaphore->sync_to[fence->ring]; |
| 111 | semaphore->sync_to[fence->ring] = radeon_fence_later(fence, other); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * radeon_semaphore_sync_rings - sync ring to all registered fences |
| 116 | * |
| 117 | * @rdev: radeon_device pointer |
| 118 | * @semaphore: semaphore object to use for sync |
| 119 | * @ring: ring that needs sync |
| 120 | * |
| 121 | * Ensure that all registered fences are signaled before letting |
| 122 | * the ring continue. The caller must hold the ring lock. |
| 123 | */ |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 124 | int radeon_semaphore_sync_rings(struct radeon_device *rdev, |
| 125 | struct radeon_semaphore *semaphore, |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 126 | int ring) |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 127 | { |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 128 | int i, r; |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 129 | |
Christian König | 1654b81 | 2013-11-12 12:58:05 +0100 | [diff] [blame^] | 130 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 131 | struct radeon_fence *fence = semaphore->sync_to[i]; |
| 132 | |
| 133 | /* check if we really need to sync */ |
| 134 | if (!radeon_fence_need_sync(fence, ring)) |
| 135 | continue; |
| 136 | |
| 137 | /* prevent GPU deadlocks */ |
| 138 | if (!rdev->ring[i].ready) { |
| 139 | dev_err(rdev->dev, "Syncing to a disabled ring!"); |
| 140 | return -EINVAL; |
| 141 | } |
| 142 | |
| 143 | /* allocate enough space for sync command */ |
| 144 | r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); |
| 145 | if (r) { |
| 146 | return r; |
| 147 | } |
| 148 | |
| 149 | /* emit the signal semaphore */ |
| 150 | if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { |
| 151 | /* signaling wasn't successful wait manually */ |
| 152 | radeon_ring_undo(&rdev->ring[i]); |
| 153 | radeon_fence_wait_locked(fence); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | /* we assume caller has already allocated space on waiters ring */ |
| 158 | if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { |
| 159 | /* waiting wasn't successful wait manually */ |
| 160 | radeon_ring_undo(&rdev->ring[i]); |
| 161 | radeon_fence_wait_locked(fence); |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | radeon_ring_commit(rdev, &rdev->ring[i]); |
| 166 | radeon_fence_note_sync(fence, ring); |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 167 | } |
| 168 | |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 169 | return 0; |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 172 | void radeon_semaphore_free(struct radeon_device *rdev, |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 173 | struct radeon_semaphore **semaphore, |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 174 | struct radeon_fence *fence) |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 175 | { |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 176 | if (semaphore == NULL || *semaphore == NULL) { |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 177 | return; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 178 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 179 | if ((*semaphore)->waiters > 0) { |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 180 | dev_err(rdev->dev, "semaphore %p has more waiters than signalers," |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 181 | " hardware lockup imminent!\n", *semaphore); |
Jerome Glisse | a8c0594 | 2012-05-09 15:34:57 +0200 | [diff] [blame] | 182 | } |
Christian König | 220907d | 2012-05-10 16:46:43 +0200 | [diff] [blame] | 183 | radeon_sa_bo_free(rdev, &(*semaphore)->sa_bo, fence); |
| 184 | kfree(*semaphore); |
| 185 | *semaphore = NULL; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 186 | } |