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
26 #include "qemu/osdep.h"
27 #include <linux/kvm.h>
28 #include "qapi/qapi-types-machine.h"
29 #include "qapi/error.h"
30 #include "qemu/module.h"
31 #include "qemu/timer.h"
32 #include "sysemu/runstate.h"
33 #include "hw/timer/i8254.h"
34 #include "hw/timer/i8254_internal.h"
35 #include "hw/qdev-properties-system.h"
36 #include "sysemu/kvm.h"
37 #include "target/i386/kvm/kvm_i386.h"
38 #include "qom/object.h"
40 #define KVM_PIT_REINJECT_BIT 0
42 #define CALIBRATION_ROUNDS 3
44 typedef struct KVMPITClass KVMPITClass
;
45 typedef struct KVMPITState KVMPITState
;
46 DECLARE_OBJ_CHECKERS(KVMPITState
, KVMPITClass
,
47 KVM_PIT
, TYPE_KVM_I8254
)
50 PITCommonState parent_obj
;
52 LostTickPolicy lost_tick_policy
;
54 int64_t kernel_clock_offset
;
58 PITCommonClass parent_class
;
60 DeviceRealize parent_realize
;
63 static void kvm_pit_update_clock_offset(KVMPITState
*s
)
65 int64_t offset
, clock_offset
;
70 * Measure the delta between CLOCK_MONOTONIC, the base used for
71 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
72 * minimum of several samples to filter out scheduling noise.
74 clock_offset
= INT64_MAX
;
75 for (i
= 0; i
< CALIBRATION_ROUNDS
; i
++) {
76 offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
77 clock_gettime(CLOCK_MONOTONIC
, &ts
);
79 offset
-= (int64_t)ts
.tv_sec
* 1000000000;
80 if (uabs64(offset
) < uabs64(clock_offset
)) {
81 clock_offset
= offset
;
84 s
->kernel_clock_offset
= clock_offset
;
87 static void kvm_pit_get(PITCommonState
*pit
)
89 KVMPITState
*s
= KVM_PIT(pit
);
90 struct kvm_pit_state2 kpit
;
91 struct kvm_pit_channel_state
*kchan
;
92 struct PITChannelState
*sc
;
95 /* No need to re-read the state if VM is stopped. */
100 if (kvm_has_pit_state2()) {
101 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, &kpit
);
103 fprintf(stderr
, "KVM_GET_PIT2 failed: %s\n", strerror(-ret
));
106 pit
->channels
[0].irq_disabled
= kpit
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
109 * kvm_pit_state2 is superset of kvm_pit_state struct,
110 * so we can use it for KVM_GET_PIT as well.
112 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, &kpit
);
114 fprintf(stderr
, "KVM_GET_PIT failed: %s\n", strerror(-ret
));
118 for (i
= 0; i
< 3; i
++) {
119 kchan
= &kpit
.channels
[i
];
120 sc
= &pit
->channels
[i
];
121 sc
->count
= kchan
->count
;
122 sc
->latched_count
= kchan
->latched_count
;
123 sc
->count_latched
= kchan
->count_latched
;
124 sc
->status_latched
= kchan
->status_latched
;
125 sc
->status
= kchan
->status
;
126 sc
->read_state
= kchan
->read_state
;
127 sc
->write_state
= kchan
->write_state
;
128 sc
->write_latch
= kchan
->write_latch
;
129 sc
->rw_mode
= kchan
->rw_mode
;
130 sc
->mode
= kchan
->mode
;
131 sc
->bcd
= kchan
->bcd
;
132 sc
->gate
= kchan
->gate
;
133 sc
->count_load_time
= kchan
->count_load_time
+ s
->kernel_clock_offset
;
136 sc
= &pit
->channels
[0];
137 sc
->next_transition_time
=
138 pit_get_next_transition_time(sc
, sc
->count_load_time
);
141 static void kvm_pit_put(PITCommonState
*pit
)
143 KVMPITState
*s
= KVM_PIT(pit
);
144 struct kvm_pit_state2 kpit
= {};
145 struct kvm_pit_channel_state
*kchan
;
146 struct PITChannelState
*sc
;
149 /* The offset keeps changing as long as the VM is stopped. */
151 kvm_pit_update_clock_offset(s
);
154 kpit
.flags
= pit
->channels
[0].irq_disabled
? KVM_PIT_FLAGS_HPET_LEGACY
: 0;
155 for (i
= 0; i
< 3; i
++) {
156 kchan
= &kpit
.channels
[i
];
157 sc
= &pit
->channels
[i
];
158 kchan
->count
= sc
->count
;
159 kchan
->latched_count
= sc
->latched_count
;
160 kchan
->count_latched
= sc
->count_latched
;
161 kchan
->status_latched
= sc
->status_latched
;
162 kchan
->status
= sc
->status
;
163 kchan
->read_state
= sc
->read_state
;
164 kchan
->write_state
= sc
->write_state
;
165 kchan
->write_latch
= sc
->write_latch
;
166 kchan
->rw_mode
= sc
->rw_mode
;
167 kchan
->mode
= sc
->mode
;
168 kchan
->bcd
= sc
->bcd
;
169 kchan
->gate
= sc
->gate
;
170 kchan
->count_load_time
= sc
->count_load_time
- s
->kernel_clock_offset
;
173 ret
= kvm_vm_ioctl(kvm_state
,
174 kvm_has_pit_state2() ? KVM_SET_PIT2
: KVM_SET_PIT
,
177 fprintf(stderr
, "%s failed: %s\n",
178 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
184 static void kvm_pit_set_gate(PITCommonState
*s
, PITChannelState
*sc
, int val
)
192 /* XXX: just disable/enable counting */
198 if (sc
->gate
< val
) {
199 /* restart counting on rising edge */
200 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
209 static void kvm_pit_get_channel_info(PITCommonState
*s
, PITChannelState
*sc
,
210 PITChannelInfo
*info
)
214 pit_get_channel_info_common(s
, sc
, info
);
217 static void kvm_pit_reset(DeviceState
*dev
)
219 PITCommonState
*s
= PIT_COMMON(dev
);
226 static void kvm_pit_irq_control(void *opaque
, int n
, int enable
)
228 PITCommonState
*pit
= opaque
;
229 PITChannelState
*s
= &pit
->channels
[0];
233 s
->irq_disabled
= !enable
;
238 static void kvm_pit_vm_state_change(void *opaque
, bool running
,
241 KVMPITState
*s
= opaque
;
244 kvm_pit_update_clock_offset(s
);
245 kvm_pit_put(PIT_COMMON(s
));
246 s
->vm_stopped
= false;
248 kvm_pit_update_clock_offset(s
);
249 kvm_pit_get(PIT_COMMON(s
));
250 s
->vm_stopped
= true;
254 static void kvm_pit_realizefn(DeviceState
*dev
, Error
**errp
)
256 PITCommonState
*pit
= PIT_COMMON(dev
);
257 KVMPITClass
*kpc
= KVM_PIT_GET_CLASS(dev
);
258 KVMPITState
*s
= KVM_PIT(pit
);
259 struct kvm_pit_config config
= {
264 if (kvm_check_extension(kvm_state
, KVM_CAP_PIT2
)) {
265 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT2
, &config
);
267 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
270 error_setg(errp
, "Create kernel PIC irqchip failed: %s",
274 switch (s
->lost_tick_policy
) {
275 case LOST_TICK_POLICY_DELAY
:
276 break; /* enabled by default */
277 case LOST_TICK_POLICY_DISCARD
:
278 if (kvm_check_extension(kvm_state
, KVM_CAP_REINJECT_CONTROL
)) {
279 struct kvm_reinject_control control
= { .pit_reinject
= 0 };
281 ret
= kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
284 "Can't disable in-kernel PIT reinjection: %s",
291 error_setg(errp
, "Lost tick policy not supported.");
295 memory_region_init_io(&pit
->ioports
, OBJECT(dev
), NULL
, NULL
, "kvm-pit", 4);
297 qdev_init_gpio_in(dev
, kvm_pit_irq_control
, 1);
299 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change
, s
);
301 kpc
->parent_realize(dev
, errp
);
304 static Property kvm_pit_properties
[] = {
305 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState
,
306 lost_tick_policy
, LOST_TICK_POLICY_DELAY
),
307 DEFINE_PROP_END_OF_LIST(),
310 static void kvm_pit_class_init(ObjectClass
*klass
, void *data
)
312 KVMPITClass
*kpc
= KVM_PIT_CLASS(klass
);
313 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
314 DeviceClass
*dc
= DEVICE_CLASS(klass
);
316 device_class_set_parent_realize(dc
, kvm_pit_realizefn
,
317 &kpc
->parent_realize
);
318 k
->set_channel_gate
= kvm_pit_set_gate
;
319 k
->get_channel_info
= kvm_pit_get_channel_info
;
320 dc
->reset
= kvm_pit_reset
;
321 device_class_set_props(dc
, kvm_pit_properties
);
324 static const TypeInfo kvm_pit_info
= {
325 .name
= TYPE_KVM_I8254
,
326 .parent
= TYPE_PIT_COMMON
,
327 .instance_size
= sizeof(KVMPITState
),
328 .class_init
= kvm_pit_class_init
,
329 .class_size
= sizeof(KVMPITClass
),
332 static void kvm_pit_register(void)
334 type_register_static(&kvm_pit_info
);
337 type_init(kvm_pit_register
)