2012-03-02 23:28:49 +04: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.
|
|
|
|
*/
|
2016-06-22 20:11:19 +03:00
|
|
|
|
2016-01-26 21:17:03 +03:00
|
|
|
#include "qemu/osdep.h"
|
2016-06-22 20:11:19 +03:00
|
|
|
#include <linux/kvm.h>
|
2020-09-13 22:53:41 +03:00
|
|
|
#include "qapi/qapi-types-machine.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2019-08-12 08:23:59 +03:00
|
|
|
#include "sysemu/runstate.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/timer/i8254.h"
|
|
|
|
#include "hw/timer/i8254_internal.h"
|
2020-12-12 01:05:12 +03:00
|
|
|
#include "hw/qdev-properties-system.h"
|
2012-12-17 21:20:04 +04:00
|
|
|
#include "sysemu/kvm.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2012-03-02 23:28:49 +04:00
|
|
|
|
|
|
|
#define KVM_PIT_REINJECT_BIT 0
|
|
|
|
|
2012-06-06 18:28:42 +04:00
|
|
|
#define CALIBRATION_ROUNDS 3
|
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
typedef struct KVMPITClass KVMPITClass;
|
|
|
|
typedef struct KVMPITState KVMPITState;
|
2020-09-01 00:07:33 +03:00
|
|
|
DECLARE_OBJ_CHECKERS(KVMPITState, KVMPITClass,
|
|
|
|
KVM_PIT, TYPE_KVM_I8254)
|
2012-11-25 21:13:38 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct KVMPITState {
|
2012-11-25 21:13:38 +04:00
|
|
|
PITCommonState parent_obj;
|
|
|
|
|
2012-03-02 23:28:49 +04:00
|
|
|
LostTickPolicy lost_tick_policy;
|
2012-08-14 12:24:03 +04:00
|
|
|
bool vm_stopped;
|
|
|
|
int64_t kernel_clock_offset;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2012-03-02 23:28:49 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct KVMPITClass {
|
2012-11-25 21:47:58 +04:00
|
|
|
PITCommonClass parent_class;
|
|
|
|
|
|
|
|
DeviceRealize parent_realize;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2012-11-25 21:47:58 +04:00
|
|
|
|
2012-08-14 12:24:03 +04:00
|
|
|
static void kvm_pit_update_clock_offset(KVMPITState *s)
|
2012-06-06 18:28:42 +04:00
|
|
|
{
|
|
|
|
int64_t offset, clock_offset;
|
|
|
|
struct timespec ts;
|
2012-08-14 12:24:03 +04:00
|
|
|
int i;
|
2012-06-06 18:28:42 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Measure the delta between CLOCK_MONOTONIC, the base used for
|
2013-08-21 19:03:08 +04:00
|
|
|
* kvm_pit_channel_state::count_load_time, and QEMU_CLOCK_VIRTUAL. Take the
|
2012-06-06 18:28:42 +04:00
|
|
|
* minimum of several samples to filter out scheduling noise.
|
|
|
|
*/
|
|
|
|
clock_offset = INT64_MAX;
|
|
|
|
for (i = 0; i < CALIBRATION_ROUNDS; i++) {
|
2013-08-21 19:03:08 +04:00
|
|
|
offset = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2012-06-06 18:28:42 +04:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
offset -= ts.tv_nsec;
|
|
|
|
offset -= (int64_t)ts.tv_sec * 1000000000;
|
2021-09-10 14:26:06 +03:00
|
|
|
if (uabs64(offset) < uabs64(clock_offset)) {
|
2012-06-06 18:28:42 +04:00
|
|
|
clock_offset = offset;
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 12:24:03 +04:00
|
|
|
s->kernel_clock_offset = clock_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void kvm_pit_get(PITCommonState *pit)
|
|
|
|
{
|
2012-11-25 21:13:38 +04:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2012-08-14 12:24:03 +04: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 18:28:42 +04:00
|
|
|
|
2012-03-02 23:28:49 +04:00
|
|
|
if (kvm_has_pit_state2()) {
|
|
|
|
ret = kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, &kpit);
|
|
|
|
if (ret < 0) {
|
2021-05-19 14:35:28 +03:00
|
|
|
fprintf(stderr, "KVM_GET_PIT2 failed: %s\n", strerror(-ret));
|
2012-03-02 23:28:49 +04:00
|
|
|
abort();
|
|
|
|
}
|
2012-06-06 18:28:42 +04:00
|
|
|
pit->channels[0].irq_disabled = kpit.flags & KVM_PIT_FLAGS_HPET_LEGACY;
|
2012-03-02 23:28:49 +04: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) {
|
2021-05-19 14:35:28 +03:00
|
|
|
fprintf(stderr, "KVM_GET_PIT failed: %s\n", strerror(-ret));
|
2012-03-02 23:28:49 +04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
kchan = &kpit.channels[i];
|
2012-06-06 18:28:42 +04:00
|
|
|
sc = &pit->channels[i];
|
2012-03-02 23:28:49 +04: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 12:24:03 +04:00
|
|
|
sc->count_load_time = kchan->count_load_time + s->kernel_clock_offset;
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
|
2012-06-06 18:28:42 +04:00
|
|
|
sc = &pit->channels[0];
|
2012-03-02 23:28:49 +04:00
|
|
|
sc->next_transition_time =
|
|
|
|
pit_get_next_transition_time(sc, sc->count_load_time);
|
|
|
|
}
|
|
|
|
|
2012-08-14 12:24:47 +04:00
|
|
|
static void kvm_pit_put(PITCommonState *pit)
|
2012-03-02 23:28:49 +04:00
|
|
|
{
|
2012-11-25 21:13:38 +04:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2014-10-14 13:58:34 +04:00
|
|
|
struct kvm_pit_state2 kpit = {};
|
2012-03-02 23:28:49 +04:00
|
|
|
struct kvm_pit_channel_state *kchan;
|
|
|
|
struct PITChannelState *sc;
|
|
|
|
int i, ret;
|
|
|
|
|
2012-08-14 12:24:47 +04: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-02 23:28:49 +04:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
kchan = &kpit.channels[i];
|
2012-08-14 12:24:47 +04:00
|
|
|
sc = &pit->channels[i];
|
2012-03-02 23:28:49 +04: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 12:24:47 +04:00
|
|
|
kchan->count_load_time = sc->count_load_time - s->kernel_clock_offset;
|
2012-03-02 23:28:49 +04: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",
|
2021-05-19 14:35:28 +03:00
|
|
|
strerror(-ret));
|
2012-03-02 23:28:49 +04:00
|
|
|
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 */
|
2013-08-21 19:03:08 +04:00
|
|
|
sc->count_load_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
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-25 21:13:38 +04:00
|
|
|
PITCommonState *s = PIT_COMMON(dev);
|
2012-03-02 23:28:49 +04: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);
|
|
|
|
}
|
|
|
|
|
2021-01-11 18:20:20 +03:00
|
|
|
static void kvm_pit_vm_state_change(void *opaque, bool running,
|
2012-06-06 18:28:42 +04:00
|
|
|
RunState state)
|
|
|
|
{
|
|
|
|
KVMPITState *s = opaque;
|
|
|
|
|
|
|
|
if (running) {
|
2012-08-14 12:24:03 +04:00
|
|
|
kvm_pit_update_clock_offset(s);
|
2014-09-16 11:26:32 +04:00
|
|
|
kvm_pit_put(PIT_COMMON(s));
|
2012-08-14 12:24:03 +04:00
|
|
|
s->vm_stopped = false;
|
2012-06-06 18:28:42 +04:00
|
|
|
} else {
|
2012-08-14 12:24:03 +04:00
|
|
|
kvm_pit_update_clock_offset(s);
|
2012-11-25 21:13:38 +04:00
|
|
|
kvm_pit_get(PIT_COMMON(s));
|
2012-08-14 12:24:03 +04:00
|
|
|
s->vm_stopped = true;
|
2012-06-06 18:28:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-25 21:47:58 +04:00
|
|
|
static void kvm_pit_realizefn(DeviceState *dev, Error **errp)
|
2012-03-02 23:28:49 +04:00
|
|
|
{
|
2012-11-25 21:47:58 +04:00
|
|
|
PITCommonState *pit = PIT_COMMON(dev);
|
|
|
|
KVMPITClass *kpc = KVM_PIT_GET_CLASS(dev);
|
2012-11-25 21:13:38 +04:00
|
|
|
KVMPITState *s = KVM_PIT(pit);
|
2012-03-02 23:28:49 +04: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-25 21:47:58 +04:00
|
|
|
error_setg(errp, "Create kernel PIC irqchip failed: %s",
|
2021-05-19 14:35:28 +03:00
|
|
|
strerror(-ret));
|
2012-11-25 21:47:58 +04:00
|
|
|
return;
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
switch (s->lost_tick_policy) {
|
2014-02-08 14:01:55 +04:00
|
|
|
case LOST_TICK_POLICY_DELAY:
|
2012-03-02 23:28:49 +04:00
|
|
|
break; /* enabled by default */
|
2014-02-08 14:01:55 +04:00
|
|
|
case LOST_TICK_POLICY_DISCARD:
|
2012-03-02 23:28:49 +04:00
|
|
|
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-25 21:47:58 +04:00
|
|
|
error_setg(errp,
|
|
|
|
"Can't disable in-kernel PIT reinjection: %s",
|
2021-05-19 14:35:28 +03:00
|
|
|
strerror(-ret));
|
2012-11-25 21:47:58 +04:00
|
|
|
return;
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2012-11-25 21:47:58 +04:00
|
|
|
error_setg(errp, "Lost tick policy not supported.");
|
|
|
|
return;
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
|
2018-05-15 17:35:16 +03:00
|
|
|
memory_region_init_io(&pit->ioports, OBJECT(dev), NULL, NULL, "kvm-pit", 4);
|
2012-03-02 23:28:49 +04:00
|
|
|
|
2012-11-25 21:47:58 +04:00
|
|
|
qdev_init_gpio_in(dev, kvm_pit_irq_control, 1);
|
2012-03-02 23:28:49 +04:00
|
|
|
|
2012-06-06 18:28:42 +04:00
|
|
|
qemu_add_vm_change_state_handler(kvm_pit_vm_state_change, s);
|
|
|
|
|
2012-11-25 21:47:58 +04:00
|
|
|
kpc->parent_realize(dev, errp);
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static Property kvm_pit_properties[] = {
|
2014-02-08 14:01:53 +04:00
|
|
|
DEFINE_PROP_UINT32("iobase", PITCommonState, iobase, -1),
|
2012-03-02 23:28:49 +04:00
|
|
|
DEFINE_PROP_LOSTTICKPOLICY("lost_tick_policy", KVMPITState,
|
2014-02-08 14:01:55 +04:00
|
|
|
lost_tick_policy, LOST_TICK_POLICY_DELAY),
|
2012-03-02 23:28:49 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_pit_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2012-11-25 21:47:58 +04:00
|
|
|
KVMPITClass *kpc = KVM_PIT_CLASS(klass);
|
2012-03-02 23:28:49 +04:00
|
|
|
PITCommonClass *k = PIT_COMMON_CLASS(klass);
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
2018-01-14 05:04:12 +03:00
|
|
|
device_class_set_parent_realize(dc, kvm_pit_realizefn,
|
|
|
|
&kpc->parent_realize);
|
2012-03-02 23:28:49 +04:00
|
|
|
k->set_channel_gate = kvm_pit_set_gate;
|
|
|
|
k->get_channel_info = kvm_pit_get_channel_info;
|
|
|
|
dc->reset = kvm_pit_reset;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, kvm_pit_properties);
|
2012-03-02 23:28:49 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo kvm_pit_info = {
|
2012-11-25 21:13:38 +04:00
|
|
|
.name = TYPE_KVM_I8254,
|
2012-03-02 23:28:49 +04:00
|
|
|
.parent = TYPE_PIT_COMMON,
|
|
|
|
.instance_size = sizeof(KVMPITState),
|
|
|
|
.class_init = kvm_pit_class_init,
|
2012-11-25 21:47:58 +04:00
|
|
|
.class_size = sizeof(KVMPITClass),
|
2012-03-02 23:28:49 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_pit_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_pit_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(kvm_pit_register)
|