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"
27 #include "hw/i8254_internal.h"
30 #define KVM_PIT_REINJECT_BIT 0
32 typedef struct KVMPITState
{
34 LostTickPolicy lost_tick_policy
;
37 static void kvm_pit_get(PITCommonState
*s
)
39 struct kvm_pit_state2 kpit
;
40 struct kvm_pit_channel_state
*kchan
;
41 struct PITChannelState
*sc
;
44 if (kvm_has_pit_state2()) {
45 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT2
, &kpit
);
47 fprintf(stderr
, "KVM_GET_PIT2 failed: %s\n", strerror(ret
));
50 s
->channels
[0].irq_disabled
= kpit
.flags
& KVM_PIT_FLAGS_HPET_LEGACY
;
53 * kvm_pit_state2 is superset of kvm_pit_state struct,
54 * so we can use it for KVM_GET_PIT as well.
56 ret
= kvm_vm_ioctl(kvm_state
, KVM_GET_PIT
, &kpit
);
58 fprintf(stderr
, "KVM_GET_PIT failed: %s\n", strerror(ret
));
62 for (i
= 0; i
< 3; i
++) {
63 kchan
= &kpit
.channels
[i
];
65 sc
->count
= kchan
->count
;
66 sc
->latched_count
= kchan
->latched_count
;
67 sc
->count_latched
= kchan
->count_latched
;
68 sc
->status_latched
= kchan
->status_latched
;
69 sc
->status
= kchan
->status
;
70 sc
->read_state
= kchan
->read_state
;
71 sc
->write_state
= kchan
->write_state
;
72 sc
->write_latch
= kchan
->write_latch
;
73 sc
->rw_mode
= kchan
->rw_mode
;
74 sc
->mode
= kchan
->mode
;
76 sc
->gate
= kchan
->gate
;
77 sc
->count_load_time
= kchan
->count_load_time
;
81 sc
->next_transition_time
=
82 pit_get_next_transition_time(sc
, sc
->count_load_time
);
85 static void kvm_pit_put(PITCommonState
*s
)
87 struct kvm_pit_state2 kpit
;
88 struct kvm_pit_channel_state
*kchan
;
89 struct PITChannelState
*sc
;
92 kpit
.flags
= s
->channels
[0].irq_disabled
? KVM_PIT_FLAGS_HPET_LEGACY
: 0;
93 for (i
= 0; i
< 3; i
++) {
94 kchan
= &kpit
.channels
[i
];
96 kchan
->count
= sc
->count
;
97 kchan
->latched_count
= sc
->latched_count
;
98 kchan
->count_latched
= sc
->count_latched
;
99 kchan
->status_latched
= sc
->status_latched
;
100 kchan
->status
= sc
->status
;
101 kchan
->read_state
= sc
->read_state
;
102 kchan
->write_state
= sc
->write_state
;
103 kchan
->write_latch
= sc
->write_latch
;
104 kchan
->rw_mode
= sc
->rw_mode
;
105 kchan
->mode
= sc
->mode
;
106 kchan
->bcd
= sc
->bcd
;
107 kchan
->gate
= sc
->gate
;
108 kchan
->count_load_time
= sc
->count_load_time
;
111 ret
= kvm_vm_ioctl(kvm_state
,
112 kvm_has_pit_state2() ? KVM_SET_PIT2
: KVM_SET_PIT
,
115 fprintf(stderr
, "%s failed: %s\n",
116 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
122 static void kvm_pit_set_gate(PITCommonState
*s
, PITChannelState
*sc
, int val
)
130 /* XXX: just disable/enable counting */
136 if (sc
->gate
< val
) {
137 /* restart counting on rising edge */
138 sc
->count_load_time
= qemu_get_clock_ns(vm_clock
);
147 static void kvm_pit_get_channel_info(PITCommonState
*s
, PITChannelState
*sc
,
148 PITChannelInfo
*info
)
152 pit_get_channel_info_common(s
, sc
, info
);
155 static void kvm_pit_reset(DeviceState
*dev
)
157 PITCommonState
*s
= DO_UPCAST(PITCommonState
, dev
.qdev
, dev
);
164 static void kvm_pit_irq_control(void *opaque
, int n
, int enable
)
166 PITCommonState
*pit
= opaque
;
167 PITChannelState
*s
= &pit
->channels
[0];
171 s
->irq_disabled
= !enable
;
176 static int kvm_pit_initfn(PITCommonState
*pit
)
178 KVMPITState
*s
= DO_UPCAST(KVMPITState
, pit
, pit
);
179 struct kvm_pit_config config
= {
184 if (kvm_check_extension(kvm_state
, KVM_CAP_PIT2
)) {
185 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT2
, &config
);
187 ret
= kvm_vm_ioctl(kvm_state
, KVM_CREATE_PIT
);
190 fprintf(stderr
, "Create kernel PIC irqchip failed: %s\n",
194 switch (s
->lost_tick_policy
) {
195 case LOST_TICK_DELAY
:
196 break; /* enabled by default */
197 case LOST_TICK_DISCARD
:
198 if (kvm_check_extension(kvm_state
, KVM_CAP_REINJECT_CONTROL
)) {
199 struct kvm_reinject_control control
= { .pit_reinject
= 0 };
201 ret
= kvm_vm_ioctl(kvm_state
, KVM_REINJECT_CONTROL
, &control
);
204 "Can't disable in-kernel PIT reinjection: %s\n",
214 memory_region_init_reservation(&pit
->ioports
, "kvm-pit", 4);
216 qdev_init_gpio_in(&pit
->dev
.qdev
, kvm_pit_irq_control
, 1);
221 static Property kvm_pit_properties
[] = {
222 DEFINE_PROP_HEX32("iobase", KVMPITState
, pit
.iobase
, -1),
223 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState
,
224 lost_tick_policy
, LOST_TICK_DELAY
),
225 DEFINE_PROP_END_OF_LIST(),
228 static void kvm_pit_class_init(ObjectClass
*klass
, void *data
)
230 PITCommonClass
*k
= PIT_COMMON_CLASS(klass
);
231 DeviceClass
*dc
= DEVICE_CLASS(klass
);
233 k
->init
= kvm_pit_initfn
;
234 k
->set_channel_gate
= kvm_pit_set_gate
;
235 k
->get_channel_info
= kvm_pit_get_channel_info
;
236 k
->pre_save
= kvm_pit_get
;
237 k
->post_load
= kvm_pit_put
;
238 dc
->reset
= kvm_pit_reset
;
239 dc
->props
= kvm_pit_properties
;
242 static TypeInfo kvm_pit_info
= {
244 .parent
= TYPE_PIT_COMMON
,
245 .instance_size
= sizeof(KVMPITState
),
246 .class_init
= kvm_pit_class_init
,
249 static void kvm_pit_register(void)
251 type_register_static(&kvm_pit_info
);
254 type_init(kvm_pit_register
)