Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
AlexeiFedorov | 2f30f10 | 2023-03-13 19:37:46 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2020-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 | |
AlexeiFedorov | 2f30f10 | 2023-03-13 19:37:46 +0000 | [diff] [blame^] | 7 | #include <stdlib.h> |
| 8 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 9 | #include <arch_helpers.h> |
J-Alves | 952e1f7 | 2021-07-30 17:19:09 +0100 | [diff] [blame] | 10 | #include <cactus_test_cmds.h> |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 11 | #include <plat_topology.h> |
| 12 | #include <platform.h> |
| 13 | #include <power_management.h> |
| 14 | #include <test_helpers.h> |
| 15 | #include <tftf_lib.h> |
| 16 | |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 17 | static struct mailbox_buffers test_mb = {.send = NULL, .recv = NULL}; |
| 18 | |
Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 19 | int is_sys_suspend_state_ready(void) |
| 20 | { |
| 21 | int aff_info; |
| 22 | unsigned int target_node; |
| 23 | u_register_t target_mpid; |
| 24 | u_register_t current_mpid = read_mpidr_el1() & MPID_MASK; |
| 25 | |
| 26 | for_each_cpu(target_node) { |
| 27 | target_mpid = tftf_get_mpidr_from_node(target_node); |
| 28 | |
| 29 | /* Skip current CPU, as it is powered on */ |
| 30 | if (target_mpid == current_mpid) |
| 31 | continue; |
| 32 | |
| 33 | aff_info = tftf_psci_affinity_info(target_mpid, MPIDR_AFFLVL0); |
| 34 | if (aff_info != PSCI_STATE_OFF) |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | return 1; |
| 39 | } |
| 40 | |
| 41 | void psci_system_reset(void) |
| 42 | { |
| 43 | smc_args args = { SMC_PSCI_SYSTEM_RESET }; |
| 44 | smc_ret_values ret; |
| 45 | |
| 46 | ret = tftf_smc(&args); |
| 47 | |
| 48 | /* The PSCI SYSTEM_RESET call is not supposed to return */ |
| 49 | tftf_testcase_printf("System didn't reboot properly (%d)\n", |
| 50 | (unsigned int)ret.ret0); |
| 51 | } |
| 52 | |
| 53 | int psci_mem_protect(int val) |
| 54 | { |
| 55 | smc_args args = { SMC_PSCI_MEM_PROTECT}; |
| 56 | smc_ret_values ret; |
| 57 | |
| 58 | args.arg1 = val; |
| 59 | ret = tftf_smc(&args); |
| 60 | |
| 61 | return ret.ret0; |
| 62 | } |
| 63 | |
| 64 | int psci_mem_protect_check(uintptr_t addr, size_t size) |
| 65 | { |
| 66 | smc_args args = { SMC_PSCI_MEM_PROTECT_CHECK }; |
| 67 | smc_ret_values ret; |
| 68 | |
| 69 | args.arg1 = addr; |
| 70 | args.arg2 = size; |
| 71 | ret = tftf_smc(&args); |
| 72 | return ret.ret0; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * This function returns an address that can be used as |
| 77 | * sentinel for mem_protect functions. The logic behind |
| 78 | * it is that it has to search one region that doesn't intersect |
| 79 | * with the memory used by TFTF. |
| 80 | */ |
| 81 | unsigned char *psci_mem_prot_get_sentinel(void) |
| 82 | { |
| 83 | const mem_region_t *ranges, *rp, *lim; |
| 84 | int nranges; |
| 85 | IMPORT_SYM(uintptr_t, __TFTF_BASE__, tftf_base); |
| 86 | IMPORT_SYM(uintptr_t, __TFTF_END__, tftf_end); |
| 87 | uintptr_t p = 0; |
| 88 | |
| 89 | ranges = plat_get_prot_regions(&nranges); |
| 90 | if (!ranges) |
| 91 | return NULL; |
| 92 | |
| 93 | lim = &ranges[nranges]; |
| 94 | for (rp = ranges ; rp < lim; rp++) { |
| 95 | p = rp->addr; |
| 96 | if (p < tftf_base || p > tftf_end) |
| 97 | break; |
| 98 | p = p + (rp->size - 1); |
| 99 | if (p < tftf_base || p > tftf_end) |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return (rp == lim) ? NULL : (unsigned char *) p; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This function maps the memory region before the |
| 108 | * test and unmap it after the test is run |
| 109 | */ |
| 110 | test_result_t map_test_unmap(const map_args_unmap_t *args, |
| 111 | test_function_arg_t test) |
| 112 | { |
| 113 | int mmap_ret; |
| 114 | test_result_t test_ret; |
| 115 | |
| 116 | mmap_ret = mmap_add_dynamic_region(args->addr, args->addr, |
| 117 | args->size, args->attr); |
| 118 | |
| 119 | if (mmap_ret != 0) { |
| 120 | tftf_testcase_printf("Couldn't map memory (ret = %d)\n", |
| 121 | mmap_ret); |
| 122 | return TEST_RESULT_FAIL; |
| 123 | } |
| 124 | |
| 125 | test_ret = (*test)(args->arg); |
| 126 | |
| 127 | mmap_ret = mmap_remove_dynamic_region(args->addr, args->size); |
| 128 | if (mmap_ret != 0) { |
| 129 | tftf_testcase_printf("Couldn't unmap memory (ret = %d)\n", |
| 130 | mmap_ret); |
| 131 | return TEST_RESULT_FAIL; |
| 132 | } |
| 133 | |
| 134 | return test_ret; |
| 135 | } |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 136 | |
J-Alves | 08b7860 | 2023-01-24 15:54:50 +0000 | [diff] [blame] | 137 | bool reset_tftf_mailbox(void) |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 138 | { |
J-Alves | 08b7860 | 2023-01-24 15:54:50 +0000 | [diff] [blame] | 139 | if (is_ffa_call_error(ffa_rxtx_unmap())) { |
| 140 | return false; |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 141 | } |
J-Alves | 08b7860 | 2023-01-24 15:54:50 +0000 | [diff] [blame] | 142 | |
| 143 | test_mb.send = NULL; |
| 144 | test_mb.recv = NULL; |
| 145 | |
| 146 | return true; |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool get_tftf_mailbox(struct mailbox_buffers *mb) |
| 150 | { |
J-Alves | 08b7860 | 2023-01-24 15:54:50 +0000 | [diff] [blame] | 151 | struct ffa_value ret; |
| 152 | |
| 153 | if (test_mb.recv == NULL || test_mb.send == NULL) { |
| 154 | CONFIGURE_AND_MAP_MAILBOX(test_mb, PAGE_SIZE, ret); |
| 155 | if (is_ffa_call_error(ret)) { |
| 156 | return false; |
| 157 | } |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 158 | } |
J-Alves | 08b7860 | 2023-01-24 15:54:50 +0000 | [diff] [blame] | 159 | |
| 160 | *mb = test_mb; |
| 161 | |
| 162 | return true; |
J-Alves | f1126f2 | 2020-11-02 17:28:20 +0000 | [diff] [blame] | 163 | } |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 164 | |
J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 165 | test_result_t check_spmc_testing_set_up( |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 166 | uint32_t ffa_version_major, uint32_t ffa_version_minor, |
| 167 | const struct ffa_uuid *ffa_uuids, size_t ffa_uuids_size) |
| 168 | { |
| 169 | struct mailbox_buffers mb; |
| 170 | |
| 171 | if (ffa_uuids == NULL) { |
| 172 | ERROR("Invalid parameter ffa_uuids!\n"); |
| 173 | return TEST_RESULT_FAIL; |
| 174 | } |
| 175 | |
| 176 | SKIP_TEST_IF_FFA_VERSION_LESS_THAN(ffa_version_major, |
| 177 | ffa_version_minor); |
| 178 | |
| 179 | /********************************************************************** |
| 180 | * If OP-TEE is SPMC skip the current test. |
| 181 | **********************************************************************/ |
| 182 | if (check_spmc_execution_level()) { |
| 183 | VERBOSE("OPTEE as SPMC at S-EL1. Skipping test!\n"); |
| 184 | return TEST_RESULT_SKIPPED; |
| 185 | } |
| 186 | |
| 187 | GET_TFTF_MAILBOX(mb); |
| 188 | |
| 189 | for (unsigned int i = 0U; i < ffa_uuids_size; i++) |
Max Shvetsov | 0b7d25f | 2021-03-05 13:46:42 +0000 | [diff] [blame] | 190 | SKIP_TEST_IF_FFA_ENDPOINT_NOT_DEPLOYED(*mb, ffa_uuids[i]); |
J-Alves | d708c03 | 2020-11-19 12:14:21 +0000 | [diff] [blame] | 191 | |
| 192 | return TEST_RESULT_SUCCESS; |
| 193 | } |
J-Alves | d56c53c | 2021-07-01 16:32:16 +0100 | [diff] [blame] | 194 | |
| 195 | test_result_t spm_run_multi_core_test(uintptr_t cpu_on_handler, |
| 196 | event_t *cpu_done) |
| 197 | { |
| 198 | unsigned int lead_mpid = read_mpidr_el1() & MPID_MASK; |
| 199 | unsigned int core_pos, cpu_node, mpidr; |
| 200 | int32_t ret; |
| 201 | |
| 202 | VERBOSE("Powering on all cpus.\n"); |
| 203 | |
| 204 | for (unsigned int i = 0U; i < PLATFORM_CORE_COUNT; i++) { |
| 205 | tftf_init_event(&cpu_done[i]); |
| 206 | } |
| 207 | |
Madhukar Pappireddy | 1632b4a | 2022-09-29 09:54:09 -0500 | [diff] [blame] | 208 | /* Power on each secondary CPU one after the other. */ |
J-Alves | d56c53c | 2021-07-01 16:32:16 +0100 | [diff] [blame] | 209 | for_each_cpu(cpu_node) { |
| 210 | mpidr = tftf_get_mpidr_from_node(cpu_node); |
| 211 | if (mpidr == lead_mpid) { |
| 212 | continue; |
| 213 | } |
| 214 | |
| 215 | ret = tftf_cpu_on(mpidr, cpu_on_handler, 0U); |
| 216 | if (ret != 0) { |
| 217 | ERROR("tftf_cpu_on mpidr 0x%x returns %d\n", |
| 218 | mpidr, ret); |
| 219 | } |
J-Alves | d56c53c | 2021-07-01 16:32:16 +0100 | [diff] [blame] | 220 | |
Madhukar Pappireddy | 1632b4a | 2022-09-29 09:54:09 -0500 | [diff] [blame] | 221 | /* Wait for the secondary CPU to be ready. */ |
J-Alves | d56c53c | 2021-07-01 16:32:16 +0100 | [diff] [blame] | 222 | core_pos = platform_get_core_pos(mpidr); |
| 223 | tftf_wait_for_event(&cpu_done[core_pos]); |
| 224 | } |
| 225 | |
| 226 | VERBOSE("Done exiting.\n"); |
| 227 | |
| 228 | return TEST_RESULT_SUCCESS; |
| 229 | } |
J-Alves | 952e1f7 | 2021-07-30 17:19:09 +0100 | [diff] [blame] | 230 | |
J-Alves | 79c08f1 | 2021-10-27 15:15:16 +0100 | [diff] [blame] | 231 | bool spm_core_sp_init(ffa_id_t sp_id) |
| 232 | { |
| 233 | /* |
| 234 | * Secure Partitions secondary ECs need one round of ffa_run to reach |
| 235 | * the message loop. |
| 236 | */ |
| 237 | if (sp_id != SP_ID(1)) { |
| 238 | uint32_t core_pos = get_current_core_id(); |
Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 239 | struct ffa_value ret = ffa_run(sp_id, core_pos); |
J-Alves | 79c08f1 | 2021-10-27 15:15:16 +0100 | [diff] [blame] | 240 | |
| 241 | if (ffa_func_id(ret) != FFA_MSG_WAIT) { |
| 242 | ERROR("Failed to run SP%x on core %u\n", |
| 243 | sp_id, core_pos); |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
nabkah01 | d5c67b8 | 2022-03-22 22:54:23 +0000 | [diff] [blame] | 251 | /* |
nabkah01 | 9ea1664 | 2022-03-01 19:39:59 +0000 | [diff] [blame] | 252 | * Utility function to wait for all CPUs other than the caller to be |
| 253 | * OFF. |
| 254 | */ |
| 255 | void wait_for_non_lead_cpus(void) |
| 256 | { |
| 257 | unsigned int target_mpid, target_node; |
| 258 | |
| 259 | for_each_cpu(target_node) { |
| 260 | target_mpid = tftf_get_mpidr_from_node(target_node); |
| 261 | wait_for_core_to_turn_off(target_mpid); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | void wait_for_core_to_turn_off(unsigned int mpidr) |
| 266 | { |
| 267 | /* Skip lead CPU, as it is powered on */ |
| 268 | if (mpidr == (read_mpidr_el1() & MPID_MASK)) |
| 269 | return; |
| 270 | |
| 271 | while (tftf_psci_affinity_info(mpidr, MPIDR_AFFLVL0) != PSCI_STATE_OFF) { |
| 272 | continue; |
| 273 | } |
| 274 | } |
AlexeiFedorov | 2f30f10 | 2023-03-13 19:37:46 +0000 | [diff] [blame^] | 275 | |
| 276 | /* Generate 64-bit random number */ |
| 277 | unsigned long long rand64(void) |
| 278 | { |
| 279 | return ((unsigned long long)rand() << 32) | rand(); |
| 280 | } |