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 | */ |
| 30 | #include "drmP.h" |
| 31 | #include "drm.h" |
| 32 | #include "radeon.h" |
| 33 | |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 34 | static int radeon_semaphore_add_bo(struct radeon_device *rdev) |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 35 | { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 36 | struct radeon_semaphore_bo *bo; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 37 | unsigned long irq_flags; |
| 38 | uint64_t gpu_addr; |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 39 | uint32_t *cpu_ptr; |
| 40 | int r, i; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 41 | |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 42 | |
| 43 | bo = kmalloc(sizeof(struct radeon_semaphore_bo), GFP_KERNEL); |
| 44 | if (bo == NULL) { |
| 45 | return -ENOMEM; |
| 46 | } |
| 47 | INIT_LIST_HEAD(&bo->free); |
| 48 | INIT_LIST_HEAD(&bo->list); |
| 49 | bo->nused = 0; |
| 50 | |
| 51 | r = radeon_ib_get(rdev, 0, &bo->ib, RADEON_SEMAPHORE_BO_SIZE); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 52 | if (r) { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 53 | dev_err(rdev->dev, "failed to get a bo after 5 retry\n"); |
| 54 | kfree(bo); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 55 | return r; |
| 56 | } |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 57 | gpu_addr = rdev->ib_pool.sa_manager.gpu_addr; |
| 58 | gpu_addr += bo->ib->sa_bo.offset; |
| 59 | cpu_ptr = rdev->ib_pool.sa_manager.cpu_ptr; |
| 60 | cpu_ptr += (bo->ib->sa_bo.offset >> 2); |
| 61 | for (i = 0; i < (RADEON_SEMAPHORE_BO_SIZE/8); i++) { |
| 62 | bo->semaphores[i].gpu_addr = gpu_addr; |
| 63 | bo->semaphores[i].cpu_ptr = cpu_ptr; |
| 64 | bo->semaphores[i].bo = bo; |
| 65 | list_add_tail(&bo->semaphores[i].list, &bo->free); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 66 | gpu_addr += 8; |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 67 | cpu_ptr += 2; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 68 | } |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 69 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 70 | list_add_tail(&bo->list, &rdev->semaphore_drv.bo); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 71 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 75 | static void radeon_semaphore_del_bo_locked(struct radeon_device *rdev, |
| 76 | struct radeon_semaphore_bo *bo) |
| 77 | { |
| 78 | radeon_sa_bo_free(rdev, &bo->ib->sa_bo); |
| 79 | radeon_fence_unref(&bo->ib->fence); |
| 80 | list_del(&bo->list); |
| 81 | kfree(bo); |
| 82 | } |
| 83 | |
| 84 | void radeon_semaphore_shrink_locked(struct radeon_device *rdev) |
| 85 | { |
| 86 | struct radeon_semaphore_bo *bo, *n; |
| 87 | |
| 88 | if (list_empty(&rdev->semaphore_drv.bo)) { |
| 89 | return; |
| 90 | } |
| 91 | /* only shrink if first bo has free semaphore */ |
| 92 | bo = list_first_entry(&rdev->semaphore_drv.bo, struct radeon_semaphore_bo, list); |
| 93 | if (list_empty(&bo->free)) { |
| 94 | return; |
| 95 | } |
| 96 | list_for_each_entry_safe_continue(bo, n, &rdev->semaphore_drv.bo, list) { |
| 97 | if (bo->nused) |
| 98 | continue; |
| 99 | radeon_semaphore_del_bo_locked(rdev, bo); |
| 100 | } |
| 101 | } |
| 102 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 103 | int radeon_semaphore_create(struct radeon_device *rdev, |
| 104 | struct radeon_semaphore **semaphore) |
| 105 | { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 106 | struct radeon_semaphore_bo *bo; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 107 | unsigned long irq_flags; |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 108 | bool do_retry = true; |
| 109 | int r; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 110 | |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 111 | retry: |
| 112 | *semaphore = NULL; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 113 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 114 | list_for_each_entry(bo, &rdev->semaphore_drv.bo, list) { |
| 115 | if (list_empty(&bo->free)) |
| 116 | continue; |
| 117 | *semaphore = list_first_entry(&bo->free, struct radeon_semaphore, list); |
| 118 | (*semaphore)->cpu_ptr[0] = 0; |
| 119 | (*semaphore)->cpu_ptr[1] = 0; |
| 120 | list_del(&(*semaphore)->list); |
| 121 | bo->nused++; |
| 122 | break; |
| 123 | } |
| 124 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
| 125 | |
| 126 | if (*semaphore == NULL) { |
| 127 | if (do_retry) { |
| 128 | do_retry = false; |
| 129 | r = radeon_semaphore_add_bo(rdev); |
| 130 | if (r) |
| 131 | return r; |
| 132 | goto retry; |
| 133 | } |
| 134 | return -ENOMEM; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void radeon_semaphore_emit_signal(struct radeon_device *rdev, int ring, |
| 141 | struct radeon_semaphore *semaphore) |
| 142 | { |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 143 | radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, false); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void radeon_semaphore_emit_wait(struct radeon_device *rdev, int ring, |
| 147 | struct radeon_semaphore *semaphore) |
| 148 | { |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 149 | radeon_semaphore_ring_emit(rdev, ring, &rdev->ring[ring], semaphore, true); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Christian König | 8f676c4 | 2012-05-02 15:11:18 +0200 | [diff] [blame^] | 152 | int radeon_semaphore_sync_rings(struct radeon_device *rdev, |
| 153 | struct radeon_semaphore *semaphore, |
| 154 | bool sync_to[RADEON_NUM_RINGS], |
| 155 | int dst_ring) |
| 156 | { |
| 157 | int i, r; |
| 158 | |
| 159 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 160 | unsigned num_ops = i == dst_ring ? RADEON_NUM_RINGS : 1; |
| 161 | |
| 162 | /* don't lock unused rings */ |
| 163 | if (!sync_to[i] && i != dst_ring) |
| 164 | continue; |
| 165 | |
| 166 | /* prevent GPU deadlocks */ |
| 167 | if (!rdev->ring[i].ready) { |
| 168 | dev_err(rdev->dev, "Trying to sync to a disabled ring!"); |
| 169 | r = -EINVAL; |
| 170 | goto error; |
| 171 | } |
| 172 | |
| 173 | r = radeon_ring_lock(rdev, &rdev->ring[i], num_ops * 8); |
| 174 | if (r) |
| 175 | goto error; |
| 176 | } |
| 177 | |
| 178 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 179 | /* no need to sync to our own or unused rings */ |
| 180 | if (!sync_to[i] || i == dst_ring) |
| 181 | continue; |
| 182 | |
| 183 | radeon_semaphore_emit_signal(rdev, i, semaphore); |
| 184 | radeon_semaphore_emit_wait(rdev, dst_ring, semaphore); |
| 185 | } |
| 186 | |
| 187 | for (i = 0; i < RADEON_NUM_RINGS; ++i) { |
| 188 | |
| 189 | /* don't unlock unused rings */ |
| 190 | if (!sync_to[i] && i != dst_ring) |
| 191 | continue; |
| 192 | |
| 193 | radeon_ring_unlock_commit(rdev, &rdev->ring[i]); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | |
| 198 | error: |
| 199 | /* unlock all locks taken so far */ |
| 200 | for (--i; i >= 0; --i) { |
| 201 | if (sync_to[i] || i == dst_ring) { |
| 202 | radeon_ring_unlock_undo(rdev, &rdev->ring[i]); |
| 203 | } |
| 204 | } |
| 205 | return r; |
| 206 | } |
| 207 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 208 | void radeon_semaphore_free(struct radeon_device *rdev, |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 209 | struct radeon_semaphore *semaphore) |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 210 | { |
| 211 | unsigned long irq_flags; |
| 212 | |
| 213 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 214 | semaphore->bo->nused--; |
| 215 | list_add_tail(&semaphore->list, &semaphore->bo->free); |
| 216 | radeon_semaphore_shrink_locked(rdev); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 217 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
| 218 | } |
| 219 | |
| 220 | void radeon_semaphore_driver_fini(struct radeon_device *rdev) |
| 221 | { |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 222 | struct radeon_semaphore_bo *bo, *n; |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 223 | unsigned long irq_flags; |
| 224 | |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 225 | write_lock_irqsave(&rdev->semaphore_drv.lock, irq_flags); |
Jerome Glisse | c1341e5 | 2011-12-21 12:13:47 -0500 | [diff] [blame] | 226 | /* we force to free everything */ |
| 227 | list_for_each_entry_safe(bo, n, &rdev->semaphore_drv.bo, list) { |
| 228 | if (!list_empty(&bo->free)) { |
| 229 | dev_err(rdev->dev, "still in use semaphore\n"); |
| 230 | } |
| 231 | radeon_semaphore_del_bo_locked(rdev, bo); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 232 | } |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 233 | write_unlock_irqrestore(&rdev->semaphore_drv.lock, irq_flags); |
Christian König | 15d3332 | 2011-09-15 19:02:22 +0200 | [diff] [blame] | 234 | } |