]> xenbits.xensource.com Git - xen.git/commitdiff
x86: provide and use hv_tsc_scale
authorWei Liu <liuwe@microsoft.com>
Fri, 20 Dec 2019 19:18:16 +0000 (19:18 +0000)
committerWei Liu <wl@xen.org>
Mon, 23 Dec 2019 10:51:29 +0000 (10:51 +0000)
The Hyper-V clock source and Xen's own viridian code need the same
functionality.

Move the function in viridian/time.c to hyperv.h and use it in both
places.

No functional change.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
Reviewed-by: Paul Durrant <paul@xen.org>
xen/arch/x86/hvm/viridian/time.c
xen/arch/x86/time.c
xen/include/asm-x86/guest/hyperv.h

index 32e79bbcc407ea30a4fdcfaf59bf6afaeffde0db..6b2d745f3a0abdbc8e6c37833e89a40c8966bfc5 100644 (file)
@@ -13,6 +13,7 @@
 
 #include <asm/apic.h>
 #include <asm/event.h>
+#include <asm/guest/hyperv.h>
 #include <asm/guest/hyperv-tlfs.h>
 #include <asm/hvm/support.h>
 
@@ -82,33 +83,6 @@ static void update_reference_tsc(const struct domain *d, bool initialize)
     p->tsc_sequence = seq;
 }
 
-/*
- * The specification says: "The partition reference time is computed
- * by the following formula:
- *
- * ReferenceTime = ((VirtualTsc * TscScale) >> 64) + TscOffset
- *
- * The multiplication is a 64 bit multiplication, which results in a
- * 128 bit number which is then shifted 64 times to the right to obtain
- * the high 64 bits."
- */
-static uint64_t scale_tsc(uint64_t tsc, uint64_t scale, int64_t offset)
-{
-    uint64_t result;
-
-    /*
-     * Quadword MUL takes an implicit operand in RAX, and puts the result
-     * in RDX:RAX. Because we only want the result of the multiplication
-     * after shifting right by 64 bits, we therefore only need the content
-     * of RDX.
-     */
-    asm ( "mulq %[scale]"
-          : "+a" (tsc), "=d" (result)
-          : [scale] "rm" (scale) );
-
-    return result + offset;
-}
-
 static uint64_t trc_val(const struct domain *d, int64_t offset)
 {
     uint64_t tsc, scale;
@@ -116,7 +90,7 @@ static uint64_t trc_val(const struct domain *d, int64_t offset)
     tsc = hvm_get_guest_tsc(pt_global_vcpu_target(d));
     scale = ((10000ul << 32) / d->arch.tsc_khz) << 32;
 
-    return scale_tsc(tsc, scale, offset);
+    return hv_scale_tsc(tsc, scale, offset);
 }
 
 static void time_ref_count_freeze(const struct domain *d)
index fca25e1c22769ec2e62432c8c2a86893443305d5..ea696a95e82fb06e36b87a45497ddeab15219c08 100644 (file)
@@ -725,12 +725,7 @@ static uint64_t read_hyperv_timer(void)
 
     } while ( tsc_page->tsc_sequence != seq );
 
-    /* ret = ((tsc * scale) >> 64) + offset; */
-    asm ( "mul %[scale]; add %[offset], %[ret]"
-          : "+a" (tsc), [ret] "=&d" (ret)
-          : [scale] "rm" (scale), [offset] "rm" (offset) );
-
-    return ret;
+    return hv_scale_tsc(tsc, scale, offset);
 }
 
 static struct platform_timesource __initdata plt_hyperv_timer =
index cc21b9abfc56d2a3cc7c136d433734d7916973ff..c7a7f32bd5ebb0eed7c9ec98475a8a996a8f88c2 100644 (file)
 #ifndef __X86_GUEST_HYPERV_H__
 #define __X86_GUEST_HYPERV_H__
 
-#ifdef CONFIG_HYPERV_GUEST
-
 #include <xen/types.h>
 
+/*
+ * The specification says: "The partition reference time is computed
+ * by the following formula:
+ *
+ * ReferenceTime = ((VirtualTsc * TscScale) >> 64) + TscOffset
+ *
+ * The multiplication is a 64 bit multiplication, which results in a
+ * 128 bit number which is then shifted 64 times to the right to obtain
+ * the high 64 bits."
+ */
+static inline uint64_t hv_scale_tsc(uint64_t tsc, uint64_t scale,
+                                    int64_t offset)
+{
+    uint64_t result;
+
+    /*
+     * Quadword MUL takes an implicit operand in RAX, and puts the result
+     * in RDX:RAX. Because we only want the result of the multiplication
+     * after shifting right by 64 bits, we therefore only need the content
+     * of RDX.
+     */
+    asm ( "mulq %[scale]"
+          : "+a" (tsc), "=d" (result)
+          : [scale] "rm" (scale) );
+
+    return result + offset;
+}
+
+#ifdef CONFIG_HYPERV_GUEST
+
 #include <asm/guest/hypervisor.h>
 
 struct ms_hyperv_info {