Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
Arunachalam Ganapathy | 417edca | 2023-09-05 17:44:24 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2023, Arm Limited. All rights reserved. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 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> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 16 | |
| 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 | */ |
| 21 | typedef 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 Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 38 | #define TEST_RESULT_IS_VALID(result) \ |
| 39 | ((result >= TEST_RESULT_MIN) && (result < TEST_RESULT_MAX)) |
| 40 | |
Jimmy Brisson | 16de810 | 2023-08-18 08:50:30 -0500 | [diff] [blame] | 41 | #define TEST_ASSERT(must_be_true) \ |
| 42 | do { \ |
| 43 | if (!(must_be_true)) { \ |
| 44 | tftf_testcase_printf("Failed at %s:%d\n", __FILE__, __LINE__); \ |
| 45 | return TEST_RESULT_FAIL;\ |
| 46 | } \ |
| 47 | } while (0) |
| 48 | |
| 49 | #define TEST_ASSERT_SKIP(must_be_true) \ |
| 50 | do { \ |
| 51 | if (!(must_be_true)) { \ |
| 52 | tftf_testcase_printf("Failed at %s:%d\n", __FILE__, __LINE__); \ |
| 53 | return TEST_RESULT_SKIPPED;\ |
| 54 | } \ |
| 55 | } while (0) |
| 56 | |
| 57 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 58 | /* |
| 59 | * PSCI Function Wrappers |
| 60 | * |
| 61 | * SMC calls to PSCI functions |
| 62 | */ |
| 63 | int32_t tftf_psci_cpu_on(u_register_t target_cpu, |
| 64 | uintptr_t entry_point_address, |
| 65 | u_register_t context_id); |
| 66 | int32_t tftf_psci_cpu_off(void); |
Wing Li | cb88add | 2022-10-29 02:32:06 +0100 | [diff] [blame] | 67 | int32_t tftf_psci_set_suspend_mode(uint32_t mode); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 68 | int32_t tftf_psci_affinity_info(u_register_t target_affinity, |
| 69 | uint32_t lowest_affinity_level); |
| 70 | int32_t tftf_psci_node_hw_state(u_register_t target_cpu, uint32_t power_level); |
| 71 | int32_t tftf_get_psci_feature_info(uint32_t psci_func_id); |
| 72 | u_register_t tftf_psci_stat_count(u_register_t target_cpu, |
| 73 | uint32_t power_state); |
| 74 | u_register_t tftf_psci_stat_residency(u_register_t target_cpu, |
| 75 | uint32_t power_state); |
| 76 | |
| 77 | /* |
| 78 | * PSCI Helper functions |
| 79 | */ |
| 80 | |
| 81 | /* |
| 82 | * Gets the context ID used when calling tftf_psci_cpu_on(). |
| 83 | */ |
| 84 | u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos); |
| 85 | |
| 86 | /* |
| 87 | * Sets the context ID used when calling tftf_psci_cpu_on(). |
| 88 | */ |
| 89 | void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id); |
| 90 | |
| 91 | /* |
| 92 | * Gets the PSCI version of Trusted Firmware-A. The version number returned |
| 93 | * is a 32-bit unsigned integer, with the upper 16 bits denoting the major |
| 94 | * revision, and the lower 16 bits denoting the minor revision. |
| 95 | */ |
| 96 | unsigned int tftf_get_psci_version(void); |
| 97 | |
| 98 | /* |
| 99 | * Returns 0 if version is not a valid PSCI version supported by TFTF. |
| 100 | * Otherwise it returns a value different of 0. |
| 101 | */ |
| 102 | int tftf_is_valid_psci_version(unsigned int version); |
| 103 | |
| 104 | |
| 105 | /* |
| 106 | * The function constructs a composite state_id up-to the specified |
| 107 | * affinity level querying the relevant state property from the platform. |
| 108 | * It chooses the first matching state property from the array returned |
| 109 | * by platform. In case the requested affinity level is not supported by |
| 110 | * the platform, then this function uses DUMMY_STATE_ID as the local state |
| 111 | * for that level. This allows the tests to construct composite state-id |
| 112 | * for invalid affinity levels as well. It returns the expected return |
| 113 | * value from CPU SUSPEND call. |
| 114 | */ |
| 115 | int tftf_psci_make_composite_state_id(uint32_t affinity_level, |
| 116 | uint32_t state_type, uint32_t *state_id); |
| 117 | |
| 118 | /* |
| 119 | * This function composes the power state parameter in the right format |
| 120 | * needed by PSCI. The detection of the power state format is done during |
| 121 | * cold boot by tftf_detect_psci_pstate_format() function. |
| 122 | */ |
| 123 | uint32_t tftf_make_psci_pstate(uint32_t affinity_level, |
| 124 | uint32_t state_type, |
| 125 | uint32_t state_id); |
| 126 | |
| 127 | /* |
| 128 | * Returns 1, if the EL3 software supports PSCI's original format state ID as |
| 129 | * NULL else returns zero |
| 130 | */ |
| 131 | unsigned int tftf_is_psci_state_id_null(void); |
| 132 | |
| 133 | /* |
| 134 | * Returns 1, if the EL3 software supports PSCI's original state format else |
| 135 | * returns zero |
| 136 | */ |
| 137 | unsigned int tftf_is_psci_pstate_format_original(void); |
| 138 | |
| 139 | /* Functions to wait for a specified number of ms or us */ |
| 140 | void waitms(uint64_t ms); |
| 141 | void waitus(uint64_t us); |
| 142 | |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 143 | /* Define fields in common for smc_args and smc_args_ext */ |
| 144 | #define FID_COMMON_ARGS() \ |
| 145 | /* \ |
| 146 | * Function identifier. Identifies which function is being \ |
| 147 | * invoked. \ |
| 148 | */ \ |
| 149 | uint32_t fid; \ |
| 150 | \ |
| 151 | u_register_t arg1; \ |
| 152 | u_register_t arg2; \ |
| 153 | u_register_t arg3; \ |
| 154 | u_register_t arg4; \ |
| 155 | u_register_t arg5; \ |
| 156 | u_register_t arg6; \ |
| 157 | u_register_t arg7; |
| 158 | |
| 159 | /* Define fields in common for smc_ret_values and smc_ret_values_ext */ |
| 160 | #define COMMON_RETVALS() \ |
| 161 | u_register_t ret0; \ |
| 162 | u_register_t ret1; \ |
| 163 | u_register_t ret2; \ |
| 164 | u_register_t ret3; \ |
| 165 | u_register_t ret4; \ |
| 166 | u_register_t ret5; \ |
| 167 | u_register_t ret6; \ |
| 168 | u_register_t ret7; |
| 169 | |
| 170 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 171 | /* |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 172 | * SMC calls take a function identifier and up to 7 arguments if using x8 |
| 173 | * as an address pointing to a structure where return values are stored. |
Madhukar Pappireddy | c783c0b | 2019-11-14 23:52:37 -0600 | [diff] [blame] | 174 | * Additionally, few SMC calls that originate from EL2 leverage the seventh |
| 175 | * argument explicitly. Given that TFTF runs in EL2, we need to be able to |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 176 | * specify it. |
| 177 | */ |
| 178 | typedef struct { |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 179 | FID_COMMON_ARGS() |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 180 | } smc_args; |
| 181 | |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 182 | /* |
| 183 | * If x8 is not used as an address pointing to a structure where the return |
| 184 | * values are stored, SMC calls take up to 17 arguments. |
| 185 | */ |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 186 | typedef struct { |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 187 | FID_COMMON_ARGS() |
| 188 | u_register_t arg8; |
| 189 | u_register_t arg9; |
| 190 | u_register_t arg10; |
| 191 | u_register_t arg11; |
| 192 | u_register_t arg12; |
| 193 | u_register_t arg13; |
| 194 | u_register_t arg14; |
| 195 | u_register_t arg15; |
| 196 | u_register_t arg16; |
| 197 | u_register_t arg17; |
| 198 | } smc_args_ext; |
| 199 | |
| 200 | /* |
| 201 | * SMC calls can return up to 8 register values if x8 is used as an address |
| 202 | * pointing to a structure where the return values are stored. |
| 203 | */ |
| 204 | typedef struct { |
| 205 | COMMON_RETVALS() |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 206 | } smc_ret_values; |
| 207 | |
| 208 | /* |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 209 | * If x8 is not used as an address pointing to a structure where the return |
| 210 | * values are stored, SMC calls return up to 18 register values. |
| 211 | */ |
| 212 | typedef struct { |
| 213 | COMMON_RETVALS() |
| 214 | u_register_t ret8; |
| 215 | u_register_t ret9; |
| 216 | u_register_t ret10; |
| 217 | u_register_t ret11; |
| 218 | u_register_t ret12; |
| 219 | u_register_t ret13; |
| 220 | u_register_t ret14; |
| 221 | u_register_t ret15; |
| 222 | u_register_t ret16; |
| 223 | u_register_t ret17; |
| 224 | } smc_ret_values_ext; |
| 225 | |
| 226 | /* |
| 227 | * Trigger an SMC call. Return values are stored in structure pointed by address |
| 228 | * stored in x8. |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 229 | */ |
| 230 | smc_ret_values tftf_smc(const smc_args *args); |
| 231 | |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 232 | /* |
| 233 | * Trigger an SMC call. Return values are stored in structure pointed by 'ret' |
| 234 | */ |
| 235 | void tftf_smc_no_retval_x8(const smc_args_ext *args, smc_ret_values_ext *ret); |
| 236 | |
Arunachalam Ganapathy | 417edca | 2023-09-05 17:44:24 +0100 | [diff] [blame] | 237 | /* Assembler routine to trigger a SMC call. */ |
| 238 | smc_ret_values asm_tftf_smc64(uint32_t fid, u_register_t arg1, u_register_t arg2, |
| 239 | u_register_t arg3, u_register_t arg4, |
| 240 | u_register_t arg5, u_register_t arg6, |
| 241 | u_register_t arg7); |
| 242 | |
Juan Pablo Conde | c79338a | 2024-10-28 18:48:25 -0500 | [diff] [blame] | 243 | /* Assembler routine to trigger a SMC call without smc_ret_values in x8. */ |
| 244 | u_register_t asm_tftf_smc64_no_retval_x8(const smc_args_ext *args, |
| 245 | smc_ret_values_ext *ret); |
Arunachalam Ganapathy | 417edca | 2023-09-05 17:44:24 +0100 | [diff] [blame] | 246 | /* |
| 247 | * Update the SVE hint for the current CPU. Any SMC call made through tftf_smc |
| 248 | * will update the SVE hint bit in the SMC Function ID. |
| 249 | */ |
| 250 | void tftf_smc_set_sve_hint(bool sve_hint_flag); |
| 251 | |
| 252 | /* Return the SVE hint bit value for the current CPU */ |
| 253 | bool tftf_smc_get_sve_hint(void); |
| 254 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 255 | /* |
Olivier Deprez | 3e1a295 | 2020-03-12 15:44:17 +0100 | [diff] [blame] | 256 | * Trigger an HVC call. |
| 257 | */ |
| 258 | typedef smc_args hvc_args; |
| 259 | |
| 260 | typedef smc_ret_values hvc_ret_values; |
| 261 | |
| 262 | hvc_ret_values tftf_hvc(const hvc_args *args); |
| 263 | |
| 264 | /* |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 265 | * Write a formatted string in the test output buffer. |
| 266 | * Just like the standard libc's printf() function, the string produced is under |
| 267 | * the control of a format string that specifies how subsequent arguments are |
| 268 | * converted. |
| 269 | * |
| 270 | * The string will appear in the test report. |
| 271 | * Use mp_printf() instead for volatile debug messages that are not meant to be |
| 272 | * stored into the test report. |
| 273 | * Note: The test output buffer referred here is a temporary buffer stored in |
| 274 | * RAM. This function doesn't write anything into NVM. |
| 275 | * |
| 276 | * Upon successful return, return the number of characters printed (not |
| 277 | * including the final '\0' character). If an output error is encountered, |
| 278 | * a negative value is returned. If the function is not able to print any |
| 279 | * character at all, this is considered as an output error. Note that a partial |
| 280 | * write (i.e. when the string is truncated) is not considered as an output |
| 281 | * error. |
| 282 | */ |
| 283 | __attribute__((format(printf, 1, 2))) |
| 284 | int tftf_testcase_printf(const char *format, ...); |
| 285 | |
| 286 | /* |
| 287 | * This function is meant to be used by tests. |
| 288 | * It tells the framework that the test is going to reset the platform. |
| 289 | * |
| 290 | * It the test omits to call this function before resetting, the framework will |
| 291 | * consider the test has crashed upon resumption. |
| 292 | */ |
| 293 | void tftf_notify_reboot(void); |
| 294 | |
| 295 | /* |
| 296 | * Returns 0 if the test function is executed for the first time, |
| 297 | * or 1 if the test rebooted the platform and the test function is being |
| 298 | * executed again. |
| 299 | * This function is used for tests that reboot the platform, so that they can |
| 300 | * execute different code paths on 1st execution and subsequent executions. |
| 301 | */ |
| 302 | unsigned int tftf_is_rebooted(void); |
| 303 | |
| 304 | static inline unsigned int make_mpid(unsigned int clusterid, |
Javier Almansa Sobrino | 109d5c1 | 2020-09-04 12:52:23 +0100 | [diff] [blame] | 305 | #if PLAT_MAX_PE_PER_CPU > 1 |
Alexei Fedorov | 0f30547 | 2019-10-29 14:06:54 +0000 | [diff] [blame] | 306 | unsigned int coreid, |
| 307 | unsigned int threadid) |
| 308 | #else |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 309 | unsigned int coreid) |
Alexei Fedorov | 0f30547 | 2019-10-29 14:06:54 +0000 | [diff] [blame] | 310 | #endif |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 311 | { |
| 312 | /* |
| 313 | * If MT bit is set then need to shift the affinities and also set the |
| 314 | * MT bit. |
| 315 | */ |
| 316 | if ((read_mpidr_el1() & MPIDR_MT_MASK) != 0) |
| 317 | return MPIDR_MT_MASK | |
Javier Almansa Sobrino | 109d5c1 | 2020-09-04 12:52:23 +0100 | [diff] [blame] | 318 | #if PLAT_MAX_PE_PER_CPU > 1 |
Alexei Fedorov | 0f30547 | 2019-10-29 14:06:54 +0000 | [diff] [blame] | 319 | ((threadid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT) | |
| 320 | #endif |
| 321 | ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT) | |
| 322 | ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF2_SHIFT); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 323 | else |
Alexei Fedorov | 0f30547 | 2019-10-29 14:06:54 +0000 | [diff] [blame] | 324 | return ((coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT) | |
| 325 | ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT); |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 326 | |
| 327 | } |
| 328 | |
| 329 | #endif /* __ASSEMBLY__ */ |
| 330 | #endif /* __TFTF_LIB_H__ */ |