1 1.1 riastrad /* $NetBSD: radeon_sync.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2014 Advanced Micro Devices, Inc. 5 1.1 riastrad * All Rights Reserved. 6 1.1 riastrad * 7 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 8 1.1 riastrad * copy of this software and associated documentation files (the 9 1.1 riastrad * "Software"), to deal in the Software without restriction, including 10 1.1 riastrad * without limitation the rights to use, copy, modify, merge, publish, 11 1.1 riastrad * distribute, sub license, and/or sell copies of the Software, and to 12 1.1 riastrad * permit persons to whom the Software is furnished to do so, subject to 13 1.1 riastrad * the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 18 1.1 riastrad * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 19 1.1 riastrad * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 1.1 riastrad * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 21 1.1 riastrad * USE OR OTHER DEALINGS IN THE SOFTWARE. 22 1.1 riastrad * 23 1.1 riastrad * The above copyright notice and this permission notice (including the 24 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions 25 1.1 riastrad * of the Software. 26 1.1 riastrad * 27 1.1 riastrad */ 28 1.1 riastrad /* 29 1.1 riastrad * Authors: 30 1.1 riastrad * Christian Knig <christian.koenig (at) amd.com> 31 1.1 riastrad */ 32 1.1 riastrad 33 1.1 riastrad #include <sys/cdefs.h> 34 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_sync.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $"); 35 1.1 riastrad 36 1.1 riastrad #include "radeon.h" 37 1.1 riastrad #include "radeon_trace.h" 38 1.1 riastrad 39 1.1 riastrad /** 40 1.1 riastrad * radeon_sync_create - zero init sync object 41 1.1 riastrad * 42 1.1 riastrad * @sync: sync object to initialize 43 1.1 riastrad * 44 1.1 riastrad * Just clear the sync object for now. 45 1.1 riastrad */ 46 1.1 riastrad void radeon_sync_create(struct radeon_sync *sync) 47 1.1 riastrad { 48 1.1 riastrad unsigned i; 49 1.1 riastrad 50 1.1 riastrad for (i = 0; i < RADEON_NUM_SYNCS; ++i) 51 1.1 riastrad sync->semaphores[i] = NULL; 52 1.1 riastrad 53 1.1 riastrad for (i = 0; i < RADEON_NUM_RINGS; ++i) 54 1.1 riastrad sync->sync_to[i] = NULL; 55 1.1 riastrad 56 1.1 riastrad sync->last_vm_update = NULL; 57 1.1 riastrad } 58 1.1 riastrad 59 1.1 riastrad /** 60 1.1 riastrad * radeon_sync_fence - use the semaphore to sync to a fence 61 1.1 riastrad * 62 1.1 riastrad * @sync: sync object to add fence to 63 1.1 riastrad * @fence: fence to sync to 64 1.1 riastrad * 65 1.1 riastrad * Sync to the fence using the semaphore objects 66 1.1 riastrad */ 67 1.1 riastrad void radeon_sync_fence(struct radeon_sync *sync, 68 1.1 riastrad struct radeon_fence *fence) 69 1.1 riastrad { 70 1.1 riastrad struct radeon_fence *other; 71 1.1 riastrad 72 1.1 riastrad if (!fence) 73 1.1 riastrad return; 74 1.1 riastrad 75 1.1 riastrad other = sync->sync_to[fence->ring]; 76 1.1 riastrad sync->sync_to[fence->ring] = radeon_fence_later(fence, other); 77 1.1 riastrad 78 1.1 riastrad if (fence->is_vm_update) { 79 1.1 riastrad other = sync->last_vm_update; 80 1.1 riastrad sync->last_vm_update = radeon_fence_later(fence, other); 81 1.1 riastrad } 82 1.1 riastrad } 83 1.1 riastrad 84 1.1 riastrad /** 85 1.1 riastrad * radeon_sync_resv - use the semaphores to sync to a reservation object 86 1.1 riastrad * 87 1.1 riastrad * @sync: sync object to add fences from reservation object to 88 1.1 riastrad * @resv: reservation object with embedded fence 89 1.1 riastrad * @shared: true if we should only sync to the exclusive fence 90 1.1 riastrad * 91 1.1 riastrad * Sync to the fence using the semaphore objects 92 1.1 riastrad */ 93 1.1 riastrad int radeon_sync_resv(struct radeon_device *rdev, 94 1.1 riastrad struct radeon_sync *sync, 95 1.3 riastrad struct dma_resv *resv, 96 1.1 riastrad bool shared) 97 1.1 riastrad { 98 1.3 riastrad struct dma_resv_list *flist; 99 1.3 riastrad struct dma_fence *f; 100 1.1 riastrad struct radeon_fence *fence; 101 1.1 riastrad unsigned i; 102 1.1 riastrad int r = 0; 103 1.1 riastrad 104 1.1 riastrad /* always sync to the exclusive fence */ 105 1.3 riastrad f = dma_resv_get_excl(resv); 106 1.1 riastrad fence = f ? to_radeon_fence(f) : NULL; 107 1.1 riastrad if (fence && fence->rdev == rdev) 108 1.1 riastrad radeon_sync_fence(sync, fence); 109 1.1 riastrad else if (f) 110 1.3 riastrad r = dma_fence_wait(f, true); 111 1.1 riastrad 112 1.3 riastrad flist = dma_resv_get_list(resv); 113 1.1 riastrad if (shared || !flist || r) 114 1.1 riastrad return r; 115 1.1 riastrad 116 1.1 riastrad for (i = 0; i < flist->shared_count; ++i) { 117 1.1 riastrad f = rcu_dereference_protected(flist->shared[i], 118 1.3 riastrad dma_resv_held(resv)); 119 1.1 riastrad fence = to_radeon_fence(f); 120 1.1 riastrad if (fence && fence->rdev == rdev) 121 1.1 riastrad radeon_sync_fence(sync, fence); 122 1.1 riastrad else 123 1.3 riastrad r = dma_fence_wait(f, true); 124 1.1 riastrad 125 1.1 riastrad if (r) 126 1.1 riastrad break; 127 1.1 riastrad } 128 1.1 riastrad return r; 129 1.1 riastrad } 130 1.1 riastrad 131 1.1 riastrad /** 132 1.1 riastrad * radeon_sync_rings - sync ring to all registered fences 133 1.1 riastrad * 134 1.1 riastrad * @rdev: radeon_device pointer 135 1.1 riastrad * @sync: sync object to use 136 1.1 riastrad * @ring: ring that needs sync 137 1.1 riastrad * 138 1.1 riastrad * Ensure that all registered fences are signaled before letting 139 1.1 riastrad * the ring continue. The caller must hold the ring lock. 140 1.1 riastrad */ 141 1.1 riastrad int radeon_sync_rings(struct radeon_device *rdev, 142 1.1 riastrad struct radeon_sync *sync, 143 1.1 riastrad int ring) 144 1.1 riastrad { 145 1.1 riastrad unsigned count = 0; 146 1.1 riastrad int i, r; 147 1.1 riastrad 148 1.1 riastrad for (i = 0; i < RADEON_NUM_RINGS; ++i) { 149 1.1 riastrad struct radeon_fence *fence = sync->sync_to[i]; 150 1.1 riastrad struct radeon_semaphore *semaphore; 151 1.1 riastrad 152 1.1 riastrad /* check if we really need to sync */ 153 1.1 riastrad if (!radeon_fence_need_sync(fence, ring)) 154 1.1 riastrad continue; 155 1.1 riastrad 156 1.1 riastrad /* prevent GPU deadlocks */ 157 1.1 riastrad if (!rdev->ring[i].ready) { 158 1.1 riastrad dev_err(rdev->dev, "Syncing to a disabled ring!"); 159 1.1 riastrad return -EINVAL; 160 1.1 riastrad } 161 1.1 riastrad 162 1.1 riastrad if (count >= RADEON_NUM_SYNCS) { 163 1.1 riastrad /* not enough room, wait manually */ 164 1.1 riastrad r = radeon_fence_wait(fence, false); 165 1.1 riastrad if (r) 166 1.1 riastrad return r; 167 1.1 riastrad continue; 168 1.1 riastrad } 169 1.1 riastrad r = radeon_semaphore_create(rdev, &semaphore); 170 1.1 riastrad if (r) 171 1.1 riastrad return r; 172 1.1 riastrad 173 1.1 riastrad sync->semaphores[count++] = semaphore; 174 1.1 riastrad 175 1.1 riastrad /* allocate enough space for sync command */ 176 1.1 riastrad r = radeon_ring_alloc(rdev, &rdev->ring[i], 16); 177 1.1 riastrad if (r) 178 1.1 riastrad return r; 179 1.1 riastrad 180 1.1 riastrad /* emit the signal semaphore */ 181 1.1 riastrad if (!radeon_semaphore_emit_signal(rdev, i, semaphore)) { 182 1.1 riastrad /* signaling wasn't successful wait manually */ 183 1.1 riastrad radeon_ring_undo(&rdev->ring[i]); 184 1.1 riastrad r = radeon_fence_wait(fence, false); 185 1.1 riastrad if (r) 186 1.1 riastrad return r; 187 1.1 riastrad continue; 188 1.1 riastrad } 189 1.1 riastrad 190 1.1 riastrad /* we assume caller has already allocated space on waiters ring */ 191 1.1 riastrad if (!radeon_semaphore_emit_wait(rdev, ring, semaphore)) { 192 1.1 riastrad /* waiting wasn't successful wait manually */ 193 1.1 riastrad radeon_ring_undo(&rdev->ring[i]); 194 1.1 riastrad r = radeon_fence_wait(fence, false); 195 1.1 riastrad if (r) 196 1.1 riastrad return r; 197 1.1 riastrad continue; 198 1.1 riastrad } 199 1.1 riastrad 200 1.1 riastrad radeon_ring_commit(rdev, &rdev->ring[i], false); 201 1.1 riastrad radeon_fence_note_sync(fence, ring); 202 1.1 riastrad } 203 1.1 riastrad 204 1.1 riastrad return 0; 205 1.1 riastrad } 206 1.1 riastrad 207 1.1 riastrad /** 208 1.1 riastrad * radeon_sync_free - free the sync object 209 1.1 riastrad * 210 1.1 riastrad * @rdev: radeon_device pointer 211 1.1 riastrad * @sync: sync object to use 212 1.1 riastrad * @fence: fence to use for the free 213 1.1 riastrad * 214 1.1 riastrad * Free the sync object by freeing all semaphores in it. 215 1.1 riastrad */ 216 1.1 riastrad void radeon_sync_free(struct radeon_device *rdev, 217 1.1 riastrad struct radeon_sync *sync, 218 1.1 riastrad struct radeon_fence *fence) 219 1.1 riastrad { 220 1.1 riastrad unsigned i; 221 1.1 riastrad 222 1.1 riastrad for (i = 0; i < RADEON_NUM_SYNCS; ++i) 223 1.1 riastrad radeon_semaphore_free(rdev, &sync->semaphores[i], fence); 224 1.1 riastrad } 225