1 /* 2 * Copyright 2021 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 */ 23 24 #include <linux/firmware.h> 25 #include "amdgpu.h" 26 #include "amdgpu_vcn.h" 27 #include "amdgpu_pm.h" 28 #include "amdgpu_cs.h" 29 #include "soc15.h" 30 #include "soc15d.h" 31 #include "soc15_hw_ip.h" 32 #include "vcn_v2_0.h" 33 #include "mmsch_v4_0.h" 34 #include "vcn_v4_0.h" 35 36 #include "vcn/vcn_4_0_0_offset.h" 37 #include "vcn/vcn_4_0_0_sh_mask.h" 38 #include "ivsrcid/vcn/irqsrcs_vcn_4_0.h" 39 40 #include <drm/drm_drv.h> 41 42 #define mmUVD_DPG_LMA_CTL regUVD_DPG_LMA_CTL 43 #define mmUVD_DPG_LMA_CTL_BASE_IDX regUVD_DPG_LMA_CTL_BASE_IDX 44 #define mmUVD_DPG_LMA_DATA regUVD_DPG_LMA_DATA 45 #define mmUVD_DPG_LMA_DATA_BASE_IDX regUVD_DPG_LMA_DATA_BASE_IDX 46 47 #define VCN_VID_SOC_ADDRESS_2_0 0x1fb00 48 #define VCN1_VID_SOC_ADDRESS_3_0 0x48300 49 50 #define VCN_HARVEST_MMSCH 0 51 52 #define RDECODE_MSG_CREATE 0x00000000 53 #define RDECODE_MESSAGE_CREATE 0x00000001 54 55 static int amdgpu_ih_clientid_vcns[] = { 56 SOC15_IH_CLIENTID_VCN, 57 SOC15_IH_CLIENTID_VCN1 58 }; 59 60 static int vcn_v4_0_start_sriov(struct amdgpu_device *adev); 61 static void vcn_v4_0_set_unified_ring_funcs(struct amdgpu_device *adev); 62 static void vcn_v4_0_set_irq_funcs(struct amdgpu_device *adev); 63 static int vcn_v4_0_set_powergating_state(void *handle, 64 enum amd_powergating_state state); 65 static int vcn_v4_0_pause_dpg_mode(struct amdgpu_device *adev, 66 int inst_idx, struct dpg_pause_state *new_state); 67 static void vcn_v4_0_unified_ring_set_wptr(struct amdgpu_ring *ring); 68 static void vcn_v4_0_set_ras_funcs(struct amdgpu_device *adev); 69 70 /** 71 * vcn_v4_0_early_init - set function pointers 72 * 73 * @handle: amdgpu_device pointer 74 * 75 * Set ring and irq function pointers 76 */ 77 static int vcn_v4_0_early_init(void *handle) 78 { 79 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 80 81 if (amdgpu_sriov_vf(adev)) 82 adev->vcn.harvest_config = VCN_HARVEST_MMSCH; 83 84 /* re-use enc ring as unified ring */ 85 adev->vcn.num_enc_rings = 1; 86 87 vcn_v4_0_set_unified_ring_funcs(adev); 88 vcn_v4_0_set_irq_funcs(adev); 89 vcn_v4_0_set_ras_funcs(adev); 90 91 return 0; 92 } 93 94 /** 95 * vcn_v4_0_sw_init - sw init for VCN block 96 * 97 * @handle: amdgpu_device pointer 98 * 99 * Load firmware and sw initialization 100 */ 101 static int vcn_v4_0_sw_init(void *handle) 102 { 103 struct amdgpu_ring *ring; 104 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 105 int i, r; 106 107 r = amdgpu_vcn_sw_init(adev); 108 if (r) 109 return r; 110 111 amdgpu_vcn_setup_ucode(adev); 112 113 r = amdgpu_vcn_resume(adev); 114 if (r) 115 return r; 116 117 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 118 volatile struct amdgpu_vcn4_fw_shared *fw_shared; 119 120 if (adev->vcn.harvest_config & (1 << i)) 121 continue; 122 123 atomic_set(&adev->vcn.inst[i].sched_score, 0); 124 125 /* VCN UNIFIED TRAP */ 126 r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[i], 127 VCN_4_0__SRCID__UVD_ENC_GENERAL_PURPOSE, &adev->vcn.inst[i].irq); 128 if (r) 129 return r; 130 131 /* VCN POISON TRAP */ 132 r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[i], 133 VCN_4_0__SRCID_UVD_POISON, &adev->vcn.inst[i].irq); 134 if (r) 135 return r; 136 137 ring = &adev->vcn.inst[i].ring_enc[0]; 138 ring->use_doorbell = true; 139 if (amdgpu_sriov_vf(adev)) 140 ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) + i * (adev->vcn.num_enc_rings + 1) + 1; 141 else 142 ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) + 2 + 8 * i; 143 144 sprintf(ring->name, "vcn_unified_%d", i); 145 146 r = amdgpu_ring_init(adev, ring, 512, &adev->vcn.inst[i].irq, 0, 147 AMDGPU_RING_PRIO_0, &adev->vcn.inst[i].sched_score); 148 if (r) 149 return r; 150 151 fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr; 152 fw_shared->present_flag_0 = cpu_to_le32(AMDGPU_FW_SHARED_FLAG_0_UNIFIED_QUEUE); 153 fw_shared->sq.is_enabled = 1; 154 155 fw_shared->present_flag_0 |= cpu_to_le32(AMDGPU_VCN_SMU_DPM_INTERFACE_FLAG); 156 fw_shared->smu_dpm_interface.smu_interface_type = (adev->flags & AMD_IS_APU) ? 157 AMDGPU_VCN_SMU_DPM_INTERFACE_APU : AMDGPU_VCN_SMU_DPM_INTERFACE_DGPU; 158 159 if (amdgpu_sriov_vf(adev)) 160 fw_shared->present_flag_0 |= cpu_to_le32(AMDGPU_VCN_VF_RB_SETUP_FLAG); 161 162 if (amdgpu_vcnfw_log) 163 amdgpu_vcn_fwlog_init(&adev->vcn.inst[i]); 164 } 165 166 if (amdgpu_sriov_vf(adev)) { 167 r = amdgpu_virt_alloc_mm_table(adev); 168 if (r) 169 return r; 170 } 171 172 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) 173 adev->vcn.pause_dpg_mode = vcn_v4_0_pause_dpg_mode; 174 175 return 0; 176 } 177 178 /** 179 * vcn_v4_0_sw_fini - sw fini for VCN block 180 * 181 * @handle: amdgpu_device pointer 182 * 183 * VCN suspend and free up sw allocation 184 */ 185 static int vcn_v4_0_sw_fini(void *handle) 186 { 187 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 188 int i, r, idx; 189 190 if (drm_dev_enter(adev_to_drm(adev), &idx)) { 191 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 192 volatile struct amdgpu_vcn4_fw_shared *fw_shared; 193 194 if (adev->vcn.harvest_config & (1 << i)) 195 continue; 196 197 fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr; 198 fw_shared->present_flag_0 = 0; 199 fw_shared->sq.is_enabled = 0; 200 } 201 202 drm_dev_exit(idx); 203 } 204 205 if (amdgpu_sriov_vf(adev)) 206 amdgpu_virt_free_mm_table(adev); 207 208 r = amdgpu_vcn_suspend(adev); 209 if (r) 210 return r; 211 212 r = amdgpu_vcn_sw_fini(adev); 213 214 return r; 215 } 216 217 /** 218 * vcn_v4_0_hw_init - start and test VCN block 219 * 220 * @handle: amdgpu_device pointer 221 * 222 * Initialize the hardware, boot up the VCPU and do some testing 223 */ 224 static int vcn_v4_0_hw_init(void *handle) 225 { 226 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 227 struct amdgpu_ring *ring; 228 int i, r; 229 230 if (amdgpu_sriov_vf(adev)) { 231 r = vcn_v4_0_start_sriov(adev); 232 if (r) 233 goto done; 234 235 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 236 if (adev->vcn.harvest_config & (1 << i)) 237 continue; 238 239 ring = &adev->vcn.inst[i].ring_enc[0]; 240 if (amdgpu_vcn_is_disabled_vcn(adev, VCN_ENCODE_RING, i)) { 241 ring->sched.ready = false; 242 ring->no_scheduler = true; 243 dev_info(adev->dev, "ring %s is disabled by hypervisor\n", ring->name); 244 } else { 245 ring->wptr = 0; 246 ring->wptr_old = 0; 247 vcn_v4_0_unified_ring_set_wptr(ring); 248 ring->sched.ready = true; 249 } 250 } 251 } else { 252 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 253 if (adev->vcn.harvest_config & (1 << i)) 254 continue; 255 256 ring = &adev->vcn.inst[i].ring_enc[0]; 257 258 adev->nbio.funcs->vcn_doorbell_range(adev, ring->use_doorbell, 259 ((adev->doorbell_index.vcn.vcn_ring0_1 << 1) + 8 * i), i); 260 261 r = amdgpu_ring_test_helper(ring); 262 if (r) 263 goto done; 264 265 } 266 } 267 268 done: 269 if (!r) 270 DRM_INFO("VCN decode and encode initialized successfully(under %s).\n", 271 (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)?"DPG Mode":"SPG Mode"); 272 273 return r; 274 } 275 276 /** 277 * vcn_v4_0_hw_fini - stop the hardware block 278 * 279 * @handle: amdgpu_device pointer 280 * 281 * Stop the VCN block, mark ring as not ready any more 282 */ 283 static int vcn_v4_0_hw_fini(void *handle) 284 { 285 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 286 int i; 287 288 cancel_delayed_work_sync(&adev->vcn.idle_work); 289 290 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 291 if (adev->vcn.harvest_config & (1 << i)) 292 continue; 293 if (!amdgpu_sriov_vf(adev)) { 294 if ((adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) || 295 (adev->vcn.cur_state != AMD_PG_STATE_GATE && 296 RREG32_SOC15(VCN, i, regUVD_STATUS))) { 297 vcn_v4_0_set_powergating_state(adev, AMD_PG_STATE_GATE); 298 } 299 } 300 301 amdgpu_irq_put(adev, &adev->vcn.inst[i].irq, 0); 302 } 303 304 return 0; 305 } 306 307 /** 308 * vcn_v4_0_suspend - suspend VCN block 309 * 310 * @handle: amdgpu_device pointer 311 * 312 * HW fini and suspend VCN block 313 */ 314 static int vcn_v4_0_suspend(void *handle) 315 { 316 int r; 317 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 318 319 r = vcn_v4_0_hw_fini(adev); 320 if (r) 321 return r; 322 323 r = amdgpu_vcn_suspend(adev); 324 325 return r; 326 } 327 328 /** 329 * vcn_v4_0_resume - resume VCN block 330 * 331 * @handle: amdgpu_device pointer 332 * 333 * Resume firmware and hw init VCN block 334 */ 335 static int vcn_v4_0_resume(void *handle) 336 { 337 int r; 338 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 339 340 r = amdgpu_vcn_resume(adev); 341 if (r) 342 return r; 343 344 r = vcn_v4_0_hw_init(adev); 345 346 return r; 347 } 348 349 /** 350 * vcn_v4_0_mc_resume - memory controller programming 351 * 352 * @adev: amdgpu_device pointer 353 * @inst: instance number 354 * 355 * Let the VCN memory controller know it's offsets 356 */ 357 static void vcn_v4_0_mc_resume(struct amdgpu_device *adev, int inst) 358 { 359 uint32_t offset, size; 360 const struct common_firmware_header *hdr; 361 362 hdr = (const struct common_firmware_header *)adev->vcn.fw->data; 363 size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8); 364 365 /* cache window 0: fw */ 366 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) { 367 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW, 368 (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst].tmr_mc_addr_lo)); 369 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH, 370 (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst].tmr_mc_addr_hi)); 371 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_OFFSET0, 0); 372 offset = 0; 373 } else { 374 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW, 375 lower_32_bits(adev->vcn.inst[inst].gpu_addr)); 376 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH, 377 upper_32_bits(adev->vcn.inst[inst].gpu_addr)); 378 offset = size; 379 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_OFFSET0, AMDGPU_UVD_FIRMWARE_OFFSET >> 3); 380 } 381 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_SIZE0, size); 382 383 /* cache window 1: stack */ 384 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW, 385 lower_32_bits(adev->vcn.inst[inst].gpu_addr + offset)); 386 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH, 387 upper_32_bits(adev->vcn.inst[inst].gpu_addr + offset)); 388 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_OFFSET1, 0); 389 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_SIZE1, AMDGPU_VCN_STACK_SIZE); 390 391 /* cache window 2: context */ 392 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW, 393 lower_32_bits(adev->vcn.inst[inst].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE)); 394 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH, 395 upper_32_bits(adev->vcn.inst[inst].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE)); 396 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_OFFSET2, 0); 397 WREG32_SOC15(VCN, inst, regUVD_VCPU_CACHE_SIZE2, AMDGPU_VCN_CONTEXT_SIZE); 398 399 /* non-cache window */ 400 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_NC0_64BIT_BAR_LOW, 401 lower_32_bits(adev->vcn.inst[inst].fw_shared.gpu_addr)); 402 WREG32_SOC15(VCN, inst, regUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH, 403 upper_32_bits(adev->vcn.inst[inst].fw_shared.gpu_addr)); 404 WREG32_SOC15(VCN, inst, regUVD_VCPU_NONCACHE_OFFSET0, 0); 405 WREG32_SOC15(VCN, inst, regUVD_VCPU_NONCACHE_SIZE0, 406 AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_vcn4_fw_shared))); 407 } 408 409 /** 410 * vcn_v4_0_mc_resume_dpg_mode - memory controller programming for dpg mode 411 * 412 * @adev: amdgpu_device pointer 413 * @inst_idx: instance number index 414 * @indirect: indirectly write sram 415 * 416 * Let the VCN memory controller know it's offsets with dpg mode 417 */ 418 static void vcn_v4_0_mc_resume_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect) 419 { 420 uint32_t offset, size; 421 const struct common_firmware_header *hdr; 422 hdr = (const struct common_firmware_header *)adev->vcn.fw->data; 423 size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8); 424 425 /* cache window 0: fw */ 426 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) { 427 if (!indirect) { 428 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 429 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 430 (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_lo), 0, indirect); 431 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 432 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 433 (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_hi), 0, indirect); 434 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 435 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect); 436 } else { 437 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 438 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 0, 0, indirect); 439 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 440 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 0, 0, indirect); 441 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 442 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect); 443 } 444 offset = 0; 445 } else { 446 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 447 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 448 lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect); 449 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 450 VCN, inst_idx, regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 451 upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect); 452 offset = size; 453 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 454 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET0), 455 AMDGPU_UVD_FIRMWARE_OFFSET >> 3, 0, indirect); 456 } 457 458 if (!indirect) 459 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 460 VCN, inst_idx, regUVD_VCPU_CACHE_SIZE0), size, 0, indirect); 461 else 462 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 463 VCN, inst_idx, regUVD_VCPU_CACHE_SIZE0), 0, 0, indirect); 464 465 /* cache window 1: stack */ 466 if (!indirect) { 467 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 468 VCN, inst_idx, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW), 469 lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect); 470 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 471 VCN, inst_idx, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH), 472 upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect); 473 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 474 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect); 475 } else { 476 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 477 VCN, inst_idx, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW), 0, 0, indirect); 478 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 479 VCN, inst_idx, regUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH), 0, 0, indirect); 480 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 481 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect); 482 } 483 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 484 VCN, inst_idx, regUVD_VCPU_CACHE_SIZE1), AMDGPU_VCN_STACK_SIZE, 0, indirect); 485 486 /* cache window 2: context */ 487 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 488 VCN, inst_idx, regUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW), 489 lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect); 490 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 491 VCN, inst_idx, regUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH), 492 upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect); 493 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 494 VCN, inst_idx, regUVD_VCPU_CACHE_OFFSET2), 0, 0, indirect); 495 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 496 VCN, inst_idx, regUVD_VCPU_CACHE_SIZE2), AMDGPU_VCN_CONTEXT_SIZE, 0, indirect); 497 498 /* non-cache window */ 499 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 500 VCN, inst_idx, regUVD_LMI_VCPU_NC0_64BIT_BAR_LOW), 501 lower_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect); 502 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 503 VCN, inst_idx, regUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH), 504 upper_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect); 505 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 506 VCN, inst_idx, regUVD_VCPU_NONCACHE_OFFSET0), 0, 0, indirect); 507 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 508 VCN, inst_idx, regUVD_VCPU_NONCACHE_SIZE0), 509 AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_vcn4_fw_shared)), 0, indirect); 510 511 /* VCN global tiling registers */ 512 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 513 VCN, 0, regUVD_GFX10_ADDR_CONFIG), adev->gfx.config.gb_addr_config, 0, indirect); 514 } 515 516 /** 517 * vcn_v4_0_disable_static_power_gating - disable VCN static power gating 518 * 519 * @adev: amdgpu_device pointer 520 * @inst: instance number 521 * 522 * Disable static power gating for VCN block 523 */ 524 static void vcn_v4_0_disable_static_power_gating(struct amdgpu_device *adev, int inst) 525 { 526 uint32_t data = 0; 527 528 if (adev->pg_flags & AMD_PG_SUPPORT_VCN) { 529 data = (1 << UVD_PGFSM_CONFIG__UVDM_PWR_CONFIG__SHIFT 530 | 1 << UVD_PGFSM_CONFIG__UVDS_PWR_CONFIG__SHIFT 531 | 1 << UVD_PGFSM_CONFIG__UVDLM_PWR_CONFIG__SHIFT 532 | 2 << UVD_PGFSM_CONFIG__UVDF_PWR_CONFIG__SHIFT 533 | 2 << UVD_PGFSM_CONFIG__UVDTC_PWR_CONFIG__SHIFT 534 | 2 << UVD_PGFSM_CONFIG__UVDB_PWR_CONFIG__SHIFT 535 | 2 << UVD_PGFSM_CONFIG__UVDTA_PWR_CONFIG__SHIFT 536 | 2 << UVD_PGFSM_CONFIG__UVDTD_PWR_CONFIG__SHIFT 537 | 2 << UVD_PGFSM_CONFIG__UVDTE_PWR_CONFIG__SHIFT 538 | 2 << UVD_PGFSM_CONFIG__UVDE_PWR_CONFIG__SHIFT 539 | 2 << UVD_PGFSM_CONFIG__UVDAB_PWR_CONFIG__SHIFT 540 | 2 << UVD_PGFSM_CONFIG__UVDTB_PWR_CONFIG__SHIFT 541 | 2 << UVD_PGFSM_CONFIG__UVDNA_PWR_CONFIG__SHIFT 542 | 2 << UVD_PGFSM_CONFIG__UVDNB_PWR_CONFIG__SHIFT); 543 544 WREG32_SOC15(VCN, inst, regUVD_PGFSM_CONFIG, data); 545 SOC15_WAIT_ON_RREG(VCN, inst, regUVD_PGFSM_STATUS, 546 UVD_PGFSM_STATUS__UVDM_UVDU_UVDLM_PWR_ON_3_0, 0x3F3FFFFF); 547 } else { 548 uint32_t value; 549 550 value = (inst) ? 0x2200800 : 0; 551 data = (1 << UVD_PGFSM_CONFIG__UVDM_PWR_CONFIG__SHIFT 552 | 1 << UVD_PGFSM_CONFIG__UVDS_PWR_CONFIG__SHIFT 553 | 1 << UVD_PGFSM_CONFIG__UVDLM_PWR_CONFIG__SHIFT 554 | 1 << UVD_PGFSM_CONFIG__UVDF_PWR_CONFIG__SHIFT 555 | 1 << UVD_PGFSM_CONFIG__UVDTC_PWR_CONFIG__SHIFT 556 | 1 << UVD_PGFSM_CONFIG__UVDB_PWR_CONFIG__SHIFT 557 | 1 << UVD_PGFSM_CONFIG__UVDTA_PWR_CONFIG__SHIFT 558 | 1 << UVD_PGFSM_CONFIG__UVDTD_PWR_CONFIG__SHIFT 559 | 1 << UVD_PGFSM_CONFIG__UVDTE_PWR_CONFIG__SHIFT 560 | 1 << UVD_PGFSM_CONFIG__UVDE_PWR_CONFIG__SHIFT 561 | 1 << UVD_PGFSM_CONFIG__UVDAB_PWR_CONFIG__SHIFT 562 | 1 << UVD_PGFSM_CONFIG__UVDTB_PWR_CONFIG__SHIFT 563 | 1 << UVD_PGFSM_CONFIG__UVDNA_PWR_CONFIG__SHIFT 564 | 1 << UVD_PGFSM_CONFIG__UVDNB_PWR_CONFIG__SHIFT); 565 566 WREG32_SOC15(VCN, inst, regUVD_PGFSM_CONFIG, data); 567 SOC15_WAIT_ON_RREG(VCN, inst, regUVD_PGFSM_STATUS, value, 0x3F3FFFFF); 568 } 569 570 data = RREG32_SOC15(VCN, inst, regUVD_POWER_STATUS); 571 data &= ~0x103; 572 if (adev->pg_flags & AMD_PG_SUPPORT_VCN) 573 data |= UVD_PGFSM_CONFIG__UVDM_UVDU_PWR_ON | 574 UVD_POWER_STATUS__UVD_PG_EN_MASK; 575 576 WREG32_SOC15(VCN, inst, regUVD_POWER_STATUS, data); 577 578 return; 579 } 580 581 /** 582 * vcn_v4_0_enable_static_power_gating - enable VCN static power gating 583 * 584 * @adev: amdgpu_device pointer 585 * @inst: instance number 586 * 587 * Enable static power gating for VCN block 588 */ 589 static void vcn_v4_0_enable_static_power_gating(struct amdgpu_device *adev, int inst) 590 { 591 uint32_t data; 592 593 if (adev->pg_flags & AMD_PG_SUPPORT_VCN) { 594 /* Before power off, this indicator has to be turned on */ 595 data = RREG32_SOC15(VCN, inst, regUVD_POWER_STATUS); 596 data &= ~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK; 597 data |= UVD_POWER_STATUS__UVD_POWER_STATUS_TILES_OFF; 598 WREG32_SOC15(VCN, inst, regUVD_POWER_STATUS, data); 599 600 data = (2 << UVD_PGFSM_CONFIG__UVDM_PWR_CONFIG__SHIFT 601 | 2 << UVD_PGFSM_CONFIG__UVDS_PWR_CONFIG__SHIFT 602 | 2 << UVD_PGFSM_CONFIG__UVDF_PWR_CONFIG__SHIFT 603 | 2 << UVD_PGFSM_CONFIG__UVDTC_PWR_CONFIG__SHIFT 604 | 2 << UVD_PGFSM_CONFIG__UVDB_PWR_CONFIG__SHIFT 605 | 2 << UVD_PGFSM_CONFIG__UVDTA_PWR_CONFIG__SHIFT 606 | 2 << UVD_PGFSM_CONFIG__UVDLM_PWR_CONFIG__SHIFT 607 | 2 << UVD_PGFSM_CONFIG__UVDTD_PWR_CONFIG__SHIFT 608 | 2 << UVD_PGFSM_CONFIG__UVDTE_PWR_CONFIG__SHIFT 609 | 2 << UVD_PGFSM_CONFIG__UVDE_PWR_CONFIG__SHIFT 610 | 2 << UVD_PGFSM_CONFIG__UVDAB_PWR_CONFIG__SHIFT 611 | 2 << UVD_PGFSM_CONFIG__UVDTB_PWR_CONFIG__SHIFT 612 | 2 << UVD_PGFSM_CONFIG__UVDNA_PWR_CONFIG__SHIFT 613 | 2 << UVD_PGFSM_CONFIG__UVDNB_PWR_CONFIG__SHIFT); 614 WREG32_SOC15(VCN, inst, regUVD_PGFSM_CONFIG, data); 615 616 data = (2 << UVD_PGFSM_STATUS__UVDM_PWR_STATUS__SHIFT 617 | 2 << UVD_PGFSM_STATUS__UVDS_PWR_STATUS__SHIFT 618 | 2 << UVD_PGFSM_STATUS__UVDF_PWR_STATUS__SHIFT 619 | 2 << UVD_PGFSM_STATUS__UVDTC_PWR_STATUS__SHIFT 620 | 2 << UVD_PGFSM_STATUS__UVDB_PWR_STATUS__SHIFT 621 | 2 << UVD_PGFSM_STATUS__UVDTA_PWR_STATUS__SHIFT 622 | 2 << UVD_PGFSM_STATUS__UVDLM_PWR_STATUS__SHIFT 623 | 2 << UVD_PGFSM_STATUS__UVDTD_PWR_STATUS__SHIFT 624 | 2 << UVD_PGFSM_STATUS__UVDTE_PWR_STATUS__SHIFT 625 | 2 << UVD_PGFSM_STATUS__UVDE_PWR_STATUS__SHIFT 626 | 2 << UVD_PGFSM_STATUS__UVDAB_PWR_STATUS__SHIFT 627 | 2 << UVD_PGFSM_STATUS__UVDTB_PWR_STATUS__SHIFT 628 | 2 << UVD_PGFSM_STATUS__UVDNA_PWR_STATUS__SHIFT 629 | 2 << UVD_PGFSM_STATUS__UVDNB_PWR_STATUS__SHIFT); 630 SOC15_WAIT_ON_RREG(VCN, inst, regUVD_PGFSM_STATUS, data, 0x3F3FFFFF); 631 } 632 633 return; 634 } 635 636 /** 637 * vcn_v4_0_disable_clock_gating - disable VCN clock gating 638 * 639 * @adev: amdgpu_device pointer 640 * @inst: instance number 641 * 642 * Disable clock gating for VCN block 643 */ 644 static void vcn_v4_0_disable_clock_gating(struct amdgpu_device *adev, int inst) 645 { 646 uint32_t data; 647 648 if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG) 649 return; 650 651 /* VCN disable CGC */ 652 data = RREG32_SOC15(VCN, inst, regUVD_CGC_CTRL); 653 data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK; 654 data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT; 655 data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT; 656 WREG32_SOC15(VCN, inst, regUVD_CGC_CTRL, data); 657 658 data = RREG32_SOC15(VCN, inst, regUVD_CGC_GATE); 659 data &= ~(UVD_CGC_GATE__SYS_MASK 660 | UVD_CGC_GATE__UDEC_MASK 661 | UVD_CGC_GATE__MPEG2_MASK 662 | UVD_CGC_GATE__REGS_MASK 663 | UVD_CGC_GATE__RBC_MASK 664 | UVD_CGC_GATE__LMI_MC_MASK 665 | UVD_CGC_GATE__LMI_UMC_MASK 666 | UVD_CGC_GATE__IDCT_MASK 667 | UVD_CGC_GATE__MPRD_MASK 668 | UVD_CGC_GATE__MPC_MASK 669 | UVD_CGC_GATE__LBSI_MASK 670 | UVD_CGC_GATE__LRBBM_MASK 671 | UVD_CGC_GATE__UDEC_RE_MASK 672 | UVD_CGC_GATE__UDEC_CM_MASK 673 | UVD_CGC_GATE__UDEC_IT_MASK 674 | UVD_CGC_GATE__UDEC_DB_MASK 675 | UVD_CGC_GATE__UDEC_MP_MASK 676 | UVD_CGC_GATE__WCB_MASK 677 | UVD_CGC_GATE__VCPU_MASK 678 | UVD_CGC_GATE__MMSCH_MASK); 679 680 WREG32_SOC15(VCN, inst, regUVD_CGC_GATE, data); 681 SOC15_WAIT_ON_RREG(VCN, inst, regUVD_CGC_GATE, 0, 0xFFFFFFFF); 682 683 data = RREG32_SOC15(VCN, inst, regUVD_CGC_CTRL); 684 data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK 685 | UVD_CGC_CTRL__UDEC_CM_MODE_MASK 686 | UVD_CGC_CTRL__UDEC_IT_MODE_MASK 687 | UVD_CGC_CTRL__UDEC_DB_MODE_MASK 688 | UVD_CGC_CTRL__UDEC_MP_MODE_MASK 689 | UVD_CGC_CTRL__SYS_MODE_MASK 690 | UVD_CGC_CTRL__UDEC_MODE_MASK 691 | UVD_CGC_CTRL__MPEG2_MODE_MASK 692 | UVD_CGC_CTRL__REGS_MODE_MASK 693 | UVD_CGC_CTRL__RBC_MODE_MASK 694 | UVD_CGC_CTRL__LMI_MC_MODE_MASK 695 | UVD_CGC_CTRL__LMI_UMC_MODE_MASK 696 | UVD_CGC_CTRL__IDCT_MODE_MASK 697 | UVD_CGC_CTRL__MPRD_MODE_MASK 698 | UVD_CGC_CTRL__MPC_MODE_MASK 699 | UVD_CGC_CTRL__LBSI_MODE_MASK 700 | UVD_CGC_CTRL__LRBBM_MODE_MASK 701 | UVD_CGC_CTRL__WCB_MODE_MASK 702 | UVD_CGC_CTRL__VCPU_MODE_MASK 703 | UVD_CGC_CTRL__MMSCH_MODE_MASK); 704 WREG32_SOC15(VCN, inst, regUVD_CGC_CTRL, data); 705 706 data = RREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_GATE); 707 data |= (UVD_SUVD_CGC_GATE__SRE_MASK 708 | UVD_SUVD_CGC_GATE__SIT_MASK 709 | UVD_SUVD_CGC_GATE__SMP_MASK 710 | UVD_SUVD_CGC_GATE__SCM_MASK 711 | UVD_SUVD_CGC_GATE__SDB_MASK 712 | UVD_SUVD_CGC_GATE__SRE_H264_MASK 713 | UVD_SUVD_CGC_GATE__SRE_HEVC_MASK 714 | UVD_SUVD_CGC_GATE__SIT_H264_MASK 715 | UVD_SUVD_CGC_GATE__SIT_HEVC_MASK 716 | UVD_SUVD_CGC_GATE__SCM_H264_MASK 717 | UVD_SUVD_CGC_GATE__SCM_HEVC_MASK 718 | UVD_SUVD_CGC_GATE__SDB_H264_MASK 719 | UVD_SUVD_CGC_GATE__SDB_HEVC_MASK 720 | UVD_SUVD_CGC_GATE__SCLR_MASK 721 | UVD_SUVD_CGC_GATE__UVD_SC_MASK 722 | UVD_SUVD_CGC_GATE__ENT_MASK 723 | UVD_SUVD_CGC_GATE__SIT_HEVC_DEC_MASK 724 | UVD_SUVD_CGC_GATE__SIT_HEVC_ENC_MASK 725 | UVD_SUVD_CGC_GATE__SITE_MASK 726 | UVD_SUVD_CGC_GATE__SRE_VP9_MASK 727 | UVD_SUVD_CGC_GATE__SCM_VP9_MASK 728 | UVD_SUVD_CGC_GATE__SIT_VP9_DEC_MASK 729 | UVD_SUVD_CGC_GATE__SDB_VP9_MASK 730 | UVD_SUVD_CGC_GATE__IME_HEVC_MASK); 731 WREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_GATE, data); 732 733 data = RREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_CTRL); 734 data &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK 735 | UVD_SUVD_CGC_CTRL__SIT_MODE_MASK 736 | UVD_SUVD_CGC_CTRL__SMP_MODE_MASK 737 | UVD_SUVD_CGC_CTRL__SCM_MODE_MASK 738 | UVD_SUVD_CGC_CTRL__SDB_MODE_MASK 739 | UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK 740 | UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK 741 | UVD_SUVD_CGC_CTRL__ENT_MODE_MASK 742 | UVD_SUVD_CGC_CTRL__IME_MODE_MASK 743 | UVD_SUVD_CGC_CTRL__SITE_MODE_MASK); 744 WREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_CTRL, data); 745 } 746 747 /** 748 * vcn_v4_0_disable_clock_gating_dpg_mode - disable VCN clock gating dpg mode 749 * 750 * @adev: amdgpu_device pointer 751 * @sram_sel: sram select 752 * @inst_idx: instance number index 753 * @indirect: indirectly write sram 754 * 755 * Disable clock gating for VCN block with dpg mode 756 */ 757 static void vcn_v4_0_disable_clock_gating_dpg_mode(struct amdgpu_device *adev, uint8_t sram_sel, 758 int inst_idx, uint8_t indirect) 759 { 760 uint32_t reg_data = 0; 761 762 if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG) 763 return; 764 765 /* enable sw clock gating control */ 766 reg_data = 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT; 767 reg_data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT; 768 reg_data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT; 769 reg_data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK | 770 UVD_CGC_CTRL__UDEC_CM_MODE_MASK | 771 UVD_CGC_CTRL__UDEC_IT_MODE_MASK | 772 UVD_CGC_CTRL__UDEC_DB_MODE_MASK | 773 UVD_CGC_CTRL__UDEC_MP_MODE_MASK | 774 UVD_CGC_CTRL__SYS_MODE_MASK | 775 UVD_CGC_CTRL__UDEC_MODE_MASK | 776 UVD_CGC_CTRL__MPEG2_MODE_MASK | 777 UVD_CGC_CTRL__REGS_MODE_MASK | 778 UVD_CGC_CTRL__RBC_MODE_MASK | 779 UVD_CGC_CTRL__LMI_MC_MODE_MASK | 780 UVD_CGC_CTRL__LMI_UMC_MODE_MASK | 781 UVD_CGC_CTRL__IDCT_MODE_MASK | 782 UVD_CGC_CTRL__MPRD_MODE_MASK | 783 UVD_CGC_CTRL__MPC_MODE_MASK | 784 UVD_CGC_CTRL__LBSI_MODE_MASK | 785 UVD_CGC_CTRL__LRBBM_MODE_MASK | 786 UVD_CGC_CTRL__WCB_MODE_MASK | 787 UVD_CGC_CTRL__VCPU_MODE_MASK); 788 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 789 VCN, inst_idx, regUVD_CGC_CTRL), reg_data, sram_sel, indirect); 790 791 /* turn off clock gating */ 792 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 793 VCN, inst_idx, regUVD_CGC_GATE), 0, sram_sel, indirect); 794 795 /* turn on SUVD clock gating */ 796 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 797 VCN, inst_idx, regUVD_SUVD_CGC_GATE), 1, sram_sel, indirect); 798 799 /* turn on sw mode in UVD_SUVD_CGC_CTRL */ 800 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 801 VCN, inst_idx, regUVD_SUVD_CGC_CTRL), 0, sram_sel, indirect); 802 } 803 804 /** 805 * vcn_v4_0_enable_clock_gating - enable VCN clock gating 806 * 807 * @adev: amdgpu_device pointer 808 * @inst: instance number 809 * 810 * Enable clock gating for VCN block 811 */ 812 static void vcn_v4_0_enable_clock_gating(struct amdgpu_device *adev, int inst) 813 { 814 uint32_t data; 815 816 if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG) 817 return; 818 819 /* enable VCN CGC */ 820 data = RREG32_SOC15(VCN, inst, regUVD_CGC_CTRL); 821 data |= 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT; 822 data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT; 823 data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT; 824 WREG32_SOC15(VCN, inst, regUVD_CGC_CTRL, data); 825 826 data = RREG32_SOC15(VCN, inst, regUVD_CGC_CTRL); 827 data |= (UVD_CGC_CTRL__UDEC_RE_MODE_MASK 828 | UVD_CGC_CTRL__UDEC_CM_MODE_MASK 829 | UVD_CGC_CTRL__UDEC_IT_MODE_MASK 830 | UVD_CGC_CTRL__UDEC_DB_MODE_MASK 831 | UVD_CGC_CTRL__UDEC_MP_MODE_MASK 832 | UVD_CGC_CTRL__SYS_MODE_MASK 833 | UVD_CGC_CTRL__UDEC_MODE_MASK 834 | UVD_CGC_CTRL__MPEG2_MODE_MASK 835 | UVD_CGC_CTRL__REGS_MODE_MASK 836 | UVD_CGC_CTRL__RBC_MODE_MASK 837 | UVD_CGC_CTRL__LMI_MC_MODE_MASK 838 | UVD_CGC_CTRL__LMI_UMC_MODE_MASK 839 | UVD_CGC_CTRL__IDCT_MODE_MASK 840 | UVD_CGC_CTRL__MPRD_MODE_MASK 841 | UVD_CGC_CTRL__MPC_MODE_MASK 842 | UVD_CGC_CTRL__LBSI_MODE_MASK 843 | UVD_CGC_CTRL__LRBBM_MODE_MASK 844 | UVD_CGC_CTRL__WCB_MODE_MASK 845 | UVD_CGC_CTRL__VCPU_MODE_MASK 846 | UVD_CGC_CTRL__MMSCH_MODE_MASK); 847 WREG32_SOC15(VCN, inst, regUVD_CGC_CTRL, data); 848 849 data = RREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_CTRL); 850 data |= (UVD_SUVD_CGC_CTRL__SRE_MODE_MASK 851 | UVD_SUVD_CGC_CTRL__SIT_MODE_MASK 852 | UVD_SUVD_CGC_CTRL__SMP_MODE_MASK 853 | UVD_SUVD_CGC_CTRL__SCM_MODE_MASK 854 | UVD_SUVD_CGC_CTRL__SDB_MODE_MASK 855 | UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK 856 | UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK 857 | UVD_SUVD_CGC_CTRL__ENT_MODE_MASK 858 | UVD_SUVD_CGC_CTRL__IME_MODE_MASK 859 | UVD_SUVD_CGC_CTRL__SITE_MODE_MASK); 860 WREG32_SOC15(VCN, inst, regUVD_SUVD_CGC_CTRL, data); 861 862 return; 863 } 864 865 /** 866 * vcn_v4_0_start_dpg_mode - VCN start with dpg mode 867 * 868 * @adev: amdgpu_device pointer 869 * @inst_idx: instance number index 870 * @indirect: indirectly write sram 871 * 872 * Start VCN block with dpg mode 873 */ 874 static int vcn_v4_0_start_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect) 875 { 876 volatile struct amdgpu_vcn4_fw_shared *fw_shared = adev->vcn.inst[inst_idx].fw_shared.cpu_addr; 877 struct amdgpu_ring *ring; 878 uint32_t tmp; 879 880 /* disable register anti-hang mechanism */ 881 WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, regUVD_POWER_STATUS), 1, 882 ~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK); 883 /* enable dynamic power gating mode */ 884 tmp = RREG32_SOC15(VCN, inst_idx, regUVD_POWER_STATUS); 885 tmp |= UVD_POWER_STATUS__UVD_PG_MODE_MASK; 886 tmp |= UVD_POWER_STATUS__UVD_PG_EN_MASK; 887 WREG32_SOC15(VCN, inst_idx, regUVD_POWER_STATUS, tmp); 888 889 if (indirect) 890 adev->vcn.inst[inst_idx].dpg_sram_curr_addr = (uint32_t *)adev->vcn.inst[inst_idx].dpg_sram_cpu_addr; 891 892 /* enable clock gating */ 893 vcn_v4_0_disable_clock_gating_dpg_mode(adev, 0, inst_idx, indirect); 894 895 /* enable VCPU clock */ 896 tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT); 897 tmp |= UVD_VCPU_CNTL__CLK_EN_MASK | UVD_VCPU_CNTL__BLK_RST_MASK; 898 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 899 VCN, inst_idx, regUVD_VCPU_CNTL), tmp, 0, indirect); 900 901 /* disable master interupt */ 902 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 903 VCN, inst_idx, regUVD_MASTINT_EN), 0, 0, indirect); 904 905 /* setup regUVD_LMI_CTRL */ 906 tmp = (UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK | 907 UVD_LMI_CTRL__REQ_MODE_MASK | 908 UVD_LMI_CTRL__CRC_RESET_MASK | 909 UVD_LMI_CTRL__MASK_MC_URGENT_MASK | 910 UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK | 911 UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK | 912 (8 << UVD_LMI_CTRL__WRITE_CLEAN_TIMER__SHIFT) | 913 0x00100000L); 914 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 915 VCN, inst_idx, regUVD_LMI_CTRL), tmp, 0, indirect); 916 917 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 918 VCN, inst_idx, regUVD_MPC_CNTL), 919 0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT, 0, indirect); 920 921 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 922 VCN, inst_idx, regUVD_MPC_SET_MUXA0), 923 ((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) | 924 (0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) | 925 (0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) | 926 (0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT)), 0, indirect); 927 928 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 929 VCN, inst_idx, regUVD_MPC_SET_MUXB0), 930 ((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) | 931 (0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) | 932 (0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) | 933 (0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT)), 0, indirect); 934 935 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 936 VCN, inst_idx, regUVD_MPC_SET_MUX), 937 ((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) | 938 (0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) | 939 (0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT)), 0, indirect); 940 941 vcn_v4_0_mc_resume_dpg_mode(adev, inst_idx, indirect); 942 943 tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT); 944 tmp |= UVD_VCPU_CNTL__CLK_EN_MASK; 945 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 946 VCN, inst_idx, regUVD_VCPU_CNTL), tmp, 0, indirect); 947 948 /* enable LMI MC and UMC channels */ 949 tmp = 0x1f << UVD_LMI_CTRL2__RE_OFLD_MIF_WR_REQ_NUM__SHIFT; 950 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 951 VCN, inst_idx, regUVD_LMI_CTRL2), tmp, 0, indirect); 952 953 /* enable master interrupt */ 954 WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET( 955 VCN, inst_idx, regUVD_MASTINT_EN), 956 UVD_MASTINT_EN__VCPU_EN_MASK, 0, indirect); 957 958 959 if (indirect) 960 psp_update_vcn_sram(adev, inst_idx, adev->vcn.inst[inst_idx].dpg_sram_gpu_addr, 961 (uint32_t)((uintptr_t)adev->vcn.inst[inst_idx].dpg_sram_curr_addr - 962 (uintptr_t)adev->vcn.inst[inst_idx].dpg_sram_cpu_addr)); 963 964 ring = &adev->vcn.inst[inst_idx].ring_enc[0]; 965 966 WREG32_SOC15(VCN, inst_idx, regUVD_RB_BASE_LO, ring->gpu_addr); 967 WREG32_SOC15(VCN, inst_idx, regUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr)); 968 WREG32_SOC15(VCN, inst_idx, regUVD_RB_SIZE, ring->ring_size / 4); 969 970 tmp = RREG32_SOC15(VCN, inst_idx, regVCN_RB_ENABLE); 971 tmp &= ~(VCN_RB_ENABLE__RB1_EN_MASK); 972 WREG32_SOC15(VCN, inst_idx, regVCN_RB_ENABLE, tmp); 973 fw_shared->sq.queue_mode |= FW_QUEUE_RING_RESET; 974 WREG32_SOC15(VCN, inst_idx, regUVD_RB_RPTR, 0); 975 WREG32_SOC15(VCN, inst_idx, regUVD_RB_WPTR, 0); 976 977 tmp = RREG32_SOC15(VCN, inst_idx, regUVD_RB_RPTR); 978 WREG32_SOC15(VCN, inst_idx, regUVD_RB_WPTR, tmp); 979 ring->wptr = RREG32_SOC15(VCN, inst_idx, regUVD_RB_WPTR); 980 981 tmp = RREG32_SOC15(VCN, inst_idx, regVCN_RB_ENABLE); 982 tmp |= VCN_RB_ENABLE__RB1_EN_MASK; 983 WREG32_SOC15(VCN, inst_idx, regVCN_RB_ENABLE, tmp); 984 fw_shared->sq.queue_mode &= ~(FW_QUEUE_RING_RESET | FW_QUEUE_DPG_HOLD_OFF); 985 986 WREG32_SOC15(VCN, inst_idx, regVCN_RB1_DB_CTRL, 987 ring->doorbell_index << VCN_RB1_DB_CTRL__OFFSET__SHIFT | 988 VCN_RB1_DB_CTRL__EN_MASK); 989 990 return 0; 991 } 992 993 994 /** 995 * vcn_v4_0_start - VCN start 996 * 997 * @adev: amdgpu_device pointer 998 * 999 * Start VCN block 1000 */ 1001 static int vcn_v4_0_start(struct amdgpu_device *adev) 1002 { 1003 volatile struct amdgpu_vcn4_fw_shared *fw_shared; 1004 struct amdgpu_ring *ring; 1005 uint32_t tmp; 1006 int i, j, k, r; 1007 1008 if (adev->pm.dpm_enabled) 1009 amdgpu_dpm_enable_uvd(adev, true); 1010 1011 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1012 fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr; 1013 1014 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) { 1015 r = vcn_v4_0_start_dpg_mode(adev, i, adev->vcn.indirect_sram); 1016 continue; 1017 } 1018 1019 /* disable VCN power gating */ 1020 vcn_v4_0_disable_static_power_gating(adev, i); 1021 1022 /* set VCN status busy */ 1023 tmp = RREG32_SOC15(VCN, i, regUVD_STATUS) | UVD_STATUS__UVD_BUSY; 1024 WREG32_SOC15(VCN, i, regUVD_STATUS, tmp); 1025 1026 /*SW clock gating */ 1027 vcn_v4_0_disable_clock_gating(adev, i); 1028 1029 /* enable VCPU clock */ 1030 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 1031 UVD_VCPU_CNTL__CLK_EN_MASK, ~UVD_VCPU_CNTL__CLK_EN_MASK); 1032 1033 /* disable master interrupt */ 1034 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_MASTINT_EN), 0, 1035 ~UVD_MASTINT_EN__VCPU_EN_MASK); 1036 1037 /* enable LMI MC and UMC channels */ 1038 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_LMI_CTRL2), 0, 1039 ~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK); 1040 1041 tmp = RREG32_SOC15(VCN, i, regUVD_SOFT_RESET); 1042 tmp &= ~UVD_SOFT_RESET__LMI_SOFT_RESET_MASK; 1043 tmp &= ~UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK; 1044 WREG32_SOC15(VCN, i, regUVD_SOFT_RESET, tmp); 1045 1046 /* setup regUVD_LMI_CTRL */ 1047 tmp = RREG32_SOC15(VCN, i, regUVD_LMI_CTRL); 1048 WREG32_SOC15(VCN, i, regUVD_LMI_CTRL, tmp | 1049 UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK | 1050 UVD_LMI_CTRL__MASK_MC_URGENT_MASK | 1051 UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK | 1052 UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK); 1053 1054 /* setup regUVD_MPC_CNTL */ 1055 tmp = RREG32_SOC15(VCN, i, regUVD_MPC_CNTL); 1056 tmp &= ~UVD_MPC_CNTL__REPLACEMENT_MODE_MASK; 1057 tmp |= 0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT; 1058 WREG32_SOC15(VCN, i, regUVD_MPC_CNTL, tmp); 1059 1060 /* setup UVD_MPC_SET_MUXA0 */ 1061 WREG32_SOC15(VCN, i, regUVD_MPC_SET_MUXA0, 1062 ((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) | 1063 (0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) | 1064 (0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) | 1065 (0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT))); 1066 1067 /* setup UVD_MPC_SET_MUXB0 */ 1068 WREG32_SOC15(VCN, i, regUVD_MPC_SET_MUXB0, 1069 ((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) | 1070 (0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) | 1071 (0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) | 1072 (0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT))); 1073 1074 /* setup UVD_MPC_SET_MUX */ 1075 WREG32_SOC15(VCN, i, regUVD_MPC_SET_MUX, 1076 ((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) | 1077 (0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) | 1078 (0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT))); 1079 1080 vcn_v4_0_mc_resume(adev, i); 1081 1082 /* VCN global tiling registers */ 1083 WREG32_SOC15(VCN, i, regUVD_GFX10_ADDR_CONFIG, 1084 adev->gfx.config.gb_addr_config); 1085 1086 /* unblock VCPU register access */ 1087 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_RB_ARB_CTRL), 0, 1088 ~UVD_RB_ARB_CTRL__VCPU_DIS_MASK); 1089 1090 /* release VCPU reset to boot */ 1091 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 0, 1092 ~UVD_VCPU_CNTL__BLK_RST_MASK); 1093 1094 for (j = 0; j < 10; ++j) { 1095 uint32_t status; 1096 1097 for (k = 0; k < 100; ++k) { 1098 status = RREG32_SOC15(VCN, i, regUVD_STATUS); 1099 if (status & 2) 1100 break; 1101 mdelay(10); 1102 if (amdgpu_emu_mode==1) 1103 msleep(1); 1104 } 1105 1106 if (amdgpu_emu_mode==1) { 1107 r = -1; 1108 if (status & 2) { 1109 r = 0; 1110 break; 1111 } 1112 } else { 1113 r = 0; 1114 if (status & 2) 1115 break; 1116 1117 dev_err(adev->dev, "VCN[%d] is not responding, trying to reset the VCPU!!!\n", i); 1118 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 1119 UVD_VCPU_CNTL__BLK_RST_MASK, 1120 ~UVD_VCPU_CNTL__BLK_RST_MASK); 1121 mdelay(10); 1122 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 0, 1123 ~UVD_VCPU_CNTL__BLK_RST_MASK); 1124 1125 mdelay(10); 1126 r = -1; 1127 } 1128 } 1129 1130 if (r) { 1131 dev_err(adev->dev, "VCN[%d] is not responding, giving up!!!\n", i); 1132 return r; 1133 } 1134 1135 /* enable master interrupt */ 1136 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_MASTINT_EN), 1137 UVD_MASTINT_EN__VCPU_EN_MASK, 1138 ~UVD_MASTINT_EN__VCPU_EN_MASK); 1139 1140 /* clear the busy bit of VCN_STATUS */ 1141 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_STATUS), 0, 1142 ~(2 << UVD_STATUS__VCPU_REPORT__SHIFT)); 1143 1144 ring = &adev->vcn.inst[i].ring_enc[0]; 1145 WREG32_SOC15(VCN, i, regVCN_RB1_DB_CTRL, 1146 ring->doorbell_index << VCN_RB1_DB_CTRL__OFFSET__SHIFT | 1147 VCN_RB1_DB_CTRL__EN_MASK); 1148 1149 WREG32_SOC15(VCN, i, regUVD_RB_BASE_LO, ring->gpu_addr); 1150 WREG32_SOC15(VCN, i, regUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr)); 1151 WREG32_SOC15(VCN, i, regUVD_RB_SIZE, ring->ring_size / 4); 1152 1153 tmp = RREG32_SOC15(VCN, i, regVCN_RB_ENABLE); 1154 tmp &= ~(VCN_RB_ENABLE__RB1_EN_MASK); 1155 WREG32_SOC15(VCN, i, regVCN_RB_ENABLE, tmp); 1156 fw_shared->sq.queue_mode |= FW_QUEUE_RING_RESET; 1157 WREG32_SOC15(VCN, i, regUVD_RB_RPTR, 0); 1158 WREG32_SOC15(VCN, i, regUVD_RB_WPTR, 0); 1159 1160 tmp = RREG32_SOC15(VCN, i, regUVD_RB_RPTR); 1161 WREG32_SOC15(VCN, i, regUVD_RB_WPTR, tmp); 1162 ring->wptr = RREG32_SOC15(VCN, i, regUVD_RB_WPTR); 1163 1164 tmp = RREG32_SOC15(VCN, i, regVCN_RB_ENABLE); 1165 tmp |= VCN_RB_ENABLE__RB1_EN_MASK; 1166 WREG32_SOC15(VCN, i, regVCN_RB_ENABLE, tmp); 1167 fw_shared->sq.queue_mode &= ~(FW_QUEUE_RING_RESET | FW_QUEUE_DPG_HOLD_OFF); 1168 } 1169 1170 return 0; 1171 } 1172 1173 static int vcn_v4_0_start_sriov(struct amdgpu_device *adev) 1174 { 1175 int i; 1176 struct amdgpu_ring *ring_enc; 1177 uint64_t cache_addr; 1178 uint64_t rb_enc_addr; 1179 uint64_t ctx_addr; 1180 uint32_t param, resp, expected; 1181 uint32_t offset, cache_size; 1182 uint32_t tmp, timeout; 1183 1184 struct amdgpu_mm_table *table = &adev->virt.mm_table; 1185 uint32_t *table_loc; 1186 uint32_t table_size; 1187 uint32_t size, size_dw; 1188 uint32_t init_status; 1189 uint32_t enabled_vcn; 1190 1191 struct mmsch_v4_0_cmd_direct_write 1192 direct_wt = { {0} }; 1193 struct mmsch_v4_0_cmd_direct_read_modify_write 1194 direct_rd_mod_wt = { {0} }; 1195 struct mmsch_v4_0_cmd_end end = { {0} }; 1196 struct mmsch_v4_0_init_header header; 1197 1198 volatile struct amdgpu_vcn4_fw_shared *fw_shared; 1199 volatile struct amdgpu_fw_shared_rb_setup *rb_setup; 1200 1201 direct_wt.cmd_header.command_type = 1202 MMSCH_COMMAND__DIRECT_REG_WRITE; 1203 direct_rd_mod_wt.cmd_header.command_type = 1204 MMSCH_COMMAND__DIRECT_REG_READ_MODIFY_WRITE; 1205 end.cmd_header.command_type = 1206 MMSCH_COMMAND__END; 1207 1208 header.version = MMSCH_VERSION; 1209 header.total_size = sizeof(struct mmsch_v4_0_init_header) >> 2; 1210 for (i = 0; i < AMDGPU_MAX_VCN_INSTANCES; i++) { 1211 header.inst[i].init_status = 0; 1212 header.inst[i].table_offset = 0; 1213 header.inst[i].table_size = 0; 1214 } 1215 1216 table_loc = (uint32_t *)table->cpu_addr; 1217 table_loc += header.total_size; 1218 for (i = 0; i < adev->vcn.num_vcn_inst; i++) { 1219 if (adev->vcn.harvest_config & (1 << i)) 1220 continue; 1221 1222 table_size = 0; 1223 1224 MMSCH_V4_0_INSERT_DIRECT_RD_MOD_WT(SOC15_REG_OFFSET(VCN, i, 1225 regUVD_STATUS), 1226 ~UVD_STATUS__UVD_BUSY, UVD_STATUS__UVD_BUSY); 1227 1228 cache_size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw->size + 4); 1229 1230 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) { 1231 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1232 regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 1233 adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_lo); 1234 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1235 regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 1236 adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_hi); 1237 offset = 0; 1238 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1239 regUVD_VCPU_CACHE_OFFSET0), 1240 0); 1241 } else { 1242 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1243 regUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 1244 lower_32_bits(adev->vcn.inst[i].gpu_addr)); 1245 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1246 regUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 1247 upper_32_bits(adev->vcn.inst[i].gpu_addr)); 1248 offset = cache_size; 1249 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1250 regUVD_VCPU_CACHE_OFFSET0), 1251 AMDGPU_UVD_FIRMWARE_OFFSET >> 3); 1252 } 1253 1254 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1255 regUVD_VCPU_CACHE_SIZE0), 1256 cache_size); 1257 1258 cache_addr = adev->vcn.inst[i].gpu_addr + offset; 1259 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1260 regUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW), 1261 lower_32_bits(cache_addr)); 1262 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1263 regUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH), 1264 upper_32_bits(cache_addr)); 1265 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1266 regUVD_VCPU_CACHE_OFFSET1), 1267 0); 1268 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1269 regUVD_VCPU_CACHE_SIZE1), 1270 AMDGPU_VCN_STACK_SIZE); 1271 1272 cache_addr = adev->vcn.inst[i].gpu_addr + offset + 1273 AMDGPU_VCN_STACK_SIZE; 1274 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1275 regUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW), 1276 lower_32_bits(cache_addr)); 1277 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1278 regUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH), 1279 upper_32_bits(cache_addr)); 1280 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1281 regUVD_VCPU_CACHE_OFFSET2), 1282 0); 1283 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1284 regUVD_VCPU_CACHE_SIZE2), 1285 AMDGPU_VCN_CONTEXT_SIZE); 1286 1287 fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr; 1288 rb_setup = &fw_shared->rb_setup; 1289 1290 ring_enc = &adev->vcn.inst[i].ring_enc[0]; 1291 ring_enc->wptr = 0; 1292 rb_enc_addr = ring_enc->gpu_addr; 1293 1294 rb_setup->is_rb_enabled_flags |= RB_ENABLED; 1295 rb_setup->rb_addr_lo = lower_32_bits(rb_enc_addr); 1296 rb_setup->rb_addr_hi = upper_32_bits(rb_enc_addr); 1297 rb_setup->rb_size = ring_enc->ring_size / 4; 1298 fw_shared->present_flag_0 |= cpu_to_le32(AMDGPU_VCN_VF_RB_SETUP_FLAG); 1299 1300 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1301 regUVD_LMI_VCPU_NC0_64BIT_BAR_LOW), 1302 lower_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr)); 1303 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1304 regUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH), 1305 upper_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr)); 1306 MMSCH_V4_0_INSERT_DIRECT_WT(SOC15_REG_OFFSET(VCN, i, 1307 regUVD_VCPU_NONCACHE_SIZE0), 1308 AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_vcn4_fw_shared))); 1309 1310 /* add end packet */ 1311 MMSCH_V4_0_INSERT_END(); 1312 1313 /* refine header */ 1314 header.inst[i].init_status = 0; 1315 header.inst[i].table_offset = header.total_size; 1316 header.inst[i].table_size = table_size; 1317 header.total_size += table_size; 1318 } 1319 1320 /* Update init table header in memory */ 1321 size = sizeof(struct mmsch_v4_0_init_header); 1322 table_loc = (uint32_t *)table->cpu_addr; 1323 memcpy((void *)table_loc, &header, size); 1324 1325 /* message MMSCH (in VCN[0]) to initialize this client 1326 * 1, write to mmsch_vf_ctx_addr_lo/hi register with GPU mc addr 1327 * of memory descriptor location 1328 */ 1329 ctx_addr = table->gpu_addr; 1330 WREG32_SOC15(VCN, 0, regMMSCH_VF_CTX_ADDR_LO, lower_32_bits(ctx_addr)); 1331 WREG32_SOC15(VCN, 0, regMMSCH_VF_CTX_ADDR_HI, upper_32_bits(ctx_addr)); 1332 1333 /* 2, update vmid of descriptor */ 1334 tmp = RREG32_SOC15(VCN, 0, regMMSCH_VF_VMID); 1335 tmp &= ~MMSCH_VF_VMID__VF_CTX_VMID_MASK; 1336 /* use domain0 for MM scheduler */ 1337 tmp |= (0 << MMSCH_VF_VMID__VF_CTX_VMID__SHIFT); 1338 WREG32_SOC15(VCN, 0, regMMSCH_VF_VMID, tmp); 1339 1340 /* 3, notify mmsch about the size of this descriptor */ 1341 size = header.total_size; 1342 WREG32_SOC15(VCN, 0, regMMSCH_VF_CTX_SIZE, size); 1343 1344 /* 4, set resp to zero */ 1345 WREG32_SOC15(VCN, 0, regMMSCH_VF_MAILBOX_RESP, 0); 1346 1347 /* 5, kick off the initialization and wait until 1348 * MMSCH_VF_MAILBOX_RESP becomes non-zero 1349 */ 1350 param = 0x00000001; 1351 WREG32_SOC15(VCN, 0, regMMSCH_VF_MAILBOX_HOST, param); 1352 tmp = 0; 1353 timeout = 1000; 1354 resp = 0; 1355 expected = MMSCH_VF_MAILBOX_RESP__OK; 1356 while (resp != expected) { 1357 resp = RREG32_SOC15(VCN, 0, regMMSCH_VF_MAILBOX_RESP); 1358 if (resp != 0) 1359 break; 1360 1361 udelay(10); 1362 tmp = tmp + 10; 1363 if (tmp >= timeout) { 1364 DRM_ERROR("failed to init MMSCH. TIME-OUT after %d usec"\ 1365 " waiting for regMMSCH_VF_MAILBOX_RESP "\ 1366 "(expected=0x%08x, readback=0x%08x)\n", 1367 tmp, expected, resp); 1368 return -EBUSY; 1369 } 1370 } 1371 enabled_vcn = amdgpu_vcn_is_disabled_vcn(adev, VCN_DECODE_RING, 0) ? 1 : 0; 1372 init_status = ((struct mmsch_v4_0_init_header *)(table_loc))->inst[enabled_vcn].init_status; 1373 if (resp != expected && resp != MMSCH_VF_MAILBOX_RESP__INCOMPLETE 1374 && init_status != MMSCH_VF_ENGINE_STATUS__PASS) 1375 DRM_ERROR("MMSCH init status is incorrect! readback=0x%08x, header init "\ 1376 "status for VCN%x: 0x%x\n", resp, enabled_vcn, init_status); 1377 1378 return 0; 1379 } 1380 1381 /** 1382 * vcn_v4_0_stop_dpg_mode - VCN stop with dpg mode 1383 * 1384 * @adev: amdgpu_device pointer 1385 * @inst_idx: instance number index 1386 * 1387 * Stop VCN block with dpg mode 1388 */ 1389 static void vcn_v4_0_stop_dpg_mode(struct amdgpu_device *adev, int inst_idx) 1390 { 1391 uint32_t tmp; 1392 1393 /* Wait for power status to be 1 */ 1394 SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_POWER_STATUS, 1, 1395 UVD_POWER_STATUS__UVD_POWER_STATUS_MASK); 1396 1397 /* wait for read ptr to be equal to write ptr */ 1398 tmp = RREG32_SOC15(VCN, inst_idx, regUVD_RB_WPTR); 1399 SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_RB_RPTR, tmp, 0xFFFFFFFF); 1400 1401 SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_POWER_STATUS, 1, 1402 UVD_POWER_STATUS__UVD_POWER_STATUS_MASK); 1403 1404 /* disable dynamic power gating mode */ 1405 WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, regUVD_POWER_STATUS), 0, 1406 ~UVD_POWER_STATUS__UVD_PG_MODE_MASK); 1407 } 1408 1409 /** 1410 * vcn_v4_0_stop - VCN stop 1411 * 1412 * @adev: amdgpu_device pointer 1413 * 1414 * Stop VCN block 1415 */ 1416 static int vcn_v4_0_stop(struct amdgpu_device *adev) 1417 { 1418 volatile struct amdgpu_vcn4_fw_shared *fw_shared; 1419 uint32_t tmp; 1420 int i, r = 0; 1421 1422 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1423 fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr; 1424 fw_shared->sq.queue_mode |= FW_QUEUE_DPG_HOLD_OFF; 1425 1426 if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) { 1427 vcn_v4_0_stop_dpg_mode(adev, i); 1428 continue; 1429 } 1430 1431 /* wait for vcn idle */ 1432 r = SOC15_WAIT_ON_RREG(VCN, i, regUVD_STATUS, UVD_STATUS__IDLE, 0x7); 1433 if (r) 1434 return r; 1435 1436 tmp = UVD_LMI_STATUS__VCPU_LMI_WRITE_CLEAN_MASK | 1437 UVD_LMI_STATUS__READ_CLEAN_MASK | 1438 UVD_LMI_STATUS__WRITE_CLEAN_MASK | 1439 UVD_LMI_STATUS__WRITE_CLEAN_RAW_MASK; 1440 r = SOC15_WAIT_ON_RREG(VCN, i, regUVD_LMI_STATUS, tmp, tmp); 1441 if (r) 1442 return r; 1443 1444 /* disable LMI UMC channel */ 1445 tmp = RREG32_SOC15(VCN, i, regUVD_LMI_CTRL2); 1446 tmp |= UVD_LMI_CTRL2__STALL_ARB_UMC_MASK; 1447 WREG32_SOC15(VCN, i, regUVD_LMI_CTRL2, tmp); 1448 tmp = UVD_LMI_STATUS__UMC_READ_CLEAN_RAW_MASK | 1449 UVD_LMI_STATUS__UMC_WRITE_CLEAN_RAW_MASK; 1450 r = SOC15_WAIT_ON_RREG(VCN, i, regUVD_LMI_STATUS, tmp, tmp); 1451 if (r) 1452 return r; 1453 1454 /* block VCPU register access */ 1455 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_RB_ARB_CTRL), 1456 UVD_RB_ARB_CTRL__VCPU_DIS_MASK, 1457 ~UVD_RB_ARB_CTRL__VCPU_DIS_MASK); 1458 1459 /* reset VCPU */ 1460 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 1461 UVD_VCPU_CNTL__BLK_RST_MASK, 1462 ~UVD_VCPU_CNTL__BLK_RST_MASK); 1463 1464 /* disable VCPU clock */ 1465 WREG32_P(SOC15_REG_OFFSET(VCN, i, regUVD_VCPU_CNTL), 0, 1466 ~(UVD_VCPU_CNTL__CLK_EN_MASK)); 1467 1468 /* apply soft reset */ 1469 tmp = RREG32_SOC15(VCN, i, regUVD_SOFT_RESET); 1470 tmp |= UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK; 1471 WREG32_SOC15(VCN, i, regUVD_SOFT_RESET, tmp); 1472 tmp = RREG32_SOC15(VCN, i, regUVD_SOFT_RESET); 1473 tmp |= UVD_SOFT_RESET__LMI_SOFT_RESET_MASK; 1474 WREG32_SOC15(VCN, i, regUVD_SOFT_RESET, tmp); 1475 1476 /* clear status */ 1477 WREG32_SOC15(VCN, i, regUVD_STATUS, 0); 1478 1479 /* apply HW clock gating */ 1480 vcn_v4_0_enable_clock_gating(adev, i); 1481 1482 /* enable VCN power gating */ 1483 vcn_v4_0_enable_static_power_gating(adev, i); 1484 } 1485 1486 if (adev->pm.dpm_enabled) 1487 amdgpu_dpm_enable_uvd(adev, false); 1488 1489 return 0; 1490 } 1491 1492 /** 1493 * vcn_v4_0_pause_dpg_mode - VCN pause with dpg mode 1494 * 1495 * @adev: amdgpu_device pointer 1496 * @inst_idx: instance number index 1497 * @new_state: pause state 1498 * 1499 * Pause dpg mode for VCN block 1500 */ 1501 static int vcn_v4_0_pause_dpg_mode(struct amdgpu_device *adev, int inst_idx, 1502 struct dpg_pause_state *new_state) 1503 { 1504 uint32_t reg_data = 0; 1505 int ret_code; 1506 1507 /* pause/unpause if state is changed */ 1508 if (adev->vcn.inst[inst_idx].pause_state.fw_based != new_state->fw_based) { 1509 DRM_DEV_DEBUG(adev->dev, "dpg pause state changed %d -> %d", 1510 adev->vcn.inst[inst_idx].pause_state.fw_based, new_state->fw_based); 1511 reg_data = RREG32_SOC15(VCN, inst_idx, regUVD_DPG_PAUSE) & 1512 (~UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK); 1513 1514 if (new_state->fw_based == VCN_DPG_STATE__PAUSE) { 1515 ret_code = SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_POWER_STATUS, 0x1, 1516 UVD_POWER_STATUS__UVD_POWER_STATUS_MASK); 1517 1518 if (!ret_code) { 1519 /* pause DPG */ 1520 reg_data |= UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK; 1521 WREG32_SOC15(VCN, inst_idx, regUVD_DPG_PAUSE, reg_data); 1522 1523 /* wait for ACK */ 1524 SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_DPG_PAUSE, 1525 UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK, 1526 UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK); 1527 1528 SOC15_WAIT_ON_RREG(VCN, inst_idx, regUVD_POWER_STATUS, 1529 UVD_PGFSM_CONFIG__UVDM_UVDU_PWR_ON, UVD_POWER_STATUS__UVD_POWER_STATUS_MASK); 1530 } 1531 } else { 1532 /* unpause dpg, no need to wait */ 1533 reg_data &= ~UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK; 1534 WREG32_SOC15(VCN, inst_idx, regUVD_DPG_PAUSE, reg_data); 1535 } 1536 adev->vcn.inst[inst_idx].pause_state.fw_based = new_state->fw_based; 1537 } 1538 1539 return 0; 1540 } 1541 1542 /** 1543 * vcn_v4_0_unified_ring_get_rptr - get unified read pointer 1544 * 1545 * @ring: amdgpu_ring pointer 1546 * 1547 * Returns the current hardware unified read pointer 1548 */ 1549 static uint64_t vcn_v4_0_unified_ring_get_rptr(struct amdgpu_ring *ring) 1550 { 1551 struct amdgpu_device *adev = ring->adev; 1552 1553 if (ring != &adev->vcn.inst[ring->me].ring_enc[0]) 1554 DRM_ERROR("wrong ring id is identified in %s", __func__); 1555 1556 return RREG32_SOC15(VCN, ring->me, regUVD_RB_RPTR); 1557 } 1558 1559 /** 1560 * vcn_v4_0_unified_ring_get_wptr - get unified write pointer 1561 * 1562 * @ring: amdgpu_ring pointer 1563 * 1564 * Returns the current hardware unified write pointer 1565 */ 1566 static uint64_t vcn_v4_0_unified_ring_get_wptr(struct amdgpu_ring *ring) 1567 { 1568 struct amdgpu_device *adev = ring->adev; 1569 1570 if (ring != &adev->vcn.inst[ring->me].ring_enc[0]) 1571 DRM_ERROR("wrong ring id is identified in %s", __func__); 1572 1573 if (ring->use_doorbell) 1574 return *ring->wptr_cpu_addr; 1575 else 1576 return RREG32_SOC15(VCN, ring->me, regUVD_RB_WPTR); 1577 } 1578 1579 /** 1580 * vcn_v4_0_unified_ring_set_wptr - set enc write pointer 1581 * 1582 * @ring: amdgpu_ring pointer 1583 * 1584 * Commits the enc write pointer to the hardware 1585 */ 1586 static void vcn_v4_0_unified_ring_set_wptr(struct amdgpu_ring *ring) 1587 { 1588 struct amdgpu_device *adev = ring->adev; 1589 1590 if (ring != &adev->vcn.inst[ring->me].ring_enc[0]) 1591 DRM_ERROR("wrong ring id is identified in %s", __func__); 1592 1593 if (ring->use_doorbell) { 1594 *ring->wptr_cpu_addr = lower_32_bits(ring->wptr); 1595 WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr)); 1596 } else { 1597 WREG32_SOC15(VCN, ring->me, regUVD_RB_WPTR, lower_32_bits(ring->wptr)); 1598 } 1599 } 1600 1601 static int vcn_v4_0_limit_sched(struct amdgpu_cs_parser *p, 1602 struct amdgpu_job *job) 1603 { 1604 struct drm_gpu_scheduler **scheds; 1605 1606 /* The create msg must be in the first IB submitted */ 1607 if (atomic_read(&job->base.entity->fence_seq)) 1608 return -EINVAL; 1609 1610 scheds = p->adev->gpu_sched[AMDGPU_HW_IP_VCN_ENC] 1611 [AMDGPU_RING_PRIO_0].sched; 1612 drm_sched_entity_modify_sched(job->base.entity, scheds, 1); 1613 return 0; 1614 } 1615 1616 static int vcn_v4_0_dec_msg(struct amdgpu_cs_parser *p, struct amdgpu_job *job, 1617 uint64_t addr) 1618 { 1619 struct ttm_operation_ctx ctx = { false, false }; 1620 struct amdgpu_bo_va_mapping *map; 1621 uint32_t *msg, num_buffers; 1622 struct amdgpu_bo *bo; 1623 uint64_t start, end; 1624 unsigned int i; 1625 void *ptr; 1626 int r; 1627 1628 addr &= AMDGPU_GMC_HOLE_MASK; 1629 r = amdgpu_cs_find_mapping(p, addr, &bo, &map); 1630 if (r) { 1631 DRM_ERROR("Can't find BO for addr 0x%08llx\n", addr); 1632 return r; 1633 } 1634 1635 start = map->start * AMDGPU_GPU_PAGE_SIZE; 1636 end = (map->last + 1) * AMDGPU_GPU_PAGE_SIZE; 1637 if (addr & 0x7) { 1638 DRM_ERROR("VCN messages must be 8 byte aligned!\n"); 1639 return -EINVAL; 1640 } 1641 1642 bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 1643 amdgpu_bo_placement_from_domain(bo, bo->allowed_domains); 1644 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 1645 if (r) { 1646 DRM_ERROR("Failed validating the VCN message BO (%d)!\n", r); 1647 return r; 1648 } 1649 1650 r = amdgpu_bo_kmap(bo, &ptr); 1651 if (r) { 1652 DRM_ERROR("Failed mapping the VCN message (%d)!\n", r); 1653 return r; 1654 } 1655 1656 msg = ptr + addr - start; 1657 1658 /* Check length */ 1659 if (msg[1] > end - addr) { 1660 r = -EINVAL; 1661 goto out; 1662 } 1663 1664 if (msg[3] != RDECODE_MSG_CREATE) 1665 goto out; 1666 1667 num_buffers = msg[2]; 1668 for (i = 0, msg = &msg[6]; i < num_buffers; ++i, msg += 4) { 1669 uint32_t offset, size, *create; 1670 1671 if (msg[0] != RDECODE_MESSAGE_CREATE) 1672 continue; 1673 1674 offset = msg[1]; 1675 size = msg[2]; 1676 1677 if (offset + size > end) { 1678 r = -EINVAL; 1679 goto out; 1680 } 1681 1682 create = ptr + addr + offset - start; 1683 1684 /* H246, HEVC and VP9 can run on any instance */ 1685 if (create[0] == 0x7 || create[0] == 0x10 || create[0] == 0x11) 1686 continue; 1687 1688 r = vcn_v4_0_limit_sched(p, job); 1689 if (r) 1690 goto out; 1691 } 1692 1693 out: 1694 amdgpu_bo_kunmap(bo); 1695 return r; 1696 } 1697 1698 #define RADEON_VCN_ENGINE_TYPE_DECODE (0x00000003) 1699 1700 static int vcn_v4_0_ring_patch_cs_in_place(struct amdgpu_cs_parser *p, 1701 struct amdgpu_job *job, 1702 struct amdgpu_ib *ib) 1703 { 1704 struct amdgpu_ring *ring = amdgpu_job_ring(job); 1705 struct amdgpu_vcn_decode_buffer *decode_buffer; 1706 uint64_t addr; 1707 uint32_t val; 1708 1709 /* The first instance can decode anything */ 1710 if (!ring->me) 1711 return 0; 1712 1713 /* unified queue ib header has 8 double words. */ 1714 if (ib->length_dw < 8) 1715 return 0; 1716 1717 val = amdgpu_ib_get_value(ib, 6); //RADEON_VCN_ENGINE_TYPE 1718 if (val != RADEON_VCN_ENGINE_TYPE_DECODE) 1719 return 0; 1720 1721 decode_buffer = (struct amdgpu_vcn_decode_buffer *)&ib->ptr[10]; 1722 1723 if (!(decode_buffer->valid_buf_flag & 0x1)) 1724 return 0; 1725 1726 addr = ((u64)decode_buffer->msg_buffer_address_hi) << 32 | 1727 decode_buffer->msg_buffer_address_lo; 1728 return vcn_v4_0_dec_msg(p, job, addr); 1729 } 1730 1731 static const struct amdgpu_ring_funcs vcn_v4_0_unified_ring_vm_funcs = { 1732 .type = AMDGPU_RING_TYPE_VCN_ENC, 1733 .align_mask = 0x3f, 1734 .nop = VCN_ENC_CMD_NO_OP, 1735 .vmhub = AMDGPU_MMHUB_0, 1736 .get_rptr = vcn_v4_0_unified_ring_get_rptr, 1737 .get_wptr = vcn_v4_0_unified_ring_get_wptr, 1738 .set_wptr = vcn_v4_0_unified_ring_set_wptr, 1739 .patch_cs_in_place = vcn_v4_0_ring_patch_cs_in_place, 1740 .emit_frame_size = 1741 SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 + 1742 SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 + 1743 4 + /* vcn_v2_0_enc_ring_emit_vm_flush */ 1744 5 + 5 + /* vcn_v2_0_enc_ring_emit_fence x2 vm fence */ 1745 1, /* vcn_v2_0_enc_ring_insert_end */ 1746 .emit_ib_size = 5, /* vcn_v2_0_enc_ring_emit_ib */ 1747 .emit_ib = vcn_v2_0_enc_ring_emit_ib, 1748 .emit_fence = vcn_v2_0_enc_ring_emit_fence, 1749 .emit_vm_flush = vcn_v2_0_enc_ring_emit_vm_flush, 1750 .test_ring = amdgpu_vcn_enc_ring_test_ring, 1751 .test_ib = amdgpu_vcn_unified_ring_test_ib, 1752 .insert_nop = amdgpu_ring_insert_nop, 1753 .insert_end = vcn_v2_0_enc_ring_insert_end, 1754 .pad_ib = amdgpu_ring_generic_pad_ib, 1755 .begin_use = amdgpu_vcn_ring_begin_use, 1756 .end_use = amdgpu_vcn_ring_end_use, 1757 .emit_wreg = vcn_v2_0_enc_ring_emit_wreg, 1758 .emit_reg_wait = vcn_v2_0_enc_ring_emit_reg_wait, 1759 .emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper, 1760 }; 1761 1762 /** 1763 * vcn_v4_0_set_unified_ring_funcs - set unified ring functions 1764 * 1765 * @adev: amdgpu_device pointer 1766 * 1767 * Set unified ring functions 1768 */ 1769 static void vcn_v4_0_set_unified_ring_funcs(struct amdgpu_device *adev) 1770 { 1771 int i; 1772 1773 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1774 if (adev->vcn.harvest_config & (1 << i)) 1775 continue; 1776 1777 adev->vcn.inst[i].ring_enc[0].funcs = &vcn_v4_0_unified_ring_vm_funcs; 1778 adev->vcn.inst[i].ring_enc[0].me = i; 1779 1780 DRM_INFO("VCN(%d) encode/decode are enabled in VM mode\n", i); 1781 } 1782 } 1783 1784 /** 1785 * vcn_v4_0_is_idle - check VCN block is idle 1786 * 1787 * @handle: amdgpu_device pointer 1788 * 1789 * Check whether VCN block is idle 1790 */ 1791 static bool vcn_v4_0_is_idle(void *handle) 1792 { 1793 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1794 int i, ret = 1; 1795 1796 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1797 if (adev->vcn.harvest_config & (1 << i)) 1798 continue; 1799 1800 ret &= (RREG32_SOC15(VCN, i, regUVD_STATUS) == UVD_STATUS__IDLE); 1801 } 1802 1803 return ret; 1804 } 1805 1806 /** 1807 * vcn_v4_0_wait_for_idle - wait for VCN block idle 1808 * 1809 * @handle: amdgpu_device pointer 1810 * 1811 * Wait for VCN block idle 1812 */ 1813 static int vcn_v4_0_wait_for_idle(void *handle) 1814 { 1815 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1816 int i, ret = 0; 1817 1818 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1819 if (adev->vcn.harvest_config & (1 << i)) 1820 continue; 1821 1822 ret = SOC15_WAIT_ON_RREG(VCN, i, regUVD_STATUS, UVD_STATUS__IDLE, 1823 UVD_STATUS__IDLE); 1824 if (ret) 1825 return ret; 1826 } 1827 1828 return ret; 1829 } 1830 1831 /** 1832 * vcn_v4_0_set_clockgating_state - set VCN block clockgating state 1833 * 1834 * @handle: amdgpu_device pointer 1835 * @state: clock gating state 1836 * 1837 * Set VCN block clockgating state 1838 */ 1839 static int vcn_v4_0_set_clockgating_state(void *handle, enum amd_clockgating_state state) 1840 { 1841 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1842 bool enable = (state == AMD_CG_STATE_GATE) ? true : false; 1843 int i; 1844 1845 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1846 if (adev->vcn.harvest_config & (1 << i)) 1847 continue; 1848 1849 if (enable) { 1850 if (RREG32_SOC15(VCN, i, regUVD_STATUS) != UVD_STATUS__IDLE) 1851 return -EBUSY; 1852 vcn_v4_0_enable_clock_gating(adev, i); 1853 } else { 1854 vcn_v4_0_disable_clock_gating(adev, i); 1855 } 1856 } 1857 1858 return 0; 1859 } 1860 1861 /** 1862 * vcn_v4_0_set_powergating_state - set VCN block powergating state 1863 * 1864 * @handle: amdgpu_device pointer 1865 * @state: power gating state 1866 * 1867 * Set VCN block powergating state 1868 */ 1869 static int vcn_v4_0_set_powergating_state(void *handle, enum amd_powergating_state state) 1870 { 1871 struct amdgpu_device *adev = (struct amdgpu_device *)handle; 1872 int ret; 1873 1874 /* for SRIOV, guest should not control VCN Power-gating 1875 * MMSCH FW should control Power-gating and clock-gating 1876 * guest should avoid touching CGC and PG 1877 */ 1878 if (amdgpu_sriov_vf(adev)) { 1879 adev->vcn.cur_state = AMD_PG_STATE_UNGATE; 1880 return 0; 1881 } 1882 1883 if(state == adev->vcn.cur_state) 1884 return 0; 1885 1886 if (state == AMD_PG_STATE_GATE) 1887 ret = vcn_v4_0_stop(adev); 1888 else 1889 ret = vcn_v4_0_start(adev); 1890 1891 if(!ret) 1892 adev->vcn.cur_state = state; 1893 1894 return ret; 1895 } 1896 1897 /** 1898 * vcn_v4_0_set_interrupt_state - set VCN block interrupt state 1899 * 1900 * @adev: amdgpu_device pointer 1901 * @source: interrupt sources 1902 * @type: interrupt types 1903 * @state: interrupt states 1904 * 1905 * Set VCN block interrupt state 1906 */ 1907 static int vcn_v4_0_set_interrupt_state(struct amdgpu_device *adev, struct amdgpu_irq_src *source, 1908 unsigned type, enum amdgpu_interrupt_state state) 1909 { 1910 return 0; 1911 } 1912 1913 /** 1914 * vcn_v4_0_process_interrupt - process VCN block interrupt 1915 * 1916 * @adev: amdgpu_device pointer 1917 * @source: interrupt sources 1918 * @entry: interrupt entry from clients and sources 1919 * 1920 * Process VCN block interrupt 1921 */ 1922 static int vcn_v4_0_process_interrupt(struct amdgpu_device *adev, struct amdgpu_irq_src *source, 1923 struct amdgpu_iv_entry *entry) 1924 { 1925 uint32_t ip_instance; 1926 1927 switch (entry->client_id) { 1928 case SOC15_IH_CLIENTID_VCN: 1929 ip_instance = 0; 1930 break; 1931 case SOC15_IH_CLIENTID_VCN1: 1932 ip_instance = 1; 1933 break; 1934 default: 1935 DRM_ERROR("Unhandled client id: %d\n", entry->client_id); 1936 return 0; 1937 } 1938 1939 DRM_DEBUG("IH: VCN TRAP\n"); 1940 1941 switch (entry->src_id) { 1942 case VCN_4_0__SRCID__UVD_ENC_GENERAL_PURPOSE: 1943 amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_enc[0]); 1944 break; 1945 case VCN_4_0__SRCID_UVD_POISON: 1946 amdgpu_vcn_process_poison_irq(adev, source, entry); 1947 break; 1948 default: 1949 DRM_ERROR("Unhandled interrupt: %d %d\n", 1950 entry->src_id, entry->src_data[0]); 1951 break; 1952 } 1953 1954 return 0; 1955 } 1956 1957 static const struct amdgpu_irq_src_funcs vcn_v4_0_irq_funcs = { 1958 .set = vcn_v4_0_set_interrupt_state, 1959 .process = vcn_v4_0_process_interrupt, 1960 }; 1961 1962 /** 1963 * vcn_v4_0_set_irq_funcs - set VCN block interrupt irq functions 1964 * 1965 * @adev: amdgpu_device pointer 1966 * 1967 * Set VCN block interrupt irq functions 1968 */ 1969 static void vcn_v4_0_set_irq_funcs(struct amdgpu_device *adev) 1970 { 1971 int i; 1972 1973 for (i = 0; i < adev->vcn.num_vcn_inst; ++i) { 1974 if (adev->vcn.harvest_config & (1 << i)) 1975 continue; 1976 1977 adev->vcn.inst[i].irq.num_types = adev->vcn.num_enc_rings + 1; 1978 adev->vcn.inst[i].irq.funcs = &vcn_v4_0_irq_funcs; 1979 } 1980 } 1981 1982 static const struct amd_ip_funcs vcn_v4_0_ip_funcs = { 1983 .name = "vcn_v4_0", 1984 .early_init = vcn_v4_0_early_init, 1985 .late_init = NULL, 1986 .sw_init = vcn_v4_0_sw_init, 1987 .sw_fini = vcn_v4_0_sw_fini, 1988 .hw_init = vcn_v4_0_hw_init, 1989 .hw_fini = vcn_v4_0_hw_fini, 1990 .suspend = vcn_v4_0_suspend, 1991 .resume = vcn_v4_0_resume, 1992 .is_idle = vcn_v4_0_is_idle, 1993 .wait_for_idle = vcn_v4_0_wait_for_idle, 1994 .check_soft_reset = NULL, 1995 .pre_soft_reset = NULL, 1996 .soft_reset = NULL, 1997 .post_soft_reset = NULL, 1998 .set_clockgating_state = vcn_v4_0_set_clockgating_state, 1999 .set_powergating_state = vcn_v4_0_set_powergating_state, 2000 }; 2001 2002 const struct amdgpu_ip_block_version vcn_v4_0_ip_block = 2003 { 2004 .type = AMD_IP_BLOCK_TYPE_VCN, 2005 .major = 4, 2006 .minor = 0, 2007 .rev = 0, 2008 .funcs = &vcn_v4_0_ip_funcs, 2009 }; 2010 2011 static uint32_t vcn_v4_0_query_poison_by_instance(struct amdgpu_device *adev, 2012 uint32_t instance, uint32_t sub_block) 2013 { 2014 uint32_t poison_stat = 0, reg_value = 0; 2015 2016 switch (sub_block) { 2017 case AMDGPU_VCN_V4_0_VCPU_VCODEC: 2018 reg_value = RREG32_SOC15(VCN, instance, regUVD_RAS_VCPU_VCODEC_STATUS); 2019 poison_stat = REG_GET_FIELD(reg_value, UVD_RAS_VCPU_VCODEC_STATUS, POISONED_PF); 2020 break; 2021 default: 2022 break; 2023 } 2024 2025 if (poison_stat) 2026 dev_info(adev->dev, "Poison detected in VCN%d, sub_block%d\n", 2027 instance, sub_block); 2028 2029 return poison_stat; 2030 } 2031 2032 static bool vcn_v4_0_query_ras_poison_status(struct amdgpu_device *adev) 2033 { 2034 uint32_t inst, sub; 2035 uint32_t poison_stat = 0; 2036 2037 for (inst = 0; inst < adev->vcn.num_vcn_inst; inst++) 2038 for (sub = 0; sub < AMDGPU_VCN_V4_0_MAX_SUB_BLOCK; sub++) 2039 poison_stat += 2040 vcn_v4_0_query_poison_by_instance(adev, inst, sub); 2041 2042 return !!poison_stat; 2043 } 2044 2045 const struct amdgpu_ras_block_hw_ops vcn_v4_0_ras_hw_ops = { 2046 .query_poison_status = vcn_v4_0_query_ras_poison_status, 2047 }; 2048 2049 static struct amdgpu_vcn_ras vcn_v4_0_ras = { 2050 .ras_block = { 2051 .hw_ops = &vcn_v4_0_ras_hw_ops, 2052 }, 2053 }; 2054 2055 static void vcn_v4_0_set_ras_funcs(struct amdgpu_device *adev) 2056 { 2057 switch (adev->ip_versions[VCN_HWIP][0]) { 2058 case IP_VERSION(4, 0, 0): 2059 adev->vcn.ras = &vcn_v4_0_ras; 2060 break; 2061 default: 2062 break; 2063 } 2064 2065 amdgpu_vcn_set_ras_funcs(adev); 2066 } 2067