blob: 52d41b0d4207114e0270c6df75a850c244a1d754 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Arvind Ram Prakash13887ac2024-01-04 15:22:52 -06002 * Copyright (c) 2018-2024, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
J-Alvesd708c032020-11-19 12:14:21 +00007#ifndef TEST_HELPERS_H__
8#define TEST_HELPERS_H__
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02009
Joel Hutton8790f022019-03-15 14:47:02 +000010#include <arch_features.h>
J-Alvesf7535f42021-07-30 11:58:41 +010011#include <plat_topology.h>
J-Alves8f08a052020-05-26 17:14:40 +010012#include <psci.h>
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +000013#include <sme.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020014#include <tftf_lib.h>
15#include <trusted_os.h>
16#include <tsp.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020017#include <uuid_utils.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000018#include <uuid.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020019
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
Daniel Boulby0e4629f2021-10-26 14:01:23 +010091#define SKIP_TEST_IF_DIT_NOT_SUPPORTED() \
92 do { \
93 if (!is_armv8_4_dit_present()) { \
94 tftf_testcase_printf( \
95 "DIT not supported\n"); \
96 return TEST_RESULT_SKIPPED; \
97 } \
98 } while (0)
99
Joel Hutton8790f022019-03-15 14:47:02 +0000100#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
101 do { \
102 if (!is_armv8_3_pauth_present()) { \
103 tftf_testcase_printf( \
104 "Pointer Authentication not supported\n"); \
105 return TEST_RESULT_SKIPPED; \
106 } \
107 } while (0)
108
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500109#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
110 do { \
111 if (!is_armv8_6_fgt_present()) { \
112 tftf_testcase_printf( \
113 "Fine Grained Traps not supported\n"); \
114 return TEST_RESULT_SKIPPED; \
115 } \
116 } while (0)
117
Arvind Ram Prakash2f2c9592024-06-06 16:34:28 -0500118#define SKIP_TEST_IF_DEBUGV8P9_NOT_SUPPORTED() \
119 do { \
120 if (arch_get_debug_version() != \
121 ID_AA64DFR0_V8_9_DEBUG_ARCH_SUPPORTED) { \
122 tftf_testcase_printf( \
123 "Debugv8p9 not supported\n"); \
124 return TEST_RESULT_SKIPPED; \
125 } \
126 } while (0)
127
Max Shvetsov959be332021-03-16 14:18:13 +0000128#define SKIP_TEST_IF_SVE_NOT_SUPPORTED() \
129 do { \
130 if (!is_armv8_2_sve_present()) { \
131 tftf_testcase_printf("SVE not supported\n"); \
132 return TEST_RESULT_SKIPPED; \
133 } \
134 } while (0)
135
Jimmy Brisson945095a2020-04-16 10:54:59 -0500136#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
137 do { \
138 if (get_armv8_6_ecv_support() != \
139 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
140 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
141 return TEST_RESULT_SKIPPED; \
142 } \
143 } while (0)
144
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200145#define SKIP_TEST_IF_MM_NOT_PRESENT() \
146 do { \
147 smc_args version_smc = { MM_VERSION_AARCH32 }; \
148 smc_ret_values smc_ret = tftf_smc(&version_smc); \
149 uint32_t version = smc_ret.ret0; \
150 \
151 if (version == (uint32_t) SMC_UNKNOWN) { \
152 tftf_testcase_printf("SPM not detected.\n"); \
153 return TEST_RESULT_SKIPPED; \
154 } \
155 } while (0)
156
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200157#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
158 do { \
159 if (get_armv8_5_mte_support() < (n)) { \
160 tftf_testcase_printf( \
161 "Memory Tagging Extension not supported\n"); \
162 return TEST_RESULT_SKIPPED; \
163 } \
164 } while (0)
165
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200166#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
167 do { \
168 smc_args version_smc = { MM_VERSION_AARCH32 }; \
169 smc_ret_values smc_ret = tftf_smc(&version_smc); \
170 uint32_t version = smc_ret.ret0; \
171 \
172 if (version == (uint32_t) SMC_UNKNOWN) { \
173 tftf_testcase_printf("SPM not detected.\n"); \
174 return TEST_RESULT_SKIPPED; \
175 } \
176 \
177 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100178 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
179 "The required version is %u.%u\n", \
180 version >> MM_VERSION_MAJOR_SHIFT, \
181 version & MM_VERSION_MINOR_MASK, \
182 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200183 return TEST_RESULT_SKIPPED; \
184 } \
185 \
J-Alves8f08a052020-05-26 17:14:40 +0100186 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200187 version >> MM_VERSION_MAJOR_SHIFT, \
188 version & MM_VERSION_MINOR_MASK); \
189 } while (0)
190
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100191#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
192 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100193 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100194 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100195 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100196 tftf_testcase_printf("Debug version returned %d\n" \
197 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100198 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100199 version); \
200 return TEST_RESULT_SKIPPED; \
201 } \
202 } while (0)
203
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100204#define SKIP_TEST_IF_TRBE_NOT_SUPPORTED() \
205 do { \
206 if (!get_armv9_0_trbe_support()) { \
207 tftf_testcase_printf("ARMv9-TRBE not supported\n"); \
208 return TEST_RESULT_SKIPPED; \
209 } \
210 } while (false)
211
Manish V Badarkhe2c518e52021-07-08 16:36:57 +0100212#define SKIP_TEST_IF_TRF_NOT_SUPPORTED() \
213 do { \
214 if (!get_armv8_4_trf_support()) { \
215 tftf_testcase_printf("ARMv8.4-TRF not supported\n"); \
216 return TEST_RESULT_SKIPPED; \
217 } \
218 } while (false)
219
Manish V Badarkhe6d0e1b62021-07-09 13:58:28 +0100220#define SKIP_TEST_IF_SYS_REG_TRACE_NOT_SUPPORTED() \
221 do { \
222 if (!get_armv8_0_sys_reg_trace_support()) { \
223 tftf_testcase_printf("ARMv8-system register" \
224 "trace not supported\n"); \
225 return TEST_RESULT_SKIPPED; \
226 } \
227 } while (false)
228
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000229#define SKIP_TEST_IF_AFP_NOT_SUPPORTED() \
230 do { \
231 if (!get_feat_afp_present()) { \
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000232 tftf_testcase_printf("ARMv8.7-afp not supported\n"); \
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000233 return TEST_RESULT_SKIPPED; \
234 } \
235 } while (false)
236
Arvind Ram Prakash13887ac2024-01-04 15:22:52 -0600237#define SKIP_TEST_IF_MPAM_NOT_SUPPORTED() \
238 do { \
239 if(!is_feat_mpam_supported()){ \
240 tftf_testcase_printf("ARMv8.4-mpam not supported\n"); \
241 return TEST_RESULT_SKIPPED; \
242 } \
243 } while (false)
244
Federico Recanati6328fb02022-01-14 15:48:16 +0100245#ifdef __aarch64__
Federico Recanatid3749b02022-01-14 15:44:45 +0100246#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
247 do { \
248 static const unsigned int pa_range_bits_arr[] = { \
249 PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011,\
250 PARANGE_0100, PARANGE_0101, PARANGE_0110 \
251 }; \
252 if (pa_range_bits_arr[get_pa_range()] < n) { \
253 tftf_testcase_printf("PA size less than %d bit\n", n); \
254 return TEST_RESULT_SKIPPED; \
255 } \
256 } while (false)
Federico Recanati6328fb02022-01-14 15:48:16 +0100257#else
258#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
259 do { \
260 return TEST_RESULT_SKIPPED; \
261 } while (false)
262#endif
Federico Recanatid3749b02022-01-14 15:44:45 +0100263
johpow018c3da8b2022-01-31 18:14:41 -0600264#define SKIP_TEST_IF_BRBE_NOT_SUPPORTED() \
265 do { \
266 if (!get_feat_brbe_support()) { \
267 tftf_testcase_printf("FEAT_BRBE not supported\n"); \
268 return TEST_RESULT_SKIPPED; \
269 } \
270 } while (false)
271
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000272#define SKIP_TEST_IF_WFXT_NOT_SUPPORTED() \
273 do { \
274 if (!get_feat_wfxt_present()) { \
275 tftf_testcase_printf("ARMv8.7-WFxT not supported\n"); \
276 return TEST_RESULT_SKIPPED; \
277 } \
278 } while (false)
279
Juan Pablo Conde9303f4d2022-07-25 16:38:01 -0400280#define SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED() \
281 do { \
282 if (!is_feat_rng_trap_present()) { \
283 tftf_testcase_printf("ARMv8.5-RNG_TRAP not" \
284 "supported\n"); \
285 return TEST_RESULT_SKIPPED; \
286 } \
287 } while (false)
288
Boyan Karatotev35e3ca02022-10-10 16:39:45 +0100289#define SKIP_TEST_IF_PMUV3_NOT_SUPPORTED() \
290 do { \
291 if (!get_feat_pmuv3_supported()) { \
292 tftf_testcase_printf("FEAT_PMUv3 not supported\n"); \
293 return TEST_RESULT_SKIPPED; \
294 } \
295 } while (false)
296
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000297#define SKIP_TEST_IF_SME_NOT_SUPPORTED() \
298 do { \
299 if(!is_feat_sme_supported()) { \
300 tftf_testcase_printf("FEAT_SME not supported\n"); \
301 return TEST_RESULT_SKIPPED; \
302 } \
303 } while (false)
304
Jayanth Dodderi Chidanand95d5d272023-01-16 17:58:47 +0000305#define SKIP_TEST_IF_SME2_NOT_SUPPORTED() \
306 do { \
307 if(!is_feat_sme2_supported()) { \
308 tftf_testcase_printf("FEAT_SME2 not supported\n"); \
309 return TEST_RESULT_SKIPPED; \
310 } \
311 } while (false)
312
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100313#define SKIP_TEST_IF_RME_NOT_SUPPORTED_OR_RMM_IS_TRP() \
314 do { \
Shruti Gupta40de8ec2023-10-12 21:45:12 +0100315 u_register_t retrmm = 0U; \
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100316 \
317 if (!get_armv9_2_feat_rme_support()) { \
318 tftf_testcase_printf("FEAT_RME not supported\n"); \
319 return TEST_RESULT_SKIPPED; \
320 } \
321 \
322 host_rmi_init_cmp_result(); \
Shruti Gupta40de8ec2023-10-12 21:45:12 +0100323 retrmm = host_rmi_version(RMI_ABI_VERSION_VAL); \
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100324 \
325 VERBOSE("RMM version is: %lu.%lu\n", \
326 RMI_ABI_VERSION_GET_MAJOR(retrmm), \
327 RMI_ABI_VERSION_GET_MINOR(retrmm)); \
328 \
329 /* \
330 * TODO: Remove this once SMC_RMM_REALM_CREATE is implemented \
331 * in TRP. For the moment skip the test if RMM is TRP, TRP \
332 * version is always 0. \
333 */ \
334 if (retrmm == 0U) { \
335 tftf_testcase_printf("RMM is TRP\n"); \
336 return TEST_RESULT_SKIPPED; \
337 } \
338 } while (false)
339
Jayanth Dodderi Chidanandcd6c94b2022-02-15 17:19:05 +0000340#define SKIP_TEST_IF_LS64_NOT_SUPPORTED() \
341 do { \
342 if (get_feat_ls64_support() == \
343 ID_AA64ISAR1_LS64_NOT_SUPPORTED) { \
344 tftf_testcase_printf("ARMv8.7-ls64 not supported"); \
345 return TEST_RESULT_SKIPPED; \
346 } \
347 } while (false)
348
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200349/* Helper macro to verify if system suspend API is supported */
350#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100351 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200352 == PSCI_E_SUCCESS)
353
354/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
355#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100356 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200357 == PSCI_E_SUCCESS)
358
359/*
360 * Helper function to verify the system state is ready for system
361 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
362 * Returns 1 if the system is ready to suspend, 0 otherwise.
363 */
364int is_sys_suspend_state_ready(void);
365
366/*
367 * Helper function to reset the system. This function shouldn't return.
368 * It is not marked with __dead to help the test to catch some error in
369 * TF
370 */
371void psci_system_reset(void);
372
373/*
374 * Helper function that enables/disables the mem_protect mechanism
375 */
376int psci_mem_protect(int val);
377
378
379/*
380 * Helper function to call PSCI MEM_PROTECT_CHECK
381 */
382int psci_mem_protect_check(uintptr_t addr, size_t size);
383
384
385/*
386 * Helper function to get a sentinel address that can be used to test mem_protect
387 */
388unsigned char *psci_mem_prot_get_sentinel(void);
389
390/*
391 * Helper function to memory map and unmap a region needed by a test.
392 *
393 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
394 * unmapped. Otherwise, return the test functions's result.
395 */
396test_result_t map_test_unmap(const map_args_unmap_t *args,
397 test_function_arg_t test);
398
J-Alvesf1126f22020-11-02 17:28:20 +0000399/*
nabkah019ea16642022-03-01 19:39:59 +0000400 * Utility function to wait for all CPUs other than the caller to be
401 * OFF.
402 */
403void wait_for_non_lead_cpus(void);
404
405/*
406 * Utility function to wait for a given CPU other than the caller to be
407 * OFF.
408 */
409void wait_for_core_to_turn_off(unsigned int mpidr);
AlexeiFedorov2f30f102023-03-13 19:37:46 +0000410
411/* Generate 64-bit random number */
412unsigned long long rand64(void);
413
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200414#endif /* __TEST_HELPERS_H__ */