2 * KVM in-kernel PIT (i8254) support
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2012 Jan Kiszka, Siemens AG
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/timer.h"
27 #include "sysemu/sysemu.h"
28 #include "hw/timer/i8254.h"
29 #include "hw/timer/i8254_internal.h"
30 #include "sysemu/kvm.h"
32 #define KVM_PIT_REINJECT_BIT 0
34 #define CALIBRATION_ROUNDS 3
36 #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
37 #define KVM_PIT_CLASS(class) \
38 OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
39 #define KVM_PIT_GET_CLASS(obj) \
40 OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
42 typedef struct KVMPITState {
43 PITCommonState parent_obj;
45 LostTickPolicy lost_tick_policy;
47 int64_t kernel_clock_offset;
50 typedef struct KVMPITClass {
51 PITCommonClass parent_class;
53 DeviceRealize parent_realize;
56 static int64_t abs64(int64_t v)
58 return v < 0 ? -v : v;
61 static void kvm_pit_update_clock_offset(KVMPITState *s)
63 int64_t offset, clock_offset;
68 * Measure the delta between CLOCK_MONOTONIC, the base used for
69 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
70 * minimum of several samples to filter out scheduling noise.
72 clock_offset = INT64_MAX;
73 for (i = 0; i < CALIBRATION_ROUNDS; i++) {
74 offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
75 clock_gettime(CLOCK_MONOTONIC, &ts);
77 offset -= (int64_t)ts.tv_sec * 1000000000;
78 if (abs64(offset) < abs64(clock_offset)) {
79 clock_offset = offset;
82 s->kernel_clock_offset = clock_offset;
85 static void kvm_pit_get(PITCommonState *pit)
87 KVMPITState *s = KVM_PIT(pit);
88 struct kvm_pit_state2 kpit;
89 struct kvm_pit_channel_state *kchan;
90 struct PITChannelState *sc;
93 /* No need to re-read the state if VM is stopped. */
98 if (kvm_has_pit_state2()) {
99 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
101 fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
104 pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
107 * kvm_pit_state2 is superset of kvm_pit_state struct,
108 * so we can use it for KVM_GET_PIT as well.
110 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
112 fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
116 for (i = 0; i < 3; i++) {
117 kchan = &kpit.channels[i];
118 sc = &pit->channels[i];
119 sc->count = kchan->count;
120 sc->latched_count = kchan->latched_count;
121 sc->count_latched = kchan->count_latched;
122 sc->status_latched = kchan->status_latched;
123 sc->status = kchan->status;
124 sc->read_state = kchan->read_state;
125 sc->write_state = kchan->write_state;
126 sc->write_latch = kchan->write_latch;
127 sc->rw_mode = kchan->rw_mode;
128 sc->mode = kchan->mode;
129 sc->bcd = kchan->bcd;
130 sc->gate = kchan->gate;
131 sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
134 sc = &pit->channels[0];
135 sc->next_transition_time =
136 pit_get_next_transition_time(sc, sc->count_load_time);
139 static void kvm_pit_put(PITCommonState *pit)
141 KVMPITState *s = KVM_PIT(pit);
142 struct kvm_pit_state2 kpit = {};
143 struct kvm_pit_channel_state *kchan;
144 struct PITChannelState *sc;
147 /* The offset keeps changing as long as the VM is stopped. */
149 kvm_pit_update_clock_offset(s);
152 kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
153 for (i = 0; i < 3; i++) {
154 kchan = &kpit.channels[i];
155 sc = &pit->channels[i];
156 kchan->count = sc->count;
157 kchan->latched_count = sc->latched_count;
158 kchan->count_latched = sc->count_latched;
159 kchan->status_latched = sc->status_latched;
160 kchan->status = sc->status;
161 kchan->read_state = sc->read_state;
162 kchan->write_state = sc->write_state;
163 kchan->write_latch = sc->write_latch;
164 kchan->rw_mode = sc->rw_mode;
165 kchan->mode = sc->mode;
166 kchan->bcd = sc->bcd;
167 kchan->gate = sc->gate;
168 kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
171 ret = kvm_vm_ioctl(kvm_state,
172 kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
175 fprintf(stderr, "%s failed: %s\n",
176 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
182 static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
190 /* XXX: just disable/enable counting */
196 if (sc->gate < val) {
197 /* restart counting on rising edge */
198 sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
207 static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
208 PITChannelInfo *info)
212 pit_get_channel_info_common(s, sc, info);
215 static void kvm_pit_reset(DeviceState *dev)
217 PITCommonState *s = PIT_COMMON(dev);
224 static void kvm_pit_irq_control(void *opaque, int n, int enable)
226 PITCommonState *pit = opaque;
227 PITChannelState *s = &pit->channels[0];
231 s->irq_disabled = !enable;
236 static void kvm_pit_vm_state_change(void *opaque, int running,
239 KVMPITState *s = opaque;
242 kvm_pit_update_clock_offset(s);
243 kvm_pit_put(PIT_COMMON(s));
244 s->vm_stopped = false;
246 kvm_pit_update_clock_offset(s);
247 kvm_pit_get(PIT_COMMON(s));
248 s->vm_stopped = true;
252 static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
254 PITCommonState *pit = PIT_COMMON(dev);
255 KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
256 KVMPITState *s = KVM_PIT(pit);
257 struct kvm_pit_config config = {
262 if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
263 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
265 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
268 error_setg(errp, "Create kernel PIC irqchip failed: %s",
272 switch (s->lost_tick_policy) {
273 case LOST_TICK_POLICY_DELAY:
274 break; /* enabled by default */
275 case LOST_TICK_POLICY_DISCARD:
276 if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
277 struct kvm_reinject_control control = { .pit_reinject = 0 };
279 ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
282 "Can't disable in-kernel PIT reinjection: %s",
289 error_setg(errp, "Lost tick policy not supported.");
293 memory_region_init_reservation(&pit->ioports, NULL, "kvm-pit", 4);
295 qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
297 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
299 kpc->parent_realize(dev, errp);
302 static Property kvm_pit_properties[] = {
303 DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
304 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
305 lost_tick_policy, LOST_TICK_POLICY_DELAY),
306 DEFINE_PROP_END_OF_LIST(),
309 static void kvm_pit_class_init(ObjectClass *klass, void *data)
311 KVMPITClass *kpc = KVM_PIT_CLASS(klass);
312 PITCommonClass *k = PIT_COMMON_CLASS(klass);
313 DeviceClass *dc = DEVICE_CLASS(klass);
315 kpc->parent_realize = dc->realize;
316 dc->realize = kvm_pit_realizefn;
317 k->set_channel_gate = kvm_pit_set_gate;
318 k->get_channel_info = kvm_pit_get_channel_info;
319 dc->reset = kvm_pit_reset;
320 dc->props = kvm_pit_properties;
323 static const TypeInfo kvm_pit_info = {
324 .name = TYPE_KVM_I8254,
325 .parent = TYPE_PIT_COMMON,
326 .instance_size = sizeof(KVMPITState),
327 .class_init = kvm_pit_class_init,
328 .class_size = sizeof(KVMPITClass),
331 static void kvm_pit_register(void)
333 type_register_static(&kvm_pit_info);
336 type_init(kvm_pit_register)