blob: 8220244d2c75a38f1a48ff5c238b8be155444cd4 [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>
J-Alvesd56c53c2021-07-01 16:32:16 +010012#include <events.h>
J-Alvesf7535f42021-07-30 11:58:41 +010013#include <plat_topology.h>
J-Alves8f08a052020-05-26 17:14:40 +010014#include <psci.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000015#include <spm_common.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020016#include <tftf_lib.h>
17#include <trusted_os.h>
18#include <tsp.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020019#include <uuid_utils.h>
Max Shvetsov103e0562021-02-04 16:58:31 +000020#include <uuid.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020021
22typedef struct {
23 uintptr_t addr;
24 size_t size;
25 unsigned int attr;
26 void *arg;
27} map_args_unmap_t;
28
29typedef test_result_t (*test_function_arg_t)(void *arg);
30
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060031#ifndef __aarch64__
Joel Hutton8790f022019-03-15 14:47:02 +000032#define SKIP_TEST_IF_AARCH32() \
33 do { \
34 tftf_testcase_printf("Test not supported on aarch32\n"); \
35 return TEST_RESULT_SKIPPED; \
36 } while (0)
37#else
38#define SKIP_TEST_IF_AARCH32()
39#endif
40
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020041#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
42 do { \
43 unsigned int clusters_cnt; \
44 clusters_cnt = tftf_get_total_clusters_count(); \
45 if (clusters_cnt < (n)) { \
46 tftf_testcase_printf( \
47 "Need at least %u clusters, only found %u\n", \
48 (n), clusters_cnt); \
49 return TEST_RESULT_SKIPPED; \
50 } \
51 } while (0)
52
53#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
54 do { \
55 unsigned int cpus_cnt; \
56 cpus_cnt = tftf_get_total_cpus_count(); \
57 if (cpus_cnt < (n)) { \
58 tftf_testcase_printf( \
59 "Need at least %u CPUs, only found %u\n", \
60 (n), cpus_cnt); \
61 return TEST_RESULT_SKIPPED; \
62 } \
63 } while (0)
64
65#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
66 do { \
67 uuid_t tos_uuid; \
68 \
69 if (!is_trusted_os_present(&tos_uuid)) { \
70 tftf_testcase_printf("No Trusted OS detected\n"); \
71 return TEST_RESULT_SKIPPED; \
72 } \
73 } while (0)
74
75#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
76 do { \
77 uuid_t tos_uuid; \
78 char tos_uuid_str[UUID_STR_SIZE]; \
79 \
80 if (!is_trusted_os_present(&tos_uuid)) { \
81 tftf_testcase_printf("No Trusted OS detected\n"); \
82 return TEST_RESULT_SKIPPED; \
83 } \
84 \
85 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
86 tftf_testcase_printf( \
87 "Trusted OS is not the TSP, its UUID is: %s\n", \
88 uuid_to_str(&tos_uuid, tos_uuid_str)); \
89 return TEST_RESULT_SKIPPED; \
90 } \
91 } while (0)
92
Joel Hutton8790f022019-03-15 14:47:02 +000093#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
94 do { \
95 if (!is_armv8_3_pauth_present()) { \
96 tftf_testcase_printf( \
97 "Pointer Authentication not supported\n"); \
98 return TEST_RESULT_SKIPPED; \
99 } \
100 } while (0)
101
Jimmy Brisson90f1d5c2020-04-16 10:54:51 -0500102#define SKIP_TEST_IF_FGT_NOT_SUPPORTED() \
103 do { \
104 if (!is_armv8_6_fgt_present()) { \
105 tftf_testcase_printf( \
106 "Fine Grained Traps not supported\n"); \
107 return TEST_RESULT_SKIPPED; \
108 } \
109 } while (0)
110
Max Shvetsov959be332021-03-16 14:18:13 +0000111#define SKIP_TEST_IF_SVE_NOT_SUPPORTED() \
112 do { \
113 if (!is_armv8_2_sve_present()) { \
114 tftf_testcase_printf("SVE not supported\n"); \
115 return TEST_RESULT_SKIPPED; \
116 } \
117 } while (0)
118
Jimmy Brisson945095a2020-04-16 10:54:59 -0500119#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
120 do { \
121 if (get_armv8_6_ecv_support() != \
122 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
123 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
124 return TEST_RESULT_SKIPPED; \
125 } \
126 } while (0)
127
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200128#define SKIP_TEST_IF_MM_NOT_PRESENT() \
129 do { \
130 smc_args version_smc = { MM_VERSION_AARCH32 }; \
131 smc_ret_values smc_ret = tftf_smc(&version_smc); \
132 uint32_t version = smc_ret.ret0; \
133 \
134 if (version == (uint32_t) SMC_UNKNOWN) { \
135 tftf_testcase_printf("SPM not detected.\n"); \
136 return TEST_RESULT_SKIPPED; \
137 } \
138 } while (0)
139
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200140#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
141 do { \
142 if (get_armv8_5_mte_support() < (n)) { \
143 tftf_testcase_printf( \
144 "Memory Tagging Extension not supported\n"); \
145 return TEST_RESULT_SKIPPED; \
146 } \
147 } while (0)
148
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200149#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
150 do { \
151 smc_args version_smc = { MM_VERSION_AARCH32 }; \
152 smc_ret_values smc_ret = tftf_smc(&version_smc); \
153 uint32_t version = smc_ret.ret0; \
154 \
155 if (version == (uint32_t) SMC_UNKNOWN) { \
156 tftf_testcase_printf("SPM not detected.\n"); \
157 return TEST_RESULT_SKIPPED; \
158 } \
159 \
160 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100161 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
162 "The required version is %u.%u\n", \
163 version >> MM_VERSION_MAJOR_SHIFT, \
164 version & MM_VERSION_MINOR_MASK, \
165 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200166 return TEST_RESULT_SKIPPED; \
167 } \
168 \
J-Alves8f08a052020-05-26 17:14:40 +0100169 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200170 version >> MM_VERSION_MAJOR_SHIFT, \
171 version & MM_VERSION_MINOR_MASK); \
172 } while (0)
173
J-Alves7581c382020-05-07 18:34:20 +0100174#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000175 do { \
J-Alves8f08a052020-05-26 17:14:40 +0100176 smc_ret_values smc_ret = ffa_version( \
177 MAKE_FFA_VERSION(major, minor)); \
178 uint32_t version = smc_ret.ret0; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000179 \
J-Alves8f08a052020-05-26 17:14:40 +0100180 if (version == FFA_ERROR_NOT_SUPPORTED) { \
181 tftf_testcase_printf("FFA_VERSION not supported.\n"); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000182 return TEST_RESULT_SKIPPED; \
183 } \
184 \
J-Alves8f08a052020-05-26 17:14:40 +0100185 if ((version & FFA_VERSION_BIT31_MASK) != 0U) { \
186 tftf_testcase_printf("FFA_VERSION bad response: %x\n", \
187 version); \
188 return TEST_RESULT_FAIL; \
189 } \
190 \
191 if (version < MAKE_FFA_VERSION(major, minor)) { \
192 tftf_testcase_printf("FFA_VERSION returned %u.%u\n" \
193 "The required version is %u.%u\n", \
194 version >> FFA_VERSION_MAJOR_SHIFT, \
195 version & FFA_VERSION_MINOR_MASK, \
196 major, minor); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000197 return TEST_RESULT_SKIPPED; \
198 } \
199 } while (0)
200
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100201#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
202 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100203 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100204 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100205 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100206 tftf_testcase_printf("Debug version returned %d\n" \
207 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100208 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100209 version); \
210 return TEST_RESULT_SKIPPED; \
211 } \
212 } while (0)
213
J-Alvesd708c032020-11-19 12:14:21 +0000214#define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid) \
J-Alvesf4743062020-10-27 19:39:57 +0000215 do { \
J-Alvesf4743062020-10-27 19:39:57 +0000216 smc_ret_values smc_ret = ffa_partition_info_get(ffa_uuid); \
217 ffa_rx_release(); \
Olivier Deprez58757e82021-07-30 10:18:00 +0200218 if (ffa_func_id(smc_ret) == FFA_ERROR && \
219 ffa_error_code(smc_ret) == FFA_ERROR_INVALID_PARAMETER) { \
J-Alvesf4743062020-10-27 19:39:57 +0000220 tftf_testcase_printf("FFA endpoint not deployed!\n"); \
221 return TEST_RESULT_SKIPPED; \
222 } else if (smc_ret.ret0 != FFA_SUCCESS_SMC32) { \
223 ERROR("ffa_partition_info_get failed!\n"); \
224 return TEST_RESULT_FAIL; \
225 } \
226 } while (0)
227
J-Alvesd708c032020-11-19 12:14:21 +0000228#define GET_TFTF_MAILBOX(mb) \
229 do { \
230 if (!get_tftf_mailbox(&mb)) { \
231 ERROR("Mailbox not configured!\nThis test relies on" \
232 " test suite \"FF-A RXTX Mapping\" to map/configure" \
233 " RXTX buffers\n"); \
234 return TEST_RESULT_FAIL; \
235 } \
236 } while (false);
237
J-Alves04469302021-01-21 14:48:13 +0000238#define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids) \
J-Alvesd708c032020-11-19 12:14:21 +0000239 do { \
Max Shvetsov959be332021-03-16 14:18:13 +0000240 SKIP_TEST_IF_AARCH32(); \
J-Alvesd708c032020-11-19 12:14:21 +0000241 const size_t expected_uuids_size = \
242 sizeof(expected_uuids) / sizeof(struct ffa_uuid); \
J-Alves04469302021-01-21 14:48:13 +0000243 test_result_t ret = check_spmc_testing_set_up( \
J-Alvesd708c032020-11-19 12:14:21 +0000244 ffa_major, ffa_minor, expected_uuids, \
245 expected_uuids_size); \
246 if (ret != TEST_RESULT_SUCCESS) { \
247 return ret; \
248 } \
249 } while (false);
250
Manish V Badarkhe87c03d12021-07-06 22:57:11 +0100251#define SKIP_TEST_IF_TRBE_NOT_SUPPORTED() \
252 do { \
253 if (!get_armv9_0_trbe_support()) { \
254 tftf_testcase_printf("ARMv9-TRBE not supported\n"); \
255 return TEST_RESULT_SKIPPED; \
256 } \
257 } while (false)
258
Manish V Badarkhe2c518e52021-07-08 16:36:57 +0100259#define SKIP_TEST_IF_TRF_NOT_SUPPORTED() \
260 do { \
261 if (!get_armv8_4_trf_support()) { \
262 tftf_testcase_printf("ARMv8.4-TRF not supported\n"); \
263 return TEST_RESULT_SKIPPED; \
264 } \
265 } while (false)
266
Manish V Badarkhe6d0e1b62021-07-09 13:58:28 +0100267#define SKIP_TEST_IF_SYS_REG_TRACE_NOT_SUPPORTED() \
268 do { \
269 if (!get_armv8_0_sys_reg_trace_support()) { \
270 tftf_testcase_printf("ARMv8-system register" \
271 "trace not supported\n"); \
272 return TEST_RESULT_SKIPPED; \
273 } \
274 } while (false)
275
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200276/* Helper macro to verify if system suspend API is supported */
277#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100278 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200279 == PSCI_E_SUCCESS)
280
281/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
282#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100283 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200284 == PSCI_E_SUCCESS)
285
286/*
287 * Helper function to verify the system state is ready for system
288 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
289 * Returns 1 if the system is ready to suspend, 0 otherwise.
290 */
291int is_sys_suspend_state_ready(void);
292
293/*
294 * Helper function to reset the system. This function shouldn't return.
295 * It is not marked with __dead to help the test to catch some error in
296 * TF
297 */
298void psci_system_reset(void);
299
300/*
301 * Helper function that enables/disables the mem_protect mechanism
302 */
303int psci_mem_protect(int val);
304
305
306/*
307 * Helper function to call PSCI MEM_PROTECT_CHECK
308 */
309int psci_mem_protect_check(uintptr_t addr, size_t size);
310
311
312/*
313 * Helper function to get a sentinel address that can be used to test mem_protect
314 */
315unsigned char *psci_mem_prot_get_sentinel(void);
316
317/*
318 * Helper function to memory map and unmap a region needed by a test.
319 *
320 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
321 * unmapped. Otherwise, return the test functions's result.
322 */
323test_result_t map_test_unmap(const map_args_unmap_t *args,
324 test_function_arg_t test);
325
J-Alvesf1126f22020-11-02 17:28:20 +0000326/*
327 * Helper function to set TFTF global mailbox for SPM related tests.
328 * This function should be invoked by the first TFTF test that requires
329 * RX and/or TX buffers.
330 */
331void set_tftf_mailbox(const struct mailbox_buffers *mb);
332
333/*
334 * Helper function to get TFTF global mailbox for SPM related tests.
335 * This function should be called by all tests that require access to RX or TX
336 * buffers, after the function 'set_tftf_mailbox' has been used by the first
337 * test to rely on RX and TX buffers.
338 */
339bool get_tftf_mailbox(struct mailbox_buffers *mb);
340
J-Alves04469302021-01-21 14:48:13 +0000341test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major,
J-Alvesd708c032020-11-19 12:14:21 +0000342 uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
343 size_t ffa_uuids_size);
344
J-Alvesd56c53c2021-07-01 16:32:16 +0100345/**
346 * Turn on all cpus to execute a test in all.
347 * - 'cpu_on_handler' should have the code containing the test.
348 * - 'cpu_booted' is used for notifying which cores the test has been executed.
349 * This should be used in the test executed by cpu_on_handler at the end of
350 * processing to make sure it complies with this function's implementation.
351 */
352test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler,
353 event_t *cpu_booted);
354
J-Alves952e1f72021-07-30 17:19:09 +0100355/**
356 * Enable/Disable managed exit interrupt for the provided SP.
357 */
358bool spm_set_managed_exit_int(ffa_id_t sp_id, bool enable);
359
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200360#endif /* __TEST_HELPERS_H__ */