| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 1 | /* |
| Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2022, Arm Limited. All rights reserved. |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 7 | #include <debug.h> |
| 8 | #include <smccc.h> |
| 9 | |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 10 | #include <arch_helpers.h> |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 11 | #include <cactus_test_cmds.h> |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 12 | #include <ffa_endpoints.h> |
| J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 13 | #include <ffa_svc.h> |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 14 | #include <lib/events.h> |
| 15 | #include <lib/power_management.h> |
| Max Shvetsov | 103e056 | 2021-02-04 16:58:31 +0000 | [diff] [blame] | 16 | #include <platform.h> |
| Daniel Boulby | 82bf339 | 2023-07-28 18:32:27 +0100 | [diff] [blame] | 17 | #include <spm_test_helpers.h> |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 18 | #include <test_helpers.h> |
| 19 | |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 20 | #define ECHO_VAL1 U(0xa0a0a0a0) |
| 21 | #define ECHO_VAL2 U(0xb0b0b0b0) |
| 22 | #define ECHO_VAL3 U(0xc0c0c0c0) |
| 23 | |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 24 | static const struct ffa_uuid expected_sp_uuids[] = { |
| 25 | {PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID} |
| 26 | }; |
| 27 | |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 28 | static event_t cpu_booted[PLATFORM_CORE_COUNT]; |
| 29 | |
| Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 30 | static test_result_t send_cactus_echo_cmd(ffa_id_t sender, |
| 31 | ffa_id_t dest, |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 32 | uint64_t value) |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 33 | { |
| Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 34 | struct ffa_value ret; |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 35 | ret = cactus_echo_send_cmd(sender, dest, value); |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 36 | |
| J-Alves | 43887ec | 2021-02-22 12:21:44 +0000 | [diff] [blame] | 37 | /* |
| 38 | * Return responses may be FFA_MSG_SEND_DIRECT_RESP or FFA_INTERRUPT, |
| 39 | * but only expect the former. Expect SMC32 convention from SP. |
| 40 | */ |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 41 | if (!is_ffa_direct_response(ret)) { |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 42 | return TEST_RESULT_FAIL; |
| 43 | } |
| 44 | |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 45 | if (cactus_get_response(ret) != CACTUS_SUCCESS || |
| 46 | cactus_echo_get_val(ret) != value) { |
| 47 | ERROR("Echo Failed!\n"); |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 48 | return TEST_RESULT_FAIL; |
| 49 | } |
| 50 | |
| 51 | return TEST_RESULT_SUCCESS; |
| 52 | } |
| 53 | |
| J-Alves | 7581c38 | 2020-05-07 18:34:20 +0100 | [diff] [blame] | 54 | test_result_t test_ffa_direct_messaging(void) |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 55 | { |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 56 | test_result_t result; |
| 57 | |
| 58 | /********************************************************************** |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 59 | * Check SPMC has ffa_version and expected FFA endpoints are deployed. |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 60 | **********************************************************************/ |
| J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 61 | CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids); |
| Olivier Deprez | 3f14f5e | 2020-03-25 23:31:37 +0100 | [diff] [blame] | 62 | |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 63 | /********************************************************************** |
| 64 | * Send a message to SP1 through direct messaging |
| 65 | **********************************************************************/ |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 66 | result = send_cactus_echo_cmd(HYP_ID, SP_ID(1), ECHO_VAL1); |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 67 | if (result != TEST_RESULT_SUCCESS) { |
| 68 | return result; |
| 69 | } |
| 70 | |
| 71 | /********************************************************************** |
| 72 | * Send a message to SP2 through direct messaging |
| 73 | **********************************************************************/ |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 74 | result = send_cactus_echo_cmd(HYP_ID, SP_ID(2), ECHO_VAL2); |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 75 | if (result != TEST_RESULT_SUCCESS) { |
| 76 | return result; |
| 77 | } |
| 78 | |
| 79 | /********************************************************************** |
| 80 | * Send a message to SP1 through direct messaging |
| 81 | **********************************************************************/ |
| J-Alves | 4cb9dee | 2021-03-03 13:59:52 +0000 | [diff] [blame] | 82 | result = send_cactus_echo_cmd(HYP_ID, SP_ID(1), ECHO_VAL3); |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 83 | |
| Olivier Deprez | 0be4abe | 2020-08-04 11:26:13 +0200 | [diff] [blame] | 84 | return result; |
| Olivier Deprez | 61be4c1 | 2019-12-06 17:45:07 +0100 | [diff] [blame] | 85 | } |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * The 'send_cactus_req_echo_cmd' sends a CACTUS_REQ_ECHO_CMD to a cactus SP. |
| 89 | * Handling this command, cactus should then send CACTUS_ECHO_CMD to |
| 90 | * the specified SP according to 'echo_dest'. If the CACTUS_ECHO_CMD is resolved |
| 91 | * successfully, cactus will reply to tftf with CACTUS_SUCCESS, or CACTUS_ERROR |
| 92 | * otherwise. |
| 93 | * For the CACTUS_SUCCESS response, the test returns TEST_RESULT_SUCCESS. |
| 94 | */ |
| Daniel Boulby | e79d207 | 2021-03-03 11:34:53 +0000 | [diff] [blame] | 95 | static test_result_t send_cactus_req_echo_cmd(ffa_id_t sender, |
| 96 | ffa_id_t dest, |
| 97 | ffa_id_t echo_dest, |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 98 | uint64_t value) |
| 99 | { |
| Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 100 | struct ffa_value ret; |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 101 | |
| J-Alves | 5339201 | 2020-11-18 14:51:57 +0000 | [diff] [blame] | 102 | ret = cactus_req_echo_send_cmd(sender, dest, echo_dest, value); |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 103 | |
| J-Alves | 06373c5 | 2021-02-11 15:17:42 +0000 | [diff] [blame] | 104 | if (!is_ffa_direct_response(ret)) { |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 105 | return TEST_RESULT_FAIL; |
| 106 | } |
| 107 | |
| J-Alves | 5339201 | 2020-11-18 14:51:57 +0000 | [diff] [blame] | 108 | if (cactus_get_response(ret) == CACTUS_ERROR) { |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 109 | return TEST_RESULT_FAIL; |
| 110 | } |
| 111 | |
| 112 | return TEST_RESULT_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | test_result_t test_ffa_sp_to_sp_direct_messaging(void) |
| 116 | { |
| 117 | test_result_t result; |
| 118 | |
| J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 119 | CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids); |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 120 | |
| 121 | result = send_cactus_req_echo_cmd(HYP_ID, SP_ID(1), SP_ID(2), |
| 122 | ECHO_VAL1); |
| 123 | if (result != TEST_RESULT_SUCCESS) { |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * The following the tests are intended to test the handling of a |
| 129 | * direct message request with a VM's ID as a the sender. |
| 130 | */ |
| J-Alves | d5d8715 | 2021-10-29 11:48:37 +0100 | [diff] [blame] | 131 | result = send_cactus_req_echo_cmd(VM_ID(1), SP_ID(2), SP_ID(3), |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 132 | ECHO_VAL2); |
| 133 | if (result != TEST_RESULT_SUCCESS) { |
| 134 | return result; |
| 135 | } |
| 136 | |
| J-Alves | d5d8715 | 2021-10-29 11:48:37 +0100 | [diff] [blame] | 137 | result = send_cactus_req_echo_cmd(VM_ID(2), SP_ID(3), SP_ID(1), |
| J-Alves | b1e81d8 | 2020-11-09 15:40:31 +0000 | [diff] [blame] | 138 | ECHO_VAL3); |
| 139 | |
| 140 | return result; |
| 141 | } |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 142 | |
| 143 | test_result_t test_ffa_sp_to_sp_deadlock(void) |
| 144 | { |
| Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 145 | struct ffa_value ret; |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 146 | |
| 147 | /********************************************************************** |
| 148 | * Check SPMC has ffa_version and expected FFA endpoints are deployed. |
| 149 | **********************************************************************/ |
| J-Alves | 0446930 | 2021-01-21 14:48:13 +0000 | [diff] [blame] | 150 | CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids); |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 151 | |
| J-Alves | 5339201 | 2020-11-18 14:51:57 +0000 | [diff] [blame] | 152 | ret = cactus_req_deadlock_send_cmd(HYP_ID, SP_ID(1), SP_ID(2), SP_ID(3)); |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 153 | |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 154 | if (is_ffa_direct_response(ret) == false) { |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 155 | return TEST_RESULT_FAIL; |
| 156 | } |
| 157 | |
| J-Alves | 5339201 | 2020-11-18 14:51:57 +0000 | [diff] [blame] | 158 | if (cactus_get_response(ret) == CACTUS_ERROR) { |
| J-Alves | ae95ac9 | 2020-11-11 13:06:55 +0000 | [diff] [blame] | 159 | return TEST_RESULT_FAIL; |
| 160 | } |
| 161 | |
| 162 | return TEST_RESULT_SUCCESS; |
| 163 | } |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Handler that is passed during tftf_cpu_on to individual CPU cores. |
| 167 | * Runs a specific core and send a direct message request. |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 168 | */ |
| 169 | static test_result_t cpu_on_handler(void) |
| 170 | { |
| J-Alves | 79c08f1 | 2021-10-27 15:15:16 +0100 | [diff] [blame] | 171 | unsigned int core_pos = get_current_core_id(); |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 172 | test_result_t ret = TEST_RESULT_SUCCESS; |
| Daniel Boulby | ce386b1 | 2022-03-29 18:36:36 +0100 | [diff] [blame] | 173 | struct ffa_value ffa_ret; |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
| Olivier Deprez | 35e1948 | 2021-02-11 09:32:20 +0100 | [diff] [blame] | 176 | * Send a direct message request to SP1 (MP SP) from current physical |
| 177 | * CPU. Notice SP1 ECs are already woken as a result of the PSCI_CPU_ON |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 178 | * invocation so they already reached the message loop. |
| 179 | * The SPMC uses the MP pinned context corresponding to the physical |
| 180 | * CPU emitting the request. |
| 181 | */ |
| 182 | ret = send_cactus_echo_cmd(HYP_ID, SP_ID(1), ECHO_VAL1); |
| 183 | if (ret != TEST_RESULT_SUCCESS) { |
| 184 | goto out; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Secure Partitions beyond the first SP only have their first |
| 189 | * EC (or vCPU0) woken up at boot time by the SPMC. |
| 190 | * Other ECs need one round of ffa_run to reach the message loop. |
| 191 | */ |
| 192 | ffa_ret = ffa_run(SP_ID(2), core_pos); |
| 193 | if (ffa_func_id(ffa_ret) != FFA_MSG_WAIT) { |
| 194 | ERROR("Failed to run SP%x on core %u\n", SP_ID(2), |
| 195 | core_pos); |
| 196 | ret = TEST_RESULT_FAIL; |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | /* |
| Olivier Deprez | 35e1948 | 2021-02-11 09:32:20 +0100 | [diff] [blame] | 201 | * Send a direct message request to SP2 (MP SP) from current physical |
| 202 | * CPU. The SPMC uses the MP pinned context corresponding to the |
| 203 | * physical CPU emitting the request. |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 204 | */ |
| 205 | ret = send_cactus_echo_cmd(HYP_ID, SP_ID(2), ECHO_VAL2); |
| 206 | if (ret != TEST_RESULT_SUCCESS) { |
| 207 | goto out; |
| 208 | } |
| 209 | |
| Olivier Deprez | 35e1948 | 2021-02-11 09:32:20 +0100 | [diff] [blame] | 210 | /* |
| 211 | * Send a direct message request to SP3 (UP SP) from current physical CPU. |
| 212 | * The SPMC uses the single vCPU migrated to the new physical core. |
| 213 | * The single SP vCPU may receive requests from multiple physical CPUs. |
| 214 | * Thus it is possible one message is being processed on one core while |
| 215 | * another (or multiple) cores attempt sending a new direct message |
| 216 | * request. In such case the cores attempting the new request receive |
| 217 | * a busy response from the SPMC. To handle this case a retry loop is |
| 218 | * implemented permitting some fairness. |
| 219 | */ |
| 220 | uint32_t trial_loop = 5U; |
| 221 | while (trial_loop--) { |
| 222 | ffa_ret = cactus_echo_send_cmd(HYP_ID, SP_ID(3), ECHO_VAL3); |
| 223 | if ((ffa_func_id(ffa_ret) == FFA_ERROR) && |
| 224 | (ffa_error_code(ffa_ret) == FFA_ERROR_BUSY)) { |
| 225 | VERBOSE("%s(%u) trial %u\n", __func__, core_pos, trial_loop); |
| 226 | waitms(1); |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | if (is_ffa_direct_response(ffa_ret) == true) { |
| 231 | if (cactus_get_response(ffa_ret) != CACTUS_SUCCESS || |
| 232 | cactus_echo_get_val(ffa_ret) != ECHO_VAL3) { |
| 233 | ERROR("Echo Failed!\n"); |
| 234 | ret = TEST_RESULT_FAIL; |
| 235 | } |
| 236 | |
| 237 | goto out; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | ret = TEST_RESULT_FAIL; |
| 242 | |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 243 | out: |
| 244 | /* Tell the lead CPU that the calling CPU has completed the test */ |
| 245 | tftf_send_event(&cpu_booted[core_pos]); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Test direct messaging in multicore setup. Runs SPs on all the cores and sends |
| 252 | * direct messages to SPs. |
| 253 | */ |
| 254 | test_result_t test_ffa_secondary_core_direct_msg(void) |
| 255 | { |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 256 | /********************************************************************** |
| 257 | * Check SPMC has ffa_version and expected FFA endpoints are deployed. |
| 258 | **********************************************************************/ |
| 259 | CHECK_SPMC_TESTING_SETUP(1, 0, expected_sp_uuids); |
| J-Alves | d56c53c | 2021-07-01 16:32:16 +0100 | [diff] [blame] | 260 | return spm_run_multi_core_test((uintptr_t)cpu_on_handler, cpu_booted); |
| Max Shvetsov | c0e9169 | 2020-11-12 17:47:13 +0000 | [diff] [blame] | 261 | } |