amdgpu_vce.c 27.7 KB
Newer Older
A
Alex Deucher 已提交
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 35 36 37 38
/*
 * Copyright 2013 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 <linux/module.h>
#include <drm/drmP.h>
#include <drm/drm.h>

#include "amdgpu.h"
#include "amdgpu_pm.h"
#include "amdgpu_vce.h"
#include "cikd.h"

/* 1 second timeout */
39
#define VCE_IDLE_TIMEOUT	msecs_to_jiffies(1000)
A
Alex Deucher 已提交
40 41 42

/* Firmware Names */
#ifdef CONFIG_DRM_AMDGPU_CIK
43 44 45 46 47
#define FIRMWARE_BONAIRE	"amdgpu/bonaire_vce.bin"
#define FIRMWARE_KABINI	"amdgpu/kabini_vce.bin"
#define FIRMWARE_KAVERI	"amdgpu/kaveri_vce.bin"
#define FIRMWARE_HAWAII	"amdgpu/hawaii_vce.bin"
#define FIRMWARE_MULLINS	"amdgpu/mullins_vce.bin"
A
Alex Deucher 已提交
48
#endif
49 50
#define FIRMWARE_TONGA		"amdgpu/tonga_vce.bin"
#define FIRMWARE_CARRIZO	"amdgpu/carrizo_vce.bin"
51
#define FIRMWARE_FIJI		"amdgpu/fiji_vce.bin"
52
#define FIRMWARE_STONEY		"amdgpu/stoney_vce.bin"
53
#define FIRMWARE_POLARIS10	"amdgpu/polaris10_vce.bin"
54 55 56
#define FIRMWARE_POLARIS11	"amdgpu/polaris11_vce.bin"
#define FIRMWARE_POLARIS12	"amdgpu/polaris12_vce.bin"
#define FIRMWARE_VEGAM		"amdgpu/vegam_vce.bin"
A
Alex Deucher 已提交
57

58
#define FIRMWARE_VEGA10		"amdgpu/vega10_vce.bin"
59
#define FIRMWARE_VEGA12		"amdgpu/vega12_vce.bin"
60
#define FIRMWARE_VEGA20		"amdgpu/vega20_vce.bin"
61

A
Alex Deucher 已提交
62 63 64 65 66 67 68 69 70
#ifdef CONFIG_DRM_AMDGPU_CIK
MODULE_FIRMWARE(FIRMWARE_BONAIRE);
MODULE_FIRMWARE(FIRMWARE_KABINI);
MODULE_FIRMWARE(FIRMWARE_KAVERI);
MODULE_FIRMWARE(FIRMWARE_HAWAII);
MODULE_FIRMWARE(FIRMWARE_MULLINS);
#endif
MODULE_FIRMWARE(FIRMWARE_TONGA);
MODULE_FIRMWARE(FIRMWARE_CARRIZO);
71
MODULE_FIRMWARE(FIRMWARE_FIJI);
72
MODULE_FIRMWARE(FIRMWARE_STONEY);
73 74
MODULE_FIRMWARE(FIRMWARE_POLARIS10);
MODULE_FIRMWARE(FIRMWARE_POLARIS11);
75
MODULE_FIRMWARE(FIRMWARE_POLARIS12);
76
MODULE_FIRMWARE(FIRMWARE_VEGAM);
A
Alex Deucher 已提交
77

78
MODULE_FIRMWARE(FIRMWARE_VEGA10);
79
MODULE_FIRMWARE(FIRMWARE_VEGA12);
80
MODULE_FIRMWARE(FIRMWARE_VEGA20);
81

A
Alex Deucher 已提交
82 83 84 85 86 87 88 89 90
static void amdgpu_vce_idle_work_handler(struct work_struct *work);

/**
 * amdgpu_vce_init - allocate memory, load vce firmware
 *
 * @adev: amdgpu_device pointer
 *
 * First step to get VCE online, allocate memory and load the firmware
 */
91
int amdgpu_vce_sw_init(struct amdgpu_device *adev, unsigned long size)
A
Alex Deucher 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
{
	const char *fw_name;
	const struct common_firmware_header *hdr;
	unsigned ucode_version, version_major, version_minor, binary_id;
	int i, r;

	switch (adev->asic_type) {
#ifdef CONFIG_DRM_AMDGPU_CIK
	case CHIP_BONAIRE:
		fw_name = FIRMWARE_BONAIRE;
		break;
	case CHIP_KAVERI:
		fw_name = FIRMWARE_KAVERI;
		break;
	case CHIP_KABINI:
		fw_name = FIRMWARE_KABINI;
		break;
	case CHIP_HAWAII:
		fw_name = FIRMWARE_HAWAII;
		break;
	case CHIP_MULLINS:
		fw_name = FIRMWARE_MULLINS;
		break;
#endif
	case CHIP_TONGA:
		fw_name = FIRMWARE_TONGA;
		break;
	case CHIP_CARRIZO:
		fw_name = FIRMWARE_CARRIZO;
		break;
122 123 124
	case CHIP_FIJI:
		fw_name = FIRMWARE_FIJI;
		break;
125 126 127
	case CHIP_STONEY:
		fw_name = FIRMWARE_STONEY;
		break;
128 129
	case CHIP_POLARIS10:
		fw_name = FIRMWARE_POLARIS10;
130
		break;
131 132
	case CHIP_POLARIS11:
		fw_name = FIRMWARE_POLARIS11;
133
		break;
134 135 136
	case CHIP_POLARIS12:
		fw_name = FIRMWARE_POLARIS12;
		break;
137 138 139
	case CHIP_VEGAM:
		fw_name = FIRMWARE_VEGAM;
		break;
140 141 142
	case CHIP_VEGA10:
		fw_name = FIRMWARE_VEGA10;
		break;
143 144
	case CHIP_VEGA12:
		fw_name = FIRMWARE_VEGA12;
145
		break;
146 147 148
	case CHIP_VEGA20:
		fw_name = FIRMWARE_VEGA20;
		break;
A
Alex Deucher 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

	default:
		return -EINVAL;
	}

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

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

	hdr = (const struct common_firmware_header *)adev->vce.fw->data;

	ucode_version = le32_to_cpu(hdr->ucode_version);
	version_major = (ucode_version >> 20) & 0xfff;
	version_minor = (ucode_version >> 8) & 0xfff;
	binary_id = ucode_version & 0xff;
	DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
		version_major, version_minor, binary_id);
	adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
				(binary_id << 8));

181 182 183
	r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
				    AMDGPU_GEM_DOMAIN_VRAM, &adev->vce.vcpu_bo,
				    &adev->vce.gpu_addr, &adev->vce.cpu_addr);
A
Alex Deucher 已提交
184 185 186 187 188 189 190 191 192 193
	if (r) {
		dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
		return r;
	}

	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
		atomic_set(&adev->vce.handles[i], 0);
		adev->vce.filp[i] = NULL;
	}

194 195 196
	INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
	mutex_init(&adev->vce.idle_mutex);

A
Alex Deucher 已提交
197 198 199 200 201 202 203 204 205 206 207 208
	return 0;
}

/**
 * amdgpu_vce_fini - free memory
 *
 * @adev: amdgpu_device pointer
 *
 * Last step on VCE teardown, free firmware memory
 */
int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
{
209 210
	unsigned i;

A
Alex Deucher 已提交
211 212 213
	if (adev->vce.vcpu_bo == NULL)
		return 0;

214
	drm_sched_entity_destroy(&adev->vce.entity);
215

216 217
	amdgpu_bo_free_kernel(&adev->vce.vcpu_bo, &adev->vce.gpu_addr,
		(void **)&adev->vce.cpu_addr);
A
Alex Deucher 已提交
218

219 220
	for (i = 0; i < adev->vce.num_rings; i++)
		amdgpu_ring_fini(&adev->vce.ring[i]);
A
Alex Deucher 已提交
221 222

	release_firmware(adev->vce.fw);
223
	mutex_destroy(&adev->vce.idle_mutex);
A
Alex Deucher 已提交
224 225 226 227

	return 0;
}

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/**
 * amdgpu_vce_entity_init - init entity
 *
 * @adev: amdgpu_device pointer
 *
 */
int amdgpu_vce_entity_init(struct amdgpu_device *adev)
{
	struct amdgpu_ring *ring;
	struct drm_sched_rq *rq;
	int r;

	ring = &adev->vce.ring[0];
	rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_NORMAL];
	r = drm_sched_entity_init(&adev->vce.entity, &rq, 1, NULL);
	if (r != 0) {
		DRM_ERROR("Failed setting up VCE run queue.\n");
		return r;
	}

	return 0;
}

A
Alex Deucher 已提交
251 252 253 254 255 256 257 258 259 260
/**
 * amdgpu_vce_suspend - unpin VCE fw memory
 *
 * @adev: amdgpu_device pointer
 *
 */
int amdgpu_vce_suspend(struct amdgpu_device *adev)
{
	int i;

261 262
	cancel_delayed_work_sync(&adev->vce.idle_work);

A
Alex Deucher 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	if (adev->vce.vcpu_bo == NULL)
		return 0;

	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
		if (atomic_read(&adev->vce.handles[i]))
			break;

	if (i == AMDGPU_MAX_VCE_HANDLES)
		return 0;

	/* TODO: suspending running encoding sessions isn't supported */
	return -EINVAL;
}

/**
 * amdgpu_vce_resume - pin VCE fw memory
 *
 * @adev: amdgpu_device pointer
 *
 */
int amdgpu_vce_resume(struct amdgpu_device *adev)
{
	void *cpu_addr;
	const struct common_firmware_header *hdr;
	unsigned offset;
	int r;

	if (adev->vce.vcpu_bo == NULL)
		return -EINVAL;

	r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
	if (r) {
		dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
		return r;
	}

	r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
	if (r) {
		amdgpu_bo_unreserve(adev->vce.vcpu_bo);
		dev_err(adev->dev, "(%d) VCE map failed\n", r);
		return r;
	}

	hdr = (const struct common_firmware_header *)adev->vce.fw->data;
	offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
308 309
	memcpy_toio(cpu_addr, adev->vce.fw->data + offset,
		    adev->vce.fw->size - offset);
A
Alex Deucher 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	amdgpu_bo_kunmap(adev->vce.vcpu_bo);

	amdgpu_bo_unreserve(adev->vce.vcpu_bo);

	return 0;
}

/**
 * amdgpu_vce_idle_work_handler - power off VCE
 *
 * @work: pointer to work structure
 *
 * power of VCE when it's not used any more
 */
static void amdgpu_vce_idle_work_handler(struct work_struct *work)
{
	struct amdgpu_device *adev =
		container_of(work, struct amdgpu_device, vce.idle_work.work);
329
	unsigned i, count = 0;
A
Alex Deucher 已提交
330

331 332 333 334
	for (i = 0; i < adev->vce.num_rings; i++)
		count += amdgpu_fence_count_emitted(&adev->vce.ring[i]);

	if (count == 0) {
A
Alex Deucher 已提交
335 336 337 338
		if (adev->pm.dpm_enabled) {
			amdgpu_dpm_enable_vce(adev, false);
		} else {
			amdgpu_asic_set_vce_clocks(adev, 0, 0);
339 340 341 342
			amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							       AMD_PG_STATE_GATE);
			amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							       AMD_CG_STATE_GATE);
A
Alex Deucher 已提交
343 344
		}
	} else {
345
		schedule_delayed_work(&adev->vce.idle_work, VCE_IDLE_TIMEOUT);
A
Alex Deucher 已提交
346 347 348 349
	}
}

/**
350
 * amdgpu_vce_ring_begin_use - power up VCE
A
Alex Deucher 已提交
351
 *
352
 * @ring: amdgpu ring
A
Alex Deucher 已提交
353 354 355
 *
 * Make sure VCE is powerd up when we want to use it
 */
356
void amdgpu_vce_ring_begin_use(struct amdgpu_ring *ring)
A
Alex Deucher 已提交
357
{
358 359
	struct amdgpu_device *adev = ring->adev;
	bool set_clocks;
A
Alex Deucher 已提交
360

361 362 363
	if (amdgpu_sriov_vf(adev))
		return;

364 365
	mutex_lock(&adev->vce.idle_mutex);
	set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
366
	if (set_clocks) {
A
Alex Deucher 已提交
367 368 369 370
		if (adev->pm.dpm_enabled) {
			amdgpu_dpm_enable_vce(adev, true);
		} else {
			amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
371 372 373 374
			amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							       AMD_CG_STATE_UNGATE);
			amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_VCE,
							       AMD_PG_STATE_UNGATE);
375

A
Alex Deucher 已提交
376 377
		}
	}
378 379 380 381 382 383 384 385 386 387 388 389
	mutex_unlock(&adev->vce.idle_mutex);
}

/**
 * amdgpu_vce_ring_end_use - power VCE down
 *
 * @ring: amdgpu ring
 *
 * Schedule work to power VCE down again
 */
void amdgpu_vce_ring_end_use(struct amdgpu_ring *ring)
{
390 391
	if (!amdgpu_sriov_vf(ring->adev))
		schedule_delayed_work(&ring->adev->vce.idle_work, VCE_IDLE_TIMEOUT);
A
Alex Deucher 已提交
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
}

/**
 * amdgpu_vce_free_handles - free still open VCE handles
 *
 * @adev: amdgpu_device pointer
 * @filp: drm file pointer
 *
 * Close all VCE handles still open by this file pointer
 */
void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
{
	struct amdgpu_ring *ring = &adev->vce.ring[0];
	int i, r;
	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
		uint32_t handle = atomic_read(&adev->vce.handles[i]);
408

A
Alex Deucher 已提交
409 410 411
		if (!handle || adev->vce.filp[i] != filp)
			continue;

412
		r = amdgpu_vce_get_destroy_msg(ring, handle, false, NULL);
A
Alex Deucher 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
		if (r)
			DRM_ERROR("Error destroying VCE handle (%d)!\n", r);

		adev->vce.filp[i] = NULL;
		atomic_set(&adev->vce.handles[i], 0);
	}
}

/**
 * amdgpu_vce_get_create_msg - generate a VCE create msg
 *
 * @adev: amdgpu_device pointer
 * @ring: ring we should submit the msg to
 * @handle: VCE session handle to use
 * @fence: optional fence to return
 *
 * Open up a stream for HW test
 */
int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
432
			      struct dma_fence **fence)
A
Alex Deucher 已提交
433 434
{
	const unsigned ib_size_dw = 1024;
435 436
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
437
	struct dma_fence *f = NULL;
A
Alex Deucher 已提交
438 439 440
	uint64_t dummy;
	int i, r;

441 442
	r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
	if (r)
A
Alex Deucher 已提交
443
		return r;
444 445

	ib = &job->ibs[0];
A
Alex Deucher 已提交
446

447
	dummy = ib->gpu_addr + 1024;
A
Alex Deucher 已提交
448 449

	/* stitch together an VCE create msg */
450 451 452 453 454
	ib->length_dw = 0;
	ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
	ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
	ib->ptr[ib->length_dw++] = handle;

455 456 457 458
	if ((ring->adev->vce.fw_version >> 24) >= 52)
		ib->ptr[ib->length_dw++] = 0x00000040; /* len */
	else
		ib->ptr[ib->length_dw++] = 0x00000030; /* len */
459 460 461 462 463 464 465 466 467 468 469
	ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
	ib->ptr[ib->length_dw++] = 0x00000000;
	ib->ptr[ib->length_dw++] = 0x00000042;
	ib->ptr[ib->length_dw++] = 0x0000000a;
	ib->ptr[ib->length_dw++] = 0x00000001;
	ib->ptr[ib->length_dw++] = 0x00000080;
	ib->ptr[ib->length_dw++] = 0x00000060;
	ib->ptr[ib->length_dw++] = 0x00000100;
	ib->ptr[ib->length_dw++] = 0x00000100;
	ib->ptr[ib->length_dw++] = 0x0000000c;
	ib->ptr[ib->length_dw++] = 0x00000000;
470 471 472 473 474 475
	if ((ring->adev->vce.fw_version >> 24) >= 52) {
		ib->ptr[ib->length_dw++] = 0x00000000;
		ib->ptr[ib->length_dw++] = 0x00000000;
		ib->ptr[ib->length_dw++] = 0x00000000;
		ib->ptr[ib->length_dw++] = 0x00000000;
	}
476 477 478 479 480 481 482 483 484 485

	ib->ptr[ib->length_dw++] = 0x00000014; /* len */
	ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
	ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
	ib->ptr[ib->length_dw++] = dummy;
	ib->ptr[ib->length_dw++] = 0x00000001;

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

486
	r = amdgpu_job_submit_direct(job, ring, &f);
487 488
	if (r)
		goto err;
489

A
Alex Deucher 已提交
490
	if (fence)
491 492
		*fence = dma_fence_get(f);
	dma_fence_put(f);
493
	return 0;
494

495
err:
496
	amdgpu_job_free(job);
A
Alex Deucher 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510
	return r;
}

/**
 * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
 *
 * @adev: amdgpu_device pointer
 * @ring: ring we should submit the msg to
 * @handle: VCE session handle to use
 * @fence: optional fence to return
 *
 * Close up a stream for HW test or if userspace failed to do so
 */
int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
511
			       bool direct, struct dma_fence **fence)
A
Alex Deucher 已提交
512 513
{
	const unsigned ib_size_dw = 1024;
514 515
	struct amdgpu_job *job;
	struct amdgpu_ib *ib;
516
	struct dma_fence *f = NULL;
A
Alex Deucher 已提交
517 518
	int i, r;

519 520
	r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
	if (r)
A
Alex Deucher 已提交
521 522
		return r;

523
	ib = &job->ibs[0];
A
Alex Deucher 已提交
524 525

	/* stitch together an VCE destroy msg */
526 527 528 529 530
	ib->length_dw = 0;
	ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
	ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
	ib->ptr[ib->length_dw++] = handle;

531 532 533 534 535 536 537 538
	ib->ptr[ib->length_dw++] = 0x00000020; /* len */
	ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
	ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
	ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
	ib->ptr[ib->length_dw++] = 0x00000000;
	ib->ptr[ib->length_dw++] = 0x00000000;
	ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
	ib->ptr[ib->length_dw++] = 0x00000000;
539 540 541 542 543 544

	ib->ptr[ib->length_dw++] = 0x00000008; /* len */
	ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */

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

546 547 548
	if (direct)
		r = amdgpu_job_submit_direct(job, ring, &f);
	else
549
		r = amdgpu_job_submit(job, &ring->adev->vce.entity,
550
				      AMDGPU_FENCE_OWNER_UNDEFINED, &f);
551 552
	if (r)
		goto err;
553

A
Alex Deucher 已提交
554
	if (fence)
555 556
		*fence = dma_fence_get(f);
	dma_fence_put(f);
557
	return 0;
558

559
err:
560
	amdgpu_job_free(job);
A
Alex Deucher 已提交
561 562 563
	return r;
}

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
/**
 * amdgpu_vce_cs_validate_bo - make sure not to cross 4GB boundary
 *
 * @p: parser context
 * @lo: address of lower dword
 * @hi: address of higher dword
 * @size: minimum size
 * @index: bs/fb index
 *
 * Make sure that no BO cross a 4GB boundary.
 */
static int amdgpu_vce_validate_bo(struct amdgpu_cs_parser *p, uint32_t ib_idx,
				  int lo, int hi, unsigned size, int32_t index)
{
	int64_t offset = ((uint64_t)size) * ((int64_t)index);
579
	struct ttm_operation_ctx ctx = { false, false };
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
	struct amdgpu_bo_va_mapping *mapping;
	unsigned i, fpfn, lpfn;
	struct amdgpu_bo *bo;
	uint64_t addr;
	int r;

	addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
	       ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
	if (index >= 0) {
		addr += offset;
		fpfn = PAGE_ALIGN(offset) >> PAGE_SHIFT;
		lpfn = 0x100000000ULL >> PAGE_SHIFT;
	} else {
		fpfn = 0;
		lpfn = (0x100000000ULL - PAGE_ALIGN(offset)) >> PAGE_SHIFT;
	}

	r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
	if (r) {
		DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
			  addr, lo, hi, size, index);
		return r;
	}

	for (i = 0; i < bo->placement.num_placement; ++i) {
		bo->placements[i].fpfn = max(bo->placements[i].fpfn, fpfn);
606 607
		bo->placements[i].lpfn = bo->placements[i].lpfn ?
			min(bo->placements[i].lpfn, lpfn) : lpfn;
608
	}
609
	return ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
610 611 612
}


A
Alex Deucher 已提交
613 614 615 616 617 618
/**
 * amdgpu_vce_cs_reloc - command submission relocation
 *
 * @p: parser context
 * @lo: address of lower dword
 * @hi: address of higher dword
619
 * @size: minimum size
A
Alex Deucher 已提交
620 621 622
 *
 * Patch relocation inside command stream with real buffer address
 */
623
static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
624
			       int lo, int hi, unsigned size, uint32_t index)
A
Alex Deucher 已提交
625 626 627 628
{
	struct amdgpu_bo_va_mapping *mapping;
	struct amdgpu_bo *bo;
	uint64_t addr;
629
	int r;
A
Alex Deucher 已提交
630

631 632 633
	if (index == 0xffffffff)
		index = 0;

A
Alex Deucher 已提交
634 635
	addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
	       ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
636
	addr += ((uint64_t)size) * ((uint64_t)index);
A
Alex Deucher 已提交
637

638 639
	r = amdgpu_cs_find_mapping(p, addr, &bo, &mapping);
	if (r) {
640 641
		DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
			  addr, lo, hi, size, index);
642
		return r;
A
Alex Deucher 已提交
643 644
	}

645
	if ((addr + (uint64_t)size) >
646
	    (mapping->last + 1) * AMDGPU_GPU_PAGE_SIZE) {
647 648 649 650 651
		DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
			  addr, lo, hi);
		return -EINVAL;
	}

652
	addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
A
Alex Deucher 已提交
653
	addr += amdgpu_bo_gpu_offset(bo);
654
	addr -= ((uint64_t)size) * ((uint64_t)index);
A
Alex Deucher 已提交
655

656 657
	amdgpu_set_ib_value(p, ib_idx, lo, lower_32_bits(addr));
	amdgpu_set_ib_value(p, ib_idx, hi, upper_32_bits(addr));
A
Alex Deucher 已提交
658 659 660 661

	return 0;
}

662 663 664 665 666
/**
 * amdgpu_vce_validate_handle - validate stream handle
 *
 * @p: parser context
 * @handle: handle to validate
667
 * @allocated: allocated a new handle?
668 669 670 671 672
 *
 * Validates the handle and return the found session index or -EINVAL
 * we we don't have another free session index.
 */
static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
673
				      uint32_t handle, uint32_t *allocated)
674 675 676 677 678
{
	unsigned i;

	/* validate the handle */
	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
679 680 681 682 683
		if (atomic_read(&p->adev->vce.handles[i]) == handle) {
			if (p->adev->vce.filp[i] != p->filp) {
				DRM_ERROR("VCE handle collision detected!\n");
				return -EINVAL;
			}
684
			return i;
685
		}
686 687 688 689 690 691 692
	}

	/* handle not found try to alloc a new one */
	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
		if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
			p->adev->vce.filp[i] = p->filp;
			p->adev->vce.img_size[i] = 0;
693
			*allocated |= 1 << i;
694 695 696 697 698 699 700 701
			return i;
		}
	}

	DRM_ERROR("No more free VCE handles!\n");
	return -EINVAL;
}

A
Alex Deucher 已提交
702 703 704 705 706 707 708 709
/**
 * amdgpu_vce_cs_parse - parse and validate the command stream
 *
 * @p: parser context
 *
 */
int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
{
710
	struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
711
	unsigned fb_idx = 0, bs_idx = 0;
712
	int session_idx = -1;
713 714 715
	uint32_t destroyed = 0;
	uint32_t created = 0;
	uint32_t allocated = 0;
716 717
	uint32_t tmp, handle = 0;
	uint32_t *size = &tmp;
718 719
	unsigned idx;
	int i, r = 0;
720

721 722 723
	p->job->vm = NULL;
	ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);

724
	for (idx = 0; idx < ib->length_dw;) {
A
Alex Deucher 已提交
725 726 727 728 729
		uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
		uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);

		if ((len < 8) || (len & 3)) {
			DRM_ERROR("invalid VCE command length (%d)!\n", len);
730 731
			r = -EINVAL;
			goto out;
A
Alex Deucher 已提交
732 733
		}

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
		switch (cmd) {
		case 0x00000002: /* task info */
			fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
			bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
			break;

		case 0x03000001: /* encode */
			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 10,
						   idx + 9, 0, 0);
			if (r)
				goto out;

			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 12,
						   idx + 11, 0, 0);
			if (r)
				goto out;
			break;

		case 0x05000001: /* context buffer */
			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3,
						   idx + 2, 0, 0);
			if (r)
				goto out;
			break;

		case 0x05000004: /* video bitstream buffer */
			tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
						   tmp, bs_idx);
			if (r)
				goto out;
			break;

		case 0x05000005: /* feedback buffer */
			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3, idx + 2,
						   4096, fb_idx);
			if (r)
				goto out;
			break;
773 774 775 776 777 778 779 780 781 782 783 784

		case 0x0500000d: /* MV buffer */
			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 3,
							idx + 2, 0, 0);
			if (r)
				goto out;

			r = amdgpu_vce_validate_bo(p, ib_idx, idx + 8,
							idx + 7, 0, 0);
			if (r)
				goto out;
			break;
785 786 787 788 789 790 791 792 793
		}

		idx += len / 4;
	}

	for (idx = 0; idx < ib->length_dw;) {
		uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
		uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);

A
Alex Deucher 已提交
794
		switch (cmd) {
795
		case 0x00000001: /* session */
A
Alex Deucher 已提交
796
			handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
797 798
			session_idx = amdgpu_vce_validate_handle(p, handle,
								 &allocated);
799 800 801 802
			if (session_idx < 0) {
				r = session_idx;
				goto out;
			}
803
			size = &p->adev->vce.img_size[session_idx];
A
Alex Deucher 已提交
804 805
			break;

806
		case 0x00000002: /* task info */
807 808
			fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
			bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
809 810
			break;

811
		case 0x01000001: /* create */
812 813 814 815 816 817
			created |= 1 << session_idx;
			if (destroyed & (1 << session_idx)) {
				destroyed &= ~(1 << session_idx);
				allocated |= 1 << session_idx;

			} else if (!(allocated & (1 << session_idx))) {
818 819 820 821 822
				DRM_ERROR("Handle already in use!\n");
				r = -EINVAL;
				goto out;
			}

823 824 825 826 827
			*size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
				amdgpu_get_ib_value(p, ib_idx, idx + 10) *
				8 * 3 / 2;
			break;

828 829 830 831 832 833 834
		case 0x04000001: /* config extension */
		case 0x04000002: /* pic control */
		case 0x04000005: /* rate control */
		case 0x04000007: /* motion estimation */
		case 0x04000008: /* rdo */
		case 0x04000009: /* vui */
		case 0x05000002: /* auxiliary buffer */
835
		case 0x05000009: /* clock table */
A
Alex Deucher 已提交
836 837
			break;

838 839 840 841 842 843 844 845 846 847 848 849 850 851
		case 0x0500000c: /* hw config */
			switch (p->adev->asic_type) {
#ifdef CONFIG_DRM_AMDGPU_CIK
			case CHIP_KAVERI:
			case CHIP_MULLINS:
#endif
			case CHIP_CARRIZO:
				break;
			default:
				r = -EINVAL;
				goto out;
			}
			break;

852
		case 0x03000001: /* encode */
853
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
854
						*size, 0);
A
Alex Deucher 已提交
855
			if (r)
856
				goto out;
A
Alex Deucher 已提交
857

858
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
859
						*size / 3, 0);
A
Alex Deucher 已提交
860
			if (r)
861
				goto out;
A
Alex Deucher 已提交
862 863
			break;

864
		case 0x02000001: /* destroy */
865
			destroyed |= 1 << session_idx;
A
Alex Deucher 已提交
866 867
			break;

868
		case 0x05000001: /* context buffer */
869
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
870
						*size * 2, 0);
871
			if (r)
872
				goto out;
873 874
			break;

875
		case 0x05000004: /* video bitstream buffer */
876 877
			tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
878
						tmp, bs_idx);
879
			if (r)
880
				goto out;
881 882
			break;

883
		case 0x05000005: /* feedback buffer */
884
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
885
						4096, fb_idx);
A
Alex Deucher 已提交
886
			if (r)
887
				goto out;
A
Alex Deucher 已提交
888 889
			break;

890 891 892 893 894 895 896 897 898 899 900 901
		case 0x0500000d: /* MV buffer */
			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3,
							idx + 2, *size, 0);
			if (r)
				goto out;

			r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 8,
							idx + 7, *size / 12, 0);
			if (r)
				goto out;
			break;

A
Alex Deucher 已提交
902 903
		default:
			DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
904 905
			r = -EINVAL;
			goto out;
A
Alex Deucher 已提交
906 907
		}

908 909
		if (session_idx == -1) {
			DRM_ERROR("no session command at start of IB\n");
910 911
			r = -EINVAL;
			goto out;
912 913
		}

A
Alex Deucher 已提交
914 915 916
		idx += len / 4;
	}

917
	if (allocated & ~created) {
918 919 920 921 922
		DRM_ERROR("New session without create command!\n");
		r = -ENOENT;
	}

out:
923 924 925 926 927 928
	if (!r) {
		/* No error, free all destroyed handle slots */
		tmp = destroyed;
	} else {
		/* Error during parsing, free all allocated handle slots */
		tmp = allocated;
A
Alex Deucher 已提交
929 930
	}

931 932 933 934
	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
		if (tmp & (1 << i))
			atomic_set(&p->adev->vce.handles[i], 0);

935
	return r;
A
Alex Deucher 已提交
936 937
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
/**
 * amdgpu_vce_cs_parse_vm - parse the command stream in VM mode
 *
 * @p: parser context
 *
 */
int amdgpu_vce_ring_parse_cs_vm(struct amdgpu_cs_parser *p, uint32_t ib_idx)
{
	struct amdgpu_ib *ib = &p->job->ibs[ib_idx];
	int session_idx = -1;
	uint32_t destroyed = 0;
	uint32_t created = 0;
	uint32_t allocated = 0;
	uint32_t tmp, handle = 0;
	int i, r = 0, idx = 0;

	while (idx < ib->length_dw) {
		uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
		uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);

		if ((len < 8) || (len & 3)) {
			DRM_ERROR("invalid VCE command length (%d)!\n", len);
			r = -EINVAL;
			goto out;
		}

		switch (cmd) {
		case 0x00000001: /* session */
			handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
			session_idx = amdgpu_vce_validate_handle(p, handle,
								 &allocated);
			if (session_idx < 0) {
				r = session_idx;
				goto out;
			}
			break;

		case 0x01000001: /* create */
			created |= 1 << session_idx;
			if (destroyed & (1 << session_idx)) {
				destroyed &= ~(1 << session_idx);
				allocated |= 1 << session_idx;

			} else if (!(allocated & (1 << session_idx))) {
				DRM_ERROR("Handle already in use!\n");
				r = -EINVAL;
				goto out;
			}

			break;

		case 0x02000001: /* destroy */
			destroyed |= 1 << session_idx;
			break;

		default:
			break;
		}

		if (session_idx == -1) {
			DRM_ERROR("no session command at start of IB\n");
			r = -EINVAL;
			goto out;
		}

		idx += len / 4;
	}

	if (allocated & ~created) {
		DRM_ERROR("New session without create command!\n");
		r = -ENOENT;
	}

out:
	if (!r) {
		/* No error, free all destroyed handle slots */
		tmp = destroyed;
		amdgpu_ib_free(p->adev, ib, NULL);
	} else {
		/* Error during parsing, free all allocated handle slots */
		tmp = allocated;
	}

	for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
		if (tmp & (1 << i))
			atomic_set(&p->adev->vce.handles[i], 0);

	return r;
}

A
Alex Deucher 已提交
1028 1029 1030 1031 1032 1033 1034
/**
 * amdgpu_vce_ring_emit_ib - execute indirect buffer
 *
 * @ring: engine to use
 * @ib: the IB to execute
 *
 */
1035
void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib,
1036
			     unsigned vmid, bool ctx_switch)
A
Alex Deucher 已提交
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
{
	amdgpu_ring_write(ring, VCE_CMD_IB);
	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);
}

/**
 * amdgpu_vce_ring_emit_fence - add a fence command to the ring
 *
 * @ring: engine to use
 * @fence: the fence
 *
 */
void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
1052
				unsigned flags)
A
Alex Deucher 已提交
1053
{
1054
	WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
A
Alex Deucher 已提交
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074

	amdgpu_ring_write(ring, VCE_CMD_FENCE);
	amdgpu_ring_write(ring, addr);
	amdgpu_ring_write(ring, upper_32_bits(addr));
	amdgpu_ring_write(ring, seq);
	amdgpu_ring_write(ring, VCE_CMD_TRAP);
	amdgpu_ring_write(ring, VCE_CMD_END);
}

/**
 * amdgpu_vce_ring_test_ring - test if VCE ring is working
 *
 * @ring: the engine to test on
 *
 */
int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
{
	struct amdgpu_device *adev = ring->adev;
	uint32_t rptr = amdgpu_ring_get_rptr(ring);
	unsigned i;
1075 1076
	int r, timeout = adev->usec_timeout;

1077
	/* skip ring test for sriov*/
1078
	if (amdgpu_sriov_vf(adev))
1079
		return 0;
A
Alex Deucher 已提交
1080

1081
	r = amdgpu_ring_alloc(ring, 16);
A
Alex Deucher 已提交
1082 1083 1084 1085 1086 1087
	if (r) {
		DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
			  ring->idx, r);
		return r;
	}
	amdgpu_ring_write(ring, VCE_CMD_END);
1088
	amdgpu_ring_commit(ring);
A
Alex Deucher 已提交
1089

1090
	for (i = 0; i < timeout; i++) {
A
Alex Deucher 已提交
1091 1092 1093 1094 1095
		if (amdgpu_ring_get_rptr(ring) != rptr)
			break;
		DRM_UDELAY(1);
	}

1096
	if (i < timeout) {
1097
		DRM_DEBUG("ring test on %d succeeded in %d usecs\n",
A
Alex Deucher 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
			 ring->idx, i);
	} else {
		DRM_ERROR("amdgpu: ring %d test failed\n",
			  ring->idx);
		r = -ETIMEDOUT;
	}

	return r;
}

/**
 * amdgpu_vce_ring_test_ib - test if VCE IBs are working
 *
 * @ring: the engine to test on
 *
 */
1114
int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring, long timeout)
A
Alex Deucher 已提交
1115
{
1116
	struct dma_fence *fence = NULL;
1117
	long r;
A
Alex Deucher 已提交
1118

1119 1120
	/* skip vce ring1/2 ib test for now, since it's not reliable */
	if (ring != &ring->adev->vce.ring[0])
1121 1122
		return 0;

A
Alex Deucher 已提交
1123 1124
	r = amdgpu_vce_get_create_msg(ring, 1, NULL);
	if (r) {
1125
		DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
A
Alex Deucher 已提交
1126 1127 1128
		goto error;
	}

1129
	r = amdgpu_vce_get_destroy_msg(ring, 1, true, &fence);
A
Alex Deucher 已提交
1130
	if (r) {
1131
		DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
A
Alex Deucher 已提交
1132 1133 1134
		goto error;
	}

1135
	r = dma_fence_wait_timeout(fence, false, timeout);
1136 1137 1138 1139 1140
	if (r == 0) {
		DRM_ERROR("amdgpu: IB test timed out.\n");
		r = -ETIMEDOUT;
	} else if (r < 0) {
		DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
A
Alex Deucher 已提交
1141
	} else {
1142
		DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
1143
		r = 0;
A
Alex Deucher 已提交
1144 1145
	}
error:
1146
	dma_fence_put(fence);
A
Alex Deucher 已提交
1147 1148
	return r;
}