blob: 33fce5375707b1d0dc138ad2e80d2756137e6f33 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -06002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __TEST_HELPERS_H__
8#define __TEST_HELPERS_H__
9
Joel Hutton8790f022019-03-15 14:47:02 +000010#include <arch_features.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020011#include <plat_topology.h>
12#include <psci.h>
J-Alves7581c382020-05-07 18:34:20 +010013#include <ffa_svc.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020014#include <tftf_lib.h>
15#include <trusted_os.h>
16#include <tsp.h>
17#include <uuid.h>
18#include <uuid_utils.h>
19
20typedef struct {
21 uintptr_t addr;
22 size_t size;
23 unsigned int attr;
24 void *arg;
25} map_args_unmap_t;
26
27typedef test_result_t (*test_function_arg_t)(void *arg);
28
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060029#ifndef __aarch64__
Joel Hutton8790f022019-03-15 14:47:02 +000030#define SKIP_TEST_IF_AARCH32() \
31 do { \
32 tftf_testcase_printf("Test not supported on aarch32\n"); \
33 return TEST_RESULT_SKIPPED; \
34 } while (0)
35#else
36#define SKIP_TEST_IF_AARCH32()
37#endif
38
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020039#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
40 do { \
41 unsigned int clusters_cnt; \
42 clusters_cnt = tftf_get_total_clusters_count(); \
43 if (clusters_cnt < (n)) { \
44 tftf_testcase_printf( \
45 "Need at least %u clusters, only found %u\n", \
46 (n), clusters_cnt); \
47 return TEST_RESULT_SKIPPED; \
48 } \
49 } while (0)
50
51#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
52 do { \
53 unsigned int cpus_cnt; \
54 cpus_cnt = tftf_get_total_cpus_count(); \
55 if (cpus_cnt < (n)) { \
56 tftf_testcase_printf( \
57 "Need at least %u CPUs, only found %u\n", \
58 (n), cpus_cnt); \
59 return TEST_RESULT_SKIPPED; \
60 } \
61 } while (0)
62
63#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
64 do { \
65 uuid_t tos_uuid; \
66 \
67 if (!is_trusted_os_present(&tos_uuid)) { \
68 tftf_testcase_printf("No Trusted OS detected\n"); \
69 return TEST_RESULT_SKIPPED; \
70 } \
71 } while (0)
72
73#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
74 do { \
75 uuid_t tos_uuid; \
76 char tos_uuid_str[UUID_STR_SIZE]; \
77 \
78 if (!is_trusted_os_present(&tos_uuid)) { \
79 tftf_testcase_printf("No Trusted OS detected\n"); \
80 return TEST_RESULT_SKIPPED; \
81 } \
82 \
83 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
84 tftf_testcase_printf( \
85 "Trusted OS is not the TSP, its UUID is: %s\n", \
86 uuid_to_str(&tos_uuid, tos_uuid_str)); \
87 return TEST_RESULT_SKIPPED; \
88 } \
89 } while (0)
90
Joel Hutton8790f022019-03-15 14:47:02 +000091#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
92 do { \
93 if (!is_armv8_3_pauth_present()) { \
94 tftf_testcase_printf( \
95 "Pointer Authentication not supported\n"); \
96 return TEST_RESULT_SKIPPED; \
97 } \
98 } while (0)
99
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500100#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
101 do { \
102 if (!is_armv8_6_fgt_present()) { \
103 tftf_testcase_printf( \
104 "Fine Grained Traps not supported\n"); \
105 return TEST_RESULT_SKIPPED; \
106 } \
107 } while (0)
108
Jimmy Brisson945095a2020-04-16 10:54:59 -0500109#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
110 do { \
111 if (get_armv8_6_ecv_support() != \
112 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
113 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
114 return TEST_RESULT_SKIPPED; \
115 } \
116 } while (0)
117
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200118#define SKIP_TEST_IF_MM_NOT_PRESENT() \
119 do { \
120 smc_args version_smc = { MM_VERSION_AARCH32 }; \
121 smc_ret_values smc_ret = tftf_smc(&version_smc); \
122 uint32_t version = smc_ret.ret0; \
123 \
124 if (version == (uint32_t) SMC_UNKNOWN) { \
125 tftf_testcase_printf("SPM not detected.\n"); \
126 return TEST_RESULT_SKIPPED; \
127 } \
128 } while (0)
129
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200130#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
131 do { \
132 if (get_armv8_5_mte_support() < (n)) { \
133 tftf_testcase_printf( \
134 "Memory Tagging Extension not supported\n"); \
135 return TEST_RESULT_SKIPPED; \
136 } \
137 } while (0)
138
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200139#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
140 do { \
141 smc_args version_smc = { MM_VERSION_AARCH32 }; \
142 smc_ret_values smc_ret = tftf_smc(&version_smc); \
143 uint32_t version = smc_ret.ret0; \
144 \
145 if (version == (uint32_t) SMC_UNKNOWN) { \
146 tftf_testcase_printf("SPM not detected.\n"); \
147 return TEST_RESULT_SKIPPED; \
148 } \
149 \
150 if (version < MM_VERSION_FORM(major, minor)) { \
151 tftf_testcase_printf("MM_VERSION returned %d.%d\n" \
152 "The required version is %d.%d\n", \
153 version >> MM_VERSION_MAJOR_SHIFT, \
154 version & MM_VERSION_MINOR_MASK, \
155 major, minor); \
156 return TEST_RESULT_SKIPPED; \
157 } \
158 \
159 VERBOSE("MM_VERSION returned %d.%d\n", \
160 version >> MM_VERSION_MAJOR_SHIFT, \
161 version & MM_VERSION_MINOR_MASK); \
162 } while (0)
163
J-Alves7581c382020-05-07 18:34:20 +0100164#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000165 do { \
J-Alves7581c382020-05-07 18:34:20 +0100166 smc_args version_smc = { FFA_VERSION }; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000167 smc_ret_values smc_ret = tftf_smc(&version_smc); \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100168 uint32_t version = smc_ret.ret2; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000169 \
J-Alves7581c382020-05-07 18:34:20 +0100170 if (smc_ret.ret0 != FFA_SUCCESS_SMC32) { \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000171 tftf_testcase_printf("SPM not detected.\n"); \
172 return TEST_RESULT_SKIPPED; \
173 } \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100174 \
J-Alves7581c382020-05-07 18:34:20 +0100175 if ((version & FFA_VERSION_BIT31_MASK) != 0) { \
176 tftf_testcase_printf("FFA_VERSION bad response.\n"); \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100177 return TEST_RESULT_SKIPPED; \
178 } \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000179 \
J-Alves7581c382020-05-07 18:34:20 +0100180 if (version < MAKE_FFA_VERSION(major, minor)) { \
181 tftf_testcase_printf("FFA_VERSION returned %d.%d\n" \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000182 "The required version is %d.%d\n", \
J-Alves7581c382020-05-07 18:34:20 +0100183 version >> FFA_VERSION_MAJOR_SHIFT,\
184 version & FFA_VERSION_MINOR_MASK, \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000185 major, minor); \
186 return TEST_RESULT_SKIPPED; \
187 } \
188 } while (0)
189
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100190#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
191 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100192 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100193 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100194 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100195 tftf_testcase_printf("Debug version returned %d\n" \
196 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100197 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100198 version); \
199 return TEST_RESULT_SKIPPED; \
200 } \
201 } while (0)
202
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200203/* Helper macro to verify if system suspend API is supported */
204#define is_psci_sys_susp_supported() \
205 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
206 == PSCI_E_SUCCESS)
207
208/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
209#define is_psci_stat_count_supported() \
210 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
211 == PSCI_E_SUCCESS)
212
213/*
214 * Helper function to verify the system state is ready for system
215 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
216 * Returns 1 if the system is ready to suspend, 0 otherwise.
217 */
218int is_sys_suspend_state_ready(void);
219
220/*
221 * Helper function to reset the system. This function shouldn't return.
222 * It is not marked with __dead to help the test to catch some error in
223 * TF
224 */
225void psci_system_reset(void);
226
227/*
228 * Helper function that enables/disables the mem_protect mechanism
229 */
230int psci_mem_protect(int val);
231
232
233/*
234 * Helper function to call PSCI MEM_PROTECT_CHECK
235 */
236int psci_mem_protect_check(uintptr_t addr, size_t size);
237
238
239/*
240 * Helper function to get a sentinel address that can be used to test mem_protect
241 */
242unsigned char *psci_mem_prot_get_sentinel(void);
243
244/*
245 * Helper function to memory map and unmap a region needed by a test.
246 *
247 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
248 * unmapped. Otherwise, return the test functions's result.
249 */
250test_result_t map_test_unmap(const map_args_unmap_t *args,
251 test_function_arg_t test);
252
253#endif /* __TEST_HELPERS_H__ */