1 1.1 riastrad /* $NetBSD: radeon_ib.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 2008 Advanced Micro Devices, Inc. 5 1.1 riastrad * Copyright 2008 Red Hat Inc. 6 1.1 riastrad * Copyright 2009 Jerome Glisse. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice shall be included in 16 1.1 riastrad * all copies or substantial portions of the Software. 17 1.1 riastrad * 18 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 25 1.1 riastrad * 26 1.1 riastrad * Authors: Dave Airlie 27 1.1 riastrad * Alex Deucher 28 1.1 riastrad * Jerome Glisse 29 1.1 riastrad * Christian Knig 30 1.1 riastrad */ 31 1.3 riastrad 32 1.1 riastrad #include <sys/cdefs.h> 33 1.1 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_ib.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $"); 34 1.1 riastrad 35 1.3 riastrad #include <drm/drm_debugfs.h> 36 1.3 riastrad #include <drm/drm_file.h> 37 1.3 riastrad 38 1.1 riastrad #include "radeon.h" 39 1.1 riastrad 40 1.1 riastrad /* 41 1.1 riastrad * IB 42 1.1 riastrad * IBs (Indirect Buffers) and areas of GPU accessible memory where 43 1.1 riastrad * commands are stored. You can put a pointer to the IB in the 44 1.1 riastrad * command ring and the hw will fetch the commands from the IB 45 1.1 riastrad * and execute them. Generally userspace acceleration drivers 46 1.1 riastrad * produce command buffers which are send to the kernel and 47 1.1 riastrad * put in IBs for execution by the requested ring. 48 1.1 riastrad */ 49 1.1 riastrad static int radeon_debugfs_sa_init(struct radeon_device *rdev); 50 1.1 riastrad 51 1.1 riastrad /** 52 1.1 riastrad * radeon_ib_get - request an IB (Indirect Buffer) 53 1.1 riastrad * 54 1.1 riastrad * @rdev: radeon_device pointer 55 1.1 riastrad * @ring: ring index the IB is associated with 56 1.1 riastrad * @ib: IB object returned 57 1.1 riastrad * @size: requested IB size 58 1.1 riastrad * 59 1.1 riastrad * Request an IB (all asics). IBs are allocated using the 60 1.1 riastrad * suballocator. 61 1.1 riastrad * Returns 0 on success, error on failure. 62 1.1 riastrad */ 63 1.1 riastrad int radeon_ib_get(struct radeon_device *rdev, int ring, 64 1.1 riastrad struct radeon_ib *ib, struct radeon_vm *vm, 65 1.1 riastrad unsigned size) 66 1.1 riastrad { 67 1.1 riastrad int r; 68 1.1 riastrad 69 1.1 riastrad r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256); 70 1.1 riastrad if (r) { 71 1.1 riastrad dev_err(rdev->dev, "failed to get a new IB (%d)\n", r); 72 1.1 riastrad return r; 73 1.1 riastrad } 74 1.1 riastrad 75 1.1 riastrad radeon_sync_create(&ib->sync); 76 1.1 riastrad 77 1.1 riastrad ib->ring = ring; 78 1.1 riastrad ib->fence = NULL; 79 1.1 riastrad ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo); 80 1.1 riastrad ib->vm = vm; 81 1.1 riastrad if (vm) { 82 1.1 riastrad /* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address 83 1.1 riastrad * space and soffset is the offset inside the pool bo 84 1.1 riastrad */ 85 1.1 riastrad ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET; 86 1.1 riastrad } else { 87 1.1 riastrad ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo); 88 1.1 riastrad } 89 1.1 riastrad ib->is_const_ib = false; 90 1.1 riastrad 91 1.1 riastrad return 0; 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad /** 95 1.1 riastrad * radeon_ib_free - free an IB (Indirect Buffer) 96 1.1 riastrad * 97 1.1 riastrad * @rdev: radeon_device pointer 98 1.1 riastrad * @ib: IB object to free 99 1.1 riastrad * 100 1.1 riastrad * Free an IB (all asics). 101 1.1 riastrad */ 102 1.1 riastrad void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib) 103 1.1 riastrad { 104 1.1 riastrad radeon_sync_free(rdev, &ib->sync, ib->fence); 105 1.1 riastrad radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence); 106 1.1 riastrad radeon_fence_unref(&ib->fence); 107 1.1 riastrad } 108 1.1 riastrad 109 1.1 riastrad /** 110 1.1 riastrad * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring 111 1.1 riastrad * 112 1.1 riastrad * @rdev: radeon_device pointer 113 1.1 riastrad * @ib: IB object to schedule 114 1.1 riastrad * @const_ib: Const IB to schedule (SI only) 115 1.1 riastrad * @hdp_flush: Whether or not to perform an HDP cache flush 116 1.1 riastrad * 117 1.1 riastrad * Schedule an IB on the associated ring (all asics). 118 1.1 riastrad * Returns 0 on success, error on failure. 119 1.1 riastrad * 120 1.1 riastrad * On SI, there are two parallel engines fed from the primary ring, 121 1.1 riastrad * the CE (Constant Engine) and the DE (Drawing Engine). Since 122 1.1 riastrad * resource descriptors have moved to memory, the CE allows you to 123 1.1 riastrad * prime the caches while the DE is updating register state so that 124 1.1 riastrad * the resource descriptors will be already in cache when the draw is 125 1.1 riastrad * processed. To accomplish this, the userspace driver submits two 126 1.1 riastrad * IBs, one for the CE and one for the DE. If there is a CE IB (called 127 1.1 riastrad * a CONST_IB), it will be put on the ring prior to the DE IB. Prior 128 1.1 riastrad * to SI there was just a DE IB. 129 1.1 riastrad */ 130 1.1 riastrad int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib, 131 1.1 riastrad struct radeon_ib *const_ib, bool hdp_flush) 132 1.1 riastrad { 133 1.1 riastrad struct radeon_ring *ring = &rdev->ring[ib->ring]; 134 1.1 riastrad int r = 0; 135 1.1 riastrad 136 1.1 riastrad if (!ib->length_dw || !ring->ready) { 137 1.1 riastrad /* TODO: Nothings in the ib we should report. */ 138 1.1 riastrad dev_err(rdev->dev, "couldn't schedule ib\n"); 139 1.1 riastrad return -EINVAL; 140 1.1 riastrad } 141 1.1 riastrad 142 1.1 riastrad /* 64 dwords should be enough for fence too */ 143 1.1 riastrad r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8); 144 1.1 riastrad if (r) { 145 1.1 riastrad dev_err(rdev->dev, "scheduling IB failed (%d).\n", r); 146 1.1 riastrad return r; 147 1.1 riastrad } 148 1.1 riastrad 149 1.1 riastrad /* grab a vm id if necessary */ 150 1.1 riastrad if (ib->vm) { 151 1.1 riastrad struct radeon_fence *vm_id_fence; 152 1.1 riastrad vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring); 153 1.1 riastrad radeon_sync_fence(&ib->sync, vm_id_fence); 154 1.1 riastrad } 155 1.1 riastrad 156 1.1 riastrad /* sync with other rings */ 157 1.1 riastrad r = radeon_sync_rings(rdev, &ib->sync, ib->ring); 158 1.1 riastrad if (r) { 159 1.1 riastrad dev_err(rdev->dev, "failed to sync rings (%d)\n", r); 160 1.1 riastrad radeon_ring_unlock_undo(rdev, ring); 161 1.1 riastrad return r; 162 1.1 riastrad } 163 1.1 riastrad 164 1.1 riastrad if (ib->vm) 165 1.1 riastrad radeon_vm_flush(rdev, ib->vm, ib->ring, 166 1.1 riastrad ib->sync.last_vm_update); 167 1.1 riastrad 168 1.1 riastrad if (const_ib) { 169 1.1 riastrad radeon_ring_ib_execute(rdev, const_ib->ring, const_ib); 170 1.1 riastrad radeon_sync_free(rdev, &const_ib->sync, NULL); 171 1.1 riastrad } 172 1.1 riastrad radeon_ring_ib_execute(rdev, ib->ring, ib); 173 1.1 riastrad r = radeon_fence_emit(rdev, &ib->fence, ib->ring); 174 1.1 riastrad if (r) { 175 1.1 riastrad dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r); 176 1.1 riastrad radeon_ring_unlock_undo(rdev, ring); 177 1.1 riastrad return r; 178 1.1 riastrad } 179 1.1 riastrad if (const_ib) { 180 1.1 riastrad const_ib->fence = radeon_fence_ref(ib->fence); 181 1.1 riastrad } 182 1.1 riastrad 183 1.1 riastrad if (ib->vm) 184 1.1 riastrad radeon_vm_fence(rdev, ib->vm, ib->fence); 185 1.1 riastrad 186 1.1 riastrad radeon_ring_unlock_commit(rdev, ring, hdp_flush); 187 1.1 riastrad return 0; 188 1.1 riastrad } 189 1.1 riastrad 190 1.1 riastrad /** 191 1.1 riastrad * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool 192 1.1 riastrad * 193 1.1 riastrad * @rdev: radeon_device pointer 194 1.1 riastrad * 195 1.1 riastrad * Initialize the suballocator to manage a pool of memory 196 1.1 riastrad * for use as IBs (all asics). 197 1.1 riastrad * Returns 0 on success, error on failure. 198 1.1 riastrad */ 199 1.1 riastrad int radeon_ib_pool_init(struct radeon_device *rdev) 200 1.1 riastrad { 201 1.1 riastrad int r; 202 1.1 riastrad 203 1.1 riastrad if (rdev->ib_pool_ready) { 204 1.1 riastrad return 0; 205 1.1 riastrad } 206 1.1 riastrad 207 1.1 riastrad if (rdev->family >= CHIP_BONAIRE) { 208 1.1 riastrad r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 209 1.1 riastrad RADEON_IB_POOL_SIZE*64*1024, 210 1.1 riastrad RADEON_GPU_PAGE_SIZE, 211 1.1 riastrad RADEON_GEM_DOMAIN_GTT, 212 1.1 riastrad RADEON_GEM_GTT_WC); 213 1.1 riastrad } else { 214 1.1 riastrad /* Before CIK, it's better to stick to cacheable GTT due 215 1.1 riastrad * to the command stream checking 216 1.1 riastrad */ 217 1.1 riastrad r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo, 218 1.1 riastrad RADEON_IB_POOL_SIZE*64*1024, 219 1.1 riastrad RADEON_GPU_PAGE_SIZE, 220 1.1 riastrad RADEON_GEM_DOMAIN_GTT, 0); 221 1.1 riastrad } 222 1.1 riastrad if (r) { 223 1.1 riastrad return r; 224 1.1 riastrad } 225 1.1 riastrad 226 1.1 riastrad r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo); 227 1.1 riastrad if (r) { 228 1.1 riastrad return r; 229 1.1 riastrad } 230 1.1 riastrad 231 1.1 riastrad rdev->ib_pool_ready = true; 232 1.1 riastrad if (radeon_debugfs_sa_init(rdev)) { 233 1.1 riastrad dev_err(rdev->dev, "failed to register debugfs file for SA\n"); 234 1.1 riastrad } 235 1.1 riastrad return 0; 236 1.1 riastrad } 237 1.1 riastrad 238 1.1 riastrad /** 239 1.1 riastrad * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool 240 1.1 riastrad * 241 1.1 riastrad * @rdev: radeon_device pointer 242 1.1 riastrad * 243 1.1 riastrad * Tear down the suballocator managing the pool of memory 244 1.1 riastrad * for use as IBs (all asics). 245 1.1 riastrad */ 246 1.1 riastrad void radeon_ib_pool_fini(struct radeon_device *rdev) 247 1.1 riastrad { 248 1.1 riastrad if (rdev->ib_pool_ready) { 249 1.1 riastrad radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo); 250 1.1 riastrad radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo); 251 1.1 riastrad rdev->ib_pool_ready = false; 252 1.1 riastrad } 253 1.1 riastrad } 254 1.1 riastrad 255 1.1 riastrad /** 256 1.1 riastrad * radeon_ib_ring_tests - test IBs on the rings 257 1.1 riastrad * 258 1.1 riastrad * @rdev: radeon_device pointer 259 1.1 riastrad * 260 1.1 riastrad * Test an IB (Indirect Buffer) on each ring. 261 1.1 riastrad * If the test fails, disable the ring. 262 1.1 riastrad * Returns 0 on success, error if the primary GFX ring 263 1.1 riastrad * IB test fails. 264 1.1 riastrad */ 265 1.1 riastrad int radeon_ib_ring_tests(struct radeon_device *rdev) 266 1.1 riastrad { 267 1.1 riastrad unsigned i; 268 1.1 riastrad int r; 269 1.1 riastrad 270 1.1 riastrad for (i = 0; i < RADEON_NUM_RINGS; ++i) { 271 1.1 riastrad struct radeon_ring *ring = &rdev->ring[i]; 272 1.1 riastrad 273 1.1 riastrad if (!ring->ready) 274 1.1 riastrad continue; 275 1.1 riastrad 276 1.1 riastrad r = radeon_ib_test(rdev, i, ring); 277 1.1 riastrad if (r) { 278 1.1 riastrad radeon_fence_driver_force_completion(rdev, i); 279 1.1 riastrad ring->ready = false; 280 1.1 riastrad rdev->needs_reset = false; 281 1.1 riastrad 282 1.1 riastrad if (i == RADEON_RING_TYPE_GFX_INDEX) { 283 1.1 riastrad /* oh, oh, that's really bad */ 284 1.1 riastrad DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r); 285 1.3 riastrad rdev->accel_working = false; 286 1.1 riastrad return r; 287 1.1 riastrad 288 1.1 riastrad } else { 289 1.1 riastrad /* still not good, but we can live with it */ 290 1.1 riastrad DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r); 291 1.1 riastrad } 292 1.1 riastrad } 293 1.1 riastrad } 294 1.1 riastrad return 0; 295 1.1 riastrad } 296 1.1 riastrad 297 1.1 riastrad /* 298 1.1 riastrad * Debugfs info 299 1.1 riastrad */ 300 1.1 riastrad #if defined(CONFIG_DEBUG_FS) 301 1.1 riastrad 302 1.1 riastrad static int radeon_debugfs_sa_info(struct seq_file *m, void *data) 303 1.1 riastrad { 304 1.1 riastrad struct drm_info_node *node = (struct drm_info_node *) m->private; 305 1.1 riastrad struct drm_device *dev = node->minor->dev; 306 1.1 riastrad struct radeon_device *rdev = dev->dev_private; 307 1.1 riastrad 308 1.1 riastrad radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m); 309 1.1 riastrad 310 1.1 riastrad return 0; 311 1.1 riastrad 312 1.1 riastrad } 313 1.1 riastrad 314 1.1 riastrad static struct drm_info_list radeon_debugfs_sa_list[] = { 315 1.3 riastrad {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL}, 316 1.1 riastrad }; 317 1.1 riastrad 318 1.1 riastrad #endif 319 1.1 riastrad 320 1.1 riastrad static int radeon_debugfs_sa_init(struct radeon_device *rdev) 321 1.1 riastrad { 322 1.1 riastrad #if defined(CONFIG_DEBUG_FS) 323 1.1 riastrad return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1); 324 1.1 riastrad #else 325 1.1 riastrad return 0; 326 1.1 riastrad #endif 327 1.1 riastrad } 328