Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1 | /* |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 2 | * Copyright (c) 2022-2024, Arm Limited and Contributors. All rights reserved. |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
| 8 | #include <ffa_api.h> |
| 9 | #include <ffa_internal_api.h> |
| 10 | #include <ffa_memory_descriptors.h> |
| 11 | #include <sp_api.h> |
| 12 | #include <sp_discovery.h> |
| 13 | #include <sp_memory_management.h> |
| 14 | #include <sp_rxtx.h> |
| 15 | #include <string.h> |
| 16 | #include <trace.h> |
| 17 | |
Jelle Sels | 6c19b4f | 2022-10-18 16:40:43 +0200 | [diff] [blame] | 18 | #include "config/interface/config_store.h" |
| 19 | #include "config/loader/sp/sp_config_loader.h" |
| 20 | #include "config/ramstore/config_ramstore.h" |
| 21 | #include "platform/interface/memory_region.h" |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 22 | #define SP_TEST_OK 0xaa |
| 23 | |
| 24 | static volatile uint8_t tx_buffer[4096] __aligned(4096); |
| 25 | static volatile uint8_t rx_buffer[4096] __aligned(4096); |
| 26 | static volatile uint8_t my_buf[4096] __aligned(4096); |
| 27 | static volatile uint8_t *shared_buffer; |
| 28 | static size_t shared_buffer_size; |
| 29 | |
| 30 | |
| 31 | |
| 32 | enum errors { |
| 33 | ERR_OK, |
| 34 | ERR_VERSION, |
| 35 | ERR_ID_GET, |
| 36 | ERR_FEATURES, |
| 37 | ERR_SP_COMMUNICATION, |
| 38 | ERR_RXTX_MAP, |
| 39 | ERR_PARTITION, |
| 40 | ERR_RXTX_UNMAP, |
| 41 | ERR_MEM_INCORRECT_ACCESS, |
| 42 | ERR_MEM_RETRIEVE, |
| 43 | ERR_MEM_RELINQUISH, |
| 44 | ERR_SP_SHARE, |
| 45 | ERR_SP_SHARE_EXC, |
| 46 | ERR_TEST_NOT_FOUND |
| 47 | }; |
| 48 | |
| 49 | enum sp_tests { |
| 50 | EP_TEST_SP, |
| 51 | EP_TEST_SP_COMMUNICATION, |
| 52 | EP_TEST_SP_INCREASE, |
| 53 | EP_TRY_R_ACCESS, |
| 54 | EP_TRY_W_ACCESS, |
| 55 | EP_RETRIEVE, |
| 56 | EP_RELINQUISH, |
| 57 | EP_SP_MEM_SHARING, |
| 58 | EP_SP_MEM_SHARING_MULTI, |
| 59 | EP_SP_MEM_SHARING_EXC, |
| 60 | EP_SP_MEM_INCORRECT_ACCESS, |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 61 | EP_SP_NOP, |
| 62 | EP_TEST_SP_COMMUNICATION_RESPONSE, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | const char* sp_test_str[]= { |
| 66 | "EP_TEST_SP", |
| 67 | "EP_TEST_SP_COMMUNICATION", |
| 68 | "EP_TEST_SP_INCREASE", |
| 69 | "EP_TRY_R_ACCESS", |
| 70 | "EP_TRY_W_ACCESS", |
| 71 | "EP_RETRIEVE", |
| 72 | "EP_RELINQUISH", |
| 73 | "EP_SP_MEM_SHARING", |
| 74 | "EP_SP_MEM_SHARING_MULTI", |
| 75 | "EP_SP_MEM_SHARING_EXC", |
| 76 | "EP_SP_MEM_INCORRECT_ACCESS", |
| 77 | "EP_SP_NOP" |
| 78 | }; |
| 79 | |
| 80 | static bool test_ffa_version(void) |
| 81 | { |
| 82 | sp_result result = SP_RESULT_OK; |
| 83 | uint16_t major = 0; |
| 84 | uint16_t minor = 0; |
| 85 | |
| 86 | IMSG("Testing ffa_version()\n"); |
| 87 | |
| 88 | result = sp_discovery_ffa_version_get(&major, &minor); |
| 89 | if (result == SP_RESULT_OK) { |
| 90 | IMSG("ffa_version(): %"PRIu32".%"PRIu32"\n", major, minor); |
| 91 | |
| 92 | return true; |
| 93 | } else if (result == FFA_NOT_SUPPORTED) { |
| 94 | IMSG("ffa_version(): not supported\n"); |
| 95 | } else { |
| 96 | EMSG("ffa_version(): unknown error %"PRId32"\n", result); |
| 97 | } |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | static bool test_ffa_id_get(uint16_t *id) |
| 103 | { |
| 104 | sp_result result = SP_RESULT_OK; |
| 105 | |
| 106 | IMSG("Testing ffa_id_get()\n"); |
| 107 | |
| 108 | result = sp_discovery_own_id_get(id); |
| 109 | if (result == SP_RESULT_OK) { |
| 110 | IMSG("ffa_id_get(): 0x%"PRIx16"\n", *id); |
| 111 | |
| 112 | return true; |
| 113 | } else if (result == FFA_NOT_SUPPORTED) { |
| 114 | IMSG("ffa_id_get(): not supported\n"); |
| 115 | } else { |
| 116 | EMSG("ffa_id_get(): unknown error %"PRId32"\n", result); |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | static bool test_ffa_features(void) |
| 123 | { |
| 124 | ffa_result result = FFA_OK; |
| 125 | struct ffa_interface_properties properties = {0}; |
| 126 | |
| 127 | IMSG("Testing ffa_features(FFA_RXTX_MAP)\n"); |
| 128 | |
| 129 | result = ffa_features(FFA_RXTX_MAP_32, &properties); |
| 130 | if (result == FFA_OK) { |
| 131 | static const char * const sizes[] = { |
| 132 | "4kB", "64kB", "16kB", "reserved"}; |
| 133 | uint32_t buffer_size = properties.interface_properties[0] & |
| 134 | 0x3U; |
| 135 | |
| 136 | IMSG("ffa_features(): minimum buffer size=%s\n", |
| 137 | sizes[buffer_size]); |
| 138 | return true; |
| 139 | } else if (result == FFA_NOT_SUPPORTED) { |
| 140 | IMSG("ffa_features(): not supported\n"); |
| 141 | } else { |
| 142 | EMSG("ffa_features(): unknown error %"PRId32"\n", result); |
| 143 | } |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | static bool test_ffa_rxtx_map(void) |
| 148 | { |
| 149 | sp_result result = SP_RESULT_OK; |
| 150 | |
| 151 | IMSG("Testing ffa_rxtx_map(%p %p, 1)\n", tx_buffer, rx_buffer); |
| 152 | |
| 153 | result = sp_rxtx_buffer_map((void*)tx_buffer,(void*)rx_buffer, |
| 154 | sizeof(rx_buffer)); |
| 155 | if (result == FFA_OK) { |
| 156 | IMSG("ffa_rxtx_map(): success\n"); |
| 157 | return true; |
| 158 | } else if (result == FFA_NOT_SUPPORTED) { |
| 159 | IMSG("ffa_rxtx_map(): not supported\n"); |
| 160 | } else { |
| 161 | EMSG("ffa_rxtx_map(): unknown error %"PRId32"\n", result); |
| 162 | } |
| 163 | |
| 164 | return false; |
| 165 | } |
| 166 | |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 167 | static bool ffa_partition_info_get_process(sp_result result, uint32_t count, |
| 168 | struct sp_partition_info *partitions) |
| 169 | { |
| 170 | uint32_t i = 0; |
| 171 | |
| 172 | if (result != SP_RESULT_OK) { |
| 173 | if (result == FFA_NOT_SUPPORTED) { |
| 174 | IMSG("ffa_partition_info_get(): not supported\n"); |
| 175 | return false; |
| 176 | } |
| 177 | EMSG("ffa_partition_info_get(): unknown error %"PRId32"\n", result); |
| 178 | return false; |
| 179 | } |
| 180 | IMSG("ffa_partition_info_get(): count=%"PRIu32"\n", count); |
| 181 | |
| 182 | for (i = 0; i < count; i++) { |
| 183 | IMSG("partition #%u: ID=%u, execution_count=%u \ |
| 184 | direct request = %c, send direcy request = %c, \ |
| 185 | indirect request = %c\n", |
| 186 | i, partitions[i].partition_id, |
| 187 | partitions[i].execution_context_count, |
| 188 | partitions[i].supports_direct_requests ? '1' : '0', |
| 189 | partitions[i].can_send_direct_requests ? '1' : '0', |
| 190 | partitions[i].supports_indirect_requests ? '1' : '0' |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | IMSG("Testing ffa_rx_release()\n"); |
| 195 | |
| 196 | result = ffa_rx_release(); |
| 197 | if (result == FFA_OK) { |
| 198 | IMSG("ffa_rx_release(): success\n"); |
| 199 | return true; |
| 200 | } else if (result == FFA_NOT_SUPPORTED) { |
| 201 | IMSG("ffa_rx_release(): not supported\n"); |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | EMSG("ffa_rx_release(): unknown error %"PRId32"\n", result); |
| 206 | return false; |
| 207 | } |
| 208 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 209 | static bool test_ffa_partition_info_get(void) |
| 210 | { |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 211 | sp_result result = SP_RESULT_OK; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 212 | struct sp_partition_info partitions[10] = {0}; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 213 | uint32_t count = 10; |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 214 | struct sp_uuid uuid = {.uuid = {0x23, 0xeb, 0x01, 0x00, 0xe3, 0x2a, |
| 215 | 0x44, 0x97, 0x90, 0x52, 0x2f, 0x11, |
| 216 | 0xe5, 0x84, 0xaf, 0xa6}}; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 217 | |
| 218 | IMSG("Testing ffa_partition_info_get(nil)\n"); |
| 219 | |
| 220 | result = sp_discovery_partition_info_get_all(partitions, &count); |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 221 | if (!ffa_partition_info_get_process(result, count, partitions)) |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 222 | return false; |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 223 | result = sp_discovery_partition_info_get(&uuid, |
| 224 | partitions, |
| 225 | &count); |
| 226 | |
| 227 | if (!ffa_partition_info_get_process(result, count, partitions)) |
| 228 | return false; |
| 229 | if (count < 2) { |
| 230 | EMSG("ffa_partition_info_get(): Returned not enough SPs count=%"PRIu32"\n", count); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 231 | return false; |
| 232 | } |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 233 | return true; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static bool test_ffa_rxtx_unmap() |
| 237 | { |
| 238 | sp_result result = SP_RESULT_OK; |
| 239 | |
| 240 | result = sp_rxtx_buffer_unmap(); |
| 241 | if (result == SP_RESULT_OK) { |
| 242 | IMSG("sp_rxtx_buffer_unmap(): success\n"); |
| 243 | return true; |
| 244 | } |
| 245 | EMSG("sp_rxtx_buffer_unmap(): unknown error %"PRId32"\n", result); |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | static void return_error(uint32_t error, struct ffa_direct_msg *msg) |
| 250 | { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 251 | ffa_msg_send_direct_resp_64(msg->destination_id, msg->source_id, 0xff, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 252 | error, 0, 0, 0, msg); |
| 253 | } |
| 254 | |
| 255 | static void return_ok(struct ffa_direct_msg *msg) |
| 256 | { |
| 257 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 258 | ffa_msg_send_direct_resp_64(msg->destination_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 259 | msg->source_id, SP_TEST_OK, 0, 0, 0, 0, msg); |
| 260 | } |
| 261 | |
| 262 | static bool test_read_access(void) |
| 263 | { |
| 264 | return (shared_buffer[0] != 5); |
| 265 | |
| 266 | } |
| 267 | |
| 268 | static void test_write_access(void) |
| 269 | { |
| 270 | shared_buffer[0] = 0xff; |
| 271 | } |
| 272 | |
| 273 | static void test_increase(struct ffa_direct_msg *msg) |
| 274 | { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 275 | msg->args.args64[1]++; |
| 276 | msg->args.args64[2]++; |
| 277 | msg->args.args64[3]++; |
| 278 | msg->args.args64[4]++; |
| 279 | ffa_msg_send_direct_resp_64(msg->destination_id,msg->source_id, |
| 280 | SP_TEST_OK, msg->args.args64[1], |
| 281 | msg->args.args64[2],msg->args.args64[3], |
| 282 | msg->args.args64[4], msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 283 | |
| 284 | } |
| 285 | |
| 286 | static void test_communication(struct ffa_direct_msg *msg) |
| 287 | { |
| 288 | struct ffa_direct_msg sp_msg = {0}; |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 289 | uint16_t caller = msg->source_id; |
| 290 | uint16_t src = msg->destination_id; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 291 | uint16_t dst = (uint16_t)msg->args.args64[1]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 292 | ffa_result res = FFA_OK; |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 293 | struct ffa_params raw_params = { 0 }; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 294 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 295 | sp_msg.args.args64[1] = 0x55; |
| 296 | sp_msg.args.args64[2] = 0xAA; |
| 297 | sp_msg.args.args64[3] = 0xBB; |
| 298 | sp_msg.args.args64[4] = 0xCC; |
| 299 | |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 300 | res = ffa_msg_send_direct_req_64(src, dst, |
Jelle Sels | 45353ba | 2022-09-30 14:47:56 +0200 | [diff] [blame] | 301 | EP_TEST_SP_INCREASE,0x55, 0xAA, 0xBB, |
| 302 | 0xCC, &sp_msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 303 | if (res != FFA_OK) { |
| 304 | EMSG("error % in %s:%d"PRId32, res, __FILE__, __LINE__); |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 305 | goto err; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 308 | if (sp_msg.args.args64[1] != 0x56 || sp_msg.args.args64[2] != 0xAB || |
| 309 | sp_msg.args.args64[3] != 0xBC || sp_msg.args.args64[4] != 0xCD) { |
Imre Kis | 45df16f | 2023-03-24 13:27:10 +0100 | [diff] [blame] | 310 | DMSG("Failed SP communication %lx %lx %lx %lx", |
| 311 | sp_msg.args.args64[1], sp_msg.args.args64[2], |
| 312 | sp_msg.args.args64[3], sp_msg.args.args64[4]); |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 313 | goto err; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 314 | } |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 315 | |
| 316 | /* Non-null flags (W2) register */ |
Balint Dobszay | a28cce4 | 2024-07-09 16:03:29 +0200 | [diff] [blame] | 317 | ffa_svc(FFA_MSG_SEND_DIRECT_REQ_64, (uint32_t)(src << 16 | dst), 1, 0, 0, 0, 0, 0, |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 318 | &raw_params); |
| 319 | if (raw_params.a0 != FFA_ERROR || (uint32_t)raw_params.a2 != FFA_INVALID_PARAMETERS) { |
| 320 | EMSG("Unexpected error code: %d != %ld", FFA_INVALID_PARAMETERS, raw_params.a2); |
| 321 | goto err; |
| 322 | } |
| 323 | |
| 324 | /* Testing non-matching source ID */ |
| 325 | res = ffa_msg_send_direct_req_64(src + 1, dst, 0, 0, 0, 0, 0, &sp_msg); |
| 326 | if (res != FFA_INVALID_PARAMETERS) { |
| 327 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 328 | goto err; |
| 329 | } |
| 330 | |
| 331 | /* Sending message to own ID */ |
| 332 | res = ffa_msg_send_direct_req_64(src, src, 0, 0, 0, 0, 0, &sp_msg); |
| 333 | if (res != FFA_INVALID_PARAMETERS) { |
| 334 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 335 | goto err; |
| 336 | } |
| 337 | |
| 338 | /* Sending message to normal world */ |
| 339 | res = ffa_msg_send_direct_req_64(src, 0, 0, 0, 0, 0, 0, &sp_msg); |
| 340 | if (res != FFA_NOT_SUPPORTED) { |
| 341 | EMSG("Unexpected error code: %d != %d", FFA_NOT_SUPPORTED, res); |
| 342 | goto err; |
| 343 | } |
| 344 | |
| 345 | /* Sending message for starting direct message response test */ |
| 346 | if (!caller) { |
| 347 | res = ffa_msg_send_direct_req_64(src, dst, EP_TEST_SP_COMMUNICATION_RESPONSE, 0, 0, |
| 348 | 0, 0, &sp_msg); |
| 349 | if (res != FFA_OK) { |
| 350 | EMSG("Unexpected error code: %d != %d", FFA_OK, res); |
| 351 | goto err; |
| 352 | } |
| 353 | |
| 354 | if (sp_msg.args.args64[0] != SP_TEST_OK) { |
| 355 | EMSG("Unexpected test result: %d != %ld", SP_TEST_OK, sp_msg.args.args64[0]); |
| 356 | goto err; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | return_ok(msg); |
| 361 | return; |
| 362 | |
| 363 | err: |
| 364 | return_error(ERR_SP_COMMUNICATION, msg); |
| 365 | } |
| 366 | |
| 367 | static void test_communication_response(struct ffa_direct_msg *msg) |
| 368 | { |
| 369 | struct ffa_direct_msg sp_msg = {0}; |
| 370 | uint16_t caller = msg->source_id; |
| 371 | uint16_t src = msg->destination_id; |
| 372 | ffa_result res = FFA_OK; |
| 373 | struct ffa_params raw_params = { 0 }; |
| 374 | |
| 375 | /* Non-null flags (W2) register */ |
| 376 | ffa_svc(FFA_MSG_SEND_DIRECT_RESP_64, (uint32_t)(src << 16 | 0x1000), 1, 0, 0, 0, 0, 1, |
| 377 | &raw_params); |
| 378 | if (raw_params.a0 != FFA_ERROR || (uint32_t)raw_params.a2 != FFA_INVALID_PARAMETERS) { |
| 379 | EMSG("Unexpected error code: %d != %ld", FFA_INVALID_PARAMETERS, raw_params.a2); |
| 380 | goto err; |
| 381 | } |
| 382 | |
| 383 | /* Testing non-matching source ID */ |
| 384 | res = ffa_msg_send_direct_resp_64(src + 1, caller, 0, 0, 0, 0, 2, &sp_msg); |
| 385 | if (res != FFA_INVALID_PARAMETERS) { |
| 386 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 387 | goto err; |
| 388 | } |
| 389 | |
| 390 | /* Sending message to own ID */ |
| 391 | res = ffa_msg_send_direct_resp_64(src, src, 0, 0, 0, 0, 3, &sp_msg); |
| 392 | if (res != FFA_INVALID_PARAMETERS) { |
| 393 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 394 | goto err; |
| 395 | } |
| 396 | |
| 397 | /* Sending message request to caller SP which is busy */ |
| 398 | if (caller) { |
| 399 | /* Sending message to normal world */ |
| 400 | res = ffa_msg_send_direct_resp_64(src, 0, 0, 0, 0, 0, 4, &sp_msg); |
| 401 | if (res != FFA_INVALID_PARAMETERS) { |
| 402 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 403 | goto err; |
| 404 | } |
| 405 | |
| 406 | /* Sending message to invalid SP */ |
| 407 | res = ffa_msg_send_direct_resp_64(src, 0x1000, 0, 0, 0, 0, 5, &sp_msg); |
| 408 | if (res != FFA_INVALID_PARAMETERS) { |
| 409 | EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res); |
| 410 | goto err; |
| 411 | } |
| 412 | |
| 413 | /* Sending message request to caller SP which is busy */ |
| 414 | res = ffa_msg_send_direct_req_64(src, caller, 0, 0, 0, 0, 6, &sp_msg); |
| 415 | if (res != FFA_BUSY) { |
| 416 | EMSG("Unexpected error code: %d != %d", FFA_BUSY, res); |
| 417 | goto err; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | ffa_msg_send_direct_resp_64(src, caller, SP_TEST_OK, 0, 0, 0, 0, msg); |
| 422 | return; |
| 423 | |
| 424 | err: |
| 425 | ffa_msg_send_direct_resp_64(src, caller, ERR_SP_COMMUNICATION, 0, 0, 0, 0, msg); |
| 426 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static void test_internal_sp(struct ffa_direct_msg *msg) |
| 430 | { |
| 431 | enum errors err = ERR_OK; |
| 432 | uint16_t id = 0; |
| 433 | |
| 434 | if (test_ffa_version()) { |
| 435 | if (!test_ffa_id_get(&id)) |
| 436 | err = ERR_ID_GET; |
| 437 | |
| 438 | if (!err && !test_ffa_features()) |
| 439 | err = ERR_VERSION; |
| 440 | |
| 441 | if (!err && !test_ffa_rxtx_unmap(id)) |
| 442 | err = ERR_RXTX_UNMAP; |
| 443 | |
| 444 | if (!err && !test_ffa_rxtx_map()) |
| 445 | err = ERR_RXTX_MAP; |
| 446 | |
| 447 | if (!err && !test_ffa_partition_info_get()) |
| 448 | err = ERR_PARTITION; |
| 449 | |
| 450 | } else { |
| 451 | err = ERR_VERSION; |
| 452 | } |
| 453 | |
| 454 | if (err != ERR_OK) { |
| 455 | DMSG("Failed at SP test %x", err); |
| 456 | return_error((uint32_t)err, msg); |
| 457 | } |
| 458 | |
| 459 | return_ok(msg); |
| 460 | } |
| 461 | |
| 462 | static void set_rxtx_buf(struct ffa_mem_transaction_buffer *t_buf, |
| 463 | struct ffa_mem_transaction_buffer *r_buf) |
| 464 | { |
| 465 | if (t_buf) { |
| 466 | t_buf->buffer = (void*)tx_buffer; |
| 467 | t_buf->length = 4096; |
| 468 | t_buf->used = false; |
| 469 | } |
| 470 | if (r_buf) { |
| 471 | r_buf->buffer = (void*)rx_buffer; |
| 472 | r_buf->length = 4096; |
| 473 | r_buf->used = false; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | static void test_mem_retrieve(struct ffa_direct_msg *msg) |
| 478 | { |
| 479 | ffa_result res = FFA_OK; |
| 480 | struct sp_memory_descriptor descriptor = {0}; |
| 481 | struct sp_memory_region regions[1] = {0}; |
| 482 | struct sp_memory_access_descriptor acc_desc = {0}; |
| 483 | uint64_t handle = 0; |
| 484 | uint32_t out_region_count = 1; |
| 485 | uint16_t own_id = 0; |
| 486 | |
| 487 | ffa_id_get(&own_id); |
| 488 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 489 | handle = (uint64_t)msg->args.args64[1] | |
| 490 | (((uint64_t)msg->args.args64[2]) << 32); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 491 | descriptor.tag = 0; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 492 | descriptor.sender_id = msg->args.args64[3] & 0xffff; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 493 | acc_desc.receiver_id = own_id; |
| 494 | acc_desc.data_access = sp_data_access_read_write; |
Balint Dobszay | 3e4f283 | 2023-01-03 14:20:56 +0100 | [diff] [blame] | 495 | res = sp_memory_retrieve(&descriptor, &acc_desc, regions, 0, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 496 | &out_region_count, handle); |
| 497 | |
| 498 | if (res) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 499 | DMSG("Failed retrieving shared memory"); |
| 500 | return_error((uint32_t)ERR_MEM_RETRIEVE, msg); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | if (descriptor.flags.transaction_type != sp_memory_transaction_type_share) { |
| 505 | EMSG("Invalid transaction type"); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 506 | return_error((uint32_t)ERR_MEM_RETRIEVE, msg); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | shared_buffer = regions[0].address; |
| 511 | shared_buffer_size = regions[0].page_count * 4096; |
| 512 | |
| 513 | return_ok(msg); |
| 514 | } |
| 515 | |
| 516 | static void test_mem_relinquish(struct ffa_direct_msg *msg) |
| 517 | { |
| 518 | ffa_result res = FFA_OK; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 519 | uint64_t handle = 0; |
| 520 | uint16_t endpoint_id = 0; |
| 521 | struct sp_memory_transaction_flags flags = { |
| 522 | .zero_memory = false, |
| 523 | .operation_time_slicing = false, |
| 524 | }; |
| 525 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 526 | if (msg->args.args64[3] == 1) |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 527 | flags.zero_memory = true; |
| 528 | |
| 529 | ffa_id_get(&endpoint_id); |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 530 | handle = (uint64_t)msg->args.args64[1] | |
| 531 | (((uint64_t)msg->args.args64[2]) << 32); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 532 | |
| 533 | res = sp_memory_relinquish(handle, &endpoint_id, 1, &flags); |
| 534 | if (res) { |
| 535 | DMSG("Failed to relinquish share"); |
| 536 | return_error((uint32_t)ERR_MEM_RELINQUISH, msg); |
| 537 | } |
| 538 | |
| 539 | return_ok(msg); |
| 540 | } |
| 541 | |
| 542 | static void test_mem_sharing(uint16_t service_ep_id, struct ffa_direct_msg *msg) |
| 543 | { |
| 544 | ffa_result res = FFA_OK; |
| 545 | struct sp_memory_descriptor desc = { 0 }; |
| 546 | struct sp_memory_region region = { 0 }; |
| 547 | uint64_t handle = 0; |
| 548 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 549 | uint16_t own_id = 0; |
| 550 | uint16_t src_id = msg->source_id; |
| 551 | struct sp_memory_access_descriptor acc_desc = { }; |
| 552 | |
| 553 | my_buf[0] = 0xa; |
| 554 | set_rxtx_buf(&t_buf, NULL); |
| 555 | ffa_id_get(&own_id); |
| 556 | |
| 557 | region.address = (void*) my_buf; |
| 558 | region.page_count = 1; |
| 559 | desc.sender_id = own_id; |
| 560 | desc.memory_type = sp_memory_type_normal_memory; |
| 561 | desc.mem_region_attr.normal_memory.cacheability = |
| 562 | sp_cacheability_write_back; |
| 563 | |
| 564 | desc.mem_region_attr.normal_memory.shareability = |
| 565 | sp_shareability_inner_shareable; |
| 566 | |
| 567 | acc_desc.data_access = sp_data_access_read_write; |
| 568 | acc_desc.instruction_access = sp_instruction_access_not_executable; |
| 569 | acc_desc.receiver_id = service_ep_id; |
| 570 | |
| 571 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 572 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 573 | EMSG("Failed to share memory: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 574 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 575 | return; |
| 576 | } |
| 577 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 578 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 579 | EP_RETRIEVE, handle & 0xffffffff, |
| 580 | handle >> 32, own_id, 0, msg); |
| 581 | |
| 582 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 583 | EMSG("Failed to send retrieve command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 584 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 585 | return; |
| 586 | } |
| 587 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 588 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 589 | EP_TRY_W_ACCESS, 0, |
| 590 | 0, 0, 0, msg); |
| 591 | |
| 592 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 593 | EMSG("Failed to send TRY_W_ACCESS command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 594 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 595 | return; |
| 596 | } |
| 597 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 598 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 599 | EP_RELINQUISH, handle & 0xffffffff, |
| 600 | handle >> 32, 0, 0, msg); |
| 601 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 602 | EMSG("Failed to send relinquish command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 603 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 604 | return; |
| 605 | } |
| 606 | |
| 607 | res = ffa_mem_reclaim(handle, 0); |
| 608 | |
| 609 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 610 | EMSG("Failed to reclaim memory: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 611 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 612 | return; |
| 613 | } |
| 614 | msg->destination_id = own_id; |
| 615 | msg->source_id = src_id; |
| 616 | |
| 617 | return_ok(msg); |
| 618 | } |
| 619 | |
| 620 | static void test_mem_multi_sharing(struct ffa_direct_msg *msg) |
| 621 | { |
| 622 | ffa_result res = FFA_OK; |
| 623 | struct sp_memory_descriptor desc = { 0 }; |
| 624 | struct sp_memory_region region = { 0 }; |
| 625 | uint64_t handle = 0; |
| 626 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 627 | uint16_t own_id = 0; |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 628 | uint16_t src_id = msg->source_id; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 629 | struct sp_memory_access_descriptor acc_desc[2] = { }; |
| 630 | uint32_t err = 0; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 631 | uint16_t endpoint2 = msg->args.args64[1]; |
| 632 | uint16_t endpoint3 = msg->args.args64[2]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 633 | |
| 634 | my_buf[0] = 0xa; |
| 635 | set_rxtx_buf(&t_buf, NULL); |
| 636 | ffa_id_get(&own_id); |
| 637 | |
| 638 | region.address = (void*) my_buf; |
| 639 | region.page_count = 1; |
| 640 | desc.sender_id = own_id; |
| 641 | desc.memory_type = sp_memory_type_normal_memory; |
| 642 | desc.mem_region_attr.normal_memory.cacheability = |
| 643 | sp_cacheability_write_back; |
| 644 | |
| 645 | desc.mem_region_attr.normal_memory.shareability = |
| 646 | sp_shareability_inner_shareable; |
| 647 | |
| 648 | acc_desc[0].data_access = sp_data_access_read_write; |
| 649 | acc_desc[0].instruction_access = sp_instruction_access_not_executable; |
| 650 | acc_desc[0].receiver_id = endpoint2; |
| 651 | |
| 652 | acc_desc[1].data_access = sp_data_access_read_write; |
| 653 | acc_desc[1].instruction_access = sp_instruction_access_not_executable; |
| 654 | acc_desc[1].receiver_id = endpoint3; |
| 655 | |
| 656 | res = sp_memory_share(&desc, acc_desc, 2, ®ion, 1, &handle); |
| 657 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 658 | EMSG("Failed to share memory: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 659 | err = (uint32_t)ERR_SP_SHARE; |
| 660 | goto err; |
| 661 | } |
| 662 | /* test SP2*/ |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 663 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 664 | EP_RETRIEVE, handle & 0xffffffff, |
| 665 | handle >> 32, own_id, 0, msg); |
| 666 | |
| 667 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 668 | EMSG("Failed to send retrieve command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 669 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 670 | return; |
| 671 | } |
| 672 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 673 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 674 | EP_TRY_W_ACCESS, 0, |
| 675 | 0, 0, 0, msg); |
| 676 | |
| 677 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 678 | EMSG("Failed to send TRY_W_ACCESS command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 679 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | if (my_buf[0] != 0xff) { |
| 684 | EMSG("SP2 didn't change the value of the buffer"); |
| 685 | err = (uint32_t)ERR_SP_SHARE; |
| 686 | goto err; |
| 687 | } |
| 688 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 689 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 690 | EP_RELINQUISH, handle & 0xffffffff, |
| 691 | handle >> 32, 0, 0, msg); |
| 692 | |
| 693 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 694 | EMSG("Failed to send relinquish command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 695 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 696 | return; |
| 697 | } |
| 698 | my_buf[0] = 0xa; |
| 699 | /* test SP3*/ |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 700 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 701 | EP_RETRIEVE, handle & 0xffffffff, |
| 702 | handle >> 32, own_id, 0, msg); |
| 703 | |
| 704 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 705 | EMSG("Failed to send retrieve command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 706 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 707 | return; |
| 708 | } |
| 709 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 710 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 711 | EP_TRY_W_ACCESS, 0, |
| 712 | 0, 0, 0, msg); |
| 713 | |
| 714 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 715 | EMSG("Failed to send TRY_W_ACCESS command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 716 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 717 | return; |
| 718 | } |
| 719 | |
| 720 | if (my_buf[0] != 0xff) { |
| 721 | EMSG("SP3 didn't change the value of the buffer"); |
| 722 | err = (uint32_t)ERR_SP_SHARE; |
| 723 | goto err; |
| 724 | } |
| 725 | |
| 726 | if (ffa_mem_reclaim(handle, 0) == FFA_OK) { |
| 727 | EMSG("SP3 didn't relinquish memory yet!"); |
| 728 | err = (uint32_t)ERR_SP_SHARE; |
| 729 | goto err; |
| 730 | } |
| 731 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 732 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 733 | EP_RELINQUISH, handle & 0xffffffff, |
| 734 | handle >> 32, 0, 0, msg); |
| 735 | |
| 736 | if (res != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 737 | EMSG("Failed to send relinquish command: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 738 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | if (ffa_mem_reclaim(handle, 0) != FFA_OK) { |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 743 | EMSG("Failed to reclaim memory: %"PRId32, res); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 744 | err = (uint32_t)ERR_SP_SHARE; |
| 745 | goto err; |
| 746 | } |
| 747 | |
| 748 | msg->destination_id = own_id; |
| 749 | msg->source_id = src_id; |
| 750 | return_ok(msg); |
| 751 | return; |
| 752 | err: |
| 753 | msg->destination_id = own_id; |
| 754 | msg->source_id = src_id; |
| 755 | return_error(err, msg); |
| 756 | } |
| 757 | |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 758 | #define TEST_FFA_MEM_SHARE(len, handle, expected) \ |
| 759 | do { \ |
| 760 | ffa_result res = FFA_OK; \ |
| 761 | res = ffa_mem_share_rxtx(len, len, handle); \ |
| 762 | if (res != expected) { \ |
| 763 | EMSG("Invalid FFA_MEM_SHARE result: expected = %d, actual = %d", \ |
| 764 | expected, res); \ |
| 765 | return -1; \ |
| 766 | } \ |
| 767 | } while (0) |
| 768 | |
| 769 | static int test_mem_sharing_invalid(uint16_t service_ep_id) |
| 770 | { |
| 771 | uint64_t handle = 0; |
| 772 | uint16_t own_id = 0; |
| 773 | size_t len = 0; |
| 774 | struct ffa_mem_transaction_desc *transaction = NULL; |
| 775 | struct ffa_mem_access_desc *acc_desc = NULL; |
| 776 | struct ffa_composite_mem_region_desc *comp_desc = NULL; |
| 777 | struct ffa_constituent_mem_region_desc *region = NULL; |
| 778 | |
| 779 | memset((void *)tx_buffer, 0x00, sizeof(tx_buffer)); |
| 780 | |
| 781 | transaction = (struct ffa_mem_transaction_desc *)tx_buffer; |
| 782 | |
| 783 | ffa_id_get(&own_id); |
| 784 | |
| 785 | transaction->sender_id = own_id; |
| 786 | transaction->mem_region_attr = 0x24; |
| 787 | transaction->flags = 0; |
| 788 | transaction->handle = 0; |
| 789 | transaction->tag = 0; |
| 790 | |
| 791 | len = sizeof(*transaction); |
| 792 | |
| 793 | /* Zero offset, size and count */ |
| 794 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 795 | |
| 796 | #if CFG_FFA_VERSION >= FFA_VERSION_1_1 |
| 797 | /* Zero count */ |
| 798 | transaction->mem_access_desc_size = sizeof(struct ffa_mem_access_desc); |
| 799 | transaction->mem_access_desc_offset = sizeof(*transaction); |
| 800 | #endif /* CFG_FFA_VERSION */ |
| 801 | |
| 802 | /* Too many mem access desc */ |
| 803 | transaction->mem_access_desc_count = sizeof(tx_buffer); |
| 804 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 805 | |
| 806 | transaction->mem_access_desc_count = 1; |
| 807 | |
| 808 | #if CFG_FFA_VERSION >= FFA_VERSION_1_1 |
| 809 | /* Invalid offset */ |
| 810 | transaction->mem_access_desc_offset = sizeof(tx_buffer); |
| 811 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 812 | |
| 813 | transaction->mem_access_desc_offset = sizeof(*transaction); |
| 814 | #endif /* CFG_FFA_VERSION */ |
| 815 | |
| 816 | acc_desc = (struct ffa_mem_access_desc *)(tx_buffer + len); |
| 817 | len += sizeof(*acc_desc); |
| 818 | |
| 819 | acc_desc->mem_access_perm_desc.endpoint_id = service_ep_id; |
| 820 | acc_desc->mem_access_perm_desc.mem_access_permissions = 0x06; /* RWnX */ |
| 821 | |
| 822 | /* Too large memory region descriptor offset */ |
| 823 | acc_desc->composite_mem_region_desc_offset = sizeof(tx_buffer); |
| 824 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 825 | |
| 826 | acc_desc->composite_mem_region_desc_offset = len; |
| 827 | comp_desc = (struct ffa_composite_mem_region_desc *)(tx_buffer + len); |
| 828 | len += sizeof(*comp_desc); |
| 829 | |
| 830 | /* Zero pages and address ranges */ |
| 831 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 832 | |
| 833 | region = (struct ffa_constituent_mem_region_desc *)(tx_buffer + len); |
| 834 | len += sizeof(*region); |
| 835 | |
| 836 | /* One region with zero pages */ |
| 837 | region->address = (uint64_t)shared_buffer; |
| 838 | comp_desc->address_range_count = 1; |
| 839 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 840 | |
| 841 | /* One region with not matching sum pages */ |
| 842 | region->address = (uint64_t)shared_buffer; |
| 843 | comp_desc->address_range_count = 1; |
| 844 | comp_desc->total_page_count = 2; |
| 845 | region->page_count = 1; |
| 846 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 847 | |
| 848 | /* Too many regions */ |
| 849 | comp_desc->total_page_count = sizeof(tx_buffer); |
| 850 | comp_desc->address_range_count = sizeof(tx_buffer); |
| 851 | TEST_FFA_MEM_SHARE(len, &handle, FFA_INVALID_PARAMETERS); |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 856 | static void test_mem_sharing_inccorrect_access(uint16_t service_ep_id, |
| 857 | struct ffa_direct_msg *msg) |
| 858 | { |
| 859 | ffa_result res = FFA_OK; |
| 860 | struct sp_memory_descriptor desc = { 0 }; |
| 861 | struct sp_memory_region region = { 0 }; |
| 862 | uint64_t handle = 0; |
| 863 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 864 | uint16_t own_id = 0; |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 865 | uint16_t src_id = msg->source_id; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 866 | struct sp_memory_access_descriptor acc_desc = { }; |
| 867 | |
| 868 | set_rxtx_buf(&t_buf, NULL); |
| 869 | ffa_id_get(&own_id); |
| 870 | |
| 871 | region.address = (void*) my_buf; |
| 872 | region.page_count = 1; |
| 873 | desc.sender_id = own_id; |
| 874 | desc.memory_type = sp_memory_type_normal_memory; |
| 875 | desc.mem_region_attr.normal_memory.cacheability = |
| 876 | sp_cacheability_write_back; |
| 877 | |
| 878 | desc.mem_region_attr.normal_memory.shareability = |
| 879 | sp_shareability_inner_shareable; |
| 880 | |
| 881 | acc_desc.data_access = sp_data_access_read_write; |
| 882 | acc_desc.instruction_access = sp_instruction_access_executable; |
| 883 | acc_desc.receiver_id = service_ep_id; |
| 884 | |
| 885 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 886 | if (res == FFA_OK) { |
| 887 | EMSG("ffa_memory_share(): error %"PRId32, res); |
| 888 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 889 | return; |
| 890 | } |
| 891 | |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 892 | if (test_mem_sharing_invalid(service_ep_id)) { |
| 893 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 894 | return; |
| 895 | } |
| 896 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 897 | msg->destination_id = own_id; |
| 898 | msg->source_id = src_id; |
| 899 | return_ok(msg); |
| 900 | } |
| 901 | |
| 902 | static void test_mem_sharing_exc(uint16_t service_ep_id, |
| 903 | struct ffa_direct_msg *msg) |
| 904 | { |
| 905 | ffa_result res = FFA_OK; |
| 906 | struct sp_memory_descriptor desc = { 0 }; |
| 907 | struct sp_memory_region region = { 0 }; |
| 908 | uint64_t handle = 0; |
| 909 | uint64_t handle2 = 0; |
| 910 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 911 | uint16_t own_id = 0; |
Imre Kis | e946d91 | 2024-04-24 13:24:47 +0200 | [diff] [blame] | 912 | uint16_t src_id = msg->source_id; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 913 | struct sp_memory_access_descriptor acc_desc = { }; |
| 914 | uint32_t err = 0; |
| 915 | |
| 916 | set_rxtx_buf(&t_buf, NULL); |
| 917 | ffa_id_get(&own_id); |
| 918 | |
| 919 | region.address = (void*) my_buf; |
| 920 | region.page_count = 1; |
| 921 | desc.sender_id = own_id; |
| 922 | desc.memory_type = sp_memory_type_normal_memory; |
| 923 | desc.mem_region_attr.normal_memory.cacheability = |
| 924 | sp_cacheability_write_back; |
| 925 | |
| 926 | desc.mem_region_attr.normal_memory.shareability = |
| 927 | sp_shareability_inner_shareable; |
| 928 | |
| 929 | acc_desc.data_access = sp_data_access_read_write; |
| 930 | acc_desc.instruction_access = sp_instruction_access_not_executable; |
| 931 | acc_desc.receiver_id = service_ep_id; |
| 932 | |
| 933 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 934 | if (res != FFA_OK) { |
| 935 | EMSG("test_mem_sharing_exc(): error %"PRId32, res); |
| 936 | err = (uint32_t)ERR_SP_SHARE_EXC; |
| 937 | goto err; |
| 938 | } |
| 939 | |
| 940 | /* |
| 941 | * Try it again, it should fail as we don't have acclusive access |
| 942 | * anymore |
| 943 | */ |
| 944 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle2); |
| 945 | if (res == FFA_OK) { |
| 946 | EMSG("test_mem_sharing_exc(): error %"PRId32, res); |
| 947 | err = (uint32_t)ERR_SP_SHARE_EXC; |
| 948 | goto err; |
| 949 | } |
| 950 | |
| 951 | res = ffa_mem_reclaim(handle, 0); |
| 952 | |
| 953 | if (res != FFA_OK) { |
| 954 | EMSG("ffa_memory_share(): error % in %s:%d"PRId32, res, |
| 955 | __FILE__, |
| 956 | __LINE__); |
| 957 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 958 | return; |
| 959 | } |
| 960 | |
| 961 | msg->destination_id = own_id; |
| 962 | msg->source_id = src_id; |
| 963 | return_ok(msg); |
| 964 | return; |
| 965 | err: |
| 966 | msg->destination_id = own_id; |
| 967 | msg->source_id = src_id; |
| 968 | return_error(err, msg); |
| 969 | } |
| 970 | |
Gabor Toth | 983264f | 2024-01-23 09:16:24 +0100 | [diff] [blame] | 971 | void test_mem_get_set(union ffa_boot_info *boot_info) |
Jelle Sels | 6c19b4f | 2022-10-18 16:40:43 +0200 | [diff] [blame] | 972 | { |
| 973 | void *addr = NULL; |
| 974 | ffa_result res = FFA_OK; |
| 975 | struct memory_region buffer_region = { 0 }; |
| 976 | uint32_t mem_perm = 0; |
| 977 | const uint32_t original_perm = FFA_MEM_PERM_INSTRUCTION_ACCESS_PERM_NX | |
| 978 | FFA_MEM_PERM_DATA_ACCESS_PERM_RW; |
| 979 | const uint32_t ro_perm = FFA_MEM_PERM_INSTRUCTION_ACCESS_PERM_NX | |
| 980 | FFA_MEM_PERM_DATA_ACCESS_PERM_RO; |
| 981 | |
| 982 | DMSG("Testing FFA_MEM_PERM_GET/SET"); |
| 983 | config_ramstore_init(); |
| 984 | |
Gabor Toth | 983264f | 2024-01-23 09:16:24 +0100 | [diff] [blame] | 985 | if (!sp_config_load(boot_info)) { |
Jelle Sels | 6c19b4f | 2022-10-18 16:40:43 +0200 | [diff] [blame] | 986 | EMSG("Failed to load SP config"); |
| 987 | goto err; |
| 988 | } |
| 989 | |
| 990 | /* Only run the test if we have the test-region enabled */ |
| 991 | if (!config_store_query(CONFIG_CLASSIFIER_MEMORY_REGION, "test-region", |
| 992 | 0, &buffer_region, sizeof(buffer_region))) |
| 993 | return; |
| 994 | |
| 995 | addr = (void *)buffer_region.base_addr; |
| 996 | /* Check original permissions */ |
| 997 | res = ffa_mem_perm_get(addr, &mem_perm); |
| 998 | if (res) |
| 999 | goto err; |
| 1000 | |
| 1001 | if (mem_perm != original_perm) { |
| 1002 | EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm); |
| 1003 | res = FFA_INVALID_PARAMETERS; |
| 1004 | goto err; |
| 1005 | } |
| 1006 | |
| 1007 | /* Remove write permission */ |
| 1008 | res = ffa_mem_perm_set(addr, 1, ro_perm); |
| 1009 | if (res) |
| 1010 | goto err; |
| 1011 | |
| 1012 | /* Check if write permission is removed */ |
| 1013 | res = ffa_mem_perm_get(addr, &mem_perm); |
| 1014 | if (res) |
| 1015 | goto err; |
| 1016 | |
| 1017 | if (mem_perm != ro_perm) { |
| 1018 | EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm); |
| 1019 | res = FFA_INVALID_PARAMETERS; |
| 1020 | goto err; |
| 1021 | } |
| 1022 | /* Set write permission back */ |
| 1023 | res = ffa_mem_perm_set(addr, 1, original_perm); |
| 1024 | if (res) |
| 1025 | goto err; |
| 1026 | |
| 1027 | /* Check original permissions */ |
| 1028 | res = ffa_mem_perm_get(addr, &mem_perm); |
| 1029 | if (res) |
| 1030 | goto err; |
| 1031 | |
| 1032 | if (mem_perm != original_perm) { |
| 1033 | EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm); |
| 1034 | res = FFA_INVALID_PARAMETERS; |
| 1035 | goto err; |
| 1036 | } |
| 1037 | |
| 1038 | return; |
| 1039 | err: |
| 1040 | EMSG("GET/SET_MEM failed (0x%x)", res); |
| 1041 | } |
| 1042 | |
Balint Dobszay | 4f9d8e3 | 2023-04-13 13:55:08 +0200 | [diff] [blame] | 1043 | void __noreturn sp_main(union ffa_boot_info *boot_info) { |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1044 | struct ffa_direct_msg msg = {0}; |
| 1045 | uint16_t own_id = 0; |
| 1046 | |
| 1047 | /* Boot phase */ |
| 1048 | if (sp_discovery_own_id_get(&own_id) != SP_RESULT_OK) { |
| 1049 | EMSG("Couldn't get own_id!!"); |
| 1050 | } |
| 1051 | |
| 1052 | test_ffa_rxtx_map(); |
Jelle Sels | 6c19b4f | 2022-10-18 16:40:43 +0200 | [diff] [blame] | 1053 | test_mem_get_set(boot_info); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1054 | /* End of boot phase */ |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame] | 1055 | test_ffa_partition_info_get(); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1056 | ffa_msg_wait(&msg); |
| 1057 | |
| 1058 | while (1) { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 1059 | enum sp_tests test_case = (enum sp_tests)msg.args.args64[0]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1060 | |
| 1061 | DMSG("SP:%x Starting test %s", own_id, sp_test_str[test_case]); |
| 1062 | switch (test_case) { |
| 1063 | case EP_TEST_SP: |
| 1064 | test_internal_sp(&msg); |
| 1065 | break; |
| 1066 | case EP_TEST_SP_COMMUNICATION: |
| 1067 | test_communication(&msg); |
| 1068 | break; |
Imre Kis | bd8e04e | 2023-08-22 15:17:53 +0200 | [diff] [blame] | 1069 | case EP_TEST_SP_COMMUNICATION_RESPONSE: |
| 1070 | test_communication_response(&msg); |
| 1071 | break; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1072 | case EP_TEST_SP_INCREASE: |
| 1073 | test_increase(&msg); |
| 1074 | break; |
| 1075 | case EP_TRY_R_ACCESS: |
| 1076 | test_read_access(); |
| 1077 | return_ok(&msg); |
| 1078 | break; |
| 1079 | case EP_TRY_W_ACCESS: |
| 1080 | test_write_access(); |
| 1081 | return_ok(&msg); |
| 1082 | break; |
| 1083 | case EP_RETRIEVE: |
| 1084 | test_mem_retrieve(&msg); |
| 1085 | break; |
| 1086 | case EP_RELINQUISH: |
| 1087 | test_mem_relinquish(&msg); |
| 1088 | break; |
| 1089 | case EP_SP_MEM_SHARING: |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 1090 | test_mem_sharing((uint16_t)msg.args.args64[1], &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1091 | break; |
| 1092 | case EP_SP_MEM_SHARING_MULTI: |
| 1093 | test_mem_multi_sharing(&msg); |
| 1094 | break; |
| 1095 | case EP_SP_MEM_SHARING_EXC: |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 1096 | test_mem_sharing_exc((uint16_t)msg.args.args64[1], |
Jelle Sels | 45353ba | 2022-09-30 14:47:56 +0200 | [diff] [blame] | 1097 | &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1098 | break; |
| 1099 | case EP_SP_MEM_INCORRECT_ACCESS: |
| 1100 | test_mem_sharing_inccorrect_access( |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 1101 | (uint16_t)msg.args.args64[1], &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1102 | break; |
| 1103 | case EP_SP_NOP: |
| 1104 | return_ok(&msg); |
| 1105 | break; |
| 1106 | |
| 1107 | default: |
| 1108 | return_error((uint32_t)ERR_TEST_NOT_FOUND, &msg); |
| 1109 | break; |
| 1110 | } |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | void sp_interrupt_handler(uint32_t interrupt_id) |
| 1115 | { |
| 1116 | (void)interrupt_id; |
| 1117 | DMSG("Got interrupt %x", interrupt_id); |
| 1118 | } |
Balint Dobszay | ac721da | 2024-07-02 16:33:59 +0200 | [diff] [blame] | 1119 | |
| 1120 | ffa_result ffa_vm_created_handler(uint16_t vm_id, uint64_t handle) |
| 1121 | { |
| 1122 | (void)handle; |
| 1123 | DMSG("VM with ID %d created", vm_id); |
| 1124 | |
| 1125 | return FFA_OK; |
| 1126 | } |
| 1127 | |
| 1128 | ffa_result ffa_vm_destroyed_handler(uint16_t vm_id, uint64_t handle) |
| 1129 | { |
| 1130 | (void)handle; |
| 1131 | DMSG("VM with ID %d destroyed", vm_id); |
| 1132 | |
| 1133 | return FFA_OK; |
| 1134 | } |