Trusted Firmware-A Tests, version 2.0

This is the first public version of the tests for the Trusted
Firmware-A project. Please see the documentation provided in the
source tree for more details.

Change-Id: I6f3452046a1351ac94a71b3525c30a4ca8db7867
Signed-off-by: Sandrine Bailleux <sandrine.bailleux@arm.com>
Co-authored-by: amobal01 <amol.balasokamble@arm.com>
Co-authored-by: Antonio Nino Diaz <antonio.ninodiaz@arm.com>
Co-authored-by: Asha R <asha.r@arm.com>
Co-authored-by: Chandni Cherukuri <chandni.cherukuri@arm.com>
Co-authored-by: David Cunado <david.cunado@arm.com>
Co-authored-by: Dimitris Papastamos <dimitris.papastamos@arm.com>
Co-authored-by: Douglas Raillard <douglas.raillard@arm.com>
Co-authored-by: dp-arm <dimitris.papastamos@arm.com>
Co-authored-by: Jeenu Viswambharan <jeenu.viswambharan@arm.com>
Co-authored-by: Jonathan Wright <jonathan.wright@arm.com>
Co-authored-by: Kévin Petit <kevin.petit@arm.com>
Co-authored-by: Roberto Vargas <roberto.vargas@arm.com>
Co-authored-by: Sathees Balya <sathees.balya@arm.com>
Co-authored-by: Shawon Roy <Shawon.Roy@arm.com>
Co-authored-by: Soby Mathew <soby.mathew@arm.com>
Co-authored-by: Thomas Abraham <thomas.abraham@arm.com>
Co-authored-by: Vikram Kanigiri <vikram.kanigiri@arm.com>
Co-authored-by: Yatharth Kochar <yatharth.kochar@arm.com>
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
new file mode 100644
index 0000000..942e9f8
--- /dev/null
+++ b/include/common/test_helpers.h
@@ -0,0 +1,166 @@
+/*
+ * Copyright (c) 2018, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef __TEST_HELPERS_H__
+#define __TEST_HELPERS_H__
+
+#include <plat_topology.h>
+#include <psci.h>
+#include <tftf_lib.h>
+#include <trusted_os.h>
+#include <tsp.h>
+#include <uuid.h>
+#include <uuid_utils.h>
+
+typedef struct {
+	uintptr_t addr;
+	size_t size;
+	unsigned int attr;
+	void *arg;
+} map_args_unmap_t;
+
+typedef test_result_t (*test_function_arg_t)(void *arg);
+
+#define SKIP_TEST_IF_LESS_THAN_N_CLUSTERS(n)					\
+	do {									\
+		unsigned int clusters_cnt;					\
+		clusters_cnt = tftf_get_total_clusters_count();			\
+		if (clusters_cnt < (n)) {					\
+			tftf_testcase_printf(					\
+				"Need at least %u clusters, only found %u\n",	\
+				(n), clusters_cnt);				\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
+#define SKIP_TEST_IF_LESS_THAN_N_CPUS(n)					\
+	do {									\
+		unsigned int cpus_cnt;						\
+		cpus_cnt = tftf_get_total_cpus_count();				\
+		if (cpus_cnt < (n)) {						\
+			tftf_testcase_printf(					\
+				"Need at least %u CPUs, only found %u\n",	\
+				(n), cpus_cnt);					\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
+#define SKIP_TEST_IF_TRUSTED_OS_NOT_PRESENT()					\
+	do {									\
+		uuid_t tos_uuid;						\
+										\
+		if (!is_trusted_os_present(&tos_uuid)) {			\
+			tftf_testcase_printf("No Trusted OS detected\n");	\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
+#define SKIP_TEST_IF_TSP_NOT_PRESENT()						\
+	do {									\
+		uuid_t tos_uuid;						\
+		char tos_uuid_str[UUID_STR_SIZE];				\
+										\
+		if (!is_trusted_os_present(&tos_uuid)) {			\
+			tftf_testcase_printf("No Trusted OS detected\n");	\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+										\
+		if (!uuid_equal(&tos_uuid, &tsp_uuid)) {			\
+			tftf_testcase_printf(					\
+				"Trusted OS is not the TSP, its UUID is: %s\n",	\
+				uuid_to_str(&tos_uuid, tos_uuid_str));		\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
+#define SKIP_TEST_IF_MM_NOT_PRESENT()						\
+	do {									\
+		smc_args version_smc = { MM_VERSION_AARCH32 };			\
+		smc_ret_values smc_ret = tftf_smc(&version_smc);		\
+		uint32_t version = smc_ret.ret0;				\
+										\
+		if (version == (uint32_t) SMC_UNKNOWN) {			\
+			tftf_testcase_printf("SPM not detected.\n");		\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (0)
+
+#define SKIP_TEST_IF_MM_VERSION_LESS_THAN(major, minor)				\
+	do {									\
+		smc_args version_smc = { MM_VERSION_AARCH32 };			\
+		smc_ret_values smc_ret = tftf_smc(&version_smc);		\
+		uint32_t version = smc_ret.ret0;				\
+										\
+		if (version == (uint32_t) SMC_UNKNOWN) {			\
+			tftf_testcase_printf("SPM not detected.\n");		\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+										\
+		if (version < MM_VERSION_FORM(major, minor)) {			\
+			tftf_testcase_printf("MM_VERSION returned %d.%d\n"	\
+					     "The required version is %d.%d\n",	\
+					     version >> MM_VERSION_MAJOR_SHIFT,	\
+					     version & MM_VERSION_MINOR_MASK,	\
+					     major, minor);			\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+										\
+		VERBOSE("MM_VERSION returned %d.%d\n",				\
+			version >> MM_VERSION_MAJOR_SHIFT,			\
+			version & MM_VERSION_MINOR_MASK);			\
+	} while (0)
+
+/* Helper macro to verify if system suspend API is supported */
+#define is_psci_sys_susp_supported()	\
+		(tftf_get_psci_feature_info(SMC_PSCI_SYSTEM_SUSPEND)	\
+					== PSCI_E_SUCCESS)
+
+/* Helper macro to verify if PSCI_STAT_COUNT API is supported */
+#define is_psci_stat_count_supported()	\
+		(tftf_get_psci_feature_info(SMC_PSCI_STAT_COUNT)	\
+					== PSCI_E_SUCCESS)
+
+/*
+ * Helper function to verify the system state is ready for system
+ * suspend. i.e., a single CPU is running and all other CPUs are powered off.
+ * Returns 1 if the system is ready to suspend, 0 otherwise.
+ */
+int is_sys_suspend_state_ready(void);
+
+/*
+ * Helper function to reset the system. This function shouldn't return.
+ * It is not marked with __dead to help the test to catch some error in
+ * TF
+ */
+void psci_system_reset(void);
+
+/*
+ * Helper function that enables/disables the mem_protect mechanism
+ */
+int psci_mem_protect(int val);
+
+
+/*
+ * Helper function to call PSCI MEM_PROTECT_CHECK
+ */
+int psci_mem_protect_check(uintptr_t addr, size_t size);
+
+
+/*
+ * Helper function to get a sentinel address that can be used to test mem_protect
+ */
+unsigned char *psci_mem_prot_get_sentinel(void);
+
+/*
+ * Helper function to memory map and unmap a region needed by a test.
+ *
+ * Return TEST_RESULT_FAIL if the memory could not be successfully mapped or
+ * unmapped. Otherwise, return the test functions's result.
+ */
+test_result_t map_test_unmap(const map_args_unmap_t *args,
+			     test_function_arg_t test);
+
+#endif /* __TEST_HELPERS_H__ */