Introduce syscounter_read() helper function
syscounter_read() reads the system counter value. It encapsulates the
instruction barrier required to get an accurate timestamp for any
time-sensitive related operation.
Change-Id: Ib2cfe7789ee1471e614140d425d29ab5c8d306ea
Signed-off-by: Sandrine Bailleux <sandrine.bailleux@arm.com>
diff --git a/include/lib/aarch64/arch_helpers.h b/include/lib/aarch64/arch_helpers.h
index 1d9202d..053f8ba 100644
--- a/include/lib/aarch64/arch_helpers.h
+++ b/include/lib/aarch64/arch_helpers.h
@@ -468,4 +468,16 @@
}
}
+/* Read the count value of the system counter. */
+static inline uint64_t syscounter_read(void)
+{
+ /*
+ * The instruction barrier is needed to guarantee that we read an
+ * accurate value. Otherwise, the CPU might speculatively read it and
+ * return a stale value.
+ */
+ isb();
+ return read_cntpct_el0();
+}
+
#endif /* ARCH_HELPERS_H */