2012-03-03 03:28:49 +08:00
|
|
|
/*
|
|
|
|
* KVM in-kernel PIT (i8254) support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2004 Fabrice Bellard
|
|
|
|
* Copyright (c) 2012 Jan Kiszka, Siemens AG
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2012-12-18 01:20:00 +08:00
|
|
|
#include "qemu/timer.h"
|
2012-12-18 01:20:04 +08:00
|
|
|
#include "sysemu/sysemu.h"
|
2013-02-06 00:06:20 +08:00
|
|
|
#include "hw/timer/i8254.h"
|
|
|
|
#include "hw/timer/i8254_internal.h"
|
2012-12-18 01:20:04 +08:00
|
|
|
#include "sysemu/kvm.h"
|
2012-03-03 03:28:49 +08:00
|
|
|
|
|
|
|
#define KVM_PIT_REINJECT_BIT 0
|
|
|
|
|
2012-06-06 22:28:42 +08:00
|
|
|
#define CALIBRATION_ROUNDS 3
|
|
|
|
|
2012-11-26 01:13:38 +08:00
|
|
|
#define KVM_PIT(obj) OBJECT_CHECK(KVMPITState, (obj), TYPE_KVM_I8254)
|
2012-11-26 01:47:58 +08:00
|
|
|
#define KVM_PIT_CLASS(class) \
|
|
|
|
OBJECT_CLASS_CHECK(KVMPITClass, (class), TYPE_KVM_I8254)
|
|
|
|
#define KVM_PIT_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(KVMPITClass, (obj), TYPE_KVM_I8254)
|
2012-11-26 01:13:38 +08:00
|
|
|
|
2012-03-03 03:28:49 +08:00
|
|
|
typedef struct KVMPITState {
|
2012-11-26 01:13:38 +08:00
|
|
|
PITCommonState parent_obj;
|
|
|
|
|
2012-03-03 03:28:49 +08:00
|
|
|
LostTickPolicy lost_tick_policy;
|
2012-08-14 16:24:03 +08:00
|
|
|
bool vm_stopped;
|
|
|
|
int64_t kernel_clock_offset;
|
2012-03-03 03:28:49 +08:00
|
|
|
} KVMPITState;
|
|
|
|
|
2012-11-26 01:47:58 +08:00
|
|
|
typedef struct KVMPITClass {
|
|
|
|
PITCommonClass parent_class;
|
|
|
|
|
|
|
|
DeviceRealize parent_realize;
|
|
|
|
} KVMPITClass;
|
|
|
|
|
2012-06-06 22:28:42 +08:00
|
|
|
static int64_t abs64(int64_t v)
|
2012-03-03 03:28:49 +08:00
|
|
|
{
|
2012-06-06 22:28:42 +08:00
|
|
|
return v < 0 ? -v : v;
|
|
|
|
}
|
|
|
|
|
2012-08-14 16:24:03 +08:00
|
|
|
static void kvm_pit_update_clock_offset(KVMPITState *s)
|
2012-06-06 22:28:42 +08:00
|
|
|
{
|
|
|
|
int64_t offset, clock_offset;
|
|
|
|
struct timespec ts;
|
2012-08-14 16:24:03 +08:00
|
|
|
int i;
|
2012-06-06 22:28:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Measure the delta between CLOCK_MONOTONIC, the base used for
|
|
|
|
* kvm_pit_channel_state::count_load_time, and vm_clock. Take the
|
|
|
|
* minimum of several samples to filter out scheduling noise.
|
|
|
|
*/
|
|
|
|
clock_offset = INT64_MAX;
|
|
|
|
for (i = 0; i < CALIBRATION_ROUNDS; i++) {
|
|
|
|
offset = qemu_get_clock_ns(vm_clock);
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
offset -= ts.tv_nsec;
|
|
|
|
offset -= (int64_t)ts.tv_sec * 1000000000;
|
|
|
|
if (abs64(offset) < abs64(clock_offset)) {
|
|
|
|
clock_offset = offset;
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 16:24:03 +08:00
|
|
|
s->kernel_clock_offset = clock_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_get(PITCommonState *pit)
|
|
|
|
{
|
2012-11-26 01:13:38 +08:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2012-08-14 16:24:03 +08:00
|
|
|
struct kvm_pit_state2 kpit;
|
|
|
|
struct kvm_pit_channel_state *kchan;
|
|
|
|
struct PITChannelState *sc;
|
|
|
|
int i, ret;
|
|
|
|
|
|
|
|
/* No need to re-read the state if VM is stopped. */
|
|
|
|
if (s->vm_stopped) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-06 22:28:42 +08:00
|
|
|
|
2012-03-03 03:28:49 +08:00
|
|
|
if (kvm_has_pit_state2()) {
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
2012-06-06 22:28:42 +08:00
|
|
|
pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
|
2012-03-03 03:28:49 +08:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* kvm_pit_state2 is superset of kvm_pit_state struct,
|
|
|
|
* so we can use it for KVM_GET_PIT as well.
|
|
|
|
*/
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT, &kpit);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
kchan = &kpit.channels[i];
|
2012-06-06 22:28:42 +08:00
|
|
|
sc = &pit->channels[i];
|
2012-03-03 03:28:49 +08:00
|
|
|
sc->count = kchan->count;
|
|
|
|
sc->latched_count = kchan->latched_count;
|
|
|
|
sc->count_latched = kchan->count_latched;
|
|
|
|
sc->status_latched = kchan->status_latched;
|
|
|
|
sc->status = kchan->status;
|
|
|
|
sc->read_state = kchan->read_state;
|
|
|
|
sc->write_state = kchan->write_state;
|
|
|
|
sc->write_latch = kchan->write_latch;
|
|
|
|
sc->rw_mode = kchan->rw_mode;
|
|
|
|
sc->mode = kchan->mode;
|
|
|
|
sc->bcd = kchan->bcd;
|
|
|
|
sc->gate = kchan->gate;
|
2012-08-14 16:24:03 +08:00
|
|
|
sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 22:28:42 +08:00
|
|
|
sc = &pit->channels[0];
|
2012-03-03 03:28:49 +08:00
|
|
|
sc->next_transition_time =
|
|
|
|
pit_get_next_transition_time(sc, sc->count_load_time);
|
|
|
|
}
|
|
|
|
|
2012-08-14 16:24:47 +08:00
|
|
|
static void kvm_pit_put(PITCommonState *pit)
|
2012-03-03 03:28:49 +08:00
|
|
|
{
|
2012-11-26 01:13:38 +08:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2012-03-03 03:28:49 +08:00
|
|
|
struct kvm_pit_state2 kpit;
|
|
|
|
struct kvm_pit_channel_state *kchan;
|
|
|
|
struct PITChannelState *sc;
|
|
|
|
int i, ret;
|
|
|
|
|
2012-08-14 16:24:47 +08:00
|
|
|
/* The offset keeps changing as long as the VM is stopped. */
|
|
|
|
if (s->vm_stopped) {
|
|
|
|
kvm_pit_update_clock_offset(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
kpit.flags = pit->channels[0].irq_disabled ? KVM_PIT_FLAGS_HPET_LEGACY : 0;
|
2012-03-03 03:28:49 +08:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
kchan = &kpit.channels[i];
|
2012-08-14 16:24:47 +08:00
|
|
|
sc = &pit->channels[i];
|
2012-03-03 03:28:49 +08:00
|
|
|
kchan->count = sc->count;
|
|
|
|
kchan->latched_count = sc->latched_count;
|
|
|
|
kchan->count_latched = sc->count_latched;
|
|
|
|
kchan->status_latched = sc->status_latched;
|
|
|
|
kchan->status = sc->status;
|
|
|
|
kchan->read_state = sc->read_state;
|
|
|
|
kchan->write_state = sc->write_state;
|
|
|
|
kchan->write_latch = sc->write_latch;
|
|
|
|
kchan->rw_mode = sc->rw_mode;
|
|
|
|
kchan->mode = sc->mode;
|
|
|
|
kchan->bcd = sc->bcd;
|
|
|
|
kchan->gate = sc->gate;
|
2012-08-14 16:24:47 +08:00
|
|
|
kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = kvm_vm_ioctl(kvm_state,
|
|
|
|
kvm_has_pit_state2() ? KVM_SET_PIT2 : KVM_SET_PIT,
|
|
|
|
&kpit);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "%s failed: %s\n",
|
|
|
|
kvm_has_pit_state2() ? "KVM_SET_PIT2" : "KVM_SET_PIT",
|
|
|
|
strerror(ret));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_set_gate(PITCommonState *s, PITChannelState *sc, int val)
|
|
|
|
{
|
|
|
|
kvm_pit_get(s);
|
|
|
|
|
|
|
|
switch (sc->mode) {
|
|
|
|
default:
|
|
|
|
case 0:
|
|
|
|
case 4:
|
|
|
|
/* XXX: just disable/enable counting */
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
case 5:
|
|
|
|
if (sc->gate < val) {
|
|
|
|
/* restart counting on rising edge */
|
|
|
|
sc->count_load_time = qemu_get_clock_ns(vm_clock);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sc->gate = val;
|
|
|
|
|
|
|
|
kvm_pit_put(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_get_channel_info(PITCommonState *s, PITChannelState *sc,
|
|
|
|
PITChannelInfo *info)
|
|
|
|
{
|
|
|
|
kvm_pit_get(s);
|
|
|
|
|
|
|
|
pit_get_channel_info_common(s, sc, info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_reset(DeviceState *dev)
|
|
|
|
{
|
2012-11-26 01:13:38 +08:00
|
|
|
PITCommonState *s = PIT_COMMON(dev);
|
2012-03-03 03:28:49 +08:00
|
|
|
|
|
|
|
pit_reset_common(s);
|
|
|
|
|
|
|
|
kvm_pit_put(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_irq_control(void *opaque, int n, int enable)
|
|
|
|
{
|
|
|
|
PITCommonState *pit = opaque;
|
|
|
|
PITChannelState *s = &pit->channels[0];
|
|
|
|
|
|
|
|
kvm_pit_get(pit);
|
|
|
|
|
|
|
|
s->irq_disabled = !enable;
|
|
|
|
|
|
|
|
kvm_pit_put(pit);
|
|
|
|
}
|
|
|
|
|
2012-06-06 22:28:42 +08:00
|
|
|
static void kvm_pit_vm_state_change(void *opaque, int running,
|
|
|
|
RunState state)
|
|
|
|
{
|
|
|
|
KVMPITState *s = opaque;
|
|
|
|
|
|
|
|
if (running) {
|
2012-08-14 16:24:03 +08:00
|
|
|
kvm_pit_update_clock_offset(s);
|
|
|
|
s->vm_stopped = false;
|
2012-06-06 22:28:42 +08:00
|
|
|
} else {
|
2012-08-14 16:24:03 +08:00
|
|
|
kvm_pit_update_clock_offset(s);
|
2012-11-26 01:13:38 +08:00
|
|
|
kvm_pit_get(PIT_COMMON(s));
|
2012-08-14 16:24:03 +08:00
|
|
|
s->vm_stopped = true;
|
2012-06-06 22:28:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 01:47:58 +08:00
|
|
|
static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
|
2012-03-03 03:28:49 +08:00
|
|
|
{
|
2012-11-26 01:47:58 +08:00
|
|
|
PITCommonState *pit = PIT_COMMON(dev);
|
|
|
|
KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
|
2012-11-26 01:13:38 +08:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2012-03-03 03:28:49 +08:00
|
|
|
struct kvm_pit_config config = {
|
|
|
|
.flags = 0,
|
|
|
|
};
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (kvm_check_extension(kvm_state, KVM_CAP_PIT2)) {
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT2, &config);
|
|
|
|
} else {
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
|
|
|
|
}
|
|
|
|
if (ret < 0) {
|
2012-11-26 01:47:58 +08:00
|
|
|
error_setg(errp, "Create kernel PIC irqchip failed: %s",
|
|
|
|
strerror(ret));
|
|
|
|
return;
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
switch (s->lost_tick_policy) {
|
|
|
|
case LOST_TICK_DELAY:
|
|
|
|
break; /* enabled by default */
|
|
|
|
case LOST_TICK_DISCARD:
|
|
|
|
if (kvm_check_extension(kvm_state, KVM_CAP_REINJECT_CONTROL)) {
|
|
|
|
struct kvm_reinject_control control = { .pit_reinject = 0 };
|
|
|
|
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
|
|
|
|
if (ret < 0) {
|
2012-11-26 01:47:58 +08:00
|
|
|
error_setg(errp,
|
|
|
|
"Can't disable in-kernel PIT reinjection: %s",
|
|
|
|
strerror(ret));
|
|
|
|
return;
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-11-26 01:47:58 +08:00
|
|
|
error_setg(errp, "Lost tick policy not supported.");
|
|
|
|
return;
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 17:41:28 +08:00
|
|
|
memory_region_init_reservation(&pit->ioports, NULL, "kvm-pit", 4);
|
2012-03-03 03:28:49 +08:00
|
|
|
|
2012-11-26 01:47:58 +08:00
|
|
|
qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
|
2012-03-03 03:28:49 +08:00
|
|
|
|
2012-06-06 22:28:42 +08:00
|
|
|
qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
|
|
|
|
|
2012-11-26 01:47:58 +08:00
|
|
|
kpc->parent_realize(dev, errp);
|
2012-03-03 03:28:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property kvm_pit_properties[] = {
|
2012-11-26 01:13:38 +08:00
|
|
|
DEFINE_PROP_HEX32("iobase", PITCommonState, iobase, -1),
|
2012-03-03 03:28:49 +08:00
|
|
|
DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
|
|
|
|
lost_tick_policy, LOST_TICK_DELAY),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_pit_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2012-11-26 01:47:58 +08:00
|
|
|
KVMPITClass *kpc = KVM_PIT_CLASS(klass);
|
2012-03-03 03:28:49 +08:00
|
|
|
PITCommonClass *k = PIT_COMMON_CLASS(klass);
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
2012-11-26 01:47:58 +08:00
|
|
|
kpc->parent_realize = dc->realize;
|
|
|
|
dc->realize = kvm_pit_realizefn;
|
2012-03-03 03:28:49 +08:00
|
|
|
k->set_channel_gate = kvm_pit_set_gate;
|
|
|
|
k->get_channel_info = kvm_pit_get_channel_info;
|
|
|
|
k->pre_save = kvm_pit_get;
|
|
|
|
k->post_load = kvm_pit_put;
|
|
|
|
dc->reset = kvm_pit_reset;
|
|
|
|
dc->props = kvm_pit_properties;
|
|
|
|
}
|
|
|
|
|
2013-01-10 23:19:07 +08:00
|
|
|
static const TypeInfo kvm_pit_info = {
|
2012-11-26 01:13:38 +08:00
|
|
|
.name = TYPE_KVM_I8254,
|
2012-03-03 03:28:49 +08:00
|
|
|
.parent = TYPE_PIT_COMMON,
|
|
|
|
.instance_size = sizeof(KVMPITState),
|
|
|
|
.class_init = kvm_pit_class_init,
|
2012-11-26 01:47:58 +08:00
|
|
|
.class_size = sizeof(KVMPITClass),
|
2012-03-03 03:28:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_pit_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_pit_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(kvm_pit_register)
|