Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Deepika Bhavnani | c249d5e | 2020-02-06 16:29:45 -0600 | [diff] [blame] | 2 | * Copyright (c) 2018-2020, 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 | |
| 7 | #ifndef __TEST_HELPERS_H__ |
| 8 | #define __TEST_HELPERS_H__ |
| 9 | |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 10 | #include <uuid.h> |
Joel Hutton | 8790f02 | 2019-03-15 14:47:02 +0000 | [diff] [blame] | 11 | #include <arch_features.h> |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 12 | #include <ffa_helpers.h> |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 13 | #include <ffa_svc.h> |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 14 | #include <psci.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> |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 19 | #include <plat_topology.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 | |
Jimmy Brisson | 945095a | 2020-04-16 10:54:59 -0500 | [diff] [blame] | 110 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 119 | #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 Bailleux | 277fb76 | 2019-10-08 12:10:45 +0200 | [diff] [blame] | 131 | #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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 140 | #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-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 152 | 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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 157 | return TEST_RESULT_SKIPPED; \ |
| 158 | } \ |
| 159 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 160 | VERBOSE("MM_VERSION returned %u.%u\n", \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 161 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 162 | version & MM_VERSION_MINOR_MASK); \ |
| 163 | } while (0) |
| 164 | |
J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 165 | #define SKIP_TEST_IF_FFA_VERSION_LESS_THAN(major, minor) \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 166 | do { \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 167 | smc_ret_values smc_ret = ffa_version( \ |
| 168 | MAKE_FFA_VERSION(major, minor)); \ |
| 169 | uint32_t version = smc_ret.ret0; \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 170 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 171 | if (version == FFA_ERROR_NOT_SUPPORTED) { \ |
| 172 | tftf_testcase_printf("FFA_VERSION not supported.\n"); \ |
Antonio Nino Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 173 | return TEST_RESULT_SKIPPED; \ |
| 174 | } \ |
| 175 | \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 176 | 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 Diaz | 652d20a | 2018-12-10 17:17:33 +0000 | [diff] [blame] | 188 | return TEST_RESULT_SKIPPED; \ |
| 189 | } \ |
| 190 | } while (0) |
| 191 | |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 192 | #define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \ |
| 193 | do { \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 194 | uint32_t debug_ver = arch_get_debug_version(); \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 195 | \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 196 | if (debug_ver < version) { \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 197 | tftf_testcase_printf("Debug version returned %d\n" \ |
| 198 | "The required version is %d\n", \ |
Petre-Ionut Tudor | f1a45f7 | 2019-10-08 16:51:45 +0100 | [diff] [blame] | 199 | debug_ver, \ |
Petre-Ionut Tudor | f68ebdb | 2019-09-18 16:13:00 +0100 | [diff] [blame] | 200 | version); \ |
| 201 | return TEST_RESULT_SKIPPED; \ |
| 202 | } \ |
| 203 | } while (0) |
| 204 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 205 | /* Helper macro to verify if system suspend API is supported */ |
| 206 | #define is_psci_sys_susp_supported() \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 207 | (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 208 | == PSCI_E_SUCCESS) |
| 209 | |
| 210 | /* Helper macro to verify if PSCI_STAT_COUNT API is supported */ |
| 211 | #define is_psci_stat_count_supported() \ |
J-Alves | 8f08a05 | 2020-05-26 17:14:40 +0100 | [diff] [blame^] | 212 | (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 213 | == PSCI_E_SUCCESS) |
| 214 | |
| 215 | /* |
| 216 | * Helper function to verify the system state is ready for system |
| 217 | * suspend. i.e., a single CPU is running and all other CPUs are powered off. |
| 218 | * Returns 1 if the system is ready to suspend, 0 otherwise. |
| 219 | */ |
| 220 | int is_sys_suspend_state_ready(void); |
| 221 | |
| 222 | /* |
| 223 | * Helper function to reset the system. This function shouldn't return. |
| 224 | * It is not marked with __dead to help the test to catch some error in |
| 225 | * TF |
| 226 | */ |
| 227 | void psci_system_reset(void); |
| 228 | |
| 229 | /* |
| 230 | * Helper function that enables/disables the mem_protect mechanism |
| 231 | */ |
| 232 | int psci_mem_protect(int val); |
| 233 | |
| 234 | |
| 235 | /* |
| 236 | * Helper function to call PSCI MEM_PROTECT_CHECK |
| 237 | */ |
| 238 | int psci_mem_protect_check(uintptr_t addr, size_t size); |
| 239 | |
| 240 | |
| 241 | /* |
| 242 | * Helper function to get a sentinel address that can be used to test mem_protect |
| 243 | */ |
| 244 | unsigned char *psci_mem_prot_get_sentinel(void); |
| 245 | |
| 246 | /* |
| 247 | * Helper function to memory map and unmap a region needed by a test. |
| 248 | * |
| 249 | * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or |
| 250 | * unmapped. Otherwise, return the test functions's result. |
| 251 | */ |
| 252 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 253 | test_function_arg_t test); |
| 254 | |
| 255 | #endif /* __TEST_HELPERS_H__ */ |