blob: 205d203f050927a90e8cb496d946672b5e55b43d [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Joel Hutton8790f022019-03-15 14:47:02 +00002 * Copyright (c) 2018-2019, 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
Joel Hutton8790f022019-03-15 14:47:02 +000029#ifdef AARCH32
30#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); \
150 uint32_t version = smc_ret.ret0; \
151 \
152 if (version == SMC_UNKNOWN) { \
153 tftf_testcase_printf("SPM not detected.\n"); \
154 return TEST_RESULT_SKIPPED; \
155 } \
156 \
157 if (version < SPCI_VERSION_FORM(major, minor)) { \
158 tftf_testcase_printf("SPCI_VERSION returned %d.%d\n" \
159 "The required version is %d.%d\n", \
160 version >> SPCI_VERSION_MAJOR_SHIFT,\
161 version & SPCI_VERSION_MINOR_MASK, \
162 major, minor); \
163 return TEST_RESULT_SKIPPED; \
164 } \
165 } while (0)
166
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100167#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
168 do { \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100169 uint32_t debug_ver = arch_get_debug_version(); \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100170 \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100171 if (debug_ver < version) { \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100172 tftf_testcase_printf("Debug version returned %d\n" \
173 "The required version is %d\n", \
Petre-Ionut Tudorf1a45f72019-10-08 16:51:45 +0100174 debug_ver, \
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100175 version); \
176 return TEST_RESULT_SKIPPED; \
177 } \
178 } while (0)
179
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200180/* Helper macro to verify if system suspend API is supported */
181#define is_psci_sys_susp_supported() \
182 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
183 == PSCI_E_SUCCESS)
184
185/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
186#define is_psci_stat_count_supported() \
187 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
188 == PSCI_E_SUCCESS)
189
190/*
191 * Helper function to verify the system state is ready for system
192 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
193 * Returns 1 if the system is ready to suspend, 0 otherwise.
194 */
195int is_sys_suspend_state_ready(void);
196
197/*
198 * Helper function to reset the system. This function shouldn't return.
199 * It is not marked with __dead to help the test to catch some error in
200 * TF
201 */
202void psci_system_reset(void);
203
204/*
205 * Helper function that enables/disables the mem_protect mechanism
206 */
207int psci_mem_protect(int val);
208
209
210/*
211 * Helper function to call PSCI MEM_PROTECT_CHECK
212 */
213int psci_mem_protect_check(uintptr_t addr, size_t size);
214
215
216/*
217 * Helper function to get a sentinel address that can be used to test mem_protect
218 */
219unsigned char *psci_mem_prot_get_sentinel(void);
220
221/*
222 * Helper function to memory map and unmap a region needed by a test.
223 *
224 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
225 * unmapped. Otherwise, return the test functions's result.
226 */
227test_result_t map_test_unmap(const map_args_unmap_t *args,
228 test_function_arg_t test);
229
230#endif /* __TEST_HELPERS_H__ */