2016-10-04 15:28:09 +03:00
|
|
|
/*
|
|
|
|
* KVM-based ITS implementation for a GICv3-based system
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Samsung Electronics Co., Ltd.
|
|
|
|
* Written by Pavel Fedin <p.fedin@samsung.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2020-10-23 15:29:13 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2016-10-04 15:28:09 +03:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qapi/error.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2023-12-19 20:57:44 +03:00
|
|
|
#include "qemu/error-report.h"
|
2016-10-04 15:28:09 +03:00
|
|
|
#include "hw/intc/arm_gicv3_its_common.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 08:23:59 +03:00
|
|
|
#include "sysemu/runstate.h"
|
2016-10-04 15:28:09 +03:00
|
|
|
#include "sysemu/kvm.h"
|
|
|
|
#include "kvm_arm.h"
|
2017-04-06 13:00:28 +03:00
|
|
|
#include "migration/blocker.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2016-10-04 15:28:09 +03:00
|
|
|
|
|
|
|
#define TYPE_KVM_ARM_ITS "arm-its-kvm"
|
2020-09-03 23:43:22 +03:00
|
|
|
typedef struct KVMARMITSClass KVMARMITSClass;
|
2020-09-01 00:07:36 +03:00
|
|
|
/* This is reusing the GICv3ITSState typedef from ARM_GICV3_ITS_COMMON */
|
|
|
|
DECLARE_OBJ_CHECKERS(GICv3ITSState, KVMARMITSClass,
|
|
|
|
KVM_ARM_ITS, TYPE_KVM_ARM_ITS)
|
2017-12-13 20:59:22 +03:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct KVMARMITSClass {
|
2017-12-13 20:59:22 +03:00
|
|
|
GICv3ITSCommonClass parent_class;
|
2022-12-14 17:27:13 +03:00
|
|
|
ResettablePhases parent_phases;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2017-12-13 20:59:22 +03:00
|
|
|
|
2016-10-04 15:28:09 +03:00
|
|
|
|
|
|
|
static int kvm_its_send_msi(GICv3ITSState *s, uint32_t value, uint16_t devid)
|
|
|
|
{
|
|
|
|
struct kvm_msi msi;
|
|
|
|
|
|
|
|
if (unlikely(!s->translater_gpa_known)) {
|
|
|
|
MemoryRegion *mr = &s->iomem_its_translation;
|
|
|
|
MemoryRegionSection mrs;
|
|
|
|
|
|
|
|
mrs = memory_region_find(mr, 0, 1);
|
|
|
|
memory_region_unref(mrs.mr);
|
|
|
|
s->gits_translater_gpa = mrs.offset_within_address_space + 0x40;
|
|
|
|
s->translater_gpa_known = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
msi.address_lo = extract64(s->gits_translater_gpa, 0, 32);
|
|
|
|
msi.address_hi = extract64(s->gits_translater_gpa, 32, 32);
|
|
|
|
msi.data = le32_to_cpu(value);
|
|
|
|
msi.flags = KVM_MSI_VALID_DEVID;
|
|
|
|
msi.devid = devid;
|
|
|
|
memset(msi.pad, 0, sizeof(msi.pad));
|
|
|
|
|
|
|
|
return kvm_vm_ioctl(kvm_state, KVM_SIGNAL_MSI, &msi);
|
|
|
|
}
|
|
|
|
|
2017-06-13 16:57:00 +03:00
|
|
|
/**
|
|
|
|
* vm_change_state_handler - VM change state callback aiming at flushing
|
|
|
|
* ITS tables into guest RAM
|
|
|
|
*
|
|
|
|
* The tables get flushed to guest RAM whenever the VM gets stopped.
|
|
|
|
*/
|
2021-01-11 18:20:20 +03:00
|
|
|
static void vm_change_state_handler(void *opaque, bool running,
|
2017-06-13 16:57:00 +03:00
|
|
|
RunState state)
|
|
|
|
{
|
|
|
|
GICv3ITSState *s = (GICv3ITSState *)opaque;
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 16:03:52 +03:00
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
|
|
|
KVM_DEV_ARM_ITS_SAVE_TABLES, NULL, true, &err);
|
2017-06-13 16:57:00 +03:00
|
|
|
if (err) {
|
|
|
|
error_report_err(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 15:28:09 +03:00
|
|
|
static void kvm_arm_its_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
GICv3ITSState *s = ARM_GICV3_ITS_COMMON(dev);
|
2017-01-16 14:31:53 +03:00
|
|
|
|
2016-10-04 15:28:09 +03:00
|
|
|
s->dev_fd = kvm_create_device(kvm_state, KVM_DEV_TYPE_ARM_VGIC_ITS, false);
|
|
|
|
if (s->dev_fd < 0) {
|
|
|
|
error_setg_errno(errp, -s->dev_fd, "error creating in-kernel ITS");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* explicit init of the ITS */
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
2017-06-13 16:57:00 +03:00
|
|
|
KVM_DEV_ARM_VGIC_CTRL_INIT, NULL, true, &error_abort);
|
2016-10-04 15:28:09 +03:00
|
|
|
|
|
|
|
/* register the base address */
|
|
|
|
kvm_arm_register_device(&s->iomem_its_cntrl, -1, KVM_DEV_ARM_VGIC_GRP_ADDR,
|
2018-06-22 15:28:35 +03:00
|
|
|
KVM_VGIC_ITS_ADDR_TYPE, s->dev_fd, 0);
|
2016-10-04 15:28:09 +03:00
|
|
|
|
2022-04-08 17:15:24 +03:00
|
|
|
gicv3_add_its(s->gicv3, dev);
|
|
|
|
|
2021-09-13 18:07:23 +03:00
|
|
|
gicv3_its_init_mmio(s, NULL, NULL);
|
2016-10-04 15:28:09 +03:00
|
|
|
|
2017-06-13 16:57:01 +03:00
|
|
|
if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CTLR)) {
|
|
|
|
error_setg(&s->migration_blocker, "This operating system kernel "
|
|
|
|
"does not support vITS migration");
|
2023-10-18 16:03:36 +03:00
|
|
|
if (migrate_add_blocker(&s->migration_blocker, errp) < 0) {
|
2017-06-13 16:57:01 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-11-07 16:03:51 +03:00
|
|
|
} else {
|
|
|
|
qemu_add_vm_change_state_handler(vm_change_state_handler, s);
|
2017-06-13 16:57:01 +03:00
|
|
|
}
|
|
|
|
|
2016-10-04 15:28:09 +03:00
|
|
|
kvm_msi_use_devid = true;
|
|
|
|
kvm_gsi_direct_mapping = false;
|
2023-10-17 14:34:50 +03:00
|
|
|
kvm_msi_via_irqfd_allowed = true;
|
2016-10-04 15:28:09 +03:00
|
|
|
}
|
|
|
|
|
2017-06-13 16:57:00 +03:00
|
|
|
/**
|
|
|
|
* kvm_arm_its_pre_save - handles the saving of ITS registers.
|
|
|
|
* ITS tables are flushed into guest RAM separately and earlier,
|
|
|
|
* through the VM change state handler, since at the moment pre_save()
|
|
|
|
* is called, the guest RAM has already been saved.
|
|
|
|
*/
|
|
|
|
static void kvm_arm_its_pre_save(GICv3ITSState *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_BASER + i * 8, &s->baser[i], false,
|
|
|
|
&error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CTLR, &s->ctlr, false, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CBASER, &s->cbaser, false, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CREADR, &s->creadr, false, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CWRITER, &s->cwriter, false, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_IIDR, &s->iidr, false, &error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* kvm_arm_its_post_load - Restore both the ITS registers and tables
|
|
|
|
*/
|
|
|
|
static void kvm_arm_its_post_load(GICv3ITSState *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_IIDR, &s->iidr, true, &error_abort);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* must be written before GITS_CREADR since GITS_CBASER write
|
|
|
|
* access resets GITS_CREADR.
|
|
|
|
*/
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CBASER, &s->cbaser, true, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CREADR, &s->creadr, true, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CWRITER, &s->cwriter, true, &error_abort);
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_BASER + i * 8, &s->baser[i], true,
|
|
|
|
&error_abort);
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
|
|
|
KVM_DEV_ARM_ITS_RESTORE_TABLES, NULL, true,
|
|
|
|
&error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CTLR, &s->ctlr, true, &error_abort);
|
|
|
|
}
|
|
|
|
|
2024-04-12 19:08:07 +03:00
|
|
|
static void kvm_arm_its_reset_hold(Object *obj, ResetType type)
|
2017-12-13 20:59:22 +03:00
|
|
|
{
|
2022-12-14 17:27:13 +03:00
|
|
|
GICv3ITSState *s = ARM_GICV3_ITS_COMMON(obj);
|
2017-12-13 20:59:22 +03:00
|
|
|
KVMARMITSClass *c = KVM_ARM_ITS_GET_CLASS(s);
|
|
|
|
int i;
|
|
|
|
|
2022-12-14 17:27:13 +03:00
|
|
|
if (c->parent_phases.hold) {
|
2024-04-12 19:08:07 +03:00
|
|
|
c->parent_phases.hold(obj, type);
|
2022-12-14 17:27:13 +03:00
|
|
|
}
|
2017-12-13 20:59:22 +03:00
|
|
|
|
2017-12-13 20:59:23 +03:00
|
|
|
if (kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
|
|
|
KVM_DEV_ARM_ITS_CTRL_RESET)) {
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_CTRL,
|
|
|
|
KVM_DEV_ARM_ITS_CTRL_RESET, NULL, true, &error_abort);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-20 13:24:31 +03:00
|
|
|
warn_report("ITS KVM: full reset is not supported by the host kernel");
|
2017-12-13 20:59:22 +03:00
|
|
|
|
|
|
|
if (!kvm_device_check_attr(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CTLR)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CTLR, &s->ctlr, true, &error_abort);
|
|
|
|
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_CBASER, &s->cbaser, true, &error_abort);
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
kvm_device_access(s->dev_fd, KVM_DEV_ARM_VGIC_GRP_ITS_REGS,
|
|
|
|
GITS_BASER + i * 8, &s->baser[i], true,
|
|
|
|
&error_abort);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-07 15:54:51 +03:00
|
|
|
static Property kvm_arm_its_props[] = {
|
|
|
|
DEFINE_PROP_LINK("parent-gicv3", GICv3ITSState, gicv3, "kvm-arm-gicv3",
|
|
|
|
GICv3State *),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2016-10-04 15:28:09 +03:00
|
|
|
static void kvm_arm_its_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2022-12-14 17:27:13 +03:00
|
|
|
ResettableClass *rc = RESETTABLE_CLASS(klass);
|
2016-10-04 15:28:09 +03:00
|
|
|
GICv3ITSCommonClass *icc = ARM_GICV3_ITS_COMMON_CLASS(klass);
|
2017-12-13 20:59:22 +03:00
|
|
|
KVMARMITSClass *ic = KVM_ARM_ITS_CLASS(klass);
|
2016-10-04 15:28:09 +03:00
|
|
|
|
|
|
|
dc->realize = kvm_arm_its_realize;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, kvm_arm_its_props);
|
2022-12-14 17:27:13 +03:00
|
|
|
resettable_class_set_parent_phases(rc, NULL, kvm_arm_its_reset_hold, NULL,
|
|
|
|
&ic->parent_phases);
|
2016-10-04 15:28:09 +03:00
|
|
|
icc->send_msi = kvm_its_send_msi;
|
2017-06-13 16:57:00 +03:00
|
|
|
icc->pre_save = kvm_arm_its_pre_save;
|
|
|
|
icc->post_load = kvm_arm_its_post_load;
|
2016-10-04 15:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo kvm_arm_its_info = {
|
|
|
|
.name = TYPE_KVM_ARM_ITS,
|
|
|
|
.parent = TYPE_ARM_GICV3_ITS_COMMON,
|
|
|
|
.instance_size = sizeof(GICv3ITSState),
|
|
|
|
.class_init = kvm_arm_its_class_init,
|
2017-12-13 20:59:22 +03:00
|
|
|
.class_size = sizeof(KVMARMITSClass),
|
2016-10-04 15:28:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void kvm_arm_its_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&kvm_arm_its_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(kvm_arm_its_register_types)
|