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/timer.h"
26 #include "sysemu/sysemu.h"
27 #include "hw/timer/i8254.h"
28 #include "hw/timer/i8254_internal.h"
29 #include "sysemu/kvm.h"
31 #define KVM_PIT_REINJECT_BIT 0
33 #define CALIBRATION_ROUNDS 3
35 #define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
36 #define KVM_PIT_CLASS(class) \
37 OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
38 #define KVM_PIT_GET_CLASS(obj) \
39 OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
41 typedef struct KVMPITState
{
42 PITCommonState parent_obj
;
44 LostTickPolicy lost_tick_policy
;
46 int64_t kernel_clock_offset
;
49 typedef struct KVMPITClass
{
50 PITCommonClass parent_class
;
52 DeviceRealize parent_realize
;
55 static int64_t abs64(int64_t v
)
57 return v
< 0 ? -v
: v
;
60 static void kvm_pit_update_clock_offset(KVMPITState
*s
)
62 int64_t offset
, clock_offset
;
67 * Measure the delta between CLOCK_MONOTONIC, the base used for
68 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
69 * minimum of several samples to filter out scheduling noise.
71 clock_offset
= INT64_MAX
;
72 for (i
= 0; i
< CALIBRATION_ROUNDS
; i
++) {
73 offset
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
74 clock_gettime(CLOCK_MONOTONIC
, &ts
);
76 offset
-= (int64_t)ts
.tv_sec
* 1000000000;
77 if (abs64(offset
) < abs64(clock_offset
)) {
78 clock_offset
= offset
;
81 s
->kernel_clock_offset
= clock_offset
;
84 static void kvm_pit_get(PITCommonState
*pit
)
86 KVMPITState
*s
= KVM_PIT(pit
);
87 struct kvm_pit_state2 kpit
;
88 struct kvm_pit_channel_state
*kchan
;
89 struct PITChannelState
*sc
;
92 /* No need to re-read the state if VM is stopped. */
97 if (kvm_has_pit_state2()) {
98 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, &kpit
);
100 fprintf(stderr
, "KVM_GET_PIT2 failed: %s\n", strerror(ret
));
103 pit
->channels
[0].irq_disabled
= kpit
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
106 * kvm_pit_state2 is superset of kvm_pit_state struct,
107 * so we can use it for KVM_GET_PIT as well.
109 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, &kpit
);
111 fprintf(stderr
, "KVM_GET_PIT failed: %s\n", strerror(ret
));
115 for (i
= 0; i
< 3; i
++) {
116 kchan
= &kpit
.channels
[i
];
117 sc
= &pit
->channels
[i
];
118 sc
->count
= kchan
->count
;
119 sc
->latched_count
= kchan
->latched_count
;
120 sc
->count_latched
= kchan
->count_latched
;
121 sc
->status_latched
= kchan
->status_latched
;
122 sc
->status
= kchan
->status
;
123 sc
->read_state
= kchan
->read_state
;
124 sc
->write_state
= kchan
->write_state
;
125 sc
->write_latch
= kchan
->write_latch
;
126 sc
->rw_mode
= kchan
->rw_mode
;
127 sc
->mode
= kchan
->mode
;
128 sc
->bcd
= kchan
->bcd
;
129 sc
->gate
= kchan
->gate
;
130 sc
->count_load_time
= kchan
->count_load_time
+ s
->kernel_clock_offset
;
133 sc
= &pit
->channels
[0];
134 sc
->next_transition_time
=
135 pit_get_next_transition_time(sc
, sc
->count_load_time
);
138 static void kvm_pit_put(PITCommonState
*pit
)
140 KVMPITState
*s
= KVM_PIT(pit
);
141 struct kvm_pit_state2 kpit
;
142 struct kvm_pit_channel_state
*kchan
;
143 struct PITChannelState
*sc
;
146 /* The offset keeps changing as long as the VM is stopped. */
148 kvm_pit_update_clock_offset(s
);
151 kpit
.flags
= pit
->channels
[0].irq_disabled
? KVM_PIT_FLAGS_HPET_LEGACY
: 0;
152 for (i
= 0; i
< 3; i
++) {
153 kchan
= &kpit
.channels
[i
];
154 sc
= &pit
->channels
[i
];
155 kchan
->count
= sc
->count
;
156 kchan
->latched_count
= sc
->latched_count
;
157 kchan
->count_latched
= sc
->count_latched
;
158 kchan
->status_latched
= sc
->status_latched
;
159 kchan
->status
= sc
->status
;
160 kchan
->read_state
= sc
->read_state
;
161 kchan
->write_state
= sc
->write_state
;
162 kchan
->write_latch
= sc
->write_latch
;
163 kchan
->rw_mode
= sc
->rw_mode
;
164 kchan
->mode
= sc
->mode
;
165 kchan
->bcd
= sc
->bcd
;
166 kchan
->gate
= sc
->gate
;
167 kchan
->count_load_time
= sc
->count_load_time
- s
->kernel_clock_offset
;
170 ret
= kvm_vm_ioctl(kvm_state
,
171 kvm_has_pit_state2() ? KVM_SET_PIT2
: KVM_SET_PIT
,
174 fprintf(stderr
, "%s failed: %s\n",
175 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
181 static void kvm_pit_set_gate(PITCommonState
*s
, PITChannelState
*sc
, int val
)
189 /* XXX: just disable/enable counting */
195 if (sc
->gate
< val
) {
196 /* restart counting on rising edge */
197 sc
->count_load_time
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
206 static void kvm_pit_get_channel_info(PITCommonState
*s
, PITChannelState
*sc
,
207 PITChannelInfo
*info
)
211 pit_get_channel_info_common(s
, sc
, info
);
214 static void kvm_pit_reset(DeviceState
*dev
)
216 PITCommonState
*s
= PIT_COMMON(dev
);
223 static void kvm_pit_irq_control(void *opaque
, int n
, int enable
)
225 PITCommonState
*pit
= opaque
;
226 PITChannelState
*s
= &pit
->channels
[0];
230 s
->irq_disabled
= !enable
;
235 static void kvm_pit_vm_state_change(void *opaque
, int running
,
238 KVMPITState
*s
= opaque
;
241 kvm_pit_update_clock_offset(s
);
242 s
->vm_stopped
= false;
244 kvm_pit_update_clock_offset(s
);
245 kvm_pit_get(PIT_COMMON(s
));
246 s
->vm_stopped
= true;
250 static void kvm_pit_realizefn(DeviceState
*dev
, Error
**errp
)
252 PITCommonState
*pit
= PIT_COMMON(dev
);
253 KVMPITClass
*kpc
= KVM_PIT_GET_CLASS(dev
);
254 KVMPITState
*s
= KVM_PIT(pit
);
255 struct kvm_pit_config config
= {
260 if (kvm_check_extension(kvm_state
, KVM_CAP_PIT2
)) {
261 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT2
, &config
);
263 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
266 error_setg(errp
, "Create kernel PIC irqchip failed: %s",
270 switch (s
->lost_tick_policy
) {
271 case LOST_TICK_POLICY_DELAY
:
272 break; /* enabled by default */
273 case LOST_TICK_POLICY_DISCARD
:
274 if (kvm_check_extension(kvm_state
, KVM_CAP_REINJECT_CONTROL
)) {
275 struct kvm_reinject_control control
= { .pit_reinject
= 0 };
277 ret
= kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
280 "Can't disable in-kernel PIT reinjection: %s",
287 error_setg(errp
, "Lost tick policy not supported.");
291 memory_region_init_reservation(&pit
->ioports
, NULL
, "kvm-pit", 4);
293 qdev_init_gpio_in(dev
, kvm_pit_irq_control
, 1);
295 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change
, s
);
297 kpc
->parent_realize(dev
, errp
);
300 static Property kvm_pit_properties
[] = {
301 DEFINE_PROP_UINT32("iobase", PITCommonState
, iobase
, -1),
302 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState
,
303 lost_tick_policy
, LOST_TICK_POLICY_DELAY
),
304 DEFINE_PROP_END_OF_LIST(),
307 static void kvm_pit_class_init(ObjectClass
*klass
, void *data
)
309 KVMPITClass
*kpc
= KVM_PIT_CLASS(klass
);
310 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
311 DeviceClass
*dc
= DEVICE_CLASS(klass
);
313 kpc
->parent_realize
= dc
->realize
;
314 dc
->realize
= kvm_pit_realizefn
;
315 k
->set_channel_gate
= kvm_pit_set_gate
;
316 k
->get_channel_info
= kvm_pit_get_channel_info
;
317 k
->pre_save
= kvm_pit_get
;
318 k
->post_load
= kvm_pit_put
;
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
)