26ba25
From 15af83ccafc175c7a61d39f998ac7eb43829e53c Mon Sep 17 00:00:00 2001
26ba25
From: David Hildenbrand <david@redhat.com>
26ba25
Date: Fri, 21 Dec 2018 15:36:07 +0000
26ba25
Subject: [PATCH 05/22] s390x/tcg: properly implement the TOD
26ba25
26ba25
RH-Author: David Hildenbrand <david@redhat.com>
26ba25
Message-id: <20181221153614.27961-6-david@redhat.com>
26ba25
Patchwork-id: 83751
26ba25
O-Subject: [RHEL-8.0 qemu-kvm v2 PATCH 05/12] s390x/tcg: properly implement the TOD
26ba25
Bugzilla: 1653569
26ba25
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
26ba25
RH-Acked-by: Thomas Huth <thuth@redhat.com>
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
26ba25
Right now, each CPU has its own TOD. Especially, the TOD will differ
26ba25
based on creation time of a CPU - e.g. when hotplugging a CPU the times
26ba25
will differ quite a lot, resulting in stall warnings in the guest.
26ba25
26ba25
Let's use a single TOD by implementing our new TOD device. Prepare it
26ba25
for TOD-clock epoch extension.
26ba25
26ba25
Most importantly, whenever we set the TOD, we have to update the CKC
26ba25
timer.
26ba25
26ba25
Introduce "tcg_s390x.h" just like "kvm_s390x.h" for tcg specific
26ba25
function declarations that should not go into cpu.h.
26ba25
26ba25
Reviewed-by: Thomas Huth <thuth@redhat.com>
26ba25
Signed-off-by: David Hildenbrand <david@redhat.com>
26ba25
Message-Id: <20180627134410.4901-6-david@redhat.com>
26ba25
Signed-off-by: Cornelia Huck <cohuck@redhat.com>
26ba25
(cherry picked from commit 7de3b1cdc67dcb572c1761c2051252e91a438b22)
26ba25
Signed-off-by: David Hildenbrand <david@redhat.com>
26ba25
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
26ba25
---
26ba25
 hw/s390x/tod-qemu.c        | 46 ++++++++++++++++++++++++++++++++++++++++++----
26ba25
 hw/s390x/tod.c             | 11 +++++++++++
26ba25
 include/hw/s390x/tod.h     | 19 +++++++++++++++++++
26ba25
 target/s390x/cpu.c         |  7 -------
26ba25
 target/s390x/cpu.h         |  1 -
26ba25
 target/s390x/internal.h    | 16 ----------------
26ba25
 target/s390x/misc_helper.c | 25 +++++++++++++++++++------
26ba25
 target/s390x/tcg_s390x.h   | 18 ++++++++++++++++++
26ba25
 8 files changed, 109 insertions(+), 34 deletions(-)
26ba25
 create mode 100644 target/s390x/tcg_s390x.h
26ba25
26ba25
diff --git a/hw/s390x/tod-qemu.c b/hw/s390x/tod-qemu.c
26ba25
index 03ea1ce..59c015c 100644
26ba25
--- a/hw/s390x/tod-qemu.c
26ba25
+++ b/hw/s390x/tod-qemu.c
26ba25
@@ -11,19 +11,43 @@
26ba25
 #include "qemu/osdep.h"
26ba25
 #include "qapi/error.h"
26ba25
 #include "hw/s390x/tod.h"
26ba25
+#include "qemu/timer.h"
26ba25
+#include "qemu/cutils.h"
26ba25
+#include "cpu.h"
26ba25
+#include "tcg_s390x.h"
26ba25
 
26ba25
 static void qemu_s390_tod_get(const S390TODState *td, S390TOD *tod,
26ba25
                               Error **errp)
26ba25
 {
26ba25
-    /* FIXME */
26ba25
-    tod->high = 0;
26ba25
-    tod->low = 0;
26ba25
+    *tod = td->base;
26ba25
+
26ba25
+    tod->low += time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
26ba25
+    if (tod->low < td->base.low) {
26ba25
+        tod->high++;
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static void qemu_s390_tod_set(S390TODState *td, const S390TOD *tod,
26ba25
                               Error **errp)
26ba25
 {
26ba25
-    /* FIXME */
26ba25
+    CPUState *cpu;
26ba25
+
26ba25
+    td->base = *tod;
26ba25
+
26ba25
+    td->base.low -= time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
26ba25
+    if (td->base.low > tod->low) {
26ba25
+        td->base.high--;
26ba25
+    }
26ba25
+
26ba25
+    /*
26ba25
+     * The TOD has been changed and we have to recalculate the CKC values
26ba25
+     * for all CPUs. We do this asynchronously, as "SET CLOCK should be
26ba25
+     * issued only while all other activity on all CPUs .. has been
26ba25
+     * suspended".
26ba25
+     */
26ba25
+    CPU_FOREACH(cpu) {
26ba25
+        async_run_on_cpu(cpu, tcg_s390_tod_updated, RUN_ON_CPU_NULL);
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
26ba25
@@ -34,10 +58,24 @@ static void qemu_s390_tod_class_init(ObjectClass *oc, void *data)
26ba25
     tdc->set = qemu_s390_tod_set;
26ba25
 }
26ba25
 
26ba25
+static void qemu_s390_tod_init(Object *obj)
26ba25
+{
26ba25
+    S390TODState *td = S390_TOD(obj);
26ba25
+    struct tm tm;
26ba25
+
26ba25
+    qemu_get_timedate(&tm, 0);
26ba25
+    td->base.high = 0;
26ba25
+    td->base.low = TOD_UNIX_EPOCH + (time2tod(mktimegm(&tm)) * 1000000000ULL);
26ba25
+    if (td->base.low < TOD_UNIX_EPOCH) {
26ba25
+        td->base.high += 1;
26ba25
+    }
26ba25
+}
26ba25
+
26ba25
 static TypeInfo qemu_s390_tod_info = {
26ba25
     .name = TYPE_QEMU_S390_TOD,
26ba25
     .parent = TYPE_S390_TOD,
26ba25
     .instance_size = sizeof(S390TODState),
26ba25
+    .instance_init = qemu_s390_tod_init,
26ba25
     .class_init = qemu_s390_tod_class_init,
26ba25
     .class_size = sizeof(S390TODClass),
26ba25
 };
26ba25
diff --git a/hw/s390x/tod.c b/hw/s390x/tod.c
26ba25
index 0501aff..1c63f41 100644
26ba25
--- a/hw/s390x/tod.c
26ba25
+++ b/hw/s390x/tod.c
26ba25
@@ -30,6 +30,17 @@ void s390_init_tod(void)
26ba25
     qdev_init_nofail(DEVICE(obj));
26ba25
 }
26ba25
 
26ba25
+S390TODState *s390_get_todstate(void)
26ba25
+{
26ba25
+    static S390TODState *ts;
26ba25
+
26ba25
+    if (!ts) {
26ba25
+        ts = S390_TOD(object_resolve_path_type("", TYPE_S390_TOD, NULL));
26ba25
+    }
26ba25
+
26ba25
+    return ts;
26ba25
+}
26ba25
+
26ba25
 #define S390_TOD_CLOCK_VALUE_MISSING    0x00
26ba25
 #define S390_TOD_CLOCK_VALUE_PRESENT    0x01
26ba25
 
26ba25
diff --git a/include/hw/s390x/tod.h b/include/hw/s390x/tod.h
26ba25
index 7096b57..413c0d7 100644
26ba25
--- a/include/hw/s390x/tod.h
26ba25
+++ b/include/hw/s390x/tod.h
26ba25
@@ -30,6 +30,9 @@ typedef struct S390TOD {
26ba25
 typedef struct S390TODState {
26ba25
     /* private */
26ba25
     DeviceState parent_obj;
26ba25
+
26ba25
+    /* unused by KVM implementation */
26ba25
+    S390TOD base;
26ba25
 } S390TODState;
26ba25
 
26ba25
 typedef struct S390TODClass {
26ba25
@@ -41,6 +44,22 @@ typedef struct S390TODClass {
26ba25
     void (*set)(S390TODState *td, const S390TOD *tod, Error **errp);
26ba25
 } S390TODClass;
26ba25
 
26ba25
+/* The value of the TOD clock for 1.1.1970. */
26ba25
+#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
26ba25
+
26ba25
+/* Converts ns to s390's clock format */
26ba25
+static inline uint64_t time2tod(uint64_t ns)
26ba25
+{
26ba25
+    return (ns << 9) / 125 + (((ns & 0xff10000000000000ull) / 125) << 9);
26ba25
+}
26ba25
+
26ba25
+/* Converts s390's clock format to ns */
26ba25
+static inline uint64_t tod2time(uint64_t t)
26ba25
+{
26ba25
+    return ((t >> 9) * 125) + (((t & 0x1ff) * 125) >> 9);
26ba25
+}
26ba25
+
26ba25
 void s390_init_tod(void);
26ba25
+S390TODState *s390_get_todstate(void);
26ba25
 
26ba25
 #endif
26ba25
diff --git a/target/s390x/cpu.c b/target/s390x/cpu.c
26ba25
index 5570741..f74d2a3 100644
26ba25
--- a/target/s390x/cpu.c
26ba25
+++ b/target/s390x/cpu.c
26ba25
@@ -30,7 +30,6 @@
26ba25
 #include "kvm_s390x.h"
26ba25
 #include "sysemu/kvm.h"
26ba25
 #include "qemu-common.h"
26ba25
-#include "qemu/cutils.h"
26ba25
 #include "qemu/timer.h"
26ba25
 #include "qemu/error-report.h"
26ba25
 #include "trace.h"
26ba25
@@ -276,9 +275,6 @@ static void s390_cpu_initfn(Object *obj)
26ba25
     CPUState *cs = CPU(obj);
26ba25
     S390CPU *cpu = S390_CPU(obj);
26ba25
     CPUS390XState *env = &cpu->env;
26ba25
-#if !defined(CONFIG_USER_ONLY)
26ba25
-    struct tm tm;
26ba25
-#endif
26ba25
 
26ba25
     cs->env_ptr = env;
26ba25
     cs->halted = 1;
26ba25
@@ -287,9 +283,6 @@ static void s390_cpu_initfn(Object *obj)
26ba25
                         s390_cpu_get_crash_info_qom, NULL, NULL, NULL, NULL);
26ba25
     s390_cpu_model_register_props(obj);
26ba25
 #if !defined(CONFIG_USER_ONLY)
26ba25
-    qemu_get_timedate(&tm, 0);
26ba25
-    env->tod_offset = TOD_UNIX_EPOCH +
26ba25
-                      (time2tod(mktimegm(&tm)) * 1000000000ULL);
26ba25
     env->tod_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_tod_timer, cpu);
26ba25
     env->cpu_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, s390x_cpu_timer, cpu);
26ba25
     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
26ba25
diff --git a/target/s390x/cpu.h b/target/s390x/cpu.h
26ba25
index 04f9adf..6500f42 100644
26ba25
--- a/target/s390x/cpu.h
26ba25
+++ b/target/s390x/cpu.h
26ba25
@@ -133,7 +133,6 @@ struct CPUS390XState {
26ba25
     uint64_t cpuid;
26ba25
 #endif
26ba25
 
26ba25
-    uint64_t tod_offset;
26ba25
     QEMUTimer *tod_timer;
26ba25
 
26ba25
     QEMUTimer *cpu_timer;
26ba25
diff --git a/target/s390x/internal.h b/target/s390x/internal.h
26ba25
index d1ed06f..61a509d 100644
26ba25
--- a/target/s390x/internal.h
26ba25
+++ b/target/s390x/internal.h
26ba25
@@ -237,22 +237,6 @@ enum cc_op {
26ba25
     CC_OP_MAX
26ba25
 };
26ba25
 
26ba25
-/* The value of the TOD clock for 1.1.1970. */
26ba25
-#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
26ba25
-
26ba25
-/* Converts ns to s390's clock format */
26ba25
-static inline uint64_t time2tod(uint64_t ns)
26ba25
-{
26ba25
-    return (ns << 9) / 125 + (((ns & 0xff10000000000000ull) / 125) << 9);
26ba25
-
26ba25
-}
26ba25
-
26ba25
-/* Converts s390's clock format to ns */
26ba25
-static inline uint64_t tod2time(uint64_t t)
26ba25
-{
26ba25
-    return ((t >> 9) * 125) + (((t & 0x1ff) * 125) >> 9);
26ba25
-}
26ba25
-
26ba25
 static inline hwaddr decode_basedisp_s(CPUS390XState *env, uint32_t ipb,
26ba25
                                        uint8_t *ar)
26ba25
 {
26ba25
diff --git a/target/s390x/misc_helper.c b/target/s390x/misc_helper.c
26ba25
index 8b3b040..4f675c7 100644
26ba25
--- a/target/s390x/misc_helper.c
26ba25
+++ b/target/s390x/misc_helper.c
26ba25
@@ -29,6 +29,8 @@
26ba25
 #include "exec/address-spaces.h"
26ba25
 #include "exec/exec-all.h"
26ba25
 #include "exec/cpu_ldst.h"
26ba25
+#include "qapi/error.h"
26ba25
+#include "tcg_s390x.h"
26ba25
 
26ba25
 #if !defined(CONFIG_USER_ONLY)
26ba25
 #include "sysemu/cpus.h"
26ba25
@@ -40,6 +42,7 @@
26ba25
 #include "hw/s390x/ioinst.h"
26ba25
 #include "hw/s390x/s390-pci-inst.h"
26ba25
 #include "hw/boards.h"
26ba25
+#include "hw/s390x/tod.h"
26ba25
 #endif
26ba25
 
26ba25
 /* #define DEBUG_HELPER */
26ba25
@@ -139,17 +142,19 @@ void HELPER(spx)(CPUS390XState *env, uint64_t a1)
26ba25
 /* Store Clock */
26ba25
 uint64_t HELPER(stck)(CPUS390XState *env)
26ba25
 {
26ba25
-    uint64_t time;
26ba25
+    S390TODState *td = s390_get_todstate();
26ba25
+    S390TODClass *tdc = S390_TOD_GET_CLASS(td);
26ba25
+    S390TOD tod;
26ba25
 
26ba25
-    time = env->tod_offset +
26ba25
-        time2tod(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL));
26ba25
-
26ba25
-    return time;
26ba25
+    tdc->get(td, &tod, &error_abort);
26ba25
+    return tod.low;
26ba25
 }
26ba25
 
26ba25
 /* Set Clock Comparator */
26ba25
 void HELPER(sckc)(CPUS390XState *env, uint64_t time)
26ba25
 {
26ba25
+    S390TODState *td = s390_get_todstate();
26ba25
+
26ba25
     if (time == -1ULL) {
26ba25
         return;
26ba25
     }
26ba25
@@ -157,7 +162,7 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
26ba25
     env->ckc = time;
26ba25
 
26ba25
     /* difference between origins */
26ba25
-    time -= env->tod_offset;
26ba25
+    time -= td->base.low;
26ba25
 
26ba25
     /* nanoseconds */
26ba25
     time = tod2time(time);
26ba25
@@ -165,6 +170,14 @@ void HELPER(sckc)(CPUS390XState *env, uint64_t time)
26ba25
     timer_mod(env->tod_timer, time);
26ba25
 }
26ba25
 
26ba25
+void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque)
26ba25
+{
26ba25
+    S390CPU *cpu = S390_CPU(cs);
26ba25
+    CPUS390XState *env = &cpu->env;
26ba25
+
26ba25
+    helper_sckc(env, env->ckc);
26ba25
+}
26ba25
+
26ba25
 /* Set Tod Programmable Field */
26ba25
 void HELPER(sckpf)(CPUS390XState *env, uint64_t r0)
26ba25
 {
26ba25
diff --git a/target/s390x/tcg_s390x.h b/target/s390x/tcg_s390x.h
26ba25
new file mode 100644
26ba25
index 0000000..4e308aa
26ba25
--- /dev/null
26ba25
+++ b/target/s390x/tcg_s390x.h
26ba25
@@ -0,0 +1,18 @@
26ba25
+/*
26ba25
+ * QEMU TCG support -- s390x specific functions.
26ba25
+ *
26ba25
+ * Copyright 2018 Red Hat, Inc.
26ba25
+ *
26ba25
+ * Authors:
26ba25
+ *   David Hildenbrand <david@redhat.com>
26ba25
+ *
26ba25
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
26ba25
+ * See the COPYING file in the top-level directory.
26ba25
+ */
26ba25
+
26ba25
+#ifndef TCG_S390X_H
26ba25
+#define TCG_S390X_H
26ba25
+
26ba25
+void tcg_s390_tod_updated(CPUState *cs, run_on_cpu_data opaque);
26ba25
+
26ba25
+#endif /* TCG_S390X_H */
26ba25
-- 
26ba25
1.8.3.1
26ba25