Exercise FF-A runtime helpers via sandbox DM tests

Test the successful runtime direct-message round trip and the invalid
destination-partition case, using the shared FF-A prepare step so the
sandbox emulator has initialized partition state before exercising the
runtime-only messaging helpers.
Add a dedicated no-context test that calls
ffa_sync_send_receive_runtime() before enabling the runtime context and
checks that it returns -EPERM. Reset the resident FF-A runtime context
around the transport tests so the runtime flag and private data do not
leak across test cases.
Split the ffa_to_std_errno() checks into a separate DM test so
error-mapping regressions are reported independently from transport
failures. Cover both the valid mappings and the boundary inputs that
must return -EINVAL.

Replace the hard-coded synthetic partition execution-context and
property values in ffa-emul-uclass.c with the shared
SANDBOX_SP*_EXEC_CTXT and SANDBOX_SP*_PROPERTIES macros. This lets the
sandbox emulator and tests reuse a single definition of the synthetic
partition metadata.

Reviewed-by: Simon Glass <[email protected]>
Signed-off-by: Harsimran Singh Tungal <[email protected]>
---
 arch/sandbox/include/asm/sandbox_arm_ffa.h |  16 ++-
 drivers/firmware/arm-ffa/ffa-emul-uclass.c |  37 +++++--
 test/dm/Makefile                           |   3 +-
 test/dm/ffa_runtime.c                      | 123 +++++++++++++++++++++
 4 files changed, 166 insertions(+), 13 deletions(-)
 create mode 100644 test/dm/ffa_runtime.c

diff --git a/arch/sandbox/include/asm/sandbox_arm_ffa.h 
b/arch/sandbox/include/asm/sandbox_arm_ffa.h
index be2790f4960..a20eb159b73 100644
--- a/arch/sandbox/include/asm/sandbox_arm_ffa.h
+++ b/arch/sandbox/include/asm/sandbox_arm_ffa.h
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0+ */
 /*
- * Copyright 2022-2023 Arm Limited and/or its affiliates 
<[email protected]>
+ * Copyright 2022-2023, 2026 Arm Limited and/or its affiliates 
<[email protected]>
  *
  * Authors:
  *   Abdellatif El Khlifi <[email protected]>
@@ -26,6 +26,20 @@
 #define SANDBOX_SP3_ID 0x6452
 #define SANDBOX_SP4_ID 0x7814
 
+/*
+ * The sandbox FF-A emulator uses fixed, synthetic execution-context counts and
+ * property bitfields for each partition.
+ */
+#define SANDBOX_SP1_EXEC_CTXT 0x5687
+#define SANDBOX_SP2_EXEC_CTXT 0x9587
+#define SANDBOX_SP3_EXEC_CTXT 0x7687
+#define SANDBOX_SP4_EXEC_CTXT 0x1487
+
+#define SANDBOX_SP1_PROPERTIES 0x89325621
+#define SANDBOX_SP2_PROPERTIES 0x45325621
+#define SANDBOX_SP3_PROPERTIES 0x23325621
+#define SANDBOX_SP4_PROPERTIES 0x70325621
+
 /* Invalid service UUID (no matching SP) */
 #define SANDBOX_SERVICE3_UUID  "55d532ed-0942-e699-722d-c09ca798d9cd"
 
diff --git a/drivers/firmware/arm-ffa/ffa-emul-uclass.c 
b/drivers/firmware/arm-ffa/ffa-emul-uclass.c
index d270f7b614e..3351d2e2edd 100644
--- a/drivers/firmware/arm-ffa/ffa-emul-uclass.c
+++ b/drivers/firmware/arm-ffa/ffa-emul-uclass.c
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
- * Copyright 2022-2023 Arm Limited and/or its affiliates 
<[email protected]>
+ * Copyright 2022-2023, 2026 Arm Limited and/or its affiliates 
<[email protected]>
  *
  * Authors:
  *   Abdellatif El Khlifi <[email protected]>
@@ -19,42 +19,57 @@
 /* The partitions (SPs) table */
 static struct ffa_partition_desc sandbox_partitions[SANDBOX_PARTITIONS_CNT] = {
        {
-               .info = { .id = SANDBOX_SP1_ID, .exec_ctxt = 0x5687, 
.properties = 0x89325621 },
+               .info = {
+                       .id = SANDBOX_SP1_ID,
+                       .exec_ctxt = SANDBOX_SP1_EXEC_CTXT,
+                       .properties = SANDBOX_SP1_PROPERTIES,
+               },
                .sp_uuid = {
                        .a1 = SANDBOX_SERVICE1_UUID_A1,
                        .a2 = SANDBOX_SERVICE1_UUID_A2,
                        .a3 = SANDBOX_SERVICE1_UUID_A3,
                        .a4 = SANDBOX_SERVICE1_UUID_A4,
-               }
+               },
        },
        {
-               .info = { .id = SANDBOX_SP3_ID, .exec_ctxt = 0x7687, 
.properties = 0x23325621 },
+               .info = {
+                       .id = SANDBOX_SP3_ID,
+                       .exec_ctxt = SANDBOX_SP3_EXEC_CTXT,
+                       .properties = SANDBOX_SP3_PROPERTIES,
+               },
                .sp_uuid = {
                        .a1 = SANDBOX_SERVICE2_UUID_A1,
                        .a2 = SANDBOX_SERVICE2_UUID_A2,
                        .a3 = SANDBOX_SERVICE2_UUID_A3,
                        .a4 = SANDBOX_SERVICE2_UUID_A4,
-               }
+               },
        },
        {
-               .info = { .id = SANDBOX_SP2_ID, .exec_ctxt = 0x9587, 
.properties = 0x45325621 },
+               .info = {
+                       .id = SANDBOX_SP2_ID,
+                       .exec_ctxt = SANDBOX_SP2_EXEC_CTXT,
+                       .properties = SANDBOX_SP2_PROPERTIES,
+               },
                .sp_uuid = {
                        .a1 = SANDBOX_SERVICE1_UUID_A1,
                        .a2 = SANDBOX_SERVICE1_UUID_A2,
                        .a3 = SANDBOX_SERVICE1_UUID_A3,
                        .a4 = SANDBOX_SERVICE1_UUID_A4,
-               }
+               },
        },
        {
-               .info = { .id = SANDBOX_SP4_ID, .exec_ctxt = 0x1487, 
.properties = 0x70325621 },
+               .info = {
+                       .id = SANDBOX_SP4_ID,
+                       .exec_ctxt = SANDBOX_SP4_EXEC_CTXT,
+                       .properties = SANDBOX_SP4_PROPERTIES,
+               },
                .sp_uuid = {
                        .a1 = SANDBOX_SERVICE2_UUID_A1,
                        .a2 = SANDBOX_SERVICE2_UUID_A2,
                        .a3 = SANDBOX_SERVICE2_UUID_A3,
                        .a4 = SANDBOX_SERVICE2_UUID_A4,
-               }
-       }
-
+               },
+       },
 };
 
 /* The emulator functions */
diff --git a/test/dm/Makefile b/test/dm/Makefile
index 0e3c63568dd..102b1e98284 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -1,7 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0+
 #
 # Copyright (c) 2013 Google, Inc
-# Copyright 2022-2023 Arm Limited and/or its affiliates 
<[email protected]>
+# Copyright 2022-2023, 2026 Arm Limited and/or its affiliates 
<[email protected]>
 
 # Tests for particular subsystems - when enabling driver model for a new
 # subsystem you must add sandbox tests here.
@@ -98,6 +98,7 @@ obj-$(CONFIG_ACPI_PMC) += pmc.o
 obj-$(CONFIG_DM_PMIC) += pmic.o
 obj-$(CONFIG_DM_PWM) += pwm.o
 obj-$(CONFIG_ARM_FFA_TRANSPORT) += ffa.o
+obj-$(CONFIG_ARM_FFA_TRANSPORT) += ffa_runtime.o
 obj-$(CONFIG_QFW) += qfw.o
 obj-$(CONFIG_RAM) += ram.o
 obj-y += regmap.o
diff --git a/test/dm/ffa_runtime.c b/test/dm/ffa_runtime.c
new file mode 100644
index 00000000000..3820628f86b
--- /dev/null
+++ b/test/dm/ffa_runtime.c
@@ -0,0 +1,123 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Functional tests for FF-A runtime helpers
+ *
+ * Copyright 2026 Arm Limited and/or its affiliates 
<[email protected]>
+ *
+ * Authors:
+ *      Harsimran Singh Tungal <[email protected]>
+ */
+
+#include <dm.h>
+#include <dm/test.h>
+#include <asm/sandbox_arm_ffa.h>
+#include <asm/sandbox_arm_ffa_priv.h>
+#include <arm_ffa_runtime.h>
+#include <test/ut.h>
+
+static int ffa_runtime_prepare(struct unit_test_state *uts, u16 *sp_id)
+{
+       struct ffa_partition_desc *descs;
+       u32 count;
+       struct udevice *dev;
+       const char *svc_uuid = SANDBOX_SERVICE1_UUID;
+
+       ut_assertok(uclass_first_device_err(UCLASS_FFA, &dev));
+       ut_assertok(ffa_partition_info_get(dev, svc_uuid, &count, &descs));
+       ut_assert(count > 0);
+
+       if (sp_id)
+               *sp_id = descs[0].info.id;
+
+       return 0;
+}
+
+static void ffa_runtime_teardown(void)
+{
+       ffa_runtime_context_reset();
+}
+
+static int ffa_runtime_setup(struct unit_test_state *uts, u16 *sp_id)
+{
+       ffa_runtime_teardown();
+       ut_assert(!ffa_runtime_context_ready());
+       ut_assertok(ffa_runtime_prepare(uts, sp_id));
+
+       ffa_copy_runtime_priv(&(struct ffa_priv_runtime){
+               .id = NS_PHYS_ENDPOINT_ID,
+       });
+       ffa_runtime_context_enable();
+
+       /* Ensure runtime context is available before attempting runtime-only 
paths */
+       ut_assert(ffa_runtime_context_ready());
+
+       return 0;
+}
+
+static int dm_test_ffa_runtime_no_context(struct unit_test_state *uts)
+{
+       struct ffa_send_direct_data msg = {0};
+       u16 sp_id;
+
+       ffa_runtime_teardown();
+       ut_assert(!ffa_runtime_context_ready());
+
+       ut_assertok(ffa_runtime_prepare(uts, &sp_id));
+
+       /* Runtime messaging must fail until the runtime context is enabled */
+       ut_asserteq(-EPERM, ffa_sync_send_receive_runtime(sp_id, &msg, true));
+
+       ffa_runtime_teardown();
+       return 0;
+}
+
+DM_TEST(dm_test_ffa_runtime_no_context, UTF_SCAN_FDT | UTF_CONSOLE);
+
+static int dm_test_ffa_to_std_errno(struct unit_test_state *uts)
+{
+       ut_asserteq(-EINVAL, ffa_to_std_errno(0));
+       ut_asserteq(-EINVAL, ffa_to_std_errno(MAX_NUMBER_FFA_ERR));
+       ut_asserteq(-EINVAL, ffa_to_std_errno(-MAX_NUMBER_FFA_ERR));
+       ut_asserteq(-EINVAL, ffa_to_std_errno(-(MAX_NUMBER_FFA_ERR + 1)));
+
+       ut_asserteq(-EINVAL, ffa_to_std_errno(-INVALID_PARAMETERS));
+       ut_asserteq(-EOPNOTSUPP, ffa_to_std_errno(-NOT_SUPPORTED));
+
+       return 0;
+}
+
+DM_TEST(dm_test_ffa_to_std_errno, UTF_SCAN_FDT | UTF_CONSOLE);
+
+static int dm_test_ffa_runtime_ack(struct unit_test_state *uts)
+{
+       struct ffa_send_direct_data msg = {0};
+       u16 sp_id;
+       u8 cnt;
+
+       ut_assertok(ffa_runtime_setup(uts, &sp_id));
+
+       /* Runtime messaging should reuse the sandbox emulator and return 0xff 
pattern */
+       ut_assertok(ffa_sync_send_receive_runtime(sp_id, &msg, true));
+       for (cnt = 0; cnt < sizeof(struct ffa_send_direct_data) / sizeof(u64); 
cnt++)
+               ut_asserteq_64(-1UL, ((u64 *)&msg)[cnt]);
+
+       ffa_runtime_teardown();
+       return 0;
+}
+
+DM_TEST(dm_test_ffa_runtime_ack, UTF_SCAN_FDT | UTF_CONSOLE);
+
+static int dm_test_ffa_runtime_nack(struct unit_test_state *uts)
+{
+       struct ffa_send_direct_data msg = {0};
+
+       ut_assertok(ffa_runtime_setup(uts, NULL));
+
+       /* Invalid partition IDs must be rejected and mapped to -EINVAL */
+       ut_asserteq(-EINVAL, ffa_sync_send_receive_runtime(0xffff, &msg, true));
+
+       ffa_runtime_teardown();
+       return 0;
+}
+
+DM_TEST(dm_test_ffa_runtime_nack, UTF_SCAN_FDT | UTF_CONSOLE);
-- 
2.34.1

Reply via email to