libpayload arm64: Add support for read and write registers at current EL in assembly

In order to ease the process of reading and writing any register at current EL,
provide read_current and write_current assembly macros. These are included in
arch/lib_helpers.h under the __ASSEMBLY__ macro condition. This is done to allow
the same header file to be included by .c and .S files.

BUG=chrome-os-partner:31634
BRANCH=None
TEST=Compiles successfully for ryu

Change-Id: I79241a944b68ebb24865e745a9835f54ab6d1a8f
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 2b55fbde466126c4de7f5f7bb2d1427196be842f
Original-Change-Id: I678ab89c4aa1b08898166e135b5ab2d6453bb5e8
Original-Signed-off-by: Furquan Shaikh <furquan@google.com>
Original-Reviewed-on: https://chromium-review.googlesource.com/214576
Original-Tested-by: Furquan Shaikh <furquan@chromium.org>
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Original-Commit-Queue: Furquan Shaikh <furquan@chromium.org>
Reviewed-on: http://review.coreboot.org/8801
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
diff --git a/payloads/libpayload/include/arm64/arch/lib_helpers.h b/payloads/libpayload/include/arm64/arch/lib_helpers.h
index 3adba81..d98d6f7 100644
--- a/payloads/libpayload/include/arm64/arch/lib_helpers.h
+++ b/payloads/libpayload/include/arm64/arch/lib_helpers.h
@@ -33,6 +33,61 @@
 #ifndef __ARCH_LIB_HELPERS_H__
 #define __ARCH_LIB_HELPERS_H__
 
+#ifdef __ASSEMBLY__
+
+/* Macro to switch to label based on current el */
+.macro switch_el xreg label1 label2 label3
+	mrs	\xreg, CurrentEL
+	/* Currently at EL1 */
+	cmp	\xreg, 0x4
+	b.eq	\label1
+	/* Currently at EL2 */
+	cmp	\xreg, 0x8
+	b.eq	\label2
+	/* Currently at EL3 */
+	cmp	\xreg, 0xc
+	b.eq	\label3
+.endm
+
+/* Macro to read sysreg at current EL
+   xreg - reg in which read value needs to be stored
+   sysreg - system reg that is to be read
+*/
+.macro read_current xreg sysreg
+	switch_el \xreg, 101f, 102f, 103f
+101:
+	mrs	\xreg, \sysreg\()_el1
+	b	104f
+102:
+	mrs	\xreg, \sysreg\()_el2
+	b	104f
+103:
+	mrs	\xreg, \sysreg\()_el3
+	b	104f
+104:
+.endm
+
+/* Macro to write sysreg at current EL
+   xreg - reg from which value needs to be written
+   sysreg - system reg that is to be written
+   temp - temp reg that can be used to read current EL
+*/
+.macro write_current sysreg xreg temp
+	switch_el \temp, 101f, 102f, 103f
+101:
+	msr	\sysreg\()_el1, \xreg
+	b	104f
+102:
+	msr	\sysreg\()_el2, \xreg
+	b	104f
+103:
+	msr	\sysreg\()_el3, \xreg
+	b	104f
+104:
+.endm
+
+#else
+
 #define EL0               0
 #define EL1               1
 #define EL2               2
@@ -318,4 +373,6 @@
 /* Clock */
 void set_cntfrq(uint32_t freq);
 
+#endif // __ASSEMBLY__
+
 #endif //__ARCH_LIB_HELPERS_H__