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 __TEST_HELPERS_H__ |
| 8 | #define __TEST_HELPERS_H__ |
| 9 | |
| 10 | #include <plat_topology.h> |
| 11 | #include <psci.h> |
| 12 | #include <tftf_lib.h> |
| 13 | #include <trusted_os.h> |
| 14 | #include <tsp.h> |
| 15 | #include <uuid.h> |
| 16 | #include <uuid_utils.h> |
| 17 | |
| 18 | typedef struct { |
| 19 | uintptr_t addr; |
| 20 | size_t size; |
| 21 | unsigned int attr; |
| 22 | void *arg; |
| 23 | } map_args_unmap_t; |
| 24 | |
| 25 | typedef test_result_t (*test_function_arg_t)(void *arg); |
| 26 | |
| 27 | #define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n) \ |
| 28 | do { \ |
| 29 | unsigned int clusters_cnt; \ |
| 30 | clusters_cnt = tftf_get_total_clusters_count(); \ |
| 31 | if (clusters_cnt < (n)) { \ |
| 32 | tftf_testcase_printf( \ |
| 33 | "Need at least %u clusters, only found %u\n", \ |
| 34 | (n), clusters_cnt); \ |
| 35 | return TEST_RESULT_SKIPPED; \ |
| 36 | } \ |
| 37 | } while (0) |
| 38 | |
| 39 | #define SKIP_TEST_IF_LESS_THAN_N_CPUS(n) \ |
| 40 | do { \ |
| 41 | unsigned int cpus_cnt; \ |
| 42 | cpus_cnt = tftf_get_total_cpus_count(); \ |
| 43 | if (cpus_cnt < (n)) { \ |
| 44 | tftf_testcase_printf( \ |
| 45 | "Need at least %u CPUs, only found %u\n", \ |
| 46 | (n), cpus_cnt); \ |
| 47 | return TEST_RESULT_SKIPPED; \ |
| 48 | } \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT() \ |
| 52 | do { \ |
| 53 | uuid_t tos_uuid; \ |
| 54 | \ |
| 55 | if (!is_trusted_os_present(&tos_uuid)) { \ |
| 56 | tftf_testcase_printf("No Trusted OS detected\n"); \ |
| 57 | return TEST_RESULT_SKIPPED; \ |
| 58 | } \ |
| 59 | } while (0) |
| 60 | |
| 61 | #define SKIP_TEST_IF_TSP_NOT_PRESENT() \ |
| 62 | do { \ |
| 63 | uuid_t tos_uuid; \ |
| 64 | char tos_uuid_str[UUID_STR_SIZE]; \ |
| 65 | \ |
| 66 | if (!is_trusted_os_present(&tos_uuid)) { \ |
| 67 | tftf_testcase_printf("No Trusted OS detected\n"); \ |
| 68 | return TEST_RESULT_SKIPPED; \ |
| 69 | } \ |
| 70 | \ |
| 71 | if (!uuid_equal(&tos_uuid, &tsp_uuid)) { \ |
| 72 | tftf_testcase_printf( \ |
| 73 | "Trusted OS is not the TSP, its UUID is: %s\n", \ |
| 74 | uuid_to_str(&tos_uuid, tos_uuid_str)); \ |
| 75 | return TEST_RESULT_SKIPPED; \ |
| 76 | } \ |
| 77 | } while (0) |
| 78 | |
| 79 | #define SKIP_TEST_IF_MM_NOT_PRESENT() \ |
| 80 | do { \ |
| 81 | smc_args version_smc = { MM_VERSION_AARCH32 }; \ |
| 82 | smc_ret_values smc_ret = tftf_smc(&version_smc); \ |
| 83 | uint32_t version = smc_ret.ret0; \ |
| 84 | \ |
| 85 | if (version == (uint32_t) SMC_UNKNOWN) { \ |
| 86 | tftf_testcase_printf("SPM not detected.\n"); \ |
| 87 | return TEST_RESULT_SKIPPED; \ |
| 88 | } \ |
| 89 | } while (0) |
| 90 | |
| 91 | #define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor) \ |
| 92 | do { \ |
| 93 | smc_args version_smc = { MM_VERSION_AARCH32 }; \ |
| 94 | smc_ret_values smc_ret = tftf_smc(&version_smc); \ |
| 95 | uint32_t version = smc_ret.ret0; \ |
| 96 | \ |
| 97 | if (version == (uint32_t) SMC_UNKNOWN) { \ |
| 98 | tftf_testcase_printf("SPM not detected.\n"); \ |
| 99 | return TEST_RESULT_SKIPPED; \ |
| 100 | } \ |
| 101 | \ |
| 102 | if (version < MM_VERSION_FORM(major, minor)) { \ |
| 103 | tftf_testcase_printf("MM_VERSION returned %d.%d\n" \ |
| 104 | "The required version is %d.%d\n", \ |
| 105 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 106 | version & MM_VERSION_MINOR_MASK, \ |
| 107 | major, minor); \ |
| 108 | return TEST_RESULT_SKIPPED; \ |
| 109 | } \ |
| 110 | \ |
| 111 | VERBOSE("MM_VERSION returned %d.%d\n", \ |
| 112 | version >> MM_VERSION_MAJOR_SHIFT, \ |
| 113 | version & MM_VERSION_MINOR_MASK); \ |
| 114 | } while (0) |
| 115 | |
| 116 | /* Helper macro to verify if system suspend API is supported */ |
| 117 | #define is_psci_sys_susp_supported() \ |
| 118 | (tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND) \ |
| 119 | == PSCI_E_SUCCESS) |
| 120 | |
| 121 | /* Helper macro to verify if PSCI_STAT_COUNT API is supported */ |
| 122 | #define is_psci_stat_count_supported() \ |
| 123 | (tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT) \ |
| 124 | == PSCI_E_SUCCESS) |
| 125 | |
| 126 | /* |
| 127 | * Helper function to verify the system state is ready for system |
| 128 | * suspend. i.e., a single CPU is running and all other CPUs are powered off. |
| 129 | * Returns 1 if the system is ready to suspend, 0 otherwise. |
| 130 | */ |
| 131 | int is_sys_suspend_state_ready(void); |
| 132 | |
| 133 | /* |
| 134 | * Helper function to reset the system. This function shouldn't return. |
| 135 | * It is not marked with __dead to help the test to catch some error in |
| 136 | * TF |
| 137 | */ |
| 138 | void psci_system_reset(void); |
| 139 | |
| 140 | /* |
| 141 | * Helper function that enables/disables the mem_protect mechanism |
| 142 | */ |
| 143 | int psci_mem_protect(int val); |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * Helper function to call PSCI MEM_PROTECT_CHECK |
| 148 | */ |
| 149 | int psci_mem_protect_check(uintptr_t addr, size_t size); |
| 150 | |
| 151 | |
| 152 | /* |
| 153 | * Helper function to get a sentinel address that can be used to test mem_protect |
| 154 | */ |
| 155 | unsigned char *psci_mem_prot_get_sentinel(void); |
| 156 | |
| 157 | /* |
| 158 | * Helper function to memory map and unmap a region needed by a test. |
| 159 | * |
| 160 | * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or |
| 161 | * unmapped. Otherwise, return the test functions's result. |
| 162 | */ |
| 163 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 164 | test_function_arg_t test); |
| 165 | |
| 166 | #endif /* __TEST_HELPERS_H__ */ |