[kvm-unit-tests PATCH v4 12/13] s390x: Add time functions to lib

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Always good to know what time it is.

Signed-off-by: Janosch Frank <frankja@xxxxxxxxxxxxx>
---
 lib/s390x/time.c |  18 ++++++++++
 lib/s390x/time.h | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 s390x/Makefile   |   1 +
 3 files changed, 127 insertions(+)
 create mode 100644 lib/s390x/time.c
 create mode 100644 lib/s390x/time.h

diff --git a/lib/s390x/time.c b/lib/s390x/time.c
new file mode 100644
index 0000000..e0fbcfc
--- /dev/null
+++ b/lib/s390x/time.c
@@ -0,0 +1,18 @@
+#include <libcflat.h>
+#include "time.h"
+
+uint64_t time_get_ns(void)
+{
+	unsigned long long tod;
+
+	tod = stckf();
+	return tod_to_ns(tod);
+}
+
+uint64_t time_get_s(void)
+{
+	uint64_t ns;
+
+	ns = time_get_ns();
+	return ns / 1000000UL;
+}
diff --git a/lib/s390x/time.h b/lib/s390x/time.h
new file mode 100644
index 0000000..96bb974
--- /dev/null
+++ b/lib/s390x/time.h
@@ -0,0 +1,108 @@
+/*
+ * Time related definitions.
+ *
+ * Mostly copied from arch/s390/include/asm/timex.h from the Linux
+ * kernel git.
+ *
+ * Copyright IBM Corp. 1999
+ *
+ * Derived from "include/asm-i386/timex.h"
+ * Copyright (C) 1992, Linus Torvalds
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.
+ */
+#ifndef TIME_H
+#define TIME_H
+
+/* The value of the TOD clock for 1.1.1970. */
+#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
+
+static inline void set_clock_comparator(uint64_t time)
+{
+	asm volatile("sckc %0" : : "Q" (time));
+}
+
+static inline void store_clock_comparator(uint64_t *time)
+{
+	asm volatile("stckc %0" : "=Q" (*time));
+}
+
+static inline int set_tod_clock(uint64_t time)
+{
+	int cc;
+
+	asm volatile(
+		"   sck   %1\n"
+		"   ipm   %0\n"
+		"   srl   %0,28\n"
+		: "=d" (cc) : "Q" (time) : "cc");
+	return cc;
+}
+
+static inline uint64_t stck(void)
+{
+	uint64_t tod;
+
+	asm volatile(
+		"	stck	%[tod]\n"
+		: [tod] "=Q" (tod) : : "cc"
+		);
+	return tod;
+}
+
+static inline uint64_t stckf(void)
+{
+	uint64_t tod;
+
+	asm volatile(
+		"	.machine push\n"
+		"	.machine \"z9-109\"\n"
+		"	stckf	%[tod]\n"
+		"	.machine pop\n"
+		: [tod] "=Q" (tod)
+		: : "cc");
+	return tod;
+}
+
+/*
+ * STCKE returns 16 bytes, which include the epoch, tod value and a
+ * programmable field. The value has to be trimmed to be able to work
+ * with it. As there's still some time to 2042, this code is not ready
+ * for multi epoch values.
+ */
+static inline void stcke(char *tod)
+{
+	asm volatile(
+		"	stcke	%[tod]\n"
+		: [tod] "=Q" (tod)
+		: : "cc");
+}
+
+/**
+ * tod_to_ns - convert a TOD format value to nanoseconds
+ * @todval: to be converted TOD format value
+ * Returns: number of nanoseconds that correspond to the TOD format value
+ *
+ * Converting a 64 Bit TOD format value to nanoseconds means that the value
+ * must be divided by 4.096. In order to achieve that we multiply with 125
+ * and divide by 512:
+ *
+ *    ns = (todval * 125) >> 9;
+ *
+ * In order to avoid an overflow with the multiplication we can rewrite this.
+ * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
+ * we end up with
+ *
+ *    ns = ((2^9 * th + tl) * 125 ) >> 9;
+ * -> ns = (th * 125) + ((tl * 125) >> 9);
+ *
+ */
+static inline uint64_t tod_to_ns(uint64_t todval)
+{
+	return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
+}
+
+
+uint64_t time_get_ns(void);
+uint64_t time_get_s(void);
+#endif
diff --git a/s390x/Makefile b/s390x/Makefile
index 82c4f15..afee4c9 100644
--- a/s390x/Makefile
+++ b/s390x/Makefile
@@ -43,6 +43,7 @@ cflatobjs += lib/s390x/sclp.o
 cflatobjs += lib/s390x/sclp-console.o
 cflatobjs += lib/s390x/interrupt.o
 cflatobjs += lib/s390x/mmu.o
+cflatobjs += lib/s390x/time.o
 
 OBJDIRS += lib/s390x
 
-- 
2.14.3




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux