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/error.h"
29 #include "qemu/timer.h"
30 #include "sysemu/sysemu.h"
31 #include "hw/timer/i8254.h"
32 #include "hw/timer/i8254_internal.h"
33 #include "sysemu/kvm.h"
35 #define KVM_PIT_REINJECT_BIT 0
37 #define CALIBRATION_ROUNDS 3
39 #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
40 #define KVM_PIT_CLASS(class) \
41 OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
42 #define KVM_PIT_GET_CLASS(obj) \
43 OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
45 typedef struct KVMPITState
{
46 PITCommonState parent_obj
;
48 LostTickPolicy lost_tick_policy
;
50 int64_t kernel_clock_offset
;
53 typedef struct KVMPITClass
{
54 PITCommonClass parent_class
;
56 DeviceRealize parent_realize
;
59 static int64_t abs64(int64_t v
)
61 return v
< 0 ? -v
: v
;
64 static void kvm_pit_update_clock_offset(KVMPITState
*s
)
66 int64_t offset
, clock_offset
;
71 * Measure the delta between CLOCK_MONOTONIC, the base used for
72 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
73 * minimum of several samples to filter out scheduling noise.
75 clock_offset
= INT64_MAX
;
76 for (i
= 0; i
< CALIBRATION_ROUNDS
; i
++) {
77 offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
78 clock_gettime(CLOCK_MONOTONIC
, &ts
);
80 offset
-= (int64_t)ts
.tv_sec
* 1000000000;
81 if (abs64(offset
) < abs64(clock_offset
)) {
82 clock_offset
= offset
;
85 s
->kernel_clock_offset
= clock_offset
;
88 static void kvm_pit_get(PITCommonState
*pit
)
90 KVMPITState
*s
= KVM_PIT(pit
);
91 struct kvm_pit_state2 kpit
;
92 struct kvm_pit_channel_state
*kchan
;
93 struct PITChannelState
*sc
;
96 /* No need to re-read the state if VM is stopped. */
101 if (kvm_has_pit_state2()) {
102 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, &kpit
);
104 fprintf(stderr
, "KVM_GET_PIT2 failed: %s\n", strerror(ret
));
107 pit
->channels
[0].irq_disabled
= kpit
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
110 * kvm_pit_state2 is superset of kvm_pit_state struct,
111 * so we can use it for KVM_GET_PIT as well.
113 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, &kpit
);
115 fprintf(stderr
, "KVM_GET_PIT failed: %s\n", strerror(ret
));
119 for (i
= 0; i
< 3; i
++) {
120 kchan
= &kpit
.channels
[i
];
121 sc
= &pit
->channels
[i
];
122 sc
->count
= kchan
->count
;
123 sc
->latched_count
= kchan
->latched_count
;
124 sc
->count_latched
= kchan
->count_latched
;
125 sc
->status_latched
= kchan
->status_latched
;
126 sc
->status
= kchan
->status
;
127 sc
->read_state
= kchan
->read_state
;
128 sc
->write_state
= kchan
->write_state
;
129 sc
->write_latch
= kchan
->write_latch
;
130 sc
->rw_mode
= kchan
->rw_mode
;
131 sc
->mode
= kchan
->mode
;
132 sc
->bcd
= kchan
->bcd
;
133 sc
->gate
= kchan
->gate
;
134 sc
->count_load_time
= kchan
->count_load_time
+ s
->kernel_clock_offset
;
137 sc
= &pit
->channels
[0];
138 sc
->next_transition_time
=
139 pit_get_next_transition_time(sc
, sc
->count_load_time
);
142 static void kvm_pit_put(PITCommonState
*pit
)
144 KVMPITState
*s
= KVM_PIT(pit
);
145 struct kvm_pit_state2 kpit
= {};
146 struct kvm_pit_channel_state
*kchan
;
147 struct PITChannelState
*sc
;
150 /* The offset keeps changing as long as the VM is stopped. */
152 kvm_pit_update_clock_offset(s
);
155 kpit
.flags
= pit
->channels
[0].irq_disabled
? KVM_PIT_FLAGS_HPET_LEGACY
: 0;
156 for (i
= 0; i
< 3; i
++) {
157 kchan
= &kpit
.channels
[i
];
158 sc
= &pit
->channels
[i
];
159 kchan
->count
= sc
->count
;
160 kchan
->latched_count
= sc
->latched_count
;
161 kchan
->count_latched
= sc
->count_latched
;
162 kchan
->status_latched
= sc
->status_latched
;
163 kchan
->status
= sc
->status
;
164 kchan
->read_state
= sc
->read_state
;
165 kchan
->write_state
= sc
->write_state
;
166 kchan
->write_latch
= sc
->write_latch
;
167 kchan
->rw_mode
= sc
->rw_mode
;
168 kchan
->mode
= sc
->mode
;
169 kchan
->bcd
= sc
->bcd
;
170 kchan
->gate
= sc
->gate
;
171 kchan
->count_load_time
= sc
->count_load_time
- s
->kernel_clock_offset
;
174 ret
= kvm_vm_ioctl(kvm_state
,
175 kvm_has_pit_state2() ? KVM_SET_PIT2
: KVM_SET_PIT
,
178 fprintf(stderr
, "%s failed: %s\n",
179 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
185 static void kvm_pit_set_gate(PITCommonState
*s
, PITChannelState
*sc
, int val
)
193 /* XXX: just disable/enable counting */
199 if (sc
->gate
< val
) {
200 /* restart counting on rising edge */
201 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
210 static void kvm_pit_get_channel_info(PITCommonState
*s
, PITChannelState
*sc
,
211 PITChannelInfo
*info
)
215 pit_get_channel_info_common(s
, sc
, info
);
218 static void kvm_pit_reset(DeviceState
*dev
)
220 PITCommonState
*s
= PIT_COMMON(dev
);
227 static void kvm_pit_irq_control(void *opaque
, int n
, int enable
)
229 PITCommonState
*pit
= opaque
;
230 PITChannelState
*s
= &pit
->channels
[0];
234 s
->irq_disabled
= !enable
;
239 static void kvm_pit_vm_state_change(void *opaque
, int running
,
242 KVMPITState
*s
= opaque
;
245 kvm_pit_update_clock_offset(s
);
246 kvm_pit_put(PIT_COMMON(s
));
247 s
->vm_stopped
= false;
249 kvm_pit_update_clock_offset(s
);
250 kvm_pit_get(PIT_COMMON(s
));
251 s
->vm_stopped
= true;
255 static void kvm_pit_realizefn(DeviceState
*dev
, Error
**errp
)
257 PITCommonState
*pit
= PIT_COMMON(dev
);
258 KVMPITClass
*kpc
= KVM_PIT_GET_CLASS(dev
);
259 KVMPITState
*s
= KVM_PIT(pit
);
260 struct kvm_pit_config config
= {
265 if (kvm_check_extension(kvm_state
, KVM_CAP_PIT2
)) {
266 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT2
, &config
);
268 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
271 error_setg(errp
, "Create kernel PIC irqchip failed: %s",
275 switch (s
->lost_tick_policy
) {
276 case LOST_TICK_POLICY_DELAY
:
277 break; /* enabled by default */
278 case LOST_TICK_POLICY_DISCARD
:
279 if (kvm_check_extension(kvm_state
, KVM_CAP_REINJECT_CONTROL
)) {
280 struct kvm_reinject_control control
= { .pit_reinject
= 0 };
282 ret
= kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
285 "Can't disable in-kernel PIT reinjection: %s",
292 error_setg(errp
, "Lost tick policy not supported.");
296 memory_region_init_io(&pit
->ioports
, OBJECT(dev
), NULL
, NULL
, "kvm-pit", 4);
298 qdev_init_gpio_in(dev
, kvm_pit_irq_control
, 1);
300 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change
, s
);
302 kpc
->parent_realize(dev
, errp
);
305 static Property kvm_pit_properties
[] = {
306 DEFINE_PROP_UINT32("iobase", PITCommonState
, iobase
, -1),
307 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState
,
308 lost_tick_policy
, LOST_TICK_POLICY_DELAY
),
309 DEFINE_PROP_END_OF_LIST(),
312 static void kvm_pit_class_init(ObjectClass
*klass
, void *data
)
314 KVMPITClass
*kpc
= KVM_PIT_CLASS(klass
);
315 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
316 DeviceClass
*dc
= DEVICE_CLASS(klass
);
318 device_class_set_parent_realize(dc
, kvm_pit_realizefn
,
319 &kpc
->parent_realize
);
320 k
->set_channel_gate
= kvm_pit_set_gate
;
321 k
->get_channel_info
= kvm_pit_get_channel_info
;
322 dc
->reset
= kvm_pit_reset
;
323 dc
->props
= kvm_pit_properties
;
326 static const TypeInfo kvm_pit_info
= {
327 .name
= TYPE_KVM_I8254
,
328 .parent
= TYPE_PIT_COMMON
,
329 .instance_size
= sizeof(KVMPITState
),
330 .class_init
= kvm_pit_class_init
,
331 .class_size
= sizeof(KVMPITClass
),
334 static void kvm_pit_register(void)
336 type_register_static(&kvm_pit_info
);
339 type_init(kvm_pit_register
)