blob: 66a5f27e5b1cfa9dc3bbeafa8ac1dc776af462f8 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Manish V Badarkhe82e1a252022-01-04 13:45:31 +00002 * Copyright (c) 2018-2022, 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>
Daniel Boulbyce386b12022-03-29 18:36:36 +010011#include <ffa_helpers.h>
J-Alves7581c382020-05-07 18:34:20 +010012#include <ffa_svc.h>
J-Alvesd56c53c2021-07-01 16:32:16 +010013#include <events.h>
J-Alvesf7535f42021-07-30 11:58:41 +010014#include <plat_topology.h>
J-Alves8f08a052020-05-26 17:14:40 +010015#include <psci.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000016#include <spm_common.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020017#include <tftf_lib.h>
18#include <trusted_os.h>
19#include <tsp.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020020#include <uuid_utils.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000021#include <uuid.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020022
23typedef struct {
24 uintptr_t addr;
25 size_t size;
26 unsigned int attr;
27 void *arg;
28} map_args_unmap_t;
29
30typedef test_result_t (*test_function_arg_t)(void *arg);
31
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060032#ifndef __aarch64__
Joel Hutton8790f022019-03-15 14:47:02 +000033#define SKIP_TEST_IF_AARCH32() \
34 do { \
35 tftf_testcase_printf("Test not supported on aarch32\n"); \
36 return TEST_RESULT_SKIPPED; \
37 } while (0)
38#else
39#define SKIP_TEST_IF_AARCH32()
40#endif
41
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020042#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
43 do { \
44 unsigned int clusters_cnt; \
45 clusters_cnt = tftf_get_total_clusters_count(); \
46 if (clusters_cnt < (n)) { \
47 tftf_testcase_printf( \
48 "Need at least %u clusters, only found %u\n", \
49 (n), clusters_cnt); \
50 return TEST_RESULT_SKIPPED; \
51 } \
52 } while (0)
53
54#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
55 do { \
56 unsigned int cpus_cnt; \
57 cpus_cnt = tftf_get_total_cpus_count(); \
58 if (cpus_cnt < (n)) { \
59 tftf_testcase_printf( \
60 "Need at least %u CPUs, only found %u\n", \
61 (n), cpus_cnt); \
62 return TEST_RESULT_SKIPPED; \
63 } \
64 } while (0)
65
66#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
67 do { \
68 uuid_t tos_uuid; \
69 \
70 if (!is_trusted_os_present(&tos_uuid)) { \
71 tftf_testcase_printf("No Trusted OS detected\n"); \
72 return TEST_RESULT_SKIPPED; \
73 } \
74 } while (0)
75
76#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
77 do { \
78 uuid_t tos_uuid; \
79 char tos_uuid_str[UUID_STR_SIZE]; \
80 \
81 if (!is_trusted_os_present(&tos_uuid)) { \
82 tftf_testcase_printf("No Trusted OS detected\n"); \
83 return TEST_RESULT_SKIPPED; \
84 } \
85 \
86 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
87 tftf_testcase_printf( \
88 "Trusted OS is not the TSP, its UUID is: %s\n", \
89 uuid_to_str(&tos_uuid, tos_uuid_str)); \
90 return TEST_RESULT_SKIPPED; \
91 } \
92 } while (0)
93
Daniel Boulby0e4629f2021-10-26 14:01:23 +010094#define SKIP_TEST_IF_DIT_NOT_SUPPORTED() \
95 do { \
96 if (!is_armv8_4_dit_present()) { \
97 tftf_testcase_printf( \
98 "DIT not supported\n"); \
99 return TEST_RESULT_SKIPPED; \
100 } \
101 } while (0)
102
Joel Hutton8790f022019-03-15 14:47:02 +0000103#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
104 do { \
105 if (!is_armv8_3_pauth_present()) { \
106 tftf_testcase_printf( \
107 "Pointer Authentication not supported\n"); \
108 return TEST_RESULT_SKIPPED; \
109 } \
110 } while (0)
111
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500112#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
113 do { \
114 if (!is_armv8_6_fgt_present()) { \
115 tftf_testcase_printf( \
116 "Fine Grained Traps not supported\n"); \
117 return TEST_RESULT_SKIPPED; \
118 } \
119 } while (0)
120
Max Shvetsov959be332021-03-16 14:18:13 +0000121#define SKIP_TEST_IF_SVE_NOT_SUPPORTED() \
122 do { \
123 if (!is_armv8_2_sve_present()) { \
124 tftf_testcase_printf("SVE not supported\n"); \
125 return TEST_RESULT_SKIPPED; \
126 } \
127 } while (0)
128
Jimmy Brisson945095a2020-04-16 10:54:59 -0500129#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
130 do { \
131 if (get_armv8_6_ecv_support() != \
132 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
133 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
134 return TEST_RESULT_SKIPPED; \
135 } \
136 } while (0)
137
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200138#define SKIP_TEST_IF_MM_NOT_PRESENT() \
139 do { \
140 smc_args version_smc = { MM_VERSION_AARCH32 }; \
141 smc_ret_values smc_ret = tftf_smc(&version_smc); \
142 uint32_t version = smc_ret.ret0; \
143 \
144 if (version == (uint32_t) SMC_UNKNOWN) { \
145 tftf_testcase_printf("SPM not detected.\n"); \
146 return TEST_RESULT_SKIPPED; \
147 } \
148 } while (0)
149
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200150#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
151 do { \
152 if (get_armv8_5_mte_support() < (n)) { \
153 tftf_testcase_printf( \
154 "Memory Tagging Extension not supported\n"); \
155 return TEST_RESULT_SKIPPED; \
156 } \
157 } while (0)
158
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200159#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
160 do { \
161 smc_args version_smc = { MM_VERSION_AARCH32 }; \
162 smc_ret_values smc_ret = tftf_smc(&version_smc); \
163 uint32_t version = smc_ret.ret0; \
164 \
165 if (version == (uint32_t) SMC_UNKNOWN) { \
166 tftf_testcase_printf("SPM not detected.\n"); \
167 return TEST_RESULT_SKIPPED; \
168 } \
169 \
170 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100171 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
172 "The required version is %u.%u\n", \
173 version >> MM_VERSION_MAJOR_SHIFT, \
174 version & MM_VERSION_MINOR_MASK, \
175 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200176 return TEST_RESULT_SKIPPED; \
177 } \
178 \
J-Alves8f08a052020-05-26 17:14:40 +0100179 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200180 version >> MM_VERSION_MAJOR_SHIFT, \
181 version & MM_VERSION_MINOR_MASK); \
182 } while (0)
183
J-Alves7581c382020-05-07 18:34:20 +0100184#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000185 do { \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100186 struct ffa_value ret = ffa_version( \
J-Alves8f08a052020-05-26 17:14:40 +0100187 MAKE_FFA_VERSION(major, minor)); \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100188 uint32_t version = ret.fid; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000189 \
J-Alves8f08a052020-05-26 17:14:40 +0100190 if (version == FFA_ERROR_NOT_SUPPORTED) { \
191 tftf_testcase_printf("FFA_VERSION not supported.\n"); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000192 return TEST_RESULT_SKIPPED; \
193 } \
194 \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100195 if ((version & FFA_VERSION_BIT31_MASK) != 0U) { \
J-Alves8f08a052020-05-26 17:14:40 +0100196 tftf_testcase_printf("FFA_VERSION bad response: %x\n", \
197 version); \
198 return TEST_RESULT_FAIL; \
199 } \
200 \
201 if (version < MAKE_FFA_VERSION(major, minor)) { \
202 tftf_testcase_printf("FFA_VERSION returned %u.%u\n" \
203 "The required version is %u.%u\n", \
204 version >> FFA_VERSION_MAJOR_SHIFT, \
205 version & FFA_VERSION_MINOR_MASK, \
206 major, minor); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000207 return TEST_RESULT_SKIPPED; \
208 } \
209 } while (0)
210
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100211#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
212 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100213 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100214 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100215 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100216 tftf_testcase_printf("Debug version returned %d\n" \
217 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100218 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100219 version); \
220 return TEST_RESULT_SKIPPED; \
221 } \
222 } while (0)
223
J-Alvesd708c032020-11-19 12:14:21 +0000224#define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid) \
J-Alvesf4743062020-10-27 19:39:57 +0000225 do { \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100226 struct ffa_value sc_ret = ffa_partition_info_get(ffa_uuid); \
J-Alvesf4743062020-10-27 19:39:57 +0000227 ffa_rx_release(); \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100228 if (ffa_func_id(sc_ret) == FFA_ERROR && \
229 ffa_error_code(sc_ret) == FFA_ERROR_INVALID_PARAMETER) { \
J-Alvesf4743062020-10-27 19:39:57 +0000230 tftf_testcase_printf("FFA endpoint not deployed!\n"); \
231 return TEST_RESULT_SKIPPED; \
Daniel Boulbyce386b12022-03-29 18:36:36 +0100232 } else if (ffa_func_id(sc_ret) != FFA_SUCCESS_SMC32) { \
J-Alvesf4743062020-10-27 19:39:57 +0000233 ERROR("ffa_partition_info_get failed!\n"); \
234 return TEST_RESULT_FAIL; \
235 } \
236 } while (0)
237
J-Alvesd708c032020-11-19 12:14:21 +0000238#define GET_TFTF_MAILBOX(mb) \
239 do { \
240 if (!get_tftf_mailbox(&mb)) { \
J-Alves08b78602023-01-24 15:54:50 +0000241 ERROR("Mailbox RXTX buffers not configured!\n"); \
nabkah01d5c67b82022-03-22 22:54:23 +0000242 return TEST_RESULT_FAIL; \
243 } \
244 } while (false);
245
J-Alves04469302021-01-21 14:48:13 +0000246#define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids) \
J-Alvesd708c032020-11-19 12:14:21 +0000247 do { \
Max Shvetsov959be332021-03-16 14:18:13 +0000248 SKIP_TEST_IF_AARCH32(); \
J-Alvesd708c032020-11-19 12:14:21 +0000249 const size_t expected_uuids_size = \
250 sizeof(expected_uuids) / sizeof(struct ffa_uuid); \
J-Alves04469302021-01-21 14:48:13 +0000251 test_result_t ret = check_spmc_testing_set_up( \
J-Alvesd708c032020-11-19 12:14:21 +0000252 ffa_major, ffa_minor, expected_uuids, \
253 expected_uuids_size); \
254 if (ret != TEST_RESULT_SUCCESS) { \
255 return ret; \
256 } \
257 } while (false);
258
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100259#define SKIP_TEST_IF_TRBE_NOT_SUPPORTED() \
260 do { \
261 if (!get_armv9_0_trbe_support()) { \
262 tftf_testcase_printf("ARMv9-TRBE not supported\n"); \
263 return TEST_RESULT_SKIPPED; \
264 } \
265 } while (false)
266
Manish V Badarkhe2c518e52021-07-08 16:36:57 +0100267#define SKIP_TEST_IF_TRF_NOT_SUPPORTED() \
268 do { \
269 if (!get_armv8_4_trf_support()) { \
270 tftf_testcase_printf("ARMv8.4-TRF not supported\n"); \
271 return TEST_RESULT_SKIPPED; \
272 } \
273 } while (false)
274
Manish V Badarkhe6d0e1b62021-07-09 13:58:28 +0100275#define SKIP_TEST_IF_SYS_REG_TRACE_NOT_SUPPORTED() \
276 do { \
277 if (!get_armv8_0_sys_reg_trace_support()) { \
278 tftf_testcase_printf("ARMv8-system register" \
279 "trace not supported\n"); \
280 return TEST_RESULT_SKIPPED; \
281 } \
282 } while (false)
283
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000284#define SKIP_TEST_IF_AFP_NOT_SUPPORTED() \
285 do { \
286 if (!get_feat_afp_present()) { \
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000287 tftf_testcase_printf("ARMv8.7-afp not supported\n"); \
Manish V Badarkhe82e1a252022-01-04 13:45:31 +0000288 return TEST_RESULT_SKIPPED; \
289 } \
290 } while (false)
291
Federico Recanati6328fb02022-01-14 15:48:16 +0100292#ifdef __aarch64__
Federico Recanatid3749b02022-01-14 15:44:45 +0100293#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
294 do { \
295 static const unsigned int pa_range_bits_arr[] = { \
296 PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011,\
297 PARANGE_0100, PARANGE_0101, PARANGE_0110 \
298 }; \
299 if (pa_range_bits_arr[get_pa_range()] < n) { \
300 tftf_testcase_printf("PA size less than %d bit\n", n); \
301 return TEST_RESULT_SKIPPED; \
302 } \
303 } while (false)
Federico Recanati6328fb02022-01-14 15:48:16 +0100304#else
305#define SKIP_TEST_IF_PA_SIZE_LESS_THAN(n) \
306 do { \
307 return TEST_RESULT_SKIPPED; \
308 } while (false)
309#endif
Federico Recanatid3749b02022-01-14 15:44:45 +0100310
johpow018c3da8b2022-01-31 18:14:41 -0600311#define SKIP_TEST_IF_BRBE_NOT_SUPPORTED() \
312 do { \
313 if (!get_feat_brbe_support()) { \
314 tftf_testcase_printf("FEAT_BRBE not supported\n"); \
315 return TEST_RESULT_SKIPPED; \
316 } \
317 } while (false)
318
Manish V Badarkheb31bc752021-12-24 08:52:52 +0000319#define SKIP_TEST_IF_WFXT_NOT_SUPPORTED() \
320 do { \
321 if (!get_feat_wfxt_present()) { \
322 tftf_testcase_printf("ARMv8.7-WFxT not supported\n"); \
323 return TEST_RESULT_SKIPPED; \
324 } \
325 } while (false)
326
Juan Pablo Conde9303f4d2022-07-25 16:38:01 -0400327#define SKIP_TEST_IF_RNG_TRAP_NOT_SUPPORTED() \
328 do { \
329 if (!is_feat_rng_trap_present()) { \
330 tftf_testcase_printf("ARMv8.5-RNG_TRAP not" \
331 "supported\n"); \
332 return TEST_RESULT_SKIPPED; \
333 } \
334 } while (false)
335
Boyan Karatotev35e3ca02022-10-10 16:39:45 +0100336#define SKIP_TEST_IF_PMUV3_NOT_SUPPORTED() \
337 do { \
338 if (!get_feat_pmuv3_supported()) { \
339 tftf_testcase_printf("FEAT_PMUv3 not supported\n"); \
340 return TEST_RESULT_SKIPPED; \
341 } \
342 } while (false)
343
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200344/* Helper macro to verify if system suspend API is supported */
345#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100346 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200347 == PSCI_E_SUCCESS)
348
349/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
350#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100351 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200352 == PSCI_E_SUCCESS)
353
354/*
355 * Helper function to verify the system state is ready for system
356 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
357 * Returns 1 if the system is ready to suspend, 0 otherwise.
358 */
359int is_sys_suspend_state_ready(void);
360
361/*
362 * Helper function to reset the system. This function shouldn't return.
363 * It is not marked with __dead to help the test to catch some error in
364 * TF
365 */
366void psci_system_reset(void);
367
368/*
369 * Helper function that enables/disables the mem_protect mechanism
370 */
371int psci_mem_protect(int val);
372
373
374/*
375 * Helper function to call PSCI MEM_PROTECT_CHECK
376 */
377int psci_mem_protect_check(uintptr_t addr, size_t size);
378
379
380/*
381 * Helper function to get a sentinel address that can be used to test mem_protect
382 */
383unsigned char *psci_mem_prot_get_sentinel(void);
384
385/*
386 * Helper function to memory map and unmap a region needed by a test.
387 *
388 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
389 * unmapped. Otherwise, return the test functions's result.
390 */
391test_result_t map_test_unmap(const map_args_unmap_t *args,
392 test_function_arg_t test);
393
J-Alvesf1126f22020-11-02 17:28:20 +0000394/*
J-Alves08b78602023-01-24 15:54:50 +0000395 * Helper function to reset TFTF global mailbox for SPM related tests.
396 * It calls the FFA_RXTX_UNMAP interface, for the SPMC to drop the current
397 * address.
J-Alvesf1126f22020-11-02 17:28:20 +0000398 */
J-Alves08b78602023-01-24 15:54:50 +0000399bool reset_tftf_mailbox(void);
J-Alvesf1126f22020-11-02 17:28:20 +0000400
401/*
402 * Helper function to get TFTF global mailbox for SPM related tests.
J-Alves08b78602023-01-24 15:54:50 +0000403 * Allocates RX/TX buffer pair and calls FFA_RXTX_MAP interface, for the SPMC
404 * to map them into its own S1 translation.
405 * If this function is called, and the buffers had been priorly mapped, it
406 * sets 'mb' with the respective addresses.
J-Alvesf1126f22020-11-02 17:28:20 +0000407 */
408bool get_tftf_mailbox(struct mailbox_buffers *mb);
409
J-Alves04469302021-01-21 14:48:13 +0000410test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major,
J-Alvesd708c032020-11-19 12:14:21 +0000411 uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
412 size_t ffa_uuids_size);
413
J-Alvesd56c53c2021-07-01 16:32:16 +0100414/**
415 * Turn on all cpus to execute a test in all.
416 * - 'cpu_on_handler' should have the code containing the test.
417 * - 'cpu_booted' is used for notifying which cores the test has been executed.
418 * This should be used in the test executed by cpu_on_handler at the end of
419 * processing to make sure it complies with this function's implementation.
420 */
421test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler,
422 event_t *cpu_booted);
423
J-Alves952e1f72021-07-30 17:19:09 +0100424/**
J-Alves79c08f12021-10-27 15:15:16 +0100425 * Call FFA_RUN in the designated SP to make it reach the message loop.
426 * Used within CPU_ON handlers, to bring up the SP in the current core.
427 */
428bool spm_core_sp_init(ffa_id_t sp_id);
429
430/**
nabkah01d5c67b82022-03-22 22:54:23 +0000431 * Initializes the Mailbox for other SPM related tests that need to use
432 * RXTX buffers.
433 */
434bool mailbox_init(struct mailbox_buffers mb);
nabkah019ea16642022-03-01 19:39:59 +0000435/*
436 * Utility function to wait for all CPUs other than the caller to be
437 * OFF.
438 */
439void wait_for_non_lead_cpus(void);
440
441/*
442 * Utility function to wait for a given CPU other than the caller to be
443 * OFF.
444 */
445void wait_for_core_to_turn_off(unsigned int mpidr);
AlexeiFedorov2f30f102023-03-13 19:37:46 +0000446
447/* Generate 64-bit random number */
448unsigned long long rand64(void);
449
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200450#endif /* __TEST_HELPERS_H__ */