| 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 "qemu/module.h" |
| 16 | #include "system/kvm.h" |
| 17 | #include "system/tcg.h" |
| 18 | #include "system/qtest.h" |
| 19 | #include "migration/qemu-file-types.h" |
| 20 | #include "migration/register.h" |
| 21 | |
| 22 | void s390_init_tod(void) |
| 23 | { |
| 24 | Object *obj; |
| 25 | |
| 26 | if (kvm_enabled()) { |
| 27 | obj = object_new(TYPE_KVM_S390_TOD); |
| 28 | } else if (tcg_enabled()) { |
| 29 | obj = object_new(TYPE_QEMU_S390_TOD); |
| 30 | } else if (qtest_enabled()) { |
| 31 | return; |
| 32 | } else { |
| 33 | error_report("current accelerator not handled in s390_init_tod!"); |
| 34 | abort(); |
| 35 | } |
| 36 | object_property_add_child(qdev_get_machine(), TYPE_S390_TOD, obj); |
| 37 | object_unref(obj); |
| 38 | |
| 39 | qdev_realize(DEVICE(obj), NULL, &error_fatal); |
| 40 | } |
| 41 | |
| 42 | S390TODState *s390_get_todstate(void) |
| 43 | { |
| 44 | static S390TODState *ts; |
| 45 | |
| 46 | if (!ts) { |
| 47 | ts = S390_TOD(object_resolve_path_type("", TYPE_S390_TOD, NULL)); |
| 48 | } |
| 49 | |
| 50 | return ts; |
| 51 | } |
| 52 | |
| 53 | #define S390_TOD_CLOCK_VALUE_MISSING 0x00 |
| 54 | #define S390_TOD_CLOCK_VALUE_PRESENT 0x01 |
| 55 | |
| 56 | static void s390_tod_save(QEMUFile *f, void *opaque) |
| 57 | { |
| 58 | S390TODState *td = opaque; |
| 59 | S390TODClass *tdc = S390_TOD_GET_CLASS(td); |
| 60 | Error *err = NULL; |
| 61 | S390TOD tod; |
| 62 | |
| 63 | tdc->get(td, &tod, &err); |
| 64 | if (err) { |
| 65 | warn_report_err(err); |
| 66 | error_printf("Guest clock will not be migrated " |
| 67 | "which could cause the guest to hang."); |
| 68 | qemu_put_byte(f, S390_TOD_CLOCK_VALUE_MISSING); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | qemu_put_byte(f, S390_TOD_CLOCK_VALUE_PRESENT); |
| 73 | qemu_put_byte(f, tod.high); |
| 74 | qemu_put_be64(f, tod.low); |
| 75 | |
| 76 | tdc->set(td, &tod, &err); |
| 77 | if (err) { |
| 78 | warn_report_err(err); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static int s390_tod_load(QEMUFile *f, void *opaque, int version_id) |
| 83 | { |
| 84 | S390TODState *td = opaque; |
| 85 | S390TODClass *tdc = S390_TOD_GET_CLASS(td); |
| 86 | Error *err = NULL; |
| 87 | S390TOD tod; |
| 88 | |
| 89 | if (qemu_get_byte(f) == S390_TOD_CLOCK_VALUE_MISSING) { |
| 90 | warn_report("Guest clock was not migrated. This could " |
| 91 | "cause the guest to hang."); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | tod.high = qemu_get_byte(f); |
| 96 | tod.low = qemu_get_be64(f); |
| 97 | |
| 98 | tdc->set(td, &tod, &err); |
| 99 | if (err) { |
| 100 | error_report_err(err); |
| 101 | return -1; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static SaveVMHandlers savevm_tod = { |
| 107 | .save_state = s390_tod_save, |
| 108 | .load_state = s390_tod_load, |
| 109 | }; |
| 110 | |
| 111 | static void s390_tod_realize(DeviceState *dev, Error **errp) |
| 112 | { |
| 113 | S390TODState *td = S390_TOD(dev); |
| 114 | |
| 115 | /* Legacy migration interface */ |
| 116 | register_savevm_live("todclock", 0, 1, &savevm_tod, td); |
| 117 | } |
| 118 | |
| 119 | static void s390_tod_class_init(ObjectClass *oc, const void *data) |
| 120 | { |
| 121 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 122 | |
| 123 | dc->desc = "TOD (Time Of Day) Clock"; |
| 124 | dc->realize = s390_tod_realize; |
| 125 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 126 | |
| 127 | /* We only have one TOD clock in the system attached to the machine */ |
| 128 | dc->user_creatable = false; |
| 129 | } |
| 130 | |
| 131 | static const TypeInfo s390_tod_info = { |
| 132 | .name = TYPE_S390_TOD, |
| 133 | .parent = TYPE_DEVICE, |
| 134 | .instance_size = sizeof(S390TODState), |
| 135 | .class_init = s390_tod_class_init, |
| 136 | .class_size = sizeof(S390TODClass), |
| 137 | .abstract = true, |
| 138 | }; |
| 139 | |
| 140 | static void register_types(void) |
| 141 | { |
| 142 | type_register_static(&s390_tod_info); |
| 143 | } |
| 144 | type_init(register_types); |