tests/amdgpu: Remove forward declarations
[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 {
36         amdgpu_bo_handle bo;
37         amdgpu_va_handle va_handle;
38         uint64_t bo_mc;
39         int r;
40
41         /* Test secure buffer allocation in VRAM */
42         bo = gpu_mem_alloc(device_handle, 4096, 4096,
43                            AMDGPU_GEM_DOMAIN_VRAM,
44                            AMDGPU_GEM_CREATE_ENCRYPTED,
45                            &bo_mc, &va_handle);
46
47         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
48         CU_ASSERT_EQUAL(r, 0);
49
50         /* Test secure buffer allocation in system memory */
51         bo = gpu_mem_alloc(device_handle, 4096, 4096,
52                            AMDGPU_GEM_DOMAIN_GTT,
53                            AMDGPU_GEM_CREATE_ENCRYPTED,
54                            &bo_mc, &va_handle);
55
56         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
57         CU_ASSERT_EQUAL(r, 0);
58
59         /* Test secure buffer allocation in invisible VRAM */
60         bo = gpu_mem_alloc(device_handle, 4096, 4096,
61                            AMDGPU_GEM_DOMAIN_GTT,
62                            AMDGPU_GEM_CREATE_ENCRYPTED |
63                            AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
64                            &bo_mc, &va_handle);
65
66         r = gpu_mem_free(bo, va_handle, bo_mc, 4096);
67         CU_ASSERT_EQUAL(r, 0);
68 }
69
70 static void amdgpu_security_gfx_submission_test(void)
71 {
72         amdgpu_command_submission_write_linear_helper_with_secure(device_handle,
73                                                                   AMDGPU_HW_IP_GFX,
74                                                                   true);
75 }
76
77 static void amdgpu_security_sdma_submission_test(void)
78 {
79         amdgpu_command_submission_write_linear_helper_with_secure(device_handle,
80                                                                   AMDGPU_HW_IP_DMA,
81                                                                   true);
82 }
83
84 /* ----------------------------------------------------------------- */
85
86 CU_TestInfo security_tests[] = {
87         { "allocate secure buffer test", amdgpu_security_alloc_buf_test },
88         { "graphics secure command submission", amdgpu_security_gfx_submission_test },
89         { "sdma secure command submission", amdgpu_security_sdma_submission_test },
90         CU_TEST_INFO_NULL,
91 };
92
93 CU_BOOL suite_security_tests_enable(void)
94 {
95         CU_BOOL enable = CU_TRUE;
96
97         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
98                                      &minor_version, &device_handle))
99                 return CU_FALSE;
100
101         if (device_handle->info.family_id < AMDGPU_FAMILY_RV) {
102                 printf("\n\nDon't support TMZ (trust memory zone), security suite disabled\n");
103                 enable = CU_FALSE;
104         }
105
106         if ((major_version < 3) ||
107                 ((major_version == 3) && (minor_version < 37))) {
108                 printf("\n\nDon't support TMZ (trust memory zone), kernel DRM version (%d.%d)\n",
109                         major_version, minor_version);
110                 printf("is older, security suite disabled\n");
111                 enable = CU_FALSE;
112         }
113
114         if (amdgpu_device_deinitialize(device_handle))
115                 return CU_FALSE;
116
117         return enable;
118 }
119
120 int suite_security_tests_init(void)
121 {
122         if (amdgpu_device_initialize(drm_amdgpu[0], &major_version,
123                                      &minor_version, &device_handle))
124                 return CUE_SINIT_FAILED;
125
126         return CUE_SUCCESS;
127 }
128
129 int suite_security_tests_clean(void)
130 {
131         int r;
132
133         r = amdgpu_device_deinitialize(device_handle);
134         if (r)
135                 return CUE_SCLEAN_FAILED;
136
137         return CUE_SUCCESS;
138 }