s390x/tod: factor out TOD into separate device
[qemu.git] / hw / s390x / tod.c
1 /*
2 * TOD (Time Of Day) clock
3 *
4 * Copyright 2018 Red Hat, Inc.
5 * Author(s): David Hildenbrand <david@redhat.com>
6 *
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 */
10
11 #include "qemu/osdep.h"
12 #include "hw/s390x/tod.h"
13 #include "qapi/error.h"
14 #include "qemu/error-report.h"
15 #include "sysemu/kvm.h"
16 #include "migration/register.h"
17
18 void s390_init_tod(void)
19 {
20 Object *obj;
21
22 if (kvm_enabled()) {
23 obj = object_new(TYPE_KVM_S390_TOD);
24 } else {
25 obj = object_new(TYPE_QEMU_S390_TOD);
26 }
27 object_property_add_child(qdev_get_machine(), TYPE_S390_TOD, obj, NULL);
28 object_unref(obj);
29
30 qdev_init_nofail(DEVICE(obj));
31 }
32
33 #define S390_TOD_CLOCK_VALUE_MISSING 0x00
34 #define S390_TOD_CLOCK_VALUE_PRESENT 0x01
35
36 static void s390_tod_save(QEMUFile *f, void *opaque)
37 {
38 S390TODState *td = opaque;
39 S390TODClass *tdc = S390_TOD_GET_CLASS(td);
40 Error *err = NULL;
41 S390TOD tod;
42
43 tdc->get(td, &tod, &err);
44 if (err) {
45 warn_report_err(err);
46 error_printf("Guest clock will not be migrated "
47 "which could cause the guest to hang.");
48 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING);
49 return;
50 }
51
52 qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT);
53 qemu_put_byte(f, tod.high);
54 qemu_put_be64(f, tod.low);
55 }
56
57 static int s390_tod_load(QEMUFile *f, void *opaque, int version_id)
58 {
59 S390TODState *td = opaque;
60 S390TODClass *tdc = S390_TOD_GET_CLASS(td);
61 Error *err = NULL;
62 S390TOD tod;
63
64 if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) {
65 warn_report("Guest clock was not migrated. This could "
66 "cause the guest to hang.");
67 return 0;
68 }
69
70 tod.high = qemu_get_byte(f);
71 tod.low = qemu_get_be64(f);
72
73 tdc->set(td, &tod, &err);
74 if (err) {
75 error_report_err(err);
76 return -1;
77 }
78 return 0;
79 }
80
81 static SaveVMHandlers savevm_tod = {
82 .save_state = s390_tod_save,
83 .load_state = s390_tod_load,
84 };
85
86 static void s390_tod_realize(DeviceState *dev, Error **errp)
87 {
88 S390TODState *td = S390_TOD(dev);
89
90 /* Legacy migration interface */
91 register_savevm_live(NULL, "todclock", 0, 1, &savevm_tod, td);
92 }
93
94 static void s390_tod_class_init(ObjectClass *oc, void *data)
95 {
96 DeviceClass *dc = DEVICE_CLASS(oc);
97
98 dc->desc = "TOD (Time Of Day) Clock";
99 dc->realize = s390_tod_realize;
100 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
101
102 /* We only have one TOD clock in the system attached to the machine */
103 dc->user_creatable = false;
104 }
105
106 static TypeInfo s390_tod_info = {
107 .name = TYPE_S390_TOD,
108 .parent = TYPE_DEVICE,
109 .instance_size = sizeof(S390TODState),
110 .class_init = s390_tod_class_init,
111 .class_size = sizeof(S390TODClass),
112 .abstract = true,
113 };
114
115 static void register_types(void)
116 {
117 type_register_static(&s390_tod_info);
118 }
119 type_init(register_types);