1d8807b2fSmrg/*
2d8807b2fSmrg * Copyright 2017 Advanced Micro Devices, Inc.
3d8807b2fSmrg *
4d8807b2fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
5d8807b2fSmrg * copy of this software and associated documentation files (the "Software"),
6d8807b2fSmrg * to deal in the Software without restriction, including without limitation
7d8807b2fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8d8807b2fSmrg * and/or sell copies of the Software, and to permit persons to whom the
9d8807b2fSmrg * Software is furnished to do so, subject to the following conditions:
10d8807b2fSmrg *
11d8807b2fSmrg * The above copyright notice and this permission notice shall be included in
12d8807b2fSmrg * all copies or substantial portions of the Software.
13d8807b2fSmrg *
14d8807b2fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15d8807b2fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16d8807b2fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17d8807b2fSmrg * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18d8807b2fSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19d8807b2fSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20d8807b2fSmrg * OTHER DEALINGS IN THE SOFTWARE.
21d8807b2fSmrg *
22d8807b2fSmrg*/
23d8807b2fSmrg
24d8807b2fSmrg#include <stdio.h>
25d8807b2fSmrg#include <inttypes.h>
26d8807b2fSmrg
27d8807b2fSmrg#include "CUnit/Basic.h"
28d8807b2fSmrg
29d8807b2fSmrg#include "util_math.h"
30d8807b2fSmrg
31d8807b2fSmrg#include "amdgpu_test.h"
32d8807b2fSmrg#include "amdgpu_drm.h"
33d8807b2fSmrg#include "amdgpu_internal.h"
34d8807b2fSmrg#include "frame.h"
35d8807b2fSmrg#include "uve_ib.h"
36d8807b2fSmrg
37d8807b2fSmrg#define IB_SIZE		4096
38d8807b2fSmrg#define MAX_RESOURCES	16
39d8807b2fSmrg
40d8807b2fSmrgstruct amdgpu_uvd_enc_bo {
41d8807b2fSmrg	amdgpu_bo_handle handle;
42d8807b2fSmrg	amdgpu_va_handle va_handle;
43d8807b2fSmrg	uint64_t addr;
44d8807b2fSmrg	uint64_t size;
45d8807b2fSmrg	uint8_t *ptr;
46d8807b2fSmrg};
47d8807b2fSmrg
48d8807b2fSmrgstruct amdgpu_uvd_enc {
49d8807b2fSmrg	unsigned width;
50d8807b2fSmrg	unsigned height;
51d8807b2fSmrg	struct amdgpu_uvd_enc_bo session;
52d8807b2fSmrg	struct amdgpu_uvd_enc_bo vbuf;
53d8807b2fSmrg	struct amdgpu_uvd_enc_bo bs;
54d8807b2fSmrg	struct amdgpu_uvd_enc_bo fb;
55d8807b2fSmrg	struct amdgpu_uvd_enc_bo cpb;
56d8807b2fSmrg};
57d8807b2fSmrg
58d8807b2fSmrgstatic amdgpu_device_handle device_handle;
59d8807b2fSmrgstatic uint32_t major_version;
60d8807b2fSmrgstatic uint32_t minor_version;
61d8807b2fSmrgstatic uint32_t family_id;
62d8807b2fSmrg
63d8807b2fSmrgstatic amdgpu_context_handle context_handle;
64d8807b2fSmrgstatic amdgpu_bo_handle ib_handle;
65d8807b2fSmrgstatic amdgpu_va_handle ib_va_handle;
66d8807b2fSmrgstatic uint64_t ib_mc_address;
67d8807b2fSmrgstatic uint32_t *ib_cpu;
68d8807b2fSmrg
69d8807b2fSmrgstatic struct amdgpu_uvd_enc enc;
70d8807b2fSmrgstatic amdgpu_bo_handle resources[MAX_RESOURCES];
71d8807b2fSmrgstatic unsigned num_resources;
72d8807b2fSmrg
73d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_create(void);
74d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_session_init(void);
75d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_encode(void);
76d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_destroy(void);
77d8807b2fSmrg
7800a23bdaSmrg
79d8807b2fSmrgCU_TestInfo uvd_enc_tests[] = {
80d8807b2fSmrg	{ "UVD ENC create",  amdgpu_cs_uvd_enc_create },
81d8807b2fSmrg	{ "UVD ENC session init",  amdgpu_cs_uvd_enc_session_init },
82d8807b2fSmrg	{ "UVD ENC encode",  amdgpu_cs_uvd_enc_encode },
83d8807b2fSmrg	{ "UVD ENC destroy",  amdgpu_cs_uvd_enc_destroy },
84d8807b2fSmrg	CU_TEST_INFO_NULL,
85d8807b2fSmrg};
86d8807b2fSmrg
8700a23bdaSmrgCU_BOOL suite_uvd_enc_tests_enable(void)
8800a23bdaSmrg{
8900a23bdaSmrg	int r;
9000a23bdaSmrg	struct drm_amdgpu_info_hw_ip info;
9100a23bdaSmrg
9200a23bdaSmrg	if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
9300a23bdaSmrg					     &minor_version, &device_handle))
9400a23bdaSmrg		return CU_FALSE;
9500a23bdaSmrg
9600a23bdaSmrg	r = amdgpu_query_hw_ip_info(device_handle, AMDGPU_HW_IP_UVD_ENC, 0, &info);
9700a23bdaSmrg
9800a23bdaSmrg	if (amdgpu_device_deinitialize(device_handle))
9900a23bdaSmrg		return CU_FALSE;
10000a23bdaSmrg
10100a23bdaSmrg	if (!info.available_rings)
10200a23bdaSmrg		printf("\n\nThe ASIC NOT support UVD ENC, suite disabled.\n");
10300a23bdaSmrg
10400a23bdaSmrg	return (r == 0 && (info.available_rings ? CU_TRUE : CU_FALSE));
10500a23bdaSmrg}
10600a23bdaSmrg
10700a23bdaSmrg
108d8807b2fSmrgint suite_uvd_enc_tests_init(void)
109d8807b2fSmrg{
110d8807b2fSmrg	int r;
111d8807b2fSmrg
112d8807b2fSmrg	r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
113d8807b2fSmrg				     &minor_version, &device_handle);
114d8807b2fSmrg	if (r)
115d8807b2fSmrg		return CUE_SINIT_FAILED;
116d8807b2fSmrg
117d8807b2fSmrg	family_id = device_handle->info.family_id;
118d8807b2fSmrg
119d8807b2fSmrg	r = amdgpu_cs_ctx_create(device_handle, &context_handle);
120d8807b2fSmrg	if (r)
121d8807b2fSmrg		return CUE_SINIT_FAILED;
122d8807b2fSmrg
123d8807b2fSmrg	r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
124d8807b2fSmrg				    AMDGPU_GEM_DOMAIN_GTT, 0,
125d8807b2fSmrg				    &ib_handle, (void**)&ib_cpu,
126d8807b2fSmrg				    &ib_mc_address, &ib_va_handle);
127d8807b2fSmrg	if (r)
128d8807b2fSmrg		return CUE_SINIT_FAILED;
129d8807b2fSmrg
130d8807b2fSmrg	return CUE_SUCCESS;
131d8807b2fSmrg}
132d8807b2fSmrg
133d8807b2fSmrgint suite_uvd_enc_tests_clean(void)
134d8807b2fSmrg{
135d8807b2fSmrg	int r;
136d8807b2fSmrg
13700a23bdaSmrg	r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
13800a23bdaSmrg				     ib_mc_address, IB_SIZE);
13900a23bdaSmrg	if (r)
14000a23bdaSmrg		return CUE_SCLEAN_FAILED;
141d8807b2fSmrg
14200a23bdaSmrg	r = amdgpu_cs_ctx_free(context_handle);
14300a23bdaSmrg	if (r)
14400a23bdaSmrg		return CUE_SCLEAN_FAILED;
145d8807b2fSmrg
14600a23bdaSmrg	r = amdgpu_device_deinitialize(device_handle);
14700a23bdaSmrg	if (r)
14800a23bdaSmrg		return CUE_SCLEAN_FAILED;
149d8807b2fSmrg
150d8807b2fSmrg	return CUE_SUCCESS;
151d8807b2fSmrg}
152d8807b2fSmrg
153d8807b2fSmrgstatic int submit(unsigned ndw, unsigned ip)
154d8807b2fSmrg{
155d8807b2fSmrg	struct amdgpu_cs_request ibs_request = {0};
156d8807b2fSmrg	struct amdgpu_cs_ib_info ib_info = {0};
157d8807b2fSmrg	struct amdgpu_cs_fence fence_status = {0};
158d8807b2fSmrg	uint32_t expired;
159d8807b2fSmrg	int r;
160d8807b2fSmrg
161d8807b2fSmrg	ib_info.ib_mc_address = ib_mc_address;
162d8807b2fSmrg	ib_info.size = ndw;
163d8807b2fSmrg
164d8807b2fSmrg	ibs_request.ip_type = ip;
165d8807b2fSmrg
166d8807b2fSmrg	r = amdgpu_bo_list_create(device_handle, num_resources, resources,
167d8807b2fSmrg				  NULL, &ibs_request.resources);
168d8807b2fSmrg	if (r)
169d8807b2fSmrg		return r;
170d8807b2fSmrg
171d8807b2fSmrg	ibs_request.number_of_ibs = 1;
172d8807b2fSmrg	ibs_request.ibs = &ib_info;
173d8807b2fSmrg	ibs_request.fence_info.handle = NULL;
174d8807b2fSmrg
175d8807b2fSmrg	r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
176d8807b2fSmrg	if (r)
177d8807b2fSmrg		return r;
178d8807b2fSmrg
179d8807b2fSmrg	r = amdgpu_bo_list_destroy(ibs_request.resources);
180d8807b2fSmrg	if (r)
181d8807b2fSmrg		return r;
182d8807b2fSmrg
183d8807b2fSmrg	fence_status.context = context_handle;
184d8807b2fSmrg	fence_status.ip_type = ip;
185d8807b2fSmrg	fence_status.fence = ibs_request.seq_no;
186d8807b2fSmrg
187d8807b2fSmrg	r = amdgpu_cs_query_fence_status(&fence_status,
188d8807b2fSmrg					 AMDGPU_TIMEOUT_INFINITE,
189d8807b2fSmrg					 0, &expired);
190d8807b2fSmrg	if (r)
191d8807b2fSmrg		return r;
192d8807b2fSmrg
193d8807b2fSmrg	return 0;
194d8807b2fSmrg}
195d8807b2fSmrg
196d8807b2fSmrgstatic void alloc_resource(struct amdgpu_uvd_enc_bo *uvd_enc_bo,
197d8807b2fSmrg			unsigned size, unsigned domain)
198d8807b2fSmrg{
199d8807b2fSmrg	struct amdgpu_bo_alloc_request req = {0};
200d8807b2fSmrg	amdgpu_bo_handle buf_handle;
201d8807b2fSmrg	amdgpu_va_handle va_handle;
202d8807b2fSmrg	uint64_t va = 0;
203d8807b2fSmrg	int r;
204d8807b2fSmrg
205d8807b2fSmrg	req.alloc_size = ALIGN(size, 4096);
206d8807b2fSmrg	req.preferred_heap = domain;
207d8807b2fSmrg	r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
208d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
209d8807b2fSmrg	r = amdgpu_va_range_alloc(device_handle,
210d8807b2fSmrg				  amdgpu_gpu_va_range_general,
211d8807b2fSmrg				  req.alloc_size, 1, 0, &va,
212d8807b2fSmrg				  &va_handle, 0);
213d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
214d8807b2fSmrg	r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
215d8807b2fSmrg			    AMDGPU_VA_OP_MAP);
216d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
217d8807b2fSmrg	uvd_enc_bo->addr = va;
218d8807b2fSmrg	uvd_enc_bo->handle = buf_handle;
219d8807b2fSmrg	uvd_enc_bo->size = req.alloc_size;
220d8807b2fSmrg	uvd_enc_bo->va_handle = va_handle;
221d8807b2fSmrg	r = amdgpu_bo_cpu_map(uvd_enc_bo->handle, (void **)&uvd_enc_bo->ptr);
222d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
223d8807b2fSmrg	memset(uvd_enc_bo->ptr, 0, size);
224d8807b2fSmrg	r = amdgpu_bo_cpu_unmap(uvd_enc_bo->handle);
225d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
226d8807b2fSmrg}
227d8807b2fSmrg
228d8807b2fSmrgstatic void free_resource(struct amdgpu_uvd_enc_bo *uvd_enc_bo)
229d8807b2fSmrg{
230d8807b2fSmrg	int r;
231d8807b2fSmrg
232d8807b2fSmrg	r = amdgpu_bo_va_op(uvd_enc_bo->handle, 0, uvd_enc_bo->size,
233d8807b2fSmrg			    uvd_enc_bo->addr, 0, AMDGPU_VA_OP_UNMAP);
234d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
235d8807b2fSmrg
236d8807b2fSmrg	r = amdgpu_va_range_free(uvd_enc_bo->va_handle);
237d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
238d8807b2fSmrg
239d8807b2fSmrg	r = amdgpu_bo_free(uvd_enc_bo->handle);
240d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
241d8807b2fSmrg	memset(uvd_enc_bo, 0, sizeof(*uvd_enc_bo));
242d8807b2fSmrg}
243d8807b2fSmrg
244d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_create(void)
245d8807b2fSmrg{
246d8807b2fSmrg	enc.width = 160;
247d8807b2fSmrg	enc.height = 128;
248d8807b2fSmrg
249d8807b2fSmrg	num_resources  = 0;
250d8807b2fSmrg	alloc_resource(&enc.session, 128 * 1024, AMDGPU_GEM_DOMAIN_GTT);
251d8807b2fSmrg	resources[num_resources++] = enc.session.handle;
252d8807b2fSmrg	resources[num_resources++] = ib_handle;
253d8807b2fSmrg}
254d8807b2fSmrg
255d8807b2fSmrgstatic void check_result(struct amdgpu_uvd_enc *enc)
256d8807b2fSmrg{
257d8807b2fSmrg	uint64_t sum;
25800a23bdaSmrg	uint32_t s = 175602;
259d8807b2fSmrg	uint32_t *ptr, size;
26000a23bdaSmrg	int j, r;
261d8807b2fSmrg
262d8807b2fSmrg	r = amdgpu_bo_cpu_map(enc->fb.handle, (void **)&enc->fb.ptr);
263d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
264d8807b2fSmrg	ptr = (uint32_t *)enc->fb.ptr;
265d8807b2fSmrg	size = ptr[6];
266d8807b2fSmrg	r = amdgpu_bo_cpu_unmap(enc->fb.handle);
267d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
268d8807b2fSmrg	r = amdgpu_bo_cpu_map(enc->bs.handle, (void **)&enc->bs.ptr);
269d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
270d8807b2fSmrg	for (j = 0, sum = 0; j < size; ++j)
271d8807b2fSmrg		sum += enc->bs.ptr[j];
272d8807b2fSmrg	CU_ASSERT_EQUAL(sum, s);
273d8807b2fSmrg	r = amdgpu_bo_cpu_unmap(enc->bs.handle);
274d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
275d8807b2fSmrg
276d8807b2fSmrg}
277d8807b2fSmrg
278d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_session_init(void)
279d8807b2fSmrg{
280d8807b2fSmrg	int len, r;
281d8807b2fSmrg
282d8807b2fSmrg	len = 0;
283d8807b2fSmrg	memcpy((ib_cpu + len), uve_session_info, sizeof(uve_session_info));
284d8807b2fSmrg	len += sizeof(uve_session_info) / 4;
285d8807b2fSmrg	ib_cpu[len++] = enc.session.addr >> 32;
286d8807b2fSmrg	ib_cpu[len++] = enc.session.addr;
287d8807b2fSmrg
288d8807b2fSmrg	memcpy((ib_cpu + len), uve_task_info, sizeof(uve_task_info));
289d8807b2fSmrg	len += sizeof(uve_task_info) / 4;
290d8807b2fSmrg	ib_cpu[len++] = 0x000000d8;
291d8807b2fSmrg	ib_cpu[len++] = 0x00000000;
292d8807b2fSmrg	ib_cpu[len++] = 0x00000000;
293d8807b2fSmrg
294d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_init, sizeof(uve_op_init));
295d8807b2fSmrg	len += sizeof(uve_op_init) / 4;
296d8807b2fSmrg
297d8807b2fSmrg	memcpy((ib_cpu + len), uve_session_init, sizeof(uve_session_init));
298d8807b2fSmrg	len += sizeof(uve_session_init) / 4;
299d8807b2fSmrg
300d8807b2fSmrg	memcpy((ib_cpu + len), uve_layer_ctrl, sizeof(uve_layer_ctrl));
301d8807b2fSmrg	len += sizeof(uve_layer_ctrl) / 4;
302d8807b2fSmrg
303d8807b2fSmrg	memcpy((ib_cpu + len), uve_slice_ctrl, sizeof(uve_slice_ctrl));
304d8807b2fSmrg	len += sizeof(uve_slice_ctrl) / 4;
305d8807b2fSmrg
306d8807b2fSmrg	memcpy((ib_cpu + len), uve_spec_misc, sizeof(uve_spec_misc));
307d8807b2fSmrg	len += sizeof(uve_spec_misc) / 4;
308d8807b2fSmrg
309d8807b2fSmrg	memcpy((ib_cpu + len), uve_rc_session_init, sizeof(uve_rc_session_init));
310d8807b2fSmrg	len += sizeof(uve_rc_session_init) / 4;
311d8807b2fSmrg
312d8807b2fSmrg	memcpy((ib_cpu + len), uve_deblocking_filter, sizeof(uve_deblocking_filter));
313d8807b2fSmrg	len += sizeof(uve_deblocking_filter) / 4;
314d8807b2fSmrg
315d8807b2fSmrg	memcpy((ib_cpu + len), uve_quality_params, sizeof(uve_quality_params));
316d8807b2fSmrg	len += sizeof(uve_quality_params) / 4;
317d8807b2fSmrg
318d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_init_rc, sizeof(uve_op_init_rc));
319d8807b2fSmrg	len += sizeof(uve_op_init_rc) / 4;
320d8807b2fSmrg
321d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_init_rc_vbv_level, sizeof(uve_op_init_rc_vbv_level));
322d8807b2fSmrg	len += sizeof(uve_op_init_rc_vbv_level) / 4;
323d8807b2fSmrg
324d8807b2fSmrg	r = submit(len, AMDGPU_HW_IP_UVD_ENC);
325d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
326d8807b2fSmrg}
327d8807b2fSmrg
328d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_encode(void)
329d8807b2fSmrg{
330d8807b2fSmrg	int len, r, i;
331d8807b2fSmrg	uint64_t luma_offset, chroma_offset;
332d8807b2fSmrg	uint32_t vbuf_size, bs_size = 0x003f4800, cpb_size;
333d8807b2fSmrg	unsigned align = (family_id >= AMDGPU_FAMILY_AI) ? 256 : 16;
334d8807b2fSmrg	vbuf_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16) * 1.5;
335d8807b2fSmrg	cpb_size = vbuf_size * 10;
336d8807b2fSmrg
337d8807b2fSmrg
338d8807b2fSmrg	num_resources  = 0;
339d8807b2fSmrg	alloc_resource(&enc.fb, 4096, AMDGPU_GEM_DOMAIN_VRAM);
340d8807b2fSmrg	resources[num_resources++] = enc.fb.handle;
341d8807b2fSmrg	alloc_resource(&enc.bs, bs_size, AMDGPU_GEM_DOMAIN_VRAM);
342d8807b2fSmrg	resources[num_resources++] = enc.bs.handle;
343d8807b2fSmrg	alloc_resource(&enc.vbuf, vbuf_size, AMDGPU_GEM_DOMAIN_VRAM);
344d8807b2fSmrg	resources[num_resources++] = enc.vbuf.handle;
345d8807b2fSmrg	alloc_resource(&enc.cpb, cpb_size, AMDGPU_GEM_DOMAIN_VRAM);
346d8807b2fSmrg	resources[num_resources++] = enc.cpb.handle;
347d8807b2fSmrg	resources[num_resources++] = ib_handle;
348d8807b2fSmrg
349d8807b2fSmrg	r = amdgpu_bo_cpu_map(enc.vbuf.handle, (void **)&enc.vbuf.ptr);
350d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
351d8807b2fSmrg
352d8807b2fSmrg	memset(enc.vbuf.ptr, 0, vbuf_size);
353d8807b2fSmrg	for (i = 0; i < enc.height; ++i) {
354d8807b2fSmrg		memcpy(enc.vbuf.ptr, (frame + i * enc.width), enc.width);
355d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
356d8807b2fSmrg	}
357d8807b2fSmrg	for (i = 0; i < enc.height / 2; ++i) {
358d8807b2fSmrg		memcpy(enc.vbuf.ptr, ((frame + enc.height * enc.width) + i * enc.width), enc.width);
359d8807b2fSmrg		enc.vbuf.ptr += ALIGN(enc.width, align);
360d8807b2fSmrg	}
361d8807b2fSmrg
362d8807b2fSmrg	r = amdgpu_bo_cpu_unmap(enc.vbuf.handle);
363d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
364d8807b2fSmrg
365d8807b2fSmrg	len = 0;
366d8807b2fSmrg	memcpy((ib_cpu + len), uve_session_info, sizeof(uve_session_info));
367d8807b2fSmrg	len += sizeof(uve_session_info) / 4;
368d8807b2fSmrg	ib_cpu[len++] = enc.session.addr >> 32;
369d8807b2fSmrg	ib_cpu[len++] = enc.session.addr;
370d8807b2fSmrg
371d8807b2fSmrg	memcpy((ib_cpu + len), uve_task_info, sizeof(uve_task_info));
372d8807b2fSmrg	len += sizeof(uve_task_info) / 4;
373d8807b2fSmrg	ib_cpu[len++] = 0x000005e0;
374d8807b2fSmrg	ib_cpu[len++] = 0x00000001;
375d8807b2fSmrg	ib_cpu[len++] = 0x00000001;
376d8807b2fSmrg
377d8807b2fSmrg	memcpy((ib_cpu + len), uve_nalu_buffer_1, sizeof(uve_nalu_buffer_1));
378d8807b2fSmrg	len += sizeof(uve_nalu_buffer_1) / 4;
379d8807b2fSmrg
380d8807b2fSmrg	memcpy((ib_cpu + len), uve_nalu_buffer_2, sizeof(uve_nalu_buffer_2));
381d8807b2fSmrg	len += sizeof(uve_nalu_buffer_2) / 4;
382d8807b2fSmrg
383d8807b2fSmrg	memcpy((ib_cpu + len), uve_nalu_buffer_3, sizeof(uve_nalu_buffer_3));
384d8807b2fSmrg	len += sizeof(uve_nalu_buffer_3) / 4;
385d8807b2fSmrg
386d8807b2fSmrg	memcpy((ib_cpu + len), uve_nalu_buffer_4, sizeof(uve_nalu_buffer_4));
387d8807b2fSmrg	len += sizeof(uve_nalu_buffer_4) / 4;
388d8807b2fSmrg
389d8807b2fSmrg	memcpy((ib_cpu + len), uve_slice_header, sizeof(uve_slice_header));
390d8807b2fSmrg	len += sizeof(uve_slice_header) / 4;
391d8807b2fSmrg
392d8807b2fSmrg	ib_cpu[len++] = 0x00000254;
393d8807b2fSmrg	ib_cpu[len++] = 0x00000010;
394d8807b2fSmrg	ib_cpu[len++] = enc.cpb.addr >> 32;
395d8807b2fSmrg	ib_cpu[len++] = enc.cpb.addr;
396d8807b2fSmrg	memcpy((ib_cpu + len), uve_ctx_buffer, sizeof(uve_ctx_buffer));
397d8807b2fSmrg	len += sizeof(uve_ctx_buffer) / 4;
398d8807b2fSmrg
399d8807b2fSmrg	memcpy((ib_cpu + len), uve_bitstream_buffer, sizeof(uve_bitstream_buffer));
400d8807b2fSmrg	len += sizeof(uve_bitstream_buffer) / 4;
401d8807b2fSmrg	ib_cpu[len++] = 0x00000000;
402d8807b2fSmrg	ib_cpu[len++] = enc.bs.addr >> 32;
403d8807b2fSmrg	ib_cpu[len++] = enc.bs.addr;
404d8807b2fSmrg	ib_cpu[len++] = 0x003f4800;
405d8807b2fSmrg	ib_cpu[len++] = 0x00000000;
406d8807b2fSmrg
407d8807b2fSmrg	memcpy((ib_cpu + len), uve_feedback_buffer, sizeof(uve_feedback_buffer));
408d8807b2fSmrg	len += sizeof(uve_feedback_buffer) / 4;
409d8807b2fSmrg	ib_cpu[len++] = enc.fb.addr >> 32;
410d8807b2fSmrg	ib_cpu[len++] = enc.fb.addr;
411d8807b2fSmrg	ib_cpu[len++] = 0x00000010;
412d8807b2fSmrg	ib_cpu[len++] = 0x00000028;
413d8807b2fSmrg
414d8807b2fSmrg	memcpy((ib_cpu + len), uve_feedback_buffer_additional, sizeof(uve_feedback_buffer_additional));
415d8807b2fSmrg	len += sizeof(uve_feedback_buffer_additional) / 4;
416d8807b2fSmrg
417d8807b2fSmrg	memcpy((ib_cpu + len), uve_intra_refresh, sizeof(uve_intra_refresh));
418d8807b2fSmrg	len += sizeof(uve_intra_refresh) / 4;
419d8807b2fSmrg
420d8807b2fSmrg	memcpy((ib_cpu + len), uve_layer_select, sizeof(uve_layer_select));
421d8807b2fSmrg	len += sizeof(uve_layer_select) / 4;
422d8807b2fSmrg
423d8807b2fSmrg	memcpy((ib_cpu + len), uve_rc_layer_init, sizeof(uve_rc_layer_init));
424d8807b2fSmrg	len += sizeof(uve_rc_layer_init) / 4;
425d8807b2fSmrg
426d8807b2fSmrg	memcpy((ib_cpu + len), uve_layer_select, sizeof(uve_layer_select));
427d8807b2fSmrg	len += sizeof(uve_layer_select) / 4;
428d8807b2fSmrg
429d8807b2fSmrg	memcpy((ib_cpu + len), uve_rc_per_pic, sizeof(uve_rc_per_pic));
430d8807b2fSmrg	len += sizeof(uve_rc_per_pic) / 4;
431d8807b2fSmrg
432d8807b2fSmrg	unsigned luma_size = ALIGN(enc.width, align) * ALIGN(enc.height, 16);
433d8807b2fSmrg	luma_offset = enc.vbuf.addr;
434d8807b2fSmrg	chroma_offset = luma_offset + luma_size;
435d8807b2fSmrg	ib_cpu[len++] = 0x00000054;
436d8807b2fSmrg	ib_cpu[len++] = 0x0000000c;
437d8807b2fSmrg	ib_cpu[len++] = 0x00000002;
438d8807b2fSmrg	ib_cpu[len++] = 0x003f4800;
439d8807b2fSmrg	ib_cpu[len++] = luma_offset >> 32;
440d8807b2fSmrg	ib_cpu[len++] = luma_offset;
441d8807b2fSmrg	ib_cpu[len++] = chroma_offset >> 32;
442d8807b2fSmrg	ib_cpu[len++] = chroma_offset;
443d8807b2fSmrg	memcpy((ib_cpu + len), uve_encode_param, sizeof(uve_encode_param));
44400a23bdaSmrg	ib_cpu[len] = ALIGN(enc.width, align);
44500a23bdaSmrg	ib_cpu[len + 1] = ALIGN(enc.width, align);
446d8807b2fSmrg	len += sizeof(uve_encode_param) / 4;
447d8807b2fSmrg
448d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_speed_enc_mode, sizeof(uve_op_speed_enc_mode));
449d8807b2fSmrg	len += sizeof(uve_op_speed_enc_mode) / 4;
450d8807b2fSmrg
451d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_encode, sizeof(uve_op_encode));
452d8807b2fSmrg	len += sizeof(uve_op_encode) / 4;
453d8807b2fSmrg
454d8807b2fSmrg	r = submit(len, AMDGPU_HW_IP_UVD_ENC);
455d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
456d8807b2fSmrg
457d8807b2fSmrg	check_result(&enc);
458d8807b2fSmrg
459d8807b2fSmrg	free_resource(&enc.fb);
460d8807b2fSmrg	free_resource(&enc.bs);
461d8807b2fSmrg	free_resource(&enc.vbuf);
462d8807b2fSmrg	free_resource(&enc.cpb);
463d8807b2fSmrg}
464d8807b2fSmrg
465d8807b2fSmrgstatic void amdgpu_cs_uvd_enc_destroy(void)
466d8807b2fSmrg{
467d8807b2fSmrg	int len, r;
468d8807b2fSmrg
469d8807b2fSmrg	num_resources  = 0;
470d8807b2fSmrg	resources[num_resources++] = ib_handle;
471d8807b2fSmrg
472d8807b2fSmrg	len = 0;
473d8807b2fSmrg	memcpy((ib_cpu + len), uve_session_info, sizeof(uve_session_info));
474d8807b2fSmrg	len += sizeof(uve_session_info) / 4;
475d8807b2fSmrg	ib_cpu[len++] = enc.session.addr >> 32;
476d8807b2fSmrg	ib_cpu[len++] = enc.session.addr;
477d8807b2fSmrg
478d8807b2fSmrg	memcpy((ib_cpu + len), uve_task_info, sizeof(uve_task_info));
479d8807b2fSmrg	len += sizeof(uve_task_info) / 4;
480d8807b2fSmrg	ib_cpu[len++] = 0xffffffff;
481d8807b2fSmrg	ib_cpu[len++] = 0x00000002;
482d8807b2fSmrg	ib_cpu[len++] = 0x00000000;
483d8807b2fSmrg
484d8807b2fSmrg	memcpy((ib_cpu + len), uve_op_close, sizeof(uve_op_close));
485d8807b2fSmrg	len += sizeof(uve_op_close) / 4;
486d8807b2fSmrg
487d8807b2fSmrg	r = submit(len, AMDGPU_HW_IP_UVD_ENC);
488d8807b2fSmrg	CU_ASSERT_EQUAL(r, 0);
489d8807b2fSmrg
490d8807b2fSmrg	free_resource(&enc.session);
491d8807b2fSmrg}
492