13f012e29Smrg/*
23f012e29Smrg * Copyright 2015 Advanced Micro Devices, Inc.
33f012e29Smrg *
43f012e29Smrg * Permission is hereby granted, free of charge, to any person obtaining a
53f012e29Smrg * copy of this software and associated documentation files (the "Software"),
63f012e29Smrg * to deal in the Software without restriction, including without limitation
73f012e29Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83f012e29Smrg * and/or sell copies of the Software, and to permit persons to whom the
93f012e29Smrg * Software is furnished to do so, subject to the following conditions:
103f012e29Smrg *
113f012e29Smrg * The above copyright notice and this permission notice shall be included in
123f012e29Smrg * all copies or substantial portions of the Software.
133f012e29Smrg *
143f012e29Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
153f012e29Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163f012e29Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
173f012e29Smrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
183f012e29Smrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
193f012e29Smrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
203f012e29Smrg * OTHER DEALINGS IN THE SOFTWARE.
213f012e29Smrg *
223f012e29Smrg*/
233f012e29Smrg
243f012e29Smrg#include <stdio.h>
253f012e29Smrg#include <inttypes.h>
263f012e29Smrg
273f012e29Smrg#include "CUnit/Basic.h"
283f012e29Smrg
293f012e29Smrg#include "util_math.h"
303f012e29Smrg
313f012e29Smrg#include "amdgpu_test.h"
323f012e29Smrg#include "amdgpu_drm.h"
333f012e29Smrg#include "amdgpu_internal.h"
343f012e29Smrg
353f012e29Smrg#include "vce_ib.h"
363f012e29Smrg#include "frame.h"
373f012e29Smrg
383f012e29Smrg#define IB_SIZE		4096
393f012e29Smrg#define MAX_RESOURCES	16
407cdc0497Smrg#define FW_53_0_03 ((53 << 24) | (0 << 16) | (03 << 8))
413f012e29Smrg
423f012e29Smrgstruct amdgpu_vce_bo {
433f012e29Smrg	amdgpu_bo_handle handle;
443f012e29Smrg	amdgpu_va_handle va_handle;
453f012e29Smrg	uint64_t addr;
463f012e29Smrg	uint64_t size;
473f012e29Smrg	uint8_t *ptr;
483f012e29Smrg};
493f012e29Smrg
503f012e29Smrgstruct amdgpu_vce_encode {
513f012e29Smrg	unsigned width;
523f012e29Smrg	unsigned height;
533f012e29Smrg	struct amdgpu_vce_bo vbuf;
543f012e29Smrg	struct amdgpu_vce_bo bs[2];
553f012e29Smrg	struct amdgpu_vce_bo fb[2];
563f012e29Smrg	struct amdgpu_vce_bo cpb;
573f012e29Smrg	unsigned ib_len;
583f012e29Smrg	bool two_instance;
597cdc0497Smrg	struct amdgpu_vce_bo mvrefbuf;
607cdc0497Smrg	struct amdgpu_vce_bo mvb;
617cdc0497Smrg	unsigned mvbuf_size;
623f012e29Smrg};
633f012e29Smrg
643f012e29Smrgstatic amdgpu_device_handle device_handle;
653f012e29Smrgstatic uint32_t major_version;
663f012e29Smrgstatic uint32_t minor_version;
673f012e29Smrgstatic uint32_t family_id;
683f012e29Smrgstatic uint32_t vce_harvest_config;
697cdc0497Smrgstatic uint32_t chip_rev;
707cdc0497Smrgstatic uint32_t chip_id;
717cdc0497Smrgstatic uint32_t ids_flags;
727cdc0497Smrgstatic bool is_mv_supported = true;
733f012e29Smrg
743f012e29Smrgstatic amdgpu_context_handle context_handle;
753f012e29Smrgstatic amdgpu_bo_handle ib_handle;
763f012e29Smrgstatic amdgpu_va_handle ib_va_handle;
773f012e29Smrgstatic uint64_t ib_mc_address;
783f012e29Smrgstatic uint32_t *ib_cpu;
793f012e29Smrg
803f012e29Smrgstatic struct amdgpu_vce_encode enc;
813f012e29Smrgstatic amdgpu_bo_handle resources[MAX_RESOURCES];
823f012e29Smrgstatic unsigned num_resources;
833f012e29Smrg
843f012e29Smrgstatic void amdgpu_cs_vce_create(void);
853f012e29Smrgstatic void amdgpu_cs_vce_encode(void);
867cdc0497Smrgstatic void amdgpu_cs_vce_encode_mv(void);
873f012e29Smrgstatic void amdgpu_cs_vce_destroy(void);
883f012e29Smrg
893f012e29SmrgCU_TestInfo vce_tests[] = {
903f012e29Smrg	{ "VCE create",  amdgpu_cs_vce_create },
913f012e29Smrg	{ "VCE encode",  amdgpu_cs_vce_encode },
927cdc0497Smrg	{ "VCE MV dump",  amdgpu_cs_vce_encode_mv },
933f012e29Smrg	{ "VCE destroy",  amdgpu_cs_vce_destroy },
943f012e29Smrg	CU_TEST_INFO_NULL,
953f012e29Smrg};
963f012e29Smrg
9700a23bdaSmrgCU_BOOL suite_vce_tests_enable(void)
9800a23bdaSmrg{
990ed5401bSmrg	uint32_t version, feature;
1007cdc0497Smrg	CU_BOOL ret_mv = CU_FALSE;
1017cdc0497Smrg
10200a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
10300a23bdaSmrg					     &minor_version, &device_handle))
10400a23bdaSmrg		return CU_FALSE;
10500a23bdaSmrg
10600a23bdaSmrg	family_id = device_handle->info.family_id;
1077cdc0497Smrg	chip_rev = device_handle->info.chip_rev;
1087cdc0497Smrg	chip_id = device_handle->info.chip_external_rev;
1097cdc0497Smrg	ids_flags = device_handle->info.ids_flags;
1107cdc0497Smrg
1117cdc0497Smrg	amdgpu_query_firmware_version(device_handle, AMDGPU_INFO_FW_VCE, 0,
1127cdc0497Smrg					  0, &version, &feature);
11300a23bdaSmrg
11400a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
11500a23bdaSmrg		return CU_FALSE;
11600a23bdaSmrg
11741687f09Smrg	if (family_id >= AMDGPU_FAMILY_RV || family_id == AMDGPU_FAMILY_SI ||
1184babd585Smrg		asic_is_gfx_pipe_removed(family_id, chip_id, chip_rev)) {
11900a23bdaSmrg		printf("\n\nThe ASIC NOT support VCE, suite disabled\n");
12000a23bdaSmrg		return CU_FALSE;
12100a23bdaSmrg	}
12200a23bdaSmrg
1237cdc0497Smrg	if (!(chip_id == (chip_rev + 0x3C) || /* FIJI */
1247cdc0497Smrg			chip_id == (chip_rev + 0x50) || /* Polaris 10*/
1257cdc0497Smrg			chip_id == (chip_rev + 0x5A) || /* Polaris 11*/
1267cdc0497Smrg			chip_id == (chip_rev + 0x64) || /* Polaris 12*/
1277cdc0497Smrg			(family_id >= AMDGPU_FAMILY_AI && !ids_flags))) /* dGPU > Polaris */
1287cdc0497Smrg		printf("\n\nThe ASIC NOT support VCE MV, suite disabled\n");
1297cdc0497Smrg	else if (FW_53_0_03 > version)
1307cdc0497Smrg		printf("\n\nThe ASIC FW version NOT support VCE MV, suite disabled\n");
1317cdc0497Smrg	else
1327cdc0497Smrg		ret_mv = CU_TRUE;
1337cdc0497Smrg
1347cdc0497Smrg	if (ret_mv == CU_FALSE) {
1357cdc0497Smrg		amdgpu_set_test_active("VCE Tests", "VCE MV dump", ret_mv);
1367cdc0497Smrg		is_mv_supported = false;
1377cdc0497Smrg	}
1387cdc0497Smrg
13900a23bdaSmrg	return CU_TRUE;
14000a23bdaSmrg}
14100a23bdaSmrg
1423f012e29Smrgint suite_vce_tests_init(void)
1433f012e29Smrg{
1443f012e29Smrg	int r;
1453f012e29Smrg
1463f012e29Smrg	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
1473f012e29Smrg				     &minor_version, &device_handle);
148037b3c26Smrg	if (r) {
149037b3c26Smrg		if ((r == -EACCES) && (errno == EACCES))
150037b3c26Smrg			printf("\n\nError:%s. "
151037b3c26Smrg				"Hint:Try to run this test program as root.",
152037b3c26Smrg				strerror(errno));
153037b3c26Smrg
1543f012e29Smrg		return CUE_SINIT_FAILED;
155037b3c26Smrg	}
1563f012e29Smrg
1573f012e29Smrg	family_id = device_handle->info.family_id;
1583f012e29Smrg	vce_harvest_config = device_handle->info.vce_harvest_config;
1593f012e29Smrg
1603f012e29Smrg	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
1613f012e29Smrg	if (r)
1623f012e29Smrg		return CUE_SINIT_FAILED;
1633f012e29Smrg
1643f012e29Smrg	r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
1653f012e29Smrg				    AMDGPU_GEM_DOMAIN_GTT, 0,
1663f012e29Smrg				    &ib_handle, (void**)&ib_cpu,
1673f012e29Smrg				    &ib_mc_address, &ib_va_handle);
1683f012e29Smrg	if (r)
1693f012e29Smrg		return CUE_SINIT_FAILED;
1703f012e29Smrg
1713f012e29Smrg	memset(&enc, 0, sizeof(struct amdgpu_vce_encode));
1723f012e29Smrg
1733f012e29Smrg	return CUE_SUCCESS;
1743f012e29Smrg}
1753f012e29Smrg
1763f012e29Smrgint suite_vce_tests_clean(void)
1773f012e29Smrg{
1783f012e29Smrg	int r;
1793f012e29Smrg
18000a23bdaSmrg	r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
18100a23bdaSmrg				     ib_mc_address, IB_SIZE);
18200a23bdaSmrg	if (r)
18300a23bdaSmrg		return CUE_SCLEAN_FAILED;
18400a23bdaSmrg
18500a23bdaSmrg	r = amdgpu_cs_ctx_free(context_handle);
18600a23bdaSmrg	if (r)
18700a23bdaSmrg		return CUE_SCLEAN_FAILED;
18800a23bdaSmrg
18900a23bdaSmrg	r = amdgpu_device_deinitialize(device_handle);
19000a23bdaSmrg	if (r)
19100a23bdaSmrg		return CUE_SCLEAN_FAILED;
1923f012e29Smrg
1933f012e29Smrg	return CUE_SUCCESS;
1943f012e29Smrg}
1953f012e29Smrg
1963f012e29Smrgstatic int submit(unsigned ndw, unsigned ip)
1973f012e29Smrg{
1983f012e29Smrg	struct amdgpu_cs_request ibs_request = {0};
1993f012e29Smrg	struct amdgpu_cs_ib_info ib_info = {0};
2003f012e29Smrg	struct amdgpu_cs_fence fence_status = {0};
2013f012e29Smrg	uint32_t expired;
2023f012e29Smrg	int r;
2033f012e29Smrg
2043f012e29Smrg	ib_info.ib_mc_address = ib_mc_address;
2053f012e29Smrg	ib_info.size = ndw;
2063f012e29Smrg
2073f012e29Smrg	ibs_request.ip_type = ip;
2083f012e29Smrg
2093f012e29Smrg	r = amdgpu_bo_list_create(device_handle, num_resources, resources,
2103f012e29Smrg				  NULL, &ibs_request.resources);
2113f012e29Smrg	if (r)
2123f012e29Smrg		return r;
2133f012e29Smrg
2143f012e29Smrg	ibs_request.number_of_ibs = 1;
2153f012e29Smrg	ibs_request.ibs = &ib_info;
2163f012e29Smrg	ibs_request.fence_info.handle = NULL;
2173f012e29Smrg
2183f012e29Smrg	r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
2193f012e29Smrg	if (r)
2203f012e29Smrg		return r;
2213f012e29Smrg
2223f012e29Smrg	r = amdgpu_bo_list_destroy(ibs_request.resources);
2233f012e29Smrg	if (r)
2243f012e29Smrg		return r;
2253f012e29Smrg
2263f012e29Smrg	fence_status.context = context_handle;
2273f012e29Smrg	fence_status.ip_type = ip;
2283f012e29Smrg	fence_status.fence = ibs_request.seq_no;
2293f012e29Smrg
2303f012e29Smrg	r = amdgpu_cs_query_fence_status(&fence_status,
2313f012e29Smrg					 AMDGPU_TIMEOUT_INFINITE,
2323f012e29Smrg					 0, &expired);
2333f012e29Smrg	if (r)
2343f012e29Smrg		return r;
2353f012e29Smrg
2363f012e29Smrg	return 0;
2373f012e29Smrg}
2383f012e29Smrg
2393f012e29Smrgstatic void alloc_resource(struct amdgpu_vce_bo *vce_bo, unsigned size, unsigned domain)
2403f012e29Smrg{
2413f012e29Smrg	struct amdgpu_bo_alloc_request req = {0};
2423f012e29Smrg	amdgpu_bo_handle buf_handle;
2433f012e29Smrg	amdgpu_va_handle va_handle;
2443f012e29Smrg	uint64_t va = 0;
2453f012e29Smrg	int r;
2463f012e29Smrg
2473f012e29Smrg	req.alloc_size = ALIGN(size, 4096);
2483f012e29Smrg	req.preferred_heap = domain;
2493f012e29Smrg	r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
2503f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2513f012e29Smrg	r = amdgpu_va_range_alloc(device_handle,
2523f012e29Smrg				  amdgpu_gpu_va_range_general,
2533f012e29Smrg				  req.alloc_size, 1, 0, &va,
2543f012e29Smrg				  &va_handle, 0);
2553f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2563f012e29Smrg	r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
2573f012e29Smrg			    AMDGPU_VA_OP_MAP);
2583f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2593f012e29Smrg	vce_bo->addr = va;
2603f012e29Smrg	vce_bo->handle = buf_handle;
2613f012e29Smrg	vce_bo->size = req.alloc_size;
2623f012e29Smrg	vce_bo->va_handle = va_handle;
2633f012e29Smrg	r = amdgpu_bo_cpu_map(vce_bo->handle, (void **)&vce_bo->ptr);
2643f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2653f012e29Smrg	memset(vce_bo->ptr, 0, size);
2663f012e29Smrg	r = amdgpu_bo_cpu_unmap(vce_bo->handle);
2673f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2683f012e29Smrg}
2693f012e29Smrg
2703f012e29Smrgstatic void free_resource(struct amdgpu_vce_bo *vce_bo)
2713f012e29Smrg{
2723f012e29Smrg	int r;
2733f012e29Smrg
2743f012e29Smrg	r = amdgpu_bo_va_op(vce_bo->handle, 0, vce_bo->size,
2753f012e29Smrg			    vce_bo->addr, 0, AMDGPU_VA_OP_UNMAP);
2763f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2773f012e29Smrg
2783f012e29Smrg	r = amdgpu_va_range_free(vce_bo->va_handle);
2793f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2803f012e29Smrg
2813f012e29Smrg	r = amdgpu_bo_free(vce_bo->handle);
2823f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2833f012e29Smrg	memset(vce_bo, 0, sizeof(*vce_bo));
2843f012e29Smrg}
2853f012e29Smrg
2863f012e29Smrgstatic void amdgpu_cs_vce_create(void)
2873f012e29Smrg{
288d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
2893f012e29Smrg	int len, r;
2903f012e29Smrg
2913f012e29Smrg	enc.width = vce_create[6];
2923f012e29Smrg	enc.height = vce_create[7];
2933f012e29Smrg
2943f012e29Smrg	num_resources  = 0;
2953f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
2963f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
2973f012e29Smrg	resources[num_resources++] = ib_handle;
2983f012e29Smrg
2993f012e29Smrg	len = 0;
3003f012e29Smrg	memcpy(ib_cpu, vce_session, sizeof(vce_session));
3013f012e29Smrg	len += sizeof(vce_session) / 4;
3023f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3033f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3043f012e29Smrg	memcpy((ib_cpu + len), vce_create, sizeof(vce_create));
305d8807b2fSmrg	ib_cpu[len + 8] = ALIGN(enc.width, align);
306d8807b2fSmrg	ib_cpu[len + 9] = ALIGN(enc.width, align);
3077cdc0497Smrg	if (is_mv_supported == true) {/* disableTwoInstance */
3087cdc0497Smrg		if (family_id >= AMDGPU_FAMILY_AI)
3097cdc0497Smrg			ib_cpu[len + 11] = 0x01000001;
3107cdc0497Smrg		else
3117cdc0497Smrg			ib_cpu[len + 11] = 0x01000201;
3127cdc0497Smrg	}
3133f012e29Smrg	len += sizeof(vce_create) / 4;
3143f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
3153f012e29Smrg	ib_cpu[len + 2] = enc.fb[0].addr >> 32;
3163f012e29Smrg	ib_cpu[len + 3] = enc.fb[0].addr;
3173f012e29Smrg	len += sizeof(vce_feedback) / 4;
3183f012e29Smrg
3193f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
3203f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
3213f012e29Smrg
3223f012e29Smrg	free_resource(&enc.fb[0]);
3233f012e29Smrg}
3243f012e29Smrg
3253f012e29Smrgstatic void amdgpu_cs_vce_config(void)
3263f012e29Smrg{
3273f012e29Smrg	int len = 0, r;
3283f012e29Smrg
3293f012e29Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
3303f012e29Smrg	len += sizeof(vce_session) / 4;
3313f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3323f012e29Smrg	ib_cpu[len + 3] = 2;
3333f012e29Smrg	ib_cpu[len + 6] = 0xffffffff;
3343f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3353f012e29Smrg	memcpy((ib_cpu + len), vce_rate_ctrl, sizeof(vce_rate_ctrl));
3363f012e29Smrg	len += sizeof(vce_rate_ctrl) / 4;
3373f012e29Smrg	memcpy((ib_cpu + len), vce_config_ext, sizeof(vce_config_ext));
3383f012e29Smrg	len += sizeof(vce_config_ext) / 4;
3393f012e29Smrg	memcpy((ib_cpu + len), vce_motion_est, sizeof(vce_motion_est));
3403f012e29Smrg	len += sizeof(vce_motion_est) / 4;
3413f012e29Smrg	memcpy((ib_cpu + len), vce_rdo, sizeof(vce_rdo));
3423f012e29Smrg	len += sizeof(vce_rdo) / 4;
3433f012e29Smrg	memcpy((ib_cpu + len), vce_pic_ctrl, sizeof(vce_pic_ctrl));
3447cdc0497Smrg	if (is_mv_supported == true)
3457cdc0497Smrg		ib_cpu[len + 27] = 0x00000001; /* encSliceMode */
3463f012e29Smrg	len += sizeof(vce_pic_ctrl) / 4;
3473f012e29Smrg
3483f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
3493f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
3503f012e29Smrg}
3513f012e29Smrg
3527cdc0497Smrgstatic void amdgpu_cs_vce_encode_idr(struct amdgpu_vce_encode *enc)
3533f012e29Smrg{
3543f012e29Smrg
3553f012e29Smrg	uint64_t luma_offset, chroma_offset;
356d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
357d8807b2fSmrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
358d8807b2fSmrg	int len = 0, i, r;
3593f012e29Smrg
3603f012e29Smrg	luma_offset = enc->vbuf.addr;
361d8807b2fSmrg	chroma_offset = luma_offset + luma_size;
3623f012e29Smrg
3633f012e29Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
3643f012e29Smrg	len += sizeof(vce_session) / 4;
3653f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3663f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3673f012e29Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
3683f012e29Smrg	ib_cpu[len + 2] = enc->bs[0].addr >> 32;
3693f012e29Smrg	ib_cpu[len + 3] = enc->bs[0].addr;
3703f012e29Smrg	len += sizeof(vce_bs_buffer) / 4;
3713f012e29Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
3723f012e29Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
3733f012e29Smrg	ib_cpu[len + 3] = enc->cpb.addr;
3743f012e29Smrg	len += sizeof(vce_context_buffer) / 4;
3753f012e29Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
376d8807b2fSmrg	for (i = 0; i <  8; ++i)
377d8807b2fSmrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
378d8807b2fSmrg	for (i = 0; i <  8; ++i)
379d8807b2fSmrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
3803f012e29Smrg	len += sizeof(vce_aux_buffer) / 4;
3813f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
3823f012e29Smrg	ib_cpu[len + 2] = enc->fb[0].addr >> 32;
3833f012e29Smrg	ib_cpu[len + 3] = enc->fb[0].addr;
3843f012e29Smrg	len += sizeof(vce_feedback) / 4;
3853f012e29Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
3863f012e29Smrg	ib_cpu[len + 9] = luma_offset >> 32;
3873f012e29Smrg	ib_cpu[len + 10] = luma_offset;
3883f012e29Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
3893f012e29Smrg	ib_cpu[len + 12] = chroma_offset;
390d8807b2fSmrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
391d8807b2fSmrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
392d8807b2fSmrg	ib_cpu[len + 73] = luma_size * 1.5;
393d8807b2fSmrg	ib_cpu[len + 74] = luma_size * 2.5;
3943f012e29Smrg	len += sizeof(vce_encode) / 4;
3953f012e29Smrg	enc->ib_len = len;
3963f012e29Smrg	if (!enc->two_instance) {
3973f012e29Smrg		r = submit(len, AMDGPU_HW_IP_VCE);
3983f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
3993f012e29Smrg	}
4003f012e29Smrg}
4013f012e29Smrg
4023f012e29Smrgstatic void amdgpu_cs_vce_encode_p(struct amdgpu_vce_encode *enc)
4033f012e29Smrg{
4043f012e29Smrg	uint64_t luma_offset, chroma_offset;
405d8807b2fSmrg	int len, i, r;
406d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
407d8807b2fSmrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
4083f012e29Smrg
4093f012e29Smrg	len = (enc->two_instance) ? enc->ib_len : 0;
4103f012e29Smrg	luma_offset = enc->vbuf.addr;
411d8807b2fSmrg	chroma_offset = luma_offset + luma_size;
4123f012e29Smrg
4133f012e29Smrg	if (!enc->two_instance) {
4143f012e29Smrg		memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
4153f012e29Smrg		len += sizeof(vce_session) / 4;
4163f012e29Smrg	}
4173f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
4183f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
4193f012e29Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
4203f012e29Smrg	ib_cpu[len + 2] = enc->bs[1].addr >> 32;
4213f012e29Smrg	ib_cpu[len + 3] = enc->bs[1].addr;
4223f012e29Smrg	len += sizeof(vce_bs_buffer) / 4;
4233f012e29Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
4243f012e29Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
4253f012e29Smrg	ib_cpu[len + 3] = enc->cpb.addr;
4263f012e29Smrg	len += sizeof(vce_context_buffer) / 4;
4273f012e29Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
428d8807b2fSmrg	for (i = 0; i <  8; ++i)
429d8807b2fSmrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
430d8807b2fSmrg	for (i = 0; i <  8; ++i)
431d8807b2fSmrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
4323f012e29Smrg	len += sizeof(vce_aux_buffer) / 4;
4333f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
4343f012e29Smrg	ib_cpu[len + 2] = enc->fb[1].addr >> 32;
4353f012e29Smrg	ib_cpu[len + 3] = enc->fb[1].addr;
4363f012e29Smrg	len += sizeof(vce_feedback) / 4;
4373f012e29Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
4383f012e29Smrg	ib_cpu[len + 2] = 0;
4393f012e29Smrg	ib_cpu[len + 9] = luma_offset >> 32;
4403f012e29Smrg	ib_cpu[len + 10] = luma_offset;
4413f012e29Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
4423f012e29Smrg	ib_cpu[len + 12] = chroma_offset;
443d8807b2fSmrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
444d8807b2fSmrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
4453f012e29Smrg	ib_cpu[len + 18] = 0;
4463f012e29Smrg	ib_cpu[len + 19] = 0;
4473f012e29Smrg	ib_cpu[len + 56] = 3;
4483f012e29Smrg	ib_cpu[len + 57] = 0;
4493f012e29Smrg	ib_cpu[len + 58] = 0;
450d8807b2fSmrg	ib_cpu[len + 59] = luma_size * 1.5;
451d8807b2fSmrg	ib_cpu[len + 60] = luma_size * 2.5;
4523f012e29Smrg	ib_cpu[len + 73] = 0;
453d8807b2fSmrg	ib_cpu[len + 74] = luma_size;
4543f012e29Smrg	ib_cpu[len + 81] = 1;
4553f012e29Smrg	ib_cpu[len + 82] = 1;
4563f012e29Smrg	len += sizeof(vce_encode) / 4;
4573f012e29Smrg
4583f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
4593f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
4603f012e29Smrg}
4613f012e29Smrg
4623f012e29Smrgstatic void check_result(struct amdgpu_vce_encode *enc)
4633f012e29Smrg{
4643f012e29Smrg	uint64_t sum;
4653f012e29Smrg	uint32_t s[2] = {180325, 15946};
4663f012e29Smrg	uint32_t *ptr, size;
4673f012e29Smrg	int i, j, r;
4683f012e29Smrg
4693f012e29Smrg	for (i = 0; i < 2; ++i) {
4703f012e29Smrg		r = amdgpu_bo_cpu_map(enc->fb[i].handle, (void **)&enc->fb[i].ptr);
4713f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4723f012e29Smrg		ptr = (uint32_t *)enc->fb[i].ptr;
4733f012e29Smrg		size = ptr[4] - ptr[9];
4743f012e29Smrg		r = amdgpu_bo_cpu_unmap(enc->fb[i].handle);
4753f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4763f012e29Smrg		r = amdgpu_bo_cpu_map(enc->bs[i].handle, (void **)&enc->bs[i].ptr);
4773f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4783f012e29Smrg		for (j = 0, sum = 0; j < size; ++j)
4793f012e29Smrg			sum += enc->bs[i].ptr[j];
4803f012e29Smrg		CU_ASSERT_EQUAL(sum, s[i]);
4813f012e29Smrg		r = amdgpu_bo_cpu_unmap(enc->bs[i].handle);
4823f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4833f012e29Smrg	}
4843f012e29Smrg}
4853f012e29Smrg
4863f012e29Smrgstatic void amdgpu_cs_vce_encode(void)
4873f012e29Smrg{
4883f012e29Smrg	uint32_t vbuf_size, bs_size = 0x154000, cpb_size;
489d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
490d8807b2fSmrg	int i, r;
4913f012e29Smrg
492d8807b2fSmrg	vbuf_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16) * 1.5;
4933f012e29Smrg	cpb_size = vbuf_size * 10;
4943f012e29Smrg	num_resources = 0;
4953f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
4963f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
4973f012e29Smrg	alloc_resource(&enc.fb[1], 4096, AMDGPU_GEM_DOMAIN_GTT);
4983f012e29Smrg	resources[num_resources++] = enc.fb[1].handle;
4993f012e29Smrg	alloc_resource(&enc.bs[0], bs_size, AMDGPU_GEM_DOMAIN_GTT);
5003f012e29Smrg	resources[num_resources++] = enc.bs[0].handle;
5013f012e29Smrg	alloc_resource(&enc.bs[1], bs_size, AMDGPU_GEM_DOMAIN_GTT);
5023f012e29Smrg	resources[num_resources++] = enc.bs[1].handle;
5033f012e29Smrg	alloc_resource(&enc.vbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
5043f012e29Smrg	resources[num_resources++] = enc.vbuf.handle;
5053f012e29Smrg	alloc_resource(&enc.cpb, cpb_size, AMDGPU_GEM_DOMAIN_VRAM);
5063f012e29Smrg	resources[num_resources++] = enc.cpb.handle;
5073f012e29Smrg	resources[num_resources++] = ib_handle;
5083f012e29Smrg
5093f012e29Smrg	r = amdgpu_bo_cpu_map(enc.vbuf.handle, (void **)&enc.vbuf.ptr);
5103f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
511d8807b2fSmrg
512d8807b2fSmrg	memset(enc.vbuf.ptr, 0, vbuf_size);
513d8807b2fSmrg	for (i = 0; i < enc.height; ++i) {
514d8807b2fSmrg		memcpy(enc.vbuf.ptr, (frame + i * enc.width), enc.width);
515d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
516d8807b2fSmrg	}
517d8807b2fSmrg	for (i = 0; i < enc.height / 2; ++i) {
518d8807b2fSmrg		memcpy(enc.vbuf.ptr, ((frame + enc.height * enc.width) + i * enc.width), enc.width);
519d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
520d8807b2fSmrg	}
521d8807b2fSmrg
5223f012e29Smrg	r = amdgpu_bo_cpu_unmap(enc.vbuf.handle);
5233f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
5243f012e29Smrg
5253f012e29Smrg	amdgpu_cs_vce_config();
5263f012e29Smrg
5273f012e29Smrg	if (family_id >= AMDGPU_FAMILY_VI) {
5283f012e29Smrg		vce_taskinfo[3] = 3;
5293f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5303f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5313f012e29Smrg		check_result(&enc);
5323f012e29Smrg
5333f012e29Smrg		/* two pipes */
5343f012e29Smrg		vce_encode[16] = 0;
5353f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5363f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5373f012e29Smrg		check_result(&enc);
5383f012e29Smrg
5393f012e29Smrg		/* two instances */
5403f012e29Smrg		if (vce_harvest_config == 0) {
5413f012e29Smrg			enc.two_instance = true;
5423f012e29Smrg			vce_taskinfo[2] = 0x83;
5433f012e29Smrg			vce_taskinfo[4] = 1;
5443f012e29Smrg			amdgpu_cs_vce_encode_idr(&enc);
5453f012e29Smrg			vce_taskinfo[2] = 0xffffffff;
5463f012e29Smrg			vce_taskinfo[4] = 2;
5473f012e29Smrg			amdgpu_cs_vce_encode_p(&enc);
5483f012e29Smrg			check_result(&enc);
5493f012e29Smrg		}
5503f012e29Smrg	} else {
5513f012e29Smrg		vce_taskinfo[3] = 3;
5523f012e29Smrg		vce_encode[16] = 0;
5533f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5543f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5553f012e29Smrg		check_result(&enc);
5563f012e29Smrg	}
5573f012e29Smrg
5583f012e29Smrg	free_resource(&enc.fb[0]);
5593f012e29Smrg	free_resource(&enc.fb[1]);
5603f012e29Smrg	free_resource(&enc.bs[0]);
5613f012e29Smrg	free_resource(&enc.bs[1]);
5623f012e29Smrg	free_resource(&enc.vbuf);
5633f012e29Smrg	free_resource(&enc.cpb);
5643f012e29Smrg}
5653f012e29Smrg
5667cdc0497Smrgstatic void amdgpu_cs_vce_mv(struct amdgpu_vce_encode *enc)
5677cdc0497Smrg{
5687cdc0497Smrg	uint64_t luma_offset, chroma_offset;
5697cdc0497Smrg	uint64_t mv_ref_luma_offset;
5707cdc0497Smrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
5717cdc0497Smrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
5727cdc0497Smrg	int len = 0, i, r;
5737cdc0497Smrg
5747cdc0497Smrg	luma_offset = enc->vbuf.addr;
5757cdc0497Smrg	chroma_offset = luma_offset + luma_size;
5767cdc0497Smrg	mv_ref_luma_offset = enc->mvrefbuf.addr;
5777cdc0497Smrg
5787cdc0497Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
5797cdc0497Smrg	len += sizeof(vce_session) / 4;
5807cdc0497Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
5817cdc0497Smrg	len += sizeof(vce_taskinfo) / 4;
5827cdc0497Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
5837cdc0497Smrg	ib_cpu[len + 2] = enc->bs[0].addr >> 32;
5847cdc0497Smrg	ib_cpu[len + 3] = enc->bs[0].addr;
5857cdc0497Smrg	len += sizeof(vce_bs_buffer) / 4;
5867cdc0497Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
5877cdc0497Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
5887cdc0497Smrg	ib_cpu[len + 3] = enc->cpb.addr;
5897cdc0497Smrg	len += sizeof(vce_context_buffer) / 4;
5907cdc0497Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
5917cdc0497Smrg	for (i = 0; i <  8; ++i)
5927cdc0497Smrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
5937cdc0497Smrg	for (i = 0; i <  8; ++i)
5947cdc0497Smrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
5957cdc0497Smrg	len += sizeof(vce_aux_buffer) / 4;
5967cdc0497Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
5977cdc0497Smrg	ib_cpu[len + 2] = enc->fb[0].addr >> 32;
5987cdc0497Smrg	ib_cpu[len + 3] = enc->fb[0].addr;
5997cdc0497Smrg	len += sizeof(vce_feedback) / 4;
6007cdc0497Smrg	memcpy((ib_cpu + len), vce_mv_buffer, sizeof(vce_mv_buffer));
6017cdc0497Smrg	ib_cpu[len + 2] = mv_ref_luma_offset >> 32;
6027cdc0497Smrg	ib_cpu[len + 3] = mv_ref_luma_offset;
6037cdc0497Smrg	ib_cpu[len + 4] = ALIGN(enc->width, align);
6047cdc0497Smrg	ib_cpu[len + 5] = ALIGN(enc->width, align);
6057cdc0497Smrg	ib_cpu[len + 6] = luma_size;
6067cdc0497Smrg	ib_cpu[len + 7] = enc->mvb.addr >> 32;
6077cdc0497Smrg	ib_cpu[len + 8] = enc->mvb.addr;
6087cdc0497Smrg	len += sizeof(vce_mv_buffer) / 4;
6097cdc0497Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
6107cdc0497Smrg	ib_cpu[len + 2] = 0;
6117cdc0497Smrg	ib_cpu[len + 3] = 0;
6127cdc0497Smrg	ib_cpu[len + 4] = 0x154000;
6137cdc0497Smrg	ib_cpu[len + 9] = luma_offset >> 32;
6147cdc0497Smrg	ib_cpu[len + 10] = luma_offset;
6157cdc0497Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
6167cdc0497Smrg	ib_cpu[len + 12] = chroma_offset;
6177cdc0497Smrg	ib_cpu[len + 13] = ALIGN(enc->height, 16);;
6187cdc0497Smrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
6197cdc0497Smrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
6207cdc0497Smrg	/* encDisableMBOffloading-encDisableTwoPipeMode-encInputPicArrayMode-encInputPicAddrMode */
6217cdc0497Smrg	ib_cpu[len + 16] = 0x01010000;
6227cdc0497Smrg	ib_cpu[len + 18] = 0; /* encPicType */
6237cdc0497Smrg	ib_cpu[len + 19] = 0; /* encIdrFlag */
6247cdc0497Smrg	ib_cpu[len + 20] = 0; /* encIdrPicId */
6257cdc0497Smrg	ib_cpu[len + 21] = 0; /* encMGSKeyPic */
6267cdc0497Smrg	ib_cpu[len + 22] = 0; /* encReferenceFlag */
6277cdc0497Smrg	ib_cpu[len + 23] = 0; /* encTemporalLayerIndex */
6287cdc0497Smrg	ib_cpu[len + 55] = 0; /* pictureStructure */
6297cdc0497Smrg	ib_cpu[len + 56] = 0; /* encPicType -ref[0] */
6307cdc0497Smrg	ib_cpu[len + 61] = 0; /* pictureStructure */
6317cdc0497Smrg	ib_cpu[len + 62] = 0; /* encPicType -ref[1] */
6327cdc0497Smrg	ib_cpu[len + 67] = 0; /* pictureStructure */
6337cdc0497Smrg	ib_cpu[len + 68] = 0; /* encPicType -ref1 */
6347cdc0497Smrg	ib_cpu[len + 81] = 1; /* frameNumber */
6357cdc0497Smrg	ib_cpu[len + 82] = 2; /* pictureOrderCount */
6367cdc0497Smrg	ib_cpu[len + 83] = 0xffffffff; /* numIPicRemainInRCGOP */
6377cdc0497Smrg	ib_cpu[len + 84] = 0xffffffff; /* numPPicRemainInRCGOP */
6387cdc0497Smrg	ib_cpu[len + 85] = 0xffffffff; /* numBPicRemainInRCGOP */
6397cdc0497Smrg	ib_cpu[len + 86] = 0xffffffff; /* numIRPicRemainInRCGOP */
6407cdc0497Smrg	ib_cpu[len + 87] = 0; /* remainedIntraRefreshPictures */
6417cdc0497Smrg	len += sizeof(vce_encode) / 4;
6427cdc0497Smrg
6437cdc0497Smrg	enc->ib_len = len;
6447cdc0497Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
6457cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6467cdc0497Smrg}
6477cdc0497Smrg
6487cdc0497Smrgstatic void check_mv_result(struct amdgpu_vce_encode *enc)
6497cdc0497Smrg{
6507cdc0497Smrg	uint64_t sum;
6517cdc0497Smrg	uint32_t s = 140790;
6525324fb0dSmrg	int j, r;
6537cdc0497Smrg
6547cdc0497Smrg	r = amdgpu_bo_cpu_map(enc->fb[0].handle, (void **)&enc->fb[0].ptr);
6557cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6567cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc->fb[0].handle);
6577cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6587cdc0497Smrg	r = amdgpu_bo_cpu_map(enc->mvb.handle, (void **)&enc->mvb.ptr);
6597cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6607cdc0497Smrg	for (j = 0, sum = 0; j < enc->mvbuf_size; ++j)
6617cdc0497Smrg		sum += enc->mvb.ptr[j];
6627cdc0497Smrg	CU_ASSERT_EQUAL(sum, s);
6637cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc->mvb.handle);
6647cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6657cdc0497Smrg}
6667cdc0497Smrg
6677cdc0497Smrgstatic void amdgpu_cs_vce_encode_mv(void)
6687cdc0497Smrg{
6697cdc0497Smrg	uint32_t vbuf_size, bs_size = 0x154000, cpb_size;
6707cdc0497Smrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
6717cdc0497Smrg	int i, r;
6727cdc0497Smrg
6737cdc0497Smrg	vbuf_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16) * 1.5;
6747cdc0497Smrg	enc.mvbuf_size = ALIGN(enc.width, 16) * ALIGN(enc.height, 16) / 8;
6757cdc0497Smrg	cpb_size = vbuf_size * 10;
6767cdc0497Smrg	num_resources = 0;
6777cdc0497Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
6787cdc0497Smrg	resources[num_resources++] = enc.fb[0].handle;
6797cdc0497Smrg	alloc_resource(&enc.bs[0], bs_size, AMDGPU_GEM_DOMAIN_GTT);
6807cdc0497Smrg	resources[num_resources++] = enc.bs[0].handle;
6817cdc0497Smrg	alloc_resource(&enc.mvb, enc.mvbuf_size, AMDGPU_GEM_DOMAIN_GTT);
6827cdc0497Smrg	resources[num_resources++] = enc.mvb.handle;
6837cdc0497Smrg	alloc_resource(&enc.vbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
6847cdc0497Smrg	resources[num_resources++] = enc.vbuf.handle;
6857cdc0497Smrg	alloc_resource(&enc.mvrefbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
6867cdc0497Smrg	resources[num_resources++] = enc.mvrefbuf.handle;
6877cdc0497Smrg	alloc_resource(&enc.cpb, cpb_size, AMDGPU_GEM_DOMAIN_VRAM);
6887cdc0497Smrg	resources[num_resources++] = enc.cpb.handle;
6897cdc0497Smrg	resources[num_resources++] = ib_handle;
6907cdc0497Smrg
6917cdc0497Smrg	r = amdgpu_bo_cpu_map(enc.vbuf.handle, (void **)&enc.vbuf.ptr);
6927cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6937cdc0497Smrg
6947cdc0497Smrg	memset(enc.vbuf.ptr, 0, vbuf_size);
6957cdc0497Smrg	for (i = 0; i < enc.height; ++i) {
6967cdc0497Smrg		memcpy(enc.vbuf.ptr, (frame + i * enc.width), enc.width);
6977cdc0497Smrg		enc.vbuf.ptr += ALIGN(enc.width, align);
6987cdc0497Smrg	}
6997cdc0497Smrg	for (i = 0; i < enc.height / 2; ++i) {
7007cdc0497Smrg		memcpy(enc.vbuf.ptr, ((frame + enc.height * enc.width) + i * enc.width), enc.width);
7017cdc0497Smrg		enc.vbuf.ptr += ALIGN(enc.width, align);
7027cdc0497Smrg	}
7037cdc0497Smrg
7047cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc.vbuf.handle);
7057cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7067cdc0497Smrg
7077cdc0497Smrg	r = amdgpu_bo_cpu_map(enc.mvrefbuf.handle, (void **)&enc.mvrefbuf.ptr);
7087cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7097cdc0497Smrg
7107cdc0497Smrg	memset(enc.mvrefbuf.ptr, 0, vbuf_size);
7117cdc0497Smrg	for (i = 0; i < enc.height; ++i) {
7127cdc0497Smrg		memcpy(enc.mvrefbuf.ptr, (frame + (enc.height - i -1) * enc.width), enc.width);
7137cdc0497Smrg		enc.mvrefbuf.ptr += ALIGN(enc.width, align);
7147cdc0497Smrg	}
7157cdc0497Smrg	for (i = 0; i < enc.height / 2; ++i) {
7167cdc0497Smrg		memcpy(enc.mvrefbuf.ptr,
7177cdc0497Smrg		((frame + enc.height * enc.width) + (enc.height / 2 - i -1) * enc.width), enc.width);
7187cdc0497Smrg		enc.mvrefbuf.ptr += ALIGN(enc.width, align);
7197cdc0497Smrg	}
7207cdc0497Smrg
7217cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc.mvrefbuf.handle);
7227cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7237cdc0497Smrg
7247cdc0497Smrg	amdgpu_cs_vce_config();
7257cdc0497Smrg
7267cdc0497Smrg	vce_taskinfo[3] = 3;
7277cdc0497Smrg	amdgpu_cs_vce_mv(&enc);
7287cdc0497Smrg	check_mv_result(&enc);
7297cdc0497Smrg
7307cdc0497Smrg	free_resource(&enc.fb[0]);
7317cdc0497Smrg	free_resource(&enc.bs[0]);
7327cdc0497Smrg	free_resource(&enc.vbuf);
7337cdc0497Smrg	free_resource(&enc.cpb);
7347cdc0497Smrg	free_resource(&enc.mvrefbuf);
7357cdc0497Smrg	free_resource(&enc.mvb);
7367cdc0497Smrg}
7377cdc0497Smrg
7383f012e29Smrgstatic void amdgpu_cs_vce_destroy(void)
7393f012e29Smrg{
7403f012e29Smrg	int len, r;
7413f012e29Smrg
7423f012e29Smrg	num_resources  = 0;
7433f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
7443f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
7453f012e29Smrg	resources[num_resources++] = ib_handle;
7463f012e29Smrg
7473f012e29Smrg	len = 0;
7483f012e29Smrg	memcpy(ib_cpu, vce_session, sizeof(vce_session));
7493f012e29Smrg	len += sizeof(vce_session) / 4;
7503f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
7513f012e29Smrg	ib_cpu[len + 3] = 1;
7523f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
7533f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
7543f012e29Smrg	ib_cpu[len + 2] = enc.fb[0].addr >> 32;
7553f012e29Smrg	ib_cpu[len + 3] = enc.fb[0].addr;
7563f012e29Smrg	len += sizeof(vce_feedback) / 4;
7573f012e29Smrg	memcpy((ib_cpu + len), vce_destroy, sizeof(vce_destroy));
7583f012e29Smrg	len += sizeof(vce_destroy) / 4;
7593f012e29Smrg
7603f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
7613f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
7623f012e29Smrg
7633f012e29Smrg	free_resource(&enc.fb[0]);
7643f012e29Smrg}
765