Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 1 | /* |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 2 | * Copyright (c) 2022-2023, 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 | |
| 18 | #define SP_TEST_OK 0xaa |
| 19 | |
| 20 | static volatile uint8_t tx_buffer[4096] __aligned(4096); |
| 21 | static volatile uint8_t rx_buffer[4096] __aligned(4096); |
| 22 | static volatile uint8_t my_buf[4096] __aligned(4096); |
| 23 | static volatile uint8_t *shared_buffer; |
| 24 | static size_t shared_buffer_size; |
| 25 | |
| 26 | |
| 27 | |
| 28 | enum errors { |
| 29 | ERR_OK, |
| 30 | ERR_VERSION, |
| 31 | ERR_ID_GET, |
| 32 | ERR_FEATURES, |
| 33 | ERR_SP_COMMUNICATION, |
| 34 | ERR_RXTX_MAP, |
| 35 | ERR_PARTITION, |
| 36 | ERR_RXTX_UNMAP, |
| 37 | ERR_MEM_INCORRECT_ACCESS, |
| 38 | ERR_MEM_RETRIEVE, |
| 39 | ERR_MEM_RELINQUISH, |
| 40 | ERR_SP_SHARE, |
| 41 | ERR_SP_SHARE_EXC, |
| 42 | ERR_TEST_NOT_FOUND |
| 43 | }; |
| 44 | |
| 45 | enum sp_tests { |
| 46 | EP_TEST_SP, |
| 47 | EP_TEST_SP_COMMUNICATION, |
| 48 | EP_TEST_SP_INCREASE, |
| 49 | EP_TRY_R_ACCESS, |
| 50 | EP_TRY_W_ACCESS, |
| 51 | EP_RETRIEVE, |
| 52 | EP_RELINQUISH, |
| 53 | EP_SP_MEM_SHARING, |
| 54 | EP_SP_MEM_SHARING_MULTI, |
| 55 | EP_SP_MEM_SHARING_EXC, |
| 56 | EP_SP_MEM_INCORRECT_ACCESS, |
| 57 | EP_SP_NOP |
| 58 | }; |
| 59 | |
| 60 | const char* sp_test_str[]= { |
| 61 | "EP_TEST_SP", |
| 62 | "EP_TEST_SP_COMMUNICATION", |
| 63 | "EP_TEST_SP_INCREASE", |
| 64 | "EP_TRY_R_ACCESS", |
| 65 | "EP_TRY_W_ACCESS", |
| 66 | "EP_RETRIEVE", |
| 67 | "EP_RELINQUISH", |
| 68 | "EP_SP_MEM_SHARING", |
| 69 | "EP_SP_MEM_SHARING_MULTI", |
| 70 | "EP_SP_MEM_SHARING_EXC", |
| 71 | "EP_SP_MEM_INCORRECT_ACCESS", |
| 72 | "EP_SP_NOP" |
| 73 | }; |
| 74 | |
| 75 | static bool test_ffa_version(void) |
| 76 | { |
| 77 | sp_result result = SP_RESULT_OK; |
| 78 | uint16_t major = 0; |
| 79 | uint16_t minor = 0; |
| 80 | |
| 81 | IMSG("Testing ffa_version()\n"); |
| 82 | |
| 83 | result = sp_discovery_ffa_version_get(&major, &minor); |
| 84 | if (result == SP_RESULT_OK) { |
| 85 | IMSG("ffa_version(): %"PRIu32".%"PRIu32"\n", major, minor); |
| 86 | |
| 87 | return true; |
| 88 | } else if (result == FFA_NOT_SUPPORTED) { |
| 89 | IMSG("ffa_version(): not supported\n"); |
| 90 | } else { |
| 91 | EMSG("ffa_version(): unknown error %"PRId32"\n", result); |
| 92 | } |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | static bool test_ffa_id_get(uint16_t *id) |
| 98 | { |
| 99 | sp_result result = SP_RESULT_OK; |
| 100 | |
| 101 | IMSG("Testing ffa_id_get()\n"); |
| 102 | |
| 103 | result = sp_discovery_own_id_get(id); |
| 104 | if (result == SP_RESULT_OK) { |
| 105 | IMSG("ffa_id_get(): 0x%"PRIx16"\n", *id); |
| 106 | |
| 107 | return true; |
| 108 | } else if (result == FFA_NOT_SUPPORTED) { |
| 109 | IMSG("ffa_id_get(): not supported\n"); |
| 110 | } else { |
| 111 | EMSG("ffa_id_get(): unknown error %"PRId32"\n", result); |
| 112 | } |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | static bool test_ffa_features(void) |
| 118 | { |
| 119 | ffa_result result = FFA_OK; |
| 120 | struct ffa_interface_properties properties = {0}; |
| 121 | |
| 122 | IMSG("Testing ffa_features(FFA_RXTX_MAP)\n"); |
| 123 | |
| 124 | result = ffa_features(FFA_RXTX_MAP_32, &properties); |
| 125 | if (result == FFA_OK) { |
| 126 | static const char * const sizes[] = { |
| 127 | "4kB", "64kB", "16kB", "reserved"}; |
| 128 | uint32_t buffer_size = properties.interface_properties[0] & |
| 129 | 0x3U; |
| 130 | |
| 131 | IMSG("ffa_features(): minimum buffer size=%s\n", |
| 132 | sizes[buffer_size]); |
| 133 | return true; |
| 134 | } else if (result == FFA_NOT_SUPPORTED) { |
| 135 | IMSG("ffa_features(): not supported\n"); |
| 136 | } else { |
| 137 | EMSG("ffa_features(): unknown error %"PRId32"\n", result); |
| 138 | } |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | static bool test_ffa_rxtx_map(void) |
| 143 | { |
| 144 | sp_result result = SP_RESULT_OK; |
| 145 | |
| 146 | IMSG("Testing ffa_rxtx_map(%p %p, 1)\n", tx_buffer, rx_buffer); |
| 147 | |
| 148 | result = sp_rxtx_buffer_map((void*)tx_buffer,(void*)rx_buffer, |
| 149 | sizeof(rx_buffer)); |
| 150 | if (result == FFA_OK) { |
| 151 | IMSG("ffa_rxtx_map(): success\n"); |
| 152 | return true; |
| 153 | } else if (result == FFA_NOT_SUPPORTED) { |
| 154 | IMSG("ffa_rxtx_map(): not supported\n"); |
| 155 | } else { |
| 156 | EMSG("ffa_rxtx_map(): unknown error %"PRId32"\n", result); |
| 157 | } |
| 158 | |
| 159 | return false; |
| 160 | } |
| 161 | |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 162 | static bool ffa_partition_info_get_process(sp_result result, uint32_t count, |
| 163 | struct sp_partition_info *partitions) |
| 164 | { |
| 165 | uint32_t i = 0; |
| 166 | |
| 167 | if (result != SP_RESULT_OK) { |
| 168 | if (result == FFA_NOT_SUPPORTED) { |
| 169 | IMSG("ffa_partition_info_get(): not supported\n"); |
| 170 | return false; |
| 171 | } |
| 172 | EMSG("ffa_partition_info_get(): unknown error %"PRId32"\n", result); |
| 173 | return false; |
| 174 | } |
| 175 | IMSG("ffa_partition_info_get(): count=%"PRIu32"\n", count); |
| 176 | |
| 177 | for (i = 0; i < count; i++) { |
| 178 | IMSG("partition #%u: ID=%u, execution_count=%u \ |
| 179 | direct request = %c, send direcy request = %c, \ |
| 180 | indirect request = %c\n", |
| 181 | i, partitions[i].partition_id, |
| 182 | partitions[i].execution_context_count, |
| 183 | partitions[i].supports_direct_requests ? '1' : '0', |
| 184 | partitions[i].can_send_direct_requests ? '1' : '0', |
| 185 | partitions[i].supports_indirect_requests ? '1' : '0' |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | IMSG("Testing ffa_rx_release()\n"); |
| 190 | |
| 191 | result = ffa_rx_release(); |
| 192 | if (result == FFA_OK) { |
| 193 | IMSG("ffa_rx_release(): success\n"); |
| 194 | return true; |
| 195 | } else if (result == FFA_NOT_SUPPORTED) { |
| 196 | IMSG("ffa_rx_release(): not supported\n"); |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | EMSG("ffa_rx_release(): unknown error %"PRId32"\n", result); |
| 201 | return false; |
| 202 | } |
| 203 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 204 | static bool test_ffa_partition_info_get(void) |
| 205 | { |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 206 | sp_result result = SP_RESULT_OK; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 207 | struct sp_partition_info partitions[10] = {0}; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 208 | uint32_t count = 10; |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 209 | struct sp_uuid uuid = {.uuid = {0x23, 0xeb, 0x01, 0x00, 0xe3, 0x2a, |
| 210 | 0x44, 0x97, 0x90, 0x52, 0x2f, 0x11, |
| 211 | 0xe5, 0x84, 0xaf, 0xa6}}; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 212 | |
| 213 | IMSG("Testing ffa_partition_info_get(nil)\n"); |
| 214 | |
| 215 | result = sp_discovery_partition_info_get_all(partitions, &count); |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 216 | if (!ffa_partition_info_get_process(result, count, partitions)) |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 217 | return false; |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 218 | result = sp_discovery_partition_info_get(&uuid, |
| 219 | partitions, |
| 220 | &count); |
| 221 | |
| 222 | if (!ffa_partition_info_get_process(result, count, partitions)) |
| 223 | return false; |
| 224 | if (count < 2) { |
| 225 | 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] | 226 | return false; |
| 227 | } |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 228 | return true; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static bool test_ffa_rxtx_unmap() |
| 232 | { |
| 233 | sp_result result = SP_RESULT_OK; |
| 234 | |
| 235 | result = sp_rxtx_buffer_unmap(); |
| 236 | if (result == SP_RESULT_OK) { |
| 237 | IMSG("sp_rxtx_buffer_unmap(): success\n"); |
| 238 | return true; |
| 239 | } |
| 240 | EMSG("sp_rxtx_buffer_unmap(): unknown error %"PRId32"\n", result); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | static void return_error(uint32_t error, struct ffa_direct_msg *msg) |
| 245 | { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 246 | 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] | 247 | error, 0, 0, 0, msg); |
| 248 | } |
| 249 | |
| 250 | static void return_ok(struct ffa_direct_msg *msg) |
| 251 | { |
| 252 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 253 | ffa_msg_send_direct_resp_64(msg->destination_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 254 | msg->source_id, SP_TEST_OK, 0, 0, 0, 0, msg); |
| 255 | } |
| 256 | |
| 257 | static bool test_read_access(void) |
| 258 | { |
| 259 | return (shared_buffer[0] != 5); |
| 260 | |
| 261 | } |
| 262 | |
| 263 | static void test_write_access(void) |
| 264 | { |
| 265 | shared_buffer[0] = 0xff; |
| 266 | } |
| 267 | |
| 268 | static void test_increase(struct ffa_direct_msg *msg) |
| 269 | { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 270 | msg->args.args64[1]++; |
| 271 | msg->args.args64[2]++; |
| 272 | msg->args.args64[3]++; |
| 273 | msg->args.args64[4]++; |
| 274 | ffa_msg_send_direct_resp_64(msg->destination_id,msg->source_id, |
| 275 | SP_TEST_OK, msg->args.args64[1], |
| 276 | msg->args.args64[2],msg->args.args64[3], |
| 277 | msg->args.args64[4], msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 278 | |
| 279 | } |
| 280 | |
| 281 | static void test_communication(struct ffa_direct_msg *msg) |
| 282 | { |
| 283 | struct ffa_direct_msg sp_msg = {0}; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 284 | uint16_t dst = (uint16_t)msg->args.args64[1]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 285 | ffa_result res = FFA_OK; |
| 286 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 287 | sp_msg.args.args64[1] = 0x55; |
| 288 | sp_msg.args.args64[2] = 0xAA; |
| 289 | sp_msg.args.args64[3] = 0xBB; |
| 290 | sp_msg.args.args64[4] = 0xCC; |
| 291 | |
| 292 | res = ffa_msg_send_direct_req_64(msg->destination_id, dst, |
Jelle Sels | 45353ba | 2022-09-30 14:47:56 +0200 | [diff] [blame] | 293 | EP_TEST_SP_INCREASE,0x55, 0xAA, 0xBB, |
| 294 | 0xCC, &sp_msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 295 | |
| 296 | if (res != FFA_OK) { |
| 297 | EMSG("error % in %s:%d"PRId32, res, __FILE__, __LINE__); |
| 298 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 299 | return; |
| 300 | } |
| 301 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 302 | if (sp_msg.args.args64[1] == 0x56 && sp_msg.args.args64[2] == 0xAB && |
| 303 | sp_msg.args.args64[3] == 0xBC &&sp_msg.args.args64[4] == 0xCD) { |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 304 | return_ok(msg); |
| 305 | } else { |
Imre Kis | 45df16f | 2023-03-24 13:27:10 +0100 | [diff] [blame] | 306 | DMSG("Failed SP communication %lx %lx %lx %lx", |
| 307 | sp_msg.args.args64[1], sp_msg.args.args64[2], |
| 308 | sp_msg.args.args64[3], sp_msg.args.args64[4]); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 309 | |
| 310 | return_error(ERR_SP_COMMUNICATION, msg); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void test_internal_sp(struct ffa_direct_msg *msg) |
| 315 | { |
| 316 | enum errors err = ERR_OK; |
| 317 | uint16_t id = 0; |
| 318 | |
| 319 | if (test_ffa_version()) { |
| 320 | if (!test_ffa_id_get(&id)) |
| 321 | err = ERR_ID_GET; |
| 322 | |
| 323 | if (!err && !test_ffa_features()) |
| 324 | err = ERR_VERSION; |
| 325 | |
| 326 | if (!err && !test_ffa_rxtx_unmap(id)) |
| 327 | err = ERR_RXTX_UNMAP; |
| 328 | |
| 329 | if (!err && !test_ffa_rxtx_map()) |
| 330 | err = ERR_RXTX_MAP; |
| 331 | |
| 332 | if (!err && !test_ffa_partition_info_get()) |
| 333 | err = ERR_PARTITION; |
| 334 | |
| 335 | } else { |
| 336 | err = ERR_VERSION; |
| 337 | } |
| 338 | |
| 339 | if (err != ERR_OK) { |
| 340 | DMSG("Failed at SP test %x", err); |
| 341 | return_error((uint32_t)err, msg); |
| 342 | } |
| 343 | |
| 344 | return_ok(msg); |
| 345 | } |
| 346 | |
| 347 | static void set_rxtx_buf(struct ffa_mem_transaction_buffer *t_buf, |
| 348 | struct ffa_mem_transaction_buffer *r_buf) |
| 349 | { |
| 350 | if (t_buf) { |
| 351 | t_buf->buffer = (void*)tx_buffer; |
| 352 | t_buf->length = 4096; |
| 353 | t_buf->used = false; |
| 354 | } |
| 355 | if (r_buf) { |
| 356 | r_buf->buffer = (void*)rx_buffer; |
| 357 | r_buf->length = 4096; |
| 358 | r_buf->used = false; |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | static void test_mem_retrieve(struct ffa_direct_msg *msg) |
| 363 | { |
| 364 | ffa_result res = FFA_OK; |
| 365 | struct sp_memory_descriptor descriptor = {0}; |
| 366 | struct sp_memory_region regions[1] = {0}; |
| 367 | struct sp_memory_access_descriptor acc_desc = {0}; |
| 368 | uint64_t handle = 0; |
| 369 | uint32_t out_region_count = 1; |
| 370 | uint16_t own_id = 0; |
| 371 | |
| 372 | ffa_id_get(&own_id); |
| 373 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 374 | handle = (uint64_t)msg->args.args64[1] | |
| 375 | (((uint64_t)msg->args.args64[2]) << 32); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 376 | descriptor.tag = 0; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 377 | descriptor.sender_id = msg->args.args64[3] & 0xffff; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 378 | acc_desc.receiver_id = own_id; |
| 379 | acc_desc.data_access = sp_data_access_read_write; |
| 380 | res = sp_memory_retrieve(&descriptor, &acc_desc, regions, 1, |
| 381 | &out_region_count, handle); |
| 382 | |
| 383 | if (res) { |
| 384 | DMSG("Failed retrieving me share"); |
| 385 | return_error((uint32_t)ERR_MEM_RETRIEVE, msg); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | shared_buffer = regions[0].address; |
| 390 | shared_buffer_size = regions[0].page_count * 4096; |
| 391 | |
| 392 | return_ok(msg); |
| 393 | } |
| 394 | |
| 395 | static void test_mem_relinquish(struct ffa_direct_msg *msg) |
| 396 | { |
| 397 | ffa_result res = FFA_OK; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 398 | uint64_t handle = 0; |
| 399 | uint16_t endpoint_id = 0; |
| 400 | struct sp_memory_transaction_flags flags = { |
| 401 | .zero_memory = false, |
| 402 | .operation_time_slicing = false, |
| 403 | }; |
| 404 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 405 | if (msg->args.args64[3] == 1) |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 406 | flags.zero_memory = true; |
| 407 | |
| 408 | ffa_id_get(&endpoint_id); |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 409 | handle = (uint64_t)msg->args.args64[1] | |
| 410 | (((uint64_t)msg->args.args64[2]) << 32); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 411 | |
| 412 | res = sp_memory_relinquish(handle, &endpoint_id, 1, &flags); |
| 413 | if (res) { |
| 414 | DMSG("Failed to relinquish share"); |
| 415 | return_error((uint32_t)ERR_MEM_RELINQUISH, msg); |
| 416 | } |
| 417 | |
| 418 | return_ok(msg); |
| 419 | } |
| 420 | |
| 421 | static void test_mem_sharing(uint16_t service_ep_id, struct ffa_direct_msg *msg) |
| 422 | { |
| 423 | ffa_result res = FFA_OK; |
| 424 | struct sp_memory_descriptor desc = { 0 }; |
| 425 | struct sp_memory_region region = { 0 }; |
| 426 | uint64_t handle = 0; |
| 427 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 428 | uint16_t own_id = 0; |
| 429 | uint16_t src_id = msg->source_id; |
| 430 | struct sp_memory_access_descriptor acc_desc = { }; |
| 431 | |
| 432 | my_buf[0] = 0xa; |
| 433 | set_rxtx_buf(&t_buf, NULL); |
| 434 | ffa_id_get(&own_id); |
| 435 | |
| 436 | region.address = (void*) my_buf; |
| 437 | region.page_count = 1; |
| 438 | desc.sender_id = own_id; |
| 439 | desc.memory_type = sp_memory_type_normal_memory; |
| 440 | desc.mem_region_attr.normal_memory.cacheability = |
| 441 | sp_cacheability_write_back; |
| 442 | |
| 443 | desc.mem_region_attr.normal_memory.shareability = |
| 444 | sp_shareability_inner_shareable; |
| 445 | |
| 446 | acc_desc.data_access = sp_data_access_read_write; |
| 447 | acc_desc.instruction_access = sp_instruction_access_not_executable; |
| 448 | acc_desc.receiver_id = service_ep_id; |
| 449 | |
| 450 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 451 | if (res != FFA_OK) { |
| 452 | EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res, |
| 453 | __FILE__, |
| 454 | __LINE__); |
| 455 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 456 | return; |
| 457 | } |
| 458 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 459 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 460 | EP_RETRIEVE, handle & 0xffffffff, |
| 461 | handle >> 32, own_id, 0, msg); |
| 462 | |
| 463 | if (res != FFA_OK) { |
| 464 | EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res, |
| 465 | __FILE__, |
| 466 | __LINE__); |
| 467 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 468 | return; |
| 469 | } |
| 470 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 471 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 472 | EP_TRY_W_ACCESS, 0, |
| 473 | 0, 0, 0, msg); |
| 474 | |
| 475 | if (res != FFA_OK) { |
| 476 | EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res, |
| 477 | __FILE__, |
| 478 | __LINE__); |
| 479 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 480 | return; |
| 481 | } |
| 482 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 483 | res = ffa_msg_send_direct_req_64(own_id, service_ep_id, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 484 | EP_RELINQUISH, handle & 0xffffffff, |
| 485 | handle >> 32, 0, 0, msg); |
| 486 | if (res != FFA_OK) { |
| 487 | EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res, |
| 488 | __FILE__, |
| 489 | __LINE__); |
| 490 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | res = ffa_mem_reclaim(handle, 0); |
| 495 | |
| 496 | if (res != FFA_OK) { |
| 497 | EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res, |
| 498 | __FILE__, |
| 499 | __LINE__); |
| 500 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 501 | return; |
| 502 | } |
| 503 | msg->destination_id = own_id; |
| 504 | msg->source_id = src_id; |
| 505 | |
| 506 | return_ok(msg); |
| 507 | } |
| 508 | |
| 509 | static void test_mem_multi_sharing(struct ffa_direct_msg *msg) |
| 510 | { |
| 511 | ffa_result res = FFA_OK; |
| 512 | struct sp_memory_descriptor desc = { 0 }; |
| 513 | struct sp_memory_region region = { 0 }; |
| 514 | uint64_t handle = 0; |
| 515 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 516 | uint16_t own_id = 0; |
| 517 | uint16_t src_id = msg->source_id = 0; |
| 518 | struct sp_memory_access_descriptor acc_desc[2] = { }; |
| 519 | uint32_t err = 0; |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 520 | uint16_t endpoint2 = msg->args.args64[1]; |
| 521 | uint16_t endpoint3 = msg->args.args64[2]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 522 | |
| 523 | my_buf[0] = 0xa; |
| 524 | set_rxtx_buf(&t_buf, NULL); |
| 525 | ffa_id_get(&own_id); |
| 526 | |
| 527 | region.address = (void*) my_buf; |
| 528 | region.page_count = 1; |
| 529 | desc.sender_id = own_id; |
| 530 | desc.memory_type = sp_memory_type_normal_memory; |
| 531 | desc.mem_region_attr.normal_memory.cacheability = |
| 532 | sp_cacheability_write_back; |
| 533 | |
| 534 | desc.mem_region_attr.normal_memory.shareability = |
| 535 | sp_shareability_inner_shareable; |
| 536 | |
| 537 | acc_desc[0].data_access = sp_data_access_read_write; |
| 538 | acc_desc[0].instruction_access = sp_instruction_access_not_executable; |
| 539 | acc_desc[0].receiver_id = endpoint2; |
| 540 | |
| 541 | acc_desc[1].data_access = sp_data_access_read_write; |
| 542 | acc_desc[1].instruction_access = sp_instruction_access_not_executable; |
| 543 | acc_desc[1].receiver_id = endpoint3; |
| 544 | |
| 545 | res = sp_memory_share(&desc, acc_desc, 2, ®ion, 1, &handle); |
| 546 | if (res != FFA_OK) { |
| 547 | EMSG("ffa_memory_share(): error %"PRId32, res); |
| 548 | err = (uint32_t)ERR_SP_SHARE; |
| 549 | goto err; |
| 550 | } |
| 551 | /* test SP2*/ |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 552 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 553 | EP_RETRIEVE, handle & 0xffffffff, |
| 554 | handle >> 32, own_id, 0, msg); |
| 555 | |
| 556 | if (res != FFA_OK) { |
| 557 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 558 | __FILE__, |
| 559 | __LINE__); |
| 560 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 561 | return; |
| 562 | } |
| 563 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 564 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 565 | EP_TRY_W_ACCESS, 0, |
| 566 | 0, 0, 0, msg); |
| 567 | |
| 568 | if (res != FFA_OK) { |
| 569 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 570 | __FILE__, |
| 571 | __LINE__); |
| 572 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (my_buf[0] != 0xff) { |
| 577 | EMSG("SP2 didn't change the value of the buffer"); |
| 578 | err = (uint32_t)ERR_SP_SHARE; |
| 579 | goto err; |
| 580 | } |
| 581 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 582 | res = ffa_msg_send_direct_req_64(own_id, endpoint2, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 583 | EP_RELINQUISH, handle & 0xffffffff, |
| 584 | handle >> 32, 0, 0, msg); |
| 585 | |
| 586 | if (res != FFA_OK) { |
| 587 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 588 | __FILE__, |
| 589 | __LINE__); |
| 590 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 591 | return; |
| 592 | } |
| 593 | my_buf[0] = 0xa; |
| 594 | /* test SP3*/ |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 595 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 596 | EP_RETRIEVE, handle & 0xffffffff, |
| 597 | handle >> 32, own_id, 0, msg); |
| 598 | |
| 599 | if (res != FFA_OK) { |
| 600 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 601 | __FILE__, |
| 602 | __LINE__); |
| 603 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 604 | return; |
| 605 | } |
| 606 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 607 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 608 | EP_TRY_W_ACCESS, 0, |
| 609 | 0, 0, 0, msg); |
| 610 | |
| 611 | if (res != FFA_OK) { |
| 612 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 613 | __FILE__, |
| 614 | __LINE__); |
| 615 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | if (my_buf[0] != 0xff) { |
| 620 | EMSG("SP3 didn't change the value of the buffer"); |
| 621 | err = (uint32_t)ERR_SP_SHARE; |
| 622 | goto err; |
| 623 | } |
| 624 | |
| 625 | if (ffa_mem_reclaim(handle, 0) == FFA_OK) { |
| 626 | EMSG("SP3 didn't relinquish memory yet!"); |
| 627 | err = (uint32_t)ERR_SP_SHARE; |
| 628 | goto err; |
| 629 | } |
| 630 | |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 631 | res = ffa_msg_send_direct_req_64(own_id, endpoint3, |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 632 | EP_RELINQUISH, handle & 0xffffffff, |
| 633 | handle >> 32, 0, 0, msg); |
| 634 | |
| 635 | if (res != FFA_OK) { |
| 636 | EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res, |
| 637 | __FILE__, |
| 638 | __LINE__); |
| 639 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | if (ffa_mem_reclaim(handle, 0) != FFA_OK) { |
| 644 | EMSG("All memory should have been relinquished!"); |
| 645 | err = (uint32_t)ERR_SP_SHARE; |
| 646 | goto err; |
| 647 | } |
| 648 | |
| 649 | msg->destination_id = own_id; |
| 650 | msg->source_id = src_id; |
| 651 | return_ok(msg); |
| 652 | return; |
| 653 | err: |
| 654 | msg->destination_id = own_id; |
| 655 | msg->source_id = src_id; |
| 656 | return_error(err, msg); |
| 657 | } |
| 658 | |
| 659 | static void test_mem_sharing_inccorrect_access(uint16_t service_ep_id, |
| 660 | struct ffa_direct_msg *msg) |
| 661 | { |
| 662 | ffa_result res = FFA_OK; |
| 663 | struct sp_memory_descriptor desc = { 0 }; |
| 664 | struct sp_memory_region region = { 0 }; |
| 665 | uint64_t handle = 0; |
| 666 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 667 | uint16_t own_id = 0; |
| 668 | uint16_t src_id = msg->source_id = 0; |
| 669 | struct sp_memory_access_descriptor acc_desc = { }; |
| 670 | |
| 671 | set_rxtx_buf(&t_buf, NULL); |
| 672 | ffa_id_get(&own_id); |
| 673 | |
| 674 | region.address = (void*) my_buf; |
| 675 | region.page_count = 1; |
| 676 | desc.sender_id = own_id; |
| 677 | desc.memory_type = sp_memory_type_normal_memory; |
| 678 | desc.mem_region_attr.normal_memory.cacheability = |
| 679 | sp_cacheability_write_back; |
| 680 | |
| 681 | desc.mem_region_attr.normal_memory.shareability = |
| 682 | sp_shareability_inner_shareable; |
| 683 | |
| 684 | acc_desc.data_access = sp_data_access_read_write; |
| 685 | acc_desc.instruction_access = sp_instruction_access_executable; |
| 686 | acc_desc.receiver_id = service_ep_id; |
| 687 | |
| 688 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 689 | if (res == FFA_OK) { |
| 690 | EMSG("ffa_memory_share(): error %"PRId32, res); |
| 691 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 692 | return; |
| 693 | } |
| 694 | |
| 695 | msg->destination_id = own_id; |
| 696 | msg->source_id = src_id; |
| 697 | return_ok(msg); |
| 698 | } |
| 699 | |
| 700 | static void test_mem_sharing_exc(uint16_t service_ep_id, |
| 701 | struct ffa_direct_msg *msg) |
| 702 | { |
| 703 | ffa_result res = FFA_OK; |
| 704 | struct sp_memory_descriptor desc = { 0 }; |
| 705 | struct sp_memory_region region = { 0 }; |
| 706 | uint64_t handle = 0; |
| 707 | uint64_t handle2 = 0; |
| 708 | struct ffa_mem_transaction_buffer t_buf = {0}; |
| 709 | uint16_t own_id = 0; |
| 710 | uint16_t src_id = msg->source_id = 0; |
| 711 | struct sp_memory_access_descriptor acc_desc = { }; |
| 712 | uint32_t err = 0; |
| 713 | |
| 714 | set_rxtx_buf(&t_buf, NULL); |
| 715 | ffa_id_get(&own_id); |
| 716 | |
| 717 | region.address = (void*) my_buf; |
| 718 | region.page_count = 1; |
| 719 | desc.sender_id = own_id; |
| 720 | desc.memory_type = sp_memory_type_normal_memory; |
| 721 | desc.mem_region_attr.normal_memory.cacheability = |
| 722 | sp_cacheability_write_back; |
| 723 | |
| 724 | desc.mem_region_attr.normal_memory.shareability = |
| 725 | sp_shareability_inner_shareable; |
| 726 | |
| 727 | acc_desc.data_access = sp_data_access_read_write; |
| 728 | acc_desc.instruction_access = sp_instruction_access_not_executable; |
| 729 | acc_desc.receiver_id = service_ep_id; |
| 730 | |
| 731 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle); |
| 732 | if (res != FFA_OK) { |
| 733 | EMSG("test_mem_sharing_exc(): error %"PRId32, res); |
| 734 | err = (uint32_t)ERR_SP_SHARE_EXC; |
| 735 | goto err; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Try it again, it should fail as we don't have acclusive access |
| 740 | * anymore |
| 741 | */ |
| 742 | res = sp_memory_share(&desc, &acc_desc, 1, ®ion, 1, &handle2); |
| 743 | if (res == FFA_OK) { |
| 744 | EMSG("test_mem_sharing_exc(): error %"PRId32, res); |
| 745 | err = (uint32_t)ERR_SP_SHARE_EXC; |
| 746 | goto err; |
| 747 | } |
| 748 | |
| 749 | res = ffa_mem_reclaim(handle, 0); |
| 750 | |
| 751 | if (res != FFA_OK) { |
| 752 | EMSG("ffa_memory_share(): error % in %s:%d"PRId32, res, |
| 753 | __FILE__, |
| 754 | __LINE__); |
| 755 | return_error((uint32_t)ERR_SP_SHARE, msg); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | msg->destination_id = own_id; |
| 760 | msg->source_id = src_id; |
| 761 | return_ok(msg); |
| 762 | return; |
| 763 | err: |
| 764 | msg->destination_id = own_id; |
| 765 | msg->source_id = src_id; |
| 766 | return_error(err, msg); |
| 767 | } |
| 768 | |
Balint Dobszay | 4f9d8e3 | 2023-04-13 13:55:08 +0200 | [diff] [blame] | 769 | void __noreturn sp_main(union ffa_boot_info *boot_info) { |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 770 | struct ffa_direct_msg msg = {0}; |
| 771 | uint16_t own_id = 0; |
| 772 | |
Balint Dobszay | 4f9d8e3 | 2023-04-13 13:55:08 +0200 | [diff] [blame] | 773 | (void)boot_info; |
| 774 | |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 775 | /* Boot phase */ |
| 776 | if (sp_discovery_own_id_get(&own_id) != SP_RESULT_OK) { |
| 777 | EMSG("Couldn't get own_id!!"); |
| 778 | } |
| 779 | |
| 780 | test_ffa_rxtx_map(); |
| 781 | /* End of boot phase */ |
Gyorgy Szing | 3e3db70 | 2023-07-21 14:18:19 +0200 | [diff] [blame^] | 782 | test_ffa_partition_info_get(); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 783 | ffa_msg_wait(&msg); |
| 784 | |
| 785 | while (1) { |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 786 | enum sp_tests test_case = (enum sp_tests)msg.args.args64[0]; |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 787 | |
| 788 | DMSG("SP:%x Starting test %s", own_id, sp_test_str[test_case]); |
| 789 | switch (test_case) { |
| 790 | case EP_TEST_SP: |
| 791 | test_internal_sp(&msg); |
| 792 | break; |
| 793 | case EP_TEST_SP_COMMUNICATION: |
| 794 | test_communication(&msg); |
| 795 | break; |
| 796 | case EP_TEST_SP_INCREASE: |
| 797 | test_increase(&msg); |
| 798 | break; |
| 799 | case EP_TRY_R_ACCESS: |
| 800 | test_read_access(); |
| 801 | return_ok(&msg); |
| 802 | break; |
| 803 | case EP_TRY_W_ACCESS: |
| 804 | test_write_access(); |
| 805 | return_ok(&msg); |
| 806 | break; |
| 807 | case EP_RETRIEVE: |
| 808 | test_mem_retrieve(&msg); |
| 809 | break; |
| 810 | case EP_RELINQUISH: |
| 811 | test_mem_relinquish(&msg); |
| 812 | break; |
| 813 | case EP_SP_MEM_SHARING: |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 814 | test_mem_sharing((uint16_t)msg.args.args64[1], &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 815 | break; |
| 816 | case EP_SP_MEM_SHARING_MULTI: |
| 817 | test_mem_multi_sharing(&msg); |
| 818 | break; |
| 819 | case EP_SP_MEM_SHARING_EXC: |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 820 | test_mem_sharing_exc((uint16_t)msg.args.args64[1], |
Jelle Sels | 45353ba | 2022-09-30 14:47:56 +0200 | [diff] [blame] | 821 | &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 822 | break; |
| 823 | case EP_SP_MEM_INCORRECT_ACCESS: |
| 824 | test_mem_sharing_inccorrect_access( |
Gabor Toth | 569309e | 2023-02-08 07:56:22 +0100 | [diff] [blame] | 825 | (uint16_t)msg.args.args64[1], &msg); |
Jelle Sels | 0375666 | 2021-05-20 10:41:57 +0200 | [diff] [blame] | 826 | break; |
| 827 | case EP_SP_NOP: |
| 828 | return_ok(&msg); |
| 829 | break; |
| 830 | |
| 831 | default: |
| 832 | return_error((uint32_t)ERR_TEST_NOT_FOUND, &msg); |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | void sp_interrupt_handler(uint32_t interrupt_id) |
| 839 | { |
| 840 | (void)interrupt_id; |
| 841 | DMSG("Got interrupt %x", interrupt_id); |
| 842 | } |