qemu-ga: move channel/transport functionality into wrapper class
[qemu.git] / hw / kvm / clock.c
blob2157340326f21b8a12f2552b690e324e05446833
1 /*
2 * QEMU KVM support, paravirtual clock device
4 * Copyright (C) 2011 Siemens AG
6 * Authors:
7 * Jan Kiszka <jan.kiszka@siemens.com>
9 * This work is licensed under the terms of the GNU GPL version 2.
10 * See the COPYING file in the top-level directory.
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "sysemu.h"
18 #include "kvm.h"
19 #include "hw/sysbus.h"
20 #include "hw/kvm/clock.h"
22 #include <linux/kvm.h>
23 #include <linux/kvm_para.h>
25 typedef struct KVMClockState {
26 SysBusDevice busdev;
27 uint64_t clock;
28 bool clock_valid;
29 } KVMClockState;
31 static void kvmclock_pre_save(void *opaque)
33 KVMClockState *s = opaque;
34 struct kvm_clock_data data;
35 int ret;
37 if (s->clock_valid) {
38 return;
40 ret = kvm_vm_ioctl(kvm_state, KVM_GET_CLOCK, &data);
41 if (ret < 0) {
42 fprintf(stderr, "KVM_GET_CLOCK failed: %s\n", strerror(ret));
43 data.clock = 0;
45 s->clock = data.clock;
47 * If the VM is stopped, declare the clock state valid to avoid re-reading
48 * it on next vmsave (which would return a different value). Will be reset
49 * when the VM is continued.
51 s->clock_valid = !runstate_is_running();
54 static int kvmclock_post_load(void *opaque, int version_id)
56 KVMClockState *s = opaque;
57 struct kvm_clock_data data;
59 data.clock = s->clock;
60 data.flags = 0;
61 return kvm_vm_ioctl(kvm_state, KVM_SET_CLOCK, &data);
64 static void kvmclock_vm_state_change(void *opaque, int running,
65 RunState state)
67 KVMClockState *s = opaque;
69 if (running) {
70 s->clock_valid = false;
74 static int kvmclock_init(SysBusDevice *dev)
76 KVMClockState *s = FROM_SYSBUS(KVMClockState, dev);
78 qemu_add_vm_change_state_handler(kvmclock_vm_state_change, s);
79 return 0;
82 static const VMStateDescription kvmclock_vmsd = {
83 .name = "kvmclock",
84 .version_id = 1,
85 .minimum_version_id = 1,
86 .minimum_version_id_old = 1,
87 .pre_save = kvmclock_pre_save,
88 .post_load = kvmclock_post_load,
89 .fields = (VMStateField[]) {
90 VMSTATE_UINT64(clock, KVMClockState),
91 VMSTATE_END_OF_LIST()
95 static void kvmclock_class_init(ObjectClass *klass, void *data)
97 DeviceClass *dc = DEVICE_CLASS(klass);
98 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
100 k->init = kvmclock_init;
101 dc->no_user = 1;
102 dc->vmsd = &kvmclock_vmsd;
105 static TypeInfo kvmclock_info = {
106 .name = "kvmclock",
107 .parent = TYPE_SYS_BUS_DEVICE,
108 .instance_size = sizeof(KVMClockState),
109 .class_init = kvmclock_class_init,
112 /* Note: Must be called after VCPU initialization. */
113 void kvmclock_create(void)
115 if (kvm_enabled() &&
116 first_cpu->cpuid_kvm_features & ((1ULL << KVM_FEATURE_CLOCKSOURCE) |
117 (1ULL << KVM_FEATURE_CLOCKSOURCE2))) {
118 sysbus_create_simple("kvmclock", -1, NULL);
122 static void kvmclock_register_types(void)
124 if (kvm_enabled()) {
125 type_register_static(&kvmclock_info);
129 type_init(kvmclock_register_types)