1
2
3
4
5
6
7
8
9
10
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"
17
18static void kvm_s390_get_tod_raw(S390TOD *tod, Error **errp)
19{
20 int r;
21
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);
25 }
26 if (r) {
27 error_setg(errp, "Unable to get KVM guest TOD clock: %s",
28 strerror(-r));
29 }
30}
31
32static void kvm_s390_tod_get(const S390TODState *td, S390TOD *tod, Error **errp)
33{
34 if (td->stopped) {
35 *tod = td->base;
36 return;
37 }
38
39 kvm_s390_get_tod_raw(tod, errp);
40}
41
42static void kvm_s390_set_tod_raw(const S390TOD *tod, Error **errp)
43{
44 int r;
45
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);
49 }
50 if (r) {
51 error_setg(errp, "Unable to set KVM guest TOD clock: %s",
52 strerror(-r));
53 }
54}
55
56static void kvm_s390_tod_set(S390TODState *td, const S390TOD *tod, Error **errp)
57{
58 Error *local_err = NULL;
59
60
61
62
63
64
65
66
67 kvm_s390_set_tod_raw(tod, &local_err);
68 if (local_err) {
69 error_propagate(errp, local_err);
70 return;
71 }
72
73 if (runstate_is_running()) {
74 td->stopped = false;
75 } else {
76 td->stopped = true;
77 td->base = *tod;
78 }
79}
80
81static void kvm_s390_tod_vm_state_change(void *opaque, int running,
82 RunState state)
83{
84 S390TODState *td = opaque;
85 Error *local_err = NULL;
86
87 if (running && td->stopped) {
88
89 kvm_s390_set_tod_raw(&td->base, &local_err);
90 if (local_err) {
91 warn_report_err(local_err);
92 }
93
94 td->stopped = false;
95 } else if (!running && !td->stopped) {
96
97 kvm_s390_get_tod_raw(&td->base, &local_err);
98 if (local_err) {
99
100 warn_report_err(local_err);
101 } else {
102 td->stopped = true;
103 }
104 }
105}
106
107static void kvm_s390_tod_realize(DeviceState *dev, Error **errp)
108{
109 S390TODState *td = S390_TOD(dev);
110 S390TODClass *tdc = S390_TOD_GET_CLASS(td);
111 Error *local_err = NULL;
112
113 tdc->parent_realize(dev, &local_err);
114 if (local_err) {
115 error_propagate(errp, local_err);
116 return;
117 }
118
119
120
121
122
123
124 qemu_add_vm_change_state_handler(kvm_s390_tod_vm_state_change, td);
125}
126
127static void kvm_s390_tod_class_init(ObjectClass *oc, void *data)
128{
129 S390TODClass *tdc = S390_TOD_CLASS(oc);
130
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;
135}
136
137static void kvm_s390_tod_init(Object *obj)
138{
139 S390TODState *td = S390_TOD(obj);
140
141
142
143
144
145
146
147 td->stopped = false;
148}
149
150static 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),
157};
158
159static void register_types(void)
160{
161 type_register_static(&kvm_s390_tod_info);
162}
163type_init(register_types);
164