vce_v3_0.c 25.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/*
 * Copyright 2014 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * Authors: Christian König <christian.koenig@amd.com>
 */

#include <linux/firmware.h>
#include <drm/drmP.h>
#include "amdgpu.h"
#include "amdgpu_vce.h"
#include "vid.h"
#include "vce/vce_3_0_d.h"
#include "vce/vce_3_0_sh_mask.h"
35 36
#include "oss/oss_3_0_d.h"
#include "oss/oss_3_0_sh_mask.h"
37
#include "gca/gfx_8_0_d.h"
38 39
#include "smu/smu_7_1_2_d.h"
#include "smu/smu_7_1_2_sh_mask.h"
40 41 42
#include "gca/gfx_8_0_d.h"
#include "gca/gfx_8_0_sh_mask.h"

43 44 45

#define GRBM_GFX_INDEX__VCE_INSTANCE__SHIFT	0x04
#define GRBM_GFX_INDEX__VCE_INSTANCE_MASK	0x10
46 47
#define GRBM_GFX_INDEX__VCE_ALL_PIPE		0x07

48 49 50
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR0	0x8616
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR1	0x8617
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR2	0x8618
51 52
#define mmGRBM_GFX_INDEX_DEFAULT 0xE0000000

J
jimqu 已提交
53
#define VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK	0x02
54

55 56 57 58
#define VCE_V3_0_FW_SIZE	(384 * 1024)
#define VCE_V3_0_STACK_SIZE	(64 * 1024)
#define VCE_V3_0_DATA_SIZE	((16 * 1024 * AMDGPU_MAX_VCE_HANDLES) + (52 * 1024))

59 60
#define FW_52_8_3	((52 << 24) | (8 << 16) | (3 << 8))

61 62 63
#define GET_VCE_INSTANCE(i)  ((i) << GRBM_GFX_INDEX__VCE_INSTANCE__SHIFT \
					| GRBM_GFX_INDEX__VCE_ALL_PIPE)

64
static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx);
65 66
static void vce_v3_0_set_ring_funcs(struct amdgpu_device *adev);
static void vce_v3_0_set_irq_funcs(struct amdgpu_device *adev);
J
jimqu 已提交
67
static int vce_v3_0_wait_for_idle(void *handle);
68 69 70 71 72 73 74 75

/**
 * vce_v3_0_ring_get_rptr - get read pointer
 *
 * @ring: amdgpu_ring pointer
 *
 * Returns the current hardware read pointer
 */
76
static uint64_t vce_v3_0_ring_get_rptr(struct amdgpu_ring *ring)
77 78 79 80 81
{
	struct amdgpu_device *adev = ring->adev;

	if (ring == &adev->vce.ring[0])
		return RREG32(mmVCE_RB_RPTR);
82
	else if (ring == &adev->vce.ring[1])
83
		return RREG32(mmVCE_RB_RPTR2);
84 85
	else
		return RREG32(mmVCE_RB_RPTR3);
86 87 88 89 90 91 92 93 94
}

/**
 * vce_v3_0_ring_get_wptr - get write pointer
 *
 * @ring: amdgpu_ring pointer
 *
 * Returns the current hardware write pointer
 */
95
static uint64_t vce_v3_0_ring_get_wptr(struct amdgpu_ring *ring)
96 97 98 99 100
{
	struct amdgpu_device *adev = ring->adev;

	if (ring == &adev->vce.ring[0])
		return RREG32(mmVCE_RB_WPTR);
101
	else if (ring == &adev->vce.ring[1])
102
		return RREG32(mmVCE_RB_WPTR2);
103 104
	else
		return RREG32(mmVCE_RB_WPTR3);
105 106 107 108 109 110 111 112 113 114 115 116 117 118
}

/**
 * vce_v3_0_ring_set_wptr - set write pointer
 *
 * @ring: amdgpu_ring pointer
 *
 * Commits the write pointer to the hardware
 */
static void vce_v3_0_ring_set_wptr(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;

	if (ring == &adev->vce.ring[0])
119
		WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
120
	else if (ring == &adev->vce.ring[1])
121
		WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
122
	else
123
		WREG32(mmVCE_RB_WPTR3, lower_32_bits(ring->wptr));
124 125
}

126 127
static void vce_v3_0_override_vce_clock_gating(struct amdgpu_device *adev, bool override)
{
128
	WREG32_FIELD(VCE_RB_ARB_CTRL, VCE_CGTT_OVERRIDE, override ? 1 : 0);
129 130 131 132 133
}

static void vce_v3_0_set_vce_sw_clock_gating(struct amdgpu_device *adev,
					     bool gated)
{
134
	u32 data;
135

136 137 138
	/* Set Override to disable Clock Gating */
	vce_v3_0_override_vce_clock_gating(adev, true);

139 140 141 142 143
	/* This function enables MGCG which is controlled by firmware.
	   With the clocks in the gated state the core is still
	   accessible but the firmware will throttle the clocks on the
	   fly as necessary.
	*/
144
	if (!gated) {
145
		data = RREG32(mmVCE_CLOCK_GATING_B);
146 147
		data |= 0x1ff;
		data &= ~0xef0000;
148
		WREG32(mmVCE_CLOCK_GATING_B, data);
149

150
		data = RREG32(mmVCE_UENC_CLOCK_GATING);
151 152
		data |= 0x3ff000;
		data &= ~0xffc00000;
153
		WREG32(mmVCE_UENC_CLOCK_GATING, data);
154

155
		data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
156
		data |= 0x2;
157
		data &= ~0x00010000;
158
		WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
159

160
		data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
161
		data |= 0x37f;
162
		WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
163

164
		data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
165
		data |= VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
166 167 168
			VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
			VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
			0x8;
169
		WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
170
	} else {
171
		data = RREG32(mmVCE_CLOCK_GATING_B);
172 173
		data &= ~0x80010;
		data |= 0xe70008;
174
		WREG32(mmVCE_CLOCK_GATING_B, data);
175

176
		data = RREG32(mmVCE_UENC_CLOCK_GATING);
177
		data |= 0xffc00000;
178
		WREG32(mmVCE_UENC_CLOCK_GATING, data);
179

180
		data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
181
		data |= 0x10000;
182
		WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
183

184
		data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
185
		data &= ~0x3ff;
186
		WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
187

188
		data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
189
		data &= ~(VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
190 191 192
			  VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
			  VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
			  0x8);
193
		WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
194 195 196 197
	}
	vce_v3_0_override_vce_clock_gating(adev, false);
}

J
jimqu 已提交
198 199 200 201 202 203
static int vce_v3_0_firmware_loaded(struct amdgpu_device *adev)
{
	int i, j;

	for (i = 0; i < 10; ++i) {
		for (j = 0; j < 100; ++j) {
J
jimqu 已提交
204 205
			uint32_t status = RREG32(mmVCE_STATUS);

J
jimqu 已提交
206 207 208 209 210 211
			if (status & VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK)
				return 0;
			mdelay(10);
		}

		DRM_ERROR("VCE not responding, trying to reset the ECPU!!!\n");
212
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
J
jimqu 已提交
213
		mdelay(10);
214
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
J
jimqu 已提交
215 216 217 218 219 220
		mdelay(10);
	}

	return -ETIMEDOUT;
}

221 222 223 224 225 226 227 228 229 230
/**
 * vce_v3_0_start - start VCE block
 *
 * @adev: amdgpu_device pointer
 *
 * Setup and start the VCE block
 */
static int vce_v3_0_start(struct amdgpu_device *adev)
{
	struct amdgpu_ring *ring;
J
jimqu 已提交
231 232 233
	int idx, r;

	ring = &adev->vce.ring[0];
234 235
	WREG32(mmVCE_RB_RPTR, lower_32_bits(ring->wptr));
	WREG32(mmVCE_RB_WPTR, lower_32_bits(ring->wptr));
J
jimqu 已提交
236 237 238 239 240
	WREG32(mmVCE_RB_BASE_LO, ring->gpu_addr);
	WREG32(mmVCE_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
	WREG32(mmVCE_RB_SIZE, ring->ring_size / 4);

	ring = &adev->vce.ring[1];
241 242
	WREG32(mmVCE_RB_RPTR2, lower_32_bits(ring->wptr));
	WREG32(mmVCE_RB_WPTR2, lower_32_bits(ring->wptr));
J
jimqu 已提交
243 244 245
	WREG32(mmVCE_RB_BASE_LO2, ring->gpu_addr);
	WREG32(mmVCE_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
	WREG32(mmVCE_RB_SIZE2, ring->ring_size / 4);
246

247
	ring = &adev->vce.ring[2];
248 249
	WREG32(mmVCE_RB_RPTR3, lower_32_bits(ring->wptr));
	WREG32(mmVCE_RB_WPTR3, lower_32_bits(ring->wptr));
250 251 252 253
	WREG32(mmVCE_RB_BASE_LO3, ring->gpu_addr);
	WREG32(mmVCE_RB_BASE_HI3, upper_32_bits(ring->gpu_addr));
	WREG32(mmVCE_RB_SIZE3, ring->ring_size / 4);

254 255
	mutex_lock(&adev->grbm_idx_mutex);
	for (idx = 0; idx < 2; ++idx) {
256 257 258
		if (adev->vce.harvest_config & (1 << idx))
			continue;

259
		WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(idx));
260
		vce_v3_0_mc_resume(adev, idx);
261
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 1);
J
jimqu 已提交
262

263 264 265
		if (adev->asic_type >= CHIP_STONEY)
			WREG32_P(mmVCE_VCPU_CNTL, 1, ~0x200001);
		else
266
			WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 1);
267

268
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
J
jimqu 已提交
269 270 271
		mdelay(100);

		r = vce_v3_0_firmware_loaded(adev);
272 273

		/* clear BUSY flag */
274
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 0);
275

276 277 278 279 280 281
		if (r) {
			DRM_ERROR("VCE not responding, giving up!!!\n");
			mutex_unlock(&adev->grbm_idx_mutex);
			return r;
		}
	}
282

283
	WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
284
	mutex_unlock(&adev->grbm_idx_mutex);
285

J
jimqu 已提交
286 287
	return 0;
}
288

J
jimqu 已提交
289 290 291 292 293 294 295 296 297
static int vce_v3_0_stop(struct amdgpu_device *adev)
{
	int idx;

	mutex_lock(&adev->grbm_idx_mutex);
	for (idx = 0; idx < 2; ++idx) {
		if (adev->vce.harvest_config & (1 << idx))
			continue;

298
		WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(idx));
J
jimqu 已提交
299 300 301 302

		if (adev->asic_type >= CHIP_STONEY)
			WREG32_P(mmVCE_VCPU_CNTL, 0, ~0x200001);
		else
303 304
			WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 0);

J
jimqu 已提交
305
		/* hold on ECPU */
306
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
J
jimqu 已提交
307 308

		/* clear BUSY flag */
309
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 0);
J
jimqu 已提交
310 311 312 313 314 315

		/* Set Clock-Gating off */
		if (adev->cg_flags & AMD_CG_SUPPORT_VCE_MGCG)
			vce_v3_0_set_vce_sw_clock_gating(adev, false);
	}

316
	WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
J
jimqu 已提交
317
	mutex_unlock(&adev->grbm_idx_mutex);
318 319 320 321

	return 0;
}

322 323 324 325 326 327 328 329
#define ixVCE_HARVEST_FUSE_MACRO__ADDRESS     0xC0014074
#define VCE_HARVEST_FUSE_MACRO__SHIFT       27
#define VCE_HARVEST_FUSE_MACRO__MASK        0x18000000

static unsigned vce_v3_0_get_harvest_config(struct amdgpu_device *adev)
{
	u32 tmp;

330
	/* Fiji, Stoney, Polaris10, Polaris11, Polaris12 are single pipe */
331
	if ((adev->asic_type == CHIP_FIJI) ||
332
	    (adev->asic_type == CHIP_STONEY) ||
333
	    (adev->asic_type == CHIP_POLARIS10) ||
334 335
	    (adev->asic_type == CHIP_POLARIS11) ||
	    (adev->asic_type == CHIP_POLARIS12))
336
		return AMDGPU_VCE_HARVEST_VCE1;
337 338

	/* Tonga and CZ are dual or single pipe */
339
	if (adev->flags & AMD_IS_APU)
340 341 342 343 344 345 346 347 348 349
		tmp = (RREG32_SMC(ixVCE_HARVEST_FUSE_MACRO__ADDRESS) &
		       VCE_HARVEST_FUSE_MACRO__MASK) >>
			VCE_HARVEST_FUSE_MACRO__SHIFT;
	else
		tmp = (RREG32_SMC(ixCC_HARVEST_FUSES) &
		       CC_HARVEST_FUSES__VCE_DISABLE_MASK) >>
			CC_HARVEST_FUSES__VCE_DISABLE__SHIFT;

	switch (tmp) {
	case 1:
350
		return AMDGPU_VCE_HARVEST_VCE0;
351
	case 2:
352
		return AMDGPU_VCE_HARVEST_VCE1;
353
	case 3:
354
		return AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1;
355
	default:
356
		return 0;
357 358 359
	}
}

360
static int vce_v3_0_early_init(void *handle)
361
{
362 363
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

364 365 366 367 368 369 370
	adev->vce.harvest_config = vce_v3_0_get_harvest_config(adev);

	if ((adev->vce.harvest_config &
	     (AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1)) ==
	    (AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1))
		return -ENOENT;

371
	adev->vce.num_rings = 3;
372

373 374 375 376 377 378
	vce_v3_0_set_ring_funcs(adev);
	vce_v3_0_set_irq_funcs(adev);

	return 0;
}

379
static int vce_v3_0_sw_init(void *handle)
380
{
381
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
382
	struct amdgpu_ring *ring;
383
	int r, i;
384 385

	/* VCE */
386
	r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 167, &adev->vce.irq);
387 388 389
	if (r)
		return r;

390 391
	r = amdgpu_vce_sw_init(adev, VCE_V3_0_FW_SIZE +
		(VCE_V3_0_STACK_SIZE + VCE_V3_0_DATA_SIZE) * 2);
392 393 394
	if (r)
		return r;

395 396 397
	/* 52.8.3 required for 3 ring support */
	if (adev->vce.fw_version < FW_52_8_3)
		adev->vce.num_rings = 2;
398 399 400 401 402

	r = amdgpu_vce_resume(adev);
	if (r)
		return r;

403 404 405
	for (i = 0; i < adev->vce.num_rings; i++) {
		ring = &adev->vce.ring[i];
		sprintf(ring->name, "vce%d", i);
406
		r = amdgpu_ring_init(adev, ring, 512, &adev->vce.irq, 0);
407 408 409
		if (r)
			return r;
	}
410 411 412 413

	return r;
}

414
static int vce_v3_0_sw_fini(void *handle)
415 416
{
	int r;
417
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
418 419 420 421 422 423 424 425 426 427 428 429

	r = amdgpu_vce_suspend(adev);
	if (r)
		return r;

	r = amdgpu_vce_sw_fini(adev);
	if (r)
		return r;

	return r;
}

430
static int vce_v3_0_hw_init(void *handle)
431
{
432
	int r, i;
433
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
434

435 436 437
	vce_v3_0_override_vce_clock_gating(adev, true);
	if (!(adev->flags & AMD_IS_APU))
		amdgpu_asic_set_vce_clocks(adev, 10000, 10000);
438

439 440
	for (i = 0; i < adev->vce.num_rings; i++)
		adev->vce.ring[i].ready = false;
441

442
	for (i = 0; i < adev->vce.num_rings; i++) {
443 444 445 446 447
		r = amdgpu_ring_test_ring(&adev->vce.ring[i]);
		if (r)
			return r;
		else
			adev->vce.ring[i].ready = true;
448 449 450 451 452 453 454
	}

	DRM_INFO("VCE initialized successfully.\n");

	return 0;
}

455
static int vce_v3_0_hw_fini(void *handle)
456
{
J
jimqu 已提交
457 458 459 460 461 462 463 464
	int r;
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

	r = vce_v3_0_wait_for_idle(handle);
	if (r)
		return r;

	return vce_v3_0_stop(adev);
465 466
}

467
static int vce_v3_0_suspend(void *handle)
468 469
{
	int r;
470
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
471 472 473 474 475 476 477 478 479 480 481 482

	r = vce_v3_0_hw_fini(adev);
	if (r)
		return r;

	r = amdgpu_vce_suspend(adev);
	if (r)
		return r;

	return r;
}

483
static int vce_v3_0_resume(void *handle)
484 485
{
	int r;
486
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
487 488 489 490 491 492 493 494 495 496 497 498

	r = amdgpu_vce_resume(adev);
	if (r)
		return r;

	r = vce_v3_0_hw_init(adev);
	if (r)
		return r;

	return r;
}

499
static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx)
500 501 502 503 504 505
{
	uint32_t offset, size;

	WREG32_P(mmVCE_CLOCK_GATING_A, 0, ~(1 << 16));
	WREG32_P(mmVCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
	WREG32_P(mmVCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
506
	WREG32(mmVCE_CLOCK_GATING_B, 0x1FF);
507 508 509 510 511 512

	WREG32(mmVCE_LMI_CTRL, 0x00398000);
	WREG32_P(mmVCE_LMI_CACHE_CTRL, 0x0, ~0x1);
	WREG32(mmVCE_LMI_SWAP_CNTL, 0);
	WREG32(mmVCE_LMI_SWAP_CNTL1, 0);
	WREG32(mmVCE_LMI_VM_CTRL, 0);
513 514
	WREG32_OR(mmVCE_VCPU_CNTL, 0x00100000);

515 516 517 518 519 520
	if (adev->asic_type >= CHIP_STONEY) {
		WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR0, (adev->vce.gpu_addr >> 8));
		WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR1, (adev->vce.gpu_addr >> 8));
		WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR2, (adev->vce.gpu_addr >> 8));
	} else
		WREG32(mmVCE_LMI_VCPU_CACHE_40BIT_BAR, (adev->vce.gpu_addr >> 8));
521
	offset = AMDGPU_VCE_FIRMWARE_OFFSET;
522
	size = VCE_V3_0_FW_SIZE;
523 524 525
	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
	WREG32(mmVCE_VCPU_CACHE_SIZE0, size);

526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
	if (idx == 0) {
		offset += size;
		size = VCE_V3_0_STACK_SIZE;
		WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0x7fffffff);
		WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
		offset += size;
		size = VCE_V3_0_DATA_SIZE;
		WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0x7fffffff);
		WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
	} else {
		offset += size + VCE_V3_0_STACK_SIZE + VCE_V3_0_DATA_SIZE;
		size = VCE_V3_0_STACK_SIZE;
		WREG32(mmVCE_VCPU_CACHE_OFFSET1, offset & 0xfffffff);
		WREG32(mmVCE_VCPU_CACHE_SIZE1, size);
		offset += size;
		size = VCE_V3_0_DATA_SIZE;
		WREG32(mmVCE_VCPU_CACHE_OFFSET2, offset & 0xfffffff);
		WREG32(mmVCE_VCPU_CACHE_SIZE2, size);
	}
545 546

	WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
547
	WREG32_FIELD(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN, 1);
548 549
}

550
static bool vce_v3_0_is_idle(void *handle)
551
{
552
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
553 554
	u32 mask = 0;

555 556
	mask |= (adev->vce.harvest_config & AMDGPU_VCE_HARVEST_VCE0) ? 0 : SRBM_STATUS2__VCE0_BUSY_MASK;
	mask |= (adev->vce.harvest_config & AMDGPU_VCE_HARVEST_VCE1) ? 0 : SRBM_STATUS2__VCE1_BUSY_MASK;
557 558

	return !(RREG32(mmSRBM_STATUS2) & mask);
559 560
}

561
static int vce_v3_0_wait_for_idle(void *handle)
562 563
{
	unsigned i;
564
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
565

566 567
	for (i = 0; i < adev->usec_timeout; i++)
		if (vce_v3_0_is_idle(handle))
568
			return 0;
569

570 571 572
	return -ETIMEDOUT;
}

573 574 575 576 577
#define  VCE_STATUS_VCPU_REPORT_AUTO_BUSY_MASK  0x00000008L   /* AUTO_BUSY */
#define  VCE_STATUS_VCPU_REPORT_RB0_BUSY_MASK   0x00000010L   /* RB0_BUSY */
#define  VCE_STATUS_VCPU_REPORT_RB1_BUSY_MASK   0x00000020L   /* RB1_BUSY */
#define  AMDGPU_VCE_STATUS_BUSY_MASK (VCE_STATUS_VCPU_REPORT_AUTO_BUSY_MASK | \
				      VCE_STATUS_VCPU_REPORT_RB0_BUSY_MASK)
578

579
static bool vce_v3_0_check_soft_reset(void *handle)
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
	u32 srbm_soft_reset = 0;

	/* According to VCE team , we should use VCE_STATUS instead
	 * SRBM_STATUS.VCE_BUSY bit for busy status checking.
	 * GRBM_GFX_INDEX.INSTANCE_INDEX is used to specify which VCE
	 * instance's registers are accessed
	 * (0 for 1st instance, 10 for 2nd instance).
	 *
	 *VCE_STATUS
	 *|UENC|ACPI|AUTO ACTIVE|RB1 |RB0 |RB2 |          |FW_LOADED|JOB |
	 *|----+----+-----------+----+----+----+----------+---------+----|
	 *|bit8|bit7|    bit6   |bit5|bit4|bit3|   bit2   |  bit1   |bit0|
	 *
	 * VCE team suggest use bit 3--bit 6 for busy status check
	 */
597
	mutex_lock(&adev->grbm_idx_mutex);
598
	WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
599 600 601 602
	if (RREG32(mmVCE_STATUS) & AMDGPU_VCE_STATUS_BUSY_MASK) {
		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE0, 1);
		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE1, 1);
	}
603
	WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(1));
604 605 606 607
	if (RREG32(mmVCE_STATUS) & AMDGPU_VCE_STATUS_BUSY_MASK) {
		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE0, 1);
		srbm_soft_reset = REG_SET_FIELD(srbm_soft_reset, SRBM_SOFT_RESET, SOFT_RESET_VCE1, 1);
	}
608
	WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(0));
609
	mutex_unlock(&adev->grbm_idx_mutex);
610 611 612

	if (srbm_soft_reset) {
		adev->vce.srbm_soft_reset = srbm_soft_reset;
613
		return true;
614 615
	} else {
		adev->vce.srbm_soft_reset = 0;
616
		return false;
617 618 619
	}
}

620
static int vce_v3_0_soft_reset(void *handle)
621
{
622
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
623 624
	u32 srbm_soft_reset;

625
	if (!adev->vce.srbm_soft_reset)
626 627 628 629 630
		return 0;
	srbm_soft_reset = adev->vce.srbm_soft_reset;

	if (srbm_soft_reset) {
		u32 tmp;
631

632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
		tmp = RREG32(mmSRBM_SOFT_RESET);
		tmp |= srbm_soft_reset;
		dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
		WREG32(mmSRBM_SOFT_RESET, tmp);
		tmp = RREG32(mmSRBM_SOFT_RESET);

		udelay(50);

		tmp &= ~srbm_soft_reset;
		WREG32(mmSRBM_SOFT_RESET, tmp);
		tmp = RREG32(mmSRBM_SOFT_RESET);

		/* Wait a little for things to settle down */
		udelay(50);
	}

	return 0;
}

static int vce_v3_0_pre_soft_reset(void *handle)
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

655
	if (!adev->vce.srbm_soft_reset)
656 657 658 659 660 661 662 663 664 665 666 667
		return 0;

	mdelay(5);

	return vce_v3_0_suspend(adev);
}


static int vce_v3_0_post_soft_reset(void *handle)
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

668
	if (!adev->vce.srbm_soft_reset)
669
		return 0;
670

671 672
	mdelay(5);

673
	return vce_v3_0_resume(adev);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
}

static int vce_v3_0_set_interrupt_state(struct amdgpu_device *adev,
					struct amdgpu_irq_src *source,
					unsigned type,
					enum amdgpu_interrupt_state state)
{
	uint32_t val = 0;

	if (state == AMDGPU_IRQ_STATE_ENABLE)
		val |= VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK;

	WREG32_P(mmVCE_SYS_INT_EN, val, ~VCE_SYS_INT_EN__VCE_SYS_INT_TRAP_INTERRUPT_EN_MASK);
	return 0;
}

static int vce_v3_0_process_interrupt(struct amdgpu_device *adev,
				      struct amdgpu_irq_src *source,
				      struct amdgpu_iv_entry *entry)
{
	DRM_DEBUG("IH: VCE\n");
695

696
	WREG32_FIELD(VCE_SYS_INT_STATUS, VCE_SYS_INT_TRAP_INTERRUPT_INT, 1);
697

698
	switch (entry->src_data[0]) {
699 700
	case 0:
	case 1:
701
	case 2:
702
		amdgpu_fence_process(&adev->vce.ring[entry->src_data[0]]);
703 704 705
		break;
	default:
		DRM_ERROR("Unhandled interrupt: %d %d\n",
706
			  entry->src_id, entry->src_data[0]);
707 708 709 710 711 712
		break;
	}

	return 0;
}

713 714
static int vce_v3_0_set_clockgating_state(void *handle,
					  enum amd_clockgating_state state)
715
{
716 717 718 719
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
	bool enable = (state == AMD_CG_STATE_GATE) ? true : false;
	int i;

720
	if (!(adev->cg_flags & AMD_CG_SUPPORT_VCE_MGCG))
721 722 723 724 725 726 727 728
		return 0;

	mutex_lock(&adev->grbm_idx_mutex);
	for (i = 0; i < 2; i++) {
		/* Program VCE Instance 0 or 1 if not harvested */
		if (adev->vce.harvest_config & (1 << i))
			continue;

729
		WREG32(mmGRBM_GFX_INDEX, GET_VCE_INSTANCE(i));
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

		if (enable) {
			/* initialize VCE_CLOCK_GATING_A: Clock ON/OFF delay */
			uint32_t data = RREG32(mmVCE_CLOCK_GATING_A);
			data &= ~(0xf | 0xff0);
			data |= ((0x0 << 0) | (0x04 << 4));
			WREG32(mmVCE_CLOCK_GATING_A, data);

			/* initialize VCE_UENC_CLOCK_GATING: Clock ON/OFF delay */
			data = RREG32(mmVCE_UENC_CLOCK_GATING);
			data &= ~(0xf | 0xff0);
			data |= ((0x0 << 0) | (0x04 << 4));
			WREG32(mmVCE_UENC_CLOCK_GATING, data);
		}

		vce_v3_0_set_vce_sw_clock_gating(adev, enable);
	}

748
	WREG32(mmGRBM_GFX_INDEX, mmGRBM_GFX_INDEX_DEFAULT);
749 750
	mutex_unlock(&adev->grbm_idx_mutex);

751 752 753
	return 0;
}

754 755
static int vce_v3_0_set_powergating_state(void *handle,
					  enum amd_powergating_state state)
756 757 758 759 760 761 762 763
{
	/* This doesn't actually powergate the VCE block.
	 * That's done in the dpm code via the SMC.  This
	 * just re-inits the block as necessary.  The actual
	 * gating still happens in the dpm code.  We should
	 * revisit this when there is a cleaner line between
	 * the smc and the hw blocks
	 */
764
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
765
	int ret = 0;
766

767
	if (state == AMD_PG_STATE_GATE) {
768 769 770
		ret = vce_v3_0_stop(adev);
		if (ret)
			goto out;
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
	} else {
		ret = vce_v3_0_start(adev);
		if (ret)
			goto out;
	}

out:
	return ret;
}

static void vce_v3_0_get_clockgating_state(void *handle, u32 *flags)
{
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
	int data;

	mutex_lock(&adev->pm.mutex);

788 789
	if (RREG32_SMC(ixCURRENT_PG_STATUS) &
			CURRENT_PG_STATUS__VCE_PG_STATUS_MASK) {
790 791 792 793 794 795 796 797 798 799 800 801 802
		DRM_INFO("Cannot get clockgating state when VCE is powergated.\n");
		goto out;
	}

	WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, 0);

	/* AMD_CG_SUPPORT_VCE_MGCG */
	data = RREG32(mmVCE_CLOCK_GATING_A);
	if (data & (0x04 << 4))
		*flags |= AMD_CG_SUPPORT_VCE_MGCG;

out:
	mutex_unlock(&adev->pm.mutex);
803 804
}

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
static void vce_v3_0_ring_emit_ib(struct amdgpu_ring *ring,
		struct amdgpu_ib *ib, unsigned int vm_id, bool ctx_switch)
{
	amdgpu_ring_write(ring, VCE_CMD_IB_VM);
	amdgpu_ring_write(ring, vm_id);
	amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
	amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
	amdgpu_ring_write(ring, ib->length_dw);
}

static void vce_v3_0_emit_vm_flush(struct amdgpu_ring *ring,
			 unsigned int vm_id, uint64_t pd_addr)
{
	amdgpu_ring_write(ring, VCE_CMD_UPDATE_PTB);
	amdgpu_ring_write(ring, vm_id);
	amdgpu_ring_write(ring, pd_addr >> 12);

	amdgpu_ring_write(ring, VCE_CMD_FLUSH_TLB);
	amdgpu_ring_write(ring, vm_id);
	amdgpu_ring_write(ring, VCE_CMD_END);
}

static void vce_v3_0_emit_pipeline_sync(struct amdgpu_ring *ring)
{
	uint32_t seq = ring->fence_drv.sync_seq;
	uint64_t addr = ring->fence_drv.gpu_addr;

	amdgpu_ring_write(ring, VCE_CMD_WAIT_GE);
	amdgpu_ring_write(ring, lower_32_bits(addr));
	amdgpu_ring_write(ring, upper_32_bits(addr));
	amdgpu_ring_write(ring, seq);
}

838
static const struct amd_ip_funcs vce_v3_0_ip_funcs = {
839
	.name = "vce_v3_0",
840 841 842 843 844 845 846 847 848 849
	.early_init = vce_v3_0_early_init,
	.late_init = NULL,
	.sw_init = vce_v3_0_sw_init,
	.sw_fini = vce_v3_0_sw_fini,
	.hw_init = vce_v3_0_hw_init,
	.hw_fini = vce_v3_0_hw_fini,
	.suspend = vce_v3_0_suspend,
	.resume = vce_v3_0_resume,
	.is_idle = vce_v3_0_is_idle,
	.wait_for_idle = vce_v3_0_wait_for_idle,
850 851
	.check_soft_reset = vce_v3_0_check_soft_reset,
	.pre_soft_reset = vce_v3_0_pre_soft_reset,
852
	.soft_reset = vce_v3_0_soft_reset,
853
	.post_soft_reset = vce_v3_0_post_soft_reset,
854 855
	.set_clockgating_state = vce_v3_0_set_clockgating_state,
	.set_powergating_state = vce_v3_0_set_powergating_state,
856
	.get_clockgating_state = vce_v3_0_get_clockgating_state,
857 858
};

859
static const struct amdgpu_ring_funcs vce_v3_0_ring_phys_funcs = {
860
	.type = AMDGPU_RING_TYPE_VCE,
861 862
	.align_mask = 0xf,
	.nop = VCE_CMD_NO_OP,
863
	.support_64bit_ptrs = false,
864 865 866 867
	.get_rptr = vce_v3_0_ring_get_rptr,
	.get_wptr = vce_v3_0_ring_get_wptr,
	.set_wptr = vce_v3_0_ring_set_wptr,
	.parse_cs = amdgpu_vce_ring_parse_cs,
868 869 870 871
	.emit_frame_size =
		4 + /* vce_v3_0_emit_pipeline_sync */
		6, /* amdgpu_vce_ring_emit_fence x1 no user fence */
	.emit_ib_size = 5, /* vce_v3_0_ring_emit_ib */
872 873 874 875
	.emit_ib = amdgpu_vce_ring_emit_ib,
	.emit_fence = amdgpu_vce_ring_emit_fence,
	.test_ring = amdgpu_vce_ring_test_ring,
	.test_ib = amdgpu_vce_ring_test_ib,
876
	.insert_nop = amdgpu_ring_insert_nop,
877
	.pad_ib = amdgpu_ring_generic_pad_ib,
878 879
	.begin_use = amdgpu_vce_ring_begin_use,
	.end_use = amdgpu_vce_ring_end_use,
880 881
};

882
static const struct amdgpu_ring_funcs vce_v3_0_ring_vm_funcs = {
883
	.type = AMDGPU_RING_TYPE_VCE,
884 885
	.align_mask = 0xf,
	.nop = VCE_CMD_NO_OP,
886
	.support_64bit_ptrs = false,
887 888 889
	.get_rptr = vce_v3_0_ring_get_rptr,
	.get_wptr = vce_v3_0_ring_get_wptr,
	.set_wptr = vce_v3_0_ring_set_wptr,
890
	.parse_cs = amdgpu_vce_ring_parse_cs_vm,
891 892 893 894 895
	.emit_frame_size =
		6 + /* vce_v3_0_emit_vm_flush */
		4 + /* vce_v3_0_emit_pipeline_sync */
		6 + 6, /* amdgpu_vce_ring_emit_fence x2 vm fence */
	.emit_ib_size = 4, /* amdgpu_vce_ring_emit_ib */
896 897 898 899 900 901 902 903 904 905 906 907
	.emit_ib = vce_v3_0_ring_emit_ib,
	.emit_vm_flush = vce_v3_0_emit_vm_flush,
	.emit_pipeline_sync = vce_v3_0_emit_pipeline_sync,
	.emit_fence = amdgpu_vce_ring_emit_fence,
	.test_ring = amdgpu_vce_ring_test_ring,
	.test_ib = amdgpu_vce_ring_test_ib,
	.insert_nop = amdgpu_ring_insert_nop,
	.pad_ib = amdgpu_ring_generic_pad_ib,
	.begin_use = amdgpu_vce_ring_begin_use,
	.end_use = amdgpu_vce_ring_end_use,
};

908 909
static void vce_v3_0_set_ring_funcs(struct amdgpu_device *adev)
{
910 911
	int i;

912 913 914 915 916 917 918 919 920
	if (adev->asic_type >= CHIP_STONEY) {
		for (i = 0; i < adev->vce.num_rings; i++)
			adev->vce.ring[i].funcs = &vce_v3_0_ring_vm_funcs;
		DRM_INFO("VCE enabled in VM mode\n");
	} else {
		for (i = 0; i < adev->vce.num_rings; i++)
			adev->vce.ring[i].funcs = &vce_v3_0_ring_phys_funcs;
		DRM_INFO("VCE enabled in physical mode\n");
	}
921 922 923 924 925 926 927 928 929 930 931 932
}

static const struct amdgpu_irq_src_funcs vce_v3_0_irq_funcs = {
	.set = vce_v3_0_set_interrupt_state,
	.process = vce_v3_0_process_interrupt,
};

static void vce_v3_0_set_irq_funcs(struct amdgpu_device *adev)
{
	adev->vce.irq.num_types = 1;
	adev->vce.irq.funcs = &vce_v3_0_irq_funcs;
};
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959

const struct amdgpu_ip_block_version vce_v3_0_ip_block =
{
	.type = AMD_IP_BLOCK_TYPE_VCE,
	.major = 3,
	.minor = 0,
	.rev = 0,
	.funcs = &vce_v3_0_ip_funcs,
};

const struct amdgpu_ip_block_version vce_v3_1_ip_block =
{
	.type = AMD_IP_BLOCK_TYPE_VCE,
	.major = 3,
	.minor = 1,
	.rev = 0,
	.funcs = &vce_v3_0_ip_funcs,
};

const struct amdgpu_ip_block_version vce_v3_4_ip_block =
{
	.type = AMD_IP_BLOCK_TYPE_VCE,
	.major = 3,
	.minor = 4,
	.rev = 0,
	.funcs = &vce_v3_0_ip_funcs,
};