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.
25 #include "CUnit/Basic.h"
27 #include "util_math.h"
29 #include "amdgpu_test.h"
30 #include "uvd_messages.h"
31 #include "amdgpu_drm.h"
32 #include "amdgpu_internal.h"
35 #define MAX_RESOURCES 16
37 static amdgpu_device_handle device_handle;
38 static uint32_t major_version;
39 static uint32_t minor_version;
40 static uint32_t family_id;
42 static amdgpu_context_handle context_handle;
43 static amdgpu_bo_handle ib_handle;
44 static uint64_t ib_mc_address;
45 static uint32_t *ib_cpu;
47 static amdgpu_bo_handle resources[MAX_RESOURCES];
48 static unsigned num_resources;
50 static void amdgpu_cs_uvd_create(void);
51 static void amdgpu_cs_uvd_decode(void);
52 static void amdgpu_cs_uvd_destroy(void);
54 CU_TestInfo cs_tests[] = {
55 { "UVD create", amdgpu_cs_uvd_create },
56 { "UVD decode", amdgpu_cs_uvd_decode },
57 { "UVD destroy", amdgpu_cs_uvd_destroy },
61 int suite_cs_tests_init(void)
63 amdgpu_bo_handle ib_result_handle;
65 uint64_t ib_result_mc_address;
68 r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
69 &minor_version, &device_handle);
71 return CUE_SINIT_FAILED;
73 family_id = device_handle->info.family_id;
75 r = amdgpu_cs_ctx_create(device_handle, &context_handle);
77 return CUE_SINIT_FAILED;
79 r = amdgpu_bo_alloc_and_map(device_handle, IB_SIZE, 4096,
80 AMDGPU_GEM_DOMAIN_GTT, 0,
81 &ib_result_handle, &ib_result_cpu,
82 &ib_result_mc_address);
84 return CUE_SINIT_FAILED;
86 ib_handle = ib_result_handle;
87 ib_mc_address = ib_result_mc_address;
88 ib_cpu = ib_result_cpu;
93 int suite_cs_tests_clean(void)
97 r = amdgpu_bo_free(ib_handle);
99 return CUE_SCLEAN_FAILED;
101 r = amdgpu_cs_ctx_free(context_handle);
103 return CUE_SCLEAN_FAILED;
105 r = amdgpu_device_deinitialize(device_handle);
107 return CUE_SCLEAN_FAILED;
112 static int submit(unsigned ndw, unsigned ip)
114 struct amdgpu_cs_request ibs_request = {0};
115 struct amdgpu_cs_ib_info ib_info = {0};
116 struct amdgpu_cs_query_fence fence_status = {0};
120 ib_info.ib_mc_address = ib_mc_address;
123 ibs_request.ip_type = ip;
125 r = amdgpu_bo_list_create(device_handle, num_resources, resources,
126 NULL, &ibs_request.resources);
130 ibs_request.number_of_ibs = 1;
131 ibs_request.ibs = &ib_info;
133 r = amdgpu_cs_submit(context_handle, 0,
134 &ibs_request, 1, &fence_status.fence);
138 r = amdgpu_bo_list_destroy(ibs_request.resources);
142 fence_status.context = context_handle;
143 fence_status.timeout_ns = AMDGPU_TIMEOUT_INFINITE;
144 fence_status.ip_type = ip;
146 r = amdgpu_cs_query_fence_status(&fence_status, &expired);
153 static void uvd_cmd(uint64_t addr, unsigned cmd, int *idx)
155 ib_cpu[(*idx)++] = 0x3BC4;
156 ib_cpu[(*idx)++] = addr;
157 ib_cpu[(*idx)++] = 0x3BC5;
158 ib_cpu[(*idx)++] = addr >> 32;
159 ib_cpu[(*idx)++] = 0x3BC3;
160 ib_cpu[(*idx)++] = cmd << 1;
163 static void amdgpu_cs_uvd_create(void)
165 struct amdgpu_bo_alloc_request req = {0};
166 struct amdgpu_bo_alloc_result res = {0};
170 req.alloc_size = 4*1024;
171 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
173 r = amdgpu_bo_alloc(device_handle, &req, &res);
174 CU_ASSERT_EQUAL(r, 0);
176 r = amdgpu_bo_cpu_map(res.buf_handle, &msg);
177 CU_ASSERT_EQUAL(r, 0);
179 memcpy(msg, uvd_create_msg, sizeof(uvd_create_msg));
180 if (family_id >= AMDGPU_FAMILY_VI)
181 ((uint8_t*)msg)[0x10] = 7;
183 r = amdgpu_bo_cpu_unmap(res.buf_handle);
184 CU_ASSERT_EQUAL(r, 0);
187 resources[num_resources++] = res.buf_handle;
188 resources[num_resources++] = ib_handle;
191 uvd_cmd(res.virtual_mc_base_address, 0x0, &i);
193 ib_cpu[i] = 0x80000000;
195 r = submit(i, AMDGPU_HW_IP_UVD);
196 CU_ASSERT_EQUAL(r, 0);
198 r = amdgpu_bo_free(resources[0]);
199 CU_ASSERT_EQUAL(r, 0);
202 static void amdgpu_cs_uvd_decode(void)
204 const unsigned dpb_size = 15923584, dt_size = 737280;
205 uint64_t msg_addr, fb_addr, bs_addr, dpb_addr, dt_addr, it_addr;
206 struct amdgpu_bo_alloc_request req = {0};
207 struct amdgpu_bo_alloc_result res = {0};
212 req.alloc_size = 4*1024; /* msg */
213 req.alloc_size += 4*1024; /* fb */
214 if (family_id >= AMDGPU_FAMILY_VI)
215 req.alloc_size += 4096; /*it_scaling_table*/
216 req.alloc_size += ALIGN(sizeof(uvd_bitstream), 4*1024);
217 req.alloc_size += ALIGN(dpb_size, 4*1024);
218 req.alloc_size += ALIGN(dt_size, 4*1024);
220 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
222 r = amdgpu_bo_alloc(device_handle, &req, &res);
223 CU_ASSERT_EQUAL(r, 0);
225 r = amdgpu_bo_cpu_map(res.buf_handle, (void **)&ptr);
226 CU_ASSERT_EQUAL(r, 0);
228 memcpy(ptr, uvd_decode_msg, sizeof(uvd_create_msg));
229 if (family_id >= AMDGPU_FAMILY_VI)
233 memset(ptr, 0, 4*1024);
234 if (family_id >= AMDGPU_FAMILY_VI) {
236 memcpy(ptr, uvd_it_scaling_table, sizeof(uvd_it_scaling_table));
240 memcpy(ptr, uvd_bitstream, sizeof(uvd_bitstream));
242 ptr += ALIGN(sizeof(uvd_bitstream), 4*1024);
243 memset(ptr, 0, dpb_size);
245 ptr += ALIGN(dpb_size, 4*1024);
246 memset(ptr, 0, dt_size);
249 resources[num_resources++] = res.buf_handle;
250 resources[num_resources++] = ib_handle;
252 msg_addr = res.virtual_mc_base_address;
253 fb_addr = msg_addr + 4*1024;
254 if (family_id >= AMDGPU_FAMILY_VI) {
255 it_addr = fb_addr + 4*1024;
256 bs_addr = it_addr + 4*1024;
258 bs_addr = fb_addr + 4*1024;
259 dpb_addr = ALIGN(bs_addr + sizeof(uvd_bitstream), 4*1024);
260 dt_addr = ALIGN(dpb_addr + dpb_size, 4*1024);
263 uvd_cmd(msg_addr, 0x0, &i);
264 uvd_cmd(dpb_addr, 0x1, &i);
265 uvd_cmd(dt_addr, 0x2, &i);
266 uvd_cmd(fb_addr, 0x3, &i);
267 uvd_cmd(bs_addr, 0x100, &i);
268 if (family_id >= AMDGPU_FAMILY_VI)
269 uvd_cmd(it_addr, 0x204, &i);
270 ib_cpu[i++] = 0x3BC6;
273 ib_cpu[i] = 0x80000000;
275 r = submit(i, AMDGPU_HW_IP_UVD);
276 CU_ASSERT_EQUAL(r, 0);
278 /* TODO: use a real CRC32 */
279 for (i = 0, sum = 0; i < dt_size; ++i)
281 CU_ASSERT_EQUAL(sum, 0x20345d8);
283 r = amdgpu_bo_cpu_unmap(res.buf_handle);
284 CU_ASSERT_EQUAL(r, 0);
286 r = amdgpu_bo_free(resources[0]);
287 CU_ASSERT_EQUAL(r, 0);
290 static void amdgpu_cs_uvd_destroy(void)
292 struct amdgpu_bo_alloc_request req = {0};
293 struct amdgpu_bo_alloc_result res = {0};
297 req.alloc_size = 4*1024;
298 req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
300 r = amdgpu_bo_alloc(device_handle, &req, &res);
301 CU_ASSERT_EQUAL(r, 0);
303 r = amdgpu_bo_cpu_map(res.buf_handle, &msg);
304 CU_ASSERT_EQUAL(r, 0);
306 memcpy(msg, uvd_destroy_msg, sizeof(uvd_create_msg));
307 if (family_id >= AMDGPU_FAMILY_VI)
308 ((uint8_t*)msg)[0x10] = 7;
310 r = amdgpu_bo_cpu_unmap(res.buf_handle);
311 CU_ASSERT_EQUAL(r, 0);
314 resources[num_resources++] = res.buf_handle;
315 resources[num_resources++] = ib_handle;
318 uvd_cmd(res.virtual_mc_base_address, 0x0, &i);
320 ib_cpu[i] = 0x80000000;
322 r = submit(i, AMDGPU_HW_IP_UVD);
323 CU_ASSERT_EQUAL(r, 0);
325 r = amdgpu_bo_free(resources[0]);
326 CU_ASSERT_EQUAL(r, 0);