blob: 141f00c79cbab7f4e064ac2455fca982e22a8043 [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
112#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
113 do { \
114 smc_args version_smc = { MM_VERSION_AARCH32 }; \
115 smc_ret_values smc_ret = tftf_smc(&version_smc); \
116 uint32_t version = smc_ret.ret0; \
117 \
118 if (version == (uint32_t) SMC_UNKNOWN) { \
119 tftf_testcase_printf("SPM not detected.\n"); \
120 return TEST_RESULT_SKIPPED; \
121 } \
122 \
123 if (version < MM_VERSION_FORM(major, minor)) { \
124 tftf_testcase_printf("MM_VERSION returned %d.%d\n" \
125 "The required version is %d.%d\n", \
126 version >> MM_VERSION_MAJOR_SHIFT, \
127 version & MM_VERSION_MINOR_MASK, \
128 major, minor); \
129 return TEST_RESULT_SKIPPED; \
130 } \
131 \
132 VERBOSE("MM_VERSION returned %d.%d\n", \
133 version >> MM_VERSION_MAJOR_SHIFT, \
134 version & MM_VERSION_MINOR_MASK); \
135 } while (0)
136
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000137#define SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(major, minor) \
138 do { \
139 smc_args version_smc = { SPCI_VERSION }; \
140 smc_ret_values smc_ret = tftf_smc(&version_smc); \
141 uint32_t version = smc_ret.ret0; \
142 \
143 if (version == SMC_UNKNOWN) { \
144 tftf_testcase_printf("SPM not detected.\n"); \
145 return TEST_RESULT_SKIPPED; \
146 } \
147 \
148 if (version < SPCI_VERSION_FORM(major, minor)) { \
149 tftf_testcase_printf("SPCI_VERSION returned %d.%d\n" \
150 "The required version is %d.%d\n", \
151 version >> SPCI_VERSION_MAJOR_SHIFT,\
152 version & SPCI_VERSION_MINOR_MASK, \
153 major, minor); \
154 return TEST_RESULT_SKIPPED; \
155 } \
156 } while (0)
157
Petre-Ionut Tudorf68ebdb2019-09-18 16:13:00 +0100158#define SKIP_TEST_IF_ARCH_DEBUG_VERSION_LESS_THAN(version) \
159 do { \
160 uint32_t debug_ver = read_id_aa64dfr0_el1() & \
161 (ID_AA64DFR0_DEBUG_MASK << ID_AA64DFR0_DEBUG_SHIFT); \
162 \
163 if ((debug_ver >> ID_AA64DFR0_DEBUG_SHIFT) < version) { \
164 tftf_testcase_printf("Debug version returned %d\n" \
165 "The required version is %d\n", \
166 debug_ver >> ID_AA64DFR0_DEBUG_SHIFT,\
167 version); \
168 return TEST_RESULT_SKIPPED; \
169 } \
170 } while (0)
171
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200172/* Helper macro to verify if system suspend API is supported */
173#define is_psci_sys_susp_supported() \
174 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
175 == PSCI_E_SUCCESS)
176
177/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
178#define is_psci_stat_count_supported() \
179 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
180 == PSCI_E_SUCCESS)
181
182/*
183 * Helper function to verify the system state is ready for system
184 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
185 * Returns 1 if the system is ready to suspend, 0 otherwise.
186 */
187int is_sys_suspend_state_ready(void);
188
189/*
190 * Helper function to reset the system. This function shouldn't return.
191 * It is not marked with __dead to help the test to catch some error in
192 * TF
193 */
194void psci_system_reset(void);
195
196/*
197 * Helper function that enables/disables the mem_protect mechanism
198 */
199int psci_mem_protect(int val);
200
201
202/*
203 * Helper function to call PSCI MEM_PROTECT_CHECK
204 */
205int psci_mem_protect_check(uintptr_t addr, size_t size);
206
207
208/*
209 * Helper function to get a sentinel address that can be used to test mem_protect
210 */
211unsigned char *psci_mem_prot_get_sentinel(void);
212
213/*
214 * Helper function to memory map and unmap a region needed by a test.
215 *
216 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
217 * unmapped. Otherwise, return the test functions's result.
218 */
219test_result_t map_test_unmap(const map_args_unmap_t *args,
220 test_function_arg_t test);
221
222#endif /* __TEST_HELPERS_H__ */