1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 * Christian König 28 */ 29 #include <linux/seq_file.h> 30 #include <drm/amdgpu_drm.h> 31 32 #include "amdgpu.h" 33 #include "atom.h" 34 #include "amdgpu_trace.h" 35 36 #define AMDGPU_IB_TEST_TIMEOUT msecs_to_jiffies(1000) 37 #define AMDGPU_IB_TEST_GFX_XGMI_TIMEOUT msecs_to_jiffies(2000) 38 39 /* 40 * IB 41 * IBs (Indirect Buffers) and areas of GPU accessible memory where 42 * commands are stored. You can put a pointer to the IB in the 43 * command ring and the hw will fetch the commands from the IB 44 * and execute them. Generally userspace acceleration drivers 45 * produce command buffers which are send to the kernel and 46 * put in IBs for execution by the requested ring. 47 */ 48 49 /** 50 * amdgpu_ib_get - request an IB (Indirect Buffer) 51 * 52 * @adev: amdgpu_device pointer 53 * @vm: amdgpu_vm pointer 54 * @size: requested IB size 55 * @pool_type: IB pool type (delayed, immediate, direct) 56 * @ib: IB object returned 57 * 58 * Request an IB (all asics). IBs are allocated using the 59 * suballocator. 60 * Returns 0 on success, error on failure. 61 */ 62 int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm, 63 unsigned int size, enum amdgpu_ib_pool_type pool_type, 64 struct amdgpu_ib *ib) 65 { 66 int r; 67 68 if (size) { 69 r = amdgpu_sa_bo_new(&adev->ib_pools[pool_type], 70 &ib->sa_bo, size); 71 if (r) { 72 dev_err(adev->dev, "failed to get a new IB (%d)\n", r); 73 return r; 74 } 75 76 ib->ptr = amdgpu_sa_bo_cpu_addr(ib->sa_bo); 77 /* flush the cache before commit the IB */ 78 ib->flags = AMDGPU_IB_FLAG_EMIT_MEM_SYNC; 79 80 if (!vm) 81 ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo); 82 } 83 84 return 0; 85 } 86 87 /** 88 * amdgpu_ib_free - free an IB (Indirect Buffer) 89 * 90 * @ib: IB object to free 91 * @f: the fence SA bo need wait on for the ib alloation 92 * 93 * Free an IB (all asics). 94 */ 95 void amdgpu_ib_free(struct amdgpu_ib *ib, struct dma_fence *f) 96 { 97 amdgpu_sa_bo_free(&ib->sa_bo, f); 98 } 99 100 /** 101 * amdgpu_ib_schedule - schedule an IB (Indirect Buffer) on the ring 102 * 103 * @ring: ring structure the IB is associated with 104 * @job: job to schedule 105 * @f: fence referenced during this submission 106 * 107 * Schedule an IB on the associated ring (all asics). 108 * Returns 0 on success, error on failure. 109 * 110 * On SI, there are two parallel engines fed from the primary ring, 111 * the CE (Constant Engine) and the DE (Drawing Engine). Since 112 * resource descriptors have moved to memory, the CE allows you to 113 * prime the caches while the DE is updating register state so that 114 * the resource descriptors will be already in cache when the draw is 115 * processed. To accomplish this, the userspace driver submits two 116 * IBs, one for the CE and one for the DE. If there is a CE IB (called 117 * a CONST_IB), it will be put on the ring prior to the DE IB. Prior 118 * to SI there was just a DE IB. 119 */ 120 int amdgpu_ib_schedule(struct amdgpu_ring *ring, struct amdgpu_job *job, 121 struct dma_fence **f) 122 { 123 struct amdgpu_device *adev = ring->adev; 124 struct amdgpu_ib *ib; 125 struct dma_fence *tmp = NULL; 126 struct amdgpu_fence *af; 127 struct amdgpu_fence *vm_af; 128 bool need_ctx_switch; 129 bool emit_spm_needed = false; 130 bool emit_gds_needed = false; 131 uint64_t fence_ctx; 132 uint32_t status = 0, alloc_size; 133 unsigned int fence_flags = 0; 134 bool secure; 135 unsigned int vmid; 136 bool need_pipe_sync = false; 137 unsigned int cond_exec; 138 unsigned int i; 139 int r; 140 141 if (!job || !job->num_ibs) 142 return -EINVAL; 143 144 vmid = AMDGPU_JOB_GET_VMID(job); 145 ib = &job->ibs[0]; 146 fence_ctx = job->base.s_fence ? 147 job->base.s_fence->finished.context : 0; 148 af = job->hw_fence; 149 vm_af = job->hw_vm_fence; 150 /* Save the context of the job for reset handling. 151 * The driver needs this so it can skip the ring 152 * contents for guilty contexts. 153 */ 154 af->context = fence_ctx; 155 /* the vm fence is also part of the job's context */ 156 vm_af->context = fence_ctx; 157 158 if (!ring->sched.ready) { 159 dev_err(adev->dev, "couldn't schedule ib on ring <%s>\n", ring->name); 160 return -EINVAL; 161 } 162 163 if (job->vm && !job->vmid) { 164 dev_err(adev->dev, "VM IB without ID\n"); 165 return -EINVAL; 166 } 167 168 if ((ib->flags & AMDGPU_IB_FLAGS_SECURE) && 169 (!ring->funcs->secure_submission_supported)) { 170 dev_err(adev->dev, "secure submissions not supported on ring <%s>\n", ring->name); 171 return -EINVAL; 172 } 173 174 alloc_size = ring->funcs->emit_frame_size + job->num_ibs * 175 ring->funcs->emit_ib_size; 176 177 r = amdgpu_ring_alloc(ring, alloc_size); 178 if (r) { 179 dev_err(adev->dev, "scheduling IB failed (%d).\n", r); 180 return r; 181 } 182 183 need_ctx_switch = ring->current_ctx != fence_ctx; 184 if (ring->funcs->emit_pipeline_sync && 185 ((tmp = amdgpu_sync_get_fence(&job->explicit_sync)) || 186 need_ctx_switch || amdgpu_vm_need_pipeline_sync(ring, job))) { 187 188 need_pipe_sync = true; 189 190 if (tmp) 191 trace_amdgpu_ib_pipe_sync(job, tmp); 192 193 dma_fence_put(tmp); 194 } 195 196 /* VM sequence */ 197 vm_af->ib_wptr = ring->wptr; 198 amdgpu_vm_flush(ring, job, &need_pipe_sync, &emit_spm_needed, 199 &emit_gds_needed); 200 vm_af->ib_dw_size = 201 amdgpu_ring_get_dw_distance(ring, vm_af->ib_wptr, ring->wptr); 202 203 /* IB sequence */ 204 af->ib_wptr = ring->wptr; 205 amdgpu_ring_ib_begin(ring); 206 207 if (ring->funcs->insert_start) 208 ring->funcs->insert_start(ring); 209 210 /* this may have been handled by amdgpu_vm_flush */ 211 if (need_pipe_sync) 212 amdgpu_ring_emit_pipeline_sync(ring); 213 214 if (emit_spm_needed) 215 adev->gfx.rlc.funcs->update_spm_vmid(adev, ring->xcc_id, ring, job->vmid); 216 217 if (emit_gds_needed) 218 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, 219 job->gds_size, job->gws_base, 220 job->gws_size, job->oa_base, 221 job->oa_size); 222 223 if ((ib->flags & AMDGPU_IB_FLAG_EMIT_MEM_SYNC) && ring->funcs->emit_mem_sync) 224 ring->funcs->emit_mem_sync(ring); 225 226 if (ring->funcs->emit_wave_limit && 227 ring->hw_prio == AMDGPU_GFX_PIPE_PRIO_HIGH) 228 ring->funcs->emit_wave_limit(ring, true); 229 230 if (ring->funcs->emit_gfx_shadow && adev->gfx.cp_gfx_shadow) 231 amdgpu_ring_emit_gfx_shadow(ring, job->shadow_va, job->csa_va, job->gds_va, 232 job->init_shadow, vmid); 233 234 if (ring->funcs->init_cond_exec) 235 cond_exec = amdgpu_ring_init_cond_exec(ring, 236 ring->cond_exe_gpu_addr); 237 238 /* Skip the IB for guilty contexts */ 239 af->skip_ib_dw_start_offset = 240 amdgpu_ring_get_dw_distance(ring, af->ib_wptr, ring->wptr); 241 amdgpu_device_flush_hdp(adev, ring); 242 243 if (need_ctx_switch) 244 status |= AMDGPU_HAVE_CTX_SWITCH; 245 246 if (ring->funcs->emit_cntxcntl) { 247 status |= job->preamble_status; 248 status |= job->preemption_status; 249 amdgpu_ring_emit_cntxcntl(ring, status); 250 } 251 252 /* Setup initial TMZiness and send it off. 253 */ 254 secure = false; 255 if (ring->funcs->emit_frame_cntl) { 256 secure = ib->flags & AMDGPU_IB_FLAGS_SECURE; 257 amdgpu_ring_emit_frame_cntl(ring, true, secure); 258 } 259 260 for (i = 0; i < job->num_ibs; ++i) { 261 ib = &job->ibs[i]; 262 263 if (ring->funcs->emit_frame_cntl) { 264 if (secure != !!(ib->flags & AMDGPU_IB_FLAGS_SECURE)) { 265 amdgpu_ring_emit_frame_cntl(ring, false, secure); 266 secure = !secure; 267 amdgpu_ring_emit_frame_cntl(ring, true, secure); 268 } 269 } 270 271 amdgpu_ring_emit_ib(ring, job, ib, status); 272 status &= ~AMDGPU_HAVE_CTX_SWITCH; 273 } 274 275 if (ring->funcs->emit_frame_cntl) 276 amdgpu_ring_emit_frame_cntl(ring, false, secure); 277 278 amdgpu_device_invalidate_hdp(adev, ring); 279 /* Skip the IB for guilty contexts */ 280 af->skip_ib_dw_end_offset = 281 amdgpu_ring_get_dw_distance(ring, af->ib_wptr, ring->wptr); 282 283 if (ib->flags & AMDGPU_IB_FLAG_TC_WB_NOT_INVALIDATE) 284 fence_flags |= AMDGPU_FENCE_FLAG_TC_WB_ONLY; 285 286 /* wrap the last IB with fence */ 287 if (job->uf_addr) { 288 amdgpu_ring_emit_fence(ring, job->uf_addr, job->uf_sequence, 289 fence_flags | AMDGPU_FENCE_FLAG_64BIT); 290 } 291 292 if (ring->funcs->emit_gfx_shadow && ring->funcs->init_cond_exec && 293 adev->gfx.cp_gfx_shadow) { 294 amdgpu_ring_emit_gfx_shadow(ring, 0, 0, 0, false, 0); 295 amdgpu_ring_init_cond_exec(ring, ring->cond_exe_gpu_addr); 296 } 297 298 amdgpu_fence_emit(ring, af, fence_flags); 299 *f = &af->base; 300 /* get a ref for the job */ 301 dma_fence_get(*f); 302 303 if (ring->funcs->insert_end) 304 ring->funcs->insert_end(ring); 305 306 amdgpu_ring_patch_cond_exec(ring, cond_exec); 307 308 ring->current_ctx = fence_ctx; 309 if (ring->funcs->emit_switch_buffer) 310 amdgpu_ring_emit_switch_buffer(ring); 311 312 if (ring->funcs->emit_wave_limit && 313 ring->hw_prio == AMDGPU_GFX_PIPE_PRIO_HIGH) 314 ring->funcs->emit_wave_limit(ring, false); 315 316 amdgpu_ring_ib_end(ring); 317 318 af->ib_dw_size = amdgpu_ring_get_dw_distance(ring, af->ib_wptr, ring->wptr); 319 320 amdgpu_ring_commit(ring); 321 322 return 0; 323 } 324 325 /** 326 * amdgpu_ib_pool_init - Init the IB (Indirect Buffer) pool 327 * 328 * @adev: amdgpu_device pointer 329 * 330 * Initialize the suballocator to manage a pool of memory 331 * for use as IBs (all asics). 332 * Returns 0 on success, error on failure. 333 */ 334 int amdgpu_ib_pool_init(struct amdgpu_device *adev) 335 { 336 const int sizes[AMDGPU_IB_POOL_MAX] = { 337 [AMDGPU_IB_POOL_DELAYED] = SZ_1M, 338 [AMDGPU_IB_POOL_IMMEDIATE] = SZ_128K, 339 [AMDGPU_IB_POOL_DIRECT] = SZ_512K 340 }; 341 const gfp_t gfp_flags[AMDGPU_IB_POOL_MAX] = { 342 /* 343 * For normal page table updates and recoverable retry faults 344 * (for SVM), further restricted by the VM eviction lock to not 345 * wait for memory reclaim. 346 */ 347 [AMDGPU_IB_POOL_DELAYED] = GFP_KERNEL, 348 /* 349 * For redirecting unrecoverable retry faults to the dummy page 350 * or set the PRT bits. dma_fence submissions might depend on 351 * that so we need the emmergency reserves. 352 */ 353 [AMDGPU_IB_POOL_IMMEDIATE] = GFP_ATOMIC, 354 /* 355 * For IB tests during GPU resets. Only very small and temporary 356 * allocation to allow dma_fences to signal. 357 */ 358 [AMDGPU_IB_POOL_DIRECT] = GFP_ATOMIC 359 }; 360 int r, i; 361 362 if (adev->ib_pool_ready) 363 return 0; 364 365 for (i = 0; i < AMDGPU_IB_POOL_MAX; i++) { 366 r = amdgpu_sa_bo_manager_init(adev, &adev->ib_pools[i], 367 sizes[i], gfp_flags[i]); 368 if (r) 369 goto error; 370 } 371 adev->ib_pool_ready = true; 372 373 return 0; 374 375 error: 376 while (i--) 377 amdgpu_sa_bo_manager_fini(adev, &adev->ib_pools[i]); 378 return r; 379 } 380 381 /** 382 * amdgpu_ib_pool_fini - Free the IB (Indirect Buffer) pool 383 * 384 * @adev: amdgpu_device pointer 385 * 386 * Tear down the suballocator managing the pool of memory 387 * for use as IBs (all asics). 388 */ 389 void amdgpu_ib_pool_fini(struct amdgpu_device *adev) 390 { 391 int i; 392 393 if (!adev->ib_pool_ready) 394 return; 395 396 for (i = 0; i < AMDGPU_IB_POOL_MAX; i++) 397 amdgpu_sa_bo_manager_fini(adev, &adev->ib_pools[i]); 398 adev->ib_pool_ready = false; 399 } 400 401 /** 402 * amdgpu_ib_pool_gfp_flags - Returns the gfp flags to use for each pool 403 * @adev: amdgpu device pointer 404 * @type: the IB pool type 405 */ 406 gfp_t amdgpu_ib_pool_gfp_flags(struct amdgpu_device *adev, 407 enum amdgpu_ib_pool_type type) 408 { 409 return adev->ib_pools[type].gfp_flags; 410 } 411 412 /** 413 * amdgpu_ib_ring_tests - test IBs on the rings 414 * 415 * @adev: amdgpu_device pointer 416 * 417 * Test an IB (Indirect Buffer) on each ring. 418 * If the test fails, disable the ring. 419 * Returns 0 on success, error if the primary GFX ring 420 * IB test fails. 421 */ 422 int amdgpu_ib_ring_tests(struct amdgpu_device *adev) 423 { 424 long tmo_gfx, tmo_mm; 425 int r, ret = 0; 426 unsigned int i; 427 428 tmo_mm = tmo_gfx = AMDGPU_IB_TEST_TIMEOUT; 429 if (amdgpu_sriov_vf(adev)) { 430 /* for MM engines in hypervisor side they are not scheduled together 431 * with CP and SDMA engines, so even in exclusive mode MM engine could 432 * still running on other VF thus the IB TEST TIMEOUT for MM engines 433 * under SR-IOV should be set to a long time. 8 sec should be enough 434 * for the MM comes back to this VF. 435 */ 436 tmo_mm = 8 * AMDGPU_IB_TEST_TIMEOUT; 437 } 438 439 if (amdgpu_sriov_runtime(adev)) { 440 /* for CP & SDMA engines since they are scheduled together so 441 * need to make the timeout width enough to cover the time 442 * cost waiting for it coming back under RUNTIME only 443 */ 444 tmo_gfx = 8 * AMDGPU_IB_TEST_TIMEOUT; 445 } else if (adev->gmc.xgmi.hive_id) { 446 tmo_gfx = AMDGPU_IB_TEST_GFX_XGMI_TIMEOUT; 447 } 448 449 for (i = 0; i < adev->num_rings; ++i) { 450 struct amdgpu_ring *ring = adev->rings[i]; 451 long tmo; 452 453 /* KIQ rings don't have an IB test because we never submit IBs 454 * to them and they have no interrupt support. 455 */ 456 if (!ring->sched.ready || !ring->funcs->test_ib) 457 continue; 458 459 if (adev->enable_mes && 460 ring->funcs->type == AMDGPU_RING_TYPE_KIQ) 461 continue; 462 463 /* MM engine need more time */ 464 if (ring->funcs->type == AMDGPU_RING_TYPE_UVD || 465 ring->funcs->type == AMDGPU_RING_TYPE_VCE || 466 ring->funcs->type == AMDGPU_RING_TYPE_UVD_ENC || 467 ring->funcs->type == AMDGPU_RING_TYPE_VCN_DEC || 468 ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC || 469 ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG) 470 tmo = tmo_mm; 471 else 472 tmo = tmo_gfx; 473 474 r = amdgpu_ring_test_ib(ring, tmo); 475 if (!r) { 476 DRM_DEV_DEBUG(adev->dev, "ib test on %s succeeded\n", 477 ring->name); 478 continue; 479 } 480 481 ring->sched.ready = false; 482 DRM_DEV_ERROR(adev->dev, "IB test failed on %s (%d).\n", 483 ring->name, r); 484 485 if (ring == &adev->gfx.gfx_ring[0]) { 486 /* oh, oh, that's really bad */ 487 adev->accel_working = false; 488 return r; 489 490 } else { 491 ret = r; 492 } 493 } 494 return ret; 495 } 496 497 /* 498 * Debugfs info 499 */ 500 #if defined(CONFIG_DEBUG_FS) 501 502 static int amdgpu_debugfs_sa_info_show(struct seq_file *m, void *unused) 503 { 504 struct amdgpu_device *adev = m->private; 505 506 seq_puts(m, "--------------------- DELAYED ---------------------\n"); 507 amdgpu_sa_bo_dump_debug_info(&adev->ib_pools[AMDGPU_IB_POOL_DELAYED], 508 m); 509 seq_puts(m, "-------------------- IMMEDIATE --------------------\n"); 510 amdgpu_sa_bo_dump_debug_info(&adev->ib_pools[AMDGPU_IB_POOL_IMMEDIATE], 511 m); 512 seq_puts(m, "--------------------- DIRECT ----------------------\n"); 513 amdgpu_sa_bo_dump_debug_info(&adev->ib_pools[AMDGPU_IB_POOL_DIRECT], m); 514 515 return 0; 516 } 517 518 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_sa_info); 519 520 #endif 521 522 void amdgpu_debugfs_sa_init(struct amdgpu_device *adev) 523 { 524 #if defined(CONFIG_DEBUG_FS) 525 struct drm_minor *minor = adev_to_drm(adev)->primary; 526 struct dentry *root = minor->debugfs_root; 527 528 debugfs_create_file("amdgpu_sa_info", 0444, root, adev, 529 &amdgpu_debugfs_sa_info_fops); 530 531 #endif 532 } 533