From: Abdellatif El Khlifi <abdellatif.elkhl...@arm.com>

Add functional test cases for the FF-A core driver

These tests rely on the FF-A Sandbox driver which helps in
 inspecting the FF-A core driver.

Signed-off-by: Abdellatif El Khlifi <abdellatif.elkhl...@arm.com>
---
 MAINTAINERS      |   2 +
 test/dm/Makefile |   1 +
 test/dm/ffa.c    | 418 +++++++++++++++++++++++++++++++++++++++++++++++
 test/dm/ffa.h    |  22 +++
 4 files changed, 443 insertions(+)
 create mode 100644 test/dm/ffa.c
 create mode 100644 test/dm/ffa.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 7c439ed1fd..74b7ed9388 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -242,6 +242,8 @@ F:  include/arm_ffa_helper.h
 F:     include/sandbox_arm_ffa.h
 F:     include/sandbox_arm_ffa_helper.h
 F:     lib/arm-ffa/
+F:     test/dm/ffa.c
+F:     test/dm/ffa.h
 
 ARM FREESCALE IMX
 M:     Stefano Babic <sba...@denx.de>
diff --git a/test/dm/Makefile b/test/dm/Makefile
index d46552fbf3..48eab1b1ff 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -79,6 +79,7 @@ obj-$(CONFIG_POWER_DOMAIN) += power-domain.o
 obj-$(CONFIG_ACPI_PMC) += pmc.o
 obj-$(CONFIG_DM_PMIC) += pmic.o
 obj-$(CONFIG_DM_PWM) += pwm.o
+obj-$(CONFIG_SANDBOX_FFA) += ffa.o
 obj-$(CONFIG_QFW) += qfw.o
 obj-$(CONFIG_RAM) += ram.o
 obj-y += regmap.o
diff --git a/test/dm/ffa.c b/test/dm/ffa.c
new file mode 100644
index 0000000000..85b71bd808
--- /dev/null
+++ b/test/dm/ffa.c
@@ -0,0 +1,418 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Functional tests for UCLASS_FFA  class
+ *
+ * (C) Copyright 2022 ARM Limited
+ * Abdellatif El Khlifi <abdellatif.elkhl...@arm.com>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <dm.h>
+#include <dm/test.h>
+#include "ffa.h"
+#include <test/test.h>
+#include <test/ut.h>
+#include <sandbox_arm_ffa_helper.h>
+#include <string.h>
+
+/* Functional tests for the UCLASS_FFA */
+
+static int dm_test_ffa_log(struct unit_test_state *uts, char *msg)
+{
+       char cmd[LOG_CMD_SZ] = {0};
+
+       console_record_reset();
+
+       snprintf(cmd, LOG_CMD_SZ, "echo \"%s\"", msg);
+       run_command(cmd, 0);
+
+       ut_assert_console_end();
+
+       return CMD_RET_SUCCESS;
+}
+
+static int check_fwk_version(struct ffa_prvdata *prvdata, struct 
unit_test_state *uts)
+{
+       if (prvdata->fwk_version != SANDBOX_FWK_VERSION) {
+               char msg[LOG_MSG_SZ] = {0};
+
+               snprintf(msg, LOG_MSG_SZ, "[%s]: Error: prvdata->fwk_version = 
0x%x", __func__,
+                        prvdata->fwk_version);
+               dm_test_ffa_log(uts, msg);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+static int check_endpoint_id(struct ffa_prvdata *prvdata, struct 
unit_test_state *uts)
+{
+       if (prvdata->id) {
+               char msg[LOG_MSG_SZ] = {0};
+
+               snprintf(msg, LOG_MSG_SZ, "[%s]: Error: prvdata->id = 0x%x", 
__func__, prvdata->id);
+               dm_test_ffa_log(uts, msg);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+static int check_dev(struct ffa_prvdata *prvdata, struct unit_test_state *uts)
+{
+       if (!prvdata->dev) {
+               char msg[LOG_MSG_SZ] = {0};
+
+               snprintf(msg, LOG_MSG_SZ, "[%s]: Error: device = 0x%p", 
__func__, prvdata->dev);
+               dm_test_ffa_log(uts, msg);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+static int check_rxtxbuf(struct ffa_prvdata *prvdata, struct unit_test_state 
*uts)
+{
+       if (!prvdata->pair.rxbuf && prvdata->pair.txbuf) {
+               char msg[LOG_MSG_SZ] = {0};
+
+               snprintf(msg, LOG_MSG_SZ, "[%s]: Error: rxbuf = 0x%llx txbuf = 
0x%llx", __func__,
+                        prvdata->pair.rxbuf,
+                        prvdata->pair.txbuf);
+               dm_test_ffa_log(uts, msg);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+static int check_features(struct ffa_prvdata *prvdata, struct unit_test_state 
*uts)
+{
+       u32 desc_idx;
+       char msg[LOG_MSG_SZ] = {0};
+
+       snprintf(msg,
+                LOG_MSG_SZ,
+                "[%s]: Error: FFA_RXTX_MAP features not found",
+                __func__);
+
+       for (desc_idx = 0; desc_idx < FFA_FEATURE_DESC_CNT ; desc_idx++)
+               if (prvdata->features[desc_idx].func_id == FFA_RXTX_MAP) {
+                       if (prvdata->features[desc_idx].field1 != RXTX_4K &&
+                           prvdata->features[desc_idx].field1 != RXTX_16K &&
+                           prvdata->features[desc_idx].field1 != RXTX_64K) {
+                               snprintf(msg,
+                                        LOG_MSG_SZ,
+                                        "[%s]: Error: FFA_RXTX_MAP features = 
0x%x",
+                                        __func__,
+                                        prvdata->features[desc_idx].field1);
+                                       break;
+                       }
+                       return CMD_RET_SUCCESS;
+               }
+
+       dm_test_ffa_log(uts, msg);
+       return CMD_RET_FAILURE;
+}
+
+static int check_rxbuf_mapped_flag(u32 queried_func_id,
+                                  u8 rxbuf_mapped,
+                                  struct unit_test_state *uts)
+{
+       char msg[LOG_MSG_SZ] = {0};
+
+       switch (queried_func_id) {
+       case FFA_RXTX_MAP:
+       {
+               if (rxbuf_mapped)
+                       return CMD_RET_SUCCESS;
+               break;
+       }
+       case FFA_RXTX_UNMAP:
+       {
+               if (!rxbuf_mapped)
+                       return CMD_RET_SUCCESS;
+               break;
+       }
+       default:
+               return CMD_RET_FAILURE;
+       }
+
+       snprintf(msg, LOG_MSG_SZ, "[%s]: Error: %s mapping issue", __func__,
+                (queried_func_id == FFA_RXTX_MAP ? "FFA_RXTX_MAP" : 
"FFA_RXTX_UNMAP"));
+       dm_test_ffa_log(uts, msg);
+
+       return CMD_RET_FAILURE;
+}
+
+static int check_rxbuf_release_flag(u8 rxbuf_owned, struct unit_test_state 
*uts)
+{
+       if (rxbuf_owned) {
+               char msg[LOG_MSG_SZ] = {0};
+
+               snprintf(msg, LOG_MSG_SZ, "[%s]: Error: RX buffer not 
released", __func__);
+               dm_test_ffa_log(uts, msg);
+               return CMD_RET_FAILURE;
+       }
+       return CMD_RET_SUCCESS;
+}
+
+static int  test_ffa_msg_send_direct_req(u16 part_id, struct unit_test_state 
*uts)
+{
+       struct ffa_interface_data func_data = {0};
+       struct ffa_send_direct_data msg = {0};
+       u32 pattern = 0xaabbccdd;
+       u8 cnt;
+
+       /*
+        * telling the driver which partition to use
+        */
+       func_data.data0_size = sizeof(part_id);
+       func_data.data0 = &part_id;
+
+       /*
+        * filling the message data
+        */
+       msg.a3 = pattern;
+       msg.a4 = pattern;
+       msg.a5 = pattern;
+       msg.a6 = pattern;
+       msg.a7 = pattern;
+       func_data.data1_size = sizeof(msg);
+       func_data.data1 = &msg;
+
+       ut_assertok(ffa_helper_msg_send_direct_req(&func_data));
+
+       for (cnt = 0; cnt < sizeof(struct ffa_send_direct_data) / sizeof(u32); 
cnt++)
+               ut_assertok(((u32 *)&msg)[cnt] != 0xffffffff);
+
+       return CMD_RET_SUCCESS;
+}
+
+static int test_partitions_and_comms(union ffa_partition_uuid *service_uuid,
+                                    struct sandbox_ffa_prvdata *sdx_prvdata,
+                                    struct unit_test_state *uts)
+{
+       struct ffa_interface_data func_data = {0};
+       u32 count = 0;
+       struct ffa_partition_info *parts_info;
+       u32 info_idx, exp_info_idx;
+       int ret;
+
+       /*
+        * get from the driver the count of the SPs matching the UUID
+        */
+       func_data.data0_size = sizeof(*service_uuid);
+       func_data.data0 = service_uuid;
+       func_data.data1_size = sizeof(count);
+       func_data.data1 = &count;
+
+       ut_assertok(ffa_helper_get_partitions_info(&func_data));
+
+       /* make sure partitions are detected */
+       ut_assertok(!count);
+
+       /*
+        * pre-allocate a buffer to be filled by the driver
+        * with  ffa_partition_info structs
+        */
+
+       parts_info = calloc(count, sizeof(struct ffa_partition_info));
+       ut_assertok(!parts_info);
+
+       func_data.data1_size = count * sizeof(struct ffa_partition_info);
+       func_data.data1 = parts_info;
+
+       /*
+        * ask the driver to fill the buffer with the SPs info
+        */
+       ret = ffa_helper_get_partitions_info(&func_data);
+       if (ret != FFA_ERR_STAT_SUCCESS) {
+               free(parts_info);
+               ut_assertok(ret != FFA_ERR_STAT_SUCCESS);
+       }
+
+       /*
+        * SPs found , verify the partitions information
+        */
+
+       ret = CMD_RET_FAILURE;
+
+       for (info_idx = 0; info_idx < count ; info_idx++) {
+               for (exp_info_idx = 0;
+                    exp_info_idx < sdx_prvdata->partitions.count;
+                    exp_info_idx++) {
+                       if (parts_info[info_idx].id ==
+                          sdx_prvdata->partitions.descs[exp_info_idx].info.id) 
{
+                               ret = memcmp(&parts_info[info_idx],
+                                            
&sdx_prvdata->partitions.descs[exp_info_idx]
+                                            .info,
+                                            sizeof(struct ffa_partition_info));
+                               if (ret)
+                                       free(parts_info);
+                               ut_assertok(ret != 0);
+                               /* send and receive data from the current 
partition */
+                               
test_ffa_msg_send_direct_req(parts_info[info_idx].id, uts);
+                       }
+                       ret = CMD_RET_SUCCESS;
+               }
+       }
+
+       free(parts_info);
+
+       /* Verify  expected partitions found in the emulated secure world*/
+       ut_assertok(ret != CMD_RET_SUCCESS);
+
+       return CMD_RET_SUCCESS;
+}
+
+static int dm_test_ffa_ack(struct unit_test_state *uts)
+{
+       struct ffa_prvdata *prvdata = NULL;
+       struct sandbox_ffa_prvdata *sdx_prvdata = NULL;
+       struct ffa_interface_data func_data = {0};
+       u8 rxbuf_flag = 0;
+       union ffa_partition_uuid svc1_uuid = { .bytes = 
{SANDBOX_SERVICE1_UUID_DATA}};
+       union ffa_partition_uuid svc2_uuid = { .bytes = 
{SANDBOX_SERVICE2_UUID_DATA}};
+       int ret;
+
+       /* get a pointer to the FF-A core and sandbox drivers private data */
+       func_data.data0 = &prvdata;
+       func_data.data0_size = sizeof(prvdata);
+       func_data.data1 = &sdx_prvdata;
+       func_data.data1_size = sizeof(sdx_prvdata);
+
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_VERSION, 
&func_data));
+
+       /* clear private data before the new test */
+       memset(prvdata, 0, sizeof(struct ffa_prvdata));
+       memset(sdx_prvdata, 0, sizeof(struct sandbox_ffa_prvdata));
+
+       /*  test probing FF-A devices */
+       ut_assertok(ffa_helper_bus_discover());
+       ut_assertok(check_dev(prvdata, uts));
+
+       /* test FFA_VERSION */
+       ut_assertok(check_fwk_version(prvdata, uts));
+
+       /* test FFA_ID_GET */
+       ut_assertok(check_endpoint_id(prvdata, uts));
+
+       /* test FFA_FEATURES */
+       ut_assertok(check_features(prvdata, uts));
+
+       /*  test core RX/TX buffers */
+       ut_assertok(check_rxtxbuf(prvdata, uts));
+
+       /* test FFA_RXTX_MAP */
+       func_data.data0 = &rxbuf_flag;
+       func_data.data0_size = sizeof(rxbuf_flag);
+
+       rxbuf_flag = 0;
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_RXTX_MAP, 
&func_data));
+       ut_assertok(check_rxbuf_mapped_flag(FFA_RXTX_MAP, rxbuf_flag, uts));
+
+       /* FFA_PARTITION_INFO_GET / FFA_MSG_SEND_DIRECT_REQ */
+       ret = test_partitions_and_comms(&svc1_uuid, sdx_prvdata, uts);
+       ut_assertok(ret != CMD_RET_SUCCESS);
+
+       /* test FFA_RX_RELEASE */
+       rxbuf_flag = 1;
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_RX_RELEASE, 
&func_data));
+       ut_assertok(check_rxbuf_release_flag(rxbuf_flag, uts));
+
+       /* FFA_PARTITION_INFO_GET / FFA_MSG_SEND_DIRECT_REQ */
+       ret = test_partitions_and_comms(&svc2_uuid, sdx_prvdata, uts);
+       ut_assertok(ret != CMD_RET_SUCCESS);
+
+       /* test FFA_RX_RELEASE */
+       rxbuf_flag = 1;
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_RX_RELEASE, 
&func_data));
+       ut_assertok(check_rxbuf_release_flag(rxbuf_flag, uts));
+
+       /* test FFA_RXTX_UNMAP */
+       ut_assertok(ffa_helper_unmap_rxtx_buffers());
+
+       rxbuf_flag = 1;
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_RXTX_UNMAP, 
&func_data));
+       ut_assertok(check_rxbuf_mapped_flag(FFA_RXTX_UNMAP, rxbuf_flag, uts));
+
+       return CMD_RET_SUCCESS;
+}
+
+DM_TEST(dm_test_ffa_ack, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
+
+static int dm_test_ffa_nack(struct unit_test_state *uts)
+{
+       struct ffa_prvdata *prvdata = NULL;
+       struct sandbox_ffa_prvdata *sdx_prvdata = NULL;
+
+       struct ffa_interface_data func_data = {0};
+       union ffa_partition_uuid valid_svc_uuid = { .bytes = 
{SANDBOX_SERVICE1_UUID_DATA}};
+       union ffa_partition_uuid unvalid_svc_uuid = { .bytes = 
{SANDBOX_SERVICE3_UUID_DATA}};
+       struct ffa_send_direct_data msg = {0};
+       int ret;
+       u32 count = 0;
+       u16 part_id = 0;
+
+       /* get a pointer to the FF-A core and sandbox drivers private data */
+       func_data.data0 = &prvdata;
+       func_data.data0_size = sizeof(prvdata);
+       func_data.data1 = &sdx_prvdata;
+       func_data.data1_size = sizeof(sdx_prvdata);
+       ut_assertok(sandbox_ffa_helper_query_core_state(FFA_VERSION, 
&func_data));
+
+       /* clear private data before the new test */
+       memset(prvdata, 0, sizeof(struct ffa_prvdata));
+       memset(sdx_prvdata, 0, sizeof(struct sandbox_ffa_prvdata));
+
+       /* get partitions info before probing the core driver */
+       func_data.data0_size = sizeof(union ffa_partition_uuid);
+       func_data.data0 = &unvalid_svc_uuid;
+       func_data.data1_size = sizeof(count);
+       func_data.data1 = &count;
+
+       ret = ffa_helper_get_partitions_info(&func_data);
+       ut_assertok(ret != -ENODEV);
+
+       /*  probing FF-A devices */
+       ut_assertok(ffa_helper_bus_discover());
+       ut_assertok(check_dev(prvdata, uts));
+
+       /* query partition info using  invalid arguments  */
+       func_data.data0_size = 0;
+       ret = ffa_helper_get_partitions_info(&func_data);
+       ut_assertok(ret != -EINVAL);
+
+       /* query partition info using an invalid UUID  */
+       func_data.data0_size = sizeof(union ffa_partition_uuid);
+       func_data.data0 = &unvalid_svc_uuid;
+       func_data.data1_size = sizeof(count);
+       func_data.data1 = &count;
+
+       ret = ffa_helper_get_partitions_info(&func_data);
+       ut_assertok(ret != -EPERM);
+
+       /* query partition info using a valid UUID  */
+       func_data.data0 = &valid_svc_uuid;
+       count = 0;
+       ret = ffa_helper_get_partitions_info(&func_data);
+       /* make sure partitions are detected */
+       ut_assertok(ret != FFA_ERR_STAT_SUCCESS);
+       ut_assertok(!count);
+
+       /* send data to an invalid partition */
+       func_data.data0_size = sizeof(part_id);
+       func_data.data0 = &part_id;
+       func_data.data1_size = sizeof(msg);
+       func_data.data1 = &msg;
+
+       ret = ffa_helper_msg_send_direct_req(&func_data);
+       ut_assertok(ret != -EPERM);
+
+       /* send data to a valid partition */
+       part_id = ffa_priv_data.partitions.descs[0].info.id;
+       ret = ffa_helper_msg_send_direct_req(&func_data);
+       ut_assertok(ret != FFA_ERR_STAT_SUCCESS);
+
+       return CMD_RET_SUCCESS;
+}
+
+DM_TEST(dm_test_ffa_nack, UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
diff --git a/test/dm/ffa.h b/test/dm/ffa.h
new file mode 100644
index 0000000000..11c7980fcf
--- /dev/null
+++ b/test/dm/ffa.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2022 ARM Limited
+ * Abdellatif El Khlifi <abdellatif.elkhl...@arm.com>
+ */
+
+#ifndef __TEST_DM_FFA_H
+#define __TEST_DM_FFA_H
+
+#define SANDBOX_FWK_VERSION (0x10000)
+
+#define LOG_MSG_SZ (100)
+#define LOG_CMD_SZ (LOG_MSG_SZ * 2)
+
+/* service 3  UUID binary data (little-endian format) */
+#define SANDBOX_SERVICE3_UUID_DATA     \
+       0xfd, 0x42, 0xd5, 0x33, \
+       0x19, 0xa6, 0x42, 0x09, \
+       0x9c, 0xc1, 0x2d, 0x72, \
+       0xcd, 0xd9, 0x98, 0x89
+
+#endif /*__TEST_DM_FFA_H */
-- 
2.17.1

Reply via email to