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-misc.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 "sysemu/kvm.h"
37 #define KVM_PIT_REINJECT_BIT 0
39 #define CALIBRATION_ROUNDS 3
41 #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
42 #define KVM_PIT_CLASS(class) \
43 OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
44 #define KVM_PIT_GET_CLASS(obj) \
45 OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
47 typedef struct KVMPITState
{
48 PITCommonState parent_obj
;
50 LostTickPolicy lost_tick_policy
;
52 int64_t kernel_clock_offset
;
55 typedef struct KVMPITClass
{
56 PITCommonClass parent_class
;
58 DeviceRealize parent_realize
;
61 static int64_t abs64(int64_t v
)
63 return v
< 0 ? -v
: v
;
66 static void kvm_pit_update_clock_offset(KVMPITState
*s
)
68 int64_t offset
, clock_offset
;
73 * Measure the delta between CLOCK_MONOTONIC, the base used for
74 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
75 * minimum of several samples to filter out scheduling noise.
77 clock_offset
= INT64_MAX
;
78 for (i
= 0; i
< CALIBRATION_ROUNDS
; i
++) {
79 offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
80 clock_gettime(CLOCK_MONOTONIC
, &ts
);
82 offset
-= (int64_t)ts
.tv_sec
* 1000000000;
83 if (abs64(offset
) < abs64(clock_offset
)) {
84 clock_offset
= offset
;
87 s
->kernel_clock_offset
= clock_offset
;
90 static void kvm_pit_get(PITCommonState
*pit
)
92 KVMPITState
*s
= KVM_PIT(pit
);
93 struct kvm_pit_state2 kpit
;
94 struct kvm_pit_channel_state
*kchan
;
95 struct PITChannelState
*sc
;
98 /* No need to re-read the state if VM is stopped. */
103 if (kvm_has_pit_state2()) {
104 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, &kpit
);
106 fprintf(stderr
, "KVM_GET_PIT2 failed: %s\n", strerror(ret
));
109 pit
->channels
[0].irq_disabled
= kpit
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
112 * kvm_pit_state2 is superset of kvm_pit_state struct,
113 * so we can use it for KVM_GET_PIT as well.
115 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, &kpit
);
117 fprintf(stderr
, "KVM_GET_PIT failed: %s\n", strerror(ret
));
121 for (i
= 0; i
< 3; i
++) {
122 kchan
= &kpit
.channels
[i
];
123 sc
= &pit
->channels
[i
];
124 sc
->count
= kchan
->count
;
125 sc
->latched_count
= kchan
->latched_count
;
126 sc
->count_latched
= kchan
->count_latched
;
127 sc
->status_latched
= kchan
->status_latched
;
128 sc
->status
= kchan
->status
;
129 sc
->read_state
= kchan
->read_state
;
130 sc
->write_state
= kchan
->write_state
;
131 sc
->write_latch
= kchan
->write_latch
;
132 sc
->rw_mode
= kchan
->rw_mode
;
133 sc
->mode
= kchan
->mode
;
134 sc
->bcd
= kchan
->bcd
;
135 sc
->gate
= kchan
->gate
;
136 sc
->count_load_time
= kchan
->count_load_time
+ s
->kernel_clock_offset
;
139 sc
= &pit
->channels
[0];
140 sc
->next_transition_time
=
141 pit_get_next_transition_time(sc
, sc
->count_load_time
);
144 static void kvm_pit_put(PITCommonState
*pit
)
146 KVMPITState
*s
= KVM_PIT(pit
);
147 struct kvm_pit_state2 kpit
= {};
148 struct kvm_pit_channel_state
*kchan
;
149 struct PITChannelState
*sc
;
152 /* The offset keeps changing as long as the VM is stopped. */
154 kvm_pit_update_clock_offset(s
);
157 kpit
.flags
= pit
->channels
[0].irq_disabled
? KVM_PIT_FLAGS_HPET_LEGACY
: 0;
158 for (i
= 0; i
< 3; i
++) {
159 kchan
= &kpit
.channels
[i
];
160 sc
= &pit
->channels
[i
];
161 kchan
->count
= sc
->count
;
162 kchan
->latched_count
= sc
->latched_count
;
163 kchan
->count_latched
= sc
->count_latched
;
164 kchan
->status_latched
= sc
->status_latched
;
165 kchan
->status
= sc
->status
;
166 kchan
->read_state
= sc
->read_state
;
167 kchan
->write_state
= sc
->write_state
;
168 kchan
->write_latch
= sc
->write_latch
;
169 kchan
->rw_mode
= sc
->rw_mode
;
170 kchan
->mode
= sc
->mode
;
171 kchan
->bcd
= sc
->bcd
;
172 kchan
->gate
= sc
->gate
;
173 kchan
->count_load_time
= sc
->count_load_time
- s
->kernel_clock_offset
;
176 ret
= kvm_vm_ioctl(kvm_state
,
177 kvm_has_pit_state2() ? KVM_SET_PIT2
: KVM_SET_PIT
,
180 fprintf(stderr
, "%s failed: %s\n",
181 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
187 static void kvm_pit_set_gate(PITCommonState
*s
, PITChannelState
*sc
, int val
)
195 /* XXX: just disable/enable counting */
201 if (sc
->gate
< val
) {
202 /* restart counting on rising edge */
203 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
212 static void kvm_pit_get_channel_info(PITCommonState
*s
, PITChannelState
*sc
,
213 PITChannelInfo
*info
)
217 pit_get_channel_info_common(s
, sc
, info
);
220 static void kvm_pit_reset(DeviceState
*dev
)
222 PITCommonState
*s
= PIT_COMMON(dev
);
229 static void kvm_pit_irq_control(void *opaque
, int n
, int enable
)
231 PITCommonState
*pit
= opaque
;
232 PITChannelState
*s
= &pit
->channels
[0];
236 s
->irq_disabled
= !enable
;
241 static void kvm_pit_vm_state_change(void *opaque
, int running
,
244 KVMPITState
*s
= opaque
;
247 kvm_pit_update_clock_offset(s
);
248 kvm_pit_put(PIT_COMMON(s
));
249 s
->vm_stopped
= false;
251 kvm_pit_update_clock_offset(s
);
252 kvm_pit_get(PIT_COMMON(s
));
253 s
->vm_stopped
= true;
257 static void kvm_pit_realizefn(DeviceState
*dev
, Error
**errp
)
259 PITCommonState
*pit
= PIT_COMMON(dev
);
260 KVMPITClass
*kpc
= KVM_PIT_GET_CLASS(dev
);
261 KVMPITState
*s
= KVM_PIT(pit
);
262 struct kvm_pit_config config
= {
267 if (kvm_check_extension(kvm_state
, KVM_CAP_PIT2
)) {
268 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT2
, &config
);
270 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
273 error_setg(errp
, "Create kernel PIC irqchip failed: %s",
277 switch (s
->lost_tick_policy
) {
278 case LOST_TICK_POLICY_DELAY
:
279 break; /* enabled by default */
280 case LOST_TICK_POLICY_DISCARD
:
281 if (kvm_check_extension(kvm_state
, KVM_CAP_REINJECT_CONTROL
)) {
282 struct kvm_reinject_control control
= { .pit_reinject
= 0 };
284 ret
= kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
287 "Can't disable in-kernel PIT reinjection: %s",
294 error_setg(errp
, "Lost tick policy not supported.");
298 memory_region_init_io(&pit
->ioports
, OBJECT(dev
), NULL
, NULL
, "kvm-pit", 4);
300 qdev_init_gpio_in(dev
, kvm_pit_irq_control
, 1);
302 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change
, s
);
304 kpc
->parent_realize(dev
, errp
);
307 static Property kvm_pit_properties
[] = {
308 DEFINE_PROP_UINT32("iobase", PITCommonState
, iobase
, -1),
309 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState
,
310 lost_tick_policy
, LOST_TICK_POLICY_DELAY
),
311 DEFINE_PROP_END_OF_LIST(),
314 static void kvm_pit_class_init(ObjectClass
*klass
, void *data
)
316 KVMPITClass
*kpc
= KVM_PIT_CLASS(klass
);
317 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
318 DeviceClass
*dc
= DEVICE_CLASS(klass
);
320 device_class_set_parent_realize(dc
, kvm_pit_realizefn
,
321 &kpc
->parent_realize
);
322 k
->set_channel_gate
= kvm_pit_set_gate
;
323 k
->get_channel_info
= kvm_pit_get_channel_info
;
324 dc
->reset
= kvm_pit_reset
;
325 dc
->props
= kvm_pit_properties
;
328 static const TypeInfo kvm_pit_info
= {
329 .name
= TYPE_KVM_I8254
,
330 .parent
= TYPE_PIT_COMMON
,
331 .instance_size
= sizeof(KVMPITState
),
332 .class_init
= kvm_pit_class_init
,
333 .class_size
= sizeof(KVMPITClass
),
336 static void kvm_pit_register(void)
338 type_register_static(&kvm_pit_info
);
341 type_init(kvm_pit_register
)