hw/riscv: Use the CPU to determine if 32-bit
[qemu/ar7.git] / hw / s390x / tod-kvm.c
blob6e21d83181d7a483a68e754b1dffe31c16bd048b
1 /*
2 * TOD (Time Of Day) clock - KVM implementation
4 * Copyright 2018 Red Hat, Inc.
5 * Author(s): David Hildenbrand <david@redhat.com>
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "qemu/module.h"
14 #include "sysemu/runstate.h"
15 #include "hw/s390x/tod.h"
16 #include "kvm_s390x.h"
18 static void kvm_s390_get_tod_raw(S390TOD *tod, Error **errp)
20 int r;
22 r = kvm_s390_get_clock_ext(&tod->high, &tod->low);
23 if (r == -ENXIO) {
24 r = kvm_s390_get_clock(&tod->high, &tod->low);
26 if (r) {
27 error_setg(errp, "Unable to get KVM guest TOD clock: %s",
28 strerror(-r));
32 static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
34 if (td->stopped) {
35 *tod = td->base;
36 return;
39 kvm_s390_get_tod_raw(tod, errp);
42 static void kvm_s390_set_tod_raw(const S390TOD *tod, Error **errp)
44 int r;
46 r = kvm_s390_set_clock_ext(tod->high, tod->low);
47 if (r == -ENXIO) {
48 r = kvm_s390_set_clock(tod->high, tod->low);
50 if (r) {
51 error_setg(errp, "Unable to set KVM guest TOD clock: %s",
52 strerror(-r));
56 static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
58 Error *local_err = NULL;
61 * Somebody (e.g. migration) set the TOD. We'll store it into KVM to
62 * properly detect errors now but take a look at the runstate to decide
63 * whether really to keep the tod running. E.g. during migration, this
64 * is the point where we want to stop the initially running TOD to fire
65 * it back up when actually starting the migrated guest.
67 kvm_s390_set_tod_raw(tod, &local_err);
68 if (local_err) {
69 error_propagate(errp, local_err);
70 return;
73 if (runstate_is_running()) {
74 td->stopped = false;
75 } else {
76 td->stopped = true;
77 td->base = *tod;
81 static void kvm_s390_tod_vm_state_change(void *opaque, int running,
82 RunState state)
84 S390TODState *td = opaque;
85 Error *local_err = NULL;
87 if (running && td->stopped) {
88 /* Set the old TOD when running the VM - start the TOD clock. */
89 kvm_s390_set_tod_raw(&td->base, &local_err);
90 if (local_err) {
91 warn_report_err(local_err);
93 /* Treat errors like the TOD was running all the time. */
94 td->stopped = false;
95 } else if (!running && !td->stopped) {
96 /* Store the TOD when stopping the VM - stop the TOD clock. */
97 kvm_s390_get_tod_raw(&td->base, &local_err);
98 if (local_err) {
99 /* Keep the TOD running in case we could not back it up. */
100 warn_report_err(local_err);
101 } else {
102 td->stopped = true;
107 static void kvm_s390_tod_realize(DeviceState *dev, Error **errp)
109 S390TODState *td = S390_TOD(dev);
110 S390TODClass *tdc = S390_TOD_GET_CLASS(td);
111 Error *local_err = NULL;
113 tdc->parent_realize(dev, &local_err);
114 if (local_err) {
115 error_propagate(errp, local_err);
116 return;
120 * We need to know when the VM gets started/stopped to start/stop the TOD.
121 * As we can never have more than one TOD instance (and that will never be
122 * removed), registering here and never unregistering is good enough.
124 qemu_add_vm_change_state_handler(kvm_s390_tod_vm_state_change, td);
127 static void kvm_s390_tod_class_init(ObjectClass *oc, void *data)
129 S390TODClass *tdc = S390_TOD_CLASS(oc);
131 device_class_set_parent_realize(DEVICE_CLASS(oc), kvm_s390_tod_realize,
132 &tdc->parent_realize);
133 tdc->get = kvm_s390_tod_get;
134 tdc->set = kvm_s390_tod_set;
137 static void kvm_s390_tod_init(Object *obj)
139 S390TODState *td = S390_TOD(obj);
142 * The TOD is initially running (value stored in KVM). Avoid needless
143 * loading/storing of the TOD when starting a simple VM, so let it
144 * run although the (never started) VM is stopped. For migration, we
145 * will properly set the TOD later.
147 td->stopped = false;
150 static TypeInfo kvm_s390_tod_info = {
151 .name = TYPE_KVM_S390_TOD,
152 .parent = TYPE_S390_TOD,
153 .instance_size = sizeof(S390TODState),
154 .instance_init = kvm_s390_tod_init,
155 .class_init = kvm_s390_tod_class_init,
156 .class_size = sizeof(S390TODClass),
159 static void register_types(void)
161 type_register_static(&kvm_s390_tod_info);
163 type_init(register_types);