Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 7 | #ifndef TEST_HELPERS_H__ |
| 8 | #define TEST_HELPERS_H__ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 9 | |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 10 | #include <arch_features.h> |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 11 | #include <ffa_svc.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 12 | #include <plat_topology.h> |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 13 | #include <psci.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 14 | #include <spm_common.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 15 | #include <tftf_lib.h> |
| 16 | #include <trusted_os.h> |
| 17 | #include <tsp.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 18 | #include <uuid_utils.h> |
Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 19 | #include <uuid.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 20 | |
| 21 | typedef struct { |
| 22 | uintptr_t addr; |
| 23 | size_t size; |
| 24 | unsigned int attr; |
| 25 | void *arg; |
| 26 | } map_args_unmap_t; |
| 27 | |
| 28 | typedef test_result_t (*test_function_arg_t)(void *arg); |
| 29 | |
Deepika Bhavnani | c249d5e | 2020-02-06 16:29:45 -0600 | [diff] [blame] | 30 | #ifndef __aarch64__ |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 31 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 40 | #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 Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 92 | #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 Brisson | 90f1d5c | 2020-04-16 10:54:51 -0500 | [diff] [blame] | 101 | #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 Shvetsov | 959be33 | 2021-03-16 14:18:13 +0000 | [diff] [blame] | 110 | #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 Brisson | 945095a | 2020-04-16 10:54:59 -0500 | [diff] [blame] | 118 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 127 | #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 Bailleux | 277fb76 | 2019-10-08 12:10:45 +0200 | [diff] [blame] | 139 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 148 | #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-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 160 | 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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 165 | return TEST_RESULT_SKIPPED; \ |
| 166 | } \ |
| 167 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 168 | VERBOSE("MM_VERSION returned %u.%u\n", \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 169 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 170 | version & MM_VERSION_MINOR_MASK); \ |
| 171 | } while (0) |
| 172 | |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 173 | #define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 174 | do { \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 175 | smc_ret_values smc_ret = ffa_version( \ |
| 176 | MAKE_FFA_VERSION(major, minor)); \ |
| 177 | uint32_t version = smc_ret.ret0; \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 178 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 179 | if (version == FFA_ERROR_NOT_SUPPORTED) { \ |
| 180 | tftf_testcase_printf("FFA_VERSION not supported.\n"); \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 181 | return TEST_RESULT_SKIPPED; \ |
| 182 | } \ |
| 183 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 184 | 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 Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 196 | return TEST_RESULT_SKIPPED; \ |
| 197 | } \ |
| 198 | } while (0) |
| 199 | |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 200 | #define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \ |
| 201 | do { \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 202 | uint32_t debug_ver = arch_get_debug_version(); \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 203 | \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 204 | if (debug_ver < version) { \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 205 | tftf_testcase_printf("Debug version returned %d\n" \ |
| 206 | "The required version is %d\n", \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 207 | debug_ver, \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 208 | version); \ |
| 209 | return TEST_RESULT_SKIPPED; \ |
| 210 | } \ |
| 211 | } while (0) |
| 212 | |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 213 | #define SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(mb, ffa_uuid) \ |
J-Alves | f474306 | 2020-10-27 19:39:57 +0000 | [diff] [blame] | 214 | do { \ |
J-Alves | f474306 | 2020-10-27 19:39:57 +0000 | [diff] [blame] | 215 | smc_ret_values smc_ret = ffa_partition_info_get(ffa_uuid); \ |
| 216 | ffa_rx_release(); \ |
Olivier Deprez | 58757e8 | 2021-07-30 10:18:00 +0200 | [diff] [blame] | 217 | if (ffa_func_id(smc_ret) == FFA_ERROR && \ |
| 218 | ffa_error_code(smc_ret) == FFA_ERROR_INVALID_PARAMETER) { \ |
J-Alves | f474306 | 2020-10-27 19:39:57 +0000 | [diff] [blame] | 219 | 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-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 227 | #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-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 237 | #define CHECK_SPMC_TESTING_SETUP(ffa_major, ffa_minor, expected_uuids) \ |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 238 | do { \ |
Max Shvetsov | 959be33 | 2021-03-16 14:18:13 +0000 | [diff] [blame] | 239 | SKIP_TEST_IF_AARCH32(); \ |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 240 | const size_t expected_uuids_size = \ |
| 241 | sizeof(expected_uuids) / sizeof(struct ffa_uuid); \ |
J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 242 | test_result_t ret = check_spmc_testing_set_up( \ |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 243 | 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 Badarkhe | 87c03d1 | 2021-07-06 22:57:11 +0100 | [diff] [blame] | 250 | #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 Badarkhe | 2c518e5 | 2021-07-08 16:36:57 +0100 | [diff] [blame] | 258 | #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 Badarkhe | 6d0e1b6 | 2021-07-09 13:58:28 +0100 | [diff] [blame^] | 266 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 275 | /* Helper macro to verify if system suspend API is supported */ |
| 276 | #define is_psci_sys_susp_supported() \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 277 | (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 278 | == PSCI_E_SUCCESS) |
| 279 | |
| 280 | /* Helper macro to verify if PSCI_STAT_COUNT API is supported */ |
| 281 | #define is_psci_stat_count_supported() \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame] | 282 | (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 283 | == 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 | */ |
| 290 | int 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 | */ |
| 297 | void psci_system_reset(void); |
| 298 | |
| 299 | /* |
| 300 | * Helper function that enables/disables the mem_protect mechanism |
| 301 | */ |
| 302 | int psci_mem_protect(int val); |
| 303 | |
| 304 | |
| 305 | /* |
| 306 | * Helper function to call PSCI MEM_PROTECT_CHECK |
| 307 | */ |
| 308 | int 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 | */ |
| 314 | unsigned 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 | */ |
| 322 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 323 | test_function_arg_t test); |
| 324 | |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 325 | /* |
| 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 | */ |
| 330 | void 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 | */ |
| 338 | bool get_tftf_mailbox(struct mailbox_buffers *mb); |
| 339 | |
J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 340 | test_result_t check_spmc_testing_set_up(uint32_t ffa_version_major, |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 341 | uint32_t ffa_version_minor, const struct ffa_uuid *ffa_uuids, |
| 342 | size_t ffa_uuids_size); |
| 343 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 344 | #endif /* __TEST_HELPERS_H__ */ |