libutee: add headers for user-space to access sysregs

User space may require to access system registers like generic timer
registers in case function tracing is enabled etc. So provide headers
for user space to access sysregs.

Signed-off-by: Sumit Garg <sumit.garg@linaro.org>
Reviewed-by: Jerome Forissier <jerome.forissier@linaro.org>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/lib/libutee/arch/arm/arm32_user_sysreg.txt b/lib/libutee/arch/arm/arm32_user_sysreg.txt
new file mode 100644
index 0000000..8baefd1
--- /dev/null
+++ b/lib/libutee/arch/arm/arm32_user_sysreg.txt
@@ -0,0 +1,13 @@
+# Format of file
+# <reg-name> <CRn> <opc1> <CRm> <opc2> <Type> <Description>
+# lines beginning with '@' will be printed as additional comments
+
+@ Based on register description in
+@ ARM Architecture Reference Manual
+@ ARMv7-A and ARMv7-R edition
+@ Issue C.c
+@
+
+@ B8.2 Generic Timer registers summary
+CNTFRQ    c14 0 c0  0 RW Counter Frequency register
+CNTPCT    -   0 c14 - RO Physical Count register
diff --git a/lib/libutee/include/arm64_user_sysreg.h b/lib/libutee/include/arm64_user_sysreg.h
new file mode 100644
index 0000000..9a11e0a
--- /dev/null
+++ b/lib/libutee/include/arm64_user_sysreg.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+#ifndef ARM64_USER_SYSREG_H
+#define ARM64_USER_SYSREG_H
+
+#include <compiler.h>
+#include <stdint.h>
+
+/*
+ * Templates for register read/write functions based on mrs/msr
+ */
+
+#define DEFINE_REG_READ_FUNC_(reg, type, asmreg)	\
+static inline __noprof type read_##reg(void)		\
+{							\
+	type val;					\
+							\
+	asm volatile("mrs %0, " #asmreg : "=r" (val));	\
+	return val;					\
+}
+
+#define DEFINE_REG_WRITE_FUNC_(reg, type, asmreg)		\
+static inline __noprof void write_##reg(type val)		\
+{								\
+	asm volatile("msr " #asmreg ", %0" : : "r" (val));	\
+}
+
+/* ARM Generic timer functions */
+DEFINE_REG_READ_FUNC_(cntfrq, uint32_t, cntfrq_el0)
+DEFINE_REG_READ_FUNC_(cntpct, uint64_t, cntpct_el0)
+
+#endif /*ARM64_USER_SYSREG_H*/
diff --git a/lib/libutee/include/arm_user_sysreg.h b/lib/libutee/include/arm_user_sysreg.h
new file mode 100644
index 0000000..6cae1b9
--- /dev/null
+++ b/lib/libutee/include/arm_user_sysreg.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * Copyright (c) 2019, Linaro Limited
+ */
+#ifndef ARM_USER_SYSREG_H
+#define ARM_USER_SYSREG_H
+
+#include <util.h>
+
+#ifdef ARM32
+#include <arm32_user_sysreg.h>
+#endif
+
+#ifdef ARM64
+#include <arm64_user_sysreg.h>
+#endif
+
+#endif /*ARM_USER_SYSREG_H*/
diff --git a/scripts/arm32_sysreg.py b/scripts/arm32_sysreg.py
index bd0c619..0852471 100755
--- a/scripts/arm32_sysreg.py
+++ b/scripts/arm32_sysreg.py
@@ -69,7 +69,7 @@
     print('')
     if len(descr):
         print('/* ' + descr + ' */')
-    print('static inline uint64_t read_' + reg_name.lower() + '(void)')
+    print('static inline __noprof uint64_t read_' + reg_name.lower() + '(void)')
     print('{')
     print('\tuint64_t v;')
     print('')
@@ -84,7 +84,8 @@
     print('')
     if len(descr):
         print('/* ' + descr + ' */')
-    print('static inline void write_' + reg_name.lower() + '(uint64_t v)')
+    print('static inline __noprof void write_' + reg_name.lower() +
+          '(uint64_t v)')
     print('{')
     print('\tasm volatile ("mcrr p15, ' + opc1 + ', %Q0, %R0, ' +
           crm + '"' + ' : : "r"  (v));')
@@ -95,7 +96,7 @@
     print('')
     if len(descr):
         print('/* ' + descr + ' */')
-    print('static inline uint32_t read_' + reg_name.lower() + '(void)')
+    print('static inline __noprof uint32_t read_' + reg_name.lower() + '(void)')
     print('{')
     print('\tuint32_t v;')
     print('')
@@ -110,7 +111,8 @@
     print('')
     if len(descr):
         print('/* ' + descr + ' */')
-    print('static inline void write_' + reg_name.lower() + '(uint32_t v)')
+    print('static inline __noprof void write_' + reg_name.lower() +
+          '(uint32_t v)')
     print('{')
     print('\tasm volatile ("mcr p15, ' + opc1 + ', %0, ' + crn + ', ' +
           crm + ', ' + opc2 + '"' + ' : : "r"  (v));')
@@ -225,6 +227,7 @@
         if args.guard is not None:
             print('#ifndef ' + args.guard.upper().replace('.', '_'))
             print('#define ' + args.guard.upper().replace('.', '_'))
+        print('#include <compiler.h>')
 
     line_number = 0
     for line in sys.stdin:
diff --git a/ta/ta.mk b/ta/ta.mk
index aee23d3..ca9848b 100644
--- a/ta/ta.mk
+++ b/ta/ta.mk
@@ -50,6 +50,33 @@
 cppflags$(sm)	+= -include $(conf-file)
 cppflags$(sm) += -DTRACE_LEVEL=$(CFG_TEE_TA_LOG_LEVEL)
 
+ifeq ($(ta-target),ta_arm32)
+arm32-user-sysreg-txt = lib/libutee/arch/arm/arm32_user_sysreg.txt
+arm32-user-sysregs-$(arm32-user-sysreg-txt)-h := arm32_user_sysreg.h
+arm32-user-sysregs += $(arm32-user-sysreg-txt)
+
+arm32-user-sysregs-out := $(out-dir)/include/generated
+
+define process-arm32-user-sysreg
+FORCE-GENSRC$(sm): $$(arm32-user-sysregs-out)/$$(arm32-user-sysregs-$(1)-h)
+cleanfiles := $$(cleanfiles) \
+		 $$(arm32-user-sysregs-out)/$$(arm32-user-sysregs-$(1)-h)
+
+$$(arm32-user-sysregs-out)/$$(arm32-user-sysregs-$(1)-h): \
+		$(1) scripts/arm32_sysreg.py
+	@$(cmd-echo-silent) '  GEN     $$@'
+	$(q)mkdir -p $$(dir $$@)
+	$(q)scripts/arm32_sysreg.py --guard __$$(arm32-user-sysregs-$(1)-h) \
+		< $$< > $$@
+
+endef #process-arm32-user-sysreg
+
+$(foreach sr, $(arm32-user-sysregs), \
+	 $(eval $(call process-arm32-user-sysreg,$(sr))))
+
+cppflags$(sm)	+= -I$(arm32-user-sysregs-out)
+endif
+
 base-prefix := $(sm)-
 
 libname = utils
@@ -97,6 +124,9 @@
 incfiles-extra-host += core/include/tee/tee_fs_key_manager.h
 incfiles-extra-host += core/include/tee/fs_htree.h
 incfiles-extra-host += core/include/signed_hdr.h
+ifeq ($(ta-target),ta_arm32)
+incfiles-extra-host += $(out-dir)/include/generated/arm32_user_sysreg.h
+endif
 
 #
 # Copy lib files and exported headers from each lib