tests/amdgpu: add test to submit a gfx command with secure context
[platform/upstream/libdrm.git] / tests / amdgpu / security_tests.c
1 /*
2  * Copyright 2019 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 #include "CUnit/Basic.h"
25
26 #include "amdgpu_test.h"
27 #include "amdgpu_drm.h"
28 #include "amdgpu_internal.h"
29
30 static amdgpu_device_handle device_handle;
31 static uint32_t major_version;
32 static uint32_t minor_version;
33
34 static void amdgpu_security_alloc_buf_test(void);
35 static void amdgpu_security_gfx_submission_test(void);
36
37 CU_BOOL suite_security_tests_enable(void)
38 {
39         CU_BOOL enable = CU_TRUE;
40
41         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
42                                      &minor_version, &device_handle))
43                 return CU_FALSE;
44
45         if (device_handle->info.family_id < AMDGPU_FAMILY_RV) {
46                 printf("\n\nDon't support TMZ (trust memory zone), security suite disabled\n");
47                 enable = CU_FALSE;
48         }
49
50         if (amdgpu_device_deinitialize(device_handle))
51                 return CU_FALSE;
52
53         return enable;
54 }
55
56 int suite_security_tests_init(void)
57 {
58         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
59                                      &minor_version, &device_handle))
60                 return CUE_SINIT_FAILED;
61
62         return CUE_SUCCESS;
63 }
64
65 int suite_security_tests_clean(void)
66 {
67         int r;
68
69         r = amdgpu_device_deinitialize(device_handle);
70         if (r)
71                 return CUE_SCLEAN_FAILED;
72
73         return CUE_SUCCESS;
74 }
75
76
77 CU_TestInfo security_tests[] = {
78         { "allocate secure buffer test", amdgpu_security_alloc_buf_test },
79         { "graphics secure command submission", amdgpu_security_gfx_submission_test },
80         CU_TEST_INFO_NULL,
81 };
82
83 static void amdgpu_security_alloc_buf_test(void)
84 {
85         amdgpu_bo_handle bo;
86         amdgpu_va_handle va_handle;
87         uint64_t bo_mc;
88         int r;
89
90         /* Test secure buffer allocation in VRAM */
91         bo = gpu_mem_alloc(device_handle, 4096, 4096,
92                            AMDGPU_GEM_DOMAIN_VRAM,
93                            AMDGPU_GEM_CREATE_ENCRYPTED,
94                            &bo_mc, &va_handle);
95
96         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
97         CU_ASSERT_EQUAL(r, 0);
98
99         /* Test secure buffer allocation in system memory */
100         bo = gpu_mem_alloc(device_handle, 4096, 4096,
101                            AMDGPU_GEM_DOMAIN_GTT,
102                            AMDGPU_GEM_CREATE_ENCRYPTED,
103                            &bo_mc, &va_handle);
104
105         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
106         CU_ASSERT_EQUAL(r, 0);
107
108         /* Test secure buffer allocation in invisible VRAM */
109         bo = gpu_mem_alloc(device_handle, 4096, 4096,
110                            AMDGPU_GEM_DOMAIN_GTT,
111                            AMDGPU_GEM_CREATE_ENCRYPTED |
112                            AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
113                            &bo_mc, &va_handle);
114
115         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
116         CU_ASSERT_EQUAL(r, 0);
117 }
118
119 static void amdgpu_security_gfx_submission_test(void)
120 {
121         amdgpu_command_submission_write_linear_helper_with_secure(device_handle,
122                                                                   AMDGPU_HW_IP_GFX,
123                                                                   true);
124 }