2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "CUnit/Basic.h"
29 #include "util_math.h"
31 #include "amdgpu_test.h"
32 #include "amdgpu_drm.h"
33 #include "amdgpu_internal.h"
34 #include "decode_messages.h"
37 #define MAX_RESOURCES 16
39 struct amdgpu_vcn_bo {
40 amdgpu_bo_handle handle;
41 amdgpu_va_handle va_handle;
47 struct amdgpu_vcn_reg {
55 static amdgpu_device_handle device_handle;
56 static uint32_t major_version;
57 static uint32_t minor_version;
58 static uint32_t family_id;
60 static amdgpu_context_handle context_handle;
61 static amdgpu_bo_handle ib_handle;
62 static amdgpu_va_handle ib_va_handle;
63 static uint64_t ib_mc_address;
64 static uint32_t *ib_cpu;
66 static amdgpu_bo_handle resources[MAX_RESOURCES];
67 static unsigned num_resources;
68 static struct amdgpu_vcn_reg reg;
70 static void amdgpu_cs_vcn_dec_create(void);
71 static void amdgpu_cs_vcn_dec_decode(void);
72 static void amdgpu_cs_vcn_dec_destroy(void);
74 static void amdgpu_cs_vcn_enc_create(void);
75 static void amdgpu_cs_vcn_enc_encode(void);
76 static void amdgpu_cs_vcn_enc_destroy(void);
78 CU_TestInfo vcn_tests[] = {
80 { "VCN DEC create", amdgpu_cs_vcn_dec_create },
81 { "VCN DEC decode", amdgpu_cs_vcn_dec_decode },
82 { "VCN DEC destroy", amdgpu_cs_vcn_dec_destroy },
84 { "VCN ENC create", amdgpu_cs_vcn_enc_create },
85 { "VCN ENC decode", amdgpu_cs_vcn_enc_encode },
86 { "VCN ENC destroy", amdgpu_cs_vcn_enc_destroy },
90 CU_BOOL suite_vcn_tests_enable(void)
93 if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
94 &minor_version, &device_handle))
97 family_id = device_handle->info.family_id;
99 if (amdgpu_device_deinitialize(device_handle))
103 if (family_id < AMDGPU_FAMILY_RV) {
104 printf("\n\nThe ASIC NOT support VCN, suite disabled\n");
108 if (family_id == AMDGPU_FAMILY_RV) {
114 } else if (family_id == AMDGPU_FAMILY_NV) {
126 int suite_vcn_tests_init(void)
130 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
131 &minor_version, &device_handle);
133 return CUE_SINIT_FAILED;
135 family_id = device_handle->info.family_id;
137 r = amdgpu_cs_ctx_create(device_handle, &context_handle);
139 return CUE_SINIT_FAILED;
141 r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
142 AMDGPU_GEM_DOMAIN_GTT, 0,
143 &ib_handle, (void**)&ib_cpu,
144 &ib_mc_address, &ib_va_handle);
146 return CUE_SINIT_FAILED;
151 int suite_vcn_tests_clean(void)
155 r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
156 ib_mc_address, IB_SIZE);
158 return CUE_SCLEAN_FAILED;
160 r = amdgpu_cs_ctx_free(context_handle);
162 return CUE_SCLEAN_FAILED;
164 r = amdgpu_device_deinitialize(device_handle);
166 return CUE_SCLEAN_FAILED;
171 static int submit(unsigned ndw, unsigned ip)
173 struct amdgpu_cs_request ibs_request = {0};
174 struct amdgpu_cs_ib_info ib_info = {0};
175 struct amdgpu_cs_fence fence_status = {0};
179 ib_info.ib_mc_address = ib_mc_address;
182 ibs_request.ip_type = ip;
184 r = amdgpu_bo_list_create(device_handle, num_resources, resources,
185 NULL, &ibs_request.resources);
189 ibs_request.number_of_ibs = 1;
190 ibs_request.ibs = &ib_info;
191 ibs_request.fence_info.handle = NULL;
193 r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
197 r = amdgpu_bo_list_destroy(ibs_request.resources);
201 fence_status.context = context_handle;
202 fence_status.ip_type = ip;
203 fence_status.fence = ibs_request.seq_no;
205 r = amdgpu_cs_query_fence_status(&fence_status,
206 AMDGPU_TIMEOUT_INFINITE,
214 static void alloc_resource(struct amdgpu_vcn_bo *vcn_bo,
215 unsigned size, unsigned domain)
217 struct amdgpu_bo_alloc_request req = {0};
218 amdgpu_bo_handle buf_handle;
219 amdgpu_va_handle va_handle;
223 req.alloc_size = ALIGN(size, 4096);
224 req.preferred_heap = domain;
225 r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
226 CU_ASSERT_EQUAL(r, 0);
227 r = amdgpu_va_range_alloc(device_handle,
228 amdgpu_gpu_va_range_general,
229 req.alloc_size, 1, 0, &va,
231 CU_ASSERT_EQUAL(r, 0);
232 r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
234 CU_ASSERT_EQUAL(r, 0);
236 vcn_bo->handle = buf_handle;
237 vcn_bo->size = req.alloc_size;
238 vcn_bo->va_handle = va_handle;
239 r = amdgpu_bo_cpu_map(vcn_bo->handle, (void **)&vcn_bo->ptr);
240 CU_ASSERT_EQUAL(r, 0);
241 memset(vcn_bo->ptr, 0, size);
242 r = amdgpu_bo_cpu_unmap(vcn_bo->handle);
243 CU_ASSERT_EQUAL(r, 0);
246 static void free_resource(struct amdgpu_vcn_bo *vcn_bo)
250 r = amdgpu_bo_va_op(vcn_bo->handle, 0, vcn_bo->size,
251 vcn_bo->addr, 0, AMDGPU_VA_OP_UNMAP);
252 CU_ASSERT_EQUAL(r, 0);
254 r = amdgpu_va_range_free(vcn_bo->va_handle);
255 CU_ASSERT_EQUAL(r, 0);
257 r = amdgpu_bo_free(vcn_bo->handle);
258 CU_ASSERT_EQUAL(r, 0);
259 memset(vcn_bo, 0, sizeof(*vcn_bo));
262 static void vcn_dec_cmd(uint64_t addr, unsigned cmd, int *idx)
264 ib_cpu[(*idx)++] = reg.data0;
265 ib_cpu[(*idx)++] = addr;
266 ib_cpu[(*idx)++] = reg.data1;
267 ib_cpu[(*idx)++] = addr >> 32;
268 ib_cpu[(*idx)++] = reg.cmd;
269 ib_cpu[(*idx)++] = cmd << 1;
272 static void amdgpu_cs_vcn_dec_create(void)
274 struct amdgpu_vcn_bo msg_buf;
278 alloc_resource(&msg_buf, 4096, AMDGPU_GEM_DOMAIN_GTT);
279 resources[num_resources++] = msg_buf.handle;
280 resources[num_resources++] = ib_handle;
282 r = amdgpu_bo_cpu_map(msg_buf.handle, (void **)&msg_buf.ptr);
283 CU_ASSERT_EQUAL(r, 0);
285 memset(msg_buf.ptr, 0, 4096);
286 memcpy(msg_buf.ptr, vcn_dec_create_msg, sizeof(vcn_dec_create_msg));
289 ib_cpu[len++] = reg.data0;
290 ib_cpu[len++] = msg_buf.addr;
291 ib_cpu[len++] = reg.data1;
292 ib_cpu[len++] = msg_buf.addr >> 32;
293 ib_cpu[len++] = reg.cmd;
296 ib_cpu[len++] = reg.nop;
300 r = submit(len, AMDGPU_HW_IP_VCN_DEC);
301 CU_ASSERT_EQUAL(r, 0);
303 free_resource(&msg_buf);
306 static void amdgpu_cs_vcn_dec_decode(void)
308 const unsigned dpb_size = 15923584, dt_size = 737280;
309 uint64_t msg_addr, fb_addr, bs_addr, dpb_addr, ctx_addr, dt_addr, it_addr, sum;
310 struct amdgpu_vcn_bo dec_buf;
314 size = 4*1024; /* msg */
315 size += 4*1024; /* fb */
316 size += 4096; /*it_scaling_table*/
317 size += ALIGN(sizeof(uvd_bitstream), 4*1024);
318 size += ALIGN(dpb_size, 4*1024);
319 size += ALIGN(dt_size, 4*1024);
322 alloc_resource(&dec_buf, size, AMDGPU_GEM_DOMAIN_GTT);
323 resources[num_resources++] = dec_buf.handle;
324 resources[num_resources++] = ib_handle;
326 r = amdgpu_bo_cpu_map(dec_buf.handle, (void **)&dec_buf.ptr);
329 CU_ASSERT_EQUAL(r, 0);
330 memset(dec_buf.ptr, 0, size);
331 memcpy(dec_buf.ptr, vcn_dec_decode_msg, sizeof(vcn_dec_decode_msg));
332 memcpy(dec_buf.ptr + sizeof(vcn_dec_decode_msg),
333 avc_decode_msg, sizeof(avc_decode_msg));
337 memcpy(dec, uvd_it_scaling_table, sizeof(uvd_it_scaling_table));
340 memcpy(dec, uvd_bitstream, sizeof(uvd_bitstream));
342 dec += ALIGN(sizeof(uvd_bitstream), 4*1024);
344 dec += ALIGN(dpb_size, 4*1024);
346 msg_addr = dec_buf.addr;
347 fb_addr = msg_addr + 4*1024;
348 it_addr = fb_addr + 4*1024;
349 bs_addr = it_addr + 4*1024;
350 dpb_addr = ALIGN(bs_addr + sizeof(uvd_bitstream), 4*1024);
351 ctx_addr = ALIGN(dpb_addr + 0x006B9400, 4*1024);
352 dt_addr = ALIGN(dpb_addr + dpb_size, 4*1024);
355 vcn_dec_cmd(msg_addr, 0x0, &len);
356 vcn_dec_cmd(dpb_addr, 0x1, &len);
357 vcn_dec_cmd(dt_addr, 0x2, &len);
358 vcn_dec_cmd(fb_addr, 0x3, &len);
359 vcn_dec_cmd(bs_addr, 0x100, &len);
360 vcn_dec_cmd(it_addr, 0x204, &len);
361 vcn_dec_cmd(ctx_addr, 0x206, &len);
363 ib_cpu[len++] = reg.cntl;
366 ib_cpu[len++] = reg.nop;
370 r = submit(len, AMDGPU_HW_IP_VCN_DEC);
371 CU_ASSERT_EQUAL(r, 0);
373 for (i = 0, sum = 0; i < dt_size; ++i)
376 CU_ASSERT_EQUAL(sum, SUM_DECODE);
378 free_resource(&dec_buf);
381 static void amdgpu_cs_vcn_dec_destroy(void)
383 struct amdgpu_vcn_bo msg_buf;
387 alloc_resource(&msg_buf, 1024, AMDGPU_GEM_DOMAIN_GTT);
388 resources[num_resources++] = msg_buf.handle;
389 resources[num_resources++] = ib_handle;
391 r = amdgpu_bo_cpu_map(msg_buf.handle, (void **)&msg_buf.ptr);
392 CU_ASSERT_EQUAL(r, 0);
394 memset(msg_buf.ptr, 0, 1024);
395 memcpy(msg_buf.ptr, vcn_dec_destroy_msg, sizeof(vcn_dec_destroy_msg));
398 ib_cpu[len++] = reg.data0;
399 ib_cpu[len++] = msg_buf.addr;
400 ib_cpu[len++] = reg.data1;
401 ib_cpu[len++] = msg_buf.addr >> 32;
402 ib_cpu[len++] = reg.cmd;
405 ib_cpu[len++] = reg.nop;
409 r = submit(len, AMDGPU_HW_IP_VCN_DEC);
410 CU_ASSERT_EQUAL(r, 0);
412 free_resource(&msg_buf);
415 static void amdgpu_cs_vcn_enc_create(void)
420 static void amdgpu_cs_vcn_enc_encode(void)
425 static void amdgpu_cs_vcn_enc_destroy(void)