vce_v3_0.c 24.7 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 48
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR0	0x8616
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR1	0x8617
#define mmVCE_LMI_VCPU_CACHE_40BIT_BAR2	0x8618
J
jimqu 已提交
49
#define VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK	0x02
50

51 52 53 54
#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))

55 56
#define FW_52_8_3	((52 << 24) | (8 << 16) | (3 << 8))

57
static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx);
58 59
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 已提交
60
static int vce_v3_0_wait_for_idle(void *handle);
61 62 63 64 65 66 67 68 69 70 71 72 73 74

/**
 * vce_v3_0_ring_get_rptr - get read pointer
 *
 * @ring: amdgpu_ring pointer
 *
 * Returns the current hardware read pointer
 */
static uint32_t vce_v3_0_ring_get_rptr(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;

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

/**
 * vce_v3_0_ring_get_wptr - get write pointer
 *
 * @ring: amdgpu_ring pointer
 *
 * Returns the current hardware write pointer
 */
static uint32_t vce_v3_0_ring_get_wptr(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;

	if (ring == &adev->vce.ring[0])
		return RREG32(mmVCE_RB_WPTR);
94
	else if (ring == &adev->vce.ring[1])
95
		return RREG32(mmVCE_RB_WPTR2);
96 97
	else
		return RREG32(mmVCE_RB_WPTR3);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
}

/**
 * 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])
		WREG32(mmVCE_RB_WPTR, ring->wptr);
113
	else if (ring == &adev->vce.ring[1])
114
		WREG32(mmVCE_RB_WPTR2, ring->wptr);
115 116
	else
		WREG32(mmVCE_RB_WPTR3, ring->wptr);
117 118
}

119 120
static void vce_v3_0_override_vce_clock_gating(struct amdgpu_device *adev, bool override)
{
121
	WREG32_FIELD(VCE_RB_ARB_CTRL, VCE_CGTT_OVERRIDE, override ? 1 : 0);
122 123 124 125 126
}

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

129 130 131
	/* Set Override to disable Clock Gating */
	vce_v3_0_override_vce_clock_gating(adev, true);

132 133 134 135 136
	/* 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.
	*/
137
	if (!gated) {
138
		data = RREG32(mmVCE_CLOCK_GATING_B);
139 140
		data |= 0x1ff;
		data &= ~0xef0000;
141
		WREG32(mmVCE_CLOCK_GATING_B, data);
142

143
		data = RREG32(mmVCE_UENC_CLOCK_GATING);
144 145
		data |= 0x3ff000;
		data &= ~0xffc00000;
146
		WREG32(mmVCE_UENC_CLOCK_GATING, data);
147

148
		data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
149
		data |= 0x2;
150
		data &= ~0x00010000;
151
		WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
152

153
		data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
154
		data |= 0x37f;
155
		WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
156

157
		data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
158
		data |= VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
159 160 161
			VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
			VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
			0x8;
162
		WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
163
	} else {
164
		data = RREG32(mmVCE_CLOCK_GATING_B);
165 166
		data &= ~0x80010;
		data |= 0xe70008;
167
		WREG32(mmVCE_CLOCK_GATING_B, data);
168

169
		data = RREG32(mmVCE_UENC_CLOCK_GATING);
170
		data |= 0xffc00000;
171
		WREG32(mmVCE_UENC_CLOCK_GATING, data);
172

173
		data = RREG32(mmVCE_UENC_CLOCK_GATING_2);
174
		data |= 0x10000;
175
		WREG32(mmVCE_UENC_CLOCK_GATING_2, data);
176

177
		data = RREG32(mmVCE_UENC_REG_CLOCK_GATING);
178
		data &= ~0x3ff;
179
		WREG32(mmVCE_UENC_REG_CLOCK_GATING, data);
180

181
		data = RREG32(mmVCE_UENC_DMA_DCLK_CTRL);
182
		data &= ~(VCE_UENC_DMA_DCLK_CTRL__WRDMCLK_FORCEON_MASK |
183 184 185
			  VCE_UENC_DMA_DCLK_CTRL__RDDMCLK_FORCEON_MASK |
			  VCE_UENC_DMA_DCLK_CTRL__REGCLK_FORCEON_MASK  |
			  0x8);
186
		WREG32(mmVCE_UENC_DMA_DCLK_CTRL, data);
187 188 189 190
	}
	vce_v3_0_override_vce_clock_gating(adev, false);
}

J
jimqu 已提交
191 192 193 194 195 196
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 已提交
197 198
			uint32_t status = RREG32(mmVCE_STATUS);

J
jimqu 已提交
199 200 201 202 203 204
			if (status & VCE_STATUS_VCPU_REPORT_FW_LOADED_MASK)
				return 0;
			mdelay(10);
		}

		DRM_ERROR("VCE not responding, trying to reset the ECPU!!!\n");
205
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
J
jimqu 已提交
206
		mdelay(10);
207
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
J
jimqu 已提交
208 209 210 211 212 213
		mdelay(10);
	}

	return -ETIMEDOUT;
}

214 215 216 217 218 219 220 221 222 223
/**
 * 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 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
	int idx, r;

	ring = &adev->vce.ring[0];
	WREG32(mmVCE_RB_RPTR, ring->wptr);
	WREG32(mmVCE_RB_WPTR, ring->wptr);
	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];
	WREG32(mmVCE_RB_RPTR2, ring->wptr);
	WREG32(mmVCE_RB_WPTR2, ring->wptr);
	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);
239

240 241 242 243 244 245 246
	ring = &adev->vce.ring[2];
	WREG32(mmVCE_RB_RPTR3, ring->wptr);
	WREG32(mmVCE_RB_WPTR3, ring->wptr);
	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);

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

252
		WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, idx);
253
		vce_v3_0_mc_resume(adev, idx);
254
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 1);
J
jimqu 已提交
255

256 257 258
		if (adev->asic_type >= CHIP_STONEY)
			WREG32_P(mmVCE_VCPU_CNTL, 1, ~0x200001);
		else
259
			WREG32_FIELD(VCE_VCPU_CNTL, CLK_EN, 1);
260

261
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 0);
J
jimqu 已提交
262 263 264
		mdelay(100);

		r = vce_v3_0_firmware_loaded(adev);
265 266

		/* clear BUSY flag */
267
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 0);
268

269 270 271 272 273 274
		if (r) {
			DRM_ERROR("VCE not responding, giving up!!!\n");
			mutex_unlock(&adev->grbm_idx_mutex);
			return r;
		}
	}
275

276
	WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, 0);
277
	mutex_unlock(&adev->grbm_idx_mutex);
278

J
jimqu 已提交
279 280
	return 0;
}
281

J
jimqu 已提交
282 283 284 285 286 287 288 289 290
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;

291
		WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, idx);
J
jimqu 已提交
292 293 294 295

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

J
jimqu 已提交
298
		/* hold on ECPU */
299
		WREG32_FIELD(VCE_SOFT_RESET, ECPU_SOFT_RESET, 1);
J
jimqu 已提交
300 301

		/* clear BUSY flag */
302
		WREG32_FIELD(VCE_STATUS, JOB_BUSY, 0);
J
jimqu 已提交
303 304 305 306 307 308

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

309
	WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, 0);
J
jimqu 已提交
310
	mutex_unlock(&adev->grbm_idx_mutex);
311 312 313 314

	return 0;
}

315 316 317 318 319 320 321 322
#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;

323
	/* Fiji, Stoney, Polaris10, Polaris11, Polaris12 are single pipe */
324
	if ((adev->asic_type == CHIP_FIJI) ||
325
	    (adev->asic_type == CHIP_STONEY) ||
326
	    (adev->asic_type == CHIP_POLARIS10) ||
327 328
	    (adev->asic_type == CHIP_POLARIS11) ||
	    (adev->asic_type == CHIP_POLARIS12))
329
		return AMDGPU_VCE_HARVEST_VCE1;
330 331

	/* Tonga and CZ are dual or single pipe */
332
	if (adev->flags & AMD_IS_APU)
333 334 335 336 337 338 339 340 341 342
		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:
343
		return AMDGPU_VCE_HARVEST_VCE0;
344
	case 2:
345
		return AMDGPU_VCE_HARVEST_VCE1;
346
	case 3:
347
		return AMDGPU_VCE_HARVEST_VCE0 | AMDGPU_VCE_HARVEST_VCE1;
348
	default:
349
		return 0;
350 351 352
	}
}

353
static int vce_v3_0_early_init(void *handle)
354
{
355 356
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

357 358 359 360 361 362 363
	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;

364
	adev->vce.num_rings = 3;
365

366 367 368 369 370 371
	vce_v3_0_set_ring_funcs(adev);
	vce_v3_0_set_irq_funcs(adev);

	return 0;
}

372
static int vce_v3_0_sw_init(void *handle)
373
{
374
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
375
	struct amdgpu_ring *ring;
376
	int r, i;
377 378 379 380 381 382

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

383 384
	r = amdgpu_vce_sw_init(adev, VCE_V3_0_FW_SIZE +
		(VCE_V3_0_STACK_SIZE + VCE_V3_0_DATA_SIZE) * 2);
385 386 387
	if (r)
		return r;

388 389 390
	/* 52.8.3 required for 3 ring support */
	if (adev->vce.fw_version < FW_52_8_3)
		adev->vce.num_rings = 2;
391 392 393 394 395

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

396 397 398
	for (i = 0; i < adev->vce.num_rings; i++) {
		ring = &adev->vce.ring[i];
		sprintf(ring->name, "vce%d", i);
399
		r = amdgpu_ring_init(adev, ring, 512, &adev->vce.irq, 0);
400 401 402
		if (r)
			return r;
	}
403 404 405 406

	return r;
}

407
static int vce_v3_0_sw_fini(void *handle)
408 409
{
	int r;
410
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
411 412 413 414 415 416 417 418 419 420 421 422

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

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

	return r;
}

423
static int vce_v3_0_hw_init(void *handle)
424
{
425
	int r, i;
426
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
427 428 429 430 431

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

432 433
	for (i = 0; i < adev->vce.num_rings; i++)
		adev->vce.ring[i].ready = false;
434

435
	for (i = 0; i < adev->vce.num_rings; i++) {
436 437 438 439 440
		r = amdgpu_ring_test_ring(&adev->vce.ring[i]);
		if (r)
			return r;
		else
			adev->vce.ring[i].ready = true;
441 442 443 444 445 446 447
	}

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

	return 0;
}

448
static int vce_v3_0_hw_fini(void *handle)
449
{
J
jimqu 已提交
450 451 452 453 454 455 456 457
	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);
458 459
}

460
static int vce_v3_0_suspend(void *handle)
461 462
{
	int r;
463
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
464 465 466 467 468 469 470 471 472 473 474 475

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

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

	return r;
}

476
static int vce_v3_0_resume(void *handle)
477 478
{
	int r;
479
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
480 481 482 483 484 485 486 487 488 489 490 491

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

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

	return r;
}

492
static void vce_v3_0_mc_resume(struct amdgpu_device *adev, int idx)
493 494 495 496 497 498
{
	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);
499
	WREG32(mmVCE_CLOCK_GATING_B, 0x1FF);
500 501 502 503 504 505

	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);
506 507 508 509 510 511
	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));
512
	offset = AMDGPU_VCE_FIRMWARE_OFFSET;
513
	size = VCE_V3_0_FW_SIZE;
514 515 516
	WREG32(mmVCE_VCPU_CACHE_OFFSET0, offset & 0x7fffffff);
	WREG32(mmVCE_VCPU_CACHE_SIZE0, size);

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	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);
	}
536 537

	WREG32_P(mmVCE_LMI_CTRL2, 0x0, ~0x100);
538
	WREG32_FIELD(VCE_SYS_INT_EN, VCE_SYS_INT_TRAP_INTERRUPT_EN, 1);
539 540
}

541
static bool vce_v3_0_is_idle(void *handle)
542
{
543
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
544 545
	u32 mask = 0;

546 547
	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;
548 549

	return !(RREG32(mmSRBM_STATUS2) & mask);
550 551
}

552
static int vce_v3_0_wait_for_idle(void *handle)
553 554
{
	unsigned i;
555
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
556

557 558
	for (i = 0; i < adev->usec_timeout; i++)
		if (vce_v3_0_is_idle(handle))
559
			return 0;
560

561 562 563
	return -ETIMEDOUT;
}

564 565 566 567 568
#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)
569

570
static bool vce_v3_0_check_soft_reset(void *handle)
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
{
	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
	 */
588
	mutex_lock(&adev->grbm_idx_mutex);
589
	WREG32_FIELD(GRBM_GFX_INDEX, INSTANCE_INDEX, 0);
590 591 592 593
	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);
	}
594
	WREG32_FIELD(GRBM_GFX_INDEX, INSTANCE_INDEX, 0x10);
595 596 597 598
	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);
	}
599
	WREG32_FIELD(GRBM_GFX_INDEX, INSTANCE_INDEX, 0);
600
	mutex_unlock(&adev->grbm_idx_mutex);
601 602 603

	if (srbm_soft_reset) {
		adev->vce.srbm_soft_reset = srbm_soft_reset;
604
		return true;
605 606
	} else {
		adev->vce.srbm_soft_reset = 0;
607
		return false;
608 609 610
	}
}

611
static int vce_v3_0_soft_reset(void *handle)
612
{
613
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
614 615
	u32 srbm_soft_reset;

616
	if (!adev->vce.srbm_soft_reset)
617 618 619 620 621
		return 0;
	srbm_soft_reset = adev->vce.srbm_soft_reset;

	if (srbm_soft_reset) {
		u32 tmp;
622

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		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;

646
	if (!adev->vce.srbm_soft_reset)
647 648 649 650 651 652 653 654 655 656 657 658
		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;

659
	if (!adev->vce.srbm_soft_reset)
660
		return 0;
661

662 663
	mdelay(5);

664
	return vce_v3_0_resume(adev);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
}

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");
686

687
	WREG32_FIELD(VCE_SYS_INT_STATUS, VCE_SYS_INT_TRAP_INTERRUPT_INT, 1);
688

689 690 691
	switch (entry->src_data) {
	case 0:
	case 1:
692
	case 2:
693
		amdgpu_fence_process(&adev->vce.ring[entry->src_data]);
694 695 696 697 698 699 700 701 702 703
		break;
	default:
		DRM_ERROR("Unhandled interrupt: %d %d\n",
			  entry->src_id, entry->src_data);
		break;
	}

	return 0;
}

704
static void vce_v3_0_set_bypass_mode(struct amdgpu_device *adev, bool enable)
R
Rex Zhu 已提交
705 706 707 708 709 710 711 712 713 714 715
{
	u32 tmp = RREG32_SMC(ixGCK_DFS_BYPASS_CNTL);

	if (enable)
		tmp |= GCK_DFS_BYPASS_CNTL__BYPASSECLK_MASK;
	else
		tmp &= ~GCK_DFS_BYPASS_CNTL__BYPASSECLK_MASK;

	WREG32_SMC(ixGCK_DFS_BYPASS_CNTL, tmp);
}

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

723
	if ((adev->asic_type == CHIP_POLARIS10) ||
724 725
		(adev->asic_type == CHIP_TONGA) ||
		(adev->asic_type == CHIP_FIJI))
726
		vce_v3_0_set_bypass_mode(adev, enable);
R
Rex Zhu 已提交
727

728
	if (!(adev->cg_flags & AMD_CG_SUPPORT_VCE_MGCG))
729 730 731 732 733 734 735 736
		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;

737
		WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, i);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

		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);
	}

756
	WREG32_FIELD(GRBM_GFX_INDEX, VCE_INSTANCE, 0);
757 758
	mutex_unlock(&adev->grbm_idx_mutex);

759 760 761
	return 0;
}

762 763
static int vce_v3_0_set_powergating_state(void *handle,
					  enum amd_powergating_state state)
764 765 766 767 768 769 770 771
{
	/* 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
	 */
772 773
	struct amdgpu_device *adev = (struct amdgpu_device *)handle;

774
	if (!(adev->pg_flags & AMD_PG_SUPPORT_VCE))
775 776
		return 0;

777
	if (state == AMD_PG_STATE_GATE)
778 779 780 781 782 783
		/* XXX do we need a vce_v3_0_stop()? */
		return 0;
	else
		return vce_v3_0_start(adev);
}

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
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);
}

817
static const struct amd_ip_funcs vce_v3_0_ip_funcs = {
818
	.name = "vce_v3_0",
819 820 821 822 823 824 825 826 827 828
	.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,
829 830
	.check_soft_reset = vce_v3_0_check_soft_reset,
	.pre_soft_reset = vce_v3_0_pre_soft_reset,
831
	.soft_reset = vce_v3_0_soft_reset,
832
	.post_soft_reset = vce_v3_0_post_soft_reset,
833 834 835 836
	.set_clockgating_state = vce_v3_0_set_clockgating_state,
	.set_powergating_state = vce_v3_0_set_powergating_state,
};

837
static const struct amdgpu_ring_funcs vce_v3_0_ring_phys_funcs = {
838
	.type = AMDGPU_RING_TYPE_VCE,
839 840
	.align_mask = 0xf,
	.nop = VCE_CMD_NO_OP,
841 842 843 844
	.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,
845 846 847 848
	.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 */
849 850 851 852
	.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,
853
	.insert_nop = amdgpu_ring_insert_nop,
854
	.pad_ib = amdgpu_ring_generic_pad_ib,
855 856
	.begin_use = amdgpu_vce_ring_begin_use,
	.end_use = amdgpu_vce_ring_end_use,
857 858
};

859
static const struct amdgpu_ring_funcs vce_v3_0_ring_vm_funcs = {
860
	.type = AMDGPU_RING_TYPE_VCE,
861 862
	.align_mask = 0xf,
	.nop = VCE_CMD_NO_OP,
863 864 865
	.get_rptr = vce_v3_0_ring_get_rptr,
	.get_wptr = vce_v3_0_ring_get_wptr,
	.set_wptr = vce_v3_0_ring_set_wptr,
866
	.parse_cs = amdgpu_vce_ring_parse_cs_vm,
867 868 869 870 871
	.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 */
872 873 874 875 876 877 878 879 880 881 882 883
	.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,
};

884 885
static void vce_v3_0_set_ring_funcs(struct amdgpu_device *adev)
{
886 887
	int i;

888 889 890 891 892 893 894 895 896
	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");
	}
897 898 899 900 901 902 903 904 905 906 907 908
}

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;
};
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

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,
};