blob: bf14a80387c792371e7290f3b3f78fdddee70627 [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -06002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __TEST_HELPERS_H__
8#define __TEST_HELPERS_H__
9
J-Alves8f08a052020-05-26 17:14:40 +010010#include <uuid.h>
Joel Hutton8790f022019-03-15 14:47:02 +000011#include <arch_features.h>
J-Alves8f08a052020-05-26 17:14:40 +010012#include <ffa_helpers.h>
J-Alves7581c382020-05-07 18:34:20 +010013#include <ffa_svc.h>
J-Alves8f08a052020-05-26 17:14:40 +010014#include <psci.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>
J-Alves8f08a052020-05-26 17:14:40 +010019#include <plat_topology.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
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200205/* Helper macro to verify if system suspend API is supported */
206#define is_psci_sys_susp_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100207 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200208 == PSCI_E_SUCCESS)
209
210/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
211#define is_psci_stat_count_supported() \
J-Alves8f08a052020-05-26 17:14:40 +0100212 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200213 == 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 */
220int 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 */
227void psci_system_reset(void);
228
229/*
230 * Helper function that enables/disables the mem_protect mechanism
231 */
232int psci_mem_protect(int val);
233
234
235/*
236 * Helper function to call PSCI MEM_PROTECT_CHECK
237 */
238int 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 */
244unsigned 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 */
252test_result_t map_test_unmap(const map_args_unmap_t *args,
253 test_function_arg_t test);
254
255#endif /* __TEST_HELPERS_H__ */