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