blob: fc557ae11aea2c1c2185a29cc17be124c12d200c [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
Boyan Karatotev566f07d2024-10-25 13:31:48 +010091#define SKIP_TEST_IF_SMCCC_VERSION_LT(major, minor) \
92 do { \
93 smc_args args = {0}; \
94 smc_ret_values ret; \
95 args.fid = SMCCC_VERSION; \
96 ret = tftf_smc(&args); \
97 if ((int32_t)ret.ret0 < MAKE_SMCCC_VERSION(major, minor)) { \
98 tftf_testcase_printf( \
99 "Unexpected SMCCC version: 0x%x\n", \
100 (int)ret.ret0); \
101 return TEST_RESULT_SKIPPED; \
102 } \
103 } while (0)
104
Daniel Boulby0e4629f2021-10-26 14:01:23 +0100105#define SKIP_TEST_IF_DIT_NOT_SUPPORTED() \
106 do { \
107 if (!is_armv8_4_dit_present()) { \
108 tftf_testcase_printf( \
109 "DIT not supported\n"); \
110 return TEST_RESULT_SKIPPED; \
111 } \
112 } while (0)
113
Joel Hutton8790f022019-03-15 14:47:02 +0000114#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
115 do { \
116 if (!is_armv8_3_pauth_present()) { \
117 tftf_testcase_printf( \
118 "Pointer Authentication not supported\n"); \
119 return TEST_RESULT_SKIPPED; \
120 } \
121 } while (0)
122
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500123#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
124 do { \
125 if (!is_armv8_6_fgt_present()) { \
126 tftf_testcase_printf( \
127 "Fine Grained Traps not supported\n"); \
128 return TEST_RESULT_SKIPPED; \
129 } \
130 } while (0)
131
Arvind Ram Prakash2f2c9592024-06-06 16:34:28 -0500132#define SKIP_TEST_IF_DEBUGV8P9_NOT_SUPPORTED() \
133 do { \
134 if (arch_get_debug_version() != \
135 ID_AA64DFR0_V8_9_DEBUG_ARCH_SUPPORTED) { \
136 tftf_testcase_printf( \
137 "Debugv8p9 not supported\n"); \
138 return TEST_RESULT_SKIPPED; \
139 } \
140 } while (0)
141
Arvind Ram Prakash94963d42024-06-13 17:19:56 -0500142#define SKIP_TEST_IF_FGT2_NOT_SUPPORTED() \
143 do { \
144 if (!is_armv8_9_fgt2_present()) { \
145 tftf_testcase_printf( \
146 "Fine Grained Traps 2 not supported\n"); \
147 return TEST_RESULT_SKIPPED; \
148 } \
149 } while (0)
150
Max Shvetsov959be332021-03-16 14:18:13 +0000151#define SKIP_TEST_IF_SVE_NOT_SUPPORTED() \
152 do { \
153 if (!is_armv8_2_sve_present()) { \
154 tftf_testcase_printf("SVE not supported\n"); \
155 return TEST_RESULT_SKIPPED; \
156 } \
157 } while (0)
158
Jimmy Brisson945095a2020-04-16 10:54:59 -0500159#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
160 do { \
161 if (get_armv8_6_ecv_support() != \
162 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
163 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
164 return TEST_RESULT_SKIPPED; \
165 } \
166 } while (0)
167
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200168#define SKIP_TEST_IF_MM_NOT_PRESENT() \
169 do { \
170 smc_args version_smc = { MM_VERSION_AARCH32 }; \
171 smc_ret_values smc_ret = tftf_smc(&version_smc); \
172 uint32_t version = smc_ret.ret0; \
173 \
174 if (version == (uint32_t) SMC_UNKNOWN) { \
175 tftf_testcase_printf("SPM not detected.\n"); \
176 return TEST_RESULT_SKIPPED; \
177 } \
178 } while (0)
179
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200180#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
181 do { \
182 if (get_armv8_5_mte_support() < (n)) { \
183 tftf_testcase_printf( \
184 "Memory Tagging Extension not supported\n"); \
185 return TEST_RESULT_SKIPPED; \
186 } \
187 } while (0)
188
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200189#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
190 do { \
191 smc_args version_smc = { MM_VERSION_AARCH32 }; \
192 smc_ret_values smc_ret = tftf_smc(&version_smc); \
193 uint32_t version = smc_ret.ret0; \
194 \
195 if (version == (uint32_t) SMC_UNKNOWN) { \
196 tftf_testcase_printf("SPM not detected.\n"); \
197 return TEST_RESULT_SKIPPED; \
198 } \
199 \
200 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100201 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
202 "The required version is %u.%u\n", \
203 version >> MM_VERSION_MAJOR_SHIFT, \
204 version & MM_VERSION_MINOR_MASK, \
205 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200206 return TEST_RESULT_SKIPPED; \
207 } \
208 \
J-Alves8f08a052020-05-26 17:14:40 +0100209 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200210 version >> MM_VERSION_MAJOR_SHIFT, \
211 version & MM_VERSION_MINOR_MASK); \
212 } while (0)
213
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100214#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
215 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100216 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100217 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100218 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100219 tftf_testcase_printf("Debug version returned %d\n" \
220 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100221 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100222 version); \
223 return TEST_RESULT_SKIPPED; \
224 } \
225 } while (0)
226
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100227#define SKIP_TEST_IF_TRBE_NOT_SUPPORTED() \
228 do { \
Boyan Karatotev4e282422024-10-25 14:34:13 +0100229 if (!is_feat_trbe_present()) { \
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100230 tftf_testcase_printf("ARMv9-TRBE not supported\n"); \
231 return TEST_RESULT_SKIPPED; \
232 } \
233 } while (false)
234
Manish V Badarkhe2c518e52021-07-08 16:36:57 +0100235#define SKIP_TEST_IF_TRF_NOT_SUPPORTED() \
236 do { \
237 if (!get_armv8_4_trf_support()) { \
238 tftf_testcase_printf("ARMv8.4-TRF not supported\n"); \
239 return TEST_RESULT_SKIPPED; \
240 } \
241 } while (false)
242
Manish V Badarkhe6d0e1b62021-07-09 13:58:28 +0100243#define SKIP_TEST_IF_SYS_REG_TRACE_NOT_SUPPORTED() \
244 do { \
245 if (!get_armv8_0_sys_reg_trace_support()) { \
246 tftf_testcase_printf("ARMv8-system register" \
247 "trace not supported\n"); \
248 return TEST_RESULT_SKIPPED; \
249 } \
250 } while (false)
251
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000252#define SKIP_TEST_IF_AFP_NOT_SUPPORTED() \
253 do { \
254 if (!get_feat_afp_present()) { \
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000255 tftf_testcase_printf("ARMv8.7-afp not supported\n"); \
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000256 return TEST_RESULT_SKIPPED; \
257 } \
258 } while (false)
259
Arvind Ram Prakash13887ac2024-01-04 15:22:52 -0600260#define SKIP_TEST_IF_MPAM_NOT_SUPPORTED() \
261 do { \
262 if(!is_feat_mpam_supported()){ \
263 tftf_testcase_printf("ARMv8.4-mpam not supported\n"); \
264 return TEST_RESULT_SKIPPED; \
265 } \
266 } while (false)
267
Federico Recanati6328fb02022-01-14 15:48:16 +0100268#ifdef __aarch64__
Federico Recanatid3749b02022-01-14 15:44:45 +0100269#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
270 do { \
271 static const unsigned int pa_range_bits_arr[] = { \
272 PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011,\
273 PARANGE_0100, PARANGE_0101, PARANGE_0110 \
274 }; \
275 if (pa_range_bits_arr[get_pa_range()] < n) { \
276 tftf_testcase_printf("PA size less than %d bit\n", n); \
277 return TEST_RESULT_SKIPPED; \
278 } \
279 } while (false)
Federico Recanati6328fb02022-01-14 15:48:16 +0100280#else
281#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
282 do { \
283 return TEST_RESULT_SKIPPED; \
284 } while (false)
285#endif
Federico Recanatid3749b02022-01-14 15:44:45 +0100286
johpow018c3da8b2022-01-31 18:14:41 -0600287#define SKIP_TEST_IF_BRBE_NOT_SUPPORTED() \
288 do { \
289 if (!get_feat_brbe_support()) { \
290 tftf_testcase_printf("FEAT_BRBE not supported\n"); \
291 return TEST_RESULT_SKIPPED; \
292 } \
293 } while (false)
294
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000295#define SKIP_TEST_IF_WFXT_NOT_SUPPORTED() \
296 do { \
297 if (!get_feat_wfxt_present()) { \
298 tftf_testcase_printf("ARMv8.7-WFxT not supported\n"); \
299 return TEST_RESULT_SKIPPED; \
300 } \
301 } while (false)
302
Juan Pablo Conde9303f4d2022-07-25 16:38:01 -0400303#define SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED() \
304 do { \
305 if (!is_feat_rng_trap_present()) { \
306 tftf_testcase_printf("ARMv8.5-RNG_TRAP not" \
307 "supported\n"); \
308 return TEST_RESULT_SKIPPED; \
309 } \
310 } while (false)
311
Boyan Karatotev35e3ca02022-10-10 16:39:45 +0100312#define SKIP_TEST_IF_PMUV3_NOT_SUPPORTED() \
313 do { \
314 if (!get_feat_pmuv3_supported()) { \
315 tftf_testcase_printf("FEAT_PMUv3 not supported\n"); \
316 return TEST_RESULT_SKIPPED; \
317 } \
318 } while (false)
319
Jayanth Dodderi Chidanandb3ffd3c2023-02-13 12:15:11 +0000320#define SKIP_TEST_IF_SME_NOT_SUPPORTED() \
321 do { \
322 if(!is_feat_sme_supported()) { \
323 tftf_testcase_printf("FEAT_SME not supported\n"); \
324 return TEST_RESULT_SKIPPED; \
325 } \
326 } while (false)
327
Jayanth Dodderi Chidanand95d5d272023-01-16 17:58:47 +0000328#define SKIP_TEST_IF_SME2_NOT_SUPPORTED() \
329 do { \
330 if(!is_feat_sme2_supported()) { \
331 tftf_testcase_printf("FEAT_SME2 not supported\n"); \
332 return TEST_RESULT_SKIPPED; \
333 } \
334 } while (false)
335
Arvind Ram Prakash1ab21e52024-11-12 10:52:08 -0600336#define SKIP_TEST_IF_FPMR_NOT_SUPPORTED() \
337 do { \
338 if(!is_feat_fpmr_present()) { \
339 tftf_testcase_printf("FEAT_FPMR not supported\n"); \
340 return TEST_RESULT_SKIPPED; \
341 } \
342 } while (false)
343
Igor Podgainõid1a7f4d2024-11-26 12:50:47 +0100344#define SKIP_TEST_IF_SCTLR2_NOT_SUPPORTED() \
345 do { \
346 if (!is_feat_sctlr2_supported()) { \
347 tftf_testcase_printf("FEAT_SCTLR2 not supported\n"); \
348 return TEST_RESULT_SKIPPED; \
349 } \
350 } while (false)
351
352#define SKIP_TEST_IF_THE_NOT_SUPPORTED() \
353 do { \
354 if (!is_feat_the_supported()) { \
355 tftf_testcase_printf("FEAT_THE not supported\n"); \
356 return TEST_RESULT_SKIPPED; \
357 } \
358 } while (false)
359
360#define SKIP_TEST_IF_D128_NOT_SUPPORTED() \
361 do { \
362 if (!is_feat_d128_supported()) { \
363 tftf_testcase_printf("FEAT_D128 not supported\n"); \
364 return TEST_RESULT_SKIPPED; \
365 } \
366 } while (false)
367
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100368#define SKIP_TEST_IF_RME_NOT_SUPPORTED_OR_RMM_IS_TRP() \
369 do { \
Shruti Gupta40de8ec2023-10-12 21:45:12 +0100370 u_register_t retrmm = 0U; \
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100371 \
372 if (!get_armv9_2_feat_rme_support()) { \
373 tftf_testcase_printf("FEAT_RME not supported\n"); \
374 return TEST_RESULT_SKIPPED; \
375 } \
376 \
377 host_rmi_init_cmp_result(); \
Shruti Gupta40de8ec2023-10-12 21:45:12 +0100378 retrmm = host_rmi_version(RMI_ABI_VERSION_VAL); \
Arunachalam Ganapathy4b221112023-04-05 14:19:03 +0100379 \
380 VERBOSE("RMM version is: %lu.%lu\n", \
381 RMI_ABI_VERSION_GET_MAJOR(retrmm), \
382 RMI_ABI_VERSION_GET_MINOR(retrmm)); \
383 \
384 /* \
385 * TODO: Remove this once SMC_RMM_REALM_CREATE is implemented \
386 * in TRP. For the moment skip the test if RMM is TRP, TRP \
387 * version is always 0. \
388 */ \
389 if (retrmm == 0U) { \
390 tftf_testcase_printf("RMM is TRP\n"); \
391 return TEST_RESULT_SKIPPED; \
392 } \
393 } while (false)
394
Jayanth Dodderi Chidanandcd6c94b2022-02-15 17:19:05 +0000395#define SKIP_TEST_IF_LS64_NOT_SUPPORTED() \
396 do { \
397 if (get_feat_ls64_support() == \
398 ID_AA64ISAR1_LS64_NOT_SUPPORTED) { \
399 tftf_testcase_printf("ARMv8.7-ls64 not supported"); \
400 return TEST_RESULT_SKIPPED; \
401 } \
402 } while (false)
403
Andre Przywara72b7ce12024-11-04 13:44:39 +0000404#define SKIP_TEST_IF_LS64_ACCDATA_NOT_SUPPORTED() \
405 do { \
406 if (get_feat_ls64_support() < \
407 ID_AA64ISAR1_LS64_ACCDATA_SUPPORTED) { \
408 tftf_testcase_printf("ARMv8.7-ls64-accdata not supported"); \
409 return TEST_RESULT_SKIPPED; \
410 } \
411 } while (false)
412
Javier Almansa Sobrino7c78f7b2024-10-25 11:44:32 +0100413#define SKIP_TEST_IF_DOUBLE_FAULT2_NOT_SUPPORTED() \
414 do { \
415 if (is_feat_double_fault2_present() == false) { \
416 return TEST_RESULT_SKIPPED; \
417 } \
418 } while (false)
419
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200420/* Helper macro to verify if system suspend API is supported */
421#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100422 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200423 == PSCI_E_SUCCESS)
424
425/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
426#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100427 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200428 == PSCI_E_SUCCESS)
429
430/*
431 * Helper function to verify the system state is ready for system
432 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
433 * Returns 1 if the system is ready to suspend, 0 otherwise.
434 */
435int is_sys_suspend_state_ready(void);
436
437/*
438 * Helper function to reset the system. This function shouldn't return.
439 * It is not marked with __dead to help the test to catch some error in
440 * TF
441 */
442void psci_system_reset(void);
443
444/*
445 * Helper function that enables/disables the mem_protect mechanism
446 */
447int psci_mem_protect(int val);
448
449
450/*
451 * Helper function to call PSCI MEM_PROTECT_CHECK
452 */
453int psci_mem_protect_check(uintptr_t addr, size_t size);
454
455
456/*
457 * Helper function to get a sentinel address that can be used to test mem_protect
458 */
459unsigned char *psci_mem_prot_get_sentinel(void);
460
461/*
462 * Helper function to memory map and unmap a region needed by a test.
463 *
464 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
465 * unmapped. Otherwise, return the test functions's result.
466 */
467test_result_t map_test_unmap(const map_args_unmap_t *args,
468 test_function_arg_t test);
469
J-Alvesf1126f22020-11-02 17:28:20 +0000470/*
nabkah019ea16642022-03-01 19:39:59 +0000471 * Utility function to wait for all CPUs other than the caller to be
472 * OFF.
473 */
474void wait_for_non_lead_cpus(void);
475
476/*
477 * Utility function to wait for a given CPU other than the caller to be
478 * OFF.
479 */
480void wait_for_core_to_turn_off(unsigned int mpidr);
AlexeiFedorov2f30f102023-03-13 19:37:46 +0000481
482/* Generate 64-bit random number */
483unsigned long long rand64(void);
484
Arvind Ram Prakash81916212024-08-15 15:08:23 -0500485/* TRBE Errata */
486#define CORTEX_A520_MIDR U(0x410FD800)
487#define CORTEX_X4_MIDR U(0x410FD821)
488#define RXPX_RANGE(x, y, z) (((x >= y) && (x <= z)) ? true : false)
489bool is_trbe_errata_affected_core(void);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200490#endif /* __TEST_HELPERS_H__ */