2 * Copyright 2014 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.
30 #include "CUnit/Basic.h"
32 #include "util_math.h"
34 #include "amdgpu_test.h"
35 #include "uvd_messages.h"
36 #include "amdgpu_drm.h"
37 #include "amdgpu_internal.h"
40 #define MAX_RESOURCES 16
42 static amdgpu_device_handle device_handle;
43 static uint32_t major_version;
44 static uint32_t minor_version;
45 static uint32_t family_id;
46 static uint32_t chip_rev;
47 static uint32_t chip_id;
49 static amdgpu_context_handle context_handle;
50 static amdgpu_bo_handle ib_handle;
51 static uint64_t ib_mc_address;
52 static uint32_t *ib_cpu;
53 static amdgpu_va_handle ib_va_handle;
55 static amdgpu_bo_handle resources[MAX_RESOURCES];
56 static unsigned num_resources;
58 static void amdgpu_cs_uvd_create(void);
59 static void amdgpu_cs_uvd_decode(void);
60 static void amdgpu_cs_uvd_destroy(void);
62 CU_TestInfo cs_tests[] = {
63 { "UVD create", amdgpu_cs_uvd_create },
64 { "UVD decode", amdgpu_cs_uvd_decode },
65 { "UVD destroy", amdgpu_cs_uvd_destroy },
69 int suite_cs_tests_init(void)
71 amdgpu_bo_handle ib_result_handle;
73 uint64_t ib_result_mc_address;
74 amdgpu_va_handle ib_result_va_handle;
77 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
78 &minor_version, &device_handle);
80 if ((r == -EACCES) && (errno == EACCES))
81 printf("\n\nError:%s. "
82 "Hint:Try to run this test program as root.",
85 return CUE_SINIT_FAILED;
88 family_id = device_handle->info.family_id;
89 /* VI asic POLARIS10/11 have specific external_rev_id */
90 chip_rev = device_handle->info.chip_rev;
91 chip_id = device_handle->info.chip_external_rev;
93 r = amdgpu_cs_ctx_create(device_handle, &context_handle);
95 return CUE_SINIT_FAILED;
97 r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
98 AMDGPU_GEM_DOMAIN_GTT, 0,
99 &ib_result_handle, &ib_result_cpu,
100 &ib_result_mc_address,
101 &ib_result_va_handle);
103 return CUE_SINIT_FAILED;
105 ib_handle = ib_result_handle;
106 ib_mc_address = ib_result_mc_address;
107 ib_cpu = ib_result_cpu;
108 ib_va_handle = ib_result_va_handle;
113 int suite_cs_tests_clean(void)
117 r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
118 ib_mc_address, IB_SIZE);
120 return CUE_SCLEAN_FAILED;
122 r = amdgpu_cs_ctx_free(context_handle);
124 return CUE_SCLEAN_FAILED;
126 r = amdgpu_device_deinitialize(device_handle);
128 return CUE_SCLEAN_FAILED;
133 static int submit(unsigned ndw, unsigned ip)
135 struct amdgpu_cs_request ibs_request = {0};
136 struct amdgpu_cs_ib_info ib_info = {0};
137 struct amdgpu_cs_fence fence_status = {0};
141 ib_info.ib_mc_address = ib_mc_address;
144 ibs_request.ip_type = ip;
146 r = amdgpu_bo_list_create(device_handle, num_resources, resources,
147 NULL, &ibs_request.resources);
151 ibs_request.number_of_ibs = 1;
152 ibs_request.ibs = &ib_info;
153 ibs_request.fence_info.handle = NULL;
155 r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
159 r = amdgpu_bo_list_destroy(ibs_request.resources);
163 fence_status.context = context_handle;
164 fence_status.ip_type = ip;
165 fence_status.fence = ibs_request.seq_no;
167 r = amdgpu_cs_query_fence_status(&fence_status,
168 AMDGPU_TIMEOUT_INFINITE,
176 static void uvd_cmd(uint64_t addr, unsigned cmd, int *idx)
178 ib_cpu[(*idx)++] = (family_id < AMDGPU_FAMILY_AI) ? 0x3BC4 : 0x81C4;
179 ib_cpu[(*idx)++] = addr;
180 ib_cpu[(*idx)++] = (family_id < AMDGPU_FAMILY_AI) ? 0x3BC5 : 0x81C5;
181 ib_cpu[(*idx)++] = addr >> 32;
182 ib_cpu[(*idx)++] = (family_id < AMDGPU_FAMILY_AI) ? 0x3BC3 : 0x81C3;
183 ib_cpu[(*idx)++] = cmd << 1;
186 static void amdgpu_cs_uvd_create(void)
188 struct amdgpu_bo_alloc_request req = {0};
189 amdgpu_bo_handle buf_handle;
191 amdgpu_va_handle va_handle;
195 req.alloc_size = 4*1024;
196 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
198 r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
199 CU_ASSERT_EQUAL(r, 0);
201 r = amdgpu_va_range_alloc(device_handle,
202 amdgpu_gpu_va_range_general,
205 CU_ASSERT_EQUAL(r, 0);
207 r = amdgpu_bo_va_op(buf_handle, 0, 4096, va, 0, AMDGPU_VA_OP_MAP);
208 CU_ASSERT_EQUAL(r, 0);
210 r = amdgpu_bo_cpu_map(buf_handle, &msg);
211 CU_ASSERT_EQUAL(r, 0);
213 memcpy(msg, uvd_create_msg, sizeof(uvd_create_msg));
215 if (family_id >= AMDGPU_FAMILY_VI) {
216 ((uint8_t*)msg)[0x10] = 7;
217 /* chip beyond polaris 10/11 */
218 if ((family_id == AMDGPU_FAMILY_AI) ||
219 (chip_id == chip_rev+0x50 || chip_id == chip_rev+0x5A)) {
221 ((uint8_t*)msg)[0x28] = 0x00;
222 ((uint8_t*)msg)[0x29] = 0x94;
223 ((uint8_t*)msg)[0x2A] = 0x6B;
224 ((uint8_t*)msg)[0x2B] = 0x00;
228 r = amdgpu_bo_cpu_unmap(buf_handle);
229 CU_ASSERT_EQUAL(r, 0);
232 resources[num_resources++] = buf_handle;
233 resources[num_resources++] = ib_handle;
236 uvd_cmd(va, 0x0, &i);
238 ib_cpu[i] = 0x80000000;
240 r = submit(i, AMDGPU_HW_IP_UVD);
241 CU_ASSERT_EQUAL(r, 0);
243 r = amdgpu_bo_va_op(buf_handle, 0, 4096, va, 0, AMDGPU_VA_OP_UNMAP);
244 CU_ASSERT_EQUAL(r, 0);
246 r = amdgpu_va_range_free(va_handle);
247 CU_ASSERT_EQUAL(r, 0);
249 r = amdgpu_bo_free(buf_handle);
250 CU_ASSERT_EQUAL(r, 0);
253 static void amdgpu_cs_uvd_decode(void)
255 const unsigned dpb_size = 15923584, ctx_size = 5287680, dt_size = 737280;
256 uint64_t msg_addr, fb_addr, bs_addr, dpb_addr, ctx_addr, dt_addr, it_addr;
257 struct amdgpu_bo_alloc_request req = {0};
258 amdgpu_bo_handle buf_handle;
259 amdgpu_va_handle va_handle;
265 req.alloc_size = 4*1024; /* msg */
266 req.alloc_size += 4*1024; /* fb */
267 if (family_id >= AMDGPU_FAMILY_VI)
268 req.alloc_size += 4096; /*it_scaling_table*/
269 req.alloc_size += ALIGN(sizeof(uvd_bitstream), 4*1024);
270 req.alloc_size += ALIGN(dpb_size, 4*1024);
271 req.alloc_size += ALIGN(dt_size, 4*1024);
273 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
275 r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
276 CU_ASSERT_EQUAL(r, 0);
278 r = amdgpu_va_range_alloc(device_handle,
279 amdgpu_gpu_va_range_general,
280 req.alloc_size, 1, 0, &va,
282 CU_ASSERT_EQUAL(r, 0);
284 r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
286 CU_ASSERT_EQUAL(r, 0);
288 r = amdgpu_bo_cpu_map(buf_handle, (void **)&ptr);
289 CU_ASSERT_EQUAL(r, 0);
291 memcpy(ptr, uvd_decode_msg, sizeof(uvd_create_msg));
293 if (family_id >= AMDGPU_FAMILY_VI) {
297 /* chip beyond polaris10/11 */
298 if ((family_id == AMDGPU_FAMILY_AI) ||
299 (chip_id == chip_rev+0x50 || chip_id == chip_rev+0x5A)) {
314 memset(ptr, 0, 4*1024);
315 if (family_id >= AMDGPU_FAMILY_VI) {
317 memcpy(ptr, uvd_it_scaling_table, sizeof(uvd_it_scaling_table));
321 memcpy(ptr, uvd_bitstream, sizeof(uvd_bitstream));
323 ptr += ALIGN(sizeof(uvd_bitstream), 4*1024);
324 memset(ptr, 0, dpb_size);
326 ptr += ALIGN(dpb_size, 4*1024);
327 memset(ptr, 0, dt_size);
330 resources[num_resources++] = buf_handle;
331 resources[num_resources++] = ib_handle;
334 fb_addr = msg_addr + 4*1024;
335 if (family_id >= AMDGPU_FAMILY_VI) {
336 it_addr = fb_addr + 4*1024;
337 bs_addr = it_addr + 4*1024;
339 bs_addr = fb_addr + 4*1024;
340 dpb_addr = ALIGN(bs_addr + sizeof(uvd_bitstream), 4*1024);
342 if (family_id >= AMDGPU_FAMILY_VI) {
343 if ((family_id == AMDGPU_FAMILY_AI) ||
344 (chip_id == chip_rev+0x50 || chip_id == chip_rev+0x5A)) {
345 ctx_addr = ALIGN(dpb_addr + 0x006B9400, 4*1024);
349 dt_addr = ALIGN(dpb_addr + dpb_size, 4*1024);
352 uvd_cmd(msg_addr, 0x0, &i);
353 uvd_cmd(dpb_addr, 0x1, &i);
354 uvd_cmd(dt_addr, 0x2, &i);
355 uvd_cmd(fb_addr, 0x3, &i);
356 uvd_cmd(bs_addr, 0x100, &i);
358 if (family_id >= AMDGPU_FAMILY_VI) {
359 uvd_cmd(it_addr, 0x204, &i);
360 if ((family_id == AMDGPU_FAMILY_AI) ||
361 (chip_id == chip_rev+0x50 || chip_id == chip_rev+0x5A))
362 uvd_cmd(ctx_addr, 0x206, &i);
365 ib_cpu[i++] = (family_id < AMDGPU_FAMILY_AI) ? 0x3BC6 : 0x81C6;
368 ib_cpu[i] = 0x80000000;
370 r = submit(i, AMDGPU_HW_IP_UVD);
371 CU_ASSERT_EQUAL(r, 0);
373 /* TODO: use a real CRC32 */
374 for (i = 0, sum = 0; i < dt_size; ++i)
376 CU_ASSERT_EQUAL(sum, 0x20345d8);
378 r = amdgpu_bo_cpu_unmap(buf_handle);
379 CU_ASSERT_EQUAL(r, 0);
381 r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0, AMDGPU_VA_OP_UNMAP);
382 CU_ASSERT_EQUAL(r, 0);
384 r = amdgpu_va_range_free(va_handle);
385 CU_ASSERT_EQUAL(r, 0);
387 r = amdgpu_bo_free(buf_handle);
388 CU_ASSERT_EQUAL(r, 0);
391 static void amdgpu_cs_uvd_destroy(void)
393 struct amdgpu_bo_alloc_request req = {0};
394 amdgpu_bo_handle buf_handle;
395 amdgpu_va_handle va_handle;
400 req.alloc_size = 4*1024;
401 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
403 r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
404 CU_ASSERT_EQUAL(r, 0);
406 r = amdgpu_va_range_alloc(device_handle,
407 amdgpu_gpu_va_range_general,
408 req.alloc_size, 1, 0, &va,
410 CU_ASSERT_EQUAL(r, 0);
412 r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
414 CU_ASSERT_EQUAL(r, 0);
416 r = amdgpu_bo_cpu_map(buf_handle, &msg);
417 CU_ASSERT_EQUAL(r, 0);
419 memcpy(msg, uvd_destroy_msg, sizeof(uvd_destroy_msg));
420 if (family_id >= AMDGPU_FAMILY_VI)
421 ((uint8_t*)msg)[0x10] = 7;
423 r = amdgpu_bo_cpu_unmap(buf_handle);
424 CU_ASSERT_EQUAL(r, 0);
427 resources[num_resources++] = buf_handle;
428 resources[num_resources++] = ib_handle;
431 uvd_cmd(va, 0x0, &i);
433 ib_cpu[i] = 0x80000000;
435 r = submit(i, AMDGPU_HW_IP_UVD);
436 CU_ASSERT_EQUAL(r, 0);
438 r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0, AMDGPU_VA_OP_UNMAP);
439 CU_ASSERT_EQUAL(r, 0);
441 r = amdgpu_va_range_free(va_handle);
442 CU_ASSERT_EQUAL(r, 0);
444 r = amdgpu_bo_free(buf_handle);
445 CU_ASSERT_EQUAL(r, 0);