blob: e5900b8a17c1277a7d56ee3357859efd36278bf1 [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 __TFTF_LIB_H__
8#define __TFTF_LIB_H__
9
10#ifndef __ASSEMBLY__
11
12#include <arch.h>
13#include <arch_helpers.h>
14#include <stdbool.h>
15#include <stdint.h>
16#include <sys/types.h>
17
18/*
19 * Possible error codes for signaling the result of a test
20 * TEST_RESULT_MIN and TEST_RESULT_MAX are only used as bounds in the enum.
21 */
22typedef enum {
23 /*
24 * NA = Not applicable.
25 * Initial value for a test result.
26 * Used for CPUs that don't participate in the test.
27 */
28 TEST_RESULT_NA = -1,
29
30 TEST_RESULT_MIN = 0,
31 TEST_RESULT_SKIPPED = TEST_RESULT_MIN,
32 TEST_RESULT_SUCCESS,
33 TEST_RESULT_FAIL,
34 TEST_RESULT_CRASHED,
35
36 TEST_RESULT_MAX
37} test_result_t;
38
39const char *test_result_to_string(test_result_t result);
40
41#define TEST_RESULT_IS_VALID(result) \
42 ((result >= TEST_RESULT_MIN) && (result < TEST_RESULT_MAX))
43
44/*
45 * PSCI Function Wrappers
46 *
47 * SMC calls to PSCI functions
48 */
49int32_t tftf_psci_cpu_on(u_register_t target_cpu,
50 uintptr_t entry_point_address,
51 u_register_t context_id);
52int32_t tftf_psci_cpu_off(void);
53int32_t tftf_psci_affinity_info(u_register_t target_affinity,
54 uint32_t lowest_affinity_level);
55int32_t tftf_psci_node_hw_state(u_register_t target_cpu, uint32_t power_level);
56int32_t tftf_get_psci_feature_info(uint32_t psci_func_id);
57u_register_t tftf_psci_stat_count(u_register_t target_cpu,
58 uint32_t power_state);
59u_register_t tftf_psci_stat_residency(u_register_t target_cpu,
60 uint32_t power_state);
61
62/*
63 * PSCI Helper functions
64 */
65
66/*
67 * Gets the context ID used when calling tftf_psci_cpu_on().
68 */
69u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos);
70
71/*
72 * Sets the context ID used when calling tftf_psci_cpu_on().
73 */
74void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id);
75
76/*
77 * Gets the PSCI version of Trusted Firmware-A. The version number returned
78 * is a 32-bit unsigned integer, with the upper 16 bits denoting the major
79 * revision, and the lower 16 bits denoting the minor revision.
80 */
81unsigned int tftf_get_psci_version(void);
82
83/*
84 * Returns 0 if version is not a valid PSCI version supported by TFTF.
85 * Otherwise it returns a value different of 0.
86 */
87int tftf_is_valid_psci_version(unsigned int version);
88
89
90/*
91 * The function constructs a composite state_id up-to the specified
92 * affinity level querying the relevant state property from the platform.
93 * It chooses the first matching state property from the array returned
94 * by platform. In case the requested affinity level is not supported by
95 * the platform, then this function uses DUMMY_STATE_ID as the local state
96 * for that level. This allows the tests to construct composite state-id
97 * for invalid affinity levels as well. It returns the expected return
98 * value from CPU SUSPEND call.
99 */
100int tftf_psci_make_composite_state_id(uint32_t affinity_level,
101 uint32_t state_type, uint32_t *state_id);
102
103/*
104 * This function composes the power state parameter in the right format
105 * needed by PSCI. The detection of the power state format is done during
106 * cold boot by tftf_detect_psci_pstate_format() function.
107 */
108uint32_t tftf_make_psci_pstate(uint32_t affinity_level,
109 uint32_t state_type,
110 uint32_t state_id);
111
112/*
113 * Returns 1, if the EL3 software supports PSCI's original format state ID as
114 * NULL else returns zero
115 */
116unsigned int tftf_is_psci_state_id_null(void);
117
118/*
119 * Returns 1, if the EL3 software supports PSCI's original state format else
120 * returns zero
121 */
122unsigned int tftf_is_psci_pstate_format_original(void);
123
124/* Functions to wait for a specified number of ms or us */
125void waitms(uint64_t ms);
126void waitus(uint64_t us);
127
128/*
129 * SMC calls take a function identifier and up to 6 arguments.
130 * Additionally, for SMC calls that originate from EL2, an optional seventh
131 * argument can be added. Given that TFTF runs in EL2, we need to be able to
132 * specify it.
133 */
134typedef struct {
135 u_register_t arg0;
136 u_register_t arg1;
137 u_register_t arg2;
138 u_register_t arg3;
139 u_register_t arg4;
140 u_register_t arg5;
141 u_register_t arg6;
142 u_register_t arg7;
143} smc_args;
144
145/* SMC calls can return up to 4 register values */
146typedef struct {
147 u_register_t ret0;
148 u_register_t ret1;
149 u_register_t ret2;
150 u_register_t ret3;
151} smc_ret_values;
152
153/*
154 * Trigger an SMC call.
155 */
156smc_ret_values tftf_smc(const smc_args *args);
157
158/*
159 * Write a formatted string in the test output buffer.
160 * Just like the standard libc's printf() function, the string produced is under
161 * the control of a format string that specifies how subsequent arguments are
162 * converted.
163 *
164 * The string will appear in the test report.
165 * Use mp_printf() instead for volatile debug messages that are not meant to be
166 * stored into the test report.
167 * Note: The test output buffer referred here is a temporary buffer stored in
168 * RAM. This function doesn't write anything into NVM.
169 *
170 * Upon successful return, return the number of characters printed (not
171 * including the final '\0' character). If an output error is encountered,
172 * a negative value is returned. If the function is not able to print any
173 * character at all, this is considered as an output error. Note that a partial
174 * write (i.e. when the string is truncated) is not considered as an output
175 * error.
176 */
177__attribute__((format(printf, 1, 2)))
178int tftf_testcase_printf(const char *format, ...);
179
180/*
181 * This function is meant to be used by tests.
182 * It tells the framework that the test is going to reset the platform.
183 *
184 * It the test omits to call this function before resetting, the framework will
185 * consider the test has crashed upon resumption.
186 */
187void tftf_notify_reboot(void);
188
189/*
190 * Returns 0 if the test function is executed for the first time,
191 * or 1 if the test rebooted the platform and the test function is being
192 * executed again.
193 * This function is used for tests that reboot the platform, so that they can
194 * execute different code paths on 1st execution and subsequent executions.
195 */
196unsigned int tftf_is_rebooted(void);
197
198static inline unsigned int make_mpid(unsigned int clusterid,
199 unsigned int coreid)
200{
201 /*
202 * If MT bit is set then need to shift the affinities and also set the
203 * MT bit.
204 */
205 if ((read_mpidr_el1() & MPIDR_MT_MASK) != 0)
206 return MPIDR_MT_MASK |
207 ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF2_SHIFT) |
208 ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT);
209 else
210 return ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT) |
211 ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT);
212
213}
214
215#endif /* __ASSEMBLY__ */
216#endif /* __TFTF_LIB_H__ */