tests/amdgpu: add uvd unit test support for vega10
[platform/upstream/libdrm.git] / tests / amdgpu / cs_tests.c
1 /*
2  * Copyright 2014 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <stdio.h>
29
30 #include "CUnit/Basic.h"
31
32 #include "util_math.h"
33
34 #include "amdgpu_test.h"
35 #include "uvd_messages.h"
36 #include "amdgpu_drm.h"
37 #include "amdgpu_internal.h"
38
39 #define IB_SIZE         4096
40 #define MAX_RESOURCES   16
41
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;
48
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;
54
55 static amdgpu_bo_handle resources[MAX_RESOURCES];
56 static unsigned num_resources;
57
58 static void amdgpu_cs_uvd_create(void);
59 static void amdgpu_cs_uvd_decode(void);
60 static void amdgpu_cs_uvd_destroy(void);
61
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 },
66         CU_TEST_INFO_NULL,
67 };
68
69 int suite_cs_tests_init(void)
70 {
71         amdgpu_bo_handle ib_result_handle;
72         void *ib_result_cpu;
73         uint64_t ib_result_mc_address;
74         amdgpu_va_handle ib_result_va_handle;
75         int r;
76
77         r = amdgpu_device_initialize(drm_amdgpu[0], &major_version,
78                                      &minor_version, &device_handle);
79         if (r) {
80                 if ((r == -EACCES) && (errno == EACCES))
81                         printf("\n\nError:%s. "
82                                 "Hint:Try to run this test program as root.",
83                                 strerror(errno));
84
85                 return CUE_SINIT_FAILED;
86         }
87
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;
92
93         r = amdgpu_cs_ctx_create(device_handle, &context_handle);
94         if (r)
95                 return CUE_SINIT_FAILED;
96
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);
102         if (r)
103                 return CUE_SINIT_FAILED;
104
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;
109
110         return CUE_SUCCESS;
111 }
112
113 int suite_cs_tests_clean(void)
114 {
115         int r;
116
117         r = amdgpu_bo_unmap_and_free(ib_handle, ib_va_handle,
118                                      ib_mc_address, IB_SIZE);
119         if (r)
120                 return CUE_SCLEAN_FAILED;
121
122         r = amdgpu_cs_ctx_free(context_handle);
123         if (r)
124                 return CUE_SCLEAN_FAILED;
125
126         r = amdgpu_device_deinitialize(device_handle);
127         if (r)
128                 return CUE_SCLEAN_FAILED;
129
130         return CUE_SUCCESS;
131 }
132
133 static int submit(unsigned ndw, unsigned ip)
134 {
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};
138         uint32_t expired;
139         int r;
140
141         ib_info.ib_mc_address = ib_mc_address;
142         ib_info.size = ndw;
143
144         ibs_request.ip_type = ip;
145
146         r = amdgpu_bo_list_create(device_handle, num_resources, resources,
147                                   NULL, &ibs_request.resources);
148         if (r)
149                 return r;
150
151         ibs_request.number_of_ibs = 1;
152         ibs_request.ibs = &ib_info;
153         ibs_request.fence_info.handle = NULL;
154
155         r = amdgpu_cs_submit(context_handle, 0, &ibs_request, 1);
156         if (r)
157                 return r;
158
159         r = amdgpu_bo_list_destroy(ibs_request.resources);
160         if (r)
161                 return r;
162
163         fence_status.context = context_handle;
164         fence_status.ip_type = ip;
165         fence_status.fence = ibs_request.seq_no;
166
167         r = amdgpu_cs_query_fence_status(&fence_status,
168                                          AMDGPU_TIMEOUT_INFINITE,
169                                          0, &expired);
170         if (r)
171                 return r;
172
173         return 0;
174 }
175
176 static void uvd_cmd(uint64_t addr, unsigned cmd, int *idx)
177 {
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;
184 }
185
186 static void amdgpu_cs_uvd_create(void)
187 {
188         struct amdgpu_bo_alloc_request req = {0};
189         amdgpu_bo_handle buf_handle;
190         uint64_t va = 0;
191         amdgpu_va_handle va_handle;
192         void *msg;
193         int i, r;
194
195         req.alloc_size = 4*1024;
196         req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
197
198         r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
199         CU_ASSERT_EQUAL(r, 0);
200
201         r = amdgpu_va_range_alloc(device_handle,
202                                   amdgpu_gpu_va_range_general,
203                                   4096, 1, 0, &va,
204                                   &va_handle, 0);
205         CU_ASSERT_EQUAL(r, 0);
206
207         r = amdgpu_bo_va_op(buf_handle, 0, 4096, va, 0, AMDGPU_VA_OP_MAP);
208         CU_ASSERT_EQUAL(r, 0);
209
210         r = amdgpu_bo_cpu_map(buf_handle, &msg);
211         CU_ASSERT_EQUAL(r, 0);
212
213         memcpy(msg, uvd_create_msg, sizeof(uvd_create_msg));
214
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)) {
220                         /* dpb size */
221                         ((uint8_t*)msg)[0x28] = 0x00;
222                         ((uint8_t*)msg)[0x29] = 0x94;
223                         ((uint8_t*)msg)[0x2A] = 0x6B;
224                         ((uint8_t*)msg)[0x2B] = 0x00;
225                 }
226         }
227
228         r = amdgpu_bo_cpu_unmap(buf_handle);
229         CU_ASSERT_EQUAL(r, 0);
230
231         num_resources = 0;
232         resources[num_resources++] = buf_handle;
233         resources[num_resources++] = ib_handle;
234
235         i = 0;
236         uvd_cmd(va, 0x0, &i);
237         for (; i % 16; ++i)
238                 ib_cpu[i] = 0x80000000;
239
240         r = submit(i, AMDGPU_HW_IP_UVD);
241         CU_ASSERT_EQUAL(r, 0);
242
243         r = amdgpu_bo_va_op(buf_handle, 0, 4096, va, 0, AMDGPU_VA_OP_UNMAP);
244         CU_ASSERT_EQUAL(r, 0);
245
246         r = amdgpu_va_range_free(va_handle);
247         CU_ASSERT_EQUAL(r, 0);
248
249         r = amdgpu_bo_free(buf_handle);
250         CU_ASSERT_EQUAL(r, 0);
251 }
252
253 static void amdgpu_cs_uvd_decode(void)
254 {
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;
260         uint64_t va = 0;
261         uint64_t sum;
262         uint8_t *ptr;
263         int i, r;
264
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);
272
273         req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
274
275         r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
276         CU_ASSERT_EQUAL(r, 0);
277
278         r = amdgpu_va_range_alloc(device_handle,
279                                   amdgpu_gpu_va_range_general,
280                                   req.alloc_size, 1, 0, &va,
281                                   &va_handle, 0);
282         CU_ASSERT_EQUAL(r, 0);
283
284         r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
285                             AMDGPU_VA_OP_MAP);
286         CU_ASSERT_EQUAL(r, 0);
287
288         r = amdgpu_bo_cpu_map(buf_handle, (void **)&ptr);
289         CU_ASSERT_EQUAL(r, 0);
290
291         memcpy(ptr, uvd_decode_msg, sizeof(uvd_create_msg));
292
293         if (family_id >= AMDGPU_FAMILY_VI) {
294                 ptr[0x10] = 7;
295                 ptr[0x98] = 0x00;
296                 ptr[0x99] = 0x02;
297                 /* chip beyond polaris10/11 */
298                 if ((family_id == AMDGPU_FAMILY_AI) ||
299                     (chip_id == chip_rev+0x50 || chip_id == chip_rev+0x5A)) {
300                         /* dpb size */
301                         ptr[0x24] = 0x00;
302                         ptr[0x25] = 0x94;
303                         ptr[0x26] = 0x6B;
304                         ptr[0x27] = 0x00;
305                         /*ctx size */
306                         ptr[0x2C] = 0x00;
307                         ptr[0x2D] = 0xAF;
308                         ptr[0x2E] = 0x50;
309                         ptr[0x2F] = 0x00;
310                 }
311         }
312
313         ptr += 4*1024;
314         memset(ptr, 0, 4*1024);
315         if (family_id >= AMDGPU_FAMILY_VI) {
316                 ptr += 4*1024;
317                 memcpy(ptr, uvd_it_scaling_table, sizeof(uvd_it_scaling_table));
318         }
319
320         ptr += 4*1024;
321         memcpy(ptr, uvd_bitstream, sizeof(uvd_bitstream));
322
323         ptr += ALIGN(sizeof(uvd_bitstream), 4*1024);
324         memset(ptr, 0, dpb_size);
325
326         ptr += ALIGN(dpb_size, 4*1024);
327         memset(ptr, 0, dt_size);
328
329         num_resources = 0;
330         resources[num_resources++] = buf_handle;
331         resources[num_resources++] = ib_handle;
332
333         msg_addr = va;
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;
338         } else
339                 bs_addr = fb_addr + 4*1024;
340         dpb_addr = ALIGN(bs_addr + sizeof(uvd_bitstream), 4*1024);
341
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);
346                 }
347         }
348
349         dt_addr = ALIGN(dpb_addr + dpb_size, 4*1024);
350
351         i = 0;
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);
357
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);
363         }
364
365         ib_cpu[i++] = (family_id < AMDGPU_FAMILY_AI) ? 0x3BC6 : 0x81C6;
366         ib_cpu[i++] = 0x1;
367         for (; i % 16; ++i)
368                 ib_cpu[i] = 0x80000000;
369
370         r = submit(i, AMDGPU_HW_IP_UVD);
371         CU_ASSERT_EQUAL(r, 0);
372
373         /* TODO: use a real CRC32 */
374         for (i = 0, sum = 0; i < dt_size; ++i)
375                 sum += ptr[i];
376         CU_ASSERT_EQUAL(sum, 0x20345d8);
377
378         r = amdgpu_bo_cpu_unmap(buf_handle);
379         CU_ASSERT_EQUAL(r, 0);
380
381         r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0, AMDGPU_VA_OP_UNMAP);
382         CU_ASSERT_EQUAL(r, 0);
383
384         r = amdgpu_va_range_free(va_handle);
385         CU_ASSERT_EQUAL(r, 0);
386
387         r = amdgpu_bo_free(buf_handle);
388         CU_ASSERT_EQUAL(r, 0);
389 }
390
391 static void amdgpu_cs_uvd_destroy(void)
392 {
393         struct amdgpu_bo_alloc_request req = {0};
394         amdgpu_bo_handle buf_handle;
395         amdgpu_va_handle va_handle;
396         uint64_t va = 0;
397         void *msg;
398         int i, r;
399
400         req.alloc_size = 4*1024;
401         req.preferred_heap = AMDGPU_GEM_DOMAIN_GTT;
402
403         r = amdgpu_bo_alloc(device_handle, &req, &buf_handle);
404         CU_ASSERT_EQUAL(r, 0);
405
406         r = amdgpu_va_range_alloc(device_handle,
407                                   amdgpu_gpu_va_range_general,
408                                   req.alloc_size, 1, 0, &va,
409                                   &va_handle, 0);
410         CU_ASSERT_EQUAL(r, 0);
411
412         r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0,
413                             AMDGPU_VA_OP_MAP);
414         CU_ASSERT_EQUAL(r, 0);
415
416         r = amdgpu_bo_cpu_map(buf_handle, &msg);
417         CU_ASSERT_EQUAL(r, 0);
418
419         memcpy(msg, uvd_destroy_msg, sizeof(uvd_destroy_msg));
420         if (family_id >= AMDGPU_FAMILY_VI)
421                 ((uint8_t*)msg)[0x10] = 7;
422
423         r = amdgpu_bo_cpu_unmap(buf_handle);
424         CU_ASSERT_EQUAL(r, 0);
425
426         num_resources = 0;
427         resources[num_resources++] = buf_handle;
428         resources[num_resources++] = ib_handle;
429
430         i = 0;
431         uvd_cmd(va, 0x0, &i);
432         for (; i % 16; ++i)
433                 ib_cpu[i] = 0x80000000;
434
435         r = submit(i, AMDGPU_HW_IP_UVD);
436         CU_ASSERT_EQUAL(r, 0);
437
438         r = amdgpu_bo_va_op(buf_handle, 0, req.alloc_size, va, 0, AMDGPU_VA_OP_UNMAP);
439         CU_ASSERT_EQUAL(r, 0);
440
441         r = amdgpu_va_range_free(va_handle);
442         CU_ASSERT_EQUAL(r, 0);
443
444         r = amdgpu_bo_free(buf_handle);
445         CU_ASSERT_EQUAL(r, 0);
446 }