blob: 983aed3f61d0ead80d8a034d46c3c56ec51032eb [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
2 * Copyright (c) 2018, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __TEST_HELPERS_H__
8#define __TEST_HELPERS_H__
9
10#include <plat_topology.h>
11#include <psci.h>
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +000012#include <spci_svc.h>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020013#include <tftf_lib.h>
14#include <trusted_os.h>
15#include <tsp.h>
16#include <uuid.h>
17#include <uuid_utils.h>
18
19typedef struct {
20 uintptr_t addr;
21 size_t size;
22 unsigned int attr;
23 void *arg;
24} map_args_unmap_t;
25
26typedef test_result_t (*test_function_arg_t)(void *arg);
27
28#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \
29 do { \
30 unsigned int clusters_cnt; \
31 clusters_cnt = tftf_get_total_clusters_count(); \
32 if (clusters_cnt < (n)) { \
33 tftf_testcase_printf( \
34 "Need at least %u clusters, only found %u\n", \
35 (n), clusters_cnt); \
36 return TEST_RESULT_SKIPPED; \
37 } \
38 } while (0)
39
40#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \
41 do { \
42 unsigned int cpus_cnt; \
43 cpus_cnt = tftf_get_total_cpus_count(); \
44 if (cpus_cnt < (n)) { \
45 tftf_testcase_printf( \
46 "Need at least %u CPUs, only found %u\n", \
47 (n), cpus_cnt); \
48 return TEST_RESULT_SKIPPED; \
49 } \
50 } while (0)
51
52#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \
53 do { \
54 uuid_t tos_uuid; \
55 \
56 if (!is_trusted_os_present(&tos_uuid)) { \
57 tftf_testcase_printf("No Trusted OS detected\n"); \
58 return TEST_RESULT_SKIPPED; \
59 } \
60 } while (0)
61
62#define SKIP_TEST_IF_TSP_NOT_PRESENT() \
63 do { \
64 uuid_t tos_uuid; \
65 char tos_uuid_str[UUID_STR_SIZE]; \
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 \
72 if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \
73 tftf_testcase_printf( \
74 "Trusted OS is not the TSP, its UUID is: %s\n", \
75 uuid_to_str(&tos_uuid, tos_uuid_str)); \
76 return TEST_RESULT_SKIPPED; \
77 } \
78 } while (0)
79
80#define SKIP_TEST_IF_MM_NOT_PRESENT() \
81 do { \
82 smc_args version_smc = { MM_VERSION_AARCH32 }; \
83 smc_ret_values smc_ret = tftf_smc(&version_smc); \
84 uint32_t version = smc_ret.ret0; \
85 \
86 if (version == (uint32_t) SMC_UNKNOWN) { \
87 tftf_testcase_printf("SPM not detected.\n"); \
88 return TEST_RESULT_SKIPPED; \
89 } \
90 } while (0)
91
92#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \
93 do { \
94 smc_args version_smc = { MM_VERSION_AARCH32 }; \
95 smc_ret_values smc_ret = tftf_smc(&version_smc); \
96 uint32_t version = smc_ret.ret0; \
97 \
98 if (version == (uint32_t) SMC_UNKNOWN) { \
99 tftf_testcase_printf("SPM not detected.\n"); \
100 return TEST_RESULT_SKIPPED; \
101 } \
102 \
103 if (version < MM_VERSION_FORM(major, minor)) { \
104 tftf_testcase_printf("MM_VERSION returned %d.%d\n" \
105 "The required version is %d.%d\n", \
106 version >> MM_VERSION_MAJOR_SHIFT, \
107 version & MM_VERSION_MINOR_MASK, \
108 major, minor); \
109 return TEST_RESULT_SKIPPED; \
110 } \
111 \
112 VERBOSE("MM_VERSION returned %d.%d\n", \
113 version >> MM_VERSION_MAJOR_SHIFT, \
114 version & MM_VERSION_MINOR_MASK); \
115 } while (0)
116
Antonio Nino Diaz652d20a2018-12-10 17:17:33 +0000117#define SKIP_TEST_IF_SPCI_VERSION_LESS_THAN(major, minor) \
118 do { \
119 smc_args version_smc = { SPCI_VERSION }; \
120 smc_ret_values smc_ret = tftf_smc(&version_smc); \
121 uint32_t version = smc_ret.ret0; \
122 \
123 if (version == SMC_UNKNOWN) { \
124 tftf_testcase_printf("SPM not detected.\n"); \
125 return TEST_RESULT_SKIPPED; \
126 } \
127 \
128 if (version < SPCI_VERSION_FORM(major, minor)) { \
129 tftf_testcase_printf("SPCI_VERSION returned %d.%d\n" \
130 "The required version is %d.%d\n", \
131 version >> SPCI_VERSION_MAJOR_SHIFT,\
132 version & SPCI_VERSION_MINOR_MASK, \
133 major, minor); \
134 return TEST_RESULT_SKIPPED; \
135 } \
136 } while (0)
137
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200138/* Helper macro to verify if system suspend API is supported */
139#define is_psci_sys_susp_supported() \
140 (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \
141 == PSCI_E_SUCCESS)
142
143/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
144#define is_psci_stat_count_supported() \
145 (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \
146 == PSCI_E_SUCCESS)
147
148/*
149 * Helper function to verify the system state is ready for system
150 * suspend. i.e., a single CPU is running and all other CPUs are powered off.
151 * Returns 1 if the system is ready to suspend, 0 otherwise.
152 */
153int is_sys_suspend_state_ready(void);
154
155/*
156 * Helper function to reset the system. This function shouldn't return.
157 * It is not marked with __dead to help the test to catch some error in
158 * TF
159 */
160void psci_system_reset(void);
161
162/*
163 * Helper function that enables/disables the mem_protect mechanism
164 */
165int psci_mem_protect(int val);
166
167
168/*
169 * Helper function to call PSCI MEM_PROTECT_CHECK
170 */
171int psci_mem_protect_check(uintptr_t addr, size_t size);
172
173
174/*
175 * Helper function to get a sentinel address that can be used to test mem_protect
176 */
177unsigned char *psci_mem_prot_get_sentinel(void);
178
179/*
180 * Helper function to memory map and unmap a region needed by a test.
181 *
182 * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
183 * unmapped. Otherwise, return the test functions's result.
184 */
185test_result_t map_test_unmap(const map_args_unmap_t *args,
186 test_function_arg_t test);
187
188#endif /* __TEST_HELPERS_H__ */