Linux Audio

Check our new training course

Loading...
v6.2
   1/*
   2 * Copyright 2019 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 <drm/drm_drv.h>
  26
  27#include "amdgpu.h"
  28#include "amdgpu_vcn.h"
  29#include "amdgpu_pm.h"
  30#include "soc15.h"
  31#include "soc15d.h"
  32#include "vcn_v2_0.h"
  33#include "mmsch_v1_0.h"
  34#include "vcn_v2_5.h"
  35
  36#include "vcn/vcn_2_5_offset.h"
  37#include "vcn/vcn_2_5_sh_mask.h"
  38#include "ivsrcid/vcn/irqsrcs_vcn_2_0.h"
  39
  40#define VCN_VID_SOC_ADDRESS_2_0					0x1fa00
  41#define VCN1_VID_SOC_ADDRESS_3_0				0x48200
  42
  43#define mmUVD_CONTEXT_ID_INTERNAL_OFFSET			0x27
  44#define mmUVD_GPCOM_VCPU_CMD_INTERNAL_OFFSET			0x0f
  45#define mmUVD_GPCOM_VCPU_DATA0_INTERNAL_OFFSET			0x10
  46#define mmUVD_GPCOM_VCPU_DATA1_INTERNAL_OFFSET			0x11
  47#define mmUVD_NO_OP_INTERNAL_OFFSET				0x29
  48#define mmUVD_GP_SCRATCH8_INTERNAL_OFFSET			0x66
  49#define mmUVD_SCRATCH9_INTERNAL_OFFSET				0xc01d
  50
  51#define mmUVD_LMI_RBC_IB_VMID_INTERNAL_OFFSET			0x431
  52#define mmUVD_LMI_RBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET		0x3b4
  53#define mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET		0x3b5
  54#define mmUVD_RBC_IB_SIZE_INTERNAL_OFFSET			0x25c
  55
  56#define VCN25_MAX_HW_INSTANCES_ARCTURUS			2
  57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  58static void vcn_v2_5_set_dec_ring_funcs(struct amdgpu_device *adev);
  59static void vcn_v2_5_set_enc_ring_funcs(struct amdgpu_device *adev);
  60static void vcn_v2_5_set_irq_funcs(struct amdgpu_device *adev);
  61static int vcn_v2_5_set_powergating_state(void *handle,
  62				enum amd_powergating_state state);
  63static int vcn_v2_5_pause_dpg_mode(struct amdgpu_device *adev,
  64				int inst_idx, struct dpg_pause_state *new_state);
  65static int vcn_v2_5_sriov_start(struct amdgpu_device *adev);
  66static void vcn_v2_5_set_ras_funcs(struct amdgpu_device *adev);
  67
  68static int amdgpu_ih_clientid_vcns[] = {
  69	SOC15_IH_CLIENTID_VCN,
  70	SOC15_IH_CLIENTID_VCN1
  71};
  72
  73/**
  74 * vcn_v2_5_early_init - set function pointers
  75 *
  76 * @handle: amdgpu_device pointer
  77 *
  78 * Set ring and irq function pointers
 
  79 */
  80static int vcn_v2_5_early_init(void *handle)
  81{
  82	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  83
  84	if (amdgpu_sriov_vf(adev)) {
  85		adev->vcn.num_vcn_inst = 2;
  86		adev->vcn.harvest_config = 0;
  87		adev->vcn.num_enc_rings = 1;
  88	} else {
  89		u32 harvest;
  90		int i;
  91
  92		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
  93			harvest = RREG32_SOC15(VCN, i, mmCC_UVD_HARVESTING);
  94			if (harvest & CC_UVD_HARVESTING__UVD_DISABLE_MASK)
  95				adev->vcn.harvest_config |= 1 << i;
  96		}
  97		if (adev->vcn.harvest_config == (AMDGPU_VCN_HARVEST_VCN0 |
  98					AMDGPU_VCN_HARVEST_VCN1))
  99			/* both instances are harvested, disable the block */
 100			return -ENOENT;
 101
 102		adev->vcn.num_enc_rings = 2;
 103	}
 104
 105	vcn_v2_5_set_dec_ring_funcs(adev);
 106	vcn_v2_5_set_enc_ring_funcs(adev);
 107	vcn_v2_5_set_irq_funcs(adev);
 108	vcn_v2_5_set_ras_funcs(adev);
 109
 110	return 0;
 111}
 112
 113/**
 114 * vcn_v2_5_sw_init - sw init for VCN block
 115 *
 116 * @handle: amdgpu_device pointer
 117 *
 118 * Load firmware and sw initialization
 119 */
 120static int vcn_v2_5_sw_init(void *handle)
 121{
 122	struct amdgpu_ring *ring;
 123	int i, j, r;
 124	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 
 
 125
 126	for (j = 0; j < adev->vcn.num_vcn_inst; j++) {
 127		if (adev->vcn.harvest_config & (1 << j))
 128			continue;
 129		/* VCN DEC TRAP */
 130		r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 131				VCN_2_0__SRCID__UVD_SYSTEM_MESSAGE_INTERRUPT, &adev->vcn.inst[j].irq);
 132		if (r)
 133			return r;
 134
 135		/* VCN ENC TRAP */
 136		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 137			r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 138				i + VCN_2_0__SRCID__UVD_ENC_GENERAL_PURPOSE, &adev->vcn.inst[j].irq);
 139			if (r)
 140				return r;
 141		}
 142
 143		/* VCN POISON TRAP */
 144		r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 145			VCN_2_6__SRCID_UVD_POISON, &adev->vcn.inst[j].irq);
 146		if (r)
 147			return r;
 148	}
 149
 150	r = amdgpu_vcn_sw_init(adev);
 151	if (r)
 152		return r;
 153
 154	amdgpu_vcn_setup_ucode(adev);
 155
 156	r = amdgpu_vcn_resume(adev);
 157	if (r)
 158		return r;
 159
 160	for (j = 0; j < adev->vcn.num_vcn_inst; j++) {
 161		volatile struct amdgpu_fw_shared *fw_shared;
 162
 163		if (adev->vcn.harvest_config & (1 << j))
 164			continue;
 165		adev->vcn.internal.context_id = mmUVD_CONTEXT_ID_INTERNAL_OFFSET;
 166		adev->vcn.internal.ib_vmid = mmUVD_LMI_RBC_IB_VMID_INTERNAL_OFFSET;
 167		adev->vcn.internal.ib_bar_low = mmUVD_LMI_RBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET;
 168		adev->vcn.internal.ib_bar_high = mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET;
 169		adev->vcn.internal.ib_size = mmUVD_RBC_IB_SIZE_INTERNAL_OFFSET;
 170		adev->vcn.internal.gp_scratch8 = mmUVD_GP_SCRATCH8_INTERNAL_OFFSET;
 171
 172		adev->vcn.internal.scratch9 = mmUVD_SCRATCH9_INTERNAL_OFFSET;
 173		adev->vcn.inst[j].external.scratch9 = SOC15_REG_OFFSET(VCN, j, mmUVD_SCRATCH9);
 174		adev->vcn.internal.data0 = mmUVD_GPCOM_VCPU_DATA0_INTERNAL_OFFSET;
 175		adev->vcn.inst[j].external.data0 = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_DATA0);
 176		adev->vcn.internal.data1 = mmUVD_GPCOM_VCPU_DATA1_INTERNAL_OFFSET;
 177		adev->vcn.inst[j].external.data1 = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_DATA1);
 178		adev->vcn.internal.cmd = mmUVD_GPCOM_VCPU_CMD_INTERNAL_OFFSET;
 179		adev->vcn.inst[j].external.cmd = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_CMD);
 180		adev->vcn.internal.nop = mmUVD_NO_OP_INTERNAL_OFFSET;
 181		adev->vcn.inst[j].external.nop = SOC15_REG_OFFSET(VCN, j, mmUVD_NO_OP);
 182
 183		ring = &adev->vcn.inst[j].ring_dec;
 184		ring->use_doorbell = true;
 185
 186		ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) +
 187				(amdgpu_sriov_vf(adev) ? 2*j : 8*j);
 
 
 
 
 
 
 188		sprintf(ring->name, "vcn_dec_%d", j);
 189		r = amdgpu_ring_init(adev, ring, 512, &adev->vcn.inst[j].irq,
 190				     0, AMDGPU_RING_PRIO_DEFAULT, NULL);
 191		if (r)
 192			return r;
 193
 194		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 195			enum amdgpu_ring_priority_level hw_prio = amdgpu_vcn_get_enc_ring_prio(i);
 196
 197			ring = &adev->vcn.inst[j].ring_enc[i];
 198			ring->use_doorbell = true;
 199
 200			ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) +
 201					(amdgpu_sriov_vf(adev) ? (1 + i + 2*j) : (2 + i + 8*j));
 202
 
 
 
 
 
 
 203			sprintf(ring->name, "vcn_enc_%d.%d", j, i);
 204			r = amdgpu_ring_init(adev, ring, 512,
 205					     &adev->vcn.inst[j].irq, 0,
 206					     hw_prio, NULL);
 207			if (r)
 208				return r;
 209		}
 210
 211		fw_shared = adev->vcn.inst[j].fw_shared.cpu_addr;
 212		fw_shared->present_flag_0 = cpu_to_le32(AMDGPU_VCN_MULTI_QUEUE_FLAG);
 213
 214		if (amdgpu_vcnfw_log)
 215			amdgpu_vcn_fwlog_init(&adev->vcn.inst[i]);
 216	}
 217
 218	if (amdgpu_sriov_vf(adev)) {
 219		r = amdgpu_virt_alloc_mm_table(adev);
 220		if (r)
 221			return r;
 222	}
 223
 224	if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)
 225		adev->vcn.pause_dpg_mode = vcn_v2_5_pause_dpg_mode;
 226
 
 
 
 
 
 
 
 
 
 
 
 
 
 227	return 0;
 228}
 229
 230/**
 231 * vcn_v2_5_sw_fini - sw fini for VCN block
 232 *
 233 * @handle: amdgpu_device pointer
 234 *
 235 * VCN suspend and free up sw allocation
 236 */
 237static int vcn_v2_5_sw_fini(void *handle)
 238{
 239	int i, r, idx;
 240	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 241	volatile struct amdgpu_fw_shared *fw_shared;
 242
 243	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
 244		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
 245			if (adev->vcn.harvest_config & (1 << i))
 246				continue;
 247			fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr;
 248			fw_shared->present_flag_0 = 0;
 249		}
 250		drm_dev_exit(idx);
 251	}
 252
 253
 254	if (amdgpu_sriov_vf(adev))
 255		amdgpu_virt_free_mm_table(adev);
 256
 257	r = amdgpu_vcn_suspend(adev);
 258	if (r)
 259		return r;
 260
 261	r = amdgpu_vcn_sw_fini(adev);
 262
 
 
 263	return r;
 264}
 265
 266/**
 267 * vcn_v2_5_hw_init - start and test VCN block
 268 *
 269 * @handle: amdgpu_device pointer
 270 *
 271 * Initialize the hardware, boot up the VCPU and do some testing
 272 */
 273static int vcn_v2_5_hw_init(void *handle)
 274{
 275	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 276	struct amdgpu_ring *ring;
 277	int i, j, r = 0;
 278
 279	if (amdgpu_sriov_vf(adev))
 280		r = vcn_v2_5_sriov_start(adev);
 281
 282	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
 283		if (adev->vcn.harvest_config & (1 << j))
 284			continue;
 285
 286		if (amdgpu_sriov_vf(adev)) {
 287			adev->vcn.inst[j].ring_enc[0].sched.ready = true;
 288			adev->vcn.inst[j].ring_enc[1].sched.ready = false;
 289			adev->vcn.inst[j].ring_enc[2].sched.ready = false;
 290			adev->vcn.inst[j].ring_dec.sched.ready = true;
 291		} else {
 292
 293			ring = &adev->vcn.inst[j].ring_dec;
 294
 295			adev->nbio.funcs->vcn_doorbell_range(adev, ring->use_doorbell,
 296						     ring->doorbell_index, j);
 297
 298			r = amdgpu_ring_test_helper(ring);
 299			if (r)
 300				goto done;
 301
 302			for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 303				ring = &adev->vcn.inst[j].ring_enc[i];
 304				r = amdgpu_ring_test_helper(ring);
 305				if (r)
 306					goto done;
 307			}
 308		}
 309	}
 310
 311done:
 312	if (!r)
 313		DRM_INFO("VCN decode and encode initialized successfully(under %s).\n",
 314			(adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)?"DPG Mode":"SPG Mode");
 315
 316	return r;
 317}
 318
 319/**
 320 * vcn_v2_5_hw_fini - stop the hardware block
 321 *
 322 * @handle: amdgpu_device pointer
 323 *
 324 * Stop the VCN block, mark ring as not ready any more
 325 */
 326static int vcn_v2_5_hw_fini(void *handle)
 327{
 328	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 329	int i;
 330
 331	cancel_delayed_work_sync(&adev->vcn.idle_work);
 332
 333	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 334		if (adev->vcn.harvest_config & (1 << i))
 335			continue;
 336
 337		if ((adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) ||
 338		    (adev->vcn.cur_state != AMD_PG_STATE_GATE &&
 339		     RREG32_SOC15(VCN, i, mmUVD_STATUS)))
 340			vcn_v2_5_set_powergating_state(adev, AMD_PG_STATE_GATE);
 
 
 
 341	}
 342
 343	return 0;
 344}
 345
 346/**
 347 * vcn_v2_5_suspend - suspend VCN block
 348 *
 349 * @handle: amdgpu_device pointer
 350 *
 351 * HW fini and suspend VCN block
 352 */
 353static int vcn_v2_5_suspend(void *handle)
 354{
 355	int r;
 356	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 357
 358	r = vcn_v2_5_hw_fini(adev);
 359	if (r)
 360		return r;
 361
 362	r = amdgpu_vcn_suspend(adev);
 363
 364	return r;
 365}
 366
 367/**
 368 * vcn_v2_5_resume - resume VCN block
 369 *
 370 * @handle: amdgpu_device pointer
 371 *
 372 * Resume firmware and hw init VCN block
 373 */
 374static int vcn_v2_5_resume(void *handle)
 375{
 376	int r;
 377	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 378
 379	r = amdgpu_vcn_resume(adev);
 380	if (r)
 381		return r;
 382
 383	r = vcn_v2_5_hw_init(adev);
 384
 385	return r;
 386}
 387
 388/**
 389 * vcn_v2_5_mc_resume - memory controller programming
 390 *
 391 * @adev: amdgpu_device pointer
 392 *
 393 * Let the VCN memory controller know it's offsets
 394 */
 395static void vcn_v2_5_mc_resume(struct amdgpu_device *adev)
 396{
 397	uint32_t size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw->size + 4);
 398	uint32_t offset;
 399	int i;
 400
 401	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 402		if (adev->vcn.harvest_config & (1 << i))
 403			continue;
 
 
 404		/* cache window 0: fw */
 405		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
 406			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
 407				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_lo));
 408			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
 409				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_hi));
 410			WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET0, 0);
 411			offset = 0;
 412		} else {
 413			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
 414				lower_32_bits(adev->vcn.inst[i].gpu_addr));
 415			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
 416				upper_32_bits(adev->vcn.inst[i].gpu_addr));
 417			offset = size;
 418			WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET0,
 419				AMDGPU_UVD_FIRMWARE_OFFSET >> 3);
 420		}
 421		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE0, size);
 422
 423		/* cache window 1: stack */
 424		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW,
 425			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset));
 426		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH,
 427			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset));
 428		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET1, 0);
 429		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE1, AMDGPU_VCN_STACK_SIZE);
 430
 431		/* cache window 2: context */
 432		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW,
 433			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE));
 434		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH,
 435			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE));
 436		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET2, 0);
 437		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE2, AMDGPU_VCN_CONTEXT_SIZE);
 438
 439		/* non-cache window */
 440		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_NC0_64BIT_BAR_LOW,
 441			lower_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr));
 442		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH,
 443			upper_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr));
 444		WREG32_SOC15(VCN, i, mmUVD_VCPU_NONCACHE_OFFSET0, 0);
 445		WREG32_SOC15(VCN, i, mmUVD_VCPU_NONCACHE_SIZE0,
 446			AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_fw_shared)));
 447	}
 448}
 449
 450static void vcn_v2_5_mc_resume_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect)
 451{
 452	uint32_t size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw->size + 4);
 453	uint32_t offset;
 454
 455	/* cache window 0: fw */
 456	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
 457		if (!indirect) {
 458			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 459				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
 460				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_lo), 0, indirect);
 461			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 462				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
 463				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_hi), 0, indirect);
 464			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 465				VCN, 0, mmUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect);
 466		} else {
 467			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 468				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 0, 0, indirect);
 469			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 470				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 0, 0, indirect);
 471			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 472				VCN, 0, mmUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect);
 473		}
 474		offset = 0;
 475	} else {
 476		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 477			VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
 478			lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect);
 479		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 480			VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
 481			upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect);
 482		offset = size;
 483		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 484			VCN, 0, mmUVD_VCPU_CACHE_OFFSET0),
 485			AMDGPU_UVD_FIRMWARE_OFFSET >> 3, 0, indirect);
 486	}
 487
 488	if (!indirect)
 489		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 490			VCN, 0, mmUVD_VCPU_CACHE_SIZE0), size, 0, indirect);
 491	else
 492		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 493			VCN, 0, mmUVD_VCPU_CACHE_SIZE0), 0, 0, indirect);
 494
 495	/* cache window 1: stack */
 496	if (!indirect) {
 497		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 498			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW),
 499			lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect);
 500		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 501			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH),
 502			upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect);
 503		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 504			VCN, 0, mmUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect);
 505	} else {
 506		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 507			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW), 0, 0, indirect);
 508		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 509			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH), 0, 0, indirect);
 510		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 511			VCN, 0, mmUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect);
 512	}
 513	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 514		VCN, 0, mmUVD_VCPU_CACHE_SIZE1), AMDGPU_VCN_STACK_SIZE, 0, indirect);
 515
 516	/* cache window 2: context */
 517	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 518		VCN, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW),
 519		lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect);
 520	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 521		VCN, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH),
 522		upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect);
 523	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 524		VCN, 0, mmUVD_VCPU_CACHE_OFFSET2), 0, 0, indirect);
 525	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 526		VCN, 0, mmUVD_VCPU_CACHE_SIZE2), AMDGPU_VCN_CONTEXT_SIZE, 0, indirect);
 527
 528	/* non-cache window */
 529	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 530		VCN, 0, mmUVD_LMI_VCPU_NC0_64BIT_BAR_LOW),
 531		lower_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect);
 532	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 533		VCN, 0, mmUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH),
 534		upper_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect);
 535	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 536		VCN, 0, mmUVD_VCPU_NONCACHE_OFFSET0), 0, 0, indirect);
 537	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 538		VCN, 0, mmUVD_VCPU_NONCACHE_SIZE0),
 539		AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_fw_shared)), 0, indirect);
 540
 541	/* VCN global tiling registers */
 542	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 543		VCN, 0, mmUVD_GFX8_ADDR_CONFIG), adev->gfx.config.gb_addr_config, 0, indirect);
 544}
 545
 546/**
 547 * vcn_v2_5_disable_clock_gating - disable VCN clock gating
 548 *
 549 * @adev: amdgpu_device pointer
 550 *
 551 * Disable clock gating for VCN block
 552 */
 553static void vcn_v2_5_disable_clock_gating(struct amdgpu_device *adev)
 554{
 555	uint32_t data;
 556	int i;
 557
 558	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 559		if (adev->vcn.harvest_config & (1 << i))
 560			continue;
 561		/* UVD disable CGC */
 562		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 563		if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 564			data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 565		else
 566			data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
 567		data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 568		data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 569		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 570
 571		data = RREG32_SOC15(VCN, i, mmUVD_CGC_GATE);
 572		data &= ~(UVD_CGC_GATE__SYS_MASK
 573			| UVD_CGC_GATE__UDEC_MASK
 574			| UVD_CGC_GATE__MPEG2_MASK
 575			| UVD_CGC_GATE__REGS_MASK
 576			| UVD_CGC_GATE__RBC_MASK
 577			| UVD_CGC_GATE__LMI_MC_MASK
 578			| UVD_CGC_GATE__LMI_UMC_MASK
 579			| UVD_CGC_GATE__IDCT_MASK
 580			| UVD_CGC_GATE__MPRD_MASK
 581			| UVD_CGC_GATE__MPC_MASK
 582			| UVD_CGC_GATE__LBSI_MASK
 583			| UVD_CGC_GATE__LRBBM_MASK
 584			| UVD_CGC_GATE__UDEC_RE_MASK
 585			| UVD_CGC_GATE__UDEC_CM_MASK
 586			| UVD_CGC_GATE__UDEC_IT_MASK
 587			| UVD_CGC_GATE__UDEC_DB_MASK
 588			| UVD_CGC_GATE__UDEC_MP_MASK
 589			| UVD_CGC_GATE__WCB_MASK
 590			| UVD_CGC_GATE__VCPU_MASK
 591			| UVD_CGC_GATE__MMSCH_MASK);
 592
 593		WREG32_SOC15(VCN, i, mmUVD_CGC_GATE, data);
 594
 595		SOC15_WAIT_ON_RREG(VCN, i, mmUVD_CGC_GATE, 0,  0xFFFFFFFF);
 596
 597		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 598		data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK
 599			| UVD_CGC_CTRL__UDEC_CM_MODE_MASK
 600			| UVD_CGC_CTRL__UDEC_IT_MODE_MASK
 601			| UVD_CGC_CTRL__UDEC_DB_MODE_MASK
 602			| UVD_CGC_CTRL__UDEC_MP_MODE_MASK
 603			| UVD_CGC_CTRL__SYS_MODE_MASK
 604			| UVD_CGC_CTRL__UDEC_MODE_MASK
 605			| UVD_CGC_CTRL__MPEG2_MODE_MASK
 606			| UVD_CGC_CTRL__REGS_MODE_MASK
 607			| UVD_CGC_CTRL__RBC_MODE_MASK
 608			| UVD_CGC_CTRL__LMI_MC_MODE_MASK
 609			| UVD_CGC_CTRL__LMI_UMC_MODE_MASK
 610			| UVD_CGC_CTRL__IDCT_MODE_MASK
 611			| UVD_CGC_CTRL__MPRD_MODE_MASK
 612			| UVD_CGC_CTRL__MPC_MODE_MASK
 613			| UVD_CGC_CTRL__LBSI_MODE_MASK
 614			| UVD_CGC_CTRL__LRBBM_MODE_MASK
 615			| UVD_CGC_CTRL__WCB_MODE_MASK
 616			| UVD_CGC_CTRL__VCPU_MODE_MASK
 617			| UVD_CGC_CTRL__MMSCH_MODE_MASK);
 618		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 619
 620		/* turn on */
 621		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_GATE);
 622		data |= (UVD_SUVD_CGC_GATE__SRE_MASK
 623			| UVD_SUVD_CGC_GATE__SIT_MASK
 624			| UVD_SUVD_CGC_GATE__SMP_MASK
 625			| UVD_SUVD_CGC_GATE__SCM_MASK
 626			| UVD_SUVD_CGC_GATE__SDB_MASK
 627			| UVD_SUVD_CGC_GATE__SRE_H264_MASK
 628			| UVD_SUVD_CGC_GATE__SRE_HEVC_MASK
 629			| UVD_SUVD_CGC_GATE__SIT_H264_MASK
 630			| UVD_SUVD_CGC_GATE__SIT_HEVC_MASK
 631			| UVD_SUVD_CGC_GATE__SCM_H264_MASK
 632			| UVD_SUVD_CGC_GATE__SCM_HEVC_MASK
 633			| UVD_SUVD_CGC_GATE__SDB_H264_MASK
 634			| UVD_SUVD_CGC_GATE__SDB_HEVC_MASK
 635			| UVD_SUVD_CGC_GATE__SCLR_MASK
 636			| UVD_SUVD_CGC_GATE__UVD_SC_MASK
 637			| UVD_SUVD_CGC_GATE__ENT_MASK
 638			| UVD_SUVD_CGC_GATE__SIT_HEVC_DEC_MASK
 639			| UVD_SUVD_CGC_GATE__SIT_HEVC_ENC_MASK
 640			| UVD_SUVD_CGC_GATE__SITE_MASK
 641			| UVD_SUVD_CGC_GATE__SRE_VP9_MASK
 642			| UVD_SUVD_CGC_GATE__SCM_VP9_MASK
 643			| UVD_SUVD_CGC_GATE__SIT_VP9_DEC_MASK
 644			| UVD_SUVD_CGC_GATE__SDB_VP9_MASK
 645			| UVD_SUVD_CGC_GATE__IME_HEVC_MASK);
 646		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_GATE, data);
 647
 648		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL);
 649		data &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
 650			| UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
 651			| UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
 652			| UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
 653			| UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
 654			| UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
 655			| UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
 656			| UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
 657			| UVD_SUVD_CGC_CTRL__IME_MODE_MASK
 658			| UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
 659		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL, data);
 660	}
 661}
 662
 663static void vcn_v2_5_clock_gating_dpg_mode(struct amdgpu_device *adev,
 664		uint8_t sram_sel, int inst_idx, uint8_t indirect)
 665{
 666	uint32_t reg_data = 0;
 667
 668	/* enable sw clock gating control */
 669	if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 670		reg_data = 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 671	else
 672		reg_data = 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 673	reg_data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 674	reg_data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 675	reg_data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK |
 676		 UVD_CGC_CTRL__UDEC_CM_MODE_MASK |
 677		 UVD_CGC_CTRL__UDEC_IT_MODE_MASK |
 678		 UVD_CGC_CTRL__UDEC_DB_MODE_MASK |
 679		 UVD_CGC_CTRL__UDEC_MP_MODE_MASK |
 680		 UVD_CGC_CTRL__SYS_MODE_MASK |
 681		 UVD_CGC_CTRL__UDEC_MODE_MASK |
 682		 UVD_CGC_CTRL__MPEG2_MODE_MASK |
 683		 UVD_CGC_CTRL__REGS_MODE_MASK |
 684		 UVD_CGC_CTRL__RBC_MODE_MASK |
 685		 UVD_CGC_CTRL__LMI_MC_MODE_MASK |
 686		 UVD_CGC_CTRL__LMI_UMC_MODE_MASK |
 687		 UVD_CGC_CTRL__IDCT_MODE_MASK |
 688		 UVD_CGC_CTRL__MPRD_MODE_MASK |
 689		 UVD_CGC_CTRL__MPC_MODE_MASK |
 690		 UVD_CGC_CTRL__LBSI_MODE_MASK |
 691		 UVD_CGC_CTRL__LRBBM_MODE_MASK |
 692		 UVD_CGC_CTRL__WCB_MODE_MASK |
 693		 UVD_CGC_CTRL__VCPU_MODE_MASK |
 694		 UVD_CGC_CTRL__MMSCH_MODE_MASK);
 695	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 696		VCN, 0, mmUVD_CGC_CTRL), reg_data, sram_sel, indirect);
 697
 698	/* turn off clock gating */
 699	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 700		VCN, 0, mmUVD_CGC_GATE), 0, sram_sel, indirect);
 701
 702	/* turn on SUVD clock gating */
 703	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 704		VCN, 0, mmUVD_SUVD_CGC_GATE), 1, sram_sel, indirect);
 705
 706	/* turn on sw mode in UVD_SUVD_CGC_CTRL */
 707	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 708		VCN, 0, mmUVD_SUVD_CGC_CTRL), 0, sram_sel, indirect);
 709}
 710
 711/**
 712 * vcn_v2_5_enable_clock_gating - enable VCN clock gating
 713 *
 714 * @adev: amdgpu_device pointer
 715 *
 716 * Enable clock gating for VCN block
 717 */
 718static void vcn_v2_5_enable_clock_gating(struct amdgpu_device *adev)
 719{
 720	uint32_t data = 0;
 721	int i;
 722
 723	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 724		if (adev->vcn.harvest_config & (1 << i))
 725			continue;
 726		/* enable UVD CGC */
 727		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 728		if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 729			data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 730		else
 731			data |= 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 732		data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 733		data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 734		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 735
 736		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 737		data |= (UVD_CGC_CTRL__UDEC_RE_MODE_MASK
 738			| UVD_CGC_CTRL__UDEC_CM_MODE_MASK
 739			| UVD_CGC_CTRL__UDEC_IT_MODE_MASK
 740			| UVD_CGC_CTRL__UDEC_DB_MODE_MASK
 741			| UVD_CGC_CTRL__UDEC_MP_MODE_MASK
 742			| UVD_CGC_CTRL__SYS_MODE_MASK
 743			| UVD_CGC_CTRL__UDEC_MODE_MASK
 744			| UVD_CGC_CTRL__MPEG2_MODE_MASK
 745			| UVD_CGC_CTRL__REGS_MODE_MASK
 746			| UVD_CGC_CTRL__RBC_MODE_MASK
 747			| UVD_CGC_CTRL__LMI_MC_MODE_MASK
 748			| UVD_CGC_CTRL__LMI_UMC_MODE_MASK
 749			| UVD_CGC_CTRL__IDCT_MODE_MASK
 750			| UVD_CGC_CTRL__MPRD_MODE_MASK
 751			| UVD_CGC_CTRL__MPC_MODE_MASK
 752			| UVD_CGC_CTRL__LBSI_MODE_MASK
 753			| UVD_CGC_CTRL__LRBBM_MODE_MASK
 754			| UVD_CGC_CTRL__WCB_MODE_MASK
 755			| UVD_CGC_CTRL__VCPU_MODE_MASK);
 756		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 757
 758		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL);
 759		data |= (UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
 760			| UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
 761			| UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
 762			| UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
 763			| UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
 764			| UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
 765			| UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
 766			| UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
 767			| UVD_SUVD_CGC_CTRL__IME_MODE_MASK
 768			| UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
 769		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL, data);
 770	}
 771}
 772
 773static void vcn_v2_6_enable_ras(struct amdgpu_device *adev, int inst_idx,
 774				bool indirect)
 775{
 776	uint32_t tmp;
 777
 778	if (adev->ip_versions[UVD_HWIP][0] != IP_VERSION(2, 6, 0))
 779		return;
 780
 781	tmp = VCN_RAS_CNTL__VCPU_VCODEC_REARM_MASK |
 782	      VCN_RAS_CNTL__VCPU_VCODEC_IH_EN_MASK |
 783	      VCN_RAS_CNTL__VCPU_VCODEC_PMI_EN_MASK |
 784	      VCN_RAS_CNTL__VCPU_VCODEC_STALL_EN_MASK;
 785	WREG32_SOC15_DPG_MODE(inst_idx,
 786			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmVCN_RAS_CNTL),
 787			      tmp, 0, indirect);
 788
 789	tmp = UVD_VCPU_INT_EN__RASCNTL_VCPU_VCODEC_EN_MASK;
 790	WREG32_SOC15_DPG_MODE(inst_idx,
 791			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmUVD_VCPU_INT_EN),
 792			      tmp, 0, indirect);
 793
 794	tmp = UVD_SYS_INT_EN__RASCNTL_VCPU_VCODEC_EN_MASK;
 795	WREG32_SOC15_DPG_MODE(inst_idx,
 796			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmUVD_SYS_INT_EN),
 797			      tmp, 0, indirect);
 798}
 799
 800static int vcn_v2_5_start_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect)
 801{
 802	volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[inst_idx].fw_shared.cpu_addr;
 803	struct amdgpu_ring *ring;
 804	uint32_t rb_bufsz, tmp;
 805
 806	/* disable register anti-hang mechanism */
 807	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS), 1,
 808		~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
 809	/* enable dynamic power gating mode */
 810	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_POWER_STATUS);
 811	tmp |= UVD_POWER_STATUS__UVD_PG_MODE_MASK;
 812	tmp |= UVD_POWER_STATUS__UVD_PG_EN_MASK;
 813	WREG32_SOC15(VCN, inst_idx, mmUVD_POWER_STATUS, tmp);
 814
 815	if (indirect)
 816		adev->vcn.inst[inst_idx].dpg_sram_curr_addr = (uint32_t *)adev->vcn.inst[inst_idx].dpg_sram_cpu_addr;
 817
 818	/* enable clock gating */
 819	vcn_v2_5_clock_gating_dpg_mode(adev, 0, inst_idx, indirect);
 820
 821	/* enable VCPU clock */
 822	tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT);
 823	tmp |= UVD_VCPU_CNTL__CLK_EN_MASK;
 824	tmp |= UVD_VCPU_CNTL__BLK_RST_MASK;
 825	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 826		VCN, 0, mmUVD_VCPU_CNTL), tmp, 0, indirect);
 827
 828	/* disable master interupt */
 829	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 830		VCN, 0, mmUVD_MASTINT_EN), 0, 0, indirect);
 831
 832	/* setup mmUVD_LMI_CTRL */
 833	tmp = (0x8 | UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK |
 834		UVD_LMI_CTRL__REQ_MODE_MASK |
 835		UVD_LMI_CTRL__CRC_RESET_MASK |
 836		UVD_LMI_CTRL__MASK_MC_URGENT_MASK |
 837		UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK |
 838		UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK |
 839		(8 << UVD_LMI_CTRL__WRITE_CLEAN_TIMER__SHIFT) |
 840		0x00100000L);
 841	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 842		VCN, 0, mmUVD_LMI_CTRL), tmp, 0, indirect);
 843
 844	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 845		VCN, 0, mmUVD_MPC_CNTL),
 846		0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT, 0, indirect);
 847
 848	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 849		VCN, 0, mmUVD_MPC_SET_MUXA0),
 850		((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) |
 851		 (0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) |
 852		 (0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) |
 853		 (0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT)), 0, indirect);
 854
 855	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 856		VCN, 0, mmUVD_MPC_SET_MUXB0),
 857		((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) |
 858		 (0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) |
 859		 (0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) |
 860		 (0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT)), 0, indirect);
 861
 862	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 863		VCN, 0, mmUVD_MPC_SET_MUX),
 864		((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) |
 865		 (0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) |
 866		 (0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT)), 0, indirect);
 867
 868	vcn_v2_5_mc_resume_dpg_mode(adev, inst_idx, indirect);
 869
 870	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 871		VCN, 0, mmUVD_REG_XX_MASK), 0x10, 0, indirect);
 872	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 873		VCN, 0, mmUVD_RBC_XX_IB_REG_CHECK), 0x3, 0, indirect);
 874
 875	/* enable LMI MC and UMC channels */
 876	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 877		VCN, 0, mmUVD_LMI_CTRL2), 0, 0, indirect);
 878
 879	vcn_v2_6_enable_ras(adev, inst_idx, indirect);
 880
 881	/* unblock VCPU register access */
 882	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 883		VCN, 0, mmUVD_RB_ARB_CTRL), 0, 0, indirect);
 884
 885	tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT);
 886	tmp |= UVD_VCPU_CNTL__CLK_EN_MASK;
 887	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 888		VCN, 0, mmUVD_VCPU_CNTL), tmp, 0, indirect);
 889
 890	/* enable master interrupt */
 891	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 892		VCN, 0, mmUVD_MASTINT_EN),
 893		UVD_MASTINT_EN__VCPU_EN_MASK, 0, indirect);
 894
 895	if (indirect)
 896		psp_update_vcn_sram(adev, inst_idx, adev->vcn.inst[inst_idx].dpg_sram_gpu_addr,
 897				    (uint32_t)((uintptr_t)adev->vcn.inst[inst_idx].dpg_sram_curr_addr -
 898					       (uintptr_t)adev->vcn.inst[inst_idx].dpg_sram_cpu_addr));
 899
 900	ring = &adev->vcn.inst[inst_idx].ring_dec;
 901	/* force RBC into idle state */
 902	rb_bufsz = order_base_2(ring->ring_size);
 903	tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
 904	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
 905	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
 906	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
 907	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
 908	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_CNTL, tmp);
 909
 910	/* Stall DPG before WPTR/RPTR reset */
 911	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
 912		UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK,
 913		~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
 914	fw_shared->multi_queue.decode_queue_mode |= FW_QUEUE_RING_RESET;
 915
 916	/* set the write pointer delay */
 917	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR_CNTL, 0);
 918
 919	/* set the wb address */
 920	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR_ADDR,
 921		(upper_32_bits(ring->gpu_addr) >> 2));
 922
 923	/* program the RB_BASE for ring buffer */
 924	WREG32_SOC15(VCN, inst_idx, mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
 925		lower_32_bits(ring->gpu_addr));
 926	WREG32_SOC15(VCN, inst_idx, mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
 927		upper_32_bits(ring->gpu_addr));
 928
 929	/* Initialize the ring buffer's read and write pointers */
 930	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR, 0);
 931
 932	WREG32_SOC15(VCN, inst_idx, mmUVD_SCRATCH2, 0);
 933
 934	ring->wptr = RREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR);
 935	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR,
 936		lower_32_bits(ring->wptr));
 937
 938	fw_shared->multi_queue.decode_queue_mode &= ~FW_QUEUE_RING_RESET;
 939	/* Unstall DPG */
 940	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
 941		0, ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
 942
 943	return 0;
 944}
 945
 946static int vcn_v2_5_start(struct amdgpu_device *adev)
 947{
 948	struct amdgpu_ring *ring;
 949	uint32_t rb_bufsz, tmp;
 950	int i, j, k, r;
 951
 952	if (adev->pm.dpm_enabled)
 953		amdgpu_dpm_enable_uvd(adev, true);
 954
 955	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 956		if (adev->vcn.harvest_config & (1 << i))
 957			continue;
 958		if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
 959			r = vcn_v2_5_start_dpg_mode(adev, i, adev->vcn.indirect_sram);
 960			continue;
 961		}
 962
 963		/* disable register anti-hang mechanism */
 964		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_POWER_STATUS), 0,
 965			~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
 966
 967		/* set uvd status busy */
 968		tmp = RREG32_SOC15(VCN, i, mmUVD_STATUS) | UVD_STATUS__UVD_BUSY;
 969		WREG32_SOC15(VCN, i, mmUVD_STATUS, tmp);
 970	}
 971
 972	if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)
 973		return 0;
 974
 975	/*SW clock gating */
 976	vcn_v2_5_disable_clock_gating(adev);
 977
 978	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 979		if (adev->vcn.harvest_config & (1 << i))
 980			continue;
 981		/* enable VCPU clock */
 982		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
 983			UVD_VCPU_CNTL__CLK_EN_MASK, ~UVD_VCPU_CNTL__CLK_EN_MASK);
 984
 985		/* disable master interrupt */
 986		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_MASTINT_EN), 0,
 987			~UVD_MASTINT_EN__VCPU_EN_MASK);
 988
 989		/* setup mmUVD_LMI_CTRL */
 990		tmp = RREG32_SOC15(VCN, i, mmUVD_LMI_CTRL);
 991		tmp &= ~0xff;
 992		WREG32_SOC15(VCN, i, mmUVD_LMI_CTRL, tmp | 0x8|
 993			UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK	|
 994			UVD_LMI_CTRL__MASK_MC_URGENT_MASK |
 995			UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK |
 996			UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK);
 997
 998		/* setup mmUVD_MPC_CNTL */
 999		tmp = RREG32_SOC15(VCN, i, mmUVD_MPC_CNTL);
1000		tmp &= ~UVD_MPC_CNTL__REPLACEMENT_MODE_MASK;
1001		tmp |= 0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT;
1002		WREG32_SOC15(VCN, i, mmUVD_MPC_CNTL, tmp);
1003
1004		/* setup UVD_MPC_SET_MUXA0 */
1005		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUXA0,
1006			((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) |
1007			(0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) |
1008			(0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) |
1009			(0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT)));
1010
1011		/* setup UVD_MPC_SET_MUXB0 */
1012		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUXB0,
1013			((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) |
1014			(0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) |
1015			(0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) |
1016			(0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT)));
1017
1018		/* setup mmUVD_MPC_SET_MUX */
1019		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUX,
1020			((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) |
1021			(0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) |
1022			(0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT)));
1023	}
1024
1025	vcn_v2_5_mc_resume(adev);
1026
1027	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1028		volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr;
1029		if (adev->vcn.harvest_config & (1 << i))
1030			continue;
1031		/* VCN global tiling registers */
1032		WREG32_SOC15(VCN, i, mmUVD_GFX8_ADDR_CONFIG,
1033			adev->gfx.config.gb_addr_config);
1034		WREG32_SOC15(VCN, i, mmUVD_GFX8_ADDR_CONFIG,
1035			adev->gfx.config.gb_addr_config);
1036
1037		/* enable LMI MC and UMC channels */
1038		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_LMI_CTRL2), 0,
1039			~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
1040
1041		/* unblock VCPU register access */
1042		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_RB_ARB_CTRL), 0,
1043			~UVD_RB_ARB_CTRL__VCPU_DIS_MASK);
1044
1045		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1046			~UVD_VCPU_CNTL__BLK_RST_MASK);
1047
1048		for (k = 0; k < 10; ++k) {
1049			uint32_t status;
1050
1051			for (j = 0; j < 100; ++j) {
1052				status = RREG32_SOC15(VCN, i, mmUVD_STATUS);
1053				if (status & 2)
1054					break;
1055				if (amdgpu_emu_mode == 1)
1056					msleep(500);
1057				else
1058					mdelay(10);
1059			}
1060			r = 0;
1061			if (status & 2)
1062				break;
1063
1064			DRM_ERROR("VCN decode not responding, trying to reset the VCPU!!!\n");
1065			WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
1066				UVD_VCPU_CNTL__BLK_RST_MASK,
1067				~UVD_VCPU_CNTL__BLK_RST_MASK);
1068			mdelay(10);
1069			WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1070				~UVD_VCPU_CNTL__BLK_RST_MASK);
1071
1072			mdelay(10);
1073			r = -1;
1074		}
1075
1076		if (r) {
1077			DRM_ERROR("VCN decode not responding, giving up!!!\n");
1078			return r;
1079		}
1080
1081		/* enable master interrupt */
1082		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_MASTINT_EN),
1083			UVD_MASTINT_EN__VCPU_EN_MASK,
1084			~UVD_MASTINT_EN__VCPU_EN_MASK);
1085
1086		/* clear the busy bit of VCN_STATUS */
1087		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_STATUS), 0,
1088			~(2 << UVD_STATUS__VCPU_REPORT__SHIFT));
1089
1090		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_VMID, 0);
1091
1092		ring = &adev->vcn.inst[i].ring_dec;
1093		/* force RBC into idle state */
1094		rb_bufsz = order_base_2(ring->ring_size);
1095		tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
1096		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
1097		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
1098		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
1099		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
1100		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_CNTL, tmp);
1101
1102		fw_shared->multi_queue.decode_queue_mode |= FW_QUEUE_RING_RESET;
1103		/* program the RB_BASE for ring buffer */
1104		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
1105			lower_32_bits(ring->gpu_addr));
1106		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
1107			upper_32_bits(ring->gpu_addr));
1108
1109		/* Initialize the ring buffer's read and write pointers */
1110		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_RPTR, 0);
1111
1112		ring->wptr = RREG32_SOC15(VCN, i, mmUVD_RBC_RB_RPTR);
1113		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_WPTR,
1114				lower_32_bits(ring->wptr));
1115		fw_shared->multi_queue.decode_queue_mode &= ~FW_QUEUE_RING_RESET;
1116
1117		fw_shared->multi_queue.encode_generalpurpose_queue_mode |= FW_QUEUE_RING_RESET;
1118		ring = &adev->vcn.inst[i].ring_enc[0];
1119		WREG32_SOC15(VCN, i, mmUVD_RB_RPTR, lower_32_bits(ring->wptr));
1120		WREG32_SOC15(VCN, i, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1121		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_LO, ring->gpu_addr);
1122		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
1123		WREG32_SOC15(VCN, i, mmUVD_RB_SIZE, ring->ring_size / 4);
1124		fw_shared->multi_queue.encode_generalpurpose_queue_mode &= ~FW_QUEUE_RING_RESET;
1125
1126		fw_shared->multi_queue.encode_lowlatency_queue_mode |= FW_QUEUE_RING_RESET;
1127		ring = &adev->vcn.inst[i].ring_enc[1];
1128		WREG32_SOC15(VCN, i, mmUVD_RB_RPTR2, lower_32_bits(ring->wptr));
1129		WREG32_SOC15(VCN, i, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1130		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_LO2, ring->gpu_addr);
1131		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
1132		WREG32_SOC15(VCN, i, mmUVD_RB_SIZE2, ring->ring_size / 4);
1133		fw_shared->multi_queue.encode_lowlatency_queue_mode &= ~FW_QUEUE_RING_RESET;
1134	}
1135
1136	return 0;
1137}
1138
1139static int vcn_v2_5_mmsch_start(struct amdgpu_device *adev,
1140				struct amdgpu_mm_table *table)
1141{
1142	uint32_t data = 0, loop = 0, size = 0;
1143	uint64_t addr = table->gpu_addr;
1144	struct mmsch_v1_1_init_header *header = NULL;
1145
1146	header = (struct mmsch_v1_1_init_header *)table->cpu_addr;
1147	size = header->total_size;
1148
1149	/*
1150	 * 1, write to vce_mmsch_vf_ctx_addr_lo/hi register with GPU mc addr of
1151	 *  memory descriptor location
1152	 */
1153	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_ADDR_LO, lower_32_bits(addr));
1154	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_ADDR_HI, upper_32_bits(addr));
1155
1156	/* 2, update vmid of descriptor */
1157	data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_VMID);
1158	data &= ~MMSCH_VF_VMID__VF_CTX_VMID_MASK;
1159	/* use domain0 for MM scheduler */
1160	data |= (0 << MMSCH_VF_VMID__VF_CTX_VMID__SHIFT);
1161	WREG32_SOC15(VCN, 0, mmMMSCH_VF_VMID, data);
1162
1163	/* 3, notify mmsch about the size of this descriptor */
1164	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_SIZE, size);
1165
1166	/* 4, set resp to zero */
1167	WREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP, 0);
1168
1169	/*
1170	 * 5, kick off the initialization and wait until
1171	 * VCE_MMSCH_VF_MAILBOX_RESP becomes non-zero
1172	 */
1173	WREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_HOST, 0x10000001);
1174
1175	data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP);
1176	loop = 10;
1177	while ((data & 0x10000002) != 0x10000002) {
1178		udelay(100);
1179		data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP);
1180		loop--;
1181		if (!loop)
1182			break;
1183	}
1184
1185	if (!loop) {
1186		dev_err(adev->dev,
1187			"failed to init MMSCH, mmMMSCH_VF_MAILBOX_RESP = %x\n",
1188			data);
1189		return -EBUSY;
1190	}
1191
1192	return 0;
1193}
1194
1195static int vcn_v2_5_sriov_start(struct amdgpu_device *adev)
1196{
1197	struct amdgpu_ring *ring;
1198	uint32_t offset, size, tmp, i, rb_bufsz;
1199	uint32_t table_size = 0;
1200	struct mmsch_v1_0_cmd_direct_write direct_wt = { { 0 } };
1201	struct mmsch_v1_0_cmd_direct_read_modify_write direct_rd_mod_wt = { { 0 } };
1202	struct mmsch_v1_0_cmd_end end = { { 0 } };
1203	uint32_t *init_table = adev->virt.mm_table.cpu_addr;
1204	struct mmsch_v1_1_init_header *header = (struct mmsch_v1_1_init_header *)init_table;
1205
1206	direct_wt.cmd_header.command_type = MMSCH_COMMAND__DIRECT_REG_WRITE;
1207	direct_rd_mod_wt.cmd_header.command_type = MMSCH_COMMAND__DIRECT_REG_READ_MODIFY_WRITE;
1208	end.cmd_header.command_type = MMSCH_COMMAND__END;
1209
1210	header->version = MMSCH_VERSION;
1211	header->total_size = sizeof(struct mmsch_v1_1_init_header) >> 2;
1212	init_table += header->total_size;
1213
1214	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1215		header->eng[i].table_offset = header->total_size;
1216		header->eng[i].init_status = 0;
1217		header->eng[i].table_size = 0;
1218
1219		table_size = 0;
1220
1221		MMSCH_V1_0_INSERT_DIRECT_RD_MOD_WT(
1222			SOC15_REG_OFFSET(VCN, i, mmUVD_STATUS),
1223			~UVD_STATUS__UVD_BUSY, UVD_STATUS__UVD_BUSY);
1224
1225		size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw->size + 4);
1226		/* mc resume*/
1227		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1228			MMSCH_V1_0_INSERT_DIRECT_WT(
1229				SOC15_REG_OFFSET(VCN, i,
1230					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
1231				adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_lo);
1232			MMSCH_V1_0_INSERT_DIRECT_WT(
1233				SOC15_REG_OFFSET(VCN, i,
1234					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
1235				adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_hi);
1236			offset = 0;
1237			MMSCH_V1_0_INSERT_DIRECT_WT(
1238				SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET0), 0);
1239		} else {
1240			MMSCH_V1_0_INSERT_DIRECT_WT(
1241				SOC15_REG_OFFSET(VCN, i,
1242					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
1243				lower_32_bits(adev->vcn.inst[i].gpu_addr));
1244			MMSCH_V1_0_INSERT_DIRECT_WT(
1245				SOC15_REG_OFFSET(VCN, i,
1246					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
1247				upper_32_bits(adev->vcn.inst[i].gpu_addr));
1248			offset = size;
1249			MMSCH_V1_0_INSERT_DIRECT_WT(
1250				SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET0),
1251				AMDGPU_UVD_FIRMWARE_OFFSET >> 3);
1252		}
1253
1254		MMSCH_V1_0_INSERT_DIRECT_WT(
1255			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE0),
1256			size);
1257		MMSCH_V1_0_INSERT_DIRECT_WT(
1258			SOC15_REG_OFFSET(VCN, i,
1259				mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW),
1260			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset));
1261		MMSCH_V1_0_INSERT_DIRECT_WT(
1262			SOC15_REG_OFFSET(VCN, i,
1263				mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH),
1264			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset));
1265		MMSCH_V1_0_INSERT_DIRECT_WT(
1266			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET1),
1267			0);
1268		MMSCH_V1_0_INSERT_DIRECT_WT(
1269			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE1),
1270			AMDGPU_VCN_STACK_SIZE);
1271		MMSCH_V1_0_INSERT_DIRECT_WT(
1272			SOC15_REG_OFFSET(VCN, i,
1273				mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW),
1274			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset +
1275				AMDGPU_VCN_STACK_SIZE));
1276		MMSCH_V1_0_INSERT_DIRECT_WT(
1277			SOC15_REG_OFFSET(VCN, i,
1278				mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH),
1279			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset +
1280				AMDGPU_VCN_STACK_SIZE));
1281		MMSCH_V1_0_INSERT_DIRECT_WT(
1282			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET2),
1283			0);
1284		MMSCH_V1_0_INSERT_DIRECT_WT(
1285			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE2),
1286			AMDGPU_VCN_CONTEXT_SIZE);
1287
1288		ring = &adev->vcn.inst[i].ring_enc[0];
1289		ring->wptr = 0;
1290
1291		MMSCH_V1_0_INSERT_DIRECT_WT(
1292			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_BASE_LO),
1293			lower_32_bits(ring->gpu_addr));
1294		MMSCH_V1_0_INSERT_DIRECT_WT(
1295			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_BASE_HI),
1296			upper_32_bits(ring->gpu_addr));
1297		MMSCH_V1_0_INSERT_DIRECT_WT(
1298			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_SIZE),
1299			ring->ring_size / 4);
1300
1301		ring = &adev->vcn.inst[i].ring_dec;
1302		ring->wptr = 0;
1303		MMSCH_V1_0_INSERT_DIRECT_WT(
1304			SOC15_REG_OFFSET(VCN, i,
1305				mmUVD_LMI_RBC_RB_64BIT_BAR_LOW),
1306			lower_32_bits(ring->gpu_addr));
1307		MMSCH_V1_0_INSERT_DIRECT_WT(
1308			SOC15_REG_OFFSET(VCN, i,
1309				mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH),
1310			upper_32_bits(ring->gpu_addr));
1311
1312		/* force RBC into idle state */
1313		rb_bufsz = order_base_2(ring->ring_size);
1314		tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
1315		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
1316		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
1317		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
1318		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
1319		MMSCH_V1_0_INSERT_DIRECT_WT(
1320			SOC15_REG_OFFSET(VCN, i, mmUVD_RBC_RB_CNTL), tmp);
1321
1322		/* add end packet */
1323		memcpy((void *)init_table, &end, sizeof(struct mmsch_v1_0_cmd_end));
1324		table_size += sizeof(struct mmsch_v1_0_cmd_end) / 4;
1325		init_table += sizeof(struct mmsch_v1_0_cmd_end) / 4;
1326
1327		/* refine header */
1328		header->eng[i].table_size = table_size;
1329		header->total_size += table_size;
1330	}
1331
1332	return vcn_v2_5_mmsch_start(adev, &adev->virt.mm_table);
1333}
1334
1335static int vcn_v2_5_stop_dpg_mode(struct amdgpu_device *adev, int inst_idx)
1336{
1337	uint32_t tmp;
1338
1339	/* Wait for power status to be 1 */
1340	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 1,
1341		UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1342
1343	/* wait for read ptr to be equal to write ptr */
1344	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR);
1345	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RB_RPTR, tmp, 0xFFFFFFFF);
1346
1347	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR2);
1348	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RB_RPTR2, tmp, 0xFFFFFFFF);
1349
1350	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR) & 0x7FFFFFFF;
1351	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RBC_RB_RPTR, tmp, 0xFFFFFFFF);
1352
1353	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 1,
1354		UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1355
1356	/* disable dynamic power gating mode */
1357	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS), 0,
1358			~UVD_POWER_STATUS__UVD_PG_MODE_MASK);
1359
1360	return 0;
1361}
1362
1363static int vcn_v2_5_stop(struct amdgpu_device *adev)
1364{
1365	uint32_t tmp;
1366	int i, r = 0;
1367
1368	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1369		if (adev->vcn.harvest_config & (1 << i))
1370			continue;
1371		if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
1372			r = vcn_v2_5_stop_dpg_mode(adev, i);
1373			continue;
1374		}
1375
1376		/* wait for vcn idle */
1377		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_STATUS, UVD_STATUS__IDLE, 0x7);
1378		if (r)
1379			return r;
1380
1381		tmp = UVD_LMI_STATUS__VCPU_LMI_WRITE_CLEAN_MASK |
1382			UVD_LMI_STATUS__READ_CLEAN_MASK |
1383			UVD_LMI_STATUS__WRITE_CLEAN_MASK |
1384			UVD_LMI_STATUS__WRITE_CLEAN_RAW_MASK;
1385		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_LMI_STATUS, tmp, tmp);
1386		if (r)
1387			return r;
1388
1389		/* block LMI UMC channel */
1390		tmp = RREG32_SOC15(VCN, i, mmUVD_LMI_CTRL2);
1391		tmp |= UVD_LMI_CTRL2__STALL_ARB_UMC_MASK;
1392		WREG32_SOC15(VCN, i, mmUVD_LMI_CTRL2, tmp);
1393
1394		tmp = UVD_LMI_STATUS__UMC_READ_CLEAN_RAW_MASK|
1395			UVD_LMI_STATUS__UMC_WRITE_CLEAN_RAW_MASK;
1396		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_LMI_STATUS, tmp, tmp);
1397		if (r)
1398			return r;
1399
1400		/* block VCPU register access */
1401		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_RB_ARB_CTRL),
1402			UVD_RB_ARB_CTRL__VCPU_DIS_MASK,
1403			~UVD_RB_ARB_CTRL__VCPU_DIS_MASK);
1404
1405		/* reset VCPU */
1406		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
1407			UVD_VCPU_CNTL__BLK_RST_MASK,
1408			~UVD_VCPU_CNTL__BLK_RST_MASK);
1409
1410		/* disable VCPU clock */
1411		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1412			~(UVD_VCPU_CNTL__CLK_EN_MASK));
1413
1414		/* clear status */
1415		WREG32_SOC15(VCN, i, mmUVD_STATUS, 0);
1416
1417		vcn_v2_5_enable_clock_gating(adev);
1418
1419		/* enable register anti-hang mechanism */
1420		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_POWER_STATUS),
1421			UVD_POWER_STATUS__UVD_POWER_STATUS_MASK,
1422			~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1423	}
1424
1425	if (adev->pm.dpm_enabled)
1426		amdgpu_dpm_enable_uvd(adev, false);
1427
1428	return 0;
1429}
1430
1431static int vcn_v2_5_pause_dpg_mode(struct amdgpu_device *adev,
1432				int inst_idx, struct dpg_pause_state *new_state)
1433{
1434	struct amdgpu_ring *ring;
1435	uint32_t reg_data = 0;
1436	int ret_code = 0;
1437
1438	/* pause/unpause if state is changed */
1439	if (adev->vcn.inst[inst_idx].pause_state.fw_based != new_state->fw_based) {
1440		DRM_DEBUG("dpg pause state changed %d -> %d",
1441			adev->vcn.inst[inst_idx].pause_state.fw_based,	new_state->fw_based);
1442		reg_data = RREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE) &
1443			(~UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK);
1444
1445		if (new_state->fw_based == VCN_DPG_STATE__PAUSE) {
1446			ret_code = SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 0x1,
1447				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1448
1449			if (!ret_code) {
1450				volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[inst_idx].fw_shared.cpu_addr;
1451
1452				/* pause DPG */
1453				reg_data |= UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK;
1454				WREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE, reg_data);
1455
1456				/* wait for ACK */
1457				SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_DPG_PAUSE,
1458					   UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK,
1459					   UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK);
1460
1461				/* Stall DPG before WPTR/RPTR reset */
1462				WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
1463					   UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK,
1464					   ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
1465
1466				/* Restore */
1467				fw_shared->multi_queue.encode_generalpurpose_queue_mode |= FW_QUEUE_RING_RESET;
1468				ring = &adev->vcn.inst[inst_idx].ring_enc[0];
1469				ring->wptr = 0;
1470				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_LO, ring->gpu_addr);
1471				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
1472				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_SIZE, ring->ring_size / 4);
1473				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_RPTR, lower_32_bits(ring->wptr));
1474				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1475				fw_shared->multi_queue.encode_generalpurpose_queue_mode &= ~FW_QUEUE_RING_RESET;
1476
1477				fw_shared->multi_queue.encode_lowlatency_queue_mode |= FW_QUEUE_RING_RESET;
1478				ring = &adev->vcn.inst[inst_idx].ring_enc[1];
1479				ring->wptr = 0;
1480				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_LO2, ring->gpu_addr);
1481				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
1482				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_SIZE2, ring->ring_size / 4);
1483				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_RPTR2, lower_32_bits(ring->wptr));
1484				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1485				fw_shared->multi_queue.encode_lowlatency_queue_mode &= ~FW_QUEUE_RING_RESET;
1486
1487				/* Unstall DPG */
1488				WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
1489					   0, ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
1490
1491				SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS,
1492					   UVD_PGFSM_CONFIG__UVDM_UVDU_PWR_ON, UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1493			}
1494		} else {
1495			reg_data &= ~UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK;
1496			WREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE, reg_data);
1497			SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 0x1,
1498				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1499		}
1500		adev->vcn.inst[inst_idx].pause_state.fw_based = new_state->fw_based;
1501	}
1502
1503	return 0;
1504}
1505
1506/**
1507 * vcn_v2_5_dec_ring_get_rptr - get read pointer
1508 *
1509 * @ring: amdgpu_ring pointer
1510 *
1511 * Returns the current hardware read pointer
1512 */
1513static uint64_t vcn_v2_5_dec_ring_get_rptr(struct amdgpu_ring *ring)
1514{
1515	struct amdgpu_device *adev = ring->adev;
1516
1517	return RREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_RPTR);
1518}
1519
1520/**
1521 * vcn_v2_5_dec_ring_get_wptr - get write pointer
1522 *
1523 * @ring: amdgpu_ring pointer
1524 *
1525 * Returns the current hardware write pointer
1526 */
1527static uint64_t vcn_v2_5_dec_ring_get_wptr(struct amdgpu_ring *ring)
1528{
1529	struct amdgpu_device *adev = ring->adev;
1530
1531	if (ring->use_doorbell)
1532		return *ring->wptr_cpu_addr;
1533	else
1534		return RREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_WPTR);
1535}
1536
1537/**
1538 * vcn_v2_5_dec_ring_set_wptr - set write pointer
1539 *
1540 * @ring: amdgpu_ring pointer
1541 *
1542 * Commits the write pointer to the hardware
1543 */
1544static void vcn_v2_5_dec_ring_set_wptr(struct amdgpu_ring *ring)
1545{
1546	struct amdgpu_device *adev = ring->adev;
1547
1548	if (ring->use_doorbell) {
1549		*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1550		WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1551	} else {
1552		WREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_WPTR, lower_32_bits(ring->wptr));
1553	}
1554}
1555
1556static const struct amdgpu_ring_funcs vcn_v2_5_dec_ring_vm_funcs = {
1557	.type = AMDGPU_RING_TYPE_VCN_DEC,
1558	.align_mask = 0xf,
1559	.secure_submission_supported = true,
1560	.vmhub = AMDGPU_MMHUB_1,
1561	.get_rptr = vcn_v2_5_dec_ring_get_rptr,
1562	.get_wptr = vcn_v2_5_dec_ring_get_wptr,
1563	.set_wptr = vcn_v2_5_dec_ring_set_wptr,
1564	.emit_frame_size =
1565		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
1566		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
1567		8 + /* vcn_v2_0_dec_ring_emit_vm_flush */
1568		14 + 14 + /* vcn_v2_0_dec_ring_emit_fence x2 vm fence */
1569		6,
1570	.emit_ib_size = 8, /* vcn_v2_0_dec_ring_emit_ib */
1571	.emit_ib = vcn_v2_0_dec_ring_emit_ib,
1572	.emit_fence = vcn_v2_0_dec_ring_emit_fence,
1573	.emit_vm_flush = vcn_v2_0_dec_ring_emit_vm_flush,
1574	.test_ring = vcn_v2_0_dec_ring_test_ring,
1575	.test_ib = amdgpu_vcn_dec_ring_test_ib,
1576	.insert_nop = vcn_v2_0_dec_ring_insert_nop,
1577	.insert_start = vcn_v2_0_dec_ring_insert_start,
1578	.insert_end = vcn_v2_0_dec_ring_insert_end,
1579	.pad_ib = amdgpu_ring_generic_pad_ib,
1580	.begin_use = amdgpu_vcn_ring_begin_use,
1581	.end_use = amdgpu_vcn_ring_end_use,
1582	.emit_wreg = vcn_v2_0_dec_ring_emit_wreg,
1583	.emit_reg_wait = vcn_v2_0_dec_ring_emit_reg_wait,
1584	.emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1585};
1586
1587static const struct amdgpu_ring_funcs vcn_v2_6_dec_ring_vm_funcs = {
1588	.type = AMDGPU_RING_TYPE_VCN_DEC,
1589	.align_mask = 0xf,
1590	.secure_submission_supported = true,
1591	.vmhub = AMDGPU_MMHUB_0,
1592	.get_rptr = vcn_v2_5_dec_ring_get_rptr,
1593	.get_wptr = vcn_v2_5_dec_ring_get_wptr,
1594	.set_wptr = vcn_v2_5_dec_ring_set_wptr,
1595	.emit_frame_size =
1596		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
1597		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
1598		8 + /* vcn_v2_0_dec_ring_emit_vm_flush */
1599		14 + 14 + /* vcn_v2_0_dec_ring_emit_fence x2 vm fence */
1600		6,
1601	.emit_ib_size = 8, /* vcn_v2_0_dec_ring_emit_ib */
1602	.emit_ib = vcn_v2_0_dec_ring_emit_ib,
1603	.emit_fence = vcn_v2_0_dec_ring_emit_fence,
1604	.emit_vm_flush = vcn_v2_0_dec_ring_emit_vm_flush,
1605	.test_ring = vcn_v2_0_dec_ring_test_ring,
1606	.test_ib = amdgpu_vcn_dec_ring_test_ib,
1607	.insert_nop = vcn_v2_0_dec_ring_insert_nop,
1608	.insert_start = vcn_v2_0_dec_ring_insert_start,
1609	.insert_end = vcn_v2_0_dec_ring_insert_end,
1610	.pad_ib = amdgpu_ring_generic_pad_ib,
1611	.begin_use = amdgpu_vcn_ring_begin_use,
1612	.end_use = amdgpu_vcn_ring_end_use,
1613	.emit_wreg = vcn_v2_0_dec_ring_emit_wreg,
1614	.emit_reg_wait = vcn_v2_0_dec_ring_emit_reg_wait,
1615	.emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1616};
1617
1618/**
1619 * vcn_v2_5_enc_ring_get_rptr - get enc read pointer
1620 *
1621 * @ring: amdgpu_ring pointer
1622 *
1623 * Returns the current hardware enc read pointer
1624 */
1625static uint64_t vcn_v2_5_enc_ring_get_rptr(struct amdgpu_ring *ring)
1626{
1627	struct amdgpu_device *adev = ring->adev;
1628
1629	if (ring == &adev->vcn.inst[ring->me].ring_enc[0])
1630		return RREG32_SOC15(VCN, ring->me, mmUVD_RB_RPTR);
1631	else
1632		return RREG32_SOC15(VCN, ring->me, mmUVD_RB_RPTR2);
1633}
1634
1635/**
1636 * vcn_v2_5_enc_ring_get_wptr - get enc write pointer
1637 *
1638 * @ring: amdgpu_ring pointer
1639 *
1640 * Returns the current hardware enc write pointer
1641 */
1642static uint64_t vcn_v2_5_enc_ring_get_wptr(struct amdgpu_ring *ring)
1643{
1644	struct amdgpu_device *adev = ring->adev;
1645
1646	if (ring == &adev->vcn.inst[ring->me].ring_enc[0]) {
1647		if (ring->use_doorbell)
1648			return *ring->wptr_cpu_addr;
1649		else
1650			return RREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR);
1651	} else {
1652		if (ring->use_doorbell)
1653			return *ring->wptr_cpu_addr;
1654		else
1655			return RREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR2);
1656	}
1657}
1658
1659/**
1660 * vcn_v2_5_enc_ring_set_wptr - set enc write pointer
1661 *
1662 * @ring: amdgpu_ring pointer
1663 *
1664 * Commits the enc write pointer to the hardware
1665 */
1666static void vcn_v2_5_enc_ring_set_wptr(struct amdgpu_ring *ring)
1667{
1668	struct amdgpu_device *adev = ring->adev;
1669
1670	if (ring == &adev->vcn.inst[ring->me].ring_enc[0]) {
1671		if (ring->use_doorbell) {
1672			*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1673			WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1674		} else {
1675			WREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1676		}
1677	} else {
1678		if (ring->use_doorbell) {
1679			*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1680			WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1681		} else {
1682			WREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1683		}
1684	}
1685}
1686
1687static const struct amdgpu_ring_funcs vcn_v2_5_enc_ring_vm_funcs = {
1688	.type = AMDGPU_RING_TYPE_VCN_ENC,
1689	.align_mask = 0x3f,
1690	.nop = VCN_ENC_CMD_NO_OP,
1691	.vmhub = AMDGPU_MMHUB_1,
1692	.get_rptr = vcn_v2_5_enc_ring_get_rptr,
1693	.get_wptr = vcn_v2_5_enc_ring_get_wptr,
1694	.set_wptr = vcn_v2_5_enc_ring_set_wptr,
1695	.emit_frame_size =
1696		SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
1697		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 +
1698		4 + /* vcn_v2_0_enc_ring_emit_vm_flush */
1699		5 + 5 + /* vcn_v2_0_enc_ring_emit_fence x2 vm fence */
1700		1, /* vcn_v2_0_enc_ring_insert_end */
1701	.emit_ib_size = 5, /* vcn_v2_0_enc_ring_emit_ib */
1702	.emit_ib = vcn_v2_0_enc_ring_emit_ib,
1703	.emit_fence = vcn_v2_0_enc_ring_emit_fence,
1704	.emit_vm_flush = vcn_v2_0_enc_ring_emit_vm_flush,
1705	.test_ring = amdgpu_vcn_enc_ring_test_ring,
1706	.test_ib = amdgpu_vcn_enc_ring_test_ib,
1707	.insert_nop = amdgpu_ring_insert_nop,
1708	.insert_end = vcn_v2_0_enc_ring_insert_end,
1709	.pad_ib = amdgpu_ring_generic_pad_ib,
1710	.begin_use = amdgpu_vcn_ring_begin_use,
1711	.end_use = amdgpu_vcn_ring_end_use,
1712	.emit_wreg = vcn_v2_0_enc_ring_emit_wreg,
1713	.emit_reg_wait = vcn_v2_0_enc_ring_emit_reg_wait,
1714	.emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1715};
1716
1717static const struct amdgpu_ring_funcs vcn_v2_6_enc_ring_vm_funcs = {
1718        .type = AMDGPU_RING_TYPE_VCN_ENC,
1719        .align_mask = 0x3f,
1720        .nop = VCN_ENC_CMD_NO_OP,
1721        .vmhub = AMDGPU_MMHUB_0,
1722        .get_rptr = vcn_v2_5_enc_ring_get_rptr,
1723        .get_wptr = vcn_v2_5_enc_ring_get_wptr,
1724        .set_wptr = vcn_v2_5_enc_ring_set_wptr,
1725        .emit_frame_size =
1726                SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
1727                SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 +
1728                4 + /* vcn_v2_0_enc_ring_emit_vm_flush */
1729                5 + 5 + /* vcn_v2_0_enc_ring_emit_fence x2 vm fence */
1730                1, /* vcn_v2_0_enc_ring_insert_end */
1731        .emit_ib_size = 5, /* vcn_v2_0_enc_ring_emit_ib */
1732        .emit_ib = vcn_v2_0_enc_ring_emit_ib,
1733        .emit_fence = vcn_v2_0_enc_ring_emit_fence,
1734        .emit_vm_flush = vcn_v2_0_enc_ring_emit_vm_flush,
1735        .test_ring = amdgpu_vcn_enc_ring_test_ring,
1736        .test_ib = amdgpu_vcn_enc_ring_test_ib,
1737        .insert_nop = amdgpu_ring_insert_nop,
1738        .insert_end = vcn_v2_0_enc_ring_insert_end,
1739        .pad_ib = amdgpu_ring_generic_pad_ib,
1740        .begin_use = amdgpu_vcn_ring_begin_use,
1741        .end_use = amdgpu_vcn_ring_end_use,
1742        .emit_wreg = vcn_v2_0_enc_ring_emit_wreg,
1743        .emit_reg_wait = vcn_v2_0_enc_ring_emit_reg_wait,
1744        .emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1745};
1746
1747static void vcn_v2_5_set_dec_ring_funcs(struct amdgpu_device *adev)
1748{
1749	int i;
1750
1751	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1752		if (adev->vcn.harvest_config & (1 << i))
1753			continue;
1754		if (adev->ip_versions[UVD_HWIP][0] == IP_VERSION(2, 5, 0))
1755			adev->vcn.inst[i].ring_dec.funcs = &vcn_v2_5_dec_ring_vm_funcs;
1756		else /* CHIP_ALDEBARAN */
1757			adev->vcn.inst[i].ring_dec.funcs = &vcn_v2_6_dec_ring_vm_funcs;
1758		adev->vcn.inst[i].ring_dec.me = i;
1759		DRM_INFO("VCN(%d) decode is enabled in VM mode\n", i);
1760	}
1761}
1762
1763static void vcn_v2_5_set_enc_ring_funcs(struct amdgpu_device *adev)
1764{
1765	int i, j;
1766
1767	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
1768		if (adev->vcn.harvest_config & (1 << j))
1769			continue;
1770		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
1771			if (adev->ip_versions[UVD_HWIP][0] == IP_VERSION(2, 5, 0))
1772				adev->vcn.inst[j].ring_enc[i].funcs = &vcn_v2_5_enc_ring_vm_funcs;
1773			else /* CHIP_ALDEBARAN */
1774				adev->vcn.inst[j].ring_enc[i].funcs = &vcn_v2_6_enc_ring_vm_funcs;
1775			adev->vcn.inst[j].ring_enc[i].me = j;
1776		}
1777		DRM_INFO("VCN(%d) encode is enabled in VM mode\n", j);
1778	}
1779}
1780
1781static bool vcn_v2_5_is_idle(void *handle)
1782{
1783	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1784	int i, ret = 1;
1785
1786	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1787		if (adev->vcn.harvest_config & (1 << i))
1788			continue;
1789		ret &= (RREG32_SOC15(VCN, i, mmUVD_STATUS) == UVD_STATUS__IDLE);
1790	}
1791
1792	return ret;
1793}
1794
1795static int vcn_v2_5_wait_for_idle(void *handle)
1796{
1797	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1798	int i, ret = 0;
1799
1800	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1801		if (adev->vcn.harvest_config & (1 << i))
1802			continue;
1803		ret = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_STATUS, UVD_STATUS__IDLE,
1804			UVD_STATUS__IDLE);
1805		if (ret)
1806			return ret;
1807	}
1808
1809	return ret;
1810}
1811
1812static int vcn_v2_5_set_clockgating_state(void *handle,
1813					  enum amd_clockgating_state state)
1814{
1815	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1816	bool enable = (state == AMD_CG_STATE_GATE);
1817
1818	if (amdgpu_sriov_vf(adev))
1819		return 0;
1820
1821	if (enable) {
1822		if (!vcn_v2_5_is_idle(handle))
1823			return -EBUSY;
1824		vcn_v2_5_enable_clock_gating(adev);
1825	} else {
1826		vcn_v2_5_disable_clock_gating(adev);
1827	}
1828
1829	return 0;
1830}
1831
1832static int vcn_v2_5_set_powergating_state(void *handle,
1833					  enum amd_powergating_state state)
1834{
1835	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1836	int ret;
1837
1838	if (amdgpu_sriov_vf(adev))
1839		return 0;
1840
1841	if(state == adev->vcn.cur_state)
1842		return 0;
1843
1844	if (state == AMD_PG_STATE_GATE)
1845		ret = vcn_v2_5_stop(adev);
1846	else
1847		ret = vcn_v2_5_start(adev);
1848
1849	if(!ret)
1850		adev->vcn.cur_state = state;
1851
1852	return ret;
1853}
1854
1855static int vcn_v2_5_set_interrupt_state(struct amdgpu_device *adev,
1856					struct amdgpu_irq_src *source,
1857					unsigned type,
1858					enum amdgpu_interrupt_state state)
1859{
1860	return 0;
1861}
1862
 
 
 
 
 
 
 
 
1863static int vcn_v2_5_process_interrupt(struct amdgpu_device *adev,
1864				      struct amdgpu_irq_src *source,
1865				      struct amdgpu_iv_entry *entry)
1866{
1867	uint32_t ip_instance;
1868
1869	switch (entry->client_id) {
1870	case SOC15_IH_CLIENTID_VCN:
1871		ip_instance = 0;
1872		break;
1873	case SOC15_IH_CLIENTID_VCN1:
1874		ip_instance = 1;
1875		break;
1876	default:
1877		DRM_ERROR("Unhandled client id: %d\n", entry->client_id);
1878		return 0;
1879	}
1880
1881	DRM_DEBUG("IH: VCN TRAP\n");
1882
1883	switch (entry->src_id) {
1884	case VCN_2_0__SRCID__UVD_SYSTEM_MESSAGE_INTERRUPT:
1885		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_dec);
1886		break;
1887	case VCN_2_0__SRCID__UVD_ENC_GENERAL_PURPOSE:
1888		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_enc[0]);
1889		break;
1890	case VCN_2_0__SRCID__UVD_ENC_LOW_LATENCY:
1891		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_enc[1]);
1892		break;
1893	case VCN_2_6__SRCID_UVD_POISON:
1894		amdgpu_vcn_process_poison_irq(adev, source, entry);
1895		break;
1896	default:
1897		DRM_ERROR("Unhandled interrupt: %d %d\n",
1898			  entry->src_id, entry->src_data[0]);
1899		break;
1900	}
1901
1902	return 0;
1903}
1904
1905static const struct amdgpu_irq_src_funcs vcn_v2_5_irq_funcs = {
1906	.set = vcn_v2_5_set_interrupt_state,
1907	.process = vcn_v2_5_process_interrupt,
1908};
1909
 
 
 
 
 
1910static void vcn_v2_5_set_irq_funcs(struct amdgpu_device *adev)
1911{
1912	int i;
1913
1914	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1915		if (adev->vcn.harvest_config & (1 << i))
1916			continue;
1917		adev->vcn.inst[i].irq.num_types = adev->vcn.num_enc_rings + 1;
1918		adev->vcn.inst[i].irq.funcs = &vcn_v2_5_irq_funcs;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1919	}
1920}
1921
1922static const struct amd_ip_funcs vcn_v2_5_ip_funcs = {
1923	.name = "vcn_v2_5",
1924	.early_init = vcn_v2_5_early_init,
1925	.late_init = NULL,
1926	.sw_init = vcn_v2_5_sw_init,
1927	.sw_fini = vcn_v2_5_sw_fini,
1928	.hw_init = vcn_v2_5_hw_init,
1929	.hw_fini = vcn_v2_5_hw_fini,
1930	.suspend = vcn_v2_5_suspend,
1931	.resume = vcn_v2_5_resume,
1932	.is_idle = vcn_v2_5_is_idle,
1933	.wait_for_idle = vcn_v2_5_wait_for_idle,
1934	.check_soft_reset = NULL,
1935	.pre_soft_reset = NULL,
1936	.soft_reset = NULL,
1937	.post_soft_reset = NULL,
1938	.set_clockgating_state = vcn_v2_5_set_clockgating_state,
1939	.set_powergating_state = vcn_v2_5_set_powergating_state,
 
 
1940};
1941
1942static const struct amd_ip_funcs vcn_v2_6_ip_funcs = {
1943        .name = "vcn_v2_6",
1944        .early_init = vcn_v2_5_early_init,
1945        .late_init = NULL,
1946        .sw_init = vcn_v2_5_sw_init,
1947        .sw_fini = vcn_v2_5_sw_fini,
1948        .hw_init = vcn_v2_5_hw_init,
1949        .hw_fini = vcn_v2_5_hw_fini,
1950        .suspend = vcn_v2_5_suspend,
1951        .resume = vcn_v2_5_resume,
1952        .is_idle = vcn_v2_5_is_idle,
1953        .wait_for_idle = vcn_v2_5_wait_for_idle,
1954        .check_soft_reset = NULL,
1955        .pre_soft_reset = NULL,
1956        .soft_reset = NULL,
1957        .post_soft_reset = NULL,
1958        .set_clockgating_state = vcn_v2_5_set_clockgating_state,
1959        .set_powergating_state = vcn_v2_5_set_powergating_state,
 
 
1960};
1961
1962const struct amdgpu_ip_block_version vcn_v2_5_ip_block =
1963{
1964		.type = AMD_IP_BLOCK_TYPE_VCN,
1965		.major = 2,
1966		.minor = 5,
1967		.rev = 0,
1968		.funcs = &vcn_v2_5_ip_funcs,
1969};
1970
1971const struct amdgpu_ip_block_version vcn_v2_6_ip_block =
1972{
1973		.type = AMD_IP_BLOCK_TYPE_VCN,
1974		.major = 2,
1975		.minor = 6,
1976		.rev = 0,
1977		.funcs = &vcn_v2_6_ip_funcs,
1978};
1979
1980static uint32_t vcn_v2_6_query_poison_by_instance(struct amdgpu_device *adev,
1981			uint32_t instance, uint32_t sub_block)
1982{
1983	uint32_t poison_stat = 0, reg_value = 0;
1984
1985	switch (sub_block) {
1986	case AMDGPU_VCN_V2_6_VCPU_VCODEC:
1987		reg_value = RREG32_SOC15(VCN, instance, mmUVD_RAS_VCPU_VCODEC_STATUS);
1988		poison_stat = REG_GET_FIELD(reg_value, UVD_RAS_VCPU_VCODEC_STATUS, POISONED_PF);
1989		break;
1990	default:
1991		break;
1992	}
1993
1994	if (poison_stat)
1995		dev_info(adev->dev, "Poison detected in VCN%d, sub_block%d\n",
1996			instance, sub_block);
1997
1998	return poison_stat;
1999}
2000
2001static bool vcn_v2_6_query_poison_status(struct amdgpu_device *adev)
2002{
2003	uint32_t inst, sub;
2004	uint32_t poison_stat = 0;
2005
2006	for (inst = 0; inst < adev->vcn.num_vcn_inst; inst++)
2007		for (sub = 0; sub < AMDGPU_VCN_V2_6_MAX_SUB_BLOCK; sub++)
2008			poison_stat +=
2009			vcn_v2_6_query_poison_by_instance(adev, inst, sub);
2010
2011	return !!poison_stat;
2012}
2013
2014const struct amdgpu_ras_block_hw_ops vcn_v2_6_ras_hw_ops = {
2015	.query_poison_status = vcn_v2_6_query_poison_status,
2016};
2017
2018static struct amdgpu_vcn_ras vcn_v2_6_ras = {
2019	.ras_block = {
2020		.hw_ops = &vcn_v2_6_ras_hw_ops,
 
2021	},
2022};
2023
2024static void vcn_v2_5_set_ras_funcs(struct amdgpu_device *adev)
2025{
2026	switch (adev->ip_versions[VCN_HWIP][0]) {
2027	case IP_VERSION(2, 6, 0):
2028		adev->vcn.ras = &vcn_v2_6_ras;
2029		break;
2030	default:
2031		break;
2032	}
2033
2034	amdgpu_vcn_set_ras_funcs(adev);
2035}
v6.13.7
   1/*
   2 * Copyright 2019 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 <drm/drm_drv.h>
  26
  27#include "amdgpu.h"
  28#include "amdgpu_vcn.h"
  29#include "amdgpu_pm.h"
  30#include "soc15.h"
  31#include "soc15d.h"
  32#include "vcn_v2_0.h"
  33#include "mmsch_v1_0.h"
  34#include "vcn_v2_5.h"
  35
  36#include "vcn/vcn_2_5_offset.h"
  37#include "vcn/vcn_2_5_sh_mask.h"
  38#include "ivsrcid/vcn/irqsrcs_vcn_2_0.h"
  39
  40#define VCN_VID_SOC_ADDRESS_2_0					0x1fa00
  41#define VCN1_VID_SOC_ADDRESS_3_0				0x48200
  42
  43#define mmUVD_CONTEXT_ID_INTERNAL_OFFSET			0x27
  44#define mmUVD_GPCOM_VCPU_CMD_INTERNAL_OFFSET			0x0f
  45#define mmUVD_GPCOM_VCPU_DATA0_INTERNAL_OFFSET			0x10
  46#define mmUVD_GPCOM_VCPU_DATA1_INTERNAL_OFFSET			0x11
  47#define mmUVD_NO_OP_INTERNAL_OFFSET				0x29
  48#define mmUVD_GP_SCRATCH8_INTERNAL_OFFSET			0x66
  49#define mmUVD_SCRATCH9_INTERNAL_OFFSET				0xc01d
  50
  51#define mmUVD_LMI_RBC_IB_VMID_INTERNAL_OFFSET			0x431
  52#define mmUVD_LMI_RBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET		0x3b4
  53#define mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET		0x3b5
  54#define mmUVD_RBC_IB_SIZE_INTERNAL_OFFSET			0x25c
  55
  56#define VCN25_MAX_HW_INSTANCES_ARCTURUS			2
  57
  58static const struct amdgpu_hwip_reg_entry vcn_reg_list_2_5[] = {
  59	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_POWER_STATUS),
  60	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_POWER_STATUS),
  61	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_STATUS),
  62	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_CONTEXT_ID),
  63	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_CONTEXT_ID2),
  64	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_GPCOM_VCPU_DATA0),
  65	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_GPCOM_VCPU_DATA1),
  66	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_GPCOM_VCPU_CMD),
  67	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_HI),
  68	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_LO),
  69	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_HI2),
  70	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_LO2),
  71	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_HI3),
  72	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_LO3),
  73	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_HI4),
  74	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_BASE_LO4),
  75	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_RPTR),
  76	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_WPTR),
  77	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_RPTR2),
  78	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_WPTR2),
  79	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_RPTR3),
  80	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_WPTR3),
  81	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_RPTR4),
  82	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_WPTR4),
  83	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_SIZE),
  84	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_SIZE2),
  85	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_SIZE3),
  86	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_RB_SIZE4),
  87	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_PGFSM_CONFIG),
  88	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_PGFSM_STATUS),
  89	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_DPG_LMA_CTL),
  90	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_DPG_LMA_DATA),
  91	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_DPG_LMA_MASK),
  92	SOC15_REG_ENTRY_STR(VCN, 0, mmUVD_DPG_PAUSE)
  93};
  94
  95static void vcn_v2_5_set_dec_ring_funcs(struct amdgpu_device *adev);
  96static void vcn_v2_5_set_enc_ring_funcs(struct amdgpu_device *adev);
  97static void vcn_v2_5_set_irq_funcs(struct amdgpu_device *adev);
  98static int vcn_v2_5_set_powergating_state(void *handle,
  99				enum amd_powergating_state state);
 100static int vcn_v2_5_pause_dpg_mode(struct amdgpu_device *adev,
 101				int inst_idx, struct dpg_pause_state *new_state);
 102static int vcn_v2_5_sriov_start(struct amdgpu_device *adev);
 103static void vcn_v2_5_set_ras_funcs(struct amdgpu_device *adev);
 104
 105static int amdgpu_ih_clientid_vcns[] = {
 106	SOC15_IH_CLIENTID_VCN,
 107	SOC15_IH_CLIENTID_VCN1
 108};
 109
 110/**
 111 * vcn_v2_5_early_init - set function pointers and load microcode
 112 *
 113 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 114 *
 115 * Set ring and irq function pointers
 116 * Load microcode from filesystem
 117 */
 118static int vcn_v2_5_early_init(struct amdgpu_ip_block *ip_block)
 119{
 120	struct amdgpu_device *adev = ip_block->adev;
 121
 122	if (amdgpu_sriov_vf(adev)) {
 123		adev->vcn.num_vcn_inst = 2;
 124		adev->vcn.harvest_config = 0;
 125		adev->vcn.num_enc_rings = 1;
 126	} else {
 127		u32 harvest;
 128		int i;
 129
 130		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
 131			harvest = RREG32_SOC15(VCN, i, mmCC_UVD_HARVESTING);
 132			if (harvest & CC_UVD_HARVESTING__UVD_DISABLE_MASK)
 133				adev->vcn.harvest_config |= 1 << i;
 134		}
 135		if (adev->vcn.harvest_config == (AMDGPU_VCN_HARVEST_VCN0 |
 136					AMDGPU_VCN_HARVEST_VCN1))
 137			/* both instances are harvested, disable the block */
 138			return -ENOENT;
 139
 140		adev->vcn.num_enc_rings = 2;
 141	}
 142
 143	vcn_v2_5_set_dec_ring_funcs(adev);
 144	vcn_v2_5_set_enc_ring_funcs(adev);
 145	vcn_v2_5_set_irq_funcs(adev);
 146	vcn_v2_5_set_ras_funcs(adev);
 147
 148	return amdgpu_vcn_early_init(adev);
 149}
 150
 151/**
 152 * vcn_v2_5_sw_init - sw init for VCN block
 153 *
 154 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 155 *
 156 * Load firmware and sw initialization
 157 */
 158static int vcn_v2_5_sw_init(struct amdgpu_ip_block *ip_block)
 159{
 160	struct amdgpu_ring *ring;
 161	int i, j, r;
 162	uint32_t reg_count = ARRAY_SIZE(vcn_reg_list_2_5);
 163	uint32_t *ptr;
 164	struct amdgpu_device *adev = ip_block->adev;
 165
 166	for (j = 0; j < adev->vcn.num_vcn_inst; j++) {
 167		if (adev->vcn.harvest_config & (1 << j))
 168			continue;
 169		/* VCN DEC TRAP */
 170		r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 171				VCN_2_0__SRCID__UVD_SYSTEM_MESSAGE_INTERRUPT, &adev->vcn.inst[j].irq);
 172		if (r)
 173			return r;
 174
 175		/* VCN ENC TRAP */
 176		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 177			r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 178				i + VCN_2_0__SRCID__UVD_ENC_GENERAL_PURPOSE, &adev->vcn.inst[j].irq);
 179			if (r)
 180				return r;
 181		}
 182
 183		/* VCN POISON TRAP */
 184		r = amdgpu_irq_add_id(adev, amdgpu_ih_clientid_vcns[j],
 185			VCN_2_6__SRCID_UVD_POISON, &adev->vcn.inst[j].ras_poison_irq);
 186		if (r)
 187			return r;
 188	}
 189
 190	r = amdgpu_vcn_sw_init(adev);
 191	if (r)
 192		return r;
 193
 194	amdgpu_vcn_setup_ucode(adev);
 195
 196	r = amdgpu_vcn_resume(adev);
 197	if (r)
 198		return r;
 199
 200	for (j = 0; j < adev->vcn.num_vcn_inst; j++) {
 201		volatile struct amdgpu_fw_shared *fw_shared;
 202
 203		if (adev->vcn.harvest_config & (1 << j))
 204			continue;
 205		adev->vcn.internal.context_id = mmUVD_CONTEXT_ID_INTERNAL_OFFSET;
 206		adev->vcn.internal.ib_vmid = mmUVD_LMI_RBC_IB_VMID_INTERNAL_OFFSET;
 207		adev->vcn.internal.ib_bar_low = mmUVD_LMI_RBC_IB_64BIT_BAR_LOW_INTERNAL_OFFSET;
 208		adev->vcn.internal.ib_bar_high = mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH_INTERNAL_OFFSET;
 209		adev->vcn.internal.ib_size = mmUVD_RBC_IB_SIZE_INTERNAL_OFFSET;
 210		adev->vcn.internal.gp_scratch8 = mmUVD_GP_SCRATCH8_INTERNAL_OFFSET;
 211
 212		adev->vcn.internal.scratch9 = mmUVD_SCRATCH9_INTERNAL_OFFSET;
 213		adev->vcn.inst[j].external.scratch9 = SOC15_REG_OFFSET(VCN, j, mmUVD_SCRATCH9);
 214		adev->vcn.internal.data0 = mmUVD_GPCOM_VCPU_DATA0_INTERNAL_OFFSET;
 215		adev->vcn.inst[j].external.data0 = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_DATA0);
 216		adev->vcn.internal.data1 = mmUVD_GPCOM_VCPU_DATA1_INTERNAL_OFFSET;
 217		adev->vcn.inst[j].external.data1 = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_DATA1);
 218		adev->vcn.internal.cmd = mmUVD_GPCOM_VCPU_CMD_INTERNAL_OFFSET;
 219		adev->vcn.inst[j].external.cmd = SOC15_REG_OFFSET(VCN, j, mmUVD_GPCOM_VCPU_CMD);
 220		adev->vcn.internal.nop = mmUVD_NO_OP_INTERNAL_OFFSET;
 221		adev->vcn.inst[j].external.nop = SOC15_REG_OFFSET(VCN, j, mmUVD_NO_OP);
 222
 223		ring = &adev->vcn.inst[j].ring_dec;
 224		ring->use_doorbell = true;
 225
 226		ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) +
 227				(amdgpu_sriov_vf(adev) ? 2*j : 8*j);
 228
 229		if (amdgpu_ip_version(adev, UVD_HWIP, 0) == IP_VERSION(2, 5, 0))
 230			ring->vm_hub = AMDGPU_MMHUB1(0);
 231		else
 232			ring->vm_hub = AMDGPU_MMHUB0(0);
 233
 234		sprintf(ring->name, "vcn_dec_%d", j);
 235		r = amdgpu_ring_init(adev, ring, 512, &adev->vcn.inst[j].irq,
 236				     0, AMDGPU_RING_PRIO_DEFAULT, NULL);
 237		if (r)
 238			return r;
 239
 240		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 241			enum amdgpu_ring_priority_level hw_prio = amdgpu_vcn_get_enc_ring_prio(i);
 242
 243			ring = &adev->vcn.inst[j].ring_enc[i];
 244			ring->use_doorbell = true;
 245
 246			ring->doorbell_index = (adev->doorbell_index.vcn.vcn_ring0_1 << 1) +
 247					(amdgpu_sriov_vf(adev) ? (1 + i + 2*j) : (2 + i + 8*j));
 248
 249			if (amdgpu_ip_version(adev, UVD_HWIP, 0) ==
 250			    IP_VERSION(2, 5, 0))
 251				ring->vm_hub = AMDGPU_MMHUB1(0);
 252			else
 253				ring->vm_hub = AMDGPU_MMHUB0(0);
 254
 255			sprintf(ring->name, "vcn_enc_%d.%d", j, i);
 256			r = amdgpu_ring_init(adev, ring, 512,
 257					     &adev->vcn.inst[j].irq, 0,
 258					     hw_prio, NULL);
 259			if (r)
 260				return r;
 261		}
 262
 263		fw_shared = adev->vcn.inst[j].fw_shared.cpu_addr;
 264		fw_shared->present_flag_0 = cpu_to_le32(AMDGPU_VCN_MULTI_QUEUE_FLAG);
 265
 266		if (amdgpu_vcnfw_log)
 267			amdgpu_vcn_fwlog_init(&adev->vcn.inst[i]);
 268	}
 269
 270	if (amdgpu_sriov_vf(adev)) {
 271		r = amdgpu_virt_alloc_mm_table(adev);
 272		if (r)
 273			return r;
 274	}
 275
 276	if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)
 277		adev->vcn.pause_dpg_mode = vcn_v2_5_pause_dpg_mode;
 278
 279	r = amdgpu_vcn_ras_sw_init(adev);
 280	if (r)
 281		return r;
 282
 283	/* Allocate memory for VCN IP Dump buffer */
 284	ptr = kcalloc(adev->vcn.num_vcn_inst * reg_count, sizeof(uint32_t), GFP_KERNEL);
 285	if (!ptr) {
 286		DRM_ERROR("Failed to allocate memory for VCN IP Dump\n");
 287		adev->vcn.ip_dump = NULL;
 288	} else {
 289		adev->vcn.ip_dump = ptr;
 290	}
 291
 292	return 0;
 293}
 294
 295/**
 296 * vcn_v2_5_sw_fini - sw fini for VCN block
 297 *
 298 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 299 *
 300 * VCN suspend and free up sw allocation
 301 */
 302static int vcn_v2_5_sw_fini(struct amdgpu_ip_block *ip_block)
 303{
 304	int i, r, idx;
 305	struct amdgpu_device *adev = ip_block->adev;
 306	volatile struct amdgpu_fw_shared *fw_shared;
 307
 308	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
 309		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
 310			if (adev->vcn.harvest_config & (1 << i))
 311				continue;
 312			fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr;
 313			fw_shared->present_flag_0 = 0;
 314		}
 315		drm_dev_exit(idx);
 316	}
 317
 318
 319	if (amdgpu_sriov_vf(adev))
 320		amdgpu_virt_free_mm_table(adev);
 321
 322	r = amdgpu_vcn_suspend(adev);
 323	if (r)
 324		return r;
 325
 326	r = amdgpu_vcn_sw_fini(adev);
 327
 328	kfree(adev->vcn.ip_dump);
 329
 330	return r;
 331}
 332
 333/**
 334 * vcn_v2_5_hw_init - start and test VCN block
 335 *
 336 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 337 *
 338 * Initialize the hardware, boot up the VCPU and do some testing
 339 */
 340static int vcn_v2_5_hw_init(struct amdgpu_ip_block *ip_block)
 341{
 342	struct amdgpu_device *adev = ip_block->adev;
 343	struct amdgpu_ring *ring;
 344	int i, j, r = 0;
 345
 346	if (amdgpu_sriov_vf(adev))
 347		r = vcn_v2_5_sriov_start(adev);
 348
 349	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
 350		if (adev->vcn.harvest_config & (1 << j))
 351			continue;
 352
 353		if (amdgpu_sriov_vf(adev)) {
 354			adev->vcn.inst[j].ring_enc[0].sched.ready = true;
 355			adev->vcn.inst[j].ring_enc[1].sched.ready = false;
 356			adev->vcn.inst[j].ring_enc[2].sched.ready = false;
 357			adev->vcn.inst[j].ring_dec.sched.ready = true;
 358		} else {
 359
 360			ring = &adev->vcn.inst[j].ring_dec;
 361
 362			adev->nbio.funcs->vcn_doorbell_range(adev, ring->use_doorbell,
 363						     ring->doorbell_index, j);
 364
 365			r = amdgpu_ring_test_helper(ring);
 366			if (r)
 367				return r;
 368
 369			for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
 370				ring = &adev->vcn.inst[j].ring_enc[i];
 371				r = amdgpu_ring_test_helper(ring);
 372				if (r)
 373					return r;
 374			}
 375		}
 376	}
 377
 
 
 
 
 
 378	return r;
 379}
 380
 381/**
 382 * vcn_v2_5_hw_fini - stop the hardware block
 383 *
 384 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 385 *
 386 * Stop the VCN block, mark ring as not ready any more
 387 */
 388static int vcn_v2_5_hw_fini(struct amdgpu_ip_block *ip_block)
 389{
 390	struct amdgpu_device *adev = ip_block->adev;
 391	int i;
 392
 393	cancel_delayed_work_sync(&adev->vcn.idle_work);
 394
 395	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 396		if (adev->vcn.harvest_config & (1 << i))
 397			continue;
 398
 399		if ((adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) ||
 400		    (adev->vcn.cur_state != AMD_PG_STATE_GATE &&
 401		     RREG32_SOC15(VCN, i, mmUVD_STATUS)))
 402			vcn_v2_5_set_powergating_state(adev, AMD_PG_STATE_GATE);
 403
 404		if (amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__VCN))
 405			amdgpu_irq_put(adev, &adev->vcn.inst[i].ras_poison_irq, 0);
 406	}
 407
 408	return 0;
 409}
 410
 411/**
 412 * vcn_v2_5_suspend - suspend VCN block
 413 *
 414 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 415 *
 416 * HW fini and suspend VCN block
 417 */
 418static int vcn_v2_5_suspend(struct amdgpu_ip_block *ip_block)
 419{
 420	int r;
 
 421
 422	r = vcn_v2_5_hw_fini(ip_block);
 423	if (r)
 424		return r;
 425
 426	r = amdgpu_vcn_suspend(ip_block->adev);
 427
 428	return r;
 429}
 430
 431/**
 432 * vcn_v2_5_resume - resume VCN block
 433 *
 434 * @ip_block: Pointer to the amdgpu_ip_block for this hw instance.
 435 *
 436 * Resume firmware and hw init VCN block
 437 */
 438static int vcn_v2_5_resume(struct amdgpu_ip_block *ip_block)
 439{
 440	int r;
 
 441
 442	r = amdgpu_vcn_resume(ip_block->adev);
 443	if (r)
 444		return r;
 445
 446	r = vcn_v2_5_hw_init(ip_block);
 447
 448	return r;
 449}
 450
 451/**
 452 * vcn_v2_5_mc_resume - memory controller programming
 453 *
 454 * @adev: amdgpu_device pointer
 455 *
 456 * Let the VCN memory controller know it's offsets
 457 */
 458static void vcn_v2_5_mc_resume(struct amdgpu_device *adev)
 459{
 460	uint32_t size;
 461	uint32_t offset;
 462	int i;
 463
 464	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 465		if (adev->vcn.harvest_config & (1 << i))
 466			continue;
 467
 468		size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw[i]->size + 4);
 469		/* cache window 0: fw */
 470		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
 471			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
 472				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_lo));
 473			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
 474				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_hi));
 475			WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET0, 0);
 476			offset = 0;
 477		} else {
 478			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
 479				lower_32_bits(adev->vcn.inst[i].gpu_addr));
 480			WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
 481				upper_32_bits(adev->vcn.inst[i].gpu_addr));
 482			offset = size;
 483			WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET0,
 484				AMDGPU_UVD_FIRMWARE_OFFSET >> 3);
 485		}
 486		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE0, size);
 487
 488		/* cache window 1: stack */
 489		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW,
 490			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset));
 491		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH,
 492			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset));
 493		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET1, 0);
 494		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE1, AMDGPU_VCN_STACK_SIZE);
 495
 496		/* cache window 2: context */
 497		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW,
 498			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE));
 499		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH,
 500			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE));
 501		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_OFFSET2, 0);
 502		WREG32_SOC15(VCN, i, mmUVD_VCPU_CACHE_SIZE2, AMDGPU_VCN_CONTEXT_SIZE);
 503
 504		/* non-cache window */
 505		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_NC0_64BIT_BAR_LOW,
 506			lower_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr));
 507		WREG32_SOC15(VCN, i, mmUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH,
 508			upper_32_bits(adev->vcn.inst[i].fw_shared.gpu_addr));
 509		WREG32_SOC15(VCN, i, mmUVD_VCPU_NONCACHE_OFFSET0, 0);
 510		WREG32_SOC15(VCN, i, mmUVD_VCPU_NONCACHE_SIZE0,
 511			AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_fw_shared)));
 512	}
 513}
 514
 515static void vcn_v2_5_mc_resume_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect)
 516{
 517	uint32_t size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw[inst_idx]->size + 4);
 518	uint32_t offset;
 519
 520	/* cache window 0: fw */
 521	if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
 522		if (!indirect) {
 523			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 524				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
 525				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_lo), 0, indirect);
 526			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 527				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
 528				(adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + inst_idx].tmr_mc_addr_hi), 0, indirect);
 529			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 530				VCN, 0, mmUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect);
 531		} else {
 532			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 533				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW), 0, 0, indirect);
 534			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 535				VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH), 0, 0, indirect);
 536			WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 537				VCN, 0, mmUVD_VCPU_CACHE_OFFSET0), 0, 0, indirect);
 538		}
 539		offset = 0;
 540	} else {
 541		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 542			VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
 543			lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect);
 544		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 545			VCN, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
 546			upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr), 0, indirect);
 547		offset = size;
 548		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 549			VCN, 0, mmUVD_VCPU_CACHE_OFFSET0),
 550			AMDGPU_UVD_FIRMWARE_OFFSET >> 3, 0, indirect);
 551	}
 552
 553	if (!indirect)
 554		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 555			VCN, 0, mmUVD_VCPU_CACHE_SIZE0), size, 0, indirect);
 556	else
 557		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 558			VCN, 0, mmUVD_VCPU_CACHE_SIZE0), 0, 0, indirect);
 559
 560	/* cache window 1: stack */
 561	if (!indirect) {
 562		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 563			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW),
 564			lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect);
 565		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 566			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH),
 567			upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset), 0, indirect);
 568		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 569			VCN, 0, mmUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect);
 570	} else {
 571		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 572			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW), 0, 0, indirect);
 573		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 574			VCN, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH), 0, 0, indirect);
 575		WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 576			VCN, 0, mmUVD_VCPU_CACHE_OFFSET1), 0, 0, indirect);
 577	}
 578	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 579		VCN, 0, mmUVD_VCPU_CACHE_SIZE1), AMDGPU_VCN_STACK_SIZE, 0, indirect);
 580
 581	/* cache window 2: context */
 582	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 583		VCN, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW),
 584		lower_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect);
 585	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 586		VCN, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH),
 587		upper_32_bits(adev->vcn.inst[inst_idx].gpu_addr + offset + AMDGPU_VCN_STACK_SIZE), 0, indirect);
 588	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 589		VCN, 0, mmUVD_VCPU_CACHE_OFFSET2), 0, 0, indirect);
 590	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 591		VCN, 0, mmUVD_VCPU_CACHE_SIZE2), AMDGPU_VCN_CONTEXT_SIZE, 0, indirect);
 592
 593	/* non-cache window */
 594	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 595		VCN, 0, mmUVD_LMI_VCPU_NC0_64BIT_BAR_LOW),
 596		lower_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect);
 597	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 598		VCN, 0, mmUVD_LMI_VCPU_NC0_64BIT_BAR_HIGH),
 599		upper_32_bits(adev->vcn.inst[inst_idx].fw_shared.gpu_addr), 0, indirect);
 600	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 601		VCN, 0, mmUVD_VCPU_NONCACHE_OFFSET0), 0, 0, indirect);
 602	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 603		VCN, 0, mmUVD_VCPU_NONCACHE_SIZE0),
 604		AMDGPU_GPU_PAGE_ALIGN(sizeof(struct amdgpu_fw_shared)), 0, indirect);
 605
 606	/* VCN global tiling registers */
 607	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 608		VCN, 0, mmUVD_GFX8_ADDR_CONFIG), adev->gfx.config.gb_addr_config, 0, indirect);
 609}
 610
 611/**
 612 * vcn_v2_5_disable_clock_gating - disable VCN clock gating
 613 *
 614 * @adev: amdgpu_device pointer
 615 *
 616 * Disable clock gating for VCN block
 617 */
 618static void vcn_v2_5_disable_clock_gating(struct amdgpu_device *adev)
 619{
 620	uint32_t data;
 621	int i;
 622
 623	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 624		if (adev->vcn.harvest_config & (1 << i))
 625			continue;
 626		/* UVD disable CGC */
 627		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 628		if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 629			data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 630		else
 631			data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
 632		data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 633		data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 634		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 635
 636		data = RREG32_SOC15(VCN, i, mmUVD_CGC_GATE);
 637		data &= ~(UVD_CGC_GATE__SYS_MASK
 638			| UVD_CGC_GATE__UDEC_MASK
 639			| UVD_CGC_GATE__MPEG2_MASK
 640			| UVD_CGC_GATE__REGS_MASK
 641			| UVD_CGC_GATE__RBC_MASK
 642			| UVD_CGC_GATE__LMI_MC_MASK
 643			| UVD_CGC_GATE__LMI_UMC_MASK
 644			| UVD_CGC_GATE__IDCT_MASK
 645			| UVD_CGC_GATE__MPRD_MASK
 646			| UVD_CGC_GATE__MPC_MASK
 647			| UVD_CGC_GATE__LBSI_MASK
 648			| UVD_CGC_GATE__LRBBM_MASK
 649			| UVD_CGC_GATE__UDEC_RE_MASK
 650			| UVD_CGC_GATE__UDEC_CM_MASK
 651			| UVD_CGC_GATE__UDEC_IT_MASK
 652			| UVD_CGC_GATE__UDEC_DB_MASK
 653			| UVD_CGC_GATE__UDEC_MP_MASK
 654			| UVD_CGC_GATE__WCB_MASK
 655			| UVD_CGC_GATE__VCPU_MASK
 656			| UVD_CGC_GATE__MMSCH_MASK);
 657
 658		WREG32_SOC15(VCN, i, mmUVD_CGC_GATE, data);
 659
 660		SOC15_WAIT_ON_RREG(VCN, i, mmUVD_CGC_GATE, 0,  0xFFFFFFFF);
 661
 662		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 663		data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK
 664			| UVD_CGC_CTRL__UDEC_CM_MODE_MASK
 665			| UVD_CGC_CTRL__UDEC_IT_MODE_MASK
 666			| UVD_CGC_CTRL__UDEC_DB_MODE_MASK
 667			| UVD_CGC_CTRL__UDEC_MP_MODE_MASK
 668			| UVD_CGC_CTRL__SYS_MODE_MASK
 669			| UVD_CGC_CTRL__UDEC_MODE_MASK
 670			| UVD_CGC_CTRL__MPEG2_MODE_MASK
 671			| UVD_CGC_CTRL__REGS_MODE_MASK
 672			| UVD_CGC_CTRL__RBC_MODE_MASK
 673			| UVD_CGC_CTRL__LMI_MC_MODE_MASK
 674			| UVD_CGC_CTRL__LMI_UMC_MODE_MASK
 675			| UVD_CGC_CTRL__IDCT_MODE_MASK
 676			| UVD_CGC_CTRL__MPRD_MODE_MASK
 677			| UVD_CGC_CTRL__MPC_MODE_MASK
 678			| UVD_CGC_CTRL__LBSI_MODE_MASK
 679			| UVD_CGC_CTRL__LRBBM_MODE_MASK
 680			| UVD_CGC_CTRL__WCB_MODE_MASK
 681			| UVD_CGC_CTRL__VCPU_MODE_MASK
 682			| UVD_CGC_CTRL__MMSCH_MODE_MASK);
 683		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 684
 685		/* turn on */
 686		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_GATE);
 687		data |= (UVD_SUVD_CGC_GATE__SRE_MASK
 688			| UVD_SUVD_CGC_GATE__SIT_MASK
 689			| UVD_SUVD_CGC_GATE__SMP_MASK
 690			| UVD_SUVD_CGC_GATE__SCM_MASK
 691			| UVD_SUVD_CGC_GATE__SDB_MASK
 692			| UVD_SUVD_CGC_GATE__SRE_H264_MASK
 693			| UVD_SUVD_CGC_GATE__SRE_HEVC_MASK
 694			| UVD_SUVD_CGC_GATE__SIT_H264_MASK
 695			| UVD_SUVD_CGC_GATE__SIT_HEVC_MASK
 696			| UVD_SUVD_CGC_GATE__SCM_H264_MASK
 697			| UVD_SUVD_CGC_GATE__SCM_HEVC_MASK
 698			| UVD_SUVD_CGC_GATE__SDB_H264_MASK
 699			| UVD_SUVD_CGC_GATE__SDB_HEVC_MASK
 700			| UVD_SUVD_CGC_GATE__SCLR_MASK
 701			| UVD_SUVD_CGC_GATE__UVD_SC_MASK
 702			| UVD_SUVD_CGC_GATE__ENT_MASK
 703			| UVD_SUVD_CGC_GATE__SIT_HEVC_DEC_MASK
 704			| UVD_SUVD_CGC_GATE__SIT_HEVC_ENC_MASK
 705			| UVD_SUVD_CGC_GATE__SITE_MASK
 706			| UVD_SUVD_CGC_GATE__SRE_VP9_MASK
 707			| UVD_SUVD_CGC_GATE__SCM_VP9_MASK
 708			| UVD_SUVD_CGC_GATE__SIT_VP9_DEC_MASK
 709			| UVD_SUVD_CGC_GATE__SDB_VP9_MASK
 710			| UVD_SUVD_CGC_GATE__IME_HEVC_MASK);
 711		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_GATE, data);
 712
 713		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL);
 714		data &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
 715			| UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
 716			| UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
 717			| UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
 718			| UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
 719			| UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
 720			| UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
 721			| UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
 722			| UVD_SUVD_CGC_CTRL__IME_MODE_MASK
 723			| UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
 724		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL, data);
 725	}
 726}
 727
 728static void vcn_v2_5_clock_gating_dpg_mode(struct amdgpu_device *adev,
 729		uint8_t sram_sel, int inst_idx, uint8_t indirect)
 730{
 731	uint32_t reg_data = 0;
 732
 733	/* enable sw clock gating control */
 734	if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 735		reg_data = 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 736	else
 737		reg_data = 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 738	reg_data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 739	reg_data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 740	reg_data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK |
 741		 UVD_CGC_CTRL__UDEC_CM_MODE_MASK |
 742		 UVD_CGC_CTRL__UDEC_IT_MODE_MASK |
 743		 UVD_CGC_CTRL__UDEC_DB_MODE_MASK |
 744		 UVD_CGC_CTRL__UDEC_MP_MODE_MASK |
 745		 UVD_CGC_CTRL__SYS_MODE_MASK |
 746		 UVD_CGC_CTRL__UDEC_MODE_MASK |
 747		 UVD_CGC_CTRL__MPEG2_MODE_MASK |
 748		 UVD_CGC_CTRL__REGS_MODE_MASK |
 749		 UVD_CGC_CTRL__RBC_MODE_MASK |
 750		 UVD_CGC_CTRL__LMI_MC_MODE_MASK |
 751		 UVD_CGC_CTRL__LMI_UMC_MODE_MASK |
 752		 UVD_CGC_CTRL__IDCT_MODE_MASK |
 753		 UVD_CGC_CTRL__MPRD_MODE_MASK |
 754		 UVD_CGC_CTRL__MPC_MODE_MASK |
 755		 UVD_CGC_CTRL__LBSI_MODE_MASK |
 756		 UVD_CGC_CTRL__LRBBM_MODE_MASK |
 757		 UVD_CGC_CTRL__WCB_MODE_MASK |
 758		 UVD_CGC_CTRL__VCPU_MODE_MASK |
 759		 UVD_CGC_CTRL__MMSCH_MODE_MASK);
 760	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 761		VCN, 0, mmUVD_CGC_CTRL), reg_data, sram_sel, indirect);
 762
 763	/* turn off clock gating */
 764	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 765		VCN, 0, mmUVD_CGC_GATE), 0, sram_sel, indirect);
 766
 767	/* turn on SUVD clock gating */
 768	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 769		VCN, 0, mmUVD_SUVD_CGC_GATE), 1, sram_sel, indirect);
 770
 771	/* turn on sw mode in UVD_SUVD_CGC_CTRL */
 772	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 773		VCN, 0, mmUVD_SUVD_CGC_CTRL), 0, sram_sel, indirect);
 774}
 775
 776/**
 777 * vcn_v2_5_enable_clock_gating - enable VCN clock gating
 778 *
 779 * @adev: amdgpu_device pointer
 780 *
 781 * Enable clock gating for VCN block
 782 */
 783static void vcn_v2_5_enable_clock_gating(struct amdgpu_device *adev)
 784{
 785	uint32_t data = 0;
 786	int i;
 787
 788	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
 789		if (adev->vcn.harvest_config & (1 << i))
 790			continue;
 791		/* enable UVD CGC */
 792		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 793		if (adev->cg_flags & AMD_CG_SUPPORT_VCN_MGCG)
 794			data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 795		else
 796			data |= 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
 797		data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
 798		data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
 799		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 800
 801		data = RREG32_SOC15(VCN, i, mmUVD_CGC_CTRL);
 802		data |= (UVD_CGC_CTRL__UDEC_RE_MODE_MASK
 803			| UVD_CGC_CTRL__UDEC_CM_MODE_MASK
 804			| UVD_CGC_CTRL__UDEC_IT_MODE_MASK
 805			| UVD_CGC_CTRL__UDEC_DB_MODE_MASK
 806			| UVD_CGC_CTRL__UDEC_MP_MODE_MASK
 807			| UVD_CGC_CTRL__SYS_MODE_MASK
 808			| UVD_CGC_CTRL__UDEC_MODE_MASK
 809			| UVD_CGC_CTRL__MPEG2_MODE_MASK
 810			| UVD_CGC_CTRL__REGS_MODE_MASK
 811			| UVD_CGC_CTRL__RBC_MODE_MASK
 812			| UVD_CGC_CTRL__LMI_MC_MODE_MASK
 813			| UVD_CGC_CTRL__LMI_UMC_MODE_MASK
 814			| UVD_CGC_CTRL__IDCT_MODE_MASK
 815			| UVD_CGC_CTRL__MPRD_MODE_MASK
 816			| UVD_CGC_CTRL__MPC_MODE_MASK
 817			| UVD_CGC_CTRL__LBSI_MODE_MASK
 818			| UVD_CGC_CTRL__LRBBM_MODE_MASK
 819			| UVD_CGC_CTRL__WCB_MODE_MASK
 820			| UVD_CGC_CTRL__VCPU_MODE_MASK);
 821		WREG32_SOC15(VCN, i, mmUVD_CGC_CTRL, data);
 822
 823		data = RREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL);
 824		data |= (UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
 825			| UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
 826			| UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
 827			| UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
 828			| UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
 829			| UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
 830			| UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
 831			| UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
 832			| UVD_SUVD_CGC_CTRL__IME_MODE_MASK
 833			| UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
 834		WREG32_SOC15(VCN, i, mmUVD_SUVD_CGC_CTRL, data);
 835	}
 836}
 837
 838static void vcn_v2_6_enable_ras(struct amdgpu_device *adev, int inst_idx,
 839				bool indirect)
 840{
 841	uint32_t tmp;
 842
 843	if (amdgpu_ip_version(adev, UVD_HWIP, 0) != IP_VERSION(2, 6, 0))
 844		return;
 845
 846	tmp = VCN_RAS_CNTL__VCPU_VCODEC_REARM_MASK |
 847	      VCN_RAS_CNTL__VCPU_VCODEC_IH_EN_MASK |
 848	      VCN_RAS_CNTL__VCPU_VCODEC_PMI_EN_MASK |
 849	      VCN_RAS_CNTL__VCPU_VCODEC_STALL_EN_MASK;
 850	WREG32_SOC15_DPG_MODE(inst_idx,
 851			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmVCN_RAS_CNTL),
 852			      tmp, 0, indirect);
 853
 854	tmp = UVD_VCPU_INT_EN__RASCNTL_VCPU_VCODEC_EN_MASK;
 855	WREG32_SOC15_DPG_MODE(inst_idx,
 856			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmUVD_VCPU_INT_EN),
 857			      tmp, 0, indirect);
 858
 859	tmp = UVD_SYS_INT_EN__RASCNTL_VCPU_VCODEC_EN_MASK;
 860	WREG32_SOC15_DPG_MODE(inst_idx,
 861			      SOC15_DPG_MODE_OFFSET(VCN, 0, mmUVD_SYS_INT_EN),
 862			      tmp, 0, indirect);
 863}
 864
 865static int vcn_v2_5_start_dpg_mode(struct amdgpu_device *adev, int inst_idx, bool indirect)
 866{
 867	volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[inst_idx].fw_shared.cpu_addr;
 868	struct amdgpu_ring *ring;
 869	uint32_t rb_bufsz, tmp;
 870
 871	/* disable register anti-hang mechanism */
 872	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS), 1,
 873		~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
 874	/* enable dynamic power gating mode */
 875	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_POWER_STATUS);
 876	tmp |= UVD_POWER_STATUS__UVD_PG_MODE_MASK;
 877	tmp |= UVD_POWER_STATUS__UVD_PG_EN_MASK;
 878	WREG32_SOC15(VCN, inst_idx, mmUVD_POWER_STATUS, tmp);
 879
 880	if (indirect)
 881		adev->vcn.inst[inst_idx].dpg_sram_curr_addr = (uint32_t *)adev->vcn.inst[inst_idx].dpg_sram_cpu_addr;
 882
 883	/* enable clock gating */
 884	vcn_v2_5_clock_gating_dpg_mode(adev, 0, inst_idx, indirect);
 885
 886	/* enable VCPU clock */
 887	tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT);
 888	tmp |= UVD_VCPU_CNTL__CLK_EN_MASK;
 889	tmp |= UVD_VCPU_CNTL__BLK_RST_MASK;
 890	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 891		VCN, 0, mmUVD_VCPU_CNTL), tmp, 0, indirect);
 892
 893	/* disable master interupt */
 894	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 895		VCN, 0, mmUVD_MASTINT_EN), 0, 0, indirect);
 896
 897	/* setup mmUVD_LMI_CTRL */
 898	tmp = (0x8 | UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK |
 899		UVD_LMI_CTRL__REQ_MODE_MASK |
 900		UVD_LMI_CTRL__CRC_RESET_MASK |
 901		UVD_LMI_CTRL__MASK_MC_URGENT_MASK |
 902		UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK |
 903		UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK |
 904		(8 << UVD_LMI_CTRL__WRITE_CLEAN_TIMER__SHIFT) |
 905		0x00100000L);
 906	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 907		VCN, 0, mmUVD_LMI_CTRL), tmp, 0, indirect);
 908
 909	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 910		VCN, 0, mmUVD_MPC_CNTL),
 911		0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT, 0, indirect);
 912
 913	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 914		VCN, 0, mmUVD_MPC_SET_MUXA0),
 915		((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) |
 916		 (0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) |
 917		 (0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) |
 918		 (0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT)), 0, indirect);
 919
 920	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 921		VCN, 0, mmUVD_MPC_SET_MUXB0),
 922		((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) |
 923		 (0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) |
 924		 (0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) |
 925		 (0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT)), 0, indirect);
 926
 927	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 928		VCN, 0, mmUVD_MPC_SET_MUX),
 929		((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) |
 930		 (0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) |
 931		 (0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT)), 0, indirect);
 932
 933	vcn_v2_5_mc_resume_dpg_mode(adev, inst_idx, indirect);
 934
 935	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 936		VCN, 0, mmUVD_REG_XX_MASK), 0x10, 0, indirect);
 937	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 938		VCN, 0, mmUVD_RBC_XX_IB_REG_CHECK), 0x3, 0, indirect);
 939
 940	/* enable LMI MC and UMC channels */
 941	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 942		VCN, 0, mmUVD_LMI_CTRL2), 0, 0, indirect);
 943
 944	vcn_v2_6_enable_ras(adev, inst_idx, indirect);
 945
 946	/* unblock VCPU register access */
 947	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 948		VCN, 0, mmUVD_RB_ARB_CTRL), 0, 0, indirect);
 949
 950	tmp = (0xFF << UVD_VCPU_CNTL__PRB_TIMEOUT_VAL__SHIFT);
 951	tmp |= UVD_VCPU_CNTL__CLK_EN_MASK;
 952	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 953		VCN, 0, mmUVD_VCPU_CNTL), tmp, 0, indirect);
 954
 955	/* enable master interrupt */
 956	WREG32_SOC15_DPG_MODE(inst_idx, SOC15_DPG_MODE_OFFSET(
 957		VCN, 0, mmUVD_MASTINT_EN),
 958		UVD_MASTINT_EN__VCPU_EN_MASK, 0, indirect);
 959
 960	if (indirect)
 961		amdgpu_vcn_psp_update_sram(adev, inst_idx, 0);
 
 
 962
 963	ring = &adev->vcn.inst[inst_idx].ring_dec;
 964	/* force RBC into idle state */
 965	rb_bufsz = order_base_2(ring->ring_size);
 966	tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
 967	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
 968	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
 969	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
 970	tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
 971	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_CNTL, tmp);
 972
 973	/* Stall DPG before WPTR/RPTR reset */
 974	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
 975		UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK,
 976		~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
 977	fw_shared->multi_queue.decode_queue_mode |= FW_QUEUE_RING_RESET;
 978
 979	/* set the write pointer delay */
 980	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR_CNTL, 0);
 981
 982	/* set the wb address */
 983	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR_ADDR,
 984		(upper_32_bits(ring->gpu_addr) >> 2));
 985
 986	/* program the RB_BASE for ring buffer */
 987	WREG32_SOC15(VCN, inst_idx, mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
 988		lower_32_bits(ring->gpu_addr));
 989	WREG32_SOC15(VCN, inst_idx, mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
 990		upper_32_bits(ring->gpu_addr));
 991
 992	/* Initialize the ring buffer's read and write pointers */
 993	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR, 0);
 994
 995	WREG32_SOC15(VCN, inst_idx, mmUVD_SCRATCH2, 0);
 996
 997	ring->wptr = RREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_RPTR);
 998	WREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR,
 999		lower_32_bits(ring->wptr));
1000
1001	fw_shared->multi_queue.decode_queue_mode &= ~FW_QUEUE_RING_RESET;
1002	/* Unstall DPG */
1003	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
1004		0, ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
1005
1006	return 0;
1007}
1008
1009static int vcn_v2_5_start(struct amdgpu_device *adev)
1010{
1011	struct amdgpu_ring *ring;
1012	uint32_t rb_bufsz, tmp;
1013	int i, j, k, r;
1014
1015	if (adev->pm.dpm_enabled)
1016		amdgpu_dpm_enable_uvd(adev, true);
1017
1018	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1019		if (adev->vcn.harvest_config & (1 << i))
1020			continue;
1021		if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
1022			r = vcn_v2_5_start_dpg_mode(adev, i, adev->vcn.indirect_sram);
1023			continue;
1024		}
1025
1026		/* disable register anti-hang mechanism */
1027		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_POWER_STATUS), 0,
1028			~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1029
1030		/* set uvd status busy */
1031		tmp = RREG32_SOC15(VCN, i, mmUVD_STATUS) | UVD_STATUS__UVD_BUSY;
1032		WREG32_SOC15(VCN, i, mmUVD_STATUS, tmp);
1033	}
1034
1035	if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)
1036		return 0;
1037
1038	/*SW clock gating */
1039	vcn_v2_5_disable_clock_gating(adev);
1040
1041	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1042		if (adev->vcn.harvest_config & (1 << i))
1043			continue;
1044		/* enable VCPU clock */
1045		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
1046			UVD_VCPU_CNTL__CLK_EN_MASK, ~UVD_VCPU_CNTL__CLK_EN_MASK);
1047
1048		/* disable master interrupt */
1049		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_MASTINT_EN), 0,
1050			~UVD_MASTINT_EN__VCPU_EN_MASK);
1051
1052		/* setup mmUVD_LMI_CTRL */
1053		tmp = RREG32_SOC15(VCN, i, mmUVD_LMI_CTRL);
1054		tmp &= ~0xff;
1055		WREG32_SOC15(VCN, i, mmUVD_LMI_CTRL, tmp | 0x8|
1056			UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK	|
1057			UVD_LMI_CTRL__MASK_MC_URGENT_MASK |
1058			UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK |
1059			UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK);
1060
1061		/* setup mmUVD_MPC_CNTL */
1062		tmp = RREG32_SOC15(VCN, i, mmUVD_MPC_CNTL);
1063		tmp &= ~UVD_MPC_CNTL__REPLACEMENT_MODE_MASK;
1064		tmp |= 0x2 << UVD_MPC_CNTL__REPLACEMENT_MODE__SHIFT;
1065		WREG32_SOC15(VCN, i, mmUVD_MPC_CNTL, tmp);
1066
1067		/* setup UVD_MPC_SET_MUXA0 */
1068		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUXA0,
1069			((0x1 << UVD_MPC_SET_MUXA0__VARA_1__SHIFT) |
1070			(0x2 << UVD_MPC_SET_MUXA0__VARA_2__SHIFT) |
1071			(0x3 << UVD_MPC_SET_MUXA0__VARA_3__SHIFT) |
1072			(0x4 << UVD_MPC_SET_MUXA0__VARA_4__SHIFT)));
1073
1074		/* setup UVD_MPC_SET_MUXB0 */
1075		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUXB0,
1076			((0x1 << UVD_MPC_SET_MUXB0__VARB_1__SHIFT) |
1077			(0x2 << UVD_MPC_SET_MUXB0__VARB_2__SHIFT) |
1078			(0x3 << UVD_MPC_SET_MUXB0__VARB_3__SHIFT) |
1079			(0x4 << UVD_MPC_SET_MUXB0__VARB_4__SHIFT)));
1080
1081		/* setup mmUVD_MPC_SET_MUX */
1082		WREG32_SOC15(VCN, i, mmUVD_MPC_SET_MUX,
1083			((0x0 << UVD_MPC_SET_MUX__SET_0__SHIFT) |
1084			(0x1 << UVD_MPC_SET_MUX__SET_1__SHIFT) |
1085			(0x2 << UVD_MPC_SET_MUX__SET_2__SHIFT)));
1086	}
1087
1088	vcn_v2_5_mc_resume(adev);
1089
1090	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1091		volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[i].fw_shared.cpu_addr;
1092		if (adev->vcn.harvest_config & (1 << i))
1093			continue;
1094		/* VCN global tiling registers */
1095		WREG32_SOC15(VCN, i, mmUVD_GFX8_ADDR_CONFIG,
1096			adev->gfx.config.gb_addr_config);
1097		WREG32_SOC15(VCN, i, mmUVD_GFX8_ADDR_CONFIG,
1098			adev->gfx.config.gb_addr_config);
1099
1100		/* enable LMI MC and UMC channels */
1101		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_LMI_CTRL2), 0,
1102			~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
1103
1104		/* unblock VCPU register access */
1105		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_RB_ARB_CTRL), 0,
1106			~UVD_RB_ARB_CTRL__VCPU_DIS_MASK);
1107
1108		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1109			~UVD_VCPU_CNTL__BLK_RST_MASK);
1110
1111		for (k = 0; k < 10; ++k) {
1112			uint32_t status;
1113
1114			for (j = 0; j < 100; ++j) {
1115				status = RREG32_SOC15(VCN, i, mmUVD_STATUS);
1116				if (status & 2)
1117					break;
1118				if (amdgpu_emu_mode == 1)
1119					msleep(500);
1120				else
1121					mdelay(10);
1122			}
1123			r = 0;
1124			if (status & 2)
1125				break;
1126
1127			DRM_ERROR("VCN decode not responding, trying to reset the VCPU!!!\n");
1128			WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
1129				UVD_VCPU_CNTL__BLK_RST_MASK,
1130				~UVD_VCPU_CNTL__BLK_RST_MASK);
1131			mdelay(10);
1132			WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1133				~UVD_VCPU_CNTL__BLK_RST_MASK);
1134
1135			mdelay(10);
1136			r = -1;
1137		}
1138
1139		if (r) {
1140			DRM_ERROR("VCN decode not responding, giving up!!!\n");
1141			return r;
1142		}
1143
1144		/* enable master interrupt */
1145		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_MASTINT_EN),
1146			UVD_MASTINT_EN__VCPU_EN_MASK,
1147			~UVD_MASTINT_EN__VCPU_EN_MASK);
1148
1149		/* clear the busy bit of VCN_STATUS */
1150		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_STATUS), 0,
1151			~(2 << UVD_STATUS__VCPU_REPORT__SHIFT));
1152
1153		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_VMID, 0);
1154
1155		ring = &adev->vcn.inst[i].ring_dec;
1156		/* force RBC into idle state */
1157		rb_bufsz = order_base_2(ring->ring_size);
1158		tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
1159		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
1160		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
1161		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
1162		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
1163		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_CNTL, tmp);
1164
1165		fw_shared->multi_queue.decode_queue_mode |= FW_QUEUE_RING_RESET;
1166		/* program the RB_BASE for ring buffer */
1167		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
1168			lower_32_bits(ring->gpu_addr));
1169		WREG32_SOC15(VCN, i, mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
1170			upper_32_bits(ring->gpu_addr));
1171
1172		/* Initialize the ring buffer's read and write pointers */
1173		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_RPTR, 0);
1174
1175		ring->wptr = RREG32_SOC15(VCN, i, mmUVD_RBC_RB_RPTR);
1176		WREG32_SOC15(VCN, i, mmUVD_RBC_RB_WPTR,
1177				lower_32_bits(ring->wptr));
1178		fw_shared->multi_queue.decode_queue_mode &= ~FW_QUEUE_RING_RESET;
1179
1180		fw_shared->multi_queue.encode_generalpurpose_queue_mode |= FW_QUEUE_RING_RESET;
1181		ring = &adev->vcn.inst[i].ring_enc[0];
1182		WREG32_SOC15(VCN, i, mmUVD_RB_RPTR, lower_32_bits(ring->wptr));
1183		WREG32_SOC15(VCN, i, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1184		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_LO, ring->gpu_addr);
1185		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
1186		WREG32_SOC15(VCN, i, mmUVD_RB_SIZE, ring->ring_size / 4);
1187		fw_shared->multi_queue.encode_generalpurpose_queue_mode &= ~FW_QUEUE_RING_RESET;
1188
1189		fw_shared->multi_queue.encode_lowlatency_queue_mode |= FW_QUEUE_RING_RESET;
1190		ring = &adev->vcn.inst[i].ring_enc[1];
1191		WREG32_SOC15(VCN, i, mmUVD_RB_RPTR2, lower_32_bits(ring->wptr));
1192		WREG32_SOC15(VCN, i, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1193		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_LO2, ring->gpu_addr);
1194		WREG32_SOC15(VCN, i, mmUVD_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
1195		WREG32_SOC15(VCN, i, mmUVD_RB_SIZE2, ring->ring_size / 4);
1196		fw_shared->multi_queue.encode_lowlatency_queue_mode &= ~FW_QUEUE_RING_RESET;
1197	}
1198
1199	return 0;
1200}
1201
1202static int vcn_v2_5_mmsch_start(struct amdgpu_device *adev,
1203				struct amdgpu_mm_table *table)
1204{
1205	uint32_t data = 0, loop = 0, size = 0;
1206	uint64_t addr = table->gpu_addr;
1207	struct mmsch_v1_1_init_header *header = NULL;
1208
1209	header = (struct mmsch_v1_1_init_header *)table->cpu_addr;
1210	size = header->total_size;
1211
1212	/*
1213	 * 1, write to vce_mmsch_vf_ctx_addr_lo/hi register with GPU mc addr of
1214	 *  memory descriptor location
1215	 */
1216	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_ADDR_LO, lower_32_bits(addr));
1217	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_ADDR_HI, upper_32_bits(addr));
1218
1219	/* 2, update vmid of descriptor */
1220	data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_VMID);
1221	data &= ~MMSCH_VF_VMID__VF_CTX_VMID_MASK;
1222	/* use domain0 for MM scheduler */
1223	data |= (0 << MMSCH_VF_VMID__VF_CTX_VMID__SHIFT);
1224	WREG32_SOC15(VCN, 0, mmMMSCH_VF_VMID, data);
1225
1226	/* 3, notify mmsch about the size of this descriptor */
1227	WREG32_SOC15(VCN, 0, mmMMSCH_VF_CTX_SIZE, size);
1228
1229	/* 4, set resp to zero */
1230	WREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP, 0);
1231
1232	/*
1233	 * 5, kick off the initialization and wait until
1234	 * VCE_MMSCH_VF_MAILBOX_RESP becomes non-zero
1235	 */
1236	WREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_HOST, 0x10000001);
1237
1238	data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP);
1239	loop = 10;
1240	while ((data & 0x10000002) != 0x10000002) {
1241		udelay(100);
1242		data = RREG32_SOC15(VCN, 0, mmMMSCH_VF_MAILBOX_RESP);
1243		loop--;
1244		if (!loop)
1245			break;
1246	}
1247
1248	if (!loop) {
1249		dev_err(adev->dev,
1250			"failed to init MMSCH, mmMMSCH_VF_MAILBOX_RESP = %x\n",
1251			data);
1252		return -EBUSY;
1253	}
1254
1255	return 0;
1256}
1257
1258static int vcn_v2_5_sriov_start(struct amdgpu_device *adev)
1259{
1260	struct amdgpu_ring *ring;
1261	uint32_t offset, size, tmp, i, rb_bufsz;
1262	uint32_t table_size = 0;
1263	struct mmsch_v1_0_cmd_direct_write direct_wt = { { 0 } };
1264	struct mmsch_v1_0_cmd_direct_read_modify_write direct_rd_mod_wt = { { 0 } };
1265	struct mmsch_v1_0_cmd_end end = { { 0 } };
1266	uint32_t *init_table = adev->virt.mm_table.cpu_addr;
1267	struct mmsch_v1_1_init_header *header = (struct mmsch_v1_1_init_header *)init_table;
1268
1269	direct_wt.cmd_header.command_type = MMSCH_COMMAND__DIRECT_REG_WRITE;
1270	direct_rd_mod_wt.cmd_header.command_type = MMSCH_COMMAND__DIRECT_REG_READ_MODIFY_WRITE;
1271	end.cmd_header.command_type = MMSCH_COMMAND__END;
1272
1273	header->version = MMSCH_VERSION;
1274	header->total_size = sizeof(struct mmsch_v1_1_init_header) >> 2;
1275	init_table += header->total_size;
1276
1277	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1278		header->eng[i].table_offset = header->total_size;
1279		header->eng[i].init_status = 0;
1280		header->eng[i].table_size = 0;
1281
1282		table_size = 0;
1283
1284		MMSCH_V1_0_INSERT_DIRECT_RD_MOD_WT(
1285			SOC15_REG_OFFSET(VCN, i, mmUVD_STATUS),
1286			~UVD_STATUS__UVD_BUSY, UVD_STATUS__UVD_BUSY);
1287
1288		size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw[i]->size + 4);
1289		/* mc resume*/
1290		if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1291			MMSCH_V1_0_INSERT_DIRECT_WT(
1292				SOC15_REG_OFFSET(VCN, i,
1293					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
1294				adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_lo);
1295			MMSCH_V1_0_INSERT_DIRECT_WT(
1296				SOC15_REG_OFFSET(VCN, i,
1297					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
1298				adev->firmware.ucode[AMDGPU_UCODE_ID_VCN + i].tmr_mc_addr_hi);
1299			offset = 0;
1300			MMSCH_V1_0_INSERT_DIRECT_WT(
1301				SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET0), 0);
1302		} else {
1303			MMSCH_V1_0_INSERT_DIRECT_WT(
1304				SOC15_REG_OFFSET(VCN, i,
1305					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW),
1306				lower_32_bits(adev->vcn.inst[i].gpu_addr));
1307			MMSCH_V1_0_INSERT_DIRECT_WT(
1308				SOC15_REG_OFFSET(VCN, i,
1309					mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH),
1310				upper_32_bits(adev->vcn.inst[i].gpu_addr));
1311			offset = size;
1312			MMSCH_V1_0_INSERT_DIRECT_WT(
1313				SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET0),
1314				AMDGPU_UVD_FIRMWARE_OFFSET >> 3);
1315		}
1316
1317		MMSCH_V1_0_INSERT_DIRECT_WT(
1318			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE0),
1319			size);
1320		MMSCH_V1_0_INSERT_DIRECT_WT(
1321			SOC15_REG_OFFSET(VCN, i,
1322				mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW),
1323			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset));
1324		MMSCH_V1_0_INSERT_DIRECT_WT(
1325			SOC15_REG_OFFSET(VCN, i,
1326				mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH),
1327			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset));
1328		MMSCH_V1_0_INSERT_DIRECT_WT(
1329			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET1),
1330			0);
1331		MMSCH_V1_0_INSERT_DIRECT_WT(
1332			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE1),
1333			AMDGPU_VCN_STACK_SIZE);
1334		MMSCH_V1_0_INSERT_DIRECT_WT(
1335			SOC15_REG_OFFSET(VCN, i,
1336				mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW),
1337			lower_32_bits(adev->vcn.inst[i].gpu_addr + offset +
1338				AMDGPU_VCN_STACK_SIZE));
1339		MMSCH_V1_0_INSERT_DIRECT_WT(
1340			SOC15_REG_OFFSET(VCN, i,
1341				mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH),
1342			upper_32_bits(adev->vcn.inst[i].gpu_addr + offset +
1343				AMDGPU_VCN_STACK_SIZE));
1344		MMSCH_V1_0_INSERT_DIRECT_WT(
1345			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_OFFSET2),
1346			0);
1347		MMSCH_V1_0_INSERT_DIRECT_WT(
1348			SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CACHE_SIZE2),
1349			AMDGPU_VCN_CONTEXT_SIZE);
1350
1351		ring = &adev->vcn.inst[i].ring_enc[0];
1352		ring->wptr = 0;
1353
1354		MMSCH_V1_0_INSERT_DIRECT_WT(
1355			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_BASE_LO),
1356			lower_32_bits(ring->gpu_addr));
1357		MMSCH_V1_0_INSERT_DIRECT_WT(
1358			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_BASE_HI),
1359			upper_32_bits(ring->gpu_addr));
1360		MMSCH_V1_0_INSERT_DIRECT_WT(
1361			SOC15_REG_OFFSET(VCN, i, mmUVD_RB_SIZE),
1362			ring->ring_size / 4);
1363
1364		ring = &adev->vcn.inst[i].ring_dec;
1365		ring->wptr = 0;
1366		MMSCH_V1_0_INSERT_DIRECT_WT(
1367			SOC15_REG_OFFSET(VCN, i,
1368				mmUVD_LMI_RBC_RB_64BIT_BAR_LOW),
1369			lower_32_bits(ring->gpu_addr));
1370		MMSCH_V1_0_INSERT_DIRECT_WT(
1371			SOC15_REG_OFFSET(VCN, i,
1372				mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH),
1373			upper_32_bits(ring->gpu_addr));
1374
1375		/* force RBC into idle state */
1376		rb_bufsz = order_base_2(ring->ring_size);
1377		tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
1378		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
1379		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
1380		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
1381		tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
1382		MMSCH_V1_0_INSERT_DIRECT_WT(
1383			SOC15_REG_OFFSET(VCN, i, mmUVD_RBC_RB_CNTL), tmp);
1384
1385		/* add end packet */
1386		memcpy((void *)init_table, &end, sizeof(struct mmsch_v1_0_cmd_end));
1387		table_size += sizeof(struct mmsch_v1_0_cmd_end) / 4;
1388		init_table += sizeof(struct mmsch_v1_0_cmd_end) / 4;
1389
1390		/* refine header */
1391		header->eng[i].table_size = table_size;
1392		header->total_size += table_size;
1393	}
1394
1395	return vcn_v2_5_mmsch_start(adev, &adev->virt.mm_table);
1396}
1397
1398static int vcn_v2_5_stop_dpg_mode(struct amdgpu_device *adev, int inst_idx)
1399{
1400	uint32_t tmp;
1401
1402	/* Wait for power status to be 1 */
1403	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 1,
1404		UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1405
1406	/* wait for read ptr to be equal to write ptr */
1407	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR);
1408	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RB_RPTR, tmp, 0xFFFFFFFF);
1409
1410	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR2);
1411	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RB_RPTR2, tmp, 0xFFFFFFFF);
1412
1413	tmp = RREG32_SOC15(VCN, inst_idx, mmUVD_RBC_RB_WPTR) & 0x7FFFFFFF;
1414	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_RBC_RB_RPTR, tmp, 0xFFFFFFFF);
1415
1416	SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 1,
1417		UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1418
1419	/* disable dynamic power gating mode */
1420	WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS), 0,
1421			~UVD_POWER_STATUS__UVD_PG_MODE_MASK);
1422
1423	return 0;
1424}
1425
1426static int vcn_v2_5_stop(struct amdgpu_device *adev)
1427{
1428	uint32_t tmp;
1429	int i, r = 0;
1430
1431	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1432		if (adev->vcn.harvest_config & (1 << i))
1433			continue;
1434		if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG) {
1435			r = vcn_v2_5_stop_dpg_mode(adev, i);
1436			continue;
1437		}
1438
1439		/* wait for vcn idle */
1440		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_STATUS, UVD_STATUS__IDLE, 0x7);
1441		if (r)
1442			return r;
1443
1444		tmp = UVD_LMI_STATUS__VCPU_LMI_WRITE_CLEAN_MASK |
1445			UVD_LMI_STATUS__READ_CLEAN_MASK |
1446			UVD_LMI_STATUS__WRITE_CLEAN_MASK |
1447			UVD_LMI_STATUS__WRITE_CLEAN_RAW_MASK;
1448		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_LMI_STATUS, tmp, tmp);
1449		if (r)
1450			return r;
1451
1452		/* block LMI UMC channel */
1453		tmp = RREG32_SOC15(VCN, i, mmUVD_LMI_CTRL2);
1454		tmp |= UVD_LMI_CTRL2__STALL_ARB_UMC_MASK;
1455		WREG32_SOC15(VCN, i, mmUVD_LMI_CTRL2, tmp);
1456
1457		tmp = UVD_LMI_STATUS__UMC_READ_CLEAN_RAW_MASK|
1458			UVD_LMI_STATUS__UMC_WRITE_CLEAN_RAW_MASK;
1459		r = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_LMI_STATUS, tmp, tmp);
1460		if (r)
1461			return r;
1462
1463		/* block VCPU register access */
1464		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_RB_ARB_CTRL),
1465			UVD_RB_ARB_CTRL__VCPU_DIS_MASK,
1466			~UVD_RB_ARB_CTRL__VCPU_DIS_MASK);
1467
1468		/* reset VCPU */
1469		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL),
1470			UVD_VCPU_CNTL__BLK_RST_MASK,
1471			~UVD_VCPU_CNTL__BLK_RST_MASK);
1472
1473		/* disable VCPU clock */
1474		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_VCPU_CNTL), 0,
1475			~(UVD_VCPU_CNTL__CLK_EN_MASK));
1476
1477		/* clear status */
1478		WREG32_SOC15(VCN, i, mmUVD_STATUS, 0);
1479
1480		vcn_v2_5_enable_clock_gating(adev);
1481
1482		/* enable register anti-hang mechanism */
1483		WREG32_P(SOC15_REG_OFFSET(VCN, i, mmUVD_POWER_STATUS),
1484			UVD_POWER_STATUS__UVD_POWER_STATUS_MASK,
1485			~UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1486	}
1487
1488	if (adev->pm.dpm_enabled)
1489		amdgpu_dpm_enable_uvd(adev, false);
1490
1491	return 0;
1492}
1493
1494static int vcn_v2_5_pause_dpg_mode(struct amdgpu_device *adev,
1495				int inst_idx, struct dpg_pause_state *new_state)
1496{
1497	struct amdgpu_ring *ring;
1498	uint32_t reg_data = 0;
1499	int ret_code = 0;
1500
1501	/* pause/unpause if state is changed */
1502	if (adev->vcn.inst[inst_idx].pause_state.fw_based != new_state->fw_based) {
1503		DRM_DEBUG("dpg pause state changed %d -> %d",
1504			adev->vcn.inst[inst_idx].pause_state.fw_based,	new_state->fw_based);
1505		reg_data = RREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE) &
1506			(~UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK);
1507
1508		if (new_state->fw_based == VCN_DPG_STATE__PAUSE) {
1509			ret_code = SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 0x1,
1510				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1511
1512			if (!ret_code) {
1513				volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst[inst_idx].fw_shared.cpu_addr;
1514
1515				/* pause DPG */
1516				reg_data |= UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK;
1517				WREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE, reg_data);
1518
1519				/* wait for ACK */
1520				SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_DPG_PAUSE,
1521					   UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK,
1522					   UVD_DPG_PAUSE__NJ_PAUSE_DPG_ACK_MASK);
1523
1524				/* Stall DPG before WPTR/RPTR reset */
1525				WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
1526					   UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK,
1527					   ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
1528
1529				/* Restore */
1530				fw_shared->multi_queue.encode_generalpurpose_queue_mode |= FW_QUEUE_RING_RESET;
1531				ring = &adev->vcn.inst[inst_idx].ring_enc[0];
1532				ring->wptr = 0;
1533				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_LO, ring->gpu_addr);
1534				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
1535				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_SIZE, ring->ring_size / 4);
1536				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_RPTR, lower_32_bits(ring->wptr));
1537				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1538				fw_shared->multi_queue.encode_generalpurpose_queue_mode &= ~FW_QUEUE_RING_RESET;
1539
1540				fw_shared->multi_queue.encode_lowlatency_queue_mode |= FW_QUEUE_RING_RESET;
1541				ring = &adev->vcn.inst[inst_idx].ring_enc[1];
1542				ring->wptr = 0;
1543				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_LO2, ring->gpu_addr);
1544				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
1545				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_SIZE2, ring->ring_size / 4);
1546				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_RPTR2, lower_32_bits(ring->wptr));
1547				WREG32_SOC15(VCN, inst_idx, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1548				fw_shared->multi_queue.encode_lowlatency_queue_mode &= ~FW_QUEUE_RING_RESET;
1549
1550				/* Unstall DPG */
1551				WREG32_P(SOC15_REG_OFFSET(VCN, inst_idx, mmUVD_POWER_STATUS),
1552					   0, ~UVD_POWER_STATUS__STALL_DPG_POWER_UP_MASK);
1553
1554				SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS,
1555					   UVD_PGFSM_CONFIG__UVDM_UVDU_PWR_ON, UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1556			}
1557		} else {
1558			reg_data &= ~UVD_DPG_PAUSE__NJ_PAUSE_DPG_REQ_MASK;
1559			WREG32_SOC15(VCN, inst_idx, mmUVD_DPG_PAUSE, reg_data);
1560			SOC15_WAIT_ON_RREG(VCN, inst_idx, mmUVD_POWER_STATUS, 0x1,
1561				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK);
1562		}
1563		adev->vcn.inst[inst_idx].pause_state.fw_based = new_state->fw_based;
1564	}
1565
1566	return 0;
1567}
1568
1569/**
1570 * vcn_v2_5_dec_ring_get_rptr - get read pointer
1571 *
1572 * @ring: amdgpu_ring pointer
1573 *
1574 * Returns the current hardware read pointer
1575 */
1576static uint64_t vcn_v2_5_dec_ring_get_rptr(struct amdgpu_ring *ring)
1577{
1578	struct amdgpu_device *adev = ring->adev;
1579
1580	return RREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_RPTR);
1581}
1582
1583/**
1584 * vcn_v2_5_dec_ring_get_wptr - get write pointer
1585 *
1586 * @ring: amdgpu_ring pointer
1587 *
1588 * Returns the current hardware write pointer
1589 */
1590static uint64_t vcn_v2_5_dec_ring_get_wptr(struct amdgpu_ring *ring)
1591{
1592	struct amdgpu_device *adev = ring->adev;
1593
1594	if (ring->use_doorbell)
1595		return *ring->wptr_cpu_addr;
1596	else
1597		return RREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_WPTR);
1598}
1599
1600/**
1601 * vcn_v2_5_dec_ring_set_wptr - set write pointer
1602 *
1603 * @ring: amdgpu_ring pointer
1604 *
1605 * Commits the write pointer to the hardware
1606 */
1607static void vcn_v2_5_dec_ring_set_wptr(struct amdgpu_ring *ring)
1608{
1609	struct amdgpu_device *adev = ring->adev;
1610
1611	if (ring->use_doorbell) {
1612		*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1613		WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1614	} else {
1615		WREG32_SOC15(VCN, ring->me, mmUVD_RBC_RB_WPTR, lower_32_bits(ring->wptr));
1616	}
1617}
1618
1619static const struct amdgpu_ring_funcs vcn_v2_5_dec_ring_vm_funcs = {
1620	.type = AMDGPU_RING_TYPE_VCN_DEC,
1621	.align_mask = 0xf,
1622	.secure_submission_supported = true,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1623	.get_rptr = vcn_v2_5_dec_ring_get_rptr,
1624	.get_wptr = vcn_v2_5_dec_ring_get_wptr,
1625	.set_wptr = vcn_v2_5_dec_ring_set_wptr,
1626	.emit_frame_size =
1627		SOC15_FLUSH_GPU_TLB_NUM_WREG * 6 +
1628		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 8 +
1629		8 + /* vcn_v2_0_dec_ring_emit_vm_flush */
1630		14 + 14 + /* vcn_v2_0_dec_ring_emit_fence x2 vm fence */
1631		6,
1632	.emit_ib_size = 8, /* vcn_v2_0_dec_ring_emit_ib */
1633	.emit_ib = vcn_v2_0_dec_ring_emit_ib,
1634	.emit_fence = vcn_v2_0_dec_ring_emit_fence,
1635	.emit_vm_flush = vcn_v2_0_dec_ring_emit_vm_flush,
1636	.test_ring = vcn_v2_0_dec_ring_test_ring,
1637	.test_ib = amdgpu_vcn_dec_ring_test_ib,
1638	.insert_nop = vcn_v2_0_dec_ring_insert_nop,
1639	.insert_start = vcn_v2_0_dec_ring_insert_start,
1640	.insert_end = vcn_v2_0_dec_ring_insert_end,
1641	.pad_ib = amdgpu_ring_generic_pad_ib,
1642	.begin_use = amdgpu_vcn_ring_begin_use,
1643	.end_use = amdgpu_vcn_ring_end_use,
1644	.emit_wreg = vcn_v2_0_dec_ring_emit_wreg,
1645	.emit_reg_wait = vcn_v2_0_dec_ring_emit_reg_wait,
1646	.emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1647};
1648
1649/**
1650 * vcn_v2_5_enc_ring_get_rptr - get enc read pointer
1651 *
1652 * @ring: amdgpu_ring pointer
1653 *
1654 * Returns the current hardware enc read pointer
1655 */
1656static uint64_t vcn_v2_5_enc_ring_get_rptr(struct amdgpu_ring *ring)
1657{
1658	struct amdgpu_device *adev = ring->adev;
1659
1660	if (ring == &adev->vcn.inst[ring->me].ring_enc[0])
1661		return RREG32_SOC15(VCN, ring->me, mmUVD_RB_RPTR);
1662	else
1663		return RREG32_SOC15(VCN, ring->me, mmUVD_RB_RPTR2);
1664}
1665
1666/**
1667 * vcn_v2_5_enc_ring_get_wptr - get enc write pointer
1668 *
1669 * @ring: amdgpu_ring pointer
1670 *
1671 * Returns the current hardware enc write pointer
1672 */
1673static uint64_t vcn_v2_5_enc_ring_get_wptr(struct amdgpu_ring *ring)
1674{
1675	struct amdgpu_device *adev = ring->adev;
1676
1677	if (ring == &adev->vcn.inst[ring->me].ring_enc[0]) {
1678		if (ring->use_doorbell)
1679			return *ring->wptr_cpu_addr;
1680		else
1681			return RREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR);
1682	} else {
1683		if (ring->use_doorbell)
1684			return *ring->wptr_cpu_addr;
1685		else
1686			return RREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR2);
1687	}
1688}
1689
1690/**
1691 * vcn_v2_5_enc_ring_set_wptr - set enc write pointer
1692 *
1693 * @ring: amdgpu_ring pointer
1694 *
1695 * Commits the enc write pointer to the hardware
1696 */
1697static void vcn_v2_5_enc_ring_set_wptr(struct amdgpu_ring *ring)
1698{
1699	struct amdgpu_device *adev = ring->adev;
1700
1701	if (ring == &adev->vcn.inst[ring->me].ring_enc[0]) {
1702		if (ring->use_doorbell) {
1703			*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1704			WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1705		} else {
1706			WREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
1707		}
1708	} else {
1709		if (ring->use_doorbell) {
1710			*ring->wptr_cpu_addr = lower_32_bits(ring->wptr);
1711			WDOORBELL32(ring->doorbell_index, lower_32_bits(ring->wptr));
1712		} else {
1713			WREG32_SOC15(VCN, ring->me, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
1714		}
1715	}
1716}
1717
1718static const struct amdgpu_ring_funcs vcn_v2_5_enc_ring_vm_funcs = {
1719	.type = AMDGPU_RING_TYPE_VCN_ENC,
1720	.align_mask = 0x3f,
1721	.nop = VCN_ENC_CMD_NO_OP,
 
1722	.get_rptr = vcn_v2_5_enc_ring_get_rptr,
1723	.get_wptr = vcn_v2_5_enc_ring_get_wptr,
1724	.set_wptr = vcn_v2_5_enc_ring_set_wptr,
1725	.emit_frame_size =
1726		SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
1727		SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 4 +
1728		4 + /* vcn_v2_0_enc_ring_emit_vm_flush */
1729		5 + 5 + /* vcn_v2_0_enc_ring_emit_fence x2 vm fence */
1730		1, /* vcn_v2_0_enc_ring_insert_end */
1731	.emit_ib_size = 5, /* vcn_v2_0_enc_ring_emit_ib */
1732	.emit_ib = vcn_v2_0_enc_ring_emit_ib,
1733	.emit_fence = vcn_v2_0_enc_ring_emit_fence,
1734	.emit_vm_flush = vcn_v2_0_enc_ring_emit_vm_flush,
1735	.test_ring = amdgpu_vcn_enc_ring_test_ring,
1736	.test_ib = amdgpu_vcn_enc_ring_test_ib,
1737	.insert_nop = amdgpu_ring_insert_nop,
1738	.insert_end = vcn_v2_0_enc_ring_insert_end,
1739	.pad_ib = amdgpu_ring_generic_pad_ib,
1740	.begin_use = amdgpu_vcn_ring_begin_use,
1741	.end_use = amdgpu_vcn_ring_end_use,
1742	.emit_wreg = vcn_v2_0_enc_ring_emit_wreg,
1743	.emit_reg_wait = vcn_v2_0_enc_ring_emit_reg_wait,
1744	.emit_reg_write_reg_wait = amdgpu_ring_emit_reg_write_reg_wait_helper,
1745};
1746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1747static void vcn_v2_5_set_dec_ring_funcs(struct amdgpu_device *adev)
1748{
1749	int i;
1750
1751	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1752		if (adev->vcn.harvest_config & (1 << i))
1753			continue;
1754		adev->vcn.inst[i].ring_dec.funcs = &vcn_v2_5_dec_ring_vm_funcs;
 
 
 
1755		adev->vcn.inst[i].ring_dec.me = i;
 
1756	}
1757}
1758
1759static void vcn_v2_5_set_enc_ring_funcs(struct amdgpu_device *adev)
1760{
1761	int i, j;
1762
1763	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
1764		if (adev->vcn.harvest_config & (1 << j))
1765			continue;
1766		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
1767			adev->vcn.inst[j].ring_enc[i].funcs = &vcn_v2_5_enc_ring_vm_funcs;
 
 
 
1768			adev->vcn.inst[j].ring_enc[i].me = j;
1769		}
 
1770	}
1771}
1772
1773static bool vcn_v2_5_is_idle(void *handle)
1774{
1775	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1776	int i, ret = 1;
1777
1778	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1779		if (adev->vcn.harvest_config & (1 << i))
1780			continue;
1781		ret &= (RREG32_SOC15(VCN, i, mmUVD_STATUS) == UVD_STATUS__IDLE);
1782	}
1783
1784	return ret;
1785}
1786
1787static int vcn_v2_5_wait_for_idle(struct amdgpu_ip_block *ip_block)
1788{
1789	struct amdgpu_device *adev = ip_block->adev;
1790	int i, ret = 0;
1791
1792	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1793		if (adev->vcn.harvest_config & (1 << i))
1794			continue;
1795		ret = SOC15_WAIT_ON_RREG(VCN, i, mmUVD_STATUS, UVD_STATUS__IDLE,
1796			UVD_STATUS__IDLE);
1797		if (ret)
1798			return ret;
1799	}
1800
1801	return ret;
1802}
1803
1804static int vcn_v2_5_set_clockgating_state(void *handle,
1805					  enum amd_clockgating_state state)
1806{
1807	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1808	bool enable = (state == AMD_CG_STATE_GATE);
1809
1810	if (amdgpu_sriov_vf(adev))
1811		return 0;
1812
1813	if (enable) {
1814		if (!vcn_v2_5_is_idle(handle))
1815			return -EBUSY;
1816		vcn_v2_5_enable_clock_gating(adev);
1817	} else {
1818		vcn_v2_5_disable_clock_gating(adev);
1819	}
1820
1821	return 0;
1822}
1823
1824static int vcn_v2_5_set_powergating_state(void *handle,
1825					  enum amd_powergating_state state)
1826{
1827	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1828	int ret;
1829
1830	if (amdgpu_sriov_vf(adev))
1831		return 0;
1832
1833	if(state == adev->vcn.cur_state)
1834		return 0;
1835
1836	if (state == AMD_PG_STATE_GATE)
1837		ret = vcn_v2_5_stop(adev);
1838	else
1839		ret = vcn_v2_5_start(adev);
1840
1841	if(!ret)
1842		adev->vcn.cur_state = state;
1843
1844	return ret;
1845}
1846
1847static int vcn_v2_5_set_interrupt_state(struct amdgpu_device *adev,
1848					struct amdgpu_irq_src *source,
1849					unsigned type,
1850					enum amdgpu_interrupt_state state)
1851{
1852	return 0;
1853}
1854
1855static int vcn_v2_6_set_ras_interrupt_state(struct amdgpu_device *adev,
1856					struct amdgpu_irq_src *source,
1857					unsigned int type,
1858					enum amdgpu_interrupt_state state)
1859{
1860	return 0;
1861}
1862
1863static int vcn_v2_5_process_interrupt(struct amdgpu_device *adev,
1864				      struct amdgpu_irq_src *source,
1865				      struct amdgpu_iv_entry *entry)
1866{
1867	uint32_t ip_instance;
1868
1869	switch (entry->client_id) {
1870	case SOC15_IH_CLIENTID_VCN:
1871		ip_instance = 0;
1872		break;
1873	case SOC15_IH_CLIENTID_VCN1:
1874		ip_instance = 1;
1875		break;
1876	default:
1877		DRM_ERROR("Unhandled client id: %d\n", entry->client_id);
1878		return 0;
1879	}
1880
1881	DRM_DEBUG("IH: VCN TRAP\n");
1882
1883	switch (entry->src_id) {
1884	case VCN_2_0__SRCID__UVD_SYSTEM_MESSAGE_INTERRUPT:
1885		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_dec);
1886		break;
1887	case VCN_2_0__SRCID__UVD_ENC_GENERAL_PURPOSE:
1888		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_enc[0]);
1889		break;
1890	case VCN_2_0__SRCID__UVD_ENC_LOW_LATENCY:
1891		amdgpu_fence_process(&adev->vcn.inst[ip_instance].ring_enc[1]);
1892		break;
 
 
 
1893	default:
1894		DRM_ERROR("Unhandled interrupt: %d %d\n",
1895			  entry->src_id, entry->src_data[0]);
1896		break;
1897	}
1898
1899	return 0;
1900}
1901
1902static const struct amdgpu_irq_src_funcs vcn_v2_5_irq_funcs = {
1903	.set = vcn_v2_5_set_interrupt_state,
1904	.process = vcn_v2_5_process_interrupt,
1905};
1906
1907static const struct amdgpu_irq_src_funcs vcn_v2_6_ras_irq_funcs = {
1908	.set = vcn_v2_6_set_ras_interrupt_state,
1909	.process = amdgpu_vcn_process_poison_irq,
1910};
1911
1912static void vcn_v2_5_set_irq_funcs(struct amdgpu_device *adev)
1913{
1914	int i;
1915
1916	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
1917		if (adev->vcn.harvest_config & (1 << i))
1918			continue;
1919		adev->vcn.inst[i].irq.num_types = adev->vcn.num_enc_rings + 1;
1920		adev->vcn.inst[i].irq.funcs = &vcn_v2_5_irq_funcs;
1921
1922		adev->vcn.inst[i].ras_poison_irq.num_types = adev->vcn.num_enc_rings + 1;
1923		adev->vcn.inst[i].ras_poison_irq.funcs = &vcn_v2_6_ras_irq_funcs;
1924	}
1925}
1926
1927static void vcn_v2_5_print_ip_state(struct amdgpu_ip_block *ip_block, struct drm_printer *p)
1928{
1929	struct amdgpu_device *adev = ip_block->adev;
1930	int i, j;
1931	uint32_t reg_count = ARRAY_SIZE(vcn_reg_list_2_5);
1932	uint32_t inst_off, is_powered;
1933
1934	if (!adev->vcn.ip_dump)
1935		return;
1936
1937	drm_printf(p, "num_instances:%d\n", adev->vcn.num_vcn_inst);
1938	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
1939		if (adev->vcn.harvest_config & (1 << i)) {
1940			drm_printf(p, "\nHarvested Instance:VCN%d Skipping dump\n", i);
1941			continue;
1942		}
1943
1944		inst_off = i * reg_count;
1945		is_powered = (adev->vcn.ip_dump[inst_off] &
1946				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK) != 1;
1947
1948		if (is_powered) {
1949			drm_printf(p, "\nActive Instance:VCN%d\n", i);
1950			for (j = 0; j < reg_count; j++)
1951				drm_printf(p, "%-50s \t 0x%08x\n", vcn_reg_list_2_5[j].reg_name,
1952					   adev->vcn.ip_dump[inst_off + j]);
1953		} else {
1954			drm_printf(p, "\nInactive Instance:VCN%d\n", i);
1955		}
1956	}
1957}
1958
1959static void vcn_v2_5_dump_ip_state(struct amdgpu_ip_block *ip_block)
1960{
1961	struct amdgpu_device *adev = ip_block->adev;
1962	int i, j;
1963	bool is_powered;
1964	uint32_t inst_off;
1965	uint32_t reg_count = ARRAY_SIZE(vcn_reg_list_2_5);
1966
1967	if (!adev->vcn.ip_dump)
1968		return;
1969
1970	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
1971		if (adev->vcn.harvest_config & (1 << i))
1972			continue;
1973
1974		inst_off = i * reg_count;
1975		/* mmUVD_POWER_STATUS is always readable and is first element of the array */
1976		adev->vcn.ip_dump[inst_off] = RREG32_SOC15(VCN, i, mmUVD_POWER_STATUS);
1977		is_powered = (adev->vcn.ip_dump[inst_off] &
1978				UVD_POWER_STATUS__UVD_POWER_STATUS_MASK) != 1;
1979
1980		if (is_powered)
1981			for (j = 1; j < reg_count; j++)
1982				adev->vcn.ip_dump[inst_off + j] =
1983					RREG32(SOC15_REG_ENTRY_OFFSET_INST(vcn_reg_list_2_5[j], i));
1984	}
1985}
1986
1987static const struct amd_ip_funcs vcn_v2_5_ip_funcs = {
1988	.name = "vcn_v2_5",
1989	.early_init = vcn_v2_5_early_init,
 
1990	.sw_init = vcn_v2_5_sw_init,
1991	.sw_fini = vcn_v2_5_sw_fini,
1992	.hw_init = vcn_v2_5_hw_init,
1993	.hw_fini = vcn_v2_5_hw_fini,
1994	.suspend = vcn_v2_5_suspend,
1995	.resume = vcn_v2_5_resume,
1996	.is_idle = vcn_v2_5_is_idle,
1997	.wait_for_idle = vcn_v2_5_wait_for_idle,
 
 
 
 
1998	.set_clockgating_state = vcn_v2_5_set_clockgating_state,
1999	.set_powergating_state = vcn_v2_5_set_powergating_state,
2000	.dump_ip_state = vcn_v2_5_dump_ip_state,
2001	.print_ip_state = vcn_v2_5_print_ip_state,
2002};
2003
2004static const struct amd_ip_funcs vcn_v2_6_ip_funcs = {
2005        .name = "vcn_v2_6",
2006        .early_init = vcn_v2_5_early_init,
 
2007        .sw_init = vcn_v2_5_sw_init,
2008        .sw_fini = vcn_v2_5_sw_fini,
2009        .hw_init = vcn_v2_5_hw_init,
2010        .hw_fini = vcn_v2_5_hw_fini,
2011        .suspend = vcn_v2_5_suspend,
2012        .resume = vcn_v2_5_resume,
2013        .is_idle = vcn_v2_5_is_idle,
2014        .wait_for_idle = vcn_v2_5_wait_for_idle,
 
 
 
 
2015        .set_clockgating_state = vcn_v2_5_set_clockgating_state,
2016        .set_powergating_state = vcn_v2_5_set_powergating_state,
2017	.dump_ip_state = vcn_v2_5_dump_ip_state,
2018	.print_ip_state = vcn_v2_5_print_ip_state,
2019};
2020
2021const struct amdgpu_ip_block_version vcn_v2_5_ip_block =
2022{
2023		.type = AMD_IP_BLOCK_TYPE_VCN,
2024		.major = 2,
2025		.minor = 5,
2026		.rev = 0,
2027		.funcs = &vcn_v2_5_ip_funcs,
2028};
2029
2030const struct amdgpu_ip_block_version vcn_v2_6_ip_block =
2031{
2032		.type = AMD_IP_BLOCK_TYPE_VCN,
2033		.major = 2,
2034		.minor = 6,
2035		.rev = 0,
2036		.funcs = &vcn_v2_6_ip_funcs,
2037};
2038
2039static uint32_t vcn_v2_6_query_poison_by_instance(struct amdgpu_device *adev,
2040			uint32_t instance, uint32_t sub_block)
2041{
2042	uint32_t poison_stat = 0, reg_value = 0;
2043
2044	switch (sub_block) {
2045	case AMDGPU_VCN_V2_6_VCPU_VCODEC:
2046		reg_value = RREG32_SOC15(VCN, instance, mmUVD_RAS_VCPU_VCODEC_STATUS);
2047		poison_stat = REG_GET_FIELD(reg_value, UVD_RAS_VCPU_VCODEC_STATUS, POISONED_PF);
2048		break;
2049	default:
2050		break;
2051	}
2052
2053	if (poison_stat)
2054		dev_info(adev->dev, "Poison detected in VCN%d, sub_block%d\n",
2055			instance, sub_block);
2056
2057	return poison_stat;
2058}
2059
2060static bool vcn_v2_6_query_poison_status(struct amdgpu_device *adev)
2061{
2062	uint32_t inst, sub;
2063	uint32_t poison_stat = 0;
2064
2065	for (inst = 0; inst < adev->vcn.num_vcn_inst; inst++)
2066		for (sub = 0; sub < AMDGPU_VCN_V2_6_MAX_SUB_BLOCK; sub++)
2067			poison_stat +=
2068			vcn_v2_6_query_poison_by_instance(adev, inst, sub);
2069
2070	return !!poison_stat;
2071}
2072
2073const struct amdgpu_ras_block_hw_ops vcn_v2_6_ras_hw_ops = {
2074	.query_poison_status = vcn_v2_6_query_poison_status,
2075};
2076
2077static struct amdgpu_vcn_ras vcn_v2_6_ras = {
2078	.ras_block = {
2079		.hw_ops = &vcn_v2_6_ras_hw_ops,
2080		.ras_late_init = amdgpu_vcn_ras_late_init,
2081	},
2082};
2083
2084static void vcn_v2_5_set_ras_funcs(struct amdgpu_device *adev)
2085{
2086	switch (amdgpu_ip_version(adev, VCN_HWIP, 0)) {
2087	case IP_VERSION(2, 6, 0):
2088		adev->vcn.ras = &vcn_v2_6_ras;
2089		break;
2090	default:
2091		break;
2092	}
 
 
2093}