vce_tests.c revision 5324fb0d
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{
997cdc0497Smrg	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
11700a23bdaSmrg	if (family_id >= AMDGPU_FAMILY_RV || family_id == AMDGPU_FAMILY_SI) {
11800a23bdaSmrg		printf("\n\nThe ASIC NOT support VCE, suite disabled\n");
11900a23bdaSmrg		return CU_FALSE;
12000a23bdaSmrg	}
12100a23bdaSmrg
1227cdc0497Smrg	if (!(chip_id == (chip_rev + 0x3C) || /* FIJI */
1237cdc0497Smrg			chip_id == (chip_rev + 0x50) || /* Polaris 10*/
1247cdc0497Smrg			chip_id == (chip_rev + 0x5A) || /* Polaris 11*/
1257cdc0497Smrg			chip_id == (chip_rev + 0x64) || /* Polaris 12*/
1267cdc0497Smrg			(family_id >= AMDGPU_FAMILY_AI && !ids_flags))) /* dGPU > Polaris */
1277cdc0497Smrg		printf("\n\nThe ASIC NOT support VCE MV, suite disabled\n");
1287cdc0497Smrg	else if (FW_53_0_03 > version)
1297cdc0497Smrg		printf("\n\nThe ASIC FW version NOT support VCE MV, suite disabled\n");
1307cdc0497Smrg	else
1317cdc0497Smrg		ret_mv = CU_TRUE;
1327cdc0497Smrg
1337cdc0497Smrg	if (ret_mv == CU_FALSE) {
1347cdc0497Smrg		amdgpu_set_test_active("VCE Tests", "VCE MV dump", ret_mv);
1357cdc0497Smrg		is_mv_supported = false;
1367cdc0497Smrg	}
1377cdc0497Smrg
13800a23bdaSmrg	return CU_TRUE;
13900a23bdaSmrg}
14000a23bdaSmrg
1413f012e29Smrgint suite_vce_tests_init(void)
1423f012e29Smrg{
1433f012e29Smrg	int r;
1443f012e29Smrg
1453f012e29Smrg	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
1463f012e29Smrg				     &minor_version, &device_handle);
147037b3c26Smrg	if (r) {
148037b3c26Smrg		if ((r == -EACCES) && (errno == EACCES))
149037b3c26Smrg			printf("\n\nError:%s. "
150037b3c26Smrg				"Hint:Try to run this test program as root.",
151037b3c26Smrg				strerror(errno));
152037b3c26Smrg
1533f012e29Smrg		return CUE_SINIT_FAILED;
154037b3c26Smrg	}
1553f012e29Smrg
1563f012e29Smrg	family_id = device_handle->info.family_id;
1573f012e29Smrg	vce_harvest_config = device_handle->info.vce_harvest_config;
1583f012e29Smrg
1593f012e29Smrg	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
1603f012e29Smrg	if (r)
1613f012e29Smrg		return CUE_SINIT_FAILED;
1623f012e29Smrg
1633f012e29Smrg	r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
1643f012e29Smrg				    AMDGPU_GEM_DOMAIN_GTT, 0,
1653f012e29Smrg				    &ib_handle, (void**)&ib_cpu,
1663f012e29Smrg				    &ib_mc_address, &ib_va_handle);
1673f012e29Smrg	if (r)
1683f012e29Smrg		return CUE_SINIT_FAILED;
1693f012e29Smrg
1703f012e29Smrg	memset(&enc, 0, sizeof(struct amdgpu_vce_encode));
1713f012e29Smrg
1723f012e29Smrg	return CUE_SUCCESS;
1733f012e29Smrg}
1743f012e29Smrg
1753f012e29Smrgint suite_vce_tests_clean(void)
1763f012e29Smrg{
1773f012e29Smrg	int r;
1783f012e29Smrg
17900a23bdaSmrg	r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
18000a23bdaSmrg				     ib_mc_address, IB_SIZE);
18100a23bdaSmrg	if (r)
18200a23bdaSmrg		return CUE_SCLEAN_FAILED;
18300a23bdaSmrg
18400a23bdaSmrg	r = amdgpu_cs_ctx_free(context_handle);
18500a23bdaSmrg	if (r)
18600a23bdaSmrg		return CUE_SCLEAN_FAILED;
18700a23bdaSmrg
18800a23bdaSmrg	r = amdgpu_device_deinitialize(device_handle);
18900a23bdaSmrg	if (r)
19000a23bdaSmrg		return CUE_SCLEAN_FAILED;
1913f012e29Smrg
1923f012e29Smrg	return CUE_SUCCESS;
1933f012e29Smrg}
1943f012e29Smrg
1953f012e29Smrgstatic int submit(unsigned ndw, unsigned ip)
1963f012e29Smrg{
1973f012e29Smrg	struct amdgpu_cs_request ibs_request = {0};
1983f012e29Smrg	struct amdgpu_cs_ib_info ib_info = {0};
1993f012e29Smrg	struct amdgpu_cs_fence fence_status = {0};
2003f012e29Smrg	uint32_t expired;
2013f012e29Smrg	int r;
2023f012e29Smrg
2033f012e29Smrg	ib_info.ib_mc_address = ib_mc_address;
2043f012e29Smrg	ib_info.size = ndw;
2053f012e29Smrg
2063f012e29Smrg	ibs_request.ip_type = ip;
2073f012e29Smrg
2083f012e29Smrg	r = amdgpu_bo_list_create(device_handle, num_resources, resources,
2093f012e29Smrg				  NULL, &ibs_request.resources);
2103f012e29Smrg	if (r)
2113f012e29Smrg		return r;
2123f012e29Smrg
2133f012e29Smrg	ibs_request.number_of_ibs = 1;
2143f012e29Smrg	ibs_request.ibs = &ib_info;
2153f012e29Smrg	ibs_request.fence_info.handle = NULL;
2163f012e29Smrg
2173f012e29Smrg	r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
2183f012e29Smrg	if (r)
2193f012e29Smrg		return r;
2203f012e29Smrg
2213f012e29Smrg	r = amdgpu_bo_list_destroy(ibs_request.resources);
2223f012e29Smrg	if (r)
2233f012e29Smrg		return r;
2243f012e29Smrg
2253f012e29Smrg	fence_status.context = context_handle;
2263f012e29Smrg	fence_status.ip_type = ip;
2273f012e29Smrg	fence_status.fence = ibs_request.seq_no;
2283f012e29Smrg
2293f012e29Smrg	r = amdgpu_cs_query_fence_status(&fence_status,
2303f012e29Smrg					 AMDGPU_TIMEOUT_INFINITE,
2313f012e29Smrg					 0, &expired);
2323f012e29Smrg	if (r)
2333f012e29Smrg		return r;
2343f012e29Smrg
2353f012e29Smrg	return 0;
2363f012e29Smrg}
2373f012e29Smrg
2383f012e29Smrgstatic void alloc_resource(struct amdgpu_vce_bo *vce_bo, unsigned size, unsigned domain)
2393f012e29Smrg{
2403f012e29Smrg	struct amdgpu_bo_alloc_request req = {0};
2413f012e29Smrg	amdgpu_bo_handle buf_handle;
2423f012e29Smrg	amdgpu_va_handle va_handle;
2433f012e29Smrg	uint64_t va = 0;
2443f012e29Smrg	int r;
2453f012e29Smrg
2463f012e29Smrg	req.alloc_size = ALIGN(size, 4096);
2473f012e29Smrg	req.preferred_heap = domain;
2483f012e29Smrg	r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
2493f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2503f012e29Smrg	r = amdgpu_va_range_alloc(device_handle,
2513f012e29Smrg				  amdgpu_gpu_va_range_general,
2523f012e29Smrg				  req.alloc_size, 1, 0, &va,
2533f012e29Smrg				  &va_handle, 0);
2543f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2553f012e29Smrg	r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
2563f012e29Smrg			    AMDGPU_VA_OP_MAP);
2573f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2583f012e29Smrg	vce_bo->addr = va;
2593f012e29Smrg	vce_bo->handle = buf_handle;
2603f012e29Smrg	vce_bo->size = req.alloc_size;
2613f012e29Smrg	vce_bo->va_handle = va_handle;
2623f012e29Smrg	r = amdgpu_bo_cpu_map(vce_bo->handle, (void **)&vce_bo->ptr);
2633f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2643f012e29Smrg	memset(vce_bo->ptr, 0, size);
2653f012e29Smrg	r = amdgpu_bo_cpu_unmap(vce_bo->handle);
2663f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2673f012e29Smrg}
2683f012e29Smrg
2693f012e29Smrgstatic void free_resource(struct amdgpu_vce_bo *vce_bo)
2703f012e29Smrg{
2713f012e29Smrg	int r;
2723f012e29Smrg
2733f012e29Smrg	r = amdgpu_bo_va_op(vce_bo->handle, 0, vce_bo->size,
2743f012e29Smrg			    vce_bo->addr, 0, AMDGPU_VA_OP_UNMAP);
2753f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2763f012e29Smrg
2773f012e29Smrg	r = amdgpu_va_range_free(vce_bo->va_handle);
2783f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2793f012e29Smrg
2803f012e29Smrg	r = amdgpu_bo_free(vce_bo->handle);
2813f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
2823f012e29Smrg	memset(vce_bo, 0, sizeof(*vce_bo));
2833f012e29Smrg}
2843f012e29Smrg
2853f012e29Smrgstatic void amdgpu_cs_vce_create(void)
2863f012e29Smrg{
287d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
2883f012e29Smrg	int len, r;
2893f012e29Smrg
2903f012e29Smrg	enc.width = vce_create[6];
2913f012e29Smrg	enc.height = vce_create[7];
2923f012e29Smrg
2933f012e29Smrg	num_resources  = 0;
2943f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
2953f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
2963f012e29Smrg	resources[num_resources++] = ib_handle;
2973f012e29Smrg
2983f012e29Smrg	len = 0;
2993f012e29Smrg	memcpy(ib_cpu, vce_session, sizeof(vce_session));
3003f012e29Smrg	len += sizeof(vce_session) / 4;
3013f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3023f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3033f012e29Smrg	memcpy((ib_cpu + len), vce_create, sizeof(vce_create));
304d8807b2fSmrg	ib_cpu[len + 8] = ALIGN(enc.width, align);
305d8807b2fSmrg	ib_cpu[len + 9] = ALIGN(enc.width, align);
3067cdc0497Smrg	if (is_mv_supported == true) {/* disableTwoInstance */
3077cdc0497Smrg		if (family_id >= AMDGPU_FAMILY_AI)
3087cdc0497Smrg			ib_cpu[len + 11] = 0x01000001;
3097cdc0497Smrg		else
3107cdc0497Smrg			ib_cpu[len + 11] = 0x01000201;
3117cdc0497Smrg	}
3123f012e29Smrg	len += sizeof(vce_create) / 4;
3133f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
3143f012e29Smrg	ib_cpu[len + 2] = enc.fb[0].addr >> 32;
3153f012e29Smrg	ib_cpu[len + 3] = enc.fb[0].addr;
3163f012e29Smrg	len += sizeof(vce_feedback) / 4;
3173f012e29Smrg
3183f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
3193f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
3203f012e29Smrg
3213f012e29Smrg	free_resource(&enc.fb[0]);
3223f012e29Smrg}
3233f012e29Smrg
3243f012e29Smrgstatic void amdgpu_cs_vce_config(void)
3253f012e29Smrg{
3263f012e29Smrg	int len = 0, r;
3273f012e29Smrg
3283f012e29Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
3293f012e29Smrg	len += sizeof(vce_session) / 4;
3303f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3313f012e29Smrg	ib_cpu[len + 3] = 2;
3323f012e29Smrg	ib_cpu[len + 6] = 0xffffffff;
3333f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3343f012e29Smrg	memcpy((ib_cpu + len), vce_rate_ctrl, sizeof(vce_rate_ctrl));
3353f012e29Smrg	len += sizeof(vce_rate_ctrl) / 4;
3363f012e29Smrg	memcpy((ib_cpu + len), vce_config_ext, sizeof(vce_config_ext));
3373f012e29Smrg	len += sizeof(vce_config_ext) / 4;
3383f012e29Smrg	memcpy((ib_cpu + len), vce_motion_est, sizeof(vce_motion_est));
3393f012e29Smrg	len += sizeof(vce_motion_est) / 4;
3403f012e29Smrg	memcpy((ib_cpu + len), vce_rdo, sizeof(vce_rdo));
3413f012e29Smrg	len += sizeof(vce_rdo) / 4;
3423f012e29Smrg	memcpy((ib_cpu + len), vce_pic_ctrl, sizeof(vce_pic_ctrl));
3437cdc0497Smrg	if (is_mv_supported == true)
3447cdc0497Smrg		ib_cpu[len + 27] = 0x00000001; /* encSliceMode */
3453f012e29Smrg	len += sizeof(vce_pic_ctrl) / 4;
3463f012e29Smrg
3473f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
3483f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
3493f012e29Smrg}
3503f012e29Smrg
3517cdc0497Smrgstatic void amdgpu_cs_vce_encode_idr(struct amdgpu_vce_encode *enc)
3523f012e29Smrg{
3533f012e29Smrg
3543f012e29Smrg	uint64_t luma_offset, chroma_offset;
355d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
356d8807b2fSmrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
357d8807b2fSmrg	int len = 0, i, r;
3583f012e29Smrg
3593f012e29Smrg	luma_offset = enc->vbuf.addr;
360d8807b2fSmrg	chroma_offset = luma_offset + luma_size;
3613f012e29Smrg
3623f012e29Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
3633f012e29Smrg	len += sizeof(vce_session) / 4;
3643f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
3653f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
3663f012e29Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
3673f012e29Smrg	ib_cpu[len + 2] = enc->bs[0].addr >> 32;
3683f012e29Smrg	ib_cpu[len + 3] = enc->bs[0].addr;
3693f012e29Smrg	len += sizeof(vce_bs_buffer) / 4;
3703f012e29Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
3713f012e29Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
3723f012e29Smrg	ib_cpu[len + 3] = enc->cpb.addr;
3733f012e29Smrg	len += sizeof(vce_context_buffer) / 4;
3743f012e29Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
375d8807b2fSmrg	for (i = 0; i <  8; ++i)
376d8807b2fSmrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
377d8807b2fSmrg	for (i = 0; i <  8; ++i)
378d8807b2fSmrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
3793f012e29Smrg	len += sizeof(vce_aux_buffer) / 4;
3803f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
3813f012e29Smrg	ib_cpu[len + 2] = enc->fb[0].addr >> 32;
3823f012e29Smrg	ib_cpu[len + 3] = enc->fb[0].addr;
3833f012e29Smrg	len += sizeof(vce_feedback) / 4;
3843f012e29Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
3853f012e29Smrg	ib_cpu[len + 9] = luma_offset >> 32;
3863f012e29Smrg	ib_cpu[len + 10] = luma_offset;
3873f012e29Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
3883f012e29Smrg	ib_cpu[len + 12] = chroma_offset;
389d8807b2fSmrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
390d8807b2fSmrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
391d8807b2fSmrg	ib_cpu[len + 73] = luma_size * 1.5;
392d8807b2fSmrg	ib_cpu[len + 74] = luma_size * 2.5;
3933f012e29Smrg	len += sizeof(vce_encode) / 4;
3943f012e29Smrg	enc->ib_len = len;
3953f012e29Smrg	if (!enc->two_instance) {
3963f012e29Smrg		r = submit(len, AMDGPU_HW_IP_VCE);
3973f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
3983f012e29Smrg	}
3993f012e29Smrg}
4003f012e29Smrg
4013f012e29Smrgstatic void amdgpu_cs_vce_encode_p(struct amdgpu_vce_encode *enc)
4023f012e29Smrg{
4033f012e29Smrg	uint64_t luma_offset, chroma_offset;
404d8807b2fSmrg	int len, i, r;
405d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
406d8807b2fSmrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
4073f012e29Smrg
4083f012e29Smrg	len = (enc->two_instance) ? enc->ib_len : 0;
4093f012e29Smrg	luma_offset = enc->vbuf.addr;
410d8807b2fSmrg	chroma_offset = luma_offset + luma_size;
4113f012e29Smrg
4123f012e29Smrg	if (!enc->two_instance) {
4133f012e29Smrg		memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
4143f012e29Smrg		len += sizeof(vce_session) / 4;
4153f012e29Smrg	}
4163f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
4173f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
4183f012e29Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
4193f012e29Smrg	ib_cpu[len + 2] = enc->bs[1].addr >> 32;
4203f012e29Smrg	ib_cpu[len + 3] = enc->bs[1].addr;
4213f012e29Smrg	len += sizeof(vce_bs_buffer) / 4;
4223f012e29Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
4233f012e29Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
4243f012e29Smrg	ib_cpu[len + 3] = enc->cpb.addr;
4253f012e29Smrg	len += sizeof(vce_context_buffer) / 4;
4263f012e29Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
427d8807b2fSmrg	for (i = 0; i <  8; ++i)
428d8807b2fSmrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
429d8807b2fSmrg	for (i = 0; i <  8; ++i)
430d8807b2fSmrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
4313f012e29Smrg	len += sizeof(vce_aux_buffer) / 4;
4323f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
4333f012e29Smrg	ib_cpu[len + 2] = enc->fb[1].addr >> 32;
4343f012e29Smrg	ib_cpu[len + 3] = enc->fb[1].addr;
4353f012e29Smrg	len += sizeof(vce_feedback) / 4;
4363f012e29Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
4373f012e29Smrg	ib_cpu[len + 2] = 0;
4383f012e29Smrg	ib_cpu[len + 9] = luma_offset >> 32;
4393f012e29Smrg	ib_cpu[len + 10] = luma_offset;
4403f012e29Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
4413f012e29Smrg	ib_cpu[len + 12] = chroma_offset;
442d8807b2fSmrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
443d8807b2fSmrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
4443f012e29Smrg	ib_cpu[len + 18] = 0;
4453f012e29Smrg	ib_cpu[len + 19] = 0;
4463f012e29Smrg	ib_cpu[len + 56] = 3;
4473f012e29Smrg	ib_cpu[len + 57] = 0;
4483f012e29Smrg	ib_cpu[len + 58] = 0;
449d8807b2fSmrg	ib_cpu[len + 59] = luma_size * 1.5;
450d8807b2fSmrg	ib_cpu[len + 60] = luma_size * 2.5;
4513f012e29Smrg	ib_cpu[len + 73] = 0;
452d8807b2fSmrg	ib_cpu[len + 74] = luma_size;
4533f012e29Smrg	ib_cpu[len + 81] = 1;
4543f012e29Smrg	ib_cpu[len + 82] = 1;
4553f012e29Smrg	len += sizeof(vce_encode) / 4;
4563f012e29Smrg
4573f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
4583f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
4593f012e29Smrg}
4603f012e29Smrg
4613f012e29Smrgstatic void check_result(struct amdgpu_vce_encode *enc)
4623f012e29Smrg{
4633f012e29Smrg	uint64_t sum;
4643f012e29Smrg	uint32_t s[2] = {180325, 15946};
4653f012e29Smrg	uint32_t *ptr, size;
4663f012e29Smrg	int i, j, r;
4673f012e29Smrg
4683f012e29Smrg	for (i = 0; i < 2; ++i) {
4693f012e29Smrg		r = amdgpu_bo_cpu_map(enc->fb[i].handle, (void **)&enc->fb[i].ptr);
4703f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4713f012e29Smrg		ptr = (uint32_t *)enc->fb[i].ptr;
4723f012e29Smrg		size = ptr[4] - ptr[9];
4733f012e29Smrg		r = amdgpu_bo_cpu_unmap(enc->fb[i].handle);
4743f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4753f012e29Smrg		r = amdgpu_bo_cpu_map(enc->bs[i].handle, (void **)&enc->bs[i].ptr);
4763f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4773f012e29Smrg		for (j = 0, sum = 0; j < size; ++j)
4783f012e29Smrg			sum += enc->bs[i].ptr[j];
4793f012e29Smrg		CU_ASSERT_EQUAL(sum, s[i]);
4803f012e29Smrg		r = amdgpu_bo_cpu_unmap(enc->bs[i].handle);
4813f012e29Smrg		CU_ASSERT_EQUAL(r, 0);
4823f012e29Smrg	}
4833f012e29Smrg}
4843f012e29Smrg
4853f012e29Smrgstatic void amdgpu_cs_vce_encode(void)
4863f012e29Smrg{
4873f012e29Smrg	uint32_t vbuf_size, bs_size = 0x154000, cpb_size;
488d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
489d8807b2fSmrg	int i, r;
4903f012e29Smrg
491d8807b2fSmrg	vbuf_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16) * 1.5;
4923f012e29Smrg	cpb_size = vbuf_size * 10;
4933f012e29Smrg	num_resources = 0;
4943f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
4953f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
4963f012e29Smrg	alloc_resource(&enc.fb[1], 4096, AMDGPU_GEM_DOMAIN_GTT);
4973f012e29Smrg	resources[num_resources++] = enc.fb[1].handle;
4983f012e29Smrg	alloc_resource(&enc.bs[0], bs_size, AMDGPU_GEM_DOMAIN_GTT);
4993f012e29Smrg	resources[num_resources++] = enc.bs[0].handle;
5003f012e29Smrg	alloc_resource(&enc.bs[1], bs_size, AMDGPU_GEM_DOMAIN_GTT);
5013f012e29Smrg	resources[num_resources++] = enc.bs[1].handle;
5023f012e29Smrg	alloc_resource(&enc.vbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
5033f012e29Smrg	resources[num_resources++] = enc.vbuf.handle;
5043f012e29Smrg	alloc_resource(&enc.cpb, cpb_size, AMDGPU_GEM_DOMAIN_VRAM);
5053f012e29Smrg	resources[num_resources++] = enc.cpb.handle;
5063f012e29Smrg	resources[num_resources++] = ib_handle;
5073f012e29Smrg
5083f012e29Smrg	r = amdgpu_bo_cpu_map(enc.vbuf.handle, (void **)&enc.vbuf.ptr);
5093f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
510d8807b2fSmrg
511d8807b2fSmrg	memset(enc.vbuf.ptr, 0, vbuf_size);
512d8807b2fSmrg	for (i = 0; i < enc.height; ++i) {
513d8807b2fSmrg		memcpy(enc.vbuf.ptr, (frame + i * enc.width), enc.width);
514d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
515d8807b2fSmrg	}
516d8807b2fSmrg	for (i = 0; i < enc.height / 2; ++i) {
517d8807b2fSmrg		memcpy(enc.vbuf.ptr, ((frame + enc.height * enc.width) + i * enc.width), enc.width);
518d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
519d8807b2fSmrg	}
520d8807b2fSmrg
5213f012e29Smrg	r = amdgpu_bo_cpu_unmap(enc.vbuf.handle);
5223f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
5233f012e29Smrg
5243f012e29Smrg	amdgpu_cs_vce_config();
5253f012e29Smrg
5263f012e29Smrg	if (family_id >= AMDGPU_FAMILY_VI) {
5273f012e29Smrg		vce_taskinfo[3] = 3;
5283f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5293f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5303f012e29Smrg		check_result(&enc);
5313f012e29Smrg
5323f012e29Smrg		/* two pipes */
5333f012e29Smrg		vce_encode[16] = 0;
5343f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5353f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5363f012e29Smrg		check_result(&enc);
5373f012e29Smrg
5383f012e29Smrg		/* two instances */
5393f012e29Smrg		if (vce_harvest_config == 0) {
5403f012e29Smrg			enc.two_instance = true;
5413f012e29Smrg			vce_taskinfo[2] = 0x83;
5423f012e29Smrg			vce_taskinfo[4] = 1;
5433f012e29Smrg			amdgpu_cs_vce_encode_idr(&enc);
5443f012e29Smrg			vce_taskinfo[2] = 0xffffffff;
5453f012e29Smrg			vce_taskinfo[4] = 2;
5463f012e29Smrg			amdgpu_cs_vce_encode_p(&enc);
5473f012e29Smrg			check_result(&enc);
5483f012e29Smrg		}
5493f012e29Smrg	} else {
5503f012e29Smrg		vce_taskinfo[3] = 3;
5513f012e29Smrg		vce_encode[16] = 0;
5523f012e29Smrg		amdgpu_cs_vce_encode_idr(&enc);
5533f012e29Smrg		amdgpu_cs_vce_encode_p(&enc);
5543f012e29Smrg		check_result(&enc);
5553f012e29Smrg	}
5563f012e29Smrg
5573f012e29Smrg	free_resource(&enc.fb[0]);
5583f012e29Smrg	free_resource(&enc.fb[1]);
5593f012e29Smrg	free_resource(&enc.bs[0]);
5603f012e29Smrg	free_resource(&enc.bs[1]);
5613f012e29Smrg	free_resource(&enc.vbuf);
5623f012e29Smrg	free_resource(&enc.cpb);
5633f012e29Smrg}
5643f012e29Smrg
5657cdc0497Smrgstatic void amdgpu_cs_vce_mv(struct amdgpu_vce_encode *enc)
5667cdc0497Smrg{
5677cdc0497Smrg	uint64_t luma_offset, chroma_offset;
5687cdc0497Smrg	uint64_t mv_ref_luma_offset;
5697cdc0497Smrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
5707cdc0497Smrg	unsigned luma_size = ALIGN(enc->width, align) * ALIGN(enc->height, 16);
5717cdc0497Smrg	int len = 0, i, r;
5727cdc0497Smrg
5737cdc0497Smrg	luma_offset = enc->vbuf.addr;
5747cdc0497Smrg	chroma_offset = luma_offset + luma_size;
5757cdc0497Smrg	mv_ref_luma_offset = enc->mvrefbuf.addr;
5767cdc0497Smrg
5777cdc0497Smrg	memcpy((ib_cpu + len), vce_session, sizeof(vce_session));
5787cdc0497Smrg	len += sizeof(vce_session) / 4;
5797cdc0497Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
5807cdc0497Smrg	len += sizeof(vce_taskinfo) / 4;
5817cdc0497Smrg	memcpy((ib_cpu + len), vce_bs_buffer, sizeof(vce_bs_buffer));
5827cdc0497Smrg	ib_cpu[len + 2] = enc->bs[0].addr >> 32;
5837cdc0497Smrg	ib_cpu[len + 3] = enc->bs[0].addr;
5847cdc0497Smrg	len += sizeof(vce_bs_buffer) / 4;
5857cdc0497Smrg	memcpy((ib_cpu + len), vce_context_buffer, sizeof(vce_context_buffer));
5867cdc0497Smrg	ib_cpu[len + 2] = enc->cpb.addr >> 32;
5877cdc0497Smrg	ib_cpu[len + 3] = enc->cpb.addr;
5887cdc0497Smrg	len += sizeof(vce_context_buffer) / 4;
5897cdc0497Smrg	memcpy((ib_cpu + len), vce_aux_buffer, sizeof(vce_aux_buffer));
5907cdc0497Smrg	for (i = 0; i <  8; ++i)
5917cdc0497Smrg		ib_cpu[len + 2 + i] = luma_size * 1.5 * (i + 2);
5927cdc0497Smrg	for (i = 0; i <  8; ++i)
5937cdc0497Smrg		ib_cpu[len + 10 + i] = luma_size * 1.5;
5947cdc0497Smrg	len += sizeof(vce_aux_buffer) / 4;
5957cdc0497Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
5967cdc0497Smrg	ib_cpu[len + 2] = enc->fb[0].addr >> 32;
5977cdc0497Smrg	ib_cpu[len + 3] = enc->fb[0].addr;
5987cdc0497Smrg	len += sizeof(vce_feedback) / 4;
5997cdc0497Smrg	memcpy((ib_cpu + len), vce_mv_buffer, sizeof(vce_mv_buffer));
6007cdc0497Smrg	ib_cpu[len + 2] = mv_ref_luma_offset >> 32;
6017cdc0497Smrg	ib_cpu[len + 3] = mv_ref_luma_offset;
6027cdc0497Smrg	ib_cpu[len + 4] = ALIGN(enc->width, align);
6037cdc0497Smrg	ib_cpu[len + 5] = ALIGN(enc->width, align);
6047cdc0497Smrg	ib_cpu[len + 6] = luma_size;
6057cdc0497Smrg	ib_cpu[len + 7] = enc->mvb.addr >> 32;
6067cdc0497Smrg	ib_cpu[len + 8] = enc->mvb.addr;
6077cdc0497Smrg	len += sizeof(vce_mv_buffer) / 4;
6087cdc0497Smrg	memcpy((ib_cpu + len), vce_encode, sizeof(vce_encode));
6097cdc0497Smrg	ib_cpu[len + 2] = 0;
6107cdc0497Smrg	ib_cpu[len + 3] = 0;
6117cdc0497Smrg	ib_cpu[len + 4] = 0x154000;
6127cdc0497Smrg	ib_cpu[len + 9] = luma_offset >> 32;
6137cdc0497Smrg	ib_cpu[len + 10] = luma_offset;
6147cdc0497Smrg	ib_cpu[len + 11] = chroma_offset >> 32;
6157cdc0497Smrg	ib_cpu[len + 12] = chroma_offset;
6167cdc0497Smrg	ib_cpu[len + 13] = ALIGN(enc->height, 16);;
6177cdc0497Smrg	ib_cpu[len + 14] = ALIGN(enc->width, align);
6187cdc0497Smrg	ib_cpu[len + 15] = ALIGN(enc->width, align);
6197cdc0497Smrg	/* encDisableMBOffloading-encDisableTwoPipeMode-encInputPicArrayMode-encInputPicAddrMode */
6207cdc0497Smrg	ib_cpu[len + 16] = 0x01010000;
6217cdc0497Smrg	ib_cpu[len + 18] = 0; /* encPicType */
6227cdc0497Smrg	ib_cpu[len + 19] = 0; /* encIdrFlag */
6237cdc0497Smrg	ib_cpu[len + 20] = 0; /* encIdrPicId */
6247cdc0497Smrg	ib_cpu[len + 21] = 0; /* encMGSKeyPic */
6257cdc0497Smrg	ib_cpu[len + 22] = 0; /* encReferenceFlag */
6267cdc0497Smrg	ib_cpu[len + 23] = 0; /* encTemporalLayerIndex */
6277cdc0497Smrg	ib_cpu[len + 55] = 0; /* pictureStructure */
6287cdc0497Smrg	ib_cpu[len + 56] = 0; /* encPicType -ref[0] */
6297cdc0497Smrg	ib_cpu[len + 61] = 0; /* pictureStructure */
6307cdc0497Smrg	ib_cpu[len + 62] = 0; /* encPicType -ref[1] */
6317cdc0497Smrg	ib_cpu[len + 67] = 0; /* pictureStructure */
6327cdc0497Smrg	ib_cpu[len + 68] = 0; /* encPicType -ref1 */
6337cdc0497Smrg	ib_cpu[len + 81] = 1; /* frameNumber */
6347cdc0497Smrg	ib_cpu[len + 82] = 2; /* pictureOrderCount */
6357cdc0497Smrg	ib_cpu[len + 83] = 0xffffffff; /* numIPicRemainInRCGOP */
6367cdc0497Smrg	ib_cpu[len + 84] = 0xffffffff; /* numPPicRemainInRCGOP */
6377cdc0497Smrg	ib_cpu[len + 85] = 0xffffffff; /* numBPicRemainInRCGOP */
6387cdc0497Smrg	ib_cpu[len + 86] = 0xffffffff; /* numIRPicRemainInRCGOP */
6397cdc0497Smrg	ib_cpu[len + 87] = 0; /* remainedIntraRefreshPictures */
6407cdc0497Smrg	len += sizeof(vce_encode) / 4;
6417cdc0497Smrg
6427cdc0497Smrg	enc->ib_len = len;
6437cdc0497Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
6447cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6457cdc0497Smrg}
6467cdc0497Smrg
6477cdc0497Smrgstatic void check_mv_result(struct amdgpu_vce_encode *enc)
6487cdc0497Smrg{
6497cdc0497Smrg	uint64_t sum;
6507cdc0497Smrg	uint32_t s = 140790;
6515324fb0dSmrg	int j, r;
6527cdc0497Smrg
6537cdc0497Smrg	r = amdgpu_bo_cpu_map(enc->fb[0].handle, (void **)&enc->fb[0].ptr);
6547cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6557cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc->fb[0].handle);
6567cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6577cdc0497Smrg	r = amdgpu_bo_cpu_map(enc->mvb.handle, (void **)&enc->mvb.ptr);
6587cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6597cdc0497Smrg	for (j = 0, sum = 0; j < enc->mvbuf_size; ++j)
6607cdc0497Smrg		sum += enc->mvb.ptr[j];
6617cdc0497Smrg	CU_ASSERT_EQUAL(sum, s);
6627cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc->mvb.handle);
6637cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6647cdc0497Smrg}
6657cdc0497Smrg
6667cdc0497Smrgstatic void amdgpu_cs_vce_encode_mv(void)
6677cdc0497Smrg{
6687cdc0497Smrg	uint32_t vbuf_size, bs_size = 0x154000, cpb_size;
6697cdc0497Smrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
6707cdc0497Smrg	int i, r;
6717cdc0497Smrg
6727cdc0497Smrg	vbuf_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16) * 1.5;
6737cdc0497Smrg	enc.mvbuf_size = ALIGN(enc.width, 16) * ALIGN(enc.height, 16) / 8;
6747cdc0497Smrg	cpb_size = vbuf_size * 10;
6757cdc0497Smrg	num_resources = 0;
6767cdc0497Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
6777cdc0497Smrg	resources[num_resources++] = enc.fb[0].handle;
6787cdc0497Smrg	alloc_resource(&enc.bs[0], bs_size, AMDGPU_GEM_DOMAIN_GTT);
6797cdc0497Smrg	resources[num_resources++] = enc.bs[0].handle;
6807cdc0497Smrg	alloc_resource(&enc.mvb, enc.mvbuf_size, AMDGPU_GEM_DOMAIN_GTT);
6817cdc0497Smrg	resources[num_resources++] = enc.mvb.handle;
6827cdc0497Smrg	alloc_resource(&enc.vbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
6837cdc0497Smrg	resources[num_resources++] = enc.vbuf.handle;
6847cdc0497Smrg	alloc_resource(&enc.mvrefbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
6857cdc0497Smrg	resources[num_resources++] = enc.mvrefbuf.handle;
6867cdc0497Smrg	alloc_resource(&enc.cpb, cpb_size, AMDGPU_GEM_DOMAIN_VRAM);
6877cdc0497Smrg	resources[num_resources++] = enc.cpb.handle;
6887cdc0497Smrg	resources[num_resources++] = ib_handle;
6897cdc0497Smrg
6907cdc0497Smrg	r = amdgpu_bo_cpu_map(enc.vbuf.handle, (void **)&enc.vbuf.ptr);
6917cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
6927cdc0497Smrg
6937cdc0497Smrg	memset(enc.vbuf.ptr, 0, vbuf_size);
6947cdc0497Smrg	for (i = 0; i < enc.height; ++i) {
6957cdc0497Smrg		memcpy(enc.vbuf.ptr, (frame + i * enc.width), enc.width);
6967cdc0497Smrg		enc.vbuf.ptr += ALIGN(enc.width, align);
6977cdc0497Smrg	}
6987cdc0497Smrg	for (i = 0; i < enc.height / 2; ++i) {
6997cdc0497Smrg		memcpy(enc.vbuf.ptr, ((frame + enc.height * enc.width) + i * enc.width), enc.width);
7007cdc0497Smrg		enc.vbuf.ptr += ALIGN(enc.width, align);
7017cdc0497Smrg	}
7027cdc0497Smrg
7037cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc.vbuf.handle);
7047cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7057cdc0497Smrg
7067cdc0497Smrg	r = amdgpu_bo_cpu_map(enc.mvrefbuf.handle, (void **)&enc.mvrefbuf.ptr);
7077cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7087cdc0497Smrg
7097cdc0497Smrg	memset(enc.mvrefbuf.ptr, 0, vbuf_size);
7107cdc0497Smrg	for (i = 0; i < enc.height; ++i) {
7117cdc0497Smrg		memcpy(enc.mvrefbuf.ptr, (frame + (enc.height - i -1) * enc.width), enc.width);
7127cdc0497Smrg		enc.mvrefbuf.ptr += ALIGN(enc.width, align);
7137cdc0497Smrg	}
7147cdc0497Smrg	for (i = 0; i < enc.height / 2; ++i) {
7157cdc0497Smrg		memcpy(enc.mvrefbuf.ptr,
7167cdc0497Smrg		((frame + enc.height * enc.width) + (enc.height / 2 - i -1) * enc.width), enc.width);
7177cdc0497Smrg		enc.mvrefbuf.ptr += ALIGN(enc.width, align);
7187cdc0497Smrg	}
7197cdc0497Smrg
7207cdc0497Smrg	r = amdgpu_bo_cpu_unmap(enc.mvrefbuf.handle);
7217cdc0497Smrg	CU_ASSERT_EQUAL(r, 0);
7227cdc0497Smrg
7237cdc0497Smrg	amdgpu_cs_vce_config();
7247cdc0497Smrg
7257cdc0497Smrg	vce_taskinfo[3] = 3;
7267cdc0497Smrg	amdgpu_cs_vce_mv(&enc);
7277cdc0497Smrg	check_mv_result(&enc);
7287cdc0497Smrg
7297cdc0497Smrg	free_resource(&enc.fb[0]);
7307cdc0497Smrg	free_resource(&enc.bs[0]);
7317cdc0497Smrg	free_resource(&enc.vbuf);
7327cdc0497Smrg	free_resource(&enc.cpb);
7337cdc0497Smrg	free_resource(&enc.mvrefbuf);
7347cdc0497Smrg	free_resource(&enc.mvb);
7357cdc0497Smrg}
7367cdc0497Smrg
7373f012e29Smrgstatic void amdgpu_cs_vce_destroy(void)
7383f012e29Smrg{
7393f012e29Smrg	int len, r;
7403f012e29Smrg
7413f012e29Smrg	num_resources  = 0;
7423f012e29Smrg	alloc_resource(&enc.fb[0], 4096, AMDGPU_GEM_DOMAIN_GTT);
7433f012e29Smrg	resources[num_resources++] = enc.fb[0].handle;
7443f012e29Smrg	resources[num_resources++] = ib_handle;
7453f012e29Smrg
7463f012e29Smrg	len = 0;
7473f012e29Smrg	memcpy(ib_cpu, vce_session, sizeof(vce_session));
7483f012e29Smrg	len += sizeof(vce_session) / 4;
7493f012e29Smrg	memcpy((ib_cpu + len), vce_taskinfo, sizeof(vce_taskinfo));
7503f012e29Smrg	ib_cpu[len + 3] = 1;
7513f012e29Smrg	len += sizeof(vce_taskinfo) / 4;
7523f012e29Smrg	memcpy((ib_cpu + len), vce_feedback, sizeof(vce_feedback));
7533f012e29Smrg	ib_cpu[len + 2] = enc.fb[0].addr >> 32;
7543f012e29Smrg	ib_cpu[len + 3] = enc.fb[0].addr;
7553f012e29Smrg	len += sizeof(vce_feedback) / 4;
7563f012e29Smrg	memcpy((ib_cpu + len), vce_destroy, sizeof(vce_destroy));
7573f012e29Smrg	len += sizeof(vce_destroy) / 4;
7583f012e29Smrg
7593f012e29Smrg	r = submit(len, AMDGPU_HW_IP_VCE);
7603f012e29Smrg	CU_ASSERT_EQUAL(r, 0);
7613f012e29Smrg
7623f012e29Smrg	free_resource(&enc.fb[0]);
7633f012e29Smrg}
764