blob: 3ee2b53185ec40e5e6ddeadc7966f5ef9df49d93 [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
Jimmy Brisson945095a2020-04-16 10:54:59 -0500110#define SKIP_TEST_IF_ECV_NOT_SELF_SYNC() \
111 do { \
112 if (get_armv8_6_ecv_support() != \
113 ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) { \
114 tftf_testcase_printf("ARMv8.6-ECV not supported\n"); \
115 return TEST_RESULT_SKIPPED; \
116 } \
117 } while (0)
118
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200119#define SKIP_TEST_IF_MM_NOT_PRESENT() \
120 do { \
121 smc_args version_smc = { MM_VERSION_AARCH32 }; \
122 smc_ret_values smc_ret = tftf_smc(&version_smc); \
123 uint32_t version = smc_ret.ret0; \
124 \
125 if (version == (uint32_t) SMC_UNKNOWN) { \
126 tftf_testcase_printf("SPM not detected.\n"); \
127 return TEST_RESULT_SKIPPED; \
128 } \
129 } while (0)
130
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200131#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
132 do { \
133 if (get_armv8_5_mte_support() < (n)) { \
134 tftf_testcase_printf( \
135 "Memory Tagging Extension not supported\n"); \
136 return TEST_RESULT_SKIPPED; \
137 } \
138 } while (0)
139
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200140#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
141 do { \
142 smc_args version_smc = { MM_VERSION_AARCH32 }; \
143 smc_ret_values smc_ret = tftf_smc(&version_smc); \
144 uint32_t version = smc_ret.ret0; \
145 \
146 if (version == (uint32_t) SMC_UNKNOWN) { \
147 tftf_testcase_printf("SPM not detected.\n"); \
148 return TEST_RESULT_SKIPPED; \
149 } \
150 \
151 if (version < MM_VERSION_FORM(major, minor)) { \
J-Alves8f08a052020-05-26 17:14:40 +0100152 tftf_testcase_printf("MM_VERSION returned %u.%u\n" \
153 "The required version is %u.%u\n", \
154 version >> MM_VERSION_MAJOR_SHIFT, \
155 version & MM_VERSION_MINOR_MASK, \
156 major, minor); \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200157 return TEST_RESULT_SKIPPED; \
158 } \
159 \
J-Alves8f08a052020-05-26 17:14:40 +0100160 VERBOSE("MM_VERSION returned %u.%u\n", \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200161 version >> MM_VERSION_MAJOR_SHIFT, \
162 version & MM_VERSION_MINOR_MASK); \
163 } while (0)
164
J-Alves7581c382020-05-07 18:34:20 +0100165#define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000166 do { \
J-Alves8f08a052020-05-26 17:14:40 +0100167 smc_ret_values smc_ret = ffa_version( \
168 MAKE_FFA_VERSION(major, minor)); \
169 uint32_t version = smc_ret.ret0; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000170 \
J-Alves8f08a052020-05-26 17:14:40 +0100171 if (version == FFA_ERROR_NOT_SUPPORTED) { \
172 tftf_testcase_printf("FFA_VERSION not supported.\n"); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000173 return TEST_RESULT_SKIPPED; \
174 } \
175 \
J-Alves8f08a052020-05-26 17:14:40 +0100176 if ((version & FFA_VERSION_BIT31_MASK) != 0U) { \
177 tftf_testcase_printf("FFA_VERSION bad response: %x\n", \
178 version); \
179 return TEST_RESULT_FAIL; \
180 } \
181 \
182 if (version < MAKE_FFA_VERSION(major, minor)) { \
183 tftf_testcase_printf("FFA_VERSION returned %u.%u\n" \
184 "The required version is %u.%u\n", \
185 version >> FFA_VERSION_MAJOR_SHIFT, \
186 version & FFA_VERSION_MINOR_MASK, \
187 major, minor); \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000188 return TEST_RESULT_SKIPPED; \
189 } \
190 } while (0)
191
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100192#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
193 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100194 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100195 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100196 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100197 tftf_testcase_printf("Debug version returned %d\n" \
198 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100199 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100200 version); \
201 return TEST_RESULT_SKIPPED; \
202 } \
203 } while (0)
204
J-Alvesd708c032020-11-19 12:14:21 +0000205#define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid) \
J-Alvesf4743062020-10-27 19:39:57 +0000206 do { \
J-Alvesf4743062020-10-27 19:39:57 +0000207 smc_ret_values smc_ret = ffa_partition_info_get(ffa_uuid); \
208 ffa_rx_release(); \
209 if (smc_ret.ret0 == FFA_ERROR && \
210 smc_ret.ret2 == FFA_ERROR_INVALID_PARAMETER) { \
211 tftf_testcase_printf("FFA endpoint not deployed!\n"); \
212 return TEST_RESULT_SKIPPED; \
213 } else if (smc_ret.ret0 != FFA_SUCCESS_SMC32) { \
214 ERROR("ffa_partition_info_get failed!\n"); \
215 return TEST_RESULT_FAIL; \
216 } \
217 } while (0)
218
J-Alvesd708c032020-11-19 12:14:21 +0000219#define GET_TFTF_MAILBOX(mb) \
220 do { \
221 if (!get_tftf_mailbox(&mb)) { \
222 ERROR("Mailbox not configured!\nThis test relies on" \
223 " test suite \"FF-A RXTX Mapping\" to map/configure" \
224 " RXTX buffers\n"); \
225 return TEST_RESULT_FAIL; \
226 } \
227 } while (false);
228
J-Alves04469302021-01-21 14:48:13 +0000229#define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids) \
J-Alvesd708c032020-11-19 12:14:21 +0000230 do { \
231 const size_t expected_uuids_size = \
232 sizeof(expected_uuids) / sizeof(struct ffa_uuid); \
J-Alves04469302021-01-21 14:48:13 +0000233 test_result_t ret = check_spmc_testing_set_up( \
J-Alvesd708c032020-11-19 12:14:21 +0000234 ffa_major, ffa_minor, expected_uuids, \
235 expected_uuids_size); \
236 if (ret != TEST_RESULT_SUCCESS) { \
237 return ret; \
238 } \
239 } while (false);
240
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200241/* Helper macro to verify if system suspend API is supported */
242#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100243 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200244 == PSCI_E_SUCCESS)
245
246/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
247#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100248 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200249 == PSCI_E_SUCCESS)
250
251/*
252 * Helper function to verify the system state is ready for system
253 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
254 * Returns 1 if the system is ready to suspend, 0 otherwise.
255 */
256int is_sys_suspend_state_ready(void);
257
258/*
259 * Helper function to reset the system. This function shouldn't return.
260 * It is not marked with __dead to help the test to catch some error in
261 * TF
262 */
263void psci_system_reset(void);
264
265/*
266 * Helper function that enables/disables the mem_protect mechanism
267 */
268int psci_mem_protect(int val);
269
270
271/*
272 * Helper function to call PSCI MEM_PROTECT_CHECK
273 */
274int psci_mem_protect_check(uintptr_t addr, size_t size);
275
276
277/*
278 * Helper function to get a sentinel address that can be used to test mem_protect
279 */
280unsigned char *psci_mem_prot_get_sentinel(void);
281
282/*
283 * Helper function to memory map and unmap a region needed by a test.
284 *
285 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
286 * unmapped. Otherwise, return the test functions's result.
287 */
288test_result_t map_test_unmap(const map_args_unmap_t *args,
289 test_function_arg_t test);
290
J-Alvesf1126f22020-11-02 17:28:20 +0000291/*
292 * Helper function to set TFTF global mailbox for SPM related tests.
293 * This function should be invoked by the first TFTF test that requires
294 * RX and/or TX buffers.
295 */
296void set_tftf_mailbox(const struct mailbox_buffers *mb);
297
298/*
299 * Helper function to get TFTF global mailbox for SPM related tests.
300 * This function should be called by all tests that require access to RX or TX
301 * buffers, after the function 'set_tftf_mailbox' has been used by the first
302 * test to rely on RX and TX buffers.
303 */
304bool get_tftf_mailbox(struct mailbox_buffers *mb);
305
J-Alves04469302021-01-21 14:48:13 +0000306test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major,
J-Alvesd708c032020-11-19 12:14:21 +0000307 uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids,
308 size_t ffa_uuids_size);
309
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200310#endif /* __TEST_HELPERS_H__ */