154976b75SPaolo Bonzini /*
254976b75SPaolo Bonzini * KVM in-kernel PIT (i8254) support
354976b75SPaolo Bonzini *
454976b75SPaolo Bonzini * Copyright (c) 2003-2004 Fabrice Bellard
554976b75SPaolo Bonzini * Copyright (c) 2012 Jan Kiszka, Siemens AG
654976b75SPaolo Bonzini *
754976b75SPaolo Bonzini * Permission is hereby granted, free of charge, to any person obtaining a copy
854976b75SPaolo Bonzini * of this software and associated documentation files (the "Software"), to deal
954976b75SPaolo Bonzini * in the Software without restriction, including without limitation the rights
1054976b75SPaolo Bonzini * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1154976b75SPaolo Bonzini * copies of the Software, and to permit persons to whom the Software is
1254976b75SPaolo Bonzini * furnished to do so, subject to the following conditions:
1354976b75SPaolo Bonzini *
1454976b75SPaolo Bonzini * The above copyright notice and this permission notice shall be included in
1554976b75SPaolo Bonzini * all copies or substantial portions of the Software.
1654976b75SPaolo Bonzini *
1754976b75SPaolo Bonzini * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1854976b75SPaolo Bonzini * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1954976b75SPaolo Bonzini * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2054976b75SPaolo Bonzini * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2154976b75SPaolo Bonzini * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2254976b75SPaolo Bonzini * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2354976b75SPaolo Bonzini * THE SOFTWARE.
2454976b75SPaolo Bonzini */
25a9c94277SMarkus Armbruster
26b6a0aa05SPeter Maydell #include "qemu/osdep.h"
27a9c94277SMarkus Armbruster #include <linux/kvm.h>
288dc007d3SPhilippe Mathieu-Daudé #include "qapi/qapi-types-machine.h"
29da34e65cSMarkus Armbruster #include "qapi/error.h"
300b8fa32fSMarkus Armbruster #include "qemu/module.h"
3154976b75SPaolo Bonzini #include "qemu/timer.h"
3254d31236SMarkus Armbruster #include "sysemu/runstate.h"
3354976b75SPaolo Bonzini #include "hw/timer/i8254.h"
3454976b75SPaolo Bonzini #include "hw/timer/i8254_internal.h"
35ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
3654976b75SPaolo Bonzini #include "sysemu/kvm.h"
37fc30abf8SPhilippe Mathieu-Daudé #include "target/i386/kvm/kvm_i386.h"
38db1015e9SEduardo Habkost #include "qom/object.h"
3954976b75SPaolo Bonzini
4054976b75SPaolo Bonzini #define KVM_PIT_REINJECT_BIT 0
4154976b75SPaolo Bonzini
4254976b75SPaolo Bonzini #define CALIBRATION_ROUNDS 3
4354976b75SPaolo Bonzini
44db1015e9SEduardo Habkost typedef struct KVMPITClass KVMPITClass;
45db1015e9SEduardo Habkost typedef struct KVMPITState KVMPITState;
468110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(KVMPITState, KVMPITClass,
478110fa1dSEduardo Habkost KVM_PIT, TYPE_KVM_I8254)
4858cd9864SAndreas Färber
49db1015e9SEduardo Habkost struct KVMPITState {
5058cd9864SAndreas Färber PITCommonState parent_obj;
5158cd9864SAndreas Färber
5254976b75SPaolo Bonzini LostTickPolicy lost_tick_policy;
5354976b75SPaolo Bonzini bool vm_stopped;
5454976b75SPaolo Bonzini int64_t kernel_clock_offset;
55db1015e9SEduardo Habkost };
5654976b75SPaolo Bonzini
57db1015e9SEduardo Habkost struct KVMPITClass {
58a15d0912SAndreas Färber PITCommonClass parent_class;
59a15d0912SAndreas Färber
60a15d0912SAndreas Färber DeviceRealize parent_realize;
61db1015e9SEduardo Habkost };
62a15d0912SAndreas Färber
kvm_pit_update_clock_offset(KVMPITState * s)6354976b75SPaolo Bonzini static void kvm_pit_update_clock_offset(KVMPITState *s)
6454976b75SPaolo Bonzini {
6554976b75SPaolo Bonzini int64_t offset, clock_offset;
6654976b75SPaolo Bonzini struct timespec ts;
6754976b75SPaolo Bonzini int i;
6854976b75SPaolo Bonzini
6954976b75SPaolo Bonzini /*
7054976b75SPaolo Bonzini * Measure the delta between CLOCK_MONOTONIC, the base used for
71bc72ad67SAlex Bligh * kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
7254976b75SPaolo Bonzini * minimum of several samples to filter out scheduling noise.
7354976b75SPaolo Bonzini */
7454976b75SPaolo Bonzini clock_offset = INT64_MAX;
7554976b75SPaolo Bonzini for (i = 0; i < CALIBRATION_ROUNDS; i++) {
76bc72ad67SAlex Bligh offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
7754976b75SPaolo Bonzini clock_gettime(CLOCK_MONOTONIC, &ts);
7854976b75SPaolo Bonzini offset -= ts.tv_nsec;
7954976b75SPaolo Bonzini offset -= (int64_t)ts.tv_sec * 1000000000;
80bb89646cSLuis Pires if (uabs64(offset) < uabs64(clock_offset)) {
8154976b75SPaolo Bonzini clock_offset = offset;
8254976b75SPaolo Bonzini }
8354976b75SPaolo Bonzini }
8454976b75SPaolo Bonzini s->kernel_clock_offset = clock_offset;
8554976b75SPaolo Bonzini }
8654976b75SPaolo Bonzini
kvm_pit_get(PITCommonState * pit)8754976b75SPaolo Bonzini static void kvm_pit_get(PITCommonState *pit)
8854976b75SPaolo Bonzini {
8958cd9864SAndreas Färber KVMPITState *s = KVM_PIT(pit);
9054976b75SPaolo Bonzini struct kvm_pit_state2 kpit;
9154976b75SPaolo Bonzini struct kvm_pit_channel_state *kchan;
9254976b75SPaolo Bonzini struct PITChannelState *sc;
9354976b75SPaolo Bonzini int i, ret;
9454976b75SPaolo Bonzini
9554976b75SPaolo Bonzini /* No need to re-read the state if VM is stopped. */
9654976b75SPaolo Bonzini if (s->vm_stopped) {
9754976b75SPaolo Bonzini return;
9854976b75SPaolo Bonzini }
9954976b75SPaolo Bonzini
10054976b75SPaolo Bonzini ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
10154976b75SPaolo Bonzini if (ret < 0) {
102d84451d3SDmitry Voronetskiy fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(-ret));
10354976b75SPaolo Bonzini abort();
10454976b75SPaolo Bonzini }
10554976b75SPaolo Bonzini pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
10654976b75SPaolo Bonzini for (i = 0; i < 3; i++) {
10754976b75SPaolo Bonzini kchan = &kpit.channels[i];
10854976b75SPaolo Bonzini sc = &pit->channels[i];
10954976b75SPaolo Bonzini sc->count = kchan->count;
11054976b75SPaolo Bonzini sc->latched_count = kchan->latched_count;
11154976b75SPaolo Bonzini sc->count_latched = kchan->count_latched;
11254976b75SPaolo Bonzini sc->status_latched = kchan->status_latched;
11354976b75SPaolo Bonzini sc->status = kchan->status;
11454976b75SPaolo Bonzini sc->read_state = kchan->read_state;
11554976b75SPaolo Bonzini sc->write_state = kchan->write_state;
11654976b75SPaolo Bonzini sc->write_latch = kchan->write_latch;
11754976b75SPaolo Bonzini sc->rw_mode = kchan->rw_mode;
11854976b75SPaolo Bonzini sc->mode = kchan->mode;
11954976b75SPaolo Bonzini sc->bcd = kchan->bcd;
12054976b75SPaolo Bonzini sc->gate = kchan->gate;
12154976b75SPaolo Bonzini sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
12254976b75SPaolo Bonzini }
12354976b75SPaolo Bonzini
12454976b75SPaolo Bonzini sc = &pit->channels[0];
12554976b75SPaolo Bonzini sc->next_transition_time =
12654976b75SPaolo Bonzini pit_get_next_transition_time(sc, sc->count_load_time);
12754976b75SPaolo Bonzini }
12854976b75SPaolo Bonzini
kvm_pit_put(PITCommonState * pit)12954976b75SPaolo Bonzini static void kvm_pit_put(PITCommonState *pit)
13054976b75SPaolo Bonzini {
13158cd9864SAndreas Färber KVMPITState *s = KVM_PIT(pit);
132b0a05512SChristian Borntraeger struct kvm_pit_state2 kpit = {};
13354976b75SPaolo Bonzini struct kvm_pit_channel_state *kchan;
13454976b75SPaolo Bonzini struct PITChannelState *sc;
13554976b75SPaolo Bonzini int i, ret;
13654976b75SPaolo Bonzini
13754976b75SPaolo Bonzini /* The offset keeps changing as long as the VM is stopped. */
13854976b75SPaolo Bonzini if (s->vm_stopped) {
13954976b75SPaolo Bonzini kvm_pit_update_clock_offset(s);
14054976b75SPaolo Bonzini }
14154976b75SPaolo Bonzini
14254976b75SPaolo Bonzini kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
14354976b75SPaolo Bonzini for (i = 0; i < 3; i++) {
14454976b75SPaolo Bonzini kchan = &kpit.channels[i];
14554976b75SPaolo Bonzini sc = &pit->channels[i];
14654976b75SPaolo Bonzini kchan->count = sc->count;
14754976b75SPaolo Bonzini kchan->latched_count = sc->latched_count;
14854976b75SPaolo Bonzini kchan->count_latched = sc->count_latched;
14954976b75SPaolo Bonzini kchan->status_latched = sc->status_latched;
15054976b75SPaolo Bonzini kchan->status = sc->status;
15154976b75SPaolo Bonzini kchan->read_state = sc->read_state;
15254976b75SPaolo Bonzini kchan->write_state = sc->write_state;
15354976b75SPaolo Bonzini kchan->write_latch = sc->write_latch;
15454976b75SPaolo Bonzini kchan->rw_mode = sc->rw_mode;
15554976b75SPaolo Bonzini kchan->mode = sc->mode;
15654976b75SPaolo Bonzini kchan->bcd = sc->bcd;
15754976b75SPaolo Bonzini kchan->gate = sc->gate;
15854976b75SPaolo Bonzini kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
15954976b75SPaolo Bonzini }
16054976b75SPaolo Bonzini
16139dd3e1fSPaolo Bonzini ret = kvm_vm_ioctl(kvm_state, KVM_SET_PIT2, &kpit);
16254976b75SPaolo Bonzini if (ret < 0) {
16339dd3e1fSPaolo Bonzini fprintf(stderr, "KVM_SET_PIT2 failed: %s\n",
164d84451d3SDmitry Voronetskiy strerror(-ret));
16554976b75SPaolo Bonzini abort();
16654976b75SPaolo Bonzini }
16754976b75SPaolo Bonzini }
16854976b75SPaolo Bonzini
kvm_pit_set_gate(PITCommonState * s,PITChannelState * sc,int val)16954976b75SPaolo Bonzini static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
17054976b75SPaolo Bonzini {
17154976b75SPaolo Bonzini kvm_pit_get(s);
17254976b75SPaolo Bonzini
17354976b75SPaolo Bonzini switch (sc->mode) {
17454976b75SPaolo Bonzini default:
17554976b75SPaolo Bonzini case 0:
17654976b75SPaolo Bonzini case 4:
17754976b75SPaolo Bonzini /* XXX: just disable/enable counting */
17854976b75SPaolo Bonzini break;
17954976b75SPaolo Bonzini case 1:
18054976b75SPaolo Bonzini case 2:
18154976b75SPaolo Bonzini case 3:
18254976b75SPaolo Bonzini case 5:
18354976b75SPaolo Bonzini if (sc->gate < val) {
18454976b75SPaolo Bonzini /* restart counting on rising edge */
185bc72ad67SAlex Bligh sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
18654976b75SPaolo Bonzini }
18754976b75SPaolo Bonzini break;
18854976b75SPaolo Bonzini }
18954976b75SPaolo Bonzini sc->gate = val;
19054976b75SPaolo Bonzini
19154976b75SPaolo Bonzini kvm_pit_put(s);
19254976b75SPaolo Bonzini }
19354976b75SPaolo Bonzini
kvm_pit_get_channel_info(PITCommonState * s,PITChannelState * sc,PITChannelInfo * info)19454976b75SPaolo Bonzini static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
19554976b75SPaolo Bonzini PITChannelInfo *info)
19654976b75SPaolo Bonzini {
19754976b75SPaolo Bonzini kvm_pit_get(s);
19854976b75SPaolo Bonzini
19954976b75SPaolo Bonzini pit_get_channel_info_common(s, sc, info);
20054976b75SPaolo Bonzini }
20154976b75SPaolo Bonzini
kvm_pit_reset(DeviceState * dev)20254976b75SPaolo Bonzini static void kvm_pit_reset(DeviceState *dev)
20354976b75SPaolo Bonzini {
20458cd9864SAndreas Färber PITCommonState *s = PIT_COMMON(dev);
20554976b75SPaolo Bonzini
20654976b75SPaolo Bonzini pit_reset_common(s);
20754976b75SPaolo Bonzini
20854976b75SPaolo Bonzini kvm_pit_put(s);
20954976b75SPaolo Bonzini }
21054976b75SPaolo Bonzini
kvm_pit_irq_control(void * opaque,int n,int enable)21154976b75SPaolo Bonzini static void kvm_pit_irq_control(void *opaque, int n, int enable)
21254976b75SPaolo Bonzini {
21354976b75SPaolo Bonzini PITCommonState *pit = opaque;
21454976b75SPaolo Bonzini PITChannelState *s = &pit->channels[0];
21554976b75SPaolo Bonzini
21654976b75SPaolo Bonzini kvm_pit_get(pit);
21754976b75SPaolo Bonzini
21854976b75SPaolo Bonzini s->irq_disabled = !enable;
21954976b75SPaolo Bonzini
22054976b75SPaolo Bonzini kvm_pit_put(pit);
22154976b75SPaolo Bonzini }
22254976b75SPaolo Bonzini
kvm_pit_vm_state_change(void * opaque,bool running,RunState state)223538f0497SPhilippe Mathieu-Daudé static void kvm_pit_vm_state_change(void *opaque, bool running,
22454976b75SPaolo Bonzini RunState state)
22554976b75SPaolo Bonzini {
22654976b75SPaolo Bonzini KVMPITState *s = opaque;
22754976b75SPaolo Bonzini
22854976b75SPaolo Bonzini if (running) {
22954976b75SPaolo Bonzini kvm_pit_update_clock_offset(s);
230be894f51SChenLiang kvm_pit_put(PIT_COMMON(s));
23154976b75SPaolo Bonzini s->vm_stopped = false;
23254976b75SPaolo Bonzini } else {
23354976b75SPaolo Bonzini kvm_pit_update_clock_offset(s);
23458cd9864SAndreas Färber kvm_pit_get(PIT_COMMON(s));
23554976b75SPaolo Bonzini s->vm_stopped = true;
23654976b75SPaolo Bonzini }
23754976b75SPaolo Bonzini }
23854976b75SPaolo Bonzini
kvm_pit_realizefn(DeviceState * dev,Error ** errp)239a15d0912SAndreas Färber static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
24054976b75SPaolo Bonzini {
241a15d0912SAndreas Färber PITCommonState *pit = PIT_COMMON(dev);
242a15d0912SAndreas Färber KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
24358cd9864SAndreas Färber KVMPITState *s = KVM_PIT(pit);
24454976b75SPaolo Bonzini struct kvm_pit_config config = {
24554976b75SPaolo Bonzini .flags = 0,
24654976b75SPaolo Bonzini };
24754976b75SPaolo Bonzini int ret;
24854976b75SPaolo Bonzini
24939dd3e1fSPaolo Bonzini if (!kvm_check_extension(kvm_state, KVM_CAP_PIT_STATE2) ||
25039dd3e1fSPaolo Bonzini !kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
25139dd3e1fSPaolo Bonzini error_setg(errp, "In-kernel PIT not available");
25254976b75SPaolo Bonzini }
25339dd3e1fSPaolo Bonzini
25439dd3e1fSPaolo Bonzini ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
25554976b75SPaolo Bonzini if (ret < 0) {
256a15d0912SAndreas Färber error_setg(errp, "Create kernel PIC irqchip failed: %s",
257d84451d3SDmitry Voronetskiy strerror(-ret));
258a15d0912SAndreas Färber return;
25954976b75SPaolo Bonzini }
26054976b75SPaolo Bonzini switch (s->lost_tick_policy) {
261104059daSPaolo Bonzini case LOST_TICK_POLICY_DELAY:
26254976b75SPaolo Bonzini break; /* enabled by default */
263104059daSPaolo Bonzini case LOST_TICK_POLICY_DISCARD:
26454976b75SPaolo Bonzini if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
26554976b75SPaolo Bonzini struct kvm_reinject_control control = { .pit_reinject = 0 };
26654976b75SPaolo Bonzini
26754976b75SPaolo Bonzini ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
26854976b75SPaolo Bonzini if (ret < 0) {
269a15d0912SAndreas Färber error_setg(errp,
270a15d0912SAndreas Färber "Can't disable in-kernel PIT reinjection: %s",
271d84451d3SDmitry Voronetskiy strerror(-ret));
272a15d0912SAndreas Färber return;
27354976b75SPaolo Bonzini }
27454976b75SPaolo Bonzini }
27554976b75SPaolo Bonzini break;
27654976b75SPaolo Bonzini default:
277a15d0912SAndreas Färber error_setg(errp, "Lost tick policy not supported.");
278a15d0912SAndreas Färber return;
27954976b75SPaolo Bonzini }
28054976b75SPaolo Bonzini
281257a7430SPaolo Bonzini memory_region_init_io(&pit->ioports, OBJECT(dev), NULL, NULL, "kvm-pit", 4);
28254976b75SPaolo Bonzini
283a15d0912SAndreas Färber qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
28454976b75SPaolo Bonzini
28554976b75SPaolo Bonzini qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
28654976b75SPaolo Bonzini
287a15d0912SAndreas Färber kpc->parent_realize(dev, errp);
28854976b75SPaolo Bonzini }
28954976b75SPaolo Bonzini
29054976b75SPaolo Bonzini static Property kvm_pit_properties[] = {
29154976b75SPaolo Bonzini DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
292104059daSPaolo Bonzini lost_tick_policy, LOST_TICK_POLICY_DELAY),
29354976b75SPaolo Bonzini DEFINE_PROP_END_OF_LIST(),
29454976b75SPaolo Bonzini };
29554976b75SPaolo Bonzini
kvm_pit_class_init(ObjectClass * klass,void * data)29654976b75SPaolo Bonzini static void kvm_pit_class_init(ObjectClass *klass, void *data)
29754976b75SPaolo Bonzini {
298a15d0912SAndreas Färber KVMPITClass *kpc = KVM_PIT_CLASS(klass);
29954976b75SPaolo Bonzini PITCommonClass *k = PIT_COMMON_CLASS(klass);
30054976b75SPaolo Bonzini DeviceClass *dc = DEVICE_CLASS(klass);
30154976b75SPaolo Bonzini
302bf853881SPhilippe Mathieu-Daudé device_class_set_parent_realize(dc, kvm_pit_realizefn,
303bf853881SPhilippe Mathieu-Daudé &kpc->parent_realize);
30454976b75SPaolo Bonzini k->set_channel_gate = kvm_pit_set_gate;
30554976b75SPaolo Bonzini k->get_channel_info = kvm_pit_get_channel_info;
306*e3d08143SPeter Maydell device_class_set_legacy_reset(dc, kvm_pit_reset);
3074f67d30bSMarc-André Lureau device_class_set_props(dc, kvm_pit_properties);
30854976b75SPaolo Bonzini }
30954976b75SPaolo Bonzini
31054976b75SPaolo Bonzini static const TypeInfo kvm_pit_info = {
31158cd9864SAndreas Färber .name = TYPE_KVM_I8254,
31254976b75SPaolo Bonzini .parent = TYPE_PIT_COMMON,
31354976b75SPaolo Bonzini .instance_size = sizeof(KVMPITState),
31454976b75SPaolo Bonzini .class_init = kvm_pit_class_init,
315a15d0912SAndreas Färber .class_size = sizeof(KVMPITClass),
31654976b75SPaolo Bonzini };
31754976b75SPaolo Bonzini
kvm_pit_register(void)31854976b75SPaolo Bonzini static void kvm_pit_register(void)
31954976b75SPaolo Bonzini {
32054976b75SPaolo Bonzini type_register_static(&kvm_pit_info);
32154976b75SPaolo Bonzini }
32254976b75SPaolo Bonzini
32354976b75SPaolo Bonzini type_init(kvm_pit_register)
324