blob: 663e29b8d06ecca091308a8b6d436b00763ec45a [file] [log] [blame]
Jelle Sels03756662021-05-20 10:41:57 +02001/*
Gabor Toth569309e2023-02-08 07:56:22 +01002 * Copyright (c) 2022-2023, Arm Limited and Contributors. All rights reserved.
Jelle Sels03756662021-05-20 10:41:57 +02003 *
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
20static volatile uint8_t tx_buffer[4096] __aligned(4096);
21static volatile uint8_t rx_buffer[4096] __aligned(4096);
22static volatile uint8_t my_buf[4096] __aligned(4096);
23static volatile uint8_t *shared_buffer;
24static size_t shared_buffer_size;
25
26
27
28enum 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
45enum 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,
Imre Kisbd8e04e2023-08-22 15:17:53 +020057 EP_SP_NOP,
58 EP_TEST_SP_COMMUNICATION_RESPONSE,
Jelle Sels03756662021-05-20 10:41:57 +020059};
60
61const char* sp_test_str[]= {
62 "EP_TEST_SP",
63 "EP_TEST_SP_COMMUNICATION",
64 "EP_TEST_SP_INCREASE",
65 "EP_TRY_R_ACCESS",
66 "EP_TRY_W_ACCESS",
67 "EP_RETRIEVE",
68 "EP_RELINQUISH",
69 "EP_SP_MEM_SHARING",
70 "EP_SP_MEM_SHARING_MULTI",
71 "EP_SP_MEM_SHARING_EXC",
72 "EP_SP_MEM_INCORRECT_ACCESS",
73 "EP_SP_NOP"
74};
75
76static bool test_ffa_version(void)
77{
78 sp_result result = SP_RESULT_OK;
79 uint16_t major = 0;
80 uint16_t minor = 0;
81
82 IMSG("Testing ffa_version()\n");
83
84 result = sp_discovery_ffa_version_get(&major, &minor);
85 if (result == SP_RESULT_OK) {
86 IMSG("ffa_version(): %"PRIu32".%"PRIu32"\n", major, minor);
87
88 return true;
89 } else if (result == FFA_NOT_SUPPORTED) {
90 IMSG("ffa_version(): not supported\n");
91 } else {
92 EMSG("ffa_version(): unknown error %"PRId32"\n", result);
93 }
94
95 return false;
96}
97
98static bool test_ffa_id_get(uint16_t *id)
99{
100 sp_result result = SP_RESULT_OK;
101
102 IMSG("Testing ffa_id_get()\n");
103
104 result = sp_discovery_own_id_get(id);
105 if (result == SP_RESULT_OK) {
106 IMSG("ffa_id_get(): 0x%"PRIx16"\n", *id);
107
108 return true;
109 } else if (result == FFA_NOT_SUPPORTED) {
110 IMSG("ffa_id_get(): not supported\n");
111 } else {
112 EMSG("ffa_id_get(): unknown error %"PRId32"\n", result);
113 }
114
115 return false;
116}
117
118static bool test_ffa_features(void)
119{
120 ffa_result result = FFA_OK;
121 struct ffa_interface_properties properties = {0};
122
123 IMSG("Testing ffa_features(FFA_RXTX_MAP)\n");
124
125 result = ffa_features(FFA_RXTX_MAP_32, &properties);
126 if (result == FFA_OK) {
127 static const char * const sizes[] = {
128 "4kB", "64kB", "16kB", "reserved"};
129 uint32_t buffer_size = properties.interface_properties[0] &
130 0x3U;
131
132 IMSG("ffa_features(): minimum buffer size=%s\n",
133 sizes[buffer_size]);
134 return true;
135 } else if (result == FFA_NOT_SUPPORTED) {
136 IMSG("ffa_features(): not supported\n");
137 } else {
138 EMSG("ffa_features(): unknown error %"PRId32"\n", result);
139 }
140 return false;
141}
142
143static bool test_ffa_rxtx_map(void)
144{
145 sp_result result = SP_RESULT_OK;
146
147 IMSG("Testing ffa_rxtx_map(%p %p, 1)\n", tx_buffer, rx_buffer);
148
149 result = sp_rxtx_buffer_map((void*)tx_buffer,(void*)rx_buffer,
150 sizeof(rx_buffer));
151 if (result == FFA_OK) {
152 IMSG("ffa_rxtx_map(): success\n");
153 return true;
154 } else if (result == FFA_NOT_SUPPORTED) {
155 IMSG("ffa_rxtx_map(): not supported\n");
156 } else {
157 EMSG("ffa_rxtx_map(): unknown error %"PRId32"\n", result);
158 }
159
160 return false;
161}
162
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200163static bool ffa_partition_info_get_process(sp_result result, uint32_t count,
164 struct sp_partition_info *partitions)
165{
166 uint32_t i = 0;
167
168 if (result != SP_RESULT_OK) {
169 if (result == FFA_NOT_SUPPORTED) {
170 IMSG("ffa_partition_info_get(): not supported\n");
171 return false;
172 }
173 EMSG("ffa_partition_info_get(): unknown error %"PRId32"\n", result);
174 return false;
175 }
176 IMSG("ffa_partition_info_get(): count=%"PRIu32"\n", count);
177
178 for (i = 0; i < count; i++) {
179 IMSG("partition #%u: ID=%u, execution_count=%u \
180 direct request = %c, send direcy request = %c, \
181 indirect request = %c\n",
182 i, partitions[i].partition_id,
183 partitions[i].execution_context_count,
184 partitions[i].supports_direct_requests ? '1' : '0',
185 partitions[i].can_send_direct_requests ? '1' : '0',
186 partitions[i].supports_indirect_requests ? '1' : '0'
187 );
188 }
189
190 IMSG("Testing ffa_rx_release()\n");
191
192 result = ffa_rx_release();
193 if (result == FFA_OK) {
194 IMSG("ffa_rx_release(): success\n");
195 return true;
196 } else if (result == FFA_NOT_SUPPORTED) {
197 IMSG("ffa_rx_release(): not supported\n");
198 return false;
199 }
200
201 EMSG("ffa_rx_release(): unknown error %"PRId32"\n", result);
202 return false;
203}
204
Jelle Sels03756662021-05-20 10:41:57 +0200205static bool test_ffa_partition_info_get(void)
206{
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200207 sp_result result = SP_RESULT_OK;
Jelle Sels03756662021-05-20 10:41:57 +0200208 struct sp_partition_info partitions[10] = {0};
Jelle Sels03756662021-05-20 10:41:57 +0200209 uint32_t count = 10;
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200210 struct sp_uuid uuid = {.uuid = {0x23, 0xeb, 0x01, 0x00, 0xe3, 0x2a,
211 0x44, 0x97, 0x90, 0x52, 0x2f, 0x11,
212 0xe5, 0x84, 0xaf, 0xa6}};
Jelle Sels03756662021-05-20 10:41:57 +0200213
214 IMSG("Testing ffa_partition_info_get(nil)\n");
215
216 result = sp_discovery_partition_info_get_all(partitions, &count);
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200217 if (!ffa_partition_info_get_process(result, count, partitions))
Jelle Sels03756662021-05-20 10:41:57 +0200218 return false;
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200219 result = sp_discovery_partition_info_get(&uuid,
220 partitions,
221 &count);
222
223 if (!ffa_partition_info_get_process(result, count, partitions))
224 return false;
225 if (count < 2) {
226 EMSG("ffa_partition_info_get(): Returned not enough SPs count=%"PRIu32"\n", count);
Jelle Sels03756662021-05-20 10:41:57 +0200227 return false;
228 }
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200229 return true;
Jelle Sels03756662021-05-20 10:41:57 +0200230}
231
232static bool test_ffa_rxtx_unmap()
233{
234 sp_result result = SP_RESULT_OK;
235
236 result = sp_rxtx_buffer_unmap();
237 if (result == SP_RESULT_OK) {
238 IMSG("sp_rxtx_buffer_unmap(): success\n");
239 return true;
240 }
241 EMSG("sp_rxtx_buffer_unmap(): unknown error %"PRId32"\n", result);
242 return false;
243}
244
245static void return_error(uint32_t error, struct ffa_direct_msg *msg)
246{
Gabor Toth569309e2023-02-08 07:56:22 +0100247 ffa_msg_send_direct_resp_64(msg->destination_id, msg->source_id, 0xff,
Jelle Sels03756662021-05-20 10:41:57 +0200248 error, 0, 0, 0, msg);
249}
250
251static void return_ok(struct ffa_direct_msg *msg)
252{
253
Gabor Toth569309e2023-02-08 07:56:22 +0100254 ffa_msg_send_direct_resp_64(msg->destination_id,
Jelle Sels03756662021-05-20 10:41:57 +0200255 msg->source_id, SP_TEST_OK, 0, 0, 0, 0, msg);
256}
257
258static bool test_read_access(void)
259{
260 return (shared_buffer[0] != 5);
261
262}
263
264static void test_write_access(void)
265{
266 shared_buffer[0] = 0xff;
267}
268
269static void test_increase(struct ffa_direct_msg *msg)
270{
Gabor Toth569309e2023-02-08 07:56:22 +0100271 msg->args.args64[1]++;
272 msg->args.args64[2]++;
273 msg->args.args64[3]++;
274 msg->args.args64[4]++;
275 ffa_msg_send_direct_resp_64(msg->destination_id,msg->source_id,
276 SP_TEST_OK, msg->args.args64[1],
277 msg->args.args64[2],msg->args.args64[3],
278 msg->args.args64[4], msg);
Jelle Sels03756662021-05-20 10:41:57 +0200279
280}
281
282static void test_communication(struct ffa_direct_msg *msg)
283{
284 struct ffa_direct_msg sp_msg = {0};
Imre Kisbd8e04e2023-08-22 15:17:53 +0200285 uint16_t caller = msg->source_id;
286 uint16_t src = msg->destination_id;
Gabor Toth569309e2023-02-08 07:56:22 +0100287 uint16_t dst = (uint16_t)msg->args.args64[1];
Jelle Sels03756662021-05-20 10:41:57 +0200288 ffa_result res = FFA_OK;
Imre Kisbd8e04e2023-08-22 15:17:53 +0200289 struct ffa_params raw_params = { 0 };
Jelle Sels03756662021-05-20 10:41:57 +0200290
Gabor Toth569309e2023-02-08 07:56:22 +0100291 sp_msg.args.args64[1] = 0x55;
292 sp_msg.args.args64[2] = 0xAA;
293 sp_msg.args.args64[3] = 0xBB;
294 sp_msg.args.args64[4] = 0xCC;
295
Imre Kisbd8e04e2023-08-22 15:17:53 +0200296 res = ffa_msg_send_direct_req_64(src, dst,
Jelle Sels45353ba2022-09-30 14:47:56 +0200297 EP_TEST_SP_INCREASE,0x55, 0xAA, 0xBB,
298 0xCC, &sp_msg);
Jelle Sels03756662021-05-20 10:41:57 +0200299 if (res != FFA_OK) {
300 EMSG("error % in %s:%d"PRId32, res, __FILE__, __LINE__);
Imre Kisbd8e04e2023-08-22 15:17:53 +0200301 goto err;
Jelle Sels03756662021-05-20 10:41:57 +0200302 }
303
Imre Kisbd8e04e2023-08-22 15:17:53 +0200304 if (sp_msg.args.args64[1] != 0x56 || sp_msg.args.args64[2] != 0xAB ||
305 sp_msg.args.args64[3] != 0xBC || sp_msg.args.args64[4] != 0xCD) {
Imre Kis45df16f2023-03-24 13:27:10 +0100306 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]);
Imre Kisbd8e04e2023-08-22 15:17:53 +0200309 goto err;
Jelle Sels03756662021-05-20 10:41:57 +0200310 }
Imre Kisbd8e04e2023-08-22 15:17:53 +0200311
312 /* Non-null flags (W2) register */
313 ffa_svc(FFA_MSG_SEND_DIRECT_REQ_64, (uint32_t)(src << 16 | 0x1000), 1, 0, 0, 0, 0, 0,
314 &raw_params);
315 if (raw_params.a0 != FFA_ERROR || (uint32_t)raw_params.a2 != FFA_INVALID_PARAMETERS) {
316 EMSG("Unexpected error code: %d != %ld", FFA_INVALID_PARAMETERS, raw_params.a2);
317 goto err;
318 }
319
320 /* Testing non-matching source ID */
321 res = ffa_msg_send_direct_req_64(src + 1, dst, 0, 0, 0, 0, 0, &sp_msg);
322 if (res != FFA_INVALID_PARAMETERS) {
323 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
324 goto err;
325 }
326
327 /* Sending message to own ID */
328 res = ffa_msg_send_direct_req_64(src, src, 0, 0, 0, 0, 0, &sp_msg);
329 if (res != FFA_INVALID_PARAMETERS) {
330 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
331 goto err;
332 }
333
334 /* Sending message to normal world */
335 res = ffa_msg_send_direct_req_64(src, 0, 0, 0, 0, 0, 0, &sp_msg);
336 if (res != FFA_NOT_SUPPORTED) {
337 EMSG("Unexpected error code: %d != %d", FFA_NOT_SUPPORTED, res);
338 goto err;
339 }
340
341 /* Sending message for starting direct message response test */
342 if (!caller) {
343 res = ffa_msg_send_direct_req_64(src, dst, EP_TEST_SP_COMMUNICATION_RESPONSE, 0, 0,
344 0, 0, &sp_msg);
345 if (res != FFA_OK) {
346 EMSG("Unexpected error code: %d != %d", FFA_OK, res);
347 goto err;
348 }
349
350 if (sp_msg.args.args64[0] != SP_TEST_OK) {
351 EMSG("Unexpected test result: %d != %ld", SP_TEST_OK, sp_msg.args.args64[0]);
352 goto err;
353 }
354 }
355
356 return_ok(msg);
357 return;
358
359err:
360 return_error(ERR_SP_COMMUNICATION, msg);
361}
362
363static void test_communication_response(struct ffa_direct_msg *msg)
364{
365 struct ffa_direct_msg sp_msg = {0};
366 uint16_t caller = msg->source_id;
367 uint16_t src = msg->destination_id;
368 ffa_result res = FFA_OK;
369 struct ffa_params raw_params = { 0 };
370
371 /* Non-null flags (W2) register */
372 ffa_svc(FFA_MSG_SEND_DIRECT_RESP_64, (uint32_t)(src << 16 | 0x1000), 1, 0, 0, 0, 0, 1,
373 &raw_params);
374 if (raw_params.a0 != FFA_ERROR || (uint32_t)raw_params.a2 != FFA_INVALID_PARAMETERS) {
375 EMSG("Unexpected error code: %d != %ld", FFA_INVALID_PARAMETERS, raw_params.a2);
376 goto err;
377 }
378
379 /* Testing non-matching source ID */
380 res = ffa_msg_send_direct_resp_64(src + 1, caller, 0, 0, 0, 0, 2, &sp_msg);
381 if (res != FFA_INVALID_PARAMETERS) {
382 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
383 goto err;
384 }
385
386 /* Sending message to own ID */
387 res = ffa_msg_send_direct_resp_64(src, src, 0, 0, 0, 0, 3, &sp_msg);
388 if (res != FFA_INVALID_PARAMETERS) {
389 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
390 goto err;
391 }
392
393 /* Sending message request to caller SP which is busy */
394 if (caller) {
395 /* Sending message to normal world */
396 res = ffa_msg_send_direct_resp_64(src, 0, 0, 0, 0, 0, 4, &sp_msg);
397 if (res != FFA_INVALID_PARAMETERS) {
398 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
399 goto err;
400 }
401
402 /* Sending message to invalid SP */
403 res = ffa_msg_send_direct_resp_64(src, 0x1000, 0, 0, 0, 0, 5, &sp_msg);
404 if (res != FFA_INVALID_PARAMETERS) {
405 EMSG("Unexpected error code: %d != %d", FFA_INVALID_PARAMETERS, res);
406 goto err;
407 }
408
409 /* Sending message request to caller SP which is busy */
410 res = ffa_msg_send_direct_req_64(src, caller, 0, 0, 0, 0, 6, &sp_msg);
411 if (res != FFA_BUSY) {
412 EMSG("Unexpected error code: %d != %d", FFA_BUSY, res);
413 goto err;
414 }
415 }
416
417 ffa_msg_send_direct_resp_64(src, caller, SP_TEST_OK, 0, 0, 0, 0, msg);
418 return;
419
420err:
421 ffa_msg_send_direct_resp_64(src, caller, ERR_SP_COMMUNICATION, 0, 0, 0, 0, msg);
422
Jelle Sels03756662021-05-20 10:41:57 +0200423}
424
425static void test_internal_sp(struct ffa_direct_msg *msg)
426{
427 enum errors err = ERR_OK;
428 uint16_t id = 0;
429
430 if (test_ffa_version()) {
431 if (!test_ffa_id_get(&id))
432 err = ERR_ID_GET;
433
434 if (!err && !test_ffa_features())
435 err = ERR_VERSION;
436
437 if (!err && !test_ffa_rxtx_unmap(id))
438 err = ERR_RXTX_UNMAP;
439
440 if (!err && !test_ffa_rxtx_map())
441 err = ERR_RXTX_MAP;
442
443 if (!err && !test_ffa_partition_info_get())
444 err = ERR_PARTITION;
445
446 } else {
447 err = ERR_VERSION;
448 }
449
450 if (err != ERR_OK) {
451 DMSG("Failed at SP test %x", err);
452 return_error((uint32_t)err, msg);
453 }
454
455 return_ok(msg);
456}
457
458static void set_rxtx_buf(struct ffa_mem_transaction_buffer *t_buf,
459 struct ffa_mem_transaction_buffer *r_buf)
460{
461 if (t_buf) {
462 t_buf->buffer = (void*)tx_buffer;
463 t_buf->length = 4096;
464 t_buf->used = false;
465 }
466 if (r_buf) {
467 r_buf->buffer = (void*)rx_buffer;
468 r_buf->length = 4096;
469 r_buf->used = false;
470 }
471}
472
473static void test_mem_retrieve(struct ffa_direct_msg *msg)
474{
475 ffa_result res = FFA_OK;
476 struct sp_memory_descriptor descriptor = {0};
477 struct sp_memory_region regions[1] = {0};
478 struct sp_memory_access_descriptor acc_desc = {0};
479 uint64_t handle = 0;
480 uint32_t out_region_count = 1;
481 uint16_t own_id = 0;
482
483 ffa_id_get(&own_id);
484
Gabor Toth569309e2023-02-08 07:56:22 +0100485 handle = (uint64_t)msg->args.args64[1] |
486 (((uint64_t)msg->args.args64[2]) << 32);
Jelle Sels03756662021-05-20 10:41:57 +0200487 descriptor.tag = 0;
Gabor Toth569309e2023-02-08 07:56:22 +0100488 descriptor.sender_id = msg->args.args64[3] & 0xffff;
Jelle Sels03756662021-05-20 10:41:57 +0200489 acc_desc.receiver_id = own_id;
490 acc_desc.data_access = sp_data_access_read_write;
491 res = sp_memory_retrieve(&descriptor, &acc_desc, regions, 1,
492 &out_region_count, handle);
493
494 if (res) {
495 DMSG("Failed retrieving me share");
496 return_error((uint32_t)ERR_MEM_RETRIEVE, msg);
497 return;
498 }
499
500 shared_buffer = regions[0].address;
501 shared_buffer_size = regions[0].page_count * 4096;
502
503 return_ok(msg);
504}
505
506static void test_mem_relinquish(struct ffa_direct_msg *msg)
507{
508 ffa_result res = FFA_OK;
Jelle Sels03756662021-05-20 10:41:57 +0200509 uint64_t handle = 0;
510 uint16_t endpoint_id = 0;
511 struct sp_memory_transaction_flags flags = {
512 .zero_memory = false,
513 .operation_time_slicing = false,
514 };
515
Gabor Toth569309e2023-02-08 07:56:22 +0100516 if (msg->args.args64[3] == 1)
Jelle Sels03756662021-05-20 10:41:57 +0200517 flags.zero_memory = true;
518
519 ffa_id_get(&endpoint_id);
Gabor Toth569309e2023-02-08 07:56:22 +0100520 handle = (uint64_t)msg->args.args64[1] |
521 (((uint64_t)msg->args.args64[2]) << 32);
Jelle Sels03756662021-05-20 10:41:57 +0200522
523 res = sp_memory_relinquish(handle, &endpoint_id, 1, &flags);
524 if (res) {
525 DMSG("Failed to relinquish share");
526 return_error((uint32_t)ERR_MEM_RELINQUISH, msg);
527 }
528
529 return_ok(msg);
530}
531
532static void test_mem_sharing(uint16_t service_ep_id, struct ffa_direct_msg *msg)
533{
534 ffa_result res = FFA_OK;
535 struct sp_memory_descriptor desc = { 0 };
536 struct sp_memory_region region = { 0 };
537 uint64_t handle = 0;
538 struct ffa_mem_transaction_buffer t_buf = {0};
539 uint16_t own_id = 0;
540 uint16_t src_id = msg->source_id;
541 struct sp_memory_access_descriptor acc_desc = { };
542
543 my_buf[0] = 0xa;
544 set_rxtx_buf(&t_buf, NULL);
545 ffa_id_get(&own_id);
546
547 region.address = (void*) my_buf;
548 region.page_count = 1;
549 desc.sender_id = own_id;
550 desc.memory_type = sp_memory_type_normal_memory;
551 desc.mem_region_attr.normal_memory.cacheability =
552 sp_cacheability_write_back;
553
554 desc.mem_region_attr.normal_memory.shareability =
555 sp_shareability_inner_shareable;
556
557 acc_desc.data_access = sp_data_access_read_write;
558 acc_desc.instruction_access = sp_instruction_access_not_executable;
559 acc_desc.receiver_id = service_ep_id;
560
561 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
562 if (res != FFA_OK) {
563 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
564 __FILE__,
565 __LINE__);
566 return_error((uint32_t)ERR_SP_SHARE, msg);
567 return;
568 }
569
Gabor Toth569309e2023-02-08 07:56:22 +0100570 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200571 EP_RETRIEVE, handle & 0xffffffff,
572 handle >> 32, own_id, 0, msg);
573
574 if (res != FFA_OK) {
575 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
576 __FILE__,
577 __LINE__);
578 return_error((uint32_t)ERR_SP_SHARE, msg);
579 return;
580 }
581
Gabor Toth569309e2023-02-08 07:56:22 +0100582 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200583 EP_TRY_W_ACCESS, 0,
584 0, 0, 0, msg);
585
586 if (res != FFA_OK) {
587 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
588 __FILE__,
589 __LINE__);
590 return_error((uint32_t)ERR_SP_SHARE, msg);
591 return;
592 }
593
Gabor Toth569309e2023-02-08 07:56:22 +0100594 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200595 EP_RELINQUISH, handle & 0xffffffff,
596 handle >> 32, 0, 0, msg);
597 if (res != FFA_OK) {
598 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
599 __FILE__,
600 __LINE__);
601 return_error((uint32_t)ERR_SP_SHARE, msg);
602 return;
603 }
604
605 res = ffa_mem_reclaim(handle, 0);
606
607 if (res != FFA_OK) {
608 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
609 __FILE__,
610 __LINE__);
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
620static 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;
628 uint16_t src_id = msg->source_id = 0;
629 struct sp_memory_access_descriptor acc_desc[2] = { };
630 uint32_t err = 0;
Gabor Toth569309e2023-02-08 07:56:22 +0100631 uint16_t endpoint2 = msg->args.args64[1];
632 uint16_t endpoint3 = msg->args.args64[2];
Jelle Sels03756662021-05-20 10:41:57 +0200633
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, &region, 1, &handle);
657 if (res != FFA_OK) {
658 EMSG("ffa_memory_share(): error %"PRId32, res);
659 err = (uint32_t)ERR_SP_SHARE;
660 goto err;
661 }
662 /* test SP2*/
Gabor Toth569309e2023-02-08 07:56:22 +0100663 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200664 EP_RETRIEVE, handle & 0xffffffff,
665 handle >> 32, own_id, 0, msg);
666
667 if (res != FFA_OK) {
668 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
669 __FILE__,
670 __LINE__);
671 return_error((uint32_t)ERR_SP_SHARE, msg);
672 return;
673 }
674
Gabor Toth569309e2023-02-08 07:56:22 +0100675 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200676 EP_TRY_W_ACCESS, 0,
677 0, 0, 0, msg);
678
679 if (res != FFA_OK) {
680 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
681 __FILE__,
682 __LINE__);
683 return_error((uint32_t)ERR_SP_SHARE, msg);
684 return;
685 }
686
687 if (my_buf[0] != 0xff) {
688 EMSG("SP2 didn't change the value of the buffer");
689 err = (uint32_t)ERR_SP_SHARE;
690 goto err;
691 }
692
Gabor Toth569309e2023-02-08 07:56:22 +0100693 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200694 EP_RELINQUISH, handle & 0xffffffff,
695 handle >> 32, 0, 0, msg);
696
697 if (res != FFA_OK) {
698 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
699 __FILE__,
700 __LINE__);
701 return_error((uint32_t)ERR_SP_SHARE, msg);
702 return;
703 }
704 my_buf[0] = 0xa;
705 /* test SP3*/
Gabor Toth569309e2023-02-08 07:56:22 +0100706 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200707 EP_RETRIEVE, handle & 0xffffffff,
708 handle >> 32, own_id, 0, msg);
709
710 if (res != FFA_OK) {
711 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
712 __FILE__,
713 __LINE__);
714 return_error((uint32_t)ERR_SP_SHARE, msg);
715 return;
716 }
717
Gabor Toth569309e2023-02-08 07:56:22 +0100718 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200719 EP_TRY_W_ACCESS, 0,
720 0, 0, 0, msg);
721
722 if (res != FFA_OK) {
723 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
724 __FILE__,
725 __LINE__);
726 return_error((uint32_t)ERR_SP_SHARE, msg);
727 return;
728 }
729
730 if (my_buf[0] != 0xff) {
731 EMSG("SP3 didn't change the value of the buffer");
732 err = (uint32_t)ERR_SP_SHARE;
733 goto err;
734 }
735
736 if (ffa_mem_reclaim(handle, 0) == FFA_OK) {
737 EMSG("SP3 didn't relinquish memory yet!");
738 err = (uint32_t)ERR_SP_SHARE;
739 goto err;
740 }
741
Gabor Toth569309e2023-02-08 07:56:22 +0100742 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200743 EP_RELINQUISH, handle & 0xffffffff,
744 handle >> 32, 0, 0, msg);
745
746 if (res != FFA_OK) {
747 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
748 __FILE__,
749 __LINE__);
750 return_error((uint32_t)ERR_SP_SHARE, msg);
751 return;
752 }
753
754 if (ffa_mem_reclaim(handle, 0) != FFA_OK) {
755 EMSG("All memory should have been relinquished!");
756 err = (uint32_t)ERR_SP_SHARE;
757 goto err;
758 }
759
760 msg->destination_id = own_id;
761 msg->source_id = src_id;
762 return_ok(msg);
763 return;
764err:
765 msg->destination_id = own_id;
766 msg->source_id = src_id;
767 return_error(err, msg);
768}
769
770static void test_mem_sharing_inccorrect_access(uint16_t service_ep_id,
771 struct ffa_direct_msg *msg)
772{
773 ffa_result res = FFA_OK;
774 struct sp_memory_descriptor desc = { 0 };
775 struct sp_memory_region region = { 0 };
776 uint64_t handle = 0;
777 struct ffa_mem_transaction_buffer t_buf = {0};
778 uint16_t own_id = 0;
779 uint16_t src_id = msg->source_id = 0;
780 struct sp_memory_access_descriptor acc_desc = { };
781
782 set_rxtx_buf(&t_buf, NULL);
783 ffa_id_get(&own_id);
784
785 region.address = (void*) my_buf;
786 region.page_count = 1;
787 desc.sender_id = own_id;
788 desc.memory_type = sp_memory_type_normal_memory;
789 desc.mem_region_attr.normal_memory.cacheability =
790 sp_cacheability_write_back;
791
792 desc.mem_region_attr.normal_memory.shareability =
793 sp_shareability_inner_shareable;
794
795 acc_desc.data_access = sp_data_access_read_write;
796 acc_desc.instruction_access = sp_instruction_access_executable;
797 acc_desc.receiver_id = service_ep_id;
798
799 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
800 if (res == FFA_OK) {
801 EMSG("ffa_memory_share(): error %"PRId32, res);
802 return_error((uint32_t)ERR_SP_SHARE, msg);
803 return;
804 }
805
806 msg->destination_id = own_id;
807 msg->source_id = src_id;
808 return_ok(msg);
809}
810
811static void test_mem_sharing_exc(uint16_t service_ep_id,
812 struct ffa_direct_msg *msg)
813{
814 ffa_result res = FFA_OK;
815 struct sp_memory_descriptor desc = { 0 };
816 struct sp_memory_region region = { 0 };
817 uint64_t handle = 0;
818 uint64_t handle2 = 0;
819 struct ffa_mem_transaction_buffer t_buf = {0};
820 uint16_t own_id = 0;
821 uint16_t src_id = msg->source_id = 0;
822 struct sp_memory_access_descriptor acc_desc = { };
823 uint32_t err = 0;
824
825 set_rxtx_buf(&t_buf, NULL);
826 ffa_id_get(&own_id);
827
828 region.address = (void*) my_buf;
829 region.page_count = 1;
830 desc.sender_id = own_id;
831 desc.memory_type = sp_memory_type_normal_memory;
832 desc.mem_region_attr.normal_memory.cacheability =
833 sp_cacheability_write_back;
834
835 desc.mem_region_attr.normal_memory.shareability =
836 sp_shareability_inner_shareable;
837
838 acc_desc.data_access = sp_data_access_read_write;
839 acc_desc.instruction_access = sp_instruction_access_not_executable;
840 acc_desc.receiver_id = service_ep_id;
841
842 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
843 if (res != FFA_OK) {
844 EMSG("test_mem_sharing_exc(): error %"PRId32, res);
845 err = (uint32_t)ERR_SP_SHARE_EXC;
846 goto err;
847 }
848
849 /*
850 * Try it again, it should fail as we don't have acclusive access
851 * anymore
852 */
853 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle2);
854 if (res == FFA_OK) {
855 EMSG("test_mem_sharing_exc(): error %"PRId32, res);
856 err = (uint32_t)ERR_SP_SHARE_EXC;
857 goto err;
858 }
859
860 res = ffa_mem_reclaim(handle, 0);
861
862 if (res != FFA_OK) {
863 EMSG("ffa_memory_share(): error % in %s:%d"PRId32, res,
864 __FILE__,
865 __LINE__);
866 return_error((uint32_t)ERR_SP_SHARE, msg);
867 return;
868 }
869
870 msg->destination_id = own_id;
871 msg->source_id = src_id;
872 return_ok(msg);
873 return;
874err:
875 msg->destination_id = own_id;
876 msg->source_id = src_id;
877 return_error(err, msg);
878}
879
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200880void __noreturn sp_main(union ffa_boot_info *boot_info) {
Jelle Sels03756662021-05-20 10:41:57 +0200881 struct ffa_direct_msg msg = {0};
882 uint16_t own_id = 0;
883
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200884 (void)boot_info;
885
Jelle Sels03756662021-05-20 10:41:57 +0200886 /* Boot phase */
887 if (sp_discovery_own_id_get(&own_id) != SP_RESULT_OK) {
888 EMSG("Couldn't get own_id!!");
889 }
890
891 test_ffa_rxtx_map();
892 /* End of boot phase */
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200893 test_ffa_partition_info_get();
Jelle Sels03756662021-05-20 10:41:57 +0200894 ffa_msg_wait(&msg);
895
896 while (1) {
Gabor Toth569309e2023-02-08 07:56:22 +0100897 enum sp_tests test_case = (enum sp_tests)msg.args.args64[0];
Jelle Sels03756662021-05-20 10:41:57 +0200898
899 DMSG("SP:%x Starting test %s", own_id, sp_test_str[test_case]);
900 switch (test_case) {
901 case EP_TEST_SP:
902 test_internal_sp(&msg);
903 break;
904 case EP_TEST_SP_COMMUNICATION:
905 test_communication(&msg);
906 break;
Imre Kisbd8e04e2023-08-22 15:17:53 +0200907 case EP_TEST_SP_COMMUNICATION_RESPONSE:
908 test_communication_response(&msg);
909 break;
Jelle Sels03756662021-05-20 10:41:57 +0200910 case EP_TEST_SP_INCREASE:
911 test_increase(&msg);
912 break;
913 case EP_TRY_R_ACCESS:
914 test_read_access();
915 return_ok(&msg);
916 break;
917 case EP_TRY_W_ACCESS:
918 test_write_access();
919 return_ok(&msg);
920 break;
921 case EP_RETRIEVE:
922 test_mem_retrieve(&msg);
923 break;
924 case EP_RELINQUISH:
925 test_mem_relinquish(&msg);
926 break;
927 case EP_SP_MEM_SHARING:
Gabor Toth569309e2023-02-08 07:56:22 +0100928 test_mem_sharing((uint16_t)msg.args.args64[1], &msg);
Jelle Sels03756662021-05-20 10:41:57 +0200929 break;
930 case EP_SP_MEM_SHARING_MULTI:
931 test_mem_multi_sharing(&msg);
932 break;
933 case EP_SP_MEM_SHARING_EXC:
Gabor Toth569309e2023-02-08 07:56:22 +0100934 test_mem_sharing_exc((uint16_t)msg.args.args64[1],
Jelle Sels45353ba2022-09-30 14:47:56 +0200935 &msg);
Jelle Sels03756662021-05-20 10:41:57 +0200936 break;
937 case EP_SP_MEM_INCORRECT_ACCESS:
938 test_mem_sharing_inccorrect_access(
Gabor Toth569309e2023-02-08 07:56:22 +0100939 (uint16_t)msg.args.args64[1], &msg);
Jelle Sels03756662021-05-20 10:41:57 +0200940 break;
941 case EP_SP_NOP:
942 return_ok(&msg);
943 break;
944
945 default:
946 return_error((uint32_t)ERR_TEST_NOT_FOUND, &msg);
947 break;
948 }
949 }
950}
951
952void sp_interrupt_handler(uint32_t interrupt_id)
953{
954 (void)interrupt_id;
955 DMSG("Got interrupt %x", interrupt_id);
956}