blob: 5c0f090e5986a344aa92f0bebe63e5c2f73f020b [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
Joel Hutton8790f022019-03-15 14:47:02 +000010#include <arch_features.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020011#include <plat_topology.h>
12#include <psci.h>
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +000013#include <spci_svc.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020014#include <tftf_lib.h>
15#include <trusted_os.h>
16#include <tsp.h>
17#include <uuid.h>
18#include <uuid_utils.h>
19
20typedef struct {
21 uintptr_t addr;
22 size_t size;
23 unsigned int attr;
24 void *arg;
25} map_args_unmap_t;
26
27typedef test_result_t (*test_function_arg_t)(void *arg);
28
Deepika Bhavnanic249d5e2020-02-06 16:29:45 -060029#ifndef __aarch64__
Joel Hutton8790f022019-03-15 14:47:02 +000030#define SKIP_TEST_IF_AARCH32() \
31 do { \
32 tftf_testcase_printf("Test not supported on aarch32\n"); \
33 return TEST_RESULT_SKIPPED; \
34 } while (0)
35#else
36#define SKIP_TEST_IF_AARCH32()
37#endif
38
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020039#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
40 do { \
41 unsigned int clusters_cnt; \
42 clusters_cnt = tftf_get_total_clusters_count(); \
43 if (clusters_cnt < (n)) { \
44 tftf_testcase_printf( \
45 "Need at least %u clusters, only found %u\n", \
46 (n), clusters_cnt); \
47 return TEST_RESULT_SKIPPED; \
48 } \
49 } while (0)
50
51#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
52 do { \
53 unsigned int cpus_cnt; \
54 cpus_cnt = tftf_get_total_cpus_count(); \
55 if (cpus_cnt < (n)) { \
56 tftf_testcase_printf( \
57 "Need at least %u CPUs, only found %u\n", \
58 (n), cpus_cnt); \
59 return TEST_RESULT_SKIPPED; \
60 } \
61 } while (0)
62
63#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
64 do { \
65 uuid_t tos_uuid; \
66 \
67 if (!is_trusted_os_present(&tos_uuid)) { \
68 tftf_testcase_printf("No Trusted OS detected\n"); \
69 return TEST_RESULT_SKIPPED; \
70 } \
71 } while (0)
72
73#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
74 do { \
75 uuid_t tos_uuid; \
76 char tos_uuid_str[UUID_STR_SIZE]; \
77 \
78 if (!is_trusted_os_present(&tos_uuid)) { \
79 tftf_testcase_printf("No Trusted OS detected\n"); \
80 return TEST_RESULT_SKIPPED; \
81 } \
82 \
83 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
84 tftf_testcase_printf( \
85 "Trusted OS is not the TSP, its UUID is: %s\n", \
86 uuid_to_str(&tos_uuid, tos_uuid_str)); \
87 return TEST_RESULT_SKIPPED; \
88 } \
89 } while (0)
90
Joel Hutton8790f022019-03-15 14:47:02 +000091#define SKIP_TEST_IF_PAUTH_NOT_SUPPORTED() \
92 do { \
93 if (!is_armv8_3_pauth_present()) { \
94 tftf_testcase_printf( \
95 "Pointer Authentication not supported\n"); \
96 return TEST_RESULT_SKIPPED; \
97 } \
98 } while (0)
99
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200100#define SKIP_TEST_IF_MM_NOT_PRESENT() \
101 do { \
102 smc_args version_smc = { MM_VERSION_AARCH32 }; \
103 smc_ret_values smc_ret = tftf_smc(&version_smc); \
104 uint32_t version = smc_ret.ret0; \
105 \
106 if (version == (uint32_t) SMC_UNKNOWN) { \
107 tftf_testcase_printf("SPM not detected.\n"); \
108 return TEST_RESULT_SKIPPED; \
109 } \
110 } while (0)
111
Sandrine Bailleux277fb762019-10-08 12:10:45 +0200112#define SKIP_TEST_IF_MTE_SUPPORT_LESS_THAN(n) \
113 do { \
114 if (get_armv8_5_mte_support() < (n)) { \
115 tftf_testcase_printf( \
116 "Memory Tagging Extension not supported\n"); \
117 return TEST_RESULT_SKIPPED; \
118 } \
119 } while (0)
120
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200121#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
122 do { \
123 smc_args version_smc = { MM_VERSION_AARCH32 }; \
124 smc_ret_values smc_ret = tftf_smc(&version_smc); \
125 uint32_t version = smc_ret.ret0; \
126 \
127 if (version == (uint32_t) SMC_UNKNOWN) { \
128 tftf_testcase_printf("SPM not detected.\n"); \
129 return TEST_RESULT_SKIPPED; \
130 } \
131 \
132 if (version < MM_VERSION_FORM(major, minor)) { \
133 tftf_testcase_printf("MM_VERSION returned %d.%d\n" \
134 "The required version is %d.%d\n", \
135 version >> MM_VERSION_MAJOR_SHIFT, \
136 version & MM_VERSION_MINOR_MASK, \
137 major, minor); \
138 return TEST_RESULT_SKIPPED; \
139 } \
140 \
141 VERBOSE("MM_VERSION returned %d.%d\n", \
142 version >> MM_VERSION_MAJOR_SHIFT, \
143 version & MM_VERSION_MINOR_MASK); \
144 } while (0)
145
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000146#define SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(major, minor) \
147 do { \
148 smc_args version_smc = { SPCI_VERSION }; \
149 smc_ret_values smc_ret = tftf_smc(&version_smc); \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100150 uint32_t version = smc_ret.ret2; \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000151 \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100152 if (smc_ret.ret0 != SPCI_SUCCESS_SMC32) { \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000153 tftf_testcase_printf("SPM not detected.\n"); \
154 return TEST_RESULT_SKIPPED; \
155 } \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100156 \
157 if ((version & SPCI_VERSION_BIT31_MASK) != 0) { \
158 tftf_testcase_printf("SPCI_VERSION bad response.\n"); \
159 return TEST_RESULT_SKIPPED; \
160 } \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000161 \
Olivier Deprez61be4c12019-12-06 17:45:07 +0100162 if (version < MAKE_SPCI_VERSION(major, minor)) { \
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000163 tftf_testcase_printf("SPCI_VERSION returned %d.%d\n" \
164 "The required version is %d.%d\n", \
165 version >> SPCI_VERSION_MAJOR_SHIFT,\
166 version & SPCI_VERSION_MINOR_MASK, \
167 major, minor); \
168 return TEST_RESULT_SKIPPED; \
169 } \
170 } while (0)
171
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100172#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
173 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100174 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100175 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100176 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100177 tftf_testcase_printf("Debug version returned %d\n" \
178 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100179 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100180 version); \
181 return TEST_RESULT_SKIPPED; \
182 } \
183 } while (0)
184
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200185/* Helper macro to verify if system suspend API is supported */
186#define is_psci_sys_susp_supported() \
187 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
188 == PSCI_E_SUCCESS)
189
190/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
191#define is_psci_stat_count_supported() \
192 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
193 == PSCI_E_SUCCESS)
194
195/*
196 * Helper function to verify the system state is ready for system
197 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
198 * Returns 1 if the system is ready to suspend, 0 otherwise.
199 */
200int is_sys_suspend_state_ready(void);
201
202/*
203 * Helper function to reset the system. This function shouldn't return.
204 * It is not marked with __dead to help the test to catch some error in
205 * TF
206 */
207void psci_system_reset(void);
208
209/*
210 * Helper function that enables/disables the mem_protect mechanism
211 */
212int psci_mem_protect(int val);
213
214
215/*
216 * Helper function to call PSCI MEM_PROTECT_CHECK
217 */
218int psci_mem_protect_check(uintptr_t addr, size_t size);
219
220
221/*
222 * Helper function to get a sentinel address that can be used to test mem_protect
223 */
224unsigned char *psci_mem_prot_get_sentinel(void);
225
226/*
227 * Helper function to memory map and unmap a region needed by a test.
228 *
229 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
230 * unmapped. Otherwise, return the test functions's result.
231 */
232test_result_t map_test_unmap(const map_args_unmap_t *args,
233 test_function_arg_t test);
234
235#endif /* __TEST_HELPERS_H__ */