feat(brbe): test that EL3 has properly enabled access to BRBE

Access to FEAT_BRBE control registers must be explicitly enabled in EL3,
this simple test just ensures that the registers are accessible or traps
to EL3, similar to the TRBE test.

Signed-off-by: John Powell <john.powell@arm.com>
Change-Id: I0a25c5ce6beb6aa96b9428264b75cb3569ac535a
diff --git a/include/common/test_helpers.h b/include/common/test_helpers.h
index a69eb72..e838270 100644
--- a/include/common/test_helpers.h
+++ b/include/common/test_helpers.h
@@ -309,6 +309,14 @@
 	} while (false)
 #endif
 
+#define SKIP_TEST_IF_BRBE_NOT_SUPPORTED()					\
+	do {									\
+		if (!get_feat_brbe_support()) {				\
+			tftf_testcase_printf("FEAT_BRBE not supported\n");	\
+			return TEST_RESULT_SKIPPED;				\
+		}								\
+	} while (false)
+
 /* 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)		\
diff --git a/include/lib/aarch64/arch.h b/include/lib/aarch64/arch.h
index 399da93..dd0f899 100644
--- a/include/lib/aarch64/arch.h
+++ b/include/lib/aarch64/arch.h
@@ -167,6 +167,11 @@
 #define ID_AA64DFR0_V8_2_DEBUG_ARCH_SUPPORTED	U(8)
 #define ID_AA64DFR0_V8_4_DEBUG_ARCH_SUPPORTED	U(9)
 
+/* ID_AA64DFR0_EL1.BRBE definitions */
+#define ID_AA64DFR0_BRBE_SHIFT			U(52)
+#define ID_AA64DFR0_BRBE_MASK			ULL(0xf)
+#define ID_AA64DFR0_BRBE_SUPPORTED		ULL(1)
+
 /* ID_AA64DFR0_EL1.TraceBuffer definitions */
 #define ID_AA64DFR0_TRACEBUFFER_SHIFT		U(44)
 #define ID_AA64DFR0_TRACEBUFFER_MASK		ULL(0xf)
@@ -1114,6 +1119,19 @@
 #define TRBIDR_EL1	S3_0_C9_C11_7
 
 /*******************************************************************************
+ * FEAT_BRBE - Branch Record Buffer Extension System Registers
+ ******************************************************************************/
+
+#define BRBCR_EL1	S2_1_C9_C0_0
+#define BRBCR_EL2	S2_4_C9_C0_0
+#define BRBFCR_EL1	S2_1_C9_C0_1
+#define BRBTS_EL1	S2_1_C9_C0_2
+#define BRBINFINJ_EL1	S2_1_C9_C1_0
+#define BRBSRCINJ_EL1	S2_1_C9_C1_1
+#define BRBTGTINJ_EL1	S2_1_C9_C1_2
+#define BRBIDR0_EL1	S2_1_C9_C2_0
+
+/*******************************************************************************
  * Armv8.4 - Trace Filter System Registers
  ******************************************************************************/
 #define TRFCR_EL1	S3_0_C1_C2_1
diff --git a/include/lib/aarch64/arch_features.h b/include/lib/aarch64/arch_features.h
index 4da6407..77b9f9d 100644
--- a/include/lib/aarch64/arch_features.h
+++ b/include/lib/aarch64/arch_features.h
@@ -154,4 +154,11 @@
 		  ID_AA64MMFR1_EL1_AFP_MASK) == ID_AA64MMFR1_EL1_AFP_SUPPORTED);
 }
 
+static inline bool get_feat_brbe_support(void)
+{
+	return ((read_id_aa64dfr0_el1() >> ID_AA64DFR0_BRBE_SHIFT) &
+		ID_AA64DFR0_BRBE_MASK) ==
+		ID_AA64DFR0_BRBE_SUPPORTED;
+}
+
 #endif /* ARCH_FEATURES_H */
diff --git a/include/lib/aarch64/arch_helpers.h b/include/lib/aarch64/arch_helpers.h
index eac8b7c..b6d924b 100644
--- a/include/lib/aarch64/arch_helpers.h
+++ b/include/lib/aarch64/arch_helpers.h
@@ -524,6 +524,16 @@
 DEFINE_RENAME_SYSREG_RW_FUNCS(trbtrg_el1, TRBTRG_EL1)
 DEFINE_RENAME_SYSREG_READ_FUNC(trbidr_el1, TRBIDR_EL1)
 
+/* FEAT_BRBE Branch record buffer extension system registers */
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbcr_el1, BRBCR_EL1)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbcr_el2, BRBCR_EL2)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbfcr_el1, BRBFCR_EL1)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbts_el1, BRBTS_EL1)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbinfinj_el1, BRBINFINJ_EL1)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbsrcinj_el1, BRBSRCINJ_EL1)
+DEFINE_RENAME_SYSREG_RW_FUNCS(brbtgtinj_el1, BRBTGTINJ_EL1)
+DEFINE_RENAME_SYSREG_READ_FUNC(brbidr0_el1, BRBIDR0_EL1)
+
 /* Armv8.4 Trace filter control System Registers */
 DEFINE_RENAME_SYSREG_RW_FUNCS(trfcr_el1, TRFCR_EL1)
 DEFINE_RENAME_SYSREG_RW_FUNCS(trfcr_el2, TRFCR_EL2)
diff --git a/tftf/tests/extensions/brbe/test_brbe.c b/tftf/tests/extensions/brbe/test_brbe.c
new file mode 100644
index 0000000..f2c244a
--- /dev/null
+++ b/tftf/tests/extensions/brbe/test_brbe.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2022, Arm Limited. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <string.h>
+
+#include <test_helpers.h>
+#include <tftf_lib.h>
+#include <tftf.h>
+
+/*
+ * EL3 is expected to allow access to branch record buffer control registers
+ * from NS world. Accessing these registers will trap to EL3 and crash when EL3
+ * has not properly enabled it.
+ */
+test_result_t test_brbe_enabled(void)
+{
+	SKIP_TEST_IF_AARCH32();
+
+#ifdef __aarch64__
+	SKIP_TEST_IF_BRBE_NOT_SUPPORTED();
+
+	read_brbcr_el1();
+	read_brbcr_el2();
+	read_brbfcr_el1();
+	read_brbts_el1();
+	read_brbinfinj_el1();
+	read_brbsrcinj_el1();
+	read_brbtgtinj_el1();
+	read_brbidr0_el1();
+
+	return TEST_RESULT_SUCCESS;
+#endif /* __aarch64__ */
+}
diff --git a/tftf/tests/tests-cpu-extensions.mk b/tftf/tests/tests-cpu-extensions.mk
index 4600e7f..0719486 100644
--- a/tftf/tests/tests-cpu-extensions.mk
+++ b/tftf/tests/tests-cpu-extensions.mk
@@ -7,6 +7,7 @@
 TESTS_SOURCES	+=	$(addprefix tftf/tests/,			\
 	extensions/afp/test_afp.c					\
 	extensions/amu/test_amu.c					\
+	extensions/brbe/test_brbe.c					\
 	extensions/ecv/test_ecv.c					\
 	extensions/fgt/test_fgt.c					\
 	extensions/mte/test_mte.c					\
diff --git a/tftf/tests/tests-cpu-extensions.xml b/tftf/tests/tests-cpu-extensions.xml
index 97826cc..ba85866 100644
--- a/tftf/tests/tests-cpu-extensions.xml
+++ b/tftf/tests/tests-cpu-extensions.xml
@@ -21,6 +21,7 @@
     <testcase name="Use FGT Registers" function="test_fgt_enabled" />
     <testcase name="Use ECV Registers" function="test_ecv_enabled" />
     <testcase name="Use trace buffer control Registers" function="test_trbe_enabled" />
+    <testcase name="Use branch record buffer control registers" function="test_brbe_enabled" />
     <testcase name="Use trace filter control Registers" function="test_trf_enabled" />
     <testcase name="Use trace system Registers" function="test_sys_reg_trace_enabled" />
     <testcase name="SME support" function="test_sme_support" />