blob: d265bb9b476512b8859d7cdee9fddc1481c08d0a [file] [log] [blame]
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +02001/*
Olivier Deprez3e1a2952020-03-12 15:44:17 +01002 * 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 __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>
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020016
17/*
18 * Possible error codes for signaling the result of a test
19 * TEST_RESULT_MIN and TEST_RESULT_MAX are only used as bounds in the enum.
20 */
21typedef enum {
22 /*
23 * NA = Not applicable.
24 * Initial value for a test result.
25 * Used for CPUs that don't participate in the test.
26 */
27 TEST_RESULT_NA = -1,
28
29 TEST_RESULT_MIN = 0,
30 TEST_RESULT_SKIPPED = TEST_RESULT_MIN,
31 TEST_RESULT_SUCCESS,
32 TEST_RESULT_FAIL,
33 TEST_RESULT_CRASHED,
34
35 TEST_RESULT_MAX
36} test_result_t;
37
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020038#define TEST_RESULT_IS_VALID(result) \
39 ((result >= TEST_RESULT_MIN) && (result < TEST_RESULT_MAX))
40
41/*
42 * PSCI Function Wrappers
43 *
44 * SMC calls to PSCI functions
45 */
46int32_t tftf_psci_cpu_on(u_register_t target_cpu,
47 uintptr_t entry_point_address,
48 u_register_t context_id);
49int32_t tftf_psci_cpu_off(void);
Wing Licb88add2022-10-29 02:32:06 +010050int32_t tftf_psci_set_suspend_mode(uint32_t mode);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +020051int32_t tftf_psci_affinity_info(u_register_t target_affinity,
52 uint32_t lowest_affinity_level);
53int32_t tftf_psci_node_hw_state(u_register_t target_cpu, uint32_t power_level);
54int32_t tftf_get_psci_feature_info(uint32_t psci_func_id);
55u_register_t tftf_psci_stat_count(u_register_t target_cpu,
56 uint32_t power_state);
57u_register_t tftf_psci_stat_residency(u_register_t target_cpu,
58 uint32_t power_state);
59
60/*
61 * PSCI Helper functions
62 */
63
64/*
65 * Gets the context ID used when calling tftf_psci_cpu_on().
66 */
67u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos);
68
69/*
70 * Sets the context ID used when calling tftf_psci_cpu_on().
71 */
72void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id);
73
74/*
75 * Gets the PSCI version of Trusted Firmware-A. The version number returned
76 * is a 32-bit unsigned integer, with the upper 16 bits denoting the major
77 * revision, and the lower 16 bits denoting the minor revision.
78 */
79unsigned int tftf_get_psci_version(void);
80
81/*
82 * Returns 0 if version is not a valid PSCI version supported by TFTF.
83 * Otherwise it returns a value different of 0.
84 */
85int tftf_is_valid_psci_version(unsigned int version);
86
87
88/*
89 * The function constructs a composite state_id up-to the specified
90 * affinity level querying the relevant state property from the platform.
91 * It chooses the first matching state property from the array returned
92 * by platform. In case the requested affinity level is not supported by
93 * the platform, then this function uses DUMMY_STATE_ID as the local state
94 * for that level. This allows the tests to construct composite state-id
95 * for invalid affinity levels as well. It returns the expected return
96 * value from CPU SUSPEND call.
97 */
98int tftf_psci_make_composite_state_id(uint32_t affinity_level,
99 uint32_t state_type, uint32_t *state_id);
100
101/*
102 * This function composes the power state parameter in the right format
103 * needed by PSCI. The detection of the power state format is done during
104 * cold boot by tftf_detect_psci_pstate_format() function.
105 */
106uint32_t tftf_make_psci_pstate(uint32_t affinity_level,
107 uint32_t state_type,
108 uint32_t state_id);
109
110/*
111 * Returns 1, if the EL3 software supports PSCI's original format state ID as
112 * NULL else returns zero
113 */
114unsigned int tftf_is_psci_state_id_null(void);
115
116/*
117 * Returns 1, if the EL3 software supports PSCI's original state format else
118 * returns zero
119 */
120unsigned int tftf_is_psci_pstate_format_original(void);
121
122/* Functions to wait for a specified number of ms or us */
123void waitms(uint64_t ms);
124void waitus(uint64_t us);
125
126/*
Madhukar Pappireddyc783c0b2019-11-14 23:52:37 -0600127 * SMC calls take a function identifier and up to 7 arguments.
128 * Additionally, few SMC calls that originate from EL2 leverage the seventh
129 * argument explicitly. Given that TFTF runs in EL2, we need to be able to
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200130 * specify it.
131 */
132typedef struct {
Sandrine Bailleux13d99f92018-11-16 15:36:08 +0100133 /* Function identifier. Identifies which function is being invoked. */
Sandrine Bailleux17795062018-12-13 16:02:41 +0100134 uint32_t fid;
Sandrine Bailleux13d99f92018-11-16 15:36:08 +0100135
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;
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200143} smc_args;
144
Madhukar Pappireddyc783c0b2019-11-14 23:52:37 -0600145/* SMC calls can return up to 8 register values */
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200146typedef struct {
Sandrine Bailleux13d99f92018-11-16 15:36:08 +0100147 u_register_t ret0;
148 u_register_t ret1;
149 u_register_t ret2;
150 u_register_t ret3;
Madhukar Pappireddyc783c0b2019-11-14 23:52:37 -0600151 u_register_t ret4;
152 u_register_t ret5;
153 u_register_t ret6;
154 u_register_t ret7;
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200155} smc_ret_values;
156
157/*
158 * Trigger an SMC call.
159 */
160smc_ret_values tftf_smc(const smc_args *args);
161
162/*
Olivier Deprez3e1a2952020-03-12 15:44:17 +0100163 * Trigger an HVC call.
164 */
165typedef smc_args hvc_args;
166
167typedef smc_ret_values hvc_ret_values;
168
169hvc_ret_values tftf_hvc(const hvc_args *args);
170
171/*
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200172 * Write a formatted string in the test output buffer.
173 * Just like the standard libc's printf() function, the string produced is under
174 * the control of a format string that specifies how subsequent arguments are
175 * converted.
176 *
177 * The string will appear in the test report.
178 * Use mp_printf() instead for volatile debug messages that are not meant to be
179 * stored into the test report.
180 * Note: The test output buffer referred here is a temporary buffer stored in
181 * RAM. This function doesn't write anything into NVM.
182 *
183 * Upon successful return, return the number of characters printed (not
184 * including the final '\0' character). If an output error is encountered,
185 * a negative value is returned. If the function is not able to print any
186 * character at all, this is considered as an output error. Note that a partial
187 * write (i.e. when the string is truncated) is not considered as an output
188 * error.
189 */
190__attribute__((format(printf, 1, 2)))
191int tftf_testcase_printf(const char *format, ...);
192
193/*
194 * This function is meant to be used by tests.
195 * It tells the framework that the test is going to reset the platform.
196 *
197 * It the test omits to call this function before resetting, the framework will
198 * consider the test has crashed upon resumption.
199 */
200void tftf_notify_reboot(void);
201
202/*
203 * Returns 0 if the test function is executed for the first time,
204 * or 1 if the test rebooted the platform and the test function is being
205 * executed again.
206 * This function is used for tests that reboot the platform, so that they can
207 * execute different code paths on 1st execution and subsequent executions.
208 */
209unsigned int tftf_is_rebooted(void);
210
211static inline unsigned int make_mpid(unsigned int clusterid,
Javier Almansa Sobrino109d5c12020-09-04 12:52:23 +0100212#if PLAT_MAX_PE_PER_CPU > 1
Alexei Fedorov0f305472019-10-29 14:06:54 +0000213 unsigned int coreid,
214 unsigned int threadid)
215#else
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200216 unsigned int coreid)
Alexei Fedorov0f305472019-10-29 14:06:54 +0000217#endif
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200218{
219 /*
220 * If MT bit is set then need to shift the affinities and also set the
221 * MT bit.
222 */
223 if ((read_mpidr_el1() & MPIDR_MT_MASK) != 0)
224 return MPIDR_MT_MASK |
Javier Almansa Sobrino109d5c12020-09-04 12:52:23 +0100225#if PLAT_MAX_PE_PER_CPU > 1
Alexei Fedorov0f305472019-10-29 14:06:54 +0000226 ((threadid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT) |
227#endif
228 ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT) |
229 ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF2_SHIFT);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200230 else
Alexei Fedorov0f305472019-10-29 14:06:54 +0000231 return ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT) |
232 ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT);
Sandrine Bailleux3cd87d72018-10-09 11:12:55 +0200233
234}
235
236#endif /* __ASSEMBLY__ */
237#endif /* __TFTF_LIB_H__ */