test(spm): run ffa_tests on ivy

Move cactus_test_ffa.c to a common directory for SPs so the
ivy partition can also run the same tests.

We also create a sp_platform_def.h file which contains various
plaform defines required for the tests. This header is defined
in both cactus and ivy directories and then fvp and tc0
subdirectories within those. The appropriate header for the sp
and platform being built is then included in the build scripts.

Signed-off-by: Daniel Boulby <daniel.boulby@arm.com>
Change-Id: If65d099d43cd930ef730539b5ad1596e686f788a
diff --git a/spm/common/sp_def.h b/spm/common/sp_def.h
new file mode 100644
index 0000000..2b26b68
--- /dev/null
+++ b/spm/common/sp_def.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef SP_DEF_H
+#define SP_DEF_H
+
+#include <utils_def.h>
+#include <sp_platform_def.h>
+
+/*
+ * Layout of the Secure Partition image.
+ */
+
+/* Up to 2 MiB at an arbitrary address that doesn't overlap the devices. */
+#define SP_IMAGE_BASE		ULL(0x1000)
+#define SP_IMAGE_SIZE		ULL(0x200000)
+
+/* Memory reserved for stacks */
+#define SP_STACKS_SIZE		ULL(0x1000)
+
+/*
+ * RX/TX buffer used by VM's in SPM for memory sharing
+ * Each VM allocated 2 pages, one for RX and one for TX buffer.
+ */
+#define SP_RX_BASE			PLAT_SP_RX_BASE
+#define SP_TX_BASE			SP_RX_BASE + PAGE_SIZE
+#define SP_RX_TX_SIZE			PAGE_SIZE * 2
+
+/*
+ * RX/TX buffer helpers.
+ */
+#define get_sp_rx_start(sp_id) (SP_RX_BASE \
+				+ (((sp_id & 0x7FFFU) - 1U) * SP_RX_TX_SIZE))
+#define get_sp_rx_end(sp_id) (SP_RX_BASE \
+			      + (((sp_id & 0x7FFFU) - 1U) * SP_RX_TX_SIZE) \
+			      + PAGE_SIZE)
+#define get_sp_tx_start(sp_id) (SP_TX_BASE + \
+				(((sp_id & 0x7FFFU) - 1U) * SP_RX_TX_SIZE))
+#define get_sp_tx_end(sp_id) (SP_TX_BASE \
+			      + (((sp_id & 0x7FFFU) - 1U) * SP_RX_TX_SIZE) \
+			      + PAGE_SIZE)
+
+#endif /* SP_DEF_H */
diff --git a/spm/common/sp_tests/sp_test_ffa.c b/spm/common/sp_tests/sp_test_ffa.c
new file mode 100644
index 0000000..f81505a
--- /dev/null
+++ b/spm/common/sp_tests/sp_test_ffa.c
@@ -0,0 +1,205 @@
+/*
+ * Copyright (c) 2018-2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#include <assert.h>
+#include <debug.h>
+#include <errno.h>
+
+#include <sp_def.h>
+#include <ffa_endpoints.h>
+#include <sp_helpers.h>
+#include <spm_helpers.h>
+#include <spm_common.h>
+
+#include <lib/libc/string.h>
+
+/* FFA version test helpers */
+#define FFA_MAJOR 1U
+#define FFA_MINOR 1U
+
+static uint32_t spm_version;
+
+static const struct ffa_uuid sp_uuids[] = {
+		{PRIMARY_UUID}, {SECONDARY_UUID}, {TERTIARY_UUID}, {IVY_UUID}
+	};
+static const struct ffa_uuid null_uuid = { .uuid = {0} };
+
+static const struct ffa_partition_info ffa_expected_partition_info[] = {
+	/* Primary partition info */
+	{
+		.id = SP_ID(1),
+		.exec_context = PRIMARY_EXEC_CTX_COUNT,
+		.properties = (FFA_PARTITION_DIRECT_REQ_RECV |
+			       FFA_PARTITION_DIRECT_REQ_SEND |
+			       FFA_PARTITION_NOTIFICATION),
+		.uuid = sp_uuids[0]
+	},
+	/* Secondary partition info */
+	{
+		.id = SP_ID(2),
+		.exec_context = SECONDARY_EXEC_CTX_COUNT,
+		.properties = (FFA_PARTITION_DIRECT_REQ_RECV |
+			       FFA_PARTITION_DIRECT_REQ_SEND |
+			       FFA_PARTITION_NOTIFICATION),
+		.uuid = sp_uuids[1]
+	},
+	/* Tertiary partition info */
+	{
+		.id = SP_ID(3),
+		.exec_context = TERTIARY_EXEC_CTX_COUNT,
+		.properties = (FFA_PARTITION_DIRECT_REQ_RECV |
+			       FFA_PARTITION_DIRECT_REQ_SEND |
+			       FFA_PARTITION_NOTIFICATION),
+		.uuid = sp_uuids[2]
+	},
+	/* Ivy partition info */
+	{
+		.id = SP_ID(4),
+		.exec_context = IVY_EXEC_CTX_COUNT,
+		.properties = (FFA_PARTITION_DIRECT_REQ_RECV |
+			       FFA_PARTITION_DIRECT_REQ_SEND),
+		.uuid = sp_uuids[3]
+	}
+};
+
+/*
+ * Test FFA_FEATURES interface.
+ */
+static void ffa_features_test(void)
+{
+	const char *test_features = "FFA Features interface";
+	struct ffa_value ffa_ret;
+	unsigned int expected_ret;
+	const struct ffa_features_test *ffa_feature_test_target;
+	unsigned int i, test_target_size =
+		get_ffa_feature_test_target(&ffa_feature_test_target);
+	struct ffa_features_test test_target;
+
+
+	announce_test_section_start(test_features);
+
+	for (i = 0U; i < test_target_size; i++) {
+		test_target = ffa_feature_test_target[i];
+
+		announce_test_start(test_target.test_name);
+
+		ffa_ret = ffa_features(test_target.feature);
+		expected_ret = FFA_VERSION_COMPILED
+				>= test_target.version_added ?
+				test_target.expected_ret : FFA_ERROR;
+
+		expect(ffa_func_id(ffa_ret), expected_ret);
+		if (expected_ret == FFA_ERROR) {
+			expect(ffa_error_code(ffa_ret), FFA_ERROR_NOT_SUPPORTED);
+		}
+
+		announce_test_end(test_target.test_name);
+	}
+
+	announce_test_section_end(test_features);
+}
+
+static void ffa_partition_info_wrong_test(void)
+{
+	const char *test_wrong_uuid = "Request wrong UUID";
+	const struct ffa_uuid uuid = { .uuid = {1} };
+
+	announce_test_start(test_wrong_uuid);
+
+	struct ffa_value ret = ffa_partition_info_get(uuid);
+	expect(ffa_func_id(ret), FFA_ERROR);
+	expect(ffa_error_code(ret), FFA_ERROR_INVALID_PARAMETER);
+
+	announce_test_end(test_wrong_uuid);
+}
+
+static void ffa_partition_info_get_test(struct mailbox_buffers *mb)
+{
+	const char *test_partition_info = "FFA Partition info interface";
+
+	announce_test_section_start(test_partition_info);
+
+	expect(ffa_partition_info_helper(mb, sp_uuids[2],
+		&ffa_expected_partition_info[2], 1), true);
+
+	expect(ffa_partition_info_helper(mb, sp_uuids[1],
+		&ffa_expected_partition_info[1], 1), true);
+
+	expect(ffa_partition_info_helper(mb, sp_uuids[0],
+		&ffa_expected_partition_info[0], 1), true);
+
+	expect(ffa_partition_info_helper(mb, null_uuid,
+		ffa_expected_partition_info,
+		ARRAY_SIZE(ffa_expected_partition_info)), true);
+
+	ffa_partition_info_wrong_test();
+
+	announce_test_section_end(test_partition_info);
+}
+
+void ffa_version_test(void)
+{
+	const char *test_ffa_version = "FFA Version interface";
+
+	announce_test_start(test_ffa_version);
+
+	struct ffa_value ret = ffa_version(MAKE_FFA_VERSION(FFA_MAJOR,
+							    FFA_MINOR));
+
+	spm_version = (uint32_t)ret.fid;
+
+	bool ffa_version_compatible =
+		((spm_version >> FFA_VERSION_MAJOR_SHIFT) == FFA_MAJOR &&
+		 (spm_version & FFA_VERSION_MINOR_MASK) >= FFA_MINOR);
+
+	VERBOSE("FFA_VERSION returned %u.%u; Compatible: %i\n",
+		spm_version >> FFA_VERSION_MAJOR_SHIFT,
+		spm_version & FFA_VERSION_MINOR_MASK,
+		(int)ffa_version_compatible);
+
+	expect((int)ffa_version_compatible, (int)true);
+
+	announce_test_end(test_ffa_version);
+}
+
+void ffa_spm_id_get_test(void)
+{
+	const char *test_spm_id_get = "FFA_SPM_ID_GET SMC Function";
+
+	announce_test_start(test_spm_id_get);
+
+	if (spm_version >= MAKE_FFA_VERSION(1, 1)) {
+		struct ffa_value ret = ffa_spm_id_get();
+
+		expect(ffa_func_id(ret), FFA_SUCCESS_SMC32);
+
+		ffa_id_t spm_id = ffa_endpoint_id(ret);
+
+		VERBOSE("SPM ID = 0x%x\n", spm_id);
+		/*
+		 * Check the SPMC value given in the fvp_spmc_manifest
+		 * is returned.
+		 */
+		expect(spm_id, SPMC_ID);
+	} else {
+		NOTICE("FFA_SPM_ID_GET not supported in this version of FF-A."
+			" Test skipped.\n");
+	}
+	announce_test_end(test_spm_id_get);
+}
+
+void ffa_tests(struct mailbox_buffers *mb)
+{
+	const char *test_ffa = "FFA Interfaces";
+
+	announce_test_section_start(test_ffa);
+
+	ffa_features_test();
+	ffa_version_test();
+	ffa_spm_id_get_test();
+	ffa_partition_info_get_test(mb);
+
+	announce_test_section_end(test_ffa);
+}
diff --git a/spm/common/sp_tests/sp_tests.h b/spm/common/sp_tests/sp_tests.h
new file mode 100644
index 0000000..1039ba5
--- /dev/null
+++ b/spm/common/sp_tests/sp_tests.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2017-2021, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef CACTUS_TESTS_H
+#define CACTUS_TESTS_H
+
+#include <spm_common.h>
+
+/*
+ * Test functions
+ */
+
+void ffa_tests(struct mailbox_buffers *mb);
+
+/*
+ * Test other things like the version number returned by SPM.
+ */
+void misc_tests(void);
+
+/*
+ * The Arm TF is responsible for setting up system registers on behalf of the
+ * Secure Partition. For example, TF is supposed to allow Secure Partitions to
+ * perform cache maintenance operations (by setting the SCTLR_EL1.UCI bit).
+ *
+ * This function attempts to verify that we indeed have access to these system
+ * features from S-EL0. These tests report their results on the UART. They do
+ * not recover from a failure : when an error is encountered they will most
+ * likely trigger an exception into S-EL1.
+ */
+void system_setup_tests(void);
+
+/*
+ * Exercise the SP_MEMORY_ATTRIBUTES_SET_AARCH64 SMC interface. A variety of
+ * valid and invalid requests to change memory attributes are tested.
+ *
+ * These tests report their results on the UART. They do not recover from a
+ * failure : when an error is encountered they endlessly loop.
+ */
+void mem_attr_changes_tests(void);
+
+#endif /* CACTUS_TESTS_H */