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