feat: tftf realm extension

This patch adds Realm payload management capabilities to TFTF
to act as a NS Host, it includes creation and destruction of a Realm,
mapping of protected data and creation of all needed RTT levels,
sharing of NS memory buffer from Host to Realm by mapping of
unprotected IPA, create REC and auxiliary granules, exit Realm
using RSI_HOST_CALL ABI.

Older realm_payload name is used now for only R-EL1 test cases,
RMI and SPM test cases have been moved to new file tests-rmi-spm.

New TFTF_MAX_IMAGE_SIZE argument added to FVP platform.mk,
as an offset from where R-EL1 payload memory resources start.

Signed-off-by: Nabil Kahlouche <nabil.kahlouche@arm.com>
Change-Id: Ida4cfd334795879d55924bb33b9b77182a3dcef7
diff --git a/realm/realm_payload_main.c b/realm/realm_payload_main.c
new file mode 100644
index 0000000..bd4dec7
--- /dev/null
+++ b/realm/realm_payload_main.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ *
+ */
+
+#include <stdio.h>
+
+#include <debug.h>
+#include <host_realm_helper.h>
+#include <host_shared_data.h>
+#include "realm_def.h"
+#include <realm_rsi.h>
+#include <tftf_lib.h>
+
+/*
+ * This function reads sleep time in ms from shared buffer and spins PE in a loop
+ * for that time period.
+ */
+static void realm_sleep_cmd(void)
+{
+	uint64_t sleep = realm_shared_data_get_host_val(HOST_SLEEP_INDEX);
+
+	INFO("REALM_PAYLOAD: Realm payload going to sleep for %llums\n", sleep);
+	waitms(sleep);
+}
+
+/*
+ * This function requests RSI/ABI version from RMM.
+ */
+static void realm_get_rsi_version(void)
+{
+	u_register_t version;
+
+	version = rsi_get_version();
+	if (version == (u_register_t)SMC_UNKNOWN) {
+		ERROR("SMC_RSI_ABI_VERSION failed (%ld)", (long)version);
+		return;
+	}
+
+	INFO("RSI ABI version %u.%u (expected: %u.%u)",
+	RSI_ABI_VERSION_GET_MAJOR(version),
+	RSI_ABI_VERSION_GET_MINOR(version),
+	RSI_ABI_VERSION_GET_MAJOR(RSI_ABI_VERSION),
+	RSI_ABI_VERSION_GET_MINOR(RSI_ABI_VERSION));
+}
+
+/*
+ * This is the entry function for Realm payload, it first requests the shared buffer
+ * IPA address from Host using HOST_CALL/RSI, it reads the command to be executed,
+ * performs the request, and returns to Host with the execution state SUCCESS/FAILED
+ *
+ * Host in NS world requests Realm to execute certain operations using command
+ * depending on the test case the Host wants to perform.
+ */
+void realm_payload_main(void)
+{
+	uint8_t cmd = 0U;
+	bool test_succeed = false;
+
+	realm_set_shared_structure((host_shared_data_t *)rsi_get_ns_buffer());
+	if (realm_get_shared_structure() != NULL) {
+		cmd = realm_shared_data_get_realm_cmd();
+		switch (cmd) {
+		case REALM_SLEEP_CMD:
+			realm_sleep_cmd();
+			test_succeed = true;
+			break;
+		case REALM_GET_RSI_VERSION:
+			realm_get_rsi_version();
+			test_succeed = true;
+			break;
+		default:
+			INFO("REALM_PAYLOAD: %s invalid cmd=%hhu", __func__, cmd);
+			break;
+		}
+	}
+
+	if (test_succeed) {
+		rsi_exit_to_host(HOST_CALL_EXIT_SUCCESS_CMD);
+	} else {
+		rsi_exit_to_host(HOST_CALL_EXIT_FAILED_CMD);
+	}
+}