blob: c79051e61837b545e21f11216dce6836af41be1d [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
Jelle Sels6c19b4f2022-10-18 16:40:43 +020018#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 Sels03756662021-05-20 10:41:57 +020022#define SP_TEST_OK 0xaa
23
24static volatile uint8_t tx_buffer[4096] __aligned(4096);
25static volatile uint8_t rx_buffer[4096] __aligned(4096);
26static volatile uint8_t my_buf[4096] __aligned(4096);
27static volatile uint8_t *shared_buffer;
28static size_t shared_buffer_size;
29
30
31
32enum 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
49enum 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 Kisbd8e04e2023-08-22 15:17:53 +020061 EP_SP_NOP,
62 EP_TEST_SP_COMMUNICATION_RESPONSE,
Jelle Sels03756662021-05-20 10:41:57 +020063};
64
65const 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
80static 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
102static 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
122static 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
147static 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 Szing3e3db702023-07-21 14:18:19 +0200167static 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 Sels03756662021-05-20 10:41:57 +0200209static bool test_ffa_partition_info_get(void)
210{
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200211 sp_result result = SP_RESULT_OK;
Jelle Sels03756662021-05-20 10:41:57 +0200212 struct sp_partition_info partitions[10] = {0};
Jelle Sels03756662021-05-20 10:41:57 +0200213 uint32_t count = 10;
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200214 struct sp_uuid uuid = {.uuid = {0x23, 0xeb, 0x01, 0x00, 0xe3, 0x2a,
215 0x44, 0x97, 0x90, 0x52, 0x2f, 0x11,
216 0xe5, 0x84, 0xaf, 0xa6}};
Jelle Sels03756662021-05-20 10:41:57 +0200217
218 IMSG("Testing ffa_partition_info_get(nil)\n");
219
220 result = sp_discovery_partition_info_get_all(partitions, &count);
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200221 if (!ffa_partition_info_get_process(result, count, partitions))
Jelle Sels03756662021-05-20 10:41:57 +0200222 return false;
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200223 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 Sels03756662021-05-20 10:41:57 +0200231 return false;
232 }
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200233 return true;
Jelle Sels03756662021-05-20 10:41:57 +0200234}
235
236static 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
249static void return_error(uint32_t error, struct ffa_direct_msg *msg)
250{
Gabor Toth569309e2023-02-08 07:56:22 +0100251 ffa_msg_send_direct_resp_64(msg->destination_id, msg->source_id, 0xff,
Jelle Sels03756662021-05-20 10:41:57 +0200252 error, 0, 0, 0, msg);
253}
254
255static void return_ok(struct ffa_direct_msg *msg)
256{
257
Gabor Toth569309e2023-02-08 07:56:22 +0100258 ffa_msg_send_direct_resp_64(msg->destination_id,
Jelle Sels03756662021-05-20 10:41:57 +0200259 msg->source_id, SP_TEST_OK, 0, 0, 0, 0, msg);
260}
261
262static bool test_read_access(void)
263{
264 return (shared_buffer[0] != 5);
265
266}
267
268static void test_write_access(void)
269{
270 shared_buffer[0] = 0xff;
271}
272
273static void test_increase(struct ffa_direct_msg *msg)
274{
Gabor Toth569309e2023-02-08 07:56:22 +0100275 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 Sels03756662021-05-20 10:41:57 +0200283
284}
285
286static void test_communication(struct ffa_direct_msg *msg)
287{
288 struct ffa_direct_msg sp_msg = {0};
Imre Kisbd8e04e2023-08-22 15:17:53 +0200289 uint16_t caller = msg->source_id;
290 uint16_t src = msg->destination_id;
Gabor Toth569309e2023-02-08 07:56:22 +0100291 uint16_t dst = (uint16_t)msg->args.args64[1];
Jelle Sels03756662021-05-20 10:41:57 +0200292 ffa_result res = FFA_OK;
Imre Kisbd8e04e2023-08-22 15:17:53 +0200293 struct ffa_params raw_params = { 0 };
Jelle Sels03756662021-05-20 10:41:57 +0200294
Gabor Toth569309e2023-02-08 07:56:22 +0100295 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 Kisbd8e04e2023-08-22 15:17:53 +0200300 res = ffa_msg_send_direct_req_64(src, dst,
Jelle Sels45353ba2022-09-30 14:47:56 +0200301 EP_TEST_SP_INCREASE,0x55, 0xAA, 0xBB,
302 0xCC, &sp_msg);
Jelle Sels03756662021-05-20 10:41:57 +0200303 if (res != FFA_OK) {
304 EMSG("error % in %s:%d"PRId32, res, __FILE__, __LINE__);
Imre Kisbd8e04e2023-08-22 15:17:53 +0200305 goto err;
Jelle Sels03756662021-05-20 10:41:57 +0200306 }
307
Imre Kisbd8e04e2023-08-22 15:17:53 +0200308 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 Kis45df16f2023-03-24 13:27:10 +0100310 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 Kisbd8e04e2023-08-22 15:17:53 +0200313 goto err;
Jelle Sels03756662021-05-20 10:41:57 +0200314 }
Imre Kisbd8e04e2023-08-22 15:17:53 +0200315
316 /* Non-null flags (W2) register */
317 ffa_svc(FFA_MSG_SEND_DIRECT_REQ_64, (uint32_t)(src << 16 | 0x1000), 1, 0, 0, 0, 0, 0,
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
363err:
364 return_error(ERR_SP_COMMUNICATION, msg);
365}
366
367static 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
424err:
425 ffa_msg_send_direct_resp_64(src, caller, ERR_SP_COMMUNICATION, 0, 0, 0, 0, msg);
426
Jelle Sels03756662021-05-20 10:41:57 +0200427}
428
429static 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
462static 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
477static 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 Toth569309e2023-02-08 07:56:22 +0100489 handle = (uint64_t)msg->args.args64[1] |
490 (((uint64_t)msg->args.args64[2]) << 32);
Jelle Sels03756662021-05-20 10:41:57 +0200491 descriptor.tag = 0;
Gabor Toth569309e2023-02-08 07:56:22 +0100492 descriptor.sender_id = msg->args.args64[3] & 0xffff;
Jelle Sels03756662021-05-20 10:41:57 +0200493 acc_desc.receiver_id = own_id;
494 acc_desc.data_access = sp_data_access_read_write;
495 res = sp_memory_retrieve(&descriptor, &acc_desc, regions, 1,
496 &out_region_count, handle);
497
498 if (res) {
499 DMSG("Failed retrieving me share");
500 return_error((uint32_t)ERR_MEM_RETRIEVE, msg);
501 return;
502 }
503
504 shared_buffer = regions[0].address;
505 shared_buffer_size = regions[0].page_count * 4096;
506
507 return_ok(msg);
508}
509
510static void test_mem_relinquish(struct ffa_direct_msg *msg)
511{
512 ffa_result res = FFA_OK;
Jelle Sels03756662021-05-20 10:41:57 +0200513 uint64_t handle = 0;
514 uint16_t endpoint_id = 0;
515 struct sp_memory_transaction_flags flags = {
516 .zero_memory = false,
517 .operation_time_slicing = false,
518 };
519
Gabor Toth569309e2023-02-08 07:56:22 +0100520 if (msg->args.args64[3] == 1)
Jelle Sels03756662021-05-20 10:41:57 +0200521 flags.zero_memory = true;
522
523 ffa_id_get(&endpoint_id);
Gabor Toth569309e2023-02-08 07:56:22 +0100524 handle = (uint64_t)msg->args.args64[1] |
525 (((uint64_t)msg->args.args64[2]) << 32);
Jelle Sels03756662021-05-20 10:41:57 +0200526
527 res = sp_memory_relinquish(handle, &endpoint_id, 1, &flags);
528 if (res) {
529 DMSG("Failed to relinquish share");
530 return_error((uint32_t)ERR_MEM_RELINQUISH, msg);
531 }
532
533 return_ok(msg);
534}
535
536static void test_mem_sharing(uint16_t service_ep_id, struct ffa_direct_msg *msg)
537{
538 ffa_result res = FFA_OK;
539 struct sp_memory_descriptor desc = { 0 };
540 struct sp_memory_region region = { 0 };
541 uint64_t handle = 0;
542 struct ffa_mem_transaction_buffer t_buf = {0};
543 uint16_t own_id = 0;
544 uint16_t src_id = msg->source_id;
545 struct sp_memory_access_descriptor acc_desc = { };
546
547 my_buf[0] = 0xa;
548 set_rxtx_buf(&t_buf, NULL);
549 ffa_id_get(&own_id);
550
551 region.address = (void*) my_buf;
552 region.page_count = 1;
553 desc.sender_id = own_id;
554 desc.memory_type = sp_memory_type_normal_memory;
555 desc.mem_region_attr.normal_memory.cacheability =
556 sp_cacheability_write_back;
557
558 desc.mem_region_attr.normal_memory.shareability =
559 sp_shareability_inner_shareable;
560
561 acc_desc.data_access = sp_data_access_read_write;
562 acc_desc.instruction_access = sp_instruction_access_not_executable;
563 acc_desc.receiver_id = service_ep_id;
564
565 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
566 if (res != FFA_OK) {
567 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
568 __FILE__,
569 __LINE__);
570 return_error((uint32_t)ERR_SP_SHARE, msg);
571 return;
572 }
573
Gabor Toth569309e2023-02-08 07:56:22 +0100574 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200575 EP_RETRIEVE, handle & 0xffffffff,
576 handle >> 32, own_id, 0, msg);
577
578 if (res != FFA_OK) {
579 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
580 __FILE__,
581 __LINE__);
582 return_error((uint32_t)ERR_SP_SHARE, msg);
583 return;
584 }
585
Gabor Toth569309e2023-02-08 07:56:22 +0100586 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200587 EP_TRY_W_ACCESS, 0,
588 0, 0, 0, msg);
589
590 if (res != FFA_OK) {
591 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
592 __FILE__,
593 __LINE__);
594 return_error((uint32_t)ERR_SP_SHARE, msg);
595 return;
596 }
597
Gabor Toth569309e2023-02-08 07:56:22 +0100598 res = ffa_msg_send_direct_req_64(own_id, service_ep_id,
Jelle Sels03756662021-05-20 10:41:57 +0200599 EP_RELINQUISH, handle & 0xffffffff,
600 handle >> 32, 0, 0, msg);
601 if (res != FFA_OK) {
602 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
603 __FILE__,
604 __LINE__);
605 return_error((uint32_t)ERR_SP_SHARE, msg);
606 return;
607 }
608
609 res = ffa_mem_reclaim(handle, 0);
610
611 if (res != FFA_OK) {
612 EMSG("test_mem_sharing(): error % in %s:%d"PRId32, res,
613 __FILE__,
614 __LINE__);
615 return_error((uint32_t)ERR_SP_SHARE, msg);
616 return;
617 }
618 msg->destination_id = own_id;
619 msg->source_id = src_id;
620
621 return_ok(msg);
622}
623
624static void test_mem_multi_sharing(struct ffa_direct_msg *msg)
625{
626 ffa_result res = FFA_OK;
627 struct sp_memory_descriptor desc = { 0 };
628 struct sp_memory_region region = { 0 };
629 uint64_t handle = 0;
630 struct ffa_mem_transaction_buffer t_buf = {0};
631 uint16_t own_id = 0;
632 uint16_t src_id = msg->source_id = 0;
633 struct sp_memory_access_descriptor acc_desc[2] = { };
634 uint32_t err = 0;
Gabor Toth569309e2023-02-08 07:56:22 +0100635 uint16_t endpoint2 = msg->args.args64[1];
636 uint16_t endpoint3 = msg->args.args64[2];
Jelle Sels03756662021-05-20 10:41:57 +0200637
638 my_buf[0] = 0xa;
639 set_rxtx_buf(&t_buf, NULL);
640 ffa_id_get(&own_id);
641
642 region.address = (void*) my_buf;
643 region.page_count = 1;
644 desc.sender_id = own_id;
645 desc.memory_type = sp_memory_type_normal_memory;
646 desc.mem_region_attr.normal_memory.cacheability =
647 sp_cacheability_write_back;
648
649 desc.mem_region_attr.normal_memory.shareability =
650 sp_shareability_inner_shareable;
651
652 acc_desc[0].data_access = sp_data_access_read_write;
653 acc_desc[0].instruction_access = sp_instruction_access_not_executable;
654 acc_desc[0].receiver_id = endpoint2;
655
656 acc_desc[1].data_access = sp_data_access_read_write;
657 acc_desc[1].instruction_access = sp_instruction_access_not_executable;
658 acc_desc[1].receiver_id = endpoint3;
659
660 res = sp_memory_share(&desc, acc_desc, 2, &region, 1, &handle);
661 if (res != FFA_OK) {
662 EMSG("ffa_memory_share(): error %"PRId32, res);
663 err = (uint32_t)ERR_SP_SHARE;
664 goto err;
665 }
666 /* test SP2*/
Gabor Toth569309e2023-02-08 07:56:22 +0100667 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200668 EP_RETRIEVE, handle & 0xffffffff,
669 handle >> 32, own_id, 0, msg);
670
671 if (res != FFA_OK) {
672 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
673 __FILE__,
674 __LINE__);
675 return_error((uint32_t)ERR_SP_SHARE, msg);
676 return;
677 }
678
Gabor Toth569309e2023-02-08 07:56:22 +0100679 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200680 EP_TRY_W_ACCESS, 0,
681 0, 0, 0, msg);
682
683 if (res != FFA_OK) {
684 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
685 __FILE__,
686 __LINE__);
687 return_error((uint32_t)ERR_SP_SHARE, msg);
688 return;
689 }
690
691 if (my_buf[0] != 0xff) {
692 EMSG("SP2 didn't change the value of the buffer");
693 err = (uint32_t)ERR_SP_SHARE;
694 goto err;
695 }
696
Gabor Toth569309e2023-02-08 07:56:22 +0100697 res = ffa_msg_send_direct_req_64(own_id, endpoint2,
Jelle Sels03756662021-05-20 10:41:57 +0200698 EP_RELINQUISH, handle & 0xffffffff,
699 handle >> 32, 0, 0, msg);
700
701 if (res != FFA_OK) {
702 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
703 __FILE__,
704 __LINE__);
705 return_error((uint32_t)ERR_SP_SHARE, msg);
706 return;
707 }
708 my_buf[0] = 0xa;
709 /* test SP3*/
Gabor Toth569309e2023-02-08 07:56:22 +0100710 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200711 EP_RETRIEVE, handle & 0xffffffff,
712 handle >> 32, own_id, 0, msg);
713
714 if (res != FFA_OK) {
715 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
716 __FILE__,
717 __LINE__);
718 return_error((uint32_t)ERR_SP_SHARE, msg);
719 return;
720 }
721
Gabor Toth569309e2023-02-08 07:56:22 +0100722 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200723 EP_TRY_W_ACCESS, 0,
724 0, 0, 0, msg);
725
726 if (res != FFA_OK) {
727 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
728 __FILE__,
729 __LINE__);
730 return_error((uint32_t)ERR_SP_SHARE, msg);
731 return;
732 }
733
734 if (my_buf[0] != 0xff) {
735 EMSG("SP3 didn't change the value of the buffer");
736 err = (uint32_t)ERR_SP_SHARE;
737 goto err;
738 }
739
740 if (ffa_mem_reclaim(handle, 0) == FFA_OK) {
741 EMSG("SP3 didn't relinquish memory yet!");
742 err = (uint32_t)ERR_SP_SHARE;
743 goto err;
744 }
745
Gabor Toth569309e2023-02-08 07:56:22 +0100746 res = ffa_msg_send_direct_req_64(own_id, endpoint3,
Jelle Sels03756662021-05-20 10:41:57 +0200747 EP_RELINQUISH, handle & 0xffffffff,
748 handle >> 32, 0, 0, msg);
749
750 if (res != FFA_OK) {
751 EMSG("test_mem_multi_sharing(): error % in %s:%d"PRId32, res,
752 __FILE__,
753 __LINE__);
754 return_error((uint32_t)ERR_SP_SHARE, msg);
755 return;
756 }
757
758 if (ffa_mem_reclaim(handle, 0) != FFA_OK) {
759 EMSG("All memory should have been relinquished!");
760 err = (uint32_t)ERR_SP_SHARE;
761 goto err;
762 }
763
764 msg->destination_id = own_id;
765 msg->source_id = src_id;
766 return_ok(msg);
767 return;
768err:
769 msg->destination_id = own_id;
770 msg->source_id = src_id;
771 return_error(err, msg);
772}
773
774static void test_mem_sharing_inccorrect_access(uint16_t service_ep_id,
775 struct ffa_direct_msg *msg)
776{
777 ffa_result res = FFA_OK;
778 struct sp_memory_descriptor desc = { 0 };
779 struct sp_memory_region region = { 0 };
780 uint64_t handle = 0;
781 struct ffa_mem_transaction_buffer t_buf = {0};
782 uint16_t own_id = 0;
783 uint16_t src_id = msg->source_id = 0;
784 struct sp_memory_access_descriptor acc_desc = { };
785
786 set_rxtx_buf(&t_buf, NULL);
787 ffa_id_get(&own_id);
788
789 region.address = (void*) my_buf;
790 region.page_count = 1;
791 desc.sender_id = own_id;
792 desc.memory_type = sp_memory_type_normal_memory;
793 desc.mem_region_attr.normal_memory.cacheability =
794 sp_cacheability_write_back;
795
796 desc.mem_region_attr.normal_memory.shareability =
797 sp_shareability_inner_shareable;
798
799 acc_desc.data_access = sp_data_access_read_write;
800 acc_desc.instruction_access = sp_instruction_access_executable;
801 acc_desc.receiver_id = service_ep_id;
802
803 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
804 if (res == FFA_OK) {
805 EMSG("ffa_memory_share(): error %"PRId32, res);
806 return_error((uint32_t)ERR_SP_SHARE, msg);
807 return;
808 }
809
810 msg->destination_id = own_id;
811 msg->source_id = src_id;
812 return_ok(msg);
813}
814
815static void test_mem_sharing_exc(uint16_t service_ep_id,
816 struct ffa_direct_msg *msg)
817{
818 ffa_result res = FFA_OK;
819 struct sp_memory_descriptor desc = { 0 };
820 struct sp_memory_region region = { 0 };
821 uint64_t handle = 0;
822 uint64_t handle2 = 0;
823 struct ffa_mem_transaction_buffer t_buf = {0};
824 uint16_t own_id = 0;
825 uint16_t src_id = msg->source_id = 0;
826 struct sp_memory_access_descriptor acc_desc = { };
827 uint32_t err = 0;
828
829 set_rxtx_buf(&t_buf, NULL);
830 ffa_id_get(&own_id);
831
832 region.address = (void*) my_buf;
833 region.page_count = 1;
834 desc.sender_id = own_id;
835 desc.memory_type = sp_memory_type_normal_memory;
836 desc.mem_region_attr.normal_memory.cacheability =
837 sp_cacheability_write_back;
838
839 desc.mem_region_attr.normal_memory.shareability =
840 sp_shareability_inner_shareable;
841
842 acc_desc.data_access = sp_data_access_read_write;
843 acc_desc.instruction_access = sp_instruction_access_not_executable;
844 acc_desc.receiver_id = service_ep_id;
845
846 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle);
847 if (res != FFA_OK) {
848 EMSG("test_mem_sharing_exc(): error %"PRId32, res);
849 err = (uint32_t)ERR_SP_SHARE_EXC;
850 goto err;
851 }
852
853 /*
854 * Try it again, it should fail as we don't have acclusive access
855 * anymore
856 */
857 res = sp_memory_share(&desc, &acc_desc, 1, &region, 1, &handle2);
858 if (res == FFA_OK) {
859 EMSG("test_mem_sharing_exc(): error %"PRId32, res);
860 err = (uint32_t)ERR_SP_SHARE_EXC;
861 goto err;
862 }
863
864 res = ffa_mem_reclaim(handle, 0);
865
866 if (res != FFA_OK) {
867 EMSG("ffa_memory_share(): error % in %s:%d"PRId32, res,
868 __FILE__,
869 __LINE__);
870 return_error((uint32_t)ERR_SP_SHARE, msg);
871 return;
872 }
873
874 msg->destination_id = own_id;
875 msg->source_id = src_id;
876 return_ok(msg);
877 return;
878err:
879 msg->destination_id = own_id;
880 msg->source_id = src_id;
881 return_error(err, msg);
882}
883
Jelle Sels6c19b4f2022-10-18 16:40:43 +0200884void test_mem_get_set(struct ffa_init_info *init_info)
885{
886 void *addr = NULL;
887 ffa_result res = FFA_OK;
888 struct memory_region buffer_region = { 0 };
889 uint32_t mem_perm = 0;
890 const uint32_t original_perm = FFA_MEM_PERM_INSTRUCTION_ACCESS_PERM_NX |
891 FFA_MEM_PERM_DATA_ACCESS_PERM_RW;
892 const uint32_t ro_perm = FFA_MEM_PERM_INSTRUCTION_ACCESS_PERM_NX |
893 FFA_MEM_PERM_DATA_ACCESS_PERM_RO;
894
895 DMSG("Testing FFA_MEM_PERM_GET/SET");
896 config_ramstore_init();
897
898 if (!sp_config_load(init_info)) {
899 EMSG("Failed to load SP config");
900 goto err;
901 }
902
903 /* Only run the test if we have the test-region enabled */
904 if (!config_store_query(CONFIG_CLASSIFIER_MEMORY_REGION, "test-region",
905 0, &buffer_region, sizeof(buffer_region)))
906 return;
907
908 addr = (void *)buffer_region.base_addr;
909 /* Check original permissions */
910 res = ffa_mem_perm_get(addr, &mem_perm);
911 if (res)
912 goto err;
913
914 if (mem_perm != original_perm) {
915 EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm);
916 res = FFA_INVALID_PARAMETERS;
917 goto err;
918 }
919
920 /* Remove write permission */
921 res = ffa_mem_perm_set(addr, 1, ro_perm);
922 if (res)
923 goto err;
924
925 /* Check if write permission is removed */
926 res = ffa_mem_perm_get(addr, &mem_perm);
927 if (res)
928 goto err;
929
930 if (mem_perm != ro_perm) {
931 EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm);
932 res = FFA_INVALID_PARAMETERS;
933 goto err;
934 }
935 /* Set write permission back */
936 res = ffa_mem_perm_set(addr, 1, original_perm);
937 if (res)
938 goto err;
939
940 /* Check original permissions */
941 res = ffa_mem_perm_get(addr, &mem_perm);
942 if (res)
943 goto err;
944
945 if (mem_perm != original_perm) {
946 EMSG("Incorrect permision got 0x%x expected 0x%x", mem_perm, original_perm);
947 res = FFA_INVALID_PARAMETERS;
948 goto err;
949 }
950
951 return;
952err:
953 EMSG("GET/SET_MEM failed (0x%x)", res);
954}
955
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200956void __noreturn sp_main(union ffa_boot_info *boot_info) {
Jelle Sels03756662021-05-20 10:41:57 +0200957 struct ffa_direct_msg msg = {0};
958 uint16_t own_id = 0;
959
Balint Dobszay4f9d8e32023-04-13 13:55:08 +0200960 (void)boot_info;
961
Jelle Sels03756662021-05-20 10:41:57 +0200962 /* Boot phase */
963 if (sp_discovery_own_id_get(&own_id) != SP_RESULT_OK) {
964 EMSG("Couldn't get own_id!!");
965 }
966
967 test_ffa_rxtx_map();
Jelle Sels6c19b4f2022-10-18 16:40:43 +0200968 test_mem_get_set(boot_info);
Jelle Sels03756662021-05-20 10:41:57 +0200969 /* End of boot phase */
Gyorgy Szing3e3db702023-07-21 14:18:19 +0200970 test_ffa_partition_info_get();
Jelle Sels03756662021-05-20 10:41:57 +0200971 ffa_msg_wait(&msg);
972
973 while (1) {
Gabor Toth569309e2023-02-08 07:56:22 +0100974 enum sp_tests test_case = (enum sp_tests)msg.args.args64[0];
Jelle Sels03756662021-05-20 10:41:57 +0200975
976 DMSG("SP:%x Starting test %s", own_id, sp_test_str[test_case]);
977 switch (test_case) {
978 case EP_TEST_SP:
979 test_internal_sp(&msg);
980 break;
981 case EP_TEST_SP_COMMUNICATION:
982 test_communication(&msg);
983 break;
Imre Kisbd8e04e2023-08-22 15:17:53 +0200984 case EP_TEST_SP_COMMUNICATION_RESPONSE:
985 test_communication_response(&msg);
986 break;
Jelle Sels03756662021-05-20 10:41:57 +0200987 case EP_TEST_SP_INCREASE:
988 test_increase(&msg);
989 break;
990 case EP_TRY_R_ACCESS:
991 test_read_access();
992 return_ok(&msg);
993 break;
994 case EP_TRY_W_ACCESS:
995 test_write_access();
996 return_ok(&msg);
997 break;
998 case EP_RETRIEVE:
999 test_mem_retrieve(&msg);
1000 break;
1001 case EP_RELINQUISH:
1002 test_mem_relinquish(&msg);
1003 break;
1004 case EP_SP_MEM_SHARING:
Gabor Toth569309e2023-02-08 07:56:22 +01001005 test_mem_sharing((uint16_t)msg.args.args64[1], &msg);
Jelle Sels03756662021-05-20 10:41:57 +02001006 break;
1007 case EP_SP_MEM_SHARING_MULTI:
1008 test_mem_multi_sharing(&msg);
1009 break;
1010 case EP_SP_MEM_SHARING_EXC:
Gabor Toth569309e2023-02-08 07:56:22 +01001011 test_mem_sharing_exc((uint16_t)msg.args.args64[1],
Jelle Sels45353ba2022-09-30 14:47:56 +02001012 &msg);
Jelle Sels03756662021-05-20 10:41:57 +02001013 break;
1014 case EP_SP_MEM_INCORRECT_ACCESS:
1015 test_mem_sharing_inccorrect_access(
Gabor Toth569309e2023-02-08 07:56:22 +01001016 (uint16_t)msg.args.args64[1], &msg);
Jelle Sels03756662021-05-20 10:41:57 +02001017 break;
1018 case EP_SP_NOP:
1019 return_ok(&msg);
1020 break;
1021
1022 default:
1023 return_error((uint32_t)ERR_TEST_NOT_FOUND, &msg);
1024 break;
1025 }
1026 }
1027}
1028
1029void sp_interrupt_handler(uint32_t interrupt_id)
1030{
1031 (void)interrupt_id;
1032 DMSG("Got interrupt %x", interrupt_id);
1033}