/* * Copyright (c) 2014-2016, ARM Limited and Contributors. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of ARM nor the names of its contributors may be used * to endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #ifndef __TFTF_LIB_H__ #define __TFTF_LIB_H__ #ifndef __ASSEMBLY__ #include #include #include #include #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) /* * Possible error codes for signaling the result of a test * TEST_RESULT_MIN and TEST_RESULT_MAX are only used as bounds in the enum. */ typedef enum { /* * NA = Not applicable. * Initial value for a test result. * Used for CPUs that don't participate in the test. */ TEST_RESULT_NA = -1, TEST_RESULT_MIN = 0, TEST_RESULT_SKIPPED = TEST_RESULT_MIN, TEST_RESULT_SUCCESS, TEST_RESULT_FAIL, TEST_RESULT_CRASHED, TEST_RESULT_MAX } test_result_t; #define TEST_RESULT_IS_VALID(result) \ ((result >= TEST_RESULT_MIN) && (result < TEST_RESULT_MAX)) /* * PSCI Function Wrappers * * SMC calls to PSCI functions */ int32_t tftf_psci_cpu_on(u_register_t target_cpu, uintptr_t entry_point_address, u_register_t context_id); int32_t tftf_psci_cpu_off(void); int32_t tftf_psci_affinity_info(u_register_t target_affinity, uint32_t lowest_affinity_level); int32_t tftf_psci_node_hw_state(u_register_t target_cpu, uint32_t power_level); int32_t tftf_get_psci_feature_info(uint32_t psci_func_id); u_register_t tftf_psci_stat_count(u_register_t target_cpu, uint32_t power_state); u_register_t tftf_psci_stat_residency(u_register_t target_cpu, uint32_t power_state); /* * PSCI Helper functions */ /* * Gets the context ID used when calling tftf_psci_cpu_on(). */ u_register_t tftf_get_cpu_on_ctx_id(unsigned int core_pos); /* * Sets the context ID used when calling tftf_psci_cpu_on(). */ void tftf_set_cpu_on_ctx_id(unsigned int core_pos, u_register_t context_id); /* * Gets the PSCI version of Trusted Firmware. The version number returned * is a 32-bit unsigned integer, with the upper 16 bits denoting the major * revision, and the lower 16 bits denoting the minor revision. */ unsigned int tftf_get_psci_version(void); /* * The function constructs a composite state_id up-to the specified * affinity level querying the relevant state property from the platform. * It chooses the first matching state property from the array returned * by platform. In case the requested affinity level is not supported by * the platform, then this function uses DUMMY_STATE_ID as the local state * for that level. This allows the tests to construct composite state-id * for invalid affinity levels as well. It returns the expected return * value from CPU SUSPEND call. */ int tftf_psci_make_composite_state_id(uint32_t affinity_level, uint32_t state_type, uint32_t *state_id); /* * This function composes the power state parameter in the right format * needed by PSCI. The detection of the power state format is done during * cold boot by tftf_detect_psci_pstate_format() function. */ uint32_t tftf_make_psci_pstate(uint32_t affinity_level, uint32_t state_type, uint32_t state_id); /* * Returns 1, if the EL3 software supports PSCI's original format state ID as * NULL else returns zero */ unsigned int tftf_is_psci_state_id_null(void); /* * Returns 1, if the EL3 software supports PSCI's original state format else * returns zero */ unsigned int tftf_is_psci_pstate_format_original(void); void waitms(uint64_t ms); /* SMC64 calls can take up to 7 64-bit arguments */ typedef struct { uint64_t arg0; uint64_t arg1; uint64_t arg2; uint64_t arg3; uint64_t arg4; uint64_t arg5; uint64_t arg6; } smc64_args; /* SMC64 calls can return up to 4 64-bit arguments */ typedef struct { uint64_t ret0; uint64_t ret1; uint64_t ret2; uint64_t ret3; } smc64_ret_values; /* * Trigger an SMC64 call. */ smc64_ret_values tftf_smc64(const smc64_args *args); /* * Write a formatted string in the test output buffer. * Just like the standard libc's printf() function, the string produced is under * the control of a format string that specifies how subsequent arguments are * converted. * * The string will appear in the test report. * Use mp_printf() instead for volatile debug messages that are not meant to be * stored into the test report. * Note: The test output buffer referred here is a temporary buffer stored in * RAM. This function doesn't write anything into NVM. * * Upon successful return, return the number of characters printed (not * including the final '\0' character). If an output error is encountered, * a negative value is returned. If the function is not able to print any * character at all, this is considered as an output error. Note that a partial * write (i.e. when the string is truncated) is not considered as an output * error. */ __attribute__((format(printf, 1, 2))) int tftf_testcase_printf(const char *format, ...); /* * Prevent the compiler from optimising the access to a variable. * Note that this only affects the compiler, this is NOT a run-time * memory barrier. */ #define ACCESS(var) (*(volatile __typeof__(var) *)&(var)) typedef unsigned long long UINTN; #define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \ do { \ unsigned int clusters_cnt; \ clusters_cnt = tftf_get_total_clusters_count(); \ if (clusters_cnt < (n)) { \ tftf_testcase_printf( \ "Need at least %u clusters, only found %u\n", \ (n), clusters_cnt); \ return TEST_RESULT_SKIPPED; \ } \ } while (0) #define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \ do { \ unsigned int cpus_cnt; \ cpus_cnt = tftf_get_total_cpus_count(); \ if (cpus_cnt < (n)) { \ tftf_testcase_printf( \ "Need at least %u CPUs, only found %u\n", \ (n), cpus_cnt); \ return TEST_RESULT_SKIPPED; \ } \ } while (0) /* * This function is meant to be used by tests. * It tells the framework that the test is going to reset the platform. * * It the test omits to call this function before resetting, the framework will * consider the test has crashed upon resumption. */ void tftf_notify_reboot(void); /* * Returns 0 if the test function is executed for the first time, * or 1 if the test rebooted the platform and the test function is being * executed again. * This function is used for tests that reboot the platform, so that they can * execute different code paths on 1st execution and subsequent executions. */ unsigned int tftf_is_rebooted(void); static inline unsigned int make_mpid(unsigned int clusterid, unsigned int coreid) { return ((clusterid & MPIDR_AFFLVL_MASK) << MPIDR_AFF1_SHIFT) | ((coreid & coreid & MPIDR_AFFLVL_MASK) << MPIDR_AFF0_SHIFT); } #endif /* __ASSEMBLY__ */ #endif /* __TFTF_LIB_H__ */