amdgpu_vcn.c 20.0 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
/*
 * Copyright 2016 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.
 *
 */

#include <linux/firmware.h>
#include <linux/module.h>
29 30
#include <linux/pci.h>

31 32 33 34 35 36 37 38
#include <drm/drm.h>

#include "amdgpu.h"
#include "amdgpu_pm.h"
#include "amdgpu_vcn.h"
#include "soc15d.h"
#include "soc15_common.h"

39
#include "vcn/vcn_1_0_offset.h"
40
#include "vcn/vcn_1_0_sh_mask.h"
41 42 43 44 45 46

/* 1 second timeout */
#define VCN_IDLE_TIMEOUT	msecs_to_jiffies(1000)

/* Firmware Names */
#define FIRMWARE_RAVEN		"amdgpu/raven_vcn.bin"
47
#define FIRMWARE_PICASSO	"amdgpu/picasso_vcn.bin"
48
#define FIRMWARE_RAVEN2		"amdgpu/raven2_vcn.bin"
49
#define FIRMWARE_ARCTURUS 	"amdgpu/arcturus_vcn.bin"
50
#define FIRMWARE_NAVI10 	"amdgpu/navi10_vcn.bin"
J
James Zhu 已提交
51
#define FIRMWARE_NAVI14 	"amdgpu/navi14_vcn.bin"
52
#define FIRMWARE_NAVI12 	"amdgpu/navi12_vcn.bin"
53 54

MODULE_FIRMWARE(FIRMWARE_RAVEN);
55
MODULE_FIRMWARE(FIRMWARE_PICASSO);
56
MODULE_FIRMWARE(FIRMWARE_RAVEN2);
57
MODULE_FIRMWARE(FIRMWARE_ARCTURUS);
58
MODULE_FIRMWARE(FIRMWARE_NAVI10);
J
James Zhu 已提交
59
MODULE_FIRMWARE(FIRMWARE_NAVI14);
60
MODULE_FIRMWARE(FIRMWARE_NAVI12);
61 62 63 64 65 66 67 68

static void amdgpu_vcn_idle_work_handler(struct work_struct *work);

int amdgpu_vcn_sw_init(struct amdgpu_device *adev)
{
	unsigned long bo_size;
	const char *fw_name;
	const struct common_firmware_header *hdr;
69
	unsigned char fw_check;
70
	int i, r;
71 72 73 74 75

	INIT_DELAYED_WORK(&adev->vcn.idle_work, amdgpu_vcn_idle_work_handler);

	switch (adev->asic_type) {
	case CHIP_RAVEN:
76
		if (adev->rev_id >= 8)
77
			fw_name = FIRMWARE_RAVEN2;
78 79
		else if (adev->pdev->device == 0x15d8)
			fw_name = FIRMWARE_PICASSO;
80 81
		else
			fw_name = FIRMWARE_RAVEN;
82
		break;
83 84 85
	case CHIP_ARCTURUS:
		fw_name = FIRMWARE_ARCTURUS;
		break;
86 87
	case CHIP_NAVI10:
		fw_name = FIRMWARE_NAVI10;
88 89 90
		if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
		    (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG))
			adev->vcn.indirect_sram = true;
91
		break;
92
	case CHIP_NAVI14:
J
James Zhu 已提交
93
		fw_name = FIRMWARE_NAVI14;
94 95 96
		if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
		    (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG))
			adev->vcn.indirect_sram = true;
J
James Zhu 已提交
97
		break;
98 99 100 101 102 103
	case CHIP_NAVI12:
		fw_name = FIRMWARE_NAVI12;
		if ((adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) &&
		    (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG))
			adev->vcn.indirect_sram = true;
		break;
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	default:
		return -EINVAL;
	}

	r = request_firmware(&adev->vcn.fw, fw_name, adev->dev);
	if (r) {
		dev_err(adev->dev, "amdgpu_vcn: Can't load firmware \"%s\"\n",
			fw_name);
		return r;
	}

	r = amdgpu_ucode_validate(adev->vcn.fw);
	if (r) {
		dev_err(adev->dev, "amdgpu_vcn: Can't validate firmware \"%s\"\n",
			fw_name);
		release_firmware(adev->vcn.fw);
		adev->vcn.fw = NULL;
		return r;
	}

	hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
125
	adev->vcn.fw_version = le32_to_cpu(hdr->ucode_version);
126

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	/* Bit 20-23, it is encode major and non-zero for new naming convention.
	 * This field is part of version minor and DRM_DISABLED_FLAG in old naming
	 * convention. Since the l:wq!atest version minor is 0x5B and DRM_DISABLED_FLAG
	 * is zero in old naming convention, this field is always zero so far.
	 * These four bits are used to tell which naming convention is present.
	 */
	fw_check = (le32_to_cpu(hdr->ucode_version) >> 20) & 0xf;
	if (fw_check) {
		unsigned int dec_ver, enc_major, enc_minor, vep, fw_rev;

		fw_rev = le32_to_cpu(hdr->ucode_version) & 0xfff;
		enc_minor = (le32_to_cpu(hdr->ucode_version) >> 12) & 0xff;
		enc_major = fw_check;
		dec_ver = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xf;
		vep = (le32_to_cpu(hdr->ucode_version) >> 28) & 0xf;
		DRM_INFO("Found VCN firmware Version ENC: %hu.%hu DEC: %hu VEP: %hu Revision: %hu\n",
			enc_major, enc_minor, dec_ver, vep, fw_rev);
	} else {
		unsigned int version_major, version_minor, family_id;

		family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
		version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
		version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
		DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
			version_major, version_minor, family_id);
	}
153

154
	bo_size = AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_CONTEXT_SIZE;
155 156
	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
		bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
157 158

	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
159 160 161
		if (adev->vcn.harvest_config & (1 << i))
			continue;

162 163 164 165 166 167 168
		r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
						AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.inst[i].vcpu_bo,
						&adev->vcn.inst[i].gpu_addr, &adev->vcn.inst[i].cpu_addr);
		if (r) {
			dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
			return r;
		}
169 170
	}

171 172 173 174 175 176 177 178 179 180
	if (adev->vcn.indirect_sram) {
		r = amdgpu_bo_create_kernel(adev, 64 * 2 * 4, PAGE_SIZE,
			    AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.dpg_sram_bo,
			    &adev->vcn.dpg_sram_gpu_addr, &adev->vcn.dpg_sram_cpu_addr);
		if (r) {
			dev_err(adev->dev, "(%d) failed to allocate DPG bo\n", r);
			return r;
		}
	}

181 182 183 184 185
	return 0;
}

int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
{
186
	int i, j;
187

188 189
	if (adev->vcn.indirect_sram) {
		amdgpu_bo_free_kernel(&adev->vcn.dpg_sram_bo,
190 191
				      &adev->vcn.dpg_sram_gpu_addr,
				      (void **)&adev->vcn.dpg_sram_cpu_addr);
192 193
	}

194
	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
195 196
		if (adev->vcn.harvest_config & (1 << j))
			continue;
197
		kvfree(adev->vcn.inst[j].saved_bo);
198

199 200 201
		amdgpu_bo_free_kernel(&adev->vcn.inst[j].vcpu_bo,
					  &adev->vcn.inst[j].gpu_addr,
					  (void **)&adev->vcn.inst[j].cpu_addr);
202

203
		amdgpu_ring_fini(&adev->vcn.inst[j].ring_dec);
L
Leo Liu 已提交
204

205 206 207 208 209
		for (i = 0; i < adev->vcn.num_enc_rings; ++i)
			amdgpu_ring_fini(&adev->vcn.inst[j].ring_enc[i]);

		amdgpu_ring_fini(&adev->vcn.inst[j].ring_jpeg);
	}
210

211 212 213 214 215 216 217 218 219
	release_firmware(adev->vcn.fw);

	return 0;
}

int amdgpu_vcn_suspend(struct amdgpu_device *adev)
{
	unsigned size;
	void *ptr;
220
	int i;
221

222 223
	cancel_delayed_work_sync(&adev->vcn.idle_work);

224
	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
225 226
		if (adev->vcn.harvest_config & (1 << i))
			continue;
227 228
		if (adev->vcn.inst[i].vcpu_bo == NULL)
			return 0;
229

230 231
		size = amdgpu_bo_size(adev->vcn.inst[i].vcpu_bo);
		ptr = adev->vcn.inst[i].cpu_addr;
232

233 234 235
		adev->vcn.inst[i].saved_bo = kvmalloc(size, GFP_KERNEL);
		if (!adev->vcn.inst[i].saved_bo)
			return -ENOMEM;
236

237 238
		memcpy_fromio(adev->vcn.inst[i].saved_bo, ptr, size);
	}
239 240 241 242 243 244 245
	return 0;
}

int amdgpu_vcn_resume(struct amdgpu_device *adev)
{
	unsigned size;
	void *ptr;
246
	int i;
247

248
	for (i = 0; i < adev->vcn.num_vcn_inst; ++i) {
249 250
		if (adev->vcn.harvest_config & (1 << i))
			continue;
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		if (adev->vcn.inst[i].vcpu_bo == NULL)
			return -EINVAL;

		size = amdgpu_bo_size(adev->vcn.inst[i].vcpu_bo);
		ptr = adev->vcn.inst[i].cpu_addr;

		if (adev->vcn.inst[i].saved_bo != NULL) {
			memcpy_toio(ptr, adev->vcn.inst[i].saved_bo, size);
			kvfree(adev->vcn.inst[i].saved_bo);
			adev->vcn.inst[i].saved_bo = NULL;
		} else {
			const struct common_firmware_header *hdr;
			unsigned offset;

			hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
			if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
				offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
				memcpy_toio(adev->vcn.inst[i].cpu_addr, adev->vcn.fw->data + offset,
					    le32_to_cpu(hdr->ucode_size_bytes));
				size -= le32_to_cpu(hdr->ucode_size_bytes);
				ptr += le32_to_cpu(hdr->ucode_size_bytes);
			}
			memset_io(ptr, 0, size);
274
		}
275 276 277 278
	}
	return 0;
}

279 280 281 282
static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
{
	struct amdgpu_device *adev =
		container_of(work, struct amdgpu_device, vcn.idle_work.work);
283 284
	unsigned int fences = 0, fence[AMDGPU_MAX_VCN_INSTANCES] = {0};
	unsigned int i, j;
285

286
	for (j = 0; j < adev->vcn.num_vcn_inst; ++j) {
287 288
		if (adev->vcn.harvest_config & (1 << j))
			continue;
289 290 291
		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
			fence[j] += amdgpu_fence_count_emitted(&adev->vcn.inst[j].ring_enc[i]);
		}
292

293 294
		if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)	{
			struct dpg_pause_state new_state;
295

296 297 298 299
			if (fence[j])
				new_state.fw_based = VCN_DPG_STATE__PAUSE;
			else
				new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
300

301 302 303 304
			if (amdgpu_fence_count_emitted(&adev->vcn.inst[j].ring_jpeg))
				new_state.jpeg = VCN_DPG_STATE__PAUSE;
			else
				new_state.jpeg = VCN_DPG_STATE__UNPAUSE;
305

306 307
			adev->vcn.pause_dpg_mode(adev, &new_state);
		}
308

309 310 311 312
		fence[j] += amdgpu_fence_count_emitted(&adev->vcn.inst[j].ring_jpeg);
		fence[j] += amdgpu_fence_count_emitted(&adev->vcn.inst[j].ring_dec);
		fences += fence[j];
	}
313

314
	if (fences == 0) {
315
		amdgpu_gfx_off_ctrl(adev, true);
316
		if (adev->asic_type < CHIP_ARCTURUS && adev->pm.dpm_enabled)
317
			amdgpu_dpm_enable_uvd(adev, false);
R
Rex Zhu 已提交
318 319 320
		else
			amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
							       AMD_PG_STATE_GATE);
321 322 323 324 325 326 327 328 329 330
	} else {
		schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
	}
}

void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;
	bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);

331
	if (set_clocks) {
332
		amdgpu_gfx_off_ctrl(adev, false);
333
		if (adev->asic_type < CHIP_ARCTURUS && adev->pm.dpm_enabled)
R
Rex Zhu 已提交
334 335 336 337
			amdgpu_dpm_enable_uvd(adev, true);
		else
			amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCN,
							       AMD_PG_STATE_UNGATE);
338
	}
339 340 341

	if (adev->pg_flags & AMD_PG_SUPPORT_VCN_DPG)	{
		struct dpg_pause_state new_state;
342 343
		unsigned int fences = 0;
		unsigned int i;
344

345
		for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
346
			fences += amdgpu_fence_count_emitted(&adev->vcn.inst[ring->me].ring_enc[i]);
347 348
		}
		if (fences)
349 350
			new_state.fw_based = VCN_DPG_STATE__PAUSE;
		else
351
			new_state.fw_based = VCN_DPG_STATE__UNPAUSE;
352

353
		if (amdgpu_fence_count_emitted(&adev->vcn.inst[ring->me].ring_jpeg))
354 355
			new_state.jpeg = VCN_DPG_STATE__PAUSE;
		else
356 357 358 359 360 361
			new_state.jpeg = VCN_DPG_STATE__UNPAUSE;

		if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_ENC)
			new_state.fw_based = VCN_DPG_STATE__PAUSE;
		else if (ring->funcs->type == AMDGPU_RING_TYPE_VCN_JPEG)
			new_state.jpeg = VCN_DPG_STATE__PAUSE;
362

363
		adev->vcn.pause_dpg_mode(adev, &new_state);
364
	}
365 366 367 368 369 370 371
}

void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
{
	schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
}

372 373 374 375 376 377 378
int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;
	uint32_t tmp = 0;
	unsigned i;
	int r;

379
	WREG32(adev->vcn.inst[ring->me].external.scratch9, 0xCAFEDEAD);
380
	r = amdgpu_ring_alloc(ring, 3);
381
	if (r)
382
		return r;
383
	amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.scratch9, 0));
384 385 386
	amdgpu_ring_write(ring, 0xDEADBEEF);
	amdgpu_ring_commit(ring);
	for (i = 0; i < adev->usec_timeout; i++) {
387
		tmp = RREG32(adev->vcn.inst[ring->me].external.scratch9);
388 389
		if (tmp == 0xDEADBEEF)
			break;
390
		udelay(1);
391 392
	}

393 394 395
	if (i >= adev->usec_timeout)
		r = -ETIMEDOUT;

396 397 398
	return r;
}

399
static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring,
400
				   struct amdgpu_bo *bo,
401
				   struct dma_fence **fence)
402
{
403 404
	struct amdgpu_device *adev = ring->adev;
	struct dma_fence *f = NULL;
405 406 407 408 409 410 411 412 413 414 415
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
	uint64_t addr;
	int i, r;

	r = amdgpu_job_alloc_with_ib(adev, 64, &job);
	if (r)
		goto err;

	ib = &job->ibs[0];
	addr = amdgpu_bo_gpu_offset(bo);
L
Leo Liu 已提交
416
	ib->ptr[0] = PACKET0(adev->vcn.internal.data0, 0);
417
	ib->ptr[1] = addr;
L
Leo Liu 已提交
418
	ib->ptr[2] = PACKET0(adev->vcn.internal.data1, 0);
419
	ib->ptr[3] = addr >> 32;
L
Leo Liu 已提交
420
	ib->ptr[4] = PACKET0(adev->vcn.internal.cmd, 0);
421 422
	ib->ptr[5] = 0;
	for (i = 6; i < 16; i += 2) {
L
Leo Liu 已提交
423
		ib->ptr[i] = PACKET0(adev->vcn.internal.nop, 0);
424 425 426 427
		ib->ptr[i+1] = 0;
	}
	ib->length_dw = 16;

428
	r = amdgpu_job_submit_direct(job, ring, &f);
429 430
	if (r)
		goto err_free;
431

432 433 434
	amdgpu_bo_fence(bo, f, false);
	amdgpu_bo_unreserve(bo);
	amdgpu_bo_unref(&bo);
435 436 437 438 439 440 441 442 443 444 445

	if (fence)
		*fence = dma_fence_get(f);
	dma_fence_put(f);

	return 0;

err_free:
	amdgpu_job_free(job);

err:
446 447
	amdgpu_bo_unreserve(bo);
	amdgpu_bo_unref(&bo);
448 449 450 451 452 453 454
	return r;
}

static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
			      struct dma_fence **fence)
{
	struct amdgpu_device *adev = ring->adev;
455
	struct amdgpu_bo *bo = NULL;
456 457 458
	uint32_t *msg;
	int r, i;

459 460 461
	r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
				      AMDGPU_GEM_DOMAIN_VRAM,
				      &bo, NULL, (void **)&msg);
462 463 464
	if (r)
		return r;

465
	msg[0] = cpu_to_le32(0x00000028);
466
	msg[1] = cpu_to_le32(0x00000038);
467
	msg[2] = cpu_to_le32(0x00000001);
468
	msg[3] = cpu_to_le32(0x00000000);
469
	msg[4] = cpu_to_le32(handle);
470
	msg[5] = cpu_to_le32(0x00000000);
471 472
	msg[6] = cpu_to_le32(0x00000001);
	msg[7] = cpu_to_le32(0x00000028);
473
	msg[8] = cpu_to_le32(0x00000010);
474
	msg[9] = cpu_to_le32(0x00000000);
475 476
	msg[10] = cpu_to_le32(0x00000007);
	msg[11] = cpu_to_le32(0x00000000);
477 478 479
	msg[12] = cpu_to_le32(0x00000780);
	msg[13] = cpu_to_le32(0x00000440);
	for (i = 14; i < 1024; ++i)
480 481
		msg[i] = cpu_to_le32(0x0);

482
	return amdgpu_vcn_dec_send_msg(ring, bo, fence);
483 484 485
}

static int amdgpu_vcn_dec_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
486
			       struct dma_fence **fence)
487 488
{
	struct amdgpu_device *adev = ring->adev;
489
	struct amdgpu_bo *bo = NULL;
490 491 492
	uint32_t *msg;
	int r, i;

493 494 495
	r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
				      AMDGPU_GEM_DOMAIN_VRAM,
				      &bo, NULL, (void **)&msg);
496 497 498
	if (r)
		return r;

499 500 501 502 503 504 505
	msg[0] = cpu_to_le32(0x00000028);
	msg[1] = cpu_to_le32(0x00000018);
	msg[2] = cpu_to_le32(0x00000000);
	msg[3] = cpu_to_le32(0x00000002);
	msg[4] = cpu_to_le32(handle);
	msg[5] = cpu_to_le32(0x00000000);
	for (i = 6; i < 1024; ++i)
506 507
		msg[i] = cpu_to_le32(0x0);

508
	return amdgpu_vcn_dec_send_msg(ring, bo, fence);
509 510 511 512 513 514 515 516
}

int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
{
	struct dma_fence *fence;
	long r;

	r = amdgpu_vcn_dec_get_create_msg(ring, 1, NULL);
517
	if (r)
518 519
		goto error;

520
	r = amdgpu_vcn_dec_get_destroy_msg(ring, 1, &fence);
521
	if (r)
522 523 524
		goto error;

	r = dma_fence_wait_timeout(fence, false, timeout);
525
	if (r == 0)
526
		r = -ETIMEDOUT;
527
	else if (r > 0)
528 529 530 531 532 533
		r = 0;

	dma_fence_put(fence);
error:
	return r;
}
L
Leo Liu 已提交
534

535 536 537
int amdgpu_vcn_enc_ring_test_ring(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;
538
	uint32_t rptr;
539 540 541 542
	unsigned i;
	int r;

	r = amdgpu_ring_alloc(ring, 16);
543
	if (r)
544
		return r;
545

546 547
	rptr = amdgpu_ring_get_rptr(ring);

548
	amdgpu_ring_write(ring, VCN_ENC_CMD_END);
549 550 551 552 553
	amdgpu_ring_commit(ring);

	for (i = 0; i < adev->usec_timeout; i++) {
		if (amdgpu_ring_get_rptr(ring) != rptr)
			break;
554
		udelay(1);
555 556
	}

557
	if (i >= adev->usec_timeout)
558 559 560 561 562
		r = -ETIMEDOUT;

	return r;
}

L
Leo Liu 已提交
563 564 565
static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
			      struct dma_fence **fence)
{
L
Leo Liu 已提交
566
	const unsigned ib_size_dw = 16;
L
Leo Liu 已提交
567 568 569 570 571 572 573 574 575 576 577 578 579 580
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
	struct dma_fence *f = NULL;
	uint64_t dummy;
	int i, r;

	r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
	if (r)
		return r;

	ib = &job->ibs[0];
	dummy = ib->gpu_addr + 1024;

	ib->length_dw = 0;
L
Leo Liu 已提交
581 582
	ib->ptr[ib->length_dw++] = 0x00000018;
	ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
L
Leo Liu 已提交
583
	ib->ptr[ib->length_dw++] = handle;
L
Leo Liu 已提交
584 585 586
	ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
	ib->ptr[ib->length_dw++] = dummy;
	ib->ptr[ib->length_dw++] = 0x0000000b;
L
Leo Liu 已提交
587

L
Leo Liu 已提交
588 589 590
	ib->ptr[ib->length_dw++] = 0x00000014;
	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
	ib->ptr[ib->length_dw++] = 0x0000001c;
L
Leo Liu 已提交
591 592 593
	ib->ptr[ib->length_dw++] = 0x00000000;
	ib->ptr[ib->length_dw++] = 0x00000000;

L
Leo Liu 已提交
594 595
	ib->ptr[ib->length_dw++] = 0x00000008;
	ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
L
Leo Liu 已提交
596 597 598 599

	for (i = ib->length_dw; i < ib_size_dw; ++i)
		ib->ptr[i] = 0x0;

600
	r = amdgpu_job_submit_direct(job, ring, &f);
L
Leo Liu 已提交
601 602 603 604 605 606
	if (r)
		goto err;

	if (fence)
		*fence = dma_fence_get(f);
	dma_fence_put(f);
L
Leo Liu 已提交
607

L
Leo Liu 已提交
608 609 610 611 612 613 614 615
	return 0;

err:
	amdgpu_job_free(job);
	return r;
}

static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
L
Leo Liu 已提交
616
				struct dma_fence **fence)
L
Leo Liu 已提交
617
{
L
Leo Liu 已提交
618
	const unsigned ib_size_dw = 16;
L
Leo Liu 已提交
619 620 621
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
	struct dma_fence *f = NULL;
L
Leo Liu 已提交
622
	uint64_t dummy;
L
Leo Liu 已提交
623 624 625 626 627 628 629
	int i, r;

	r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
	if (r)
		return r;

	ib = &job->ibs[0];
L
Leo Liu 已提交
630
	dummy = ib->gpu_addr + 1024;
L
Leo Liu 已提交
631 632

	ib->length_dw = 0;
L
Leo Liu 已提交
633 634
	ib->ptr[ib->length_dw++] = 0x00000018;
	ib->ptr[ib->length_dw++] = 0x00000001;
L
Leo Liu 已提交
635
	ib->ptr[ib->length_dw++] = handle;
L
Leo Liu 已提交
636 637 638
	ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
	ib->ptr[ib->length_dw++] = dummy;
	ib->ptr[ib->length_dw++] = 0x0000000b;
L
Leo Liu 已提交
639

L
Leo Liu 已提交
640 641 642
	ib->ptr[ib->length_dw++] = 0x00000014;
	ib->ptr[ib->length_dw++] = 0x00000002;
	ib->ptr[ib->length_dw++] = 0x0000001c;
L
Leo Liu 已提交
643 644 645
	ib->ptr[ib->length_dw++] = 0x00000000;
	ib->ptr[ib->length_dw++] = 0x00000000;

L
Leo Liu 已提交
646 647
	ib->ptr[ib->length_dw++] = 0x00000008;
	ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
L
Leo Liu 已提交
648 649 650 651

	for (i = ib->length_dw; i < ib_size_dw; ++i)
		ib->ptr[i] = 0x0;

652
	r = amdgpu_job_submit_direct(job, ring, &f);
L
Leo Liu 已提交
653 654
	if (r)
		goto err;
L
Leo Liu 已提交
655 656 657 658

	if (fence)
		*fence = dma_fence_get(f);
	dma_fence_put(f);
L
Leo Liu 已提交
659

L
Leo Liu 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672
	return 0;

err:
	amdgpu_job_free(job);
	return r;
}

int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
{
	struct dma_fence *fence = NULL;
	long r;

	r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
673
	if (r)
L
Leo Liu 已提交
674 675
		goto error;

L
Leo Liu 已提交
676
	r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &fence);
677
	if (r)
L
Leo Liu 已提交
678 679 680
		goto error;

	r = dma_fence_wait_timeout(fence, false, timeout);
681
	if (r == 0)
L
Leo Liu 已提交
682
		r = -ETIMEDOUT;
683
	else if (r > 0)
L
Leo Liu 已提交
684
		r = 0;
685

L
Leo Liu 已提交
686 687 688 689
error:
	dma_fence_put(fence);
	return r;
}
690 691 692 693 694 695 696 697

int amdgpu_vcn_jpeg_ring_test_ring(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;
	uint32_t tmp = 0;
	unsigned i;
	int r;

698
	WREG32(adev->vcn.inst[ring->me].external.jpeg_pitch, 0xCAFEDEAD);
699
	r = amdgpu_ring_alloc(ring, 3);
700
	if (r)
701 702
		return r;

703
	amdgpu_ring_write(ring, PACKET0(adev->vcn.internal.jpeg_pitch, 0));
704 705 706 707
	amdgpu_ring_write(ring, 0xDEADBEEF);
	amdgpu_ring_commit(ring);

	for (i = 0; i < adev->usec_timeout; i++) {
708
		tmp = RREG32(adev->vcn.inst[ring->me].external.jpeg_pitch);
709 710
		if (tmp == 0xDEADBEEF)
			break;
711
		udelay(1);
712 713
	}

714 715
	if (i >= adev->usec_timeout)
		r = -ETIMEDOUT;
716 717 718

	return r;
}
B
Boyuan Zhang 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735

static int amdgpu_vcn_jpeg_set_reg(struct amdgpu_ring *ring, uint32_t handle,
		struct dma_fence **fence)
{
	struct amdgpu_device *adev = ring->adev;
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
	struct dma_fence *f = NULL;
	const unsigned ib_size_dw = 16;
	int i, r;

	r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
	if (r)
		return r;

	ib = &job->ibs[0];

736
	ib->ptr[0] = PACKETJ(adev->vcn.internal.jpeg_pitch, 0, 0, PACKETJ_TYPE0);
B
Boyuan Zhang 已提交
737 738 739 740 741 742 743
	ib->ptr[1] = 0xDEADBEEF;
	for (i = 2; i < 16; i += 2) {
		ib->ptr[i] = PACKETJ(0, 0, 0, PACKETJ_TYPE6);
		ib->ptr[i+1] = 0;
	}
	ib->length_dw = 16;

744
	r = amdgpu_job_submit_direct(job, ring, &f);
B
Boyuan Zhang 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
	if (r)
		goto err;

	if (fence)
		*fence = dma_fence_get(f);
	dma_fence_put(f);

	return 0;

err:
	amdgpu_job_free(job);
	return r;
}

int amdgpu_vcn_jpeg_ring_test_ib(struct amdgpu_ring *ring, long timeout)
{
	struct amdgpu_device *adev = ring->adev;
	uint32_t tmp = 0;
	unsigned i;
	struct dma_fence *fence = NULL;
	long r = 0;

	r = amdgpu_vcn_jpeg_set_reg(ring, 1, &fence);
768
	if (r)
B
Boyuan Zhang 已提交
769 770 771 772 773 774 775 776
		goto error;

	r = dma_fence_wait_timeout(fence, false, timeout);
	if (r == 0) {
		r = -ETIMEDOUT;
		goto error;
	} else if (r < 0) {
		goto error;
777
	} else {
B
Boyuan Zhang 已提交
778
		r = 0;
779
	}
B
Boyuan Zhang 已提交
780 781

	for (i = 0; i < adev->usec_timeout; i++) {
782
		tmp = RREG32(adev->vcn.inst[ring->me].external.jpeg_pitch);
B
Boyuan Zhang 已提交
783 784
		if (tmp == 0xDEADBEEF)
			break;
785
		udelay(1);
B
Boyuan Zhang 已提交
786 787
	}

788 789
	if (i >= adev->usec_timeout)
		r = -ETIMEDOUT;
B
Boyuan Zhang 已提交
790 791 792 793 794

	dma_fence_put(fence);
error:
	return r;
}