blob: 332f1d8cd335635abb49b0c58e3c22f63d83f3a9 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Max Shvetsov103e0562021-02-04 16:58:31 +00002 * Copyright (c) 2018-2021, 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-Alves7581c382020-05-07 18:34:20 +010011#include <ffa_svc.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000012#include <plat_topology.h>
J-Alves8f08a052020-05-26 17:14:40 +010013#include <psci.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000014#include <spm_common.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020015#include <tftf_lib.h>
16#include <trusted_os.h>
17#include <tsp.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020018#include <uuid_utils.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000019#include <uuid.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020020
21typedef struct {
22 uintptr_t addr;
23 size_t size;
24 unsigned int attr;
25 void *arg;
26} map_args_unmap_t;
27
28typedef test_result_t (*test_function_arg_t)(void *arg);
29
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060030#ifndef __aarch64__
Joel Hutton8790f022019-03-15 14:47:02 +000031#define SKIP_TEST_IF_AARCH32() \
32 do { \
33 tftf_testcase_printf("Test not supported on aarch32\n"); \
34 return TEST_RESULT_SKIPPED; \
35 } while (0)
36#else
37#define SKIP_TEST_IF_AARCH32()
38#endif
39
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020040#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
41 do { \
42 unsigned int clusters_cnt; \
43 clusters_cnt = tftf_get_total_clusters_count(); \
44 if (clusters_cnt < (n)) { \
45 tftf_testcase_printf( \
46 "Need at least %u clusters, only found %u\n", \
47 (n), clusters_cnt); \
48 return TEST_RESULT_SKIPPED; \
49 } \
50 } while (0)
51
52#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
53 do { \
54 unsigned int cpus_cnt; \
55 cpus_cnt = tftf_get_total_cpus_count(); \
56 if (cpus_cnt < (n)) { \
57 tftf_testcase_printf( \
58 "Need at least %u CPUs, only found %u\n", \
59 (n), cpus_cnt); \
60 return TEST_RESULT_SKIPPED; \
61 } \
62 } while (0)
63
64#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
65 do { \
66 uuid_t tos_uuid; \
67 \
68 if (!is_trusted_os_present(&tos_uuid)) { \
69 tftf_testcase_printf("No Trusted OS detected\n"); \
70 return TEST_RESULT_SKIPPED; \
71 } \
72 } while (0)
73
74#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
75 do { \
76 uuid_t tos_uuid; \
77 char tos_uuid_str[UUID_STR_SIZE]; \
78 \
79 if (!is_trusted_os_present(&tos_uuid)) { \
80 tftf_testcase_printf("No Trusted OS detected\n"); \
81 return TEST_RESULT_SKIPPED; \
82 } \
83 \
84 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
85 tftf_testcase_printf( \
86 "Trusted OS is not the TSP, its UUID is: %s\n", \
87 uuid_to_str(&tos_uuid, tos_uuid_str)); \
88 return TEST_RESULT_SKIPPED; \
89 } \
90 } while (0)
91
Joel Hutton8790f022019-03-15 14:47:02 +000092#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
93 do { \
94 if (!is_armv8_3_pauth_present()) { \
95 tftf_testcase_printf( \
96 "Pointer Authentication not supported\n"); \
97 return TEST_RESULT_SKIPPED; \
98 } \
99 } while (0)
100
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500101#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
102 do { \
103 if (!is_armv8_6_fgt_present()) { \
104 tftf_testcase_printf( \
105 "Fine Grained Traps not supported\n"); \
106 return TEST_RESULT_SKIPPED; \
107 } \
108 } while (0)
109
Max Shvetsov959be332021-03-16 14:18:13 +0000110#define SKIP_TEST_IF_SVE_NOT_SUPPORTED() \
111 do { \
112 if (!is_armv8_2_sve_present()) { \
113 tftf_testcase_printf("SVE not supported\n"); \
114 return TEST_RESULT_SKIPPED; \
115 } \
116 } while (0)
117
Jimmy Brisson945095a2020-04-16 10:54:59 -0500118#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
119 do { \
120 if (get_armv8_6_ecv_support() != \
121 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
122 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
123 return TEST_RESULT_SKIPPED; \
124 } \
125 } while (0)
126
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200127#define SKIP_TEST_IF_MM_NOT_PRESENT() \
128 do { \
129 smc_args version_smc = { MM_VERSION_AARCH32 }; \
130 smc_ret_values smc_ret = tftf_smc(&version_smc); \
131 uint32_t version = smc_ret.ret0; \
132 \
133 if (version == (uint32_t) SMC_UNKNOWN) { \
134 tftf_testcase_printf("SPM not detected.\n"); \
135 return TEST_RESULT_SKIPPED; \
136 } \
137 } while (0)
138
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200139#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
140 do { \
141 if (get_armv8_5_mte_support() < (n)) { \
142 tftf_testcase_printf( \
143 "Memory Tagging Extension not supported\n"); \
144 return TEST_RESULT_SKIPPED; \
145 } \
146 } while (0)
147
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200148#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
149 do { \
150 smc_args version_smc = { MM_VERSION_AARCH32 }; \
151 smc_ret_values smc_ret = tftf_smc(&version_smc); \
152 uint32_t version = smc_ret.ret0; \
153 \
154 if (version == (uint32_t) SMC_UNKNOWN) { \
155 tftf_testcase_printf("SPM not detected.\n"); \
156 return TEST_RESULT_SKIPPED; \
157 } \
158 \
159 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100160 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
161 "The required version is %u.%u\n", \
162 version >> MM_VERSION_MAJOR_SHIFT, \
163 version & MM_VERSION_MINOR_MASK, \
164 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200165 return TEST_RESULT_SKIPPED; \
166 } \
167 \
J-Alves8f08a052020-05-26 17:14:40 +0100168 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200169 version >> MM_VERSION_MAJOR_SHIFT, \
170 version & MM_VERSION_MINOR_MASK); \
171 } while (0)
172
J-Alves7581c382020-05-07 18:34:20 +0100173#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000174 do { \
J-Alves8f08a052020-05-26 17:14:40 +0100175 smc_ret_values smc_ret = ffa_version( \
176 MAKE_FFA_VERSION(major, minor)); \
177 uint32_t version = smc_ret.ret0; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000178 \
J-Alves8f08a052020-05-26 17:14:40 +0100179 if (version == FFA_ERROR_NOT_SUPPORTED) { \
180 tftf_testcase_printf("FFA_VERSION not supported.\n"); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000181 return TEST_RESULT_SKIPPED; \
182 } \
183 \
J-Alves8f08a052020-05-26 17:14:40 +0100184 if ((version & FFA_VERSION_BIT31_MASK) != 0U) { \
185 tftf_testcase_printf("FFA_VERSION bad response: %x\n", \
186 version); \
187 return TEST_RESULT_FAIL; \
188 } \
189 \
190 if (version < MAKE_FFA_VERSION(major, minor)) { \
191 tftf_testcase_printf("FFA_VERSION returned %u.%u\n" \
192 "The required version is %u.%u\n", \
193 version >> FFA_VERSION_MAJOR_SHIFT, \
194 version & FFA_VERSION_MINOR_MASK, \
195 major, minor); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000196 return TEST_RESULT_SKIPPED; \
197 } \
198 } while (0)
199
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100200#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
201 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100202 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100203 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100204 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100205 tftf_testcase_printf("Debug version returned %d\n" \
206 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100207 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100208 version); \
209 return TEST_RESULT_SKIPPED; \
210 } \
211 } while (0)
212
J-Alvesd708c032020-11-19 12:14:21 +0000213#define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid) \
J-Alvesf4743062020-10-27 19:39:57 +0000214 do { \
J-Alvesf4743062020-10-27 19:39:57 +0000215 smc_ret_values smc_ret = ffa_partition_info_get(ffa_uuid); \
216 ffa_rx_release(); \
Olivier Deprez58757e82021-07-30 10:18:00 +0200217 if (ffa_func_id(smc_ret) == FFA_ERROR && \
218 ffa_error_code(smc_ret) == FFA_ERROR_INVALID_PARAMETER) { \
J-Alvesf4743062020-10-27 19:39:57 +0000219 tftf_testcase_printf("FFA endpoint not deployed!\n"); \
220 return TEST_RESULT_SKIPPED; \
221 } else if (smc_ret.ret0 != FFA_SUCCESS_SMC32) { \
222 ERROR("ffa_partition_info_get failed!\n"); \
223 return TEST_RESULT_FAIL; \
224 } \
225 } while (0)
226
J-Alvesd708c032020-11-19 12:14:21 +0000227#define GET_TFTF_MAILBOX(mb) \
228 do { \
229 if (!get_tftf_mailbox(&mb)) { \
230 ERROR("Mailbox not configured!\nThis test relies on" \
231 " test suite \"FF-A RXTX Mapping\" to map/configure" \
232 " RXTX buffers\n"); \
233 return TEST_RESULT_FAIL; \
234 } \
235 } while (false);
236
J-Alves04469302021-01-21 14:48:13 +0000237#define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids) \
J-Alvesd708c032020-11-19 12:14:21 +0000238 do { \
Max Shvetsov959be332021-03-16 14:18:13 +0000239 SKIP_TEST_IF_AARCH32(); \
J-Alvesd708c032020-11-19 12:14:21 +0000240 const size_t expected_uuids_size = \
241 sizeof(expected_uuids) / sizeof(struct ffa_uuid); \
J-Alves04469302021-01-21 14:48:13 +0000242 test_result_t ret = check_spmc_testing_set_up( \
J-Alvesd708c032020-11-19 12:14:21 +0000243 ffa_major, ffa_minor, expected_uuids, \
244 expected_uuids_size); \
245 if (ret != TEST_RESULT_SUCCESS) { \
246 return ret; \
247 } \
248 } while (false);
249
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100250#define SKIP_TEST_IF_TRBE_NOT_SUPPORTED() \
251 do { \
252 if (!get_armv9_0_trbe_support()) { \
253 tftf_testcase_printf("ARMv9-TRBE not supported\n"); \
254 return TEST_RESULT_SKIPPED; \
255 } \
256 } while (false)
257
Manish V Badarkhe2c518e52021-07-08 16:36:57 +0100258#define SKIP_TEST_IF_TRF_NOT_SUPPORTED() \
259 do { \
260 if (!get_armv8_4_trf_support()) { \
261 tftf_testcase_printf("ARMv8.4-TRF not supported\n"); \
262 return TEST_RESULT_SKIPPED; \
263 } \
264 } while (false)
265
Manish V Badarkhe6d0e1b62021-07-09 13:58:28 +0100266#define SKIP_TEST_IF_SYS_REG_TRACE_NOT_SUPPORTED() \
267 do { \
268 if (!get_armv8_0_sys_reg_trace_support()) { \
269 tftf_testcase_printf("ARMv8-system register" \
270 "trace not supported\n"); \
271 return TEST_RESULT_SKIPPED; \
272 } \
273 } while (false)
274
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200275/* Helper macro to verify if system suspend API is supported */
276#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100277 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200278 == PSCI_E_SUCCESS)
279
280/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
281#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100282 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200283 == PSCI_E_SUCCESS)
284
285/*
286 * Helper function to verify the system state is ready for system
287 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
288 * Returns 1 if the system is ready to suspend, 0 otherwise.
289 */
290int is_sys_suspend_state_ready(void);
291
292/*
293 * Helper function to reset the system. This function shouldn't return.
294 * It is not marked with __dead to help the test to catch some error in
295 * TF
296 */
297void psci_system_reset(void);
298
299/*
300 * Helper function that enables/disables the mem_protect mechanism
301 */
302int psci_mem_protect(int val);
303
304
305/*
306 * Helper function to call PSCI MEM_PROTECT_CHECK
307 */
308int psci_mem_protect_check(uintptr_t addr, size_t size);
309
310
311/*
312 * Helper function to get a sentinel address that can be used to test mem_protect
313 */
314unsigned char *psci_mem_prot_get_sentinel(void);
315
316/*
317 * Helper function to memory map and unmap a region needed by a test.
318 *
319 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
320 * unmapped. Otherwise, return the test functions's result.
321 */
322test_result_t map_test_unmap(const map_args_unmap_t *args,
323 test_function_arg_t test);
324
J-Alvesf1126f22020-11-02 17:28:20 +0000325/*
326 * Helper function to set TFTF global mailbox for SPM related tests.
327 * This function should be invoked by the first TFTF test that requires
328 * RX and/or TX buffers.
329 */
330void set_tftf_mailbox(const struct mailbox_buffers *mb);
331
332/*
333 * Helper function to get TFTF global mailbox for SPM related tests.
334 * This function should be called by all tests that require access to RX or TX
335 * buffers, after the function 'set_tftf_mailbox' has been used by the first
336 * test to rely on RX and TX buffers.
337 */
338bool get_tftf_mailbox(struct mailbox_buffers *mb);
339
J-Alves04469302021-01-21 14:48:13 +0000340test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major,
J-Alvesd708c032020-11-19 12:14:21 +0000341 uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
342 size_t ffa_uuids_size);
343
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200344#endif /* __TEST_HELPERS_H__ */