]> Git Repo - qemu.git/blame - hw/i386/kvm/i8254.c
pci-assign: do not include sys/io.h
[qemu.git] / hw / i386 / kvm / i8254.c
CommitLineData
5d17c0d2
JK
1/*
2 * KVM in-kernel PIT (i8254) support
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2012 Jan Kiszka, Siemens AG
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
1de7afc9 25#include "qemu/timer.h"
9c17d615 26#include "sysemu/sysemu.h"
0d09e41a
PB
27#include "hw/timer/i8254.h"
28#include "hw/timer/i8254_internal.h"
9c17d615 29#include "sysemu/kvm.h"
5d17c0d2
JK
30
31#define KVM_PIT_REINJECT_BIT 0
32
0cdd3d14
JK
33#define CALIBRATION_ROUNDS 3
34
58cd9864 35#define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
a15d0912
AF
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)
58cd9864 40
5d17c0d2 41typedef struct KVMPITState {
58cd9864
AF
42 PITCommonState parent_obj;
43
5d17c0d2 44 LostTickPolicy lost_tick_policy;
205df4d1
JK
45 bool vm_stopped;
46 int64_t kernel_clock_offset;
5d17c0d2
JK
47} KVMPITState;
48
a15d0912
AF
49typedef struct KVMPITClass {
50 PITCommonClass parent_class;
51
52 DeviceRealize parent_realize;
53} KVMPITClass;
54
0cdd3d14 55static int64_t abs64(int64_t v)
5d17c0d2 56{
0cdd3d14
JK
57 return v < 0 ? -v : v;
58}
59
205df4d1 60static void kvm_pit_update_clock_offset(KVMPITState *s)
0cdd3d14 61{
0cdd3d14
JK
62 int64_t offset, clock_offset;
63 struct timespec ts;
205df4d1 64 int i;
0cdd3d14
JK
65
66 /*
67 * Measure the delta between CLOCK_MONOTONIC, the base used for
bc72ad67 68 * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
0cdd3d14
JK
69 * minimum of several samples to filter out scheduling noise.
70 */
71 clock_offset = INT64_MAX;
72 for (i = 0; i < CALIBRATION_ROUNDS; i++) {
bc72ad67 73 offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
0cdd3d14
JK
74 clock_gettime(CLOCK_MONOTONIC, &ts);
75 offset -= ts.tv_nsec;
76 offset -= (int64_t)ts.tv_sec * 1000000000;
77 if (abs64(offset) < abs64(clock_offset)) {
78 clock_offset = offset;
79 }
80 }
205df4d1
JK
81 s->kernel_clock_offset = clock_offset;
82}
83
84static void kvm_pit_get(PITCommonState *pit)
85{
58cd9864 86 KVMPITState *s = KVM_PIT(pit);
205df4d1
JK
87 struct kvm_pit_state2 kpit;
88 struct kvm_pit_channel_state *kchan;
89 struct PITChannelState *sc;
90 int i, ret;
91
92 /* No need to re-read the state if VM is stopped. */
93 if (s->vm_stopped) {
94 return;
95 }
0cdd3d14 96
5d17c0d2
JK
97 if (kvm_has_pit_state2()) {
98 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
99 if (ret < 0) {
100 fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
101 abort();
102 }
0cdd3d14 103 pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
5d17c0d2
JK
104 } else {
105 /*
106 * kvm_pit_state2 is superset of kvm_pit_state struct,
107 * so we can use it for KVM_GET_PIT as well.
108 */
109 ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
110 if (ret < 0) {
111 fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
112 abort();
113 }
114 }
115 for (i = 0; i < 3; i++) {
116 kchan = &kpit.channels[i];
0cdd3d14 117 sc = &pit->channels[i];
5d17c0d2
JK
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;
205df4d1 130 sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
5d17c0d2
JK
131 }
132
0cdd3d14 133 sc = &pit->channels[0];
5d17c0d2
JK
134 sc->next_transition_time =
135 pit_get_next_transition_time(sc, sc->count_load_time);
136}
137
050a4606 138static void kvm_pit_put(PITCommonState *pit)
5d17c0d2 139{
58cd9864 140 KVMPITState *s = KVM_PIT(pit);
b0a05512 141 struct kvm_pit_state2 kpit = {};
5d17c0d2
JK
142 struct kvm_pit_channel_state *kchan;
143 struct PITChannelState *sc;
144 int i, ret;
145
050a4606
JK
146 /* The offset keeps changing as long as the VM is stopped. */
147 if (s->vm_stopped) {
148 kvm_pit_update_clock_offset(s);
149 }
150
151 kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
5d17c0d2
JK
152 for (i = 0; i < 3; i++) {
153 kchan = &kpit.channels[i];
050a4606 154 sc = &pit->channels[i];
5d17c0d2
JK
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;
050a4606 167 kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
5d17c0d2
JK
168 }
169
170 ret = kvm_vm_ioctl(kvm_state,
171 kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
172 &kpit);
173 if (ret < 0) {
174 fprintf(stderr, "%s failed: %s\n",
175 kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
176 strerror(ret));
177 abort();
178 }
179}
180
181static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
182{
183 kvm_pit_get(s);
184
185 switch (sc->mode) {
186 default:
187 case 0:
188 case 4:
189 /* XXX: just disable/enable counting */
190 break;
191 case 1:
192 case 2:
193 case 3:
194 case 5:
195 if (sc->gate < val) {
196 /* restart counting on rising edge */
bc72ad67 197 sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
5d17c0d2
JK
198 }
199 break;
200 }
201 sc->gate = val;
202
203 kvm_pit_put(s);
204}
205
206static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
207 PITChannelInfo *info)
208{
209 kvm_pit_get(s);
210
211 pit_get_channel_info_common(s, sc, info);
212}
213
214static void kvm_pit_reset(DeviceState *dev)
215{
58cd9864 216 PITCommonState *s = PIT_COMMON(dev);
5d17c0d2
JK
217
218 pit_reset_common(s);
219
220 kvm_pit_put(s);
221}
222
223static void kvm_pit_irq_control(void *opaque, int n, int enable)
224{
225 PITCommonState *pit = opaque;
226 PITChannelState *s = &pit->channels[0];
227
228 kvm_pit_get(pit);
229
230 s->irq_disabled = !enable;
231
232 kvm_pit_put(pit);
233}
234
0cdd3d14
JK
235static void kvm_pit_vm_state_change(void *opaque, int running,
236 RunState state)
237{
238 KVMPITState *s = opaque;
239
240 if (running) {
205df4d1 241 kvm_pit_update_clock_offset(s);
be894f51 242 kvm_pit_put(PIT_COMMON(s));
205df4d1 243 s->vm_stopped = false;
0cdd3d14 244 } else {
205df4d1 245 kvm_pit_update_clock_offset(s);
58cd9864 246 kvm_pit_get(PIT_COMMON(s));
205df4d1 247 s->vm_stopped = true;
0cdd3d14
JK
248 }
249}
250
a15d0912 251static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
5d17c0d2 252{
a15d0912
AF
253 PITCommonState *pit = PIT_COMMON(dev);
254 KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
58cd9864 255 KVMPITState *s = KVM_PIT(pit);
5d17c0d2
JK
256 struct kvm_pit_config config = {
257 .flags = 0,
258 };
259 int ret;
260
261 if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
262 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
263 } else {
264 ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
265 }
266 if (ret < 0) {
a15d0912
AF
267 error_setg(errp, "Create kernel PIC irqchip failed: %s",
268 strerror(ret));
269 return;
5d17c0d2
JK
270 }
271 switch (s->lost_tick_policy) {
104059da 272 case LOST_TICK_POLICY_DELAY:
5d17c0d2 273 break; /* enabled by default */
104059da 274 case LOST_TICK_POLICY_DISCARD:
5d17c0d2
JK
275 if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
276 struct kvm_reinject_control control = { .pit_reinject = 0 };
277
278 ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
279 if (ret < 0) {
a15d0912
AF
280 error_setg(errp,
281 "Can't disable in-kernel PIT reinjection: %s",
282 strerror(ret));
283 return;
5d17c0d2
JK
284 }
285 }
286 break;
287 default:
a15d0912
AF
288 error_setg(errp, "Lost tick policy not supported.");
289 return;
5d17c0d2
JK
290 }
291
2c9b15ca 292 memory_region_init_reservation(&pit->ioports, NULL, "kvm-pit", 4);
5d17c0d2 293
a15d0912 294 qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
5d17c0d2 295
0cdd3d14
JK
296 qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
297
a15d0912 298 kpc->parent_realize(dev, errp);
5d17c0d2
JK
299}
300
301static Property kvm_pit_properties[] = {
c7bcc85d 302 DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
5d17c0d2 303 DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
104059da 304 lost_tick_policy, LOST_TICK_POLICY_DELAY),
5d17c0d2
JK
305 DEFINE_PROP_END_OF_LIST(),
306};
307
308static void kvm_pit_class_init(ObjectClass *klass, void *data)
309{
a15d0912 310 KVMPITClass *kpc = KVM_PIT_CLASS(klass);
5d17c0d2
JK
311 PITCommonClass *k = PIT_COMMON_CLASS(klass);
312 DeviceClass *dc = DEVICE_CLASS(klass);
313
a15d0912
AF
314 kpc->parent_realize = dc->realize;
315 dc->realize = kvm_pit_realizefn;
5d17c0d2
JK
316 k->set_channel_gate = kvm_pit_set_gate;
317 k->get_channel_info = kvm_pit_get_channel_info;
5d17c0d2
JK
318 dc->reset = kvm_pit_reset;
319 dc->props = kvm_pit_properties;
320}
321
8c43a6f0 322static const TypeInfo kvm_pit_info = {
58cd9864 323 .name = TYPE_KVM_I8254,
5d17c0d2
JK
324 .parent = TYPE_PIT_COMMON,
325 .instance_size = sizeof(KVMPITState),
326 .class_init = kvm_pit_class_init,
a15d0912 327 .class_size = sizeof(KVMPITClass),
5d17c0d2
JK
328};
329
330static void kvm_pit_register(void)
331{
332 type_register_static(&kvm_pit_info);
333}
334
335type_init(kvm_pit_register)
This page took 0.33037 seconds and 4 git commands to generate.